summaryrefslogtreecommitdiff
path: root/packages/uboot/u-boot-mkimage-openmoko-native/nand-createbbt.patch
blob: 74b79da0a9dfbb1654d9f06ebc2eb47fd8796ca6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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

Experimental.

- Werner Almesberger <werner@openmoko.org>


Index: u-boot/common/cmd_nand.c
===================================================================
--- u-boot.orig/common/cmd_nand.c	2007-02-16 23:53:28.000000000 +0100
+++ u-boot/common/cmd_nand.c	2007-02-16 23:53:57.000000000 +0100
@@ -163,6 +163,17 @@
 	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;
@@ -228,7 +239,8 @@
 	    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 */
@@ -283,13 +295,23 @@
 			     "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");
 
@@ -458,6 +480,33 @@
 		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;
@@ -478,7 +527,8 @@
 	"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)
Index: u-boot/drivers/nand/nand_bbt.c
===================================================================
--- u-boot.orig/drivers/nand/nand_bbt.c	2007-02-16 23:53:54.000000000 +0100
+++ u-boot/drivers/nand/nand_bbt.c	2007-02-16 23:53:57.000000000 +0100
@@ -795,7 +795,8 @@
 
 	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;