decode_rs.c 8.0 KB

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