blob: 3d36142cf81208d408cab75e28edf957fa865c9e [file] [log] [blame]
Mark Salter3c7f2552014-04-15 22:47:52 -04001/*
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
Ard Biesheuvelbd669472014-07-02 14:54:42 +020015#include <linux/efi.h>
Ard Biesheuvel0ce3cc02015-09-25 23:02:19 +010016#include <linux/sort.h>
Ard Biesheuvelbd669472014-07-02 14:54:42 +020017#include <asm/efi.h>
18
19#include "efistub.h"
20
Ard Biesheuvele69176d2017-04-04 17:09:10 +010021/*
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
Ard Biesheuvel197dece2017-04-17 10:32:01 +010035#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
Ard Biesheuvele69176d2017-04-04 17:09:10 +010041static u64 virtmap_base = EFI_RT_VIRTUAL_BASE;
42
Ard Biesheuvelbd669472014-07-02 14:54:42 +020043void efi_char16_printk(efi_system_table_t *sys_table_arg,
Mark Salter3c7f2552014-04-15 22:47:52 -040044 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
Ard Biesheuvelf0827e12016-04-25 21:06:54 +010052static 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}
Mark Salter3c7f2552014-04-15 22:47:52 -040071
Ard Biesheuvelb8444702018-09-21 09:32:45 -070072void 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
Ard Biesheuveleff89622018-11-14 09:55:43 -080078 if (IS_ENABLED(CONFIG_ARM))
79 return;
80
Ard Biesheuvelb8444702018-09-21 09:32:45 -070081 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
Mark Salter3c7f2552014-04-15 22:47:52 -0400100/*
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 */
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200106efi_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);
Mark Salter3c7f2552014-04-15 22:47:52 -0400113/*
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 */
Ard Biesheuvelddeeefe2015-01-12 20:28:20 +0000119unsigned long efi_entry(void *handle, efi_system_table_t *sys_table,
Mark Salter3c7f2552014-04-15 22:47:52 -0400120 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;
Ard Biesheuvel345c7362014-04-03 17:46:58 +0200129 unsigned long fdt_addr = 0; /* Original DTB */
Ard Biesheuvela6433752015-03-04 13:02:29 +0100130 unsigned long fdt_size = 0;
Mark Salter3c7f2552014-04-15 22:47:52 -0400131 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;
David Howellsde8cb452017-02-06 11:22:43 +0000137 enum efi_secureboot_mode secure_boot;
Ard Biesheuvelf0827e12016-04-25 21:06:54 +0100138 struct screen_info *si;
Mark Salter3c7f2552014-04-15 22:47:52 -0400139
140 /* Check if we were booted by the EFI firmware */
141 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
142 goto fail;
143
Ard Biesheuvelb9d6769b2016-02-17 12:36:03 +0000144 status = check_platform_features(sys_table);
145 if (status != EFI_SUCCESS)
146 goto fail;
147
Mark Salter3c7f2552014-04-15 22:47:52 -0400148 /*
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 }
Mark Salter3c7f2552014-04-15 22:47:52 -0400165
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");
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +0100174 goto fail;
175 }
176
Ard Biesheuveleeff7d62017-04-04 17:09:09 +0100177 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
Ard Biesheuvelf0827e12016-04-25 21:06:54 +0100187 si = setup_graphics(sys_table);
188
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +0100189 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;
Mark Salter3c7f2552014-04-15 22:47:52 -0400196 }
197
Matthew Garrettccc829b2017-08-25 16:50:15 +0100198 /* Ask the firmware to clear memory on unclean shutdown */
199 efi_enable_reset_attack_mitigation(sys_table);
200
Linn Crosetto73a649252016-04-25 21:06:36 +0100201 secure_boot = efi_get_secureboot(sys_table);
Linn Crosetto73a649252016-04-25 21:06:36 +0100202
Ard Biesheuvel345c7362014-04-03 17:46:58 +0200203 /*
David Howellsde8cb452017-02-06 11:22:43 +0000204 * 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.
Ard Biesheuvel345c7362014-04-03 17:46:58 +0200207 */
Ard Biesheuvel3d7ee342018-07-11 11:40:38 +0200208 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");
Ard Biesheuvel345c7362014-04-03 17:46:58 +0200212 } else {
Mark Salter3c7f2552014-04-15 22:47:52 -0400213 status = handle_cmdline_files(sys_table, image, cmdline_ptr,
214 "dtb=",
Ard Biesheuvela6433752015-03-04 13:02:29 +0100215 ~0UL, &fdt_addr, &fdt_size);
Mark Salter3c7f2552014-04-15 22:47:52 -0400216
217 if (status != EFI_SUCCESS) {
218 pr_efi_err(sys_table, "Failed to load device tree!\n");
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +0100219 goto fail_free_image;
Mark Salter3c7f2552014-04-15 22:47:52 -0400220 }
221 }
Mark Rutland0bcaa902014-10-23 16:33:33 +0100222
223 if (fdt_addr) {
224 pr_efi(sys_table, "Using DTB from command line\n");
225 } else {
Ard Biesheuvel345c7362014-04-03 17:46:58 +0200226 /* Look for a device tree configuration table entry. */
Ard Biesheuvela6433752015-03-04 13:02:29 +0100227 fdt_addr = (uintptr_t)get_fdt(sys_table, &fdt_size);
Mark Rutland0bcaa902014-10-23 16:33:33 +0100228 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");
Mark Salter3c7f2552014-04-15 22:47:52 -0400234
Ard Biesheuvel138728dd2017-04-04 17:02:37 +0100235 status = handle_cmdline_files(sys_table, image, cmdline_ptr, "initrd=",
236 efi_get_max_initrd_addr(dram_base,
237 *image_addr),
Mark Salter3c7f2552014-04-15 22:47:52 -0400238 (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
Ard Biesheuvel568bc4e2016-11-12 21:32:33 +0000243 efi_random_get_seed(sys_table);
244
Ard Biesheuvel38fb6652017-10-25 11:04:48 +0100245 /* hibernation expects the runtime regions to stay in the same place */
246 if (!IS_ENABLED(CONFIG_HIBERNATION) && !nokaslr()) {
Ard Biesheuvele69176d2017-04-04 17:09:10 +0100247 /*
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 */
Ard Biesheuvel197dece2017-04-17 10:32:01 +0100253 static const u64 headroom = EFI_RT_VIRTUAL_LIMIT -
254 EFI_RT_VIRTUAL_BASE -
255 EFI_RT_VIRTUAL_SIZE;
Ard Biesheuvele69176d2017-04-04 17:09:10 +0100256 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
Ard Biesheuvelb8444702018-09-21 09:32:45 -0700266 install_memreserve_table(sys_table);
267
Mark Salter3c7f2552014-04-15 22:47:52 -0400268 new_fdt_addr = fdt_addr;
269 status = allocate_new_fdt_and_exit_boot(sys_table, handle,
Ard Biesheuvel138728dd2017-04-04 17:02:37 +0100270 &new_fdt_addr, efi_get_max_fdt_addr(dram_base),
Mark Salter3c7f2552014-04-15 22:47:52 -0400271 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
Mark Salter3c7f2552014-04-15 22:47:52 -0400287fail_free_image:
288 efi_free(sys_table, image_size, *image_addr);
289 efi_free(sys_table, reserve_size, reserve_addr);
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +0100290fail_free_cmdline:
Ard Biesheuvelf0827e12016-04-25 21:06:54 +0100291 free_screen_info(sys_table, si);
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +0100292 efi_free(sys_table, cmdline_size, (unsigned long)cmdline_ptr);
Mark Salter3c7f2552014-04-15 22:47:52 -0400293fail:
294 return EFI_ERROR;
295}
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200296
Ard Biesheuvel0ce3cc02015-09-25 23:02:19 +0100297static 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
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200335/*
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{
Ard Biesheuvele69176d2017-04-04 17:09:10 +0100346 u64 efi_virt_base = virtmap_base;
Ard Biesheuvel0ce3cc02015-09-25 23:02:19 +0100347 efi_memory_desc_t *in, *prev = NULL, *out = runtime_map;
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200348 int l;
349
Ard Biesheuvel0ce3cc02015-09-25 23:02:19 +0100350 /*
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 */
Ard Biesheuvel29f90072017-10-22 15:14:57 +0100359 if (IS_ENABLED(CONFIG_ARM64))
360 sort(memory_map, map_size / desc_size, desc_size, cmp_mem_desc,
361 NULL);
Ard Biesheuvel0ce3cc02015-09-25 23:02:19 +0100362
363 for (l = 0; l < map_size; l += desc_size, prev = in) {
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200364 u64 paddr, size;
365
Ard Biesheuvel0ce3cc02015-09-25 23:02:19 +0100366 in = (void *)memory_map + l;
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200367 if (!(in->attribute & EFI_MEMORY_RUNTIME))
368 continue;
369
Ard Biesheuvel0ce3cc02015-09-25 23:02:19 +0100370 paddr = in->phys_addr;
371 size = in->num_pages * EFI_PAGE_SIZE;
372
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200373 /*
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 */
Ard Biesheuvel29f90072017-10-22 15:14:57 +0100378 if ((IS_ENABLED(CONFIG_ARM64) &&
379 !regions_are_adjacent(prev, in)) ||
Ard Biesheuvel0ce3cc02015-09-25 23:02:19 +0100380 !regions_have_compatible_memory_type_attrs(prev, in)) {
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200381
Ard Biesheuvel0ce3cc02015-09-25 23:02:19 +0100382 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 }
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200396
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}