155 lines
4.8 KiB
C
155 lines
4.8 KiB
C
/* sun.c - Read SUN style partition tables. */
|
|
/*
|
|
* VAS_EBOOT -- GRand Unified Bootloader
|
|
* Copyright (C) 2002,2005,2006,2007 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_MAGIC 0xDABE
|
|
#define VAS_EBOOT_PARTMAP_SUN_MAX_PARTS 8
|
|
#define VAS_EBOOT_PARTMAP_SUN_WHOLE_DISK_ID 0x05
|
|
|
|
struct VasEBoot_sun_partition_info
|
|
{
|
|
VasEBoot_uint8_t spare1;
|
|
VasEBoot_uint8_t id;
|
|
VasEBoot_uint8_t spare2;
|
|
VasEBoot_uint8_t flags;
|
|
} VAS_EBOOT_PACKED;
|
|
|
|
struct VasEBoot_sun_partition_descriptor
|
|
{
|
|
VasEBoot_uint32_t start_cylinder;
|
|
VasEBoot_uint32_t num_sectors;
|
|
} VAS_EBOOT_PACKED;
|
|
|
|
struct VasEBoot_sun_block
|
|
{
|
|
VasEBoot_uint8_t info[128]; /* Informative text string. */
|
|
VasEBoot_uint8_t spare0[14];
|
|
struct VasEBoot_sun_partition_info infos[8];
|
|
VasEBoot_uint8_t spare1[246]; /* Boot information etc. */
|
|
VasEBoot_uint16_t rspeed; /* Disk rotational speed. */
|
|
VasEBoot_uint16_t pcylcount; /* Physical cylinder count. */
|
|
VasEBoot_uint16_t sparecyl; /* extra sects per cylinder. */
|
|
VasEBoot_uint8_t spare2[4]; /* More magic... */
|
|
VasEBoot_uint16_t ilfact; /* Interleave factor. */
|
|
VasEBoot_uint16_t ncyl; /* Data cylinder count. */
|
|
VasEBoot_uint16_t nacyl; /* Alt. cylinder count. */
|
|
VasEBoot_uint16_t ntrks; /* Tracks per cylinder. */
|
|
VasEBoot_uint16_t nsect; /* Sectors per track. */
|
|
VasEBoot_uint8_t spare3[4]; /* Even more magic... */
|
|
struct VasEBoot_sun_partition_descriptor partitions[8];
|
|
VasEBoot_uint16_t magic; /* Magic number. */
|
|
VasEBoot_uint16_t csum; /* Label xor'd checksum. */
|
|
} VAS_EBOOT_PACKED;
|
|
|
|
static struct VasEBoot_partition_map VasEBoot_sun_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_block) / 2);
|
|
pos++)
|
|
sum ^= *pos;
|
|
|
|
return ! sum;
|
|
}
|
|
|
|
static VasEBoot_err_t
|
|
sun_partition_map_iterate (VasEBoot_disk_t disk,
|
|
VasEBoot_partition_iterate_hook_t hook, void *hook_data)
|
|
{
|
|
struct VasEBoot_partition p;
|
|
union
|
|
{
|
|
struct VasEBoot_sun_block sun_block;
|
|
VasEBoot_uint16_t raw[0];
|
|
} block;
|
|
int partnum;
|
|
VasEBoot_err_t err;
|
|
|
|
p.partmap = &VasEBoot_sun_partition_map;
|
|
err = VasEBoot_disk_read (disk, 0, 0, sizeof (struct VasEBoot_sun_block),
|
|
&block);
|
|
if (err)
|
|
return err;
|
|
|
|
if (VAS_EBOOT_PARTMAP_SUN_MAGIC != VasEBoot_be_to_cpu16 (block.sun_block.magic))
|
|
return VasEBoot_error (VAS_EBOOT_ERR_BAD_PART_TABLE, "not a sun partition table");
|
|
|
|
if (! VasEBoot_sun_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 < VAS_EBOOT_PARTMAP_SUN_MAX_PARTS; partnum++)
|
|
{
|
|
struct VasEBoot_sun_partition_descriptor *desc;
|
|
|
|
if (block.sun_block.infos[partnum].id == 0
|
|
|| block.sun_block.infos[partnum].id == VAS_EBOOT_PARTMAP_SUN_WHOLE_DISK_ID)
|
|
continue;
|
|
|
|
desc = &block.sun_block.partitions[partnum];
|
|
p.start = ((VasEBoot_uint64_t) VasEBoot_be_to_cpu32 (desc->start_cylinder)
|
|
* VasEBoot_be_to_cpu16 (block.sun_block.ntrks)
|
|
* VasEBoot_be_to_cpu16 (block.sun_block.nsect));
|
|
p.len = VasEBoot_be_to_cpu32 (desc->num_sectors);
|
|
p.number = p.index = partnum;
|
|
if (p.len)
|
|
{
|
|
if (hook (disk, &p, hook_data))
|
|
partnum = VAS_EBOOT_PARTMAP_SUN_MAX_PARTS;
|
|
}
|
|
}
|
|
|
|
return VasEBoot_errno;
|
|
}
|
|
|
|
/* Partition map type. */
|
|
static struct VasEBoot_partition_map VasEBoot_sun_partition_map =
|
|
{
|
|
.name = "sun",
|
|
.iterate = sun_partition_map_iterate,
|
|
};
|
|
|
|
VAS_EBOOT_MOD_INIT(part_sun)
|
|
{
|
|
VasEBoot_partition_map_register (&VasEBoot_sun_partition_map);
|
|
}
|
|
|
|
VAS_EBOOT_MOD_FINI(part_sun)
|
|
{
|
|
VasEBoot_partition_map_unregister (&VasEBoot_sun_partition_map);
|
|
}
|
|
|