mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/3] partitions: efi: register guid device parameter for disk GUID
@ 2022-06-30 12:40 Ahmad Fatoum
  2022-06-30 12:40 ` [PATCH 2/3] commands: implement devlookup to find device behind device file Ahmad Fatoum
  2022-06-30 12:40 ` [PATCH 3/3] block: efi: allow disabling /dev/usbdiskX renaming Ahmad Fatoum
  0 siblings, 2 replies; 8+ messages in thread
From: Ahmad Fatoum @ 2022-06-30 12:40 UTC (permalink / raw)
  To: barebox; +Cc: has, Ahmad Fatoum

We already register an nt_signature parameter for MBR partitions.
Register an equivalent guid parameter for GPT partitions as well.
This is less critical because disk GUID isn't used for root=
Linux boot argument computation, as each partition has its own
PARTUUID. It's still useful to allow shell scripts to check
against it to detect e.g. a factory flash image.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/partitions/efi.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/common/partitions/efi.c b/common/partitions/efi.c
index 848bed22610f..0f3f790539cd 100644
--- a/common/partitions/efi.c
+++ b/common/partitions/efi.c
@@ -446,6 +446,7 @@ static void efi_partition(void *buf, struct block_device *blk,
 	}
 
 	snprintf(blk->cdev.uuid, sizeof(blk->cdev.uuid), "%pUl", &gpt->disk_guid);
+	dev_add_param_string_fixed(blk->dev, "guid", blk->cdev.uuid);
 
 	nb_part = le32_to_cpu(gpt->num_partition_entries);
 
-- 
2.30.2




^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 2/3] commands: implement devlookup to find device behind device file
  2022-06-30 12:40 [PATCH 1/3] partitions: efi: register guid device parameter for disk GUID Ahmad Fatoum
@ 2022-06-30 12:40 ` Ahmad Fatoum
  2022-06-30 15:26   ` Michael Olbrich
  2022-07-05  7:45   ` Sascha Hauer
  2022-06-30 12:40 ` [PATCH 3/3] block: efi: allow disabling /dev/usbdiskX renaming Ahmad Fatoum
  1 sibling, 2 replies; 8+ messages in thread
From: Ahmad Fatoum @ 2022-06-30 12:40 UTC (permalink / raw)
  To: barebox; +Cc: has, Ahmad Fatoum

For OF-enabled platforms with aliases, device file naming is pretty much
solved: If there is mmc2 = &something, then we have a mmc2 device and
a /dev/mmc2 device file. For other platforms like x86, EFI-provided
devices are harder to get ahold of. Add a command to make this
straight-forward to do in scripts. The main use of this is probably to
access parameters like nt_signature or guid:

  devloop /dev/disk0 guid

This would print to console, but we have no output capture yet, so add
an optional -v VARIABLE parameter as well to allow easy use from
scripts.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 commands/Kconfig     | 13 ++++++++
 commands/Makefile    |  1 +
 commands/devlookup.c | 73 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 87 insertions(+)
 create mode 100644 commands/devlookup.c

diff --git a/commands/Kconfig b/commands/Kconfig
index fb4dcefed8bf..5bab78fd1ce7 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -75,6 +75,19 @@ config CMD_DEVINFO
 	  If called with a device path being the argument, devinfo shows more
 	  default information about this device and its parameters.
 
+config CMD_DEVLOOKUP
+	tristate
+	prompt "devlookup"
+	help
+	  Look up device behind device file and its parameters
+
+	  devlookup [-v VAR] /dev/DEVICE [parameter]
+
+          Detects the device behind a device file and outputs it,
+          unless a second argument is given. In that case the device
+          parameter with that name is looked up. Specifying -v VARIABLE
+          will write output to VARIABLE instead of printing it.
+
 config CMD_DEVUNBIND
 	tristate
 	prompt "devunbind"
diff --git a/commands/Makefile b/commands/Makefile
index 6c3a7a1dabcd..b43da1a80173 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -110,6 +110,7 @@ obj-$(CONFIG_CMD_DETECT)	+= detect.o
 obj-$(CONFIG_CMD_BOOT)		+= boot.o
 obj-$(CONFIG_CMD_DEVINFO)	+= devinfo.o
 obj-$(CONFIG_CMD_DEVUNBIND)	+= devunbind.o
+obj-$(CONFIG_CMD_DEVLOOKUP)	+= devlookup.o
 obj-$(CONFIG_CMD_DRVINFO)	+= drvinfo.o
 obj-$(CONFIG_CMD_READF)		+= readf.o
 obj-$(CONFIG_CMD_MENUTREE)	+= menutree.o
