blob: 07f967c4c567ffd0816878f3d55351ed68a9f58b [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
Linn Crosetto73a649252016-04-25 21:06:36 +010023static int efi_get_secureboot(efi_system_table_t *sys_table_arg)
Ard Biesheuvel345c7362014-04-03 17:46:58 +020024{
Ard Biesheuvelddeeefe2015-01-12 20:28:20 +000025 static efi_guid_t const var_guid = EFI_GLOBAL_VARIABLE_GUID;
26 static efi_char16_t const var_name[] = {
Ard Biesheuvel345c7362014-04-03 17:46:58 +020027 'S', 'e', 'c', 'u', 'r', 'e', 'B', 'o', 'o', 't', 0 };
28
29 efi_get_variable_t *f_getvar = sys_table_arg->runtime->get_variable;
30 unsigned long size = sizeof(u8);
31 efi_status_t status;
32 u8 val;
33
34 status = f_getvar((efi_char16_t *)var_name, (efi_guid_t *)&var_guid,
35 NULL, &size, &val);
36
37 switch (status) {
38 case EFI_SUCCESS:
39 return val;
40 case EFI_NOT_FOUND:
41 return 0;
Linn Crosetto73a649252016-04-25 21:06:36 +010042 case EFI_DEVICE_ERROR:
43 return -EIO;
44 case EFI_SECURITY_VIOLATION:
45 return -EACCES;
Ard Biesheuvel345c7362014-04-03 17:46:58 +020046 default:
Linn Crosetto73a649252016-04-25 21:06:36 +010047 return -EINVAL;
Ard Biesheuvel345c7362014-04-03 17:46:58 +020048 }
49}
50
Ard Biesheuvelbd669472014-07-02 14:54:42 +020051efi_status_t efi_open_volume(efi_system_table_t *sys_table_arg,
52 void *__image, void **__fh)
Mark Salter3c7f2552014-04-15 22:47:52 -040053{
54 efi_file_io_interface_t *io;
55 efi_loaded_image_t *image = __image;
56 efi_file_handle_t *fh;
57 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
58 efi_status_t status;
59 void *handle = (void *)(unsigned long)image->device_handle;
60
61 status = sys_table_arg->boottime->handle_protocol(handle,
62 &fs_proto, (void **)&io);
63 if (status != EFI_SUCCESS) {
64 efi_printk(sys_table_arg, "Failed to handle fs_proto\n");
65 return status;
66 }
67
68 status = io->open_volume(io, &fh);
69 if (status != EFI_SUCCESS)
70 efi_printk(sys_table_arg, "Failed to open volume\n");
71
72 *__fh = fh;
73 return status;
74}
Ard Biesheuvelbd669472014-07-02 14:54:42 +020075
76efi_status_t efi_file_close(void *handle)
Mark Salter3c7f2552014-04-15 22:47:52 -040077{
78 efi_file_handle_t *fh = handle;
79
80 return fh->close(handle);
81}
82
Ard Biesheuvelbd669472014-07-02 14:54:42 +020083efi_status_t
Mark Salter3c7f2552014-04-15 22:47:52 -040084efi_file_read(void *handle, unsigned long *size, void *addr)
85{
86 efi_file_handle_t *fh = handle;
87
88 return fh->read(handle, size, addr);
89}
90
91
Ard Biesheuvelbd669472014-07-02 14:54:42 +020092efi_status_t
Mark Salter3c7f2552014-04-15 22:47:52 -040093efi_file_size(efi_system_table_t *sys_table_arg, void *__fh,
94 efi_char16_t *filename_16, void **handle, u64 *file_sz)
95{
96 efi_file_handle_t *h, *fh = __fh;
97 efi_file_info_t *info;
98 efi_status_t status;
99 efi_guid_t info_guid = EFI_FILE_INFO_ID;
100 unsigned long info_sz;
101
102 status = fh->open(fh, &h, filename_16, EFI_FILE_MODE_READ, (u64)0);
103 if (status != EFI_SUCCESS) {
104 efi_printk(sys_table_arg, "Failed to open file: ");
105 efi_char16_printk(sys_table_arg, filename_16);
106 efi_printk(sys_table_arg, "\n");
107 return status;
108 }
109
110 *handle = h;
111
112 info_sz = 0;
113 status = h->get_info(h, &info_guid, &info_sz, NULL);
114 if (status != EFI_BUFFER_TOO_SMALL) {
115 efi_printk(sys_table_arg, "Failed to get file info size\n");
116 return status;
117 }
118
119grow:
120 status = sys_table_arg->boottime->allocate_pool(EFI_LOADER_DATA,
121 info_sz, (void **)&info);
122 if (status != EFI_SUCCESS) {
123 efi_printk(sys_table_arg, "Failed to alloc mem for file info\n");
124 return status;
125 }
126
127 status = h->get_info(h, &info_guid, &info_sz,
128 info);
129 if (status == EFI_BUFFER_TOO_SMALL) {
130 sys_table_arg->boottime->free_pool(info);
131 goto grow;
132 }
133
134 *file_sz = info->file_size;
135 sys_table_arg->boottime->free_pool(info);
136
137 if (status != EFI_SUCCESS)
138 efi_printk(sys_table_arg, "Failed to get initrd info\n");
139
140 return status;
141}
142
143
144
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200145void efi_char16_printk(efi_system_table_t *sys_table_arg,
Mark Salter3c7f2552014-04-15 22:47:52 -0400146 efi_char16_t *str)
147{
148 struct efi_simple_text_output_protocol *out;
149
150 out = (struct efi_simple_text_output_protocol *)sys_table_arg->con_out;
151 out->output_string(out, str);
152}
153
154
155/*
156 * This function handles the architcture specific differences between arm and
157 * arm64 regarding where the kernel image must be loaded and any memory that
158 * must be reserved. On failure it is required to free all
159 * all allocations it has made.
160 */
Ard Biesheuvelbd669472014-07-02 14:54:42 +0200161efi_status_t handle_kernel_image(efi_system_table_t *sys_table,
162 unsigned long *image_addr,
163 unsigned long *image_size,
164 unsigned long *reserve_addr,
165 unsigned long *reserve_size,
166 unsigned long dram_base,
167 efi_loaded_image_t *image);
Mark Salter3c7f2552014-04-15 22:47:52 -0400168/*
169 * EFI entry point for the arm/arm64 EFI stubs. This is the entrypoint
170 * that is described in the PE/COFF header. Most of the code is the same
171 * for both archictectures, with the arch-specific code provided in the
172 * handle_kernel_image() function.
173 */
Ard Biesheuvelddeeefe2015-01-12 20:28:20 +0000174unsigned long efi_entry(void *handle, efi_system_table_t *sys_table,
Mark Salter3c7f2552014-04-15 22:47:52 -0400175 unsigned long *image_addr)
176{
177 efi_loaded_image_t *image;
178 efi_status_t status;
179 unsigned long image_size = 0;
180 unsigned long dram_base;
181 /* addr/point and size pairs for memory management*/
182 unsigned long initrd_addr;
183 u64 initrd_size = 0;
Ard Biesheuvel345c7362014-04-03 17:46:58 +0200184 unsigned long fdt_addr = 0; /* Original DTB */
Ard Biesheuvela6433752015-03-04 13:02:29 +0100185 unsigned long fdt_size = 0;
Mark Salter3c7f2552014-04-15 22:47:52 -0400186 char *cmdline_ptr = NULL;
187 int cmdline_size = 0;
188 unsigned long new_fdt_addr;
189 efi_guid_t loaded_image_proto = LOADED_IMAGE_PROTOCOL_GUID;
190 unsigned long reserve_addr = 0;
191 unsigned long reserve_size = 0;
Linn Crosetto73a649252016-04-25 21:06:36 +0100192 int secure_boot = 0;
Mark Salter3c7f2552014-04-15 22:47:52 -0400193
194 /* Check if we were booted by the EFI firmware */
195 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
196 goto fail;
197
198 pr_efi(sys_table, "Booting Linux Kernel...\n");
199
Ard Biesheuvelb9d6769b2016-02-17 12:36:03 +0000200 status = check_platform_features(sys_table);
201 if (status != EFI_SUCCESS)
202 goto fail;
203
Mark Salter3c7f2552014-04-15 22:47:52 -0400204 /*
205 * Get a handle to the loaded image protocol. This is used to get
206 * information about the running image, such as size and the command
207 * line.
208 */
209 status = sys_table->boottime->handle_protocol(handle,
210 &loaded_image_proto, (void *)&image);
211 if (status != EFI_SUCCESS) {
212 pr_efi_err(sys_table, "Failed to get loaded image protocol\n");
213 goto fail;
214 }
215
216 dram_base = get_dram_base(sys_table);
217 if (dram_base == EFI_ERROR) {
218 pr_efi_err(sys_table, "Failed to find DRAM base\n");
219 goto fail;
220 }
Mark Salter3c7f2552014-04-15 22:47:52 -0400221
222 /*
223 * Get the command line from EFI, using the LOADED_IMAGE
224 * protocol. We are going to copy the command line into the
225 * device tree, so this can be allocated anywhere.
226 */
227 cmdline_ptr = efi_convert_cmdline(sys_table, image, &cmdline_size);
228 if (!cmdline_ptr) {
229 pr_efi_err(sys_table, "getting command line via LOADED_IMAGE_PROTOCOL\n");
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +0100230 goto fail;
231 }
232
233 /* check whether 'nokaslr' was passed on the command line */
234 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
235 static const u8 default_cmdline[] = CONFIG_CMDLINE;
236 const u8 *str, *cmdline = cmdline_ptr;
237
238 if (IS_ENABLED(CONFIG_CMDLINE_FORCE))
239 cmdline = default_cmdline;
240 str = strstr(cmdline, "nokaslr");
241 if (str == cmdline || (str > cmdline && *(str - 1) == ' '))
242 __nokaslr = true;
243 }
244
245 status = handle_kernel_image(sys_table, image_addr, &image_size,
246 &reserve_addr,
247 &reserve_size,
248 dram_base, image);
249 if (status != EFI_SUCCESS) {
250 pr_efi_err(sys_table, "Failed to relocate kernel\n");
251 goto fail_free_cmdline;
Mark Salter3c7f2552014-04-15 22:47:52 -0400252 }
253
Matt Fleming5a17dae2014-08-05 11:52:11 +0100254 status = efi_parse_options(cmdline_ptr);
255 if (status != EFI_SUCCESS)
256 pr_efi_err(sys_table, "Failed to parse EFI cmdline options\n");
257
Linn Crosetto73a649252016-04-25 21:06:36 +0100258 secure_boot = efi_get_secureboot(sys_table);
259 if (secure_boot > 0)
260 pr_efi(sys_table, "UEFI Secure Boot is enabled.\n");
261
262 if (secure_boot < 0) {
263 pr_efi_err(sys_table,
264 "could not determine UEFI Secure Boot status.\n");
265 }
266
Ard Biesheuvel345c7362014-04-03 17:46:58 +0200267 /*
268 * Unauthenticated device tree data is a security hazard, so
269 * ignore 'dtb=' unless UEFI Secure Boot is disabled.
270 */
Linn Crosetto73a649252016-04-25 21:06:36 +0100271 if (secure_boot != 0 && strstr(cmdline_ptr, "dtb=")) {
272 pr_efi(sys_table, "Ignoring DTB from command line.\n");
Ard Biesheuvel345c7362014-04-03 17:46:58 +0200273 } else {
Mark Salter3c7f2552014-04-15 22:47:52 -0400274 status = handle_cmdline_files(sys_table, image, cmdline_ptr,
275 "dtb=",
Ard Biesheuvela6433752015-03-04 13:02:29 +0100276 ~0UL, &fdt_addr, &fdt_size);
Mark Salter3c7f2552014-04-15 22:47:52 -0400277
278 if (status != EFI_SUCCESS) {
279 pr_efi_err(sys_table, "Failed to load device tree!\n");
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +0100280 goto fail_free_image;
Mark Salter3c7f2552014-04-15 22:47:52 -0400281 }
282 }
Mark Rutland0bcaa902014-10-23 16:33:33 +0100283
284 if (fdt_addr) {
285 pr_efi(sys_table, "Using DTB from command line\n");
286 } else {
Ard Biesheuvel345c7362014-04-03 17:46:58 +0200287 /* Look for a device tree configuration table entry. */
Ard Biesheuvela6433752015-03-04 13:02:29 +0100288 fdt_addr = (uintptr_t)get_fdt(sys_table, &fdt_size);
Mark Rutland0bcaa902014-10-23 16:33:33 +0100289 if (fdt_addr)
290 pr_efi(sys_table, "Using DTB from configuration table\n");
291 }
292
293 if (!fdt_addr)
294 pr_efi(sys_table, "Generating empty DTB\n");
Mark Salter3c7f2552014-04-15 22:47:52 -0400295
296 status = handle_cmdline_files(sys_table, image, cmdline_ptr,
297 "initrd=", dram_base + SZ_512M,
298 (unsigned long *)&initrd_addr,
299 (unsigned long *)&initrd_size);
300 if (status != EFI_SUCCESS)
301 pr_efi_err(sys_table, "Failed initrd from command line!\n");
302
303 new_fdt_addr = fdt_addr;
304 status = allocate_new_fdt_and_exit_boot(sys_table, handle,
305 &new_fdt_addr, dram_base + MAX_FDT_OFFSET,
306 initrd_addr, initrd_size, cmdline_ptr,
307 fdt_addr, fdt_size);
308
309 /*
310 * If all went well, we need to return the FDT address to the
311 * calling function so it can be passed to kernel as part of
312 * the kernel boot protocol.
313 */
314 if (status == EFI_SUCCESS)
315 return new_fdt_addr;
316
317 pr_efi_err(sys_table, "Failed to update FDT and exit boot services\n");
318
319 efi_free(sys_table, initrd_size, initrd_addr);
320 efi_free(sys_table, fdt_size, fdt_addr);
321
Mark Salter3c7f2552014-04-15 22:47:52 -0400322fail_free_image:
323 efi_free(sys_table, image_size, *image_addr);
324 efi_free(sys_table, reserve_size, reserve_addr);
Ard Biesheuvel2b5fe072016-01-26 14:48:29 +0100325fail_free_cmdline:
326 efi_free(sys_table, cmdline_size, (unsigned long)cmdline_ptr);
Mark Salter3c7f2552014-04-15 22:47:52 -0400327fail:
328 return EFI_ERROR;
329}
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200330
331/*
332 * This is the base address at which to start allocating virtual memory ranges
333 * for UEFI Runtime Services. This is in the low TTBR0 range so that we can use
334 * any allocation we choose, and eliminate the risk of a conflict after kexec.
335 * The value chosen is the largest non-zero power of 2 suitable for this purpose
336 * both on 32-bit and 64-bit ARM CPUs, to maximize the likelihood that it can
337 * be mapped efficiently.
Roy Franz81a0bc32015-09-23 20:17:54 -0700338 * Since 32-bit ARM could potentially execute with a 1G/3G user/kernel split,
339 * map everything below 1 GB.
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200340 */
Roy Franz81a0bc32015-09-23 20:17:54 -0700341#define EFI_RT_VIRTUAL_BASE SZ_512M
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200342
Ard Biesheuvel0ce3cc02015-09-25 23:02:19 +0100343static int cmp_mem_desc(const void *l, const void *r)
344{
345 const efi_memory_desc_t *left = l, *right = r;
346
347 return (left->phys_addr > right->phys_addr) ? 1 : -1;
348}
349
350/*
351 * Returns whether region @left ends exactly where region @right starts,
352 * or false if either argument is NULL.
353 */
354static bool regions_are_adjacent(efi_memory_desc_t *left,
355 efi_memory_desc_t *right)
356{
357 u64 left_end;
358
359 if (left == NULL || right == NULL)
360 return false;
361
362 left_end = left->phys_addr + left->num_pages * EFI_PAGE_SIZE;
363
364 return left_end == right->phys_addr;
365}
366
367/*
368 * Returns whether region @left and region @right have compatible memory type
369 * mapping attributes, and are both EFI_MEMORY_RUNTIME regions.
370 */
371static bool regions_have_compatible_memory_type_attrs(efi_memory_desc_t *left,
372 efi_memory_desc_t *right)
373{
374 static const u64 mem_type_mask = EFI_MEMORY_WB | EFI_MEMORY_WT |
375 EFI_MEMORY_WC | EFI_MEMORY_UC |
376 EFI_MEMORY_RUNTIME;
377
378 return ((left->attribute ^ right->attribute) & mem_type_mask) == 0;
379}
380
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200381/*
382 * efi_get_virtmap() - create a virtual mapping for the EFI memory map
383 *
384 * This function populates the virt_addr fields of all memory region descriptors
385 * in @memory_map whose EFI_MEMORY_RUNTIME attribute is set. Those descriptors
386 * are also copied to @runtime_map, and their total count is returned in @count.
387 */
388void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
389 unsigned long desc_size, efi_memory_desc_t *runtime_map,
390 int *count)
391{
392 u64 efi_virt_base = EFI_RT_VIRTUAL_BASE;
Ard Biesheuvel0ce3cc02015-09-25 23:02:19 +0100393 efi_memory_desc_t *in, *prev = NULL, *out = runtime_map;
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200394 int l;
395
Ard Biesheuvel0ce3cc02015-09-25 23:02:19 +0100396 /*
397 * To work around potential issues with the Properties Table feature
398 * introduced in UEFI 2.5, which may split PE/COFF executable images
399 * in memory into several RuntimeServicesCode and RuntimeServicesData
400 * regions, we need to preserve the relative offsets between adjacent
401 * EFI_MEMORY_RUNTIME regions with the same memory type attributes.
402 * The easiest way to find adjacent regions is to sort the memory map
403 * before traversing it.
404 */
405 sort(memory_map, map_size / desc_size, desc_size, cmp_mem_desc, NULL);
406
407 for (l = 0; l < map_size; l += desc_size, prev = in) {
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200408 u64 paddr, size;
409
Ard Biesheuvel0ce3cc02015-09-25 23:02:19 +0100410 in = (void *)memory_map + l;
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200411 if (!(in->attribute & EFI_MEMORY_RUNTIME))
412 continue;
413
Ard Biesheuvel0ce3cc02015-09-25 23:02:19 +0100414 paddr = in->phys_addr;
415 size = in->num_pages * EFI_PAGE_SIZE;
416
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200417 /*
418 * Make the mapping compatible with 64k pages: this allows
419 * a 4k page size kernel to kexec a 64k page size kernel and
420 * vice versa.
421 */
Ard Biesheuvel0ce3cc02015-09-25 23:02:19 +0100422 if (!regions_are_adjacent(prev, in) ||
423 !regions_have_compatible_memory_type_attrs(prev, in)) {
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200424
Ard Biesheuvel0ce3cc02015-09-25 23:02:19 +0100425 paddr = round_down(in->phys_addr, SZ_64K);
426 size += in->phys_addr - paddr;
427
428 /*
429 * Avoid wasting memory on PTEs by choosing a virtual
430 * base that is compatible with section mappings if this
431 * region has the appropriate size and physical
432 * alignment. (Sections are 2 MB on 4k granule kernels)
433 */
434 if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M)
435 efi_virt_base = round_up(efi_virt_base, SZ_2M);
436 else
437 efi_virt_base = round_up(efi_virt_base, SZ_64K);
438 }
Ard Biesheuvelf3cdfd22014-10-20 16:27:26 +0200439
440 in->virt_addr = efi_virt_base + in->phys_addr - paddr;
441 efi_virt_base += size;
442
443 memcpy(out, in, desc_size);
444 out = (void *)out + desc_size;
445 ++*count;
446 }
447}