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>
18 #include <linux/ctype.h>
20 #define BS systab.boottime
23 * efi_get_device_handle_info() - get information of UEFI device
25 * @handle: Handle of UEFI device
26 * @dev_path_text: Pointer to text of device path
27 * Return: 0 on success, -1 on failure
29 * Currently return a formatted text of device path.
31 static int efi_get_device_handle_info(efi_handle_t handle, u16 **dev_path_text)
33 struct efi_device_path *dp;
36 ret = EFI_CALL(BS->open_protocol(handle, &efi_guid_device_path,
37 (void **)&dp, NULL /* FIXME */, NULL,
38 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
39 if (ret == EFI_SUCCESS) {
40 *dev_path_text = efi_dp_str(dp);
47 #define EFI_HANDLE_WIDTH ((int)sizeof(efi_handle_t) * 2)
49 static const char spc[] = " ";
50 static const char sep[] = "================";
53 * do_efi_show_devices() - show UEFI devices
55 * @cmdtp: Command table
57 * @argc: Number of arguments
58 * @argv: Argument array
59 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
61 * Implement efidebug "devices" sub-command.
62 * Show all UEFI devices and their information.
64 static int do_efi_show_devices(struct cmd_tbl *cmdtp, int flag,
65 int argc, char *const argv[])
67 efi_handle_t *handles;
72 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
74 if (ret != EFI_SUCCESS)
75 return CMD_RET_FAILURE;
78 return CMD_RET_SUCCESS;
80 printf("Device%.*s Device Path\n", EFI_HANDLE_WIDTH - 6, spc);
81 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
82 for (i = 0; i < num; i++) {
83 if (!efi_get_device_handle_info(handles[i], &dev_path_text)) {
84 printf("%p %ls\n", handles[i], dev_path_text);
85 efi_free_pool(dev_path_text);
89 efi_free_pool(handles);
91 return CMD_RET_SUCCESS;
95 * efi_get_driver_handle_info() - get information of UEFI driver
97 * @handle: Handle of UEFI device
98 * @driver_name: Driver name
99 * @image_path: Pointer to text of device path
100 * Return: 0 on success, -1 on failure
102 * Currently return no useful information as all UEFI drivers are
105 static int efi_get_driver_handle_info(efi_handle_t handle, u16 **driver_name,
108 struct efi_handler *handler;
109 struct efi_loaded_image *image;
114 * TODO: support EFI_COMPONENT_NAME2_PROTOCOL
119 ret = efi_search_protocol(handle, &efi_guid_loaded_image, &handler);
120 if (ret != EFI_SUCCESS) {
125 image = handler->protocol_interface;
126 *image_path = efi_dp_str(image->file_path);
132 * do_efi_show_drivers() - show UEFI drivers
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 "drivers" sub-command.
141 * Show all UEFI drivers and their information.
143 static int do_efi_show_drivers(struct cmd_tbl *cmdtp, int flag,
144 int argc, char *const argv[])
146 efi_handle_t *handles;
148 u16 *driver_name, *image_path_text;
151 ret = EFI_CALL(efi_locate_handle_buffer(
152 BY_PROTOCOL, &efi_guid_driver_binding_protocol,
153 NULL, &num, &handles));
154 if (ret != EFI_SUCCESS)
155 return CMD_RET_FAILURE;
158 return CMD_RET_SUCCESS;
160 printf("Driver%.*s Name Image Path\n",
161 EFI_HANDLE_WIDTH - 6, spc);
162 printf("%.*s ==================== ====================\n",
163 EFI_HANDLE_WIDTH, sep);
164 for (i = 0; i < num; i++) {
165 if (!efi_get_driver_handle_info(handles[i], &driver_name,
168 printf("%p %-20ls %ls\n", handles[i],
169 driver_name, image_path_text);
171 printf("%p %-20ls <built-in>\n",
172 handles[i], driver_name);
173 efi_free_pool(driver_name);
174 efi_free_pool(image_path_text);
178 efi_free_pool(handles);
180 return CMD_RET_SUCCESS;
183 static const struct {
185 const efi_guid_t guid;
189 EFI_DEVICE_PATH_PROTOCOL_GUID,
192 "Device Path To Text",
193 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID,
196 "Device Path Utilities",
197 EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID,
200 "Unicode Collation 2",
201 EFI_UNICODE_COLLATION_PROTOCOL2_GUID,
205 EFI_DRIVER_BINDING_PROTOCOL_GUID,
209 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID,
212 "Simple Text Input Ex",
213 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID,
216 "Simple Text Output",
217 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID,
221 EFI_BLOCK_IO_PROTOCOL_GUID,
224 "Simple File System",
225 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
229 EFI_LOADED_IMAGE_PROTOCOL_GUID,
233 EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID,
237 EFI_HII_STRING_PROTOCOL_GUID,
241 EFI_HII_DATABASE_PROTOCOL_GUID,
244 "HII Config Routing",
245 EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID,
249 EFI_LOAD_FILE2_PROTOCOL_GUID,
253 EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
257 EFI_PXE_BASE_CODE_PROTOCOL_GUID,
259 /* Configuration table GUIDs */
273 "Runtime properties",
274 EFI_RT_PROPERTIES_TABLE_GUID,
279 * get_guid_text - get string of GUID
281 * Return description of GUID.
284 * Return: description of GUID or NULL
286 static const char *get_guid_text(const void *guid)
290 for (i = 0; i < ARRAY_SIZE(guid_list); i++) {
292 * As guidcmp uses memcmp() we can safely accept unaligned
295 if (!guidcmp(&guid_list[i].guid, guid))
296 return guid_list[i].text;
303 * do_efi_show_handles() - show UEFI handles
305 * @cmdtp: Command table
306 * @flag: Command flag
307 * @argc: Number of arguments
308 * @argv: Argument array
309 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
311 * Implement efidebug "dh" sub-command.
312 * Show all UEFI handles and their information, currently all protocols
315 static int do_efi_show_handles(struct cmd_tbl *cmdtp, int flag,
316 int argc, char *const argv[])
318 efi_handle_t *handles;
320 efi_uintn_t num, count, i, j;
321 const char *guid_text;
324 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
326 if (ret != EFI_SUCCESS)
327 return CMD_RET_FAILURE;
330 return CMD_RET_SUCCESS;
332 printf("Handle%.*s Protocols\n", EFI_HANDLE_WIDTH - 6, spc);
333 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
334 for (i = 0; i < num; i++) {
335 printf("%p", handles[i]);
336 ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid,
343 for (j = 0; j < count; j++) {
349 guid_text = get_guid_text(guid[j]);
353 printf("%pUl", guid[j]);
358 efi_free_pool(handles);
360 return CMD_RET_SUCCESS;
364 * do_efi_show_images() - show UEFI images
366 * @cmdtp: Command table
367 * @flag: Command flag
368 * @argc: Number of arguments
369 * @argv: Argument array
370 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
372 * Implement efidebug "images" sub-command.
373 * Show all UEFI loaded images and their information.
375 static int do_efi_show_images(struct cmd_tbl *cmdtp, int flag,
376 int argc, char *const argv[])
378 efi_print_image_infos(NULL);
380 return CMD_RET_SUCCESS;
383 static const char * const efi_mem_type_string[] = {
384 [EFI_RESERVED_MEMORY_TYPE] = "RESERVED",
385 [EFI_LOADER_CODE] = "LOADER CODE",
386 [EFI_LOADER_DATA] = "LOADER DATA",
387 [EFI_BOOT_SERVICES_CODE] = "BOOT CODE",
388 [EFI_BOOT_SERVICES_DATA] = "BOOT DATA",
389 [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE",
390 [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA",
391 [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL",
392 [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM",
393 [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM",
394 [EFI_ACPI_MEMORY_NVS] = "ACPI NVS",
395 [EFI_MMAP_IO] = "IO",
396 [EFI_MMAP_IO_PORT] = "IO PORT",
397 [EFI_PAL_CODE] = "PAL",
398 [EFI_PERSISTENT_MEMORY_TYPE] = "PERSISTENT",
401 static const struct efi_mem_attrs {
404 } efi_mem_attrs[] = {
405 {EFI_MEMORY_UC, "UC"},
406 {EFI_MEMORY_UC, "UC"},
407 {EFI_MEMORY_WC, "WC"},
408 {EFI_MEMORY_WT, "WT"},
409 {EFI_MEMORY_WB, "WB"},
410 {EFI_MEMORY_UCE, "UCE"},
411 {EFI_MEMORY_WP, "WP"},
412 {EFI_MEMORY_RP, "RP"},
413 {EFI_MEMORY_XP, "WP"},
414 {EFI_MEMORY_NV, "NV"},
415 {EFI_MEMORY_MORE_RELIABLE, "REL"},
416 {EFI_MEMORY_RO, "RO"},
417 {EFI_MEMORY_SP, "SP"},
418 {EFI_MEMORY_RUNTIME, "RT"},
422 * print_memory_attributes() - print memory map attributes
424 * @attributes: Attribute value
426 * Print memory map attributes
428 static void print_memory_attributes(u64 attributes)
432 for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
433 if (attributes & efi_mem_attrs[i].bit) {
440 puts(efi_mem_attrs[i].text);
444 #define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
447 * do_efi_show_memmap() - show UEFI memory map
449 * @cmdtp: Command table
450 * @flag: Command flag
451 * @argc: Number of arguments
452 * @argv: Argument array
453 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
455 * Implement efidebug "memmap" sub-command.
456 * Show UEFI memory map.
458 static int do_efi_show_memmap(struct cmd_tbl *cmdtp, int flag,
459 int argc, char *const argv[])
461 struct efi_mem_desc *memmap = NULL, *map;
462 efi_uintn_t map_size = 0;
467 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
468 if (ret == EFI_BUFFER_TOO_SMALL) {
469 map_size += sizeof(struct efi_mem_desc); /* for my own */
470 ret = efi_allocate_pool(EFI_LOADER_DATA, map_size,
472 if (ret != EFI_SUCCESS)
473 return CMD_RET_FAILURE;
474 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
476 if (ret != EFI_SUCCESS) {
477 efi_free_pool(memmap);
478 return CMD_RET_FAILURE;
481 printf("Type Start%.*s End%.*s Attributes\n",
482 EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc);
483 printf("================ %.*s %.*s ==========\n",
484 EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep);
486 * Coverity check: dereferencing null pointer "map."
487 * This is a false positive as memmap will always be
488 * populated by allocate_pool() above.
490 for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
491 if (map->type < ARRAY_SIZE(efi_mem_type_string))
492 type = efi_mem_type_string[map->type];
496 printf("%-16s %.*llx-%.*llx", type,
498 (u64)map_to_sysmem((void *)(uintptr_t)
499 map->physical_start),
501 (u64)map_to_sysmem((void *)(uintptr_t)
502 (map->physical_start +
503 map->num_pages * EFI_PAGE_SIZE)));
505 print_memory_attributes(map->attribute);
509 efi_free_pool(memmap);
511 return CMD_RET_SUCCESS;
515 * do_efi_show_tables() - show UEFI configuration tables
517 * @cmdtp: Command table
518 * @flag: Command flag
519 * @argc: Number of arguments
520 * @argv: Argument array
521 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
523 * Implement efidebug "tables" sub-command.
524 * Show UEFI configuration tables.
526 static int do_efi_show_tables(struct cmd_tbl *cmdtp, int flag,
527 int argc, char *const argv[])
530 const char *guid_str;
532 for (i = 0; i < systab.nr_tables; ++i) {
533 guid_str = get_guid_text(&systab.tables[i].guid);
536 printf("%pUl %s\n", &systab.tables[i].guid, guid_str);
539 return CMD_RET_SUCCESS;
543 * do_efi_boot_add() - set UEFI load option
545 * @cmdtp: Command table
546 * @flag: Command flag
547 * @argc: Number of arguments
548 * @argv: Argument array
549 * Return: CMD_RET_SUCCESS on success,
550 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
552 * Implement efidebug "boot add" sub-command. Create or change UEFI load option.
554 * efidebug boot add <id> <label> <interface> <devnum>[:<part>] <file> <options>
556 static int do_efi_boot_add(struct cmd_tbl *cmdtp, int flag,
557 int argc, char *const argv[])
562 u16 var_name16[9], *p;
564 size_t label_len, label_len16;
566 struct efi_device_path *device_path = NULL, *file_path = NULL;
567 struct efi_load_option lo;
571 int r = CMD_RET_SUCCESS;
573 if (argc < 6 || argc > 7)
574 return CMD_RET_USAGE;
576 id = (int)simple_strtoul(argv[1], &endp, 16);
577 if (*endp != '\0' || id > 0xffff)
578 return CMD_RET_USAGE;
580 sprintf(var_name, "Boot%04X", id);
582 utf8_utf16_strncpy(&p, var_name, 9);
584 guid = efi_global_variable_guid;
587 lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
590 label_len = strlen(argv[2]);
591 label_len16 = utf8_utf16_strnlen(argv[2], label_len);
592 label = malloc((label_len16 + 1) * sizeof(u16));
594 return CMD_RET_FAILURE;
595 lo.label = label; /* label will be changed below */
596 utf8_utf16_strncpy(&label, argv[2], label_len);
599 ret = efi_dp_from_name(argv[3], argv[4], argv[5], &device_path,
601 if (ret != EFI_SUCCESS) {
602 printf("Cannot create device path for \"%s %s\"\n",
607 lo.file_path = file_path;
608 lo.file_path_length = efi_dp_size(file_path)
609 + sizeof(struct efi_device_path); /* for END */
613 lo.optional_data = NULL;
615 lo.optional_data = (const u8 *)argv[6];
617 size = efi_serialize_load_option(&lo, (u8 **)&data);
623 ret = EFI_CALL(efi_set_variable(var_name16, &guid,
624 EFI_VARIABLE_NON_VOLATILE |
625 EFI_VARIABLE_BOOTSERVICE_ACCESS |
626 EFI_VARIABLE_RUNTIME_ACCESS,
628 if (ret != EFI_SUCCESS) {
629 printf("Cannot set %ls\n", var_name16);
634 efi_free_pool(device_path);
635 efi_free_pool(file_path);
642 * do_efi_boot_rm() - delete UEFI load options
644 * @cmdtp: Command table
645 * @flag: Command flag
646 * @argc: Number of arguments
647 * @argv: Argument array
648 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
650 * Implement efidebug "boot rm" sub-command.
651 * Delete UEFI load options.
653 * efidebug boot rm <id> ...
655 static int do_efi_boot_rm(struct cmd_tbl *cmdtp, int flag,
656 int argc, char *const argv[])
662 u16 var_name16[9], *p;
666 return CMD_RET_USAGE;
668 guid = efi_global_variable_guid;
669 for (i = 1; i < argc; i++, argv++) {
670 id = (int)simple_strtoul(argv[1], &endp, 16);
671 if (*endp != '\0' || id > 0xffff)
672 return CMD_RET_FAILURE;
674 sprintf(var_name, "Boot%04X", id);
676 utf8_utf16_strncpy(&p, var_name, 9);
678 ret = EFI_CALL(efi_set_variable(var_name16, &guid, 0, 0, NULL));
680 printf("Cannot remove %ls\n", var_name16);
681 return CMD_RET_FAILURE;
685 return CMD_RET_SUCCESS;
689 * show_efi_boot_opt_data() - dump UEFI load option
691 * @varname16: variable name
692 * @data: value of UEFI load option variable
693 * @size: size of the boot option
695 * Decode the value of UEFI load option variable and print information.
697 static void show_efi_boot_opt_data(u16 *varname16, void *data, size_t *size)
699 struct efi_load_option lo;
701 size_t label_len16, label_len;
705 ret = efi_deserialize_load_option(&lo, data, size);
706 if (ret != EFI_SUCCESS) {
707 printf("%ls: invalid load option\n", varname16);
711 label_len16 = u16_strlen(lo.label);
712 label_len = utf16_utf8_strnlen(lo.label, label_len16);
713 label = malloc(label_len + 1);
717 utf16_utf8_strncpy(&p, lo.label, label_len16);
719 printf("%ls:\nattributes: %c%c%c (0x%08x)\n",
722 lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
723 /* FORCE RECONNECT */
724 lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
726 lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
728 printf(" label: %s\n", label);
730 dp_str = efi_dp_str(lo.file_path);
731 printf(" file_path: %ls\n", dp_str);
732 efi_free_pool(dp_str);
735 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
736 lo.optional_data, *size, true);
741 * show_efi_boot_opt() - dump UEFI load option
743 * @varname16: variable name
745 * Dump information defined by UEFI load option.
747 static void show_efi_boot_opt(u16 *varname16)
754 ret = EFI_CALL(efi_get_variable(varname16, &efi_global_variable_guid,
756 if (ret == EFI_BUFFER_TOO_SMALL) {
759 printf("ERROR: Out of memory\n");
762 ret = EFI_CALL(efi_get_variable(varname16,
763 &efi_global_variable_guid,
765 if (ret == EFI_SUCCESS)
766 show_efi_boot_opt_data(varname16, data, &size);
771 static int u16_tohex(u16 c)
773 if (c >= '0' && c <= '9')
775 if (c >= 'A' && c <= 'F')
778 /* not hexadecimal */
783 * show_efi_boot_dump() - dump all UEFI load options
785 * @cmdtp: Command table
786 * @flag: Command flag
787 * @argc: Number of arguments
788 * @argv: Argument array
789 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
791 * Implement efidebug "boot dump" sub-command.
792 * Dump information of all UEFI load options defined.
796 static int do_efi_boot_dump(struct cmd_tbl *cmdtp, int flag,
797 int argc, char *const argv[])
800 efi_uintn_t buf_size, size;
806 return CMD_RET_USAGE;
809 var_name16 = malloc(buf_size);
811 return CMD_RET_FAILURE;
816 ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
818 if (ret == EFI_NOT_FOUND)
820 if (ret == EFI_BUFFER_TOO_SMALL) {
822 p = realloc(var_name16, buf_size);
825 return CMD_RET_FAILURE;
828 ret = EFI_CALL(efi_get_next_variable_name(&size,
832 if (ret != EFI_SUCCESS) {
834 return CMD_RET_FAILURE;
837 if (memcmp(var_name16, L"Boot", 8))
840 for (id = 0, i = 0; i < 4; i++) {
841 digit = u16_tohex(var_name16[4 + i]);
844 id = (id << 4) + digit;
846 if (i == 4 && !var_name16[8])
847 show_efi_boot_opt(var_name16);
852 return CMD_RET_SUCCESS;
856 * show_efi_boot_order() - show order of UEFI load options
858 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
860 * Show order of UEFI load options defined by BootOrder variable.
862 static int show_efi_boot_order(void)
868 u16 var_name16[9], *p16;
870 struct efi_load_option lo;
872 size_t label_len16, label_len;
876 ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid,
878 if (ret != EFI_BUFFER_TOO_SMALL) {
879 if (ret == EFI_NOT_FOUND) {
880 printf("BootOrder not defined\n");
881 return CMD_RET_SUCCESS;
883 return CMD_RET_FAILURE;
886 bootorder = malloc(size);
888 printf("ERROR: Out of memory\n");
889 return CMD_RET_FAILURE;
891 ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid,
892 NULL, &size, bootorder));
893 if (ret != EFI_SUCCESS) {
894 ret = CMD_RET_FAILURE;
898 num = size / sizeof(u16);
899 for (i = 0; i < num; i++) {
900 sprintf(var_name, "Boot%04X", bootorder[i]);
902 utf8_utf16_strncpy(&p16, var_name, 9);
905 ret = EFI_CALL(efi_get_variable(var_name16,
906 &efi_global_variable_guid, NULL,
908 if (ret != EFI_BUFFER_TOO_SMALL) {
909 printf("%2d: %s: (not defined)\n", i + 1, var_name);
915 ret = CMD_RET_FAILURE;
918 ret = EFI_CALL(efi_get_variable(var_name16,
919 &efi_global_variable_guid, NULL,
921 if (ret != EFI_SUCCESS) {
923 ret = CMD_RET_FAILURE;
927 ret = efi_deserialize_load_option(&lo, data, &size);
928 if (ret != EFI_SUCCESS) {
929 printf("%ls: invalid load option\n", var_name16);
930 ret = CMD_RET_FAILURE;
934 label_len16 = u16_strlen(lo.label);
935 label_len = utf16_utf8_strnlen(lo.label, label_len16);
936 label = malloc(label_len + 1);
939 ret = CMD_RET_FAILURE;
943 utf16_utf8_strncpy(&p, lo.label, label_len16);
944 printf("%2d: %s: %s\n", i + 1, var_name, label);
956 * do_efi_boot_next() - manage UEFI BootNext variable
958 * @cmdtp: Command table
959 * @flag: Command flag
960 * @argc: Number of arguments
961 * @argv: Argument array
962 * Return: CMD_RET_SUCCESS on success,
963 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
965 * Implement efidebug "boot next" sub-command.
966 * Set BootNext variable.
968 * efidebug boot next <id>
970 static int do_efi_boot_next(struct cmd_tbl *cmdtp, int flag,
971 int argc, char *const argv[])
978 int r = CMD_RET_SUCCESS;
981 return CMD_RET_USAGE;
983 bootnext = (u16)simple_strtoul(argv[1], &endp, 16);
985 printf("invalid value: %s\n", argv[1]);
990 guid = efi_global_variable_guid;
992 ret = EFI_CALL(efi_set_variable(L"BootNext", &guid,
993 EFI_VARIABLE_NON_VOLATILE |
994 EFI_VARIABLE_BOOTSERVICE_ACCESS |
995 EFI_VARIABLE_RUNTIME_ACCESS,
997 if (ret != EFI_SUCCESS) {
998 printf("Cannot set BootNext\n");
1006 * do_efi_boot_order() - manage UEFI BootOrder variable
1008 * @cmdtp: Command table
1009 * @flag: Command flag
1010 * @argc: Number of arguments
1011 * @argv: Argument array
1012 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1014 * Implement efidebug "boot order" sub-command.
1015 * Show order of UEFI load options, or change it in BootOrder variable.
1017 * efidebug boot order [<id> ...]
1019 static int do_efi_boot_order(struct cmd_tbl *cmdtp, int flag,
1020 int argc, char *const argv[])
1022 u16 *bootorder = NULL;
1028 int r = CMD_RET_SUCCESS;
1031 return show_efi_boot_order();
1036 size = argc * sizeof(u16);
1037 bootorder = malloc(size);
1039 return CMD_RET_FAILURE;
1041 for (i = 0; i < argc; i++) {
1042 id = (int)simple_strtoul(argv[i], &endp, 16);
1043 if (*endp != '\0' || id > 0xffff) {
1044 printf("invalid value: %s\n", argv[i]);
1045 r = CMD_RET_FAILURE;
1049 bootorder[i] = (u16)id;
1052 guid = efi_global_variable_guid;
1053 ret = EFI_CALL(efi_set_variable(L"BootOrder", &guid,
1054 EFI_VARIABLE_NON_VOLATILE |
1055 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1056 EFI_VARIABLE_RUNTIME_ACCESS,
1058 if (ret != EFI_SUCCESS) {
1059 printf("Cannot set BootOrder\n");
1060 r = CMD_RET_FAILURE;
1068 static struct cmd_tbl cmd_efidebug_boot_sub[] = {
1069 U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
1070 U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
1071 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
1072 U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
1073 U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
1078 * do_efi_boot_opt() - manage UEFI load options
1080 * @cmdtp: Command table
1081 * @flag: Command flag
1082 * @argc: Number of arguments
1083 * @argv: Argument array
1084 * Return: CMD_RET_SUCCESS on success,
1085 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1087 * Implement efidebug "boot" sub-command.
1089 static int do_efi_boot_opt(struct cmd_tbl *cmdtp, int flag,
1090 int argc, char *const argv[])
1095 return CMD_RET_USAGE;
1099 cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
1100 ARRAY_SIZE(cmd_efidebug_boot_sub));
1102 return CMD_RET_USAGE;
1104 return cp->cmd(cmdtp, flag, argc, argv);
1108 * do_efi_test_bootmgr() - run simple bootmgr for test
1110 * @cmdtp: Command table
1111 * @flag: Command flag
1112 * @argc: Number of arguments
1113 * @argv: Argument array
1114 * Return: CMD_RET_SUCCESS on success,
1115 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1117 * Implement efidebug "test bootmgr" sub-command.
1118 * Run simple bootmgr for test.
1120 * efidebug test bootmgr
1122 static int do_efi_test_bootmgr(struct cmd_tbl *cmdtp, int flag,
1123 int argc, char * const argv[])
1126 efi_uintn_t exit_data_size = 0;
1127 u16 *exit_data = NULL;
1130 ret = efi_bootmgr_load(&image);
1131 printf("efi_bootmgr_load() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1133 /* We call efi_start_image() even if error for test purpose. */
1134 ret = EFI_CALL(efi_start_image(image, &exit_data_size, &exit_data));
1135 printf("efi_start_image() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1136 if (ret && exit_data)
1137 efi_free_pool(exit_data);
1141 return CMD_RET_SUCCESS;
1144 static struct cmd_tbl cmd_efidebug_test_sub[] = {
1145 U_BOOT_CMD_MKENT(bootmgr, CONFIG_SYS_MAXARGS, 1, do_efi_test_bootmgr,
1150 * do_efi_test() - manage UEFI load options
1152 * @cmdtp: Command table
1153 * @flag: Command flag
1154 * @argc: Number of arguments
1155 * @argv: Argument array
1156 * Return: CMD_RET_SUCCESS on success,
1157 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1159 * Implement efidebug "test" sub-command.
1161 static int do_efi_test(struct cmd_tbl *cmdtp, int flag,
1162 int argc, char * const argv[])
1167 return CMD_RET_USAGE;
1171 cp = find_cmd_tbl(argv[0], cmd_efidebug_test_sub,
1172 ARRAY_SIZE(cmd_efidebug_test_sub));
1174 return CMD_RET_USAGE;
1176 return cp->cmd(cmdtp, flag, argc, argv);
1180 * do_efi_query_info() - QueryVariableInfo EFI service
1182 * @cmdtp: Command table
1183 * @flag: Command flag
1184 * @argc: Number of arguments
1185 * @argv: Argument array
1186 * Return: CMD_RET_SUCCESS on success,
1187 * CMD_RET_USAGE or CMD_RET_FAILURE on failure
1189 * Implement efidebug "test" sub-command.
1192 static int do_efi_query_info(struct cmd_tbl *cmdtp, int flag,
1193 int argc, char * const argv[])
1197 u64 max_variable_storage_size;
1198 u64 remain_variable_storage_size;
1199 u64 max_variable_size;
1202 for (i = 1; i < argc; i++) {
1203 if (!strcmp(argv[i], "-bs"))
1204 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
1205 else if (!strcmp(argv[i], "-rt"))
1206 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
1207 else if (!strcmp(argv[i], "-nv"))
1208 attr |= EFI_VARIABLE_NON_VOLATILE;
1209 else if (!strcmp(argv[i], "-at"))
1211 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
1214 ret = EFI_CALL(efi_query_variable_info(attr,
1215 &max_variable_storage_size,
1216 &remain_variable_storage_size,
1217 &max_variable_size));
1218 if (ret != EFI_SUCCESS) {
1219 printf("Error: Cannot query UEFI variables, r = %lu\n",
1220 ret & ~EFI_ERROR_MASK);
1221 return CMD_RET_FAILURE;
1224 printf("Max storage size %llu\n", max_variable_storage_size);
1225 printf("Remaining storage size %llu\n", remain_variable_storage_size);
1226 printf("Max variable size %llu\n", max_variable_size);
1228 return CMD_RET_SUCCESS;
1231 static struct cmd_tbl cmd_efidebug_sub[] = {
1232 U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
1233 U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices,
1235 U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
1237 U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
1239 U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
1241 U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
1243 U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables,
1245 U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_efi_test,
1247 U_BOOT_CMD_MKENT(query, CONFIG_SYS_MAXARGS, 1, do_efi_query_info,
1252 * do_efidebug() - display and configure UEFI environment
1254 * @cmdtp: Command table
1255 * @flag: Command flag
1256 * @argc: Number of arguments
1257 * @argv: Argument array
1258 * Return: CMD_RET_SUCCESS on success,
1259 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1261 * Implement efidebug command which allows us to display and
1262 * configure UEFI environment.
1264 static int do_efidebug(struct cmd_tbl *cmdtp, int flag,
1265 int argc, char *const argv[])
1271 return CMD_RET_USAGE;
1275 /* Initialize UEFI drivers */
1276 r = efi_init_obj_list();
1277 if (r != EFI_SUCCESS) {
1278 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1279 r & ~EFI_ERROR_MASK);
1280 return CMD_RET_FAILURE;
1283 cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
1284 ARRAY_SIZE(cmd_efidebug_sub));
1286 return CMD_RET_USAGE;
1288 return cp->cmd(cmdtp, flag, argc, argv);
1291 #ifdef CONFIG_SYS_LONGHELP
1292 static char efidebug_help_text[] =
1293 " - UEFI Shell-like interface to configure UEFI environment\n"
1295 "efidebug boot add <bootid> <label> <interface> <devnum>[:<part>] <file path> [<load options>]\n"
1296 " - set UEFI BootXXXX variable\n"
1297 " <load options> will be passed to UEFI application\n"
1298 "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
1299 " - delete UEFI BootXXXX variables\n"
1300 "efidebug boot dump\n"
1301 " - dump all UEFI BootXXXX variables\n"
1302 "efidebug boot next <bootid>\n"
1303 " - set UEFI BootNext variable\n"
1304 "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
1305 " - set/show UEFI boot order\n"
1307 "efidebug devices\n"
1308 " - show UEFI devices\n"
1309 "efidebug drivers\n"
1310 " - show UEFI drivers\n"
1312 " - show UEFI handles\n"
1314 " - show loaded images\n"
1316 " - show UEFI memory map\n"
1318 " - show UEFI configuration tables\n"
1319 "efidebug test bootmgr\n"
1320 " - run simple bootmgr for test\n"
1321 "efidebug query [-nv][-bs][-rt][-at]\n"
1322 " - show size of UEFI variables store\n";
1326 efidebug, 10, 0, do_efidebug,
1327 "Configure UEFI environment",