blob: d4056c6be1ec0f029b7c0c457e34ab570356f0c6 [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 Biesheuvel2b5fe072016-01-26 14:48:29 +010021bool __nokaslr;
22
Ard Biesheuvelbd669472014-07-02 14:54:42 +020023efi_status_t efi_open_volume(efi_system_table_t *sys_table_arg,
24 void *__image, void **__fh)
Mark Salter3c7f2552014-04-15 22:47:52 -040025{
26 efi_file_io_interface_t *io;
27 efi_loaded_image_t *image = __image;
28 efi_file_handle_t *fh;
29 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
30 efi_status_t status;
31 void *handle = (void *)(unsigned long)image->device_handle;
32
33 status = sys_table_arg->boottime->handle_protocol(handle,
34 &fs_proto, (void **)&io);
35 if (status != EFI_SUCCESS) {
36 efi_printk(sys_table_arg, "Failed to handle fs_proto\n");
37 return status;
38 }
39
40 status = io->open_volume(io, &fh);
41 if (status != EFI_SUCCESS)
42 efi_printk(sys_table_arg, "Failed to open volume\n");
43
44 *__fh = fh;
45 return status;
46}
Ard Biesheuvelbd669472014-07-02 14:54:42 +020047
Ard Biesheuvelbd669472014-07-02 14:54:42 +020048void efi_char16_printk(efi_system_table_t *sys_table_arg,
Mark Salter3c7f2552014-04-15 22:47:52 -040049 efi_char16_t *str)
50{
51 struct efi_simple_text_output_protocol *out;
52
53 out = (struct efi_simple_text_output_protocol *)sys_table_arg->con_out;
54 out->output_string(out, str);
55}
56
Ard Biesheuvelf0827e12016-04-25 21:06:54 +010057static struct screen_info *setup_graphics(efi_system_table_t *sys_table_arg)
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_call_early(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(sys_table_arg);
70 if (!si)
71 return NULL;
72 efi_setup_gop(sys_table_arg, si, &gop_proto, size);
73 }
74 return si;
75}
Mark Salter3c7f2552014-04-15 22:47:52 -040076
77/*
78 * This function handles the architcture specific differences between arm and
79 * arm64 regarding where the kernel image must be loaded and any memory that
80 * must be reserved. On failure it is required to free all
81 * all allocations it has made.
82 */
Ard Biesheuvelbd669472014-07-02 14:54:42 +020083efi_status_t handle_kernel_image(efi_system_table_t *sys_table,
84 unsigned long *image_addr,
85 unsigned long *image_size,
86 unsigned long *reserve_addr,
87 unsigned long *reserve_size,
88 unsigned long dram_base,
89 efi_loaded_image_t *image);
Mark Salter3c7f2552014-04-15 22:47:52 -040090/*
91 * EFI entry point for the arm/arm64 EFI stubs. This is the entrypoint
92 * that is described in the PE/COFF header. Most of the code is the same
93 * for both archictectures, with the arch-specific code provided in the
94 * handle_kernel_image() function.
95 */
Ard Biesheuvelddeeefe2015-01-12 20:28:20 +000096unsigned long efi_entry(void *handle, efi_system_table_t *sys_table,
Mark Salter3c7f2552014-04-15 22:47:52 -040097 unsigned long *image_addr)
98{
99 efi_loaded_image_t *image;
100 efi_status_t status;
101 unsigned long image_size = 0;
102 unsigned long dram_base;
103 /* addr/point and size pairs for memory management*/
104 unsigned long initrd_addr;
105 u64 initrd_size = 0;
Ard Biesheuvel345c7362014-04-03 17:46:58 +0200106 unsigned long fdt_addr = 0; /* Original DTB */
Ard Biesheuvela6433752015-03-04 13:02:29 +0100107 unsigned long fdt_size = 0;
Mark Salter3c7f2552014-04-15 22:47:52 -0400108 char *cmdline_ptr = NULL;
109 int cmdline_size = 0;
110 unsigned long new_fdt_addr;
111 efi_guid_t loaded_image_proto = LOADED_IMAGE_PROTOCOL_GUID;
112 unsigned long reserve_addr = 0;
113 unsigned long reserve_size = 0;
David Howellsde8cb452017-02-06 11:22:43 +0000114 enum efi_secureboot_mode secure_boot;
Ard Biesheuvelf0827e12016-04-25 21:06:54 +0100115 struct screen_info *si;
Mark Salter3c7f2552014-04-15 22:47:52 -0400116
117 /* Check if we were booted by the EFI firmware */
118 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
119 goto fail;
120
121 pr_efi(sys_table, "Booting Linux Kernel...\n");
122
Ard Biesheuvelb9d6769b2016-02-17 12:36:03 +0000123 status = check_platform_features(sys_table);
124 if (status != EFI_SUCCESS)
125 goto fail;
126
Mark Salter3c7f2552014-04-15 22:47:52 -0400127 /*
128 * Get a handle to the loaded image protocol. This is used to get
129 * information about the running image, such as size and the command
130 * line.
131 */
132 status = sys_table->boottime->handle_protocol(handle,
133 &loaded_image_proto, (void *)&image);
134 if (status != EFI_SUCCESS) {
135 pr_efi_err(sys_table, "Failed to get loaded image protocol\n");
136 goto fail;
137 }
138
139 dram_base = get_dram_base(sys_table);
140 if (dram_base == EFI_ERROR) {
141 pr_efi_err(sys_table, "Failed to find DRAM base\n");
142 goto fail;
143 }
Mark Salter3c7f2552014-04-15 22:47:52 -0400144
145 /*
146 * Get the command line from EFI, using the LOADED_IMAGE
147 * protocol. We are going to copy the command line into the
148 * device tree, so this can be allocated anywhere.
149 */
150 cmdline_ptr = efi_convert_cmdline(sys_table, image, &cmdline_size);
151 if (!cmdline_ptr) {
152 pr_efi_err(sys_table, "getting command line via LOADED_IMAGE_PROTOCOL\n");
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +0100153 goto fail;
154 }
155
156 /* check whether 'nokaslr' was passed on the command line */
157 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
158 static const u8 default_cmdline[] = CONFIG_CMDLINE;
159 const u8 *str, *cmdline = cmdline_ptr;
160
161 if (IS_ENABLED(CONFIG_CMDLINE_FORCE))
162 cmdline = default_cmdline;
163 str = strstr(cmdline, "nokaslr");
164 if (str == cmdline || (str > cmdline && *(str - 1) == ' '))
165 __nokaslr = true;
166 }
167
Ard Biesheuvelf0827e12016-04-25 21:06:54 +0100168 si = setup_graphics(sys_table);
169
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +0100170 status = handle_kernel_image(sys_table, image_addr, &image_size,
171 &reserve_addr,
172 &reserve_size,
173 dram_base, image);
174 if (status != EFI_SUCCESS) {
175 pr_efi_err(sys_table, "Failed to relocate kernel\n");
176 goto fail_free_cmdline;
Mark Salter3c7f2552014-04-15 22:47:52 -0400177 }
178
Matt Fleming5a17dae2014-08-05 11:52:11 +0100179 status = efi_parse_options(cmdline_ptr);
180 if (status != EFI_SUCCESS)
181 pr_efi_err(sys_table, "Failed to parse EFI cmdline options\n");
182
Linn Crosetto73a649252016-04-25 21:06:36 +0100183 secure_boot = efi_get_secureboot(sys_table);
Linn Crosetto73a649252016-04-25 21:06:36 +0100184
Ard Biesheuvel345c7362014-04-03 17:46:58 +0200185 /*
David Howellsde8cb452017-02-06 11:22:43 +0000186 * Unauthenticated device tree data is a security hazard, so ignore
187 * 'dtb=' unless UEFI Secure Boot is disabled. We assume that secure
188 * boot is enabled if we can't determine its state.
Ard Biesheuvel345c7362014-04-03 17:46:58 +0200189 */
David Howellsde8cb452017-02-06 11:22:43 +0000190 if (secure_boot != efi_secureboot_mode_disabled &&
191 strstr(cmdline_ptr, "dtb=")) {
Linn Crosetto73a649252016-04-25 21:06:36 +0100192 pr_efi(sys_table, "Ignoring DTB from command line.\n");
Ard Biesheuvel345c7362014-04-03 17:46:58 +0200193 } else {
Mark Salter3c7f2552014-04-15 22:47:52 -0400194 status = handle_cmdline_files(sys_table, image, cmdline_ptr,
195 "dtb=",
Ard Biesheuvela6433752015-03-04 13:02:29 +0100196 ~0UL, &fdt_addr, &fdt_size);
Mark Salter3c7f2552014-04-15 22:47:52 -0400197
198 if (status != EFI_SUCCESS) {
199 pr_efi_err(sys_table, "Failed to load device tree!\n");
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +0100200 goto fail_free_image;
Mark Salter3c7f2552014-04-15 22:47:52 -0400201 }
202 }
Mark Rutland0bcaa902014-10-23 16:33:33 +0100203
204 if (fdt_addr) {
205 pr_efi(sys_table, "Using DTB from command line\n");
206 } else {
Ard Biesheuvel345c7362014-04-03 17:46:58 +0200207 /* Look for a device tree configuration table entry. */
Ard Biesheuvela6433752015-03-04 13:02:29 +0100208 fdt_addr = (uintptr_t)get_fdt(sys_table, &fdt_size);
Mark Rutland0bcaa902014-10-23 16:33:33 +0100209 if (fdt_addr)
210 pr_efi(sys_table, "Using DTB from configuration table\n");
211 }
212
213 if (!fdt_addr)
214 pr_efi(sys_table, "Generating empty DTB\n");
Mark Salter3c7f2552014-04-15 22:47:52 -0400215
216 status = handle_cmdline_files(sys_table, image, cmdline_ptr,
217 "initrd=", dram_base + SZ_512M,
218 (unsigned long *)&initrd_addr,
219 (unsigned long *)&initrd_size);
220 if (status != EFI_SUCCESS)
221 pr_efi_err(sys_table, "Failed initrd from command line!\n");
222
Ard Biesheuvel568bc4e2016-11-12 21:32:33 +0000223 efi_random_get_seed(sys_table);
224
Mark Salter3c7f2552014-04-15 22:47:52 -0400225 new_fdt_addr = fdt_addr;
226 status = allocate_new_fdt_and_exit_boot(sys_table, handle,
227 &new_fdt_addr, dram_base + MAX_FDT_OFFSET,
228 initrd_addr, initrd_size, cmdline_ptr,
229 fdt_addr, fdt_size);
230
231 /*
232 * If all went well, we need to return the FDT address to the
233 * calling function so it can be passed to kernel as part of
234 * the kernel boot protocol.
235 */
236 if (status == EFI_SUCCESS)
237 return new_fdt_addr;
238
239 pr_efi_err(sys_table, "Failed to update FDT and exit boot services\n");
240
241 efi_free(sys_table, initrd_size, initrd_addr);
242 efi_free(sys_table, fdt_size, fdt_addr);
243
Mark Salter3c7f2552014-04-15 22:47:52 -0400244fail_free_image:
245 efi_free(sys_table, image_size, *image_addr);
246 efi_free(sys_table, reserve_size, reserve_addr);
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +0100247fail_free_cmdline:
Ard Biesheuvelf0827e12016-04-25 21:06:54 +0100248 free_screen_info(sys_table, si);
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +0100249 efi_free(sys_table, cmdline_size, (unsigned long)cmdline_ptr);
Mark Salter3c7f2552014-04-15 22:47:52 -0400250fail:
251 return EFI_ERROR;
252}
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200253
254/*
255 * This is the base address at which to start allocating virtual memory ranges
256 * for UEFI Runtime Services. This is in the low TTBR0 range so that we can use
257 * any allocation we choose, and eliminate the risk of a conflict after kexec.
258 * The value chosen is the largest non-zero power of 2 suitable for this purpose
259 * both on 32-bit and 64-bit ARM CPUs, to maximize the likelihood that it can
260 * be mapped efficiently.
Roy Franz81a0bc32015-09-23 20:17:54 -0700261 * Since 32-bit ARM could potentially execute with a 1G/3G user/kernel split,
262 * map everything below 1 GB.
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200263 */
Roy Franz81a0bc32015-09-23 20:17:54 -0700264#define EFI_RT_VIRTUAL_BASE SZ_512M
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200265
Ard Biesheuvel0ce3cc02015-09-25 23:02:19 +0100266static int cmp_mem_desc(const void *l, const void *r)
267{
268 const efi_memory_desc_t *left = l, *right = r;
269
270 return (left->phys_addr > right->phys_addr) ? 1 : -1;
271}
272
273/*
274 * Returns whether region @left ends exactly where region @right starts,
275 * or false if either argument is NULL.
276 */
277static bool regions_are_adjacent(efi_memory_desc_t *left,
278 efi_memory_desc_t *right)
279{
280 u64 left_end;
281
282 if (left == NULL || right == NULL)
283 return false;
284
285 left_end = left->phys_addr + left->num_pages * EFI_PAGE_SIZE;
286
287 return left_end == right->phys_addr;
288}
289
290/*
291 * Returns whether region @left and region @right have compatible memory type
292 * mapping attributes, and are both EFI_MEMORY_RUNTIME regions.
293 */
294static bool regions_have_compatible_memory_type_attrs(efi_memory_desc_t *left,
295 efi_memory_desc_t *right)
296{
297 static const u64 mem_type_mask = EFI_MEMORY_WB | EFI_MEMORY_WT |
298 EFI_MEMORY_WC | EFI_MEMORY_UC |
299 EFI_MEMORY_RUNTIME;
300
301 return ((left->attribute ^ right->attribute) & mem_type_mask) == 0;
302}
303
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200304/*
305 * efi_get_virtmap() - create a virtual mapping for the EFI memory map
306 *
307 * This function populates the virt_addr fields of all memory region descriptors
308 * in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors
309 * are also copied to @runtime_map, and their total count is returned in @count.
310 */
311void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
312 unsigned long desc_size, efi_memory_desc_t *runtime_map,
313 int *count)
314{
315 u64 efi_virt_base = EFI_RT_VIRTUAL_BASE;
Ard Biesheuvel0ce3cc02015-09-25 23:02:19 +0100316 efi_memory_desc_t *in, *prev = NULL, *out = runtime_map;
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200317 int l;
318
Ard Biesheuvel0ce3cc02015-09-25 23:02:19 +0100319 /*
320 * To work around potential issues with the Properties Table feature
321 * introduced in UEFI 2.5, which may split PE/COFF executable images
322 * in memory into several RuntimeServicesCode and RuntimeServicesData
323 * regions, we need to preserve the relative offsets between adjacent
324 * EFI_MEMORY_RUNTIME regions with the same memory type attributes.
325 * The easiest way to find adjacent regions is to sort the memory map
326 * before traversing it.
327 */
328 sort(memory_map, map_size / desc_size, desc_size, cmp_mem_desc, NULL);
329
330 for (l = 0; l < map_size; l += desc_size, prev = in) {
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200331 u64 paddr, size;
332
Ard Biesheuvel0ce3cc02015-09-25 23:02:19 +0100333 in = (void *)memory_map + l;
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200334 if (!(in->attribute & EFI_MEMORY_RUNTIME))
335 continue;
336
Ard Biesheuvel0ce3cc02015-09-25 23:02:19 +0100337 paddr = in->phys_addr;
338 size = in->num_pages * EFI_PAGE_SIZE;
339
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200340 /*
341 * Make the mapping compatible with 64k pages: this allows
342 * a 4k page size kernel to kexec a 64k page size kernel and
343 * vice versa.
344 */
Ard Biesheuvel0ce3cc02015-09-25 23:02:19 +0100345 if (!regions_are_adjacent(prev, in) ||
346 !regions_have_compatible_memory_type_attrs(prev, in)) {
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200347
Ard Biesheuvel0ce3cc02015-09-25 23:02:19 +0100348 paddr = round_down(in->phys_addr, SZ_64K);
349 size += in->phys_addr - paddr;
350
351 /*
352 * Avoid wasting memory on PTEs by choosing a virtual
353 * base that is compatible with section mappings if this
354 * region has the appropriate size and physical
355 * alignment. (Sections are 2 MB on 4k granule kernels)
356 */
357 if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M)
358 efi_virt_base = round_up(efi_virt_base, SZ_2M);
359 else
360 efi_virt_base = round_up(efi_virt_base, SZ_64K);
361 }
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200362
363 in->virt_addr = efi_virt_base + in->phys_addr - paddr;
364 efi_virt_base += size;
365
366 memcpy(out, in, desc_size);
367 out = (void *)out + desc_size;
368 ++*count;
369 }
370}