12e0eb483SAtish Patra // SPDX-License-Identifier: GPL-2.0-only
22e0eb483SAtish Patra /*
32e0eb483SAtish Patra  * EFI stub implementation that is shared by arm and arm64 architectures.
42e0eb483SAtish Patra  * This should be #included by the EFI stub implementation files.
52e0eb483SAtish Patra  *
62e0eb483SAtish Patra  * Copyright (C) 2013,2014 Linaro Limited
72e0eb483SAtish Patra  *     Roy Franz <roy.franz@linaro.org
82e0eb483SAtish Patra  * Copyright (C) 2013 Red Hat, Inc.
92e0eb483SAtish Patra  *     Mark Salter <msalter@redhat.com>
102e0eb483SAtish Patra  */
112e0eb483SAtish Patra 
122e0eb483SAtish Patra #include <linux/efi.h>
132e0eb483SAtish Patra #include <asm/efi.h>
142e0eb483SAtish Patra 
152e0eb483SAtish Patra #include "efistub.h"
162e0eb483SAtish Patra 
172e0eb483SAtish Patra /*
182e0eb483SAtish Patra  * This is the base address at which to start allocating virtual memory ranges
19b91540d5SAtish Patra  * for UEFI Runtime Services.
20b91540d5SAtish Patra  *
21b91540d5SAtish Patra  * For ARM/ARM64:
22b91540d5SAtish Patra  * This is in the low TTBR0 range so that we can use
232e0eb483SAtish Patra  * any allocation we choose, and eliminate the risk of a conflict after kexec.
242e0eb483SAtish Patra  * The value chosen is the largest non-zero power of 2 suitable for this purpose
252e0eb483SAtish Patra  * both on 32-bit and 64-bit ARM CPUs, to maximize the likelihood that it can
262e0eb483SAtish Patra  * be mapped efficiently.
272e0eb483SAtish Patra  * Since 32-bit ARM could potentially execute with a 1G/3G user/kernel split,
282e0eb483SAtish Patra  * map everything below 1 GB. (512 MB is a reasonable upper bound for the
292e0eb483SAtish Patra  * entire footprint of the UEFI runtime services memory regions)
30b91540d5SAtish Patra  *
31b91540d5SAtish Patra  * For RISC-V:
32b91540d5SAtish Patra  * There is no specific reason for which, this address (512MB) can't be used
33b91540d5SAtish Patra  * EFI runtime virtual address for RISC-V. It also helps to use EFI runtime
34b91540d5SAtish Patra  * services on both RV32/RV64. Keep the same runtime virtual address for RISC-V
35b91540d5SAtish Patra  * as well to minimize the code churn.
362e0eb483SAtish Patra  */
372e0eb483SAtish Patra #define EFI_RT_VIRTUAL_BASE	SZ_512M
382e0eb483SAtish Patra 
39ead384d9SHuacai Chen /*
40ead384d9SHuacai Chen  * Some architectures map the EFI regions into the kernel's linear map using a
41ead384d9SHuacai Chen  * fixed offset.
42ead384d9SHuacai Chen  */
43ead384d9SHuacai Chen #ifndef EFI_RT_VIRTUAL_OFFSET
44ead384d9SHuacai Chen #define EFI_RT_VIRTUAL_OFFSET	0
45ead384d9SHuacai Chen #endif
46ead384d9SHuacai Chen 
472e0eb483SAtish Patra static u64 virtmap_base = EFI_RT_VIRTUAL_BASE;
48ead384d9SHuacai Chen static bool flat_va_mapping = (EFI_RT_VIRTUAL_OFFSET != 0);
492e0eb483SAtish Patra 
free_screen_info(struct screen_info * si)50732ea9dbSArd Biesheuvel void __weak free_screen_info(struct screen_info *si)
51732ea9dbSArd Biesheuvel {
52732ea9dbSArd Biesheuvel }
53732ea9dbSArd Biesheuvel 
setup_graphics(void)542e0eb483SAtish Patra static struct screen_info *setup_graphics(void)
552e0eb483SAtish Patra {
562e0eb483SAtish Patra 	efi_guid_t gop_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
572e0eb483SAtish Patra 	efi_status_t status;
582e0eb483SAtish Patra 	unsigned long size;
592e0eb483SAtish Patra 	void **gop_handle = NULL;
602e0eb483SAtish Patra 	struct screen_info *si = NULL;
612e0eb483SAtish Patra 
622e0eb483SAtish Patra 	size = 0;
632e0eb483SAtish Patra 	status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
642e0eb483SAtish Patra 			     &gop_proto, NULL, &size, gop_handle);
652e0eb483SAtish Patra 	if (status == EFI_BUFFER_TOO_SMALL) {
662e0eb483SAtish Patra 		si = alloc_screen_info();
672e0eb483SAtish Patra 		if (!si)
682e0eb483SAtish Patra 			return NULL;
69a5d8e55bSIngo Molnar 		status = efi_setup_gop(si, &gop_proto, size);
70a5d8e55bSIngo Molnar 		if (status != EFI_SUCCESS) {
71a5d8e55bSIngo Molnar 			free_screen_info(si);
72a5d8e55bSIngo Molnar 			return NULL;
73a5d8e55bSIngo Molnar 		}
742e0eb483SAtish Patra 	}
752e0eb483SAtish Patra 	return si;
762e0eb483SAtish Patra }
772e0eb483SAtish Patra 
install_memreserve_table(void)7887cd6378SZou Wei static void install_memreserve_table(void)
792e0eb483SAtish Patra {
802e0eb483SAtish Patra 	struct linux_efi_memreserve *rsv;
812e0eb483SAtish Patra 	efi_guid_t memreserve_table_guid = LINUX_EFI_MEMRESERVE_TABLE_GUID;
822e0eb483SAtish Patra 	efi_status_t status;
832e0eb483SAtish Patra 
842e0eb483SAtish Patra 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, sizeof(*rsv),
852e0eb483SAtish Patra 			     (void **)&rsv);
862e0eb483SAtish Patra 	if (status != EFI_SUCCESS) {
87793473c2SArvind Sankar 		efi_err("Failed to allocate memreserve entry!\n");
882e0eb483SAtish Patra 		return;
892e0eb483SAtish Patra 	}
902e0eb483SAtish Patra 
912e0eb483SAtish Patra 	rsv->next = 0;
922e0eb483SAtish Patra 	rsv->size = 0;
932e0eb483SAtish Patra 	atomic_set(&rsv->count, 0);
942e0eb483SAtish Patra 
952e0eb483SAtish Patra 	status = efi_bs_call(install_configuration_table,
962e0eb483SAtish Patra 			     &memreserve_table_guid, rsv);
972e0eb483SAtish Patra 	if (status != EFI_SUCCESS)
98793473c2SArvind Sankar 		efi_err("Failed to install memreserve config table!\n");
992e0eb483SAtish Patra }
1002e0eb483SAtish Patra 
get_supported_rt_services(void)1019e9888a0SArd Biesheuvel static u32 get_supported_rt_services(void)
1029e9888a0SArd Biesheuvel {
1039e9888a0SArd Biesheuvel 	const efi_rt_properties_table_t *rt_prop_table;
1049e9888a0SArd Biesheuvel 	u32 supported = EFI_RT_SUPPORTED_ALL;
1059e9888a0SArd Biesheuvel 
1069e9888a0SArd Biesheuvel 	rt_prop_table = get_efi_config_table(EFI_RT_PROPERTIES_TABLE_GUID);
1079e9888a0SArd Biesheuvel 	if (rt_prop_table)
1089e9888a0SArd Biesheuvel 		supported &= rt_prop_table->runtime_services_supported;
1099e9888a0SArd Biesheuvel 
1109e9888a0SArd Biesheuvel 	return supported;
1119e9888a0SArd Biesheuvel }
1129e9888a0SArd Biesheuvel 
efi_handle_cmdline(efi_loaded_image_t * image,char ** cmdline_ptr)113*42c8ea3dSArd Biesheuvel efi_status_t efi_handle_cmdline(efi_loaded_image_t *image, char **cmdline_ptr)
1142e0eb483SAtish Patra {
1152e0eb483SAtish Patra 	int cmdline_size = 0;
116*42c8ea3dSArd Biesheuvel 	efi_status_t status;
117*42c8ea3dSArd Biesheuvel 	char *cmdline;
1182e0eb483SAtish Patra 
1192e0eb483SAtish Patra 	/*
1202e0eb483SAtish Patra 	 * Get the command line from EFI, using the LOADED_IMAGE
1212e0eb483SAtish Patra 	 * protocol. We are going to copy the command line into the
1222e0eb483SAtish Patra 	 * device tree, so this can be allocated anywhere.
1232e0eb483SAtish Patra 	 */
124*42c8ea3dSArd Biesheuvel 	cmdline = efi_convert_cmdline(image, &cmdline_size);
125*42c8ea3dSArd Biesheuvel 	if (!cmdline) {
126793473c2SArvind Sankar 		efi_err("getting command line via LOADED_IMAGE_PROTOCOL\n");
127*42c8ea3dSArd Biesheuvel 		return EFI_OUT_OF_RESOURCES;
1282e0eb483SAtish Patra 	}
1292e0eb483SAtish Patra 
1302e0eb483SAtish Patra 	if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) ||
1312e0eb483SAtish Patra 	    IS_ENABLED(CONFIG_CMDLINE_FORCE) ||
132055042beSArvind Sankar 	    cmdline_size == 0) {
133055042beSArvind Sankar 		status = efi_parse_options(CONFIG_CMDLINE);
134055042beSArvind Sankar 		if (status != EFI_SUCCESS) {
135055042beSArvind Sankar 			efi_err("Failed to parse options\n");
136055042beSArvind Sankar 			goto fail_free_cmdline;
137055042beSArvind Sankar 		}
138055042beSArvind Sankar 	}
1392e0eb483SAtish Patra 
140055042beSArvind Sankar 	if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && cmdline_size > 0) {
141*42c8ea3dSArd Biesheuvel 		status = efi_parse_options(cmdline);
142055042beSArvind Sankar 		if (status != EFI_SUCCESS) {
143055042beSArvind Sankar 			efi_err("Failed to parse options\n");
144055042beSArvind Sankar 			goto fail_free_cmdline;
145055042beSArvind Sankar 		}
146055042beSArvind Sankar 	}
1472e0eb483SAtish Patra 
148*42c8ea3dSArd Biesheuvel 	*cmdline_ptr = cmdline;
149*42c8ea3dSArd Biesheuvel 	return EFI_SUCCESS;
150*42c8ea3dSArd Biesheuvel 
151*42c8ea3dSArd Biesheuvel fail_free_cmdline:
152*42c8ea3dSArd Biesheuvel 	efi_bs_call(free_pool, cmdline_ptr);
153*42c8ea3dSArd Biesheuvel 	return status;
154*42c8ea3dSArd Biesheuvel }
155*42c8ea3dSArd Biesheuvel 
efi_stub_common(efi_handle_t handle,efi_loaded_image_t * image,unsigned long image_addr,char * cmdline_ptr)156*42c8ea3dSArd Biesheuvel efi_status_t efi_stub_common(efi_handle_t handle,
157*42c8ea3dSArd Biesheuvel 			     efi_loaded_image_t *image,
158*42c8ea3dSArd Biesheuvel 			     unsigned long image_addr,
159*42c8ea3dSArd Biesheuvel 			     char *cmdline_ptr)
160*42c8ea3dSArd Biesheuvel {
161*42c8ea3dSArd Biesheuvel 	struct screen_info *si;
162*42c8ea3dSArd Biesheuvel 	efi_status_t status;
163*42c8ea3dSArd Biesheuvel 
164*42c8ea3dSArd Biesheuvel 	status = check_platform_features();
165*42c8ea3dSArd Biesheuvel 	if (status != EFI_SUCCESS)
166*42c8ea3dSArd Biesheuvel 		return status;
1672e0eb483SAtish Patra 
1682e0eb483SAtish Patra 	si = setup_graphics();
1692e0eb483SAtish Patra 
1702e0eb483SAtish Patra 	efi_retrieve_tpm2_eventlog();
1712e0eb483SAtish Patra 
1722e0eb483SAtish Patra 	/* Ask the firmware to clear memory on unclean shutdown */
1732e0eb483SAtish Patra 	efi_enable_reset_attack_mitigation();
1742e0eb483SAtish Patra 
175f4dc7fffSArd Biesheuvel 	efi_load_initrd(image, ULONG_MAX, efi_get_max_initrd_addr(image_addr),
176f4dc7fffSArd Biesheuvel 			NULL);
1772e0eb483SAtish Patra 
1782e0eb483SAtish Patra 	efi_random_get_seed();
1792e0eb483SAtish Patra 
1809e9888a0SArd Biesheuvel 	/* force efi_novamap if SetVirtualAddressMap() is unsupported */
1819e9888a0SArd Biesheuvel 	efi_novamap |= !(get_supported_rt_services() &
1829e9888a0SArd Biesheuvel 			 EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP);
1839e9888a0SArd Biesheuvel 
1842e0eb483SAtish Patra 	install_memreserve_table();
1852e0eb483SAtish Patra 
1864fc8e738SArd Biesheuvel 	status = efi_boot_kernel(handle, image, image_addr, cmdline_ptr);
1872e0eb483SAtish Patra 
1882e0eb483SAtish Patra 	free_screen_info(si);
1892e0eb483SAtish Patra 	return status;
1902e0eb483SAtish Patra }
1912e0eb483SAtish Patra 
1922e0eb483SAtish Patra /*
193f80d2604SArd Biesheuvel  * efi_allocate_virtmap() - create a pool allocation for the virtmap
194f80d2604SArd Biesheuvel  *
195f80d2604SArd Biesheuvel  * Create an allocation that is of sufficient size to hold all the memory
196f80d2604SArd Biesheuvel  * descriptors that will be passed to SetVirtualAddressMap() to inform the
197f80d2604SArd Biesheuvel  * firmware about the virtual mapping that will be used under the OS to call
198f80d2604SArd Biesheuvel  * into the firmware.
199f80d2604SArd Biesheuvel  */
efi_alloc_virtmap(efi_memory_desc_t ** virtmap,unsigned long * desc_size,u32 * desc_ver)200f80d2604SArd Biesheuvel efi_status_t efi_alloc_virtmap(efi_memory_desc_t **virtmap,
201f80d2604SArd Biesheuvel 			       unsigned long *desc_size, u32 *desc_ver)
202f80d2604SArd Biesheuvel {
203f80d2604SArd Biesheuvel 	unsigned long size, mmap_key;
204f80d2604SArd Biesheuvel 	efi_status_t status;
205f80d2604SArd Biesheuvel 
206f80d2604SArd Biesheuvel 	/*
207f80d2604SArd Biesheuvel 	 * Use the size of the current memory map as an upper bound for the
208f80d2604SArd Biesheuvel 	 * size of the buffer we need to pass to SetVirtualAddressMap() to
209f80d2604SArd Biesheuvel 	 * cover all EFI_MEMORY_RUNTIME regions.
210f80d2604SArd Biesheuvel 	 */
211f80d2604SArd Biesheuvel 	size = 0;
212f80d2604SArd Biesheuvel 	status = efi_bs_call(get_memory_map, &size, NULL, &mmap_key, desc_size,
213f80d2604SArd Biesheuvel 			     desc_ver);
214f80d2604SArd Biesheuvel 	if (status != EFI_BUFFER_TOO_SMALL)
215f80d2604SArd Biesheuvel 		return EFI_LOAD_ERROR;
216f80d2604SArd Biesheuvel 
217f80d2604SArd Biesheuvel 	return efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
218f80d2604SArd Biesheuvel 			   (void **)virtmap);
219f80d2604SArd Biesheuvel }
220f80d2604SArd Biesheuvel 
221f80d2604SArd Biesheuvel /*
2222e0eb483SAtish Patra  * efi_get_virtmap() - create a virtual mapping for the EFI memory map
2232e0eb483SAtish Patra  *
2242e0eb483SAtish Patra  * This function populates the virt_addr fields of all memory region descriptors
2252e0eb483SAtish Patra  * in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors
2262e0eb483SAtish Patra  * are also copied to @runtime_map, and their total count is returned in @count.
2272e0eb483SAtish Patra  */
efi_get_virtmap(efi_memory_desc_t * memory_map,unsigned long map_size,unsigned long desc_size,efi_memory_desc_t * runtime_map,int * count)2282e0eb483SAtish Patra void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
2292e0eb483SAtish Patra 		     unsigned long desc_size, efi_memory_desc_t *runtime_map,
2302e0eb483SAtish Patra 		     int *count)
2312e0eb483SAtish Patra {
2322e0eb483SAtish Patra 	u64 efi_virt_base = virtmap_base;
2332e0eb483SAtish Patra 	efi_memory_desc_t *in, *out = runtime_map;
2342e0eb483SAtish Patra 	int l;
2352e0eb483SAtish Patra 
236f80d2604SArd Biesheuvel 	*count = 0;
237f80d2604SArd Biesheuvel 
2382e0eb483SAtish Patra 	for (l = 0; l < map_size; l += desc_size) {
2392e0eb483SAtish Patra 		u64 paddr, size;
2402e0eb483SAtish Patra 
2412e0eb483SAtish Patra 		in = (void *)memory_map + l;
2422e0eb483SAtish Patra 		if (!(in->attribute & EFI_MEMORY_RUNTIME))
2432e0eb483SAtish Patra 			continue;
2442e0eb483SAtish Patra 
2452e0eb483SAtish Patra 		paddr = in->phys_addr;
2462e0eb483SAtish Patra 		size = in->num_pages * EFI_PAGE_SIZE;
2472e0eb483SAtish Patra 
248ead384d9SHuacai Chen 		in->virt_addr = in->phys_addr + EFI_RT_VIRTUAL_OFFSET;
249980771f6SArd Biesheuvel 		if (efi_novamap) {
2502e0eb483SAtish Patra 			continue;
2512e0eb483SAtish Patra 		}
2522e0eb483SAtish Patra 
2532e0eb483SAtish Patra 		/*
2542e0eb483SAtish Patra 		 * Make the mapping compatible with 64k pages: this allows
2552e0eb483SAtish Patra 		 * a 4k page size kernel to kexec a 64k page size kernel and
2562e0eb483SAtish Patra 		 * vice versa.
2572e0eb483SAtish Patra 		 */
2582e0eb483SAtish Patra 		if (!flat_va_mapping) {
2592e0eb483SAtish Patra 
2602e0eb483SAtish Patra 			paddr = round_down(in->phys_addr, SZ_64K);
2612e0eb483SAtish Patra 			size += in->phys_addr - paddr;
2622e0eb483SAtish Patra 
2632e0eb483SAtish Patra 			/*
2642e0eb483SAtish Patra 			 * Avoid wasting memory on PTEs by choosing a virtual
2652e0eb483SAtish Patra 			 * base that is compatible with section mappings if this
2662e0eb483SAtish Patra 			 * region has the appropriate size and physical
2672e0eb483SAtish Patra 			 * alignment. (Sections are 2 MB on 4k granule kernels)
2682e0eb483SAtish Patra 			 */
2692e0eb483SAtish Patra 			if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M)
2702e0eb483SAtish Patra 				efi_virt_base = round_up(efi_virt_base, SZ_2M);
2712e0eb483SAtish Patra 			else
2722e0eb483SAtish Patra 				efi_virt_base = round_up(efi_virt_base, SZ_64K);
2732e0eb483SAtish Patra 
2742e0eb483SAtish Patra 			in->virt_addr += efi_virt_base - paddr;
2752e0eb483SAtish Patra 			efi_virt_base += size;
2762e0eb483SAtish Patra 		}
2772e0eb483SAtish Patra 
2782e0eb483SAtish Patra 		memcpy(out, in, desc_size);
2792e0eb483SAtish Patra 		out = (void *)out + desc_size;
2802e0eb483SAtish Patra 		++*count;
2812e0eb483SAtish Patra 	}
2822e0eb483SAtish Patra }
283