blob: 055e2e8f985a3fbc5b334f8c0d414c8b6c51e3e3 [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
Kweh, Hock Leong65117f12016-04-25 21:07:01 +010023/**
24 * efi_free_all_buff_pages - free all previous allocated buffer pages
25 * @cap_info: pointer to current instance of capsule_info structure
26 *
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)
29 * is called.
30 **/
31static void efi_free_all_buff_pages(struct capsule_info *cap_info)
32{
33 while (cap_info->index > 0)
Ard Biesheuvelf24c4d42018-01-02 17:21:10 +000034 __free_page(cap_info->pages[--cap_info->index]);
Kweh, Hock Leong65117f12016-04-25 21:07:01 +010035
36 cap_info->index = NO_FURTHER_WRITE_ACTION;
37}
38
Ard Biesheuvel3fabd622017-06-02 13:52:02 +000039int __efi_capsule_setup_info(struct capsule_info *cap_info)
Kweh, Hock Leong65117f12016-04-25 21:07:01 +010040{
Kweh, Hock Leong65117f12016-04-25 21:07:01 +010041 size_t pages_needed;
42 int ret;
43 void *temp_page;
44
Ard Biesheuvel3fabd622017-06-02 13:52:02 +000045 pages_needed = ALIGN(cap_info->total_size, PAGE_SIZE) / PAGE_SIZE;
Kweh, Hock Leong65117f12016-04-25 21:07:01 +010046
47 if (pages_needed == 0) {
Jan Kiszka5dce14b2017-06-02 13:51:58 +000048 pr_err("invalid capsule size");
Kweh, Hock Leong65117f12016-04-25 21:07:01 +010049 return -EINVAL;
50 }
51
52 /* Check if the capsule binary supported */
Ard Biesheuvel82c37682017-06-02 13:52:00 +000053 ret = efi_capsule_supported(cap_info->header.guid,
54 cap_info->header.flags,
55 cap_info->header.imagesize,
Kweh, Hock Leong65117f12016-04-25 21:07:01 +010056 &cap_info->reset_type);
57 if (ret) {
Jan Kiszka5dce14b2017-06-02 13:51:58 +000058 pr_err("capsule not supported\n");
Kweh, Hock Leong65117f12016-04-25 21:07:01 +010059 return ret;
60 }
61
Kweh, Hock Leong65117f12016-04-25 21:07:01 +010062 temp_page = krealloc(cap_info->pages,
63 pages_needed * sizeof(void *),
64 GFP_KERNEL | __GFP_ZERO);
Jan Kiszka73676332017-06-02 13:51:57 +000065 if (!temp_page)
Kweh, Hock Leong65117f12016-04-25 21:07:01 +010066 return -ENOMEM;
Kweh, Hock Leong65117f12016-04-25 21:07:01 +010067
68 cap_info->pages = temp_page;
Kweh, Hock Leong65117f12016-04-25 21:07:01 +010069
Ard Biesheuvelf24c4d42018-01-02 17:21:10 +000070 temp_page = krealloc(cap_info->phys,
71 pages_needed * sizeof(phys_addr_t *),
72 GFP_KERNEL | __GFP_ZERO);
73 if (!temp_page)
74 return -ENOMEM;
75
76 cap_info->phys = temp_page;
77
Kweh, Hock Leong65117f12016-04-25 21:07:01 +010078 return 0;
79}
80
81/**
Ard Biesheuvel3fabd622017-06-02 13:52:02 +000082 * 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
87 *
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.
91 **/
92int __weak efi_capsule_setup_info(struct capsule_info *cap_info, void *kbuff,
93 size_t hdr_bytes)
94{
95 /* Only process data block that is larger than efi header size */
96 if (hdr_bytes < sizeof(efi_capsule_header_t))
97 return 0;
98
99 memcpy(&cap_info->header, kbuff, sizeof(cap_info->header));
100 cap_info->total_size = cap_info->header.imagesize;
101
102 return __efi_capsule_setup_info(cap_info);
103}
104
105/**
Kweh, Hock Leong65117f12016-04-25 21:07:01 +0100106 * efi_capsule_submit_update - invoke the efi_capsule_update API once binary
107 * upload done
108 * @cap_info: pointer to current instance of capsule_info structure
109 **/
110static ssize_t efi_capsule_submit_update(struct capsule_info *cap_info)
111{
Ard Biesheuvelf24c4d42018-01-02 17:21:10 +0000112 bool do_vunmap = false;
Kweh, Hock Leong65117f12016-04-25 21:07:01 +0100113 int ret;
Kweh, Hock Leong65117f12016-04-25 21:07:01 +0100114
Ard Biesheuvelf24c4d42018-01-02 17:21:10 +0000115 /*
116 * cap_info->capsule may have been assigned already by a quirk
117 * handler, so only overwrite it if it is NULL
118 */
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)
123 return -ENOMEM;
124 do_vunmap = true;
125 }
126
127 ret = efi_capsule_update(cap_info->capsule, cap_info->phys);
128 if (do_vunmap)
129 vunmap(cap_info->capsule);
Kweh, Hock Leong65117f12016-04-25 21:07:01 +0100130 if (ret) {
Jan Kiszka5dce14b2017-06-02 13:51:58 +0000131 pr_err("capsule update failed\n");
Kweh, Hock Leong65117f12016-04-25 21:07:01 +0100132 return ret;
133 }
134
135 /* Indicate capsule binary uploading is done */
136 cap_info->index = NO_FURTHER_WRITE_ACTION;
Jan Kiszka5dce14b2017-06-02 13:51:58 +0000137 pr_info("Successfully upload capsule file with reboot type '%s'\n",
138 !cap_info->reset_type ? "RESET_COLD" :
Kweh, Hock Leong65117f12016-04-25 21:07:01 +0100139 cap_info->reset_type == 1 ? "RESET_WARM" :
140 "RESET_SHUTDOWN");
141 return 0;
142}
143
144/**
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
150 * @offp: not used
151 *
152 * Expectation:
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
156 * capsules.
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
159 * file is closed.
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.
162 **/
163static ssize_t efi_capsule_write(struct file *file, const char __user *buff,
164 size_t count, loff_t *offp)
165{
166 int ret = 0;
167 struct capsule_info *cap_info = file->private_data;
168 struct page *page;
169 void *kbuff = NULL;
170 size_t write_byte;
171
172 if (count == 0)
173 return 0;
174
175 /* Return error while NO_FURTHER_WRITE_ACTION is flagged */
176 if (cap_info->index < 0)
177 return -EIO;
178
179 /* Only alloc a new page when previous page is full */
180 if (!cap_info->page_bytes_remain) {
181 page = alloc_page(GFP_KERNEL);
182 if (!page) {
Kweh, Hock Leong65117f12016-04-25 21:07:01 +0100183 ret = -ENOMEM;
184 goto failed;
185 }
186
Ard Biesheuvelf24c4d42018-01-02 17:21:10 +0000187 cap_info->pages[cap_info->index] = page;
188 cap_info->phys[cap_info->index] = page_to_phys(page);
Kweh, Hock Leong65117f12016-04-25 21:07:01 +0100189 cap_info->page_bytes_remain = PAGE_SIZE;
Ard Biesheuvelf24c4d42018-01-02 17:21:10 +0000190 cap_info->index++;
Ard Biesheuvel2a457fb2017-06-02 13:52:03 +0000191 } else {
Ard Biesheuvelf24c4d42018-01-02 17:21:10 +0000192 page = cap_info->pages[cap_info->index - 1];
Kweh, Hock Leong65117f12016-04-25 21:07:01 +0100193 }
194
Kweh, Hock Leong65117f12016-04-25 21:07:01 +0100195 kbuff = kmap(page);
Kweh, Hock Leong65117f12016-04-25 21:07:01 +0100196 kbuff += PAGE_SIZE - cap_info->page_bytes_remain;
197
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)) {
Kweh, Hock Leong65117f12016-04-25 21:07:01 +0100201 ret = -EFAULT;
202 goto fail_unmap;
203 }
204 cap_info->page_bytes_remain -= write_byte;
205
206 /* Setup capsule binary info structure */
Ard Biesheuvel82c37682017-06-02 13:52:00 +0000207 if (cap_info->header.headersize == 0) {
Ard Biesheuvel3fabd622017-06-02 13:52:02 +0000208 ret = efi_capsule_setup_info(cap_info, kbuff - cap_info->count,
Kweh, Hock Leong65117f12016-04-25 21:07:01 +0100209 cap_info->count + write_byte);
210 if (ret)
211 goto fail_unmap;
212 }
213
214 cap_info->count += write_byte;
215 kunmap(page);
216
217 /* Submit the full binary to efi_capsule_update() API */
Ard Biesheuvel82c37682017-06-02 13:52:00 +0000218 if (cap_info->header.headersize > 0 &&
Kweh, Hock Leong65117f12016-04-25 21:07:01 +0100219 cap_info->count >= cap_info->total_size) {
220 if (cap_info->count > cap_info->total_size) {
Jan Kiszka5dce14b2017-06-02 13:51:58 +0000221 pr_err("capsule upload size exceeded header defined size\n");
Kweh, Hock Leong65117f12016-04-25 21:07:01 +0100222 ret = -EINVAL;
223 goto failed;
224 }
225
226 ret = efi_capsule_submit_update(cap_info);
227 if (ret)
228 goto failed;
229 }
230
231 return write_byte;
232
233fail_unmap:
234 kunmap(page);
235failed:
236 efi_free_all_buff_pages(cap_info);
237 return ret;
238}
239
240/**
241 * efi_capsule_flush - called by file close or file flush
242 * @file: file pointer
243 * @id: not used
244 *
245 * If a capsule is being partially uploaded then calling this function
246 * will be treated as upload termination and will free those completed
247 * buffer pages and -ECANCELED will be returned.
248 **/
249static int efi_capsule_flush(struct file *file, fl_owner_t id)
250{
251 int ret = 0;
252 struct capsule_info *cap_info = file->private_data;
253
254 if (cap_info->index > 0) {
Jan Kiszka5dce14b2017-06-02 13:51:58 +0000255 pr_err("capsule upload not complete\n");
Kweh, Hock Leong65117f12016-04-25 21:07:01 +0100256 efi_free_all_buff_pages(cap_info);
257 ret = -ECANCELED;
258 }
259
260 return ret;
261}
262
263/**
264 * efi_capsule_release - called by file close
265 * @inode: not used
266 * @file: file pointer
267 *
268 * We will not free successfully submitted pages since efi update
269 * requires data to be maintained across system reboot.
270 **/
271static int efi_capsule_release(struct inode *inode, struct file *file)
272{
273 struct capsule_info *cap_info = file->private_data;
274
275 kfree(cap_info->pages);
Ard Biesheuvelf24c4d42018-01-02 17:21:10 +0000276 kfree(cap_info->phys);
Kweh, Hock Leong65117f12016-04-25 21:07:01 +0100277 kfree(file->private_data);
278 file->private_data = NULL;
279 return 0;
280}
281
282/**
283 * efi_capsule_open - called by file open
284 * @inode: not used
285 * @file: file pointer
286 *
287 * Will allocate each capsule_info memory for each file open call.
288 * This provided the capability to support multiple file open feature
289 * where user is not needed to wait for others to finish in order to
290 * upload their capsule binary.
291 **/
292static int efi_capsule_open(struct inode *inode, struct file *file)
293{
294 struct capsule_info *cap_info;
295
296 cap_info = kzalloc(sizeof(*cap_info), GFP_KERNEL);
297 if (!cap_info)
298 return -ENOMEM;
299
300 cap_info->pages = kzalloc(sizeof(void *), GFP_KERNEL);
301 if (!cap_info->pages) {
302 kfree(cap_info);
303 return -ENOMEM;
304 }
305
Ard Biesheuvelf24c4d42018-01-02 17:21:10 +0000306 cap_info->phys = kzalloc(sizeof(void *), GFP_KERNEL);
307 if (!cap_info->phys) {
308 kfree(cap_info->pages);
309 kfree(cap_info);
310 return -ENOMEM;
311 }
312
Kweh, Hock Leong65117f12016-04-25 21:07:01 +0100313 file->private_data = cap_info;
314
315 return 0;
316}
317
318static const struct file_operations efi_capsule_fops = {
319 .owner = THIS_MODULE,
320 .open = efi_capsule_open,
321 .write = efi_capsule_write,
322 .flush = efi_capsule_flush,
323 .release = efi_capsule_release,
324 .llseek = no_llseek,
325};
326
327static struct miscdevice efi_capsule_misc = {
328 .minor = MISC_DYNAMIC_MINOR,
329 .name = "efi_capsule_loader",
330 .fops = &efi_capsule_fops,
331};
332
333static int __init efi_capsule_loader_init(void)
334{
335 int ret;
336
337 if (!efi_enabled(EFI_RUNTIME_SERVICES))
338 return -ENODEV;
339
340 ret = misc_register(&efi_capsule_misc);
341 if (ret)
Jan Kiszka5dce14b2017-06-02 13:51:58 +0000342 pr_err("Unable to register capsule loader device\n");
Kweh, Hock Leong65117f12016-04-25 21:07:01 +0100343
344 return ret;
345}
346module_init(efi_capsule_loader_init);
347
348static void __exit efi_capsule_loader_exit(void)
349{
350 misc_deregister(&efi_capsule_misc);
351}
352module_exit(efi_capsule_loader_exit);
353
354MODULE_DESCRIPTION("EFI capsule firmware binary loader");
355MODULE_LICENSE("GPL v2");