Skip to contentSkip to conversation

Add support for ubi environment to auto create volumes

PATCH Superseded Weijie Gao 2 participants 12 messages Apr 17 – Apr 20

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Weijie Gao

author
#0 lore raw

cover Add support for ubi environment to auto create volumes

This patch series expose more ubi helpers to allow the ubi environment to create ubi env volumes if not exist.

Weijie Gao (3):
cmd: ubi: expose more APIs to public
cmd: ubi: allow creating volume with all free spaces in ubi_create_vol
env: ubi: add support to create environment volume if it does not
exist

 cmd/ubi.c           | 16 ++++++++++------
 env/Kconfig         |  6 ++++++
 env/ubi.c           | 20 ++++++++++++++++++++
 include/ubi_uboot.h |  5 +++++
 4 files changed, 41 insertions(+), 6 deletions(-)
Signature · 1 line
2.45.2
author
#1 lore raw

PATCH 1/3 cmd: ubi: expose more APIs to public

Export ubi_detach/ubi_create_vol/ubi_find_volume/ubi_remove_vol to public for better ubi manipulation used by other modules.

---
 cmd/ubi.c           | 10 +++++-----
 include/ubi_uboot.h |  5 +++++
 2 files changed, 10 insertions(+), 5 deletions(-)
cmd/ubi.c +5 -5
index 93de6f3aea2..e7fd7165457 100644
@@ -213,8 +213,8 @@ bad:
 	return err;
 }
 
