mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/2] mtd: peb: Add mtd_peb_read_file()
@ 2022-04-20  6:38 Sascha Hauer
  2022-04-20  6:38 ` [PATCH 2/2] ARM: omap: xload: read from unpartitioned device Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Sascha Hauer @ 2022-04-20  6:38 UTC (permalink / raw)
  To: Barebox List; +Cc: Alexander Shiyan

Analog to mtd_peb_write_file() this adds a mtd_peb_read_file()

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/mtd/peb.c     | 45 +++++++++++++++++++++++++++++++++++++++++++
 include/mtd/mtd-peb.h |  2 ++
 2 files changed, 47 insertions(+)

diff --git a/drivers/mtd/peb.c b/drivers/mtd/peb.c
index 6c61e0734c..8443ed86bc 100644
--- a/drivers/mtd/peb.c
+++ b/drivers/mtd/peb.c
@@ -508,6 +508,51 @@ out:
 	return ret;
 }
 
+/**
+ * mtd_peb_read_file - read data from a mtd device
+ * @mtd: mtd device
+ * @peb_start: The first PEB where to start reading
+ * @peb_last: last PEB where to read from
+ * @buf: buffer to read to
+ * @len: how many bytes to read
+ *
+ * This function reads @len bytes of data to buffer @buf from the mtd device
+ * @mtd starting at @peb_start. Reading will stop at @peb_last. This function
+ * skips all bad blocks and returns 0 on success or a negative error code
+ * otherwise.
+ */
+int mtd_peb_read_file(struct mtd_info *mtd, unsigned int peb_start,
+		      unsigned int peb_last, void *buf, size_t len)
+{
+	int ret, pnum;
+
+	pnum = peb_start;
+
+	while (len) {
+		size_t now = min_t(size_t, mtd->erasesize, len);
+
+		if (pnum > peb_last)
+			return -EIO;
+
+		if (mtd_peb_is_bad(mtd, pnum)) {
+			pnum++;
+			continue;
+		}
+
+		ret = mtd_peb_read(mtd, buf, pnum, 0, now);
+		if (ret)
+			goto out;
+
+		len -= now;
+		pnum++;
+		buf += now;
+	}
+
+	ret = 0;
+out:
+	return ret;
+}
+
 /**
  * mtd_peb_erase - erase a physical eraseblock.
  * @mtd: mtd device
diff --git a/include/mtd/mtd-peb.h b/include/mtd/mtd-peb.h
index cfcc0be611..cf8d8ff8da 100644
--- a/include/mtd/mtd-peb.h
+++ b/include/mtd/mtd-peb.h
@@ -23,6 +23,8 @@ int mtd_num_pebs(struct mtd_info *mtd);
 int mtd_peb_create_bitflips(struct mtd_info *mtd, int pnum, int offset,
 				   int len, int num_bitflips, int random,
 				   int info);
+int mtd_peb_read_file(struct mtd_info *mtd, unsigned int peb_start,
+		      unsigned int peb_last, void *buf, size_t len);
 int mtd_peb_write_file(struct mtd_info *mtd, int peb_start, int max_pebs,
 		       const void *buf, size_t len);
 
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH 2/2] ARM: omap: xload: read from unpartitioned device
  2022-04-20  6:38 [PATCH 1/2] mtd: peb: Add mtd_peb_read_file() Sascha Hauer
@ 2022-04-20  6:38 ` Sascha Hauer
  0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2022-04-20  6:38 UTC (permalink / raw)
  To: Barebox List; +Cc: Alexander Shiyan

The omap xload code creates a temporary partition where the barebox
image is read from. Since 7f9f45b9bf it is no longer allowed to create
overlapping partitions which means the temporary partition can no longer
be created when the device was partitioned already. Fix this by using
the mtd PEB api to read the barebox image from the full device and
not from a partition.

Fixes: 7f9f45b9bf ("devfs: Do not create overlapping partitions")
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 arch/arm/mach-omap/xload.c | 86 +++++++++++++++-----------------------
 1 file changed, 33 insertions(+), 53 deletions(-)

diff --git a/arch/arm/mach-omap/xload.c b/arch/arm/mach-omap/xload.c
index af9abf38b5..d786e6ceb2 100644
--- a/arch/arm/mach-omap/xload.c
+++ b/arch/arm/mach-omap/xload.c
@@ -20,6 +20,7 @@
 #include <net.h>
 #include <environment.h>
 #include <dhcp.h>
+#include <mtd/mtd-peb.h>
 
 struct omap_barebox_part *barebox_part;
 
