]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/firmware/efi/libstub/arm-stub.c
Merge tag 'omap-for-v5.0/fixes-rc7-signed' of git://git.kernel.org/pub/scm/linux...
[mirror_ubuntu-eoan-kernel.git] / drivers / firmware / efi / libstub / arm-stub.c
CommitLineData
3c7f2550
MS
1/*
2 * EFI stub implementation that is shared by arm and arm64 architectures.
3 * This should be #included by the EFI stub implementation files.
4 *
5 * Copyright (C) 2013,2014 Linaro Limited
6 * Roy Franz <roy.franz@linaro.org
7 * Copyright (C) 2013 Red Hat, Inc.
8 * Mark Salter <msalter@redhat.com>
9 *
10 * This file is part of the Linux kernel, and is made available under the
11 * terms of the GNU General Public License version 2.
12 *
13 */
14
bd669475 15#include <linux/efi.h>
0ce3cc00 16#include <linux/sort.h>
bd669475
AB
17#include <asm/efi.h>
18
19#include "efistub.h"
20
e69176d6
AB
21/*
22 * This is the base address at which to start allocating virtual memory ranges
23 * for UEFI Runtime Services. This is in the low TTBR0 range so that we can use
24 * any allocation we choose, and eliminate the risk of a conflict after kexec.
25 * The value chosen is the largest non-zero power of 2 suitable for this purpose
26 * both on 32-bit and 64-bit ARM CPUs, to maximize the likelihood that it can
27 * be mapped efficiently.
28 * Since 32-bit ARM could potentially execute with a 1G/3G user/kernel split,
29 * map everything below 1 GB. (512 MB is a reasonable upper bound for the
30 * entire footprint of the UEFI runtime services memory regions)
31 */
32#define EFI_RT_VIRTUAL_BASE SZ_512M
33#define EFI_RT_VIRTUAL_SIZE SZ_512M
34
197decef 35#ifdef CONFIG_ARM64
363524d2 36# define EFI_RT_VIRTUAL_LIMIT DEFAULT_MAP_WINDOW_64
197decef
AB
37#else
38# define EFI_RT_VIRTUAL_LIMIT TASK_SIZE
39#endif
40
e69176d6
AB
41static u64 virtmap_base = EFI_RT_VIRTUAL_BASE;
42
bd669475 43void efi_char16_printk(efi_system_table_t *sys_table_arg,
3c7f2550
MS
44 efi_char16_t *str)
45{
46 struct efi_simple_text_output_protocol *out;
47
48 out = (struct efi_simple_text_output_protocol *)sys_table_arg->con_out;
49 out->output_string(out, str);
50}
51
f0827e18
AB
52static struct screen_info *setup_graphics(efi_system_table_t *sys_table_arg)
53{
54 efi_guid_t gop_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
55 efi_status_t status;
56 unsigned long size;
57 void **gop_handle = NULL;
58 struct screen_info *si = NULL;
59
60 size = 0;
61 status = efi_call_early(locate_handle, EFI_LOCATE_BY_PROTOCOL,
62 &gop_proto, NULL, &size, gop_handle);
63 if (status == EFI_BUFFER_TOO_SMALL) {
64 si = alloc_screen_info(sys_table_arg);
65 if (!si)
66 return NULL;
67 efi_setup_gop(sys_table_arg, si, &gop_proto, size);
68 }
69 return si;
70}
3c7f2550 71
b844470f
AB
72void install_memreserve_table(efi_system_table_t *sys_table_arg)
73{
74 struct linux_efi_memreserve *rsv;
75 efi_guid_t memreserve_table_guid = LINUX_EFI_MEMRESERVE_TABLE_GUID;
76 efi_status_t status;
77
78 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, sizeof(*rsv),
79 (void **)&rsv);
80 if (status != EFI_SUCCESS) {
81 pr_efi_err(sys_table_arg, "Failed to allocate memreserve entry!\n");
82 return;
83 }
84
85 rsv->next = 0;
b844470f 86 rsv->size = 0;
5f0b0ecf 87 atomic_set(&rsv->count, 0);
b844470f
AB
88
89 status = efi_call_early(install_configuration_table,
90 &memreserve_table_guid,
91 rsv);
92 if (status != EFI_SUCCESS)
93 pr_efi_err(sys_table_arg, "Failed to install memreserve config table!\n");
94}
95
96
3c7f2550
MS
97/*
98 * This function handles the architcture specific differences between arm and
99 * arm64 regarding where the kernel image must be loaded and any memory that
100 * must be reserved. On failure it is required to free all
101 * all allocations it has made.
102 */
bd669475
AB
103efi_status_t handle_kernel_image(efi_system_table_t *sys_table,
104 unsigned long *image_addr,
105 unsigned long *image_size,
106 unsigned long *reserve_addr,
107 unsigned long *reserve_size,
108 unsigned long dram_base,
109 efi_loaded_image_t *image);
3c7f2550
MS
110/*
111 * EFI entry point for the arm/arm64 EFI stubs. This is the entrypoint
112 * that is described in the PE/COFF header. Most of the code is the same
113 * for both archictectures, with the arch-specific code provided in the
114 * handle_kernel_image() function.
115 */
ddeeefe2 116unsigned long efi_entry(void *handle, efi_system_table_t *sys_table,
3c7f2550
MS
117 unsigned long *image_addr)
118{
119 efi_loaded_image_t *image;
120 efi_status_t status;
121 unsigned long image_size = 0;
122 unsigned long dram_base;
123 /* addr/point and size pairs for memory management*/
124 unsigned long initrd_addr;
125 u64 initrd_size = 0;
345c736e 126 unsigned long fdt_addr = 0; /* Original DTB */
a643375f 127 unsigned long fdt_size = 0;
3c7f2550
MS
128 char *cmdline_ptr = NULL;
129 int cmdline_size = 0;
130 unsigned long new_fdt_addr;
131 efi_guid_t loaded_image_proto = LOADED_IMAGE_PROTOCOL_GUID;
132 unsigned long reserve_addr = 0;
133 unsigned long reserve_size = 0;
de8cb458 134 enum efi_secureboot_mode secure_boot;
f0827e18 135 struct screen_info *si;
3c7f2550
MS
136
137 /* Check if we were booted by the EFI firmware */
138 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
139 goto fail;
140
b9d6769b
AB
141 status = check_platform_features(sys_table);
142 if (status != EFI_SUCCESS)
143 goto fail;
144
3c7f2550
MS
145 /*
146 * Get a handle to the loaded image protocol. This is used to get
147 * information about the running image, such as size and the command
148 * line.
149 */
150 status = sys_table->boottime->handle_protocol(handle,
151 &loaded_image_proto, (void *)&image);
152 if (status != EFI_SUCCESS) {
153 pr_efi_err(sys_table, "Failed to get loaded image protocol\n");
154 goto fail;
155 }
156
157 dram_base = get_dram_base(sys_table);
158 if (dram_base == EFI_ERROR) {
159 pr_efi_err(sys_table, "Failed to find DRAM base\n");
160 goto fail;
161 }
3c7f2550
MS
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(sys_table, image, &cmdline_size);
169 if (!cmdline_ptr) {
170 pr_efi_err(sys_table, "getting command line via LOADED_IMAGE_PROTOCOL\n");
2b5fe07a
AB
171 goto fail;
172 }
173
eeff7d63
AB
174 if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) ||
175 IS_ENABLED(CONFIG_CMDLINE_FORCE) ||
176 cmdline_size == 0)
177 efi_parse_options(CONFIG_CMDLINE);
178
179 if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && cmdline_size > 0)
180 efi_parse_options(cmdline_ptr);
181
182 pr_efi(sys_table, "Booting Linux Kernel...\n");
183
f0827e18
AB
184 si = setup_graphics(sys_table);
185
2b5fe07a
AB
186 status = handle_kernel_image(sys_table, image_addr, &image_size,
187 &reserve_addr,
188 &reserve_size,
189 dram_base, image);
190 if (status != EFI_SUCCESS) {
191 pr_efi_err(sys_table, "Failed to relocate kernel\n");
192 goto fail_free_cmdline;
3c7f2550
MS
193 }
194
ccc829ba
MG
195 /* Ask the firmware to clear memory on unclean shutdown */
196 efi_enable_reset_attack_mitigation(sys_table);
197
73a64925 198 secure_boot = efi_get_secureboot(sys_table);
73a64925 199
345c736e 200 /*
de8cb458
DH
201 * Unauthenticated device tree data is a security hazard, so ignore
202 * 'dtb=' unless UEFI Secure Boot is disabled. We assume that secure
203 * boot is enabled if we can't determine its state.
345c736e 204 */
3d7ee348
AB
205 if (!IS_ENABLED(CONFIG_EFI_ARMSTUB_DTB_LOADER) ||
206 secure_boot != efi_secureboot_mode_disabled) {
207 if (strstr(cmdline_ptr, "dtb="))
208 pr_efi(sys_table, "Ignoring DTB from command line.\n");
345c736e 209 } else {
3c7f2550
MS
210 status = handle_cmdline_files(sys_table, image, cmdline_ptr,
211 "dtb=",
a643375f 212 ~0UL, &fdt_addr, &fdt_size);
3c7f2550
MS
213
214 if (status != EFI_SUCCESS) {
215 pr_efi_err(sys_table, "Failed to load device tree!\n");
2b5fe07a 216 goto fail_free_image;
3c7f2550
MS
217 }
218 }
0bcaa904
MR
219
220 if (fdt_addr) {
221 pr_efi(sys_table, "Using DTB from command line\n");
222 } else {
345c736e 223 /* Look for a device tree configuration table entry. */
a643375f 224 fdt_addr = (uintptr_t)get_fdt(sys_table, &fdt_size);
0bcaa904
MR
225 if (fdt_addr)
226 pr_efi(sys_table, "Using DTB from configuration table\n");
227 }
228
229 if (!fdt_addr)
230 pr_efi(sys_table, "Generating empty DTB\n");
3c7f2550 231
138728dd
AB
232 status = handle_cmdline_files(sys_table, image, cmdline_ptr, "initrd=",
233 efi_get_max_initrd_addr(dram_base,
234 *image_addr),
3c7f2550
MS
235 (unsigned long *)&initrd_addr,
236 (unsigned long *)&initrd_size);
237 if (status != EFI_SUCCESS)
238 pr_efi_err(sys_table, "Failed initrd from command line!\n");
239
568bc4e8
AB
240 efi_random_get_seed(sys_table);
241
38fb6652
AB
242 /* hibernation expects the runtime regions to stay in the same place */
243 if (!IS_ENABLED(CONFIG_HIBERNATION) && !nokaslr()) {
e69176d6
AB
244 /*
245 * Randomize the base of the UEFI runtime services region.
246 * Preserve the 2 MB alignment of the region by taking a
247 * shift of 21 bit positions into account when scaling
248 * the headroom value using a 32-bit random value.
249 */
197decef
AB
250 static const u64 headroom = EFI_RT_VIRTUAL_LIMIT -
251 EFI_RT_VIRTUAL_BASE -
252 EFI_RT_VIRTUAL_SIZE;
e69176d6
AB
253 u32 rnd;
254
255 status = efi_get_random_bytes(sys_table, sizeof(rnd),
256 (u8 *)&rnd);
257 if (status == EFI_SUCCESS) {
258 virtmap_base = EFI_RT_VIRTUAL_BASE +
259 (((headroom >> 21) * rnd) >> (32 - 21));
260 }
261 }
262
b844470f
AB
263 install_memreserve_table(sys_table);
264
3c7f2550
MS
265 new_fdt_addr = fdt_addr;
266 status = allocate_new_fdt_and_exit_boot(sys_table, handle,
138728dd 267 &new_fdt_addr, efi_get_max_fdt_addr(dram_base),
3c7f2550
MS
268 initrd_addr, initrd_size, cmdline_ptr,
269 fdt_addr, fdt_size);
270
271 /*
272 * If all went well, we need to return the FDT address to the
273 * calling function so it can be passed to kernel as part of
274 * the kernel boot protocol.
275 */
276 if (status == EFI_SUCCESS)
277 return new_fdt_addr;
278
279 pr_efi_err(sys_table, "Failed to update FDT and exit boot services\n");
280
281 efi_free(sys_table, initrd_size, initrd_addr);
282 efi_free(sys_table, fdt_size, fdt_addr);
283
3c7f2550
MS
284fail_free_image:
285 efi_free(sys_table, image_size, *image_addr);
286 efi_free(sys_table, reserve_size, reserve_addr);
2b5fe07a 287fail_free_cmdline:
f0827e18 288 free_screen_info(sys_table, si);
2b5fe07a 289 efi_free(sys_table, cmdline_size, (unsigned long)cmdline_ptr);
3c7f2550
MS
290fail:
291 return EFI_ERROR;
292}
f3cdfd23 293
0ce3cc00
AB
294static int cmp_mem_desc(const void *l, const void *r)
295{
296 const efi_memory_desc_t *left = l, *right = r;
297
298 return (left->phys_addr > right->phys_addr) ? 1 : -1;
299}
300
301/*
302 * Returns whether region @left ends exactly where region @right starts,
303 * or false if either argument is NULL.
304 */
305static bool regions_are_adjacent(efi_memory_desc_t *left,
306 efi_memory_desc_t *right)
307{
308 u64 left_end;
309
310 if (left == NULL || right == NULL)
311 return false;
312
313 left_end = left->phys_addr + left->num_pages * EFI_PAGE_SIZE;
314
315 return left_end == right->phys_addr;
316}
317
318/*
319 * Returns whether region @left and region @right have compatible memory type
320 * mapping attributes, and are both EFI_MEMORY_RUNTIME regions.
321 */
322static bool regions_have_compatible_memory_type_attrs(efi_memory_desc_t *left,
323 efi_memory_desc_t *right)
324{
325 static const u64 mem_type_mask = EFI_MEMORY_WB | EFI_MEMORY_WT |
326 EFI_MEMORY_WC | EFI_MEMORY_UC |
327 EFI_MEMORY_RUNTIME;
328
329 return ((left->attribute ^ right->attribute) & mem_type_mask) == 0;
330}
331
f3cdfd23
AB
332/*
333 * efi_get_virtmap() - create a virtual mapping for the EFI memory map
334 *
335 * This function populates the virt_addr fields of all memory region descriptors
336 * in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors
337 * are also copied to @runtime_map, and their total count is returned in @count.
338 */
339void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
340 unsigned long desc_size, efi_memory_desc_t *runtime_map,
341 int *count)
342{
e69176d6 343 u64 efi_virt_base = virtmap_base;
0ce3cc00 344 efi_memory_desc_t *in, *prev = NULL, *out = runtime_map;
f3cdfd23
AB
345 int l;
346
0ce3cc00
AB
347 /*
348 * To work around potential issues with the Properties Table feature
349 * introduced in UEFI 2.5, which may split PE/COFF executable images
350 * in memory into several RuntimeServicesCode and RuntimeServicesData
351 * regions, we need to preserve the relative offsets between adjacent
352 * EFI_MEMORY_RUNTIME regions with the same memory type attributes.
353 * The easiest way to find adjacent regions is to sort the memory map
354 * before traversing it.
355 */
29f9007b
AB
356 if (IS_ENABLED(CONFIG_ARM64))
357 sort(memory_map, map_size / desc_size, desc_size, cmp_mem_desc,
358 NULL);
0ce3cc00
AB
359
360 for (l = 0; l < map_size; l += desc_size, prev = in) {
f3cdfd23
AB
361 u64 paddr, size;
362
0ce3cc00 363 in = (void *)memory_map + l;
f3cdfd23
AB
364 if (!(in->attribute & EFI_MEMORY_RUNTIME))
365 continue;
366
0ce3cc00
AB
367 paddr = in->phys_addr;
368 size = in->num_pages * EFI_PAGE_SIZE;
369
f3cdfd23
AB
370 /*
371 * Make the mapping compatible with 64k pages: this allows
372 * a 4k page size kernel to kexec a 64k page size kernel and
373 * vice versa.
374 */
29f9007b
AB
375 if ((IS_ENABLED(CONFIG_ARM64) &&
376 !regions_are_adjacent(prev, in)) ||
0ce3cc00
AB
377 !regions_have_compatible_memory_type_attrs(prev, in)) {
378
379 paddr = round_down(in->phys_addr, SZ_64K);
380 size += in->phys_addr - paddr;
381
382 /*
383 * Avoid wasting memory on PTEs by choosing a virtual
384 * base that is compatible with section mappings if this
385 * region has the appropriate size and physical
386 * alignment. (Sections are 2 MB on 4k granule kernels)
387 */
388 if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M)
389 efi_virt_base = round_up(efi_virt_base, SZ_2M);
390 else
391 efi_virt_base = round_up(efi_virt_base, SZ_64K);
392 }
f3cdfd23
AB
393
394 in->virt_addr = efi_virt_base + in->phys_addr - paddr;
395 efi_virt_base += size;
396
397 memcpy(out, in, desc_size);
398 out = (void *)out + desc_size;
399 ++*count;
400 }
401}