diff mbox series

[v5,3/7] cmd: efidebug: add drivers command

Message ID 20190121074923.29959-4-takahiro.akashi@linaro.org
State New
Headers show
Series cmd: add efidebug for efi environment | expand

Commit Message

AKASHI Takahiro Jan. 21, 2019, 7:49 a.m. UTC
"drivers" command prints all the uefi drivers on the system.

=> efi drivers
D
r
v                Driver Name          Image Path
diff mbox series

Patch

================ ==================== ==========
        7ef31d30 <NULL>               <built-in>

Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>
---
 cmd/efidebug.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 81 insertions(+), 1 deletion(-)

diff --git a/cmd/efidebug.c b/cmd/efidebug.c
index 0c7b51364753..739b7880459d 100644
--- a/cmd/efidebug.c
+++ b/cmd/efidebug.c
@@ -8,6 +8,7 @@ 
 #include <charset.h>
 #include <common.h>
 #include <command.h>
+#include <efi_driver.h>
 #include <efi_loader.h>
 #include <environment.h>
 #include <errno.h>
@@ -16,6 +17,7 @@ 
 #include <malloc.h>
 #include <search.h>
 #include <linux/ctype.h>
+#include <linux/kernel.h>
 #include <asm/global_data.h>
 
 #define BS systab.boottime
@@ -394,6 +396,80 @@  static int do_efi_show_devices(cmd_tbl_t *cmdtp, int flag,
 	return CMD_RET_SUCCESS;
 }
 
+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;
+}
+
+static int do_efi_show_drivers(cmd_tbl_t *cmdtp, int flag,
+			       int argc, char * const argv[])
+{
+	efi_handle_t *handles = NULL, *handle;
+	efi_uintn_t size = 0;
+	u16 *driver_name, *image_path_text;
+	efi_status_t ret;
+	int i;
+
+	ret = BS->locate_handle(BY_PROTOCOL, &efi_guid_driver_binding_protocol,
+				NULL, &size, NULL);
+	if (ret == EFI_BUFFER_TOO_SMALL) {
+		handles = calloc(1, size);
+		if (!handles)
+			return CMD_RET_FAILURE;
+
+		ret = BS->locate_handle(BY_PROTOCOL,
+					&efi_guid_driver_binding_protocol,
+					NULL, &size, handles);
+	}
+	if (ret != EFI_SUCCESS) {
+		free(handles);
+		return CMD_RET_FAILURE;
+	}
+
+	printf("D\n");
+	printf("r\n");
+	printf("v                Driver Name          Image Path\n");
+	printf("================ ==================== ==========\n");
+	handle = handles;
+	for (i = 0; i < size / sizeof(*handle); i++) {
+		if (!efi_get_driver_handle_info(*handle, &driver_name,
+						&image_path_text)) {
+			if (image_path_text)
+				printf("%16p %-20ls %ls\n",
+				       *handle, driver_name, image_path_text);
+			else
+				printf("%16p %-20ls <built-in>\n",
+				       *handle, driver_name);
+			efi_free_pool(driver_name);
+			efi_free_pool(image_path_text);
+		}
+		handle++;
+	}
+
+	return CMD_RET_SUCCESS;
+}
+
 static int do_efi_boot_add(cmd_tbl_t *cmdtp, int flag,
 			   int argc, char * const argv[])
 {
@@ -789,6 +865,8 @@  static cmd_tbl_t cmd_efidebug_sub[] = {
 	U_BOOT_CMD_MKENT(setvar, CONFIG_SYS_MAXARGS, 1, do_efi_set_var, "", ""),
 	U_BOOT_CMD_MKENT(devices, CONFIG_SYS_MAXARGS, 1, do_efi_show_devices,
 			 "", ""),
+	U_BOOT_CMD_MKENT(drivers, CONFIG_SYS_MAXARGS, 1, do_efi_show_drivers,
+			 "", ""),
 };
 
 /* Interpreter command to configure UEFI environment */
@@ -841,7 +919,9 @@  static char efidebug_help_text[] =
 	"  - set/delete uefi variable's value\n"
 	"    <value> may be \"=\"...\"\", \"=0x...\", \"=H...\", (set) or \"=\" (delete)\n"
 	"efidebug devices\n"
-	"  - show uefi devices\n";
+	"  - show uefi devices\n"
+	"efidebug drivers\n"
+	"  - show uefi drivers\n";
 #endif
 
 U_BOOT_CMD(