[PATCH 1/3] cmd: mtd: add markbad command support
Mikhail Kshevetskiy
mikhail.kshevetskiy at iopsys.eu
Thu Jun 27 13:26:50 CEST 2024
Some nand flashes (like spi-nand one) are registered with mtd
subsystem only, thus nand command can't be used to work with
such flashes. As result some functionality is missing.
This patch implements 'nand markbad' functionality for mtd command.
Signed-off-by: Mikhail Kshevetskiy <mikhail.kshevetskiy at iopsys.eu>
---
cmd/Kconfig | 6 ++++++
cmd/mtd.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 64 insertions(+)
diff --git a/cmd/Kconfig b/cmd/Kconfig
index 6834bbd82f3..70c2d6a2dda 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -1418,6 +1418,12 @@ config CMD_MTD_OTP
help
MTD commands for OTP access.
+config CMD_MTD_MARKBAD
+ bool "mtd markbad"
+ depends on CMD_MTD
+ help
+ MTD markbad command support.
+
config CMD_MUX
bool "mux"
depends on MULTIPLEXER
diff --git a/cmd/mtd.c b/cmd/mtd.c
index 9189f45cabd..18f00493780 100644
--- a/cmd/mtd.c
+++ b/cmd/mtd.c
@@ -692,6 +692,57 @@ out_put_mtd:
return ret;
}
+#ifdef CONFIG_CMD_MTD_MARKBAD
+static int do_mtd_markbad(struct cmd_tbl *cmdtp, int flag, int argc,
+ char *const argv[])
+{
+ struct mtd_info *mtd;
+ loff_t off;
+ int ret = 0;
+
+ if (argc < 3)
+ return CMD_RET_USAGE;
+
+ mtd = get_mtd_by_name(argv[1]);
+ if (IS_ERR_OR_NULL(mtd))
+ return CMD_RET_FAILURE;
+
+ if (!mtd_can_have_bb(mtd)) {
+ printf("Only NAND-based devices can have bad blocks\n");
+ goto out_put_mtd;
+ }
+
+ argc -= 2;
+ argv += 2;
+ while (argc > 0) {
+ off = hextoul(argv[0], NULL);
+ if (!mtd_is_aligned_with_block_size(mtd, off)) {
+ printf("Offset not aligned with a block (0x%x)\n",
+ mtd->erasesize);
+ ret = CMD_RET_FAILURE;
+ goto out_put_mtd;
+ }
+
+ ret = mtd_block_markbad(mtd, off);
+ if (ret) {
+ printf("block 0x%08llx NOT marked as bad! ERROR %d\n",
+ off, ret);
+ ret = CMD_RET_FAILURE;
+ } else {
+ printf("block 0x%08llx successfully marked as bad\n",
+ off);
+ }
+ --argc;
+ ++argv;
+ }
+
+out_put_mtd:
+ put_mtd_device(mtd);
+
+ return ret;
+}
+#endif
+
static int do_mtd_bad(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{
@@ -774,6 +825,9 @@ U_BOOT_LONGHELP(mtd,
"mtd otpwrite <name> <off> <hex string>\n"
"mtd otplock <name> <off> <size>\n"
"mtd otpinfo <name> [u|f]\n"
+#endif
+#ifdef CONFIG_CMD_MTD_MARKBAD
+ "mtd markbad <name> <off> [<off> ...]\n"
#endif
"\n"
"With:\n"
@@ -808,5 +862,9 @@ U_BOOT_CMD_WITH_SUBCMDS(mtd, "MTD utils", mtd_help_text,
mtd_name_complete),
U_BOOT_SUBCMD_MKENT_COMPLETE(erase, 4, 0, do_mtd_erase,
mtd_name_complete),
+#ifdef CONFIG_CMD_MTD_MARKBAD
+ U_BOOT_SUBCMD_MKENT_COMPLETE(markbad, 20, 0, do_mtd_markbad,
+ mtd_name_complete),
+#endif
U_BOOT_SUBCMD_MKENT_COMPLETE(bad, 2, 1, do_mtd_bad,
mtd_name_complete));
--
2.43.0
More information about the U-Boot
mailing list