public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
* [PATCH] cmd: ubi: Add 'ubi list' command for printing list of all UBI volumes
@ 2022-09-12  9:38 Pali Rohár
  2022-10-09 11:42 ` Pali Rohár
  2022-10-12 19:14 ` Tom Rini
  0 siblings, 2 replies; 3+ messages in thread
From: Pali Rohár @ 2022-09-12  9:38 UTC (permalink / raw)
  To: u-boot

To allow easily iterate over all UBI volumes, add a new command which
either print all user UBI volumes on output or set them into env variable.

As UBI volumes can have arbitrary name/label, in most cases it is useful to
iterate them by their numbers. This can be achieved by -numeric flag.

This functionality is similar to already existing 'part list' command which
prints partitions on formatted block device.

Signed-off-by: Pali Rohár <pali@kernel.org>
---
 cmd/ubi.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 85 insertions(+)

diff --git a/cmd/ubi.c b/cmd/ubi.c
index fe8ac58bac08..2a32575a257c 100644
--- a/cmd/ubi.c
+++ b/cmd/ubi.c
@@ -27,6 +27,7 @@
 #include <ubi_uboot.h>
 #include <linux/errno.h>
 #include <jffs2/load_kernel.h>
+#include <linux/log2.h>
 
 #undef ubi_msg
 #define ubi_msg(fmt, ...) printf("UBI: " fmt "\n", ##__VA_ARGS__)
@@ -84,6 +85,70 @@ static int ubi_info(int layout)
 	return 0;
 }
 
+static int ubi_list(const char *var, int numeric)
+{
+	size_t namelen, len, size;
+	char *str, *str2;
+	int i;
+
+	if (!var) {
+		for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
+			if (!ubi->volumes[i])
+				continue;
+			if (ubi->volumes[i]->vol_id >= UBI_INTERNAL_VOL_START)
+				continue;
+			printf("%d: %s\n",
+			       ubi->volumes[i]->vol_id,
+			       ubi->volumes[i]->name);
+		}
+		return 0;
+	}
+
+	len = 0;
+	size = 16;
+	str = malloc(size);
+	if (!str)
+		return 1;
+
+	for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
+		if (!ubi->volumes[i])
+			continue;
+		if (ubi->volumes[i]->vol_id >= UBI_INTERNAL_VOL_START)
+			continue;
+
+		if (numeric)
+			namelen = 10; /* strlen(stringify(INT_MAX)) */
+		else
+			namelen = strlen(ubi->volumes[i]->name);
+
+		if (len + namelen + 1 > size) {
+			size = roundup_pow_of_two(len + namelen + 1) * 2;
+			str2 = realloc(str, size);
+			if (!str2) {
+				free(str);
+				return 1;
+			}
+			str = str2;
+		}
+
+		if (len)
+			str[len++] = ' ';
+
+		if (numeric) {
+			len += sprintf(str + len, "%d", ubi->volumes[i]->vol_id) + 1;
+		} else {
+			memcpy(str + len, ubi->volumes[i]->name, namelen);
+			len += namelen;
+			str[len] = 0;
+		}
+	}
+
+	env_set(var, str);
+	free(str);
+
+	return 0;
+}
+
 static int ubi_check_volumename(const struct ubi_volume *vol, char *name)
 {
 	return strcmp(vol->name, name);
@@ -581,6 +646,21 @@ static int do_ubi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 		return ubi_info(layout);
 	}
 
