* [PATCH v2 0/8] Add support for ubi environment to create volumes
@ 2026-04-21 8:35 Weijie Gao
2026-04-21 8:35 ` [PATCH v2 1/8] ubi: remove unnecessary extern directive from function prototypes Weijie Gao
` (7 more replies)
0 siblings, 8 replies; 9+ messages in thread
From: Weijie Gao @ 2026-04-21 8:35 UTC (permalink / raw)
To: u-boot
Cc: GSS_MTK_Uboot_upstream, Tom Rini, Marek Vasut, Simon Glass,
Weijie Gao
This patch series expose more ubi helpers to allow the ubi environment to
create ubi env volumes if not exist.
Changes:
v2: Add const qualifier for char */void * in function parameters
Adjust normal command message
Weijie Gao (8):
ubi: remove unnecessary extern directive from function prototypes
cmd: ubi: mark read-only function parameters with const
cmd: ubi: use void * for buf parameter in ubi_volume_read
cmd: ubifs: mark string parameters with const
cmd: ubi: reorganize command messages
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 | 141 ++++++++++++++++++++++++++++--------------
cmd/ubifs.c | 2 +-
env/Kconfig | 11 ++++
env/ubi.c | 34 +++++++++-
fs/ubifs/super.c | 2 +-
fs/ubifs/ubifs.c | 2 +-
include/ubi_uboot.h | 20 +++---
include/ubifs_uboot.h | 4 +-
8 files changed, 154 insertions(+), 62 deletions(-)
--
2.45.2
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v2 1/8] ubi: remove unnecessary extern directive from function prototypes
2026-04-21 8:35 [PATCH v2 0/8] Add support for ubi environment to create volumes Weijie Gao
@ 2026-04-21 8:35 ` Weijie Gao
2026-04-21 8:35 ` [PATCH v2 2/8] cmd: ubi: mark read-only function parameters with const Weijie Gao
` (6 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Weijie Gao @ 2026-04-21 8:35 UTC (permalink / raw)
To: u-boot
Cc: GSS_MTK_Uboot_upstream, Tom Rini, Marek Vasut, Simon Glass,
Weijie Gao
The extern directive is unnecessary for function declaration and should be
removed.
Signed-off-by: Weijie Gao <weijie.gao@mediatek.com>
---
v2: new
---
include/ubi_uboot.h | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/include/ubi_uboot.h b/include/ubi_uboot.h
index ea0db69c72a..05fb9634d81 100644
--- a/include/ubi_uboot.h
+++ b/include/ubi_uboot.h
@@ -44,12 +44,12 @@
#endif
/* functions */
-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_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);
+int ubi_mtd_param_parse(const char *val, struct kernel_param *kp);
+int ubi_init(void);
+void ubi_exit(void);
+int ubi_part(char *part_name, const char *vid_header_offset);
+int ubi_volume_write(char *volume, void *buf, loff_t offset, size_t size);
+int ubi_volume_read(char *volume, char *buf, loff_t offset, size_t size);
extern struct ubi_device *ubi_devices[];
int cmd_ubifs_mount(char *vol_name);
--
2.45.2
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 2/8] cmd: ubi: mark read-only function parameters with const
2026-04-21 8:35 [PATCH v2 0/8] Add support for ubi environment to create volumes Weijie Gao
2026-04-21 8:35 ` [PATCH v2 1/8] ubi: remove unnecessary extern directive from function prototypes Weijie Gao
@ 2026-04-21 8:35 ` Weijie Gao
2026-04-21 8:36 ` [PATCH v2 3/8] cmd: ubi: use void * for buf parameter in ubi_volume_read Weijie Gao
` (5 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Weijie Gao @ 2026-04-21 8:35 UTC (permalink / raw)
To: u-boot
Cc: GSS_MTK_Uboot_upstream, Tom Rini, Marek Vasut, Simon Glass,
Weijie Gao
Parameters like part/volume name and buffer for writing are not being
modified by the callee functions and should be marked const.
Signed-off-by: Weijie Gao <weijie.gao@mediatek.com>
---
v2: new
---
cmd/ubi.c | 41 ++++++++++++++++++++++-------------------
include/ubi_uboot.h | 7 ++++---
2 files changed, 26 insertions(+), 22 deletions(-)
diff --git a/cmd/ubi.c b/cmd/ubi.c
index 93de6f3aea2..f75ceff11ee 100644
--- a/cmd/ubi.c
+++ b/cmd/ubi.c
@@ -39,7 +39,7 @@ static struct ubi_device *ubi;
#include <ubifs_uboot.h>
#endif
-static void display_volume_info(struct ubi_device *ubi)
+static void display_volume_info(const struct ubi_device *ubi)
{
int i;
@@ -50,7 +50,7 @@ static void display_volume_info(struct ubi_device *ubi)
}
}
-static void display_ubi_info(struct ubi_device *ubi)
+static void display_ubi_info(const struct ubi_device *ubi)
{
ubi_msg("MTD device name: \"%s\"", ubi->mtd->name);
ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20);
@@ -149,12 +149,12 @@ static int ubi_list(const char *var, int numeric)
return 0;
}
-static int ubi_check_volumename(const struct ubi_volume *vol, char *name)
+static int ubi_check_volumename(const struct ubi_volume *vol, const char *name)
{
return strcmp(vol->name, name);
}
-static int ubi_check(char *name)
+static int ubi_check(const char *name)
{
int i;
@@ -213,8 +213,8 @@ bad:
return err;
}
-static int ubi_create_vol(char *volume, int64_t size, int dynamic, int vol_id,
- bool skipcheck)
+static int ubi_create_vol(const 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)
+static struct ubi_volume *ubi_find_volume(const 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)
+static int ubi_remove_vol(const char *volume)
{
int err, reserved_pebs, i;
struct ubi_volume *vol;
@@ -316,7 +316,7 @@ out_err:
return err;
}
-static int ubi_rename_vol(char *oldname, char *newname)
+static int ubi_rename_vol(const char *oldname, const char *newname)
{
struct ubi_volume *vol;
struct ubi_rename_entry rename;
@@ -354,7 +354,8 @@ static int ubi_rename_vol(char *oldname, char *newname)
return ubi_rename_volumes(ubi, &list);
}
-static int ubi_volume_continue_write(char *volume, void *buf, size_t size)
+static int ubi_volume_continue_write(const char *volume, const void *buf,
+ size_t size)
{
int err;
struct ubi_volume *vol;
@@ -394,8 +395,8 @@ static int ubi_volume_continue_write(char *volume, void *buf, size_t size)
return 0;
}
-int ubi_volume_begin_write(char *volume, void *buf, size_t size,
- size_t full_size)
+int ubi_volume_begin_write(const char *volume, const void *buf, size_t size,
+ size_t full_size)
{
int err;
int rsvd_bytes;
@@ -424,8 +425,8 @@ int ubi_volume_begin_write(char *volume, void *buf, size_t size,
return ubi_volume_continue_write(volume, buf, size);
}
-static int ubi_volume_offset_write(char *volume, void *buf, loff_t offset,
- size_t size)
+static int ubi_volume_offset_write(const char *volume, const void *buf,
+ loff_t offset, size_t size)
{
int len, tbuf_size, ret;
u64 lnum;
@@ -487,7 +488,8 @@ exit:
return ret;
}
-int ubi_volume_write(char *volume, void *buf, loff_t offset, size_t size)
+int ubi_volume_write(const char *volume, const void *buf, loff_t offset,
+ size_t size)
{
int ret;
@@ -503,7 +505,7 @@ int ubi_volume_write(char *volume, void *buf, loff_t offset, size_t size)
return ret;
}
-int ubi_volume_read(char *volume, char *buf, loff_t offset, size_t size)
+int ubi_volume_read(const char *volume, char *buf, loff_t offset, size_t size)
{
int err, lnum, off, len, tbuf_size;
void *tbuf;
@@ -587,7 +589,8 @@ int ubi_volume_read(char *volume, char *buf, loff_t offset, size_t size)
return err;
}
-static int ubi_dev_scan(struct mtd_info *info, const char *vid_header_offset)
+static int ubi_dev_scan(const struct mtd_info *info,
+ const char *vid_header_offset)
{
char ubi_mtd_param_buffer[80];
int err;
@@ -611,7 +614,7 @@ static int ubi_dev_scan(struct mtd_info *info, const char *vid_header_offset)
return 0;
}
-static int ubi_set_skip_check(char *volume, bool skip_check)
+static int ubi_set_skip_check(const char *volume, bool skip_check)
{
struct ubi_vtbl_record vtbl_rec;
struct ubi_volume *vol;
@@ -658,7 +661,7 @@ static int ubi_detach(void)
return 0;
}
-int ubi_part(char *part_name, const char *vid_header_offset)
+int ubi_part(const char *part_name, const char *vid_header_offset)
{
struct mtd_info *mtd;
int err;
diff --git a/include/ubi_uboot.h b/include/ubi_uboot.h
index 05fb9634d81..a949d1b80dd 100644
--- a/include/ubi_uboot.h
+++ b/include/ubi_uboot.h
@@ -47,9 +47,10 @@
int ubi_mtd_param_parse(const char *val, struct kernel_param *kp);
int ubi_init(void);
void ubi_exit(void);
-int ubi_part(char *part_name, const char *vid_header_offset);
-int ubi_volume_write(char *volume, void *buf, loff_t offset, size_t size);
-int ubi_volume_read(char *volume, char *buf, loff_t offset, size_t size);
+int ubi_part(const char *part_name, const char *vid_header_offset);
+int ubi_volume_write(const char *volume, const void *buf, loff_t offset,
+ size_t size);
+int ubi_volume_read(const char *volume, char *buf, loff_t offset, size_t size);
extern struct ubi_device *ubi_devices[];
int cmd_ubifs_mount(char *vol_name);
--
2.45.2
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 3/8] cmd: ubi: use void * for buf parameter in ubi_volume_read
2026-04-21 8:35 [PATCH v2 0/8] Add support for ubi environment to create volumes Weijie Gao
2026-04-21 8:35 ` [PATCH v2 1/8] ubi: remove unnecessary extern directive from function prototypes Weijie Gao
2026-04-21 8:35 ` [PATCH v2 2/8] cmd: ubi: mark read-only function parameters with const Weijie Gao
@ 2026-04-21 8:36 ` Weijie Gao
2026-04-21 8:36 ` [PATCH v2 4/8] cmd: ubifs: mark string parameters with const Weijie Gao
` (4 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Weijie Gao @ 2026-04-21 8:36 UTC (permalink / raw)
To: u-boot
Cc: GSS_MTK_Uboot_upstream, Tom Rini, Marek Vasut, Simon Glass,
Weijie Gao
Use void * to avoid explicit type casting as what ubi_volume_write has done
already.
Signed-off-by: Weijie Gao <weijie.gao@mediatek.com>
---
v2: new
---
cmd/ubi.c | 4 ++--
env/ubi.c | 4 ++--
include/ubi_uboot.h | 2 +-
3 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/cmd/ubi.c b/cmd/ubi.c
index f75ceff11ee..8e3cfaaddbb 100644
--- a/cmd/ubi.c
+++ b/cmd/ubi.c
@@ -505,7 +505,7 @@ int ubi_volume_write(const char *volume, const void *buf, loff_t offset,
return ret;
}
-int ubi_volume_read(const char *volume, char *buf, loff_t offset, size_t size)
+int ubi_volume_read(const char *volume, void *buf, loff_t offset, size_t size)
{
int err, lnum, off, len, tbuf_size;
void *tbuf;
@@ -877,7 +877,7 @@ static int do_ubi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
}
if (argc == 3) {
- return ubi_volume_read(argv[3], (char *)addr, 0, size);
+ return ubi_volume_read(argv[3], (void *)addr, 0, size);
}
}
diff --git a/env/ubi.c b/env/ubi.c
index e9865b45ebc..46970ba754f 100644
--- a/env/ubi.c
+++ b/env/ubi.c
@@ -132,14 +132,14 @@ static int env_ubi_load(void)
return -EIO;
}
- read1_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1, 0,
+ read1_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME, tmp_env1, 0,
CONFIG_ENV_SIZE);
if (read1_fail)
printf("\n** Unable to read env from %s:%s **\n",
CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
read2_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND,
- (void *)tmp_env2, 0, CONFIG_ENV_SIZE);
+ tmp_env2, 0, CONFIG_ENV_SIZE);
if (read2_fail)
printf("\n** Unable to read redundant env from %s:%s **\n",
CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND);
diff --git a/include/ubi_uboot.h b/include/ubi_uboot.h
index a949d1b80dd..dd22ec7537a 100644
--- a/include/ubi_uboot.h
+++ b/include/ubi_uboot.h
@@ -50,7 +50,7 @@ void ubi_exit(void);
int ubi_part(const char *part_name, const char *vid_header_offset);
int ubi_volume_write(const char *volume, const void *buf, loff_t offset,
size_t size);
-int ubi_volume_read(const char *volume, char *buf, loff_t offset, size_t size);
+int ubi_volume_read(const char *volume, void *buf, loff_t offset, size_t size);
extern struct ubi_device *ubi_devices[];
int cmd_ubifs_mount(char *vol_name);
--
2.45.2
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 4/8] cmd: ubifs: mark string parameters with const
2026-04-21 8:35 [PATCH v2 0/8] Add support for ubi environment to create volumes Weijie Gao
` (2 preceding siblings ...)
2026-04-21 8:36 ` [PATCH v2 3/8] cmd: ubi: use void * for buf parameter in ubi_volume_read Weijie Gao
@ 2026-04-21 8:36 ` Weijie Gao
2026-04-21 8:36 ` [PATCH v2 5/8] cmd: ubi: reorganize command messages Weijie Gao
` (3 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Weijie Gao @ 2026-04-21 8:36 UTC (permalink / raw)
To: u-boot
Cc: GSS_MTK_Uboot_upstream, Tom Rini, Marek Vasut, Simon Glass,
Weijie Gao
File name and volume name should be const as they will not be modified in
these functions.
Signed-off-by: Weijie Gao <weijie.gao@mediatek.com>
---
v2: new
---
cmd/ubifs.c | 2 +-
fs/ubifs/super.c | 2 +-
fs/ubifs/ubifs.c | 2 +-
include/ubi_uboot.h | 2 +-
include/ubifs_uboot.h | 4 ++--
5 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/cmd/ubifs.c b/cmd/ubifs.c
index 22e95db8ca5..81f2d37fc7b 100644
--- a/cmd/ubifs.c
+++ b/cmd/ubifs.c
@@ -19,7 +19,7 @@
static int ubifs_initialized;
static int ubifs_mounted;
-int cmd_ubifs_mount(char *vol_name)
+int cmd_ubifs_mount(const char *vol_name)
{
int ret;
diff --git a/fs/ubifs/super.c b/fs/ubifs/super.c
index b6004b88f4e..9c8974a97a5 100644
--- a/fs/ubifs/super.c
+++ b/fs/ubifs/super.c
@@ -2694,7 +2694,7 @@ MODULE_VERSION(__stringify(UBIFS_VERSION));
MODULE_AUTHOR("Artem Bityutskiy, Adrian Hunter");
MODULE_DESCRIPTION("UBIFS - UBI File System");
#else
-int uboot_ubifs_mount(char *vol_name)
+int uboot_ubifs_mount(const char *vol_name)
{
struct dentry *ret;
int flags;
diff --git a/fs/ubifs/ubifs.c b/fs/ubifs/ubifs.c
index b0cc0d2e1b2..f6f21e6af22 100644
--- a/fs/ubifs/ubifs.c
+++ b/fs/ubifs/ubifs.c
@@ -974,7 +974,7 @@ void ubifs_close(void)
}
/* Compat wrappers for common/cmd_ubifs.c */
-int ubifs_load(char *filename, unsigned long addr, u32 size)
+int ubifs_load(const char *filename, unsigned long addr, u32 size)
{
loff_t actread;
int err;
diff --git a/include/ubi_uboot.h b/include/ubi_uboot.h
index dd22ec7537a..6ebd8a3b613 100644
--- a/include/ubi_uboot.h
+++ b/include/ubi_uboot.h
@@ -53,7 +53,7 @@ int ubi_volume_write(const char *volume, const void *buf, loff_t offset,
int ubi_volume_read(const char *volume, void *buf, loff_t offset, size_t size);
extern struct ubi_device *ubi_devices[];
-int cmd_ubifs_mount(char *vol_name);
+int cmd_ubifs_mount(const char *vol_name);
int cmd_ubifs_umount(void);
#if IS_ENABLED(CONFIG_UBI_BLOCK)
diff --git a/include/ubifs_uboot.h b/include/ubifs_uboot.h
index db8a29e9bbd..0877dd84f99 100644
--- a/include/ubifs_uboot.h
+++ b/include/ubifs_uboot.h
@@ -18,10 +18,10 @@ struct blk_desc;
struct disk_partition;
int ubifs_init(void);
-int uboot_ubifs_mount(char *vol_name);
+int uboot_ubifs_mount(const char *vol_name);
void uboot_ubifs_umount(void);
int ubifs_is_mounted(void);
-int ubifs_load(char *filename, unsigned long addr, u32 size);
+int ubifs_load(const char *filename, unsigned long addr, u32 size);
int ubifs_set_blk_dev(struct blk_desc *rbdd, struct disk_partition *info);
int ubifs_ls(const char *dir_name);
--
2.45.2
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 5/8] cmd: ubi: reorganize command messages
2026-04-21 8:35 [PATCH v2 0/8] Add support for ubi environment to create volumes Weijie Gao
` (3 preceding siblings ...)
2026-04-21 8:36 ` [PATCH v2 4/8] cmd: ubifs: mark string parameters with const Weijie Gao
@ 2026-04-21 8:36 ` Weijie Gao
2026-04-21 8:36 ` [PATCH v2 6/8] cmd: ubi: expose more APIs to public Weijie Gao
` (2 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Weijie Gao @ 2026-04-21 8:36 UTC (permalink / raw)
To: u-boot
Cc: GSS_MTK_Uboot_upstream, Tom Rini, Marek Vasut, Simon Glass,
Weijie Gao
This patch moves normal subcommand messages into the main command function.
This will allow current and potential api functions being called with clean
output.
A new function ubi_require_volume() is added for finding and printing error
message if volume not found. The original ubi_find_volume() will be silent
for being an api function.
Signed-off-by: Weijie Gao <weijie.gao@mediatek.com>
---
v2: new
---
cmd/ubi.c | 92 +++++++++++++++++++++++++++++++++++++++----------------
1 file changed, 65 insertions(+), 27 deletions(-)
diff --git a/cmd/ubi.c b/cmd/ubi.c
index 8e3cfaaddbb..f2ae63e6a7a 100644
--- a/cmd/ubi.c
+++ b/cmd/ubi.c
@@ -241,8 +241,7 @@ static int ubi_create_vol(const char *volume, int64_t size, int dynamic,
printf("verify_mkvol_req failed %d\n", err);
return err;
}
- printf("Creating %s volume %s of size %lld\n",
- dynamic ? "dynamic" : "static", volume, size);
+
/* Call real ubi create volume */
return ubi_create_volume(ubi, &req);
}
@@ -258,10 +257,19 @@ static struct ubi_volume *ubi_find_volume(const char *volume)
return vol;
}
- printf("Volume %s not found!\n", volume);
return NULL;
}
+static struct ubi_volume *ubi_require_volume(const char *volume)
+{
+ struct ubi_volume *vol = ubi_find_volume(volume);
+
+ if (!vol)
+ printf("Volume %s not found!\n", volume);
+
+ return vol;
+}
+
static int ubi_remove_vol(const char *volume)
{
int err, reserved_pebs, i;
@@ -271,8 +279,6 @@ static int ubi_remove_vol(const char *volume)
if (vol == NULL)
return ENODEV;
- printf("Remove UBI volume %s (id %d)\n", vol->name, vol->vol_id);
-
if (ubi->ro_mode) {
printf("It's read-only mode\n");
err = EROFS;
@@ -334,8 +340,6 @@ static int ubi_rename_vol(const char *oldname, const char *newname)
return EINVAL;
}
- printf("Rename UBI volume %s to %s\n", oldname, newname);
-
if (ubi->ro_mode) {
printf("%s: ubi device is in read-only mode\n", __func__);
return EROFS;
@@ -360,7 +364,7 @@ static int ubi_volume_continue_write(const char *volume, const void *buf,
int err;
struct ubi_volume *vol;
- vol = ubi_find_volume(volume);
+ vol = ubi_require_volume(volume);
if (vol == NULL)
return ENODEV;
@@ -402,7 +406,7 @@ int ubi_volume_begin_write(const char *volume, const void *buf, size_t size,
int rsvd_bytes;
struct ubi_volume *vol;
- vol = ubi_find_volume(volume);
+ vol = ubi_require_volume(volume);
if (vol == NULL)
return ENODEV;
@@ -434,7 +438,7 @@ static int ubi_volume_offset_write(const char *volume, const void *buf,
loff_t off = offset;
void *tbuf;
- vol = ubi_find_volume(volume);
+ vol = ubi_require_volume(volume);
if (!vol)
return -ENODEV;
@@ -514,7 +518,7 @@ int ubi_volume_read(const char *volume, void *buf, loff_t offset, size_t size)
loff_t offp = offset;
size_t len_read;
- vol = ubi_find_volume(volume);
+ vol = ubi_require_volume(volume);
if (vol == NULL)
return ENODEV;
@@ -534,8 +538,6 @@ int ubi_volume_read(const char *volume, void *buf, loff_t offset, size_t size)
size = vol->used_bytes;
}
- printf("Read %zu bytes from volume %s to %p\n", size, volume, buf);
-
if (vol->corrupted)
printf("read from corrupted volume %d", vol->vol_id);
if (offp + size > vol->used_bytes)
@@ -619,13 +621,10 @@ static int ubi_set_skip_check(const char *volume, bool skip_check)
struct ubi_vtbl_record vtbl_rec;
struct ubi_volume *vol;
- vol = ubi_find_volume(volume);
+ vol = ubi_require_volume(volume);
if (!vol)
return ENODEV;
- printf("%sing skip_check on volume %s\n",
- skip_check ? "Sett" : "Clear", volume);
-
vtbl_rec = ubi->vtbl[vol->vol_id];
if (skip_check) {
vtbl_rec.flags |= UBI_VTBL_SKIP_CRC_CHECK_FLG;
@@ -698,6 +697,7 @@ static int do_ubi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
int64_t size;
ulong addr = 0;
bool skipcheck = false;
+ int ret;
if (argc < 2)
return CMD_RET_USAGE;
@@ -806,31 +806,63 @@ static int do_ubi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
}
/* E.g., create volume */
if (argc == 3) {
- return ubi_create_vol(argv[2], size, dynamic, id,
- skipcheck);
+ ret = ubi_create_vol(argv[2], size, dynamic, id,
+ skipcheck);
+ if (!ret)
+ printf("Created %s volume %s of size %lld\n",
+ dynamic ? "dynamic" : "static", argv[2],
+ size);
+ else if (ret == -EEXIST)
+ printf("Volume %s already exists!\n", argv[2]);
+
+ return ret;
}
}
if (strncmp(argv[1], "remove", 6) == 0) {
/* E.g., remove volume */
- if (argc == 3)
- return ubi_remove_vol(argv[2]);
+ if (argc == 3) {
+ struct ubi_volume *vol;
+
+ vol = ubi_find_volume(argv[2]);
+ if (!vol)
+ return 0;
+
+ ret = ubi_remove_vol(argv[2]);
+ if (!ret) {
+ printf("Removed UBI volume %s (id %d)\n",
+ vol->name, vol->vol_id);
+ }
+
+ return ret;
+ }
}
- if (IS_ENABLED(CONFIG_CMD_UBI_RENAME) && !strncmp(argv[1], "rename", 6))
- return ubi_rename_vol(argv[2], argv[3]);
+ if (IS_ENABLED(CONFIG_CMD_UBI_RENAME) && !strncmp(argv[1], "rename", 6)) {
+ ret = ubi_rename_vol(argv[2], argv[3]);
+ if (!ret) {
+ printf("UBI volume %s renamed to %s\n", argv[2],
+ argv[3]);
+ }
+
+ return ret;
+ }
if (strncmp(argv[1], "skipcheck", 9) == 0) {
/* E.g., change skip_check flag */
if (argc == 4) {
skipcheck = strncmp(argv[3], "on", 2) == 0;
- return ubi_set_skip_check(argv[2], skipcheck);
+ ret = ubi_set_skip_check(argv[2], skipcheck);
+ if (!ret) {
+ printf("%s skip_check on volume %s\n",
+ skipcheck ? "Set" : "Cleared", argv[2]);
+ }
+
+ return ret;
}
}
if (strncmp(argv[1], "write", 5) == 0) {
- int ret;
-
if (argc < 5) {
printf("Please see usage\n");
return 1;
@@ -877,7 +909,13 @@ static int do_ubi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
}
if (argc == 3) {
- return ubi_volume_read(argv[3], (void *)addr, 0, size);
+ ret = ubi_volume_read(argv[3], (void *)addr, 0, size);
+ if (!ret) {
+ printf("%lld bytes read from volume %s to 0x%lx\n",
+ size, argv[3], addr);
+ }
+
+ return ret;
}
}
--
2.45.2
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 6/8] cmd: ubi: expose more APIs to public
2026-04-21 8:35 [PATCH v2 0/8] Add support for ubi environment to create volumes Weijie Gao
` (4 preceding siblings ...)
2026-04-21 8:36 ` [PATCH v2 5/8] cmd: ubi: reorganize command messages Weijie Gao
@ 2026-04-21 8:36 ` Weijie Gao
2026-04-21 8:36 ` [PATCH v2 7/8] cmd: ubi: allow creating volume with all free spaces in ubi_create_vol Weijie Gao
2026-04-21 8:36 ` [PATCH v2 8/8] env: ubi: add support to create environment volume if it does not exist Weijie Gao
7 siblings, 0 replies; 9+ messages in thread
From: Weijie Gao @ 2026-04-21 8:36 UTC (permalink / raw)
To: u-boot
Cc: GSS_MTK_Uboot_upstream, Tom Rini, Marek Vasut, Simon Glass,
Weijie Gao
Export ubi_detach/ubi_create_vol/ubi_find_volume/ubi_remove_vol to
public for better ubi manipulation used by other modules.
Signed-off-by: Weijie Gao <weijie.gao@mediatek.com>
---
v2: not changed
---
cmd/ubi.c | 10 +++++-----
include/ubi_uboot.h | 5 +++++
2 files changed, 10 insertions(+), 5 deletions(-)
diff --git a/cmd/ubi.c b/cmd/ubi.c
index f2ae63e6a7a..aae33d4bca3 100644
--- a/cmd/ubi.c
+++ b/cmd/ubi.c
@@ -213,8 +213,8 @@ bad:
return err;
}
-static int ubi_create_vol(const char *volume, int64_t size, int dynamic,
- int vol_id, bool skipcheck)
+int ubi_create_vol(const char *volume, int64_t size, int dynamic, int vol_id,
+ bool skipcheck)
{
struct ubi_mkvol_req req;
int err;
@@ -246,7 +246,7 @@ static int ubi_create_vol(const char *volume, int64_t size, int dynamic,
return ubi_create_volume(ubi, &req);
}
-static struct ubi_volume *ubi_find_volume(const char *volume)
+struct ubi_volume *ubi_find_volume(const char *volume)
{
struct ubi_volume *vol;
int i;
@@ -270,7 +270,7 @@ static struct ubi_volume *ubi_require_volume(const char *volume)
return vol;
}
-static int ubi_remove_vol(const char *volume)
+int ubi_remove_vol(const char *volume)
{
int err, reserved_pebs, i;
struct ubi_volume *vol;
@@ -637,7 +637,7 @@ static int ubi_set_skip_check(const 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
/*
diff --git a/include/ubi_uboot.h b/include/ubi_uboot.h
index 6ebd8a3b613..902f214c73d 100644
--- a/include/ubi_uboot.h
+++ b/include/ubi_uboot.h
@@ -47,10 +47,15 @@
int ubi_mtd_param_parse(const char *val, struct kernel_param *kp);
int ubi_init(void);
void ubi_exit(void);
+int ubi_detach(void);
int ubi_part(const char *part_name, const char *vid_header_offset);
int ubi_volume_write(const char *volume, const void *buf, loff_t offset,
size_t size);
int ubi_volume_read(const char *volume, void *buf, loff_t offset, size_t size);
+int ubi_create_vol(const char *volume, int64_t size, int dynamic, int vol_id,
+ bool skipcheck);
+struct ubi_volume *ubi_find_volume(const char *volume);
+int ubi_remove_vol(const char *volume);
extern struct ubi_device *ubi_devices[];
int cmd_ubifs_mount(const char *vol_name);
--
2.45.2
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 7/8] cmd: ubi: allow creating volume with all free spaces in ubi_create_vol
2026-04-21 8:35 [PATCH v2 0/8] Add support for ubi environment to create volumes Weijie Gao
` (5 preceding siblings ...)
2026-04-21 8:36 ` [PATCH v2 6/8] cmd: ubi: expose more APIs to public Weijie Gao
@ 2026-04-21 8:36 ` Weijie Gao
2026-04-21 8:36 ` [PATCH v2 8/8] env: ubi: add support to create environment volume if it does not exist Weijie Gao
7 siblings, 0 replies; 9+ messages in thread
From: Weijie Gao @ 2026-04-21 8:36 UTC (permalink / raw)
To: u-boot
Cc: GSS_MTK_Uboot_upstream, Tom Rini, Marek Vasut, Simon Glass,
Weijie Gao
Although the ubi command itself supports creating volume with all
free spaces, the api ubi_create_vol() does not.
Since negative size is invalid, this patch replaces negative size
with all free space size in ubi_create_vol().
Signed-off-by: Weijie Gao <weijie.gao@mediatek.com>
---
v2: not changed
---
cmd/ubi.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/cmd/ubi.c b/cmd/ubi.c
index aae33d4bca3..95642736cef 100644
--- a/cmd/ubi.c
+++ b/cmd/ubi.c
@@ -226,7 +226,11 @@ int ubi_create_vol(const 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);
--
2.45.2
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 8/8] env: ubi: add support to create environment volume if it does not exist
2026-04-21 8:35 [PATCH v2 0/8] Add support for ubi environment to create volumes Weijie Gao
` (6 preceding siblings ...)
2026-04-21 8:36 ` [PATCH v2 7/8] cmd: ubi: allow creating volume with all free spaces in ubi_create_vol Weijie Gao
@ 2026-04-21 8:36 ` Weijie Gao
7 siblings, 0 replies; 9+ messages in thread
From: Weijie Gao @ 2026-04-21 8:36 UTC (permalink / raw)
To: u-boot
Cc: GSS_MTK_Uboot_upstream, Tom Rini, Marek Vasut, Simon Glass,
Weijie Gao
Add an option to allow environment volume being auto created if not
exist.
Signed-off-by: Weijie Gao <weijie.gao@mediatek.com>
---
v2: updated kconfig help text
added error output when failed to create volume
---
env/Kconfig | 11 +++++++++++
env/ubi.c | 30 ++++++++++++++++++++++++++++++
2 files changed, 41 insertions(+)
diff --git a/env/Kconfig b/env/Kconfig
index 7abd82ab6f3..8404e0d44eb 100644
--- a/env/Kconfig
+++ b/env/Kconfig
@@ -716,6 +716,17 @@ 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
+ This option is useful if u-boot will be booted from a fresh device
+ where environment volume hasn't been created in the UBI partition.
+ This is a common case where factory UBI image contains only volumes
+ with valid data.
+ By enabling this option, environment volume(s) will be created before
+ loading if 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 46970ba754f..55c9c543444 100644
--- a/env/ubi.c
+++ b/env/ubi.c
@@ -103,6 +103,23 @@ static int env_ubi_save(void)
}
#endif /* CONFIG_ENV_REDUNDANT */
+static int env_ubi_volume_create(const char *volume)
+{
+ struct ubi_volume *vol;
+ int ret;
+
+ vol = ubi_find_volume(volume);
+ if (vol)
+ return 0;
+
+ ret = ubi_create_vol(volume, CONFIG_ENV_SIZE, true, UBI_VOL_NUM_AUTO,
+ false);
+ if (ret)
+ printf("Failed to create environment volume '%s'\n", volume);
+
+ return ret;
+}
+
#ifdef CONFIG_ENV_REDUNDANT
static int env_ubi_load(void)
{
@@ -132,6 +149,14 @@ static int env_ubi_load(void)
return -EIO;
}
+ if (IS_ENABLED(CONFIG_ENV_UBI_VOLUME_CREATE)) {
+ read1_fail = env_ubi_volume_create(CONFIG_ENV_UBI_VOLUME);
+ read2_fail = env_ubi_volume_create(CONFIG_ENV_UBI_VOLUME_REDUND);
+
+ if (read1_fail && read2_fail)
+ return -ENODEV;
+ }
+
read1_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME, tmp_env1, 0,
CONFIG_ENV_SIZE);
if (read1_fail)
@@ -169,6 +194,11 @@ static int env_ubi_load(void)
return -EIO;
}
+ if (IS_ENABLED(CONFIG_ENV_UBI_VOLUME_CREATE)) {
+ if (env_ubi_volume_create(CONFIG_ENV_UBI_VOLUME))
+ return -ENODEV;
+ }
+
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
^ permalink raw reply related [flat|nested] 9+ messages in thread
end of thread, other threads:[~2026-04-21 8:37 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-21 8:35 [PATCH v2 0/8] Add support for ubi environment to create volumes Weijie Gao
2026-04-21 8:35 ` [PATCH v2 1/8] ubi: remove unnecessary extern directive from function prototypes Weijie Gao
2026-04-21 8:35 ` [PATCH v2 2/8] cmd: ubi: mark read-only function parameters with const Weijie Gao
2026-04-21 8:36 ` [PATCH v2 3/8] cmd: ubi: use void * for buf parameter in ubi_volume_read Weijie Gao
2026-04-21 8:36 ` [PATCH v2 4/8] cmd: ubifs: mark string parameters with const Weijie Gao
2026-04-21 8:36 ` [PATCH v2 5/8] cmd: ubi: reorganize command messages Weijie Gao
2026-04-21 8:36 ` [PATCH v2 6/8] cmd: ubi: expose more APIs to public Weijie Gao
2026-04-21 8:36 ` [PATCH v2 7/8] cmd: ubi: allow creating volume with all free spaces in ubi_create_vol Weijie Gao
2026-04-21 8:36 ` [PATCH v2 8/8] env: ubi: add support to create environment volume if it does not exist Weijie Gao
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox