diff mbox series

[v2,06/14] cmd: efishell: add drivers command

Message ID 20181105090653.7409-7-takahiro.akashi@linaro.org
State Superseded
Headers show
Series efi: make efi and bootmgr more usable | expand

Commit Message

AKASHI Takahiro Nov. 5, 2018, 9:06 a.m. UTC
"drivers" command prints all the uefi drivers on the system.
=> efi drivers
Driver Name     Image Path
diff mbox series

Patch

============================================
(unknown)       <NULL>+(not found)
    guid: 18a031ab-b443-4d1a-a5c0-0c09261e9f71

Currently, no useful information can be printed.

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

diff --git a/cmd/efishell.c b/cmd/efishell.c
index f4fa3fdf28a7..2f54f7803ac5 100644
--- a/cmd/efishell.c
+++ b/cmd/efishell.c
@@ -346,6 +346,95 @@  static int do_efi_show_devices(int argc, char * const argv[])
 	return CMD_RET_SUCCESS;
 }
 
+static int efi_get_driver_handle_info(efi_handle_t handle, u16 **name,
+				      u16 **devname, u16 **filename)
+{
+	struct efi_driver_binding_protocol *binding;
+	struct efi_loaded_image *image;
+	efi_status_t ret;
+
+	ret = bs->open_protocol(handle, &efi_guid_driver_binding_protocol,
+				(void **)&binding, NULL, NULL,
+				EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+	if (ret != EFI_SUCCESS)
+		return -1;
+
+	ret = bs->open_protocol(binding->image_handle, &efi_guid_loaded_image,
+				(void **)&image, NULL /* FIXME */, NULL,
+				EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+	if (ret != EFI_SUCCESS)
+		goto e_out;
+
+	/*
+	 * TODO:
+	 * handle image->device_handle,
+	 * use append_device_path()
+	 */
+	*devname = NULL;
+	*filename = efi_dp_str(image->file_path);
+
+	return 0;
+
+e_out:
+	*devname = NULL;
+	*filename = L"(not found)";
+
+	return 0;
+}
+
+static int do_efi_show_drivers(int argc, char * const argv[])
+{
+	efi_handle_t *handles = NULL, *handle;
+	efi_uintn_t size = 0;
+	u16 *drvname, *devname, *filename;
+	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("Driver Name     Image Path\n");
+	printf("============================================\n");
+	handle = handles;
+	for (i = 0; i < size / sizeof(*handle); i++) {
+		if (!efi_get_driver_handle_info(*handle, &drvname, &devname,
+						&filename)) {
+			printf("%-16s%ls+%ls\n",
+			       "(unknown)", devname, filename);
+			efi_free_pool(devname);
+			efi_free_pool(filename);
+
+			/* TODO: no other info */
+			struct efi_object *efiobj;
+			struct list_head *lhandle;
+			struct efi_handler *protocol;
+
+			efiobj = efi_search_obj(*handle);
+			list_for_each(lhandle, &efiobj->protocols) {
+				protocol = list_entry(lhandle,
+						      struct efi_handler, link);
+				printf("    guid: %pUl\n", protocol->guid);
+			}
+		}
+		handle++;
+	}
+
+	return CMD_RET_SUCCESS;
+}
+
 static int do_efi_boot_add(int argc, char * const argv[])
 {
 	int id;
@@ -726,6 +815,8 @@  static int do_efishell(cmd_tbl_t *cmdtp, int flag,
 		return do_efi_set_var(argc, argv);
 	else if (!strcmp(command, "devices"))
 		return do_efi_show_devices(argc, argv);
+	else if (!strcmp(command, "drivers"))
+		return do_efi_show_drivers(argc, argv);
 	else
 		return CMD_RET_USAGE;
 }
@@ -749,7 +840,9 @@  static char efishell_help_text[] =
 	"  - set/delete uefi variable's value\n"
 	"    <value> may be \"=\"...\"\", \"=0x...\" (set) or \"=\" (delete)\n"
 	"efishell devices\n"
-	"  - show uefi devices\n";
+	"  - show uefi devices\n"
+	"efishell drivers\n"
+	"  - show uefi drivers\n";
 #endif
 
 U_BOOT_CMD(