// SPDX-License-Identifier: GPL-2.0-only1#define pr_fmt(fmt) "efi: " fmt23#include <linux/init.h>4#include <linux/kernel.h>5#include <linux/string.h>6#include <linux/time.h>7#include <linux/types.h>8#include <linux/efi.h>9#include <linux/slab.h>10#include <linux/memblock.h>11#include <linux/acpi.h>12#include <linux/dmi.h>1314#include <asm/e820/api.h>15#include <asm/efi.h>16#include <asm/uv/uv.h>17#include <asm/cpu_device_id.h>18#include <asm/realmode.h>19#include <asm/reboot.h>2021#define EFI_MIN_RESERVE 51202223#define EFI_DUMMY_GUID \24EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)2526#define QUARK_CSH_SIGNATURE 0x5f435348 /* _CSH */27#define QUARK_SECURITY_HEADER_SIZE 0x4002829/*30* Header prepended to the standard EFI capsule on Quark systems the are based31* on Intel firmware BSP.32* @csh_signature: Unique identifier to sanity check signed module33* presence ("_CSH").34* @version: Current version of CSH used. Should be one for Quark A0.35* @modulesize: Size of the entire module including the module header36* and payload.37* @security_version_number_index: Index of SVN to use for validation of signed38* module.39* @security_version_number: Used to prevent against roll back of modules.40* @rsvd_module_id: Currently unused for Clanton (Quark).41* @rsvd_module_vendor: Vendor Identifier. For Intel products value is42* 0x00008086.43* @rsvd_date: BCD representation of build date as yyyymmdd, where44* yyyy=4 digit year, mm=1-12, dd=1-31.45* @headersize: Total length of the header including including any46* padding optionally added by the signing tool.47* @hash_algo: What Hash is used in the module signing.48* @cryp_algo: What Crypto is used in the module signing.49* @keysize: Total length of the key data including including any50* padding optionally added by the signing tool.51* @signaturesize: Total length of the signature including including any52* padding optionally added by the signing tool.53* @rsvd_next_header: 32-bit pointer to the next Secure Boot Module in the54* chain, if there is a next header.55* @rsvd: Reserved, padding structure to required size.56*57* See also QuartSecurityHeader_t in58* Quark_EDKII_v1.2.1.1/QuarkPlatformPkg/Include/QuarkBootRom.h59* from https://downloadcenter.intel.com/download/23197/Intel-Quark-SoC-X1000-Board-Support-Package-BSP60*/61struct quark_security_header {62u32 csh_signature;63u32 version;64u32 modulesize;65u32 security_version_number_index;66u32 security_version_number;67u32 rsvd_module_id;68u32 rsvd_module_vendor;69u32 rsvd_date;70u32 headersize;71u32 hash_algo;72u32 cryp_algo;73u32 keysize;74u32 signaturesize;75u32 rsvd_next_header;76u32 rsvd[2];77};7879static const efi_char16_t efi_dummy_name[] = L"DUMMY";8081static bool efi_no_storage_paranoia;8283/*84* Some firmware implementations refuse to boot if there's insufficient85* space in the variable store. The implementation of garbage collection86* in some FW versions causes stale (deleted) variables to take up space87* longer than intended and space is only freed once the store becomes88* almost completely full.89*90* Enabling this option disables the space checks in91* efi_query_variable_store() and forces garbage collection.92*93* Only enable this option if deleting EFI variables does not free up94* space in your variable store, e.g. if despite deleting variables95* you're unable to create new ones.96*/97static int __init setup_storage_paranoia(char *arg)98{99efi_no_storage_paranoia = true;100return 0;101}102early_param("efi_no_storage_paranoia", setup_storage_paranoia);103104/*105* Deleting the dummy variable which kicks off garbage collection106*/107void efi_delete_dummy_variable(void)108{109efi.set_variable_nonblocking((efi_char16_t *)efi_dummy_name,110&EFI_DUMMY_GUID,111EFI_VARIABLE_NON_VOLATILE |112EFI_VARIABLE_BOOTSERVICE_ACCESS |113EFI_VARIABLE_RUNTIME_ACCESS, 0, NULL);114}115116u64 efivar_reserved_space(void)117{118if (efi_no_storage_paranoia)119return 0;120return EFI_MIN_RESERVE;121}122EXPORT_SYMBOL_GPL(efivar_reserved_space);123124/*125* In the nonblocking case we do not attempt to perform garbage126* collection if we do not have enough free space. Rather, we do the127* bare minimum check and give up immediately if the available space128* is below EFI_MIN_RESERVE.129*130* This function is intended to be small and simple because it is131* invoked from crash handler paths.132*/133static efi_status_t134query_variable_store_nonblocking(u32 attributes, unsigned long size)135{136efi_status_t status;137u64 storage_size, remaining_size, max_size;138139status = efi.query_variable_info_nonblocking(attributes, &storage_size,140&remaining_size,141&max_size);142if (status != EFI_SUCCESS)143return status;144145if (remaining_size - size < EFI_MIN_RESERVE)146return EFI_OUT_OF_RESOURCES;147148return EFI_SUCCESS;149}150151/*152* Some firmware implementations refuse to boot if there's insufficient space153* in the variable store. Ensure that we never use more than a safe limit.154*155* Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable156* store.157*/158efi_status_t efi_query_variable_store(u32 attributes, unsigned long size,159bool nonblocking)160{161efi_status_t status;162u64 storage_size, remaining_size, max_size;163164if (!(attributes & EFI_VARIABLE_NON_VOLATILE))165return 0;166167if (nonblocking)168return query_variable_store_nonblocking(attributes, size);169170status = efi.query_variable_info(attributes, &storage_size,171&remaining_size, &max_size);172if (status != EFI_SUCCESS)173return status;174175/*176* We account for that by refusing the write if permitting it would177* reduce the available space to under 5KB. This figure was provided by178* Samsung, so should be safe.179*/180if ((remaining_size - size < EFI_MIN_RESERVE) &&181!efi_no_storage_paranoia) {182183/*184* Triggering garbage collection may require that the firmware185* generate a real EFI_OUT_OF_RESOURCES error. We can force186* that by attempting to use more space than is available.187*/188unsigned long dummy_size = remaining_size + 1024;189void *dummy = kzalloc(dummy_size, GFP_KERNEL);190191if (!dummy)192return EFI_OUT_OF_RESOURCES;193194status = efi.set_variable((efi_char16_t *)efi_dummy_name,195&EFI_DUMMY_GUID,196EFI_VARIABLE_NON_VOLATILE |197EFI_VARIABLE_BOOTSERVICE_ACCESS |198EFI_VARIABLE_RUNTIME_ACCESS,199dummy_size, dummy);200201if (status == EFI_SUCCESS) {202/*203* This should have failed, so if it didn't make sure204* that we delete it...205*/206efi_delete_dummy_variable();207}208209kfree(dummy);210211/*212* The runtime code may now have triggered a garbage collection213* run, so check the variable info again214*/215status = efi.query_variable_info(attributes, &storage_size,216&remaining_size, &max_size);217218if (status != EFI_SUCCESS)219return status;220221/*222* There still isn't enough room, so return an error223*/224if (remaining_size - size < EFI_MIN_RESERVE)225return EFI_OUT_OF_RESOURCES;226}227228return EFI_SUCCESS;229}230EXPORT_SYMBOL_GPL(efi_query_variable_store);231232/*233* The UEFI specification makes it clear that the operating system is234* free to do whatever it wants with boot services code after235* ExitBootServices() has been called. Ignoring this recommendation a236* significant bunch of EFI implementations continue calling into boot237* services code (SetVirtualAddressMap). In order to work around such238* buggy implementations we reserve boot services region during EFI239* init and make sure it stays executable. Then, after240* SetVirtualAddressMap(), it is discarded.241*242* However, some boot services regions contain data that is required243* by drivers, so we need to track which memory ranges can never be244* freed. This is done by tagging those regions with the245* EFI_MEMORY_RUNTIME attribute.246*247* Any driver that wants to mark a region as reserved must use248* efi_mem_reserve() which will insert a new EFI memory descriptor249* into efi.memmap (splitting existing regions if necessary) and tag250* it with EFI_MEMORY_RUNTIME.251*/252void __init efi_arch_mem_reserve(phys_addr_t addr, u64 size)253{254struct efi_memory_map_data data = { 0 };255struct efi_mem_range mr;256efi_memory_desc_t md;257int num_entries;258void *new;259260if (efi_mem_desc_lookup(addr, &md) ||261md.type != EFI_BOOT_SERVICES_DATA) {262pr_err("Failed to lookup EFI memory descriptor for %pa\n", &addr);263return;264}265266if (addr + size > md.phys_addr + (md.num_pages << EFI_PAGE_SHIFT)) {267pr_err("Region spans EFI memory descriptors, %pa\n", &addr);268return;269}270271size += addr % EFI_PAGE_SIZE;272size = round_up(size, EFI_PAGE_SIZE);273addr = round_down(addr, EFI_PAGE_SIZE);274275mr.range.start = addr;276mr.range.end = addr + size - 1;277mr.attribute = md.attribute | EFI_MEMORY_RUNTIME;278279num_entries = efi_memmap_split_count(&md, &mr.range);280num_entries += efi.memmap.nr_map;281282if (efi_memmap_alloc(num_entries, &data) != 0) {283pr_err("Could not allocate boot services memmap\n");284return;285}286287new = early_memremap_prot(data.phys_map, data.size,288pgprot_val(pgprot_encrypted(FIXMAP_PAGE_NORMAL)));289if (!new) {290pr_err("Failed to map new boot services memmap\n");291return;292}293294efi_memmap_insert(&efi.memmap, new, &mr);295early_memunmap(new, data.size);296297efi_memmap_install(&data);298e820__range_update(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);299e820__update_table(e820_table);300}301302/*303* Helper function for efi_reserve_boot_services() to figure out if we304* can free regions in efi_free_boot_services().305*306* Use this function to ensure we do not free regions owned by somebody307* else. We must only reserve (and then free) regions:308*309* - Not within any part of the kernel310* - Not the BIOS reserved area (E820_TYPE_RESERVED, E820_TYPE_NVS, etc)311*/312static __init bool can_free_region(u64 start, u64 size)313{314if (start + size > __pa_symbol(_text) && start <= __pa_symbol(_end))315return false;316317if (!e820__mapped_all(start, start+size, E820_TYPE_RAM))318return false;319320return true;321}322323void __init efi_reserve_boot_services(void)324{325efi_memory_desc_t *md;326327if (!efi_enabled(EFI_MEMMAP))328return;329330for_each_efi_memory_desc(md) {331u64 start = md->phys_addr;332u64 size = md->num_pages << EFI_PAGE_SHIFT;333bool already_reserved;334335if (md->type != EFI_BOOT_SERVICES_CODE &&336md->type != EFI_BOOT_SERVICES_DATA)337continue;338339already_reserved = memblock_is_region_reserved(start, size);340341/*342* Because the following memblock_reserve() is paired343* with memblock_free_late() for this region in344* efi_free_boot_services(), we must be extremely345* careful not to reserve, and subsequently free,346* critical regions of memory (like the kernel image) or347* those regions that somebody else has already348* reserved.349*350* A good example of a critical region that must not be351* freed is page zero (first 4Kb of memory), which may352* contain boot services code/data but is marked353* E820_TYPE_RESERVED by trim_bios_range().354*/355if (!already_reserved) {356memblock_reserve(start, size);357358/*359* If we are the first to reserve the region, no360* one else cares about it. We own it and can361* free it later.362*/363if (can_free_region(start, size))364continue;365}366367/*368* We don't own the region. We must not free it.369*370* Setting this bit for a boot services region really371* doesn't make sense as far as the firmware is372* concerned, but it does provide us with a way to tag373* those regions that must not be paired with374* memblock_free_late().375*/376md->attribute |= EFI_MEMORY_RUNTIME;377}378}379380/*381* Apart from having VA mappings for EFI boot services code/data regions,382* (duplicate) 1:1 mappings were also created as a quirk for buggy firmware. So,383* unmap both 1:1 and VA mappings.384*/385static void __init efi_unmap_pages(efi_memory_desc_t *md)386{387pgd_t *pgd = efi_mm.pgd;388u64 pa = md->phys_addr;389u64 va = md->virt_addr;390391/*392* EFI mixed mode has all RAM mapped to access arguments while making393* EFI runtime calls, hence don't unmap EFI boot services code/data394* regions.395*/396if (efi_is_mixed())397return;398399if (kernel_unmap_pages_in_pgd(pgd, pa, md->num_pages))400pr_err("Failed to unmap 1:1 mapping for 0x%llx\n", pa);401402if (kernel_unmap_pages_in_pgd(pgd, va, md->num_pages))403pr_err("Failed to unmap VA mapping for 0x%llx\n", va);404}405406void __init efi_free_boot_services(void)407{408struct efi_memory_map_data data = { 0 };409efi_memory_desc_t *md;410int num_entries = 0;411void *new, *new_md;412413/* Keep all regions for /sys/kernel/debug/efi */414if (efi_enabled(EFI_DBG))415return;416417for_each_efi_memory_desc(md) {418unsigned long long start = md->phys_addr;419unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;420size_t rm_size;421422if (md->type != EFI_BOOT_SERVICES_CODE &&423md->type != EFI_BOOT_SERVICES_DATA) {424num_entries++;425continue;426}427428/* Do not free, someone else owns it: */429if (md->attribute & EFI_MEMORY_RUNTIME) {430num_entries++;431continue;432}433434/*435* Before calling set_virtual_address_map(), EFI boot services436* code/data regions were mapped as a quirk for buggy firmware.437* Unmap them from efi_pgd before freeing them up.438*/439efi_unmap_pages(md);440441/*442* Nasty quirk: if all sub-1MB memory is used for boot443* services, we can get here without having allocated the444* real mode trampoline. It's too late to hand boot services445* memory back to the memblock allocator, so instead446* try to manually allocate the trampoline if needed.447*448* I've seen this on a Dell XPS 13 9350 with firmware449* 1.4.4 with SGX enabled booting Linux via Fedora 24's450* grub2-efi on a hard disk. (And no, I don't know why451* this happened, but Linux should still try to boot rather452* panicking early.)453*/454rm_size = real_mode_size_needed();455if (rm_size && (start + rm_size) < (1<<20) && size >= rm_size) {456set_real_mode_mem(start);457start += rm_size;458size -= rm_size;459}460461/*462* Don't free memory under 1M for two reasons:463* - BIOS might clobber it464* - Crash kernel needs it to be reserved465*/466if (start + size < SZ_1M)467continue;468if (start < SZ_1M) {469size -= (SZ_1M - start);470start = SZ_1M;471}472473memblock_free_late(start, size);474}475476if (!num_entries)477return;478479if (efi_memmap_alloc(num_entries, &data) != 0) {480pr_err("Failed to allocate new EFI memmap\n");481return;482}483484new = memremap(data.phys_map, data.size, MEMREMAP_WB);485if (!new) {486pr_err("Failed to map new EFI memmap\n");487return;488}489490/*491* Build a new EFI memmap that excludes any boot services492* regions that are not tagged EFI_MEMORY_RUNTIME, since those493* regions have now been freed.494*/495new_md = new;496for_each_efi_memory_desc(md) {497if (!(md->attribute & EFI_MEMORY_RUNTIME) &&498(md->type == EFI_BOOT_SERVICES_CODE ||499md->type == EFI_BOOT_SERVICES_DATA))500continue;501502memcpy(new_md, md, efi.memmap.desc_size);503new_md += efi.memmap.desc_size;504}505506memunmap(new);507508if (efi_memmap_install(&data) != 0) {509pr_err("Could not install new EFI memmap\n");510return;511}512}513514/*515* A number of config table entries get remapped to virtual addresses516* after entering EFI virtual mode. However, the kexec kernel requires517* their physical addresses therefore we pass them via setup_data and518* correct those entries to their respective physical addresses here.519*520* Currently only handles smbios which is necessary for some firmware521* implementation.522*/523int __init efi_reuse_config(u64 tables, int nr_tables)524{525int i, sz, ret = 0;526void *p, *tablep;527struct efi_setup_data *data;528529if (nr_tables == 0)530return 0;531532if (!efi_setup)533return 0;534535if (!efi_enabled(EFI_64BIT))536return 0;537538data = early_memremap(efi_setup, sizeof(*data));539if (!data) {540ret = -ENOMEM;541goto out;542}543544if (!data->smbios)545goto out_memremap;546547sz = sizeof(efi_config_table_64_t);548549p = tablep = early_memremap(tables, nr_tables * sz);550if (!p) {551pr_err("Could not map Configuration table!\n");552ret = -ENOMEM;553goto out_memremap;554}555556for (i = 0; i < nr_tables; i++) {557efi_guid_t guid;558559guid = ((efi_config_table_64_t *)p)->guid;560561if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))562((efi_config_table_64_t *)p)->table = data->smbios;563564/* Do not bother to play with mem attr table across kexec */565if (!efi_guidcmp(guid, EFI_MEMORY_ATTRIBUTES_TABLE_GUID))566((efi_config_table_64_t *)p)->table = EFI_INVALID_TABLE_ADDR;567568p += sz;569}570early_memunmap(tablep, nr_tables * sz);571572out_memremap:573early_memunmap(data, sizeof(*data));574out:575return ret;576}577578void __init efi_apply_memmap_quirks(void)579{580/*581* Once setup is done earlier, unmap the EFI memory map on mismatched582* firmware/kernel architectures since there is no support for runtime583* services.584*/585if (!efi_runtime_supported()) {586pr_info("Setup done, disabling due to 32/64-bit mismatch\n");587efi_memmap_unmap();588}589}590591/*592* For most modern platforms the preferred method of powering off is via593* ACPI. However, there are some that are known to require the use of594* EFI runtime services and for which ACPI does not work at all.595*596* Using EFI is a last resort, to be used only if no other option597* exists.598*/599bool efi_reboot_required(void)600{601if (!acpi_gbl_reduced_hardware)602return false;603604efi_reboot_quirk_mode = EFI_RESET_WARM;605return true;606}607608bool efi_poweroff_required(void)609{610return acpi_gbl_reduced_hardware || acpi_no_s5;611}612613#ifdef CONFIG_EFI_CAPSULE_QUIRK_QUARK_CSH614615static int qrk_capsule_setup_info(struct capsule_info *cap_info, void **pkbuff,616size_t hdr_bytes)617{618struct quark_security_header *csh = *pkbuff;619620/* Only process data block that is larger than the security header */621if (hdr_bytes < sizeof(struct quark_security_header))622return 0;623624if (csh->csh_signature != QUARK_CSH_SIGNATURE ||625csh->headersize != QUARK_SECURITY_HEADER_SIZE)626return 1;627628/* Only process data block if EFI header is included */629if (hdr_bytes < QUARK_SECURITY_HEADER_SIZE +630sizeof(efi_capsule_header_t))631return 0;632633pr_debug("Quark security header detected\n");634635if (csh->rsvd_next_header != 0) {636pr_err("multiple Quark security headers not supported\n");637return -EINVAL;638}639640*pkbuff += csh->headersize;641cap_info->total_size = csh->headersize;642643/*644* Update the first page pointer to skip over the CSH header.645*/646cap_info->phys[0] += csh->headersize;647648/*649* cap_info->capsule should point at a virtual mapping of the entire650* capsule, starting at the capsule header. Our image has the Quark651* security header prepended, so we cannot rely on the default vmap()652* mapping created by the generic capsule code.653* Given that the Quark firmware does not appear to care about the654* virtual mapping, let's just point cap_info->capsule at our copy655* of the capsule header.656*/657cap_info->capsule = &cap_info->header;658659return 1;660}661662static const struct x86_cpu_id efi_capsule_quirk_ids[] = {663X86_MATCH_VFM(INTEL_QUARK_X1000, &qrk_capsule_setup_info),664{ }665};666667int efi_capsule_setup_info(struct capsule_info *cap_info, void *kbuff,668size_t hdr_bytes)669{670int (*quirk_handler)(struct capsule_info *, void **, size_t);671const struct x86_cpu_id *id;672int ret;673674if (hdr_bytes < sizeof(efi_capsule_header_t))675return 0;676677cap_info->total_size = 0;678679id = x86_match_cpu(efi_capsule_quirk_ids);680if (id) {681/*682* The quirk handler is supposed to return683* - a value > 0 if the setup should continue, after advancing684* kbuff as needed685* - 0 if not enough hdr_bytes are available yet686* - a negative error code otherwise687*/688quirk_handler = (typeof(quirk_handler))id->driver_data;689ret = quirk_handler(cap_info, &kbuff, hdr_bytes);690if (ret <= 0)691return ret;692}693694memcpy(&cap_info->header, kbuff, sizeof(cap_info->header));695696cap_info->total_size += cap_info->header.imagesize;697698return __efi_capsule_setup_info(cap_info);699}700701#endif702703/*704* If any access by any efi runtime service causes a page fault, then,705* 1. If it's efi_reset_system(), reboot through BIOS.706* 2. If any other efi runtime service, then707* a. Return error status to the efi caller process.708* b. Disable EFI Runtime Services forever and709* c. Freeze efi_rts_wq and schedule new process.710*711* @return: Returns, if the page fault is not handled. This function712* will never return if the page fault is handled successfully.713*/714void efi_crash_gracefully_on_page_fault(unsigned long phys_addr)715{716if (!IS_ENABLED(CONFIG_X86_64))717return;718719/*720* If we get an interrupt/NMI while processing an EFI runtime service721* then this is a regular OOPS, not an EFI failure.722*/723if (in_interrupt())724return;725726/*727* Make sure that an efi runtime service caused the page fault.728* READ_ONCE() because we might be OOPSing in a different thread,729* and we don't want to trip KTSAN while trying to OOPS.730*/731if (READ_ONCE(efi_rts_work.efi_rts_id) == EFI_NONE ||732current_work() != &efi_rts_work.work)733return;734735/*736* Address range 0x0000 - 0x0fff is always mapped in the efi_pgd, so737* page faulting on these addresses isn't expected.738*/739if (phys_addr <= 0x0fff)740return;741742/*743* Print stack trace as it might be useful to know which EFI Runtime744* Service is buggy.745*/746WARN(1, FW_BUG "Page fault caused by firmware at PA: 0x%lx\n",747phys_addr);748749/*750* Buggy efi_reset_system() is handled differently from other EFI751* Runtime Services as it doesn't use efi_rts_wq. Although,752* native_machine_emergency_restart() says that machine_real_restart()753* could fail, it's better not to complicate this fault handler754* because this case occurs *very* rarely and hence could be improved755* on a need by basis.756*/757if (efi_rts_work.efi_rts_id == EFI_RESET_SYSTEM) {758pr_info("efi_reset_system() buggy! Reboot through BIOS\n");759machine_real_restart(MRR_BIOS);760return;761}762763/*764* Before calling EFI Runtime Service, the kernel has switched the765* calling process to efi_mm. Hence, switch back to task_mm.766*/767arch_efi_call_virt_teardown();768769/* Signal error status to the efi caller process */770efi_rts_work.status = EFI_ABORTED;771complete(&efi_rts_work.efi_rts_comp);772773clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);774pr_info("Froze efi_rts_wq and disabled EFI Runtime Services\n");775776/*777* Call schedule() in an infinite loop, so that any spurious wake ups778* will never run efi_rts_wq again.779*/780for (;;) {781set_current_state(TASK_IDLE);782schedule();783}784}785786787