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_loader.h>
20 #include <linux/ctype.h>
22 #define BS systab.boottime
23 #define RT systab.runtime
25 #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
27 * do_efi_capsule_update() - process a capsule update
29 * @cmdtp: Command table
31 * @argc: Number of arguments
32 * @argv: Argument array
33 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
35 * Implement efidebug "capsule update" sub-command.
36 * process a capsule update.
38 * efidebug capsule update [-v] <capsule address>
40 static int do_efi_capsule_update(struct cmd_tbl *cmdtp, int flag,
41 int argc, char * const argv[])
43 struct efi_capsule_header *capsule;
48 if (argc != 2 && argc != 3)
52 if (strcmp(argv[1], "-v"))
60 capsule = (typeof(capsule))simple_strtoul(argv[1], &endp, 16);
61 if (endp == argv[1]) {
62 printf("Invalid address: %s", argv[1]);
63 return CMD_RET_FAILURE;
67 printf("Capsule guid: %pUl\n", &capsule->capsule_guid);
68 printf("Capsule flags: 0x%x\n", capsule->flags);
69 printf("Capsule header size: 0x%x\n", capsule->header_size);
70 printf("Capsule image size: 0x%x\n",
71 capsule->capsule_image_size);
74 ret = EFI_CALL(RT->update_capsule(&capsule, 1, (u64)NULL));
76 printf("Cannot handle a capsule at %p", capsule);
77 return CMD_RET_FAILURE;
80 return CMD_RET_SUCCESS;
83 static int do_efi_capsule_on_disk_update(struct cmd_tbl *cmdtp, int flag,
84 int argc, char * const argv[])
88 ret = efi_launch_capsules();
90 return ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
94 * do_efi_capsule_show() - show capsule information
96 * @cmdtp: Command table
98 * @argc: Number of arguments
99 * @argv: Argument array
100 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
102 * Implement efidebug "capsule show" sub-command.
103 * show capsule information.
105 * efidebug capsule show <capsule address>
107 static int do_efi_capsule_show(struct cmd_tbl *cmdtp, int flag,
108 int argc, char * const argv[])
110 struct efi_capsule_header *capsule;
114 return CMD_RET_USAGE;
116 capsule = (typeof(capsule))simple_strtoul(argv[1], &endp, 16);
117 if (endp == argv[1]) {
118 printf("Invalid address: %s", argv[1]);
119 return CMD_RET_FAILURE;
122 printf("Capsule guid: %pUl\n", &capsule->capsule_guid);
123 printf("Capsule flags: 0x%x\n", capsule->flags);
124 printf("Capsule header size: 0x%x\n", capsule->header_size);
125 printf("Capsule image size: 0x%x\n",
126 capsule->capsule_image_size);
128 return CMD_RET_SUCCESS;
132 * do_efi_capsule_res() - show a capsule update result
134 * @cmdtp: Command table
135 * @flag: Command flag
136 * @argc: Number of arguments
137 * @argv: Argument array
138 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
140 * Implement efidebug "capsule result" sub-command.
141 * show a capsule update result.
142 * If result number is not specified, CapsuleLast will be shown.
144 * efidebug capsule result [<capsule result number>]
146 static int do_efi_capsule_res(struct cmd_tbl *cmdtp, int flag,
147 int argc, char * const argv[])
152 u16 var_name16[12], *p;
154 struct efi_capsule_result_variable_header *result = NULL;
158 if (argc != 1 && argc != 2)
159 return CMD_RET_USAGE;
161 guid = efi_guid_capsule_report;
163 size = sizeof(var_name16);
164 ret = EFI_CALL(RT->get_variable(L"CapsuleLast", &guid, NULL,
166 if (ret != EFI_SUCCESS) {
167 if (ret == EFI_NOT_FOUND)
168 printf("CapsuleLast doesn't exist\n");
170 printf("Failed to get CapsuleLast\n");
172 return CMD_RET_FAILURE;
174 printf("CapsuleLast is %ls\n", var_name16);
179 capsule_id = simple_strtoul(argv[0], &endp, 16);
180 if (capsule_id < 0 || capsule_id > 0xffff)
181 return CMD_RET_USAGE;
183 sprintf(var_name, "Capsule%04X", capsule_id);
185 utf8_utf16_strncpy(&p, var_name, 9);
189 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, NULL));
190 if (ret == EFI_BUFFER_TOO_SMALL) {
191 result = malloc(size);
192 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
194 if (ret != EFI_SUCCESS) {
196 printf("Failed to get %ls\n", var_name16);
198 return CMD_RET_FAILURE;
202 printf("Result total size: 0x%x\n", result->variable_total_size);
203 printf("Capsule guid: %pUl\n", &result->capsule_guid);
204 printf("Time processed: %04d-%02d-%02d %02d:%02d:%02d\n",
205 result->capsule_processed.year, result->capsule_processed.month,
206 result->capsule_processed.day, result->capsule_processed.hour,
207 result->capsule_processed.minute,
208 result->capsule_processed.second);
209 printf("Capsule status: 0x%lx\n", result->capsule_status);
213 return CMD_RET_SUCCESS;
216 static struct cmd_tbl cmd_efidebug_capsule_sub[] = {
217 U_BOOT_CMD_MKENT(update, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_update,
219 U_BOOT_CMD_MKENT(show, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_show,
221 U_BOOT_CMD_MKENT(disk-update, 0, 0, do_efi_capsule_on_disk_update,
223 U_BOOT_CMD_MKENT(result, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_res,
228 * do_efi_capsule() - manage UEFI capsules
230 * @cmdtp: Command table
231 * @flag: Command flag
232 * @argc: Number of arguments
233 * @argv: Argument array
234 * Return: CMD_RET_SUCCESS on success,
235 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
237 * Implement efidebug "capsule" sub-command.
239 static int do_efi_capsule(struct cmd_tbl *cmdtp, int flag,
240 int argc, char * const argv[])
245 return CMD_RET_USAGE;
249 cp = find_cmd_tbl(argv[0], cmd_efidebug_capsule_sub,
250 ARRAY_SIZE(cmd_efidebug_capsule_sub));
252 return CMD_RET_USAGE;
254 return cp->cmd(cmdtp, flag, argc, argv);
256 #endif /* CONFIG_EFI_HAVE_CAPSULE_SUPPORT */
259 * efi_get_device_handle_info() - get information of UEFI device
261 * @handle: Handle of UEFI device
262 * @dev_path_text: Pointer to text of device path
263 * Return: 0 on success, -1 on failure
265 * Currently return a formatted text of device path.
267 static int efi_get_device_handle_info(efi_handle_t handle, u16 **dev_path_text)
269 struct efi_device_path *dp;
272 ret = EFI_CALL(BS->open_protocol(handle, &efi_guid_device_path,
273 (void **)&dp, NULL /* FIXME */, NULL,
274 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
275 if (ret == EFI_SUCCESS) {
276 *dev_path_text = efi_dp_str(dp);
283 #define EFI_HANDLE_WIDTH ((int)sizeof(efi_handle_t) * 2)
285 static const char spc[] = " ";
286 static const char sep[] = "================";
289 * do_efi_show_devices() - show UEFI devices
291 * @cmdtp: Command table
292 * @flag: Command flag
293 * @argc: Number of arguments
294 * @argv: Argument array
295 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
297 * Implement efidebug "devices" sub-command.
298 * Show all UEFI devices and their information.
300 static int do_efi_show_devices(struct cmd_tbl *cmdtp, int flag,
301 int argc, char *const argv[])
303 efi_handle_t *handles;
308 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
310 if (ret != EFI_SUCCESS)
311 return CMD_RET_FAILURE;
314 return CMD_RET_SUCCESS;
316 printf("Device%.*s Device Path\n", EFI_HANDLE_WIDTH - 6, spc);
317 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
318 for (i = 0; i < num; i++) {
319 if (!efi_get_device_handle_info(handles[i], &dev_path_text)) {
320 printf("%p %ls\n", handles[i], dev_path_text);
321 efi_free_pool(dev_path_text);
325 efi_free_pool(handles);
327 return CMD_RET_SUCCESS;
331 * efi_get_driver_handle_info() - get information of UEFI driver
333 * @handle: Handle of UEFI device
334 * @driver_name: Driver name
335 * @image_path: Pointer to text of device path
336 * Return: 0 on success, -1 on failure
338 * Currently return no useful information as all UEFI drivers are
341 static int efi_get_driver_handle_info(efi_handle_t handle, u16 **driver_name,
344 struct efi_handler *handler;
345 struct efi_loaded_image *image;
350 * TODO: support EFI_COMPONENT_NAME2_PROTOCOL
355 ret = efi_search_protocol(handle, &efi_guid_loaded_image, &handler);
356 if (ret != EFI_SUCCESS) {
361 image = handler->protocol_interface;
362 *image_path = efi_dp_str(image->file_path);
368 * do_efi_show_drivers() - show UEFI drivers
370 * @cmdtp: Command table
371 * @flag: Command flag
372 * @argc: Number of arguments
373 * @argv: Argument array
374 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
376 * Implement efidebug "drivers" sub-command.
377 * Show all UEFI drivers and their information.
379 static int do_efi_show_drivers(struct cmd_tbl *cmdtp, int flag,
380 int argc, char *const argv[])
382 efi_handle_t *handles;
384 u16 *driver_name, *image_path_text;
387 ret = EFI_CALL(efi_locate_handle_buffer(
388 BY_PROTOCOL, &efi_guid_driver_binding_protocol,
389 NULL, &num, &handles));
390 if (ret != EFI_SUCCESS)
391 return CMD_RET_FAILURE;
394 return CMD_RET_SUCCESS;
396 printf("Driver%.*s Name Image Path\n",
397 EFI_HANDLE_WIDTH - 6, spc);
398 printf("%.*s ==================== ====================\n",
399 EFI_HANDLE_WIDTH, sep);
400 for (i = 0; i < num; i++) {
401 if (!efi_get_driver_handle_info(handles[i], &driver_name,
404 printf("%p %-20ls %ls\n", handles[i],
405 driver_name, image_path_text);
407 printf("%p %-20ls <built-in>\n",
408 handles[i], driver_name);
409 efi_free_pool(driver_name);
410 efi_free_pool(image_path_text);
414 efi_free_pool(handles);
416 return CMD_RET_SUCCESS;
419 static const struct {
421 const efi_guid_t guid;
425 EFI_DEVICE_PATH_PROTOCOL_GUID,
428 "Device Path To Text",
429 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID,
432 "Device Path Utilities",
433 EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID,
436 "Unicode Collation 2",
437 EFI_UNICODE_COLLATION_PROTOCOL2_GUID,
441 EFI_DRIVER_BINDING_PROTOCOL_GUID,
445 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID,
448 "Simple Text Input Ex",
449 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID,
452 "Simple Text Output",
453 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID,
457 EFI_BLOCK_IO_PROTOCOL_GUID,
460 "Simple File System",
461 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
465 EFI_LOADED_IMAGE_PROTOCOL_GUID,
469 EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID,
473 EFI_HII_STRING_PROTOCOL_GUID,
477 EFI_HII_DATABASE_PROTOCOL_GUID,
480 "HII Config Routing",
481 EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID,
485 EFI_LOAD_FILE2_PROTOCOL_GUID,
488 "Random Number Generator",
489 EFI_RNG_PROTOCOL_GUID,
493 EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
497 EFI_PXE_BASE_CODE_PROTOCOL_GUID,
501 EFI_DT_FIXUP_PROTOCOL_GUID,
503 /* Configuration table GUIDs */
517 "Runtime properties",
518 EFI_RT_PROPERTIES_TABLE_GUID,
521 "TCG2 Final Events Table",
522 EFI_TCG2_FINAL_EVENTS_TABLE_GUID,
527 * get_guid_text - get string of GUID
529 * Return description of GUID.
532 * Return: description of GUID or NULL
534 static const char *get_guid_text(const void *guid)
538 for (i = 0; i < ARRAY_SIZE(guid_list); i++) {
540 * As guidcmp uses memcmp() we can safely accept unaligned
543 if (!guidcmp(&guid_list[i].guid, guid))
544 return guid_list[i].text;
551 * do_efi_show_handles() - show UEFI handles
553 * @cmdtp: Command table
554 * @flag: Command flag
555 * @argc: Number of arguments
556 * @argv: Argument array
557 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
559 * Implement efidebug "dh" sub-command.
560 * Show all UEFI handles and their information, currently all protocols
563 static int do_efi_show_handles(struct cmd_tbl *cmdtp, int flag,
564 int argc, char *const argv[])
566 efi_handle_t *handles;
568 efi_uintn_t num, count, i, j;
569 const char *guid_text;
572 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
574 if (ret != EFI_SUCCESS)
575 return CMD_RET_FAILURE;
578 return CMD_RET_SUCCESS;
580 printf("Handle%.*s Protocols\n", EFI_HANDLE_WIDTH - 6, spc);
581 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
582 for (i = 0; i < num; i++) {
583 printf("%p", handles[i]);
584 ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid,
591 for (j = 0; j < count; j++) {
597 guid_text = get_guid_text(guid[j]);
601 printf("%pUl", guid[j]);
606 efi_free_pool(handles);
608 return CMD_RET_SUCCESS;
612 * do_efi_show_images() - show UEFI images
614 * @cmdtp: Command table
615 * @flag: Command flag
616 * @argc: Number of arguments
617 * @argv: Argument array
618 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
620 * Implement efidebug "images" sub-command.
621 * Show all UEFI loaded images and their information.
623 static int do_efi_show_images(struct cmd_tbl *cmdtp, int flag,
624 int argc, char *const argv[])
626 efi_print_image_infos(NULL);
628 return CMD_RET_SUCCESS;
631 static const char * const efi_mem_type_string[] = {
632 [EFI_RESERVED_MEMORY_TYPE] = "RESERVED",
633 [EFI_LOADER_CODE] = "LOADER CODE",
634 [EFI_LOADER_DATA] = "LOADER DATA",
635 [EFI_BOOT_SERVICES_CODE] = "BOOT CODE",
636 [EFI_BOOT_SERVICES_DATA] = "BOOT DATA",
637 [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE",
638 [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA",
639 [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL",
640 [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM",
641 [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM",
642 [EFI_ACPI_MEMORY_NVS] = "ACPI NVS",
643 [EFI_MMAP_IO] = "IO",
644 [EFI_MMAP_IO_PORT] = "IO PORT",
645 [EFI_PAL_CODE] = "PAL",
646 [EFI_PERSISTENT_MEMORY_TYPE] = "PERSISTENT",
649 static const struct efi_mem_attrs {
652 } efi_mem_attrs[] = {
653 {EFI_MEMORY_UC, "UC"},
654 {EFI_MEMORY_UC, "UC"},
655 {EFI_MEMORY_WC, "WC"},
656 {EFI_MEMORY_WT, "WT"},
657 {EFI_MEMORY_WB, "WB"},
658 {EFI_MEMORY_UCE, "UCE"},
659 {EFI_MEMORY_WP, "WP"},
660 {EFI_MEMORY_RP, "RP"},
661 {EFI_MEMORY_XP, "WP"},
662 {EFI_MEMORY_NV, "NV"},
663 {EFI_MEMORY_MORE_RELIABLE, "REL"},
664 {EFI_MEMORY_RO, "RO"},
665 {EFI_MEMORY_SP, "SP"},
666 {EFI_MEMORY_RUNTIME, "RT"},
670 * print_memory_attributes() - print memory map attributes
672 * @attributes: Attribute value
674 * Print memory map attributes
676 static void print_memory_attributes(u64 attributes)
680 for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
681 if (attributes & efi_mem_attrs[i].bit) {
688 puts(efi_mem_attrs[i].text);
692 #define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
695 * do_efi_show_memmap() - show UEFI memory map
697 * @cmdtp: Command table
698 * @flag: Command flag
699 * @argc: Number of arguments
700 * @argv: Argument array
701 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
703 * Implement efidebug "memmap" sub-command.
704 * Show UEFI memory map.
706 static int do_efi_show_memmap(struct cmd_tbl *cmdtp, int flag,
707 int argc, char *const argv[])
709 struct efi_mem_desc *memmap = NULL, *map;
710 efi_uintn_t map_size = 0;
715 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
716 if (ret == EFI_BUFFER_TOO_SMALL) {
717 map_size += sizeof(struct efi_mem_desc); /* for my own */
718 ret = efi_allocate_pool(EFI_LOADER_DATA, map_size,
720 if (ret != EFI_SUCCESS)
721 return CMD_RET_FAILURE;
722 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
724 if (ret != EFI_SUCCESS) {
725 efi_free_pool(memmap);
726 return CMD_RET_FAILURE;
729 printf("Type Start%.*s End%.*s Attributes\n",
730 EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc);
731 printf("================ %.*s %.*s ==========\n",
732 EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep);
734 * Coverity check: dereferencing null pointer "map."
735 * This is a false positive as memmap will always be
736 * populated by allocate_pool() above.
738 for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
739 if (map->type < ARRAY_SIZE(efi_mem_type_string))
740 type = efi_mem_type_string[map->type];
744 printf("%-16s %.*llx-%.*llx", type,
746 (u64)map_to_sysmem((void *)(uintptr_t)
747 map->physical_start),
749 (u64)map_to_sysmem((void *)(uintptr_t)
750 (map->physical_start +
751 map->num_pages * EFI_PAGE_SIZE)));
753 print_memory_attributes(map->attribute);
757 efi_free_pool(memmap);
759 return CMD_RET_SUCCESS;
763 * do_efi_show_tables() - show UEFI configuration tables
765 * @cmdtp: Command table
766 * @flag: Command flag
767 * @argc: Number of arguments
768 * @argv: Argument array
769 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
771 * Implement efidebug "tables" sub-command.
772 * Show UEFI configuration tables.
774 static int do_efi_show_tables(struct cmd_tbl *cmdtp, int flag,
775 int argc, char *const argv[])
778 const char *guid_str;
780 for (i = 0; i < systab.nr_tables; ++i) {
781 guid_str = get_guid_text(&systab.tables[i].guid);
784 printf("%pUl %s\n", &systab.tables[i].guid, guid_str);
787 return CMD_RET_SUCCESS;
791 * do_efi_boot_add() - set UEFI load option
793 * @cmdtp: Command table
794 * @flag: Command flag
795 * @argc: Number of arguments
796 * @argv: Argument array
797 * Return: CMD_RET_SUCCESS on success,
798 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
800 * Implement efidebug "boot add" sub-command. Create or change UEFI load option.
802 * efidebug boot add <id> <label> <interface> <devnum>[:<part>] <file> <options>
804 static int do_efi_boot_add(struct cmd_tbl *cmdtp, int flag,
805 int argc, char *const argv[])
810 u16 var_name16[9], *p;
812 size_t label_len, label_len16;
814 struct efi_device_path *device_path = NULL, *file_path = NULL;
815 struct efi_load_option lo;
819 int r = CMD_RET_SUCCESS;
821 if (argc < 6 || argc > 7)
822 return CMD_RET_USAGE;
824 id = (int)simple_strtoul(argv[1], &endp, 16);
825 if (*endp != '\0' || id > 0xffff)
826 return CMD_RET_USAGE;
828 sprintf(var_name, "Boot%04X", id);
830 utf8_utf16_strncpy(&p, var_name, 9);
832 guid = efi_global_variable_guid;
835 lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
838 label_len = strlen(argv[2]);
839 label_len16 = utf8_utf16_strnlen(argv[2], label_len);
840 label = malloc((label_len16 + 1) * sizeof(u16));
842 return CMD_RET_FAILURE;
843 lo.label = label; /* label will be changed below */
844 utf8_utf16_strncpy(&label, argv[2], label_len);
847 ret = efi_dp_from_name(argv[3], argv[4], argv[5], &device_path,
849 if (ret != EFI_SUCCESS) {
850 printf("Cannot create device path for \"%s %s\"\n",
855 lo.file_path = file_path;
856 lo.file_path_length = efi_dp_size(file_path)
857 + sizeof(struct efi_device_path); /* for END */
861 lo.optional_data = NULL;
863 lo.optional_data = (const u8 *)argv[6];
865 size = efi_serialize_load_option(&lo, (u8 **)&data);
871 ret = EFI_CALL(efi_set_variable(var_name16, &guid,
872 EFI_VARIABLE_NON_VOLATILE |
873 EFI_VARIABLE_BOOTSERVICE_ACCESS |
874 EFI_VARIABLE_RUNTIME_ACCESS,
876 if (ret != EFI_SUCCESS) {
877 printf("Cannot set %ls\n", var_name16);
882 efi_free_pool(device_path);
883 efi_free_pool(file_path);
890 * do_efi_boot_rm() - delete UEFI load options
892 * @cmdtp: Command table
893 * @flag: Command flag
894 * @argc: Number of arguments
895 * @argv: Argument array
896 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
898 * Implement efidebug "boot rm" sub-command.
899 * Delete UEFI load options.
901 * efidebug boot rm <id> ...
903 static int do_efi_boot_rm(struct cmd_tbl *cmdtp, int flag,
904 int argc, char *const argv[])
910 u16 var_name16[9], *p;
914 return CMD_RET_USAGE;
916 guid = efi_global_variable_guid;
917 for (i = 1; i < argc; i++, argv++) {
918 id = (int)simple_strtoul(argv[1], &endp, 16);
919 if (*endp != '\0' || id > 0xffff)
920 return CMD_RET_FAILURE;
922 sprintf(var_name, "Boot%04X", id);
924 utf8_utf16_strncpy(&p, var_name, 9);
926 ret = EFI_CALL(efi_set_variable(var_name16, &guid, 0, 0, NULL));
928 printf("Cannot remove %ls\n", var_name16);
929 return CMD_RET_FAILURE;
933 return CMD_RET_SUCCESS;
937 * show_efi_boot_opt_data() - dump UEFI load option
939 * @varname16: variable name
940 * @data: value of UEFI load option variable
941 * @size: size of the boot option
943 * Decode the value of UEFI load option variable and print information.
945 static void show_efi_boot_opt_data(u16 *varname16, void *data, size_t *size)
947 struct efi_load_option lo;
949 size_t label_len16, label_len;
953 ret = efi_deserialize_load_option(&lo, data, size);
954 if (ret != EFI_SUCCESS) {
955 printf("%ls: invalid load option\n", varname16);
959 label_len16 = u16_strlen(lo.label);
960 label_len = utf16_utf8_strnlen(lo.label, label_len16);
961 label = malloc(label_len + 1);
965 utf16_utf8_strncpy(&p, lo.label, label_len16);
967 printf("%ls:\nattributes: %c%c%c (0x%08x)\n",
970 lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
971 /* FORCE RECONNECT */
972 lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
974 lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
976 printf(" label: %s\n", label);
978 dp_str = efi_dp_str(lo.file_path);
979 printf(" file_path: %ls\n", dp_str);
980 efi_free_pool(dp_str);
983 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
984 lo.optional_data, *size, true);
989 * show_efi_boot_opt() - dump UEFI load option
991 * @varname16: variable name
993 * Dump information defined by UEFI load option.
995 static void show_efi_boot_opt(u16 *varname16)
1002 ret = EFI_CALL(efi_get_variable(varname16, &efi_global_variable_guid,
1003 NULL, &size, NULL));
1004 if (ret == EFI_BUFFER_TOO_SMALL) {
1005 data = malloc(size);
1007 printf("ERROR: Out of memory\n");
1010 ret = EFI_CALL(efi_get_variable(varname16,
1011 &efi_global_variable_guid,
1012 NULL, &size, data));
1013 if (ret == EFI_SUCCESS)
1014 show_efi_boot_opt_data(varname16, data, &size);
1019 static int u16_tohex(u16 c)
1021 if (c >= '0' && c <= '9')
1023 if (c >= 'A' && c <= 'F')
1024 return c - 'A' + 10;
1026 /* not hexadecimal */
1031 * show_efi_boot_dump() - dump all UEFI load options
1033 * @cmdtp: Command table
1034 * @flag: Command flag
1035 * @argc: Number of arguments
1036 * @argv: Argument array
1037 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1039 * Implement efidebug "boot dump" sub-command.
1040 * Dump information of all UEFI load options defined.
1042 * efidebug boot dump
1044 static int do_efi_boot_dump(struct cmd_tbl *cmdtp, int flag,
1045 int argc, char *const argv[])
1047 u16 *var_name16, *p;
1048 efi_uintn_t buf_size, size;
1054 return CMD_RET_USAGE;
1057 var_name16 = malloc(buf_size);
1059 return CMD_RET_FAILURE;
1064 ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
1066 if (ret == EFI_NOT_FOUND)
1068 if (ret == EFI_BUFFER_TOO_SMALL) {
1070 p = realloc(var_name16, buf_size);
1073 return CMD_RET_FAILURE;
1076 ret = EFI_CALL(efi_get_next_variable_name(&size,
1080 if (ret != EFI_SUCCESS) {
1082 return CMD_RET_FAILURE;
1085 if (memcmp(var_name16, L"Boot", 8))
1088 for (id = 0, i = 0; i < 4; i++) {
1089 digit = u16_tohex(var_name16[4 + i]);
1092 id = (id << 4) + digit;
1094 if (i == 4 && !var_name16[8])
1095 show_efi_boot_opt(var_name16);
1100 return CMD_RET_SUCCESS;
1104 * show_efi_boot_order() - show order of UEFI load options
1106 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1108 * Show order of UEFI load options defined by BootOrder variable.
1110 static int show_efi_boot_order(void)
1116 u16 var_name16[9], *p16;
1118 struct efi_load_option lo;
1120 size_t label_len16, label_len;
1124 ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid,
1125 NULL, &size, NULL));
1126 if (ret != EFI_BUFFER_TOO_SMALL) {
1127 if (ret == EFI_NOT_FOUND) {
1128 printf("BootOrder not defined\n");
1129 return CMD_RET_SUCCESS;
1131 return CMD_RET_FAILURE;
1134 bootorder = malloc(size);
1136 printf("ERROR: Out of memory\n");
1137 return CMD_RET_FAILURE;
1139 ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid,
1140 NULL, &size, bootorder));
1141 if (ret != EFI_SUCCESS) {
1142 ret = CMD_RET_FAILURE;
1146 num = size / sizeof(u16);
1147 for (i = 0; i < num; i++) {
1148 sprintf(var_name, "Boot%04X", bootorder[i]);
1150 utf8_utf16_strncpy(&p16, var_name, 9);
1153 ret = EFI_CALL(efi_get_variable(var_name16,
1154 &efi_global_variable_guid, NULL,
1156 if (ret != EFI_BUFFER_TOO_SMALL) {
1157 printf("%2d: %s: (not defined)\n", i + 1, var_name);
1161 data = malloc(size);
1163 ret = CMD_RET_FAILURE;
1166 ret = EFI_CALL(efi_get_variable(var_name16,
1167 &efi_global_variable_guid, NULL,
1169 if (ret != EFI_SUCCESS) {
1171 ret = CMD_RET_FAILURE;
1175 ret = efi_deserialize_load_option(&lo, data, &size);
1176 if (ret != EFI_SUCCESS) {
1177 printf("%ls: invalid load option\n", var_name16);
1178 ret = CMD_RET_FAILURE;
1182 label_len16 = u16_strlen(lo.label);
1183 label_len = utf16_utf8_strnlen(lo.label, label_len16);
1184 label = malloc(label_len + 1);
1187 ret = CMD_RET_FAILURE;
1191 utf16_utf8_strncpy(&p, lo.label, label_len16);
1192 printf("%2d: %s: %s\n", i + 1, var_name, label);
1204 * do_efi_boot_next() - manage UEFI BootNext variable
1206 * @cmdtp: Command table
1207 * @flag: Command flag
1208 * @argc: Number of arguments
1209 * @argv: Argument array
1210 * Return: CMD_RET_SUCCESS on success,
1211 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1213 * Implement efidebug "boot next" sub-command.
1214 * Set BootNext variable.
1216 * efidebug boot next <id>
1218 static int do_efi_boot_next(struct cmd_tbl *cmdtp, int flag,
1219 int argc, char *const argv[])
1226 int r = CMD_RET_SUCCESS;
1229 return CMD_RET_USAGE;
1231 bootnext = (u16)simple_strtoul(argv[1], &endp, 16);
1233 printf("invalid value: %s\n", argv[1]);
1234 r = CMD_RET_FAILURE;
1238 guid = efi_global_variable_guid;
1240 ret = EFI_CALL(efi_set_variable(L"BootNext", &guid,
1241 EFI_VARIABLE_NON_VOLATILE |
1242 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1243 EFI_VARIABLE_RUNTIME_ACCESS,
1245 if (ret != EFI_SUCCESS) {
1246 printf("Cannot set BootNext\n");
1247 r = CMD_RET_FAILURE;
1254 * do_efi_boot_order() - manage UEFI BootOrder variable
1256 * @cmdtp: Command table
1257 * @flag: Command flag
1258 * @argc: Number of arguments
1259 * @argv: Argument array
1260 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1262 * Implement efidebug "boot order" sub-command.
1263 * Show order of UEFI load options, or change it in BootOrder variable.
1265 * efidebug boot order [<id> ...]
1267 static int do_efi_boot_order(struct cmd_tbl *cmdtp, int flag,
1268 int argc, char *const argv[])
1270 u16 *bootorder = NULL;
1276 int r = CMD_RET_SUCCESS;
1279 return show_efi_boot_order();
1284 size = argc * sizeof(u16);
1285 bootorder = malloc(size);
1287 return CMD_RET_FAILURE;
1289 for (i = 0; i < argc; i++) {
1290 id = (int)simple_strtoul(argv[i], &endp, 16);
1291 if (*endp != '\0' || id > 0xffff) {
1292 printf("invalid value: %s\n", argv[i]);
1293 r = CMD_RET_FAILURE;
1297 bootorder[i] = (u16)id;
1300 guid = efi_global_variable_guid;
1301 ret = EFI_CALL(efi_set_variable(L"BootOrder", &guid,
1302 EFI_VARIABLE_NON_VOLATILE |
1303 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1304 EFI_VARIABLE_RUNTIME_ACCESS,
1306 if (ret != EFI_SUCCESS) {
1307 printf("Cannot set BootOrder\n");
1308 r = CMD_RET_FAILURE;
1316 static struct cmd_tbl cmd_efidebug_boot_sub[] = {
1317 U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
1318 U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
1319 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
1320 U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
1321 U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
1326 * do_efi_boot_opt() - manage UEFI load options
1328 * @cmdtp: Command table
1329 * @flag: Command flag
1330 * @argc: Number of arguments
1331 * @argv: Argument array
1332 * Return: CMD_RET_SUCCESS on success,
1333 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1335 * Implement efidebug "boot" sub-command.
1337 static int do_efi_boot_opt(struct cmd_tbl *cmdtp, int flag,
1338 int argc, char *const argv[])
1343 return CMD_RET_USAGE;
1347 cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
1348 ARRAY_SIZE(cmd_efidebug_boot_sub));
1350 return CMD_RET_USAGE;
1352 return cp->cmd(cmdtp, flag, argc, argv);
1356 * do_efi_test_bootmgr() - run simple bootmgr for test
1358 * @cmdtp: Command table
1359 * @flag: Command flag
1360 * @argc: Number of arguments
1361 * @argv: Argument array
1362 * Return: CMD_RET_SUCCESS on success,
1363 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1365 * Implement efidebug "test bootmgr" sub-command.
1366 * Run simple bootmgr for test.
1368 * efidebug test bootmgr
1370 static __maybe_unused int do_efi_test_bootmgr(struct cmd_tbl *cmdtp, int flag,
1371 int argc, char * const argv[])
1374 efi_uintn_t exit_data_size = 0;
1375 u16 *exit_data = NULL;
1377 void *load_options = NULL;
1379 ret = efi_bootmgr_load(&image, &load_options);
1380 printf("efi_bootmgr_load() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1382 /* We call efi_start_image() even if error for test purpose. */
1383 ret = EFI_CALL(efi_start_image(image, &exit_data_size, &exit_data));
1384 printf("efi_start_image() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1385 if (ret && exit_data)
1386 efi_free_pool(exit_data);
1391 return CMD_RET_SUCCESS;
1394 static struct cmd_tbl cmd_efidebug_test_sub[] = {
1395 #ifdef CONFIG_CMD_BOOTEFI_BOOTMGR
1396 U_BOOT_CMD_MKENT(bootmgr, CONFIG_SYS_MAXARGS, 1, do_efi_test_bootmgr,
1402 * do_efi_test() - manage UEFI load options
1404 * @cmdtp: Command table
1405 * @flag: Command flag
1406 * @argc: Number of arguments
1407 * @argv: Argument array
1408 * Return: CMD_RET_SUCCESS on success,
1409 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1411 * Implement efidebug "test" sub-command.
1413 static int do_efi_test(struct cmd_tbl *cmdtp, int flag,
1414 int argc, char * const argv[])
1419 return CMD_RET_USAGE;
1423 cp = find_cmd_tbl(argv[0], cmd_efidebug_test_sub,
1424 ARRAY_SIZE(cmd_efidebug_test_sub));
1426 return CMD_RET_USAGE;
1428 return cp->cmd(cmdtp, flag, argc, argv);
1432 * do_efi_query_info() - QueryVariableInfo EFI service
1434 * @cmdtp: Command table
1435 * @flag: Command flag
1436 * @argc: Number of arguments
1437 * @argv: Argument array
1438 * Return: CMD_RET_SUCCESS on success,
1439 * CMD_RET_USAGE or CMD_RET_FAILURE on failure
1441 * Implement efidebug "test" sub-command.
1444 static int do_efi_query_info(struct cmd_tbl *cmdtp, int flag,
1445 int argc, char * const argv[])
1449 u64 max_variable_storage_size;
1450 u64 remain_variable_storage_size;
1451 u64 max_variable_size;
1454 for (i = 1; i < argc; i++) {
1455 if (!strcmp(argv[i], "-bs"))
1456 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
1457 else if (!strcmp(argv[i], "-rt"))
1458 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
1459 else if (!strcmp(argv[i], "-nv"))
1460 attr |= EFI_VARIABLE_NON_VOLATILE;
1461 else if (!strcmp(argv[i], "-at"))
1463 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
1466 ret = EFI_CALL(efi_query_variable_info(attr,
1467 &max_variable_storage_size,
1468 &remain_variable_storage_size,
1469 &max_variable_size));
1470 if (ret != EFI_SUCCESS) {
1471 printf("Error: Cannot query UEFI variables, r = %lu\n",
1472 ret & ~EFI_ERROR_MASK);
1473 return CMD_RET_FAILURE;
1476 printf("Max storage size %llu\n", max_variable_storage_size);
1477 printf("Remaining storage size %llu\n", remain_variable_storage_size);
1478 printf("Max variable size %llu\n", max_variable_size);
1480 return CMD_RET_SUCCESS;
1483 static struct cmd_tbl cmd_efidebug_sub[] = {
1484 U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
1485 #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
1486 U_BOOT_CMD_MKENT(capsule, CONFIG_SYS_MAXARGS, 1, do_efi_capsule,
1489 U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices,
1491 U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
1493 U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
1495 U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
1497 U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
1499 U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables,
1501 U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_efi_test,
1503 U_BOOT_CMD_MKENT(query, CONFIG_SYS_MAXARGS, 1, do_efi_query_info,
1508 * do_efidebug() - display and configure UEFI environment
1510 * @cmdtp: Command table
1511 * @flag: Command flag
1512 * @argc: Number of arguments
1513 * @argv: Argument array
1514 * Return: CMD_RET_SUCCESS on success,
1515 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1517 * Implement efidebug command which allows us to display and
1518 * configure UEFI environment.
1520 static int do_efidebug(struct cmd_tbl *cmdtp, int flag,
1521 int argc, char *const argv[])
1527 return CMD_RET_USAGE;
1531 /* Initialize UEFI drivers */
1532 r = efi_init_obj_list();
1533 if (r != EFI_SUCCESS) {
1534 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1535 r & ~EFI_ERROR_MASK);
1536 return CMD_RET_FAILURE;
1539 cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
1540 ARRAY_SIZE(cmd_efidebug_sub));
1542 return CMD_RET_USAGE;
1544 return cp->cmd(cmdtp, flag, argc, argv);
1547 #ifdef CONFIG_SYS_LONGHELP
1548 static char efidebug_help_text[] =
1549 " - UEFI Shell-like interface to configure UEFI environment\n"
1551 "efidebug boot add <bootid> <label> <interface> <devnum>[:<part>] <file path> [<load options>]\n"
1552 " - set UEFI BootXXXX variable\n"
1553 " <load options> will be passed to UEFI application\n"
1554 "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
1555 " - delete UEFI BootXXXX variables\n"
1556 "efidebug boot dump\n"
1557 " - dump all UEFI BootXXXX variables\n"
1558 "efidebug boot next <bootid>\n"
1559 " - set UEFI BootNext variable\n"
1560 "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
1561 " - set/show UEFI boot order\n"
1563 #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
1564 "efidebug capsule update [-v] <capsule address>\n"
1565 " - process a capsule\n"
1566 "efidebug capsule disk-update\n"
1567 " - update a capsule from disk\n"
1568 "efidebug capsule show <capsule address>\n"
1569 " - show capsule information\n"
1570 "efidebug capsule result [<capsule result var>]\n"
1571 " - show a capsule update result\n"
1574 "efidebug devices\n"
1575 " - show UEFI devices\n"
1576 "efidebug drivers\n"
1577 " - show UEFI drivers\n"
1579 " - show UEFI handles\n"
1581 " - show loaded images\n"
1583 " - show UEFI memory map\n"
1585 " - show UEFI configuration tables\n"
1586 #ifdef CONFIG_CMD_BOOTEFI_BOOTMGR
1587 "efidebug test bootmgr\n"
1588 " - run simple bootmgr for test\n"
1590 "efidebug query [-nv][-bs][-rt][-at]\n"
1591 " - show size of UEFI variables store\n";
1595 efidebug, 10, 0, do_efidebug,
1596 "Configure UEFI environment",