summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2019-01-28 14:07:54 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2019-01-28 14:23:37 +0100
commiteaa3484ad66da8912e19fa5a4bdceea86dddc4d4 (patch)
tree65e8c870a248e2c7e65c830e7339d52742666f1d
parent1b6253ab991d272adf8c84b0a6063b4076253d61 (diff)
downloadbarebox-eaa3484ad66da8912e19fa5a4bdceea86dddc4d4.tar.gz
barebox-eaa3484ad66da8912e19fa5a4bdceea86dddc4d4.tar.xz
mtd: peb: Add function to skip bad blocks
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>
-rw-r--r--drivers/mtd/peb.c27
-rw-r--r--include/mtd/mtd-peb.h1
2 files changed, 28 insertions, 0 deletions
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
@@ -130,6 +130,33 @@ int mtd_num_pebs(struct mtd_info *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
* @pnum: The number of the block
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,