mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Cc: Alexander Shiyan <eagle.alexander923@gmail.com>
Subject: [PATCH 1/2] mtd: peb: Add mtd_peb_read_file()
Date: Wed, 20 Apr 2022 08:38:39 +0200	[thread overview]
Message-ID: <20220420063840.1062067-1-s.hauer@pengutronix.de> (raw)

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


             reply	other threads:[~2022-04-20  6:40 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-20  6:38 Sascha Hauer [this message]
2022-04-20  6:38 ` [PATCH 2/2] ARM: omap: xload: read from unpartitioned device Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220420063840.1062067-1-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=eagle.alexander923@gmail.com \
    --subject='Re: [PATCH 1/2] mtd: peb: Add mtd_peb_read_file()' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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