1N/A/* -*- Mode: c; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
1N/A
1N/A libparted - a library for manipulating disk partitions
1N/A disk_amiga.c - libparted module to manipulate amiga RDB partition tables.
1N/A Copyright (C) 2000-2001, 2004, 2007-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
1N/A#include <parted/parted.h>
1N/A#include <parted/debug.h>
1N/A#include <parted/endian.h>
1N/A
1N/A#include "pt-tools.h"
1N/A
1N/A#ifndef MAX
1N/A# define MAX(a,b) ((a) < (b) ? (b) : (a))
1N/A#endif
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#include "misc.h"
1N/A
1N/A/* String manipulation */
1N/Astatic void _amiga_set_bstr (const char *cstr, char *bstr, int maxsize) {
1N/A int size = strlen (cstr);
1N/A int i;
1N/A
1N/A if (size >= maxsize) return;
1N/A bstr[0] = size;
1N/A for (i = 0; i<size; i++) bstr[i+1] = cstr[i];
1N/A}
1N/Astatic const char * _amiga_get_bstr (char * bstr) {
1N/A char * cstr = bstr + 1;
1N/A int size = bstr[0];
1N/A
1N/A cstr[size] = '\0';
1N/A return cstr;
1N/A}
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 uint32_t ID;
1N/A struct AmigaIds *next;
1N/A};
1N/A
1N/Astatic struct 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 return 0;
1N/A newid->ID = id;
1N/A newid->next = ids;
1N/A return newid;
1N/A}
1N/A
1N/Astatic void
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/Astatic int
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/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/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 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/Astatic struct AmigaBlock *
1N/A_amiga_read_block (const PedDevice *dev, struct AmigaBlock *blk,
1N/A PedSector block, struct AmigaIds *ids)
1N/A{
1N/A if (!ped_device_read (dev, blk, block, 1))
1N/A return NULL;
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."),
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 ((PedDevice*)dev, blk, block, 1))
1N/A return NULL;
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/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 RDSK(pos) ((struct RigidDiskBlock *)(pos))
1N/A
1N/A#define AMIGA_RDB_NOT_FOUND ((uint32_t)0xffffffff)
1N/A#define RDB_LOCATION_LIMIT 16
1N/A#define AMIGA_MAX_PARTITIONS 128
1N/A#define MAX_RDB_BLOCK (RDB_LOCATION_LIMIT + 2 * AMIGA_MAX_PARTITIONS + 2)
1N/A
1N/Astatic uint32_t
1N/A_amiga_find_rdb (const 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/Astruct PartitionBlock {
1N/A uint32_t pb_ID; /* Identifier 32 bit word : 'PART' */
1N/A uint32_t pb_SummedLongs; /* Size of the structure for checksums */
1N/A int32_t pb_ChkSum; /* Checksum of the structure */
1N/A uint32_t pb_HostID; /* SCSI Target ID of host, not really used */
1N/A uint32_t pb_Next; /* Block number of the next PartitionBlock */
1N/A uint32_t pb_Flags; /* Part Flags (NOMOUNT and BOOTABLE) */
1N/A uint32_t pb_Reserved1[2];
1N/A uint32_t pb_DevFlags; /* Preferred flags for OpenDevice */
1N/A char pb_DriveName[32]; /* Preferred DOS device name: BSTR form */
1N/A uint32_t pb_Reserved2[15];
1N/A uint32_t de_TableSize; /* Size of Environment vector */
1N/A /* Size of the blocks in 32 bit words, usually 128 */
1N/A uint32_t de_SizeBlock;
1N/A uint32_t de_SecOrg; /* Not used; must be 0 */
1N/A uint32_t de_Surfaces; /* Number of heads (surfaces) */
1N/A /* Disk sectors per block, used with SizeBlock, usually 1 */
1N/A uint32_t de_SectorPerBlock;
1N/A uint32_t de_BlocksPerTrack; /* Blocks per track. drive specific */
1N/A uint32_t de_Reserved; /* DOS reserved blocks at start of partition. */
1N/A uint32_t de_PreAlloc; /* DOS reserved blocks at end of partition */
1N/A uint32_t de_Interleave; /* Not used, usually 0 */
1N/A uint32_t de_LowCyl; /* First cylinder of the partition */
1N/A uint32_t de_HighCyl; /* Last cylinder of the partition */
1N/A uint32_t de_NumBuffers; /* Initial # DOS of buffers. */
1N/A uint32_t de_BufMemType; /* Type of mem to allocate for buffers */
1N/A uint32_t de_MaxTransfer; /* Max number of bytes to transfer at a time */
1N/A uint32_t de_Mask; /* Address Mask to block out certain memory */
1N/A int32_t de_BootPri; /* Boot priority for autoboot */
1N/A uint32_t de_DosType; /* Dostype of the file system */
1N/A uint32_t de_Baud; /* Baud rate for serial handler */
1N/A uint32_t de_Control; /* Control word for handler/filesystem */
1N/A uint32_t de_BootBlocks; /* Number of blocks containing boot code */
1N/A uint32_t pb_EReserved[12];
1N/A};
1N/A
1N/A#define PART(pos) ((struct PartitionBlock *)(pos))
1N/A
1N/A#define PBFB_BOOTABLE 0 /* this partition is intended to be bootable */
1N/A#define PBFF_BOOTABLE 1L /* (expected directories and files exist) */
1N/A#define PBFB_NOMOUNT 1 /* do not mount this partition (e.g. manually */
1N/A#define PBFF_NOMOUNT 2L /* mounted, but space reserved here) */
1N/A#define PBFB_RAID 2 /* this partition is intended to be part of */
1N/A#define PBFF_RAID 4L /* a RAID array */
1N/A#define PBFB_LVM 3 /* this partition is intended to be part of */
1N/A#define PBFF_LVM 8L /* a LVM volume group */
1N/A
1N/A
1N/Astruct LinkedBlock {
1N/A uint32_t lk_ID; /* Identifier 32 bit word */
1N/A uint32_t lk_SummedLongs; /* Size of the structure for checksums */
1N/A int32_t lk_ChkSum; /* Checksum of the structure */
1N/A uint32_t pb_HostID; /* SCSI Target ID of host, not really used */
1N/A uint32_t lk_Next; /* Block number of the next PartitionBlock */
1N/A};
1N/Astruct Linked2Block {
1N/A uint32_t lk2_ID; /* Identifier 32 bit word */
1N/A uint32_t lk2_SummedLongs; /* Size of the structure for checksums */
1N/A int32_t lk2_ChkSum; /* Checksum of the structure */
1N/A uint32_t lk2_HostID; /* SCSI Target ID of host, not really used */
1N/A uint32_t lk2_Next; /* Block number of the next PartitionBlock */
1N/A uint32_t lk2_Reverved[13];
1N/A uint32_t lk2_Linked; /* Secondary linked list */
1N/A};
1N/A#define LINK_END (uint32_t)0xffffffff
1N/A#define LNK(pos) ((struct LinkedBlock *)(pos))
1N/A#define LNK2(pos) ((struct Linked2Block *)(pos))
1N/A
1N/A
1N/Astatic PedDiskType amiga_disk_type;
1N/A
1N/Astatic int
1N/Aamiga_probe (const PedDevice *dev)
1N/A{
1N/A struct RigidDiskBlock *rdb;
1N/A uint32_t found;
1N/A PED_ASSERT(dev != NULL, return 0);
1N/A
1N/A if ((rdb=RDSK(ped_malloc(dev->sector_size)))==NULL)
1N/A return 0;
1N/A found = _amiga_find_rdb (dev, rdb);
1N/A free (rdb);
1N/A
1N/A return (found == AMIGA_RDB_NOT_FOUND ? 0 : 1);
1N/A}
1N/A
1N/Astatic PedDisk*
1N/Aamiga_alloc (const PedDevice* dev)
1N/A{
1N/A PedDisk *disk;
1N/A struct RigidDiskBlock *rdb;
1N/A PedSector cyl_size;
1N/A int highest_cylinder, highest_block;
1N/A
1N/A PED_ASSERT(dev != NULL, return NULL);
1N/A cyl_size = dev->hw_geom.sectors * dev->hw_geom.heads;
1N/A
1N/A if (!(disk = _ped_disk_alloc (dev, &amiga_disk_type)))
1N/A return NULL;
1N/A
1N/A if (!(disk->disk_specific = ped_malloc (disk->dev->sector_size))) {
1N/A free (disk);
1N/A return NULL;
1N/A }
1N/A rdb = disk->disk_specific;
1N/A
1N/A /* Upon failed assertion this does leak. That's fine, because
1N/A if the assertion fails, you have bigger problems than this leak. */
1N/A PED_ASSERT(sizeof(*rdb) <= disk->dev->sector_size, return NULL);
1N/A
1N/A memset(rdb, 0, disk->dev->sector_size);
1N/A
1N/A rdb->rdb_ID = PED_CPU_TO_BE32 (IDNAME_RIGIDDISK);
1N/A rdb->rdb_SummedLongs = PED_CPU_TO_BE32 (64);
1N/A rdb->rdb_HostID = PED_CPU_TO_BE32 (0);
1N/A rdb->rdb_BlockBytes = PED_CPU_TO_BE32 (disk->dev->sector_size);
1N/A rdb->rdb_Flags = PED_CPU_TO_BE32 (0);
1N/A
1N/A /* Block lists */
1N/A rdb->rdb_BadBlockList = PED_CPU_TO_BE32 (LINK_END);
1N/A rdb->rdb_PartitionList = PED_CPU_TO_BE32 (LINK_END);
1N/A rdb->rdb_FileSysHeaderList = PED_CPU_TO_BE32 (LINK_END);
1N/A rdb->rdb_DriveInit = PED_CPU_TO_BE32 (LINK_END);
1N/A rdb->rdb_BootBlockList = PED_CPU_TO_BE32 (LINK_END);
1N/A
1N/A /* Physical drive characteristics */
1N/A rdb->rdb_Cylinders = PED_CPU_TO_BE32 (dev->hw_geom.cylinders);
1N/A rdb->rdb_Sectors = PED_CPU_TO_BE32 (dev->hw_geom.sectors);
1N/A rdb->rdb_Heads = PED_CPU_TO_BE32 (dev->hw_geom.heads);
1N/A rdb->rdb_Interleave = PED_CPU_TO_BE32 (0);
1N/A rdb->rdb_Park = PED_CPU_TO_BE32 (dev->hw_geom.cylinders);
1N/A rdb->rdb_WritePreComp = PED_CPU_TO_BE32 (dev->hw_geom.cylinders);
1N/A rdb->rdb_ReducedWrite = PED_CPU_TO_BE32 (dev->hw_geom.cylinders);
1N/A rdb->rdb_StepRate = PED_CPU_TO_BE32 (0);
1N/A
1N/A highest_cylinder = 1 + MAX_RDB_BLOCK / cyl_size;
1N/A highest_block = highest_cylinder * cyl_size - 1;
1N/A
1N/A /* Logical driver characteristics */
1N/A rdb->rdb_RDBBlocksLo = PED_CPU_TO_BE32 (0);
1N/A rdb->rdb_RDBBlocksHi = PED_CPU_TO_BE32 (highest_block);
1N/A rdb->rdb_LoCylinder = PED_CPU_TO_BE32 (highest_cylinder);
1N/A rdb->rdb_HiCylinder = PED_CPU_TO_BE32 (dev->hw_geom.cylinders -1);
1N/A rdb->rdb_CylBlocks = PED_CPU_TO_BE32 (cyl_size);
1N/A rdb->rdb_AutoParkSeconds = PED_CPU_TO_BE32 (0);
1N/A /* rdb_HighRDSKBlock will only be set when writing */
1N/A rdb->rdb_HighRDSKBlock = PED_CPU_TO_BE32 (0);
1N/A
1N/A /* Driver identification */
1N/A _amiga_set_bstr("", rdb->rdb_DiskVendor, 8);
1N/A _amiga_set_bstr(dev->model, rdb->rdb_DiskProduct, 16);
1N/A _amiga_set_bstr("", rdb->rdb_DiskRevision, 4);
1N/A _amiga_set_bstr("", rdb->rdb_ControllerVendor, 8);
1N/A _amiga_set_bstr("", rdb->rdb_ControllerProduct, 16);
1N/A _amiga_set_bstr("", rdb->rdb_ControllerRevision, 4);
1N/A
1N/A /* And calculate the checksum */
1N/A _amiga_calculate_checksum ((struct AmigaBlock *) rdb);
1N/A
1N/A return disk;
1N/A}
1N/A
1N/Astatic PedDisk*
1N/Aamiga_duplicate (const PedDisk* disk)
1N/A{
1N/A PedDisk* new_disk;
1N/A struct RigidDiskBlock * new_rdb;
1N/A struct RigidDiskBlock * old_rdb;
1N/A PED_ASSERT(disk != NULL, return NULL);
1N/A PED_ASSERT(disk->dev != NULL, return NULL);
1N/A PED_ASSERT(disk->disk_specific != NULL, return NULL);
1N/A
1N/A old_rdb = (struct RigidDiskBlock *) disk->disk_specific;
1N/A
1N/A if (!(new_disk = ped_disk_new_fresh (disk->dev, &amiga_disk_type)))
1N/A return NULL;
1N/A
1N/A new_rdb = (struct RigidDiskBlock *) new_disk->disk_specific;
1N/A memcpy (new_rdb, old_rdb, 256);
1N/A return new_disk;
1N/A}
1N/A
1N/Astatic void
1N/Aamiga_free (PedDisk* disk)
1N/A{
1N/A PED_ASSERT(disk != NULL, return);
1N/A PED_ASSERT(disk->disk_specific != NULL, return);
1N/A
1N/A free (disk->disk_specific);
1N/A _ped_disk_free (disk);
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/Astatic int
1N/Aamiga_read (PedDisk* disk)
1N/A{
1N/A struct RigidDiskBlock *rdb;
1N/A struct PartitionBlock *partition;
1N/A uint32_t partblock;
1N/A uint32_t partlist[AMIGA_MAX_PARTITIONS];
1N/A PedSector cylblocks;
1N/A int i;
1N/A
1N/A PED_ASSERT(disk != NULL, return 0);
1N/A PED_ASSERT(disk->dev != NULL, return 0);
1N/A PED_ASSERT(disk->dev->sector_size % PED_SECTOR_SIZE_DEFAULT == 0,
1N/A return 0);
1N/A PED_ASSERT(disk->disk_specific != NULL, return 0);
1N/A rdb = RDSK(disk->disk_specific);
1N/A
1N/A if (_amiga_find_rdb (disk->dev, rdb) == AMIGA_RDB_NOT_FOUND) {
1N/A ped_exception_throw(PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
1N/A _("%s : Didn't find rdb block, should never happen."), __func__);
1N/A return 0;
1N/A }
1N/A
1N/A /* Let's copy the rdb read geometry to the dev */
1N/A /* FIXME: should this go into disk->dev->bios_geom instead? */
1N/A disk->dev->hw_geom.cylinders = PED_BE32_TO_CPU (rdb->rdb_Cylinders);
1N/A disk->dev->hw_geom.heads = PED_BE32_TO_CPU (rdb->rdb_Heads);
1N/A disk->dev->hw_geom.sectors = PED_BE32_TO_CPU (rdb->rdb_Sectors);
1N/A cylblocks = (PedSector) PED_BE32_TO_CPU (rdb->rdb_Heads) *
1N/A (PedSector) PED_BE32_TO_CPU (rdb->rdb_Sectors);
1N/A
1N/A /* Remove all partitions in the former in memory table */
1N/A ped_disk_delete_all (disk);
1N/A
1N/A /* Let's allocate a partition block */
1N/A if (!(partition = ped_malloc (disk->dev->sector_size)))
1N/A return 0;
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] = LINK_END;
1N/A
1N/A for (i = 1, partblock = PED_BE32_TO_CPU(rdb->rdb_PartitionList);
1N/A i < AMIGA_MAX_PARTITIONS && partblock != LINK_END;
1N/A i++, partblock = PED_BE32_TO_CPU(partition->pb_Next))
1N/A {
1N/A PedPartition *part;
1N/A PedSector start, end;
1N/A PedConstraint *constraint_exact;
1N/A
1N/A /* Let's look for loops in the partition table */
1N/A if (_amiga_loop_check(partblock, partlist, i)) {
1N/A break;
1N/A }
1N/A
1N/A /* Let's allocate and read a partition block to get its geometry*/
1N/A if (!_amiga_read_block (disk->dev, AMIGA(partition),
1N/A (PedSector)partblock, NULL)) {
1N/A free(partition);
1N/A return 0;
1N/A }
1N/A
1N/A start = ((PedSector) PED_BE32_TO_CPU (partition->de_LowCyl))
1N/A * cylblocks;
1N/A end = (((PedSector) PED_BE32_TO_CPU (partition->de_HighCyl))
1N/A + 1) * cylblocks - 1;
1N/A
1N/A /* We can now construct a new partition */
1N/A if (!(part = ped_partition_new (disk, PED_PARTITION_NORMAL,
1N/A NULL, start, end))) {
1N/A free(partition);
1N/A return 0;
1N/A }
1N/A /* And copy over the partition block */
1N/A memcpy(part->disk_specific, partition, 256);
1N/A
1N/A part->num = i;
1N/A part->type = 0;
1N/A /* Let's probe what file system is present on the disk */
1N/A part->fs_type = ped_file_system_probe (&part->geom);
1N/A
1N/A constraint_exact = ped_constraint_exact (&part->geom);
1N/A if (!ped_disk_add_partition (disk, part, constraint_exact)) {
1N/A ped_partition_destroy(part);
1N/A free(partition);
1N/A return 0;
1N/A }
1N/A ped_constraint_destroy (constraint_exact);
1N/A }
1N/A free(partition);
1N/A return 1;
1N/A}
1N/A
1N/Astatic int
1N/A_amiga_find_free_blocks(const PedDisk *disk, uint32_t *table,
1N/A struct LinkedBlock *block, uint32_t first, uint32_t type)
1N/A{
1N/A PedSector next;
1N/A
1N/A PED_ASSERT(disk != NULL, return 0);
1N/A PED_ASSERT(disk->dev != NULL, return 0);
1N/A
1N/A for (next = first; next != LINK_END; next = PED_BE32_TO_CPU(block->lk_Next)) {
1N/A if (table[next] != IDNAME_FREE) {
1N/A switch (ped_exception_throw(PED_EXCEPTION_ERROR,
1N/A PED_EXCEPTION_FIX | PED_EXCEPTION_IGNORE | PED_EXCEPTION_CANCEL,
1N/A _("%s : Loop detected at block %d."), __func__, next))
1N/A {
1N/A case PED_EXCEPTION_CANCEL :
1N/A return 0;
1N/A case PED_EXCEPTION_FIX :
1N/A /* TODO : Need to add fixing code */
1N/A case PED_EXCEPTION_IGNORE :
1N/A case PED_EXCEPTION_UNHANDLED :
1N/A default :
1N/A return 1;
1N/A }
1N/A }
1N/A
1N/A if (!_amiga_read_block (disk->dev, AMIGA(block), next, NULL)) {
1N/A return 0;
1N/A }
1N/A if (PED_BE32_TO_CPU(block->lk_ID) != type) {
1N/A switch (ped_exception_throw(PED_EXCEPTION_ERROR,
1N/A PED_EXCEPTION_CANCEL,
1N/A _("%s : The %s list seems bad at block %s."),
1N/A __func__, _amiga_block_id(PED_BE32_TO_CPU(block->lk_ID)), next))
1N/A {
1N/A /* TODO : to more subtile things here */
1N/A case PED_EXCEPTION_CANCEL :
1N/A case PED_EXCEPTION_UNHANDLED :
1N/A default :
1N/A return 0;
1N/A }
1N/A }
1N/A table[next] = type;
1N/A if (PED_BE32_TO_CPU(block->lk_ID) == IDNAME_FILESYSHEADER) {
1N/A if (_amiga_find_free_blocks(disk, table, block,
1N/A PED_BE32_TO_CPU(LNK2(block)->lk2_Linked),
1N/A IDNAME_LOADSEG) == 0) return 0;
1N/A }
1N/A }
1N/A return 1;
1N/A}
1N/Astatic uint32_t
1N/A_amiga_next_free_block(uint32_t *table, uint32_t start, uint32_t type) {
1N/A int i;
1N/A
1N/A for (i = start; table[i] != type && table[i] != IDNAME_FREE; i++);
1N/A return i;
1N/A}
1N/Astatic PedPartition *
1N/A_amiga_next_real_partition(const PedDisk *disk, PedPartition *part) {
1N/A PedPartition *next;
1N/A
1N/A for (next = ped_disk_next_partition (disk, part);
1N/A next != NULL && !ped_partition_is_active (next);
1N/A next = ped_disk_next_partition (disk, next));
1N/A return next;
1N/A}
1N/A#ifndef DISCOVER_ONLY
1N/Astatic int
1N/Aamiga_write (const PedDisk* disk)
1N/A{
1N/A struct RigidDiskBlock *rdb;
1N/A struct LinkedBlock *block;
1N/A struct PartitionBlock *partition;
1N/A PedPartition *part, *next_part;
1N/A PedSector cylblocks, first_hb, last_hb;
1N/A uint32_t * table;
1N/A uint32_t i;
1N/A uint32_t rdb_num, part_num, block_num, next_num;
1N/A
1N/A PED_ASSERT (disk != NULL, return 0);
1N/A PED_ASSERT (disk->dev != NULL, return 0);
1N/A PED_ASSERT (disk->disk_specific != NULL, return 0);
1N/A
1N/A if (!(rdb = ped_malloc (disk->dev->sector_size)))
1N/A return 0;
1N/A
1N/A /* Let's read the rdb */
1N/A if ((rdb_num = _amiga_find_rdb (disk->dev, rdb)) == AMIGA_RDB_NOT_FOUND) {
1N/A rdb_num = 2;
1N/A size_t pb_size = sizeof (struct PartitionBlock);
1N/A /* Initialize only the part that won't be copied over
1N/A with a partition block in amiga_read. */
1N/A memset ((char *)(RDSK(disk->disk_specific)) + pb_size,
1N/A 0, PED_SECTOR_SIZE_DEFAULT - pb_size);
1N/A } else {
1N/A memcpy (RDSK(disk->disk_specific), rdb, disk->dev->sector_size);
1N/A }
1N/A free (rdb);
1N/A rdb = RDSK(disk->disk_specific);
1N/A
1N/A cylblocks = (PedSector) PED_BE32_TO_CPU (rdb->rdb_Heads) *
1N/A (PedSector) PED_BE32_TO_CPU (rdb->rdb_Sectors);
1N/A first_hb = (PedSector) PED_BE32_TO_CPU (rdb->rdb_RDBBlocksLo);
1N/A last_hb = (PedSector) PED_BE32_TO_CPU (rdb->rdb_RDBBlocksHi);
1N/A
1N/A /* Allocate a free block table and initialize it.
1N/A There must be room for at least RDB_NUM + 2 entries, since
1N/A the first RDB_NUM+1 entries get IDNAME_RIGIDDISK, and the
1N/A following one must have LINK_END to serve as sentinel. */
1N/A size_t tab_size = 2 + MAX (last_hb - first_hb, rdb_num);
1N/A if (!(table = ped_malloc (tab_size * sizeof *table)))
1N/A return 0;
1N/A
1N/A for (i = 0; i <= rdb_num; i++)
1N/A table[i] = IDNAME_RIGIDDISK;
1N/A for ( ; i < tab_size; i++)
1N/A table[i] = LINK_END;
1N/A
1N/A /* Let's allocate a partition block */
1N/A if (!(block = ped_malloc (disk->dev->sector_size))) {
1N/A free (table);
1N/A return 0;
1N/A }
1N/A
1N/A /* And fill the free block table */
1N/A if (_amiga_find_free_blocks(disk, table, block,
1N/A PED_BE32_TO_CPU (rdb->rdb_BadBlockList), IDNAME_BADBLOCK) == 0)
1N/A {
1N/A ped_exception_throw(PED_EXCEPTION_ERROR,
1N/A PED_EXCEPTION_CANCEL,
1N/A _("%s : Failed to list bad blocks."), __func__);
1N/A goto error_free_table;
1N/A }
1N/A if (_amiga_find_free_blocks(disk, table, block,
1N/A PED_BE32_TO_CPU (rdb->rdb_PartitionList), IDNAME_PARTITION) == 0)
1N/A {
1N/A ped_exception_throw(PED_EXCEPTION_ERROR,
1N/A PED_EXCEPTION_CANCEL,
1N/A _("%s : Failed to list partition blocks."), __func__);
1N/A goto error_free_table;
1N/A }
1N/A if (_amiga_find_free_blocks(disk, table, block,
1N/A PED_BE32_TO_CPU (rdb->rdb_FileSysHeaderList), IDNAME_FILESYSHEADER) == 0)
1N/A {
1N/A ped_exception_throw(PED_EXCEPTION_ERROR,
1N/A PED_EXCEPTION_CANCEL,
1N/A _("%s : Failed to list file system blocks."), __func__);
1N/A goto error_free_table;
1N/A }
1N/A if (_amiga_find_free_blocks(disk, table, block,
1N/A PED_BE32_TO_CPU (rdb->rdb_BootBlockList), IDNAME_BOOT) == 0)
1N/A {
1N/A ped_exception_throw(PED_EXCEPTION_ERROR,
1N/A PED_EXCEPTION_CANCEL,
1N/A _("%s : Failed to list boot blocks."), __func__);
1N/A goto error_free_table;
1N/A }
1N/A
1N/A block_num = part_num = _amiga_next_free_block(table, rdb_num+1,
1N/A IDNAME_PARTITION);
1N/A part = _amiga_next_real_partition(disk, NULL);
1N/A rdb->rdb_PartitionList = PED_CPU_TO_BE32(part ? part_num : LINK_END);
1N/A for (; part != NULL; part = next_part, block_num = next_num) {
1N/A PED_ASSERT(part->disk_specific != NULL, return 0);
1N/A PED_ASSERT(part->geom.start % cylblocks == 0, return 0);
1N/A PED_ASSERT((part->geom.end + 1) % cylblocks == 0, return 0);
1N/A
1N/A next_part = _amiga_next_real_partition(disk, part);
1N/A next_num = _amiga_next_free_block(table, block_num+1, IDNAME_PARTITION);
1N/A
1N/A partition = PART(part->disk_specific);
1N/A if (next_part == NULL)
1N/A partition->pb_Next = PED_CPU_TO_BE32(LINK_END);
1N/A else
1N/A partition->pb_Next = PED_CPU_TO_BE32(next_num);
1N/A partition->de_LowCyl = PED_CPU_TO_BE32(part->geom.start/cylblocks);
1N/A partition->de_HighCyl = PED_CPU_TO_BE32((part->geom.end+1)/cylblocks-1);
1N/A _amiga_calculate_checksum(AMIGA(partition));
1N/A if (!ped_device_write (disk->dev, (void*) partition, block_num, 1)) {
1N/A ped_exception_throw(PED_EXCEPTION_ERROR,
1N/A PED_EXCEPTION_CANCEL,
1N/A _("Failed to write partition block at %d."),
1N/A block_num);
1N/A goto error_free_table;
1N/A /* WARNING : If we fail here, we stop everything,
1N/A * and the partition table is lost. A better
1N/A * solution should be found, using the second
1N/A * half of the hardblocks to not overwrite the
1N/A * old partition table. It becomes problematic
1N/A * if we use more than half of the hardblocks. */
1N/A }
1N/A }
1N/A
1N/A if (block_num > PED_BE32_TO_CPU (rdb->rdb_HighRDSKBlock))
1N/A rdb->rdb_HighRDSKBlock = PED_CPU_TO_BE32(block_num);
1N/A
1N/A _amiga_calculate_checksum(AMIGA(rdb));
1N/A if (!ped_device_write (disk->dev, (void*) disk->disk_specific, rdb_num, 1))
1N/A goto error_free_table;
1N/A
1N/A free (table);
1N/A free (block);
1N/A return ped_device_sync (disk->dev);
1N/A
1N/Aerror_free_table:
1N/A free (table);
1N/A free (block);
1N/A return 0;
1N/A}
1N/A#endif /* !DISCOVER_ONLY */
1N/A
1N/Astatic PedPartition*
1N/Aamiga_partition_new (const PedDisk* disk, PedPartitionType part_type,
1N/A const PedFileSystemType* fs_type,
1N/A PedSector start, PedSector end)
1N/A{
1N/A PedPartition *part;
1N/A PedDevice *dev;
1N/A PedSector cyl;
1N/A struct PartitionBlock *partition;
1N/A struct RigidDiskBlock *rdb;
1N/A
1N/A PED_ASSERT(disk != NULL, return NULL);
1N/A PED_ASSERT(disk->dev != NULL, return NULL);
1N/A PED_ASSERT(disk->disk_specific != NULL, return NULL);
1N/A dev = disk->dev;
1N/A cyl = (PedSector) (dev->hw_geom.sectors * dev->hw_geom.heads);
1N/A rdb = RDSK(disk->disk_specific);
1N/A
1N/A if (!(part = _ped_partition_alloc (disk, part_type, fs_type, start, end)))
1N/A return NULL;
1N/A
1N/A if (ped_partition_is_active (part)) {
1N/A if (!(part->disk_specific = ped_malloc (disk->dev->sector_size))) {
1N/A free (part);
1N/A return NULL;
1N/A }
1N/A partition = PART(part->disk_specific);
1N/A memset(partition, 0, sizeof(struct PartitionBlock));
1N/A
1N/A partition->pb_ID = PED_CPU_TO_BE32(IDNAME_PARTITION);
1N/A partition->pb_SummedLongs = PED_CPU_TO_BE32(64);
1N/A partition->pb_HostID = rdb->rdb_HostID;
1N/A partition->pb_Flags = PED_CPU_TO_BE32(0);
1N/A /* TODO : use a scheme including the device name and the
1N/A * partition number, if it is possible */
1N/A _amiga_set_bstr("dhx", partition->pb_DriveName, 32);
1N/A
1N/A partition->de_TableSize = PED_CPU_TO_BE32(19);
1N/A partition->de_SizeBlock = PED_CPU_TO_BE32(128);
1N/A partition->de_SecOrg = PED_CPU_TO_BE32(0);
1N/A partition->de_Surfaces = PED_CPU_TO_BE32(dev->hw_geom.heads);
1N/A partition->de_SectorPerBlock = PED_CPU_TO_BE32(1);
1N/A partition->de_BlocksPerTrack
1N/A = PED_CPU_TO_BE32(dev->hw_geom.sectors);
1N/A partition->de_Reserved = PED_CPU_TO_BE32(2);
1N/A partition->de_PreAlloc = PED_CPU_TO_BE32(0);
1N/A partition->de_Interleave = PED_CPU_TO_BE32(0);
1N/A partition->de_LowCyl = PED_CPU_TO_BE32(start/cyl);
1N/A partition->de_HighCyl = PED_CPU_TO_BE32((end+1)/cyl-1);
1N/A partition->de_NumBuffers = PED_CPU_TO_BE32(30);
1N/A partition->de_BufMemType = PED_CPU_TO_BE32(0);
1N/A partition->de_MaxTransfer = PED_CPU_TO_BE32(0x7fffffff);
1N/A partition->de_Mask = PED_CPU_TO_BE32(0xffffffff);
1N/A partition->de_BootPri = PED_CPU_TO_BE32(0);
1N/A partition->de_DosType = PED_CPU_TO_BE32(0x4c4e5800);
1N/A partition->de_Baud = PED_CPU_TO_BE32(0);
1N/A partition->de_Control = PED_CPU_TO_BE32(0);
1N/A partition->de_BootBlocks = PED_CPU_TO_BE32(0);
1N/A
1N/A } else {
1N/A part->disk_specific = NULL;
1N/A }
1N/A return part;
1N/A}
1N/A
1N/Astatic PedPartition*
1N/Aamiga_partition_duplicate (const PedPartition* part)
1N/A{
1N/A PedPartition *new_part;
1N/A struct PartitionBlock *new_amiga_part;
1N/A struct PartitionBlock *old_amiga_part;
1N/A
1N/A PED_ASSERT(part != NULL, return NULL);
1N/A PED_ASSERT(part->disk != NULL, return NULL);
1N/A PED_ASSERT(part->disk_specific != NULL, return NULL);
1N/A old_amiga_part = (struct PartitionBlock *) part->disk_specific;
1N/A
1N/A new_part = ped_partition_new (part->disk, part->type,
1N/A part->fs_type, part->geom.start,
1N/A part->geom.end);
1N/A if (!new_part)
1N/A return NULL;
1N/A
1N/A new_amiga_part = (struct PartitionBlock *) new_part->disk_specific;
1N/A memcpy (new_amiga_part, old_amiga_part, 256);
1N/A
1N/A return new_part;
1N/A}
1N/A
1N/Astatic void
1N/Aamiga_partition_destroy (PedPartition* part)
1N/A{
1N/A PED_ASSERT (part != NULL, return);
1N/A
1N/A if (ped_partition_is_active (part)) {
1N/A PED_ASSERT (part->disk_specific != NULL, return);
1N/A free (part->disk_specific);
1N/A }
1N/A _ped_partition_free (part);
1N/A}
1N/A
1N/Astatic int
1N/Aamiga_partition_set_system (PedPartition* part,
1N/A const PedFileSystemType* fs_type)
1N/A{
1N/A struct PartitionBlock *partition;
1N/A
1N/A PED_ASSERT (part != NULL, return 0);
1N/A PED_ASSERT (part->disk_specific != NULL, return 0);
1N/A partition = PART(part->disk_specific);
1N/A
1N/A part->fs_type = fs_type;
1N/A
1N/A if (!fs_type)
1N/A partition->de_DosType = PED_CPU_TO_BE32(0x4c4e5800); /* 'LNX\0' */
1N/A else if (!strcmp (fs_type->name, "ext2"))
1N/A partition->de_DosType = PED_CPU_TO_BE32(0x4c4e5800); /* 'LNX\0' */
1N/A else if (!strcmp (fs_type->name, "ext3"))
1N/A partition->de_DosType = PED_CPU_TO_BE32(0x45585403); /* 'EXT\3' */
1N/A else if (is_linux_swap (fs_type->name))
1N/A partition->de_DosType = PED_CPU_TO_BE32(0x53575000); /* 'SWP\0' */
1N/A else if (!strcmp (fs_type->name, "fat16"))
1N/A partition->de_DosType = PED_CPU_TO_BE32(0x46415400); /* 'FAT\0' */
1N/A else if (!strcmp (fs_type->name, "fat32"))
1N/A partition->de_DosType = PED_CPU_TO_BE32(0x46415401); /* 'FAT\1'*/
1N/A else if (!strcmp (fs_type->name, "hfs"))
1N/A partition->de_DosType = PED_CPU_TO_BE32(0x48465300); /* 'HFS\0' */
1N/A else if (!strcmp (fs_type->name, "jfs"))
1N/A partition->de_DosType = PED_CPU_TO_BE32(0x4a465300); /* 'JFS\0' */
1N/A else if (!strcmp (fs_type->name, "ntfs"))
1N/A partition->de_DosType = PED_CPU_TO_BE32(0x4e544653); /* 'NTFS' */
1N/A else if (!strcmp (fs_type->name, "reiserfs"))
1N/A partition->de_DosType = PED_CPU_TO_BE32(0x52465300); /* 'RFS\0' */
1N/A else if (!strcmp (fs_type->name, "sun-ufs"))
1N/A partition->de_DosType = PED_CPU_TO_BE32(0x53554653); /* 'SUFS' */
1N/A else if (!strcmp (fs_type->name, "hp-ufs"))
1N/A partition->de_DosType = PED_CPU_TO_BE32(0x48554653); /* 'HUFS' */
1N/A else if (!strcmp (fs_type->name, "xfs"))
1N/A partition->de_DosType = PED_CPU_TO_BE32(0x58465300); /* 'XFS\0' */
1N/A else
1N/A partition->de_DosType = PED_CPU_TO_BE32(0x00000000); /* unknown */
1N/A return 1;
1N/A}
1N/A
1N/Astatic int
1N/Aamiga_partition_set_flag (PedPartition* part, PedPartitionFlag flag, int state)
1N/A{
1N/A struct PartitionBlock *partition;
1N/A
1N/A PED_ASSERT (part != NULL, return 0);
1N/A PED_ASSERT (part->disk_specific != NULL, return 0);
1N/A
1N/A partition = PART(part->disk_specific);
1N/A
1N/A switch (flag) {
1N/A case PED_PARTITION_BOOT:
1N/A if (state) partition->pb_Flags |= PED_CPU_TO_BE32(PBFF_BOOTABLE);
1N/A else partition->pb_Flags &= ~(PED_CPU_TO_BE32(PBFF_BOOTABLE));
1N/A return 1;
1N/A case PED_PARTITION_HIDDEN:
1N/A if (state) partition->pb_Flags |= PED_CPU_TO_BE32(PBFF_NOMOUNT);
1N/A else partition->pb_Flags &= ~(PED_CPU_TO_BE32(PBFF_NOMOUNT));
1N/A return 1;
1N/A case PED_PARTITION_RAID:
1N/A if (state) partition->pb_Flags |= PED_CPU_TO_BE32(PBFF_RAID);
1N/A else partition->pb_Flags &= ~(PED_CPU_TO_BE32(PBFF_RAID));
1N/A return 1;
1N/A case PED_PARTITION_LVM:
1N/A if (state) partition->pb_Flags |= PED_CPU_TO_BE32(PBFF_LVM);
1N/A else partition->pb_Flags &= ~(PED_CPU_TO_BE32(PBFF_LVM));
1N/A return 1;
1N/A default:
1N/A return 0;
1N/A }
1N/A}
1N/A
1N/Astatic int
1N/Aamiga_partition_get_flag (const PedPartition* part, PedPartitionFlag flag)
1N/A{
1N/A struct PartitionBlock *partition;
1N/A
1N/A PED_ASSERT (part != NULL, return 0);
1N/A PED_ASSERT (part->disk_specific != NULL, return 0);
1N/A
1N/A partition = PART(part->disk_specific);
1N/A
1N/A switch (flag) {
1N/A case PED_PARTITION_BOOT:
1N/A return (partition->pb_Flags & PED_CPU_TO_BE32(PBFF_BOOTABLE));
1N/A case PED_PARTITION_HIDDEN:
1N/A return (partition->pb_Flags & PED_CPU_TO_BE32(PBFF_NOMOUNT));
1N/A case PED_PARTITION_RAID:
1N/A return (partition->pb_Flags & PED_CPU_TO_BE32(PBFF_RAID));
1N/A case PED_PARTITION_LVM:
1N/A return (partition->pb_Flags & PED_CPU_TO_BE32(PBFF_LVM));
1N/A default:
1N/A return 0;
1N/A }
1N/A}
1N/A
1N/Astatic int
1N/Aamiga_partition_is_flag_available (const PedPartition* part,
1N/A PedPartitionFlag flag)
1N/A{
1N/A switch (flag) {
1N/A case PED_PARTITION_BOOT:
1N/A case PED_PARTITION_HIDDEN:
1N/A case PED_PARTITION_RAID:
1N/A case PED_PARTITION_LVM:
1N/A return 1;
1N/A default:
1N/A return 0;
1N/A }
1N/A}
1N/A
1N/Astatic void
1N/Aamiga_partition_set_name (PedPartition* part, const char* name)
1N/A{
1N/A struct PartitionBlock *partition;
1N/A
1N/A PED_ASSERT (part != NULL, return);
1N/A PED_ASSERT (part->disk_specific != NULL, return);
1N/A
1N/A partition = PART(part->disk_specific);
1N/A _amiga_set_bstr(name, partition->pb_DriveName, 32);
1N/A}
1N/Astatic const char*
1N/Aamiga_partition_get_name (const PedPartition* part)
1N/A{
1N/A struct PartitionBlock *partition;
1N/A
1N/A PED_ASSERT (part != NULL, return 0);
1N/A PED_ASSERT (part->disk_specific != NULL, return 0);
1N/A
1N/A partition = PART(part->disk_specific);
1N/A
1N/A return _amiga_get_bstr(partition->pb_DriveName);
1N/A}
1N/A
1N/Astatic PedAlignment*
1N/Aamiga_get_partition_alignment(const PedDisk *disk)
1N/A{
1N/A PedSector cylinder_size =
1N/A disk->dev->hw_geom.sectors * disk->dev->hw_geom.heads;
1N/A
1N/A return ped_alignment_new(0, cylinder_size);
1N/A}
1N/A
1N/Astatic PedConstraint*
1N/A_amiga_get_constraint (const PedDisk *disk)
1N/A{
1N/A PedDevice *dev = disk->dev;
1N/A PedAlignment start_align, end_align;
1N/A PedGeometry max_geom;
1N/A PedSector cyl_size = dev->hw_geom.sectors * dev->hw_geom.heads;
1N/A
1N/A if (!ped_alignment_init(&start_align, 0, cyl_size))
1N/A return NULL;
1N/A if (!ped_alignment_init(&end_align, -1, cyl_size))
1N/A return NULL;
1N/A if (!ped_geometry_init(&max_geom, dev, MAX_RDB_BLOCK + 1,
1N/A dev->length - MAX_RDB_BLOCK - 1))
1N/A return NULL;
1N/A
1N/A return ped_constraint_new (&start_align, &end_align,
1N/A &max_geom, &max_geom, 1, dev->length);
1N/A}
1N/A
1N/Astatic int
1N/Aamiga_partition_align (PedPartition* part, const PedConstraint* constraint)
1N/A{
1N/A PED_ASSERT (part != NULL, return 0);
1N/A PED_ASSERT (part->disk != NULL, return 0);
1N/A
1N/A if (_ped_partition_attempt_align (part, constraint,
1N/A _amiga_get_constraint (part->disk)))
1N/A return 1;
1N/A
1N/A#ifndef DISCOVER_ONLY
1N/A ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
1N/A _("Unable to satisfy all constraints on the partition."));
1N/A#endif
1N/A return 0;
1N/A}
1N/A
1N/Astatic int
1N/Aamiga_partition_enumerate (PedPartition* part)
1N/A{
1N/A int i;
1N/A PedPartition* p;
1N/A
1N/A PED_ASSERT (part != NULL, return 0);
1N/A PED_ASSERT (part->disk != NULL, return 0);
1N/A
1N/A /* never change the partition numbers */
1N/A if (part->num != -1)
1N/A return 1;
1N/A for (i = 1; i <= AMIGA_MAX_PARTITIONS; i++) {
1N/A p = ped_disk_get_partition (part->disk, i);
1N/A if (!p) {
1N/A part->num = i;
1N/A return 1;
1N/A }
1N/A }
1N/A
1N/A /* failed to allocate a number */
1N/A#ifndef DISCOVER_ONLY
1N/A ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
1N/A _("Unable to allocate a partition number."));
1N/A#endif
1N/A return 0;
1N/A}
1N/A
1N/Astatic int
1N/Aamiga_alloc_metadata (PedDisk* disk)
1N/A{
1N/A PedPartition* new_part;
1N/A PedConstraint* constraint_any = NULL;
1N/A
1N/A PED_ASSERT (disk != NULL, goto error);
1N/A PED_ASSERT (disk->dev != NULL, goto error);
1N/A
1N/A constraint_any = ped_constraint_any (disk->dev);
1N/A
1N/A /* Allocate space for the RDB */
1N/A new_part = ped_partition_new (disk, PED_PARTITION_METADATA, NULL,
1N/A 0, MAX_RDB_BLOCK);
1N/A if (!new_part)
1N/A goto error;
1N/A
1N/A if (!ped_disk_add_partition (disk, new_part, constraint_any)) {
1N/A ped_partition_destroy (new_part);
1N/A goto error;
1N/A }
1N/A
1N/A ped_constraint_destroy (constraint_any);
1N/A return 1;
1N/Aerror:
1N/A ped_constraint_destroy (constraint_any);
1N/A return 0;
1N/A}
1N/A
1N/Astatic int
1N/Aamiga_get_max_primary_partition_count (const PedDisk* disk)
1N/A{
1N/A return AMIGA_MAX_PARTITIONS;
1N/A}
1N/A
1N/Astatic bool
1N/Aamiga_get_max_supported_partition_count (const PedDisk* disk, int *max_n)
1N/A{
1N/A *max_n = AMIGA_MAX_PARTITIONS;
1N/A return true;
1N/A}
1N/A
1N/A#include "pt-common.h"
1N/APT_define_limit_functions (amiga)
1N/A
1N/Astatic PedDiskOps amiga_disk_ops = {
1N/A .clobber = NULL,
1N/A .write = NULL_IF_DISCOVER_ONLY (amiga_write),
1N/A
1N/A .partition_set_name = amiga_partition_set_name,
1N/A .partition_get_name = amiga_partition_get_name,
1N/A
1N/A .get_partition_alignment = amiga_get_partition_alignment,
1N/A
1N/A PT_op_function_initializers (amiga)
1N/A};
1N/A
1N/Astatic PedDiskType amiga_disk_type = {
1N/A .next = NULL,
1N/A .name = "amiga",
1N/A .ops = &amiga_disk_ops,
1N/A .features = PED_DISK_TYPE_PARTITION_NAME
1N/A};
1N/A
1N/Avoid
1N/Aped_disk_amiga_init ()
1N/A{
1N/A PED_ASSERT (sizeof (struct AmigaBlock) != 3, return);
1N/A PED_ASSERT (sizeof (struct RigidDiskBlock) != 64, return);
1N/A PED_ASSERT (sizeof (struct PartitionBlock) != 64, return);
1N/A PED_ASSERT (sizeof (struct LinkedBlock) != 5, return);
1N/A PED_ASSERT (sizeof (struct Linked2Block) != 18, return);
1N/A
1N/A ped_disk_type_register (&amiga_disk_type);
1N/A}
1N/A
1N/Avoid
1N/Aped_disk_amiga_done ()
1N/A{
1N/A ped_disk_type_unregister (&amiga_disk_type);
1N/A}