@@ -32,29 +33,6 @@ static struct omap_barebox_part default_part = {
 	.nor_size = SZ_1M,
 };
 
-static void *read_image_head(const char *name)
-{
-	void *header = xmalloc(ARM_HEAD_SIZE);
-	struct cdev *cdev;
-	int ret;
-
-	cdev = cdev_open_by_name(name, O_RDONLY);
-	if (!cdev) {
-		printf("failed to open %s\n", name);
-		return NULL;
-	}
-
-	ret = cdev_read(cdev, header, ARM_HEAD_SIZE, 0, 0);
-	cdev_close(cdev);
-
-	if (ret != ARM_HEAD_SIZE) {
-		printf("failed to read from %s\n", name);
-		return NULL;
-	}
-
-	return header;
-}
-
 static unsigned int get_image_size(void *head)
 {
 	unsigned int ret = 0;
@@ -67,57 +45,62 @@ static unsigned int get_image_size(void *head)
 	return ret;
 }
 
-static void *read_mtd_barebox(const char *partition)
+static void *read_mtd_barebox(const char *part, unsigned int start, unsigned int size)
 {
 	int ret;
-	int size;
-	void *to, *header;
+	void *to;
 	struct cdev *cdev;
+	struct mtd_info *mtd;
+	unsigned int ps, pe;
+
+	cdev = cdev_open_by_name(part, O_RDONLY);
+	if (!cdev) {
+		printf("failed to open partition\n");
+		return NULL;
+	}
 
-	header = read_image_head(partition);
-	if (header == NULL)
+	mtd = cdev->mtd;
+	if (!mtd)
 		return NULL;
 
-	size = get_image_size(header);
-	if (!size) {
-		printf("failed to get image size\n");
+	if (mtd_mod_by_eb(start, mtd) != 0) {
+		printf("Start must be eraseblock aligned\n");
 		return NULL;
 	}
 
 	to = xmalloc(size);
 
-	cdev = cdev_open_by_name(partition, O_RDONLY);
-	if (!cdev) {
-		printf("failed to open partition\n");
-		return NULL;
+	ps = mtd_div_by_eb(start, mtd);
+	pe = mtd_div_by_eb(start + size, mtd);
+	ret = mtd_peb_read_file(mtd, ps, pe, to, size);
+	if (ret) {
+		printf("Can't read image from %s: %d\n", part, ret);
+		goto err;
 	}
 
-	ret = cdev_read(cdev, to, size, 0, 0);
-	if (ret != size) {
-		printf("failed to read from partition\n");
-		return NULL;
+	size = get_image_size(to);
+	if (!size) {
+		printf("failed to get image size\n");
+		goto err;
 	}
 
 	return to;
+
+err:
+	free(to);
+	return NULL;
 }
 
 static void *omap_xload_boot_nand(struct omap_barebox_part *part)
 {
 	void *to;
 
-	devfs_add_partition("nand0", part->nand_offset, part->nand_size,
-					DEVFS_PARTITION_FIXED, "x");
-	dev_add_bb_dev("x", "bbx");
-
-	to = read_mtd_barebox("bbx");
+	to = read_mtd_barebox("nand0", part->nand_offset, part->nand_size);
 	if (to == NULL && part->nand_bkup_size != 0) {
 		printf("trying to load image from backup partition.\n");
-		devfs_add_partition("nand0", part->nand_bkup_offset,
-				part->nand_bkup_size,
-				DEVFS_PARTITION_FIXED, "x_bkup");
-		dev_add_bb_dev("x_bkup", "bbx_bkup");
 
-		to = read_mtd_barebox("bbx_bkup");
+		to = read_mtd_barebox("nand0", part->nand_bkup_offset,
+				part->nand_bkup_size);
 	}
 
 	return to;
@@ -162,10 +145,7 @@ static void *omap_xload_boot_mmc(void)
 
 static void *omap_xload_boot_spi(struct omap_barebox_part *part)
 {
-	devfs_add_partition("m25p0", part->nor_offset, part->nor_size,
-					DEVFS_PARTITION_FIXED, "x");
-
-	return read_mtd_barebox("x");
+	return read_mtd_barebox("m25p0", part->nor_offset, part->nor_size);
 }
 
 static void *omap4_xload_boot_usb(void){
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-04-20  6:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-20  6:38 [PATCH 1/2] mtd: peb: Add mtd_peb_read_file() Sascha Hauer
2022-04-20  6:38 ` [PATCH 2/2] ARM: omap: xload: read from unpartitioned device Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox