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>
19 #include <linux/ctype.h>
21 #define BS systab.boottime
24 * efi_get_device_handle_info() - get information of UEFI device
26 * @handle: Handle of UEFI device
27 * @dev_path_text: Pointer to text of device path
28 * Return: 0 on success, -1 on failure
30 * Currently return a formatted text of device path.
32 static int efi_get_device_handle_info(efi_handle_t handle, u16 **dev_path_text)
34 struct efi_device_path *dp;
37 ret = EFI_CALL(BS->open_protocol(handle, &efi_guid_device_path,
38 (void **)&dp, NULL /* FIXME */, NULL,
39 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
40 if (ret == EFI_SUCCESS) {
41 *dev_path_text = efi_dp_str(dp);
48 #define EFI_HANDLE_WIDTH ((int)sizeof(efi_handle_t) * 2)
50 static const char spc[] = " ";
51 static const char sep[] = "================";
54 * do_efi_show_devices() - show UEFI devices
56 * @cmdtp: Command table
58 * @argc: Number of arguments
59 * @argv: Argument array
60 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
62 * Implement efidebug "devices" sub-command.
63 * Show all UEFI devices and their information.
65 static int do_efi_show_devices(struct cmd_tbl *cmdtp, int flag,
66 int argc, char *const argv[])
68 efi_handle_t *handles;
73 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
75 if (ret != EFI_SUCCESS)
76 return CMD_RET_FAILURE;
79 return CMD_RET_SUCCESS;
81 printf("Device%.*s Device Path\n", EFI_HANDLE_WIDTH - 6, spc);
82 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
83 for (i = 0; i < num; i++) {
84 if (!efi_get_device_handle_info(handles[i], &dev_path_text)) {
85 printf("%p %ls\n", handles[i], dev_path_text);
86 efi_free_pool(dev_path_text);
90 efi_free_pool(handles);
92 return CMD_RET_SUCCESS;
96 * efi_get_driver_handle_info() - get information of UEFI driver
98 * @handle: Handle of UEFI device
99 * @driver_name: Driver name
100 * @image_path: Pointer to text of device path
101 * Return: 0 on success, -1 on failure
103 * Currently return no useful information as all UEFI drivers are
106 static int efi_get_driver_handle_info(efi_handle_t handle, u16 **driver_name,
109 struct efi_handler *handler;
110 struct efi_loaded_image *image;
115 * TODO: support EFI_COMPONENT_NAME2_PROTOCOL
120 ret = efi_search_protocol(handle, &efi_guid_loaded_image, &handler);
121 if (ret != EFI_SUCCESS) {
126 image = handler->protocol_interface;
127 *image_path = efi_dp_str(image->file_path);
133 * do_efi_show_drivers() - show UEFI drivers
135 * @cmdtp: Command table
136 * @flag: Command flag
137 * @argc: Number of arguments
138 * @argv: Argument array
139 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
141 * Implement efidebug "drivers" sub-command.
142 * Show all UEFI drivers and their information.
144 static int do_efi_show_drivers(struct cmd_tbl *cmdtp, int flag,
145 int argc, char *const argv[])
147 efi_handle_t *handles;
149 u16 *driver_name, *image_path_text;
152 ret = EFI_CALL(efi_locate_handle_buffer(
153 BY_PROTOCOL, &efi_guid_driver_binding_protocol,
154 NULL, &num, &handles));
155 if (ret != EFI_SUCCESS)
156 return CMD_RET_FAILURE;
159 return CMD_RET_SUCCESS;
161 printf("Driver%.*s Name Image Path\n",
162 EFI_HANDLE_WIDTH - 6, spc);
163 printf("%.*s ==================== ====================\n",
164 EFI_HANDLE_WIDTH, sep);
165 for (i = 0; i < num; i++) {
166 if (!efi_get_driver_handle_info(handles[i], &driver_name,
169 printf("%p %-20ls %ls\n", handles[i],
170 driver_name, image_path_text);
172 printf("%p %-20ls <built-in>\n",
173 handles[i], driver_name);
174 efi_free_pool(driver_name);
175 efi_free_pool(image_path_text);
179 efi_free_pool(handles);
181 return CMD_RET_SUCCESS;
184 static const struct {
186 const efi_guid_t guid;
190 EFI_DEVICE_PATH_PROTOCOL_GUID,
193 "Device Path To Text",
194 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID,
197 "Device Path Utilities",
198 EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID,
201 "Unicode Collation 2",
202 EFI_UNICODE_COLLATION_PROTOCOL2_GUID,
206 EFI_DRIVER_BINDING_PROTOCOL_GUID,
210 EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID,
213 "Simple Text Input Ex",
214 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID,
217 "Simple Text Output",
218 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID,
222 EFI_BLOCK_IO_PROTOCOL_GUID,
225 "Simple File System",
226 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
230 EFI_LOADED_IMAGE_PROTOCOL_GUID,
234 EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID,
238 EFI_HII_STRING_PROTOCOL_GUID,
242 EFI_HII_DATABASE_PROTOCOL_GUID,
245 "HII Config Routing",
246 EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID,
250 EFI_LOAD_FILE2_PROTOCOL_GUID,
253 "Random Number Generator",
254 EFI_RNG_PROTOCOL_GUID,
258 EFI_SIMPLE_NETWORK_PROTOCOL_GUID,
262 EFI_PXE_BASE_CODE_PROTOCOL_GUID,
264 /* Configuration table GUIDs */
278 "Runtime properties",
279 EFI_RT_PROPERTIES_TABLE_GUID,
284 * get_guid_text - get string of GUID
286 * Return description of GUID.
289 * Return: description of GUID or NULL
291 static const char *get_guid_text(const void *guid)
295 for (i = 0; i < ARRAY_SIZE(guid_list); i++) {
297 * As guidcmp uses memcmp() we can safely accept unaligned
300 if (!guidcmp(&guid_list[i].guid, guid))
301 return guid_list[i].text;
308 * do_efi_show_handles() - show UEFI handles
310 * @cmdtp: Command table
311 * @flag: Command flag
312 * @argc: Number of arguments
313 * @argv: Argument array
314 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
316 * Implement efidebug "dh" sub-command.
317 * Show all UEFI handles and their information, currently all protocols
320 static int do_efi_show_handles(struct cmd_tbl *cmdtp, int flag,
321 int argc, char *const argv[])
323 efi_handle_t *handles;
325 efi_uintn_t num, count, i, j;
326 const char *guid_text;
329 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
331 if (ret != EFI_SUCCESS)
332 return CMD_RET_FAILURE;
335 return CMD_RET_SUCCESS;
337 printf("Handle%.*s Protocols\n", EFI_HANDLE_WIDTH - 6, spc);
338 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
339 for (i = 0; i < num; i++) {
340 printf("%p", handles[i]);
341 ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid,
348 for (j = 0; j < count; j++) {
354 guid_text = get_guid_text(guid[j]);
358 printf("%pUl", guid[j]);
363 efi_free_pool(handles);
365 return CMD_RET_SUCCESS;
369 * do_efi_show_images() - show UEFI images
371 * @cmdtp: Command table
372 * @flag: Command flag
373 * @argc: Number of arguments
374 * @argv: Argument array
375 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
377 * Implement efidebug "images" sub-command.
378 * Show all UEFI loaded images and their information.
380 static int do_efi_show_images(struct cmd_tbl *cmdtp, int flag,
381 int argc, char *const argv[])
383 efi_print_image_infos(NULL);
385 return CMD_RET_SUCCESS;
388 static const char * const efi_mem_type_string[] = {
389 [EFI_RESERVED_MEMORY_TYPE] = "RESERVED",
390 [EFI_LOADER_CODE] = "LOADER CODE",
391 [EFI_LOADER_DATA] = "LOADER DATA",
392 [EFI_BOOT_SERVICES_CODE] = "BOOT CODE",
393 [EFI_BOOT_SERVICES_DATA] = "BOOT DATA",
394 [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE",
395 [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA",
396 [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL",
397 [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM",
398 [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM",
399 [EFI_ACPI_MEMORY_NVS] = "ACPI NVS",
400 [EFI_MMAP_IO] = "IO",
401 [EFI_MMAP_IO_PORT] = "IO PORT",
402 [EFI_PAL_CODE] = "PAL",
403 [EFI_PERSISTENT_MEMORY_TYPE] = "PERSISTENT",
406 static const struct efi_mem_attrs {
409 } efi_mem_attrs[] = {
410 {EFI_MEMORY_UC, "UC"},
411 {EFI_MEMORY_UC, "UC"},
412 {EFI_MEMORY_WC, "WC"},
413 {EFI_MEMORY_WT, "WT"},
414 {EFI_MEMORY_WB, "WB"},
415 {EFI_MEMORY_UCE, "UCE"},
416 {EFI_MEMORY_WP, "WP"},
417 {EFI_MEMORY_RP, "RP"},
418 {EFI_MEMORY_XP, "WP"},
419 {EFI_MEMORY_NV, "NV"},
420 {EFI_MEMORY_MORE_RELIABLE, "REL"},
421 {EFI_MEMORY_RO, "RO"},
422 {EFI_MEMORY_SP, "SP"},
423 {EFI_MEMORY_RUNTIME, "RT"},
427 * print_memory_attributes() - print memory map attributes
429 * @attributes: Attribute value
431 * Print memory map attributes
433 static void print_memory_attributes(u64 attributes)
437 for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
438 if (attributes & efi_mem_attrs[i].bit) {
445 puts(efi_mem_attrs[i].text);
449 #define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
452 * do_efi_show_memmap() - show UEFI memory map
454 * @cmdtp: Command table
455 * @flag: Command flag
456 * @argc: Number of arguments
457 * @argv: Argument array
458 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
460 * Implement efidebug "memmap" sub-command.
461 * Show UEFI memory map.
463 static int do_efi_show_memmap(struct cmd_tbl *cmdtp, int flag,
464 int argc, char *const argv[])
466 struct efi_mem_desc *memmap = NULL, *map;
467 efi_uintn_t map_size = 0;
472 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
473 if (ret == EFI_BUFFER_TOO_SMALL) {
474 map_size += sizeof(struct efi_mem_desc); /* for my own */
475 ret = efi_allocate_pool(EFI_LOADER_DATA, map_size,
477 if (ret != EFI_SUCCESS)
478 return CMD_RET_FAILURE;
479 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
481 if (ret != EFI_SUCCESS) {
482 efi_free_pool(memmap);
483 return CMD_RET_FAILURE;
486 printf("Type Start%.*s End%.*s Attributes\n",
487 EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc);
488 printf("================ %.*s %.*s ==========\n",
489 EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep);
491 * Coverity check: dereferencing null pointer "map."
492 * This is a false positive as memmap will always be
493 * populated by allocate_pool() above.
495 for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
496 if (map->type < ARRAY_SIZE(efi_mem_type_string))
497 type = efi_mem_type_string[map->type];
501 printf("%-16s %.*llx-%.*llx", type,
503 (u64)map_to_sysmem((void *)(uintptr_t)
504 map->physical_start),
506 (u64)map_to_sysmem((void *)(uintptr_t)
507 (map->physical_start +
508 map->num_pages * EFI_PAGE_SIZE)));
510 print_memory_attributes(map->attribute);
514 efi_free_pool(memmap);
516 return CMD_RET_SUCCESS;
520 * do_efi_show_tables() - show UEFI configuration tables
522 * @cmdtp: Command table
523 * @flag: Command flag
524 * @argc: Number of arguments
525 * @argv: Argument array
526 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
528 * Implement efidebug "tables" sub-command.
529 * Show UEFI configuration tables.
531 static int do_efi_show_tables(struct cmd_tbl *cmdtp, int flag,
532 int argc, char *const argv[])
535 const char *guid_str;
537 for (i = 0; i < systab.nr_tables; ++i) {
538 guid_str = get_guid_text(&systab.tables[i].guid);
541 printf("%pUl %s\n", &systab.tables[i].guid, guid_str);
544 return CMD_RET_SUCCESS;
548 * do_efi_boot_add() - set UEFI load option
550 * @cmdtp: Command table
551 * @flag: Command flag
552 * @argc: Number of arguments
553 * @argv: Argument array
554 * Return: CMD_RET_SUCCESS on success,
555 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
557 * Implement efidebug "boot add" sub-command. Create or change UEFI load option.
559 * efidebug boot add <id> <label> <interface> <devnum>[:<part>] <file> <options>
561 static int do_efi_boot_add(struct cmd_tbl *cmdtp, int flag,
562 int argc, char *const argv[])
567 u16 var_name16[9], *p;
569 size_t label_len, label_len16;
571 struct efi_device_path *device_path = NULL, *file_path = NULL;
572 struct efi_load_option lo;
576 int r = CMD_RET_SUCCESS;
578 if (argc < 6 || argc > 7)
579 return CMD_RET_USAGE;
581 id = (int)simple_strtoul(argv[1], &endp, 16);
582 if (*endp != '\0' || id > 0xffff)
583 return CMD_RET_USAGE;
585 sprintf(var_name, "Boot%04X", id);
587 utf8_utf16_strncpy(&p, var_name, 9);
589 guid = efi_global_variable_guid;
592 lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
595 label_len = strlen(argv[2]);
596 label_len16 = utf8_utf16_strnlen(argv[2], label_len);
597 label = malloc((label_len16 + 1) * sizeof(u16));
599 return CMD_RET_FAILURE;
600 lo.label = label; /* label will be changed below */
601 utf8_utf16_strncpy(&label, argv[2], label_len);
604 ret = efi_dp_from_name(argv[3], argv[4], argv[5], &device_path,
606 if (ret != EFI_SUCCESS) {
607 printf("Cannot create device path for \"%s %s\"\n",
612 lo.file_path = file_path;
613 lo.file_path_length = efi_dp_size(file_path)
614 + sizeof(struct efi_device_path); /* for END */
618 lo.optional_data = NULL;
620 lo.optional_data = (const u8 *)argv[6];
622 size = efi_serialize_load_option(&lo, (u8 **)&data);
628 ret = EFI_CALL(efi_set_variable(var_name16, &guid,
629 EFI_VARIABLE_NON_VOLATILE |
630 EFI_VARIABLE_BOOTSERVICE_ACCESS |
631 EFI_VARIABLE_RUNTIME_ACCESS,
633 if (ret != EFI_SUCCESS) {
634 printf("Cannot set %ls\n", var_name16);
639 efi_free_pool(device_path);
640 efi_free_pool(file_path);
647 * do_efi_boot_rm() - delete UEFI load options
649 * @cmdtp: Command table
650 * @flag: Command flag
651 * @argc: Number of arguments
652 * @argv: Argument array
653 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
655 * Implement efidebug "boot rm" sub-command.
656 * Delete UEFI load options.
658 * efidebug boot rm <id> ...
660 static int do_efi_boot_rm(struct cmd_tbl *cmdtp, int flag,
661 int argc, char *const argv[])
667 u16 var_name16[9], *p;
671 return CMD_RET_USAGE;
673 guid = efi_global_variable_guid;
674 for (i = 1; i < argc; i++, argv++) {
675 id = (int)simple_strtoul(argv[1], &endp, 16);
676 if (*endp != '\0' || id > 0xffff)
677 return CMD_RET_FAILURE;
679 sprintf(var_name, "Boot%04X", id);
681 utf8_utf16_strncpy(&p, var_name, 9);
683 ret = EFI_CALL(efi_set_variable(var_name16, &guid, 0, 0, NULL));
685 printf("Cannot remove %ls\n", var_name16);
686 return CMD_RET_FAILURE;
690 return CMD_RET_SUCCESS;
694 * show_efi_boot_opt_data() - dump UEFI load option
696 * @varname16: variable name
697 * @data: value of UEFI load option variable
698 * @size: size of the boot option
700 * Decode the value of UEFI load option variable and print information.
702 static void show_efi_boot_opt_data(u16 *varname16, void *data, size_t *size)
704 struct efi_load_option lo;
706 size_t label_len16, label_len;
710 ret = efi_deserialize_load_option(&lo, data, size);
711 if (ret != EFI_SUCCESS) {
712 printf("%ls: invalid load option\n", varname16);
716 label_len16 = u16_strlen(lo.label);
717 label_len = utf16_utf8_strnlen(lo.label, label_len16);
718 label = malloc(label_len + 1);
722 utf16_utf8_strncpy(&p, lo.label, label_len16);
724 printf("%ls:\nattributes: %c%c%c (0x%08x)\n",
727 lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
728 /* FORCE RECONNECT */
729 lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
731 lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
733 printf(" label: %s\n", label);
735 dp_str = efi_dp_str(lo.file_path);
736 printf(" file_path: %ls\n", dp_str);
737 efi_free_pool(dp_str);
740 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
741 lo.optional_data, *size, true);
746 * show_efi_boot_opt() - dump UEFI load option
748 * @varname16: variable name
750 * Dump information defined by UEFI load option.
752 static void show_efi_boot_opt(u16 *varname16)
759 ret = EFI_CALL(efi_get_variable(varname16, &efi_global_variable_guid,
761 if (ret == EFI_BUFFER_TOO_SMALL) {
764 printf("ERROR: Out of memory\n");
767 ret = EFI_CALL(efi_get_variable(varname16,
768 &efi_global_variable_guid,
770 if (ret == EFI_SUCCESS)
771 show_efi_boot_opt_data(varname16, data, &size);
776 static int u16_tohex(u16 c)
778 if (c >= '0' && c <= '9')
780 if (c >= 'A' && c <= 'F')
783 /* not hexadecimal */
788 * show_efi_boot_dump() - dump all UEFI load options
790 * @cmdtp: Command table
791 * @flag: Command flag
792 * @argc: Number of arguments
793 * @argv: Argument array
794 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
796 * Implement efidebug "boot dump" sub-command.
797 * Dump information of all UEFI load options defined.
801 static int do_efi_boot_dump(struct cmd_tbl *cmdtp, int flag,
802 int argc, char *const argv[])
805 efi_uintn_t buf_size, size;
811 return CMD_RET_USAGE;
814 var_name16 = malloc(buf_size);
816 return CMD_RET_FAILURE;
821 ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
823 if (ret == EFI_NOT_FOUND)
825 if (ret == EFI_BUFFER_TOO_SMALL) {
827 p = realloc(var_name16, buf_size);
830 return CMD_RET_FAILURE;
833 ret = EFI_CALL(efi_get_next_variable_name(&size,
837 if (ret != EFI_SUCCESS) {
839 return CMD_RET_FAILURE;
842 if (memcmp(var_name16, L"Boot", 8))
845 for (id = 0, i = 0; i < 4; i++) {
846 digit = u16_tohex(var_name16[4 + i]);
849 id = (id << 4) + digit;
851 if (i == 4 && !var_name16[8])
852 show_efi_boot_opt(var_name16);
857 return CMD_RET_SUCCESS;
861 * show_efi_boot_order() - show order of UEFI load options
863 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
865 * Show order of UEFI load options defined by BootOrder variable.
867 static int show_efi_boot_order(void)
873 u16 var_name16[9], *p16;
875 struct efi_load_option lo;
877 size_t label_len16, label_len;
881 ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid,
883 if (ret != EFI_BUFFER_TOO_SMALL) {
884 if (ret == EFI_NOT_FOUND) {
885 printf("BootOrder not defined\n");
886 return CMD_RET_SUCCESS;
888 return CMD_RET_FAILURE;
891 bootorder = malloc(size);
893 printf("ERROR: Out of memory\n");
894 return CMD_RET_FAILURE;
896 ret = EFI_CALL(efi_get_variable(L"BootOrder", &efi_global_variable_guid,
897 NULL, &size, bootorder));
898 if (ret != EFI_SUCCESS) {
899 ret = CMD_RET_FAILURE;
903 num = size / sizeof(u16);
904 for (i = 0; i < num; i++) {
905 sprintf(var_name, "Boot%04X", bootorder[i]);
907 utf8_utf16_strncpy(&p16, var_name, 9);
910 ret = EFI_CALL(efi_get_variable(var_name16,
911 &efi_global_variable_guid, NULL,
913 if (ret != EFI_BUFFER_TOO_SMALL) {
914 printf("%2d: %s: (not defined)\n", i + 1, var_name);
920 ret = CMD_RET_FAILURE;
923 ret = EFI_CALL(efi_get_variable(var_name16,
924 &efi_global_variable_guid, NULL,
926 if (ret != EFI_SUCCESS) {
928 ret = CMD_RET_FAILURE;
932 ret = efi_deserialize_load_option(&lo, data, &size);
933 if (ret != EFI_SUCCESS) {
934 printf("%ls: invalid load option\n", var_name16);
935 ret = CMD_RET_FAILURE;
939 label_len16 = u16_strlen(lo.label);
940 label_len = utf16_utf8_strnlen(lo.label, label_len16);
941 label = malloc(label_len + 1);
944 ret = CMD_RET_FAILURE;
948 utf16_utf8_strncpy(&p, lo.label, label_len16);
949 printf("%2d: %s: %s\n", i + 1, var_name, label);
961 * do_efi_boot_next() - manage UEFI BootNext variable
963 * @cmdtp: Command table
964 * @flag: Command flag
965 * @argc: Number of arguments
966 * @argv: Argument array
967 * Return: CMD_RET_SUCCESS on success,
968 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
970 * Implement efidebug "boot next" sub-command.
971 * Set BootNext variable.
973 * efidebug boot next <id>
975 static int do_efi_boot_next(struct cmd_tbl *cmdtp, int flag,
976 int argc, char *const argv[])
983 int r = CMD_RET_SUCCESS;
986 return CMD_RET_USAGE;
988 bootnext = (u16)simple_strtoul(argv[1], &endp, 16);
990 printf("invalid value: %s\n", argv[1]);
995 guid = efi_global_variable_guid;
997 ret = EFI_CALL(efi_set_variable(L"BootNext", &guid,
998 EFI_VARIABLE_NON_VOLATILE |
999 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1000 EFI_VARIABLE_RUNTIME_ACCESS,
1002 if (ret != EFI_SUCCESS) {
1003 printf("Cannot set BootNext\n");
1004 r = CMD_RET_FAILURE;
1011 * do_efi_boot_order() - manage UEFI BootOrder variable
1013 * @cmdtp: Command table
1014 * @flag: Command flag
1015 * @argc: Number of arguments
1016 * @argv: Argument array
1017 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1019 * Implement efidebug "boot order" sub-command.
1020 * Show order of UEFI load options, or change it in BootOrder variable.
1022 * efidebug boot order [<id> ...]
1024 static int do_efi_boot_order(struct cmd_tbl *cmdtp, int flag,
1025 int argc, char *const argv[])
1027 u16 *bootorder = NULL;
1033 int r = CMD_RET_SUCCESS;
1036 return show_efi_boot_order();
1041 size = argc * sizeof(u16);
1042 bootorder = malloc(size);
1044 return CMD_RET_FAILURE;
1046 for (i = 0; i < argc; i++) {
1047 id = (int)simple_strtoul(argv[i], &endp, 16);
1048 if (*endp != '\0' || id > 0xffff) {
1049 printf("invalid value: %s\n", argv[i]);
1050 r = CMD_RET_FAILURE;
1054 bootorder[i] = (u16)id;
1057 guid = efi_global_variable_guid;
1058 ret = EFI_CALL(efi_set_variable(L"BootOrder", &guid,
1059 EFI_VARIABLE_NON_VOLATILE |
1060 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1061 EFI_VARIABLE_RUNTIME_ACCESS,
1063 if (ret != EFI_SUCCESS) {
1064 printf("Cannot set BootOrder\n");
1065 r = CMD_RET_FAILURE;
1073 static struct cmd_tbl cmd_efidebug_boot_sub[] = {
1074 U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
1075 U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
1076 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
1077 U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
1078 U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
1083 * do_efi_boot_opt() - manage UEFI load options
1085 * @cmdtp: Command table
1086 * @flag: Command flag
1087 * @argc: Number of arguments
1088 * @argv: Argument array
1089 * Return: CMD_RET_SUCCESS on success,
1090 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1092 * Implement efidebug "boot" sub-command.
1094 static int do_efi_boot_opt(struct cmd_tbl *cmdtp, int flag,
1095 int argc, char *const argv[])
1100 return CMD_RET_USAGE;
1104 cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
1105 ARRAY_SIZE(cmd_efidebug_boot_sub));
1107 return CMD_RET_USAGE;
1109 return cp->cmd(cmdtp, flag, argc, argv);
1113 * do_efi_test_bootmgr() - run simple bootmgr for test
1115 * @cmdtp: Command table
1116 * @flag: Command flag
1117 * @argc: Number of arguments
1118 * @argv: Argument array
1119 * Return: CMD_RET_SUCCESS on success,
1120 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1122 * Implement efidebug "test bootmgr" sub-command.
1123 * Run simple bootmgr for test.
1125 * efidebug test bootmgr
1127 static int do_efi_test_bootmgr(struct cmd_tbl *cmdtp, int flag,
1128 int argc, char * const argv[])
1131 efi_uintn_t exit_data_size = 0;
1132 u16 *exit_data = NULL;
1134 void *load_options = NULL;
1136 ret = efi_bootmgr_load(&image, &load_options);
1137 printf("efi_bootmgr_load() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1139 /* We call efi_start_image() even if error for test purpose. */
1140 ret = EFI_CALL(efi_start_image(image, &exit_data_size, &exit_data));
1141 printf("efi_start_image() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1142 if (ret && exit_data)
1143 efi_free_pool(exit_data);
1148 return CMD_RET_SUCCESS;
1151 static struct cmd_tbl cmd_efidebug_test_sub[] = {
1152 U_BOOT_CMD_MKENT(bootmgr, CONFIG_SYS_MAXARGS, 1, do_efi_test_bootmgr,
1157 * do_efi_test() - manage UEFI load options
1159 * @cmdtp: Command table
1160 * @flag: Command flag
1161 * @argc: Number of arguments
1162 * @argv: Argument array
1163 * Return: CMD_RET_SUCCESS on success,
1164 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1166 * Implement efidebug "test" sub-command.
1168 static int do_efi_test(struct cmd_tbl *cmdtp, int flag,
1169 int argc, char * const argv[])
1174 return CMD_RET_USAGE;
1178 cp = find_cmd_tbl(argv[0], cmd_efidebug_test_sub,
1179 ARRAY_SIZE(cmd_efidebug_test_sub));
1181 return CMD_RET_USAGE;
1183 return cp->cmd(cmdtp, flag, argc, argv);
1187 * do_efi_query_info() - QueryVariableInfo EFI service
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_FAILURE on failure
1196 * Implement efidebug "test" sub-command.
1199 static int do_efi_query_info(struct cmd_tbl *cmdtp, int flag,
1200 int argc, char * const argv[])
1204 u64 max_variable_storage_size;
1205 u64 remain_variable_storage_size;
1206 u64 max_variable_size;
1209 for (i = 1; i < argc; i++) {
1210 if (!strcmp(argv[i], "-bs"))
1211 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
1212 else if (!strcmp(argv[i], "-rt"))
1213 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
1214 else if (!strcmp(argv[i], "-nv"))
1215 attr |= EFI_VARIABLE_NON_VOLATILE;
1216 else if (!strcmp(argv[i], "-at"))
1218 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
1221 ret = EFI_CALL(efi_query_variable_info(attr,
1222 &max_variable_storage_size,
1223 &remain_variable_storage_size,
1224 &max_variable_size));
1225 if (ret != EFI_SUCCESS) {
1226 printf("Error: Cannot query UEFI variables, r = %lu\n",
1227 ret & ~EFI_ERROR_MASK);
1228 return CMD_RET_FAILURE;
1231 printf("Max storage size %llu\n", max_variable_storage_size);
1232 printf("Remaining storage size %llu\n", remain_variable_storage_size);
1233 printf("Max variable size %llu\n", max_variable_size);
1235 return CMD_RET_SUCCESS;
1238 static struct cmd_tbl cmd_efidebug_sub[] = {
1239 U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
1240 U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices,
1242 U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
1244 U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
1246 U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
1248 U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
1250 U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables,
1252 U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_efi_test,
1254 U_BOOT_CMD_MKENT(query, CONFIG_SYS_MAXARGS, 1, do_efi_query_info,
1259 * do_efidebug() - display and configure UEFI environment
1261 * @cmdtp: Command table
1262 * @flag: Command flag
1263 * @argc: Number of arguments
1264 * @argv: Argument array
1265 * Return: CMD_RET_SUCCESS on success,
1266 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1268 * Implement efidebug command which allows us to display and
1269 * configure UEFI environment.
1271 static int do_efidebug(struct cmd_tbl *cmdtp, int flag,
1272 int argc, char *const argv[])
1278 return CMD_RET_USAGE;
1282 /* Initialize UEFI drivers */
1283 r = efi_init_obj_list();
1284 if (r != EFI_SUCCESS) {
1285 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1286 r & ~EFI_ERROR_MASK);
1287 return CMD_RET_FAILURE;
1290 cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
1291 ARRAY_SIZE(cmd_efidebug_sub));
1293 return CMD_RET_USAGE;
1295 return cp->cmd(cmdtp, flag, argc, argv);
1298 #ifdef CONFIG_SYS_LONGHELP
1299 static char efidebug_help_text[] =
1300 " - UEFI Shell-like interface to configure UEFI environment\n"
1302 "efidebug boot add <bootid> <label> <interface> <devnum>[:<part>] <file path> [<load options>]\n"
1303 " - set UEFI BootXXXX variable\n"
1304 " <load options> will be passed to UEFI application\n"
1305 "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
1306 " - delete UEFI BootXXXX variables\n"
1307 "efidebug boot dump\n"
1308 " - dump all UEFI BootXXXX variables\n"
1309 "efidebug boot next <bootid>\n"
1310 " - set UEFI BootNext variable\n"
1311 "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
1312 " - set/show UEFI boot order\n"
1314 "efidebug devices\n"
1315 " - show UEFI devices\n"
1316 "efidebug drivers\n"
1317 " - show UEFI drivers\n"
1319 " - show UEFI handles\n"
1321 " - show loaded images\n"
1323 " - show UEFI memory map\n"
1325 " - show UEFI configuration tables\n"
1326 "efidebug test bootmgr\n"
1327 " - run simple bootmgr for test\n"
1328 "efidebug query [-nv][-bs][-rt][-at]\n"
1329 " - show size of UEFI variables store\n";
1333 efidebug, 10, 0, do_efidebug,
1334 "Configure UEFI environment",