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 2365117f1aSKweh, Hock Leong struct capsule_info { 2465117f1aSKweh, Hock Leong bool header_obtained; 2565117f1aSKweh, Hock Leong int reset_type; 2665117f1aSKweh, Hock Leong long index; 2765117f1aSKweh, Hock Leong size_t count; 2865117f1aSKweh, Hock Leong size_t total_size; 2965117f1aSKweh, Hock Leong struct page **pages; 3065117f1aSKweh, Hock Leong size_t page_bytes_remain; 3165117f1aSKweh, Hock Leong }; 3265117f1aSKweh, Hock Leong 3365117f1aSKweh, Hock Leong /** 3465117f1aSKweh, Hock Leong * efi_free_all_buff_pages - free all previous allocated buffer pages 3565117f1aSKweh, Hock Leong * @cap_info: pointer to current instance of capsule_info structure 3665117f1aSKweh, Hock Leong * 3765117f1aSKweh, Hock Leong * In addition to freeing buffer pages, it flags NO_FURTHER_WRITE_ACTION 3865117f1aSKweh, Hock Leong * to cease processing data in subsequent write(2) calls until close(2) 3965117f1aSKweh, Hock Leong * is called. 4065117f1aSKweh, Hock Leong **/ 4165117f1aSKweh, Hock Leong static void efi_free_all_buff_pages(struct capsule_info *cap_info) 4265117f1aSKweh, Hock Leong { 4365117f1aSKweh, Hock Leong while (cap_info->index > 0) 4465117f1aSKweh, Hock Leong __free_page(cap_info->pages[--cap_info->index]); 4565117f1aSKweh, Hock Leong 4665117f1aSKweh, Hock Leong cap_info->index = NO_FURTHER_WRITE_ACTION; 4765117f1aSKweh, Hock Leong } 4865117f1aSKweh, Hock Leong 4965117f1aSKweh, Hock Leong /** 5065117f1aSKweh, Hock Leong * efi_capsule_setup_info - obtain the efi capsule header in the binary and 5165117f1aSKweh, Hock Leong * setup capsule_info structure 5265117f1aSKweh, Hock Leong * @cap_info: pointer to current instance of capsule_info structure 5365117f1aSKweh, Hock Leong * @kbuff: a mapped first page buffer pointer 5465117f1aSKweh, Hock Leong * @hdr_bytes: the total received number of bytes for efi header 5565117f1aSKweh, Hock Leong **/ 5665117f1aSKweh, Hock Leong static ssize_t efi_capsule_setup_info(struct capsule_info *cap_info, 5765117f1aSKweh, Hock Leong void *kbuff, size_t hdr_bytes) 5865117f1aSKweh, Hock Leong { 5965117f1aSKweh, Hock Leong efi_capsule_header_t *cap_hdr; 6065117f1aSKweh, Hock Leong size_t pages_needed; 6165117f1aSKweh, Hock Leong int ret; 6265117f1aSKweh, Hock Leong void *temp_page; 6365117f1aSKweh, Hock Leong 6465117f1aSKweh, Hock Leong /* Only process data block that is larger than efi header size */ 6565117f1aSKweh, Hock Leong if (hdr_bytes < sizeof(efi_capsule_header_t)) 6665117f1aSKweh, Hock Leong return 0; 6765117f1aSKweh, Hock Leong 6865117f1aSKweh, Hock Leong /* Reset back to the correct offset of header */ 6965117f1aSKweh, Hock Leong cap_hdr = kbuff - cap_info->count; 7065117f1aSKweh, Hock Leong pages_needed = ALIGN(cap_hdr->imagesize, PAGE_SIZE) >> PAGE_SHIFT; 7165117f1aSKweh, Hock Leong 7265117f1aSKweh, Hock Leong if (pages_needed == 0) { 7365117f1aSKweh, Hock Leong pr_err("%s: pages count invalid\n", __func__); 7465117f1aSKweh, Hock Leong return -EINVAL; 7565117f1aSKweh, Hock Leong } 7665117f1aSKweh, Hock Leong 7765117f1aSKweh, Hock Leong /* Check if the capsule binary supported */ 7865117f1aSKweh, Hock Leong ret = efi_capsule_supported(cap_hdr->guid, cap_hdr->flags, 7965117f1aSKweh, Hock Leong cap_hdr->imagesize, 8065117f1aSKweh, Hock Leong &cap_info->reset_type); 8165117f1aSKweh, Hock Leong if (ret) { 8265117f1aSKweh, Hock Leong pr_err("%s: efi_capsule_supported() failed\n", 8365117f1aSKweh, Hock Leong __func__); 8465117f1aSKweh, Hock Leong return ret; 8565117f1aSKweh, Hock Leong } 8665117f1aSKweh, Hock Leong 8765117f1aSKweh, Hock Leong cap_info->total_size = cap_hdr->imagesize; 8865117f1aSKweh, Hock Leong temp_page = krealloc(cap_info->pages, 8965117f1aSKweh, Hock Leong pages_needed * sizeof(void *), 9065117f1aSKweh, Hock Leong GFP_KERNEL | __GFP_ZERO); 91*7367633fSJan Kiszka if (!temp_page) 9265117f1aSKweh, Hock Leong return -ENOMEM; 9365117f1aSKweh, Hock Leong 9465117f1aSKweh, Hock Leong cap_info->pages = temp_page; 9565117f1aSKweh, Hock Leong cap_info->header_obtained = true; 9665117f1aSKweh, Hock Leong 9765117f1aSKweh, Hock Leong return 0; 9865117f1aSKweh, Hock Leong } 9965117f1aSKweh, Hock Leong 10065117f1aSKweh, Hock Leong /** 10165117f1aSKweh, Hock Leong * efi_capsule_submit_update - invoke the efi_capsule_update API once binary 10265117f1aSKweh, Hock Leong * upload done 10365117f1aSKweh, Hock Leong * @cap_info: pointer to current instance of capsule_info structure 10465117f1aSKweh, Hock Leong **/ 10565117f1aSKweh, Hock Leong static ssize_t efi_capsule_submit_update(struct capsule_info *cap_info) 10665117f1aSKweh, Hock Leong { 10765117f1aSKweh, Hock Leong int ret; 10865117f1aSKweh, Hock Leong void *cap_hdr_temp; 10965117f1aSKweh, Hock Leong 1106862e6adSAustin Christ cap_hdr_temp = vmap(cap_info->pages, cap_info->index, 1116862e6adSAustin Christ VM_MAP, PAGE_KERNEL); 112*7367633fSJan Kiszka if (!cap_hdr_temp) 113fb153dc5SJan Kiszka return -ENOMEM; 11465117f1aSKweh, Hock Leong 11565117f1aSKweh, Hock Leong ret = efi_capsule_update(cap_hdr_temp, cap_info->pages); 1166862e6adSAustin Christ vunmap(cap_hdr_temp); 11765117f1aSKweh, Hock Leong if (ret) { 11865117f1aSKweh, Hock Leong pr_err("%s: efi_capsule_update() failed\n", __func__); 11965117f1aSKweh, Hock Leong return ret; 12065117f1aSKweh, Hock Leong } 12165117f1aSKweh, Hock Leong 12265117f1aSKweh, Hock Leong /* Indicate capsule binary uploading is done */ 12365117f1aSKweh, Hock Leong cap_info->index = NO_FURTHER_WRITE_ACTION; 12465117f1aSKweh, Hock Leong pr_info("%s: Successfully upload capsule file with reboot type '%s'\n", 12565117f1aSKweh, Hock Leong __func__, !cap_info->reset_type ? "RESET_COLD" : 12665117f1aSKweh, Hock Leong cap_info->reset_type == 1 ? "RESET_WARM" : 12765117f1aSKweh, Hock Leong "RESET_SHUTDOWN"); 12865117f1aSKweh, Hock Leong return 0; 12965117f1aSKweh, Hock Leong } 13065117f1aSKweh, Hock Leong 13165117f1aSKweh, Hock Leong /** 13265117f1aSKweh, Hock Leong * efi_capsule_write - store the capsule binary and pass it to 13365117f1aSKweh, Hock Leong * efi_capsule_update() API 13465117f1aSKweh, Hock Leong * @file: file pointer 13565117f1aSKweh, Hock Leong * @buff: buffer pointer 13665117f1aSKweh, Hock Leong * @count: number of bytes in @buff 13765117f1aSKweh, Hock Leong * @offp: not used 13865117f1aSKweh, Hock Leong * 13965117f1aSKweh, Hock Leong * Expectation: 14065117f1aSKweh, Hock Leong * - A user space tool should start at the beginning of capsule binary and 14165117f1aSKweh, Hock Leong * pass data in sequentially. 14265117f1aSKweh, Hock Leong * - Users should close and re-open this file note in order to upload more 14365117f1aSKweh, Hock Leong * capsules. 14465117f1aSKweh, Hock Leong * - After an error returned, user should close the file and restart the 14565117f1aSKweh, Hock Leong * operation for the next try otherwise -EIO will be returned until the 14665117f1aSKweh, Hock Leong * file is closed. 14765117f1aSKweh, Hock Leong * - An EFI capsule header must be located at the beginning of capsule 14865117f1aSKweh, Hock Leong * binary file and passed in as first block data of write operation. 14965117f1aSKweh, Hock Leong **/ 15065117f1aSKweh, Hock Leong static ssize_t efi_capsule_write(struct file *file, const char __user *buff, 15165117f1aSKweh, Hock Leong size_t count, loff_t *offp) 15265117f1aSKweh, Hock Leong { 15365117f1aSKweh, Hock Leong int ret = 0; 15465117f1aSKweh, Hock Leong struct capsule_info *cap_info = file->private_data; 15565117f1aSKweh, Hock Leong struct page *page; 15665117f1aSKweh, Hock Leong void *kbuff = NULL; 15765117f1aSKweh, Hock Leong size_t write_byte; 15865117f1aSKweh, Hock Leong 15965117f1aSKweh, Hock Leong if (count == 0) 16065117f1aSKweh, Hock Leong return 0; 16165117f1aSKweh, Hock Leong 16265117f1aSKweh, Hock Leong /* Return error while NO_FURTHER_WRITE_ACTION is flagged */ 16365117f1aSKweh, Hock Leong if (cap_info->index < 0) 16465117f1aSKweh, Hock Leong return -EIO; 16565117f1aSKweh, Hock Leong 16665117f1aSKweh, Hock Leong /* Only alloc a new page when previous page is full */ 16765117f1aSKweh, Hock Leong if (!cap_info->page_bytes_remain) { 16865117f1aSKweh, Hock Leong page = alloc_page(GFP_KERNEL); 16965117f1aSKweh, Hock Leong if (!page) { 17065117f1aSKweh, Hock Leong ret = -ENOMEM; 17165117f1aSKweh, Hock Leong goto failed; 17265117f1aSKweh, Hock Leong } 17365117f1aSKweh, Hock Leong 17465117f1aSKweh, Hock Leong cap_info->pages[cap_info->index++] = page; 17565117f1aSKweh, Hock Leong cap_info->page_bytes_remain = PAGE_SIZE; 17665117f1aSKweh, Hock Leong } 17765117f1aSKweh, Hock Leong 17865117f1aSKweh, Hock Leong page = cap_info->pages[cap_info->index - 1]; 17965117f1aSKweh, Hock Leong 18065117f1aSKweh, Hock Leong kbuff = kmap(page); 18165117f1aSKweh, Hock Leong if (!kbuff) { 182fb153dc5SJan Kiszka ret = -ENOMEM; 18365117f1aSKweh, Hock Leong goto failed; 18465117f1aSKweh, Hock Leong } 18565117f1aSKweh, Hock Leong kbuff += PAGE_SIZE - cap_info->page_bytes_remain; 18665117f1aSKweh, Hock Leong 18765117f1aSKweh, Hock Leong /* Copy capsule binary data from user space to kernel space buffer */ 18865117f1aSKweh, Hock Leong write_byte = min_t(size_t, count, cap_info->page_bytes_remain); 18965117f1aSKweh, Hock Leong if (copy_from_user(kbuff, buff, write_byte)) { 19065117f1aSKweh, Hock Leong ret = -EFAULT; 19165117f1aSKweh, Hock Leong goto fail_unmap; 19265117f1aSKweh, Hock Leong } 19365117f1aSKweh, Hock Leong cap_info->page_bytes_remain -= write_byte; 19465117f1aSKweh, Hock Leong 19565117f1aSKweh, Hock Leong /* Setup capsule binary info structure */ 19665117f1aSKweh, Hock Leong if (!cap_info->header_obtained) { 19765117f1aSKweh, Hock Leong ret = efi_capsule_setup_info(cap_info, kbuff, 19865117f1aSKweh, Hock Leong cap_info->count + write_byte); 19965117f1aSKweh, Hock Leong if (ret) 20065117f1aSKweh, Hock Leong goto fail_unmap; 20165117f1aSKweh, Hock Leong } 20265117f1aSKweh, Hock Leong 20365117f1aSKweh, Hock Leong cap_info->count += write_byte; 20465117f1aSKweh, Hock Leong kunmap(page); 20565117f1aSKweh, Hock Leong 20665117f1aSKweh, Hock Leong /* Submit the full binary to efi_capsule_update() API */ 20765117f1aSKweh, Hock Leong if (cap_info->header_obtained && 20865117f1aSKweh, Hock Leong cap_info->count >= cap_info->total_size) { 20965117f1aSKweh, Hock Leong if (cap_info->count > cap_info->total_size) { 21065117f1aSKweh, Hock Leong pr_err("%s: upload size exceeded header defined size\n", 21165117f1aSKweh, Hock Leong __func__); 21265117f1aSKweh, Hock Leong ret = -EINVAL; 21365117f1aSKweh, Hock Leong goto failed; 21465117f1aSKweh, Hock Leong } 21565117f1aSKweh, Hock Leong 21665117f1aSKweh, Hock Leong ret = efi_capsule_submit_update(cap_info); 21765117f1aSKweh, Hock Leong if (ret) 21865117f1aSKweh, Hock Leong goto failed; 21965117f1aSKweh, Hock Leong } 22065117f1aSKweh, Hock Leong 22165117f1aSKweh, Hock Leong return write_byte; 22265117f1aSKweh, Hock Leong 22365117f1aSKweh, Hock Leong fail_unmap: 22465117f1aSKweh, Hock Leong kunmap(page); 22565117f1aSKweh, Hock Leong failed: 22665117f1aSKweh, Hock Leong efi_free_all_buff_pages(cap_info); 22765117f1aSKweh, Hock Leong return ret; 22865117f1aSKweh, Hock Leong } 22965117f1aSKweh, Hock Leong 23065117f1aSKweh, Hock Leong /** 23165117f1aSKweh, Hock Leong * efi_capsule_flush - called by file close or file flush 23265117f1aSKweh, Hock Leong * @file: file pointer 23365117f1aSKweh, Hock Leong * @id: not used 23465117f1aSKweh, Hock Leong * 23565117f1aSKweh, Hock Leong * If a capsule is being partially uploaded then calling this function 23665117f1aSKweh, Hock Leong * will be treated as upload termination and will free those completed 23765117f1aSKweh, Hock Leong * buffer pages and -ECANCELED will be returned. 23865117f1aSKweh, Hock Leong **/ 23965117f1aSKweh, Hock Leong static int efi_capsule_flush(struct file *file, fl_owner_t id) 24065117f1aSKweh, Hock Leong { 24165117f1aSKweh, Hock Leong int ret = 0; 24265117f1aSKweh, Hock Leong struct capsule_info *cap_info = file->private_data; 24365117f1aSKweh, Hock Leong 24465117f1aSKweh, Hock Leong if (cap_info->index > 0) { 24565117f1aSKweh, Hock Leong pr_err("%s: capsule upload not complete\n", __func__); 24665117f1aSKweh, Hock Leong efi_free_all_buff_pages(cap_info); 24765117f1aSKweh, Hock Leong ret = -ECANCELED; 24865117f1aSKweh, Hock Leong } 24965117f1aSKweh, Hock Leong 25065117f1aSKweh, Hock Leong return ret; 25165117f1aSKweh, Hock Leong } 25265117f1aSKweh, Hock Leong 25365117f1aSKweh, Hock Leong /** 25465117f1aSKweh, Hock Leong * efi_capsule_release - called by file close 25565117f1aSKweh, Hock Leong * @inode: not used 25665117f1aSKweh, Hock Leong * @file: file pointer 25765117f1aSKweh, Hock Leong * 25865117f1aSKweh, Hock Leong * We will not free successfully submitted pages since efi update 25965117f1aSKweh, Hock Leong * requires data to be maintained across system reboot. 26065117f1aSKweh, Hock Leong **/ 26165117f1aSKweh, Hock Leong static int efi_capsule_release(struct inode *inode, struct file *file) 26265117f1aSKweh, Hock Leong { 26365117f1aSKweh, Hock Leong struct capsule_info *cap_info = file->private_data; 26465117f1aSKweh, Hock Leong 26565117f1aSKweh, Hock Leong kfree(cap_info->pages); 26665117f1aSKweh, Hock Leong kfree(file->private_data); 26765117f1aSKweh, Hock Leong file->private_data = NULL; 26865117f1aSKweh, Hock Leong return 0; 26965117f1aSKweh, Hock Leong } 27065117f1aSKweh, Hock Leong 27165117f1aSKweh, Hock Leong /** 27265117f1aSKweh, Hock Leong * efi_capsule_open - called by file open 27365117f1aSKweh, Hock Leong * @inode: not used 27465117f1aSKweh, Hock Leong * @file: file pointer 27565117f1aSKweh, Hock Leong * 27665117f1aSKweh, Hock Leong * Will allocate each capsule_info memory for each file open call. 27765117f1aSKweh, Hock Leong * This provided the capability to support multiple file open feature 27865117f1aSKweh, Hock Leong * where user is not needed to wait for others to finish in order to 27965117f1aSKweh, Hock Leong * upload their capsule binary. 28065117f1aSKweh, Hock Leong **/ 28165117f1aSKweh, Hock Leong static int efi_capsule_open(struct inode *inode, struct file *file) 28265117f1aSKweh, Hock Leong { 28365117f1aSKweh, Hock Leong struct capsule_info *cap_info; 28465117f1aSKweh, Hock Leong 28565117f1aSKweh, Hock Leong cap_info = kzalloc(sizeof(*cap_info), GFP_KERNEL); 28665117f1aSKweh, Hock Leong if (!cap_info) 28765117f1aSKweh, Hock Leong return -ENOMEM; 28865117f1aSKweh, Hock Leong 28965117f1aSKweh, Hock Leong cap_info->pages = kzalloc(sizeof(void *), GFP_KERNEL); 29065117f1aSKweh, Hock Leong if (!cap_info->pages) { 29165117f1aSKweh, Hock Leong kfree(cap_info); 29265117f1aSKweh, Hock Leong return -ENOMEM; 29365117f1aSKweh, Hock Leong } 29465117f1aSKweh, Hock Leong 29565117f1aSKweh, Hock Leong file->private_data = cap_info; 29665117f1aSKweh, Hock Leong 29765117f1aSKweh, Hock Leong return 0; 29865117f1aSKweh, Hock Leong } 29965117f1aSKweh, Hock Leong 30065117f1aSKweh, Hock Leong static const struct file_operations efi_capsule_fops = { 30165117f1aSKweh, Hock Leong .owner = THIS_MODULE, 30265117f1aSKweh, Hock Leong .open = efi_capsule_open, 30365117f1aSKweh, Hock Leong .write = efi_capsule_write, 30465117f1aSKweh, Hock Leong .flush = efi_capsule_flush, 30565117f1aSKweh, Hock Leong .release = efi_capsule_release, 30665117f1aSKweh, Hock Leong .llseek = no_llseek, 30765117f1aSKweh, Hock Leong }; 30865117f1aSKweh, Hock Leong 30965117f1aSKweh, Hock Leong static struct miscdevice efi_capsule_misc = { 31065117f1aSKweh, Hock Leong .minor = MISC_DYNAMIC_MINOR, 31165117f1aSKweh, Hock Leong .name = "efi_capsule_loader", 31265117f1aSKweh, Hock Leong .fops = &efi_capsule_fops, 31365117f1aSKweh, Hock Leong }; 31465117f1aSKweh, Hock Leong 31565117f1aSKweh, Hock Leong static int __init efi_capsule_loader_init(void) 31665117f1aSKweh, Hock Leong { 31765117f1aSKweh, Hock Leong int ret; 31865117f1aSKweh, Hock Leong 31965117f1aSKweh, Hock Leong if (!efi_enabled(EFI_RUNTIME_SERVICES)) 32065117f1aSKweh, Hock Leong return -ENODEV; 32165117f1aSKweh, Hock Leong 32265117f1aSKweh, Hock Leong ret = misc_register(&efi_capsule_misc); 32365117f1aSKweh, Hock Leong if (ret) 32465117f1aSKweh, Hock Leong pr_err("%s: Failed to register misc char file note\n", 32565117f1aSKweh, Hock Leong __func__); 32665117f1aSKweh, Hock Leong 32765117f1aSKweh, Hock Leong return ret; 32865117f1aSKweh, Hock Leong } 32965117f1aSKweh, Hock Leong module_init(efi_capsule_loader_init); 33065117f1aSKweh, Hock Leong 33165117f1aSKweh, Hock Leong static void __exit efi_capsule_loader_exit(void) 33265117f1aSKweh, Hock Leong { 33365117f1aSKweh, Hock Leong misc_deregister(&efi_capsule_misc); 33465117f1aSKweh, Hock Leong } 33565117f1aSKweh, Hock Leong module_exit(efi_capsule_loader_exit); 33665117f1aSKweh, Hock Leong 33765117f1aSKweh, Hock Leong MODULE_DESCRIPTION("EFI capsule firmware binary loader"); 33865117f1aSKweh, Hock Leong MODULE_LICENSE("GPL v2"); 339