diff --git a/commands/devlookup.c b/commands/devlookup.c
new file mode 100644
index 000000000000..9599df7b10bc
--- /dev/null
+++ b/commands/devlookup.c
@@ -0,0 +1,73 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <common.h>
+#include <command.h>
+#include <fs.h>
+#include <getopt.h>
+#include <malloc.h>
+#include <linux/stat.h>
+#include <linux/ctype.h>
+#include <environment.h>
+
+static int report(const char *variable, const char *val)
+{
+	if (variable)
+		return setenv(variable, val);
+
+	if (!val)
+		return -errno;
+
+	printf("%s\n", val);
+	return 0;
+}
+
+static int do_devlookup(int argc, char *argv[])
+{
+	const char *variable = NULL, *devicefile, *paramname;
+	struct cdev *cdev;
+	int opt;
+
+	while ((opt = getopt(argc, argv, "v:")) > 0) {
+		switch(opt) {
+		case 'v':
+			variable = optarg;
+			break;
+		}
+	}
+
+	if (argc - optind == 0 || argc - optind > 2)
+		return COMMAND_ERROR_USAGE;
+
+	devicefile = argv[optind];
+	paramname  = argv[optind+1];
+
+	if (!strstarts(devicefile, "/dev/"))
+		return COMMAND_ERROR_USAGE;
+
+	devicefile += sizeof "/dev/" - 1;
+
+	cdev = cdev_by_name(devicefile);
+	if (!cdev)
+		return -ENOENT;
+
+	if (paramname)
+		return report(variable, dev_get_param(cdev->dev, paramname));
+
+	return report(variable, dev_name(cdev->dev));
+}
+
+BAREBOX_CMD_HELP_START(devlookup)
+BAREBOX_CMD_HELP_TEXT("Detects the device behind a device file and outputs it,")
+BAREBOX_CMD_HELP_TEXT("unless a second argument is given. In that case the device")
+BAREBOX_CMD_HELP_TEXT("parameter with that name is looked up. Specifying -v VARIABLE")
+BAREBOX_CMD_HELP_TEXT("will write output to VARIABLE instead of printing it")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(devlookup)
+	.cmd		= do_devlookup,
+	BAREBOX_CMD_DESC("look up device behind device file and its parameters")
+	BAREBOX_CMD_OPTS("[-v VAR] /dev/DEVICE [parameter]")
+	BAREBOX_CMD_GROUP(CMD_GRP_SCRIPT)
+	BAREBOX_CMD_HELP(cmd_devlookup_help)
+BAREBOX_CMD_END
+
-- 
2.30.2




^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 3/3] block: efi: allow disabling /dev/usbdiskX renaming
  2022-06-30 12:40 [PATCH 1/3] partitions: efi: register guid device parameter for disk GUID Ahmad Fatoum
  2022-06-30 12:40 ` [PATCH 2/3] commands: implement devlookup to find device behind device file Ahmad Fatoum
@ 2022-06-30 12:40 ` Ahmad Fatoum
  2022-06-30 13:26   ` [PATCH] fixup! commands: implement new devlookup command Ahmad Fatoum
  1 sibling, 1 reply; 8+ messages in thread
From: Ahmad Fatoum @ 2022-06-30 12:40 UTC (permalink / raw)
  To: barebox; +Cc: has, Ahmad Fatoum

Some buggy UEFI implementations have been observed to not set
EFI_USB_IO protocol on handles that were instantiated from
USB mass storage. This leads to confusing behavior when barebox
uses /dev/usbdisk for some USB sticks and doesn't for some others.

The proper behavior is not relying on the UEFI and instead check
e.g. GUID of the boot disk image:

  if [ "$bootsource" = usb ]; then
    mydisk = /dev/usbdisk${bootsource_instance}
  else
    mydisk = /dev/disk${bootsource_instance}
  fi

  devlookup -v bootdiskguid $mydisk guid
  if [ "$bootdiskguid" = "my-guid" ]; then
    boot ${mydisk}.1
  else
    boot boochooser
  fi

When going that way, the renaming to usbdisk is just annoying,
so allow disabling it to simplify scripting.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/block/Kconfig        | 21 +++++++++++++++++++++
 drivers/block/Makefile       |  2 +-
 drivers/block/efi-block-io.c |  3 ++-
 3 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/drivers/block/Kconfig b/drivers/block/Kconfig
index 68a11438dcf3..625e81a4ec97 100644
--- a/drivers/block/Kconfig
+++ b/drivers/block/Kconfig
@@ -5,3 +5,24 @@ config VIRTIO_BLK
         help
           This is the virtual block driver for virtio.  It can be used with
           QEMU based VMMs (like KVM or Xen).
+
+config EFI_BLK
+	bool "EFI block I/O driver"
+	default y
+	depends on EFI_BOOTUP
+
+config EFI_BLK_SEPARATE_USBDISK
+	bool "rename USB devices to /dev/usbdiskX"
+	default y
+	depends on EFI_BLK
+	help
+	  EFI block devices will be normally called /dev/diskX. Setting this
+	  option will cause block devices instantiated from handles with a
+	  EFI_USB_IO protocol to be called /dev/usbdiskX instead. Note that
+	  some buggy UEFI implementations have been observed to not do this
+	  consistently for all USB mass storage. If you need to absolutely
+	  be sure your boot device is a USB mass storage device and you can't
+	  fix your UEFI, consider disabling this options and setting a GUID
+	  for your disk and checking against it with
+
+	    devlookup -v $bootguid /dev/disk$bootsource_instance guid
diff --git a/drivers/block/Makefile b/drivers/block/Makefile
index c50bdc1d02bd..a4e14a559cf7 100644
--- a/drivers/block/Makefile
+++ b/drivers/block/Makefile
@@ -1,3 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0-only
-obj-$(CONFIG_EFI_BOOTUP) += efi-block-io.o
+obj-$(CONFIG_EFI_BLK) += efi-block-io.o
 obj-$(CONFIG_VIRTIO_BLK) += virtio_blk.o
diff --git a/drivers/block/efi-block-io.c b/drivers/block/efi-block-io.c
index 086afb378ab4..120e4d8f1ad2 100644
--- a/drivers/block/efi-block-io.c
+++ b/drivers/block/efi-block-io.c
@@ -140,7 +140,8 @@ static void efi_bio_print_info(struct device_d *dev)
 
 static bool is_bio_usbdev(struct efi_device *efidev)
 {
-	return efi_device_has_guid(efidev, EFI_USB_IO_PROTOCOL_GUID);
+	return IS_ENABLED(CONFIG_EFI_BLK_SEPARATE_USBDISK) &&
+		efi_device_has_guid(efidev, EFI_USB_IO_PROTOCOL_GUID);
 }
 
 static int efi_bio_probe(struct efi_device *efidev)
-- 
2.30.2




^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH] fixup! commands: implement new devlookup command
  2022-06-30 12:40 ` [PATCH 3/3] block: efi: allow disabling /dev/usbdiskX renaming Ahmad Fatoum
@ 2022-06-30 13:26   ` Ahmad Fatoum
  0 siblings, 0 replies; 8+ messages in thread
From: Ahmad Fatoum @ 2022-06-30 13:26 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

Propagate error correctly when writing into environment variable as
well. While at it, ensure we do return an error value.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 commands/devlookup.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/commands/devlookup.c b/commands/devlookup.c
index 9599df7b10bc..ce4cf2e10f45 100644
--- a/commands/devlookup.c
+++ b/commands/devlookup.c
@@ -11,12 +11,12 @@
 
 static int report(const char *variable, const char *val)
 {
+	if (!val)
+		return -(errno ?: EINVAL);
+
 	if (variable)
 		return setenv(variable, val);
 
-	if (!val)
-		return -errno;
-
 	printf("%s\n", val);
 	return 0;
 }
-- 
2.30.2




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/3] commands: implement devlookup to find device behind device file
  2022-06-30 12:40 ` [PATCH 2/3] commands: implement devlookup to find device behind device file Ahmad Fatoum
