yaffs_ecc.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  3. *
  4. * Copyright (C) 2002-2007 Aleph One Ltd.
  5. * for Toby Churchill Ltd and Brightstar Engineering
  6. *
  7. * Created by Charles Manning <charles@aleph1.co.uk>
  8. *
  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.
  12. */
  13. /*
  14. * This code implements the ECC algorithm used in SmartMedia.
  15. *
  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 such ECC
  19. * blocks are used on a 512-byte NAND page.
  20. *
  21. */
  22. /* Table generated by gen-ecc.c
  23. * Using a table means we do not have to calculate p1..p4 and p1'..p4'
  24. * for each byte of data. These are instead provided in a table in bits7..2.
  25. * Bit 0 of each entry indicates whether the entry has an odd or even parity, and therefore
  26. * this bytes influence on the line parity.
  27. */
  28. const char *yaffs_ecc_c_version =
  29. "$Id: yaffs_ecc.c,v 1.1 2007/08/13 07:18:33 jsgood Exp $";
  30. #include "yportenv.h"
  31. #include "yaffs_ecc.h"
  32. static const unsigned char column_parity_table[] = {
  33. 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
  34. 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
  35. 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
  36. 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
  37. 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
  38. 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
  39. 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
  40. 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
  41. 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
  42. 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
  43. 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
  44. 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
  45. 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
  46. 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
  47. 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
  48. 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
  49. 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
  50. 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
  51. 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
  52. 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
  53. 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
  54. 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
  55. 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
  56. 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
  57. 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
  58. 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
  59. 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
  60. 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
  61. 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
  62. 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
  63. 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
  64. 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
  65. };
  66. /* Count the bits in an unsigned char or a U32 */
  67. static int yaffs_CountBits(unsigned char x)
  68. {
  69. int r = 0;
  70. while (x) {
  71. if (x & 1)
  72. r++;
  73. x >>= 1;
  74. }
  75. return r;
  76. }
  77. static int yaffs_CountBits32(unsigned x)
  78. {
  79. int r = 0;
  80. while (x) {
  81. if (x & 1)
  82. r++;
  83. x >>= 1;
  84. }
  85. return r;
  86. }
  87. /* Calculate the ECC for a 256-byte block of data */
  88. void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc)
  89. {
  90. unsigned int i;
  91. unsigned char col_parity = 0;
  92. unsigned char line_parity = 0;
  93. unsigned char line_parity_prime = 0;
  94. unsigned char t;
  95. unsigned char b;
  96. for (i = 0; i < 256; i++) {
  97. b = column_parity_table[*data++];
  98. col_parity ^= b;
  99. if (b & 0x01) // odd number of bits in the byte
  100. {
  101. line_parity ^= i;
  102. line_parity_prime ^= ~i;
  103. }
  104. }
  105. ecc[2] = (~col_parity) | 0x03;
  106. t = 0;
  107. if (line_parity & 0x80)
  108. t |= 0x80;
  109. if (line_parity_prime & 0x80)
  110. t |= 0x40;
  111. if (line_parity & 0x40)
  112. t |= 0x20;
  113. if (line_parity_prime & 0x40)
  114. t |= 0x10;
  115. if (line_parity & 0x20)
  116. t |= 0x08;
  117. if (line_parity_prime & 0x20)
  118. t |= 0x04;
  119. if (line_parity & 0x10)
  120. t |= 0x02;
  121. if (line_parity_prime & 0x10)
  122. t |= 0x01;
  123. ecc[1] = ~t;
  124. t = 0;
  125. if (line_parity & 0x08)
  126. t |= 0x80;
  127. if (line_parity_prime & 0x08)
  128. t |= 0x40;
  129. if (line_parity & 0x04)
  130. t |= 0x20;
  131. if (line_parity_prime & 0x04)
  132. t |= 0x10;
  133. if (line_parity & 0x02)
  134. t |= 0x08;
  135. if (line_parity_prime & 0x02)
  136. t |= 0x04;
  137. if (line_parity & 0x01)
  138. t |= 0x02;
  139. if (line_parity_prime & 0x01)
  140. t |= 0x01;
  141. ecc[0] = ~t;
  142. #ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
  143. // Swap the bytes into the wrong order
  144. t = ecc[0];
  145. ecc[0] = ecc[1];
  146. ecc[1] = t;
  147. #endif
  148. }
  149. /* Correct the ECC on a 256 byte block of data */
  150. int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc,
  151. const unsigned char *test_ecc)
  152. {
  153. unsigned char d0, d1, d2; /* deltas */
  154. d0 = read_ecc[0] ^ test_ecc[0];
  155. d1 = read_ecc[1] ^ test_ecc[1];
  156. d2 = read_ecc[2] ^ test_ecc[2];
  157. if ((d0 | d1 | d2) == 0)
  158. return 0; /* no error */
  159. if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 &&
  160. ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 &&
  161. ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) {
  162. /* Single bit (recoverable) error in data */
  163. unsigned byte;
  164. unsigned bit;
  165. #ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
  166. // swap the bytes to correct for the wrong order
  167. unsigned char t;
  168. t = d0;
  169. d0 = d1;
  170. d1 = t;
  171. #endif
  172. bit = byte = 0;
  173. if (d1 & 0x80)
  174. byte |= 0x80;
  175. if (d1 & 0x20)
  176. byte |= 0x40;
  177. if (d1 & 0x08)
  178. byte |= 0x20;
  179. if (d1 & 0x02)
  180. byte |= 0x10;
  181. if (d0 & 0x80)
  182. byte |= 0x08;
  183. if (d0 & 0x20)
  184. byte |= 0x04;
  185. if (d0 & 0x08)
  186. byte |= 0x02;
  187. if (d0 & 0x02)
  188. byte |= 0x01;
  189. if (d2 & 0x80)
  190. bit |= 0x04;
  191. if (d2 & 0x20)
  192. bit |= 0x02;
  193. if (d2 & 0x08)
  194. bit |= 0x01;
  195. data[byte] ^= (1 << bit);
  196. return 1; /* Corrected the error */
  197. }
  198. if ((yaffs_CountBits(d0) +
  199. yaffs_CountBits(d1) +
  200. yaffs_CountBits(d2)) == 1) {
  201. /* Reccoverable error in ecc */
  202. read_ecc[0] = test_ecc[0];
  203. read_ecc[1] = test_ecc[1];
  204. read_ecc[2] = test_ecc[2];
  205. return 1; /* Corrected the error */
  206. }
  207. /* Unrecoverable error */
  208. return -1;
  209. }
  210. /*
  211. * ECCxxxOther does ECC calcs on arbitrary n bytes of data
  212. */
  213. void yaffs_ECCCalculateOther(const unsigned char *data, unsigned nBytes,
  214. yaffs_ECCOther * eccOther)
  215. {
  216. unsigned int i;
  217. unsigned char col_parity = 0;
  218. unsigned line_parity = 0;
  219. unsigned line_parity_prime = 0;
  220. unsigned char b;
  221. for (i = 0; i < nBytes; i++) {
  222. b = column_parity_table[*data++];
  223. col_parity ^= b;
  224. if (b & 0x01) {
  225. /* odd number of bits in the byte */
  226. line_parity ^= i;
  227. line_parity_prime ^= ~i;
  228. }
  229. }
  230. eccOther->colParity = (col_parity >> 2) & 0x3f;
  231. eccOther->lineParity = line_parity;
  232. eccOther->lineParityPrime = line_parity_prime;
  233. }
  234. int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes,
  235. yaffs_ECCOther * read_ecc,
  236. const yaffs_ECCOther * test_ecc)
  237. {
  238. unsigned char cDelta; /* column parity delta */
  239. unsigned lDelta; /* line parity delta */
  240. unsigned lDeltaPrime; /* line parity delta */
  241. unsigned bit;
  242. cDelta = read_ecc->colParity ^ test_ecc->colParity;
  243. lDelta = read_ecc->lineParity ^ test_ecc->lineParity;
  244. lDeltaPrime = read_ecc->lineParityPrime ^ test_ecc->lineParityPrime;
  245. if ((cDelta | lDelta | lDeltaPrime) == 0)
  246. return 0; /* no error */
  247. if (lDelta == ~lDeltaPrime &&
  248. (((cDelta ^ (cDelta >> 1)) & 0x15) == 0x15))
  249. {
  250. /* Single bit (recoverable) error in data */
  251. bit = 0;
  252. if (cDelta & 0x20)
  253. bit |= 0x04;
  254. if (cDelta & 0x08)
  255. bit |= 0x02;
  256. if (cDelta & 0x02)
  257. bit |= 0x01;
  258. if(lDelta >= nBytes)
  259. return -1;
  260. data[lDelta] ^= (1 << bit);
  261. return 1; /* corrected */
  262. }
  263. if ((yaffs_CountBits32(lDelta) + yaffs_CountBits32(lDeltaPrime) +
  264. yaffs_CountBits(cDelta)) == 1) {
  265. /* Reccoverable error in ecc */
  266. *read_ecc = *test_ecc;
  267. return 1; /* corrected */
  268. }
  269. /* Unrecoverable error */
  270. return -1;
  271. }