+	if (strcmp(argv[1], "list") == 0) {
+		int numeric = 0;
+		if (argc >= 2 && argv[2][0] == '-') {
+			if (strcmp(argv[2], "-numeric") == 0)
+				numeric = 1;
+			else
+				return CMD_RET_USAGE;
+		}
+		if (!numeric && argc != 2 && argc != 3)
+			return CMD_RET_USAGE;
+		if (numeric && argc != 3 && argc != 4)
+			return CMD_RET_USAGE;
+		return ubi_list(argv[numeric ? 3 : 2], numeric);
+	}
+
 	if (strcmp(argv[1], "check") == 0) {
 		if (argc > 2)
 			return ubi_check(argv[2]);
@@ -720,6 +800,11 @@ U_BOOT_CMD(
 		" header offset)\n"
 	"ubi info [l[ayout]]"
 		" - Display volume and ubi layout information\n"
+	"ubi list [flags]"
+		" - print the list of volumes\n"
+	"ubi list [flags] <varname>"
+		" - set environment variable to the list of volumes"
+		" (flags can be -numeric)\n"
 	"ubi check volumename"
 		" - check if volumename exists\n"
 	"ubi create[vol] volume [size] [type] [id] [--skipcheck]\n"
-- 
2.20.1


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

* Re: [PATCH] cmd: ubi: Add 'ubi list' command for printing list of all UBI volumes
  2022-09-12  9:38 [PATCH] cmd: ubi: Add 'ubi list' command for printing list of all UBI volumes Pali Rohár
@ 2022-10-09 11:42 ` Pali Rohár
  2022-10-12 19:14 ` Tom Rini
  1 sibling, 0 replies; 3+ messages in thread
From: Pali Rohár @ 2022-10-09 11:42 UTC (permalink / raw)
  To: u-boot

PING?

On Monday 12 September 2022 11:38:55 Pali Rohár wrote:
> To allow easily iterate over all UBI volumes, add a new command which
> either print all user UBI volumes on output or set them into env variable.
> 
> As UBI volumes can have arbitrary name/label, in most cases it is useful to
> iterate them by their numbers. This can be achieved by -numeric flag.
> 
> This functionality is similar to already existing 'part list' command which
> prints partitions on formatted block device.
> 
> Signed-off-by: Pali Rohár <pali@kernel.org>
> ---
>  cmd/ubi.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 85 insertions(+)
> 
> diff --git a/cmd/ubi.c b/cmd/ubi.c
> index fe8ac58bac08..2a32575a257c 100644
> --- a/cmd/ubi.c
> +++ b/cmd/ubi.c
> @@ -27,6 +27,7 @@
>  #include <ubi_uboot.h>
>  #include <linux/errno.h>
>  #include <jffs2/load_kernel.h>
> +#include <linux/log2.h>
>  
>  #undef ubi_msg
>  #define ubi_msg(fmt, ...) printf("UBI: " fmt "\n", ##__VA_ARGS__)
> @@ -84,6 +85,70 @@ static int ubi_info(int layout)
>  	return 0;
>  }
>  
> +static int ubi_list(const char *var, int numeric)
> +{
> +	size_t namelen, len, size;
> +	char *str, *str2;
> +	int i;
> +
> +	if (!var) {
> +		for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
> +			if (!ubi->volumes[i])
> +				continue;
> +			if (ubi->volumes[i]->vol_id >= UBI_INTERNAL_VOL_START)
> +				continue;
> +			printf("%d: %s\n",
> +			       ubi->volumes[i]->vol_id,
> +			       ubi->volumes[i]->name);
> +		}
> +		return 0;
> +	}
> +
> +	len = 0;
> +	size = 16;
> +	str = malloc(size);
> +	if (!str)
> +		return 1;
> +
> +	for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
> +		if (!ubi->volumes[i])
> +			continue;
> +		if (ubi->volumes[i]->vol_id >= UBI_INTERNAL_VOL_START)
> +			continue;
> +
> +		if (numeric)
> +			namelen = 10; /* strlen(stringify(INT_MAX)) */
> +		else
> +			namelen = strlen(ubi->volumes[i]->name);
> +
> +		if (len + namelen + 1 > size) {
> +			size = roundup_pow_of_two(len + namelen + 1) * 2;
> +			str2 = realloc(str, size);
> +			if (!str2) {
> +				free(str);
> +				return 1;
> +			}
> +			str = str2;
> +		}
> +
> +		if (len)
> +			str[len++] = ' ';
> +
> +		if (numeric) {
> +			len += sprintf(str + len, "%d", ubi->volumes[i]->vol_id) + 1;
> +		} else {
> +			memcpy(str + len, ubi->volumes[i]->name, namelen);
> +			len += namelen;
> +			str[len] = 0;
> +		}
> +	}
> +
> +	env_set(var, str);
> +	free(str);
> +
> +	return 0;
> +}
> +
>  static int ubi_check_volumename(const struct ubi_volume *vol, char *name)
>  {
>  	return strcmp(vol->name, name);
> @@ -581,6 +646,21 @@ static int do_ubi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
>  		return ubi_info(layout);
>  	}
>  
> +	if (strcmp(argv[1], "list") == 0) {
> +		int numeric = 0;
> +		if (argc >= 2 && argv[2][0] == '-') {
> +			if (strcmp(argv[2], "-numeric") == 0)
> +				numeric = 1;
> +			else
> +				return CMD_RET_USAGE;
> +		}
> +		if (!numeric && argc != 2 && argc != 3)
> +			return CMD_RET_USAGE;
> +		if (numeric && argc != 3 && argc != 4)
> +			return CMD_RET_USAGE;
> +		return ubi_list(argv[numeric ? 3 : 2], numeric);
> +	}
> +
>  	if (strcmp(argv[1], "check") == 0) {
>  		if (argc > 2)
>  			return ubi_check(argv[2]);
> @@ -720,6 +800,11 @@ U_BOOT_CMD(
>  		" header offset)\n"
>  	"ubi info [l[ayout]]"
>  		" - Display volume and ubi layout information\n"
> +	"ubi list [flags]"
> +		" - print the list of volumes\n"
> +	"ubi list [flags] <varname>"
> +		" - set environment variable to the list of volumes"
> +		" (flags can be -numeric)\n"
>  	"ubi check volumename"
>  		" - check if volumename exists\n"
>  	"ubi create[vol] volume [size] [type] [id] [--skipcheck]\n"
> -- 
> 2.20.1
> 

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

* Re: [PATCH] cmd: ubi: Add 'ubi list' command for printing list of all UBI volumes
  2022-09-12  9:38 [PATCH] cmd: ubi: Add 'ubi list' command for printing list of all UBI volumes Pali Rohár
  2022-10-09 11:42 ` Pali Rohár
@ 2022-10-12 19:14 ` Tom Rini
  1 sibling, 0 replies; 3+ messages in thread
From: Tom Rini @ 2022-10-12 19:14 UTC (permalink / raw)
  To: Pali Rohár; +Cc: u-boot

[-- Attachment #1: Type: text/plain, Size: 601 bytes --]

On Mon, Sep 12, 2022 at 11:38:55AM +0200, Pali Rohár wrote:

> To allow easily iterate over all UBI volumes, add a new command which
> either print all user UBI volumes on output or set them into env variable.
> 
> As UBI volumes can have arbitrary name/label, in most cases it is useful to
> iterate them by their numbers. This can be achieved by -numeric flag.
> 
> This functionality is similar to already existing 'part list' command which
> prints partitions on formatted block device.
> 
> Signed-off-by: Pali Rohár <pali@kernel.org>

Applied to u-boot/master, thanks!

-- 
Tom

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]

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

end of thread, other threads:[~2022-10-12 19:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-09-12  9:38 [PATCH] cmd: ubi: Add 'ubi list' command for printing list of all UBI volumes Pali Rohár
2022-10-09 11:42 ` Pali Rohár
2022-10-12 19:14 ` Tom Rini

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