2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2018 Aleph One Ltd.
6 * Created by Charles Manning <charles@aleph1.co.uk>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 * This code implements the ECC algorithm used in SmartMedia.
16 * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
17 * The two unused bit are set to 1.
18 * The ECC can correct single bit errors in a 256-byte page of data. Thus, two
19 * such ECC blocks are used on a 512-byte NAND page.
25 #include "yaffs_ecc.h"
27 /* Table generated by gen-ecc.c
28 * Using a table means we do not have to calculate p1..p4 and p1'..p4'
29 * for each byte of data. These are instead provided in a table in bits7..2.
30 * Bit 0 of each entry indicates whether the entry has an odd or even parity,
31 * and therefore this bytes influence on the line parity.
34 static const unsigned char column_parity_table[] = {
35 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
36 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
37 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
38 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
39 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
40 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
41 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
42 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
43 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
44 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
45 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
46 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
47 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
48 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
49 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
50 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
51 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
52 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
53 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
54 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
55 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
56 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
57 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
58 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
59 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
60 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
61 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
62 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
63 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
64 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
65 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
66 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
70 /* Calculate the ECC for a 256-byte block of data */
71 void yaffs_ecc_calc(const unsigned char *data, unsigned char *ecc)
74 unsigned char col_parity = 0;
75 unsigned char line_parity = 0;
76 unsigned char line_parity_prime = 0;
80 for (i = 0; i < 256; i++) {
81 b = column_parity_table[*data++];
84 if (b & 0x01) { /* odd number of bits in the byte */
86 line_parity_prime ^= ~i;
90 ecc[2] = (~col_parity) | 0x03;
93 if (line_parity & 0x80)
95 if (line_parity_prime & 0x80)
97 if (line_parity & 0x40)
99 if (line_parity_prime & 0x40)
101 if (line_parity & 0x20)
103 if (line_parity_prime & 0x20)
105 if (line_parity & 0x10)
107 if (line_parity_prime & 0x10)
112 if (line_parity & 0x08)
114 if (line_parity_prime & 0x08)
116 if (line_parity & 0x04)
118 if (line_parity_prime & 0x04)
120 if (line_parity & 0x02)
122 if (line_parity_prime & 0x02)
124 if (line_parity & 0x01)
126 if (line_parity_prime & 0x01)
132 /* Correct the ECC on a 256 byte block of data */
134 int yaffs_ecc_correct(unsigned char *data, unsigned char *read_ecc,
135 const unsigned char *test_ecc)
137 unsigned char d0, d1, d2; /* deltas */
139 d0 = read_ecc[0] ^ test_ecc[0];
140 d1 = read_ecc[1] ^ test_ecc[1];
141 d2 = read_ecc[2] ^ test_ecc[2];
143 if ((d0 | d1 | d2) == 0)
144 return 0; /* no error */
146 if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 &&
147 ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 &&
148 ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) {
149 /* Single bit (recoverable) error in data */
180 data[byte] ^= (1 << bit);
182 return 1; /* Corrected the error */
185 if ((hweight8(d0) + hweight8(d1) + hweight8(d2)) == 1) {
186 /* Reccoverable error in ecc */
188 read_ecc[0] = test_ecc[0];
189 read_ecc[1] = test_ecc[1];
190 read_ecc[2] = test_ecc[2];
192 return 1; /* Corrected the error */
195 /* Unrecoverable error */
202 * ECCxxxOther does ECC calcs on arbitrary n bytes of data
204 void yaffs_ecc_calc_other(const unsigned char *data, unsigned n_bytes,
205 struct yaffs_ecc_other *ecc_other)
208 unsigned char col_parity = 0;
209 unsigned line_parity = 0;
210 unsigned line_parity_prime = 0;
213 for (i = 0; i < n_bytes; i++) {
214 b = column_parity_table[*data++];
218 /* odd number of bits in the byte */
220 line_parity_prime ^= ~i;
225 ecc_other->col_parity = (col_parity >> 2) & 0x3f;
226 ecc_other->line_parity = line_parity;
227 ecc_other->line_parity_prime = line_parity_prime;
230 int yaffs_ecc_correct_other(unsigned char *data, unsigned n_bytes,
231 struct yaffs_ecc_other *read_ecc,
232 const struct yaffs_ecc_other *test_ecc)
234 unsigned char delta_col; /* column parity delta */
235 unsigned delta_line; /* line parity delta */
236 unsigned delta_line_prime; /* line parity delta */
239 delta_col = read_ecc->col_parity ^ test_ecc->col_parity;
240 delta_line = read_ecc->line_parity ^ test_ecc->line_parity;
242 read_ecc->line_parity_prime ^ test_ecc->line_parity_prime;
244 if ((delta_col | delta_line | delta_line_prime) == 0)
245 return 0; /* no error */
247 if (delta_line == ~delta_line_prime &&
248 (((delta_col ^ (delta_col >> 1)) & 0x15) == 0x15)) {
249 /* Single bit (recoverable) error in data */
253 if (delta_col & 0x20)
255 if (delta_col & 0x08)
257 if (delta_col & 0x02)
260 if (delta_line >= n_bytes)
263 data[delta_line] ^= (1 << bit);
265 return 1; /* corrected */
268 if ((hweight32(delta_line) +
269 hweight32(delta_line_prime) +
270 hweight8(delta_col)) == 1) {
271 /* Reccoverable error in ecc */
273 *read_ecc = *test_ecc;
274 return 1; /* corrected */
277 /* Unrecoverable error */