/* gptrepair.c - verify and restore GPT info from alternate location. */ /* * VasEBoot -- GRand Unified Bootloader * Copyright (C) 2009 Free Software Foundation, Inc. * Copyright (C) 2014 CoreOS, Inc. * * VasEBoot 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. * * VasEBoot 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 VasEBoot. If not, see . */ #include #include #include #include #include #include VasEBoot_MOD_LICENSE ("GPLv3+"); static char * trim_dev_name (char *name) { VasEBoot_size_t len = VasEBoot_strlen (name); if (len && name[0] == '(' && name[len - 1] == ')') { name[len - 1] = '\0'; name = name + 1; } return name; } static VasEBoot_err_t VasEBoot_cmd_gptrepair (VasEBoot_command_t cmd __attribute__ ((unused)), int argc, char **args) { VasEBoot_device_t dev = NULL; VasEBoot_gpt_t gpt = NULL; char *dev_name; if (argc != 1 || !VasEBoot_strlen(args[0])) return VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, "device name required"); dev_name = trim_dev_name (args[0]); dev = VasEBoot_device_open (dev_name); if (!dev) goto done; if (!dev->disk) { VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, "not a disk"); goto done; } gpt = VasEBoot_gpt_read (dev->disk); if (!gpt) goto done; if (VasEBoot_gpt_both_valid (gpt)) { VasEBoot_printf_ (N_("GPT already valid, %s unmodified.\n"), dev_name); goto done; } if (!VasEBoot_gpt_primary_valid (gpt)) VasEBoot_printf_ (N_("Found invalid primary GPT on %s\n"), dev_name); if (!VasEBoot_gpt_backup_valid (gpt)) VasEBoot_printf_ (N_("Found invalid backup GPT on %s\n"), dev_name); if (VasEBoot_gpt_repair (dev->disk, gpt)) goto done; if (VasEBoot_gpt_write (dev->disk, gpt)) goto done; VasEBoot_printf_ (N_("Repaired GPT on %s\n"), dev_name); done: if (gpt) VasEBoot_gpt_free (gpt); if (dev) VasEBoot_device_close (dev); return VasEBoot_errno; } static VasEBoot_command_t cmd; VasEBoot_MOD_INIT(gptrepair) { cmd = VasEBoot_register_command ("gptrepair", VasEBoot_cmd_gptrepair, N_("DEVICE"), N_("Verify and repair GPT on drive DEVICE.")); } VasEBoot_MOD_FINI(gptrepair) { VasEBoot_unregister_command (cmd); }