]> git.ipfire.org Git - thirdparty/u-boot.git/blame - cmd/bootefi.c
efi_selftest: allow to select a single test for execution
[thirdparty/u-boot.git] / cmd / bootefi.c
CommitLineData
b9939336
AG
1/*
2 * EFI application loader
3 *
4 * Copyright (c) 2016 Alexander Graf
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
d78e40d6 9#include <charset.h>
b9939336
AG
10#include <common.h>
11#include <command.h>
9d922450 12#include <dm.h>
b9939336 13#include <efi_loader.h>
d78e40d6 14#include <efi_selftest.h>
b9939336
AG
15#include <errno.h>
16#include <libfdt.h>
17#include <libfdt_env.h>
ad0c1a3d 18#include <memalign.h>
0d9d501f 19#include <asm/global_data.h>
e275458c
SG
20#include <asm-generic/sections.h>
21#include <linux/linkage.h>
0d9d501f
AG
22
23DECLARE_GLOBAL_DATA_PTR;
b9939336 24
7cbc1241
HS
25static uint8_t efi_obj_list_initalized;
26
95c5553e
RC
27static struct efi_device_path *bootefi_image_path;
28static struct efi_device_path *bootefi_device_path;
b9939336 29
7cbc1241
HS
30/* Initialize and populate EFI object list */
31static void efi_init_obj_list(void)
32{
33 efi_obj_list_initalized = 1;
34
7cbc1241
HS
35 efi_console_register();
36#ifdef CONFIG_PARTITIONS
37 efi_disk_register();
38#endif
39#if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO)
40 efi_gop_register();
41#endif
42#ifdef CONFIG_NET
95c5553e 43 efi_net_register();
7cbc1241
HS
44#endif
45#ifdef CONFIG_GENERATE_SMBIOS_TABLE
46 efi_smbios_register();
47#endif
b3d60900 48 efi_watchdog_register();
7cbc1241
HS
49
50 /* Initialize EFI runtime services */
51 efi_reset_system_init();
52 efi_get_time_init();
53}
54
d78e40d6
HS
55/*
56 * Set the load options of an image from an environment variable.
57 *
58 * @loaded_image_info: the image
59 * @env_var: name of the environment variable
60 */
61static void set_load_options(struct efi_loaded_image *loaded_image_info,
62 const char *env_var)
63{
64 size_t size;
65 const char *env = env_get(env_var);
66
67 loaded_image_info->load_options = NULL;
68 loaded_image_info->load_options_size = 0;
69 if (!env)
70 return;
71 size = strlen(env) + 1;
72 loaded_image_info->load_options = calloc(size, sizeof(u16));
73 if (!loaded_image_info->load_options) {
74 printf("ERROR: Out of memory\n");
75 return;
76 }
77 utf8_to_utf16(loaded_image_info->load_options, (u8 *)env, size);
78 loaded_image_info->load_options_size = size * 2;
79}
80
0d9d501f
AG
81static void *copy_fdt(void *fdt)
82{
83 u64 fdt_size = fdt_totalsize(fdt);
ad0c1a3d
AG
84 unsigned long fdt_ram_start = -1L, fdt_pages;
85 u64 new_fdt_addr;
0d9d501f 86 void *new_fdt;
ad0c1a3d 87 int i;
0d9d501f 88
ad0c1a3d
AG
89 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
90 u64 ram_start = gd->bd->bi_dram[i].start;
91 u64 ram_size = gd->bd->bi_dram[i].size;
0d9d501f 92
ad0c1a3d
AG
93 if (!ram_size)
94 continue;
95
96 if (ram_start < fdt_ram_start)
97 fdt_ram_start = ram_start;
98 }
99
100 /* Give us at least 4kb breathing room */
a44bffcc 101 fdt_size = ALIGN(fdt_size + 4096, EFI_PAGE_SIZE);
ad0c1a3d
AG
102 fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
103
104 /* Safe fdt location is at 128MB */
105 new_fdt_addr = fdt_ram_start + (128 * 1024 * 1024) + fdt_size;
106 if (efi_allocate_pages(1, EFI_BOOT_SERVICES_DATA, fdt_pages,
107 &new_fdt_addr) != EFI_SUCCESS) {
108 /* If we can't put it there, put it somewhere */
a44bffcc 109 new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
85a6e9b3
AG
110 if (efi_allocate_pages(1, EFI_BOOT_SERVICES_DATA, fdt_pages,
111 &new_fdt_addr) != EFI_SUCCESS) {
112 printf("ERROR: Failed to reserve space for FDT\n");
113 return NULL;
114 }
ad0c1a3d 115 }
85a6e9b3 116
ad0c1a3d 117 new_fdt = (void*)(ulong)new_fdt_addr;
0d9d501f
AG
118 memcpy(new_fdt, fdt, fdt_totalsize(fdt));
119 fdt_set_totalsize(new_fdt, fdt_size);
120
121 return new_fdt;
122}
123
3eb0841b
HS
124static efi_status_t efi_do_enter(
125 void *image_handle, struct efi_system_table *st,
126 asmlinkage ulong (*entry)(void *image_handle,
127 struct efi_system_table *st))
b06d8ac3
HS
128{
129 efi_status_t ret = EFI_LOAD_ERROR;
130
131 if (entry)
132 ret = entry(image_handle, st);
133 st->boottime->exit(image_handle, ret, 0, NULL);
134 return ret;
135}
136
ec6617c3 137#ifdef CONFIG_ARM64
3eb0841b 138static efi_status_t efi_run_in_el2(asmlinkage ulong (*entry)(
b06d8ac3
HS
139 void *image_handle, struct efi_system_table *st),
140 void *image_handle, struct efi_system_table *st)
ec6617c3
AW
141{
142 /* Enable caches again */
143 dcache_enable();
144
b06d8ac3 145 return efi_do_enter(image_handle, st, entry);
ec6617c3
AW
146}
147#endif
148
b9939336
AG
149/*
150 * Load an EFI payload into a newly allocated piece of memory, register all
151 * EFI objects it would want to access and jump to it.
152 */
3eb0841b
HS
153static efi_status_t do_bootefi_exec(void *efi, void *fdt,
154 struct efi_device_path *device_path,
155 struct efi_device_path *image_path)
b9939336 156{
95c5553e
RC
157 struct efi_loaded_image loaded_image_info = {};
158 struct efi_object loaded_image_info_obj = {};
bf19273e 159 struct efi_device_path *memdp = NULL;
95c5553e
RC
160 ulong ret;
161
e275458c
SG
162 ulong (*entry)(void *image_handle, struct efi_system_table *st)
163 asmlinkage;
b9939336 164 ulong fdt_pages, fdt_size, fdt_start, fdt_end;
f4f9993f 165 const efi_guid_t fdt_guid = EFI_FDT_GUID;
dea2174d 166 bootm_headers_t img = { 0 };
b9939336 167
bf19273e
RC
168 /*
169 * Special case for efi payload not loaded from disk, such as
170 * 'bootefi hello' or for example payload loaded directly into
171 * memory via jtag/etc:
172 */
173 if (!device_path && !image_path) {
174 printf("WARNING: using memory device/image path, this may confuse some payloads!\n");
175 /* actual addresses filled in after efi_load_pe() */
176 memdp = efi_dp_from_mem(0, 0, 0);
177 device_path = image_path = memdp;
178 } else {
179 assert(device_path && image_path);
180 }
181
95c5553e
RC
182 /* Initialize and populate EFI object list */
183 if (!efi_obj_list_initalized)
184 efi_init_obj_list();
185
186 efi_setup_loaded_image(&loaded_image_info, &loaded_image_info_obj,
187 device_path, image_path);
188
b9939336
AG
189 /*
190 * gd lives in a fixed register which may get clobbered while we execute
191 * the payload. So save it here and restore it on every callback entry
192 */
193 efi_save_gd();
194
1c39809b 195 if (fdt && !fdt_check_header(fdt)) {
dea2174d 196 /* Prepare fdt for payload */
0d9d501f
AG
197 fdt = copy_fdt(fdt);
198
199 if (image_setup_libfdt(&img, fdt, 0, NULL)) {
dea2174d
AG
200 printf("ERROR: Failed to process device tree\n");
201 return -EINVAL;
202 }
203
204 /* Link to it in the efi tables */
f4f9993f 205 efi_install_configuration_table(&fdt_guid, fdt);
b9939336
AG
206
207 /* And reserve the space in the memory map */
0d9d501f
AG
208 fdt_start = ((ulong)fdt) & ~EFI_PAGE_MASK;
209 fdt_end = ((ulong)fdt) + fdt_totalsize(fdt);
b9939336
AG
210 fdt_size = (fdt_end - fdt_start) + EFI_PAGE_MASK;
211 fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
212 /* Give a bootloader the chance to modify the device tree */
213 fdt_pages += 2;
214 efi_add_memory_map(fdt_start, fdt_pages,
215 EFI_BOOT_SERVICES_DATA, true);
b9939336 216 } else {
1c39809b 217 printf("WARNING: Invalid device tree, expect boot to fail\n");
f4f9993f 218 efi_install_configuration_table(&fdt_guid, NULL);
b9939336
AG
219 }
220
221 /* Load the EFI payload */
222 entry = efi_load_pe(efi, &loaded_image_info);
95c5553e
RC
223 if (!entry) {
224 ret = -ENOENT;
225 goto exit;
226 }
80a4800e 227
bf19273e
RC
228 if (memdp) {
229 struct efi_device_path_memory *mdp = (void *)memdp;
230 mdp->memory_type = loaded_image_info.image_code_type;
231 mdp->start_address = (uintptr_t)loaded_image_info.image_base;
232 mdp->end_address = mdp->start_address +
233 loaded_image_info.image_size;
234 }
235
ad644e7c
RC
236 /* we don't support much: */
237 env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported",
238 "{ro,boot}(blob)0000000000000000");
239
b9939336 240 /* Call our payload! */
edcef3ba 241 debug("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry);
a86aeaf2
AG
242
243 if (setjmp(&loaded_image_info.exit_jmp)) {
95c5553e 244 ret = loaded_image_info.exit_status;
95c5553e 245 goto exit;
a86aeaf2
AG
246 }
247
69bd459d
AG
248#ifdef CONFIG_ARM64
249 /* On AArch64 we need to make sure we call our payload in < EL3 */
250 if (current_el() == 3) {
251 smp_kick_all_cpus();
252 dcache_disable(); /* flush cache before switch to EL2 */
ec6617c3
AW
253
254 /* Move into EL2 and keep running there */
255 armv8_switch_to_el2((ulong)entry, (ulong)&loaded_image_info,
7c5e1feb 256 (ulong)&systab, 0, (ulong)efi_run_in_el2,
ec6617c3
AW
257 ES_TO_AARCH64);
258
259 /* Should never reach here, efi exits with longjmp */
260 while (1) { }
69bd459d
AG
261 }
262#endif
263
95c5553e
RC
264 ret = efi_do_enter(&loaded_image_info, &systab, entry);
265
266exit:
267 /* image has returned, loaded-image obj goes *poof*: */
268 list_del(&loaded_image_info_obj.link);
269
270 return ret;
b9939336
AG
271}
272
9975fe96
RC
273static int do_bootefi_bootmgr_exec(unsigned long fdt_addr)
274{
275 struct efi_device_path *device_path, *file_path;
276 void *addr;
277 efi_status_t r;
278
279 /* Initialize and populate EFI object list */
280 if (!efi_obj_list_initalized)
281 efi_init_obj_list();
282
283 /*
284 * gd lives in a fixed register which may get clobbered while we execute
285 * the payload. So save it here and restore it on every callback entry
286 */
287 efi_save_gd();
288
289 addr = efi_bootmgr_load(&device_path, &file_path);
290 if (!addr)
291 return 1;
292
293 printf("## Starting EFI application at %p ...\n", addr);
294 r = do_bootefi_exec(addr, (void *)fdt_addr, device_path, file_path);
295 printf("## Application terminated, r = %lu\n",
296 r & ~EFI_ERROR_MASK);
297
298 if (r != EFI_SUCCESS)
299 return 1;
300
301 return 0;
302}
303
b9939336
AG
304/* Interpreter command to boot an arbitrary EFI image from memory */
305static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
306{
1c39809b
AG
307 char *saddr, *sfdt;
308 unsigned long addr, fdt_addr = 0;
3eb0841b 309 efi_status_t r;
b9939336
AG
310
311 if (argc < 2)
3c1dcef6 312 return CMD_RET_USAGE;
c7ae3dfd
SG
313#ifdef CONFIG_CMD_BOOTEFI_HELLO
314 if (!strcmp(argv[1], "hello")) {
5e44489b 315 ulong size = __efi_helloworld_end - __efi_helloworld_begin;
b9939336 316
51c533fd
HS
317 saddr = env_get("loadaddr");
318 if (saddr)
319 addr = simple_strtoul(saddr, NULL, 16);
320 else
321 addr = CONFIG_SYS_LOAD_ADDR;
5e44489b 322 memcpy((char *)addr, __efi_helloworld_begin, size);
c7ae3dfd 323 } else
623b3a57
HS
324#endif
325#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
326 if (!strcmp(argv[1], "selftest")) {
7aca68ca
HS
327 struct efi_loaded_image loaded_image_info = {};
328 struct efi_object loaded_image_info_obj = {};
329
f972dc14
HS
330 /* Construct a dummy device path. */
331 bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
332 (uintptr_t)&efi_selftest,
333 (uintptr_t)&efi_selftest);
334 bootefi_image_path = efi_dp_from_file(NULL, 0, "\\selftest");
335
7aca68ca
HS
336 efi_setup_loaded_image(&loaded_image_info,
337 &loaded_image_info_obj,
338 bootefi_device_path, bootefi_image_path);
623b3a57
HS
339 /*
340 * gd lives in a fixed register which may get clobbered while we
341 * execute the payload. So save it here and restore it on every
342 * callback entry
343 */
344 efi_save_gd();
345 /* Initialize and populate EFI object list */
346 if (!efi_obj_list_initalized)
347 efi_init_obj_list();
d78e40d6
HS
348 /* Transfer environment variable efi_selftest as load options */
349 set_load_options(&loaded_image_info, "efi_selftest");
350 /* Execute the test */
351 r = efi_selftest(&loaded_image_info, &systab);
352 free(loaded_image_info.load_options);
353 return r;
623b3a57 354 } else
c7ae3dfd 355#endif
9975fe96
RC
356 if (!strcmp(argv[1], "bootmgr")) {
357 unsigned long fdt_addr = 0;
358
359 if (argc > 2)
360 fdt_addr = simple_strtoul(argv[2], NULL, 16);
361
362 return do_bootefi_bootmgr_exec(fdt_addr);
363 } else {
c7ae3dfd 364 saddr = argv[1];
b9939336 365
c7ae3dfd
SG
366 addr = simple_strtoul(saddr, NULL, 16);
367
368 if (argc > 2) {
369 sfdt = argv[2];
370 fdt_addr = simple_strtoul(sfdt, NULL, 16);
371 }
1c39809b
AG
372 }
373
5ee31baf 374 printf("## Starting EFI application at %08lx ...\n", addr);
95c5553e
RC
375 r = do_bootefi_exec((void *)addr, (void *)fdt_addr,
376 bootefi_device_path, bootefi_image_path);
1da1bac4
HS
377 printf("## Application terminated, r = %lu\n",
378 r & ~EFI_ERROR_MASK);
b9939336 379
1da1bac4
HS
380 if (r != EFI_SUCCESS)
381 return 1;
382 else
383 return 0;
b9939336
AG
384}
385
386#ifdef CONFIG_SYS_LONGHELP
387static char bootefi_help_text[] =
1c39809b
AG
388 "<image address> [fdt address]\n"
389 " - boot EFI payload stored at address <image address>.\n"
390 " If specified, the device tree located at <fdt address> gets\n"
c7ae3dfd
SG
391 " exposed as EFI configuration table.\n"
392#ifdef CONFIG_CMD_BOOTEFI_HELLO
623b3a57
HS
393 "bootefi hello\n"
394 " - boot a sample Hello World application stored within U-Boot\n"
395#endif
396#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
397 "bootefi selftest\n"
398 " - boot an EFI selftest application stored within U-Boot\n"
d78e40d6
HS
399 " Use environment variable efi_selftest to select a single test.\n"
400 " Use 'setenv efi_selftest list' to enumerate all tests.\n"
c7ae3dfd 401#endif
9975fe96
RC
402 "bootmgr [fdt addr]\n"
403 " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
404 "\n"
405 " If specified, the device tree located at <fdt address> gets\n"
406 " exposed as EFI configuration table.\n";
b9939336
AG
407#endif
408
409U_BOOT_CMD(
1c39809b 410 bootefi, 3, 0, do_bootefi,
92dfd922 411 "Boots an EFI payload from memory",
b9939336
AG
412 bootefi_help_text
413);
0f4060eb 414
95c5553e 415static int parse_partnum(const char *devnr)
0f4060eb 416{
95c5553e
RC
417 const char *str = strchr(devnr, ':');
418 if (str) {
419 str++;
420 return simple_strtoul(str, NULL, 16);
f9d334bd 421 }
95c5553e
RC
422 return 0;
423}
f9d334bd 424
95c5553e
RC
425void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
426{
427 char filename[32] = { 0 }; /* dp->str is u16[32] long */
428 char *s;
f9d334bd 429
95c5553e
RC
430 if (strcmp(dev, "Net")) {
431 struct blk_desc *desc;
432 int part;
8c3df0bf 433
95c5553e
RC
434 desc = blk_get_dev(dev, simple_strtol(devnr, NULL, 10));
435 part = parse_partnum(devnr);
0f4060eb 436
95c5553e
RC
437 bootefi_device_path = efi_dp_from_part(desc, part);
438 } else {
439#ifdef CONFIG_NET
440 bootefi_device_path = efi_dp_from_eth();
441#endif
442 }
c07ad7c0 443
9975fe96
RC
444 if (!path)
445 return;
446
49271666
AG
447 if (strcmp(dev, "Net")) {
448 /* Add leading / to fs paths, because they're absolute */
95c5553e 449 snprintf(filename, sizeof(filename), "/%s", path);
49271666 450 } else {
95c5553e 451 snprintf(filename, sizeof(filename), "%s", path);
49271666 452 }
3e433e96 453 /* DOS style file path: */
95c5553e 454 s = filename;
3e433e96
RC
455 while ((s = strchr(s, '/')))
456 *s++ = '\\';
95c5553e 457 bootefi_image_path = efi_dp_from_file(NULL, 0, filename);
0f4060eb 458}