[U-Boot-Users] [PATCH] add explicit bbt creation to commandline

Harald Welte laforge at gnumonks.org
Sun Jul 6 10:04:56 CEST 2008


[PATCH] add explicit bbt creation to commandline ("nand createbbt" command)

This patch adds user-requested BBT creation. It includes the following changes:
    
- common/cmd_nand.c: move yes/no decision to separate function
- do_nand: ask for confirmation for "nand erase"
- do_nand: add command "nand createbbt" to erase NAND and create a new BBT
    
Signed-off-by: Werner Almesberger <werner at openmoko.org>
Signed-off-by: Harald Welte <laforge at openmoko.org>

---

diff --git a/common/cmd_nand.c b/common/cmd_nand.c
index bb46f34..2f41157 100644
--- a/common/cmd_nand.c
+++ b/common/cmd_nand.c
@@ -162,6 +162,17 @@ out:
 	return 0;
 }
 
+static int yes(void)
+{
+	char c;
+
+	c = getc();
+	if (c != 'y' && c != 'Y')
+		return 0;
+	c = getc();
+	return c == '\r' || c == '\n';
+}
+
 int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
 {
 	int i, dev, ret;
@@ -231,7 +242,8 @@ int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
 	    strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0 &&
 	    strcmp(cmd, "scrub") != 0 && strcmp(cmd, "markbad") != 0 &&
 	    strcmp(cmd, "biterr") != 0 &&
-	    strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0 )
+	    strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0 &&
+	    strcmp(cmd, "createbbt") != 0 )
 		goto usage;
 
 	/* the following commands operate on the current device */
@@ -286,13 +298,23 @@ int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
 			     "are sure of what you are doing!\n"
 			     "\nReally scrub this NAND flash? <y/N>\n");
 
-			if (getc() == 'y' && getc() == '\r') {
+			if (yes()) {
 				opts.scrub = 1;
 			} else {
 				puts("scrub aborted\n");
 				return -1;
 			}
 		}
+		else {
+			if (opts.length == nand->size) {
+				puts("Really erase everything ? <y/N>\n");
+				if (!yes()) {
+					puts("erase aborted\n");
+					return -1;
+				}
+			}
+		}
+
 		ret = nand_erase_opts(nand, &opts);
 		printf("%s\n", ret ? "ERROR" : "OK");
 
@@ -461,6 +483,33 @@ int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
 		return 0;
 	}
 
+	if (strcmp(cmd, "createbbt") == 0) {
+		struct nand_chip *nand_chip = nand->priv;
+		nand_erase_options_t opts;
+
+		puts("Create BBT and erase everything ? <y/N>\n");
+		if (!yes()) {
+			puts("createbbt aborted\n");
+			return -1;
+		}
+		memset(&opts, 0, sizeof(opts));
+		opts.length = nand->size;
+		if (nand_erase_opts(nand, &opts)) {
+			puts("Erase failed\n");
+			return 1;
+		}
+		nand_chip->options &= ~NAND_DONT_CREATE_BBT;
+		puts("Creating BBT. Please wait ...");
+		if (nand_default_bbt(nand)) {
+			puts("\nFailed\n");
+			return 1;
+		}
+		else {
+			puts("\n");
+			return 0;
+		}
+	}
+
 usage:
 	printf("Usage:\n%s\n", cmdtp->usage);
 	return 1;
@@ -481,7 +530,8 @@ U_BOOT_CMD(nand, 5, 1, do_nand,
 	"nand markbad off - mark bad block at offset (UNSAFE)\n"
 	"nand biterr off - make a bit error at offset (UNSAFE)\n"
 	"nand lock [tight] [status] - bring nand to lock state or display locked pages\n"
-	"nand unlock [offset] [size] - unlock section\n");
+	"nand unlock [offset] [size] - unlock section\n"
+	"nand createbbt - create bad block table\n");
 
 static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
 			   ulong offset, ulong addr, char *cmd)
diff --git a/drivers/mtd/nand/nand_bbt.c b/drivers/mtd/nand/nand_bbt.c
index 27d5988..3fdc25a 100644
--- a/drivers/mtd/nand/nand_bbt.c
+++ b/drivers/mtd/nand/nand_bbt.c
@@ -795,7 +795,8 @@ int nand_scan_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd)
 
 	len = mtd->size >> (this->bbt_erase_shift + 2);
 	/* Allocate memory (2bit per block) */
-	this->bbt = kmalloc (len, GFP_KERNEL);
+	if (!this->bbt)
+		this->bbt = kmalloc (len, GFP_KERNEL);
 	if (!this->bbt) {
 		printk (KERN_ERR "nand_scan_bbt: Out of memory\n");
 		return -ENOMEM;
-- 
- Harald Welte <laforge at gnumonks.org>           http://laforge.gnumonks.org/
============================================================================
"Privacy in residential applications is a desirable marketing option."
                                                  (ETSI EN 300 175-7 Ch. A6)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://lists.denx.de/pipermail/u-boot/attachments/20080706/746c7054/attachment.pgp 


More information about the U-Boot mailing list