1 /* 2 * Generic Reed Solomon encoder / decoder library 3 * 4 * Copyright 2002, Phil Karn, KA9Q 5 * May be used under the terms of the GNU General Public License (GPL) 6 * 7 * Adaption to the kernel by Thomas Gleixner (tglx@linutronix.de) 8 * 9 * Generic data width independent code which is included by the wrappers. 10 */ 11 { 12 int deg_lambda, el, deg_omega; 13 int i, j, r, k, pad; 14 int nn = rs->nn; 15 int nroots = rs->nroots; 16 int fcr = rs->fcr; 17 int prim = rs->prim; 18 int iprim = rs->iprim; 19 uint16_t *alpha_to = rs->alpha_to; 20 uint16_t *index_of = rs->index_of; 21 uint16_t u, q, tmp, num1, num2, den, discr_r, syn_error; 22 /* Err+Eras Locator poly and syndrome poly The maximum value 23 * of nroots is 8. So the necessary stack size will be about 24 * 220 bytes max. 25 */ 26 uint16_t lambda[nroots + 1], syn[nroots]; 27 uint16_t b[nroots + 1], t[nroots + 1], omega[nroots + 1]; 28 uint16_t root[nroots], reg[nroots + 1], loc[nroots]; 29 int count = 0; 30 uint16_t msk = (uint16_t) rs->nn; 31 32 /* Check length parameter for validity */ 33 pad = nn - nroots - len; 34 BUG_ON(pad < 0 || pad >= nn); 35 36 /* Does the caller provide the syndrome ? */ 37 if (s != NULL) 38 goto decode; 39 40 /* form the syndromes; i.e., evaluate data(x) at roots of 41 * g(x) */ 42 for (i = 0; i < nroots; i++) 43 syn[i] = (((uint16_t) data[0]) ^ invmsk) & msk; 44 45 for (j = 1; j < len; j++) { 46 for (i = 0; i < nroots; i++) { 47 if (syn[i] == 0) { 48 syn[i] = (((uint16_t) data[j]) ^ 49 invmsk) & msk; 50 } else { 51 syn[i] = ((((uint16_t) data[j]) ^ 52 invmsk) & msk) ^ 53 alpha_to[rs_modnn(rs, index_of[syn[i]] + 54 (fcr + i) * prim)]; 55 } 56 } 57 } 58 59 for (j = 0; j < nroots; j++) { 60 for (i = 0; i < nroots; i++) { 61 if (syn[i] == 0) { 62 syn[i] = ((uint16_t) par[j]) & msk; 63 } else { 64 syn[i] = (((uint16_t) par[j]) & msk) ^ 65 alpha_to[rs_modnn(rs, index_of[syn[i]] + 66 (fcr+i)*prim)]; 67 } 68 } 69 } 70 s = syn; 71 72 /* Convert syndromes to index form, checking for nonzero condition */ 73 syn_error = 0; 74 for (i = 0; i < nroots; i++) { 75 syn_error |= s[i]; 76 s[i] = index_of[s[i]]; 77 } 78 79 if (!syn_error) { 80 /* if syndrome is zero, data[] is a codeword and there are no 81 * errors to correct. So return data[] unmodified 82 */ 83 count = 0; 84 goto finish; 85 } 86 87 decode: 88 memset(&lambda[1], 0, nroots * sizeof(lambda[0])); 89 lambda[0] = 1; 90 91 if (no_eras > 0) { 92 /* Init lambda to be the erasure locator polynomial */ 93 lambda[1] = alpha_to[rs_modnn(rs, 94 prim * (nn - 1 - eras_pos[0]))]; 95 for (i = 1; i < no_eras; i++) { 96 u = rs_modnn(rs, prim * (nn - 1 - eras_pos[i])); 97 for (j = i + 1; j > 0; j--) { 98 tmp = index_of[lambda[j - 1]]; 99 if (tmp != nn) { 100 lambda[j] ^= 101 alpha_to[rs_modnn(rs, u + tmp)]; 102 } 103 } 104 } 105 } 106 107 for (i = 0; i < nroots + 1; i++) 108 b[i] = index_of[lambda[i]]; 109 110 /* 111 * Begin Berlekamp-Massey algorithm to determine error+erasure 112 * locator polynomial 113 */ 114 r = no_eras; 115 el = no_eras; 116 while (++r <= nroots) { /* r is the step number */ 117 /* Compute discrepancy at the r-th step in poly-form */ 118 discr_r = 0; 119 for (i = 0; i < r; i++) { 120 if ((lambda[i] != 0) && (s[r - i - 1] != nn)) { 121 discr_r ^= 122 alpha_to[rs_modnn(rs, 123 index_of[lambda[i]] + 124 s[r - i - 1])]; 125 } 126 } 127 discr_r = index_of[discr_r]; /* Index form */ 128 if (discr_r == nn) { 129 /* 2 lines below: B(x) <-- x*B(x) */ 130 memmove (&b[1], b, nroots * sizeof (b[0])); 131 b[0] = nn; 132 } else { 133 /* 7 lines below: T(x) <-- lambda(x)-discr_r*x*b(x) */ 134 t[0] = lambda[0]; 135 for (i = 0; i < nroots; i++) { 136 if (b[i] != nn) { 137 t[i + 1] = lambda[i + 1] ^ 138 alpha_to[rs_modnn(rs, discr_r + 139 b[i])]; 140 } else 141 t[i + 1] = lambda[i + 1]; 142 } 143 if (2 * el <= r + no_eras - 1) { 144 el = r + no_eras - el; 145 /* 146 * 2 lines below: B(x) <-- inv(discr_r) * 147 * lambda(x) 148 */ 149 for (i = 0; i <= nroots; i++) { 150 b[i] = (lambda[i] == 0) ? nn : 151 rs_modnn(rs, index_of[lambda[i]] 152 - discr_r + nn); 153 } 154 } else { 155 /* 2 lines below: B(x) <-- x*B(x) */ 156 memmove(&b[1], b, nroots * sizeof(b[0])); 157 b[0] = nn; 158 } 159 memcpy(lambda, t, (nroots + 1) * sizeof(t[0])); 160 } 161 } 162 163 /* Convert lambda to index form and compute deg(lambda(x)) */ 164 deg_lambda = 0; 165 for (i = 0; i < nroots + 1; i++) { 166 lambda[i] = index_of[lambda[i]]; 167 if (lambda[i] != nn) 168 deg_lambda = i; 169 } 170 /* Find roots of error+erasure locator polynomial by Chien search */ 171 memcpy(®[1], &lambda[1], nroots * sizeof(reg[0])); 172 count = 0; /* Number of roots of lambda(x) */ 173 for (i = 1, k = iprim - 1; i <= nn; i++, k = rs_modnn(rs, k + iprim)) { 174 q = 1; /* lambda[0] is always 0 */ 175 for (j = deg_lambda; j > 0; j--) { 176 if (reg[j] != nn) { 177 reg[j] = rs_modnn(rs, reg[j] + j); 178 q ^= alpha_to[reg[j]]; 179 } 180 } 181 if (q != 0) 182 continue; /* Not a root */ 183 /* store root (index-form) and error location number */ 184 root[count] = i; 185 loc[count] = k; 186 /* If we've already found max possible roots, 187 * abort the search to save time 188 */ 189 if (++count == deg_lambda) 190 break; 191 } 192 if (deg_lambda != count) { 193 /* 194 * deg(lambda) unequal to number of roots => uncorrectable 195 * error detected 196 */ 197 count = -EBADMSG; 198 goto finish; 199 } 200 /* 201 * Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo 202 * x**nroots). in index form. Also find deg(omega). 203 */ 204 deg_omega = deg_lambda - 1; 205 for (i = 0; i <= deg_omega; i++) { 206 tmp = 0; 207 for (j = i; j >= 0; j--) { 208 if ((s[i - j] != nn) && (lambda[j] != nn)) 209 tmp ^= 210 alpha_to[rs_modnn(rs, s[i - j] + lambda[j])]; 211 } 212 omega[i] = index_of[tmp]; 213 } 214 215 /* 216 * Compute error values in poly-form. num1 = omega(inv(X(l))), num2 = 217 * inv(X(l))**(fcr-1) and den = lambda_pr(inv(X(l))) all in poly-form 218 */ 219 for (j = count - 1; j >= 0; j--) { 220 num1 = 0; 221 for (i = deg_omega; i >= 0; i--) { 222 if (omega[i] != nn) 223 num1 ^= alpha_to[rs_modnn(rs, omega[i] + 224 i * root[j])]; 225 } 226 num2 = alpha_to[rs_modnn(rs, root[j] * (fcr - 1) + nn)]; 227 den = 0; 228 229 /* lambda[i+1] for i even is the formal derivative 230 * lambda_pr of lambda[i] */ 231 for (i = min(deg_lambda, nroots - 1) & ~1; i >= 0; i -= 2) { 232 if (lambda[i + 1] != nn) { 233 den ^= alpha_to[rs_modnn(rs, lambda[i + 1] + 234 i * root[j])]; 235 } 236 } 237 /* Apply error to data */ 238 if (num1 != 0 && loc[j] >= pad) { 239 uint16_t cor = alpha_to[rs_modnn(rs,index_of[num1] + 240 index_of[num2] + 241 nn - index_of[den])]; 242 /* Store the error correction pattern, if a 243 * correction buffer is available */ 244 if (corr) { 245 corr[j] = cor; 246 } else { 247 /* If a data buffer is given and the 248 * error is inside the message, 249 * correct it */ 250 if (data && (loc[j] < (nn - nroots))) 251 data[loc[j] - pad] ^= cor; 252 } 253 } 254 } 255 256 finish: 257 if (eras_pos != NULL) { 258 for (i = 0; i < count; i++) 259 eras_pos[i] = loc[i] - pad; 260 } 261 return count; 262 263 } 264