f957cca538a9c3c19c4ea5d19b569c6617ed75d0
2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2007 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
15 * This code implements the ECC algorithm used in SmartMedia.
17 * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
18 * The two unused bit are set to 1.
19 * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
20 * blocks are used on a 512-byte NAND page.
24 /* Table generated by gen-ecc.c
25 * Using a table means we do not have to calculate p1..p4 and p1'..p4'
26 * for each byte of data. These are instead provided in a table in bits7..2.
27 * Bit 0 of each entry indicates whether the entry has an odd or even parity, and therefore
28 * this bytes influence on the line parity.
31 const char *yaffs_ecc_c_version
=
35 #include "yaffs_ecc.h"
37 static const unsigned char column_parity_table
[] = {
38 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
39 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
40 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
41 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
42 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
43 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
44 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
45 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
46 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
47 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
48 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
49 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
50 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
51 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
52 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
53 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
54 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
55 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
56 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
57 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
58 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
59 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
60 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
61 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
62 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
63 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
64 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
65 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
66 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
67 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
68 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
69 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
72 /* Count the bits in an unsigned char or a U32 */
74 static int yaffs_CountBits(unsigned char x
)
85 static int yaffs_CountBits32(unsigned x
)
96 /* Calculate the ECC for a 256-byte block of data */
97 void yaffs_ECCCalculate(const unsigned char *data
, unsigned char *ecc
)
101 unsigned char col_parity
= 0;
102 unsigned char line_parity
= 0;
103 unsigned char line_parity_prime
= 0;
107 for (i
= 0; i
< 256; i
++) {
108 b
= column_parity_table
[*data
++];
111 if (b
& 0x01) // odd number of bits in the byte
114 line_parity_prime
^= ~i
;
119 ecc
[2] = (~col_parity
) | 0x03;
122 if (line_parity
& 0x80)
124 if (line_parity_prime
& 0x80)
126 if (line_parity
& 0x40)
128 if (line_parity_prime
& 0x40)
130 if (line_parity
& 0x20)
132 if (line_parity_prime
& 0x20)
134 if (line_parity
& 0x10)
136 if (line_parity_prime
& 0x10)
141 if (line_parity
& 0x08)
143 if (line_parity_prime
& 0x08)
145 if (line_parity
& 0x04)
147 if (line_parity_prime
& 0x04)
149 if (line_parity
& 0x02)
151 if (line_parity_prime
& 0x02)
153 if (line_parity
& 0x01)
155 if (line_parity_prime
& 0x01)
159 #ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
160 // Swap the bytes into the wrong order
168 /* Correct the ECC on a 256 byte block of data */
170 int yaffs_ECCCorrect(unsigned char *data
, unsigned char *read_ecc
,
171 const unsigned char *test_ecc
)
173 unsigned char d0
, d1
, d2
; /* deltas */
175 d0
= read_ecc
[0] ^ test_ecc
[0];
176 d1
= read_ecc
[1] ^ test_ecc
[1];
177 d2
= read_ecc
[2] ^ test_ecc
[2];
179 if ((d0
| d1
| d2
) == 0)
180 return 0; /* no error */
182 if (((d0
^ (d0
>> 1)) & 0x55) == 0x55 &&
183 ((d1
^ (d1
>> 1)) & 0x55) == 0x55 &&
184 ((d2
^ (d2
>> 1)) & 0x54) == 0x54) {
185 /* Single bit (recoverable) error in data */
190 #ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
191 // swap the bytes to correct for the wrong order
225 data
[byte
] ^= (1 << bit
);
227 return 1; /* Corrected the error */
230 if ((yaffs_CountBits(d0
) +
231 yaffs_CountBits(d1
) +
232 yaffs_CountBits(d2
)) == 1) {
233 /* Reccoverable error in ecc */
235 read_ecc
[0] = test_ecc
[0];
236 read_ecc
[1] = test_ecc
[1];
237 read_ecc
[2] = test_ecc
[2];
239 return 1; /* Corrected the error */
242 /* Unrecoverable error */
250 * ECCxxxOther does ECC calcs on arbitrary n bytes of data
252 void yaffs_ECCCalculateOther(const unsigned char *data
, unsigned nBytes
,
253 yaffs_ECCOther
* eccOther
)
257 unsigned char col_parity
= 0;
258 unsigned line_parity
= 0;
259 unsigned line_parity_prime
= 0;
262 for (i
= 0; i
< nBytes
; i
++) {
263 b
= column_parity_table
[*data
++];
267 /* odd number of bits in the byte */
269 line_parity_prime
^= ~i
;
274 eccOther
->colParity
= (col_parity
>> 2) & 0x3f;
275 eccOther
->lineParity
= line_parity
;
276 eccOther
->lineParityPrime
= line_parity_prime
;
279 int yaffs_ECCCorrectOther(unsigned char *data
, unsigned nBytes
,
280 yaffs_ECCOther
* read_ecc
,
281 const yaffs_ECCOther
* test_ecc
)
283 unsigned char cDelta
; /* column parity delta */
284 unsigned lDelta
; /* line parity delta */
285 unsigned lDeltaPrime
; /* line parity delta */
288 cDelta
= read_ecc
->colParity
^ test_ecc
->colParity
;
289 lDelta
= read_ecc
->lineParity
^ test_ecc
->lineParity
;
290 lDeltaPrime
= read_ecc
->lineParityPrime
^ test_ecc
->lineParityPrime
;
292 if ((cDelta
| lDelta
| lDeltaPrime
) == 0)
293 return 0; /* no error */
295 if (lDelta
== ~lDeltaPrime
&&
296 (((cDelta
^ (cDelta
>> 1)) & 0x15) == 0x15))
298 /* Single bit (recoverable) error in data */
312 data
[lDelta
] ^= (1 << bit
);
314 return 1; /* corrected */
317 if ((yaffs_CountBits32(lDelta
) + yaffs_CountBits32(lDeltaPrime
) +
318 yaffs_CountBits(cDelta
)) == 1) {
319 /* Reccoverable error in ecc */
321 *read_ecc
= *test_ecc
;
322 return 1; /* corrected */
325 /* Unrecoverable error */
This page took 0.068597 seconds and 3 git commands to generate.