vaseboot/VasEBoot-core/partmap/sunpc.c

152 lines
4.2 KiB
C

/* sunpc.c - Read SUN PC style partition tables. */
/*
* VAS_EBOOT -- GRand Unified Bootloader
* Copyright (C) 2002,2005,2006,2007,2009 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 <http://www.gnu.org/licenses/>.
*/
#include <VasEBoot/partition.h>
#include <VasEBoot/disk.h>
#include <VasEBoot/mm.h>
#include <VasEBoot/misc.h>
#include <VasEBoot/dl.h>
#include <VasEBoot/symbol.h>
#include <VasEBoot/types.h>
#include <VasEBoot/err.h>
VAS_EBOOT_MOD_LICENSE ("GPLv3+");
#define VAS_EBOOT_PARTMAP_SUN_PC_MAGIC 0xDABE
#define VAS_EBOOT_PARTMAP_SUN_PC_MAX_PARTS 16
#define VAS_EBOOT_PARTMAP_SUN_PC_WHOLE_DISK_ID 0x05
struct VasEBoot_sun_pc_partition_descriptor
{
VasEBoot_uint16_t id;
VasEBoot_uint16_t unused;
VasEBoot_uint32_t start_sector;
VasEBoot_uint32_t num_sectors;
} VAS_EBOOT_PACKED;
struct VasEBoot_sun_pc_block
{
VasEBoot_uint8_t unused[72];
struct VasEBoot_sun_pc_partition_descriptor partitions[VAS_EBOOT_PARTMAP_SUN_PC_MAX_PARTS];
VasEBoot_uint8_t unused2[244];
VasEBoot_uint16_t magic; /* Magic number. */
VasEBoot_uint16_t csum; /* Label xor'd checksum. */
} VAS_EBOOT_PACKED;
static struct VasEBoot_partition_map VasEBoot_sun_pc_partition_map;
/* Verify checksum (true=ok). */
static int
VasEBoot_sun_is_valid (VasEBoot_uint16_t *label)
{
VasEBoot_uint16_t *pos;
VasEBoot_uint16_t sum = 0;
for (pos = label;
pos < (label + sizeof (struct VasEBoot_sun_pc_block) / 2);
pos++)
sum ^= *pos;
return ! sum;
}
static VasEBoot_err_t
sun_pc_partition_map_iterate (VasEBoot_disk_t disk,
VasEBoot_partition_iterate_hook_t hook,
void *hook_data)
{
VasEBoot_partition_t p;
union
{
struct VasEBoot_sun_pc_block sun_block;
VasEBoot_uint16_t raw[0];
} block;
int partnum;
VasEBoot_err_t err;
p = (VasEBoot_partition_t) VasEBoot_zalloc (sizeof (struct VasEBoot_partition));
if (! p)
return VasEBoot_errno;
p->partmap = &VasEBoot_sun_pc_partition_map;
err = VasEBoot_disk_read (disk, 1, 0, sizeof (struct VasEBoot_sun_pc_block), &block);
if (err)
{
VasEBoot_free (p);
return err;
}
if (VAS_EBOOT_PARTMAP_SUN_PC_MAGIC != VasEBoot_le_to_cpu16 (block.sun_block.magic))
{
VasEBoot_free (p);
return VasEBoot_error (VAS_EBOOT_ERR_BAD_PART_TABLE,
"not a sun_pc partition table");
}
if (! VasEBoot_sun_is_valid (block.raw))
{
VasEBoot_free (p);
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 < VAS_EBOOT_PARTMAP_SUN_PC_MAX_PARTS; partnum++)
{
struct VasEBoot_sun_pc_partition_descriptor *desc;
if (block.sun_block.partitions[partnum].id == 0
|| block.sun_block.partitions[partnum].id
== VAS_EBOOT_PARTMAP_SUN_PC_WHOLE_DISK_ID)
continue;
desc = &block.sun_block.partitions[partnum];
p->start = VasEBoot_le_to_cpu32 (desc->start_sector);
p->len = VasEBoot_le_to_cpu32 (desc->num_sectors);
p->number = partnum;
if (p->len)
{
if (hook (disk, p, hook_data))
partnum = VAS_EBOOT_PARTMAP_SUN_PC_MAX_PARTS;
}
}
VasEBoot_free (p);
return VasEBoot_errno;
}
/* Partition map type. */
static struct VasEBoot_partition_map VasEBoot_sun_pc_partition_map =
{
.name = "sunpc",
.iterate = sun_pc_partition_map_iterate,
};
VAS_EBOOT_MOD_INIT(part_sunpc)
{
VasEBoot_partition_map_register (&VasEBoot_sun_pc_partition_map);
}
VAS_EBOOT_MOD_FINI(part_sunpc)
{
VasEBoot_partition_map_unregister (&VasEBoot_sun_pc_partition_map);
}