1*4febfb8dSArd Biesheuvel // SPDX-License-Identifier: GPL-2.0 265117f1aSKweh, Hock Leong /* 365117f1aSKweh, Hock Leong * EFI capsule loader driver. 465117f1aSKweh, Hock Leong * 565117f1aSKweh, Hock Leong * Copyright 2015 Intel Corporation 665117f1aSKweh, Hock Leong */ 765117f1aSKweh, Hock Leong 865117f1aSKweh, Hock Leong #define pr_fmt(fmt) "efi: " fmt 965117f1aSKweh, Hock Leong 1065117f1aSKweh, Hock Leong #include <linux/kernel.h> 1165117f1aSKweh, Hock Leong #include <linux/module.h> 1265117f1aSKweh, Hock Leong #include <linux/miscdevice.h> 1365117f1aSKweh, Hock Leong #include <linux/highmem.h> 1465117f1aSKweh, Hock Leong #include <linux/slab.h> 1565117f1aSKweh, Hock Leong #include <linux/mutex.h> 1665117f1aSKweh, Hock Leong #include <linux/efi.h> 176862e6adSAustin Christ #include <linux/vmalloc.h> 1865117f1aSKweh, Hock Leong 1965117f1aSKweh, Hock Leong #define NO_FURTHER_WRITE_ACTION -1 2065117f1aSKweh, Hock Leong 2165117f1aSKweh, Hock Leong /** 2265117f1aSKweh, Hock Leong * efi_free_all_buff_pages - free all previous allocated buffer pages 2365117f1aSKweh, Hock Leong * @cap_info: pointer to current instance of capsule_info structure 2465117f1aSKweh, Hock Leong * 2565117f1aSKweh, Hock Leong * In addition to freeing buffer pages, it flags NO_FURTHER_WRITE_ACTION 2665117f1aSKweh, Hock Leong * to cease processing data in subsequent write(2) calls until close(2) 2765117f1aSKweh, Hock Leong * is called. 2865117f1aSKweh, Hock Leong **/ 2965117f1aSKweh, Hock Leong static void efi_free_all_buff_pages(struct capsule_info *cap_info) 3065117f1aSKweh, Hock Leong { 3165117f1aSKweh, Hock Leong while (cap_info->index > 0) 32f24c4d47SArd Biesheuvel __free_page(cap_info->pages[--cap_info->index]); 3365117f1aSKweh, Hock Leong 3465117f1aSKweh, Hock Leong cap_info->index = NO_FURTHER_WRITE_ACTION; 3565117f1aSKweh, Hock Leong } 3665117f1aSKweh, Hock Leong 373fabd628SArd Biesheuvel int __efi_capsule_setup_info(struct capsule_info *cap_info) 3865117f1aSKweh, Hock Leong { 3965117f1aSKweh, Hock Leong size_t pages_needed; 4065117f1aSKweh, Hock Leong int ret; 4165117f1aSKweh, Hock Leong void *temp_page; 4265117f1aSKweh, Hock Leong 433fabd628SArd Biesheuvel pages_needed = ALIGN(cap_info->total_size, PAGE_SIZE) / PAGE_SIZE; 4465117f1aSKweh, Hock Leong 4565117f1aSKweh, Hock Leong if (pages_needed == 0) { 4676ad9dffSArvind Yadav pr_err("invalid capsule size\n"); 4765117f1aSKweh, Hock Leong return -EINVAL; 4865117f1aSKweh, Hock Leong } 4965117f1aSKweh, Hock Leong 5065117f1aSKweh, Hock Leong /* Check if the capsule binary supported */ 5182c3768bSArd Biesheuvel ret = efi_capsule_supported(cap_info->header.guid, 5282c3768bSArd Biesheuvel cap_info->header.flags, 5382c3768bSArd Biesheuvel cap_info->header.imagesize, 5465117f1aSKweh, Hock Leong &cap_info->reset_type); 5565117f1aSKweh, Hock Leong if (ret) { 565dce14b9SJan Kiszka pr_err("capsule not supported\n"); 5765117f1aSKweh, Hock Leong return ret; 5865117f1aSKweh, Hock Leong } 5965117f1aSKweh, Hock Leong 6065117f1aSKweh, Hock Leong temp_page = krealloc(cap_info->pages, 6165117f1aSKweh, Hock Leong pages_needed * sizeof(void *), 6265117f1aSKweh, Hock Leong GFP_KERNEL | __GFP_ZERO); 637367633fSJan Kiszka if (!temp_page) 6465117f1aSKweh, Hock Leong return -ENOMEM; 6565117f1aSKweh, Hock Leong 6665117f1aSKweh, Hock Leong cap_info->pages = temp_page; 6765117f1aSKweh, Hock Leong 68f24c4d47SArd Biesheuvel temp_page = krealloc(cap_info->phys, 69f24c4d47SArd Biesheuvel pages_needed * sizeof(phys_addr_t *), 70f24c4d47SArd Biesheuvel GFP_KERNEL | __GFP_ZERO); 71f24c4d47SArd Biesheuvel if (!temp_page) 72f24c4d47SArd Biesheuvel return -ENOMEM; 73f24c4d47SArd Biesheuvel 74f24c4d47SArd Biesheuvel cap_info->phys = temp_page; 75f24c4d47SArd Biesheuvel 7665117f1aSKweh, Hock Leong return 0; 7765117f1aSKweh, Hock Leong } 7865117f1aSKweh, Hock Leong 7965117f1aSKweh, Hock Leong /** 803fabd628SArd Biesheuvel * efi_capsule_setup_info - obtain the efi capsule header in the binary and 813fabd628SArd Biesheuvel * setup capsule_info structure 823fabd628SArd Biesheuvel * @cap_info: pointer to current instance of capsule_info structure 833fabd628SArd Biesheuvel * @kbuff: a mapped first page buffer pointer 843fabd628SArd Biesheuvel * @hdr_bytes: the total received number of bytes for efi header 853fabd628SArd Biesheuvel * 863fabd628SArd Biesheuvel * Platforms with non-standard capsule update mechanisms can override 873fabd628SArd Biesheuvel * this __weak function so they can perform any required capsule 883fabd628SArd Biesheuvel * image munging. See quark_quirk_function() for an example. 893fabd628SArd Biesheuvel **/ 903fabd628SArd Biesheuvel int __weak efi_capsule_setup_info(struct capsule_info *cap_info, void *kbuff, 913fabd628SArd Biesheuvel size_t hdr_bytes) 923fabd628SArd Biesheuvel { 933fabd628SArd Biesheuvel /* Only process data block that is larger than efi header size */ 943fabd628SArd Biesheuvel if (hdr_bytes < sizeof(efi_capsule_header_t)) 953fabd628SArd Biesheuvel return 0; 963fabd628SArd Biesheuvel 973fabd628SArd Biesheuvel memcpy(&cap_info->header, kbuff, sizeof(cap_info->header)); 983fabd628SArd Biesheuvel cap_info->total_size = cap_info->header.imagesize; 993fabd628SArd Biesheuvel 1003fabd628SArd Biesheuvel return __efi_capsule_setup_info(cap_info); 1013fabd628SArd Biesheuvel } 1023fabd628SArd Biesheuvel 1033fabd628SArd Biesheuvel /** 10465117f1aSKweh, Hock Leong * efi_capsule_submit_update - invoke the efi_capsule_update API once binary 10565117f1aSKweh, Hock Leong * upload done 10665117f1aSKweh, Hock Leong * @cap_info: pointer to current instance of capsule_info structure 10765117f1aSKweh, Hock Leong **/ 10865117f1aSKweh, Hock Leong static ssize_t efi_capsule_submit_update(struct capsule_info *cap_info) 10965117f1aSKweh, Hock Leong { 110f24c4d47SArd Biesheuvel bool do_vunmap = false; 11165117f1aSKweh, Hock Leong int ret; 11265117f1aSKweh, Hock Leong 113f24c4d47SArd Biesheuvel /* 114f24c4d47SArd Biesheuvel * cap_info->capsule may have been assigned already by a quirk 115f24c4d47SArd Biesheuvel * handler, so only overwrite it if it is NULL 116f24c4d47SArd Biesheuvel */ 117f24c4d47SArd Biesheuvel if (!cap_info->capsule) { 118f24c4d47SArd Biesheuvel cap_info->capsule = vmap(cap_info->pages, cap_info->index, 119f24c4d47SArd Biesheuvel VM_MAP, PAGE_KERNEL); 120f24c4d47SArd Biesheuvel if (!cap_info->capsule) 121f24c4d47SArd Biesheuvel return -ENOMEM; 122f24c4d47SArd Biesheuvel do_vunmap = true; 123f24c4d47SArd Biesheuvel } 124f24c4d47SArd Biesheuvel 125f24c4d47SArd Biesheuvel ret = efi_capsule_update(cap_info->capsule, cap_info->phys); 126f24c4d47SArd Biesheuvel if (do_vunmap) 127f24c4d47SArd Biesheuvel vunmap(cap_info->capsule); 12865117f1aSKweh, Hock Leong if (ret) { 1295dce14b9SJan Kiszka pr_err("capsule update failed\n"); 13065117f1aSKweh, Hock Leong return ret; 13165117f1aSKweh, Hock Leong } 13265117f1aSKweh, Hock Leong 13365117f1aSKweh, Hock Leong /* Indicate capsule binary uploading is done */ 13465117f1aSKweh, Hock Leong cap_info->index = NO_FURTHER_WRITE_ACTION; 13583f0a7c7SShunyong Yang 13683f0a7c7SShunyong Yang if (cap_info->header.flags & EFI_CAPSULE_PERSIST_ACROSS_RESET) { 13783f0a7c7SShunyong Yang pr_info("Successfully uploaded capsule file with reboot type '%s'\n", 1385dce14b9SJan Kiszka !cap_info->reset_type ? "RESET_COLD" : 13965117f1aSKweh, Hock Leong cap_info->reset_type == 1 ? "RESET_WARM" : 14065117f1aSKweh, Hock Leong "RESET_SHUTDOWN"); 14183f0a7c7SShunyong Yang } else { 14283f0a7c7SShunyong Yang pr_info("Successfully processed capsule file\n"); 14383f0a7c7SShunyong Yang } 14483f0a7c7SShunyong Yang 14565117f1aSKweh, Hock Leong return 0; 14665117f1aSKweh, Hock Leong } 14765117f1aSKweh, Hock Leong 14865117f1aSKweh, Hock Leong /** 14965117f1aSKweh, Hock Leong * efi_capsule_write - store the capsule binary and pass it to 15065117f1aSKweh, Hock Leong * efi_capsule_update() API 15165117f1aSKweh, Hock Leong * @file: file pointer 15265117f1aSKweh, Hock Leong * @buff: buffer pointer 15365117f1aSKweh, Hock Leong * @count: number of bytes in @buff 15465117f1aSKweh, Hock Leong * @offp: not used 15565117f1aSKweh, Hock Leong * 15665117f1aSKweh, Hock Leong * Expectation: 15765117f1aSKweh, Hock Leong * - A user space tool should start at the beginning of capsule binary and 15865117f1aSKweh, Hock Leong * pass data in sequentially. 15965117f1aSKweh, Hock Leong * - Users should close and re-open this file note in order to upload more 16065117f1aSKweh, Hock Leong * capsules. 16165117f1aSKweh, Hock Leong * - After an error returned, user should close the file and restart the 16265117f1aSKweh, Hock Leong * operation for the next try otherwise -EIO will be returned until the 16365117f1aSKweh, Hock Leong * file is closed. 16465117f1aSKweh, Hock Leong * - An EFI capsule header must be located at the beginning of capsule 16565117f1aSKweh, Hock Leong * binary file and passed in as first block data of write operation. 16665117f1aSKweh, Hock Leong **/ 16765117f1aSKweh, Hock Leong static ssize_t efi_capsule_write(struct file *file, const char __user *buff, 16865117f1aSKweh, Hock Leong size_t count, loff_t *offp) 16965117f1aSKweh, Hock Leong { 17065117f1aSKweh, Hock Leong int ret = 0; 17165117f1aSKweh, Hock Leong struct capsule_info *cap_info = file->private_data; 17265117f1aSKweh, Hock Leong struct page *page; 17365117f1aSKweh, Hock Leong void *kbuff = NULL; 17465117f1aSKweh, Hock Leong size_t write_byte; 17565117f1aSKweh, Hock Leong 17665117f1aSKweh, Hock Leong if (count == 0) 17765117f1aSKweh, Hock Leong return 0; 17865117f1aSKweh, Hock Leong 17965117f1aSKweh, Hock Leong /* Return error while NO_FURTHER_WRITE_ACTION is flagged */ 18065117f1aSKweh, Hock Leong if (cap_info->index < 0) 18165117f1aSKweh, Hock Leong return -EIO; 18265117f1aSKweh, Hock Leong 18365117f1aSKweh, Hock Leong /* Only alloc a new page when previous page is full */ 18465117f1aSKweh, Hock Leong if (!cap_info->page_bytes_remain) { 18565117f1aSKweh, Hock Leong page = alloc_page(GFP_KERNEL); 18665117f1aSKweh, Hock Leong if (!page) { 18765117f1aSKweh, Hock Leong ret = -ENOMEM; 18865117f1aSKweh, Hock Leong goto failed; 18965117f1aSKweh, Hock Leong } 19065117f1aSKweh, Hock Leong 191f24c4d47SArd Biesheuvel cap_info->pages[cap_info->index] = page; 192f24c4d47SArd Biesheuvel cap_info->phys[cap_info->index] = page_to_phys(page); 19365117f1aSKweh, Hock Leong cap_info->page_bytes_remain = PAGE_SIZE; 194f24c4d47SArd Biesheuvel cap_info->index++; 1952a457fb3SArd Biesheuvel } else { 196f24c4d47SArd Biesheuvel page = cap_info->pages[cap_info->index - 1]; 19765117f1aSKweh, Hock Leong } 19865117f1aSKweh, Hock Leong 19965117f1aSKweh, Hock Leong kbuff = kmap(page); 20065117f1aSKweh, Hock Leong kbuff += PAGE_SIZE - cap_info->page_bytes_remain; 20165117f1aSKweh, Hock Leong 20265117f1aSKweh, Hock Leong /* Copy capsule binary data from user space to kernel space buffer */ 20365117f1aSKweh, Hock Leong write_byte = min_t(size_t, count, cap_info->page_bytes_remain); 20465117f1aSKweh, Hock Leong if (copy_from_user(kbuff, buff, write_byte)) { 20565117f1aSKweh, Hock Leong ret = -EFAULT; 20665117f1aSKweh, Hock Leong goto fail_unmap; 20765117f1aSKweh, Hock Leong } 20865117f1aSKweh, Hock Leong cap_info->page_bytes_remain -= write_byte; 20965117f1aSKweh, Hock Leong 21065117f1aSKweh, Hock Leong /* Setup capsule binary info structure */ 21182c3768bSArd Biesheuvel if (cap_info->header.headersize == 0) { 2123fabd628SArd Biesheuvel ret = efi_capsule_setup_info(cap_info, kbuff - cap_info->count, 21365117f1aSKweh, Hock Leong cap_info->count + write_byte); 21465117f1aSKweh, Hock Leong if (ret) 21565117f1aSKweh, Hock Leong goto fail_unmap; 21665117f1aSKweh, Hock Leong } 21765117f1aSKweh, Hock Leong 21865117f1aSKweh, Hock Leong cap_info->count += write_byte; 21965117f1aSKweh, Hock Leong kunmap(page); 22065117f1aSKweh, Hock Leong 22165117f1aSKweh, Hock Leong /* Submit the full binary to efi_capsule_update() API */ 22282c3768bSArd Biesheuvel if (cap_info->header.headersize > 0 && 22365117f1aSKweh, Hock Leong cap_info->count >= cap_info->total_size) { 22465117f1aSKweh, Hock Leong if (cap_info->count > cap_info->total_size) { 2255dce14b9SJan Kiszka pr_err("capsule upload size exceeded header defined size\n"); 22665117f1aSKweh, Hock Leong ret = -EINVAL; 22765117f1aSKweh, Hock Leong goto failed; 22865117f1aSKweh, Hock Leong } 22965117f1aSKweh, Hock Leong 23065117f1aSKweh, Hock Leong ret = efi_capsule_submit_update(cap_info); 23165117f1aSKweh, Hock Leong if (ret) 23265117f1aSKweh, Hock Leong goto failed; 23365117f1aSKweh, Hock Leong } 23465117f1aSKweh, Hock Leong 23565117f1aSKweh, Hock Leong return write_byte; 23665117f1aSKweh, Hock Leong 23765117f1aSKweh, Hock Leong fail_unmap: 23865117f1aSKweh, Hock Leong kunmap(page); 23965117f1aSKweh, Hock Leong failed: 24065117f1aSKweh, Hock Leong efi_free_all_buff_pages(cap_info); 24165117f1aSKweh, Hock Leong return ret; 24265117f1aSKweh, Hock Leong } 24365117f1aSKweh, Hock Leong 24465117f1aSKweh, Hock Leong /** 24565117f1aSKweh, Hock Leong * efi_capsule_flush - called by file close or file flush 24665117f1aSKweh, Hock Leong * @file: file pointer 24765117f1aSKweh, Hock Leong * @id: not used 24865117f1aSKweh, Hock Leong * 24965117f1aSKweh, Hock Leong * If a capsule is being partially uploaded then calling this function 25065117f1aSKweh, Hock Leong * will be treated as upload termination and will free those completed 25165117f1aSKweh, Hock Leong * buffer pages and -ECANCELED will be returned. 25265117f1aSKweh, Hock Leong **/ 25365117f1aSKweh, Hock Leong static int efi_capsule_flush(struct file *file, fl_owner_t id) 25465117f1aSKweh, Hock Leong { 25565117f1aSKweh, Hock Leong int ret = 0; 25665117f1aSKweh, Hock Leong struct capsule_info *cap_info = file->private_data; 25765117f1aSKweh, Hock Leong 25865117f1aSKweh, Hock Leong if (cap_info->index > 0) { 2595dce14b9SJan Kiszka pr_err("capsule upload not complete\n"); 26065117f1aSKweh, Hock Leong efi_free_all_buff_pages(cap_info); 26165117f1aSKweh, Hock Leong ret = -ECANCELED; 26265117f1aSKweh, Hock Leong } 26365117f1aSKweh, Hock Leong 26465117f1aSKweh, Hock Leong return ret; 26565117f1aSKweh, Hock Leong } 26665117f1aSKweh, Hock Leong 26765117f1aSKweh, Hock Leong /** 26865117f1aSKweh, Hock Leong * efi_capsule_release - called by file close 26965117f1aSKweh, Hock Leong * @inode: not used 27065117f1aSKweh, Hock Leong * @file: file pointer 27165117f1aSKweh, Hock Leong * 27265117f1aSKweh, Hock Leong * We will not free successfully submitted pages since efi update 27365117f1aSKweh, Hock Leong * requires data to be maintained across system reboot. 27465117f1aSKweh, Hock Leong **/ 27565117f1aSKweh, Hock Leong static int efi_capsule_release(struct inode *inode, struct file *file) 27665117f1aSKweh, Hock Leong { 27765117f1aSKweh, Hock Leong struct capsule_info *cap_info = file->private_data; 27865117f1aSKweh, Hock Leong 27965117f1aSKweh, Hock Leong kfree(cap_info->pages); 280f24c4d47SArd Biesheuvel kfree(cap_info->phys); 28165117f1aSKweh, Hock Leong kfree(file->private_data); 28265117f1aSKweh, Hock Leong file->private_data = NULL; 28365117f1aSKweh, Hock Leong return 0; 28465117f1aSKweh, Hock Leong } 28565117f1aSKweh, Hock Leong 28665117f1aSKweh, Hock Leong /** 28765117f1aSKweh, Hock Leong * efi_capsule_open - called by file open 28865117f1aSKweh, Hock Leong * @inode: not used 28965117f1aSKweh, Hock Leong * @file: file pointer 29065117f1aSKweh, Hock Leong * 29165117f1aSKweh, Hock Leong * Will allocate each capsule_info memory for each file open call. 29265117f1aSKweh, Hock Leong * This provided the capability to support multiple file open feature 29365117f1aSKweh, Hock Leong * where user is not needed to wait for others to finish in order to 29465117f1aSKweh, Hock Leong * upload their capsule binary. 29565117f1aSKweh, Hock Leong **/ 29665117f1aSKweh, Hock Leong static int efi_capsule_open(struct inode *inode, struct file *file) 29765117f1aSKweh, Hock Leong { 29865117f1aSKweh, Hock Leong struct capsule_info *cap_info; 29965117f1aSKweh, Hock Leong 30065117f1aSKweh, Hock Leong cap_info = kzalloc(sizeof(*cap_info), GFP_KERNEL); 30165117f1aSKweh, Hock Leong if (!cap_info) 30265117f1aSKweh, Hock Leong return -ENOMEM; 30365117f1aSKweh, Hock Leong 30465117f1aSKweh, Hock Leong cap_info->pages = kzalloc(sizeof(void *), GFP_KERNEL); 30565117f1aSKweh, Hock Leong if (!cap_info->pages) { 30665117f1aSKweh, Hock Leong kfree(cap_info); 30765117f1aSKweh, Hock Leong return -ENOMEM; 30865117f1aSKweh, Hock Leong } 30965117f1aSKweh, Hock Leong 310f24c4d47SArd Biesheuvel cap_info->phys = kzalloc(sizeof(void *), GFP_KERNEL); 311f24c4d47SArd Biesheuvel if (!cap_info->phys) { 312f24c4d47SArd Biesheuvel kfree(cap_info->pages); 313f24c4d47SArd Biesheuvel kfree(cap_info); 314f24c4d47SArd Biesheuvel return -ENOMEM; 315f24c4d47SArd Biesheuvel } 316f24c4d47SArd Biesheuvel 31765117f1aSKweh, Hock Leong file->private_data = cap_info; 31865117f1aSKweh, Hock Leong 31965117f1aSKweh, Hock Leong return 0; 32065117f1aSKweh, Hock Leong } 32165117f1aSKweh, Hock Leong 32265117f1aSKweh, Hock Leong static const struct file_operations efi_capsule_fops = { 32365117f1aSKweh, Hock Leong .owner = THIS_MODULE, 32465117f1aSKweh, Hock Leong .open = efi_capsule_open, 32565117f1aSKweh, Hock Leong .write = efi_capsule_write, 32665117f1aSKweh, Hock Leong .flush = efi_capsule_flush, 32765117f1aSKweh, Hock Leong .release = efi_capsule_release, 32865117f1aSKweh, Hock Leong .llseek = no_llseek, 32965117f1aSKweh, Hock Leong }; 33065117f1aSKweh, Hock Leong 33165117f1aSKweh, Hock Leong static struct miscdevice efi_capsule_misc = { 33265117f1aSKweh, Hock Leong .minor = MISC_DYNAMIC_MINOR, 33365117f1aSKweh, Hock Leong .name = "efi_capsule_loader", 33465117f1aSKweh, Hock Leong .fops = &efi_capsule_fops, 33565117f1aSKweh, Hock Leong }; 33665117f1aSKweh, Hock Leong 33765117f1aSKweh, Hock Leong static int __init efi_capsule_loader_init(void) 33865117f1aSKweh, Hock Leong { 33965117f1aSKweh, Hock Leong int ret; 34065117f1aSKweh, Hock Leong 34165117f1aSKweh, Hock Leong if (!efi_enabled(EFI_RUNTIME_SERVICES)) 34265117f1aSKweh, Hock Leong return -ENODEV; 34365117f1aSKweh, Hock Leong 34465117f1aSKweh, Hock Leong ret = misc_register(&efi_capsule_misc); 34565117f1aSKweh, Hock Leong if (ret) 3465dce14b9SJan Kiszka pr_err("Unable to register capsule loader device\n"); 34765117f1aSKweh, Hock Leong 34865117f1aSKweh, Hock Leong return ret; 34965117f1aSKweh, Hock Leong } 35065117f1aSKweh, Hock Leong module_init(efi_capsule_loader_init); 35165117f1aSKweh, Hock Leong 35265117f1aSKweh, Hock Leong static void __exit efi_capsule_loader_exit(void) 35365117f1aSKweh, Hock Leong { 35465117f1aSKweh, Hock Leong misc_deregister(&efi_capsule_misc); 35565117f1aSKweh, Hock Leong } 35665117f1aSKweh, Hock Leong module_exit(efi_capsule_loader_exit); 35765117f1aSKweh, Hock Leong 35865117f1aSKweh, Hock Leong MODULE_DESCRIPTION("EFI capsule firmware binary loader"); 35965117f1aSKweh, Hock Leong MODULE_LICENSE("GPL v2"); 360