-static int ubi_create_vol(char *volume, int64_t size, int dynamic, int vol_id,
-			  bool skipcheck)
+int ubi_create_vol(char *volume, int64_t size, int dynamic, int vol_id,
+		   bool skipcheck)
 {
 	struct ubi_mkvol_req req;
 	int err;
@@ -247,7 +247,7 @@ static int ubi_create_vol(char *volume, int64_t size, int dynamic, int vol_id,
 	return ubi_create_volume(ubi, &req);
 }
 
-static struct ubi_volume *ubi_find_volume(char *volume)
+struct ubi_volume *ubi_find_volume(char *volume)
 {
 	struct ubi_volume *vol;
 	int i;
@@ -262,7 +262,7 @@ static struct ubi_volume *ubi_find_volume(char *volume)
 	return NULL;
 }
 
-static int ubi_remove_vol(char *volume)
+int ubi_remove_vol(char *volume)
 {
 	int err, reserved_pebs, i;
 	struct ubi_volume *vol;
@@ -635,7 +635,7 @@ static int ubi_set_skip_check(char *volume, bool skip_check)
 	return ubi_change_vtbl_record(ubi, vol->vol_id, &vtbl_rec);
 }
 
-static int ubi_detach(void)
+int ubi_detach(void)
 {
 #ifdef CONFIG_CMD_UBIFS
 	/*
include/ubi_uboot.h +5 -0
index ea0db69c72a..36b5b69dbfd 100644
@@ -47,9 +47,14 @@
 extern int ubi_mtd_param_parse(const char *val, struct kernel_param *kp);
 extern int ubi_init(void);
 extern void ubi_exit(void);
+extern int ubi_detach(void);
 extern int ubi_part(char *part_name, const char *vid_header_offset);
 extern int ubi_volume_write(char *volume, void *buf, loff_t offset, size_t size);
 extern int ubi_volume_read(char *volume, char *buf, loff_t offset, size_t size);
+extern int ubi_create_vol(char *volume, int64_t size, int dynamic, int vol_id,
+			  bool skipcheck);
+extern struct ubi_volume *ubi_find_volume(char *volume);
+extern int ubi_remove_vol(char *volume);
 
 extern struct ubi_device *ubi_devices[];
 int cmd_ubifs_mount(char *vol_name);
Signature · 1 line
2.45.2
author
#5 lore raw

PATCH 2/3 cmd: ubi: allow creating volume with all free spaces in ubi_create_vol

In reply to Weijie Gao

Although the ubi command itself supports creating volume with all free spaces, the api ubi_create_vol() does not.

Since negative size in invalid, this patch replaces negative size with all free space size in ubi_create_vol().

---
 cmd/ubi.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
cmd/ubi.c +5 -1
index e7fd7165457..3a1d47d1942 100644
@@ -226,7 +226,11 @@ int ubi_create_vol(char *volume, int64_t size, int dynamic, int vol_id,
 
 	req.vol_id = vol_id;
 	req.alignment = 1;
-	req.bytes = size;
+
+	if (size < 0)
+		req.bytes = ubi->avail_pebs * ubi->leb_size;
+	else
+		req.bytes = size;
 
 	strcpy(req.name, volume);
 	req.name_len = strlen(volume);
Signature · 1 line
2.45.2
author
#9 lore raw

PATCH 3/3 env: ubi: add support to create environment volume if it does not exist

In reply to Weijie Gao

Add an option to allow environment volume being auto created if not exist.

---
 env/Kconfig |  6 ++++++
 env/ubi.c   | 20 ++++++++++++++++++++
 2 files changed, 26 insertions(+)
env/Kconfig +6 -0
index 7abd82ab6f3..cec1bf0bb11 100644
@@ -716,6 +716,12 @@ config ENV_UBI_VOLUME_REDUND
 	help
 	  Name of the redundant volume that you want to store the environment in.
 
+config ENV_UBI_VOLUME_CREATE
+	bool "Create UBI volume if not exist"
+	depends on ENV_IS_IN_UBI
+	help
+	  Create the UBI volume if it does not exist.
+
 config ENV_UBI_VID_OFFSET
 	int "ubi environment VID offset"
 	depends on ENV_IS_IN_UBI
env/ubi.c +20 -0
index e9865b45ebc..bbd86adf176 100644
@@ -103,6 +103,18 @@ static int env_ubi_save(void)
 }
 #endif /* CONFIG_ENV_REDUNDANT */
 
+int __weak env_ubi_volume_create(const char *volume)
+{
+	struct ubi_volume *vol;
+
+	vol = ubi_find_volume((char *)volume);
+	if (vol)
+		return 0;
+
+	return ubi_create_vol((char *)volume, CONFIG_ENV_SIZE, true,
+			      UBI_VOL_NUM_AUTO, false);
+}
+
 #ifdef CONFIG_ENV_REDUNDANT
 static int env_ubi_load(void)
 {
@@ -132,6 +144,11 @@ static int env_ubi_load(void)
 		return -EIO;
 	}
 
+	if (IS_ENABLED(CONFIG_ENV_UBI_VOLUME_CREATE)) {
+		env_ubi_volume_create(CONFIG_ENV_UBI_VOLUME);
+		env_ubi_volume_create(CONFIG_ENV_UBI_VOLUME_REDUND);
+	}
+
 	read1_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1, 0,
 				     CONFIG_ENV_SIZE);
 	if (read1_fail)
@@ -169,6 +186,9 @@ static int env_ubi_load(void)
 		return -EIO;
 	}
 
