2N/A/* raid6_recover.c - module to recover from faulty RAID6 arrays. */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2006,2007,2008,2009 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/dl.h>
2N/A#include <grub/disk.h>
2N/A#include <grub/mm.h>
2N/A#include <grub/err.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/raid.h>
2N/A#include <grub/crypto.h>
2N/A
2N/AGRUB_MOD_LICENSE ("GPLv3+");
2N/A
2N/A/* x**y. */
2N/Astatic grub_uint8_t powx[255 * 2];
2N/A/* Such an s that x**s = y */
2N/Astatic int powx_inv[256];
2N/Astatic const grub_uint8_t poly = 0x1d;
2N/A
2N/Astatic void
2N/Agrub_raid_block_mulx (int mul, char *buf, int size)
2N/A{
2N/A int i;
2N/A grub_uint8_t *p;
2N/A
2N/A p = (grub_uint8_t *) buf;
2N/A for (i = 0; i < size; i++, p++)
2N/A if (*p)
2N/A *p = powx[mul + powx_inv[*p]];
2N/A}
2N/A
2N/Astatic void
2N/Agrub_raid6_init_table (void)
2N/A{
2N/A int i;
2N/A
2N/A grub_uint8_t cur = 1;
2N/A for (i = 0; i < 255; i++)
2N/A {
2N/A powx[i] = cur;
2N/A powx[i + 255] = cur;
2N/A powx_inv[cur] = i;
2N/A if (cur & 0x80)
2N/A cur = (cur << 1) ^ poly;
2N/A else
2N/A cur <<= 1;
2N/A }
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_raid6_recover (struct grub_raid_array *array, int disknr, int p,
2N/A char *buf, grub_disk_addr_t sector, int size)
2N/A{
2N/A int i, q, pos;
2N/A int bad1 = -1, bad2 = -1;
2N/A char *pbuf = 0, *qbuf = 0;
2N/A
2N/A size <<= GRUB_DISK_SECTOR_BITS;
2N/A pbuf = grub_zalloc (size);
2N/A if (!pbuf)
2N/A goto quit;
2N/A
2N/A qbuf = grub_zalloc (size);
2N/A if (!qbuf)
2N/A goto quit;
2N/A
2N/A q = p + 1;
2N/A if (q == (int) array->total_devs)
2N/A q = 0;
2N/A
2N/A pos = q + 1;
2N/A if (pos == (int) array->total_devs)
2N/A pos = 0;
2N/A
2N/A for (i = 0; i < (int) array->total_devs - 2; i++)
2N/A {
2N/A if (pos == disknr)
2N/A bad1 = i;
2N/A else
2N/A {
2N/A if ((array->members[pos].device) &&
2N/A (! grub_disk_read (array->members[pos].device,
2N/A array->members[pos].start_sector + sector,
2N/A 0, size, buf)))
2N/A {
2N/A grub_crypto_xor (pbuf, pbuf, buf, size);
2N/A grub_raid_block_mulx (i, buf, size);
2N/A grub_crypto_xor (qbuf, qbuf, buf, size);
2N/A }
2N/A else
2N/A {
2N/A /* Too many bad devices */
2N/A if (bad2 >= 0)
2N/A goto quit;
2N/A
2N/A bad2 = i;
2N/A grub_errno = GRUB_ERR_NONE;
2N/A }
2N/A }
2N/A
2N/A pos++;
2N/A if (pos == (int) array->total_devs)
2N/A pos = 0;
2N/A }
2N/A
2N/A /* Invalid disknr or p */
2N/A if (bad1 < 0)
2N/A goto quit;
2N/A
2N/A if (bad2 < 0)
2N/A {
2N/A /* One bad device */
2N/A if ((array->members[p].device) &&
2N/A (! grub_disk_read (array->members[p].device,
2N/A array->members[p].start_sector + sector,
2N/A 0, size, buf)))
2N/A {
2N/A grub_crypto_xor (buf, buf, pbuf, size);
2N/A goto quit;
2N/A }
2N/A
2N/A if (! array->members[q].device)
2N/A {
2N/A grub_error (GRUB_ERR_READ_ERROR, "not enough disk to restore");
2N/A goto quit;
2N/A }
2N/A
2N/A grub_errno = GRUB_ERR_NONE;
2N/A if (grub_disk_read (array->members[q].device,
2N/A array->members[q].start_sector + sector, 0, size, buf))
2N/A goto quit;
2N/A
2N/A grub_crypto_xor (buf, buf, qbuf, size);
2N/A grub_raid_block_mulx (255 - bad1, buf,
2N/A size);
2N/A }
2N/A else
2N/A {
2N/A /* Two bad devices */
2N/A int c;
2N/A
2N/A if ((! array->members[p].device) || (! array->members[q].device))
2N/A {
2N/A grub_error (GRUB_ERR_READ_ERROR, "not enough disk to restore");
2N/A goto quit;
2N/A }
2N/A
2N/A if (grub_disk_read (array->members[p].device,
2N/A array->members[p].start_sector + sector,
2N/A 0, size, buf))
2N/A goto quit;
2N/A
2N/A grub_crypto_xor (pbuf, pbuf, buf, size);
2N/A
2N/A if (grub_disk_read (array->members[q].device,
2N/A array->members[q].start_sector + sector,
2N/A 0, size, buf))
2N/A goto quit;
2N/A
2N/A grub_crypto_xor (qbuf, qbuf, buf, size);
2N/A
2N/A c = (255 - bad1 + (255 - powx_inv[(powx[bad2 - bad1 + 255] ^ 1)])) % 255;
2N/A grub_raid_block_mulx (c, qbuf, size);
2N/A
2N/A c = (bad2 + c) % 255;
2N/A grub_raid_block_mulx (c, pbuf, size);
2N/A
2N/A grub_crypto_xor (pbuf, pbuf, qbuf, size);
2N/A grub_memcpy (buf, pbuf, size);
2N/A }
2N/A
2N/Aquit:
2N/A grub_free (pbuf);
2N/A grub_free (qbuf);
2N/A
2N/A return grub_errno;
2N/A}
2N/A
2N/AGRUB_MOD_INIT(raid6rec)
2N/A{
2N/A grub_raid6_init_table ();
2N/A grub_raid6_recover_func = grub_raid6_recover;
2N/A}
2N/A
2N/AGRUB_MOD_FINI(raid6rec)
2N/A{
2N/A grub_raid6_recover_func = 0;
2N/A}