diff mbox series

[v4,2/9] cmd: efitool: add devices command

Message ID 20190115025522.12060-3-takahiro.akashi@linaro.org
State New
Headers show
Series cmd: add efitool for efi environment | expand

Commit Message

AKASHI Takahiro Jan. 15, 2019, 2:55 a.m. UTC
"devices" command prints all the uefi variables on the system.

=> efi devices
Scanning disk ahci_scsi.id0lun0...
Scanning disk ahci_scsi.id1lun0...
Found 4 disks
D
e
v                Device Path

Comments

Heinrich Schuchardt Jan. 15, 2019, 5:09 a.m. UTC | #1
On 1/15/19 3:55 AM, AKASHI Takahiro wrote:
> "devices" command prints all the uefi variables on the system.
> 
> => efi devices
> Scanning disk ahci_scsi.id0lun0...
> Scanning disk ahci_scsi.id1lun0...
> Found 4 disks
> D
> e
> v                Device Path
> ================ ====================
>         7ef3bfa0 /VenHw(e61d73b9-a384-4acc-aeab-82e828f3628b)
>         7ef3cca0 /VenHw(e61d73b9-a384-4acc-aeab-82e828f3628b)/Scsi(0,0)
>         7ef3d070 /VenHw(e61d73b9-a384-4acc-aeab-82e828f3628b)/Scsi(1,0)
>         7ef3d1b0 /VenHw(e61d73b9-a384-4acc-aeab-82e828f3628b)/Scsi(1,0)/HD(1,MBR,0x086246ba,0x800,0x40000)
>         7ef3d3e0 /VenHw(e61d73b9-a384-4acc-aeab-82e828f3628b)/Scsi(1,0)/HD(2,MBR,0x086246ba,0x40800,0x3f800)
> 
> Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>
> ---
>  cmd/efitool.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++-
>  1 file changed, 97 insertions(+), 1 deletion(-)
> 
> diff --git a/cmd/efitool.c b/cmd/efitool.c
> index c7dc2c11f2e7..6b2df1beedb5 100644
> --- a/cmd/efitool.c
> +++ b/cmd/efitool.c
> @@ -18,6 +18,8 @@
>  #include <linux/ctype.h>
>  #include <asm/global_data.h>
>  
> +#define BS systab.boottime
> +
>  static void dump_var_data(char *data, unsigned long len)
>  {
>  	char *start, *end, *p;
> @@ -266,6 +268,96 @@ out:
>  	return ret;
>  }
>  
> +static int efi_get_handles_by_proto(efi_guid_t *guid, efi_handle_t **handlesp,
> +				    int *num)
> +{
> +	efi_handle_t *handles = NULL;
> +	efi_uintn_t size = 0;
> +	efi_status_t ret;
> +
> +	if (guid) {
> +		ret = BS->locate_handle(BY_PROTOCOL, guid, NULL, &size,
> +					handles);
> +		if (ret == EFI_BUFFER_TOO_SMALL) {
> +			handles = calloc(1, size);
> +			if (!handles)
> +				return -1;
> +
> +			ret = BS->locate_handle(BY_PROTOCOL, guid, NULL, &size,
> +						handles);
> +		}
> +		if (ret != EFI_SUCCESS) {
> +			free(handles);
> +			return -1;
> +		}
> +	} else {
> +		ret = BS->locate_handle(ALL_HANDLES, NULL, NULL, &size, NULL);
> +		if (ret == EFI_BUFFER_TOO_SMALL) {
> +			handles = calloc(1, size);
> +			if (!handles)
> +				return -1;
> +
> +			ret = BS->locate_handle(ALL_HANDLES, NULL, NULL, &size,
> +						handles);
> +		}
> +		if (ret != EFI_SUCCESS) {
> +			free(handles);
> +			return -1;
> +		}
> +	}
> +
> +	*handlesp = handles;
> +	*num = size / sizeof(efi_handle_t);
> +
> +	return 0;
> +}
> +
> +static int efi_get_device_handle_info(efi_handle_t handle, u16 **name)
> +{
> +	struct efi_device_path *dp;
> +	efi_status_t ret;
> +
> +	ret = BS->open_protocol(handle, &efi_guid_device_path,
> +				(void **)&dp, NULL /* FIXME */, NULL,
> +				EFI_OPEN_PROTOCOL_GET_PROTOCOL);
> +	if (ret == EFI_SUCCESS) {
> +		*name = efi_dp_str(dp);
> +		return 0;
> +	} else {
> +		return -1;
> +	}
> +}
> +
> +static int do_efi_show_devices(int argc, char * const argv[])
> +{
> +	efi_handle_t *handles;
> +	u16 *devname;

This is the device path. So why not call it dev_path?

> +	int num, i;
> +
> +	handles = NULL;
> +	num = 0;
> +	if (efi_get_handles_by_proto(NULL, &handles, &num))
> +		return CMD_RET_FAILURE;
> +
> +	if (!num)
> +		return CMD_RET_SUCCESS;
> +
> +	printf("D\n");
> +	printf("e\n");
> +	printf("v                Device Path\n");
> +	printf("================ ====================\n");
> +	for (i = 0; i < num; i++) {
> +		if (!efi_get_device_handle_info(handles[i], &devname)) {
> +			printf("%16p %ls\n", handles[i], devname);
> +			efi_free_pool(devname);
> +		}
> +	}
> +
> +	free(handles);
> +
> +	return CMD_RET_SUCCESS;
> +}
> +
>  static int do_efi_boot_add(int argc, char * const argv[])
>  {

Please, use the standard way of adding sub-commands, cf.
doc/README.commands.

Thanks a lot for your contribution.

Regards

Heinrich

>  	int id;
> @@ -673,6 +765,8 @@ static int do_efitool(cmd_tbl_t *cmdtp, int flag,
>  		return do_efi_dump_var(argc, argv);
>  	else if (!strcmp(command, "setvar"))
>  		return do_efi_set_var(argc, argv);
> +	else if (!strcmp(command, "devices"))
> +		return do_efi_show_devices(argc, argv);
>  	else
>  		return CMD_RET_USAGE;
>  }
> @@ -697,7 +791,9 @@ static char efitool_help_text[] =
>  	"  - show UEFI variable's value\n"
>  	"efitool setvar <name> [<value>]\n"
>  	"  - set/delete uefi variable's value\n"
> -	"    <value> may be \"=\"...\"\", \"=0x...\", \"=H...\", (set) or \"=\" (delete)\n";
> +	"    <value> may be \"=\"...\"\", \"=0x...\", \"=H...\", (set) or \"=\" (delete)\n"
> +	"efitool devices\n"
> +	"  - show uefi devices\n";
>  #endif
>  
>  U_BOOT_CMD(
>
AKASHI Takahiro Jan. 17, 2019, 4:48 a.m. UTC | #2
On Tue, Jan 15, 2019 at 06:09:31AM +0100, Heinrich Schuchardt wrote:
> On 1/15/19 3:55 AM, AKASHI Takahiro wrote:
> > "devices" command prints all the uefi variables on the system.
> > 
> > => efi devices
> > Scanning disk ahci_scsi.id0lun0...
> > Scanning disk ahci_scsi.id1lun0...
> > Found 4 disks
> > D
> > e
> > v                Device Path
> > ================ ====================
> >         7ef3bfa0 /VenHw(e61d73b9-a384-4acc-aeab-82e828f3628b)
> >         7ef3cca0 /VenHw(e61d73b9-a384-4acc-aeab-82e828f3628b)/Scsi(0,0)
> >         7ef3d070 /VenHw(e61d73b9-a384-4acc-aeab-82e828f3628b)/Scsi(1,0)
> >         7ef3d1b0 /VenHw(e61d73b9-a384-4acc-aeab-82e828f3628b)/Scsi(1,0)/HD(1,MBR,0x086246ba,0x800,0x40000)
> >         7ef3d3e0 /VenHw(e61d73b9-a384-4acc-aeab-82e828f3628b)/Scsi(1,0)/HD(2,MBR,0x086246ba,0x40800,0x3f800)
> > 
> > Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>
> > ---
> >  cmd/efitool.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++-
> >  1 file changed, 97 insertions(+), 1 deletion(-)
> > 
> > diff --git a/cmd/efitool.c b/cmd/efitool.c
> > index c7dc2c11f2e7..6b2df1beedb5 100644
> > --- a/cmd/efitool.c
> > +++ b/cmd/efitool.c
> > @@ -18,6 +18,8 @@
> >  #include <linux/ctype.h>
> >  #include <asm/global_data.h>
> >  
> > +#define BS systab.boottime
> > +
> >  static void dump_var_data(char *data, unsigned long len)
> >  {
> >  	char *start, *end, *p;
> > @@ -266,6 +268,96 @@ out:
> >  	return ret;
> >  }
> >  
> > +static int efi_get_handles_by_proto(efi_guid_t *guid, efi_handle_t **handlesp,
> > +				    int *num)
> > +{
> > +	efi_handle_t *handles = NULL;
> > +	efi_uintn_t size = 0;
> > +	efi_status_t ret;
> > +
> > +	if (guid) {
> > +		ret = BS->locate_handle(BY_PROTOCOL, guid, NULL, &size,
> > +					handles);
> > +		if (ret == EFI_BUFFER_TOO_SMALL) {
> > +			handles = calloc(1, size);
> > +			if (!handles)
> > +				return -1;
> > +
> > +			ret = BS->locate_handle(BY_PROTOCOL, guid, NULL, &size,
> > +						handles);
> > +		}
> > +		if (ret != EFI_SUCCESS) {
> > +			free(handles);
> > +			return -1;
> > +		}
> > +	} else {
> > +		ret = BS->locate_handle(ALL_HANDLES, NULL, NULL, &size, NULL);
> > +		if (ret == EFI_BUFFER_TOO_SMALL) {
> > +			handles = calloc(1, size);
> > +			if (!handles)
> > +				return -1;
> > +
> > +			ret = BS->locate_handle(ALL_HANDLES, NULL, NULL, &size,
> > +						handles);
> > +		}
> > +		if (ret != EFI_SUCCESS) {
> > +			free(handles);
> > +			return -1;
> > +		}
> > +	}
> > +
> > +	*handlesp = handles;
> > +	*num = size / sizeof(efi_handle_t);
> > +
> > +	return 0;
> > +}
> > +
> > +static int efi_get_device_handle_info(efi_handle_t handle, u16 **name)
> > +{
> > +	struct efi_device_path *dp;
> > +	efi_status_t ret;
> > +
> > +	ret = BS->open_protocol(handle, &efi_guid_device_path,
> > +				(void **)&dp, NULL /* FIXME */, NULL,
> > +				EFI_OPEN_PROTOCOL_GET_PROTOCOL);
> > +	if (ret == EFI_SUCCESS) {
> > +		*name = efi_dp_str(dp);
> > +		return 0;
> > +	} else {
> > +		return -1;
> > +	}
> > +}
> > +
> > +static int do_efi_show_devices(int argc, char * const argv[])
> > +{
> > +	efi_handle_t *handles;
> > +	u16 *devname;
> 
> This is the device path. So why not call it dev_path?

dev_path is normally used for a device path object itself.
I prefer dp_text.

> > +	int num, i;
> > +
> > +	handles = NULL;
> > +	num = 0;
> > +	if (efi_get_handles_by_proto(NULL, &handles, &num))
> > +		return CMD_RET_FAILURE;
> > +
> > +	if (!num)
> > +		return CMD_RET_SUCCESS;
> > +
> > +	printf("D\n");
> > +	printf("e\n");
> > +	printf("v                Device Path\n");
> > +	printf("================ ====================\n");
> > +	for (i = 0; i < num; i++) {
> > +		if (!efi_get_device_handle_info(handles[i], &devname)) {
> > +			printf("%16p %ls\n", handles[i], devname);
> > +			efi_free_pool(devname);
> > +		}
> > +	}
> > +
> > +	free(handles);
> > +
> > +	return CMD_RET_SUCCESS;
> > +}
> > +
> >  static int do_efi_boot_add(int argc, char * const argv[])
> >  {
> 
> Please, use the standard way of adding sub-commands, cf.
> doc/README.commands.

OK

Thanks,
-Takahiro Akashi

> Thanks a lot for your contribution.
> 
> Regards
> 
> Heinrich
> 
> >  	int id;
> > @@ -673,6 +765,8 @@ static int do_efitool(cmd_tbl_t *cmdtp, int flag,
> >  		return do_efi_dump_var(argc, argv);
> >  	else if (!strcmp(command, "setvar"))
> >  		return do_efi_set_var(argc, argv);
> > +	else if (!strcmp(command, "devices"))
> > +		return do_efi_show_devices(argc, argv);
> >  	else
> >  		return CMD_RET_USAGE;
> >  }
> > @@ -697,7 +791,9 @@ static char efitool_help_text[] =
> >  	"  - show UEFI variable's value\n"
> >  	"efitool setvar <name> [<value>]\n"
> >  	"  - set/delete uefi variable's value\n"
> > -	"    <value> may be \"=\"...\"\", \"=0x...\", \"=H...\", (set) or \"=\" (delete)\n";
> > +	"    <value> may be \"=\"...\"\", \"=0x...\", \"=H...\", (set) or \"=\" (delete)\n"
> > +	"efitool devices\n"
> > +	"  - show uefi devices\n";
> >  #endif
> >  
> >  U_BOOT_CMD(
> > 
>
diff mbox series

Patch

================ ====================
        7ef3bfa0 /VenHw(e61d73b9-a384-4acc-aeab-82e828f3628b)
        7ef3cca0 /VenHw(e61d73b9-a384-4acc-aeab-82e828f3628b)/Scsi(0,0)
        7ef3d070 /VenHw(e61d73b9-a384-4acc-aeab-82e828f3628b)/Scsi(1,0)
        7ef3d1b0 /VenHw(e61d73b9-a384-4acc-aeab-82e828f3628b)/Scsi(1,0)/HD(1,MBR,0x086246ba,0x800,0x40000)
        7ef3d3e0 /VenHw(e61d73b9-a384-4acc-aeab-82e828f3628b)/Scsi(1,0)/HD(2,MBR,0x086246ba,0x40800,0x3f800)

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

diff --git a/cmd/efitool.c b/cmd/efitool.c
index c7dc2c11f2e7..6b2df1beedb5 100644
--- a/cmd/efitool.c
+++ b/cmd/efitool.c
@@ -18,6 +18,8 @@ 
 #include <linux/ctype.h>
 #include <asm/global_data.h>
 
+#define BS systab.boottime
+
 static void dump_var_data(char *data, unsigned long len)
 {
 	char *start, *end, *p;
@@ -266,6 +268,96 @@  out:
 	return ret;
 }
 
+static int efi_get_handles_by_proto(efi_guid_t *guid, efi_handle_t **handlesp,
+				    int *num)
+{
+	efi_handle_t *handles = NULL;
+	efi_uintn_t size = 0;
+	efi_status_t ret;
+
+	if (guid) {
+		ret = BS->locate_handle(BY_PROTOCOL, guid, NULL, &size,
+					handles);
+		if (ret == EFI_BUFFER_TOO_SMALL) {
+			handles = calloc(1, size);
+			if (!handles)
+				return -1;
+
+			ret = BS->locate_handle(BY_PROTOCOL, guid, NULL, &size,
+						handles);
+		}
+		if (ret != EFI_SUCCESS) {
+			free(handles);
+			return -1;
+		}
+	} else {
+		ret = BS->locate_handle(ALL_HANDLES, NULL, NULL, &size, NULL);
+		if (ret == EFI_BUFFER_TOO_SMALL) {
+			handles = calloc(1, size);
+			if (!handles)
+				return -1;
+
+			ret = BS->locate_handle(ALL_HANDLES, NULL, NULL, &size,
+						handles);
+		}
+		if (ret != EFI_SUCCESS) {
+			free(handles);
+			return -1;
+		}
+	}
+
+	*handlesp = handles;
+	*num = size / sizeof(efi_handle_t);
+
+	return 0;
+}
+
+static int efi_get_device_handle_info(efi_handle_t handle, u16 **name)
+{
+	struct efi_device_path *dp;
+	efi_status_t ret;
+
+	ret = BS->open_protocol(handle, &efi_guid_device_path,
+				(void **)&dp, NULL /* FIXME */, NULL,
+				EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+	if (ret == EFI_SUCCESS) {
+		*name = efi_dp_str(dp);
+		return 0;
+	} else {
+		return -1;
+	}
+}
+
+static int do_efi_show_devices(int argc, char * const argv[])
+{
+	efi_handle_t *handles;
+	u16 *devname;
+	int num, i;
+
+	handles = NULL;
+	num = 0;
+	if (efi_get_handles_by_proto(NULL, &handles, &num))
+		return CMD_RET_FAILURE;
+
+	if (!num)
+		return CMD_RET_SUCCESS;
+
+	printf("D\n");
+	printf("e\n");
+	printf("v                Device Path\n");
+	printf("================ ====================\n");
+	for (i = 0; i < num; i++) {
+		if (!efi_get_device_handle_info(handles[i], &devname)) {
+			printf("%16p %ls\n", handles[i], devname);
+			efi_free_pool(devname);
+		}
+	}
+
+	free(handles);
+
+	return CMD_RET_SUCCESS;
+}
+
 static int do_efi_boot_add(int argc, char * const argv[])
 {
 	int id;
@@ -673,6 +765,8 @@  static int do_efitool(cmd_tbl_t *cmdtp, int flag,
 		return do_efi_dump_var(argc, argv);
 	else if (!strcmp(command, "setvar"))
 		return do_efi_set_var(argc, argv);
+	else if (!strcmp(command, "devices"))
+		return do_efi_show_devices(argc, argv);
 	else
 		return CMD_RET_USAGE;
 }
@@ -697,7 +791,9 @@  static char efitool_help_text[] =
 	"  - show UEFI variable's value\n"
 	"efitool setvar <name> [<value>]\n"
 	"  - set/delete uefi variable's value\n"
-	"    <value> may be \"=\"...\"\", \"=0x...\", \"=H...\", (set) or \"=\" (delete)\n";
+	"    <value> may be \"=\"...\"\", \"=0x...\", \"=H...\", (set) or \"=\" (delete)\n"
+	"efitool devices\n"
+	"  - show uefi devices\n";
 #endif
 
 U_BOOT_CMD(