Blame gpt.h

Packit 062bc7
/* gpt.h -- GPT and data structure definitions, types, and
Packit 062bc7
   functions */
Packit 062bc7
Packit 062bc7
/* This program is copyright (c) 2009-2011 by Roderick W. Smith. It is distributed
Packit 062bc7
  under the terms of the GNU GPL version 2, as detailed in the COPYING file. */
Packit 062bc7
Packit 062bc7
#include <stdint.h>
Packit 062bc7
#include <sys/types.h>
Packit 062bc7
#include "gptpart.h"
Packit 062bc7
#include "support.h"
Packit 062bc7
#include "mbr.h"
Packit 062bc7
#include "bsd.h"
Packit 062bc7
#include "gptpart.h"
Packit 062bc7
Packit 062bc7
#ifndef __GPTSTRUCTS
Packit 062bc7
#define __GPTSTRUCTS
Packit 062bc7
Packit 062bc7
// Default values for sector alignment
Packit 062bc7
#define DEFAULT_ALIGNMENT 2048
Packit 062bc7
#define MAX_ALIGNMENT 65536
Packit 062bc7
#define MIN_AF_ALIGNMENT 8
Packit 062bc7
Packit 062bc7
// Below constant corresponds to a ~279GiB (300GB) disk, since the
Packit 062bc7
// smallest Advanced Format drive I know of is 320GB in size
Packit 062bc7
#define SMALLEST_ADVANCED_FORMAT UINT64_C(585937500)
Packit 062bc7
Packit 062bc7
using namespace std;
Packit 062bc7
Packit 062bc7
/****************************************
Packit 062bc7
 *                                      *
Packit 062bc7
 * GPTData class and related structures *
Packit 062bc7
 *                                      *
Packit 062bc7
 ****************************************/
Packit 062bc7
Packit 062bc7
// Validity state of GPT data
Packit 062bc7
enum GPTValidity {gpt_valid, gpt_corrupt, gpt_invalid};
Packit 062bc7
Packit 062bc7
// Which set of partition data to use
Packit 062bc7
enum WhichToUse {use_gpt, use_mbr, use_bsd, use_new, use_abort};
Packit 062bc7
Packit 062bc7
// Header (first 512 bytes) of GPT table
Packit 062bc7
#pragma pack(1)
Packit 062bc7
struct GPTHeader {
Packit 062bc7
   uint64_t signature;
Packit 062bc7
   uint32_t revision;
Packit 062bc7
   uint32_t headerSize;
Packit 062bc7
   uint32_t headerCRC;
Packit 062bc7
   uint32_t reserved;
Packit 062bc7
   uint64_t currentLBA;
Packit 062bc7
   uint64_t backupLBA;
Packit 062bc7
   uint64_t firstUsableLBA;
Packit 062bc7
   uint64_t lastUsableLBA;
Packit 062bc7
   GUIDData diskGUID;
Packit 062bc7
   uint64_t partitionEntriesLBA;
Packit 062bc7
   uint32_t numParts;
Packit 062bc7
   uint32_t sizeOfPartitionEntries;
Packit 062bc7
   uint32_t partitionEntriesCRC;
Packit 062bc7
   unsigned char reserved2[GPT_RESERVED];
Packit 062bc7
}; // struct GPTHeader
Packit 062bc7
Packit 062bc7
// Data in GPT format
Packit 062bc7
class GPTData {
Packit 062bc7
protected:
Packit 062bc7
   struct GPTHeader mainHeader;
Packit 062bc7
   GPTPart *partitions;
Packit 062bc7
   uint32_t numParts; // # of partitions the table can hold
Packit 062bc7
   struct GPTHeader secondHeader;
Packit 062bc7
   MBRData protectiveMBR;
Packit 062bc7
   string device; // device filename
Packit 062bc7
   DiskIO myDisk;
Packit 062bc7
   uint32_t blockSize; // device logical block size
Packit 062bc7
   uint32_t physBlockSize; // device physical block size (or 0 if it can't be determined)
Packit 062bc7
   uint64_t diskSize; // size of device, in logical blocks
Packit 062bc7
   GPTValidity state; // is GPT valid?
Packit 062bc7
   int justLooking; // Set to 1 if program launched with "-l" or if read-only
Packit 062bc7
   int mainCrcOk;
Packit 062bc7
   int secondCrcOk;
Packit 062bc7
   int mainPartsCrcOk;
Packit 062bc7
   int secondPartsCrcOk;
Packit 062bc7
   int apmFound; // set to 1 if APM detected
Packit 062bc7
   int bsdFound; // set to 1 if BSD disklabel detected in MBR
Packit 062bc7
   uint32_t sectorAlignment; // Start partitions at multiples of sectorAlignment
Packit 062bc7
   int beQuiet;
Packit 062bc7
   WhichToUse whichWasUsed;
Packit 062bc7
Packit 062bc7
   int LoadHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector, int *crcOk);
