2 libparted - a library for manipulating disk partitions
3 Copyright (C) 2000, 2002, 2004, 2007-2009 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <parted/parted.h>
22 #include <parted/debug.h>
23 #include <parted/endian.h>
28 # define _(String) dgettext (PACKAGE, String)
30 # define _(String) (String)
31 #endif /* ENABLE_NLS */
36 /* struct's hacked from Linux source: fs/partitions/mac.h
37 * I believe it was originally written by Paul Mackerras (from comments in
41 * http://developer.apple.com/documentation/mac/Devices/Devices-126.html
42 * http://developer.apple.com/documentation/mac/Devices/Devices-121.html
43 * http://devworld.apple.com/technotes/tn/tn1189.html
46 * Apple_Bootstrap new-world (HFS) boot partition
47 * Apple_partition_map partition map (table)
48 * Apple_Driver device driver
49 * Apple_Driver43 SCSI Manager 4.3 device driver
50 * Apple_MFS original Macintosh File System
51 * Apple_HFS Hierarchical File System (and +)
52 * Apple_HFSX HFS+ with case sensitivity and more
53 * Apple_UNIX_SVR2 UNIX file system (UFS?)
54 * Apple_PRODOS ProDOS file system
55 * Apple_Free unused space
57 * Apple_Void padding for iso9660
58 * Apple_Extra an unused partition map entry
68 * partition volume or partition
71 * * All space must be accounted for, except block 0 (driver block) and
72 * block 1-X (the partition map: i.e. lots of MacRawPartitions)
74 * * It's really hard to grow/shrink the number of MacRawPartition
75 * entries in the partition map, because the first partition starts
76 * immediately after the partition map. When we can move the start of
77 * HFS and ext2 partitions, this problem will disappear ;-)
80 #define MAC_PARTITION_MAGIC_1 0x5453 /* old */
81 #define MAC_PARTITION_MAGIC_2 0x504d
82 #define MAC_DISK_MAGIC 0x4552
84 #define MAC_STATUS_BOOTABLE 8 /* partition is bootable */
86 typedef struct _MacRawPartition MacRawPartition;
87 typedef struct _MacRawDisk MacRawDisk;
88 typedef struct _MacDeviceDriver MacDeviceDriver;
89 typedef struct _MacPartitionData MacPartitionData;
90 typedef struct _MacDiskData MacDiskData;
92 struct __attribute__ ((packed)) _MacRawPartition {
93 uint16_t signature; /* expected to be MAC_PARTITION_MAGIC */
95 uint32_t map_count; /* # blocks in partition map */
96 uint32_t start_block; /* absolute starting block # of partition */
97 uint32_t block_count; /* number of blocks in partition */
98 char name[32]; /* partition name */
99 char type[32]; /* string type description */
100 uint32_t data_start; /* rel block # of first data block */
101 uint32_t data_count; /* number of data blocks */
102 uint32_t status; /* partition status bits */
108 uint32_t boot_entry2;
110 char processor[16]; /* Contains 680x0, x=0,2,3,4; or empty */
115 /* Driver descriptor structure, in block 0 */
116 struct __attribute__ ((packed)) _MacRawDisk {
117 uint16_t signature; /* expected to be MAC_DRIVER_MAGIC */
118 uint16_t block_size; /* physical sector size */
119 uint32_t block_count; /* size of device in blocks */
120 uint16_t dev_type; /* reserved */
121 uint16_t dev_id; /* reserved */
122 uint32_t data; /* reserved */
123 uint16_t driver_count; /* # of driver descriptor entries */
124 uint8_t driverlist[488];/* info about available drivers */
125 uint16_t padding[3]; /* pad to 512 bytes */
128 struct __attribute__ ((packed)) _MacDeviceDriver {
129 uint32_t block; /* startblock in MacRawDisk->block_size units */
130 uint16_t size; /* size in 512 byte units */
131 uint16_t type; /* operating system type (MacOS = 1) */
134 struct _MacPartitionData {
135 char volume_name[33]; /* eg: "Games" */
136 char system_name[33]; /* eg: "Apple_Unix_SVR2" */
137 char processor_name[17];
147 PedSector data_region_length;
148 PedSector boot_region_length;
150 uint32_t boot_base_address;
151 uint32_t boot_entry_address;
152 uint32_t boot_checksum;
158 struct _MacDiskData {
159 int ghost_size; /* sectors per "driver" block */
160 int part_map_entry_count; /* # entries (incl. ghost) */
161 int part_map_entry_num; /* partition map location */
163 int active_part_entry_count; /* # real partitions */
164 int free_part_entry_count; /* # free space */
165 int last_part_entry_num; /* last entry number */
167 uint16_t block_size; /* physical sector size */
168 uint16_t driver_count;
169 MacDeviceDriver driverlist[1 + 60]; /* 488 bytes */
172 static PedDiskType mac_disk_type;
175 _check_signature (MacRawDisk const *raw_disk)
177 if (PED_BE16_TO_CPU (raw_disk->signature) != MAC_DISK_MAGIC) {
181 return ped_exception_throw (
183 PED_EXCEPTION_IGNORE_CANCEL,
184 _("Invalid signature %x for Mac disk labels."),
185 (int) PED_BE16_TO_CPU (raw_disk->signature))
186 == PED_EXCEPTION_IGNORE;
194 _rawpart_check_signature (MacRawPartition* raw_part)
196 int sig = (int) PED_BE16_TO_CPU (raw_part->signature);
197 return sig == MAC_PARTITION_MAGIC_1 || sig == MAC_PARTITION_MAGIC_2;
201 mac_probe (const PedDevice * dev)
203 PED_ASSERT (dev != NULL, return 0);
205 if (dev->sector_size < sizeof (MacRawDisk))
209 if (!ptt_read_sector (dev, 0, &label))
212 int valid = _check_signature (label);
219 _disk_add_part_map_entry (PedDisk* disk, int warn)
221 MacDiskData* mac_disk_data = disk->disk_specific;
222 PedPartition* new_part;
223 MacPartitionData* mac_part_data;
224 PedSector part_map_size;
225 PedConstraint* constraint_any = ped_constraint_any (disk->dev);
227 #ifndef DISCOVER_ONLY
228 if (warn && ped_exception_throw (
230 PED_EXCEPTION_FIX | PED_EXCEPTION_CANCEL,
231 _("Partition map has no partition map entry!"))
232 != PED_EXCEPTION_FIX)
234 #endif /* !DISCOVER_ONLY */
237 = ped_round_up_to (mac_disk_data->last_part_entry_num, 64);
238 if (part_map_size == 0)
241 new_part = ped_partition_new (disk, PED_PARTITION_NORMAL, NULL,
242 1, part_map_size - 1);
246 mac_part_data = new_part->disk_specific;
247 strcpy (mac_part_data->volume_name, "Apple");
248 strcpy (mac_part_data->system_name, "Apple_partition_map");
250 if (!ped_disk_add_partition (disk, new_part, constraint_any))
251 goto error_destroy_new_part;
253 mac_disk_data->part_map_entry_num = new_part->num;
254 mac_disk_data->part_map_entry_count
255 = new_part->geom.end - mac_disk_data->ghost_size;
256 ped_constraint_destroy (constraint_any);
259 error_destroy_new_part:
260 ped_partition_destroy (new_part);
262 ped_constraint_destroy (constraint_any);
267 mac_alloc (const PedDevice* dev)
270 MacDiskData* mac_disk_data;
272 PED_ASSERT (dev != NULL, return NULL);
274 #ifndef DISCOVER_ONLY
275 if (dev->length < 256) {
276 ped_exception_throw (
278 PED_EXCEPTION_CANCEL,
279 _("%s is too small for a Mac disk label!"),
285 disk = _ped_disk_alloc (dev, &mac_disk_type);
289 mac_disk_data = (MacDiskData*) ped_malloc (sizeof (MacDiskData));
291 goto error_free_disk;
292 disk->disk_specific = mac_disk_data;
293 mac_disk_data->ghost_size = disk->dev->sector_size / 512;
294 mac_disk_data->active_part_entry_count = 0;
295 mac_disk_data->free_part_entry_count = 1;
296 mac_disk_data->last_part_entry_num = 1;
297 mac_disk_data->block_size = 0;
298 mac_disk_data->driver_count = 0;
299 memset(&mac_disk_data->driverlist[0], 0, sizeof(mac_disk_data->driverlist));
301 if (!_disk_add_part_map_entry (disk, 0))
302 goto error_free_disk;
306 _ped_disk_free (disk);
312 mac_duplicate (const PedDisk* disk)
315 MacDiskData* new_mac_data;
316 MacDiskData* old_mac_data = (MacDiskData*) disk->disk_specific;
317 PedPartition* partition_map;
319 new_disk = ped_disk_new_fresh (disk->dev, &mac_disk_type);
323 new_mac_data = (MacDiskData*) new_disk->disk_specific;
325 /* remove the partition map partition - it will be duplicated
328 partition_map = ped_disk_get_partition_by_sector (new_disk, 1);
329 PED_ASSERT (partition_map != NULL, return 0);
330 ped_disk_remove_partition (new_disk, partition_map);
332 /* ugly, but C is ugly :p */
333 memcpy (new_mac_data, old_mac_data, sizeof (MacDiskData));
336 _ped_disk_free (new_disk);
342 mac_free (PedDisk* disk)
344 MacDiskData* mac_disk_data = disk->disk_specific;
346 _ped_disk_free (disk);
347 free (mac_disk_data);
350 #ifndef DISCOVER_ONLY
352 _clobber_part_map (PedDevice* dev)
354 void *buf = ped_malloc (dev->sector_size);
360 for (sector=1; 1; sector++) {
361 if (!ped_device_read (dev, buf, sector, 1)) {
365 if (!_rawpart_check_signature (buf)) {
369 memset (buf, 0, dev->sector_size);
370 if (!ped_device_write (dev, buf, sector, 1)) {
380 mac_clobber (PedDevice* dev)
383 if (!ptt_read_sector (dev, 0, &buf))
386 if (!_check_signature (buf)) {
391 memset (buf, 0, dev->sector_size);
392 bool ok = ped_device_write (dev, buf, 0, 1);
397 return _clobber_part_map (dev);
399 #endif /* !DISCOVER_ONLY */
402 _rawpart_cmp_type (const MacRawPartition* raw_part, const char* type)
404 return strncasecmp (raw_part->type, type, 32) == 0;
408 _rawpart_cmp_name (const MacRawPartition* raw_part, const char* name)
410 return strncasecmp (raw_part->name, name, 32) == 0;
414 _rawpart_is_partition_map (const MacRawPartition* raw_part)
416 return _rawpart_cmp_type (raw_part, "Apple_partition_map");
420 strncasestr (const char* haystack, const char* needle, int n)
422 int needle_size = strlen (needle);
425 for (i = 0; haystack[i] && i < n - needle_size; i++) {
426 if (strncasecmp (haystack + i, needle, needle_size) == 0)
434 _rawpart_is_boot (const MacRawPartition* raw_part)
436 if (!strcasecmp(raw_part->type, "Apple_Bootstrap"))
439 if (!strcasecmp(raw_part->type, "Apple_Boot"))
446 _rawpart_is_driver (const MacRawPartition* raw_part)
448 if (strncmp (raw_part->type, "Apple_", 6) != 0)
450 if (!strncasestr (raw_part->type, "driver", 32))
456 _rawpart_has_driver (const MacRawPartition* raw_part, MacDiskData* mac_disk_data)
458 MacDeviceDriver *driverlist;
460 uint32_t driver_bs, driver_be, part_be;
462 driverlist = &mac_disk_data->driverlist[0];
463 bsz = mac_disk_data->block_size / 512;
464 for (i = 0; i < mac_disk_data->driver_count; i++) {
465 driver_bs = driverlist->block * bsz;
466 driver_be = driver_bs + driverlist->size;
467 part_be = raw_part->start_block + raw_part->block_count;
468 if (driver_bs >= raw_part->start_block && driver_be <= part_be)
476 _rawpart_is_root (MacRawPartition* raw_part)
478 if (!_rawpart_cmp_type (raw_part, "Apple_UNIX_SVR2"))
480 if (strcmp (raw_part->name, "root") != 0)
486 _rawpart_is_swap (MacRawPartition* raw_part)
488 if (!_rawpart_cmp_type (raw_part, "Apple_UNIX_SVR2"))
490 if (strcmp (raw_part->name, "swap") != 0)
496 _rawpart_is_lvm (MacRawPartition* raw_part)
498 if (strcmp (raw_part->type, "Linux_LVM") != 0)
504 _rawpart_is_raid (MacRawPartition* raw_part)
506 if (strcmp (raw_part->type, "Linux_RAID") != 0)
512 _rawpart_is_void (MacRawPartition* raw_part)
514 return _rawpart_cmp_type (raw_part, "Apple_Void");
517 /* returns 1 if the raw_part represents a partition that is "unused space", or
518 * doesn't represent a partition at all. NOTE: some people make Apple_Free
519 * partitions with MacOS, because they can't select another type. So, if the
520 * name is anything other than "Extra" or "", it is treated as a "real"
524 _rawpart_is_active (MacRawPartition* raw_part)
526 if (_rawpart_cmp_type (raw_part, "Apple_Free")
527 && (_rawpart_cmp_name (raw_part, "Extra")
528 || _rawpart_cmp_name (raw_part, "")))
530 if (_rawpart_cmp_type (raw_part, "Apple_Void"))
532 if (_rawpart_cmp_type (raw_part, "Apple_Scratch"))
534 if (_rawpart_cmp_type (raw_part, "Apple_Extra"))
541 _rawpart_analyse (MacRawPartition* raw_part, PedDisk* disk, int num)
543 MacDiskData* mac_disk_data;
545 MacPartitionData* mac_part_data;
546 PedSector block_size;
547 PedSector start, length;
549 if (!_rawpart_check_signature (raw_part)) {
550 #ifndef DISCOVER_ONLY
551 if (ped_exception_throw (
552 PED_EXCEPTION_WARNING,
553 PED_EXCEPTION_IGNORE_CANCEL,
554 _("Partition %d has an invalid signature %x."),
556 (int) PED_BE16_TO_CPU (raw_part->signature))
557 != PED_EXCEPTION_IGNORE)
562 mac_disk_data = (MacDiskData*) disk->disk_specific;
563 block_size = disk->dev->sector_size / 512;
565 start = PED_BE32_TO_CPU (raw_part->start_block) * block_size;
566 length = PED_BE32_TO_CPU (raw_part->block_count) * block_size;
568 #ifndef DISCOVER_ONLY
569 ped_exception_throw (
571 PED_EXCEPTION_CANCEL,
572 _("Partition %d has an invalid length of 0 bytes!"),
577 part = ped_partition_new (disk, PED_PARTITION_NORMAL, NULL,
578 start, start + length - 1);
582 mac_part_data = part->disk_specific;
584 strncpy (mac_part_data->volume_name, raw_part->name, 32);
585 strncpy (mac_part_data->system_name, raw_part->type, 32);
586 strncpy (mac_part_data->processor_name, raw_part->processor, 16);
588 mac_part_data->is_boot = _rawpart_is_boot (raw_part);
589 mac_part_data->is_driver = _rawpart_is_driver (raw_part);
590 if (mac_part_data->is_driver)
591 mac_part_data->has_driver = _rawpart_has_driver(raw_part, mac_disk_data);
592 mac_part_data->is_root = _rawpart_is_root (raw_part);
593 mac_part_data->is_swap = _rawpart_is_swap (raw_part);
594 mac_part_data->is_lvm = _rawpart_is_lvm (raw_part);
595 mac_part_data->is_raid = _rawpart_is_raid (raw_part);
598 #ifndef DISCOVER_ONLY
599 if (raw_part->data_start) {
600 ped_exception_throw (
602 PED_EXCEPTION_CANCEL,
603 _("The data region doesn't start at the start "
604 "of the partition."));
605 goto error_destroy_part;
607 #endif /* !DISCOVER_ONLY */
608 mac_part_data->data_region_length
609 = PED_BE32_TO_CPU (raw_part->data_count) * block_size;
611 /* boot region - we have no idea what this is for, but Mac OSX
612 * seems to put garbage here, and doesn't pay any attention to
613 * it afterwards. [clausen, dan burcaw]
616 if (raw_part->boot_start) {
617 ped_exception_throw (
619 PED_EXCEPTION_CANCEL,
620 _("The boot region doesn't start at the start "
621 "of the partition."));
622 goto error_destroy_part;
625 mac_part_data->boot_region_length
626 = PED_BE32_TO_CPU (raw_part->boot_count) * block_size;
628 #ifndef DISCOVER_ONLY
629 if (mac_part_data->has_driver) {
630 if (mac_part_data->boot_region_length < part->geom.length) {
631 if (ped_exception_throw (
633 PED_EXCEPTION_IGNORE_CANCEL,
634 _("The partition's boot region doesn't occupy "
635 "the entire partition."))
636 != PED_EXCEPTION_IGNORE)
637 goto error_destroy_part;
640 if (mac_part_data->data_region_length < part->geom.length &&
641 !mac_part_data->is_boot) {
642 if (ped_exception_throw (
644 PED_EXCEPTION_IGNORE_CANCEL,
645 _("The partition's data region doesn't occupy "
646 "the entire partition."))
647 != PED_EXCEPTION_IGNORE)
648 goto error_destroy_part;
651 #endif /* !DISCOVER_ONLY */
653 mac_part_data->boot_base_address
654 = PED_BE32_TO_CPU (raw_part->boot_load);
655 mac_part_data->boot_entry_address
656 = PED_BE32_TO_CPU (raw_part->boot_entry);
657 mac_part_data->boot_checksum
658 = PED_BE32_TO_CPU (raw_part->boot_cksum);
660 mac_part_data->status = PED_BE32_TO_CPU (raw_part->status);
661 mac_part_data->driver_sig = PED_BE32_TO_CPU (raw_part->driver_sig);
666 ped_partition_destroy (part);
671 /* looks at the partition map size field in a mac raw partition, and calculates
672 * what the size of the partition map should be, from it
675 _rawpart_get_partmap_size (MacRawPartition* raw_part, PedDisk* disk)
677 MacDiskData* mac_disk_data = disk->disk_specific;
678 PedSector sector_size = disk->dev->sector_size / 512;
679 PedSector part_map_start;
680 PedSector part_map_end;
682 part_map_start = mac_disk_data->ghost_size;
683 part_map_end = sector_size * PED_BE32_TO_CPU (raw_part->map_count);
685 return part_map_end - part_map_start + 1;
689 _disk_analyse_block_size (PedDisk* disk, MacRawDisk* raw_disk)
691 PedSector block_size;
693 if (PED_BE16_TO_CPU (raw_disk->block_size) % 512) {
694 #ifndef DISCOVER_ONLY
695 ped_exception_throw (
697 PED_EXCEPTION_CANCEL,
698 _("Weird block size on device descriptor: %d bytes is "
699 "not divisible by 512."),
700 (int) PED_BE16_TO_CPU (raw_disk->block_size));
705 block_size = PED_BE16_TO_CPU (raw_disk->block_size) / 512;
706 if (block_size != disk->dev->sector_size / 512) {
707 #ifndef DISCOVER_ONLY
708 if (ped_exception_throw (
709 PED_EXCEPTION_WARNING,
710 PED_EXCEPTION_IGNORE_CANCEL,
711 _("The driver descriptor says the physical block size "
712 "is %d bytes, but Linux says it is %d bytes."),
713 (int) block_size * 512,
714 (int) disk->dev->sector_size)
715 != PED_EXCEPTION_IGNORE)
718 disk->dev->sector_size = block_size * 512;
727 /* Tries to figure out the block size used by the drivers, for the ghost
728 * partitioning scheme. Ghost partitioning works like this: the OpenFirmware
729 * (OF) sees 512 byte blocks, but some drivers use 2048 byte blocks (and,
730 * perhaps, some other number?). To remain compatible, the partition map
731 * only has "real" partition map entries on ghost-aligned block numbers (and
732 * the others are padded with Apple_Void partitions). This function tries
733 * to figure out what the "ghost-aligned" size is... (which, believe-it-or-not,
734 * doesn't always equal 2048!!!)
737 _disk_analyse_ghost_size (PedDisk* disk)
739 MacDiskData* mac_disk_data = disk->disk_specific;
740 MacRawPartition raw_part;
743 for (i = 1; i < 64; i *= 2) {
744 if (!ped_device_read (disk->dev, &raw_part, i, 1))
746 if (_rawpart_check_signature (&raw_part)
747 && !_rawpart_is_void (&raw_part)) {
748 mac_disk_data->ghost_size = i;
749 PED_ASSERT (i <= disk->dev->sector_size / 512,
755 #ifndef DISCOVER_ONLY
756 ped_exception_throw (
758 PED_EXCEPTION_CANCEL,
759 _("No valid partition map found."));
765 mac_read (PedDisk* disk)
767 MacRawPartition raw_part;
768 MacDiskData* mac_disk_data;
771 PedSector ghost_size;
772 PedConstraint* constraint_exact;
773 int last_part_entry_num = 0;
775 PED_ASSERT (disk != NULL, return 0);
777 mac_disk_data = disk->disk_specific;
778 mac_disk_data->part_map_entry_num = 0; /* 0 == none */
781 if (!ptt_read_sector (disk->dev, 0, &s0))
784 MacRawDisk *raw_disk = (MacRawDisk *) s0;
786 if (!_check_signature (raw_disk))
789 if (!_disk_analyse_block_size (disk, raw_disk))
791 if (!_disk_analyse_ghost_size (disk))
793 ghost_size = mac_disk_data->ghost_size;
795 if (!ped_disk_delete_all (disk))
798 if (raw_disk->driver_count && raw_disk->driver_count < 62) {
799 memcpy(&mac_disk_data->driverlist[0], &raw_disk->driverlist[0],
800 sizeof(mac_disk_data->driverlist));
801 mac_disk_data->driver_count = raw_disk->driver_count;
802 mac_disk_data->block_size = raw_disk->block_size;
805 for (num=1; num==1 || num <= last_part_entry_num; num++) {
806 if (!ped_device_read (disk->dev, &raw_part,
807 num * ghost_size, 1))
808 goto error_delete_all;
810 if (!_rawpart_check_signature (&raw_part))
815 = _rawpart_get_partmap_size (&raw_part, disk);
816 if (_rawpart_get_partmap_size (&raw_part, disk)
817 != last_part_entry_num) {
818 if (ped_exception_throw (
820 PED_EXCEPTION_IGNORE_CANCEL,
821 _("Conflicting partition map entry sizes! "
822 "Entry 1 says it is %d, but entry %d says "
825 _rawpart_get_partmap_size (&raw_part, disk))
826 != PED_EXCEPTION_IGNORE)
827 goto error_delete_all;
830 if (!_rawpart_is_active (&raw_part))
833 part = _rawpart_analyse (&raw_part, disk, num);
835 goto error_delete_all;
837 part->fs_type = ped_file_system_probe (&part->geom);
838 constraint_exact = ped_constraint_exact (&part->geom);
839 if (!ped_disk_add_partition (disk, part, constraint_exact))
840 goto error_delete_all;
841 ped_constraint_destroy (constraint_exact);
843 if (_rawpart_is_partition_map (&raw_part)) {
844 if (mac_disk_data->part_map_entry_num
845 && ped_exception_throw (
847 PED_EXCEPTION_IGNORE_CANCEL,
848 _("Weird! There are 2 partitions "
850 != PED_EXCEPTION_IGNORE)
851 goto error_delete_all;
853 mac_disk_data->part_map_entry_num = num;
854 mac_disk_data->part_map_entry_count
855 = part->geom.end - ghost_size + 1;
859 if (!mac_disk_data->part_map_entry_num) {
860 if (!_disk_add_part_map_entry (disk, 1))
861 goto error_delete_all;
862 ped_disk_commit_to_dev (disk);
868 ped_disk_delete_all (disk);
874 #ifndef DISCOVER_ONLY
875 /* The Ghost partition: is a blank entry, used to pad out each block (where
876 * there physical block size > 512 bytes). This is because OpenFirmware uses
877 * 512 byte blocks, but device drivers Think Different TM, with a different
878 * lbock size, so we need to do this to avoid a clash (!)
881 _pad_raw_part (PedDisk* disk, int num, MacRawPartition* part_map)
883 MacDiskData* mac_disk_data = disk->disk_specific;
884 MacRawPartition ghost_entry;
887 memset (&ghost_entry, 0, sizeof (ghost_entry));
888 ghost_entry.signature = PED_CPU_TO_BE16 (MAC_PARTITION_MAGIC_2);
889 strcpy (ghost_entry.type, "Apple_Void");
890 ghost_entry.map_count
891 = PED_CPU_TO_BE32 (mac_disk_data->last_part_entry_num);
893 for (i=0; i < mac_disk_data->ghost_size - 1; i++)
894 memcpy (&part_map [i + (num - 1) * mac_disk_data->ghost_size],
895 &ghost_entry, sizeof (MacRawPartition));
901 _update_driver_count (MacRawPartition* part_map_entry,
902 MacDiskData *mac_driverdata, const MacDiskData* mac_disk_data)
904 uint16_t i, count_orig, count_cur, bsz;
905 uint32_t driver_bs, driver_be, part_be;
907 bsz = mac_disk_data->block_size / 512;
908 count_cur = mac_driverdata->driver_count;
909 count_orig = mac_disk_data->driver_count;
910 for (i = 0; i < count_orig; i++) {
911 driver_bs = mac_disk_data->driverlist[i].block * bsz;
912 driver_be = driver_bs + mac_disk_data->driverlist[i].size;
913 part_be = part_map_entry->start_block + part_map_entry->block_count;
914 if (driver_bs >= part_map_entry->start_block
915 && driver_be <= part_be) {
916 mac_driverdata->driverlist[count_cur].block
917 = mac_disk_data->driverlist[i].block;
918 mac_driverdata->driverlist[count_cur].size
919 = mac_disk_data->driverlist[i].size;
920 mac_driverdata->driverlist[count_cur].type
921 = mac_disk_data->driverlist[i].type;
922 mac_driverdata->driver_count++;
929 _generate_raw_part (PedDisk* disk, PedPartition* part,
930 MacRawPartition* part_map, MacDiskData *mac_driverdata)
932 MacDiskData* mac_disk_data;
933 MacPartitionData* mac_part_data;
934 MacRawPartition* part_map_entry;
935 PedSector block_size = disk->dev->sector_size / 512;
937 PED_ASSERT (part->num > 0, goto error);
939 mac_disk_data = disk->disk_specific;
940 mac_part_data = part->disk_specific;
942 part_map_entry = &part_map [part->num * mac_disk_data->ghost_size - 1];
944 part_map_entry->signature = PED_CPU_TO_BE16 (MAC_PARTITION_MAGIC_2);
945 part_map_entry->map_count
946 = PED_CPU_TO_BE32 (mac_disk_data->last_part_entry_num);
947 part_map_entry->start_block
948 = PED_CPU_TO_BE32 (part->geom.start / block_size);
949 part_map_entry->block_count
950 = PED_CPU_TO_BE32 (part->geom.length / block_size);
951 strcpy (part_map_entry->name, mac_part_data->volume_name);
952 strcpy (part_map_entry->type, mac_part_data->system_name);
954 if (mac_part_data->is_driver) {
955 mac_part_data->boot_region_length = part->geom.length;
956 if (mac_part_data->has_driver)
957 _update_driver_count(part_map_entry, mac_driverdata,
960 mac_part_data->data_region_length = part->geom.length;
961 part_map_entry->data_count = PED_CPU_TO_BE32 (
962 mac_part_data->data_region_length / block_size);
963 part_map_entry->boot_count = PED_CPU_TO_BE32 (
964 mac_part_data->boot_region_length / block_size);
965 part_map_entry->status = PED_CPU_TO_BE32 (mac_part_data->status);
966 part_map_entry->driver_sig
967 = PED_CPU_TO_BE32 (mac_part_data->driver_sig);
969 part_map_entry->boot_load =
970 PED_CPU_TO_BE32 (mac_part_data->boot_base_address);
971 part_map_entry->boot_entry =
972 PED_CPU_TO_BE32 (mac_part_data->boot_entry_address);
973 part_map_entry->boot_cksum =
974 PED_CPU_TO_BE32 (mac_part_data->boot_checksum);
976 strncpy (part_map_entry->processor, mac_part_data->processor_name, 16);
978 if (!_pad_raw_part (disk, part->num, part_map))
988 _generate_raw_freespace_part (PedDisk* disk, PedGeometry* geom, int num,
989 MacRawPartition* part_map)
991 MacDiskData* mac_disk_data = disk->disk_specific;
992 MacRawPartition* part_map_entry;
993 PedSector block_size = disk->dev->sector_size / 512;
995 PED_ASSERT (num > 0, goto error);
997 part_map_entry = &part_map [num * mac_disk_data->ghost_size - 1];
999 part_map_entry->signature = PED_CPU_TO_BE16 (MAC_PARTITION_MAGIC_2);
1000 part_map_entry->map_count
1001 = PED_CPU_TO_BE32 (mac_disk_data->last_part_entry_num);
1002 part_map_entry->start_block
1003 = PED_CPU_TO_BE32 (geom->start / block_size);
1004 part_map_entry->block_count
1005 = PED_CPU_TO_BE32 (geom->length / block_size);
1006 strcpy (part_map_entry->name, "Extra");
1007 strcpy (part_map_entry->type, "Apple_Free");
1009 part_map_entry->data_count = PED_CPU_TO_BE32 (geom->length);
1010 part_map_entry->status = 0;
1011 part_map_entry->driver_sig = 0;
1013 if (!_pad_raw_part (disk, num, part_map))
1023 _generate_empty_part (PedDisk* disk, int num, MacRawPartition* part_map)
1025 MacDiskData* mac_disk_data = disk->disk_specific;
1026 MacRawPartition* part_map_entry;
1028 PED_ASSERT (num > 0, return 0);
1030 part_map_entry = &part_map [num * mac_disk_data->ghost_size - 1];
1031 part_map_entry->signature = PED_CPU_TO_BE16 (MAC_PARTITION_MAGIC_2);
1032 part_map_entry->map_count
1033 = PED_CPU_TO_BE32 (mac_disk_data->last_part_entry_num);
1034 strcpy (part_map_entry->type, "Apple_Void");
1036 return _pad_raw_part (disk, num, part_map);
1039 /* returns the first empty entry in the partition map */
1041 _get_first_empty_part_entry (PedDisk* disk, MacRawPartition* part_map)
1043 MacDiskData* mac_disk_data = disk->disk_specific;
1046 for (i=1; i <= mac_disk_data->last_part_entry_num; i++) {
1047 if (!part_map[i * mac_disk_data->ghost_size - 1].signature)
1055 write_block_zero (PedDisk* disk, MacDiskData* mac_driverdata)
1057 PedDevice* dev = disk->dev;
1059 if (!ptt_read_sector (dev, 0, &s0))
1061 MacRawDisk *raw_disk = (MacRawDisk *) s0;
1063 raw_disk->signature = PED_CPU_TO_BE16 (MAC_DISK_MAGIC);
1064 raw_disk->block_size = PED_CPU_TO_BE16 (dev->sector_size);
1065 raw_disk->block_count
1066 = PED_CPU_TO_BE32 (dev->length / (dev->sector_size / 512));
1068 raw_disk->driver_count = mac_driverdata->driver_count;
1069 memcpy(&raw_disk->driverlist[0], &mac_driverdata->driverlist[0],
1070 sizeof(raw_disk->driverlist));
1072 return ped_device_write (dev, raw_disk, 0, 1);
1076 mac_write (PedDisk* disk)
1078 MacRawPartition* part_map;
1079 MacDiskData* mac_disk_data;
1080 MacDiskData* mac_driverdata; /* updated driver list */
1084 PED_ASSERT (disk != NULL, return 0);
1085 PED_ASSERT (disk->disk_specific != NULL, return 0);
1086 PED_ASSERT (disk->dev != NULL, return 0);
1087 PED_ASSERT (!disk->update_mode, return 0);
1089 mac_disk_data = disk->disk_specific;
1091 if (!ped_disk_get_partition (disk, mac_disk_data->part_map_entry_num)) {
1092 if (!_disk_add_part_map_entry (disk, 1))
1096 mac_driverdata = ped_malloc(sizeof(MacDiskData));
1097 if (!mac_driverdata)
1099 memset (mac_driverdata, 0, sizeof(MacDiskData));
1101 size_t pmap_bytes = (mac_disk_data->part_map_entry_count
1102 * disk->dev->sector_size);
1103 part_map = (MacRawPartition*) ped_calloc (pmap_bytes);
1105 goto error_free_driverdata;
1107 /* write (to memory) the "real" partitions */
1108 for (part = ped_disk_next_partition (disk, NULL); part;
1109 part = ped_disk_next_partition (disk, part)) {
1110 if (!ped_partition_is_active (part))
1112 if (!_generate_raw_part (disk, part, part_map, mac_driverdata))
1113 goto error_free_part_map;
1116 /* write the "free space" partitions */
1117 for (part = ped_disk_next_partition (disk, NULL); part;
1118 part = ped_disk_next_partition (disk, part)) {
1119 if (part->type != PED_PARTITION_FREESPACE)
1121 num = _get_first_empty_part_entry (disk, part_map);
1122 if (!_generate_raw_freespace_part (disk, &part->geom, num,
1124 goto error_free_part_map;
1127 /* write the "void" (empty) partitions */
1128 for (num = _get_first_empty_part_entry (disk, part_map); num;
1129 num = _get_first_empty_part_entry (disk, part_map))
1130 _generate_empty_part (disk, num, part_map);
1133 if (!ped_device_write (disk->dev, part_map, 1,
1134 mac_disk_data->part_map_entry_count))
1135 goto error_free_part_map;
1137 return write_block_zero (disk, mac_driverdata);
1139 error_free_part_map:
1141 error_free_driverdata:
1142 free (mac_driverdata);
1146 #endif /* !DISCOVER_ONLY */
1148 static PedPartition*
1150 const PedDisk* disk, PedPartitionType part_type,
1151 const PedFileSystemType* fs_type, PedSector start, PedSector end)
1154 MacPartitionData* mac_data;
1156 part = _ped_partition_alloc (disk, part_type, fs_type, start, end);
1160 if (ped_partition_is_active (part)) {
1162 = mac_data = ped_malloc (sizeof (MacPartitionData));
1164 goto error_free_part;
1166 memset (mac_data, 0, sizeof (MacPartitionData));
1167 strcpy (mac_data->volume_name, "untitled");
1169 part->disk_specific = NULL;
1179 static PedPartition*
1180 mac_partition_duplicate (const PedPartition* part)
1182 PedPartition* new_part;
1183 MacPartitionData* new_mac_data;
1184 MacPartitionData* old_mac_data;
1186 new_part = ped_partition_new (part->disk, part->type,
1187 part->fs_type, part->geom.start,
1191 new_part->num = part->num;
1193 old_mac_data = (MacPartitionData*) part->disk_specific;
1194 new_mac_data = (MacPartitionData*) new_part->disk_specific;
1196 /* ugly, but C is ugly :p */
1197 memcpy (new_mac_data, old_mac_data, sizeof (MacPartitionData));
1202 mac_partition_destroy (PedPartition* part)
1204 PED_ASSERT (part != NULL, return);
1206 if (ped_partition_is_active (part))
1207 free (part->disk_specific);
1212 mac_partition_set_system (PedPartition* part, const PedFileSystemType* fs_type)
1214 MacPartitionData* mac_data = part->disk_specific;
1216 part->fs_type = fs_type;
1218 if (fs_type && is_linux_swap (fs_type->name))
1219 ped_partition_set_flag (part, PED_PARTITION_SWAP, 1);
1221 if (mac_data->is_boot) {
1222 strcpy (mac_data->system_name, "Apple_Bootstrap");
1223 mac_data->status = 0x33;
1227 if (fs_type && (!strcmp (fs_type->name, "hfs")
1228 || !strcmp (fs_type->name, "hfs+"))) {
1229 strcpy (mac_data->system_name, "Apple_HFS");
1230 mac_data->status |= 0x7f;
1231 } else if (fs_type && !strcmp (fs_type->name, "hfsx")) {
1232 strcpy (mac_data->system_name, "Apple_HFSX");
1233 mac_data->status |= 0x7f;
1235 strcpy (mac_data->system_name, "Apple_UNIX_SVR2");
1236 mac_data->status = 0x33;
1243 mac_partition_set_flag (PedPartition* part, PedPartitionFlag flag, int state)
1245 MacPartitionData* mac_data;
1247 PED_ASSERT (part != NULL, return 0);
1248 PED_ASSERT (part->disk_specific != NULL, return 0);
1250 mac_data = part->disk_specific;
1253 case PED_PARTITION_BOOT:
1254 mac_data->is_boot = state;
1257 return mac_partition_set_system (part, part->fs_type);
1260 strcpy (mac_data->system_name, "Apple_Bootstrap");
1261 mac_data->status = 0x33;
1265 case PED_PARTITION_ROOT:
1267 strcpy (mac_data->volume_name, "root");
1268 mac_data->is_swap = 0;
1270 if (mac_data->is_root)
1271 strcpy (mac_data->volume_name, "untitled");
1273 mac_data->is_root = state;
1276 case PED_PARTITION_SWAP:
1278 strcpy (mac_data->volume_name, "swap");
1279 mac_data->is_root = 0;
1281 if (mac_data->is_swap)
1282 strcpy (mac_data->volume_name, "untitled");
1284 mac_data->is_swap = state;
1287 case PED_PARTITION_LVM:
1289 strcpy (mac_data->system_name, "Linux_LVM");
1290 mac_data->is_lvm = state;
1292 if (mac_data->is_lvm)
1293 mac_partition_set_system (part, part->fs_type);
1297 case PED_PARTITION_RAID:
1299 strcpy (mac_data->system_name, "Linux_RAID");
1300 mac_data->is_raid = state;
1302 if (mac_data->is_raid)
1303 mac_partition_set_system (part, part->fs_type);
1313 mac_partition_get_flag (const PedPartition* part, PedPartitionFlag flag)
1315 MacPartitionData* mac_data;
1317 PED_ASSERT (part != NULL, return 0);
1318 PED_ASSERT (part->disk_specific != NULL, return 0);
1320 mac_data = part->disk_specific;
1322 case PED_PARTITION_BOOT:
1323 return mac_data->is_boot;
1325 case PED_PARTITION_ROOT:
1326 return mac_data->is_root;
1328 case PED_PARTITION_SWAP:
1329 return mac_data->is_swap;
1331 case PED_PARTITION_LVM:
1332 return mac_data->is_lvm;
1334 case PED_PARTITION_RAID:
1335 return mac_data->is_raid;
1343 mac_partition_is_flag_available (
1344 const PedPartition* part, PedPartitionFlag flag)
1347 case PED_PARTITION_BOOT:
1348 case PED_PARTITION_ROOT:
1349 case PED_PARTITION_SWAP:
1350 case PED_PARTITION_LVM:
1351 case PED_PARTITION_RAID:
1360 mac_partition_set_name (PedPartition* part, const char* name)
1362 MacPartitionData* mac_data;
1365 PED_ASSERT (part != NULL, return);
1366 PED_ASSERT (part->disk_specific != NULL, return);
1367 mac_data = part->disk_specific;
1369 #ifndef DISCOVER_ONLY
1370 if (mac_data->is_root || mac_data->is_swap) {
1371 if (ped_exception_throw (
1372 PED_EXCEPTION_WARNING,
1373 PED_EXCEPTION_IGNORE_CANCEL,
1374 _("Changing the name of a root or swap partition "
1375 "will prevent Linux from recognising it as such."))
1376 != PED_EXCEPTION_IGNORE)
1378 mac_data->is_root = mac_data->is_swap = 0;
1382 strncpy (mac_data->volume_name, name, 32);
1383 mac_data->volume_name [32] = 0;
1384 for (i = strlen (mac_data->volume_name) - 1;
1385 mac_data->volume_name[i] == ' '; i--)
1386 mac_data->volume_name [i] = 0;
1390 mac_partition_get_name (const PedPartition* part)
1392 MacPartitionData* mac_data;
1394 PED_ASSERT (part != NULL, return NULL);
1395 PED_ASSERT (part->disk_specific != NULL, return NULL);
1396 mac_data = part->disk_specific;
1398 return mac_data->volume_name;
1401 static PedConstraint*
1402 _primary_constraint (PedDisk* disk)
1404 PedAlignment start_align;
1405 PedAlignment end_align;
1406 PedGeometry max_geom;
1407 PedSector sector_size;
1409 sector_size = disk->dev->sector_size / 512;
1411 if (!ped_alignment_init (&start_align, 0, sector_size))
1413 if (!ped_alignment_init (&end_align, -1, sector_size))
1415 if (!ped_geometry_init (&max_geom, disk->dev, 1, disk->dev->length - 1))
1418 return ped_constraint_new (&start_align, &end_align, &max_geom,
1419 &max_geom, 1, disk->dev->length);
1423 mac_partition_align (PedPartition* part, const PedConstraint* constraint)
1425 PED_ASSERT (part != NULL, return 0);
1427 if (_ped_partition_attempt_align (part, constraint,
1428 _primary_constraint (part->disk)))
1431 #ifndef DISCOVER_ONLY
1432 ped_exception_throw (
1433 PED_EXCEPTION_ERROR,
1434 PED_EXCEPTION_CANCEL,
1435 _("Unable to satisfy all constraints on the partition."));
1441 mac_partition_enumerate (PedPartition* part)
1444 MacDiskData* mac_disk_data;
1448 PED_ASSERT (part != NULL, return 0);
1449 PED_ASSERT (part->disk != NULL, return 0);
1452 mac_disk_data = (MacDiskData*) disk->disk_specific;
1454 max_part_count = ped_disk_get_max_primary_partition_count (disk);
1456 if (part->num > 0 && part->num <= mac_disk_data->part_map_entry_count)
1459 for (i = 1; i <= max_part_count; i++) {
1460 if (!ped_disk_get_partition (disk, i)) {
1466 #ifndef DISCOVER_ONLY
1467 ped_exception_throw (
1468 PED_EXCEPTION_ERROR,
1469 PED_EXCEPTION_CANCEL,
1470 _("Can't add another partition -- the partition map is too "
1478 _disk_count_partitions (PedDisk* disk)
1480 MacDiskData* mac_disk_data = disk->disk_specific;
1481 PedPartition* part = NULL;
1482 PedPartition* last = NULL;
1484 PED_ASSERT (disk->update_mode, return 0);
1486 mac_disk_data->active_part_entry_count = 0;
1487 mac_disk_data->free_part_entry_count = 0;
1488 mac_disk_data->last_part_entry_num = 0;
1490 /* subtle: we only care about free space after the partition map.
1491 * the partition map is an "active" partition, BTW... */
1492 for (part = ped_disk_next_partition (disk, part); part;
1493 part = ped_disk_next_partition (disk, part)) {
1494 if (!ped_partition_is_active (part))
1497 mac_disk_data->active_part_entry_count++;
1498 if (last && last->geom.end + 1 < part->geom.start)
1499 mac_disk_data->free_part_entry_count++;
1500 mac_disk_data->last_part_entry_num
1501 = PED_MAX (mac_disk_data->last_part_entry_num,
1507 if (last && last->geom.end < disk->dev->length - 1)
1508 mac_disk_data->free_part_entry_count++;
1510 mac_disk_data->last_part_entry_num
1511 = PED_MAX (mac_disk_data->last_part_entry_num,
1512 mac_disk_data->active_part_entry_count
1513 + mac_disk_data->free_part_entry_count);
1518 add_metadata_part (PedDisk* disk, PedSector start, PedSector end)
1520 PedPartition* new_part;
1521 PedConstraint* constraint_any = ped_constraint_any (disk->dev);
1523 PED_ASSERT (disk != NULL, return 0);
1525 new_part = ped_partition_new (disk, PED_PARTITION_METADATA, NULL,
1529 if (!ped_disk_add_partition (disk, new_part, constraint_any))
1530 goto error_destroy_new_part;
1532 ped_constraint_destroy (constraint_any);
1535 error_destroy_new_part:
1536 ped_partition_destroy (new_part);
1538 ped_constraint_destroy (constraint_any);
1543 mac_alloc_metadata (PedDisk* disk)
1545 MacDiskData* mac_disk_data;
1547 PED_ASSERT (disk != NULL, return 0);
1548 PED_ASSERT (disk->disk_specific != NULL, return 0);
1549 PED_ASSERT (disk->dev != NULL, return 0);
1551 mac_disk_data = disk->disk_specific;
1553 if (!add_metadata_part (disk, 0, disk->dev->sector_size / 512 - 1))
1556 /* hack: this seems to be a good place, to update the partition map
1557 * entry count, since mac_alloc_metadata() gets called during
1558 * _disk_pop_update_mode()
1560 return _disk_count_partitions (disk);
1564 mac_get_max_primary_partition_count (const PedDisk* disk)
1566 MacDiskData* mac_disk_data = disk->disk_specific;
1567 PedPartition* part_map_partition;
1569 part_map_partition = ped_disk_get_partition (disk,
1570 mac_disk_data->part_map_entry_num);
1572 /* HACK: if we haven't found the partition map partition (yet),
1575 if (!part_map_partition) {
1576 mac_disk_data->part_map_entry_num = 0;
1580 /* HACK: since Mac labels need an entry for free-space regions, we
1581 * must allow half plus 1 entries for free-space partitions. I hate
1582 * this, but things get REALLY complicated, otherwise.
1583 * (I'm prepared to complicate things later, but I want to get
1584 * everything working, first)
1586 return mac_disk_data->part_map_entry_count / mac_disk_data->ghost_size
1587 - mac_disk_data->free_part_entry_count + 1;
1591 mac_get_max_supported_partition_count (const PedDisk* disk, int *max_n)
1597 static PedDiskOps mac_disk_ops = {
1599 #ifndef DISCOVER_ONLY
1600 clobber: mac_clobber,
1605 duplicate: mac_duplicate,
1608 #ifndef DISCOVER_ONLY
1609 /* FIXME: remove this cast, once mac_write is fixed not to
1610 modify its *DISK parameter. */
1611 write: (int (*) (const PedDisk*)) mac_write,
1616 partition_new: mac_partition_new,
1617 partition_duplicate: mac_partition_duplicate,
1618 partition_destroy: mac_partition_destroy,
1619 partition_set_system: mac_partition_set_system,
1620 partition_set_flag: mac_partition_set_flag,
1621 partition_get_flag: mac_partition_get_flag,
1622 partition_is_flag_available: mac_partition_is_flag_available,
1623 partition_set_name: mac_partition_set_name,
1624 partition_get_name: mac_partition_get_name,
1625 partition_align: mac_partition_align,
1626 partition_enumerate: mac_partition_enumerate,
1628 alloc_metadata: mac_alloc_metadata,
1629 get_max_primary_partition_count:
1630 mac_get_max_primary_partition_count,
1631 get_max_supported_partition_count:
1632 mac_get_max_supported_partition_count
1635 static PedDiskType mac_disk_type = {
1639 features: PED_DISK_TYPE_PARTITION_NAME
1643 ped_disk_mac_init ()
1645 PED_ASSERT (sizeof (MacRawPartition) == 512, return);
1646 PED_ASSERT (sizeof (MacRawDisk) == 512, return);
1648 ped_disk_type_register (&mac_disk_type);
1652 ped_disk_mac_done ()
1654 ped_disk_type_unregister (&mac_disk_type);