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 void __weak free_screen_info(struct screen_info *si) 51 { 52 } 53 54 static struct screen_info *setup_graphics(void) 55 { 56 efi_guid_t gop_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; 57 efi_status_t status; 58 unsigned long size; 59 void **gop_handle = NULL; 60 struct screen_info *si = NULL; 61 62 size = 0; 63 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, 64 &gop_proto, NULL, &size, gop_handle); 65 if (status == EFI_BUFFER_TOO_SMALL) { 66 si = alloc_screen_info(); 67 if (!si) 68 return NULL; 69 status = efi_setup_gop(si, &gop_proto, size); 70 if (status != EFI_SUCCESS) { 71 free_screen_info(si); 72 return NULL; 73 } 74 } 75 return si; 76 } 77 78 static void install_memreserve_table(void) 79 { 80 struct linux_efi_memreserve *rsv; 81 efi_guid_t memreserve_table_guid = LINUX_EFI_MEMRESERVE_TABLE_GUID; 82 efi_status_t status; 83 84 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, sizeof(*rsv), 85 (void **)&rsv); 86 if (status != EFI_SUCCESS) { 87 efi_err("Failed to allocate memreserve entry!\n"); 88 return; 89 } 90 91 rsv->next = 0; 92 rsv->size = 0; 93 atomic_set(&rsv->count, 0); 94 95 status = efi_bs_call(install_configuration_table, 96 &memreserve_table_guid, rsv); 97 if (status != EFI_SUCCESS) 98 efi_err("Failed to install memreserve config table!\n"); 99 } 100 101 static u32 get_supported_rt_services(void) 102 { 103 const efi_rt_properties_table_t *rt_prop_table; 104 u32 supported = EFI_RT_SUPPORTED_ALL; 105 106 rt_prop_table = get_efi_config_table(EFI_RT_PROPERTIES_TABLE_GUID); 107 if (rt_prop_table) 108 supported &= rt_prop_table->runtime_services_supported; 109 110 return supported; 111 } 112 113 efi_status_t efi_handle_cmdline(efi_loaded_image_t *image, char **cmdline_ptr) 114 { 115 efi_status_t status; 116 char *cmdline; 117 118 /* 119 * Get the command line from EFI, using the LOADED_IMAGE 120 * protocol. We are going to copy the command line into the 121 * device tree, so this can be allocated anywhere. 122 */ 123 cmdline = efi_convert_cmdline(image); 124 if (!cmdline) { 125 efi_err("getting command line via LOADED_IMAGE_PROTOCOL\n"); 126 return EFI_OUT_OF_RESOURCES; 127 } 128 129 if (!IS_ENABLED(CONFIG_CMDLINE_FORCE)) { 130 status = efi_parse_options(cmdline); 131 if (status != EFI_SUCCESS) 132 goto fail_free_cmdline; 133 } 134 135 if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) || 136 IS_ENABLED(CONFIG_CMDLINE_FORCE) || 137 cmdline[0] == 0) { 138 status = efi_parse_options(CONFIG_CMDLINE); 139 if (status != EFI_SUCCESS) 140 goto fail_free_cmdline; 141 } 142 143 *cmdline_ptr = cmdline; 144 return EFI_SUCCESS; 145 146 fail_free_cmdline: 147 efi_err("Failed to parse options\n"); 148 efi_bs_call(free_pool, cmdline); 149 return status; 150 } 151 152 efi_status_t efi_stub_common(efi_handle_t handle, 153 efi_loaded_image_t *image, 154 unsigned long image_addr, 155 char *cmdline_ptr) 156 { 157 struct screen_info *si; 158 efi_status_t status; 159 160 status = check_platform_features(); 161 if (status != EFI_SUCCESS) 162 return status; 163 164 si = setup_graphics(); 165 166 efi_retrieve_eventlog(); 167 168 /* Ask the firmware to clear memory on unclean shutdown */ 169 efi_enable_reset_attack_mitigation(); 170 171 efi_load_initrd(image, ULONG_MAX, efi_get_max_initrd_addr(image_addr), 172 NULL); 173 174 efi_random_get_seed(); 175 176 /* force efi_novamap if SetVirtualAddressMap() is unsupported */ 177 efi_novamap |= !(get_supported_rt_services() & 178 EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP); 179 180 install_memreserve_table(); 181 182 status = efi_boot_kernel(handle, image, image_addr, cmdline_ptr); 183 184 free_screen_info(si); 185 return status; 186 } 187 188 /* 189 * efi_allocate_virtmap() - create a pool allocation for the virtmap 190 * 191 * Create an allocation that is of sufficient size to hold all the memory 192 * descriptors that will be passed to SetVirtualAddressMap() to inform the 193 * firmware about the virtual mapping that will be used under the OS to call 194 * into the firmware. 195 */ 196 efi_status_t efi_alloc_virtmap(efi_memory_desc_t **virtmap, 197 unsigned long *desc_size, u32 *desc_ver) 198 { 199 unsigned long size, mmap_key; 200 efi_status_t status; 201 202 /* 203 * Use the size of the current memory map as an upper bound for the 204 * size of the buffer we need to pass to SetVirtualAddressMap() to 205 * cover all EFI_MEMORY_RUNTIME regions. 206 */ 207 size = 0; 208 status = efi_bs_call(get_memory_map, &size, NULL, &mmap_key, desc_size, 209 desc_ver); 210 if (status != EFI_BUFFER_TOO_SMALL) 211 return EFI_LOAD_ERROR; 212 213 return efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, 214 (void **)virtmap); 215 } 216 217 /* 218 * efi_get_virtmap() - create a virtual mapping for the EFI memory map 219 * 220 * This function populates the virt_addr fields of all memory region descriptors 221 * in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors 222 * are also copied to @runtime_map, and their total count is returned in @count. 223 */ 224 void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size, 225 unsigned long desc_size, efi_memory_desc_t *runtime_map, 226 int *count) 227 { 228 u64 efi_virt_base = virtmap_base; 229 efi_memory_desc_t *in, *out = runtime_map; 230 int l; 231 232 *count = 0; 233 234 for (l = 0; l < map_size; l += desc_size) { 235 u64 paddr, size; 236 237 in = (void *)memory_map + l; 238 if (!(in->attribute & EFI_MEMORY_RUNTIME)) 239 continue; 240 241 paddr = in->phys_addr; 242 size = in->num_pages * EFI_PAGE_SIZE; 243 244 in->virt_addr = in->phys_addr + EFI_RT_VIRTUAL_OFFSET; 245 if (efi_novamap) { 246 continue; 247 } 248 249 /* 250 * Make the mapping compatible with 64k pages: this allows 251 * a 4k page size kernel to kexec a 64k page size kernel and 252 * vice versa. 253 */ 254 if (!flat_va_mapping) { 255 256 paddr = round_down(in->phys_addr, SZ_64K); 257 size += in->phys_addr - paddr; 258 259 /* 260 * Avoid wasting memory on PTEs by choosing a virtual 261 * base that is compatible with section mappings if this 262 * region has the appropriate size and physical 263 * alignment. (Sections are 2 MB on 4k granule kernels) 264 */ 265 if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M) 266 efi_virt_base = round_up(efi_virt_base, SZ_2M); 267 else 268 efi_virt_base = round_up(efi_virt_base, SZ_64K); 269 270 in->virt_addr += efi_virt_base - paddr; 271 efi_virt_base += size; 272 } 273 274 memcpy(out, in, desc_size); 275 out = (void *)out + desc_size; 276 ++*count; 277 } 278 } 279