blob: 81dd8e0284f7084cae37dac32e69e541c2094548 [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 Schuchardtc0018372020-07-17 20:21:00 +02008#define LOG_CATEGORY LOGC_EFI
9
Alexander Grafb9939332016-03-10 00:27:20 +010010#include <common.h>
Heinrich Schuchardt82d01f02021-01-24 14:34:12 +000011#include <bootm.h>
Heinrich Schuchardtf6c6df72019-01-08 18:13:06 +010012#include <charset.h>
Alexander Grafb9939332016-03-10 00:27:20 +010013#include <command.h>
Simon Glass9d922452017-05-17 17:18:03 -060014#include <dm.h>
Alexander Grafb9939332016-03-10 00:27:20 +010015#include <efi_loader.h>
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +020016#include <efi_selftest.h>
Simon Glass7b51b572019-08-01 09:46:52 -060017#include <env.h>
Alexander Grafb9939332016-03-10 00:27:20 +010018#include <errno.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060019#include <image.h>
Heinrich Schuchardtc0018372020-07-17 20:21:00 +020020#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070021#include <malloc.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090022#include <linux/libfdt.h>
23#include <linux/libfdt_env.h>
Alexander Graf354264b2018-06-18 17:22:58 +020024#include <mapmem.h>
Alexander Grafad0c1a32016-04-11 23:51:01 +020025#include <memalign.h>
Simon Glasse2754582016-09-25 15:27:32 -060026#include <asm-generic/sections.h>
27#include <linux/linkage.h>
Alexander Graf0d9d5012016-04-11 16:55:26 +020028
29DECLARE_GLOBAL_DATA_PTR;
Alexander Grafb9939332016-03-10 00:27:20 +010030
Rob Clark95c55532017-09-13 18:05:33 -040031static struct efi_device_path *bootefi_image_path;
32static struct efi_device_path *bootefi_device_path;
Heinrich Schuchardt5f595182021-01-12 12:46:24 +010033static void *image_addr;
34static size_t image_size;
35
36/**
37 * efi_clear_bootdev() - clear boot device
38 */
39static void efi_clear_bootdev(void)
40{
41 efi_free_pool(bootefi_device_path);
42 efi_free_pool(bootefi_image_path);
43 bootefi_device_path = NULL;
44 bootefi_image_path = NULL;
45 image_addr = NULL;
46 image_size = 0;
47}
48
49/**
50 * efi_set_bootdev() - set boot device
51 *
52 * This function is called when a file is loaded, e.g. via the 'load' command.
53 * We use the path to this file to inform the UEFI binary about the boot device.
54 *
55 * @dev: device, e.g. "MMC"
56 * @devnr: number of the device, e.g. "1:2"
57 * @path: path to file loaded
58 * @buffer: buffer with file loaded
59 * @buffer_size: size of file loaded
60 */
61void efi_set_bootdev(const char *dev, const char *devnr, const char *path,
62 void *buffer, size_t buffer_size)
63{
64 struct efi_device_path *device, *image;
65 efi_status_t ret;
66
67 /* Forget overwritten image */
68 if (buffer + buffer_size >= image_addr &&
69 image_addr + image_size >= buffer)
70 efi_clear_bootdev();
71
72 /* Remember only PE-COFF and FIT images */
73 if (efi_check_pe(buffer, buffer_size, NULL) != EFI_SUCCESS) {
74#ifdef CONFIG_FIT
75 if (!fit_check_format(buffer))
76 return;
77 /*
78 * FIT images of type EFI_OS are started via command bootm.
79 * We should not use their boot device with the bootefi command.
80 */
81 buffer = 0;
82 buffer_size = 0;
83#else
84 return;
85#endif
86 }
87
88 /* efi_set_bootdev() is typically called repeatedly, recover memory */
89 efi_clear_bootdev();
90
91 image_addr = buffer;
92 image_size = buffer_size;
93
94 ret = efi_dp_from_name(dev, devnr, path, &device, &image);
95 if (ret == EFI_SUCCESS) {
96 bootefi_device_path = device;
97 if (image) {
98 /* FIXME: image should not contain device */
99 struct efi_device_path *image_tmp = image;
100
101 efi_dp_split_file_path(image, &device, &image);
102 efi_free_pool(image_tmp);
103 }
104 bootefi_image_path = image;
105 } else {
106 efi_clear_bootdev();
107 }
108}
Alexander Grafb9939332016-03-10 00:27:20 +0100109
Heinrich Schuchardt810371a2019-07-14 13:00:44 +0200110/**
Heinrich Schuchardt1064d042020-08-07 17:47:13 +0200111 * efi_env_set_load_options() - set load options from environment variable
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200112 *
Heinrich Schuchardta3850e42020-01-03 22:53:42 +0100113 * @handle: the image handle
114 * @env_var: name of the environment variable
115 * @load_options: pointer to load options (output)
116 * Return: status code
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200117 */
Heinrich Schuchardt1064d042020-08-07 17:47:13 +0200118static efi_status_t efi_env_set_load_options(efi_handle_t handle,
119 const char *env_var,
120 u16 **load_options)
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200121{
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200122 const char *env = env_get(env_var);
Heinrich Schuchardt1064d042020-08-07 17:47:13 +0200123 size_t size;
Heinrich Schuchardt7086a712018-08-31 21:31:33 +0200124 u16 *pos;
AKASHI Takahirodefa7b82019-04-19 12:22:28 +0900125 efi_status_t ret;
126
Heinrich Schuchardta3850e42020-01-03 22:53:42 +0100127 *load_options = NULL;
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200128 if (!env)
Heinrich Schuchardt1064d042020-08-07 17:47:13 +0200129 return EFI_SUCCESS;
130 size = sizeof(u16) * (utf8_utf16_strlen(env) + 1);
131 pos = calloc(size, 1);
132 if (!pos)
AKASHI Takahirodefa7b82019-04-19 12:22:28 +0900133 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardta3850e42020-01-03 22:53:42 +0100134 *load_options = pos;
Heinrich Schuchardt7086a712018-08-31 21:31:33 +0200135 utf8_utf16_strcpy(&pos, env);
Heinrich Schuchardt1064d042020-08-07 17:47:13 +0200136 ret = efi_set_load_options(handle, size, *load_options);
137 if (ret != EFI_SUCCESS) {
138 free(*load_options);
139 *load_options = NULL;
140 }
141 return ret;
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200142}
143
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200144#if !CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE)
145
Simon Glass9dff4902018-08-08 03:54:30 -0600146/**
147 * copy_fdt() - Copy the device tree to a new location available to EFI
148 *
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +0100149 * The FDT is copied to a suitable location within the EFI memory map.
Heinrich Schuchardtc3772ca2018-11-18 17:58:49 +0100150 * Additional 12 KiB are added to the space in case the device tree needs to be
Simon Glass9dff4902018-08-08 03:54:30 -0600151 * expanded later with fdt_open_into().
152 *
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +0100153 * @fdtp: On entry a pointer to the flattened device tree.
154 * On exit a pointer to the copy of the flattened device tree.
Heinrich Schuchardt4574d1b2018-11-12 18:55:22 +0100155 * FDT start
156 * Return: status code
Simon Glass9dff4902018-08-08 03:54:30 -0600157 */
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +0100158static efi_status_t copy_fdt(void **fdtp)
Alexander Graf0d9d5012016-04-11 16:55:26 +0200159{
Alexander Grafad0c1a32016-04-11 23:51:01 +0200160 unsigned long fdt_ram_start = -1L, fdt_pages;
Simon Glass9dff4902018-08-08 03:54:30 -0600161 efi_status_t ret = 0;
162 void *fdt, *new_fdt;
Alexander Grafad0c1a32016-04-11 23:51:01 +0200163 u64 new_fdt_addr;
Simon Glass9dff4902018-08-08 03:54:30 -0600164 uint fdt_size;
Alexander Grafad0c1a32016-04-11 23:51:01 +0200165 int i;
Alexander Graf0d9d5012016-04-11 16:55:26 +0200166
Simon Glass9dff4902018-08-08 03:54:30 -0600167 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
168 u64 ram_start = gd->bd->bi_dram[i].start;
169 u64 ram_size = gd->bd->bi_dram[i].size;
Alexander Graf0d9d5012016-04-11 16:55:26 +0200170
Alexander Grafad0c1a32016-04-11 23:51:01 +0200171 if (!ram_size)
172 continue;
173
174 if (ram_start < fdt_ram_start)
175 fdt_ram_start = ram_start;
176 }
177
Simon Glassbc9a6382018-06-18 08:08:25 -0600178 /*
Heinrich Schuchardtc3772ca2018-11-18 17:58:49 +0100179 * Give us at least 12 KiB of breathing room in case the device tree
180 * needs to be expanded later.
Simon Glassbc9a6382018-06-18 08:08:25 -0600181 */
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +0100182 fdt = *fdtp;
Heinrich Schuchardtc3772ca2018-11-18 17:58:49 +0100183 fdt_pages = efi_size_in_pages(fdt_totalsize(fdt) + 0x3000);
184 fdt_size = fdt_pages << EFI_PAGE_SHIFT;
Alexander Grafad0c1a32016-04-11 23:51:01 +0200185
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +0100186 /*
187 * Safe fdt location is at 127 MiB.
188 * On the sandbox convert from the sandbox address space.
189 */
190 new_fdt_addr = (uintptr_t)map_sysmem(fdt_ram_start + 0x7f00000 +
191 fdt_size, 0);
Simon Glass9dff4902018-08-08 03:54:30 -0600192 ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
Heinrich Schuchardt42a426e2020-05-06 20:32:31 +0200193 EFI_ACPI_RECLAIM_MEMORY, fdt_pages,
Simon Glass9dff4902018-08-08 03:54:30 -0600194 &new_fdt_addr);
195 if (ret != EFI_SUCCESS) {
Alexander Grafad0c1a32016-04-11 23:51:01 +0200196 /* If we can't put it there, put it somewhere */
xypron.glpk@gmx.dea44bffc2017-08-11 21:19:25 +0200197 new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
Simon Glass9dff4902018-08-08 03:54:30 -0600198 ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
Heinrich Schuchardt42a426e2020-05-06 20:32:31 +0200199 EFI_ACPI_RECLAIM_MEMORY, fdt_pages,
Simon Glass9dff4902018-08-08 03:54:30 -0600200 &new_fdt_addr);
201 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200202 log_err("ERROR: Failed to reserve space for FDT\n");
Simon Glass9dff4902018-08-08 03:54:30 -0600203 goto done;
Alexander Graf85a6e9b2017-07-03 13:32:35 +0200204 }
Alexander Grafad0c1a32016-04-11 23:51:01 +0200205 }
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +0100206 new_fdt = (void *)(uintptr_t)new_fdt_addr;
Alexander Graf0d9d5012016-04-11 16:55:26 +0200207 memcpy(new_fdt, fdt, fdt_totalsize(fdt));
208 fdt_set_totalsize(new_fdt, fdt_size);
209
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +0100210 *fdtp = (void *)(uintptr_t)new_fdt_addr;
Simon Glass9dff4902018-08-08 03:54:30 -0600211done:
212 return ret;
Alexander Graf0d9d5012016-04-11 16:55:26 +0200213}
214
Heinrich Schuchardt4cbb2932020-08-27 12:52:20 +0200215/**
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200216 * get_config_table() - get configuration table
217 *
218 * @guid: GUID of the configuration table
219 * Return: pointer to configuration table or NULL
220 */
221static void *get_config_table(const efi_guid_t *guid)
222{
223 size_t i;
224
225 for (i = 0; i < systab.nr_tables; i++) {
226 if (!guidcmp(guid, &systab.tables[i].guid))
227 return systab.tables[i].table;
228 }
229 return NULL;
230}
231
232#endif /* !CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE) */
233
234/**
Heinrich Schuchardt7a597252019-11-28 06:46:09 +0100235 * efi_install_fdt() - install device tree
Heinrich Schuchardte2d82f82019-05-12 20:16:25 +0200236 *
Heinrich Schuchardt7d4d5512020-02-07 22:10:49 +0100237 * If fdt is not EFI_FDT_USE_INTERNAL, the device tree located at that memory
238 * address will will be installed as configuration table, otherwise the device
239 * tree located at the address indicated by environment variable fdt_addr or as
240 * fallback fdtcontroladdr will be used.
Heinrich Schuchardte2d82f82019-05-12 20:16:25 +0200241 *
Heinrich Schuchardt7a597252019-11-28 06:46:09 +0100242 * On architectures using ACPI tables device trees shall not be installed as
243 * configuration table.
Heinrich Schuchardte2d82f82019-05-12 20:16:25 +0200244 *
Heinrich Schuchardt7d4d5512020-02-07 22:10:49 +0100245 * @fdt: address of device tree or EFI_FDT_USE_INTERNAL to use the
Heinrich Schuchardt753aa182019-12-04 12:31:12 +0100246 * the hardware device tree as indicated by environment variable
247 * fdt_addr or as fallback the internal device tree as indicated by
248 * the environment variable fdtcontroladdr
AKASHI Takahiroe878e6a2019-04-19 12:22:29 +0900249 * Return: status code
AKASHI Takahiroe878e6a2019-04-19 12:22:29 +0900250 */
Heinrich Schuchardtf64f2232019-12-08 01:07:01 +0100251efi_status_t efi_install_fdt(void *fdt)
AKASHI Takahiroe878e6a2019-04-19 12:22:29 +0900252{
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200253 /*
254 * The EBBR spec requires that we have either an FDT or an ACPI table
255 * but not both.
256 */
257#if CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE)
Heinrich Schuchardtf64f2232019-12-08 01:07:01 +0100258 if (fdt) {
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200259 log_err("ERROR: can't have ACPI table and device tree.\n");
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200260 return EFI_LOAD_ERROR;
261 }
262#else
AKASHI Takahiro3ffc52f2019-04-19 12:22:30 +0900263 bootm_headers_t img = { 0 };
AKASHI Takahiroe878e6a2019-04-19 12:22:29 +0900264 efi_status_t ret;
265
Heinrich Schuchardtf64f2232019-12-08 01:07:01 +0100266 if (fdt == EFI_FDT_USE_INTERNAL) {
Heinrich Schuchardt7a597252019-11-28 06:46:09 +0100267 const char *fdt_opt;
Heinrich Schuchardtf64f2232019-12-08 01:07:01 +0100268 uintptr_t fdt_addr;
Heinrich Schuchardt7a597252019-11-28 06:46:09 +0100269
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200270 /* Look for device tree that is already installed */
271 if (get_config_table(&efi_guid_fdt))
272 return EFI_SUCCESS;
Heinrich Schuchardt753aa182019-12-04 12:31:12 +0100273 /* Check if there is a hardware device tree */
274 fdt_opt = env_get("fdt_addr");
275 /* Use our own device tree as fallback */
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200276 if (!fdt_opt) {
Heinrich Schuchardt753aa182019-12-04 12:31:12 +0100277 fdt_opt = env_get("fdtcontroladdr");
278 if (!fdt_opt) {
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200279 log_err("ERROR: need device tree\n");
Heinrich Schuchardt753aa182019-12-04 12:31:12 +0100280 return EFI_NOT_FOUND;
281 }
AKASHI Takahiro3ffc52f2019-04-19 12:22:30 +0900282 }
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200283 fdt_addr = simple_strtoul(fdt_opt, NULL, 16);
284 if (!fdt_addr) {
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200285 log_err("ERROR: invalid $fdt_addr or $fdtcontroladdr\n");
AKASHI Takahiro3ffc52f2019-04-19 12:22:30 +0900286 return EFI_LOAD_ERROR;
287 }
Heinrich Schuchardtf64f2232019-12-08 01:07:01 +0100288 fdt = map_sysmem(fdt_addr, 0);
AKASHI Takahiroe878e6a2019-04-19 12:22:29 +0900289 }
290
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200291 /* Install device tree */
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200292 if (fdt_check_header(fdt)) {
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200293 log_err("ERROR: invalid device tree\n");
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200294 return EFI_LOAD_ERROR;
295 }
296
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200297 /* Prepare device tree for payload */
298 ret = copy_fdt(&fdt);
299 if (ret) {
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200300 log_err("ERROR: out of memory\n");
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200301 return EFI_OUT_OF_RESOURCES;
302 }
303
304 if (image_setup_libfdt(&img, fdt, 0, NULL)) {
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200305 log_err("ERROR: failed to process device tree\n");
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200306 return EFI_LOAD_ERROR;
307 }
308
Heinrich Schuchardtfef907b2020-03-14 10:59:34 +0100309 /* Create memory reservations as indicated by the device tree */
310 efi_carve_out_dt_rsv(fdt);
311
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200312 /* Install device tree as UEFI table */
313 ret = efi_install_configuration_table(&efi_guid_fdt, fdt);
314 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200315 log_err("ERROR: failed to install device tree\n");
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200316 return ret;
317 }
318#endif /* GENERATE_ACPI_TABLE */
319
AKASHI Takahiroe878e6a2019-04-19 12:22:29 +0900320 return EFI_SUCCESS;
321}
322
Simon Glass5e2f0392018-11-25 20:14:39 -0700323/**
Heinrich Schuchardtc9828742018-09-23 17:21:51 +0200324 * do_bootefi_exec() - execute EFI binary
325 *
Heinrich Schuchardt72e1fca2020-08-15 23:10:22 +0200326 * The image indicated by @handle is started. When it returns the allocated
327 * memory for the @load_options is freed.
328 *
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900329 * @handle: handle of loaded image
Heinrich Schuchardt72e1fca2020-08-15 23:10:22 +0200330 * @load_options: load options
Heinrich Schuchardtc9828742018-09-23 17:21:51 +0200331 * Return: status code
332 *
333 * Load the EFI binary into a newly assigned memory unwinding the relocation
334 * information, install the loaded image protocol, and call the binary.
Alexander Grafb9939332016-03-10 00:27:20 +0100335 */
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200336static efi_status_t do_bootefi_exec(efi_handle_t handle, void *load_options)
Alexander Grafb9939332016-03-10 00:27:20 +0100337{
Heinrich Schuchardt45204b12018-03-03 15:29:01 +0100338 efi_status_t ret;
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +0200339 efi_uintn_t exit_data_size = 0;
340 u16 *exit_data = NULL;
Rob Clarkbf192732017-10-10 08:23:06 -0400341
Heinrich Schuchardt82d01f02021-01-24 14:34:12 +0000342 /* On ARM switch from EL3 or secure mode to EL2 or non-secure mode */
343 switch_to_non_secure_mode();
344
Alexander Grafb9939332016-03-10 00:27:20 +0100345 /* Call our payload! */
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +0200346 ret = EFI_CALL(efi_start_image(handle, &exit_data_size, &exit_data));
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200347 if (ret != EFI_SUCCESS) {
348 log_err("## Application failed, r = %lu\n",
349 ret & ~EFI_ERROR_MASK);
350 if (exit_data) {
351 log_err("## %ls\n", exit_data);
352 efi_free_pool(exit_data);
353 }
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +0200354 }
Rob Clark95c55532017-09-13 18:05:33 -0400355
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900356 efi_restore_gd();
Simon Glass5e2f0392018-11-25 20:14:39 -0700357
Heinrich Schuchardta3850e42020-01-03 22:53:42 +0100358 free(load_options);
Rob Clark95c55532017-09-13 18:05:33 -0400359
360 return ret;
Alexander Grafb9939332016-03-10 00:27:20 +0100361}
362
AKASHI Takahirod6b21892019-04-19 12:22:33 +0900363/**
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200364 * do_efibootmgr() - execute EFI boot manager
AKASHI Takahirod6b21892019-04-19 12:22:33 +0900365 *
AKASHI Takahirod6b21892019-04-19 12:22:33 +0900366 * Return: status code
AKASHI Takahirod6b21892019-04-19 12:22:33 +0900367 */
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200368static int do_efibootmgr(void)
AKASHI Takahirocc999d52019-04-19 12:22:32 +0900369{
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900370 efi_handle_t handle;
AKASHI Takahirod6b21892019-04-19 12:22:33 +0900371 efi_status_t ret;
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200372 void *load_options;
AKASHI Takahirod6b21892019-04-19 12:22:33 +0900373
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200374 ret = efi_bootmgr_load(&handle, &load_options);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900375 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200376 log_notice("EFI boot manager: Cannot load any image\n");
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900377 return CMD_RET_FAILURE;
378 }
AKASHI Takahirocc999d52019-04-19 12:22:32 +0900379
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200380 ret = do_bootefi_exec(handle, load_options);
AKASHI Takahirocc999d52019-04-19 12:22:32 +0900381
AKASHI Takahirod6b21892019-04-19 12:22:33 +0900382 if (ret != EFI_SUCCESS)
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900383 return CMD_RET_FAILURE;
AKASHI Takahirocc999d52019-04-19 12:22:32 +0900384
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900385 return CMD_RET_SUCCESS;
AKASHI Takahirocc999d52019-04-19 12:22:32 +0900386}
387
Heinrich Schuchardt810371a2019-07-14 13:00:44 +0200388/**
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200389 * do_bootefi_image() - execute EFI binary
390 *
391 * Set up memory image for the binary to be loaded, prepare device path, and
392 * then call do_bootefi_exec() to execute it.
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900393 *
394 * @image_opt: string of image start address
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900395 * Return: status code
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900396 */
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200397static int do_bootefi_image(const char *image_opt)
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900398{
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900399 void *image_buf;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900400 unsigned long addr, size;
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900401 efi_status_t ret;
402
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900403#ifdef CONFIG_CMD_BOOTEFI_HELLO
404 if (!strcmp(image_opt, "hello")) {
Heinrich Schuchardtbb33c792021-01-12 17:44:08 +0100405 image_buf = __efi_helloworld_begin;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900406 size = __efi_helloworld_end - __efi_helloworld_begin;
Heinrich Schuchardt5f595182021-01-12 12:46:24 +0100407 efi_clear_bootdev();
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900408 } else
409#endif
410 {
Heinrich Schuchardt5f595182021-01-12 12:46:24 +0100411 addr = strtoul(image_opt, NULL, 16);
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900412 /* Check that a numeric value was passed */
Heinrich Schuchardt5f595182021-01-12 12:46:24 +0100413 if (!addr)
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900414 return CMD_RET_USAGE;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900415
Heinrich Schuchardt5f595182021-01-12 12:46:24 +0100416 image_buf = map_sysmem(addr, 0);
417
418 if (image_buf != image_addr) {
419 log_err("No UEFI binary known at %s\n", image_opt);
420 return CMD_RET_FAILURE;
421 }
422 size = image_size;
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900423 }
Heinrich Schuchardtf9ceb6a2019-12-07 20:51:06 +0100424 ret = efi_run_image(image_buf, size);
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900425
Heinrich Schuchardtf9ceb6a2019-12-07 20:51:06 +0100426 if (ret != EFI_SUCCESS)
427 return CMD_RET_FAILURE;
428
429 return CMD_RET_SUCCESS;
430}
431
432/**
433 * efi_run_image() - run loaded UEFI image
434 *
435 * @source_buffer: memory address of the UEFI image
436 * @source_size: size of the UEFI image
437 * Return: status code
438 */
439efi_status_t efi_run_image(void *source_buffer, efi_uintn_t source_size)
440{
441 efi_handle_t mem_handle = NULL, handle;
442 struct efi_device_path *file_path = NULL;
Heinrich Schuchardtc2f01032020-08-25 17:54:05 +0000443 struct efi_device_path *msg_path;
Heinrich Schuchardtf9ceb6a2019-12-07 20:51:06 +0100444 efi_status_t ret;
Heinrich Schuchardtc2f01032020-08-25 17:54:05 +0000445 u16 *load_options;
Heinrich Schuchardtf9ceb6a2019-12-07 20:51:06 +0100446
447 if (!bootefi_device_path || !bootefi_image_path) {
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900448 /*
449 * Special case for efi payload not loaded from disk,
450 * such as 'bootefi hello' or for example payload
451 * loaded directly into memory via JTAG, etc:
452 */
453 file_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
Heinrich Schuchardtf9ceb6a2019-12-07 20:51:06 +0100454 (uintptr_t)source_buffer,
455 source_size);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900456 /*
457 * Make sure that device for device_path exist
458 * in load_image(). Otherwise, shell and grub will fail.
459 */
460 ret = efi_create_handle(&mem_handle);
461 if (ret != EFI_SUCCESS)
462 goto out;
463
464 ret = efi_add_protocol(mem_handle, &efi_guid_device_path,
465 file_path);
466 if (ret != EFI_SUCCESS)
467 goto out;
Heinrich Schuchardtc2f01032020-08-25 17:54:05 +0000468 msg_path = file_path;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900469 } else {
Heinrich Schuchardtf9ceb6a2019-12-07 20:51:06 +0100470 file_path = efi_dp_append(bootefi_device_path,
471 bootefi_image_path);
Heinrich Schuchardtc2f01032020-08-25 17:54:05 +0000472 msg_path = bootefi_image_path;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900473 }
474
Heinrich Schuchardtc2f01032020-08-25 17:54:05 +0000475 log_info("Booting %pD\n", msg_path);
476
Heinrich Schuchardtf9ceb6a2019-12-07 20:51:06 +0100477 ret = EFI_CALL(efi_load_image(false, efi_root, file_path, source_buffer,
478 source_size, &handle));
Heinrich Schuchardtc2f01032020-08-25 17:54:05 +0000479 if (ret != EFI_SUCCESS) {
480 log_err("Loading image failed\n");
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900481 goto out;
Heinrich Schuchardtc2f01032020-08-25 17:54:05 +0000482 }
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200483
484 /* Transfer environment variable as load options */
485 ret = efi_env_set_load_options(handle, "bootargs", &load_options);
486 if (ret != EFI_SUCCESS)
487 goto out;
488
489 ret = do_bootefi_exec(handle, load_options);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900490
491out:
Heinrich Schuchardt4fe050e2020-04-20 12:44:56 +0200492 efi_delete_handle(mem_handle);
493 efi_free_pool(file_path);
Heinrich Schuchardtf9ceb6a2019-12-07 20:51:06 +0100494 return ret;
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900495}
496
Simon Glassd9717ea2018-11-25 20:14:37 -0700497#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900498static efi_status_t bootefi_run_prepare(const char *load_options_path,
499 struct efi_device_path *device_path,
500 struct efi_device_path *image_path,
501 struct efi_loaded_image_obj **image_objp,
502 struct efi_loaded_image **loaded_image_infop)
503{
504 efi_status_t ret;
Heinrich Schuchardta3850e42020-01-03 22:53:42 +0100505 u16 *load_options;
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900506
507 ret = efi_setup_loaded_image(device_path, image_path, image_objp,
508 loaded_image_infop);
509 if (ret != EFI_SUCCESS)
510 return ret;
511
512 /* Transfer environment variable as load options */
Heinrich Schuchardt1064d042020-08-07 17:47:13 +0200513 return efi_env_set_load_options((efi_handle_t)*image_objp,
514 load_options_path,
515 &load_options);
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900516}
517
Simon Glassd9717ea2018-11-25 20:14:37 -0700518/**
519 * bootefi_test_prepare() - prepare to run an EFI test
520 *
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100521 * Prepare to run a test as if it were provided by a loaded image.
Simon Glassd9717ea2018-11-25 20:14:37 -0700522 *
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100523 * @image_objp: pointer to be set to the loaded image handle
524 * @loaded_image_infop: pointer to be set to the loaded image protocol
525 * @path: dummy file path used to construct the device path
526 * set in the loaded image protocol
527 * @load_options_path: name of a U-Boot environment variable. Its value is
528 * set as load options in the loaded image protocol.
529 * Return: status code
Simon Glassd9717ea2018-11-25 20:14:37 -0700530 */
531static efi_status_t bootefi_test_prepare
532 (struct efi_loaded_image_obj **image_objp,
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100533 struct efi_loaded_image **loaded_image_infop, const char *path,
534 const char *load_options_path)
Simon Glassd9717ea2018-11-25 20:14:37 -0700535{
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100536 efi_status_t ret;
537
Simon Glassd9717ea2018-11-25 20:14:37 -0700538 /* Construct a dummy device path */
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100539 bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, 0, 0);
Simon Glassd9717ea2018-11-25 20:14:37 -0700540 if (!bootefi_device_path)
541 return EFI_OUT_OF_RESOURCES;
Simon Glassd9717ea2018-11-25 20:14:37 -0700542
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100543 bootefi_image_path = efi_dp_from_file(NULL, 0, path);
544 if (!bootefi_image_path) {
545 ret = EFI_OUT_OF_RESOURCES;
546 goto failure;
547 }
548
549 ret = bootefi_run_prepare(load_options_path, bootefi_device_path,
550 bootefi_image_path, image_objp,
551 loaded_image_infop);
552 if (ret == EFI_SUCCESS)
553 return ret;
554
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100555failure:
Heinrich Schuchardt5f595182021-01-12 12:46:24 +0100556 efi_clear_bootdev();
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100557 return ret;
Simon Glassd9717ea2018-11-25 20:14:37 -0700558}
559
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900560/**
561 * bootefi_run_finish() - finish up after running an EFI test
562 *
563 * @loaded_image_info: Pointer to a struct which holds the loaded image info
564 * @image_obj: Pointer to a struct which holds the loaded image object
565 */
566static void bootefi_run_finish(struct efi_loaded_image_obj *image_obj,
567 struct efi_loaded_image *loaded_image_info)
568{
569 efi_restore_gd();
570 free(loaded_image_info->load_options);
571 efi_delete_handle(&image_obj->header);
572}
573
574/**
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200575 * do_efi_selftest() - execute EFI selftest
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900576 *
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900577 * Return: status code
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900578 */
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200579static int do_efi_selftest(void)
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900580{
581 struct efi_loaded_image_obj *image_obj;
582 struct efi_loaded_image *loaded_image_info;
583 efi_status_t ret;
584
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900585 ret = bootefi_test_prepare(&image_obj, &loaded_image_info,
586 "\\selftest", "efi_selftest");
587 if (ret != EFI_SUCCESS)
588 return CMD_RET_FAILURE;
589
590 /* Execute the test */
591 ret = EFI_CALL(efi_selftest(&image_obj->header, &systab));
592 bootefi_run_finish(image_obj, loaded_image_info);
593
594 return ret != EFI_SUCCESS;
595}
Simon Glassd9717ea2018-11-25 20:14:37 -0700596#endif /* CONFIG_CMD_BOOTEFI_SELFTEST */
597
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200598/**
599 * do_bootefi() - execute `bootefi` command
600 *
601 * @cmdtp: table entry describing command
602 * @flag: bitmap indicating how the command was invoked
603 * @argc: number of arguments
604 * @argv: command line arguments
605 * Return: status code
606 */
Simon Glass09140112020-05-10 11:40:03 -0600607static int do_bootefi(struct cmd_tbl *cmdtp, int flag, int argc,
608 char *const argv[])
Alexander Grafb9939332016-03-10 00:27:20 +0100609{
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200610 efi_status_t ret;
Heinrich Schuchardtf64f2232019-12-08 01:07:01 +0100611 void *fdt;
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200612
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900613 if (argc < 2)
614 return CMD_RET_USAGE;
AKASHI Takahirod6b21892019-04-19 12:22:33 +0900615
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200616 /* Initialize EFI drivers */
617 ret = efi_init_obj_list();
618 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200619 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
620 ret & ~EFI_ERROR_MASK);
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200621 return CMD_RET_FAILURE;
622 }
623
Heinrich Schuchardtf64f2232019-12-08 01:07:01 +0100624 if (argc > 2) {
625 uintptr_t fdt_addr;
626
Heinrich Schuchardt7a597252019-11-28 06:46:09 +0100627 fdt_addr = simple_strtoul(argv[2], NULL, 16);
Heinrich Schuchardtf64f2232019-12-08 01:07:01 +0100628 fdt = map_sysmem(fdt_addr, 0);
629 } else {
630 fdt = EFI_FDT_USE_INTERNAL;
631 }
632 ret = efi_install_fdt(fdt);
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200633 if (ret == EFI_INVALID_PARAMETER)
634 return CMD_RET_USAGE;
635 else if (ret != EFI_SUCCESS)
636 return CMD_RET_FAILURE;
637
Heinrich Schuchardtff2f5322021-01-15 19:02:50 +0100638 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) {
639 if (!strcmp(argv[1], "bootmgr"))
640 return do_efibootmgr();
641 }
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900642#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
Heinrich Schuchardtff2f5322021-01-15 19:02:50 +0100643 if (!strcmp(argv[1], "selftest"))
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200644 return do_efi_selftest();
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900645#endif
646
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200647 return do_bootefi_image(argv[1]);
Alexander Grafb9939332016-03-10 00:27:20 +0100648}
649
650#ifdef CONFIG_SYS_LONGHELP
651static char bootefi_help_text[] =
Alexander Graf1c398092016-04-14 16:07:53 +0200652 "<image address> [fdt address]\n"
653 " - boot EFI payload stored at address <image address>.\n"
654 " If specified, the device tree located at <fdt address> gets\n"
Simon Glassc7ae3df2016-11-07 08:47:08 -0700655 " exposed as EFI configuration table.\n"
656#ifdef CONFIG_CMD_BOOTEFI_HELLO
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200657 "bootefi hello\n"
658 " - boot a sample Hello World application stored within U-Boot\n"
659#endif
660#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100661 "bootefi selftest [fdt address]\n"
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200662 " - boot an EFI selftest application stored within U-Boot\n"
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200663 " Use environment variable efi_selftest to select a single test.\n"
664 " Use 'setenv efi_selftest list' to enumerate all tests.\n"
Simon Glassc7ae3df2016-11-07 08:47:08 -0700665#endif
Heinrich Schuchardtff2f5322021-01-15 19:02:50 +0100666#ifdef CONFIG_CMD_BOOTEFI_BOOTMGR
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900667 "bootefi bootmgr [fdt address]\n"
Rob Clark9975fe92017-09-13 18:05:38 -0400668 " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
669 "\n"
670 " If specified, the device tree located at <fdt address> gets\n"
Heinrich Schuchardtff2f5322021-01-15 19:02:50 +0100671 " exposed as EFI configuration table.\n"
672#endif
673 ;
Alexander Grafb9939332016-03-10 00:27:20 +0100674#endif
675
676U_BOOT_CMD(
Alexander Graf1c398092016-04-14 16:07:53 +0200677 bootefi, 3, 0, do_bootefi,
Sergey Kubushyn92dfd922016-06-07 11:14:31 -0700678 "Boots an EFI payload from memory",
Alexander Grafb9939332016-03-10 00:27:20 +0100679 bootefi_help_text
680);