summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2016-10-18 13:46:00 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2016-11-11 09:54:42 +0100
commit0d68468c9613d0307f1d05f4dce6e583edd3cf07 (patch)
treef0987ad4102f60b160bb125c3f287069e0535edd
parent6f58e5cac9742a48ef9e1022eaf6e6b1dbae9b96 (diff)
downloadbarebox-0d68468c9613d0307f1d05f4dce6e583edd3cf07.tar.gz
barebox-0d68468c9613d0307f1d05f4dce6e583edd3cf07.tar.xz
mtd: Make UBI detection more robust
When we want to detect if a mtd device contains an UBI image then testing the first block is not enough since it can always happen that UBI has just erased the block before the power failed during last boot. Since UBI only ever erases one block at a time and directly writes the ec header to it afterwards, it shouldn't be necessary to scan the whole device for UBI data. Scan the first 64 blocks. The first non-empty block then must contain UBI data, if instead we find foreign data we assume that no UBI is on that mtd device. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
-rw-r--r--drivers/mtd/core.c35
1 files changed, 24 insertions, 11 deletions
diff --git a/drivers/mtd/core.c b/drivers/mtd/core.c
index 6d04b88553..90b800c7b2 100644
--- a/drivers/mtd/core.c
+++ b/drivers/mtd/core.c
@@ -18,6 +18,7 @@
#include <common.h>
#include <linux/mtd/nand.h>
#include <linux/mtd/mtd.h>
+#include <mtd/mtd-peb.h>
#include <mtd/ubi-user.h>
#include <cmdlinepart.h>
#include <filetype.h>
@@ -622,9 +623,11 @@ static int mtd_detect(struct device_d *dev)
struct mtd_info *mtd = container_of(dev, struct mtd_info, class_dev);
int bufsize = 512;
void *buf;
- int ret;
+ int ret = 0, i;
enum filetype filetype;
- size_t retlen;
+ int npebs = mtd_div_by_eb(mtd->size, mtd);
+
+ npebs = max(npebs, 64);
/*
* Do not try to attach an UBI device if this device has partitions
@@ -636,17 +639,27 @@ static int mtd_detect(struct device_d *dev)
buf = xmalloc(bufsize);
- ret = mtd_read(mtd, 0, bufsize, &retlen, buf);
- if (ret)
- goto out;
+ for (i = 0; i < npebs; i++) {
+ if (mtd_peb_is_bad(mtd, i))
+ continue;
- filetype = file_detect_type(buf, bufsize);
- if (filetype == filetype_ubi) {
- ret = ubi_attach_mtd_dev(mtd, UBI_DEV_NUM_AUTO, 0, 20);
- if (ret == -EEXIST)
- ret = 0;
+ ret = mtd_peb_read(mtd, buf, i, 0, 512);
+ if (ret)
+ continue;
+
+ if (mtd_buf_all_ff(buf, 512))
+ continue;
+
+ filetype = file_detect_type(buf, bufsize);
+ if (filetype == filetype_ubi) {
+ ret = ubi_attach_mtd_dev(mtd, UBI_DEV_NUM_AUTO, 0, 20);
+ if (ret == -EEXIST)
+ ret = 0;
+ }
+
+ break;
}
-out:
+
free(buf);
return ret;