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

Weijie Gao weijie.gao at mediatek.com
Fri Apr 17 04:35:32 CEST 2026


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

Signed-off-by: Weijie Gao <weijie.gao at mediatek.com>
---
 env/Kconfig |  6 ++++++
 env/ubi.c   | 20 ++++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/env/Kconfig b/env/Kconfig
index 7abd82ab6f3..cec1bf0bb11 100644
--- a/env/Kconfig
+++ b/env/Kconfig
@@ -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
diff --git a/env/ubi.c b/env/ubi.c
index e9865b45ebc..bbd86adf176 100644
--- a/env/ubi.c
+++ b/env/ubi.c
@@ -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);
-- 
2.45.2



More information about the U-Boot mailing list