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