1
2
3
4
5
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
24
25
26
27
28
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
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
82
83
84
85
86
87
88
89
90
91int __weak efi_capsule_setup_info(struct capsule_info *cap_info, void *kbuff,
92 size_t hdr_bytes)
93{
94
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
106
107
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
116
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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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 = 0;
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
181 if (cap_info->index < 0)
182 return -EIO;
183
184
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
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
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
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
247
248
249
250
251
252
253
254static int efi_capsule_flush(struct file *file, fl_owner_t id)
255{
256 int ret = 0;
257 struct capsule_info *cap_info = file->private_data;
258
259 if (cap_info->index > 0) {
260 pr_err("capsule upload not complete\n");
261 efi_free_all_buff_pages(cap_info);
262 ret = -ECANCELED;
263 }
264
265 return ret;
266}
267
268
269
270
271
272
273
274
275
276static int efi_capsule_release(struct inode *inode, struct file *file)
277{
278 struct capsule_info *cap_info = file->private_data;
279
280 kfree(cap_info->pages);
281 kfree(cap_info->phys);
282 kfree(file->private_data);
283 file->private_data = NULL;
284 return 0;
285}
286
287
288
289
290
291
292
293
294
295
296
297static int efi_capsule_open(struct inode *inode, struct file *file)
298{
299 struct capsule_info *cap_info;
300
301 cap_info = kzalloc(sizeof(*cap_info), GFP_KERNEL);
302 if (!cap_info)
303 return -ENOMEM;
304
305 cap_info->pages = kzalloc(sizeof(void *), GFP_KERNEL);
306 if (!cap_info->pages) {
307 kfree(cap_info);
308 return -ENOMEM;
309 }
310
311 cap_info->phys = kzalloc(sizeof(void *), GFP_KERNEL);
312 if (!cap_info->phys) {
313 kfree(cap_info->pages);
314 kfree(cap_info);
315 return -ENOMEM;
316 }
317
318 file->private_data = cap_info;
319
320 return 0;
321}
322
323static const struct file_operations efi_capsule_fops = {
324 .owner = THIS_MODULE,
325 .open = efi_capsule_open,
326 .write = efi_capsule_write,
327 .flush = efi_capsule_flush,
328 .release = efi_capsule_release,
329 .llseek = no_llseek,
330};
331
332static struct miscdevice efi_capsule_misc = {
333 .minor = MISC_DYNAMIC_MINOR,
334 .name = "efi_capsule_loader",
335 .fops = &efi_capsule_fops,
336};
337
338static int __init efi_capsule_loader_init(void)
339{
340 int ret;
341
342 if (!efi_enabled(EFI_RUNTIME_SERVICES))
343 return -ENODEV;
344
345 ret = misc_register(&efi_capsule_misc);
346 if (ret)
347 pr_err("Unable to register capsule loader device\n");
348
349 return ret;
350}
351module_init(efi_capsule_loader_init);
352
353static void __exit efi_capsule_loader_exit(void)
354{
355 misc_deregister(&efi_capsule_misc);
356}
357module_exit(efi_capsule_loader_exit);
358
359MODULE_DESCRIPTION("EFI capsule firmware binary loader");
360MODULE_LICENSE("GPL v2");
361