blob: 7b57dda2417d84cf46ebd3089f63ecea8e35148c [file] [log] [blame]
Kweh, Hock Leong65117f12016-04-25 21:07:01 +01001/*
2 * EFI capsule loader driver.
3 *
4 * Copyright 2015 Intel Corporation
5 *
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.
8 */
9
10#define pr_fmt(fmt) "efi: " fmt
11
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>
Austin Christ6862e6a2016-08-11 11:42:00 +010019#include <linux/vmalloc.h>
Kweh, Hock Leong65117f12016-04-25 21:07:01 +010020
21#define NO_FURTHER_WRITE_ACTION -1
22
23struct capsule_info {
24 bool header_obtained;
25 int reset_type;
26 long index;
27 size_t count;
28 size_t total_size;
29 struct page **pages;
30 size_t page_bytes_remain;
31};
32
33/**
34 * efi_free_all_buff_pages - free all previous allocated buffer pages
35 * @cap_info: pointer to current instance of capsule_info structure
36 *
37 * In addition to freeing buffer pages, it flags NO_FURTHER_WRITE_ACTION
38 * to cease processing data in subsequent write(2) calls until close(2)
39 * is called.
40 **/
41static void efi_free_all_buff_pages(struct capsule_info *cap_info)
42{
43 while (cap_info->index > 0)
44 __free_page(cap_info->pages[--cap_info->index]);
45
46 cap_info->index = NO_FURTHER_WRITE_ACTION;
47}
48
49/**
50 * efi_capsule_setup_info - obtain the efi capsule header in the binary and
51 * setup capsule_info structure
52 * @cap_info: pointer to current instance of capsule_info structure
53 * @kbuff: a mapped first page buffer pointer
54 * @hdr_bytes: the total received number of bytes for efi header
55 **/
56static ssize_t efi_capsule_setup_info(struct capsule_info *cap_info,
57 void *kbuff, size_t hdr_bytes)
58{
59 efi_capsule_header_t *cap_hdr;
60 size_t pages_needed;
61 int ret;
62 void *temp_page;
63
64 /* Only process data block that is larger than efi header size */
65 if (hdr_bytes < sizeof(efi_capsule_header_t))
66 return 0;
67
68 /* Reset back to the correct offset of header */
69 cap_hdr = kbuff - cap_info->count;
70 pages_needed = ALIGN(cap_hdr->imagesize, PAGE_SIZE) >> PAGE_SHIFT;
71
72 if (pages_needed == 0) {
73 pr_err("%s: pages count invalid\n", __func__);
74 return -EINVAL;
75 }
76
77 /* Check if the capsule binary supported */
78 ret = efi_capsule_supported(cap_hdr->guid, cap_hdr->flags,
79 cap_hdr->imagesize,
80 &cap_info->reset_type);
81 if (ret) {
82 pr_err("%s: efi_capsule_supported() failed\n",
83 __func__);
84 return ret;
85 }
86
87 cap_info->total_size = cap_hdr->imagesize;
88 temp_page = krealloc(cap_info->pages,
89 pages_needed * sizeof(void *),
90 GFP_KERNEL | __GFP_ZERO);
Jan Kiszka73676332017-06-02 13:51:57 +000091 if (!temp_page)
Kweh, Hock Leong65117f12016-04-25 21:07:01 +010092 return -ENOMEM;
Kweh, Hock Leong65117f12016-04-25 21:07:01 +010093
94 cap_info->pages = temp_page;
95 cap_info->header_obtained = true;
96
97 return 0;
98}
99
100/**
101 * efi_capsule_submit_update - invoke the efi_capsule_update API once binary
102 * upload done
103 * @cap_info: pointer to current instance of capsule_info structure
104 **/
105static ssize_t efi_capsule_submit_update(struct capsule_info *cap_info)
106{
107 int ret;
108 void *cap_hdr_temp;
109
Austin Christ6862e6a2016-08-11 11:42:00 +0100110 cap_hdr_temp = vmap(cap_info->pages, cap_info->index,
111 VM_MAP, PAGE_KERNEL);
Jan Kiszka73676332017-06-02 13:51:57 +0000112 if (!cap_hdr_temp)
Jan Kiszkafb153dc2017-06-02 13:51:56 +0000113 return -ENOMEM;
Kweh, Hock Leong65117f12016-04-25 21:07:01 +0100114
115 ret = efi_capsule_update(cap_hdr_temp, cap_info->pages);
Austin Christ6862e6a2016-08-11 11:42:00 +0100116 vunmap(cap_hdr_temp);
Kweh, Hock Leong65117f12016-04-25 21:07:01 +0100117 if (ret) {
118 pr_err("%s: efi_capsule_update() failed\n", __func__);
119 return ret;
120 }
121
122 /* Indicate capsule binary uploading is done */
123 cap_info->index = NO_FURTHER_WRITE_ACTION;
124 pr_info("%s: Successfully upload capsule file with reboot type '%s'\n",
125 __func__, !cap_info->reset_type ? "RESET_COLD" :
126 cap_info->reset_type == 1 ? "RESET_WARM" :
127 "RESET_SHUTDOWN");
128 return 0;
129}
130
131/**
132 * efi_capsule_write - store the capsule binary and pass it to
133 * efi_capsule_update() API
134 * @file: file pointer
135 * @buff: buffer pointer
136 * @count: number of bytes in @buff
137 * @offp: not used
138 *
139 * Expectation:
140 * - A user space tool should start at the beginning of capsule binary and
141 * pass data in sequentially.
142 * - Users should close and re-open this file note in order to upload more
143 * capsules.
144 * - After an error returned, user should close the file and restart the
145 * operation for the next try otherwise -EIO will be returned until the
146 * file is closed.
147 * - An EFI capsule header must be located at the beginning of capsule
148 * binary file and passed in as first block data of write operation.
149 **/
150static ssize_t efi_capsule_write(struct file *file, const char __user *buff,
151 size_t count, loff_t *offp)
152{
153 int ret = 0;
154 struct capsule_info *cap_info = file->private_data;
155 struct page *page;
156 void *kbuff = NULL;
157 size_t write_byte;
158
159 if (count == 0)
160 return 0;
161
162 /* Return error while NO_FURTHER_WRITE_ACTION is flagged */
163 if (cap_info->index < 0)
164 return -EIO;
165
166 /* Only alloc a new page when previous page is full */
167 if (!cap_info->page_bytes_remain) {
168 page = alloc_page(GFP_KERNEL);
169 if (!page) {
Kweh, Hock Leong65117f12016-04-25 21:07:01 +0100170 ret = -ENOMEM;
171 goto failed;
172 }
173
174 cap_info->pages[cap_info->index++] = page;
175 cap_info->page_bytes_remain = PAGE_SIZE;
176 }
177
178 page = cap_info->pages[cap_info->index - 1];
179
180 kbuff = kmap(page);
181 if (!kbuff) {
Jan Kiszkafb153dc2017-06-02 13:51:56 +0000182 ret = -ENOMEM;
Kweh, Hock Leong65117f12016-04-25 21:07:01 +0100183 goto failed;
184 }
185 kbuff += PAGE_SIZE - cap_info->page_bytes_remain;
186
187 /* Copy capsule binary data from user space to kernel space buffer */
188 write_byte = min_t(size_t, count, cap_info->page_bytes_remain);
189 if (copy_from_user(kbuff, buff, write_byte)) {
Kweh, Hock Leong65117f12016-04-25 21:07:01 +0100190 ret = -EFAULT;
191 goto fail_unmap;
192 }
193 cap_info->page_bytes_remain -= write_byte;
194
195 /* Setup capsule binary info structure */
196 if (!cap_info->header_obtained) {
197 ret = efi_capsule_setup_info(cap_info, kbuff,
198 cap_info->count + write_byte);
199 if (ret)
200 goto fail_unmap;
201 }
202
203 cap_info->count += write_byte;
204 kunmap(page);
205
206 /* Submit the full binary to efi_capsule_update() API */
207 if (cap_info->header_obtained &&
208 cap_info->count >= cap_info->total_size) {
209 if (cap_info->count > cap_info->total_size) {
210 pr_err("%s: upload size exceeded header defined size\n",
211 __func__);
212 ret = -EINVAL;
213 goto failed;
214 }
215
216 ret = efi_capsule_submit_update(cap_info);
217 if (ret)
218 goto failed;
219 }
220
221 return write_byte;
222
223fail_unmap:
224 kunmap(page);
225failed:
226 efi_free_all_buff_pages(cap_info);
227 return ret;
228}
229
230/**
231 * efi_capsule_flush - called by file close or file flush
232 * @file: file pointer
233 * @id: not used
234 *
235 * If a capsule is being partially uploaded then calling this function
236 * will be treated as upload termination and will free those completed
237 * buffer pages and -ECANCELED will be returned.
238 **/
239static int efi_capsule_flush(struct file *file, fl_owner_t id)
240{
241 int ret = 0;
242 struct capsule_info *cap_info = file->private_data;
243
244 if (cap_info->index > 0) {
245 pr_err("%s: capsule upload not complete\n", __func__);
246 efi_free_all_buff_pages(cap_info);
247 ret = -ECANCELED;
248 }
249
250 return ret;
251}
252
253/**
254 * efi_capsule_release - called by file close
255 * @inode: not used
256 * @file: file pointer
257 *
258 * We will not free successfully submitted pages since efi update
259 * requires data to be maintained across system reboot.
260 **/
261static int efi_capsule_release(struct inode *inode, struct file *file)
262{
263 struct capsule_info *cap_info = file->private_data;
264
265 kfree(cap_info->pages);
266 kfree(file->private_data);
267 file->private_data = NULL;
268 return 0;
269}
270
271/**
272 * efi_capsule_open - called by file open
273 * @inode: not used
274 * @file: file pointer
275 *
276 * Will allocate each capsule_info memory for each file open call.
277 * This provided the capability to support multiple file open feature
278 * where user is not needed to wait for others to finish in order to
279 * upload their capsule binary.
280 **/
281static int efi_capsule_open(struct inode *inode, struct file *file)
282{
283 struct capsule_info *cap_info;
284
285 cap_info = kzalloc(sizeof(*cap_info), GFP_KERNEL);
286 if (!cap_info)
287 return -ENOMEM;
288
289 cap_info->pages = kzalloc(sizeof(void *), GFP_KERNEL);
290 if (!cap_info->pages) {
291 kfree(cap_info);
292 return -ENOMEM;
293 }
294
295 file->private_data = cap_info;
296
297 return 0;
298}
299
300static const struct file_operations efi_capsule_fops = {
301 .owner = THIS_MODULE,
302 .open = efi_capsule_open,
303 .write = efi_capsule_write,
304 .flush = efi_capsule_flush,
305 .release = efi_capsule_release,
306 .llseek = no_llseek,
307};
308
309static struct miscdevice efi_capsule_misc = {
310 .minor = MISC_DYNAMIC_MINOR,
311 .name = "efi_capsule_loader",
312 .fops = &efi_capsule_fops,
313};
314
315static int __init efi_capsule_loader_init(void)
316{
317 int ret;
318
319 if (!efi_enabled(EFI_RUNTIME_SERVICES))
320 return -ENODEV;
321
322 ret = misc_register(&efi_capsule_misc);
323 if (ret)
324 pr_err("%s: Failed to register misc char file note\n",
325 __func__);
326
327 return ret;
328}
329module_init(efi_capsule_loader_init);
330
331static void __exit efi_capsule_loader_exit(void)
332{
333 misc_deregister(&efi_capsule_misc);
334}
335module_exit(efi_capsule_loader_exit);
336
337MODULE_DESCRIPTION("EFI capsule firmware binary loader");
338MODULE_LICENSE("GPL v2");