// 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 #define BS systab.boottime #define RT systab.runtime /** * 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(cmd_tbl_t *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(BS->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_CALL(BS->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(cmd_tbl_t *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(BS->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_CALL(BS->free_pool(driver_name)); EFI_CALL(BS->free_pool(image_path_text)); } } EFI_CALL(BS->free_pool(handles)); return CMD_RET_SUCCESS; } static const struct { const char *text; const efi_guid_t guid; } guid_list[] = { { "Device Path", DEVICE_PATH_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", BLOCK_IO_GUID, }, { "Simple File System", EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID, }, { "Loaded Image", LOADED_IMAGE_PROTOCOL_GUID, }, { "GOP", EFI_GOP_GUID, }, }; /** * get_guid_text - get string of protocol guid * @guid: Protocol guid * Return: String * * Return string for display to represent the protocol. */ static const char *get_guid_text(const efi_guid_t *guid) { int i; for (i = 0; i < ARRAY_SIZE(guid_list); i++) if (!guidcmp(&guid_list[i].guid, guid)) break; if (i != ARRAY_SIZE(guid_list)) return guid_list[i].text; else 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(cmd_tbl_t *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(BS->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_CALL(BS->free_pool(handles)); return CMD_RET_SUCCESS; } /** * 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. * - boot add