blob: 9cf9027bf40914e1665957a6c06694d2a07e27a9 [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 <command.h>
AKASHI Takahiro0bef4b02023-11-21 10:29:44 +090011#include <efi.h>
Alexander Grafb9939332016-03-10 00:27:20 +010012#include <efi_loader.h>
AKASHI Takahiro0bef4b02023-11-21 10:29:44 +090013#include <exports.h>
Heinrich Schuchardtc0018372020-07-17 20:21:00 +020014#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070015#include <malloc.h>
Alexander Graf354264b2018-06-18 17:22:58 +020016#include <mapmem.h>
AKASHI Takahiro0bef4b02023-11-21 10:29:44 +090017#include <vsprintf.h>
Simon Glasse2754582016-09-25 15:27:32 -060018#include <asm-generic/sections.h>
AKASHI Takahiro0bef4b02023-11-21 10:29:44 +090019#include <asm/global_data.h>
20#include <linux/string.h>
Alexander Graf0d9d5012016-04-11 16:55:26 +020021
22DECLARE_GLOBAL_DATA_PTR;
Alexander Grafb9939332016-03-10 00:27:20 +010023
AKASHI Takahirod07e7be2023-11-21 10:29:43 +090024static struct efi_device_path *test_image_path;
25static struct efi_device_path *test_device_path;
AKASHI Takahiro5c129fe2023-11-21 10:29:42 +090026
AKASHI Takahiro3fc2b162019-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 Schuchardta3850e42020-01-03 22:53:42 +010034 u16 *load_options;
AKASHI Takahiro3fc2b162019-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
41 /* Transfer environment variable as load options */
Heinrich Schuchardt1064d042020-08-07 17:47:13 +020042 return efi_env_set_load_options((efi_handle_t)*image_objp,
43 load_options_path,
44 &load_options);
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +090045}
46
Simon Glassd9717ea2018-11-25 20:14:37 -070047/**
48 * bootefi_test_prepare() - prepare to run an EFI test
49 *
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +010050 * Prepare to run a test as if it were provided by a loaded image.
Simon Glassd9717ea2018-11-25 20:14:37 -070051 *
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +010052 * @image_objp: pointer to be set to the loaded image handle
53 * @loaded_image_infop: pointer to be set to the loaded image protocol
54 * @path: dummy file path used to construct the device path
55 * set in the loaded image protocol
56 * @load_options_path: name of a U-Boot environment variable. Its value is
57 * set as load options in the loaded image protocol.
58 * Return: status code
Simon Glassd9717ea2018-11-25 20:14:37 -070059 */
60static efi_status_t bootefi_test_prepare
61 (struct efi_loaded_image_obj **image_objp,
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +010062 struct efi_loaded_image **loaded_image_infop, const char *path,
63 const char *load_options_path)
Simon Glassd9717ea2018-11-25 20:14:37 -070064{
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +010065 efi_status_t ret;
66
Simon Glassd9717ea2018-11-25 20:14:37 -070067 /* Construct a dummy device path */
AKASHI Takahirod07e7be2023-11-21 10:29:43 +090068 test_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, 0, 0);
69 if (!test_device_path)
Simon Glassd9717ea2018-11-25 20:14:37 -070070 return EFI_OUT_OF_RESOURCES;
Simon Glassd9717ea2018-11-25 20:14:37 -070071
AKASHI Takahirod07e7be2023-11-21 10:29:43 +090072 test_image_path = efi_dp_from_file(NULL, path);
73 if (!test_image_path) {
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +010074 ret = EFI_OUT_OF_RESOURCES;
75 goto failure;
76 }
77
AKASHI Takahirod07e7be2023-11-21 10:29:43 +090078 ret = bootefi_run_prepare(load_options_path, test_device_path,
79 test_image_path, image_objp,
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +010080 loaded_image_infop);
81 if (ret == EFI_SUCCESS)
82 return ret;
83
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +010084failure:
AKASHI Takahirod07e7be2023-11-21 10:29:43 +090085 efi_free_pool(test_device_path);
86 efi_free_pool(test_image_path);
87 /* TODO: not sure calling clear function is necessary */
Heinrich Schuchardt5f595182021-01-12 12:46:24 +010088 efi_clear_bootdev();
Heinrich Schuchardt1504bb02019-01-12 14:42:40 +010089 return ret;
Simon Glassd9717ea2018-11-25 20:14:37 -070090}
91
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +090092/**
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +020093 * do_efi_selftest() - execute EFI selftest
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +090094 *
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +090095 * Return: status code
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +090096 */
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +020097static int do_efi_selftest(void)
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +090098{
99 struct efi_loaded_image_obj *image_obj;
100 struct efi_loaded_image *loaded_image_info;
101 efi_status_t ret;
102
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900103 ret = bootefi_test_prepare(&image_obj, &loaded_image_info,
104 "\\selftest", "efi_selftest");
105 if (ret != EFI_SUCCESS)
106 return CMD_RET_FAILURE;
107
108 /* Execute the test */
109 ret = EFI_CALL(efi_selftest(&image_obj->header, &systab));
Ilias Apalodimas54edc372023-07-24 13:17:36 +0300110 efi_restore_gd();
111 free(loaded_image_info->load_options);
AKASHI Takahirod07e7be2023-11-21 10:29:43 +0900112 efi_free_pool(test_device_path);
113 efi_free_pool(test_image_path);
Ilias Apalodimas54edc372023-07-24 13:17:36 +0300114 if (ret != EFI_SUCCESS)
115 efi_delete_handle(&image_obj->header);
116 else
117 ret = efi_delete_handle(&image_obj->header);
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900118
119 return ret != EFI_SUCCESS;
120}
Simon Glassd9717ea2018-11-25 20:14:37 -0700121
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200122/**
123 * do_bootefi() - execute `bootefi` command
124 *
125 * @cmdtp: table entry describing command
126 * @flag: bitmap indicating how the command was invoked
127 * @argc: number of arguments
128 * @argv: command line arguments
129 * Return: status code
130 */
Simon Glass09140112020-05-10 11:40:03 -0600131static int do_bootefi(struct cmd_tbl *cmdtp, int flag, int argc,
132 char *const argv[])
Alexander Grafb9939332016-03-10 00:27:20 +0100133{
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200134 efi_status_t ret;
AKASHI Takahiro05e2cad2023-11-21 10:29:39 +0900135 char *p;
136 void *fdt, *image_buf;
137 unsigned long addr, size;
AKASHI Takahiro0bef4b02023-11-21 10:29:44 +0900138 void *image_addr;
139 size_t image_size;
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200140
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900141 if (argc < 2)
142 return CMD_RET_USAGE;
AKASHI Takahirod6b21892019-04-19 12:22:33 +0900143
Heinrich Schuchardt8131c852022-05-19 08:00:56 +0200144 if (argc > 2) {
Heinrich Schuchardtf64f2232019-12-08 01:07:01 +0100145 uintptr_t fdt_addr;
146
Simon Glass7e5f4602021-07-24 09:03:29 -0600147 fdt_addr = hextoul(argv[2], NULL);
Heinrich Schuchardtf64f2232019-12-08 01:07:01 +0100148 fdt = map_sysmem(fdt_addr, 0);
149 } else {
150 fdt = EFI_FDT_USE_INTERNAL;
151 }
Heinrich Schuchardt7e92db82019-05-12 20:16:25 +0200152
AKASHI Takahiro296faf42023-11-21 10:29:40 +0900153 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR) &&
154 !strcmp(argv[1], "bootmgr")) {
AKASHI Takahiroc3530ae2023-11-21 10:29:41 +0900155 ret = efi_bootmgr_run(fdt);
AKASHI Takahiro296faf42023-11-21 10:29:40 +0900156
AKASHI Takahiro296faf42023-11-21 10:29:40 +0900157 if (ret == EFI_INVALID_PARAMETER)
158 return CMD_RET_USAGE;
AKASHI Takahiroc3530ae2023-11-21 10:29:41 +0900159 else if (ret)
AKASHI Takahiro296faf42023-11-21 10:29:40 +0900160 return CMD_RET_FAILURE;
161
AKASHI Takahiroc3530ae2023-11-21 10:29:41 +0900162 return CMD_RET_SUCCESS;
Heinrich Schuchardtff2f5322021-01-15 19:02:50 +0100163 }
AKASHI Takahiro3fc2b162019-04-19 12:22:31 +0900164
AKASHI Takahiro296faf42023-11-21 10:29:40 +0900165 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);
176 if (ret == EFI_INVALID_PARAMETER)
177 return CMD_RET_USAGE;
178 else if (ret != EFI_SUCCESS)
179 return CMD_RET_FAILURE;
180
181 return do_efi_selftest();
182 }
183
184 if (!IS_ENABLED(CONFIG_CMD_BOOTEFI_BINARY))
185 return CMD_RET_SUCCESS;
186
187 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_HELLO) &&
188 !strcmp(argv[1], "hello")) {
AKASHI Takahiro05e2cad2023-11-21 10:29:39 +0900189 image_buf = __efi_helloworld_begin;
190 size = __efi_helloworld_end - __efi_helloworld_begin;
AKASHI Takahiro0bef4b02023-11-21 10:29:44 +0900191 /* TODO: not sure calling clear function is necessary */
AKASHI Takahiro05e2cad2023-11-21 10:29:39 +0900192 efi_clear_bootdev();
AKASHI Takahiro296faf42023-11-21 10:29:40 +0900193 } else {
AKASHI Takahiro05e2cad2023-11-21 10:29:39 +0900194 addr = strtoul(argv[1], NULL, 16);
195 /* Check that a numeric value was passed */
196 if (!addr)
197 return CMD_RET_USAGE;
198 image_buf = map_sysmem(addr, 0);
199
200 p = strchr(argv[1], ':');
201 if (p) {
202 size = strtoul(++p, NULL, 16);
203 if (!size)
204 return CMD_RET_USAGE;
205 efi_clear_bootdev();
206 } else {
AKASHI Takahiro0bef4b02023-11-21 10:29:44 +0900207 /* Image should be already loaded */
208 efi_get_image_parameters(&image_addr, &image_size);
209
AKASHI Takahiro05e2cad2023-11-21 10:29:39 +0900210 if (image_buf != image_addr) {
211 log_err("No UEFI binary known at %s\n",
212 argv[1]);
213 return CMD_RET_FAILURE;
214 }
215 size = image_size;
216 }
217 }
AKASHI Takahiro296faf42023-11-21 10:29:40 +0900218
AKASHI Takahiro5c129fe2023-11-21 10:29:42 +0900219 ret = efi_binary_run(image_buf, size, fdt);
AKASHI Takahiro296faf42023-11-21 10:29:40 +0900220
AKASHI Takahiro296faf42023-11-21 10:29:40 +0900221 if (ret == EFI_INVALID_PARAMETER)
222 return CMD_RET_USAGE;
AKASHI Takahiro5c129fe2023-11-21 10:29:42 +0900223 else if (ret)
AKASHI Takahiro05e2cad2023-11-21 10:29:39 +0900224 return CMD_RET_FAILURE;
225
226 return CMD_RET_SUCCESS;
Alexander Grafb9939332016-03-10 00:27:20 +0100227}
228
Tom Rini36162182023-10-07 15:13:08 -0400229U_BOOT_LONGHELP(bootefi,
Heinrich Schuchardt8131c852022-05-19 08:00:56 +0200230 "<image address>[:<image size>] [<fdt address>]\n"
231 " - boot EFI payload\n"
Simon Glassc7ae3df2016-11-07 08:47:08 -0700232#ifdef CONFIG_CMD_BOOTEFI_HELLO
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200233 "bootefi hello\n"
234 " - boot a sample Hello World application stored within U-Boot\n"
235#endif
236#ifdef CONFIG_CMD_BOOTEFI_SELFTEST
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +0100237 "bootefi selftest [fdt address]\n"
Heinrich Schuchardt623b3a52017-09-15 10:06:11 +0200238 " - boot an EFI selftest application stored within U-Boot\n"
Heinrich Schuchardtd78e40d2017-10-18 18:13:13 +0200239 " Use environment variable efi_selftest to select a single test.\n"
240 " Use 'setenv efi_selftest list' to enumerate all tests.\n"
Simon Glassc7ae3df2016-11-07 08:47:08 -0700241#endif
Heinrich Schuchardtff2f5322021-01-15 19:02:50 +0100242#ifdef CONFIG_CMD_BOOTEFI_BOOTMGR
AKASHI Takahiro6b95b382019-04-19 12:22:35 +0900243 "bootefi bootmgr [fdt address]\n"
Rob Clark9975fe92017-09-13 18:05:38 -0400244 " - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
245 "\n"
246 " If specified, the device tree located at <fdt address> gets\n"
Heinrich Schuchardtff2f5322021-01-15 19:02:50 +0100247 " exposed as EFI configuration table.\n"
248#endif
Tom Rini36162182023-10-07 15:13:08 -0400249 );
Alexander Grafb9939332016-03-10 00:27:20 +0100250
251U_BOOT_CMD(
Kyle Evans20589832022-04-10 16:05:55 -0500252 bootefi, 4, 0, do_bootefi,
Sergey Kubushyn92dfd922016-06-07 11:14:31 -0700253 "Boots an EFI payload from memory",
Alexander Grafb9939332016-03-10 00:27:20 +0100254 bootefi_help_text
255);