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