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_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(BS->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_CALL(BS->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",
399 static const struct efi_mem_attrs {
402 } efi_mem_attrs[] = {
403 {EFI_MEMORY_UC, "UC"},
404 {EFI_MEMORY_UC, "UC"},
405 {EFI_MEMORY_WC, "WC"},
406 {EFI_MEMORY_WT, "WT"},
407 {EFI_MEMORY_WB, "WB"},
408 {EFI_MEMORY_UCE, "UCE"},
409 {EFI_MEMORY_WP, "WP"},
410 {EFI_MEMORY_RP, "RP"},
411 {EFI_MEMORY_XP, "WP"},
412 {EFI_MEMORY_NV, "NV"},
413 {EFI_MEMORY_MORE_RELIABLE, "REL"},
414 {EFI_MEMORY_RO, "RO"},
415 {EFI_MEMORY_RUNTIME, "RT"},
419 * print_memory_attributes() - print memory map attributes
421 * @attributes: Attribute value
423 * Print memory map attributes
425 static void print_memory_attributes(u64 attributes)
429 for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
430 if (attributes & efi_mem_attrs[i].bit) {
437 puts(efi_mem_attrs[i].text);
441 #define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
444 * do_efi_show_memmap() - show UEFI memory map
446 * @cmdtp: Command table
447 * @flag: Command flag
448 * @argc: Number of arguments
449 * @argv: Argument array
450 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
452 * Implement efidebug "memmap" sub-command.
453 * Show UEFI memory map.
455 static int do_efi_show_memmap(cmd_tbl_t *cmdtp, int flag,
456 int argc, char * const argv[])
458 struct efi_mem_desc *memmap = NULL, *map;
459 efi_uintn_t map_size = 0;
464 ret = EFI_CALL(BS->get_memory_map(&map_size, memmap, NULL, NULL, NULL));
465 if (ret == EFI_BUFFER_TOO_SMALL) {
466 map_size += sizeof(struct efi_mem_desc); /* for my own */
467 ret = EFI_CALL(BS->allocate_pool(EFI_LOADER_DATA,
468 map_size, (void *)&memmap));
469 if (ret != EFI_SUCCESS)
470 return CMD_RET_FAILURE;
471 ret = EFI_CALL(BS->get_memory_map(&map_size, memmap,
474 if (ret != EFI_SUCCESS) {
475 EFI_CALL(BS->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);
483 for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
484 if (map->type < EFI_MAX_MEMORY_TYPE)
485 type = efi_mem_type_string[map->type];
489 printf("%-16s %.*llx-%.*llx", type,
493 map->physical_start + map->num_pages * EFI_PAGE_SIZE);
495 print_memory_attributes(map->attribute);
499 EFI_CALL(BS->free_pool(memmap));
501 return CMD_RET_SUCCESS;
505 * do_efi_show_tables() - show UEFI configuration tables
507 * @cmdtp: Command table
508 * @flag: Command flag
509 * @argc: Number of arguments
510 * @argv: Argument array
511 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
513 * Implement efidebug "tables" sub-command.
514 * Show UEFI configuration tables.
516 static int do_efi_show_tables(cmd_tbl_t *cmdtp, int flag,
517 int argc, char * const argv[])
520 const char *guid_str;
522 for (i = 0; i < systab.nr_tables; ++i) {
523 guid_str = get_guid_text(&systab.tables[i].guid);
526 printf("%pUl %s\n", &systab.tables[i].guid, guid_str);
529 return CMD_RET_SUCCESS;
533 * do_efi_boot_add() - set UEFI load option
535 * @cmdtp: Command table
536 * @flag: Command flag
537 * @argc: Number of arguments
538 * @argv: Argument array
539 * Return: CMD_RET_SUCCESS on success,
540 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
542 * Implement efidebug "boot add" sub-command. Create or change UEFI load option.
544 * efidebug boot add <id> <label> <interface> <devnum>[:<part>] <file> <options>
546 static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag,
547 int argc, char * const argv[])
552 u16 var_name16[9], *p;
554 size_t label_len, label_len16;
556 struct efi_device_path *device_path = NULL, *file_path = NULL;
557 struct efi_load_option lo;
561 int r = CMD_RET_SUCCESS;
563 if (argc < 6 || argc > 7)
564 return CMD_RET_USAGE;
566 id = (int)simple_strtoul(argv[1], &endp, 16);
567 if (*endp != '\0' || id > 0xffff)
568 return CMD_RET_USAGE;
570 sprintf(var_name, "Boot%04X", id);
572 utf8_utf16_strncpy(&p, var_name, 9);
574 guid = efi_global_variable_guid;
577 lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
580 label_len = strlen(argv[2]);
581 label_len16 = utf8_utf16_strnlen(argv[2], label_len);
582 label = malloc((label_len16 + 1) * sizeof(u16));
584 return CMD_RET_FAILURE;
585 lo.label = label; /* label will be changed below */
586 utf8_utf16_strncpy(&label, argv[2], label_len);
589 ret = efi_dp_from_name(argv[3], argv[4], argv[5], &device_path,
591 if (ret != EFI_SUCCESS) {
592 printf("Cannot create device path for \"%s %s\"\n",
597 lo.file_path = file_path;
598 lo.file_path_length = efi_dp_size(file_path)
599 + sizeof(struct efi_device_path); /* for END */
603 lo.optional_data = NULL;
605 lo.optional_data = (const u8 *)argv[6];
607 size = efi_serialize_load_option(&lo, (u8 **)&data);
613 ret = EFI_CALL(RT->set_variable(var_name16, &guid,
614 EFI_VARIABLE_NON_VOLATILE |
615 EFI_VARIABLE_BOOTSERVICE_ACCESS |
616 EFI_VARIABLE_RUNTIME_ACCESS,
618 if (ret != EFI_SUCCESS) {
619 printf("Cannot set %ls\n", var_name16);
624 efi_free_pool(device_path);
625 efi_free_pool(file_path);
632 * do_efi_boot_rm() - delete UEFI load options
634 * @cmdtp: Command table
635 * @flag: Command flag
636 * @argc: Number of arguments
637 * @argv: Argument array
638 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
640 * Implement efidebug "boot rm" sub-command.
641 * Delete UEFI load options.
643 * efidebug boot rm <id> ...
645 static int do_efi_boot_rm(cmd_tbl_t *cmdtp, int flag,
646 int argc, char * const argv[])
652 u16 var_name16[9], *p;
656 return CMD_RET_USAGE;
658 guid = efi_global_variable_guid;
659 for (i = 1; i < argc; i++, argv++) {
660 id = (int)simple_strtoul(argv[1], &endp, 16);
661 if (*endp != '\0' || id > 0xffff)
662 return CMD_RET_FAILURE;
664 sprintf(var_name, "Boot%04X", id);
666 utf8_utf16_strncpy(&p, var_name, 9);
668 ret = EFI_CALL(RT->set_variable(var_name16, &guid, 0, 0, NULL));
670 printf("Cannot remove %ls\n", var_name16);
671 return CMD_RET_FAILURE;
675 return CMD_RET_SUCCESS;
679 * show_efi_boot_opt_data() - dump UEFI load option
681 * @id: load option number
682 * @data: value of UEFI load option variable
683 * @size: size of the boot option
685 * Decode the value of UEFI load option variable and print information.
687 static void show_efi_boot_opt_data(int id, void *data, size_t size)
689 struct efi_load_option lo;
691 size_t label_len16, label_len;
694 efi_deserialize_load_option(&lo, data);
696 label_len16 = u16_strlen(lo.label);
697 label_len = utf16_utf8_strnlen(lo.label, label_len16);
698 label = malloc(label_len + 1);
702 utf16_utf8_strncpy(&p, lo.label, label_len16);
704 printf("Boot%04X:\n", id);
705 printf(" attributes: %c%c%c (0x%08x)\n",
707 lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
708 /* FORCE RECONNECT */
709 lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
711 lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
713 printf(" label: %s\n", label);
715 dp_str = efi_dp_str(lo.file_path);
716 printf(" file_path: %ls\n", dp_str);
717 efi_free_pool(dp_str);
720 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
721 lo.optional_data, size + (u8 *)data -
722 (u8 *)lo.optional_data, true);
727 * show_efi_boot_opt() - dump UEFI load option
729 * @id: Load option number
731 * Dump information defined by UEFI load option.
733 static void show_efi_boot_opt(int id)
736 u16 var_name16[9], *p;
742 sprintf(var_name, "Boot%04X", id);
744 utf8_utf16_strncpy(&p, var_name, 9);
745 guid = efi_global_variable_guid;
748 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, NULL));
749 if (ret == EFI_BUFFER_TOO_SMALL) {
751 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
754 if (ret == EFI_SUCCESS)
755 show_efi_boot_opt_data(id, data, size);
756 else if (ret == EFI_NOT_FOUND)
757 printf("Boot%04X: not found\n", id);
762 static int u16_tohex(u16 c)
764 if (c >= '0' && c <= '9')
766 if (c >= 'A' && c <= 'F')
769 /* not hexadecimal */
774 * show_efi_boot_dump() - dump all UEFI load options
776 * @cmdtp: Command table
777 * @flag: Command flag
778 * @argc: Number of arguments
779 * @argv: Argument array
780 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
782 * Implement efidebug "boot dump" sub-command.
783 * Dump information of all UEFI load options defined.
787 static int do_efi_boot_dump(cmd_tbl_t *cmdtp, int flag,
788 int argc, char * const argv[])
791 efi_uintn_t buf_size, size;
797 return CMD_RET_USAGE;
800 var_name16 = malloc(buf_size);
802 return CMD_RET_FAILURE;
807 ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
809 if (ret == EFI_NOT_FOUND)
811 if (ret == EFI_BUFFER_TOO_SMALL) {
813 p = realloc(var_name16, buf_size);
816 return CMD_RET_FAILURE;
819 ret = EFI_CALL(efi_get_next_variable_name(&size,
823 if (ret != EFI_SUCCESS) {
825 return CMD_RET_FAILURE;
828 if (memcmp(var_name16, L"Boot", 8))
831 for (id = 0, i = 0; i < 4; i++) {
832 digit = u16_tohex(var_name16[4 + i]);
835 id = (id << 4) + digit;
837 if (i == 4 && !var_name16[8])
838 show_efi_boot_opt(id);
843 return CMD_RET_SUCCESS;
847 * show_efi_boot_order() - show order of UEFI load options
849 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
851 * Show order of UEFI load options defined by BootOrder variable.
853 static int show_efi_boot_order(void)
856 u16 *bootorder = NULL;
860 u16 var_name16[9], *p16;
862 struct efi_load_option lo;
864 size_t label_len16, label_len;
867 guid = efi_global_variable_guid;
869 ret = EFI_CALL(RT->get_variable(L"BootOrder", &guid, NULL, &size,
871 if (ret == EFI_BUFFER_TOO_SMALL) {
872 bootorder = malloc(size);
873 ret = EFI_CALL(RT->get_variable(L"BootOrder", &guid, NULL,
876 if (ret == EFI_NOT_FOUND) {
877 printf("BootOrder not defined\n");
878 ret = CMD_RET_SUCCESS;
880 } else if (ret != EFI_SUCCESS) {
881 ret = CMD_RET_FAILURE;
885 num = size / sizeof(u16);
886 for (i = 0; i < num; i++) {
887 sprintf(var_name, "Boot%04X", bootorder[i]);
889 utf8_utf16_strncpy(&p16, var_name, 9);
892 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
894 if (ret != EFI_BUFFER_TOO_SMALL) {
895 printf("%2d: Boot%04X: (not defined)\n",
896 i + 1, bootorder[i]);
902 ret = CMD_RET_FAILURE;
905 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
907 if (ret != EFI_SUCCESS) {
909 ret = CMD_RET_FAILURE;
913 efi_deserialize_load_option(&lo, data);
915 label_len16 = u16_strlen(lo.label);
916 label_len = utf16_utf8_strnlen(lo.label, label_len16);
917 label = malloc(label_len + 1);
920 ret = CMD_RET_FAILURE;
924 utf16_utf8_strncpy(&p, lo.label, label_len16);
925 printf("%2d: Boot%04X: %s\n", i + 1, bootorder[i], label);
937 * do_efi_boot_next() - manage UEFI BootNext variable
939 * @cmdtp: Command table
940 * @flag: Command flag
941 * @argc: Number of arguments
942 * @argv: Argument array
943 * Return: CMD_RET_SUCCESS on success,
944 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
946 * Implement efidebug "boot next" sub-command.
947 * Set BootNext variable.
949 * efidebug boot next <id>
951 static int do_efi_boot_next(cmd_tbl_t *cmdtp, int flag,
952 int argc, char * const argv[])
959 int r = CMD_RET_SUCCESS;
962 return CMD_RET_USAGE;
964 bootnext = (u16)simple_strtoul(argv[1], &endp, 16);
965 if (*endp != '\0' || bootnext > 0xffff) {
966 printf("invalid value: %s\n", argv[1]);
971 guid = efi_global_variable_guid;
973 ret = EFI_CALL(RT->set_variable(L"BootNext", &guid,
974 EFI_VARIABLE_NON_VOLATILE |
975 EFI_VARIABLE_BOOTSERVICE_ACCESS |
976 EFI_VARIABLE_RUNTIME_ACCESS,
978 if (ret != EFI_SUCCESS) {
979 printf("Cannot set BootNext\n");
987 * do_efi_boot_order() - manage UEFI BootOrder variable
989 * @cmdtp: Command table
990 * @flag: Command flag
991 * @argc: Number of arguments
992 * @argv: Argument array
993 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
995 * Implement efidebug "boot order" sub-command.
996 * Show order of UEFI load options, or change it in BootOrder variable.
998 * efidebug boot order [<id> ...]
1000 static int do_efi_boot_order(cmd_tbl_t *cmdtp, int flag,
1001 int argc, char * const argv[])
1003 u16 *bootorder = NULL;
1009 int r = CMD_RET_SUCCESS;
1012 return show_efi_boot_order();
1017 size = argc * sizeof(u16);
1018 bootorder = malloc(size);
1020 return CMD_RET_FAILURE;
1022 for (i = 0; i < argc; i++) {
1023 id = (int)simple_strtoul(argv[i], &endp, 16);
1024 if (*endp != '\0' || id > 0xffff) {
1025 printf("invalid value: %s\n", argv[i]);
1026 r = CMD_RET_FAILURE;
1030 bootorder[i] = (u16)id;
1033 guid = efi_global_variable_guid;
1034 ret = EFI_CALL(RT->set_variable(L"BootOrder", &guid,
1035 EFI_VARIABLE_NON_VOLATILE |
1036 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1037 EFI_VARIABLE_RUNTIME_ACCESS,
1039 if (ret != EFI_SUCCESS) {
1040 printf("Cannot set BootOrder\n");
1041 r = CMD_RET_FAILURE;
1049 static cmd_tbl_t cmd_efidebug_boot_sub[] = {
1050 U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
1051 U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
1052 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
1053 U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
1054 U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
1059 * do_efi_boot_opt() - manage UEFI load options
1061 * @cmdtp: Command table
1062 * @flag: Command flag
1063 * @argc: Number of arguments
1064 * @argv: Argument array
1065 * Return: CMD_RET_SUCCESS on success,
1066 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1068 * Implement efidebug "boot" sub-command.
1070 static int do_efi_boot_opt(cmd_tbl_t *cmdtp, int flag,
1071 int argc, char * const argv[])
1076 return CMD_RET_USAGE;
1080 cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
1081 ARRAY_SIZE(cmd_efidebug_boot_sub));
1083 return CMD_RET_USAGE;
1085 return cp->cmd(cmdtp, flag, argc, argv);
1088 static cmd_tbl_t cmd_efidebug_sub[] = {
1089 U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
1090 U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices,
1092 U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
1094 U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
1096 U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
1098 U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
1100 U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables,
1105 * do_efidebug() - display and configure UEFI environment
1107 * @cmdtp: Command table
1108 * @flag: Command flag
1109 * @argc: Number of arguments
1110 * @argv: Argument array
1111 * Return: CMD_RET_SUCCESS on success,
1112 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1114 * Implement efidebug command which allows us to display and
1115 * configure UEFI environment.
1117 static int do_efidebug(cmd_tbl_t *cmdtp, int flag,
1118 int argc, char * const argv[])
1124 return CMD_RET_USAGE;
1128 /* Initialize UEFI drivers */
1129 r = efi_init_obj_list();
1130 if (r != EFI_SUCCESS) {
1131 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1132 r & ~EFI_ERROR_MASK);
1133 return CMD_RET_FAILURE;
1136 cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
1137 ARRAY_SIZE(cmd_efidebug_sub));
1139 return CMD_RET_USAGE;
1141 return cp->cmd(cmdtp, flag, argc, argv);
1144 #ifdef CONFIG_SYS_LONGHELP
1145 static char efidebug_help_text[] =
1146 " - UEFI Shell-like interface to configure UEFI environment\n"
1148 "efidebug boot add <bootid> <label> <interface> <devnum>[:<part>] <file path> [<load options>]\n"
1149 " - set UEFI BootXXXX variable\n"
1150 " <load options> will be passed to UEFI application\n"
1151 "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
1152 " - delete UEFI BootXXXX variables\n"
1153 "efidebug boot dump\n"
1154 " - dump all UEFI BootXXXX variables\n"
1155 "efidebug boot next <bootid>\n"
1156 " - set UEFI BootNext variable\n"
1157 "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
1158 " - set/show UEFI boot order\n"
1160 "efidebug devices\n"
1161 " - show UEFI devices\n"
1162 "efidebug drivers\n"
1163 " - show UEFI drivers\n"
1165 " - show UEFI handles\n"
1167 " - show loaded images\n"
1169 " - show UEFI memory map\n"
1171 " - show UEFI configuration tables\n";
1175 efidebug, 10, 0, do_efidebug,
1176 "Configure UEFI environment",