[U-Boot] [PATCH] nand/onenand: Fix missing argument checking for "markbad" command

Wolfgang Denk wd at denx.de
Sun May 24 17:34:33 CEST 2009


The "nand markbad" and "onenand markbad" commands did not check if an
argument was passed; if this was forgotten, no error was raised but
block 0 was marked as bad.

While fixing this bug, clean up the code a bit and allow to pass more
than one block address, thus allowing to mark several blocks as bad
in a single command invocation.

Signed-off-by: Wolfgang Denk <wd at denx.de>
---
 common/cmd_nand.c    |   35 +++++++++++++++++++++++------------
 common/cmd_onenand.c |   37 ++++++++++++++++++++++++-------------
 2 files changed, 47 insertions(+), 25 deletions(-)

diff --git a/common/cmd_nand.c b/common/cmd_nand.c
index 1992531..cc35f38 100644
--- a/common/cmd_nand.c
+++ b/common/cmd_nand.c
@@ -414,18 +414,29 @@ int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
 	}
 
 	if (strcmp(cmd, "markbad") == 0) {
-		addr = (ulong)simple_strtoul(argv[2], NULL, 16);
+		argc -= 2;
+		argv += 2;
 
-		int ret = nand->block_markbad(nand, addr);
-		if (ret == 0) {
-			printf("block 0x%08lx successfully marked as bad\n",
-			       (ulong) addr);
-			return 0;
-		} else {
-			printf("block 0x%08lx NOT marked as bad! ERROR %d\n",
-			       (ulong) addr, ret);
+		if (argc <= 0)
+			goto usage;
+
+		while (argc > 0) {
+			addr = simple_strtoul(*argv, NULL, 16);
+
+			if (nand->block_markbad(nand, addr)) {
+				printf("block 0x%08lx NOT marked "
+					"as bad! ERROR %d\n",
+					addr, ret);
+				ret = 1;
+			} else {
+				printf("block 0x%08lx successfully "
+					"marked as bad\n",
+					addr);
+			}
+			--argc;
+			++argv;
 		}
-		return 1;
+		return ret;
 	}
 
 	if (strcmp(cmd, "biterr") == 0) {
@@ -476,7 +487,7 @@ usage:
 	return 1;
 }
 
-U_BOOT_CMD(nand, 5, 1, do_nand,
+U_BOOT_CMD(nand, CONFIG_SYS_MAXARGS, 1, do_nand,
 	   "NAND sub-system",
 	   "info - show available NAND devices\n"
 	   "nand device [dev] - show or set current device\n"
@@ -489,7 +500,7 @@ U_BOOT_CMD(nand, 5, 1, do_nand,
 	   "nand bad - show bad blocks\n"
 	   "nand dump[.oob] off - dump page\n"
 	   "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"
-	   "nand markbad off - mark bad block at offset (UNSAFE)\n"
+	   "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n"
 	   "nand biterr off - make a bit error at offset (UNSAFE)\n"
 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
 	   "nand lock [tight] [status]\n"
diff --git a/common/cmd_onenand.c b/common/cmd_onenand.c
index 5832ff8..c4f158e 100644
--- a/common/cmd_onenand.c
+++ b/common/cmd_onenand.c
@@ -336,7 +336,7 @@ int do_onenand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
 	int blocksize;
 	ulong addr, ofs;
 	size_t len, retlen = 0;
-	int ret;
+	int ret = 0;
 	char *cmd, *s;
 
 	mtd = &onenand_mtd;
@@ -430,18 +430,29 @@ int do_onenand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
 		}
 
 		if (strcmp(cmd, "markbad") == 0) {
-			addr = (ulong)simple_strtoul(argv[2], NULL, 16);
+			argc -= 2;
+			argv += 2;
 
-			int ret = mtd->block_markbad(mtd, addr);
-			if (ret == 0) {
-				printf("block 0x%08lx successfully marked as bad\n",
-						(ulong) addr);
-				return 0;
-			} else {
-				printf("block 0x%08lx NOT marked as bad! ERROR %d\n",
-						(ulong) addr, ret);
+			if (argc <= 0)
+				goto usage;
+
+			while (argc > 0) {
+				addr = simple_strtoul(*argv, NULL, 16);
+
+				if (mtd->block_markbad(mtd, addr)) {
+					printf("block 0x%08lx NOT marked "
+						"as bad! ERROR %d\n",
+						addr, ret);
+					ret = 1;
+				} else {
+					printf("block 0x%08lx successfully "
+						"marked as bad\n",
+						addr);
+				}
+				--argc;
+				++argv;
 			}
-			return 1;
+			return ret;
 		}
 
 		if (strncmp(cmd, "dump", 4) == 0) {
@@ -470,7 +481,7 @@ usage:
 }
 
 U_BOOT_CMD(
-	onenand,	6,	1,	do_onenand,
+	onenand,	CONFIG_SYS_MAXARGS,	1,	do_onenand,
 	"OneNAND sub-system",
 	"info - show available OneNAND devices\n"
 	"onenand bad - show bad blocks\n"
@@ -482,5 +493,5 @@ U_BOOT_CMD(
 	"onenand test [off size] - test 'size' bytes from\n"
 	"    offset 'off' (entire device if not specified)\n"
 	"onenand dump[.oob] off - dump page\n"
-	"onenand markbad off - mark bad block at offset (UNSAFE)\n"
+	"onenand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n"
 );
-- 
1.6.0.6



More information about the U-Boot mailing list