AKASHI Takahiro | 59df7e7 | 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 | f3290be | 2022-10-04 15:31:17 +0200 | [diff] [blame] | 11 | #include <dm/device.h> |
Heinrich Schuchardt | 94686f6 | 2020-12-13 10:30:24 +0100 | [diff] [blame] | 12 | #include <efi_dt_fixup.h> |
Ilias Apalodimas | cbea241 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 13 | #include <efi_load_initrd.h> |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 14 | #include <efi_loader.h> |
Heinrich Schuchardt | 7969335 | 2020-09-27 14:50:25 +0200 | [diff] [blame] | 15 | #include <efi_rng.h> |
Heinrich Schuchardt | acfe1de | 2021-05-24 11:10:59 +0200 | [diff] [blame] | 16 | #include <efi_variable.h> |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 17 | #include <exports.h> |
Heinrich Schuchardt | 39a1ff8 | 2019-04-29 13:51:45 +0200 | [diff] [blame] | 18 | #include <hexdump.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 19 | #include <log.h> |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 20 | #include <malloc.h> |
Heinrich Schuchardt | a415d61 | 2020-03-14 08:44:07 +0100 | [diff] [blame] | 21 | #include <mapmem.h> |
Heinrich Schuchardt | b9b0ea3 | 2021-02-02 17:53:14 +0100 | [diff] [blame] | 22 | #include <part.h> |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 23 | #include <search.h> |
| 24 | #include <linux/ctype.h> |
Ilias Apalodimas | cbea241 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 25 | #include <linux/err.h> |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 26 | |
AKASHI Takahiro | 355cdb5 | 2019-02-25 15:54:39 +0900 | [diff] [blame] | 27 | #define BS systab.boottime |
AKASHI Takahiro | 7f35ced | 2020-11-17 09:28:01 +0900 | [diff] [blame] | 28 | |
| 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 | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 64 | capsule = (typeof(capsule))hextoul(argv[1], &endp); |
AKASHI Takahiro | 7f35ced | 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 | f4d52c4 | 2022-10-15 13:21:01 +0200 | [diff] [blame] | 78 | ret = EFI_CALL(efi_update_capsule(&capsule, 1, 0)); |
AKASHI Takahiro | 7f35ced | 2020-11-17 09:28:01 +0900 | [diff] [blame] | 79 | if (ret) { |
Michal Simek | 046d7a0 | 2022-08-09 16:31:12 +0200 | [diff] [blame] | 80 | printf("Cannot handle a capsule at %p\n", capsule); |
AKASHI Takahiro | 7f35ced | 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 | 4cd1fca | 2022-03-21 22:37:35 +0900 | [diff] [blame] | 87 | #ifdef CONFIG_EFI_CAPSULE_ON_DISK |
Sughosh Ganu | 7407595 | 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 | 4cd1fca | 2022-03-21 22:37:35 +0900 | [diff] [blame] | 97 | #endif |
Sughosh Ganu | 7407595 | 2020-12-30 19:27:11 +0530 | [diff] [blame] | 98 | |
AKASHI Takahiro | 7f35ced | 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 | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 122 | capsule = (typeof(capsule))hextoul(argv[1], &endp); |
AKASHI Takahiro | 7f35ced | 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 | aa31a87 | 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 | 7f35ced | 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 | 2ecee31 | 2021-05-24 10:57:03 +0200 | [diff] [blame] | 233 | u16 var_name16[12]; |
AKASHI Takahiro | 7f35ced | 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 | 156ccbc | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 245 | ret = efi_get_variable_int(u"CapsuleLast", &guid, NULL, |
Heinrich Schuchardt | acfe1de | 2021-05-24 11:10:59 +0200 | [diff] [blame] | 246 | &size, var_name16, NULL); |
| 247 | |
AKASHI Takahiro | 7f35ced | 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 | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 261 | capsule_id = hextoul(argv[0], &endp); |
AKASHI Takahiro | 7f35ced | 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 | 2ecee31 | 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 | 7f35ced | 2020-11-17 09:28:01 +0900 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | size = 0; |
Heinrich Schuchardt | acfe1de | 2021-05-24 11:10:59 +0200 | [diff] [blame] | 270 | ret = efi_get_variable_int(var_name16, &guid, NULL, &size, NULL, NULL); |
AKASHI Takahiro | 7f35ced | 2020-11-17 09:28:01 +0900 | [diff] [blame] | 271 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 272 | result = malloc(size); |
AKASHI Takahiro | 30f8222 | 2021-01-22 10:42:48 +0900 | [diff] [blame] | 273 | if (!result) |
| 274 | return CMD_RET_FAILURE; |
Heinrich Schuchardt | acfe1de | 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 | 30f8222 | 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 | 7f35ced | 2020-11-17 09:28:01 +0900 | [diff] [blame] | 281 | |
AKASHI Takahiro | 30f8222 | 2021-01-22 10:42:48 +0900 | [diff] [blame] | 282 | return CMD_RET_FAILURE; |
AKASHI Takahiro | 7f35ced | 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 | aa31a87 | 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 | 4cd1fca | 2022-03-21 22:37:35 +0900 | [diff] [blame] | 308 | #ifdef CONFIG_EFI_CAPSULE_ON_DISK |
Sughosh Ganu | 7407595 | 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 | 4cd1fca | 2022-03-21 22:37:35 +0900 | [diff] [blame] | 311 | #endif |
AKASHI Takahiro | 7f35ced | 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 */ |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 346 | |
AKASHI Takahiro | 355cdb5 | 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 | 66eaf56 | 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 | 0914011 | 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 | 66eaf56 | 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 | a30c723 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 409 | ret = EFI_CALL(efi_locate_handle_buffer( |
AKASHI Takahiro | 66eaf56 | 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 | a30c723 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 431 | efi_free_pool(driver_name); |
| 432 | efi_free_pool(image_path_text); |
AKASHI Takahiro | 66eaf56 | 2019-02-25 15:54:40 +0900 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
Heinrich Schuchardt | a30c723 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 436 | efi_free_pool(handles); |
AKASHI Takahiro | 66eaf56 | 2019-02-25 15:54:40 +0900 | [diff] [blame] | 437 | |
| 438 | return CMD_RET_SUCCESS; |
| 439 | } |
| 440 | |
AKASHI Takahiro | a801462 | 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 | 0914011 | 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 | a801462 | 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 | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 460 | efi_status_t ret; |
| 461 | |
Heinrich Schuchardt | a30c723 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 462 | ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL, |
AKASHI Takahiro | a801462 | 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 | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 470 | for (i = 0; i < num; i++) { |
Heinrich Schuchardt | f3290be | 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 | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 482 | ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid, |
| 483 | &count)); |
Heinrich Schuchardt | f3290be | 2022-10-04 15:31:17 +0200 | [diff] [blame] | 484 | /* Print other protocols */ |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 485 | for (j = 0; j < count; j++) { |
Heinrich Schuchardt | f3290be | 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 | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 488 | } |
Masahisa Kojima | 0c95744 | 2023-06-29 11:13:51 +0900 | [diff] [blame] | 489 | efi_free_pool(guid); |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 490 | } |
| 491 | |
Heinrich Schuchardt | a30c723 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 492 | efi_free_pool(handles); |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 493 | |
| 494 | return CMD_RET_SUCCESS; |
| 495 | } |
| 496 | |
AKASHI Takahiro | 66eaf56 | 2019-02-25 15:54:40 +0900 | [diff] [blame] | 497 | /** |
AKASHI Takahiro | fa53673 | 2019-02-25 15:54:42 +0900 | [diff] [blame] | 498 | * do_efi_show_images() - show UEFI images |
| 499 | * |
| 500 | * @cmdtp: Command table |
| 501 | * @flag: Command flag |
| 502 | * @argc: Number of arguments |
| 503 | * @argv: Argument array |
| 504 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 505 | * |
| 506 | * Implement efidebug "images" sub-command. |
| 507 | * Show all UEFI loaded images and their information. |
| 508 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 509 | static int do_efi_show_images(struct cmd_tbl *cmdtp, int flag, |
| 510 | int argc, char *const argv[]) |
AKASHI Takahiro | fa53673 | 2019-02-25 15:54:42 +0900 | [diff] [blame] | 511 | { |
| 512 | efi_print_image_infos(NULL); |
| 513 | |
| 514 | return CMD_RET_SUCCESS; |
| 515 | } |
| 516 | |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 517 | static const char * const efi_mem_type_string[] = { |
| 518 | [EFI_RESERVED_MEMORY_TYPE] = "RESERVED", |
| 519 | [EFI_LOADER_CODE] = "LOADER CODE", |
| 520 | [EFI_LOADER_DATA] = "LOADER DATA", |
| 521 | [EFI_BOOT_SERVICES_CODE] = "BOOT CODE", |
| 522 | [EFI_BOOT_SERVICES_DATA] = "BOOT DATA", |
| 523 | [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE", |
| 524 | [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA", |
| 525 | [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL", |
| 526 | [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM", |
| 527 | [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM", |
| 528 | [EFI_ACPI_MEMORY_NVS] = "ACPI NVS", |
| 529 | [EFI_MMAP_IO] = "IO", |
| 530 | [EFI_MMAP_IO_PORT] = "IO PORT", |
| 531 | [EFI_PAL_CODE] = "PAL", |
Heinrich Schuchardt | dd9056c | 2020-04-29 20:21:39 +0200 | [diff] [blame] | 532 | [EFI_PERSISTENT_MEMORY_TYPE] = "PERSISTENT", |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 533 | }; |
| 534 | |
| 535 | static const struct efi_mem_attrs { |
| 536 | const u64 bit; |
| 537 | const char *text; |
| 538 | } efi_mem_attrs[] = { |
| 539 | {EFI_MEMORY_UC, "UC"}, |
| 540 | {EFI_MEMORY_UC, "UC"}, |
| 541 | {EFI_MEMORY_WC, "WC"}, |
| 542 | {EFI_MEMORY_WT, "WT"}, |
| 543 | {EFI_MEMORY_WB, "WB"}, |
| 544 | {EFI_MEMORY_UCE, "UCE"}, |
| 545 | {EFI_MEMORY_WP, "WP"}, |
| 546 | {EFI_MEMORY_RP, "RP"}, |
| 547 | {EFI_MEMORY_XP, "WP"}, |
| 548 | {EFI_MEMORY_NV, "NV"}, |
| 549 | {EFI_MEMORY_MORE_RELIABLE, "REL"}, |
| 550 | {EFI_MEMORY_RO, "RO"}, |
Heinrich Schuchardt | 255a473 | 2020-05-19 07:20:46 +0200 | [diff] [blame] | 551 | {EFI_MEMORY_SP, "SP"}, |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 552 | {EFI_MEMORY_RUNTIME, "RT"}, |
| 553 | }; |
| 554 | |
| 555 | /** |
| 556 | * print_memory_attributes() - print memory map attributes |
Heinrich Schuchardt | 0b01656 | 2019-07-14 14:00:41 +0200 | [diff] [blame] | 557 | * |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 558 | * @attributes: Attribute value |
| 559 | * |
| 560 | * Print memory map attributes |
| 561 | */ |
| 562 | static void print_memory_attributes(u64 attributes) |
| 563 | { |
| 564 | int sep, i; |
| 565 | |
| 566 | for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++) |
| 567 | if (attributes & efi_mem_attrs[i].bit) { |
| 568 | if (sep) { |
| 569 | putc('|'); |
| 570 | } else { |
| 571 | putc(' '); |
| 572 | sep = 1; |
| 573 | } |
| 574 | puts(efi_mem_attrs[i].text); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | #define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2) |
| 579 | |
| 580 | /** |
| 581 | * do_efi_show_memmap() - show UEFI memory map |
| 582 | * |
| 583 | * @cmdtp: Command table |
| 584 | * @flag: Command flag |
| 585 | * @argc: Number of arguments |
| 586 | * @argv: Argument array |
| 587 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 588 | * |
| 589 | * Implement efidebug "memmap" sub-command. |
| 590 | * Show UEFI memory map. |
| 591 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 592 | static int do_efi_show_memmap(struct cmd_tbl *cmdtp, int flag, |
| 593 | int argc, char *const argv[]) |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 594 | { |
Heinrich Schuchardt | eff4440 | 2023-01-05 18:26:01 +0100 | [diff] [blame] | 595 | struct efi_mem_desc *memmap, *map; |
| 596 | efi_uintn_t map_size; |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 597 | const char *type; |
| 598 | int i; |
| 599 | efi_status_t ret; |
| 600 | |
Heinrich Schuchardt | eff4440 | 2023-01-05 18:26:01 +0100 | [diff] [blame] | 601 | ret = efi_get_memory_map_alloc(&map_size, &memmap); |
| 602 | if (ret != EFI_SUCCESS) |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 603 | return CMD_RET_FAILURE; |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 604 | |
| 605 | printf("Type Start%.*s End%.*s Attributes\n", |
| 606 | EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc); |
| 607 | printf("================ %.*s %.*s ==========\n", |
| 608 | EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep); |
AKASHI Takahiro | e2a5b86 | 2020-05-08 14:50:18 +0900 | [diff] [blame] | 609 | /* |
| 610 | * Coverity check: dereferencing null pointer "map." |
| 611 | * This is a false positive as memmap will always be |
| 612 | * populated by allocate_pool() above. |
| 613 | */ |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 614 | for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) { |
Heinrich Schuchardt | dd9056c | 2020-04-29 20:21:39 +0200 | [diff] [blame] | 615 | if (map->type < ARRAY_SIZE(efi_mem_type_string)) |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 616 | type = efi_mem_type_string[map->type]; |
| 617 | else |
| 618 | type = "(unknown)"; |
| 619 | |
| 620 | printf("%-16s %.*llx-%.*llx", type, |
| 621 | EFI_PHYS_ADDR_WIDTH, |
Heinrich Schuchardt | 6c0ef35 | 2020-03-27 04:33:17 +0000 | [diff] [blame] | 622 | (u64)map_to_sysmem((void *)(uintptr_t) |
| 623 | map->physical_start), |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 624 | EFI_PHYS_ADDR_WIDTH, |
Heinrich Schuchardt | 6c0ef35 | 2020-03-27 04:33:17 +0000 | [diff] [blame] | 625 | (u64)map_to_sysmem((void *)(uintptr_t) |
| 626 | (map->physical_start + |
| 627 | map->num_pages * EFI_PAGE_SIZE))); |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 628 | |
| 629 | print_memory_attributes(map->attribute); |
| 630 | putc('\n'); |
| 631 | } |
| 632 | |
Heinrich Schuchardt | a30c723 | 2020-05-02 16:08:37 +0200 | [diff] [blame] | 633 | efi_free_pool(memmap); |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 634 | |
| 635 | return CMD_RET_SUCCESS; |
| 636 | } |
| 637 | |
AKASHI Takahiro | fa53673 | 2019-02-25 15:54:42 +0900 | [diff] [blame] | 638 | /** |
Heinrich Schuchardt | 986e064 | 2020-01-07 05:57:47 +0100 | [diff] [blame] | 639 | * do_efi_show_tables() - show UEFI configuration tables |
| 640 | * |
| 641 | * @cmdtp: Command table |
| 642 | * @flag: Command flag |
| 643 | * @argc: Number of arguments |
| 644 | * @argv: Argument array |
| 645 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 646 | * |
| 647 | * Implement efidebug "tables" sub-command. |
| 648 | * Show UEFI configuration tables. |
| 649 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 650 | static int do_efi_show_tables(struct cmd_tbl *cmdtp, int flag, |
| 651 | int argc, char *const argv[]) |
Heinrich Schuchardt | 986e064 | 2020-01-07 05:57:47 +0100 | [diff] [blame] | 652 | { |
Simon Glass | 041840e | 2023-03-20 08:30:14 +1300 | [diff] [blame] | 653 | efi_show_tables(&systab); |
Heinrich Schuchardt | 986e064 | 2020-01-07 05:57:47 +0100 | [diff] [blame] | 654 | |
| 655 | return CMD_RET_SUCCESS; |
| 656 | } |
| 657 | |
| 658 | /** |
Heinrich Schuchardt | 63276a5 | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 659 | * create_initrd_dp() - create a special device for our Boot### option |
Ilias Apalodimas | cbea241 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 660 | * |
Heinrich Schuchardt | 63276a5 | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 661 | * @dev: device |
| 662 | * @part: disk partition |
| 663 | * @file: filename |
| 664 | * @shortform: create short form device path |
| 665 | * Return: pointer to the device path or ERR_PTR |
Ilias Apalodimas | cbea241 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 666 | */ |
| 667 | static |
| 668 | struct efi_device_path *create_initrd_dp(const char *dev, const char *part, |
Heinrich Schuchardt | 63276a5 | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 669 | const char *file, int shortform) |
Ilias Apalodimas | cbea241 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 670 | |
| 671 | { |
Heinrich Schuchardt | 63276a5 | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 672 | struct efi_device_path *tmp_dp = NULL, *tmp_fp = NULL, *short_fp = NULL; |
Ilias Apalodimas | cbea241 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 673 | struct efi_device_path *initrd_dp = NULL; |
| 674 | efi_status_t ret; |
| 675 | const struct efi_initrd_dp id_dp = { |
| 676 | .vendor = { |
| 677 | { |
| 678 | DEVICE_PATH_TYPE_MEDIA_DEVICE, |
| 679 | DEVICE_PATH_SUB_TYPE_VENDOR_PATH, |
| 680 | sizeof(id_dp.vendor), |
| 681 | }, |
| 682 | EFI_INITRD_MEDIA_GUID, |
| 683 | }, |
| 684 | .end = { |
| 685 | DEVICE_PATH_TYPE_END, |
| 686 | DEVICE_PATH_SUB_TYPE_END, |
| 687 | sizeof(id_dp.end), |
| 688 | } |
| 689 | }; |
| 690 | |
| 691 | ret = efi_dp_from_name(dev, part, file, &tmp_dp, &tmp_fp); |
| 692 | if (ret != EFI_SUCCESS) { |
| 693 | printf("Cannot create device path for \"%s %s\"\n", part, file); |
| 694 | goto out; |
| 695 | } |
Heinrich Schuchardt | 63276a5 | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 696 | if (shortform) |
| 697 | short_fp = efi_dp_shorten(tmp_fp); |
| 698 | if (!short_fp) |
| 699 | short_fp = tmp_fp; |
Ilias Apalodimas | cbea241 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 700 | |
| 701 | initrd_dp = efi_dp_append((const struct efi_device_path *)&id_dp, |
Heinrich Schuchardt | 63276a5 | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 702 | short_fp); |
Ilias Apalodimas | cbea241 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 703 | |
| 704 | out: |
| 705 | efi_free_pool(tmp_dp); |
| 706 | efi_free_pool(tmp_fp); |
| 707 | return initrd_dp; |
| 708 | } |
| 709 | |
| 710 | /** |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 711 | * do_efi_boot_add() - set UEFI load option |
| 712 | * |
| 713 | * @cmdtp: Command table |
| 714 | * @flag: Command flag |
| 715 | * @argc: Number of arguments |
| 716 | * @argv: Argument array |
| 717 | * Return: CMD_RET_SUCCESS on success, |
| 718 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure |
| 719 | * |
Heinrich Schuchardt | 0b01656 | 2019-07-14 14:00:41 +0200 | [diff] [blame] | 720 | * Implement efidebug "boot add" sub-command. Create or change UEFI load option. |
| 721 | * |
Ilias Apalodimas | cbea241 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 722 | * efidebug boot add -b <id> <label> <interface> <devnum>[:<part>] <file> |
| 723 | * -i <file> <interface2> <devnum2>[:<part>] <initrd> |
| 724 | * -s '<options>' |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 725 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 726 | static int do_efi_boot_add(struct cmd_tbl *cmdtp, int flag, |
| 727 | int argc, char *const argv[]) |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 728 | { |
| 729 | int id; |
| 730 | char *endp; |
Heinrich Schuchardt | 2ecee31 | 2021-05-24 10:57:03 +0200 | [diff] [blame] | 731 | u16 var_name16[9]; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 732 | efi_guid_t guid; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 733 | u16 *label; |
Heinrich Schuchardt | ccc41fc | 2022-03-23 20:26:25 +0100 | [diff] [blame] | 734 | struct efi_device_path *file_path = NULL; |
Heinrich Schuchardt | 63276a5 | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 735 | struct efi_device_path *fp_free = NULL; |
Ilias Apalodimas | cbea241 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 736 | struct efi_device_path *final_fp = NULL; |
| 737 | struct efi_device_path *initrd_dp = NULL; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 738 | struct efi_load_option lo; |
| 739 | void *data = NULL; |
| 740 | efi_uintn_t size; |
Ilias Apalodimas | cbea241 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 741 | efi_uintn_t fp_size = 0; |
Heinrich Schuchardt | 428a470 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 742 | efi_status_t ret; |
Heinrich Schuchardt | a332f25 | 2019-06-20 12:59:45 +0200 | [diff] [blame] | 743 | int r = CMD_RET_SUCCESS; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 744 | |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 745 | guid = efi_global_variable_guid; |
| 746 | |
| 747 | /* attributes */ |
| 748 | lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */ |
Ilias Apalodimas | cbea241 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 749 | lo.optional_data = NULL; |
| 750 | lo.label = NULL; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 751 | |
Ilias Apalodimas | cbea241 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 752 | argc--; |
| 753 | argv++; /* 'add' */ |
| 754 | for (; argc > 0; argc--, argv++) { |
Heinrich Schuchardt | 63276a5 | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 755 | int shortform; |
| 756 | |
| 757 | if (*argv[0] != '-' || strlen(argv[0]) != 2) { |
| 758 | r = CMD_RET_USAGE; |
| 759 | goto out; |
| 760 | } |
| 761 | shortform = 0; |
| 762 | switch (argv[0][1]) { |
| 763 | case 'b': |
| 764 | shortform = 1; |
| 765 | /* fallthrough */ |
| 766 | case 'B': |
Ilias Apalodimas | cbea241 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 767 | if (argc < 5 || lo.label) { |
| 768 | r = CMD_RET_USAGE; |
| 769 | goto out; |
| 770 | } |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 771 | id = (int)hextoul(argv[1], &endp); |
Ilias Apalodimas | cbea241 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 772 | if (*endp != '\0' || id > 0xffff) |
| 773 | return CMD_RET_USAGE; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 774 | |
Heinrich Schuchardt | 2ecee31 | 2021-05-24 10:57:03 +0200 | [diff] [blame] | 775 | efi_create_indexed_name(var_name16, sizeof(var_name16), |
| 776 | "Boot", id); |
Ilias Apalodimas | cbea241 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 777 | |
| 778 | /* label */ |
Heinrich Schuchardt | 80fadf4 | 2022-10-06 06:48:02 +0200 | [diff] [blame] | 779 | label = efi_convert_string(argv[2]); |
Ilias Apalodimas | cbea241 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 780 | if (!label) |
| 781 | return CMD_RET_FAILURE; |
| 782 | lo.label = label; /* label will be changed below */ |
Ilias Apalodimas | cbea241 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 783 | |
| 784 | /* file path */ |
| 785 | ret = efi_dp_from_name(argv[3], argv[4], argv[5], |
Heinrich Schuchardt | ccc41fc | 2022-03-23 20:26:25 +0100 | [diff] [blame] | 786 | NULL, &fp_free); |
Ilias Apalodimas | cbea241 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 787 | if (ret != EFI_SUCCESS) { |
| 788 | printf("Cannot create device path for \"%s %s\"\n", |
| 789 | argv[3], argv[4]); |
| 790 | r = CMD_RET_FAILURE; |
| 791 | goto out; |
| 792 | } |
Heinrich Schuchardt | 63276a5 | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 793 | if (shortform) |
| 794 | file_path = efi_dp_shorten(fp_free); |
| 795 | if (!file_path) |
| 796 | file_path = fp_free; |
Ilias Apalodimas | cbea241 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 797 | fp_size += efi_dp_size(file_path) + |
| 798 | sizeof(struct efi_device_path); |
| 799 | argc -= 5; |
| 800 | argv += 5; |
Heinrich Schuchardt | 63276a5 | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 801 | break; |
| 802 | case 'i': |
| 803 | shortform = 1; |
| 804 | /* fallthrough */ |
| 805 | case 'I': |
Ilias Apalodimas | cbea241 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 806 | if (argc < 3 || initrd_dp) { |
| 807 | r = CMD_RET_USAGE; |
| 808 | goto out; |
| 809 | } |
| 810 | |
Heinrich Schuchardt | 63276a5 | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 811 | initrd_dp = create_initrd_dp(argv[1], argv[2], argv[3], |
| 812 | shortform); |
Ilias Apalodimas | cbea241 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 813 | if (!initrd_dp) { |
| 814 | printf("Cannot add an initrd\n"); |
| 815 | r = CMD_RET_FAILURE; |
| 816 | goto out; |
| 817 | } |
| 818 | argc -= 3; |
| 819 | argv += 3; |
| 820 | fp_size += efi_dp_size(initrd_dp) + |
| 821 | sizeof(struct efi_device_path); |
Heinrich Schuchardt | 63276a5 | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 822 | break; |
| 823 | case 's': |
Ilias Apalodimas | cbea241 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 824 | if (argc < 1 || lo.optional_data) { |
| 825 | r = CMD_RET_USAGE; |
| 826 | goto out; |
| 827 | } |
| 828 | lo.optional_data = (const u8 *)argv[1]; |
| 829 | argc -= 1; |
| 830 | argv += 1; |
Heinrich Schuchardt | 63276a5 | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 831 | break; |
| 832 | default: |
Ilias Apalodimas | cbea241 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 833 | r = CMD_RET_USAGE; |
| 834 | goto out; |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | if (!file_path) { |
| 839 | printf("Missing binary\n"); |
| 840 | r = CMD_RET_USAGE; |
| 841 | goto out; |
| 842 | } |
| 843 | |
| 844 | final_fp = efi_dp_concat(file_path, initrd_dp); |
| 845 | if (!final_fp) { |
| 846 | printf("Cannot create final device path\n"); |
Heinrich Schuchardt | 428a470 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 847 | r = CMD_RET_FAILURE; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 848 | goto out; |
| 849 | } |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 850 | |
Ilias Apalodimas | cbea241 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 851 | lo.file_path = final_fp; |
| 852 | lo.file_path_length = fp_size; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 853 | |
| 854 | size = efi_serialize_load_option(&lo, (u8 **)&data); |
| 855 | if (!size) { |
Heinrich Schuchardt | 428a470 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 856 | r = CMD_RET_FAILURE; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 857 | goto out; |
| 858 | } |
| 859 | |
Heinrich Schuchardt | acfe1de | 2021-05-24 11:10:59 +0200 | [diff] [blame] | 860 | ret = efi_set_variable_int(var_name16, &guid, |
| 861 | EFI_VARIABLE_NON_VOLATILE | |
| 862 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 863 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 864 | size, data, false); |
Heinrich Schuchardt | a332f25 | 2019-06-20 12:59:45 +0200 | [diff] [blame] | 865 | if (ret != EFI_SUCCESS) { |
| 866 | printf("Cannot set %ls\n", var_name16); |
| 867 | r = CMD_RET_FAILURE; |
| 868 | } |
Ilias Apalodimas | cbea241 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 869 | |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 870 | out: |
| 871 | free(data); |
Ilias Apalodimas | cbea241 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 872 | efi_free_pool(final_fp); |
| 873 | efi_free_pool(initrd_dp); |
Heinrich Schuchardt | 63276a5 | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 874 | efi_free_pool(fp_free); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 875 | free(lo.label); |
| 876 | |
Heinrich Schuchardt | 428a470 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 877 | return r; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | /** |
| 881 | * do_efi_boot_rm() - delete UEFI load options |
| 882 | * |
| 883 | * @cmdtp: Command table |
| 884 | * @flag: Command flag |
| 885 | * @argc: Number of arguments |
| 886 | * @argv: Argument array |
| 887 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 888 | * |
| 889 | * Implement efidebug "boot rm" sub-command. |
| 890 | * Delete UEFI load options. |
Heinrich Schuchardt | 0b01656 | 2019-07-14 14:00:41 +0200 | [diff] [blame] | 891 | * |
| 892 | * efidebug boot rm <id> ... |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 893 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 894 | static int do_efi_boot_rm(struct cmd_tbl *cmdtp, int flag, |
| 895 | int argc, char *const argv[]) |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 896 | { |
| 897 | efi_guid_t guid; |
| 898 | int id, i; |
| 899 | char *endp; |
Heinrich Schuchardt | 2ecee31 | 2021-05-24 10:57:03 +0200 | [diff] [blame] | 900 | u16 var_name16[9]; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 901 | efi_status_t ret; |
| 902 | |
| 903 | if (argc == 1) |
| 904 | return CMD_RET_USAGE; |
| 905 | |
| 906 | guid = efi_global_variable_guid; |
| 907 | for (i = 1; i < argc; i++, argv++) { |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 908 | id = (int)hextoul(argv[1], &endp); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 909 | if (*endp != '\0' || id > 0xffff) |
| 910 | return CMD_RET_FAILURE; |
| 911 | |
Heinrich Schuchardt | 2ecee31 | 2021-05-24 10:57:03 +0200 | [diff] [blame] | 912 | efi_create_indexed_name(var_name16, sizeof(var_name16), |
| 913 | "Boot", id); |
Heinrich Schuchardt | acfe1de | 2021-05-24 11:10:59 +0200 | [diff] [blame] | 914 | ret = efi_set_variable_int(var_name16, &guid, 0, 0, NULL, |
| 915 | false); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 916 | if (ret) { |
Heinrich Schuchardt | 30efb5d | 2020-03-02 20:13:10 +0100 | [diff] [blame] | 917 | printf("Cannot remove %ls\n", var_name16); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 918 | return CMD_RET_FAILURE; |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | return CMD_RET_SUCCESS; |
| 923 | } |
| 924 | |
| 925 | /** |
| 926 | * show_efi_boot_opt_data() - dump UEFI load option |
| 927 | * |
Heinrich Schuchardt | b5f4e9e | 2020-04-29 19:20:35 +0200 | [diff] [blame] | 928 | * @varname16: variable name |
Heinrich Schuchardt | 39a1ff8 | 2019-04-29 13:51:45 +0200 | [diff] [blame] | 929 | * @data: value of UEFI load option variable |
| 930 | * @size: size of the boot option |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 931 | * |
| 932 | * Decode the value of UEFI load option variable and print information. |
| 933 | */ |
Heinrich Schuchardt | 0e69bcf | 2020-05-31 22:46:09 +0200 | [diff] [blame] | 934 | static void show_efi_boot_opt_data(u16 *varname16, void *data, size_t *size) |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 935 | { |
Ilias Apalodimas | cbea241 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 936 | struct efi_device_path *initrd_path = NULL; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 937 | struct efi_load_option lo; |
Heinrich Schuchardt | 0e69bcf | 2020-05-31 22:46:09 +0200 | [diff] [blame] | 938 | efi_status_t ret; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 939 | |
Heinrich Schuchardt | 0e69bcf | 2020-05-31 22:46:09 +0200 | [diff] [blame] | 940 | ret = efi_deserialize_load_option(&lo, data, size); |
| 941 | if (ret != EFI_SUCCESS) { |
| 942 | printf("%ls: invalid load option\n", varname16); |
| 943 | return; |
| 944 | } |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 945 | |
Heinrich Schuchardt | b5f4e9e | 2020-04-29 19:20:35 +0200 | [diff] [blame] | 946 | printf("%ls:\nattributes: %c%c%c (0x%08x)\n", |
| 947 | varname16, |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 948 | /* ACTIVE */ |
| 949 | lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-', |
| 950 | /* FORCE RECONNECT */ |
| 951 | lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-', |
| 952 | /* HIDDEN */ |
| 953 | lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-', |
| 954 | lo.attributes); |
Heinrich Schuchardt | cd5a87e | 2021-05-24 10:35:25 +0200 | [diff] [blame] | 955 | printf(" label: %ls\n", lo.label); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 956 | |
Heinrich Schuchardt | fc42b8b | 2021-10-15 01:47:40 +0200 | [diff] [blame] | 957 | printf(" file_path: %pD\n", lo.file_path); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 958 | |
Heinrich Schuchardt | 9ad37fe | 2021-10-15 02:33:33 +0200 | [diff] [blame] | 959 | initrd_path = efi_dp_from_lo(&lo, &efi_lf2_initrd_guid); |
Ilias Apalodimas | cbea241 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 960 | if (initrd_path) { |
Heinrich Schuchardt | fc42b8b | 2021-10-15 01:47:40 +0200 | [diff] [blame] | 961 | printf(" initrd_path: %pD\n", initrd_path); |
Ilias Apalodimas | cbea241 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 962 | efi_free_pool(initrd_path); |
| 963 | } |
| 964 | |
Heinrich Schuchardt | 39a1ff8 | 2019-04-29 13:51:45 +0200 | [diff] [blame] | 965 | printf(" data:\n"); |
| 966 | print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1, |
Heinrich Schuchardt | 0e69bcf | 2020-05-31 22:46:09 +0200 | [diff] [blame] | 967 | lo.optional_data, *size, true); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | /** |
| 971 | * show_efi_boot_opt() - dump UEFI load option |
| 972 | * |
Heinrich Schuchardt | b5f4e9e | 2020-04-29 19:20:35 +0200 | [diff] [blame] | 973 | * @varname16: variable name |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 974 | * |
| 975 | * Dump information defined by UEFI load option. |
| 976 | */ |
Heinrich Schuchardt | b5f4e9e | 2020-04-29 19:20:35 +0200 | [diff] [blame] | 977 | static void show_efi_boot_opt(u16 *varname16) |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 978 | { |
Heinrich Schuchardt | b5f4e9e | 2020-04-29 19:20:35 +0200 | [diff] [blame] | 979 | void *data; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 980 | efi_uintn_t size; |
AKASHI Takahiro | 0bffb8c | 2019-11-26 10:11:22 +0900 | [diff] [blame] | 981 | efi_status_t ret; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 982 | |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 983 | size = 0; |
Heinrich Schuchardt | f4d52c4 | 2022-10-15 13:21:01 +0200 | [diff] [blame] | 984 | ret = efi_get_variable_int(varname16, &efi_global_variable_guid, |
| 985 | NULL, &size, NULL, NULL); |
AKASHI Takahiro | 0bffb8c | 2019-11-26 10:11:22 +0900 | [diff] [blame] | 986 | if (ret == EFI_BUFFER_TOO_SMALL) { |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 987 | data = malloc(size); |
Heinrich Schuchardt | b5f4e9e | 2020-04-29 19:20:35 +0200 | [diff] [blame] | 988 | if (!data) { |
| 989 | printf("ERROR: Out of memory\n"); |
| 990 | return; |
| 991 | } |
Heinrich Schuchardt | f4d52c4 | 2022-10-15 13:21:01 +0200 | [diff] [blame] | 992 | ret = efi_get_variable_int(varname16, &efi_global_variable_guid, |
| 993 | NULL, &size, data, NULL); |
Heinrich Schuchardt | b5f4e9e | 2020-04-29 19:20:35 +0200 | [diff] [blame] | 994 | if (ret == EFI_SUCCESS) |
Heinrich Schuchardt | 0e69bcf | 2020-05-31 22:46:09 +0200 | [diff] [blame] | 995 | show_efi_boot_opt_data(varname16, data, &size); |
Heinrich Schuchardt | b5f4e9e | 2020-04-29 19:20:35 +0200 | [diff] [blame] | 996 | free(data); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 997 | } |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | /** |
| 1001 | * show_efi_boot_dump() - dump all UEFI load options |
| 1002 | * |
| 1003 | * @cmdtp: Command table |
| 1004 | * @flag: Command flag |
| 1005 | * @argc: Number of arguments |
| 1006 | * @argv: Argument array |
| 1007 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 1008 | * |
| 1009 | * Implement efidebug "boot dump" sub-command. |
| 1010 | * Dump information of all UEFI load options defined. |
Heinrich Schuchardt | a6ccba0 | 2019-07-25 20:52:23 +0200 | [diff] [blame] | 1011 | * |
| 1012 | * efidebug boot dump |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1013 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1014 | static int do_efi_boot_dump(struct cmd_tbl *cmdtp, int flag, |
| 1015 | int argc, char *const argv[]) |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1016 | { |
AKASHI Takahiro | ffe2157 | 2019-04-26 09:44:18 +0900 | [diff] [blame] | 1017 | u16 *var_name16, *p; |
| 1018 | efi_uintn_t buf_size, size; |
| 1019 | efi_guid_t guid; |
AKASHI Takahiro | ffe2157 | 2019-04-26 09:44:18 +0900 | [diff] [blame] | 1020 | efi_status_t ret; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1021 | |
| 1022 | if (argc > 1) |
| 1023 | return CMD_RET_USAGE; |
| 1024 | |
AKASHI Takahiro | ffe2157 | 2019-04-26 09:44:18 +0900 | [diff] [blame] | 1025 | buf_size = 128; |
| 1026 | var_name16 = malloc(buf_size); |
| 1027 | if (!var_name16) |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1028 | return CMD_RET_FAILURE; |
| 1029 | |
AKASHI Takahiro | ffe2157 | 2019-04-26 09:44:18 +0900 | [diff] [blame] | 1030 | var_name16[0] = 0; |
| 1031 | for (;;) { |
| 1032 | size = buf_size; |
Heinrich Schuchardt | f4d52c4 | 2022-10-15 13:21:01 +0200 | [diff] [blame] | 1033 | ret = efi_get_next_variable_name_int(&size, var_name16, &guid); |
AKASHI Takahiro | ffe2157 | 2019-04-26 09:44:18 +0900 | [diff] [blame] | 1034 | if (ret == EFI_NOT_FOUND) |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1035 | break; |
AKASHI Takahiro | ffe2157 | 2019-04-26 09:44:18 +0900 | [diff] [blame] | 1036 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 1037 | buf_size = size; |
| 1038 | p = realloc(var_name16, buf_size); |
| 1039 | if (!p) { |
| 1040 | free(var_name16); |
| 1041 | return CMD_RET_FAILURE; |
| 1042 | } |
| 1043 | var_name16 = p; |
Heinrich Schuchardt | f4d52c4 | 2022-10-15 13:21:01 +0200 | [diff] [blame] | 1044 | ret = efi_get_next_variable_name_int(&size, var_name16, |
| 1045 | &guid); |
AKASHI Takahiro | ffe2157 | 2019-04-26 09:44:18 +0900 | [diff] [blame] | 1046 | } |
| 1047 | if (ret != EFI_SUCCESS) { |
| 1048 | free(var_name16); |
| 1049 | return CMD_RET_FAILURE; |
| 1050 | } |
| 1051 | |
Masahisa Kojima | 3ac026a | 2022-12-02 13:59:35 +0900 | [diff] [blame] | 1052 | if (efi_varname_is_load_option(var_name16, NULL)) |
Heinrich Schuchardt | b5f4e9e | 2020-04-29 19:20:35 +0200 | [diff] [blame] | 1053 | show_efi_boot_opt(var_name16); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1054 | } |
AKASHI Takahiro | ffe2157 | 2019-04-26 09:44:18 +0900 | [diff] [blame] | 1055 | |
| 1056 | free(var_name16); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1057 | |
| 1058 | return CMD_RET_SUCCESS; |
| 1059 | } |
| 1060 | |
| 1061 | /** |
| 1062 | * show_efi_boot_order() - show order of UEFI load options |
| 1063 | * |
| 1064 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 1065 | * |
| 1066 | * Show order of UEFI load options defined by BootOrder variable. |
| 1067 | */ |
| 1068 | static int show_efi_boot_order(void) |
| 1069 | { |
Heinrich Schuchardt | f9f5f92 | 2020-04-29 21:15:08 +0200 | [diff] [blame] | 1070 | u16 *bootorder; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1071 | efi_uintn_t size; |
| 1072 | int num, i; |
Heinrich Schuchardt | 2ecee31 | 2021-05-24 10:57:03 +0200 | [diff] [blame] | 1073 | u16 var_name16[9]; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1074 | void *data; |
| 1075 | struct efi_load_option lo; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1076 | efi_status_t ret; |
| 1077 | |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1078 | size = 0; |
Heinrich Schuchardt | f4d52c4 | 2022-10-15 13:21:01 +0200 | [diff] [blame] | 1079 | ret = efi_get_variable_int(u"BootOrder", &efi_global_variable_guid, |
| 1080 | NULL, &size, NULL, NULL); |
Heinrich Schuchardt | f9f5f92 | 2020-04-29 21:15:08 +0200 | [diff] [blame] | 1081 | if (ret != EFI_BUFFER_TOO_SMALL) { |
| 1082 | if (ret == EFI_NOT_FOUND) { |
| 1083 | printf("BootOrder not defined\n"); |
| 1084 | return CMD_RET_SUCCESS; |
| 1085 | } else { |
| 1086 | return CMD_RET_FAILURE; |
| 1087 | } |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1088 | } |
Heinrich Schuchardt | f9f5f92 | 2020-04-29 21:15:08 +0200 | [diff] [blame] | 1089 | bootorder = malloc(size); |
| 1090 | if (!bootorder) { |
| 1091 | printf("ERROR: Out of memory\n"); |
| 1092 | return CMD_RET_FAILURE; |
| 1093 | } |
Heinrich Schuchardt | f4d52c4 | 2022-10-15 13:21:01 +0200 | [diff] [blame] | 1094 | ret = efi_get_variable_int(u"BootOrder", &efi_global_variable_guid, |
| 1095 | NULL, &size, bootorder, NULL); |
Heinrich Schuchardt | f9f5f92 | 2020-04-29 21:15:08 +0200 | [diff] [blame] | 1096 | if (ret != EFI_SUCCESS) { |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1097 | ret = CMD_RET_FAILURE; |
| 1098 | goto out; |
| 1099 | } |
| 1100 | |
| 1101 | num = size / sizeof(u16); |
| 1102 | for (i = 0; i < num; i++) { |
Heinrich Schuchardt | 2ecee31 | 2021-05-24 10:57:03 +0200 | [diff] [blame] | 1103 | efi_create_indexed_name(var_name16, sizeof(var_name16), |
Heinrich Schuchardt | 2b8723c | 2021-06-12 00:01:44 +0200 | [diff] [blame] | 1104 | "Boot", bootorder[i]); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1105 | |
| 1106 | size = 0; |
Heinrich Schuchardt | f4d52c4 | 2022-10-15 13:21:01 +0200 | [diff] [blame] | 1107 | ret = efi_get_variable_int(var_name16, |
| 1108 | &efi_global_variable_guid, NULL, |
| 1109 | &size, NULL, NULL); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1110 | if (ret != EFI_BUFFER_TOO_SMALL) { |
Heinrich Schuchardt | 2ecee31 | 2021-05-24 10:57:03 +0200 | [diff] [blame] | 1111 | printf("%2d: %ls: (not defined)\n", i + 1, var_name16); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1112 | continue; |
| 1113 | } |
| 1114 | |
| 1115 | data = malloc(size); |
| 1116 | if (!data) { |
| 1117 | ret = CMD_RET_FAILURE; |
| 1118 | goto out; |
| 1119 | } |
Heinrich Schuchardt | f4d52c4 | 2022-10-15 13:21:01 +0200 | [diff] [blame] | 1120 | ret = efi_get_variable_int(var_name16, |
| 1121 | &efi_global_variable_guid, NULL, |
| 1122 | &size, data, NULL); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1123 | if (ret != EFI_SUCCESS) { |
| 1124 | free(data); |
| 1125 | ret = CMD_RET_FAILURE; |
| 1126 | goto out; |
| 1127 | } |
| 1128 | |
Heinrich Schuchardt | 0e69bcf | 2020-05-31 22:46:09 +0200 | [diff] [blame] | 1129 | ret = efi_deserialize_load_option(&lo, data, &size); |
| 1130 | if (ret != EFI_SUCCESS) { |
| 1131 | printf("%ls: invalid load option\n", var_name16); |
| 1132 | ret = CMD_RET_FAILURE; |
| 1133 | goto out; |
| 1134 | } |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1135 | |
Heinrich Schuchardt | 2ecee31 | 2021-05-24 10:57:03 +0200 | [diff] [blame] | 1136 | printf("%2d: %ls: %ls\n", i + 1, var_name16, lo.label); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1137 | |
| 1138 | free(data); |
| 1139 | } |
| 1140 | out: |
| 1141 | free(bootorder); |
| 1142 | |
| 1143 | return ret; |
| 1144 | } |
| 1145 | |
| 1146 | /** |
| 1147 | * do_efi_boot_next() - manage UEFI BootNext variable |
| 1148 | * |
| 1149 | * @cmdtp: Command table |
| 1150 | * @flag: Command flag |
| 1151 | * @argc: Number of arguments |
| 1152 | * @argv: Argument array |
| 1153 | * Return: CMD_RET_SUCCESS on success, |
| 1154 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure |
| 1155 | * |
| 1156 | * Implement efidebug "boot next" sub-command. |
| 1157 | * Set BootNext variable. |
Heinrich Schuchardt | 0b01656 | 2019-07-14 14:00:41 +0200 | [diff] [blame] | 1158 | * |
| 1159 | * efidebug boot next <id> |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1160 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1161 | static int do_efi_boot_next(struct cmd_tbl *cmdtp, int flag, |
| 1162 | int argc, char *const argv[]) |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1163 | { |
| 1164 | u16 bootnext; |
| 1165 | efi_uintn_t size; |
| 1166 | char *endp; |
| 1167 | efi_guid_t guid; |
| 1168 | efi_status_t ret; |
Heinrich Schuchardt | a332f25 | 2019-06-20 12:59:45 +0200 | [diff] [blame] | 1169 | int r = CMD_RET_SUCCESS; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1170 | |
| 1171 | if (argc != 2) |
| 1172 | return CMD_RET_USAGE; |
| 1173 | |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 1174 | bootnext = (u16)hextoul(argv[1], &endp); |
Heinrich Schuchardt | bdb1577 | 2020-05-09 17:59:19 +0200 | [diff] [blame] | 1175 | if (*endp) { |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1176 | printf("invalid value: %s\n", argv[1]); |
Heinrich Schuchardt | 428a470 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 1177 | r = CMD_RET_FAILURE; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1178 | goto out; |
| 1179 | } |
| 1180 | |
| 1181 | guid = efi_global_variable_guid; |
| 1182 | size = sizeof(u16); |
Simon Glass | 156ccbc | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 1183 | ret = efi_set_variable_int(u"BootNext", &guid, |
| 1184 | EFI_VARIABLE_NON_VOLATILE | |
| 1185 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 1186 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 1187 | size, &bootnext, false); |
Heinrich Schuchardt | a332f25 | 2019-06-20 12:59:45 +0200 | [diff] [blame] | 1188 | if (ret != EFI_SUCCESS) { |
| 1189 | printf("Cannot set BootNext\n"); |
| 1190 | r = CMD_RET_FAILURE; |
| 1191 | } |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1192 | out: |
Heinrich Schuchardt | 428a470 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 1193 | return r; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | /** |
| 1197 | * do_efi_boot_order() - manage UEFI BootOrder variable |
| 1198 | * |
| 1199 | * @cmdtp: Command table |
| 1200 | * @flag: Command flag |
| 1201 | * @argc: Number of arguments |
| 1202 | * @argv: Argument array |
| 1203 | * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure |
| 1204 | * |
| 1205 | * Implement efidebug "boot order" sub-command. |
| 1206 | * Show order of UEFI load options, or change it in BootOrder variable. |
Heinrich Schuchardt | 0b01656 | 2019-07-14 14:00:41 +0200 | [diff] [blame] | 1207 | * |
| 1208 | * efidebug boot order [<id> ...] |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1209 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1210 | static int do_efi_boot_order(struct cmd_tbl *cmdtp, int flag, |
| 1211 | int argc, char *const argv[]) |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1212 | { |
| 1213 | u16 *bootorder = NULL; |
| 1214 | efi_uintn_t size; |
| 1215 | int id, i; |
| 1216 | char *endp; |
| 1217 | efi_guid_t guid; |
| 1218 | efi_status_t ret; |
Heinrich Schuchardt | a332f25 | 2019-06-20 12:59:45 +0200 | [diff] [blame] | 1219 | int r = CMD_RET_SUCCESS; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1220 | |
| 1221 | if (argc == 1) |
| 1222 | return show_efi_boot_order(); |
| 1223 | |
| 1224 | argc--; |
| 1225 | argv++; |
| 1226 | |
| 1227 | size = argc * sizeof(u16); |
| 1228 | bootorder = malloc(size); |
| 1229 | if (!bootorder) |
| 1230 | return CMD_RET_FAILURE; |
| 1231 | |
| 1232 | for (i = 0; i < argc; i++) { |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 1233 | id = (int)hextoul(argv[i], &endp); |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1234 | if (*endp != '\0' || id > 0xffff) { |
| 1235 | printf("invalid value: %s\n", argv[i]); |
Heinrich Schuchardt | 428a470 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 1236 | r = CMD_RET_FAILURE; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1237 | goto out; |
| 1238 | } |
| 1239 | |
| 1240 | bootorder[i] = (u16)id; |
| 1241 | } |
| 1242 | |
| 1243 | guid = efi_global_variable_guid; |
Simon Glass | 156ccbc | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 1244 | ret = efi_set_variable_int(u"BootOrder", &guid, |
| 1245 | EFI_VARIABLE_NON_VOLATILE | |
| 1246 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 1247 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 1248 | size, bootorder, true); |
Heinrich Schuchardt | a332f25 | 2019-06-20 12:59:45 +0200 | [diff] [blame] | 1249 | if (ret != EFI_SUCCESS) { |
| 1250 | printf("Cannot set BootOrder\n"); |
| 1251 | r = CMD_RET_FAILURE; |
| 1252 | } |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1253 | out: |
| 1254 | free(bootorder); |
| 1255 | |
Heinrich Schuchardt | 428a470 | 2019-06-20 12:48:04 +0200 | [diff] [blame] | 1256 | return r; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1257 | } |
| 1258 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1259 | static struct cmd_tbl cmd_efidebug_boot_sub[] = { |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1260 | U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""), |
| 1261 | U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""), |
| 1262 | U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""), |
| 1263 | U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""), |
| 1264 | U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order, |
| 1265 | "", ""), |
| 1266 | }; |
| 1267 | |
| 1268 | /** |
| 1269 | * do_efi_boot_opt() - manage UEFI load options |
| 1270 | * |
| 1271 | * @cmdtp: Command table |
| 1272 | * @flag: Command flag |
| 1273 | * @argc: Number of arguments |
| 1274 | * @argv: Argument array |
| 1275 | * Return: CMD_RET_SUCCESS on success, |
| 1276 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure |
| 1277 | * |
| 1278 | * Implement efidebug "boot" sub-command. |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1279 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1280 | static int do_efi_boot_opt(struct cmd_tbl *cmdtp, int flag, |
| 1281 | int argc, char *const argv[]) |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1282 | { |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1283 | struct cmd_tbl *cp; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1284 | |
| 1285 | if (argc < 2) |
| 1286 | return CMD_RET_USAGE; |
| 1287 | |
| 1288 | argc--; argv++; |
| 1289 | |
| 1290 | cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub, |
| 1291 | ARRAY_SIZE(cmd_efidebug_boot_sub)); |
| 1292 | if (!cp) |
| 1293 | return CMD_RET_USAGE; |
| 1294 | |
| 1295 | return cp->cmd(cmdtp, flag, argc, argv); |
| 1296 | } |
| 1297 | |
AKASHI Takahiro | 525fc06 | 2020-04-14 11:51:48 +0900 | [diff] [blame] | 1298 | /** |
| 1299 | * do_efi_test_bootmgr() - run simple bootmgr for test |
| 1300 | * |
| 1301 | * @cmdtp: Command table |
| 1302 | * @flag: Command flag |
| 1303 | * @argc: Number of arguments |
| 1304 | * @argv: Argument array |
| 1305 | * Return: CMD_RET_SUCCESS on success, |
| 1306 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure |
| 1307 | * |
| 1308 | * Implement efidebug "test bootmgr" sub-command. |
| 1309 | * Run simple bootmgr for test. |
| 1310 | * |
| 1311 | * efidebug test bootmgr |
| 1312 | */ |
Heinrich Schuchardt | ff2f532 | 2021-01-15 19:02:50 +0100 | [diff] [blame] | 1313 | static __maybe_unused int do_efi_test_bootmgr(struct cmd_tbl *cmdtp, int flag, |
| 1314 | int argc, char * const argv[]) |
AKASHI Takahiro | 525fc06 | 2020-04-14 11:51:48 +0900 | [diff] [blame] | 1315 | { |
| 1316 | efi_handle_t image; |
| 1317 | efi_uintn_t exit_data_size = 0; |
| 1318 | u16 *exit_data = NULL; |
| 1319 | efi_status_t ret; |
Heinrich Schuchardt | bc78d22 | 2020-08-11 18:20:50 +0200 | [diff] [blame] | 1320 | void *load_options = NULL; |
AKASHI Takahiro | 525fc06 | 2020-04-14 11:51:48 +0900 | [diff] [blame] | 1321 | |
Heinrich Schuchardt | 0ad6400 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 1322 | ret = efi_bootmgr_load(&image, &load_options); |
AKASHI Takahiro | 525fc06 | 2020-04-14 11:51:48 +0900 | [diff] [blame] | 1323 | printf("efi_bootmgr_load() returned: %ld\n", ret & ~EFI_ERROR_MASK); |
| 1324 | |
| 1325 | /* We call efi_start_image() even if error for test purpose. */ |
| 1326 | ret = EFI_CALL(efi_start_image(image, &exit_data_size, &exit_data)); |
| 1327 | printf("efi_start_image() returned: %ld\n", ret & ~EFI_ERROR_MASK); |
| 1328 | if (ret && exit_data) |
| 1329 | efi_free_pool(exit_data); |
| 1330 | |
| 1331 | efi_restore_gd(); |
| 1332 | |
Heinrich Schuchardt | 0ad6400 | 2020-08-07 17:49:39 +0200 | [diff] [blame] | 1333 | free(load_options); |
AKASHI Takahiro | 525fc06 | 2020-04-14 11:51:48 +0900 | [diff] [blame] | 1334 | return CMD_RET_SUCCESS; |
| 1335 | } |
| 1336 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1337 | static struct cmd_tbl cmd_efidebug_test_sub[] = { |
Heinrich Schuchardt | ff2f532 | 2021-01-15 19:02:50 +0100 | [diff] [blame] | 1338 | #ifdef CONFIG_CMD_BOOTEFI_BOOTMGR |
AKASHI Takahiro | 525fc06 | 2020-04-14 11:51:48 +0900 | [diff] [blame] | 1339 | U_BOOT_CMD_MKENT(bootmgr, CONFIG_SYS_MAXARGS, 1, do_efi_test_bootmgr, |
| 1340 | "", ""), |
Heinrich Schuchardt | ff2f532 | 2021-01-15 19:02:50 +0100 | [diff] [blame] | 1341 | #endif |
AKASHI Takahiro | 525fc06 | 2020-04-14 11:51:48 +0900 | [diff] [blame] | 1342 | }; |
| 1343 | |
| 1344 | /** |
| 1345 | * do_efi_test() - manage UEFI load options |
| 1346 | * |
| 1347 | * @cmdtp: Command table |
| 1348 | * @flag: Command flag |
| 1349 | * @argc: Number of arguments |
| 1350 | * @argv: Argument array |
| 1351 | * Return: CMD_RET_SUCCESS on success, |
| 1352 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure |
| 1353 | * |
| 1354 | * Implement efidebug "test" sub-command. |
| 1355 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1356 | static int do_efi_test(struct cmd_tbl *cmdtp, int flag, |
AKASHI Takahiro | 525fc06 | 2020-04-14 11:51:48 +0900 | [diff] [blame] | 1357 | int argc, char * const argv[]) |
| 1358 | { |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1359 | struct cmd_tbl *cp; |
AKASHI Takahiro | 525fc06 | 2020-04-14 11:51:48 +0900 | [diff] [blame] | 1360 | |
| 1361 | if (argc < 2) |
| 1362 | return CMD_RET_USAGE; |
| 1363 | |
| 1364 | argc--; argv++; |
| 1365 | |
| 1366 | cp = find_cmd_tbl(argv[0], cmd_efidebug_test_sub, |
| 1367 | ARRAY_SIZE(cmd_efidebug_test_sub)); |
| 1368 | if (!cp) |
| 1369 | return CMD_RET_USAGE; |
| 1370 | |
| 1371 | return cp->cmd(cmdtp, flag, argc, argv); |
| 1372 | } |
| 1373 | |
Ilias Apalodimas | b0e4f2c | 2020-05-17 22:25:45 +0300 | [diff] [blame] | 1374 | /** |
| 1375 | * do_efi_query_info() - QueryVariableInfo EFI service |
| 1376 | * |
| 1377 | * @cmdtp: Command table |
| 1378 | * @flag: Command flag |
| 1379 | * @argc: Number of arguments |
| 1380 | * @argv: Argument array |
| 1381 | * Return: CMD_RET_SUCCESS on success, |
| 1382 | * CMD_RET_USAGE or CMD_RET_FAILURE on failure |
| 1383 | * |
| 1384 | * Implement efidebug "test" sub-command. |
| 1385 | */ |
| 1386 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1387 | static int do_efi_query_info(struct cmd_tbl *cmdtp, int flag, |
Ilias Apalodimas | b0e4f2c | 2020-05-17 22:25:45 +0300 | [diff] [blame] | 1388 | int argc, char * const argv[]) |
| 1389 | { |
| 1390 | efi_status_t ret; |
| 1391 | u32 attr = 0; |
| 1392 | u64 max_variable_storage_size; |
| 1393 | u64 remain_variable_storage_size; |
| 1394 | u64 max_variable_size; |
| 1395 | int i; |
| 1396 | |
| 1397 | for (i = 1; i < argc; i++) { |
| 1398 | if (!strcmp(argv[i], "-bs")) |
| 1399 | attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS; |
| 1400 | else if (!strcmp(argv[i], "-rt")) |
| 1401 | attr |= EFI_VARIABLE_RUNTIME_ACCESS; |
| 1402 | else if (!strcmp(argv[i], "-nv")) |
| 1403 | attr |= EFI_VARIABLE_NON_VOLATILE; |
| 1404 | else if (!strcmp(argv[i], "-at")) |
| 1405 | attr |= |
| 1406 | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS; |
| 1407 | } |
| 1408 | |
Heinrich Schuchardt | f4d52c4 | 2022-10-15 13:21:01 +0200 | [diff] [blame] | 1409 | ret = efi_query_variable_info_int(attr, &max_variable_storage_size, |
| 1410 | &remain_variable_storage_size, |
| 1411 | &max_variable_size); |
Ilias Apalodimas | b0e4f2c | 2020-05-17 22:25:45 +0300 | [diff] [blame] | 1412 | if (ret != EFI_SUCCESS) { |
| 1413 | printf("Error: Cannot query UEFI variables, r = %lu\n", |
| 1414 | ret & ~EFI_ERROR_MASK); |
| 1415 | return CMD_RET_FAILURE; |
| 1416 | } |
| 1417 | |
| 1418 | printf("Max storage size %llu\n", max_variable_storage_size); |
| 1419 | printf("Remaining storage size %llu\n", remain_variable_storage_size); |
| 1420 | printf("Max variable size %llu\n", max_variable_size); |
| 1421 | |
| 1422 | return CMD_RET_SUCCESS; |
| 1423 | } |
| 1424 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1425 | static struct cmd_tbl cmd_efidebug_sub[] = { |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1426 | U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""), |
AKASHI Takahiro | 7f35ced | 2020-11-17 09:28:01 +0900 | [diff] [blame] | 1427 | #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT |
| 1428 | U_BOOT_CMD_MKENT(capsule, CONFIG_SYS_MAXARGS, 1, do_efi_capsule, |
| 1429 | "", ""), |
| 1430 | #endif |
AKASHI Takahiro | 66eaf56 | 2019-02-25 15:54:40 +0900 | [diff] [blame] | 1431 | U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers, |
| 1432 | "", ""), |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 1433 | U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles, |
| 1434 | "", ""), |
AKASHI Takahiro | fa53673 | 2019-02-25 15:54:42 +0900 | [diff] [blame] | 1435 | U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images, |
| 1436 | "", ""), |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 1437 | U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap, |
| 1438 | "", ""), |
Heinrich Schuchardt | 986e064 | 2020-01-07 05:57:47 +0100 | [diff] [blame] | 1439 | U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables, |
| 1440 | "", ""), |
AKASHI Takahiro | 525fc06 | 2020-04-14 11:51:48 +0900 | [diff] [blame] | 1441 | U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_efi_test, |
| 1442 | "", ""), |
Ilias Apalodimas | b0e4f2c | 2020-05-17 22:25:45 +0300 | [diff] [blame] | 1443 | U_BOOT_CMD_MKENT(query, CONFIG_SYS_MAXARGS, 1, do_efi_query_info, |
| 1444 | "", ""), |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1445 | }; |
| 1446 | |
| 1447 | /** |
| 1448 | * do_efidebug() - display and configure UEFI environment |
| 1449 | * |
| 1450 | * @cmdtp: Command table |
| 1451 | * @flag: Command flag |
| 1452 | * @argc: Number of arguments |
| 1453 | * @argv: Argument array |
| 1454 | * Return: CMD_RET_SUCCESS on success, |
| 1455 | * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure |
| 1456 | * |
| 1457 | * Implement efidebug command which allows us to display and |
| 1458 | * configure UEFI environment. |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1459 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1460 | static int do_efidebug(struct cmd_tbl *cmdtp, int flag, |
| 1461 | int argc, char *const argv[]) |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1462 | { |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1463 | struct cmd_tbl *cp; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1464 | efi_status_t r; |
| 1465 | |
| 1466 | if (argc < 2) |
| 1467 | return CMD_RET_USAGE; |
| 1468 | |
| 1469 | argc--; argv++; |
| 1470 | |
| 1471 | /* Initialize UEFI drivers */ |
| 1472 | r = efi_init_obj_list(); |
| 1473 | if (r != EFI_SUCCESS) { |
| 1474 | printf("Error: Cannot initialize UEFI sub-system, r = %lu\n", |
| 1475 | r & ~EFI_ERROR_MASK); |
| 1476 | return CMD_RET_FAILURE; |
| 1477 | } |
| 1478 | |
| 1479 | cp = find_cmd_tbl(argv[0], cmd_efidebug_sub, |
| 1480 | ARRAY_SIZE(cmd_efidebug_sub)); |
| 1481 | if (!cp) |
| 1482 | return CMD_RET_USAGE; |
| 1483 | |
| 1484 | return cp->cmd(cmdtp, flag, argc, argv); |
| 1485 | } |
| 1486 | |
| 1487 | #ifdef CONFIG_SYS_LONGHELP |
| 1488 | static char efidebug_help_text[] = |
| 1489 | " - UEFI Shell-like interface to configure UEFI environment\n" |
| 1490 | "\n" |
Heinrich Schuchardt | 63276a5 | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 1491 | "efidebug boot add - set UEFI BootXXXX variable\n" |
| 1492 | " -b|-B <bootid> <label> <interface> <devnum>[:<part>] <file path>\n" |
| 1493 | " -i|-I <interface> <devnum>[:<part>] <initrd file path>\n" |
| 1494 | " (-b, -i for short form device path)\n" |
| 1495 | " -s '<optional data>'\n" |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1496 | "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n" |
| 1497 | " - delete UEFI BootXXXX variables\n" |
| 1498 | "efidebug boot dump\n" |
| 1499 | " - dump all UEFI BootXXXX variables\n" |
| 1500 | "efidebug boot next <bootid>\n" |
| 1501 | " - set UEFI BootNext variable\n" |
| 1502 | "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n" |
| 1503 | " - set/show UEFI boot order\n" |
AKASHI Takahiro | 355cdb5 | 2019-02-25 15:54:39 +0900 | [diff] [blame] | 1504 | "\n" |
AKASHI Takahiro | 7f35ced | 2020-11-17 09:28:01 +0900 | [diff] [blame] | 1505 | #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT |
| 1506 | "efidebug capsule update [-v] <capsule address>\n" |
| 1507 | " - process a capsule\n" |
Sughosh Ganu | 7407595 | 2020-12-30 19:27:11 +0530 | [diff] [blame] | 1508 | "efidebug capsule disk-update\n" |
| 1509 | " - update a capsule from disk\n" |
AKASHI Takahiro | 7f35ced | 2020-11-17 09:28:01 +0900 | [diff] [blame] | 1510 | "efidebug capsule show <capsule address>\n" |
| 1511 | " - show capsule information\n" |
| 1512 | "efidebug capsule result [<capsule result var>]\n" |
| 1513 | " - show a capsule update result\n" |
Jose Marinho | aa31a87 | 2021-03-11 13:18:51 +0000 | [diff] [blame] | 1514 | #ifdef CONFIG_EFI_ESRT |
| 1515 | "efidebug capsule esrt\n" |
| 1516 | " - print the ESRT\n" |
| 1517 | #endif |
AKASHI Takahiro | 7f35ced | 2020-11-17 09:28:01 +0900 | [diff] [blame] | 1518 | "\n" |
| 1519 | #endif |
AKASHI Takahiro | 66eaf56 | 2019-02-25 15:54:40 +0900 | [diff] [blame] | 1520 | "efidebug drivers\n" |
Heinrich Schuchardt | 07e2fe7 | 2020-01-07 07:48:15 +0100 | [diff] [blame] | 1521 | " - show UEFI drivers\n" |
AKASHI Takahiro | a801462 | 2019-02-25 15:54:41 +0900 | [diff] [blame] | 1522 | "efidebug dh\n" |
Heinrich Schuchardt | 07e2fe7 | 2020-01-07 07:48:15 +0100 | [diff] [blame] | 1523 | " - show UEFI handles\n" |
AKASHI Takahiro | fa53673 | 2019-02-25 15:54:42 +0900 | [diff] [blame] | 1524 | "efidebug images\n" |
AKASHI Takahiro | 00358bb | 2019-02-25 15:54:43 +0900 | [diff] [blame] | 1525 | " - show loaded images\n" |
| 1526 | "efidebug memmap\n" |
Heinrich Schuchardt | 07e2fe7 | 2020-01-07 07:48:15 +0100 | [diff] [blame] | 1527 | " - show UEFI memory map\n" |
Heinrich Schuchardt | 986e064 | 2020-01-07 05:57:47 +0100 | [diff] [blame] | 1528 | "efidebug tables\n" |
AKASHI Takahiro | 525fc06 | 2020-04-14 11:51:48 +0900 | [diff] [blame] | 1529 | " - show UEFI configuration tables\n" |
Heinrich Schuchardt | ff2f532 | 2021-01-15 19:02:50 +0100 | [diff] [blame] | 1530 | #ifdef CONFIG_CMD_BOOTEFI_BOOTMGR |
AKASHI Takahiro | 525fc06 | 2020-04-14 11:51:48 +0900 | [diff] [blame] | 1531 | "efidebug test bootmgr\n" |
Ilias Apalodimas | b0e4f2c | 2020-05-17 22:25:45 +0300 | [diff] [blame] | 1532 | " - run simple bootmgr for test\n" |
Heinrich Schuchardt | ff2f532 | 2021-01-15 19:02:50 +0100 | [diff] [blame] | 1533 | #endif |
Ilias Apalodimas | b0e4f2c | 2020-05-17 22:25:45 +0300 | [diff] [blame] | 1534 | "efidebug query [-nv][-bs][-rt][-at]\n" |
| 1535 | " - show size of UEFI variables store\n"; |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1536 | #endif |
| 1537 | |
| 1538 | U_BOOT_CMD( |
Ilias Apalodimas | cbea241 | 2021-03-17 21:55:01 +0200 | [diff] [blame] | 1539 | efidebug, CONFIG_SYS_MAXARGS, 0, do_efidebug, |
AKASHI Takahiro | 59df7e7 | 2019-02-25 15:54:38 +0900 | [diff] [blame] | 1540 | "Configure UEFI environment", |
| 1541 | efidebug_help_text |
| 1542 | ); |