Packit 062bc7
   int LoadPartitionTable(const struct GPTHeader & header, DiskIO & disk, uint64_t sector = 0);
Packit 062bc7
   int CheckTable(struct GPTHeader *header);
Packit 062bc7
   int SaveHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector);
Packit 062bc7
   int SavePartitionTable(DiskIO & disk, uint64_t sector);
Packit 062bc7
public:
Packit 062bc7
   // Basic necessary functions....
Packit 062bc7
   GPTData(void);
Packit 062bc7
   GPTData(string deviceFilename);
Packit 062bc7
   virtual ~GPTData(void);
Packit 062bc7
   GPTData & operator=(const GPTData & orig);
Packit 062bc7
Packit 062bc7
   // Verify (or update) data integrity
Packit 062bc7
   int Verify(void);
Packit 062bc7
   int CheckGPTSize(void);
Packit 062bc7
   int CheckHeaderValidity(void);
Packit 062bc7
   int CheckHeaderCRC(struct GPTHeader* header, int warn = 0);
Packit 062bc7
   void RecomputeCRCs(void);
Packit 062bc7
   void RebuildMainHeader(void);
Packit 062bc7
   void RebuildSecondHeader(void);
Packit 062bc7
   int VerifyMBR(void) {return protectiveMBR.FindOverlaps();}
Packit 062bc7
   int FindHybridMismatches(void);
Packit 062bc7
   int FindOverlaps(void);
Packit 062bc7
   int FindInsanePartitions(void);
Packit 062bc7
Packit 062bc7
   // Load or save data from/to disk
Packit 062bc7
   int SetDisk(const string & deviceFilename);
Packit 062bc7
   DiskIO* GetDisk(void) {return &myDisk;}
Packit 062bc7
   int LoadMBR(const string & f) {return protectiveMBR.ReadMBRData(f);}
Packit 062bc7
   int WriteProtectiveMBR(void) {return protectiveMBR.WriteMBRData(&myDisk);}
Packit 062bc7
   void PartitionScan(void);
Packit 062bc7
   int LoadPartitions(const string & deviceFilename);
Packit 062bc7
   int ForceLoadGPTData(void);
Packit 062bc7
   int LoadMainTable(void);
Packit 062bc7
   int LoadSecondTableAsMain(void);
Packit 062bc7
   int SaveGPTData(int quiet = 0);
Packit 062bc7
   int SaveGPTBackup(const string & filename);
Packit 062bc7
   int LoadGPTBackup(const string & filename);
Packit 062bc7
   int SaveMBR(void);
Packit 062bc7
   int DestroyGPT(void);
Packit 062bc7
   int DestroyMBR(void);
Packit 062bc7
Packit 062bc7
   // Display data....
Packit 062bc7
   void ShowAPMState(void);
Packit 062bc7
   void ShowGPTState(void);
Packit 062bc7
   void DisplayGPTData(void);
Packit 062bc7
   void DisplayMBRData(void) {protectiveMBR.DisplayMBRData();}
Packit 062bc7
   void ShowPartDetails(uint32_t partNum);
Packit 062bc7
Packit 062bc7
   // Convert between GPT and other formats
Packit 062bc7
   virtual WhichToUse UseWhichPartitions(void);
Packit 062bc7
   void XFormPartitions(void);
Packit 062bc7
   int XFormDisklabel(uint32_t partNum);
Packit 062bc7
   int XFormDisklabel(BSDData* disklabel);
Packit 062bc7
   int OnePartToMBR(uint32_t gptPart, int mbrPart); // add one partition to MBR. Returns 1 if successful
Packit 062bc7
Packit 062bc7
   // Adjust GPT structures WITHOUT user interaction...
Packit 062bc7
   int SetGPTSize(uint32_t numEntries, int fillGPTSectors = 1);
Packit 062bc7
   int MoveMainTable(uint64_t pteSector);
Packit 062bc7
   void BlankPartitions(void);
Packit 062bc7
   int DeletePartition(uint32_t partNum);
Packit 062bc7
   uint32_t CreatePartition(uint32_t partNum, uint64_t startSector, uint64_t endSector);
Packit 062bc7
   void SortGPT(void);
Packit 062bc7
   int SwapPartitions(uint32_t partNum1, uint32_t partNum2);
Packit 062bc7
   int ClearGPTData(void);
Packit 062bc7
   void MoveSecondHeaderToEnd();
Packit 062bc7
   int SetName(uint32_t partNum, const UnicodeString & theName);
