blob: 985ad48fde55dccbfabff39f2158af4fb68b6abc [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 *
AKASHI Takahirodefa7b82019-04-19 12:22:28 +090042 * @handle: the image handle
43 * @env_var: name of the environment variable
44 * Return: status code
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +020045 */
AKASHI Takahirodefa7b82019-04-19 12:22:28 +090046static efi_status_t set_load_options(efi_handle_t handle, const char *env_var)
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +020047{
AKASHI Takahirodefa7b82019-04-19 12:22:28 +090048 struct efi_loaded_image *loaded_image_info;
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +020049 size_t size;
50 const char *env = env_get(env_var);
Heinrich Schuchardt7086a712018-08-31 21:31:33 +020051 u16 *pos;
AKASHI Takahirodefa7b82019-04-19 12:22:28 +090052 efi_status_t ret;
53
54 ret = EFI_CALL(systab.boottime->open_protocol(
55 handle,
56 &efi_guid_loaded_image,
57 (void **)&loaded_image_info,
58 efi_root, NULL,
59 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL));
60 if (ret != EFI_SUCCESS)
61 return EFI_INVALID_PARAMETER;
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +020062
63 loaded_image_info->load_options = NULL;
64 loaded_image_info->load_options_size = 0;
65 if (!env)
AKASHI Takahirodefa7b82019-04-19 12:22:28 +090066 goto out;
67
Heinrich Schuchardt7086a712018-08-31 21:31:33 +020068 size = utf8_utf16_strlen(env) + 1;
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +020069 loaded_image_info->load_options = calloc(size, sizeof(u16));
70 if (!loaded_image_info->load_options) {
71 printf("ERROR: Out of memory\n");
AKASHI Takahirodefa7b82019-04-19 12:22:28 +090072 EFI_CALL(systab.boottime->close_protocol(handle,
73 &efi_guid_loaded_image,
74 efi_root, NULL));
75 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +020076 }
Heinrich Schuchardt7086a712018-08-31 21:31:33 +020077 pos = loaded_image_info->load_options;
78 utf8_utf16_strcpy(&pos, env);
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +020079 loaded_image_info->load_options_size = size * 2;
AKASHI Takahirodefa7b82019-04-19 12:22:28 +090080
81out:
82 return EFI_CALL(systab.boottime->close_protocol(handle,
83 &efi_guid_loaded_image,
84 efi_root, NULL));
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +020085}
86
Simon Glass9dff4902018-08-08 03:54:30 -060087/**
88 * copy_fdt() - Copy the device tree to a new location available to EFI
89 *
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +010090 * The FDT is copied to a suitable location within the EFI memory map.
Heinrich Schuchardtc3772ca2018-11-18 17:58:49 +010091 * Additional 12 KiB are added to the space in case the device tree needs to be
Simon Glass9dff4902018-08-08 03:54:30 -060092 * expanded later with fdt_open_into().
93 *
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +010094 * @fdtp: On entry a pointer to the flattened device tree.
95 * On exit a pointer to the copy of the flattened device tree.
Heinrich Schuchardt4574d1b2018-11-12 18:55:22 +010096 * FDT start
97 * Return: status code
Simon Glass9dff4902018-08-08 03:54:30 -060098 */
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +010099static efi_status_t copy_fdt(void **fdtp)
Alexander Graf0d9d5012016-04-11 16:55:26 +0200100{
Alexander Grafad0c1a32016-04-11 23:51:01 +0200101 unsigned long fdt_ram_start = -1L, fdt_pages;
Simon Glass9dff4902018-08-08 03:54:30 -0600102 efi_status_t ret = 0;
103 void *fdt, *new_fdt;
Alexander Grafad0c1a32016-04-11 23:51:01 +0200104 u64 new_fdt_addr;
Simon Glass9dff4902018-08-08 03:54:30 -0600105 uint fdt_size;
Alexander Grafad0c1a32016-04-11 23:51:01 +0200106 int i;
Alexander Graf0d9d5012016-04-11 16:55:26 +0200107
Simon Glass9dff4902018-08-08 03:54:30 -0600108 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
109 u64 ram_start = gd->bd->bi_dram[i].start;
110 u64 ram_size = gd->bd->bi_dram[i].size;
Alexander Graf0d9d5012016-04-11 16:55:26 +0200111
Alexander Grafad0c1a32016-04-11 23:51:01 +0200112 if (!ram_size)
113 continue;
114
115 if (ram_start < fdt_ram_start)
116 fdt_ram_start = ram_start;
117 }
118
Simon Glassbc9a6382018-06-18 08:08:25 -0600119 /*
Heinrich Schuchardtc3772ca2018-11-18 17:58:49 +0100120 * Give us at least 12 KiB of breathing room in case the device tree
121 * needs to be expanded later.
Simon Glassbc9a6382018-06-18 08:08:25 -0600122 */
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +0100123 fdt = *fdtp;
Heinrich Schuchardtc3772ca2018-11-18 17:58:49 +0100124 fdt_pages = efi_size_in_pages(fdt_totalsize(fdt) + 0x3000);
125 fdt_size = fdt_pages << EFI_PAGE_SHIFT;
Alexander Grafad0c1a32016-04-11 23:51:01 +0200126
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +0100127 /*
128 * Safe fdt location is at 127 MiB.
129 * On the sandbox convert from the sandbox address space.
130 */
131 new_fdt_addr = (uintptr_t)map_sysmem(fdt_ram_start + 0x7f00000 +
132 fdt_size, 0);
Simon Glass9dff4902018-08-08 03:54:30 -0600133 ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
Ilias Apalodimas29361ec2019-04-12 21:26:28 +0300134 EFI_BOOT_SERVICES_DATA, fdt_pages,
Simon Glass9dff4902018-08-08 03:54:30 -0600135 &new_fdt_addr);
136 if (ret != EFI_SUCCESS) {
Alexander Grafad0c1a32016-04-11 23:51:01 +0200137 /* If we can't put it there, put it somewhere */
xypron.glpk@gmx.dea44bffc2017-08-11 21:19:25 +0200138 new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
Simon Glass9dff4902018-08-08 03:54:30 -0600139 ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
Ilias Apalodimas29361ec2019-04-12 21:26:28 +0300140 EFI_BOOT_SERVICES_DATA, fdt_pages,
Simon Glass9dff4902018-08-08 03:54:30 -0600141 &new_fdt_addr);
142 if (ret != EFI_SUCCESS) {
Alexander Graf85a6e9b2017-07-03 13:32:35 +0200143 printf("ERROR: Failed to reserve space for FDT\n");
Simon Glass9dff4902018-08-08 03:54:30 -0600144 goto done;
Alexander Graf85a6e9b2017-07-03 13:32:35 +0200145 }
Alexander Grafad0c1a32016-04-11 23:51:01 +0200146 }
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +0100147 new_fdt = (void *)(uintptr_t)new_fdt_addr;
Alexander Graf0d9d5012016-04-11 16:55:26 +0200148 memcpy(new_fdt, fdt, fdt_totalsize(fdt));
149 fdt_set_totalsize(new_fdt, fdt_size);
150
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +0100151 *fdtp = (void *)(uintptr_t)new_fdt_addr;
Simon Glass9dff4902018-08-08 03:54:30 -0600152done:
153 return ret;
Alexander Graf0d9d5012016-04-11 16:55:26 +0200154}
155
Simon Glass416e07e2018-06-18 08:08:28 -0600156/*
157 * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges
158 *
159 * The mem_rsv entries of the FDT are added to the memory map. Any failures are
160 * ignored because this is not critical and we would rather continue to try to
161 * boot.
162 *
163 * @fdt: Pointer to device tree
164 */
165static void efi_carve_out_dt_rsv(void *fdt)
Alexander Graf806d2fa2018-04-06 09:40:51 +0200166{
167 int nr_rsv, i;
168 uint64_t addr, size, pages;
169
170 nr_rsv = fdt_num_mem_rsv(fdt);
171
172 /* Look for an existing entry and add it to the efi mem map. */
173 for (i = 0; i < nr_rsv; i++) {
174 if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0)
175 continue;
176
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +0100177 /* Convert from sandbox address space. */
178 addr = (uintptr_t)map_sysmem(addr, 0);
179
Heinrich Schuchardtc3772ca2018-11-18 17:58:49 +0100180 pages = efi_size_in_pages(size + (addr & EFI_PAGE_MASK));
Heinrich Schuchardt23fd84b2018-11-12 18:55:23 +0100181 addr &= ~EFI_PAGE_MASK;
Simon Glass416e07e2018-06-18 08:08:28 -0600182 if (!efi_add_memory_map(addr, pages, EFI_RESERVED_MEMORY_TYPE,
183 false))
184 printf("FDT memrsv map %d: Failed to add to map\n", i);
Alexander Graf806d2fa2018-04-06 09:40:51 +0200185 }
Alexander Graf806d2fa2018-04-06 09:40:51 +0200186}
187
AKASHI Takahiroe878e6a2019-04-19 12:22:29 +0900188/**
AKASHI Takahiro3ffc52f2019-04-19 12:22:30 +0900189 * efi_install_fdt() - install fdt passed by a command argument
AKASHI Takahiroe878e6a2019-04-19 12:22:29 +0900190 * @fdt_opt: pointer to argument
191 * Return: status code
192 *
193 * If specified, fdt will be installed as configuration table,
194 * otherwise no fdt will be passed.
195 */
AKASHI Takahiro3ffc52f2019-04-19 12:22:30 +0900196static efi_status_t efi_install_fdt(const char *fdt_opt)
AKASHI Takahiroe878e6a2019-04-19 12:22:29 +0900197{
198 unsigned long fdt_addr;
AKASHI Takahiro3ffc52f2019-04-19 12:22:30 +0900199 void *fdt;
200 bootm_headers_t img = { 0 };
AKASHI Takahiroe878e6a2019-04-19 12:22:29 +0900201 efi_status_t ret;
202
203 if (fdt_opt) {
AKASHI Takahiro3ffc52f2019-04-19 12:22:30 +0900204 /* Install device tree */
AKASHI Takahiroe878e6a2019-04-19 12:22:29 +0900205 fdt_addr = simple_strtoul(fdt_opt, NULL, 16);
206 if (!fdt_addr && *fdt_opt != '0')
207 return EFI_INVALID_PARAMETER;
208
AKASHI Takahiro3ffc52f2019-04-19 12:22:30 +0900209 fdt = map_sysmem(fdt_addr, 0);
210 if (fdt_check_header(fdt)) {
211 printf("ERROR: invalid device tree\n");
212 return EFI_INVALID_PARAMETER;
213 }
214
215 /* Create memory reservation as indicated by the device tree */
216 efi_carve_out_dt_rsv(fdt);
217
218 /* Prepare fdt for payload */
219 ret = copy_fdt(&fdt);
220 if (ret)
221 return ret;
222
223 if (image_setup_libfdt(&img, fdt, 0, NULL)) {
224 printf("ERROR: failed to process device tree\n");
225 return EFI_LOAD_ERROR;
226 }
227
228 /* Link to it in the efi tables */
229 ret = efi_install_configuration_table(&efi_guid_fdt, fdt);
AKASHI Takahiroe878e6a2019-04-19 12:22:29 +0900230 if (ret != EFI_SUCCESS) {
231 printf("ERROR: failed to install device tree\n");
232 return ret;
233 }
234 } else {
235 /* Remove device tree. EFI_NOT_FOUND can be ignored here */
236 efi_install_configuration_table(&efi_guid_fdt, NULL);
237 }
238
239 return EFI_SUCCESS;
240}
241
Simon Glass5e2f0392018-11-25 20:14:39 -0700242/**
Heinrich Schuchardtc9828742018-09-23 17:21:51 +0200243 * do_bootefi_exec() - execute EFI binary
244 *
245 * @efi: address of the binary
246 * @device_path: path of the device from which the binary was loaded
247 * @image_path: device path of the binary
248 * Return: status code
249 *
250 * Load the EFI binary into a newly assigned memory unwinding the relocation
251 * information, install the loaded image protocol, and call the binary.
Alexander Grafb9939332016-03-10 00:27:20 +0100252 */
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100253static efi_status_t do_bootefi_exec(void *efi,
Heinrich Schuchardt3eb08412017-10-18 18:13:08 +0200254 struct efi_device_path *device_path,
255 struct efi_device_path *image_path)
Alexander Grafb9939332016-03-10 00:27:20 +0100256{
Heinrich Schuchardt8887acc2018-09-16 07:19:48 +0200257 efi_handle_t mem_handle = NULL;
Rob Clarkbf192732017-10-10 08:23:06 -0400258 struct efi_device_path *memdp = NULL;
Heinrich Schuchardt45204b12018-03-03 15:29:01 +0100259 efi_status_t ret;
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +0200260 struct efi_loaded_image_obj *image_obj = NULL;
Heinrich Schuchardtc9828742018-09-23 17:21:51 +0200261 struct efi_loaded_image *loaded_image_info = NULL;
Rob Clark95c55532017-09-13 18:05:33 -0400262
Rob Clarkbf192732017-10-10 08:23:06 -0400263 /*
264 * Special case for efi payload not loaded from disk, such as
265 * 'bootefi hello' or for example payload loaded directly into
Heinrich Schuchardte1fec152018-10-18 21:51:38 +0200266 * memory via JTAG, etc:
Rob Clarkbf192732017-10-10 08:23:06 -0400267 */
268 if (!device_path && !image_path) {
269 printf("WARNING: using memory device/image path, this may confuse some payloads!\n");
270 /* actual addresses filled in after efi_load_pe() */
Heinrich Schuchardt0a76ba62018-12-26 22:23:00 +0100271 memdp = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, 0, 0);
Rob Clarkbf192732017-10-10 08:23:06 -0400272 device_path = image_path = memdp;
Heinrich Schuchardt8887acc2018-09-16 07:19:48 +0200273 /*
274 * Grub expects that the device path of the loaded image is
275 * installed on a handle.
276 */
277 ret = efi_create_handle(&mem_handle);
278 if (ret != EFI_SUCCESS)
Simon Glass5e2f0392018-11-25 20:14:39 -0700279 return ret; /* TODO: leaks device_path */
Heinrich Schuchardt8887acc2018-09-16 07:19:48 +0200280 ret = efi_add_protocol(mem_handle, &efi_guid_device_path,
Alexander Graf58bc69d2018-06-12 10:21:16 +0200281 device_path);
282 if (ret != EFI_SUCCESS)
Simon Glass5e2f0392018-11-25 20:14:39 -0700283 goto err_add_protocol;
Rob Clarkbf192732017-10-10 08:23:06 -0400284 } else {
285 assert(device_path && image_path);
286 }
287
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900288 ret = efi_setup_loaded_image(device_path, image_path, &image_obj,
289 &loaded_image_info);
Simon Glassf4f0f7c2018-11-25 20:14:38 -0700290 if (ret)
Simon Glass5e2f0392018-11-25 20:14:39 -0700291 goto err_prepare;
Rob Clark95c55532017-09-13 18:05:33 -0400292
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900293 /* Transfer environment variable as load options */
294 ret = set_load_options((efi_handle_t)image_obj, "bootargs");
295 if (ret != EFI_SUCCESS)
296 goto err_prepare;
297
Alexander Grafb9939332016-03-10 00:27:20 +0100298 /* Load the EFI payload */
Heinrich Schuchardt8f7e2b22018-12-26 12:49:09 +0100299 ret = efi_load_pe(image_obj, efi, loaded_image_info);
300 if (ret != EFI_SUCCESS)
Simon Glass5e2f0392018-11-25 20:14:39 -0700301 goto err_prepare;
Alexander Graf80a48002016-08-16 21:08:45 +0200302
Rob Clarkbf192732017-10-10 08:23:06 -0400303 if (memdp) {
304 struct efi_device_path_memory *mdp = (void *)memdp;
Heinrich Schuchardtc9828742018-09-23 17:21:51 +0200305 mdp->memory_type = loaded_image_info->image_code_type;
306 mdp->start_address = (uintptr_t)loaded_image_info->image_base;
Rob Clarkbf192732017-10-10 08:23:06 -0400307 mdp->end_address = mdp->start_address +
Heinrich Schuchardtc9828742018-09-23 17:21:51 +0200308 loaded_image_info->image_size;
Rob Clarkbf192732017-10-10 08:23:06 -0400309 }
310
Rob Clarkad644e72017-09-13 18:05:37 -0400311 /* we don't support much: */
312 env_set("efi_8be4df61-93ca-11d2-aa0d-00e098032b8c_OsIndicationsSupported",
313 "{ro,boot}(blob)0000000000000000");
314
Alexander Grafb9939332016-03-10 00:27:20 +0100315 /* Call our payload! */
Heinrich Schuchardt8f7e2b22018-12-26 12:49:09 +0100316 debug("%s: Jumping to 0x%p\n", __func__, image_obj->entry);
Heinrich Schuchardtf69d63f2018-12-26 13:28:09 +0100317 ret = EFI_CALL(efi_start_image(&image_obj->header, NULL, NULL));
Rob Clark95c55532017-09-13 18:05:33 -0400318
Simon Glass5e2f0392018-11-25 20:14:39 -0700319err_prepare:
Rob Clark95c55532017-09-13 18:05:33 -0400320 /* image has returned, loaded-image obj goes *poof*: */
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900321 efi_restore_gd();
322 free(loaded_image_info->load_options);
323 efi_delete_handle(&image_obj->header);
Simon Glass5e2f0392018-11-25 20:14:39 -0700324
325err_add_protocol:
Heinrich Schuchardt8887acc2018-09-16 07:19:48 +0200326 if (mem_handle)
327 efi_delete_handle(mem_handle);
Rob Clark95c55532017-09-13 18:05:33 -0400328
329 return ret;
Alexander Grafb9939332016-03-10 00:27:20 +0100330}
331
AKASHI Takahirocc999d52019-04-19 12:22:32 +0900332static int do_bootefi_bootmgr_exec(void)
333{
334 struct efi_device_path *device_path, *file_path;
335 void *addr;
336 efi_status_t r;
337
338 addr = efi_bootmgr_load(&device_path, &file_path);
339 if (!addr)
340 return 1;
341
342 printf("## Starting EFI application at %p ...\n", addr);
343 r = do_bootefi_exec(addr, device_path, file_path);
344 printf("## Application terminated, r = %lu\n",
345 r & ~EFI_ERROR_MASK);
346
347 if (r != EFI_SUCCESS)
348 return 1;
349
350 return 0;
351}
352
Simon Glassd9717ea2018-11-25 20:14:37 -0700353#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900354static efi_status_t bootefi_run_prepare(const char *load_options_path,
355 struct efi_device_path *device_path,
356 struct efi_device_path *image_path,
357 struct efi_loaded_image_obj **image_objp,
358 struct efi_loaded_image **loaded_image_infop)
359{
360 efi_status_t ret;
361
362 ret = efi_setup_loaded_image(device_path, image_path, image_objp,
363 loaded_image_infop);
364 if (ret != EFI_SUCCESS)
365 return ret;
366
367 /* Transfer environment variable as load options */
368 return set_load_options((efi_handle_t)*image_objp, load_options_path);
369}
370
Simon Glassd9717ea2018-11-25 20:14:37 -0700371/**
372 * bootefi_test_prepare() - prepare to run an EFI test
373 *
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100374 * Prepare to run a test as if it were provided by a loaded image.
Simon Glassd9717ea2018-11-25 20:14:37 -0700375 *
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100376 * @image_objp: pointer to be set to the loaded image handle
377 * @loaded_image_infop: pointer to be set to the loaded image protocol
378 * @path: dummy file path used to construct the device path
379 * set in the loaded image protocol
380 * @load_options_path: name of a U-Boot environment variable. Its value is
381 * set as load options in the loaded image protocol.
382 * Return: status code
Simon Glassd9717ea2018-11-25 20:14:37 -0700383 */
384static efi_status_t bootefi_test_prepare
385 (struct efi_loaded_image_obj **image_objp,
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100386 struct efi_loaded_image **loaded_image_infop, const char *path,
387 const char *load_options_path)
Simon Glassd9717ea2018-11-25 20:14:37 -0700388{
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100389 efi_status_t ret;
390
Simon Glassd9717ea2018-11-25 20:14:37 -0700391 /* Construct a dummy device path */
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100392 bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, 0, 0);
Simon Glassd9717ea2018-11-25 20:14:37 -0700393 if (!bootefi_device_path)
394 return EFI_OUT_OF_RESOURCES;
Simon Glassd9717ea2018-11-25 20:14:37 -0700395
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100396 bootefi_image_path = efi_dp_from_file(NULL, 0, path);
397 if (!bootefi_image_path) {
398 ret = EFI_OUT_OF_RESOURCES;
399 goto failure;
400 }
401
402 ret = bootefi_run_prepare(load_options_path, bootefi_device_path,
403 bootefi_image_path, image_objp,
404 loaded_image_infop);
405 if (ret == EFI_SUCCESS)
406 return ret;
407
408 efi_free_pool(bootefi_image_path);
409 bootefi_image_path = NULL;
410failure:
411 efi_free_pool(bootefi_device_path);
412 bootefi_device_path = NULL;
413 return ret;
Simon Glassd9717ea2018-11-25 20:14:37 -0700414}
415
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900416/**
417 * bootefi_run_finish() - finish up after running an EFI test
418 *
419 * @loaded_image_info: Pointer to a struct which holds the loaded image info
420 * @image_obj: Pointer to a struct which holds the loaded image object
421 */
422static void bootefi_run_finish(struct efi_loaded_image_obj *image_obj,
423 struct efi_loaded_image *loaded_image_info)
424{
425 efi_restore_gd();
426 free(loaded_image_info->load_options);
427 efi_delete_handle(&image_obj->header);
428}
429
430/**
431 * do_efi_selftest() - execute EFI Selftest
432 *
433 * @fdt_opt: string of fdt start address
434 * Return: status code
435 *
436 * Execute EFI Selftest
437 */
438static int do_efi_selftest(const char *fdt_opt)
439{
440 struct efi_loaded_image_obj *image_obj;
441 struct efi_loaded_image *loaded_image_info;
442 efi_status_t ret;
443
444 /* Allow unaligned memory access */
445 allow_unaligned();
446
447 switch_to_non_secure_mode();
448
449 /* Initialize EFI drivers */
450 ret = efi_init_obj_list();
451 if (ret != EFI_SUCCESS) {
452 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
453 ret & ~EFI_ERROR_MASK);
454 return CMD_RET_FAILURE;
455 }
456
457 ret = efi_install_fdt(fdt_opt);
458 if (ret == EFI_INVALID_PARAMETER)
459 return CMD_RET_USAGE;
460 else if (ret != EFI_SUCCESS)
461 return CMD_RET_FAILURE;
462
463 ret = bootefi_test_prepare(&image_obj, &loaded_image_info,
464 "\\selftest", "efi_selftest");
465 if (ret != EFI_SUCCESS)
466 return CMD_RET_FAILURE;
467
468 /* Execute the test */
469 ret = EFI_CALL(efi_selftest(&image_obj->header, &systab));
470 bootefi_run_finish(image_obj, loaded_image_info);
471
472 return ret != EFI_SUCCESS;
473}
Simon Glassd9717ea2018-11-25 20:14:37 -0700474#endif /* CONFIG_CMD_BOOTEFI_SELFTEST */
475
Alexander Grafb9939332016-03-10 00:27:20 +0100476/* Interpreter command to boot an arbitrary EFI image from memory */
477static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
478{
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100479 unsigned long addr;
480 char *saddr;
Heinrich Schuchardt3eb08412017-10-18 18:13:08 +0200481 efi_status_t r;
Alexander Grafb9939332016-03-10 00:27:20 +0100482
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900483 if (argc < 2)
484 return CMD_RET_USAGE;
485#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
486 else if (!strcmp(argv[1], "selftest"))
487 return do_efi_selftest(argc > 2 ? argv[2] : NULL);
488#endif
489
Heinrich Schuchardtc3b11de2018-04-03 21:59:32 +0200490 /* Allow unaligned memory access */
491 allow_unaligned();
492
Heinrich Schuchardtf6c6df72019-01-08 18:13:06 +0100493 switch_to_non_secure_mode();
494
Heinrich Schuchardtfc225e62018-03-03 15:29:02 +0100495 /* Initialize EFI drivers */
496 r = efi_init_obj_list();
497 if (r != EFI_SUCCESS) {
498 printf("Error: Cannot set up EFI drivers, r = %lu\n",
499 r & ~EFI_ERROR_MASK);
500 return CMD_RET_FAILURE;
501 }
502
AKASHI Takahiro3ffc52f2019-04-19 12:22:30 +0900503 r = efi_install_fdt(argc > 2 ? argv[2] : NULL);
AKASHI Takahiroe878e6a2019-04-19 12:22:29 +0900504 if (r == EFI_INVALID_PARAMETER)
505 return CMD_RET_USAGE;
506 else if (r != EFI_SUCCESS)
507 return CMD_RET_FAILURE;
508
Simon Glassc7ae3df2016-11-07 08:47:08 -0700509#ifdef CONFIG_CMD_BOOTEFI_HELLO
510 if (!strcmp(argv[1], "hello")) {
Heinrich Schuchardt5e444892017-09-05 03:19:37 +0200511 ulong size = __efi_helloworld_end - __efi_helloworld_begin;
Alexander Grafb9939332016-03-10 00:27:20 +0100512
Heinrich Schuchardt51c533f2017-08-28 18:54:30 +0200513 saddr = env_get("loadaddr");
514 if (saddr)
515 addr = simple_strtoul(saddr, NULL, 16);
516 else
517 addr = CONFIG_SYS_LOAD_ADDR;
Alexander Graf354264b2018-06-18 17:22:58 +0200518 memcpy(map_sysmem(addr, size), __efi_helloworld_begin, size);
Simon Glassc7ae3df2016-11-07 08:47:08 -0700519 } else
520#endif
Rob Clark9975fe92017-09-13 18:05:38 -0400521 if (!strcmp(argv[1], "bootmgr")) {
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100522 return do_bootefi_bootmgr_exec();
Rob Clark9975fe92017-09-13 18:05:38 -0400523 } else {
Simon Glassc7ae3df2016-11-07 08:47:08 -0700524 saddr = argv[1];
Alexander Grafb9939332016-03-10 00:27:20 +0100525
Simon Glassc7ae3df2016-11-07 08:47:08 -0700526 addr = simple_strtoul(saddr, NULL, 16);
Heinrich Schuchardt49db1cb2018-01-24 20:33:54 +0100527 /* Check that a numeric value was passed */
528 if (!addr && *saddr != '0')
529 return CMD_RET_USAGE;
Simon Glassc7ae3df2016-11-07 08:47:08 -0700530
Alexander Graf1c398092016-04-14 16:07:53 +0200531 }
532
Simon Glass5ee31ba2016-11-07 08:47:05 -0700533 printf("## Starting EFI application at %08lx ...\n", addr);
Alexander Graf354264b2018-06-18 17:22:58 +0200534 r = do_bootefi_exec(map_sysmem(addr, 0), bootefi_device_path,
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100535 bootefi_image_path);
xypron.glpk@gmx.de1da1bac2017-07-04 23:15:23 +0200536 printf("## Application terminated, r = %lu\n",
537 r & ~EFI_ERROR_MASK);
Alexander Grafb9939332016-03-10 00:27:20 +0100538
xypron.glpk@gmx.de1da1bac2017-07-04 23:15:23 +0200539 if (r != EFI_SUCCESS)
540 return 1;
541 else
542 return 0;
Alexander Grafb9939332016-03-10 00:27:20 +0100543}
544
545#ifdef CONFIG_SYS_LONGHELP
546static char bootefi_help_text[] =
Alexander Graf1c398092016-04-14 16:07:53 +0200547 "<image address> [fdt address]\n"
548 " - boot EFI payload stored at address <image address>.\n"
549 " If specified, the device tree located at <fdt address> gets\n"
Simon Glassc7ae3df2016-11-07 08:47:08 -0700550 " exposed as EFI configuration table.\n"
551#ifdef CONFIG_CMD_BOOTEFI_HELLO
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200552 "bootefi hello\n"
553 " - boot a sample Hello World application stored within U-Boot\n"
554#endif
555#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100556 "bootefi selftest [fdt address]\n"
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200557 " - boot an EFI selftest application stored within U-Boot\n"
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200558 " Use environment variable efi_selftest to select a single test.\n"
559 " Use 'setenv efi_selftest list' to enumerate all tests.\n"
Simon Glassc7ae3df2016-11-07 08:47:08 -0700560#endif
Heinrich Schuchardtf623e072018-01-30 19:47:43 +0100561 "bootefi bootmgr [fdt addr]\n"
Rob Clark9975fe92017-09-13 18:05:38 -0400562 " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
563 "\n"
564 " If specified, the device tree located at <fdt address> gets\n"
565 " exposed as EFI configuration table.\n";
Alexander Grafb9939332016-03-10 00:27:20 +0100566#endif
567
568U_BOOT_CMD(
Alexander Graf1c398092016-04-14 16:07:53 +0200569 bootefi, 3, 0, do_bootefi,
Sergey Kubushyn92dfd922016-06-07 11:14:31 -0700570 "Boots an EFI payload from memory",
Alexander Grafb9939332016-03-10 00:27:20 +0100571 bootefi_help_text
572);
Alexander Graf0f4060e2016-03-04 01:10:14 +0100573
Alexander Grafc07ad7c2016-04-11 16:16:19 +0200574void efi_set_bootdev(const char *dev, const char *devnr, const char *path)
Alexander Graf0f4060e2016-03-04 01:10:14 +0100575{
AKASHI Takahirof1589ff2018-10-17 16:32:03 +0900576 struct efi_device_path *device, *image;
577 efi_status_t ret;
Alexander Graf0f4060e2016-03-04 01:10:14 +0100578
Heinrich Schuchardt79276eb2018-09-16 07:20:21 +0200579 /* efi_set_bootdev is typically called repeatedly, recover memory */
580 efi_free_pool(bootefi_device_path);
581 efi_free_pool(bootefi_image_path);
Heinrich Schuchardt79276eb2018-09-16 07:20:21 +0200582
AKASHI Takahirof1589ff2018-10-17 16:32:03 +0900583 ret = efi_dp_from_name(dev, devnr, path, &device, &image);
584 if (ret == EFI_SUCCESS) {
585 bootefi_device_path = device;
586 bootefi_image_path = image;
Rob Clark95c55532017-09-13 18:05:33 -0400587 } else {
AKASHI Takahirof1589ff2018-10-17 16:32:03 +0900588 bootefi_device_path = NULL;
589 bootefi_image_path = NULL;
Alexander Graff9d334b2016-08-05 14:49:53 +0200590 }
Alexander Graf0f4060e2016-03-04 01:10:14 +0100591}