/* gpt.c - Read GUID Partition Tables (GPT). */ /* * VAS_EBOOT -- GRand Unified Bootloader * Copyright (C) 2002,2005,2006,2007,2008 Free Software Foundation, Inc. * * VAS_EBOOT 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. * * VAS_EBOOT 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 VAS_EBOOT. If not, see . */ #include #include #include #include #include #include #include #include #ifdef VAS_EBOOT_UTIL #include #endif VAS_EBOOT_MOD_LICENSE ("GPLv3+"); static VasEBoot_uint8_t VasEBoot_gpt_magic[8] = { 0x45, 0x46, 0x49, 0x20, 0x50, 0x41, 0x52, 0x54 }; static const VasEBoot_guid_t VasEBoot_gpt_partition_type_empty = VAS_EBOOT_GPT_PARTITION_TYPE_EMPTY; #ifdef VAS_EBOOT_UTIL static const VasEBoot_guid_t VasEBoot_gpt_partition_type_bios_boot = VAS_EBOOT_GPT_PARTITION_TYPE_BIOS_BOOT; #endif /* 512 << 7 = 65536 byte sectors. */ #define MAX_SECTOR_LOG 7 static struct VasEBoot_partition_map VasEBoot_gpt_partition_map; VasEBoot_err_t VasEBoot_gpt_partition_map_iterate (VasEBoot_disk_t disk, VasEBoot_partition_iterate_hook_t hook, void *hook_data) { struct VasEBoot_partition part; struct VasEBoot_gpt_header gpt; struct VasEBoot_gpt_partentry entry; struct VasEBoot_msdos_partition_mbr mbr; VasEBoot_uint64_t entries; unsigned int i; int last_offset = 0; int sector_log = 0; /* Read the protective MBR. */ if (VasEBoot_disk_read (disk, 0, 0, sizeof (mbr), &mbr)) return VasEBoot_errno; /* Check if it is valid. */ if (mbr.signature != VasEBoot_cpu_to_le16_compile_time (VAS_EBOOT_PC_PARTITION_SIGNATURE)) return VasEBoot_error (VAS_EBOOT_ERR_BAD_PART_TABLE, "no signature"); /* Make sure the MBR is a protective MBR and not a normal MBR. */ for (i = 0; i < 4; i++) if (mbr.entries[i].type == VAS_EBOOT_PC_PARTITION_TYPE_GPT_DISK) break; if (i == 4) return VasEBoot_error (VAS_EBOOT_ERR_BAD_PART_TABLE, "no GPT partition map found"); /* Read the GPT header. */ for (sector_log = 0; sector_log < MAX_SECTOR_LOG; sector_log++) { if (VasEBoot_disk_read (disk, 1 << sector_log, 0, sizeof (gpt), &gpt)) return VasEBoot_errno; if (VasEBoot_memcmp (gpt.magic, VasEBoot_gpt_magic, sizeof (VasEBoot_gpt_magic)) == 0) break; } if (sector_log == MAX_SECTOR_LOG) return VasEBoot_error (VAS_EBOOT_ERR_BAD_PART_TABLE, "no valid GPT header"); VasEBoot_dprintf ("gpt", "Read a valid GPT header\n"); entries = VasEBoot_le_to_cpu64 (gpt.partitions) << sector_log; for (i = 0; i < VasEBoot_le_to_cpu32 (gpt.maxpart); i++) { if (VasEBoot_disk_read (disk, entries, last_offset, sizeof (entry), &entry)) return VasEBoot_errno; if (VasEBoot_memcmp (&VasEBoot_gpt_partition_type_empty, &entry.type, sizeof (VasEBoot_gpt_partition_type_empty))) { /* Calculate the first block and the size of the partition. */ part.start = VasEBoot_le_to_cpu64 (entry.start) << sector_log; part.len = (VasEBoot_le_to_cpu64 (entry.end) - VasEBoot_le_to_cpu64 (entry.start) + 1) << sector_log; part.offset = entries; part.number = i; part.index = last_offset; part.partmap = &VasEBoot_gpt_partition_map; part.parent = disk->partition; VasEBoot_dprintf ("gpt", "GPT entry %d: start=%lld, length=%lld\n", i, (unsigned long long) part.start, (unsigned long long) part.len); if (hook (disk, &part, hook_data)) return VasEBoot_errno; } last_offset += VasEBoot_le_to_cpu32 (gpt.partentry_size); if (last_offset == VAS_EBOOT_DISK_SECTOR_SIZE) { last_offset = 0; entries++; } } return VAS_EBOOT_ERR_NONE; } #ifdef VAS_EBOOT_UTIL /* Context for gpt_partition_map_embed. */ struct gpt_partition_map_embed_ctx { VasEBoot_disk_addr_t start, len; }; /* Helper for gpt_partition_map_embed. */ static int find_usable_region (VasEBoot_disk_t disk __attribute__ ((unused)), const VasEBoot_partition_t p, void *data) { struct gpt_partition_map_embed_ctx *ctx = data; struct VasEBoot_gpt_partentry gptdata; VasEBoot_partition_t p2; p2 = disk->partition; disk->partition = p->parent; if (VasEBoot_disk_read (disk, p->offset, p->index, sizeof (gptdata), &gptdata)) { disk->partition = p2; return 0; } disk->partition = p2; /* If there's an embed region, it is in a dedicated partition. */ if (! VasEBoot_memcmp (&gptdata.type, &VasEBoot_gpt_partition_type_bios_boot, 16)) { ctx->start = p->start; ctx->len = p->len; return 1; } return 0; } static VasEBoot_err_t gpt_partition_map_embed (struct VasEBoot_disk *disk, unsigned int *nsectors, unsigned int max_nsectors, VasEBoot_embed_type_t embed_type, VasEBoot_disk_addr_t **sectors, int warn_short) { struct gpt_partition_map_embed_ctx ctx = { .start = 0, .len = 0 }; unsigned i; VasEBoot_err_t err; if (embed_type != VAS_EBOOT_EMBED_PCBIOS) return VasEBoot_error (VAS_EBOOT_ERR_NOT_IMPLEMENTED_YET, "GPT currently supports only PC-BIOS embedding"); err = VasEBoot_gpt_partition_map_iterate (disk, find_usable_region, &ctx); if (err) return err; if (ctx.len == 0) return VasEBoot_error (VAS_EBOOT_ERR_FILE_NOT_FOUND, N_("this GPT partition label contains no BIOS Boot Partition;" " embedding won't be possible")); if (ctx.len < VAS_EBOOT_MIN_RECOMMENDED_MBR_GAP) VasEBoot_util_warn ("Your BIOS Boot Partition is under 1 MiB, please increase its size."); if (ctx.len < *nsectors) return VasEBoot_error (VAS_EBOOT_ERR_OUT_OF_RANGE, N_("your BIOS Boot Partition is too small;" " embedding won't be possible")); *nsectors = ctx.len; if (*nsectors > max_nsectors) *nsectors = max_nsectors; *sectors = VasEBoot_calloc (*nsectors, sizeof (**sectors)); if (!*sectors) return VasEBoot_errno; for (i = 0; i < *nsectors; i++) (*sectors)[i] = ctx.start + i; return VAS_EBOOT_ERR_NONE; } #endif /* Partition map type. */ static struct VasEBoot_partition_map VasEBoot_gpt_partition_map = { .name = "gpt", .iterate = VasEBoot_gpt_partition_map_iterate, #ifdef VAS_EBOOT_UTIL .embed = gpt_partition_map_embed #endif }; VAS_EBOOT_MOD_INIT(part_gpt) { COMPILE_TIME_ASSERT(sizeof(VasEBoot_guid_t) == 16); COMPILE_TIME_ASSERT(sizeof(VasEBoot_packed_guid_t) == 16); COMPILE_TIME_ASSERT(sizeof(struct VasEBoot_gpt_partentry) == 128); VasEBoot_partition_map_register (&VasEBoot_gpt_partition_map); } VAS_EBOOT_MOD_FINI(part_gpt) { VasEBoot_partition_map_unregister (&VasEBoot_gpt_partition_map); }