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