1N/A/*
1N/A libparted/fs_amiga - amiga file system support.
1N/A Copyright (C) 2000-2001, 2007, 2009-2010 Free Software Foundation,
1N/A Inc.
1N/A
1N/A This program is free software; you can redistribute it and/or modify
1N/A it under the terms of the GNU General Public License as published by
1N/A the Free Software Foundation; either version 3 of the License, or
1N/A (at your option) any later version.
1N/A
1N/A This program is distributed in the hope that it will be useful,
1N/A but WITHOUT ANY WARRANTY; without even the implied warranty of
1N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1N/A GNU General Public License for more details.
1N/A
1N/A You should have received a copy of the GNU General Public License
1N/A along with this program. If not, see <http://www.gnu.org/licenses/>.
1N/A
1N/A Contributor: Sven Luther <luther@debian.org>
1N/A*/
1N/A
1N/A#include <config.h>
1N/A#include <parted/parted.h>
1N/A#include <parted/debug.h>
1N/A#include <parted/endian.h>
1N/A
1N/A#include "amiga.h"
1N/A
1N/A#if ENABLE_NLS
1N/A# include <libintl.h>
1N/A# define _(String) dgettext (PACKAGE, String)
1N/A#else
1N/A# define _(String) (String)
1N/A#endif /* ENABLE_NLS */
1N/A
1N/A#define IDNAME_RIGIDDISK (uint32_t)0x5244534B /* 'RDSK' */
1N/A#define IDNAME_BADBLOCK (uint32_t)0x42414442 /* 'BADB' */
1N/A#define IDNAME_PARTITION (uint32_t)0x50415254 /* 'PART' */
1N/A#define IDNAME_FILESYSHEADER (uint32_t)0x46534844 /* 'FSHD' */
1N/A#define IDNAME_LOADSEG (uint32_t)0x4C534547 /* 'LSEG' */
1N/A#define IDNAME_BOOT (uint32_t)0x424f4f54 /* 'BOOT' */
1N/A#define IDNAME_FREE (uint32_t)0xffffffff
1N/A
1N/Astatic const char *
1N/A_amiga_block_id (uint32_t id) {
1N/A switch (id) {
1N/A case IDNAME_RIGIDDISK :
1N/A return "RDSK";
1N/A case IDNAME_BADBLOCK :
1N/A return "BADB";
1N/A case IDNAME_PARTITION :
1N/A return "PART";
1N/A case IDNAME_FILESYSHEADER :
1N/A return "FSHD";
1N/A case IDNAME_LOADSEG :
1N/A return "LSEG";
1N/A case IDNAME_BOOT :
1N/A return "BOOT";
1N/A case IDNAME_FREE :
1N/A return "<free>";
1N/A default :
1N/A return "<unknown>";
1N/A }
1N/A}
1N/A
1N/Astruct AmigaIds *
1N/A_amiga_add_id (uint32_t id, struct AmigaIds *ids) {
1N/A struct AmigaIds *newid;
1N/A
1N/A if ((newid=ped_malloc(sizeof (struct AmigaIds)))==NULL) {
1N/A ped_exception_throw(PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
1N/A _("%s : Failed to allocate id list element\n"), __func__);
1N/A return 0;
1N/A }
1N/A newid->ID = id;
1N/A newid->next = ids;
1N/A return newid;
1N/A}
1N/A
1N/Avoid
1N/A_amiga_free_ids (struct AmigaIds *ids) {
1N/A struct AmigaIds *current, *next;
1N/A
1N/A for (current = ids; current != NULL; current = next) {
1N/A next = current->next;
1N/A free (current);
1N/A }
1N/A}
1N/Aint
1N/A_amiga_id_in_list (uint32_t id, struct AmigaIds *ids) {
1N/A struct AmigaIds *current;
1N/A
1N/A for (current = ids; current != NULL; current = current->next) {
1N/A if (id == current->ID)
1N/A return 1;
1N/A }
1N/A return 0;
1N/A}
1N/A
1N/A#define AMIGA_RDB_NOT_FOUND ((uint32_t)0xffffffff)
1N/A
1N/Astruct AmigaBlock {
1N/A uint32_t amiga_ID; /* Identifier 32 bit word */
1N/A uint32_t amiga_SummedLongss; /* Size of the structure for checksums */
1N/A int32_t amiga_ChkSum; /* Checksum of the structure */
1N/A};
1N/A#define AMIGA(pos) ((struct AmigaBlock *)(pos))
1N/A
1N/Astruct RigidDiskBlock {
1N/A uint32_t rdb_ID; /* Identifier 32 bit word : 'RDSK' */
1N/A uint32_t rdb_SummedLongs; /* Size of the structure for checksums */
1N/A int32_t rdb_ChkSum; /* Checksum of the structure */
1N/A uint32_t rdb_HostID; /* SCSI Target ID of host, not really used */
1N/A uint32_t rdb_BlockBytes; /* Size of disk blocks */
1N/A uint32_t rdb_Flags; /* RDB Flags */
1N/A /* block list heads */
1N/A uint32_t rdb_BadBlockList; /* Bad block list */
1N/A uint32_t rdb_PartitionList; /* Partition list */
1N/A uint32_t rdb_FileSysHeaderList; /* File system header list */
1N/A uint32_t rdb_DriveInit; /* Drive specific init code */
1N/A uint32_t rdb_BootBlockList; /* Amiga OS 4 Boot Blocks */
1N/A uint32_t rdb_Reserved1[5]; /* Unused word, need to be set to $ffffffff */
1N/A /* physical drive characteristics */
1N/A uint32_t rdb_Cylinders; /* Number of the cylinders of the drive */
1N/A uint32_t rdb_Sectors; /* Number of sectors of the drive */
1N/A uint32_t rdb_Heads; /* Number of heads of the drive */
1N/A uint32_t rdb_Interleave; /* Interleave */
1N/A uint32_t rdb_Park; /* Head parking cylinder */
1N/A uint32_t rdb_Reserved2[3]; /* Unused word, need to be set to $ffffffff */
1N/A uint32_t rdb_WritePreComp; /* Starting cylinder of write precompensation */
1N/A uint32_t rdb_ReducedWrite; /* Starting cylinder of reduced write current */
1N/A uint32_t rdb_StepRate; /* Step rate of the drive */
1N/A uint32_t rdb_Reserved3[5]; /* Unused word, need to be set to $ffffffff */
1N/A /* logical drive characteristics */
1N/A uint32_t rdb_RDBBlocksLo; /* low block of range reserved for hardblocks */
1N/A uint32_t rdb_RDBBlocksHi; /* high block of range for these hardblocks */
1N/A uint32_t rdb_LoCylinder; /* low cylinder of partitionable disk area */
1N/A uint32_t rdb_HiCylinder; /* high cylinder of partitionable data area */
1N/A uint32_t rdb_CylBlocks; /* number of blocks available per cylinder */
1N/A uint32_t rdb_AutoParkSeconds; /* zero for no auto park */
1N/A uint32_t rdb_HighRDSKBlock; /* highest block used by RDSK */
1N/A /* (not including replacement bad blocks) */
1N/A uint32_t rdb_Reserved4;
1N/A /* drive identification */
1N/A char rdb_DiskVendor[8];
1N/A char rdb_DiskProduct[16];
1N/A char rdb_DiskRevision[4];
1N/A char rdb_ControllerVendor[8];
1N/A char rdb_ControllerProduct[16];
1N/A char rdb_ControllerRevision[4];
1N/A uint32_t rdb_Reserved5[10];
1N/A};
1N/A
1N/A#define AMIGA_MAX_PARTITIONS 128
1N/A#define RDB_LOCATION_LIMIT 16
1N/A#define RDSK(pos) ((struct RigidDiskBlock *)(pos))
1N/A
1N/Astatic int
1N/A_amiga_checksum (struct AmigaBlock *blk) {
1N/A uint32_t *rdb = (uint32_t *) blk;
1N/A uint32_t sum;
1N/A int i, end;
1N/A
1N/A sum = PED_BE32_TO_CPU (rdb[0]);
1N/A end = PED_BE32_TO_CPU (rdb[1]);
1N/A
1N/A if (end > PED_SECTOR_SIZE_DEFAULT) end = PED_SECTOR_SIZE_DEFAULT;
1N/A
1N/A for (i = 1; i < end; i++) sum += PED_BE32_TO_CPU (rdb[i]);
1N/A
1N/A return sum;
1N/A}
1N/A
1N/Astatic void
1N/A_amiga_calculate_checksum (struct AmigaBlock *blk) {
1N/A
1N/A blk->amiga_ChkSum = PED_CPU_TO_BE32(
1N/A PED_BE32_TO_CPU(blk->amiga_ChkSum) -
1N/A _amiga_checksum((struct AmigaBlock *) blk));
1N/A return;
1N/A}
1N/A
1N/A
1N/Astatic struct AmigaBlock *
1N/A_amiga_read_block (PedDevice *dev, struct AmigaBlock *blk, PedSector block, struct AmigaIds *ids) {
1N/A if (!ped_device_read (dev, blk, block, 1)) {
1N/A switch (ped_exception_throw(PED_EXCEPTION_ERROR,
1N/A PED_EXCEPTION_CANCEL,
1N/A _("%s : Couldn't read block %llu\n"), __func__, block))
1N/A {
1N/A case PED_EXCEPTION_CANCEL :
1N/A case PED_EXCEPTION_UNHANDLED :
1N/A default :
1N/A return NULL;
1N/A }
1N/A }
1N/A if (ids && !_amiga_id_in_list(PED_BE32_TO_CPU(blk->amiga_ID), ids))
1N/A return NULL;
1N/A if (_amiga_checksum (blk) != 0) {
1N/A switch (ped_exception_throw(PED_EXCEPTION_ERROR,
1N/A PED_EXCEPTION_FIX | PED_EXCEPTION_IGNORE | PED_EXCEPTION_CANCEL,
1N/A _("%s : Bad checksum on block %llu of type %s\n"),
1N/A __func__, block, _amiga_block_id(PED_BE32_TO_CPU(blk->amiga_ID))))
1N/A {
1N/A case PED_EXCEPTION_CANCEL :
1N/A return NULL;
1N/A case PED_EXCEPTION_FIX :
1N/A _amiga_calculate_checksum(AMIGA(blk));
1N/A if (!ped_device_write (dev, blk, block, 1)) {
1N/A switch (ped_exception_throw(PED_EXCEPTION_FATAL,
1N/A PED_EXCEPTION_CANCEL,
1N/A _("%s : Couldn't write block %d\n"), __func__, block))
1N/A {
1N/A case PED_EXCEPTION_CANCEL :
1N/A case PED_EXCEPTION_UNHANDLED :
1N/A default :
1N/A return NULL;
1N/A }
1N/A }
1N/A case PED_EXCEPTION_IGNORE :
1N/A case PED_EXCEPTION_UNHANDLED :
1N/A default :
1N/A return blk;
1N/A }
1N/A }
1N/A return blk;
1N/A}
1N/A
1N/Astatic uint32_t
1N/A_amiga_find_rdb (PedDevice *dev, struct RigidDiskBlock *rdb) {
1N/A int i;
1N/A struct AmigaIds *ids;
1N/A
1N/A ids = _amiga_add_id (IDNAME_RIGIDDISK, NULL);
1N/A
1N/A for (i = 0; i<RDB_LOCATION_LIMIT; i++) {
1N/A if (!_amiga_read_block (dev, AMIGA(rdb), i, ids)) {
1N/A continue;
1N/A }
1N/A if (PED_BE32_TO_CPU (rdb->rdb_ID) == IDNAME_RIGIDDISK) {
1N/A _amiga_free_ids (ids);
1N/A return i;
1N/A }
1N/A }
1N/A _amiga_free_ids (ids);
1N/A return AMIGA_RDB_NOT_FOUND;
1N/A}
1N/A
1N/Astatic int
1N/A_amiga_loop_check (uint32_t block, uint32_t * blocklist, uint32_t max)
1N/A{
1N/A uint32_t i;
1N/A
1N/A for (i = 0; i < max; i++)
1N/A if (block == blocklist[i]) {
1N/A /* We are looping, let's stop. */
1N/A return 1;
1N/A }
1N/A blocklist[max] = block;
1N/A return 0;
1N/A}
1N/A
1N/A/* We have already allocated a rdb, we are now reading it from the disk */
1N/Astruct PartitionBlock *
1N/Aamiga_find_part (PedGeometry *geom, struct PartitionBlock *part)
1N/A{
1N/A struct RigidDiskBlock *rdb;
1N/A uint32_t partblock;
1N/A uint32_t partlist[AMIGA_MAX_PARTITIONS];
1N/A int i;
1N/A
1N/A PED_ASSERT(geom!= NULL, return NULL);
1N/A PED_ASSERT(geom->dev!= NULL, return NULL);
1N/A
1N/A if (!(rdb = ped_malloc (PED_SECTOR_SIZE_DEFAULT))) {
1N/A switch (ped_exception_throw(PED_EXCEPTION_ERROR,
1N/A PED_EXCEPTION_CANCEL,
1N/A _("%s : Failed to allocate disk_specific rdb block\n"), __func__))
1N/A {
1N/A case PED_EXCEPTION_CANCEL :
1N/A case PED_EXCEPTION_UNHANDLED :
1N/A default :
1N/A return NULL;
1N/A }
1N/A }
1N/A if (_amiga_find_rdb (geom->dev, rdb) == AMIGA_RDB_NOT_FOUND) {
1N/A switch (ped_exception_throw(PED_EXCEPTION_ERROR,
1N/A PED_EXCEPTION_CANCEL,
1N/A _("%s : Didn't find rdb block, should never happen\n"), __func__))
1N/A {
1N/A case PED_EXCEPTION_CANCEL :
1N/A case PED_EXCEPTION_UNHANDLED :
1N/A default :
1N/A free(rdb);
1N/A return NULL;
1N/A }
1N/A }
1N/A
1N/A /* We initialize the hardblock free list to detect loops */
1N/A for (i = 0; i < AMIGA_MAX_PARTITIONS; i++) partlist[i] = IDNAME_FREE;
1N/A
1N/A for (i = 1, partblock = PED_BE32_TO_CPU(rdb->rdb_PartitionList);
1N/A i < AMIGA_MAX_PARTITIONS && partblock != IDNAME_FREE;
1N/A i++, partblock = PED_BE32_TO_CPU(part->pb_Next))
1N/A {
1N/A PedSector start, end;
1N/A PedSector cylblocks;
1N/A
1N/A /* Let's look for loops in the partition table */
1N/A if (_amiga_loop_check(partblock, partlist, i)) {
1N/A free (rdb);
1N/A return NULL;
1N/A }
1N/A /* Let's read a partition block to get its geometry*/
1N/A if (!ped_device_read (geom->dev, part, (PedSector)partblock, 1)) {
1N/A switch (ped_exception_throw(PED_EXCEPTION_ERROR,
1N/A PED_EXCEPTION_CANCEL,
1N/A _("%s : Failed to read partition block %llu\n"),
1N/A __func__, (PedSector)partblock))
1N/A {
1N/A case PED_EXCEPTION_CANCEL :
1N/A case PED_EXCEPTION_UNHANDLED :
1N/A default :
1N/A free(rdb);
1N/A return NULL;
1N/A }
1N/A }
1N/A
1N/A /* Current block is not a Partition Block */
1N/A if (part->pb_ID != IDNAME_PARTITION) {
1N/A free (rdb);
1N/A return NULL;
1N/A }
1N/A
1N/A /* Calculate the geometry of the partition */
1N/A cylblocks = ((PedSector) PED_BE32_TO_CPU (part->de_Surfaces)) *
1N/A ((PedSector) PED_BE32_TO_CPU (part->de_BlocksPerTrack));
1N/A start = ((PedSector) PED_BE32_TO_CPU (part->de_LowCyl)) * cylblocks;
1N/A end = ((((PedSector) PED_BE32_TO_CPU (part->de_HighCyl))+1) * (cylblocks))-1;
1N/A
1N/A /* And check if it is the one we are searching for */
1N/A if (start == geom->start && end == geom->end) {
1N/A free (rdb);
1N/A return part;
1N/A }
1N/A }
1N/A
1N/A free (rdb);
1N/A return NULL;
1N/A}