Tom Rini | f739fcd | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexander Graf | b993933 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 2 | /* |
| 3 | * EFI application loader |
| 4 | * |
| 5 | * Copyright (c) 2016 Alexander Graf |
Alexander Graf | b993933 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Heinrich Schuchardt | c001837 | 2020-07-17 20:21:00 +0200 | [diff] [blame] | 8 | #define LOG_CATEGORY LOGC_EFI |
| 9 | |
Alexander Graf | b993933 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 10 | #include <command.h> |
AKASHI Takahiro | 0bef4b0 | 2023-11-21 10:29:44 +0900 | [diff] [blame] | 11 | #include <efi.h> |
Alexander Graf | b993933 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 12 | #include <efi_loader.h> |
AKASHI Takahiro | 0bef4b0 | 2023-11-21 10:29:44 +0900 | [diff] [blame] | 13 | #include <exports.h> |
Heinrich Schuchardt | c001837 | 2020-07-17 20:21:00 +0200 | [diff] [blame] | 14 | #include <log.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 15 | #include <malloc.h> |
Alexander Graf | 354264b | 2018-06-18 17:22:58 +0200 | [diff] [blame] | 16 | #include <mapmem.h> |
AKASHI Takahiro | 0bef4b0 | 2023-11-21 10:29:44 +0900 | [diff] [blame] | 17 | #include <vsprintf.h> |
Simon Glass | e275458 | 2016-09-25 15:27:32 -0600 | [diff] [blame] | 18 | #include <asm-generic/sections.h> |
AKASHI Takahiro | 0bef4b0 | 2023-11-21 10:29:44 +0900 | [diff] [blame] | 19 | #include <asm/global_data.h> |
| 20 | #include <linux/string.h> |
Alexander Graf | 0d9d501 | 2016-04-11 16:55:26 +0200 | [diff] [blame] | 21 | |
| 22 | DECLARE_GLOBAL_DATA_PTR; |
Alexander Graf | b993933 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 23 | |
AKASHI Takahiro | d07e7be | 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 | 5c129fe | 2023-11-21 10:29:42 +0900 | [diff] [blame] | 26 | |
AKASHI Takahiro | 3fc2b16 | 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 | a3850e4 | 2020-01-03 22:53:42 +0100 | [diff] [blame] | 34 | u16 *load_options; |
AKASHI Takahiro | 3fc2b16 | 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 | |
| 41 | /* Transfer environment variable as load options */ |
Heinrich Schuchardt | 1064d04 | 2020-08-07 17:47:13 +0200 | [diff] [blame] | 42 | return efi_env_set_load_options((efi_handle_t)*image_objp, |
| 43 | load_options_path, |
| 44 | &load_options); |
AKASHI Takahiro | 3fc2b16 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 45 | } |
| 46 | |
Simon Glass | d9717ea | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 47 | /** |
| 48 | * bootefi_test_prepare() - prepare to run an EFI test |
| 49 | * |
Heinrich Schuchardt | 1504bb0 | 2019-01-12 14:42:40 +0100 | [diff] [blame] | 50 | * Prepare to run a test as if it were provided by a loaded image. |
Simon Glass | d9717ea | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 51 | * |
Heinrich Schuchardt | 1504bb0 | 2019-01-12 14:42:40 +0100 | [diff] [blame] | 52 | * @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 Glass | d9717ea | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 59 | */ |
| 60 | static efi_status_t bootefi_test_prepare |
| 61 | (struct efi_loaded_image_obj **image_objp, |
Heinrich Schuchardt | 1504bb0 | 2019-01-12 14:42:40 +0100 | [diff] [blame] | 62 | struct efi_loaded_image **loaded_image_infop, const char *path, |
| 63 | const char *load_options_path) |
Simon Glass | d9717ea | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 64 | { |
Heinrich Schuchardt | 1504bb0 | 2019-01-12 14:42:40 +0100 | [diff] [blame] | 65 | efi_status_t ret; |
| 66 | |
Simon Glass | d9717ea | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 67 | /* Construct a dummy device path */ |
AKASHI Takahiro | d07e7be | 2023-11-21 10:29:43 +0900 | [diff] [blame] | 68 | test_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, 0, 0); |
| 69 | if (!test_device_path) |
Simon Glass | d9717ea | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 70 | return EFI_OUT_OF_RESOURCES; |
Simon Glass | d9717ea | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 71 | |
AKASHI Takahiro | d07e7be | 2023-11-21 10:29:43 +0900 | [diff] [blame] | 72 | test_image_path = efi_dp_from_file(NULL, path); |
| 73 | if (!test_image_path) { |
Heinrich Schuchardt | 1504bb0 | 2019-01-12 14:42:40 +0100 | [diff] [blame] | 74 | ret = EFI_OUT_OF_RESOURCES; |
| 75 | goto failure; |
| 76 | } |
| 77 | |
AKASHI Takahiro | d07e7be | 2023-11-21 10:29:43 +0900 | [diff] [blame] | 78 | ret = bootefi_run_prepare(load_options_path, test_device_path, |
| 79 | test_image_path, image_objp, |
Heinrich Schuchardt | 1504bb0 | 2019-01-12 14:42:40 +0100 | [diff] [blame] | 80 | loaded_image_infop); |
| 81 | if (ret == EFI_SUCCESS) |
| 82 | return ret; |
| 83 | |
Heinrich Schuchardt | 1504bb0 | 2019-01-12 14:42:40 +0100 | [diff] [blame] | 84 | failure: |
AKASHI Takahiro | d07e7be | 2023-11-21 10:29:43 +0900 | [diff] [blame] | 85 | efi_free_pool(test_device_path); |
| 86 | efi_free_pool(test_image_path); |
| 87 | /* TODO: not sure calling clear function is necessary */ |
Heinrich Schuchardt | 5f59518 | 2021-01-12 12:46:24 +0100 | [diff] [blame] | 88 | efi_clear_bootdev(); |
Heinrich Schuchardt | 1504bb0 | 2019-01-12 14:42:40 +0100 | [diff] [blame] | 89 | return ret; |
Simon Glass | d9717ea | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 90 | } |
| 91 | |
AKASHI Takahiro | 3fc2b16 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 92 | /** |
Heinrich Schuchardt | 7e92db8 | 2019-05-12 20:16:25 +0200 | [diff] [blame] | 93 | * do_efi_selftest() - execute EFI selftest |
AKASHI Takahiro | 3fc2b16 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 94 | * |
AKASHI Takahiro | 3fc2b16 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 95 | * Return: status code |
AKASHI Takahiro | 3fc2b16 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 96 | */ |
Heinrich Schuchardt | 7e92db8 | 2019-05-12 20:16:25 +0200 | [diff] [blame] | 97 | static int do_efi_selftest(void) |
AKASHI Takahiro | 3fc2b16 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 98 | { |
| 99 | struct efi_loaded_image_obj *image_obj; |
| 100 | struct efi_loaded_image *loaded_image_info; |
| 101 | efi_status_t ret; |
| 102 | |
AKASHI Takahiro | 3fc2b16 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 103 | 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 Apalodimas | 54edc37 | 2023-07-24 13:17:36 +0300 | [diff] [blame] | 110 | efi_restore_gd(); |
| 111 | free(loaded_image_info->load_options); |
AKASHI Takahiro | d07e7be | 2023-11-21 10:29:43 +0900 | [diff] [blame] | 112 | efi_free_pool(test_device_path); |
| 113 | efi_free_pool(test_image_path); |
Ilias Apalodimas | 54edc37 | 2023-07-24 13:17:36 +0300 | [diff] [blame] | 114 | if (ret != EFI_SUCCESS) |
| 115 | efi_delete_handle(&image_obj->header); |
| 116 | else |
| 117 | ret = efi_delete_handle(&image_obj->header); |
AKASHI Takahiro | 3fc2b16 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 118 | |
| 119 | return ret != EFI_SUCCESS; |
| 120 | } |
Simon Glass | d9717ea | 2018-11-25 20:14:37 -0700 | [diff] [blame] | 121 | |
Heinrich Schuchardt | 7e92db8 | 2019-05-12 20:16:25 +0200 | [diff] [blame] | 122 | /** |
| 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 Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 131 | static int do_bootefi(struct cmd_tbl *cmdtp, int flag, int argc, |
| 132 | char *const argv[]) |
Alexander Graf | b993933 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 133 | { |
Heinrich Schuchardt | 7e92db8 | 2019-05-12 20:16:25 +0200 | [diff] [blame] | 134 | efi_status_t ret; |
AKASHI Takahiro | 05e2cad | 2023-11-21 10:29:39 +0900 | [diff] [blame] | 135 | char *p; |
| 136 | void *fdt, *image_buf; |
| 137 | unsigned long addr, size; |
AKASHI Takahiro | 0bef4b0 | 2023-11-21 10:29:44 +0900 | [diff] [blame] | 138 | void *image_addr; |
| 139 | size_t image_size; |
Heinrich Schuchardt | 7e92db8 | 2019-05-12 20:16:25 +0200 | [diff] [blame] | 140 | |
AKASHI Takahiro | 3fc2b16 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 141 | if (argc < 2) |
| 142 | return CMD_RET_USAGE; |
AKASHI Takahiro | d6b2189 | 2019-04-19 12:22:33 +0900 | [diff] [blame] | 143 | |
Heinrich Schuchardt | 8131c85 | 2022-05-19 08:00:56 +0200 | [diff] [blame] | 144 | if (argc > 2) { |
Heinrich Schuchardt | f64f223 | 2019-12-08 01:07:01 +0100 | [diff] [blame] | 145 | uintptr_t fdt_addr; |
| 146 | |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 147 | fdt_addr = hextoul(argv[2], NULL); |
Heinrich Schuchardt | f64f223 | 2019-12-08 01:07:01 +0100 | [diff] [blame] | 148 | fdt = map_sysmem(fdt_addr, 0); |
| 149 | } else { |
| 150 | fdt = EFI_FDT_USE_INTERNAL; |
| 151 | } |
Heinrich Schuchardt | 7e92db8 | 2019-05-12 20:16:25 +0200 | [diff] [blame] | 152 | |
AKASHI Takahiro | 296faf4 | 2023-11-21 10:29:40 +0900 | [diff] [blame] | 153 | if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR) && |
| 154 | !strcmp(argv[1], "bootmgr")) { |
AKASHI Takahiro | c3530ae | 2023-11-21 10:29:41 +0900 | [diff] [blame] | 155 | ret = efi_bootmgr_run(fdt); |
AKASHI Takahiro | 296faf4 | 2023-11-21 10:29:40 +0900 | [diff] [blame] | 156 | |
AKASHI Takahiro | 296faf4 | 2023-11-21 10:29:40 +0900 | [diff] [blame] | 157 | if (ret == EFI_INVALID_PARAMETER) |
| 158 | return CMD_RET_USAGE; |
AKASHI Takahiro | c3530ae | 2023-11-21 10:29:41 +0900 | [diff] [blame] | 159 | else if (ret) |
AKASHI Takahiro | 296faf4 | 2023-11-21 10:29:40 +0900 | [diff] [blame] | 160 | return CMD_RET_FAILURE; |
| 161 | |
AKASHI Takahiro | c3530ae | 2023-11-21 10:29:41 +0900 | [diff] [blame] | 162 | return CMD_RET_SUCCESS; |
Heinrich Schuchardt | ff2f532 | 2021-01-15 19:02:50 +0100 | [diff] [blame] | 163 | } |
AKASHI Takahiro | 3fc2b16 | 2019-04-19 12:22:31 +0900 | [diff] [blame] | 164 | |
AKASHI Takahiro | 296faf4 | 2023-11-21 10:29:40 +0900 | [diff] [blame] | 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); |
| 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 Takahiro | 05e2cad | 2023-11-21 10:29:39 +0900 | [diff] [blame] | 189 | image_buf = __efi_helloworld_begin; |
| 190 | size = __efi_helloworld_end - __efi_helloworld_begin; |
AKASHI Takahiro | 0bef4b0 | 2023-11-21 10:29:44 +0900 | [diff] [blame] | 191 | /* TODO: not sure calling clear function is necessary */ |
AKASHI Takahiro | 05e2cad | 2023-11-21 10:29:39 +0900 | [diff] [blame] | 192 | efi_clear_bootdev(); |
AKASHI Takahiro | 296faf4 | 2023-11-21 10:29:40 +0900 | [diff] [blame] | 193 | } else { |
AKASHI Takahiro | 05e2cad | 2023-11-21 10:29:39 +0900 | [diff] [blame] | 194 | 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 Takahiro | 0bef4b0 | 2023-11-21 10:29:44 +0900 | [diff] [blame] | 207 | /* Image should be already loaded */ |
| 208 | efi_get_image_parameters(&image_addr, &image_size); |
| 209 | |
AKASHI Takahiro | 05e2cad | 2023-11-21 10:29:39 +0900 | [diff] [blame] | 210 | 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 Takahiro | 296faf4 | 2023-11-21 10:29:40 +0900 | [diff] [blame] | 218 | |
AKASHI Takahiro | 5c129fe | 2023-11-21 10:29:42 +0900 | [diff] [blame] | 219 | ret = efi_binary_run(image_buf, size, fdt); |
AKASHI Takahiro | 296faf4 | 2023-11-21 10:29:40 +0900 | [diff] [blame] | 220 | |
AKASHI Takahiro | 296faf4 | 2023-11-21 10:29:40 +0900 | [diff] [blame] | 221 | if (ret == EFI_INVALID_PARAMETER) |
| 222 | return CMD_RET_USAGE; |
AKASHI Takahiro | 5c129fe | 2023-11-21 10:29:42 +0900 | [diff] [blame] | 223 | else if (ret) |
AKASHI Takahiro | 05e2cad | 2023-11-21 10:29:39 +0900 | [diff] [blame] | 224 | return CMD_RET_FAILURE; |
| 225 | |
| 226 | return CMD_RET_SUCCESS; |
Alexander Graf | b993933 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 227 | } |
| 228 | |
Tom Rini | 3616218 | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 229 | U_BOOT_LONGHELP(bootefi, |
Heinrich Schuchardt | 8131c85 | 2022-05-19 08:00:56 +0200 | [diff] [blame] | 230 | "<image address>[:<image size>] [<fdt address>]\n" |
| 231 | " - boot EFI payload\n" |
Simon Glass | c7ae3df | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 232 | #ifdef CONFIG_CMD_BOOTEFI_HELLO |
Heinrich Schuchardt | 623b3a5 | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 233 | "bootefi hello\n" |
| 234 | " - boot a sample Hello World application stored within U-Boot\n" |
| 235 | #endif |
| 236 | #ifdef CONFIG_CMD_BOOTEFI_SELFTEST |
Heinrich Schuchardt | bc4f913 | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 237 | "bootefi selftest [fdt address]\n" |
Heinrich Schuchardt | 623b3a5 | 2017-09-15 10:06:11 +0200 | [diff] [blame] | 238 | " - boot an EFI selftest application stored within U-Boot\n" |
Heinrich Schuchardt | d78e40d | 2017-10-18 18:13:13 +0200 | [diff] [blame] | 239 | " Use environment variable efi_selftest to select a single test.\n" |
| 240 | " Use 'setenv efi_selftest list' to enumerate all tests.\n" |
Simon Glass | c7ae3df | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 241 | #endif |
Heinrich Schuchardt | ff2f532 | 2021-01-15 19:02:50 +0100 | [diff] [blame] | 242 | #ifdef CONFIG_CMD_BOOTEFI_BOOTMGR |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 243 | "bootefi bootmgr [fdt address]\n" |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 244 | " - 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 Schuchardt | ff2f532 | 2021-01-15 19:02:50 +0100 | [diff] [blame] | 247 | " exposed as EFI configuration table.\n" |
| 248 | #endif |
Tom Rini | 3616218 | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 249 | ); |
Alexander Graf | b993933 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 250 | |
| 251 | U_BOOT_CMD( |
Kyle Evans | 2058983 | 2022-04-10 16:05:55 -0500 | [diff] [blame] | 252 | bootefi, 4, 0, do_bootefi, |
Sergey Kubushyn | 92dfd92 | 2016-06-07 11:14:31 -0700 | [diff] [blame] | 253 | "Boots an EFI payload from memory", |
Alexander Graf | b993933 | 2016-03-10 00:27:20 +0100 | [diff] [blame] | 254 | bootefi_help_text |
| 255 | ); |