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
20 #define RT systab.runtime
23 * efi_get_device_handle_info() - get information of UEFI device
25 * @handle: Handle of UEFI device
26 * @dev_path_text: Pointer to text of device path
27 * Return: 0 on success, -1 on failure
29 * Currently return a formatted text of device path.
31 static int efi_get_device_handle_info(efi_handle_t handle, u16 **dev_path_text)
33 struct efi_device_path *dp;
36 ret = EFI_CALL(BS->open_protocol(handle, &efi_guid_device_path,
37 (void **)&dp, NULL /* FIXME */, NULL,
38 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
39 if (ret == EFI_SUCCESS) {
40 *dev_path_text = efi_dp_str(dp);
47 #define EFI_HANDLE_WIDTH ((int)sizeof(efi_handle_t) * 2)
49 static const char spc[] = " ";
50 static const char sep[] = "================";
53 * do_efi_show_devices() - show UEFI devices
55 * @cmdtp: Command table
57 * @argc: Number of arguments
58 * @argv: Argument array
59 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
61 * Implement efidebug "devices" sub-command.
62 * Show all UEFI devices and their information.
64 static int do_efi_show_devices(cmd_tbl_t *cmdtp, int flag,
65 int argc, char * const argv[])
67 efi_handle_t *handles;
72 ret = EFI_CALL(BS->locate_handle_buffer(ALL_HANDLES, NULL, NULL,
74 if (ret != EFI_SUCCESS)
75 return CMD_RET_FAILURE;
78 return CMD_RET_SUCCESS;
80 printf("Device%.*s Device Path\n", EFI_HANDLE_WIDTH - 6, spc);
81 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
82 for (i = 0; i < num; i++) {
83 if (!efi_get_device_handle_info(handles[i], &dev_path_text)) {
84 printf("%p %ls\n", handles[i], dev_path_text);
85 efi_free_pool(dev_path_text);
89 EFI_CALL(BS->free_pool(handles));
91 return CMD_RET_SUCCESS;
95 * efi_get_driver_handle_info() - get information of UEFI driver
97 * @handle: Handle of UEFI device
98 * @driver_name: Driver name
99 * @image_path: Pointer to text of device path
100 * Return: 0 on success, -1 on failure
102 * Currently return no useful information as all UEFI drivers are
105 static int efi_get_driver_handle_info(efi_handle_t handle, u16 **driver_name,
108 struct efi_handler *handler;
109 struct efi_loaded_image *image;
114 * TODO: support EFI_COMPONENT_NAME2_PROTOCOL
119 ret = efi_search_protocol(handle, &efi_guid_loaded_image, &handler);
120 if (ret != EFI_SUCCESS) {
125 image = handler->protocol_interface;
126 *image_path = efi_dp_str(image->file_path);
132 * do_efi_show_drivers() - show UEFI drivers
134 * @cmdtp: Command table
135 * @flag: Command flag
136 * @argc: Number of arguments
137 * @argv: Argument array
138 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
140 * Implement efidebug "drivers" sub-command.
141 * Show all UEFI drivers and their information.
143 static int do_efi_show_drivers(cmd_tbl_t *cmdtp, int flag,
144 int argc, char * const argv[])
146 efi_handle_t *handles;
148 u16 *driver_name, *image_path_text;
151 ret = EFI_CALL(BS->locate_handle_buffer(
152 BY_PROTOCOL, &efi_guid_driver_binding_protocol,
153 NULL, &num, &handles));
154 if (ret != EFI_SUCCESS)
155 return CMD_RET_FAILURE;
158 return CMD_RET_SUCCESS;
160 printf("Driver%.*s Name Image Path\n",
161 EFI_HANDLE_WIDTH - 6, spc);
162 printf("%.*s ==================== ====================\n",
163 EFI_HANDLE_WIDTH, sep);
164 for (i = 0; i < num; i++) {
165 if (!efi_get_driver_handle_info(handles[i], &driver_name,
168 printf("%p %-20ls %ls\n", handles[i],
169 driver_name, image_path_text);
171 printf("%p %-20ls <built-in>\n",
172 handles[i], driver_name);
173 EFI_CALL(BS->free_pool(driver_name));
174 EFI_CALL(BS->free_pool(image_path_text));
178 EFI_CALL(BS->free_pool(handles));
180 return CMD_RET_SUCCESS;
183 static const struct {
185 const efi_guid_t guid;
189 EFI_DEVICE_PATH_PROTOCOL_GUID,
192 "Device Path To Text",
193 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID,
196 "Device Path Utilities",
197 EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID,
200 "Unicode Collation 2",
201 EFI_UNICODE_COLLATION_PROTOCOL2_GUID,
205 EFI_DRIVER_BINDING_PROTOCOL_GUID,
209 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID,
212 "Simple Text Input Ex",
213 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID,
216 "Simple Text Output",
217 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID,
221 EFI_BLOCK_IO_PROTOCOL_GUID,
224 "Simple File System",
225 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
229 EFI_LOADED_IMAGE_PROTOCOL_GUID,
233 EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID,
237 EFI_HII_STRING_PROTOCOL_GUID,
241 EFI_HII_DATABASE_PROTOCOL_GUID,
244 "HII Config Routing",
245 EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID,
249 EFI_LOAD_FILE2_PROTOCOL_GUID,
253 EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
257 EFI_PXE_BASE_CODE_PROTOCOL_GUID,
259 /* Configuration table GUIDs */
273 "Runtime properties",
274 EFI_RT_PROPERTIES_TABLE_GUID,
279 * get_guid_text - get string of GUID
281 * Return description of GUID.
284 * Return: description of GUID or NULL
286 static const char *get_guid_text(const void *guid)
290 for (i = 0; i < ARRAY_SIZE(guid_list); i++) {
292 * As guidcmp uses memcmp() we can safely accept unaligned
295 if (!guidcmp(&guid_list[i].guid, guid))
296 return guid_list[i].text;
303 * do_efi_show_handles() - show UEFI handles
305 * @cmdtp: Command table
306 * @flag: Command flag
307 * @argc: Number of arguments
308 * @argv: Argument array
309 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
311 * Implement efidebug "dh" sub-command.
312 * Show all UEFI handles and their information, currently all protocols
315 static int do_efi_show_handles(cmd_tbl_t *cmdtp, int flag,
316 int argc, char * const argv[])
318 efi_handle_t *handles;
320 efi_uintn_t num, count, i, j;
321 const char *guid_text;
324 ret = EFI_CALL(BS->locate_handle_buffer(ALL_HANDLES, NULL, NULL,
326 if (ret != EFI_SUCCESS)
327 return CMD_RET_FAILURE;
330 return CMD_RET_SUCCESS;
332 printf("Handle%.*s Protocols\n", EFI_HANDLE_WIDTH - 6, spc);
333 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
334 for (i = 0; i < num; i++) {
335 printf("%p", handles[i]);
336 ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid,
343 for (j = 0; j < count; j++) {
349 guid_text = get_guid_text(guid[j]);
353 printf("%pUl", guid[j]);
358 EFI_CALL(BS->free_pool(handles));
360 return CMD_RET_SUCCESS;
364 * do_efi_show_images() - show UEFI images
366 * @cmdtp: Command table
367 * @flag: Command flag
368 * @argc: Number of arguments
369 * @argv: Argument array
370 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
372 * Implement efidebug "images" sub-command.
373 * Show all UEFI loaded images and their information.
375 static int do_efi_show_images(cmd_tbl_t *cmdtp, int flag,
376 int argc, char * const argv[])
378 efi_print_image_infos(NULL);
380 return CMD_RET_SUCCESS;
383 static const char * const efi_mem_type_string[] = {
384 [EFI_RESERVED_MEMORY_TYPE] = "RESERVED",
385 [EFI_LOADER_CODE] = "LOADER CODE",
386 [EFI_LOADER_DATA] = "LOADER DATA",
387 [EFI_BOOT_SERVICES_CODE] = "BOOT CODE",
388 [EFI_BOOT_SERVICES_DATA] = "BOOT DATA",
389 [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE",
390 [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA",
391 [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL",
392 [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM",
393 [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM",
394 [EFI_ACPI_MEMORY_NVS] = "ACPI NVS",
395 [EFI_MMAP_IO] = "IO",
396 [EFI_MMAP_IO_PORT] = "IO PORT",
397 [EFI_PAL_CODE] = "PAL",
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_CALL(BS->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_CALL(BS->allocate_pool(EFI_LOADER_DATA,
469 map_size, (void *)&memmap));
470 if (ret != EFI_SUCCESS)
471 return CMD_RET_FAILURE;
472 ret = EFI_CALL(BS->get_memory_map(&map_size, memmap,
475 if (ret != EFI_SUCCESS) {
476 EFI_CALL(BS->free_pool(memmap));
477 return CMD_RET_FAILURE;
480 printf("Type Start%.*s End%.*s Attributes\n",
481 EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc);
482 printf("================ %.*s %.*s ==========\n",
483 EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep);
484 for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
485 if (map->type < EFI_MAX_MEMORY_TYPE)
486 type = efi_mem_type_string[map->type];
490 printf("%-16s %.*llx-%.*llx", type,
492 (u64)map_to_sysmem((void *)(uintptr_t)
493 map->physical_start),
495 (u64)map_to_sysmem((void *)(uintptr_t)
496 (map->physical_start +
497 map->num_pages * EFI_PAGE_SIZE)));
499 print_memory_attributes(map->attribute);
503 EFI_CALL(BS->free_pool(memmap));
505 return CMD_RET_SUCCESS;
509 * do_efi_show_tables() - show UEFI configuration tables
511 * @cmdtp: Command table
512 * @flag: Command flag
513 * @argc: Number of arguments
514 * @argv: Argument array
515 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
517 * Implement efidebug "tables" sub-command.
518 * Show UEFI configuration tables.
520 static int do_efi_show_tables(cmd_tbl_t *cmdtp, int flag,
521 int argc, char * const argv[])
524 const char *guid_str;
526 for (i = 0; i < systab.nr_tables; ++i) {
527 guid_str = get_guid_text(&systab.tables[i].guid);
530 printf("%pUl %s\n", &systab.tables[i].guid, guid_str);
533 return CMD_RET_SUCCESS;
537 * do_efi_boot_add() - set UEFI load option
539 * @cmdtp: Command table
540 * @flag: Command flag
541 * @argc: Number of arguments
542 * @argv: Argument array
543 * Return: CMD_RET_SUCCESS on success,
544 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
546 * Implement efidebug "boot add" sub-command. Create or change UEFI load option.
548 * efidebug boot add <id> <label> <interface> <devnum>[:<part>] <file> <options>
550 static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag,
551 int argc, char * const argv[])
556 u16 var_name16[9], *p;
558 size_t label_len, label_len16;
560 struct efi_device_path *device_path = NULL, *file_path = NULL;
561 struct efi_load_option lo;
565 int r = CMD_RET_SUCCESS;
567 if (argc < 6 || argc > 7)
568 return CMD_RET_USAGE;
570 id = (int)simple_strtoul(argv[1], &endp, 16);
571 if (*endp != '\0' || id > 0xffff)
572 return CMD_RET_USAGE;
574 sprintf(var_name, "Boot%04X", id);
576 utf8_utf16_strncpy(&p, var_name, 9);
578 guid = efi_global_variable_guid;
581 lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
584 label_len = strlen(argv[2]);
585 label_len16 = utf8_utf16_strnlen(argv[2], label_len);
586 label = malloc((label_len16 + 1) * sizeof(u16));
588 return CMD_RET_FAILURE;
589 lo.label = label; /* label will be changed below */
590 utf8_utf16_strncpy(&label, argv[2], label_len);
593 ret = efi_dp_from_name(argv[3], argv[4], argv[5], &device_path,
595 if (ret != EFI_SUCCESS) {
596 printf("Cannot create device path for \"%s %s\"\n",
601 lo.file_path = file_path;
602 lo.file_path_length = efi_dp_size(file_path)
603 + sizeof(struct efi_device_path); /* for END */
607 lo.optional_data = NULL;
609 lo.optional_data = (const u8 *)argv[6];
611 size = efi_serialize_load_option(&lo, (u8 **)&data);
617 ret = EFI_CALL(RT->set_variable(var_name16, &guid,
618 EFI_VARIABLE_NON_VOLATILE |
619 EFI_VARIABLE_BOOTSERVICE_ACCESS |
620 EFI_VARIABLE_RUNTIME_ACCESS,
622 if (ret != EFI_SUCCESS) {
623 printf("Cannot set %ls\n", var_name16);
628 efi_free_pool(device_path);
629 efi_free_pool(file_path);
636 * do_efi_boot_rm() - delete UEFI load options
638 * @cmdtp: Command table
639 * @flag: Command flag
640 * @argc: Number of arguments
641 * @argv: Argument array
642 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
644 * Implement efidebug "boot rm" sub-command.
645 * Delete UEFI load options.
647 * efidebug boot rm <id> ...
649 static int do_efi_boot_rm(cmd_tbl_t *cmdtp, int flag,
650 int argc, char * const argv[])
656 u16 var_name16[9], *p;
660 return CMD_RET_USAGE;
662 guid = efi_global_variable_guid;
663 for (i = 1; i < argc; i++, argv++) {
664 id = (int)simple_strtoul(argv[1], &endp, 16);
665 if (*endp != '\0' || id > 0xffff)
666 return CMD_RET_FAILURE;
668 sprintf(var_name, "Boot%04X", id);
670 utf8_utf16_strncpy(&p, var_name, 9);
672 ret = EFI_CALL(RT->set_variable(var_name16, &guid, 0, 0, NULL));
674 printf("Cannot remove %ls\n", var_name16);
675 return CMD_RET_FAILURE;
679 return CMD_RET_SUCCESS;
683 * show_efi_boot_opt_data() - dump UEFI load option
685 * @id: load option number
686 * @data: value of UEFI load option variable
687 * @size: size of the boot option
689 * Decode the value of UEFI load option variable and print information.
691 static void show_efi_boot_opt_data(int id, void *data, size_t size)
693 struct efi_load_option lo;
695 size_t label_len16, label_len;
698 efi_deserialize_load_option(&lo, data);
700 label_len16 = u16_strlen(lo.label);
701 label_len = utf16_utf8_strnlen(lo.label, label_len16);
702 label = malloc(label_len + 1);
706 utf16_utf8_strncpy(&p, lo.label, label_len16);
708 printf("Boot%04X:\n", id);
709 printf(" attributes: %c%c%c (0x%08x)\n",
711 lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
712 /* FORCE RECONNECT */
713 lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
715 lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
717 printf(" label: %s\n", label);
719 dp_str = efi_dp_str(lo.file_path);
720 printf(" file_path: %ls\n", dp_str);
721 efi_free_pool(dp_str);
724 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
725 lo.optional_data, size + (u8 *)data -
726 (u8 *)lo.optional_data, true);
731 * show_efi_boot_opt() - dump UEFI load option
733 * @id: Load option number
735 * Dump information defined by UEFI load option.
737 static void show_efi_boot_opt(int id)
740 u16 var_name16[9], *p;
746 sprintf(var_name, "Boot%04X", id);
748 utf8_utf16_strncpy(&p, var_name, 9);
749 guid = efi_global_variable_guid;
752 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, NULL));
753 if (ret == EFI_BUFFER_TOO_SMALL) {
755 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
758 if (ret == EFI_SUCCESS)
759 show_efi_boot_opt_data(id, data, size);
760 else if (ret == EFI_NOT_FOUND)
761 printf("Boot%04X: not found\n", id);
766 static int u16_tohex(u16 c)
768 if (c >= '0' && c <= '9')
770 if (c >= 'A' && c <= 'F')
773 /* not hexadecimal */
778 * show_efi_boot_dump() - dump all UEFI load options
780 * @cmdtp: Command table
781 * @flag: Command flag
782 * @argc: Number of arguments
783 * @argv: Argument array
784 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
786 * Implement efidebug "boot dump" sub-command.
787 * Dump information of all UEFI load options defined.
791 static int do_efi_boot_dump(cmd_tbl_t *cmdtp, int flag,
792 int argc, char * const argv[])
795 efi_uintn_t buf_size, size;
801 return CMD_RET_USAGE;
804 var_name16 = malloc(buf_size);
806 return CMD_RET_FAILURE;
811 ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
813 if (ret == EFI_NOT_FOUND)
815 if (ret == EFI_BUFFER_TOO_SMALL) {
817 p = realloc(var_name16, buf_size);
820 return CMD_RET_FAILURE;
823 ret = EFI_CALL(efi_get_next_variable_name(&size,
827 if (ret != EFI_SUCCESS) {
829 return CMD_RET_FAILURE;
832 if (memcmp(var_name16, L"Boot", 8))
835 for (id = 0, i = 0; i < 4; i++) {
836 digit = u16_tohex(var_name16[4 + i]);
839 id = (id << 4) + digit;
841 if (i == 4 && !var_name16[8])
842 show_efi_boot_opt(id);
847 return CMD_RET_SUCCESS;
851 * show_efi_boot_order() - show order of UEFI load options
853 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
855 * Show order of UEFI load options defined by BootOrder variable.
857 static int show_efi_boot_order(void)
860 u16 *bootorder = NULL;
864 u16 var_name16[9], *p16;
866 struct efi_load_option lo;
868 size_t label_len16, label_len;
871 guid = efi_global_variable_guid;
873 ret = EFI_CALL(RT->get_variable(L"BootOrder", &guid, NULL, &size,
875 if (ret == EFI_BUFFER_TOO_SMALL) {
876 bootorder = malloc(size);
877 ret = EFI_CALL(RT->get_variable(L"BootOrder", &guid, NULL,
880 if (ret == EFI_NOT_FOUND) {
881 printf("BootOrder not defined\n");
882 ret = CMD_RET_SUCCESS;
884 } else if (ret != EFI_SUCCESS) {
885 ret = CMD_RET_FAILURE;
889 num = size / sizeof(u16);
890 for (i = 0; i < num; i++) {
891 sprintf(var_name, "Boot%04X", bootorder[i]);
893 utf8_utf16_strncpy(&p16, var_name, 9);
896 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
898 if (ret != EFI_BUFFER_TOO_SMALL) {
899 printf("%2d: Boot%04X: (not defined)\n",
900 i + 1, bootorder[i]);
906 ret = CMD_RET_FAILURE;
909 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
911 if (ret != EFI_SUCCESS) {
913 ret = CMD_RET_FAILURE;
917 efi_deserialize_load_option(&lo, data);
919 label_len16 = u16_strlen(lo.label);
920 label_len = utf16_utf8_strnlen(lo.label, label_len16);
921 label = malloc(label_len + 1);
924 ret = CMD_RET_FAILURE;
928 utf16_utf8_strncpy(&p, lo.label, label_len16);
929 printf("%2d: Boot%04X: %s\n", i + 1, bootorder[i], label);
941 * do_efi_boot_next() - manage UEFI BootNext variable
943 * @cmdtp: Command table
944 * @flag: Command flag
945 * @argc: Number of arguments
946 * @argv: Argument array
947 * Return: CMD_RET_SUCCESS on success,
948 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
950 * Implement efidebug "boot next" sub-command.
951 * Set BootNext variable.
953 * efidebug boot next <id>
955 static int do_efi_boot_next(cmd_tbl_t *cmdtp, int flag,
956 int argc, char * const argv[])
963 int r = CMD_RET_SUCCESS;
966 return CMD_RET_USAGE;
968 bootnext = (u16)simple_strtoul(argv[1], &endp, 16);
969 if (*endp != '\0' || bootnext > 0xffff) {
970 printf("invalid value: %s\n", argv[1]);
975 guid = efi_global_variable_guid;
977 ret = EFI_CALL(RT->set_variable(L"BootNext", &guid,
978 EFI_VARIABLE_NON_VOLATILE |
979 EFI_VARIABLE_BOOTSERVICE_ACCESS |
980 EFI_VARIABLE_RUNTIME_ACCESS,
982 if (ret != EFI_SUCCESS) {
983 printf("Cannot set BootNext\n");
991 * do_efi_boot_order() - manage UEFI BootOrder variable
993 * @cmdtp: Command table
994 * @flag: Command flag
995 * @argc: Number of arguments
996 * @argv: Argument array
997 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
999 * Implement efidebug "boot order" sub-command.
1000 * Show order of UEFI load options, or change it in BootOrder variable.
1002 * efidebug boot order [<id> ...]
1004 static int do_efi_boot_order(cmd_tbl_t *cmdtp, int flag,
1005 int argc, char * const argv[])
1007 u16 *bootorder = NULL;
1013 int r = CMD_RET_SUCCESS;
1016 return show_efi_boot_order();
1021 size = argc * sizeof(u16);
1022 bootorder = malloc(size);
1024 return CMD_RET_FAILURE;
1026 for (i = 0; i < argc; i++) {
1027 id = (int)simple_strtoul(argv[i], &endp, 16);
1028 if (*endp != '\0' || id > 0xffff) {
1029 printf("invalid value: %s\n", argv[i]);
1030 r = CMD_RET_FAILURE;
1034 bootorder[i] = (u16)id;
1037 guid = efi_global_variable_guid;
1038 ret = EFI_CALL(RT->set_variable(L"BootOrder", &guid,
1039 EFI_VARIABLE_NON_VOLATILE |
1040 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1041 EFI_VARIABLE_RUNTIME_ACCESS,
1043 if (ret != EFI_SUCCESS) {
1044 printf("Cannot set BootOrder\n");
1045 r = CMD_RET_FAILURE;
1053 static cmd_tbl_t cmd_efidebug_boot_sub[] = {
1054 U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
1055 U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
1056 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
1057 U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
1058 U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
1063 * do_efi_boot_opt() - manage UEFI load options
1065 * @cmdtp: Command table
1066 * @flag: Command flag
1067 * @argc: Number of arguments
1068 * @argv: Argument array
1069 * Return: CMD_RET_SUCCESS on success,
1070 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1072 * Implement efidebug "boot" sub-command.
1074 static int do_efi_boot_opt(cmd_tbl_t *cmdtp, int flag,
1075 int argc, char * const argv[])
1080 return CMD_RET_USAGE;
1084 cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
1085 ARRAY_SIZE(cmd_efidebug_boot_sub));
1087 return CMD_RET_USAGE;
1089 return cp->cmd(cmdtp, flag, argc, argv);
1092 static cmd_tbl_t cmd_efidebug_sub[] = {
1093 U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
1094 U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices,
1096 U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
1098 U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
1100 U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
1102 U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
1104 U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables,
1109 * do_efidebug() - display and configure UEFI environment
1111 * @cmdtp: Command table
1112 * @flag: Command flag
1113 * @argc: Number of arguments
1114 * @argv: Argument array
1115 * Return: CMD_RET_SUCCESS on success,
1116 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1118 * Implement efidebug command which allows us to display and
1119 * configure UEFI environment.
1121 static int do_efidebug(cmd_tbl_t *cmdtp, int flag,
1122 int argc, char * const argv[])
1128 return CMD_RET_USAGE;
1132 /* Initialize UEFI drivers */
1133 r = efi_init_obj_list();
1134 if (r != EFI_SUCCESS) {
1135 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1136 r & ~EFI_ERROR_MASK);
1137 return CMD_RET_FAILURE;
1140 cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
1141 ARRAY_SIZE(cmd_efidebug_sub));
1143 return CMD_RET_USAGE;
1145 return cp->cmd(cmdtp, flag, argc, argv);
1148 #ifdef CONFIG_SYS_LONGHELP
1149 static char efidebug_help_text[] =
1150 " - UEFI Shell-like interface to configure UEFI environment\n"
1152 "efidebug boot add <bootid> <label> <interface> <devnum>[:<part>] <file path> [<load options>]\n"
1153 " - set UEFI BootXXXX variable\n"
1154 " <load options> will be passed to UEFI application\n"
1155 "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
1156 " - delete UEFI BootXXXX variables\n"
1157 "efidebug boot dump\n"
1158 " - dump all UEFI BootXXXX variables\n"
1159 "efidebug boot next <bootid>\n"
1160 " - set UEFI BootNext variable\n"
1161 "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
1162 " - set/show UEFI boot order\n"
1164 "efidebug devices\n"
1165 " - show UEFI devices\n"
1166 "efidebug drivers\n"
1167 " - show UEFI drivers\n"
1169 " - show UEFI handles\n"
1171 " - show loaded images\n"
1173 " - show UEFI memory map\n"
1175 " - show UEFI configuration tables\n";
1179 efidebug, 10, 0, do_efidebug,
1180 "Configure UEFI environment",