@ 2022-06-30 15:26   ` Michael Olbrich
  2022-07-01  5:24     ` Ahmad Fatoum
  2022-07-05  7:45   ` Sascha Hauer
  1 sibling, 1 reply; 8+ messages in thread
From: Michael Olbrich @ 2022-06-30 15:26 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox, has

On Thu, Jun 30, 2022 at 02:40:34PM +0200, Ahmad Fatoum wrote:
> For OF-enabled platforms with aliases, device file naming is pretty much
> solved: If there is mmc2 = &something, then we have a mmc2 device and
> a /dev/mmc2 device file. For other platforms like x86, EFI-provided
> devices are harder to get ahold of. Add a command to make this
> straight-forward to do in scripts. The main use of this is probably to
> access parameters like nt_signature or guid:
> 
>   devloop /dev/disk0 guid

   devlookup /dev/disk0 guid

Right?

Michael

> 
> This would print to console, but we have no output capture yet, so add
> an optional -v VARIABLE parameter as well to allow easy use from
> scripts.
> 
> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
>  commands/Kconfig     | 13 ++++++++
>  commands/Makefile    |  1 +
>  commands/devlookup.c | 73 ++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 87 insertions(+)
>  create mode 100644 commands/devlookup.c
> 
> diff --git a/commands/Kconfig b/commands/Kconfig
> index fb4dcefed8bf..5bab78fd1ce7 100644
> --- a/commands/Kconfig
> +++ b/commands/Kconfig
> @@ -75,6 +75,19 @@ config CMD_DEVINFO
>  	  If called with a device path being the argument, devinfo shows more
>  	  default information about this device and its parameters.
>  
> +config CMD_DEVLOOKUP
> +	tristate
> +	prompt "devlookup"
> +	help
> +	  Look up device behind device file and its parameters
> +
> +	  devlookup [-v VAR] /dev/DEVICE [parameter]
> +
> +          Detects the device behind a device file and outputs it,
> +          unless a second argument is given. In that case the device
> +          parameter with that name is looked up. Specifying -v VARIABLE
> +          will write output to VARIABLE instead of printing it.
> +
>  config CMD_DEVUNBIND
>  	tristate
>  	prompt "devunbind"
> diff --git a/commands/Makefile b/commands/Makefile
> index 6c3a7a1dabcd..b43da1a80173 100644
> --- a/commands/Makefile
> +++ b/commands/Makefile
> @@ -110,6 +110,7 @@ obj-$(CONFIG_CMD_DETECT)	+= detect.o
>  obj-$(CONFIG_CMD_BOOT)		+= boot.o
>  obj-$(CONFIG_CMD_DEVINFO)	+= devinfo.o
>  obj-$(CONFIG_CMD_DEVUNBIND)	+= devunbind.o
> +obj-$(CONFIG_CMD_DEVLOOKUP)	+= devlookup.o
>  obj-$(CONFIG_CMD_DRVINFO)	+= drvinfo.o
>  obj-$(CONFIG_CMD_READF)		+= readf.o
>  obj-$(CONFIG_CMD_MENUTREE)	+= menutree.o
> diff --git a/commands/devlookup.c b/commands/devlookup.c
> new file mode 100644
> index 000000000000..9599df7b10bc
> --- /dev/null
> +++ b/commands/devlookup.c
> @@ -0,0 +1,73 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +
> +#include <common.h>
> +#include <command.h>
> +#include <fs.h>
> +#include <getopt.h>
> +#include <malloc.h>
> +#include <linux/stat.h>
> +#include <linux/ctype.h>
> +#include <environment.h>
> +
> +static int report(const char *variable, const char *val)
> +{
> +	if (variable)
> +		return setenv(variable, val);
> +
> +	if (!val)
> +		return -errno;
> +
> +	printf("%s\n", val);
> +	return 0;
> +}
> +
> +static int do_devlookup(int argc, char *argv[])
> +{
> +	const char *variable = NULL, *devicefile, *paramname;
> +	struct cdev *cdev;
> +	int opt;
> +
> +	while ((opt = getopt(argc, argv, "v:")) > 0) {
> +		switch(opt) {
> +		case 'v':
> +			variable = optarg;
> +			break;
> +		}
> +	}
> +
> +	if (argc - optind == 0 || argc - optind > 2)
> +		return COMMAND_ERROR_USAGE;
> +
> +	devicefile = argv[optind];
> +	paramname  = argv[optind+1];
> +
> +	if (!strstarts(devicefile, "/dev/"))
> +		return COMMAND_ERROR_USAGE;
> +
> +	devicefile += sizeof "/dev/" - 1;
> +
> +	cdev = cdev_by_name(devicefile);
> +	if (!cdev)
> +		return -ENOENT;
> +
> +	if (paramname)
> +		return report(variable, dev_get_param(cdev->dev, paramname));
> +
> +	return report(variable, dev_name(cdev->dev));
> +}
> +
> +BAREBOX_CMD_HELP_START(devlookup)
> +BAREBOX_CMD_HELP_TEXT("Detects the device behind a device file and outputs it,")
> +BAREBOX_CMD_HELP_TEXT("unless a second argument is given. In that case the device")
> +BAREBOX_CMD_HELP_TEXT("parameter with that name is looked up. Specifying -v VARIABLE")
> +BAREBOX_CMD_HELP_TEXT("will write output to VARIABLE instead of printing it")
> +BAREBOX_CMD_HELP_END
> +
> +BAREBOX_CMD_START(devlookup)
> +	.cmd		= do_devlookup,
> +	BAREBOX_CMD_DESC("look up device behind device file and its parameters")
> +	BAREBOX_CMD_OPTS("[-v VAR] /dev/DEVICE [parameter]")
> +	BAREBOX_CMD_GROUP(CMD_GRP_SCRIPT)
> +	BAREBOX_CMD_HELP(cmd_devlookup_help)
> +BAREBOX_CMD_END
> +
> -- 
> 2.30.2
> 
> 
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/3] commands: implement devlookup to find device behind device file
  2022-06-30 15:26   ` Michael Olbrich
