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 */
268 "Runtime properties",
269 EFI_RT_PROPERTIES_TABLE_GUID,
274 * get_guid_text - get string of GUID
276 * Return description of GUID.
279 * Return: description of GUID or NULL
281 static const char *get_guid_text(const void *guid)
285 for (i = 0; i < ARRAY_SIZE(guid_list); i++) {
287 * As guidcmp uses memcmp() we can safely accept unaligned
290 if (!guidcmp(&guid_list[i].guid, guid))
291 return guid_list[i].text;
298 * do_efi_show_handles() - show UEFI handles
300 * @cmdtp: Command table
301 * @flag: Command flag
302 * @argc: Number of arguments
303 * @argv: Argument array
304 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
306 * Implement efidebug "dh" sub-command.
307 * Show all UEFI handles and their information, currently all protocols
310 static int do_efi_show_handles(cmd_tbl_t *cmdtp, int flag,
311 int argc, char * const argv[])
313 efi_handle_t *handles;
315 efi_uintn_t num, count, i, j;
316 const char *guid_text;
319 ret = EFI_CALL(BS->locate_handle_buffer(ALL_HANDLES, NULL, NULL,
321 if (ret != EFI_SUCCESS)
322 return CMD_RET_FAILURE;
325 return CMD_RET_SUCCESS;
327 printf("Handle%.*s Protocols\n", EFI_HANDLE_WIDTH - 6, spc);
328 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
329 for (i = 0; i < num; i++) {
330 printf("%p", handles[i]);
331 ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid,
338 for (j = 0; j < count; j++) {
344 guid_text = get_guid_text(guid[j]);
348 printf("%pUl", guid[j]);
353 EFI_CALL(BS->free_pool(handles));
355 return CMD_RET_SUCCESS;
359 * do_efi_show_images() - show UEFI images
361 * @cmdtp: Command table
362 * @flag: Command flag
363 * @argc: Number of arguments
364 * @argv: Argument array
365 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
367 * Implement efidebug "images" sub-command.
368 * Show all UEFI loaded images and their information.
370 static int do_efi_show_images(cmd_tbl_t *cmdtp, int flag,
371 int argc, char * const argv[])
373 efi_print_image_infos(NULL);
375 return CMD_RET_SUCCESS;
378 static const char * const efi_mem_type_string[] = {
379 [EFI_RESERVED_MEMORY_TYPE] = "RESERVED",
380 [EFI_LOADER_CODE] = "LOADER CODE",
381 [EFI_LOADER_DATA] = "LOADER DATA",
382 [EFI_BOOT_SERVICES_CODE] = "BOOT CODE",
383 [EFI_BOOT_SERVICES_DATA] = "BOOT DATA",
384 [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE",
385 [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA",
386 [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL",
387 [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM",
388 [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM",
389 [EFI_ACPI_MEMORY_NVS] = "ACPI NVS",
390 [EFI_MMAP_IO] = "IO",
391 [EFI_MMAP_IO_PORT] = "IO PORT",
392 [EFI_PAL_CODE] = "PAL",
395 static const struct efi_mem_attrs {
398 } efi_mem_attrs[] = {
399 {EFI_MEMORY_UC, "UC"},
400 {EFI_MEMORY_UC, "UC"},
401 {EFI_MEMORY_WC, "WC"},
402 {EFI_MEMORY_WT, "WT"},
403 {EFI_MEMORY_WB, "WB"},
404 {EFI_MEMORY_UCE, "UCE"},
405 {EFI_MEMORY_WP, "WP"},
406 {EFI_MEMORY_RP, "RP"},
407 {EFI_MEMORY_XP, "WP"},
408 {EFI_MEMORY_NV, "NV"},
409 {EFI_MEMORY_MORE_RELIABLE, "REL"},
410 {EFI_MEMORY_RO, "RO"},
411 {EFI_MEMORY_RUNTIME, "RT"},
415 * print_memory_attributes() - print memory map attributes
417 * @attributes: Attribute value
419 * Print memory map attributes
421 static void print_memory_attributes(u64 attributes)
425 for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
426 if (attributes & efi_mem_attrs[i].bit) {
433 puts(efi_mem_attrs[i].text);
437 #define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
440 * do_efi_show_memmap() - show UEFI memory map
442 * @cmdtp: Command table
443 * @flag: Command flag
444 * @argc: Number of arguments
445 * @argv: Argument array
446 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
448 * Implement efidebug "memmap" sub-command.
449 * Show UEFI memory map.
451 static int do_efi_show_memmap(cmd_tbl_t *cmdtp, int flag,
452 int argc, char * const argv[])
454 struct efi_mem_desc *memmap = NULL, *map;
455 efi_uintn_t map_size = 0;
460 ret = EFI_CALL(BS->get_memory_map(&map_size, memmap, NULL, NULL, NULL));
461 if (ret == EFI_BUFFER_TOO_SMALL) {
462 map_size += sizeof(struct efi_mem_desc); /* for my own */
463 ret = EFI_CALL(BS->allocate_pool(EFI_LOADER_DATA,
464 map_size, (void *)&memmap));
465 if (ret != EFI_SUCCESS)
466 return CMD_RET_FAILURE;
467 ret = EFI_CALL(BS->get_memory_map(&map_size, memmap,
470 if (ret != EFI_SUCCESS) {
471 EFI_CALL(BS->free_pool(memmap));
472 return CMD_RET_FAILURE;
475 printf("Type Start%.*s End%.*s Attributes\n",
476 EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc);
477 printf("================ %.*s %.*s ==========\n",
478 EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep);
479 for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
480 if (map->type < EFI_MAX_MEMORY_TYPE)
481 type = efi_mem_type_string[map->type];
485 printf("%-16s %.*llx-%.*llx", type,
489 map->physical_start + map->num_pages * EFI_PAGE_SIZE);
491 print_memory_attributes(map->attribute);
495 EFI_CALL(BS->free_pool(memmap));
497 return CMD_RET_SUCCESS;
501 * do_efi_show_tables() - show UEFI configuration tables
503 * @cmdtp: Command table
504 * @flag: Command flag
505 * @argc: Number of arguments
506 * @argv: Argument array
507 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
509 * Implement efidebug "tables" sub-command.
510 * Show UEFI configuration tables.
512 static int do_efi_show_tables(cmd_tbl_t *cmdtp, int flag,
513 int argc, char * const argv[])
516 const char *guid_str;
518 for (i = 0; i < systab.nr_tables; ++i) {
519 guid_str = get_guid_text(&systab.tables[i].guid);
522 printf("%pUl %s\n", &systab.tables[i].guid, guid_str);
525 return CMD_RET_SUCCESS;
529 * do_efi_boot_add() - set UEFI load option
531 * @cmdtp: Command table
532 * @flag: Command flag
533 * @argc: Number of arguments
534 * @argv: Argument array
535 * Return: CMD_RET_SUCCESS on success,
536 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
538 * Implement efidebug "boot add" sub-command. Create or change UEFI load option.
540 * efidebug boot add <id> <label> <interface> <devnum>[:<part>] <file> <options>
542 static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag,
543 int argc, char * const argv[])
548 u16 var_name16[9], *p;
550 size_t label_len, label_len16;
552 struct efi_device_path *device_path = NULL, *file_path = NULL;
553 struct efi_load_option lo;
557 int r = CMD_RET_SUCCESS;
559 if (argc < 6 || argc > 7)
560 return CMD_RET_USAGE;
562 id = (int)simple_strtoul(argv[1], &endp, 16);
563 if (*endp != '\0' || id > 0xffff)
564 return CMD_RET_USAGE;
566 sprintf(var_name, "Boot%04X", id);
568 utf8_utf16_strncpy(&p, var_name, 9);
570 guid = efi_global_variable_guid;
573 lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
576 label_len = strlen(argv[2]);
577 label_len16 = utf8_utf16_strnlen(argv[2], label_len);
578 label = malloc((label_len16 + 1) * sizeof(u16));
580 return CMD_RET_FAILURE;
581 lo.label = label; /* label will be changed below */
582 utf8_utf16_strncpy(&label, argv[2], label_len);
585 ret = efi_dp_from_name(argv[3], argv[4], argv[5], &device_path,
587 if (ret != EFI_SUCCESS) {
588 printf("Cannot create device path for \"%s %s\"\n",
593 lo.file_path = file_path;
594 lo.file_path_length = efi_dp_size(file_path)
595 + sizeof(struct efi_device_path); /* for END */
599 lo.optional_data = NULL;
601 lo.optional_data = (const u8 *)argv[6];
603 size = efi_serialize_load_option(&lo, (u8 **)&data);
609 ret = EFI_CALL(RT->set_variable(var_name16, &guid,
610 EFI_VARIABLE_NON_VOLATILE |
611 EFI_VARIABLE_BOOTSERVICE_ACCESS |
612 EFI_VARIABLE_RUNTIME_ACCESS,
614 if (ret != EFI_SUCCESS) {
615 printf("Cannot set %ls\n", var_name16);
620 efi_free_pool(device_path);
621 efi_free_pool(file_path);
628 * do_efi_boot_rm() - delete UEFI load options
630 * @cmdtp: Command table
631 * @flag: Command flag
632 * @argc: Number of arguments
633 * @argv: Argument array
634 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
636 * Implement efidebug "boot rm" sub-command.
637 * Delete UEFI load options.
639 * efidebug boot rm <id> ...
641 static int do_efi_boot_rm(cmd_tbl_t *cmdtp, int flag,
642 int argc, char * const argv[])
652 return CMD_RET_USAGE;
654 guid = efi_global_variable_guid;
655 for (i = 1; i < argc; i++, argv++) {
656 id = (int)simple_strtoul(argv[1], &endp, 16);
657 if (*endp != '\0' || id > 0xffff)
658 return CMD_RET_FAILURE;
660 sprintf(var_name, "Boot%04X", id);
661 utf8_utf16_strncpy((u16 **)&var_name16, var_name, 9);
663 ret = EFI_CALL(RT->set_variable(var_name16, &guid, 0, 0, NULL));
665 printf("Cannot remove Boot%04X", id);
666 return CMD_RET_FAILURE;
670 return CMD_RET_SUCCESS;
674 * show_efi_boot_opt_data() - dump UEFI load option
676 * @id: load option number
677 * @data: value of UEFI load option variable
678 * @size: size of the boot option
680 * Decode the value of UEFI load option variable and print information.
682 static void show_efi_boot_opt_data(int id, void *data, size_t size)
684 struct efi_load_option lo;
686 size_t label_len16, label_len;
689 efi_deserialize_load_option(&lo, data);
691 label_len16 = u16_strlen(lo.label);
692 label_len = utf16_utf8_strnlen(lo.label, label_len16);
693 label = malloc(label_len + 1);
697 utf16_utf8_strncpy(&p, lo.label, label_len16);
699 printf("Boot%04X:\n", id);
700 printf(" attributes: %c%c%c (0x%08x)\n",
702 lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
703 /* FORCE RECONNECT */
704 lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
706 lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
708 printf(" label: %s\n", label);
710 dp_str = efi_dp_str(lo.file_path);
711 printf(" file_path: %ls\n", dp_str);
712 efi_free_pool(dp_str);
715 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
716 lo.optional_data, size + (u8 *)data -
717 (u8 *)lo.optional_data, true);
722 * show_efi_boot_opt() - dump UEFI load option
724 * @id: Load option number
726 * Dump information defined by UEFI load option.
728 static void show_efi_boot_opt(int id)
731 u16 var_name16[9], *p;
737 sprintf(var_name, "Boot%04X", id);
739 utf8_utf16_strncpy(&p, var_name, 9);
740 guid = efi_global_variable_guid;
743 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, NULL));
744 if (ret == EFI_BUFFER_TOO_SMALL) {
746 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
749 if (ret == EFI_SUCCESS)
750 show_efi_boot_opt_data(id, data, size);
751 else if (ret == EFI_NOT_FOUND)
752 printf("Boot%04X: not found\n", id);
757 static int u16_tohex(u16 c)
759 if (c >= '0' && c <= '9')
761 if (c >= 'A' && c <= 'F')
764 /* not hexadecimal */
769 * show_efi_boot_dump() - dump all UEFI load options
771 * @cmdtp: Command table
772 * @flag: Command flag
773 * @argc: Number of arguments
774 * @argv: Argument array
775 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
777 * Implement efidebug "boot dump" sub-command.
778 * Dump information of all UEFI load options defined.
782 static int do_efi_boot_dump(cmd_tbl_t *cmdtp, int flag,
783 int argc, char * const argv[])
786 efi_uintn_t buf_size, size;
792 return CMD_RET_USAGE;
795 var_name16 = malloc(buf_size);
797 return CMD_RET_FAILURE;
802 ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
804 if (ret == EFI_NOT_FOUND)
806 if (ret == EFI_BUFFER_TOO_SMALL) {
808 p = realloc(var_name16, buf_size);
811 return CMD_RET_FAILURE;
814 ret = EFI_CALL(efi_get_next_variable_name(&size,
818 if (ret != EFI_SUCCESS) {
820 return CMD_RET_FAILURE;
823 if (memcmp(var_name16, L"Boot", 8))
826 for (id = 0, i = 0; i < 4; i++) {
827 digit = u16_tohex(var_name16[4 + i]);
830 id = (id << 4) + digit;
832 if (i == 4 && !var_name16[8])
833 show_efi_boot_opt(id);
838 return CMD_RET_SUCCESS;
842 * show_efi_boot_order() - show order of UEFI load options
844 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
846 * Show order of UEFI load options defined by BootOrder variable.
848 static int show_efi_boot_order(void)
851 u16 *bootorder = NULL;
855 u16 var_name16[9], *p16;
857 struct efi_load_option lo;
859 size_t label_len16, label_len;
862 guid = efi_global_variable_guid;
864 ret = EFI_CALL(RT->get_variable(L"BootOrder", &guid, NULL, &size,
866 if (ret == EFI_BUFFER_TOO_SMALL) {
867 bootorder = malloc(size);
868 ret = EFI_CALL(RT->get_variable(L"BootOrder", &guid, NULL,
871 if (ret == EFI_NOT_FOUND) {
872 printf("BootOrder not defined\n");
873 ret = CMD_RET_SUCCESS;
875 } else if (ret != EFI_SUCCESS) {
876 ret = CMD_RET_FAILURE;
880 num = size / sizeof(u16);
881 for (i = 0; i < num; i++) {
882 sprintf(var_name, "Boot%04X", bootorder[i]);
884 utf8_utf16_strncpy(&p16, var_name, 9);
887 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
889 if (ret != EFI_BUFFER_TOO_SMALL) {
890 printf("%2d: Boot%04X: (not defined)\n",
891 i + 1, bootorder[i]);
897 ret = CMD_RET_FAILURE;
900 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
902 if (ret != EFI_SUCCESS) {
904 ret = CMD_RET_FAILURE;
908 efi_deserialize_load_option(&lo, data);
910 label_len16 = u16_strlen(lo.label);
911 label_len = utf16_utf8_strnlen(lo.label, label_len16);
912 label = malloc(label_len + 1);
915 ret = CMD_RET_FAILURE;
919 utf16_utf8_strncpy(&p, lo.label, label_len16);
920 printf("%2d: Boot%04X: %s\n", i + 1, bootorder[i], label);
932 * do_efi_boot_next() - manage UEFI BootNext variable
934 * @cmdtp: Command table
935 * @flag: Command flag
936 * @argc: Number of arguments
937 * @argv: Argument array
938 * Return: CMD_RET_SUCCESS on success,
939 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
941 * Implement efidebug "boot next" sub-command.
942 * Set BootNext variable.
944 * efidebug boot next <id>
946 static int do_efi_boot_next(cmd_tbl_t *cmdtp, int flag,
947 int argc, char * const argv[])
954 int r = CMD_RET_SUCCESS;
957 return CMD_RET_USAGE;
959 bootnext = (u16)simple_strtoul(argv[1], &endp, 16);
960 if (*endp != '\0' || bootnext > 0xffff) {
961 printf("invalid value: %s\n", argv[1]);
966 guid = efi_global_variable_guid;
968 ret = EFI_CALL(RT->set_variable(L"BootNext", &guid,
969 EFI_VARIABLE_NON_VOLATILE |
970 EFI_VARIABLE_BOOTSERVICE_ACCESS |
971 EFI_VARIABLE_RUNTIME_ACCESS,
973 if (ret != EFI_SUCCESS) {
974 printf("Cannot set BootNext\n");
982 * do_efi_boot_order() - manage UEFI BootOrder variable
984 * @cmdtp: Command table
985 * @flag: Command flag
986 * @argc: Number of arguments
987 * @argv: Argument array
988 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
990 * Implement efidebug "boot order" sub-command.
991 * Show order of UEFI load options, or change it in BootOrder variable.
993 * efidebug boot order [<id> ...]
995 static int do_efi_boot_order(cmd_tbl_t *cmdtp, int flag,
996 int argc, char * const argv[])
998 u16 *bootorder = NULL;
1004 int r = CMD_RET_SUCCESS;
1007 return show_efi_boot_order();
1012 size = argc * sizeof(u16);
1013 bootorder = malloc(size);
1015 return CMD_RET_FAILURE;
1017 for (i = 0; i < argc; i++) {
1018 id = (int)simple_strtoul(argv[i], &endp, 16);
1019 if (*endp != '\0' || id > 0xffff) {
1020 printf("invalid value: %s\n", argv[i]);
1021 r = CMD_RET_FAILURE;
1025 bootorder[i] = (u16)id;
1028 guid = efi_global_variable_guid;
1029 ret = EFI_CALL(RT->set_variable(L"BootOrder", &guid,
1030 EFI_VARIABLE_NON_VOLATILE |
1031 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1032 EFI_VARIABLE_RUNTIME_ACCESS,
1034 if (ret != EFI_SUCCESS) {
1035 printf("Cannot set BootOrder\n");
1036 r = CMD_RET_FAILURE;
1044 static cmd_tbl_t cmd_efidebug_boot_sub[] = {
1045 U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
1046 U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
1047 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
1048 U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
1049 U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
1054 * do_efi_boot_opt() - manage UEFI load options
1056 * @cmdtp: Command table
1057 * @flag: Command flag
1058 * @argc: Number of arguments
1059 * @argv: Argument array
1060 * Return: CMD_RET_SUCCESS on success,
1061 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1063 * Implement efidebug "boot" sub-command.
1065 static int do_efi_boot_opt(cmd_tbl_t *cmdtp, int flag,
1066 int argc, char * const argv[])
1071 return CMD_RET_USAGE;
1075 cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
1076 ARRAY_SIZE(cmd_efidebug_boot_sub));
1078 return CMD_RET_USAGE;
1080 return cp->cmd(cmdtp, flag, argc, argv);
1083 static cmd_tbl_t cmd_efidebug_sub[] = {
1084 U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
1085 U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices,
1087 U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
1089 U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
1091 U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
1093 U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
1095 U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables,
1100 * do_efidebug() - display and configure UEFI environment
1102 * @cmdtp: Command table
1103 * @flag: Command flag
1104 * @argc: Number of arguments
1105 * @argv: Argument array
1106 * Return: CMD_RET_SUCCESS on success,
1107 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1109 * Implement efidebug command which allows us to display and
1110 * configure UEFI environment.
1112 static int do_efidebug(cmd_tbl_t *cmdtp, int flag,
1113 int argc, char * const argv[])
1119 return CMD_RET_USAGE;
1123 /* Initialize UEFI drivers */
1124 r = efi_init_obj_list();
1125 if (r != EFI_SUCCESS) {
1126 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1127 r & ~EFI_ERROR_MASK);
1128 return CMD_RET_FAILURE;
1131 cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
1132 ARRAY_SIZE(cmd_efidebug_sub));
1134 return CMD_RET_USAGE;
1136 return cp->cmd(cmdtp, flag, argc, argv);
1139 #ifdef CONFIG_SYS_LONGHELP
1140 static char efidebug_help_text[] =
1141 " - UEFI Shell-like interface to configure UEFI environment\n"
1143 "efidebug boot add <bootid> <label> <interface> <devnum>[:<part>] <file path> [<load options>]\n"
1144 " - set UEFI BootXXXX variable\n"
1145 " <load options> will be passed to UEFI application\n"
1146 "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
1147 " - delete UEFI BootXXXX variables\n"
1148 "efidebug boot dump\n"
1149 " - dump all UEFI BootXXXX variables\n"
1150 "efidebug boot next <bootid>\n"
1151 " - set UEFI BootNext variable\n"
1152 "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
1153 " - set/show UEFI boot order\n"
1155 "efidebug devices\n"
1156 " - show UEFI devices\n"
1157 "efidebug drivers\n"
1158 " - show UEFI drivers\n"
1160 " - show UEFI handles\n"
1162 " - show loaded images\n"
1164 " - show UEFI memory map\n"
1166 " - show UEFI configuration tables\n";
1170 efidebug, 10, 0, do_efidebug,
1171 "Configure UEFI environment",