diff mbox series

[1/1] cmd: blkls: Add blkls command

Message ID 20200325134624.2014092-1-lusus@denx.de
State Superseded
Delegated to: Tom Rini
Headers show
Series [1/1] cmd: blkls: Add blkls command | expand

Commit Message

Niel Fourie March 25, 2020, 1:46 p.m. UTC
Add a command to print a list of available block device drivers,
and for each, the list of known block devices.

Signed-off-by: Niel Fourie <lusus@denx.de>
---
 cmd/Kconfig                 |  8 ++++
 cmd/Makefile                |  1 +
 cmd/lsblk.c                 | 83 +++++++++++++++++++++++++++++++++++++
 test/py/tests/test_lsblk.py | 23 ++++++++++
 4 files changed, 115 insertions(+)
 create mode 100644 cmd/lsblk.c
 create mode 100644 test/py/tests/test_lsblk.py

Comments

Stefan Roese March 25, 2020, 2:13 p.m. UTC | #1
Hi Niel,

On 25.03.20 14:46, Niel Fourie wrote:
> Add a command to print a list of available block device drivers,
> and for each, the list of known block devices.
> 
> Signed-off-by: Niel Fourie <lusus@denx.de>
> ---
>   cmd/Kconfig                 |  8 ++++
>   cmd/Makefile                |  1 +
>   cmd/lsblk.c                 | 83 +++++++++++++++++++++++++++++++++++++
>   test/py/tests/test_lsblk.py | 23 ++++++++++
>   4 files changed, 115 insertions(+)
>   create mode 100644 cmd/lsblk.c
>   create mode 100644 test/py/tests/test_lsblk.py
> 
> diff --git a/cmd/Kconfig b/cmd/Kconfig
> index 6403bc45a5..ee6ff467ae 100644
> --- a/cmd/Kconfig
> +++ b/cmd/Kconfig
> @@ -1047,6 +1047,14 @@ config CMD_LOADS
>   	help
>   	  Load an S-Record file over serial line
>   
> +config CMD_LSBLK
> +	depends on HAVE_BLOCK_DEVICE

You depend on CONFIG_HAVE_BLOCK_DEVICE here...

> +	bool "lsblk - list block drivers and devices"
> +	default n
> +	help
> +	  Print list of available block device drivers, and for each, the list
> +	  of known block devices.
> +
>   config CMD_MMC
>   	bool "mmc"
>   	help
> diff --git a/cmd/Makefile b/cmd/Makefile
> index f1dd513a4b..6f80974a55 100644
> --- a/cmd/Makefile
> +++ b/cmd/Makefile
> @@ -83,6 +83,7 @@ obj-$(CONFIG_CMD_LED) += led.o
>   obj-$(CONFIG_CMD_LICENSE) += license.o
>   obj-y += load.o
>   obj-$(CONFIG_CMD_LOG) += log.o
> +obj-$(CONFIG_CMD_LSBLK) += lsblk.o
>   obj-$(CONFIG_ID_EEPROM) += mac.o
>   obj-$(CONFIG_CMD_MD5SUM) += md5sum.o
>   obj-$(CONFIG_CMD_MEMORY) += mem.o
> diff --git a/cmd/lsblk.c b/cmd/lsblk.c
> new file mode 100644
> index 0000000000..575a1c9277
> --- /dev/null
> +++ b/cmd/lsblk.c
> @@ -0,0 +1,83 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * (C) Copyright 2020
> + * Niel Fourie, DENX Software Engineering, lusus@denx.de.
> + */
> +
> +#include <config.h>
> +#include <common.h>
> +#include <dm/uclass.h>
> +#include <dm/device.h>
> +
> +#if CONFIG_IS_ENABLED(HAVE_BLOCK_DEVICE)

... so this #ifdef should not be necessary, right?

Thanks,
Stefan

