blob: b7087e3da874e7f14745dc63dfbf62458bf80795 [file] [log] [blame]
Alexander Grafb9939332016-03-10 00:27:20 +01001/*
2 * EFI application loader
3 *
4 * Copyright (c) 2016 Alexander Graf
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9#include <common.h>
10#include <command.h>
Simon Glass9d922452017-05-17 17:18:03 -060011#include <dm.h>
Alexander Grafb9939332016-03-10 00:27:20 +010012#include <efi_loader.h>
13#include <errno.h>
14#include <libfdt.h>
15#include <libfdt_env.h>
Alexander Grafad0c1a32016-04-11 23:51:01 +020016#include <memalign.h>
Alexander Graf0d9d5012016-04-11 16:55:26 +020017#include <asm/global_data.h>
Simon Glasse2754582016-09-25 15:27:32 -060018#include <asm-generic/sections.h>
19#include <linux/linkage.h>
Alexander Graf0d9d5012016-04-11 16:55:26 +020020
21DECLARE_GLOBAL_DATA_PTR;
Alexander Grafb9939332016-03-10 00:27:20 +010022
Heinrich Schuchardt7cbc1242017-07-19 19:37:22 +020023static uint8_t efi_obj_list_initalized;
24
Rob Clark95c55532017-09-13 18:05:33 -040025static struct efi_device_path *bootefi_image_path;
26static struct efi_device_path *bootefi_device_path;
Alexander Grafb9939332016-03-10 00:27:20 +010027
Heinrich Schuchardt7cbc1242017-07-19 19:37:22 +020028/* Initialize and populate EFI object list */
29static void efi_init_obj_list(void)
30{
31 efi_obj_list_initalized = 1;
32
Heinrich Schuchardt7cbc1242017-07-19 19:37:22 +020033 efi_console_register();
34#ifdef CONFIG_PARTITIONS
35 efi_disk_register();
36#endif
37#if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO)
38 efi_gop_register();
39#endif
40#ifdef CONFIG_NET
Rob Clark95c55532017-09-13 18:05:33 -040041 efi_net_register();
Heinrich Schuchardt7cbc1242017-07-19 19:37:22 +020042#endif
43#ifdef CONFIG_GENERATE_SMBIOS_TABLE
44 efi_smbios_register();
45#endif
46
47 /* Initialize EFI runtime services */
48 efi_reset_system_init();
49 efi_get_time_init();
50}
51
Alexander Graf0d9d5012016-04-11 16:55:26 +020052static void *copy_fdt(void *fdt)
53{
54 u64 fdt_size = fdt_totalsize(fdt);
Alexander Grafad0c1a32016-04-11 23:51:01 +020055 unsigned long fdt_ram_start = -1L, fdt_pages;
56 u64 new_fdt_addr;
Alexander Graf0d9d5012016-04-11 16:55:26 +020057 void *new_fdt;
Alexander Grafad0c1a32016-04-11 23:51:01 +020058 int i;
Alexander Graf0d9d5012016-04-11 16:55:26 +020059
Alexander Grafad0c1a32016-04-11 23:51:01 +020060 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
61 u64 ram_start = gd->bd->bi_dram[i].start;
62 u64 ram_size = gd->bd->bi_dram[i].size;
Alexander Graf0d9d5012016-04-11 16:55:26 +020063
Alexander Grafad0c1a32016-04-11 23:51:01 +020064 if (!ram_size)
65 continue;
66
67 if (ram_start < fdt_ram_start)
68 fdt_ram_start = ram_start;
69 }
70
71 /* Give us at least 4kb breathing room */
xypron.glpk@gmx.dea44bffc2017-08-11 21:19:25 +020072 fdt_size = ALIGN(fdt_size + 4096, EFI_PAGE_SIZE);
Alexander Grafad0c1a32016-04-11 23:51:01 +020073 fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
74
75 /* Safe fdt location is at 128MB */
76 new_fdt_addr = fdt_ram_start + (128 * 1024 * 1024) + fdt_size;
77 if (efi_allocate_pages(1, EFI_BOOT_SERVICES_DATA, fdt_pages,
78 &new_fdt_addr) != EFI_SUCCESS) {
79 /* If we can't put it there, put it somewhere */
xypron.glpk@gmx.dea44bffc2017-08-11 21:19:25 +020080 new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
Alexander Graf85a6e9b2017-07-03 13:32:35 +020081 if (efi_allocate_pages(1, EFI_BOOT_SERVICES_DATA, fdt_pages,
82 &new_fdt_addr) != EFI_SUCCESS) {
83 printf("ERROR: Failed to reserve space for FDT\n");
84 return NULL;
85 }
Alexander Grafad0c1a32016-04-11 23:51:01 +020086 }
Alexander Graf85a6e9b2017-07-03 13:32:35 +020087
Alexander Grafad0c1a32016-04-11 23:51:01 +020088 new_fdt = (void*)(ulong)new_fdt_addr;
Alexander Graf0d9d5012016-04-11 16:55:26 +020089 memcpy(new_fdt, fdt, fdt_totalsize(fdt));
90 fdt_set_totalsize(new_fdt, fdt_size);
91
92 return new_fdt;
93}
94
xypron.glpk@gmx.deb06d8ac2017-07-04 23:15:21 +020095static ulong efi_do_enter(void *image_handle,
96 struct efi_system_table *st,
97 asmlinkage ulong (*entry)(void *image_handle,
98 struct efi_system_table *st))
99{
100 efi_status_t ret = EFI_LOAD_ERROR;
101
102 if (entry)
103 ret = entry(image_handle, st);
104 st->boottime->exit(image_handle, ret, 0, NULL);
105 return ret;
106}
107
Alison Wangec6617c2016-11-10 10:49:03 +0800108#ifdef CONFIG_ARM64
xypron.glpk@gmx.deb06d8ac2017-07-04 23:15:21 +0200109static unsigned long efi_run_in_el2(asmlinkage ulong (*entry)(
110 void *image_handle, struct efi_system_table *st),
111 void *image_handle, struct efi_system_table *st)
Alison Wangec6617c2016-11-10 10:49:03 +0800112{
113 /* Enable caches again */
114 dcache_enable();
115
xypron.glpk@gmx.deb06d8ac2017-07-04 23:15:21 +0200116 return efi_do_enter(image_handle, st, entry);
Alison Wangec6617c2016-11-10 10:49:03 +0800117}
118#endif
119
Alexander Grafb9939332016-03-10 00:27:20 +0100120/*
121 * Load an EFI payload into a newly allocated piece of memory, register all
122 * EFI objects it would want to access and jump to it.
123 */
Rob Clark95c55532017-09-13 18:05:33 -0400124static unsigned long do_bootefi_exec(void *efi, void *fdt,
125 struct efi_device_path *device_path,
126 struct efi_device_path *image_path)
Alexander Grafb9939332016-03-10 00:27:20 +0100127{
Rob Clark95c55532017-09-13 18:05:33 -0400128 struct efi_loaded_image loaded_image_info = {};
129 struct efi_object loaded_image_info_obj = {};
130 ulong ret;
131
Simon Glasse2754582016-09-25 15:27:32 -0600132 ulong (*entry)(void *image_handle, struct efi_system_table *st)
133 asmlinkage;
Alexander Grafb9939332016-03-10 00:27:20 +0100134 ulong fdt_pages, fdt_size, fdt_start, fdt_end;
Alexander Graff4f99932017-07-26 13:41:05 +0200135 const efi_guid_t fdt_guid = EFI_FDT_GUID;
Alexander Grafdea21742016-03-04 01:10:13 +0100136 bootm_headers_t img = { 0 };
Alexander Grafb9939332016-03-10 00:27:20 +0100137
Rob Clark95c55532017-09-13 18:05:33 -0400138 /* Initialize and populate EFI object list */
139 if (!efi_obj_list_initalized)
140 efi_init_obj_list();
141
142 efi_setup_loaded_image(&loaded_image_info, &loaded_image_info_obj,
143 device_path, image_path);
144
Alexander Grafb9939332016-03-10 00:27:20 +0100145 /*
146 * gd lives in a fixed register which may get clobbered while we execute
147 * the payload. So save it here and restore it on every callback entry
148 */
149 efi_save_gd();
150
Alexander Graf1c398092016-04-14 16:07:53 +0200151 if (fdt && !fdt_check_header(fdt)) {
Alexander Grafdea21742016-03-04 01:10:13 +0100152 /* Prepare fdt for payload */
Alexander Graf0d9d5012016-04-11 16:55:26 +0200153 fdt = copy_fdt(fdt);
154
155 if (image_setup_libfdt(&img, fdt, 0, NULL)) {
Alexander Grafdea21742016-03-04 01:10:13 +0100156 printf("ERROR: Failed to process device tree\n");
157 return -EINVAL;
158 }
159
160 /* Link to it in the efi tables */
Alexander Graff4f99932017-07-26 13:41:05 +0200161 efi_install_configuration_table(&fdt_guid, fdt);
Alexander Grafb9939332016-03-10 00:27:20 +0100162
163 /* And reserve the space in the memory map */
Alexander Graf0d9d5012016-04-11 16:55:26 +0200164 fdt_start = ((ulong)fdt) & ~EFI_PAGE_MASK;
165 fdt_end = ((ulong)fdt) + fdt_totalsize(fdt);
Alexander Grafb9939332016-03-10 00:27:20 +0100166 fdt_size = (fdt_end - fdt_start) + EFI_PAGE_MASK;
167 fdt_pages = fdt_size >> EFI_PAGE_SHIFT;
168 /* Give a bootloader the chance to modify the device tree */
169 fdt_pages += 2;
170 efi_add_memory_map(fdt_start, fdt_pages,
171 EFI_BOOT_SERVICES_DATA, true);
Alexander Grafb9939332016-03-10 00:27:20 +0100172 } else {
Alexander Graf1c398092016-04-14 16:07:53 +0200173 printf("WARNING: Invalid device tree, expect boot to fail\n");
Alexander Graff4f99932017-07-26 13:41:05 +0200174 efi_install_configuration_table(&fdt_guid, NULL);
Alexander Grafb9939332016-03-10 00:27:20 +0100175 }
176
177 /* Load the EFI payload */
178 entry = efi_load_pe(efi, &loaded_image_info);
Rob Clark95c55532017-09-13 18:05:33 -0400179 if (!entry) {
180 ret = -ENOENT;
181 goto exit;
182 }
Alexander Graf80a48002016-08-16 21:08:45 +0200183
Rob Clarkad644e72017-09-13 18:05:37 -0400184 /* we don't support much: */
185 env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported",
186 "{ro,boot}(blob)0000000000000000");
187
Alexander Grafb9939332016-03-10 00:27:20 +0100188 /* Call our payload! */
Alexander Grafedcef3b2016-06-02 11:38:27 +0200189 debug("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry);
Alexander Grafa86aeaf2016-05-20 23:28:23 +0200190
191 if (setjmp(&loaded_image_info.exit_jmp)) {
Rob Clark95c55532017-09-13 18:05:33 -0400192 ret = loaded_image_info.exit_status;
Rob Clark95c55532017-09-13 18:05:33 -0400193 goto exit;
Alexander Grafa86aeaf2016-05-20 23:28:23 +0200194 }
195
Alexander Graf69bd4592016-11-17 01:02:58 +0100196#ifdef CONFIG_ARM64
197 /* On AArch64 we need to make sure we call our payload in < EL3 */
198 if (current_el() == 3) {
199 smp_kick_all_cpus();
200 dcache_disable(); /* flush cache before switch to EL2 */
Alison Wangec6617c2016-11-10 10:49:03 +0800201
202 /* Move into EL2 and keep running there */
203 armv8_switch_to_el2((ulong)entry, (ulong)&loaded_image_info,
Alison Wang7c5e1fe2017-01-17 09:39:17 +0800204 (ulong)&systab, 0, (ulong)efi_run_in_el2,
Alison Wangec6617c2016-11-10 10:49:03 +0800205 ES_TO_AARCH64);
206
207 /* Should never reach here, efi exits with longjmp */
208 while (1) { }
Alexander Graf69bd4592016-11-17 01:02:58 +0100209 }
210#endif
211
Rob Clark95c55532017-09-13 18:05:33 -0400212 ret = efi_do_enter(&loaded_image_info, &systab, entry);
213
214exit:
215 /* image has returned, loaded-image obj goes *poof*: */
216 list_del(&loaded_image_info_obj.link);
217
218 return ret;
Alexander Grafb9939332016-03-10 00:27:20 +0100219}
220
Rob Clark9975fe92017-09-13 18:05:38 -0400221static int do_bootefi_bootmgr_exec(unsigned long fdt_addr)
222{
223 struct efi_device_path *device_path, *file_path;
224 void *addr;
225 efi_status_t r;
226
227 /* Initialize and populate EFI object list */
228 if (!efi_obj_list_initalized)
229 efi_init_obj_list();
230
231 /*
232 * gd lives in a fixed register which may get clobbered while we execute
233 * the payload. So save it here and restore it on every callback entry
234 */
235 efi_save_gd();
236
237 addr = efi_bootmgr_load(&device_path, &file_path);
238 if (!addr)
239 return 1;
240
241 printf("## Starting EFI application at %p ...\n", addr);
242 r = do_bootefi_exec(addr, (void *)fdt_addr, device_path, file_path);
243 printf("## Application terminated, r = %lu\n",
244 r & ~EFI_ERROR_MASK);
245
246 if (r != EFI_SUCCESS)
247 return 1;
248
249 return 0;
250}
251
Alexander Grafb9939332016-03-10 00:27:20 +0100252/* Interpreter command to boot an arbitrary EFI image from memory */
253static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
254{
Alexander Graf1c398092016-04-14 16:07:53 +0200255 char *saddr, *sfdt;
256 unsigned long addr, fdt_addr = 0;
xypron.glpk@gmx.de1da1bac2017-07-04 23:15:23 +0200257 unsigned long r;
Alexander Grafb9939332016-03-10 00:27:20 +0100258
259 if (argc < 2)
Bin Meng3c1dcef2016-08-13 04:02:06 -0700260 return CMD_RET_USAGE;
Simon Glassc7ae3df2016-11-07 08:47:08 -0700261#ifdef CONFIG_CMD_BOOTEFI_HELLO
262 if (!strcmp(argv[1], "hello")) {
Heinrich Schuchardt5e444892017-09-05 03:19:37 +0200263 ulong size = __efi_helloworld_end - __efi_helloworld_begin;
Alexander Grafb9939332016-03-10 00:27:20 +0100264
Heinrich Schuchardt51c533f2017-08-28 18:54:30 +0200265 saddr = env_get("loadaddr");
266 if (saddr)
267 addr = simple_strtoul(saddr, NULL, 16);
268 else
269 addr = CONFIG_SYS_LOAD_ADDR;
Heinrich Schuchardt5e444892017-09-05 03:19:37 +0200270 memcpy((char *)addr, __efi_helloworld_begin, size);
Simon Glassc7ae3df2016-11-07 08:47:08 -0700271 } else
272#endif
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200273#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
274 if (!strcmp(argv[1], "selftest")) {
Heinrich Schuchardt7aca68c2017-09-20 22:54:58 +0200275 struct efi_loaded_image loaded_image_info = {};
276 struct efi_object loaded_image_info_obj = {};
277
278 efi_setup_loaded_image(&loaded_image_info,
279 &loaded_image_info_obj,
280 bootefi_device_path, bootefi_image_path);
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200281 /*
282 * gd lives in a fixed register which may get clobbered while we
283 * execute the payload. So save it here and restore it on every
284 * callback entry
285 */
286 efi_save_gd();
287 /* Initialize and populate EFI object list */
288 if (!efi_obj_list_initalized)
289 efi_init_obj_list();
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200290 return efi_selftest(&loaded_image_info, &systab);
291 } else
292#endif
Rob Clark9975fe92017-09-13 18:05:38 -0400293 if (!strcmp(argv[1], "bootmgr")) {
294 unsigned long fdt_addr = 0;
295
296 if (argc > 2)
297 fdt_addr = simple_strtoul(argv[2], NULL, 16);
298
299 return do_bootefi_bootmgr_exec(fdt_addr);
300 } else {
Simon Glassc7ae3df2016-11-07 08:47:08 -0700301 saddr = argv[1];
Alexander Grafb9939332016-03-10 00:27:20 +0100302
Simon Glassc7ae3df2016-11-07 08:47:08 -0700303 addr = simple_strtoul(saddr, NULL, 16);
304
305 if (argc > 2) {
306 sfdt = argv[2];
307 fdt_addr = simple_strtoul(sfdt, NULL, 16);
308 }
Alexander Graf1c398092016-04-14 16:07:53 +0200309 }
310
Simon Glass5ee31ba2016-11-07 08:47:05 -0700311 printf("## Starting EFI application at %08lx ...\n", addr);
Rob Clark95c55532017-09-13 18:05:33 -0400312 r = do_bootefi_exec((void *)addr, (void *)fdt_addr,
313 bootefi_device_path, bootefi_image_path);
xypron.glpk@gmx.de1da1bac2017-07-04 23:15:23 +0200314 printf("## Application terminated, r = %lu\n",
315 r & ~EFI_ERROR_MASK);
Alexander Grafb9939332016-03-10 00:27:20 +0100316
xypron.glpk@gmx.de1da1bac2017-07-04 23:15:23 +0200317 if (r != EFI_SUCCESS)
318 return 1;
319 else
320 return 0;
Alexander Grafb9939332016-03-10 00:27:20 +0100321}
322
323#ifdef CONFIG_SYS_LONGHELP
324static char bootefi_help_text[] =
Alexander Graf1c398092016-04-14 16:07:53 +0200325 "<image address> [fdt address]\n"
326 " - boot EFI payload stored at address <image address>.\n"
327 " If specified, the device tree located at <fdt address> gets\n"
Simon Glassc7ae3df2016-11-07 08:47:08 -0700328 " exposed as EFI configuration table.\n"
329#ifdef CONFIG_CMD_BOOTEFI_HELLO
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200330 "bootefi hello\n"
331 " - boot a sample Hello World application stored within U-Boot\n"
332#endif
333#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
334 "bootefi selftest\n"
335 " - boot an EFI selftest application stored within U-Boot\n"
Simon Glassc7ae3df2016-11-07 08:47:08 -0700336#endif
Rob Clark9975fe92017-09-13 18:05:38 -0400337 "bootmgr [fdt addr]\n"
338 " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
339 "\n"
340 " If specified, the device tree located at <fdt address> gets\n"
341 " exposed as EFI configuration table.\n";
Alexander Grafb9939332016-03-10 00:27:20 +0100342#endif
343
344U_BOOT_CMD(
Alexander Graf1c398092016-04-14 16:07:53 +0200345 bootefi, 3, 0, do_bootefi,
Sergey Kubushyn92dfd922016-06-07 11:14:31 -0700346 "Boots an EFI payload from memory",
Alexander Grafb9939332016-03-10 00:27:20 +0100347 bootefi_help_text
348);
Alexander Graf0f4060e2016-03-04 01:10:14 +0100349
Rob Clark95c55532017-09-13 18:05:33 -0400350static int parse_partnum(const char *devnr)
351{
352 const char *str = strchr(devnr, ':');
353 if (str) {
354 str++;
355 return simple_strtoul(str, NULL, 16);
356 }
357 return 0;
358}
359
Alexander Grafc07ad7c2016-04-11 16:16:19 +0200360void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
Alexander Graf0f4060e2016-03-04 01:10:14 +0100361{
Rob Clark95c55532017-09-13 18:05:33 -0400362 char filename[32] = { 0 }; /* dp->str is u16[32] long */
363 char *s;
Alexander Graf0f4060e2016-03-04 01:10:14 +0100364
Rob Clark95c55532017-09-13 18:05:33 -0400365 if (strcmp(dev, "Net")) {
366 struct blk_desc *desc;
367 int part;
368
369 desc = blk_get_dev(dev, simple_strtol(devnr, NULL, 10));
370 part = parse_partnum(devnr);
371
372 bootefi_device_path = efi_dp_from_part(desc, part);
373 } else {
374#ifdef CONFIG_NET
375 bootefi_device_path = efi_dp_from_eth();
Alexander Graff9d334b2016-08-05 14:49:53 +0200376#endif
Alexander Graff9d334b2016-08-05 14:49:53 +0200377 }
378
Rob Clark9975fe92017-09-13 18:05:38 -0400379 if (!path)
380 return;
381
Alexander Graf49271662016-07-21 01:44:46 +0200382 if (strcmp(dev, "Net")) {
383 /* Add leading / to fs paths, because they're absolute */
Rob Clark95c55532017-09-13 18:05:33 -0400384 snprintf(filename, sizeof(filename), "/%s", path);
Alexander Graf49271662016-07-21 01:44:46 +0200385 } else {
Rob Clark95c55532017-09-13 18:05:33 -0400386 snprintf(filename, sizeof(filename), "%s", path);
Alexander Graf49271662016-07-21 01:44:46 +0200387 }
Rob Clark3e433e92017-07-24 07:59:09 -0400388 /* DOS style file path: */
Rob Clark95c55532017-09-13 18:05:33 -0400389 s = filename;
Rob Clark3e433e92017-07-24 07:59:09 -0400390 while ((s = strchr(s, '/')))
391 *s++ = '\\';
Rob Clark95c55532017-09-13 18:05:33 -0400392 bootefi_image_path = efi_dp_from_file(NULL, 0, filename);
Alexander Graf0f4060e2016-03-04 01:10:14 +0100393}