blob: 3619a20e64337838c3496086ffaeb41eb77e5d94 [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
8#include <common.h>
Heinrich Schuchardtf6c6df72019-01-08 18:13:06 +01009#include <bootm.h>
10#include <charset.h>
Alexander Grafb9939332016-03-10 00:27:20 +010011#include <command.h>
Simon Glass9d922452017-05-17 17:18:03 -060012#include <dm.h>
Alexander Grafb9939332016-03-10 00:27:20 +010013#include <efi_loader.h>
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +020014#include <efi_selftest.h>
Alexander Grafb9939332016-03-10 00:27:20 +010015#include <errno.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090016#include <linux/libfdt.h>
17#include <linux/libfdt_env.h>
Alexander Graf354264b2018-06-18 17:22:58 +020018#include <mapmem.h>
Alexander Grafad0c1a32016-04-11 23:51:01 +020019#include <memalign.h>
Alexander Graf0d9d5012016-04-11 16:55:26 +020020#include <asm/global_data.h>
Simon Glasse2754582016-09-25 15:27:32 -060021#include <asm-generic/sections.h>
Heinrich Schuchardtc3b11de2018-04-03 21:59:32 +020022#include <asm-generic/unaligned.h>
Simon Glasse2754582016-09-25 15:27:32 -060023#include <linux/linkage.h>
Alexander Graf0d9d5012016-04-11 16:55:26 +020024
25DECLARE_GLOBAL_DATA_PTR;
Alexander Grafb9939332016-03-10 00:27:20 +010026
Rob Clark95c55532017-09-13 18:05:33 -040027static struct efi_device_path *bootefi_image_path;
28static struct efi_device_path *bootefi_device_path;
Alexander Grafb9939332016-03-10 00:27:20 +010029
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +020030/*
Heinrich Schuchardtc3b11de2018-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 Schuchardtd78e40d2017-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 Schuchardt7086a712018-08-31 21:31:33 +020050 u16 *pos;
Heinrich Schuchardtd78e40d2017-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 Schuchardt7086a712018-08-31 21:31:33 +020056 size = utf8_utf16_strlen(env) + 1;
Heinrich Schuchardtd78e40d2017-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 Schuchardt7086a712018-08-31 21:31:33 +020062 pos = loaded_image_info->load_options;
63 utf8_utf16_strcpy(&pos, env);
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +020064 loaded_image_info->load_options_size = size * 2;
65}
66
Simon Glass9dff4902018-08-08 03:54:30 -060067/**
68 * copy_fdt() - Copy the device tree to a new location available to EFI
69 *
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +010070 * The FDT is copied to a suitable location within the EFI memory map.
Heinrich Schuchardtc3772ca2018-11-18 17:58:49 +010071 * Additional 12 KiB are added to the space in case the device tree needs to be
Simon Glass9dff4902018-08-08 03:54:30 -060072 * expanded later with fdt_open_into().
73 *
Heinrich Schuchardt16b615d2018-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 Schuchardt4574d1b2018-11-12 18:55:22 +010076 * FDT start
77 * Return: status code
Simon Glass9dff4902018-08-08 03:54:30 -060078 */
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +010079static efi_status_t copy_fdt(void **fdtp)
Alexander Graf0d9d5012016-04-11 16:55:26 +020080{
Alexander Grafad0c1a32016-04-11 23:51:01 +020081 unsigned long fdt_ram_start = -1L, fdt_pages;
Simon Glass9dff4902018-08-08 03:54:30 -060082 efi_status_t ret = 0;
83 void *fdt, *new_fdt;
Alexander Grafad0c1a32016-04-11 23:51:01 +020084 u64 new_fdt_addr;
Simon Glass9dff4902018-08-08 03:54:30 -060085 uint fdt_size;
Alexander Grafad0c1a32016-04-11 23:51:01 +020086 int i;
Alexander Graf0d9d5012016-04-11 16:55:26 +020087
Simon Glass9dff4902018-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 Graf0d9d5012016-04-11 16:55:26 +020091
Alexander Grafad0c1a32016-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 Glassbc9a6382018-06-18 08:08:25 -060099 /*
Heinrich Schuchardtc3772ca2018-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 Glassbc9a6382018-06-18 08:08:25 -0600102 */
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +0100103 fdt = *fdtp;
Heinrich Schuchardtc3772ca2018-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 Grafad0c1a32016-04-11 23:51:01 +0200106
Heinrich Schuchardt16b615d2018-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 Glass9dff4902018-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 Grafad0c1a32016-04-11 23:51:01 +0200117 /* If we can't put it there, put it somewhere */
xypron.glpk@gmx.dea44bffc2017-08-11 21:19:25 +0200118 new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
Simon Glass9dff4902018-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 Graf85a6e9b2017-07-03 13:32:35 +0200123 printf("ERROR: Failed to reserve space for FDT\n");
Simon Glass9dff4902018-08-08 03:54:30 -0600124 goto done;
Alexander Graf85a6e9b2017-07-03 13:32:35 +0200125 }
Alexander Grafad0c1a32016-04-11 23:51:01 +0200126 }
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +0100127 new_fdt = (void *)(uintptr_t)new_fdt_addr;
Alexander Graf0d9d5012016-04-11 16:55:26 +0200128 memcpy(new_fdt, fdt, fdt_totalsize(fdt));
129 fdt_set_totalsize(new_fdt, fdt_size);
130
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +0100131 *fdtp = (void *)(uintptr_t)new_fdt_addr;
Simon Glass9dff4902018-08-08 03:54:30 -0600132done:
133 return ret;
Alexander Graf0d9d5012016-04-11 16:55:26 +0200134}
135
Simon Glass416e07e2018-06-18 08:08:28 -0600136/*
137 * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges
138 *
139 * The mem_rsv entries of the FDT are added to the memory map. Any failures are
140 * ignored because this is not critical and we would rather continue to try to
141 * boot.
142 *
143 * @fdt: Pointer to device tree
144 */
145static void efi_carve_out_dt_rsv(void *fdt)
Alexander Graf806d2fa2018-04-06 09:40:51 +0200146{
147 int nr_rsv, i;
148 uint64_t addr, size, pages;
149
150 nr_rsv = fdt_num_mem_rsv(fdt);
151
152 /* Look for an existing entry and add it to the efi mem map. */
153 for (i = 0; i < nr_rsv; i++) {
154 if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0)
155 continue;
156
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +0100157 /* Convert from sandbox address space. */
158 addr = (uintptr_t)map_sysmem(addr, 0);
159
Heinrich Schuchardtc3772ca2018-11-18 17:58:49 +0100160 pages = efi_size_in_pages(size + (addr & EFI_PAGE_MASK));
Heinrich Schuchardt23fd84b2018-11-12 18:55:23 +0100161 addr &= ~EFI_PAGE_MASK;
Simon Glass416e07e2018-06-18 08:08:28 -0600162 if (!efi_add_memory_map(addr, pages, EFI_RESERVED_MEMORY_TYPE,
163 false))
164 printf("FDT memrsv map %d: Failed to add to map\n", i);
Alexander Graf806d2fa2018-04-06 09:40:51 +0200165 }
Alexander Graf806d2fa2018-04-06 09:40:51 +0200166}
167
Simon Glass9dff4902018-08-08 03:54:30 -0600168static efi_status_t efi_install_fdt(ulong fdt_addr)
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100169{
170 bootm_headers_t img = { 0 };
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100171 efi_status_t ret;
Simon Glass9dff4902018-08-08 03:54:30 -0600172 void *fdt;
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100173
Simon Glass9dff4902018-08-08 03:54:30 -0600174 fdt = map_sysmem(fdt_addr, 0);
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100175 if (fdt_check_header(fdt)) {
176 printf("ERROR: invalid device tree\n");
177 return EFI_INVALID_PARAMETER;
178 }
179
Heinrich Schuchardt0c9ac062018-11-18 17:58:53 +0100180 /* Create memory reservation as indicated by the device tree */
181 efi_carve_out_dt_rsv(fdt);
182
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100183 /* Prepare fdt for payload */
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +0100184 ret = copy_fdt(&fdt);
Simon Glass9dff4902018-08-08 03:54:30 -0600185 if (ret)
186 return ret;
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100187
188 if (image_setup_libfdt(&img, fdt, 0, NULL)) {
189 printf("ERROR: failed to process device tree\n");
190 return EFI_LOAD_ERROR;
191 }
192
193 /* Link to it in the efi tables */
194 ret = efi_install_configuration_table(&efi_guid_fdt, fdt);
195 if (ret != EFI_SUCCESS)
196 return EFI_OUT_OF_RESOURCES;
197
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100198 return ret;
199}
200
Simon Glassf4f0f7c2018-11-25 20:14:38 -0700201static efi_status_t bootefi_run_prepare(const char *load_options_path,
202 struct efi_device_path *device_path,
203 struct efi_device_path *image_path,
204 struct efi_loaded_image_obj **image_objp,
205 struct efi_loaded_image **loaded_image_infop)
206{
207 efi_status_t ret;
208
209 ret = efi_setup_loaded_image(device_path, image_path, image_objp,
210 loaded_image_infop);
211 if (ret != EFI_SUCCESS)
212 return ret;
213
214 /* Transfer environment variable as load options */
215 set_load_options(*loaded_image_infop, load_options_path);
216
217 return 0;
218}
219
Heinrich Schuchardtc9828742018-09-23 17:21:51 +0200220/**
Simon Glass5e2f0392018-11-25 20:14:39 -0700221 * bootefi_run_finish() - finish up after running an EFI test
222 *
223 * @loaded_image_info: Pointer to a struct which holds the loaded image info
224 * @image_objj: Pointer to a struct which holds the loaded image object
225 */
226static void bootefi_run_finish(struct efi_loaded_image_obj *image_obj,
227 struct efi_loaded_image *loaded_image_info)
228{
229 efi_restore_gd();
230 free(loaded_image_info->load_options);
231 efi_delete_handle(&image_obj->header);
232}
233
234/**
Heinrich Schuchardtc9828742018-09-23 17:21:51 +0200235 * do_bootefi_exec() - execute EFI binary
236 *
237 * @efi: address of the binary
238 * @device_path: path of the device from which the binary was loaded
239 * @image_path: device path of the binary
240 * Return: status code
241 *
242 * Load the EFI binary into a newly assigned memory unwinding the relocation
243 * information, install the loaded image protocol, and call the binary.
Alexander Grafb9939332016-03-10 00:27:20 +0100244 */
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100245static efi_status_t do_bootefi_exec(void *efi,
Heinrich Schuchardt3eb08412017-10-18 18:13:08 +0200246 struct efi_device_path *device_path,
247 struct efi_device_path *image_path)
Alexander Grafb9939332016-03-10 00:27:20 +0100248{
Heinrich Schuchardt8887acc2018-09-16 07:19:48 +0200249 efi_handle_t mem_handle = NULL;
Rob Clarkbf192732017-10-10 08:23:06 -0400250 struct efi_device_path *memdp = NULL;
Heinrich Schuchardt45204b12018-03-03 15:29:01 +0100251 efi_status_t ret;
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +0200252 struct efi_loaded_image_obj *image_obj = NULL;
Heinrich Schuchardtc9828742018-09-23 17:21:51 +0200253 struct efi_loaded_image *loaded_image_info = NULL;
Rob Clark95c55532017-09-13 18:05:33 -0400254
Rob Clarkbf192732017-10-10 08:23:06 -0400255 /*
256 * Special case for efi payload not loaded from disk, such as
257 * 'bootefi hello' or for example payload loaded directly into
Heinrich Schuchardte1fec152018-10-18 21:51:38 +0200258 * memory via JTAG, etc:
Rob Clarkbf192732017-10-10 08:23:06 -0400259 */
260 if (!device_path && !image_path) {
261 printf("WARNING: using memory device/image path, this may confuse some payloads!\n");
262 /* actual addresses filled in after efi_load_pe() */
Heinrich Schuchardt0a76ba62018-12-26 22:23:00 +0100263 memdp = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, 0, 0);
Rob Clarkbf192732017-10-10 08:23:06 -0400264 device_path = image_path = memdp;
Heinrich Schuchardt8887acc2018-09-16 07:19:48 +0200265 /*
266 * Grub expects that the device path of the loaded image is
267 * installed on a handle.
268 */
269 ret = efi_create_handle(&mem_handle);
270 if (ret != EFI_SUCCESS)
Simon Glass5e2f0392018-11-25 20:14:39 -0700271 return ret; /* TODO: leaks device_path */
Heinrich Schuchardt8887acc2018-09-16 07:19:48 +0200272 ret = efi_add_protocol(mem_handle, &efi_guid_device_path,
Alexander Graf58bc69d2018-06-12 10:21:16 +0200273 device_path);
274 if (ret != EFI_SUCCESS)
Simon Glass5e2f0392018-11-25 20:14:39 -0700275 goto err_add_protocol;
Rob Clarkbf192732017-10-10 08:23:06 -0400276 } else {
277 assert(device_path && image_path);
278 }
279
Simon Glassf4f0f7c2018-11-25 20:14:38 -0700280 ret = bootefi_run_prepare("bootargs", device_path, image_path,
281 &image_obj, &loaded_image_info);
282 if (ret)
Simon Glass5e2f0392018-11-25 20:14:39 -0700283 goto err_prepare;
Rob Clark95c55532017-09-13 18:05:33 -0400284
Alexander Grafb9939332016-03-10 00:27:20 +0100285 /* Load the EFI payload */
Heinrich Schuchardt8f7e2b22018-12-26 12:49:09 +0100286 ret = efi_load_pe(image_obj, efi, loaded_image_info);
287 if (ret != EFI_SUCCESS)
Simon Glass5e2f0392018-11-25 20:14:39 -0700288 goto err_prepare;
Alexander Graf80a48002016-08-16 21:08:45 +0200289
Rob Clarkbf192732017-10-10 08:23:06 -0400290 if (memdp) {
291 struct efi_device_path_memory *mdp = (void *)memdp;
Heinrich Schuchardtc9828742018-09-23 17:21:51 +0200292 mdp->memory_type = loaded_image_info->image_code_type;
293 mdp->start_address = (uintptr_t)loaded_image_info->image_base;
Rob Clarkbf192732017-10-10 08:23:06 -0400294 mdp->end_address = mdp->start_address +
Heinrich Schuchardtc9828742018-09-23 17:21:51 +0200295 loaded_image_info->image_size;
Rob Clarkbf192732017-10-10 08:23:06 -0400296 }
297
Rob Clarkad644e72017-09-13 18:05:37 -0400298 /* we don't support much: */
299 env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported",
300 "{ro,boot}(blob)0000000000000000");
301
Alexander Grafb9939332016-03-10 00:27:20 +0100302 /* Call our payload! */
Heinrich Schuchardt8f7e2b22018-12-26 12:49:09 +0100303 debug("%s: Jumping to 0x%p\n", __func__, image_obj->entry);
Heinrich Schuchardtf69d63f2018-12-26 13:28:09 +0100304 ret = EFI_CALL(efi_start_image(&image_obj->header, NULL, NULL));
Rob Clark95c55532017-09-13 18:05:33 -0400305
Simon Glass5e2f0392018-11-25 20:14:39 -0700306err_prepare:
Rob Clark95c55532017-09-13 18:05:33 -0400307 /* image has returned, loaded-image obj goes *poof*: */
Simon Glass5e2f0392018-11-25 20:14:39 -0700308 bootefi_run_finish(image_obj, loaded_image_info);
309
310err_add_protocol:
Heinrich Schuchardt8887acc2018-09-16 07:19:48 +0200311 if (mem_handle)
312 efi_delete_handle(mem_handle);
Rob Clark95c55532017-09-13 18:05:33 -0400313
314 return ret;
Alexander Grafb9939332016-03-10 00:27:20 +0100315}
316
Simon Glassd9717ea2018-11-25 20:14:37 -0700317#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
318/**
319 * bootefi_test_prepare() - prepare to run an EFI test
320 *
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100321 * Prepare to run a test as if it were provided by a loaded image.
Simon Glassd9717ea2018-11-25 20:14:37 -0700322 *
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100323 * @image_objp: pointer to be set to the loaded image handle
324 * @loaded_image_infop: pointer to be set to the loaded image protocol
325 * @path: dummy file path used to construct the device path
326 * set in the loaded image protocol
327 * @load_options_path: name of a U-Boot environment variable. Its value is
328 * set as load options in the loaded image protocol.
329 * Return: status code
Simon Glassd9717ea2018-11-25 20:14:37 -0700330 */
331static efi_status_t bootefi_test_prepare
332 (struct efi_loaded_image_obj **image_objp,
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100333 struct efi_loaded_image **loaded_image_infop, const char *path,
334 const char *load_options_path)
Simon Glassd9717ea2018-11-25 20:14:37 -0700335{
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100336 efi_status_t ret;
337
Simon Glassd9717ea2018-11-25 20:14:37 -0700338 /* Construct a dummy device path */
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100339 bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, 0, 0);
Simon Glassd9717ea2018-11-25 20:14:37 -0700340 if (!bootefi_device_path)
341 return EFI_OUT_OF_RESOURCES;
Simon Glassd9717ea2018-11-25 20:14:37 -0700342
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100343 bootefi_image_path = efi_dp_from_file(NULL, 0, path);
344 if (!bootefi_image_path) {
345 ret = EFI_OUT_OF_RESOURCES;
346 goto failure;
347 }
348
349 ret = bootefi_run_prepare(load_options_path, bootefi_device_path,
350 bootefi_image_path, image_objp,
351 loaded_image_infop);
352 if (ret == EFI_SUCCESS)
353 return ret;
354
355 efi_free_pool(bootefi_image_path);
356 bootefi_image_path = NULL;
357failure:
358 efi_free_pool(bootefi_device_path);
359 bootefi_device_path = NULL;
360 return ret;
Simon Glassd9717ea2018-11-25 20:14:37 -0700361}
362
Simon Glassd9717ea2018-11-25 20:14:37 -0700363#endif /* CONFIG_CMD_BOOTEFI_SELFTEST */
364
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100365static int do_bootefi_bootmgr_exec(void)
Rob Clark9975fe92017-09-13 18:05:38 -0400366{
367 struct efi_device_path *device_path, *file_path;
368 void *addr;
369 efi_status_t r;
370
Rob Clark9975fe92017-09-13 18:05:38 -0400371 addr = efi_bootmgr_load(&device_path, &file_path);
372 if (!addr)
373 return 1;
374
375 printf("## Starting EFI application at %p ...\n", addr);
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100376 r = do_bootefi_exec(addr, device_path, file_path);
Rob Clark9975fe92017-09-13 18:05:38 -0400377 printf("## Application terminated, r = %lu\n",
378 r & ~EFI_ERROR_MASK);
379
380 if (r != EFI_SUCCESS)
381 return 1;
382
383 return 0;
384}
385
Alexander Grafb9939332016-03-10 00:27:20 +0100386/* Interpreter command to boot an arbitrary EFI image from memory */
387static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
388{
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100389 unsigned long addr;
390 char *saddr;
Heinrich Schuchardt3eb08412017-10-18 18:13:08 +0200391 efi_status_t r;
Alexander Graf354264b2018-06-18 17:22:58 +0200392 unsigned long fdt_addr;
Alexander Grafb9939332016-03-10 00:27:20 +0100393
Heinrich Schuchardtc3b11de2018-04-03 21:59:32 +0200394 /* Allow unaligned memory access */
395 allow_unaligned();
396
Heinrich Schuchardtf6c6df72019-01-08 18:13:06 +0100397 switch_to_non_secure_mode();
398
Heinrich Schuchardtfc225e62018-03-03 15:29:02 +0100399 /* Initialize EFI drivers */
400 r = efi_init_obj_list();
401 if (r != EFI_SUCCESS) {
402 printf("Error: Cannot set up EFI drivers, r = %lu\n",
403 r & ~EFI_ERROR_MASK);
404 return CMD_RET_FAILURE;
405 }
406
Alexander Grafb9939332016-03-10 00:27:20 +0100407 if (argc < 2)
Bin Meng3c1dcef2016-08-13 04:02:06 -0700408 return CMD_RET_USAGE;
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100409
410 if (argc > 2) {
Alexander Graf354264b2018-06-18 17:22:58 +0200411 fdt_addr = simple_strtoul(argv[2], NULL, 16);
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100412 if (!fdt_addr && *argv[2] != '0')
413 return CMD_RET_USAGE;
414 /* Install device tree */
Simon Glass9dff4902018-08-08 03:54:30 -0600415 r = efi_install_fdt(fdt_addr);
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100416 if (r != EFI_SUCCESS) {
417 printf("ERROR: failed to install device tree\n");
418 return CMD_RET_FAILURE;
419 }
420 } else {
421 /* Remove device tree. EFI_NOT_FOUND can be ignored here */
422 efi_install_configuration_table(&efi_guid_fdt, NULL);
423 printf("WARNING: booting without device tree\n");
424 }
Simon Glassc7ae3df2016-11-07 08:47:08 -0700425#ifdef CONFIG_CMD_BOOTEFI_HELLO
426 if (!strcmp(argv[1], "hello")) {
Heinrich Schuchardt5e444892017-09-05 03:19:37 +0200427 ulong size = __efi_helloworld_end - __efi_helloworld_begin;
Alexander Grafb9939332016-03-10 00:27:20 +0100428
Heinrich Schuchardt51c533f2017-08-28 18:54:30 +0200429 saddr = env_get("loadaddr");
430 if (saddr)
431 addr = simple_strtoul(saddr, NULL, 16);
432 else
433 addr = CONFIG_SYS_LOAD_ADDR;
Alexander Graf354264b2018-06-18 17:22:58 +0200434 memcpy(map_sysmem(addr, size), __efi_helloworld_begin, size);
Simon Glassc7ae3df2016-11-07 08:47:08 -0700435 } else
436#endif
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200437#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
438 if (!strcmp(argv[1], "selftest")) {
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +0200439 struct efi_loaded_image_obj *image_obj;
Heinrich Schuchardtc9828742018-09-23 17:21:51 +0200440 struct efi_loaded_image *loaded_image_info;
Heinrich Schuchardt7aca68c2017-09-20 22:54:58 +0200441
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100442 r = bootefi_test_prepare(&image_obj, &loaded_image_info,
443 "\\selftest", "efi_selftest");
444 if (r != EFI_SUCCESS)
Simon Glass2ab7ef72018-11-25 20:14:36 -0700445 return CMD_RET_FAILURE;
446
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200447 /* Execute the test */
Heinrich Schuchardt914df752019-02-09 14:10:39 +0100448 r = EFI_CALL(efi_selftest(&image_obj->header, &systab));
Simon Glass5e2f0392018-11-25 20:14:39 -0700449 bootefi_run_finish(image_obj, loaded_image_info);
Heinrich Schuchardtc2b53902017-10-18 18:13:14 +0200450 return r != EFI_SUCCESS;
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200451 } else
452#endif
Rob Clark9975fe92017-09-13 18:05:38 -0400453 if (!strcmp(argv[1], "bootmgr")) {
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100454 return do_bootefi_bootmgr_exec();
Rob Clark9975fe92017-09-13 18:05:38 -0400455 } else {
Simon Glassc7ae3df2016-11-07 08:47:08 -0700456 saddr = argv[1];
Alexander Grafb9939332016-03-10 00:27:20 +0100457
Simon Glassc7ae3df2016-11-07 08:47:08 -0700458 addr = simple_strtoul(saddr, NULL, 16);
Heinrich Schuchardt49db1cb2018-01-24 20:33:54 +0100459 /* Check that a numeric value was passed */
460 if (!addr && *saddr != '0')
461 return CMD_RET_USAGE;
Simon Glassc7ae3df2016-11-07 08:47:08 -0700462
Alexander Graf1c398092016-04-14 16:07:53 +0200463 }
464
Simon Glass5ee31ba2016-11-07 08:47:05 -0700465 printf("## Starting EFI application at %08lx ...\n", addr);
Alexander Graf354264b2018-06-18 17:22:58 +0200466 r = do_bootefi_exec(map_sysmem(addr, 0), bootefi_device_path,
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100467 bootefi_image_path);
xypron.glpk@gmx.de1da1bac2017-07-04 23:15:23 +0200468 printf("## Application terminated, r = %lu\n",
469 r & ~EFI_ERROR_MASK);
Alexander Grafb9939332016-03-10 00:27:20 +0100470
xypron.glpk@gmx.de1da1bac2017-07-04 23:15:23 +0200471 if (r != EFI_SUCCESS)
472 return 1;
473 else
474 return 0;
Alexander Grafb9939332016-03-10 00:27:20 +0100475}
476
477#ifdef CONFIG_SYS_LONGHELP
478static char bootefi_help_text[] =
Alexander Graf1c398092016-04-14 16:07:53 +0200479 "<image address> [fdt address]\n"
480 " - boot EFI payload stored at address <image address>.\n"
481 " If specified, the device tree located at <fdt address> gets\n"
Simon Glassc7ae3df2016-11-07 08:47:08 -0700482 " exposed as EFI configuration table.\n"
483#ifdef CONFIG_CMD_BOOTEFI_HELLO
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200484 "bootefi hello\n"
485 " - boot a sample Hello World application stored within U-Boot\n"
486#endif
487#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100488 "bootefi selftest [fdt address]\n"
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200489 " - boot an EFI selftest application stored within U-Boot\n"
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200490 " Use environment variable efi_selftest to select a single test.\n"
491 " Use 'setenv efi_selftest list' to enumerate all tests.\n"
Simon Glassc7ae3df2016-11-07 08:47:08 -0700492#endif
Heinrich Schuchardtf623e072018-01-30 19:47:43 +0100493 "bootefi bootmgr [fdt addr]\n"
Rob Clark9975fe92017-09-13 18:05:38 -0400494 " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
495 "\n"
496 " If specified, the device tree located at <fdt address> gets\n"
497 " exposed as EFI configuration table.\n";
Alexander Grafb9939332016-03-10 00:27:20 +0100498#endif
499
500U_BOOT_CMD(
Alexander Graf1c398092016-04-14 16:07:53 +0200501 bootefi, 3, 0, do_bootefi,
Sergey Kubushyn92dfd922016-06-07 11:14:31 -0700502 "Boots an EFI payload from memory",
Alexander Grafb9939332016-03-10 00:27:20 +0100503 bootefi_help_text
504);
Alexander Graf0f4060e2016-03-04 01:10:14 +0100505
Alexander Grafc07ad7c2016-04-11 16:16:19 +0200506void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
Alexander Graf0f4060e2016-03-04 01:10:14 +0100507{
AKASHI Takahirof1589ff2018-10-17 16:32:03 +0900508 struct efi_device_path *device, *image;
509 efi_status_t ret;
Alexander Graf0f4060e2016-03-04 01:10:14 +0100510
Heinrich Schuchardt79276eb2018-09-16 07:20:21 +0200511 /* efi_set_bootdev is typically called repeatedly, recover memory */
512 efi_free_pool(bootefi_device_path);
513 efi_free_pool(bootefi_image_path);
Heinrich Schuchardt79276eb2018-09-16 07:20:21 +0200514
AKASHI Takahirof1589ff2018-10-17 16:32:03 +0900515 ret = efi_dp_from_name(dev, devnr, path, &device, &image);
516 if (ret == EFI_SUCCESS) {
517 bootefi_device_path = device;
518 bootefi_image_path = image;
Rob Clark95c55532017-09-13 18:05:33 -0400519 } else {
AKASHI Takahirof1589ff2018-10-17 16:32:03 +0900520 bootefi_device_path = NULL;
521 bootefi_image_path = NULL;
Alexander Graff9d334b2016-08-05 14:49:53 +0200522 }
Alexander Graf0f4060e2016-03-04 01:10:14 +0100523}