// SPDX-License-Identifier: GPL-2.0+ /* * UEFI Shell-like command * * Copyright (c) 2018 AKASHI Takahiro, Linaro Limited */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define BS systab.boottime #define RT systab.runtime #ifdef CONFIG_EFI_HAVE_CAPSULE_SUPPORT /** * do_efi_capsule_update() - process a capsule update * * @cmdtp: Command table * @flag: Command flag * @argc: Number of arguments * @argv: Argument array * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure * * Implement efidebug "capsule update" sub-command. * process a capsule update. * * efidebug capsule update [-v] */ static int do_efi_capsule_update(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]) { struct efi_capsule_header *capsule; int verbose = 0; char *endp; efi_status_t ret; if (argc != 2 && argc != 3) return CMD_RET_USAGE; if (argc == 3) { if (strcmp(argv[1], "-v")) return CMD_RET_USAGE; verbose = 1; argc--; argv++; } capsule = (typeof(capsule))simple_strtoul(argv[1], &endp, 16); if (endp == argv[1]) { printf("Invalid address: %s", argv[1]); return CMD_RET_FAILURE; } if (verbose) { printf("Capsule guid: %pUl\n", &capsule->capsule_guid); printf("Capsule flags: 0x%x\n", capsule->flags); printf("Capsule header size: 0x%x\n", capsule->header_size); printf("Capsule image size: 0x%x\n", capsule->capsule_image_size); } ret = EFI_CALL(RT->update_capsule(&capsule, 1, 0)); if (ret) { printf("Cannot handle a capsule at %p", capsule); return CMD_RET_FAILURE; } return CMD_RET_SUCCESS; } static int do_efi_capsule_on_disk_update(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]) { efi_status_t ret; ret = efi_launch_capsules(); return ret == EFI_SUCCESS ? CMD_RET_SUCCESS : CMD_RET_FAILURE; } /** * do_efi_capsule_show() - show capsule information * * @cmdtp: Command table * @flag: Command flag * @argc: Number of arguments * @argv: Argument array * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure * * Implement efidebug "capsule show" sub-command. * show capsule information. * * efidebug capsule show */ static int do_efi_capsule_show(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]) { struct efi_capsule_header *capsule; char *endp; if (argc != 2) return CMD_RET_USAGE; capsule = (typeof(capsule))simple_strtoul(argv[1], &endp, 16); if (endp == argv[1]) { printf("Invalid address: %s", argv[1]); return CMD_RET_FAILURE; } printf("Capsule guid: %pUl\n", &capsule->capsule_guid); printf("Capsule flags: 0x%x\n", capsule->flags); printf("Capsule header size: 0x%x\n", capsule->header_size); printf("Capsule image size: 0x%x\n", capsule->capsule_image_size); return CMD_RET_SUCCESS; } /** * do_efi_capsule_res() - show a capsule update result * * @cmdtp: Command table * @flag: Command flag * @argc: Number of arguments * @argv: Argument array * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure * * Implement efidebug "capsule result" sub-command. * show a capsule update result. * If result number is not specified, CapsuleLast will be shown. * * efidebug capsule result [] */ static int do_efi_capsule_res(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]) { int capsule_id; char *endp; char var_name[12]; u16 var_name16[12], *p; efi_guid_t guid; struct efi_capsule_result_variable_header *result = NULL; efi_uintn_t size; efi_status_t ret; if (argc != 1 && argc != 2) return CMD_RET_USAGE; guid = efi_guid_capsule_report; if (argc == 1) { size = sizeof(var_name16); ret = EFI_CALL(RT->get_variable(L"CapsuleLast", &guid, NULL, &size, var_name16)); if (ret != EFI_SUCCESS) { if (ret == EFI_NOT_FOUND) printf("CapsuleLast doesn't exist\n"); else printf("Failed to get CapsuleLast\n"); return CMD_RET_FAILURE; } printf("CapsuleLast is %ls\n", var_name16); } else { argc--; argv++; capsule_id = simple_strtoul(argv[0], &endp, 16); if (capsule_id < 0 || capsule_id > 0xffff) return CMD_RET_USAGE; sprintf(var_name, "Capsule%04X", capsule_id); p = var_name16; utf8_utf16_strncpy(&p, var_name, 9); } size = 0; ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, NULL)); if (ret == EFI_BUFFER_TOO_SMALL) { result = malloc(size); if (!result) return CMD_RET_FAILURE; ret = EFI_CALL(RT->get_variable(var_name16, &guid, NULL, &size, result)); } if (ret != EFI_SUCCESS) { free(result); printf("Failed to get %ls\n", var_name16); return CMD_RET_FAILURE; } printf("Result total size: 0x%x\n", result->variable_total_size); printf("Capsule guid: %pUl\n", &result->capsule_guid); printf("Time processed: %04d-%02d-%02d %02d:%02d:%02d\n", result->capsule_processed.year, result->capsule_processed.month, result->capsule_processed.day, result->capsule_processed.hour, result->capsule_processed.minute, result->capsule_processed.second); printf("Capsule status: 0x%lx\n", result->capsule_status); free(result); return CMD_RET_SUCCESS; } static struct cmd_tbl cmd_efidebug_capsule_sub[] = { U_BOOT_CMD_MKENT(update, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_update, "", ""), U_BOOT_CMD_MKENT(show, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_show, "", ""), U_BOOT_CMD_MKENT(disk-update, 0, 0, do_efi_capsule_on_disk_update, "", ""), U_BOOT_CMD_MKENT(result, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_res, "", ""), }; /** * do_efi_capsule() - manage UEFI capsules * * @cmdtp: Command table * @flag: Command flag * @argc: Number of arguments * @argv: Argument array * Return: CMD_RET_SUCCESS on success, * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure * * Implement efidebug "capsule" sub-command. */ static int do_efi_capsule(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]) { struct cmd_tbl *cp; if (argc < 2) return CMD_RET_USAGE; argc--; argv++; cp = find_cmd_tbl(argv[0], cmd_efidebug_capsule_sub, ARRAY_SIZE(cmd_efidebug_capsule_sub)); if (!cp) return CMD_RET_USAGE; return cp->cmd(cmdtp, flag, argc, argv); } #endif /* CONFIG_EFI_HAVE_CAPSULE_SUPPORT */ /** * efi_get_device_handle_info() - get information of UEFI device * * @handle: Handle of UEFI device * @dev_path_text: Pointer to text of device path * Return: 0 on success, -1 on failure * * Currently return a formatted text of device path. */ static int efi_get_device_handle_info(efi_handle_t handle, u16 **dev_path_text) { struct efi_device_path *dp; efi_status_t ret; ret = EFI_CALL(BS->open_protocol(handle, &efi_guid_device_path, (void **)&dp, NULL /* FIXME */, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL)); if (ret == EFI_SUCCESS) { *dev_path_text = efi_dp_str(dp); return 0; } else { return -1; } } #define EFI_HANDLE_WIDTH ((int)sizeof(efi_handle_t) * 2) static const char spc[] = " "; static const char sep[] = "================"; /** * do_efi_show_devices() - show UEFI devices * * @cmdtp: Command table * @flag: Command flag * @argc: Number of arguments * @argv: Argument array * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure * * Implement efidebug "devices" sub-command. * Show all UEFI devices and their information. */ static int do_efi_show_devices(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { efi_handle_t *handles; efi_uintn_t num, i; u16 *dev_path_text; efi_status_t ret; ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL, &num, &handles)); if (ret != EFI_SUCCESS) return CMD_RET_FAILURE; if (!num) return CMD_RET_SUCCESS; printf("Device%.*s Device Path\n", EFI_HANDLE_WIDTH - 6, spc); printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep); for (i = 0; i < num; i++) { if (!efi_get_device_handle_info(handles[i], &dev_path_text)) { printf("%p %ls\n", handles[i], dev_path_text); efi_free_pool(dev_path_text); } } efi_free_pool(handles); return CMD_RET_SUCCESS; } /** * efi_get_driver_handle_info() - get information of UEFI driver * * @handle: Handle of UEFI device * @driver_name: Driver name * @image_path: Pointer to text of device path * Return: 0 on success, -1 on failure * * Currently return no useful information as all UEFI drivers are * built-in.. */ static int efi_get_driver_handle_info(efi_handle_t handle, u16 **driver_name, u16 **image_path) { struct efi_handler *handler; struct efi_loaded_image *image; efi_status_t ret; /* * driver name * TODO: support EFI_COMPONENT_NAME2_PROTOCOL */ *driver_name = NULL; /* image name */ ret = efi_search_protocol(handle, &efi_guid_loaded_image, &handler); if (ret != EFI_SUCCESS) { *image_path = NULL; return 0; } image = handler->protocol_interface; *image_path = efi_dp_str(image->file_path); return 0; } /** * do_efi_show_drivers() - show UEFI drivers * * @cmdtp: Command table * @flag: Command flag * @argc: Number of arguments * @argv: Argument array * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure * * Implement efidebug "drivers" sub-command. * Show all UEFI drivers and their information. */ static int do_efi_show_drivers(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { efi_handle_t *handles; efi_uintn_t num, i; u16 *driver_name, *image_path_text; efi_status_t ret; ret = EFI_CALL(efi_locate_handle_buffer( BY_PROTOCOL, &efi_guid_driver_binding_protocol, NULL, &num, &handles)); if (ret != EFI_SUCCESS) return CMD_RET_FAILURE; if (!num) return CMD_RET_SUCCESS; printf("Driver%.*s Name Image Path\n", EFI_HANDLE_WIDTH - 6, spc); printf("%.*s ==================== ====================\n", EFI_HANDLE_WIDTH, sep); for (i = 0; i < num; i++) { if (!efi_get_driver_handle_info(handles[i], &driver_name, &image_path_text)) { if (image_path_text) printf("%p %-20ls %ls\n", handles[i], driver_name, image_path_text); else printf("%p %-20ls \n", handles[i], driver_name); efi_free_pool(driver_name); efi_free_pool(image_path_text); } } efi_free_pool(handles); return CMD_RET_SUCCESS; } static const struct { const char *text; const efi_guid_t guid; } guid_list[] = { { "Device Path", EFI_DEVICE_PATH_PROTOCOL_GUID, }, { "Device Path To Text", EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID, }, { "Device Path Utilities", EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID, }, { "Unicode Collation 2", EFI_UNICODE_COLLATION_PROTOCOL2_GUID, }, { "Driver Binding", EFI_DRIVER_BINDING_PROTOCOL_GUID, }, { "Simple Text Input", EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID, }, { "Simple Text Input Ex", EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID, }, { "Simple Text Output", EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID, }, { "Block IO", EFI_BLOCK_IO_PROTOCOL_GUID, }, { "Simple File System", EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID, }, { "Loaded Image", EFI_LOADED_IMAGE_PROTOCOL_GUID, }, { "Graphics Output", EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID, }, { "HII String", EFI_HII_STRING_PROTOCOL_GUID, }, { "HII Database", EFI_HII_DATABASE_PROTOCOL_GUID, }, { "HII Config Routing", EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID, }, { "Load File2", EFI_LOAD_FILE2_PROTOCOL_GUID, }, { "Random Number Generator", EFI_RNG_PROTOCOL_GUID, }, { "Simple Network", EFI_SIMPLE_NETWORK_PROTOCOL_GUID, }, { "PXE Base Code", EFI_PXE_BASE_CODE_PROTOCOL_GUID, }, { "Device-Tree Fixup", EFI_DT_FIXUP_PROTOCOL_GUID, }, { "System Partition", PARTITION_SYSTEM_GUID }, { "Firmware Management", EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID }, /* Configuration table GUIDs */ { "ACPI table", EFI_ACPI_TABLE_GUID, }, { "device tree", EFI_FDT_GUID, }, { "SMBIOS table", SMBIOS_TABLE_GUID, }, { "Runtime properties", EFI_RT_PROPERTIES_TABLE_GUID, }, { "TCG2 Final Events Table", EFI_TCG2_FINAL_EVENTS_TABLE_GUID, }, }; /** * get_guid_text - get string of GUID * * Return description of GUID. * * @guid: GUID * Return: description of GUID or NULL */ static const char *get_guid_text(const void *guid) { int i; for (i = 0; i < ARRAY_SIZE(guid_list); i++) { /* * As guidcmp uses memcmp() we can safely accept unaligned * GUIDs. */ if (!guidcmp(&guid_list[i].guid, guid)) return guid_list[i].text; } return NULL; } /** * do_efi_show_handles() - show UEFI handles * * @cmdtp: Command table * @flag: Command flag * @argc: Number of arguments * @argv: Argument array * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure * * Implement efidebug "dh" sub-command. * Show all UEFI handles and their information, currently all protocols * added to handle. */ static int do_efi_show_handles(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { efi_handle_t *handles; efi_guid_t **guid; efi_uintn_t num, count, i, j; const char *guid_text; efi_status_t ret; ret = EFI_CALL(efi_locate_handle_buffer(ALL_HANDLES, NULL, NULL, &num, &handles)); if (ret != EFI_SUCCESS) return CMD_RET_FAILURE; if (!num) return CMD_RET_SUCCESS; printf("Handle%.*s Protocols\n", EFI_HANDLE_WIDTH - 6, spc); printf("%.*s ====================\n", EFI_HANDLE_WIDTH, sep); for (i = 0; i < num; i++) { printf("%p", handles[i]); ret = EFI_CALL(BS->protocols_per_handle(handles[i], &guid, &count)); if (ret || !count) { putc('\n'); continue; } for (j = 0; j < count; j++) { if (j) printf(", "); else putc(' '); guid_text = get_guid_text(guid[j]); if (guid_text) puts(guid_text); else printf("%pUl", guid[j]); } putc('\n'); } efi_free_pool(handles); return CMD_RET_SUCCESS; } /** * do_efi_show_images() - show UEFI images * * @cmdtp: Command table * @flag: Command flag * @argc: Number of arguments * @argv: Argument array * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure * * Implement efidebug "images" sub-command. * Show all UEFI loaded images and their information. */ static int do_efi_show_images(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { efi_print_image_infos(NULL); return CMD_RET_SUCCESS; } static const char * const efi_mem_type_string[] = { [EFI_RESERVED_MEMORY_TYPE] = "RESERVED", [EFI_LOADER_CODE] = "LOADER CODE", [EFI_LOADER_DATA] = "LOADER DATA", [EFI_BOOT_SERVICES_CODE] = "BOOT CODE", [EFI_BOOT_SERVICES_DATA] = "BOOT DATA", [EFI_RUNTIME_SERVICES_CODE] = "RUNTIME CODE", [EFI_RUNTIME_SERVICES_DATA] = "RUNTIME DATA", [EFI_CONVENTIONAL_MEMORY] = "CONVENTIONAL", [EFI_UNUSABLE_MEMORY] = "UNUSABLE MEM", [EFI_ACPI_RECLAIM_MEMORY] = "ACPI RECLAIM MEM", [EFI_ACPI_MEMORY_NVS] = "ACPI NVS", [EFI_MMAP_IO] = "IO", [EFI_MMAP_IO_PORT] = "IO PORT", [EFI_PAL_CODE] = "PAL", [EFI_PERSISTENT_MEMORY_TYPE] = "PERSISTENT", }; static const struct efi_mem_attrs { const u64 bit; const char *text; } efi_mem_attrs[] = { {EFI_MEMORY_UC, "UC"}, {EFI_MEMORY_UC, "UC"}, {EFI_MEMORY_WC, "WC"}, {EFI_MEMORY_WT, "WT"}, {EFI_MEMORY_WB, "WB"}, {EFI_MEMORY_UCE, "UCE"}, {EFI_MEMORY_WP, "WP"}, {EFI_MEMORY_RP, "RP"}, {EFI_MEMORY_XP, "WP"}, {EFI_MEMORY_NV, "NV"}, {EFI_MEMORY_MORE_RELIABLE, "REL"}, {EFI_MEMORY_RO, "RO"}, {EFI_MEMORY_SP, "SP"}, {EFI_MEMORY_RUNTIME, "RT"}, }; /** * print_memory_attributes() - print memory map attributes * * @attributes: Attribute value * * Print memory map attributes */ static void print_memory_attributes(u64 attributes) { int sep, i; for (sep = 0, i = 0; i < ARRAY_SIZE(efi_mem_attrs); i++) if (attributes & efi_mem_attrs[i].bit) { if (sep) { putc('|'); } else { putc(' '); sep = 1; } puts(efi_mem_attrs[i].text); } } #define EFI_PHYS_ADDR_WIDTH (int)(sizeof(efi_physical_addr_t) * 2) /** * do_efi_show_memmap() - show UEFI memory map * * @cmdtp: Command table * @flag: Command flag * @argc: Number of arguments * @argv: Argument array * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure * * Implement efidebug "memmap" sub-command. * Show UEFI memory map. */ static int do_efi_show_memmap(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { struct efi_mem_desc *memmap = NULL, *map; efi_uintn_t map_size = 0; const char *type; int i; efi_status_t ret; ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL); if (ret == EFI_BUFFER_TOO_SMALL) { map_size += sizeof(struct efi_mem_desc); /* for my own */ ret = efi_allocate_pool(EFI_LOADER_DATA, map_size, (void *)&memmap); if (ret != EFI_SUCCESS) return CMD_RET_FAILURE; ret = efi_get_memory_map(&map_size, memmap, NULL, NULL, NULL); } if (ret != EFI_SUCCESS) { efi_free_pool(memmap); return CMD_RET_FAILURE; } printf("Type Start%.*s End%.*s Attributes\n", EFI_PHYS_ADDR_WIDTH - 5, spc, EFI_PHYS_ADDR_WIDTH - 3, spc); printf("================ %.*s %.*s ==========\n", EFI_PHYS_ADDR_WIDTH, sep, EFI_PHYS_ADDR_WIDTH, sep); /* * Coverity check: dereferencing null pointer "map." * This is a false positive as memmap will always be * populated by allocate_pool() above. */ for (i = 0, map = memmap; i < map_size / sizeof(*map); map++, i++) { if (map->type < ARRAY_SIZE(efi_mem_type_string)) type = efi_mem_type_string[map->type]; else type = "(unknown)"; printf("%-16s %.*llx-%.*llx", type, EFI_PHYS_ADDR_WIDTH, (u64)map_to_sysmem((void *)(uintptr_t) map->physical_start), EFI_PHYS_ADDR_WIDTH, (u64)map_to_sysmem((void *)(uintptr_t) (map->physical_start + map->num_pages * EFI_PAGE_SIZE))); print_memory_attributes(map->attribute); putc('\n'); } efi_free_pool(memmap); return CMD_RET_SUCCESS; } /** * do_efi_show_tables() - show UEFI configuration tables * * @cmdtp: Command table * @flag: Command flag * @argc: Number of arguments * @argv: Argument array * Return: CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure * * Implement efidebug "tables" sub-command. * Show UEFI configuration tables. */ static int do_efi_show_tables(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { efi_uintn_t i; const char *guid_str; for (i = 0; i < systab.nr_tables; ++i) { guid_str = get_guid_text(&systab.tables[i].guid); if (!guid_str) guid_str = ""; printf("%pUl %s\n", &systab.tables[i].guid, guid_str); } return CMD_RET_SUCCESS; } /** * create_initrd_dp() - Create a special device for our Boot### option * * @dev: Device * @part: Disk partition * @file: Filename * Return: Pointer to the device path or ERR_PTR * */ static struct efi_device_path *create_initrd_dp(const char *dev, const char *part, const char *file) { struct efi_device_path *tmp_dp = NULL, *tmp_fp = NULL; struct efi_device_path *initrd_dp = NULL; efi_status_t ret; const struct efi_initrd_dp id_dp = { .vendor = { { DEVICE_PATH_TYPE_MEDIA_DEVICE, DEVICE_PATH_SUB_TYPE_VENDOR_PATH, sizeof(id_dp.vendor), }, EFI_INITRD_MEDIA_GUID, }, .end = { DEVICE_PATH_TYPE_END, DEVICE_PATH_SUB_TYPE_END, sizeof(id_dp.end), } }; ret = efi_dp_from_name(dev, part, file, &tmp_dp, &tmp_fp); if (ret != EFI_SUCCESS) { printf("Cannot create device path for \"%s %s\"\n", part, file); goto out; } initrd_dp = efi_dp_append((const struct efi_device_path *)&id_dp, tmp_fp); out: efi_free_pool(tmp_dp); efi_free_pool(tmp_fp); return initrd_dp; } /** * do_efi_boot_add() - set UEFI load option * * @cmdtp: Command table * @flag: Command flag * @argc: Number of arguments * @argv: Argument array * Return: CMD_RET_SUCCESS on success, * CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure * * Implement efidebug "boot add" sub-command. Create or change UEFI load option. * * efidebug boot add -b