+	if (IS_ENABLED(CONFIG_ENV_UBI_VOLUME_CREATE))
+		env_ubi_volume_create(CONFIG_ENV_UBI_VOLUME);
+
 	if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, buf, 0, CONFIG_ENV_SIZE)) {
 		printf("\n** Unable to read env from %s:%s **\n",
 		       CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
Signature · 1 line
2.45.2
#2 lore raw

Re: [PATCH 1/3] cmd: ubi: expose more APIs to public

In reply to Weijie Gao

Hi Weijie,

Quoted 13 lines · Weijie Gao

cmd: ubi: expose more APIs to public

Export ubi_detach/ubi_create_vol/ubi_find_volume/ubi_remove_vol to public for better ubi manipulation used by other modules.

cmd/ubi.c | 10 +++++-----

 include/ubi_uboot.h |  5 +++++
 2 files changed, 10 insertions(+), 5 deletions(-)

include/ubi_uboot.h line 50

Quoted 4 lines
include/ubi_uboot.h +4 -0
@@ -47,9 +47,14 @@
+extern int ubi_create_vol(char *volume, int64_t size, int dynamic, int vol_id,
+                       bool skipcheck);
+extern struct ubi_volume *ubi_find_volume(char *volume);
+extern int ubi_remove_vol(char *volume);

The volume parameter should be const char * since these functions do not modify the string. In patch 3/3, env_ubi_volume_create() receives a const char * and has to cast away const when calling these functions. Please can you fix the signatures in both cmd/ubi.c and ubi_uboot.h to take const char *volume.

Regards,
Simon

#6 lore raw

Re: [PATCH 2/3] cmd: ubi: allow creating volume with all free spaces in ubi_create_vol

In reply to Weijie Gao

Hi Weijie,

Quoted 6 lines · Weijie Gao

cmd: ubi: allow creating volume with all free spaces in ubi_create_vol

Although the ubi command itself supports creating volume with all free spaces, the api ubi_create_vol() does not.

Since negative size in invalid, this patch replaces negative size

is invalid

cmd/ubi.c int ubi_create_vol(char *volume, int64_t size, int dynamic, int vol_id, · line 229

Quoted 13 lines

with all free space size in ubi_create_vol().

cmd/ubi.c | 6 +++++-

 1 file changed, 5 insertions(+), 1 deletion(-)
cmd/ubi.c +4 -0
@@ -226,7 +226,11 @@ int ubi_create_vol(char *volume, int64_t size, int dynamic, int vol_id,
+     if (size < 0)
+             req.bytes = ubi->avail_pebs * ubi->leb_size;
+     else
+             req.bytes = size;

When size is negative, the printf() below still prints the original negative size rather than the computed req.bytes

Regards,
Simon

#10 lore raw

Re: [PATCH 3/3] env: ubi: add support to create environment volume if it does not exist

In reply to Weijie Gao

Hi Weijie,

Quoted 10 lines · Weijie Gao

env: ubi: add support to create environment volume if it does not exist

Add an option to allow environment volume being auto created if not exist.

env/Kconfig | 6 ++++++

 env/ubi.c   | 20 ++++++++++++++++++++
 2 files changed, 26 insertions(+)

Could you beef up the Kconfig help text for ENV_UBI_VOLUME_CREATE - e.g. mention when users might want to enable this, such as on first boot of a fresh device where the UBI partition exists but no volumes have been created yet.

env/ubi.c static int env_ubi_save(void) · line 109

Quoted 7 lines
env/ubi.c +5 -0
@@ -105,6 +105,18 @@ static int env_ubi_save(void)
+int __weak env_ubi_volume_create(const char *volume)
+{
+     struct ubi_volume *vol;
+
+     vol = ubi_find_volume((char *)volume);

Does this function need to be weak?

ubi_find_volume() prints "Volume %s not found!" when the volume does not exist. Since the whole point of this function is to handle missing volumes, this message will appear every time auto-creation is used. Please can you either add a quiet variant of ubi_find_volume(), or suppress the message here?

env/ubi.c static int env_ubi_load(void) · line 149

Quoted 6 lines
env/ubi.c +4 -0
@@ -134,6 +146,11 @@ static int env_ubi_load(void)
+     if (IS_ENABLED(CONFIG_ENV_UBI_VOLUME_CREATE)) {
+             env_ubi_volume_create(CONFIG_ENV_UBI_VOLUME);
+             env_ubi_volume_create(CONFIG_ENV_UBI_VOLUME_REDUND);
+     }

The return value from env_ubi_volume_create() is not checked. If volume creation fails (e.g. no space left), the subsequent ubi_volume_read() will also fail, but the error message will not indicate the underlying problem. Please can you check the return value and print an appropriate message if creation fails?

Regards,
Simon

author
#3 lore raw

Re: [PATCH 1/3] cmd: ubi: expose more APIs to public

In reply to Simon Glass
Quoted 10 lines · Simon Glass

Hi Weijie,

Quoted 7 lines · Weijie Gao

cmd: ubi: expose more APIs to public

Export ubi_detach/ubi_create_vol/ubi_find_volume/ubi_remove_vol to public for better ubi manipulation used by other modules.

Quoted 16 lines

include/ubi_uboot.h line 51

Quoted 10 lines

cmd/ubi.c | 10 +++++-----

 include/ubi_uboot.h |  5 +++++
 2 files changed, 10 insertions(+), 5 deletions(-)
include/ubi_uboot.h +4 -0
@@ -47,9 +47,14 @@
+extern int ubi_create_vol(char *volume, int64_t size, int dynamic,
 int vol_id,
+                       bool skipcheck);
+extern struct ubi_volume *ubi_find_volume(char *volume);
+extern int ubi_remove_vol(char *volume);

The volume parameter should be const char * since these functions do not modify the string. In patch 3/3, env_ubi_volume_create() receives a const char * and has to cast away const when calling these functions. Please can you fix the signatures in both cmd/ubi.c and ubi_uboot.h to take const char *volume.

OK. We need another patch to change all existing functions with char * to const char *.

Quoted 3 lines

Regards,
Simon

author
#7 lore raw

Re: [PATCH 2/3] cmd: ubi: allow creating volume with all free spaces in ubi_create_vol

In reply to Simon Glass
Quoted 17 lines · Simon Glass

Hi Weijie,

Quoted 7 lines · Weijie Gao

cmd: ubi: allow creating volume with all free spaces in ubi_create_vol

Although the ubi command itself supports creating volume with all free spaces, the api ubi_create_vol() does not.

Since negative size in invalid, this patch replaces negative size

is invalid

Quoted 4 lines

with all free space size in ubi_create_vol().

Quoted 12 lines

cmd/ubi.c int ubi_create_vol(char *volume, int64_t size, · line 230

Quoted 9 lines

cmd/ubi.c | 6 +++++-

 1 file changed, 5 insertions(+), 1 deletion(-)
cmd/ubi.c +4 -0
@@ -226,7 +226,11 @@ int ubi_create_vol(char *volume, int64_t size,
 int dynamic, int vol_id,
+     if (size < 0)
+             req.bytes = ubi->avail_pebs * ubi->leb_size;
+     else
+             req.bytes = size;

When size is negative, the printf() below still prints the original negative size rather than the computed req.bytes

OK. As you mentioned in patch 3/3, I think maybe we can move these printf logs to the command's main function to avoid logs being printed every time these API functions are called.

Quoted 3 lines

Regards,
Simon

author
#11 lore raw

Re: [PATCH 3/3] env: ubi: add support to create environment volume if it does not exist

In reply to Simon Glass
Quoted 11 lines · Simon Glass

Hi Weijie,

Quoted 8 lines · Weijie Gao

env: ubi: add support to create environment volume if it does not exist

Add an option to allow environment volume being auto created if not exist.

Quoted 8 lines
Quoted 3 lines

env/Kconfig | 6 ++++++

 env/ubi.c   | 20 ++++++++++++++++++++
 2 files changed, 26 insertions(+)

Could you beef up the Kconfig help text for ENV_UBI_VOLUME_CREATE - e.g. mention when users might want to enable this, such as on first boot of a fresh device where the UBI partition exists but no volumes have been created yet.

OK.

Quoted 10 lines

env/ubi.c static int env_ubi_save(void) · line 109

Quoted 8 lines
env/ubi.c +5 -0
@@ -105,6 +105,18 @@ static int env_ubi_save(void)
+int __weak env_ubi_volume_create(const char *volume)
+{
+     struct ubi_volume *vol;
+
+     vol = ubi_find_volume((char *)volume);

Does this function need to be weak?

Is unnecessary at all. I added in my early test.

Quoted 7 lines

ubi_find_volume() prints "Volume %s not found!" when the volume does not exist. Since the whole point of this function is to handle missing
volumes, this message will appear every time auto-creation is used. Please can you either add a quiet variant of ubi_find_volume(), or suppress the message here?

As I replied in 2/3, I'll move these messages into the main command function.

Quoted 14 lines

env/ubi.c static int env_ubi_load(void) · line 149

Quoted 7 lines
env/ubi.c +4 -0
@@ -134,6 +146,11 @@ static int env_ubi_load(void)
+     if (IS_ENABLED(CONFIG_ENV_UBI_VOLUME_CREATE)) {
+             env_ubi_volume_create(CONFIG_ENV_UBI_VOLUME);
+             env_ubi_volume_create(CONFIG_ENV_UBI_VOLUME_REDUND);
+     }

The return value from env_ubi_volume_create() is not checked. If volume creation fails (e.g. no space left), the subsequent ubi_volume_read() will also fail, but the error message will not indicate the underlying problem. Please can you check the return value
and print an appropriate message if creation fails?

OK.

Quoted 3 lines

Regards,
Simon

#4 lore raw

Re: [PATCH 1/3] cmd: ubi: expose more APIs to public

In reply to Weijie Gao

Hi Weijie,

Quoted 29 lines · Weijie Gao
Quoted 10 lines · Simon Glass

Hi Weijie,

Quoted 7 lines · Weijie Gao

cmd: ubi: expose more APIs to public

Export ubi_detach/ubi_create_vol/ubi_find_volume/ubi_remove_vol to public for better ubi manipulation used by other modules.

Quoted 16 lines

include/ubi_uboot.h line 51

Quoted 10 lines

cmd/ubi.c | 10 +++++-----

 include/ubi_uboot.h |  5 +++++
 2 files changed, 10 insertions(+), 5 deletions(-)
include/ubi_uboot.h +4 -0
@@ -47,9 +47,14 @@
+extern int ubi_create_vol(char *volume, int64_t size, int dynamic,
 int vol_id,
+                       bool skipcheck);
+extern struct ubi_volume *ubi_find_volume(char *volume);
+extern int ubi_remove_vol(char *volume);

The volume parameter should be const char * since these functions do not modify the string. In patch 3/3, env_ubi_volume_create() receives a const char * and has to cast away const when calling these functions. Please can you fix the signatures in both cmd/ubi.c and ubi_uboot.h to take const char *volume.

Quoted 2 lines

OK. We need another patch to change all existing functions with char * to const char *.

Yes that sounds like a good idea.

Regards,
Simon

#8 lore raw

Re: [PATCH 2/3] cmd: ubi: allow creating volume with all free spaces in ubi_create_vol

In reply to Weijie Gao

Hi Weijie,

Quoted 32 lines · Weijie Gao
Quoted 17 lines · Simon Glass

Hi Weijie,

Quoted 7 lines · Weijie Gao

cmd: ubi: allow creating volume with all free spaces in ubi_create_vol

Although the ubi command itself supports creating volume with all free spaces, the api ubi_create_vol() does not.

Since negative size in invalid, this patch replaces negative size

is invalid

Quoted 4 lines

with all free space size in ubi_create_vol().

Quoted 12 lines

cmd/ubi.c int ubi_create_vol(char *volume, int64_t size, · line 230

Quoted 9 lines

cmd/ubi.c | 6 +++++-

 1 file changed, 5 insertions(+), 1 deletion(-)
cmd/ubi.c +4 -0
@@ -226,7 +226,11 @@ int ubi_create_vol(char *volume, int64_t size,
 int dynamic, int vol_id,
+     if (size < 0)
+             req.bytes = ubi->avail_pebs * ubi->leb_size;
+     else
+             req.bytes = size;

When size is negative, the printf() below still prints the original negative size rather than the computed req.bytes

Quoted 3 lines

OK. As you mentioned in patch 3/3, I think maybe we can move these printf logs to the command's main function to avoid logs being printed every time these API functions are called.

Yes, good idea.

Regards,
Simon