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_show_images() - show UEFI images
321 * @cmdtp: Command table
322 * @flag: Command flag
323 * @argc: Number of arguments
324 * @argv: Argument array
325 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
327 * Implement efidebug "images" sub-command.
328 * Show all UEFI loaded images and their information.
330 static int do_efi_show_images(cmd_tbl_t *cmdtp, int flag,
331 int argc, char * const argv[])
333 efi_print_image_infos(NULL);
335 return CMD_RET_SUCCESS;
339 * do_efi_boot_add() - set UEFI load option
341 * @cmdtp: Command table
342 * @flag: Command flag
343 * @argc: Number of arguments
344 * @argv: Argument array
345 * Return: CMD_RET_SUCCESS on success,
346 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
348 * Implement efidebug "boot add" sub-command.
349 * Create or change UEFI load option.
350 * - boot add <id> <label> <interface> <devnum>[:<part>] <file> <options>
352 static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag,
353 int argc, char * const argv[])
358 u16 var_name16[9], *p;
360 size_t label_len, label_len16;
362 struct efi_device_path *device_path = NULL, *file_path = NULL;
363 struct efi_load_option lo;
368 if (argc < 6 || argc > 7)
369 return CMD_RET_USAGE;
371 id = (int)simple_strtoul(argv[1], &endp, 16);
372 if (*endp != '\0' || id > 0xffff)
373 return CMD_RET_FAILURE;
375 sprintf(var_name, "Boot%04X", id);
377 utf8_utf16_strncpy(&p, var_name, 9);
379 guid = efi_global_variable_guid;
382 lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
385 label_len = strlen(argv[2]);
386 label_len16 = utf8_utf16_strnlen(argv[2], label_len);
387 label = malloc((label_len16 + 1) * sizeof(u16));
389 return CMD_RET_FAILURE;
390 lo.label = label; /* label will be changed below */
391 utf8_utf16_strncpy(&label, argv[2], label_len);
394 ret = efi_dp_from_name(argv[3], argv[4], argv[5], &device_path,
396 if (ret != EFI_SUCCESS) {
397 printf("Cannot create device path for \"%s %s\"\n",
399 ret = CMD_RET_FAILURE;
402 lo.file_path = file_path;
403 lo.file_path_length = efi_dp_size(file_path)
404 + sizeof(struct efi_device_path); /* for END */
407 lo.optional_data = (u8 *)(argc == 6 ? "" : argv[6]);
409 size = efi_serialize_load_option(&lo, (u8 **)&data);
411 ret = CMD_RET_FAILURE;
415 ret = EFI_CALL(RT->set_variable(var_name16, &guid,
416 EFI_VARIABLE_BOOTSERVICE_ACCESS |
417 EFI_VARIABLE_RUNTIME_ACCESS,
419 ret = (ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE);
422 efi_free_pool(device_path);
423 efi_free_pool(file_path);
430 * do_efi_boot_rm() - delete UEFI load options
432 * @cmdtp: Command table
433 * @flag: Command flag
434 * @argc: Number of arguments
435 * @argv: Argument array
436 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
438 * Implement efidebug "boot rm" sub-command.
439 * Delete UEFI load options.
442 static int do_efi_boot_rm(cmd_tbl_t *cmdtp, int flag,
443 int argc, char * const argv[])
453 return CMD_RET_USAGE;
455 guid = efi_global_variable_guid;
456 for (i = 1; i < argc; i++, argv++) {
457 id = (int)simple_strtoul(argv[1], &endp, 16);
458 if (*endp != '\0' || id > 0xffff)
459 return CMD_RET_FAILURE;
461 sprintf(var_name, "Boot%04X", id);
462 utf8_utf16_strncpy((u16 **)&var_name16, var_name, 9);
464 ret = EFI_CALL(RT->set_variable(var_name16, &guid, 0, 0, NULL));
466 printf("cannot remove Boot%04X", id);
467 return CMD_RET_FAILURE;
471 return CMD_RET_SUCCESS;
475 * show_efi_boot_opt_data() - dump UEFI load option
477 * @id: Load option number
478 * @data: Value of UEFI load option variable
480 * Decode the value of UEFI load option variable and print information.
482 static void show_efi_boot_opt_data(int id, void *data)
484 struct efi_load_option lo;
486 size_t label_len16, label_len;
489 efi_deserialize_load_option(&lo, data);
491 label_len16 = u16_strlen(lo.label);
492 label_len = utf16_utf8_strnlen(lo.label, label_len16);
493 label = malloc(label_len + 1);
497 utf16_utf8_strncpy(&p, lo.label, label_len16);
499 printf("Boot%04X:\n", id);
500 printf("\tattributes: %c%c%c (0x%08x)\n",
502 lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
503 /* FORCE RECONNECT */
504 lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
506 lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
508 printf("\tlabel: %s\n", label);
510 dp_str = efi_dp_str(lo.file_path);
511 printf("\tfile_path: %ls\n", dp_str);
512 efi_free_pool(dp_str);
514 printf("\tdata: %s\n", lo.optional_data);
520 * show_efi_boot_opt() - dump UEFI load option
522 * @id: Load option number
524 * Dump information defined by UEFI load option.
526 static void show_efi_boot_opt(int id)
529 u16 var_name16[9], *p;
535 sprintf(var_name, "Boot%04X", id);
537 utf8_utf16_strncpy(&p, var_name, 9);
538 guid = efi_global_variable_guid;
541 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, NULL));
542 if (ret == (int)EFI_BUFFER_TOO_SMALL) {
544 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
547 if (ret == EFI_SUCCESS)
548 show_efi_boot_opt_data(id, data);
549 else if (ret == EFI_NOT_FOUND)
550 printf("Boot%04X: not found\n", id);
556 * show_efi_boot_dump() - dump all UEFI load options
558 * @cmdtp: Command table
559 * @flag: Command flag
560 * @argc: Number of arguments
561 * @argv: Argument array
562 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
564 * Implement efidebug "boot dump" sub-command.
565 * Dump information of all UEFI load options defined.
568 static int do_efi_boot_dump(cmd_tbl_t *cmdtp, int flag,
569 int argc, char * const argv[])
572 char * const regexlist[] = {regex};
573 char *variables = NULL, *boot, *value;
578 return CMD_RET_USAGE;
580 snprintf(regex, 256, "efi_.*-.*-.*-.*-.*_Boot[0-9A-F]+");
582 /* TODO: use GetNextVariableName? */
583 len = hexport_r(&env_htab, '\n', H_MATCH_REGEX | H_MATCH_KEY,
584 &variables, 0, 1, regexlist);
587 return CMD_RET_SUCCESS;
590 return CMD_RET_FAILURE;
594 value = strstr(boot, "Boot") + 4;
595 id = (int)simple_strtoul(value, NULL, 16);
596 show_efi_boot_opt(id);
597 boot = strchr(boot, '\n');
604 return CMD_RET_SUCCESS;
608 * show_efi_boot_order() - show order of UEFI load options
610 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
612 * Show order of UEFI load options defined by BootOrder variable.
614 static int show_efi_boot_order(void)
617 u16 *bootorder = NULL;
621 u16 var_name16[9], *p16;
623 struct efi_load_option lo;
625 size_t label_len16, label_len;
628 guid = efi_global_variable_guid;
630 ret = EFI_CALL(RT->get_variable(L"BootOrder", &guid, NULL, &size,
632 if (ret == EFI_BUFFER_TOO_SMALL) {
633 bootorder = malloc(size);
634 ret = EFI_CALL(RT->get_variable(L"BootOrder", &guid, NULL,
637 if (ret == EFI_NOT_FOUND) {
638 printf("BootOrder not defined\n");
639 ret = CMD_RET_SUCCESS;
641 } else if (ret != EFI_SUCCESS) {
642 ret = CMD_RET_FAILURE;
646 num = size / sizeof(u16);
647 for (i = 0; i < num; i++) {
648 sprintf(var_name, "Boot%04X", bootorder[i]);
650 utf8_utf16_strncpy(&p16, var_name, 9);
653 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
655 if (ret != EFI_BUFFER_TOO_SMALL) {
656 printf("%2d: Boot%04X: (not defined)\n",
657 i + 1, bootorder[i]);
663 ret = CMD_RET_FAILURE;
666 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
668 if (ret != EFI_SUCCESS) {
670 ret = CMD_RET_FAILURE;
674 efi_deserialize_load_option(&lo, data);
676 label_len16 = u16_strlen(lo.label);
677 label_len = utf16_utf8_strnlen(lo.label, label_len16);
678 label = malloc(label_len + 1);
681 ret = CMD_RET_FAILURE;
685 utf16_utf8_strncpy(&p, lo.label, label_len16);
686 printf("%2d: Boot%04X: %s\n", i + 1, bootorder[i], label);
698 * do_efi_boot_next() - manage UEFI BootNext variable
700 * @cmdtp: Command table
701 * @flag: Command flag
702 * @argc: Number of arguments
703 * @argv: Argument array
704 * Return: CMD_RET_SUCCESS on success,
705 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
707 * Implement efidebug "boot next" sub-command.
708 * Set BootNext variable.
711 static int do_efi_boot_next(cmd_tbl_t *cmdtp, int flag,
712 int argc, char * const argv[])
721 return CMD_RET_USAGE;
723 bootnext = (u16)simple_strtoul(argv[1], &endp, 16);
724 if (*endp != '\0' || bootnext > 0xffff) {
725 printf("invalid value: %s\n", argv[1]);
726 ret = CMD_RET_FAILURE;
730 guid = efi_global_variable_guid;
732 ret = EFI_CALL(RT->set_variable(L"BootNext", &guid,
733 EFI_VARIABLE_BOOTSERVICE_ACCESS |
734 EFI_VARIABLE_RUNTIME_ACCESS,
736 ret = (ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE);
742 * do_efi_boot_order() - manage UEFI BootOrder variable
744 * @cmdtp: Command table
745 * @flag: Command flag
746 * @argc: Number of arguments
747 * @argv: Argument array
748 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
750 * Implement efidebug "boot order" sub-command.
751 * Show order of UEFI load options, or change it in BootOrder variable.
752 * - boot order [<id> ...]
754 static int do_efi_boot_order(cmd_tbl_t *cmdtp, int flag,
755 int argc, char * const argv[])
757 u16 *bootorder = NULL;
765 return show_efi_boot_order();
770 size = argc * sizeof(u16);
771 bootorder = malloc(size);
773 return CMD_RET_FAILURE;
775 for (i = 0; i < argc; i++) {
776 id = (int)simple_strtoul(argv[i], &endp, 16);
777 if (*endp != '\0' || id > 0xffff) {
778 printf("invalid value: %s\n", argv[i]);
779 ret = CMD_RET_FAILURE;
783 bootorder[i] = (u16)id;
786 guid = efi_global_variable_guid;
787 ret = EFI_CALL(RT->set_variable(L"BootOrder", &guid,
788 EFI_VARIABLE_BOOTSERVICE_ACCESS |
789 EFI_VARIABLE_RUNTIME_ACCESS,
791 ret = (ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE);
798 static cmd_tbl_t cmd_efidebug_boot_sub[] = {
799 U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
800 U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
801 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
802 U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
803 U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
808 * do_efi_boot_opt() - manage UEFI load options
810 * @cmdtp: Command table
811 * @flag: Command flag
812 * @argc: Number of arguments
813 * @argv: Argument array
814 * Return: CMD_RET_SUCCESS on success,
815 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
817 * Implement efidebug "boot" sub-command.
818 * See above for details of sub-commands.
820 static int do_efi_boot_opt(cmd_tbl_t *cmdtp, int flag,
821 int argc, char * const argv[])
826 return CMD_RET_USAGE;
830 cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
831 ARRAY_SIZE(cmd_efidebug_boot_sub));
833 return CMD_RET_USAGE;
835 return cp->cmd(cmdtp, flag, argc, argv);
838 static cmd_tbl_t cmd_efidebug_sub[] = {
839 U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
840 U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices,
842 U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
844 U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
846 U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
851 * do_efidebug() - display and configure UEFI environment
853 * @cmdtp: Command table
854 * @flag: Command flag
855 * @argc: Number of arguments
856 * @argv: Argument array
857 * Return: CMD_RET_SUCCESS on success,
858 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
860 * Implement efidebug command which allows us to display and
861 * configure UEFI environment.
862 * See above for details of sub-commands.
864 static int do_efidebug(cmd_tbl_t *cmdtp, int flag,
865 int argc, char * const argv[])
871 return CMD_RET_USAGE;
875 /* Initialize UEFI drivers */
876 r = efi_init_obj_list();
877 if (r != EFI_SUCCESS) {
878 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
879 r & ~EFI_ERROR_MASK);
880 return CMD_RET_FAILURE;
883 cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
884 ARRAY_SIZE(cmd_efidebug_sub));
886 return CMD_RET_USAGE;
888 return cp->cmd(cmdtp, flag, argc, argv);
891 #ifdef CONFIG_SYS_LONGHELP
892 static char efidebug_help_text[] =
893 " - UEFI Shell-like interface to configure UEFI environment\n"
895 "efidebug boot add <bootid> <label> <interface> <devnum>[:<part>] <file path> [<load options>]\n"
896 " - set UEFI BootXXXX variable\n"
897 " <load options> will be passed to UEFI application\n"
898 "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
899 " - delete UEFI BootXXXX variables\n"
900 "efidebug boot dump\n"
901 " - dump all UEFI BootXXXX variables\n"
902 "efidebug boot next <bootid>\n"
903 " - set UEFI BootNext variable\n"
904 "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
905 " - set/show UEFI boot order\n"
908 " - show uefi devices\n"
910 " - show uefi drivers\n"
912 " - show uefi handles\n"
914 " - show loaded images\n";
918 efidebug, 10, 0, do_efidebug,
919 "Configure UEFI environment",