--- a/drivers/mtd/ubi/build.c
+++ b/drivers/mtd/ubi/build.c
-@@ -1172,6 +1172,54 @@ static struct mtd_info * __init open_mtd
+@@ -1172,6 +1172,68 @@ static struct mtd_info * __init open_mtd
return mtd;
}
+{
+ int err;
+ struct mtd_info *mtd;
++ loff_t offset = 0;
+ size_t len;
+ char magic[4];
+
+ 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;
+ }
static int __init ubi_init(void)
{
int err, i, k;
-@@ -1255,6 +1303,12 @@ static int __init ubi_init(void)
+@@ -1255,6 +1317,12 @@ static int __init ubi_init(void)
}
}
--- a/drivers/mtd/ubi/build.c
+++ b/drivers/mtd/ubi/build.c
-@@ -1226,6 +1226,54 @@ static struct mtd_info * __init open_mtd
+@@ -1226,6 +1226,68 @@ static struct mtd_info * __init open_mtd
return mtd;
}
+{
+ int err;
+ struct mtd_info *mtd;
++ loff_t offset = 0;
+ size_t len;
+ char magic[4];
+
+ 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;
+ }
static int __init ubi_init(void)
{
int err, i, k;
-@@ -1309,6 +1357,12 @@ static int __init ubi_init(void)
+@@ -1309,6 +1371,12 @@ static int __init ubi_init(void)
}
}