Done so far: amiga, bsd, loop, gpt.
Fix leaks along the way.
remove unused label
Add a cast to avoid a warning.
ped_exception_fetch_all ();
for (walk = ped_disk_type_get_next (NULL); walk;
walk = ped_disk_type_get_next (walk))
+ {
+ if (getenv ("PARTED_DEBUG")) {
+ fprintf (stderr, "probe label: %s\n",
+ walk->name);
+ fflush (stderr);
+ }
if (walk->ops->probe (dev))
break;
+ }
if (ped_exception)
ped_exception_catch ();
#include <config.h>
+#include <stdbool.h>
#include <parted/parted.h>
#include <parted/debug.h>
#include <parted/endian.h>
dp[63] = sum;
}
+/* FIXME: factor out this function: copied from dos.c
+ Read sector, SECTOR_NUM (which has length DEV->sector_size) into malloc'd
+ storage. If the read fails, free the memory and return zero without
+ modifying *BUF. Otherwise, set *BUF to the new buffer and return 1. */
+static int
+read_sector (const PedDevice *dev, PedSector sector_num, char **buf)
+{
+ char *b = ped_malloc (dev->sector_size);
+ PED_ASSERT (b != NULL, return 0);
+ if (!ped_device_read (dev, b, sector_num, 1)) {
+ free (b);
+ return 0;
+ }
+ *buf = b;
+ return 1;
+}
static int
bsd_probe (const PedDevice *dev)
{
- char boot[512];
- BSDRawLabel *label;
+ BSDRawLabel *partition;
PED_ASSERT (dev != NULL, return 0);
- if (dev->sector_size != 512)
+ if (dev->sector_size < 512)
return 0;
- if (!ped_device_read (dev, boot, 0, 1))
+ char *label;
+ if (!read_sector (dev, 0, &label))
return 0;
- label = (BSDRawLabel *) (boot + BSD_LABEL_OFFSET);
+ partition = (BSDRawLabel *) (label + BSD_LABEL_OFFSET);
- alpha_bootblock_checksum(boot);
+ alpha_bootblock_checksum(label);
/* check magic */
- if (PED_LE32_TO_CPU (label->d_magic) != BSD_DISKMAGIC)
- return 0;
-
- return 1;
+ bool found = PED_LE32_TO_CPU (partition->d_magic) == BSD_DISKMAGIC;
+ free (label);
+ return found;
}
static PedDisk*
static int
bsd_clobber (PedDevice* dev)
{
- char boot [512];
- BSDRawLabel* label = (BSDRawLabel *) (boot + BSD_LABEL_OFFSET);
-
- if (!ped_device_read (dev, boot, 0, 1))
+ char *label;
+ if (!read_sector (dev, 0, &label))
return 0;
- label->d_magic = 0;
- return ped_device_write (dev, (void*) boot, 0, 1);
+ BSDRawLabel *rawlabel = (BSDRawLabel *) (label + BSD_LABEL_OFFSET);
+ rawlabel->d_magic = 0;
+ return ped_device_write (dev, label, 0, 1);
}
#endif /* !DISCOVER_ONLY */
ped_disk_delete_all (disk);
- if (!ped_device_read (disk->dev, bsd_specific->boot_code, 0, 1))
- goto error;
+ char *s0;
+ if (!read_sector (disk->dev, 0, &s0))
+ return 0;
+
+ memcpy (bsd_specific->boot_code, s0, sizeof (bsd_specific->boot_code));
+ free (s0);
+
label = (BSDRawLabel *) (bsd_specific->boot_code + BSD_LABEL_OFFSET);
for (i = 1; i <= BSD_MAXPARTITIONS; i++) {
static PedDiskType gpt_disk_type;
+/* FIXME: factor out this function: copied from dos.c
+ Read sector, SECTOR_NUM (which has length DEV->sector_size) into malloc'd
+ storage. If the read fails, free the memory and return zero without
+ modifying *BUF. Otherwise, set *BUF to the new buffer and return 1. */
+static int
+read_sector (const PedDevice *dev, PedSector sector_num, char **buf)
+{
+ char *b = ped_malloc (dev->sector_size);
+ PED_ASSERT (b != NULL, return 0);
+ if (!ped_device_read (dev, b, sector_num, 1)) {
+ free (b);
+ return 0;
+ }
+ *buf = b;
+ return 1;
+}
+
static inline uint32_t
pth_get_size (const PedDevice* dev)
{
{
GuidPartitionTableHeader_t* gpt = NULL;
uint8_t* pth_raw = ped_malloc (pth_get_size (dev));
- LegacyMBR_t legacy_mbr;
int gpt_sig_found = 0;
PED_ASSERT (dev != NULL, return 0);
if (!gpt_sig_found)
return 0;
- if (ped_device_read(dev, &legacy_mbr, 0, GPT_HEADER_SECTORS)) {
- if (!_pmbr_is_valid (&legacy_mbr)) {
- int ex_status = ped_exception_throw (
- PED_EXCEPTION_WARNING,
- PED_EXCEPTION_YES_NO,
- _("%s contains GPT signatures, indicating that it has "
- "a GPT table. However, it does not have a valid "
- "fake msdos partition table, as it should. Perhaps "
- "it was corrupted -- possibly by a program that "
- "doesn't understand GPT partition tables. Or "
- "perhaps you deleted the GPT table, and are now "
- "using an msdos partition table. Is this a GPT "
- "partition table?"),
- dev->path);
- if (ex_status == PED_EXCEPTION_NO)
- return 0;
- }
+
+ char *label;
+ if (!read_sector (dev, 0, &label))
+ return 0;
+
+ int ok = 1;
+ if (!_pmbr_is_valid ( (const LegacyMBR_t *) label)) {
+ int ex_status = ped_exception_throw (
+ PED_EXCEPTION_WARNING,
+ PED_EXCEPTION_YES_NO,
+ _("%s contains GPT signatures, indicating that it has "
+ "a GPT table. However, it does not have a valid "
+ "fake msdos partition table, as it should. Perhaps "
+ "it was corrupted -- possibly by a program that "
+ "doesn't understand GPT partition tables. Or "
+ "perhaps you deleted the GPT table, and are now "
+ "using an msdos partition table. Is this a GPT "
+ "partition table?"),
+ dev->path);
+ if (ex_status == PED_EXCEPTION_NO)
+ ok = 0;
}
- return 1;
+ free (label);
+ return ok;
}
#ifndef DISCOVER_ONLY
static int
_write_pmbr (PedDevice * dev)
{
- LegacyMBR_t pmbr;
+ size_t buf_len = pth_get_size (dev);
+ LegacyMBR_t *pmbr = ped_malloc (buf_len);
+#if 0
/* The UEFI spec is not clear about what to do with the following
elements of the Protective MBR (pmbr): BootCode (0-440B),
UniqueMBRSignature (440B-444B) and Unknown (444B-446B).
/* Zero out all the legacy partitions.
There are 4 PartitionRecords. */
memset (pmbr.PartitionRecord, 0, sizeof pmbr.PartitionRecord);
+#endif
- pmbr.Signature = PED_CPU_TO_LE16(MSDOS_MBR_SIGNATURE);
- pmbr.PartitionRecord[0].OSType = EFI_PMBR_OSTYPE_EFI;
- pmbr.PartitionRecord[0].StartSector = 1;
- pmbr.PartitionRecord[0].EndHead = 0xFE;
- pmbr.PartitionRecord[0].EndSector = 0xFF;
- pmbr.PartitionRecord[0].EndTrack = 0xFF;
- pmbr.PartitionRecord[0].StartingLBA = PED_CPU_TO_LE32(1);
+ memset(pmbr, 0, buf_len);
+ pmbr->Signature = PED_CPU_TO_LE16(MSDOS_MBR_SIGNATURE);
+ pmbr->PartitionRecord[0].OSType = EFI_PMBR_OSTYPE_EFI;
+ pmbr->PartitionRecord[0].StartSector = 1;
+ pmbr->PartitionRecord[0].EndHead = 0xFE;
+ pmbr->PartitionRecord[0].EndSector = 0xFF;
+ pmbr->PartitionRecord[0].EndTrack = 0xFF;
+ pmbr->PartitionRecord[0].StartingLBA = PED_CPU_TO_LE32(1);
if ((dev->length - 1ULL) > 0xFFFFFFFFULL)
- pmbr.PartitionRecord[0].SizeInLBA = PED_CPU_TO_LE32(0xFFFFFFFF);
+ pmbr->PartitionRecord[0].SizeInLBA = PED_CPU_TO_LE32(0xFFFFFFFF);
else
- pmbr.PartitionRecord[0].SizeInLBA = PED_CPU_TO_LE32(dev->length - 1UL);
+ pmbr->PartitionRecord[0].SizeInLBA = PED_CPU_TO_LE32(dev->length - 1UL);
- return ped_device_write (dev, &pmbr, GPT_PMBR_LBA, GPT_PMBR_SECTORS);
+ int write_ok = ped_device_write (dev, pmbr, GPT_PMBR_LBA,
+ GPT_PMBR_SECTORS);
+ free (pmbr);
+ return write_ok;
}
static void
free (ptes);
return ped_device_sync (disk->dev);
+ free (pth_raw);
error_free_ptes:
free (ptes);
error:
if (!(disk = _ped_disk_alloc (dev, &amiga_disk_type)))
return NULL;
- if (!(disk->disk_specific = ped_malloc (PED_SECTOR_SIZE_DEFAULT))) {
+ if (!(disk->disk_specific = ped_malloc (disk->dev->sector_size))) {
free (disk);
return NULL;
}
rdb->rdb_ID = PED_CPU_TO_BE32 (IDNAME_RIGIDDISK);
rdb->rdb_SummedLongs = PED_CPU_TO_BE32 (64);
rdb->rdb_HostID = PED_CPU_TO_BE32 (0);
- rdb->rdb_BlockBytes = PED_CPU_TO_BE32 (PED_SECTOR_SIZE_DEFAULT);
+ rdb->rdb_BlockBytes = PED_CPU_TO_BE32 (disk->dev->sector_size);
rdb->rdb_Flags = PED_CPU_TO_BE32 (0);
/* Block lists */
int result = 0;
PED_ASSERT(dev != NULL, return 0);
- if ((rdb=RDSK(ped_malloc(PED_SECTOR_SIZE_DEFAULT)))==NULL)
+ if ((rdb=RDSK(ped_malloc(dev->sector_size)))==NULL)
return 0;
while ((i = _amiga_find_rdb (dev, rdb)) != AMIGA_RDB_NOT_FOUND) {
PED_ASSERT (disk->dev != NULL, return 0);
PED_ASSERT (disk->disk_specific != NULL, return 0);
- if (!(rdb = ped_malloc (PED_SECTOR_SIZE_DEFAULT)))
+ if (!(rdb = ped_malloc (disk->dev->sector_size)))
return 0;
/* Let's read the rdb */
memset ((char *)(RDSK(disk->disk_specific)) + pb_size,
0, PED_SECTOR_SIZE_DEFAULT - pb_size);
} else {
- memcpy (RDSK(disk->disk_specific), rdb, PED_SECTOR_SIZE_DEFAULT);
+ memcpy (RDSK(disk->disk_specific), rdb, disk->dev->sector_size);
}
free (rdb);
rdb = RDSK(disk->disk_specific);
table[i] = LINK_END;
/* Let's allocate a partition block */
- if (!(block = ped_malloc (PED_SECTOR_SIZE_DEFAULT))) {
+ if (!(block = ped_malloc (disk->dev->sector_size))) {
free (table);
return 0;
}
return NULL;
if (ped_partition_is_active (part)) {
- if (!(part->disk_specific = ped_malloc (PED_SECTOR_SIZE_DEFAULT))) {
+ if (!(part->disk_specific = ped_malloc (disk->dev->sector_size))) {
free (part);
return NULL;
}
for (type = ped_disk_type_get_next (NULL); type;
type = ped_disk_type_get_next (type)) {
+ fprintf (stderr, "create label: %s\n", type->name); fflush (stderr);
if (!_implemented_disk_label (type->name))
continue;
for (type = ped_disk_type_get_next (NULL); type;
type = ped_disk_type_get_next (type)) {
+ fprintf (stderr, "read label: %s\n", type->name); fflush (stderr);
if (!_implemented_disk_label (type->name))
continue;
for (type = ped_disk_type_get_next (NULL); type;
type = ped_disk_type_get_next (type)) {
+ fprintf (stderr, "read label: %s\n", type->name); fflush (stderr);
if (!_implemented_disk_label (type->name))
continue;