blob: c8eb5c32b0733d9913c1e03b51f9dfeb29488666 [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 Schuchardtf6c6df72019-01-08 18:13:06 +010011#include <charset.h>
Alexander Grafb9939332016-03-10 00:27:20 +010012#include <command.h>
Simon Glass9d922452017-05-17 17:18:03 -060013#include <dm.h>
Alexander Grafb9939332016-03-10 00:27:20 +010014#include <efi_loader.h>
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +020015#include <efi_selftest.h>
Simon Glass7b51b572019-08-01 09:46:52 -060016#include <env.h>
Alexander Grafb9939332016-03-10 00:27:20 +010017#include <errno.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060018#include <image.h>
Heinrich Schuchardtc0018372020-07-17 20:21:00 +020019#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070020#include <malloc.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090021#include <linux/libfdt.h>
22#include <linux/libfdt_env.h>
Alexander Graf354264b2018-06-18 17:22:58 +020023#include <mapmem.h>
Alexander Grafad0c1a32016-04-11 23:51:01 +020024#include <memalign.h>
Simon Glasse2754582016-09-25 15:27:32 -060025#include <asm-generic/sections.h>
26#include <linux/linkage.h>
Alexander Graf0d9d5012016-04-11 16:55:26 +020027
28DECLARE_GLOBAL_DATA_PTR;
Alexander Grafb9939332016-03-10 00:27:20 +010029
Rob Clark95c55532017-09-13 18:05:33 -040030static struct efi_device_path *bootefi_image_path;
31static struct efi_device_path *bootefi_device_path;
Heinrich Schuchardt5f595182021-01-12 12:46:24 +010032static void *image_addr;
33static size_t image_size;
34
35/**
36 * efi_clear_bootdev() - clear boot device
37 */
38static void efi_clear_bootdev(void)
39{
40 efi_free_pool(bootefi_device_path);
41 efi_free_pool(bootefi_image_path);
42 bootefi_device_path = NULL;
43 bootefi_image_path = NULL;
44 image_addr = NULL;
45 image_size = 0;
46}
47
48/**
49 * efi_set_bootdev() - set boot device
50 *
51 * This function is called when a file is loaded, e.g. via the 'load' command.
52 * We use the path to this file to inform the UEFI binary about the boot device.
53 *
54 * @dev: device, e.g. "MMC"
55 * @devnr: number of the device, e.g. "1:2"
56 * @path: path to file loaded
57 * @buffer: buffer with file loaded
58 * @buffer_size: size of file loaded
59 */
60void efi_set_bootdev(const char *dev, const char *devnr, const char *path,
61 void *buffer, size_t buffer_size)
62{
63 struct efi_device_path *device, *image;
64 efi_status_t ret;
65
66 /* Forget overwritten image */
67 if (buffer + buffer_size >= image_addr &&
68 image_addr + image_size >= buffer)
69 efi_clear_bootdev();
70
71 /* Remember only PE-COFF and FIT images */
72 if (efi_check_pe(buffer, buffer_size, NULL) != EFI_SUCCESS) {
73#ifdef CONFIG_FIT
74 if (!fit_check_format(buffer))
75 return;
76 /*
77 * FIT images of type EFI_OS are started via command bootm.
78 * We should not use their boot device with the bootefi command.
79 */
80 buffer = 0;
81 buffer_size = 0;
82#else
83 return;
84#endif
85 }
86
87 /* efi_set_bootdev() is typically called repeatedly, recover memory */
88 efi_clear_bootdev();
89
90 image_addr = buffer;
91 image_size = buffer_size;
92
93 ret = efi_dp_from_name(dev, devnr, path, &device, &image);
94 if (ret == EFI_SUCCESS) {
95 bootefi_device_path = device;
96 if (image) {
97 /* FIXME: image should not contain device */
98 struct efi_device_path *image_tmp = image;
99
100 efi_dp_split_file_path(image, &device, &image);
101 efi_free_pool(image_tmp);
102 }
103 bootefi_image_path = image;
104 } else {
105 efi_clear_bootdev();
106 }
107}
Alexander Grafb9939332016-03-10 00:27:20 +0100108
Heinrich Schuchardt810371a2019-07-14 13:00:44 +0200109/**
Heinrich Schuchardt1064d042020-08-07 17:47:13 +0200110 * efi_env_set_load_options() - set load options from environment variable
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200111 *
Heinrich Schuchardta3850e42020-01-03 22:53:42 +0100112 * @handle: the image handle
113 * @env_var: name of the environment variable
114 * @load_options: pointer to load options (output)
115 * Return: status code
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200116 */
Heinrich Schuchardt1064d042020-08-07 17:47:13 +0200117static efi_status_t efi_env_set_load_options(efi_handle_t handle,
118 const char *env_var,
119 u16 **load_options)
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200120{
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200121 const char *env = env_get(env_var);
Heinrich Schuchardt1064d042020-08-07 17:47:13 +0200122 size_t size;
Heinrich Schuchardt7086a712018-08-31 21:31:33 +0200123 u16 *pos;
AKASHI Takahirodefa7b82019-04-19 12:22:28 +0900124 efi_status_t ret;
125
Heinrich Schuchardta3850e42020-01-03 22:53:42 +0100126 *load_options = NULL;
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200127 if (!env)
Heinrich Schuchardt1064d042020-08-07 17:47:13 +0200128 return EFI_SUCCESS;
129 size = sizeof(u16) * (utf8_utf16_strlen(env) + 1);
130 pos = calloc(size, 1);
131 if (!pos)
AKASHI Takahirodefa7b82019-04-19 12:22:28 +0900132 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardta3850e42020-01-03 22:53:42 +0100133 *load_options = pos;
Heinrich Schuchardt7086a712018-08-31 21:31:33 +0200134 utf8_utf16_strcpy(&pos, env);
Heinrich Schuchardt1064d042020-08-07 17:47:13 +0200135 ret = efi_set_load_options(handle, size, *load_options);
136 if (ret != EFI_SUCCESS) {
137 free(*load_options);
138 *load_options = NULL;
139 }
140 return ret;
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200141}
142
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200143#if !CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE)
144
Simon Glass9dff4902018-08-08 03:54:30 -0600145/**
146 * copy_fdt() - Copy the device tree to a new location available to EFI
147 *
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +0100148 * The FDT is copied to a suitable location within the EFI memory map.
Heinrich Schuchardtc3772ca2018-11-18 17:58:49 +0100149 * Additional 12 KiB are added to the space in case the device tree needs to be
Simon Glass9dff4902018-08-08 03:54:30 -0600150 * expanded later with fdt_open_into().
151 *
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +0100152 * @fdtp: On entry a pointer to the flattened device tree.
153 * On exit a pointer to the copy of the flattened device tree.
Heinrich Schuchardt4574d1b2018-11-12 18:55:22 +0100154 * FDT start
155 * Return: status code
Simon Glass9dff4902018-08-08 03:54:30 -0600156 */
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +0100157static efi_status_t copy_fdt(void **fdtp)
Alexander Graf0d9d5012016-04-11 16:55:26 +0200158{
Alexander Grafad0c1a32016-04-11 23:51:01 +0200159 unsigned long fdt_ram_start = -1L, fdt_pages;
Simon Glass9dff4902018-08-08 03:54:30 -0600160 efi_status_t ret = 0;
161 void *fdt, *new_fdt;
Alexander Grafad0c1a32016-04-11 23:51:01 +0200162 u64 new_fdt_addr;
Simon Glass9dff4902018-08-08 03:54:30 -0600163 uint fdt_size;
Alexander Grafad0c1a32016-04-11 23:51:01 +0200164 int i;
Alexander Graf0d9d5012016-04-11 16:55:26 +0200165
Simon Glass9dff4902018-08-08 03:54:30 -0600166 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
167 u64 ram_start = gd->bd->bi_dram[i].start;
168 u64 ram_size = gd->bd->bi_dram[i].size;
Alexander Graf0d9d5012016-04-11 16:55:26 +0200169
Alexander Grafad0c1a32016-04-11 23:51:01 +0200170 if (!ram_size)
171 continue;
172
173 if (ram_start < fdt_ram_start)
174 fdt_ram_start = ram_start;
175 }
176
Simon Glassbc9a6382018-06-18 08:08:25 -0600177 /*
Heinrich Schuchardtc3772ca2018-11-18 17:58:49 +0100178 * Give us at least 12 KiB of breathing room in case the device tree
179 * needs to be expanded later.
Simon Glassbc9a6382018-06-18 08:08:25 -0600180 */
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +0100181 fdt = *fdtp;
Heinrich Schuchardtc3772ca2018-11-18 17:58:49 +0100182 fdt_pages = efi_size_in_pages(fdt_totalsize(fdt) + 0x3000);
183 fdt_size = fdt_pages << EFI_PAGE_SHIFT;
Alexander Grafad0c1a32016-04-11 23:51:01 +0200184
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +0100185 /*
186 * Safe fdt location is at 127 MiB.
187 * On the sandbox convert from the sandbox address space.
188 */
189 new_fdt_addr = (uintptr_t)map_sysmem(fdt_ram_start + 0x7f00000 +
190 fdt_size, 0);
Simon Glass9dff4902018-08-08 03:54:30 -0600191 ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
Heinrich Schuchardt42a426e2020-05-06 20:32:31 +0200192 EFI_ACPI_RECLAIM_MEMORY, fdt_pages,
Simon Glass9dff4902018-08-08 03:54:30 -0600193 &new_fdt_addr);
194 if (ret != EFI_SUCCESS) {
Alexander Grafad0c1a32016-04-11 23:51:01 +0200195 /* If we can't put it there, put it somewhere */
xypron.glpk@gmx.dea44bffc2017-08-11 21:19:25 +0200196 new_fdt_addr = (ulong)memalign(EFI_PAGE_SIZE, fdt_size);
Simon Glass9dff4902018-08-08 03:54:30 -0600197 ret = efi_allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
Heinrich Schuchardt42a426e2020-05-06 20:32:31 +0200198 EFI_ACPI_RECLAIM_MEMORY, fdt_pages,
Simon Glass9dff4902018-08-08 03:54:30 -0600199 &new_fdt_addr);
200 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200201 log_err("ERROR: Failed to reserve space for FDT\n");
Simon Glass9dff4902018-08-08 03:54:30 -0600202 goto done;
Alexander Graf85a6e9b2017-07-03 13:32:35 +0200203 }
Alexander Grafad0c1a32016-04-11 23:51:01 +0200204 }
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +0100205 new_fdt = (void *)(uintptr_t)new_fdt_addr;
Alexander Graf0d9d5012016-04-11 16:55:26 +0200206 memcpy(new_fdt, fdt, fdt_totalsize(fdt));
207 fdt_set_totalsize(new_fdt, fdt_size);
208
Heinrich Schuchardt16b615d2018-11-18 17:58:52 +0100209 *fdtp = (void *)(uintptr_t)new_fdt_addr;
Simon Glass9dff4902018-08-08 03:54:30 -0600210done:
211 return ret;
Alexander Graf0d9d5012016-04-11 16:55:26 +0200212}
213
Heinrich Schuchardt4cbb2932020-08-27 12:52:20 +0200214/**
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200215 * get_config_table() - get configuration table
216 *
217 * @guid: GUID of the configuration table
218 * Return: pointer to configuration table or NULL
219 */
220static void *get_config_table(const efi_guid_t *guid)
221{
222 size_t i;
223
224 for (i = 0; i < systab.nr_tables; i++) {
225 if (!guidcmp(guid, &systab.tables[i].guid))
226 return systab.tables[i].table;
227 }
228 return NULL;
229}
230
231#endif /* !CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE) */
232
233/**
Heinrich Schuchardt7a597252019-11-28 06:46:09 +0100234 * efi_install_fdt() - install device tree
Heinrich Schuchardte2d82f82019-05-12 20:16:25 +0200235 *
Heinrich Schuchardt7d4d5512020-02-07 22:10:49 +0100236 * If fdt is not EFI_FDT_USE_INTERNAL, the device tree located at that memory
237 * address will will be installed as configuration table, otherwise the device
238 * tree located at the address indicated by environment variable fdt_addr or as
239 * fallback fdtcontroladdr will be used.
Heinrich Schuchardte2d82f82019-05-12 20:16:25 +0200240 *
Heinrich Schuchardt7a597252019-11-28 06:46:09 +0100241 * On architectures using ACPI tables device trees shall not be installed as
242 * configuration table.
Heinrich Schuchardte2d82f82019-05-12 20:16:25 +0200243 *
Heinrich Schuchardt7d4d5512020-02-07 22:10:49 +0100244 * @fdt: address of device tree or EFI_FDT_USE_INTERNAL to use the
Heinrich Schuchardt753aa182019-12-04 12:31:12 +0100245 * the hardware device tree as indicated by environment variable
246 * fdt_addr or as fallback the internal device tree as indicated by
247 * the environment variable fdtcontroladdr
AKASHI Takahiroe878e6a2019-04-19 12:22:29 +0900248 * Return: status code
AKASHI Takahiroe878e6a2019-04-19 12:22:29 +0900249 */
Heinrich Schuchardtf64f2232019-12-08 01:07:01 +0100250efi_status_t efi_install_fdt(void *fdt)
AKASHI Takahiroe878e6a2019-04-19 12:22:29 +0900251{
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200252 /*
253 * The EBBR spec requires that we have either an FDT or an ACPI table
254 * but not both.
255 */
256#if CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE)
Heinrich Schuchardtf64f2232019-12-08 01:07:01 +0100257 if (fdt) {
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200258 log_err("ERROR: can't have ACPI table and device tree.\n");
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200259 return EFI_LOAD_ERROR;
260 }
261#else
AKASHI Takahiro3ffc52f2019-04-19 12:22:30 +0900262 bootm_headers_t img = { 0 };
AKASHI Takahiroe878e6a2019-04-19 12:22:29 +0900263 efi_status_t ret;
264
Heinrich Schuchardtf64f2232019-12-08 01:07:01 +0100265 if (fdt == EFI_FDT_USE_INTERNAL) {
Heinrich Schuchardt7a597252019-11-28 06:46:09 +0100266 const char *fdt_opt;
Heinrich Schuchardtf64f2232019-12-08 01:07:01 +0100267 uintptr_t fdt_addr;
Heinrich Schuchardt7a597252019-11-28 06:46:09 +0100268
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200269 /* Look for device tree that is already installed */
270 if (get_config_table(&efi_guid_fdt))
271 return EFI_SUCCESS;
Heinrich Schuchardt753aa182019-12-04 12:31:12 +0100272 /* Check if there is a hardware device tree */
273 fdt_opt = env_get("fdt_addr");
274 /* Use our own device tree as fallback */
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200275 if (!fdt_opt) {
Heinrich Schuchardt753aa182019-12-04 12:31:12 +0100276 fdt_opt = env_get("fdtcontroladdr");
277 if (!fdt_opt) {
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200278 log_err("ERROR: need device tree\n");
Heinrich Schuchardt753aa182019-12-04 12:31:12 +0100279 return EFI_NOT_FOUND;
280 }
AKASHI Takahiro3ffc52f2019-04-19 12:22:30 +0900281 }
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200282 fdt_addr = simple_strtoul(fdt_opt, NULL, 16);
283 if (!fdt_addr) {
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200284 log_err("ERROR: invalid $fdt_addr or $fdtcontroladdr\n");
AKASHI Takahiro3ffc52f2019-04-19 12:22:30 +0900285 return EFI_LOAD_ERROR;
286 }
Heinrich Schuchardtf64f2232019-12-08 01:07:01 +0100287 fdt = map_sysmem(fdt_addr, 0);
AKASHI Takahiroe878e6a2019-04-19 12:22:29 +0900288 }
289
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200290 /* Install device tree */
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200291 if (fdt_check_header(fdt)) {
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200292 log_err("ERROR: invalid device tree\n");
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200293 return EFI_LOAD_ERROR;
294 }
295
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200296 /* Prepare device tree for payload */
297 ret = copy_fdt(&fdt);
298 if (ret) {
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200299 log_err("ERROR: out of memory\n");
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200300 return EFI_OUT_OF_RESOURCES;
301 }
302
303 if (image_setup_libfdt(&img, fdt, 0, NULL)) {
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200304 log_err("ERROR: failed to process device tree\n");
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200305 return EFI_LOAD_ERROR;
306 }
307
Heinrich Schuchardtfef907b2020-03-14 10:59:34 +0100308 /* Create memory reservations as indicated by the device tree */
309 efi_carve_out_dt_rsv(fdt);
310
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200311 /* Install device tree as UEFI table */
312 ret = efi_install_configuration_table(&efi_guid_fdt, fdt);
313 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200314 log_err("ERROR: failed to install device tree\n");
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200315 return ret;
316 }
317#endif /* GENERATE_ACPI_TABLE */
318
AKASHI Takahiroe878e6a2019-04-19 12:22:29 +0900319 return EFI_SUCCESS;
320}
321
Simon Glass5e2f0392018-11-25 20:14:39 -0700322/**
Heinrich Schuchardtc9828742018-09-23 17:21:51 +0200323 * do_bootefi_exec() - execute EFI binary
324 *
Heinrich Schuchardt72e1fca2020-08-15 23:10:22 +0200325 * The image indicated by @handle is started. When it returns the allocated
326 * memory for the @load_options is freed.
327 *
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900328 * @handle: handle of loaded image
Heinrich Schuchardt72e1fca2020-08-15 23:10:22 +0200329 * @load_options: load options
Heinrich Schuchardtc9828742018-09-23 17:21:51 +0200330 * Return: status code
331 *
332 * Load the EFI binary into a newly assigned memory unwinding the relocation
333 * information, install the loaded image protocol, and call the binary.
Alexander Grafb9939332016-03-10 00:27:20 +0100334 */
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200335static efi_status_t do_bootefi_exec(efi_handle_t handle, void *load_options)
Alexander Grafb9939332016-03-10 00:27:20 +0100336{
Heinrich Schuchardt45204b12018-03-03 15:29:01 +0100337 efi_status_t ret;
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +0200338 efi_uintn_t exit_data_size = 0;
339 u16 *exit_data = NULL;
Rob Clarkbf192732017-10-10 08:23:06 -0400340
Alexander Grafb9939332016-03-10 00:27:20 +0100341 /* Call our payload! */
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +0200342 ret = EFI_CALL(efi_start_image(handle, &exit_data_size, &exit_data));
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200343 if (ret != EFI_SUCCESS) {
344 log_err("## Application failed, r = %lu\n",
345 ret & ~EFI_ERROR_MASK);
346 if (exit_data) {
347 log_err("## %ls\n", exit_data);
348 efi_free_pool(exit_data);
349 }
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +0200350 }
Rob Clark95c55532017-09-13 18:05:33 -0400351
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900352 efi_restore_gd();
Simon Glass5e2f0392018-11-25 20:14:39 -0700353
Heinrich Schuchardta3850e42020-01-03 22:53:42 +0100354 free(load_options);
Rob Clark95c55532017-09-13 18:05:33 -0400355
356 return ret;
Alexander Grafb9939332016-03-10 00:27:20 +0100357}
358
AKASHI Takahirod6b21892019-04-19 12:22:33 +0900359/**
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200360 * do_efibootmgr() - execute EFI boot manager
AKASHI Takahirod6b21892019-04-19 12:22:33 +0900361 *
AKASHI Takahirod6b21892019-04-19 12:22:33 +0900362 * Return: status code
AKASHI Takahirod6b21892019-04-19 12:22:33 +0900363 */
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200364static int do_efibootmgr(void)
AKASHI Takahirocc999d52019-04-19 12:22:32 +0900365{
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900366 efi_handle_t handle;
AKASHI Takahirod6b21892019-04-19 12:22:33 +0900367 efi_status_t ret;
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200368 void *load_options;
AKASHI Takahirod6b21892019-04-19 12:22:33 +0900369
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200370 ret = efi_bootmgr_load(&handle, &load_options);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900371 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200372 log_notice("EFI boot manager: Cannot load any image\n");
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900373 return CMD_RET_FAILURE;
374 }
AKASHI Takahirocc999d52019-04-19 12:22:32 +0900375
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200376 ret = do_bootefi_exec(handle, load_options);
AKASHI Takahirocc999d52019-04-19 12:22:32 +0900377
AKASHI Takahirod6b21892019-04-19 12:22:33 +0900378 if (ret != EFI_SUCCESS)
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900379 return CMD_RET_FAILURE;
AKASHI Takahirocc999d52019-04-19 12:22:32 +0900380
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900381 return CMD_RET_SUCCESS;
AKASHI Takahirocc999d52019-04-19 12:22:32 +0900382}
383
Heinrich Schuchardt810371a2019-07-14 13:00:44 +0200384/**
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200385 * do_bootefi_image() - execute EFI binary
386 *
387 * Set up memory image for the binary to be loaded, prepare device path, and
388 * then call do_bootefi_exec() to execute it.
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900389 *
390 * @image_opt: string of image start address
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900391 * Return: status code
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900392 */
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200393static int do_bootefi_image(const char *image_opt)
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900394{
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900395 void *image_buf;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900396 unsigned long addr, size;
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900397 efi_status_t ret;
398
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900399#ifdef CONFIG_CMD_BOOTEFI_HELLO
400 if (!strcmp(image_opt, "hello")) {
Heinrich Schuchardtbb33c792021-01-12 17:44:08 +0100401 image_buf = __efi_helloworld_begin;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900402 size = __efi_helloworld_end - __efi_helloworld_begin;
Heinrich Schuchardt5f595182021-01-12 12:46:24 +0100403 efi_clear_bootdev();
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900404 } else
405#endif
406 {
Heinrich Schuchardt5f595182021-01-12 12:46:24 +0100407 addr = strtoul(image_opt, NULL, 16);
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900408 /* Check that a numeric value was passed */
Heinrich Schuchardt5f595182021-01-12 12:46:24 +0100409 if (!addr)
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900410 return CMD_RET_USAGE;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900411
Heinrich Schuchardt5f595182021-01-12 12:46:24 +0100412 image_buf = map_sysmem(addr, 0);
413
414 if (image_buf != image_addr) {
415 log_err("No UEFI binary known at %s\n", image_opt);
416 return CMD_RET_FAILURE;
417 }
418 size = image_size;
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900419 }
Heinrich Schuchardtf9ceb6a2019-12-07 20:51:06 +0100420 ret = efi_run_image(image_buf, size);
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900421
Heinrich Schuchardtf9ceb6a2019-12-07 20:51:06 +0100422 if (ret != EFI_SUCCESS)
423 return CMD_RET_FAILURE;
424
425 return CMD_RET_SUCCESS;
426}
427
428/**
429 * efi_run_image() - run loaded UEFI image
430 *
431 * @source_buffer: memory address of the UEFI image
432 * @source_size: size of the UEFI image
433 * Return: status code
434 */
435efi_status_t efi_run_image(void *source_buffer, efi_uintn_t source_size)
436{
437 efi_handle_t mem_handle = NULL, handle;
438 struct efi_device_path *file_path = NULL;
Heinrich Schuchardtc2f01032020-08-25 17:54:05 +0000439 struct efi_device_path *msg_path;
Heinrich Schuchardtf9ceb6a2019-12-07 20:51:06 +0100440 efi_status_t ret;
Heinrich Schuchardtc2f01032020-08-25 17:54:05 +0000441 u16 *load_options;
Heinrich Schuchardtf9ceb6a2019-12-07 20:51:06 +0100442
443 if (!bootefi_device_path || !bootefi_image_path) {
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900444 /*
445 * Special case for efi payload not loaded from disk,
446 * such as 'bootefi hello' or for example payload
447 * loaded directly into memory via JTAG, etc:
448 */
449 file_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
Heinrich Schuchardtf9ceb6a2019-12-07 20:51:06 +0100450 (uintptr_t)source_buffer,
451 source_size);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900452 /*
453 * Make sure that device for device_path exist
454 * in load_image(). Otherwise, shell and grub will fail.
455 */
456 ret = efi_create_handle(&mem_handle);
457 if (ret != EFI_SUCCESS)
458 goto out;
459
460 ret = efi_add_protocol(mem_handle, &efi_guid_device_path,
461 file_path);
462 if (ret != EFI_SUCCESS)
463 goto out;
Heinrich Schuchardtc2f01032020-08-25 17:54:05 +0000464 msg_path = file_path;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900465 } else {
Heinrich Schuchardtf9ceb6a2019-12-07 20:51:06 +0100466 file_path = efi_dp_append(bootefi_device_path,
467 bootefi_image_path);
Heinrich Schuchardtc2f01032020-08-25 17:54:05 +0000468 msg_path = bootefi_image_path;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900469 }
470
Heinrich Schuchardtc2f01032020-08-25 17:54:05 +0000471 log_info("Booting %pD\n", msg_path);
472
Heinrich Schuchardtf9ceb6a2019-12-07 20:51:06 +0100473 ret = EFI_CALL(efi_load_image(false, efi_root, file_path, source_buffer,
474 source_size, &handle));
Heinrich Schuchardtc2f01032020-08-25 17:54:05 +0000475 if (ret != EFI_SUCCESS) {
476 log_err("Loading image failed\n");
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900477 goto out;
Heinrich Schuchardtc2f01032020-08-25 17:54:05 +0000478 }
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200479
480 /* Transfer environment variable as load options */
481 ret = efi_env_set_load_options(handle, "bootargs", &load_options);
482 if (ret != EFI_SUCCESS)
483 goto out;
484
485 ret = do_bootefi_exec(handle, load_options);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900486
487out:
Heinrich Schuchardt4fe050e2020-04-20 12:44:56 +0200488 efi_delete_handle(mem_handle);
489 efi_free_pool(file_path);
Heinrich Schuchardtf9ceb6a2019-12-07 20:51:06 +0100490 return ret;
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900491}
492
Simon Glassd9717ea2018-11-25 20:14:37 -0700493#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900494static efi_status_t bootefi_run_prepare(const char *load_options_path,
495 struct efi_device_path *device_path,
496 struct efi_device_path *image_path,
497 struct efi_loaded_image_obj **image_objp,
498 struct efi_loaded_image **loaded_image_infop)
499{
500 efi_status_t ret;
Heinrich Schuchardta3850e42020-01-03 22:53:42 +0100501 u16 *load_options;
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900502
503 ret = efi_setup_loaded_image(device_path, image_path, image_objp,
504 loaded_image_infop);
505 if (ret != EFI_SUCCESS)
506 return ret;
507
508 /* Transfer environment variable as load options */
Heinrich Schuchardt1064d042020-08-07 17:47:13 +0200509 return efi_env_set_load_options((efi_handle_t)*image_objp,
510 load_options_path,
511 &load_options);
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900512}
513
Simon Glassd9717ea2018-11-25 20:14:37 -0700514/**
515 * bootefi_test_prepare() - prepare to run an EFI test
516 *
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100517 * Prepare to run a test as if it were provided by a loaded image.
Simon Glassd9717ea2018-11-25 20:14:37 -0700518 *
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100519 * @image_objp: pointer to be set to the loaded image handle
520 * @loaded_image_infop: pointer to be set to the loaded image protocol
521 * @path: dummy file path used to construct the device path
522 * set in the loaded image protocol
523 * @load_options_path: name of a U-Boot environment variable. Its value is
524 * set as load options in the loaded image protocol.
525 * Return: status code
Simon Glassd9717ea2018-11-25 20:14:37 -0700526 */
527static efi_status_t bootefi_test_prepare
528 (struct efi_loaded_image_obj **image_objp,
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100529 struct efi_loaded_image **loaded_image_infop, const char *path,
530 const char *load_options_path)
Simon Glassd9717ea2018-11-25 20:14:37 -0700531{
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100532 efi_status_t ret;
533
Simon Glassd9717ea2018-11-25 20:14:37 -0700534 /* Construct a dummy device path */
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100535 bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, 0, 0);
Simon Glassd9717ea2018-11-25 20:14:37 -0700536 if (!bootefi_device_path)
537 return EFI_OUT_OF_RESOURCES;
Simon Glassd9717ea2018-11-25 20:14:37 -0700538
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100539 bootefi_image_path = efi_dp_from_file(NULL, 0, path);
540 if (!bootefi_image_path) {
541 ret = EFI_OUT_OF_RESOURCES;
542 goto failure;
543 }
544
545 ret = bootefi_run_prepare(load_options_path, bootefi_device_path,
546 bootefi_image_path, image_objp,
547 loaded_image_infop);
548 if (ret == EFI_SUCCESS)
549 return ret;
550
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100551failure:
Heinrich Schuchardt5f595182021-01-12 12:46:24 +0100552 efi_clear_bootdev();
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100553 return ret;
Simon Glassd9717ea2018-11-25 20:14:37 -0700554}
555
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900556/**
557 * bootefi_run_finish() - finish up after running an EFI test
558 *
559 * @loaded_image_info: Pointer to a struct which holds the loaded image info
560 * @image_obj: Pointer to a struct which holds the loaded image object
561 */
562static void bootefi_run_finish(struct efi_loaded_image_obj *image_obj,
563 struct efi_loaded_image *loaded_image_info)
564{
565 efi_restore_gd();
566 free(loaded_image_info->load_options);
567 efi_delete_handle(&image_obj->header);
568}
569
570/**
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200571 * do_efi_selftest() - execute EFI selftest
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900572 *
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900573 * Return: status code
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900574 */
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200575static int do_efi_selftest(void)
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900576{
577 struct efi_loaded_image_obj *image_obj;
578 struct efi_loaded_image *loaded_image_info;
579 efi_status_t ret;
580
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900581 ret = bootefi_test_prepare(&image_obj, &loaded_image_info,
582 "\\selftest", "efi_selftest");
583 if (ret != EFI_SUCCESS)
584 return CMD_RET_FAILURE;
585
586 /* Execute the test */
587 ret = EFI_CALL(efi_selftest(&image_obj->header, &systab));
588 bootefi_run_finish(image_obj, loaded_image_info);
589
590 return ret != EFI_SUCCESS;
591}
Simon Glassd9717ea2018-11-25 20:14:37 -0700592#endif /* CONFIG_CMD_BOOTEFI_SELFTEST */
593
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200594/**
595 * do_bootefi() - execute `bootefi` command
596 *
597 * @cmdtp: table entry describing command
598 * @flag: bitmap indicating how the command was invoked
599 * @argc: number of arguments
600 * @argv: command line arguments
601 * Return: status code
602 */
Simon Glass09140112020-05-10 11:40:03 -0600603static int do_bootefi(struct cmd_tbl *cmdtp, int flag, int argc,
604 char *const argv[])
Alexander Grafb9939332016-03-10 00:27:20 +0100605{
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200606 efi_status_t ret;
Heinrich Schuchardtf64f2232019-12-08 01:07:01 +0100607 void *fdt;
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200608
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900609 if (argc < 2)
610 return CMD_RET_USAGE;
AKASHI Takahirod6b21892019-04-19 12:22:33 +0900611
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200612 /* Initialize EFI drivers */
613 ret = efi_init_obj_list();
614 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200615 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
616 ret & ~EFI_ERROR_MASK);
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200617 return CMD_RET_FAILURE;
618 }
619
Heinrich Schuchardtf64f2232019-12-08 01:07:01 +0100620 if (argc > 2) {
621 uintptr_t fdt_addr;
622
Heinrich Schuchardt7a597252019-11-28 06:46:09 +0100623 fdt_addr = simple_strtoul(argv[2], NULL, 16);
Heinrich Schuchardtf64f2232019-12-08 01:07:01 +0100624 fdt = map_sysmem(fdt_addr, 0);
625 } else {
626 fdt = EFI_FDT_USE_INTERNAL;
627 }
628 ret = efi_install_fdt(fdt);
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200629 if (ret == EFI_INVALID_PARAMETER)
630 return CMD_RET_USAGE;
631 else if (ret != EFI_SUCCESS)
632 return CMD_RET_FAILURE;
633
Heinrich Schuchardtff2f5322021-01-15 19:02:50 +0100634 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) {
635 if (!strcmp(argv[1], "bootmgr"))
636 return do_efibootmgr();
637 }
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900638#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
Heinrich Schuchardtff2f5322021-01-15 19:02:50 +0100639 if (!strcmp(argv[1], "selftest"))
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200640 return do_efi_selftest();
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900641#endif
642
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200643 return do_bootefi_image(argv[1]);
Alexander Grafb9939332016-03-10 00:27:20 +0100644}
645
646#ifdef CONFIG_SYS_LONGHELP
647static char bootefi_help_text[] =
Alexander Graf1c398092016-04-14 16:07:53 +0200648 "<image address> [fdt address]\n"
649 " - boot EFI payload stored at address <image address>.\n"
650 " If specified, the device tree located at <fdt address> gets\n"
Simon Glassc7ae3df2016-11-07 08:47:08 -0700651 " exposed as EFI configuration table.\n"
652#ifdef CONFIG_CMD_BOOTEFI_HELLO
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200653 "bootefi hello\n"
654 " - boot a sample Hello World application stored within U-Boot\n"
655#endif
656#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100657 "bootefi selftest [fdt address]\n"
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200658 " - boot an EFI selftest application stored within U-Boot\n"
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200659 " Use environment variable efi_selftest to select a single test.\n"
660 " Use 'setenv efi_selftest list' to enumerate all tests.\n"
Simon Glassc7ae3df2016-11-07 08:47:08 -0700661#endif
Heinrich Schuchardtff2f5322021-01-15 19:02:50 +0100662#ifdef CONFIG_CMD_BOOTEFI_BOOTMGR
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900663 "bootefi bootmgr [fdt address]\n"
Rob Clark9975fe92017-09-13 18:05:38 -0400664 " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
665 "\n"
666 " If specified, the device tree located at <fdt address> gets\n"
Heinrich Schuchardtff2f5322021-01-15 19:02:50 +0100667 " exposed as EFI configuration table.\n"
668#endif
669 ;
Alexander Grafb9939332016-03-10 00:27:20 +0100670#endif
671
672U_BOOT_CMD(
Alexander Graf1c398092016-04-14 16:07:53 +0200673 bootefi, 3, 0, do_bootefi,
Sergey Kubushyn92dfd922016-06-07 11:14:31 -0700674 "Boots an EFI payload from memory",
Alexander Grafb9939332016-03-10 00:27:20 +0100675 bootefi_help_text
676);