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>
12 #include <environment.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,
257 * get_guid_text - get string of protocol guid
258 * @guid: Protocol guid
261 * Return string for display to represent the protocol.
263 static const char *get_guid_text(const efi_guid_t *guid)
267 for (i = 0; i < ARRAY_SIZE(guid_list); i++)
268 if (!guidcmp(&guid_list[i].guid, guid))
271 if (i != ARRAY_SIZE(guid_list))
272 return guid_list[i].text;
278 * do_efi_show_handles() - show UEFI handles
280 * @cmdtp: Command table
281 * @flag: Command flag
282 * @argc: Number of arguments
283 * @argv: Argument array
284 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
286 * Implement efidebug "dh" sub-command.
287 * Show all UEFI handles and their information, currently all protocols
290 static int do_efi_show_handles(cmd_tbl_t *cmdtp, int flag,
291 int argc, char * const argv[])
293 efi_handle_t *handles;
295 efi_uintn_t num, count, i, j;
296 const char *guid_text;
299 ret = EFI_CALL(BS->locate_handle_buffer(ALL_HANDLES, NULL, NULL,
301 if (ret != EFI_SUCCESS)
302 return CMD_RET_FAILURE;
305 return CMD_RET_SUCCESS;
307 printf("Handle%.*s Protocols\n", EFI_HANDLE_WIDTH - 6, spc);
308 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
309 for (i = 0; i < num; i++) {
310 printf("%p", handles[i]);
311 ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid,
318 for (j = 0; j < count; j++) {
324 guid_text = get_guid_text(guid[j]);
328 printf("%pUl", guid[j]);
333 EFI_CALL(BS->free_pool(handles));
335 return CMD_RET_SUCCESS;
339 * do_efi_show_images() - show UEFI images
341 * @cmdtp: Command table
342 * @flag: Command flag
343 * @argc: Number of arguments
344 * @argv: Argument array
345 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
347 * Implement efidebug "images" sub-command.
348 * Show all UEFI loaded images and their information.
350 static int do_efi_show_images(cmd_tbl_t *cmdtp, int flag,
351 int argc, char * const argv[])
353 efi_print_image_infos(NULL);
355 return CMD_RET_SUCCESS;
358 static const char * const efi_mem_type_string[] = {
359 [EFI_RESERVED_MEMORY_TYPE] = "RESERVED",
360 [EFI_LOADER_CODE] = "LOADER CODE",
361 [EFI_LOADER_DATA] = "LOADER DATA",
362 [EFI_BOOT_SERVICES_CODE] = "BOOT CODE",
363 [EFI_BOOT_SERVICES_DATA] = "BOOT DATA",
364 [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE",
365 [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA",
366 [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL",
367 [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM",
368 [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM",
369 [EFI_ACPI_MEMORY_NVS] = "ACPI NVS",
370 [EFI_MMAP_IO] = "IO",
371 [EFI_MMAP_IO_PORT] = "IO PORT",
372 [EFI_PAL_CODE] = "PAL",
375 static const struct efi_mem_attrs {
378 } efi_mem_attrs[] = {
379 {EFI_MEMORY_UC, "UC"},
380 {EFI_MEMORY_UC, "UC"},
381 {EFI_MEMORY_WC, "WC"},
382 {EFI_MEMORY_WT, "WT"},
383 {EFI_MEMORY_WB, "WB"},
384 {EFI_MEMORY_UCE, "UCE"},
385 {EFI_MEMORY_WP, "WP"},
386 {EFI_MEMORY_RP, "RP"},
387 {EFI_MEMORY_XP, "WP"},
388 {EFI_MEMORY_NV, "NV"},
389 {EFI_MEMORY_MORE_RELIABLE, "REL"},
390 {EFI_MEMORY_RO, "RO"},
391 {EFI_MEMORY_RUNTIME, "RT"},
395 * print_memory_attributes() - print memory map attributes
396 * @attributes: Attribute value
398 * Print memory map attributes
400 static void print_memory_attributes(u64 attributes)
404 for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
405 if (attributes & efi_mem_attrs[i].bit) {
412 puts(efi_mem_attrs[i].text);
416 #define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
419 * do_efi_show_memmap() - show UEFI memory map
421 * @cmdtp: Command table
422 * @flag: Command flag
423 * @argc: Number of arguments
424 * @argv: Argument array
425 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
427 * Implement efidebug "memmap" sub-command.
428 * Show UEFI memory map.
430 static int do_efi_show_memmap(cmd_tbl_t *cmdtp, int flag,
431 int argc, char * const argv[])
433 struct efi_mem_desc *memmap = NULL, *map;
434 efi_uintn_t map_size = 0;
439 ret = EFI_CALL(BS->get_memory_map(&map_size, memmap, NULL, NULL, NULL));
440 if (ret == EFI_BUFFER_TOO_SMALL) {
441 map_size += sizeof(struct efi_mem_desc); /* for my own */
442 ret = EFI_CALL(BS->allocate_pool(EFI_LOADER_DATA,
443 map_size, (void *)&memmap));
444 if (ret != EFI_SUCCESS)
445 return CMD_RET_FAILURE;
446 ret = EFI_CALL(BS->get_memory_map(&map_size, memmap,
449 if (ret != EFI_SUCCESS) {
450 EFI_CALL(BS->free_pool(memmap));
451 return CMD_RET_FAILURE;
454 printf("Type Start%.*s End%.*s Attributes\n",
455 EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc);
456 printf("================ %.*s %.*s ==========\n",
457 EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep);
458 for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
459 if (map->type < EFI_MAX_MEMORY_TYPE)
460 type = efi_mem_type_string[map->type];
464 printf("%-16s %.*llx-%.*llx", type,
468 map->physical_start + map->num_pages * EFI_PAGE_SIZE);
470 print_memory_attributes(map->attribute);
474 EFI_CALL(BS->free_pool(memmap));
476 return CMD_RET_SUCCESS;
480 * do_efi_boot_add() - set UEFI load option
482 * @cmdtp: Command table
483 * @flag: Command flag
484 * @argc: Number of arguments
485 * @argv: Argument array
486 * Return: CMD_RET_SUCCESS on success,
487 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
489 * Implement efidebug "boot add" sub-command.
490 * Create or change UEFI load option.
491 * - boot add <id> <label> <interface> <devnum>[:<part>] <file> <options>
493 static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag,
494 int argc, char * const argv[])
499 u16 var_name16[9], *p;
501 size_t label_len, label_len16;
503 struct efi_device_path *device_path = NULL, *file_path = NULL;
504 struct efi_load_option lo;
509 if (argc < 6 || argc > 7)
510 return CMD_RET_USAGE;
512 id = (int)simple_strtoul(argv[1], &endp, 16);
513 if (*endp != '\0' || id > 0xffff)
514 return CMD_RET_USAGE;
516 sprintf(var_name, "Boot%04X", id);
518 utf8_utf16_strncpy(&p, var_name, 9);
520 guid = efi_global_variable_guid;
523 lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
526 label_len = strlen(argv[2]);
527 label_len16 = utf8_utf16_strnlen(argv[2], label_len);
528 label = malloc((label_len16 + 1) * sizeof(u16));
530 return CMD_RET_FAILURE;
531 lo.label = label; /* label will be changed below */
532 utf8_utf16_strncpy(&label, argv[2], label_len);
535 ret = efi_dp_from_name(argv[3], argv[4], argv[5], &device_path,
537 if (ret != EFI_SUCCESS) {
538 printf("Cannot create device path for \"%s %s\"\n",
540 ret = CMD_RET_FAILURE;
543 lo.file_path = file_path;
544 lo.file_path_length = efi_dp_size(file_path)
545 + sizeof(struct efi_device_path); /* for END */
548 lo.optional_data = (u8 *)(argc == 6 ? "" : argv[6]);
550 size = efi_serialize_load_option(&lo, (u8 **)&data);
552 ret = CMD_RET_FAILURE;
556 ret = EFI_CALL(RT->set_variable(var_name16, &guid,
557 EFI_VARIABLE_BOOTSERVICE_ACCESS |
558 EFI_VARIABLE_RUNTIME_ACCESS,
560 ret = (ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE);
563 efi_free_pool(device_path);
564 efi_free_pool(file_path);
571 * do_efi_boot_rm() - delete UEFI load options
573 * @cmdtp: Command table
574 * @flag: Command flag
575 * @argc: Number of arguments
576 * @argv: Argument array
577 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
579 * Implement efidebug "boot rm" sub-command.
580 * Delete UEFI load options.
583 static int do_efi_boot_rm(cmd_tbl_t *cmdtp, int flag,
584 int argc, char * const argv[])
594 return CMD_RET_USAGE;
596 guid = efi_global_variable_guid;
597 for (i = 1; i < argc; i++, argv++) {
598 id = (int)simple_strtoul(argv[1], &endp, 16);
599 if (*endp != '\0' || id > 0xffff)
600 return CMD_RET_FAILURE;
602 sprintf(var_name, "Boot%04X", id);
603 utf8_utf16_strncpy((u16 **)&var_name16, var_name, 9);
605 ret = EFI_CALL(RT->set_variable(var_name16, &guid, 0, 0, NULL));
607 printf("cannot remove Boot%04X", id);
608 return CMD_RET_FAILURE;
612 return CMD_RET_SUCCESS;
616 * show_efi_boot_opt_data() - dump UEFI load option
618 * @id: Load option number
619 * @data: Value of UEFI load option variable
621 * Decode the value of UEFI load option variable and print information.
623 static void show_efi_boot_opt_data(int id, void *data)
625 struct efi_load_option lo;
627 size_t label_len16, label_len;
630 efi_deserialize_load_option(&lo, data);
632 label_len16 = u16_strlen(lo.label);
633 label_len = utf16_utf8_strnlen(lo.label, label_len16);
634 label = malloc(label_len + 1);
638 utf16_utf8_strncpy(&p, lo.label, label_len16);
640 printf("Boot%04X:\n", id);
641 printf("\tattributes: %c%c%c (0x%08x)\n",
643 lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
644 /* FORCE RECONNECT */
645 lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
647 lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
649 printf("\tlabel: %s\n", label);
651 dp_str = efi_dp_str(lo.file_path);
652 printf("\tfile_path: %ls\n", dp_str);
653 efi_free_pool(dp_str);
655 printf("\tdata: %s\n", lo.optional_data);
661 * show_efi_boot_opt() - dump UEFI load option
663 * @id: Load option number
665 * Dump information defined by UEFI load option.
667 static void show_efi_boot_opt(int id)
670 u16 var_name16[9], *p;
676 sprintf(var_name, "Boot%04X", id);
678 utf8_utf16_strncpy(&p, var_name, 9);
679 guid = efi_global_variable_guid;
682 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, NULL));
683 if (ret == (int)EFI_BUFFER_TOO_SMALL) {
685 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
688 if (ret == EFI_SUCCESS)
689 show_efi_boot_opt_data(id, data);
690 else if (ret == EFI_NOT_FOUND)
691 printf("Boot%04X: not found\n", id);
696 static int u16_tohex(u16 c)
698 if (c >= '0' && c <= '9')
700 if (c >= 'A' && c <= 'F')
703 /* not hexadecimal */
708 * show_efi_boot_dump() - dump all UEFI load options
710 * @cmdtp: Command table
711 * @flag: Command flag
712 * @argc: Number of arguments
713 * @argv: Argument array
714 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
716 * Implement efidebug "boot dump" sub-command.
717 * Dump information of all UEFI load options defined.
720 static int do_efi_boot_dump(cmd_tbl_t *cmdtp, int flag,
721 int argc, char * const argv[])
724 efi_uintn_t buf_size, size;
730 return CMD_RET_USAGE;
733 var_name16 = malloc(buf_size);
735 return CMD_RET_FAILURE;
740 ret = EFI_CALL(efi_get_next_variable_name(&size, var_name16,
742 if (ret == EFI_NOT_FOUND)
744 if (ret == EFI_BUFFER_TOO_SMALL) {
746 p = realloc(var_name16, buf_size);
749 return CMD_RET_FAILURE;
752 ret = EFI_CALL(efi_get_next_variable_name(&size,
756 if (ret != EFI_SUCCESS) {
758 return CMD_RET_FAILURE;
761 if (memcmp(var_name16, L"Boot", 8))
764 for (id = 0, i = 0; i < 4; i++) {
765 digit = u16_tohex(var_name16[4 + i]);
768 id = (id << 4) + digit;
770 if (i == 4 && !var_name16[8])
771 show_efi_boot_opt(id);
776 return CMD_RET_SUCCESS;
780 * show_efi_boot_order() - show order of UEFI load options
782 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
784 * Show order of UEFI load options defined by BootOrder variable.
786 static int show_efi_boot_order(void)
789 u16 *bootorder = NULL;
793 u16 var_name16[9], *p16;
795 struct efi_load_option lo;
797 size_t label_len16, label_len;
800 guid = efi_global_variable_guid;
802 ret = EFI_CALL(RT->get_variable(L"BootOrder", &guid, NULL, &size,
804 if (ret == EFI_BUFFER_TOO_SMALL) {
805 bootorder = malloc(size);
806 ret = EFI_CALL(RT->get_variable(L"BootOrder", &guid, NULL,
809 if (ret == EFI_NOT_FOUND) {
810 printf("BootOrder not defined\n");
811 ret = CMD_RET_SUCCESS;
813 } else if (ret != EFI_SUCCESS) {
814 ret = CMD_RET_FAILURE;
818 num = size / sizeof(u16);
819 for (i = 0; i < num; i++) {
820 sprintf(var_name, "Boot%04X", bootorder[i]);
822 utf8_utf16_strncpy(&p16, var_name, 9);
825 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
827 if (ret != EFI_BUFFER_TOO_SMALL) {
828 printf("%2d: Boot%04X: (not defined)\n",
829 i + 1, bootorder[i]);
835 ret = CMD_RET_FAILURE;
838 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
840 if (ret != EFI_SUCCESS) {
842 ret = CMD_RET_FAILURE;
846 efi_deserialize_load_option(&lo, data);
848 label_len16 = u16_strlen(lo.label);
849 label_len = utf16_utf8_strnlen(lo.label, label_len16);
850 label = malloc(label_len + 1);
853 ret = CMD_RET_FAILURE;
857 utf16_utf8_strncpy(&p, lo.label, label_len16);
858 printf("%2d: Boot%04X: %s\n", i + 1, bootorder[i], label);
870 * do_efi_boot_next() - manage UEFI BootNext variable
872 * @cmdtp: Command table
873 * @flag: Command flag
874 * @argc: Number of arguments
875 * @argv: Argument array
876 * Return: CMD_RET_SUCCESS on success,
877 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
879 * Implement efidebug "boot next" sub-command.
880 * Set BootNext variable.
883 static int do_efi_boot_next(cmd_tbl_t *cmdtp, int flag,
884 int argc, char * const argv[])
893 return CMD_RET_USAGE;
895 bootnext = (u16)simple_strtoul(argv[1], &endp, 16);
896 if (*endp != '\0' || bootnext > 0xffff) {
897 printf("invalid value: %s\n", argv[1]);
898 ret = CMD_RET_FAILURE;
902 guid = efi_global_variable_guid;
904 ret = EFI_CALL(RT->set_variable(L"BootNext", &guid,
905 EFI_VARIABLE_BOOTSERVICE_ACCESS |
906 EFI_VARIABLE_RUNTIME_ACCESS,
908 ret = (ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE);
914 * do_efi_boot_order() - manage UEFI BootOrder variable
916 * @cmdtp: Command table
917 * @flag: Command flag
918 * @argc: Number of arguments
919 * @argv: Argument array
920 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
922 * Implement efidebug "boot order" sub-command.
923 * Show order of UEFI load options, or change it in BootOrder variable.
924 * - boot order [<id> ...]
926 static int do_efi_boot_order(cmd_tbl_t *cmdtp, int flag,
927 int argc, char * const argv[])
929 u16 *bootorder = NULL;
937 return show_efi_boot_order();
942 size = argc * sizeof(u16);
943 bootorder = malloc(size);
945 return CMD_RET_FAILURE;
947 for (i = 0; i < argc; i++) {
948 id = (int)simple_strtoul(argv[i], &endp, 16);
949 if (*endp != '\0' || id > 0xffff) {
950 printf("invalid value: %s\n", argv[i]);
951 ret = CMD_RET_FAILURE;
955 bootorder[i] = (u16)id;
958 guid = efi_global_variable_guid;
959 ret = EFI_CALL(RT->set_variable(L"BootOrder", &guid,
960 EFI_VARIABLE_BOOTSERVICE_ACCESS |
961 EFI_VARIABLE_RUNTIME_ACCESS,
963 ret = (ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE);
970 static cmd_tbl_t cmd_efidebug_boot_sub[] = {
971 U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
972 U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
973 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
974 U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
975 U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
980 * do_efi_boot_opt() - manage UEFI load options
982 * @cmdtp: Command table
983 * @flag: Command flag
984 * @argc: Number of arguments
985 * @argv: Argument array
986 * Return: CMD_RET_SUCCESS on success,
987 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
989 * Implement efidebug "boot" sub-command.
990 * See above for details of sub-commands.
992 static int do_efi_boot_opt(cmd_tbl_t *cmdtp, int flag,
993 int argc, char * const argv[])
998 return CMD_RET_USAGE;
1002 cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
1003 ARRAY_SIZE(cmd_efidebug_boot_sub));
1005 return CMD_RET_USAGE;
1007 return cp->cmd(cmdtp, flag, argc, argv);
1010 static cmd_tbl_t cmd_efidebug_sub[] = {
1011 U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
1012 U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices,
1014 U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
1016 U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
1018 U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
1020 U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
1025 * do_efidebug() - display and configure UEFI environment
1027 * @cmdtp: Command table
1028 * @flag: Command flag
1029 * @argc: Number of arguments
1030 * @argv: Argument array
1031 * Return: CMD_RET_SUCCESS on success,
1032 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1034 * Implement efidebug command which allows us to display and
1035 * configure UEFI environment.
1036 * See above for details of sub-commands.
1038 static int do_efidebug(cmd_tbl_t *cmdtp, int flag,
1039 int argc, char * const argv[])
1045 return CMD_RET_USAGE;
1049 /* Initialize UEFI drivers */
1050 r = efi_init_obj_list();
1051 if (r != EFI_SUCCESS) {
1052 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1053 r & ~EFI_ERROR_MASK);
1054 return CMD_RET_FAILURE;
1057 cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
1058 ARRAY_SIZE(cmd_efidebug_sub));
1060 return CMD_RET_USAGE;
1062 return cp->cmd(cmdtp, flag, argc, argv);
1065 #ifdef CONFIG_SYS_LONGHELP
1066 static char efidebug_help_text[] =
1067 " - UEFI Shell-like interface to configure UEFI environment\n"
1069 "efidebug boot add <bootid> <label> <interface> <devnum>[:<part>] <file path> [<load options>]\n"
1070 " - set UEFI BootXXXX variable\n"
1071 " <load options> will be passed to UEFI application\n"
1072 "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
1073 " - delete UEFI BootXXXX variables\n"
1074 "efidebug boot dump\n"
1075 " - dump all UEFI BootXXXX variables\n"
1076 "efidebug boot next <bootid>\n"
1077 " - set UEFI BootNext variable\n"
1078 "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
1079 " - set/show UEFI boot order\n"
1081 "efidebug devices\n"
1082 " - show uefi devices\n"
1083 "efidebug drivers\n"
1084 " - show uefi drivers\n"
1086 " - show uefi handles\n"
1088 " - show loaded images\n"
1090 " - show uefi memory map\n";
1094 efidebug, 10, 0, do_efidebug,
1095 "Configure UEFI environment",