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;
83 * do_efi_capsule_show() - show capsule information
85 * @cmdtp: Command table
87 * @argc: Number of arguments
88 * @argv: Argument array
89 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
91 * Implement efidebug "capsule show" sub-command.
92 * show capsule information.
94 * efidebug capsule show <capsule address>
96 static int do_efi_capsule_show(struct cmd_tbl *cmdtp, int flag,
97 int argc, char * const argv[])
99 struct efi_capsule_header *capsule;
103 return CMD_RET_USAGE;
105 capsule = (typeof(capsule))simple_strtoul(argv[1], &endp, 16);
106 if (endp == argv[1]) {
107 printf("Invalid address: %s", argv[1]);
108 return CMD_RET_FAILURE;
111 printf("Capsule guid: %pUl\n", &capsule->capsule_guid);
112 printf("Capsule flags: 0x%x\n", capsule->flags);
113 printf("Capsule header size: 0x%x\n", capsule->header_size);
114 printf("Capsule image size: 0x%x\n",
115 capsule->capsule_image_size);
117 return CMD_RET_SUCCESS;
121 * do_efi_capsule_res() - show a capsule update result
123 * @cmdtp: Command table
124 * @flag: Command flag
125 * @argc: Number of arguments
126 * @argv: Argument array
127 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
129 * Implement efidebug "capsule result" sub-command.
130 * show a capsule update result.
131 * If result number is not specified, CapsuleLast will be shown.
133 * efidebug capsule result [<capsule result number>]
135 static int do_efi_capsule_res(struct cmd_tbl *cmdtp, int flag,
136 int argc, char * const argv[])
141 u16 var_name16[12], *p;
143 struct efi_capsule_result_variable_header *result = NULL;
147 if (argc != 1 && argc != 2)
148 return CMD_RET_USAGE;
150 guid = efi_guid_capsule_report;
152 size = sizeof(var_name16);
153 ret = EFI_CALL(RT->get_variable(L"CapsuleLast", &guid, NULL,
155 if (ret != EFI_SUCCESS) {
156 if (ret == EFI_NOT_FOUND)
157 printf("CapsuleLast doesn't exist\n");
159 printf("Failed to get CapsuleLast\n");
161 return CMD_RET_FAILURE;
163 printf("CapsuleLast is %ls\n", var_name16);
168 capsule_id = simple_strtoul(argv[0], &endp, 16);
169 if (capsule_id < 0 || capsule_id > 0xffff)
170 return CMD_RET_USAGE;
172 sprintf(var_name, "Capsule%04X", capsule_id);
174 utf8_utf16_strncpy(&p, var_name, 9);
178 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, NULL));
179 if (ret == EFI_BUFFER_TOO_SMALL) {
180 result = malloc(size);
181 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
183 if (ret != EFI_SUCCESS) {
185 printf("Failed to get %ls\n", var_name16);
187 return CMD_RET_FAILURE;
191 printf("Result total size: 0x%x\n", result->variable_total_size);
192 printf("Capsule guid: %pUl\n", &result->capsule_guid);
193 printf("Time processed: %04d-%02d-%02d %02d:%02d:%02d\n",
194 result->capsule_processed.year, result->capsule_processed.month,
195 result->capsule_processed.day, result->capsule_processed.hour,
196 result->capsule_processed.minute,
197 result->capsule_processed.second);
198 printf("Capsule status: 0x%lx\n", result->capsule_status);
202 return CMD_RET_SUCCESS;
205 static struct cmd_tbl cmd_efidebug_capsule_sub[] = {
206 U_BOOT_CMD_MKENT(update, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_update,
208 U_BOOT_CMD_MKENT(show, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_show,
210 U_BOOT_CMD_MKENT(result, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_res,
215 * do_efi_capsule() - manage UEFI capsules
217 * @cmdtp: Command table
218 * @flag: Command flag
219 * @argc: Number of arguments
220 * @argv: Argument array
221 * Return: CMD_RET_SUCCESS on success,
222 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
224 * Implement efidebug "capsule" sub-command.
226 static int do_efi_capsule(struct cmd_tbl *cmdtp, int flag,
227 int argc, char * const argv[])
232 return CMD_RET_USAGE;
236 cp = find_cmd_tbl(argv[0], cmd_efidebug_capsule_sub,
237 ARRAY_SIZE(cmd_efidebug_capsule_sub));
239 return CMD_RET_USAGE;
241 return cp->cmd(cmdtp, flag, argc, argv);
243 #endif /* CONFIG_EFI_HAVE_CAPSULE_SUPPORT */
246 * efi_get_device_handle_info() - get information of UEFI device
248 * @handle: Handle of UEFI device
249 * @dev_path_text: Pointer to text of device path
250 * Return: 0 on success, -1 on failure
252 * Currently return a formatted text of device path.
254 static int efi_get_device_handle_info(efi_handle_t handle, u16 **dev_path_text)
256 struct efi_device_path *dp;
259 ret = EFI_CALL(BS->open_protocol(handle, &efi_guid_device_path,
260 (void **)&dp, NULL /* FIXME */, NULL,
261 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
262 if (ret == EFI_SUCCESS) {
263 *dev_path_text = efi_dp_str(dp);
270 #define EFI_HANDLE_WIDTH ((int)sizeof(efi_handle_t) * 2)
272 static const char spc[] = " ";
273 static const char sep[] = "================";
276 * do_efi_show_devices() - show UEFI devices
278 * @cmdtp: Command table
279 * @flag: Command flag
280 * @argc: Number of arguments
281 * @argv: Argument array
282 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
284 * Implement efidebug "devices" sub-command.
285 * Show all UEFI devices and their information.
287 static int do_efi_show_devices(struct cmd_tbl *cmdtp, int flag,
288 int argc, char *const argv[])
290 efi_handle_t *handles;
295 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
297 if (ret != EFI_SUCCESS)
298 return CMD_RET_FAILURE;
301 return CMD_RET_SUCCESS;
303 printf("Device%.*s Device Path\n", EFI_HANDLE_WIDTH - 6, spc);
304 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
305 for (i = 0; i < num; i++) {
306 if (!efi_get_device_handle_info(handles[i], &dev_path_text)) {
307 printf("%p %ls\n", handles[i], dev_path_text);
308 efi_free_pool(dev_path_text);
312 efi_free_pool(handles);
314 return CMD_RET_SUCCESS;
318 * efi_get_driver_handle_info() - get information of UEFI driver
320 * @handle: Handle of UEFI device
321 * @driver_name: Driver name
322 * @image_path: Pointer to text of device path
323 * Return: 0 on success, -1 on failure
325 * Currently return no useful information as all UEFI drivers are
328 static int efi_get_driver_handle_info(efi_handle_t handle, u16 **driver_name,
331 struct efi_handler *handler;
332 struct efi_loaded_image *image;
337 * TODO: support EFI_COMPONENT_NAME2_PROTOCOL
342 ret = efi_search_protocol(handle, &efi_guid_loaded_image, &handler);
343 if (ret != EFI_SUCCESS) {
348 image = handler->protocol_interface;
349 *image_path = efi_dp_str(image->file_path);
355 * do_efi_show_drivers() - show UEFI drivers
357 * @cmdtp: Command table
358 * @flag: Command flag
359 * @argc: Number of arguments
360 * @argv: Argument array
361 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
363 * Implement efidebug "drivers" sub-command.
364 * Show all UEFI drivers and their information.
366 static int do_efi_show_drivers(struct cmd_tbl *cmdtp, int flag,
367 int argc, char *const argv[])
369 efi_handle_t *handles;
371 u16 *driver_name, *image_path_text;
374 ret = EFI_CALL(efi_locate_handle_buffer(
375 BY_PROTOCOL, &efi_guid_driver_binding_protocol,
376 NULL, &num, &handles));
377 if (ret != EFI_SUCCESS)
378 return CMD_RET_FAILURE;
381 return CMD_RET_SUCCESS;
383 printf("Driver%.*s Name Image Path\n",
384 EFI_HANDLE_WIDTH - 6, spc);
385 printf("%.*s ==================== ====================\n",
386 EFI_HANDLE_WIDTH, sep);
387 for (i = 0; i < num; i++) {
388 if (!efi_get_driver_handle_info(handles[i], &driver_name,
391 printf("%p %-20ls %ls\n", handles[i],
392 driver_name, image_path_text);
394 printf("%p %-20ls <built-in>\n",
395 handles[i], driver_name);
396 efi_free_pool(driver_name);
397 efi_free_pool(image_path_text);
401 efi_free_pool(handles);
403 return CMD_RET_SUCCESS;
406 static const struct {
408 const efi_guid_t guid;
412 EFI_DEVICE_PATH_PROTOCOL_GUID,
415 "Device Path To Text",
416 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID,
419 "Device Path Utilities",
420 EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID,
423 "Unicode Collation 2",
424 EFI_UNICODE_COLLATION_PROTOCOL2_GUID,
428 EFI_DRIVER_BINDING_PROTOCOL_GUID,
432 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID,
435 "Simple Text Input Ex",
436 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID,
439 "Simple Text Output",
440 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID,
444 EFI_BLOCK_IO_PROTOCOL_GUID,
447 "Simple File System",
448 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
452 EFI_LOADED_IMAGE_PROTOCOL_GUID,
456 EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID,
460 EFI_HII_STRING_PROTOCOL_GUID,
464 EFI_HII_DATABASE_PROTOCOL_GUID,
467 "HII Config Routing",
468 EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID,
472 EFI_LOAD_FILE2_PROTOCOL_GUID,
475 "Random Number Generator",
476 EFI_RNG_PROTOCOL_GUID,
480 EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
484 EFI_PXE_BASE_CODE_PROTOCOL_GUID,
486 /* Configuration table GUIDs */
500 "Runtime properties",
501 EFI_RT_PROPERTIES_TABLE_GUID,
504 "TCG2 Final Events Table",
505 EFI_TCG2_FINAL_EVENTS_TABLE_GUID,
510 * get_guid_text - get string of GUID
512 * Return description of GUID.
515 * Return: description of GUID or NULL
517 static const char *get_guid_text(const void *guid)
521 for (i = 0; i < ARRAY_SIZE(guid_list); i++) {
523 * As guidcmp uses memcmp() we can safely accept unaligned
526 if (!guidcmp(&guid_list[i].guid, guid))
527 return guid_list[i].text;
534 * do_efi_show_handles() - show UEFI handles
536 * @cmdtp: Command table
537 * @flag: Command flag
538 * @argc: Number of arguments
539 * @argv: Argument array
540 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
542 * Implement efidebug "dh" sub-command.
543 * Show all UEFI handles and their information, currently all protocols
546 static int do_efi_show_handles(struct cmd_tbl *cmdtp, int flag,
547 int argc, char *const argv[])
549 efi_handle_t *handles;
551 efi_uintn_t num, count, i, j;
552 const char *guid_text;
555 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
557 if (ret != EFI_SUCCESS)
558 return CMD_RET_FAILURE;
561 return CMD_RET_SUCCESS;
563 printf("Handle%.*s Protocols\n", EFI_HANDLE_WIDTH - 6, spc);
564 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
565 for (i = 0; i < num; i++) {
566 printf("%p", handles[i]);
567 ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid,
574 for (j = 0; j < count; j++) {
580 guid_text = get_guid_text(guid[j]);
584 printf("%pUl", guid[j]);
589 efi_free_pool(handles);
591 return CMD_RET_SUCCESS;
595 * do_efi_show_images() - show UEFI images
597 * @cmdtp: Command table
598 * @flag: Command flag
599 * @argc: Number of arguments
600 * @argv: Argument array
601 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
603 * Implement efidebug "images" sub-command.
604 * Show all UEFI loaded images and their information.
606 static int do_efi_show_images(struct cmd_tbl *cmdtp, int flag,
607 int argc, char *const argv[])
609 efi_print_image_infos(NULL);
611 return CMD_RET_SUCCESS;
614 static const char * const efi_mem_type_string[] = {
615 [EFI_RESERVED_MEMORY_TYPE] = "RESERVED",
616 [EFI_LOADER_CODE] = "LOADER CODE",
617 [EFI_LOADER_DATA] = "LOADER DATA",
618 [EFI_BOOT_SERVICES_CODE] = "BOOT CODE",
619 [EFI_BOOT_SERVICES_DATA] = "BOOT DATA",
620 [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE",
621 [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA",
622 [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL",
623 [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM",
624 [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM",
625 [EFI_ACPI_MEMORY_NVS] = "ACPI NVS",
626 [EFI_MMAP_IO] = "IO",
627 [EFI_MMAP_IO_PORT] = "IO PORT",
628 [EFI_PAL_CODE] = "PAL",
629 [EFI_PERSISTENT_MEMORY_TYPE] = "PERSISTENT",
632 static const struct efi_mem_attrs {
635 } efi_mem_attrs[] = {
636 {EFI_MEMORY_UC, "UC"},
637 {EFI_MEMORY_UC, "UC"},
638 {EFI_MEMORY_WC, "WC"},
639 {EFI_MEMORY_WT, "WT"},
640 {EFI_MEMORY_WB, "WB"},
641 {EFI_MEMORY_UCE, "UCE"},
642 {EFI_MEMORY_WP, "WP"},
643 {EFI_MEMORY_RP, "RP"},
644 {EFI_MEMORY_XP, "WP"},
645 {EFI_MEMORY_NV, "NV"},
646 {EFI_MEMORY_MORE_RELIABLE, "REL"},
647 {EFI_MEMORY_RO, "RO"},
648 {EFI_MEMORY_SP, "SP"},
649 {EFI_MEMORY_RUNTIME, "RT"},
653 * print_memory_attributes() - print memory map attributes
655 * @attributes: Attribute value
657 * Print memory map attributes
659 static void print_memory_attributes(u64 attributes)
663 for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
664 if (attributes & efi_mem_attrs[i].bit) {
671 puts(efi_mem_attrs[i].text);
675 #define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
678 * do_efi_show_memmap() - show UEFI memory map
680 * @cmdtp: Command table
681 * @flag: Command flag
682 * @argc: Number of arguments
683 * @argv: Argument array
684 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
686 * Implement efidebug "memmap" sub-command.
687 * Show UEFI memory map.
689 static int do_efi_show_memmap(struct cmd_tbl *cmdtp, int flag,
690 int argc, char *const argv[])
692 struct efi_mem_desc *memmap = NULL, *map;
693 efi_uintn_t map_size = 0;
698 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
699 if (ret == EFI_BUFFER_TOO_SMALL) {
700 map_size += sizeof(struct efi_mem_desc); /* for my own */
701 ret = efi_allocate_pool(EFI_LOADER_DATA, map_size,
703 if (ret != EFI_SUCCESS)
704 return CMD_RET_FAILURE;
705 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
707 if (ret != EFI_SUCCESS) {
708 efi_free_pool(memmap);
709 return CMD_RET_FAILURE;
712 printf("Type Start%.*s End%.*s Attributes\n",
713 EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc);
714 printf("================ %.*s %.*s ==========\n",
715 EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep);
717 * Coverity check: dereferencing null pointer "map."
718 * This is a false positive as memmap will always be
719 * populated by allocate_pool() above.
721 for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
722 if (map->type < ARRAY_SIZE(efi_mem_type_string))
723 type = efi_mem_type_string[map->type];
727 printf("%-16s %.*llx-%.*llx", type,
729 (u64)map_to_sysmem((void *)(uintptr_t)
730 map->physical_start),
732 (u64)map_to_sysmem((void *)(uintptr_t)
733 (map->physical_start +
734 map->num_pages * EFI_PAGE_SIZE)));
736 print_memory_attributes(map->attribute);
740 efi_free_pool(memmap);
742 return CMD_RET_SUCCESS;
746 * do_efi_show_tables() - show UEFI configuration tables
748 * @cmdtp: Command table
749 * @flag: Command flag
750 * @argc: Number of arguments
751 * @argv: Argument array
752 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
754 * Implement efidebug "tables" sub-command.
755 * Show UEFI configuration tables.
757 static int do_efi_show_tables(struct cmd_tbl *cmdtp, int flag,
758 int argc, char *const argv[])
761 const char *guid_str;
763 for (i = 0; i < systab.nr_tables; ++i) {
764 guid_str = get_guid_text(&systab.tables[i].guid);
767 printf("%pUl %s\n", &systab.tables[i].guid, guid_str);
770 return CMD_RET_SUCCESS;
774 * do_efi_boot_add() - set UEFI load option
776 * @cmdtp: Command table
777 * @flag: Command flag
778 * @argc: Number of arguments
779 * @argv: Argument array
780 * Return: CMD_RET_SUCCESS on success,
781 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
783 * Implement efidebug "boot add" sub-command. Create or change UEFI load option.
785 * efidebug boot add <id> <label> <interface> <devnum>[:<part>] <file> <options>
787 static int do_efi_boot_add(struct cmd_tbl *cmdtp, int flag,
788 int argc, char *const argv[])
793 u16 var_name16[9], *p;
795 size_t label_len, label_len16;
797 struct efi_device_path *device_path = NULL, *file_path = NULL;
798 struct efi_load_option lo;
802 int r = CMD_RET_SUCCESS;
804 if (argc < 6 || argc > 7)
805 return CMD_RET_USAGE;
807 id = (int)simple_strtoul(argv[1], &endp, 16);
808 if (*endp != '\0' || id > 0xffff)
809 return CMD_RET_USAGE;
811 sprintf(var_name, "Boot%04X", id);
813 utf8_utf16_strncpy(&p, var_name, 9);
815 guid = efi_global_variable_guid;
818 lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
821 label_len = strlen(argv[2]);
822 label_len16 = utf8_utf16_strnlen(argv[2], label_len);
823 label = malloc((label_len16 + 1) * sizeof(u16));
825 return CMD_RET_FAILURE;
826 lo.label = label; /* label will be changed below */
827 utf8_utf16_strncpy(&label, argv[2], label_len);
830 ret = efi_dp_from_name(argv[3], argv[4], argv[5], &device_path,
832 if (ret != EFI_SUCCESS) {
833 printf("Cannot create device path for \"%s %s\"\n",
838 lo.file_path = file_path;
839 lo.file_path_length = efi_dp_size(file_path)
840 + sizeof(struct efi_device_path); /* for END */
844 lo.optional_data = NULL;
846 lo.optional_data = (const u8 *)argv[6];
848 size = efi_serialize_load_option(&lo, (u8 **)&data);
854 ret = EFI_CALL(efi_set_variable(var_name16, &guid,
855 EFI_VARIABLE_NON_VOLATILE |
856 EFI_VARIABLE_BOOTSERVICE_ACCESS |
857 EFI_VARIABLE_RUNTIME_ACCESS,
859 if (ret != EFI_SUCCESS) {
860 printf("Cannot set %ls\n", var_name16);
865 efi_free_pool(device_path);
866 efi_free_pool(file_path);
873 * do_efi_boot_rm() - delete UEFI load options
875 * @cmdtp: Command table
876 * @flag: Command flag
877 * @argc: Number of arguments
878 * @argv: Argument array
879 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
881 * Implement efidebug "boot rm" sub-command.
882 * Delete UEFI load options.
884 * efidebug boot rm <id> ...
886 static int do_efi_boot_rm(struct cmd_tbl *cmdtp, int flag,
887 int argc, char *const argv[])
893 u16 var_name16[9], *p;
897 return CMD_RET_USAGE;
899 guid = efi_global_variable_guid;
900 for (i = 1; i < argc; i++, argv++) {
901 id = (int)simple_strtoul(argv[1], &endp, 16);
902 if (*endp != '\0' || id > 0xffff)
903 return CMD_RET_FAILURE;
905 sprintf(var_name, "Boot%04X", id);
907 utf8_utf16_strncpy(&p, var_name, 9);
909 ret = EFI_CALL(efi_set_variable(var_name16, &guid, 0, 0, NULL));
911 printf("Cannot remove %ls\n", var_name16);
912 return CMD_RET_FAILURE;
916 return CMD_RET_SUCCESS;
920 * show_efi_boot_opt_data() - dump UEFI load option
922 * @varname16: variable name
923 * @data: value of UEFI load option variable
924 * @size: size of the boot option
926 * Decode the value of UEFI load option variable and print information.
928 static void show_efi_boot_opt_data(u16 *varname16, void *data, size_t *size)
930 struct efi_load_option lo;
932 size_t label_len16, label_len;
936 ret = efi_deserialize_load_option(&lo, data, size);
937 if (ret != EFI_SUCCESS) {
938 printf("%ls: invalid load option\n", varname16);
942 label_len16 = u16_strlen(lo.label);
943 label_len = utf16_utf8_strnlen(lo.label, label_len16);
944 label = malloc(label_len + 1);
948 utf16_utf8_strncpy(&p, lo.label, label_len16);
950 printf("%ls:\nattributes: %c%c%c (0x%08x)\n",
953 lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
954 /* FORCE RECONNECT */
955 lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
957 lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
959 printf(" label: %s\n", label);
961 dp_str = efi_dp_str(lo.file_path);
962 printf(" file_path: %ls\n", dp_str);
963 efi_free_pool(dp_str);
966 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
967 lo.optional_data, *size, true);
972 * show_efi_boot_opt() - dump UEFI load option
974 * @varname16: variable name
976 * Dump information defined by UEFI load option.
978 static void show_efi_boot_opt(u16 *varname16)
985 ret = EFI_CALL(efi_get_variable(varname16, &efi_global_variable_guid,
987 if (ret == EFI_BUFFER_TOO_SMALL) {
990 printf("ERROR: Out of memory\n");
993 ret = EFI_CALL(efi_get_variable(varname16,
994 &efi_global_variable_guid,
996 if (ret == EFI_SUCCESS)
997 show_efi_boot_opt_data(varname16, data, &size);
1002 static int u16_tohex(u16 c)
1004 if (c >= '0' && c <= '9')
1006 if (c >= 'A' && c <= 'F')
1007 return c - 'A' + 10;
1009 /* not hexadecimal */
1014 * show_efi_boot_dump() - dump all UEFI load options
1016 * @cmdtp: Command table
1017 * @flag: Command flag
1018 * @argc: Number of arguments
1019 * @argv: Argument array
1020 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1022 * Implement efidebug "boot dump" sub-command.
1023 * Dump information of all UEFI load options defined.
1025 * efidebug boot dump
1027 static int do_efi_boot_dump(struct cmd_tbl *cmdtp, int flag,
1028 int argc, char *const argv[])
1030 u16 *var_name16, *p;
1031 efi_uintn_t buf_size, size;
1037 return CMD_RET_USAGE;
1040 var_name16 = malloc(buf_size);
1042 return CMD_RET_FAILURE;
1047 ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
1049 if (ret == EFI_NOT_FOUND)
1051 if (ret == EFI_BUFFER_TOO_SMALL) {
1053 p = realloc(var_name16, buf_size);
1056 return CMD_RET_FAILURE;
1059 ret = EFI_CALL(efi_get_next_variable_name(&size,
1063 if (ret != EFI_SUCCESS) {
1065 return CMD_RET_FAILURE;
1068 if (memcmp(var_name16, L"Boot", 8))
1071 for (id = 0, i = 0; i < 4; i++) {
1072 digit = u16_tohex(var_name16[4 + i]);
1075 id = (id << 4) + digit;
1077 if (i == 4 && !var_name16[8])
1078 show_efi_boot_opt(var_name16);
1083 return CMD_RET_SUCCESS;
1087 * show_efi_boot_order() - show order of UEFI load options
1089 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1091 * Show order of UEFI load options defined by BootOrder variable.
1093 static int show_efi_boot_order(void)
1099 u16 var_name16[9], *p16;
1101 struct efi_load_option lo;
1103 size_t label_len16, label_len;
1107 ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid,
1108 NULL, &size, NULL));
1109 if (ret != EFI_BUFFER_TOO_SMALL) {
1110 if (ret == EFI_NOT_FOUND) {
1111 printf("BootOrder not defined\n");
1112 return CMD_RET_SUCCESS;
1114 return CMD_RET_FAILURE;
1117 bootorder = malloc(size);
1119 printf("ERROR: Out of memory\n");
1120 return CMD_RET_FAILURE;
1122 ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid,
1123 NULL, &size, bootorder));
1124 if (ret != EFI_SUCCESS) {
1125 ret = CMD_RET_FAILURE;
1129 num = size / sizeof(u16);
1130 for (i = 0; i < num; i++) {
1131 sprintf(var_name, "Boot%04X", bootorder[i]);
1133 utf8_utf16_strncpy(&p16, var_name, 9);
1136 ret = EFI_CALL(efi_get_variable(var_name16,
1137 &efi_global_variable_guid, NULL,
1139 if (ret != EFI_BUFFER_TOO_SMALL) {
1140 printf("%2d: %s: (not defined)\n", i + 1, var_name);
1144 data = malloc(size);
1146 ret = CMD_RET_FAILURE;
1149 ret = EFI_CALL(efi_get_variable(var_name16,
1150 &efi_global_variable_guid, NULL,
1152 if (ret != EFI_SUCCESS) {
1154 ret = CMD_RET_FAILURE;
1158 ret = efi_deserialize_load_option(&lo, data, &size);
1159 if (ret != EFI_SUCCESS) {
1160 printf("%ls: invalid load option\n", var_name16);
1161 ret = CMD_RET_FAILURE;
1165 label_len16 = u16_strlen(lo.label);
1166 label_len = utf16_utf8_strnlen(lo.label, label_len16);
1167 label = malloc(label_len + 1);
1170 ret = CMD_RET_FAILURE;
1174 utf16_utf8_strncpy(&p, lo.label, label_len16);
1175 printf("%2d: %s: %s\n", i + 1, var_name, label);
1187 * do_efi_boot_next() - manage UEFI BootNext variable
1189 * @cmdtp: Command table
1190 * @flag: Command flag
1191 * @argc: Number of arguments
1192 * @argv: Argument array
1193 * Return: CMD_RET_SUCCESS on success,
1194 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1196 * Implement efidebug "boot next" sub-command.
1197 * Set BootNext variable.
1199 * efidebug boot next <id>
1201 static int do_efi_boot_next(struct cmd_tbl *cmdtp, int flag,
1202 int argc, char *const argv[])
1209 int r = CMD_RET_SUCCESS;
1212 return CMD_RET_USAGE;
1214 bootnext = (u16)simple_strtoul(argv[1], &endp, 16);
1216 printf("invalid value: %s\n", argv[1]);
1217 r = CMD_RET_FAILURE;
1221 guid = efi_global_variable_guid;
1223 ret = EFI_CALL(efi_set_variable(L"BootNext", &guid,
1224 EFI_VARIABLE_NON_VOLATILE |
1225 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1226 EFI_VARIABLE_RUNTIME_ACCESS,
1228 if (ret != EFI_SUCCESS) {
1229 printf("Cannot set BootNext\n");
1230 r = CMD_RET_FAILURE;
1237 * do_efi_boot_order() - manage UEFI BootOrder variable
1239 * @cmdtp: Command table
1240 * @flag: Command flag
1241 * @argc: Number of arguments
1242 * @argv: Argument array
1243 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1245 * Implement efidebug "boot order" sub-command.
1246 * Show order of UEFI load options, or change it in BootOrder variable.
1248 * efidebug boot order [<id> ...]
1250 static int do_efi_boot_order(struct cmd_tbl *cmdtp, int flag,
1251 int argc, char *const argv[])
1253 u16 *bootorder = NULL;
1259 int r = CMD_RET_SUCCESS;
1262 return show_efi_boot_order();
1267 size = argc * sizeof(u16);
1268 bootorder = malloc(size);
1270 return CMD_RET_FAILURE;
1272 for (i = 0; i < argc; i++) {
1273 id = (int)simple_strtoul(argv[i], &endp, 16);
1274 if (*endp != '\0' || id > 0xffff) {
1275 printf("invalid value: %s\n", argv[i]);
1276 r = CMD_RET_FAILURE;
1280 bootorder[i] = (u16)id;
1283 guid = efi_global_variable_guid;
1284 ret = EFI_CALL(efi_set_variable(L"BootOrder", &guid,
1285 EFI_VARIABLE_NON_VOLATILE |
1286 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1287 EFI_VARIABLE_RUNTIME_ACCESS,
1289 if (ret != EFI_SUCCESS) {
1290 printf("Cannot set BootOrder\n");
1291 r = CMD_RET_FAILURE;
1299 static struct cmd_tbl cmd_efidebug_boot_sub[] = {
1300 U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
1301 U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
1302 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
1303 U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
1304 U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
1309 * do_efi_boot_opt() - manage UEFI load options
1311 * @cmdtp: Command table
1312 * @flag: Command flag
1313 * @argc: Number of arguments
1314 * @argv: Argument array
1315 * Return: CMD_RET_SUCCESS on success,
1316 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1318 * Implement efidebug "boot" sub-command.
1320 static int do_efi_boot_opt(struct cmd_tbl *cmdtp, int flag,
1321 int argc, char *const argv[])
1326 return CMD_RET_USAGE;
1330 cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
1331 ARRAY_SIZE(cmd_efidebug_boot_sub));
1333 return CMD_RET_USAGE;
1335 return cp->cmd(cmdtp, flag, argc, argv);
1339 * do_efi_test_bootmgr() - run simple bootmgr for test
1341 * @cmdtp: Command table
1342 * @flag: Command flag
1343 * @argc: Number of arguments
1344 * @argv: Argument array
1345 * Return: CMD_RET_SUCCESS on success,
1346 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1348 * Implement efidebug "test bootmgr" sub-command.
1349 * Run simple bootmgr for test.
1351 * efidebug test bootmgr
1353 static int do_efi_test_bootmgr(struct cmd_tbl *cmdtp, int flag,
1354 int argc, char * const argv[])
1357 efi_uintn_t exit_data_size = 0;
1358 u16 *exit_data = NULL;
1360 void *load_options = NULL;
1362 ret = efi_bootmgr_load(&image, &load_options);
1363 printf("efi_bootmgr_load() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1365 /* We call efi_start_image() even if error for test purpose. */
1366 ret = EFI_CALL(efi_start_image(image, &exit_data_size, &exit_data));
1367 printf("efi_start_image() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1368 if (ret && exit_data)
1369 efi_free_pool(exit_data);
1374 return CMD_RET_SUCCESS;
1377 static struct cmd_tbl cmd_efidebug_test_sub[] = {
1378 U_BOOT_CMD_MKENT(bootmgr, CONFIG_SYS_MAXARGS, 1, do_efi_test_bootmgr,
1383 * do_efi_test() - manage UEFI load options
1385 * @cmdtp: Command table
1386 * @flag: Command flag
1387 * @argc: Number of arguments
1388 * @argv: Argument array
1389 * Return: CMD_RET_SUCCESS on success,
1390 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1392 * Implement efidebug "test" sub-command.
1394 static int do_efi_test(struct cmd_tbl *cmdtp, int flag,
1395 int argc, char * const argv[])
1400 return CMD_RET_USAGE;
1404 cp = find_cmd_tbl(argv[0], cmd_efidebug_test_sub,
1405 ARRAY_SIZE(cmd_efidebug_test_sub));
1407 return CMD_RET_USAGE;
1409 return cp->cmd(cmdtp, flag, argc, argv);
1413 * do_efi_query_info() - QueryVariableInfo EFI service
1415 * @cmdtp: Command table
1416 * @flag: Command flag
1417 * @argc: Number of arguments
1418 * @argv: Argument array
1419 * Return: CMD_RET_SUCCESS on success,
1420 * CMD_RET_USAGE or CMD_RET_FAILURE on failure
1422 * Implement efidebug "test" sub-command.
1425 static int do_efi_query_info(struct cmd_tbl *cmdtp, int flag,
1426 int argc, char * const argv[])
1430 u64 max_variable_storage_size;
1431 u64 remain_variable_storage_size;
1432 u64 max_variable_size;
1435 for (i = 1; i < argc; i++) {
1436 if (!strcmp(argv[i], "-bs"))
1437 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
1438 else if (!strcmp(argv[i], "-rt"))
1439 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
1440 else if (!strcmp(argv[i], "-nv"))
1441 attr |= EFI_VARIABLE_NON_VOLATILE;
1442 else if (!strcmp(argv[i], "-at"))
1444 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
1447 ret = EFI_CALL(efi_query_variable_info(attr,
1448 &max_variable_storage_size,
1449 &remain_variable_storage_size,
1450 &max_variable_size));
1451 if (ret != EFI_SUCCESS) {
1452 printf("Error: Cannot query UEFI variables, r = %lu\n",
1453 ret & ~EFI_ERROR_MASK);
1454 return CMD_RET_FAILURE;
1457 printf("Max storage size %llu\n", max_variable_storage_size);
1458 printf("Remaining storage size %llu\n", remain_variable_storage_size);
1459 printf("Max variable size %llu\n", max_variable_size);
1461 return CMD_RET_SUCCESS;
1464 static struct cmd_tbl cmd_efidebug_sub[] = {
1465 U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
1466 #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
1467 U_BOOT_CMD_MKENT(capsule, CONFIG_SYS_MAXARGS, 1, do_efi_capsule,
1470 U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices,
1472 U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
1474 U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
1476 U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
1478 U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
1480 U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables,
1482 U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_efi_test,
1484 U_BOOT_CMD_MKENT(query, CONFIG_SYS_MAXARGS, 1, do_efi_query_info,
1489 * do_efidebug() - display and configure UEFI environment
1491 * @cmdtp: Command table
1492 * @flag: Command flag
1493 * @argc: Number of arguments
1494 * @argv: Argument array
1495 * Return: CMD_RET_SUCCESS on success,
1496 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1498 * Implement efidebug command which allows us to display and
1499 * configure UEFI environment.
1501 static int do_efidebug(struct cmd_tbl *cmdtp, int flag,
1502 int argc, char *const argv[])
1508 return CMD_RET_USAGE;
1512 /* Initialize UEFI drivers */
1513 r = efi_init_obj_list();
1514 if (r != EFI_SUCCESS) {
1515 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1516 r & ~EFI_ERROR_MASK);
1517 return CMD_RET_FAILURE;
1520 cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
1521 ARRAY_SIZE(cmd_efidebug_sub));
1523 return CMD_RET_USAGE;
1525 return cp->cmd(cmdtp, flag, argc, argv);
1528 #ifdef CONFIG_SYS_LONGHELP
1529 static char efidebug_help_text[] =
1530 " - UEFI Shell-like interface to configure UEFI environment\n"
1532 "efidebug boot add <bootid> <label> <interface> <devnum>[:<part>] <file path> [<load options>]\n"
1533 " - set UEFI BootXXXX variable\n"
1534 " <load options> will be passed to UEFI application\n"
1535 "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
1536 " - delete UEFI BootXXXX variables\n"
1537 "efidebug boot dump\n"
1538 " - dump all UEFI BootXXXX variables\n"
1539 "efidebug boot next <bootid>\n"
1540 " - set UEFI BootNext variable\n"
1541 "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
1542 " - set/show UEFI boot order\n"
1544 #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
1545 "efidebug capsule update [-v] <capsule address>\n"
1546 " - process a capsule\n"
1547 "efidebug capsule show <capsule address>\n"
1548 " - show capsule information\n"
1549 "efidebug capsule result [<capsule result var>]\n"
1550 " - show a capsule update result\n"
1553 "efidebug devices\n"
1554 " - show UEFI devices\n"
1555 "efidebug drivers\n"
1556 " - show UEFI drivers\n"
1558 " - show UEFI handles\n"
1560 " - show loaded images\n"
1562 " - show UEFI memory map\n"
1564 " - show UEFI configuration tables\n"
1565 "efidebug test bootmgr\n"
1566 " - run simple bootmgr for test\n"
1567 "efidebug query [-nv][-bs][-rt][-at]\n"
1568 " - show size of UEFI variables store\n";
1572 efidebug, 10, 0, do_efidebug,
1573 "Configure UEFI environment",