1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * EFI stub implementation that is shared by arm and arm64 architectures. 4 * This should be #included by the EFI stub implementation files. 5 * 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> 10 */ 11 12 #include <linux/efi.h> 13 #include <asm/efi.h> 14 15 #include "efistub.h" 16 17 /* 18 * This is the base address at which to start allocating virtual memory ranges 19 * for UEFI Runtime Services. 20 * 21 * For ARM/ARM64: 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) 30 * 31 * For RISC-V: 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. 36 */ 37 #define EFI_RT_VIRTUAL_BASE SZ_512M 38 39 /* 40 * Some architectures map the EFI regions into the kernel's linear map using a 41 * fixed offset. 42 */ 43 #ifndef EFI_RT_VIRTUAL_OFFSET 44 #define EFI_RT_VIRTUAL_OFFSET 0 45 #endif 46 47 static u64 virtmap_base = EFI_RT_VIRTUAL_BASE; 48 static bool flat_va_mapping = (EFI_RT_VIRTUAL_OFFSET != 0); 49 50 static struct screen_info *setup_graphics(void) 51 { 52 efi_guid_t gop_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; 53 efi_status_t status; 54 unsigned long size; 55 void **gop_handle = NULL; 56 struct screen_info *si = NULL; 57 58 size = 0; 59 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, 60 &gop_proto, NULL, &size, gop_handle); 61 if (status == EFI_BUFFER_TOO_SMALL) { 62 si = alloc_screen_info(); 63 if (!si) 64 return NULL; 65 status = efi_setup_gop(si, &gop_proto, size); 66 if (status != EFI_SUCCESS) { 67 free_screen_info(si); 68 return NULL; 69 } 70 } 71 return si; 72 } 73 74 static void install_memreserve_table(void) 75 { 76 struct linux_efi_memreserve *rsv; 77 efi_guid_t memreserve_table_guid = LINUX_EFI_MEMRESERVE_TABLE_GUID; 78 efi_status_t status; 79 80 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, sizeof(*rsv), 81 (void **)&rsv); 82 if (status != EFI_SUCCESS) { 83 efi_err("Failed to allocate memreserve entry!\n"); 84 return; 85 } 86 87 rsv->next = 0; 88 rsv->size = 0; 89 atomic_set(&rsv->count, 0); 90 91 status = efi_bs_call(install_configuration_table, 92 &memreserve_table_guid, rsv); 93 if (status != EFI_SUCCESS) 94 efi_err("Failed to install memreserve config table!\n"); 95 } 96 97 static u32 get_supported_rt_services(void) 98 { 99 const efi_rt_properties_table_t *rt_prop_table; 100 u32 supported = EFI_RT_SUPPORTED_ALL; 101 102 rt_prop_table = get_efi_config_table(EFI_RT_PROPERTIES_TABLE_GUID); 103 if (rt_prop_table) 104 supported &= rt_prop_table->runtime_services_supported; 105 106 return supported; 107 } 108 109 /* 110 * EFI entry point for the arm/arm64 EFI stubs. This is the entrypoint 111 * that is described in the PE/COFF header. Most of the code is the same 112 * for both archictectures, with the arch-specific code provided in the 113 * handle_kernel_image() function. 114 */ 115 efi_status_t __efiapi efi_pe_entry(efi_handle_t handle, 116 efi_system_table_t *sys_table_arg) 117 { 118 efi_loaded_image_t *image; 119 efi_status_t status; 120 unsigned long image_addr; 121 unsigned long image_size = 0; 122 /* addr/point and size pairs for memory management*/ 123 char *cmdline_ptr = NULL; 124 int cmdline_size = 0; 125 efi_guid_t loaded_image_proto = LOADED_IMAGE_PROTOCOL_GUID; 126 unsigned long reserve_addr = 0; 127 unsigned long reserve_size = 0; 128 struct screen_info *si; 129 130 efi_system_table = sys_table_arg; 131 132 /* Check if we were booted by the EFI firmware */ 133 if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) { 134 status = EFI_INVALID_PARAMETER; 135 goto fail; 136 } 137 138 status = check_platform_features(); 139 if (status != EFI_SUCCESS) 140 goto fail; 141 142 /* 143 * Get a handle to the loaded image protocol. This is used to get 144 * information about the running image, such as size and the command 145 * line. 146 */ 147 status = efi_bs_call(handle_protocol, handle, &loaded_image_proto, 148 (void *)&image); 149 if (status != EFI_SUCCESS) { 150 efi_err("Failed to get loaded image protocol\n"); 151 goto fail; 152 } 153 154 /* 155 * Get the command line from EFI, using the LOADED_IMAGE 156 * protocol. We are going to copy the command line into the 157 * device tree, so this can be allocated anywhere. 158 */ 159 cmdline_ptr = efi_convert_cmdline(image, &cmdline_size); 160 if (!cmdline_ptr) { 161 efi_err("getting command line via LOADED_IMAGE_PROTOCOL\n"); 162 status = EFI_OUT_OF_RESOURCES; 163 goto fail; 164 } 165 166 if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) || 167 IS_ENABLED(CONFIG_CMDLINE_FORCE) || 168 cmdline_size == 0) { 169 status = efi_parse_options(CONFIG_CMDLINE); 170 if (status != EFI_SUCCESS) { 171 efi_err("Failed to parse options\n"); 172 goto fail_free_cmdline; 173 } 174 } 175 176 if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && cmdline_size > 0) { 177 status = efi_parse_options(cmdline_ptr); 178 if (status != EFI_SUCCESS) { 179 efi_err("Failed to parse options\n"); 180 goto fail_free_cmdline; 181 } 182 } 183 184 efi_info("Booting Linux Kernel...\n"); 185 186 si = setup_graphics(); 187 188 status = handle_kernel_image(&image_addr, &image_size, 189 &reserve_addr, 190 &reserve_size, 191 image, handle); 192 if (status != EFI_SUCCESS) { 193 efi_err("Failed to relocate kernel\n"); 194 goto fail_free_screeninfo; 195 } 196 197 efi_retrieve_tpm2_eventlog(); 198 199 /* Ask the firmware to clear memory on unclean shutdown */ 200 efi_enable_reset_attack_mitigation(); 201 202 efi_load_initrd(image, ULONG_MAX, efi_get_max_initrd_addr(image_addr), 203 NULL); 204 205 efi_random_get_seed(); 206 207 /* force efi_novamap if SetVirtualAddressMap() is unsupported */ 208 efi_novamap |= !(get_supported_rt_services() & 209 EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP); 210 211 install_memreserve_table(); 212 213 status = efi_boot_kernel(handle, image, image_addr, cmdline_ptr); 214 215 efi_free(image_size, image_addr); 216 efi_free(reserve_size, reserve_addr); 217 fail_free_screeninfo: 218 free_screen_info(si); 219 fail_free_cmdline: 220 efi_bs_call(free_pool, cmdline_ptr); 221 fail: 222 return status; 223 } 224 225 /* 226 * efi_allocate_virtmap() - create a pool allocation for the virtmap 227 * 228 * Create an allocation that is of sufficient size to hold all the memory 229 * descriptors that will be passed to SetVirtualAddressMap() to inform the 230 * firmware about the virtual mapping that will be used under the OS to call 231 * into the firmware. 232 */ 233 efi_status_t efi_alloc_virtmap(efi_memory_desc_t **virtmap, 234 unsigned long *desc_size, u32 *desc_ver) 235 { 236 unsigned long size, mmap_key; 237 efi_status_t status; 238 239 /* 240 * Use the size of the current memory map as an upper bound for the 241 * size of the buffer we need to pass to SetVirtualAddressMap() to 242 * cover all EFI_MEMORY_RUNTIME regions. 243 */ 244 size = 0; 245 status = efi_bs_call(get_memory_map, &size, NULL, &mmap_key, desc_size, 246 desc_ver); 247 if (status != EFI_BUFFER_TOO_SMALL) 248 return EFI_LOAD_ERROR; 249 250 return efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, 251 (void **)virtmap); 252 } 253 254 /* 255 * efi_get_virtmap() - create a virtual mapping for the EFI memory map 256 * 257 * This function populates the virt_addr fields of all memory region descriptors 258 * in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors 259 * are also copied to @runtime_map, and their total count is returned in @count. 260 */ 261 void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size, 262 unsigned long desc_size, efi_memory_desc_t *runtime_map, 263 int *count) 264 { 265 u64 efi_virt_base = virtmap_base; 266 efi_memory_desc_t *in, *out = runtime_map; 267 int l; 268 269 *count = 0; 270 271 for (l = 0; l < map_size; l += desc_size) { 272 u64 paddr, size; 273 274 in = (void *)memory_map + l; 275 if (!(in->attribute & EFI_MEMORY_RUNTIME)) 276 continue; 277 278 paddr = in->phys_addr; 279 size = in->num_pages * EFI_PAGE_SIZE; 280 281 in->virt_addr = in->phys_addr + EFI_RT_VIRTUAL_OFFSET; 282 if (efi_novamap) { 283 continue; 284 } 285 286 /* 287 * Make the mapping compatible with 64k pages: this allows 288 * a 4k page size kernel to kexec a 64k page size kernel and 289 * vice versa. 290 */ 291 if (!flat_va_mapping) { 292 293 paddr = round_down(in->phys_addr, SZ_64K); 294 size += in->phys_addr - paddr; 295 296 /* 297 * Avoid wasting memory on PTEs by choosing a virtual 298 * base that is compatible with section mappings if this 299 * region has the appropriate size and physical 300 * alignment. (Sections are 2 MB on 4k granule kernels) 301 */ 302 if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M) 303 efi_virt_base = round_up(efi_virt_base, SZ_2M); 304 else 305 efi_virt_base = round_up(efi_virt_base, SZ_64K); 306 307 in->virt_addr += efi_virt_base - paddr; 308 efi_virt_base += size; 309 } 310 311 memcpy(out, in, desc_size); 312 out = (void *)out + desc_size; 313 ++*count; 314 } 315 } 316