/* * VAS_EBOOT -- GRand Unified Bootloader * Copyright (C) 2002,2005,2006,2007,2011 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 VAS_EBOOT_MOD_LICENSE ("GPLv3+"); #define DVH_MAGIC 0x0be5a941 struct VasEBoot_dvh_partition_descriptor { VasEBoot_uint32_t length; VasEBoot_uint32_t start; VasEBoot_uint32_t type; } VAS_EBOOT_PACKED; struct VasEBoot_dvh_block { VasEBoot_uint32_t magic; VasEBoot_uint8_t unused[308]; struct VasEBoot_dvh_partition_descriptor parts[16]; VasEBoot_uint32_t checksum; VasEBoot_uint32_t unused2; } VAS_EBOOT_PACKED; static struct VasEBoot_partition_map VasEBoot_dvh_partition_map; /* Verify checksum (true=ok). */ static int VasEBoot_dvh_is_valid (VasEBoot_uint32_t *label) { VasEBoot_uint32_t *pos; VasEBoot_uint32_t sum = 0; for (pos = label; pos < (label + sizeof (struct VasEBoot_dvh_block) / 4); pos++) sum += VasEBoot_be_to_cpu32 (*pos); return ! sum; } static VasEBoot_err_t dvh_partition_map_iterate (VasEBoot_disk_t disk, VasEBoot_partition_iterate_hook_t hook, void *hook_data) { struct VasEBoot_partition p; union { struct VasEBoot_dvh_block dvh; VasEBoot_uint32_t raw[0]; } block; unsigned partnum; VasEBoot_err_t err; p.partmap = &VasEBoot_dvh_partition_map; err = VasEBoot_disk_read (disk, 0, 0, sizeof (struct VasEBoot_dvh_block), &block); if (err) return err; if (DVH_MAGIC != VasEBoot_be_to_cpu32 (block.dvh.magic)) return VasEBoot_error (VAS_EBOOT_ERR_BAD_PART_TABLE, "not a dvh partition table"); if (! VasEBoot_dvh_is_valid (block.raw)) return VasEBoot_error (VAS_EBOOT_ERR_BAD_PART_TABLE, "invalid checksum"); /* Maybe another error value would be better, because partition table _is_ recognized but invalid. */ for (partnum = 0; partnum < ARRAY_SIZE (block.dvh.parts); partnum++) { if (block.dvh.parts[partnum].length == 0) continue; if (partnum == 10) continue; p.start = VasEBoot_be_to_cpu32 (block.dvh.parts[partnum].start); p.len = VasEBoot_be_to_cpu32 (block.dvh.parts[partnum].length); p.number = p.index = partnum; if (hook (disk, &p, hook_data)) break; } return VasEBoot_errno; } /* Partition map type. */ static struct VasEBoot_partition_map VasEBoot_dvh_partition_map = { .name = "dvh", .iterate = dvh_partition_map_iterate, }; VAS_EBOOT_MOD_INIT(part_dvh) { VasEBoot_partition_map_register (&VasEBoot_dvh_partition_map); } VAS_EBOOT_MOD_FINI(part_dvh) { VasEBoot_partition_map_unregister (&VasEBoot_dvh_partition_map); }