blob: ad97a9c01960098fe33b60bd13a13a98db7cb40b [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Grafb9939332016-03-10 00:27:20 +01002/*
3 * EFI application loader
4 *
5 * Copyright (c) 2016 Alexander Graf
Alexander Grafb9939332016-03-10 00:27:20 +01006 */
7
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +02008#include <charset.h>
Alexander Grafb9939332016-03-10 00:27:20 +01009#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>
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +020013#include <efi_selftest.h>
Alexander Grafb9939332016-03-10 00:27:20 +010014#include <errno.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090015#include <linux/libfdt.h>
16#include <linux/libfdt_env.h>
Alexander Graf354264b2018-06-18 17:22:58 +020017#include <mapmem.h>
Alexander Grafad0c1a32016-04-11 23:51:01 +020018#include <memalign.h>
Alexander Graf0d9d5012016-04-11 16:55:26 +020019#include <asm/global_data.h>
Simon Glasse2754582016-09-25 15:27:32 -060020#include <asm-generic/sections.h>
Heinrich Schuchardtc3b11de2018-04-03 21:59:32 +020021#include <asm-generic/unaligned.h>
Simon Glasse2754582016-09-25 15:27:32 -060022#include <linux/linkage.h>
Alexander Graf0d9d5012016-04-11 16:55:26 +020023
Mark Kettenisdc500c32018-06-15 23:47:12 +020024#ifdef CONFIG_ARMV7_NONSEC
25#include <asm/armv7.h>
26#include <asm/secure.h>
27#endif
28
Alexander Graf0d9d5012016-04-11 16:55:26 +020029DECLARE_GLOBAL_DATA_PTR;
Alexander Grafb9939332016-03-10 00:27:20 +010030
Heinrich Schuchardtfc225e62018-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 Schuchardt7cbc1242017-07-19 19:37:22 +020034
Rob Clark95c55532017-09-13 18:05:33 -040035static struct efi_device_path *bootefi_image_path;
36static struct efi_device_path *bootefi_device_path;
Alexander Grafb9939332016-03-10 00:27:20 +010037
Heinrich Schuchardt7cbc1242017-07-19 19:37:22 +020038/* Initialize and populate EFI object list */
Heinrich Schuchardtfc225e62018-03-03 15:29:02 +010039efi_status_t efi_init_obj_list(void)
Heinrich Schuchardt7cbc1242017-07-19 19:37:22 +020040{
Heinrich Schuchardtfc225e62018-03-03 15:29:02 +010041 efi_status_t ret = EFI_SUCCESS;
42
Heinrich Schuchardt1e1e1c22018-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 Schuchardt098a6cd2018-03-03 15:28:58 +010050 /* Initialize once only */
Heinrich Schuchardtfc225e62018-03-03 15:29:02 +010051 if (efi_obj_list_initialized != OBJ_LIST_NOT_INITIALIZED)
52 return efi_obj_list_initialized;
Heinrich Schuchardt7cbc1242017-07-19 19:37:22 +020053
Heinrich Schuchardt640adad2018-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 Schuchardt4e6b5d62018-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 Schuchardt05ef48a2018-01-21 19:29:30 +010064 /* Initialize EFI driver uclass */
Heinrich Schuchardtfc225e62018-03-03 15:29:02 +010065 ret = efi_driver_init();
66 if (ret != EFI_SUCCESS)
67 goto out;
Heinrich Schuchardt05ef48a2018-01-21 19:29:30 +010068
Heinrich Schuchardtfc225e62018-03-03 15:29:02 +010069 ret = efi_console_register();
70 if (ret != EFI_SUCCESS)
71 goto out;
Heinrich Schuchardt7cbc1242017-07-19 19:37:22 +020072#ifdef CONFIG_PARTITIONS
Heinrich Schuchardtfc225e62018-03-03 15:29:02 +010073 ret = efi_disk_register();
74 if (ret != EFI_SUCCESS)
75 goto out;
Heinrich Schuchardt7cbc1242017-07-19 19:37:22 +020076#endif
77#if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO)
Heinrich Schuchardtfc225e62018-03-03 15:29:02 +010078 ret = efi_gop_register();
79 if (ret != EFI_SUCCESS)
80 goto out;
Heinrich Schuchardt7cbc1242017-07-19 19:37:22 +020081#endif
Joe Hershberger092f2f32018-04-13 15:26:39 -050082#ifdef CONFIG_NET
Heinrich Schuchardtfc225e62018-03-03 15:29:02 +010083 ret = efi_net_register();
84 if (ret != EFI_SUCCESS)
85 goto out;
Heinrich Schuchardt7cbc1242017-07-19 19:37:22 +020086#endif
Bin Meng86df34d2018-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 Schuchardt7cbc1242017-07-19 19:37:22 +020092#ifdef CONFIG_GENERATE_SMBIOS_TABLE
Heinrich Schuchardtfc225e62018-03-03 15:29:02 +010093 ret = efi_smbios_register();
94 if (ret != EFI_SUCCESS)
95 goto out;
Heinrich Schuchardt7cbc1242017-07-19 19:37:22 +020096#endif
Heinrich Schuchardtfc225e62018-03-03 15:29:02 +010097 ret = efi_watchdog_register();
98 if (ret != EFI_SUCCESS)
99 goto out;
Heinrich Schuchardt7cbc1242017-07-19 19:37:22 +0200100
101 /* Initialize EFI runtime services */
Heinrich Schuchardtfc225e62018-03-03 15:29:02 +0100102 ret = efi_reset_system_init();
103 if (ret != EFI_SUCCESS)
104 goto out;
Heinrich Schuchardtfc225e62018-03-03 15:29:02 +0100105
106out:
107 efi_obj_list_initialized = ret;
108 return ret;
Heinrich Schuchardt7cbc1242017-07-19 19:37:22 +0200109}
110
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200111/*
Heinrich Schuchardtc3b11de2018-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 Schuchardtd78e40d2017-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 Schuchardt7086a712018-08-31 21:31:33 +0200131 u16 *pos;
Heinrich Schuchardtd78e40d2017-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 Schuchardt7086a712018-08-31 21:31:33 +0200137 size = utf8_utf16_strlen(env) + 1;
Heinrich Schuchardtd78e40d2017-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 Schuchardt7086a712018-08-31 21:31:33 +0200143 pos = loaded_image_info->load_options;
144 utf8_utf16_strcpy(&pos, env);
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200145 loaded_image_info->load_options_size = size * 2;
146}
147
Simon Glass9dff4902018-08-08 03:54:30 -0600148/**
149 * copy_fdt() - Copy the device tree to a new location available to EFI
150 *
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +0100151 * The FDT is copied to a suitable location within the EFI memory map.
Heinrich Schuchardtc3772ca2018-11-18 17:58:49 +0100152 * Additional 12 KiB are added to the space in case the device tree needs to be
Simon Glass9dff4902018-08-08 03:54:30 -0600153 * expanded later with fdt_open_into().
154 *
Heinrich Schuchardt16b615d2018-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 Schuchardt4574d1b2018-11-12 18:55:22 +0100157 * FDT start
158 * Return: status code
Simon Glass9dff4902018-08-08 03:54:30 -0600159 */
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +0100160static efi_status_t copy_fdt(void **fdtp)
Alexander Graf0d9d5012016-04-11 16:55:26 +0200161{
Alexander Grafad0c1a32016-04-11 23:51:01 +0200162 unsigned long fdt_ram_start = -1L, fdt_pages;
Simon Glass9dff4902018-08-08 03:54:30 -0600163 efi_status_t ret = 0;
164 void *fdt, *new_fdt;
Alexander Grafad0c1a32016-04-11 23:51:01 +0200165 u64 new_fdt_addr;
Simon Glass9dff4902018-08-08 03:54:30 -0600166 uint fdt_size;
Alexander Grafad0c1a32016-04-11 23:51:01 +0200167 int i;
Alexander Graf0d9d5012016-04-11 16:55:26 +0200168
Simon Glass9dff4902018-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 Graf0d9d5012016-04-11 16:55:26 +0200172
Alexander Grafad0c1a32016-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 Glassbc9a6382018-06-18 08:08:25 -0600180 /*
Heinrich Schuchardtc3772ca2018-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 Glassbc9a6382018-06-18 08:08:25 -0600183 */
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +0100184 fdt = *fdtp;
Heinrich Schuchardtc3772ca2018-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 Grafad0c1a32016-04-11 23:51:01 +0200187
Heinrich Schuchardt16b615d2018-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 Glass9dff4902018-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 Grafad0c1a32016-04-11 23:51:01 +0200198 /* If we can't put it there, put it somewhere */
xypron.glpk@gmx.dea44bffc2017-08-11 21:19:25 +0200199 new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
Simon Glass9dff4902018-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 Graf85a6e9b2017-07-03 13:32:35 +0200204 printf("ERROR: Failed to reserve space for FDT\n");
Simon Glass9dff4902018-08-08 03:54:30 -0600205 goto done;
Alexander Graf85a6e9b2017-07-03 13:32:35 +0200206 }
Alexander Grafad0c1a32016-04-11 23:51:01 +0200207 }
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +0100208 new_fdt = (void *)(uintptr_t)new_fdt_addr;
Alexander Graf0d9d5012016-04-11 16:55:26 +0200209 memcpy(new_fdt, fdt, fdt_totalsize(fdt));
210 fdt_set_totalsize(new_fdt, fdt_size);
211
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +0100212 *fdtp = (void *)(uintptr_t)new_fdt_addr;
Simon Glass9dff4902018-08-08 03:54:30 -0600213done:
214 return ret;
Alexander Graf0d9d5012016-04-11 16:55:26 +0200215}
216
Heinrich Schuchardt3eb08412017-10-18 18:13:08 +0200217static efi_status_t efi_do_enter(
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100218 efi_handle_t image_handle, struct efi_system_table *st,
Alexander Grafc6fa5df2018-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.deb06d8ac2017-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 Wangec6617c2016-11-10 10:49:03 +0800231#ifdef CONFIG_ARM64
Alexander Grafc6fa5df2018-01-24 00:18:08 +0100232static efi_status_t efi_run_in_el2(EFIAPI efi_status_t (*entry)(
Heinrich Schuchardt2074f702018-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 Wangec6617c2016-11-10 10:49:03 +0800235{
236 /* Enable caches again */
237 dcache_enable();
238
xypron.glpk@gmx.deb06d8ac2017-07-04 23:15:21 +0200239 return efi_do_enter(image_handle, st, entry);
Alison Wangec6617c2016-11-10 10:49:03 +0800240}
241#endif
242
Mark Kettenisdc500c32018-06-15 23:47:12 +0200243#ifdef CONFIG_ARMV7_NONSEC
Mark Kettenisf17f2002018-06-15 23:47:13 +0200244static bool is_nonsec;
245
Mark Kettenisdc500c32018-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 Kettenisf17f2002018-06-15 23:47:13 +0200253 is_nonsec = true;
254
Mark Kettenisdc500c32018-06-15 23:47:12 +0200255 return efi_do_enter(image_handle, st, entry);
256}
257#endif
258
Simon Glass416e07e2018-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 Graf806d2fa2018-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 Schuchardt16b615d2018-11-18 17:58:52 +0100280 /* Convert from sandbox address space. */
281 addr = (uintptr_t)map_sysmem(addr, 0);
282
Heinrich Schuchardt23fd84b2018-11-12 18:55:23 +0100283 /*
284 * Do not carve out the device tree. It is already marked as
285 * EFI_RUNTIME_SERVICES_DATA
286 */
287 if (addr == (uintptr_t)fdt)
288 continue;
289
Heinrich Schuchardtc3772ca2018-11-18 17:58:49 +0100290 pages = efi_size_in_pages(size + (addr & EFI_PAGE_MASK));
Heinrich Schuchardt23fd84b2018-11-12 18:55:23 +0100291 addr &= ~EFI_PAGE_MASK;
Simon Glass416e07e2018-06-18 08:08:28 -0600292 if (!efi_add_memory_map(addr, pages, EFI_RESERVED_MEMORY_TYPE,
293 false))
294 printf("FDT memrsv map %d: Failed to add to map\n", i);
Alexander Graf806d2fa2018-04-06 09:40:51 +0200295 }
Alexander Graf806d2fa2018-04-06 09:40:51 +0200296}
297
Simon Glass9dff4902018-08-08 03:54:30 -0600298static efi_status_t efi_install_fdt(ulong fdt_addr)
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100299{
300 bootm_headers_t img = { 0 };
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100301 efi_status_t ret;
Simon Glass9dff4902018-08-08 03:54:30 -0600302 void *fdt;
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100303
Simon Glass9dff4902018-08-08 03:54:30 -0600304 fdt = map_sysmem(fdt_addr, 0);
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100305 if (fdt_check_header(fdt)) {
306 printf("ERROR: invalid device tree\n");
307 return EFI_INVALID_PARAMETER;
308 }
309
310 /* Prepare fdt for payload */
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +0100311 ret = copy_fdt(&fdt);
Simon Glass9dff4902018-08-08 03:54:30 -0600312 if (ret)
313 return ret;
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100314
315 if (image_setup_libfdt(&img, fdt, 0, NULL)) {
316 printf("ERROR: failed to process device tree\n");
317 return EFI_LOAD_ERROR;
318 }
319
Simon Glass416e07e2018-06-18 08:08:28 -0600320 efi_carve_out_dt_rsv(fdt);
Alexander Graf806d2fa2018-04-06 09:40:51 +0200321
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100322 /* Link to it in the efi tables */
323 ret = efi_install_configuration_table(&efi_guid_fdt, fdt);
324 if (ret != EFI_SUCCESS)
325 return EFI_OUT_OF_RESOURCES;
326
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100327 return ret;
328}
329
Simon Glassf4f0f7c2018-11-25 20:14:38 -0700330static efi_status_t bootefi_run_prepare(const char *load_options_path,
331 struct efi_device_path *device_path,
332 struct efi_device_path *image_path,
333 struct efi_loaded_image_obj **image_objp,
334 struct efi_loaded_image **loaded_image_infop)
335{
336 efi_status_t ret;
337
338 ret = efi_setup_loaded_image(device_path, image_path, image_objp,
339 loaded_image_infop);
340 if (ret != EFI_SUCCESS)
341 return ret;
342
343 /* Transfer environment variable as load options */
344 set_load_options(*loaded_image_infop, load_options_path);
345
346 return 0;
347}
348
Heinrich Schuchardtc9828742018-09-23 17:21:51 +0200349/**
Simon Glass5e2f0392018-11-25 20:14:39 -0700350 * bootefi_run_finish() - finish up after running an EFI test
351 *
352 * @loaded_image_info: Pointer to a struct which holds the loaded image info
353 * @image_objj: Pointer to a struct which holds the loaded image object
354 */
355static void bootefi_run_finish(struct efi_loaded_image_obj *image_obj,
356 struct efi_loaded_image *loaded_image_info)
357{
358 efi_restore_gd();
359 free(loaded_image_info->load_options);
360 efi_delete_handle(&image_obj->header);
361}
362
363/**
Heinrich Schuchardtc9828742018-09-23 17:21:51 +0200364 * do_bootefi_exec() - execute EFI binary
365 *
366 * @efi: address of the binary
367 * @device_path: path of the device from which the binary was loaded
368 * @image_path: device path of the binary
369 * Return: status code
370 *
371 * Load the EFI binary into a newly assigned memory unwinding the relocation
372 * information, install the loaded image protocol, and call the binary.
Alexander Grafb9939332016-03-10 00:27:20 +0100373 */
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100374static efi_status_t do_bootefi_exec(void *efi,
Heinrich Schuchardt3eb08412017-10-18 18:13:08 +0200375 struct efi_device_path *device_path,
376 struct efi_device_path *image_path)
Alexander Grafb9939332016-03-10 00:27:20 +0100377{
Heinrich Schuchardt8887acc2018-09-16 07:19:48 +0200378 efi_handle_t mem_handle = NULL;
Rob Clarkbf192732017-10-10 08:23:06 -0400379 struct efi_device_path *memdp = NULL;
Heinrich Schuchardt45204b12018-03-03 15:29:01 +0100380 efi_status_t ret;
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +0200381 struct efi_loaded_image_obj *image_obj = NULL;
Heinrich Schuchardtc9828742018-09-23 17:21:51 +0200382 struct efi_loaded_image *loaded_image_info = NULL;
Rob Clark95c55532017-09-13 18:05:33 -0400383
Alexander Grafc6fa5df2018-01-24 00:18:08 +0100384 EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
385 struct efi_system_table *st);
Alexander Grafb9939332016-03-10 00:27:20 +0100386
Rob Clarkbf192732017-10-10 08:23:06 -0400387 /*
388 * Special case for efi payload not loaded from disk, such as
389 * 'bootefi hello' or for example payload loaded directly into
Heinrich Schuchardte1fec152018-10-18 21:51:38 +0200390 * memory via JTAG, etc:
Rob Clarkbf192732017-10-10 08:23:06 -0400391 */
392 if (!device_path && !image_path) {
393 printf("WARNING: using memory device/image path, this may confuse some payloads!\n");
394 /* actual addresses filled in after efi_load_pe() */
395 memdp = efi_dp_from_mem(0, 0, 0);
396 device_path = image_path = memdp;
Heinrich Schuchardt8887acc2018-09-16 07:19:48 +0200397 /*
398 * Grub expects that the device path of the loaded image is
399 * installed on a handle.
400 */
401 ret = efi_create_handle(&mem_handle);
402 if (ret != EFI_SUCCESS)
Simon Glass5e2f0392018-11-25 20:14:39 -0700403 return ret; /* TODO: leaks device_path */
Heinrich Schuchardt8887acc2018-09-16 07:19:48 +0200404 ret = efi_add_protocol(mem_handle, &efi_guid_device_path,
Alexander Graf58bc69d2018-06-12 10:21:16 +0200405 device_path);
406 if (ret != EFI_SUCCESS)
Simon Glass5e2f0392018-11-25 20:14:39 -0700407 goto err_add_protocol;
Rob Clarkbf192732017-10-10 08:23:06 -0400408 } else {
409 assert(device_path && image_path);
410 }
411
Simon Glassf4f0f7c2018-11-25 20:14:38 -0700412 ret = bootefi_run_prepare("bootargs", device_path, image_path,
413 &image_obj, &loaded_image_info);
414 if (ret)
Simon Glass5e2f0392018-11-25 20:14:39 -0700415 goto err_prepare;
Rob Clark95c55532017-09-13 18:05:33 -0400416
Alexander Grafb9939332016-03-10 00:27:20 +0100417 /* Load the EFI payload */
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +0200418 entry = efi_load_pe(image_obj, efi, loaded_image_info);
Rob Clark95c55532017-09-13 18:05:33 -0400419 if (!entry) {
Heinrich Schuchardt45204b12018-03-03 15:29:01 +0100420 ret = EFI_LOAD_ERROR;
Simon Glass5e2f0392018-11-25 20:14:39 -0700421 goto err_prepare;
Rob Clark95c55532017-09-13 18:05:33 -0400422 }
Alexander Graf80a48002016-08-16 21:08:45 +0200423
Rob Clarkbf192732017-10-10 08:23:06 -0400424 if (memdp) {
425 struct efi_device_path_memory *mdp = (void *)memdp;
Heinrich Schuchardtc9828742018-09-23 17:21:51 +0200426 mdp->memory_type = loaded_image_info->image_code_type;
427 mdp->start_address = (uintptr_t)loaded_image_info->image_base;
Rob Clarkbf192732017-10-10 08:23:06 -0400428 mdp->end_address = mdp->start_address +
Heinrich Schuchardtc9828742018-09-23 17:21:51 +0200429 loaded_image_info->image_size;
Rob Clarkbf192732017-10-10 08:23:06 -0400430 }
431
Rob Clarkad644e72017-09-13 18:05:37 -0400432 /* we don't support much: */
433 env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported",
434 "{ro,boot}(blob)0000000000000000");
435
Alexander Grafb9939332016-03-10 00:27:20 +0100436 /* Call our payload! */
Alexander Grafedcef3b2016-06-02 11:38:27 +0200437 debug("%s:%d Jumping to 0x%lx\n", __func__, __LINE__, (long)entry);
Alexander Grafa86aeaf2016-05-20 23:28:23 +0200438
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +0200439 if (setjmp(&image_obj->exit_jmp)) {
440 ret = image_obj->exit_status;
Simon Glass5e2f0392018-11-25 20:14:39 -0700441 goto err_prepare;
Alexander Grafa86aeaf2016-05-20 23:28:23 +0200442 }
443
Alexander Graf69bd4592016-11-17 01:02:58 +0100444#ifdef CONFIG_ARM64
445 /* On AArch64 we need to make sure we call our payload in < EL3 */
446 if (current_el() == 3) {
447 smp_kick_all_cpus();
448 dcache_disable(); /* flush cache before switch to EL2 */
Alison Wangec6617c2016-11-10 10:49:03 +0800449
450 /* Move into EL2 and keep running there */
Heinrich Schuchardtea54ad52017-11-26 14:05:22 +0100451 armv8_switch_to_el2((ulong)entry,
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200452 (ulong)&image_obj->header,
Alison Wang7c5e1fe2017-01-17 09:39:17 +0800453 (ulong)&systab, 0, (ulong)efi_run_in_el2,
Alison Wangec6617c2016-11-10 10:49:03 +0800454 ES_TO_AARCH64);
455
456 /* Should never reach here, efi exits with longjmp */
457 while (1) { }
Alexander Graf69bd4592016-11-17 01:02:58 +0100458 }
459#endif
460
Mark Kettenisdc500c32018-06-15 23:47:12 +0200461#ifdef CONFIG_ARMV7_NONSEC
Mark Kettenisf17f2002018-06-15 23:47:13 +0200462 if (armv7_boot_nonsec() && !is_nonsec) {
Mark Kettenisdc500c32018-06-15 23:47:12 +0200463 dcache_disable(); /* flush cache before switch to HYP */
464
465 armv7_init_nonsec();
466 secure_ram_addr(_do_nonsec_entry)(
467 efi_run_in_hyp,
468 (uintptr_t)entry,
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200469 (uintptr_t)&image_obj->header,
Mark Kettenisdc500c32018-06-15 23:47:12 +0200470 (uintptr_t)&systab);
471
472 /* Should never reach here, efi exits with longjmp */
473 while (1) { }
474 }
475#endif
476
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200477 ret = efi_do_enter(&image_obj->header, &systab, entry);
Rob Clark95c55532017-09-13 18:05:33 -0400478
Simon Glass5e2f0392018-11-25 20:14:39 -0700479err_prepare:
Rob Clark95c55532017-09-13 18:05:33 -0400480 /* image has returned, loaded-image obj goes *poof*: */
Simon Glass5e2f0392018-11-25 20:14:39 -0700481 bootefi_run_finish(image_obj, loaded_image_info);
482
483err_add_protocol:
Heinrich Schuchardt8887acc2018-09-16 07:19:48 +0200484 if (mem_handle)
485 efi_delete_handle(mem_handle);
Rob Clark95c55532017-09-13 18:05:33 -0400486
487 return ret;
Alexander Grafb9939332016-03-10 00:27:20 +0100488}
489
Simon Glassd9717ea2018-11-25 20:14:37 -0700490#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
491/**
492 * bootefi_test_prepare() - prepare to run an EFI test
493 *
494 * This sets things up so we can call EFI functions. This involves preparing
495 * the 'gd' pointer and setting up the load ed image data structures.
496 *
497 * @image_objp: loaded_image_infop: Pointer to a struct which will hold the
498 * loaded image object. This struct will be inited by this function before
499 * use.
500 * @loaded_image_infop: Pointer to a struct which will hold the loaded image
501 * info. This struct will be inited by this function before use.
502 * @path: File path to the test being run (often just the test name with a
503 * backslash before it
504 * @test_func: Address of the test function that is being run
Simon Glassf4f0f7c2018-11-25 20:14:38 -0700505 * @load_options_path: U-Boot environment variable to use as load options
Simon Glassd9717ea2018-11-25 20:14:37 -0700506 * @return 0 if OK, -ve on error
507 */
508static efi_status_t bootefi_test_prepare
509 (struct efi_loaded_image_obj **image_objp,
Simon Glassf4f0f7c2018-11-25 20:14:38 -0700510 struct efi_loaded_image **loaded_image_infop, const char *path,
511 ulong test_func, const char *load_options_path)
Simon Glassd9717ea2018-11-25 20:14:37 -0700512{
Simon Glassd9717ea2018-11-25 20:14:37 -0700513 /* Construct a dummy device path */
514 bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
515 (uintptr_t)test_func,
516 (uintptr_t)test_func);
517 if (!bootefi_device_path)
518 return EFI_OUT_OF_RESOURCES;
519 bootefi_image_path = efi_dp_from_file(NULL, 0, path);
520 if (!bootefi_image_path)
521 return EFI_OUT_OF_RESOURCES;
Simon Glassd9717ea2018-11-25 20:14:37 -0700522
Simon Glassf4f0f7c2018-11-25 20:14:38 -0700523 return bootefi_run_prepare(load_options_path, bootefi_device_path,
524 bootefi_image_path, image_objp,
525 loaded_image_infop);
Simon Glassd9717ea2018-11-25 20:14:37 -0700526}
527
Simon Glassd9717ea2018-11-25 20:14:37 -0700528#endif /* CONFIG_CMD_BOOTEFI_SELFTEST */
529
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100530static int do_bootefi_bootmgr_exec(void)
Rob Clark9975fe92017-09-13 18:05:38 -0400531{
532 struct efi_device_path *device_path, *file_path;
533 void *addr;
534 efi_status_t r;
535
Rob Clark9975fe92017-09-13 18:05:38 -0400536 addr = efi_bootmgr_load(&device_path, &file_path);
537 if (!addr)
538 return 1;
539
540 printf("## Starting EFI application at %p ...\n", addr);
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100541 r = do_bootefi_exec(addr, device_path, file_path);
Rob Clark9975fe92017-09-13 18:05:38 -0400542 printf("## Application terminated, r = %lu\n",
543 r & ~EFI_ERROR_MASK);
544
545 if (r != EFI_SUCCESS)
546 return 1;
547
548 return 0;
549}
550
Alexander Grafb9939332016-03-10 00:27:20 +0100551/* Interpreter command to boot an arbitrary EFI image from memory */
552static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
553{
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100554 unsigned long addr;
555 char *saddr;
Heinrich Schuchardt3eb08412017-10-18 18:13:08 +0200556 efi_status_t r;
Alexander Graf354264b2018-06-18 17:22:58 +0200557 unsigned long fdt_addr;
Alexander Grafb9939332016-03-10 00:27:20 +0100558
Heinrich Schuchardtc3b11de2018-04-03 21:59:32 +0200559 /* Allow unaligned memory access */
560 allow_unaligned();
561
Heinrich Schuchardtfc225e62018-03-03 15:29:02 +0100562 /* Initialize EFI drivers */
563 r = efi_init_obj_list();
564 if (r != EFI_SUCCESS) {
565 printf("Error: Cannot set up EFI drivers, r = %lu\n",
566 r & ~EFI_ERROR_MASK);
567 return CMD_RET_FAILURE;
568 }
569
Alexander Grafb9939332016-03-10 00:27:20 +0100570 if (argc < 2)
Bin Meng3c1dcef2016-08-13 04:02:06 -0700571 return CMD_RET_USAGE;
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100572
573 if (argc > 2) {
Alexander Graf354264b2018-06-18 17:22:58 +0200574 fdt_addr = simple_strtoul(argv[2], NULL, 16);
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100575 if (!fdt_addr && *argv[2] != '0')
576 return CMD_RET_USAGE;
577 /* Install device tree */
Simon Glass9dff4902018-08-08 03:54:30 -0600578 r = efi_install_fdt(fdt_addr);
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100579 if (r != EFI_SUCCESS) {
580 printf("ERROR: failed to install device tree\n");
581 return CMD_RET_FAILURE;
582 }
583 } else {
584 /* Remove device tree. EFI_NOT_FOUND can be ignored here */
585 efi_install_configuration_table(&efi_guid_fdt, NULL);
586 printf("WARNING: booting without device tree\n");
587 }
Simon Glassc7ae3df2016-11-07 08:47:08 -0700588#ifdef CONFIG_CMD_BOOTEFI_HELLO
589 if (!strcmp(argv[1], "hello")) {
Heinrich Schuchardt5e444892017-09-05 03:19:37 +0200590 ulong size = __efi_helloworld_end - __efi_helloworld_begin;
Alexander Grafb9939332016-03-10 00:27:20 +0100591
Heinrich Schuchardt51c533f2017-08-28 18:54:30 +0200592 saddr = env_get("loadaddr");
593 if (saddr)
594 addr = simple_strtoul(saddr, NULL, 16);
595 else
596 addr = CONFIG_SYS_LOAD_ADDR;
Alexander Graf354264b2018-06-18 17:22:58 +0200597 memcpy(map_sysmem(addr, size), __efi_helloworld_begin, size);
Simon Glassc7ae3df2016-11-07 08:47:08 -0700598 } else
599#endif
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200600#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
601 if (!strcmp(argv[1], "selftest")) {
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +0200602 struct efi_loaded_image_obj *image_obj;
Heinrich Schuchardtc9828742018-09-23 17:21:51 +0200603 struct efi_loaded_image *loaded_image_info;
Heinrich Schuchardt7aca68c2017-09-20 22:54:58 +0200604
Simon Glassd9717ea2018-11-25 20:14:37 -0700605 if (bootefi_test_prepare(&image_obj, &loaded_image_info,
Simon Glassf4f0f7c2018-11-25 20:14:38 -0700606 "\\selftest", (uintptr_t)&efi_selftest,
607 "efi_selftest"))
Simon Glass2ab7ef72018-11-25 20:14:36 -0700608 return CMD_RET_FAILURE;
609
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200610 /* Execute the test */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +0200611 r = efi_selftest(&image_obj->header, &systab);
Simon Glass5e2f0392018-11-25 20:14:39 -0700612 bootefi_run_finish(image_obj, loaded_image_info);
Heinrich Schuchardtc2b53902017-10-18 18:13:14 +0200613 return r != EFI_SUCCESS;
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200614 } else
615#endif
Rob Clark9975fe92017-09-13 18:05:38 -0400616 if (!strcmp(argv[1], "bootmgr")) {
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100617 return do_bootefi_bootmgr_exec();
Rob Clark9975fe92017-09-13 18:05:38 -0400618 } else {
Simon Glassc7ae3df2016-11-07 08:47:08 -0700619 saddr = argv[1];
Alexander Grafb9939332016-03-10 00:27:20 +0100620
Simon Glassc7ae3df2016-11-07 08:47:08 -0700621 addr = simple_strtoul(saddr, NULL, 16);
Heinrich Schuchardt49db1cb2018-01-24 20:33:54 +0100622 /* Check that a numeric value was passed */
623 if (!addr && *saddr != '0')
624 return CMD_RET_USAGE;
Simon Glassc7ae3df2016-11-07 08:47:08 -0700625
Alexander Graf1c398092016-04-14 16:07:53 +0200626 }
627
Simon Glass5ee31ba2016-11-07 08:47:05 -0700628 printf("## Starting EFI application at %08lx ...\n", addr);
Alexander Graf354264b2018-06-18 17:22:58 +0200629 r = do_bootefi_exec(map_sysmem(addr, 0), bootefi_device_path,
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100630 bootefi_image_path);
xypron.glpk@gmx.de1da1bac2017-07-04 23:15:23 +0200631 printf("## Application terminated, r = %lu\n",
632 r & ~EFI_ERROR_MASK);
Alexander Grafb9939332016-03-10 00:27:20 +0100633
xypron.glpk@gmx.de1da1bac2017-07-04 23:15:23 +0200634 if (r != EFI_SUCCESS)
635 return 1;
636 else
637 return 0;
Alexander Grafb9939332016-03-10 00:27:20 +0100638}
639
640#ifdef CONFIG_SYS_LONGHELP
641static char bootefi_help_text[] =
Alexander Graf1c398092016-04-14 16:07:53 +0200642 "<image address> [fdt address]\n"
643 " - boot EFI payload stored at address <image address>.\n"
644 " If specified, the device tree located at <fdt address> gets\n"
Simon Glassc7ae3df2016-11-07 08:47:08 -0700645 " exposed as EFI configuration table.\n"
646#ifdef CONFIG_CMD_BOOTEFI_HELLO
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200647 "bootefi hello\n"
648 " - boot a sample Hello World application stored within U-Boot\n"
649#endif
650#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100651 "bootefi selftest [fdt address]\n"
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200652 " - boot an EFI selftest application stored within U-Boot\n"
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200653 " Use environment variable efi_selftest to select a single test.\n"
654 " Use 'setenv efi_selftest list' to enumerate all tests.\n"
Simon Glassc7ae3df2016-11-07 08:47:08 -0700655#endif
Heinrich Schuchardtf623e072018-01-30 19:47:43 +0100656 "bootefi bootmgr [fdt addr]\n"
Rob Clark9975fe92017-09-13 18:05:38 -0400657 " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
658 "\n"
659 " If specified, the device tree located at <fdt address> gets\n"
660 " exposed as EFI configuration table.\n";
Alexander Grafb9939332016-03-10 00:27:20 +0100661#endif
662
663U_BOOT_CMD(
Alexander Graf1c398092016-04-14 16:07:53 +0200664 bootefi, 3, 0, do_bootefi,
Sergey Kubushyn92dfd922016-06-07 11:14:31 -0700665 "Boots an EFI payload from memory",
Alexander Grafb9939332016-03-10 00:27:20 +0100666 bootefi_help_text
667);
Alexander Graf0f4060e2016-03-04 01:10:14 +0100668
Alexander Grafc07ad7c2016-04-11 16:16:19 +0200669void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
Alexander Graf0f4060e2016-03-04 01:10:14 +0100670{
AKASHI Takahirof1589ff2018-10-17 16:32:03 +0900671 struct efi_device_path *device, *image;
672 efi_status_t ret;
Alexander Graf0f4060e2016-03-04 01:10:14 +0100673
Heinrich Schuchardt79276eb2018-09-16 07:20:21 +0200674 /* efi_set_bootdev is typically called repeatedly, recover memory */
675 efi_free_pool(bootefi_device_path);
676 efi_free_pool(bootefi_image_path);
Heinrich Schuchardt79276eb2018-09-16 07:20:21 +0200677
AKASHI Takahirof1589ff2018-10-17 16:32:03 +0900678 ret = efi_dp_from_name(dev, devnr, path, &device, &image);
679 if (ret == EFI_SUCCESS) {
680 bootefi_device_path = device;
681 bootefi_image_path = image;
Rob Clark95c55532017-09-13 18:05:33 -0400682 } else {
AKASHI Takahirof1589ff2018-10-17 16:32:03 +0900683 bootefi_device_path = NULL;
684 bootefi_image_path = NULL;
Alexander Graff9d334b2016-08-05 14:49:53 +0200685 }
Alexander Graf0f4060e2016-03-04 01:10:14 +0100686}