Tom Rini | 70df9d6 | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 2 | /* |
| 3 | * EFI application loader |
| 4 | * |
| 5 | * Copyright (c) 2016 Alexander Graf |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Heinrich Schuchardt | bb38a1f | 2020-07-17 20:21:00 +0200 | [diff] [blame] | 8 | #define LOG_CATEGORY LOGC_EFI |
| 9 | |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 10 | #include <command.h> |
AKASHI Takahiro | 7b06192 | 2023-11-21 10:29:44 +0900 | [diff] [blame] | 11 | #include <efi.h> |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 12 | #include <efi_loader.h> |
AKASHI Takahiro | 7b06192 | 2023-11-21 10:29:44 +0900 | [diff] [blame] | 13 | #include <exports.h> |
Heinrich Schuchardt | bb38a1f | 2020-07-17 20:21:00 +0200 | [diff] [blame] | 14 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 15 | #include <malloc.h> |
Alexander Graf | a241451 | 2018-06-18 17:22:58 +0200 | [diff] [blame] | 16 | #include <mapmem.h> |
AKASHI Takahiro | 7b06192 | 2023-11-21 10:29:44 +0900 | [diff] [blame] | 17 | #include <vsprintf.h> |
Simon Glass | c2194bf | 2016-09-25 15:27:32 -0600 | [diff] [blame] | 18 | #include <asm-generic/sections.h> |
AKASHI Takahiro | 7b06192 | 2023-11-21 10:29:44 +0900 | [diff] [blame] | 19 | #include <asm/global_data.h> |
| 20 | #include <linux/string.h> |
Alexander Graf | 2ff5cc3 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 21 | |
| 22 | DECLARE_GLOBAL_DATA_PTR; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 23 | |
AKASHI Takahiro | 203d196 | 2023-11-21 10:29:43 +0900 | [diff] [blame] | 24 | static struct efi_device_path *test_image_path; |
| 25 | static struct efi_device_path *test_device_path; |
AKASHI Takahiro | d52dacb | 2023-11-21 10:29:42 +0900 | [diff] [blame] | 26 | |
AKASHI Takahiro | 09a81c7 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 27 | static 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 Schuchardt | e626395 | 2020-01-03 22:53:42 +0100 | [diff] [blame] | 34 | u16 *load_options; |
AKASHI Takahiro | 09a81c7 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 35 | |
| 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 Schuchardt | f0b4423 | 2025-04-11 07:36:45 +0200 | [diff] [blame] | 41 | (*image_objp)->auth_status = EFI_IMAGE_AUTH_PASSED; |
| 42 | (*image_objp)->entry = efi_selftest; |
| 43 | |
AKASHI Takahiro | 09a81c7 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 44 | /* Transfer environment variable as load options */ |
Heinrich Schuchardt | 25c6be5 | 2020-08-07 17:47:13 +0200 | [diff] [blame] | 45 | return efi_env_set_load_options((efi_handle_t)*image_objp, |
| 46 | load_options_path, |
| 47 | &load_options); |
AKASHI Takahiro | 09a81c7 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 48 | } |
| 49 | |
Simon Glass | 93f2559 | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 50 | /** |
| 51 | * bootefi_test_prepare() - prepare to run an EFI test |
| 52 | * |
Heinrich Schuchardt | fd9cbe3 | 2019-01-12 14:42:40 +0100 | [diff] [blame] | 53 | * Prepare to run a test as if it were provided by a loaded image. |
Simon Glass | 93f2559 | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 54 | * |
Heinrich Schuchardt | fd9cbe3 | 2019-01-12 14:42:40 +0100 | [diff] [blame] | 55 | * @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 Glass | 93f2559 | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 62 | */ |
| 63 | static efi_status_t bootefi_test_prepare |
| 64 | (struct efi_loaded_image_obj **image_objp, |
Heinrich Schuchardt | fd9cbe3 | 2019-01-12 14:42:40 +0100 | [diff] [blame] | 65 | struct efi_loaded_image **loaded_image_infop, const char *path, |
| 66 | const char *load_options_path) |
Simon Glass | 93f2559 | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 67 | { |
Heinrich Schuchardt | fd9cbe3 | 2019-01-12 14:42:40 +0100 | [diff] [blame] | 68 | efi_status_t ret; |
| 69 | |
Simon Glass | 93f2559 | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 70 | /* Construct a dummy device path */ |
AKASHI Takahiro | 203d196 | 2023-11-21 10:29:43 +0900 | [diff] [blame] | 71 | test_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, 0, 0); |
| 72 | if (!test_device_path) |
Simon Glass | 93f2559 | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 73 | return EFI_OUT_OF_RESOURCES; |
Heinrich Schuchardt | fd9cbe3 | 2019-01-12 14:42:40 +0100 | [diff] [blame] | 74 | |
AKASHI Takahiro | 203d196 | 2023-11-21 10:29:43 +0900 | [diff] [blame] | 75 | test_image_path = efi_dp_from_file(NULL, path); |
| 76 | if (!test_image_path) { |
Heinrich Schuchardt | fd9cbe3 | 2019-01-12 14:42:40 +0100 | [diff] [blame] | 77 | ret = EFI_OUT_OF_RESOURCES; |
| 78 | goto failure; |
| 79 | } |
| 80 | |
AKASHI Takahiro | 203d196 | 2023-11-21 10:29:43 +0900 | [diff] [blame] | 81 | ret = bootefi_run_prepare(load_options_path, test_device_path, |
| 82 | test_image_path, image_objp, |
Heinrich Schuchardt | fd9cbe3 | 2019-01-12 14:42:40 +0100 | [diff] [blame] | 83 | loaded_image_infop); |
| 84 | if (ret == EFI_SUCCESS) |
| 85 | return ret; |
Simon Glass | 93f2559 | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 86 | |
Heinrich Schuchardt | fd9cbe3 | 2019-01-12 14:42:40 +0100 | [diff] [blame] | 87 | failure: |
AKASHI Takahiro | 203d196 | 2023-11-21 10:29:43 +0900 | [diff] [blame] | 88 | efi_free_pool(test_device_path); |
| 89 | efi_free_pool(test_image_path); |
| 90 | /* TODO: not sure calling clear function is necessary */ |
Heinrich Schuchardt | 6b82159 | 2021-01-12 12:46:24 +0100 | [diff] [blame] | 91 | efi_clear_bootdev(); |
Heinrich Schuchardt | fd9cbe3 | 2019-01-12 14:42:40 +0100 | [diff] [blame] | 92 | return ret; |
Simon Glass | 93f2559 | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 93 | } |
| 94 | |
AKASHI Takahiro | 09a81c7 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 95 | /** |
Heinrich Schuchardt | 8fc56c6 | 2019-05-12 20:16:25 +0200 | [diff] [blame] | 96 | * do_efi_selftest() - execute EFI selftest |
AKASHI Takahiro | 09a81c7 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 97 | * |
AKASHI Takahiro | 09a81c7 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 98 | * Return: status code |
AKASHI Takahiro | 09a81c7 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 99 | */ |
Heinrich Schuchardt | 8fc56c6 | 2019-05-12 20:16:25 +0200 | [diff] [blame] | 100 | static int do_efi_selftest(void) |
AKASHI Takahiro | 09a81c7 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 101 | { |
| 102 | struct efi_loaded_image_obj *image_obj; |
| 103 | struct efi_loaded_image *loaded_image_info; |
| 104 | efi_status_t ret; |
| 105 | |
AKASHI Takahiro | 09a81c7 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 106 | 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 Schuchardt | f0b4423 | 2025-04-11 07:36:45 +0200 | [diff] [blame] | 112 | ret = do_bootefi_exec(&image_obj->header, |
| 113 | loaded_image_info->load_options); |
AKASHI Takahiro | 203d196 | 2023-11-21 10:29:43 +0900 | [diff] [blame] | 114 | efi_free_pool(test_device_path); |
| 115 | efi_free_pool(test_image_path); |
Ilias Apalodimas | b09ecf1 | 2023-07-24 13:17:36 +0300 | [diff] [blame] | 116 | if (ret != EFI_SUCCESS) |
| 117 | efi_delete_handle(&image_obj->header); |
| 118 | else |
| 119 | ret = efi_delete_handle(&image_obj->header); |
AKASHI Takahiro | 09a81c7 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 120 | |
| 121 | return ret != EFI_SUCCESS; |
| 122 | } |
Simon Glass | 93f2559 | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 123 | |
Heinrich Schuchardt | 8fc56c6 | 2019-05-12 20:16:25 +0200 | [diff] [blame] | 124 | /** |
| 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 Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 133 | static int do_bootefi(struct cmd_tbl *cmdtp, int flag, int argc, |
| 134 | char *const argv[]) |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 135 | { |
Heinrich Schuchardt | 8fc56c6 | 2019-05-12 20:16:25 +0200 | [diff] [blame] | 136 | efi_status_t ret; |
AKASHI Takahiro | 28cb4a8 | 2023-11-21 10:29:39 +0900 | [diff] [blame] | 137 | char *p; |
| 138 | void *fdt, *image_buf; |
| 139 | unsigned long addr, size; |
AKASHI Takahiro | 7b06192 | 2023-11-21 10:29:44 +0900 | [diff] [blame] | 140 | void *image_addr; |
| 141 | size_t image_size; |
Heinrich Schuchardt | 8fc56c6 | 2019-05-12 20:16:25 +0200 | [diff] [blame] | 142 | |
AKASHI Takahiro | 09a81c7 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 143 | if (argc < 2) |
| 144 | return CMD_RET_USAGE; |
AKASHI Takahiro | cf81983 | 2019-04-19 12:22:33 +0900 | [diff] [blame] | 145 | |
Heinrich Schuchardt | 3ea20e7 | 2022-05-19 08:00:56 +0200 | [diff] [blame] | 146 | if (argc > 2) { |
Heinrich Schuchardt | 6246741 | 2019-12-08 01:07:01 +0100 | [diff] [blame] | 147 | uintptr_t fdt_addr; |
| 148 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 149 | fdt_addr = hextoul(argv[2], NULL); |
Heinrich Schuchardt | 6246741 | 2019-12-08 01:07:01 +0100 | [diff] [blame] | 150 | fdt = map_sysmem(fdt_addr, 0); |
| 151 | } else { |
| 152 | fdt = EFI_FDT_USE_INTERNAL; |
| 153 | } |
AKASHI Takahiro | 6a6858e | 2023-11-21 10:29:40 +0900 | [diff] [blame] | 154 | |
| 155 | if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR) && |
| 156 | !strcmp(argv[1], "bootmgr")) { |
AKASHI Takahiro | 4ab6c5b | 2023-11-21 10:29:41 +0900 | [diff] [blame] | 157 | ret = efi_bootmgr_run(fdt); |
Heinrich Schuchardt | 8fc56c6 | 2019-05-12 20:16:25 +0200 | [diff] [blame] | 158 | |
Heinrich Schuchardt | 985482d | 2024-03-16 10:36:43 +0100 | [diff] [blame] | 159 | if (ret != EFI_SUCCESS) |
AKASHI Takahiro | 6a6858e | 2023-11-21 10:29:40 +0900 | [diff] [blame] | 160 | return CMD_RET_FAILURE; |
| 161 | |
AKASHI Takahiro | 4ab6c5b | 2023-11-21 10:29:41 +0900 | [diff] [blame] | 162 | return CMD_RET_SUCCESS; |
Heinrich Schuchardt | b2625e8 | 2021-01-15 19:02:50 +0100 | [diff] [blame] | 163 | } |
AKASHI Takahiro | 6a6858e | 2023-11-21 10:29:40 +0900 | [diff] [blame] | 164 | |
| 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 Schuchardt | 42f9979 | 2024-03-16 10:36:44 +0100 | [diff] [blame] | 176 | if (ret != EFI_SUCCESS) |
AKASHI Takahiro | 6a6858e | 2023-11-21 10:29:40 +0900 | [diff] [blame] | 177 | return CMD_RET_FAILURE; |
| 178 | |
Heinrich Schuchardt | 8fc56c6 | 2019-05-12 20:16:25 +0200 | [diff] [blame] | 179 | return do_efi_selftest(); |
AKASHI Takahiro | 6a6858e | 2023-11-21 10:29:40 +0900 | [diff] [blame] | 180 | } |
AKASHI Takahiro | 28cb4a8 | 2023-11-21 10:29:39 +0900 | [diff] [blame] | 181 | |
AKASHI Takahiro | 6a6858e | 2023-11-21 10:29:40 +0900 | [diff] [blame] | 182 | 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 Takahiro | 28cb4a8 | 2023-11-21 10:29:39 +0900 | [diff] [blame] | 187 | image_buf = __efi_helloworld_begin; |
| 188 | size = __efi_helloworld_end - __efi_helloworld_begin; |
AKASHI Takahiro | 7b06192 | 2023-11-21 10:29:44 +0900 | [diff] [blame] | 189 | /* TODO: not sure calling clear function is necessary */ |
AKASHI Takahiro | 28cb4a8 | 2023-11-21 10:29:39 +0900 | [diff] [blame] | 190 | efi_clear_bootdev(); |
AKASHI Takahiro | 6a6858e | 2023-11-21 10:29:40 +0900 | [diff] [blame] | 191 | } else { |
AKASHI Takahiro | 28cb4a8 | 2023-11-21 10:29:39 +0900 | [diff] [blame] | 192 | 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 Takahiro | 7b06192 | 2023-11-21 10:29:44 +0900 | [diff] [blame] | 205 | /* Image should be already loaded */ |
| 206 | efi_get_image_parameters(&image_addr, &image_size); |
| 207 | |
AKASHI Takahiro | 28cb4a8 | 2023-11-21 10:29:39 +0900 | [diff] [blame] | 208 | 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 Schuchardt | 3ea20e7 | 2022-05-19 08:00:56 +0200 | [diff] [blame] | 215 | } |
AKASHI Takahiro | 6a6858e | 2023-11-21 10:29:40 +0900 | [diff] [blame] | 216 | |
Adriano Cordova | cea09ba | 2025-03-19 11:45:00 -0300 | [diff] [blame] | 217 | ret = efi_binary_run(image_buf, size, fdt, NULL, 0); |
AKASHI Takahiro | 6a6858e | 2023-11-21 10:29:40 +0900 | [diff] [blame] | 218 | |
Heinrich Schuchardt | 985482d | 2024-03-16 10:36:43 +0100 | [diff] [blame] | 219 | if (ret != EFI_SUCCESS) |
AKASHI Takahiro | 28cb4a8 | 2023-11-21 10:29:39 +0900 | [diff] [blame] | 220 | return CMD_RET_FAILURE; |
| 221 | |
| 222 | return CMD_RET_SUCCESS; |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 223 | } |
| 224 | |
Tom Rini | 03f146c | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 225 | U_BOOT_LONGHELP(bootefi, |
Heinrich Schuchardt | 3ea20e7 | 2022-05-19 08:00:56 +0200 | [diff] [blame] | 226 | "<image address>[:<image size>] [<fdt address>]\n" |
| 227 | " - boot EFI payload\n" |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 228 | #ifdef CONFIG_CMD_BOOTEFI_HELLO |
Heinrich Schuchardt | d33ae3e | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 229 | "bootefi hello\n" |
| 230 | " - boot a sample Hello World application stored within U-Boot\n" |
| 231 | #endif |
| 232 | #ifdef CONFIG_CMD_BOOTEFI_SELFTEST |
Heinrich Schuchardt | 44ab21b | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 233 | "bootefi selftest [fdt address]\n" |
Heinrich Schuchardt | d33ae3e | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 234 | " - boot an EFI selftest application stored within U-Boot\n" |
Heinrich Schuchardt | 02efd5d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 235 | " Use environment variable efi_selftest to select a single test.\n" |
| 236 | " Use 'setenv efi_selftest list' to enumerate all tests.\n" |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 237 | #endif |
Heinrich Schuchardt | b2625e8 | 2021-01-15 19:02:50 +0100 | [diff] [blame] | 238 | #ifdef CONFIG_CMD_BOOTEFI_BOOTMGR |
AKASHI Takahiro | 14ff23b | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 239 | "bootefi bootmgr [fdt address]\n" |
Rob Clark | c84c110 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 240 | " - 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 Schuchardt | b2625e8 | 2021-01-15 19:02:50 +0100 | [diff] [blame] | 243 | " exposed as EFI configuration table.\n" |
| 244 | #endif |
Tom Rini | 03f146c | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 245 | ); |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 246 | |
| 247 | U_BOOT_CMD( |
Kyle Evans | 2c3bf4e | 2022-04-10 16:05:55 -0500 | [diff] [blame] | 248 | bootefi, 4, 0, do_bootefi, |
Sergey Kubushyn | 268f19e | 2016-06-07 11:14:31 -0700 | [diff] [blame] | 249 | "Boots an EFI payload from memory", |
Alexander Graf | e2b04f2 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 250 | bootefi_help_text |
| 251 | ); |