blob: 8fa4a1c28764b72d780f5612b38a326292cab22e [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/**
215 * efi_reserve_memory() - add reserved memory to memory map
216 *
217 * @addr: start address of the reserved memory range
218 * @size: size of the reserved memory range
219 * @nomap: indicates that the memory range shall not be accessed by the
220 * UEFI payload
221 */
222static void efi_reserve_memory(u64 addr, u64 size, bool nomap)
Atish Patra7be64b82020-03-13 17:11:30 -0700223{
Heinrich Schuchardt4cbb2932020-08-27 12:52:20 +0200224 int type;
225 efi_uintn_t ret;
226
Atish Patra7be64b82020-03-13 17:11:30 -0700227 /* Convert from sandbox address space. */
228 addr = (uintptr_t)map_sysmem(addr, 0);
Heinrich Schuchardt4cbb2932020-08-27 12:52:20 +0200229
230 if (nomap)
231 type = EFI_RESERVED_MEMORY_TYPE;
232 else
233 type = EFI_BOOT_SERVICES_DATA;
234
235 ret = efi_add_memory_map(addr, size, type);
236 if (ret != EFI_SUCCESS)
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200237 log_err("Reserved memory mapping failed addr %llx size %llx\n",
238 addr, size);
Atish Patra7be64b82020-03-13 17:11:30 -0700239}
240
Heinrich Schuchardt810371a2019-07-14 13:00:44 +0200241/**
Simon Glass416e07e2018-06-18 08:08:28 -0600242 * efi_carve_out_dt_rsv() - Carve out DT reserved memory ranges
243 *
244 * The mem_rsv entries of the FDT are added to the memory map. Any failures are
245 * ignored because this is not critical and we would rather continue to try to
246 * boot.
247 *
248 * @fdt: Pointer to device tree
249 */
250static void efi_carve_out_dt_rsv(void *fdt)
Alexander Graf806d2fa2018-04-06 09:40:51 +0200251{
252 int nr_rsv, i;
Atish Patra7be64b82020-03-13 17:11:30 -0700253 u64 addr, size;
254 int nodeoffset, subnode;
Alexander Graf806d2fa2018-04-06 09:40:51 +0200255
256 nr_rsv = fdt_num_mem_rsv(fdt);
257
258 /* Look for an existing entry and add it to the efi mem map. */
259 for (i = 0; i < nr_rsv; i++) {
260 if (fdt_get_mem_rsv(fdt, i, &addr, &size) != 0)
261 continue;
Heinrich Schuchardt4cbb2932020-08-27 12:52:20 +0200262 efi_reserve_memory(addr, size, false);
Atish Patra7be64b82020-03-13 17:11:30 -0700263 }
Alexander Graf806d2fa2018-04-06 09:40:51 +0200264
Atish Patra7be64b82020-03-13 17:11:30 -0700265 /* process reserved-memory */
266 nodeoffset = fdt_subnode_offset(fdt, 0, "reserved-memory");
267 if (nodeoffset >= 0) {
268 subnode = fdt_first_subnode(fdt, nodeoffset);
269 while (subnode >= 0) {
Bin Mengb1c272d2020-06-22 23:50:50 -0700270 fdt_addr_t fdt_addr;
271 fdt_size_t fdt_size;
Atish Patra0d7c2912020-06-18 18:51:50 -0700272
Atish Patra7be64b82020-03-13 17:11:30 -0700273 /* check if this subnode has a reg property */
Atish Patra0d7c2912020-06-18 18:51:50 -0700274 fdt_addr = fdtdec_get_addr_size_auto_parent(
275 fdt, nodeoffset, subnode,
276 "reg", 0, &fdt_size, false);
Atish Patra7be64b82020-03-13 17:11:30 -0700277 /*
278 * The /reserved-memory node may have children with
279 * a size instead of a reg property.
280 */
Heinrich Schuchardt039d4f52020-06-30 14:12:43 +0200281 if (fdt_addr != FDT_ADDR_T_NONE &&
Heinrich Schuchardt4cbb2932020-08-27 12:52:20 +0200282 fdtdec_get_is_enabled(fdt, subnode)) {
283 bool nomap;
284
285 nomap = !!fdt_getprop(fdt, subnode, "no-map",
286 NULL);
287 efi_reserve_memory(fdt_addr, fdt_size, nomap);
288 }
Atish Patra7be64b82020-03-13 17:11:30 -0700289 subnode = fdt_next_subnode(fdt, subnode);
290 }
Alexander Graf806d2fa2018-04-06 09:40:51 +0200291 }
Alexander Graf806d2fa2018-04-06 09:40:51 +0200292}
293
AKASHI Takahiroe878e6a2019-04-19 12:22:29 +0900294/**
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200295 * get_config_table() - get configuration table
296 *
297 * @guid: GUID of the configuration table
298 * Return: pointer to configuration table or NULL
299 */
300static void *get_config_table(const efi_guid_t *guid)
301{
302 size_t i;
303
304 for (i = 0; i < systab.nr_tables; i++) {
305 if (!guidcmp(guid, &systab.tables[i].guid))
306 return systab.tables[i].table;
307 }
308 return NULL;
309}
310
311#endif /* !CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE) */
312
313/**
Heinrich Schuchardt7a597252019-11-28 06:46:09 +0100314 * efi_install_fdt() - install device tree
Heinrich Schuchardte2d82f82019-05-12 20:16:25 +0200315 *
Heinrich Schuchardt7d4d5512020-02-07 22:10:49 +0100316 * If fdt is not EFI_FDT_USE_INTERNAL, the device tree located at that memory
317 * address will will be installed as configuration table, otherwise the device
318 * tree located at the address indicated by environment variable fdt_addr or as
319 * fallback fdtcontroladdr will be used.
Heinrich Schuchardte2d82f82019-05-12 20:16:25 +0200320 *
Heinrich Schuchardt7a597252019-11-28 06:46:09 +0100321 * On architectures using ACPI tables device trees shall not be installed as
322 * configuration table.
Heinrich Schuchardte2d82f82019-05-12 20:16:25 +0200323 *
Heinrich Schuchardt7d4d5512020-02-07 22:10:49 +0100324 * @fdt: address of device tree or EFI_FDT_USE_INTERNAL to use the
Heinrich Schuchardt753aa182019-12-04 12:31:12 +0100325 * the hardware device tree as indicated by environment variable
326 * fdt_addr or as fallback the internal device tree as indicated by
327 * the environment variable fdtcontroladdr
AKASHI Takahiroe878e6a2019-04-19 12:22:29 +0900328 * Return: status code
AKASHI Takahiroe878e6a2019-04-19 12:22:29 +0900329 */
Heinrich Schuchardtf64f2232019-12-08 01:07:01 +0100330efi_status_t efi_install_fdt(void *fdt)
AKASHI Takahiroe878e6a2019-04-19 12:22:29 +0900331{
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200332 /*
333 * The EBBR spec requires that we have either an FDT or an ACPI table
334 * but not both.
335 */
336#if CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE)
Heinrich Schuchardtf64f2232019-12-08 01:07:01 +0100337 if (fdt) {
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200338 log_err("ERROR: can't have ACPI table and device tree.\n");
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200339 return EFI_LOAD_ERROR;
340 }
341#else
AKASHI Takahiro3ffc52f2019-04-19 12:22:30 +0900342 bootm_headers_t img = { 0 };
AKASHI Takahiroe878e6a2019-04-19 12:22:29 +0900343 efi_status_t ret;
344
Heinrich Schuchardtf64f2232019-12-08 01:07:01 +0100345 if (fdt == EFI_FDT_USE_INTERNAL) {
Heinrich Schuchardt7a597252019-11-28 06:46:09 +0100346 const char *fdt_opt;
Heinrich Schuchardtf64f2232019-12-08 01:07:01 +0100347 uintptr_t fdt_addr;
Heinrich Schuchardt7a597252019-11-28 06:46:09 +0100348
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200349 /* Look for device tree that is already installed */
350 if (get_config_table(&efi_guid_fdt))
351 return EFI_SUCCESS;
Heinrich Schuchardt753aa182019-12-04 12:31:12 +0100352 /* Check if there is a hardware device tree */
353 fdt_opt = env_get("fdt_addr");
354 /* Use our own device tree as fallback */
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200355 if (!fdt_opt) {
Heinrich Schuchardt753aa182019-12-04 12:31:12 +0100356 fdt_opt = env_get("fdtcontroladdr");
357 if (!fdt_opt) {
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200358 log_err("ERROR: need device tree\n");
Heinrich Schuchardt753aa182019-12-04 12:31:12 +0100359 return EFI_NOT_FOUND;
360 }
AKASHI Takahiro3ffc52f2019-04-19 12:22:30 +0900361 }
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200362 fdt_addr = simple_strtoul(fdt_opt, NULL, 16);
363 if (!fdt_addr) {
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200364 log_err("ERROR: invalid $fdt_addr or $fdtcontroladdr\n");
AKASHI Takahiro3ffc52f2019-04-19 12:22:30 +0900365 return EFI_LOAD_ERROR;
366 }
Heinrich Schuchardtf64f2232019-12-08 01:07:01 +0100367 fdt = map_sysmem(fdt_addr, 0);
AKASHI Takahiroe878e6a2019-04-19 12:22:29 +0900368 }
369
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200370 /* Install device tree */
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200371 if (fdt_check_header(fdt)) {
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200372 log_err("ERROR: invalid device tree\n");
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200373 return EFI_LOAD_ERROR;
374 }
375
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200376 /* Prepare device tree for payload */
377 ret = copy_fdt(&fdt);
378 if (ret) {
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200379 log_err("ERROR: out of memory\n");
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200380 return EFI_OUT_OF_RESOURCES;
381 }
382
383 if (image_setup_libfdt(&img, fdt, 0, NULL)) {
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200384 log_err("ERROR: failed to process device tree\n");
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200385 return EFI_LOAD_ERROR;
386 }
387
Heinrich Schuchardtfef907b2020-03-14 10:59:34 +0100388 /* Create memory reservations as indicated by the device tree */
389 efi_carve_out_dt_rsv(fdt);
390
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200391 /* Install device tree as UEFI table */
392 ret = efi_install_configuration_table(&efi_guid_fdt, fdt);
393 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200394 log_err("ERROR: failed to install device tree\n");
Heinrich Schuchardt61824952019-04-20 13:33:55 +0200395 return ret;
396 }
397#endif /* GENERATE_ACPI_TABLE */
398
AKASHI Takahiroe878e6a2019-04-19 12:22:29 +0900399 return EFI_SUCCESS;
400}
401
Simon Glass5e2f0392018-11-25 20:14:39 -0700402/**
Heinrich Schuchardtc9828742018-09-23 17:21:51 +0200403 * do_bootefi_exec() - execute EFI binary
404 *
Heinrich Schuchardt72e1fca2020-08-15 23:10:22 +0200405 * The image indicated by @handle is started. When it returns the allocated
406 * memory for the @load_options is freed.
407 *
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900408 * @handle: handle of loaded image
Heinrich Schuchardt72e1fca2020-08-15 23:10:22 +0200409 * @load_options: load options
Heinrich Schuchardtc9828742018-09-23 17:21:51 +0200410 * Return: status code
411 *
412 * Load the EFI binary into a newly assigned memory unwinding the relocation
413 * information, install the loaded image protocol, and call the binary.
Alexander Grafb9939332016-03-10 00:27:20 +0100414 */
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200415static efi_status_t do_bootefi_exec(efi_handle_t handle, void *load_options)
Alexander Grafb9939332016-03-10 00:27:20 +0100416{
Heinrich Schuchardt45204b12018-03-03 15:29:01 +0100417 efi_status_t ret;
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +0200418 efi_uintn_t exit_data_size = 0;
419 u16 *exit_data = NULL;
Rob Clarkbf192732017-10-10 08:23:06 -0400420
Alexander Grafb9939332016-03-10 00:27:20 +0100421 /* Call our payload! */
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +0200422 ret = EFI_CALL(efi_start_image(handle, &exit_data_size, &exit_data));
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200423 if (ret != EFI_SUCCESS) {
424 log_err("## Application failed, r = %lu\n",
425 ret & ~EFI_ERROR_MASK);
426 if (exit_data) {
427 log_err("## %ls\n", exit_data);
428 efi_free_pool(exit_data);
429 }
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +0200430 }
Rob Clark95c55532017-09-13 18:05:33 -0400431
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900432 efi_restore_gd();
Simon Glass5e2f0392018-11-25 20:14:39 -0700433
Heinrich Schuchardta3850e42020-01-03 22:53:42 +0100434 free(load_options);
Rob Clark95c55532017-09-13 18:05:33 -0400435
436 return ret;
Alexander Grafb9939332016-03-10 00:27:20 +0100437}
438
AKASHI Takahirod6b21892019-04-19 12:22:33 +0900439/**
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200440 * do_efibootmgr() - execute EFI boot manager
AKASHI Takahirod6b21892019-04-19 12:22:33 +0900441 *
AKASHI Takahirod6b21892019-04-19 12:22:33 +0900442 * Return: status code
AKASHI Takahirod6b21892019-04-19 12:22:33 +0900443 */
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200444static int do_efibootmgr(void)
AKASHI Takahirocc999d52019-04-19 12:22:32 +0900445{
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900446 efi_handle_t handle;
AKASHI Takahirod6b21892019-04-19 12:22:33 +0900447 efi_status_t ret;
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200448 void *load_options;
AKASHI Takahirod6b21892019-04-19 12:22:33 +0900449
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200450 ret = efi_bootmgr_load(&handle, &load_options);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900451 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200452 log_notice("EFI boot manager: Cannot load any image\n");
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900453 return CMD_RET_FAILURE;
454 }
AKASHI Takahirocc999d52019-04-19 12:22:32 +0900455
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200456 ret = do_bootefi_exec(handle, load_options);
AKASHI Takahirocc999d52019-04-19 12:22:32 +0900457
AKASHI Takahirod6b21892019-04-19 12:22:33 +0900458 if (ret != EFI_SUCCESS)
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900459 return CMD_RET_FAILURE;
AKASHI Takahirocc999d52019-04-19 12:22:32 +0900460
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900461 return CMD_RET_SUCCESS;
AKASHI Takahirocc999d52019-04-19 12:22:32 +0900462}
463
Heinrich Schuchardt810371a2019-07-14 13:00:44 +0200464/**
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200465 * do_bootefi_image() - execute EFI binary
466 *
467 * Set up memory image for the binary to be loaded, prepare device path, and
468 * then call do_bootefi_exec() to execute it.
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900469 *
470 * @image_opt: string of image start address
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900471 * Return: status code
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900472 */
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200473static int do_bootefi_image(const char *image_opt)
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900474{
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900475 void *image_buf;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900476 unsigned long addr, size;
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900477 efi_status_t ret;
478
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900479#ifdef CONFIG_CMD_BOOTEFI_HELLO
480 if (!strcmp(image_opt, "hello")) {
Heinrich Schuchardtbb33c792021-01-12 17:44:08 +0100481 image_buf = __efi_helloworld_begin;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900482 size = __efi_helloworld_end - __efi_helloworld_begin;
Heinrich Schuchardt5f595182021-01-12 12:46:24 +0100483 efi_clear_bootdev();
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900484 } else
485#endif
486 {
Heinrich Schuchardt5f595182021-01-12 12:46:24 +0100487 addr = strtoul(image_opt, NULL, 16);
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900488 /* Check that a numeric value was passed */
Heinrich Schuchardt5f595182021-01-12 12:46:24 +0100489 if (!addr)
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900490 return CMD_RET_USAGE;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900491
Heinrich Schuchardt5f595182021-01-12 12:46:24 +0100492 image_buf = map_sysmem(addr, 0);
493
494 if (image_buf != image_addr) {
495 log_err("No UEFI binary known at %s\n", image_opt);
496 return CMD_RET_FAILURE;
497 }
498 size = image_size;
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900499 }
Heinrich Schuchardtf9ceb6a2019-12-07 20:51:06 +0100500 ret = efi_run_image(image_buf, size);
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900501
Heinrich Schuchardtf9ceb6a2019-12-07 20:51:06 +0100502 if (ret != EFI_SUCCESS)
503 return CMD_RET_FAILURE;
504
505 return CMD_RET_SUCCESS;
506}
507
508/**
509 * efi_run_image() - run loaded UEFI image
510 *
511 * @source_buffer: memory address of the UEFI image
512 * @source_size: size of the UEFI image
513 * Return: status code
514 */
515efi_status_t efi_run_image(void *source_buffer, efi_uintn_t source_size)
516{
517 efi_handle_t mem_handle = NULL, handle;
518 struct efi_device_path *file_path = NULL;
Heinrich Schuchardtc2f01032020-08-25 17:54:05 +0000519 struct efi_device_path *msg_path;
Heinrich Schuchardtf9ceb6a2019-12-07 20:51:06 +0100520 efi_status_t ret;
Heinrich Schuchardtc2f01032020-08-25 17:54:05 +0000521 u16 *load_options;
Heinrich Schuchardtf9ceb6a2019-12-07 20:51:06 +0100522
523 if (!bootefi_device_path || !bootefi_image_path) {
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900524 /*
525 * Special case for efi payload not loaded from disk,
526 * such as 'bootefi hello' or for example payload
527 * loaded directly into memory via JTAG, etc:
528 */
529 file_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
Heinrich Schuchardtf9ceb6a2019-12-07 20:51:06 +0100530 (uintptr_t)source_buffer,
531 source_size);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900532 /*
533 * Make sure that device for device_path exist
534 * in load_image(). Otherwise, shell and grub will fail.
535 */
536 ret = efi_create_handle(&mem_handle);
537 if (ret != EFI_SUCCESS)
538 goto out;
539
540 ret = efi_add_protocol(mem_handle, &efi_guid_device_path,
541 file_path);
542 if (ret != EFI_SUCCESS)
543 goto out;
Heinrich Schuchardtc2f01032020-08-25 17:54:05 +0000544 msg_path = file_path;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900545 } else {
Heinrich Schuchardtf9ceb6a2019-12-07 20:51:06 +0100546 file_path = efi_dp_append(bootefi_device_path,
547 bootefi_image_path);
Heinrich Schuchardtc2f01032020-08-25 17:54:05 +0000548 msg_path = bootefi_image_path;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900549 }
550
Heinrich Schuchardtc2f01032020-08-25 17:54:05 +0000551 log_info("Booting %pD\n", msg_path);
552
Heinrich Schuchardtf9ceb6a2019-12-07 20:51:06 +0100553 ret = EFI_CALL(efi_load_image(false, efi_root, file_path, source_buffer,
554 source_size, &handle));
Heinrich Schuchardtc2f01032020-08-25 17:54:05 +0000555 if (ret != EFI_SUCCESS) {
556 log_err("Loading image failed\n");
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900557 goto out;
Heinrich Schuchardtc2f01032020-08-25 17:54:05 +0000558 }
Heinrich Schuchardt0ad64002020-08-07 17:49:39 +0200559
560 /* Transfer environment variable as load options */
561 ret = efi_env_set_load_options(handle, "bootargs", &load_options);
562 if (ret != EFI_SUCCESS)
563 goto out;
564
565 ret = do_bootefi_exec(handle, load_options);
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900566
567out:
Heinrich Schuchardt4fe050e2020-04-20 12:44:56 +0200568 efi_delete_handle(mem_handle);
569 efi_free_pool(file_path);
Heinrich Schuchardtf9ceb6a2019-12-07 20:51:06 +0100570 return ret;
AKASHI Takahiroe2e40982019-04-19 12:22:34 +0900571}
572
Simon Glassd9717ea2018-11-25 20:14:37 -0700573#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900574static efi_status_t bootefi_run_prepare(const char *load_options_path,
575 struct efi_device_path *device_path,
576 struct efi_device_path *image_path,
577 struct efi_loaded_image_obj **image_objp,
578 struct efi_loaded_image **loaded_image_infop)
579{
580 efi_status_t ret;
Heinrich Schuchardta3850e42020-01-03 22:53:42 +0100581 u16 *load_options;
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900582
583 ret = efi_setup_loaded_image(device_path, image_path, image_objp,
584 loaded_image_infop);
585 if (ret != EFI_SUCCESS)
586 return ret;
587
588 /* Transfer environment variable as load options */
Heinrich Schuchardt1064d042020-08-07 17:47:13 +0200589 return efi_env_set_load_options((efi_handle_t)*image_objp,
590 load_options_path,
591 &load_options);
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900592}
593
Simon Glassd9717ea2018-11-25 20:14:37 -0700594/**
595 * bootefi_test_prepare() - prepare to run an EFI test
596 *
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100597 * Prepare to run a test as if it were provided by a loaded image.
Simon Glassd9717ea2018-11-25 20:14:37 -0700598 *
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100599 * @image_objp: pointer to be set to the loaded image handle
600 * @loaded_image_infop: pointer to be set to the loaded image protocol
601 * @path: dummy file path used to construct the device path
602 * set in the loaded image protocol
603 * @load_options_path: name of a U-Boot environment variable. Its value is
604 * set as load options in the loaded image protocol.
605 * Return: status code
Simon Glassd9717ea2018-11-25 20:14:37 -0700606 */
607static efi_status_t bootefi_test_prepare
608 (struct efi_loaded_image_obj **image_objp,
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100609 struct efi_loaded_image **loaded_image_infop, const char *path,
610 const char *load_options_path)
Simon Glassd9717ea2018-11-25 20:14:37 -0700611{
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100612 efi_status_t ret;
613
Simon Glassd9717ea2018-11-25 20:14:37 -0700614 /* Construct a dummy device path */
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100615 bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, 0, 0);
Simon Glassd9717ea2018-11-25 20:14:37 -0700616 if (!bootefi_device_path)
617 return EFI_OUT_OF_RESOURCES;
Simon Glassd9717ea2018-11-25 20:14:37 -0700618
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100619 bootefi_image_path = efi_dp_from_file(NULL, 0, path);
620 if (!bootefi_image_path) {
621 ret = EFI_OUT_OF_RESOURCES;
622 goto failure;
623 }
624
625 ret = bootefi_run_prepare(load_options_path, bootefi_device_path,
626 bootefi_image_path, image_objp,
627 loaded_image_infop);
628 if (ret == EFI_SUCCESS)
629 return ret;
630
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100631failure:
Heinrich Schuchardt5f595182021-01-12 12:46:24 +0100632 efi_clear_bootdev();
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +0100633 return ret;
Simon Glassd9717ea2018-11-25 20:14:37 -0700634}
635
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900636/**
637 * bootefi_run_finish() - finish up after running an EFI test
638 *
639 * @loaded_image_info: Pointer to a struct which holds the loaded image info
640 * @image_obj: Pointer to a struct which holds the loaded image object
641 */
642static void bootefi_run_finish(struct efi_loaded_image_obj *image_obj,
643 struct efi_loaded_image *loaded_image_info)
644{
645 efi_restore_gd();
646 free(loaded_image_info->load_options);
647 efi_delete_handle(&image_obj->header);
648}
649
650/**
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200651 * do_efi_selftest() - execute EFI selftest
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900652 *
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900653 * Return: status code
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900654 */
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200655static int do_efi_selftest(void)
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900656{
657 struct efi_loaded_image_obj *image_obj;
658 struct efi_loaded_image *loaded_image_info;
659 efi_status_t ret;
660
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900661 ret = bootefi_test_prepare(&image_obj, &loaded_image_info,
662 "\\selftest", "efi_selftest");
663 if (ret != EFI_SUCCESS)
664 return CMD_RET_FAILURE;
665
666 /* Execute the test */
667 ret = EFI_CALL(efi_selftest(&image_obj->header, &systab));
668 bootefi_run_finish(image_obj, loaded_image_info);
669
670 return ret != EFI_SUCCESS;
671}
Simon Glassd9717ea2018-11-25 20:14:37 -0700672#endif /* CONFIG_CMD_BOOTEFI_SELFTEST */
673
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200674/**
675 * do_bootefi() - execute `bootefi` command
676 *
677 * @cmdtp: table entry describing command
678 * @flag: bitmap indicating how the command was invoked
679 * @argc: number of arguments
680 * @argv: command line arguments
681 * Return: status code
682 */
Simon Glass09140112020-05-10 11:40:03 -0600683static int do_bootefi(struct cmd_tbl *cmdtp, int flag, int argc,
684 char *const argv[])
Alexander Grafb9939332016-03-10 00:27:20 +0100685{
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200686 efi_status_t ret;
Heinrich Schuchardtf64f2232019-12-08 01:07:01 +0100687 void *fdt;
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200688
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900689 if (argc < 2)
690 return CMD_RET_USAGE;
AKASHI Takahirod6b21892019-04-19 12:22:33 +0900691
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200692 /* Initialize EFI drivers */
693 ret = efi_init_obj_list();
694 if (ret != EFI_SUCCESS) {
Heinrich Schuchardtc0018372020-07-17 20:21:00 +0200695 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
696 ret & ~EFI_ERROR_MASK);
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200697 return CMD_RET_FAILURE;
698 }
699
Heinrich Schuchardtf64f2232019-12-08 01:07:01 +0100700 if (argc > 2) {
701 uintptr_t fdt_addr;
702
Heinrich Schuchardt7a597252019-11-28 06:46:09 +0100703 fdt_addr = simple_strtoul(argv[2], NULL, 16);
Heinrich Schuchardtf64f2232019-12-08 01:07:01 +0100704 fdt = map_sysmem(fdt_addr, 0);
705 } else {
706 fdt = EFI_FDT_USE_INTERNAL;
707 }
708 ret = efi_install_fdt(fdt);
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200709 if (ret == EFI_INVALID_PARAMETER)
710 return CMD_RET_USAGE;
711 else if (ret != EFI_SUCCESS)
712 return CMD_RET_FAILURE;
713
AKASHI Takahirod6b21892019-04-19 12:22:33 +0900714 if (!strcmp(argv[1], "bootmgr"))
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200715 return do_efibootmgr();
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900716#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
717 else if (!strcmp(argv[1], "selftest"))
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200718 return do_efi_selftest();
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900719#endif
720
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200721 return do_bootefi_image(argv[1]);
Alexander Grafb9939332016-03-10 00:27:20 +0100722}
723
724#ifdef CONFIG_SYS_LONGHELP
725static char bootefi_help_text[] =
Alexander Graf1c398092016-04-14 16:07:53 +0200726 "<image address> [fdt address]\n"
727 " - boot EFI payload stored at address <image address>.\n"
728 " If specified, the device tree located at <fdt address> gets\n"
Simon Glassc7ae3df2016-11-07 08:47:08 -0700729 " exposed as EFI configuration table.\n"
730#ifdef CONFIG_CMD_BOOTEFI_HELLO
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200731 "bootefi hello\n"
732 " - boot a sample Hello World application stored within U-Boot\n"
733#endif
734#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100735 "bootefi selftest [fdt address]\n"
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200736 " - boot an EFI selftest application stored within U-Boot\n"
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200737 " Use environment variable efi_selftest to select a single test.\n"
738 " Use 'setenv efi_selftest list' to enumerate all tests.\n"
Simon Glassc7ae3df2016-11-07 08:47:08 -0700739#endif
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900740 "bootefi bootmgr [fdt address]\n"
Rob Clark9975fe92017-09-13 18:05:38 -0400741 " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
742 "\n"
743 " If specified, the device tree located at <fdt address> gets\n"
744 " exposed as EFI configuration table.\n";
Alexander Grafb9939332016-03-10 00:27:20 +0100745#endif
746
747U_BOOT_CMD(
Alexander Graf1c398092016-04-14 16:07:53 +0200748 bootefi, 3, 0, do_bootefi,
Sergey Kubushyn92dfd922016-06-07 11:14:31 -0700749 "Boots an EFI payload from memory",
Alexander Grafb9939332016-03-10 00:27:20 +0100750 bootefi_help_text
751);