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 #define EFI_RT_VIRTUAL_SIZE SZ_512M
39
40 #ifdef CONFIG_ARM64
41 # define EFI_RT_VIRTUAL_LIMIT DEFAULT_MAP_WINDOW_64
42 #elif defined(CONFIG_RISCV) || defined(CONFIG_LOONGARCH)
43 # define EFI_RT_VIRTUAL_LIMIT TASK_SIZE_MIN
44 #else
45 # define EFI_RT_VIRTUAL_LIMIT TASK_SIZE
46 #endif
47
48 #ifndef EFI_RT_VIRTUAL_OFFSET
49 #define EFI_RT_VIRTUAL_OFFSET 0
50 #endif
51
52 static u64 virtmap_base = EFI_RT_VIRTUAL_BASE;
53 static bool flat_va_mapping = !!EFI_RT_VIRTUAL_OFFSET;
54
55 const efi_system_table_t *efi_system_table;
56
setup_graphics(void)57 static struct screen_info *setup_graphics(void)
58 {
59 efi_guid_t gop_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
60 efi_status_t status;
61 unsigned long size;
62 void **gop_handle = NULL;
63 struct screen_info *si = NULL;
64
65 size = 0;
66 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
67 &gop_proto, NULL, &size, gop_handle);
68 if (status == EFI_BUFFER_TOO_SMALL) {
69 si = alloc_screen_info();
70 if (!si)
71 return NULL;
72 status = efi_setup_gop(si, &gop_proto, size);
73 if (status != EFI_SUCCESS) {
74 free_screen_info(si);
75 return NULL;
76 }
77 }
78 return si;
79 }
80
install_memreserve_table(void)81 static void install_memreserve_table(void)
82 {
83 struct linux_efi_memreserve *rsv;
84 efi_guid_t memreserve_table_guid = LINUX_EFI_MEMRESERVE_TABLE_GUID;
85 efi_status_t status;
86
87 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, sizeof(*rsv),
88 (void **)&rsv);
89 if (status != EFI_SUCCESS) {
90 efi_err("Failed to allocate memreserve entry!\n");
91 return;
92 }
93
94 rsv->next = 0;
95 rsv->size = 0;
96 atomic_set(&rsv->count, 0);
97
98 status = efi_bs_call(install_configuration_table,
99 &memreserve_table_guid, rsv);
100 if (status != EFI_SUCCESS)
101 efi_err("Failed to install memreserve config table!\n");
102 }
103
get_supported_rt_services(void)104 static u32 get_supported_rt_services(void)
105 {
106 const efi_rt_properties_table_t *rt_prop_table;
107 u32 supported = EFI_RT_SUPPORTED_ALL;
108
109 rt_prop_table = get_efi_config_table(EFI_RT_PROPERTIES_TABLE_GUID);
110 if (rt_prop_table)
111 supported &= rt_prop_table->runtime_services_supported;
112
113 return supported;
114 }
115
116 /*
117 * EFI entry point for the arm/arm64 EFI stubs. This is the entrypoint
118 * that is described in the PE/COFF header. Most of the code is the same
119 * for both archictectures, with the arch-specific code provided in the
120 * handle_kernel_image() function.
121 */
efi_pe_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg)122 efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
123 efi_system_table_t *sys_table_arg)
124 {
125 efi_loaded_image_t *image;
126 efi_status_t status;
127 unsigned long image_addr;
128 unsigned long image_size = 0;
129 /* addr/point and size pairs for memory management*/
130 char *cmdline_ptr = NULL;
131 int cmdline_size = 0;
132 efi_guid_t loaded_image_proto = LOADED_IMAGE_PROTOCOL_GUID;
133 unsigned long reserve_addr = 0;
134 unsigned long reserve_size = 0;
135 struct screen_info *si;
136 efi_properties_table_t *prop_tbl;
137 unsigned long max_addr;
138
139 efi_system_table = sys_table_arg;
140
141 /* Check if we were booted by the EFI firmware */
142 if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
143 status = EFI_INVALID_PARAMETER;
144 goto fail;
145 }
146
147 status = check_platform_features();
148 if (status != EFI_SUCCESS)
149 goto fail;
150
151 /*
152 * Get a handle to the loaded image protocol. This is used to get
153 * information about the running image, such as size and the command
154 * line.
155 */
156 status = efi_system_table->boottime->handle_protocol(handle,
157 &loaded_image_proto, (void *)&image);
158 if (status != EFI_SUCCESS) {
159 efi_err("Failed to get loaded image protocol\n");
160 goto fail;
161 }
162
163 /*
164 * Get the command line from EFI, using the LOADED_IMAGE
165 * protocol. We are going to copy the command line into the
166 * device tree, so this can be allocated anywhere.
167 */
168 cmdline_ptr = efi_convert_cmdline(image, &cmdline_size);
169 if (!cmdline_ptr) {
170 efi_err("getting command line via LOADED_IMAGE_PROTOCOL\n");
171 status = EFI_OUT_OF_RESOURCES;
172 goto fail;
173 }
174
175 if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) ||
176 IS_ENABLED(CONFIG_CMDLINE_FORCE) ||
177 cmdline_size == 0) {
178 status = efi_parse_options(CONFIG_CMDLINE);
179 if (status != EFI_SUCCESS) {
180 efi_err("Failed to parse options\n");
181 goto fail_free_cmdline;
182 }
183 }
184
185 if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && cmdline_size > 0) {
186 status = efi_parse_options(cmdline_ptr);
187 if (status != EFI_SUCCESS) {
188 efi_err("Failed to parse options\n");
189 goto fail_free_cmdline;
190 }
191 }
192
193 efi_info("Booting Linux Kernel...\n");
194
195 si = setup_graphics();
196
197 status = handle_kernel_image(&image_addr, &image_size,
198 &reserve_addr,
199 &reserve_size,
200 image);
201 if (status != EFI_SUCCESS) {
202 efi_err("Failed to relocate kernel\n");
203 goto fail_free_screeninfo;
204 }
205
206 efi_retrieve_tpm2_eventlog();
207
208 /* Ask the firmware to clear memory on unclean shutdown */
209 efi_enable_reset_attack_mitigation();
210
211 if (!efi_noinitrd) {
212 max_addr = efi_get_max_initrd_addr(image_addr);
213 efi_load_initrd(image, ULONG_MAX, efi_get_max_initrd_addr(image_addr),
214 NULL);
215 if (status != EFI_SUCCESS)
216 efi_err("Failed to load initrd!\n");
217 }
218
219 efi_random_get_seed();
220
221 /*
222 * If the NX PE data feature is enabled in the properties table, we
223 * should take care not to create a virtual mapping that changes the
224 * relative placement of runtime services code and data regions, as
225 * they may belong to the same PE/COFF executable image in memory.
226 * The easiest way to achieve that is to simply use a 1:1 mapping.
227 */
228 prop_tbl = get_efi_config_table(EFI_PROPERTIES_TABLE_GUID);
229 flat_va_mapping |= prop_tbl &&
230 (prop_tbl->memory_protection_attribute &
231 EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA);
232
233 /* force efi_novamap if SetVirtualAddressMap() is unsupported */
234 efi_novamap |= !(get_supported_rt_services() &
235 EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP);
236
237 /* hibernation expects the runtime regions to stay in the same place */
238 if (!IS_ENABLED(CONFIG_HIBERNATION) && !efi_nokaslr && !flat_va_mapping) {
239 /*
240 * Randomize the base of the UEFI runtime services region.
241 * Preserve the 2 MB alignment of the region by taking a
242 * shift of 21 bit positions into account when scaling
243 * the headroom value using a 32-bit random value.
244 */
245 static const u64 headroom = EFI_RT_VIRTUAL_LIMIT -
246 EFI_RT_VIRTUAL_BASE -
247 EFI_RT_VIRTUAL_SIZE;
248 u32 rnd;
249
250 status = efi_get_random_bytes(sizeof(rnd), (u8 *)&rnd);
251 if (status == EFI_SUCCESS) {
252 virtmap_base = EFI_RT_VIRTUAL_BASE +
253 (((headroom >> 21) * rnd) >> (32 - 21));
254 }
255 }
256
257 install_memreserve_table();
258
259 status = efi_boot_kernel(handle, image, image_addr, cmdline_ptr);
260
261 efi_free(image_size, image_addr);
262 efi_free(reserve_size, reserve_addr);
263 fail_free_screeninfo:
264 free_screen_info(si);
265 fail_free_cmdline:
266 efi_bs_call(free_pool, cmdline_ptr);
267 fail:
268 return status;
269 }
270
271 /*
272 * efi_allocate_virtmap() - create a pool allocation for the virtmap
273 *
274 * Create an allocation that is of sufficient size to hold all the memory
275 * descriptors that will be passed to SetVirtualAddressMap() to inform the
276 * firmware about the virtual mapping that will be used under the OS to call
277 * into the firmware.
278 */
efi_alloc_virtmap(efi_memory_desc_t **virtmap, unsigned long *desc_size, u32 *desc_ver)279 efi_status_t efi_alloc_virtmap(efi_memory_desc_t **virtmap,
280 unsigned long *desc_size, u32 *desc_ver)
281 {
282 unsigned long size, mmap_key;
283 efi_status_t status;
284
285 /*
286 * Use the size of the current memory map as an upper bound for the
287 * size of the buffer we need to pass to SetVirtualAddressMap() to
288 * cover all EFI_MEMORY_RUNTIME regions.
289 */
290 size = 0;
291 status = efi_bs_call(get_memory_map, &size, NULL, &mmap_key, desc_size,
292 desc_ver);
293 if (status != EFI_BUFFER_TOO_SMALL)
294 return EFI_LOAD_ERROR;
295
296 return efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
297 (void **)virtmap);
298 }
299
300 /*
301 * efi_get_virtmap() - create a virtual mapping for the EFI memory map
302 *
303 * This function populates the virt_addr fields of all memory region descriptors
304 * in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors
305 * are also copied to @runtime_map, and their total count is returned in @count.
306 */
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)307 void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
308 unsigned long desc_size, efi_memory_desc_t *runtime_map,
309 int *count)
310 {
311 u64 efi_virt_base = virtmap_base;
312 efi_memory_desc_t *in, *out = runtime_map;
313 int l;
314
315 *count = 0;
316
317 for (l = 0; l < map_size; l += desc_size) {
318 u64 paddr, size;
319
320 in = (void *)memory_map + l;
321 if (!(in->attribute & EFI_MEMORY_RUNTIME))
322 continue;
323
324 paddr = in->phys_addr;
325 size = in->num_pages * EFI_PAGE_SIZE;
326
327 in->virt_addr = in->phys_addr + EFI_RT_VIRTUAL_OFFSET;
328 if (efi_novamap) {
329 continue;
330 }
331
332 /*
333 * Make the mapping compatible with 64k pages: this allows
334 * a 4k page size kernel to kexec a 64k page size kernel and
335 * vice versa.
336 */
337 if (!flat_va_mapping) {
338
339 paddr = round_down(in->phys_addr, SZ_64K);
340 size += in->phys_addr - paddr;
341
342 /*
343 * Avoid wasting memory on PTEs by choosing a virtual
344 * base that is compatible with section mappings if this
345 * region has the appropriate size and physical
346 * alignment. (Sections are 2 MB on 4k granule kernels)
347 */
348 if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M)
349 efi_virt_base = round_up(efi_virt_base, SZ_2M);
350 else
351 efi_virt_base = round_up(efi_virt_base, SZ_64K);
352
353 in->virt_addr += efi_virt_base - paddr;
354 efi_virt_base += size;
355 }
356
357 memcpy(out, in, desc_size);
358 out = (void *)out + desc_size;
359 ++*count;
360 }
361 }
362