165117f1aSKweh, Hock Leong /* 265117f1aSKweh, Hock Leong * EFI capsule loader driver. 365117f1aSKweh, Hock Leong * 465117f1aSKweh, Hock Leong * Copyright 2015 Intel Corporation 565117f1aSKweh, Hock Leong * 665117f1aSKweh, Hock Leong * This file is part of the Linux kernel, and is made available under 765117f1aSKweh, Hock Leong * the terms of the GNU General Public License version 2. 865117f1aSKweh, Hock Leong */ 965117f1aSKweh, Hock Leong 1065117f1aSKweh, Hock Leong #define pr_fmt(fmt) "efi: " fmt 1165117f1aSKweh, Hock Leong 1265117f1aSKweh, Hock Leong #include <linux/kernel.h> 1365117f1aSKweh, Hock Leong #include <linux/module.h> 1465117f1aSKweh, Hock Leong #include <linux/miscdevice.h> 1565117f1aSKweh, Hock Leong #include <linux/highmem.h> 1665117f1aSKweh, Hock Leong #include <linux/slab.h> 1765117f1aSKweh, Hock Leong #include <linux/mutex.h> 1865117f1aSKweh, Hock Leong #include <linux/efi.h> 196862e6adSAustin Christ #include <linux/vmalloc.h> 2065117f1aSKweh, Hock Leong 2165117f1aSKweh, Hock Leong #define NO_FURTHER_WRITE_ACTION -1 2265117f1aSKweh, Hock Leong 23*2a457fb3SArd Biesheuvel #ifndef phys_to_page 24*2a457fb3SArd Biesheuvel #define phys_to_page(x) pfn_to_page((x) >> PAGE_SHIFT) 25*2a457fb3SArd Biesheuvel #endif 26*2a457fb3SArd Biesheuvel 2765117f1aSKweh, Hock Leong /** 2865117f1aSKweh, Hock Leong * efi_free_all_buff_pages - free all previous allocated buffer pages 2965117f1aSKweh, Hock Leong * @cap_info: pointer to current instance of capsule_info structure 3065117f1aSKweh, Hock Leong * 3165117f1aSKweh, Hock Leong * In addition to freeing buffer pages, it flags NO_FURTHER_WRITE_ACTION 3265117f1aSKweh, Hock Leong * to cease processing data in subsequent write(2) calls until close(2) 3365117f1aSKweh, Hock Leong * is called. 3465117f1aSKweh, Hock Leong **/ 3565117f1aSKweh, Hock Leong static void efi_free_all_buff_pages(struct capsule_info *cap_info) 3665117f1aSKweh, Hock Leong { 3765117f1aSKweh, Hock Leong while (cap_info->index > 0) 38*2a457fb3SArd Biesheuvel __free_page(phys_to_page(cap_info->pages[--cap_info->index])); 3965117f1aSKweh, Hock Leong 4065117f1aSKweh, Hock Leong cap_info->index = NO_FURTHER_WRITE_ACTION; 4165117f1aSKweh, Hock Leong } 4265117f1aSKweh, Hock Leong 433fabd628SArd Biesheuvel int __efi_capsule_setup_info(struct capsule_info *cap_info) 4465117f1aSKweh, Hock Leong { 4565117f1aSKweh, Hock Leong size_t pages_needed; 4665117f1aSKweh, Hock Leong int ret; 4765117f1aSKweh, Hock Leong void *temp_page; 4865117f1aSKweh, Hock Leong 493fabd628SArd Biesheuvel pages_needed = ALIGN(cap_info->total_size, PAGE_SIZE) / PAGE_SIZE; 5065117f1aSKweh, Hock Leong 5165117f1aSKweh, Hock Leong if (pages_needed == 0) { 525dce14b9SJan Kiszka pr_err("invalid capsule size"); 5365117f1aSKweh, Hock Leong return -EINVAL; 5465117f1aSKweh, Hock Leong } 5565117f1aSKweh, Hock Leong 5665117f1aSKweh, Hock Leong /* Check if the capsule binary supported */ 5782c3768bSArd Biesheuvel ret = efi_capsule_supported(cap_info->header.guid, 5882c3768bSArd Biesheuvel cap_info->header.flags, 5982c3768bSArd Biesheuvel cap_info->header.imagesize, 6065117f1aSKweh, Hock Leong &cap_info->reset_type); 6165117f1aSKweh, Hock Leong if (ret) { 625dce14b9SJan Kiszka pr_err("capsule not supported\n"); 6365117f1aSKweh, Hock Leong return ret; 6465117f1aSKweh, Hock Leong } 6565117f1aSKweh, Hock Leong 6665117f1aSKweh, Hock Leong temp_page = krealloc(cap_info->pages, 6765117f1aSKweh, Hock Leong pages_needed * sizeof(void *), 6865117f1aSKweh, Hock Leong GFP_KERNEL | __GFP_ZERO); 697367633fSJan Kiszka if (!temp_page) 7065117f1aSKweh, Hock Leong return -ENOMEM; 7165117f1aSKweh, Hock Leong 7265117f1aSKweh, Hock Leong cap_info->pages = temp_page; 7365117f1aSKweh, Hock Leong 7465117f1aSKweh, Hock Leong return 0; 7565117f1aSKweh, Hock Leong } 7665117f1aSKweh, Hock Leong 7765117f1aSKweh, Hock Leong /** 783fabd628SArd Biesheuvel * efi_capsule_setup_info - obtain the efi capsule header in the binary and 793fabd628SArd Biesheuvel * setup capsule_info structure 803fabd628SArd Biesheuvel * @cap_info: pointer to current instance of capsule_info structure 813fabd628SArd Biesheuvel * @kbuff: a mapped first page buffer pointer 823fabd628SArd Biesheuvel * @hdr_bytes: the total received number of bytes for efi header 833fabd628SArd Biesheuvel * 843fabd628SArd Biesheuvel * Platforms with non-standard capsule update mechanisms can override 853fabd628SArd Biesheuvel * this __weak function so they can perform any required capsule 863fabd628SArd Biesheuvel * image munging. See quark_quirk_function() for an example. 873fabd628SArd Biesheuvel **/ 883fabd628SArd Biesheuvel int __weak efi_capsule_setup_info(struct capsule_info *cap_info, void *kbuff, 893fabd628SArd Biesheuvel size_t hdr_bytes) 903fabd628SArd Biesheuvel { 913fabd628SArd Biesheuvel /* Only process data block that is larger than efi header size */ 923fabd628SArd Biesheuvel if (hdr_bytes < sizeof(efi_capsule_header_t)) 933fabd628SArd Biesheuvel return 0; 943fabd628SArd Biesheuvel 953fabd628SArd Biesheuvel memcpy(&cap_info->header, kbuff, sizeof(cap_info->header)); 963fabd628SArd Biesheuvel cap_info->total_size = cap_info->header.imagesize; 973fabd628SArd Biesheuvel 983fabd628SArd Biesheuvel return __efi_capsule_setup_info(cap_info); 993fabd628SArd Biesheuvel } 1003fabd628SArd Biesheuvel 1013fabd628SArd Biesheuvel /** 10265117f1aSKweh, Hock Leong * efi_capsule_submit_update - invoke the efi_capsule_update API once binary 10365117f1aSKweh, Hock Leong * upload done 10465117f1aSKweh, Hock Leong * @cap_info: pointer to current instance of capsule_info structure 10565117f1aSKweh, Hock Leong **/ 10665117f1aSKweh, Hock Leong static ssize_t efi_capsule_submit_update(struct capsule_info *cap_info) 10765117f1aSKweh, Hock Leong { 10865117f1aSKweh, Hock Leong int ret; 10965117f1aSKweh, Hock Leong 11082c3768bSArd Biesheuvel ret = efi_capsule_update(&cap_info->header, cap_info->pages); 11165117f1aSKweh, Hock Leong if (ret) { 1125dce14b9SJan Kiszka pr_err("capsule update failed\n"); 11365117f1aSKweh, Hock Leong return ret; 11465117f1aSKweh, Hock Leong } 11565117f1aSKweh, Hock Leong 11665117f1aSKweh, Hock Leong /* Indicate capsule binary uploading is done */ 11765117f1aSKweh, Hock Leong cap_info->index = NO_FURTHER_WRITE_ACTION; 1185dce14b9SJan Kiszka pr_info("Successfully upload capsule file with reboot type '%s'\n", 1195dce14b9SJan Kiszka !cap_info->reset_type ? "RESET_COLD" : 12065117f1aSKweh, Hock Leong cap_info->reset_type == 1 ? "RESET_WARM" : 12165117f1aSKweh, Hock Leong "RESET_SHUTDOWN"); 12265117f1aSKweh, Hock Leong return 0; 12365117f1aSKweh, Hock Leong } 12465117f1aSKweh, Hock Leong 12565117f1aSKweh, Hock Leong /** 12665117f1aSKweh, Hock Leong * efi_capsule_write - store the capsule binary and pass it to 12765117f1aSKweh, Hock Leong * efi_capsule_update() API 12865117f1aSKweh, Hock Leong * @file: file pointer 12965117f1aSKweh, Hock Leong * @buff: buffer pointer 13065117f1aSKweh, Hock Leong * @count: number of bytes in @buff 13165117f1aSKweh, Hock Leong * @offp: not used 13265117f1aSKweh, Hock Leong * 13365117f1aSKweh, Hock Leong * Expectation: 13465117f1aSKweh, Hock Leong * - A user space tool should start at the beginning of capsule binary and 13565117f1aSKweh, Hock Leong * pass data in sequentially. 13665117f1aSKweh, Hock Leong * - Users should close and re-open this file note in order to upload more 13765117f1aSKweh, Hock Leong * capsules. 13865117f1aSKweh, Hock Leong * - After an error returned, user should close the file and restart the 13965117f1aSKweh, Hock Leong * operation for the next try otherwise -EIO will be returned until the 14065117f1aSKweh, Hock Leong * file is closed. 14165117f1aSKweh, Hock Leong * - An EFI capsule header must be located at the beginning of capsule 14265117f1aSKweh, Hock Leong * binary file and passed in as first block data of write operation. 14365117f1aSKweh, Hock Leong **/ 14465117f1aSKweh, Hock Leong static ssize_t efi_capsule_write(struct file *file, const char __user *buff, 14565117f1aSKweh, Hock Leong size_t count, loff_t *offp) 14665117f1aSKweh, Hock Leong { 14765117f1aSKweh, Hock Leong int ret = 0; 14865117f1aSKweh, Hock Leong struct capsule_info *cap_info = file->private_data; 14965117f1aSKweh, Hock Leong struct page *page; 15065117f1aSKweh, Hock Leong void *kbuff = NULL; 15165117f1aSKweh, Hock Leong size_t write_byte; 15265117f1aSKweh, Hock Leong 15365117f1aSKweh, Hock Leong if (count == 0) 15465117f1aSKweh, Hock Leong return 0; 15565117f1aSKweh, Hock Leong 15665117f1aSKweh, Hock Leong /* Return error while NO_FURTHER_WRITE_ACTION is flagged */ 15765117f1aSKweh, Hock Leong if (cap_info->index < 0) 15865117f1aSKweh, Hock Leong return -EIO; 15965117f1aSKweh, Hock Leong 16065117f1aSKweh, Hock Leong /* Only alloc a new page when previous page is full */ 16165117f1aSKweh, Hock Leong if (!cap_info->page_bytes_remain) { 16265117f1aSKweh, Hock Leong page = alloc_page(GFP_KERNEL); 16365117f1aSKweh, Hock Leong if (!page) { 16465117f1aSKweh, Hock Leong ret = -ENOMEM; 16565117f1aSKweh, Hock Leong goto failed; 16665117f1aSKweh, Hock Leong } 16765117f1aSKweh, Hock Leong 168*2a457fb3SArd Biesheuvel cap_info->pages[cap_info->index++] = page_to_phys(page); 16965117f1aSKweh, Hock Leong cap_info->page_bytes_remain = PAGE_SIZE; 170*2a457fb3SArd Biesheuvel } else { 171*2a457fb3SArd Biesheuvel page = phys_to_page(cap_info->pages[cap_info->index - 1]); 17265117f1aSKweh, Hock Leong } 17365117f1aSKweh, Hock Leong 17465117f1aSKweh, Hock Leong kbuff = kmap(page); 17565117f1aSKweh, Hock Leong kbuff += PAGE_SIZE - cap_info->page_bytes_remain; 17665117f1aSKweh, Hock Leong 17765117f1aSKweh, Hock Leong /* Copy capsule binary data from user space to kernel space buffer */ 17865117f1aSKweh, Hock Leong write_byte = min_t(size_t, count, cap_info->page_bytes_remain); 17965117f1aSKweh, Hock Leong if (copy_from_user(kbuff, buff, write_byte)) { 18065117f1aSKweh, Hock Leong ret = -EFAULT; 18165117f1aSKweh, Hock Leong goto fail_unmap; 18265117f1aSKweh, Hock Leong } 18365117f1aSKweh, Hock Leong cap_info->page_bytes_remain -= write_byte; 18465117f1aSKweh, Hock Leong 18565117f1aSKweh, Hock Leong /* Setup capsule binary info structure */ 18682c3768bSArd Biesheuvel if (cap_info->header.headersize == 0) { 1873fabd628SArd Biesheuvel ret = efi_capsule_setup_info(cap_info, kbuff - cap_info->count, 18865117f1aSKweh, Hock Leong cap_info->count + write_byte); 18965117f1aSKweh, Hock Leong if (ret) 19065117f1aSKweh, Hock Leong goto fail_unmap; 19165117f1aSKweh, Hock Leong } 19265117f1aSKweh, Hock Leong 19365117f1aSKweh, Hock Leong cap_info->count += write_byte; 19465117f1aSKweh, Hock Leong kunmap(page); 19565117f1aSKweh, Hock Leong 19665117f1aSKweh, Hock Leong /* Submit the full binary to efi_capsule_update() API */ 19782c3768bSArd Biesheuvel if (cap_info->header.headersize > 0 && 19865117f1aSKweh, Hock Leong cap_info->count >= cap_info->total_size) { 19965117f1aSKweh, Hock Leong if (cap_info->count > cap_info->total_size) { 2005dce14b9SJan Kiszka pr_err("capsule upload size exceeded header defined size\n"); 20165117f1aSKweh, Hock Leong ret = -EINVAL; 20265117f1aSKweh, Hock Leong goto failed; 20365117f1aSKweh, Hock Leong } 20465117f1aSKweh, Hock Leong 20565117f1aSKweh, Hock Leong ret = efi_capsule_submit_update(cap_info); 20665117f1aSKweh, Hock Leong if (ret) 20765117f1aSKweh, Hock Leong goto failed; 20865117f1aSKweh, Hock Leong } 20965117f1aSKweh, Hock Leong 21065117f1aSKweh, Hock Leong return write_byte; 21165117f1aSKweh, Hock Leong 21265117f1aSKweh, Hock Leong fail_unmap: 21365117f1aSKweh, Hock Leong kunmap(page); 21465117f1aSKweh, Hock Leong failed: 21565117f1aSKweh, Hock Leong efi_free_all_buff_pages(cap_info); 21665117f1aSKweh, Hock Leong return ret; 21765117f1aSKweh, Hock Leong } 21865117f1aSKweh, Hock Leong 21965117f1aSKweh, Hock Leong /** 22065117f1aSKweh, Hock Leong * efi_capsule_flush - called by file close or file flush 22165117f1aSKweh, Hock Leong * @file: file pointer 22265117f1aSKweh, Hock Leong * @id: not used 22365117f1aSKweh, Hock Leong * 22465117f1aSKweh, Hock Leong * If a capsule is being partially uploaded then calling this function 22565117f1aSKweh, Hock Leong * will be treated as upload termination and will free those completed 22665117f1aSKweh, Hock Leong * buffer pages and -ECANCELED will be returned. 22765117f1aSKweh, Hock Leong **/ 22865117f1aSKweh, Hock Leong static int efi_capsule_flush(struct file *file, fl_owner_t id) 22965117f1aSKweh, Hock Leong { 23065117f1aSKweh, Hock Leong int ret = 0; 23165117f1aSKweh, Hock Leong struct capsule_info *cap_info = file->private_data; 23265117f1aSKweh, Hock Leong 23365117f1aSKweh, Hock Leong if (cap_info->index > 0) { 2345dce14b9SJan Kiszka pr_err("capsule upload not complete\n"); 23565117f1aSKweh, Hock Leong efi_free_all_buff_pages(cap_info); 23665117f1aSKweh, Hock Leong ret = -ECANCELED; 23765117f1aSKweh, Hock Leong } 23865117f1aSKweh, Hock Leong 23965117f1aSKweh, Hock Leong return ret; 24065117f1aSKweh, Hock Leong } 24165117f1aSKweh, Hock Leong 24265117f1aSKweh, Hock Leong /** 24365117f1aSKweh, Hock Leong * efi_capsule_release - called by file close 24465117f1aSKweh, Hock Leong * @inode: not used 24565117f1aSKweh, Hock Leong * @file: file pointer 24665117f1aSKweh, Hock Leong * 24765117f1aSKweh, Hock Leong * We will not free successfully submitted pages since efi update 24865117f1aSKweh, Hock Leong * requires data to be maintained across system reboot. 24965117f1aSKweh, Hock Leong **/ 25065117f1aSKweh, Hock Leong static int efi_capsule_release(struct inode *inode, struct file *file) 25165117f1aSKweh, Hock Leong { 25265117f1aSKweh, Hock Leong struct capsule_info *cap_info = file->private_data; 25365117f1aSKweh, Hock Leong 25465117f1aSKweh, Hock Leong kfree(cap_info->pages); 25565117f1aSKweh, Hock Leong kfree(file->private_data); 25665117f1aSKweh, Hock Leong file->private_data = NULL; 25765117f1aSKweh, Hock Leong return 0; 25865117f1aSKweh, Hock Leong } 25965117f1aSKweh, Hock Leong 26065117f1aSKweh, Hock Leong /** 26165117f1aSKweh, Hock Leong * efi_capsule_open - called by file open 26265117f1aSKweh, Hock Leong * @inode: not used 26365117f1aSKweh, Hock Leong * @file: file pointer 26465117f1aSKweh, Hock Leong * 26565117f1aSKweh, Hock Leong * Will allocate each capsule_info memory for each file open call. 26665117f1aSKweh, Hock Leong * This provided the capability to support multiple file open feature 26765117f1aSKweh, Hock Leong * where user is not needed to wait for others to finish in order to 26865117f1aSKweh, Hock Leong * upload their capsule binary. 26965117f1aSKweh, Hock Leong **/ 27065117f1aSKweh, Hock Leong static int efi_capsule_open(struct inode *inode, struct file *file) 27165117f1aSKweh, Hock Leong { 27265117f1aSKweh, Hock Leong struct capsule_info *cap_info; 27365117f1aSKweh, Hock Leong 27465117f1aSKweh, Hock Leong cap_info = kzalloc(sizeof(*cap_info), GFP_KERNEL); 27565117f1aSKweh, Hock Leong if (!cap_info) 27665117f1aSKweh, Hock Leong return -ENOMEM; 27765117f1aSKweh, Hock Leong 27865117f1aSKweh, Hock Leong cap_info->pages = kzalloc(sizeof(void *), GFP_KERNEL); 27965117f1aSKweh, Hock Leong if (!cap_info->pages) { 28065117f1aSKweh, Hock Leong kfree(cap_info); 28165117f1aSKweh, Hock Leong return -ENOMEM; 28265117f1aSKweh, Hock Leong } 28365117f1aSKweh, Hock Leong 28465117f1aSKweh, Hock Leong file->private_data = cap_info; 28565117f1aSKweh, Hock Leong 28665117f1aSKweh, Hock Leong return 0; 28765117f1aSKweh, Hock Leong } 28865117f1aSKweh, Hock Leong 28965117f1aSKweh, Hock Leong static const struct file_operations efi_capsule_fops = { 29065117f1aSKweh, Hock Leong .owner = THIS_MODULE, 29165117f1aSKweh, Hock Leong .open = efi_capsule_open, 29265117f1aSKweh, Hock Leong .write = efi_capsule_write, 29365117f1aSKweh, Hock Leong .flush = efi_capsule_flush, 29465117f1aSKweh, Hock Leong .release = efi_capsule_release, 29565117f1aSKweh, Hock Leong .llseek = no_llseek, 29665117f1aSKweh, Hock Leong }; 29765117f1aSKweh, Hock Leong 29865117f1aSKweh, Hock Leong static struct miscdevice efi_capsule_misc = { 29965117f1aSKweh, Hock Leong .minor = MISC_DYNAMIC_MINOR, 30065117f1aSKweh, Hock Leong .name = "efi_capsule_loader", 30165117f1aSKweh, Hock Leong .fops = &efi_capsule_fops, 30265117f1aSKweh, Hock Leong }; 30365117f1aSKweh, Hock Leong 30465117f1aSKweh, Hock Leong static int __init efi_capsule_loader_init(void) 30565117f1aSKweh, Hock Leong { 30665117f1aSKweh, Hock Leong int ret; 30765117f1aSKweh, Hock Leong 30865117f1aSKweh, Hock Leong if (!efi_enabled(EFI_RUNTIME_SERVICES)) 30965117f1aSKweh, Hock Leong return -ENODEV; 31065117f1aSKweh, Hock Leong 31165117f1aSKweh, Hock Leong ret = misc_register(&efi_capsule_misc); 31265117f1aSKweh, Hock Leong if (ret) 3135dce14b9SJan Kiszka pr_err("Unable to register capsule loader device\n"); 31465117f1aSKweh, Hock Leong 31565117f1aSKweh, Hock Leong return ret; 31665117f1aSKweh, Hock Leong } 31765117f1aSKweh, Hock Leong module_init(efi_capsule_loader_init); 31865117f1aSKweh, Hock Leong 31965117f1aSKweh, Hock Leong static void __exit efi_capsule_loader_exit(void) 32065117f1aSKweh, Hock Leong { 32165117f1aSKweh, Hock Leong misc_deregister(&efi_capsule_misc); 32265117f1aSKweh, Hock Leong } 32365117f1aSKweh, Hock Leong module_exit(efi_capsule_loader_exit); 32465117f1aSKweh, Hock Leong 32565117f1aSKweh, Hock Leong MODULE_DESCRIPTION("EFI capsule firmware binary loader"); 32665117f1aSKweh, Hock Leong MODULE_LICENSE("GPL v2"); 327