gpt: add write function and gptrepair command
The first hint of something practical, a command that can restore any of the GPT structures from the alternate location. New test case must run under QEMU because the loopback device used by the other unit tests does not support writing.
This commit is contained in:
parent
478458d404
commit
051545ddf0
6 changed files with 277 additions and 4 deletions
|
@ -826,6 +826,11 @@ module = {
|
|||
common = commands/gptsync.c;
|
||||
};
|
||||
|
||||
module = {
|
||||
name = gptrepair;
|
||||
common = commands/gptrepair.c;
|
||||
};
|
||||
|
||||
module = {
|
||||
name = gpt;
|
||||
common = lib/gpt.c;
|
||||
|
|
116
grub-core/commands/gptrepair.c
Normal file
116
grub-core/commands/gptrepair.c
Normal file
|
@ -0,0 +1,116 @@
|
|||
/* gptrepair.c - verify and restore GPT info from alternate location. */
|
||||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 2009 Free Software Foundation, Inc.
|
||||
* Copyright (C) 2014 CoreOS, Inc.
|
||||
*
|
||||
* 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/command.h>
|
||||
#include <grub/device.h>
|
||||
#include <grub/err.h>
|
||||
#include <grub/gpt_partition.h>
|
||||
#include <grub/i18n.h>
|
||||
#include <grub/misc.h>
|
||||
|
||||
GRUB_MOD_LICENSE ("GPLv3+");
|
||||
|
||||
static char *
|
||||
trim_dev_name (char *name)
|
||||
{
|
||||
grub_size_t len = grub_strlen (name);
|
||||
if (len && name[0] == '(' && name[len - 1] == ')')
|
||||
{
|
||||
name[len - 1] = '\0';
|
||||
name = name + 1;
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
static grub_err_t
|
||||
grub_cmd_gptrepair (grub_command_t cmd __attribute__ ((unused)),
|
||||
int argc, char **args)
|
||||
{
|
||||
grub_device_t dev = NULL;
|
||||
grub_gpt_t gpt = NULL;
|
||||
char *dev_name;
|
||||
grub_uint32_t primary_crc, backup_crc;
|
||||
enum grub_gpt_status old_status;
|
||||
|
||||
if (argc != 1 || !grub_strlen(args[0]))
|
||||
return grub_error (GRUB_ERR_BAD_ARGUMENT, "device name required");
|
||||
|
||||
dev_name = trim_dev_name (args[0]);
|
||||
dev = grub_device_open (dev_name);
|
||||
if (!dev)
|
||||
goto done;
|
||||
|
||||
if (!dev->disk)
|
||||
{
|
||||
grub_error (GRUB_ERR_BAD_ARGUMENT, "not a disk");
|
||||
goto done;
|
||||
}
|
||||
|
||||
gpt = grub_gpt_read (dev->disk);
|
||||
if (!gpt)
|
||||
goto done;
|
||||
|
||||
primary_crc = gpt->primary.crc32;
|
||||
backup_crc = gpt->backup.crc32;
|
||||
old_status = gpt->status;
|
||||
|
||||
if (grub_gpt_repair (dev->disk, gpt))
|
||||
goto done;
|
||||
|
||||
if (primary_crc == gpt->primary.crc32 &&
|
||||
backup_crc == gpt->backup.crc32 &&
|
||||
old_status && gpt->status)
|
||||
{
|
||||
grub_printf_ (N_("GPT already valid, %s unmodified.\n"), dev_name);
|
||||
goto done;
|
||||
}
|
||||
|
||||
if (grub_gpt_write (dev->disk, gpt))
|
||||
goto done;
|
||||
|
||||
if (!(old_status & GRUB_GPT_PRIMARY_VALID))
|
||||
grub_printf_ (N_("Primary GPT for %s repaired.\n"), dev_name);
|
||||
|
||||
if (!(old_status & GRUB_GPT_BACKUP_VALID))
|
||||
grub_printf_ (N_("Backup GPT for %s repaired.\n"), dev_name);
|
||||
|
||||
done:
|
||||
if (gpt)
|
||||
grub_gpt_free (gpt);
|
||||
|
||||
if (dev)
|
||||
grub_device_close (dev);
|
||||
|
||||
return grub_errno;
|
||||
}
|
||||
|
||||
static grub_command_t cmd;
|
||||
|
||||
GRUB_MOD_INIT(gptrepair)
|
||||
{
|
||||
cmd = grub_register_command ("gptrepair", grub_cmd_gptrepair,
|
||||
N_("DEVICE"),
|
||||
N_("Verify and repair GPT on drive DEVICE."));
|
||||
}
|
||||
|
||||
GRUB_MOD_FINI(gptrepair)
|
||||
{
|
||||
grub_unregister_command (cmd);
|
||||
}
|
|
@ -357,10 +357,46 @@ grub_gpt_repair (grub_disk_t disk, grub_gpt_t gpt)
|
|||
if (grub_gpt_header_check (&gpt->backup, gpt->log_sector_size))
|
||||
return grub_error (GRUB_ERR_BUG, "Generated invalid GPT backup header");
|
||||
|
||||
gpt->status |= (GRUB_GPT_PRIMARY_HEADER_VALID |
|
||||
GRUB_GPT_PRIMARY_ENTRIES_VALID |
|
||||
GRUB_GPT_BACKUP_HEADER_VALID |
|
||||
GRUB_GPT_BACKUP_ENTRIES_VALID);
|
||||
gpt->status |= GRUB_GPT_BOTH_VALID;
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
||||
static grub_err_t
|
||||
grub_gpt_write_table (grub_disk_t disk, grub_gpt_t gpt,
|
||||
struct grub_gpt_header *header)
|
||||
{
|
||||
grub_disk_addr_t addr;
|
||||
|
||||
if (grub_le_to_cpu32 (header->headersize) != sizeof (*header))
|
||||
return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
|
||||
"Header size is %u, must be %u",
|
||||
grub_le_to_cpu32 (header->headersize),
|
||||
sizeof (*header));
|
||||
|
||||
addr = grub_gpt_sector_to_addr (gpt, grub_le_to_cpu64 (header->header_lba));
|
||||
if (grub_disk_write (disk, addr, 0, sizeof (*header), header))
|
||||
return grub_errno;
|
||||
|
||||
addr = grub_gpt_sector_to_addr (gpt, grub_le_to_cpu64 (header->partitions));
|
||||
if (grub_disk_write (disk, addr, 0, gpt->entries_size, gpt->entries))
|
||||
return grub_errno;
|
||||
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
||||
grub_err_t
|
||||
grub_gpt_write (grub_disk_t disk, grub_gpt_t gpt)
|
||||
{
|
||||
/* TODO: update/repair protective MBRs too. */
|
||||
|
||||
if (!(gpt->status & GRUB_GPT_BOTH_VALID))
|
||||
return grub_error (GRUB_ERR_BAD_PART_TABLE, "Invalid GPT data");
|
||||
|
||||
if (grub_gpt_write_table (disk, gpt, &gpt->primary))
|
||||
return grub_errno;
|
||||
|
||||
if (grub_gpt_write_table (disk, gpt, &gpt->backup))
|
||||
return grub_errno;
|
||||
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue