From a7d690cf3a7bdc1a56b72094f33a44042b898cfc Mon Sep 17 00:00:00 2001 From: Weijie Gao Date: Wed, 20 May 2026 16:27:25 +0800 Subject: ubi: remove unnecessary extern directive from function prototypes The extern directive is unnecessary for function declaration and should be removed. Reviewed-by: Simon Glass Signed-off-by: Weijie Gao --- include/ubi_uboot.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'include') 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); -- cgit v1.3.1 From 065b0ba3b14fd6795626b89d5e8e3daede02475a Mon Sep 17 00:00:00 2001 From: Weijie Gao Date: Wed, 20 May 2026 16:27:28 +0800 Subject: cmd: ubi: mark read-only function parameters with const Parameters like part/volume name and buffer for writing are not being modified by the callee functions and should be marked const. Reviewed-by: Simon Glass Signed-off-by: Weijie Gao --- cmd/ubi.c | 41 ++++++++++++++++++++++------------------- include/ubi_uboot.h | 7 ++++--- 2 files changed, 26 insertions(+), 22 deletions(-) (limited to 'include') 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 #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); -- cgit v1.3.1 From b63af5e2c20cc9d2d510c5ff38d48114582d410d Mon Sep 17 00:00:00 2001 From: Weijie Gao Date: Wed, 20 May 2026 16:27:30 +0800 Subject: cmd: ubi: use void * for buf parameter in ubi_volume_read Use void * to avoid explicit type casting as what ubi_volume_write has done already. Reviewed-by: Simon Glass Signed-off-by: Weijie Gao --- cmd/ubi.c | 4 ++-- env/ubi.c | 4 ++-- include/ubi_uboot.h | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) (limited to 'include') 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); -- cgit v1.3.1 From d9a9d72e45c12571acb2258d669484eaef235872 Mon Sep 17 00:00:00 2001 From: Weijie Gao Date: Wed, 20 May 2026 16:27:31 +0800 Subject: cmd: ubifs: mark string parameters with const File name and volume name should be const as they will not be modified in these functions. Reviewed-by: Simon Glass Signed-off-by: Weijie Gao --- 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(-) (limited to 'include') 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 59bc7547304..6121fb0b135 100644 --- a/fs/ubifs/ubifs.c +++ b/fs/ubifs/ubifs.c @@ -991,7 +991,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); -- cgit v1.3.1 From 14fe02ac5e5260a0dad58804cc6c922827bf9d7e Mon Sep 17 00:00:00 2001 From: Weijie Gao Date: Wed, 20 May 2026 16:27:38 +0800 Subject: cmd: ubi: export more APIs to public Export the following functions to public: - ubi_detach(): this is paired with ubi_part(). One may call this function to completely clean up the ubi subsystem after using ubi_part(). - ubi_{create,find,remove}_vol: this is a set of functions for volume management. The original ubi_remove_vol is renamed to __ubi_remove_vol to allow the new ubi_remove_vol() being used as a wrapper for __ubi_remove_vol() with volume name. Also, comments are added for all exported functions. Reviewed-by: Simon Glass Signed-off-by: Weijie Gao --- cmd/ubi.c | 23 ++++++++++---- include/ubi_uboot.h | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 6 deletions(-) (limited to 'include') diff --git a/cmd/ubi.c b/cmd/ubi.c index 04616d49fac..eea863ac303 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, bool dynamic, - int vol_id, bool skipcheck) +int ubi_create_vol(const char *volume, int64_t size, bool 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, bool 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(struct ubi_volume *vol) +static int __ubi_remove_vol(struct ubi_volume *vol) { int err, reserved_pebs, i; @@ -315,6 +315,17 @@ out_err: return err; } +int ubi_remove_vol(const char *volume) +{ + struct ubi_volume *vol; + + vol = ubi_require_volume(volume); + if (!vol) + return -ENODEV; + + return __ubi_remove_vol(vol); +} + static int ubi_rename_vol(const char *oldname, const char *newname) { struct ubi_volume *vol; @@ -632,7 +643,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 /* @@ -828,7 +839,7 @@ static int do_ubi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) vol_id = vol->vol_id; - ret = ubi_remove_vol(vol); + ret = __ubi_remove_vol(vol); if (ret) return CMD_RET_FAILURE; diff --git a/include/ubi_uboot.h b/include/ubi_uboot.h index 6ebd8a3b613..bdf5645de3d 100644 --- a/include/ubi_uboot.h +++ b/include/ubi_uboot.h @@ -47,11 +47,102 @@ int ubi_mtd_param_parse(const char *val, struct kernel_param *kp); int ubi_init(void); void ubi_exit(void); + +/** + * ubi_detach() - detach UBI from MTD partition + * + * This function performs the cleanup of the UBI subsystem to make sure the + * MTD partition can be safely used for another purpose, or be attached again + * with ubi_part(). + * + * Any mounted UBIFS will be unmounted automatically. + * + * Return: 0 + */ +int ubi_detach(void); + +/** + * ubi_part() - attach UBI to MTD partition + * @part_name: name of the MTD partition to attach + * @vid_header_offset: VID header offset (string) + * + * This function detaches any existing UBI device, then probes for the + * specified MTD partition, and then scans it to initialize UBI. + * + * @vid_header_offset is optional and is usually set to NULL. + * + * Return: 0 on success, or -ve on error. + */ int ubi_part(const char *part_name, const char *vid_header_offset); + +/** + * ubi_volume_write() - write data to UBI volume + * @volume: name of the volume to write to + * @buf: data buffer to be written + * @offset: start offset for writing + * @size: number of bytes to write + * + * This function writes data to the specific UBI volume. If the offset is zero, + * it initiates a full volume update. Otherwise, it performs an offset-based + * write using LEB changes. + * + * Return: 0 on success, or -ve on error. + */ int ubi_volume_write(const char *volume, const void *buf, loff_t offset, size_t size); + +/** + * ubi_volume_read() - read data from UBI volume + * @volume: name of the volume to read from + * @buf: buffer to hold the read data + * @offset: start offset for reading + * @size: number of bytes to read + * + * This function reads data from the specified UBI volume. If @size is zero, + * the function reads the entire volume content starting from @offset. + * + * Return: 0 on success, or -ve on error. + */ int ubi_volume_read(const char *volume, void *buf, loff_t offset, size_t size); +/** + * ubi_create_vol() - create UBI volume + * @volume: name of the volume to create + * @size: size of the volume in bytes + * @dynamic: create dynamic volume if set to true + * @vol_id: volume ID + * @skipcheck: skip CRC check on this volume if set to true + * + * This function creates a new UBI volume with the specified parameters. + * If @size is negative, all available space will be used. + * For volume ID auto assignment, pass %UBI_VOL_NUM_AUTO to @vol_id. + * + * Return: 0 on success, or -ve on error. + */ +int ubi_create_vol(const char *volume, int64_t size, bool dynamic, int vol_id, + bool skipcheck); + +/** + * ubi_find_volume() - find UBI volume by name + * @volume: name of the volume to find + * + * This function searches for a UBI volume with the specified name in the + * current UBI device. + * + * Return: pointer to the UBI volume structure, or %NULL if not found. + */ +struct ubi_volume *ubi_find_volume(const char *volume); + +/** + * ubi_remove_vol() - remove UBI volume + * @volume: name of the volume to remove + * + * This function removes an existing UBI volume from the current UBI device. + * + * Return: 0 on success, or -ve on error. + */ +int ubi_remove_vol(const char *volume); + extern struct ubi_device *ubi_devices[]; int cmd_ubifs_mount(const char *vol_name); int cmd_ubifs_umount(void); -- cgit v1.3.1