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>
16 #include <linux/ctype.h>
18 #define BS systab.boottime
19 #define RT systab.runtime
22 * efi_get_device_handle_info() - get information of UEFI device
24 * @handle: Handle of UEFI device
25 * @dev_path_text: Pointer to text of device path
26 * Return: 0 on success, -1 on failure
28 * Currently return a formatted text of device path.
30 static int efi_get_device_handle_info(efi_handle_t handle, u16 **dev_path_text)
32 struct efi_device_path *dp;
35 ret = EFI_CALL(BS->open_protocol(handle, &efi_guid_device_path,
36 (void **)&dp, NULL /* FIXME */, NULL,
37 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
38 if (ret == EFI_SUCCESS) {
39 *dev_path_text = efi_dp_str(dp);
46 #define EFI_HANDLE_WIDTH ((int)sizeof(efi_handle_t) * 2)
48 static const char spc[] = " ";
49 static const char sep[] = "================";
52 * do_efi_show_devices() - show UEFI devices
54 * @cmdtp: Command table
56 * @argc: Number of arguments
57 * @argv: Argument array
58 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
60 * Implement efidebug "devices" sub-command.
61 * Show all UEFI devices and their information.
63 static int do_efi_show_devices(cmd_tbl_t *cmdtp, int flag,
64 int argc, char * const argv[])
66 efi_handle_t *handles;
71 ret = EFI_CALL(BS->locate_handle_buffer(ALL_HANDLES, NULL, NULL,
73 if (ret != EFI_SUCCESS)
74 return CMD_RET_FAILURE;
77 return CMD_RET_SUCCESS;
79 printf("Device%.*s Device Path\n", EFI_HANDLE_WIDTH - 6, spc);
80 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
81 for (i = 0; i < num; i++) {
82 if (!efi_get_device_handle_info(handles[i], &dev_path_text)) {
83 printf("%p %ls\n", handles[i], dev_path_text);
84 efi_free_pool(dev_path_text);
88 EFI_CALL(BS->free_pool(handles));
90 return CMD_RET_SUCCESS;
94 * efi_get_driver_handle_info() - get information of UEFI driver
96 * @handle: Handle of UEFI device
97 * @driver_name: Driver name
98 * @image_path: Pointer to text of device path
99 * Return: 0 on success, -1 on failure
101 * Currently return no useful information as all UEFI drivers are
104 static int efi_get_driver_handle_info(efi_handle_t handle, u16 **driver_name,
107 struct efi_handler *handler;
108 struct efi_loaded_image *image;
113 * TODO: support EFI_COMPONENT_NAME2_PROTOCOL
118 ret = efi_search_protocol(handle, &efi_guid_loaded_image, &handler);
119 if (ret != EFI_SUCCESS) {
124 image = handler->protocol_interface;
125 *image_path = efi_dp_str(image->file_path);
131 * do_efi_show_drivers() - show UEFI drivers
133 * @cmdtp: Command table
134 * @flag: Command flag
135 * @argc: Number of arguments
136 * @argv: Argument array
137 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
139 * Implement efidebug "drivers" sub-command.
140 * Show all UEFI drivers and their information.
142 static int do_efi_show_drivers(cmd_tbl_t *cmdtp, int flag,
143 int argc, char * const argv[])
145 efi_handle_t *handles;
147 u16 *driver_name, *image_path_text;
150 ret = EFI_CALL(BS->locate_handle_buffer(
151 BY_PROTOCOL, &efi_guid_driver_binding_protocol,
152 NULL, &num, &handles));
153 if (ret != EFI_SUCCESS)
154 return CMD_RET_FAILURE;
157 return CMD_RET_SUCCESS;
159 printf("Driver%.*s Name Image Path\n",
160 EFI_HANDLE_WIDTH - 6, spc);
161 printf("%.*s ==================== ====================\n",
162 EFI_HANDLE_WIDTH, sep);
163 for (i = 0; i < num; i++) {
164 if (!efi_get_driver_handle_info(handles[i], &driver_name,
167 printf("%p %-20ls %ls\n", handles[i],
168 driver_name, image_path_text);
170 printf("%p %-20ls <built-in>\n",
171 handles[i], driver_name);
172 EFI_CALL(BS->free_pool(driver_name));
173 EFI_CALL(BS->free_pool(image_path_text));
177 EFI_CALL(BS->free_pool(handles));
179 return CMD_RET_SUCCESS;
182 static const struct {
184 const efi_guid_t guid;
188 EFI_DEVICE_PATH_PROTOCOL_GUID,
191 "Device Path To Text",
192 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID,
195 "Device Path Utilities",
196 EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID,
199 "Unicode Collation 2",
200 EFI_UNICODE_COLLATION_PROTOCOL2_GUID,
204 EFI_DRIVER_BINDING_PROTOCOL_GUID,
208 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID,
211 "Simple Text Input Ex",
212 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID,
215 "Simple Text Output",
216 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID,
220 EFI_BLOCK_IO_PROTOCOL_GUID,
223 "Simple File System",
224 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
228 EFI_LOADED_IMAGE_PROTOCOL_GUID,
232 EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID,
236 EFI_HII_STRING_PROTOCOL_GUID,
240 EFI_HII_DATABASE_PROTOCOL_GUID,
243 "HII Config Routing",
244 EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID,
248 EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
252 EFI_PXE_BASE_CODE_PROTOCOL_GUID,
254 /* Configuration table GUIDs */
270 * get_guid_text - get string of GUID
272 * Return description of GUID.
275 * Return: description of GUID or NULL
277 static const char *get_guid_text(const void *guid)
281 for (i = 0; i < ARRAY_SIZE(guid_list); i++) {
283 * As guidcmp uses memcmp() we can safely accept unaligned
286 if (!guidcmp(&guid_list[i].guid, guid))
287 return guid_list[i].text;
294 * do_efi_show_handles() - show UEFI handles
296 * @cmdtp: Command table
297 * @flag: Command flag
298 * @argc: Number of arguments
299 * @argv: Argument array
300 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
302 * Implement efidebug "dh" sub-command.
303 * Show all UEFI handles and their information, currently all protocols
306 static int do_efi_show_handles(cmd_tbl_t *cmdtp, int flag,
307 int argc, char * const argv[])
309 efi_handle_t *handles;
311 efi_uintn_t num, count, i, j;
312 const char *guid_text;
315 ret = EFI_CALL(BS->locate_handle_buffer(ALL_HANDLES, NULL, NULL,
317 if (ret != EFI_SUCCESS)
318 return CMD_RET_FAILURE;
321 return CMD_RET_SUCCESS;
323 printf("Handle%.*s Protocols\n", EFI_HANDLE_WIDTH - 6, spc);
324 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
325 for (i = 0; i < num; i++) {
326 printf("%p", handles[i]);
327 ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid,
334 for (j = 0; j < count; j++) {
340 guid_text = get_guid_text(guid[j]);
344 printf("%pUl", guid[j]);
349 EFI_CALL(BS->free_pool(handles));
351 return CMD_RET_SUCCESS;
355 * do_efi_show_images() - show UEFI images
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 "images" sub-command.
364 * Show all UEFI loaded images and their information.
366 static int do_efi_show_images(cmd_tbl_t *cmdtp, int flag,
367 int argc, char * const argv[])
369 efi_print_image_infos(NULL);
371 return CMD_RET_SUCCESS;
374 static const char * const efi_mem_type_string[] = {
375 [EFI_RESERVED_MEMORY_TYPE] = "RESERVED",
376 [EFI_LOADER_CODE] = "LOADER CODE",
377 [EFI_LOADER_DATA] = "LOADER DATA",
378 [EFI_BOOT_SERVICES_CODE] = "BOOT CODE",
379 [EFI_BOOT_SERVICES_DATA] = "BOOT DATA",
380 [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE",
381 [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA",
382 [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL",
383 [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM",
384 [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM",
385 [EFI_ACPI_MEMORY_NVS] = "ACPI NVS",
386 [EFI_MMAP_IO] = "IO",
387 [EFI_MMAP_IO_PORT] = "IO PORT",
388 [EFI_PAL_CODE] = "PAL",
391 static const struct efi_mem_attrs {
394 } efi_mem_attrs[] = {
395 {EFI_MEMORY_UC, "UC"},
396 {EFI_MEMORY_UC, "UC"},
397 {EFI_MEMORY_WC, "WC"},
398 {EFI_MEMORY_WT, "WT"},
399 {EFI_MEMORY_WB, "WB"},
400 {EFI_MEMORY_UCE, "UCE"},
401 {EFI_MEMORY_WP, "WP"},
402 {EFI_MEMORY_RP, "RP"},
403 {EFI_MEMORY_XP, "WP"},
404 {EFI_MEMORY_NV, "NV"},
405 {EFI_MEMORY_MORE_RELIABLE, "REL"},
406 {EFI_MEMORY_RO, "RO"},
407 {EFI_MEMORY_RUNTIME, "RT"},
411 * print_memory_attributes() - print memory map attributes
413 * @attributes: Attribute value
415 * Print memory map attributes
417 static void print_memory_attributes(u64 attributes)
421 for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
422 if (attributes & efi_mem_attrs[i].bit) {
429 puts(efi_mem_attrs[i].text);
433 #define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
436 * do_efi_show_memmap() - show UEFI memory map
438 * @cmdtp: Command table
439 * @flag: Command flag
440 * @argc: Number of arguments
441 * @argv: Argument array
442 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
444 * Implement efidebug "memmap" sub-command.
445 * Show UEFI memory map.
447 static int do_efi_show_memmap(cmd_tbl_t *cmdtp, int flag,
448 int argc, char * const argv[])
450 struct efi_mem_desc *memmap = NULL, *map;
451 efi_uintn_t map_size = 0;
456 ret = EFI_CALL(BS->get_memory_map(&map_size, memmap, NULL, NULL, NULL));
457 if (ret == EFI_BUFFER_TOO_SMALL) {
458 map_size += sizeof(struct efi_mem_desc); /* for my own */
459 ret = EFI_CALL(BS->allocate_pool(EFI_LOADER_DATA,
460 map_size, (void *)&memmap));
461 if (ret != EFI_SUCCESS)
462 return CMD_RET_FAILURE;
463 ret = EFI_CALL(BS->get_memory_map(&map_size, memmap,
466 if (ret != EFI_SUCCESS) {
467 EFI_CALL(BS->free_pool(memmap));
468 return CMD_RET_FAILURE;
471 printf("Type Start%.*s End%.*s Attributes\n",
472 EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc);
473 printf("================ %.*s %.*s ==========\n",
474 EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep);
475 for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
476 if (map->type < EFI_MAX_MEMORY_TYPE)
477 type = efi_mem_type_string[map->type];
481 printf("%-16s %.*llx-%.*llx", type,
485 map->physical_start + map->num_pages * EFI_PAGE_SIZE);
487 print_memory_attributes(map->attribute);
491 EFI_CALL(BS->free_pool(memmap));
493 return CMD_RET_SUCCESS;
497 * do_efi_show_tables() - show UEFI configuration tables
499 * @cmdtp: Command table
500 * @flag: Command flag
501 * @argc: Number of arguments
502 * @argv: Argument array
503 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
505 * Implement efidebug "tables" sub-command.
506 * Show UEFI configuration tables.
508 static int do_efi_show_tables(cmd_tbl_t *cmdtp, int flag,
509 int argc, char * const argv[])
512 const char *guid_str;
514 for (i = 0; i < systab.nr_tables; ++i) {
515 guid_str = get_guid_text(&systab.tables[i].guid);
518 printf("%pUl %s\n", &systab.tables[i].guid, guid_str);
521 return CMD_RET_SUCCESS;
525 * do_efi_boot_add() - set UEFI load option
527 * @cmdtp: Command table
528 * @flag: Command flag
529 * @argc: Number of arguments
530 * @argv: Argument array
531 * Return: CMD_RET_SUCCESS on success,
532 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
534 * Implement efidebug "boot add" sub-command. Create or change UEFI load option.
536 * efidebug boot add <id> <label> <interface> <devnum>[:<part>] <file> <options>
538 static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag,
539 int argc, char * const argv[])
544 u16 var_name16[9], *p;
546 size_t label_len, label_len16;
548 struct efi_device_path *device_path = NULL, *file_path = NULL;
549 struct efi_load_option lo;
553 int r = CMD_RET_SUCCESS;
555 if (argc < 6 || argc > 7)
556 return CMD_RET_USAGE;
558 id = (int)simple_strtoul(argv[1], &endp, 16);
559 if (*endp != '\0' || id > 0xffff)
560 return CMD_RET_USAGE;
562 sprintf(var_name, "Boot%04X", id);
564 utf8_utf16_strncpy(&p, var_name, 9);
566 guid = efi_global_variable_guid;
569 lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
572 label_len = strlen(argv[2]);
573 label_len16 = utf8_utf16_strnlen(argv[2], label_len);
574 label = malloc((label_len16 + 1) * sizeof(u16));
576 return CMD_RET_FAILURE;
577 lo.label = label; /* label will be changed below */
578 utf8_utf16_strncpy(&label, argv[2], label_len);
581 ret = efi_dp_from_name(argv[3], argv[4], argv[5], &device_path,
583 if (ret != EFI_SUCCESS) {
584 printf("Cannot create device path for \"%s %s\"\n",
589 lo.file_path = file_path;
590 lo.file_path_length = efi_dp_size(file_path)
591 + sizeof(struct efi_device_path); /* for END */
595 lo.optional_data = NULL;
597 lo.optional_data = (const u8 *)argv[6];
599 size = efi_serialize_load_option(&lo, (u8 **)&data);
605 ret = EFI_CALL(RT->set_variable(var_name16, &guid,
606 EFI_VARIABLE_NON_VOLATILE |
607 EFI_VARIABLE_BOOTSERVICE_ACCESS |
608 EFI_VARIABLE_RUNTIME_ACCESS,
610 if (ret != EFI_SUCCESS) {
611 printf("Cannot set %ls\n", var_name16);
616 efi_free_pool(device_path);
617 efi_free_pool(file_path);
624 * do_efi_boot_rm() - delete UEFI load options
626 * @cmdtp: Command table
627 * @flag: Command flag
628 * @argc: Number of arguments
629 * @argv: Argument array
630 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
632 * Implement efidebug "boot rm" sub-command.
633 * Delete UEFI load options.
635 * efidebug boot rm <id> ...
637 static int do_efi_boot_rm(cmd_tbl_t *cmdtp, int flag,
638 int argc, char * const argv[])
648 return CMD_RET_USAGE;
650 guid = efi_global_variable_guid;
651 for (i = 1; i < argc; i++, argv++) {
652 id = (int)simple_strtoul(argv[1], &endp, 16);
653 if (*endp != '\0' || id > 0xffff)
654 return CMD_RET_FAILURE;
656 sprintf(var_name, "Boot%04X", id);
657 utf8_utf16_strncpy((u16 **)&var_name16, var_name, 9);
659 ret = EFI_CALL(RT->set_variable(var_name16, &guid, 0, 0, NULL));
661 printf("Cannot remove Boot%04X", id);
662 return CMD_RET_FAILURE;
666 return CMD_RET_SUCCESS;
670 * show_efi_boot_opt_data() - dump UEFI load option
672 * @id: load option number
673 * @data: value of UEFI load option variable
674 * @size: size of the boot option
676 * Decode the value of UEFI load option variable and print information.
678 static void show_efi_boot_opt_data(int id, void *data, size_t size)
680 struct efi_load_option lo;
682 size_t label_len16, label_len;
685 efi_deserialize_load_option(&lo, data);
687 label_len16 = u16_strlen(lo.label);
688 label_len = utf16_utf8_strnlen(lo.label, label_len16);
689 label = malloc(label_len + 1);
693 utf16_utf8_strncpy(&p, lo.label, label_len16);
695 printf("Boot%04X:\n", id);
696 printf(" attributes: %c%c%c (0x%08x)\n",
698 lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
699 /* FORCE RECONNECT */
700 lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
702 lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
704 printf(" label: %s\n", label);
706 dp_str = efi_dp_str(lo.file_path);
707 printf(" file_path: %ls\n", dp_str);
708 efi_free_pool(dp_str);
711 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
712 lo.optional_data, size + (u8 *)data -
713 (u8 *)lo.optional_data, true);
718 * show_efi_boot_opt() - dump UEFI load option
720 * @id: Load option number
722 * Dump information defined by UEFI load option.
724 static void show_efi_boot_opt(int id)
727 u16 var_name16[9], *p;
733 sprintf(var_name, "Boot%04X", id);
735 utf8_utf16_strncpy(&p, var_name, 9);
736 guid = efi_global_variable_guid;
739 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, NULL));
740 if (ret == EFI_BUFFER_TOO_SMALL) {
742 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
745 if (ret == EFI_SUCCESS)
746 show_efi_boot_opt_data(id, data, size);
747 else if (ret == EFI_NOT_FOUND)
748 printf("Boot%04X: not found\n", id);
753 static int u16_tohex(u16 c)
755 if (c >= '0' && c <= '9')
757 if (c >= 'A' && c <= 'F')
760 /* not hexadecimal */
765 * show_efi_boot_dump() - dump all UEFI load options
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 "boot dump" sub-command.
774 * Dump information of all UEFI load options defined.
778 static int do_efi_boot_dump(cmd_tbl_t *cmdtp, int flag,
779 int argc, char * const argv[])
782 efi_uintn_t buf_size, size;
788 return CMD_RET_USAGE;
791 var_name16 = malloc(buf_size);
793 return CMD_RET_FAILURE;
798 ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
800 if (ret == EFI_NOT_FOUND)
802 if (ret == EFI_BUFFER_TOO_SMALL) {
804 p = realloc(var_name16, buf_size);
807 return CMD_RET_FAILURE;
810 ret = EFI_CALL(efi_get_next_variable_name(&size,
814 if (ret != EFI_SUCCESS) {
816 return CMD_RET_FAILURE;
819 if (memcmp(var_name16, L"Boot", 8))
822 for (id = 0, i = 0; i < 4; i++) {
823 digit = u16_tohex(var_name16[4 + i]);
826 id = (id << 4) + digit;
828 if (i == 4 && !var_name16[8])
829 show_efi_boot_opt(id);
834 return CMD_RET_SUCCESS;
838 * show_efi_boot_order() - show order of UEFI load options
840 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
842 * Show order of UEFI load options defined by BootOrder variable.
844 static int show_efi_boot_order(void)
847 u16 *bootorder = NULL;
851 u16 var_name16[9], *p16;
853 struct efi_load_option lo;
855 size_t label_len16, label_len;
858 guid = efi_global_variable_guid;
860 ret = EFI_CALL(RT->get_variable(L"BootOrder", &guid, NULL, &size,
862 if (ret == EFI_BUFFER_TOO_SMALL) {
863 bootorder = malloc(size);
864 ret = EFI_CALL(RT->get_variable(L"BootOrder", &guid, NULL,
867 if (ret == EFI_NOT_FOUND) {
868 printf("BootOrder not defined\n");
869 ret = CMD_RET_SUCCESS;
871 } else if (ret != EFI_SUCCESS) {
872 ret = CMD_RET_FAILURE;
876 num = size / sizeof(u16);
877 for (i = 0; i < num; i++) {
878 sprintf(var_name, "Boot%04X", bootorder[i]);
880 utf8_utf16_strncpy(&p16, var_name, 9);
883 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
885 if (ret != EFI_BUFFER_TOO_SMALL) {
886 printf("%2d: Boot%04X: (not defined)\n",
887 i + 1, bootorder[i]);
893 ret = CMD_RET_FAILURE;
896 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
898 if (ret != EFI_SUCCESS) {
900 ret = CMD_RET_FAILURE;
904 efi_deserialize_load_option(&lo, data);
906 label_len16 = u16_strlen(lo.label);
907 label_len = utf16_utf8_strnlen(lo.label, label_len16);
908 label = malloc(label_len + 1);
911 ret = CMD_RET_FAILURE;
915 utf16_utf8_strncpy(&p, lo.label, label_len16);
916 printf("%2d: Boot%04X: %s\n", i + 1, bootorder[i], label);
928 * do_efi_boot_next() - manage UEFI BootNext variable
930 * @cmdtp: Command table
931 * @flag: Command flag
932 * @argc: Number of arguments
933 * @argv: Argument array
934 * Return: CMD_RET_SUCCESS on success,
935 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
937 * Implement efidebug "boot next" sub-command.
938 * Set BootNext variable.
940 * efidebug boot next <id>
942 static int do_efi_boot_next(cmd_tbl_t *cmdtp, int flag,
943 int argc, char * const argv[])
950 int r = CMD_RET_SUCCESS;
953 return CMD_RET_USAGE;
955 bootnext = (u16)simple_strtoul(argv[1], &endp, 16);
956 if (*endp != '\0' || bootnext > 0xffff) {
957 printf("invalid value: %s\n", argv[1]);
962 guid = efi_global_variable_guid;
964 ret = EFI_CALL(RT->set_variable(L"BootNext", &guid,
965 EFI_VARIABLE_NON_VOLATILE |
966 EFI_VARIABLE_BOOTSERVICE_ACCESS |
967 EFI_VARIABLE_RUNTIME_ACCESS,
969 if (ret != EFI_SUCCESS) {
970 printf("Cannot set BootNext\n");
978 * do_efi_boot_order() - manage UEFI BootOrder variable
980 * @cmdtp: Command table
981 * @flag: Command flag
982 * @argc: Number of arguments
983 * @argv: Argument array
984 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
986 * Implement efidebug "boot order" sub-command.
987 * Show order of UEFI load options, or change it in BootOrder variable.
989 * efidebug boot order [<id> ...]
991 static int do_efi_boot_order(cmd_tbl_t *cmdtp, int flag,
992 int argc, char * const argv[])
994 u16 *bootorder = NULL;
1000 int r = CMD_RET_SUCCESS;
1003 return show_efi_boot_order();
1008 size = argc * sizeof(u16);
1009 bootorder = malloc(size);
1011 return CMD_RET_FAILURE;
1013 for (i = 0; i < argc; i++) {
1014 id = (int)simple_strtoul(argv[i], &endp, 16);
1015 if (*endp != '\0' || id > 0xffff) {
1016 printf("invalid value: %s\n", argv[i]);
1017 r = CMD_RET_FAILURE;
1021 bootorder[i] = (u16)id;
1024 guid = efi_global_variable_guid;
1025 ret = EFI_CALL(RT->set_variable(L"BootOrder", &guid,
1026 EFI_VARIABLE_NON_VOLATILE |
1027 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1028 EFI_VARIABLE_RUNTIME_ACCESS,
1030 if (ret != EFI_SUCCESS) {
1031 printf("Cannot set BootOrder\n");
1032 r = CMD_RET_FAILURE;
1040 static cmd_tbl_t cmd_efidebug_boot_sub[] = {
1041 U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
1042 U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
1043 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
1044 U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
1045 U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
1050 * do_efi_boot_opt() - manage UEFI load options
1052 * @cmdtp: Command table
1053 * @flag: Command flag
1054 * @argc: Number of arguments
1055 * @argv: Argument array
1056 * Return: CMD_RET_SUCCESS on success,
1057 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1059 * Implement efidebug "boot" sub-command.
1061 static int do_efi_boot_opt(cmd_tbl_t *cmdtp, int flag,
1062 int argc, char * const argv[])
1067 return CMD_RET_USAGE;
1071 cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
1072 ARRAY_SIZE(cmd_efidebug_boot_sub));
1074 return CMD_RET_USAGE;
1076 return cp->cmd(cmdtp, flag, argc, argv);
1079 static cmd_tbl_t cmd_efidebug_sub[] = {
1080 U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
1081 U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices,
1083 U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
1085 U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
1087 U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
1089 U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
1091 U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables,
1096 * do_efidebug() - display and configure UEFI environment
1098 * @cmdtp: Command table
1099 * @flag: Command flag
1100 * @argc: Number of arguments
1101 * @argv: Argument array
1102 * Return: CMD_RET_SUCCESS on success,
1103 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1105 * Implement efidebug command which allows us to display and
1106 * configure UEFI environment.
1108 static int do_efidebug(cmd_tbl_t *cmdtp, int flag,
1109 int argc, char * const argv[])
1115 return CMD_RET_USAGE;
1119 /* Initialize UEFI drivers */
1120 r = efi_init_obj_list();
1121 if (r != EFI_SUCCESS) {
1122 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1123 r & ~EFI_ERROR_MASK);
1124 return CMD_RET_FAILURE;
1127 cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
1128 ARRAY_SIZE(cmd_efidebug_sub));
1130 return CMD_RET_USAGE;
1132 return cp->cmd(cmdtp, flag, argc, argv);
1135 #ifdef CONFIG_SYS_LONGHELP
1136 static char efidebug_help_text[] =
1137 " - UEFI Shell-like interface to configure UEFI environment\n"
1139 "efidebug boot add <bootid> <label> <interface> <devnum>[:<part>] <file path> [<load options>]\n"
1140 " - set UEFI BootXXXX variable\n"
1141 " <load options> will be passed to UEFI application\n"
1142 "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
1143 " - delete UEFI BootXXXX variables\n"
1144 "efidebug boot dump\n"
1145 " - dump all UEFI BootXXXX variables\n"
1146 "efidebug boot next <bootid>\n"
1147 " - set UEFI BootNext variable\n"
1148 "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
1149 " - set/show UEFI boot order\n"
1151 "efidebug devices\n"
1152 " - show uefi devices\n"
1153 "efidebug drivers\n"
1154 " - show uefi drivers\n"
1156 " - show uefi handles\n"
1158 " - show loaded images\n"
1160 " - show uefi memory map\n"
1162 " - show UEFI configuration tables\n";
1166 efidebug, 10, 0, do_efidebug,
1167 "Configure UEFI environment",