* [PATCH 1/2] mtd: nand-imx: Cleanup and simplify code creating bad block table
@ 2024-04-24 16:47 Uwe Kleine-König
2024-04-24 16:47 ` [PATCH 2/2] mtd: nand-imx: Only automatically create BBT if NAND seems to be pristine Uwe Kleine-König
0 siblings, 1 reply; 2+ messages in thread
From: Uwe Kleine-König @ 2024-04-24 16:47 UTC (permalink / raw)
To: barebox
Let the variable "numblocks" contain the number of blocks of the NAND
device (and not twice this value). Also the loop variable counts blocks
now instead of the doubled value. Further calculate the offset of the
i-th block from i instead of incrementing in each step and pick a more
sensbile variable name for it.
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
drivers/mtd/nand/nand_imx.c | 22 +++++++++++-----------
1 file changed, 11 insertions(+), 11 deletions(-)
diff --git a/drivers/mtd/nand/nand_imx.c b/drivers/mtd/nand/nand_imx.c
index 77655daa4efe..b26889358b3f 100644
--- a/drivers/mtd/nand/nand_imx.c
+++ b/drivers/mtd/nand/nand_imx.c
@@ -1194,31 +1194,31 @@ static int imxnd_create_bbt(struct nand_chip *chip)
{
struct mtd_info *mtd = nand_to_mtd(chip);
int len, i, numblocks, ret;
- loff_t from = 0;
uint8_t *bbt;
- len = mtd->size >> (chip->bbt_erase_shift + 2);
+ numblocks = mtd->size >> chip->bbt_erase_shift;
- /* Allocate memory (2bit per block) and clear the memory bad block table */
+ /*
+ * Allocate memory (2bit per block = 1 byte per 4 blocks) and clear the
+ * memory bad block table
+ */
+ len = (numblocks + 3) >> 2;
bbt = kzalloc(len, GFP_KERNEL);
if (!bbt)
return -ENOMEM;
- numblocks = mtd->size >> (chip->bbt_erase_shift - 1);
+ for (i = 0; i < numblocks; ++i) {
+ loff_t ofs = i << chip->bbt_erase_shift;
- for (i = 0; i < numblocks;) {
- ret = checkbad(chip, from);
+ ret = checkbad(chip, ofs);
if (ret < 0)
goto out;
if (ret) {
- bbt[i >> 3] |= 0x03 << (i & 0x6);
+ bbt[i >> 2] |= 0x03 << (2 * (i & 0x3));
dev_info(mtd->dev.parent, "Bad eraseblock %d at 0x%08x\n",
- i >> 1, (unsigned int)from);
+ i, (unsigned int)ofs);
}
-
- i += 2;
- from += (1 << chip->bbt_erase_shift);
}
chip->bbt_td->options |= NAND_BBT_CREATE;
base-commit: f40319c8e157c90117d32aed1dae5549a723c63e
prerequisite-patch-id: acee91c52d3f79ce61961584c6712403adad66ef
--
2.43.0
^ permalink raw reply [flat|nested] 2+ messages in thread
* [PATCH 2/2] mtd: nand-imx: Only automatically create BBT if NAND seems to be pristine
2024-04-24 16:47 [PATCH 1/2] mtd: nand-imx: Cleanup and simplify code creating bad block table Uwe Kleine-König
@ 2024-04-24 16:47 ` Uwe Kleine-König
0 siblings, 0 replies; 2+ messages in thread
From: Uwe Kleine-König @ 2024-04-24 16:47 UTC (permalink / raw)
To: barebox
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 the first
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@pengutronix.de>
---
drivers/mtd/nand/nand_imx.c | 59 ++++++++++++++++++++-----------------
1 file changed, 32 insertions(+), 27 deletions(-)
diff --git a/drivers/mtd/nand/nand_imx.c b/drivers/mtd/nand/nand_imx.c
index b26889358b3f..9a7f82d59029 100644
--- a/drivers/mtd/nand/nand_imx.c
+++ b/drivers/mtd/nand/nand_imx.c
@@ -12,6 +12,7 @@
#include <common.h>
#include <driver.h>
+#include <filetype.h>
#include <malloc.h>
#include <init.h>
#include <linux/mtd/mtd.h>
@@ -1166,30 +1167,6 @@ static int __init mxcnd_probe_dt(struct imx_nand_host *host)
* From this point on we can forget about the BBMs and rely completely
* on the flash BBT.
*/
-static int checkbad(struct nand_chip *chip, loff_t ofs)
-{
- struct mtd_info *mtd = nand_to_mtd(chip);
- int ret;
- uint8_t buf[mtd->writesize + mtd->oobsize];
- struct mtd_oob_ops ops;
-
- ops.mode = MTD_OPS_RAW;
- ops.ooboffs = 0;
- ops.datbuf = buf;
- ops.len = mtd->writesize;
- ops.oobbuf = buf + mtd->writesize;
- ops.ooblen = mtd->oobsize;
-
- ret = mtd_read_oob(mtd, ofs, &ops);
- if (ret < 0)
- return ret;
-
- if (buf[2000] != 0xff)
- return 1;
-
- return 0;
-}
-
static int imxnd_create_bbt(struct nand_chip *chip)
{
struct mtd_info *mtd = nand_to_mtd(chip);
@@ -1209,12 +1186,40 @@ static int imxnd_create_bbt(struct nand_chip *chip)
for (i = 0; i < numblocks; ++i) {
loff_t ofs = i << chip->bbt_erase_shift;
+ uint8_t buf[mtd->writesize + mtd->oobsize];
+ struct mtd_oob_ops ops = {
+ .mode = MTD_OPS_RAW,
+ .ooboffs = 0,
+ .datbuf = buf,
+ .len = mtd->writesize,
+ .oobbuf = buf + mtd->writesize,
+ .ooblen = mtd->oobsize,
+ };
- ret = checkbad(chip, ofs);
- if (ret < 0)
+ ret = mtd_read_oob(mtd, ofs, &ops);
+ if (ret < 0) {
+ dev_err(mtd->dev.parent, "Failed to read page at 0x%08x\n", (unsigned int)ofs);
goto out;
+ }
- if (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 (ofs == 0 && is_barebox_arm_head(buf)) {
+ dev_err(mtd->dev.parent, "Flash seems to contain a barebox image, refusing\n");
+ ret = -EINVAL;
+ goto out;
+ }
+
+ if (ofs == 0 && !memcmp(buf, "UBI#", 4)) {
+ dev_err(mtd->dev.parent, "Flash seems to contain a UBI, refusing\n");
+ ret = -EINVAL;
+ goto out;
+ }
+
+ if (buf[2000] != 0xff) {
bbt[i >> 2] |= 0x03 << (2 * (i & 0x3));
dev_info(mtd->dev.parent, "Bad eraseblock %d at 0x%08x\n",
i, (unsigned int)ofs);
--
2.43.0
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-04-24 16:48 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-24 16:47 [PATCH 1/2] mtd: nand-imx: Cleanup and simplify code creating bad block table Uwe Kleine-König
2024-04-24 16:47 ` [PATCH 2/2] mtd: nand-imx: Only automatically create BBT if NAND seems to be pristine Uwe Kleine-König
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox