OSDN Git Service

start to make things work with 2048-byte sector size.
authorJim Meyering <jim@meyering.net>
Fri, 1 Jun 2007 17:10:56 +0000 (19:10 +0200)
committerJim Meyering <meyering@redhat.com>
Fri, 24 Jul 2009 13:04:39 +0000 (15:04 +0200)
Done so far: amiga, bsd, loop, gpt.
Fix leaks along the way.
remove unused label
Add a cast to avoid a warning.

libparted/disk.c
libparted/labels/bsd.c
libparted/labels/gpt.c
libparted/labels/rdb.c
libparted/tests/label.c

index 5fb8060..4dae762 100644 (file)
@@ -148,8 +148,15 @@ ped_disk_probe (PedDevice* dev)
         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 ();
index 3d6b5ab..d030d44 100644 (file)
@@ -21,6 +21,7 @@
 
 #include <config.h>
 
+#include <stdbool.h>
 #include <parted/parted.h>
 #include <parted/debug.h>
 #include <parted/endian.h>
@@ -146,30 +147,45 @@ alpha_bootblock_checksum (char *boot) {
        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*
@@ -258,13 +274,12 @@ bsd_free (PedDisk* disk)
 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 */
 
@@ -277,8 +292,13 @@ bsd_read (PedDisk* disk)
 
        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++) {
index 3afc369..f1329fe 100644 (file)
@@ -259,6 +259,23 @@ typedef struct _GPTPartitionData {
 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)
 {
@@ -430,7 +447,6 @@ gpt_probe (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);
@@ -451,26 +467,31 @@ gpt_probe (const PedDevice * dev)
        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
@@ -973,8 +994,10 @@ error:
 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).
@@ -985,20 +1008,25 @@ _write_pmbr (PedDevice * dev)
        /* 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
@@ -1132,6 +1160,7 @@ gpt_write(const PedDisk * disk)
        free (ptes);
        return ped_device_sync (disk->dev);
 
+       free (pth_raw);
 error_free_ptes:
        free (ptes);
 error:
index c39230d..7232366 100644 (file)
@@ -354,7 +354,7 @@ amiga_alloc (const PedDevice* dev)
        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;
        }
@@ -365,7 +365,7 @@ amiga_alloc (const PedDevice* dev)
        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 */
@@ -451,7 +451,7 @@ amiga_clobber (PedDevice* dev)
        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) {
@@ -656,7 +656,7 @@ amiga_write (const PedDisk* disk)
        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 */
@@ -668,7 +668,7 @@ amiga_write (const PedDisk* disk)
                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);
@@ -693,7 +693,7 @@ amiga_write (const PedDisk* disk)
                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;
        }
@@ -807,7 +807,7 @@ amiga_partition_new (const PedDisk* disk, PedPartitionType part_type,
                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;
                }
index 2871c2c..1a0fbbc 100644 (file)
@@ -36,6 +36,7 @@ START_TEST (test_create_label)
 
         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;
 
@@ -90,6 +91,7 @@ START_TEST (test_read_label)
 
         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;
 
@@ -124,6 +126,7 @@ START_TEST (test_clone_label)
 
         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;