Kweh, Hock Leong | 65117f1 | 2016-04-25 21:07:01 +0100 | [diff] [blame] | 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> |
Austin Christ | 6862e6a | 2016-08-11 11:42:00 +0100 | [diff] [blame] | 19 | #include <linux/vmalloc.h> |
Kweh, Hock Leong | 65117f1 | 2016-04-25 21:07:01 +0100 | [diff] [blame] | 20 | |
| 21 | #define NO_FURTHER_WRITE_ACTION -1 |
| 22 | |
| 23 | struct 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 | **/ |
| 41 | static 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 | **/ |
| 56 | static 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) { |
Jan Kiszka | 5dce14b | 2017-06-02 13:51:58 +0000 | [diff] [blame^] | 73 | pr_err("invalid capsule size"); |
Kweh, Hock Leong | 65117f1 | 2016-04-25 21:07:01 +0100 | [diff] [blame] | 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) { |
Jan Kiszka | 5dce14b | 2017-06-02 13:51:58 +0000 | [diff] [blame^] | 82 | pr_err("capsule not supported\n"); |
Kweh, Hock Leong | 65117f1 | 2016-04-25 21:07:01 +0100 | [diff] [blame] | 83 | return ret; |
| 84 | } |
| 85 | |
| 86 | cap_info->total_size = cap_hdr->imagesize; |
| 87 | temp_page = krealloc(cap_info->pages, |
| 88 | pages_needed * sizeof(void *), |
| 89 | GFP_KERNEL | __GFP_ZERO); |
Jan Kiszka | 7367633 | 2017-06-02 13:51:57 +0000 | [diff] [blame] | 90 | if (!temp_page) |
Kweh, Hock Leong | 65117f1 | 2016-04-25 21:07:01 +0100 | [diff] [blame] | 91 | return -ENOMEM; |
Kweh, Hock Leong | 65117f1 | 2016-04-25 21:07:01 +0100 | [diff] [blame] | 92 | |
| 93 | cap_info->pages = temp_page; |
| 94 | cap_info->header_obtained = true; |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * efi_capsule_submit_update - invoke the efi_capsule_update API once binary |
| 101 | * upload done |
| 102 | * @cap_info: pointer to current instance of capsule_info structure |
| 103 | **/ |
| 104 | static ssize_t efi_capsule_submit_update(struct capsule_info *cap_info) |
| 105 | { |
| 106 | int ret; |
| 107 | void *cap_hdr_temp; |
| 108 | |
Austin Christ | 6862e6a | 2016-08-11 11:42:00 +0100 | [diff] [blame] | 109 | cap_hdr_temp = vmap(cap_info->pages, cap_info->index, |
| 110 | VM_MAP, PAGE_KERNEL); |
Jan Kiszka | 7367633 | 2017-06-02 13:51:57 +0000 | [diff] [blame] | 111 | if (!cap_hdr_temp) |
Jan Kiszka | fb153dc | 2017-06-02 13:51:56 +0000 | [diff] [blame] | 112 | return -ENOMEM; |
Kweh, Hock Leong | 65117f1 | 2016-04-25 21:07:01 +0100 | [diff] [blame] | 113 | |
| 114 | ret = efi_capsule_update(cap_hdr_temp, cap_info->pages); |
Austin Christ | 6862e6a | 2016-08-11 11:42:00 +0100 | [diff] [blame] | 115 | vunmap(cap_hdr_temp); |
Kweh, Hock Leong | 65117f1 | 2016-04-25 21:07:01 +0100 | [diff] [blame] | 116 | if (ret) { |
Jan Kiszka | 5dce14b | 2017-06-02 13:51:58 +0000 | [diff] [blame^] | 117 | pr_err("capsule update failed\n"); |
Kweh, Hock Leong | 65117f1 | 2016-04-25 21:07:01 +0100 | [diff] [blame] | 118 | return ret; |
| 119 | } |
| 120 | |
| 121 | /* Indicate capsule binary uploading is done */ |
| 122 | cap_info->index = NO_FURTHER_WRITE_ACTION; |
Jan Kiszka | 5dce14b | 2017-06-02 13:51:58 +0000 | [diff] [blame^] | 123 | pr_info("Successfully upload capsule file with reboot type '%s'\n", |
| 124 | !cap_info->reset_type ? "RESET_COLD" : |
Kweh, Hock Leong | 65117f1 | 2016-04-25 21:07:01 +0100 | [diff] [blame] | 125 | cap_info->reset_type == 1 ? "RESET_WARM" : |
| 126 | "RESET_SHUTDOWN"); |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * efi_capsule_write - store the capsule binary and pass it to |
| 132 | * efi_capsule_update() API |
| 133 | * @file: file pointer |
| 134 | * @buff: buffer pointer |
| 135 | * @count: number of bytes in @buff |
| 136 | * @offp: not used |
| 137 | * |
| 138 | * Expectation: |
| 139 | * - A user space tool should start at the beginning of capsule binary and |
| 140 | * pass data in sequentially. |
| 141 | * - Users should close and re-open this file note in order to upload more |
| 142 | * capsules. |
| 143 | * - After an error returned, user should close the file and restart the |
| 144 | * operation for the next try otherwise -EIO will be returned until the |
| 145 | * file is closed. |
| 146 | * - An EFI capsule header must be located at the beginning of capsule |
| 147 | * binary file and passed in as first block data of write operation. |
| 148 | **/ |
| 149 | static ssize_t efi_capsule_write(struct file *file, const char __user *buff, |
| 150 | size_t count, loff_t *offp) |
| 151 | { |
| 152 | int ret = 0; |
| 153 | struct capsule_info *cap_info = file->private_data; |
| 154 | struct page *page; |
| 155 | void *kbuff = NULL; |
| 156 | size_t write_byte; |
| 157 | |
| 158 | if (count == 0) |
| 159 | return 0; |
| 160 | |
| 161 | /* Return error while NO_FURTHER_WRITE_ACTION is flagged */ |
| 162 | if (cap_info->index < 0) |
| 163 | return -EIO; |
| 164 | |
| 165 | /* Only alloc a new page when previous page is full */ |
| 166 | if (!cap_info->page_bytes_remain) { |
| 167 | page = alloc_page(GFP_KERNEL); |
| 168 | if (!page) { |
Kweh, Hock Leong | 65117f1 | 2016-04-25 21:07:01 +0100 | [diff] [blame] | 169 | ret = -ENOMEM; |
| 170 | goto failed; |
| 171 | } |
| 172 | |
| 173 | cap_info->pages[cap_info->index++] = page; |
| 174 | cap_info->page_bytes_remain = PAGE_SIZE; |
| 175 | } |
| 176 | |
| 177 | page = cap_info->pages[cap_info->index - 1]; |
| 178 | |
| 179 | kbuff = kmap(page); |
| 180 | if (!kbuff) { |
Jan Kiszka | fb153dc | 2017-06-02 13:51:56 +0000 | [diff] [blame] | 181 | ret = -ENOMEM; |
Kweh, Hock Leong | 65117f1 | 2016-04-25 21:07:01 +0100 | [diff] [blame] | 182 | goto failed; |
| 183 | } |
| 184 | kbuff += PAGE_SIZE - cap_info->page_bytes_remain; |
| 185 | |
| 186 | /* Copy capsule binary data from user space to kernel space buffer */ |
| 187 | write_byte = min_t(size_t, count, cap_info->page_bytes_remain); |
| 188 | if (copy_from_user(kbuff, buff, write_byte)) { |
Kweh, Hock Leong | 65117f1 | 2016-04-25 21:07:01 +0100 | [diff] [blame] | 189 | ret = -EFAULT; |
| 190 | goto fail_unmap; |
| 191 | } |
| 192 | cap_info->page_bytes_remain -= write_byte; |
| 193 | |
| 194 | /* Setup capsule binary info structure */ |
| 195 | if (!cap_info->header_obtained) { |
| 196 | ret = efi_capsule_setup_info(cap_info, kbuff, |
| 197 | cap_info->count + write_byte); |
| 198 | if (ret) |
| 199 | goto fail_unmap; |
| 200 | } |
| 201 | |
| 202 | cap_info->count += write_byte; |
| 203 | kunmap(page); |
| 204 | |
| 205 | /* Submit the full binary to efi_capsule_update() API */ |
| 206 | if (cap_info->header_obtained && |
| 207 | cap_info->count >= cap_info->total_size) { |
| 208 | if (cap_info->count > cap_info->total_size) { |
Jan Kiszka | 5dce14b | 2017-06-02 13:51:58 +0000 | [diff] [blame^] | 209 | pr_err("capsule upload size exceeded header defined size\n"); |
Kweh, Hock Leong | 65117f1 | 2016-04-25 21:07:01 +0100 | [diff] [blame] | 210 | ret = -EINVAL; |
| 211 | goto failed; |
| 212 | } |
| 213 | |
| 214 | ret = efi_capsule_submit_update(cap_info); |
| 215 | if (ret) |
| 216 | goto failed; |
| 217 | } |
| 218 | |
| 219 | return write_byte; |
| 220 | |
| 221 | fail_unmap: |
| 222 | kunmap(page); |
| 223 | failed: |
| 224 | efi_free_all_buff_pages(cap_info); |
| 225 | return ret; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * efi_capsule_flush - called by file close or file flush |
| 230 | * @file: file pointer |
| 231 | * @id: not used |
| 232 | * |
| 233 | * If a capsule is being partially uploaded then calling this function |
| 234 | * will be treated as upload termination and will free those completed |
| 235 | * buffer pages and -ECANCELED will be returned. |
| 236 | **/ |
| 237 | static int efi_capsule_flush(struct file *file, fl_owner_t id) |
| 238 | { |
| 239 | int ret = 0; |
| 240 | struct capsule_info *cap_info = file->private_data; |
| 241 | |
| 242 | if (cap_info->index > 0) { |
Jan Kiszka | 5dce14b | 2017-06-02 13:51:58 +0000 | [diff] [blame^] | 243 | pr_err("capsule upload not complete\n"); |
Kweh, Hock Leong | 65117f1 | 2016-04-25 21:07:01 +0100 | [diff] [blame] | 244 | efi_free_all_buff_pages(cap_info); |
| 245 | ret = -ECANCELED; |
| 246 | } |
| 247 | |
| 248 | return ret; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * efi_capsule_release - called by file close |
| 253 | * @inode: not used |
| 254 | * @file: file pointer |
| 255 | * |
| 256 | * We will not free successfully submitted pages since efi update |
| 257 | * requires data to be maintained across system reboot. |
| 258 | **/ |
| 259 | static int efi_capsule_release(struct inode *inode, struct file *file) |
| 260 | { |
| 261 | struct capsule_info *cap_info = file->private_data; |
| 262 | |
| 263 | kfree(cap_info->pages); |
| 264 | kfree(file->private_data); |
| 265 | file->private_data = NULL; |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * efi_capsule_open - called by file open |
| 271 | * @inode: not used |
| 272 | * @file: file pointer |
| 273 | * |
| 274 | * Will allocate each capsule_info memory for each file open call. |
| 275 | * This provided the capability to support multiple file open feature |
| 276 | * where user is not needed to wait for others to finish in order to |
| 277 | * upload their capsule binary. |
| 278 | **/ |
| 279 | static int efi_capsule_open(struct inode *inode, struct file *file) |
| 280 | { |
| 281 | struct capsule_info *cap_info; |
| 282 | |
| 283 | cap_info = kzalloc(sizeof(*cap_info), GFP_KERNEL); |
| 284 | if (!cap_info) |
| 285 | return -ENOMEM; |
| 286 | |
| 287 | cap_info->pages = kzalloc(sizeof(void *), GFP_KERNEL); |
| 288 | if (!cap_info->pages) { |
| 289 | kfree(cap_info); |
| 290 | return -ENOMEM; |
| 291 | } |
| 292 | |
| 293 | file->private_data = cap_info; |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | static const struct file_operations efi_capsule_fops = { |
| 299 | .owner = THIS_MODULE, |
| 300 | .open = efi_capsule_open, |
| 301 | .write = efi_capsule_write, |
| 302 | .flush = efi_capsule_flush, |
| 303 | .release = efi_capsule_release, |
| 304 | .llseek = no_llseek, |
| 305 | }; |
| 306 | |
| 307 | static struct miscdevice efi_capsule_misc = { |
| 308 | .minor = MISC_DYNAMIC_MINOR, |
| 309 | .name = "efi_capsule_loader", |
| 310 | .fops = &efi_capsule_fops, |
| 311 | }; |
| 312 | |
| 313 | static int __init efi_capsule_loader_init(void) |
| 314 | { |
| 315 | int ret; |
| 316 | |
| 317 | if (!efi_enabled(EFI_RUNTIME_SERVICES)) |
| 318 | return -ENODEV; |
| 319 | |
| 320 | ret = misc_register(&efi_capsule_misc); |
| 321 | if (ret) |
Jan Kiszka | 5dce14b | 2017-06-02 13:51:58 +0000 | [diff] [blame^] | 322 | pr_err("Unable to register capsule loader device\n"); |
Kweh, Hock Leong | 65117f1 | 2016-04-25 21:07:01 +0100 | [diff] [blame] | 323 | |
| 324 | return ret; |
| 325 | } |
| 326 | module_init(efi_capsule_loader_init); |
| 327 | |
| 328 | static void __exit efi_capsule_loader_exit(void) |
| 329 | { |
| 330 | misc_deregister(&efi_capsule_misc); |
| 331 | } |
| 332 | module_exit(efi_capsule_loader_exit); |
| 333 | |
| 334 | MODULE_DESCRIPTION("EFI capsule firmware binary loader"); |
| 335 | MODULE_LICENSE("GPL v2"); |