[U-Boot-Users] [PATCH, resend] Support dynamic/patched NAND ENV offset

Harald Welte
2008-07-09T07:25:10Z

On Wed, Jul 09, 2008 at 09:05:24AM +0200, Wolfgang Denk wrote:
> In message <20080709002357.GS25698@prithivi.gnumonks.org> you wrote:
> >
> > More precisely, the flow of events in a full dynenv + dynpart setup
> > (like the three openmoko devices so far) is:
> >
> > 1) u-boot creates a bad block table as part of the production process
> > of the device
> > 2) afterwards, the device-unique partition table is created (net
> > partition sizes as per board-level spec), including an environment
> > partition.
>
> How do you "create" the partition table? Do you use the "mtdparts"
> command for this?

There is a new 'dynpart' command, which when executed uses the
compile-time board-level net partiton sizes, combined with the
bad-block-table to generate the device-specific 'dynamic' partition
table. The result is stored in the mtdparts environment variable.
Everything else is standard u-boot/kernel behaviour.

Please see the attached patch (just for reference, not inclusion yet)
for the details of the implementation. This code was used successfully
in the production/flashing of a couple of thousand devices, using
the Samsung NAND flash chips I mentioned (up to 1.3MByte of bad blocks
within 64MByte of total flash). We have seen quite a number of
different device-specific partition tables, and everything has worked
fine so far.

Just like with the 'dynenv' patch, I think it is something quite useful
(if not neccessarry) for the economic large-scale production of
NAND-flash only devices. We can always talk about the implementation
details, and I'm willing to address any feedback regarding it.

> > 3) the standard regular partition table is stored in the environment
>
> How do you store it? In the same way as "mtdparts" is working?

yes.

--
- Harald Welte <laforge@openmoko.org> http://openmoko.org/
============================================================================
Software for the world's first truly open Free Software mobile phone
-------------- next part --------------
commit e05835df019027391f58f9d8ce5e1257d6924798
Author: Harald Welte <laforge@openmoko.org>
Date: Fri Apr 11 14:26:48 2008 +0100

nand-dynamic_partitions.patch
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>

diff --git a/board/neo1973/gta01/gta01.c b/board/neo1973/gta01/gta01.c
index 598574a..206a0d4 100644
--- a/board/neo1973/gta01/gta01.c
+++ b/board/neo1973/gta01/gta01.c
@@ -494,3 +494,14 @@ void neo1973_led(int led, int on)
}

+
+/* 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 };
+
+
diff --git a/board/qt2410/qt2410.c b/board/qt2410/qt2410.c
index 8c0e77f..9b3df4e 100644
--- a/board/qt2410/qt2410.c
+++ b/board/qt2410/qt2410.c
@@ -150,3 +150,9 @@ int dram_init (void)

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 };
+
diff --git a/common/cmd_dynenv.c b/common/cmd_dynenv.c
index 4dd6148..91f7623 100644
--- a/common/cmd_dynenv.c
+++ b/common/cmd_dynenv.c
@@ -60,7 +60,7 @@ int do_dynenv(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
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;
}
diff --git a/common/cmd_jffs2.c b/common/cmd_jffs2.c
index 1b67e73..ca7d5a6 100644
--- a/common/cmd_jffs2.c
+++ b/common/cmd_jffs2.c
@@ -1838,6 +1838,29 @@ static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int pa
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 */
/***************************************************/
@@ -2129,6 +2152,30 @@ int do_jffs2_mtdparts(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
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[])
+{
+ extern void dfu_update_strings(void);
+
+#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]);
+
+#ifdef CONFIG_USBD_DFU
+ dfu_update_strings();
+#endif
+
+ return 0;
+}
+#endif /* CONFIG_NAND_DYNPART */
+
+
#endif /* #ifdef CONFIG_JFFS2_CMDLINE */

/***************************************************/
@@ -2194,6 +2241,15 @@ U_BOOT_CMD(
"<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 */

/***************************************************/
diff --git a/common/cmd_nand.c b/common/cmd_nand.c
index 7977060..bb46f34 100644
--- a/common/cmd_nand.c
+++ b/common/cmd_nand.c
@@ -93,7 +93,7 @@ static inline int str2long(char *p, ulong *num)
}

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 defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
@@ -114,10 +114,17 @@ arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, ulong *size)
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];
@@ -257,7 +264,7 @@ int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])

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));
@@ -319,7 +326,7 @@ int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])

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, '.');
@@ -441,7 +448,7 @@ int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
}

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)) {
diff --git a/drivers/mtd/nand/nand_bbt.c b/drivers/mtd/nand/nand_bbt.c
index 19a9bc2..748b266 100644
--- a/drivers/mtd/nand/nand_bbt.c
+++ b/drivers/mtd/nand/nand_bbt.c
@@ -1044,9 +1044,86 @@ int nand_isbad_bbt (struct mtd_info *mtd, loff_t offs, int allowbbt)
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
diff --git a/include/configs/neo1973_gta01.h b/include/configs/neo1973_gta01.h
index 2122fd5..6638d8d 100644
--- a/include/configs/neo1973_gta01.h
+++ b/include/configs/neo1973_gta01.h
@@ -92,6 +92,7 @@
#define CONFIG_CMD_BSP
#define CONFIG_CMD_ELF
#define CONFIG_CMD_MISC
+#define CONFIG_CMD_JFFS2
#define CONFIG_CMD_DIAG
#define CONFIG_CMD_SAVES
#define CONFIG_CMD_NAND
@@ -201,13 +202,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 */
@@ -249,4 +250,9 @@
#define CONFIG_DRIVER_PCF50606 1
#define CONFIG_RTC_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 */
diff --git a/include/configs/qt2410.h b/include/configs/qt2410.h
index bd21ae3..06c4972 100644
--- a/include/configs/qt2410.h
+++ b/include/configs/qt2410.h
@@ -275,5 +275,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 */
diff --git a/include/util.h b/include/util.h
index c1ded14..7f3ac3b 100644
--- a/include/util.h
+++ b/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 */