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