@ 2022-07-01  5:24     ` Ahmad Fatoum
  0 siblings, 0 replies; 8+ messages in thread
From: Ahmad Fatoum @ 2022-07-01  5:24 UTC (permalink / raw)
  To: barebox, has, Michael Olbrich

Hello,

On 30.06.22 17:26, Michael Olbrich wrote:
> On Thu, Jun 30, 2022 at 02:40:34PM +0200, Ahmad Fatoum wrote:
>> For OF-enabled platforms with aliases, device file naming is pretty much
>> solved: If there is mmc2 = &something, then we have a mmc2 device and
>> a /dev/mmc2 device file. For other platforms like x86, EFI-provided
>> devices are harder to get ahold of. Add a command to make this
>> straight-forward to do in scripts. The main use of this is probably to
>> access parameters like nt_signature or guid:
>>
>>   devloop /dev/disk0 guid
> 
>    devlookup /dev/disk0 guid
> 
> Right?

Yes, thanks. Sascha, please let me know if I should resend.

> 
> Michael
> 
>>
>> This would print to console, but we have no output capture yet, so add
>> an optional -v VARIABLE parameter as well to allow easy use from
>> scripts.
>>
>> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
>> ---
>>  commands/Kconfig     | 13 ++++++++
>>  commands/Makefile    |  1 +
>>  commands/devlookup.c | 73 ++++++++++++++++++++++++++++++++++++++++++++
>>  3 files changed, 87 insertions(+)
>>  create mode 100644 commands/devlookup.c
>>
>> diff --git a/commands/Kconfig b/commands/Kconfig
>> index fb4dcefed8bf..5bab78fd1ce7 100644
>> --- a/commands/Kconfig
>> +++ b/commands/Kconfig
>> @@ -75,6 +75,19 @@ config CMD_DEVINFO
>>  	  If called with a device path being the argument, devinfo shows more
>>  	  default information about this device and its parameters.
>>  
>> +config CMD_DEVLOOKUP
>> +	tristate
>> +	prompt "devlookup"
>> +	help
>> +	  Look up device behind device file and its parameters
>> +
>> +	  devlookup [-v VAR] /dev/DEVICE [parameter]
>> +
>> +          Detects the device behind a device file and outputs it,
>> +          unless a second argument is given. In that case the device
>> +          parameter with that name is looked up. Specifying -v VARIABLE
>> +          will write output to VARIABLE instead of printing it.
>> +
>>  config CMD_DEVUNBIND
>>  	tristate
>>  	prompt "devunbind"
>> diff --git a/commands/Makefile b/commands/Makefile
>> index 6c3a7a1dabcd..b43da1a80173 100644
>> --- a/commands/Makefile
>> +++ b/commands/Makefile
>> @@ -110,6 +110,7 @@ obj-$(CONFIG_CMD_DETECT)	+= detect.o
>>  obj-$(CONFIG_CMD_BOOT)		+= boot.o
>>  obj-$(CONFIG_CMD_DEVINFO)	+= devinfo.o
>>  obj-$(CONFIG_CMD_DEVUNBIND)	+= devunbind.o
>> +obj-$(CONFIG_CMD_DEVLOOKUP)	+= devlookup.o
>>  obj-$(CONFIG_CMD_DRVINFO)	+= drvinfo.o
>>  obj-$(CONFIG_CMD_READF)		+= readf.o
>>  obj-$(CONFIG_CMD_MENUTREE)	+= menutree.o
>> diff --git a/commands/devlookup.c b/commands/devlookup.c
>> new file mode 100644
>> index 000000000000..9599df7b10bc
>> --- /dev/null
>> +++ b/commands/devlookup.c
>> @@ -0,0 +1,73 @@
>> +// SPDX-License-Identifier: GPL-2.0-only
>> +
>> +#include <common.h>
>> +#include <command.h>
>> +#include <fs.h>
>> +#include <getopt.h>
>> +#include <malloc.h>
>> +#include <linux/stat.h>
>> +#include <linux/ctype.h>
>> +#include <environment.h>
>> +
>> +static int report(const char *variable, const char *val)
>> +{
>> +	if (variable)
>> +		return setenv(variable, val);
>> +
>> +	if (!val)
>> +		return -errno;
>> +
>> +	printf("%s\n", val);
>> +	return 0;
>> +}
>> +
>> +static int do_devlookup(int argc, char *argv[])
>> +{
>> +	const char *variable = NULL, *devicefile, *paramname;
>> +	struct cdev *cdev;
>> +	int opt;
>> +
>> +	while ((opt = getopt(argc, argv, "v:")) > 0) {
>> +		switch(opt) {
>> +		case 'v':
>> +			variable = optarg;
>> +			break;
>> +		}
>> +	}
>> +
>> +	if (argc - optind == 0 || argc - optind > 2)
>> +		return COMMAND_ERROR_USAGE;
>> +
>> +	devicefile = argv[optind];
>> +	paramname  = argv[optind+1];
>> +
>> +	if (!strstarts(devicefile, "/dev/"))
>> +		return COMMAND_ERROR_USAGE;
>> +
>> +	devicefile += sizeof "/dev/" - 1;
>> +
>> +	cdev = cdev_by_name(devicefile);
>> +	if (!cdev)
>> +		return -ENOENT;
>> +
>> +	if (paramname)
>> +		return report(variable, dev_get_param(cdev->dev, paramname));
>> +
>> +	return report(variable, dev_name(cdev->dev));
>> +}
>> +
>> +BAREBOX_CMD_HELP_START(devlookup)
>> +BAREBOX_CMD_HELP_TEXT("Detects the device behind a device file and outputs it,")
>> +BAREBOX_CMD_HELP_TEXT("unless a second argument is given. In that case the device")
>> +BAREBOX_CMD_HELP_TEXT("parameter with that name is looked up. Specifying -v VARIABLE")
>> +BAREBOX_CMD_HELP_TEXT("will write output to VARIABLE instead of printing it")
>> +BAREBOX_CMD_HELP_END
>> +
>> +BAREBOX_CMD_START(devlookup)
>> +	.cmd		= do_devlookup,
>> +	BAREBOX_CMD_DESC("look up device behind device file and its parameters")
>> +	BAREBOX_CMD_OPTS("[-v VAR] /dev/DEVICE [parameter]")
>> +	BAREBOX_CMD_GROUP(CMD_GRP_SCRIPT)
>> +	BAREBOX_CMD_HELP(cmd_devlookup_help)
>> +BAREBOX_CMD_END
>> +
>> -- 
>> 2.30.2
>>
>>
>>
> 


-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/3] commands: implement devlookup to find device behind device file
  2022-06-30 12:40 ` [PATCH 2/3] commands: implement devlookup to find device behind device file Ahmad Fatoum
  2022-06-30 15:26   ` Michael Olbrich
@ 2022-07-05  7:45   ` Sascha Hauer
  2022-07-05  7:48     ` Ahmad Fatoum
  1 sibling, 1 reply; 8+ messages in thread
From: Sascha Hauer @ 2022-07-05  7:45 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox, has

On Thu, Jun 30, 2022 at 02:40:34PM +0200, Ahmad Fatoum wrote:
> For OF-enabled platforms with aliases, device file naming is pretty much
> solved: If there is mmc2 = &something, then we have a mmc2 device and
> a /dev/mmc2 device file. For other platforms like x86, EFI-provided
> devices are harder to get ahold of. Add a command to make this
> straight-forward to do in scripts. The main use of this is probably to
> access parameters like nt_signature or guid:
> 
>   devloop /dev/disk0 guid
> 
> This would print to console, but we have no output capture yet, so add
> an optional -v VARIABLE parameter as well to allow easy use from
> scripts.
> 
> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
>  commands/Kconfig     | 13 ++++++++
>  commands/Makefile    |  1 +
>  commands/devlookup.c | 73 ++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 87 insertions(+)
>  create mode 100644 commands/devlookup.c
> 
> diff --git a/commands/Kconfig b/commands/Kconfig
> index fb4dcefed8bf..5bab78fd1ce7 100644
> --- a/commands/Kconfig
> +++ b/commands/Kconfig
> @@ -75,6 +75,19 @@ config CMD_DEVINFO
>  	  If called with a device path being the argument, devinfo shows more
>  	  default information about this device and its parameters.
>  
> +config CMD_DEVLOOKUP
> +	tristate
> +	prompt "devlookup"
> +	help
> +	  Look up device behind device file and its parameters
> +
> +	  devlookup [-v VAR] /dev/DEVICE [parameter]
> +
> +          Detects the device behind a device file and outputs it,
> +          unless a second argument is given. In that case the device
> +          parameter with that name is looked up. Specifying -v VARIABLE
> +          will write output to VARIABLE instead of printing it.
> +
>  config CMD_DEVUNBIND
>  	tristate
>  	prompt "devunbind"
> diff --git a/commands/Makefile b/commands/Makefile
> index 6c3a7a1dabcd..b43da1a80173 100644
> --- a/commands/Makefile
> +++ b/commands/Makefile
> @@ -110,6 +110,7 @@ obj-$(CONFIG_CMD_DETECT)	+= detect.o
>  obj-$(CONFIG_CMD_BOOT)		+= boot.o
>  obj-$(CONFIG_CMD_DEVINFO)	+= devinfo.o
>  obj-$(CONFIG_CMD_DEVUNBIND)	+= devunbind.o
> +obj-$(CONFIG_CMD_DEVLOOKUP)	+= devlookup.o
>  obj-$(CONFIG_CMD_DRVINFO)	+= drvinfo.o
>  obj-$(CONFIG_CMD_READF)		+= readf.o
>  obj-$(CONFIG_CMD_MENUTREE)	+= menutree.o
> diff --git a/commands/devlookup.c b/commands/devlookup.c
> new file mode 100644
> index 000000000000..9599df7b10bc
> --- /dev/null
> +++ b/commands/devlookup.c
> @@ -0,0 +1,73 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +
> +#include <common.h>
> +#include <command.h>
> +#include <fs.h>
> +#include <getopt.h>
> +#include <malloc.h>
> +#include <linux/stat.h>
> +#include <linux/ctype.h>
> +#include <environment.h>
> +
> +static int report(const char *variable, const char *val)
> +{
> +	if (variable)
> +		return setenv(variable, val);
> +
> +	if (!val)
> +		return -errno;
> +
> +	printf("%s\n", val);
> +	return 0;
> +}
> +
> +static int do_devlookup(int argc, char *argv[])
> +{
> +	const char *variable = NULL, *devicefile, *paramname;
> +	struct cdev *cdev;
> +	int opt;
> +
> +	while ((opt = getopt(argc, argv, "v:")) > 0) {
> +		switch(opt) {
> +		case 'v':
> +			variable = optarg;
> +			break;
> +		}
> +	}
> +
> +	if (argc - optind == 0 || argc - optind > 2)
> +		return COMMAND_ERROR_USAGE;
> +
> +	devicefile = argv[optind];
> +	paramname  = argv[optind+1];
> +
> +	if (!strstarts(devicefile, "/dev/"))
> +		return COMMAND_ERROR_USAGE;

Should we skip the /dev/ part so that we can allow cdev names directly?
Something like:

	if (strstarts(devicefile, "/dev/"))
		devicefile += sizeof "/dev/" - 1;

Sascha


-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/3] commands: implement devlookup to find device behind device file
  2022-07-05  7:45   ` Sascha Hauer
