docecc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. * ECC algorithm for M-systems disk on chip. We use the excellent Reed
  3. * Solmon code of Phil Karn (karn@ka9q.ampr.org) available under the
  4. * GNU GPL License. The rest is simply to convert the disk on chip
  5. * syndrom into a standard syndom.
  6. *
  7. * Author: Fabrice Bellard (fabrice.bellard@netgem.com)
  8. * Copyright (C) 2000 Netgem S.A.
  9. *
  10. * $Id: docecc.c,v 1.4 2001/10/02 15:05:13 dwmw2 Exp $
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. */
  26. #include <config.h>
  27. #include <common.h>
  28. #include <malloc.h>
  29. #include <linux/mtd/doc2000.h>
  30. #undef ECC_DEBUG
  31. #undef PSYCHO_DEBUG
  32. #if (CONFIG_COMMANDS & CFG_CMD_DOC)
  33. /* need to undef it (from asm/termbits.h) */
  34. #undef B0
  35. #define MM 10 /* Symbol size in bits */
  36. #define KK (1023-4) /* Number of data symbols per block */
  37. #define B0 510 /* First root of generator polynomial, alpha form */
  38. #define PRIM 1 /* power of alpha used to generate roots of generator poly */
  39. #define NN ((1 << MM) - 1)
  40. typedef unsigned short dtype;
  41. /* 1+x^3+x^10 */
  42. static const int Pp[MM+1] = { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1 };
  43. /* This defines the type used to store an element of the Galois Field
  44. * used by the code. Make sure this is something larger than a char if
  45. * if anything larger than GF(256) is used.
  46. *
  47. * Note: unsigned char will work up to GF(256) but int seems to run
  48. * faster on the Pentium.
  49. */
  50. typedef int gf;
  51. /* No legal value in index form represents zero, so
  52. * we need a special value for this purpose
  53. */
  54. #define A0 (NN)
  55. /* Compute x % NN, where NN is 2**MM - 1,
  56. * without a slow divide
  57. */
  58. static inline gf
  59. modnn(int x)
  60. {
  61. while (x >= NN) {
  62. x -= NN;
  63. x = (x >> MM) + (x & NN);
  64. }
  65. return x;
  66. }
  67. #define CLEAR(a,n) {\
  68. int ci;\
  69. for(ci=(n)-1;ci >=0;ci--)\
  70. (a)[ci] = 0;\
  71. }
  72. #define COPY(a,b,n) {\
  73. int ci;\
  74. for(ci=(n)-1;ci >=0;ci--)\
  75. (a)[ci] = (b)[ci];\
  76. }
  77. #define COPYDOWN(a,b,n) {\
  78. int ci;\
  79. for(ci=(n)-1;ci >=0;ci--)\
  80. (a)[ci] = (b)[ci];\
  81. }
  82. #define Ldec 1
  83. /* generate GF(2**m) from the irreducible polynomial p(X) in Pp[0]..Pp[m]
  84. lookup tables: index->polynomial form alpha_to[] contains j=alpha**i;
  85. polynomial form -> index form index_of[j=alpha**i] = i
  86. alpha=2 is the primitive element of GF(2**m)
  87. HARI's COMMENT: (4/13/94) alpha_to[] can be used as follows:
  88. Let @ represent the primitive element commonly called "alpha" that
  89. is the root of the primitive polynomial p(x). Then in GF(2^m), for any
  90. 0 <= i <= 2^m-2,
  91. @^i = a(0) + a(1) @ + a(2) @^2 + ... + a(m-1) @^(m-1)
  92. where the binary vector (a(0),a(1),a(2),...,a(m-1)) is the representation
  93. of the integer "alpha_to[i]" with a(0) being the LSB and a(m-1) the MSB. Thus for
  94. example the polynomial representation of @^5 would be given by the binary
  95. representation of the integer "alpha_to[5]".
  96. Similarily, index_of[] can be used as follows:
  97. As above, let @ represent the primitive element of GF(2^m) that is
  98. the root of the primitive polynomial p(x). In order to find the power
  99. of @ (alpha) that has the polynomial representation
  100. a(0) + a(1) @ + a(2) @^2 + ... + a(m-1) @^(m-1)
  101. we consider the integer "i" whose binary representation with a(0) being LSB
  102. and a(m-1) MSB is (a(0),a(1),...,a(m-1)) and locate the entry
  103. "index_of[i]". Now, @^index_of[i] is that element whose polynomial
  104. representation is (a(0),a(1),a(2),...,a(m-1)).
  105. NOTE:
  106. The element alpha_to[2^m-1] = 0 always signifying that the
  107. representation of "@^infinity" = 0 is (0,0,0,...,0).
  108. Similarily, the element index_of[0] = A0 always signifying
  109. that the power of alpha which has the polynomial representation
  110. (0,0,...,0) is "infinity".
  111. */
  112. static void
  113. generate_gf(dtype Alpha_to[NN + 1], dtype Index_of[NN + 1])
  114. {
  115. register int i, mask;
  116. mask = 1;
  117. Alpha_to[MM] = 0;
  118. for (i = 0; i < MM; i++) {
  119. Alpha_to[i] = mask;
  120. Index_of[Alpha_to[i]] = i;
  121. /* If Pp[i] == 1 then, term @^i occurs in poly-repr of @^MM */
  122. if (Pp[i] != 0)
  123. Alpha_to[MM] ^= mask; /* Bit-wise EXOR operation */
  124. mask <<= 1; /* single left-shift */
  125. }
  126. Index_of[Alpha_to[MM]] = MM;
  127. /*
  128. * Have obtained poly-repr of @^MM. Poly-repr of @^(i+1) is given by
  129. * poly-repr of @^i shifted left one-bit and accounting for any @^MM
  130. * term that may occur when poly-repr of @^i is shifted.
  131. */
  132. mask >>= 1;
  133. for (i = MM + 1; i < NN; i++) {
  134. if (Alpha_to[i - 1] >= mask)
  135. Alpha_to[i] = Alpha_to[MM] ^ ((Alpha_to[i - 1] ^ mask) << 1);
  136. else
  137. Alpha_to[i] = Alpha_to[i - 1] << 1;
  138. Index_of[Alpha_to[i]] = i;
  139. }
  140. Index_of[0] = A0;
  141. Alpha_to[NN] = 0;
  142. }
  143. /*
  144. * Performs ERRORS+ERASURES decoding of RS codes. bb[] is the content
  145. * of the feedback shift register after having processed the data and
  146. * the ECC.
  147. *
  148. * Return number of symbols corrected, or -1 if codeword is illegal
  149. * or uncorrectable. If eras_pos is non-null, the detected error locations
  150. * are written back. NOTE! This array must be at least NN-KK elements long.
  151. * The corrected data are written in eras_val[]. They must be xor with the data
  152. * to retrieve the correct data : data[erase_pos[i]] ^= erase_val[i] .
  153. *
  154. * First "no_eras" erasures are declared by the calling program. Then, the
  155. * maximum # of errors correctable is t_after_eras = floor((NN-KK-no_eras)/2).
  156. * If the number of channel errors is not greater than "t_after_eras" the
  157. * transmitted codeword will be recovered. Details of algorithm can be found
  158. * in R. Blahut's "Theory ... of Error-Correcting Codes".
  159. * Warning: the eras_pos[] array must not contain duplicate entries; decoder failure
  160. * will result. The decoder *could* check for this condition, but it would involve
  161. * extra time on every decoding operation.
  162. * */
  163. static int
  164. eras_dec_rs(dtype Alpha_to[NN + 1], dtype Index_of[NN + 1],
  165. gf bb[NN - KK + 1], gf eras_val[NN-KK], int eras_pos[NN-KK],
  166. int no_eras)
  167. {
  168. int deg_lambda, el, deg_omega;
  169. int i, j, r,k;
  170. gf u,q,tmp,num1,num2,den,discr_r;
  171. gf lambda[NN-KK + 1], s[NN-KK + 1]; /* Err+Eras Locator poly
  172. * and syndrome poly */
  173. gf b[NN-KK + 1], t[NN-KK + 1], omega[NN-KK + 1];
  174. gf root[NN-KK], reg[NN-KK + 1], loc[NN-KK];
  175. int syn_error, count;
  176. syn_error = 0;
  177. for(i=0;i<NN-KK;i++)
  178. syn_error |= bb[i];
  179. if (!syn_error) {
  180. /* if remainder is zero, data[] is a codeword and there are no
  181. * errors to correct. So return data[] unmodified
  182. */
  183. count = 0;
  184. goto finish;
  185. }
  186. for(i=1;i<=NN-KK;i++){
  187. s[i] = bb[0];
  188. }
  189. for(j=1;j<NN-KK;j++){
  190. if(bb[j] == 0)
  191. continue;
  192. tmp = Index_of[bb[j]];
  193. for(i=1;i<=NN-KK;i++)
  194. s[i] ^= Alpha_to[modnn(tmp + (B0+i-1)*PRIM*j)];
  195. }
  196. /* undo the feedback register implicit multiplication and convert
  197. syndromes to index form */
  198. for(i=1;i<=NN-KK;i++) {
  199. tmp = Index_of[s[i]];
  200. if (tmp != A0)
  201. tmp = modnn(tmp + 2 * KK * (B0+i-1)*PRIM);
  202. s[i] = tmp;
  203. }
  204. CLEAR(&lambda[1],NN-KK);
  205. lambda[0] = 1;
  206. if (no_eras > 0) {
  207. /* Init lambda to be the erasure locator polynomial */
  208. lambda[1] = Alpha_to[modnn(PRIM * eras_pos[0])];
  209. for (i = 1; i < no_eras; i++) {
  210. u = modnn(PRIM*eras_pos[i]);
  211. for (j = i+1; j > 0; j--) {
  212. tmp = Index_of[lambda[j - 1]];
  213. if(tmp != A0)
  214. lambda[j] ^= Alpha_to[modnn(u + tmp)];
  215. }
  216. }
  217. #ifdef ECC_DEBUG
  218. /* Test code that verifies the erasure locator polynomial just constructed
  219. Needed only for decoder debugging. */
  220. /* find roots of the erasure location polynomial */
  221. for(i=1;i<=no_eras;i++)
  222. reg[i] = Index_of[lambda[i]];
  223. count = 0;
  224. for (i = 1,k=NN-Ldec; i <= NN; i++,k = modnn(NN+k-Ldec)) {
  225. q = 1;
  226. for (j = 1; j <= no_eras; j++)
  227. if (reg[j] != A0) {
  228. reg[j] = modnn(reg[j] + j);
  229. q ^= Alpha_to[reg[j]];
  230. }
  231. if (q != 0)
  232. continue;
  233. /* store root and error location number indices */
  234. root[count] = i;
  235. loc[count] = k;
  236. count++;
  237. }
  238. if (count != no_eras) {
  239. printf("\n lambda(x) is WRONG\n");
  240. count = -1;
  241. goto finish;
  242. }
  243. #ifdef PSYCHO_DEBUG
  244. printf("\n Erasure positions as determined by roots of Eras Loc Poly:\n");
  245. for (i = 0; i < count; i++)
  246. printf("%d ", loc[i]);
  247. printf("\n");
  248. #endif
  249. #endif
  250. }
  251. for(i=0;i<NN-KK+1;i++)
  252. b[i] = Index_of[lambda[i]];
  253. /*
  254. * Begin Berlekamp-Massey algorithm to determine error+erasure
  255. * locator polynomial
  256. */
  257. r = no_eras;
  258. el = no_eras;
  259. while (++r <= NN-KK) { /* r is the step number */
  260. /* Compute discrepancy at the r-th step in poly-form */
  261. discr_r = 0;
  262. for (i = 0; i < r; i++){
  263. if ((lambda[i] != 0) && (s[r - i] != A0)) {
  264. discr_r ^= Alpha_to[modnn(Index_of[lambda[i]] + s[r - i])];
  265. }
  266. }
  267. discr_r = Index_of[discr_r]; /* Index form */
  268. if (discr_r == A0) {
  269. /* 2 lines below: B(x) <-- x*B(x) */
  270. COPYDOWN(&b[1],b,NN-KK);
  271. b[0] = A0;
  272. } else {
  273. /* 7 lines below: T(x) <-- lambda(x) - discr_r*x*b(x) */
  274. t[0] = lambda[0];
  275. for (i = 0 ; i < NN-KK; i++) {
  276. if(b[i] != A0)
  277. t[i+1] = lambda[i+1] ^ Alpha_to[modnn(discr_r + b[i])];
  278. else
  279. t[i+1] = lambda[i+1];
  280. }
  281. if (2 * el <= r + no_eras - 1) {
  282. el = r + no_eras - el;
  283. /*
  284. * 2 lines below: B(x) <-- inv(discr_r) *
  285. * lambda(x)
  286. */
  287. for (i = 0; i <= NN-KK; i++)
  288. b[i] = (lambda[i] == 0) ? A0 : modnn(Index_of[lambda[i]] - discr_r + NN);
  289. } else {
  290. /* 2 lines below: B(x) <-- x*B(x) */
  291. COPYDOWN(&b[1],b,NN-KK);
  292. b[0] = A0;
  293. }
  294. COPY(lambda,t,NN-KK+1);
  295. }
  296. }
  297. /* Convert lambda to index form and compute deg(lambda(x)) */
  298. deg_lambda = 0;
  299. for(i=0;i<NN-KK+1;i++){
  300. lambda[i] = Index_of[lambda[i]];
  301. if(lambda[i] != A0)
  302. deg_lambda = i;
  303. }
  304. /*
  305. * Find roots of the error+erasure locator polynomial by Chien
  306. * Search
  307. */
  308. COPY(&reg[1],&lambda[1],NN-KK);
  309. count = 0; /* Number of roots of lambda(x) */
  310. for (i = 1,k=NN-Ldec; i <= NN; i++,k = modnn(NN+k-Ldec)) {
  311. q = 1;
  312. for (j = deg_lambda; j > 0; j--){
  313. if (reg[j] != A0) {
  314. reg[j] = modnn(reg[j] + j);
  315. q ^= Alpha_to[reg[j]];
  316. }
  317. }
  318. if (q != 0)
  319. continue;
  320. /* store root (index-form) and error location number */
  321. root[count] = i;
  322. loc[count] = k;
  323. /* If we've already found max possible roots,
  324. * abort the search to save time
  325. */
  326. if(++count == deg_lambda)
  327. break;
  328. }
  329. if (deg_lambda != count) {
  330. /*
  331. * deg(lambda) unequal to number of roots => uncorrectable
  332. * error detected
  333. */
  334. count = -1;
  335. goto finish;
  336. }
  337. /*
  338. * Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo
  339. * x**(NN-KK)). in index form. Also find deg(omega).
  340. */
  341. deg_omega = 0;
  342. for (i = 0; i < NN-KK;i++){
  343. tmp = 0;
  344. j = (deg_lambda < i) ? deg_lambda : i;
  345. for(;j >= 0; j--){
  346. if ((s[i + 1 - j] != A0) && (lambda[j] != A0))
  347. tmp ^= Alpha_to[modnn(s[i + 1 - j] + lambda[j])];
  348. }
  349. if(tmp != 0)
  350. deg_omega = i;
  351. omega[i] = Index_of[tmp];
  352. }
  353. omega[NN-KK] = A0;
  354. /*
  355. * Compute error values in poly-form. num1 = omega(inv(X(l))), num2 =
  356. * inv(X(l))**(B0-1) and den = lambda_pr(inv(X(l))) all in poly-form
  357. */
  358. for (j = count-1; j >=0; j--) {
  359. num1 = 0;
  360. for (i = deg_omega; i >= 0; i--) {
  361. if (omega[i] != A0)
  362. num1 ^= Alpha_to[modnn(omega[i] + i * root[j])];
  363. }
  364. num2 = Alpha_to[modnn(root[j] * (B0 - 1) + NN)];
  365. den = 0;
  366. /* lambda[i+1] for i even is the formal derivative lambda_pr of lambda[i] */
  367. for (i = min(deg_lambda,NN-KK-1) & ~1; i >= 0; i -=2) {
  368. if(lambda[i+1] != A0)
  369. den ^= Alpha_to[modnn(lambda[i+1] + i * root[j])];
  370. }
  371. if (den == 0) {
  372. #ifdef ECC_DEBUG
  373. printf("\n ERROR: denominator = 0\n");
  374. #endif
  375. /* Convert to dual- basis */
  376. count = -1;
  377. goto finish;
  378. }
  379. /* Apply error to data */
  380. if (num1 != 0) {
  381. eras_val[j] = Alpha_to[modnn(Index_of[num1] + Index_of[num2] + NN - Index_of[den])];
  382. } else {
  383. eras_val[j] = 0;
  384. }
  385. }
  386. finish:
  387. for(i=0;i<count;i++)
  388. eras_pos[i] = loc[i];
  389. return count;
  390. }
  391. /***************************************************************************/
  392. /* The DOC specific code begins here */
  393. #define SECTOR_SIZE 512
  394. /* The sector bytes are packed into NB_DATA MM bits words */
  395. #define NB_DATA (((SECTOR_SIZE + 1) * 8 + 6) / MM)
  396. /*
  397. * Correct the errors in 'sector[]' by using 'ecc1[]' which is the
  398. * content of the feedback shift register applyied to the sector and
  399. * the ECC. Return the number of errors corrected (and correct them in
  400. * sector), or -1 if error
  401. */
  402. int doc_decode_ecc(unsigned char sector[SECTOR_SIZE], unsigned char ecc1[6])
  403. {
  404. int parity, i, nb_errors;
  405. gf bb[NN - KK + 1];
  406. gf error_val[NN-KK];
  407. int error_pos[NN-KK], pos, bitpos, index, val;
  408. dtype *Alpha_to, *Index_of;
  409. /* init log and exp tables here to save memory. However, it is slower */
  410. Alpha_to = malloc((NN + 1) * sizeof(dtype));
  411. if (!Alpha_to)
  412. return -1;
  413. Index_of = malloc((NN + 1) * sizeof(dtype));
  414. if (!Index_of) {
  415. free(Alpha_to);
  416. return -1;
  417. }
  418. generate_gf(Alpha_to, Index_of);
  419. parity = ecc1[1];
  420. bb[0] = (ecc1[4] & 0xff) | ((ecc1[5] & 0x03) << 8);
  421. bb[1] = ((ecc1[5] & 0xfc) >> 2) | ((ecc1[2] & 0x0f) << 6);
  422. bb[2] = ((ecc1[2] & 0xf0) >> 4) | ((ecc1[3] & 0x3f) << 4);
  423. bb[3] = ((ecc1[3] & 0xc0) >> 6) | ((ecc1[0] & 0xff) << 2);
  424. nb_errors = eras_dec_rs(Alpha_to, Index_of, bb,
  425. error_val, error_pos, 0);
  426. if (nb_errors <= 0)
  427. goto the_end;
  428. /* correct the errors */
  429. for(i=0;i<nb_errors;i++) {
  430. pos = error_pos[i];
  431. if (pos >= NB_DATA && pos < KK) {
  432. nb_errors = -1;
  433. goto the_end;
  434. }
  435. if (pos < NB_DATA) {
  436. /* extract bit position (MSB first) */
  437. pos = 10 * (NB_DATA - 1 - pos) - 6;
  438. /* now correct the following 10 bits. At most two bytes
  439. can be modified since pos is even */
  440. index = (pos >> 3) ^ 1;
  441. bitpos = pos & 7;
  442. if ((index >= 0 && index < SECTOR_SIZE) ||
  443. index == (SECTOR_SIZE + 1)) {
  444. val = error_val[i] >> (2 + bitpos);
  445. parity ^= val;
  446. if (index < SECTOR_SIZE)
  447. sector[index] ^= val;
  448. }
  449. index = ((pos >> 3) + 1) ^ 1;
  450. bitpos = (bitpos + 10) & 7;
  451. if (bitpos == 0)
  452. bitpos = 8;
  453. if ((index >= 0 && index < SECTOR_SIZE) ||
  454. index == (SECTOR_SIZE + 1)) {
  455. val = error_val[i] << (8 - bitpos);
  456. parity ^= val;
  457. if (index < SECTOR_SIZE)
  458. sector[index] ^= val;
  459. }
  460. }
  461. }
  462. /* use parity to test extra errors */
  463. if ((parity & 0xff) != 0)
  464. nb_errors = -1;
  465. the_end:
  466. free(Alpha_to);
  467. free(Index_of);
  468. return nb_errors;
  469. }
  470. #endif /* (CONFIG_COMMANDS & CFG_CMD_DOC) */