// SPDX-License-Identifier: GPL-2.01/*2* EFI capsule support.3*4* Copyright 2013 Intel Corporation; author Matt Fleming5*/67#define pr_fmt(fmt) "efi: " fmt89#include <linux/slab.h>10#include <linux/mutex.h>11#include <linux/highmem.h>12#include <linux/efi.h>13#include <linux/vmalloc.h>14#include <asm/efi.h>15#include <asm/io.h>1617typedef struct {18u64 length;19u64 data;20} efi_capsule_block_desc_t;2122static bool capsule_pending;23static bool stop_capsules;24static int efi_reset_type = -1;2526/*27* capsule_mutex serialises access to both capsule_pending and28* efi_reset_type and stop_capsules.29*/30static DEFINE_MUTEX(capsule_mutex);3132/**33* efi_capsule_pending - has a capsule been passed to the firmware?34* @reset_type: store the type of EFI reset if capsule is pending35*36* To ensure that the registered capsule is processed correctly by the37* firmware we need to perform a specific type of reset. If a capsule is38* pending return the reset type in @reset_type.39*40* This function will race with callers of efi_capsule_update(), for41* example, calling this function while somebody else is in42* efi_capsule_update() but hasn't reached efi_capsue_update_locked()43* will miss the updates to capsule_pending and efi_reset_type after44* efi_capsule_update_locked() completes.45*46* A non-racy use is from platform reboot code because we use47* system_state to ensure no capsules can be sent to the firmware once48* we're at SYSTEM_RESTART. See efi_capsule_update_locked().49*/50bool efi_capsule_pending(int *reset_type)51{52if (!capsule_pending)53return false;5455if (reset_type)56*reset_type = efi_reset_type;5758return true;59}6061/*62* Whitelist of EFI capsule flags that we support.63*64* We do not handle EFI_CAPSULE_INITIATE_RESET because that would65* require us to prepare the kernel for reboot. Refuse to load any66* capsules with that flag and any other flags that we do not know how67* to handle.68*/69#define EFI_CAPSULE_SUPPORTED_FLAG_MASK \70(EFI_CAPSULE_PERSIST_ACROSS_RESET | EFI_CAPSULE_POPULATE_SYSTEM_TABLE)7172/**73* efi_capsule_supported - does the firmware support the capsule?74* @guid: vendor guid of capsule75* @flags: capsule flags76* @size: size of capsule data77* @reset: the reset type required for this capsule78*79* Check whether a capsule with @flags is supported by the firmware80* and that @size doesn't exceed the maximum size for a capsule.81*82* No attempt is made to check @reset against the reset type required83* by any pending capsules because of the races involved.84*/85int efi_capsule_supported(efi_guid_t guid, u32 flags, size_t size, int *reset)86{87efi_capsule_header_t capsule;88efi_capsule_header_t *cap_list[] = { &capsule };89efi_status_t status;90u64 max_size;9192if (flags & ~EFI_CAPSULE_SUPPORTED_FLAG_MASK)93return -EINVAL;9495capsule.headersize = capsule.imagesize = sizeof(capsule);96memcpy(&capsule.guid, &guid, sizeof(efi_guid_t));97capsule.flags = flags;9899status = efi.query_capsule_caps(cap_list, 1, &max_size, reset);100if (status != EFI_SUCCESS)101return efi_status_to_err(status);102103if (size > max_size)104return -ENOSPC;105106return 0;107}108EXPORT_SYMBOL_GPL(efi_capsule_supported);109110/*111* Every scatter gather list (block descriptor) page must end with a112* continuation pointer. The last continuation pointer of the last113* page must be zero to mark the end of the chain.114*/115#define SGLIST_PER_PAGE ((PAGE_SIZE / sizeof(efi_capsule_block_desc_t)) - 1)116117/*118* How many scatter gather list (block descriptor) pages do we need119* to map @count pages?120*/121static inline unsigned int sg_pages_num(unsigned int count)122{123return DIV_ROUND_UP(count, SGLIST_PER_PAGE);124}125126/**127* efi_capsule_update_locked - pass a single capsule to the firmware128* @capsule: capsule to send to the firmware129* @sg_pages: array of scatter gather (block descriptor) pages130* @reset: the reset type required for @capsule131*132* Since this function must be called under capsule_mutex check133* whether efi_reset_type will conflict with @reset, and atomically134* set it and capsule_pending if a capsule was successfully sent to135* the firmware.136*137* We also check to see if the system is about to restart, and if so,138* abort. This avoids races between efi_capsule_update() and139* efi_capsule_pending().140*/141static int142efi_capsule_update_locked(efi_capsule_header_t *capsule,143struct page **sg_pages, int reset)144{145efi_physical_addr_t sglist_phys;146efi_status_t status;147148lockdep_assert_held(&capsule_mutex);149150/*151* If someone has already registered a capsule that requires a152* different reset type, we're out of luck and must abort.153*/154if (efi_reset_type >= 0 && efi_reset_type != reset) {155pr_err("Conflicting capsule reset type %d (%d).\n",156reset, efi_reset_type);157return -EINVAL;158}159160/*161* If the system is getting ready to restart it may have162* called efi_capsule_pending() to make decisions (such as163* whether to force an EFI reboot), and we're racing against164* that call. Abort in that case.165*/166if (unlikely(stop_capsules)) {167pr_warn("Capsule update raced with reboot, aborting.\n");168return -EINVAL;169}170171sglist_phys = page_to_phys(sg_pages[0]);172173status = efi.update_capsule(&capsule, 1, sglist_phys);174if (status == EFI_SUCCESS) {175capsule_pending = true;176efi_reset_type = reset;177}178179return efi_status_to_err(status);180}181182/**183* efi_capsule_update - send a capsule to the firmware184* @capsule: capsule to send to firmware185* @pages: an array of capsule data pages186*187* Build a scatter gather list with EFI capsule block descriptors to188* map the capsule described by @capsule with its data in @pages and189* send it to the firmware via the UpdateCapsule() runtime service.190*191* @capsule must be a virtual mapping of the complete capsule update in the192* kernel address space, as the capsule can be consumed immediately.193* A capsule_header_t that describes the entire contents of the capsule194* must be at the start of the first data page.195*196* Even though this function will validate that the firmware supports197* the capsule guid, users will likely want to check that198* efi_capsule_supported() returns true before calling this function199* because it makes it easier to print helpful error messages.200*201* If the capsule is successfully submitted to the firmware, any202* subsequent calls to efi_capsule_pending() will return true. @pages203* must not be released or modified if this function returns204* successfully.205*206* Callers must be prepared for this function to fail, which can207* happen if we raced with system reboot or if there is already a208* pending capsule that has a reset type that conflicts with the one209* required by @capsule. Do NOT use efi_capsule_pending() to detect210* this conflict since that would be racy. Instead, submit the capsule211* to efi_capsule_update() and check the return value.212*213* Return 0 on success, a converted EFI status code on failure.214*/215int efi_capsule_update(efi_capsule_header_t *capsule, phys_addr_t *pages)216{217u32 imagesize = capsule->imagesize;218efi_guid_t guid = capsule->guid;219unsigned int count, sg_count;220u32 flags = capsule->flags;221struct page **sg_pages;222int rv, reset_type;223int i, j;224225rv = efi_capsule_supported(guid, flags, imagesize, &reset_type);226if (rv)227return rv;228229count = DIV_ROUND_UP(imagesize, PAGE_SIZE);230sg_count = sg_pages_num(count);231232sg_pages = kcalloc(sg_count, sizeof(*sg_pages), GFP_KERNEL);233if (!sg_pages)234return -ENOMEM;235236for (i = 0; i < sg_count; i++) {237sg_pages[i] = alloc_page(GFP_KERNEL);238if (!sg_pages[i]) {239rv = -ENOMEM;240goto out;241}242}243244for (i = 0; i < sg_count; i++) {245efi_capsule_block_desc_t *sglist;246247sglist = kmap_atomic(sg_pages[i]);248249for (j = 0; j < SGLIST_PER_PAGE && count > 0; j++) {250u64 sz = min_t(u64, imagesize,251PAGE_SIZE - (u64)*pages % PAGE_SIZE);252253sglist[j].length = sz;254sglist[j].data = *pages++;255256imagesize -= sz;257count--;258}259260/* Continuation pointer */261sglist[j].length = 0;262263if (i + 1 == sg_count)264sglist[j].data = 0;265else266sglist[j].data = page_to_phys(sg_pages[i + 1]);267268#if defined(CONFIG_ARM) || defined(CONFIG_ARM64)269/*270* At runtime, the firmware has no way to find out where the271* sglist elements are mapped, if they are mapped in the first272* place. Therefore, on architectures that can only perform273* cache maintenance by virtual address, the firmware is unable274* to perform this maintenance, and so it is up to the OS to do275* it instead.276*/277efi_capsule_flush_cache_range(sglist, PAGE_SIZE);278#endif279kunmap_atomic(sglist);280}281282mutex_lock(&capsule_mutex);283rv = efi_capsule_update_locked(capsule, sg_pages, reset_type);284mutex_unlock(&capsule_mutex);285286out:287for (i = 0; rv && i < sg_count; i++) {288if (sg_pages[i])289__free_page(sg_pages[i]);290}291292kfree(sg_pages);293return rv;294}295EXPORT_SYMBOL_GPL(efi_capsule_update);296297static int capsule_reboot_notify(struct notifier_block *nb, unsigned long event, void *cmd)298{299mutex_lock(&capsule_mutex);300stop_capsules = true;301mutex_unlock(&capsule_mutex);302303return NOTIFY_DONE;304}305306static struct notifier_block capsule_reboot_nb = {307.notifier_call = capsule_reboot_notify,308};309310static int __init capsule_reboot_register(void)311{312return register_reboot_notifier(&capsule_reboot_nb);313}314core_initcall(capsule_reboot_register);315316317