summaryrefslogtreecommitdiff
path: root/recipes/u-boot/u-boot-mkimage-openmoko-native/nand-dynamic_partitions.patch
blob: ecce004ca680caa0613980ba09aa2c3f11831861 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
This patch adds support for 'dynamic partitions'.  This basically 
works as follows:
* The nand code generates a bad-block-table at the first scan of the chip
* The dynamic partition code calculates the raw partition sizes based on 
  the bad block table.  E.g. if you have a partition of size 0x30000, and there are 
  two bad blocks (0x4000 each) in it, the raw size will increase to 0x38000, and the
  following partitions get shifted towards the end of flash.

Please note that currently the desired partition sizes are stored at compile-time
in an array in drivers/nand/nand_bbt.c, so this definitely needs to change before
submitting/merging upstream.

In order to calculate the partiton map (and set mtdparts accordingly), you can use
the 'dynpart' command at the prompt.  Use 'saveenv' to make the setting permanent.

Signed-off-by: Harald Welte <laforge@openmoko.org>

Index: u-boot/drivers/nand/nand_bbt.c
===================================================================
--- u-boot.orig/drivers/nand/nand_bbt.c
+++ u-boot/drivers/nand/nand_bbt.c
@@ -1044,9 +1044,86 @@
 	switch ((int)res) {
 	case 0x00:	return 0;
 	case 0x01:	return 1;
+	case 0x03:	return 1;
 	case 0x02:	return allowbbt ? 0 : 1;
 	}
 	return 1;
 }
 
+#if defined(CONFIG_NAND_DYNPART)
+
+extern unsigned int dynpart_size[];
+extern char *dynpart_names[];
+
+#define MTDPARTS_MAX_SIZE 512
+
+
+static int skip_offs(const struct nand_chip *this, unsigned int offs)
+{
+	int block = (int) (offs >> (this->bbt_erase_shift - 1));
+	u_int8_t bbt = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03;
+
+	return bbt == 3;
+}
+
+int nand_create_mtd_dynpart(struct mtd_info *mtd)
+{
+	struct nand_chip *this = mtd->priv;
+	int part;
+	char *mtdparts;
+	unsigned int cur_offs = 0;
+
+	mtdparts = malloc(MTDPARTS_MAX_SIZE); /* FIXME: bounds checking */
+	if (!mtdparts)
+		return -ENOMEM;
+
+	sprintf(mtdparts, "mtdparts=" CFG_NAND_DYNPART_MTD_KERNEL_NAME ":");
+
+	for (part = 0; dynpart_size[part] != 0; part++) {
+		unsigned int bb_delta = 0;
+		unsigned int offs = 0;
+		char mtdpart[32];
+
+		for (offs = cur_offs;
+		     offs < cur_offs + dynpart_size[part] + bb_delta;
+		     offs += mtd->erasesize) {
+			if (skip_offs(this, offs))
+				bb_delta += mtd->erasesize;
+		}
+
+		/*
+		 * Absorb bad blocks immediately following this partition also
+		 * into the partition, in order to make next partition start
+		 * with a good block. This simplifies handling of the
+		 * environment partition.
+		 */
+		while (offs < this->chipsize && skip_offs(this, offs)) {
+			bb_delta += mtd->erasesize;
+			offs += mtd->erasesize;
+		}
+
+		if (cur_offs + dynpart_size[part] + bb_delta > this->chipsize)
+			dynpart_size[part] = this->chipsize - cur_offs - bb_delta;
+#if 0
+		printf("partition %u: start = 0x%08x, end=%08x size=%08x, size_inc_bb=%08x\n",
+			part, cur_offs, cur_offs + dynpart_size[part] + bb_delta,
+			dynpart_size[part], dynpart_size[part] + bb_delta);
+#endif
+		cur_offs += dynpart_size[part] + bb_delta;
+		sprintf(mtdpart, "0x%.8x(%.16s),", dynpart_size[part] + bb_delta,
+			dynpart_names[part]);
+		mtdpart[sizeof(mtdpart)-1] = '\0';
+		strncat(mtdparts, mtdpart,
+		    MTDPARTS_MAX_SIZE-strlen(mtdparts)-1);
+	}
+
+	mtdparts[strlen(mtdparts)-1] = '\0';
+	printf("mtdparts %s\n", mtdparts);
+	setenv("mtdparts", mtdparts);
+
+	free(mtdparts);
+	return 0;
+}
+#endif /* CONFIG_NAND_DYNPART */
+
 #endif
