raid6_recover.c 4.6 KB
Newer Older
1 2 3
/* raid6_recover.c - module to recover from faulty RAID6 arrays.  */
/*
 *  GRUB  --  GRand Unified Bootloader
4
 *  Copyright (C) 2006,2007,2008,2009  Free Software Foundation, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <grub/dl.h>
#include <grub/disk.h>
#include <grub/mm.h>
#include <grub/err.h>
#include <grub/misc.h>
25
#include <grub/diskfilter.h>
26
#include <grub/crypto.h>
27

28 29
GRUB_MOD_LICENSE ("GPLv3+");

30 31 32
/* x**y.  */
static grub_uint8_t powx[255 * 2];
/* Such an s that x**s = y */
33
static unsigned powx_inv[256];
34
static const grub_uint8_t poly = 0x1d;
35 36

static void
37
grub_raid_block_mulx (unsigned mul, char *buf, grub_size_t size)
38
{
39
  grub_size_t i;
40 41 42 43
  grub_uint8_t *p;

  p = (grub_uint8_t *) buf;
  for (i = 0; i < size; i++, p++)
44 45
    if (*p)
      *p = powx[mul + powx_inv[*p]];
46 47 48 49 50
}

static void
grub_raid6_init_table (void)
{
51
  unsigned i;
52

53 54 55 56 57 58 59 60 61 62 63
  grub_uint8_t cur = 1;
  for (i = 0; i < 255; i++)
    {
      powx[i] = cur;
      powx[i + 255] = cur;
      powx_inv[cur] = i;
      if (cur & 0x80)
	cur = (cur << 1) ^ poly;
      else
	cur <<= 1;
    }
64 65
}

66 67 68 69 70 71 72 73 74 75
static unsigned
mod_255 (unsigned x)
{
  while (x > 0xff)
    x = (x >> 8) + (x & 0xff);
  if (x == 0xff)
    return 0;
  return x;
}

76
static grub_err_t
77
grub_raid6_recover (struct grub_diskfilter_segment *array, int disknr, int p,
78
                    char *buf, grub_disk_addr_t sector, grub_size_t size)
79 80
{
  int i, q, pos;
81
  int bad1 = -1, bad2 = -1;
82 83 84
  char *pbuf = 0, *qbuf = 0;

  size <<= GRUB_DISK_SECTOR_BITS;
85
  pbuf = grub_zalloc (size);
86 87 88
  if (!pbuf)
    goto quit;

89
  qbuf = grub_zalloc (size);
90 91 92 93
  if (!qbuf)
    goto quit;

  q = p + 1;
94
  if (q == (int) array->node_count)
95 96 97
    q = 0;

  pos = q + 1;
98
  if (pos == (int) array->node_count)
99 100
    pos = 0;

101
  for (i = 0; i < (int) array->node_count - 2; i++)
102
    {
103 104 105 106 107
      int c;
      if (array->layout & GRUB_RAID_LAYOUT_MUL_FROM_POS)
	c = pos;
      else
	c = i;
108
      if (pos == disknr)
109
        bad1 = c;
110 111
      else
        {
112 113
          if (! grub_diskfilter_read_node (&array->nodes[pos], sector,
					   size >> GRUB_DISK_SECTOR_BITS, buf))
114
            {
115
              grub_crypto_xor (pbuf, pbuf, buf, size);
116
              grub_raid_block_mulx (c, buf, size);
117
              grub_crypto_xor (qbuf, qbuf, buf, size);
118 119 120
            }
          else
            {
121 122
              /* Too many bad devices */
              if (bad2 >= 0)
123 124
                goto quit;

125
              bad2 = c;
126 127 128 129 130
              grub_errno = GRUB_ERR_NONE;
            }
        }

      pos++;
131
      if (pos == (int) array->node_count)
132 133 134
        pos = 0;
    }

135 136 137 138 139
  /* Invalid disknr or p */
  if (bad1 < 0)
    goto quit;

  if (bad2 < 0)
140
    {
141
      /* One bad device */
142 143
      if ((! grub_diskfilter_read_node (&array->nodes[p], sector,
					size >> GRUB_DISK_SECTOR_BITS, buf)))
144
        {
145
          grub_crypto_xor (buf, buf, pbuf, size);
146 147 148 149
          goto quit;
        }

      grub_errno = GRUB_ERR_NONE;
150 151
      if (grub_diskfilter_read_node (&array->nodes[q], sector,
				     size >> GRUB_DISK_SECTOR_BITS, buf))
152 153
        goto quit;

154
      grub_crypto_xor (buf, buf, qbuf, size);
155
      grub_raid_block_mulx (255 - bad1, buf,
156 157 158 159
                           size);
    }
  else
    {
160
      /* Two bad devices */
161
      unsigned c;
162

163 164
      if (grub_diskfilter_read_node (&array->nodes[p], sector,
				     size >> GRUB_DISK_SECTOR_BITS, buf))
165 166
        goto quit;

167
      grub_crypto_xor (pbuf, pbuf, buf, size);
168

169 170
      if (grub_diskfilter_read_node (&array->nodes[q], sector,
				     size >> GRUB_DISK_SECTOR_BITS, buf))
171 172
        goto quit;

173
      grub_crypto_xor (qbuf, qbuf, buf, size);
174

175 176
      c = mod_255((255 ^ bad1)
		  + (255 ^ powx_inv[(powx[bad2 + (bad1 ^ 255)] ^ 1)]));
177
      grub_raid_block_mulx (c, qbuf, size);
178

179
      c = mod_255((unsigned) bad2 + c);
180
      grub_raid_block_mulx (c, pbuf, size);
181

182
      grub_crypto_xor (pbuf, pbuf, qbuf, size);
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
      grub_memcpy (buf, pbuf, size);
    }

quit:
  grub_free (pbuf);
  grub_free (qbuf);

  return grub_errno;
}

GRUB_MOD_INIT(raid6rec)
{
  grub_raid6_init_table ();
  grub_raid6_recover_func = grub_raid6_recover;
}

GRUB_MOD_FINI(raid6rec)
{
  grub_raid6_recover_func = 0;
}