> +#if CONFIG_IS_ENABLED(BLK)
> +static int do_lsblk(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
> +{
> +	struct driver *d = ll_entry_start(struct driver, driver);
> +	const int n_ents = ll_entry_count(struct driver, driver);
> +	struct driver *entry;
> +	struct udevice *udev;
> +	struct uclass *uc;
> +	struct blk_desc *desc;
> +	int i;
> +
> +	puts("Block Driver          Devices\n");
> +	puts("-----------------------------\n");
> +	uclass_get(UCLASS_BLK, &uc);
> +	for (entry = d; entry < d + n_ents; entry++) {
> +		if (entry->id != UCLASS_BLK)
> +			continue;
> +		i = 0;
> +		printf("%-20.20s", entry->name);
> +		uclass_foreach_dev(udev, uc) {
> +			if (udev->driver != entry)
> +				continue;
> +			desc = dev_get_uclass_platdata(udev);
> +			printf("%c %s %u", i ? ',' : ':',
> +			       blk_get_if_type_name(desc->if_type),
> +			       desc->devnum);
> +			i++;
> +		}
> +		if (!i)
> +			puts(": <none>");
> +		puts("\n");
> +	}
> +	return CMD_RET_SUCCESS;
> +}
> +#else
> +static int do_lsblk(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
> +{
> +	struct blk_driver *drv = ll_entry_start(struct blk_driver, blk_driver);
> +	const int n_ents = ll_entry_count(struct blk_driver, blk_driver);
> +	struct blk_driver *entry;
> +	struct blk_desc *desc;
> +	int i, j;
> +
> +	puts("Block Driver          Devices\n");
> +	puts("-----------------------------\n");
> +	for (entry = drv; entry != drv + n_ents; entry++) {
> +		i = 0;
> +		printf("%-20.20s", entry->if_typename);
> +		for (j = 0; j < entry->max_devs; j++) {
> +			desc = &entry->desc[j];
> +			if (!entry->get_dev)
> +				continue;
> +			if (entry->get_dev(j, &desc))
> +				continue;
> +			if (desc->type == DEV_TYPE_UNKNOWN)
> +				continue;
> +			printf("%c %s %u", i ? ',' : ':', entry->if_typename,
> +			       desc->devnum);
> +			i++;
> +		}
> +		if (!i)
> +			puts(": <none>");
> +		puts("\n");
> +	}
> +	return CMD_RET_SUCCESS;
> +}
> +#endif /* CONFIG_IS_ENABLED(BLK) */
> +U_BOOT_CMD(lsblk, 1, 0, do_lsblk, "list block drivers and devices",
> +	   "- display list of block device drivers and attached block devices"
> +);
> +#endif /* CONFIG_IS_ENABLED(HAVE_BLOCK_DEVICE) */
> diff --git a/test/py/tests/test_lsblk.py b/test/py/tests/test_lsblk.py
> new file mode 100644
> index 0000000000..f4b36b581e
> --- /dev/null
> +++ b/test/py/tests/test_lsblk.py
> @@ -0,0 +1,23 @@
> +# SPDX-License-Identifier: GPL-2.0
> +# Copyright (C) 2020
> +# Niel Fourie, DENX Software Engineering, lusus@denx.de
> +
> +import pytest
> +
> +@pytest.mark.buildconfigspec('have_block_device')
> +@pytest.mark.buildconfigspec('blk')
> +@pytest.mark.buildconfigspec('cmd_lsblk')
> +def test_lsblk(u_boot_console):
> +    """Test that `lsblk` prints a result which includes `host`."""
> +    output = u_boot_console.run_command('lsblk')
> +    assert "Block Driver" in output
> +    assert "sandbox_host_blk" in output
> +
> +@pytest.mark.buildconfigspec('have_block_device')
> +@pytest.mark.notbuildconfigspec('blk')
> +@pytest.mark.buildconfigspec('cmd_lsblk')
> +def test_lsblk_legacy(u_boot_console):
> +    """Test that `lsblk` prints a result which includes `host`."""
> +    output = u_boot_console.run_command('lsblk')
> +    assert "Block Driver" in output
> +    assert "host" in output
>
Niel Fourie March 25, 2020, 2:30 p.m. UTC | #2
Hi Stefan,

On 3/25/20 3:13 PM, Stefan Roese wrote:
>> +config CMD_LSBLK
>> +    depends on HAVE_BLOCK_DEVICE
> 
> You depend on CONFIG_HAVE_BLOCK_DEVICE here...
> 
>> +
>> +#if CONFIG_IS_ENABLED(HAVE_BLOCK_DEVICE)
> 
> ... so this #ifdef should not be necessary, right?

Oops, you are right... I forgot to take out the #ifdef after adding the 
Kconfig dependency. Will fix. Well spotted, thanks!

Best regards,
Niel Fourie
Simon Glass March 26, 2020, 4:20 p.m. UTC | #3
Hi Niel,

On Wed, 25 Mar 2020 at 07:46, Niel Fourie <lusus@denx.de> wrote:
>
> Add a command to print a list of available block device drivers,
> and for each, the list of known block devices.
>
> Signed-off-by: Niel Fourie <lusus@denx.de>
> ---
>  cmd/Kconfig                 |  8 ++++
>  cmd/Makefile                |  1 +
>  cmd/lsblk.c                 | 83 +++++++++++++++++++++++++++++++++++++
>  test/py/tests/test_lsblk.py | 23 ++++++++++
>  4 files changed, 115 insertions(+)
>  create mode 100644 cmd/lsblk.c
>  create mode 100644 test/py/tests/test_lsblk.py
>
> diff --git a/cmd/Kconfig b/cmd/Kconfig
> index 6403bc45a5..ee6ff467ae 100644
> --- a/cmd/Kconfig
> +++ b/cmd/Kconfig
> @@ -1047,6 +1047,14 @@ config CMD_LOADS
>         help
>           Load an S-Record file over serial line
>
> +config CMD_LSBLK
> +       depends on HAVE_BLOCK_DEVICE

depends on BLK

(the migration date has passed so we MUST NOT add new features that
don't use BLK)

> +       bool "lsblk - list block drivers and devices"
> +       default n
> +       help
> +         Print list of available block device drivers, and for each, the list
> +         of known block devices.
> +
>  config CMD_MMC
>         bool "mmc"
>         help
> diff --git a/cmd/Makefile b/cmd/Makefile
> index f1dd513a4b..6f80974a55 100644
> --- a/cmd/Makefile
> +++ b/cmd/Makefile
> @@ -83,6 +83,7 @@ obj-$(CONFIG_CMD_LED) += led.o
>  obj-$(CONFIG_CMD_LICENSE) += license.o
>  obj-y += load.o
>  obj-$(CONFIG_CMD_LOG) += log.o
> +obj-$(CONFIG_CMD_LSBLK) += lsblk.o
>  obj-$(CONFIG_ID_EEPROM) += mac.o
>  obj-$(CONFIG_CMD_MD5SUM) += md5sum.o
>  obj-$(CONFIG_CMD_MEMORY) += mem.o
> diff --git a/cmd/lsblk.c b/cmd/lsblk.c
> new file mode 100644
> index 0000000000..575a1c9277
> --- /dev/null
> +++ b/cmd/lsblk.c
> @@ -0,0 +1,83 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * (C) Copyright 2020
> + * Niel Fourie, DENX Software Engineering, lusus@denx.de.
> + */
> +
> +#include <config.h>
> +#include <common.h>
> +#include <dm/uclass.h>
> +#include <dm/device.h>
> +
> +#if CONFIG_IS_ENABLED(HAVE_BLOCK_DEVICE)
> +#if CONFIG_IS_ENABLED(BLK)

drop these I think

> +static int do_lsblk(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
> +{
> +       struct driver *d = ll_entry_start(struct driver, driver);
> +       const int n_ents = ll_entry_count(struct driver, driver);
> +       struct driver *entry;
> +       struct udevice *udev;
> +       struct uclass *uc;
> +       struct blk_desc *desc;
> +       int i;
> +
> +       puts("Block Driver          Devices\n");
> +       puts("-----------------------------\n");
> +       uclass_get(UCLASS_BLK, &uc);

Check return value

> +       for (entry = d; entry < d + n_ents; entry++) {
> +               if (entry->id != UCLASS_BLK)
> +                       continue;
> +               i = 0;
> +               printf("%-20.20s", entry->name);
> +               uclass_foreach_dev(udev, uc) {
> +                       if (udev->driver != entry)
> +                               continue;
> +                       desc = dev_get_uclass_platdata(udev);
> +                       printf("%c %s %u", i ? ',' : ':',
> +                              blk_get_if_type_name(desc->if_type),
> +                              desc->devnum);
> +                       i++;
> +               }
> +               if (!i)
> +                       puts(": <none>");
> +               puts("\n");
> +       }
> +       return CMD_RET_SUCCESS;
> +}
> +#else
> +static int do_lsblk(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])

Drop this function

> +{
> +       struct blk_driver *drv = ll_entry_start(struct blk_driver, blk_driver);
> +       const int n_ents = ll_entry_count(struct blk_driver, blk_driver);
> +       struct blk_driver *entry;
> +       struct blk_desc *desc;
> +       int i, j;
> +
> +       puts("Block Driver          Devices\n");
> +       puts("-----------------------------\n");
> +       for (entry = drv; entry != drv + n_ents; entry++) {
> +               i = 0;
> +               printf("%-20.20s", entry->if_typename);
> +               for (j = 0; j < entry->max_devs; j++) {
> +                       desc = &entry->desc[j];
> +                       if (!entry->get_dev)
> +                               continue;
> +                       if (entry->get_dev(j, &desc))
> +                               continue;
> +                       if (desc->type == DEV_TYPE_UNKNOWN)
> +                               continue;
> +                       printf("%c %s %u", i ? ',' : ':', entry->if_typename,
> +                              desc->devnum);
> +                       i++;
> +               }
> +               if (!i)
> +                       puts(": <none>");
> +               puts("\n");
> +       }
> +       return CMD_RET_SUCCESS;
> +}
> +#endif /* CONFIG_IS_ENABLED(BLK) */
> +U_BOOT_CMD(lsblk, 1, 0, do_lsblk, "list block drivers and devices",
> +          "- display list of block device drivers and attached block devices"
> +);
> +#endif /* CONFIG_IS_ENABLED(HAVE_BLOCK_DEVICE) */
> diff --git a/test/py/tests/test_lsblk.py b/test/py/tests/test_lsblk.py
> new file mode 100644
> index 0000000000..f4b36b581e
> --- /dev/null
> +++ b/test/py/tests/test_lsblk.py
> @@ -0,0 +1,23 @@
> +# SPDX-License-Identifier: GPL-2.0
> +# Copyright (C) 2020
> +# Niel Fourie, DENX Software Engineering, lusus@denx.de
> +
> +import pytest
> +
> +@pytest.mark.buildconfigspec('have_block_device')
> +@pytest.mark.buildconfigspec('blk')
> +@pytest.mark.buildconfigspec('cmd_lsblk')
> +def test_lsblk(u_boot_console):
> +    """Test that `lsblk` prints a result which includes `host`."""
> +    output = u_boot_console.run_command('lsblk')
> +    assert "Block Driver" in output
> +    assert "sandbox_host_blk" in output
> +
> +@pytest.mark.buildconfigspec('have_block_device')
> +@pytest.mark.notbuildconfigspec('blk')
> +@pytest.mark.buildconfigspec('cmd_lsblk')
> +def test_lsblk_legacy(u_boot_console):
> +    """Test that `lsblk` prints a result which includes `host`."""
> +    output = u_boot_console.run_command('lsblk')
> +    assert "Block Driver" in output
> +    assert "host" in output

Drop this test

> --
> 2.25.1
>

Regards,
Simon
diff mbox series

Patch

diff --git a/cmd/Kconfig b/cmd/Kconfig
index 6403bc45a5..ee6ff467ae 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -1047,6 +1047,14 @@  config CMD_LOADS
 	help
 	  Load an S-Record file over serial line
 
+config CMD_LSBLK
+	depends on HAVE_BLOCK_DEVICE
+	bool "lsblk - list block drivers and devices"
+	default n
+	help
+	  Print list of available block device drivers, and for each, the list
+	  of known block devices.
+
 config CMD_MMC
 	bool "mmc"
 	help
diff --git a/cmd/Makefile b/cmd/Makefile
index f1dd513a4b..6f80974a55 100644
--- a/cmd/Makefile
+++ b/cmd/Makefile
@@ -83,6 +83,7 @@  obj-$(CONFIG_CMD_LED) += led.o
 obj-$(CONFIG_CMD_LICENSE) += license.o
 obj-y += load.o
 obj-$(CONFIG_CMD_LOG) += log.o
+obj-$(CONFIG_CMD_LSBLK) += lsblk.o
 obj-$(CONFIG_ID_EEPROM) += mac.o
 obj-$(CONFIG_CMD_MD5SUM) += md5sum.o
 obj-$(CONFIG_CMD_MEMORY) += mem.o
diff --git a/cmd/lsblk.c b/cmd/lsblk.c
new file mode 100644
index 0000000000..575a1c9277
--- /dev/null
+++ b/cmd/lsblk.c
@@ -0,0 +1,83 @@ 
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * (C) Copyright 2020
+ * Niel Fourie, DENX Software Engineering, lusus@denx.de.
+ */
+
+#include <config.h>
+#include <common.h>
+#include <dm/uclass.h>
+#include <dm/device.h>
+
+#if CONFIG_IS_ENABLED(HAVE_BLOCK_DEVICE)
+#if CONFIG_IS_ENABLED(BLK)
+static int do_lsblk(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+	struct driver *d = ll_entry_start(struct driver, driver);
+	const int n_ents = ll_entry_count(struct driver, driver);
+	struct driver *entry;
+	struct udevice *udev;
+	struct uclass *uc;
+	struct blk_desc *desc;
+	int i;
+
+	puts("Block Driver          Devices\n");
+	puts("-----------------------------\n");
+	uclass_get(UCLASS_BLK, &uc);
+	for (entry = d; entry < d + n_ents; entry++) {
+		if (entry->id != UCLASS_BLK)
+			continue;
+		i = 0;
+		printf("%-20.20s", entry->name);
+		uclass_foreach_dev(udev, uc) {
+			if (udev->driver != entry)
+				continue;
+			desc = dev_get_uclass_platdata(udev);
+			printf("%c %s %u", i ? ',' : ':',
+			       blk_get_if_type_name(desc->if_type),
+			       desc->devnum);
+			i++;
+		}
+		if (!i)
+			puts(": <none>");
+		puts("\n");
+	}
+	return CMD_RET_SUCCESS;
+}
+#else
+static int do_lsblk(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+	struct blk_driver *drv = ll_entry_start(struct blk_driver, blk_driver);
+	const int n_ents = ll_entry_count(struct blk_driver, blk_driver);
+	struct blk_driver *entry;
+	struct blk_desc *desc;
+	int i, j;
+
+	puts("Block Driver          Devices\n");
+	puts("-----------------------------\n");
+	for (entry = drv; entry != drv + n_ents; entry++) {
+		i = 0;
+		printf("%-20.20s", entry->if_typename);
+		for (j = 0; j < entry->max_devs; j++) {
+			desc = &entry->desc[j];
+			if (!entry->get_dev)
+				continue;
+			if (entry->get_dev(j, &desc))
+				continue;
+			if (desc->type == DEV_TYPE_UNKNOWN)
+				continue;
+			printf("%c %s %u", i ? ',' : ':', entry->if_typename,
+			       desc->devnum);
+			i++;
+		}
+		if (!i)
+			puts(": <none>");
+		puts("\n");
+	}
+	return CMD_RET_SUCCESS;
+}
+#endif /* CONFIG_IS_ENABLED(BLK) */
+U_BOOT_CMD(lsblk, 1, 0, do_lsblk, "list block drivers and devices",
+	   "- display list of block device drivers and attached block devices"
+);
+#endif /* CONFIG_IS_ENABLED(HAVE_BLOCK_DEVICE) */
diff --git a/test/py/tests/test_lsblk.py b/test/py/tests/test_lsblk.py
new file mode 100644
index 0000000000..f4b36b581e
--- /dev/null
+++ b/test/py/tests/test_lsblk.py
@@ -0,0 +1,23 @@ 
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2020
+# Niel Fourie, DENX Software Engineering, lusus@denx.de
+
+import pytest
+
+@pytest.mark.buildconfigspec('have_block_device')
+@pytest.mark.buildconfigspec('blk')
+@pytest.mark.buildconfigspec('cmd_lsblk')
+def test_lsblk(u_boot_console):
+    """Test that `lsblk` prints a result which includes `host`."""
+    output = u_boot_console.run_command('lsblk')
+    assert "Block Driver" in output
+    assert "sandbox_host_blk" in output
+
+@pytest.mark.buildconfigspec('have_block_device')
+@pytest.mark.notbuildconfigspec('blk')
+@pytest.mark.buildconfigspec('cmd_lsblk')
+def test_lsblk_legacy(u_boot_console):
+    """Test that `lsblk` prints a result which includes `host`."""
+    output = u_boot_console.run_command('lsblk')
+    assert "Block Driver" in output
+    assert "host" in output