blob: ee685d8644e5449aec25a232623911ce9a7d291a [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
8#include <common.h>
Heinrich Schuchardtaa0b11b2019-01-08 18:13:06 +01009#include <bootm.h>
10#include <charset.h>
Alexander Grafe2b04f22016-03-10 00:27:20 +010011#include <command.h>
Simon Glass11c89f32017-05-17 17:18:03 -060012#include <dm.h>
Alexander Grafe2b04f22016-03-10 00:27:20 +010013#include <efi_loader.h>
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +020014#include <efi_selftest.h>
Alexander Grafe2b04f22016-03-10 00:27:20 +010015#include <errno.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090016#include <linux/libfdt.h>
17#include <linux/libfdt_env.h>
Alexander Grafa2414512018-06-18 17:22:58 +020018#include <mapmem.h>
Alexander Graffdbae012016-04-11 23:51:01 +020019#include <memalign.h>
Alexander Graf2ff5cc32016-04-11 16:55:26 +020020#include <asm/global_data.h>
Simon Glassc2194bf2016-09-25 15:27:32 -060021#include <asm-generic/sections.h>
Heinrich Schuchardt954e2b92018-04-03 21:59:32 +020022#include <asm-generic/unaligned.h>
Simon Glassc2194bf2016-09-25 15:27:32 -060023#include <linux/linkage.h>
Alexander Graf2ff5cc32016-04-11 16:55:26 +020024
25DECLARE_GLOBAL_DATA_PTR;
Alexander Grafe2b04f22016-03-10 00:27:20 +010026
Rob Clarkf8db9222017-09-13 18:05:33 -040027static struct efi_device_path *bootefi_image_path;
28static struct efi_device_path *bootefi_device_path;
Alexander Grafe2b04f22016-03-10 00:27:20 +010029
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +020030/*
Heinrich Schuchardt954e2b92018-04-03 21:59:32 +020031 * Allow unaligned memory access.
32 *
33 * This routine is overridden by architectures providing this feature.
34 */
35void __weak allow_unaligned(void)
36{
37}
38
39/*
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +020040 * Set the load options of an image from an environment variable.
41 *
42 * @loaded_image_info: the image
43 * @env_var: name of the environment variable
44 */
45static void set_load_options(struct efi_loaded_image *loaded_image_info,
46 const char *env_var)
47{
48 size_t size;
49 const char *env = env_get(env_var);
Heinrich Schuchardtde527802018-08-31 21:31:33 +020050 u16 *pos;
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +020051
52 loaded_image_info->load_options = NULL;
53 loaded_image_info->load_options_size = 0;
54 if (!env)
55 return;
Heinrich Schuchardtde527802018-08-31 21:31:33 +020056 size = utf8_utf16_strlen(env) + 1;
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +020057 loaded_image_info->load_options = calloc(size, sizeof(u16));
58 if (!loaded_image_info->load_options) {
59 printf("ERROR: Out of memory\n");
60 return;
61 }
Heinrich Schuchardtde527802018-08-31 21:31:33 +020062 pos = loaded_image_info->load_options;
63 utf8_utf16_strcpy(&pos, env);
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +020064 loaded_image_info->load_options_size = size * 2;
65}
66
Simon Glass5d618df2018-08-08 03:54:30 -060067/**
68 * copy_fdt() - Copy the device tree to a new location available to EFI
69 *
Heinrich Schuchardtb23f9b72018-11-18 17:58:52 +010070 * The FDT is copied to a suitable location within the EFI memory map.
Heinrich Schuchardtdcdca292018-11-18 17:58:49 +010071 * Additional 12 KiB are added to the space in case the device tree needs to be
Simon Glass5d618df2018-08-08 03:54:30 -060072 * expanded later with fdt_open_into().
73 *
Heinrich Schuchardtb23f9b72018-11-18 17:58:52 +010074 * @fdtp: On entry a pointer to the flattened device tree.
75 * On exit a pointer to the copy of the flattened device tree.
Heinrich Schuchardt96af56e2018-11-12 18:55:22 +010076 * FDT start
77 * Return: status code
Simon Glass5d618df2018-08-08 03:54:30 -060078 */
Heinrich Schuchardtb23f9b72018-11-18 17:58:52 +010079static efi_status_t copy_fdt(void **fdtp)
Alexander Graf2ff5cc32016-04-11 16:55:26 +020080{
Alexander Graffdbae012016-04-11 23:51:01 +020081 unsigned long fdt_ram_start = -1L, fdt_pages;
Simon Glass5d618df2018-08-08 03:54:30 -060082 efi_status_t ret = 0;
83 void *fdt, *new_fdt;
Alexander Graffdbae012016-04-11 23:51:01 +020084 u64 new_fdt_addr;
Simon Glass5d618df2018-08-08 03:54:30 -060085 uint fdt_size;
Alexander Graffdbae012016-04-11 23:51:01 +020086 int i;
Alexander Graf2ff5cc32016-04-11 16:55:26 +020087
Simon Glass5d618df2018-08-08 03:54:30 -060088 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
89 u64 ram_start = gd->bd->bi_dram[i].start;
90 u64 ram_size = gd->bd->bi_dram[i].size;
Alexander Graf2ff5cc32016-04-11 16:55:26 +020091
Alexander Graffdbae012016-04-11 23:51:01 +020092 if (!ram_size)
93 continue;
94
95 if (ram_start < fdt_ram_start)
96 fdt_ram_start = ram_start;
97 }
98
Simon Glass56bbcc12018-06-18 08:08:25 -060099 /*
Heinrich Schuchardtdcdca292018-11-18 17:58:49 +0100100 * Give us at least 12 KiB of breathing room in case the device tree
101 * needs to be expanded later.
Simon Glass56bbcc12018-06-18 08:08:25 -0600102 */
Heinrich Schuchardtb23f9b72018-11-18 17:58:52 +0100103 fdt = *fdtp;
Heinrich Schuchardtdcdca292018-11-18 17:58:49 +0100104 fdt_pages = efi_size_in_pages(fdt_totalsize(fdt) + 0x3000);
105 fdt_size = fdt_pages << EFI_PAGE_SHIFT;
Alexander Graffdbae012016-04-11 23:51:01 +0200106
Heinrich Schuchardtb23f9b72018-11-18 17:58:52 +0100107 /*
108 * Safe fdt location is at 127 MiB.
109 * On the sandbox convert from the sandbox address space.
110 */
111 new_fdt_addr = (uintptr_t)map_sysmem(fdt_ram_start + 0x7f00000 +
112 fdt_size, 0);
Simon Glass5d618df2018-08-08 03:54:30 -0600113 ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
114 EFI_RUNTIME_SERVICES_DATA, fdt_pages,
115 &new_fdt_addr);
116 if (ret != EFI_SUCCESS) {
Alexander Graffdbae012016-04-11 23:51:01 +0200117 /* If we can't put it there, put it somewhere */
xypron.glpk@gmx.def6b99122017-08-11 21:19:25 +0200118 new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
Simon Glass5d618df2018-08-08 03:54:30 -0600119 ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
120 EFI_RUNTIME_SERVICES_DATA, fdt_pages,
121 &new_fdt_addr);
122 if (ret != EFI_SUCCESS) {
Alexander Graf9b9b7162017-07-03 13:32:35 +0200123 printf("ERROR: Failed to reserve space for FDT\n");
Simon Glass5d618df2018-08-08 03:54:30 -0600124 goto done;
Alexander Graf9b9b7162017-07-03 13:32:35 +0200125 }
Alexander Graffdbae012016-04-11 23:51:01 +0200126 }
Heinrich Schuchardtb23f9b72018-11-18 17:58:52 +0100127 new_fdt = (void *)(uintptr_t)new_fdt_addr;
Alexander Graf2ff5cc32016-04-11 16:55:26 +0200128 memcpy(new_fdt, fdt, fdt_totalsize(fdt));
129 fdt_set_totalsize(new_fdt, fdt_size);
130
Heinrich Schuchardtb23f9b72018-11-18 17:58:52 +0100131 *fdtp = (void *)(uintptr_t)new_fdt_addr;
Simon Glass5d618df2018-08-08 03:54:30 -0600132done:
133 return ret;
Alexander Graf2ff5cc32016-04-11 16:55:26 +0200134}
135
Heinrich Schuchardt4e363a92017-10-18 18:13:08 +0200136static efi_status_t efi_do_enter(
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100137 efi_handle_t image_handle, struct efi_system_table *st,
Alexander Graf54d8cdf2018-01-24 00:18:08 +0100138 EFIAPI efi_status_t (*entry)(
139 efi_handle_t image_handle,
140 struct efi_system_table *st))
xypron.glpk@gmx.de1e5afc62017-07-04 23:15:21 +0200141{
142 efi_status_t ret = EFI_LOAD_ERROR;
143
144 if (entry)
145 ret = entry(image_handle, st);
146 st->boottime->exit(image_handle, ret, 0, NULL);
147 return ret;
148}
Mark Kettenis5eb68ed2018-06-15 23:47:12 +0200149
Simon Glass54b9b6e2018-06-18 08:08:28 -0600150/*
151 * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges
152 *
153 * The mem_rsv entries of the FDT are added to the memory map. Any failures are
154 * ignored because this is not critical and we would rather continue to try to
155 * boot.
156 *
157 * @fdt: Pointer to device tree
158 */
159static void efi_carve_out_dt_rsv(void *fdt)
Alexander Graf22c88bf2018-04-06 09:40:51 +0200160{
161 int nr_rsv, i;
162 uint64_t addr, size, pages;
163
164 nr_rsv = fdt_num_mem_rsv(fdt);
165
166 /* Look for an existing entry and add it to the efi mem map. */
167 for (i = 0; i < nr_rsv; i++) {
168 if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0)
169 continue;
170
Heinrich Schuchardtb23f9b72018-11-18 17:58:52 +0100171 /* Convert from sandbox address space. */
172 addr = (uintptr_t)map_sysmem(addr, 0);
173
Heinrich Schuchardtdcdca292018-11-18 17:58:49 +0100174 pages = efi_size_in_pages(size + (addr & EFI_PAGE_MASK));
Heinrich Schuchardt3d92fc82018-11-12 18:55:23 +0100175 addr &= ~EFI_PAGE_MASK;
Simon Glass54b9b6e2018-06-18 08:08:28 -0600176 if (!efi_add_memory_map(addr, pages, EFI_RESERVED_MEMORY_TYPE,
177 false))
178 printf("FDT memrsv map %d: Failed to add to map\n", i);
Alexander Graf22c88bf2018-04-06 09:40:51 +0200179 }
Alexander Graf22c88bf2018-04-06 09:40:51 +0200180}
181
Simon Glass5d618df2018-08-08 03:54:30 -0600182static efi_status_t efi_install_fdt(ulong fdt_addr)
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100183{
184 bootm_headers_t img = { 0 };
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100185 efi_status_t ret;
Simon Glass5d618df2018-08-08 03:54:30 -0600186 void *fdt;
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100187
Simon Glass5d618df2018-08-08 03:54:30 -0600188 fdt = map_sysmem(fdt_addr, 0);
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100189 if (fdt_check_header(fdt)) {
190 printf("ERROR: invalid device tree\n");
191 return EFI_INVALID_PARAMETER;
192 }
193
Heinrich Schuchardt68a0d5b2018-11-18 17:58:53 +0100194 /* Create memory reservation as indicated by the device tree */
195 efi_carve_out_dt_rsv(fdt);
196
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100197 /* Prepare fdt for payload */
Heinrich Schuchardtb23f9b72018-11-18 17:58:52 +0100198 ret = copy_fdt(&fdt);
Simon Glass5d618df2018-08-08 03:54:30 -0600199 if (ret)
200 return ret;
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100201
202 if (image_setup_libfdt(&img, fdt, 0, NULL)) {
203 printf("ERROR: failed to process device tree\n");
204 return EFI_LOAD_ERROR;
205 }
206
207 /* Link to it in the efi tables */
208 ret = efi_install_configuration_table(&efi_guid_fdt, fdt);
209 if (ret != EFI_SUCCESS)
210 return EFI_OUT_OF_RESOURCES;
211
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100212 return ret;
213}
214
Simon Glass7816c7a2018-11-25 20:14:38 -0700215static efi_status_t bootefi_run_prepare(const char *load_options_path,
216 struct efi_device_path *device_path,
217 struct efi_device_path *image_path,
218 struct efi_loaded_image_obj **image_objp,
219 struct efi_loaded_image **loaded_image_infop)
220{
221 efi_status_t ret;
222
223 ret = efi_setup_loaded_image(device_path, image_path, image_objp,
224 loaded_image_infop);
225 if (ret != EFI_SUCCESS)
226 return ret;
227
228 /* Transfer environment variable as load options */
229 set_load_options(*loaded_image_infop, load_options_path);
230
231 return 0;
232}
233
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +0200234/**
Simon Glass4d77ee62018-11-25 20:14:39 -0700235 * bootefi_run_finish() - finish up after running an EFI test
236 *
237 * @loaded_image_info: Pointer to a struct which holds the loaded image info
238 * @image_objj: Pointer to a struct which holds the loaded image object
239 */
240static void bootefi_run_finish(struct efi_loaded_image_obj *image_obj,
241 struct efi_loaded_image *loaded_image_info)
242{
243 efi_restore_gd();
244 free(loaded_image_info->load_options);
245 efi_delete_handle(&image_obj->header);
246}
247
248/**
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +0200249 * do_bootefi_exec() - execute EFI binary
250 *
251 * @efi: address of the binary
252 * @device_path: path of the device from which the binary was loaded
253 * @image_path: device path of the binary
254 * Return: status code
255 *
256 * Load the EFI binary into a newly assigned memory unwinding the relocation
257 * information, install the loaded image protocol, and call the binary.
Alexander Grafe2b04f22016-03-10 00:27:20 +0100258 */
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100259static efi_status_t do_bootefi_exec(void *efi,
Heinrich Schuchardt4e363a92017-10-18 18:13:08 +0200260 struct efi_device_path *device_path,
261 struct efi_device_path *image_path)
Alexander Grafe2b04f22016-03-10 00:27:20 +0100262{
Heinrich Schuchardt078d6e22018-09-16 07:19:48 +0200263 efi_handle_t mem_handle = NULL;
Rob Clark18ceba72017-10-10 08:23:06 -0400264 struct efi_device_path *memdp = NULL;
Heinrich Schuchardt4b055a22018-03-03 15:29:01 +0100265 efi_status_t ret;
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +0200266 struct efi_loaded_image_obj *image_obj = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +0200267 struct efi_loaded_image *loaded_image_info = NULL;
Rob Clarkf8db9222017-09-13 18:05:33 -0400268
Alexander Graf54d8cdf2018-01-24 00:18:08 +0100269 EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
270 struct efi_system_table *st);
Alexander Grafe2b04f22016-03-10 00:27:20 +0100271
Rob Clark18ceba72017-10-10 08:23:06 -0400272 /*
273 * Special case for efi payload not loaded from disk, such as
274 * 'bootefi hello' or for example payload loaded directly into
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200275 * memory via JTAG, etc:
Rob Clark18ceba72017-10-10 08:23:06 -0400276 */
277 if (!device_path && !image_path) {
278 printf("WARNING: using memory device/image path, this may confuse some payloads!\n");
279 /* actual addresses filled in after efi_load_pe() */
Heinrich Schuchardt4c419bb2018-12-26 22:23:00 +0100280 memdp = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, 0, 0);
Rob Clark18ceba72017-10-10 08:23:06 -0400281 device_path = image_path = memdp;
Heinrich Schuchardt078d6e22018-09-16 07:19:48 +0200282 /*
283 * Grub expects that the device path of the loaded image is
284 * installed on a handle.
285 */
286 ret = efi_create_handle(&mem_handle);
287 if (ret != EFI_SUCCESS)
Simon Glass4d77ee62018-11-25 20:14:39 -0700288 return ret; /* TODO: leaks device_path */
Heinrich Schuchardt078d6e22018-09-16 07:19:48 +0200289 ret = efi_add_protocol(mem_handle, &efi_guid_device_path,
Alexander Graf2d00dbe2018-06-12 10:21:16 +0200290 device_path);
291 if (ret != EFI_SUCCESS)
Simon Glass4d77ee62018-11-25 20:14:39 -0700292 goto err_add_protocol;
Rob Clark18ceba72017-10-10 08:23:06 -0400293 } else {
294 assert(device_path && image_path);
295 }
296
Simon Glass7816c7a2018-11-25 20:14:38 -0700297 ret = bootefi_run_prepare("bootargs", device_path, image_path,
298 &image_obj, &loaded_image_info);
299 if (ret)
Simon Glass4d77ee62018-11-25 20:14:39 -0700300 goto err_prepare;
Rob Clarkf8db9222017-09-13 18:05:33 -0400301
Alexander Grafe2b04f22016-03-10 00:27:20 +0100302 /* Load the EFI payload */
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +0200303 entry = efi_load_pe(image_obj, efi, loaded_image_info);
Rob Clarkf8db9222017-09-13 18:05:33 -0400304 if (!entry) {
Heinrich Schuchardt4b055a22018-03-03 15:29:01 +0100305 ret = EFI_LOAD_ERROR;
Simon Glass4d77ee62018-11-25 20:14:39 -0700306 goto err_prepare;
Rob Clarkf8db9222017-09-13 18:05:33 -0400307 }
Alexander Graf77256092016-08-16 21:08:45 +0200308
Rob Clark18ceba72017-10-10 08:23:06 -0400309 if (memdp) {
310 struct efi_device_path_memory *mdp = (void *)memdp;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +0200311 mdp->memory_type = loaded_image_info->image_code_type;
312 mdp->start_address = (uintptr_t)loaded_image_info->image_base;
Rob Clark18ceba72017-10-10 08:23:06 -0400313 mdp->end_address = mdp->start_address +
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +0200314 loaded_image_info->image_size;
Rob Clark18ceba72017-10-10 08:23:06 -0400315 }
316
Rob Clark15f3d742017-09-13 18:05:37 -0400317 /* we don't support much: */
318 env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported",
319 "{ro,boot}(blob)0000000000000000");
320
Alexander Grafe2b04f22016-03-10 00:27:20 +0100321 /* Call our payload! */
Heinrich Schuchardtc8989c72018-12-28 12:41:14 +0100322 debug("%s: Jumping to 0x%p\n", __func__, entry);
Alexander Graf988c0662016-05-20 23:28:23 +0200323
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +0200324 if (setjmp(&image_obj->exit_jmp)) {
325 ret = image_obj->exit_status;
Simon Glass4d77ee62018-11-25 20:14:39 -0700326 goto err_prepare;
Alexander Graf988c0662016-05-20 23:28:23 +0200327 }
Mark Kettenis5eb68ed2018-06-15 23:47:12 +0200328
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200329 ret = efi_do_enter(&image_obj->header, &systab, entry);
Rob Clarkf8db9222017-09-13 18:05:33 -0400330
Simon Glass4d77ee62018-11-25 20:14:39 -0700331err_prepare:
Rob Clarkf8db9222017-09-13 18:05:33 -0400332 /* image has returned, loaded-image obj goes *poof*: */
Simon Glass4d77ee62018-11-25 20:14:39 -0700333 bootefi_run_finish(image_obj, loaded_image_info);
334
335err_add_protocol:
Heinrich Schuchardt078d6e22018-09-16 07:19:48 +0200336 if (mem_handle)
337 efi_delete_handle(mem_handle);
Rob Clarkf8db9222017-09-13 18:05:33 -0400338
339 return ret;
Alexander Grafe2b04f22016-03-10 00:27:20 +0100340}
341
Simon Glass93f25592018-11-25 20:14:37 -0700342#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
343/**
344 * bootefi_test_prepare() - prepare to run an EFI test
345 *
346 * This sets things up so we can call EFI functions. This involves preparing
347 * the 'gd' pointer and setting up the load ed image data structures.
348 *
349 * @image_objp: loaded_image_infop: Pointer to a struct which will hold the
350 * loaded image object. This struct will be inited by this function before
351 * use.
352 * @loaded_image_infop: Pointer to a struct which will hold the loaded image
353 * info. This struct will be inited by this function before use.
354 * @path: File path to the test being run (often just the test name with a
355 * backslash before it
356 * @test_func: Address of the test function that is being run
Simon Glass7816c7a2018-11-25 20:14:38 -0700357 * @load_options_path: U-Boot environment variable to use as load options
Simon Glass93f25592018-11-25 20:14:37 -0700358 * @return 0 if OK, -ve on error
359 */
360static efi_status_t bootefi_test_prepare
361 (struct efi_loaded_image_obj **image_objp,
Simon Glass7816c7a2018-11-25 20:14:38 -0700362 struct efi_loaded_image **loaded_image_infop, const char *path,
363 ulong test_func, const char *load_options_path)
Simon Glass93f25592018-11-25 20:14:37 -0700364{
Simon Glass93f25592018-11-25 20:14:37 -0700365 /* Construct a dummy device path */
366 bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
367 (uintptr_t)test_func,
368 (uintptr_t)test_func);
369 if (!bootefi_device_path)
370 return EFI_OUT_OF_RESOURCES;
371 bootefi_image_path = efi_dp_from_file(NULL, 0, path);
372 if (!bootefi_image_path)
373 return EFI_OUT_OF_RESOURCES;
Simon Glass93f25592018-11-25 20:14:37 -0700374
Simon Glass7816c7a2018-11-25 20:14:38 -0700375 return bootefi_run_prepare(load_options_path, bootefi_device_path,
376 bootefi_image_path, image_objp,
377 loaded_image_infop);
Simon Glass93f25592018-11-25 20:14:37 -0700378}
379
Simon Glass93f25592018-11-25 20:14:37 -0700380#endif /* CONFIG_CMD_BOOTEFI_SELFTEST */
381
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100382static int do_bootefi_bootmgr_exec(void)
Rob Clarkc84c1102017-09-13 18:05:38 -0400383{
384 struct efi_device_path *device_path, *file_path;
385 void *addr;
386 efi_status_t r;
387
Rob Clarkc84c1102017-09-13 18:05:38 -0400388 addr = efi_bootmgr_load(&device_path, &file_path);
389 if (!addr)
390 return 1;
391
392 printf("## Starting EFI application at %p ...\n", addr);
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100393 r = do_bootefi_exec(addr, device_path, file_path);
Rob Clarkc84c1102017-09-13 18:05:38 -0400394 printf("## Application terminated, r = %lu\n",
395 r & ~EFI_ERROR_MASK);
396
397 if (r != EFI_SUCCESS)
398 return 1;
399
400 return 0;
401}
402
Alexander Grafe2b04f22016-03-10 00:27:20 +0100403/* Interpreter command to boot an arbitrary EFI image from memory */
404static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
405{
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100406 unsigned long addr;
407 char *saddr;
Heinrich Schuchardt4e363a92017-10-18 18:13:08 +0200408 efi_status_t r;
Alexander Grafa2414512018-06-18 17:22:58 +0200409 unsigned long fdt_addr;
Alexander Grafe2b04f22016-03-10 00:27:20 +0100410
Heinrich Schuchardt954e2b92018-04-03 21:59:32 +0200411 /* Allow unaligned memory access */
412 allow_unaligned();
413
Heinrich Schuchardtaa0b11b2019-01-08 18:13:06 +0100414 switch_to_non_secure_mode();
415
Heinrich Schuchardt8c0dfc22018-03-03 15:29:02 +0100416 /* Initialize EFI drivers */
417 r = efi_init_obj_list();
418 if (r != EFI_SUCCESS) {
419 printf("Error: Cannot set up EFI drivers, r = %lu\n",
420 r & ~EFI_ERROR_MASK);
421 return CMD_RET_FAILURE;
422 }
423
Alexander Grafe2b04f22016-03-10 00:27:20 +0100424 if (argc < 2)
Bin Mengda800ff2016-08-13 04:02:06 -0700425 return CMD_RET_USAGE;
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100426
427 if (argc > 2) {
Alexander Grafa2414512018-06-18 17:22:58 +0200428 fdt_addr = simple_strtoul(argv[2], NULL, 16);
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100429 if (!fdt_addr && *argv[2] != '0')
430 return CMD_RET_USAGE;
431 /* Install device tree */
Simon Glass5d618df2018-08-08 03:54:30 -0600432 r = efi_install_fdt(fdt_addr);
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100433 if (r != EFI_SUCCESS) {
434 printf("ERROR: failed to install device tree\n");
435 return CMD_RET_FAILURE;
436 }
437 } else {
438 /* Remove device tree. EFI_NOT_FOUND can be ignored here */
439 efi_install_configuration_table(&efi_guid_fdt, NULL);
440 printf("WARNING: booting without device tree\n");
441 }
Simon Glassfac4ced2016-11-07 08:47:08 -0700442#ifdef CONFIG_CMD_BOOTEFI_HELLO
443 if (!strcmp(argv[1], "hello")) {
Heinrich Schuchardt7804ae92017-09-05 03:19:37 +0200444 ulong size = __efi_helloworld_end - __efi_helloworld_begin;
Alexander Grafe2b04f22016-03-10 00:27:20 +0100445
Heinrich Schuchardte6a9ab52017-08-28 18:54:30 +0200446 saddr = env_get("loadaddr");
447 if (saddr)
448 addr = simple_strtoul(saddr, NULL, 16);
449 else
450 addr = CONFIG_SYS_LOAD_ADDR;
Alexander Grafa2414512018-06-18 17:22:58 +0200451 memcpy(map_sysmem(addr, size), __efi_helloworld_begin, size);
Simon Glassfac4ced2016-11-07 08:47:08 -0700452 } else
453#endif
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200454#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
455 if (!strcmp(argv[1], "selftest")) {
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +0200456 struct efi_loaded_image_obj *image_obj;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +0200457 struct efi_loaded_image *loaded_image_info;
Heinrich Schuchardt389f2512017-09-20 22:54:58 +0200458
Simon Glass93f25592018-11-25 20:14:37 -0700459 if (bootefi_test_prepare(&image_obj, &loaded_image_info,
Simon Glass7816c7a2018-11-25 20:14:38 -0700460 "\\selftest", (uintptr_t)&efi_selftest,
461 "efi_selftest"))
Simon Glass758cfa12018-11-25 20:14:36 -0700462 return CMD_RET_FAILURE;
463
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200464 /* Execute the test */
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200465 r = efi_selftest(&image_obj->header, &systab);
Simon Glass4d77ee62018-11-25 20:14:39 -0700466 bootefi_run_finish(image_obj, loaded_image_info);
Heinrich Schuchardtd2d90b42017-10-18 18:13:14 +0200467 return r != EFI_SUCCESS;
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200468 } else
469#endif
Rob Clarkc84c1102017-09-13 18:05:38 -0400470 if (!strcmp(argv[1], "bootmgr")) {
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100471 return do_bootefi_bootmgr_exec();
Rob Clarkc84c1102017-09-13 18:05:38 -0400472 } else {
Simon Glassfac4ced2016-11-07 08:47:08 -0700473 saddr = argv[1];
Alexander Grafe2b04f22016-03-10 00:27:20 +0100474
Simon Glassfac4ced2016-11-07 08:47:08 -0700475 addr = simple_strtoul(saddr, NULL, 16);
Heinrich Schuchardtaa6fd9a2018-01-24 20:33:54 +0100476 /* Check that a numeric value was passed */
477 if (!addr && *saddr != '0')
478 return CMD_RET_USAGE;
Simon Glassfac4ced2016-11-07 08:47:08 -0700479
Alexander Graf54c1e832016-04-14 16:07:53 +0200480 }
481
Simon Glass1a5490e2016-11-07 08:47:05 -0700482 printf("## Starting EFI application at %08lx ...\n", addr);
Alexander Grafa2414512018-06-18 17:22:58 +0200483 r = do_bootefi_exec(map_sysmem(addr, 0), bootefi_device_path,
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100484 bootefi_image_path);
xypron.glpk@gmx.de5eadbfa2017-07-04 23:15:23 +0200485 printf("## Application terminated, r = %lu\n",
486 r & ~EFI_ERROR_MASK);
Alexander Grafe2b04f22016-03-10 00:27:20 +0100487
xypron.glpk@gmx.de5eadbfa2017-07-04 23:15:23 +0200488 if (r != EFI_SUCCESS)
489 return 1;
490 else
491 return 0;
Alexander Grafe2b04f22016-03-10 00:27:20 +0100492}
493
494#ifdef CONFIG_SYS_LONGHELP
495static char bootefi_help_text[] =
Alexander Graf54c1e832016-04-14 16:07:53 +0200496 "<image address> [fdt address]\n"
497 " - boot EFI payload stored at address <image address>.\n"
498 " If specified, the device tree located at <fdt address> gets\n"
Simon Glassfac4ced2016-11-07 08:47:08 -0700499 " exposed as EFI configuration table.\n"
500#ifdef CONFIG_CMD_BOOTEFI_HELLO
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200501 "bootefi hello\n"
502 " - boot a sample Hello World application stored within U-Boot\n"
503#endif
504#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100505 "bootefi selftest [fdt address]\n"
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200506 " - boot an EFI selftest application stored within U-Boot\n"
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200507 " Use environment variable efi_selftest to select a single test.\n"
508 " Use 'setenv efi_selftest list' to enumerate all tests.\n"
Simon Glassfac4ced2016-11-07 08:47:08 -0700509#endif
Heinrich Schuchardt1b2320f2018-01-30 19:47:43 +0100510 "bootefi bootmgr [fdt addr]\n"
Rob Clarkc84c1102017-09-13 18:05:38 -0400511 " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
512 "\n"
513 " If specified, the device tree located at <fdt address> gets\n"
514 " exposed as EFI configuration table.\n";
Alexander Grafe2b04f22016-03-10 00:27:20 +0100515#endif
516
517U_BOOT_CMD(
Alexander Graf54c1e832016-04-14 16:07:53 +0200518 bootefi, 3, 0, do_bootefi,
Sergey Kubushyn268f19e2016-06-07 11:14:31 -0700519 "Boots an EFI payload from memory",
Alexander Grafe2b04f22016-03-10 00:27:20 +0100520 bootefi_help_text
521);
Alexander Graf8f19f262016-03-04 01:10:14 +0100522
Rob Clarkf8db9222017-09-13 18:05:33 -0400523void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
524{
AKASHI Takahiro035fb012018-10-17 16:32:03 +0900525 struct efi_device_path *device, *image;
526 efi_status_t ret;
Alexander Graf34118e72016-08-05 14:49:53 +0200527
Heinrich Schuchardt761ecc82018-09-16 07:20:21 +0200528 /* efi_set_bootdev is typically called repeatedly, recover memory */
529 efi_free_pool(bootefi_device_path);
530 efi_free_pool(bootefi_image_path);
Heinrich Schuchardt761ecc82018-09-16 07:20:21 +0200531
AKASHI Takahiro035fb012018-10-17 16:32:03 +0900532 ret = efi_dp_from_name(dev, devnr, path, &device, &image);
533 if (ret == EFI_SUCCESS) {
534 bootefi_device_path = device;
535 bootefi_image_path = image;
Alexander Graf45e437d2016-07-21 01:44:46 +0200536 } else {
AKASHI Takahiro035fb012018-10-17 16:32:03 +0900537 bootefi_device_path = NULL;
538 bootefi_image_path = NULL;
Alexander Graf45e437d2016-07-21 01:44:46 +0200539 }
Alexander Graf8f19f262016-03-04 01:10:14 +0100540}