source: SVN/rincon/u-boot/common/docecc.c @ 55

Last change on this file since 55 was 55, checked in by Tim Harvey, 22 months ago

rincon: added latest u-boot source

restored form server backup

Signed-off-by: Tim Harvey <tharvey@…>

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