1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615
|
/*
libparted - a library for manipulating disk partitions
Copyright (C) 2000, 2002, 2004, 2007-2014 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <parted/parted.h>
#include <parted/debug.h>
#include <parted/endian.h>
#include <stdbool.h>
#if ENABLE_NLS
# include <libintl.h>
# define _(String) dgettext (PACKAGE, String)
#else
# define _(String) (String)
#endif /* ENABLE_NLS */
#include "misc.h"
#include "pt-tools.h"
/* struct's hacked from Linux source: fs/partitions/mac.h
* I believe it was originally written by Paul Mackerras (from comments in
* Quik source)
*
* See also:
* http://developer.apple.com/documentation/mac/Devices/Devices-126.html
* http://developer.apple.com/documentation/mac/Devices/Devices-121.html
* http://devworld.apple.com/technotes/tn/tn1189.html
*
* Partition types:
* Apple_Bootstrap new-world (HFS) boot partition
* Apple_partition_map partition map (table)
* Apple_Driver device driver
* Apple_Driver43 SCSI Manager 4.3 device driver
* Apple_MFS original Macintosh File System
* Apple_HFS Hierarchical File System (and +)
* Apple_HFSX HFS+ with case sensitivity and more
* Apple_UNIX_SVR2 UNIX file system (UFS?)
* Apple_PRODOS ProDOS file system
* Apple_Free unused space
* Apple_Scratch empty
* Apple_Void padding for iso9660
* Apple_Extra an unused partition map entry
*
* Quick explanation:
* ------------------
* Terminology:
*
* Parted Apple
* ------ -----
* device disk/device
* disk no equivalent.
* partition volume or partition
* sector block
*
* * All space must be accounted for, except block 0 (driver block) and
* block 1-X (the partition map: i.e. lots of MacRawPartitions)
*
* * It's really hard to grow/shrink the number of MacRawPartition
* entries in the partition map, because the first partition starts
* immediately after the partition map. When we can move the start of
* HFS and ext2 partitions, this problem will disappear ;-)
*/
#define MAC_PARTITION_MAGIC_1 0x5453 /* old */
#define MAC_PARTITION_MAGIC_2 0x504d
#define MAC_DISK_MAGIC 0x4552
#define MAC_STATUS_BOOTABLE 8 /* partition is bootable */
typedef struct _MacRawPartition MacRawPartition;
typedef struct _MacRawDisk MacRawDisk;
typedef struct _MacDeviceDriver MacDeviceDriver;
typedef struct _MacPartitionData MacPartitionData;
typedef struct _MacDiskData MacDiskData;
struct __attribute__ ((packed)) _MacRawPartition {
uint16_t signature; /* expected to be MAC_PARTITION_MAGIC */
uint16_t res1;
uint32_t map_count; /* # blocks in partition map */
uint32_t start_block; /* absolute starting block # of partition */
uint32_t block_count; /* number of blocks in partition */
char name[32]; /* partition name */
char type[32]; /* string type description */
uint32_t data_start; /* rel block # of first data block */
uint32_t data_count; /* number of data blocks */
uint32_t status; /* partition status bits */
uint32_t boot_start;
uint32_t boot_count;
uint32_t boot_load;
uint32_t boot_load2;
uint32_t boot_entry;
uint32_t boot_entry2;
uint32_t boot_cksum;
char processor[16]; /* Contains 680x0, x=0,2,3,4; or empty */
uint32_t driver_sig;
char _padding[372];
};
/* Driver descriptor structure, in block 0 */
struct __attribute__ ((packed)) _MacRawDisk {
uint16_t signature; /* expected to be MAC_DRIVER_MAGIC */
uint16_t block_size; /* physical sector size */
uint32_t block_count; /* size of device in blocks */
uint16_t dev_type; /* reserved */
uint16_t dev_id; /* reserved */
uint32_t data; /* reserved */
uint16_t driver_count; /* # of driver descriptor entries */
uint8_t driverlist[488];/* info about available drivers */
uint16_t padding[3]; /* pad to 512 bytes */
};
struct __attribute__ ((packed)) _MacDeviceDriver {
uint32_t block; /* startblock in MacRawDisk->block_size units */
uint16_t size; /* size in 512 byte units */
uint16_t type; /* operating system type (MacOS = 1) */
};
struct _MacPartitionData {
char volume_name[33]; /* eg: "Games" */
char system_name[33]; /* eg: "Apple_Unix_SVR2" */
char processor_name[17];
int is_boot;
int is_driver;
int has_driver;
int is_root;
int is_swap;
int is_lvm;
int is_raid;
PedSector data_region_length;
PedSector boot_region_length;
uint32_t boot_base_address;
uint32_t boot_entry_address;
uint32_t boot_checksum;
uint32_t status;
uint32_t driver_sig;
};
struct _MacDiskData {
int ghost_size; /* sectors per "driver" block */
int part_map_entry_count; /* # entries (incl. ghost) */
int part_map_entry_num; /* partition map location */
int active_part_entry_count; /* # real partitions */
int free_part_entry_count; /* # free space */
int last_part_entry_num; /* last entry number */
uint16_t block_size; /* physical sector size */
uint16_t driver_count;
MacDeviceDriver driverlist[1 + 60]; /* 488 bytes */
};
static PedDiskType mac_disk_type;
static int
_check_signature (MacRawDisk const *raw_disk)
{
if (PED_BE16_TO_CPU (raw_disk->signature) != MAC_DISK_MAGIC) {
#ifdef DISCOVER_ONLY
return 0;
#else
return ped_exception_throw (
PED_EXCEPTION_ERROR,
PED_EXCEPTION_IGNORE_CANCEL,
_("Invalid signature %x for Mac disk labels."),
(int) PED_BE16_TO_CPU (raw_disk->signature))
== PED_EXCEPTION_IGNORE;
#endif
}
return 1;
}
static int
_rawpart_check_signature (MacRawPartition* raw_part)
{
int sig = (int) PED_BE16_TO_CPU (raw_part->signature);
return sig == MAC_PARTITION_MAGIC_1 || sig == MAC_PARTITION_MAGIC_2;
}
static int
mac_probe (const PedDevice * dev)
{
PED_ASSERT (dev != NULL);
if (dev->sector_size < sizeof (MacRawDisk))
return 0;
void *label;
if (!ptt_read_sector (dev, 0, &label))
return 0;
int valid = _check_signature (label);
free (label);
return valid;
}
static int
_disk_add_part_map_entry (PedDisk* disk, int warn)
{
MacDiskData* mac_disk_data = disk->disk_specific;
PedPartition* new_part;
MacPartitionData* mac_part_data;
PedSector part_map_size;
PedConstraint* constraint_any = ped_constraint_any (disk->dev);
#ifndef DISCOVER_ONLY
if (warn && ped_exception_throw (
PED_EXCEPTION_ERROR,
PED_EXCEPTION_FIX | PED_EXCEPTION_CANCEL,
_("Partition map has no partition map entry!"))
!= PED_EXCEPTION_FIX)
goto error;
#endif /* !DISCOVER_ONLY */
part_map_size
= ped_round_up_to (mac_disk_data->last_part_entry_num, 64);
if (part_map_size == 0)
part_map_size = 64;
new_part = ped_partition_new (disk, PED_PARTITION_NORMAL, NULL,
1, part_map_size - 1);
if (!new_part)
goto error;
mac_part_data = new_part->disk_specific;
strcpy (mac_part_data->volume_name, "Apple");
strcpy (mac_part_data->system_name, "Apple_partition_map");
if (!ped_disk_add_partition (disk, new_part, constraint_any))
goto error_destroy_new_part;
mac_disk_data->part_map_entry_num = new_part->num;
mac_disk_data->part_map_entry_count
= new_part->geom.end - mac_disk_data->ghost_size;
ped_constraint_destroy (constraint_any);
return 1;
error_destroy_new_part:
ped_partition_destroy (new_part);
error:
ped_constraint_destroy (constraint_any);
return 0;
}
static PedDisk*
mac_alloc (const PedDevice* dev)
{
PedDisk* disk;
MacDiskData* mac_disk_data;
PED_ASSERT (dev != NULL);
#ifndef DISCOVER_ONLY
if (dev->length < 256) {
ped_exception_throw (
PED_EXCEPTION_ERROR,
PED_EXCEPTION_CANCEL,
_("%s is too small for a Mac disk label!"),
dev->path);
goto error;
}
#endif
disk = _ped_disk_alloc (dev, &mac_disk_type);
if (!disk)
goto error;
mac_disk_data = (MacDiskData*) ped_malloc (sizeof (MacDiskData));
if (!mac_disk_data)
goto error_free_disk;
disk->disk_specific = mac_disk_data;
mac_disk_data->ghost_size = 1;
mac_disk_data->active_part_entry_count = 0;
mac_disk_data->free_part_entry_count = 1;
mac_disk_data->last_part_entry_num = 1;
mac_disk_data->block_size = 0;
mac_disk_data->driver_count = 0;
memset(&mac_disk_data->driverlist[0], 0, sizeof(mac_disk_data->driverlist));
if (!_disk_add_part_map_entry (disk, 0))
goto error_free_disk;
return disk;
error_free_disk:
_ped_disk_free (disk);
error:
return NULL;
}
static PedDisk*
mac_duplicate (const PedDisk* disk)
{
PedDisk* new_disk;
MacDiskData* new_mac_data;
MacDiskData* old_mac_data = (MacDiskData*) disk->disk_specific;
new_disk = ped_disk_new_fresh (disk->dev, &mac_disk_type);
if (!new_disk)
return NULL;
new_mac_data = (MacDiskData*) new_disk->disk_specific;
/* remove the partition map partition - it will be duplicated
* later.
*/
PedSector first_part_map_sector = old_mac_data->ghost_size;
PedPartition *partition_map
= ped_disk_get_partition_by_sector (new_disk, first_part_map_sector);
PED_ASSERT (partition_map != NULL);
/* ped_disk_remove_partition may be used only to delete a "normal"
partition. Trying to delete at least "freespace" or "metadata"
partitions leads to a violation of assumptions in
ped_disk_remove_partition, since it calls _disk_push_update_mode,
which destroys all "freespace" and "metadata" partitions, and
depends on that destruction not freeing its PART parameter. */
PED_ASSERT (partition_map->type == PED_PARTITION_NORMAL);
ped_disk_remove_partition (new_disk, partition_map);
/* ugly, but C is ugly :p */
memcpy (new_mac_data, old_mac_data, sizeof (MacDiskData));
return new_disk;
}
static void
mac_free (PedDisk* disk)
{
MacDiskData* mac_disk_data = disk->disk_specific;
_ped_disk_free (disk);
free (mac_disk_data);
}
static int
_rawpart_cmp_type (const MacRawPartition* raw_part, const char* type)
{
return strncasecmp (raw_part->type, type, 32) == 0;
}
static int
_rawpart_cmp_name (const MacRawPartition* raw_part, const char* name)
{
return strncasecmp (raw_part->name, name, 32) == 0;
}
static int
_rawpart_is_partition_map (const MacRawPartition* raw_part)
{
return _rawpart_cmp_type (raw_part, "Apple_partition_map");
}
static int
strncasestr (const char* haystack, const char* needle, int n)
{
int needle_size = strlen (needle);
int i;
for (i = 0; haystack[i] && i < n - needle_size; i++) {
if (strncasecmp (haystack + i, needle, needle_size) == 0)
return 1;
}
return 0;
}
static int
_rawpart_is_boot (const MacRawPartition* raw_part)
{
if (!strcasecmp(raw_part->type, "Apple_Bootstrap"))
return 1;
if (!strcasecmp(raw_part->type, "Apple_Boot"))
return 1;
return 0;
}
static int
_rawpart_is_driver (const MacRawPartition* raw_part)
{
if (strncmp (raw_part->type, "Apple_", 6) != 0)
return 0;
if (!strncasestr (raw_part->type, "driver", 32))
return 0;
return 1;
}
static int _GL_ATTRIBUTE_PURE
_rawpart_has_driver (const MacRawPartition* raw_part, MacDiskData* mac_disk_data)
{
MacDeviceDriver *driverlist;
uint16_t i;
uint32_t driver_bs, driver_be, part_be;
driverlist = &mac_disk_data->driverlist[0];
for (i = 0; i < mac_disk_data->driver_count; i++) {
driver_bs = driverlist->block;
driver_be = driver_bs + driverlist->size;
part_be = raw_part->start_block + raw_part->block_count;
if (driver_bs >= raw_part->start_block && driver_be <= part_be)
return 1;
driverlist++;
}
return 0;
}
static int
_rawpart_is_root (MacRawPartition* raw_part)
{
if (!_rawpart_cmp_type (raw_part, "Apple_UNIX_SVR2"))
return 0;
if (strcmp (raw_part->name, "root") != 0)
return 0;
return 1;
}
static int
_rawpart_is_swap (MacRawPartition* raw_part)
{
if (!_rawpart_cmp_type (raw_part, "Apple_UNIX_SVR2"))
return 0;
if (strcmp (raw_part->name, "swap") != 0)
return 0;
return 1;
}
static int
_rawpart_is_lvm (MacRawPartition* raw_part)
{
if (strcmp (raw_part->type, "Linux_LVM") != 0)
return 0;
return 1;
}
static int
_rawpart_is_raid (MacRawPartition* raw_part)
{
if (strcmp (raw_part->type, "Linux_RAID") != 0)
return 0;
return 1;
}
static int
_rawpart_is_void (MacRawPartition* raw_part)
{
return _rawpart_cmp_type (raw_part, "Apple_Void");
}
/* returns 1 if the raw_part represents a partition that is "unused space", or
* doesn't represent a partition at all. NOTE: some people make Apple_Free
* partitions with MacOS, because they can't select another type. So, if the
* name is anything other than "Extra" or "", it is treated as a "real"
* partition.
*/
static int
_rawpart_is_active (MacRawPartition* raw_part)
{
if (_rawpart_cmp_type (raw_part, "Apple_Free")
&& (_rawpart_cmp_name (raw_part, "Extra")
|| _rawpart_cmp_name (raw_part, "")))
return 0;
if (_rawpart_cmp_type (raw_part, "Apple_Void"))
return 0;
if (_rawpart_cmp_type (raw_part, "Apple_Scratch"))
return 0;
if (_rawpart_cmp_type (raw_part, "Apple_Extra"))
return 0;
return 1;
}
static PedPartition*
_rawpart_analyse (MacRawPartition* raw_part, PedDisk* disk, int num)
{
MacDiskData* mac_disk_data;
PedPartition* part;
MacPartitionData* mac_part_data;
PedSector start, length;
if (!_rawpart_check_signature (raw_part)) {
#ifndef DISCOVER_ONLY
if (ped_exception_throw (
PED_EXCEPTION_WARNING,
PED_EXCEPTION_IGNORE_CANCEL,
_("Partition %d has an invalid signature %x."),
num,
(int) PED_BE16_TO_CPU (raw_part->signature))
!= PED_EXCEPTION_IGNORE)
#endif
goto error;
}
mac_disk_data = (MacDiskData*) disk->disk_specific;
start = PED_BE32_TO_CPU (raw_part->start_block);
length = PED_BE32_TO_CPU (raw_part->block_count);
if (length == 0) {
#ifndef DISCOVER_ONLY
ped_exception_throw (
PED_EXCEPTION_ERROR,
PED_EXCEPTION_CANCEL,
_("Partition %d has an invalid length of 0 bytes!"),
num);
#endif
return NULL;
}
part = ped_partition_new (disk, PED_PARTITION_NORMAL, NULL,
start, start + length - 1);
if (!part)
goto error;
mac_part_data = part->disk_specific;
strncpy (mac_part_data->volume_name, raw_part->name, 32);
strncpy (mac_part_data->system_name, raw_part->type, 32);
strncpy (mac_part_data->processor_name, raw_part->processor, 16);
mac_part_data->is_boot = _rawpart_is_boot (raw_part);
mac_part_data->is_driver = _rawpart_is_driver (raw_part);
if (mac_part_data->is_driver)
mac_part_data->has_driver = _rawpart_has_driver(raw_part, mac_disk_data);
mac_part_data->is_root = _rawpart_is_root (raw_part);
mac_part_data->is_swap = _rawpart_is_swap (raw_part);
mac_part_data->is_lvm = _rawpart_is_lvm (raw_part);
mac_part_data->is_raid = _rawpart_is_raid (raw_part);
/* "data" region */
#ifndef DISCOVER_ONLY
if (raw_part->data_start) {
ped_exception_throw (
PED_EXCEPTION_ERROR,
PED_EXCEPTION_CANCEL,
_("The data region doesn't start at the start "
"of the partition."));
goto error_destroy_part;
}
#endif /* !DISCOVER_ONLY */
mac_part_data->data_region_length
= PED_BE32_TO_CPU (raw_part->data_count);
/* boot region - we have no idea what this is for, but Mac OSX
* seems to put garbage here, and doesn't pay any attention to
* it afterwards. [clausen, dan burcaw]
*/
#if 0
if (raw_part->boot_start) {
ped_exception_throw (
PED_EXCEPTION_ERROR,
PED_EXCEPTION_CANCEL,
_("The boot region doesn't start at the start "
"of the partition."));
goto error_destroy_part;
}
#endif
mac_part_data->boot_region_length
= PED_BE32_TO_CPU (raw_part->boot_count);
#ifndef DISCOVER_ONLY
if (mac_part_data->has_driver) {
if (mac_part_data->boot_region_length < part->geom.length) {
if (ped_exception_throw (
PED_EXCEPTION_ERROR,
PED_EXCEPTION_IGNORE_CANCEL,
_("The partition's boot region doesn't occupy "
"the entire partition."))
!= PED_EXCEPTION_IGNORE)
goto error_destroy_part;
}
} else {
if (mac_part_data->data_region_length < part->geom.length &&
!mac_part_data->is_boot) {
if (ped_exception_throw (
PED_EXCEPTION_ERROR,
PED_EXCEPTION_IGNORE_CANCEL,
_("The partition's data region doesn't occupy "
"the entire partition."))
!= PED_EXCEPTION_IGNORE)
goto error_destroy_part;
}
}
#endif /* !DISCOVER_ONLY */
mac_part_data->boot_base_address
= PED_BE32_TO_CPU (raw_part->boot_load);
mac_part_data->boot_entry_address
= PED_BE32_TO_CPU (raw_part->boot_entry);
mac_part_data->boot_checksum
= PED_BE32_TO_CPU (raw_part->boot_cksum);
mac_part_data->status = PED_BE32_TO_CPU (raw_part->status);
mac_part_data->driver_sig = PED_BE32_TO_CPU (raw_part->driver_sig);
return part;
error_destroy_part:
ped_partition_destroy (part);
error:
return NULL;
}
/* looks at the partition map size field in a mac raw partition, and calculates
* what the size of the partition map should be, from it
*/
static int
_rawpart_get_partmap_size (MacRawPartition* raw_part, PedDisk* disk)
{
MacDiskData* mac_disk_data = disk->disk_specific;
PedSector part_map_start;
PedSector part_map_end;
part_map_start = mac_disk_data->ghost_size;
part_map_end = PED_BE32_TO_CPU (raw_part->map_count);
return part_map_end - part_map_start + 1;
}
static int
_disk_analyse_block_size (PedDisk* disk, MacRawDisk* raw_disk)
{
PedSector block_size;
if (PED_BE16_TO_CPU (raw_disk->block_size) % 512) {
#ifndef DISCOVER_ONLY
ped_exception_throw (
PED_EXCEPTION_ERROR,
PED_EXCEPTION_CANCEL,
_("Weird block size on device descriptor: %d bytes is "
"not divisible by 512."),
(int) PED_BE16_TO_CPU (raw_disk->block_size));
#endif
goto error;
}
block_size = PED_BE16_TO_CPU (raw_disk->block_size) / 512;
if (block_size != disk->dev->sector_size / 512) {
#ifndef DISCOVER_ONLY
if (ped_exception_throw (
PED_EXCEPTION_WARNING,
PED_EXCEPTION_IGNORE_CANCEL,
_("The driver descriptor says the physical block size "
"is %d bytes, but Linux says it is %d bytes."),
(int) block_size * 512,
(int) disk->dev->sector_size)
!= PED_EXCEPTION_IGNORE)
goto error;
#endif
disk->dev->sector_size = block_size * 512;
}
return 1;
error:
return 0;
}
/* Tries to figure out the block size used by the drivers, for the ghost
* partitioning scheme. Ghost partitioning works like this: the OpenFirmware
* (OF) sees 512 byte blocks, but some drivers use 2048 byte blocks (and,
* perhaps, some other number?). To remain compatible, the partition map
* only has "real" partition map entries on ghost-aligned block numbers (and
* the others are padded with Apple_Void partitions). This function tries
* to figure out what the "ghost-aligned" size is... (which, believe-it-or-not,
* doesn't always equal 2048!!!)
*/
static int
_disk_analyse_ghost_size (PedDisk* disk)
{
MacDiskData* mac_disk_data = disk->disk_specific;
void *buf = ped_malloc (disk->dev->sector_size);
if (!buf)
return 0;
int i;
int found = 0;
for (i = 1; i < 64; i *= 2) {
if (!ped_device_read (disk->dev, buf, i, 1))
break;
if (_rawpart_check_signature (buf)
&& !_rawpart_is_void (buf)) {
mac_disk_data->ghost_size = i;
found = (i <= disk->dev->sector_size / 512);
break;
}
}
free (buf);
#ifndef DISCOVER_ONLY
if (!found)
ped_exception_throw (
PED_EXCEPTION_ERROR,
PED_EXCEPTION_CANCEL,
_("No valid partition map found."));
#endif
return found;
}
static int
mac_read (PedDisk* disk)
{
MacDiskData* mac_disk_data;
PedPartition* part;
int num;
PedSector ghost_size;
int last_part_entry_num = 0;
PED_ASSERT (disk != NULL);
mac_disk_data = disk->disk_specific;
mac_disk_data->part_map_entry_num = 0; /* 0 == none */
void *buf;
if (!ptt_read_sector (disk->dev, 0, &buf))
return 0;
MacRawDisk *raw_disk = buf;
if (!_check_signature (raw_disk))
goto error;
/* Record the original sector size; this function may change it. */
PedSector ss0 = disk->dev->sector_size;
if (!_disk_analyse_block_size (disk, raw_disk))
goto error;
if (!_disk_analyse_ghost_size (disk))
goto error;
ghost_size = mac_disk_data->ghost_size;
if (!ped_disk_delete_all (disk))
goto error;
if (raw_disk->driver_count && raw_disk->driver_count < 62) {
memcpy(&mac_disk_data->driverlist[0], &raw_disk->driverlist[0],
sizeof(mac_disk_data->driverlist));
mac_disk_data->driver_count = raw_disk->driver_count;
mac_disk_data->block_size = raw_disk->block_size;
}
/* If _disk_analyse_block_size has increased the sector_size,
reallocate this buffer, so we can still read a sector into it. */
if (ss0 < disk->dev->sector_size) {
free (buf);
buf = ped_malloc (disk->dev->sector_size);
if (buf == NULL)
goto error;
}
for (num=1; num==1 || num <= last_part_entry_num; num++) {
void *raw_part = buf;
if (!ped_device_read (disk->dev, raw_part,
num * ghost_size, 1))
goto error_delete_all;
if (!_rawpart_check_signature (raw_part))
continue;
if (num == 1)
last_part_entry_num
= _rawpart_get_partmap_size (raw_part, disk);
if (_rawpart_get_partmap_size (raw_part, disk)
!= last_part_entry_num) {
if (ped_exception_throw (
PED_EXCEPTION_ERROR,
PED_EXCEPTION_IGNORE_CANCEL,
_("Conflicting partition map entry sizes! "
"Entry 1 says it is %d, but entry %d says "
"it is %d!"),
last_part_entry_num,
_rawpart_get_partmap_size (raw_part, disk))
!= PED_EXCEPTION_IGNORE)
goto error_delete_all;
}
if (!_rawpart_is_active (raw_part))
continue;
part = _rawpart_analyse (raw_part, disk, num);
if (!part)
goto error_delete_all;
part->num = num;
part->fs_type = ped_file_system_probe (&part->geom);
PedConstraint *constraint_exact
= ped_constraint_exact (&part->geom);
if (constraint_exact == NULL)
goto error_delete_all;
bool ok = ped_disk_add_partition (disk, part, constraint_exact);
ped_constraint_destroy (constraint_exact);
if (!ok)
goto error_delete_all;
if (_rawpart_is_partition_map (raw_part)) {
if (mac_disk_data->part_map_entry_num
&& ped_exception_throw (
PED_EXCEPTION_ERROR,
PED_EXCEPTION_IGNORE_CANCEL,
_("Weird! There are 2 partitions "
"map entries!"))
!= PED_EXCEPTION_IGNORE)
goto error_delete_all;
mac_disk_data->part_map_entry_num = num;
mac_disk_data->part_map_entry_count
= part->geom.end - ghost_size + 1;
}
}
if (!mac_disk_data->part_map_entry_num) {
if (!_disk_add_part_map_entry (disk, 1))
goto error_delete_all;
ped_disk_commit_to_dev (disk);
}
free (buf);
return 1;
error_delete_all:
ped_disk_delete_all (disk);
error:
free (buf);
return 0;
}
#ifndef DISCOVER_ONLY
/* The Ghost partition: is a blank entry, used to pad out each block (where
* there physical block size > 512 bytes). This is because OpenFirmware uses
* 512 byte blocks, but device drivers Think Different TM, with a different
* lbock size, so we need to do this to avoid a clash (!)
*/
static int
_pad_raw_part (PedDisk* disk, int num, MacRawPartition* part_map)
{
MacDiskData* mac_disk_data = disk->disk_specific;
int i;
size_t ss = disk->dev->sector_size;
void *buf = ped_calloc (ss);
if (!buf)
return 0;
MacRawPartition *ghost_entry = buf;
ghost_entry->signature = PED_CPU_TO_BE16 (MAC_PARTITION_MAGIC_2);
strcpy (ghost_entry->type, "Apple_Void");
ghost_entry->map_count
= PED_CPU_TO_BE32 (mac_disk_data->last_part_entry_num);
for (i=0; i < mac_disk_data->ghost_size - 1; i++) {
PedSector idx = i + (num - 1) * mac_disk_data->ghost_size;
memcpy ((char*)part_map + idx * ss, ghost_entry, ss);
}
free (buf);
return 1;
}
static void
_update_driver_count (MacRawPartition* part_map_entry,
MacDiskData *mac_driverdata, const MacDiskData* mac_disk_data)
{
uint16_t i, count_orig, count_cur;
uint32_t driver_bs, driver_be, part_be;
count_cur = mac_driverdata->driver_count;
count_orig = mac_disk_data->driver_count;
for (i = 0; i < count_orig; i++) {
driver_bs = mac_disk_data->driverlist[i].block;
driver_be = driver_bs + mac_disk_data->driverlist[i].size;
part_be = part_map_entry->start_block + part_map_entry->block_count;
if (driver_bs >= part_map_entry->start_block
&& driver_be <= part_be) {
mac_driverdata->driverlist[count_cur].block
= mac_disk_data->driverlist[i].block;
mac_driverdata->driverlist[count_cur].size
= mac_disk_data->driverlist[i].size;
mac_driverdata->driverlist[count_cur].type
= mac_disk_data->driverlist[i].type;
mac_driverdata->driver_count++;
break;
}
}
}
static MacRawPartition *
get_pme (MacRawPartition const *part_map, PedSector i, PedDisk const *disk)
{
MacDiskData const *mac_disk_data = disk->disk_specific;
PedSector idx = i * mac_disk_data->ghost_size - 1;
return (MacRawPartition *) ((char*)part_map
+ idx * disk->dev->sector_size);
}
/* Initialize the disk->dev->sector_size bytes of part_map[part->num]. */
static int
_generate_raw_part (PedDisk* disk, PedPartition* part,
MacRawPartition* part_map, MacDiskData *mac_driverdata)
{
MacDiskData* mac_disk_data;
MacPartitionData* mac_part_data;
PED_ASSERT (part->num > 0);
mac_disk_data = disk->disk_specific;
mac_part_data = part->disk_specific;
MacRawPartition *part_map_entry = get_pme (part_map, part->num, disk);
memset (part_map_entry, 0, disk->dev->sector_size);
part_map_entry->signature = PED_CPU_TO_BE16 (MAC_PARTITION_MAGIC_2);
part_map_entry->map_count
= PED_CPU_TO_BE32 (mac_disk_data->last_part_entry_num);
part_map_entry->start_block = PED_CPU_TO_BE32 (part->geom.start);
part_map_entry->block_count = PED_CPU_TO_BE32 (part->geom.length);
strcpy (part_map_entry->name, mac_part_data->volume_name);
strcpy (part_map_entry->type, mac_part_data->system_name);
if (mac_part_data->is_driver) {
mac_part_data->boot_region_length = part->geom.length;
if (mac_part_data->has_driver)
_update_driver_count(part_map_entry, mac_driverdata,
mac_disk_data);
} else
mac_part_data->data_region_length = part->geom.length;
part_map_entry->data_count = PED_CPU_TO_BE32 (
mac_part_data->data_region_length);
part_map_entry->boot_count = PED_CPU_TO_BE32 (
mac_part_data->boot_region_length);
part_map_entry->status = PED_CPU_TO_BE32 (mac_part_data->status);
part_map_entry->driver_sig
= PED_CPU_TO_BE32 (mac_part_data->driver_sig);
part_map_entry->boot_load =
PED_CPU_TO_BE32 (mac_part_data->boot_base_address);
part_map_entry->boot_entry =
PED_CPU_TO_BE32 (mac_part_data->boot_entry_address);
part_map_entry->boot_cksum =
PED_CPU_TO_BE32 (mac_part_data->boot_checksum);
strncpy (part_map_entry->processor, mac_part_data->processor_name, 16);
if (!_pad_raw_part (disk, part->num, part_map))
goto error;
return 1;
error:
return 0;
}
static int
_generate_raw_freespace_part (PedDisk* disk, PedGeometry* geom, int num,
MacRawPartition* part_map)
{
MacDiskData* mac_disk_data = disk->disk_specific;
PED_ASSERT (num > 0);
MacRawPartition *part_map_entry = get_pme (part_map, num, disk);
part_map_entry->signature = PED_CPU_TO_BE16 (MAC_PARTITION_MAGIC_2);
part_map_entry->map_count
= PED_CPU_TO_BE32 (mac_disk_data->last_part_entry_num);
part_map_entry->start_block = PED_CPU_TO_BE32 (geom->start);
part_map_entry->block_count = PED_CPU_TO_BE32 (geom->length);
strcpy (part_map_entry->name, "Extra");
strcpy (part_map_entry->type, "Apple_Free");
part_map_entry->data_count = PED_CPU_TO_BE32 (geom->length);
part_map_entry->status = 0;
part_map_entry->driver_sig = 0;
if (!_pad_raw_part (disk, num, part_map))
goto error;
return 1;
error:
return 0;
}
static int
_generate_empty_part (PedDisk* disk, int num, MacRawPartition* part_map)
{
MacDiskData* mac_disk_data = disk->disk_specific;
PED_ASSERT (num > 0);
MacRawPartition *part_map_entry = get_pme (part_map, num, disk);
part_map_entry->signature = PED_CPU_TO_BE16 (MAC_PARTITION_MAGIC_2);
part_map_entry->map_count
= PED_CPU_TO_BE32 (mac_disk_data->last_part_entry_num);
strcpy (part_map_entry->type, "Apple_Void");
return _pad_raw_part (disk, num, part_map);
}
/* returns the first empty entry in the partition map */
static int _GL_ATTRIBUTE_PURE
_get_first_empty_part_entry (PedDisk* disk, MacRawPartition* part_map)
{
MacDiskData* mac_disk_data = disk->disk_specific;
int i;
for (i=1; i <= mac_disk_data->last_part_entry_num; i++) {
MacRawPartition *part_map_entry = get_pme (part_map, i, disk);
if (!part_map_entry->signature)
return i;
}
return 0;
}
static int
write_block_zero (PedDisk* disk, MacDiskData* mac_driverdata)
{
PedDevice* dev = disk->dev;
void *s0;
if (!ptt_read_sector (dev, 0, &s0))
return 0;
MacRawDisk *raw_disk = (MacRawDisk *) s0;
raw_disk->signature = PED_CPU_TO_BE16 (MAC_DISK_MAGIC);
raw_disk->block_size = PED_CPU_TO_BE16 (dev->sector_size);
raw_disk->block_count = PED_CPU_TO_BE32 (dev->length);
raw_disk->driver_count = mac_driverdata->driver_count;
memcpy(&raw_disk->driverlist[0], &mac_driverdata->driverlist[0],
sizeof(raw_disk->driverlist));
int write_ok = ped_device_write (dev, raw_disk, 0, 1);
free (s0);
return write_ok;
}
static int
mac_write (PedDisk* disk)
{
MacRawPartition* part_map;
MacDiskData* mac_disk_data;
MacDiskData* mac_driverdata; /* updated driver list */
PedPartition* part;
int num;
PED_ASSERT (disk != NULL);
PED_ASSERT (disk->disk_specific != NULL);
PED_ASSERT (disk->dev != NULL);
PED_ASSERT (!disk->update_mode);
mac_disk_data = disk->disk_specific;
if (!ped_disk_get_partition (disk, mac_disk_data->part_map_entry_num)) {
if (!_disk_add_part_map_entry (disk, 1))
goto error;
}
mac_driverdata = ped_malloc(sizeof(MacDiskData));
if (!mac_driverdata)
goto error;
memset (mac_driverdata, 0, sizeof(MacDiskData));
size_t pmap_bytes = (mac_disk_data->part_map_entry_count
* mac_disk_data->ghost_size
* disk->dev->sector_size);
part_map = (MacRawPartition*) ped_calloc (pmap_bytes);
if (!part_map)
goto error_free_driverdata;
/* write (to memory) the "real" partitions */
for (part = ped_disk_next_partition (disk, NULL); part;
part = ped_disk_next_partition (disk, part)) {
if (!ped_partition_is_active (part))
continue;
if (!_generate_raw_part (disk, part, part_map, mac_driverdata))
goto error_free_part_map;
}
/* write the "free space" partitions */
for (part = ped_disk_next_partition (disk, NULL); part;
part = ped_disk_next_partition (disk, part)) {
if (part->type != PED_PARTITION_FREESPACE)
continue;
num = _get_first_empty_part_entry (disk, part_map);
if (!_generate_raw_freespace_part (disk, &part->geom, num,
part_map))
goto error_free_part_map;
}
/* write the "void" (empty) partitions */
for (num = _get_first_empty_part_entry (disk, part_map); num;
num = _get_first_empty_part_entry (disk, part_map))
_generate_empty_part (disk, num, part_map);
/* write to disk */
if (!ped_device_write (disk->dev, part_map, 1,
mac_disk_data->part_map_entry_count))
goto error_free_part_map;
free (part_map);
int write_ok = write_block_zero (disk, mac_driverdata);
free (mac_driverdata);
return write_ok;
error_free_part_map:
free (part_map);
error_free_driverdata:
free (mac_driverdata);
error:
return 0;
}
#endif /* !DISCOVER_ONLY */
static PedPartition*
mac_partition_new (
const PedDisk* disk, PedPartitionType part_type,
const PedFileSystemType* fs_type, PedSector start, PedSector end)
{
PedPartition* part;
MacPartitionData* mac_data;
part = _ped_partition_alloc (disk, part_type, fs_type, start, end);
if (!part)
goto error;
if (ped_partition_is_active (part)) {
part->disk_specific
= mac_data = ped_malloc (sizeof (MacPartitionData));
if (!mac_data)
goto error_free_part;
memset (mac_data, 0, sizeof (MacPartitionData));
strcpy (mac_data->volume_name, "untitled");
} else {
part->disk_specific = NULL;
}
return part;
error_free_part:
free (part);
error:
return NULL;
}
static PedPartition*
mac_partition_duplicate (const PedPartition* part)
{
PedPartition* new_part;
MacPartitionData* new_mac_data;
MacPartitionData* old_mac_data;
new_part = ped_partition_new (part->disk, part->type,
part->fs_type, part->geom.start,
part->geom.end);
if (!new_part)
return NULL;
new_part->num = part->num;
old_mac_data = (MacPartitionData*) part->disk_specific;
new_mac_data = (MacPartitionData*) new_part->disk_specific;
/* ugly, but C is ugly :p */
memcpy (new_mac_data, old_mac_data, sizeof (MacPartitionData));
return new_part;
}
static void
mac_partition_destroy (PedPartition* part)
{
PED_ASSERT (part != NULL);
if (ped_partition_is_active (part))
free (part->disk_specific);
free (part);
}
static int
mac_partition_set_system (PedPartition* part, const PedFileSystemType* fs_type)
{
MacPartitionData* mac_data = part->disk_specific;
part->fs_type = fs_type;
if (fs_type && is_linux_swap (fs_type->name))
ped_partition_set_flag (part, PED_PARTITION_SWAP, 1);
if (mac_data->is_boot) {
strcpy (mac_data->system_name, "Apple_Bootstrap");
mac_data->status = 0x33;
return 1;
}
if (fs_type && (!strcmp (fs_type->name, "hfs")
|| !strcmp (fs_type->name, "hfs+"))) {
strcpy (mac_data->system_name, "Apple_HFS");
mac_data->status |= 0x7f;
} else if (fs_type && !strcmp (fs_type->name, "hfsx")) {
strcpy (mac_data->system_name, "Apple_HFSX");
mac_data->status |= 0x7f;
} else {
strcpy (mac_data->system_name, "Apple_UNIX_SVR2");
mac_data->status = 0x33;
}
return 1;
}
static int
mac_partition_set_flag (PedPartition* part, PedPartitionFlag flag, int state)
{
MacPartitionData* mac_data;
PED_ASSERT (part != NULL);
PED_ASSERT (part->disk_specific != NULL);
mac_data = part->disk_specific;
switch (flag) {
case PED_PARTITION_BOOT:
mac_data->is_boot = state;
if (part->fs_type)
return mac_partition_set_system (part, part->fs_type);
if (state) {
strcpy (mac_data->system_name, "Apple_Bootstrap");
mac_data->status = 0x33;
}
return 1;
case PED_PARTITION_ROOT:
if (state) {
strcpy (mac_data->volume_name, "root");
mac_data->is_swap = 0;
} else {
if (mac_data->is_root)
strcpy (mac_data->volume_name, "untitled");
}
mac_data->is_root = state;
return 1;
case PED_PARTITION_SWAP:
if (state) {
strcpy (mac_data->volume_name, "swap");
mac_data->is_root = 0;
} else {
if (mac_data->is_swap)
strcpy (mac_data->volume_name, "untitled");
}
mac_data->is_swap = state;
return 1;
case PED_PARTITION_LVM:
if (state) {
strcpy (mac_data->system_name, "Linux_LVM");
mac_data->is_lvm = state;
} else {
if (mac_data->is_lvm)
mac_partition_set_system (part, part->fs_type);
}
return 1;
case PED_PARTITION_RAID:
if (state) {
strcpy (mac_data->system_name, "Linux_RAID");
mac_data->is_raid = state;
} else {
if (mac_data->is_raid)
mac_partition_set_system (part, part->fs_type);
}
return 1;
default:
return 0;
}
}
static int _GL_ATTRIBUTE_PURE
mac_partition_get_flag (const PedPartition* part, PedPartitionFlag flag)
{
MacPartitionData* mac_data;
PED_ASSERT (part != NULL);
PED_ASSERT (part->disk_specific != NULL);
mac_data = part->disk_specific;
switch (flag) {
case PED_PARTITION_BOOT:
return mac_data->is_boot;
case PED_PARTITION_ROOT:
return mac_data->is_root;
case PED_PARTITION_SWAP:
return mac_data->is_swap;
case PED_PARTITION_LVM:
return mac_data->is_lvm;
case PED_PARTITION_RAID:
return mac_data->is_raid;
default:
return 0;
}
}
static int
mac_partition_is_flag_available (
const PedPartition* part, PedPartitionFlag flag)
{
switch (flag) {
case PED_PARTITION_BOOT:
case PED_PARTITION_ROOT:
case PED_PARTITION_SWAP:
case PED_PARTITION_LVM:
case PED_PARTITION_RAID:
return 1;
default:
return 0;
}
}
static void
mac_partition_set_name (PedPartition* part, const char* name)
{
MacPartitionData* mac_data;
int i;
PED_ASSERT (part != NULL);
PED_ASSERT (part->disk_specific != NULL);
mac_data = part->disk_specific;
#ifndef DISCOVER_ONLY
if (mac_data->is_root || mac_data->is_swap) {
if (ped_exception_throw (
PED_EXCEPTION_WARNING,
PED_EXCEPTION_IGNORE_CANCEL,
_("Changing the name of a root or swap partition "
"will prevent Linux from recognising it as such."))
!= PED_EXCEPTION_IGNORE)
return;
mac_data->is_root = mac_data->is_swap = 0;
}
#endif
strncpy (mac_data->volume_name, name, 32);
mac_data->volume_name [32] = 0;
for (i = strlen (mac_data->volume_name) - 1;
mac_data->volume_name[i] == ' '; i--)
mac_data->volume_name [i] = 0;
}
static const char* _GL_ATTRIBUTE_PURE
mac_partition_get_name (const PedPartition* part)
{
MacPartitionData* mac_data;
PED_ASSERT (part != NULL);
PED_ASSERT (part->disk_specific != NULL);
mac_data = part->disk_specific;
return mac_data->volume_name;
}
static PedAlignment*
mac_get_partition_alignment(const PedDisk *disk)
{
return ped_alignment_new(0, 1);
}
static PedConstraint*
_primary_constraint (PedDisk* disk)
{
PedAlignment start_align;
PedAlignment end_align;
PedGeometry max_geom;
if (!ped_alignment_init (&start_align, 0, 1))
return NULL;
if (!ped_alignment_init (&end_align, -1, 1))
return NULL;
if (!ped_geometry_init (&max_geom, disk->dev, 1, disk->dev->length - 1))
return NULL;
return ped_constraint_new (&start_align, &end_align, &max_geom,
&max_geom, 1, disk->dev->length);
}
static int
mac_partition_align (PedPartition* part, const PedConstraint* constraint)
{
PED_ASSERT (part != NULL);
if (_ped_partition_attempt_align (part, constraint,
_primary_constraint (part->disk)))
return 1;
#ifndef DISCOVER_ONLY
ped_exception_throw (
PED_EXCEPTION_ERROR,
PED_EXCEPTION_CANCEL,
_("Unable to satisfy all constraints on the partition."));
#endif
return 0;
}
static int
mac_partition_enumerate (PedPartition* part)
{
PedDisk* disk;
MacDiskData* mac_disk_data;
int i;
int max_part_count;
PED_ASSERT (part != NULL);
PED_ASSERT (part->disk != NULL);
disk = part->disk;
mac_disk_data = (MacDiskData*) disk->disk_specific;
max_part_count = ped_disk_get_max_primary_partition_count (disk);
if (part->num > 0 && part->num <= mac_disk_data->part_map_entry_count)
return 1;
for (i = 1; i <= max_part_count; i++) {
if (!ped_disk_get_partition (disk, i)) {
part->num = i;
return 1;
}
}
#ifndef DISCOVER_ONLY
ped_exception_throw (
PED_EXCEPTION_ERROR,
PED_EXCEPTION_CANCEL,
_("Can't add another partition -- the partition map is too "
"small!"));
#endif
return 0;
}
static int
_disk_count_partitions (PedDisk* disk)
{
MacDiskData* mac_disk_data = disk->disk_specific;
PedPartition* part = NULL;
PedPartition* last = NULL;
PED_ASSERT (disk->update_mode);
mac_disk_data->active_part_entry_count = 0;
mac_disk_data->free_part_entry_count = 0;
mac_disk_data->last_part_entry_num = 0;
/* subtle: we only care about free space after the partition map.
* the partition map is an "active" partition, BTW... */
for (part = ped_disk_next_partition (disk, part); part;
part = ped_disk_next_partition (disk, part)) {
if (!ped_partition_is_active (part))
continue;
mac_disk_data->active_part_entry_count++;
if (last && last->geom.end + 1 < part->geom.start)
mac_disk_data->free_part_entry_count++;
mac_disk_data->last_part_entry_num
= PED_MAX (mac_disk_data->last_part_entry_num,
part->num);
last = part;
}
if (last && last->geom.end < disk->dev->length - 1)
mac_disk_data->free_part_entry_count++;
mac_disk_data->last_part_entry_num
= PED_MAX (mac_disk_data->last_part_entry_num,
mac_disk_data->active_part_entry_count
+ mac_disk_data->free_part_entry_count);
return 1;
}
static int
add_metadata_part (PedDisk* disk, PedSector start, PedSector end)
{
PedPartition* new_part;
PedConstraint* constraint_any = ped_constraint_any (disk->dev);
PED_ASSERT (disk != NULL);
new_part = ped_partition_new (disk, PED_PARTITION_METADATA, NULL,
start, end);
if (!new_part)
goto error;
if (!ped_disk_add_partition (disk, new_part, constraint_any))
goto error_destroy_new_part;
ped_constraint_destroy (constraint_any);
return 1;
error_destroy_new_part:
ped_partition_destroy (new_part);
error:
ped_constraint_destroy (constraint_any);
return 0;
}
static int
mac_alloc_metadata (PedDisk* disk)
{
PED_ASSERT (disk != NULL);
PED_ASSERT (disk->disk_specific != NULL);
PED_ASSERT (disk->dev != NULL);
if (!add_metadata_part (disk, 0, 0))
return 0;
/* hack: this seems to be a good place, to update the partition map
* entry count, since mac_alloc_metadata() gets called during
* _disk_pop_update_mode()
*/
return _disk_count_partitions (disk);
}
static int
mac_get_max_primary_partition_count (const PedDisk* disk)
{
MacDiskData* mac_disk_data = disk->disk_specific;
PedPartition* part_map_partition;
part_map_partition = ped_disk_get_partition (disk,
mac_disk_data->part_map_entry_num);
/* HACK: if we haven't found the partition map partition (yet),
* we return this.
*/
if (!part_map_partition) {
mac_disk_data->part_map_entry_num = 0;
return 65536;
}
/* HACK: since Mac labels need an entry for free-space regions, we
* must allow half plus 1 entries for free-space partitions. I hate
* this, but things get REALLY complicated, otherwise.
* (I'm prepared to complicate things later, but I want to get
* everything working, first)
*/
return mac_disk_data->part_map_entry_count / mac_disk_data->ghost_size
- mac_disk_data->free_part_entry_count + 1;
}
static bool
mac_get_max_supported_partition_count (const PedDisk* disk, int *max_n)
{
*max_n = 65536;
return true;
}
#include "pt-common.h"
PT_define_limit_functions (mac)
static PedDiskOps mac_disk_ops = {
clobber: NULL,
/* FIXME: remove this cast, once mac_write is fixed not to
modify its *DISK parameter. */
write: NULL_IF_DISCOVER_ONLY ((int (*) (const PedDisk*)) mac_write),
partition_set_name: mac_partition_set_name,
partition_get_name: mac_partition_get_name,
get_partition_alignment: mac_get_partition_alignment,
PT_op_function_initializers (mac)
};
static PedDiskType mac_disk_type = {
next: NULL,
name: "mac",
ops: &mac_disk_ops,
features: PED_DISK_TYPE_PARTITION_NAME
};
void
ped_disk_mac_init ()
{
PED_ASSERT (sizeof (MacRawPartition) == 512);
PED_ASSERT (sizeof (MacRawDisk) == 512);
ped_disk_type_register (&mac_disk_type);
}
void
ped_disk_mac_done ()
{
ped_disk_type_unregister (&mac_disk_type);
}
|