1 // SPDX-License-Identifier: GPL-2.0+
3 * UEFI Shell-like command
5 * Copyright (c) 2018 AKASHI Takahiro, Linaro Limited
11 #include <dm/device.h>
12 #include <efi_dt_fixup.h>
13 #include <efi_load_initrd.h>
14 #include <efi_loader.h>
16 #include <efi_variable.h>
24 #include <linux/ctype.h>
25 #include <linux/err.h>
27 #define BS systab.boottime
28 #define RT systab.runtime
30 #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
32 * do_efi_capsule_update() - process a capsule update
34 * @cmdtp: Command table
36 * @argc: Number of arguments
37 * @argv: Argument array
38 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
40 * Implement efidebug "capsule update" sub-command.
41 * process a capsule update.
43 * efidebug capsule update [-v] <capsule address>
45 static int do_efi_capsule_update(struct cmd_tbl *cmdtp, int flag,
46 int argc, char * const argv[])
48 struct efi_capsule_header *capsule;
53 if (argc != 2 && argc != 3)
57 if (strcmp(argv[1], "-v"))
65 capsule = (typeof(capsule))hextoul(argv[1], &endp);
66 if (endp == argv[1]) {
67 printf("Invalid address: %s", argv[1]);
68 return CMD_RET_FAILURE;
72 printf("Capsule guid: %pUl\n", &capsule->capsule_guid);
73 printf("Capsule flags: 0x%x\n", capsule->flags);
74 printf("Capsule header size: 0x%x\n", capsule->header_size);
75 printf("Capsule image size: 0x%x\n",
76 capsule->capsule_image_size);
79 ret = EFI_CALL(RT->update_capsule(&capsule, 1, 0));
81 printf("Cannot handle a capsule at %p\n", capsule);
82 return CMD_RET_FAILURE;
85 return CMD_RET_SUCCESS;
88 #ifdef CONFIG_EFI_CAPSULE_ON_DISK
89 static int do_efi_capsule_on_disk_update(struct cmd_tbl *cmdtp, int flag,
90 int argc, char * const argv[])
94 ret = efi_launch_capsules();
96 return ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
101 * do_efi_capsule_show() - show capsule information
103 * @cmdtp: Command table
104 * @flag: Command flag
105 * @argc: Number of arguments
106 * @argv: Argument array
107 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
109 * Implement efidebug "capsule show" sub-command.
110 * show capsule information.
112 * efidebug capsule show <capsule address>
114 static int do_efi_capsule_show(struct cmd_tbl *cmdtp, int flag,
115 int argc, char * const argv[])
117 struct efi_capsule_header *capsule;
121 return CMD_RET_USAGE;
123 capsule = (typeof(capsule))hextoul(argv[1], &endp);
124 if (endp == argv[1]) {
125 printf("Invalid address: %s", argv[1]);
126 return CMD_RET_FAILURE;
129 printf("Capsule guid: %pUl\n", &capsule->capsule_guid);
130 printf("Capsule flags: 0x%x\n", capsule->flags);
131 printf("Capsule header size: 0x%x\n", capsule->header_size);
132 printf("Capsule image size: 0x%x\n",
133 capsule->capsule_image_size);
135 return CMD_RET_SUCCESS;
138 #ifdef CONFIG_EFI_ESRT
140 #define EFI_ESRT_FW_TYPE_NUM 4
141 char *efi_fw_type_str[EFI_ESRT_FW_TYPE_NUM] = {"unknown", "system FW", "device FW",
144 #define EFI_ESRT_UPDATE_STATUS_NUM 9
145 char *efi_update_status_str[EFI_ESRT_UPDATE_STATUS_NUM] = {"success", "unsuccessful",
146 "insufficient resources", "incorrect version", "invalid format",
147 "auth error", "power event (AC)", "power event (batt)",
148 "unsatisfied dependencies"};
150 #define EFI_FW_TYPE_STR_GET(idx) (\
151 EFI_ESRT_FW_TYPE_NUM > (idx) ? efi_fw_type_str[(idx)] : "error"\
154 #define EFI_FW_STATUS_STR_GET(idx) (\
155 EFI_ESRT_UPDATE_STATUS_NUM > (idx) ? efi_update_status_str[(idx)] : "error"\
159 * do_efi_capsule_esrt() - manage UEFI capsules
161 * @cmdtp: Command table
162 * @flag: Command flag
163 * @argc: Number of arguments
164 * @argv: Argument array
165 * Return: CMD_RET_SUCCESS on success,
166 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
168 * Implement efidebug "capsule esrt" sub-command.
169 * The prints the current ESRT table.
171 * efidebug capsule esrt
173 static int do_efi_capsule_esrt(struct cmd_tbl *cmdtp, int flag,
174 int argc, char * const argv[])
176 struct efi_system_resource_table *esrt = NULL;
179 return CMD_RET_USAGE;
181 for (int idx = 0; idx < systab.nr_tables; idx++)
182 if (!guidcmp(&efi_esrt_guid, &systab.tables[idx].guid))
183 esrt = (struct efi_system_resource_table *)systab.tables[idx].table;
186 log_info("ESRT: table not present\n");
187 return CMD_RET_SUCCESS;
190 printf("========================================\n");
191 printf("ESRT: fw_resource_count=%d\n", esrt->fw_resource_count);
192 printf("ESRT: fw_resource_count_max=%d\n", esrt->fw_resource_count_max);
193 printf("ESRT: fw_resource_version=%lld\n", esrt->fw_resource_version);
195 for (int idx = 0; idx < esrt->fw_resource_count; idx++) {
196 printf("[entry %d]==============================\n", idx);
197 printf("ESRT: fw_class=%pUL\n", &esrt->entries[idx].fw_class);
198 printf("ESRT: fw_type=%s\n", EFI_FW_TYPE_STR_GET(esrt->entries[idx].fw_type));
199 printf("ESRT: fw_version=%d\n", esrt->entries[idx].fw_version);
200 printf("ESRT: lowest_supported_fw_version=%d\n",
201 esrt->entries[idx].lowest_supported_fw_version);
202 printf("ESRT: capsule_flags=%d\n",
203 esrt->entries[idx].capsule_flags);
204 printf("ESRT: last_attempt_version=%d\n",
205 esrt->entries[idx].last_attempt_version);
206 printf("ESRT: last_attempt_status=%s\n",
207 EFI_FW_STATUS_STR_GET(esrt->entries[idx].last_attempt_status));
209 printf("========================================\n");
211 return CMD_RET_SUCCESS;
213 #endif /* CONFIG_EFI_ESRT */
215 * do_efi_capsule_res() - show a capsule update result
217 * @cmdtp: Command table
218 * @flag: Command flag
219 * @argc: Number of arguments
220 * @argv: Argument array
221 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
223 * Implement efidebug "capsule result" sub-command.
224 * show a capsule update result.
225 * If result number is not specified, CapsuleLast will be shown.
227 * efidebug capsule result [<capsule result number>]
229 static int do_efi_capsule_res(struct cmd_tbl *cmdtp, int flag,
230 int argc, char * const argv[])
236 struct efi_capsule_result_variable_header *result = NULL;
240 if (argc != 1 && argc != 2)
241 return CMD_RET_USAGE;
243 guid = efi_guid_capsule_report;
245 size = sizeof(var_name16);
246 ret = efi_get_variable_int(u"CapsuleLast", &guid, NULL,
247 &size, var_name16, NULL);
249 if (ret != EFI_SUCCESS) {
250 if (ret == EFI_NOT_FOUND)
251 printf("CapsuleLast doesn't exist\n");
253 printf("Failed to get CapsuleLast\n");
255 return CMD_RET_FAILURE;
257 printf("CapsuleLast is %ls\n", var_name16);
262 capsule_id = hextoul(argv[0], &endp);
263 if (capsule_id < 0 || capsule_id > 0xffff)
264 return CMD_RET_USAGE;
266 efi_create_indexed_name(var_name16, sizeof(var_name16),
267 "Capsule", capsule_id);
271 ret = efi_get_variable_int(var_name16, &guid, NULL, &size, NULL, NULL);
272 if (ret == EFI_BUFFER_TOO_SMALL) {
273 result = malloc(size);
275 return CMD_RET_FAILURE;
276 ret = efi_get_variable_int(var_name16, &guid, NULL, &size,
279 if (ret != EFI_SUCCESS) {
281 printf("Failed to get %ls\n", var_name16);
283 return CMD_RET_FAILURE;
286 printf("Result total size: 0x%x\n", result->variable_total_size);
287 printf("Capsule guid: %pUl\n", &result->capsule_guid);
288 printf("Time processed: %04d-%02d-%02d %02d:%02d:%02d\n",
289 result->capsule_processed.year, result->capsule_processed.month,
290 result->capsule_processed.day, result->capsule_processed.hour,
291 result->capsule_processed.minute,
292 result->capsule_processed.second);
293 printf("Capsule status: 0x%lx\n", result->capsule_status);
297 return CMD_RET_SUCCESS;
300 static struct cmd_tbl cmd_efidebug_capsule_sub[] = {
301 U_BOOT_CMD_MKENT(update, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_update,
303 U_BOOT_CMD_MKENT(show, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_show,
305 #ifdef CONFIG_EFI_ESRT
306 U_BOOT_CMD_MKENT(esrt, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_esrt,
309 #ifdef CONFIG_EFI_CAPSULE_ON_DISK
310 U_BOOT_CMD_MKENT(disk-update, 0, 0, do_efi_capsule_on_disk_update,
313 U_BOOT_CMD_MKENT(result, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_res,
318 * do_efi_capsule() - manage UEFI capsules
320 * @cmdtp: Command table
321 * @flag: Command flag
322 * @argc: Number of arguments
323 * @argv: Argument array
324 * Return: CMD_RET_SUCCESS on success,
325 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
327 * Implement efidebug "capsule" sub-command.
329 static int do_efi_capsule(struct cmd_tbl *cmdtp, int flag,
330 int argc, char * const argv[])
335 return CMD_RET_USAGE;
339 cp = find_cmd_tbl(argv[0], cmd_efidebug_capsule_sub,
340 ARRAY_SIZE(cmd_efidebug_capsule_sub));
342 return CMD_RET_USAGE;
344 return cp->cmd(cmdtp, flag, argc, argv);
346 #endif /* CONFIG_EFI_HAVE_CAPSULE_SUPPORT */
348 #define EFI_HANDLE_WIDTH ((int)sizeof(efi_handle_t) * 2)
350 static const char spc[] = " ";
351 static const char sep[] = "================";
354 * efi_get_driver_handle_info() - get information of UEFI driver
356 * @handle: Handle of UEFI device
357 * @driver_name: Driver name
358 * @image_path: Pointer to text of device path
359 * Return: 0 on success, -1 on failure
361 * Currently return no useful information as all UEFI drivers are
364 static int efi_get_driver_handle_info(efi_handle_t handle, u16 **driver_name,
367 struct efi_handler *handler;
368 struct efi_loaded_image *image;
373 * TODO: support EFI_COMPONENT_NAME2_PROTOCOL
378 ret = efi_search_protocol(handle, &efi_guid_loaded_image, &handler);
379 if (ret != EFI_SUCCESS) {
384 image = handler->protocol_interface;
385 *image_path = efi_dp_str(image->file_path);
391 * do_efi_show_drivers() - show UEFI drivers
393 * @cmdtp: Command table
394 * @flag: Command flag
395 * @argc: Number of arguments
396 * @argv: Argument array
397 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
399 * Implement efidebug "drivers" sub-command.
400 * Show all UEFI drivers and their information.
402 static int do_efi_show_drivers(struct cmd_tbl *cmdtp, int flag,
403 int argc, char *const argv[])
405 efi_handle_t *handles;
407 u16 *driver_name, *image_path_text;
410 ret = EFI_CALL(efi_locate_handle_buffer(
411 BY_PROTOCOL, &efi_guid_driver_binding_protocol,
412 NULL, &num, &handles));
413 if (ret != EFI_SUCCESS)
414 return CMD_RET_FAILURE;
417 return CMD_RET_SUCCESS;
419 printf("Driver%.*s Name Image Path\n",
420 EFI_HANDLE_WIDTH - 6, spc);
421 printf("%.*s ==================== ====================\n",
422 EFI_HANDLE_WIDTH, sep);
423 for (i = 0; i < num; i++) {
424 if (!efi_get_driver_handle_info(handles[i], &driver_name,
427 printf("%p %-20ls %ls\n", handles[i],
428 driver_name, image_path_text);
430 printf("%p %-20ls <built-in>\n",
431 handles[i], driver_name);
432 efi_free_pool(driver_name);
433 efi_free_pool(image_path_text);
437 efi_free_pool(handles);
439 return CMD_RET_SUCCESS;
443 * do_efi_show_handles() - show UEFI handles
445 * @cmdtp: Command table
446 * @flag: Command flag
447 * @argc: Number of arguments
448 * @argv: Argument array
449 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
451 * Implement efidebug "dh" sub-command.
452 * Show all UEFI handles and their information, currently all protocols
455 static int do_efi_show_handles(struct cmd_tbl *cmdtp, int flag,
456 int argc, char *const argv[])
458 efi_handle_t *handles;
460 efi_uintn_t num, count, i, j;
463 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
465 if (ret != EFI_SUCCESS)
466 return CMD_RET_FAILURE;
469 return CMD_RET_SUCCESS;
471 for (i = 0; i < num; i++) {
472 struct efi_handler *handler;
474 printf("\n%p", handles[i]);
476 printf(" (%s)", handles[i]->dev->name);
478 /* Print device path */
479 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
481 if (ret == EFI_SUCCESS)
482 printf(" %pD\n", handler->protocol_interface);
483 ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid,
485 /* Print other protocols */
486 for (j = 0; j < count; j++) {
487 if (guidcmp(guid[j], &efi_guid_device_path))
488 printf(" %pUs\n", guid[j]);
492 efi_free_pool(handles);
494 return CMD_RET_SUCCESS;
498 * do_efi_show_images() - show UEFI images
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
506 * Implement efidebug "images" sub-command.
507 * Show all UEFI loaded images and their information.
509 static int do_efi_show_images(struct cmd_tbl *cmdtp, int flag,
510 int argc, char *const argv[])
512 efi_print_image_infos(NULL);
514 return CMD_RET_SUCCESS;
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",
532 [EFI_PERSISTENT_MEMORY_TYPE] = "PERSISTENT",
535 static const struct efi_mem_attrs {
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"},
551 {EFI_MEMORY_SP, "SP"},
552 {EFI_MEMORY_RUNTIME, "RT"},
556 * print_memory_attributes() - print memory map attributes
558 * @attributes: Attribute value
560 * Print memory map attributes
562 static void print_memory_attributes(u64 attributes)
566 for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
567 if (attributes & efi_mem_attrs[i].bit) {
574 puts(efi_mem_attrs[i].text);
578 #define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
581 * do_efi_show_memmap() - show UEFI memory map
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
589 * Implement efidebug "memmap" sub-command.
590 * Show UEFI memory map.
592 static int do_efi_show_memmap(struct cmd_tbl *cmdtp, int flag,
593 int argc, char *const argv[])
595 struct efi_mem_desc *memmap = NULL, *map;
596 efi_uintn_t map_size = 0;
601 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
602 if (ret == EFI_BUFFER_TOO_SMALL) {
603 map_size += sizeof(struct efi_mem_desc); /* for my own */
604 ret = efi_allocate_pool(EFI_LOADER_DATA, map_size,
606 if (ret != EFI_SUCCESS)
607 return CMD_RET_FAILURE;
608 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
610 if (ret != EFI_SUCCESS) {
611 efi_free_pool(memmap);
612 return CMD_RET_FAILURE;
615 printf("Type Start%.*s End%.*s Attributes\n",
616 EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc);
617 printf("================ %.*s %.*s ==========\n",
618 EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep);
620 * Coverity check: dereferencing null pointer "map."
621 * This is a false positive as memmap will always be
622 * populated by allocate_pool() above.
624 for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
625 if (map->type < ARRAY_SIZE(efi_mem_type_string))
626 type = efi_mem_type_string[map->type];
630 printf("%-16s %.*llx-%.*llx", type,
632 (u64)map_to_sysmem((void *)(uintptr_t)
633 map->physical_start),
635 (u64)map_to_sysmem((void *)(uintptr_t)
636 (map->physical_start +
637 map->num_pages * EFI_PAGE_SIZE)));
639 print_memory_attributes(map->attribute);
643 efi_free_pool(memmap);
645 return CMD_RET_SUCCESS;
649 * do_efi_show_tables() - show UEFI configuration tables
651 * @cmdtp: Command table
652 * @flag: Command flag
653 * @argc: Number of arguments
654 * @argv: Argument array
655 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
657 * Implement efidebug "tables" sub-command.
658 * Show UEFI configuration tables.
660 static int do_efi_show_tables(struct cmd_tbl *cmdtp, int flag,
661 int argc, char *const argv[])
665 for (i = 0; i < systab.nr_tables; ++i)
666 printf("%pUl (%pUs)\n",
667 &systab.tables[i].guid, &systab.tables[i].guid);
669 return CMD_RET_SUCCESS;
673 * create_initrd_dp() - create a special device for our Boot### option
676 * @part: disk partition
678 * @shortform: create short form device path
679 * Return: pointer to the device path or ERR_PTR
682 struct efi_device_path *create_initrd_dp(const char *dev, const char *part,
683 const char *file, int shortform)
686 struct efi_device_path *tmp_dp = NULL, *tmp_fp = NULL, *short_fp = NULL;
687 struct efi_device_path *initrd_dp = NULL;
689 const struct efi_initrd_dp id_dp = {
692 DEVICE_PATH_TYPE_MEDIA_DEVICE,
693 DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
694 sizeof(id_dp.vendor),
696 EFI_INITRD_MEDIA_GUID,
699 DEVICE_PATH_TYPE_END,
700 DEVICE_PATH_SUB_TYPE_END,
705 ret = efi_dp_from_name(dev, part, file, &tmp_dp, &tmp_fp);
706 if (ret != EFI_SUCCESS) {
707 printf("Cannot create device path for \"%s %s\"\n", part, file);
711 short_fp = efi_dp_shorten(tmp_fp);
715 initrd_dp = efi_dp_append((const struct efi_device_path *)&id_dp,
719 efi_free_pool(tmp_dp);
720 efi_free_pool(tmp_fp);
725 * do_efi_boot_add() - set UEFI load option
727 * @cmdtp: Command table
728 * @flag: Command flag
729 * @argc: Number of arguments
730 * @argv: Argument array
731 * Return: CMD_RET_SUCCESS on success,
732 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
734 * Implement efidebug "boot add" sub-command. Create or change UEFI load option.
736 * efidebug boot add -b <id> <label> <interface> <devnum>[:<part>] <file>
737 * -i <file> <interface2> <devnum2>[:<part>] <initrd>
740 static int do_efi_boot_add(struct cmd_tbl *cmdtp, int flag,
741 int argc, char *const argv[])
748 struct efi_device_path *file_path = NULL;
749 struct efi_device_path *fp_free = NULL;
750 struct efi_device_path *final_fp = NULL;
751 struct efi_device_path *initrd_dp = NULL;
752 struct efi_load_option lo;
755 efi_uintn_t fp_size = 0;
757 int r = CMD_RET_SUCCESS;
759 guid = efi_global_variable_guid;
762 lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
763 lo.optional_data = NULL;
768 for (; argc > 0; argc--, argv++) {
771 if (*argv[0] != '-' || strlen(argv[0]) != 2) {
776 switch (argv[0][1]) {
781 if (argc < 5 || lo.label) {
785 id = (int)hextoul(argv[1], &endp);
786 if (*endp != '\0' || id > 0xffff)
787 return CMD_RET_USAGE;
789 efi_create_indexed_name(var_name16, sizeof(var_name16),
793 label = efi_convert_string(argv[2]);
795 return CMD_RET_FAILURE;
796 lo.label = label; /* label will be changed below */
799 ret = efi_dp_from_name(argv[3], argv[4], argv[5],
801 if (ret != EFI_SUCCESS) {
802 printf("Cannot create device path for \"%s %s\"\n",
808 file_path = efi_dp_shorten(fp_free);
811 fp_size += efi_dp_size(file_path) +
812 sizeof(struct efi_device_path);
820 if (argc < 3 || initrd_dp) {
825 initrd_dp = create_initrd_dp(argv[1], argv[2], argv[3],
828 printf("Cannot add an initrd\n");
834 fp_size += efi_dp_size(initrd_dp) +
835 sizeof(struct efi_device_path);
838 if (argc < 1 || lo.optional_data) {
842 lo.optional_data = (const u8 *)argv[1];
853 printf("Missing binary\n");
858 final_fp = efi_dp_concat(file_path, initrd_dp);
860 printf("Cannot create final device path\n");
865 lo.file_path = final_fp;
866 lo.file_path_length = fp_size;
868 size = efi_serialize_load_option(&lo, (u8 **)&data);
874 ret = efi_set_variable_int(var_name16, &guid,
875 EFI_VARIABLE_NON_VOLATILE |
876 EFI_VARIABLE_BOOTSERVICE_ACCESS |
877 EFI_VARIABLE_RUNTIME_ACCESS,
879 if (ret != EFI_SUCCESS) {
880 printf("Cannot set %ls\n", var_name16);
886 efi_free_pool(final_fp);
887 efi_free_pool(initrd_dp);
888 efi_free_pool(fp_free);
895 * do_efi_boot_rm() - delete UEFI load options
897 * @cmdtp: Command table
898 * @flag: Command flag
899 * @argc: Number of arguments
900 * @argv: Argument array
901 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
903 * Implement efidebug "boot rm" sub-command.
904 * Delete UEFI load options.
906 * efidebug boot rm <id> ...
908 static int do_efi_boot_rm(struct cmd_tbl *cmdtp, int flag,
909 int argc, char *const argv[])
918 return CMD_RET_USAGE;
920 guid = efi_global_variable_guid;
921 for (i = 1; i < argc; i++, argv++) {
922 id = (int)hextoul(argv[1], &endp);
923 if (*endp != '\0' || id > 0xffff)
924 return CMD_RET_FAILURE;
926 efi_create_indexed_name(var_name16, sizeof(var_name16),
928 ret = efi_set_variable_int(var_name16, &guid, 0, 0, NULL,
931 printf("Cannot remove %ls\n", var_name16);
932 return CMD_RET_FAILURE;
936 return CMD_RET_SUCCESS;
940 * show_efi_boot_opt_data() - dump UEFI load option
942 * @varname16: variable name
943 * @data: value of UEFI load option variable
944 * @size: size of the boot option
946 * Decode the value of UEFI load option variable and print information.
948 static void show_efi_boot_opt_data(u16 *varname16, void *data, size_t *size)
950 struct efi_device_path *initrd_path = NULL;
951 struct efi_load_option lo;
954 ret = efi_deserialize_load_option(&lo, data, size);
955 if (ret != EFI_SUCCESS) {
956 printf("%ls: invalid load option\n", varname16);
960 printf("%ls:\nattributes: %c%c%c (0x%08x)\n",
963 lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
964 /* FORCE RECONNECT */
965 lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
967 lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
969 printf(" label: %ls\n", lo.label);
971 printf(" file_path: %pD\n", lo.file_path);
973 initrd_path = efi_dp_from_lo(&lo, &efi_lf2_initrd_guid);
975 printf(" initrd_path: %pD\n", initrd_path);
976 efi_free_pool(initrd_path);
980 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
981 lo.optional_data, *size, true);
985 * show_efi_boot_opt() - dump UEFI load option
987 * @varname16: variable name
989 * Dump information defined by UEFI load option.
991 static void show_efi_boot_opt(u16 *varname16)
998 ret = EFI_CALL(efi_get_variable(varname16, &efi_global_variable_guid,
1000 if (ret == EFI_BUFFER_TOO_SMALL) {
1001 data = malloc(size);
1003 printf("ERROR: Out of memory\n");
1006 ret = EFI_CALL(efi_get_variable(varname16,
1007 &efi_global_variable_guid,
1008 NULL, &size, data));
1009 if (ret == EFI_SUCCESS)
1010 show_efi_boot_opt_data(varname16, data, &size);
1015 static int u16_tohex(u16 c)
1017 if (c >= '0' && c <= '9')
1019 if (c >= 'A' && c <= 'F')
1020 return c - 'A' + 10;
1022 /* not hexadecimal */
1027 * show_efi_boot_dump() - dump all UEFI load options
1029 * @cmdtp: Command table
1030 * @flag: Command flag
1031 * @argc: Number of arguments
1032 * @argv: Argument array
1033 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1035 * Implement efidebug "boot dump" sub-command.
1036 * Dump information of all UEFI load options defined.
1038 * efidebug boot dump
1040 static int do_efi_boot_dump(struct cmd_tbl *cmdtp, int flag,
1041 int argc, char *const argv[])
1043 u16 *var_name16, *p;
1044 efi_uintn_t buf_size, size;
1050 return CMD_RET_USAGE;
1053 var_name16 = malloc(buf_size);
1055 return CMD_RET_FAILURE;
1060 ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
1062 if (ret == EFI_NOT_FOUND)
1064 if (ret == EFI_BUFFER_TOO_SMALL) {
1066 p = realloc(var_name16, buf_size);
1069 return CMD_RET_FAILURE;
1072 ret = EFI_CALL(efi_get_next_variable_name(&size,
1076 if (ret != EFI_SUCCESS) {
1078 return CMD_RET_FAILURE;
1081 if (memcmp(var_name16, u"Boot", 8))
1084 for (id = 0, i = 0; i < 4; i++) {
1085 digit = u16_tohex(var_name16[4 + i]);
1088 id = (id << 4) + digit;
1090 if (i == 4 && !var_name16[8])
1091 show_efi_boot_opt(var_name16);
1096 return CMD_RET_SUCCESS;
1100 * show_efi_boot_order() - show order of UEFI load options
1102 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1104 * Show order of UEFI load options defined by BootOrder variable.
1106 static int show_efi_boot_order(void)
1113 struct efi_load_option lo;
1117 ret = EFI_CALL(efi_get_variable(u"BootOrder", &efi_global_variable_guid,
1118 NULL, &size, NULL));
1119 if (ret != EFI_BUFFER_TOO_SMALL) {
1120 if (ret == EFI_NOT_FOUND) {
1121 printf("BootOrder not defined\n");
1122 return CMD_RET_SUCCESS;
1124 return CMD_RET_FAILURE;
1127 bootorder = malloc(size);
1129 printf("ERROR: Out of memory\n");
1130 return CMD_RET_FAILURE;
1132 ret = EFI_CALL(efi_get_variable(u"BootOrder", &efi_global_variable_guid,
1133 NULL, &size, bootorder));
1134 if (ret != EFI_SUCCESS) {
1135 ret = CMD_RET_FAILURE;
1139 num = size / sizeof(u16);
1140 for (i = 0; i < num; i++) {
1141 efi_create_indexed_name(var_name16, sizeof(var_name16),
1142 "Boot", bootorder[i]);
1145 ret = EFI_CALL(efi_get_variable(var_name16,
1146 &efi_global_variable_guid, NULL,
1148 if (ret != EFI_BUFFER_TOO_SMALL) {
1149 printf("%2d: %ls: (not defined)\n", i + 1, var_name16);
1153 data = malloc(size);
1155 ret = CMD_RET_FAILURE;
1158 ret = EFI_CALL(efi_get_variable(var_name16,
1159 &efi_global_variable_guid, NULL,
1161 if (ret != EFI_SUCCESS) {
1163 ret = CMD_RET_FAILURE;
1167 ret = efi_deserialize_load_option(&lo, data, &size);
1168 if (ret != EFI_SUCCESS) {
1169 printf("%ls: invalid load option\n", var_name16);
1170 ret = CMD_RET_FAILURE;
1174 printf("%2d: %ls: %ls\n", i + 1, var_name16, lo.label);
1185 * do_efi_boot_next() - manage UEFI BootNext variable
1187 * @cmdtp: Command table
1188 * @flag: Command flag
1189 * @argc: Number of arguments
1190 * @argv: Argument array
1191 * Return: CMD_RET_SUCCESS on success,
1192 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1194 * Implement efidebug "boot next" sub-command.
1195 * Set BootNext variable.
1197 * efidebug boot next <id>
1199 static int do_efi_boot_next(struct cmd_tbl *cmdtp, int flag,
1200 int argc, char *const argv[])
1207 int r = CMD_RET_SUCCESS;
1210 return CMD_RET_USAGE;
1212 bootnext = (u16)hextoul(argv[1], &endp);
1214 printf("invalid value: %s\n", argv[1]);
1215 r = CMD_RET_FAILURE;
1219 guid = efi_global_variable_guid;
1221 ret = efi_set_variable_int(u"BootNext", &guid,
1222 EFI_VARIABLE_NON_VOLATILE |
1223 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1224 EFI_VARIABLE_RUNTIME_ACCESS,
1225 size, &bootnext, false);
1226 if (ret != EFI_SUCCESS) {
1227 printf("Cannot set BootNext\n");
1228 r = CMD_RET_FAILURE;
1235 * do_efi_boot_order() - manage UEFI BootOrder variable
1237 * @cmdtp: Command table
1238 * @flag: Command flag
1239 * @argc: Number of arguments
1240 * @argv: Argument array
1241 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1243 * Implement efidebug "boot order" sub-command.
1244 * Show order of UEFI load options, or change it in BootOrder variable.
1246 * efidebug boot order [<id> ...]
1248 static int do_efi_boot_order(struct cmd_tbl *cmdtp, int flag,
1249 int argc, char *const argv[])
1251 u16 *bootorder = NULL;
1257 int r = CMD_RET_SUCCESS;
1260 return show_efi_boot_order();
1265 size = argc * sizeof(u16);
1266 bootorder = malloc(size);
1268 return CMD_RET_FAILURE;
1270 for (i = 0; i < argc; i++) {
1271 id = (int)hextoul(argv[i], &endp);
1272 if (*endp != '\0' || id > 0xffff) {
1273 printf("invalid value: %s\n", argv[i]);
1274 r = CMD_RET_FAILURE;
1278 bootorder[i] = (u16)id;
1281 guid = efi_global_variable_guid;
1282 ret = efi_set_variable_int(u"BootOrder", &guid,
1283 EFI_VARIABLE_NON_VOLATILE |
1284 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1285 EFI_VARIABLE_RUNTIME_ACCESS,
1286 size, bootorder, true);
1287 if (ret != EFI_SUCCESS) {
1288 printf("Cannot set BootOrder\n");
1289 r = CMD_RET_FAILURE;
1297 static struct cmd_tbl cmd_efidebug_boot_sub[] = {
1298 U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
1299 U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
1300 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
1301 U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
1302 U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
1307 * do_efi_boot_opt() - manage UEFI load options
1309 * @cmdtp: Command table
1310 * @flag: Command flag
1311 * @argc: Number of arguments
1312 * @argv: Argument array
1313 * Return: CMD_RET_SUCCESS on success,
1314 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1316 * Implement efidebug "boot" sub-command.
1318 static int do_efi_boot_opt(struct cmd_tbl *cmdtp, int flag,
1319 int argc, char *const argv[])
1324 return CMD_RET_USAGE;
1328 cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
1329 ARRAY_SIZE(cmd_efidebug_boot_sub));
1331 return CMD_RET_USAGE;
1333 return cp->cmd(cmdtp, flag, argc, argv);
1337 * do_efi_test_bootmgr() - run simple bootmgr for test
1339 * @cmdtp: Command table
1340 * @flag: Command flag
1341 * @argc: Number of arguments
1342 * @argv: Argument array
1343 * Return: CMD_RET_SUCCESS on success,
1344 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1346 * Implement efidebug "test bootmgr" sub-command.
1347 * Run simple bootmgr for test.
1349 * efidebug test bootmgr
1351 static __maybe_unused int do_efi_test_bootmgr(struct cmd_tbl *cmdtp, int flag,
1352 int argc, char * const argv[])
1355 efi_uintn_t exit_data_size = 0;
1356 u16 *exit_data = NULL;
1358 void *load_options = NULL;
1360 ret = efi_bootmgr_load(&image, &load_options);
1361 printf("efi_bootmgr_load() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1363 /* We call efi_start_image() even if error for test purpose. */
1364 ret = EFI_CALL(efi_start_image(image, &exit_data_size, &exit_data));
1365 printf("efi_start_image() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1366 if (ret && exit_data)
1367 efi_free_pool(exit_data);
1372 return CMD_RET_SUCCESS;
1375 static struct cmd_tbl cmd_efidebug_test_sub[] = {
1376 #ifdef CONFIG_CMD_BOOTEFI_BOOTMGR
1377 U_BOOT_CMD_MKENT(bootmgr, CONFIG_SYS_MAXARGS, 1, do_efi_test_bootmgr,
1383 * do_efi_test() - manage UEFI load options
1385 * @cmdtp: Command table
1386 * @flag: Command flag
1387 * @argc: Number of arguments
1388 * @argv: Argument array
1389 * Return: CMD_RET_SUCCESS on success,
1390 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1392 * Implement efidebug "test" sub-command.
1394 static int do_efi_test(struct cmd_tbl *cmdtp, int flag,
1395 int argc, char * const argv[])
1400 return CMD_RET_USAGE;
1404 cp = find_cmd_tbl(argv[0], cmd_efidebug_test_sub,
1405 ARRAY_SIZE(cmd_efidebug_test_sub));
1407 return CMD_RET_USAGE;
1409 return cp->cmd(cmdtp, flag, argc, argv);
1413 * do_efi_query_info() - QueryVariableInfo EFI service
1415 * @cmdtp: Command table
1416 * @flag: Command flag
1417 * @argc: Number of arguments
1418 * @argv: Argument array
1419 * Return: CMD_RET_SUCCESS on success,
1420 * CMD_RET_USAGE or CMD_RET_FAILURE on failure
1422 * Implement efidebug "test" sub-command.
1425 static int do_efi_query_info(struct cmd_tbl *cmdtp, int flag,
1426 int argc, char * const argv[])
1430 u64 max_variable_storage_size;
1431 u64 remain_variable_storage_size;
1432 u64 max_variable_size;
1435 for (i = 1; i < argc; i++) {
1436 if (!strcmp(argv[i], "-bs"))
1437 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
1438 else if (!strcmp(argv[i], "-rt"))
1439 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
1440 else if (!strcmp(argv[i], "-nv"))
1441 attr |= EFI_VARIABLE_NON_VOLATILE;
1442 else if (!strcmp(argv[i], "-at"))
1444 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
1447 ret = EFI_CALL(efi_query_variable_info(attr,
1448 &max_variable_storage_size,
1449 &remain_variable_storage_size,
1450 &max_variable_size));
1451 if (ret != EFI_SUCCESS) {
1452 printf("Error: Cannot query UEFI variables, r = %lu\n",
1453 ret & ~EFI_ERROR_MASK);
1454 return CMD_RET_FAILURE;
1457 printf("Max storage size %llu\n", max_variable_storage_size);
1458 printf("Remaining storage size %llu\n", remain_variable_storage_size);
1459 printf("Max variable size %llu\n", max_variable_size);
1461 return CMD_RET_SUCCESS;
1464 static struct cmd_tbl cmd_efidebug_sub[] = {
1465 U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
1466 #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
1467 U_BOOT_CMD_MKENT(capsule, CONFIG_SYS_MAXARGS, 1, do_efi_capsule,
1470 U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
1472 U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
1474 U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
1476 U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
1478 U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables,
1480 U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_efi_test,
1482 U_BOOT_CMD_MKENT(query, CONFIG_SYS_MAXARGS, 1, do_efi_query_info,
1487 * do_efidebug() - display and configure UEFI environment
1489 * @cmdtp: Command table
1490 * @flag: Command flag
1491 * @argc: Number of arguments
1492 * @argv: Argument array
1493 * Return: CMD_RET_SUCCESS on success,
1494 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1496 * Implement efidebug command which allows us to display and
1497 * configure UEFI environment.
1499 static int do_efidebug(struct cmd_tbl *cmdtp, int flag,
1500 int argc, char *const argv[])
1506 return CMD_RET_USAGE;
1510 /* Initialize UEFI drivers */
1511 r = efi_init_obj_list();
1512 if (r != EFI_SUCCESS) {
1513 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1514 r & ~EFI_ERROR_MASK);
1515 return CMD_RET_FAILURE;
1518 cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
1519 ARRAY_SIZE(cmd_efidebug_sub));
1521 return CMD_RET_USAGE;
1523 return cp->cmd(cmdtp, flag, argc, argv);
1526 #ifdef CONFIG_SYS_LONGHELP
1527 static char efidebug_help_text[] =
1528 " - UEFI Shell-like interface to configure UEFI environment\n"
1530 "efidebug boot add - set UEFI BootXXXX variable\n"
1531 " -b|-B <bootid> <label> <interface> <devnum>[:<part>] <file path>\n"
1532 " -i|-I <interface> <devnum>[:<part>] <initrd file path>\n"
1533 " (-b, -i for short form device path)\n"
1534 " -s '<optional data>'\n"
1535 "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
1536 " - delete UEFI BootXXXX variables\n"
1537 "efidebug boot dump\n"
1538 " - dump all UEFI BootXXXX variables\n"
1539 "efidebug boot next <bootid>\n"
1540 " - set UEFI BootNext variable\n"
1541 "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
1542 " - set/show UEFI boot order\n"
1544 #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
1545 "efidebug capsule update [-v] <capsule address>\n"
1546 " - process a capsule\n"
1547 "efidebug capsule disk-update\n"
1548 " - update a capsule from disk\n"
1549 "efidebug capsule show <capsule address>\n"
1550 " - show capsule information\n"
1551 "efidebug capsule result [<capsule result var>]\n"
1552 " - show a capsule update result\n"
1553 #ifdef CONFIG_EFI_ESRT
1554 "efidebug capsule esrt\n"
1555 " - print the ESRT\n"
1559 "efidebug drivers\n"
1560 " - show UEFI drivers\n"
1562 " - show UEFI handles\n"
1564 " - show loaded images\n"
1566 " - show UEFI memory map\n"
1568 " - show UEFI configuration tables\n"
1569 #ifdef CONFIG_CMD_BOOTEFI_BOOTMGR
1570 "efidebug test bootmgr\n"
1571 " - run simple bootmgr for test\n"
1573 "efidebug query [-nv][-bs][-rt][-at]\n"
1574 " - show size of UEFI variables store\n";
1578 efidebug, CONFIG_SYS_MAXARGS, 0, do_efidebug,
1579 "Configure UEFI environment",