decode_rs.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. uint16_t msk = (uint16_t) rs->nn;
  26. /*
  27. * The decoder buffers are in the rs control struct. They are
  28. * arrays sized [nroots + 1]
  29. */
  30. uint16_t *lambda = rsc->buffers + RS_DECODE_LAMBDA * (nroots + 1);
  31. uint16_t *syn = rsc->buffers + RS_DECODE_SYN * (nroots + 1);
  32. uint16_t *b = rsc->buffers + RS_DECODE_B * (nroots + 1);
  33. uint16_t *t = rsc->buffers + RS_DECODE_T * (nroots + 1);
  34. uint16_t *omega = rsc->buffers + RS_DECODE_OMEGA * (nroots + 1);
  35. uint16_t *root = rsc->buffers + RS_DECODE_ROOT * (nroots + 1);
  36. uint16_t *reg = rsc->buffers + RS_DECODE_REG * (nroots + 1);
  37. uint16_t *loc = rsc->buffers + RS_DECODE_LOC * (nroots + 1);
  38. /* Check length parameter for validity */
  39. pad = nn - nroots - len;
  40. BUG_ON(pad < 0 || pad >= nn);
  41. /* Does the caller provide the syndrome ? */
  42. if (s != NULL) {
  43. for (i = 0; i < nroots; i++) {
  44. /* The syndrome is in index form,
  45. * so nn represents zero
  46. */
  47. if (s[i] != nn)
  48. goto decode;
  49. }
  50. /* syndrome is zero, no errors to correct */
  51. return 0;
  52. }
  53. /* form the syndromes; i.e., evaluate data(x) at roots of
  54. * g(x) */
  55. for (i = 0; i < nroots; i++)
  56. syn[i] = (((uint16_t) data[0]) ^ invmsk) & msk;
  57. for (j = 1; j < len; j++) {
  58. for (i = 0; i < nroots; i++) {
  59. if (syn[i] == 0) {
  60. syn[i] = (((uint16_t) data[j]) ^
  61. invmsk) & msk;
  62. } else {
  63. syn[i] = ((((uint16_t) data[j]) ^
  64. invmsk) & msk) ^
  65. alpha_to[rs_modnn(rs, index_of[syn[i]] +
  66. (fcr + i) * prim)];
  67. }
  68. }
  69. }
  70. for (j = 0; j < nroots; j++) {
  71. for (i = 0; i < nroots; i++) {
  72. if (syn[i] == 0) {
  73. syn[i] = ((uint16_t) par[j]) & msk;
  74. } else {
  75. syn[i] = (((uint16_t) par[j]) & msk) ^
  76. alpha_to[rs_modnn(rs, index_of[syn[i]] +
  77. (fcr+i)*prim)];
  78. }
  79. }
  80. }
  81. s = syn;
  82. /* Convert syndromes to index form, checking for nonzero condition */
  83. syn_error = 0;
  84. for (i = 0; i < nroots; i++) {
  85. syn_error |= s[i];
  86. s[i] = index_of[s[i]];
  87. }
  88. if (!syn_error) {
  89. /* if syndrome is zero, data[] is a codeword and there are no
  90. * errors to correct. So return data[] unmodified
  91. */
  92. count = 0;
  93. goto finish;
  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. /* Find roots of error+erasure locator polynomial by Chien search */
  175. memcpy(&reg[1], &lambda[1], nroots * sizeof(reg[0]));
  176. count = 0; /* Number of roots of lambda(x) */
  177. for (i = 1, k = iprim - 1; i <= nn; i++, k = rs_modnn(rs, k + iprim)) {
  178. q = 1; /* lambda[0] is always 0 */
  179. for (j = deg_lambda; j > 0; j--) {
  180. if (reg[j] != nn) {
  181. reg[j] = rs_modnn(rs, reg[j] + j);
  182. q ^= alpha_to[reg[j]];
  183. }
  184. }
  185. if (q != 0)
  186. continue; /* Not a root */
  187. /* store root (index-form) and error location number */
  188. root[count] = i;
  189. loc[count] = k;
  190. /* If we've already found max possible roots,
  191. * abort the search to save time
  192. */
  193. if (++count == deg_lambda)
  194. break;
  195. }
  196. if (deg_lambda != count) {
  197. /*
  198. * deg(lambda) unequal to number of roots => uncorrectable
  199. * error detected
  200. */
  201. count = -EBADMSG;
  202. goto finish;
  203. }
  204. /*
  205. * Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo
  206. * x**nroots). in index form. Also find deg(omega).
  207. */
  208. deg_omega = deg_lambda - 1;
  209. for (i = 0; i <= deg_omega; i++) {
  210. tmp = 0;
  211. for (j = i; j >= 0; j--) {
  212. if ((s[i - j] != nn) && (lambda[j] != nn))
  213. tmp ^=
  214. alpha_to[rs_modnn(rs, s[i - j] + lambda[j])];
  215. }
  216. omega[i] = index_of[tmp];
  217. }
  218. /*
  219. * Compute error values in poly-form. num1 = omega(inv(X(l))), num2 =
  220. * inv(X(l))**(fcr-1) and den = lambda_pr(inv(X(l))) all in poly-form
  221. */
  222. for (j = count - 1; j >= 0; j--) {
  223. num1 = 0;
  224. for (i = deg_omega; i >= 0; i--) {
  225. if (omega[i] != nn)
  226. num1 ^= alpha_to[rs_modnn(rs, omega[i] +
  227. i * root[j])];
  228. }
  229. num2 = alpha_to[rs_modnn(rs, root[j] * (fcr - 1) + nn)];
  230. den = 0;
  231. /* lambda[i+1] for i even is the formal derivative
  232. * lambda_pr of lambda[i] */
  233. for (i = min(deg_lambda, nroots - 1) & ~1; i >= 0; i -= 2) {
  234. if (lambda[i + 1] != nn) {
  235. den ^= alpha_to[rs_modnn(rs, lambda[i + 1] +
  236. i * root[j])];
  237. }
  238. }
  239. /* Apply error to data */
  240. if (num1 != 0 && loc[j] >= pad) {
  241. uint16_t cor = alpha_to[rs_modnn(rs,index_of[num1] +
  242. index_of[num2] +
  243. nn - index_of[den])];
  244. /* Store the error correction pattern, if a
  245. * correction buffer is available */
  246. if (corr) {
  247. corr[j] = cor;
  248. } else {
  249. /* If a data buffer is given and the
  250. * error is inside the message,
  251. * correct it */
  252. if (data && (loc[j] < (nn - nroots)))
  253. data[loc[j] - pad] ^= cor;
  254. }
  255. }
  256. }
  257. finish:
  258. if (eras_pos != NULL) {
  259. for (i = 0; i < count; i++)
  260. eras_pos[i] = loc[i] - pad;
  261. }
  262. return count;
  263. }