xref: /OK3568_Linux_fs/kernel/drivers/firmware/efi/capsule.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * EFI capsule support.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright 2013 Intel Corporation; author Matt Fleming
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #define pr_fmt(fmt) "efi: " fmt
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/slab.h>
11*4882a593Smuzhiyun #include <linux/mutex.h>
12*4882a593Smuzhiyun #include <linux/highmem.h>
13*4882a593Smuzhiyun #include <linux/efi.h>
14*4882a593Smuzhiyun #include <linux/vmalloc.h>
15*4882a593Smuzhiyun #include <asm/io.h>
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun typedef struct {
18*4882a593Smuzhiyun 	u64 length;
19*4882a593Smuzhiyun 	u64 data;
20*4882a593Smuzhiyun } efi_capsule_block_desc_t;
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun static bool capsule_pending;
23*4882a593Smuzhiyun static bool stop_capsules;
24*4882a593Smuzhiyun static int efi_reset_type = -1;
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun /*
27*4882a593Smuzhiyun  * capsule_mutex serialises access to both capsule_pending and
28*4882a593Smuzhiyun  * efi_reset_type and stop_capsules.
29*4882a593Smuzhiyun  */
30*4882a593Smuzhiyun static DEFINE_MUTEX(capsule_mutex);
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun /**
33*4882a593Smuzhiyun  * efi_capsule_pending - has a capsule been passed to the firmware?
34*4882a593Smuzhiyun  * @reset_type: store the type of EFI reset if capsule is pending
35*4882a593Smuzhiyun  *
36*4882a593Smuzhiyun  * To ensure that the registered capsule is processed correctly by the
37*4882a593Smuzhiyun  * firmware we need to perform a specific type of reset. If a capsule is
38*4882a593Smuzhiyun  * pending return the reset type in @reset_type.
39*4882a593Smuzhiyun  *
40*4882a593Smuzhiyun  * This function will race with callers of efi_capsule_update(), for
41*4882a593Smuzhiyun  * example, calling this function while somebody else is in
42*4882a593Smuzhiyun  * efi_capsule_update() but hasn't reached efi_capsue_update_locked()
43*4882a593Smuzhiyun  * will miss the updates to capsule_pending and efi_reset_type after
44*4882a593Smuzhiyun  * efi_capsule_update_locked() completes.
45*4882a593Smuzhiyun  *
46*4882a593Smuzhiyun  * A non-racy use is from platform reboot code because we use
47*4882a593Smuzhiyun  * system_state to ensure no capsules can be sent to the firmware once
48*4882a593Smuzhiyun  * we're at SYSTEM_RESTART. See efi_capsule_update_locked().
49*4882a593Smuzhiyun  */
efi_capsule_pending(int * reset_type)50*4882a593Smuzhiyun bool efi_capsule_pending(int *reset_type)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun 	if (!capsule_pending)
53*4882a593Smuzhiyun 		return false;
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	if (reset_type)
56*4882a593Smuzhiyun 		*reset_type = efi_reset_type;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	return true;
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun /*
62*4882a593Smuzhiyun  * Whitelist of EFI capsule flags that we support.
63*4882a593Smuzhiyun  *
64*4882a593Smuzhiyun  * We do not handle EFI_CAPSULE_INITIATE_RESET because that would
65*4882a593Smuzhiyun  * require us to prepare the kernel for reboot. Refuse to load any
66*4882a593Smuzhiyun  * capsules with that flag and any other flags that we do not know how
67*4882a593Smuzhiyun  * to handle.
68*4882a593Smuzhiyun  */
69*4882a593Smuzhiyun #define EFI_CAPSULE_SUPPORTED_FLAG_MASK			\
70*4882a593Smuzhiyun 	(EFI_CAPSULE_PERSIST_ACROSS_RESET | EFI_CAPSULE_POPULATE_SYSTEM_TABLE)
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun /**
73*4882a593Smuzhiyun  * efi_capsule_supported - does the firmware support the capsule?
74*4882a593Smuzhiyun  * @guid: vendor guid of capsule
75*4882a593Smuzhiyun  * @flags: capsule flags
76*4882a593Smuzhiyun  * @size: size of capsule data
77*4882a593Smuzhiyun  * @reset: the reset type required for this capsule
78*4882a593Smuzhiyun  *
79*4882a593Smuzhiyun  * Check whether a capsule with @flags is supported by the firmware
80*4882a593Smuzhiyun  * and that @size doesn't exceed the maximum size for a capsule.
81*4882a593Smuzhiyun  *
82*4882a593Smuzhiyun  * No attempt is made to check @reset against the reset type required
83*4882a593Smuzhiyun  * by any pending capsules because of the races involved.
84*4882a593Smuzhiyun  */
efi_capsule_supported(efi_guid_t guid,u32 flags,size_t size,int * reset)85*4882a593Smuzhiyun int efi_capsule_supported(efi_guid_t guid, u32 flags, size_t size, int *reset)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun 	efi_capsule_header_t capsule;
88*4882a593Smuzhiyun 	efi_capsule_header_t *cap_list[] = { &capsule };
89*4882a593Smuzhiyun 	efi_status_t status;
90*4882a593Smuzhiyun 	u64 max_size;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	if (flags & ~EFI_CAPSULE_SUPPORTED_FLAG_MASK)
93*4882a593Smuzhiyun 		return -EINVAL;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	capsule.headersize = capsule.imagesize = sizeof(capsule);
96*4882a593Smuzhiyun 	memcpy(&capsule.guid, &guid, sizeof(efi_guid_t));
97*4882a593Smuzhiyun 	capsule.flags = flags;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	status = efi.query_capsule_caps(cap_list, 1, &max_size, reset);
100*4882a593Smuzhiyun 	if (status != EFI_SUCCESS)
101*4882a593Smuzhiyun 		return efi_status_to_err(status);
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	if (size > max_size)
104*4882a593Smuzhiyun 		return -ENOSPC;
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	return 0;
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(efi_capsule_supported);
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun /*
111*4882a593Smuzhiyun  * Every scatter gather list (block descriptor) page must end with a
112*4882a593Smuzhiyun  * continuation pointer. The last continuation pointer of the last
113*4882a593Smuzhiyun  * page must be zero to mark the end of the chain.
114*4882a593Smuzhiyun  */
115*4882a593Smuzhiyun #define SGLIST_PER_PAGE	((PAGE_SIZE / sizeof(efi_capsule_block_desc_t)) - 1)
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun /*
118*4882a593Smuzhiyun  * How many scatter gather list (block descriptor) pages do we need
119*4882a593Smuzhiyun  * to map @count pages?
120*4882a593Smuzhiyun  */
sg_pages_num(unsigned int count)121*4882a593Smuzhiyun static inline unsigned int sg_pages_num(unsigned int count)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun 	return DIV_ROUND_UP(count, SGLIST_PER_PAGE);
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun /**
127*4882a593Smuzhiyun  * efi_capsule_update_locked - pass a single capsule to the firmware
128*4882a593Smuzhiyun  * @capsule: capsule to send to the firmware
129*4882a593Smuzhiyun  * @sg_pages: array of scatter gather (block descriptor) pages
130*4882a593Smuzhiyun  * @reset: the reset type required for @capsule
131*4882a593Smuzhiyun  *
132*4882a593Smuzhiyun  * Since this function must be called under capsule_mutex check
133*4882a593Smuzhiyun  * whether efi_reset_type will conflict with @reset, and atomically
134*4882a593Smuzhiyun  * set it and capsule_pending if a capsule was successfully sent to
135*4882a593Smuzhiyun  * the firmware.
136*4882a593Smuzhiyun  *
137*4882a593Smuzhiyun  * We also check to see if the system is about to restart, and if so,
138*4882a593Smuzhiyun  * abort. This avoids races between efi_capsule_update() and
139*4882a593Smuzhiyun  * efi_capsule_pending().
140*4882a593Smuzhiyun  */
141*4882a593Smuzhiyun static int
efi_capsule_update_locked(efi_capsule_header_t * capsule,struct page ** sg_pages,int reset)142*4882a593Smuzhiyun efi_capsule_update_locked(efi_capsule_header_t *capsule,
143*4882a593Smuzhiyun 			  struct page **sg_pages, int reset)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun 	efi_physical_addr_t sglist_phys;
146*4882a593Smuzhiyun 	efi_status_t status;
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	lockdep_assert_held(&capsule_mutex);
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	/*
151*4882a593Smuzhiyun 	 * If someone has already registered a capsule that requires a
152*4882a593Smuzhiyun 	 * different reset type, we're out of luck and must abort.
153*4882a593Smuzhiyun 	 */
154*4882a593Smuzhiyun 	if (efi_reset_type >= 0 && efi_reset_type != reset) {
155*4882a593Smuzhiyun 		pr_err("Conflicting capsule reset type %d (%d).\n",
156*4882a593Smuzhiyun 		       reset, efi_reset_type);
157*4882a593Smuzhiyun 		return -EINVAL;
158*4882a593Smuzhiyun 	}
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	/*
161*4882a593Smuzhiyun 	 * If the system is getting ready to restart it may have
162*4882a593Smuzhiyun 	 * called efi_capsule_pending() to make decisions (such as
163*4882a593Smuzhiyun 	 * whether to force an EFI reboot), and we're racing against
164*4882a593Smuzhiyun 	 * that call. Abort in that case.
165*4882a593Smuzhiyun 	 */
166*4882a593Smuzhiyun 	if (unlikely(stop_capsules)) {
167*4882a593Smuzhiyun 		pr_warn("Capsule update raced with reboot, aborting.\n");
168*4882a593Smuzhiyun 		return -EINVAL;
169*4882a593Smuzhiyun 	}
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	sglist_phys = page_to_phys(sg_pages[0]);
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 	status = efi.update_capsule(&capsule, 1, sglist_phys);
174*4882a593Smuzhiyun 	if (status == EFI_SUCCESS) {
175*4882a593Smuzhiyun 		capsule_pending = true;
176*4882a593Smuzhiyun 		efi_reset_type = reset;
177*4882a593Smuzhiyun 	}
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	return efi_status_to_err(status);
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun /**
183*4882a593Smuzhiyun  * efi_capsule_update - send a capsule to the firmware
184*4882a593Smuzhiyun  * @capsule: capsule to send to firmware
185*4882a593Smuzhiyun  * @pages: an array of capsule data pages
186*4882a593Smuzhiyun  *
187*4882a593Smuzhiyun  * Build a scatter gather list with EFI capsule block descriptors to
188*4882a593Smuzhiyun  * map the capsule described by @capsule with its data in @pages and
189*4882a593Smuzhiyun  * send it to the firmware via the UpdateCapsule() runtime service.
190*4882a593Smuzhiyun  *
191*4882a593Smuzhiyun  * @capsule must be a virtual mapping of the complete capsule update in the
192*4882a593Smuzhiyun  * kernel address space, as the capsule can be consumed immediately.
193*4882a593Smuzhiyun  * A capsule_header_t that describes the entire contents of the capsule
194*4882a593Smuzhiyun  * must be at the start of the first data page.
195*4882a593Smuzhiyun  *
196*4882a593Smuzhiyun  * Even though this function will validate that the firmware supports
197*4882a593Smuzhiyun  * the capsule guid, users will likely want to check that
198*4882a593Smuzhiyun  * efi_capsule_supported() returns true before calling this function
199*4882a593Smuzhiyun  * because it makes it easier to print helpful error messages.
200*4882a593Smuzhiyun  *
201*4882a593Smuzhiyun  * If the capsule is successfully submitted to the firmware, any
202*4882a593Smuzhiyun  * subsequent calls to efi_capsule_pending() will return true. @pages
203*4882a593Smuzhiyun  * must not be released or modified if this function returns
204*4882a593Smuzhiyun  * successfully.
205*4882a593Smuzhiyun  *
206*4882a593Smuzhiyun  * Callers must be prepared for this function to fail, which can
207*4882a593Smuzhiyun  * happen if we raced with system reboot or if there is already a
208*4882a593Smuzhiyun  * pending capsule that has a reset type that conflicts with the one
209*4882a593Smuzhiyun  * required by @capsule. Do NOT use efi_capsule_pending() to detect
210*4882a593Smuzhiyun  * this conflict since that would be racy. Instead, submit the capsule
211*4882a593Smuzhiyun  * to efi_capsule_update() and check the return value.
212*4882a593Smuzhiyun  *
213*4882a593Smuzhiyun  * Return 0 on success, a converted EFI status code on failure.
214*4882a593Smuzhiyun  */
efi_capsule_update(efi_capsule_header_t * capsule,phys_addr_t * pages)215*4882a593Smuzhiyun int efi_capsule_update(efi_capsule_header_t *capsule, phys_addr_t *pages)
216*4882a593Smuzhiyun {
217*4882a593Smuzhiyun 	u32 imagesize = capsule->imagesize;
218*4882a593Smuzhiyun 	efi_guid_t guid = capsule->guid;
219*4882a593Smuzhiyun 	unsigned int count, sg_count;
220*4882a593Smuzhiyun 	u32 flags = capsule->flags;
221*4882a593Smuzhiyun 	struct page **sg_pages;
222*4882a593Smuzhiyun 	int rv, reset_type;
223*4882a593Smuzhiyun 	int i, j;
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	rv = efi_capsule_supported(guid, flags, imagesize, &reset_type);
226*4882a593Smuzhiyun 	if (rv)
227*4882a593Smuzhiyun 		return rv;
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	count = DIV_ROUND_UP(imagesize, PAGE_SIZE);
230*4882a593Smuzhiyun 	sg_count = sg_pages_num(count);
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	sg_pages = kcalloc(sg_count, sizeof(*sg_pages), GFP_KERNEL);
233*4882a593Smuzhiyun 	if (!sg_pages)
234*4882a593Smuzhiyun 		return -ENOMEM;
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	for (i = 0; i < sg_count; i++) {
237*4882a593Smuzhiyun 		sg_pages[i] = alloc_page(GFP_KERNEL);
238*4882a593Smuzhiyun 		if (!sg_pages[i]) {
239*4882a593Smuzhiyun 			rv = -ENOMEM;
240*4882a593Smuzhiyun 			goto out;
241*4882a593Smuzhiyun 		}
242*4882a593Smuzhiyun 	}
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	for (i = 0; i < sg_count; i++) {
245*4882a593Smuzhiyun 		efi_capsule_block_desc_t *sglist;
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 		sglist = kmap(sg_pages[i]);
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 		for (j = 0; j < SGLIST_PER_PAGE && count > 0; j++) {
250*4882a593Smuzhiyun 			u64 sz = min_t(u64, imagesize,
251*4882a593Smuzhiyun 				       PAGE_SIZE - (u64)*pages % PAGE_SIZE);
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 			sglist[j].length = sz;
254*4882a593Smuzhiyun 			sglist[j].data = *pages++;
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 			imagesize -= sz;
257*4882a593Smuzhiyun 			count--;
258*4882a593Smuzhiyun 		}
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 		/* Continuation pointer */
261*4882a593Smuzhiyun 		sglist[j].length = 0;
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 		if (i + 1 == sg_count)
264*4882a593Smuzhiyun 			sglist[j].data = 0;
265*4882a593Smuzhiyun 		else
266*4882a593Smuzhiyun 			sglist[j].data = page_to_phys(sg_pages[i + 1]);
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 		kunmap(sg_pages[i]);
269*4882a593Smuzhiyun 	}
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 	mutex_lock(&capsule_mutex);
272*4882a593Smuzhiyun 	rv = efi_capsule_update_locked(capsule, sg_pages, reset_type);
273*4882a593Smuzhiyun 	mutex_unlock(&capsule_mutex);
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun out:
276*4882a593Smuzhiyun 	for (i = 0; rv && i < sg_count; i++) {
277*4882a593Smuzhiyun 		if (sg_pages[i])
278*4882a593Smuzhiyun 			__free_page(sg_pages[i]);
279*4882a593Smuzhiyun 	}
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	kfree(sg_pages);
282*4882a593Smuzhiyun 	return rv;
283*4882a593Smuzhiyun }
284*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(efi_capsule_update);
285*4882a593Smuzhiyun 
capsule_reboot_notify(struct notifier_block * nb,unsigned long event,void * cmd)286*4882a593Smuzhiyun static int capsule_reboot_notify(struct notifier_block *nb, unsigned long event, void *cmd)
287*4882a593Smuzhiyun {
288*4882a593Smuzhiyun 	mutex_lock(&capsule_mutex);
289*4882a593Smuzhiyun 	stop_capsules = true;
290*4882a593Smuzhiyun 	mutex_unlock(&capsule_mutex);
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 	return NOTIFY_DONE;
293*4882a593Smuzhiyun }
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun static struct notifier_block capsule_reboot_nb = {
296*4882a593Smuzhiyun 	.notifier_call = capsule_reboot_notify,
297*4882a593Smuzhiyun };
298*4882a593Smuzhiyun 
capsule_reboot_register(void)299*4882a593Smuzhiyun static int __init capsule_reboot_register(void)
300*4882a593Smuzhiyun {
301*4882a593Smuzhiyun 	return register_reboot_notifier(&capsule_reboot_nb);
302*4882a593Smuzhiyun }
303*4882a593Smuzhiyun core_initcall(capsule_reboot_register);
304