Path: blob/master/drivers/firmware/efi/libstub/efi-stub.c
26483 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* EFI stub implementation that is shared by arm and arm64 architectures.3* This should be #included by the EFI stub implementation files.4*5* Copyright (C) 2013,2014 Linaro Limited6* Roy Franz <[email protected]7* Copyright (C) 2013 Red Hat, Inc.8* Mark Salter <[email protected]>9*/1011#include <linux/efi.h>12#include <linux/screen_info.h>13#include <asm/efi.h>1415#include "efistub.h"1617/*18* This is the base address at which to start allocating virtual memory ranges19* for UEFI Runtime Services.20*21* For ARM/ARM64:22* This is in the low TTBR0 range so that we can use23* any allocation we choose, and eliminate the risk of a conflict after kexec.24* The value chosen is the largest non-zero power of 2 suitable for this purpose25* both on 32-bit and 64-bit ARM CPUs, to maximize the likelihood that it can26* be mapped efficiently.27* Since 32-bit ARM could potentially execute with a 1G/3G user/kernel split,28* map everything below 1 GB. (512 MB is a reasonable upper bound for the29* entire footprint of the UEFI runtime services memory regions)30*31* For RISC-V:32* There is no specific reason for which, this address (512MB) can't be used33* EFI runtime virtual address for RISC-V. It also helps to use EFI runtime34* services on both RV32/RV64. Keep the same runtime virtual address for RISC-V35* as well to minimize the code churn.36*/37#define EFI_RT_VIRTUAL_BASE SZ_512M3839/*40* Some architectures map the EFI regions into the kernel's linear map using a41* fixed offset.42*/43#ifndef EFI_RT_VIRTUAL_OFFSET44#define EFI_RT_VIRTUAL_OFFSET 045#endif4647static u64 virtmap_base = EFI_RT_VIRTUAL_BASE;48static bool flat_va_mapping = (EFI_RT_VIRTUAL_OFFSET != 0);4950void __weak free_screen_info(struct screen_info *si)51{52}5354static struct screen_info *setup_graphics(void)55{56struct screen_info *si, tmp = {};5758if (efi_setup_gop(&tmp) != EFI_SUCCESS)59return NULL;6061si = alloc_screen_info();62if (!si)63return NULL;6465*si = tmp;66return si;67}6869static void install_memreserve_table(void)70{71struct linux_efi_memreserve *rsv;72efi_guid_t memreserve_table_guid = LINUX_EFI_MEMRESERVE_TABLE_GUID;73efi_status_t status;7475status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, sizeof(*rsv),76(void **)&rsv);77if (status != EFI_SUCCESS) {78efi_err("Failed to allocate memreserve entry!\n");79return;80}8182rsv->next = 0;83rsv->size = 0;84atomic_set(&rsv->count, 0);8586status = efi_bs_call(install_configuration_table,87&memreserve_table_guid, rsv);88if (status != EFI_SUCCESS)89efi_err("Failed to install memreserve config table!\n");90}9192static u32 get_supported_rt_services(void)93{94const efi_rt_properties_table_t *rt_prop_table;95u32 supported = EFI_RT_SUPPORTED_ALL;9697rt_prop_table = get_efi_config_table(EFI_RT_PROPERTIES_TABLE_GUID);98if (rt_prop_table)99supported &= rt_prop_table->runtime_services_supported;100101return supported;102}103104efi_status_t efi_handle_cmdline(efi_loaded_image_t *image, char **cmdline_ptr)105{106char *cmdline __free(efi_pool) = NULL;107efi_status_t status;108109/*110* Get the command line from EFI, using the LOADED_IMAGE111* protocol. We are going to copy the command line into the112* device tree, so this can be allocated anywhere.113*/114cmdline = efi_convert_cmdline(image);115if (!cmdline) {116efi_err("getting command line via LOADED_IMAGE_PROTOCOL\n");117return EFI_OUT_OF_RESOURCES;118}119120if (!IS_ENABLED(CONFIG_CMDLINE_FORCE)) {121status = efi_parse_options(cmdline);122if (status != EFI_SUCCESS) {123efi_err("Failed to parse EFI load options\n");124return status;125}126}127128if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) ||129IS_ENABLED(CONFIG_CMDLINE_FORCE) ||130cmdline[0] == 0) {131status = efi_parse_options(CONFIG_CMDLINE);132if (status != EFI_SUCCESS) {133efi_err("Failed to parse built-in command line\n");134return status;135}136}137138*cmdline_ptr = no_free_ptr(cmdline);139return EFI_SUCCESS;140}141142efi_status_t efi_stub_common(efi_handle_t handle,143efi_loaded_image_t *image,144unsigned long image_addr,145char *cmdline_ptr)146{147struct screen_info *si;148efi_status_t status;149150status = check_platform_features();151if (status != EFI_SUCCESS)152return status;153154si = setup_graphics();155156efi_retrieve_eventlog();157158/* Ask the firmware to clear memory on unclean shutdown */159efi_enable_reset_attack_mitigation();160161efi_load_initrd(image, ULONG_MAX, efi_get_max_initrd_addr(image_addr),162NULL);163164efi_random_get_seed();165166/* force efi_novamap if SetVirtualAddressMap() is unsupported */167efi_novamap |= !(get_supported_rt_services() &168EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP);169170install_memreserve_table();171172status = efi_boot_kernel(handle, image, image_addr, cmdline_ptr);173174free_screen_info(si);175return status;176}177178/*179* efi_allocate_virtmap() - create a pool allocation for the virtmap180*181* Create an allocation that is of sufficient size to hold all the memory182* descriptors that will be passed to SetVirtualAddressMap() to inform the183* firmware about the virtual mapping that will be used under the OS to call184* into the firmware.185*/186efi_status_t efi_alloc_virtmap(efi_memory_desc_t **virtmap,187unsigned long *desc_size, u32 *desc_ver)188{189unsigned long size, mmap_key;190efi_status_t status;191192/*193* Use the size of the current memory map as an upper bound for the194* size of the buffer we need to pass to SetVirtualAddressMap() to195* cover all EFI_MEMORY_RUNTIME regions.196*/197size = 0;198status = efi_bs_call(get_memory_map, &size, NULL, &mmap_key, desc_size,199desc_ver);200if (status != EFI_BUFFER_TOO_SMALL)201return EFI_LOAD_ERROR;202203return efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,204(void **)virtmap);205}206207/*208* efi_get_virtmap() - create a virtual mapping for the EFI memory map209*210* This function populates the virt_addr fields of all memory region descriptors211* in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors212* are also copied to @runtime_map, and their total count is returned in @count.213*/214void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,215unsigned long desc_size, efi_memory_desc_t *runtime_map,216int *count)217{218u64 efi_virt_base = virtmap_base;219efi_memory_desc_t *in, *out = runtime_map;220int l;221222*count = 0;223224for (l = 0; l < map_size; l += desc_size) {225u64 paddr, size;226227in = (void *)memory_map + l;228if (!(in->attribute & EFI_MEMORY_RUNTIME))229continue;230231paddr = in->phys_addr;232size = in->num_pages * EFI_PAGE_SIZE;233234in->virt_addr = in->phys_addr + EFI_RT_VIRTUAL_OFFSET;235if (efi_novamap) {236continue;237}238239/*240* Make the mapping compatible with 64k pages: this allows241* a 4k page size kernel to kexec a 64k page size kernel and242* vice versa.243*/244if (!flat_va_mapping) {245246paddr = round_down(in->phys_addr, SZ_64K);247size += in->phys_addr - paddr;248249/*250* Avoid wasting memory on PTEs by choosing a virtual251* base that is compatible with section mappings if this252* region has the appropriate size and physical253* alignment. (Sections are 2 MB on 4k granule kernels)254*/255if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M)256efi_virt_base = round_up(efi_virt_base, SZ_2M);257else258efi_virt_base = round_up(efi_virt_base, SZ_64K);259260in->virt_addr += efi_virt_base - paddr;261efi_virt_base += size;262}263264memcpy(out, in, desc_size);265out = (void *)out + desc_size;266++*count;267}268}269270271