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;
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,
223 "Simple File System",
224 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
228 LOADED_IMAGE_PROTOCOL_GUID,
237 * get_guid_text - get string of protocol guid
238 * @guid: Protocol guid
241 * Return string for display to represent the protocol.
243 static const char *get_guid_text(const efi_guid_t *guid)
247 for (i = 0; i < ARRAY_SIZE(guid_list); i++)
248 if (!guidcmp(&guid_list[i].guid, guid))
251 if (i != ARRAY_SIZE(guid_list))
252 return guid_list[i].text;
258 * do_efi_show_handles() - show UEFI handles
260 * @cmdtp: Command table
261 * @flag: Command flag
262 * @argc: Number of arguments
263 * @argv: Argument array
264 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
266 * Implement efidebug "dh" sub-command.
267 * Show all UEFI handles and their information, currently all protocols
270 static int do_efi_show_handles(cmd_tbl_t *cmdtp, int flag,
271 int argc, char * const argv[])
273 efi_handle_t *handles;
275 efi_uintn_t num, count, i, j;
276 const char *guid_text;
279 ret = EFI_CALL(BS->locate_handle_buffer(ALL_HANDLES, NULL, NULL,
281 if (ret != EFI_SUCCESS)
282 return CMD_RET_FAILURE;
285 return CMD_RET_SUCCESS;
287 printf("Handle%.*s Protocols\n", EFI_HANDLE_WIDTH - 6, spc);
288 printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep);
289 for (i = 0; i < num; i++) {
290 printf("%p", handles[i]);
291 ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid,
298 for (j = 0; j < count; j++) {
304 guid_text = get_guid_text(guid[j]);
308 printf("%pUl", guid[j]);
313 EFI_CALL(BS->free_pool(handles));
315 return CMD_RET_SUCCESS;
319 * do_efi_boot_add() - set UEFI load option
321 * @cmdtp: Command table
322 * @flag: Command flag
323 * @argc: Number of arguments
324 * @argv: Argument array
325 * Return: CMD_RET_SUCCESS on success,
326 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
328 * Implement efidebug "boot add" sub-command.
329 * Create or change UEFI load option.
330 * - boot add <id> <label> <interface> <devnum>[:<part>] <file> <options>
332 static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag,
333 int argc, char * const argv[])
338 u16 var_name16[9], *p;
340 size_t label_len, label_len16;
342 struct efi_device_path *device_path = NULL, *file_path = NULL;
343 struct efi_load_option lo;
348 if (argc < 6 || argc > 7)
349 return CMD_RET_USAGE;
351 id = (int)simple_strtoul(argv[1], &endp, 16);
352 if (*endp != '\0' || id > 0xffff)
353 return CMD_RET_FAILURE;
355 sprintf(var_name, "Boot%04X", id);
357 utf8_utf16_strncpy(&p, var_name, 9);
359 guid = efi_global_variable_guid;
362 lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
365 label_len = strlen(argv[2]);
366 label_len16 = utf8_utf16_strnlen(argv[2], label_len);
367 label = malloc((label_len16 + 1) * sizeof(u16));
369 return CMD_RET_FAILURE;
370 lo.label = label; /* label will be changed below */
371 utf8_utf16_strncpy(&label, argv[2], label_len);
374 ret = efi_dp_from_name(argv[3], argv[4], argv[5], &device_path,
376 if (ret != EFI_SUCCESS) {
377 printf("Cannot create device path for \"%s %s\"\n",
379 ret = CMD_RET_FAILURE;
382 lo.file_path = file_path;
383 lo.file_path_length = efi_dp_size(file_path)
384 + sizeof(struct efi_device_path); /* for END */
387 lo.optional_data = (u8 *)(argc == 6 ? "" : argv[6]);
389 size = efi_serialize_load_option(&lo, (u8 **)&data);
391 ret = CMD_RET_FAILURE;
395 ret = EFI_CALL(RT->set_variable(var_name16, &guid,
396 EFI_VARIABLE_BOOTSERVICE_ACCESS |
397 EFI_VARIABLE_RUNTIME_ACCESS,
399 ret = (ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE);
402 efi_free_pool(device_path);
403 efi_free_pool(file_path);
410 * do_efi_boot_rm() - delete UEFI load options
412 * @cmdtp: Command table
413 * @flag: Command flag
414 * @argc: Number of arguments
415 * @argv: Argument array
416 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
418 * Implement efidebug "boot rm" sub-command.
419 * Delete UEFI load options.
422 static int do_efi_boot_rm(cmd_tbl_t *cmdtp, int flag,
423 int argc, char * const argv[])
433 return CMD_RET_USAGE;
435 guid = efi_global_variable_guid;
436 for (i = 1; i < argc; i++, argv++) {
437 id = (int)simple_strtoul(argv[1], &endp, 16);
438 if (*endp != '\0' || id > 0xffff)
439 return CMD_RET_FAILURE;
441 sprintf(var_name, "Boot%04X", id);
442 utf8_utf16_strncpy((u16 **)&var_name16, var_name, 9);
444 ret = EFI_CALL(RT->set_variable(var_name16, &guid, 0, 0, NULL));
446 printf("cannot remove Boot%04X", id);
447 return CMD_RET_FAILURE;
451 return CMD_RET_SUCCESS;
455 * show_efi_boot_opt_data() - dump UEFI load option
457 * @id: Load option number
458 * @data: Value of UEFI load option variable
460 * Decode the value of UEFI load option variable and print information.
462 static void show_efi_boot_opt_data(int id, void *data)
464 struct efi_load_option lo;
466 size_t label_len16, label_len;
469 efi_deserialize_load_option(&lo, data);
471 label_len16 = u16_strlen(lo.label);
472 label_len = utf16_utf8_strnlen(lo.label, label_len16);
473 label = malloc(label_len + 1);
477 utf16_utf8_strncpy(&p, lo.label, label_len16);
479 printf("Boot%04X:\n", id);
480 printf("\tattributes: %c%c%c (0x%08x)\n",
482 lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
483 /* FORCE RECONNECT */
484 lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
486 lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
488 printf("\tlabel: %s\n", label);
490 dp_str = efi_dp_str(lo.file_path);
491 printf("\tfile_path: %ls\n", dp_str);
492 efi_free_pool(dp_str);
494 printf("\tdata: %s\n", lo.optional_data);
500 * show_efi_boot_opt() - dump UEFI load option
502 * @id: Load option number
504 * Dump information defined by UEFI load option.
506 static void show_efi_boot_opt(int id)
509 u16 var_name16[9], *p;
515 sprintf(var_name, "Boot%04X", id);
517 utf8_utf16_strncpy(&p, var_name, 9);
518 guid = efi_global_variable_guid;
521 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, NULL));
522 if (ret == (int)EFI_BUFFER_TOO_SMALL) {
524 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
527 if (ret == EFI_SUCCESS)
528 show_efi_boot_opt_data(id, data);
529 else if (ret == EFI_NOT_FOUND)
530 printf("Boot%04X: not found\n", id);
536 * show_efi_boot_dump() - dump all UEFI load options
538 * @cmdtp: Command table
539 * @flag: Command flag
540 * @argc: Number of arguments
541 * @argv: Argument array
542 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
544 * Implement efidebug "boot dump" sub-command.
545 * Dump information of all UEFI load options defined.
548 static int do_efi_boot_dump(cmd_tbl_t *cmdtp, int flag,
549 int argc, char * const argv[])
552 char * const regexlist[] = {regex};
553 char *variables = NULL, *boot, *value;
558 return CMD_RET_USAGE;
560 snprintf(regex, 256, "efi_.*-.*-.*-.*-.*_Boot[0-9A-F]+");
562 /* TODO: use GetNextVariableName? */
563 len = hexport_r(&env_htab, '\n', H_MATCH_REGEX | H_MATCH_KEY,
564 &variables, 0, 1, regexlist);
567 return CMD_RET_SUCCESS;
570 return CMD_RET_FAILURE;
574 value = strstr(boot, "Boot") + 4;
575 id = (int)simple_strtoul(value, NULL, 16);
576 show_efi_boot_opt(id);
577 boot = strchr(boot, '\n');
584 return CMD_RET_SUCCESS;
588 * show_efi_boot_order() - show order of UEFI load options
590 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
592 * Show order of UEFI load options defined by BootOrder variable.
594 static int show_efi_boot_order(void)
597 u16 *bootorder = NULL;
601 u16 var_name16[9], *p16;
603 struct efi_load_option lo;
605 size_t label_len16, label_len;
608 guid = efi_global_variable_guid;
610 ret = EFI_CALL(RT->get_variable(L"BootOrder", &guid, NULL, &size,
612 if (ret == EFI_BUFFER_TOO_SMALL) {
613 bootorder = malloc(size);
614 ret = EFI_CALL(RT->get_variable(L"BootOrder", &guid, NULL,
617 if (ret == EFI_NOT_FOUND) {
618 printf("BootOrder not defined\n");
619 ret = CMD_RET_SUCCESS;
621 } else if (ret != EFI_SUCCESS) {
622 ret = CMD_RET_FAILURE;
626 num = size / sizeof(u16);
627 for (i = 0; i < num; i++) {
628 sprintf(var_name, "Boot%04X", bootorder[i]);
630 utf8_utf16_strncpy(&p16, var_name, 9);
633 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
635 if (ret != EFI_BUFFER_TOO_SMALL) {
636 printf("%2d: Boot%04X: (not defined)\n",
637 i + 1, bootorder[i]);
643 ret = CMD_RET_FAILURE;
646 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
648 if (ret != EFI_SUCCESS) {
650 ret = CMD_RET_FAILURE;
654 efi_deserialize_load_option(&lo, data);
656 label_len16 = u16_strlen(lo.label);
657 label_len = utf16_utf8_strnlen(lo.label, label_len16);
658 label = malloc(label_len + 1);
661 ret = CMD_RET_FAILURE;
665 utf16_utf8_strncpy(&p, lo.label, label_len16);
666 printf("%2d: Boot%04X: %s\n", i + 1, bootorder[i], label);
678 * do_efi_boot_next() - manage UEFI BootNext variable
680 * @cmdtp: Command table
681 * @flag: Command flag
682 * @argc: Number of arguments
683 * @argv: Argument array
684 * Return: CMD_RET_SUCCESS on success,
685 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
687 * Implement efidebug "boot next" sub-command.
688 * Set BootNext variable.
691 static int do_efi_boot_next(cmd_tbl_t *cmdtp, int flag,
692 int argc, char * const argv[])
701 return CMD_RET_USAGE;
703 bootnext = (u16)simple_strtoul(argv[1], &endp, 16);
704 if (*endp != '\0' || bootnext > 0xffff) {
705 printf("invalid value: %s\n", argv[1]);
706 ret = CMD_RET_FAILURE;
710 guid = efi_global_variable_guid;
712 ret = EFI_CALL(RT->set_variable(L"BootNext", &guid,
713 EFI_VARIABLE_BOOTSERVICE_ACCESS |
714 EFI_VARIABLE_RUNTIME_ACCESS,
716 ret = (ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE);
722 * do_efi_boot_order() - manage UEFI BootOrder variable
724 * @cmdtp: Command table
725 * @flag: Command flag
726 * @argc: Number of arguments
727 * @argv: Argument array
728 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
730 * Implement efidebug "boot order" sub-command.
731 * Show order of UEFI load options, or change it in BootOrder variable.
732 * - boot order [<id> ...]
734 static int do_efi_boot_order(cmd_tbl_t *cmdtp, int flag,
735 int argc, char * const argv[])
737 u16 *bootorder = NULL;
745 return show_efi_boot_order();
750 size = argc * sizeof(u16);
751 bootorder = malloc(size);
753 return CMD_RET_FAILURE;
755 for (i = 0; i < argc; i++) {
756 id = (int)simple_strtoul(argv[i], &endp, 16);
757 if (*endp != '\0' || id > 0xffff) {
758 printf("invalid value: %s\n", argv[i]);
759 ret = CMD_RET_FAILURE;
763 bootorder[i] = (u16)id;
766 guid = efi_global_variable_guid;
767 ret = EFI_CALL(RT->set_variable(L"BootOrder", &guid,
768 EFI_VARIABLE_BOOTSERVICE_ACCESS |
769 EFI_VARIABLE_RUNTIME_ACCESS,
771 ret = (ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE);
778 static cmd_tbl_t cmd_efidebug_boot_sub[] = {
779 U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
780 U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
781 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
782 U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
783 U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
788 * do_efi_boot_opt() - manage 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,
795 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
797 * Implement efidebug "boot" sub-command.
798 * See above for details of sub-commands.
800 static int do_efi_boot_opt(cmd_tbl_t *cmdtp, int flag,
801 int argc, char * const argv[])
806 return CMD_RET_USAGE;
810 cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
811 ARRAY_SIZE(cmd_efidebug_boot_sub));
813 return CMD_RET_USAGE;
815 return cp->cmd(cmdtp, flag, argc, argv);
818 static cmd_tbl_t cmd_efidebug_sub[] = {
819 U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
820 U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices,
822 U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
824 U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
829 * do_efidebug() - display and configure UEFI environment
831 * @cmdtp: Command table
832 * @flag: Command flag
833 * @argc: Number of arguments
834 * @argv: Argument array
835 * Return: CMD_RET_SUCCESS on success,
836 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
838 * Implement efidebug command which allows us to display and
839 * configure UEFI environment.
840 * See above for details of sub-commands.
842 static int do_efidebug(cmd_tbl_t *cmdtp, int flag,
843 int argc, char * const argv[])
849 return CMD_RET_USAGE;
853 /* Initialize UEFI drivers */
854 r = efi_init_obj_list();
855 if (r != EFI_SUCCESS) {
856 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
857 r & ~EFI_ERROR_MASK);
858 return CMD_RET_FAILURE;
861 cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
862 ARRAY_SIZE(cmd_efidebug_sub));
864 return CMD_RET_USAGE;
866 return cp->cmd(cmdtp, flag, argc, argv);
869 #ifdef CONFIG_SYS_LONGHELP
870 static char efidebug_help_text[] =
871 " - UEFI Shell-like interface to configure UEFI environment\n"
873 "efidebug boot add <bootid> <label> <interface> <devnum>[:<part>] <file path> [<load options>]\n"
874 " - set UEFI BootXXXX variable\n"
875 " <load options> will be passed to UEFI application\n"
876 "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
877 " - delete UEFI BootXXXX variables\n"
878 "efidebug boot dump\n"
879 " - dump all UEFI BootXXXX variables\n"
880 "efidebug boot next <bootid>\n"
881 " - set UEFI BootNext variable\n"
882 "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
883 " - set/show UEFI boot order\n"
886 " - show uefi devices\n"
888 " - show uefi drivers\n"
890 " - show uefi handles\n";
894 efidebug, 10, 0, do_efidebug,
895 "Configure UEFI environment",