Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * EFI stub implementation that is shared by arm and arm64 architectures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * This should be #included by the EFI stub implementation files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (C) 2013,2014 Linaro Limited
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *     Roy Franz <roy.franz@linaro.org
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Copyright (C) 2013 Red Hat, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *     Mark Salter <msalter@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/efi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/libfdt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <asm/efi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include "efistub.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * This is the base address at which to start allocating virtual memory ranges
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * for UEFI Runtime Services.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * For ARM/ARM64:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * This is in the low TTBR0 range so that we can use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * any allocation we choose, and eliminate the risk of a conflict after kexec.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * The value chosen is the largest non-zero power of 2 suitable for this purpose
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * both on 32-bit and 64-bit ARM CPUs, to maximize the likelihood that it can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * be mapped efficiently.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * Since 32-bit ARM could potentially execute with a 1G/3G user/kernel split,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * map everything below 1 GB. (512 MB is a reasonable upper bound for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * entire footprint of the UEFI runtime services memory regions)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * For RISC-V:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * There is no specific reason for which, this address (512MB) can't be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * EFI runtime virtual address for RISC-V. It also helps to use EFI runtime
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * services on both RV32/RV64. Keep the same runtime virtual address for RISC-V
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * as well to minimize the code churn.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define EFI_RT_VIRTUAL_BASE	SZ_512M
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define EFI_RT_VIRTUAL_SIZE	SZ_512M
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #ifdef CONFIG_ARM64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) # define EFI_RT_VIRTUAL_LIMIT	DEFAULT_MAP_WINDOW_64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) # define EFI_RT_VIRTUAL_LIMIT	TASK_SIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static u64 virtmap_base = EFI_RT_VIRTUAL_BASE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) static bool flat_va_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) const efi_system_table_t *efi_system_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) static struct screen_info *setup_graphics(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	efi_guid_t gop_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	efi_status_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	unsigned long size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	void **gop_handle = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	struct screen_info *si = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 			     &gop_proto, NULL, &size, gop_handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	if (status == EFI_BUFFER_TOO_SMALL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		si = alloc_screen_info();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		if (!si)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		status = efi_setup_gop(si, &gop_proto, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		if (status != EFI_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 			free_screen_info(si);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	return si;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) static void install_memreserve_table(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	struct linux_efi_memreserve *rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	efi_guid_t memreserve_table_guid = LINUX_EFI_MEMRESERVE_TABLE_GUID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	efi_status_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, sizeof(*rsv),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 			     (void **)&rsv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	if (status != EFI_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		efi_err("Failed to allocate memreserve entry!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	rsv->next = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	rsv->size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	atomic_set(&rsv->count, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	status = efi_bs_call(install_configuration_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			     &memreserve_table_guid, rsv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (status != EFI_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		efi_err("Failed to install memreserve config table!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) static u32 get_supported_rt_services(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	const efi_rt_properties_table_t *rt_prop_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	u32 supported = EFI_RT_SUPPORTED_ALL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	rt_prop_table = get_efi_config_table(EFI_RT_PROPERTIES_TABLE_GUID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (rt_prop_table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		supported &= rt_prop_table->runtime_services_supported;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	return supported;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)  * EFI entry point for the arm/arm64 EFI stubs.  This is the entrypoint
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)  * that is described in the PE/COFF header.  Most of the code is the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)  * for both archictectures, with the arch-specific code provided in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)  * handle_kernel_image() function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 				   efi_system_table_t *sys_table_arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	efi_loaded_image_t *image;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	efi_status_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	unsigned long image_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	unsigned long image_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	/* addr/point and size pairs for memory management*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	unsigned long initrd_addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	unsigned long initrd_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	unsigned long fdt_addr = 0;  /* Original DTB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	unsigned long fdt_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	char *cmdline_ptr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	int cmdline_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	efi_guid_t loaded_image_proto = LOADED_IMAGE_PROTOCOL_GUID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	unsigned long reserve_addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	unsigned long reserve_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	enum efi_secureboot_mode secure_boot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	struct screen_info *si;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	efi_properties_table_t *prop_tbl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	unsigned long max_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	efi_system_table = sys_table_arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	/* Check if we were booted by the EFI firmware */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		status = EFI_INVALID_PARAMETER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	status = check_platform_features();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	if (status != EFI_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	 * Get a handle to the loaded image protocol.  This is used to get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	 * information about the running image, such as size and the command
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	 * line.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	status = efi_system_table->boottime->handle_protocol(handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 					&loaded_image_proto, (void *)&image);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (status != EFI_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		efi_err("Failed to get loaded image protocol\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	 * Get the command line from EFI, using the LOADED_IMAGE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	 * protocol. We are going to copy the command line into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	 * device tree, so this can be allocated anywhere.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	cmdline_ptr = efi_convert_cmdline(image, &cmdline_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	if (!cmdline_ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		efi_err("getting command line via LOADED_IMAGE_PROTOCOL\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		status = EFI_OUT_OF_RESOURCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	    IS_ENABLED(CONFIG_CMDLINE_FORCE) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	    cmdline_size == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		status = efi_parse_options(CONFIG_CMDLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		if (status != EFI_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 			efi_err("Failed to parse options\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 			goto fail_free_cmdline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && cmdline_size > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		status = efi_parse_options(cmdline_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		if (status != EFI_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 			efi_err("Failed to parse options\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 			goto fail_free_cmdline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	efi_info("Booting Linux Kernel...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	si = setup_graphics();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	status = handle_kernel_image(&image_addr, &image_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 				     &reserve_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 				     &reserve_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 				     image);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	if (status != EFI_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		efi_err("Failed to relocate kernel\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		goto fail_free_screeninfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	efi_retrieve_tpm2_eventlog();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	/* Ask the firmware to clear memory on unclean shutdown */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	efi_enable_reset_attack_mitigation();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	secure_boot = efi_get_secureboot();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	 * Unauthenticated device tree data is a security hazard, so ignore
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	 * 'dtb=' unless UEFI Secure Boot is disabled.  We assume that secure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	 * boot is enabled if we can't determine its state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	if (!IS_ENABLED(CONFIG_EFI_ARMSTUB_DTB_LOADER) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	     secure_boot != efi_secureboot_mode_disabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		if (strstr(cmdline_ptr, "dtb="))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 			efi_err("Ignoring DTB from command line.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		status = efi_load_dtb(image, &fdt_addr, &fdt_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		if (status != EFI_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 			efi_err("Failed to load device tree!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 			goto fail_free_image;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	if (fdt_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		efi_info("Using DTB from command line\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		/* Look for a device tree configuration table entry. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		fdt_addr = (uintptr_t)get_fdt(&fdt_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		if (fdt_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			efi_info("Using DTB from configuration table\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	if (!fdt_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		efi_info("Generating empty DTB\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	if (!efi_noinitrd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		max_addr = efi_get_max_initrd_addr(image_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		status = efi_load_initrd(image, &initrd_addr, &initrd_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 					 ULONG_MAX, max_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		if (status != EFI_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 			efi_err("Failed to load initrd!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	efi_random_get_seed();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	 * If the NX PE data feature is enabled in the properties table, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	 * should take care not to create a virtual mapping that changes the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	 * relative placement of runtime services code and data regions, as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	 * they may belong to the same PE/COFF executable image in memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	 * The easiest way to achieve that is to simply use a 1:1 mapping.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	prop_tbl = get_efi_config_table(EFI_PROPERTIES_TABLE_GUID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	flat_va_mapping = prop_tbl &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			  (prop_tbl->memory_protection_attribute &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 			   EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	/* force efi_novamap if SetVirtualAddressMap() is unsupported */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	efi_novamap |= !(get_supported_rt_services() &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 			 EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	/* hibernation expects the runtime regions to stay in the same place */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	if (!IS_ENABLED(CONFIG_HIBERNATION) && !efi_nokaslr && !flat_va_mapping) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		 * Randomize the base of the UEFI runtime services region.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		 * Preserve the 2 MB alignment of the region by taking a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		 * shift of 21 bit positions into account when scaling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		 * the headroom value using a 32-bit random value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		static const u64 headroom = EFI_RT_VIRTUAL_LIMIT -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 					    EFI_RT_VIRTUAL_BASE -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 					    EFI_RT_VIRTUAL_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		u32 rnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		status = efi_get_random_bytes(sizeof(rnd), (u8 *)&rnd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		if (status == EFI_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 			virtmap_base = EFI_RT_VIRTUAL_BASE +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 				       (((headroom >> 21) * rnd) >> (32 - 21));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	install_memreserve_table();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	status = allocate_new_fdt_and_exit_boot(handle, &fdt_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 						efi_get_max_fdt_addr(image_addr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 						initrd_addr, initrd_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 						cmdline_ptr, fdt_addr, fdt_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	if (status != EFI_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		goto fail_free_initrd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	if (IS_ENABLED(CONFIG_ARM))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		efi_handle_post_ebs_state();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	efi_enter_kernel(image_addr, fdt_addr, fdt_totalsize((void *)fdt_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	/* not reached */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) fail_free_initrd:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	efi_err("Failed to update FDT and exit boot services\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	efi_free(initrd_size, initrd_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	efi_free(fdt_size, fdt_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) fail_free_image:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	efi_free(image_size, image_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	efi_free(reserve_size, reserve_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) fail_free_screeninfo:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	free_screen_info(si);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) fail_free_cmdline:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	efi_bs_call(free_pool, cmdline_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)  * efi_get_virtmap() - create a virtual mapping for the EFI memory map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)  * This function populates the virt_addr fields of all memory region descriptors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)  * in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)  * are also copied to @runtime_map, and their total count is returned in @count.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		     unsigned long desc_size, efi_memory_desc_t *runtime_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		     int *count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	u64 efi_virt_base = virtmap_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	efi_memory_desc_t *in, *out = runtime_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	int l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	for (l = 0; l < map_size; l += desc_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		u64 paddr, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		in = (void *)memory_map + l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		if (!(in->attribute & EFI_MEMORY_RUNTIME))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		paddr = in->phys_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		size = in->num_pages * EFI_PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		in->virt_addr = in->phys_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		if (efi_novamap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		 * Make the mapping compatible with 64k pages: this allows
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		 * a 4k page size kernel to kexec a 64k page size kernel and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		 * vice versa.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		if (!flat_va_mapping) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 			paddr = round_down(in->phys_addr, SZ_64K);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 			size += in->phys_addr - paddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 			 * Avoid wasting memory on PTEs by choosing a virtual
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 			 * base that is compatible with section mappings if this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 			 * region has the appropriate size and physical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 			 * alignment. (Sections are 2 MB on 4k granule kernels)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 			if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 				efi_virt_base = round_up(efi_virt_base, SZ_2M);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 				efi_virt_base = round_up(efi_virt_base, SZ_64K);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 			in->virt_addr += efi_virt_base - paddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 			efi_virt_base += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		memcpy(out, in, desc_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		out = (void *)out + desc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		++*count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) }