Index: u-boot/include/configs/neo1973_gta01.h
===================================================================
--- u-boot.orig/include/configs/neo1973_gta01.h
+++ u-boot/include/configs/neo1973_gta01.h
@@ -99,7 +99,7 @@
 			CFG_CMD_ELF	 | \
 			CFG_CMD_MISC	 | \
 			/* CFG_CMD_USB	 | */ \
-			/* CFG_CMD_JFFS2	 | */ \
+			CFG_CMD_JFFS2	 | \
 			CFG_CMD_DIAG	 | \
 			/* CFG_CMD_HWFLOW	 | */ \
 			CFG_CMD_SAVES	 | \
@@ -212,13 +212,13 @@
 #define CONFIG_FAT		1
 #define CONFIG_SUPPORT_VFAT
 
-#if 0
+#if 1
 /* JFFS2 driver */
 #define CONFIG_JFFS2_CMDLINE	1
 #define CONFIG_JFFS2_NAND	1
 #define CONFIG_JFFS2_NAND_DEV	0
-#define CONFIG_JFFS2_NAND_OFF	0x634000
-#define CONFIG_JFFS2_NAND_SIZE	0x39cc000
+//#define CONFIG_JFFS2_NAND_OFF	0x634000
+//#define CONFIG_JFFS2_NAND_SIZE	0x39cc000
 #endif
 
 /* ATAG configuration */
@@ -257,4 +257,9 @@
 
 #define CONFIG_DRIVER_PCF50606		1
 
+#define MTDIDS_DEFAULT	"nand0=neo1973-nand"
+#define MTPARTS_DEFAULT	"neo1973-nand:256k(u-boot),16k(u-boot_env),2M(kernel),640k(splash),-(jffs2)"
+#define CFG_NAND_DYNPART_MTD_KERNEL_NAME "neo1973-nand"
+#define CONFIG_NAND_DYNPART
+
 #endif	/* __CONFIG_H */
Index: u-boot/common/cmd_jffs2.c
===================================================================
--- u-boot.orig/common/cmd_jffs2.c
+++ u-boot/common/cmd_jffs2.c
@@ -1841,6 +1841,29 @@
 	return NULL;
 }
 
+/* Return the 'net size' of the partition (i.e. excluding any bad blocks) */
+unsigned int nand_net_part_size(struct part_info *part)
+{
+	struct mtd_info *mtd;
+	unsigned int offs;
+	unsigned int bb_delta = 0;
+
+	if (!part || !part->dev || !part->dev->id ||
+	    part->dev->id->num >= CFG_MAX_NAND_DEVICE)
+		return 0;
+
+ 	mtd = &nand_info[part->dev->id->num];
+
+	for (offs = part->offset; offs < part->offset + part->size;
+	     offs += mtd->erasesize) {
+		if (nand_isbad_bbt(mtd, offs, 0))
+			bb_delta += mtd->erasesize;
+	}
+
+	return part->size - bb_delta;
+}
+
+
 /***************************************************/
 /* U-boot commands				   */
 /***************************************************/
@@ -2132,6 +2155,24 @@
 	printf ("Usage:\n%s\n", cmdtp->usage);
 	return 1;
 }
+
+#if defined(CONFIG_NAND_DYNPART)
+extern int nand_create_mtd_dynpart(struct mtd_info *mtd);
+
+int do_dynpart(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
+{
+#if 0
+	int i = simple_strtoul(argv[1], NULL, 0);
+	if (i >= CFG_MAX_NAND_DEVICE)
+		return -EINVAL;
+#endif
+	nand_create_mtd_dynpart(&nand_info[0]);
+
+	return 0;
+}
+#endif /* CONFIG_NAND_DYNPART */
+
+
 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
 
 /***************************************************/
@@ -2197,6 +2238,15 @@
 	"<name>     := '(' NAME ')'\n"
 	"<ro-flag>  := when set to 'ro' makes partition read-only (not used, passed to kernel)\n"
 );
+
+#if defined(CONFIG_NAND_DYNPART)
+U_BOOT_CMD(
+	dynpart, 1,	1,	do_dynpart,
+	"dynpart\t- dynamically calculate partition table based on BBT\n",
+	"\n"
+	"    - sets 'mtdparts' according to BBT\n");
+#endif /* CONFIG_NAND_DYNPART */
+
 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
 
 /***************************************************/
Index: u-boot/common/cmd_nand.c
===================================================================
--- u-boot.orig/common/cmd_nand.c
+++ u-boot/common/cmd_nand.c
@@ -101,7 +101,7 @@
 }
 
 int
-arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, ulong *size)
+arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, ulong *size, int net)
 {
 	int idx = nand_curr_device;
 #if (CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
@@ -122,10 +122,17 @@
 					printf("'%s' is not a number\n", argv[1]);
 					return -1;
 				}
