mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <sha@pengutronix.de>
To: Ahmad Fatoum <a.fatoum@pengutronix.de>
Cc: barebox@lists.infradead.org
Subject: Re: [PATCH v2 2/3] commands: implement devlookup to find device behind device file
Date: Tue, 5 Jul 2022 13:59:23 +0200	[thread overview]
Message-ID: <20220705115923.GG5208@pengutronix.de> (raw)
In-Reply-To: <20220705115538.3248502-2-a.fatoum@pengutronix.de>

On Tue, Jul 05, 2022 at 01:55:37PM +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:
> 
>   devlookup /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>
> ---
> v1 -> v2:
>   - fix typo in commit message (Michael)
>   - accept cdev without /dev/ prefix (Sascha)
>   - early exit on cdev without dev, e.g. /dev/zero
> ---
>  commands/Kconfig     | 13 ++++++++
>  commands/Makefile    |  1 +
>  commands/devlookup.c | 78 ++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 92 insertions(+)
>  create mode 100644 commands/devlookup.c
> 
> diff --git a/commands/Kconfig b/commands/Kconfig
> index c5505321cfd9..037b41155695 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 b3b7bafe6b64..0aae8893d696 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..c361d5b58860
> --- /dev/null
> +++ b/commands/devlookup.c
> @@ -0,0 +1,78 @@
> +// 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 (!val)
> +		return -(errno ?: EINVAL);
> +
> +	if (variable)
> +		return setenv(variable, val);
> +
> +	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/"))
> +		devicefile += sizeof("/dev/") - 1;

Forgot to mention: We have devpath_to_name() which does exactly that.
Also I merged your last series with that change, so unless you have
made other changes we should be fine.

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 |



  reply	other threads:[~2022-07-05 12:00 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-05 11:55 [PATCH v2 1/3] partitions: efi: register guid device parameter for disk GUID Ahmad Fatoum
2022-07-05 11:55 ` [PATCH v2 2/3] commands: implement devlookup to find device behind device file Ahmad Fatoum
2022-07-05 11:59   ` Sascha Hauer [this message]
2022-07-05 12:04     ` Ahmad Fatoum
2022-07-05 13:26       ` Sascha Hauer
2022-07-05 11:55 ` [PATCH v2 3/3] block: efi: allow disabling /dev/usbdiskX renaming Ahmad Fatoum

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220705115923.GG5208@pengutronix.de \
    --to=sha@pengutronix.de \
    --cc=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox