reed_solomon.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2010 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifdef TEST
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #define xcalloc calloc
  23. #define xmalloc malloc
  24. #define grub_memset memset
  25. #define grub_memcpy memcpy
  26. #endif
  27. #ifndef STANDALONE
  28. #include <assert.h>
  29. #endif
  30. #ifndef STANDALONE
  31. #ifdef TEST
  32. typedef unsigned int grub_size_t;
  33. typedef unsigned char grub_uint8_t;
  34. #else
  35. #include <grub/types.h>
  36. #include <grub/reed_solomon.h>
  37. #include <grub/util/misc.h>
  38. #include <grub/misc.h>
  39. #endif
  40. #endif
  41. #define SECTOR_SIZE 512
  42. #define MAX_BLOCK_SIZE (200 * SECTOR_SIZE)
  43. #ifdef STANDALONE
  44. #ifdef TEST
  45. typedef unsigned int grub_size_t;
  46. typedef unsigned char grub_uint8_t;
  47. #else
  48. #include <grub/types.h>
  49. #include <grub/misc.h>
  50. #endif
  51. #ifdef __i386__
  52. #define REED_SOLOMON_ATTRIBUTE __attribute__ ((regparm(3)))
  53. #else
  54. #define REED_SOLOMON_ATTRIBUTE
  55. #endif
  56. void
  57. grub_reed_solomon_recover (void *ptr_, grub_size_t s, grub_size_t rs)
  58. REED_SOLOMON_ATTRIBUTE;
  59. #else
  60. #define REED_SOLOMON_ATTRIBUTE
  61. #endif
  62. #define GF_SIZE 8
  63. typedef grub_uint8_t gf_single_t;
  64. #define GF_POLYNOMIAL 0x1d
  65. #define GF_INVERT2 0x8e
  66. #if defined (STANDALONE) && !defined (TEST)
  67. #ifdef __APPLE__
  68. #define ATTRIBUTE_TEXT __attribute__ ((section("_text,_text")))
  69. #else
  70. #define ATTRIBUTE_TEXT __attribute__ ((section(".text")))
  71. #endif
  72. static gf_single_t * const gf_powx ATTRIBUTE_TEXT = (void *) 0x100000;
  73. static gf_single_t * const gf_powx_inv ATTRIBUTE_TEXT = (void *) 0x100200;
  74. static int *const chosenstat ATTRIBUTE_TEXT = (void *) 0x100300;
  75. static gf_single_t *const sigma ATTRIBUTE_TEXT = (void *) 0x100700;
  76. static gf_single_t *const errpot ATTRIBUTE_TEXT = (void *) 0x100800;
  77. static int *const errpos ATTRIBUTE_TEXT = (void *) 0x100900;
  78. static gf_single_t *const sy ATTRIBUTE_TEXT = (void *) 0x100d00;
  79. static gf_single_t *const mstat ATTRIBUTE_TEXT = (void *) 0x100e00;
  80. static gf_single_t *const errvals ATTRIBUTE_TEXT = (void *) 0x100f00;
  81. static gf_single_t *const eqstat ATTRIBUTE_TEXT = (void *) 0x101000;
  82. /* Next available address: (void *) 0x112000. */
  83. #else
  84. static gf_single_t gf_powx[255 * 2];
  85. static gf_single_t gf_powx_inv[256];
  86. static int chosenstat[256];
  87. static gf_single_t sigma[256];
  88. static gf_single_t errpot[256];
  89. static int errpos[256];
  90. static gf_single_t sy[256];
  91. static gf_single_t mstat[256];
  92. static gf_single_t errvals[256];
  93. static gf_single_t eqstat[65536 + 256];
  94. #endif
  95. #if __GNUC__ == 12
  96. #pragma GCC diagnostic push
  97. #pragma GCC diagnostic ignored "-Warray-bounds"
  98. #endif
  99. static gf_single_t
  100. gf_mul (gf_single_t a, gf_single_t b)
  101. {
  102. if (a == 0 || b == 0)
  103. return 0;
  104. return gf_powx[(int) gf_powx_inv[a] + (int) gf_powx_inv[b]];
  105. }
  106. static inline gf_single_t
  107. gf_invert (gf_single_t a)
  108. {
  109. return gf_powx[255 - (int) gf_powx_inv[a]];
  110. }
  111. static void
  112. init_powx (void)
  113. {
  114. int i;
  115. grub_uint8_t cur = 1;
  116. gf_powx_inv[0] = 0;
  117. for (i = 0; i < 255; i++)
  118. {
  119. gf_powx[i] = cur;
  120. gf_powx[i + 255] = cur;
  121. gf_powx_inv[cur] = i;
  122. if (cur & (1ULL << (GF_SIZE - 1)))
  123. cur = (cur << 1) ^ GF_POLYNOMIAL;
  124. else
  125. cur <<= 1;
  126. }
  127. }
  128. static gf_single_t
  129. pol_evaluate (gf_single_t *pol, grub_size_t degree, int log_x)
  130. {
  131. int i;
  132. gf_single_t s = 0;
  133. int log_xn = 0;
  134. for (i = degree; i >= 0; i--)
  135. {
  136. if (pol[i])
  137. s ^= gf_powx[(int) gf_powx_inv[pol[i]] + log_xn];
  138. log_xn += log_x;
  139. if (log_xn >= ((1 << GF_SIZE) - 1))
  140. log_xn -= ((1 << GF_SIZE) - 1);
  141. }
  142. return s;
  143. }
  144. #if !defined (STANDALONE)
  145. static void
  146. rs_encode (gf_single_t *data, grub_size_t s, grub_size_t rs)
  147. {
  148. gf_single_t *rs_polynomial;
  149. int i, j;
  150. gf_single_t *m;
  151. m = xcalloc (s + rs, sizeof (gf_single_t));
  152. grub_memcpy (m, data, s * sizeof (gf_single_t));
  153. rs_polynomial = xcalloc (rs + 1, sizeof (gf_single_t));
  154. rs_polynomial[rs] = 1;
  155. /* Multiply with X - a^r */
  156. for (j = 0; j < rs; j++)
  157. {
  158. for (i = 0; i < rs; i++)
  159. if (rs_polynomial[i])
  160. rs_polynomial[i] = (rs_polynomial[i + 1]
  161. ^ gf_powx[j + (int) gf_powx_inv[rs_polynomial[i]]]);
  162. else
  163. rs_polynomial[i] = rs_polynomial[i + 1];
  164. if (rs_polynomial[rs])
  165. rs_polynomial[rs] = gf_powx[j + (int) gf_powx_inv[rs_polynomial[rs]]];
  166. }
  167. for (j = 0; j < s; j++)
  168. if (m[j])
  169. {
  170. gf_single_t f = m[j];
  171. for (i = 0; i <= rs; i++)
  172. m[i+j] ^= gf_mul (rs_polynomial[i], f);
  173. }
  174. free (rs_polynomial);
  175. grub_memcpy (data + s, m + s, rs * sizeof (gf_single_t));
  176. free (m);
  177. }
  178. #endif
  179. static void
  180. gauss_eliminate (gf_single_t *eq, int n, int m, int *chosen)
  181. {
  182. int i, j;
  183. for (i = 0 ; i < n; i++)
  184. {
  185. int nzidx;
  186. int k;
  187. gf_single_t r;
  188. for (nzidx = 0; nzidx < m && (eq[i * (m + 1) + nzidx] == 0);
  189. nzidx++);
  190. if (nzidx == m)
  191. continue;
  192. chosen[i] = nzidx;
  193. r = gf_invert (eq[i * (m + 1) + nzidx]);
  194. for (j = 0; j < m + 1; j++)
  195. eq[i * (m + 1) + j] = gf_mul (eq[i * (m + 1) + j], r);
  196. for (j = i + 1; j < n; j++)
  197. {
  198. gf_single_t rr = eq[j * (m + 1) + nzidx];
  199. for (k = 0; k < m + 1; k++)
  200. eq[j * (m + 1) + k] ^= gf_mul (eq[i * (m + 1) + k], rr);
  201. }
  202. }
  203. }
  204. static void
  205. gauss_solve (gf_single_t *eq, int n, int m, gf_single_t *sol)
  206. {
  207. int i, j;
  208. for (i = 0; i < n; i++)
  209. chosenstat[i] = -1;
  210. for (i = 0; i < m; i++)
  211. sol[i] = 0;
  212. gauss_eliminate (eq, n, m, chosenstat);
  213. for (i = n - 1; i >= 0; i--)
  214. {
  215. gf_single_t s = 0;
  216. if (chosenstat[i] == -1)
  217. continue;
  218. for (j = 0; j < m; j++)
  219. s ^= gf_mul (eq[i * (m + 1) + j], sol[j]);
  220. s ^= eq[i * (m + 1) + m];
  221. sol[chosenstat[i]] = s;
  222. }
  223. }
  224. static void
  225. rs_recover (gf_single_t *mm, grub_size_t s, grub_size_t rs)
  226. {
  227. grub_size_t rs2 = rs / 2;
  228. int errnum = 0;
  229. int i, j;
  230. for (i = 0; i < (int) rs; i++)
  231. sy[i] = pol_evaluate (mm, s + rs - 1, i);
  232. for (i = 0; i < (int) rs; i++)
  233. if (sy[i] != 0)
  234. break;
  235. /* No error detected. */
  236. if (i == (int) rs)
  237. return;
  238. {
  239. for (i = 0; i < (int) rs2; i++)
  240. for (j = 0; j < (int) rs2 + 1; j++)
  241. eqstat[i * (rs2 + 1) + j] = sy[i+j];
  242. for (i = 0; i < (int) rs2; i++)
  243. sigma[i] = 0;
  244. gauss_solve (eqstat, rs2, rs2, sigma);
  245. }
  246. for (i = 0; i < (int) (rs + s); i++)
  247. if (pol_evaluate (sigma, rs2 - 1, 255 - i) == gf_powx[i])
  248. {
  249. errpot[errnum] = gf_powx[i];
  250. errpos[errnum++] = s + rs - i - 1;
  251. }
  252. {
  253. for (j = 0; j < errnum; j++)
  254. eqstat[j] = 1;
  255. eqstat[errnum] = sy[0];
  256. for (i = 1; i < (int) rs; i++)
  257. {
  258. for (j = 0; j < (int) errnum; j++)
  259. eqstat[(errnum + 1) * i + j] = gf_mul (errpot[j],
  260. eqstat[(errnum + 1) * (i - 1)
  261. + j]);
  262. eqstat[(errnum + 1) * i + errnum] = sy[i];
  263. }
  264. gauss_solve (eqstat, rs, errnum, errvals);
  265. for (i = 0; i < (int) errnum; i++)
  266. mm[errpos[i]] ^= errvals[i];
  267. }
  268. }
  269. static void
  270. decode_block (gf_single_t *ptr, grub_size_t s,
  271. gf_single_t *rptr, grub_size_t rs)
  272. {
  273. int i, j;
  274. for (i = 0; i < SECTOR_SIZE; i++)
  275. {
  276. grub_size_t ds = (s + SECTOR_SIZE - 1 - i) / SECTOR_SIZE;
  277. grub_size_t rr = (rs + SECTOR_SIZE - 1 - i) / SECTOR_SIZE;
  278. /* Nothing to do. */
  279. if (!ds || !rr)
  280. continue;
  281. for (j = 0; j < (int) ds; j++)
  282. mstat[j] = ptr[SECTOR_SIZE * j + i];
  283. for (j = 0; j < (int) rr; j++)
  284. mstat[j + ds] = rptr[SECTOR_SIZE * j + i];
  285. rs_recover (mstat, ds, rr);
  286. for (j = 0; j < (int) ds; j++)
  287. ptr[SECTOR_SIZE * j + i] = mstat[j];
  288. }
  289. }
  290. #if __GNUC__ == 12
  291. #pragma GCC diagnostic pop
  292. #endif
  293. #if !defined (STANDALONE)
  294. static void
  295. encode_block (gf_single_t *ptr, grub_size_t s,
  296. gf_single_t *rptr, grub_size_t rs)
  297. {
  298. int i, j;
  299. for (i = 0; i < SECTOR_SIZE; i++)
  300. {
  301. grub_size_t ds = (s + SECTOR_SIZE - 1 - i) / SECTOR_SIZE;
  302. grub_size_t rr = (rs + SECTOR_SIZE - 1 - i) / SECTOR_SIZE;
  303. gf_single_t *m;
  304. if (!ds || !rr)
  305. continue;
  306. m = xmalloc (ds + rr);
  307. for (j = 0; j < ds; j++)
  308. m[j] = ptr[SECTOR_SIZE * j + i];
  309. rs_encode (m, ds, rr);
  310. for (j = 0; j < rr; j++)
  311. rptr[SECTOR_SIZE * j + i] = m[j + ds];
  312. free (m);
  313. }
  314. }
  315. #endif
  316. #if !defined (STANDALONE)
  317. void
  318. grub_reed_solomon_add_redundancy (void *buffer, grub_size_t data_size,
  319. grub_size_t redundancy)
  320. {
  321. grub_size_t s = data_size;
  322. grub_size_t rs = redundancy;
  323. gf_single_t *ptr = buffer;
  324. gf_single_t *rptr = ptr + s;
  325. void *tmp;
  326. tmp = xmalloc (data_size);
  327. grub_memcpy (tmp, buffer, data_size);
  328. /* Nothing to do. */
  329. if (!rs)
  330. goto exit;
  331. init_powx ();
  332. while (s > 0)
  333. {
  334. grub_size_t tt;
  335. grub_size_t cs, crs;
  336. cs = s;
  337. crs = rs;
  338. tt = cs + crs;
  339. if (tt > MAX_BLOCK_SIZE)
  340. {
  341. cs = ((cs * (MAX_BLOCK_SIZE / 512)) / tt) * 512;
  342. crs = ((crs * (MAX_BLOCK_SIZE / 512)) / tt) * 512;
  343. }
  344. encode_block (ptr, cs, rptr, crs);
  345. ptr += cs;
  346. rptr += crs;
  347. s -= cs;
  348. rs -= crs;
  349. }
  350. #ifndef TEST
  351. assert (grub_memcmp (tmp, buffer, data_size) == 0);
  352. #endif
  353. exit:
  354. free (tmp);
  355. }
  356. #endif
  357. void REED_SOLOMON_ATTRIBUTE
  358. grub_reed_solomon_recover (void *ptr_, grub_size_t s, grub_size_t rs)
  359. {
  360. gf_single_t *ptr = ptr_;
  361. gf_single_t *rptr = ptr + s;
  362. grub_uint8_t *cptr;
  363. /* Nothing to do. */
  364. if (!rs)
  365. return;
  366. for (cptr = rptr + rs - 1; cptr >= rptr; cptr--)
  367. if (*cptr)
  368. break;
  369. if (rptr + rs - 1 - cptr > (grub_ssize_t) rs / 2)
  370. return;
  371. init_powx ();
  372. while (s > 0)
  373. {
  374. grub_size_t tt;
  375. grub_size_t cs, crs;
  376. cs = s;
  377. crs = rs;
  378. tt = cs + crs;
  379. if (tt > MAX_BLOCK_SIZE)
  380. {
  381. cs = ((cs * (MAX_BLOCK_SIZE / 512)) / tt) * 512;
  382. crs = ((crs * (MAX_BLOCK_SIZE / 512)) / tt) * 512;
  383. }
  384. decode_block (ptr, cs, rptr, crs);
  385. ptr += cs;
  386. rptr += crs;
  387. s -= cs;
  388. rs -= crs;
  389. }
  390. }
  391. #ifdef TEST
  392. int
  393. main (int argc, char **argv)
  394. {
  395. FILE *in, *out;
  396. grub_size_t s, rs;
  397. char *buf;
  398. grub_memset (gf_powx, 0xee, sizeof (gf_powx));
  399. grub_memset (gf_powx_inv, 0xdd, sizeof (gf_powx_inv));
  400. #ifndef STANDALONE
  401. init_powx ();
  402. #endif
  403. #ifndef STANDALONE
  404. in = grub_util_fopen ("tst.bin", "rb");
  405. if (!in)
  406. return 1;
  407. fseek (in, 0, SEEK_END);
  408. s = ftell (in);
  409. fseek (in, 0, SEEK_SET);
  410. rs = 0x7007;
  411. buf = xmalloc (s + rs + SECTOR_SIZE);
  412. fread (buf, 1, s, in);
  413. fclose (in);
  414. grub_reed_solomon_add_redundancy (buf, s, rs);
  415. out = grub_util_fopen ("tst_rs.bin", "wb");
  416. fwrite (buf, 1, s + rs, out);
  417. fclose (out);
  418. #else
  419. out = grub_util_fopen ("tst_rs.bin", "rb");
  420. fseek (out, 0, SEEK_END);
  421. s = ftell (out);
  422. fseek (out, 0, SEEK_SET);
  423. rs = 0x7007;
  424. s -= rs;
  425. buf = xmalloc (s + rs + SECTOR_SIZE);
  426. fread (buf, 1, s + rs, out);
  427. fclose (out);
  428. #endif
  429. #if 1
  430. grub_memset (buf + 512 * 15, 0, 512);
  431. #endif
  432. out = grub_util_fopen ("tst_dam.bin", "wb");
  433. fwrite (buf, 1, s + rs, out);
  434. fclose (out);
  435. grub_reed_solomon_recover (buf, s, rs);
  436. out = grub_util_fopen ("tst_rec.bin", "wb");
  437. fwrite (buf, 1, s, out);
  438. fclose (out);
  439. return 0;
  440. }
  441. #endif