[PATCH v2] cmd: mtd: check if a block has to be skipped or erased

Dario Binacchi dario.binacchi at amarulasolutions.com
Mon Oct 24 11:35:21 CEST 2022


From: Mikhail Kshevetskiy <mikhail.kshevetskiy at iopsys.eu>

As reported by patch [1], the `mtd erase' command should not erase bad
blocks.
To force bad block erasing you have to use the `mtd erase.dontskipbad'
command.

This patch tries to fix the same issue without modifying code taken
from the linux kernel, in order to make further upgrades easier.

[1] https://lore.kernel.org/all/20221006031501.110290-2-mikhail.kshevetskiy@iopsys.eu/
Suggested-by: Michael Trimarchi <michael at amarulasolutions.com>
Co-developed-by: Michael Trimarchi <michael at amarulasolutions.com>
Signed-off-by: Michael Trimarchi <michael at amarulasolutions.com>
Co-developed-by: Dario Binacchi <dario.binacchi at amarulasolutions.com>
Signed-off-by: Dario Binacchi <dario.binacchi at amarulasolutions.com>
Tested-by: Mikhail Kshevetskiy <mikhail.kshevetskiy at iopsys.eu>
Signed-off-by: Mikhail Kshevetskiy <mikhail.kshevetskiy at iopsys.eu>

---

Changes in v2:
- Change the commit author
- Do not continue to erase if scrub option is enabled and a bad block
  was found but return from the function.
- Update the patch tags.

 cmd/mtd.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/cmd/mtd.c b/cmd/mtd.c
index ad5cc9827d55..a314745e95e1 100644
--- a/cmd/mtd.c
+++ b/cmd/mtd.c
@@ -434,11 +434,24 @@ static int do_mtd_erase(struct cmd_tbl *cmdtp, int flag, int argc,
 	erase_op.mtd = mtd;
 	erase_op.addr = off;
 	erase_op.len = mtd->erasesize;
-	erase_op.scrub = scrub;
 
 	while (len) {
-		ret = mtd_erase(mtd, &erase_op);
+		if (!scrub) {
+			ret = mtd_block_isbad(mtd, erase_op.addr);
+			if (ret < 0) {
+				printf("Failed to get bad block at 0x%08llx\n",
+				       erase_op.addr);
+				ret = CMD_RET_FAILURE;
+				goto out_put_mtd;
+			} else if (ret > 0) {
+				/* simulate bad block behavior */
+				ret = -EIO;
+				goto skip_block_erasing;
+			}
+		}
 
+		ret = mtd_erase(mtd, &erase_op);
+skip_block_erasing:
 		if (ret) {
 			/* Abort if its not a bad block error */
 			if (ret != -EIO)
-- 
2.32.0



More information about the U-Boot mailing list