firmware-utils: add a small utility for generating nand flash images with ecc info
authornbd <nbd@3c298f89-4303-0410-b956-a3cf2f4a3e73>
Sun, 14 Jun 2009 20:42:28 +0000 (20:42 +0000)
committernbd <nbd@3c298f89-4303-0410-b956-a3cf2f4a3e73>
Sun, 14 Jun 2009 20:42:28 +0000 (20:42 +0000)
git-svn-id: svn://svn.openwrt.org/openwrt/trunk@16458 3c298f89-4303-0410-b956-a3cf2f4a3e73

tools/firmware-utils/Makefile
tools/firmware-utils/src/nand_ecc.c [new file with mode: 0644]

index 4e5789b..96b4b59 100644 (file)
@@ -46,6 +46,7 @@ define Host/Compile
        $(call cc,add_header)
        $(call cc,makeamitbin)
        $(call cc,encode_crc)
+       $(call cc,nand_ecc)
        $(call cc2,mkplanexfw sha1)
        $(call cc2,mktplinkfw md5)
 endef
diff --git a/tools/firmware-utils/src/nand_ecc.c b/tools/firmware-utils/src/nand_ecc.c
new file mode 100644 (file)
index 0000000..e270480
--- /dev/null
@@ -0,0 +1,204 @@
+/*\r
+ * calculate ecc code for nand flash\r
+ *\r
+ * Copyright (C) 2008 yajin <yajin@vm-kernel.org>\r
+ * Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>\r
+ *\r
+ * This program is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU General Public License as\r
+ * published by the Free Software Foundation; either version 2 or\r
+ * (at your option) version 3 of the License.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program; if not, write to the Free Software\r
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,\r
+ * MA 02111-1307 USA\r
+ */\r
+\r
+\r
+#include <sys/types.h>\r
+#include <sys/stat.h>\r
+#include <unistd.h>\r
+#include <stdlib.h>\r
+#include <stdint.h>\r
+#include <fcntl.h>\r
+#include <stdio.h>\r
+\r
+#define DEF_NAND_PAGE_SIZE   2048\r
+#define DEF_NAND_OOB_SIZE     64\r
+#define DEF_NAND_ECC_OFFSET   0x28\r
+\r
+static int page_size = DEF_NAND_PAGE_SIZE;\r
+static int oob_size = DEF_NAND_OOB_SIZE;\r
+static int ecc_offset = DEF_NAND_ECC_OFFSET;\r
+\r
+/*\r
+ * Pre-calculated 256-way 1 byte column parity\r
+ */\r
+static const uint8_t nand_ecc_precalc_table[] = {\r
+       0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00,\r
+       0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,\r
+       0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,\r
+       0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,\r
+       0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,\r
+       0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,\r
+       0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,\r
+       0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,\r
+       0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,\r
+       0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,\r
+       0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,\r
+       0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,\r
+       0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,\r
+       0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,\r
+       0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,\r
+       0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00\r
+};\r
+\r
+/**\r
+ * nand_calculate_ecc - [NAND Interface] Calculate 3-byte ECC for 256-byte block\r
+ * @dat:       raw data\r
+ * @ecc_code:  buffer for ECC\r
+ */\r
+int nand_calculate_ecc(const uint8_t *dat,\r
+                      uint8_t *ecc_code)\r
+{\r
+       uint8_t idx, reg1, reg2, reg3, tmp1, tmp2;\r
+       int i;\r
+\r
+       /* Initialize variables */\r
+       reg1 = reg2 = reg3 = 0;\r
+\r
+       /* Build up column parity */\r
+       for(i = 0; i < 256; i++) {\r
+               /* Get CP0 - CP5 from table */\r
+               idx = nand_ecc_precalc_table[*dat++];\r
+               reg1 ^= (idx & 0x3f);\r
+\r
+               /* All bit XOR = 1 ? */\r
+               if (idx & 0x40) {\r
+                       reg3 ^= (uint8_t) i;\r
+                       reg2 ^= ~((uint8_t) i);\r
+               }\r
+       }\r
+\r
+       /* Create non-inverted ECC code from line parity */\r
+       tmp1  = (reg3 & 0x80) >> 0; /* B7 -> B7 */\r
+       tmp1 |= (reg2 & 0x80) >> 1; /* B7 -> B6 */\r
+       tmp1 |= (reg3 & 0x40) >> 1; /* B6 -> B5 */\r
+       tmp1 |= (reg2 & 0x40) >> 2; /* B6 -> B4 */\r
+       tmp1 |= (reg3 & 0x20) >> 2; /* B5 -> B3 */\r
+       tmp1 |= (reg2 & 0x20) >> 3; /* B5 -> B2 */\r
+       tmp1 |= (reg3 & 0x10) >> 3; /* B4 -> B1 */\r
+       tmp1 |= (reg2 & 0x10) >> 4; /* B4 -> B0 */\r
+\r
+       tmp2  = (reg3 & 0x08) << 4; /* B3 -> B7 */\r
+       tmp2 |= (reg2 & 0x08) << 3; /* B3 -> B6 */\r
+       tmp2 |= (reg3 & 0x04) << 3; /* B2 -> B5 */\r
+       tmp2 |= (reg2 & 0x04) << 2; /* B2 -> B4 */\r
+       tmp2 |= (reg3 & 0x02) << 2; /* B1 -> B3 */\r
+       tmp2 |= (reg2 & 0x02) << 1; /* B1 -> B2 */\r
+       tmp2 |= (reg3 & 0x01) << 1; /* B0 -> B1 */\r
+       tmp2 |= (reg2 & 0x01) << 0; /* B7 -> B0 */\r
+\r
+       /* Calculate final ECC code */\r
+#ifdef CONFIG_MTD_NAND_ECC_SMC\r
+       ecc_code[0] = ~tmp2;\r
+       ecc_code[1] = ~tmp1;\r
+#else\r
+       ecc_code[0] = ~tmp1;\r
+       ecc_code[1] = ~tmp2;\r
+#endif\r
+       ecc_code[2] = ((~reg1) << 2) | 0x03;\r
+\r
+       return 0;\r
+}\r
+\r
+/*\r
+ *  usage: bb-nandflash-ecc    start_address  size\r
+ */\r
+void usage(const char *prog)\r
+{\r
+       fprintf(stderr, "Usage: %s [options] <input> <output>\n"\r
+               "Options:\n"\r
+               "    -p <pagesize>      NAND page size (default: %d)\n"\r
+               "    -o <oobsize>       NAND OOB size (default: %d)\n"\r
+               "    -e <offset>        NAND ECC offset (default: %d)\n"\r
+               "\n", prog, DEF_NAND_PAGE_SIZE, DEF_NAND_OOB_SIZE,\r
+               DEF_NAND_ECC_OFFSET);\r
+       exit(1);\r
+}\r
+\r
+/*start_address/size does not include oob\r
+  */\r
+int main(int argc, char **argv)\r
+{\r
+       uint8_t *page_data = NULL;\r
+       uint8_t *ecc_data;\r
+       int infd = -1, outfd = -1;\r
+       int ret = 1;\r
+       ssize_t bytes;\r
+       int ch;\r
+\r
+       while ((ch = getopt(argc, argv, "e:o:p:")) != -1) {\r
+               switch(ch) {\r
+               case 'p':\r
+                       page_size = strtoul(optarg, NULL, 0);\r
+                       break;\r
+               case 'o':\r
+                       oob_size = strtoul(optarg, NULL, 0);\r
+                       break;\r
+               case 'e':\r
+                       ecc_offset = strtoul(optarg, NULL, 0);\r
+                       break;\r
+               default:\r
+                       usage(argv[0]);\r
+               }\r
+       }\r
+       argc -= optind;\r
+       if (argc < 2)\r
+               usage(argv[0]);\r
+\r
+       argv += optind;\r
+\r
+       infd = open(argv[0], O_RDONLY, 0);\r
+       if (infd < 0) {\r
+               perror("open input file");\r
+               goto out;\r
+       }\r
+\r
+       outfd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC, 0644);\r
+       if (outfd < 0) {\r
+               perror("open output file");\r
+               goto out;\r
+       }\r
+\r
+       page_data = malloc(page_size + oob_size);\r
+\r
+       while ((bytes = read(infd, page_data, page_size)) == page_size) {\r
+               int j;\r
+\r
+               ecc_data = page_data + page_size + ecc_offset;\r
+               for (j = 0; j < page_size / 256; j++)\r
+               {\r
+                       nand_calculate_ecc(page_data + j * 256, ecc_data);\r
+                       ecc_data += 3;\r
+               }\r
+               write(outfd, page_data, page_size + oob_size);\r
+       }\r
+\r
+       ret = 0;\r
+out:\r
+       if (infd >= 0)\r
+               close(infd);\r
+       if (outfd >= 0)\r
+               close(outfd);\r
+       if (page_data)\r
+               free(page_data);\r
+       return ret;\r
+}\r
+\r