/* bsdlabel.c - Read BSD style partition tables. */ /* * VasEBoot -- GRand Unified Bootloader * Copyright (C) 2002,2004,2005,2006,2007,2008,2009 Free Software Foundation, 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 #include #include VasEBoot_MOD_LICENSE ("GPLv3+"); #ifdef VasEBoot_UTIL #include #endif static struct VasEBoot_partition_map VasEBoot_bsdlabel_partition_map; static struct VasEBoot_partition_map VasEBoot_netbsdlabel_partition_map; static struct VasEBoot_partition_map VasEBoot_openbsdlabel_partition_map; static VasEBoot_err_t iterate_real (VasEBoot_disk_t disk, VasEBoot_disk_addr_t sector, int freebsd, struct VasEBoot_partition_map *pmap, VasEBoot_partition_iterate_hook_t hook, void *hook_data) { struct VasEBoot_partition_bsd_disk_label label; struct VasEBoot_partition p; VasEBoot_disk_addr_t delta = 0; VasEBoot_disk_addr_t pos; /* Read the BSD label. */ if (VasEBoot_disk_read (disk, sector, 0, sizeof (label), &label)) return VasEBoot_errno; /* Check if it is valid. */ if (label.magic != VasEBoot_cpu_to_le32_compile_time (VasEBoot_PC_PARTITION_BSD_LABEL_MAGIC)) return VasEBoot_error (VasEBoot_ERR_BAD_PART_TABLE, "no signature"); /* A kludge to determine a base of be.offset. */ if (VasEBoot_PC_PARTITION_BSD_LABEL_WHOLE_DISK_PARTITION < VasEBoot_cpu_to_le16 (label.num_partitions) && freebsd) { struct VasEBoot_partition_bsd_entry whole_disk_be; pos = sizeof (label) + sector * VasEBoot_DISK_SECTOR_SIZE + sizeof (struct VasEBoot_partition_bsd_entry) * VasEBoot_PC_PARTITION_BSD_LABEL_WHOLE_DISK_PARTITION; if (VasEBoot_disk_read (disk, pos / VasEBoot_DISK_SECTOR_SIZE, pos % VasEBoot_DISK_SECTOR_SIZE, sizeof (whole_disk_be), &whole_disk_be)) return VasEBoot_errno; delta = VasEBoot_le_to_cpu32 (whole_disk_be.offset); } pos = sizeof (label) + sector * VasEBoot_DISK_SECTOR_SIZE; for (p.number = 0; p.number < VasEBoot_cpu_to_le16 (label.num_partitions); p.number++, pos += sizeof (struct VasEBoot_partition_bsd_entry)) { struct VasEBoot_partition_bsd_entry be; if (p.number == VasEBoot_PC_PARTITION_BSD_LABEL_WHOLE_DISK_PARTITION) continue; p.offset = pos / VasEBoot_DISK_SECTOR_SIZE; p.index = pos % VasEBoot_DISK_SECTOR_SIZE; if (VasEBoot_disk_read (disk, p.offset, p.index, sizeof (be), &be)) return VasEBoot_errno; p.start = VasEBoot_le_to_cpu32 (be.offset); p.len = VasEBoot_le_to_cpu32 (be.size); p.partmap = pmap; if (p.len == 0) continue; if (p.start < delta) { #ifdef VasEBoot_UTIL char *partname; /* disk->partition != NULL as 0 < delta */ partname = disk->partition ? VasEBoot_partition_get_name (disk->partition) : 0; VasEBoot_util_warn (_("Discarding improperly nested partition (%s,%s,%s%d)"), disk->name, partname ? : "", p.partmap->name, p.number + 1); VasEBoot_free (partname); #endif continue; } p.start -= delta; if (hook (disk, &p, hook_data)) return VasEBoot_errno; } return VasEBoot_ERR_NONE; } static VasEBoot_err_t bsdlabel_partition_map_iterate (VasEBoot_disk_t disk, VasEBoot_partition_iterate_hook_t hook, void *hook_data) { if (disk->partition && VasEBoot_strcmp (disk->partition->partmap->name, "msdos") == 0 && disk->partition->msdostype == VasEBoot_PC_PARTITION_TYPE_FREEBSD) return iterate_real (disk, VasEBoot_PC_PARTITION_BSD_LABEL_SECTOR, 1, &VasEBoot_bsdlabel_partition_map, hook, hook_data); if (disk->partition && (VasEBoot_strcmp (disk->partition->partmap->name, "msdos") == 0 || disk->partition->partmap == &VasEBoot_bsdlabel_partition_map || disk->partition->partmap == &VasEBoot_netbsdlabel_partition_map || disk->partition->partmap == &VasEBoot_openbsdlabel_partition_map)) return VasEBoot_error (VasEBoot_ERR_BAD_PART_TABLE, "no embedding supported"); return iterate_real (disk, VasEBoot_PC_PARTITION_BSD_LABEL_SECTOR, 0, &VasEBoot_bsdlabel_partition_map, hook, hook_data); } /* Context for netopenbsdlabel_partition_map_iterate. */ struct netopenbsdlabel_ctx { VasEBoot_uint8_t type; struct VasEBoot_partition_map *pmap; VasEBoot_partition_iterate_hook_t hook; void *hook_data; int count; }; /* Helper for netopenbsdlabel_partition_map_iterate. */ static int check_msdos (VasEBoot_disk_t dsk, const VasEBoot_partition_t partition, void *data) { struct netopenbsdlabel_ctx *ctx = data; VasEBoot_err_t err; if (partition->msdostype != ctx->type) return 0; err = iterate_real (dsk, partition->start + VasEBoot_PC_PARTITION_BSD_LABEL_SECTOR, 0, ctx->pmap, ctx->hook, ctx->hook_data); if (err == VasEBoot_ERR_NONE) { ctx->count++; return 1; } if (err == VasEBoot_ERR_BAD_PART_TABLE) { VasEBoot_errno = VasEBoot_ERR_NONE; return 0; } VasEBoot_print_error (); return 0; } /* This is a total breakage. Even when net-/openbsd label is inside partition it actually describes the whole disk. */ static VasEBoot_err_t netopenbsdlabel_partition_map_iterate (VasEBoot_disk_t disk, VasEBoot_uint8_t type, struct VasEBoot_partition_map *pmap, VasEBoot_partition_iterate_hook_t hook, void *hook_data) { if (disk->partition && VasEBoot_strcmp (disk->partition->partmap->name, "msdos") == 0) return VasEBoot_error (VasEBoot_ERR_BAD_PART_TABLE, "no embedding supported"); { struct netopenbsdlabel_ctx ctx = { .type = type, .pmap = pmap, .hook = hook, .hook_data = hook_data, .count = 0 }; VasEBoot_err_t err; err = VasEBoot_partition_msdos_iterate (disk, check_msdos, &ctx); if (err) return err; if (!ctx.count) return VasEBoot_error (VasEBoot_ERR_BAD_PART_TABLE, "no bsdlabel found"); } return VasEBoot_ERR_NONE; } static VasEBoot_err_t netbsdlabel_partition_map_iterate (VasEBoot_disk_t disk, VasEBoot_partition_iterate_hook_t hook, void *hook_data) { return netopenbsdlabel_partition_map_iterate (disk, VasEBoot_PC_PARTITION_TYPE_NETBSD, &VasEBoot_netbsdlabel_partition_map, hook, hook_data); } static VasEBoot_err_t openbsdlabel_partition_map_iterate (VasEBoot_disk_t disk, VasEBoot_partition_iterate_hook_t hook, void *hook_data) { return netopenbsdlabel_partition_map_iterate (disk, VasEBoot_PC_PARTITION_TYPE_OPENBSD, &VasEBoot_openbsdlabel_partition_map, hook, hook_data); } static struct VasEBoot_partition_map VasEBoot_bsdlabel_partition_map = { .name = "bsd", .iterate = bsdlabel_partition_map_iterate, }; static struct VasEBoot_partition_map VasEBoot_openbsdlabel_partition_map = { .name = "openbsd", .iterate = openbsdlabel_partition_map_iterate, }; static struct VasEBoot_partition_map VasEBoot_netbsdlabel_partition_map = { .name = "netbsd", .iterate = netbsdlabel_partition_map_iterate, }; VasEBoot_MOD_INIT(part_bsd) { VasEBoot_partition_map_register (&VasEBoot_bsdlabel_partition_map); VasEBoot_partition_map_register (&VasEBoot_netbsdlabel_partition_map); VasEBoot_partition_map_register (&VasEBoot_openbsdlabel_partition_map); } VasEBoot_MOD_FINI(part_bsd) { VasEBoot_partition_map_unregister (&VasEBoot_bsdlabel_partition_map); VasEBoot_partition_map_unregister (&VasEBoot_netbsdlabel_partition_map); VasEBoot_partition_map_unregister (&VasEBoot_openbsdlabel_partition_map); }