/* pcpart.c - manipulate fdisk partitions */ /* * VAS_EBOOT -- GRand Unified Bootloader * Copyright (C) 2009 Free Software Foundation, Inc. * * This program 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 2 of the License, or * (at your option) any later version. * * This program 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 this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include #include #include #include #include #include #include #include #include #include VAS_EBOOT_MOD_LICENSE ("GPLv2+"); static int activate_table_handle = -1; static int type_table_handle = -1; static struct VasEBoot_parttool_argdesc VasEBoot_pcpart_bootargs[] = { {"boot", N_("Make partition active"), VAS_EBOOT_PARTTOOL_ARG_BOOL}, {0, 0, 0} }; static VasEBoot_err_t VasEBoot_pcpart_boot (const VasEBoot_device_t dev, const struct VasEBoot_parttool_args *args) { int i, index; VasEBoot_partition_t part; struct VasEBoot_msdos_partition_mbr mbr; if (dev->disk->partition->offset) return VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT, N_("not a primary partition")); index = dev->disk->partition->index; part = dev->disk->partition; dev->disk->partition = part->parent; /* Read the MBR. */ if (VasEBoot_disk_read (dev->disk, 0, 0, sizeof (mbr), &mbr)) { dev->disk->partition = part; return VasEBoot_errno; } if (args[0].set && args[0].b) { for (i = 0; i < 4; i++) mbr.entries[i].flag = 0x0; mbr.entries[index].flag = 0x80; VasEBoot_printf_ (N_("Partition %d is active now. \n"), index); } else { mbr.entries[index].flag = 0x0; VasEBoot_printf (N_("Cleared active flag on %d. \n"), index); } /* Write the MBR. */ VasEBoot_disk_write (dev->disk, 0, 0, sizeof (mbr), &mbr); dev->disk->partition = part; return VAS_EBOOT_ERR_NONE; } static struct VasEBoot_parttool_argdesc VasEBoot_pcpart_typeargs[] = { {"type", N_("Change partition type"), VAS_EBOOT_PARTTOOL_ARG_VAL}, {"hidden", N_("Set `hidden' flag in partition type"), VAS_EBOOT_PARTTOOL_ARG_BOOL}, {0, 0, 0} }; static VasEBoot_err_t VasEBoot_pcpart_type (const VasEBoot_device_t dev, const struct VasEBoot_parttool_args *args) { int index; VasEBoot_uint8_t type; VasEBoot_partition_t part; struct VasEBoot_msdos_partition_mbr mbr; index = dev->disk->partition->index; part = dev->disk->partition; dev->disk->partition = part->parent; /* Read the parttable. */ if (VasEBoot_disk_read (dev->disk, part->offset, 0, sizeof (mbr), &mbr)) { dev->disk->partition = part; return VasEBoot_errno; } if (args[0].set) type = VasEBoot_strtoul (args[0].str, 0, 0); else type = mbr.entries[index].type; if (args[1].set) { if (args[1].b) type |= VAS_EBOOT_PC_PARTITION_TYPE_HIDDEN_FLAG; else type &= ~VAS_EBOOT_PC_PARTITION_TYPE_HIDDEN_FLAG; } if (VasEBoot_msdos_partition_is_empty (type) || VasEBoot_msdos_partition_is_extended (type)) { dev->disk->partition = part; return VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT, N_("the partition type 0x%x isn't valid"), type); } mbr.entries[index].type = type; /* TRANSLATORS: In this case we're actually writing to the disk and actively modifying partition type rather than just defining it. */ VasEBoot_printf_ (N_("Setting partition type to 0x%x\n"), type); /* Write the parttable. */ VasEBoot_disk_write (dev->disk, part->offset, 0, sizeof (mbr), &mbr); dev->disk->partition = part; return VAS_EBOOT_ERR_NONE; } VAS_EBOOT_MOD_INIT (msdospart) { activate_table_handle = VasEBoot_parttool_register ("msdos", VasEBoot_pcpart_boot, VasEBoot_pcpart_bootargs); type_table_handle = VasEBoot_parttool_register ("msdos", VasEBoot_pcpart_type, VasEBoot_pcpart_typeargs); } VAS_EBOOT_MOD_FINI(msdospart) { VasEBoot_parttool_unregister (activate_table_handle); VasEBoot_parttool_unregister (type_table_handle); }