Home | History | Annotate | Download | only in gptfdisk
      1 /* gpt.h -- GPT and data structure definitions, types, and
      2    functions */
      3 
      4 /* This program is copyright (c) 2009-2011 by Roderick W. Smith. It is distributed
      5   under the terms of the GNU GPL version 2, as detailed in the COPYING file. */
      6 
      7 #include <stdint.h>
      8 #include <sys/types.h>
      9 #include "gptpart.h"
     10 #include "support.h"
     11 #include "mbr.h"
     12 #include "bsd.h"
     13 #include "gptpart.h"
     14 
     15 #ifndef __GPTSTRUCTS
     16 #define __GPTSTRUCTS
     17 
     18 // Default values for sector alignment
     19 #define DEFAULT_ALIGNMENT 2048
     20 #define MAX_ALIGNMENT 65536
     21 #define MIN_AF_ALIGNMENT 8
     22 
     23 // Below constant corresponds to a ~279GiB (300GB) disk, since the
     24 // smallest Advanced Format drive I know of is 320GB in size
     25 #define SMALLEST_ADVANCED_FORMAT UINT64_C(585937500)
     26 
     27 using namespace std;
     28 
     29 /****************************************
     30  *                                      *
     31  * GPTData class and related structures *
     32  *                                      *
     33  ****************************************/
     34 
     35 // Validity state of GPT data
     36 enum GPTValidity {gpt_valid, gpt_corrupt, gpt_invalid};
     37 
     38 // Which set of partition data to use
     39 enum WhichToUse {use_gpt, use_mbr, use_bsd, use_new, use_abort};
     40 
     41 // Header (first 512 bytes) of GPT table
     42 #pragma pack(1)
     43 struct GPTHeader {
     44    uint64_t signature;
     45    uint32_t revision;
     46    uint32_t headerSize;
     47    uint32_t headerCRC;
     48    uint32_t reserved;
     49    uint64_t currentLBA;
     50    uint64_t backupLBA;
     51    uint64_t firstUsableLBA;
     52    uint64_t lastUsableLBA;
     53    GUIDData diskGUID;
     54    uint64_t partitionEntriesLBA;
     55    uint32_t numParts;
     56    uint32_t sizeOfPartitionEntries;
     57    uint32_t partitionEntriesCRC;
     58    unsigned char reserved2[GPT_RESERVED];
     59 }; // struct GPTHeader
     60 
     61 // Data in GPT format
     62 class GPTData {
     63 protected:
     64    struct GPTHeader mainHeader;
     65    GPTPart *partitions;
     66    uint32_t numParts; // # of partitions the table can hold
     67    struct GPTHeader secondHeader;
     68    MBRData protectiveMBR;
     69    string device; // device filename
     70    DiskIO myDisk;
     71    uint32_t blockSize; // device block size
     72    uint64_t diskSize; // size of device, in blocks
     73    GPTValidity state; // is GPT valid?
     74    int justLooking; // Set to 1 if program launched with "-l" or if read-only
     75    bool syncing; // Set to true if we should sync and reload the partition table
     76    int mainCrcOk;
     77    int secondCrcOk;
     78    int mainPartsCrcOk;
     79    int secondPartsCrcOk;
     80    int apmFound; // set to 1 if APM detected
     81    int bsdFound; // set to 1 if BSD disklabel detected in MBR
     82    uint32_t sectorAlignment; // Start partitions at multiples of sectorAlignment
     83    int beQuiet;
     84    WhichToUse whichWasUsed;
     85 
     86    int LoadHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector, int *crcOk);
     87    int LoadPartitionTable(const struct GPTHeader & header, DiskIO & disk, uint64_t sector = 0);
     88    int CheckTable(struct GPTHeader *header);
     89    int SaveHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector);
     90    int SavePartitionTable(DiskIO & disk, uint64_t sector);
     91 public:
     92    // Basic necessary functions....
     93    GPTData(void);
     94    GPTData(string deviceFilename);
     95    virtual ~GPTData(void);
     96    GPTData & operator=(const GPTData & orig);
     97 
     98    // Verify (or update) data integrity
     99    int Verify(void);
    100    int CheckGPTSize(void);
    101    int CheckHeaderValidity(void);
    102    int CheckHeaderCRC(struct GPTHeader* header, int warn = 0);
    103    void RecomputeCRCs(void);
    104    void RebuildMainHeader(void);
    105    void RebuildSecondHeader(void);
    106    int VerifyMBR(void) {return protectiveMBR.FindOverlaps();}
    107    int FindHybridMismatches(void);
    108    int FindOverlaps(void);
    109    int FindInsanePartitions(void);
    110 
    111    // Load or save data from/to disk
    112    int SetDisk(const string & deviceFilename);
    113    DiskIO* GetDisk(void) {return &myDisk;}
    114    int LoadMBR(const string & f) {return protectiveMBR.ReadMBRData(f);}
    115    int WriteProtectiveMBR(void) {return protectiveMBR.WriteMBRData(&myDisk);}
    116    void PartitionScan(void);
    117    int LoadPartitions(const string & deviceFilename);
    118    int ForceLoadGPTData(void);
    119    int LoadMainTable(void);
    120    int LoadSecondTableAsMain(void);
    121    int SaveGPTData(int quiet = 0);
    122    int SaveGPTBackup(const string & filename);
    123    int LoadGPTBackup(const string & filename);
    124    int SaveMBR(void);
    125    int DestroyGPT(void);
    126    int DestroyMBR(void);
    127 
    128    // Display data....
    129    void ShowAPMState(void);
    130    void ShowGPTState(void);
    131    void DisplayGPTData(void);
    132    void DisplayMBRData(void) {protectiveMBR.DisplayMBRData();}
    133    void ShowPartDetails(uint32_t partNum);
    134 
    135    // Convert between GPT and other formats
    136    virtual WhichToUse UseWhichPartitions(void);
    137    void XFormPartitions(void);
    138    int XFormDisklabel(uint32_t partNum);
    139    int XFormDisklabel(BSDData* disklabel);
    140    int OnePartToMBR(uint32_t gptPart, int mbrPart); // add one partition to MBR. Returns 1 if successful
    141 
    142    // Adjust GPT structures WITHOUT user interaction...
    143    int SetGPTSize(uint32_t numEntries, int fillGPTSectors = 1);
    144    void BlankPartitions(void);
    145    int DeletePartition(uint32_t partNum);
    146    uint32_t CreatePartition(uint32_t partNum, uint64_t startSector, uint64_t endSector);
    147    void SortGPT(void);
    148    int SwapPartitions(uint32_t partNum1, uint32_t partNum2);
    149    int ClearGPTData(void);
    150    void MoveSecondHeaderToEnd();
    151    int SetName(uint32_t partNum, const UnicodeString & theName);
    152    void SetDiskGUID(GUIDData newGUID);
    153    int SetPartitionGUID(uint32_t pn, GUIDData theGUID);
    154    void RandomizeGUIDs(void);
    155    int ChangePartType(uint32_t pn, PartType theGUID);
    156    void MakeProtectiveMBR(void) {protectiveMBR.MakeProtectiveMBR();}
    157    void RecomputeCHS(void);
    158    int Align(uint64_t* sector);
    159    void SetProtectiveMBR(BasicMBRData & newMBR) {protectiveMBR = newMBR;}
    160 
    161    // Return data about the GPT structures....
    162    WhichToUse GetState(void) {return whichWasUsed;}
    163    int GetPartRange(uint32_t* low, uint32_t* high);
    164    int FindFirstFreePart(void);
    165    uint32_t GetNumParts(void) {return mainHeader.numParts;}
    166    uint64_t GetMainHeaderLBA(void) {return mainHeader.currentLBA;}
    167    uint64_t GetSecondHeaderLBA(void) {return secondHeader.currentLBA;}
    168    uint64_t GetMainPartsLBA(void) {return mainHeader.partitionEntriesLBA;}
    169    uint64_t GetSecondPartsLBA(void) {return secondHeader.partitionEntriesLBA;}
    170    uint64_t GetFirstUsableLBA(void) {return mainHeader.firstUsableLBA;}
    171    uint64_t GetLastUsableLBA(void) {return mainHeader.lastUsableLBA;}
    172    uint32_t CountParts(void);
    173    bool ValidPartNum (const uint32_t partNum);
    174    const GPTPart & operator[](uint32_t partNum) const;
    175    const GUIDData & GetDiskGUID(void) const;
    176    uint32_t GetBlockSize(void) {return blockSize;}
    177 
    178    // Find information about free space
    179    uint64_t FindFirstAvailable(uint64_t start = 0);
    180    uint64_t FindFirstInLargest(void);
    181    uint64_t FindLastAvailable();
    182    uint64_t FindLastInFree(uint64_t start);
    183    uint64_t FindFreeBlocks(uint32_t *numSegments, uint64_t *largestSegment);
    184    int IsFree(uint64_t sector, uint32_t *partNum = NULL);
    185    int IsFreePartNum(uint32_t partNum);
    186    int IsUsedPartNum(uint32_t partNum);
    187 
    188    // Change how functions work, or return information on same
    189    void SetAlignment(uint32_t n);
    190    uint32_t ComputeAlignment(void); // Set alignment based on current partitions
    191    uint32_t GetAlignment(void) {return sectorAlignment;}
    192    void JustLooking(int i = 1) {justLooking = i;}
    193    void TurnOffSyncing() {syncing = 0;}
    194    void BeQuiet(int i = 1) {beQuiet = i;}
    195    WhichToUse WhichWasUsed(void) {return whichWasUsed;}
    196 
    197    // Endianness functions
    198    void ReverseHeaderBytes(struct GPTHeader* header);
    199    void ReversePartitionBytes(); // for endianness
    200 
    201    // Attributes functions
    202    int ManageAttributes(int partNum, const string & command, const string & bits);
    203    void ShowAttributes(const uint32_t partNum);
    204    void GetAttribute(const uint32_t partNum, const string& attributeBits);
    205 
    206 }; // class GPTData
    207 
    208 // Function prototypes....
    209 int SizesOK(void);
    210 
    211 #endif
    212