[RFC PATCH 07/20] cmd: ubi: add ubi_part_from_mtd()

Daniel Golle daniel at makrotopia.org
Mon Feb 16 22:22:25 CET 2026


Add ubi_part_from_mtd() which attaches UBI to a given struct mtd_info
directly, without going through the name-based lookup that ubi_part()
uses. This is useful for callers that already hold a reference to the
MTD device, e.g. after finding it via its device-tree node.

If the same MTD device is already attached, the function returns
immediately.

Declared in include/ubi_uboot.h alongside ubi_part().

Signed-off-by: Daniel Golle <daniel at makrotopia.org>
---
 cmd/ubi.c           | 23 +++++++++++++++++++++++
 include/ubi_uboot.h |  1 +
 2 files changed, 24 insertions(+)

diff --git a/cmd/ubi.c b/cmd/ubi.c
index ca7d657aa07..9f73c36aaa9 100644
--- a/cmd/ubi.c
+++ b/cmd/ubi.c
@@ -662,6 +662,29 @@ static int ubi_detach(void)
 	return 0;
 }
 
+int ubi_part_from_mtd(struct mtd_info *mtd)
+{
+	int err;
+
+	if (ubi && ubi->mtd == mtd) {
+		printf("UBI partition '%s' already selected\n", mtd->name);
+		return 0;
+	}
+
+	ubi_detach();
+
+	err = ubi_dev_scan(mtd, NULL);
+	if (err) {
+		printf("UBI init error %d\n", err);
+		printf("Please check, if the correct MTD partition is used (size big enough?)\n");
+		return err;
+	}
+
+	ubi = ubi_devices[0];
+
+	return 0;
+}
+
 int ubi_part(char *part_name, const char *vid_header_offset)
 {
 	struct mtd_info *mtd;
diff --git a/include/ubi_uboot.h b/include/ubi_uboot.h
index 92892704d0a..8f3fb007b0e 100644
--- a/include/ubi_uboot.h
+++ b/include/ubi_uboot.h
@@ -48,6 +48,7 @@ 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);
+int ubi_part_from_mtd(struct mtd_info *mtd);
 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);
 struct ubi_volume *ubi_find_volume(const char *volume);
-- 
2.53.0


More information about the U-Boot mailing list