2 * calculate ecc code for nand flash
4 * Copyright (C) 2008 yajin <yajin@vm-kernel.org>
5 * Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 or
10 * (at your option) version 3 of the License.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 #include <sys/types.h>
32 #define DEF_NAND_PAGE_SIZE 2048
33 #define DEF_NAND_OOB_SIZE 64
34 #define DEF_NAND_ECC_OFFSET 0x28
36 static int page_size
= DEF_NAND_PAGE_SIZE
;
37 static int oob_size
= DEF_NAND_OOB_SIZE
;
38 static int ecc_offset
= DEF_NAND_ECC_OFFSET
;
41 * Pre-calculated 256-way 1 byte column parity
43 static const uint8_t nand_ecc_precalc_table
[] = {
44 0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00,
45 0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
46 0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
47 0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
48 0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
49 0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
50 0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
51 0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
52 0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
53 0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
54 0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
55 0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
56 0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
57 0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
58 0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
59 0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00
63 * nand_calculate_ecc - [NAND Interface] Calculate 3-byte ECC for 256-byte block
65 * @ecc_code: buffer for ECC
67 int nand_calculate_ecc(const uint8_t *dat
,
70 uint8_t idx
, reg1
, reg2
, reg3
, tmp1
, tmp2
;
73 /* Initialize variables */
74 reg1
= reg2
= reg3
= 0;
76 /* Build up column parity */
77 for(i
= 0; i
< 256; i
++) {
78 /* Get CP0 - CP5 from table */
79 idx
= nand_ecc_precalc_table
[*dat
++];
82 /* All bit XOR = 1 ? */
85 reg2
^= ~((uint8_t) i
);
89 /* Create non-inverted ECC code from line parity */
90 tmp1
= (reg3
& 0x80) >> 0; /* B7 -> B7 */
91 tmp1
|= (reg2
& 0x80) >> 1; /* B7 -> B6 */
92 tmp1
|= (reg3
& 0x40) >> 1; /* B6 -> B5 */
93 tmp1
|= (reg2
& 0x40) >> 2; /* B6 -> B4 */
94 tmp1
|= (reg3
& 0x20) >> 2; /* B5 -> B3 */
95 tmp1
|= (reg2
& 0x20) >> 3; /* B5 -> B2 */
96 tmp1
|= (reg3
& 0x10) >> 3; /* B4 -> B1 */
97 tmp1
|= (reg2
& 0x10) >> 4; /* B4 -> B0 */
99 tmp2
= (reg3
& 0x08) << 4; /* B3 -> B7 */
100 tmp2
|= (reg2
& 0x08) << 3; /* B3 -> B6 */
101 tmp2
|= (reg3
& 0x04) << 3; /* B2 -> B5 */
102 tmp2
|= (reg2
& 0x04) << 2; /* B2 -> B4 */
103 tmp2
|= (reg3
& 0x02) << 2; /* B1 -> B3 */
104 tmp2
|= (reg2
& 0x02) << 1; /* B1 -> B2 */
105 tmp2
|= (reg3
& 0x01) << 1; /* B0 -> B1 */
106 tmp2
|= (reg2
& 0x01) << 0; /* B7 -> B0 */
108 /* Calculate final ECC code */
109 #ifdef CONFIG_MTD_NAND_ECC_SMC
116 ecc_code
[2] = ((~reg1
) << 2) | 0x03;
122 * usage: bb-nandflash-ecc start_address size
124 void usage(const char *prog
)
126 fprintf(stderr
, "Usage: %s [options] <input> <output>\n"
128 " -p <pagesize> NAND page size (default: %d)\n"
129 " -o <oobsize> NAND OOB size (default: %d)\n"
130 " -e <offset> NAND ECC offset (default: %d)\n"
131 "\n", prog
, DEF_NAND_PAGE_SIZE
, DEF_NAND_OOB_SIZE
,
132 DEF_NAND_ECC_OFFSET
);
136 /*start_address/size does not include oob
138 int main(int argc
, char **argv
)
140 uint8_t *page_data
= NULL
;
142 int infd
= -1, outfd
= -1;
147 while ((ch
= getopt(argc
, argv
, "e:o:p:")) != -1) {
150 page_size
= strtoul(optarg
, NULL
, 0);
153 oob_size
= strtoul(optarg
, NULL
, 0);
156 ecc_offset
= strtoul(optarg
, NULL
, 0);
168 infd
= open(argv
[0], O_RDONLY
, 0);
170 perror("open input file");
174 outfd
= open(argv
[1], O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
176 perror("open output file");
180 page_data
= malloc(page_size
+ oob_size
);
182 while ((bytes
= read(infd
, page_data
, page_size
)) == page_size
) {
185 ecc_data
= page_data
+ page_size
+ ecc_offset
;
186 for (j
= 0; j
< page_size
/ 256; j
++)
188 nand_calculate_ecc(page_data
+ j
* 256, ecc_data
);
191 write(outfd
, page_data
, page_size
+ oob_size
);
This page took 0.05634 seconds and 5 git commands to generate.