[PATCH v3 06/10] cmd: ubi: reorganize command messages
Weijie Gao
weijie.gao at mediatek.com
Mon Apr 27 09:09:32 CEST 2026
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 at mediatek.com>
---
v2: new
v3: Fix use-after-free and "ubi read" command message displaying error
---
cmd/ubi.c | 109 +++++++++++++++++++++++++++++++++++++++---------------
1 file changed, 79 insertions(+), 30 deletions(-)
diff --git a/cmd/ubi.c b/cmd/ubi.c
index 8d04ff61fb6..9f121ea2931 100644
--- a/cmd/ubi.c
+++ b/cmd/ubi.c
@@ -241,8 +241,7 @@ static int ubi_create_vol(const char *volume, int64_t size, bool 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;
@@ -529,12 +533,8 @@ int ubi_volume_read(const char *volume, void *buf, loff_t offset, size_t size)
if (offp == vol->used_bytes)
return 0;
- if (size == 0) {
- printf("No size specified -> Using max size (%lld)\n", vol->used_bytes);
+ if (size == 0)
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);
@@ -619,13 +619,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 +695,8 @@ static int do_ubi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
int64_t size;
ulong addr = 0;
bool skipcheck = false;
+ struct ubi_volume *vol;
+ int ret;
if (argc < 2)
return CMD_RET_USAGE;
@@ -806,31 +805,65 @@ 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) {
+ int vol_id;
+
+ vol = ubi_find_volume(argv[2]);
+ if (!vol)
+ return 0;
+
+ vol_id = vol->vol_id;
+
+ ret = ubi_remove_vol(argv[2]);
+ if (!ret) {
+ printf("Removed UBI volume %s (id %d)\n",
+ argv[2], 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 +910,23 @@ 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);
+ if (!size) {
+ vol = ubi_require_volume(argv[3]);
+ if (!vol)
+ return 1;
+
+ printf("No size specified -> Using max size (%lld)\n",
+ vol->used_bytes);
+ size = vol->used_bytes;
+ }
+
+ 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
More information about the U-Boot
mailing list