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>
19 #include <linux/vmalloc.h>
21 #define NO_FURTHER_WRITE_ACTION -1
24 * efi_free_all_buff_pages - free all previous allocated buffer pages
25 * @cap_info: pointer to current instance of capsule_info structure
27 * In addition to freeing buffer pages, it flags NO_FURTHER_WRITE_ACTION
28 * to cease processing data in subsequent write(2) calls until close(2)
31 static void efi_free_all_buff_pages(struct capsule_info *cap_info)
33 while (cap_info->index > 0)
34 __free_page(cap_info->pages[--cap_info->index]);
36 cap_info->index = NO_FURTHER_WRITE_ACTION;
39 int __efi_capsule_setup_info(struct capsule_info *cap_info)
45 pages_needed = ALIGN(cap_info->total_size, PAGE_SIZE) / PAGE_SIZE;
47 if (pages_needed == 0) {
48 pr_err("invalid capsule size");
52 /* Check if the capsule binary supported */
53 ret = efi_capsule_supported(cap_info->header.guid,
54 cap_info->header.flags,
55 cap_info->header.imagesize,
56 &cap_info->reset_type);
58 pr_err("capsule not supported\n");
62 temp_page = krealloc(cap_info->pages,
63 pages_needed * sizeof(void *),
64 GFP_KERNEL | __GFP_ZERO);
68 cap_info->pages = temp_page;
74 * efi_capsule_setup_info - obtain the efi capsule header in the binary and
75 * setup capsule_info structure
76 * @cap_info: pointer to current instance of capsule_info structure
77 * @kbuff: a mapped first page buffer pointer
78 * @hdr_bytes: the total received number of bytes for efi header
80 * Platforms with non-standard capsule update mechanisms can override
81 * this __weak function so they can perform any required capsule
82 * image munging. See quark_quirk_function() for an example.
84 int __weak efi_capsule_setup_info(struct capsule_info *cap_info, void *kbuff,
87 /* Only process data block that is larger than efi header size */
88 if (hdr_bytes < sizeof(efi_capsule_header_t))
91 memcpy(&cap_info->header, kbuff, sizeof(cap_info->header));
92 cap_info->total_size = cap_info->header.imagesize;
94 return __efi_capsule_setup_info(cap_info);
98 * efi_capsule_submit_update - invoke the efi_capsule_update API once binary
100 * @cap_info: pointer to current instance of capsule_info structure
102 static ssize_t efi_capsule_submit_update(struct capsule_info *cap_info)
106 ret = efi_capsule_update(&cap_info->header, cap_info->pages);
108 pr_err("capsule update failed\n");
112 /* Indicate capsule binary uploading is done */
113 cap_info->index = NO_FURTHER_WRITE_ACTION;
114 pr_info("Successfully upload capsule file with reboot type '%s'\n",
115 !cap_info->reset_type ? "RESET_COLD" :
116 cap_info->reset_type == 1 ? "RESET_WARM" :
122 * efi_capsule_write - store the capsule binary and pass it to
123 * efi_capsule_update() API
124 * @file: file pointer
125 * @buff: buffer pointer
126 * @count: number of bytes in @buff
130 * - A user space tool should start at the beginning of capsule binary and
131 * pass data in sequentially.
132 * - Users should close and re-open this file note in order to upload more
134 * - After an error returned, user should close the file and restart the
135 * operation for the next try otherwise -EIO will be returned until the
137 * - An EFI capsule header must be located at the beginning of capsule
138 * binary file and passed in as first block data of write operation.
140 static ssize_t efi_capsule_write(struct file *file, const char __user *buff,
141 size_t count, loff_t *offp)
144 struct capsule_info *cap_info = file->private_data;
152 /* Return error while NO_FURTHER_WRITE_ACTION is flagged */
153 if (cap_info->index < 0)
156 /* Only alloc a new page when previous page is full */
157 if (!cap_info->page_bytes_remain) {
158 page = alloc_page(GFP_KERNEL);
164 cap_info->pages[cap_info->index++] = page;
165 cap_info->page_bytes_remain = PAGE_SIZE;
168 page = cap_info->pages[cap_info->index - 1];
171 kbuff += PAGE_SIZE - cap_info->page_bytes_remain;
173 /* Copy capsule binary data from user space to kernel space buffer */
174 write_byte = min_t(size_t, count, cap_info->page_bytes_remain);
175 if (copy_from_user(kbuff, buff, write_byte)) {
179 cap_info->page_bytes_remain -= write_byte;
181 /* Setup capsule binary info structure */
182 if (cap_info->header.headersize == 0) {
183 ret = efi_capsule_setup_info(cap_info, kbuff - cap_info->count,
184 cap_info->count + write_byte);
189 cap_info->count += write_byte;
192 /* Submit the full binary to efi_capsule_update() API */
193 if (cap_info->header.headersize > 0 &&
194 cap_info->count >= cap_info->total_size) {
195 if (cap_info->count > cap_info->total_size) {
196 pr_err("capsule upload size exceeded header defined size\n");
201 ret = efi_capsule_submit_update(cap_info);
211 efi_free_all_buff_pages(cap_info);
216 * efi_capsule_flush - called by file close or file flush
217 * @file: file pointer
220 * If a capsule is being partially uploaded then calling this function
221 * will be treated as upload termination and will free those completed
222 * buffer pages and -ECANCELED will be returned.
224 static int efi_capsule_flush(struct file *file, fl_owner_t id)
227 struct capsule_info *cap_info = file->private_data;
229 if (cap_info->index > 0) {
230 pr_err("capsule upload not complete\n");
231 efi_free_all_buff_pages(cap_info);
239 * efi_capsule_release - called by file close
241 * @file: file pointer
243 * We will not free successfully submitted pages since efi update
244 * requires data to be maintained across system reboot.
246 static int efi_capsule_release(struct inode *inode, struct file *file)
248 struct capsule_info *cap_info = file->private_data;
250 kfree(cap_info->pages);
251 kfree(file->private_data);
252 file->private_data = NULL;
257 * efi_capsule_open - called by file open
259 * @file: file pointer
261 * Will allocate each capsule_info memory for each file open call.
262 * This provided the capability to support multiple file open feature
263 * where user is not needed to wait for others to finish in order to
264 * upload their capsule binary.
266 static int efi_capsule_open(struct inode *inode, struct file *file)
268 struct capsule_info *cap_info;
270 cap_info = kzalloc(sizeof(*cap_info), GFP_KERNEL);
274 cap_info->pages = kzalloc(sizeof(void *), GFP_KERNEL);
275 if (!cap_info->pages) {
280 file->private_data = cap_info;
285 static const struct file_operations efi_capsule_fops = {
286 .owner = THIS_MODULE,
287 .open = efi_capsule_open,
288 .write = efi_capsule_write,
289 .flush = efi_capsule_flush,
290 .release = efi_capsule_release,
294 static struct miscdevice efi_capsule_misc = {
295 .minor = MISC_DYNAMIC_MINOR,
296 .name = "efi_capsule_loader",
297 .fops = &efi_capsule_fops,
300 static int __init efi_capsule_loader_init(void)
304 if (!efi_enabled(EFI_RUNTIME_SERVICES))
307 ret = misc_register(&efi_capsule_misc);
309 pr_err("Unable to register capsule loader device\n");
313 module_init(efi_capsule_loader_init);
315 static void __exit efi_capsule_loader_exit(void)
317 misc_deregister(&efi_capsule_misc);
319 module_exit(efi_capsule_loader_exit);
321 MODULE_DESCRIPTION("EFI capsule firmware binary loader");
322 MODULE_LICENSE("GPL v2");