1 // SPDX-License-Identifier: GPL-2.0+
3 * UEFI Shell-like command
5 * Copyright (c) 2018 AKASHI Takahiro, Linaro Limited
11 #include <dm/device.h>
12 #include <efi_dt_fixup.h>
13 #include <efi_load_initrd.h>
14 #include <efi_loader.h>
16 #include <efi_variable.h>
24 #include <linux/ctype.h>
25 #include <linux/err.h>
27 #define BS systab.boottime
29 #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
31 * do_efi_capsule_update() - process a capsule update
33 * @cmdtp: Command table
35 * @argc: Number of arguments
36 * @argv: Argument array
37 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
39 * Implement efidebug "capsule update" sub-command.
40 * process a capsule update.
42 * efidebug capsule update [-v] <capsule address>
44 static int do_efi_capsule_update(struct cmd_tbl *cmdtp, int flag,
45 int argc, char * const argv[])
47 struct efi_capsule_header *capsule;
52 if (argc != 2 && argc != 3)
56 if (strcmp(argv[1], "-v"))
64 capsule = (typeof(capsule))hextoul(argv[1], &endp);
65 if (endp == argv[1]) {
66 printf("Invalid address: %s", argv[1]);
67 return CMD_RET_FAILURE;
71 printf("Capsule guid: %pUl\n", &capsule->capsule_guid);
72 printf("Capsule flags: 0x%x\n", capsule->flags);
73 printf("Capsule header size: 0x%x\n", capsule->header_size);
74 printf("Capsule image size: 0x%x\n",
75 capsule->capsule_image_size);
78 ret = EFI_CALL(efi_update_capsule(&capsule, 1, 0));
80 printf("Cannot handle a capsule at %p\n", capsule);
81 return CMD_RET_FAILURE;
84 return CMD_RET_SUCCESS;
87 #ifdef CONFIG_EFI_CAPSULE_ON_DISK
88 static int do_efi_capsule_on_disk_update(struct cmd_tbl *cmdtp, int flag,
89 int argc, char * const argv[])
93 ret = efi_launch_capsules();
95 return ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
100 * do_efi_capsule_show() - show capsule information
102 * @cmdtp: Command table
103 * @flag: Command flag
104 * @argc: Number of arguments
105 * @argv: Argument array
106 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
108 * Implement efidebug "capsule show" sub-command.
109 * show capsule information.
111 * efidebug capsule show <capsule address>
113 static int do_efi_capsule_show(struct cmd_tbl *cmdtp, int flag,
114 int argc, char * const argv[])
116 struct efi_capsule_header *capsule;
120 return CMD_RET_USAGE;
122 capsule = (typeof(capsule))hextoul(argv[1], &endp);
123 if (endp == argv[1]) {
124 printf("Invalid address: %s", argv[1]);
125 return CMD_RET_FAILURE;
128 printf("Capsule guid: %pUl\n", &capsule->capsule_guid);
129 printf("Capsule flags: 0x%x\n", capsule->flags);
130 printf("Capsule header size: 0x%x\n", capsule->header_size);
131 printf("Capsule image size: 0x%x\n",
132 capsule->capsule_image_size);
134 return CMD_RET_SUCCESS;
137 #ifdef CONFIG_EFI_ESRT
139 #define EFI_ESRT_FW_TYPE_NUM 4
140 char *efi_fw_type_str[EFI_ESRT_FW_TYPE_NUM] = {"unknown", "system FW", "device FW",
143 #define EFI_ESRT_UPDATE_STATUS_NUM 9
144 char *efi_update_status_str[EFI_ESRT_UPDATE_STATUS_NUM] = {"success", "unsuccessful",
145 "insufficient resources", "incorrect version", "invalid format",
146 "auth error", "power event (AC)", "power event (batt)",
147 "unsatisfied dependencies"};
149 #define EFI_FW_TYPE_STR_GET(idx) (\
150 EFI_ESRT_FW_TYPE_NUM > (idx) ? efi_fw_type_str[(idx)] : "error"\
153 #define EFI_FW_STATUS_STR_GET(idx) (\
154 EFI_ESRT_UPDATE_STATUS_NUM > (idx) ? efi_update_status_str[(idx)] : "error"\
158 * do_efi_capsule_esrt() - manage UEFI capsules
160 * @cmdtp: Command table
161 * @flag: Command flag
162 * @argc: Number of arguments
163 * @argv: Argument array
164 * Return: CMD_RET_SUCCESS on success,
165 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
167 * Implement efidebug "capsule esrt" sub-command.
168 * The prints the current ESRT table.
170 * efidebug capsule esrt
172 static int do_efi_capsule_esrt(struct cmd_tbl *cmdtp, int flag,
173 int argc, char * const argv[])
175 struct efi_system_resource_table *esrt = NULL;
178 return CMD_RET_USAGE;
180 for (int idx = 0; idx < systab.nr_tables; idx++)
181 if (!guidcmp(&efi_esrt_guid, &systab.tables[idx].guid))
182 esrt = (struct efi_system_resource_table *)systab.tables[idx].table;
185 log_info("ESRT: table not present\n");
186 return CMD_RET_SUCCESS;
189 printf("========================================\n");
190 printf("ESRT: fw_resource_count=%d\n", esrt->fw_resource_count);
191 printf("ESRT: fw_resource_count_max=%d\n", esrt->fw_resource_count_max);
192 printf("ESRT: fw_resource_version=%lld\n", esrt->fw_resource_version);
194 for (int idx = 0; idx < esrt->fw_resource_count; idx++) {
195 printf("[entry %d]==============================\n", idx);
196 printf("ESRT: fw_class=%pUL\n", &esrt->entries[idx].fw_class);
197 printf("ESRT: fw_type=%s\n", EFI_FW_TYPE_STR_GET(esrt->entries[idx].fw_type));
198 printf("ESRT: fw_version=%d\n", esrt->entries[idx].fw_version);
199 printf("ESRT: lowest_supported_fw_version=%d\n",
200 esrt->entries[idx].lowest_supported_fw_version);
201 printf("ESRT: capsule_flags=%d\n",
202 esrt->entries[idx].capsule_flags);
203 printf("ESRT: last_attempt_version=%d\n",
204 esrt->entries[idx].last_attempt_version);
205 printf("ESRT: last_attempt_status=%s\n",
206 EFI_FW_STATUS_STR_GET(esrt->entries[idx].last_attempt_status));
208 printf("========================================\n");
210 return CMD_RET_SUCCESS;
212 #endif /* CONFIG_EFI_ESRT */
214 * do_efi_capsule_res() - show a capsule update result
216 * @cmdtp: Command table
217 * @flag: Command flag
218 * @argc: Number of arguments
219 * @argv: Argument array
220 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
222 * Implement efidebug "capsule result" sub-command.
223 * show a capsule update result.
224 * If result number is not specified, CapsuleLast will be shown.
226 * efidebug capsule result [<capsule result number>]
228 static int do_efi_capsule_res(struct cmd_tbl *cmdtp, int flag,
229 int argc, char * const argv[])
235 struct efi_capsule_result_variable_header *result = NULL;
239 if (argc != 1 && argc != 2)
240 return CMD_RET_USAGE;
242 guid = efi_guid_capsule_report;
244 size = sizeof(var_name16);
245 ret = efi_get_variable_int(u"CapsuleLast", &guid, NULL,
246 &size, var_name16, NULL);
248 if (ret != EFI_SUCCESS) {
249 if (ret == EFI_NOT_FOUND)
250 printf("CapsuleLast doesn't exist\n");
252 printf("Failed to get CapsuleLast\n");
254 return CMD_RET_FAILURE;
256 printf("CapsuleLast is %ls\n", var_name16);
261 capsule_id = hextoul(argv[0], &endp);
262 if (capsule_id < 0 || capsule_id > 0xffff)
263 return CMD_RET_USAGE;
265 efi_create_indexed_name(var_name16, sizeof(var_name16),
266 "Capsule", capsule_id);
270 ret = efi_get_variable_int(var_name16, &guid, NULL, &size, NULL, NULL);
271 if (ret == EFI_BUFFER_TOO_SMALL) {
272 result = malloc(size);
274 return CMD_RET_FAILURE;
275 ret = efi_get_variable_int(var_name16, &guid, NULL, &size,
278 if (ret != EFI_SUCCESS) {
280 printf("Failed to get %ls\n", var_name16);
282 return CMD_RET_FAILURE;
285 printf("Result total size: 0x%x\n", result->variable_total_size);
286 printf("Capsule guid: %pUl\n", &result->capsule_guid);
287 printf("Time processed: %04d-%02d-%02d %02d:%02d:%02d\n",
288 result->capsule_processed.year, result->capsule_processed.month,
289 result->capsule_processed.day, result->capsule_processed.hour,
290 result->capsule_processed.minute,
291 result->capsule_processed.second);
292 printf("Capsule status: 0x%lx\n", result->capsule_status);
296 return CMD_RET_SUCCESS;
299 static struct cmd_tbl cmd_efidebug_capsule_sub[] = {
300 U_BOOT_CMD_MKENT(update, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_update,
302 U_BOOT_CMD_MKENT(show, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_show,
304 #ifdef CONFIG_EFI_ESRT
305 U_BOOT_CMD_MKENT(esrt, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_esrt,
308 #ifdef CONFIG_EFI_CAPSULE_ON_DISK
309 U_BOOT_CMD_MKENT(disk-update, 0, 0, do_efi_capsule_on_disk_update,
312 U_BOOT_CMD_MKENT(result, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_res,
317 * do_efi_capsule() - manage UEFI capsules
319 * @cmdtp: Command table
320 * @flag: Command flag
321 * @argc: Number of arguments
322 * @argv: Argument array
323 * Return: CMD_RET_SUCCESS on success,
324 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
326 * Implement efidebug "capsule" sub-command.
328 static int do_efi_capsule(struct cmd_tbl *cmdtp, int flag,
329 int argc, char * const argv[])
334 return CMD_RET_USAGE;
338 cp = find_cmd_tbl(argv[0], cmd_efidebug_capsule_sub,
339 ARRAY_SIZE(cmd_efidebug_capsule_sub));
341 return CMD_RET_USAGE;
343 return cp->cmd(cmdtp, flag, argc, argv);
345 #endif /* CONFIG_EFI_HAVE_CAPSULE_SUPPORT */
347 #define EFI_HANDLE_WIDTH ((int)sizeof(efi_handle_t) * 2)
349 static const char spc[] = " ";
350 static const char sep[] = "================";
353 * efi_get_driver_handle_info() - get information of UEFI driver
355 * @handle: Handle of UEFI device
356 * @driver_name: Driver name
357 * @image_path: Pointer to text of device path
358 * Return: 0 on success, -1 on failure
360 * Currently return no useful information as all UEFI drivers are
363 static int efi_get_driver_handle_info(efi_handle_t handle, u16 **driver_name,
366 struct efi_handler *handler;
367 struct efi_loaded_image *image;
372 * TODO: support EFI_COMPONENT_NAME2_PROTOCOL
377 ret = efi_search_protocol(handle, &efi_guid_loaded_image, &handler);
378 if (ret != EFI_SUCCESS) {
383 image = handler->protocol_interface;
384 *image_path = efi_dp_str(image->file_path);
390 * do_efi_show_drivers() - show UEFI drivers
392 * @cmdtp: Command table
393 * @flag: Command flag
394 * @argc: Number of arguments
395 * @argv: Argument array
396 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
398 * Implement efidebug "drivers" sub-command.
399 * Show all UEFI drivers and their information.
401 static int do_efi_show_drivers(struct cmd_tbl *cmdtp, int flag,
402 int argc, char *const argv[])
404 efi_handle_t *handles;
406 u16 *driver_name, *image_path_text;
409 ret = EFI_CALL(efi_locate_handle_buffer(
410 BY_PROTOCOL, &efi_guid_driver_binding_protocol,
411 NULL, &num, &handles));
412 if (ret != EFI_SUCCESS)
413 return CMD_RET_FAILURE;
416 return CMD_RET_SUCCESS;
418 printf("Driver%.*s Name Image Path\n",
419 EFI_HANDLE_WIDTH - 6, spc);
420 printf("%.*s ==================== ====================\n",
421 EFI_HANDLE_WIDTH, sep);
422 for (i = 0; i < num; i++) {
423 if (!efi_get_driver_handle_info(handles[i], &driver_name,
426 printf("%p %-20ls %ls\n", handles[i],
427 driver_name, image_path_text);
429 printf("%p %-20ls <built-in>\n",
430 handles[i], driver_name);
431 efi_free_pool(driver_name);
432 efi_free_pool(image_path_text);
436 efi_free_pool(handles);
438 return CMD_RET_SUCCESS;
442 * do_efi_show_handles() - show UEFI handles
444 * @cmdtp: Command table
445 * @flag: Command flag
446 * @argc: Number of arguments
447 * @argv: Argument array
448 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
450 * Implement efidebug "dh" sub-command.
451 * Show all UEFI handles and their information, currently all protocols
454 static int do_efi_show_handles(struct cmd_tbl *cmdtp, int flag,
455 int argc, char *const argv[])
457 efi_handle_t *handles;
459 efi_uintn_t num, count, i, j;
462 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
464 if (ret != EFI_SUCCESS)
465 return CMD_RET_FAILURE;
468 return CMD_RET_SUCCESS;
470 for (i = 0; i < num; i++) {
471 struct efi_handler *handler;
473 printf("\n%p", handles[i]);
475 printf(" (%s)", handles[i]->dev->name);
477 /* Print device path */
478 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
480 if (ret == EFI_SUCCESS)
481 printf(" %pD\n", handler->protocol_interface);
482 ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid,
484 /* Print other protocols */
485 for (j = 0; j < count; j++) {
486 if (guidcmp(guid[j], &efi_guid_device_path))
487 printf(" %pUs\n", guid[j]);
491 efi_free_pool(handles);
493 return CMD_RET_SUCCESS;
497 * do_efi_show_images() - show UEFI images
499 * @cmdtp: Command table
500 * @flag: Command flag
501 * @argc: Number of arguments
502 * @argv: Argument array
503 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
505 * Implement efidebug "images" sub-command.
506 * Show all UEFI loaded images and their information.
508 static int do_efi_show_images(struct cmd_tbl *cmdtp, int flag,
509 int argc, char *const argv[])
511 efi_print_image_infos(NULL);
513 return CMD_RET_SUCCESS;
516 static const char * const efi_mem_type_string[] = {
517 [EFI_RESERVED_MEMORY_TYPE] = "RESERVED",
518 [EFI_LOADER_CODE] = "LOADER CODE",
519 [EFI_LOADER_DATA] = "LOADER DATA",
520 [EFI_BOOT_SERVICES_CODE] = "BOOT CODE",
521 [EFI_BOOT_SERVICES_DATA] = "BOOT DATA",
522 [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE",
523 [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA",
524 [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL",
525 [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM",
526 [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM",
527 [EFI_ACPI_MEMORY_NVS] = "ACPI NVS",
528 [EFI_MMAP_IO] = "IO",
529 [EFI_MMAP_IO_PORT] = "IO PORT",
530 [EFI_PAL_CODE] = "PAL",
531 [EFI_PERSISTENT_MEMORY_TYPE] = "PERSISTENT",
534 static const struct efi_mem_attrs {
537 } efi_mem_attrs[] = {
538 {EFI_MEMORY_UC, "UC"},
539 {EFI_MEMORY_UC, "UC"},
540 {EFI_MEMORY_WC, "WC"},
541 {EFI_MEMORY_WT, "WT"},
542 {EFI_MEMORY_WB, "WB"},
543 {EFI_MEMORY_UCE, "UCE"},
544 {EFI_MEMORY_WP, "WP"},
545 {EFI_MEMORY_RP, "RP"},
546 {EFI_MEMORY_XP, "WP"},
547 {EFI_MEMORY_NV, "NV"},
548 {EFI_MEMORY_MORE_RELIABLE, "REL"},
549 {EFI_MEMORY_RO, "RO"},
550 {EFI_MEMORY_SP, "SP"},
551 {EFI_MEMORY_RUNTIME, "RT"},
555 * print_memory_attributes() - print memory map attributes
557 * @attributes: Attribute value
559 * Print memory map attributes
561 static void print_memory_attributes(u64 attributes)
565 for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
566 if (attributes & efi_mem_attrs[i].bit) {
573 puts(efi_mem_attrs[i].text);
577 #define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
580 * do_efi_show_memmap() - show UEFI memory map
582 * @cmdtp: Command table
583 * @flag: Command flag
584 * @argc: Number of arguments
585 * @argv: Argument array
586 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
588 * Implement efidebug "memmap" sub-command.
589 * Show UEFI memory map.
591 static int do_efi_show_memmap(struct cmd_tbl *cmdtp, int flag,
592 int argc, char *const argv[])
594 struct efi_mem_desc *memmap = NULL, *map;
595 efi_uintn_t map_size = 0;
600 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
601 if (ret == EFI_BUFFER_TOO_SMALL) {
602 map_size += sizeof(struct efi_mem_desc); /* for my own */
603 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, map_size,
605 if (ret != EFI_SUCCESS)
606 return CMD_RET_FAILURE;
607 ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL);
609 if (ret != EFI_SUCCESS) {
610 efi_free_pool(memmap);
611 return CMD_RET_FAILURE;
614 printf("Type Start%.*s End%.*s Attributes\n",
615 EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc);
616 printf("================ %.*s %.*s ==========\n",
617 EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep);
619 * Coverity check: dereferencing null pointer "map."
620 * This is a false positive as memmap will always be
621 * populated by allocate_pool() above.
623 for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
624 if (map->type < ARRAY_SIZE(efi_mem_type_string))
625 type = efi_mem_type_string[map->type];
629 printf("%-16s %.*llx-%.*llx", type,
631 (u64)map_to_sysmem((void *)(uintptr_t)
632 map->physical_start),
634 (u64)map_to_sysmem((void *)(uintptr_t)
635 (map->physical_start +
636 map->num_pages * EFI_PAGE_SIZE)));
638 print_memory_attributes(map->attribute);
642 efi_free_pool(memmap);
644 return CMD_RET_SUCCESS;
648 * do_efi_show_tables() - show UEFI configuration tables
650 * @cmdtp: Command table
651 * @flag: Command flag
652 * @argc: Number of arguments
653 * @argv: Argument array
654 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
656 * Implement efidebug "tables" sub-command.
657 * Show UEFI configuration tables.
659 static int do_efi_show_tables(struct cmd_tbl *cmdtp, int flag,
660 int argc, char *const argv[])
664 for (i = 0; i < systab.nr_tables; ++i)
665 printf("%pUl (%pUs)\n",
666 &systab.tables[i].guid, &systab.tables[i].guid);
668 return CMD_RET_SUCCESS;
672 * create_initrd_dp() - create a special device for our Boot### option
675 * @part: disk partition
677 * @shortform: create short form device path
678 * Return: pointer to the device path or ERR_PTR
681 struct efi_device_path *create_initrd_dp(const char *dev, const char *part,
682 const char *file, int shortform)
685 struct efi_device_path *tmp_dp = NULL, *tmp_fp = NULL, *short_fp = NULL;
686 struct efi_device_path *initrd_dp = NULL;
688 const struct efi_initrd_dp id_dp = {
691 DEVICE_PATH_TYPE_MEDIA_DEVICE,
692 DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
693 sizeof(id_dp.vendor),
695 EFI_INITRD_MEDIA_GUID,
698 DEVICE_PATH_TYPE_END,
699 DEVICE_PATH_SUB_TYPE_END,
704 ret = efi_dp_from_name(dev, part, file, &tmp_dp, &tmp_fp);
705 if (ret != EFI_SUCCESS) {
706 printf("Cannot create device path for \"%s %s\"\n", part, file);
710 short_fp = efi_dp_shorten(tmp_fp);
714 initrd_dp = efi_dp_append((const struct efi_device_path *)&id_dp,
718 efi_free_pool(tmp_dp);
719 efi_free_pool(tmp_fp);
724 * do_efi_boot_add() - set UEFI load option
726 * @cmdtp: Command table
727 * @flag: Command flag
728 * @argc: Number of arguments
729 * @argv: Argument array
730 * Return: CMD_RET_SUCCESS on success,
731 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
733 * Implement efidebug "boot add" sub-command. Create or change UEFI load option.
735 * efidebug boot add -b <id> <label> <interface> <devnum>[:<part>] <file>
736 * -i <file> <interface2> <devnum2>[:<part>] <initrd>
739 static int do_efi_boot_add(struct cmd_tbl *cmdtp, int flag,
740 int argc, char *const argv[])
747 struct efi_device_path *file_path = NULL;
748 struct efi_device_path *fp_free = NULL;
749 struct efi_device_path *final_fp = NULL;
750 struct efi_device_path *initrd_dp = NULL;
751 struct efi_load_option lo;
754 efi_uintn_t fp_size = 0;
756 int r = CMD_RET_SUCCESS;
758 guid = efi_global_variable_guid;
761 lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
762 lo.optional_data = NULL;
767 for (; argc > 0; argc--, argv++) {
770 if (*argv[0] != '-' || strlen(argv[0]) != 2) {
775 switch (argv[0][1]) {
780 if (argc < 5 || lo.label) {
784 id = (int)hextoul(argv[1], &endp);
785 if (*endp != '\0' || id > 0xffff)
786 return CMD_RET_USAGE;
788 efi_create_indexed_name(var_name16, sizeof(var_name16),
792 label = efi_convert_string(argv[2]);
794 return CMD_RET_FAILURE;
795 lo.label = label; /* label will be changed below */
798 ret = efi_dp_from_name(argv[3], argv[4], argv[5],
800 if (ret != EFI_SUCCESS) {
801 printf("Cannot create device path for \"%s %s\"\n",
807 file_path = efi_dp_shorten(fp_free);
810 fp_size += efi_dp_size(file_path) +
811 sizeof(struct efi_device_path);
819 if (argc < 3 || initrd_dp) {
824 initrd_dp = create_initrd_dp(argv[1], argv[2], argv[3],
827 printf("Cannot add an initrd\n");
833 fp_size += efi_dp_size(initrd_dp) +
834 sizeof(struct efi_device_path);
837 if (argc < 1 || lo.optional_data) {
841 lo.optional_data = (const u8 *)argv[1];
852 printf("Missing binary\n");
857 final_fp = efi_dp_concat(file_path, initrd_dp);
859 printf("Cannot create final device path\n");
864 lo.file_path = final_fp;
865 lo.file_path_length = fp_size;
867 size = efi_serialize_load_option(&lo, (u8 **)&data);
873 ret = efi_set_variable_int(var_name16, &guid,
874 EFI_VARIABLE_NON_VOLATILE |
875 EFI_VARIABLE_BOOTSERVICE_ACCESS |
876 EFI_VARIABLE_RUNTIME_ACCESS,
878 if (ret != EFI_SUCCESS) {
879 printf("Cannot set %ls\n", var_name16);
885 efi_free_pool(final_fp);
886 efi_free_pool(initrd_dp);
887 efi_free_pool(fp_free);
894 * do_efi_boot_rm() - delete UEFI load options
896 * @cmdtp: Command table
897 * @flag: Command flag
898 * @argc: Number of arguments
899 * @argv: Argument array
900 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
902 * Implement efidebug "boot rm" sub-command.
903 * Delete UEFI load options.
905 * efidebug boot rm <id> ...
907 static int do_efi_boot_rm(struct cmd_tbl *cmdtp, int flag,
908 int argc, char *const argv[])
917 return CMD_RET_USAGE;
919 guid = efi_global_variable_guid;
920 for (i = 1; i < argc; i++, argv++) {
921 id = (int)hextoul(argv[1], &endp);
922 if (*endp != '\0' || id > 0xffff)
923 return CMD_RET_FAILURE;
925 efi_create_indexed_name(var_name16, sizeof(var_name16),
927 ret = efi_set_variable_int(var_name16, &guid, 0, 0, NULL,
930 printf("Cannot remove %ls\n", var_name16);
931 return CMD_RET_FAILURE;
935 return CMD_RET_SUCCESS;
939 * show_efi_boot_opt_data() - dump UEFI load option
941 * @varname16: variable name
942 * @data: value of UEFI load option variable
943 * @size: size of the boot option
945 * Decode the value of UEFI load option variable and print information.
947 static void show_efi_boot_opt_data(u16 *varname16, void *data, size_t *size)
949 struct efi_device_path *initrd_path = NULL;
950 struct efi_load_option lo;
953 ret = efi_deserialize_load_option(&lo, data, size);
954 if (ret != EFI_SUCCESS) {
955 printf("%ls: invalid load option\n", varname16);
959 printf("%ls:\nattributes: %c%c%c (0x%08x)\n",
962 lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
963 /* FORCE RECONNECT */
964 lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
966 lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
968 printf(" label: %ls\n", lo.label);
970 printf(" file_path: %pD\n", lo.file_path);
972 initrd_path = efi_dp_from_lo(&lo, &efi_lf2_initrd_guid);
974 printf(" initrd_path: %pD\n", initrd_path);
975 efi_free_pool(initrd_path);
979 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
980 lo.optional_data, *size, true);
984 * show_efi_boot_opt() - dump UEFI load option
986 * @varname16: variable name
988 * Dump information defined by UEFI load option.
990 static void show_efi_boot_opt(u16 *varname16)
997 ret = efi_get_variable_int(varname16, &efi_global_variable_guid,
998 NULL, &size, NULL, NULL);
999 if (ret == EFI_BUFFER_TOO_SMALL) {
1000 data = malloc(size);
1002 printf("ERROR: Out of memory\n");
1005 ret = efi_get_variable_int(varname16, &efi_global_variable_guid,
1006 NULL, &size, data, NULL);
1007 if (ret == EFI_SUCCESS)
1008 show_efi_boot_opt_data(varname16, data, &size);
1014 * show_efi_boot_dump() - dump all UEFI load options
1016 * @cmdtp: Command table
1017 * @flag: Command flag
1018 * @argc: Number of arguments
1019 * @argv: Argument array
1020 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1022 * Implement efidebug "boot dump" sub-command.
1023 * Dump information of all UEFI load options defined.
1025 * efidebug boot dump
1027 static int do_efi_boot_dump(struct cmd_tbl *cmdtp, int flag,
1028 int argc, char *const argv[])
1030 u16 *var_name16, *p;
1031 efi_uintn_t buf_size, size;
1036 return CMD_RET_USAGE;
1039 var_name16 = malloc(buf_size);
1041 return CMD_RET_FAILURE;
1046 ret = efi_get_next_variable_name_int(&size, var_name16, &guid);
1047 if (ret == EFI_NOT_FOUND)
1049 if (ret == EFI_BUFFER_TOO_SMALL) {
1051 p = realloc(var_name16, buf_size);
1054 return CMD_RET_FAILURE;
1057 ret = efi_get_next_variable_name_int(&size, var_name16,
1060 if (ret != EFI_SUCCESS) {
1062 return CMD_RET_FAILURE;
1065 if (efi_varname_is_load_option(var_name16, NULL))
1066 show_efi_boot_opt(var_name16);
1071 return CMD_RET_SUCCESS;
1075 * show_efi_boot_order() - show order of UEFI load options
1077 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1079 * Show order of UEFI load options defined by BootOrder variable.
1081 static int show_efi_boot_order(void)
1088 struct efi_load_option lo;
1092 ret = efi_get_variable_int(u"BootOrder", &efi_global_variable_guid,
1093 NULL, &size, NULL, NULL);
1094 if (ret != EFI_BUFFER_TOO_SMALL) {
1095 if (ret == EFI_NOT_FOUND) {
1096 printf("BootOrder not defined\n");
1097 return CMD_RET_SUCCESS;
1099 return CMD_RET_FAILURE;
1102 bootorder = malloc(size);
1104 printf("ERROR: Out of memory\n");
1105 return CMD_RET_FAILURE;
1107 ret = efi_get_variable_int(u"BootOrder", &efi_global_variable_guid,
1108 NULL, &size, bootorder, NULL);
1109 if (ret != EFI_SUCCESS) {
1110 ret = CMD_RET_FAILURE;
1114 num = size / sizeof(u16);
1115 for (i = 0; i < num; i++) {
1116 efi_create_indexed_name(var_name16, sizeof(var_name16),
1117 "Boot", bootorder[i]);
1120 ret = efi_get_variable_int(var_name16,
1121 &efi_global_variable_guid, NULL,
1123 if (ret != EFI_BUFFER_TOO_SMALL) {
1124 printf("%2d: %ls: (not defined)\n", i + 1, var_name16);
1128 data = malloc(size);
1130 ret = CMD_RET_FAILURE;
1133 ret = efi_get_variable_int(var_name16,
1134 &efi_global_variable_guid, NULL,
1136 if (ret != EFI_SUCCESS) {
1138 ret = CMD_RET_FAILURE;
1142 ret = efi_deserialize_load_option(&lo, data, &size);
1143 if (ret != EFI_SUCCESS) {
1144 printf("%ls: invalid load option\n", var_name16);
1145 ret = CMD_RET_FAILURE;
1149 printf("%2d: %ls: %ls\n", i + 1, var_name16, lo.label);
1160 * do_efi_boot_next() - manage UEFI BootNext variable
1162 * @cmdtp: Command table
1163 * @flag: Command flag
1164 * @argc: Number of arguments
1165 * @argv: Argument array
1166 * Return: CMD_RET_SUCCESS on success,
1167 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1169 * Implement efidebug "boot next" sub-command.
1170 * Set BootNext variable.
1172 * efidebug boot next <id>
1174 static int do_efi_boot_next(struct cmd_tbl *cmdtp, int flag,
1175 int argc, char *const argv[])
1182 int r = CMD_RET_SUCCESS;
1185 return CMD_RET_USAGE;
1187 bootnext = (u16)hextoul(argv[1], &endp);
1189 printf("invalid value: %s\n", argv[1]);
1190 r = CMD_RET_FAILURE;
1194 guid = efi_global_variable_guid;
1196 ret = efi_set_variable_int(u"BootNext", &guid,
1197 EFI_VARIABLE_NON_VOLATILE |
1198 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1199 EFI_VARIABLE_RUNTIME_ACCESS,
1200 size, &bootnext, false);
1201 if (ret != EFI_SUCCESS) {
1202 printf("Cannot set BootNext\n");
1203 r = CMD_RET_FAILURE;
1210 * do_efi_boot_order() - manage UEFI BootOrder variable
1212 * @cmdtp: Command table
1213 * @flag: Command flag
1214 * @argc: Number of arguments
1215 * @argv: Argument array
1216 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1218 * Implement efidebug "boot order" sub-command.
1219 * Show order of UEFI load options, or change it in BootOrder variable.
1221 * efidebug boot order [<id> ...]
1223 static int do_efi_boot_order(struct cmd_tbl *cmdtp, int flag,
1224 int argc, char *const argv[])
1226 u16 *bootorder = NULL;
1232 int r = CMD_RET_SUCCESS;
1235 return show_efi_boot_order();
1240 size = argc * sizeof(u16);
1241 bootorder = malloc(size);
1243 return CMD_RET_FAILURE;
1245 for (i = 0; i < argc; i++) {
1246 id = (int)hextoul(argv[i], &endp);
1247 if (*endp != '\0' || id > 0xffff) {
1248 printf("invalid value: %s\n", argv[i]);
1249 r = CMD_RET_FAILURE;
1253 bootorder[i] = (u16)id;
1256 guid = efi_global_variable_guid;
1257 ret = efi_set_variable_int(u"BootOrder", &guid,
1258 EFI_VARIABLE_NON_VOLATILE |
1259 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1260 EFI_VARIABLE_RUNTIME_ACCESS,
1261 size, bootorder, true);
1262 if (ret != EFI_SUCCESS) {
1263 printf("Cannot set BootOrder\n");
1264 r = CMD_RET_FAILURE;
1272 static struct cmd_tbl cmd_efidebug_boot_sub[] = {
1273 U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
1274 U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
1275 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
1276 U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
1277 U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
1282 * do_efi_boot_opt() - manage UEFI load options
1284 * @cmdtp: Command table
1285 * @flag: Command flag
1286 * @argc: Number of arguments
1287 * @argv: Argument array
1288 * Return: CMD_RET_SUCCESS on success,
1289 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1291 * Implement efidebug "boot" sub-command.
1293 static int do_efi_boot_opt(struct cmd_tbl *cmdtp, int flag,
1294 int argc, char *const argv[])
1299 return CMD_RET_USAGE;
1303 cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
1304 ARRAY_SIZE(cmd_efidebug_boot_sub));
1306 return CMD_RET_USAGE;
1308 return cp->cmd(cmdtp, flag, argc, argv);
1312 * do_efi_test_bootmgr() - run simple bootmgr for test
1314 * @cmdtp: Command table
1315 * @flag: Command flag
1316 * @argc: Number of arguments
1317 * @argv: Argument array
1318 * Return: CMD_RET_SUCCESS on success,
1319 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1321 * Implement efidebug "test bootmgr" sub-command.
1322 * Run simple bootmgr for test.
1324 * efidebug test bootmgr
1326 static __maybe_unused int do_efi_test_bootmgr(struct cmd_tbl *cmdtp, int flag,
1327 int argc, char * const argv[])
1330 efi_uintn_t exit_data_size = 0;
1331 u16 *exit_data = NULL;
1333 void *load_options = NULL;
1335 ret = efi_bootmgr_load(&image, &load_options);
1336 printf("efi_bootmgr_load() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1338 /* We call efi_start_image() even if error for test purpose. */
1339 ret = EFI_CALL(efi_start_image(image, &exit_data_size, &exit_data));
1340 printf("efi_start_image() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1341 if (ret && exit_data)
1342 efi_free_pool(exit_data);
1347 return CMD_RET_SUCCESS;
1350 static struct cmd_tbl cmd_efidebug_test_sub[] = {
1351 #ifdef CONFIG_CMD_BOOTEFI_BOOTMGR
1352 U_BOOT_CMD_MKENT(bootmgr, CONFIG_SYS_MAXARGS, 1, do_efi_test_bootmgr,
1358 * do_efi_test() - manage UEFI load options
1360 * @cmdtp: Command table
1361 * @flag: Command flag
1362 * @argc: Number of arguments
1363 * @argv: Argument array
1364 * Return: CMD_RET_SUCCESS on success,
1365 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1367 * Implement efidebug "test" sub-command.
1369 static int do_efi_test(struct cmd_tbl *cmdtp, int flag,
1370 int argc, char * const argv[])
1375 return CMD_RET_USAGE;
1379 cp = find_cmd_tbl(argv[0], cmd_efidebug_test_sub,
1380 ARRAY_SIZE(cmd_efidebug_test_sub));
1382 return CMD_RET_USAGE;
1384 return cp->cmd(cmdtp, flag, argc, argv);
1388 * do_efi_query_info() - QueryVariableInfo EFI service
1390 * @cmdtp: Command table
1391 * @flag: Command flag
1392 * @argc: Number of arguments
1393 * @argv: Argument array
1394 * Return: CMD_RET_SUCCESS on success,
1395 * CMD_RET_USAGE or CMD_RET_FAILURE on failure
1397 * Implement efidebug "test" sub-command.
1400 static int do_efi_query_info(struct cmd_tbl *cmdtp, int flag,
1401 int argc, char * const argv[])
1405 u64 max_variable_storage_size;
1406 u64 remain_variable_storage_size;
1407 u64 max_variable_size;
1410 for (i = 1; i < argc; i++) {
1411 if (!strcmp(argv[i], "-bs"))
1412 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
1413 else if (!strcmp(argv[i], "-rt"))
1414 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
1415 else if (!strcmp(argv[i], "-nv"))
1416 attr |= EFI_VARIABLE_NON_VOLATILE;
1417 else if (!strcmp(argv[i], "-at"))
1419 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
1422 ret = efi_query_variable_info_int(attr, &max_variable_storage_size,
1423 &remain_variable_storage_size,
1424 &max_variable_size);
1425 if (ret != EFI_SUCCESS) {
1426 printf("Error: Cannot query UEFI variables, r = %lu\n",
1427 ret & ~EFI_ERROR_MASK);
1428 return CMD_RET_FAILURE;
1431 printf("Max storage size %llu\n", max_variable_storage_size);
1432 printf("Remaining storage size %llu\n", remain_variable_storage_size);
1433 printf("Max variable size %llu\n", max_variable_size);
1435 return CMD_RET_SUCCESS;
1438 static struct cmd_tbl cmd_efidebug_sub[] = {
1439 U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
1440 #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
1441 U_BOOT_CMD_MKENT(capsule, CONFIG_SYS_MAXARGS, 1, do_efi_capsule,
1444 U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
1446 U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
1448 U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
1450 U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
1452 U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables,
1454 U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_efi_test,
1456 U_BOOT_CMD_MKENT(query, CONFIG_SYS_MAXARGS, 1, do_efi_query_info,
1461 * do_efidebug() - display and configure UEFI environment
1463 * @cmdtp: Command table
1464 * @flag: Command flag
1465 * @argc: Number of arguments
1466 * @argv: Argument array
1467 * Return: CMD_RET_SUCCESS on success,
1468 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1470 * Implement efidebug command which allows us to display and
1471 * configure UEFI environment.
1473 static int do_efidebug(struct cmd_tbl *cmdtp, int flag,
1474 int argc, char *const argv[])
1480 return CMD_RET_USAGE;
1484 /* Initialize UEFI drivers */
1485 r = efi_init_obj_list();
1486 if (r != EFI_SUCCESS) {
1487 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1488 r & ~EFI_ERROR_MASK);
1489 return CMD_RET_FAILURE;
1492 cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
1493 ARRAY_SIZE(cmd_efidebug_sub));
1495 return CMD_RET_USAGE;
1497 return cp->cmd(cmdtp, flag, argc, argv);
1500 #ifdef CONFIG_SYS_LONGHELP
1501 static char efidebug_help_text[] =
1502 " - UEFI Shell-like interface to configure UEFI environment\n"
1504 "efidebug boot add - set UEFI BootXXXX variable\n"
1505 " -b|-B <bootid> <label> <interface> <devnum>[:<part>] <file path>\n"
1506 " -i|-I <interface> <devnum>[:<part>] <initrd file path>\n"
1507 " (-b, -i for short form device path)\n"
1508 " -s '<optional data>'\n"
1509 "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
1510 " - delete UEFI BootXXXX variables\n"
1511 "efidebug boot dump\n"
1512 " - dump all UEFI BootXXXX variables\n"
1513 "efidebug boot next <bootid>\n"
1514 " - set UEFI BootNext variable\n"
1515 "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
1516 " - set/show UEFI boot order\n"
1518 #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
1519 "efidebug capsule update [-v] <capsule address>\n"
1520 " - process a capsule\n"
1521 "efidebug capsule disk-update\n"
1522 " - update a capsule from disk\n"
1523 "efidebug capsule show <capsule address>\n"
1524 " - show capsule information\n"
1525 "efidebug capsule result [<capsule result var>]\n"
1526 " - show a capsule update result\n"
1527 #ifdef CONFIG_EFI_ESRT
1528 "efidebug capsule esrt\n"
1529 " - print the ESRT\n"
1533 "efidebug drivers\n"
1534 " - show UEFI drivers\n"
1536 " - show UEFI handles\n"
1538 " - show loaded images\n"
1540 " - show UEFI memory map\n"
1542 " - show UEFI configuration tables\n"
1543 #ifdef CONFIG_CMD_BOOTEFI_BOOTMGR
1544 "efidebug test bootmgr\n"
1545 " - run simple bootmgr for test\n"
1547 "efidebug query [-nv][-bs][-rt][-at]\n"
1548 " - show size of UEFI variables store\n";
1552 efidebug, CONFIG_SYS_MAXARGS, 0, do_efidebug,
1553 "Configure UEFI environment",