AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * UEFI Shell-like command |
| 4 | * |
| 5 | * Copyright (c) 2018 AKASHI Takahiro, Linaro Limited |
| 6 | */ |
| 7 | |
| 8 | #include <charset.h> |
| 9 | #include <common.h> |
| 10 | #include <command.h> |
Heinrich Schuchardt | d16463a | 2022-10-04 15:31:17 +0200 | [diff] [blame] | 11 | #include <dm/device.h> |
Heinrich Schuchardt | 2c1e224 | 2020-12-13 10:30:24 +0100 | [diff] [blame] | 12 | #include <efi_dt_fixup.h> |
Ilias Apalodimas | 773c090 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 13 | #include <efi_load_initrd.h> |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 14 | #include <efi_loader.h> |
Heinrich Schuchardt | 50adeff | 2020-09-27 14:50:25 +0200 | [diff] [blame] | 15 | #include <efi_rng.h> |
Heinrich Schuchardt | e8324f5 | 2021-05-24 11:10:59 +0200 | [diff] [blame] | 16 | #include <efi_variable.h> |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 17 | #include <exports.h> |
Heinrich Schuchardt | 9df002e | 2019-04-29 13:51:45 +0200 | [diff] [blame] | 18 | #include <hexdump.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 19 | #include <log.h> |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 20 | #include <malloc.h> |
Heinrich Schuchardt | fec10a4 | 2020-03-14 08:44:07 +0100 | [diff] [blame] | 21 | #include <mapmem.h> |
Heinrich Schuchardt | 2639762 | 2021-02-02 17:53:14 +0100 | [diff] [blame] | 22 | #include <part.h> |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 23 | #include <search.h> |
| 24 | #include <linux/ctype.h> |
Ilias Apalodimas | 773c090 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 25 | #include <linux/err.h> |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 26 | |
AKASHI Takahiro | e32788a | 2019-02-25 15:54:39 +0900 | [diff] [blame] | 27 | #define BS systab.boottime |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 28 | |
AKASHI Takahiro | 38833c8 | 2020-11-17 09:28:01 +0900 | [diff] [blame] | 29 | #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT |
| 30 | /** |
| 31 | * do_efi_capsule_update() - process a capsule update |
| 32 | * |
| 33 | * @cmdtp: Command table |
| 34 | * @flag: Command flag |
| 35 | * @argc: Number of arguments |
| 36 | * @argv: Argument array |
| 37 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 38 | * |
| 39 | * Implement efidebug "capsule update" sub-command. |
| 40 | * process a capsule update. |
| 41 | * |
| 42 | * efidebug capsule update [-v] <capsule address> |
| 43 | */ |
| 44 | static int do_efi_capsule_update(struct cmd_tbl *cmdtp, int flag, |
| 45 | int argc, char * const argv[]) |
| 46 | { |
| 47 | struct efi_capsule_header *capsule; |
| 48 | int verbose = 0; |
| 49 | char *endp; |
| 50 | efi_status_t ret; |
| 51 | |
| 52 | if (argc != 2 && argc != 3) |
| 53 | return CMD_RET_USAGE; |
| 54 | |
| 55 | if (argc == 3) { |
| 56 | if (strcmp(argv[1], "-v")) |
| 57 | return CMD_RET_USAGE; |
| 58 | |
| 59 | verbose = 1; |
| 60 | argc--; |
| 61 | argv++; |
| 62 | } |
| 63 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 64 | capsule = (typeof(capsule))hextoul(argv[1], &endp); |
AKASHI Takahiro | 38833c8 | 2020-11-17 09:28:01 +0900 | [diff] [blame] | 65 | if (endp == argv[1]) { |
| 66 | printf("Invalid address: %s", argv[1]); |
| 67 | return CMD_RET_FAILURE; |
| 68 | } |
| 69 | |
| 70 | if (verbose) { |
| 71 | printf("Capsule guid: %pUl\n", &capsule->capsule_guid); |
| 72 | printf("Capsule flags: 0x%x\n", capsule->flags); |
| 73 | printf("Capsule header size: 0x%x\n", capsule->header_size); |
| 74 | printf("Capsule image size: 0x%x\n", |
| 75 | capsule->capsule_image_size); |
| 76 | } |
| 77 | |
Heinrich Schuchardt | f312087 | 2022-10-15 13:21:01 +0200 | [diff] [blame] | 78 | ret = EFI_CALL(efi_update_capsule(&capsule, 1, 0)); |
AKASHI Takahiro | 38833c8 | 2020-11-17 09:28:01 +0900 | [diff] [blame] | 79 | if (ret) { |
Michal Simek | ecb7ce5 | 2022-08-09 16:31:12 +0200 | [diff] [blame] | 80 | printf("Cannot handle a capsule at %p\n", capsule); |
AKASHI Takahiro | 38833c8 | 2020-11-17 09:28:01 +0900 | [diff] [blame] | 81 | return CMD_RET_FAILURE; |
| 82 | } |
| 83 | |
| 84 | return CMD_RET_SUCCESS; |
| 85 | } |
| 86 | |
Masami Hiramatsu | fc1f85e | 2022-03-21 22:37:35 +0900 | [diff] [blame] | 87 | #ifdef CONFIG_EFI_CAPSULE_ON_DISK |
Sughosh Ganu | e6f9efc | 2020-12-30 19:27:11 +0530 | [diff] [blame] | 88 | static int do_efi_capsule_on_disk_update(struct cmd_tbl *cmdtp, int flag, |
| 89 | int argc, char * const argv[]) |
| 90 | { |
| 91 | efi_status_t ret; |
| 92 | |
| 93 | ret = efi_launch_capsules(); |
| 94 | |
| 95 | return ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE; |
| 96 | } |
Masami Hiramatsu | fc1f85e | 2022-03-21 22:37:35 +0900 | [diff] [blame] | 97 | #endif |
Sughosh Ganu | e6f9efc | 2020-12-30 19:27:11 +0530 | [diff] [blame] | 98 | |
AKASHI Takahiro | 38833c8 | 2020-11-17 09:28:01 +0900 | [diff] [blame] | 99 | /** |
| 100 | * do_efi_capsule_show() - show capsule information |
| 101 | * |
| 102 | * @cmdtp: Command table |
| 103 | * @flag: Command flag |
| 104 | * @argc: Number of arguments |
| 105 | * @argv: Argument array |
| 106 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 107 | * |
| 108 | * Implement efidebug "capsule show" sub-command. |
| 109 | * show capsule information. |
| 110 | * |
| 111 | * efidebug capsule show <capsule address> |
| 112 | */ |
| 113 | static int do_efi_capsule_show(struct cmd_tbl *cmdtp, int flag, |
| 114 | int argc, char * const argv[]) |
| 115 | { |
| 116 | struct efi_capsule_header *capsule; |
| 117 | char *endp; |
| 118 | |
| 119 | if (argc != 2) |
| 120 | return CMD_RET_USAGE; |
| 121 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 122 | capsule = (typeof(capsule))hextoul(argv[1], &endp); |
AKASHI Takahiro | 38833c8 | 2020-11-17 09:28:01 +0900 | [diff] [blame] | 123 | if (endp == argv[1]) { |
| 124 | printf("Invalid address: %s", argv[1]); |
| 125 | return CMD_RET_FAILURE; |
| 126 | } |
| 127 | |
| 128 | printf("Capsule guid: %pUl\n", &capsule->capsule_guid); |
| 129 | printf("Capsule flags: 0x%x\n", capsule->flags); |
| 130 | printf("Capsule header size: 0x%x\n", capsule->header_size); |
| 131 | printf("Capsule image size: 0x%x\n", |
| 132 | capsule->capsule_image_size); |
| 133 | |
| 134 | return CMD_RET_SUCCESS; |
| 135 | } |
| 136 | |
Jose Marinho | b027e79 | 2021-03-11 13:18:51 +0000 | [diff] [blame] | 137 | #ifdef CONFIG_EFI_ESRT |
| 138 | |
| 139 | #define EFI_ESRT_FW_TYPE_NUM 4 |
| 140 | char *efi_fw_type_str[EFI_ESRT_FW_TYPE_NUM] = {"unknown", "system FW", "device FW", |
| 141 | "UEFI driver"}; |
| 142 | |
| 143 | #define EFI_ESRT_UPDATE_STATUS_NUM 9 |
| 144 | char *efi_update_status_str[EFI_ESRT_UPDATE_STATUS_NUM] = {"success", "unsuccessful", |
| 145 | "insufficient resources", "incorrect version", "invalid format", |
| 146 | "auth error", "power event (AC)", "power event (batt)", |
| 147 | "unsatisfied dependencies"}; |
| 148 | |
| 149 | #define EFI_FW_TYPE_STR_GET(idx) (\ |
| 150 | EFI_ESRT_FW_TYPE_NUM > (idx) ? efi_fw_type_str[(idx)] : "error"\ |
| 151 | ) |
| 152 | |
| 153 | #define EFI_FW_STATUS_STR_GET(idx) (\ |
| 154 | EFI_ESRT_UPDATE_STATUS_NUM > (idx) ? efi_update_status_str[(idx)] : "error"\ |
| 155 | ) |
| 156 | |
| 157 | /** |
| 158 | * do_efi_capsule_esrt() - manage UEFI capsules |
| 159 | * |
| 160 | * @cmdtp: Command table |
| 161 | * @flag: Command flag |
| 162 | * @argc: Number of arguments |
| 163 | * @argv: Argument array |
| 164 | * Return: CMD_RET_SUCCESS on success, |
| 165 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure |
| 166 | * |
| 167 | * Implement efidebug "capsule esrt" sub-command. |
| 168 | * The prints the current ESRT table. |
| 169 | * |
| 170 | * efidebug capsule esrt |
| 171 | */ |
| 172 | static int do_efi_capsule_esrt(struct cmd_tbl *cmdtp, int flag, |
| 173 | int argc, char * const argv[]) |
| 174 | { |
| 175 | struct efi_system_resource_table *esrt = NULL; |
| 176 | |
| 177 | if (argc != 1) |
| 178 | return CMD_RET_USAGE; |
| 179 | |
| 180 | for (int idx = 0; idx < systab.nr_tables; idx++) |
| 181 | if (!guidcmp(&efi_esrt_guid, &systab.tables[idx].guid)) |
| 182 | esrt = (struct efi_system_resource_table *)systab.tables[idx].table; |
| 183 | |
| 184 | if (!esrt) { |
| 185 | log_info("ESRT: table not present\n"); |
| 186 | return CMD_RET_SUCCESS; |
| 187 | } |
| 188 | |
| 189 | printf("========================================\n"); |
| 190 | printf("ESRT: fw_resource_count=%d\n", esrt->fw_resource_count); |
| 191 | printf("ESRT: fw_resource_count_max=%d\n", esrt->fw_resource_count_max); |
| 192 | printf("ESRT: fw_resource_version=%lld\n", esrt->fw_resource_version); |
| 193 | |
| 194 | for (int idx = 0; idx < esrt->fw_resource_count; idx++) { |
| 195 | printf("[entry %d]==============================\n", idx); |
| 196 | printf("ESRT: fw_class=%pUL\n", &esrt->entries[idx].fw_class); |
| 197 | printf("ESRT: fw_type=%s\n", EFI_FW_TYPE_STR_GET(esrt->entries[idx].fw_type)); |
| 198 | printf("ESRT: fw_version=%d\n", esrt->entries[idx].fw_version); |
| 199 | printf("ESRT: lowest_supported_fw_version=%d\n", |
| 200 | esrt->entries[idx].lowest_supported_fw_version); |
| 201 | printf("ESRT: capsule_flags=%d\n", |
| 202 | esrt->entries[idx].capsule_flags); |
| 203 | printf("ESRT: last_attempt_version=%d\n", |
| 204 | esrt->entries[idx].last_attempt_version); |
| 205 | printf("ESRT: last_attempt_status=%s\n", |
| 206 | EFI_FW_STATUS_STR_GET(esrt->entries[idx].last_attempt_status)); |
| 207 | } |
| 208 | printf("========================================\n"); |
| 209 | |
| 210 | return CMD_RET_SUCCESS; |
| 211 | } |
| 212 | #endif /* CONFIG_EFI_ESRT */ |
AKASHI Takahiro | 38833c8 | 2020-11-17 09:28:01 +0900 | [diff] [blame] | 213 | /** |
| 214 | * do_efi_capsule_res() - show a capsule update result |
| 215 | * |
| 216 | * @cmdtp: Command table |
| 217 | * @flag: Command flag |
| 218 | * @argc: Number of arguments |
| 219 | * @argv: Argument array |
| 220 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 221 | * |
| 222 | * Implement efidebug "capsule result" sub-command. |
| 223 | * show a capsule update result. |
| 224 | * If result number is not specified, CapsuleLast will be shown. |
| 225 | * |
| 226 | * efidebug capsule result [<capsule result number>] |
| 227 | */ |
| 228 | static int do_efi_capsule_res(struct cmd_tbl *cmdtp, int flag, |
| 229 | int argc, char * const argv[]) |
| 230 | { |
| 231 | int capsule_id; |
| 232 | char *endp; |
Heinrich Schuchardt | 9a5e74e | 2021-05-24 10:57:03 +0200 | [diff] [blame] | 233 | u16 var_name16[12]; |
AKASHI Takahiro | 38833c8 | 2020-11-17 09:28:01 +0900 | [diff] [blame] | 234 | efi_guid_t guid; |
| 235 | struct efi_capsule_result_variable_header *result = NULL; |
| 236 | efi_uintn_t size; |
| 237 | efi_status_t ret; |
| 238 | |
| 239 | if (argc != 1 && argc != 2) |
| 240 | return CMD_RET_USAGE; |
| 241 | |
| 242 | guid = efi_guid_capsule_report; |
| 243 | if (argc == 1) { |
| 244 | size = sizeof(var_name16); |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 245 | ret = efi_get_variable_int(u"CapsuleLast", &guid, NULL, |
Heinrich Schuchardt | e8324f5 | 2021-05-24 11:10:59 +0200 | [diff] [blame] | 246 | &size, var_name16, NULL); |
| 247 | |
AKASHI Takahiro | 38833c8 | 2020-11-17 09:28:01 +0900 | [diff] [blame] | 248 | if (ret != EFI_SUCCESS) { |
| 249 | if (ret == EFI_NOT_FOUND) |
| 250 | printf("CapsuleLast doesn't exist\n"); |
| 251 | else |
| 252 | printf("Failed to get CapsuleLast\n"); |
| 253 | |
| 254 | return CMD_RET_FAILURE; |
| 255 | } |
| 256 | printf("CapsuleLast is %ls\n", var_name16); |
| 257 | } else { |
| 258 | argc--; |
| 259 | argv++; |
| 260 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 261 | capsule_id = hextoul(argv[0], &endp); |
AKASHI Takahiro | 38833c8 | 2020-11-17 09:28:01 +0900 | [diff] [blame] | 262 | if (capsule_id < 0 || capsule_id > 0xffff) |
| 263 | return CMD_RET_USAGE; |
| 264 | |
Heinrich Schuchardt | 9a5e74e | 2021-05-24 10:57:03 +0200 | [diff] [blame] | 265 | efi_create_indexed_name(var_name16, sizeof(var_name16), |
| 266 | "Capsule", capsule_id); |
AKASHI Takahiro | 38833c8 | 2020-11-17 09:28:01 +0900 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | size = 0; |
Heinrich Schuchardt | e8324f5 | 2021-05-24 11:10:59 +0200 | [diff] [blame] | 270 | ret = efi_get_variable_int(var_name16, &guid, NULL, &size, NULL, NULL); |
AKASHI Takahiro | 38833c8 | 2020-11-17 09:28:01 +0900 | [diff] [blame] | 271 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 272 | result = malloc(size); |
AKASHI Takahiro | 1af1673 | 2021-01-22 10:42:48 +0900 | [diff] [blame] | 273 | if (!result) |
| 274 | return CMD_RET_FAILURE; |
Heinrich Schuchardt | e8324f5 | 2021-05-24 11:10:59 +0200 | [diff] [blame] | 275 | ret = efi_get_variable_int(var_name16, &guid, NULL, &size, |
| 276 | result, NULL); |
AKASHI Takahiro | 1af1673 | 2021-01-22 10:42:48 +0900 | [diff] [blame] | 277 | } |
| 278 | if (ret != EFI_SUCCESS) { |
| 279 | free(result); |
| 280 | printf("Failed to get %ls\n", var_name16); |
AKASHI Takahiro | 38833c8 | 2020-11-17 09:28:01 +0900 | [diff] [blame] | 281 | |
AKASHI Takahiro | 1af1673 | 2021-01-22 10:42:48 +0900 | [diff] [blame] | 282 | return CMD_RET_FAILURE; |
AKASHI Takahiro | 38833c8 | 2020-11-17 09:28:01 +0900 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | printf("Result total size: 0x%x\n", result->variable_total_size); |
| 286 | printf("Capsule guid: %pUl\n", &result->capsule_guid); |
| 287 | printf("Time processed: %04d-%02d-%02d %02d:%02d:%02d\n", |
| 288 | result->capsule_processed.year, result->capsule_processed.month, |
| 289 | result->capsule_processed.day, result->capsule_processed.hour, |
| 290 | result->capsule_processed.minute, |
| 291 | result->capsule_processed.second); |
| 292 | printf("Capsule status: 0x%lx\n", result->capsule_status); |
| 293 | |
| 294 | free(result); |
| 295 | |
| 296 | return CMD_RET_SUCCESS; |
| 297 | } |
| 298 | |
| 299 | static struct cmd_tbl cmd_efidebug_capsule_sub[] = { |
| 300 | U_BOOT_CMD_MKENT(update, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_update, |
| 301 | "", ""), |
| 302 | U_BOOT_CMD_MKENT(show, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_show, |
| 303 | "", ""), |
Jose Marinho | b027e79 | 2021-03-11 13:18:51 +0000 | [diff] [blame] | 304 | #ifdef CONFIG_EFI_ESRT |
| 305 | U_BOOT_CMD_MKENT(esrt, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_esrt, |
| 306 | "", ""), |
| 307 | #endif |
Masami Hiramatsu | fc1f85e | 2022-03-21 22:37:35 +0900 | [diff] [blame] | 308 | #ifdef CONFIG_EFI_CAPSULE_ON_DISK |
Sughosh Ganu | e6f9efc | 2020-12-30 19:27:11 +0530 | [diff] [blame] | 309 | U_BOOT_CMD_MKENT(disk-update, 0, 0, do_efi_capsule_on_disk_update, |
| 310 | "", ""), |
Masami Hiramatsu | fc1f85e | 2022-03-21 22:37:35 +0900 | [diff] [blame] | 311 | #endif |
AKASHI Takahiro | 38833c8 | 2020-11-17 09:28:01 +0900 | [diff] [blame] | 312 | U_BOOT_CMD_MKENT(result, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_res, |
| 313 | "", ""), |
| 314 | }; |
| 315 | |
| 316 | /** |
| 317 | * do_efi_capsule() - manage UEFI capsules |
| 318 | * |
| 319 | * @cmdtp: Command table |
| 320 | * @flag: Command flag |
| 321 | * @argc: Number of arguments |
| 322 | * @argv: Argument array |
| 323 | * Return: CMD_RET_SUCCESS on success, |
| 324 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure |
| 325 | * |
| 326 | * Implement efidebug "capsule" sub-command. |
| 327 | */ |
| 328 | static int do_efi_capsule(struct cmd_tbl *cmdtp, int flag, |
| 329 | int argc, char * const argv[]) |
| 330 | { |
| 331 | struct cmd_tbl *cp; |
| 332 | |
| 333 | if (argc < 2) |
| 334 | return CMD_RET_USAGE; |
| 335 | |
| 336 | argc--; argv++; |
| 337 | |
| 338 | cp = find_cmd_tbl(argv[0], cmd_efidebug_capsule_sub, |
| 339 | ARRAY_SIZE(cmd_efidebug_capsule_sub)); |
| 340 | if (!cp) |
| 341 | return CMD_RET_USAGE; |
| 342 | |
| 343 | return cp->cmd(cmdtp, flag, argc, argv); |
| 344 | } |
| 345 | #endif /* CONFIG_EFI_HAVE_CAPSULE_SUPPORT */ |
| 346 | |
AKASHI Takahiro | e32788a | 2019-02-25 15:54:39 +0900 | [diff] [blame] | 347 | #define EFI_HANDLE_WIDTH ((int)sizeof(efi_handle_t) * 2) |
| 348 | |
| 349 | static const char spc[] = " "; |
| 350 | static const char sep[] = "================"; |
| 351 | |
| 352 | /** |
AKASHI Takahiro | 185716a | 2019-02-25 15:54:40 +0900 | [diff] [blame] | 353 | * efi_get_driver_handle_info() - get information of UEFI driver |
| 354 | * |
| 355 | * @handle: Handle of UEFI device |
| 356 | * @driver_name: Driver name |
| 357 | * @image_path: Pointer to text of device path |
| 358 | * Return: 0 on success, -1 on failure |
| 359 | * |
| 360 | * Currently return no useful information as all UEFI drivers are |
| 361 | * built-in.. |
| 362 | */ |
| 363 | static int efi_get_driver_handle_info(efi_handle_t handle, u16 **driver_name, |
| 364 | u16 **image_path) |
| 365 | { |
| 366 | struct efi_handler *handler; |
| 367 | struct efi_loaded_image *image; |
| 368 | efi_status_t ret; |
| 369 | |
| 370 | /* |
| 371 | * driver name |
| 372 | * TODO: support EFI_COMPONENT_NAME2_PROTOCOL |
| 373 | */ |
| 374 | *driver_name = NULL; |
| 375 | |
| 376 | /* image name */ |
| 377 | ret = efi_search_protocol(handle, &efi_guid_loaded_image, &handler); |
| 378 | if (ret != EFI_SUCCESS) { |
| 379 | *image_path = NULL; |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | image = handler->protocol_interface; |
| 384 | *image_path = efi_dp_str(image->file_path); |
| 385 | |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * do_efi_show_drivers() - show UEFI drivers |
| 391 | * |
| 392 | * @cmdtp: Command table |
| 393 | * @flag: Command flag |
| 394 | * @argc: Number of arguments |
| 395 | * @argv: Argument array |
| 396 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 397 | * |
| 398 | * Implement efidebug "drivers" sub-command. |
| 399 | * Show all UEFI drivers and their information. |
| 400 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 401 | static int do_efi_show_drivers(struct cmd_tbl *cmdtp, int flag, |
| 402 | int argc, char *const argv[]) |
AKASHI Takahiro | 185716a | 2019-02-25 15:54:40 +0900 | [diff] [blame] | 403 | { |
| 404 | efi_handle_t *handles; |
| 405 | efi_uintn_t num, i; |
| 406 | u16 *driver_name, *image_path_text; |
| 407 | efi_status_t ret; |
| 408 | |
Heinrich Schuchardt | 982c7d9 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 409 | ret = EFI_CALL(efi_locate_handle_buffer( |
AKASHI Takahiro | 185716a | 2019-02-25 15:54:40 +0900 | [diff] [blame] | 410 | BY_PROTOCOL, &efi_guid_driver_binding_protocol, |
| 411 | NULL, &num, &handles)); |
| 412 | if (ret != EFI_SUCCESS) |
| 413 | return CMD_RET_FAILURE; |
| 414 | |
| 415 | if (!num) |
| 416 | return CMD_RET_SUCCESS; |
| 417 | |
| 418 | printf("Driver%.*s Name Image Path\n", |
| 419 | EFI_HANDLE_WIDTH - 6, spc); |
| 420 | printf("%.*s ==================== ====================\n", |
| 421 | EFI_HANDLE_WIDTH, sep); |
| 422 | for (i = 0; i < num; i++) { |
| 423 | if (!efi_get_driver_handle_info(handles[i], &driver_name, |
| 424 | &image_path_text)) { |
| 425 | if (image_path_text) |
| 426 | printf("%p %-20ls %ls\n", handles[i], |
| 427 | driver_name, image_path_text); |
| 428 | else |
| 429 | printf("%p %-20ls <built-in>\n", |
| 430 | handles[i], driver_name); |
Heinrich Schuchardt | 982c7d9 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 431 | efi_free_pool(driver_name); |
| 432 | efi_free_pool(image_path_text); |
AKASHI Takahiro | 185716a | 2019-02-25 15:54:40 +0900 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
Heinrich Schuchardt | 982c7d9 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 436 | efi_free_pool(handles); |
AKASHI Takahiro | 185716a | 2019-02-25 15:54:40 +0900 | [diff] [blame] | 437 | |
| 438 | return CMD_RET_SUCCESS; |
| 439 | } |
| 440 | |
AKASHI Takahiro | 02e8ca6 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 441 | /** |
| 442 | * do_efi_show_handles() - show UEFI handles |
| 443 | * |
| 444 | * @cmdtp: Command table |
| 445 | * @flag: Command flag |
| 446 | * @argc: Number of arguments |
| 447 | * @argv: Argument array |
| 448 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 449 | * |
| 450 | * Implement efidebug "dh" sub-command. |
| 451 | * Show all UEFI handles and their information, currently all protocols |
| 452 | * added to handle. |
| 453 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 454 | static int do_efi_show_handles(struct cmd_tbl *cmdtp, int flag, |
| 455 | int argc, char *const argv[]) |
AKASHI Takahiro | 02e8ca6 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 456 | { |
| 457 | efi_handle_t *handles; |
| 458 | efi_guid_t **guid; |
| 459 | efi_uintn_t num, count, i, j; |
AKASHI Takahiro | 02e8ca6 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 460 | efi_status_t ret; |
| 461 | |
Heinrich Schuchardt | 982c7d9 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 462 | ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL, |
AKASHI Takahiro | 02e8ca6 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 463 | &num, &handles)); |
| 464 | if (ret != EFI_SUCCESS) |
| 465 | return CMD_RET_FAILURE; |
| 466 | |
| 467 | if (!num) |
| 468 | return CMD_RET_SUCCESS; |
| 469 | |
AKASHI Takahiro | 02e8ca6 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 470 | for (i = 0; i < num; i++) { |
Heinrich Schuchardt | d16463a | 2022-10-04 15:31:17 +0200 | [diff] [blame] | 471 | struct efi_handler *handler; |
| 472 | |
| 473 | printf("\n%p", handles[i]); |
| 474 | if (handles[i]->dev) |
| 475 | printf(" (%s)", handles[i]->dev->name); |
| 476 | printf("\n"); |
| 477 | /* Print device path */ |
| 478 | ret = efi_search_protocol(handles[i], &efi_guid_device_path, |
| 479 | &handler); |
| 480 | if (ret == EFI_SUCCESS) |
| 481 | printf(" %pD\n", handler->protocol_interface); |
AKASHI Takahiro | 02e8ca6 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 482 | ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid, |
| 483 | &count)); |
Heinrich Schuchardt | d16463a | 2022-10-04 15:31:17 +0200 | [diff] [blame] | 484 | /* Print other protocols */ |
AKASHI Takahiro | 02e8ca6 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 485 | for (j = 0; j < count; j++) { |
Heinrich Schuchardt | d16463a | 2022-10-04 15:31:17 +0200 | [diff] [blame] | 486 | if (guidcmp(guid[j], &efi_guid_device_path)) |
| 487 | printf(" %pUs\n", guid[j]); |
AKASHI Takahiro | 02e8ca6 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 488 | } |
AKASHI Takahiro | 02e8ca6 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 489 | } |
| 490 | |
Heinrich Schuchardt | 982c7d9 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 491 | efi_free_pool(handles); |
AKASHI Takahiro | 02e8ca6 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 492 | |
| 493 | return CMD_RET_SUCCESS; |
| 494 | } |
| 495 | |
AKASHI Takahiro | 185716a | 2019-02-25 15:54:40 +0900 | [diff] [blame] | 496 | /** |
AKASHI Takahiro | 71ab1bf | 2019-02-25 15:54:42 +0900 | [diff] [blame] | 497 | * do_efi_show_images() - show UEFI images |
| 498 | * |
| 499 | * @cmdtp: Command table |
| 500 | * @flag: Command flag |
| 501 | * @argc: Number of arguments |
| 502 | * @argv: Argument array |
| 503 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 504 | * |
| 505 | * Implement efidebug "images" sub-command. |
| 506 | * Show all UEFI loaded images and their information. |
| 507 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 508 | static int do_efi_show_images(struct cmd_tbl *cmdtp, int flag, |
| 509 | int argc, char *const argv[]) |
AKASHI Takahiro | 71ab1bf | 2019-02-25 15:54:42 +0900 | [diff] [blame] | 510 | { |
| 511 | efi_print_image_infos(NULL); |
| 512 | |
| 513 | return CMD_RET_SUCCESS; |
| 514 | } |
| 515 | |
AKASHI Takahiro | 32979d8 | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 516 | static const char * const efi_mem_type_string[] = { |
| 517 | [EFI_RESERVED_MEMORY_TYPE] = "RESERVED", |
| 518 | [EFI_LOADER_CODE] = "LOADER CODE", |
| 519 | [EFI_LOADER_DATA] = "LOADER DATA", |
| 520 | [EFI_BOOT_SERVICES_CODE] = "BOOT CODE", |
| 521 | [EFI_BOOT_SERVICES_DATA] = "BOOT DATA", |
| 522 | [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE", |
| 523 | [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA", |
| 524 | [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL", |
| 525 | [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM", |
| 526 | [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM", |
| 527 | [EFI_ACPI_MEMORY_NVS] = "ACPI NVS", |
| 528 | [EFI_MMAP_IO] = "IO", |
| 529 | [EFI_MMAP_IO_PORT] = "IO PORT", |
| 530 | [EFI_PAL_CODE] = "PAL", |
Heinrich Schuchardt | 45176e8 | 2020-04-29 20:21:39 +0200 | [diff] [blame] | 531 | [EFI_PERSISTENT_MEMORY_TYPE] = "PERSISTENT", |
AKASHI Takahiro | 32979d8 | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 532 | }; |
| 533 | |
| 534 | static const struct efi_mem_attrs { |
| 535 | const u64 bit; |
| 536 | const char *text; |
| 537 | } efi_mem_attrs[] = { |
| 538 | {EFI_MEMORY_UC, "UC"}, |
| 539 | {EFI_MEMORY_UC, "UC"}, |
| 540 | {EFI_MEMORY_WC, "WC"}, |
| 541 | {EFI_MEMORY_WT, "WT"}, |
| 542 | {EFI_MEMORY_WB, "WB"}, |
| 543 | {EFI_MEMORY_UCE, "UCE"}, |
| 544 | {EFI_MEMORY_WP, "WP"}, |
| 545 | {EFI_MEMORY_RP, "RP"}, |
| 546 | {EFI_MEMORY_XP, "WP"}, |
| 547 | {EFI_MEMORY_NV, "NV"}, |
| 548 | {EFI_MEMORY_MORE_RELIABLE, "REL"}, |
| 549 | {EFI_MEMORY_RO, "RO"}, |
Heinrich Schuchardt | e76f0e4 | 2020-05-19 07:20:46 +0200 | [diff] [blame] | 550 | {EFI_MEMORY_SP, "SP"}, |
AKASHI Takahiro | 32979d8 | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 551 | {EFI_MEMORY_RUNTIME, "RT"}, |
| 552 | }; |
| 553 | |
| 554 | /** |
| 555 | * print_memory_attributes() - print memory map attributes |
Heinrich Schuchardt | b1d6543 | 2019-07-14 14:00:41 +0200 | [diff] [blame] | 556 | * |
AKASHI Takahiro | 32979d8 | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 557 | * @attributes: Attribute value |
| 558 | * |
| 559 | * Print memory map attributes |
| 560 | */ |
| 561 | static void print_memory_attributes(u64 attributes) |
| 562 | { |
| 563 | int sep, i; |
| 564 | |
| 565 | for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++) |
| 566 | if (attributes & efi_mem_attrs[i].bit) { |
| 567 | if (sep) { |
| 568 | putc('|'); |
| 569 | } else { |
| 570 | putc(' '); |
| 571 | sep = 1; |
| 572 | } |
| 573 | puts(efi_mem_attrs[i].text); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | #define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2) |
| 578 | |
| 579 | /** |
| 580 | * do_efi_show_memmap() - show UEFI memory map |
| 581 | * |
| 582 | * @cmdtp: Command table |
| 583 | * @flag: Command flag |
| 584 | * @argc: Number of arguments |
| 585 | * @argv: Argument array |
| 586 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 587 | * |
| 588 | * Implement efidebug "memmap" sub-command. |
| 589 | * Show UEFI memory map. |
| 590 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 591 | static int do_efi_show_memmap(struct cmd_tbl *cmdtp, int flag, |
| 592 | int argc, char *const argv[]) |
AKASHI Takahiro | 32979d8 | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 593 | { |
| 594 | struct efi_mem_desc *memmap = NULL, *map; |
| 595 | efi_uintn_t map_size = 0; |
| 596 | const char *type; |
| 597 | int i; |
| 598 | efi_status_t ret; |
| 599 | |
Heinrich Schuchardt | 982c7d9 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 600 | ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL); |
AKASHI Takahiro | 32979d8 | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 601 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 602 | map_size += sizeof(struct efi_mem_desc); /* for my own */ |
Heinrich Schuchardt | 982c7d9 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 603 | ret = efi_allocate_pool(EFI_LOADER_DATA, map_size, |
| 604 | (void *)&memmap); |
AKASHI Takahiro | 32979d8 | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 605 | if (ret != EFI_SUCCESS) |
| 606 | return CMD_RET_FAILURE; |
Heinrich Schuchardt | 982c7d9 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 607 | ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL); |
AKASHI Takahiro | 32979d8 | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 608 | } |
| 609 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | 982c7d9 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 610 | efi_free_pool(memmap); |
AKASHI Takahiro | 32979d8 | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 611 | return CMD_RET_FAILURE; |
| 612 | } |
| 613 | |
| 614 | printf("Type Start%.*s End%.*s Attributes\n", |
| 615 | EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc); |
| 616 | printf("================ %.*s %.*s ==========\n", |
| 617 | EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep); |
AKASHI Takahiro | ac0abe2 | 2020-05-08 14:50:18 +0900 | [diff] [blame] | 618 | /* |
| 619 | * Coverity check: dereferencing null pointer "map." |
| 620 | * This is a false positive as memmap will always be |
| 621 | * populated by allocate_pool() above. |
| 622 | */ |
AKASHI Takahiro | 32979d8 | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 623 | for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) { |
Heinrich Schuchardt | 45176e8 | 2020-04-29 20:21:39 +0200 | [diff] [blame] | 624 | if (map->type < ARRAY_SIZE(efi_mem_type_string)) |
AKASHI Takahiro | 32979d8 | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 625 | type = efi_mem_type_string[map->type]; |
| 626 | else |
| 627 | type = "(unknown)"; |
| 628 | |
| 629 | printf("%-16s %.*llx-%.*llx", type, |
| 630 | EFI_PHYS_ADDR_WIDTH, |
Heinrich Schuchardt | 9d4c788 | 2020-03-27 04:33:17 +0000 | [diff] [blame] | 631 | (u64)map_to_sysmem((void *)(uintptr_t) |
| 632 | map->physical_start), |
AKASHI Takahiro | 32979d8 | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 633 | EFI_PHYS_ADDR_WIDTH, |
Heinrich Schuchardt | 9d4c788 | 2020-03-27 04:33:17 +0000 | [diff] [blame] | 634 | (u64)map_to_sysmem((void *)(uintptr_t) |
| 635 | (map->physical_start + |
| 636 | map->num_pages * EFI_PAGE_SIZE))); |
AKASHI Takahiro | 32979d8 | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 637 | |
| 638 | print_memory_attributes(map->attribute); |
| 639 | putc('\n'); |
| 640 | } |
| 641 | |
Heinrich Schuchardt | 982c7d9 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 642 | efi_free_pool(memmap); |
AKASHI Takahiro | 32979d8 | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 643 | |
| 644 | return CMD_RET_SUCCESS; |
| 645 | } |
| 646 | |
AKASHI Takahiro | 71ab1bf | 2019-02-25 15:54:42 +0900 | [diff] [blame] | 647 | /** |
Heinrich Schuchardt | 75844e5 | 2020-01-07 05:57:47 +0100 | [diff] [blame] | 648 | * do_efi_show_tables() - show UEFI configuration tables |
| 649 | * |
| 650 | * @cmdtp: Command table |
| 651 | * @flag: Command flag |
| 652 | * @argc: Number of arguments |
| 653 | * @argv: Argument array |
| 654 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 655 | * |
| 656 | * Implement efidebug "tables" sub-command. |
| 657 | * Show UEFI configuration tables. |
| 658 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 659 | static int do_efi_show_tables(struct cmd_tbl *cmdtp, int flag, |
| 660 | int argc, char *const argv[]) |
Heinrich Schuchardt | 75844e5 | 2020-01-07 05:57:47 +0100 | [diff] [blame] | 661 | { |
| 662 | efi_uintn_t i; |
Heinrich Schuchardt | 75844e5 | 2020-01-07 05:57:47 +0100 | [diff] [blame] | 663 | |
Heinrich Schuchardt | a4628b4 | 2022-01-16 14:10:23 +0100 | [diff] [blame] | 664 | for (i = 0; i < systab.nr_tables; ++i) |
| 665 | printf("%pUl (%pUs)\n", |
| 666 | &systab.tables[i].guid, &systab.tables[i].guid); |
Heinrich Schuchardt | 75844e5 | 2020-01-07 05:57:47 +0100 | [diff] [blame] | 667 | |
| 668 | return CMD_RET_SUCCESS; |
| 669 | } |
| 670 | |
| 671 | /** |
Heinrich Schuchardt | 57274c8 | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 672 | * create_initrd_dp() - create a special device for our Boot### option |
Ilias Apalodimas | 773c090 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 673 | * |
Heinrich Schuchardt | 57274c8 | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 674 | * @dev: device |
| 675 | * @part: disk partition |
| 676 | * @file: filename |
| 677 | * @shortform: create short form device path |
| 678 | * Return: pointer to the device path or ERR_PTR |
Ilias Apalodimas | 773c090 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 679 | */ |
| 680 | static |
| 681 | struct efi_device_path *create_initrd_dp(const char *dev, const char *part, |
Heinrich Schuchardt | 57274c8 | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 682 | const char *file, int shortform) |
Ilias Apalodimas | 773c090 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 683 | |
| 684 | { |
Heinrich Schuchardt | 57274c8 | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 685 | struct efi_device_path *tmp_dp = NULL, *tmp_fp = NULL, *short_fp = NULL; |
Ilias Apalodimas | 773c090 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 686 | struct efi_device_path *initrd_dp = NULL; |
| 687 | efi_status_t ret; |
| 688 | const struct efi_initrd_dp id_dp = { |
| 689 | .vendor = { |
| 690 | { |
| 691 | DEVICE_PATH_TYPE_MEDIA_DEVICE, |
| 692 | DEVICE_PATH_SUB_TYPE_VENDOR_PATH, |
| 693 | sizeof(id_dp.vendor), |
| 694 | }, |
| 695 | EFI_INITRD_MEDIA_GUID, |
| 696 | }, |
| 697 | .end = { |
| 698 | DEVICE_PATH_TYPE_END, |
| 699 | DEVICE_PATH_SUB_TYPE_END, |
| 700 | sizeof(id_dp.end), |
| 701 | } |
| 702 | }; |
| 703 | |
| 704 | ret = efi_dp_from_name(dev, part, file, &tmp_dp, &tmp_fp); |
| 705 | if (ret != EFI_SUCCESS) { |
| 706 | printf("Cannot create device path for \"%s %s\"\n", part, file); |
| 707 | goto out; |
| 708 | } |
Heinrich Schuchardt | 57274c8 | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 709 | if (shortform) |
| 710 | short_fp = efi_dp_shorten(tmp_fp); |
| 711 | if (!short_fp) |
| 712 | short_fp = tmp_fp; |
Ilias Apalodimas | 773c090 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 713 | |
| 714 | initrd_dp = efi_dp_append((const struct efi_device_path *)&id_dp, |
Heinrich Schuchardt | 57274c8 | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 715 | short_fp); |
Ilias Apalodimas | 773c090 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 716 | |
| 717 | out: |
| 718 | efi_free_pool(tmp_dp); |
| 719 | efi_free_pool(tmp_fp); |
| 720 | return initrd_dp; |
| 721 | } |
| 722 | |
| 723 | /** |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 724 | * do_efi_boot_add() - set UEFI load option |
| 725 | * |
| 726 | * @cmdtp: Command table |
| 727 | * @flag: Command flag |
| 728 | * @argc: Number of arguments |
| 729 | * @argv: Argument array |
| 730 | * Return: CMD_RET_SUCCESS on success, |
| 731 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure |
| 732 | * |
Heinrich Schuchardt | b1d6543 | 2019-07-14 14:00:41 +0200 | [diff] [blame] | 733 | * Implement efidebug "boot add" sub-command. Create or change UEFI load option. |
| 734 | * |
Ilias Apalodimas | 773c090 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 735 | * efidebug boot add -b <id> <label> <interface> <devnum>[:<part>] <file> |
| 736 | * -i <file> <interface2> <devnum2>[:<part>] <initrd> |
| 737 | * -s '<options>' |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 738 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 739 | static int do_efi_boot_add(struct cmd_tbl *cmdtp, int flag, |
| 740 | int argc, char *const argv[]) |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 741 | { |
| 742 | int id; |
| 743 | char *endp; |
Heinrich Schuchardt | 9a5e74e | 2021-05-24 10:57:03 +0200 | [diff] [blame] | 744 | u16 var_name16[9]; |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 745 | efi_guid_t guid; |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 746 | u16 *label; |
Heinrich Schuchardt | efe92b7 | 2022-03-23 20:26:25 +0100 | [diff] [blame] | 747 | struct efi_device_path *file_path = NULL; |
Heinrich Schuchardt | 57274c8 | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 748 | struct efi_device_path *fp_free = NULL; |
Ilias Apalodimas | 773c090 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 749 | struct efi_device_path *final_fp = NULL; |
| 750 | struct efi_device_path *initrd_dp = NULL; |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 751 | struct efi_load_option lo; |
| 752 | void *data = NULL; |
| 753 | efi_uintn_t size; |
Ilias Apalodimas | 773c090 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 754 | efi_uintn_t fp_size = 0; |
Heinrich Schuchardt | 8bdd149 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 755 | efi_status_t ret; |
Heinrich Schuchardt | 99a21fc | 2019-06-20 12:59:45 +0200 | [diff] [blame] | 756 | int r = CMD_RET_SUCCESS; |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 757 | |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 758 | guid = efi_global_variable_guid; |
| 759 | |
| 760 | /* attributes */ |
| 761 | lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */ |
Ilias Apalodimas | 773c090 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 762 | lo.optional_data = NULL; |
| 763 | lo.label = NULL; |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 764 | |
Ilias Apalodimas | 773c090 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 765 | argc--; |
| 766 | argv++; /* 'add' */ |
| 767 | for (; argc > 0; argc--, argv++) { |
Heinrich Schuchardt | 57274c8 | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 768 | int shortform; |
| 769 | |
| 770 | if (*argv[0] != '-' || strlen(argv[0]) != 2) { |
| 771 | r = CMD_RET_USAGE; |
| 772 | goto out; |
| 773 | } |
| 774 | shortform = 0; |
| 775 | switch (argv[0][1]) { |
| 776 | case 'b': |
| 777 | shortform = 1; |
| 778 | /* fallthrough */ |
| 779 | case 'B': |
Ilias Apalodimas | 773c090 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 780 | if (argc < 5 || lo.label) { |
| 781 | r = CMD_RET_USAGE; |
| 782 | goto out; |
| 783 | } |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 784 | id = (int)hextoul(argv[1], &endp); |
Ilias Apalodimas | 773c090 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 785 | if (*endp != '\0' || id > 0xffff) |
| 786 | return CMD_RET_USAGE; |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 787 | |
Heinrich Schuchardt | 9a5e74e | 2021-05-24 10:57:03 +0200 | [diff] [blame] | 788 | efi_create_indexed_name(var_name16, sizeof(var_name16), |
| 789 | "Boot", id); |
Ilias Apalodimas | 773c090 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 790 | |
| 791 | /* label */ |
Heinrich Schuchardt | 412f9ec | 2022-10-06 06:48:02 +0200 | [diff] [blame] | 792 | label = efi_convert_string(argv[2]); |
Ilias Apalodimas | 773c090 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 793 | if (!label) |
| 794 | return CMD_RET_FAILURE; |
| 795 | lo.label = label; /* label will be changed below */ |
Ilias Apalodimas | 773c090 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 796 | |
| 797 | /* file path */ |
| 798 | ret = efi_dp_from_name(argv[3], argv[4], argv[5], |
Heinrich Schuchardt | efe92b7 | 2022-03-23 20:26:25 +0100 | [diff] [blame] | 799 | NULL, &fp_free); |
Ilias Apalodimas | 773c090 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 800 | if (ret != EFI_SUCCESS) { |
| 801 | printf("Cannot create device path for \"%s %s\"\n", |
| 802 | argv[3], argv[4]); |
| 803 | r = CMD_RET_FAILURE; |
| 804 | goto out; |
| 805 | } |
Heinrich Schuchardt | 57274c8 | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 806 | if (shortform) |
| 807 | file_path = efi_dp_shorten(fp_free); |
| 808 | if (!file_path) |
| 809 | file_path = fp_free; |
Ilias Apalodimas | 773c090 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 810 | fp_size += efi_dp_size(file_path) + |
| 811 | sizeof(struct efi_device_path); |
| 812 | argc -= 5; |
| 813 | argv += 5; |
Heinrich Schuchardt | 57274c8 | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 814 | break; |
| 815 | case 'i': |
| 816 | shortform = 1; |
| 817 | /* fallthrough */ |
| 818 | case 'I': |
Ilias Apalodimas | 773c090 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 819 | if (argc < 3 || initrd_dp) { |
| 820 | r = CMD_RET_USAGE; |
| 821 | goto out; |
| 822 | } |
| 823 | |
Heinrich Schuchardt | 57274c8 | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 824 | initrd_dp = create_initrd_dp(argv[1], argv[2], argv[3], |
| 825 | shortform); |
Ilias Apalodimas | 773c090 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 826 | if (!initrd_dp) { |
| 827 | printf("Cannot add an initrd\n"); |
| 828 | r = CMD_RET_FAILURE; |
| 829 | goto out; |
| 830 | } |
| 831 | argc -= 3; |
| 832 | argv += 3; |
| 833 | fp_size += efi_dp_size(initrd_dp) + |
| 834 | sizeof(struct efi_device_path); |
Heinrich Schuchardt | 57274c8 | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 835 | break; |
| 836 | case 's': |
Ilias Apalodimas | 773c090 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 837 | if (argc < 1 || lo.optional_data) { |
| 838 | r = CMD_RET_USAGE; |
| 839 | goto out; |
| 840 | } |
| 841 | lo.optional_data = (const u8 *)argv[1]; |
| 842 | argc -= 1; |
| 843 | argv += 1; |
Heinrich Schuchardt | 57274c8 | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 844 | break; |
| 845 | default: |
Ilias Apalodimas | 773c090 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 846 | r = CMD_RET_USAGE; |
| 847 | goto out; |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | if (!file_path) { |
| 852 | printf("Missing binary\n"); |
| 853 | r = CMD_RET_USAGE; |
| 854 | goto out; |
| 855 | } |
| 856 | |
| 857 | final_fp = efi_dp_concat(file_path, initrd_dp); |
| 858 | if (!final_fp) { |
| 859 | printf("Cannot create final device path\n"); |
Heinrich Schuchardt | 8bdd149 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 860 | r = CMD_RET_FAILURE; |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 861 | goto out; |
| 862 | } |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 863 | |
Ilias Apalodimas | 773c090 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 864 | lo.file_path = final_fp; |
| 865 | lo.file_path_length = fp_size; |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 866 | |
| 867 | size = efi_serialize_load_option(&lo, (u8 **)&data); |
| 868 | if (!size) { |
Heinrich Schuchardt | 8bdd149 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 869 | r = CMD_RET_FAILURE; |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 870 | goto out; |
| 871 | } |
| 872 | |
Heinrich Schuchardt | e8324f5 | 2021-05-24 11:10:59 +0200 | [diff] [blame] | 873 | ret = efi_set_variable_int(var_name16, &guid, |
| 874 | EFI_VARIABLE_NON_VOLATILE | |
| 875 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 876 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 877 | size, data, false); |
Heinrich Schuchardt | 99a21fc | 2019-06-20 12:59:45 +0200 | [diff] [blame] | 878 | if (ret != EFI_SUCCESS) { |
| 879 | printf("Cannot set %ls\n", var_name16); |
| 880 | r = CMD_RET_FAILURE; |
| 881 | } |
Ilias Apalodimas | 773c090 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 882 | |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 883 | out: |
| 884 | free(data); |
Ilias Apalodimas | 773c090 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 885 | efi_free_pool(final_fp); |
| 886 | efi_free_pool(initrd_dp); |
Heinrich Schuchardt | 57274c8 | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 887 | efi_free_pool(fp_free); |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 888 | free(lo.label); |
| 889 | |
Heinrich Schuchardt | 8bdd149 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 890 | return r; |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | /** |
| 894 | * do_efi_boot_rm() - delete UEFI load options |
| 895 | * |
| 896 | * @cmdtp: Command table |
| 897 | * @flag: Command flag |
| 898 | * @argc: Number of arguments |
| 899 | * @argv: Argument array |
| 900 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 901 | * |
| 902 | * Implement efidebug "boot rm" sub-command. |
| 903 | * Delete UEFI load options. |
Heinrich Schuchardt | b1d6543 | 2019-07-14 14:00:41 +0200 | [diff] [blame] | 904 | * |
| 905 | * efidebug boot rm <id> ... |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 906 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 907 | static int do_efi_boot_rm(struct cmd_tbl *cmdtp, int flag, |
| 908 | int argc, char *const argv[]) |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 909 | { |
| 910 | efi_guid_t guid; |
| 911 | int id, i; |
| 912 | char *endp; |
Heinrich Schuchardt | 9a5e74e | 2021-05-24 10:57:03 +0200 | [diff] [blame] | 913 | u16 var_name16[9]; |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 914 | efi_status_t ret; |
| 915 | |
| 916 | if (argc == 1) |
| 917 | return CMD_RET_USAGE; |
| 918 | |
| 919 | guid = efi_global_variable_guid; |
| 920 | for (i = 1; i < argc; i++, argv++) { |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 921 | id = (int)hextoul(argv[1], &endp); |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 922 | if (*endp != '\0' || id > 0xffff) |
| 923 | return CMD_RET_FAILURE; |
| 924 | |
Heinrich Schuchardt | 9a5e74e | 2021-05-24 10:57:03 +0200 | [diff] [blame] | 925 | efi_create_indexed_name(var_name16, sizeof(var_name16), |
| 926 | "Boot", id); |
Heinrich Schuchardt | e8324f5 | 2021-05-24 11:10:59 +0200 | [diff] [blame] | 927 | ret = efi_set_variable_int(var_name16, &guid, 0, 0, NULL, |
| 928 | false); |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 929 | if (ret) { |
Heinrich Schuchardt | 3be0bda | 2020-03-02 20:13:10 +0100 | [diff] [blame] | 930 | printf("Cannot remove %ls\n", var_name16); |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 931 | return CMD_RET_FAILURE; |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | return CMD_RET_SUCCESS; |
| 936 | } |
| 937 | |
| 938 | /** |
| 939 | * show_efi_boot_opt_data() - dump UEFI load option |
| 940 | * |
Heinrich Schuchardt | 8d8be69 | 2020-04-29 19:20:35 +0200 | [diff] [blame] | 941 | * @varname16: variable name |
Heinrich Schuchardt | 9df002e | 2019-04-29 13:51:45 +0200 | [diff] [blame] | 942 | * @data: value of UEFI load option variable |
| 943 | * @size: size of the boot option |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 944 | * |
| 945 | * Decode the value of UEFI load option variable and print information. |
| 946 | */ |
Heinrich Schuchardt | 7ca84f8 | 2020-05-31 22:46:09 +0200 | [diff] [blame] | 947 | static void show_efi_boot_opt_data(u16 *varname16, void *data, size_t *size) |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 948 | { |
Ilias Apalodimas | 773c090 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 949 | struct efi_device_path *initrd_path = NULL; |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 950 | struct efi_load_option lo; |
Heinrich Schuchardt | 7ca84f8 | 2020-05-31 22:46:09 +0200 | [diff] [blame] | 951 | efi_status_t ret; |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 952 | |
Heinrich Schuchardt | 7ca84f8 | 2020-05-31 22:46:09 +0200 | [diff] [blame] | 953 | ret = efi_deserialize_load_option(&lo, data, size); |
| 954 | if (ret != EFI_SUCCESS) { |
| 955 | printf("%ls: invalid load option\n", varname16); |
| 956 | return; |
| 957 | } |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 958 | |
Heinrich Schuchardt | 8d8be69 | 2020-04-29 19:20:35 +0200 | [diff] [blame] | 959 | printf("%ls:\nattributes: %c%c%c (0x%08x)\n", |
| 960 | varname16, |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 961 | /* ACTIVE */ |
| 962 | lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-', |
| 963 | /* FORCE RECONNECT */ |
| 964 | lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-', |
| 965 | /* HIDDEN */ |
| 966 | lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-', |
| 967 | lo.attributes); |
Heinrich Schuchardt | e6720b9 | 2021-05-24 10:35:25 +0200 | [diff] [blame] | 968 | printf(" label: %ls\n", lo.label); |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 969 | |
Heinrich Schuchardt | ca44a5d | 2021-10-15 01:47:40 +0200 | [diff] [blame] | 970 | printf(" file_path: %pD\n", lo.file_path); |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 971 | |
Heinrich Schuchardt | 6c405cb | 2021-10-15 02:33:33 +0200 | [diff] [blame] | 972 | initrd_path = efi_dp_from_lo(&lo, &efi_lf2_initrd_guid); |
Ilias Apalodimas | 773c090 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 973 | if (initrd_path) { |
Heinrich Schuchardt | ca44a5d | 2021-10-15 01:47:40 +0200 | [diff] [blame] | 974 | printf(" initrd_path: %pD\n", initrd_path); |
Ilias Apalodimas | 773c090 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 975 | efi_free_pool(initrd_path); |
| 976 | } |
| 977 | |
Heinrich Schuchardt | 9df002e | 2019-04-29 13:51:45 +0200 | [diff] [blame] | 978 | printf(" data:\n"); |
| 979 | print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1, |
Heinrich Schuchardt | 7ca84f8 | 2020-05-31 22:46:09 +0200 | [diff] [blame] | 980 | lo.optional_data, *size, true); |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | /** |
| 984 | * show_efi_boot_opt() - dump UEFI load option |
| 985 | * |
Heinrich Schuchardt | 8d8be69 | 2020-04-29 19:20:35 +0200 | [diff] [blame] | 986 | * @varname16: variable name |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 987 | * |
| 988 | * Dump information defined by UEFI load option. |
| 989 | */ |
Heinrich Schuchardt | 8d8be69 | 2020-04-29 19:20:35 +0200 | [diff] [blame] | 990 | static void show_efi_boot_opt(u16 *varname16) |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 991 | { |
Heinrich Schuchardt | 8d8be69 | 2020-04-29 19:20:35 +0200 | [diff] [blame] | 992 | void *data; |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 993 | efi_uintn_t size; |
AKASHI Takahiro | dab93f4 | 2019-11-26 10:11:22 +0900 | [diff] [blame] | 994 | efi_status_t ret; |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 995 | |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 996 | size = 0; |
Heinrich Schuchardt | f312087 | 2022-10-15 13:21:01 +0200 | [diff] [blame] | 997 | ret = efi_get_variable_int(varname16, &efi_global_variable_guid, |
| 998 | NULL, &size, NULL, NULL); |
AKASHI Takahiro | dab93f4 | 2019-11-26 10:11:22 +0900 | [diff] [blame] | 999 | if (ret == EFI_BUFFER_TOO_SMALL) { |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1000 | data = malloc(size); |
Heinrich Schuchardt | 8d8be69 | 2020-04-29 19:20:35 +0200 | [diff] [blame] | 1001 | if (!data) { |
| 1002 | printf("ERROR: Out of memory\n"); |
| 1003 | return; |
| 1004 | } |
Heinrich Schuchardt | f312087 | 2022-10-15 13:21:01 +0200 | [diff] [blame] | 1005 | ret = efi_get_variable_int(varname16, &efi_global_variable_guid, |
| 1006 | NULL, &size, data, NULL); |
Heinrich Schuchardt | 8d8be69 | 2020-04-29 19:20:35 +0200 | [diff] [blame] | 1007 | if (ret == EFI_SUCCESS) |
Heinrich Schuchardt | 7ca84f8 | 2020-05-31 22:46:09 +0200 | [diff] [blame] | 1008 | show_efi_boot_opt_data(varname16, data, &size); |
Heinrich Schuchardt | 8d8be69 | 2020-04-29 19:20:35 +0200 | [diff] [blame] | 1009 | free(data); |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1010 | } |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1011 | } |
| 1012 | |
AKASHI Takahiro | 016bfcd | 2019-04-26 09:44:18 +0900 | [diff] [blame] | 1013 | static int u16_tohex(u16 c) |
| 1014 | { |
| 1015 | if (c >= '0' && c <= '9') |
| 1016 | return c - '0'; |
| 1017 | if (c >= 'A' && c <= 'F') |
| 1018 | return c - 'A' + 10; |
| 1019 | |
| 1020 | /* not hexadecimal */ |
| 1021 | return -1; |
| 1022 | } |
| 1023 | |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1024 | /** |
| 1025 | * show_efi_boot_dump() - dump all UEFI load options |
| 1026 | * |
| 1027 | * @cmdtp: Command table |
| 1028 | * @flag: Command flag |
| 1029 | * @argc: Number of arguments |
| 1030 | * @argv: Argument array |
| 1031 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 1032 | * |
| 1033 | * Implement efidebug "boot dump" sub-command. |
| 1034 | * Dump information of all UEFI load options defined. |
Heinrich Schuchardt | 2171509 | 2019-07-25 20:52:23 +0200 | [diff] [blame] | 1035 | * |
| 1036 | * efidebug boot dump |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1037 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1038 | static int do_efi_boot_dump(struct cmd_tbl *cmdtp, int flag, |
| 1039 | int argc, char *const argv[]) |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1040 | { |
AKASHI Takahiro | 016bfcd | 2019-04-26 09:44:18 +0900 | [diff] [blame] | 1041 | u16 *var_name16, *p; |
| 1042 | efi_uintn_t buf_size, size; |
| 1043 | efi_guid_t guid; |
| 1044 | int id, i, digit; |
| 1045 | efi_status_t ret; |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1046 | |
| 1047 | if (argc > 1) |
| 1048 | return CMD_RET_USAGE; |
| 1049 | |
AKASHI Takahiro | 016bfcd | 2019-04-26 09:44:18 +0900 | [diff] [blame] | 1050 | buf_size = 128; |
| 1051 | var_name16 = malloc(buf_size); |
| 1052 | if (!var_name16) |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1053 | return CMD_RET_FAILURE; |
| 1054 | |
AKASHI Takahiro | 016bfcd | 2019-04-26 09:44:18 +0900 | [diff] [blame] | 1055 | var_name16[0] = 0; |
| 1056 | for (;;) { |
| 1057 | size = buf_size; |
Heinrich Schuchardt | f312087 | 2022-10-15 13:21:01 +0200 | [diff] [blame] | 1058 | ret = efi_get_next_variable_name_int(&size, var_name16, &guid); |
AKASHI Takahiro | 016bfcd | 2019-04-26 09:44:18 +0900 | [diff] [blame] | 1059 | if (ret == EFI_NOT_FOUND) |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1060 | break; |
AKASHI Takahiro | 016bfcd | 2019-04-26 09:44:18 +0900 | [diff] [blame] | 1061 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 1062 | buf_size = size; |
| 1063 | p = realloc(var_name16, buf_size); |
| 1064 | if (!p) { |
| 1065 | free(var_name16); |
| 1066 | return CMD_RET_FAILURE; |
| 1067 | } |
| 1068 | var_name16 = p; |
Heinrich Schuchardt | f312087 | 2022-10-15 13:21:01 +0200 | [diff] [blame] | 1069 | ret = efi_get_next_variable_name_int(&size, var_name16, |
| 1070 | &guid); |
AKASHI Takahiro | 016bfcd | 2019-04-26 09:44:18 +0900 | [diff] [blame] | 1071 | } |
| 1072 | if (ret != EFI_SUCCESS) { |
| 1073 | free(var_name16); |
| 1074 | return CMD_RET_FAILURE; |
| 1075 | } |
| 1076 | |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 1077 | if (memcmp(var_name16, u"Boot", 8)) |
AKASHI Takahiro | 016bfcd | 2019-04-26 09:44:18 +0900 | [diff] [blame] | 1078 | continue; |
| 1079 | |
| 1080 | for (id = 0, i = 0; i < 4; i++) { |
| 1081 | digit = u16_tohex(var_name16[4 + i]); |
| 1082 | if (digit < 0) |
| 1083 | break; |
| 1084 | id = (id << 4) + digit; |
| 1085 | } |
| 1086 | if (i == 4 && !var_name16[8]) |
Heinrich Schuchardt | 8d8be69 | 2020-04-29 19:20:35 +0200 | [diff] [blame] | 1087 | show_efi_boot_opt(var_name16); |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1088 | } |
AKASHI Takahiro | 016bfcd | 2019-04-26 09:44:18 +0900 | [diff] [blame] | 1089 | |
| 1090 | free(var_name16); |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1091 | |
| 1092 | return CMD_RET_SUCCESS; |
| 1093 | } |
| 1094 | |
| 1095 | /** |
| 1096 | * show_efi_boot_order() - show order of UEFI load options |
| 1097 | * |
| 1098 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 1099 | * |
| 1100 | * Show order of UEFI load options defined by BootOrder variable. |
| 1101 | */ |
| 1102 | static int show_efi_boot_order(void) |
| 1103 | { |
Heinrich Schuchardt | 98fc615 | 2020-04-29 21:15:08 +0200 | [diff] [blame] | 1104 | u16 *bootorder; |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1105 | efi_uintn_t size; |
| 1106 | int num, i; |
Heinrich Schuchardt | 9a5e74e | 2021-05-24 10:57:03 +0200 | [diff] [blame] | 1107 | u16 var_name16[9]; |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1108 | void *data; |
| 1109 | struct efi_load_option lo; |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1110 | efi_status_t ret; |
| 1111 | |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1112 | size = 0; |
Heinrich Schuchardt | f312087 | 2022-10-15 13:21:01 +0200 | [diff] [blame] | 1113 | ret = efi_get_variable_int(u"BootOrder", &efi_global_variable_guid, |
| 1114 | NULL, &size, NULL, NULL); |
Heinrich Schuchardt | 98fc615 | 2020-04-29 21:15:08 +0200 | [diff] [blame] | 1115 | if (ret != EFI_BUFFER_TOO_SMALL) { |
| 1116 | if (ret == EFI_NOT_FOUND) { |
| 1117 | printf("BootOrder not defined\n"); |
| 1118 | return CMD_RET_SUCCESS; |
| 1119 | } else { |
| 1120 | return CMD_RET_FAILURE; |
| 1121 | } |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1122 | } |
Heinrich Schuchardt | 98fc615 | 2020-04-29 21:15:08 +0200 | [diff] [blame] | 1123 | bootorder = malloc(size); |
| 1124 | if (!bootorder) { |
| 1125 | printf("ERROR: Out of memory\n"); |
| 1126 | return CMD_RET_FAILURE; |
| 1127 | } |
Heinrich Schuchardt | f312087 | 2022-10-15 13:21:01 +0200 | [diff] [blame] | 1128 | ret = efi_get_variable_int(u"BootOrder", &efi_global_variable_guid, |
| 1129 | NULL, &size, bootorder, NULL); |
Heinrich Schuchardt | 98fc615 | 2020-04-29 21:15:08 +0200 | [diff] [blame] | 1130 | if (ret != EFI_SUCCESS) { |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1131 | ret = CMD_RET_FAILURE; |
| 1132 | goto out; |
| 1133 | } |
| 1134 | |
| 1135 | num = size / sizeof(u16); |
| 1136 | for (i = 0; i < num; i++) { |
Heinrich Schuchardt | 9a5e74e | 2021-05-24 10:57:03 +0200 | [diff] [blame] | 1137 | efi_create_indexed_name(var_name16, sizeof(var_name16), |
Heinrich Schuchardt | 205bc3f | 2021-06-12 00:01:44 +0200 | [diff] [blame] | 1138 | "Boot", bootorder[i]); |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1139 | |
| 1140 | size = 0; |
Heinrich Schuchardt | f312087 | 2022-10-15 13:21:01 +0200 | [diff] [blame] | 1141 | ret = efi_get_variable_int(var_name16, |
| 1142 | &efi_global_variable_guid, NULL, |
| 1143 | &size, NULL, NULL); |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1144 | if (ret != EFI_BUFFER_TOO_SMALL) { |
Heinrich Schuchardt | 9a5e74e | 2021-05-24 10:57:03 +0200 | [diff] [blame] | 1145 | printf("%2d: %ls: (not defined)\n", i + 1, var_name16); |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1146 | continue; |
| 1147 | } |
| 1148 | |
| 1149 | data = malloc(size); |
| 1150 | if (!data) { |
| 1151 | ret = CMD_RET_FAILURE; |
| 1152 | goto out; |
| 1153 | } |
Heinrich Schuchardt | f312087 | 2022-10-15 13:21:01 +0200 | [diff] [blame] | 1154 | ret = efi_get_variable_int(var_name16, |
| 1155 | &efi_global_variable_guid, NULL, |
| 1156 | &size, data, NULL); |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1157 | if (ret != EFI_SUCCESS) { |
| 1158 | free(data); |
| 1159 | ret = CMD_RET_FAILURE; |
| 1160 | goto out; |
| 1161 | } |
| 1162 | |
Heinrich Schuchardt | 7ca84f8 | 2020-05-31 22:46:09 +0200 | [diff] [blame] | 1163 | ret = efi_deserialize_load_option(&lo, data, &size); |
| 1164 | if (ret != EFI_SUCCESS) { |
| 1165 | printf("%ls: invalid load option\n", var_name16); |
| 1166 | ret = CMD_RET_FAILURE; |
| 1167 | goto out; |
| 1168 | } |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1169 | |
Heinrich Schuchardt | 9a5e74e | 2021-05-24 10:57:03 +0200 | [diff] [blame] | 1170 | printf("%2d: %ls: %ls\n", i + 1, var_name16, lo.label); |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1171 | |
| 1172 | free(data); |
| 1173 | } |
| 1174 | out: |
| 1175 | free(bootorder); |
| 1176 | |
| 1177 | return ret; |
| 1178 | } |
| 1179 | |
| 1180 | /** |
| 1181 | * do_efi_boot_next() - manage UEFI BootNext variable |
| 1182 | * |
| 1183 | * @cmdtp: Command table |
| 1184 | * @flag: Command flag |
| 1185 | * @argc: Number of arguments |
| 1186 | * @argv: Argument array |
| 1187 | * Return: CMD_RET_SUCCESS on success, |
| 1188 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure |
| 1189 | * |
| 1190 | * Implement efidebug "boot next" sub-command. |
| 1191 | * Set BootNext variable. |
Heinrich Schuchardt | b1d6543 | 2019-07-14 14:00:41 +0200 | [diff] [blame] | 1192 | * |
| 1193 | * efidebug boot next <id> |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1194 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1195 | static int do_efi_boot_next(struct cmd_tbl *cmdtp, int flag, |
| 1196 | int argc, char *const argv[]) |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1197 | { |
| 1198 | u16 bootnext; |
| 1199 | efi_uintn_t size; |
| 1200 | char *endp; |
| 1201 | efi_guid_t guid; |
| 1202 | efi_status_t ret; |
Heinrich Schuchardt | 99a21fc | 2019-06-20 12:59:45 +0200 | [diff] [blame] | 1203 | int r = CMD_RET_SUCCESS; |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1204 | |
| 1205 | if (argc != 2) |
| 1206 | return CMD_RET_USAGE; |
| 1207 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 1208 | bootnext = (u16)hextoul(argv[1], &endp); |
Heinrich Schuchardt | 90c9d04 | 2020-05-09 17:59:19 +0200 | [diff] [blame] | 1209 | if (*endp) { |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1210 | printf("invalid value: %s\n", argv[1]); |
Heinrich Schuchardt | 8bdd149 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 1211 | r = CMD_RET_FAILURE; |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1212 | goto out; |
| 1213 | } |
| 1214 | |
| 1215 | guid = efi_global_variable_guid; |
| 1216 | size = sizeof(u16); |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 1217 | ret = efi_set_variable_int(u"BootNext", &guid, |
| 1218 | EFI_VARIABLE_NON_VOLATILE | |
| 1219 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 1220 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 1221 | size, &bootnext, false); |
Heinrich Schuchardt | 99a21fc | 2019-06-20 12:59:45 +0200 | [diff] [blame] | 1222 | if (ret != EFI_SUCCESS) { |
| 1223 | printf("Cannot set BootNext\n"); |
| 1224 | r = CMD_RET_FAILURE; |
| 1225 | } |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1226 | out: |
Heinrich Schuchardt | 8bdd149 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 1227 | return r; |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | /** |
| 1231 | * do_efi_boot_order() - manage UEFI BootOrder variable |
| 1232 | * |
| 1233 | * @cmdtp: Command table |
| 1234 | * @flag: Command flag |
| 1235 | * @argc: Number of arguments |
| 1236 | * @argv: Argument array |
| 1237 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 1238 | * |
| 1239 | * Implement efidebug "boot order" sub-command. |
| 1240 | * Show order of UEFI load options, or change it in BootOrder variable. |
Heinrich Schuchardt | b1d6543 | 2019-07-14 14:00:41 +0200 | [diff] [blame] | 1241 | * |
| 1242 | * efidebug boot order [<id> ...] |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1243 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1244 | static int do_efi_boot_order(struct cmd_tbl *cmdtp, int flag, |
| 1245 | int argc, char *const argv[]) |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1246 | { |
| 1247 | u16 *bootorder = NULL; |
| 1248 | efi_uintn_t size; |
| 1249 | int id, i; |
| 1250 | char *endp; |
| 1251 | efi_guid_t guid; |
| 1252 | efi_status_t ret; |
Heinrich Schuchardt | 99a21fc | 2019-06-20 12:59:45 +0200 | [diff] [blame] | 1253 | int r = CMD_RET_SUCCESS; |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1254 | |
| 1255 | if (argc == 1) |
| 1256 | return show_efi_boot_order(); |
| 1257 | |
| 1258 | argc--; |
| 1259 | argv++; |
| 1260 | |
| 1261 | size = argc * sizeof(u16); |
| 1262 | bootorder = malloc(size); |
| 1263 | if (!bootorder) |
| 1264 | return CMD_RET_FAILURE; |
| 1265 | |
| 1266 | for (i = 0; i < argc; i++) { |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 1267 | id = (int)hextoul(argv[i], &endp); |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1268 | if (*endp != '\0' || id > 0xffff) { |
| 1269 | printf("invalid value: %s\n", argv[i]); |
Heinrich Schuchardt | 8bdd149 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 1270 | r = CMD_RET_FAILURE; |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1271 | goto out; |
| 1272 | } |
| 1273 | |
| 1274 | bootorder[i] = (u16)id; |
| 1275 | } |
| 1276 | |
| 1277 | guid = efi_global_variable_guid; |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 1278 | ret = efi_set_variable_int(u"BootOrder", &guid, |
| 1279 | EFI_VARIABLE_NON_VOLATILE | |
| 1280 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 1281 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 1282 | size, bootorder, true); |
Heinrich Schuchardt | 99a21fc | 2019-06-20 12:59:45 +0200 | [diff] [blame] | 1283 | if (ret != EFI_SUCCESS) { |
| 1284 | printf("Cannot set BootOrder\n"); |
| 1285 | r = CMD_RET_FAILURE; |
| 1286 | } |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1287 | out: |
| 1288 | free(bootorder); |
| 1289 | |
Heinrich Schuchardt | 8bdd149 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 1290 | return r; |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1291 | } |
| 1292 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1293 | static struct cmd_tbl cmd_efidebug_boot_sub[] = { |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1294 | U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""), |
| 1295 | U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""), |
| 1296 | U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""), |
| 1297 | U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""), |
| 1298 | U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order, |
| 1299 | "", ""), |
| 1300 | }; |
| 1301 | |
| 1302 | /** |
| 1303 | * do_efi_boot_opt() - manage UEFI load options |
| 1304 | * |
| 1305 | * @cmdtp: Command table |
| 1306 | * @flag: Command flag |
| 1307 | * @argc: Number of arguments |
| 1308 | * @argv: Argument array |
| 1309 | * Return: CMD_RET_SUCCESS on success, |
| 1310 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure |
| 1311 | * |
| 1312 | * Implement efidebug "boot" sub-command. |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1313 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1314 | static int do_efi_boot_opt(struct cmd_tbl *cmdtp, int flag, |
| 1315 | int argc, char *const argv[]) |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1316 | { |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1317 | struct cmd_tbl *cp; |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1318 | |
| 1319 | if (argc < 2) |
| 1320 | return CMD_RET_USAGE; |
| 1321 | |
| 1322 | argc--; argv++; |
| 1323 | |
| 1324 | cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub, |
| 1325 | ARRAY_SIZE(cmd_efidebug_boot_sub)); |
| 1326 | if (!cp) |
| 1327 | return CMD_RET_USAGE; |
| 1328 | |
| 1329 | return cp->cmd(cmdtp, flag, argc, argv); |
| 1330 | } |
| 1331 | |
AKASHI Takahiro | 36bba2c | 2020-04-14 11:51:48 +0900 | [diff] [blame] | 1332 | /** |
| 1333 | * do_efi_test_bootmgr() - run simple bootmgr for test |
| 1334 | * |
| 1335 | * @cmdtp: Command table |
| 1336 | * @flag: Command flag |
| 1337 | * @argc: Number of arguments |
| 1338 | * @argv: Argument array |
| 1339 | * Return: CMD_RET_SUCCESS on success, |
| 1340 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure |
| 1341 | * |
| 1342 | * Implement efidebug "test bootmgr" sub-command. |
| 1343 | * Run simple bootmgr for test. |
| 1344 | * |
| 1345 | * efidebug test bootmgr |
| 1346 | */ |
Heinrich Schuchardt | b2625e8 | 2021-01-15 19:02:50 +0100 | [diff] [blame] | 1347 | static __maybe_unused int do_efi_test_bootmgr(struct cmd_tbl *cmdtp, int flag, |
| 1348 | int argc, char * const argv[]) |
AKASHI Takahiro | 36bba2c | 2020-04-14 11:51:48 +0900 | [diff] [blame] | 1349 | { |
| 1350 | efi_handle_t image; |
| 1351 | efi_uintn_t exit_data_size = 0; |
| 1352 | u16 *exit_data = NULL; |
| 1353 | efi_status_t ret; |
Heinrich Schuchardt | e6ed835 | 2020-08-11 18:20:50 +0200 | [diff] [blame] | 1354 | void *load_options = NULL; |
AKASHI Takahiro | 36bba2c | 2020-04-14 11:51:48 +0900 | [diff] [blame] | 1355 | |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 1356 | ret = efi_bootmgr_load(&image, &load_options); |
AKASHI Takahiro | 36bba2c | 2020-04-14 11:51:48 +0900 | [diff] [blame] | 1357 | printf("efi_bootmgr_load() returned: %ld\n", ret & ~EFI_ERROR_MASK); |
| 1358 | |
| 1359 | /* We call efi_start_image() even if error for test purpose. */ |
| 1360 | ret = EFI_CALL(efi_start_image(image, &exit_data_size, &exit_data)); |
| 1361 | printf("efi_start_image() returned: %ld\n", ret & ~EFI_ERROR_MASK); |
| 1362 | if (ret && exit_data) |
| 1363 | efi_free_pool(exit_data); |
| 1364 | |
| 1365 | efi_restore_gd(); |
| 1366 | |
Heinrich Schuchardt | a7647a7 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 1367 | free(load_options); |
AKASHI Takahiro | 36bba2c | 2020-04-14 11:51:48 +0900 | [diff] [blame] | 1368 | return CMD_RET_SUCCESS; |
| 1369 | } |
| 1370 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1371 | static struct cmd_tbl cmd_efidebug_test_sub[] = { |
Heinrich Schuchardt | b2625e8 | 2021-01-15 19:02:50 +0100 | [diff] [blame] | 1372 | #ifdef CONFIG_CMD_BOOTEFI_BOOTMGR |
AKASHI Takahiro | 36bba2c | 2020-04-14 11:51:48 +0900 | [diff] [blame] | 1373 | U_BOOT_CMD_MKENT(bootmgr, CONFIG_SYS_MAXARGS, 1, do_efi_test_bootmgr, |
| 1374 | "", ""), |
Heinrich Schuchardt | b2625e8 | 2021-01-15 19:02:50 +0100 | [diff] [blame] | 1375 | #endif |
AKASHI Takahiro | 36bba2c | 2020-04-14 11:51:48 +0900 | [diff] [blame] | 1376 | }; |
| 1377 | |
| 1378 | /** |
| 1379 | * do_efi_test() - manage UEFI load options |
| 1380 | * |
| 1381 | * @cmdtp: Command table |
| 1382 | * @flag: Command flag |
| 1383 | * @argc: Number of arguments |
| 1384 | * @argv: Argument array |
| 1385 | * Return: CMD_RET_SUCCESS on success, |
| 1386 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure |
| 1387 | * |
| 1388 | * Implement efidebug "test" sub-command. |
| 1389 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1390 | static int do_efi_test(struct cmd_tbl *cmdtp, int flag, |
AKASHI Takahiro | 36bba2c | 2020-04-14 11:51:48 +0900 | [diff] [blame] | 1391 | int argc, char * const argv[]) |
| 1392 | { |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1393 | struct cmd_tbl *cp; |
AKASHI Takahiro | 36bba2c | 2020-04-14 11:51:48 +0900 | [diff] [blame] | 1394 | |
| 1395 | if (argc < 2) |
| 1396 | return CMD_RET_USAGE; |
| 1397 | |
| 1398 | argc--; argv++; |
| 1399 | |
| 1400 | cp = find_cmd_tbl(argv[0], cmd_efidebug_test_sub, |
| 1401 | ARRAY_SIZE(cmd_efidebug_test_sub)); |
| 1402 | if (!cp) |
| 1403 | return CMD_RET_USAGE; |
| 1404 | |
| 1405 | return cp->cmd(cmdtp, flag, argc, argv); |
| 1406 | } |
| 1407 | |
Ilias Apalodimas | f500940 | 2020-05-17 22:25:45 +0300 | [diff] [blame] | 1408 | /** |
| 1409 | * do_efi_query_info() - QueryVariableInfo EFI service |
| 1410 | * |
| 1411 | * @cmdtp: Command table |
| 1412 | * @flag: Command flag |
| 1413 | * @argc: Number of arguments |
| 1414 | * @argv: Argument array |
| 1415 | * Return: CMD_RET_SUCCESS on success, |
| 1416 | * CMD_RET_USAGE or CMD_RET_FAILURE on failure |
| 1417 | * |
| 1418 | * Implement efidebug "test" sub-command. |
| 1419 | */ |
| 1420 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1421 | static int do_efi_query_info(struct cmd_tbl *cmdtp, int flag, |
Ilias Apalodimas | f500940 | 2020-05-17 22:25:45 +0300 | [diff] [blame] | 1422 | int argc, char * const argv[]) |
| 1423 | { |
| 1424 | efi_status_t ret; |
| 1425 | u32 attr = 0; |
| 1426 | u64 max_variable_storage_size; |
| 1427 | u64 remain_variable_storage_size; |
| 1428 | u64 max_variable_size; |
| 1429 | int i; |
| 1430 | |
| 1431 | for (i = 1; i < argc; i++) { |
| 1432 | if (!strcmp(argv[i], "-bs")) |
| 1433 | attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS; |
| 1434 | else if (!strcmp(argv[i], "-rt")) |
| 1435 | attr |= EFI_VARIABLE_RUNTIME_ACCESS; |
| 1436 | else if (!strcmp(argv[i], "-nv")) |
| 1437 | attr |= EFI_VARIABLE_NON_VOLATILE; |
| 1438 | else if (!strcmp(argv[i], "-at")) |
| 1439 | attr |= |
| 1440 | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS; |
| 1441 | } |
| 1442 | |
Heinrich Schuchardt | f312087 | 2022-10-15 13:21:01 +0200 | [diff] [blame] | 1443 | ret = efi_query_variable_info_int(attr, &max_variable_storage_size, |
| 1444 | &remain_variable_storage_size, |
| 1445 | &max_variable_size); |
Ilias Apalodimas | f500940 | 2020-05-17 22:25:45 +0300 | [diff] [blame] | 1446 | if (ret != EFI_SUCCESS) { |
| 1447 | printf("Error: Cannot query UEFI variables, r = %lu\n", |
| 1448 | ret & ~EFI_ERROR_MASK); |
| 1449 | return CMD_RET_FAILURE; |
| 1450 | } |
| 1451 | |
| 1452 | printf("Max storage size %llu\n", max_variable_storage_size); |
| 1453 | printf("Remaining storage size %llu\n", remain_variable_storage_size); |
| 1454 | printf("Max variable size %llu\n", max_variable_size); |
| 1455 | |
| 1456 | return CMD_RET_SUCCESS; |
| 1457 | } |
| 1458 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1459 | static struct cmd_tbl cmd_efidebug_sub[] = { |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1460 | U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""), |
AKASHI Takahiro | 38833c8 | 2020-11-17 09:28:01 +0900 | [diff] [blame] | 1461 | #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT |
| 1462 | U_BOOT_CMD_MKENT(capsule, CONFIG_SYS_MAXARGS, 1, do_efi_capsule, |
| 1463 | "", ""), |
| 1464 | #endif |
AKASHI Takahiro | 185716a | 2019-02-25 15:54:40 +0900 | [diff] [blame] | 1465 | U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers, |
| 1466 | "", ""), |
AKASHI Takahiro | 02e8ca6 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 1467 | U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles, |
| 1468 | "", ""), |
AKASHI Takahiro | 71ab1bf | 2019-02-25 15:54:42 +0900 | [diff] [blame] | 1469 | U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images, |
| 1470 | "", ""), |
AKASHI Takahiro | 32979d8 | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 1471 | U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap, |
| 1472 | "", ""), |
Heinrich Schuchardt | 75844e5 | 2020-01-07 05:57:47 +0100 | [diff] [blame] | 1473 | U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables, |
| 1474 | "", ""), |
AKASHI Takahiro | 36bba2c | 2020-04-14 11:51:48 +0900 | [diff] [blame] | 1475 | U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_efi_test, |
| 1476 | "", ""), |
Ilias Apalodimas | f500940 | 2020-05-17 22:25:45 +0300 | [diff] [blame] | 1477 | U_BOOT_CMD_MKENT(query, CONFIG_SYS_MAXARGS, 1, do_efi_query_info, |
| 1478 | "", ""), |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1479 | }; |
| 1480 | |
| 1481 | /** |
| 1482 | * do_efidebug() - display and configure UEFI environment |
| 1483 | * |
| 1484 | * @cmdtp: Command table |
| 1485 | * @flag: Command flag |
| 1486 | * @argc: Number of arguments |
| 1487 | * @argv: Argument array |
| 1488 | * Return: CMD_RET_SUCCESS on success, |
| 1489 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure |
| 1490 | * |
| 1491 | * Implement efidebug command which allows us to display and |
| 1492 | * configure UEFI environment. |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1493 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1494 | static int do_efidebug(struct cmd_tbl *cmdtp, int flag, |
| 1495 | int argc, char *const argv[]) |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1496 | { |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1497 | struct cmd_tbl *cp; |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1498 | efi_status_t r; |
| 1499 | |
| 1500 | if (argc < 2) |
| 1501 | return CMD_RET_USAGE; |
| 1502 | |
| 1503 | argc--; argv++; |
| 1504 | |
| 1505 | /* Initialize UEFI drivers */ |
| 1506 | r = efi_init_obj_list(); |
| 1507 | if (r != EFI_SUCCESS) { |
| 1508 | printf("Error: Cannot initialize UEFI sub-system, r = %lu\n", |
| 1509 | r & ~EFI_ERROR_MASK); |
| 1510 | return CMD_RET_FAILURE; |
| 1511 | } |
| 1512 | |
| 1513 | cp = find_cmd_tbl(argv[0], cmd_efidebug_sub, |
| 1514 | ARRAY_SIZE(cmd_efidebug_sub)); |
| 1515 | if (!cp) |
| 1516 | return CMD_RET_USAGE; |
| 1517 | |
| 1518 | return cp->cmd(cmdtp, flag, argc, argv); |
| 1519 | } |
| 1520 | |
| 1521 | #ifdef CONFIG_SYS_LONGHELP |
| 1522 | static char efidebug_help_text[] = |
| 1523 | " - UEFI Shell-like interface to configure UEFI environment\n" |
| 1524 | "\n" |
Heinrich Schuchardt | 57274c8 | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 1525 | "efidebug boot add - set UEFI BootXXXX variable\n" |
| 1526 | " -b|-B <bootid> <label> <interface> <devnum>[:<part>] <file path>\n" |
| 1527 | " -i|-I <interface> <devnum>[:<part>] <initrd file path>\n" |
| 1528 | " (-b, -i for short form device path)\n" |
| 1529 | " -s '<optional data>'\n" |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1530 | "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n" |
| 1531 | " - delete UEFI BootXXXX variables\n" |
| 1532 | "efidebug boot dump\n" |
| 1533 | " - dump all UEFI BootXXXX variables\n" |
| 1534 | "efidebug boot next <bootid>\n" |
| 1535 | " - set UEFI BootNext variable\n" |
| 1536 | "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n" |
| 1537 | " - set/show UEFI boot order\n" |
AKASHI Takahiro | e32788a | 2019-02-25 15:54:39 +0900 | [diff] [blame] | 1538 | "\n" |
AKASHI Takahiro | 38833c8 | 2020-11-17 09:28:01 +0900 | [diff] [blame] | 1539 | #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT |
| 1540 | "efidebug capsule update [-v] <capsule address>\n" |
| 1541 | " - process a capsule\n" |
Sughosh Ganu | e6f9efc | 2020-12-30 19:27:11 +0530 | [diff] [blame] | 1542 | "efidebug capsule disk-update\n" |
| 1543 | " - update a capsule from disk\n" |
AKASHI Takahiro | 38833c8 | 2020-11-17 09:28:01 +0900 | [diff] [blame] | 1544 | "efidebug capsule show <capsule address>\n" |
| 1545 | " - show capsule information\n" |
| 1546 | "efidebug capsule result [<capsule result var>]\n" |
| 1547 | " - show a capsule update result\n" |
Jose Marinho | b027e79 | 2021-03-11 13:18:51 +0000 | [diff] [blame] | 1548 | #ifdef CONFIG_EFI_ESRT |
| 1549 | "efidebug capsule esrt\n" |
| 1550 | " - print the ESRT\n" |
| 1551 | #endif |
AKASHI Takahiro | 38833c8 | 2020-11-17 09:28:01 +0900 | [diff] [blame] | 1552 | "\n" |
| 1553 | #endif |
AKASHI Takahiro | 185716a | 2019-02-25 15:54:40 +0900 | [diff] [blame] | 1554 | "efidebug drivers\n" |
Heinrich Schuchardt | c414d88 | 2020-01-07 07:48:15 +0100 | [diff] [blame] | 1555 | " - show UEFI drivers\n" |
AKASHI Takahiro | 02e8ca6 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 1556 | "efidebug dh\n" |
Heinrich Schuchardt | c414d88 | 2020-01-07 07:48:15 +0100 | [diff] [blame] | 1557 | " - show UEFI handles\n" |
AKASHI Takahiro | 71ab1bf | 2019-02-25 15:54:42 +0900 | [diff] [blame] | 1558 | "efidebug images\n" |
AKASHI Takahiro | 32979d8 | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 1559 | " - show loaded images\n" |
| 1560 | "efidebug memmap\n" |
Heinrich Schuchardt | c414d88 | 2020-01-07 07:48:15 +0100 | [diff] [blame] | 1561 | " - show UEFI memory map\n" |
Heinrich Schuchardt | 75844e5 | 2020-01-07 05:57:47 +0100 | [diff] [blame] | 1562 | "efidebug tables\n" |
AKASHI Takahiro | 36bba2c | 2020-04-14 11:51:48 +0900 | [diff] [blame] | 1563 | " - show UEFI configuration tables\n" |
Heinrich Schuchardt | b2625e8 | 2021-01-15 19:02:50 +0100 | [diff] [blame] | 1564 | #ifdef CONFIG_CMD_BOOTEFI_BOOTMGR |
AKASHI Takahiro | 36bba2c | 2020-04-14 11:51:48 +0900 | [diff] [blame] | 1565 | "efidebug test bootmgr\n" |
Ilias Apalodimas | f500940 | 2020-05-17 22:25:45 +0300 | [diff] [blame] | 1566 | " - run simple bootmgr for test\n" |
Heinrich Schuchardt | b2625e8 | 2021-01-15 19:02:50 +0100 | [diff] [blame] | 1567 | #endif |
Ilias Apalodimas | f500940 | 2020-05-17 22:25:45 +0300 | [diff] [blame] | 1568 | "efidebug query [-nv][-bs][-rt][-at]\n" |
| 1569 | " - show size of UEFI variables store\n"; |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1570 | #endif |
| 1571 | |
| 1572 | U_BOOT_CMD( |
Ilias Apalodimas | 773c090 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 1573 | efidebug, CONFIG_SYS_MAXARGS, 0, do_efidebug, |
AKASHI Takahiro | e7c0883 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1574 | "Configure UEFI environment", |
| 1575 | efidebug_help_text |
| 1576 | ); |