blob: 8e8752127ed85fe350dede9d54f9fe4cf110a452 [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Grafe2b04f22016-03-10 00:27:20 +01002/*
3 * EFI application loader
4 *
5 * Copyright (c) 2016 Alexander Graf
Alexander Grafe2b04f22016-03-10 00:27:20 +01006 */
7
Heinrich Schuchardtbb38a1f2020-07-17 20:21:00 +02008#define LOG_CATEGORY LOGC_EFI
9
Alexander Grafe2b04f22016-03-10 00:27:20 +010010#include <command.h>
AKASHI Takahiro7b061922023-11-21 10:29:44 +090011#include <efi.h>
Simon Glass37972f42025-05-24 11:28:21 -060012#include <efi_device_path.h>
Alexander Grafe2b04f22016-03-10 00:27:20 +010013#include <efi_loader.h>
AKASHI Takahiro7b061922023-11-21 10:29:44 +090014#include <exports.h>
Heinrich Schuchardtbb38a1f2020-07-17 20:21:00 +020015#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070016#include <malloc.h>
Alexander Grafa2414512018-06-18 17:22:58 +020017#include <mapmem.h>
AKASHI Takahiro7b061922023-11-21 10:29:44 +090018#include <vsprintf.h>
Simon Glassc2194bf2016-09-25 15:27:32 -060019#include <asm-generic/sections.h>
AKASHI Takahiro7b061922023-11-21 10:29:44 +090020#include <asm/global_data.h>
21#include <linux/string.h>
Alexander Graf2ff5cc32016-04-11 16:55:26 +020022
23DECLARE_GLOBAL_DATA_PTR;
Alexander Grafe2b04f22016-03-10 00:27:20 +010024
AKASHI Takahiro203d1962023-11-21 10:29:43 +090025static struct efi_device_path *test_image_path;
26static struct efi_device_path *test_device_path;
AKASHI Takahirod52dacb2023-11-21 10:29:42 +090027
AKASHI Takahiro09a81c72019-04-19 12:22:31 +090028static efi_status_t bootefi_run_prepare(const char *load_options_path,
29 struct efi_device_path *device_path,
30 struct efi_device_path *image_path,
31 struct efi_loaded_image_obj **image_objp,
32 struct efi_loaded_image **loaded_image_infop)
33{
34 efi_status_t ret;
Heinrich Schuchardte6263952020-01-03 22:53:42 +010035 u16 *load_options;
AKASHI Takahiro09a81c72019-04-19 12:22:31 +090036
37 ret = efi_setup_loaded_image(device_path, image_path, image_objp,
38 loaded_image_infop);
39 if (ret != EFI_SUCCESS)
40 return ret;
41
Heinrich Schuchardtf0b44232025-04-11 07:36:45 +020042 (*image_objp)->auth_status = EFI_IMAGE_AUTH_PASSED;
43 (*image_objp)->entry = efi_selftest;
44
AKASHI Takahiro09a81c72019-04-19 12:22:31 +090045 /* Transfer environment variable as load options */
Heinrich Schuchardt25c6be52020-08-07 17:47:13 +020046 return efi_env_set_load_options((efi_handle_t)*image_objp,
47 load_options_path,
48 &load_options);
AKASHI Takahiro09a81c72019-04-19 12:22:31 +090049}
50
Simon Glass93f25592018-11-25 20:14:37 -070051/**
52 * bootefi_test_prepare() - prepare to run an EFI test
53 *
Heinrich Schuchardtfd9cbe32019-01-12 14:42:40 +010054 * Prepare to run a test as if it were provided by a loaded image.
Simon Glass93f25592018-11-25 20:14:37 -070055 *
Heinrich Schuchardtfd9cbe32019-01-12 14:42:40 +010056 * @image_objp: pointer to be set to the loaded image handle
57 * @loaded_image_infop: pointer to be set to the loaded image protocol
58 * @path: dummy file path used to construct the device path
59 * set in the loaded image protocol
60 * @load_options_path: name of a U-Boot environment variable. Its value is
61 * set as load options in the loaded image protocol.
62 * Return: status code
Simon Glass93f25592018-11-25 20:14:37 -070063 */
64static efi_status_t bootefi_test_prepare
65 (struct efi_loaded_image_obj **image_objp,
Heinrich Schuchardtfd9cbe32019-01-12 14:42:40 +010066 struct efi_loaded_image **loaded_image_infop, const char *path,
67 const char *load_options_path)
Simon Glass93f25592018-11-25 20:14:37 -070068{
Heinrich Schuchardtfd9cbe32019-01-12 14:42:40 +010069 efi_status_t ret;
70
Simon Glass93f25592018-11-25 20:14:37 -070071 /* Construct a dummy device path */
AKASHI Takahiro203d1962023-11-21 10:29:43 +090072 test_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, 0, 0);
73 if (!test_device_path)
Simon Glass93f25592018-11-25 20:14:37 -070074 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardtfd9cbe32019-01-12 14:42:40 +010075
AKASHI Takahiro203d1962023-11-21 10:29:43 +090076 test_image_path = efi_dp_from_file(NULL, path);
77 if (!test_image_path) {
Heinrich Schuchardtfd9cbe32019-01-12 14:42:40 +010078 ret = EFI_OUT_OF_RESOURCES;
79 goto failure;
80 }
81
AKASHI Takahiro203d1962023-11-21 10:29:43 +090082 ret = bootefi_run_prepare(load_options_path, test_device_path,
83 test_image_path, image_objp,
Heinrich Schuchardtfd9cbe32019-01-12 14:42:40 +010084 loaded_image_infop);
85 if (ret == EFI_SUCCESS)
86 return ret;
Simon Glass93f25592018-11-25 20:14:37 -070087
Heinrich Schuchardtfd9cbe32019-01-12 14:42:40 +010088failure:
AKASHI Takahiro203d1962023-11-21 10:29:43 +090089 efi_free_pool(test_device_path);
90 efi_free_pool(test_image_path);
91 /* TODO: not sure calling clear function is necessary */
Heinrich Schuchardt6b821592021-01-12 12:46:24 +010092 efi_clear_bootdev();
Heinrich Schuchardtfd9cbe32019-01-12 14:42:40 +010093 return ret;
Simon Glass93f25592018-11-25 20:14:37 -070094}
95
AKASHI Takahiro09a81c72019-04-19 12:22:31 +090096/**
Heinrich Schuchardt8fc56c62019-05-12 20:16:25 +020097 * do_efi_selftest() - execute EFI selftest
AKASHI Takahiro09a81c72019-04-19 12:22:31 +090098 *
AKASHI Takahiro09a81c72019-04-19 12:22:31 +090099 * Return: status code
AKASHI Takahiro09a81c72019-04-19 12:22:31 +0900100 */
Heinrich Schuchardt8fc56c62019-05-12 20:16:25 +0200101static int do_efi_selftest(void)
AKASHI Takahiro09a81c72019-04-19 12:22:31 +0900102{
103 struct efi_loaded_image_obj *image_obj;
104 struct efi_loaded_image *loaded_image_info;
105 efi_status_t ret;
106
AKASHI Takahiro09a81c72019-04-19 12:22:31 +0900107 ret = bootefi_test_prepare(&image_obj, &loaded_image_info,
108 "\\selftest", "efi_selftest");
109 if (ret != EFI_SUCCESS)
110 return CMD_RET_FAILURE;
111
112 /* Execute the test */
Heinrich Schuchardtf0b44232025-04-11 07:36:45 +0200113 ret = do_bootefi_exec(&image_obj->header,
114 loaded_image_info->load_options);
AKASHI Takahiro203d1962023-11-21 10:29:43 +0900115 efi_free_pool(test_device_path);
116 efi_free_pool(test_image_path);
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +0300117 if (ret != EFI_SUCCESS)
118 efi_delete_handle(&image_obj->header);
119 else
120 ret = efi_delete_handle(&image_obj->header);
AKASHI Takahiro09a81c72019-04-19 12:22:31 +0900121
122 return ret != EFI_SUCCESS;
123}
Simon Glass93f25592018-11-25 20:14:37 -0700124
Heinrich Schuchardt8fc56c62019-05-12 20:16:25 +0200125/**
126 * do_bootefi() - execute `bootefi` command
127 *
128 * @cmdtp: table entry describing command
129 * @flag: bitmap indicating how the command was invoked
130 * @argc: number of arguments
131 * @argv: command line arguments
132 * Return: status code
133 */
Simon Glassed38aef2020-05-10 11:40:03 -0600134static int do_bootefi(struct cmd_tbl *cmdtp, int flag, int argc,
135 char *const argv[])
Alexander Grafe2b04f22016-03-10 00:27:20 +0100136{
Heinrich Schuchardt8fc56c62019-05-12 20:16:25 +0200137 efi_status_t ret;
AKASHI Takahiro28cb4a82023-11-21 10:29:39 +0900138 char *p;
139 void *fdt, *image_buf;
140 unsigned long addr, size;
AKASHI Takahiro7b061922023-11-21 10:29:44 +0900141 void *image_addr;
142 size_t image_size;
Heinrich Schuchardt8fc56c62019-05-12 20:16:25 +0200143
AKASHI Takahiro09a81c72019-04-19 12:22:31 +0900144 if (argc < 2)
145 return CMD_RET_USAGE;
AKASHI Takahirocf819832019-04-19 12:22:33 +0900146
Heinrich Schuchardt3ea20e72022-05-19 08:00:56 +0200147 if (argc > 2) {
Heinrich Schuchardt62467412019-12-08 01:07:01 +0100148 uintptr_t fdt_addr;
149
Simon Glass3ff49ec2021-07-24 09:03:29 -0600150 fdt_addr = hextoul(argv[2], NULL);
Heinrich Schuchardt62467412019-12-08 01:07:01 +0100151 fdt = map_sysmem(fdt_addr, 0);
152 } else {
153 fdt = EFI_FDT_USE_INTERNAL;
154 }
AKASHI Takahiro6a6858e2023-11-21 10:29:40 +0900155
156 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR) &&
157 !strcmp(argv[1], "bootmgr")) {
AKASHI Takahiro4ab6c5b2023-11-21 10:29:41 +0900158 ret = efi_bootmgr_run(fdt);
Heinrich Schuchardt8fc56c62019-05-12 20:16:25 +0200159
Heinrich Schuchardt985482d2024-03-16 10:36:43 +0100160 if (ret != EFI_SUCCESS)
AKASHI Takahiro6a6858e2023-11-21 10:29:40 +0900161 return CMD_RET_FAILURE;
162
AKASHI Takahiro4ab6c5b2023-11-21 10:29:41 +0900163 return CMD_RET_SUCCESS;
Heinrich Schuchardtb2625e82021-01-15 19:02:50 +0100164 }
AKASHI Takahiro6a6858e2023-11-21 10:29:40 +0900165
166 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_SELFTEST) &&
167 !strcmp(argv[1], "selftest")) {
168 /* Initialize EFI drivers */
169 ret = efi_init_obj_list();
170 if (ret != EFI_SUCCESS) {
171 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
172 ret & ~EFI_ERROR_MASK);
173 return CMD_RET_FAILURE;
174 }
175
176 ret = efi_install_fdt(fdt);
Heinrich Schuchardt42f99792024-03-16 10:36:44 +0100177 if (ret != EFI_SUCCESS)
AKASHI Takahiro6a6858e2023-11-21 10:29:40 +0900178 return CMD_RET_FAILURE;
179
Heinrich Schuchardt8fc56c62019-05-12 20:16:25 +0200180 return do_efi_selftest();
AKASHI Takahiro6a6858e2023-11-21 10:29:40 +0900181 }
AKASHI Takahiro28cb4a82023-11-21 10:29:39 +0900182
AKASHI Takahiro6a6858e2023-11-21 10:29:40 +0900183 if (!IS_ENABLED(CONFIG_CMD_BOOTEFI_BINARY))
184 return CMD_RET_SUCCESS;
185
186 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_HELLO) &&
187 !strcmp(argv[1], "hello")) {
AKASHI Takahiro28cb4a82023-11-21 10:29:39 +0900188 image_buf = __efi_helloworld_begin;
189 size = __efi_helloworld_end - __efi_helloworld_begin;
AKASHI Takahiro7b061922023-11-21 10:29:44 +0900190 /* TODO: not sure calling clear function is necessary */
AKASHI Takahiro28cb4a82023-11-21 10:29:39 +0900191 efi_clear_bootdev();
AKASHI Takahiro6a6858e2023-11-21 10:29:40 +0900192 } else {
AKASHI Takahiro28cb4a82023-11-21 10:29:39 +0900193 addr = strtoul(argv[1], NULL, 16);
194 /* Check that a numeric value was passed */
195 if (!addr)
196 return CMD_RET_USAGE;
197 image_buf = map_sysmem(addr, 0);
198
199 p = strchr(argv[1], ':');
200 if (p) {
201 size = strtoul(++p, NULL, 16);
202 if (!size)
203 return CMD_RET_USAGE;
204 efi_clear_bootdev();
205 } else {
AKASHI Takahiro7b061922023-11-21 10:29:44 +0900206 /* Image should be already loaded */
207 efi_get_image_parameters(&image_addr, &image_size);
208
AKASHI Takahiro28cb4a82023-11-21 10:29:39 +0900209 if (image_buf != image_addr) {
210 log_err("No UEFI binary known at %s\n",
211 argv[1]);
212 return CMD_RET_FAILURE;
213 }
214 size = image_size;
215 }
Heinrich Schuchardt3ea20e72022-05-19 08:00:56 +0200216 }
AKASHI Takahiro6a6858e2023-11-21 10:29:40 +0900217
Adriano Cordovacea09ba2025-03-19 11:45:00 -0300218 ret = efi_binary_run(image_buf, size, fdt, NULL, 0);
AKASHI Takahiro6a6858e2023-11-21 10:29:40 +0900219
Heinrich Schuchardt985482d2024-03-16 10:36:43 +0100220 if (ret != EFI_SUCCESS)
AKASHI Takahiro28cb4a82023-11-21 10:29:39 +0900221 return CMD_RET_FAILURE;
222
223 return CMD_RET_SUCCESS;
Alexander Grafe2b04f22016-03-10 00:27:20 +0100224}
225
Tom Rini03f146c2023-10-07 15:13:08 -0400226U_BOOT_LONGHELP(bootefi,
Heinrich Schuchardt3ea20e72022-05-19 08:00:56 +0200227 "<image address>[:<image size>] [<fdt address>]\n"
228 " - boot EFI payload\n"
Simon Glassfac4ced2016-11-07 08:47:08 -0700229#ifdef CONFIG_CMD_BOOTEFI_HELLO
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200230 "bootefi hello\n"
231 " - boot a sample Hello World application stored within U-Boot\n"
232#endif
233#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100234 "bootefi selftest [fdt address]\n"
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200235 " - boot an EFI selftest application stored within U-Boot\n"
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200236 " Use environment variable efi_selftest to select a single test.\n"
237 " Use 'setenv efi_selftest list' to enumerate all tests.\n"
Simon Glassfac4ced2016-11-07 08:47:08 -0700238#endif
Heinrich Schuchardtb2625e82021-01-15 19:02:50 +0100239#ifdef CONFIG_CMD_BOOTEFI_BOOTMGR
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900240 "bootefi bootmgr [fdt address]\n"
Rob Clarkc84c1102017-09-13 18:05:38 -0400241 " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
242 "\n"
243 " If specified, the device tree located at <fdt address> gets\n"
Heinrich Schuchardtb2625e82021-01-15 19:02:50 +0100244 " exposed as EFI configuration table.\n"
245#endif
Tom Rini03f146c2023-10-07 15:13:08 -0400246 );
Alexander Grafe2b04f22016-03-10 00:27:20 +0100247
248U_BOOT_CMD(
Kyle Evans2c3bf4e2022-04-10 16:05:55 -0500249 bootefi, 4, 0, do_bootefi,
Sergey Kubushyn268f19e2016-06-07 11:14:31 -0700250 "Boots an EFI payload from memory",
Alexander Grafe2b04f22016-03-10 00:27:20 +0100251 bootefi_help_text
252);