docecc.c 15 KB

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