@ 2022-07-05  7:48     ` Ahmad Fatoum
  0 siblings, 0 replies; 8+ messages in thread
From: Ahmad Fatoum @ 2022-07-05  7:48 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: barebox, has

On 05.07.22 09:45, Sascha Hauer wrote:
> On Thu, Jun 30, 2022 at 02:40:34PM +0200, Ahmad Fatoum wrote:
>> For OF-enabled platforms with aliases, device file naming is pretty much
>> solved: If there is mmc2 = &something, then we have a mmc2 device and
>> a /dev/mmc2 device file. For other platforms like x86, EFI-provided
>> devices are harder to get ahold of. Add a command to make this
>> straight-forward to do in scripts. The main use of this is probably to
>> access parameters like nt_signature or guid:
>>
>>   devloop /dev/disk0 guid
>>
>> This would print to console, but we have no output capture yet, so add
>> an optional -v VARIABLE parameter as well to allow easy use from
>> scripts.
>>
>> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
>> ---
>>  commands/Kconfig     | 13 ++++++++
>>  commands/Makefile    |  1 +
>>  commands/devlookup.c | 73 ++++++++++++++++++++++++++++++++++++++++++++
>>  3 files changed, 87 insertions(+)
>>  create mode 100644 commands/devlookup.c
>>
>> diff --git a/commands/Kconfig b/commands/Kconfig
>> index fb4dcefed8bf..5bab78fd1ce7 100644
>> --- a/commands/Kconfig
>> +++ b/commands/Kconfig
>> @@ -75,6 +75,19 @@ config CMD_DEVINFO
>>  	  If called with a device path being the argument, devinfo shows more
>>  	  default information about this device and its parameters.
>>  
>> +config CMD_DEVLOOKUP
>> +	tristate
>> +	prompt "devlookup"
>> +	help
>> +	  Look up device behind device file and its parameters
>> +
>> +	  devlookup [-v VAR] /dev/DEVICE [parameter]
>> +
>> +          Detects the device behind a device file and outputs it,
>> +          unless a second argument is given. In that case the device
>> +          parameter with that name is looked up. Specifying -v VARIABLE
>> +          will write output to VARIABLE instead of printing it.
>> +
>>  config CMD_DEVUNBIND
>>  	tristate
>>  	prompt "devunbind"
>> diff --git a/commands/Makefile b/commands/Makefile
>> index 6c3a7a1dabcd..b43da1a80173 100644
>> --- a/commands/Makefile
>> +++ b/commands/Makefile
>> @@ -110,6 +110,7 @@ obj-$(CONFIG_CMD_DETECT)	+= detect.o
>>  obj-$(CONFIG_CMD_BOOT)		+= boot.o
>>  obj-$(CONFIG_CMD_DEVINFO)	+= devinfo.o
>>  obj-$(CONFIG_CMD_DEVUNBIND)	+= devunbind.o
>> +obj-$(CONFIG_CMD_DEVLOOKUP)	+= devlookup.o
>>  obj-$(CONFIG_CMD_DRVINFO)	+= drvinfo.o
>>  obj-$(CONFIG_CMD_READF)		+= readf.o
>>  obj-$(CONFIG_CMD_MENUTREE)	+= menutree.o
>> diff --git a/commands/devlookup.c b/commands/devlookup.c
>> new file mode 100644
>> index 000000000000..9599df7b10bc
>> --- /dev/null
>> +++ b/commands/devlookup.c
>> @@ -0,0 +1,73 @@
>> +// SPDX-License-Identifier: GPL-2.0-only
>> +
>> +#include <common.h>
>> +#include <command.h>
>> +#include <fs.h>
>> +#include <getopt.h>
>> +#include <malloc.h>
>> +#include <linux/stat.h>
>> +#include <linux/ctype.h>
>> +#include <environment.h>
>> +
>> +static int report(const char *variable, const char *val)
>> +{
>> +	if (variable)
>> +		return setenv(variable, val);
>> +
>> +	if (!val)
>> +		return -errno;
>> +
>> +	printf("%s\n", val);
>> +	return 0;
>> +}
>> +
>> +static int do_devlookup(int argc, char *argv[])
>> +{
>> +	const char *variable = NULL, *devicefile, *paramname;
>> +	struct cdev *cdev;
>> +	int opt;
>> +
>> +	while ((opt = getopt(argc, argv, "v:")) > 0) {
>> +		switch(opt) {
>> +		case 'v':
>> +			variable = optarg;
>> +			break;
>> +		}
>> +	}
>> +
>> +	if (argc - optind == 0 || argc - optind > 2)
>> +		return COMMAND_ERROR_USAGE;
>> +
>> +	devicefile = argv[optind];
>> +	paramname  = argv[optind+1];
>> +
>> +	if (!strstarts(devicefile, "/dev/"))
>> +		return COMMAND_ERROR_USAGE;
> 
> Should we skip the /dev/ part so that we can allow cdev names directly?
> Something like:
> 
> 	if (strstarts(devicefile, "/dev/"))
> 		devicefile += sizeof "/dev/" - 1;

Sounds good to me.

> 
> Sascha
> 
> 


-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2022-07-05  7:49 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-30 12:40 [PATCH 1/3] partitions: efi: register guid device parameter for disk GUID Ahmad Fatoum
2022-06-30 12:40 ` [PATCH 2/3] commands: implement devlookup to find device behind device file Ahmad Fatoum
2022-06-30 15:26   ` Michael Olbrich
2022-07-01  5:24     ` Ahmad Fatoum
2022-07-05  7:45   ` Sascha Hauer
2022-07-05  7:48     ` Ahmad Fatoum
2022-06-30 12:40 ` [PATCH 3/3] block: efi: allow disabling /dev/usbdiskX renaming Ahmad Fatoum
2022-06-30 13:26   ` [PATCH] fixup! commands: implement new devlookup command Ahmad Fatoum

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox