blob: 38679ffc56a95af3737daacf16240b3fd82755e2 [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Grafe2b04f22016-03-10 00:27:20 +01002/*
3 * EFI application loader
4 *
5 * Copyright (c) 2016 Alexander Graf
Alexander Grafe2b04f22016-03-10 00:27:20 +01006 */
7
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +02008#include <charset.h>
Alexander Grafe2b04f22016-03-10 00:27:20 +01009#include <common.h>
10#include <command.h>
Simon Glass11c89f32017-05-17 17:18:03 -060011#include <dm.h>
Alexander Grafe2b04f22016-03-10 00:27:20 +010012#include <efi_loader.h>
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +020013#include <efi_selftest.h>
Alexander Grafe2b04f22016-03-10 00:27:20 +010014#include <errno.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090015#include <linux/libfdt.h>
16#include <linux/libfdt_env.h>
Alexander Grafa2414512018-06-18 17:22:58 +020017#include <mapmem.h>
Alexander Graffdbae012016-04-11 23:51:01 +020018#include <memalign.h>
Alexander Graf2ff5cc32016-04-11 16:55:26 +020019#include <asm/global_data.h>
Simon Glassc2194bf2016-09-25 15:27:32 -060020#include <asm-generic/sections.h>
Heinrich Schuchardt954e2b92018-04-03 21:59:32 +020021#include <asm-generic/unaligned.h>
Simon Glassc2194bf2016-09-25 15:27:32 -060022#include <linux/linkage.h>
Alexander Graf2ff5cc32016-04-11 16:55:26 +020023
Mark Kettenis5eb68ed2018-06-15 23:47:12 +020024#ifdef CONFIG_ARMV7_NONSEC
25#include <asm/armv7.h>
26#include <asm/secure.h>
27#endif
28
Alexander Graf2ff5cc32016-04-11 16:55:26 +020029DECLARE_GLOBAL_DATA_PTR;
Alexander Grafe2b04f22016-03-10 00:27:20 +010030
Heinrich Schuchardt8c0dfc22018-03-03 15:29:02 +010031#define OBJ_LIST_NOT_INITIALIZED 1
32
33static efi_status_t efi_obj_list_initialized = OBJ_LIST_NOT_INITIALIZED;
Heinrich Schuchardt5e9900f2017-07-19 19:37:22 +020034
Rob Clarkf8db9222017-09-13 18:05:33 -040035static struct efi_device_path *bootefi_image_path;
36static struct efi_device_path *bootefi_device_path;
Alexander Grafe2b04f22016-03-10 00:27:20 +010037
Heinrich Schuchardt5e9900f2017-07-19 19:37:22 +020038/* Initialize and populate EFI object list */
Heinrich Schuchardt8c0dfc22018-03-03 15:29:02 +010039efi_status_t efi_init_obj_list(void)
Heinrich Schuchardt5e9900f2017-07-19 19:37:22 +020040{
Heinrich Schuchardt8c0dfc22018-03-03 15:29:02 +010041 efi_status_t ret = EFI_SUCCESS;
42
Heinrich Schuchardt237a73e2018-10-03 23:55:38 +020043 /*
44 * On the ARM architecture gd is mapped to a fixed register (r9 or x18).
45 * As this register may be overwritten by an EFI payload we save it here
46 * and restore it on every callback entered.
47 */
48 efi_save_gd();
49
Heinrich Schuchardtd4b38f02018-03-03 15:28:58 +010050 /* Initialize once only */
Heinrich Schuchardt8c0dfc22018-03-03 15:29:02 +010051 if (efi_obj_list_initialized != OBJ_LIST_NOT_INITIALIZED)
52 return efi_obj_list_initialized;
Heinrich Schuchardt5e9900f2017-07-19 19:37:22 +020053
Heinrich Schuchardt15070db2018-06-28 12:45:31 +020054 /* Initialize system table */
55 ret = efi_initialize_system_table();
56 if (ret != EFI_SUCCESS)
57 goto out;
58
Heinrich Schuchardt79d72022018-09-20 21:58:23 +020059 /* Initialize root node */
60 ret = efi_root_node_register();
61 if (ret != EFI_SUCCESS)
62 goto out;
63
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010064 /* Initialize EFI driver uclass */
Heinrich Schuchardt8c0dfc22018-03-03 15:29:02 +010065 ret = efi_driver_init();
66 if (ret != EFI_SUCCESS)
67 goto out;
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010068
Heinrich Schuchardt8c0dfc22018-03-03 15:29:02 +010069 ret = efi_console_register();
70 if (ret != EFI_SUCCESS)
71 goto out;
Heinrich Schuchardt5e9900f2017-07-19 19:37:22 +020072#ifdef CONFIG_PARTITIONS
Heinrich Schuchardt8c0dfc22018-03-03 15:29:02 +010073 ret = efi_disk_register();
74 if (ret != EFI_SUCCESS)
75 goto out;
Heinrich Schuchardt5e9900f2017-07-19 19:37:22 +020076#endif
77#if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO)
Heinrich Schuchardt8c0dfc22018-03-03 15:29:02 +010078 ret = efi_gop_register();
79 if (ret != EFI_SUCCESS)
80 goto out;
Heinrich Schuchardt5e9900f2017-07-19 19:37:22 +020081#endif
Joe Hershberger5277a972018-04-13 15:26:39 -050082#ifdef CONFIG_NET
Heinrich Schuchardt8c0dfc22018-03-03 15:29:02 +010083 ret = efi_net_register();
84 if (ret != EFI_SUCCESS)
85 goto out;
Heinrich Schuchardt5e9900f2017-07-19 19:37:22 +020086#endif
Bin Meng2cfbdae2018-06-27 20:38:03 -070087#ifdef CONFIG_GENERATE_ACPI_TABLE
88 ret = efi_acpi_register();
89 if (ret != EFI_SUCCESS)
90 goto out;
91#endif
Heinrich Schuchardt5e9900f2017-07-19 19:37:22 +020092#ifdef CONFIG_GENERATE_SMBIOS_TABLE
Heinrich Schuchardt8c0dfc22018-03-03 15:29:02 +010093 ret = efi_smbios_register();
94 if (ret != EFI_SUCCESS)
95 goto out;
Heinrich Schuchardt5e9900f2017-07-19 19:37:22 +020096#endif
Heinrich Schuchardt8c0dfc22018-03-03 15:29:02 +010097 ret = efi_watchdog_register();
98 if (ret != EFI_SUCCESS)
99 goto out;
Heinrich Schuchardt5e9900f2017-07-19 19:37:22 +0200100
101 /* Initialize EFI runtime services */
Heinrich Schuchardt8c0dfc22018-03-03 15:29:02 +0100102 ret = efi_reset_system_init();
103 if (ret != EFI_SUCCESS)
104 goto out;
Heinrich Schuchardt8c0dfc22018-03-03 15:29:02 +0100105
106out:
107 efi_obj_list_initialized = ret;
108 return ret;
Heinrich Schuchardt5e9900f2017-07-19 19:37:22 +0200109}
110
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200111/*
Heinrich Schuchardt954e2b92018-04-03 21:59:32 +0200112 * Allow unaligned memory access.
113 *
114 * This routine is overridden by architectures providing this feature.
115 */
116void __weak allow_unaligned(void)
117{
118}
119
120/*
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200121 * Set the load options of an image from an environment variable.
122 *
123 * @loaded_image_info: the image
124 * @env_var: name of the environment variable
125 */
126static void set_load_options(struct efi_loaded_image *loaded_image_info,
127 const char *env_var)
128{
129 size_t size;
130 const char *env = env_get(env_var);
Heinrich Schuchardtde527802018-08-31 21:31:33 +0200131 u16 *pos;
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200132
133 loaded_image_info->load_options = NULL;
134 loaded_image_info->load_options_size = 0;
135 if (!env)
136 return;
Heinrich Schuchardtde527802018-08-31 21:31:33 +0200137 size = utf8_utf16_strlen(env) + 1;
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200138 loaded_image_info->load_options = calloc(size, sizeof(u16));
139 if (!loaded_image_info->load_options) {
140 printf("ERROR: Out of memory\n");
141 return;
142 }
Heinrich Schuchardtde527802018-08-31 21:31:33 +0200143 pos = loaded_image_info->load_options;
144 utf8_utf16_strcpy(&pos, env);
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200145 loaded_image_info->load_options_size = size * 2;
146}
147
Simon Glass5d618df2018-08-08 03:54:30 -0600148/**
149 * copy_fdt() - Copy the device tree to a new location available to EFI
150 *
Heinrich Schuchardtb23f9b72018-11-18 17:58:52 +0100151 * The FDT is copied to a suitable location within the EFI memory map.
Heinrich Schuchardtdcdca292018-11-18 17:58:49 +0100152 * Additional 12 KiB are added to the space in case the device tree needs to be
Simon Glass5d618df2018-08-08 03:54:30 -0600153 * expanded later with fdt_open_into().
154 *
Heinrich Schuchardtb23f9b72018-11-18 17:58:52 +0100155 * @fdtp: On entry a pointer to the flattened device tree.
156 * On exit a pointer to the copy of the flattened device tree.
Heinrich Schuchardt96af56e2018-11-12 18:55:22 +0100157 * FDT start
158 * Return: status code
Simon Glass5d618df2018-08-08 03:54:30 -0600159 */
Heinrich Schuchardtb23f9b72018-11-18 17:58:52 +0100160static efi_status_t copy_fdt(void **fdtp)
Alexander Graf2ff5cc32016-04-11 16:55:26 +0200161{
Alexander Graffdbae012016-04-11 23:51:01 +0200162 unsigned long fdt_ram_start = -1L, fdt_pages;
Simon Glass5d618df2018-08-08 03:54:30 -0600163 efi_status_t ret = 0;
164 void *fdt, *new_fdt;
Alexander Graffdbae012016-04-11 23:51:01 +0200165 u64 new_fdt_addr;
Simon Glass5d618df2018-08-08 03:54:30 -0600166 uint fdt_size;
Alexander Graffdbae012016-04-11 23:51:01 +0200167 int i;
Alexander Graf2ff5cc32016-04-11 16:55:26 +0200168
Simon Glass5d618df2018-08-08 03:54:30 -0600169 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
170 u64 ram_start = gd->bd->bi_dram[i].start;
171 u64 ram_size = gd->bd->bi_dram[i].size;
Alexander Graf2ff5cc32016-04-11 16:55:26 +0200172
Alexander Graffdbae012016-04-11 23:51:01 +0200173 if (!ram_size)
174 continue;
175
176 if (ram_start < fdt_ram_start)
177 fdt_ram_start = ram_start;
178 }
179
Simon Glass56bbcc12018-06-18 08:08:25 -0600180 /*
Heinrich Schuchardtdcdca292018-11-18 17:58:49 +0100181 * Give us at least 12 KiB of breathing room in case the device tree
182 * needs to be expanded later.
Simon Glass56bbcc12018-06-18 08:08:25 -0600183 */
Heinrich Schuchardtb23f9b72018-11-18 17:58:52 +0100184 fdt = *fdtp;
Heinrich Schuchardtdcdca292018-11-18 17:58:49 +0100185 fdt_pages = efi_size_in_pages(fdt_totalsize(fdt) + 0x3000);
186 fdt_size = fdt_pages << EFI_PAGE_SHIFT;
Alexander Graffdbae012016-04-11 23:51:01 +0200187
Heinrich Schuchardtb23f9b72018-11-18 17:58:52 +0100188 /*
189 * Safe fdt location is at 127 MiB.
190 * On the sandbox convert from the sandbox address space.
191 */
192 new_fdt_addr = (uintptr_t)map_sysmem(fdt_ram_start + 0x7f00000 +
193 fdt_size, 0);
Simon Glass5d618df2018-08-08 03:54:30 -0600194 ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
195 EFI_RUNTIME_SERVICES_DATA, fdt_pages,
196 &new_fdt_addr);
197 if (ret != EFI_SUCCESS) {
Alexander Graffdbae012016-04-11 23:51:01 +0200198 /* If we can't put it there, put it somewhere */
xypron.glpk@gmx.def6b99122017-08-11 21:19:25 +0200199 new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
Simon Glass5d618df2018-08-08 03:54:30 -0600200 ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
201 EFI_RUNTIME_SERVICES_DATA, fdt_pages,
202 &new_fdt_addr);
203 if (ret != EFI_SUCCESS) {
Alexander Graf9b9b7162017-07-03 13:32:35 +0200204 printf("ERROR: Failed to reserve space for FDT\n");
Simon Glass5d618df2018-08-08 03:54:30 -0600205 goto done;
Alexander Graf9b9b7162017-07-03 13:32:35 +0200206 }
Alexander Graffdbae012016-04-11 23:51:01 +0200207 }
Heinrich Schuchardtb23f9b72018-11-18 17:58:52 +0100208 new_fdt = (void *)(uintptr_t)new_fdt_addr;
Alexander Graf2ff5cc32016-04-11 16:55:26 +0200209 memcpy(new_fdt, fdt, fdt_totalsize(fdt));
210 fdt_set_totalsize(new_fdt, fdt_size);
211
Heinrich Schuchardtb23f9b72018-11-18 17:58:52 +0100212 *fdtp = (void *)(uintptr_t)new_fdt_addr;
Simon Glass5d618df2018-08-08 03:54:30 -0600213done:
214 return ret;
Alexander Graf2ff5cc32016-04-11 16:55:26 +0200215}
216
Heinrich Schuchardt4e363a92017-10-18 18:13:08 +0200217static efi_status_t efi_do_enter(
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100218 efi_handle_t image_handle, struct efi_system_table *st,
Alexander Graf54d8cdf2018-01-24 00:18:08 +0100219 EFIAPI efi_status_t (*entry)(
220 efi_handle_t image_handle,
221 struct efi_system_table *st))
xypron.glpk@gmx.de1e5afc62017-07-04 23:15:21 +0200222{
223 efi_status_t ret = EFI_LOAD_ERROR;
224
225 if (entry)
226 ret = entry(image_handle, st);
227 st->boottime->exit(image_handle, ret, 0, NULL);
228 return ret;
229}
230
Alison Wang73818d52016-11-10 10:49:03 +0800231#ifdef CONFIG_ARM64
Alexander Graf54d8cdf2018-01-24 00:18:08 +0100232static efi_status_t efi_run_in_el2(EFIAPI efi_status_t (*entry)(
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100233 efi_handle_t image_handle, struct efi_system_table *st),
234 efi_handle_t image_handle, struct efi_system_table *st)
Alison Wang73818d52016-11-10 10:49:03 +0800235{
236 /* Enable caches again */
237 dcache_enable();
238
xypron.glpk@gmx.de1e5afc62017-07-04 23:15:21 +0200239 return efi_do_enter(image_handle, st, entry);
Alison Wang73818d52016-11-10 10:49:03 +0800240}
241#endif
242
Mark Kettenis5eb68ed2018-06-15 23:47:12 +0200243#ifdef CONFIG_ARMV7_NONSEC
Mark Kettenis6d140a62018-06-15 23:47:13 +0200244static bool is_nonsec;
245
Mark Kettenis5eb68ed2018-06-15 23:47:12 +0200246static efi_status_t efi_run_in_hyp(EFIAPI efi_status_t (*entry)(
247 efi_handle_t image_handle, struct efi_system_table *st),
248 efi_handle_t image_handle, struct efi_system_table *st)
249{
250 /* Enable caches again */
251 dcache_enable();
252
Mark Kettenis6d140a62018-06-15 23:47:13 +0200253 is_nonsec = true;
254
Mark Kettenis5eb68ed2018-06-15 23:47:12 +0200255 return efi_do_enter(image_handle, st, entry);
256}
257#endif
258
Simon Glass54b9b6e2018-06-18 08:08:28 -0600259/*
260 * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges
261 *
262 * The mem_rsv entries of the FDT are added to the memory map. Any failures are
263 * ignored because this is not critical and we would rather continue to try to
264 * boot.
265 *
266 * @fdt: Pointer to device tree
267 */
268static void efi_carve_out_dt_rsv(void *fdt)
Alexander Graf22c88bf2018-04-06 09:40:51 +0200269{
270 int nr_rsv, i;
271 uint64_t addr, size, pages;
272
273 nr_rsv = fdt_num_mem_rsv(fdt);
274
275 /* Look for an existing entry and add it to the efi mem map. */
276 for (i = 0; i < nr_rsv; i++) {
277 if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0)
278 continue;
279
Heinrich Schuchardtb23f9b72018-11-18 17:58:52 +0100280 /* Convert from sandbox address space. */
281 addr = (uintptr_t)map_sysmem(addr, 0);
282
Heinrich Schuchardtdcdca292018-11-18 17:58:49 +0100283 pages = efi_size_in_pages(size + (addr & EFI_PAGE_MASK));
Heinrich Schuchardt3d92fc82018-11-12 18:55:23 +0100284 addr &= ~EFI_PAGE_MASK;
Simon Glass54b9b6e2018-06-18 08:08:28 -0600285 if (!efi_add_memory_map(addr, pages, EFI_RESERVED_MEMORY_TYPE,
286 false))
287 printf("FDT memrsv map %d: Failed to add to map\n", i);
Alexander Graf22c88bf2018-04-06 09:40:51 +0200288 }
Alexander Graf22c88bf2018-04-06 09:40:51 +0200289}
290
Simon Glass5d618df2018-08-08 03:54:30 -0600291static efi_status_t efi_install_fdt(ulong fdt_addr)
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100292{
293 bootm_headers_t img = { 0 };
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100294 efi_status_t ret;
Simon Glass5d618df2018-08-08 03:54:30 -0600295 void *fdt;
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100296
Simon Glass5d618df2018-08-08 03:54:30 -0600297 fdt = map_sysmem(fdt_addr, 0);
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100298 if (fdt_check_header(fdt)) {
299 printf("ERROR: invalid device tree\n");
300 return EFI_INVALID_PARAMETER;
301 }
302
Heinrich Schuchardt68a0d5b2018-11-18 17:58:53 +0100303 /* Create memory reservation as indicated by the device tree */
304 efi_carve_out_dt_rsv(fdt);
305
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100306 /* Prepare fdt for payload */
Heinrich Schuchardtb23f9b72018-11-18 17:58:52 +0100307 ret = copy_fdt(&fdt);
Simon Glass5d618df2018-08-08 03:54:30 -0600308 if (ret)
309 return ret;
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100310
311 if (image_setup_libfdt(&img, fdt, 0, NULL)) {
312 printf("ERROR: failed to process device tree\n");
313 return EFI_LOAD_ERROR;
314 }
315
316 /* Link to it in the efi tables */
317 ret = efi_install_configuration_table(&efi_guid_fdt, fdt);
318 if (ret != EFI_SUCCESS)
319 return EFI_OUT_OF_RESOURCES;
320
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100321 return ret;
322}
323
Simon Glass7816c7a2018-11-25 20:14:38 -0700324static efi_status_t bootefi_run_prepare(const char *load_options_path,
325 struct efi_device_path *device_path,
326 struct efi_device_path *image_path,
327 struct efi_loaded_image_obj **image_objp,
328 struct efi_loaded_image **loaded_image_infop)
329{
330 efi_status_t ret;
331
332 ret = efi_setup_loaded_image(device_path, image_path, image_objp,
333 loaded_image_infop);
334 if (ret != EFI_SUCCESS)
335 return ret;
336
337 /* Transfer environment variable as load options */
338 set_load_options(*loaded_image_infop, load_options_path);
339
340 return 0;
341}
342
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +0200343/**
Simon Glass4d77ee62018-11-25 20:14:39 -0700344 * bootefi_run_finish() - finish up after running an EFI test
345 *
346 * @loaded_image_info: Pointer to a struct which holds the loaded image info
347 * @image_objj: Pointer to a struct which holds the loaded image object
348 */
349static void bootefi_run_finish(struct efi_loaded_image_obj *image_obj,
350 struct efi_loaded_image *loaded_image_info)
351{
352 efi_restore_gd();
353 free(loaded_image_info->load_options);
354 efi_delete_handle(&image_obj->header);
355}
356
357/**
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +0200358 * do_bootefi_exec() - execute EFI binary
359 *
360 * @efi: address of the binary
361 * @device_path: path of the device from which the binary was loaded
362 * @image_path: device path of the binary
363 * Return: status code
364 *
365 * Load the EFI binary into a newly assigned memory unwinding the relocation
366 * information, install the loaded image protocol, and call the binary.
Alexander Grafe2b04f22016-03-10 00:27:20 +0100367 */
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100368static efi_status_t do_bootefi_exec(void *efi,
Heinrich Schuchardt4e363a92017-10-18 18:13:08 +0200369 struct efi_device_path *device_path,
370 struct efi_device_path *image_path)
Alexander Grafe2b04f22016-03-10 00:27:20 +0100371{
Heinrich Schuchardt078d6e22018-09-16 07:19:48 +0200372 efi_handle_t mem_handle = NULL;
Rob Clark18ceba72017-10-10 08:23:06 -0400373 struct efi_device_path *memdp = NULL;
Heinrich Schuchardt4b055a22018-03-03 15:29:01 +0100374 efi_status_t ret;
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +0200375 struct efi_loaded_image_obj *image_obj = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +0200376 struct efi_loaded_image *loaded_image_info = NULL;
Rob Clarkf8db9222017-09-13 18:05:33 -0400377
Alexander Graf54d8cdf2018-01-24 00:18:08 +0100378 EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
379 struct efi_system_table *st);
Alexander Grafe2b04f22016-03-10 00:27:20 +0100380
Rob Clark18ceba72017-10-10 08:23:06 -0400381 /*
382 * Special case for efi payload not loaded from disk, such as
383 * 'bootefi hello' or for example payload loaded directly into
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200384 * memory via JTAG, etc:
Rob Clark18ceba72017-10-10 08:23:06 -0400385 */
386 if (!device_path && !image_path) {
387 printf("WARNING: using memory device/image path, this may confuse some payloads!\n");
388 /* actual addresses filled in after efi_load_pe() */
389 memdp = efi_dp_from_mem(0, 0, 0);
390 device_path = image_path = memdp;
Heinrich Schuchardt078d6e22018-09-16 07:19:48 +0200391 /*
392 * Grub expects that the device path of the loaded image is
393 * installed on a handle.
394 */
395 ret = efi_create_handle(&mem_handle);
396 if (ret != EFI_SUCCESS)
Simon Glass4d77ee62018-11-25 20:14:39 -0700397 return ret; /* TODO: leaks device_path */
Heinrich Schuchardt078d6e22018-09-16 07:19:48 +0200398 ret = efi_add_protocol(mem_handle, &efi_guid_device_path,
Alexander Graf2d00dbe2018-06-12 10:21:16 +0200399 device_path);
400 if (ret != EFI_SUCCESS)
Simon Glass4d77ee62018-11-25 20:14:39 -0700401 goto err_add_protocol;
Rob Clark18ceba72017-10-10 08:23:06 -0400402 } else {
403 assert(device_path && image_path);
404 }
405
Simon Glass7816c7a2018-11-25 20:14:38 -0700406 ret = bootefi_run_prepare("bootargs", device_path, image_path,
407 &image_obj, &loaded_image_info);
408 if (ret)
Simon Glass4d77ee62018-11-25 20:14:39 -0700409 goto err_prepare;
Rob Clarkf8db9222017-09-13 18:05:33 -0400410
Alexander Grafe2b04f22016-03-10 00:27:20 +0100411 /* Load the EFI payload */
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +0200412 entry = efi_load_pe(image_obj, efi, loaded_image_info);
Rob Clarkf8db9222017-09-13 18:05:33 -0400413 if (!entry) {
Heinrich Schuchardt4b055a22018-03-03 15:29:01 +0100414 ret = EFI_LOAD_ERROR;
Simon Glass4d77ee62018-11-25 20:14:39 -0700415 goto err_prepare;
Rob Clarkf8db9222017-09-13 18:05:33 -0400416 }
Alexander Graf77256092016-08-16 21:08:45 +0200417
Rob Clark18ceba72017-10-10 08:23:06 -0400418 if (memdp) {
419 struct efi_device_path_memory *mdp = (void *)memdp;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +0200420 mdp->memory_type = loaded_image_info->image_code_type;
421 mdp->start_address = (uintptr_t)loaded_image_info->image_base;
Rob Clark18ceba72017-10-10 08:23:06 -0400422 mdp->end_address = mdp->start_address +
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +0200423 loaded_image_info->image_size;
Rob Clark18ceba72017-10-10 08:23:06 -0400424 }
425
Rob Clark15f3d742017-09-13 18:05:37 -0400426 /* we don't support much: */
427 env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported",
428 "{ro,boot}(blob)0000000000000000");
429
Alexander Grafe2b04f22016-03-10 00:27:20 +0100430 /* Call our payload! */
Alexander Graf62a67482016-06-02 11:38:27 +0200431 debug("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry);
Alexander Graf988c0662016-05-20 23:28:23 +0200432
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +0200433 if (setjmp(&image_obj->exit_jmp)) {
434 ret = image_obj->exit_status;
Simon Glass4d77ee62018-11-25 20:14:39 -0700435 goto err_prepare;
Alexander Graf988c0662016-05-20 23:28:23 +0200436 }
437
Alexander Grafbfd199c2016-11-17 01:02:58 +0100438#ifdef CONFIG_ARM64
439 /* On AArch64 we need to make sure we call our payload in < EL3 */
440 if (current_el() == 3) {
441 smp_kick_all_cpus();
442 dcache_disable(); /* flush cache before switch to EL2 */
Alison Wang73818d52016-11-10 10:49:03 +0800443
444 /* Move into EL2 and keep running there */
Heinrich Schuchardt754d4972017-11-26 14:05:22 +0100445 armv8_switch_to_el2((ulong)entry,
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200446 (ulong)&image_obj->header,
Alison Wangeb2088d2017-01-17 09:39:17 +0800447 (ulong)&systab, 0, (ulong)efi_run_in_el2,
Alison Wang73818d52016-11-10 10:49:03 +0800448 ES_TO_AARCH64);
449
450 /* Should never reach here, efi exits with longjmp */
451 while (1) { }
Alexander Grafbfd199c2016-11-17 01:02:58 +0100452 }
453#endif
454
Mark Kettenis5eb68ed2018-06-15 23:47:12 +0200455#ifdef CONFIG_ARMV7_NONSEC
Mark Kettenis6d140a62018-06-15 23:47:13 +0200456 if (armv7_boot_nonsec() && !is_nonsec) {
Mark Kettenis5eb68ed2018-06-15 23:47:12 +0200457 dcache_disable(); /* flush cache before switch to HYP */
458
459 armv7_init_nonsec();
460 secure_ram_addr(_do_nonsec_entry)(
461 efi_run_in_hyp,
462 (uintptr_t)entry,
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200463 (uintptr_t)&image_obj->header,
Mark Kettenis5eb68ed2018-06-15 23:47:12 +0200464 (uintptr_t)&systab);
465
466 /* Should never reach here, efi exits with longjmp */
467 while (1) { }
468 }
469#endif
470
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200471 ret = efi_do_enter(&image_obj->header, &systab, entry);
Rob Clarkf8db9222017-09-13 18:05:33 -0400472
Simon Glass4d77ee62018-11-25 20:14:39 -0700473err_prepare:
Rob Clarkf8db9222017-09-13 18:05:33 -0400474 /* image has returned, loaded-image obj goes *poof*: */
Simon Glass4d77ee62018-11-25 20:14:39 -0700475 bootefi_run_finish(image_obj, loaded_image_info);
476
477err_add_protocol:
Heinrich Schuchardt078d6e22018-09-16 07:19:48 +0200478 if (mem_handle)
479 efi_delete_handle(mem_handle);
Rob Clarkf8db9222017-09-13 18:05:33 -0400480
481 return ret;
Alexander Grafe2b04f22016-03-10 00:27:20 +0100482}
483
Simon Glass93f25592018-11-25 20:14:37 -0700484#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
485/**
486 * bootefi_test_prepare() - prepare to run an EFI test
487 *
488 * This sets things up so we can call EFI functions. This involves preparing
489 * the 'gd' pointer and setting up the load ed image data structures.
490 *
491 * @image_objp: loaded_image_infop: Pointer to a struct which will hold the
492 * loaded image object. This struct will be inited by this function before
493 * use.
494 * @loaded_image_infop: Pointer to a struct which will hold the loaded image
495 * info. This struct will be inited by this function before use.
496 * @path: File path to the test being run (often just the test name with a
497 * backslash before it
498 * @test_func: Address of the test function that is being run
Simon Glass7816c7a2018-11-25 20:14:38 -0700499 * @load_options_path: U-Boot environment variable to use as load options
Simon Glass93f25592018-11-25 20:14:37 -0700500 * @return 0 if OK, -ve on error
501 */
502static efi_status_t bootefi_test_prepare
503 (struct efi_loaded_image_obj **image_objp,
Simon Glass7816c7a2018-11-25 20:14:38 -0700504 struct efi_loaded_image **loaded_image_infop, const char *path,
505 ulong test_func, const char *load_options_path)
Simon Glass93f25592018-11-25 20:14:37 -0700506{
Simon Glass93f25592018-11-25 20:14:37 -0700507 /* Construct a dummy device path */
508 bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
509 (uintptr_t)test_func,
510 (uintptr_t)test_func);
511 if (!bootefi_device_path)
512 return EFI_OUT_OF_RESOURCES;
513 bootefi_image_path = efi_dp_from_file(NULL, 0, path);
514 if (!bootefi_image_path)
515 return EFI_OUT_OF_RESOURCES;
Simon Glass93f25592018-11-25 20:14:37 -0700516
Simon Glass7816c7a2018-11-25 20:14:38 -0700517 return bootefi_run_prepare(load_options_path, bootefi_device_path,
518 bootefi_image_path, image_objp,
519 loaded_image_infop);
Simon Glass93f25592018-11-25 20:14:37 -0700520}
521
Simon Glass93f25592018-11-25 20:14:37 -0700522#endif /* CONFIG_CMD_BOOTEFI_SELFTEST */
523
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100524static int do_bootefi_bootmgr_exec(void)
Rob Clarkc84c1102017-09-13 18:05:38 -0400525{
526 struct efi_device_path *device_path, *file_path;
527 void *addr;
528 efi_status_t r;
529
Rob Clarkc84c1102017-09-13 18:05:38 -0400530 addr = efi_bootmgr_load(&device_path, &file_path);
531 if (!addr)
532 return 1;
533
534 printf("## Starting EFI application at %p ...\n", addr);
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100535 r = do_bootefi_exec(addr, device_path, file_path);
Rob Clarkc84c1102017-09-13 18:05:38 -0400536 printf("## Application terminated, r = %lu\n",
537 r & ~EFI_ERROR_MASK);
538
539 if (r != EFI_SUCCESS)
540 return 1;
541
542 return 0;
543}
544
Alexander Grafe2b04f22016-03-10 00:27:20 +0100545/* Interpreter command to boot an arbitrary EFI image from memory */
546static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
547{
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100548 unsigned long addr;
549 char *saddr;
Heinrich Schuchardt4e363a92017-10-18 18:13:08 +0200550 efi_status_t r;
Alexander Grafa2414512018-06-18 17:22:58 +0200551 unsigned long fdt_addr;
Alexander Grafe2b04f22016-03-10 00:27:20 +0100552
Heinrich Schuchardt954e2b92018-04-03 21:59:32 +0200553 /* Allow unaligned memory access */
554 allow_unaligned();
555
Heinrich Schuchardt8c0dfc22018-03-03 15:29:02 +0100556 /* Initialize EFI drivers */
557 r = efi_init_obj_list();
558 if (r != EFI_SUCCESS) {
559 printf("Error: Cannot set up EFI drivers, r = %lu\n",
560 r & ~EFI_ERROR_MASK);
561 return CMD_RET_FAILURE;
562 }
563
Alexander Grafe2b04f22016-03-10 00:27:20 +0100564 if (argc < 2)
Bin Mengda800ff2016-08-13 04:02:06 -0700565 return CMD_RET_USAGE;
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100566
567 if (argc > 2) {
Alexander Grafa2414512018-06-18 17:22:58 +0200568 fdt_addr = simple_strtoul(argv[2], NULL, 16);
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100569 if (!fdt_addr && *argv[2] != '0')
570 return CMD_RET_USAGE;
571 /* Install device tree */
Simon Glass5d618df2018-08-08 03:54:30 -0600572 r = efi_install_fdt(fdt_addr);
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100573 if (r != EFI_SUCCESS) {
574 printf("ERROR: failed to install device tree\n");
575 return CMD_RET_FAILURE;
576 }
577 } else {
578 /* Remove device tree. EFI_NOT_FOUND can be ignored here */
579 efi_install_configuration_table(&efi_guid_fdt, NULL);
580 printf("WARNING: booting without device tree\n");
581 }
Simon Glassfac4ced2016-11-07 08:47:08 -0700582#ifdef CONFIG_CMD_BOOTEFI_HELLO
583 if (!strcmp(argv[1], "hello")) {
Heinrich Schuchardt7804ae92017-09-05 03:19:37 +0200584 ulong size = __efi_helloworld_end - __efi_helloworld_begin;
Alexander Grafe2b04f22016-03-10 00:27:20 +0100585
Heinrich Schuchardte6a9ab52017-08-28 18:54:30 +0200586 saddr = env_get("loadaddr");
587 if (saddr)
588 addr = simple_strtoul(saddr, NULL, 16);
589 else
590 addr = CONFIG_SYS_LOAD_ADDR;
Alexander Grafa2414512018-06-18 17:22:58 +0200591 memcpy(map_sysmem(addr, size), __efi_helloworld_begin, size);
Simon Glassfac4ced2016-11-07 08:47:08 -0700592 } else
593#endif
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200594#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
595 if (!strcmp(argv[1], "selftest")) {
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +0200596 struct efi_loaded_image_obj *image_obj;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +0200597 struct efi_loaded_image *loaded_image_info;
Heinrich Schuchardt389f2512017-09-20 22:54:58 +0200598
Simon Glass93f25592018-11-25 20:14:37 -0700599 if (bootefi_test_prepare(&image_obj, &loaded_image_info,
Simon Glass7816c7a2018-11-25 20:14:38 -0700600 "\\selftest", (uintptr_t)&efi_selftest,
601 "efi_selftest"))
Simon Glass758cfa12018-11-25 20:14:36 -0700602 return CMD_RET_FAILURE;
603
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200604 /* Execute the test */
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200605 r = efi_selftest(&image_obj->header, &systab);
Simon Glass4d77ee62018-11-25 20:14:39 -0700606 bootefi_run_finish(image_obj, loaded_image_info);
Heinrich Schuchardtd2d90b42017-10-18 18:13:14 +0200607 return r != EFI_SUCCESS;
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200608 } else
609#endif
Rob Clarkc84c1102017-09-13 18:05:38 -0400610 if (!strcmp(argv[1], "bootmgr")) {
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100611 return do_bootefi_bootmgr_exec();
Rob Clarkc84c1102017-09-13 18:05:38 -0400612 } else {
Simon Glassfac4ced2016-11-07 08:47:08 -0700613 saddr = argv[1];
Alexander Grafe2b04f22016-03-10 00:27:20 +0100614
Simon Glassfac4ced2016-11-07 08:47:08 -0700615 addr = simple_strtoul(saddr, NULL, 16);
Heinrich Schuchardtaa6fd9a2018-01-24 20:33:54 +0100616 /* Check that a numeric value was passed */
617 if (!addr && *saddr != '0')
618 return CMD_RET_USAGE;
Simon Glassfac4ced2016-11-07 08:47:08 -0700619
Alexander Graf54c1e832016-04-14 16:07:53 +0200620 }
621
Simon Glass1a5490e2016-11-07 08:47:05 -0700622 printf("## Starting EFI application at %08lx ...\n", addr);
Alexander Grafa2414512018-06-18 17:22:58 +0200623 r = do_bootefi_exec(map_sysmem(addr, 0), bootefi_device_path,
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100624 bootefi_image_path);
xypron.glpk@gmx.de5eadbfa2017-07-04 23:15:23 +0200625 printf("## Application terminated, r = %lu\n",
626 r & ~EFI_ERROR_MASK);
Alexander Grafe2b04f22016-03-10 00:27:20 +0100627
xypron.glpk@gmx.de5eadbfa2017-07-04 23:15:23 +0200628 if (r != EFI_SUCCESS)
629 return 1;
630 else
631 return 0;
Alexander Grafe2b04f22016-03-10 00:27:20 +0100632}
633
634#ifdef CONFIG_SYS_LONGHELP
635static char bootefi_help_text[] =
Alexander Graf54c1e832016-04-14 16:07:53 +0200636 "<image address> [fdt address]\n"
637 " - boot EFI payload stored at address <image address>.\n"
638 " If specified, the device tree located at <fdt address> gets\n"
Simon Glassfac4ced2016-11-07 08:47:08 -0700639 " exposed as EFI configuration table.\n"
640#ifdef CONFIG_CMD_BOOTEFI_HELLO
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200641 "bootefi hello\n"
642 " - boot a sample Hello World application stored within U-Boot\n"
643#endif
644#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100645 "bootefi selftest [fdt address]\n"
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200646 " - boot an EFI selftest application stored within U-Boot\n"
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200647 " Use environment variable efi_selftest to select a single test.\n"
648 " Use 'setenv efi_selftest list' to enumerate all tests.\n"
Simon Glassfac4ced2016-11-07 08:47:08 -0700649#endif
Heinrich Schuchardt1b2320f2018-01-30 19:47:43 +0100650 "bootefi bootmgr [fdt addr]\n"
Rob Clarkc84c1102017-09-13 18:05:38 -0400651 " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
652 "\n"
653 " If specified, the device tree located at <fdt address> gets\n"
654 " exposed as EFI configuration table.\n";
Alexander Grafe2b04f22016-03-10 00:27:20 +0100655#endif
656
657U_BOOT_CMD(
Alexander Graf54c1e832016-04-14 16:07:53 +0200658 bootefi, 3, 0, do_bootefi,
Sergey Kubushyn268f19e2016-06-07 11:14:31 -0700659 "Boots an EFI payload from memory",
Alexander Grafe2b04f22016-03-10 00:27:20 +0100660 bootefi_help_text
661);
Alexander Graf8f19f262016-03-04 01:10:14 +0100662
Rob Clarkf8db9222017-09-13 18:05:33 -0400663void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
664{
AKASHI Takahiro035fb012018-10-17 16:32:03 +0900665 struct efi_device_path *device, *image;
666 efi_status_t ret;
Alexander Graf34118e72016-08-05 14:49:53 +0200667
Heinrich Schuchardt761ecc82018-09-16 07:20:21 +0200668 /* efi_set_bootdev is typically called repeatedly, recover memory */
669 efi_free_pool(bootefi_device_path);
670 efi_free_pool(bootefi_image_path);
Heinrich Schuchardt761ecc82018-09-16 07:20:21 +0200671
AKASHI Takahiro035fb012018-10-17 16:32:03 +0900672 ret = efi_dp_from_name(dev, devnr, path, &device, &image);
673 if (ret == EFI_SUCCESS) {
674 bootefi_device_path = device;
675 bootefi_image_path = image;
Alexander Graf45e437d2016-07-21 01:44:46 +0200676 } else {
AKASHI Takahiro035fb012018-10-17 16:32:03 +0900677 bootefi_device_path = NULL;
678 bootefi_image_path = NULL;
Alexander Graf45e437d2016-07-21 01:44:46 +0200679 }
Alexander Graf8f19f262016-03-04 01:10:14 +0100680}