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;
183 * do_efi_boot_add() - set UEFI load option
185 * @cmdtp: Command table
186 * @flag: Command flag
187 * @argc: Number of arguments
188 * @argv: Argument array
189 * Return: CMD_RET_SUCCESS on success,
190 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
192 * Implement efidebug "boot add" sub-command.
193 * Create or change UEFI load option.
194 * - boot add <id> <label> <interface> <devnum>[:<part>] <file> <options>
196 static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag,
197 int argc, char * const argv[])
202 u16 var_name16[9], *p;
204 size_t label_len, label_len16;
206 struct efi_device_path *device_path = NULL, *file_path = NULL;
207 struct efi_load_option lo;
212 if (argc < 6 || argc > 7)
213 return CMD_RET_USAGE;
215 id = (int)simple_strtoul(argv[1], &endp, 16);
216 if (*endp != '\0' || id > 0xffff)
217 return CMD_RET_FAILURE;
219 sprintf(var_name, "Boot%04X", id);
221 utf8_utf16_strncpy(&p, var_name, 9);
223 guid = efi_global_variable_guid;
226 lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
229 label_len = strlen(argv[2]);
230 label_len16 = utf8_utf16_strnlen(argv[2], label_len);
231 label = malloc((label_len16 + 1) * sizeof(u16));
233 return CMD_RET_FAILURE;
234 lo.label = label; /* label will be changed below */
235 utf8_utf16_strncpy(&label, argv[2], label_len);
238 ret = efi_dp_from_name(argv[3], argv[4], argv[5], &device_path,
240 if (ret != EFI_SUCCESS) {
241 printf("Cannot create device path for \"%s %s\"\n",
243 ret = CMD_RET_FAILURE;
246 lo.file_path = file_path;
247 lo.file_path_length = efi_dp_size(file_path)
248 + sizeof(struct efi_device_path); /* for END */
251 lo.optional_data = (u8 *)(argc == 6 ? "" : argv[6]);
253 size = efi_serialize_load_option(&lo, (u8 **)&data);
255 ret = CMD_RET_FAILURE;
259 ret = EFI_CALL(RT->set_variable(var_name16, &guid,
260 EFI_VARIABLE_BOOTSERVICE_ACCESS |
261 EFI_VARIABLE_RUNTIME_ACCESS,
263 ret = (ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE);
266 efi_free_pool(device_path);
267 efi_free_pool(file_path);
274 * do_efi_boot_rm() - delete UEFI load options
276 * @cmdtp: Command table
277 * @flag: Command flag
278 * @argc: Number of arguments
279 * @argv: Argument array
280 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
282 * Implement efidebug "boot rm" sub-command.
283 * Delete UEFI load options.
286 static int do_efi_boot_rm(cmd_tbl_t *cmdtp, int flag,
287 int argc, char * const argv[])
297 return CMD_RET_USAGE;
299 guid = efi_global_variable_guid;
300 for (i = 1; i < argc; i++, argv++) {
301 id = (int)simple_strtoul(argv[1], &endp, 16);
302 if (*endp != '\0' || id > 0xffff)
303 return CMD_RET_FAILURE;
305 sprintf(var_name, "Boot%04X", id);
306 utf8_utf16_strncpy((u16 **)&var_name16, var_name, 9);
308 ret = EFI_CALL(RT->set_variable(var_name16, &guid, 0, 0, NULL));
310 printf("cannot remove Boot%04X", id);
311 return CMD_RET_FAILURE;
315 return CMD_RET_SUCCESS;
319 * show_efi_boot_opt_data() - dump UEFI load option
321 * @id: Load option number
322 * @data: Value of UEFI load option variable
324 * Decode the value of UEFI load option variable and print information.
326 static void show_efi_boot_opt_data(int id, void *data)
328 struct efi_load_option lo;
330 size_t label_len16, label_len;
333 efi_deserialize_load_option(&lo, data);
335 label_len16 = u16_strlen(lo.label);
336 label_len = utf16_utf8_strnlen(lo.label, label_len16);
337 label = malloc(label_len + 1);
341 utf16_utf8_strncpy(&p, lo.label, label_len16);
343 printf("Boot%04X:\n", id);
344 printf("\tattributes: %c%c%c (0x%08x)\n",
346 lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
347 /* FORCE RECONNECT */
348 lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
350 lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
352 printf("\tlabel: %s\n", label);
354 dp_str = efi_dp_str(lo.file_path);
355 printf("\tfile_path: %ls\n", dp_str);
356 efi_free_pool(dp_str);
358 printf("\tdata: %s\n", lo.optional_data);
364 * show_efi_boot_opt() - dump UEFI load option
366 * @id: Load option number
368 * Dump information defined by UEFI load option.
370 static void show_efi_boot_opt(int id)
373 u16 var_name16[9], *p;
379 sprintf(var_name, "Boot%04X", id);
381 utf8_utf16_strncpy(&p, var_name, 9);
382 guid = efi_global_variable_guid;
385 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, NULL));
386 if (ret == (int)EFI_BUFFER_TOO_SMALL) {
388 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
391 if (ret == EFI_SUCCESS)
392 show_efi_boot_opt_data(id, data);
393 else if (ret == EFI_NOT_FOUND)
394 printf("Boot%04X: not found\n", id);
400 * show_efi_boot_dump() - dump all UEFI load options
402 * @cmdtp: Command table
403 * @flag: Command flag
404 * @argc: Number of arguments
405 * @argv: Argument array
406 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
408 * Implement efidebug "boot dump" sub-command.
409 * Dump information of all UEFI load options defined.
412 static int do_efi_boot_dump(cmd_tbl_t *cmdtp, int flag,
413 int argc, char * const argv[])
416 char * const regexlist[] = {regex};
417 char *variables = NULL, *boot, *value;
422 return CMD_RET_USAGE;
424 snprintf(regex, 256, "efi_.*-.*-.*-.*-.*_Boot[0-9A-F]+");
426 /* TODO: use GetNextVariableName? */
427 len = hexport_r(&env_htab, '\n', H_MATCH_REGEX | H_MATCH_KEY,
428 &variables, 0, 1, regexlist);
431 return CMD_RET_SUCCESS;
434 return CMD_RET_FAILURE;
438 value = strstr(boot, "Boot") + 4;
439 id = (int)simple_strtoul(value, NULL, 16);
440 show_efi_boot_opt(id);
441 boot = strchr(boot, '\n');
448 return CMD_RET_SUCCESS;
452 * show_efi_boot_order() - show order of UEFI load options
454 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
456 * Show order of UEFI load options defined by BootOrder variable.
458 static int show_efi_boot_order(void)
461 u16 *bootorder = NULL;
465 u16 var_name16[9], *p16;
467 struct efi_load_option lo;
469 size_t label_len16, label_len;
472 guid = efi_global_variable_guid;
474 ret = EFI_CALL(RT->get_variable(L"BootOrder", &guid, NULL, &size,
476 if (ret == EFI_BUFFER_TOO_SMALL) {
477 bootorder = malloc(size);
478 ret = EFI_CALL(RT->get_variable(L"BootOrder", &guid, NULL,
481 if (ret == EFI_NOT_FOUND) {
482 printf("BootOrder not defined\n");
483 ret = CMD_RET_SUCCESS;
485 } else if (ret != EFI_SUCCESS) {
486 ret = CMD_RET_FAILURE;
490 num = size / sizeof(u16);
491 for (i = 0; i < num; i++) {
492 sprintf(var_name, "Boot%04X", bootorder[i]);
494 utf8_utf16_strncpy(&p16, var_name, 9);
497 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
499 if (ret != EFI_BUFFER_TOO_SMALL) {
500 printf("%2d: Boot%04X: (not defined)\n",
501 i + 1, bootorder[i]);
507 ret = CMD_RET_FAILURE;
510 ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size,
512 if (ret != EFI_SUCCESS) {
514 ret = CMD_RET_FAILURE;
518 efi_deserialize_load_option(&lo, data);
520 label_len16 = u16_strlen(lo.label);
521 label_len = utf16_utf8_strnlen(lo.label, label_len16);
522 label = malloc(label_len + 1);
525 ret = CMD_RET_FAILURE;
529 utf16_utf8_strncpy(&p, lo.label, label_len16);
530 printf("%2d: Boot%04X: %s\n", i + 1, bootorder[i], label);
542 * do_efi_boot_next() - manage UEFI BootNext variable
544 * @cmdtp: Command table
545 * @flag: Command flag
546 * @argc: Number of arguments
547 * @argv: Argument array
548 * Return: CMD_RET_SUCCESS on success,
549 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
551 * Implement efidebug "boot next" sub-command.
552 * Set BootNext variable.
555 static int do_efi_boot_next(cmd_tbl_t *cmdtp, int flag,
556 int argc, char * const argv[])
565 return CMD_RET_USAGE;
567 bootnext = (u16)simple_strtoul(argv[1], &endp, 16);
568 if (*endp != '\0' || bootnext > 0xffff) {
569 printf("invalid value: %s\n", argv[1]);
570 ret = CMD_RET_FAILURE;
574 guid = efi_global_variable_guid;
576 ret = EFI_CALL(RT->set_variable(L"BootNext", &guid,
577 EFI_VARIABLE_BOOTSERVICE_ACCESS |
578 EFI_VARIABLE_RUNTIME_ACCESS,
580 ret = (ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE);
586 * do_efi_boot_order() - manage UEFI BootOrder variable
588 * @cmdtp: Command table
589 * @flag: Command flag
590 * @argc: Number of arguments
591 * @argv: Argument array
592 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
594 * Implement efidebug "boot order" sub-command.
595 * Show order of UEFI load options, or change it in BootOrder variable.
596 * - boot order [<id> ...]
598 static int do_efi_boot_order(cmd_tbl_t *cmdtp, int flag,
599 int argc, char * const argv[])
601 u16 *bootorder = NULL;
609 return show_efi_boot_order();
614 size = argc * sizeof(u16);
615 bootorder = malloc(size);
617 return CMD_RET_FAILURE;
619 for (i = 0; i < argc; i++) {
620 id = (int)simple_strtoul(argv[i], &endp, 16);
621 if (*endp != '\0' || id > 0xffff) {
622 printf("invalid value: %s\n", argv[i]);
623 ret = CMD_RET_FAILURE;
627 bootorder[i] = (u16)id;
630 guid = efi_global_variable_guid;
631 ret = EFI_CALL(RT->set_variable(L"BootOrder", &guid,
632 EFI_VARIABLE_BOOTSERVICE_ACCESS |
633 EFI_VARIABLE_RUNTIME_ACCESS,
635 ret = (ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE);
642 static cmd_tbl_t cmd_efidebug_boot_sub[] = {
643 U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
644 U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
645 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
646 U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
647 U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
652 * do_efi_boot_opt() - manage UEFI load options
654 * @cmdtp: Command table
655 * @flag: Command flag
656 * @argc: Number of arguments
657 * @argv: Argument array
658 * Return: CMD_RET_SUCCESS on success,
659 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
661 * Implement efidebug "boot" sub-command.
662 * See above for details of sub-commands.
664 static int do_efi_boot_opt(cmd_tbl_t *cmdtp, int flag,
665 int argc, char * const argv[])
670 return CMD_RET_USAGE;
674 cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
675 ARRAY_SIZE(cmd_efidebug_boot_sub));
677 return CMD_RET_USAGE;
679 return cp->cmd(cmdtp, flag, argc, argv);
682 static cmd_tbl_t cmd_efidebug_sub[] = {
683 U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
684 U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices,
686 U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
691 * do_efidebug() - display and configure UEFI environment
693 * @cmdtp: Command table
694 * @flag: Command flag
695 * @argc: Number of arguments
696 * @argv: Argument array
697 * Return: CMD_RET_SUCCESS on success,
698 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
700 * Implement efidebug command which allows us to display and
701 * configure UEFI environment.
702 * See above for details of sub-commands.
704 static int do_efidebug(cmd_tbl_t *cmdtp, int flag,
705 int argc, char * const argv[])
711 return CMD_RET_USAGE;
715 /* Initialize UEFI drivers */
716 r = efi_init_obj_list();
717 if (r != EFI_SUCCESS) {
718 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
719 r & ~EFI_ERROR_MASK);
720 return CMD_RET_FAILURE;
723 cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
724 ARRAY_SIZE(cmd_efidebug_sub));
726 return CMD_RET_USAGE;
728 return cp->cmd(cmdtp, flag, argc, argv);
731 #ifdef CONFIG_SYS_LONGHELP
732 static char efidebug_help_text[] =
733 " - UEFI Shell-like interface to configure UEFI environment\n"
735 "efidebug boot add <bootid> <label> <interface> <devnum>[:<part>] <file path> [<load options>]\n"
736 " - set UEFI BootXXXX variable\n"
737 " <load options> will be passed to UEFI application\n"
738 "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
739 " - delete UEFI BootXXXX variables\n"
740 "efidebug boot dump\n"
741 " - dump all UEFI BootXXXX variables\n"
742 "efidebug boot next <bootid>\n"
743 " - set UEFI BootNext variable\n"
744 "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
745 " - set/show UEFI boot order\n"
748 " - show uefi devices\n"
750 " - show uefi drivers\n";
754 efidebug, 10, 0, do_efidebug,
755 "Configure UEFI environment",