Automatically creating a BBT is the right thing to do if the NAND is factory new. However when migrating from a barebox older than commit v2020.03.0~28^2~1 ("mtd: nand-imx: Create BBT automatically when necessary") on a used machine, this automatism is really bad because it most likely marks the blocks containing the barebox image (and possibly more) as bad. On such a system the vendor BBMs are gone, but it was operated without that information before, so continuing to do so is a sane option. Add a light check for the NAND to be really pristine: If any block looks like containing a barebox image or a UBI refuse to create a BBT. Signed-off-by: Uwe Kleine-König <u.kleine-koenig@xxxxxxxxxxxxxx> --- drivers/mtd/nand/raw/mxc_nand.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/drivers/mtd/nand/raw/mxc_nand.c b/drivers/mtd/nand/raw/mxc_nand.c index faea1c95f95e..f8517df823e5 100644 --- a/drivers/mtd/nand/raw/mxc_nand.c +++ b/drivers/mtd/nand/raw/mxc_nand.c @@ -1575,6 +1575,23 @@ static int checkbad(struct mtd_info *mtd, loff_t ofs) return ret; } + /* + * Automatically adding a BBT based on factory BBTs is only + * sensible if the NAND is pristine. Abort if the first page + * looks like a bootloader or UBI block. + */ + if (is_barebox_arm_head(buf)) { + dev_err(mtd->dev.parent, + "Flash seems to contain a barebox image, refusing to create BBT\n"); + return -EINVAL; + } + + if (!memcmp(buf, "UBI#", 4)) { + dev_err(mtd->dev.parent, + "Flash seems to contain a UBI, refusing to create BBT\n"); + return -EINVAL; + } + if (buf[2000] != 0xff) /* block considered bad */ return 1; -- 2.43.0