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>
14 #include <linux/slab.h>
15 #include <linux/mutex.h>
16 #include <linux/efi.h>
17 #include <linux/vmalloc.h>
19 #define NO_FURTHER_WRITE_ACTION -1
22 * efi_free_all_buff_pages - free all previous allocated buffer pages
23 * @cap_info: pointer to current instance of capsule_info structure
25 * In addition to freeing buffer pages, it flags NO_FURTHER_WRITE_ACTION
26 * to cease processing data in subsequent write(2) calls until close(2)
29 static void efi_free_all_buff_pages(struct capsule_info
*cap_info
)
31 while (cap_info
->index
> 0)
32 __free_page(cap_info
->pages
[--cap_info
->index
]);
34 cap_info
->index
= NO_FURTHER_WRITE_ACTION
;
37 int __efi_capsule_setup_info(struct capsule_info
*cap_info
)
43 pages_needed
= ALIGN(cap_info
->total_size
, PAGE_SIZE
) / PAGE_SIZE
;
45 if (pages_needed
== 0) {
46 pr_err("invalid capsule size\n");
50 /* Check if the capsule binary supported */
51 ret
= efi_capsule_supported(cap_info
->header
.guid
,
52 cap_info
->header
.flags
,
53 cap_info
->header
.imagesize
,
54 &cap_info
->reset_type
);
56 pr_err("capsule not supported\n");
60 temp_page
= krealloc(cap_info
->pages
,
61 pages_needed
* sizeof(void *),
62 GFP_KERNEL
| __GFP_ZERO
);
66 cap_info
->pages
= temp_page
;
68 temp_page
= krealloc(cap_info
->phys
,
69 pages_needed
* sizeof(phys_addr_t
*),
70 GFP_KERNEL
| __GFP_ZERO
);
74 cap_info
->phys
= temp_page
;
80 * efi_capsule_setup_info - obtain the efi capsule header in the binary and
81 * setup capsule_info structure
82 * @cap_info: pointer to current instance of capsule_info structure
83 * @kbuff: a mapped first page buffer pointer
84 * @hdr_bytes: the total received number of bytes for efi header
86 * Platforms with non-standard capsule update mechanisms can override
87 * this __weak function so they can perform any required capsule
88 * image munging. See quark_quirk_function() for an example.
90 int __weak
efi_capsule_setup_info(struct capsule_info
*cap_info
, void *kbuff
,
93 /* Only process data block that is larger than efi header size */
94 if (hdr_bytes
< sizeof(efi_capsule_header_t
))
97 memcpy(&cap_info
->header
, kbuff
, sizeof(cap_info
->header
));
98 cap_info
->total_size
= cap_info
->header
.imagesize
;
100 return __efi_capsule_setup_info(cap_info
);
104 * efi_capsule_submit_update - invoke the efi_capsule_update API once binary
106 * @cap_info: pointer to current instance of capsule_info structure
108 static ssize_t
efi_capsule_submit_update(struct capsule_info
*cap_info
)
110 bool do_vunmap
= false;
114 * cap_info->capsule may have been assigned already by a quirk
115 * handler, so only overwrite it if it is NULL
117 if (!cap_info
->capsule
) {
118 cap_info
->capsule
= vmap(cap_info
->pages
, cap_info
->index
,
119 VM_MAP
, PAGE_KERNEL
);
120 if (!cap_info
->capsule
)
125 ret
= efi_capsule_update(cap_info
->capsule
, cap_info
->phys
);
127 vunmap(cap_info
->capsule
);
129 pr_err("capsule update failed\n");
133 /* Indicate capsule binary uploading is done */
134 cap_info
->index
= NO_FURTHER_WRITE_ACTION
;
136 if (cap_info
->header
.flags
& EFI_CAPSULE_PERSIST_ACROSS_RESET
) {
137 pr_info("Successfully uploaded capsule file with reboot type '%s'\n",
138 !cap_info
->reset_type
? "RESET_COLD" :
139 cap_info
->reset_type
== 1 ? "RESET_WARM" :
142 pr_info("Successfully processed capsule file\n");
149 * efi_capsule_write - store the capsule binary and pass it to
150 * efi_capsule_update() API
151 * @file: file pointer
152 * @buff: buffer pointer
153 * @count: number of bytes in @buff
157 * - A user space tool should start at the beginning of capsule binary and
158 * pass data in sequentially.
159 * - Users should close and re-open this file note in order to upload more
161 * - After an error returned, user should close the file and restart the
162 * operation for the next try otherwise -EIO will be returned until the
164 * - An EFI capsule header must be located at the beginning of capsule
165 * binary file and passed in as first block data of write operation.
167 static ssize_t
efi_capsule_write(struct file
*file
, const char __user
*buff
,
168 size_t count
, loff_t
*offp
)
171 struct capsule_info
*cap_info
= file
->private_data
;
179 /* Return error while NO_FURTHER_WRITE_ACTION is flagged */
180 if (cap_info
->index
< 0)
183 /* Only alloc a new page when previous page is full */
184 if (!cap_info
->page_bytes_remain
) {
185 page
= alloc_page(GFP_KERNEL
);
191 cap_info
->pages
[cap_info
->index
] = page
;
192 cap_info
->phys
[cap_info
->index
] = page_to_phys(page
);
193 cap_info
->page_bytes_remain
= PAGE_SIZE
;
196 page
= cap_info
->pages
[cap_info
->index
- 1];
200 kbuff
+= PAGE_SIZE
- cap_info
->page_bytes_remain
;
202 /* Copy capsule binary data from user space to kernel space buffer */
203 write_byte
= min_t(size_t, count
, cap_info
->page_bytes_remain
);
204 if (copy_from_user(kbuff
, buff
, write_byte
)) {
208 cap_info
->page_bytes_remain
-= write_byte
;
210 /* Setup capsule binary info structure */
211 if (cap_info
->header
.headersize
== 0) {
212 ret
= efi_capsule_setup_info(cap_info
, kbuff
- cap_info
->count
,
213 cap_info
->count
+ write_byte
);
218 cap_info
->count
+= write_byte
;
221 /* Submit the full binary to efi_capsule_update() API */
222 if (cap_info
->header
.headersize
> 0 &&
223 cap_info
->count
>= cap_info
->total_size
) {
224 if (cap_info
->count
> cap_info
->total_size
) {
225 pr_err("capsule upload size exceeded header defined size\n");
230 ret
= efi_capsule_submit_update(cap_info
);
240 efi_free_all_buff_pages(cap_info
);
245 * efi_capsule_flush - called by file close or file flush
246 * @file: file pointer
249 * If a capsule is being partially uploaded then calling this function
250 * will be treated as upload termination and will free those completed
251 * buffer pages and -ECANCELED will be returned.
253 static int efi_capsule_flush(struct file
*file
, fl_owner_t id
)
256 struct capsule_info
*cap_info
= file
->private_data
;
258 if (cap_info
->index
> 0) {
259 pr_err("capsule upload not complete\n");
260 efi_free_all_buff_pages(cap_info
);
268 * efi_capsule_release - called by file close
270 * @file: file pointer
272 * We will not free successfully submitted pages since efi update
273 * requires data to be maintained across system reboot.
275 static int efi_capsule_release(struct inode
*inode
, struct file
*file
)
277 struct capsule_info
*cap_info
= file
->private_data
;
279 kfree(cap_info
->pages
);
280 kfree(cap_info
->phys
);
281 kfree(file
->private_data
);
282 file
->private_data
= NULL
;
287 * efi_capsule_open - called by file open
289 * @file: file pointer
291 * Will allocate each capsule_info memory for each file open call.
292 * This provided the capability to support multiple file open feature
293 * where user is not needed to wait for others to finish in order to
294 * upload their capsule binary.
296 static int efi_capsule_open(struct inode
*inode
, struct file
*file
)
298 struct capsule_info
*cap_info
;
300 cap_info
= kzalloc(sizeof(*cap_info
), GFP_KERNEL
);
304 cap_info
->pages
= kzalloc(sizeof(void *), GFP_KERNEL
);
305 if (!cap_info
->pages
) {
310 cap_info
->phys
= kzalloc(sizeof(void *), GFP_KERNEL
);
311 if (!cap_info
->phys
) {
312 kfree(cap_info
->pages
);
317 file
->private_data
= cap_info
;
322 static const struct file_operations efi_capsule_fops
= {
323 .owner
= THIS_MODULE
,
324 .open
= efi_capsule_open
,
325 .write
= efi_capsule_write
,
326 .flush
= efi_capsule_flush
,
327 .release
= efi_capsule_release
,
331 static struct miscdevice efi_capsule_misc
= {
332 .minor
= MISC_DYNAMIC_MINOR
,
333 .name
= "efi_capsule_loader",
334 .fops
= &efi_capsule_fops
,
337 static int __init
efi_capsule_loader_init(void)
341 if (!efi_enabled(EFI_RUNTIME_SERVICES
))
344 ret
= misc_register(&efi_capsule_misc
);
346 pr_err("Unable to register capsule loader device\n");
350 module_init(efi_capsule_loader_init
);
352 static void __exit
efi_capsule_loader_exit(void)
354 misc_deregister(&efi_capsule_misc
);
356 module_exit(efi_capsule_loader_exit
);
358 MODULE_DESCRIPTION("EFI capsule firmware binary loader");
359 MODULE_LICENSE("GPL v2");