blob: 7ec09ea81b9422b070be166c4f6cb764f696cea5 [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
Linn Crosetto73a649252016-04-25 21:06:36 +010021static int efi_get_secureboot(efi_system_table_t *sys_table_arg)
Ard Biesheuvel345c7362014-04-03 17:46:58 +020022{
Linn Crosetto30d7bf02016-04-25 21:06:37 +010023 static efi_char16_t const sb_var_name[] = {
Ard Biesheuvel345c7362014-04-03 17:46:58 +020024 'S', 'e', 'c', 'u', 'r', 'e', 'B', 'o', 'o', 't', 0 };
Linn Crosetto30d7bf02016-04-25 21:06:37 +010025 static efi_char16_t const sm_var_name[] = {
26 'S', 'e', 't', 'u', 'p', 'M', 'o', 'd', 'e', 0 };
Ard Biesheuvel345c7362014-04-03 17:46:58 +020027
Linn Crosetto30d7bf02016-04-25 21:06:37 +010028 efi_guid_t var_guid = EFI_GLOBAL_VARIABLE_GUID;
Ard Biesheuvel345c7362014-04-03 17:46:58 +020029 efi_get_variable_t *f_getvar = sys_table_arg->runtime->get_variable;
Ard Biesheuvel345c7362014-04-03 17:46:58 +020030 u8 val;
Linn Crosetto30d7bf02016-04-25 21:06:37 +010031 unsigned long size = sizeof(val);
32 efi_status_t status;
Ard Biesheuvel345c7362014-04-03 17:46:58 +020033
Linn Crosetto30d7bf02016-04-25 21:06:37 +010034 status = f_getvar((efi_char16_t *)sb_var_name, (efi_guid_t *)&var_guid,
Ard Biesheuvel345c7362014-04-03 17:46:58 +020035 NULL, &size, &val);
36
Linn Crosetto30d7bf02016-04-25 21:06:37 +010037 if (status != EFI_SUCCESS)
38 goto out_efi_err;
39
40 if (val == 0)
41 return 0;
42
43 status = f_getvar((efi_char16_t *)sm_var_name, (efi_guid_t *)&var_guid,
44 NULL, &size, &val);
45
46 if (status != EFI_SUCCESS)
47 goto out_efi_err;
48
49 if (val == 1)
50 return 0;
51
52 return 1;
53
54out_efi_err:
Ard Biesheuvel345c7362014-04-03 17:46:58 +020055 switch (status) {
Ard Biesheuvel345c7362014-04-03 17:46:58 +020056 case EFI_NOT_FOUND:
57 return 0;
Linn Crosetto73a649252016-04-25 21:06:36 +010058 case EFI_DEVICE_ERROR:
59 return -EIO;
60 case EFI_SECURITY_VIOLATION:
61 return -EACCES;
Ard Biesheuvel345c7362014-04-03 17:46:58 +020062 default:
Linn Crosetto73a649252016-04-25 21:06:36 +010063 return -EINVAL;
Ard Biesheuvel345c7362014-04-03 17:46:58 +020064 }
65}
66
Ard Biesheuvelbd669472014-07-02 14:54:42 +020067efi_status_t efi_open_volume(efi_system_table_t *sys_table_arg,
68 void *__image, void **__fh)
Mark Salter3c7f2552014-04-15 22:47:52 -040069{
70 efi_file_io_interface_t *io;
71 efi_loaded_image_t *image = __image;
72 efi_file_handle_t *fh;
73 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
74 efi_status_t status;
75 void *handle = (void *)(unsigned long)image->device_handle;
76
77 status = sys_table_arg->boottime->handle_protocol(handle,
78 &fs_proto, (void **)&io);
79 if (status != EFI_SUCCESS) {
80 efi_printk(sys_table_arg, "Failed to handle fs_proto\n");
81 return status;
82 }
83
84 status = io->open_volume(io, &fh);
85 if (status != EFI_SUCCESS)
86 efi_printk(sys_table_arg, "Failed to open volume\n");
87
88 *__fh = fh;
89 return status;
90}
Ard Biesheuvelbd669472014-07-02 14:54:42 +020091
92efi_status_t efi_file_close(void *handle)
Mark Salter3c7f2552014-04-15 22:47:52 -040093{
94 efi_file_handle_t *fh = handle;
95
96 return fh->close(handle);
97}
98
Ard Biesheuvelbd669472014-07-02 14:54:42 +020099efi_status_t
Mark Salter3c7f2552014-04-15 22:47:52 -0400100efi_file_read(void *handle, unsigned long *size, void *addr)
101{
102 efi_file_handle_t *fh = handle;
103
104 return fh->read(handle, size, addr);
105}
106
107
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200108efi_status_t
Mark Salter3c7f2552014-04-15 22:47:52 -0400109efi_file_size(efi_system_table_t *sys_table_arg, void *__fh,
110 efi_char16_t *filename_16, void **handle, u64 *file_sz)
111{
112 efi_file_handle_t *h, *fh = __fh;
113 efi_file_info_t *info;
114 efi_status_t status;
115 efi_guid_t info_guid = EFI_FILE_INFO_ID;
116 unsigned long info_sz;
117
118 status = fh->open(fh, &h, filename_16, EFI_FILE_MODE_READ, (u64)0);
119 if (status != EFI_SUCCESS) {
120 efi_printk(sys_table_arg, "Failed to open file: ");
121 efi_char16_printk(sys_table_arg, filename_16);
122 efi_printk(sys_table_arg, "\n");
123 return status;
124 }
125
126 *handle = h;
127
128 info_sz = 0;
129 status = h->get_info(h, &info_guid, &info_sz, NULL);
130 if (status != EFI_BUFFER_TOO_SMALL) {
131 efi_printk(sys_table_arg, "Failed to get file info size\n");
132 return status;
133 }
134
135grow:
136 status = sys_table_arg->boottime->allocate_pool(EFI_LOADER_DATA,
137 info_sz, (void **)&info);
138 if (status != EFI_SUCCESS) {
139 efi_printk(sys_table_arg, "Failed to alloc mem for file info\n");
140 return status;
141 }
142
143 status = h->get_info(h, &info_guid, &info_sz,
144 info);
145 if (status == EFI_BUFFER_TOO_SMALL) {
146 sys_table_arg->boottime->free_pool(info);
147 goto grow;
148 }
149
150 *file_sz = info->file_size;
151 sys_table_arg->boottime->free_pool(info);
152
153 if (status != EFI_SUCCESS)
154 efi_printk(sys_table_arg, "Failed to get initrd info\n");
155
156 return status;
157}
158
159
160
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200161void efi_char16_printk(efi_system_table_t *sys_table_arg,
Mark Salter3c7f2552014-04-15 22:47:52 -0400162 efi_char16_t *str)
163{
164 struct efi_simple_text_output_protocol *out;
165
166 out = (struct efi_simple_text_output_protocol *)sys_table_arg->con_out;
167 out->output_string(out, str);
168}
169
Ard Biesheuvelf0827e12016-04-25 21:06:54 +0100170static struct screen_info *setup_graphics(efi_system_table_t *sys_table_arg)
171{
172 efi_guid_t gop_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
173 efi_status_t status;
174 unsigned long size;
175 void **gop_handle = NULL;
176 struct screen_info *si = NULL;
177
178 size = 0;
179 status = efi_call_early(locate_handle, EFI_LOCATE_BY_PROTOCOL,
180 &gop_proto, NULL, &size, gop_handle);
181 if (status == EFI_BUFFER_TOO_SMALL) {
182 si = alloc_screen_info(sys_table_arg);
183 if (!si)
184 return NULL;
185 efi_setup_gop(sys_table_arg, si, &gop_proto, size);
186 }
187 return si;
188}
Mark Salter3c7f2552014-04-15 22:47:52 -0400189
190/*
191 * This function handles the architcture specific differences between arm and
192 * arm64 regarding where the kernel image must be loaded and any memory that
193 * must be reserved. On failure it is required to free all
194 * all allocations it has made.
195 */
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200196efi_status_t handle_kernel_image(efi_system_table_t *sys_table,
197 unsigned long *image_addr,
198 unsigned long *image_size,
199 unsigned long *reserve_addr,
200 unsigned long *reserve_size,
201 unsigned long dram_base,
202 efi_loaded_image_t *image);
Mark Salter3c7f2552014-04-15 22:47:52 -0400203/*
204 * EFI entry point for the arm/arm64 EFI stubs. This is the entrypoint
205 * that is described in the PE/COFF header. Most of the code is the same
206 * for both archictectures, with the arch-specific code provided in the
207 * handle_kernel_image() function.
208 */
Ard Biesheuvelddeeefe2015-01-12 20:28:20 +0000209unsigned long efi_entry(void *handle, efi_system_table_t *sys_table,
Mark Salter3c7f2552014-04-15 22:47:52 -0400210 unsigned long *image_addr)
211{
212 efi_loaded_image_t *image;
213 efi_status_t status;
214 unsigned long image_size = 0;
215 unsigned long dram_base;
216 /* addr/point and size pairs for memory management*/
217 unsigned long initrd_addr;
218 u64 initrd_size = 0;
Ard Biesheuvel345c7362014-04-03 17:46:58 +0200219 unsigned long fdt_addr = 0; /* Original DTB */
Ard Biesheuvela6433752015-03-04 13:02:29 +0100220 unsigned long fdt_size = 0;
Mark Salter3c7f2552014-04-15 22:47:52 -0400221 char *cmdline_ptr = NULL;
222 int cmdline_size = 0;
223 unsigned long new_fdt_addr;
224 efi_guid_t loaded_image_proto = LOADED_IMAGE_PROTOCOL_GUID;
225 unsigned long reserve_addr = 0;
226 unsigned long reserve_size = 0;
Linn Crosetto73a649252016-04-25 21:06:36 +0100227 int secure_boot = 0;
Ard Biesheuvelf0827e12016-04-25 21:06:54 +0100228 struct screen_info *si;
Mark Salter3c7f2552014-04-15 22:47:52 -0400229
230 /* Check if we were booted by the EFI firmware */
231 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
232 goto fail;
233
234 pr_efi(sys_table, "Booting Linux Kernel...\n");
235
Ard Biesheuvelb9d6769b2016-02-17 12:36:03 +0000236 status = check_platform_features(sys_table);
237 if (status != EFI_SUCCESS)
238 goto fail;
239
Mark Salter3c7f2552014-04-15 22:47:52 -0400240 /*
241 * Get a handle to the loaded image protocol. This is used to get
242 * information about the running image, such as size and the command
243 * line.
244 */
245 status = sys_table->boottime->handle_protocol(handle,
246 &loaded_image_proto, (void *)&image);
247 if (status != EFI_SUCCESS) {
248 pr_efi_err(sys_table, "Failed to get loaded image protocol\n");
249 goto fail;
250 }
251
252 dram_base = get_dram_base(sys_table);
253 if (dram_base == EFI_ERROR) {
254 pr_efi_err(sys_table, "Failed to find DRAM base\n");
255 goto fail;
256 }
Mark Salter3c7f2552014-04-15 22:47:52 -0400257
258 /*
259 * Get the command line from EFI, using the LOADED_IMAGE
260 * protocol. We are going to copy the command line into the
261 * device tree, so this can be allocated anywhere.
262 */
263 cmdline_ptr = efi_convert_cmdline(sys_table, image, &cmdline_size);
264 if (!cmdline_ptr) {
265 pr_efi_err(sys_table, "getting command line via LOADED_IMAGE_PROTOCOL\n");
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +0100266 goto fail;
267 }
268
Ard Biesheuvelf0827e12016-04-25 21:06:54 +0100269 si = setup_graphics(sys_table);
270
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +0100271 status = handle_kernel_image(sys_table, image_addr, &image_size,
272 &reserve_addr,
273 &reserve_size,
274 dram_base, image);
275 if (status != EFI_SUCCESS) {
276 pr_efi_err(sys_table, "Failed to relocate kernel\n");
277 goto fail_free_cmdline;
Mark Salter3c7f2552014-04-15 22:47:52 -0400278 }
279
Ard Biesheuvele3111d52017-04-04 17:09:08 +0100280 if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) ||
281 IS_ENABLED(CONFIG_CMDLINE_FORCE) ||
282 cmdline_size == 0)
283 efi_parse_options(CONFIG_CMDLINE);
284
285 if (!IS_ENABLED(CONFIG_CMDLINE_FORCE) && cmdline_size > 0)
286 efi_parse_options(cmdline_ptr);
Matt Fleming5a17dae2014-08-05 11:52:11 +0100287
Linn Crosetto73a649252016-04-25 21:06:36 +0100288 secure_boot = efi_get_secureboot(sys_table);
289 if (secure_boot > 0)
290 pr_efi(sys_table, "UEFI Secure Boot is enabled.\n");
291
292 if (secure_boot < 0) {
293 pr_efi_err(sys_table,
294 "could not determine UEFI Secure Boot status.\n");
295 }
296
Ard Biesheuvel345c7362014-04-03 17:46:58 +0200297 /*
298 * Unauthenticated device tree data is a security hazard, so
299 * ignore 'dtb=' unless UEFI Secure Boot is disabled.
300 */
Linn Crosetto73a649252016-04-25 21:06:36 +0100301 if (secure_boot != 0 && strstr(cmdline_ptr, "dtb=")) {
302 pr_efi(sys_table, "Ignoring DTB from command line.\n");
Ard Biesheuvel345c7362014-04-03 17:46:58 +0200303 } else {
Mark Salter3c7f2552014-04-15 22:47:52 -0400304 status = handle_cmdline_files(sys_table, image, cmdline_ptr,
305 "dtb=",
Ard Biesheuvela6433752015-03-04 13:02:29 +0100306 ~0UL, &fdt_addr, &fdt_size);
Mark Salter3c7f2552014-04-15 22:47:52 -0400307
308 if (status != EFI_SUCCESS) {
309 pr_efi_err(sys_table, "Failed to load device tree!\n");
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +0100310 goto fail_free_image;
Mark Salter3c7f2552014-04-15 22:47:52 -0400311 }
312 }
Mark Rutland0bcaa902014-10-23 16:33:33 +0100313
314 if (fdt_addr) {
315 pr_efi(sys_table, "Using DTB from command line\n");
316 } else {
Ard Biesheuvel345c7362014-04-03 17:46:58 +0200317 /* Look for a device tree configuration table entry. */
Ard Biesheuvela6433752015-03-04 13:02:29 +0100318 fdt_addr = (uintptr_t)get_fdt(sys_table, &fdt_size);
Mark Rutland0bcaa902014-10-23 16:33:33 +0100319 if (fdt_addr)
320 pr_efi(sys_table, "Using DTB from configuration table\n");
321 }
322
323 if (!fdt_addr)
324 pr_efi(sys_table, "Generating empty DTB\n");
Mark Salter3c7f2552014-04-15 22:47:52 -0400325
326 status = handle_cmdline_files(sys_table, image, cmdline_ptr,
327 "initrd=", dram_base + SZ_512M,
328 (unsigned long *)&initrd_addr,
329 (unsigned long *)&initrd_size);
330 if (status != EFI_SUCCESS)
331 pr_efi_err(sys_table, "Failed initrd from command line!\n");
332
333 new_fdt_addr = fdt_addr;
334 status = allocate_new_fdt_and_exit_boot(sys_table, handle,
335 &new_fdt_addr, dram_base + MAX_FDT_OFFSET,
336 initrd_addr, initrd_size, cmdline_ptr,
337 fdt_addr, fdt_size);
338
339 /*
340 * If all went well, we need to return the FDT address to the
341 * calling function so it can be passed to kernel as part of
342 * the kernel boot protocol.
343 */
344 if (status == EFI_SUCCESS)
345 return new_fdt_addr;
346
347 pr_efi_err(sys_table, "Failed to update FDT and exit boot services\n");
348
349 efi_free(sys_table, initrd_size, initrd_addr);
350 efi_free(sys_table, fdt_size, fdt_addr);
351
Mark Salter3c7f2552014-04-15 22:47:52 -0400352fail_free_image:
353 efi_free(sys_table, image_size, *image_addr);
354 efi_free(sys_table, reserve_size, reserve_addr);
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +0100355fail_free_cmdline:
Ard Biesheuvelf0827e12016-04-25 21:06:54 +0100356 free_screen_info(sys_table, si);
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +0100357 efi_free(sys_table, cmdline_size, (unsigned long)cmdline_ptr);
Mark Salter3c7f2552014-04-15 22:47:52 -0400358fail:
359 return EFI_ERROR;
360}
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200361
362/*
363 * This is the base address at which to start allocating virtual memory ranges
364 * for UEFI Runtime Services. This is in the low TTBR0 range so that we can use
365 * any allocation we choose, and eliminate the risk of a conflict after kexec.
366 * The value chosen is the largest non-zero power of 2 suitable for this purpose
367 * both on 32-bit and 64-bit ARM CPUs, to maximize the likelihood that it can
368 * be mapped efficiently.
Roy Franz81a0bc32015-09-23 20:17:54 -0700369 * Since 32-bit ARM could potentially execute with a 1G/3G user/kernel split,
370 * map everything below 1 GB.
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200371 */
Roy Franz81a0bc32015-09-23 20:17:54 -0700372#define EFI_RT_VIRTUAL_BASE SZ_512M
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200373
Ard Biesheuvel0ce3cc02015-09-25 23:02:19 +0100374static int cmp_mem_desc(const void *l, const void *r)
375{
376 const efi_memory_desc_t *left = l, *right = r;
377
378 return (left->phys_addr > right->phys_addr) ? 1 : -1;
379}
380
381/*
382 * Returns whether region @left ends exactly where region @right starts,
383 * or false if either argument is NULL.
384 */
385static bool regions_are_adjacent(efi_memory_desc_t *left,
386 efi_memory_desc_t *right)
387{
388 u64 left_end;
389
390 if (left == NULL || right == NULL)
391 return false;
392
393 left_end = left->phys_addr + left->num_pages * EFI_PAGE_SIZE;
394
395 return left_end == right->phys_addr;
396}
397
398/*
399 * Returns whether region @left and region @right have compatible memory type
400 * mapping attributes, and are both EFI_MEMORY_RUNTIME regions.
401 */
402static bool regions_have_compatible_memory_type_attrs(efi_memory_desc_t *left,
403 efi_memory_desc_t *right)
404{
405 static const u64 mem_type_mask = EFI_MEMORY_WB | EFI_MEMORY_WT |
406 EFI_MEMORY_WC | EFI_MEMORY_UC |
407 EFI_MEMORY_RUNTIME;
408
409 return ((left->attribute ^ right->attribute) & mem_type_mask) == 0;
410}
411
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200412/*
413 * efi_get_virtmap() - create a virtual mapping for the EFI memory map
414 *
415 * This function populates the virt_addr fields of all memory region descriptors
416 * in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors
417 * are also copied to @runtime_map, and their total count is returned in @count.
418 */
419void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
420 unsigned long desc_size, efi_memory_desc_t *runtime_map,
421 int *count)
422{
423 u64 efi_virt_base = EFI_RT_VIRTUAL_BASE;
Ard Biesheuvel0ce3cc02015-09-25 23:02:19 +0100424 efi_memory_desc_t *in, *prev = NULL, *out = runtime_map;
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200425 int l;
426
Ard Biesheuvel0ce3cc02015-09-25 23:02:19 +0100427 /*
428 * To work around potential issues with the Properties Table feature
429 * introduced in UEFI 2.5, which may split PE/COFF executable images
430 * in memory into several RuntimeServicesCode and RuntimeServicesData
431 * regions, we need to preserve the relative offsets between adjacent
432 * EFI_MEMORY_RUNTIME regions with the same memory type attributes.
433 * The easiest way to find adjacent regions is to sort the memory map
434 * before traversing it.
435 */
436 sort(memory_map, map_size / desc_size, desc_size, cmp_mem_desc, NULL);
437
438 for (l = 0; l < map_size; l += desc_size, prev = in) {
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200439 u64 paddr, size;
440
Ard Biesheuvel0ce3cc02015-09-25 23:02:19 +0100441 in = (void *)memory_map + l;
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200442 if (!(in->attribute & EFI_MEMORY_RUNTIME))
443 continue;
444
Ard Biesheuvel0ce3cc02015-09-25 23:02:19 +0100445 paddr = in->phys_addr;
446 size = in->num_pages * EFI_PAGE_SIZE;
447
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200448 /*
449 * Make the mapping compatible with 64k pages: this allows
450 * a 4k page size kernel to kexec a 64k page size kernel and
451 * vice versa.
452 */
Ard Biesheuvel0ce3cc02015-09-25 23:02:19 +0100453 if (!regions_are_adjacent(prev, in) ||
454 !regions_have_compatible_memory_type_attrs(prev, in)) {
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200455
Ard Biesheuvel0ce3cc02015-09-25 23:02:19 +0100456 paddr = round_down(in->phys_addr, SZ_64K);
457 size += in->phys_addr - paddr;
458
459 /*
460 * Avoid wasting memory on PTEs by choosing a virtual
461 * base that is compatible with section mappings if this
462 * region has the appropriate size and physical
463 * alignment. (Sections are 2 MB on 4k granule kernels)
464 */
465 if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M)
466 efi_virt_base = round_up(efi_virt_base, SZ_2M);
467 else
468 efi_virt_base = round_up(efi_virt_base, SZ_64K);
469 }
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200470
471 in->virt_addr = efi_virt_base + in->phys_addr - paddr;
472 efi_virt_base += size;
473
474 memcpy(out, in, desc_size);
475 out = (void *)out + desc_size;
476 ++*count;
477 }
478}