2 libparted - a library for manipulating disk partitions
3 Copyright (C) 2000, 2002, 2004, 2007-2008 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>
27 # define _(String) dgettext (PACKAGE, String)
29 # define _(String) (String)
30 #endif /* ENABLE_NLS */
34 /* struct's hacked from Linux source: fs/partitions/mac.h
35 * I believe it was originally written by Paul Mackerras (from comments in
39 * http://developer.apple.com/documentation/mac/Devices/Devices-126.html
40 * http://developer.apple.com/documentation/mac/Devices/Devices-121.html
41 * http://devworld.apple.com/technotes/tn/tn1189.html
44 * Apple_Bootstrap new-world (HFS) boot partition
45 * Apple_partition_map partition map (table)
46 * Apple_Driver device driver
47 * Apple_Driver43 SCSI Manager 4.3 device driver
48 * Apple_MFS original Macintosh File System
49 * Apple_HFS Hierarchical File System (and +)
50 * Apple_HFSX HFS+ with case sensitivity and more
51 * Apple_UNIX_SVR2 UNIX file system (UFS?)
52 * Apple_PRODOS ProDOS file system
53 * Apple_Free unused space
55 * Apple_Void padding for iso9660
56 * Apple_Extra an unused partition map entry
66 * partition volume or partition
69 * * All space must be accounted for, except block 0 (driver block) and
70 * block 1-X (the partition map: i.e. lots of MacRawPartitions)
72 * * It's really hard to grow/shrink the number of MacRawPartition
73 * entries in the partition map, because the first partition starts
74 * immediately after the partition map. When we can move the start of
75 * HFS and ext2 partitions, this problem will disappear ;-)
78 #define MAC_PARTITION_MAGIC_1 0x5453 /* old */
79 #define MAC_PARTITION_MAGIC_2 0x504d
80 #define MAC_DISK_MAGIC 0x4552
82 #define MAC_STATUS_BOOTABLE 8 /* partition is bootable */
84 typedef struct _MacRawPartition MacRawPartition;
85 typedef struct _MacRawDisk MacRawDisk;
86 typedef struct _MacDeviceDriver MacDeviceDriver;
87 typedef struct _MacPartitionData MacPartitionData;
88 typedef struct _MacDiskData MacDiskData;
90 struct __attribute__ ((packed)) _MacRawPartition {
91 uint16_t signature; /* expected to be MAC_PARTITION_MAGIC */
93 uint32_t map_count; /* # blocks in partition map */
94 uint32_t start_block; /* absolute starting block # of partition */
95 uint32_t block_count; /* number of blocks in partition */
96 char name[32]; /* partition name */
97 char type[32]; /* string type description */
98 uint32_t data_start; /* rel block # of first data block */
99 uint32_t data_count; /* number of data blocks */
100 uint32_t status; /* partition status bits */
106 uint32_t boot_entry2;
108 char processor[16]; /* Contains 680x0, x=0,2,3,4; or empty */
113 /* Driver descriptor structure, in block 0 */
114 struct __attribute__ ((packed)) _MacRawDisk {
115 uint16_t signature; /* expected to be MAC_DRIVER_MAGIC */
116 uint16_t block_size; /* physical sector size */
117 uint32_t block_count; /* size of device in blocks */
118 uint16_t dev_type; /* reserved */
119 uint16_t dev_id; /* reserved */
120 uint32_t data; /* reserved */
121 uint16_t driver_count; /* # of driver descriptor entries */
122 uint8_t driverlist[488];/* info about available drivers */
123 uint16_t padding[3]; /* pad to 512 bytes */
126 struct __attribute__ ((packed)) _MacDeviceDriver {
127 uint32_t block; /* startblock in MacRawDisk->block_size units */
128 uint16_t size; /* size in 512 byte units */
129 uint16_t type; /* operating system type (MacOS = 1) */
132 struct _MacPartitionData {
133 char volume_name[33]; /* eg: "Games" */
134 char system_name[33]; /* eg: "Apple_Unix_SVR2" */
135 char processor_name[17];
145 PedSector data_region_length;
146 PedSector boot_region_length;
148 uint32_t boot_base_address;
149 uint32_t boot_entry_address;
150 uint32_t boot_checksum;
156 struct _MacDiskData {
157 int ghost_size; /* sectors per "driver" block */
158 int part_map_entry_count; /* # entries (incl. ghost) */
159 int part_map_entry_num; /* partition map location */
161 int active_part_entry_count; /* # real partitions */
162 int free_part_entry_count; /* # free space */
163 int last_part_entry_num; /* last entry number */
165 uint16_t block_size; /* physical sector size */
166 uint16_t driver_count;
167 MacDeviceDriver driverlist[1 + 60]; /* 488 bytes */
170 static PedDiskType mac_disk_type;
173 _check_signature (MacRawDisk* raw_disk)
175 if (PED_BE16_TO_CPU (raw_disk->signature) != MAC_DISK_MAGIC) {
179 return ped_exception_throw (
181 PED_EXCEPTION_IGNORE_CANCEL,
182 _("Invalid signature %x for Mac disk labels."),
183 (int) PED_BE16_TO_CPU (raw_disk->signature))
184 == PED_EXCEPTION_IGNORE;
192 _rawpart_check_signature (MacRawPartition* raw_part)
194 int sig = (int) PED_BE16_TO_CPU (raw_part->signature);
195 return sig == MAC_PARTITION_MAGIC_1 || sig == MAC_PARTITION_MAGIC_2;
199 mac_probe (const PedDevice * dev)
203 PED_ASSERT (dev != NULL, return 0);
205 if (dev->sector_size != 512)
208 if (!ped_device_read (dev, &buf, 0, 1))
211 return _check_signature (&buf);
215 _disk_add_part_map_entry (PedDisk* disk, int warn)
217 MacDiskData* mac_disk_data = disk->disk_specific;
218 PedPartition* new_part;
219 MacPartitionData* mac_part_data;
220 PedSector part_map_size;
221 PedConstraint* constraint_any = ped_constraint_any (disk->dev);
223 #ifndef DISCOVER_ONLY
224 if (warn && ped_exception_throw (
226 PED_EXCEPTION_FIX | PED_EXCEPTION_CANCEL,
227 _("Partition map has no partition map entry!"))
228 != PED_EXCEPTION_FIX)
230 #endif /* !DISCOVER_ONLY */
233 = ped_round_up_to (mac_disk_data->last_part_entry_num, 64);
234 if (part_map_size == 0)
237 new_part = ped_partition_new (disk, PED_PARTITION_NORMAL, NULL,
238 1, part_map_size - 1);
242 mac_part_data = new_part->disk_specific;
243 strcpy (mac_part_data->volume_name, "Apple");
244 strcpy (mac_part_data->system_name, "Apple_partition_map");
246 if (!ped_disk_add_partition (disk, new_part, constraint_any))
247 goto error_destroy_new_part;
249 mac_disk_data->part_map_entry_num = new_part->num;
250 mac_disk_data->part_map_entry_count
251 = new_part->geom.end - mac_disk_data->ghost_size;
252 ped_constraint_destroy (constraint_any);
255 error_destroy_new_part:
256 ped_partition_destroy (new_part);
258 ped_constraint_destroy (constraint_any);
263 mac_alloc (const PedDevice* dev)
266 MacDiskData* mac_disk_data;
268 PED_ASSERT (dev != NULL, return NULL);
270 #ifndef DISCOVER_ONLY
271 if (dev->length < 256) {
272 ped_exception_throw (
274 PED_EXCEPTION_CANCEL,
275 _("%s is too small for a Mac disk label!"),
281 disk = _ped_disk_alloc (dev, &mac_disk_type);
285 mac_disk_data = (MacDiskData*) ped_malloc (sizeof (MacDiskData));
287 goto error_free_disk;
288 disk->disk_specific = mac_disk_data;
289 mac_disk_data->ghost_size = disk->dev->sector_size / 512;
290 mac_disk_data->active_part_entry_count = 0;
291 mac_disk_data->free_part_entry_count = 1;
292 mac_disk_data->last_part_entry_num = 1;
293 mac_disk_data->block_size = 0;
294 mac_disk_data->driver_count = 0;
295 memset(&mac_disk_data->driverlist[0], 0, sizeof(mac_disk_data->driverlist));
297 if (!_disk_add_part_map_entry (disk, 0))
298 goto error_free_disk;
302 _ped_disk_free (disk);
308 mac_duplicate (const PedDisk* disk)
311 MacDiskData* new_mac_data;
312 MacDiskData* old_mac_data = (MacDiskData*) disk->disk_specific;
313 PedPartition* partition_map;
315 new_disk = ped_disk_new_fresh (disk->dev, &mac_disk_type);
319 new_mac_data = (MacDiskData*) new_disk->disk_specific;
321 /* remove the partition map partition - it will be duplicated
324 partition_map = ped_disk_get_partition_by_sector (new_disk, 1);
325 PED_ASSERT (partition_map != NULL, return 0);
326 ped_disk_remove_partition (new_disk, partition_map);
328 /* ugly, but C is ugly :p */
329 memcpy (new_mac_data, old_mac_data, sizeof (MacDiskData));
332 _ped_disk_free (new_disk);
338 mac_free (PedDisk* disk)
340 MacDiskData* mac_disk_data = disk->disk_specific;
342 _ped_disk_free (disk);
343 free (mac_disk_data);
346 #ifndef DISCOVER_ONLY
348 _clobber_part_map (PedDevice* dev)
350 MacRawPartition raw_part;
353 for (sector=1; 1; sector++) {
354 if (!ped_device_read (dev, &raw_part, sector, 1))
356 if (!_rawpart_check_signature (&raw_part))
358 memset (&raw_part, 0, 512);
359 if (!ped_device_write (dev, &raw_part, sector, 1))
365 mac_clobber (PedDevice* dev)
369 if (!ped_device_read (dev, &raw_disk, 0, 1))
371 if (!_check_signature (&raw_disk))
373 memset (&raw_disk, 0, 512);
374 if (!ped_device_write (dev, &raw_disk, 0, 1))
377 return _clobber_part_map (dev);
379 #endif /* !DISCOVER_ONLY */
382 _rawpart_cmp_type (const MacRawPartition* raw_part, const char* type)
384 return strncasecmp (raw_part->type, type, 32) == 0;
388 _rawpart_cmp_name (const MacRawPartition* raw_part, const char* name)
390 return strncasecmp (raw_part->name, name, 32) == 0;
394 _rawpart_is_partition_map (const MacRawPartition* raw_part)
396 return _rawpart_cmp_type (raw_part, "Apple_partition_map");
400 strncasestr (const char* haystack, const char* needle, int n)
402 int needle_size = strlen (needle);
405 for (i = 0; haystack[i] && i < n - needle_size; i++) {
406 if (strncasecmp (haystack + i, needle, needle_size) == 0)
414 _rawpart_is_boot (const MacRawPartition* raw_part)
416 if (!strcasecmp(raw_part->type, "Apple_Bootstrap"))
419 if (!strcasecmp(raw_part->type, "Apple_Boot"))
426 _rawpart_is_driver (const MacRawPartition* raw_part)
428 if (strncmp (raw_part->type, "Apple_", 6) != 0)
430 if (!strncasestr (raw_part->type, "driver", 32))
436 _rawpart_has_driver (const MacRawPartition* raw_part, MacDiskData* mac_disk_data)
438 MacDeviceDriver *driverlist;
440 uint32_t driver_bs, driver_be, part_be;
442 driverlist = &mac_disk_data->driverlist[0];
443 bsz = mac_disk_data->block_size / 512;
444 for (i = 0; i < mac_disk_data->driver_count; i++) {
445 driver_bs = driverlist->block * bsz;
446 driver_be = driver_bs + driverlist->size;
447 part_be = raw_part->start_block + raw_part->block_count;
448 if (driver_bs >= raw_part->start_block && driver_be <= part_be)
456 _rawpart_is_root (MacRawPartition* raw_part)
458 if (!_rawpart_cmp_type (raw_part, "Apple_UNIX_SVR2"))
460 if (strcmp (raw_part->name, "root") != 0)
466 _rawpart_is_swap (MacRawPartition* raw_part)
468 if (!_rawpart_cmp_type (raw_part, "Apple_UNIX_SVR2"))
470 if (strcmp (raw_part->name, "swap") != 0)
476 _rawpart_is_lvm (MacRawPartition* raw_part)
478 if (strcmp (raw_part->type, "Linux_LVM") != 0)
484 _rawpart_is_raid (MacRawPartition* raw_part)
486 if (strcmp (raw_part->type, "Linux_RAID") != 0)
492 _rawpart_is_void (MacRawPartition* raw_part)
494 return _rawpart_cmp_type (raw_part, "Apple_Void");
497 /* returns 1 if the raw_part represents a partition that is "unused space", or
498 * doesn't represent a partition at all. NOTE: some people make Apple_Free
499 * partitions with MacOS, because they can't select another type. So, if the
500 * name is anything other than "Extra" or "", it is treated as a "real"
504 _rawpart_is_active (MacRawPartition* raw_part)
506 if (_rawpart_cmp_type (raw_part, "Apple_Free")
507 && (_rawpart_cmp_name (raw_part, "Extra")
508 || _rawpart_cmp_name (raw_part, "")))
510 if (_rawpart_cmp_type (raw_part, "Apple_Void"))
512 if (_rawpart_cmp_type (raw_part, "Apple_Scratch"))
514 if (_rawpart_cmp_type (raw_part, "Apple_Extra"))
521 _rawpart_analyse (MacRawPartition* raw_part, PedDisk* disk, int num)
523 MacDiskData* mac_disk_data;
525 MacPartitionData* mac_part_data;
526 PedSector block_size;
527 PedSector start, length;
529 if (!_rawpart_check_signature (raw_part)) {
530 #ifndef DISCOVER_ONLY
531 if (ped_exception_throw (
532 PED_EXCEPTION_WARNING,
533 PED_EXCEPTION_IGNORE_CANCEL,
534 _("Partition %d has an invalid signature %x."),
536 (int) PED_BE16_TO_CPU (raw_part->signature))
537 != PED_EXCEPTION_IGNORE)
542 mac_disk_data = (MacDiskData*) disk->disk_specific;
543 block_size = disk->dev->sector_size / 512;
545 start = PED_BE32_TO_CPU (raw_part->start_block) * block_size;
546 length = PED_BE32_TO_CPU (raw_part->block_count) * block_size;
548 #ifndef DISCOVER_ONLY
549 ped_exception_throw (
551 PED_EXCEPTION_CANCEL,
552 _("Partition %d has an invalid length of 0 bytes!"),
557 part = ped_partition_new (disk, PED_PARTITION_NORMAL, NULL,
558 start, start + length - 1);
562 mac_part_data = part->disk_specific;
564 strncpy (mac_part_data->volume_name, raw_part->name, 32);
565 strncpy (mac_part_data->system_name, raw_part->type, 32);
566 strncpy (mac_part_data->processor_name, raw_part->processor, 16);
568 mac_part_data->is_boot = _rawpart_is_boot (raw_part);
569 mac_part_data->is_driver = _rawpart_is_driver (raw_part);
570 if (mac_part_data->is_driver)
571 mac_part_data->has_driver = _rawpart_has_driver(raw_part, mac_disk_data);
572 mac_part_data->is_root = _rawpart_is_root (raw_part);
573 mac_part_data->is_swap = _rawpart_is_swap (raw_part);
574 mac_part_data->is_lvm = _rawpart_is_lvm (raw_part);
575 mac_part_data->is_raid = _rawpart_is_raid (raw_part);
578 #ifndef DISCOVER_ONLY
579 if (raw_part->data_start) {
580 ped_exception_throw (
582 PED_EXCEPTION_CANCEL,
583 _("The data region doesn't start at the start "
584 "of the partition."));
585 goto error_destroy_part;
587 #endif /* !DISCOVER_ONLY */
588 mac_part_data->data_region_length
589 = PED_BE32_TO_CPU (raw_part->data_count) * block_size;
591 /* boot region - we have no idea what this is for, but Mac OSX
592 * seems to put garbage here, and doesn't pay any attention to
593 * it afterwards. [clausen, dan burcaw]
596 if (raw_part->boot_start) {
597 ped_exception_throw (
599 PED_EXCEPTION_CANCEL,
600 _("The boot region doesn't start at the start "
601 "of the partition."));
602 goto error_destroy_part;
605 mac_part_data->boot_region_length
606 = PED_BE32_TO_CPU (raw_part->boot_count) * block_size;
608 #ifndef DISCOVER_ONLY
609 if (mac_part_data->has_driver) {
610 if (mac_part_data->boot_region_length < part->geom.length) {
611 if (ped_exception_throw (
613 PED_EXCEPTION_IGNORE_CANCEL,
614 _("The partition's boot region doesn't occupy "
615 "the entire partition."))
616 != PED_EXCEPTION_IGNORE)
617 goto error_destroy_part;
620 if (mac_part_data->data_region_length < part->geom.length &&
621 !mac_part_data->is_boot) {
622 if (ped_exception_throw (
624 PED_EXCEPTION_IGNORE_CANCEL,
625 _("The partition's data region doesn't occupy "
626 "the entire partition."))
627 != PED_EXCEPTION_IGNORE)
628 goto error_destroy_part;
631 #endif /* !DISCOVER_ONLY */
633 mac_part_data->boot_base_address
634 = PED_BE32_TO_CPU (raw_part->boot_load);
635 mac_part_data->boot_entry_address
636 = PED_BE32_TO_CPU (raw_part->boot_entry);
637 mac_part_data->boot_checksum
638 = PED_BE32_TO_CPU (raw_part->boot_cksum);
640 mac_part_data->status = PED_BE32_TO_CPU (raw_part->status);
641 mac_part_data->driver_sig = PED_BE32_TO_CPU (raw_part->driver_sig);
646 ped_partition_destroy (part);
651 /* looks at the partition map size field in a mac raw partition, and calculates
652 * what the size of the partition map should be, from it
655 _rawpart_get_partmap_size (MacRawPartition* raw_part, PedDisk* disk)
657 MacDiskData* mac_disk_data = disk->disk_specific;
658 PedSector sector_size = disk->dev->sector_size / 512;
659 PedSector part_map_start;
660 PedSector part_map_end;
662 part_map_start = mac_disk_data->ghost_size;
663 part_map_end = sector_size * PED_BE32_TO_CPU (raw_part->map_count);
665 return part_map_end - part_map_start + 1;
669 _disk_analyse_block_size (PedDisk* disk, MacRawDisk* raw_disk)
671 PedSector block_size;
673 if (PED_BE16_TO_CPU (raw_disk->block_size) % 512) {
674 #ifndef DISCOVER_ONLY
675 ped_exception_throw (
677 PED_EXCEPTION_CANCEL,
678 _("Weird block size on device descriptor: %d bytes is "
679 "not divisible by 512."),
680 (int) PED_BE16_TO_CPU (raw_disk->block_size));
685 block_size = PED_BE16_TO_CPU (raw_disk->block_size) / 512;
686 if (block_size != disk->dev->sector_size / 512) {
687 #ifndef DISCOVER_ONLY
688 if (ped_exception_throw (
689 PED_EXCEPTION_WARNING,
690 PED_EXCEPTION_IGNORE_CANCEL,
691 _("The driver descriptor says the physical block size "
692 "is %d bytes, but Linux says it is %d bytes."),
693 (int) block_size * 512,
694 (int) disk->dev->sector_size)
695 != PED_EXCEPTION_IGNORE)
698 disk->dev->sector_size = block_size * 512;
707 /* Tries to figure out the block size used by the drivers, for the ghost
708 * partitioning scheme. Ghost partitioning works like this: the OpenFirmware
709 * (OF) sees 512 byte blocks, but some drivers use 2048 byte blocks (and,
710 * perhaps, some other number?). To remain compatible, the partition map
711 * only has "real" partition map entries on ghost-aligned block numbers (and
712 * the others are padded with Apple_Void partitions). This function tries
713 * to figure out what the "ghost-aligned" size is... (which, believe-it-or-not,
714 * doesn't always equal 2048!!!)
717 _disk_analyse_ghost_size (PedDisk* disk)
719 MacDiskData* mac_disk_data = disk->disk_specific;
720 MacRawPartition raw_part;
723 for (i = 1; i < 64; i *= 2) {
724 if (!ped_device_read (disk->dev, &raw_part, i, 1))
726 if (_rawpart_check_signature (&raw_part)
727 && !_rawpart_is_void (&raw_part)) {
728 mac_disk_data->ghost_size = i;
729 PED_ASSERT (i <= disk->dev->sector_size / 512,
735 #ifndef DISCOVER_ONLY
736 ped_exception_throw (
738 PED_EXCEPTION_CANCEL,
739 _("No valid partition map found."));
745 mac_read (PedDisk* disk)
748 MacRawPartition raw_part;
749 MacDiskData* mac_disk_data;
752 PedSector ghost_size;
753 PedConstraint* constraint_exact;
754 int last_part_entry_num = 0;
756 PED_ASSERT (disk != NULL, return 0);
758 mac_disk_data = disk->disk_specific;
759 mac_disk_data->part_map_entry_num = 0; /* 0 == none */
761 if (!ped_device_read (disk->dev, &raw_disk, 0, 1))
763 if (!_check_signature (&raw_disk))
766 if (!_disk_analyse_block_size (disk, &raw_disk))
768 if (!_disk_analyse_ghost_size (disk))
770 ghost_size = mac_disk_data->ghost_size;
772 if (!ped_disk_delete_all (disk))
775 if (raw_disk.driver_count && raw_disk.driver_count < 62) {
776 memcpy(&mac_disk_data->driverlist[0], &raw_disk.driverlist[0],
777 sizeof(mac_disk_data->driverlist));
778 mac_disk_data->driver_count = raw_disk.driver_count;
779 mac_disk_data->block_size = raw_disk.block_size;
782 for (num=1; num==1 || num <= last_part_entry_num; num++) {
783 if (!ped_device_read (disk->dev, &raw_part,
784 num * ghost_size, 1))
785 goto error_delete_all;
787 if (!_rawpart_check_signature (&raw_part))
792 = _rawpart_get_partmap_size (&raw_part, disk);
793 if (_rawpart_get_partmap_size (&raw_part, disk)
794 != last_part_entry_num) {
795 if (ped_exception_throw (
797 PED_EXCEPTION_IGNORE_CANCEL,
798 _("Conflicting partition map entry sizes! "
799 "Entry 1 says it is %d, but entry %d says "
802 _rawpart_get_partmap_size (&raw_part, disk))
803 != PED_EXCEPTION_IGNORE)
804 goto error_delete_all;
807 if (!_rawpart_is_active (&raw_part))
810 part = _rawpart_analyse (&raw_part, disk, num);
812 goto error_delete_all;
814 part->fs_type = ped_file_system_probe (&part->geom);
815 constraint_exact = ped_constraint_exact (&part->geom);
816 if (!ped_disk_add_partition (disk, part, constraint_exact))
817 goto error_delete_all;
818 ped_constraint_destroy (constraint_exact);
820 if (_rawpart_is_partition_map (&raw_part)) {
821 if (mac_disk_data->part_map_entry_num
822 && ped_exception_throw (
824 PED_EXCEPTION_IGNORE_CANCEL,
825 _("Weird! There are 2 partitions "
827 != PED_EXCEPTION_IGNORE)
828 goto error_delete_all;
830 mac_disk_data->part_map_entry_num = num;
831 mac_disk_data->part_map_entry_count
832 = part->geom.end - ghost_size + 1;
836 if (!mac_disk_data->part_map_entry_num) {
837 if (!_disk_add_part_map_entry (disk, 1))
838 goto error_delete_all;
839 ped_disk_commit_to_dev (disk);
844 ped_disk_delete_all (disk);
849 #ifndef DISCOVER_ONLY
850 /* The Ghost partition: is a blank entry, used to pad out each block (where
851 * there physical block size > 512 bytes). This is because OpenFirmware uses
852 * 512 byte blocks, but device drivers Think Different TM, with a different
853 * lbock size, so we need to do this to avoid a clash (!)
856 _pad_raw_part (PedDisk* disk, int num, MacRawPartition* part_map)
858 MacDiskData* mac_disk_data = disk->disk_specific;
859 MacRawPartition ghost_entry;
862 memset (&ghost_entry, 0, sizeof (ghost_entry));
863 ghost_entry.signature = PED_CPU_TO_BE16 (MAC_PARTITION_MAGIC_2);
864 strcpy (ghost_entry.type, "Apple_Void");
865 ghost_entry.map_count
866 = PED_CPU_TO_BE32 (mac_disk_data->last_part_entry_num);
868 for (i=0; i < mac_disk_data->ghost_size - 1; i++)
869 memcpy (&part_map [i + (num - 1) * mac_disk_data->ghost_size],
870 &ghost_entry, sizeof (MacRawPartition));
876 _update_driver_count (MacRawPartition* part_map_entry,
877 MacDiskData *mac_driverdata, const MacDiskData* mac_disk_data)
879 uint16_t i, count_orig, count_cur, bsz;
880 uint32_t driver_bs, driver_be, part_be;
882 bsz = mac_disk_data->block_size / 512;
883 count_cur = mac_driverdata->driver_count;
884 count_orig = mac_disk_data->driver_count;
885 for (i = 0; i < count_orig; i++) {
886 driver_bs = mac_disk_data->driverlist[i].block * bsz;
887 driver_be = driver_bs + mac_disk_data->driverlist[i].size;
888 part_be = part_map_entry->start_block + part_map_entry->block_count;
889 if (driver_bs >= part_map_entry->start_block
890 && driver_be <= part_be) {
891 mac_driverdata->driverlist[count_cur].block
892 = mac_disk_data->driverlist[i].block;
893 mac_driverdata->driverlist[count_cur].size
894 = mac_disk_data->driverlist[i].size;
895 mac_driverdata->driverlist[count_cur].type
896 = mac_disk_data->driverlist[i].type;
897 mac_driverdata->driver_count++;
904 _generate_raw_part (PedDisk* disk, PedPartition* part,
905 MacRawPartition* part_map, MacDiskData *mac_driverdata)
907 MacDiskData* mac_disk_data;
908 MacPartitionData* mac_part_data;
909 MacRawPartition* part_map_entry;
910 PedSector block_size = disk->dev->sector_size / 512;
912 PED_ASSERT (part->num > 0, goto error);
914 mac_disk_data = disk->disk_specific;
915 mac_part_data = part->disk_specific;
917 part_map_entry = &part_map [part->num * mac_disk_data->ghost_size - 1];
919 part_map_entry->signature = PED_CPU_TO_BE16 (MAC_PARTITION_MAGIC_2);
920 part_map_entry->map_count
921 = PED_CPU_TO_BE32 (mac_disk_data->last_part_entry_num);
922 part_map_entry->start_block
923 = PED_CPU_TO_BE32 (part->geom.start / block_size);
924 part_map_entry->block_count
925 = PED_CPU_TO_BE32 (part->geom.length / block_size);
926 strcpy (part_map_entry->name, mac_part_data->volume_name);
927 strcpy (part_map_entry->type, mac_part_data->system_name);
929 if (mac_part_data->is_driver) {
930 mac_part_data->boot_region_length = part->geom.length;
931 if (mac_part_data->has_driver)
932 _update_driver_count(part_map_entry, mac_driverdata,
935 mac_part_data->data_region_length = part->geom.length;
936 part_map_entry->data_count = PED_CPU_TO_BE32 (
937 mac_part_data->data_region_length / block_size);
938 part_map_entry->boot_count = PED_CPU_TO_BE32 (
939 mac_part_data->boot_region_length / block_size);
940 part_map_entry->status = PED_CPU_TO_BE32 (mac_part_data->status);
941 part_map_entry->driver_sig
942 = PED_CPU_TO_BE32 (mac_part_data->driver_sig);
944 part_map_entry->boot_load =
945 PED_CPU_TO_BE32 (mac_part_data->boot_base_address);
946 part_map_entry->boot_entry =
947 PED_CPU_TO_BE32 (mac_part_data->boot_entry_address);
948 part_map_entry->boot_cksum =
949 PED_CPU_TO_BE32 (mac_part_data->boot_checksum);
951 strncpy (part_map_entry->processor, mac_part_data->processor_name, 16);
953 if (!_pad_raw_part (disk, part->num, part_map))
963 _generate_raw_freespace_part (PedDisk* disk, PedGeometry* geom, int num,
964 MacRawPartition* part_map)
966 MacDiskData* mac_disk_data = disk->disk_specific;
967 MacRawPartition* part_map_entry;
968 PedSector block_size = disk->dev->sector_size / 512;
970 PED_ASSERT (num > 0, goto error);
972 part_map_entry = &part_map [num * mac_disk_data->ghost_size - 1];
974 part_map_entry->signature = PED_CPU_TO_BE16 (MAC_PARTITION_MAGIC_2);
975 part_map_entry->map_count
976 = PED_CPU_TO_BE32 (mac_disk_data->last_part_entry_num);
977 part_map_entry->start_block
978 = PED_CPU_TO_BE32 (geom->start / block_size);
979 part_map_entry->block_count
980 = PED_CPU_TO_BE32 (geom->length / block_size);
981 strcpy (part_map_entry->name, "Extra");
982 strcpy (part_map_entry->type, "Apple_Free");
984 part_map_entry->data_count = PED_CPU_TO_BE32 (geom->length);
985 part_map_entry->status = 0;
986 part_map_entry->driver_sig = 0;
988 if (!_pad_raw_part (disk, num, part_map))
998 _generate_empty_part (PedDisk* disk, int num, MacRawPartition* part_map)
1000 MacDiskData* mac_disk_data = disk->disk_specific;
1001 MacRawPartition* part_map_entry;
1003 PED_ASSERT (num > 0, return 0);
1005 part_map_entry = &part_map [num * mac_disk_data->ghost_size - 1];
1006 part_map_entry->signature = PED_CPU_TO_BE16 (MAC_PARTITION_MAGIC_2);
1007 part_map_entry->map_count
1008 = PED_CPU_TO_BE32 (mac_disk_data->last_part_entry_num);
1009 strcpy (part_map_entry->type, "Apple_Void");
1011 return _pad_raw_part (disk, num, part_map);
1014 /* returns the first empty entry in the partition map */
1016 _get_first_empty_part_entry (PedDisk* disk, MacRawPartition* part_map)
1018 MacDiskData* mac_disk_data = disk->disk_specific;
1021 for (i=1; i <= mac_disk_data->last_part_entry_num; i++) {
1022 if (!part_map[i * mac_disk_data->ghost_size - 1].signature)
1030 write_block_zero (PedDisk* disk, MacDiskData* mac_driverdata)
1032 PedDevice* dev = disk->dev;
1033 MacRawDisk raw_disk;
1035 if (!ped_device_read (dev, &raw_disk, 0, 1))
1038 raw_disk.signature = PED_CPU_TO_BE16 (MAC_DISK_MAGIC);
1039 raw_disk.block_size = PED_CPU_TO_BE16 (dev->sector_size);
1040 raw_disk.block_count
1041 = PED_CPU_TO_BE32 (dev->length / (dev->sector_size / 512));
1043 raw_disk.driver_count = mac_driverdata->driver_count;
1044 memcpy(&raw_disk.driverlist[0], &mac_driverdata->driverlist[0],
1045 sizeof(raw_disk.driverlist));
1047 return ped_device_write (dev, &raw_disk, 0, 1);
1051 mac_write (PedDisk* disk)
1053 MacRawPartition* part_map;
1054 MacDiskData* mac_disk_data;
1055 MacDiskData* mac_driverdata; /* updated driver list */
1059 PED_ASSERT (disk != NULL, return 0);
1060 PED_ASSERT (disk->disk_specific != NULL, return 0);
1061 PED_ASSERT (disk->dev != NULL, return 0);
1062 PED_ASSERT (!disk->update_mode, return 0);
1064 mac_disk_data = disk->disk_specific;
1066 if (!ped_disk_get_partition (disk, mac_disk_data->part_map_entry_num)) {
1067 if (!_disk_add_part_map_entry (disk, 1))
1071 mac_driverdata = ped_malloc(sizeof(MacDiskData));
1072 if (!mac_driverdata)
1074 memset (mac_driverdata, 0, sizeof(MacDiskData));
1076 part_map = (MacRawPartition*)
1077 ped_malloc (mac_disk_data->part_map_entry_count * 512);
1079 goto error_free_driverdata;
1080 memset (part_map, 0, mac_disk_data->part_map_entry_count * 512);
1082 /* write (to memory) the "real" partitions */
1083 for (part = ped_disk_next_partition (disk, NULL); part;
1084 part = ped_disk_next_partition (disk, part)) {
1085 if (!ped_partition_is_active (part))
1087 if (!_generate_raw_part (disk, part, part_map, mac_driverdata))
1088 goto error_free_part_map;
1091 /* write the "free space" partitions */
1092 for (part = ped_disk_next_partition (disk, NULL); part;
1093 part = ped_disk_next_partition (disk, part)) {
1094 if (part->type != PED_PARTITION_FREESPACE)
1096 num = _get_first_empty_part_entry (disk, part_map);
1097 if (!_generate_raw_freespace_part (disk, &part->geom, num,
1099 goto error_free_part_map;
1102 /* write the "void" (empty) partitions */
1103 for (num = _get_first_empty_part_entry (disk, part_map); num;
1104 num = _get_first_empty_part_entry (disk, part_map))
1105 _generate_empty_part (disk, num, part_map);
1108 if (!ped_device_write (disk->dev, part_map, 1,
1109 mac_disk_data->part_map_entry_count))
1110 goto error_free_part_map;
1112 return write_block_zero (disk, mac_driverdata);
1114 error_free_part_map:
1116 error_free_driverdata:
1117 free (mac_driverdata);
1121 #endif /* !DISCOVER_ONLY */
1123 static PedPartition*
1125 const PedDisk* disk, PedPartitionType part_type,
1126 const PedFileSystemType* fs_type, PedSector start, PedSector end)
1129 MacPartitionData* mac_data;
1131 part = _ped_partition_alloc (disk, part_type, fs_type, start, end);
1135 if (ped_partition_is_active (part)) {
1137 = mac_data = ped_malloc (sizeof (MacPartitionData));
1139 goto error_free_part;
1141 memset (mac_data, 0, sizeof (MacPartitionData));
1142 strcpy (mac_data->volume_name, "untitled");
1144 part->disk_specific = NULL;
1155 static PedPartition*
1156 mac_partition_duplicate (const PedPartition* part)
1158 PedPartition* new_part;
1159 MacPartitionData* new_mac_data;
1160 MacPartitionData* old_mac_data;
1162 new_part = ped_partition_new (part->disk, part->type,
1163 part->fs_type, part->geom.start,
1167 new_part->num = part->num;
1169 old_mac_data = (MacPartitionData*) part->disk_specific;
1170 new_mac_data = (MacPartitionData*) new_part->disk_specific;
1172 /* ugly, but C is ugly :p */
1173 memcpy (new_mac_data, old_mac_data, sizeof (MacPartitionData));
1178 mac_partition_destroy (PedPartition* part)
1180 PED_ASSERT (part != NULL, return);
1182 if (ped_partition_is_active (part))
1183 free (part->disk_specific);
1188 mac_partition_set_system (PedPartition* part, const PedFileSystemType* fs_type)
1190 MacPartitionData* mac_data = part->disk_specific;
1192 part->fs_type = fs_type;
1194 if (fs_type && is_linux_swap (fs_type->name))
1195 ped_partition_set_flag (part, PED_PARTITION_SWAP, 1);
1197 if (mac_data->is_boot) {
1198 strcpy (mac_data->system_name, "Apple_Bootstrap");
1199 mac_data->status = 0x33;
1203 if (fs_type && (!strcmp (fs_type->name, "hfs")
1204 || !strcmp (fs_type->name, "hfs+"))) {
1205 strcpy (mac_data->system_name, "Apple_HFS");
1206 mac_data->status |= 0x7f;
1207 } else if (fs_type && !strcmp (fs_type->name, "hfsx")) {
1208 strcpy (mac_data->system_name, "Apple_HFSX");
1209 mac_data->status |= 0x7f;
1211 strcpy (mac_data->system_name, "Apple_UNIX_SVR2");
1212 mac_data->status = 0x33;
1219 mac_partition_set_flag (PedPartition* part, PedPartitionFlag flag, int state)
1221 MacPartitionData* mac_data;
1223 PED_ASSERT (part != NULL, return 0);
1224 PED_ASSERT (part->disk_specific != NULL, return 0);
1226 mac_data = part->disk_specific;
1229 case PED_PARTITION_BOOT:
1230 mac_data->is_boot = state;
1233 return mac_partition_set_system (part, part->fs_type);
1236 strcpy (mac_data->system_name, "Apple_Bootstrap");
1237 mac_data->status = 0x33;
1241 case PED_PARTITION_ROOT:
1243 strcpy (mac_data->volume_name, "root");
1244 mac_data->is_swap = 0;
1246 if (mac_data->is_root)
1247 strcpy (mac_data->volume_name, "untitled");
1249 mac_data->is_root = state;
1252 case PED_PARTITION_SWAP:
1254 strcpy (mac_data->volume_name, "swap");
1255 mac_data->is_root = 0;
1257 if (mac_data->is_swap)
1258 strcpy (mac_data->volume_name, "untitled");
1260 mac_data->is_swap = state;
1263 case PED_PARTITION_LVM:
1265 strcpy (mac_data->system_name, "Linux_LVM");
1266 mac_data->is_lvm = state;
1268 if (mac_data->is_lvm)
1269 mac_partition_set_system (part, part->fs_type);
1273 case PED_PARTITION_RAID:
1275 strcpy (mac_data->system_name, "Linux_RAID");
1276 mac_data->is_raid = state;
1278 if (mac_data->is_raid)
1279 mac_partition_set_system (part, part->fs_type);
1289 mac_partition_get_flag (const PedPartition* part, PedPartitionFlag flag)
1291 MacPartitionData* mac_data;
1293 PED_ASSERT (part != NULL, return 0);
1294 PED_ASSERT (part->disk_specific != NULL, return 0);
1296 mac_data = part->disk_specific;
1298 case PED_PARTITION_BOOT:
1299 return mac_data->is_boot;
1301 case PED_PARTITION_ROOT:
1302 return mac_data->is_root;
1304 case PED_PARTITION_SWAP:
1305 return mac_data->is_swap;
1307 case PED_PARTITION_LVM:
1308 return mac_data->is_lvm;
1310 case PED_PARTITION_RAID:
1311 return mac_data->is_raid;
1319 mac_partition_is_flag_available (
1320 const PedPartition* part, PedPartitionFlag flag)
1323 case PED_PARTITION_BOOT:
1324 case PED_PARTITION_ROOT:
1325 case PED_PARTITION_SWAP:
1326 case PED_PARTITION_LVM:
1327 case PED_PARTITION_RAID:
1336 mac_partition_set_name (PedPartition* part, const char* name)
1338 MacPartitionData* mac_data;
1341 PED_ASSERT (part != NULL, return);
1342 PED_ASSERT (part->disk_specific != NULL, return);
1343 mac_data = part->disk_specific;
1345 #ifndef DISCOVER_ONLY
1346 if (mac_data->is_root || mac_data->is_swap) {
1347 if (ped_exception_throw (
1348 PED_EXCEPTION_WARNING,
1349 PED_EXCEPTION_IGNORE_CANCEL,
1350 _("Changing the name of a root or swap partition "
1351 "will prevent Linux from recognising it as such."))
1352 != PED_EXCEPTION_IGNORE)
1354 mac_data->is_root = mac_data->is_swap = 0;
1358 strncpy (mac_data->volume_name, name, 32);
1359 mac_data->volume_name [32] = 0;
1360 for (i = strlen (mac_data->volume_name) - 1;
1361 mac_data->volume_name[i] == ' '; i--)
1362 mac_data->volume_name [i] = 0;
1366 mac_partition_get_name (const PedPartition* part)
1368 MacPartitionData* mac_data;
1370 PED_ASSERT (part != NULL, return NULL);
1371 PED_ASSERT (part->disk_specific != NULL, return NULL);
1372 mac_data = part->disk_specific;
1374 return mac_data->volume_name;
1377 static PedConstraint*
1378 _primary_constraint (PedDisk* disk)
1380 PedAlignment start_align;
1381 PedAlignment end_align;
1382 PedGeometry max_geom;
1383 PedSector sector_size;
1385 sector_size = disk->dev->sector_size / 512;
1387 if (!ped_alignment_init (&start_align, 0, sector_size))
1389 if (!ped_alignment_init (&end_align, -1, sector_size))
1391 if (!ped_geometry_init (&max_geom, disk->dev, 1, disk->dev->length - 1))
1394 return ped_constraint_new (&start_align, &end_align, &max_geom,
1395 &max_geom, 1, disk->dev->length);
1399 mac_partition_align (PedPartition* part, const PedConstraint* constraint)
1401 PED_ASSERT (part != NULL, return 0);
1403 if (_ped_partition_attempt_align (part, constraint,
1404 _primary_constraint (part->disk)))
1407 #ifndef DISCOVER_ONLY
1408 ped_exception_throw (
1409 PED_EXCEPTION_ERROR,
1410 PED_EXCEPTION_CANCEL,
1411 _("Unable to satisfy all constraints on the partition."));
1417 mac_partition_enumerate (PedPartition* part)
1420 MacDiskData* mac_disk_data;
1424 PED_ASSERT (part != NULL, return 0);
1425 PED_ASSERT (part->disk != NULL, return 0);
1428 mac_disk_data = (MacDiskData*) disk->disk_specific;
1430 max_part_count = ped_disk_get_max_primary_partition_count (disk);
1432 if (part->num > 0 && part->num <= mac_disk_data->part_map_entry_count)
1435 for (i = 1; i <= max_part_count; i++) {
1436 if (!ped_disk_get_partition (disk, i)) {
1442 #ifndef DISCOVER_ONLY
1443 ped_exception_throw (
1444 PED_EXCEPTION_ERROR,
1445 PED_EXCEPTION_CANCEL,
1446 _("Can't add another partition -- the partition map is too "
1454 _disk_count_partitions (PedDisk* disk)
1456 MacDiskData* mac_disk_data = disk->disk_specific;
1457 PedPartition* part = NULL;
1458 PedPartition* last = NULL;
1460 PED_ASSERT (disk->update_mode, return 0);
1462 mac_disk_data->active_part_entry_count = 0;
1463 mac_disk_data->free_part_entry_count = 0;
1464 mac_disk_data->last_part_entry_num = 0;
1466 /* subtle: we only care about free space after the partition map.
1467 * the partition map is an "active" partition, BTW... */
1468 for (part = ped_disk_next_partition (disk, part); part;
1469 part = ped_disk_next_partition (disk, part)) {
1470 if (!ped_partition_is_active (part))
1473 mac_disk_data->active_part_entry_count++;
1474 if (last && last->geom.end + 1 < part->geom.start)
1475 mac_disk_data->free_part_entry_count++;
1476 mac_disk_data->last_part_entry_num
1477 = PED_MAX (mac_disk_data->last_part_entry_num,
1483 if (last && last->geom.end < disk->dev->length - 1)
1484 mac_disk_data->free_part_entry_count++;
1486 mac_disk_data->last_part_entry_num
1487 = PED_MAX (mac_disk_data->last_part_entry_num,
1488 mac_disk_data->active_part_entry_count
1489 + mac_disk_data->free_part_entry_count);
1494 add_metadata_part (PedDisk* disk, PedSector start, PedSector end)
1496 PedPartition* new_part;
1497 PedConstraint* constraint_any = ped_constraint_any (disk->dev);
1499 PED_ASSERT (disk != NULL, return 0);
1501 new_part = ped_partition_new (disk, PED_PARTITION_METADATA, NULL,
1505 if (!ped_disk_add_partition (disk, new_part, constraint_any))
1506 goto error_destroy_new_part;
1508 ped_constraint_destroy (constraint_any);
1511 error_destroy_new_part:
1512 ped_partition_destroy (new_part);
1514 ped_constraint_destroy (constraint_any);
1519 mac_alloc_metadata (PedDisk* disk)
1521 MacDiskData* mac_disk_data;
1523 PED_ASSERT (disk != NULL, return 0);
1524 PED_ASSERT (disk->disk_specific != NULL, return 0);
1525 PED_ASSERT (disk->dev != NULL, return 0);
1527 mac_disk_data = disk->disk_specific;
1529 if (!add_metadata_part (disk, 0, disk->dev->sector_size / 512 - 1))
1532 /* hack: this seems to be a good place, to update the partition map
1533 * entry count, since mac_alloc_metadata() gets called during
1534 * _disk_pop_update_mode()
1536 return _disk_count_partitions (disk);
1540 mac_get_max_primary_partition_count (const PedDisk* disk)
1542 MacDiskData* mac_disk_data = disk->disk_specific;
1543 PedPartition* part_map_partition;
1545 part_map_partition = ped_disk_get_partition (disk,
1546 mac_disk_data->part_map_entry_num);
1548 /* HACK: if we haven't found the partition map partition (yet),
1551 if (!part_map_partition) {
1552 mac_disk_data->part_map_entry_num = 0;
1556 /* HACK: since Mac labels need an entry for free-space regions, we
1557 * must allow half plus 1 entries for free-space partitions. I hate
1558 * this, but things get REALLY complicated, otherwise.
1559 * (I'm prepared to complicate things later, but I want to get
1560 * everything working, first)
1562 return mac_disk_data->part_map_entry_count / mac_disk_data->ghost_size
1563 - mac_disk_data->free_part_entry_count + 1;
1566 static PedDiskOps mac_disk_ops = {
1568 #ifndef DISCOVER_ONLY
1569 clobber: mac_clobber,
1574 duplicate: mac_duplicate,
1577 #ifndef DISCOVER_ONLY
1578 /* FIXME: remove this cast, once mac_write is fixed not to
1579 modify its *DISK parameter. */
1580 write: (int (*) (const PedDisk*)) mac_write,
1585 partition_new: mac_partition_new,
1586 partition_duplicate: mac_partition_duplicate,
1587 partition_destroy: mac_partition_destroy,
1588 partition_set_system: mac_partition_set_system,
1589 partition_set_flag: mac_partition_set_flag,
1590 partition_get_flag: mac_partition_get_flag,
1591 partition_is_flag_available: mac_partition_is_flag_available,
1592 partition_set_name: mac_partition_set_name,
1593 partition_get_name: mac_partition_get_name,
1594 partition_align: mac_partition_align,
1595 partition_enumerate: mac_partition_enumerate,
1597 alloc_metadata: mac_alloc_metadata,
1598 get_max_primary_partition_count:
1599 mac_get_max_primary_partition_count
1602 static PedDiskType mac_disk_type = {
1606 features: PED_DISK_TYPE_PARTITION_NAME
1610 ped_disk_mac_init ()
1612 PED_ASSERT (sizeof (MacRawPartition) == 512, return);
1613 PED_ASSERT (sizeof (MacRawDisk) == 512, return);
1615 ped_disk_type_register (&mac_disk_type);
1619 ped_disk_mac_done ()
1621 ped_disk_type_unregister (&mac_disk_type);