1 // SPDX-License-Identifier: GPL-2.0
3 * EFI capsule loader driver.
5 * Copyright 2015 Intel Corporation
8 #define pr_fmt(fmt) "efi: " fmt
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/miscdevice.h>
13 #include <linux/highmem.h>
15 #include <linux/slab.h>
16 #include <linux/mutex.h>
17 #include <linux/efi.h>
18 #include <linux/vmalloc.h>
20 #define NO_FURTHER_WRITE_ACTION -1
23 * efi_free_all_buff_pages - free all previous allocated buffer pages
24 * @cap_info: pointer to current instance of capsule_info structure
26 * In addition to freeing buffer pages, it flags NO_FURTHER_WRITE_ACTION
27 * to cease processing data in subsequent write(2) calls until close(2)
30 static void efi_free_all_buff_pages(struct capsule_info
*cap_info
)
32 while (cap_info
->index
> 0)
33 __free_page(cap_info
->pages
[--cap_info
->index
]);
35 cap_info
->index
= NO_FURTHER_WRITE_ACTION
;
38 int __efi_capsule_setup_info(struct capsule_info
*cap_info
)
44 pages_needed
= ALIGN(cap_info
->total_size
, PAGE_SIZE
) / PAGE_SIZE
;
46 if (pages_needed
== 0) {
47 pr_err("invalid capsule size\n");
51 /* Check if the capsule binary supported */
52 ret
= efi_capsule_supported(cap_info
->header
.guid
,
53 cap_info
->header
.flags
,
54 cap_info
->header
.imagesize
,
55 &cap_info
->reset_type
);
57 pr_err("capsule not supported\n");
61 temp_page
= krealloc(cap_info
->pages
,
62 pages_needed
* sizeof(void *),
63 GFP_KERNEL
| __GFP_ZERO
);
67 cap_info
->pages
= temp_page
;
69 temp_page
= krealloc(cap_info
->phys
,
70 pages_needed
* sizeof(phys_addr_t
*),
71 GFP_KERNEL
| __GFP_ZERO
);
75 cap_info
->phys
= temp_page
;
81 * efi_capsule_setup_info - obtain the efi capsule header in the binary and
82 * setup capsule_info structure
83 * @cap_info: pointer to current instance of capsule_info structure
84 * @kbuff: a mapped first page buffer pointer
85 * @hdr_bytes: the total received number of bytes for efi header
87 * Platforms with non-standard capsule update mechanisms can override
88 * this __weak function so they can perform any required capsule
89 * image munging. See quark_quirk_function() for an example.
91 int __weak
efi_capsule_setup_info(struct capsule_info
*cap_info
, void *kbuff
,
94 /* Only process data block that is larger than efi header size */
95 if (hdr_bytes
< sizeof(efi_capsule_header_t
))
98 memcpy(&cap_info
->header
, kbuff
, sizeof(cap_info
->header
));
99 cap_info
->total_size
= cap_info
->header
.imagesize
;
101 return __efi_capsule_setup_info(cap_info
);
105 * efi_capsule_submit_update - invoke the efi_capsule_update API once binary
107 * @cap_info: pointer to current instance of capsule_info structure
109 static ssize_t
efi_capsule_submit_update(struct capsule_info
*cap_info
)
111 bool do_vunmap
= false;
115 * cap_info->capsule may have been assigned already by a quirk
116 * handler, so only overwrite it if it is NULL
118 if (!cap_info
->capsule
) {
119 cap_info
->capsule
= vmap(cap_info
->pages
, cap_info
->index
,
120 VM_MAP
, PAGE_KERNEL
);
121 if (!cap_info
->capsule
)
126 ret
= efi_capsule_update(cap_info
->capsule
, cap_info
->phys
);
128 vunmap(cap_info
->capsule
);
130 pr_err("capsule update failed\n");
134 /* Indicate capsule binary uploading is done */
135 cap_info
->index
= NO_FURTHER_WRITE_ACTION
;
137 if (cap_info
->header
.flags
& EFI_CAPSULE_PERSIST_ACROSS_RESET
) {
138 pr_info("Successfully uploaded capsule file with reboot type '%s'\n",
139 !cap_info
->reset_type
? "RESET_COLD" :
140 cap_info
->reset_type
== 1 ? "RESET_WARM" :
143 pr_info("Successfully processed capsule file\n");
150 * efi_capsule_write - store the capsule binary and pass it to
151 * efi_capsule_update() API
152 * @file: file pointer
153 * @buff: buffer pointer
154 * @count: number of bytes in @buff
158 * - A user space tool should start at the beginning of capsule binary and
159 * pass data in sequentially.
160 * - Users should close and re-open this file note in order to upload more
162 * - After an error returned, user should close the file and restart the
163 * operation for the next try otherwise -EIO will be returned until the
165 * - An EFI capsule header must be located at the beginning of capsule
166 * binary file and passed in as first block data of write operation.
168 static ssize_t
efi_capsule_write(struct file
*file
, const char __user
*buff
,
169 size_t count
, loff_t
*offp
)
172 struct capsule_info
*cap_info
= file
->private_data
;
180 /* Return error while NO_FURTHER_WRITE_ACTION is flagged */
181 if (cap_info
->index
< 0)
184 /* Only alloc a new page when previous page is full */
185 if (!cap_info
->page_bytes_remain
) {
186 page
= alloc_page(GFP_KERNEL
);
192 cap_info
->pages
[cap_info
->index
] = page
;
193 cap_info
->phys
[cap_info
->index
] = page_to_phys(page
);
194 cap_info
->page_bytes_remain
= PAGE_SIZE
;
197 page
= cap_info
->pages
[cap_info
->index
- 1];
201 kbuff
+= PAGE_SIZE
- cap_info
->page_bytes_remain
;
203 /* Copy capsule binary data from user space to kernel space buffer */
204 write_byte
= min_t(size_t, count
, cap_info
->page_bytes_remain
);
205 if (copy_from_user(kbuff
, buff
, write_byte
)) {
209 cap_info
->page_bytes_remain
-= write_byte
;
211 /* Setup capsule binary info structure */
212 if (cap_info
->header
.headersize
== 0) {
213 ret
= efi_capsule_setup_info(cap_info
, kbuff
- cap_info
->count
,
214 cap_info
->count
+ write_byte
);
219 cap_info
->count
+= write_byte
;
222 /* Submit the full binary to efi_capsule_update() API */
223 if (cap_info
->header
.headersize
> 0 &&
224 cap_info
->count
>= cap_info
->total_size
) {
225 if (cap_info
->count
> cap_info
->total_size
) {
226 pr_err("capsule upload size exceeded header defined size\n");
231 ret
= efi_capsule_submit_update(cap_info
);
241 efi_free_all_buff_pages(cap_info
);
246 * efi_capsule_flush - called by file close or file flush
247 * @file: file pointer
250 * If a capsule is being partially uploaded then calling this function
251 * will be treated as upload termination and will free those completed
252 * buffer pages and -ECANCELED will be returned.
254 static int efi_capsule_flush(struct file
*file
, fl_owner_t id
)
257 struct capsule_info
*cap_info
= file
->private_data
;
259 if (cap_info
->index
> 0) {
260 pr_err("capsule upload not complete\n");
261 efi_free_all_buff_pages(cap_info
);
269 * efi_capsule_release - called by file close
271 * @file: file pointer
273 * We will not free successfully submitted pages since efi update
274 * requires data to be maintained across system reboot.
276 static int efi_capsule_release(struct inode
*inode
, struct file
*file
)
278 struct capsule_info
*cap_info
= file
->private_data
;
280 kfree(cap_info
->pages
);
281 kfree(cap_info
->phys
);
282 kfree(file
->private_data
);
283 file
->private_data
= NULL
;
288 * efi_capsule_open - called by file open
290 * @file: file pointer
292 * Will allocate each capsule_info memory for each file open call.
293 * This provided the capability to support multiple file open feature
294 * where user is not needed to wait for others to finish in order to
295 * upload their capsule binary.
297 static int efi_capsule_open(struct inode
*inode
, struct file
*file
)
299 struct capsule_info
*cap_info
;
301 cap_info
= kzalloc(sizeof(*cap_info
), GFP_KERNEL
);
305 cap_info
->pages
= kzalloc(sizeof(void *), GFP_KERNEL
);
306 if (!cap_info
->pages
) {
311 cap_info
->phys
= kzalloc(sizeof(void *), GFP_KERNEL
);
312 if (!cap_info
->phys
) {
313 kfree(cap_info
->pages
);
318 file
->private_data
= cap_info
;
323 static const struct file_operations efi_capsule_fops
= {
324 .owner
= THIS_MODULE
,
325 .open
= efi_capsule_open
,
326 .write
= efi_capsule_write
,
327 .flush
= efi_capsule_flush
,
328 .release
= efi_capsule_release
,
332 static struct miscdevice efi_capsule_misc
= {
333 .minor
= MISC_DYNAMIC_MINOR
,
334 .name
= "efi_capsule_loader",
335 .fops
= &efi_capsule_fops
,
338 static int __init
efi_capsule_loader_init(void)
342 if (!efi_enabled(EFI_RUNTIME_SERVICES
))
345 ret
= misc_register(&efi_capsule_misc
);
347 pr_err("Unable to register capsule loader device\n");
351 module_init(efi_capsule_loader_init
);
353 static void __exit
efi_capsule_loader_exit(void)
355 misc_deregister(&efi_capsule_misc
);
357 module_exit(efi_capsule_loader_exit
);
359 MODULE_DESCRIPTION("EFI capsule firmware binary loader");
360 MODULE_LICENSE("GPL v2");