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: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 1/2] mtd: peb: Add function to skip bad blocks
Date: Mon, 28 Jan 2019 14:16:39 +0100	[thread overview]
Message-ID: <20190128131640.3692-1-s.hauer@pengutronix.de> (raw)

This adds a function that given a pointer to a PEB number increases the
number until the corresponding PEB is good. It also checks for the PEB
number being inside the mtd device.

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

diff --git a/drivers/mtd/peb.c b/drivers/mtd/peb.c
index c35b63f2fd..388db7f587 100644
--- a/drivers/mtd/peb.c
+++ b/drivers/mtd/peb.c
@@ -129,6 +129,33 @@ int mtd_num_pebs(struct mtd_info *mtd)
 	return mtd_div_by_eb(mtd->size, mtd);
 }
 
+/**
+ * mtd_skip_bad - skip bad blocks
+ * @mtd: mtd device
+ * @pnum: The number of the block
+ *
+ * This function skips bad blocks beginning from @pnum. Returns 0 for success and
+ * a negative error code otherwise. on successful exit @pnum points to the next
+ * good block.
+ */
+int mtd_skip_bad(struct mtd_info *mtd, int *pnum)
+{
+	if (*pnum < 0)
+		return -EINVAL;
+
+	while (1) {
+		loff_t offset = (uint64_t)mtd->erasesize * *pnum;
+
+		if (offset >= mtd->size)
+			return -ENOSPC;
+
+		if (!mtd_block_isbad(mtd, offset))
+		    return 0;
+
+		*pnum = *pnum + 1;
+	}
+}
+
 /**
  * mtd_peb_mark_bad - mark a physical eraseblock as bad
  * @mtd: mtd device
diff --git a/include/mtd/mtd-peb.h b/include/mtd/mtd-peb.h
index e4fd01df90..23f89d89a8 100644
--- a/include/mtd/mtd-peb.h
+++ b/include/mtd/mtd-peb.h
@@ -12,6 +12,7 @@ int mtd_peb_torture(struct mtd_info *mtd, int pnum);
 int mtd_peb_erase(struct mtd_info *mtd, int pnum);
 int mtd_peb_mark_bad(struct mtd_info *mtd, int pnum);
 int mtd_peb_is_bad(struct mtd_info *mtd, int pnum);
+int mtd_skip_bad(struct mtd_info *mtd, int *pnum);
 int mtd_peb_check_all_ff(struct mtd_info *mtd, int pnum, int offset, int len,
 			 int warn);
 int mtd_peb_verify(struct mtd_info *mtd, const void *buf, int pnum,
-- 
2.20.1


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

             reply	other threads:[~2019-01-28 13:16 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-28 13:16 Sascha Hauer [this message]
2019-01-28 13:16 ` [PATCH 2/2] usb: fastboot: ubiformat: Fix bad block handling 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=20190128131640.3692-1-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox