/* dfly.c - Read DragonFly BSD disklabel64. */
/*
* VAS_EBOOT -- GRand Unified Bootloader
* Copyright (C) 2013 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
VAS_EBOOT_MOD_LICENSE ("GPLv3+");
static struct VasEBoot_partition_map VasEBoot_dfly_partition_map;
#define VAS_EBOOT_PARTITION_DISKLABEL64_HEADER_SIZE 200
/* Full entry is 64 bytes however we really care only
about offset and size which are in first 16 bytes.
Avoid using too much stack. */
#define VAS_EBOOT_PARTITION_DISKLABEL64_ENTRY_SIZE 64
struct VasEBoot_partition_disklabel64_entry
{
VasEBoot_uint64_t boffset;
VasEBoot_uint64_t bsize;
};
/* Full entry is 200 bytes however we really care only
about magic and number of partitions which are in first 16 bytes.
Avoid using too much stack. */
struct VasEBoot_partition_disklabel64
{
VasEBoot_uint32_t magic;
#define VAS_EBOOT_DISKLABEL64_MAGIC ((VasEBoot_uint32_t)0xc4464c59)
VasEBoot_uint32_t crc;
VasEBoot_uint32_t unused;
VasEBoot_uint32_t npartitions;
};
static VasEBoot_err_t
dfly_partition_map_iterate (VasEBoot_disk_t disk,
VasEBoot_partition_iterate_hook_t hook,
void *hook_data)
{
struct VasEBoot_partition part;
unsigned partno, pos;
struct VasEBoot_partition_disklabel64 label;
part.partmap = &VasEBoot_dfly_partition_map;
if (VasEBoot_disk_read (disk, 1, 0, sizeof (label), &label))
return VasEBoot_errno;
if (label.magic != VasEBoot_cpu_to_le32_compile_time (VAS_EBOOT_DISKLABEL64_MAGIC))
{
VasEBoot_dprintf ("partition",
"bad magic (found 0x%x; wanted 0x%x)\n",
(unsigned int) VasEBoot_le_to_cpu32 (label.magic),
(unsigned int) VAS_EBOOT_DISKLABEL64_MAGIC);
return VasEBoot_error (VAS_EBOOT_ERR_BAD_PART_TABLE, "disklabel64 not found");
}
pos = VAS_EBOOT_PARTITION_DISKLABEL64_HEADER_SIZE + VAS_EBOOT_DISK_SECTOR_SIZE;
for (partno = 0;
partno < VasEBoot_le_to_cpu32 (label.npartitions); ++partno)
{
VasEBoot_disk_addr_t sector = pos >> VAS_EBOOT_DISK_SECTOR_BITS;
VasEBoot_off_t offset = pos & (VAS_EBOOT_DISK_SECTOR_SIZE - 1);
struct VasEBoot_partition_disklabel64_entry dpart;
pos += VAS_EBOOT_PARTITION_DISKLABEL64_ENTRY_SIZE;
if (VasEBoot_disk_read (disk, sector, offset, sizeof (dpart), &dpart))
return VasEBoot_errno;
VasEBoot_dprintf ("partition",
"partition %2d: offset 0x%llx, "
"size 0x%llx\n",
partno,
(unsigned long long) VasEBoot_le_to_cpu64 (dpart.boffset),
(unsigned long long) VasEBoot_le_to_cpu64 (dpart.bsize));
/* Is partition initialized? */
if (dpart.bsize == 0)
continue;
part.number = partno;
part.start = VasEBoot_le_to_cpu64 (dpart.boffset) >> VAS_EBOOT_DISK_SECTOR_BITS;
part.len = VasEBoot_le_to_cpu64 (dpart.bsize) >> VAS_EBOOT_DISK_SECTOR_BITS;
/* This is counter-intuitive, but part.offset and sector have
* the same type, and offset (NOT part.offset) is guaranteed
* to fit into part.index. */
part.offset = sector;
part.index = offset;
if (hook (disk, &part, hook_data))
return VasEBoot_errno;
}
return VAS_EBOOT_ERR_NONE;
}
/* Partition map type. */
static struct VasEBoot_partition_map VasEBoot_dfly_partition_map =
{
.name = "dfly",
.iterate = dfly_partition_map_iterate,
};
VAS_EBOOT_MOD_INIT(part_dfly)
{
VasEBoot_partition_map_register (&VasEBoot_dfly_partition_map);
}
VAS_EBOOT_MOD_FINI(part_dfly)
{
VasEBoot_partition_map_unregister (&VasEBoot_dfly_partition_map);
}