-				if (*size > part->size)
-					*size = part->size;
+				if (*size > part->size) {
+					if (net)
+						*size = nand_net_part_size(part);
+					else
+						*size = part->size;
+				}
 			} else {
-				*size = part->size;
+				if (net)
+					*size = nand_net_part_size(part);
+				else
+					*size = part->size;
 			}
 			idx = dev->id->num;
 			*nand = nand_info[idx];
@@ -261,7 +268,7 @@
 
 		printf("\nNAND %s: ", scrub ? "scrub" : "erase");
 		/* skip first two or three arguments, look for offset and size */
-		if (arg_off_size(argc - o, argv + o, nand, &off, &size) != 0)
+		if (arg_off_size(argc - o, argv + o, nand, &off, &size, 0) != 0)
 			return 1;
 
 		memset(&opts, 0, sizeof(opts));
@@ -323,7 +330,7 @@
 
 		read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
 		printf("\nNAND %s: ", read ? "read" : "write");
-		if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0)
+		if (arg_off_size(argc - 3, argv + 3, nand, &off, &size, 1) != 0)
 			return 1;
 
 		s = strchr(cmd, '.');
@@ -445,7 +452,7 @@
 	}
 
 	if (strcmp(cmd, "unlock") == 0) {
-		if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
+		if (arg_off_size(argc - 2, argv + 2, nand, &off, &size, 0) < 0)
 			return 1;
 
 		if (!nand_unlock(nand, off, size)) {
Index: u-boot/common/cmd_dynenv.c
===================================================================
--- u-boot.orig/common/cmd_dynenv.c
+++ u-boot/common/cmd_dynenv.c
@@ -60,7 +60,7 @@
 		buf[2] = 'V';
 		buf[3] = '0';
 
-		if (arg_off_size(argc-2, argv+2, mtd, &addr, &dummy) < 0) {
+		if (arg_off_size(argc-2, argv+2, mtd, &addr, &dummy, 1) < 0) {
 			printf("Offset or partition name expected\n");
 			goto fail;
 		}
Index: u-boot/include/util.h
===================================================================
--- u-boot.orig/include/util.h
+++ u-boot/include/util.h
@@ -28,6 +28,6 @@
 
 /* common/cmd_nand.c */
 int arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off,
-  ulong *size);
+  ulong *size, int net);
 
 #endif /* UTIL_H */
Index: u-boot/board/qt2410/qt2410.c
===================================================================
--- u-boot.orig/board/qt2410/qt2410.c
+++ u-boot/board/qt2410/qt2410.c
@@ -126,3 +126,9 @@
 
 	return 0;
 }
+
+unsigned int dynpart_size[] = {
+    CFG_UBOOT_SIZE, 0x4000, 0x200000, 0xa0000, 0x3d5c000-CFG_UBOOT_SIZE, 0 };
+char *dynpart_names[] = {
+    "u-boot", "u-boot_env", "kernel", "splash", "rootfs", NULL };
+
Index: u-boot/board/neo1973/gta01/gta01.c
===================================================================
--- u-boot.orig/board/neo1973/gta01/gta01.c
+++ u-boot/board/neo1973/gta01/gta01.c
@@ -429,3 +434,14 @@
 	return 0;
 }
 
+
+/* The sum of all part_size[]s must equal to the NAND size, i.e., 0x4000000.
+   "initrd" is sized such that it can hold two uncompressed 16 bit 640*480
+   images: 640*480*2*2 = 1228800 < 1245184. */
+
+unsigned int dynpart_size[] = {
+    CFG_UBOOT_SIZE, 0x4000, 0x200000, 0xa0000, 0x3d5c000-CFG_UBOOT_SIZE, 0 };
+char *dynpart_names[] = {
+    "u-boot", "u-boot_env", "kernel", "splash", "rootfs", NULL };
+
+
Index: u-boot/include/configs/qt2410.h
===================================================================
--- u-boot.orig/include/configs/qt2410.h
+++ u-boot/include/configs/qt2410.h
@@ -283,5 +283,7 @@
 
 #define MTDIDS_DEFAULT		"nand0=qt2410-nand"
 #define MTPARTS_DEFAULT		"qt2410-nand:192k(u-boot),8k(u-boot_env),2M(kernel),2M(splash),-(jffs2)"
+#define CFG_NAND_DYNPART_MTD_KERNEL_NAME "qt2410-nand"
+#define CONFIG_NAND_DYNPART
 
 #endif	/* __CONFIG_H */