1a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs//
2a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs// C++ Interface: gptpart
3a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs//
4a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs// Description: Class to implement a single GPT partition
5a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs//
6a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs//
7a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs// Author: Rod Smith <rodsmith@rodsbooks.com>, (C) 2009
8a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs//
9a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs// Copyright: See COPYING file that comes with this distribution
10a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs//
11a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs//
12978041ca613dcb881763b36cf53639d924e52a56srs// This program is copyright (c) 2009 by Roderick W. Smith. It is distributed
13978041ca613dcb881763b36cf53639d924e52a56srs// under the terms of the GNU GPL version 2, as detailed in the COPYING file.
14a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs
15a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs#ifndef __GPTPART_H
16a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs#define __GPTPART_H
17a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs
18a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs#include <stdint.h>
19546a9c7c369df465021feecb20f6a8f81b6df6bcsrs#include <string>
20a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs#include <sys/types.h>
21a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs#include "support.h"
22a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs#include "parttypes.h"
236699b01eda84d24bfaf80ad725304fef2b0e1b2asrs#include "guid.h"
240873e9d0e9345a2c4418b4718db525c9f1111c83srs#include "attributes.h"
25a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs
26a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrsusing namespace std;
27a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs
28a345a922606a88447d2d89e28189d5372a75ea07Roderick W. Smith// Values returned by GPTPart::IsSizedForMBR()
29a345a922606a88447d2d89e28189d5372a75ea07Roderick W. Smith#define MBR_SIZED_GOOD 0 /* Whole partition under 2^32 sectors */
30a345a922606a88447d2d89e28189d5372a75ea07Roderick W. Smith#define MBR_SIZED_IFFY 1 /* Partition starts under 2^32 & is less than 2^32, but ends over 2^32 */
31a345a922606a88447d2d89e28189d5372a75ea07Roderick W. Smith#define MBR_SIZED_BAD  2 /* Partition starts over 2^32, is bigger than 2^32, or otherwise bad */
32a345a922606a88447d2d89e28189d5372a75ea07Roderick W. Smith
33978041ca613dcb881763b36cf53639d924e52a56srs/****************************************
34978041ca613dcb881763b36cf53639d924e52a56srs *                                      *
35978041ca613dcb881763b36cf53639d924e52a56srs * GPTPart class and related structures *
36978041ca613dcb881763b36cf53639d924e52a56srs *                                      *
37978041ca613dcb881763b36cf53639d924e52a56srs ****************************************/
38a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs
39a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrsclass GPTPart {
40a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs   protected:
41ba00fed2efd6c0cba60da9afb0ce3dff84fc69f9srs      // Caution: The non-static data in GPTPart is precisely the right size
42a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs      // to enable easy loading of the data directly from disk. If any
43a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs      // non-static variables are added to the below, the data size will
44a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs      // change and the program will stop working. This can be corrected by
45a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs      // adjusting the data-load operation in GPTData::LoadMainTable() and
46ba00fed2efd6c0cba60da9afb0ce3dff84fc69f9srs      // GPTData::LoadSecondTableAsMain() and then removing the GPTPart
47ba00fed2efd6c0cba60da9afb0ce3dff84fc69f9srs      // size check in SizesOK() (in gpt.cc file).
486699b01eda84d24bfaf80ad725304fef2b0e1b2asrs      PartType partitionType;
496699b01eda84d24bfaf80ad725304fef2b0e1b2asrs      GUIDData uniqueGUID;
50a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs      uint64_t firstLBA;
51a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs      uint64_t lastLBA;
520873e9d0e9345a2c4418b4718db525c9f1111c83srs      Attributes attributes;
5384aaff6b9cf3b802c621781cf9acd006aa5a3e66Roderick W. Smith      uint16_t name[NAME_SIZE];
54a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs   public:
55a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs      GPTPart(void);
56a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs      ~GPTPart(void);
57a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs
58a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs      // Simple data retrieval:
596699b01eda84d24bfaf80ad725304fef2b0e1b2asrs      PartType & GetType(void) {return partitionType;}
60bf8950cad0285ee6ab8a896e8d0a30c5fb62c7afsrs      uint16_t GetHexType(void) const;
616699b01eda84d24bfaf80ad725304fef2b0e1b2asrs      string GetTypeName(void);
625a6085310b7f8fe1c35e56bcab7de161808b488dsrs      UnicodeString GetUTypeName(void);
635a081757ea2e32a491349544fea92826ccf739f6srs      const GUIDData GetUniqueGUID(void) const {return uniqueGUID;}
645a081757ea2e32a491349544fea92826ccf739f6srs      uint64_t GetFirstLBA(void) const {return firstLBA;}
655a081757ea2e32a491349544fea92826ccf739f6srs      uint64_t GetLastLBA(void) const {return lastLBA;}
66bf8950cad0285ee6ab8a896e8d0a30c5fb62c7afsrs      uint64_t GetLengthLBA(void) const;
670873e9d0e9345a2c4418b4718db525c9f1111c83srs      Attributes GetAttributes(void) {return attributes;}
680873e9d0e9345a2c4418b4718db525c9f1111c83srs      void ShowAttributes(uint32_t partNum) {attributes.ShowAttributes(partNum);}
695a6085310b7f8fe1c35e56bcab7de161808b488dsrs      UnicodeString GetDescription(void);
7008bb0da07953af605b4918e268272de15ac151aasrs      int IsUsed(void);
719b338c50f298d04f47205f7fad082d8c21797ed7Roderick W. Smith      int IsSizedForMBR(void);
72a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs
73a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs      // Simple data assignment:
746699b01eda84d24bfaf80ad725304fef2b0e1b2asrs      void SetType(PartType t);
756699b01eda84d24bfaf80ad725304fef2b0e1b2asrs      void SetType(uint16_t hex) {partitionType = hex;}
766699b01eda84d24bfaf80ad725304fef2b0e1b2asrs      void SetUniqueGUID(GUIDData u) {uniqueGUID = u;}
776699b01eda84d24bfaf80ad725304fef2b0e1b2asrs      void RandomizeUniqueGUID(void) {uniqueGUID.Randomize();}
78a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs      void SetFirstLBA(uint64_t f) {firstLBA = f;}
79a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs      void SetLastLBA(uint64_t l) {lastLBA = l;}
80a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs      void SetAttributes(uint64_t a) {attributes = a;}
810873e9d0e9345a2c4418b4718db525c9f1111c83srs      void SetAttributes(void) {attributes.ChangeAttributes();}
82699941e25a1fcf0beec124203747c8ed20842989srs      void SetName(const string & theName);
8300b6d7a4604e759eb3c92b3ecea608d6fe024b81srs#ifdef USE_UTF16
84699941e25a1fcf0beec124203747c8ed20842989srs      void SetName(const UnicodeString & theName);
85699941e25a1fcf0beec124203747c8ed20842989srs#endif
866699b01eda84d24bfaf80ad725304fef2b0e1b2asrs      void SetDefaultDescription(void);
87a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs
88a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs      // Additional functions
89a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs      GPTPart & operator=(const GPTPart & orig);
909a46b042c57144c26a67781d335e6ba4128382d2srs      bool operator<(const GPTPart &other) const;
91978041ca613dcb881763b36cf53639d924e52a56srs      void ShowSummary(int partNum, uint32_t blockSize); // display summary information (1-line)
92d761ff5a2aac9d9b6bd0bc8236419b1cf0128c86Jeff Sharkey      void ShowDetails(uint32_t blockSize); // display detailed information (multi-line)
93a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs      void BlankPartition(void); // empty partition of data
940a6973119c9e9984ad47a6da3231e8d16f996c5csrs      int DoTheyOverlap(const GPTPart & other); // returns 1 if there's overlap
95a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs      void ReversePartBytes(void); // reverse byte order of all integer fields
96a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs
97a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs      // Functions requiring user interaction
98a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs      void ChangeType(void); // Change the type code
99a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs}; // struct GPTPart
100a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs
101a0eb11a64b4a5b78caff58f804a5fb78ddf3a5dfsrs#endif
102