Packit 062bc7
   void SetDiskGUID(GUIDData newGUID);
Packit 062bc7
   int SetPartitionGUID(uint32_t pn, GUIDData theGUID);
Packit 062bc7
   void RandomizeGUIDs(void);
Packit 062bc7
   int ChangePartType(uint32_t pn, PartType theGUID);
Packit 062bc7
   void MakeProtectiveMBR(void) {protectiveMBR.MakeProtectiveMBR();}
Packit 062bc7
   void RecomputeCHS(void);
Packit 062bc7
   int Align(uint64_t* sector);
Packit 062bc7
   void SetProtectiveMBR(BasicMBRData & newMBR) {protectiveMBR = newMBR;}
Packit 062bc7
   
Packit 062bc7
   // Return data about the GPT structures....
Packit 062bc7
   WhichToUse GetState(void) {return whichWasUsed;}
Packit 062bc7
   int GetPartRange(uint32_t* low, uint32_t* high);
Packit 062bc7
   int FindFirstFreePart(void);
Packit 062bc7
   uint32_t GetNumParts(void) {return mainHeader.numParts;}
Packit 062bc7
   uint64_t GetTableSizeInSectors(void) {return (((numParts * GPT_SIZE) / blockSize) +
Packit 062bc7
                                                 (((numParts * GPT_SIZE) % blockSize) != 0)); }
Packit 062bc7
   uint64_t GetMainHeaderLBA(void) {return mainHeader.currentLBA;}
Packit 062bc7
   uint64_t GetSecondHeaderLBA(void) {return secondHeader.currentLBA;}
Packit 062bc7
   uint64_t GetMainPartsLBA(void) {return mainHeader.partitionEntriesLBA;}
Packit 062bc7
   uint64_t GetSecondPartsLBA(void) {return secondHeader.partitionEntriesLBA;}
Packit 062bc7
   uint64_t GetFirstUsableLBA(void) {return mainHeader.firstUsableLBA;}
Packit 062bc7
   uint64_t GetLastUsableLBA(void) {return mainHeader.lastUsableLBA;}
Packit 062bc7
   uint32_t CountParts(void);
Packit 062bc7
   bool ValidPartNum (const uint32_t partNum);
Packit 062bc7
   const GPTPart & operator[](uint32_t partNum) const;
Packit 062bc7
   const GUIDData & GetDiskGUID(void) const;
Packit 062bc7
   uint32_t GetBlockSize(void) {return blockSize;}
Packit 062bc7
Packit 062bc7
   // Find information about free space
Packit 062bc7
   uint64_t FindFirstAvailable(uint64_t start = 0);
Packit 062bc7
   uint64_t FindFirstUsedLBA(void);
Packit 062bc7
   uint64_t FindFirstInLargest(void);
Packit 062bc7
   uint64_t FindLastAvailable();
Packit 062bc7
   uint64_t FindLastInFree(uint64_t start);
Packit 062bc7
   uint64_t FindFreeBlocks(uint32_t *numSegments, uint64_t *largestSegment);
Packit 062bc7
   int IsFree(uint64_t sector, uint32_t *partNum = NULL);
Packit 062bc7
   int IsFreePartNum(uint32_t partNum);
Packit 062bc7
   int IsUsedPartNum(uint32_t partNum);
Packit 062bc7
Packit 062bc7
   // Change how functions work, or return information on same
Packit 062bc7
   void SetAlignment(uint32_t n);
Packit 062bc7
   uint32_t ComputeAlignment(void); // Set alignment based on current partitions
Packit 062bc7
   uint32_t GetAlignment(void) {return sectorAlignment;}
Packit 062bc7
   void JustLooking(int i = 1) {justLooking = i;}
Packit 062bc7
   void BeQuiet(int i = 1) {beQuiet = i;}
Packit 062bc7
   WhichToUse WhichWasUsed(void) {return whichWasUsed;}
Packit 062bc7
Packit 062bc7
   // Endianness functions
Packit 062bc7
   void ReverseHeaderBytes(struct GPTHeader* header);
Packit 062bc7
   void ReversePartitionBytes(); // for endianness
Packit 062bc7
Packit 062bc7
   // Attributes functions
Packit 062bc7
   int ManageAttributes(int partNum, const string & command, const string & bits);
Packit 062bc7
   void ShowAttributes(const uint32_t partNum);
Packit 062bc7
   void GetAttribute(const uint32_t partNum, const string& attributeBits);
Packit 062bc7
Packit 062bc7
}; // class GPTData
Packit 062bc7
Packit 062bc7
// Function prototypes....
Packit 062bc7
int SizesOK(void);
Packit 062bc7
Packit 062bc7
#endif