2 * EFI capsule loader driver.
4 * Copyright 2015 Intel Corporation
6 * This file is part of the Linux kernel, and is made available under
7 * the terms of the GNU General Public License version 2.
10 #define pr_fmt(fmt) "efi: " fmt
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/miscdevice.h>
15 #include <linux/highmem.h>
16 #include <linux/slab.h>
17 #include <linux/mutex.h>
18 #include <linux/efi.h>
20 #define NO_FURTHER_WRITE_ACTION -1
29 size_t page_bytes_remain;
33 * efi_free_all_buff_pages - free all previous allocated buffer pages
34 * @cap_info: pointer to current instance of capsule_info structure
36 * In addition to freeing buffer pages, it flags NO_FURTHER_WRITE_ACTION
37 * to cease processing data in subsequent write(2) calls until close(2)
40 static void efi_free_all_buff_pages(struct capsule_info *cap_info)
42 while (cap_info->index > 0)
43 __free_page(cap_info->pages[--cap_info->index]);
45 cap_info->index = NO_FURTHER_WRITE_ACTION;
49 * efi_capsule_setup_info - obtain the efi capsule header in the binary and
50 * setup capsule_info structure
51 * @cap_info: pointer to current instance of capsule_info structure
52 * @kbuff: a mapped first page buffer pointer
53 * @hdr_bytes: the total received number of bytes for efi header
55 static ssize_t efi_capsule_setup_info(struct capsule_info *cap_info,
56 void *kbuff, size_t hdr_bytes)
58 efi_capsule_header_t *cap_hdr;
63 /* Only process data block that is larger than efi header size */
64 if (hdr_bytes < sizeof(efi_capsule_header_t))
67 /* Reset back to the correct offset of header */
68 cap_hdr = kbuff - cap_info->count;
69 pages_needed = ALIGN(cap_hdr->imagesize, PAGE_SIZE) >> PAGE_SHIFT;
71 if (pages_needed == 0) {
72 pr_err("%s: pages count invalid\n", __func__);
76 /* Check if the capsule binary supported */
77 ret = efi_capsule_supported(cap_hdr->guid, cap_hdr->flags,
79 &cap_info->reset_type);
81 pr_err("%s: efi_capsule_supported() failed\n",
86 cap_info->total_size = cap_hdr->imagesize;
87 temp_page = krealloc(cap_info->pages,
88 pages_needed * sizeof(void *),
89 GFP_KERNEL | __GFP_ZERO);
91 pr_debug("%s: krealloc() failed\n", __func__);
95 cap_info->pages = temp_page;
96 cap_info->header_obtained = true;
102 * efi_capsule_submit_update - invoke the efi_capsule_update API once binary
104 * @cap_info: pointer to current instance of capsule_info structure
106 static ssize_t efi_capsule_submit_update(struct capsule_info *cap_info)
111 cap_hdr_temp = kmap(cap_info->pages[0]);
113 pr_debug("%s: kmap() failed\n", __func__);
117 ret = efi_capsule_update(cap_hdr_temp, cap_info->pages);
118 kunmap(cap_info->pages[0]);
120 pr_err("%s: efi_capsule_update() failed\n", __func__);
124 /* Indicate capsule binary uploading is done */
125 cap_info->index = NO_FURTHER_WRITE_ACTION;
126 pr_info("%s: Successfully upload capsule file with reboot type '%s'\n",
127 __func__, !cap_info->reset_type ? "RESET_COLD" :
128 cap_info->reset_type == 1 ? "RESET_WARM" :
134 * efi_capsule_write - store the capsule binary and pass it to
135 * efi_capsule_update() API
136 * @file: file pointer
137 * @buff: buffer pointer
138 * @count: number of bytes in @buff
142 * - A user space tool should start at the beginning of capsule binary and
143 * pass data in sequentially.
144 * - Users should close and re-open this file note in order to upload more
146 * - After an error returned, user should close the file and restart the
147 * operation for the next try otherwise -EIO will be returned until the
149 * - An EFI capsule header must be located at the beginning of capsule
150 * binary file and passed in as first block data of write operation.
152 static ssize_t efi_capsule_write(struct file *file, const char __user *buff,
153 size_t count, loff_t *offp)
156 struct capsule_info *cap_info = file->private_data;
164 /* Return error while NO_FURTHER_WRITE_ACTION is flagged */
165 if (cap_info->index < 0)
168 /* Only alloc a new page when previous page is full */
169 if (!cap_info->page_bytes_remain) {
170 page = alloc_page(GFP_KERNEL);
172 pr_debug("%s: alloc_page() failed\n", __func__);
177 cap_info->pages[cap_info->index++] = page;
178 cap_info->page_bytes_remain = PAGE_SIZE;
181 page = cap_info->pages[cap_info->index - 1];
185 pr_debug("%s: kmap() failed\n", __func__);
189 kbuff += PAGE_SIZE - cap_info->page_bytes_remain;
191 /* Copy capsule binary data from user space to kernel space buffer */
192 write_byte = min_t(size_t, count, cap_info->page_bytes_remain);
193 if (copy_from_user(kbuff, buff, write_byte)) {
194 pr_debug("%s: copy_from_user() failed\n", __func__);
198 cap_info->page_bytes_remain -= write_byte;
200 /* Setup capsule binary info structure */
201 if (!cap_info->header_obtained) {
202 ret = efi_capsule_setup_info(cap_info, kbuff,
203 cap_info->count + write_byte);
208 cap_info->count += write_byte;
211 /* Submit the full binary to efi_capsule_update() API */
212 if (cap_info->header_obtained &&
213 cap_info->count >= cap_info->total_size) {
214 if (cap_info->count > cap_info->total_size) {
215 pr_err("%s: upload size exceeded header defined size\n",
221 ret = efi_capsule_submit_update(cap_info);
231 efi_free_all_buff_pages(cap_info);
236 * efi_capsule_flush - called by file close or file flush
237 * @file: file pointer
240 * If a capsule is being partially uploaded then calling this function
241 * will be treated as upload termination and will free those completed
242 * buffer pages and -ECANCELED will be returned.
244 static int efi_capsule_flush(struct file *file, fl_owner_t id)
247 struct capsule_info *cap_info = file->private_data;
249 if (cap_info->index > 0) {
250 pr_err("%s: capsule upload not complete\n", __func__);
251 efi_free_all_buff_pages(cap_info);
259 * efi_capsule_release - called by file close
261 * @file: file pointer
263 * We will not free successfully submitted pages since efi update
264 * requires data to be maintained across system reboot.
266 static int efi_capsule_release(struct inode *inode, struct file *file)
268 struct capsule_info *cap_info = file->private_data;
270 kfree(cap_info->pages);
271 kfree(file->private_data);
272 file->private_data = NULL;
277 * efi_capsule_open - called by file open
279 * @file: file pointer
281 * Will allocate each capsule_info memory for each file open call.
282 * This provided the capability to support multiple file open feature
283 * where user is not needed to wait for others to finish in order to
284 * upload their capsule binary.
286 static int efi_capsule_open(struct inode *inode, struct file *file)
288 struct capsule_info *cap_info;
290 cap_info = kzalloc(sizeof(*cap_info), GFP_KERNEL);
294 cap_info->pages = kzalloc(sizeof(void *), GFP_KERNEL);
295 if (!cap_info->pages) {
300 file->private_data = cap_info;
305 static const struct file_operations efi_capsule_fops = {
306 .owner = THIS_MODULE,
307 .open = efi_capsule_open,
308 .write = efi_capsule_write,
309 .flush = efi_capsule_flush,
310 .release = efi_capsule_release,
314 static struct miscdevice efi_capsule_misc = {
315 .minor = MISC_DYNAMIC_MINOR,
316 .name = "efi_capsule_loader",
317 .fops = &efi_capsule_fops,
320 static int __init efi_capsule_loader_init(void)
324 if (!efi_enabled(EFI_RUNTIME_SERVICES))
327 ret = misc_register(&efi_capsule_misc);
329 pr_err("%s: Failed to register misc char file note\n",
334 module_init(efi_capsule_loader_init);
336 static void __exit efi_capsule_loader_exit(void)
338 misc_deregister(&efi_capsule_misc);
340 module_exit(efi_capsule_loader_exit);
342 MODULE_DESCRIPTION("EFI capsule firmware binary loader");
343 MODULE_LICENSE("GPL v2");