]> Git Repo - linux.git/blame - drivers/firmware/efi/libstub/arm-stub.c
Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux.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 <[email protected]
7 * Copyright (C) 2013 Red Hat, Inc.
8 * Mark Salter <[email protected]>
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
AB
35#ifdef CONFIG_ARM64
36# define EFI_RT_VIRTUAL_LIMIT TASK_SIZE_64
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
eff89628
AB
78 if (IS_ENABLED(CONFIG_ARM))
79 return;
80
b844470f
AB
81 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, sizeof(*rsv),
82 (void **)&rsv);
83 if (status != EFI_SUCCESS) {
84 pr_efi_err(sys_table_arg, "Failed to allocate memreserve entry!\n");
85 return;
86 }
87
88 rsv->next = 0;
89 rsv->base = 0;
90 rsv->size = 0;
91
92 status = efi_call_early(install_configuration_table,
93 &memreserve_table_guid,
94 rsv);
95 if (status != EFI_SUCCESS)
96 pr_efi_err(sys_table_arg, "Failed to install memreserve config table!\n");
97}
98
99
3c7f2550
MS
100/*
101 * This function handles the architcture specific differences between arm and
102 * arm64 regarding where the kernel image must be loaded and any memory that
103 * must be reserved. On failure it is required to free all
104 * all allocations it has made.
105 */
bd669475
AB
106efi_status_t handle_kernel_image(efi_system_table_t *sys_table,
107 unsigned long *image_addr,
108 unsigned long *image_size,
109 unsigned long *reserve_addr,
110 unsigned long *reserve_size,
111 unsigned long dram_base,
112 efi_loaded_image_t *image);
3c7f2550
MS
113/*
114 * EFI entry point for the arm/arm64 EFI stubs. This is the entrypoint
115 * that is described in the PE/COFF header. Most of the code is the same
116 * for both archictectures, with the arch-specific code provided in the
117 * handle_kernel_image() function.
118 */
ddeeefe2 119unsigned long efi_entry(void *handle, efi_system_table_t *sys_table,
3c7f2550
MS
120 unsigned long *image_addr)
121{
122 efi_loaded_image_t *image;
123 efi_status_t status;
124 unsigned long image_size = 0;
125 unsigned long dram_base;
126 /* addr/point and size pairs for memory management*/
127 unsigned long initrd_addr;
128 u64 initrd_size = 0;
345c736e 129 unsigned long fdt_addr = 0; /* Original DTB */
a643375f 130 unsigned long fdt_size = 0;
3c7f2550
MS
131 char *cmdline_ptr = NULL;
132 int cmdline_size = 0;
133 unsigned long new_fdt_addr;
134 efi_guid_t loaded_image_proto = LOADED_IMAGE_PROTOCOL_GUID;
135 unsigned long reserve_addr = 0;
136 unsigned long reserve_size = 0;
de8cb458 137 enum efi_secureboot_mode secure_boot;
f0827e18 138 struct screen_info *si;
3c7f2550
MS
139
140 /* Check if we were booted by the EFI firmware */
141 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
142 goto fail;
143
b9d6769b
AB
144 status = check_platform_features(sys_table);
145 if (status != EFI_SUCCESS)
146 goto fail;
147
3c7f2550
MS
148 /*
149 * Get a handle to the loaded image protocol. This is used to get
150 * information about the running image, such as size and the command
151 * line.
152 */
153 status = sys_table->boottime->handle_protocol(handle,
154 &loaded_image_proto, (void *)&image);
155 if (status != EFI_SUCCESS) {
156 pr_efi_err(sys_table, "Failed to get loaded image protocol\n");
157 goto fail;
158 }
159
160 dram_base = get_dram_base(sys_table);
161 if (dram_base == EFI_ERROR) {
162 pr_efi_err(sys_table, "Failed to find DRAM base\n");
163 goto fail;
164 }
3c7f2550
MS
165
166 /*
167 * Get the command line from EFI, using the LOADED_IMAGE
168 * protocol. We are going to copy the command line into the
169 * device tree, so this can be allocated anywhere.
170 */
171 cmdline_ptr = efi_convert_cmdline(sys_table, image, &cmdline_size);
172 if (!cmdline_ptr) {
173 pr_efi_err(sys_table, "getting command line via LOADED_IMAGE_PROTOCOL\n");
2b5fe07a
AB
174 goto fail;
175 }
176
eeff7d63
AB
177 if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) ||
178 IS_ENABLED(CONFIG_CMDLINE_FORCE) ||
179 cmdline_size == 0)
180 efi_parse_options(CONFIG_CMDLINE);
181
182 if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && cmdline_size > 0)
183 efi_parse_options(cmdline_ptr);
184
185 pr_efi(sys_table, "Booting Linux Kernel...\n");
186
f0827e18
AB
187 si = setup_graphics(sys_table);
188
2b5fe07a
AB
189 status = handle_kernel_image(sys_table, image_addr, &image_size,
190 &reserve_addr,
191 &reserve_size,
192 dram_base, image);
193 if (status != EFI_SUCCESS) {
194 pr_efi_err(sys_table, "Failed to relocate kernel\n");
195 goto fail_free_cmdline;
3c7f2550
MS
196 }
197
ccc829ba
MG
198 /* Ask the firmware to clear memory on unclean shutdown */
199 efi_enable_reset_attack_mitigation(sys_table);
200
73a64925 201 secure_boot = efi_get_secureboot(sys_table);
73a64925 202
345c736e 203 /*
de8cb458
DH
204 * Unauthenticated device tree data is a security hazard, so ignore
205 * 'dtb=' unless UEFI Secure Boot is disabled. We assume that secure
206 * boot is enabled if we can't determine its state.
345c736e 207 */
3d7ee348
AB
208 if (!IS_ENABLED(CONFIG_EFI_ARMSTUB_DTB_LOADER) ||
209 secure_boot != efi_secureboot_mode_disabled) {
210 if (strstr(cmdline_ptr, "dtb="))
211 pr_efi(sys_table, "Ignoring DTB from command line.\n");
345c736e 212 } else {
3c7f2550
MS
213 status = handle_cmdline_files(sys_table, image, cmdline_ptr,
214 "dtb=",
a643375f 215 ~0UL, &fdt_addr, &fdt_size);
3c7f2550
MS
216
217 if (status != EFI_SUCCESS) {
218 pr_efi_err(sys_table, "Failed to load device tree!\n");
2b5fe07a 219 goto fail_free_image;
3c7f2550
MS
220 }
221 }
0bcaa904
MR
222
223 if (fdt_addr) {
224 pr_efi(sys_table, "Using DTB from command line\n");
225 } else {
345c736e 226 /* Look for a device tree configuration table entry. */
a643375f 227 fdt_addr = (uintptr_t)get_fdt(sys_table, &fdt_size);
0bcaa904
MR
228 if (fdt_addr)
229 pr_efi(sys_table, "Using DTB from configuration table\n");
230 }
231
232 if (!fdt_addr)
233 pr_efi(sys_table, "Generating empty DTB\n");
3c7f2550 234
138728dd
AB
235 status = handle_cmdline_files(sys_table, image, cmdline_ptr, "initrd=",
236 efi_get_max_initrd_addr(dram_base,
237 *image_addr),
3c7f2550
MS
238 (unsigned long *)&initrd_addr,
239 (unsigned long *)&initrd_size);
240 if (status != EFI_SUCCESS)
241 pr_efi_err(sys_table, "Failed initrd from command line!\n");
242
568bc4e8
AB
243 efi_random_get_seed(sys_table);
244
38fb6652
AB
245 /* hibernation expects the runtime regions to stay in the same place */
246 if (!IS_ENABLED(CONFIG_HIBERNATION) && !nokaslr()) {
e69176d6
AB
247 /*
248 * Randomize the base of the UEFI runtime services region.
249 * Preserve the 2 MB alignment of the region by taking a
250 * shift of 21 bit positions into account when scaling
251 * the headroom value using a 32-bit random value.
252 */
197decef
AB
253 static const u64 headroom = EFI_RT_VIRTUAL_LIMIT -
254 EFI_RT_VIRTUAL_BASE -
255 EFI_RT_VIRTUAL_SIZE;
e69176d6
AB
256 u32 rnd;
257
258 status = efi_get_random_bytes(sys_table, sizeof(rnd),
259 (u8 *)&rnd);
260 if (status == EFI_SUCCESS) {
261 virtmap_base = EFI_RT_VIRTUAL_BASE +
262 (((headroom >> 21) * rnd) >> (32 - 21));
263 }
264 }
265
b844470f
AB
266 install_memreserve_table(sys_table);
267
3c7f2550
MS
268 new_fdt_addr = fdt_addr;
269 status = allocate_new_fdt_and_exit_boot(sys_table, handle,
138728dd 270 &new_fdt_addr, efi_get_max_fdt_addr(dram_base),
3c7f2550
MS
271 initrd_addr, initrd_size, cmdline_ptr,
272 fdt_addr, fdt_size);
273
274 /*
275 * If all went well, we need to return the FDT address to the
276 * calling function so it can be passed to kernel as part of
277 * the kernel boot protocol.
278 */
279 if (status == EFI_SUCCESS)
280 return new_fdt_addr;
281
282 pr_efi_err(sys_table, "Failed to update FDT and exit boot services\n");
283
284 efi_free(sys_table, initrd_size, initrd_addr);
285 efi_free(sys_table, fdt_size, fdt_addr);
286
3c7f2550
MS
287fail_free_image:
288 efi_free(sys_table, image_size, *image_addr);
289 efi_free(sys_table, reserve_size, reserve_addr);
2b5fe07a 290fail_free_cmdline:
f0827e18 291 free_screen_info(sys_table, si);
2b5fe07a 292 efi_free(sys_table, cmdline_size, (unsigned long)cmdline_ptr);
3c7f2550
MS
293fail:
294 return EFI_ERROR;
295}
f3cdfd23 296
0ce3cc00
AB
297static int cmp_mem_desc(const void *l, const void *r)
298{
299 const efi_memory_desc_t *left = l, *right = r;
300
301 return (left->phys_addr > right->phys_addr) ? 1 : -1;
302}
303
304/*
305 * Returns whether region @left ends exactly where region @right starts,
306 * or false if either argument is NULL.
307 */
308static bool regions_are_adjacent(efi_memory_desc_t *left,
309 efi_memory_desc_t *right)
310{
311 u64 left_end;
312
313 if (left == NULL || right == NULL)
314 return false;
315
316 left_end = left->phys_addr + left->num_pages * EFI_PAGE_SIZE;
317
318 return left_end == right->phys_addr;
319}
320
321/*
322 * Returns whether region @left and region @right have compatible memory type
323 * mapping attributes, and are both EFI_MEMORY_RUNTIME regions.
324 */
325static bool regions_have_compatible_memory_type_attrs(efi_memory_desc_t *left,
326 efi_memory_desc_t *right)
327{
328 static const u64 mem_type_mask = EFI_MEMORY_WB | EFI_MEMORY_WT |
329 EFI_MEMORY_WC | EFI_MEMORY_UC |
330 EFI_MEMORY_RUNTIME;
331
332 return ((left->attribute ^ right->attribute) & mem_type_mask) == 0;
333}
334
f3cdfd23
AB
335/*
336 * efi_get_virtmap() - create a virtual mapping for the EFI memory map
337 *
338 * This function populates the virt_addr fields of all memory region descriptors
339 * in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors
340 * are also copied to @runtime_map, and their total count is returned in @count.
341 */
342void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
343 unsigned long desc_size, efi_memory_desc_t *runtime_map,
344 int *count)
345{
e69176d6 346 u64 efi_virt_base = virtmap_base;
0ce3cc00 347 efi_memory_desc_t *in, *prev = NULL, *out = runtime_map;
f3cdfd23
AB
348 int l;
349
0ce3cc00
AB
350 /*
351 * To work around potential issues with the Properties Table feature
352 * introduced in UEFI 2.5, which may split PE/COFF executable images
353 * in memory into several RuntimeServicesCode and RuntimeServicesData
354 * regions, we need to preserve the relative offsets between adjacent
355 * EFI_MEMORY_RUNTIME regions with the same memory type attributes.
356 * The easiest way to find adjacent regions is to sort the memory map
357 * before traversing it.
358 */
29f9007b
AB
359 if (IS_ENABLED(CONFIG_ARM64))
360 sort(memory_map, map_size / desc_size, desc_size, cmp_mem_desc,
361 NULL);
0ce3cc00
AB
362
363 for (l = 0; l < map_size; l += desc_size, prev = in) {
f3cdfd23
AB
364 u64 paddr, size;
365
0ce3cc00 366 in = (void *)memory_map + l;
f3cdfd23
AB
367 if (!(in->attribute & EFI_MEMORY_RUNTIME))
368 continue;
369
0ce3cc00
AB
370 paddr = in->phys_addr;
371 size = in->num_pages * EFI_PAGE_SIZE;
372
f3cdfd23
AB
373 /*
374 * Make the mapping compatible with 64k pages: this allows
375 * a 4k page size kernel to kexec a 64k page size kernel and
376 * vice versa.
377 */
29f9007b
AB
378 if ((IS_ENABLED(CONFIG_ARM64) &&
379 !regions_are_adjacent(prev, in)) ||
0ce3cc00
AB
380 !regions_have_compatible_memory_type_attrs(prev, in)) {
381
382 paddr = round_down(in->phys_addr, SZ_64K);
383 size += in->phys_addr - paddr;
384
385 /*
386 * Avoid wasting memory on PTEs by choosing a virtual
387 * base that is compatible with section mappings if this
388 * region has the appropriate size and physical
389 * alignment. (Sections are 2 MB on 4k granule kernels)
390 */
391 if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M)
392 efi_virt_base = round_up(efi_virt_base, SZ_2M);
393 else
394 efi_virt_base = round_up(efi_virt_base, SZ_64K);
395 }
f3cdfd23
AB
396
397 in->virt_addr = efi_virt_base + in->phys_addr - paddr;
398 efi_virt_base += size;
399
400 memcpy(out, in, desc_size);
401 out = (void *)out + desc_size;
402 ++*count;
403 }
404}
This page took 0.324371 seconds and 4 git commands to generate.