1 /* 2 MBRPart class, part of GPT fdisk program family. 3 Copyright (C) 2011 Roderick W. Smith 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 2 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License along 16 with this program; if not, write to the Free Software Foundation, Inc., 17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 */ 19 20 #ifndef MBRPART_H 21 #define MBRPART_H 22 23 #include <stdint.h> 24 25 #define MAX_HEADS 255 /* numbered 0 - 254 */ 26 #define MAX_SECSPERTRACK 63 /* numbered 1 - 63 */ 27 #define MAX_CYLINDERS 1024 /* numbered 0 - 1023 */ 28 29 #define NONE 0 /* don't include partition when writing */ 30 #define PRIMARY 1 /* write partition as primary */ 31 #define LOGICAL 2 /* write partition as logical */ 32 #define EBR 4 /* sector is used as an EBR or MBR */ 33 #define INVALID 8 /* sector number is too large for disk */ 34 35 using namespace std; 36 37 // Data for a single MBR partition record 38 // Note that firstSector and lastSector are in CHS addressing, which 39 // splits the bits up in a weird way. 40 // On read or write of MBR entries, firstLBA is an absolute disk sector. 41 // On read of logical entries, it's relative to the EBR record for that 42 // partition. When writing EBR records, it's relative to the extended 43 // partition's start. 44 #pragma pack(1) 45 struct MBRRecord { 46 uint8_t status; 47 uint8_t firstSector[3]; 48 uint8_t partitionType; 49 uint8_t lastSector[3]; 50 uint32_t firstLBA; // see above 51 uint32_t lengthLBA; 52 }; // struct MBRRecord 53 #pragma pack () 54 55 class MBRPart { 56 protected: 57 uint8_t status; 58 uint8_t firstSector[3]; 59 uint8_t partitionType; 60 uint8_t lastSector[3]; 61 uint32_t firstLBA; // see above 62 uint32_t lengthLBA; 63 int includeAs; // PRIMARY, LOGICAL, or NONE 64 int canBeLogical; 65 int canBePrimary; 66 static uint32_t numHeads; 67 static uint32_t numSecspTrack; 68 static uint64_t diskSize; 69 static uint32_t blockSize; 70 static int numInstances; 71 72 public: 73 MBRPart(); 74 MBRPart(const MBRPart& other); 75 virtual ~MBRPart(); 76 virtual MBRPart& operator=(const MBRPart& orig); 77 virtual MBRPart& operator=(const struct MBRRecord& orig); 78 bool operator<(const MBRPart &other) const; 79 80 // Set information on partitions or disks... 81 void SetGeometry(uint32_t heads, uint32_t sectors, uint64_t ds, uint32_t bs); 82 void Empty(void); 83 void SetStartLBA(uint64_t s); 84 void SetLengthLBA(uint64_t l); 85 void SetLocation(uint64_t start, uint64_t length); 86 int SetType(uint8_t typeCode, int isExtended = 0); SetStatus(uint8_t s)87 void SetStatus(uint8_t s) {status = s;} 88 void SetInclusion(int status = PRIMARY) {includeAs = status;} SetCanBeLogical(int c)89 void SetCanBeLogical(int c) {canBeLogical = c;} SetCanBePrimary(int c)90 void SetCanBePrimary(int c) {canBePrimary = c;} 91 void StoreInStruct(struct MBRRecord *theStruct); 92 93 // Get information on partitions or disk.... GetType(void)94 uint8_t GetType(void) {return partitionType;} GetStatus(void)95 uint8_t GetStatus(void) {return status;} GetStartLBA(void)96 uint64_t GetStartLBA(void) {return firstLBA;} GetLengthLBA(void)97 uint64_t GetLengthLBA(void) {return lengthLBA;} 98 uint64_t GetLastLBA(void) const; GetInclusion(void)99 uint8_t GetInclusion(void) {return includeAs;} CanBeLogical(void)100 int CanBeLogical(void) {return canBeLogical;} CanBePrimary(void)101 int CanBePrimary(void) {return canBePrimary;} 102 int DoTheyOverlap (const MBRPart& other); 103 104 // Adjust information on partitions or disks... 105 int RecomputeCHS(void); 106 int LBAtoCHS(uint32_t lba, uint8_t * chs); 107 void ReverseByteOrder(void); 108 109 // User I/O... 110 void ShowData(int isGpt); 111 }; // MBRPart 112 113 #endif // MBRPART_H 114