1 // SPDX-License-Identifier: GPL-2.0-only
3 * EFI stub implementation that is shared by arm and arm64 architectures.
4 * This should be #included by the EFI stub implementation files.
6 * Copyright (C) 2013,2014 Linaro Limited
7 * Roy Franz <roy.franz@linaro.org
8 * Copyright (C) 2013 Red Hat, Inc.
9 * Mark Salter <msalter@redhat.com>
12 #include <linux/efi.h>
18 * This is the base address at which to start allocating virtual memory ranges
19 * for UEFI Runtime Services.
22 * This is in the low TTBR0 range so that we can use
23 * 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 purpose
25 * both on 32-bit and 64-bit ARM CPUs, to maximize the likelihood that it can
26 * 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 the
29 * entire footprint of the UEFI runtime services memory regions)
32 * There is no specific reason for which, this address (512MB) can't be used
33 * EFI runtime virtual address for RISC-V. It also helps to use EFI runtime
34 * services on both RV32/RV64. Keep the same runtime virtual address for RISC-V
35 * as well to minimize the code churn.
37 #define EFI_RT_VIRTUAL_BASE SZ_512M
40 * Some architectures map the EFI regions into the kernel's linear map using a
43 #ifndef EFI_RT_VIRTUAL_OFFSET
44 #define EFI_RT_VIRTUAL_OFFSET 0
47 static u64 virtmap_base = EFI_RT_VIRTUAL_BASE;
48 static bool flat_va_mapping = (EFI_RT_VIRTUAL_OFFSET != 0);
50 struct screen_info * __weak alloc_screen_info(void)
55 void __weak free_screen_info(struct screen_info *si)
59 static struct screen_info *setup_graphics(void)
61 efi_guid_t gop_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
64 void **gop_handle = NULL;
65 struct screen_info *si = NULL;
68 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
69 &gop_proto, NULL, &size, gop_handle);
70 if (status == EFI_BUFFER_TOO_SMALL) {
71 si = alloc_screen_info();
74 status = efi_setup_gop(si, &gop_proto, size);
75 if (status != EFI_SUCCESS) {
83 static void install_memreserve_table(void)
85 struct linux_efi_memreserve *rsv;
86 efi_guid_t memreserve_table_guid = LINUX_EFI_MEMRESERVE_TABLE_GUID;
89 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, sizeof(*rsv),
91 if (status != EFI_SUCCESS) {
92 efi_err("Failed to allocate memreserve entry!\n");
98 atomic_set(&rsv->count, 0);
100 status = efi_bs_call(install_configuration_table,
101 &memreserve_table_guid, rsv);
102 if (status != EFI_SUCCESS)
103 efi_err("Failed to install memreserve config table!\n");
106 static u32 get_supported_rt_services(void)
108 const efi_rt_properties_table_t *rt_prop_table;
109 u32 supported = EFI_RT_SUPPORTED_ALL;
111 rt_prop_table = get_efi_config_table(EFI_RT_PROPERTIES_TABLE_GUID);
113 supported &= rt_prop_table->runtime_services_supported;
118 efi_status_t efi_handle_cmdline(efi_loaded_image_t *image, char **cmdline_ptr)
120 int cmdline_size = 0;
125 * Get the command line from EFI, using the LOADED_IMAGE
126 * protocol. We are going to copy the command line into the
127 * device tree, so this can be allocated anywhere.
129 cmdline = efi_convert_cmdline(image, &cmdline_size);
131 efi_err("getting command line via LOADED_IMAGE_PROTOCOL\n");
132 return EFI_OUT_OF_RESOURCES;
135 if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) ||
136 IS_ENABLED(CONFIG_CMDLINE_FORCE) ||
138 status = efi_parse_options(CONFIG_CMDLINE);
139 if (status != EFI_SUCCESS) {
140 efi_err("Failed to parse options\n");
141 goto fail_free_cmdline;
145 if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && cmdline_size > 0) {
146 status = efi_parse_options(cmdline);
147 if (status != EFI_SUCCESS) {
148 efi_err("Failed to parse options\n");
149 goto fail_free_cmdline;
153 *cmdline_ptr = cmdline;
157 efi_bs_call(free_pool, cmdline_ptr);
161 efi_status_t efi_stub_common(efi_handle_t handle,
162 efi_loaded_image_t *image,
163 unsigned long image_addr,
166 struct screen_info *si;
169 status = check_platform_features();
170 if (status != EFI_SUCCESS)
173 si = setup_graphics();
175 efi_retrieve_tpm2_eventlog();
177 /* Ask the firmware to clear memory on unclean shutdown */
178 efi_enable_reset_attack_mitigation();
180 efi_load_initrd(image, ULONG_MAX, efi_get_max_initrd_addr(image_addr),
183 efi_random_get_seed();
185 /* force efi_novamap if SetVirtualAddressMap() is unsupported */
186 efi_novamap |= !(get_supported_rt_services() &
187 EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP);
189 install_memreserve_table();
191 status = efi_boot_kernel(handle, image, image_addr, cmdline_ptr);
193 free_screen_info(si);
198 * efi_allocate_virtmap() - create a pool allocation for the virtmap
200 * Create an allocation that is of sufficient size to hold all the memory
201 * descriptors that will be passed to SetVirtualAddressMap() to inform the
202 * firmware about the virtual mapping that will be used under the OS to call
205 efi_status_t efi_alloc_virtmap(efi_memory_desc_t **virtmap,
206 unsigned long *desc_size, u32 *desc_ver)
208 unsigned long size, mmap_key;
212 * Use the size of the current memory map as an upper bound for the
213 * size of the buffer we need to pass to SetVirtualAddressMap() to
214 * cover all EFI_MEMORY_RUNTIME regions.
217 status = efi_bs_call(get_memory_map, &size, NULL, &mmap_key, desc_size,
219 if (status != EFI_BUFFER_TOO_SMALL)
220 return EFI_LOAD_ERROR;
222 return efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
227 * efi_get_virtmap() - create a virtual mapping for the EFI memory map
229 * This function populates the virt_addr fields of all memory region descriptors
230 * in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors
231 * are also copied to @runtime_map, and their total count is returned in @count.
233 void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
234 unsigned long desc_size, efi_memory_desc_t *runtime_map,
237 u64 efi_virt_base = virtmap_base;
238 efi_memory_desc_t *in, *out = runtime_map;
243 for (l = 0; l < map_size; l += desc_size) {
246 in = (void *)memory_map + l;
247 if (!(in->attribute & EFI_MEMORY_RUNTIME))
250 paddr = in->phys_addr;
251 size = in->num_pages * EFI_PAGE_SIZE;
253 in->virt_addr = in->phys_addr + EFI_RT_VIRTUAL_OFFSET;
259 * Make the mapping compatible with 64k pages: this allows
260 * a 4k page size kernel to kexec a 64k page size kernel and
263 if (!flat_va_mapping) {
265 paddr = round_down(in->phys_addr, SZ_64K);
266 size += in->phys_addr - paddr;
269 * Avoid wasting memory on PTEs by choosing a virtual
270 * base that is compatible with section mappings if this
271 * region has the appropriate size and physical
272 * alignment. (Sections are 2 MB on 4k granule kernels)
274 if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M)
275 efi_virt_base = round_up(efi_virt_base, SZ_2M);
277 efi_virt_base = round_up(efi_virt_base, SZ_64K);
279 in->virt_addr += efi_virt_base - paddr;
280 efi_virt_base += size;
283 memcpy(out, in, desc_size);
284 out = (void *)out + desc_size;