raid6_recover.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* raid6_recover.c - module to recover from faulty RAID6 arrays. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2006,2007,2008,2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/dl.h>
  20. #include <grub/disk.h>
  21. #include <grub/mm.h>
  22. #include <grub/err.h>
  23. #include <grub/misc.h>
  24. #include <grub/diskfilter.h>
  25. #include <grub/crypto.h>
  26. GRUB_MOD_LICENSE ("GPLv3+");
  27. /* x**y. */
  28. static grub_uint8_t powx[255 * 2];
  29. /* Such an s that x**s = y */
  30. static unsigned powx_inv[256];
  31. static const grub_uint8_t poly = 0x1d;
  32. static void
  33. grub_raid_block_mulx (unsigned mul, char *buf, grub_size_t size)
  34. {
  35. grub_size_t i;
  36. grub_uint8_t *p;
  37. p = (grub_uint8_t *) buf;
  38. for (i = 0; i < size; i++, p++)
  39. if (*p)
  40. *p = powx[mul + powx_inv[*p]];
  41. }
  42. static void
  43. grub_raid6_init_table (void)
  44. {
  45. unsigned i;
  46. grub_uint8_t cur = 1;
  47. for (i = 0; i < 255; i++)
  48. {
  49. powx[i] = cur;
  50. powx[i + 255] = cur;
  51. powx_inv[cur] = i;
  52. if (cur & 0x80)
  53. cur = (cur << 1) ^ poly;
  54. else
  55. cur <<= 1;
  56. }
  57. }
  58. static unsigned
  59. mod_255 (unsigned x)
  60. {
  61. while (x > 0xff)
  62. x = (x >> 8) + (x & 0xff);
  63. if (x == 0xff)
  64. return 0;
  65. return x;
  66. }
  67. static grub_err_t
  68. grub_raid6_recover (struct grub_diskfilter_segment *array, int disknr, int p,
  69. char *buf, grub_disk_addr_t sector, grub_size_t size)
  70. {
  71. int i, q, pos;
  72. int bad1 = -1, bad2 = -1;
  73. char *pbuf = 0, *qbuf = 0;
  74. size <<= GRUB_DISK_SECTOR_BITS;
  75. pbuf = grub_zalloc (size);
  76. if (!pbuf)
  77. goto quit;
  78. qbuf = grub_zalloc (size);
  79. if (!qbuf)
  80. goto quit;
  81. q = p + 1;
  82. if (q == (int) array->node_count)
  83. q = 0;
  84. pos = q + 1;
  85. if (pos == (int) array->node_count)
  86. pos = 0;
  87. for (i = 0; i < (int) array->node_count - 2; i++)
  88. {
  89. int c;
  90. if (array->layout & GRUB_RAID_LAYOUT_MUL_FROM_POS)
  91. c = pos;
  92. else
  93. c = i;
  94. if (pos == disknr)
  95. bad1 = c;
  96. else
  97. {
  98. if (! grub_diskfilter_read_node (&array->nodes[pos], sector,
  99. size >> GRUB_DISK_SECTOR_BITS, buf))
  100. {
  101. grub_crypto_xor (pbuf, pbuf, buf, size);
  102. grub_raid_block_mulx (c, buf, size);
  103. grub_crypto_xor (qbuf, qbuf, buf, size);
  104. }
  105. else
  106. {
  107. /* Too many bad devices */
  108. if (bad2 >= 0)
  109. goto quit;
  110. bad2 = c;
  111. grub_errno = GRUB_ERR_NONE;
  112. }
  113. }
  114. pos++;
  115. if (pos == (int) array->node_count)
  116. pos = 0;
  117. }
  118. /* Invalid disknr or p */
  119. if (bad1 < 0)
  120. goto quit;
  121. if (bad2 < 0)
  122. {
  123. /* One bad device */
  124. if ((! grub_diskfilter_read_node (&array->nodes[p], sector,
  125. size >> GRUB_DISK_SECTOR_BITS, buf)))
  126. {
  127. grub_crypto_xor (buf, buf, pbuf, size);
  128. goto quit;
  129. }
  130. grub_errno = GRUB_ERR_NONE;
  131. if (grub_diskfilter_read_node (&array->nodes[q], sector,
  132. size >> GRUB_DISK_SECTOR_BITS, buf))
  133. goto quit;
  134. grub_crypto_xor (buf, buf, qbuf, size);
  135. grub_raid_block_mulx (255 - bad1, buf,
  136. size);
  137. }
  138. else
  139. {
  140. /* Two bad devices */
  141. unsigned c;
  142. if (grub_diskfilter_read_node (&array->nodes[p], sector,
  143. size >> GRUB_DISK_SECTOR_BITS, buf))
  144. goto quit;
  145. grub_crypto_xor (pbuf, pbuf, buf, size);
  146. if (grub_diskfilter_read_node (&array->nodes[q], sector,
  147. size >> GRUB_DISK_SECTOR_BITS, buf))
  148. goto quit;
  149. grub_crypto_xor (qbuf, qbuf, buf, size);
  150. c = mod_255((255 ^ bad1)
  151. + (255 ^ powx_inv[(powx[bad2 + (bad1 ^ 255)] ^ 1)]));
  152. grub_raid_block_mulx (c, qbuf, size);
  153. c = mod_255((unsigned) bad2 + c);
  154. grub_raid_block_mulx (c, pbuf, size);
  155. grub_crypto_xor (pbuf, pbuf, qbuf, size);
  156. grub_memcpy (buf, pbuf, size);
  157. }
  158. quit:
  159. grub_free (pbuf);
  160. grub_free (qbuf);
  161. return grub_errno;
  162. }
  163. GRUB_MOD_INIT(raid6rec)
  164. {
  165. grub_raid6_init_table ();
  166. grub_raid6_recover_func = grub_raid6_recover;
  167. }
  168. GRUB_MOD_FINI(raid6rec)
  169. {
  170. grub_raid6_recover_func = 0;
  171. }