blob: cea6d356ee673eb377bcfe6f8bec9373c28f69fc [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>
Alexander Grafe2b04f22016-03-10 00:27:20 +010012#include <efi_loader.h>
AKASHI Takahiro7b061922023-11-21 10:29:44 +090013#include <exports.h>
Heinrich Schuchardtbb38a1f2020-07-17 20:21:00 +020014#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070015#include <malloc.h>
Alexander Grafa2414512018-06-18 17:22:58 +020016#include <mapmem.h>
AKASHI Takahiro7b061922023-11-21 10:29:44 +090017#include <vsprintf.h>
Simon Glassc2194bf2016-09-25 15:27:32 -060018#include <asm-generic/sections.h>
AKASHI Takahiro7b061922023-11-21 10:29:44 +090019#include <asm/global_data.h>
20#include <linux/string.h>
Alexander Graf2ff5cc32016-04-11 16:55:26 +020021
22DECLARE_GLOBAL_DATA_PTR;
Alexander Grafe2b04f22016-03-10 00:27:20 +010023
AKASHI Takahiro203d1962023-11-21 10:29:43 +090024static struct efi_device_path *test_image_path;
25static struct efi_device_path *test_device_path;
AKASHI Takahirod52dacb2023-11-21 10:29:42 +090026
AKASHI Takahiro09a81c72019-04-19 12:22:31 +090027static efi_status_t bootefi_run_prepare(const char *load_options_path,
28 struct efi_device_path *device_path,
29 struct efi_device_path *image_path,
30 struct efi_loaded_image_obj **image_objp,
31 struct efi_loaded_image **loaded_image_infop)
32{
33 efi_status_t ret;
Heinrich Schuchardte6263952020-01-03 22:53:42 +010034 u16 *load_options;
AKASHI Takahiro09a81c72019-04-19 12:22:31 +090035
36 ret = efi_setup_loaded_image(device_path, image_path, image_objp,
37 loaded_image_infop);
38 if (ret != EFI_SUCCESS)
39 return ret;
40
Heinrich Schuchardtf0b44232025-04-11 07:36:45 +020041 (*image_objp)->auth_status = EFI_IMAGE_AUTH_PASSED;
42 (*image_objp)->entry = efi_selftest;
43
AKASHI Takahiro09a81c72019-04-19 12:22:31 +090044 /* Transfer environment variable as load options */
Heinrich Schuchardt25c6be52020-08-07 17:47:13 +020045 return efi_env_set_load_options((efi_handle_t)*image_objp,
46 load_options_path,
47 &load_options);
AKASHI Takahiro09a81c72019-04-19 12:22:31 +090048}
49
Simon Glass93f25592018-11-25 20:14:37 -070050/**
51 * bootefi_test_prepare() - prepare to run an EFI test
52 *
Heinrich Schuchardtfd9cbe32019-01-12 14:42:40 +010053 * Prepare to run a test as if it were provided by a loaded image.
Simon Glass93f25592018-11-25 20:14:37 -070054 *
Heinrich Schuchardtfd9cbe32019-01-12 14:42:40 +010055 * @image_objp: pointer to be set to the loaded image handle
56 * @loaded_image_infop: pointer to be set to the loaded image protocol
57 * @path: dummy file path used to construct the device path
58 * set in the loaded image protocol
59 * @load_options_path: name of a U-Boot environment variable. Its value is
60 * set as load options in the loaded image protocol.
61 * Return: status code
Simon Glass93f25592018-11-25 20:14:37 -070062 */
63static efi_status_t bootefi_test_prepare
64 (struct efi_loaded_image_obj **image_objp,
Heinrich Schuchardtfd9cbe32019-01-12 14:42:40 +010065 struct efi_loaded_image **loaded_image_infop, const char *path,
66 const char *load_options_path)
Simon Glass93f25592018-11-25 20:14:37 -070067{
Heinrich Schuchardtfd9cbe32019-01-12 14:42:40 +010068 efi_status_t ret;
69
Simon Glass93f25592018-11-25 20:14:37 -070070 /* Construct a dummy device path */
AKASHI Takahiro203d1962023-11-21 10:29:43 +090071 test_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, 0, 0);
72 if (!test_device_path)
Simon Glass93f25592018-11-25 20:14:37 -070073 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardtfd9cbe32019-01-12 14:42:40 +010074
AKASHI Takahiro203d1962023-11-21 10:29:43 +090075 test_image_path = efi_dp_from_file(NULL, path);
76 if (!test_image_path) {
Heinrich Schuchardtfd9cbe32019-01-12 14:42:40 +010077 ret = EFI_OUT_OF_RESOURCES;
78 goto failure;
79 }
80
AKASHI Takahiro203d1962023-11-21 10:29:43 +090081 ret = bootefi_run_prepare(load_options_path, test_device_path,
82 test_image_path, image_objp,
Heinrich Schuchardtfd9cbe32019-01-12 14:42:40 +010083 loaded_image_infop);
84 if (ret == EFI_SUCCESS)
85 return ret;
Simon Glass93f25592018-11-25 20:14:37 -070086
Heinrich Schuchardtfd9cbe32019-01-12 14:42:40 +010087failure:
AKASHI Takahiro203d1962023-11-21 10:29:43 +090088 efi_free_pool(test_device_path);
89 efi_free_pool(test_image_path);
90 /* TODO: not sure calling clear function is necessary */
Heinrich Schuchardt6b821592021-01-12 12:46:24 +010091 efi_clear_bootdev();
Heinrich Schuchardtfd9cbe32019-01-12 14:42:40 +010092 return ret;
Simon Glass93f25592018-11-25 20:14:37 -070093}
94
AKASHI Takahiro09a81c72019-04-19 12:22:31 +090095/**
Heinrich Schuchardt8fc56c62019-05-12 20:16:25 +020096 * do_efi_selftest() - execute EFI selftest
AKASHI Takahiro09a81c72019-04-19 12:22:31 +090097 *
AKASHI Takahiro09a81c72019-04-19 12:22:31 +090098 * Return: status code
AKASHI Takahiro09a81c72019-04-19 12:22:31 +090099 */
Heinrich Schuchardt8fc56c62019-05-12 20:16:25 +0200100static int do_efi_selftest(void)
AKASHI Takahiro09a81c72019-04-19 12:22:31 +0900101{
102 struct efi_loaded_image_obj *image_obj;
103 struct efi_loaded_image *loaded_image_info;
104 efi_status_t ret;
105
AKASHI Takahiro09a81c72019-04-19 12:22:31 +0900106 ret = bootefi_test_prepare(&image_obj, &loaded_image_info,
107 "\\selftest", "efi_selftest");
108 if (ret != EFI_SUCCESS)
109 return CMD_RET_FAILURE;
110
111 /* Execute the test */
Heinrich Schuchardtf0b44232025-04-11 07:36:45 +0200112 ret = do_bootefi_exec(&image_obj->header,
113 loaded_image_info->load_options);
AKASHI Takahiro203d1962023-11-21 10:29:43 +0900114 efi_free_pool(test_device_path);
115 efi_free_pool(test_image_path);
Ilias Apalodimasb09ecf12023-07-24 13:17:36 +0300116 if (ret != EFI_SUCCESS)
117 efi_delete_handle(&image_obj->header);
118 else
119 ret = efi_delete_handle(&image_obj->header);
AKASHI Takahiro09a81c72019-04-19 12:22:31 +0900120
121 return ret != EFI_SUCCESS;
122}
Simon Glass93f25592018-11-25 20:14:37 -0700123
Heinrich Schuchardt8fc56c62019-05-12 20:16:25 +0200124/**
125 * do_bootefi() - execute `bootefi` command
126 *
127 * @cmdtp: table entry describing command
128 * @flag: bitmap indicating how the command was invoked
129 * @argc: number of arguments
130 * @argv: command line arguments
131 * Return: status code
132 */
Simon Glassed38aef2020-05-10 11:40:03 -0600133static int do_bootefi(struct cmd_tbl *cmdtp, int flag, int argc,
134 char *const argv[])
Alexander Grafe2b04f22016-03-10 00:27:20 +0100135{
Heinrich Schuchardt8fc56c62019-05-12 20:16:25 +0200136 efi_status_t ret;
AKASHI Takahiro28cb4a82023-11-21 10:29:39 +0900137 char *p;
138 void *fdt, *image_buf;
139 unsigned long addr, size;
AKASHI Takahiro7b061922023-11-21 10:29:44 +0900140 void *image_addr;
141 size_t image_size;
Heinrich Schuchardt8fc56c62019-05-12 20:16:25 +0200142
AKASHI Takahiro09a81c72019-04-19 12:22:31 +0900143 if (argc < 2)
144 return CMD_RET_USAGE;
AKASHI Takahirocf819832019-04-19 12:22:33 +0900145
Heinrich Schuchardt3ea20e72022-05-19 08:00:56 +0200146 if (argc > 2) {
Heinrich Schuchardt62467412019-12-08 01:07:01 +0100147 uintptr_t fdt_addr;
148
Simon Glass3ff49ec2021-07-24 09:03:29 -0600149 fdt_addr = hextoul(argv[2], NULL);
Heinrich Schuchardt62467412019-12-08 01:07:01 +0100150 fdt = map_sysmem(fdt_addr, 0);
151 } else {
152 fdt = EFI_FDT_USE_INTERNAL;
153 }
AKASHI Takahiro6a6858e2023-11-21 10:29:40 +0900154
155 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR) &&
156 !strcmp(argv[1], "bootmgr")) {
AKASHI Takahiro4ab6c5b2023-11-21 10:29:41 +0900157 ret = efi_bootmgr_run(fdt);
Heinrich Schuchardt8fc56c62019-05-12 20:16:25 +0200158
Heinrich Schuchardt985482d2024-03-16 10:36:43 +0100159 if (ret != EFI_SUCCESS)
AKASHI Takahiro6a6858e2023-11-21 10:29:40 +0900160 return CMD_RET_FAILURE;
161
AKASHI Takahiro4ab6c5b2023-11-21 10:29:41 +0900162 return CMD_RET_SUCCESS;
Heinrich Schuchardtb2625e82021-01-15 19:02:50 +0100163 }
AKASHI Takahiro6a6858e2023-11-21 10:29:40 +0900164
165 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_SELFTEST) &&
166 !strcmp(argv[1], "selftest")) {
167 /* Initialize EFI drivers */
168 ret = efi_init_obj_list();
169 if (ret != EFI_SUCCESS) {
170 log_err("Error: Cannot initialize UEFI sub-system, r = %lu\n",
171 ret & ~EFI_ERROR_MASK);
172 return CMD_RET_FAILURE;
173 }
174
175 ret = efi_install_fdt(fdt);
Heinrich Schuchardt42f99792024-03-16 10:36:44 +0100176 if (ret != EFI_SUCCESS)
AKASHI Takahiro6a6858e2023-11-21 10:29:40 +0900177 return CMD_RET_FAILURE;
178
Heinrich Schuchardt8fc56c62019-05-12 20:16:25 +0200179 return do_efi_selftest();
AKASHI Takahiro6a6858e2023-11-21 10:29:40 +0900180 }
AKASHI Takahiro28cb4a82023-11-21 10:29:39 +0900181
AKASHI Takahiro6a6858e2023-11-21 10:29:40 +0900182 if (!IS_ENABLED(CONFIG_CMD_BOOTEFI_BINARY))
183 return CMD_RET_SUCCESS;
184
185 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_HELLO) &&
186 !strcmp(argv[1], "hello")) {
AKASHI Takahiro28cb4a82023-11-21 10:29:39 +0900187 image_buf = __efi_helloworld_begin;
188 size = __efi_helloworld_end - __efi_helloworld_begin;
AKASHI Takahiro7b061922023-11-21 10:29:44 +0900189 /* TODO: not sure calling clear function is necessary */
AKASHI Takahiro28cb4a82023-11-21 10:29:39 +0900190 efi_clear_bootdev();
AKASHI Takahiro6a6858e2023-11-21 10:29:40 +0900191 } else {
AKASHI Takahiro28cb4a82023-11-21 10:29:39 +0900192 addr = strtoul(argv[1], NULL, 16);
193 /* Check that a numeric value was passed */
194 if (!addr)
195 return CMD_RET_USAGE;
196 image_buf = map_sysmem(addr, 0);
197
198 p = strchr(argv[1], ':');
199 if (p) {
200 size = strtoul(++p, NULL, 16);
201 if (!size)
202 return CMD_RET_USAGE;
203 efi_clear_bootdev();
204 } else {
AKASHI Takahiro7b061922023-11-21 10:29:44 +0900205 /* Image should be already loaded */
206 efi_get_image_parameters(&image_addr, &image_size);
207
AKASHI Takahiro28cb4a82023-11-21 10:29:39 +0900208 if (image_buf != image_addr) {
209 log_err("No UEFI binary known at %s\n",
210 argv[1]);
211 return CMD_RET_FAILURE;
212 }
213 size = image_size;
214 }
Heinrich Schuchardt3ea20e72022-05-19 08:00:56 +0200215 }
AKASHI Takahiro6a6858e2023-11-21 10:29:40 +0900216
Adriano Cordovacea09ba2025-03-19 11:45:00 -0300217 ret = efi_binary_run(image_buf, size, fdt, NULL, 0);
AKASHI Takahiro6a6858e2023-11-21 10:29:40 +0900218
Heinrich Schuchardt985482d2024-03-16 10:36:43 +0100219 if (ret != EFI_SUCCESS)
AKASHI Takahiro28cb4a82023-11-21 10:29:39 +0900220 return CMD_RET_FAILURE;
221
222 return CMD_RET_SUCCESS;
Alexander Grafe2b04f22016-03-10 00:27:20 +0100223}
224
Tom Rini03f146c2023-10-07 15:13:08 -0400225U_BOOT_LONGHELP(bootefi,
Heinrich Schuchardt3ea20e72022-05-19 08:00:56 +0200226 "<image address>[:<image size>] [<fdt address>]\n"
227 " - boot EFI payload\n"
Simon Glassfac4ced2016-11-07 08:47:08 -0700228#ifdef CONFIG_CMD_BOOTEFI_HELLO
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200229 "bootefi hello\n"
230 " - boot a sample Hello World application stored within U-Boot\n"
231#endif
232#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +0100233 "bootefi selftest [fdt address]\n"
Heinrich Schuchardtd33ae3e2017-09-15 10:06:11 +0200234 " - boot an EFI selftest application stored within U-Boot\n"
Heinrich Schuchardt02efd5d2017-10-18 18:13:13 +0200235 " Use environment variable efi_selftest to select a single test.\n"
236 " Use 'setenv efi_selftest list' to enumerate all tests.\n"
Simon Glassfac4ced2016-11-07 08:47:08 -0700237#endif
Heinrich Schuchardtb2625e82021-01-15 19:02:50 +0100238#ifdef CONFIG_CMD_BOOTEFI_BOOTMGR
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +0900239 "bootefi bootmgr [fdt address]\n"
Rob Clarkc84c1102017-09-13 18:05:38 -0400240 " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
241 "\n"
242 " If specified, the device tree located at <fdt address> gets\n"
Heinrich Schuchardtb2625e82021-01-15 19:02:50 +0100243 " exposed as EFI configuration table.\n"
244#endif
Tom Rini03f146c2023-10-07 15:13:08 -0400245 );
Alexander Grafe2b04f22016-03-10 00:27:20 +0100246
247U_BOOT_CMD(
Kyle Evans2c3bf4e2022-04-10 16:05:55 -0500248 bootefi, 4, 0, do_bootefi,
Sergey Kubushyn268f19e2016-06-07 11:14:31 -0700249 "Boots an EFI payload from memory",
Alexander Grafe2b04f22016-03-10 00:27:20 +0100250 bootefi_help_text
251);