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;
70 temp_page = krealloc(cap_info->phys,
71 pages_needed * sizeof(phys_addr_t *),
72 GFP_KERNEL | __GFP_ZERO);
76 cap_info->phys = temp_page;
82 * efi_capsule_setup_info - obtain the efi capsule header in the binary and
83 * setup capsule_info structure
84 * @cap_info: pointer to current instance of capsule_info structure
85 * @kbuff: a mapped first page buffer pointer
86 * @hdr_bytes: the total received number of bytes for efi header
88 * Platforms with non-standard capsule update mechanisms can override
89 * this __weak function so they can perform any required capsule
90 * image munging. See quark_quirk_function() for an example.
92 int __weak efi_capsule_setup_info(struct capsule_info *cap_info, void *kbuff,
95 /* Only process data block that is larger than efi header size */
96 if (hdr_bytes < sizeof(efi_capsule_header_t))
99 memcpy(&cap_info->header, kbuff, sizeof(cap_info->header));
100 cap_info->total_size = cap_info->header.imagesize;
102 return __efi_capsule_setup_info(cap_info);
106 * efi_capsule_submit_update - invoke the efi_capsule_update API once binary
108 * @cap_info: pointer to current instance of capsule_info structure
110 static ssize_t efi_capsule_submit_update(struct capsule_info *cap_info)
112 bool do_vunmap = false;
116 * cap_info->capsule may have been assigned already by a quirk
117 * handler, so only overwrite it if it is NULL
119 if (!cap_info->capsule) {
120 cap_info->capsule = vmap(cap_info->pages, cap_info->index,
121 VM_MAP, PAGE_KERNEL);
122 if (!cap_info->capsule)
127 ret = efi_capsule_update(cap_info->capsule, cap_info->phys);
129 vunmap(cap_info->capsule);
131 pr_err("capsule update failed\n");
135 /* Indicate capsule binary uploading is done */
136 cap_info->index = NO_FURTHER_WRITE_ACTION;
137 pr_info("Successfully upload capsule file with reboot type '%s'\n",
138 !cap_info->reset_type ? "RESET_COLD" :
139 cap_info->reset_type == 1 ? "RESET_WARM" :
145 * efi_capsule_write - store the capsule binary and pass it to
146 * efi_capsule_update() API
147 * @file: file pointer
148 * @buff: buffer pointer
149 * @count: number of bytes in @buff
153 * - A user space tool should start at the beginning of capsule binary and
154 * pass data in sequentially.
155 * - Users should close and re-open this file note in order to upload more
157 * - After an error returned, user should close the file and restart the
158 * operation for the next try otherwise -EIO will be returned until the
160 * - An EFI capsule header must be located at the beginning of capsule
161 * binary file and passed in as first block data of write operation.
163 static ssize_t efi_capsule_write(struct file *file, const char __user *buff,
164 size_t count, loff_t *offp)
167 struct capsule_info *cap_info = file->private_data;
175 /* Return error while NO_FURTHER_WRITE_ACTION is flagged */
176 if (cap_info->index < 0)
179 /* Only alloc a new page when previous page is full */
180 if (!cap_info->page_bytes_remain) {
181 page = alloc_page(GFP_KERNEL);
187 cap_info->pages[cap_info->index] = page;
188 cap_info->phys[cap_info->index] = page_to_phys(page);
189 cap_info->page_bytes_remain = PAGE_SIZE;
192 page = cap_info->pages[cap_info->index - 1];
196 kbuff += PAGE_SIZE - cap_info->page_bytes_remain;
198 /* Copy capsule binary data from user space to kernel space buffer */
199 write_byte = min_t(size_t, count, cap_info->page_bytes_remain);
200 if (copy_from_user(kbuff, buff, write_byte)) {
204 cap_info->page_bytes_remain -= write_byte;
206 /* Setup capsule binary info structure */
207 if (cap_info->header.headersize == 0) {
208 ret = efi_capsule_setup_info(cap_info, kbuff - cap_info->count,
209 cap_info->count + write_byte);
214 cap_info->count += write_byte;
217 /* Submit the full binary to efi_capsule_update() API */
218 if (cap_info->header.headersize > 0 &&
219 cap_info->count >= cap_info->total_size) {
220 if (cap_info->count > cap_info->total_size) {
221 pr_err("capsule upload size exceeded header defined size\n");
226 ret = efi_capsule_submit_update(cap_info);
236 efi_free_all_buff_pages(cap_info);
241 * efi_capsule_release - called by file close
243 * @file: file pointer
245 * We will not free successfully submitted pages since efi update
246 * requires data to be maintained across system reboot.
248 static int efi_capsule_release(struct inode *inode, struct file *file)
250 struct capsule_info *cap_info = file->private_data;
252 if (cap_info->index > 0 &&
253 (cap_info->header.headersize == 0 ||
254 cap_info->count < cap_info->total_size)) {
255 pr_err("capsule upload not complete\n");
256 efi_free_all_buff_pages(cap_info);
259 kfree(cap_info->pages);
260 kfree(cap_info->phys);
261 kfree(file->private_data);
262 file->private_data = NULL;
267 * efi_capsule_open - called by file open
269 * @file: file pointer
271 * Will allocate each capsule_info memory for each file open call.
272 * This provided the capability to support multiple file open feature
273 * where user is not needed to wait for others to finish in order to
274 * upload their capsule binary.
276 static int efi_capsule_open(struct inode *inode, struct file *file)
278 struct capsule_info *cap_info;
280 cap_info = kzalloc(sizeof(*cap_info), GFP_KERNEL);
284 cap_info->pages = kzalloc(sizeof(void *), GFP_KERNEL);
285 if (!cap_info->pages) {
290 cap_info->phys = kzalloc(sizeof(void *), GFP_KERNEL);
291 if (!cap_info->phys) {
292 kfree(cap_info->pages);
297 file->private_data = cap_info;
302 static const struct file_operations efi_capsule_fops = {
303 .owner = THIS_MODULE,
304 .open = efi_capsule_open,
305 .write = efi_capsule_write,
306 .release = efi_capsule_release,
310 static struct miscdevice efi_capsule_misc = {
311 .minor = MISC_DYNAMIC_MINOR,
312 .name = "efi_capsule_loader",
313 .fops = &efi_capsule_fops,
316 static int __init efi_capsule_loader_init(void)
320 if (!efi_enabled(EFI_RUNTIME_SERVICES))
323 ret = misc_register(&efi_capsule_misc);
325 pr_err("Unable to register capsule loader device\n");
329 module_init(efi_capsule_loader_init);
331 static void __exit efi_capsule_loader_exit(void)
333 misc_deregister(&efi_capsule_misc);
335 module_exit(efi_capsule_loader_exit);
337 MODULE_DESCRIPTION("EFI capsule firmware binary loader");
338 MODULE_LICENSE("GPL v2");