[OpenWrt-Devel] [PATCH 2/3] kernel: handle bad blocks in ubi auto attach code

Mathias Kresin dev at kresin.me
Thu Jul 19 13:05:33 EDT 2018


The first block(s) of the ubi mtd device might be bad. We need to take
care on our own to skip the bad block(s) and read the next one(s).

Don't treat recoverable read errors as fatal and check for the UBI magic
if the data of a block could be recovered using ECC or similar.

Signed-off-by: Mathias Kresin <dev at kresin.me>
---
 ...tach-mtd-device-named-ubi-or-data-on-boot.patch | 22 ++++++++++++++++++----
 ...tach-mtd-device-named-ubi-or-data-on-boot.patch | 22 ++++++++++++++++++----
 2 files changed, 36 insertions(+), 8 deletions(-)

diff --git a/target/linux/generic/pending-4.14/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch b/target/linux/generic/pending-4.14/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch
index 83b0c72..b6890e3 100644
--- a/target/linux/generic/pending-4.14/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch
+++ b/target/linux/generic/pending-4.14/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch
@@ -8,7 +8,7 @@ Signed-off-by: Daniel Golle <daniel at makrotopia.org>
 
 --- a/drivers/mtd/ubi/build.c
 +++ b/drivers/mtd/ubi/build.c
-@@ -1171,6 +1171,53 @@ static struct mtd_info * __init open_mtd
+@@ -1171,6 +1171,67 @@ static struct mtd_info * __init open_mtd
  	return mtd;
  }
  
@@ -20,6 +20,7 @@ Signed-off-by: Daniel Golle <daniel at makrotopia.org>
 +{
 +	int err;
 +	struct mtd_info *mtd;
++	loff_t offset = 0;
 +	size_t len;
 +	char magic[4];
 +
@@ -31,9 +32,22 @@ Signed-off-by: Daniel Golle <daniel at makrotopia.org>
 +	if (IS_ERR(mtd))
 +		return;
 +
++	/* get the first not bad block */
++	if (mtd_can_have_bb(mtd))
++		while (mtd_block_isbad(mtd, offset)) {
++			offset += mtd->erasesize;
++
++			if (offset > mtd->size) {
++				pr_err("UBI error: Failed to find a non-bad "
++				       "block on mtd%d\n", mtd->index);
++				goto cleanup;
++			}
++		}
++
 +	/* check for a valid ubi magic if read from flash was successful */
-+	err = mtd_read(mtd, 0, 4, &len, (void *) magic);
-+	if (!err && len == 4 && strncmp(magic, "UBI#", 4)) {
++	err = mtd_read(mtd, offset, 4, &len, (void *) magic);
++	if ((!err || mtd_is_bitflip(err)) &&
++	    len == 4 && strncmp(magic, "UBI#", 4)) {
 +		pr_err("UBI error: no valid UBI magic found inside mtd%d\n", mtd->index);
 +		goto cleanup;
 +	}
@@ -62,7 +76,7 @@ Signed-off-by: Daniel Golle <daniel at makrotopia.org>
  static int __init ubi_init(void)
  {
  	int err, i, k;
-@@ -1254,6 +1301,12 @@ static int __init ubi_init(void)
+@@ -1254,6 +1315,12 @@ static int __init ubi_init(void)
  		}
  	}
  
diff --git a/target/linux/generic/pending-4.9/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch b/target/linux/generic/pending-4.9/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch
index d6585b1..4051a01 100644
--- a/target/linux/generic/pending-4.9/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch
+++ b/target/linux/generic/pending-4.9/490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch
@@ -8,7 +8,7 @@ Signed-off-by: Daniel Golle <daniel at makrotopia.org>
 
 --- a/drivers/mtd/ubi/build.c
 +++ b/drivers/mtd/ubi/build.c
-@@ -1226,6 +1226,53 @@ static struct mtd_info * __init open_mtd
+@@ -1226,6 +1226,67 @@ static struct mtd_info * __init open_mtd
  	return mtd;
  }
  
@@ -20,6 +20,7 @@ Signed-off-by: Daniel Golle <daniel at makrotopia.org>
 +{
 +	int err;
 +	struct mtd_info *mtd;
++	loff_t offset = 0;
 +	size_t len;
 +	char magic[4];
 +
@@ -31,9 +32,22 @@ Signed-off-by: Daniel Golle <daniel at makrotopia.org>
 +	if (IS_ERR(mtd))
 +		return;
 +
++	/* get the first not bad block */
++	if (mtd_can_have_bb(mtd))
++		while (mtd_block_isbad(mtd, offset)) {
++			offset += mtd->erasesize;
++
++			if (offset > mtd->size) {
++				pr_err("UBI error: Failed to find a non-bad "
++				       "block on mtd%d\n", mtd->index);
++				goto cleanup;
++			}
++		}
++
 +	/* check for a valid ubi magic if read from flash was successful */
-+	err = mtd_read(mtd, 0, 4, &len, (void *) magic);
-+	if (!err && len == 4 && strncmp(magic, "UBI#", 4)) {
++	err = mtd_read(mtd, offset, 4, &len, (void *) magic);
++	if ((!err || mtd_is_bitflip(err)) &&
++	    len == 4 && strncmp(magic, "UBI#", 4)) {
 +		pr_err("UBI error: no valid UBI magic found inside mtd%d\n", mtd->index);
 +		goto cleanup;
 +	}
@@ -62,7 +76,7 @@ Signed-off-by: Daniel Golle <daniel at makrotopia.org>
  static int __init ubi_init(void)
  {
  	int err, i, k;
-@@ -1309,6 +1356,12 @@ static int __init ubi_init(void)
+@@ -1309,6 +1370,12 @@ static int __init ubi_init(void)
  		}
  	}
  
-- 
2.7.4


_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel



More information about the openwrt-devel mailing list