Path: blob/master/drivers/firmware/efi/libstub/efi-stub.c
51324 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/sysfb.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_primary_display(struct sysfb_display_info *dpy)51{ }5253static struct sysfb_display_info *setup_primary_display(void)54{55struct sysfb_display_info *dpy;56struct screen_info *screen = NULL;57struct edid_info *edid = NULL;58efi_status_t status;5960dpy = alloc_primary_display();61if (!dpy)62return NULL;63screen = &dpy->screen;64#if defined(CONFIG_FIRMWARE_EDID)65edid = &dpy->edid;66#endif6768status = efi_setup_graphics(screen, edid);69if (status != EFI_SUCCESS)70goto err_free_primary_display;7172return dpy;7374err_free_primary_display:75free_primary_display(dpy);76return NULL;77}7879static void install_memreserve_table(void)80{81struct linux_efi_memreserve *rsv;82efi_guid_t memreserve_table_guid = LINUX_EFI_MEMRESERVE_TABLE_GUID;83efi_status_t status;8485status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, sizeof(*rsv),86(void **)&rsv);87if (status != EFI_SUCCESS) {88efi_err("Failed to allocate memreserve entry!\n");89return;90}9192rsv->next = 0;93rsv->size = 0;94atomic_set(&rsv->count, 0);9596status = efi_bs_call(install_configuration_table,97&memreserve_table_guid, rsv);98if (status != EFI_SUCCESS)99efi_err("Failed to install memreserve config table!\n");100}101102static u32 get_supported_rt_services(void)103{104const efi_rt_properties_table_t *rt_prop_table;105u32 supported = EFI_RT_SUPPORTED_ALL;106107rt_prop_table = get_efi_config_table(EFI_RT_PROPERTIES_TABLE_GUID);108if (rt_prop_table)109supported &= rt_prop_table->runtime_services_supported;110111return supported;112}113114efi_status_t efi_handle_cmdline(efi_loaded_image_t *image, char **cmdline_ptr)115{116char *cmdline __free(efi_pool) = NULL;117efi_status_t status;118119/*120* Get the command line from EFI, using the LOADED_IMAGE121* protocol. We are going to copy the command line into the122* device tree, so this can be allocated anywhere.123*/124cmdline = efi_convert_cmdline(image);125if (!cmdline) {126efi_err("getting command line via LOADED_IMAGE_PROTOCOL\n");127return EFI_OUT_OF_RESOURCES;128}129130if (!IS_ENABLED(CONFIG_CMDLINE_FORCE)) {131status = efi_parse_options(cmdline);132if (status != EFI_SUCCESS) {133efi_err("Failed to parse EFI load options\n");134return status;135}136}137138if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) ||139IS_ENABLED(CONFIG_CMDLINE_FORCE) ||140cmdline[0] == 0) {141status = efi_parse_options(CONFIG_CMDLINE);142if (status != EFI_SUCCESS) {143efi_err("Failed to parse built-in command line\n");144return status;145}146}147148*cmdline_ptr = no_free_ptr(cmdline);149return EFI_SUCCESS;150}151152efi_status_t efi_stub_common(efi_handle_t handle,153efi_loaded_image_t *image,154unsigned long image_addr,155char *cmdline_ptr)156{157struct sysfb_display_info *dpy;158efi_status_t status;159160status = check_platform_features();161if (status != EFI_SUCCESS)162return status;163164dpy = setup_primary_display();165166efi_retrieve_eventlog();167168/* Ask the firmware to clear memory on unclean shutdown */169efi_enable_reset_attack_mitigation();170171efi_load_initrd(image, ULONG_MAX, efi_get_max_initrd_addr(image_addr),172NULL);173174efi_random_get_seed();175176/* force efi_novamap if SetVirtualAddressMap() is unsupported */177efi_novamap |= !(get_supported_rt_services() &178EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP);179180install_memreserve_table();181182status = efi_boot_kernel(handle, image, image_addr, cmdline_ptr);183184free_primary_display(dpy);185186return status;187}188189/*190* efi_allocate_virtmap() - create a pool allocation for the virtmap191*192* Create an allocation that is of sufficient size to hold all the memory193* descriptors that will be passed to SetVirtualAddressMap() to inform the194* firmware about the virtual mapping that will be used under the OS to call195* into the firmware.196*/197efi_status_t efi_alloc_virtmap(efi_memory_desc_t **virtmap,198unsigned long *desc_size, u32 *desc_ver)199{200unsigned long size, mmap_key;201efi_status_t status;202203/*204* Use the size of the current memory map as an upper bound for the205* size of the buffer we need to pass to SetVirtualAddressMap() to206* cover all EFI_MEMORY_RUNTIME regions.207*/208size = 0;209status = efi_bs_call(get_memory_map, &size, NULL, &mmap_key, desc_size,210desc_ver);211if (status != EFI_BUFFER_TOO_SMALL)212return EFI_LOAD_ERROR;213214return efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,215(void **)virtmap);216}217218/*219* efi_get_virtmap() - create a virtual mapping for the EFI memory map220*221* This function populates the virt_addr fields of all memory region descriptors222* in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors223* are also copied to @runtime_map, and their total count is returned in @count.224*/225void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,226unsigned long desc_size, efi_memory_desc_t *runtime_map,227int *count)228{229u64 efi_virt_base = virtmap_base;230efi_memory_desc_t *in, *out = runtime_map;231int l;232233*count = 0;234235for (l = 0; l < map_size; l += desc_size) {236u64 paddr, size;237238in = (void *)memory_map + l;239if (!(in->attribute & EFI_MEMORY_RUNTIME))240continue;241242paddr = in->phys_addr;243size = in->num_pages * EFI_PAGE_SIZE;244245in->virt_addr = in->phys_addr + EFI_RT_VIRTUAL_OFFSET;246if (efi_novamap) {247continue;248}249250/*251* Make the mapping compatible with 64k pages: this allows252* a 4k page size kernel to kexec a 64k page size kernel and253* vice versa.254*/255if (!flat_va_mapping) {256257paddr = round_down(in->phys_addr, SZ_64K);258size += in->phys_addr - paddr;259260/*261* Avoid wasting memory on PTEs by choosing a virtual262* base that is compatible with section mappings if this263* region has the appropriate size and physical264* alignment. (Sections are 2 MB on 4k granule kernels)265*/266if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M)267efi_virt_base = round_up(efi_virt_base, SZ_2M);268else269efi_virt_base = round_up(efi_virt_base, SZ_64K);270271in->virt_addr += efi_virt_base - paddr;272efi_virt_base += size;273}274275memcpy(out, in, desc_size);276out = (void *)out + desc_size;277++*count;278}279}280281282