]> git.ipfire.org Git - thirdparty/u-boot.git/blame - cmd/bootefi.c
Merge tag 'video-for-2019.07-rc1' of git://git.denx.de/u-boot-video
[thirdparty/u-boot.git] / cmd / bootefi.c
CommitLineData
f739fcd8 1// SPDX-License-Identifier: GPL-2.0+
b9939336
AG
2/*
3 * EFI application loader
4 *
5 * Copyright (c) 2016 Alexander Graf
b9939336
AG
6 */
7
8#include <common.h>
f6c6df7e
HS
9#include <bootm.h>
10#include <charset.h>
b9939336 11#include <command.h>
9d922450 12#include <dm.h>
b9939336 13#include <efi_loader.h>
d78e40d6 14#include <efi_selftest.h>
b9939336 15#include <errno.h>
b08c8c48
MY
16#include <linux/libfdt.h>
17#include <linux/libfdt_env.h>
354264b3 18#include <mapmem.h>
ad0c1a3d 19#include <memalign.h>
0d9d501f 20#include <asm/global_data.h>
e275458c 21#include <asm-generic/sections.h>
c3b11dea 22#include <asm-generic/unaligned.h>
e275458c 23#include <linux/linkage.h>
0d9d501f
AG
24
25DECLARE_GLOBAL_DATA_PTR;
b9939336 26
95c5553e
RC
27static struct efi_device_path *bootefi_image_path;
28static struct efi_device_path *bootefi_device_path;
b9939336 29
c3b11dea
HS
30/*
31 * Allow unaligned memory access.
32 *
33 * This routine is overridden by architectures providing this feature.
34 */
35void __weak allow_unaligned(void)
36{
37}
38
d78e40d6
HS
39/*
40 * Set the load options of an image from an environment variable.
41 *
42 * @loaded_image_info: the image
43 * @env_var: name of the environment variable
44 */
45static void set_load_options(struct efi_loaded_image *loaded_image_info,
46 const char *env_var)
47{
48 size_t size;
49 const char *env = env_get(env_var);
7086a71a 50 u16 *pos;
d78e40d6
HS
51
52 loaded_image_info->load_options = NULL;
53 loaded_image_info->load_options_size = 0;
54 if (!env)
55 return;
7086a71a 56 size = utf8_utf16_strlen(env) + 1;
d78e40d6
HS
57 loaded_image_info->load_options = calloc(size, sizeof(u16));
58 if (!loaded_image_info->load_options) {
59 printf("ERROR: Out of memory\n");
60 return;
61 }
7086a71a
HS
62 pos = loaded_image_info->load_options;
63 utf8_utf16_strcpy(&pos, env);
d78e40d6
HS
64 loaded_image_info->load_options_size = size * 2;
65}
66
9dff4900
SG
67/**
68 * copy_fdt() - Copy the device tree to a new location available to EFI
69 *
16b615d9 70 * The FDT is copied to a suitable location within the EFI memory map.
c3772ca1 71 * Additional 12 KiB are added to the space in case the device tree needs to be
9dff4900
SG
72 * expanded later with fdt_open_into().
73 *
16b615d9
HS
74 * @fdtp: On entry a pointer to the flattened device tree.
75 * On exit a pointer to the copy of the flattened device tree.
4574d1b3
HS
76 * FDT start
77 * Return: status code
9dff4900 78 */
16b615d9 79static efi_status_t copy_fdt(void **fdtp)
0d9d501f 80{
ad0c1a3d 81 unsigned long fdt_ram_start = -1L, fdt_pages;
9dff4900
SG
82 efi_status_t ret = 0;
83 void *fdt, *new_fdt;
ad0c1a3d 84 u64 new_fdt_addr;
9dff4900 85 uint fdt_size;
ad0c1a3d 86 int i;
0d9d501f 87
9dff4900
SG
88 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
89 u64 ram_start = gd->bd->bi_dram[i].start;
90 u64 ram_size = gd->bd->bi_dram[i].size;
0d9d501f 91
ad0c1a3d
AG
92 if (!ram_size)
93 continue;
94
95 if (ram_start < fdt_ram_start)
96 fdt_ram_start = ram_start;
97 }
98
bc9a638a 99 /*
c3772ca1
HS
100 * Give us at least 12 KiB of breathing room in case the device tree
101 * needs to be expanded later.
bc9a638a 102 */
16b615d9 103 fdt = *fdtp;
c3772ca1
HS
104 fdt_pages = efi_size_in_pages(fdt_totalsize(fdt) + 0x3000);
105 fdt_size = fdt_pages << EFI_PAGE_SHIFT;
ad0c1a3d 106
16b615d9
HS
107 /*
108 * Safe fdt location is at 127 MiB.
109 * On the sandbox convert from the sandbox address space.
110 */
111 new_fdt_addr = (uintptr_t)map_sysmem(fdt_ram_start + 0x7f00000 +
112 fdt_size, 0);
9dff4900
SG
113 ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
114 EFI_RUNTIME_SERVICES_DATA, fdt_pages,
115 &new_fdt_addr);
116 if (ret != EFI_SUCCESS) {
ad0c1a3d 117 /* If we can't put it there, put it somewhere */
a44bffcc 118 new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
9dff4900
SG
119 ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
120 EFI_RUNTIME_SERVICES_DATA, fdt_pages,
121 &new_fdt_addr);
122 if (ret != EFI_SUCCESS) {
85a6e9b3 123 printf("ERROR: Failed to reserve space for FDT\n");
9dff4900 124 goto done;
85a6e9b3 125 }
ad0c1a3d 126 }
16b615d9 127 new_fdt = (void *)(uintptr_t)new_fdt_addr;
0d9d501f
AG
128 memcpy(new_fdt, fdt, fdt_totalsize(fdt));
129 fdt_set_totalsize(new_fdt, fdt_size);
130
16b615d9 131 *fdtp = (void *)(uintptr_t)new_fdt_addr;
9dff4900
SG
132done:
133 return ret;
0d9d501f
AG
134}
135
416e07e2
SG
136/*
137 * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges
138 *
139 * The mem_rsv entries of the FDT are added to the memory map. Any failures are
140 * ignored because this is not critical and we would rather continue to try to
141 * boot.
142 *
143 * @fdt: Pointer to device tree
144 */
145static void efi_carve_out_dt_rsv(void *fdt)
806d2fa8
AG
146{
147 int nr_rsv, i;
148 uint64_t addr, size, pages;
149
150 nr_rsv = fdt_num_mem_rsv(fdt);
151
152 /* Look for an existing entry and add it to the efi mem map. */
153 for (i = 0; i < nr_rsv; i++) {
154 if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0)
155 continue;
156
16b615d9
HS
157 /* Convert from sandbox address space. */
158 addr = (uintptr_t)map_sysmem(addr, 0);
159
c3772ca1 160 pages = efi_size_in_pages(size + (addr & EFI_PAGE_MASK));
23fd84b3 161 addr &= ~EFI_PAGE_MASK;
416e07e2
SG
162 if (!efi_add_memory_map(addr, pages, EFI_RESERVED_MEMORY_TYPE,
163 false))
164 printf("FDT memrsv map %d: Failed to add to map\n", i);
806d2fa8 165 }
806d2fa8
AG
166}
167
9dff4900 168static efi_status_t efi_install_fdt(ulong fdt_addr)
bc4f9133
HS
169{
170 bootm_headers_t img = { 0 };
bc4f9133 171 efi_status_t ret;
9dff4900 172 void *fdt;
bc4f9133 173
9dff4900 174 fdt = map_sysmem(fdt_addr, 0);
bc4f9133
HS
175 if (fdt_check_header(fdt)) {
176 printf("ERROR: invalid device tree\n");
177 return EFI_INVALID_PARAMETER;
178 }
179
0c9ac06a
HS
180 /* Create memory reservation as indicated by the device tree */
181 efi_carve_out_dt_rsv(fdt);
182
bc4f9133 183 /* Prepare fdt for payload */
16b615d9 184 ret = copy_fdt(&fdt);
9dff4900
SG
185 if (ret)
186 return ret;
bc4f9133
HS
187
188 if (image_setup_libfdt(&img, fdt, 0, NULL)) {
189 printf("ERROR: failed to process device tree\n");
190 return EFI_LOAD_ERROR;
191 }
192
193 /* Link to it in the efi tables */
194 ret = efi_install_configuration_table(&efi_guid_fdt, fdt);
195 if (ret != EFI_SUCCESS)
196 return EFI_OUT_OF_RESOURCES;
197
bc4f9133
HS
198 return ret;
199}
200
f4f0f7cb
SG
201static efi_status_t bootefi_run_prepare(const char *load_options_path,
202 struct efi_device_path *device_path,
203 struct efi_device_path *image_path,
204 struct efi_loaded_image_obj **image_objp,
205 struct efi_loaded_image **loaded_image_infop)
206{
207 efi_status_t ret;
208
209 ret = efi_setup_loaded_image(device_path, image_path, image_objp,
210 loaded_image_infop);
211 if (ret != EFI_SUCCESS)
212 return ret;
213
214 /* Transfer environment variable as load options */
215 set_load_options(*loaded_image_infop, load_options_path);
216
217 return 0;
218}
219
5e2f0391
SG
220/**
221 * bootefi_run_finish() - finish up after running an EFI test
222 *
223 * @loaded_image_info: Pointer to a struct which holds the loaded image info
224 * @image_objj: Pointer to a struct which holds the loaded image object
225 */
226static void bootefi_run_finish(struct efi_loaded_image_obj *image_obj,
227 struct efi_loaded_image *loaded_image_info)
228{
229 efi_restore_gd();
230 free(loaded_image_info->load_options);
231 efi_delete_handle(&image_obj->header);
232}
233
c982874e
HS
234/**
235 * do_bootefi_exec() - execute EFI binary
236 *
237 * @efi: address of the binary
238 * @device_path: path of the device from which the binary was loaded
239 * @image_path: device path of the binary
240 * Return: status code
241 *
242 * Load the EFI binary into a newly assigned memory unwinding the relocation
243 * information, install the loaded image protocol, and call the binary.
b9939336 244 */
bc4f9133 245static efi_status_t do_bootefi_exec(void *efi,
3eb0841b
HS
246 struct efi_device_path *device_path,
247 struct efi_device_path *image_path)
b9939336 248{
8887acc6 249 efi_handle_t mem_handle = NULL;
bf19273e 250 struct efi_device_path *memdp = NULL;
45204b10 251 efi_status_t ret;
faea1041 252 struct efi_loaded_image_obj *image_obj = NULL;
c982874e 253 struct efi_loaded_image *loaded_image_info = NULL;
95c5553e 254
bf19273e
RC
255 /*
256 * Special case for efi payload not loaded from disk, such as
257 * 'bootefi hello' or for example payload loaded directly into
e1fec152 258 * memory via JTAG, etc:
bf19273e
RC
259 */
260 if (!device_path && !image_path) {
261 printf("WARNING: using memory device/image path, this may confuse some payloads!\n");
262 /* actual addresses filled in after efi_load_pe() */
0a76ba65 263 memdp = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, 0, 0);
bf19273e 264 device_path = image_path = memdp;
8887acc6
HS
265 /*
266 * Grub expects that the device path of the loaded image is
267 * installed on a handle.
268 */
269 ret = efi_create_handle(&mem_handle);
270 if (ret != EFI_SUCCESS)
5e2f0391 271 return ret; /* TODO: leaks device_path */
8887acc6 272 ret = efi_add_protocol(mem_handle, &efi_guid_device_path,
58bc69d2
AG
273 device_path);
274 if (ret != EFI_SUCCESS)
5e2f0391 275 goto err_add_protocol;
bf19273e
RC
276 } else {
277 assert(device_path && image_path);
278 }
279
f4f0f7cb
SG
280 ret = bootefi_run_prepare("bootargs", device_path, image_path,
281 &image_obj, &loaded_image_info);
282 if (ret)
5e2f0391 283 goto err_prepare;
95c5553e 284
b9939336 285 /* Load the EFI payload */
8f7e2b29
HS
286 ret = efi_load_pe(image_obj, efi, loaded_image_info);
287 if (ret != EFI_SUCCESS)
5e2f0391 288 goto err_prepare;
80a4800e 289
bf19273e
RC
290 if (memdp) {
291 struct efi_device_path_memory *mdp = (void *)memdp;
c982874e
HS
292 mdp->memory_type = loaded_image_info->image_code_type;
293 mdp->start_address = (uintptr_t)loaded_image_info->image_base;
bf19273e 294 mdp->end_address = mdp->start_address +
c982874e 295 loaded_image_info->image_size;
bf19273e
RC
296 }
297
ad644e7c
RC
298 /* we don't support much: */
299 env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported",
300 "{ro,boot}(blob)0000000000000000");
301
b9939336 302 /* Call our payload! */
8f7e2b29 303 debug("%s: Jumping to 0x%p\n", __func__, image_obj->entry);
f69d63fa 304 ret = EFI_CALL(efi_start_image(&image_obj->header, NULL, NULL));
95c5553e 305
5e2f0391 306err_prepare:
95c5553e 307 /* image has returned, loaded-image obj goes *poof*: */
5e2f0391
SG
308 bootefi_run_finish(image_obj, loaded_image_info);
309
310err_add_protocol:
8887acc6
HS
311 if (mem_handle)
312 efi_delete_handle(mem_handle);
95c5553e
RC
313
314 return ret;
b9939336
AG
315}
316
d9717eae
SG
317#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
318/**
319 * bootefi_test_prepare() - prepare to run an EFI test
320 *
1504bb0d 321 * Prepare to run a test as if it were provided by a loaded image.
d9717eae 322 *
1504bb0d
HS
323 * @image_objp: pointer to be set to the loaded image handle
324 * @loaded_image_infop: pointer to be set to the loaded image protocol
325 * @path: dummy file path used to construct the device path
326 * set in the loaded image protocol
327 * @load_options_path: name of a U-Boot environment variable. Its value is
328 * set as load options in the loaded image protocol.
329 * Return: status code
d9717eae
SG
330 */
331static efi_status_t bootefi_test_prepare
332 (struct efi_loaded_image_obj **image_objp,
1504bb0d
HS
333 struct efi_loaded_image **loaded_image_infop, const char *path,
334 const char *load_options_path)
d9717eae 335{
1504bb0d
HS
336 efi_status_t ret;
337
d9717eae 338 /* Construct a dummy device path */
1504bb0d 339 bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, 0, 0);
d9717eae
SG
340 if (!bootefi_device_path)
341 return EFI_OUT_OF_RESOURCES;
1504bb0d 342
d9717eae 343 bootefi_image_path = efi_dp_from_file(NULL, 0, path);
1504bb0d
HS
344 if (!bootefi_image_path) {
345 ret = EFI_OUT_OF_RESOURCES;
346 goto failure;
347 }
348
349 ret = bootefi_run_prepare(load_options_path, bootefi_device_path,
350 bootefi_image_path, image_objp,
351 loaded_image_infop);
352 if (ret == EFI_SUCCESS)
353 return ret;
d9717eae 354
1504bb0d
HS
355 efi_free_pool(bootefi_image_path);
356 bootefi_image_path = NULL;
357failure:
358 efi_free_pool(bootefi_device_path);
359 bootefi_device_path = NULL;
360 return ret;
d9717eae
SG
361}
362
d9717eae
SG
363#endif /* CONFIG_CMD_BOOTEFI_SELFTEST */
364
bc4f9133 365static int do_bootefi_bootmgr_exec(void)
9975fe96
RC
366{
367 struct efi_device_path *device_path, *file_path;
368 void *addr;
369 efi_status_t r;
370
9975fe96
RC
371 addr = efi_bootmgr_load(&device_path, &file_path);
372 if (!addr)
373 return 1;
374
375 printf("## Starting EFI application at %p ...\n", addr);
bc4f9133 376 r = do_bootefi_exec(addr, device_path, file_path);
9975fe96
RC
377 printf("## Application terminated, r = %lu\n",
378 r & ~EFI_ERROR_MASK);
379
380 if (r != EFI_SUCCESS)
381 return 1;
382
383 return 0;
384}
385
b9939336
AG
386/* Interpreter command to boot an arbitrary EFI image from memory */
387static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
388{
bc4f9133
HS
389 unsigned long addr;
390 char *saddr;
3eb0841b 391 efi_status_t r;
354264b3 392 unsigned long fdt_addr;
b9939336 393
c3b11dea
HS
394 /* Allow unaligned memory access */
395 allow_unaligned();
396
f6c6df7e
HS
397 switch_to_non_secure_mode();
398
fc225e60
HS
399 /* Initialize EFI drivers */
400 r = efi_init_obj_list();
401 if (r != EFI_SUCCESS) {
402 printf("Error: Cannot set up EFI drivers, r = %lu\n",
403 r & ~EFI_ERROR_MASK);
404 return CMD_RET_FAILURE;
405 }
406
b9939336 407 if (argc < 2)
3c1dcef6 408 return CMD_RET_USAGE;
bc4f9133
HS
409
410 if (argc > 2) {
354264b3 411 fdt_addr = simple_strtoul(argv[2], NULL, 16);
bc4f9133
HS
412 if (!fdt_addr && *argv[2] != '0')
413 return CMD_RET_USAGE;
414 /* Install device tree */
9dff4900 415 r = efi_install_fdt(fdt_addr);
bc4f9133
HS
416 if (r != EFI_SUCCESS) {
417 printf("ERROR: failed to install device tree\n");
418 return CMD_RET_FAILURE;
419 }
420 } else {
421 /* Remove device tree. EFI_NOT_FOUND can be ignored here */
422 efi_install_configuration_table(&efi_guid_fdt, NULL);
423 printf("WARNING: booting without device tree\n");
424 }
c7ae3dfd
SG
425#ifdef CONFIG_CMD_BOOTEFI_HELLO
426 if (!strcmp(argv[1], "hello")) {
5e44489b 427 ulong size = __efi_helloworld_end - __efi_helloworld_begin;
b9939336 428
51c533fd
HS
429 saddr = env_get("loadaddr");
430 if (saddr)
431 addr = simple_strtoul(saddr, NULL, 16);
432 else
433 addr = CONFIG_SYS_LOAD_ADDR;
354264b3 434 memcpy(map_sysmem(addr, size), __efi_helloworld_begin, size);
c7ae3dfd 435 } else
623b3a57
HS
436#endif
437#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
438 if (!strcmp(argv[1], "selftest")) {
faea1041 439 struct efi_loaded_image_obj *image_obj;
c982874e 440 struct efi_loaded_image *loaded_image_info;
7aca68ca 441
1504bb0d
HS
442 r = bootefi_test_prepare(&image_obj, &loaded_image_info,
443 "\\selftest", "efi_selftest");
444 if (r != EFI_SUCCESS)
2ab7ef74 445 return CMD_RET_FAILURE;
f972dc14 446
d78e40d6 447 /* Execute the test */
914df75b 448 r = EFI_CALL(efi_selftest(&image_obj->header, &systab));
5e2f0391 449 bootefi_run_finish(image_obj, loaded_image_info);
c2b53902 450 return r != EFI_SUCCESS;
623b3a57 451 } else
c7ae3dfd 452#endif
9975fe96 453 if (!strcmp(argv[1], "bootmgr")) {
bc4f9133 454 return do_bootefi_bootmgr_exec();
9975fe96 455 } else {
c7ae3dfd 456 saddr = argv[1];
b9939336 457
c7ae3dfd 458 addr = simple_strtoul(saddr, NULL, 16);
49db1cb8
HS
459 /* Check that a numeric value was passed */
460 if (!addr && *saddr != '0')
461 return CMD_RET_USAGE;
c7ae3dfd 462
1c39809b
AG
463 }
464
5ee31baf 465 printf("## Starting EFI application at %08lx ...\n", addr);
354264b3 466 r = do_bootefi_exec(map_sysmem(addr, 0), bootefi_device_path,
bc4f9133 467 bootefi_image_path);
1da1bac4
HS
468 printf("## Application terminated, r = %lu\n",
469 r & ~EFI_ERROR_MASK);
b9939336 470
1da1bac4
HS
471 if (r != EFI_SUCCESS)
472 return 1;
473 else
474 return 0;
b9939336
AG
475}
476
477#ifdef CONFIG_SYS_LONGHELP
478static char bootefi_help_text[] =
1c39809b
AG
479 "<image address> [fdt address]\n"
480 " - boot EFI payload stored at address <image address>.\n"
481 " If specified, the device tree located at <fdt address> gets\n"
c7ae3dfd
SG
482 " exposed as EFI configuration table.\n"
483#ifdef CONFIG_CMD_BOOTEFI_HELLO
623b3a57
HS
484 "bootefi hello\n"
485 " - boot a sample Hello World application stored within U-Boot\n"
486#endif
487#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
bc4f9133 488 "bootefi selftest [fdt address]\n"
623b3a57 489 " - boot an EFI selftest application stored within U-Boot\n"
d78e40d6
HS
490 " Use environment variable efi_selftest to select a single test.\n"
491 " Use 'setenv efi_selftest list' to enumerate all tests.\n"
c7ae3dfd 492#endif
f623e07f 493 "bootefi bootmgr [fdt addr]\n"
9975fe96
RC
494 " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
495 "\n"
496 " If specified, the device tree located at <fdt address> gets\n"
497 " exposed as EFI configuration table.\n";
b9939336
AG
498#endif
499
500U_BOOT_CMD(
1c39809b 501 bootefi, 3, 0, do_bootefi,
92dfd922 502 "Boots an EFI payload from memory",
b9939336
AG
503 bootefi_help_text
504);
0f4060eb 505
95c5553e
RC
506void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
507{
f1589ffb
AT
508 struct efi_device_path *device, *image;
509 efi_status_t ret;
f9d334bd 510
79276eb2
HS
511 /* efi_set_bootdev is typically called repeatedly, recover memory */
512 efi_free_pool(bootefi_device_path);
513 efi_free_pool(bootefi_image_path);
9975fe96 514
f1589ffb
AT
515 ret = efi_dp_from_name(dev, devnr, path, &device, &image);
516 if (ret == EFI_SUCCESS) {
517 bootefi_device_path = device;
518 bootefi_image_path = image;
49271666 519 } else {
f1589ffb
AT
520 bootefi_device_path = NULL;
521 bootefi_image_path = NULL;
49271666 522 }
0f4060eb 523}