1 // SPDX-License-Identifier: GPL-2.0+
3 * UEFI Shell-like command
5 * Copyright (c) 2018 AKASHI Takahiro, Linaro Limited
10 #include <dm/device.h>
11 #include <efi_dt_fixup.h>
12 #include <efi_load_initrd.h>
13 #include <efi_loader.h>
15 #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;
178 return CMD_RET_USAGE;
180 esrt = efi_get_configuration_table(&efi_esrt_guid);
182 log_info("ESRT: table not present\n");
183 return CMD_RET_SUCCESS;
186 printf("========================================\n");
187 printf("ESRT: fw_resource_count=%d\n", esrt->fw_resource_count);
188 printf("ESRT: fw_resource_count_max=%d\n", esrt->fw_resource_count_max);
189 printf("ESRT: fw_resource_version=%lld\n", esrt->fw_resource_version);
191 for (int idx = 0; idx < esrt->fw_resource_count; idx++) {
192 printf("[entry %d]==============================\n", idx);
193 printf("ESRT: fw_class=%pUL\n", &esrt->entries[idx].fw_class);
194 printf("ESRT: fw_type=%s\n", EFI_FW_TYPE_STR_GET(esrt->entries[idx].fw_type));
195 printf("ESRT: fw_version=%d\n", esrt->entries[idx].fw_version);
196 printf("ESRT: lowest_supported_fw_version=%d\n",
197 esrt->entries[idx].lowest_supported_fw_version);
198 printf("ESRT: capsule_flags=%d\n",
199 esrt->entries[idx].capsule_flags);
200 printf("ESRT: last_attempt_version=%d\n",
201 esrt->entries[idx].last_attempt_version);
202 printf("ESRT: last_attempt_status=%s\n",
203 EFI_FW_STATUS_STR_GET(esrt->entries[idx].last_attempt_status));
205 printf("========================================\n");
207 return CMD_RET_SUCCESS;
209 #endif /* CONFIG_EFI_ESRT */
211 * do_efi_capsule_res() - show a capsule update result
213 * @cmdtp: Command table
214 * @flag: Command flag
215 * @argc: Number of arguments
216 * @argv: Argument array
217 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
219 * Implement efidebug "capsule result" sub-command.
220 * show a capsule update result.
221 * If result number is not specified, CapsuleLast will be shown.
223 * efidebug capsule result [<capsule result number>]
225 static int do_efi_capsule_res(struct cmd_tbl *cmdtp, int flag,
226 int argc, char * const argv[])
232 struct efi_capsule_result_variable_header *result = NULL;
236 if (argc != 1 && argc != 2)
237 return CMD_RET_USAGE;
239 guid = efi_guid_capsule_report;
241 size = sizeof(var_name16);
242 ret = efi_get_variable_int(u"CapsuleLast", &guid, NULL,
243 &size, var_name16, NULL);
245 if (ret != EFI_SUCCESS) {
246 if (ret == EFI_NOT_FOUND)
247 printf("CapsuleLast doesn't exist\n");
249 printf("Failed to get CapsuleLast\n");
251 return CMD_RET_FAILURE;
253 printf("CapsuleLast is %ls\n", var_name16);
258 capsule_id = hextoul(argv[0], &endp);
259 if (capsule_id < 0 || capsule_id > 0xffff)
260 return CMD_RET_USAGE;
262 efi_create_indexed_name(var_name16, sizeof(var_name16),
263 "Capsule", capsule_id);
267 ret = efi_get_variable_int(var_name16, &guid, NULL, &size, NULL, NULL);
268 if (ret == EFI_BUFFER_TOO_SMALL) {
269 result = malloc(size);
271 return CMD_RET_FAILURE;
272 ret = efi_get_variable_int(var_name16, &guid, NULL, &size,
275 if (ret != EFI_SUCCESS) {
277 printf("Failed to get %ls\n", var_name16);
279 return CMD_RET_FAILURE;
282 printf("Result total size: 0x%x\n", result->variable_total_size);
283 printf("Capsule guid: %pUl\n", &result->capsule_guid);
284 printf("Time processed: %04d-%02d-%02d %02d:%02d:%02d\n",
285 result->capsule_processed.year, result->capsule_processed.month,
286 result->capsule_processed.day, result->capsule_processed.hour,
287 result->capsule_processed.minute,
288 result->capsule_processed.second);
289 printf("Capsule status: 0x%lx\n", result->capsule_status);
293 return CMD_RET_SUCCESS;
296 static struct cmd_tbl cmd_efidebug_capsule_sub[] = {
297 U_BOOT_CMD_MKENT(update, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_update,
299 U_BOOT_CMD_MKENT(show, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_show,
301 #ifdef CONFIG_EFI_ESRT
302 U_BOOT_CMD_MKENT(esrt, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_esrt,
305 #ifdef CONFIG_EFI_CAPSULE_ON_DISK
306 U_BOOT_CMD_MKENT(disk-update, 0, 0, do_efi_capsule_on_disk_update,
309 U_BOOT_CMD_MKENT(result, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_res,
314 * do_efi_capsule() - manage UEFI capsules
316 * @cmdtp: Command table
317 * @flag: Command flag
318 * @argc: Number of arguments
319 * @argv: Argument array
320 * Return: CMD_RET_SUCCESS on success,
321 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
323 * Implement efidebug "capsule" sub-command.
325 static int do_efi_capsule(struct cmd_tbl *cmdtp, int flag,
326 int argc, char * const argv[])
331 return CMD_RET_USAGE;
335 cp = find_cmd_tbl(argv[0], cmd_efidebug_capsule_sub,
336 ARRAY_SIZE(cmd_efidebug_capsule_sub));
338 return CMD_RET_USAGE;
340 return cp->cmd(cmdtp, flag, argc, argv);
342 #endif /* CONFIG_EFI_HAVE_CAPSULE_SUPPORT */
344 #define EFI_HANDLE_WIDTH ((int)sizeof(efi_handle_t) * 2)
346 static const char spc[] = " ";
347 static const char sep[] = "================";
350 * efi_get_driver_handle_info() - get information of UEFI driver
352 * @handle: Handle of UEFI device
353 * @driver_name: Driver name
354 * @image_path: Pointer to text of device path
355 * Return: 0 on success, -1 on failure
357 * Currently return no useful information as all UEFI drivers are
360 static int efi_get_driver_handle_info(efi_handle_t handle, u16 **driver_name,
363 struct efi_handler *handler;
364 struct efi_loaded_image *image;
369 * TODO: support EFI_COMPONENT_NAME2_PROTOCOL
374 ret = efi_search_protocol(handle, &efi_guid_loaded_image, &handler);
375 if (ret != EFI_SUCCESS) {
380 image = handler->protocol_interface;
381 *image_path = efi_dp_str(image->file_path);
387 * do_efi_show_drivers() - show UEFI drivers
389 * @cmdtp: Command table
390 * @flag: Command flag
391 * @argc: Number of arguments
392 * @argv: Argument array
393 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
395 * Implement efidebug "drivers" sub-command.
396 * Show all UEFI drivers and their information.
398 static int do_efi_show_drivers(struct cmd_tbl *cmdtp, int flag,
399 int argc, char *const argv[])
401 efi_handle_t *handles;
403 u16 *driver_name, *image_path_text;
406 ret = EFI_CALL(efi_locate_handle_buffer(
407 BY_PROTOCOL, &efi_guid_driver_binding_protocol,
408 NULL, &num, &handles));
409 if (ret != EFI_SUCCESS)
410 return CMD_RET_FAILURE;
413 return CMD_RET_SUCCESS;
415 printf("Driver%.*s Name Image Path\n",
416 EFI_HANDLE_WIDTH - 6, spc);
417 printf("%.*s ==================== ====================\n",
418 EFI_HANDLE_WIDTH, sep);
419 for (i = 0; i < num; i++) {
420 if (!efi_get_driver_handle_info(handles[i], &driver_name,
423 printf("%p %-20ls %ls\n", handles[i],
424 driver_name, image_path_text);
426 printf("%p %-20ls <built-in>\n",
427 handles[i], driver_name);
428 efi_free_pool(driver_name);
429 efi_free_pool(image_path_text);
433 efi_free_pool(handles);
435 return CMD_RET_SUCCESS;
439 * do_efi_show_handles() - show UEFI handles
441 * @cmdtp: Command table
442 * @flag: Command flag
443 * @argc: Number of arguments
444 * @argv: Argument array
445 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
447 * Implement efidebug "dh" sub-command.
448 * Show all UEFI handles and their information, currently all protocols
451 static int do_efi_show_handles(struct cmd_tbl *cmdtp, int flag,
452 int argc, char *const argv[])
454 efi_handle_t *handles;
456 efi_uintn_t num, count, i, j;
459 ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL,
461 if (ret != EFI_SUCCESS)
462 return CMD_RET_FAILURE;
465 return CMD_RET_SUCCESS;
467 for (i = 0; i < num; i++) {
468 struct efi_handler *handler;
470 printf("\n%p", handles[i]);
472 printf(" (%s)", handles[i]->dev->name);
474 /* Print device path */
475 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
477 if (ret == EFI_SUCCESS)
478 printf(" %pD\n", handler->protocol_interface);
479 ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid,
481 /* Print other protocols */
482 for (j = 0; j < count; j++) {
483 if (guidcmp(guid[j], &efi_guid_device_path))
484 printf(" %pUs\n", guid[j]);
489 efi_free_pool(handles);
491 return CMD_RET_SUCCESS;
495 * do_efi_show_images() - show UEFI images
497 * @cmdtp: Command table
498 * @flag: Command flag
499 * @argc: Number of arguments
500 * @argv: Argument array
501 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
503 * Implement efidebug "images" sub-command.
504 * Show all UEFI loaded images and their information.
506 static int do_efi_show_images(struct cmd_tbl *cmdtp, int flag,
507 int argc, char *const argv[])
509 efi_print_image_infos(NULL);
511 return CMD_RET_SUCCESS;
514 static const char * const efi_mem_type_string[] = {
515 [EFI_RESERVED_MEMORY_TYPE] = "RESERVED",
516 [EFI_LOADER_CODE] = "LOADER CODE",
517 [EFI_LOADER_DATA] = "LOADER DATA",
518 [EFI_BOOT_SERVICES_CODE] = "BOOT CODE",
519 [EFI_BOOT_SERVICES_DATA] = "BOOT DATA",
520 [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE",
521 [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA",
522 [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL",
523 [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM",
524 [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM",
525 [EFI_ACPI_MEMORY_NVS] = "ACPI NVS",
526 [EFI_MMAP_IO] = "IO",
527 [EFI_MMAP_IO_PORT] = "IO PORT",
528 [EFI_PAL_CODE] = "PAL",
529 [EFI_PERSISTENT_MEMORY_TYPE] = "PERSISTENT",
532 static const struct efi_mem_attrs {
535 } efi_mem_attrs[] = {
536 {EFI_MEMORY_UC, "UC"},
537 {EFI_MEMORY_UC, "UC"},
538 {EFI_MEMORY_WC, "WC"},
539 {EFI_MEMORY_WT, "WT"},
540 {EFI_MEMORY_WB, "WB"},
541 {EFI_MEMORY_UCE, "UCE"},
542 {EFI_MEMORY_WP, "WP"},
543 {EFI_MEMORY_RP, "RP"},
544 {EFI_MEMORY_XP, "WP"},
545 {EFI_MEMORY_NV, "NV"},
546 {EFI_MEMORY_MORE_RELIABLE, "REL"},
547 {EFI_MEMORY_RO, "RO"},
548 {EFI_MEMORY_SP, "SP"},
549 {EFI_MEMORY_RUNTIME, "RT"},
553 * print_memory_attributes() - print memory map attributes
555 * @attributes: Attribute value
557 * Print memory map attributes
559 static void print_memory_attributes(u64 attributes)
563 for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++)
564 if (attributes & efi_mem_attrs[i].bit) {
571 puts(efi_mem_attrs[i].text);
575 #define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2)
578 * do_efi_show_memmap() - show UEFI memory map
580 * @cmdtp: Command table
581 * @flag: Command flag
582 * @argc: Number of arguments
583 * @argv: Argument array
584 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
586 * Implement efidebug "memmap" sub-command.
587 * Show UEFI memory map.
589 static int do_efi_show_memmap(struct cmd_tbl *cmdtp, int flag,
590 int argc, char *const argv[])
592 struct efi_mem_desc *memmap, *map;
593 efi_uintn_t map_size;
598 ret = efi_get_memory_map_alloc(&map_size, &memmap);
599 if (ret != EFI_SUCCESS)
600 return CMD_RET_FAILURE;
602 printf("Type Start%.*s End%.*s Attributes\n",
603 EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc);
604 printf("================ %.*s %.*s ==========\n",
605 EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep);
607 * Coverity check: dereferencing null pointer "map."
608 * This is a false positive as memmap will always be
609 * populated by allocate_pool() above.
611 for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) {
612 if (map->type < ARRAY_SIZE(efi_mem_type_string))
613 type = efi_mem_type_string[map->type];
617 printf("%-16s %.*llx-%.*llx", type,
619 (u64)map_to_sysmem((void *)(uintptr_t)
620 map->physical_start),
622 (u64)map_to_sysmem((void *)(uintptr_t)
623 (map->physical_start +
624 map->num_pages * EFI_PAGE_SIZE)));
626 print_memory_attributes(map->attribute);
630 efi_free_pool(memmap);
632 return CMD_RET_SUCCESS;
636 * do_efi_show_tables() - show UEFI configuration tables
638 * @cmdtp: Command table
639 * @flag: Command flag
640 * @argc: Number of arguments
641 * @argv: Argument array
642 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
644 * Implement efidebug "tables" sub-command.
645 * Show UEFI configuration tables.
647 static int do_efi_show_tables(struct cmd_tbl *cmdtp, int flag,
648 int argc, char *const argv[])
650 efi_show_tables(&systab);
652 return CMD_RET_SUCCESS;
656 * enum efi_lo_dp_part - part of device path in load option
658 enum efi_lo_dp_part {
659 /** @EFI_LO_DP_PART_BINARY: binary */
660 EFI_LO_DP_PART_BINARY,
661 /** @EFI_LO_DP_PART_INITRD: initial RAM disk */
662 EFI_LO_DP_PART_INITRD,
663 /** @EFI_LP_DP_PART_FDT: device-tree */
668 * create_lo_dp_part() - create a special device path for our Boot### option
671 * @part: disk partition
673 * @shortform: create short form device path
674 * @type: part of device path to be created
675 * Return: pointer to the device path or ERR_PTR
678 struct efi_device_path *create_lo_dp_part(const char *dev, const char *part,
679 const char *file, bool shortform,
680 enum efi_lo_dp_part type)
683 struct efi_device_path *tmp_dp = NULL, *tmp_fp = NULL, *short_fp = NULL;
684 struct efi_device_path *dp = NULL;
685 const struct efi_device_path *dp_prefix;
687 const struct efi_lo_dp_prefix fdt_dp = {
690 DEVICE_PATH_TYPE_MEDIA_DEVICE,
691 DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
692 sizeof(fdt_dp.vendor),
697 DEVICE_PATH_TYPE_END,
698 DEVICE_PATH_SUB_TYPE_END,
702 const struct efi_lo_dp_prefix initrd_dp = {
705 DEVICE_PATH_TYPE_MEDIA_DEVICE,
706 DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
707 sizeof(initrd_dp.vendor),
709 EFI_INITRD_MEDIA_GUID,
712 DEVICE_PATH_TYPE_END,
713 DEVICE_PATH_SUB_TYPE_END,
714 sizeof(initrd_dp.end),
719 case EFI_LO_DP_PART_INITRD:
720 dp_prefix = &initrd_dp.vendor.dp;
722 case EFI_LP_DP_PART_FDT:
723 dp_prefix = &fdt_dp.vendor.dp;
730 ret = efi_dp_from_name(dev, part, file, &tmp_dp, &tmp_fp);
731 if (ret != EFI_SUCCESS) {
732 printf("Cannot create device path for \"%s %s\"\n", part, file);
736 short_fp = efi_dp_shorten(tmp_fp);
740 dp = efi_dp_concat(dp_prefix, short_fp, 0);
743 efi_free_pool(tmp_dp);
744 efi_free_pool(tmp_fp);
749 * efi_boot_add_uri() - set URI load option
751 * @argc: Number of arguments
752 * @argv: Argument array
753 * @var_name16: variable name buffer
754 * @var_name16_size: variable name buffer size
755 * @lo: pointer to the load option
756 * @file_path: buffer to set the generated device path pointer
757 * @fp_size: file_path size
758 * Return: CMD_RET_SUCCESS on success,
759 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
761 static int efi_boot_add_uri(int argc, char *const argv[], u16 *var_name16,
762 size_t var_name16_size, struct efi_load_option *lo,
763 struct efi_device_path **file_path,
764 efi_uintn_t *fp_size)
770 efi_uintn_t uridp_len;
771 struct efi_device_path_uri *uridp;
773 if (argc < 3 || lo->label)
774 return CMD_RET_USAGE;
776 id = (int)hextoul(argv[1], &endp);
777 if (*endp != '\0' || id > 0xffff)
778 return CMD_RET_USAGE;
780 label = efi_convert_string(argv[2]);
782 return CMD_RET_FAILURE;
784 if (!wget_validate_uri(argv[3])) {
785 printf("ERROR: invalid URI\n");
786 return CMD_RET_FAILURE;
789 efi_create_indexed_name(var_name16, var_name16_size, "Boot", id);
792 uridp_len = sizeof(struct efi_device_path) + strlen(argv[3]) + 1;
793 uridp = efi_alloc(uridp_len + sizeof(END));
795 log_err("Out of memory\n");
796 return CMD_RET_FAILURE;
798 uridp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
799 uridp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_URI;
800 uridp->dp.length = uridp_len;
801 strcpy(uridp->uri, argv[3]);
802 pos = (char *)uridp + uridp_len;
803 memcpy(pos, &END, sizeof(END));
805 *file_path = &uridp->dp;
806 *fp_size += uridp_len + sizeof(END);
808 return CMD_RET_SUCCESS;
812 * do_efi_boot_add() - set UEFI load option
814 * @cmdtp: Command table
815 * @flag: Command flag
816 * @argc: Number of arguments
817 * @argv: Argument array
818 * Return: CMD_RET_SUCCESS on success,
819 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
821 * Implement efidebug "boot add" sub-command. Create or change UEFI load option.
823 * efidebug boot add -b <id> <label> <interface> <devnum>[:<part>] <file>
824 * -i <file> <interface2> <devnum2>[:<part>] <initrd>
827 static int do_efi_boot_add(struct cmd_tbl *cmdtp, int flag,
828 int argc, char *const argv[])
835 struct efi_device_path *file_path = NULL;
836 struct efi_device_path *initrd_dp = NULL;
837 struct efi_device_path *fdt_dp = NULL;
838 struct efi_load_option lo;
841 efi_uintn_t fp_size = 0;
843 int r = CMD_RET_SUCCESS;
845 guid = efi_global_variable_guid;
848 lo.attributes = LOAD_OPTION_ACTIVE; /* always ACTIVE */
849 lo.optional_data = NULL;
854 for (; argc > 0; argc--, argv++) {
857 if (*argv[0] != '-' || strlen(argv[0]) != 2) {
862 switch (argv[0][1]) {
867 if (argc < 5 || lo.label) {
871 id = (int)hextoul(argv[1], &endp);
872 if (*endp != '\0' || id > 0xffff)
873 return CMD_RET_USAGE;
875 efi_create_indexed_name(var_name16, sizeof(var_name16),
879 label = efi_convert_string(argv[2]);
881 return CMD_RET_FAILURE;
882 lo.label = label; /* label will be changed below */
885 file_path = create_lo_dp_part(argv[3], argv[4], argv[5],
887 EFI_LO_DP_PART_BINARY);
895 if (argc < 3 || fdt_dp) {
900 fdt_dp = create_lo_dp_part(argv[1], argv[2], argv[3],
904 printf("Cannot add a device-tree\n");
915 if (argc < 3 || initrd_dp) {
920 initrd_dp = create_lo_dp_part(argv[1], argv[2], argv[3],
922 EFI_LO_DP_PART_INITRD);
924 printf("Cannot add an initrd\n");
932 if (argc < 1 || lo.optional_data) {
936 lo.optional_data = (const u8 *)argv[1];
941 if (IS_ENABLED(CONFIG_EFI_HTTP_BOOT)) {
942 r = efi_boot_add_uri(argc, argv, var_name16,
943 sizeof(var_name16), &lo,
944 &file_path, &fp_size);
945 if (r != CMD_RET_SUCCESS)
961 printf("Missing binary\n");
966 ret = efi_load_option_dp_join(&file_path, &fp_size, initrd_dp, fdt_dp);
967 if (ret != EFI_SUCCESS) {
968 printf("Cannot create final device path\n");
973 lo.file_path = file_path;
974 lo.file_path_length = fp_size;
976 size = efi_serialize_load_option(&lo, (u8 **)&data);
982 ret = efi_set_variable_int(var_name16, &guid,
983 EFI_VARIABLE_NON_VOLATILE |
984 EFI_VARIABLE_BOOTSERVICE_ACCESS |
985 EFI_VARIABLE_RUNTIME_ACCESS,
987 if (ret != EFI_SUCCESS) {
988 printf("Cannot set %ls\n", var_name16);
994 efi_free_pool(initrd_dp);
995 efi_free_pool(fdt_dp);
996 efi_free_pool(file_path);
1003 * do_efi_boot_rm() - delete UEFI load options
1005 * @cmdtp: Command table
1006 * @flag: Command flag
1007 * @argc: Number of arguments
1008 * @argv: Argument array
1009 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1011 * Implement efidebug "boot rm" sub-command.
1012 * Delete UEFI load options.
1014 * efidebug boot rm <id> ...
1016 static int do_efi_boot_rm(struct cmd_tbl *cmdtp, int flag,
1017 int argc, char *const argv[])
1026 return CMD_RET_USAGE;
1028 guid = efi_global_variable_guid;
1029 for (i = 1; i < argc; i++, argv++) {
1030 id = (int)hextoul(argv[1], &endp);
1031 if (*endp != '\0' || id > 0xffff)
1032 return CMD_RET_FAILURE;
1034 efi_create_indexed_name(var_name16, sizeof(var_name16),
1036 ret = efi_set_variable_int(var_name16, &guid, 0, 0, NULL,
1039 printf("Cannot remove %ls\n", var_name16);
1040 return CMD_RET_FAILURE;
1044 return CMD_RET_SUCCESS;
1048 * show_efi_boot_opt_data() - dump UEFI load option
1050 * @varname16: variable name
1051 * @data: value of UEFI load option variable
1052 * @size: size of the boot option
1054 * Decode the value of UEFI load option variable and print information.
1056 static void show_efi_boot_opt_data(u16 *varname16, void *data, size_t *size)
1058 struct efi_device_path *fdt_path;
1059 struct efi_device_path *initrd_path;
1060 struct efi_load_option lo;
1063 ret = efi_deserialize_load_option(&lo, data, size);
1064 if (ret != EFI_SUCCESS) {
1065 printf("%ls: invalid load option\n", varname16);
1069 printf("%ls:\nattributes: %c%c%c (0x%08x)\n",
1072 lo.attributes & LOAD_OPTION_ACTIVE ? 'A' : '-',
1073 /* FORCE RECONNECT */
1074 lo.attributes & LOAD_OPTION_FORCE_RECONNECT ? 'R' : '-',
1076 lo.attributes & LOAD_OPTION_HIDDEN ? 'H' : '-',
1078 printf(" label: %ls\n", lo.label);
1080 printf(" file_path: %pD\n", lo.file_path);
1082 initrd_path = efi_dp_from_lo(&lo, &efi_lf2_initrd_guid);
1084 printf(" initrd_path: %pD\n", initrd_path);
1085 efi_free_pool(initrd_path);
1088 fdt_path = efi_dp_from_lo(&lo, &efi_guid_fdt);
1090 printf(" device-tree path: %pD\n", fdt_path);
1091 efi_free_pool(fdt_path);
1095 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
1096 lo.optional_data, *size, true);
1100 * show_efi_boot_opt() - dump UEFI load option
1102 * @varname16: variable name
1104 * Dump information defined by UEFI load option.
1106 static void show_efi_boot_opt(u16 *varname16)
1113 ret = efi_get_variable_int(varname16, &efi_global_variable_guid,
1114 NULL, &size, NULL, NULL);
1115 if (ret == EFI_BUFFER_TOO_SMALL) {
1116 data = malloc(size);
1118 printf("ERROR: Out of memory\n");
1121 ret = efi_get_variable_int(varname16, &efi_global_variable_guid,
1122 NULL, &size, data, NULL);
1123 if (ret == EFI_SUCCESS)
1124 show_efi_boot_opt_data(varname16, data, &size);
1130 * do_efi_boot_dump() - dump all UEFI load options
1132 * @cmdtp: Command table
1133 * @flag: Command flag
1134 * @argc: Number of arguments
1135 * @argv: Argument array
1136 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1138 * Implement efidebug "boot dump" sub-command.
1139 * Dump information of all UEFI load options defined.
1141 * efidebug boot dump
1143 static int do_efi_boot_dump(struct cmd_tbl *cmdtp, int flag,
1144 int argc, char *const argv[])
1146 u16 *var_name16, *p;
1147 efi_uintn_t buf_size, size;
1152 return CMD_RET_USAGE;
1155 var_name16 = malloc(buf_size);
1157 return CMD_RET_FAILURE;
1162 ret = efi_get_next_variable_name_int(&size, var_name16, &guid);
1163 if (ret == EFI_NOT_FOUND)
1165 if (ret == EFI_BUFFER_TOO_SMALL) {
1167 p = realloc(var_name16, buf_size);
1170 return CMD_RET_FAILURE;
1173 ret = efi_get_next_variable_name_int(&size, var_name16,
1176 if (ret != EFI_SUCCESS) {
1178 return CMD_RET_FAILURE;
1181 if (efi_varname_is_load_option(var_name16, NULL))
1182 show_efi_boot_opt(var_name16);
1187 return CMD_RET_SUCCESS;
1191 * show_efi_boot_order() - show order of UEFI load options
1193 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1195 * Show order of UEFI load options defined by BootOrder variable.
1197 static int show_efi_boot_order(void)
1204 struct efi_load_option lo;
1208 ret = efi_get_variable_int(u"BootOrder", &efi_global_variable_guid,
1209 NULL, &size, NULL, NULL);
1210 if (ret != EFI_BUFFER_TOO_SMALL) {
1211 if (ret == EFI_NOT_FOUND) {
1212 printf("BootOrder not defined\n");
1213 return CMD_RET_SUCCESS;
1215 return CMD_RET_FAILURE;
1218 bootorder = malloc(size);
1220 printf("ERROR: Out of memory\n");
1221 return CMD_RET_FAILURE;
1223 ret = efi_get_variable_int(u"BootOrder", &efi_global_variable_guid,
1224 NULL, &size, bootorder, NULL);
1225 if (ret != EFI_SUCCESS) {
1226 ret = CMD_RET_FAILURE;
1230 num = size / sizeof(u16);
1231 for (i = 0; i < num; i++) {
1232 efi_create_indexed_name(var_name16, sizeof(var_name16),
1233 "Boot", bootorder[i]);
1236 ret = efi_get_variable_int(var_name16,
1237 &efi_global_variable_guid, NULL,
1239 if (ret != EFI_BUFFER_TOO_SMALL) {
1240 printf("%2d: %ls: (not defined)\n", i + 1, var_name16);
1244 data = malloc(size);
1246 ret = CMD_RET_FAILURE;
1249 ret = efi_get_variable_int(var_name16,
1250 &efi_global_variable_guid, NULL,
1252 if (ret != EFI_SUCCESS) {
1254 ret = CMD_RET_FAILURE;
1258 ret = efi_deserialize_load_option(&lo, data, &size);
1259 if (ret != EFI_SUCCESS) {
1260 printf("%ls: invalid load option\n", var_name16);
1261 ret = CMD_RET_FAILURE;
1265 printf("%2d: %ls: %ls\n", i + 1, var_name16, lo.label);
1276 * do_efi_boot_next() - manage UEFI BootNext variable
1278 * @cmdtp: Command table
1279 * @flag: Command flag
1280 * @argc: Number of arguments
1281 * @argv: Argument array
1282 * Return: CMD_RET_SUCCESS on success,
1283 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1285 * Implement efidebug "boot next" sub-command.
1286 * Set BootNext variable.
1288 * efidebug boot next <id>
1290 static int do_efi_boot_next(struct cmd_tbl *cmdtp, int flag,
1291 int argc, char *const argv[])
1298 int r = CMD_RET_SUCCESS;
1301 return CMD_RET_USAGE;
1303 bootnext = (u16)hextoul(argv[1], &endp);
1305 printf("invalid value: %s\n", argv[1]);
1306 r = CMD_RET_FAILURE;
1310 guid = efi_global_variable_guid;
1312 ret = efi_set_variable_int(u"BootNext", &guid,
1313 EFI_VARIABLE_NON_VOLATILE |
1314 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1315 EFI_VARIABLE_RUNTIME_ACCESS,
1316 size, &bootnext, false);
1317 if (ret != EFI_SUCCESS) {
1318 printf("Cannot set BootNext\n");
1319 r = CMD_RET_FAILURE;
1326 * do_efi_boot_order() - manage UEFI BootOrder variable
1328 * @cmdtp: Command table
1329 * @flag: Command flag
1330 * @argc: Number of arguments
1331 * @argv: Argument array
1332 * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
1334 * Implement efidebug "boot order" sub-command.
1335 * Show order of UEFI load options, or change it in BootOrder variable.
1337 * efidebug boot order [<id> ...]
1339 static int do_efi_boot_order(struct cmd_tbl *cmdtp, int flag,
1340 int argc, char *const argv[])
1342 u16 *bootorder = NULL;
1348 int r = CMD_RET_SUCCESS;
1351 return show_efi_boot_order();
1356 size = argc * sizeof(u16);
1357 bootorder = malloc(size);
1359 return CMD_RET_FAILURE;
1361 for (i = 0; i < argc; i++) {
1362 id = (int)hextoul(argv[i], &endp);
1363 if (*endp != '\0' || id > 0xffff) {
1364 printf("invalid value: %s\n", argv[i]);
1365 r = CMD_RET_FAILURE;
1369 bootorder[i] = (u16)id;
1372 guid = efi_global_variable_guid;
1373 ret = efi_set_variable_int(u"BootOrder", &guid,
1374 EFI_VARIABLE_NON_VOLATILE |
1375 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1376 EFI_VARIABLE_RUNTIME_ACCESS,
1377 size, bootorder, true);
1378 if (ret != EFI_SUCCESS) {
1379 printf("Cannot set BootOrder\n");
1380 r = CMD_RET_FAILURE;
1388 static struct cmd_tbl cmd_efidebug_boot_sub[] = {
1389 U_BOOT_CMD_MKENT(add, CONFIG_SYS_MAXARGS, 1, do_efi_boot_add, "", ""),
1390 U_BOOT_CMD_MKENT(rm, CONFIG_SYS_MAXARGS, 1, do_efi_boot_rm, "", ""),
1391 U_BOOT_CMD_MKENT(dump, CONFIG_SYS_MAXARGS, 1, do_efi_boot_dump, "", ""),
1392 U_BOOT_CMD_MKENT(next, CONFIG_SYS_MAXARGS, 1, do_efi_boot_next, "", ""),
1393 U_BOOT_CMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_efi_boot_order,
1398 * do_efi_boot_opt() - manage UEFI load options
1400 * @cmdtp: Command table
1401 * @flag: Command flag
1402 * @argc: Number of arguments
1403 * @argv: Argument array
1404 * Return: CMD_RET_SUCCESS on success,
1405 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1407 * Implement efidebug "boot" sub-command.
1409 static int do_efi_boot_opt(struct cmd_tbl *cmdtp, int flag,
1410 int argc, char *const argv[])
1415 return CMD_RET_USAGE;
1419 cp = find_cmd_tbl(argv[0], cmd_efidebug_boot_sub,
1420 ARRAY_SIZE(cmd_efidebug_boot_sub));
1422 return CMD_RET_USAGE;
1424 return cp->cmd(cmdtp, flag, argc, argv);
1428 * do_efi_test_bootmgr() - run simple bootmgr for test
1430 * @cmdtp: Command table
1431 * @flag: Command flag
1432 * @argc: Number of arguments
1433 * @argv: Argument array
1434 * Return: CMD_RET_SUCCESS on success,
1435 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1437 * Implement efidebug "test bootmgr" sub-command.
1438 * Run simple bootmgr for test.
1440 * efidebug test bootmgr
1442 static __maybe_unused int do_efi_test_bootmgr(struct cmd_tbl *cmdtp, int flag,
1443 int argc, char * const argv[])
1446 efi_uintn_t exit_data_size = 0;
1447 u16 *exit_data = NULL;
1449 void *load_options = NULL;
1451 ret = efi_bootmgr_load(&image, &load_options);
1452 printf("efi_bootmgr_load() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1453 if (ret != EFI_SUCCESS)
1454 return CMD_RET_SUCCESS;
1456 /* We call efi_start_image() even if error for test purpose. */
1457 ret = EFI_CALL(efi_start_image(image, &exit_data_size, &exit_data));
1458 printf("efi_start_image() returned: %ld\n", ret & ~EFI_ERROR_MASK);
1459 if (ret && exit_data)
1460 efi_free_pool(exit_data);
1463 return CMD_RET_SUCCESS;
1466 static struct cmd_tbl cmd_efidebug_test_sub[] = {
1467 #ifdef CONFIG_EFI_BOOTMGR
1468 U_BOOT_CMD_MKENT(bootmgr, CONFIG_SYS_MAXARGS, 1, do_efi_test_bootmgr,
1474 * do_efi_test() - manage UEFI load options
1476 * @cmdtp: Command table
1477 * @flag: Command flag
1478 * @argc: Number of arguments
1479 * @argv: Argument array
1480 * Return: CMD_RET_SUCCESS on success,
1481 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1483 * Implement efidebug "test" sub-command.
1485 static int do_efi_test(struct cmd_tbl *cmdtp, int flag,
1486 int argc, char * const argv[])
1491 return CMD_RET_USAGE;
1495 cp = find_cmd_tbl(argv[0], cmd_efidebug_test_sub,
1496 ARRAY_SIZE(cmd_efidebug_test_sub));
1498 return CMD_RET_USAGE;
1500 return cp->cmd(cmdtp, flag, argc, argv);
1504 * do_efi_query_info() - QueryVariableInfo EFI service
1506 * @cmdtp: Command table
1507 * @flag: Command flag
1508 * @argc: Number of arguments
1509 * @argv: Argument array
1510 * Return: CMD_RET_SUCCESS on success,
1511 * CMD_RET_USAGE or CMD_RET_FAILURE on failure
1513 * Implement efidebug "test" sub-command.
1516 static int do_efi_query_info(struct cmd_tbl *cmdtp, int flag,
1517 int argc, char * const argv[])
1521 u64 max_variable_storage_size;
1522 u64 remain_variable_storage_size;
1523 u64 max_variable_size;
1526 for (i = 1; i < argc; i++) {
1527 if (!strcmp(argv[i], "-bs"))
1528 attr |= EFI_VARIABLE_BOOTSERVICE_ACCESS;
1529 else if (!strcmp(argv[i], "-rt"))
1530 attr |= EFI_VARIABLE_RUNTIME_ACCESS;
1531 else if (!strcmp(argv[i], "-nv"))
1532 attr |= EFI_VARIABLE_NON_VOLATILE;
1533 else if (!strcmp(argv[i], "-at"))
1535 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
1538 ret = efi_query_variable_info_int(attr, &max_variable_storage_size,
1539 &remain_variable_storage_size,
1540 &max_variable_size);
1541 if (ret != EFI_SUCCESS) {
1542 printf("Error: Cannot query UEFI variables, r = %lu\n",
1543 ret & ~EFI_ERROR_MASK);
1544 return CMD_RET_FAILURE;
1547 printf("Max storage size %llu\n", max_variable_storage_size);
1548 printf("Remaining storage size %llu\n", remain_variable_storage_size);
1549 printf("Max variable size %llu\n", max_variable_size);
1551 return CMD_RET_SUCCESS;
1554 static struct cmd_tbl cmd_efidebug_sub[] = {
1555 U_BOOT_CMD_MKENT(boot, CONFIG_SYS_MAXARGS, 1, do_efi_boot_opt, "", ""),
1556 #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
1557 U_BOOT_CMD_MKENT(capsule, CONFIG_SYS_MAXARGS, 1, do_efi_capsule,
1560 U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
1562 U_BOOT_CMD_MKENT(dh, CONFIG_SYS_MAXARGS, 1, do_efi_show_handles,
1564 U_BOOT_CMD_MKENT(images, CONFIG_SYS_MAXARGS, 1, do_efi_show_images,
1566 U_BOOT_CMD_MKENT(memmap, CONFIG_SYS_MAXARGS, 1, do_efi_show_memmap,
1568 U_BOOT_CMD_MKENT(tables, CONFIG_SYS_MAXARGS, 1, do_efi_show_tables,
1570 U_BOOT_CMD_MKENT(test, CONFIG_SYS_MAXARGS, 1, do_efi_test,
1572 U_BOOT_CMD_MKENT(query, CONFIG_SYS_MAXARGS, 1, do_efi_query_info,
1577 * do_efidebug() - display and configure UEFI environment
1579 * @cmdtp: Command table
1580 * @flag: Command flag
1581 * @argc: Number of arguments
1582 * @argv: Argument array
1583 * Return: CMD_RET_SUCCESS on success,
1584 * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
1586 * Implement efidebug command which allows us to display and
1587 * configure UEFI environment.
1589 static int do_efidebug(struct cmd_tbl *cmdtp, int flag,
1590 int argc, char *const argv[])
1596 return CMD_RET_USAGE;
1600 /* Initialize UEFI drivers */
1601 r = efi_init_obj_list();
1602 if (r != EFI_SUCCESS) {
1603 printf("Error: Cannot initialize UEFI sub-system, r = %lu\n",
1604 r & ~EFI_ERROR_MASK);
1605 return CMD_RET_FAILURE;
1608 cp = find_cmd_tbl(argv[0], cmd_efidebug_sub,
1609 ARRAY_SIZE(cmd_efidebug_sub));
1611 return CMD_RET_USAGE;
1613 return cp->cmd(cmdtp, flag, argc, argv);
1616 U_BOOT_LONGHELP(efidebug,
1617 " - UEFI Shell-like interface to configure UEFI environment\n"
1619 "efidebug boot add - set UEFI BootXXXX variable\n"
1620 " -b|-B <bootid> <label> <interface> <devnum>[:<part>] <file path>\n"
1621 " -d|-D <interface> <devnum>[:<part>] <device-tree file path>\n"
1622 " -i|-I <interface> <devnum>[:<part>] <initrd file path>\n"
1623 " (-b, -d, -i for short form device path)\n"
1624 #if (IS_ENABLED(CONFIG_EFI_HTTP_BOOT))
1625 " -u <bootid> <label> <uri>\n"
1627 " -s '<optional data>'\n"
1628 "efidebug boot rm <bootid#1> [<bootid#2> [<bootid#3> [...]]]\n"
1629 " - delete UEFI BootXXXX variables\n"
1630 "efidebug boot dump\n"
1631 " - dump all UEFI BootXXXX variables\n"
1632 "efidebug boot next <bootid>\n"
1633 " - set UEFI BootNext variable\n"
1634 "efidebug boot order [<bootid#1> [<bootid#2> [<bootid#3> [...]]]]\n"
1635 " - set/show UEFI boot order\n"
1637 #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT
1638 "efidebug capsule update [-v] <capsule address>\n"
1639 " - process a capsule\n"
1640 "efidebug capsule disk-update\n"
1641 " - update a capsule from disk\n"
1642 "efidebug capsule show <capsule address>\n"
1643 " - show capsule information\n"
1644 "efidebug capsule result [<capsule result var>]\n"
1645 " - show a capsule update result\n"
1646 #ifdef CONFIG_EFI_ESRT
1647 "efidebug capsule esrt\n"
1648 " - print the ESRT\n"
1652 "efidebug drivers\n"
1653 " - show UEFI drivers\n"
1655 " - show UEFI handles\n"
1657 " - show loaded images\n"
1659 " - show UEFI memory map\n"
1661 " - show UEFI configuration tables\n"
1662 #ifdef CONFIG_EFI_BOOTMGR
1663 "efidebug test bootmgr\n"
1664 " - run simple bootmgr for test\n"
1666 "efidebug query [-nv][-bs][-rt][-at]\n"
1667 " - show size of UEFI variables store\n");
1670 efidebug, CONFIG_SYS_MAXARGS, 0, do_efidebug,
1671 "Configure UEFI environment",