1 // SPDX-License-Identifier: GPL-2.0+
3 * UEFI Shell-like command
5 * Copyright (c) 2018 AKASHI Takahiro, Linaro Limited
11 #include <efi_loader.h>
19 #include <linux/ctype.h>
21 #define BS systab.boottime
22 #define RT systab.runtime
24 #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
26 * do_efi_capsule_update() - process a capsule update
28 * @cmdtp: Command table
30 * @argc: Number of arguments
31 * @argv: Argument array
32 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
34 * Implement efidebug "capsule update" sub-command.
35 * process a capsule update.
37 * efidebug capsule update [-v] <capsule address>
39 static int do_efi_capsule_update(struct cmd_tbl *cmdtp, int flag,
40 int argc, char * const argv[])
42 struct efi_capsule_header *capsule;
47 if (argc != 2 && argc != 3)
51 if (strcmp(argv[1], "-v"))
59 capsule = (typeof(capsule))simple_strtoul(argv[1], &endp, 16);
60 if (endp == argv[1]) {
61 printf("Invalid address: %s", argv[1]);
62 return CMD_RET_FAILURE;
66 printf("Capsule guid: %pUl\n", &capsule->capsule_guid);
67 printf("Capsule flags: 0x%x\n", capsule->flags);
68 printf("Capsule header size: 0x%x\n", capsule->header_size);
69 printf("Capsule image size: 0x%x\n",
70 capsule->capsule_image_size);
73 ret = EFI_CALL(RT->update_capsule(&capsule, 1, (u64)NULL));
75 printf("Cannot handle a capsule at %p", capsule);
76 return CMD_RET_FAILURE;
79 return CMD_RET_SUCCESS;
82 static int do_efi_capsule_on_disk_update(struct cmd_tbl *cmdtp, int flag,
83 int argc, char * const argv[])
87 ret = efi_launch_capsules();
89 return ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
93 * do_efi_capsule_show() - show capsule information
95 * @cmdtp: Command table
97 * @argc: Number of arguments
98 * @argv: Argument array
99 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
101 * Implement efidebug "capsule show" sub-command.
102 * show capsule information.
104 * efidebug capsule show <capsule address>
106 static int do_efi_capsule_show(struct cmd_tbl *cmdtp, int flag,
107 int argc, char * const argv[])
109 struct efi_capsule_header *capsule;
113 return CMD_RET_USAGE;
115 capsule = (typeof(capsule))simple_strtoul(argv[1], &endp, 16);
116 if (endp == argv[1]) {
117 printf("Invalid address: %s", argv[1]);
118 return CMD_RET_FAILURE;
121 printf("Capsule guid: %pUl\n", &capsule->capsule_guid);
122 printf("Capsule flags: 0x%x\n", capsule->flags);
123 printf("Capsule header size: 0x%x\n", capsule->header_size);
124 printf("Capsule image size: 0x%x\n",
125 capsule->capsule_image_size);
127 return CMD_RET_SUCCESS;
131 * do_efi_capsule_res() - show a capsule update result
133 * @cmdtp: Command table
134 * @flag: Command flag
135 * @argc: Number of arguments
136 * @argv: Argument array
137 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
139 * Implement efidebug "capsule result" sub-command.
140 * show a capsule update result.
141 * If result number is not specified, CapsuleLast will be shown.
143 * efidebug capsule result [<capsule result number>]
145 static int do_efi_capsule_res(struct cmd_tbl *cmdtp, int flag,
146 int argc, char * const argv[])
151 u16 var_name16[12], *p;
153 struct efi_capsule_result_variable_header *result = NULL;
157 if (argc != 1 && argc != 2)
158 return CMD_RET_USAGE;
160 guid = efi_guid_capsule_report;
162 size = sizeof(var_name16);
163 ret = EFI_CALL(RT->get_variable(L"CapsuleLast", &guid, NULL,
165 if (ret != EFI_SUCCESS) {
166 if (ret == EFI_NOT_FOUND)
167 printf("CapsuleLast doesn't exist\n");
169 printf("Failed to get CapsuleLast\n");
171 return CMD_RET_FAILURE;
173 printf("CapsuleLast is %ls\n", var_name16);
178 capsule_id = simple_strtoul(argv[0], &endp, 16);
179 if (capsule_id < 0 || capsule_id > 0xffff)
180 return CMD_RET_USAGE;
182 sprintf(var_name, "Capsule%04X", capsule_id);
184 utf8_utf16_strncpy(&p, var_name, 9);
188 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, NULL));
189 if (ret == EFI_BUFFER_TOO_SMALL) {
190 result = malloc(size);
191 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
193 if (ret != EFI_SUCCESS) {
195 printf("Failed to get %ls\n", var_name16);
197 return CMD_RET_FAILURE;
201 printf("Result total size: 0x%x\n", result->variable_total_size);
202 printf("Capsule guid: %pUl\n", &result->capsule_guid);
203 printf("Time processed: %04d-%02d-%02d %02d:%02d:%02d\n",
204 result->capsule_processed.year, result->capsule_processed.month,
205 result->capsule_processed.day, result->capsule_processed.hour,
206 result->capsule_processed.minute,
207 result->capsule_processed.second);
208 printf("Capsule status: 0x%lx\n", result->capsule_status);
212 return CMD_RET_SUCCESS;
215 static struct cmd_tbl cmd_efidebug_capsule_sub[] = {
216 U_BOOT_CMD_MKENT(update, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_update,
218 U_BOOT_CMD_MKENT(show, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_show,
220 U_BOOT_CMD_MKENT(disk-update, 0, 0, do_efi_capsule_on_disk_update,
222 U_BOOT_CMD_MKENT(result, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_res,
227 * do_efi_capsule() - manage UEFI capsules
229 * @cmdtp: Command table
230 * @flag: Command flag
231 * @argc: Number of arguments
232 * @argv: Argument array
233 * Return: CMD_RET_SUCCESS on success,
234 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
236 * Implement efidebug "capsule" sub-command.
238 static int do_efi_capsule(struct cmd_tbl *cmdtp, int flag,
239 int argc, char * const argv[])
244 return CMD_RET_USAGE;
248 cp = find_cmd_tbl(argv[0], cmd_efidebug_capsule_sub,
249 ARRAY_SIZE(cmd_efidebug_capsule_sub));
251 return CMD_RET_USAGE;
253 return cp->cmd(cmdtp, flag, argc, argv);
255 #endif /* CONFIG_EFI_HAVE_CAPSULE_SUPPORT */
258 * efi_get_device_handle_info() - get information of UEFI device
260 * @handle: Handle of UEFI device
261 * @dev_path_text: Pointer to text of device path
262 * Return: 0 on success, -1 on failure
264 * Currently return a formatted text of device path.
266 static int efi_get_device_handle_info(efi_handle_t handle, u16 **dev_path_text)
268 struct efi_device_path *dp;
271 ret = EFI_CALL(BS->open_protocol(handle, &efi_guid_device_path,
272 (void **)&dp, NULL /* FIXME */, NULL,
273 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
274 if (ret == EFI_SUCCESS) {
275 *dev_path_text = efi_dp_str(dp);
282 #define EFI_HANDLE_WIDTH ((int)sizeof(efi_handle_t) * 2)
284 static const char spc[] = " ";
285 static const char sep[] = "================";
288 * do_efi_show_devices() - show UEFI devices
290 * @cmdtp: Command table
291 * @flag: Command flag
292 * @argc: Number of arguments
293 * @argv: Argument array
294 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
296 * Implement efidebug "devices" sub-command.
297 * Show all UEFI devices and their information.
299 static int do_efi_show_devices(struct cmd_tbl *cmdtp, int flag,
300 int argc, char *const argv[])
302 efi_handle_t *handles;
307 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
309 if (ret != EFI_SUCCESS)
310 return CMD_RET_FAILURE;
313 return CMD_RET_SUCCESS;
315 printf("Device%.*s Device Path\n", EFI_HANDLE_WIDTH - 6, spc);
316 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
317 for (i = 0; i < num; i++) {
318 if (!efi_get_device_handle_info(handles[i], &dev_path_text)) {
319 printf("%p %ls\n", handles[i], dev_path_text);
320 efi_free_pool(dev_path_text);
324 efi_free_pool(handles);
326 return CMD_RET_SUCCESS;
330 * efi_get_driver_handle_info() - get information of UEFI driver
332 * @handle: Handle of UEFI device
333 * @driver_name: Driver name
334 * @image_path: Pointer to text of device path
335 * Return: 0 on success, -1 on failure
337 * Currently return no useful information as all UEFI drivers are
340 static int efi_get_driver_handle_info(efi_handle_t handle, u16 **driver_name,
343 struct efi_handler *handler;
344 struct efi_loaded_image *image;
349 * TODO: support EFI_COMPONENT_NAME2_PROTOCOL
354 ret = efi_search_protocol(handle, &efi_guid_loaded_image, &handler);
355 if (ret != EFI_SUCCESS) {
360 image = handler->protocol_interface;
361 *image_path = efi_dp_str(image->file_path);
367 * do_efi_show_drivers() - show UEFI drivers
369 * @cmdtp: Command table
370 * @flag: Command flag
371 * @argc: Number of arguments
372 * @argv: Argument array
373 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
375 * Implement efidebug "drivers" sub-command.
376 * Show all UEFI drivers and their information.
378 static int do_efi_show_drivers(struct cmd_tbl *cmdtp, int flag,
379 int argc, char *const argv[])
381 efi_handle_t *handles;
383 u16 *driver_name, *image_path_text;
386 ret = EFI_CALL(efi_locate_handle_buffer(
387 BY_PROTOCOL, &efi_guid_driver_binding_protocol,
388 NULL, &num, &handles));
389 if (ret != EFI_SUCCESS)
390 return CMD_RET_FAILURE;
393 return CMD_RET_SUCCESS;
395 printf("Driver%.*s Name Image Path\n",
396 EFI_HANDLE_WIDTH - 6, spc);
397 printf("%.*s ==================== ====================\n",
398 EFI_HANDLE_WIDTH, sep);
399 for (i = 0; i < num; i++) {
400 if (!efi_get_driver_handle_info(handles[i], &driver_name,
403 printf("%p %-20ls %ls\n", handles[i],
404 driver_name, image_path_text);
406 printf("%p %-20ls <built-in>\n",
407 handles[i], driver_name);
408 efi_free_pool(driver_name);
409 efi_free_pool(image_path_text);
413 efi_free_pool(handles);
415 return CMD_RET_SUCCESS;
418 static const struct {
420 const efi_guid_t guid;
424 EFI_DEVICE_PATH_PROTOCOL_GUID,
427 "Device Path To Text",
428 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID,
431 "Device Path Utilities",
432 EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID,
435 "Unicode Collation 2",
436 EFI_UNICODE_COLLATION_PROTOCOL2_GUID,
440 EFI_DRIVER_BINDING_PROTOCOL_GUID,
444 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID,
447 "Simple Text Input Ex",
448 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID,
451 "Simple Text Output",
452 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID,
456 EFI_BLOCK_IO_PROTOCOL_GUID,
459 "Simple File System",
460 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
464 EFI_LOADED_IMAGE_PROTOCOL_GUID,
468 EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID,
472 EFI_HII_STRING_PROTOCOL_GUID,
476 EFI_HII_DATABASE_PROTOCOL_GUID,
479 "HII Config Routing",
480 EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID,
484 EFI_LOAD_FILE2_PROTOCOL_GUID,
487 "Random Number Generator",
488 EFI_RNG_PROTOCOL_GUID,
492 EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
496 EFI_PXE_BASE_CODE_PROTOCOL_GUID,
498 /* Configuration table GUIDs */
512 "Runtime properties",
513 EFI_RT_PROPERTIES_TABLE_GUID,
516 "TCG2 Final Events Table",
517 EFI_TCG2_FINAL_EVENTS_TABLE_GUID,
522 * get_guid_text - get string of GUID
524 * Return description of GUID.
527 * Return: description of GUID or NULL
529 static const char *get_guid_text(const void *guid)
533 for (i = 0; i < ARRAY_SIZE(guid_list); i++) {
535 * As guidcmp uses memcmp() we can safely accept unaligned
538 if (!guidcmp(&guid_list[i].guid, guid))
539 return guid_list[i].text;
546 * do_efi_show_handles() - show UEFI handles
548 * @cmdtp: Command table
549 * @flag: Command flag
550 * @argc: Number of arguments
551 * @argv: Argument array
552 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
554 * Implement efidebug "dh" sub-command.
555 * Show all UEFI handles and their information, currently all protocols
558 static int do_efi_show_handles(struct cmd_tbl *cmdtp, int flag,
559 int argc, char *const argv[])
561 efi_handle_t *handles;
563 efi_uintn_t num, count, i, j;
564 const char *guid_text;
567 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
569 if (ret != EFI_SUCCESS)
570 return CMD_RET_FAILURE;
573 return CMD_RET_SUCCESS;
575 printf("Handle%.*s Protocols\n", EFI_HANDLE_WIDTH - 6, spc);
576 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
577 for (i = 0; i < num; i++) {
578 printf("%p", handles[i]);
579 ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid,
586 for (j = 0; j < count; j++) {
592 guid_text = get_guid_text(guid[j]);
596 printf("%pUl", guid[j]);
601 efi_free_pool(handles);
603 return CMD_RET_SUCCESS;
607 * do_efi_show_images() - show UEFI images
609 * @cmdtp: Command table
610 * @flag: Command flag
611 * @argc: Number of arguments
612 * @argv: Argument array
613 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
615 * Implement efidebug "images" sub-command.
616 * Show all UEFI loaded images and their information.
618 static int do_efi_show_images(struct cmd_tbl *cmdtp, int flag,
619 int argc, char *const argv[])
621 efi_print_image_infos(NULL);
623 return CMD_RET_SUCCESS;
626 static const char * const efi_mem_type_string[] = {
627 [EFI_RESERVED_MEMORY_TYPE] = "RESERVED",
628 [EFI_LOADER_CODE] = "LOADER CODE",
629 [EFI_LOADER_DATA] = "LOADER DATA",
630 [EFI_BOOT_SERVICES_CODE] = "BOOT CODE",
631 [EFI_BOOT_SERVICES_DATA] = "BOOT DATA",
632 [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE",
633 [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA",
634 [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL",
635 [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM",
636 [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM",
637 [EFI_ACPI_MEMORY_NVS] = "ACPI NVS",
638 [EFI_MMAP_IO] = "IO",
639 [EFI_MMAP_IO_PORT] = "IO PORT",
640 [EFI_PAL_CODE] = "PAL",
641 [EFI_PERSISTENT_MEMORY_TYPE] = "PERSISTENT",
644 static const struct efi_mem_attrs {
647 } efi_mem_attrs[] = {
648 {EFI_MEMORY_UC, "UC"},
649 {EFI_MEMORY_UC, "UC"},
650 {EFI_MEMORY_WC, "WC"},
651 {EFI_MEMORY_WT, "WT"},
652 {EFI_MEMORY_WB, "WB"},
653 {EFI_MEMORY_UCE, "UCE"},
654 {EFI_MEMORY_WP, "WP"},
655 {EFI_MEMORY_RP, "RP"},
656 {EFI_MEMORY_XP, "WP"},
657 {EFI_MEMORY_NV, "NV"},
658 {EFI_MEMORY_MORE_RELIABLE, "REL"},
659 {EFI_MEMORY_RO, "RO"},
660 {EFI_MEMORY_SP, "SP"},
661 {EFI_MEMORY_RUNTIME, "RT"},
665 * print_memory_attributes() - print memory map attributes
667 * @attributes: Attribute value
669 * Print memory map attributes
671 static void print_memory_attributes(u64 attributes)
675 for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
676 if (attributes & efi_mem_attrs[i].bit) {
683 puts(efi_mem_attrs[i].text);
687 #define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
690 * do_efi_show_memmap() - show UEFI memory map
692 * @cmdtp: Command table
693 * @flag: Command flag
694 * @argc: Number of arguments
695 * @argv: Argument array
696 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
698 * Implement efidebug "memmap" sub-command.
699 * Show UEFI memory map.
701 static int do_efi_show_memmap(struct cmd_tbl *cmdtp, int flag,
702 int argc, char *const argv[])
704 struct efi_mem_desc *memmap = NULL, *map;
705 efi_uintn_t map_size = 0;
710 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
711 if (ret == EFI_BUFFER_TOO_SMALL) {
712 map_size += sizeof(struct efi_mem_desc); /* for my own */
713 ret = efi_allocate_pool(EFI_LOADER_DATA, map_size,
715 if (ret != EFI_SUCCESS)
716 return CMD_RET_FAILURE;
717 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
719 if (ret != EFI_SUCCESS) {
720 efi_free_pool(memmap);
721 return CMD_RET_FAILURE;
724 printf("Type Start%.*s End%.*s Attributes\n",
725 EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc);
726 printf("================ %.*s %.*s ==========\n",
727 EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep);
729 * Coverity check: dereferencing null pointer "map."
730 * This is a false positive as memmap will always be
731 * populated by allocate_pool() above.
733 for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
734 if (map->type < ARRAY_SIZE(efi_mem_type_string))
735 type = efi_mem_type_string[map->type];
739 printf("%-16s %.*llx-%.*llx", type,
741 (u64)map_to_sysmem((void *)(uintptr_t)
742 map->physical_start),
744 (u64)map_to_sysmem((void *)(uintptr_t)
745 (map->physical_start +
746 map->num_pages * EFI_PAGE_SIZE)));
748 print_memory_attributes(map->attribute);
752 efi_free_pool(memmap);
754 return CMD_RET_SUCCESS;
758 * do_efi_show_tables() - show UEFI configuration tables
760 * @cmdtp: Command table
761 * @flag: Command flag
762 * @argc: Number of arguments
763 * @argv: Argument array
764 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
766 * Implement efidebug "tables" sub-command.
767 * Show UEFI configuration tables.
769 static int do_efi_show_tables(struct cmd_tbl *cmdtp, int flag,
770 int argc, char *const argv[])
773 const char *guid_str;
775 for (i = 0; i < systab.nr_tables; ++i) {
776 guid_str = get_guid_text(&systab.tables[i].guid);
779 printf("%pUl %s\n", &systab.tables[i].guid, guid_str);
782 return CMD_RET_SUCCESS;
786 * do_efi_boot_add() - set UEFI load option
788 * @cmdtp: Command table
789 * @flag: Command flag
790 * @argc: Number of arguments
791 * @argv: Argument array
792 * Return: CMD_RET_SUCCESS on success,
793 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
795 * Implement efidebug "boot add" sub-command. Create or change UEFI load option.
797 * efidebug boot add <id> <label> <interface> <devnum>[:<part>] <file> <options>
799 static int do_efi_boot_add(struct cmd_tbl *cmdtp, int flag,
800 int argc, char *const argv[])
805 u16 var_name16[9], *p;
807 size_t label_len, label_len16;
809 struct efi_device_path *device_path = NULL, *file_path = NULL;
810 struct efi_load_option lo;
814 int r = CMD_RET_SUCCESS;
816 if (argc < 6 || argc > 7)
817 return CMD_RET_USAGE;
819 id = (int)simple_strtoul(argv[1], &endp, 16);
820 if (*endp != '\0' || id > 0xffff)
821 return CMD_RET_USAGE;
823 sprintf(var_name, "Boot%04X", id);
825 utf8_utf16_strncpy(&p, var_name, 9);
827 guid = efi_global_variable_guid;
830 lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
833 label_len = strlen(argv[2]);
834 label_len16 = utf8_utf16_strnlen(argv[2], label_len);
835 label = malloc((label_len16 + 1) * sizeof(u16));
837 return CMD_RET_FAILURE;
838 lo.label = label; /* label will be changed below */
839 utf8_utf16_strncpy(&label, argv[2], label_len);
842 ret = efi_dp_from_name(argv[3], argv[4], argv[5], &device_path,
844 if (ret != EFI_SUCCESS) {
845 printf("Cannot create device path for \"%s %s\"\n",
850 lo.file_path = file_path;
851 lo.file_path_length = efi_dp_size(file_path)
852 + sizeof(struct efi_device_path); /* for END */
856 lo.optional_data = NULL;
858 lo.optional_data = (const u8 *)argv[6];
860 size = efi_serialize_load_option(&lo, (u8 **)&data);
866 ret = EFI_CALL(efi_set_variable(var_name16, &guid,
867 EFI_VARIABLE_NON_VOLATILE |
868 EFI_VARIABLE_BOOTSERVICE_ACCESS |
869 EFI_VARIABLE_RUNTIME_ACCESS,
871 if (ret != EFI_SUCCESS) {
872 printf("Cannot set %ls\n", var_name16);
877 efi_free_pool(device_path);
878 efi_free_pool(file_path);
885 * do_efi_boot_rm() - delete UEFI load options
887 * @cmdtp: Command table
888 * @flag: Command flag
889 * @argc: Number of arguments
890 * @argv: Argument array
891 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
893 * Implement efidebug "boot rm" sub-command.
894 * Delete UEFI load options.
896 * efidebug boot rm <id> ...
898 static int do_efi_boot_rm(struct cmd_tbl *cmdtp, int flag,
899 int argc, char *const argv[])
905 u16 var_name16[9], *p;
909 return CMD_RET_USAGE;
911 guid = efi_global_variable_guid;
912 for (i = 1; i < argc; i++, argv++) {
913 id = (int)simple_strtoul(argv[1], &endp, 16);
914 if (*endp != '\0' || id > 0xffff)
915 return CMD_RET_FAILURE;
917 sprintf(var_name, "Boot%04X", id);
919 utf8_utf16_strncpy(&p, var_name, 9);
921 ret = EFI_CALL(efi_set_variable(var_name16, &guid, 0, 0, NULL));
923 printf("Cannot remove %ls\n", var_name16);
924 return CMD_RET_FAILURE;
928 return CMD_RET_SUCCESS;
932 * show_efi_boot_opt_data() - dump UEFI load option
934 * @varname16: variable name
935 * @data: value of UEFI load option variable
936 * @size: size of the boot option
938 * Decode the value of UEFI load option variable and print information.
940 static void show_efi_boot_opt_data(u16 *varname16, void *data, size_t *size)
942 struct efi_load_option lo;
944 size_t label_len16, label_len;
948 ret = efi_deserialize_load_option(&lo, data, size);
949 if (ret != EFI_SUCCESS) {
950 printf("%ls: invalid load option\n", varname16);
954 label_len16 = u16_strlen(lo.label);
955 label_len = utf16_utf8_strnlen(lo.label, label_len16);
956 label = malloc(label_len + 1);
960 utf16_utf8_strncpy(&p, lo.label, label_len16);
962 printf("%ls:\nattributes: %c%c%c (0x%08x)\n",
965 lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
966 /* FORCE RECONNECT */
967 lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
969 lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
971 printf(" label: %s\n", label);
973 dp_str = efi_dp_str(lo.file_path);
974 printf(" file_path: %ls\n", dp_str);
975 efi_free_pool(dp_str);
978 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
979 lo.optional_data, *size, true);
984 * show_efi_boot_opt() - dump UEFI load option
986 * @varname16: variable name
988 * Dump information defined by UEFI load option.
990 static void show_efi_boot_opt(u16 *varname16)
997 ret = EFI_CALL(efi_get_variable(varname16, &efi_global_variable_guid,
999 if (ret == EFI_BUFFER_TOO_SMALL) {
1000 data = malloc(size);
1002 printf("ERROR: Out of memory\n");
1005 ret = EFI_CALL(efi_get_variable(varname16,
1006 &efi_global_variable_guid,
1007 NULL, &size, data));
1008 if (ret == EFI_SUCCESS)
1009 show_efi_boot_opt_data(varname16, data, &size);
1014 static int u16_tohex(u16 c)
1016 if (c >= '0' && c <= '9')
1018 if (c >= 'A' && c <= 'F')
1019 return c - 'A' + 10;
1021 /* not hexadecimal */
1026 * show_efi_boot_dump() - dump all UEFI load options
1028 * @cmdtp: Command table
1029 * @flag: Command flag
1030 * @argc: Number of arguments
1031 * @argv: Argument array
1032 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1034 * Implement efidebug "boot dump" sub-command.
1035 * Dump information of all UEFI load options defined.
1037 * efidebug boot dump
1039 static int do_efi_boot_dump(struct cmd_tbl *cmdtp, int flag,
1040 int argc, char *const argv[])
1042 u16 *var_name16, *p;
1043 efi_uintn_t buf_size, size;
1049 return CMD_RET_USAGE;
1052 var_name16 = malloc(buf_size);
1054 return CMD_RET_FAILURE;
1059 ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
1061 if (ret == EFI_NOT_FOUND)
1063 if (ret == EFI_BUFFER_TOO_SMALL) {
1065 p = realloc(var_name16, buf_size);
1068 return CMD_RET_FAILURE;
1071 ret = EFI_CALL(efi_get_next_variable_name(&size,
1075 if (ret != EFI_SUCCESS) {
1077 return CMD_RET_FAILURE;
1080 if (memcmp(var_name16, L"Boot", 8))
1083 for (id = 0, i = 0; i < 4; i++) {
1084 digit = u16_tohex(var_name16[4 + i]);
1087 id = (id << 4) + digit;
1089 if (i == 4 && !var_name16[8])
1090 show_efi_boot_opt(var_name16);
1095 return CMD_RET_SUCCESS;
1099 * show_efi_boot_order() - show order of UEFI load options
1101 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1103 * Show order of UEFI load options defined by BootOrder variable.
1105 static int show_efi_boot_order(void)
1111 u16 var_name16[9], *p16;
1113 struct efi_load_option lo;
1115 size_t label_len16, label_len;
1119 ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid,
1120 NULL, &size, NULL));
1121 if (ret != EFI_BUFFER_TOO_SMALL) {
1122 if (ret == EFI_NOT_FOUND) {
1123 printf("BootOrder not defined\n");
1124 return CMD_RET_SUCCESS;
1126 return CMD_RET_FAILURE;
1129 bootorder = malloc(size);
1131 printf("ERROR: Out of memory\n");
1132 return CMD_RET_FAILURE;
1134 ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid,
1135 NULL, &size, bootorder));
1136 if (ret != EFI_SUCCESS) {
1137 ret = CMD_RET_FAILURE;
1141 num = size / sizeof(u16);
1142 for (i = 0; i < num; i++) {
1143 sprintf(var_name, "Boot%04X", bootorder[i]);
1145 utf8_utf16_strncpy(&p16, var_name, 9);
1148 ret = EFI_CALL(efi_get_variable(var_name16,
1149 &efi_global_variable_guid, NULL,
1151 if (ret != EFI_BUFFER_TOO_SMALL) {
1152 printf("%2d: %s: (not defined)\n", i + 1, var_name);
1156 data = malloc(size);
1158 ret = CMD_RET_FAILURE;
1161 ret = EFI_CALL(efi_get_variable(var_name16,
1162 &efi_global_variable_guid, NULL,
1164 if (ret != EFI_SUCCESS) {
1166 ret = CMD_RET_FAILURE;
1170 ret = efi_deserialize_load_option(&lo, data, &size);
1171 if (ret != EFI_SUCCESS) {
1172 printf("%ls: invalid load option\n", var_name16);
1173 ret = CMD_RET_FAILURE;
1177 label_len16 = u16_strlen(lo.label);
1178 label_len = utf16_utf8_strnlen(lo.label, label_len16);
1179 label = malloc(label_len + 1);
1182 ret = CMD_RET_FAILURE;
1186 utf16_utf8_strncpy(&p, lo.label, label_len16);
1187 printf("%2d: %s: %s\n", i + 1, var_name, label);
1199 * do_efi_boot_next() - manage UEFI BootNext variable
1201 * @cmdtp: Command table
1202 * @flag: Command flag
1203 * @argc: Number of arguments
1204 * @argv: Argument array
1205 * Return: CMD_RET_SUCCESS on success,
1206 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1208 * Implement efidebug "boot next" sub-command.
1209 * Set BootNext variable.
1211 * efidebug boot next <id>
1213 static int do_efi_boot_next(struct cmd_tbl *cmdtp, int flag,
1214 int argc, char *const argv[])
1221 int r = CMD_RET_SUCCESS;
1224 return CMD_RET_USAGE;
1226 bootnext = (u16)simple_strtoul(argv[1], &endp, 16);
1228 printf("invalid value: %s\n", argv[1]);
1229 r = CMD_RET_FAILURE;
1233 guid = efi_global_variable_guid;
1235 ret = EFI_CALL(efi_set_variable(L"BootNext", &guid,
1236 EFI_VARIABLE_NON_VOLATILE |
1237 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1238 EFI_VARIABLE_RUNTIME_ACCESS,
1240 if (ret != EFI_SUCCESS) {
1241 printf("Cannot set BootNext\n");
1242 r = CMD_RET_FAILURE;
1249 * do_efi_boot_order() - manage UEFI BootOrder variable
1251 * @cmdtp: Command table
1252 * @flag: Command flag
1253 * @argc: Number of arguments
1254 * @argv: Argument array
1255 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1257 * Implement efidebug "boot order" sub-command.
1258 * Show order of UEFI load options, or change it in BootOrder variable.
1260 * efidebug boot order [<id> ...]
1262 static int do_efi_boot_order(struct cmd_tbl *cmdtp, int flag,
1263 int argc, char *const argv[])
1265 u16 *bootorder = NULL;
1271 int r = CMD_RET_SUCCESS;
1274 return show_efi_boot_order();
1279 size = argc * sizeof(u16);
1280 bootorder = malloc(size);
1282 return CMD_RET_FAILURE;
1284 for (i = 0; i < argc; i++) {
1285 id = (int)simple_strtoul(argv[i], &endp, 16);
1286 if (*endp != '\0' || id > 0xffff) {
1287 printf("invalid value: %s\n", argv[i]);
1288 r = CMD_RET_FAILURE;
1292 bootorder[i] = (u16)id;
1295 guid = efi_global_variable_guid;
1296 ret = EFI_CALL(efi_set_variable(L"BootOrder", &guid,
1297 EFI_VARIABLE_NON_VOLATILE |
1298 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1299 EFI_VARIABLE_RUNTIME_ACCESS,
1301 if (ret != EFI_SUCCESS) {
1302 printf("Cannot set BootOrder\n");
1303 r = CMD_RET_FAILURE;
1311 static struct cmd_tbl cmd_efidebug_boot_sub[] = {
1312 U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
1313 U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
1314 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
1315 U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
1316 U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
1321 * do_efi_boot_opt() - manage UEFI load options
1323 * @cmdtp: Command table
1324 * @flag: Command flag
1325 * @argc: Number of arguments
1326 * @argv: Argument array
1327 * Return: CMD_RET_SUCCESS on success,
1328 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1330 * Implement efidebug "boot" sub-command.
1332 static int do_efi_boot_opt(struct cmd_tbl *cmdtp, int flag,
1333 int argc, char *const argv[])
1338 return CMD_RET_USAGE;
1342 cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
1343 ARRAY_SIZE(cmd_efidebug_boot_sub));
1345 return CMD_RET_USAGE;
1347 return cp->cmd(cmdtp, flag, argc, argv);
1351 * do_efi_test_bootmgr() - run simple bootmgr for test
1353 * @cmdtp: Command table
1354 * @flag: Command flag
1355 * @argc: Number of arguments
1356 * @argv: Argument array
1357 * Return: CMD_RET_SUCCESS on success,
1358 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1360 * Implement efidebug "test bootmgr" sub-command.
1361 * Run simple bootmgr for test.
1363 * efidebug test bootmgr
1365 static int do_efi_test_bootmgr(struct cmd_tbl *cmdtp, int flag,
1366 int argc, char * const argv[])
1369 efi_uintn_t exit_data_size = 0;
1370 u16 *exit_data = NULL;
1372 void *load_options = NULL;
1374 ret = efi_bootmgr_load(&image, &load_options);
1375 printf("efi_bootmgr_load() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1377 /* We call efi_start_image() even if error for test purpose. */
1378 ret = EFI_CALL(efi_start_image(image, &exit_data_size, &exit_data));
1379 printf("efi_start_image() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1380 if (ret && exit_data)
1381 efi_free_pool(exit_data);
1386 return CMD_RET_SUCCESS;
1389 static struct cmd_tbl cmd_efidebug_test_sub[] = {
1390 U_BOOT_CMD_MKENT(bootmgr, CONFIG_SYS_MAXARGS, 1, do_efi_test_bootmgr,
1395 * do_efi_test() - manage UEFI load options
1397 * @cmdtp: Command table
1398 * @flag: Command flag
1399 * @argc: Number of arguments
1400 * @argv: Argument array
1401 * Return: CMD_RET_SUCCESS on success,
1402 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1404 * Implement efidebug "test" sub-command.
1406 static int do_efi_test(struct cmd_tbl *cmdtp, int flag,
1407 int argc, char * const argv[])
1412 return CMD_RET_USAGE;
1416 cp = find_cmd_tbl(argv[0], cmd_efidebug_test_sub,
1417 ARRAY_SIZE(cmd_efidebug_test_sub));
1419 return CMD_RET_USAGE;
1421 return cp->cmd(cmdtp, flag, argc, argv);
1425 * do_efi_query_info() - QueryVariableInfo EFI service
1427 * @cmdtp: Command table
1428 * @flag: Command flag
1429 * @argc: Number of arguments
1430 * @argv: Argument array
1431 * Return: CMD_RET_SUCCESS on success,
1432 * CMD_RET_USAGE or CMD_RET_FAILURE on failure
1434 * Implement efidebug "test" sub-command.
1437 static int do_efi_query_info(struct cmd_tbl *cmdtp, int flag,
1438 int argc, char * const argv[])
1442 u64 max_variable_storage_size;
1443 u64 remain_variable_storage_size;
1444 u64 max_variable_size;
1447 for (i = 1; i < argc; i++) {
1448 if (!strcmp(argv[i], "-bs"))
1449 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
1450 else if (!strcmp(argv[i], "-rt"))
1451 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
1452 else if (!strcmp(argv[i], "-nv"))
1453 attr |= EFI_VARIABLE_NON_VOLATILE;
1454 else if (!strcmp(argv[i], "-at"))
1456 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
1459 ret = EFI_CALL(efi_query_variable_info(attr,
1460 &max_variable_storage_size,
1461 &remain_variable_storage_size,
1462 &max_variable_size));
1463 if (ret != EFI_SUCCESS) {
1464 printf("Error: Cannot query UEFI variables, r = %lu\n",
1465 ret & ~EFI_ERROR_MASK);
1466 return CMD_RET_FAILURE;
1469 printf("Max storage size %llu\n", max_variable_storage_size);
1470 printf("Remaining storage size %llu\n", remain_variable_storage_size);
1471 printf("Max variable size %llu\n", max_variable_size);
1473 return CMD_RET_SUCCESS;
1476 static struct cmd_tbl cmd_efidebug_sub[] = {
1477 U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
1478 #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
1479 U_BOOT_CMD_MKENT(capsule, CONFIG_SYS_MAXARGS, 1, do_efi_capsule,
1482 U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices,
1484 U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
1486 U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
1488 U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
1490 U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
1492 U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables,
1494 U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_efi_test,
1496 U_BOOT_CMD_MKENT(query, CONFIG_SYS_MAXARGS, 1, do_efi_query_info,
1501 * do_efidebug() - display and configure UEFI environment
1503 * @cmdtp: Command table
1504 * @flag: Command flag
1505 * @argc: Number of arguments
1506 * @argv: Argument array
1507 * Return: CMD_RET_SUCCESS on success,
1508 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1510 * Implement efidebug command which allows us to display and
1511 * configure UEFI environment.
1513 static int do_efidebug(struct cmd_tbl *cmdtp, int flag,
1514 int argc, char *const argv[])
1520 return CMD_RET_USAGE;
1524 /* Initialize UEFI drivers */
1525 r = efi_init_obj_list();
1526 if (r != EFI_SUCCESS) {
1527 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1528 r & ~EFI_ERROR_MASK);
1529 return CMD_RET_FAILURE;
1532 cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
1533 ARRAY_SIZE(cmd_efidebug_sub));
1535 return CMD_RET_USAGE;
1537 return cp->cmd(cmdtp, flag, argc, argv);
1540 #ifdef CONFIG_SYS_LONGHELP
1541 static char efidebug_help_text[] =
1542 " - UEFI Shell-like interface to configure UEFI environment\n"
1544 "efidebug boot add <bootid> <label> <interface> <devnum>[:<part>] <file path> [<load options>]\n"
1545 " - set UEFI BootXXXX variable\n"
1546 " <load options> will be passed to UEFI application\n"
1547 "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
1548 " - delete UEFI BootXXXX variables\n"
1549 "efidebug boot dump\n"
1550 " - dump all UEFI BootXXXX variables\n"
1551 "efidebug boot next <bootid>\n"
1552 " - set UEFI BootNext variable\n"
1553 "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
1554 " - set/show UEFI boot order\n"
1556 #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
1557 "efidebug capsule update [-v] <capsule address>\n"
1558 " - process a capsule\n"
1559 "efidebug capsule disk-update\n"
1560 " - update a capsule from disk\n"
1561 "efidebug capsule show <capsule address>\n"
1562 " - show capsule information\n"
1563 "efidebug capsule result [<capsule result var>]\n"
1564 " - show a capsule update result\n"
1567 "efidebug devices\n"
1568 " - show UEFI devices\n"
1569 "efidebug drivers\n"
1570 " - show UEFI drivers\n"
1572 " - show UEFI handles\n"
1574 " - show loaded images\n"
1576 " - show UEFI memory map\n"
1578 " - show UEFI configuration tables\n"
1579 "efidebug test bootmgr\n"
1580 " - run simple bootmgr for test\n"
1581 "efidebug query [-nv][-bs][-rt][-at]\n"
1582 " - show size of UEFI variables store\n";
1586 efidebug, 10, 0, do_efidebug,
1587 "Configure UEFI environment",