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>
17 #include <linux/ctype.h>
19 #define BS systab.boottime
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(efi_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_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(efi_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_free_pool(driver_name);
173 efi_free_pool(image_path_text);
177 efi_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_LOAD_FILE2_PROTOCOL_GUID,
252 EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
256 EFI_PXE_BASE_CODE_PROTOCOL_GUID,
258 /* Configuration table GUIDs */
272 "Runtime properties",
273 EFI_RT_PROPERTIES_TABLE_GUID,
278 * get_guid_text - get string of GUID
280 * Return description of GUID.
283 * Return: description of GUID or NULL
285 static const char *get_guid_text(const void *guid)
289 for (i = 0; i < ARRAY_SIZE(guid_list); i++) {
291 * As guidcmp uses memcmp() we can safely accept unaligned
294 if (!guidcmp(&guid_list[i].guid, guid))
295 return guid_list[i].text;
302 * do_efi_show_handles() - show UEFI handles
304 * @cmdtp: Command table
305 * @flag: Command flag
306 * @argc: Number of arguments
307 * @argv: Argument array
308 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
310 * Implement efidebug "dh" sub-command.
311 * Show all UEFI handles and their information, currently all protocols
314 static int do_efi_show_handles(cmd_tbl_t *cmdtp, int flag,
315 int argc, char * const argv[])
317 efi_handle_t *handles;
319 efi_uintn_t num, count, i, j;
320 const char *guid_text;
323 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
325 if (ret != EFI_SUCCESS)
326 return CMD_RET_FAILURE;
329 return CMD_RET_SUCCESS;
331 printf("Handle%.*s Protocols\n", EFI_HANDLE_WIDTH - 6, spc);
332 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
333 for (i = 0; i < num; i++) {
334 printf("%p", handles[i]);
335 ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid,
342 for (j = 0; j < count; j++) {
348 guid_text = get_guid_text(guid[j]);
352 printf("%pUl", guid[j]);
357 efi_free_pool(handles);
359 return CMD_RET_SUCCESS;
363 * do_efi_show_images() - show UEFI images
365 * @cmdtp: Command table
366 * @flag: Command flag
367 * @argc: Number of arguments
368 * @argv: Argument array
369 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
371 * Implement efidebug "images" sub-command.
372 * Show all UEFI loaded images and their information.
374 static int do_efi_show_images(cmd_tbl_t *cmdtp, int flag,
375 int argc, char * const argv[])
377 efi_print_image_infos(NULL);
379 return CMD_RET_SUCCESS;
382 static const char * const efi_mem_type_string[] = {
383 [EFI_RESERVED_MEMORY_TYPE] = "RESERVED",
384 [EFI_LOADER_CODE] = "LOADER CODE",
385 [EFI_LOADER_DATA] = "LOADER DATA",
386 [EFI_BOOT_SERVICES_CODE] = "BOOT CODE",
387 [EFI_BOOT_SERVICES_DATA] = "BOOT DATA",
388 [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE",
389 [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA",
390 [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL",
391 [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM",
392 [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM",
393 [EFI_ACPI_MEMORY_NVS] = "ACPI NVS",
394 [EFI_MMAP_IO] = "IO",
395 [EFI_MMAP_IO_PORT] = "IO PORT",
396 [EFI_PAL_CODE] = "PAL",
397 [EFI_PERSISTENT_MEMORY_TYPE] = "PERSISTENT",
400 static const struct efi_mem_attrs {
403 } efi_mem_attrs[] = {
404 {EFI_MEMORY_UC, "UC"},
405 {EFI_MEMORY_UC, "UC"},
406 {EFI_MEMORY_WC, "WC"},
407 {EFI_MEMORY_WT, "WT"},
408 {EFI_MEMORY_WB, "WB"},
409 {EFI_MEMORY_UCE, "UCE"},
410 {EFI_MEMORY_WP, "WP"},
411 {EFI_MEMORY_RP, "RP"},
412 {EFI_MEMORY_XP, "WP"},
413 {EFI_MEMORY_NV, "NV"},
414 {EFI_MEMORY_MORE_RELIABLE, "REL"},
415 {EFI_MEMORY_RO, "RO"},
416 {EFI_MEMORY_RUNTIME, "RT"},
420 * print_memory_attributes() - print memory map attributes
422 * @attributes: Attribute value
424 * Print memory map attributes
426 static void print_memory_attributes(u64 attributes)
430 for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
431 if (attributes & efi_mem_attrs[i].bit) {
438 puts(efi_mem_attrs[i].text);
442 #define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
445 * do_efi_show_memmap() - show UEFI memory map
447 * @cmdtp: Command table
448 * @flag: Command flag
449 * @argc: Number of arguments
450 * @argv: Argument array
451 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
453 * Implement efidebug "memmap" sub-command.
454 * Show UEFI memory map.
456 static int do_efi_show_memmap(cmd_tbl_t *cmdtp, int flag,
457 int argc, char * const argv[])
459 struct efi_mem_desc *memmap = NULL, *map;
460 efi_uintn_t map_size = 0;
465 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
466 if (ret == EFI_BUFFER_TOO_SMALL) {
467 map_size += sizeof(struct efi_mem_desc); /* for my own */
468 ret = efi_allocate_pool(EFI_LOADER_DATA, map_size,
470 if (ret != EFI_SUCCESS)
471 return CMD_RET_FAILURE;
472 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
474 if (ret != EFI_SUCCESS) {
475 efi_free_pool(memmap);
476 return CMD_RET_FAILURE;
479 printf("Type Start%.*s End%.*s Attributes\n",
480 EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc);
481 printf("================ %.*s %.*s ==========\n",
482 EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep);
484 * Coverity check: dereferencing null pointer "map."
485 * This is a false positive as memmap will always be
486 * populated by allocate_pool() above.
488 for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
489 if (map->type < ARRAY_SIZE(efi_mem_type_string))
490 type = efi_mem_type_string[map->type];
494 printf("%-16s %.*llx-%.*llx", type,
496 (u64)map_to_sysmem((void *)(uintptr_t)
497 map->physical_start),
499 (u64)map_to_sysmem((void *)(uintptr_t)
500 (map->physical_start +
501 map->num_pages * EFI_PAGE_SIZE)));
503 print_memory_attributes(map->attribute);
507 efi_free_pool(memmap);
509 return CMD_RET_SUCCESS;
513 * do_efi_show_tables() - show UEFI configuration tables
515 * @cmdtp: Command table
516 * @flag: Command flag
517 * @argc: Number of arguments
518 * @argv: Argument array
519 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
521 * Implement efidebug "tables" sub-command.
522 * Show UEFI configuration tables.
524 static int do_efi_show_tables(cmd_tbl_t *cmdtp, int flag,
525 int argc, char * const argv[])
528 const char *guid_str;
530 for (i = 0; i < systab.nr_tables; ++i) {
531 guid_str = get_guid_text(&systab.tables[i].guid);
534 printf("%pUl %s\n", &systab.tables[i].guid, guid_str);
537 return CMD_RET_SUCCESS;
541 * do_efi_boot_add() - set UEFI load option
543 * @cmdtp: Command table
544 * @flag: Command flag
545 * @argc: Number of arguments
546 * @argv: Argument array
547 * Return: CMD_RET_SUCCESS on success,
548 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
550 * Implement efidebug "boot add" sub-command. Create or change UEFI load option.
552 * efidebug boot add <id> <label> <interface> <devnum>[:<part>] <file> <options>
554 static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag,
555 int argc, char * const argv[])
560 u16 var_name16[9], *p;
562 size_t label_len, label_len16;
564 struct efi_device_path *device_path = NULL, *file_path = NULL;
565 struct efi_load_option lo;
569 int r = CMD_RET_SUCCESS;
571 if (argc < 6 || argc > 7)
572 return CMD_RET_USAGE;
574 id = (int)simple_strtoul(argv[1], &endp, 16);
575 if (*endp != '\0' || id > 0xffff)
576 return CMD_RET_USAGE;
578 sprintf(var_name, "Boot%04X", id);
580 utf8_utf16_strncpy(&p, var_name, 9);
582 guid = efi_global_variable_guid;
585 lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
588 label_len = strlen(argv[2]);
589 label_len16 = utf8_utf16_strnlen(argv[2], label_len);
590 label = malloc((label_len16 + 1) * sizeof(u16));
592 return CMD_RET_FAILURE;
593 lo.label = label; /* label will be changed below */
594 utf8_utf16_strncpy(&label, argv[2], label_len);
597 ret = efi_dp_from_name(argv[3], argv[4], argv[5], &device_path,
599 if (ret != EFI_SUCCESS) {
600 printf("Cannot create device path for \"%s %s\"\n",
605 lo.file_path = file_path;
606 lo.file_path_length = efi_dp_size(file_path)
607 + sizeof(struct efi_device_path); /* for END */
611 lo.optional_data = NULL;
613 lo.optional_data = (const u8 *)argv[6];
615 size = efi_serialize_load_option(&lo, (u8 **)&data);
621 ret = EFI_CALL(efi_set_variable(var_name16, &guid,
622 EFI_VARIABLE_NON_VOLATILE |
623 EFI_VARIABLE_BOOTSERVICE_ACCESS |
624 EFI_VARIABLE_RUNTIME_ACCESS,
626 if (ret != EFI_SUCCESS) {
627 printf("Cannot set %ls\n", var_name16);
632 efi_free_pool(device_path);
633 efi_free_pool(file_path);
640 * do_efi_boot_rm() - delete UEFI load options
642 * @cmdtp: Command table
643 * @flag: Command flag
644 * @argc: Number of arguments
645 * @argv: Argument array
646 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
648 * Implement efidebug "boot rm" sub-command.
649 * Delete UEFI load options.
651 * efidebug boot rm <id> ...
653 static int do_efi_boot_rm(cmd_tbl_t *cmdtp, int flag,
654 int argc, char * const argv[])
660 u16 var_name16[9], *p;
664 return CMD_RET_USAGE;
666 guid = efi_global_variable_guid;
667 for (i = 1; i < argc; i++, argv++) {
668 id = (int)simple_strtoul(argv[1], &endp, 16);
669 if (*endp != '\0' || id > 0xffff)
670 return CMD_RET_FAILURE;
672 sprintf(var_name, "Boot%04X", id);
674 utf8_utf16_strncpy(&p, var_name, 9);
676 ret = EFI_CALL(efi_set_variable(var_name16, &guid, 0, 0, NULL));
678 printf("Cannot remove %ls\n", var_name16);
679 return CMD_RET_FAILURE;
683 return CMD_RET_SUCCESS;
687 * show_efi_boot_opt_data() - dump UEFI load option
689 * @varname16: variable name
690 * @data: value of UEFI load option variable
691 * @size: size of the boot option
693 * Decode the value of UEFI load option variable and print information.
695 static void show_efi_boot_opt_data(u16 *varname16, void *data, size_t size)
697 struct efi_load_option lo;
699 size_t label_len16, label_len;
702 efi_deserialize_load_option(&lo, data);
704 label_len16 = u16_strlen(lo.label);
705 label_len = utf16_utf8_strnlen(lo.label, label_len16);
706 label = malloc(label_len + 1);
710 utf16_utf8_strncpy(&p, lo.label, label_len16);
712 printf("%ls:\nattributes: %c%c%c (0x%08x)\n",
715 lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
716 /* FORCE RECONNECT */
717 lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
719 lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
721 printf(" label: %s\n", label);
723 dp_str = efi_dp_str(lo.file_path);
724 printf(" file_path: %ls\n", dp_str);
725 efi_free_pool(dp_str);
728 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
729 lo.optional_data, size + (u8 *)data -
730 (u8 *)lo.optional_data, true);
735 * show_efi_boot_opt() - dump UEFI load option
737 * @varname16: variable name
739 * Dump information defined by UEFI load option.
741 static void show_efi_boot_opt(u16 *varname16)
748 ret = EFI_CALL(efi_get_variable(varname16, &efi_global_variable_guid,
750 if (ret == EFI_BUFFER_TOO_SMALL) {
753 printf("ERROR: Out of memory\n");
756 ret = EFI_CALL(efi_get_variable(varname16,
757 &efi_global_variable_guid,
759 if (ret == EFI_SUCCESS)
760 show_efi_boot_opt_data(varname16, data, size);
765 static int u16_tohex(u16 c)
767 if (c >= '0' && c <= '9')
769 if (c >= 'A' && c <= 'F')
772 /* not hexadecimal */
777 * show_efi_boot_dump() - dump all UEFI load options
779 * @cmdtp: Command table
780 * @flag: Command flag
781 * @argc: Number of arguments
782 * @argv: Argument array
783 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
785 * Implement efidebug "boot dump" sub-command.
786 * Dump information of all UEFI load options defined.
790 static int do_efi_boot_dump(cmd_tbl_t *cmdtp, int flag,
791 int argc, char * const argv[])
794 efi_uintn_t buf_size, size;
800 return CMD_RET_USAGE;
803 var_name16 = malloc(buf_size);
805 return CMD_RET_FAILURE;
810 ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
812 if (ret == EFI_NOT_FOUND)
814 if (ret == EFI_BUFFER_TOO_SMALL) {
816 p = realloc(var_name16, buf_size);
819 return CMD_RET_FAILURE;
822 ret = EFI_CALL(efi_get_next_variable_name(&size,
826 if (ret != EFI_SUCCESS) {
828 return CMD_RET_FAILURE;
831 if (memcmp(var_name16, L"Boot", 8))
834 for (id = 0, i = 0; i < 4; i++) {
835 digit = u16_tohex(var_name16[4 + i]);
838 id = (id << 4) + digit;
840 if (i == 4 && !var_name16[8])
841 show_efi_boot_opt(var_name16);
846 return CMD_RET_SUCCESS;
850 * show_efi_boot_order() - show order of UEFI load options
852 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
854 * Show order of UEFI load options defined by BootOrder variable.
856 static int show_efi_boot_order(void)
862 u16 var_name16[9], *p16;
864 struct efi_load_option lo;
866 size_t label_len16, label_len;
870 ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid,
872 if (ret != EFI_BUFFER_TOO_SMALL) {
873 if (ret == EFI_NOT_FOUND) {
874 printf("BootOrder not defined\n");
875 return CMD_RET_SUCCESS;
877 return CMD_RET_FAILURE;
880 bootorder = malloc(size);
882 printf("ERROR: Out of memory\n");
883 return CMD_RET_FAILURE;
885 ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid,
886 NULL, &size, bootorder));
887 if (ret != EFI_SUCCESS) {
888 ret = CMD_RET_FAILURE;
892 num = size / sizeof(u16);
893 for (i = 0; i < num; i++) {
894 sprintf(var_name, "Boot%04X", bootorder[i]);
896 utf8_utf16_strncpy(&p16, var_name, 9);
899 ret = EFI_CALL(efi_get_variable(var_name16,
900 &efi_global_variable_guid, NULL,
902 if (ret != EFI_BUFFER_TOO_SMALL) {
903 printf("%2d: %s: (not defined)\n", i + 1, var_name);
909 ret = CMD_RET_FAILURE;
912 ret = EFI_CALL(efi_get_variable(var_name16,
913 &efi_global_variable_guid, NULL,
915 if (ret != EFI_SUCCESS) {
917 ret = CMD_RET_FAILURE;
921 efi_deserialize_load_option(&lo, data);
923 label_len16 = u16_strlen(lo.label);
924 label_len = utf16_utf8_strnlen(lo.label, label_len16);
925 label = malloc(label_len + 1);
928 ret = CMD_RET_FAILURE;
932 utf16_utf8_strncpy(&p, lo.label, label_len16);
933 printf("%2d: %s: %s\n", i + 1, var_name, label);
945 * do_efi_boot_next() - manage UEFI BootNext variable
947 * @cmdtp: Command table
948 * @flag: Command flag
949 * @argc: Number of arguments
950 * @argv: Argument array
951 * Return: CMD_RET_SUCCESS on success,
952 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
954 * Implement efidebug "boot next" sub-command.
955 * Set BootNext variable.
957 * efidebug boot next <id>
959 static int do_efi_boot_next(cmd_tbl_t *cmdtp, int flag,
960 int argc, char * const argv[])
967 int r = CMD_RET_SUCCESS;
970 return CMD_RET_USAGE;
972 bootnext = (u16)simple_strtoul(argv[1], &endp, 16);
974 printf("invalid value: %s\n", argv[1]);
979 guid = efi_global_variable_guid;
981 ret = EFI_CALL(efi_set_variable(L"BootNext", &guid,
982 EFI_VARIABLE_NON_VOLATILE |
983 EFI_VARIABLE_BOOTSERVICE_ACCESS |
984 EFI_VARIABLE_RUNTIME_ACCESS,
986 if (ret != EFI_SUCCESS) {
987 printf("Cannot set BootNext\n");
995 * do_efi_boot_order() - manage UEFI BootOrder variable
997 * @cmdtp: Command table
998 * @flag: Command flag
999 * @argc: Number of arguments
1000 * @argv: Argument array
1001 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1003 * Implement efidebug "boot order" sub-command.
1004 * Show order of UEFI load options, or change it in BootOrder variable.
1006 * efidebug boot order [<id> ...]
1008 static int do_efi_boot_order(cmd_tbl_t *cmdtp, int flag,
1009 int argc, char * const argv[])
1011 u16 *bootorder = NULL;
1017 int r = CMD_RET_SUCCESS;
1020 return show_efi_boot_order();
1025 size = argc * sizeof(u16);
1026 bootorder = malloc(size);
1028 return CMD_RET_FAILURE;
1030 for (i = 0; i < argc; i++) {
1031 id = (int)simple_strtoul(argv[i], &endp, 16);
1032 if (*endp != '\0' || id > 0xffff) {
1033 printf("invalid value: %s\n", argv[i]);
1034 r = CMD_RET_FAILURE;
1038 bootorder[i] = (u16)id;
1041 guid = efi_global_variable_guid;
1042 ret = EFI_CALL(efi_set_variable(L"BootOrder", &guid,
1043 EFI_VARIABLE_NON_VOLATILE |
1044 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1045 EFI_VARIABLE_RUNTIME_ACCESS,
1047 if (ret != EFI_SUCCESS) {
1048 printf("Cannot set BootOrder\n");
1049 r = CMD_RET_FAILURE;
1057 static cmd_tbl_t cmd_efidebug_boot_sub[] = {
1058 U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
1059 U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
1060 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
1061 U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
1062 U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
1067 * do_efi_boot_opt() - manage UEFI load options
1069 * @cmdtp: Command table
1070 * @flag: Command flag
1071 * @argc: Number of arguments
1072 * @argv: Argument array
1073 * Return: CMD_RET_SUCCESS on success,
1074 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1076 * Implement efidebug "boot" sub-command.
1078 static int do_efi_boot_opt(cmd_tbl_t *cmdtp, int flag,
1079 int argc, char * const argv[])
1084 return CMD_RET_USAGE;
1088 cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
1089 ARRAY_SIZE(cmd_efidebug_boot_sub));
1091 return CMD_RET_USAGE;
1093 return cp->cmd(cmdtp, flag, argc, argv);
1097 * do_efi_test_bootmgr() - run simple bootmgr for test
1099 * @cmdtp: Command table
1100 * @flag: Command flag
1101 * @argc: Number of arguments
1102 * @argv: Argument array
1103 * Return: CMD_RET_SUCCESS on success,
1104 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1106 * Implement efidebug "test bootmgr" sub-command.
1107 * Run simple bootmgr for test.
1109 * efidebug test bootmgr
1111 static int do_efi_test_bootmgr(cmd_tbl_t *cmdtp, int flag,
1112 int argc, char * const argv[])
1115 efi_uintn_t exit_data_size = 0;
1116 u16 *exit_data = NULL;
1119 ret = efi_bootmgr_load(&image);
1120 printf("efi_bootmgr_load() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1122 /* We call efi_start_image() even if error for test purpose. */
1123 ret = EFI_CALL(efi_start_image(image, &exit_data_size, &exit_data));
1124 printf("efi_start_image() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1125 if (ret && exit_data)
1126 efi_free_pool(exit_data);
1130 return CMD_RET_SUCCESS;
1133 static cmd_tbl_t cmd_efidebug_test_sub[] = {
1134 U_BOOT_CMD_MKENT(bootmgr, CONFIG_SYS_MAXARGS, 1, do_efi_test_bootmgr,
1139 * do_efi_test() - manage UEFI load options
1141 * @cmdtp: Command table
1142 * @flag: Command flag
1143 * @argc: Number of arguments
1144 * @argv: Argument array
1145 * Return: CMD_RET_SUCCESS on success,
1146 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1148 * Implement efidebug "test" sub-command.
1150 static int do_efi_test(cmd_tbl_t *cmdtp, int flag,
1151 int argc, char * const argv[])
1156 return CMD_RET_USAGE;
1160 cp = find_cmd_tbl(argv[0], cmd_efidebug_test_sub,
1161 ARRAY_SIZE(cmd_efidebug_test_sub));
1163 return CMD_RET_USAGE;
1165 return cp->cmd(cmdtp, flag, argc, argv);
1168 static cmd_tbl_t cmd_efidebug_sub[] = {
1169 U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
1170 U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices,
1172 U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
1174 U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
1176 U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
1178 U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
1180 U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables,
1182 U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_efi_test,
1187 * do_efidebug() - display and configure UEFI environment
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 command which allows us to display and
1197 * configure UEFI environment.
1199 static int do_efidebug(cmd_tbl_t *cmdtp, int flag,
1200 int argc, char * const argv[])
1206 return CMD_RET_USAGE;
1210 /* Initialize UEFI drivers */
1211 r = efi_init_obj_list();
1212 if (r != EFI_SUCCESS) {
1213 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1214 r & ~EFI_ERROR_MASK);
1215 return CMD_RET_FAILURE;
1218 cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
1219 ARRAY_SIZE(cmd_efidebug_sub));
1221 return CMD_RET_USAGE;
1223 return cp->cmd(cmdtp, flag, argc, argv);
1226 #ifdef CONFIG_SYS_LONGHELP
1227 static char efidebug_help_text[] =
1228 " - UEFI Shell-like interface to configure UEFI environment\n"
1230 "efidebug boot add <bootid> <label> <interface> <devnum>[:<part>] <file path> [<load options>]\n"
1231 " - set UEFI BootXXXX variable\n"
1232 " <load options> will be passed to UEFI application\n"
1233 "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
1234 " - delete UEFI BootXXXX variables\n"
1235 "efidebug boot dump\n"
1236 " - dump all UEFI BootXXXX variables\n"
1237 "efidebug boot next <bootid>\n"
1238 " - set UEFI BootNext variable\n"
1239 "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
1240 " - set/show UEFI boot order\n"
1242 "efidebug devices\n"
1243 " - show UEFI devices\n"
1244 "efidebug drivers\n"
1245 " - show UEFI drivers\n"
1247 " - show UEFI handles\n"
1249 " - show loaded images\n"
1251 " - show UEFI memory map\n"
1253 " - show UEFI configuration tables\n"
1254 "efidebug test bootmgr\n"
1255 " - run simple bootmgr for test\n";
1259 efidebug, 10, 0, do_efidebug,
1260 "Configure UEFI environment",