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 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135
|
/*
libparted - a library for manipulating disk partitions
original version by Matt Domsch <Matt_Domsch@dell.com>
Disclaimed into the Public Domain
Portions Copyright (C) 2001-2003, 2005-2012 Free Software Foundation, Inc.
EFI GUID Partition Table handling
Per Intel EFI Specification v1.02
http://developer.intel.com/technology/efi/efi.htm
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 <parted/crc32.h>
#include <inttypes.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <uuid/uuid.h>
#include <stdbool.h>
#include <errno.h>
#include <iconv.h>
#include <langinfo.h>
#include "xalloc.h"
#include "xalloc-oversized.h"
#include "verify.h"
#include "pt-tools.h"
#if ENABLE_NLS
# include <libintl.h>
# define _(String) gettext (String)
#else
# define _(String) (String)
#endif /* ENABLE_NLS */
#define EFI_PMBR_OSTYPE_EFI 0xEE
#define MSDOS_MBR_SIGNATURE 0xaa55
#define GPT_HEADER_SIGNATURE 0x5452415020494645LL
/* NOTE: the document that describes revision 1.00 is labelled "version 1.02",
* so some implementors got confused...
*/
#define GPT_HEADER_REVISION_V1_02 0x00010200
#define GPT_HEADER_REVISION_V1_00 0x00010000
#define GPT_HEADER_REVISION_V0_99 0x00009900
typedef uint16_t efi_char16_t; /* UNICODE character */
typedef struct _GuidPartitionTableHeader_t GuidPartitionTableHeader_t;
typedef struct _GuidPartitionEntryAttributes_t GuidPartitionEntryAttributes_t;
typedef struct _GuidPartitionEntry_t GuidPartitionEntry_t;
typedef struct _PartitionRecord_t PartitionRecord_t;
typedef struct _LegacyMBR_t LegacyMBR_t;
typedef struct _GPTDiskData GPTDiskData;
typedef struct
{
uint32_t time_low;
uint16_t time_mid;
uint16_t time_hi_and_version;
uint8_t clock_seq_hi_and_reserved;
uint8_t clock_seq_low;
uint8_t node[6];
} /* __attribute__ ((packed)) */ efi_guid_t;
/* commented out "__attribute__ ((packed))" to work around gcc bug (fixed
* in gcc3.1): __attribute__ ((packed)) breaks addressing on initialized
* data. It turns out we don't need it in this case, so it doesn't break
* anything :)
*/
#define UNUSED_ENTRY_GUID \
((efi_guid_t) { 0x00000000, 0x0000, 0x0000, 0x00, 0x00, \
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }})
#define PARTITION_SYSTEM_GUID \
((efi_guid_t) { PED_CPU_TO_LE32 (0xC12A7328), PED_CPU_TO_LE16 (0xF81F), \
PED_CPU_TO_LE16 (0x11d2), 0xBA, 0x4B, \
{ 0x00, 0xA0, 0xC9, 0x3E, 0xC9, 0x3B }})
#define PARTITION_BIOS_GRUB_GUID \
((efi_guid_t) { PED_CPU_TO_LE32 (0x21686148), PED_CPU_TO_LE16 (0x6449), \
PED_CPU_TO_LE16 (0x6E6f), 0x74, 0x4E, \
{ 0x65, 0x65, 0x64, 0x45, 0x46, 0x49 }})
#define LEGACY_MBR_PARTITION_GUID \
((efi_guid_t) { PED_CPU_TO_LE32 (0x024DEE41), PED_CPU_TO_LE16 (0x33E7), \
PED_CPU_TO_LE16 (0x11d3, 0x9D, 0x69, \
{ 0x00, 0x08, 0xC7, 0x81, 0xF3, 0x9F }})
#define PARTITION_MSFT_RESERVED_GUID \
((efi_guid_t) { PED_CPU_TO_LE32 (0xE3C9E316), PED_CPU_TO_LE16 (0x0B5C), \
PED_CPU_TO_LE16 (0x4DB8), 0x81, 0x7D, \
{ 0xF9, 0x2D, 0xF0, 0x02, 0x15, 0xAE }})
#define PARTITION_MSFT_RECOVERY \
((efi_guid_t) { PED_CPU_TO_LE32 (0xDE94BBA4), PED_CPU_TO_LE16 (0x06D1), \
PED_CPU_TO_LE16 (0x4D40), 0xA1, 0x6A, \
{ 0xBF, 0xD5, 0x01, 0x79, 0xD6, 0xAC }})
#define PARTITION_BASIC_DATA_GUID \
((efi_guid_t) { PED_CPU_TO_LE32 (0xEBD0A0A2), PED_CPU_TO_LE16 (0xB9E5), \
PED_CPU_TO_LE16 (0x4433), 0x87, 0xC0, \
{ 0x68, 0xB6, 0xB7, 0x26, 0x99, 0xC7 }})
#define PARTITION_RAID_GUID \
((efi_guid_t) { PED_CPU_TO_LE32 (0xa19d880f), PED_CPU_TO_LE16 (0x05fc), \
PED_CPU_TO_LE16 (0x4d3b), 0xa0, 0x06, \
{ 0x74, 0x3f, 0x0f, 0x84, 0x91, 0x1e }})
#define PARTITION_SWAP_GUID \
((efi_guid_t) { PED_CPU_TO_LE32 (0x0657fd6d), PED_CPU_TO_LE16 (0xa4ab), \
PED_CPU_TO_LE16 (0x43c4), 0x84, 0xe5, \
{ 0x09, 0x33, 0xc8, 0x4b, 0x4f, 0x4f }})
#define PARTITION_LINUX_DATA_GUID \
((efi_guid_t) { PED_CPU_TO_LE32 (0x0FC63DAF), PED_CPU_TO_LE16 (0x8483), \
PED_CPU_TO_LE16 (0x4772), 0x8E, 0x79, \
{ 0x3D, 0x69, 0xD8, 0x47, 0x7D, 0xE4 }})
#define PARTITION_LVM_GUID \
((efi_guid_t) { PED_CPU_TO_LE32 (0xe6d6d379), PED_CPU_TO_LE16 (0xf507), \
PED_CPU_TO_LE16 (0x44c2), 0xa2, 0x3c, \
{ 0x23, 0x8f, 0x2a, 0x3d, 0xf9, 0x28 }})
#define PARTITION_RESERVED_GUID \
((efi_guid_t) { PED_CPU_TO_LE32 (0x8da63339), PED_CPU_TO_LE16 (0x0007), \
PED_CPU_TO_LE16 (0x60c0), 0xc4, 0x36, \
{ 0x08, 0x3a, 0xc8, 0x23, 0x09, 0x08 }})
#define PARTITION_HPSERVICE_GUID \
((efi_guid_t) { PED_CPU_TO_LE32 (0xe2a1e728), PED_CPU_TO_LE16 (0x32e3), \
PED_CPU_TO_LE16 (0x11d6), 0xa6, 0x82, \
{ 0x7b, 0x03, 0xa0, 0x00, 0x00, 0x00 }})
#define PARTITION_APPLE_HFS_GUID \
((efi_guid_t) { PED_CPU_TO_LE32 (0x48465300), PED_CPU_TO_LE16 (0x0000), \
PED_CPU_TO_LE16 (0x11AA), 0xaa, 0x11, \
{ 0x00, 0x30, 0x65, 0x43, 0xEC, 0xAC }})
#define PARTITION_APPLE_TV_RECOVERY_GUID \
((efi_guid_t) { PED_CPU_TO_LE32 (0x5265636F), PED_CPU_TO_LE16 (0x7665), \
PED_CPU_TO_LE16 (0x11AA), 0xaa, 0x11, \
{ 0x00, 0x30, 0x65, 0x43, 0xEC, 0xAC }})
#define PARTITION_PREP_GUID \
((efi_guid_t) { PED_CPU_TO_LE32 (0x9e1a2d38), PED_CPU_TO_LE16 (0xc612), \
PED_CPU_TO_LE16 (0x4316), 0xaa, 0x26, \
{ 0x8b, 0x49, 0x52, 0x1e, 0x5a, 0x8b }})
#define PARTITION_IRST_GUID \
((efi_guid_t) { PED_CPU_TO_LE32 (0xD3BFE2DE), PED_CPU_TO_LE16 (0x3DAF), \
PED_CPU_TO_LE16 (0x11DF), 0xba, 0x40, \
{ 0xE3, 0xA5, 0x56, 0xD8, 0x95, 0x93 }})
#define PARTITION_CHROMEOS_KERNEL_GUID \
((efi_guid_t) { PED_CPU_TO_LE32 (0xfe3a2a5d), PED_CPU_TO_LE16 (0x4f32), \
PED_CPU_TO_LE16 (0x41a7), 0xb7, 0x25, \
{ 0xac, 0xcc, 0x32, 0x85, 0xa3, 0x09 }})
#define PARTITION_BLS_BOOT_GUID \
((efi_guid_t) { PED_CPU_TO_LE32 (0xbc13c2ff), PED_CPU_TO_LE16 (0x59e6), \
PED_CPU_TO_LE16 (0x4262), 0xa3, 0x52, \
{ 0xb2, 0x75, 0xfd, 0x6f, 0x71, 0x72 }})
#define PARTITION_LINUX_HOME_GUID \
((efi_guid_t) { PED_CPU_TO_LE32 (0x933ac7e1), PED_CPU_TO_LE16 (0x2eb4), \
PED_CPU_TO_LE16 (0x4f13), 0xb8, 0x44, \
{ 0x0e, 0x14, 0xe2, 0xae, 0xf9, 0x15 }})
#define PARTITION_FREEBSD_UFS_GUID \
((efi_guid_t) { PED_CPU_TO_LE32 (0x516e7cb6), PED_CPU_TO_LE16 (0x6ecf), \
PED_CPU_TO_LE16 (0x11d6), 0x8f, 0xf8, \
{ 0x00, 0x02, 0x2d, 0x09, 0x71, 0x2b }})
struct flag_uuid_mapping_t
{
enum _PedPartitionFlag flag;
efi_guid_t type_uuid;
};
static const struct flag_uuid_mapping_t flag_uuid_mapping[] =
{
{ PED_PARTITION_APPLE_TV_RECOVERY, PARTITION_APPLE_TV_RECOVERY_GUID },
{ PED_PARTITION_BIOS_GRUB, PARTITION_BIOS_GRUB_GUID },
{ PED_PARTITION_BLS_BOOT, PARTITION_BLS_BOOT_GUID },
{ PED_PARTITION_BOOT, PARTITION_SYSTEM_GUID },
{ PED_PARTITION_CHROMEOS_KERNEL, PARTITION_CHROMEOS_KERNEL_GUID },
{ PED_PARTITION_DIAG, PARTITION_MSFT_RECOVERY },
{ PED_PARTITION_ESP, PARTITION_SYSTEM_GUID },
{ PED_PARTITION_HPSERVICE, PARTITION_HPSERVICE_GUID },
{ PED_PARTITION_IRST, PARTITION_IRST_GUID },
{ PED_PARTITION_LINUX_HOME, PARTITION_LINUX_HOME_GUID },
{ PED_PARTITION_LVM, PARTITION_LVM_GUID },
{ PED_PARTITION_MSFT_DATA, PARTITION_BASIC_DATA_GUID },
{ PED_PARTITION_MSFT_RESERVED, PARTITION_MSFT_RESERVED_GUID },
{ PED_PARTITION_PREP, PARTITION_PREP_GUID },
{ PED_PARTITION_RAID, PARTITION_RAID_GUID },
{ PED_PARTITION_SWAP, PARTITION_SWAP_GUID },
};
static const efi_guid_t skip_set_system_guids[] =
{
PARTITION_LVM_GUID,
PARTITION_SWAP_GUID,
PARTITION_RAID_GUID,
PARTITION_PREP_GUID,
PARTITION_SYSTEM_GUID,
PARTITION_BIOS_GRUB_GUID,
PARTITION_HPSERVICE_GUID,
PARTITION_MSFT_RESERVED_GUID,
PARTITION_BASIC_DATA_GUID,
PARTITION_MSFT_RECOVERY,
PARTITION_APPLE_TV_RECOVERY_GUID,
PARTITION_IRST_GUID,
PARTITION_CHROMEOS_KERNEL_GUID,
PARTITION_BLS_BOOT_GUID,
};
static const struct flag_uuid_mapping_t* _GL_ATTRIBUTE_CONST
gpt_find_flag_uuid_mapping (PedPartitionFlag flag)
{
int n = sizeof(flag_uuid_mapping) / sizeof(flag_uuid_mapping[0]);
for (int i = 0; i < n; ++i)
if (flag_uuid_mapping[i].flag == flag)
return &flag_uuid_mapping[i];
return NULL;
}
struct __attribute__ ((packed)) _GuidPartitionTableHeader_t
{
uint64_t Signature;
uint32_t Revision;
uint32_t HeaderSize;
uint32_t HeaderCRC32;
uint32_t Reserved1;
uint64_t MyLBA;
uint64_t AlternateLBA;
uint64_t FirstUsableLBA;
uint64_t LastUsableLBA;
efi_guid_t DiskGUID;
uint64_t PartitionEntryLBA;
uint32_t NumberOfPartitionEntries;
uint32_t SizeOfPartitionEntry;
uint32_t PartitionEntryArrayCRC32;
uint8_t *Reserved2;
};
struct __attribute__ ((packed)) _GuidPartitionEntryAttributes_t
{
#ifdef __GNUC__ /* XXX narrow this down to !TinyCC */
uint64_t RequiredToFunction:1;
uint64_t NoBlockIOProtocol:1;
uint64_t LegacyBIOSBootable:1;
uint64_t Reserved:45;
uint64_t GuidSpecific:15;
uint64_t NoAutomount:1;
#else
# warning "Using crippled partition entry type"
uint32_t RequiredToFunction:1;
uint32_t NoBlockIOProtocol:1;
uint32_t LegacyBIOSBootable:1;
uint32_t Reserved:30;
uint32_t LOST:5;
uint32_t GuidSpecific:15;
uint32_t NoAutomount:1;
#endif
};
struct __attribute__ ((packed)) _GuidPartitionEntry_t
{
efi_guid_t PartitionTypeGuid;
efi_guid_t UniquePartitionGuid;
uint64_t StartingLBA;
uint64_t EndingLBA;
GuidPartitionEntryAttributes_t Attributes;
efi_char16_t PartitionName[36];
};
#define GPT_PMBR_LBA 0
#define GPT_PMBR_SECTORS 1
#define GPT_PRIMARY_HEADER_LBA 1
#define GPT_HEADER_SECTORS 1
#define GPT_PRIMARY_PART_TABLE_LBA 2
/*
These values are only defaults. The actual on-disk structures
may define different sizes, so use those unless creating a new GPT disk!
*/
#define GPT_DEFAULT_PARTITION_ENTRY_ARRAY_SIZE 16384
/* Number of actual partition entries should be calculated as: */
#define GPT_DEFAULT_PARTITION_ENTRIES \
(GPT_DEFAULT_PARTITION_ENTRY_ARRAY_SIZE / \
sizeof(GuidPartitionEntry_t))
struct __attribute__ ((packed)) _PartitionRecord_t
{
/* Not used by EFI firmware. Set to 0x80 to indicate that this
is the bootable legacy partition. */
uint8_t BootIndicator;
/* Start of partition in CHS address, not used by EFI firmware. */
uint8_t StartHead;
/* Start of partition in CHS address, not used by EFI firmware. */
uint8_t StartSector;
/* Start of partition in CHS address, not used by EFI firmware. */
uint8_t StartTrack;
/* OS type. A value of 0xEF defines an EFI system partition.
Other values are reserved for legacy operating systems, and
allocated independently of the EFI specification. */
uint8_t OSType;
/* End of partition in CHS address, not used by EFI firmware. */
uint8_t EndHead;
/* End of partition in CHS address, not used by EFI firmware. */
uint8_t EndSector;
/* End of partition in CHS address, not used by EFI firmware. */
uint8_t EndTrack;
/* Starting LBA address of the partition on the disk. Used by
EFI firmware to define the start of the partition. */
uint32_t StartingLBA;
/* Size of partition in LBA. Used by EFI firmware to determine
the size of the partition. */
uint32_t SizeInLBA;
};
/* Protected Master Boot Record & Legacy MBR share same structure */
/* Needs to be packed because the u16s force misalignment. */
struct __attribute__ ((packed)) _LegacyMBR_t
{
uint8_t BootCode[440];
uint32_t UniqueMBRSignature;
uint16_t Unknown;
PartitionRecord_t PartitionRecord[4];
uint16_t Signature;
};
/* uses libparted's disk_specific field in PedDisk, to store our info */
struct __attribute__ ((packed, aligned(8))) _GPTDiskData
{
PedGeometry data_area;
int entry_count;
efi_guid_t uuid;
int pmbr_boot;
PedSector AlternateLBA;
};
/* uses libparted's disk_specific field in PedPartition, to store our info */
typedef struct _GPTPartitionData
{
efi_guid_t type;
efi_guid_t uuid;
efi_char16_t name[37];
char *translated_name;
GuidPartitionEntryAttributes_t attributes;
} GPTPartitionData;
static PedDiskType gpt_disk_type;
#if (defined(__i386__) || defined(__x86_64__)) && defined(__linux__)
# define USE_DMI
#endif
#define APPLE_DMI "Apple Computer, Inc."
#define APPLE_DMI_2 "Apple Inc."
static int is_apple = 0;
static char *
dmi_system_manufacturer (void)
{
#ifdef USE_DMI
FILE *dmidecode;
char *manufacturer = NULL;
size_t manufacturer_len = 0;
dmidecode = popen ("dmidecode -s system-manufacturer 2>/dev/null", "r");
if (getline (&manufacturer, &manufacturer_len, dmidecode) < 0) {
/* ignore; will return NULL */
}
pclose (dmidecode);
if (manufacturer) {
char *newline = strchr (manufacturer, '\n');
if (newline)
*newline = '\0';
}
return manufacturer;
#else /* !USE_DMI */
return NULL;
#endif /* USE_DMI */
}
static inline uint32_t
pth_get_size (const PedDevice *dev)
{
return GPT_HEADER_SECTORS * dev->sector_size;
}
static inline uint32_t
pth_get_size_static (const PedDevice *dev)
{
return sizeof (GuidPartitionTableHeader_t) - sizeof (uint8_t *);
}
static inline uint32_t
pth_get_size_rsv2 (const PedDevice *dev)
{
return pth_get_size (dev) - pth_get_size_static (dev);
}
static GuidPartitionTableHeader_t *
pth_new (const PedDevice *dev)
{
GuidPartitionTableHeader_t *pth =
ped_malloc (sizeof (GuidPartitionTableHeader_t) + sizeof (uint8_t));
pth->Reserved2 = ped_malloc (pth_get_size_rsv2 (dev));
return pth;
}
static GuidPartitionTableHeader_t *
pth_new_zeroed (const PedDevice *dev)
{
GuidPartitionTableHeader_t *pth = pth_new (dev);
memset (pth, 0, pth_get_size_static (dev));
memset (pth->Reserved2, 0, pth_get_size_rsv2 (dev));
return (pth);
}
static GuidPartitionTableHeader_t *
pth_new_from_raw (const PedDevice *dev, const uint8_t *pth_raw)
{
GuidPartitionTableHeader_t *pth = pth_new (dev);
PED_ASSERT (pth_raw != NULL);
memcpy (pth, pth_raw, pth_get_size_static (dev));
memcpy (pth->Reserved2, pth_raw + pth_get_size_static (dev),
pth_get_size_rsv2 (dev));
return pth;
}
static void
pth_free (GuidPartitionTableHeader_t *pth)
{
if (pth == NULL)
return;
PED_ASSERT (pth->Reserved2 != NULL);
free (pth->Reserved2);
free (pth);
}
static uint8_t *
pth_get_raw (const PedDevice *dev, const GuidPartitionTableHeader_t *pth)
{
PED_ASSERT (pth != NULL);
PED_ASSERT (pth->Reserved2 != NULL);
int size_static = pth_get_size_static (dev);
uint8_t *pth_raw = ped_malloc (pth_get_size (dev));
if (pth_raw == NULL)
return NULL;
memcpy (pth_raw, pth, size_static);
memcpy (pth_raw + size_static, pth->Reserved2, pth_get_size_rsv2 (dev));
return pth_raw;
}
/**
* swap_uuid_and_efi_guid() - converts between uuid formats
* @uuid - uuid_t in either format (converts it to the other)
*
* There are two different representations for Globally Unique Identifiers
* (GUIDs or UUIDs).
*
* The RFC specifies a UUID as a string of 16 bytes, essentially
* a big-endian array of char.
* Intel, in their EFI Specification, references the same RFC, but
* then defines a GUID as a structure of little-endian fields.
* Coincidentally, both structures have the same format when unparsed.
*
* When read from disk, EFI GUIDs are in struct of little endian format,
* and need to be converted to be treated as uuid_t in memory.
*
* When writing to disk, uuid_ts need to be converted into EFI GUIDs.
*
* Blame Intel.
*/
static void
swap_uuid_and_efi_guid (efi_guid_t *guid)
{
PED_ASSERT (guid != NULL);
guid->time_low = PED_SWAP32 (guid->time_low);
guid->time_mid = PED_SWAP16 (guid->time_mid);
guid->time_hi_and_version = PED_SWAP16 (guid->time_hi_and_version);
}
/* returns the EFI-style CRC32 value for buf
* This function uses the crc32 function by Gary S. Brown,
* but seeds the function with ~0, and xor's with ~0 at the end.
*/
static inline uint32_t
efi_crc32 (const void *buf, unsigned long len)
{
return (__efi_crc32 (buf, len, ~0L) ^ ~0L);
}
/* Compute the crc32 checksum of the partition table header
and store it in *CRC32. Return 0 upon success. Return 1
upon failure to allocate space. */
static int
pth_crc32 (const PedDevice *dev, const GuidPartitionTableHeader_t *pth,
uint32_t *crc32)
{
PED_ASSERT (dev != NULL);
PED_ASSERT (pth != NULL);
uint8_t *pth_raw = pth_get_raw (dev, pth);
if (pth_raw == NULL)
return 1;
*crc32 = efi_crc32 (pth_raw, PED_LE32_TO_CPU (pth->HeaderSize));
free (pth_raw);
return 0;
}
static inline int
guid_cmp (efi_guid_t left, efi_guid_t right)
{
return memcmp (&left, &right, sizeof (efi_guid_t));
}
/* checks if 'mbr' is a protective MBR partition table */
static inline int _GL_ATTRIBUTE_PURE
_pmbr_is_valid (const LegacyMBR_t *mbr)
{
int i;
PED_ASSERT (mbr != NULL);
if (mbr->Signature != PED_CPU_TO_LE16 (MSDOS_MBR_SIGNATURE))
return 0;
for (i = 0; i < 4; i++)
{
if (mbr->PartitionRecord[i].OSType == EFI_PMBR_OSTYPE_EFI)
return 1;
}
return 0;
}
static int
gpt_probe (const PedDevice *dev)
{
int gpt_sig_found = 0;
PED_ASSERT (dev != NULL);
if (dev->length <= 1)
return 0;
if (!is_apple)
{
void *label;
if (!ptt_read_sector (dev, 0, &label))
return 0;
if (!_pmbr_is_valid (label))
{
free (label);
return 0;
}
free (label);
}
void *pth_raw = ped_malloc (pth_get_size (dev));
if (ped_device_read (dev, pth_raw, 1, GPT_HEADER_SECTORS)
|| ped_device_read (dev, pth_raw, dev->length - 1, GPT_HEADER_SECTORS))
{
GuidPartitionTableHeader_t *gpt = pth_new_from_raw (dev, pth_raw);
if (gpt->Signature == PED_CPU_TO_LE64 (GPT_HEADER_SIGNATURE))
gpt_sig_found = 1;
pth_free (gpt);
}
free (pth_raw);
return gpt_sig_found;
}
static PedDisk *
gpt_alloc (const PedDevice *dev)
{
PedDisk *disk;
GPTDiskData *gpt_disk_data;
PedSector data_start, data_end;
disk = _ped_disk_alloc ((PedDevice *) dev, &gpt_disk_type);
if (!disk)
goto error;
data_start = 2 + GPT_DEFAULT_PARTITION_ENTRY_ARRAY_SIZE / dev->sector_size;
data_end = dev->length - 2
- GPT_DEFAULT_PARTITION_ENTRY_ARRAY_SIZE / dev->sector_size;
/* If the device is too small to accommodate GPT headers and one data
sector, reject it. */
if (data_end < data_start)
{
ped_exception_throw (PED_EXCEPTION_ERROR,
PED_EXCEPTION_OK,
_("device is too small for GPT"));
goto error_free_disk;
}
disk->disk_specific = gpt_disk_data = ped_malloc (sizeof (GPTDiskData));
if (!disk->disk_specific)
goto error_free_disk;
gpt_disk_data->AlternateLBA = dev->length - 1;
ped_geometry_init (&gpt_disk_data->data_area, dev, data_start,
data_end - data_start + 1);
gpt_disk_data->entry_count = GPT_DEFAULT_PARTITION_ENTRIES;
uuid_generate ((unsigned char *) &gpt_disk_data->uuid);
swap_uuid_and_efi_guid (&gpt_disk_data->uuid);
gpt_disk_data->pmbr_boot = 0;
return disk;
error_free_disk:
free (disk);
error:
return NULL;
}
static PedDisk *
gpt_duplicate (const PedDisk *disk)
{
PedDisk *new_disk;
GPTDiskData *new_disk_data;
GPTDiskData *old_disk_data;
new_disk = ped_disk_new_fresh (disk->dev, &gpt_disk_type);
if (!new_disk)
return NULL;
old_disk_data = disk->disk_specific;
new_disk_data = new_disk->disk_specific;
ped_geometry_init (&new_disk_data->data_area, disk->dev,
old_disk_data->data_area.start,
old_disk_data->data_area.length);
new_disk_data->entry_count = old_disk_data->entry_count;
new_disk_data->uuid = old_disk_data->uuid;
new_disk_data->pmbr_boot = old_disk_data->pmbr_boot;
return new_disk;
}
static void
gpt_free (PedDisk *disk)
{
ped_disk_delete_all (disk);
free (disk->disk_specific);
_ped_disk_free (disk);
}
/* Given GUID Partition table header, GPT, read its partition array
entries from DISK into malloc'd storage. Set *PTES_BYTES to the
number of bytes required. Upon success, return a pointer to the
resulting buffer. Otherwise, set errno and return NULL. */
static void *
gpt_read_PE_array (PedDisk const *disk, GuidPartitionTableHeader_t const *gpt,
size_t *ptes_bytes)
{
uint32_t p_ent_size = PED_LE32_TO_CPU (gpt->SizeOfPartitionEntry);
*ptes_bytes = p_ent_size * PED_LE32_TO_CPU(gpt->NumberOfPartitionEntries);
size_t ptes_sectors = ped_div_round_up (*ptes_bytes,
disk->dev->sector_size);
if (xalloc_oversized (ptes_sectors, disk->dev->sector_size))
{
errno = ENOMEM;
return NULL;
}
void *ptes = ped_malloc (ptes_sectors * disk->dev->sector_size);
if (ptes == NULL)
return NULL;
if (!ped_device_read (disk->dev, ptes,
PED_LE64_TO_CPU (gpt->PartitionEntryLBA), ptes_sectors))
{
int saved_errno = errno;
free (ptes);
errno = saved_errno;
return NULL;
}
return ptes;
}
static int
check_PE_array_CRC (PedDisk const *disk,
GuidPartitionTableHeader_t const *gpt, bool *valid)
{
size_t ptes_bytes;
void *ptes = gpt_read_PE_array (disk, gpt, &ptes_bytes);
if (ptes == NULL)
return 1;
uint32_t ptes_crc = efi_crc32 (ptes, ptes_bytes);
*valid = (ptes_crc == PED_LE32_TO_CPU (gpt->PartitionEntryArrayCRC32));
free (ptes);
return 0;
}
static int
_header_is_valid (PedDisk const *disk, GuidPartitionTableHeader_t *gpt,
PedSector my_lba)
{
uint32_t crc, origcrc;
PedDevice const *dev = disk->dev;
if (PED_LE64_TO_CPU (gpt->Signature) != GPT_HEADER_SIGNATURE)
return 0;
/*
* "While the GUID Partition Table Header's size may increase
* in the future it cannot span more than one block on the
* device." EFI Specification, version 1.10, 11.2.2.1
*/
if (PED_LE32_TO_CPU (gpt->HeaderSize) < pth_get_size_static (dev)
|| PED_LE32_TO_CPU (gpt->HeaderSize) > dev->sector_size)
return 0;
/* The SizeOfPartitionEntry must be a multiple of 8 and
no smaller than the size of the PartitionEntry structure.
We also require that it be no larger than 1/16th of UINT32_MAX,
as an additional sanity check. */
uint32_t pe_size = PED_LE32_TO_CPU (gpt->SizeOfPartitionEntry);
if (pe_size % 8 != 0
|| ! (sizeof (GuidPartitionEntry_t) <= pe_size
&& pe_size <= (UINT32_MAX >> 4)))
return 0;
if (PED_LE64_TO_CPU (gpt->MyLBA) != my_lba)
return 0;
PedSector alt_lba = PED_LE64_TO_CPU (gpt->AlternateLBA);
/* The backup table's AlternateLBA must be 1. */
if (my_lba != 1 && alt_lba != 1)
return 0;
/* The alt_lba must never be the same as my_lba. */
if (alt_lba == my_lba)
return 0;
bool crc_match;
if (check_PE_array_CRC (disk, gpt, &crc_match) != 0 || !crc_match)
return 0;
PedSector first_usable = PED_LE64_TO_CPU (gpt->FirstUsableLBA);
if (first_usable < 3)
return 0;
PedSector last_usable = PED_LE64_TO_CPU (gpt->LastUsableLBA);
if (last_usable < first_usable)
return 0;
origcrc = gpt->HeaderCRC32;
gpt->HeaderCRC32 = 0;
if (pth_crc32 (dev, gpt, &crc) != 0)
return 0;
gpt->HeaderCRC32 = origcrc;
return crc == PED_LE32_TO_CPU (origcrc);
}
/* Return the number of sectors that should be used by the
* partition entry table.
*/
static PedSector
_ptes_sectors(PedDisk const *disk, GuidPartitionTableHeader_t const *gpt)
{
size_t ptes_bytes = PED_LE32_TO_CPU (gpt->SizeOfPartitionEntry) *
PED_LE32_TO_CPU (gpt->NumberOfPartitionEntries);
/* Minimum amount of space reserved is 128 128 byte entries */
if (ptes_bytes < 128*128)
ptes_bytes = 128*128;
return ped_div_round_up (ptes_bytes, disk->dev->sector_size);
}
/* Return the header's idea of the last sector of the disk
* based on LastUsableLBA and the Partition Entry table.
*/
static PedSector
_hdr_disk_end(PedDisk const *disk, GuidPartitionTableHeader_t const *gpt)
{
return PED_LE64_TO_CPU (gpt->LastUsableLBA) + 1 + _ptes_sectors(disk, gpt);
}
static int
_parse_header (PedDisk *disk, const GuidPartitionTableHeader_t *gpt,
int *update_needed)
{
GPTDiskData *gpt_disk_data = disk->disk_specific;
PedSector first_usable;
PedSector last_usable;
PedSector last_usable_if_grown;
#ifndef DISCOVER_ONLY
if (PED_LE32_TO_CPU (gpt->Revision) > GPT_HEADER_REVISION_V1_02)
{
if (ped_exception_throw
(PED_EXCEPTION_WARNING,
PED_EXCEPTION_IGNORE_CANCEL,
_("The format of the GPT partition table is version "
"%x, which is newer than what Parted can "
"recognise. Please report this!"),
PED_LE32_TO_CPU (gpt->Revision)) != PED_EXCEPTION_IGNORE)
return 0;
}
#endif
first_usable = PED_LE64_TO_CPU (gpt->FirstUsableLBA);
last_usable = PED_LE64_TO_CPU (gpt->LastUsableLBA);
/* Need to check whether the volume has grown, the LastUsableLBA is
normally set to disk->dev->length - 2 - ptes_size (at least for parted
created volumes), where ptes_size is the number of entries *
size of each entry / sector size or 16k / sector size, whatever the greater.
If the volume has grown, offer the user the chance to use the new
space or continue with the current usable area. Only ask once per
parted invocation. */
last_usable_if_grown = disk->dev->length - 2 - _ptes_sectors(disk, gpt);
if (last_usable <= first_usable
|| disk->dev->length < last_usable)
return 0;
if (last_usable_if_grown <= first_usable
|| disk->dev->length < last_usable_if_grown)
return 0;
if (last_usable < last_usable_if_grown)
{
PedExceptionOption q;
q = ped_exception_throw
(PED_EXCEPTION_WARNING,
PED_EXCEPTION_FIX | PED_EXCEPTION_IGNORE,
_("Not all of the space available to %s appears "
"to be used, you can fix the GPT to use all of the "
"space (an extra %llu blocks) or continue with the "
"current setting? "), disk->dev->path,
(uint64_t) (last_usable_if_grown - last_usable));
if (q == PED_EXCEPTION_FIX)
{
last_usable = last_usable_if_grown;
/* clear the old backup gpt header */
ptt_clear_sectors (disk->dev,
gpt_disk_data->AlternateLBA, 1);
gpt_disk_data->AlternateLBA = disk->dev->length - 1;
*update_needed = 1;
}
}
ped_geometry_init (&gpt_disk_data->data_area, disk->dev,
first_usable, last_usable - first_usable + 1);
gpt_disk_data->entry_count
= PED_LE32_TO_CPU (gpt->NumberOfPartitionEntries);
PED_ASSERT (gpt_disk_data->entry_count > 0);
PED_ASSERT (gpt_disk_data->entry_count <= 8192);
gpt_disk_data->uuid = gpt->DiskGUID;
return 1;
}
static PedPartition *
_parse_part_entry (PedDisk *disk, GuidPartitionEntry_t *pte)
{
PedPartition *part;
GPTPartitionData *gpt_part_data;
unsigned int i;
part = ped_partition_new (disk, PED_PARTITION_NORMAL, NULL,
PED_LE64_TO_CPU (pte->StartingLBA),
PED_LE64_TO_CPU (pte->EndingLBA));
if (!part)
return NULL;
gpt_part_data = part->disk_specific;
gpt_part_data->type = pte->PartitionTypeGuid;
gpt_part_data->uuid = pte->UniquePartitionGuid;
for (i = 0; i < 36; i++)
gpt_part_data->name[i] = (efi_char16_t) pte->PartitionName[i];
gpt_part_data->name[i] = 0;
gpt_part_data->translated_name = 0;
gpt_part_data->attributes = pte->Attributes;
return part;
}
/* Read the primary GPT at sector 1 of DEV.
Verify its CRC and that of its partition entry array.
If they are valid, read the backup GPT specified by AlternateLBA.
If not, read the backup GPT in the last sector of the disk.
Return 1 if any read fails.
Upon successful verification of the primary GPT, set *PRIMARY_GPT, else NULL.
Upon successful verification of the backup GPT, set *BACKUP_GPT, else NULL.
If we've set *BACKUP_GPT to non-NULL, set *BACKUP_SECTOR_NUM_P to the sector
number in which it was found. */
static int
gpt_read_headers (PedDisk const *disk,
GuidPartitionTableHeader_t **primary_gpt,
GuidPartitionTableHeader_t **backup_gpt,
PedSector *backup_sector_num_p)
{
*primary_gpt = NULL;
*backup_gpt = NULL;
PedDevice const *dev = disk->dev;
GPTDiskData *gpt_disk_data = disk->disk_specific;
LegacyMBR_t *mbr;
if (!ptt_read_sector (dev, 0, (void *)&mbr))
return 1;
if (mbr->PartitionRecord[0].BootIndicator == 0x80)
gpt_disk_data->pmbr_boot = 1;
free (mbr);
void *s1;
if (!ptt_read_sector (dev, 1, &s1))
return 1;
GuidPartitionTableHeader_t *t = pth_new_from_raw (dev, s1);
free (s1);
if (t == NULL)
return 1;
GuidPartitionTableHeader_t *pri = t;
bool valid_primary = _header_is_valid (disk, pri, 1);
if (valid_primary)
*primary_gpt = pri;
else
pth_free (pri);
gpt_disk_data->AlternateLBA =
(valid_primary
? PED_LE64_TO_CPU (pri->AlternateLBA)
: dev->length - 1);
void *s_bak;
if (!ptt_read_sector (dev, gpt_disk_data->AlternateLBA ,&s_bak))
return 1;
t = pth_new_from_raw (dev, s_bak);
free (s_bak);
if (t == NULL)
return 1;
GuidPartitionTableHeader_t *bak = t;
if (_header_is_valid (disk, bak, gpt_disk_data->AlternateLBA))
{
*backup_gpt = bak;
*backup_sector_num_p = gpt_disk_data->AlternateLBA;
}
else
pth_free (bak);
return 0;
}
/************************************************************
* Intel is changing the EFI Spec. (after v1.02) to say that a
* disk is considered to have a GPT label only if the GPT
* structures are correct, and the MBR is actually a Protective
* MBR (has one 0xEE type partition).
* Problem occurs when a GPT-partitioned disk is then
* edited with a legacy (non-GPT-aware) application, such as
* fdisk (which doesn't generally erase the PGPT or AGPT).
* How should such a disk get handled? As a GPT disk (throwing
* away the fdisk changes), or as an MSDOS disk (throwing away
* the GPT information). Previously, I've taken the GPT-is-right,
* MBR is wrong, approach, to stay consistent with the EFI Spec.
* Intel disagrees, saying the disk should then be treated
* as having a msdos label, not a GPT label. If this is true,
* then what's the point of having an AGPT, since if the PGPT
* is screwed up, likely the PMBR is too, and the PMBR becomes
* a single point of failure.
* So, in the Linux kernel, I'm going to test for PMBR, and
* warn if it's not there, and treat the disk as MSDOS, with a note
* for users to use Parted to "fix up" their disk if they
* really want it to be considered GPT.
*
* Of course, this is incompatible with how Apple handle things. For
* legacy BIOS compatibility on Apple machines, we need a valid legacy MBR
* rather than a protective one. Aren't standards wonderful?
************************************************************/
static int
gpt_read (PedDisk *disk)
{
GPTDiskData *gpt_disk_data = disk->disk_specific;
int i;
#ifndef DISCOVER_ONLY
int write_back = 0;
#endif
ped_disk_delete_all (disk);
/* motivation: let the user decide about the pmbr... during
ped_disk_probe(), they probably didn't get a choice... */
if (!gpt_probe (disk->dev))
goto error;
GuidPartitionTableHeader_t *gpt = NULL;
GuidPartitionTableHeader_t *primary_gpt;
GuidPartitionTableHeader_t *backup_gpt;
PedSector backup_sector_num;
int read_failure = gpt_read_headers (disk, &primary_gpt, &backup_gpt,
&backup_sector_num);
if (read_failure)
{
/* This includes the case in which there used to be a GPT partition
table here, with an alternate LBA that extended beyond the current
end-of-device. It's treated as a non-match. */
/* Another possibility:
The primary header is ok, but backup is corrupt.
In the UEFI spec, this means the primary GUID table
is officially invalid. */
pth_free (backup_gpt);
pth_free (primary_gpt);
return 0;
}
if (primary_gpt && backup_gpt)
{
/* Both are valid. */
#ifndef DISCOVER_ONLY
/* The backup header must be at the end of the disk, or at what the primary
* header thinks is the end of the disk.
*/
gpt_disk_data->AlternateLBA = PED_LE64_TO_CPU (primary_gpt->AlternateLBA);
PedSector pri_disk_end = _hdr_disk_end(disk, primary_gpt);
if (gpt_disk_data->AlternateLBA != disk->dev->length -1 &&
gpt_disk_data->AlternateLBA != pri_disk_end)
{
if (ped_exception_throw
(PED_EXCEPTION_ERROR,
(PED_EXCEPTION_FIX | PED_EXCEPTION_IGNORE),
_("The backup GPT table is not at the end of the disk, as it "
"should be. Fix, by moving the backup to the end "
"(and removing the old backup)?")) == PED_EXCEPTION_FIX)
{
ptt_clear_sectors (disk->dev,
PED_LE64_TO_CPU (primary_gpt->AlternateLBA), 1);
gpt_disk_data->AlternateLBA = disk->dev->length -1;
write_back = 1;
}
}
#endif /* !DISCOVER_ONLY */
pth_free (backup_gpt);
gpt = primary_gpt;
}
else if (!primary_gpt && !backup_gpt)
{
/* Both are corrupt. */
ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
_("Both the primary and backup GPT tables "
"are corrupt. Try making a fresh table, "
"and using Parted's rescue feature to "
"recover partitions."));
goto error;
}
else if (primary_gpt && !backup_gpt)
{
/* The primary header is ok, but backup is corrupt. */
if (ped_exception_throw
(PED_EXCEPTION_ERROR, PED_EXCEPTION_OK_CANCEL,
_("The backup GPT table is corrupt, but the "
"primary appears OK, so that will be used."))
== PED_EXCEPTION_CANCEL)
goto error_free_gpt;
gpt = primary_gpt;
}
else /* !primary_gpt && backup_gpt */
{
/* primary GPT corrupt, backup is ok. */
if (ped_exception_throw
(PED_EXCEPTION_ERROR, PED_EXCEPTION_OK_CANCEL,
_("The primary GPT table is corrupt, but the "
"backup appears OK, so that will be used."))
== PED_EXCEPTION_CANCEL)
goto error_free_gpt;
gpt = backup_gpt;
}
backup_gpt = NULL;
primary_gpt = NULL;
if (!_parse_header (disk, gpt, &write_back))
goto error_free_gpt;
size_t ptes_bytes;
void *ptes = gpt_read_PE_array (disk, gpt, &ptes_bytes);
if (ptes == NULL)
goto error_free_gpt;
uint32_t ptes_crc = efi_crc32 (ptes, ptes_bytes);
if (ptes_crc != PED_LE32_TO_CPU (gpt->PartitionEntryArrayCRC32))
{
ped_exception_throw
(PED_EXCEPTION_ERROR,
PED_EXCEPTION_CANCEL,
_("primary partition table array CRC mismatch"));
goto error_free_ptes;
}
uint32_t p_ent_size = PED_LE32_TO_CPU (gpt->SizeOfPartitionEntry);
for (i = 0; i < gpt_disk_data->entry_count; i++)
{
GuidPartitionEntry_t *pte
= (GuidPartitionEntry_t *) ((char *) ptes + i * p_ent_size);
PedPartition *part;
if (!guid_cmp (pte->PartitionTypeGuid, UNUSED_ENTRY_GUID))
continue;
part = _parse_part_entry (disk, pte);
if (!part)
goto error_delete_all;
part->fs_type = ped_file_system_probe (&part->geom);
part->num = i + 1;
PedConstraint *constraint_exact = ped_constraint_exact (&part->geom);
if (!ped_disk_add_partition (disk, part, constraint_exact))
{
ped_constraint_destroy (constraint_exact);
ped_partition_destroy (part);
goto error_delete_all;
}
ped_constraint_destroy (constraint_exact);
}
free (ptes);
#ifndef DISCOVER_ONLY
if (write_back)
ped_disk_commit_to_dev (disk);
#endif
pth_free (gpt);
return 1;
error_delete_all:
ped_disk_delete_all (disk);
error_free_ptes:
free (ptes);
error_free_gpt:
pth_free (primary_gpt);
pth_free (backup_gpt);
pth_free (gpt);
error:
return 0;
}
#ifndef DISCOVER_ONLY
/* Write the protective MBR (to keep DOS happy) */
static int
_write_pmbr (PedDevice *dev, bool pmbr_boot)
{
/* The UEFI spec is not clear about what to do with the following
elements of the Protective MBR (pmbr): BootCode (0-440B),
UniqueMBRSignature (440B-444B) and Unknown (444B-446B).
With this in mind, we try not to modify these elements. */
void *s0;
if (!ptt_read_sector (dev, 0, &s0))
return 0;
LegacyMBR_t *pmbr = s0;
/* Zero out the legacy partitions. */
memset (pmbr->PartitionRecord, 0, sizeof pmbr->PartitionRecord);
pmbr->Signature = PED_CPU_TO_LE16 (MSDOS_MBR_SIGNATURE);
pmbr->PartitionRecord[0].OSType = EFI_PMBR_OSTYPE_EFI;
pmbr->PartitionRecord[0].StartSector = 2;
pmbr->PartitionRecord[0].EndHead = 0xFF;
pmbr->PartitionRecord[0].EndSector = 0xFF;
pmbr->PartitionRecord[0].EndTrack = 0xFF;
pmbr->PartitionRecord[0].StartingLBA = PED_CPU_TO_LE32 (1);
if ((dev->length - 1ULL) > 0xFFFFFFFFULL)
pmbr->PartitionRecord[0].SizeInLBA = PED_CPU_TO_LE32 (0xFFFFFFFF);
else
pmbr->PartitionRecord[0].SizeInLBA = PED_CPU_TO_LE32 (dev->length - 1UL);
if (pmbr_boot)
pmbr->PartitionRecord[0].BootIndicator = 0x80;
int write_ok = ped_device_write (dev, pmbr, GPT_PMBR_LBA,
GPT_PMBR_SECTORS);
free (s0);
return write_ok;
}
static void
fill_raw_part (PartitionRecord_t* raw_part, PedPartition *part, PedSector offset, int number)
{
GPTPartitionData* gpt_part_data = part->disk_specific;
if (part->fs_type) {
if (strncmp (part->fs_type->name, "fat", 3) == 0)
raw_part->OSType = 0x0b;
else if (strncmp (part->fs_type->name, "ntfs", 4) == 0)
raw_part->OSType = 0x07;
else if (strncmp (part->fs_type->name, "hfs", 3) == 0)
raw_part->OSType = 0xaf;
else if (strncmp (part->fs_type->name, "linux-swap", 10) == 0)
raw_part->OSType = 0x82;
else
raw_part->OSType = 0x83;
} else
raw_part->OSType = 0xda;
/* Apple's firmware appears to become unhappy if the second partition
isn't bootable */
if (number == 2)
raw_part->BootIndicator = 0x80;
raw_part->StartingLBA = PED_CPU_TO_LE32 ((part->geom.start - offset)
/ (part->disk->dev->sector_size / 512));
raw_part->SizeInLBA = PED_CPU_TO_LE32 (part->geom.length
/ (part->disk->dev->sector_size / 512));
/* EFI system partitions will have a FAT filesystem and
PARTITION_SYSTEM_GUID; however, it is not wise to rely on filesystem
probing */
if (number == 1) {
if (!guid_cmp (gpt_part_data->type, PARTITION_SYSTEM_GUID) ||
!guid_cmp (gpt_part_data->type, PARTITION_BIOS_GRUB_GUID)) {
raw_part->OSType = EFI_PMBR_OSTYPE_EFI;
raw_part->OSType = EFI_PMBR_OSTYPE_EFI;
}
}
/* Apple's firmware also appears to be unhappy if the EFI system
partition doesn't extend all the way to the start of the disk */
if (number == 1 && raw_part->OSType == EFI_PMBR_OSTYPE_EFI) {
raw_part->StartSector = 1;
raw_part->SizeInLBA += raw_part->StartingLBA - 1;
raw_part->StartingLBA = 1;
} else {
raw_part->StartHead = 0xfe;
raw_part->StartSector = 0xff;
raw_part->StartTrack = 0xff;
}
raw_part->EndHead = 0xfe;
raw_part->EndSector = 0xff;
raw_part->EndTrack = 0xff;
}
static int
_gptsync (const PedDisk *disk)
{
void *s0;
PedPartition* part;
int i;
if (!ptt_read_sector (disk->dev, GPT_PMBR_LBA, &s0))
return 0;
LegacyMBR_t *pmbr = s0;
int ok = 0;
memset(&pmbr->PartitionRecord, 0, sizeof(pmbr->PartitionRecord));
pmbr->Signature = PED_CPU_TO_LE16(MSDOS_MBR_SIGNATURE);
bool prot = false; /* have we found a protective partition? */
for (i=1; i<=4; i++) {
part = ped_disk_get_partition (disk, i);
if (!part)
continue;
fill_raw_part (&pmbr->PartitionRecord [i - 1], part, 0, i);
if (pmbr->PartitionRecord[i - 1].OSType == EFI_PMBR_OSTYPE_EFI)
prot = true;
}
if (!prot) { /* create one covering the gpt entries */
uint32_t prot_size;
for (i=2; i>=0; i--)
pmbr->PartitionRecord[i + 1] = pmbr->PartitionRecord[i];
memset (&pmbr->PartitionRecord[0], 0, sizeof pmbr->PartitionRecord[0]);
pmbr->PartitionRecord[0].OSType = EFI_PMBR_OSTYPE_EFI;
pmbr->PartitionRecord[0].StartSector = 1;
pmbr->PartitionRecord[0].EndHead = 0xfe;
pmbr->PartitionRecord[0].EndSector = 0xff;
pmbr->PartitionRecord[0].EndTrack = 0xff;
pmbr->PartitionRecord[0].StartingLBA = PED_CPU_TO_LE32 (1);
if ((disk->dev->length - 1ULL) > 0xFFFFFFFFULL)
prot_size = 0xFFFFFFFF;
else
prot_size = disk->dev->length - 1UL;
for (i=1; i<=3; i++) {
if (pmbr->PartitionRecord[i].StartingLBA) {
uint32_t starting_lba =
PED_LE32_TO_CPU (pmbr->PartitionRecord[i].StartingLBA);
if (starting_lba - 1 < prot_size)
prot_size = starting_lba - 1;
}
}
pmbr->PartitionRecord[0].SizeInLBA = PED_CPU_TO_LE32 (prot_size);
}
if (!ped_device_write (disk->dev, pmbr, GPT_PMBR_LBA, GPT_PMBR_SECTORS))
goto error;
ok = ped_device_sync (disk->dev);
error:
free (s0);
return ok;
}
static int
_generate_header (const PedDisk *disk, int alternate, uint32_t ptes_crc,
GuidPartitionTableHeader_t **gpt_p)
{
GPTDiskData *gpt_disk_data = disk->disk_specific;
GuidPartitionTableHeader_t *gpt;
*gpt_p = pth_new_zeroed (disk->dev);
gpt = *gpt_p;
gpt->Signature = PED_CPU_TO_LE64 (GPT_HEADER_SIGNATURE);
gpt->Revision = PED_CPU_TO_LE32 (GPT_HEADER_REVISION_V1_00);
/* per 1.00 spec */
gpt->HeaderSize = PED_CPU_TO_LE32 (pth_get_size_static (disk->dev));
gpt->HeaderCRC32 = 0;
gpt->Reserved1 = 0;
if (alternate)
{
size_t ss = disk->dev->sector_size;
PedSector ptes_bytes = (gpt_disk_data->entry_count
* sizeof (GuidPartitionEntry_t));
PedSector ptes_sectors = (ptes_bytes + ss - 1) / ss;
gpt->MyLBA = PED_CPU_TO_LE64 (gpt_disk_data->AlternateLBA);
gpt->AlternateLBA = PED_CPU_TO_LE64 (1);
gpt->PartitionEntryLBA
= PED_CPU_TO_LE64 (gpt_disk_data->AlternateLBA - ptes_sectors);
}
else
{
gpt->MyLBA = PED_CPU_TO_LE64 (1);
gpt->AlternateLBA = PED_CPU_TO_LE64 (gpt_disk_data->AlternateLBA);
gpt->PartitionEntryLBA = PED_CPU_TO_LE64 (2);
}
gpt->FirstUsableLBA = PED_CPU_TO_LE64 (gpt_disk_data->data_area.start);
gpt->LastUsableLBA = PED_CPU_TO_LE64 (gpt_disk_data->data_area.end);
gpt->DiskGUID = gpt_disk_data->uuid;
gpt->NumberOfPartitionEntries
= PED_CPU_TO_LE32 (gpt_disk_data->entry_count);
gpt->SizeOfPartitionEntry = PED_CPU_TO_LE32 (sizeof (GuidPartitionEntry_t));
gpt->PartitionEntryArrayCRC32 = PED_CPU_TO_LE32 (ptes_crc);
uint32_t crc;
if (pth_crc32 (disk->dev, gpt, &crc) != 0)
return 1;
gpt->HeaderCRC32 = PED_CPU_TO_LE32 (crc);
return 0;
}
static void
_partition_generate_part_entry (PedPartition *part, GuidPartitionEntry_t *pte)
{
GPTPartitionData *gpt_part_data = part->disk_specific;
unsigned int i;
PED_ASSERT (gpt_part_data != NULL);
pte->PartitionTypeGuid = gpt_part_data->type;
pte->UniquePartitionGuid = gpt_part_data->uuid;
pte->StartingLBA = PED_CPU_TO_LE64 (part->geom.start);
pte->EndingLBA = PED_CPU_TO_LE64 (part->geom.end);
pte->Attributes = gpt_part_data->attributes;
for (i = 0; i < 36; i++)
pte->PartitionName[i] = gpt_part_data->name[i];
}
static int
gpt_write (const PedDisk *disk)
{
GPTDiskData *gpt_disk_data;
uint32_t ptes_crc;
uint8_t *pth_raw;
GuidPartitionTableHeader_t *gpt;
PedPartition *part;
PED_ASSERT (disk != NULL);
PED_ASSERT (disk->dev != NULL);
PED_ASSERT (disk->disk_specific != NULL);
gpt_disk_data = disk->disk_specific;
size_t ptes_bytes = (gpt_disk_data->entry_count
* sizeof (GuidPartitionEntry_t));
size_t ss = disk->dev->sector_size;
PedSector ptes_sectors = (ptes_bytes + ss - 1) / ss;
/* Note that we allocate a little more than ptes_bytes,
when that number is not a multiple of sector size. */
GuidPartitionEntry_t *ptes = calloc (ptes_sectors, ss);
if (!ptes)
goto error;
for (part = ped_disk_next_partition (disk, NULL); part;
part = ped_disk_next_partition (disk, part))
{
if (part->type != 0)
continue;
_partition_generate_part_entry (part, &ptes[part->num - 1]);
}
ptes_crc = efi_crc32 (ptes, ptes_bytes);
if (is_apple) {
/* Write synced MBR */
if (!_gptsync (disk))
goto error_free_ptes;
} else {
/* Write protective MBR */
if (!_write_pmbr (disk->dev, gpt_disk_data->pmbr_boot))
goto error_free_ptes;
}
/* Write PTH and PTEs */
/* FIXME: Caution: this code is nearly identical to what's just below. */
if (_generate_header (disk, 0, ptes_crc, &gpt) != 0) {
pth_free(gpt);
goto error_free_ptes;
}
pth_raw = pth_get_raw (disk->dev, gpt);
pth_free (gpt);
if (pth_raw == NULL)
goto error_free_ptes;
int write_ok = ped_device_write (disk->dev, pth_raw, 1, 1);
free (pth_raw);
if (!write_ok)
goto error_free_ptes;
if (!ped_device_write (disk->dev, ptes, 2, ptes_sectors))
goto error_free_ptes;
/* Write Alternate PTH & PTEs */
/* FIXME: Caution: this code is nearly identical to what's just above. */
if (_generate_header (disk, 1, ptes_crc, &gpt) != 0) {
pth_free(gpt);
goto error_free_ptes;
}
pth_raw = pth_get_raw (disk->dev, gpt);
pth_free (gpt);
if (pth_raw == NULL)
goto error_free_ptes;
write_ok = ped_device_write (disk->dev, pth_raw, gpt_disk_data->AlternateLBA, 1);
free (pth_raw);
if (!write_ok)
goto error_free_ptes;
if (!ped_device_write (disk->dev, ptes,
gpt_disk_data->AlternateLBA - ptes_sectors, ptes_sectors))
goto error_free_ptes;
free (ptes);
return ped_device_sync (disk->dev);
error_free_ptes:
free (ptes);
error:
return 0;
}
#endif /* !DISCOVER_ONLY */
static int
add_metadata_part (PedDisk *disk, PedSector start, PedSector length)
{
PedPartition *part;
PedConstraint *constraint_exact;
PED_ASSERT (disk != NULL);
part = ped_partition_new (disk, PED_PARTITION_METADATA, NULL,
start, start + length - 1);
if (!part)
goto error;
constraint_exact = ped_constraint_exact (&part->geom);
if (!ped_disk_add_partition (disk, part, constraint_exact))
goto error_destroy_constraint;
ped_constraint_destroy (constraint_exact);
return 1;
error_destroy_constraint:
ped_constraint_destroy (constraint_exact);
ped_partition_destroy (part);
error:
return 0;
}
static PedPartition *
gpt_partition_new (const PedDisk *disk,
PedPartitionType part_type,
const PedFileSystemType *fs_type, PedSector start,
PedSector end)
{
PedPartition *part;
GPTPartitionData *gpt_part_data;
part = _ped_partition_alloc (disk, part_type, fs_type, start, end);
if (!part)
goto error;
if (part_type != 0)
return part;
gpt_part_data = part->disk_specific =
ped_malloc (sizeof (GPTPartitionData));
if (!gpt_part_data)
goto error_free_part;
gpt_part_data->type = PARTITION_LINUX_DATA_GUID;
gpt_part_data->translated_name = 0;
uuid_generate ((unsigned char *) &gpt_part_data->uuid);
swap_uuid_and_efi_guid (&gpt_part_data->uuid);
memset (gpt_part_data->name, 0, sizeof gpt_part_data->name);
memset (&gpt_part_data->attributes, 0, sizeof gpt_part_data->attributes);
return part;
error_free_part:
_ped_partition_free (part);
error:
return NULL;
}
static PedPartition *
gpt_partition_duplicate (const PedPartition *part)
{
PedPartition *result;
GPTPartitionData *part_data = part->disk_specific;
GPTPartitionData *result_data;
result = _ped_partition_alloc (part->disk, part->type, part->fs_type,
part->geom.start, part->geom.end);
if (!result)
goto error;
result->num = part->num;
if (result->type != 0)
return result;
result_data = result->disk_specific =
ped_malloc (sizeof (GPTPartitionData));
if (!result_data)
goto error_free_part;
*result_data = *part_data;
if (part_data->translated_name) {
result_data->translated_name = xstrdup (part_data->translated_name);
} else {
result_data->translated_name = 0;
}
return result;
error_free_part:
_ped_partition_free (result);
error:
return NULL;
}
static void
gpt_partition_destroy (PedPartition *part)
{
if (part->type == 0)
{
PED_ASSERT (part->disk_specific != NULL);
GPTPartitionData *gpt_part_data = part->disk_specific;
free (gpt_part_data->translated_name);
free (part->disk_specific);
}
_ped_partition_free (part);
}
/* is_skip_guid checks the guid against the list of guids that should not be
* overridden by set_system. It returns a 1 if it is in the list.
*/
static bool
is_skip_guid(efi_guid_t guid) {
int n = sizeof(skip_set_system_guids) / sizeof(skip_set_system_guids[0]);
for (int i = 0; i < n; ++i) {
if (guid_cmp(guid, skip_set_system_guids[i]) == 0) {
return true;
}
}
return false;
}
static int
gpt_partition_set_system (PedPartition *part,
const PedFileSystemType *fs_type)
{
GPTPartitionData *gpt_part_data = part->disk_specific;
PED_ASSERT (gpt_part_data != NULL);
part->fs_type = fs_type;
// Is this a GUID that should skip fs_type checking?
if (is_skip_guid(gpt_part_data->type)) {
return 1;
}
if (fs_type)
{
if (strncmp (fs_type->name, "fat", 3) == 0
|| strcmp (fs_type->name, "udf") == 0
|| strcmp (fs_type->name, "ntfs") == 0)
{
gpt_part_data->type = PARTITION_BASIC_DATA_GUID;
return 1;
}
if (strncmp (fs_type->name, "hfs", 3) == 0)
{
gpt_part_data->type = PARTITION_APPLE_HFS_GUID;
return 1;
}
if (strstr (fs_type->name, "swap"))
{
gpt_part_data->type = PARTITION_SWAP_GUID;
return 1;
}
if (strstr (fs_type->name, "freebsd-ufs"))
{
gpt_part_data->type = PARTITION_FREEBSD_UFS_GUID;
return 1;
}
}
gpt_part_data->type = PARTITION_LINUX_DATA_GUID;
return 1;
}
/* Allocate metadata partitions for the GPTH and PTES */
static int
gpt_alloc_metadata (PedDisk *disk)
{
PedSector gptlength, pteslength = 0;
GPTDiskData *gpt_disk_data;
PED_ASSERT (disk != NULL);
PED_ASSERT (disk->dev != NULL);
PED_ASSERT (disk->disk_specific != NULL);
gpt_disk_data = disk->disk_specific;
gptlength = ped_div_round_up (sizeof (GuidPartitionTableHeader_t),
disk->dev->sector_size);
pteslength = ped_div_round_up (gpt_disk_data->entry_count
* sizeof (GuidPartitionEntry_t),
disk->dev->sector_size);
/* metadata at the start of the disk includes the MBR */
if (!add_metadata_part (disk, GPT_PMBR_LBA,
GPT_PMBR_SECTORS + gptlength + pteslength))
return 0;
/* metadata at the end of the disk */
if (!add_metadata_part (disk, disk->dev->length - gptlength - pteslength,
gptlength + pteslength))
return 0;
return 1;
}
/* Does nothing, as the read/new/destroy functions maintain part->num */
static int
gpt_partition_enumerate (PedPartition *part)
{
GPTDiskData *gpt_disk_data = part->disk->disk_specific;
int i;
/* never change the partition numbers */
if (part->num != -1)
return 1;
for (i = 1; i <= gpt_disk_data->entry_count; i++)
{
if (!ped_disk_get_partition (part->disk, i))
{
part->num = i;
return 1;
}
}
PED_ASSERT (0);
return 0; /* used if debug is disabled */
}
static int
gpt_disk_set_flag (PedDisk *disk, PedDiskFlag flag, int state)
{
GPTDiskData *gpt_disk_data = disk->disk_specific;
switch (flag)
{
case PED_DISK_GPT_PMBR_BOOT:
gpt_disk_data->pmbr_boot = state;
return 1;
default:
return 0;
}
}
static int
gpt_disk_is_flag_available(const PedDisk *disk, PedDiskFlag flag)
{
switch (flag)
{
case PED_DISK_GPT_PMBR_BOOT:
return 1;
default:
return 0;
}
}
static uint8_t*
gpt_disk_get_uuid (const PedDisk *disk)
{
GPTDiskData *gpt_disk_data = disk->disk_specific;
efi_guid_t uuid = gpt_disk_data->uuid;
/* uuid is always LE, while uint8_t is always kind of BE */
uuid.time_low = PED_SWAP32(uuid.time_low);
uuid.time_mid = PED_SWAP16(uuid.time_mid);
uuid.time_hi_and_version = PED_SWAP16(uuid.time_hi_and_version);
uint8_t *buf = ped_malloc(sizeof (uuid_t));
memcpy(buf, &uuid, sizeof (uuid_t));
return buf;
}
static int
gpt_disk_get_flag (const PedDisk *disk, PedDiskFlag flag)
{
GPTDiskData *gpt_disk_data = disk->disk_specific;
switch (flag)
{
case PED_DISK_GPT_PMBR_BOOT:
return gpt_disk_data->pmbr_boot;
break;
default:
return 0;
}
}
static int
gpt_partition_set_flag (PedPartition *part, PedPartitionFlag flag, int state)
{
GPTPartitionData *gpt_part_data;
PED_ASSERT (part != NULL);
PED_ASSERT (part->disk_specific != NULL);
gpt_part_data = part->disk_specific;
const struct flag_uuid_mapping_t* p = gpt_find_flag_uuid_mapping (flag);
if (p)
{
if (state) {
gpt_part_data->type = p->type_uuid;
} else if (guid_cmp (gpt_part_data->type, p->type_uuid) == 0) {
// Clear the GUID so that fs_type will be used to return it to the default
gpt_part_data->type = PARTITION_LINUX_DATA_GUID;
return gpt_partition_set_system (part, part->fs_type);
}
return 1;
}
switch (flag)
{
case PED_PARTITION_HIDDEN:
gpt_part_data->attributes.RequiredToFunction = state;
return 1;
case PED_PARTITION_LEGACY_BOOT:
gpt_part_data->attributes.LegacyBIOSBootable = state;
return 1;
case PED_PARTITION_NO_AUTOMOUNT:
gpt_part_data->attributes.NoAutomount = state;
return 1;
case PED_PARTITION_ROOT:
case PED_PARTITION_LBA:
default:
return 0;
}
return 1;
}
static int _GL_ATTRIBUTE_PURE
gpt_partition_get_flag (const PedPartition *part, PedPartitionFlag flag)
{
GPTPartitionData *gpt_part_data;
PED_ASSERT (part->disk_specific != NULL);
gpt_part_data = part->disk_specific;
const struct flag_uuid_mapping_t* p = gpt_find_flag_uuid_mapping (flag);
if (p)
return guid_cmp (gpt_part_data->type, p->type_uuid) == 0;
switch (flag)
{
case PED_PARTITION_HIDDEN:
return gpt_part_data->attributes.RequiredToFunction;
case PED_PARTITION_LEGACY_BOOT:
return gpt_part_data->attributes.LegacyBIOSBootable;
case PED_PARTITION_NO_AUTOMOUNT:
return gpt_part_data->attributes.NoAutomount;
case PED_PARTITION_LBA:
case PED_PARTITION_ROOT:
default:
return 0;
}
return 0;
}
static int
gpt_partition_is_flag_available (const PedPartition *part,
PedPartitionFlag flag)
{
if (gpt_find_flag_uuid_mapping (flag))
return 1;
switch (flag)
{
case PED_PARTITION_HIDDEN:
case PED_PARTITION_LEGACY_BOOT:
case PED_PARTITION_NO_AUTOMOUNT:
return 1;
case PED_PARTITION_ROOT:
case PED_PARTITION_LBA:
default:
return 0;
}
return 0;
}
static void
gpt_partition_set_name (PedPartition *part, const char *name)
{
GPTPartitionData *gpt_part_data = part->disk_specific;
free(gpt_part_data->translated_name);
gpt_part_data->translated_name = xstrdup(name);
iconv_t conv = iconv_open ("UCS-2LE", nl_langinfo (CODESET));
if (conv == (iconv_t)-1)
goto err;
char *inbuff = gpt_part_data->translated_name;
char *outbuff = (char *)&gpt_part_data->name;
size_t inbuffsize = strlen (inbuff) + 1;
size_t outbuffsize = 72;
if (iconv (conv, &inbuff, &inbuffsize, &outbuff, &outbuffsize) == -1)
goto err;
iconv_close (conv);
return;
err:
ped_exception_throw (PED_EXCEPTION_WARNING,
PED_EXCEPTION_IGNORE,
_("failed to translate partition name"));
iconv_close (conv);
}
static const char *
gpt_partition_get_name (const PedPartition *part)
{
GPTPartitionData *gpt_part_data = part->disk_specific;
if (gpt_part_data->translated_name == NULL)
{
char buffer[200];
iconv_t conv = iconv_open (nl_langinfo (CODESET), "UCS-2LE");
if (conv == (iconv_t)-1)
goto err;
char *inbuff = (char *)&gpt_part_data->name;
char *outbuff = buffer;
size_t inbuffsize = 72;
size_t outbuffsize = sizeof(buffer);
if (iconv (conv, &inbuff, &inbuffsize, &outbuff, &outbuffsize) == -1)
goto err;
iconv_close (conv);
*outbuff = 0;
gpt_part_data->translated_name = xstrdup (buffer);
return gpt_part_data->translated_name;
err:
ped_exception_throw (PED_EXCEPTION_WARNING,
PED_EXCEPTION_IGNORE,
_("failed to translate partition name"));
iconv_close (conv);
return "";
}
return gpt_part_data->translated_name;
}
static int
gpt_partition_set_type_uuid (PedPartition *part, const uint8_t *uuid)
{
GPTPartitionData *gpt_part_data = part->disk_specific;
efi_guid_t* type_uuid = &gpt_part_data->type;
memcpy(type_uuid, uuid, sizeof (efi_guid_t));
/* type_uuid is always LE, while uint8_t is always kind of BE */
type_uuid->time_low = PED_SWAP32(type_uuid->time_low);
type_uuid->time_mid = PED_SWAP16(type_uuid->time_mid);
type_uuid->time_hi_and_version = PED_SWAP16(type_uuid->time_hi_and_version);
return 1;
}
static uint8_t*
gpt_partition_get_type_uuid (const PedPartition *part)
{
const GPTPartitionData *gpt_part_data = part->disk_specific;
efi_guid_t type_uuid = gpt_part_data->type;
/* type_uuid is always LE, while uint8_t is always kind of BE */
type_uuid.time_low = PED_SWAP32(type_uuid.time_low);
type_uuid.time_mid = PED_SWAP16(type_uuid.time_mid);
type_uuid.time_hi_and_version = PED_SWAP16(type_uuid.time_hi_and_version);
uint8_t *buf = ped_malloc(sizeof (uuid_t));
memcpy(buf, &type_uuid, sizeof (uuid_t));
return buf;
}
static uint8_t*
gpt_partition_get_uuid (const PedPartition *part)
{
const GPTPartitionData *gpt_part_data = part->disk_specific;
efi_guid_t uuid = gpt_part_data->uuid;
/* uuid is always LE, while uint8_t is always kind of BE */
uuid.time_low = PED_SWAP32(uuid.time_low);
uuid.time_mid = PED_SWAP16(uuid.time_mid);
uuid.time_hi_and_version = PED_SWAP16(uuid.time_hi_and_version);
uint8_t *buf = ped_malloc(sizeof (uuid_t));
memcpy(buf, &uuid, sizeof (uuid_t));
return buf;
}
static int
gpt_get_max_primary_partition_count (const PedDisk *disk)
{
const GPTDiskData *gpt_disk_data = disk->disk_specific;
return gpt_disk_data->entry_count;
}
/*
* From (http://developer.apple.com/technotes/tn2006/tn2166.html Chapter 5).
* According to the specs the first LBA (LBA0) is not relevant (it exists
* to maintain compatibility). on the second LBA(LBA1) gpt places the
* header. The header is as big as the block size. After the header we
* find the Entry array. Each element of said array, describes each
* partition. One can have as much elements as can fit between the end of
* the second LBA (where the header ends) and the FirstUsableLBA.
* FirstUsableLBA is the first logical block that is used for contents
* and is defined in header.
*
* /---------------------------------------------------\
* | BLOCK0 | HEADER | Entry Array | First Usable LBA |
* | | BLOCK1 | | |
* \---------------------------------------------------/
* / \
* /----------/ \----------\
* /-----------------------------------------\
* | E1 | E2 | E3 |...............| EN |
* \-----------------------------------------/
*
* The number of possible partitions or supported partitions is:
* SP = FirstUsableLBA*Blocksize - 2*Blocksize / SizeOfPartitionEntry
* SP = Blocksize(FirstusableLBA - 2) / SizeOfPartitoinEntry
*/
static bool
gpt_get_max_supported_partition_count (const PedDisk *disk, int *max_n)
{
GuidPartitionTableHeader_t *pth = NULL;
uint8_t *pth_raw = ped_malloc (pth_get_size (disk->dev));
if (ped_device_read (disk->dev, pth_raw, 1, GPT_HEADER_SECTORS)
|| ped_device_read (disk->dev, pth_raw,
disk->dev->length, GPT_HEADER_SECTORS))
pth = pth_new_from_raw (disk->dev, pth_raw);
free (pth_raw);
if (pth == NULL)
return false;
if (!_header_is_valid (disk, pth, 1))
{
pth->FirstUsableLBA = PED_CPU_TO_LE64 (34);
pth->SizeOfPartitionEntry
= PED_CPU_TO_LE32 (sizeof (GuidPartitionEntry_t));
}
*max_n = (disk->dev->sector_size * (PED_LE64_TO_CPU (pth->FirstUsableLBA) - 2)
/ PED_LE32_TO_CPU (pth->SizeOfPartitionEntry));
pth_free (pth);
return true;
}
static PedConstraint *
_non_metadata_constraint (const PedDisk *disk)
{
GPTDiskData *gpt_disk_data = disk->disk_specific;
return ped_constraint_new_from_max (&gpt_disk_data->data_area);
}
static int
gpt_partition_align (PedPartition *part, const PedConstraint *constraint)
{
PED_ASSERT (part != NULL);
if (_ped_partition_attempt_align (part, constraint,
_non_metadata_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;
}
#include "pt-common.h"
PT_define_limit_functions (gpt)
static PedDiskOps gpt_disk_ops =
{
clobber: NULL,
write: NULL_IF_DISCOVER_ONLY (gpt_write),
partition_set_name: gpt_partition_set_name,
partition_get_name: gpt_partition_get_name,
partition_set_type_id: NULL,
partition_get_type_id: NULL,
partition_set_type_uuid: gpt_partition_set_type_uuid,
partition_get_type_uuid: gpt_partition_get_type_uuid,
partition_get_uuid: gpt_partition_get_uuid,
disk_set_flag: gpt_disk_set_flag,
disk_get_flag: gpt_disk_get_flag,
disk_is_flag_available: gpt_disk_is_flag_available,
disk_get_uuid: gpt_disk_get_uuid,
PT_op_function_initializers (gpt)
};
static PedDiskType gpt_disk_type =
{
next: NULL,
name: "gpt",
ops: &gpt_disk_ops,
features: PED_DISK_TYPE_PARTITION_NAME | PED_DISK_TYPE_PARTITION_TYPE_UUID |
PED_DISK_TYPE_DISK_UUID | PED_DISK_TYPE_PARTITION_UUID
};
void
ped_disk_gpt_init ()
{
ped_disk_type_register (&gpt_disk_type);
char *force_gpt_apple = getenv ("PARTED_GPT_APPLE");
if (force_gpt_apple) {
if (strcmp (force_gpt_apple, "1") == 0)
is_apple = 1;
} else {
char *manufacturer = dmi_system_manufacturer ();
if (manufacturer &&
(strncasecmp (APPLE_DMI, manufacturer,
strlen (APPLE_DMI)) == 0 ||
strncasecmp (APPLE_DMI_2, manufacturer,
strlen (APPLE_DMI_2)) == 0))
is_apple = 1;
free (manufacturer);
}
}
void
ped_disk_gpt_done ()
{
ped_disk_type_unregister (&gpt_disk_type);
}
verify (sizeof (GuidPartitionEntryAttributes_t) == 8);
verify (sizeof (GuidPartitionEntry_t) == 128);
|