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);
193 return CMD_RET_FAILURE;
194 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
197 if (ret != EFI_SUCCESS) {
199 printf("Failed to get %ls\n", var_name16);
201 return CMD_RET_FAILURE;
204 printf("Result total size: 0x%x\n", result->variable_total_size);
205 printf("Capsule guid: %pUl\n", &result->capsule_guid);
206 printf("Time processed: %04d-%02d-%02d %02d:%02d:%02d\n",
207 result->capsule_processed.year, result->capsule_processed.month,
208 result->capsule_processed.day, result->capsule_processed.hour,
209 result->capsule_processed.minute,
210 result->capsule_processed.second);
211 printf("Capsule status: 0x%lx\n", result->capsule_status);
215 return CMD_RET_SUCCESS;
218 static struct cmd_tbl cmd_efidebug_capsule_sub[] = {
219 U_BOOT_CMD_MKENT(update, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_update,
221 U_BOOT_CMD_MKENT(show, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_show,
223 U_BOOT_CMD_MKENT(disk-update, 0, 0, do_efi_capsule_on_disk_update,
225 U_BOOT_CMD_MKENT(result, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_res,
230 * do_efi_capsule() - manage UEFI capsules
232 * @cmdtp: Command table
233 * @flag: Command flag
234 * @argc: Number of arguments
235 * @argv: Argument array
236 * Return: CMD_RET_SUCCESS on success,
237 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
239 * Implement efidebug "capsule" sub-command.
241 static int do_efi_capsule(struct cmd_tbl *cmdtp, int flag,
242 int argc, char * const argv[])
247 return CMD_RET_USAGE;
251 cp = find_cmd_tbl(argv[0], cmd_efidebug_capsule_sub,
252 ARRAY_SIZE(cmd_efidebug_capsule_sub));
254 return CMD_RET_USAGE;
256 return cp->cmd(cmdtp, flag, argc, argv);
258 #endif /* CONFIG_EFI_HAVE_CAPSULE_SUPPORT */
261 * efi_get_device_handle_info() - get information of UEFI device
263 * @handle: Handle of UEFI device
264 * @dev_path_text: Pointer to text of device path
265 * Return: 0 on success, -1 on failure
267 * Currently return a formatted text of device path.
269 static int efi_get_device_handle_info(efi_handle_t handle, u16 **dev_path_text)
271 struct efi_device_path *dp;
274 ret = EFI_CALL(BS->open_protocol(handle, &efi_guid_device_path,
275 (void **)&dp, NULL /* FIXME */, NULL,
276 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
277 if (ret == EFI_SUCCESS) {
278 *dev_path_text = efi_dp_str(dp);
285 #define EFI_HANDLE_WIDTH ((int)sizeof(efi_handle_t) * 2)
287 static const char spc[] = " ";
288 static const char sep[] = "================";
291 * do_efi_show_devices() - show UEFI devices
293 * @cmdtp: Command table
294 * @flag: Command flag
295 * @argc: Number of arguments
296 * @argv: Argument array
297 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
299 * Implement efidebug "devices" sub-command.
300 * Show all UEFI devices and their information.
302 static int do_efi_show_devices(struct cmd_tbl *cmdtp, int flag,
303 int argc, char *const argv[])
305 efi_handle_t *handles;
310 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
312 if (ret != EFI_SUCCESS)
313 return CMD_RET_FAILURE;
316 return CMD_RET_SUCCESS;
318 printf("Device%.*s Device Path\n", EFI_HANDLE_WIDTH - 6, spc);
319 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
320 for (i = 0; i < num; i++) {
321 if (!efi_get_device_handle_info(handles[i], &dev_path_text)) {
322 printf("%p %ls\n", handles[i], dev_path_text);
323 efi_free_pool(dev_path_text);
327 efi_free_pool(handles);
329 return CMD_RET_SUCCESS;
333 * efi_get_driver_handle_info() - get information of UEFI driver
335 * @handle: Handle of UEFI device
336 * @driver_name: Driver name
337 * @image_path: Pointer to text of device path
338 * Return: 0 on success, -1 on failure
340 * Currently return no useful information as all UEFI drivers are
343 static int efi_get_driver_handle_info(efi_handle_t handle, u16 **driver_name,
346 struct efi_handler *handler;
347 struct efi_loaded_image *image;
352 * TODO: support EFI_COMPONENT_NAME2_PROTOCOL
357 ret = efi_search_protocol(handle, &efi_guid_loaded_image, &handler);
358 if (ret != EFI_SUCCESS) {
363 image = handler->protocol_interface;
364 *image_path = efi_dp_str(image->file_path);
370 * do_efi_show_drivers() - show UEFI drivers
372 * @cmdtp: Command table
373 * @flag: Command flag
374 * @argc: Number of arguments
375 * @argv: Argument array
376 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
378 * Implement efidebug "drivers" sub-command.
379 * Show all UEFI drivers and their information.
381 static int do_efi_show_drivers(struct cmd_tbl *cmdtp, int flag,
382 int argc, char *const argv[])
384 efi_handle_t *handles;
386 u16 *driver_name, *image_path_text;
389 ret = EFI_CALL(efi_locate_handle_buffer(
390 BY_PROTOCOL, &efi_guid_driver_binding_protocol,
391 NULL, &num, &handles));
392 if (ret != EFI_SUCCESS)
393 return CMD_RET_FAILURE;
396 return CMD_RET_SUCCESS;
398 printf("Driver%.*s Name Image Path\n",
399 EFI_HANDLE_WIDTH - 6, spc);
400 printf("%.*s ==================== ====================\n",
401 EFI_HANDLE_WIDTH, sep);
402 for (i = 0; i < num; i++) {
403 if (!efi_get_driver_handle_info(handles[i], &driver_name,
406 printf("%p %-20ls %ls\n", handles[i],
407 driver_name, image_path_text);
409 printf("%p %-20ls <built-in>\n",
410 handles[i], driver_name);
411 efi_free_pool(driver_name);
412 efi_free_pool(image_path_text);
416 efi_free_pool(handles);
418 return CMD_RET_SUCCESS;
421 static const struct {
423 const efi_guid_t guid;
427 EFI_DEVICE_PATH_PROTOCOL_GUID,
430 "Device Path To Text",
431 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID,
434 "Device Path Utilities",
435 EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID,
438 "Unicode Collation 2",
439 EFI_UNICODE_COLLATION_PROTOCOL2_GUID,
443 EFI_DRIVER_BINDING_PROTOCOL_GUID,
447 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID,
450 "Simple Text Input Ex",
451 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID,
454 "Simple Text Output",
455 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID,
459 EFI_BLOCK_IO_PROTOCOL_GUID,
462 "Simple File System",
463 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
467 EFI_LOADED_IMAGE_PROTOCOL_GUID,
471 EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID,
475 EFI_HII_STRING_PROTOCOL_GUID,
479 EFI_HII_DATABASE_PROTOCOL_GUID,
482 "HII Config Routing",
483 EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID,
487 EFI_LOAD_FILE2_PROTOCOL_GUID,
490 "Random Number Generator",
491 EFI_RNG_PROTOCOL_GUID,
495 EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
499 EFI_PXE_BASE_CODE_PROTOCOL_GUID,
503 EFI_DT_FIXUP_PROTOCOL_GUID,
505 /* Configuration table GUIDs */
519 "Runtime properties",
520 EFI_RT_PROPERTIES_TABLE_GUID,
523 "TCG2 Final Events Table",
524 EFI_TCG2_FINAL_EVENTS_TABLE_GUID,
529 * get_guid_text - get string of GUID
531 * Return description of GUID.
534 * Return: description of GUID or NULL
536 static const char *get_guid_text(const void *guid)
540 for (i = 0; i < ARRAY_SIZE(guid_list); i++) {
542 * As guidcmp uses memcmp() we can safely accept unaligned
545 if (!guidcmp(&guid_list[i].guid, guid))
546 return guid_list[i].text;
553 * do_efi_show_handles() - show UEFI handles
555 * @cmdtp: Command table
556 * @flag: Command flag
557 * @argc: Number of arguments
558 * @argv: Argument array
559 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
561 * Implement efidebug "dh" sub-command.
562 * Show all UEFI handles and their information, currently all protocols
565 static int do_efi_show_handles(struct cmd_tbl *cmdtp, int flag,
566 int argc, char *const argv[])
568 efi_handle_t *handles;
570 efi_uintn_t num, count, i, j;
571 const char *guid_text;
574 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
576 if (ret != EFI_SUCCESS)
577 return CMD_RET_FAILURE;
580 return CMD_RET_SUCCESS;
582 printf("Handle%.*s Protocols\n", EFI_HANDLE_WIDTH - 6, spc);
583 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
584 for (i = 0; i < num; i++) {
585 printf("%p", handles[i]);
586 ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid,
593 for (j = 0; j < count; j++) {
599 guid_text = get_guid_text(guid[j]);
603 printf("%pUl", guid[j]);
608 efi_free_pool(handles);
610 return CMD_RET_SUCCESS;
614 * do_efi_show_images() - show UEFI images
616 * @cmdtp: Command table
617 * @flag: Command flag
618 * @argc: Number of arguments
619 * @argv: Argument array
620 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
622 * Implement efidebug "images" sub-command.
623 * Show all UEFI loaded images and their information.
625 static int do_efi_show_images(struct cmd_tbl *cmdtp, int flag,
626 int argc, char *const argv[])
628 efi_print_image_infos(NULL);
630 return CMD_RET_SUCCESS;
633 static const char * const efi_mem_type_string[] = {
634 [EFI_RESERVED_MEMORY_TYPE] = "RESERVED",
635 [EFI_LOADER_CODE] = "LOADER CODE",
636 [EFI_LOADER_DATA] = "LOADER DATA",
637 [EFI_BOOT_SERVICES_CODE] = "BOOT CODE",
638 [EFI_BOOT_SERVICES_DATA] = "BOOT DATA",
639 [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE",
640 [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA",
641 [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL",
642 [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM",
643 [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM",
644 [EFI_ACPI_MEMORY_NVS] = "ACPI NVS",
645 [EFI_MMAP_IO] = "IO",
646 [EFI_MMAP_IO_PORT] = "IO PORT",
647 [EFI_PAL_CODE] = "PAL",
648 [EFI_PERSISTENT_MEMORY_TYPE] = "PERSISTENT",
651 static const struct efi_mem_attrs {
654 } efi_mem_attrs[] = {
655 {EFI_MEMORY_UC, "UC"},
656 {EFI_MEMORY_UC, "UC"},
657 {EFI_MEMORY_WC, "WC"},
658 {EFI_MEMORY_WT, "WT"},
659 {EFI_MEMORY_WB, "WB"},
660 {EFI_MEMORY_UCE, "UCE"},
661 {EFI_MEMORY_WP, "WP"},
662 {EFI_MEMORY_RP, "RP"},
663 {EFI_MEMORY_XP, "WP"},
664 {EFI_MEMORY_NV, "NV"},
665 {EFI_MEMORY_MORE_RELIABLE, "REL"},
666 {EFI_MEMORY_RO, "RO"},
667 {EFI_MEMORY_SP, "SP"},
668 {EFI_MEMORY_RUNTIME, "RT"},
672 * print_memory_attributes() - print memory map attributes
674 * @attributes: Attribute value
676 * Print memory map attributes
678 static void print_memory_attributes(u64 attributes)
682 for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
683 if (attributes & efi_mem_attrs[i].bit) {
690 puts(efi_mem_attrs[i].text);
694 #define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
697 * do_efi_show_memmap() - show UEFI memory map
699 * @cmdtp: Command table
700 * @flag: Command flag
701 * @argc: Number of arguments
702 * @argv: Argument array
703 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
705 * Implement efidebug "memmap" sub-command.
706 * Show UEFI memory map.
708 static int do_efi_show_memmap(struct cmd_tbl *cmdtp, int flag,
709 int argc, char *const argv[])
711 struct efi_mem_desc *memmap = NULL, *map;
712 efi_uintn_t map_size = 0;
717 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
718 if (ret == EFI_BUFFER_TOO_SMALL) {
719 map_size += sizeof(struct efi_mem_desc); /* for my own */
720 ret = efi_allocate_pool(EFI_LOADER_DATA, map_size,
722 if (ret != EFI_SUCCESS)
723 return CMD_RET_FAILURE;
724 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
726 if (ret != EFI_SUCCESS) {
727 efi_free_pool(memmap);
728 return CMD_RET_FAILURE;
731 printf("Type Start%.*s End%.*s Attributes\n",
732 EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc);
733 printf("================ %.*s %.*s ==========\n",
734 EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep);
736 * Coverity check: dereferencing null pointer "map."
737 * This is a false positive as memmap will always be
738 * populated by allocate_pool() above.
740 for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
741 if (map->type < ARRAY_SIZE(efi_mem_type_string))
742 type = efi_mem_type_string[map->type];
746 printf("%-16s %.*llx-%.*llx", type,
748 (u64)map_to_sysmem((void *)(uintptr_t)
749 map->physical_start),
751 (u64)map_to_sysmem((void *)(uintptr_t)
752 (map->physical_start +
753 map->num_pages * EFI_PAGE_SIZE)));
755 print_memory_attributes(map->attribute);
759 efi_free_pool(memmap);
761 return CMD_RET_SUCCESS;
765 * do_efi_show_tables() - show UEFI configuration tables
767 * @cmdtp: Command table
768 * @flag: Command flag
769 * @argc: Number of arguments
770 * @argv: Argument array
771 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
773 * Implement efidebug "tables" sub-command.
774 * Show UEFI configuration tables.
776 static int do_efi_show_tables(struct cmd_tbl *cmdtp, int flag,
777 int argc, char *const argv[])
780 const char *guid_str;
782 for (i = 0; i < systab.nr_tables; ++i) {
783 guid_str = get_guid_text(&systab.tables[i].guid);
786 printf("%pUl %s\n", &systab.tables[i].guid, guid_str);
789 return CMD_RET_SUCCESS;
793 * do_efi_boot_add() - set UEFI load option
795 * @cmdtp: Command table
796 * @flag: Command flag
797 * @argc: Number of arguments
798 * @argv: Argument array
799 * Return: CMD_RET_SUCCESS on success,
800 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
802 * Implement efidebug "boot add" sub-command. Create or change UEFI load option.
804 * efidebug boot add <id> <label> <interface> <devnum>[:<part>] <file> <options>
806 static int do_efi_boot_add(struct cmd_tbl *cmdtp, int flag,
807 int argc, char *const argv[])
812 u16 var_name16[9], *p;
814 size_t label_len, label_len16;
816 struct efi_device_path *device_path = NULL, *file_path = NULL;
817 struct efi_load_option lo;
821 int r = CMD_RET_SUCCESS;
823 if (argc < 6 || argc > 7)
824 return CMD_RET_USAGE;
826 id = (int)simple_strtoul(argv[1], &endp, 16);
827 if (*endp != '\0' || id > 0xffff)
828 return CMD_RET_USAGE;
830 sprintf(var_name, "Boot%04X", id);
832 utf8_utf16_strncpy(&p, var_name, 9);
834 guid = efi_global_variable_guid;
837 lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
840 label_len = strlen(argv[2]);
841 label_len16 = utf8_utf16_strnlen(argv[2], label_len);
842 label = malloc((label_len16 + 1) * sizeof(u16));
844 return CMD_RET_FAILURE;
845 lo.label = label; /* label will be changed below */
846 utf8_utf16_strncpy(&label, argv[2], label_len);
849 ret = efi_dp_from_name(argv[3], argv[4], argv[5], &device_path,
851 if (ret != EFI_SUCCESS) {
852 printf("Cannot create device path for \"%s %s\"\n",
857 lo.file_path = file_path;
858 lo.file_path_length = efi_dp_size(file_path)
859 + sizeof(struct efi_device_path); /* for END */
863 lo.optional_data = NULL;
865 lo.optional_data = (const u8 *)argv[6];
867 size = efi_serialize_load_option(&lo, (u8 **)&data);
873 ret = EFI_CALL(efi_set_variable(var_name16, &guid,
874 EFI_VARIABLE_NON_VOLATILE |
875 EFI_VARIABLE_BOOTSERVICE_ACCESS |
876 EFI_VARIABLE_RUNTIME_ACCESS,
878 if (ret != EFI_SUCCESS) {
879 printf("Cannot set %ls\n", var_name16);
884 efi_free_pool(device_path);
885 efi_free_pool(file_path);
892 * do_efi_boot_rm() - delete UEFI load options
894 * @cmdtp: Command table
895 * @flag: Command flag
896 * @argc: Number of arguments
897 * @argv: Argument array
898 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
900 * Implement efidebug "boot rm" sub-command.
901 * Delete UEFI load options.
903 * efidebug boot rm <id> ...
905 static int do_efi_boot_rm(struct cmd_tbl *cmdtp, int flag,
906 int argc, char *const argv[])
912 u16 var_name16[9], *p;
916 return CMD_RET_USAGE;
918 guid = efi_global_variable_guid;
919 for (i = 1; i < argc; i++, argv++) {
920 id = (int)simple_strtoul(argv[1], &endp, 16);
921 if (*endp != '\0' || id > 0xffff)
922 return CMD_RET_FAILURE;
924 sprintf(var_name, "Boot%04X", id);
926 utf8_utf16_strncpy(&p, var_name, 9);
928 ret = EFI_CALL(efi_set_variable(var_name16, &guid, 0, 0, NULL));
930 printf("Cannot remove %ls\n", var_name16);
931 return CMD_RET_FAILURE;
935 return CMD_RET_SUCCESS;
939 * show_efi_boot_opt_data() - dump UEFI load option
941 * @varname16: variable name
942 * @data: value of UEFI load option variable
943 * @size: size of the boot option
945 * Decode the value of UEFI load option variable and print information.
947 static void show_efi_boot_opt_data(u16 *varname16, void *data, size_t *size)
949 struct efi_load_option lo;
951 size_t label_len16, label_len;
955 ret = efi_deserialize_load_option(&lo, data, size);
956 if (ret != EFI_SUCCESS) {
957 printf("%ls: invalid load option\n", varname16);
961 label_len16 = u16_strlen(lo.label);
962 label_len = utf16_utf8_strnlen(lo.label, label_len16);
963 label = malloc(label_len + 1);
967 utf16_utf8_strncpy(&p, lo.label, label_len16);
969 printf("%ls:\nattributes: %c%c%c (0x%08x)\n",
972 lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
973 /* FORCE RECONNECT */
974 lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
976 lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
978 printf(" label: %s\n", label);
980 dp_str = efi_dp_str(lo.file_path);
981 printf(" file_path: %ls\n", dp_str);
982 efi_free_pool(dp_str);
985 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
986 lo.optional_data, *size, true);
991 * show_efi_boot_opt() - dump UEFI load option
993 * @varname16: variable name
995 * Dump information defined by UEFI load option.
997 static void show_efi_boot_opt(u16 *varname16)
1004 ret = EFI_CALL(efi_get_variable(varname16, &efi_global_variable_guid,
1005 NULL, &size, NULL));
1006 if (ret == EFI_BUFFER_TOO_SMALL) {
1007 data = malloc(size);
1009 printf("ERROR: Out of memory\n");
1012 ret = EFI_CALL(efi_get_variable(varname16,
1013 &efi_global_variable_guid,
1014 NULL, &size, data));
1015 if (ret == EFI_SUCCESS)
1016 show_efi_boot_opt_data(varname16, data, &size);
1021 static int u16_tohex(u16 c)
1023 if (c >= '0' && c <= '9')
1025 if (c >= 'A' && c <= 'F')
1026 return c - 'A' + 10;
1028 /* not hexadecimal */
1033 * show_efi_boot_dump() - dump all UEFI load options
1035 * @cmdtp: Command table
1036 * @flag: Command flag
1037 * @argc: Number of arguments
1038 * @argv: Argument array
1039 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1041 * Implement efidebug "boot dump" sub-command.
1042 * Dump information of all UEFI load options defined.
1044 * efidebug boot dump
1046 static int do_efi_boot_dump(struct cmd_tbl *cmdtp, int flag,
1047 int argc, char *const argv[])
1049 u16 *var_name16, *p;
1050 efi_uintn_t buf_size, size;
1056 return CMD_RET_USAGE;
1059 var_name16 = malloc(buf_size);
1061 return CMD_RET_FAILURE;
1066 ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
1068 if (ret == EFI_NOT_FOUND)
1070 if (ret == EFI_BUFFER_TOO_SMALL) {
1072 p = realloc(var_name16, buf_size);
1075 return CMD_RET_FAILURE;
1078 ret = EFI_CALL(efi_get_next_variable_name(&size,
1082 if (ret != EFI_SUCCESS) {
1084 return CMD_RET_FAILURE;
1087 if (memcmp(var_name16, L"Boot", 8))
1090 for (id = 0, i = 0; i < 4; i++) {
1091 digit = u16_tohex(var_name16[4 + i]);
1094 id = (id << 4) + digit;
1096 if (i == 4 && !var_name16[8])
1097 show_efi_boot_opt(var_name16);
1102 return CMD_RET_SUCCESS;
1106 * show_efi_boot_order() - show order of UEFI load options
1108 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1110 * Show order of UEFI load options defined by BootOrder variable.
1112 static int show_efi_boot_order(void)
1118 u16 var_name16[9], *p16;
1120 struct efi_load_option lo;
1122 size_t label_len16, label_len;
1126 ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid,
1127 NULL, &size, NULL));
1128 if (ret != EFI_BUFFER_TOO_SMALL) {
1129 if (ret == EFI_NOT_FOUND) {
1130 printf("BootOrder not defined\n");
1131 return CMD_RET_SUCCESS;
1133 return CMD_RET_FAILURE;
1136 bootorder = malloc(size);
1138 printf("ERROR: Out of memory\n");
1139 return CMD_RET_FAILURE;
1141 ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid,
1142 NULL, &size, bootorder));
1143 if (ret != EFI_SUCCESS) {
1144 ret = CMD_RET_FAILURE;
1148 num = size / sizeof(u16);
1149 for (i = 0; i < num; i++) {
1150 sprintf(var_name, "Boot%04X", bootorder[i]);
1152 utf8_utf16_strncpy(&p16, var_name, 9);
1155 ret = EFI_CALL(efi_get_variable(var_name16,
1156 &efi_global_variable_guid, NULL,
1158 if (ret != EFI_BUFFER_TOO_SMALL) {
1159 printf("%2d: %s: (not defined)\n", i + 1, var_name);
1163 data = malloc(size);
1165 ret = CMD_RET_FAILURE;
1168 ret = EFI_CALL(efi_get_variable(var_name16,
1169 &efi_global_variable_guid, NULL,
1171 if (ret != EFI_SUCCESS) {
1173 ret = CMD_RET_FAILURE;
1177 ret = efi_deserialize_load_option(&lo, data, &size);
1178 if (ret != EFI_SUCCESS) {
1179 printf("%ls: invalid load option\n", var_name16);
1180 ret = CMD_RET_FAILURE;
1184 label_len16 = u16_strlen(lo.label);
1185 label_len = utf16_utf8_strnlen(lo.label, label_len16);
1186 label = malloc(label_len + 1);
1189 ret = CMD_RET_FAILURE;
1193 utf16_utf8_strncpy(&p, lo.label, label_len16);
1194 printf("%2d: %s: %s\n", i + 1, var_name, label);
1206 * do_efi_boot_next() - manage UEFI BootNext variable
1208 * @cmdtp: Command table
1209 * @flag: Command flag
1210 * @argc: Number of arguments
1211 * @argv: Argument array
1212 * Return: CMD_RET_SUCCESS on success,
1213 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1215 * Implement efidebug "boot next" sub-command.
1216 * Set BootNext variable.
1218 * efidebug boot next <id>
1220 static int do_efi_boot_next(struct cmd_tbl *cmdtp, int flag,
1221 int argc, char *const argv[])
1228 int r = CMD_RET_SUCCESS;
1231 return CMD_RET_USAGE;
1233 bootnext = (u16)simple_strtoul(argv[1], &endp, 16);
1235 printf("invalid value: %s\n", argv[1]);
1236 r = CMD_RET_FAILURE;
1240 guid = efi_global_variable_guid;
1242 ret = EFI_CALL(efi_set_variable(L"BootNext", &guid,
1243 EFI_VARIABLE_NON_VOLATILE |
1244 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1245 EFI_VARIABLE_RUNTIME_ACCESS,
1247 if (ret != EFI_SUCCESS) {
1248 printf("Cannot set BootNext\n");
1249 r = CMD_RET_FAILURE;
1256 * do_efi_boot_order() - manage UEFI BootOrder variable
1258 * @cmdtp: Command table
1259 * @flag: Command flag
1260 * @argc: Number of arguments
1261 * @argv: Argument array
1262 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1264 * Implement efidebug "boot order" sub-command.
1265 * Show order of UEFI load options, or change it in BootOrder variable.
1267 * efidebug boot order [<id> ...]
1269 static int do_efi_boot_order(struct cmd_tbl *cmdtp, int flag,
1270 int argc, char *const argv[])
1272 u16 *bootorder = NULL;
1278 int r = CMD_RET_SUCCESS;
1281 return show_efi_boot_order();
1286 size = argc * sizeof(u16);
1287 bootorder = malloc(size);
1289 return CMD_RET_FAILURE;
1291 for (i = 0; i < argc; i++) {
1292 id = (int)simple_strtoul(argv[i], &endp, 16);
1293 if (*endp != '\0' || id > 0xffff) {
1294 printf("invalid value: %s\n", argv[i]);
1295 r = CMD_RET_FAILURE;
1299 bootorder[i] = (u16)id;
1302 guid = efi_global_variable_guid;
1303 ret = EFI_CALL(efi_set_variable(L"BootOrder", &guid,
1304 EFI_VARIABLE_NON_VOLATILE |
1305 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1306 EFI_VARIABLE_RUNTIME_ACCESS,
1308 if (ret != EFI_SUCCESS) {
1309 printf("Cannot set BootOrder\n");
1310 r = CMD_RET_FAILURE;
1318 static struct cmd_tbl cmd_efidebug_boot_sub[] = {
1319 U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
1320 U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
1321 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
1322 U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
1323 U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
1328 * do_efi_boot_opt() - manage UEFI load options
1330 * @cmdtp: Command table
1331 * @flag: Command flag
1332 * @argc: Number of arguments
1333 * @argv: Argument array
1334 * Return: CMD_RET_SUCCESS on success,
1335 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1337 * Implement efidebug "boot" sub-command.
1339 static int do_efi_boot_opt(struct cmd_tbl *cmdtp, int flag,
1340 int argc, char *const argv[])
1345 return CMD_RET_USAGE;
1349 cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
1350 ARRAY_SIZE(cmd_efidebug_boot_sub));
1352 return CMD_RET_USAGE;
1354 return cp->cmd(cmdtp, flag, argc, argv);
1358 * do_efi_test_bootmgr() - run simple bootmgr for test
1360 * @cmdtp: Command table
1361 * @flag: Command flag
1362 * @argc: Number of arguments
1363 * @argv: Argument array
1364 * Return: CMD_RET_SUCCESS on success,
1365 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1367 * Implement efidebug "test bootmgr" sub-command.
1368 * Run simple bootmgr for test.
1370 * efidebug test bootmgr
1372 static __maybe_unused int do_efi_test_bootmgr(struct cmd_tbl *cmdtp, int flag,
1373 int argc, char * const argv[])
1376 efi_uintn_t exit_data_size = 0;
1377 u16 *exit_data = NULL;
1379 void *load_options = NULL;
1381 ret = efi_bootmgr_load(&image, &load_options);
1382 printf("efi_bootmgr_load() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1384 /* We call efi_start_image() even if error for test purpose. */
1385 ret = EFI_CALL(efi_start_image(image, &exit_data_size, &exit_data));
1386 printf("efi_start_image() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1387 if (ret && exit_data)
1388 efi_free_pool(exit_data);
1393 return CMD_RET_SUCCESS;
1396 static struct cmd_tbl cmd_efidebug_test_sub[] = {
1397 #ifdef CONFIG_CMD_BOOTEFI_BOOTMGR
1398 U_BOOT_CMD_MKENT(bootmgr, CONFIG_SYS_MAXARGS, 1, do_efi_test_bootmgr,
1404 * do_efi_test() - manage UEFI load options
1406 * @cmdtp: Command table
1407 * @flag: Command flag
1408 * @argc: Number of arguments
1409 * @argv: Argument array
1410 * Return: CMD_RET_SUCCESS on success,
1411 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1413 * Implement efidebug "test" sub-command.
1415 static int do_efi_test(struct cmd_tbl *cmdtp, int flag,
1416 int argc, char * const argv[])
1421 return CMD_RET_USAGE;
1425 cp = find_cmd_tbl(argv[0], cmd_efidebug_test_sub,
1426 ARRAY_SIZE(cmd_efidebug_test_sub));
1428 return CMD_RET_USAGE;
1430 return cp->cmd(cmdtp, flag, argc, argv);
1434 * do_efi_query_info() - QueryVariableInfo EFI service
1436 * @cmdtp: Command table
1437 * @flag: Command flag
1438 * @argc: Number of arguments
1439 * @argv: Argument array
1440 * Return: CMD_RET_SUCCESS on success,
1441 * CMD_RET_USAGE or CMD_RET_FAILURE on failure
1443 * Implement efidebug "test" sub-command.
1446 static int do_efi_query_info(struct cmd_tbl *cmdtp, int flag,
1447 int argc, char * const argv[])
1451 u64 max_variable_storage_size;
1452 u64 remain_variable_storage_size;
1453 u64 max_variable_size;
1456 for (i = 1; i < argc; i++) {
1457 if (!strcmp(argv[i], "-bs"))
1458 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
1459 else if (!strcmp(argv[i], "-rt"))
1460 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
1461 else if (!strcmp(argv[i], "-nv"))
1462 attr |= EFI_VARIABLE_NON_VOLATILE;
1463 else if (!strcmp(argv[i], "-at"))
1465 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
1468 ret = EFI_CALL(efi_query_variable_info(attr,
1469 &max_variable_storage_size,
1470 &remain_variable_storage_size,
1471 &max_variable_size));
1472 if (ret != EFI_SUCCESS) {
1473 printf("Error: Cannot query UEFI variables, r = %lu\n",
1474 ret & ~EFI_ERROR_MASK);
1475 return CMD_RET_FAILURE;
1478 printf("Max storage size %llu\n", max_variable_storage_size);
1479 printf("Remaining storage size %llu\n", remain_variable_storage_size);
1480 printf("Max variable size %llu\n", max_variable_size);
1482 return CMD_RET_SUCCESS;
1485 static struct cmd_tbl cmd_efidebug_sub[] = {
1486 U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
1487 #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
1488 U_BOOT_CMD_MKENT(capsule, CONFIG_SYS_MAXARGS, 1, do_efi_capsule,
1491 U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices,
1493 U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
1495 U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
1497 U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
1499 U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
1501 U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables,
1503 U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_efi_test,
1505 U_BOOT_CMD_MKENT(query, CONFIG_SYS_MAXARGS, 1, do_efi_query_info,
1510 * do_efidebug() - display and configure UEFI environment
1512 * @cmdtp: Command table
1513 * @flag: Command flag
1514 * @argc: Number of arguments
1515 * @argv: Argument array
1516 * Return: CMD_RET_SUCCESS on success,
1517 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1519 * Implement efidebug command which allows us to display and
1520 * configure UEFI environment.
1522 static int do_efidebug(struct cmd_tbl *cmdtp, int flag,
1523 int argc, char *const argv[])
1529 return CMD_RET_USAGE;
1533 /* Initialize UEFI drivers */
1534 r = efi_init_obj_list();
1535 if (r != EFI_SUCCESS) {
1536 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1537 r & ~EFI_ERROR_MASK);
1538 return CMD_RET_FAILURE;
1541 cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
1542 ARRAY_SIZE(cmd_efidebug_sub));
1544 return CMD_RET_USAGE;
1546 return cp->cmd(cmdtp, flag, argc, argv);
1549 #ifdef CONFIG_SYS_LONGHELP
1550 static char efidebug_help_text[] =
1551 " - UEFI Shell-like interface to configure UEFI environment\n"
1553 "efidebug boot add <bootid> <label> <interface> <devnum>[:<part>] <file path> [<load options>]\n"
1554 " - set UEFI BootXXXX variable\n"
1555 " <load options> will be passed to UEFI application\n"
1556 "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
1557 " - delete UEFI BootXXXX variables\n"
1558 "efidebug boot dump\n"
1559 " - dump all UEFI BootXXXX variables\n"
1560 "efidebug boot next <bootid>\n"
1561 " - set UEFI BootNext variable\n"
1562 "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
1563 " - set/show UEFI boot order\n"
1565 #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
1566 "efidebug capsule update [-v] <capsule address>\n"
1567 " - process a capsule\n"
1568 "efidebug capsule disk-update\n"
1569 " - update a capsule from disk\n"
1570 "efidebug capsule show <capsule address>\n"
1571 " - show capsule information\n"
1572 "efidebug capsule result [<capsule result var>]\n"
1573 " - show a capsule update result\n"
1576 "efidebug devices\n"
1577 " - show UEFI devices\n"
1578 "efidebug drivers\n"
1579 " - show UEFI drivers\n"
1581 " - show UEFI handles\n"
1583 " - show loaded images\n"
1585 " - show UEFI memory map\n"
1587 " - show UEFI configuration tables\n"
1588 #ifdef CONFIG_CMD_BOOTEFI_BOOTMGR
1589 "efidebug test bootmgr\n"
1590 " - run simple bootmgr for test\n"
1592 "efidebug query [-nv][-bs][-rt][-at]\n"
1593 " - show size of UEFI variables store\n";
1597 efidebug, 10, 0, do_efidebug,
1598 "Configure UEFI environment",