184 lines
5.1 KiB
C
184 lines
5.1 KiB
C
/* amiga.c - Read amiga partition tables (RDB). */
|
||
/*
|
||
* VAS_EBOOT -- GRand Unified Bootloader
|
||
* Copyright (C) 2002,2004,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/disk.h>
|
||
#include <VasEBoot/misc.h>
|
||
#include <VasEBoot/mm.h>
|
||
#include <VasEBoot/partition.h>
|
||
#include <VasEBoot/dl.h>
|
||
|
||
VAS_EBOOT_MOD_LICENSE ("GPLv3+");
|
||
|
||
#define AMIGA_CHECKSUM_WORDS 128
|
||
|
||
struct VasEBoot_amiga_rdsk
|
||
{
|
||
/* "RDSK". */
|
||
VasEBoot_uint8_t magic[4];
|
||
#define VAS_EBOOT_AMIGA_RDSK_MAGIC "RDSK"
|
||
VasEBoot_uint32_t size;
|
||
VasEBoot_int32_t checksum;
|
||
VasEBoot_uint32_t scsihost;
|
||
VasEBoot_uint32_t blksz;
|
||
VasEBoot_uint32_t flags;
|
||
VasEBoot_uint32_t badblcklst;
|
||
VasEBoot_uint32_t partitionlst;
|
||
VasEBoot_uint32_t fslst;
|
||
|
||
VasEBoot_uint32_t unused[AMIGA_CHECKSUM_WORDS - 9];
|
||
} VAS_EBOOT_PACKED;
|
||
|
||
struct VasEBoot_amiga_partition
|
||
{
|
||
/* "PART". */
|
||
VasEBoot_uint8_t magic[4];
|
||
#define VAS_EBOOT_AMIGA_PART_MAGIC "PART"
|
||
VasEBoot_uint32_t size;
|
||
VasEBoot_int32_t checksum;
|
||
VasEBoot_uint32_t scsihost;
|
||
VasEBoot_uint32_t next;
|
||
VasEBoot_uint32_t flags;
|
||
VasEBoot_uint32_t unused1[2];
|
||
VasEBoot_uint32_t devflags;
|
||
VasEBoot_uint8_t namelen;
|
||
VasEBoot_uint8_t name[31];
|
||
VasEBoot_uint32_t unused2[15];
|
||
|
||
VasEBoot_uint32_t unused3[3];
|
||
VasEBoot_uint32_t heads;
|
||
VasEBoot_uint32_t unused4;
|
||
VasEBoot_uint32_t block_per_track;
|
||
VasEBoot_uint32_t unused5[3];
|
||
VasEBoot_uint32_t lowcyl;
|
||
VasEBoot_uint32_t highcyl;
|
||
|
||
VasEBoot_uint32_t firstcyl;
|
||
VasEBoot_uint32_t unused[AMIGA_CHECKSUM_WORDS - 44];
|
||
} VAS_EBOOT_PACKED;
|
||
|
||
static struct VasEBoot_partition_map VasEBoot_amiga_partition_map;
|
||
|
||
|
||
|
||
static VasEBoot_uint32_t
|
||
amiga_partition_map_checksum (void *buf)
|
||
{
|
||
VasEBoot_uint32_t *ptr = buf;
|
||
VasEBoot_uint32_t r = 0;
|
||
VasEBoot_size_t sz;
|
||
/* Fancy and quick way of checking sz >= 512 / 4 = 128. */
|
||
if (ptr[1] & ~VasEBoot_cpu_to_be32_compile_time (AMIGA_CHECKSUM_WORDS - 1))
|
||
sz = AMIGA_CHECKSUM_WORDS;
|
||
else
|
||
sz = VasEBoot_be_to_cpu32 (ptr[1]);
|
||
|
||
for (; sz; sz--, ptr++)
|
||
r += VasEBoot_be_to_cpu32 (*ptr);
|
||
|
||
return r;
|
||
}
|
||
|
||
static VasEBoot_err_t
|
||
amiga_partition_map_iterate (VasEBoot_disk_t disk,
|
||
VasEBoot_partition_iterate_hook_t hook,
|
||
void *hook_data)
|
||
{
|
||
struct VasEBoot_partition part;
|
||
struct VasEBoot_amiga_rdsk rdsk;
|
||
int partno = 0;
|
||
int next = -1;
|
||
unsigned pos;
|
||
|
||
/* The RDSK block is one of the first 15 blocks. */
|
||
for (pos = 0; pos < 15; pos++)
|
||
{
|
||
/* Read the RDSK block which is a descriptor for the entire disk. */
|
||
if (VasEBoot_disk_read (disk, pos, 0, sizeof (rdsk), &rdsk))
|
||
return VasEBoot_errno;
|
||
|
||
if (VasEBoot_memcmp (rdsk.magic, VAS_EBOOT_AMIGA_RDSK_MAGIC,
|
||
sizeof (rdsk.magic)) == 0
|
||
&& amiga_partition_map_checksum (&rdsk) == 0)
|
||
{
|
||
/* Found the first PART block. */
|
||
next = VasEBoot_be_to_cpu32 (rdsk.partitionlst);
|
||
break;
|
||
}
|
||
}
|
||
|
||
if (next == -1)
|
||
return VasEBoot_error (VAS_EBOOT_ERR_BAD_PART_TABLE,
|
||
"Amiga partition map not found");
|
||
|
||
/* The end of the partition list is marked using "-1". */
|
||
while (next != -1)
|
||
{
|
||
struct VasEBoot_amiga_partition apart;
|
||
|
||
/* Read the RDSK block which is a descriptor for the entire disk. */
|
||
if (VasEBoot_disk_read (disk, next, 0, sizeof (apart), &apart))
|
||
return VasEBoot_errno;
|
||
|
||
if (VasEBoot_memcmp (apart.magic, VAS_EBOOT_AMIGA_PART_MAGIC,
|
||
sizeof (apart.magic)) != 0
|
||
|| amiga_partition_map_checksum (&apart) != 0)
|
||
return VasEBoot_error (VAS_EBOOT_ERR_BAD_PART_TABLE,
|
||
"invalid Amiga partition map");
|
||
/* Calculate the first block and the size of the partition. */
|
||
part.start = (VasEBoot_be_to_cpu32 (apart.lowcyl)
|
||
* VasEBoot_be_to_cpu32 (apart.heads)
|
||
* VasEBoot_be_to_cpu32 (apart.block_per_track));
|
||
part.len = ((VasEBoot_be_to_cpu32 (apart.highcyl)
|
||
- VasEBoot_be_to_cpu32 (apart.lowcyl) + 1)
|
||
* VasEBoot_be_to_cpu32 (apart.heads)
|
||
* VasEBoot_be_to_cpu32 (apart.block_per_track));
|
||
|
||
part.offset = next;
|
||
part.number = partno;
|
||
part.index = 0;
|
||
part.partmap = &VasEBoot_amiga_partition_map;
|
||
|
||
if (hook (disk, &part, hook_data))
|
||
return VasEBoot_errno;
|
||
|
||
next = VasEBoot_be_to_cpu32 (apart.next);
|
||
partno++;
|
||
}
|
||
|
||
return 0;
|
||
}
|
||
|
||
|
||
/* Partition map type. */
|
||
static struct VasEBoot_partition_map VasEBoot_amiga_partition_map =
|
||
{
|
||
.name = "amiga",
|
||
.iterate = amiga_partition_map_iterate,
|
||
};
|
||
|
||
VAS_EBOOT_MOD_INIT(part_amiga)
|
||
{
|
||
VasEBoot_partition_map_register (&VasEBoot_amiga_partition_map);
|
||
}
|
||
|
||
VAS_EBOOT_MOD_FINI(part_amiga)
|
||
{
|
||
VasEBoot_partition_map_unregister (&VasEBoot_amiga_partition_map);
|
||
}
|