1 // SPDX-License-Identifier: GPL-2.0+
3 * UEFI Shell-like command
5 * Copyright (c) 2018 AKASHI Takahiro, Linaro Limited
11 #include <efi_dt_fixup.h>
12 #include <efi_load_initrd.h>
13 #include <efi_loader.h>
22 #include <linux/ctype.h>
23 #include <linux/err.h>
25 #define BS systab.boottime
26 #define RT systab.runtime
28 #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
30 * do_efi_capsule_update() - process a capsule update
32 * @cmdtp: Command table
34 * @argc: Number of arguments
35 * @argv: Argument array
36 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
38 * Implement efidebug "capsule update" sub-command.
39 * process a capsule update.
41 * efidebug capsule update [-v] <capsule address>
43 static int do_efi_capsule_update(struct cmd_tbl *cmdtp, int flag,
44 int argc, char * const argv[])
46 struct efi_capsule_header *capsule;
51 if (argc != 2 && argc != 3)
55 if (strcmp(argv[1], "-v"))
63 capsule = (typeof(capsule))simple_strtoul(argv[1], &endp, 16);
64 if (endp == argv[1]) {
65 printf("Invalid address: %s", argv[1]);
66 return CMD_RET_FAILURE;
70 printf("Capsule guid: %pUl\n", &capsule->capsule_guid);
71 printf("Capsule flags: 0x%x\n", capsule->flags);
72 printf("Capsule header size: 0x%x\n", capsule->header_size);
73 printf("Capsule image size: 0x%x\n",
74 capsule->capsule_image_size);
77 ret = EFI_CALL(RT->update_capsule(&capsule, 1, 0));
79 printf("Cannot handle a capsule at %p", capsule);
80 return CMD_RET_FAILURE;
83 return CMD_RET_SUCCESS;
86 static int do_efi_capsule_on_disk_update(struct cmd_tbl *cmdtp, int flag,
87 int argc, char * const argv[])
91 ret = efi_launch_capsules();
93 return ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
97 * do_efi_capsule_show() - show capsule information
99 * @cmdtp: Command table
100 * @flag: Command flag
101 * @argc: Number of arguments
102 * @argv: Argument array
103 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
105 * Implement efidebug "capsule show" sub-command.
106 * show capsule information.
108 * efidebug capsule show <capsule address>
110 static int do_efi_capsule_show(struct cmd_tbl *cmdtp, int flag,
111 int argc, char * const argv[])
113 struct efi_capsule_header *capsule;
117 return CMD_RET_USAGE;
119 capsule = (typeof(capsule))simple_strtoul(argv[1], &endp, 16);
120 if (endp == argv[1]) {
121 printf("Invalid address: %s", argv[1]);
122 return CMD_RET_FAILURE;
125 printf("Capsule guid: %pUl\n", &capsule->capsule_guid);
126 printf("Capsule flags: 0x%x\n", capsule->flags);
127 printf("Capsule header size: 0x%x\n", capsule->header_size);
128 printf("Capsule image size: 0x%x\n",
129 capsule->capsule_image_size);
131 return CMD_RET_SUCCESS;
135 * do_efi_capsule_res() - show a capsule update result
137 * @cmdtp: Command table
138 * @flag: Command flag
139 * @argc: Number of arguments
140 * @argv: Argument array
141 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
143 * Implement efidebug "capsule result" sub-command.
144 * show a capsule update result.
145 * If result number is not specified, CapsuleLast will be shown.
147 * efidebug capsule result [<capsule result number>]
149 static int do_efi_capsule_res(struct cmd_tbl *cmdtp, int flag,
150 int argc, char * const argv[])
155 u16 var_name16[12], *p;
157 struct efi_capsule_result_variable_header *result = NULL;
161 if (argc != 1 && argc != 2)
162 return CMD_RET_USAGE;
164 guid = efi_guid_capsule_report;
166 size = sizeof(var_name16);
167 ret = EFI_CALL(RT->get_variable(L"CapsuleLast", &guid, NULL,
169 if (ret != EFI_SUCCESS) {
170 if (ret == EFI_NOT_FOUND)
171 printf("CapsuleLast doesn't exist\n");
173 printf("Failed to get CapsuleLast\n");
175 return CMD_RET_FAILURE;
177 printf("CapsuleLast is %ls\n", var_name16);
182 capsule_id = simple_strtoul(argv[0], &endp, 16);
183 if (capsule_id < 0 || capsule_id > 0xffff)
184 return CMD_RET_USAGE;
186 sprintf(var_name, "Capsule%04X", capsule_id);
188 utf8_utf16_strncpy(&p, var_name, 9);
192 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, NULL));
193 if (ret == EFI_BUFFER_TOO_SMALL) {
194 result = malloc(size);
196 return CMD_RET_FAILURE;
197 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
200 if (ret != EFI_SUCCESS) {
202 printf("Failed to get %ls\n", var_name16);
204 return CMD_RET_FAILURE;
207 printf("Result total size: 0x%x\n", result->variable_total_size);
208 printf("Capsule guid: %pUl\n", &result->capsule_guid);
209 printf("Time processed: %04d-%02d-%02d %02d:%02d:%02d\n",
210 result->capsule_processed.year, result->capsule_processed.month,
211 result->capsule_processed.day, result->capsule_processed.hour,
212 result->capsule_processed.minute,
213 result->capsule_processed.second);
214 printf("Capsule status: 0x%lx\n", result->capsule_status);
218 return CMD_RET_SUCCESS;
221 static struct cmd_tbl cmd_efidebug_capsule_sub[] = {
222 U_BOOT_CMD_MKENT(update, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_update,
224 U_BOOT_CMD_MKENT(show, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_show,
226 U_BOOT_CMD_MKENT(disk-update, 0, 0, do_efi_capsule_on_disk_update,
228 U_BOOT_CMD_MKENT(result, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_res,
233 * do_efi_capsule() - manage UEFI capsules
235 * @cmdtp: Command table
236 * @flag: Command flag
237 * @argc: Number of arguments
238 * @argv: Argument array
239 * Return: CMD_RET_SUCCESS on success,
240 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
242 * Implement efidebug "capsule" sub-command.
244 static int do_efi_capsule(struct cmd_tbl *cmdtp, int flag,
245 int argc, char * const argv[])
250 return CMD_RET_USAGE;
254 cp = find_cmd_tbl(argv[0], cmd_efidebug_capsule_sub,
255 ARRAY_SIZE(cmd_efidebug_capsule_sub));
257 return CMD_RET_USAGE;
259 return cp->cmd(cmdtp, flag, argc, argv);
261 #endif /* CONFIG_EFI_HAVE_CAPSULE_SUPPORT */
264 * efi_get_device_handle_info() - get information of UEFI device
266 * @handle: Handle of UEFI device
267 * @dev_path_text: Pointer to text of device path
268 * Return: 0 on success, -1 on failure
270 * Currently return a formatted text of device path.
272 static int efi_get_device_handle_info(efi_handle_t handle, u16 **dev_path_text)
274 struct efi_device_path *dp;
277 ret = EFI_CALL(BS->open_protocol(handle, &efi_guid_device_path,
278 (void **)&dp, NULL /* FIXME */, NULL,
279 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
280 if (ret == EFI_SUCCESS) {
281 *dev_path_text = efi_dp_str(dp);
288 #define EFI_HANDLE_WIDTH ((int)sizeof(efi_handle_t) * 2)
290 static const char spc[] = " ";
291 static const char sep[] = "================";
294 * do_efi_show_devices() - show UEFI devices
296 * @cmdtp: Command table
297 * @flag: Command flag
298 * @argc: Number of arguments
299 * @argv: Argument array
300 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
302 * Implement efidebug "devices" sub-command.
303 * Show all UEFI devices and their information.
305 static int do_efi_show_devices(struct cmd_tbl *cmdtp, int flag,
306 int argc, char *const argv[])
308 efi_handle_t *handles;
313 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
315 if (ret != EFI_SUCCESS)
316 return CMD_RET_FAILURE;
319 return CMD_RET_SUCCESS;
321 printf("Device%.*s Device Path\n", EFI_HANDLE_WIDTH - 6, spc);
322 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
323 for (i = 0; i < num; i++) {
324 if (!efi_get_device_handle_info(handles[i], &dev_path_text)) {
325 printf("%p %ls\n", handles[i], dev_path_text);
326 efi_free_pool(dev_path_text);
330 efi_free_pool(handles);
332 return CMD_RET_SUCCESS;
336 * efi_get_driver_handle_info() - get information of UEFI driver
338 * @handle: Handle of UEFI device
339 * @driver_name: Driver name
340 * @image_path: Pointer to text of device path
341 * Return: 0 on success, -1 on failure
343 * Currently return no useful information as all UEFI drivers are
346 static int efi_get_driver_handle_info(efi_handle_t handle, u16 **driver_name,
349 struct efi_handler *handler;
350 struct efi_loaded_image *image;
355 * TODO: support EFI_COMPONENT_NAME2_PROTOCOL
360 ret = efi_search_protocol(handle, &efi_guid_loaded_image, &handler);
361 if (ret != EFI_SUCCESS) {
366 image = handler->protocol_interface;
367 *image_path = efi_dp_str(image->file_path);
373 * do_efi_show_drivers() - show UEFI drivers
375 * @cmdtp: Command table
376 * @flag: Command flag
377 * @argc: Number of arguments
378 * @argv: Argument array
379 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
381 * Implement efidebug "drivers" sub-command.
382 * Show all UEFI drivers and their information.
384 static int do_efi_show_drivers(struct cmd_tbl *cmdtp, int flag,
385 int argc, char *const argv[])
387 efi_handle_t *handles;
389 u16 *driver_name, *image_path_text;
392 ret = EFI_CALL(efi_locate_handle_buffer(
393 BY_PROTOCOL, &efi_guid_driver_binding_protocol,
394 NULL, &num, &handles));
395 if (ret != EFI_SUCCESS)
396 return CMD_RET_FAILURE;
399 return CMD_RET_SUCCESS;
401 printf("Driver%.*s Name Image Path\n",
402 EFI_HANDLE_WIDTH - 6, spc);
403 printf("%.*s ==================== ====================\n",
404 EFI_HANDLE_WIDTH, sep);
405 for (i = 0; i < num; i++) {
406 if (!efi_get_driver_handle_info(handles[i], &driver_name,
409 printf("%p %-20ls %ls\n", handles[i],
410 driver_name, image_path_text);
412 printf("%p %-20ls <built-in>\n",
413 handles[i], driver_name);
414 efi_free_pool(driver_name);
415 efi_free_pool(image_path_text);
419 efi_free_pool(handles);
421 return CMD_RET_SUCCESS;
424 static const struct {
426 const efi_guid_t guid;
430 EFI_DEVICE_PATH_PROTOCOL_GUID,
433 "Device Path To Text",
434 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID,
437 "Device Path Utilities",
438 EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID,
441 "Unicode Collation 2",
442 EFI_UNICODE_COLLATION_PROTOCOL2_GUID,
446 EFI_DRIVER_BINDING_PROTOCOL_GUID,
450 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID,
453 "Simple Text Input Ex",
454 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID,
457 "Simple Text Output",
458 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID,
462 EFI_BLOCK_IO_PROTOCOL_GUID,
465 "Simple File System",
466 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
470 EFI_LOADED_IMAGE_PROTOCOL_GUID,
474 EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID,
478 EFI_HII_STRING_PROTOCOL_GUID,
482 EFI_HII_DATABASE_PROTOCOL_GUID,
485 "HII Config Routing",
486 EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID,
490 EFI_LOAD_FILE2_PROTOCOL_GUID,
493 "Random Number Generator",
494 EFI_RNG_PROTOCOL_GUID,
498 EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
502 EFI_PXE_BASE_CODE_PROTOCOL_GUID,
506 EFI_DT_FIXUP_PROTOCOL_GUID,
510 PARTITION_SYSTEM_GUID
513 "Firmware Management",
514 EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID
516 /* Configuration table GUIDs */
522 "EFI System Resource Table",
523 EFI_SYSTEM_RESOURCE_TABLE_GUID,
534 "Runtime properties",
535 EFI_RT_PROPERTIES_TABLE_GUID,
538 "TCG2 Final Events Table",
539 EFI_TCG2_FINAL_EVENTS_TABLE_GUID,
544 * get_guid_text - get string of GUID
546 * Return description of GUID.
549 * Return: description of GUID or NULL
551 static const char *get_guid_text(const void *guid)
555 for (i = 0; i < ARRAY_SIZE(guid_list); i++) {
557 * As guidcmp uses memcmp() we can safely accept unaligned
560 if (!guidcmp(&guid_list[i].guid, guid))
561 return guid_list[i].text;
568 * do_efi_show_handles() - show UEFI handles
570 * @cmdtp: Command table
571 * @flag: Command flag
572 * @argc: Number of arguments
573 * @argv: Argument array
574 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
576 * Implement efidebug "dh" sub-command.
577 * Show all UEFI handles and their information, currently all protocols
580 static int do_efi_show_handles(struct cmd_tbl *cmdtp, int flag,
581 int argc, char *const argv[])
583 efi_handle_t *handles;
585 efi_uintn_t num, count, i, j;
586 const char *guid_text;
589 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
591 if (ret != EFI_SUCCESS)
592 return CMD_RET_FAILURE;
595 return CMD_RET_SUCCESS;
597 printf("Handle%.*s Protocols\n", EFI_HANDLE_WIDTH - 6, spc);
598 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
599 for (i = 0; i < num; i++) {
600 printf("%p", handles[i]);
601 ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid,
608 for (j = 0; j < count; j++) {
614 guid_text = get_guid_text(guid[j]);
618 printf("%pUl", guid[j]);
623 efi_free_pool(handles);
625 return CMD_RET_SUCCESS;
629 * do_efi_show_images() - show UEFI images
631 * @cmdtp: Command table
632 * @flag: Command flag
633 * @argc: Number of arguments
634 * @argv: Argument array
635 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
637 * Implement efidebug "images" sub-command.
638 * Show all UEFI loaded images and their information.
640 static int do_efi_show_images(struct cmd_tbl *cmdtp, int flag,
641 int argc, char *const argv[])
643 efi_print_image_infos(NULL);
645 return CMD_RET_SUCCESS;
648 static const char * const efi_mem_type_string[] = {
649 [EFI_RESERVED_MEMORY_TYPE] = "RESERVED",
650 [EFI_LOADER_CODE] = "LOADER CODE",
651 [EFI_LOADER_DATA] = "LOADER DATA",
652 [EFI_BOOT_SERVICES_CODE] = "BOOT CODE",
653 [EFI_BOOT_SERVICES_DATA] = "BOOT DATA",
654 [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE",
655 [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA",
656 [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL",
657 [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM",
658 [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM",
659 [EFI_ACPI_MEMORY_NVS] = "ACPI NVS",
660 [EFI_MMAP_IO] = "IO",
661 [EFI_MMAP_IO_PORT] = "IO PORT",
662 [EFI_PAL_CODE] = "PAL",
663 [EFI_PERSISTENT_MEMORY_TYPE] = "PERSISTENT",
666 static const struct efi_mem_attrs {
669 } efi_mem_attrs[] = {
670 {EFI_MEMORY_UC, "UC"},
671 {EFI_MEMORY_UC, "UC"},
672 {EFI_MEMORY_WC, "WC"},
673 {EFI_MEMORY_WT, "WT"},
674 {EFI_MEMORY_WB, "WB"},
675 {EFI_MEMORY_UCE, "UCE"},
676 {EFI_MEMORY_WP, "WP"},
677 {EFI_MEMORY_RP, "RP"},
678 {EFI_MEMORY_XP, "WP"},
679 {EFI_MEMORY_NV, "NV"},
680 {EFI_MEMORY_MORE_RELIABLE, "REL"},
681 {EFI_MEMORY_RO, "RO"},
682 {EFI_MEMORY_SP, "SP"},
683 {EFI_MEMORY_RUNTIME, "RT"},
687 * print_memory_attributes() - print memory map attributes
689 * @attributes: Attribute value
691 * Print memory map attributes
693 static void print_memory_attributes(u64 attributes)
697 for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
698 if (attributes & efi_mem_attrs[i].bit) {
705 puts(efi_mem_attrs[i].text);
709 #define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
712 * do_efi_show_memmap() - show UEFI memory map
714 * @cmdtp: Command table
715 * @flag: Command flag
716 * @argc: Number of arguments
717 * @argv: Argument array
718 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
720 * Implement efidebug "memmap" sub-command.
721 * Show UEFI memory map.
723 static int do_efi_show_memmap(struct cmd_tbl *cmdtp, int flag,
724 int argc, char *const argv[])
726 struct efi_mem_desc *memmap = NULL, *map;
727 efi_uintn_t map_size = 0;
732 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
733 if (ret == EFI_BUFFER_TOO_SMALL) {
734 map_size += sizeof(struct efi_mem_desc); /* for my own */
735 ret = efi_allocate_pool(EFI_LOADER_DATA, map_size,
737 if (ret != EFI_SUCCESS)
738 return CMD_RET_FAILURE;
739 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
741 if (ret != EFI_SUCCESS) {
742 efi_free_pool(memmap);
743 return CMD_RET_FAILURE;
746 printf("Type Start%.*s End%.*s Attributes\n",
747 EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc);
748 printf("================ %.*s %.*s ==========\n",
749 EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep);
751 * Coverity check: dereferencing null pointer "map."
752 * This is a false positive as memmap will always be
753 * populated by allocate_pool() above.
755 for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
756 if (map->type < ARRAY_SIZE(efi_mem_type_string))
757 type = efi_mem_type_string[map->type];
761 printf("%-16s %.*llx-%.*llx", type,
763 (u64)map_to_sysmem((void *)(uintptr_t)
764 map->physical_start),
766 (u64)map_to_sysmem((void *)(uintptr_t)
767 (map->physical_start +
768 map->num_pages * EFI_PAGE_SIZE)));
770 print_memory_attributes(map->attribute);
774 efi_free_pool(memmap);
776 return CMD_RET_SUCCESS;
780 * do_efi_show_tables() - show UEFI configuration tables
782 * @cmdtp: Command table
783 * @flag: Command flag
784 * @argc: Number of arguments
785 * @argv: Argument array
786 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
788 * Implement efidebug "tables" sub-command.
789 * Show UEFI configuration tables.
791 static int do_efi_show_tables(struct cmd_tbl *cmdtp, int flag,
792 int argc, char *const argv[])
795 const char *guid_str;
797 for (i = 0; i < systab.nr_tables; ++i) {
798 guid_str = get_guid_text(&systab.tables[i].guid);
801 printf("%pUl %s\n", &systab.tables[i].guid, guid_str);
804 return CMD_RET_SUCCESS;
808 * create_initrd_dp() - Create a special device for our Boot### option
811 * @part: Disk partition
813 * Return: Pointer to the device path or ERR_PTR
817 struct efi_device_path *create_initrd_dp(const char *dev, const char *part,
821 struct efi_device_path *tmp_dp = NULL, *tmp_fp = NULL;
822 struct efi_device_path *initrd_dp = NULL;
824 const struct efi_initrd_dp id_dp = {
827 DEVICE_PATH_TYPE_MEDIA_DEVICE,
828 DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
829 sizeof(id_dp.vendor),
831 EFI_INITRD_MEDIA_GUID,
834 DEVICE_PATH_TYPE_END,
835 DEVICE_PATH_SUB_TYPE_END,
840 ret = efi_dp_from_name(dev, part, file, &tmp_dp, &tmp_fp);
841 if (ret != EFI_SUCCESS) {
842 printf("Cannot create device path for \"%s %s\"\n", part, file);
846 initrd_dp = efi_dp_append((const struct efi_device_path *)&id_dp,
850 efi_free_pool(tmp_dp);
851 efi_free_pool(tmp_fp);
856 * do_efi_boot_add() - set UEFI load option
858 * @cmdtp: Command table
859 * @flag: Command flag
860 * @argc: Number of arguments
861 * @argv: Argument array
862 * Return: CMD_RET_SUCCESS on success,
863 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
865 * Implement efidebug "boot add" sub-command. Create or change UEFI load option.
867 * efidebug boot add -b <id> <label> <interface> <devnum>[:<part>] <file>
868 * -i <file> <interface2> <devnum2>[:<part>] <initrd>
871 static int do_efi_boot_add(struct cmd_tbl *cmdtp, int flag,
872 int argc, char *const argv[])
877 u16 var_name16[9], *p;
879 size_t label_len, label_len16;
881 struct efi_device_path *device_path = NULL, *file_path = NULL;
882 struct efi_device_path *final_fp = NULL;
883 struct efi_device_path *initrd_dp = NULL;
884 struct efi_load_option lo;
887 efi_uintn_t fp_size = 0;
889 int r = CMD_RET_SUCCESS;
891 guid = efi_global_variable_guid;
894 lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
895 lo.optional_data = NULL;
900 for (; argc > 0; argc--, argv++) {
901 if (!strcmp(argv[0], "-b")) {
902 if (argc < 5 || lo.label) {
906 id = (int)simple_strtoul(argv[1], &endp, 16);
907 if (*endp != '\0' || id > 0xffff)
908 return CMD_RET_USAGE;
910 sprintf(var_name, "Boot%04X", id);
912 utf8_utf16_strncpy(&p, var_name, 9);
915 label_len = strlen(argv[2]);
916 label_len16 = utf8_utf16_strnlen(argv[2], label_len);
917 label = malloc((label_len16 + 1) * sizeof(u16));
919 return CMD_RET_FAILURE;
920 lo.label = label; /* label will be changed below */
921 utf8_utf16_strncpy(&label, argv[2], label_len);
924 ret = efi_dp_from_name(argv[3], argv[4], argv[5],
925 &device_path, &file_path);
926 if (ret != EFI_SUCCESS) {
927 printf("Cannot create device path for \"%s %s\"\n",
932 fp_size += efi_dp_size(file_path) +
933 sizeof(struct efi_device_path);
936 } else if (!strcmp(argv[0], "-i")) {
937 if (argc < 3 || initrd_dp) {
942 initrd_dp = create_initrd_dp(argv[1], argv[2], argv[3]);
944 printf("Cannot add an initrd\n");
950 fp_size += efi_dp_size(initrd_dp) +
951 sizeof(struct efi_device_path);
952 } else if (!strcmp(argv[0], "-s")) {
953 if (argc < 1 || lo.optional_data) {
957 lo.optional_data = (const u8 *)argv[1];
967 printf("Missing binary\n");
972 final_fp = efi_dp_concat(file_path, initrd_dp);
974 printf("Cannot create final device path\n");
979 lo.file_path = final_fp;
980 lo.file_path_length = fp_size;
982 size = efi_serialize_load_option(&lo, (u8 **)&data);
988 ret = EFI_CALL(efi_set_variable(var_name16, &guid,
989 EFI_VARIABLE_NON_VOLATILE |
990 EFI_VARIABLE_BOOTSERVICE_ACCESS |
991 EFI_VARIABLE_RUNTIME_ACCESS,
993 if (ret != EFI_SUCCESS) {
994 printf("Cannot set %ls\n", var_name16);
1000 efi_free_pool(final_fp);
1001 efi_free_pool(initrd_dp);
1002 efi_free_pool(device_path);
1003 efi_free_pool(file_path);
1010 * do_efi_boot_rm() - delete UEFI load options
1012 * @cmdtp: Command table
1013 * @flag: Command flag
1014 * @argc: Number of arguments
1015 * @argv: Argument array
1016 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1018 * Implement efidebug "boot rm" sub-command.
1019 * Delete UEFI load options.
1021 * efidebug boot rm <id> ...
1023 static int do_efi_boot_rm(struct cmd_tbl *cmdtp, int flag,
1024 int argc, char *const argv[])
1030 u16 var_name16[9], *p;
1034 return CMD_RET_USAGE;
1036 guid = efi_global_variable_guid;
1037 for (i = 1; i < argc; i++, argv++) {
1038 id = (int)simple_strtoul(argv[1], &endp, 16);
1039 if (*endp != '\0' || id > 0xffff)
1040 return CMD_RET_FAILURE;
1042 sprintf(var_name, "Boot%04X", id);
1044 utf8_utf16_strncpy(&p, var_name, 9);
1046 ret = EFI_CALL(efi_set_variable(var_name16, &guid, 0, 0, NULL));
1048 printf("Cannot remove %ls\n", var_name16);
1049 return CMD_RET_FAILURE;
1053 return CMD_RET_SUCCESS;
1057 * show_efi_boot_opt_data() - dump UEFI load option
1059 * @varname16: variable name
1060 * @data: value of UEFI load option variable
1061 * @size: size of the boot option
1063 * Decode the value of UEFI load option variable and print information.
1065 static void show_efi_boot_opt_data(u16 *varname16, void *data, size_t *size)
1067 struct efi_device_path *initrd_path = NULL;
1068 struct efi_load_option lo;
1070 size_t label_len16, label_len;
1073 efi_uintn_t initrd_dp_size;
1074 const efi_guid_t lf2_initrd_guid = EFI_INITRD_MEDIA_GUID;
1076 ret = efi_deserialize_load_option(&lo, data, size);
1077 if (ret != EFI_SUCCESS) {
1078 printf("%ls: invalid load option\n", varname16);
1082 label_len16 = u16_strlen(lo.label);
1083 label_len = utf16_utf8_strnlen(lo.label, label_len16);
1084 label = malloc(label_len + 1);
1088 utf16_utf8_strncpy(&p, lo.label, label_len16);
1090 printf("%ls:\nattributes: %c%c%c (0x%08x)\n",
1093 lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
1094 /* FORCE RECONNECT */
1095 lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
1097 lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
1099 printf(" label: %s\n", label);
1101 dp_str = efi_dp_str(lo.file_path);
1102 printf(" file_path: %ls\n", dp_str);
1103 efi_free_pool(dp_str);
1105 initrd_path = efi_dp_from_lo(&lo, &initrd_dp_size, lf2_initrd_guid);
1107 dp_str = efi_dp_str(initrd_path);
1108 printf(" initrd_path: %ls\n", dp_str);
1109 efi_free_pool(dp_str);
1110 efi_free_pool(initrd_path);
1114 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
1115 lo.optional_data, *size, true);
1120 * show_efi_boot_opt() - dump UEFI load option
1122 * @varname16: variable name
1124 * Dump information defined by UEFI load option.
1126 static void show_efi_boot_opt(u16 *varname16)
1133 ret = EFI_CALL(efi_get_variable(varname16, &efi_global_variable_guid,
1134 NULL, &size, NULL));
1135 if (ret == EFI_BUFFER_TOO_SMALL) {
1136 data = malloc(size);
1138 printf("ERROR: Out of memory\n");
1141 ret = EFI_CALL(efi_get_variable(varname16,
1142 &efi_global_variable_guid,
1143 NULL, &size, data));
1144 if (ret == EFI_SUCCESS)
1145 show_efi_boot_opt_data(varname16, data, &size);
1150 static int u16_tohex(u16 c)
1152 if (c >= '0' && c <= '9')
1154 if (c >= 'A' && c <= 'F')
1155 return c - 'A' + 10;
1157 /* not hexadecimal */
1162 * show_efi_boot_dump() - dump all UEFI load options
1164 * @cmdtp: Command table
1165 * @flag: Command flag
1166 * @argc: Number of arguments
1167 * @argv: Argument array
1168 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1170 * Implement efidebug "boot dump" sub-command.
1171 * Dump information of all UEFI load options defined.
1173 * efidebug boot dump
1175 static int do_efi_boot_dump(struct cmd_tbl *cmdtp, int flag,
1176 int argc, char *const argv[])
1178 u16 *var_name16, *p;
1179 efi_uintn_t buf_size, size;
1185 return CMD_RET_USAGE;
1188 var_name16 = malloc(buf_size);
1190 return CMD_RET_FAILURE;
1195 ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
1197 if (ret == EFI_NOT_FOUND)
1199 if (ret == EFI_BUFFER_TOO_SMALL) {
1201 p = realloc(var_name16, buf_size);
1204 return CMD_RET_FAILURE;
1207 ret = EFI_CALL(efi_get_next_variable_name(&size,
1211 if (ret != EFI_SUCCESS) {
1213 return CMD_RET_FAILURE;
1216 if (memcmp(var_name16, L"Boot", 8))
1219 for (id = 0, i = 0; i < 4; i++) {
1220 digit = u16_tohex(var_name16[4 + i]);
1223 id = (id << 4) + digit;
1225 if (i == 4 && !var_name16[8])
1226 show_efi_boot_opt(var_name16);
1231 return CMD_RET_SUCCESS;
1235 * show_efi_boot_order() - show order of UEFI load options
1237 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1239 * Show order of UEFI load options defined by BootOrder variable.
1241 static int show_efi_boot_order(void)
1247 u16 var_name16[9], *p16;
1249 struct efi_load_option lo;
1251 size_t label_len16, label_len;
1255 ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid,
1256 NULL, &size, NULL));
1257 if (ret != EFI_BUFFER_TOO_SMALL) {
1258 if (ret == EFI_NOT_FOUND) {
1259 printf("BootOrder not defined\n");
1260 return CMD_RET_SUCCESS;
1262 return CMD_RET_FAILURE;
1265 bootorder = malloc(size);
1267 printf("ERROR: Out of memory\n");
1268 return CMD_RET_FAILURE;
1270 ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid,
1271 NULL, &size, bootorder));
1272 if (ret != EFI_SUCCESS) {
1273 ret = CMD_RET_FAILURE;
1277 num = size / sizeof(u16);
1278 for (i = 0; i < num; i++) {
1279 sprintf(var_name, "Boot%04X", bootorder[i]);
1281 utf8_utf16_strncpy(&p16, var_name, 9);
1284 ret = EFI_CALL(efi_get_variable(var_name16,
1285 &efi_global_variable_guid, NULL,
1287 if (ret != EFI_BUFFER_TOO_SMALL) {
1288 printf("%2d: %s: (not defined)\n", i + 1, var_name);
1292 data = malloc(size);
1294 ret = CMD_RET_FAILURE;
1297 ret = EFI_CALL(efi_get_variable(var_name16,
1298 &efi_global_variable_guid, NULL,
1300 if (ret != EFI_SUCCESS) {
1302 ret = CMD_RET_FAILURE;
1306 ret = efi_deserialize_load_option(&lo, data, &size);
1307 if (ret != EFI_SUCCESS) {
1308 printf("%ls: invalid load option\n", var_name16);
1309 ret = CMD_RET_FAILURE;
1313 label_len16 = u16_strlen(lo.label);
1314 label_len = utf16_utf8_strnlen(lo.label, label_len16);
1315 label = malloc(label_len + 1);
1318 ret = CMD_RET_FAILURE;
1322 utf16_utf8_strncpy(&p, lo.label, label_len16);
1323 printf("%2d: %s: %s\n", i + 1, var_name, label);
1335 * do_efi_boot_next() - manage UEFI BootNext variable
1337 * @cmdtp: Command table
1338 * @flag: Command flag
1339 * @argc: Number of arguments
1340 * @argv: Argument array
1341 * Return: CMD_RET_SUCCESS on success,
1342 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1344 * Implement efidebug "boot next" sub-command.
1345 * Set BootNext variable.
1347 * efidebug boot next <id>
1349 static int do_efi_boot_next(struct cmd_tbl *cmdtp, int flag,
1350 int argc, char *const argv[])
1357 int r = CMD_RET_SUCCESS;
1360 return CMD_RET_USAGE;
1362 bootnext = (u16)simple_strtoul(argv[1], &endp, 16);
1364 printf("invalid value: %s\n", argv[1]);
1365 r = CMD_RET_FAILURE;
1369 guid = efi_global_variable_guid;
1371 ret = EFI_CALL(efi_set_variable(L"BootNext", &guid,
1372 EFI_VARIABLE_NON_VOLATILE |
1373 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1374 EFI_VARIABLE_RUNTIME_ACCESS,
1376 if (ret != EFI_SUCCESS) {
1377 printf("Cannot set BootNext\n");
1378 r = CMD_RET_FAILURE;
1385 * do_efi_boot_order() - manage UEFI BootOrder variable
1387 * @cmdtp: Command table
1388 * @flag: Command flag
1389 * @argc: Number of arguments
1390 * @argv: Argument array
1391 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1393 * Implement efidebug "boot order" sub-command.
1394 * Show order of UEFI load options, or change it in BootOrder variable.
1396 * efidebug boot order [<id> ...]
1398 static int do_efi_boot_order(struct cmd_tbl *cmdtp, int flag,
1399 int argc, char *const argv[])
1401 u16 *bootorder = NULL;
1407 int r = CMD_RET_SUCCESS;
1410 return show_efi_boot_order();
1415 size = argc * sizeof(u16);
1416 bootorder = malloc(size);
1418 return CMD_RET_FAILURE;
1420 for (i = 0; i < argc; i++) {
1421 id = (int)simple_strtoul(argv[i], &endp, 16);
1422 if (*endp != '\0' || id > 0xffff) {
1423 printf("invalid value: %s\n", argv[i]);
1424 r = CMD_RET_FAILURE;
1428 bootorder[i] = (u16)id;
1431 guid = efi_global_variable_guid;
1432 ret = EFI_CALL(efi_set_variable(L"BootOrder", &guid,
1433 EFI_VARIABLE_NON_VOLATILE |
1434 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1435 EFI_VARIABLE_RUNTIME_ACCESS,
1437 if (ret != EFI_SUCCESS) {
1438 printf("Cannot set BootOrder\n");
1439 r = CMD_RET_FAILURE;
1447 static struct cmd_tbl cmd_efidebug_boot_sub[] = {
1448 U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
1449 U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
1450 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
1451 U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
1452 U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
1457 * do_efi_boot_opt() - manage UEFI load options
1459 * @cmdtp: Command table
1460 * @flag: Command flag
1461 * @argc: Number of arguments
1462 * @argv: Argument array
1463 * Return: CMD_RET_SUCCESS on success,
1464 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1466 * Implement efidebug "boot" sub-command.
1468 static int do_efi_boot_opt(struct cmd_tbl *cmdtp, int flag,
1469 int argc, char *const argv[])
1474 return CMD_RET_USAGE;
1478 cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
1479 ARRAY_SIZE(cmd_efidebug_boot_sub));
1481 return CMD_RET_USAGE;
1483 return cp->cmd(cmdtp, flag, argc, argv);
1487 * do_efi_test_bootmgr() - run simple bootmgr for test
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 "test bootmgr" sub-command.
1497 * Run simple bootmgr for test.
1499 * efidebug test bootmgr
1501 static __maybe_unused int do_efi_test_bootmgr(struct cmd_tbl *cmdtp, int flag,
1502 int argc, char * const argv[])
1505 efi_uintn_t exit_data_size = 0;
1506 u16 *exit_data = NULL;
1508 void *load_options = NULL;
1510 ret = efi_bootmgr_load(&image, &load_options);
1511 printf("efi_bootmgr_load() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1513 /* We call efi_start_image() even if error for test purpose. */
1514 ret = EFI_CALL(efi_start_image(image, &exit_data_size, &exit_data));
1515 printf("efi_start_image() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1516 if (ret && exit_data)
1517 efi_free_pool(exit_data);
1522 return CMD_RET_SUCCESS;
1525 static struct cmd_tbl cmd_efidebug_test_sub[] = {
1526 #ifdef CONFIG_CMD_BOOTEFI_BOOTMGR
1527 U_BOOT_CMD_MKENT(bootmgr, CONFIG_SYS_MAXARGS, 1, do_efi_test_bootmgr,
1533 * do_efi_test() - manage UEFI load options
1535 * @cmdtp: Command table
1536 * @flag: Command flag
1537 * @argc: Number of arguments
1538 * @argv: Argument array
1539 * Return: CMD_RET_SUCCESS on success,
1540 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1542 * Implement efidebug "test" sub-command.
1544 static int do_efi_test(struct cmd_tbl *cmdtp, int flag,
1545 int argc, char * const argv[])
1550 return CMD_RET_USAGE;
1554 cp = find_cmd_tbl(argv[0], cmd_efidebug_test_sub,
1555 ARRAY_SIZE(cmd_efidebug_test_sub));
1557 return CMD_RET_USAGE;
1559 return cp->cmd(cmdtp, flag, argc, argv);
1563 * do_efi_query_info() - QueryVariableInfo EFI service
1565 * @cmdtp: Command table
1566 * @flag: Command flag
1567 * @argc: Number of arguments
1568 * @argv: Argument array
1569 * Return: CMD_RET_SUCCESS on success,
1570 * CMD_RET_USAGE or CMD_RET_FAILURE on failure
1572 * Implement efidebug "test" sub-command.
1575 static int do_efi_query_info(struct cmd_tbl *cmdtp, int flag,
1576 int argc, char * const argv[])
1580 u64 max_variable_storage_size;
1581 u64 remain_variable_storage_size;
1582 u64 max_variable_size;
1585 for (i = 1; i < argc; i++) {
1586 if (!strcmp(argv[i], "-bs"))
1587 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
1588 else if (!strcmp(argv[i], "-rt"))
1589 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
1590 else if (!strcmp(argv[i], "-nv"))
1591 attr |= EFI_VARIABLE_NON_VOLATILE;
1592 else if (!strcmp(argv[i], "-at"))
1594 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
1597 ret = EFI_CALL(efi_query_variable_info(attr,
1598 &max_variable_storage_size,
1599 &remain_variable_storage_size,
1600 &max_variable_size));
1601 if (ret != EFI_SUCCESS) {
1602 printf("Error: Cannot query UEFI variables, r = %lu\n",
1603 ret & ~EFI_ERROR_MASK);
1604 return CMD_RET_FAILURE;
1607 printf("Max storage size %llu\n", max_variable_storage_size);
1608 printf("Remaining storage size %llu\n", remain_variable_storage_size);
1609 printf("Max variable size %llu\n", max_variable_size);
1611 return CMD_RET_SUCCESS;
1614 static struct cmd_tbl cmd_efidebug_sub[] = {
1615 U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
1616 #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
1617 U_BOOT_CMD_MKENT(capsule, CONFIG_SYS_MAXARGS, 1, do_efi_capsule,
1620 U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices,
1622 U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
1624 U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
1626 U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
1628 U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
1630 U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables,
1632 U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_efi_test,
1634 U_BOOT_CMD_MKENT(query, CONFIG_SYS_MAXARGS, 1, do_efi_query_info,
1639 * do_efidebug() - display and configure UEFI environment
1641 * @cmdtp: Command table
1642 * @flag: Command flag
1643 * @argc: Number of arguments
1644 * @argv: Argument array
1645 * Return: CMD_RET_SUCCESS on success,
1646 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1648 * Implement efidebug command which allows us to display and
1649 * configure UEFI environment.
1651 static int do_efidebug(struct cmd_tbl *cmdtp, int flag,
1652 int argc, char *const argv[])
1658 return CMD_RET_USAGE;
1662 /* Initialize UEFI drivers */
1663 r = efi_init_obj_list();
1664 if (r != EFI_SUCCESS) {
1665 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1666 r & ~EFI_ERROR_MASK);
1667 return CMD_RET_FAILURE;
1670 cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
1671 ARRAY_SIZE(cmd_efidebug_sub));
1673 return CMD_RET_USAGE;
1675 return cp->cmd(cmdtp, flag, argc, argv);
1678 #ifdef CONFIG_SYS_LONGHELP
1679 static char efidebug_help_text[] =
1680 " - UEFI Shell-like interface to configure UEFI environment\n"
1682 "efidebug boot add "
1683 "-b <bootid> <label> <interface> <devnum>[:<part>] <file path> "
1684 "-i <interface> <devnum>[:<part>] <initrd file path> "
1685 "-s '<optional data>'\n"
1686 " - set UEFI BootXXXX variable\n"
1687 " <load options> will be passed to UEFI application\n"
1688 "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
1689 " - delete UEFI BootXXXX variables\n"
1690 "efidebug boot dump\n"
1691 " - dump all UEFI BootXXXX variables\n"
1692 "efidebug boot next <bootid>\n"
1693 " - set UEFI BootNext variable\n"
1694 "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
1695 " - set/show UEFI boot order\n"
1697 #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
1698 "efidebug capsule update [-v] <capsule address>\n"
1699 " - process a capsule\n"
1700 "efidebug capsule disk-update\n"
1701 " - update a capsule from disk\n"
1702 "efidebug capsule show <capsule address>\n"
1703 " - show capsule information\n"
1704 "efidebug capsule result [<capsule result var>]\n"
1705 " - show a capsule update result\n"
1708 "efidebug devices\n"
1709 " - show UEFI devices\n"
1710 "efidebug drivers\n"
1711 " - show UEFI drivers\n"
1713 " - show UEFI handles\n"
1715 " - show loaded images\n"
1717 " - show UEFI memory map\n"
1719 " - show UEFI configuration tables\n"
1720 #ifdef CONFIG_CMD_BOOTEFI_BOOTMGR
1721 "efidebug test bootmgr\n"
1722 " - run simple bootmgr for test\n"
1724 "efidebug query [-nv][-bs][-rt][-at]\n"
1725 " - show size of UEFI variables store\n";
1729 efidebug, CONFIG_SYS_MAXARGS, 0, do_efidebug,
1730 "Configure UEFI environment",