srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1 | /* gpt.cc -- Functions for loading, saving, and manipulating legacy MBR and GPT partition |
| 2 | data. */ |
| 3 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 4 | /* By Rod Smith, initial coding January to February, 2009 */ |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 5 | |
Roderick W. Smith | e3ee733 | 2013-09-24 12:56:11 -0400 | [diff] [blame] | 6 | /* This program is copyright (c) 2009-2013 by Roderick W. Smith. It is distributed |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 7 | under the terms of the GNU GPL version 2, as detailed in the COPYING file. */ |
| 8 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 9 | #define __STDC_LIMIT_MACROS |
| 10 | #define __STDC_CONSTANT_MACROS |
| 11 | |
| 12 | #include <stdio.h> |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 13 | #include <stdlib.h> |
| 14 | #include <stdint.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <string.h> |
srs5694 | a8582cf | 2010-03-19 14:21:59 -0400 | [diff] [blame] | 17 | #include <math.h> |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 18 | #include <time.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <errno.h> |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 21 | #include <iostream> |
srs5694 | 9a46b04 | 2011-03-15 00:34:10 -0400 | [diff] [blame] | 22 | #include <algorithm> |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 23 | #include "crc32.h" |
| 24 | #include "gpt.h" |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 25 | #include "bsd.h" |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 26 | #include "support.h" |
| 27 | #include "parttypes.h" |
| 28 | #include "attributes.h" |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 29 | #include "diskio.h" |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 30 | |
| 31 | using namespace std; |
| 32 | |
srs5694 | 8f1b2d6 | 2010-05-23 13:07:19 -0400 | [diff] [blame] | 33 | #ifdef __FreeBSD__ |
srs5694 | 9ba5421 | 2010-05-18 23:24:02 -0400 | [diff] [blame] | 34 | #define log2(x) (log(x) / M_LN2) |
| 35 | #endif // __FreeBSD__ |
| 36 | |
srs5694 | 8f1b2d6 | 2010-05-23 13:07:19 -0400 | [diff] [blame] | 37 | #ifdef _MSC_VER |
| 38 | #define log2(x) (log((double) x) / log(2.0)) |
| 39 | #endif // Microsoft Visual C++ |
srs5694 | 9ba5421 | 2010-05-18 23:24:02 -0400 | [diff] [blame] | 40 | |
Roderick W. Smith | 1f7822e | 2014-03-28 23:54:21 -0400 | [diff] [blame] | 41 | #ifdef EFI |
| 42 | // in UEFI mode MMX registers are not yet available so using the |
| 43 | // x86_64 ABI to move "double" values around is not an option. |
| 44 | #ifdef log2 |
| 45 | #undef log2 |
| 46 | #endif |
| 47 | #define log2(x) log2_32( x ) |
| 48 | static inline uint32_t log2_32(uint32_t v) { |
| 49 | int r = -1; |
| 50 | while (v >= 1) { |
| 51 | r++; |
| 52 | v >>= 1; |
| 53 | } |
| 54 | return r; |
| 55 | } |
| 56 | #endif |
| 57 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 58 | /**************************************** |
| 59 | * * |
| 60 | * GPTData class and related structures * |
| 61 | * * |
| 62 | ****************************************/ |
| 63 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 64 | // Default constructor |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 65 | GPTData::GPTData(void) { |
| 66 | blockSize = SECTOR_SIZE; // set a default |
| 67 | diskSize = 0; |
| 68 | partitions = NULL; |
| 69 | state = gpt_valid; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 70 | device = ""; |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 71 | justLooking = 0; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 72 | mainCrcOk = 0; |
| 73 | secondCrcOk = 0; |
| 74 | mainPartsCrcOk = 0; |
| 75 | secondPartsCrcOk = 0; |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 76 | apmFound = 0; |
| 77 | bsdFound = 0; |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 78 | sectorAlignment = MIN_AF_ALIGNMENT; // Align partitions on 4096-byte boundaries by default |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 79 | beQuiet = 0; |
| 80 | whichWasUsed = use_new; |
srs5694 | 1e09372 | 2010-01-05 00:14:19 -0500 | [diff] [blame] | 81 | mainHeader.numParts = 0; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 82 | numParts = 0; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 83 | SetGPTSize(NUM_GPT_ENTRIES); |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 84 | // Initialize CRC functions... |
| 85 | chksum_crc32gentab(); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 86 | } // GPTData default constructor |
| 87 | |
| 88 | // The following constructor loads GPT data from a device file |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 89 | GPTData::GPTData(string filename) { |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 90 | blockSize = SECTOR_SIZE; // set a default |
| 91 | diskSize = 0; |
| 92 | partitions = NULL; |
| 93 | state = gpt_invalid; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 94 | device = ""; |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 95 | justLooking = 0; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 96 | mainCrcOk = 0; |
| 97 | secondCrcOk = 0; |
| 98 | mainPartsCrcOk = 0; |
| 99 | secondPartsCrcOk = 0; |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 100 | apmFound = 0; |
| 101 | bsdFound = 0; |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 102 | sectorAlignment = MIN_AF_ALIGNMENT; // Align partitions on 4096-byte boundaries by default |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 103 | beQuiet = 0; |
| 104 | whichWasUsed = use_new; |
srs5694 | 1e09372 | 2010-01-05 00:14:19 -0500 | [diff] [blame] | 105 | mainHeader.numParts = 0; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 106 | numParts = 0; |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 107 | // Initialize CRC functions... |
| 108 | chksum_crc32gentab(); |
srs5694 | 3c0af38 | 2010-01-15 19:19:18 -0500 | [diff] [blame] | 109 | if (!LoadPartitions(filename)) |
| 110 | exit(2); |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 111 | } // GPTData(string filename) constructor |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 112 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 113 | // Destructor |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 114 | GPTData::~GPTData(void) { |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 115 | delete[] partitions; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 116 | } // GPTData destructor |
| 117 | |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 118 | // Assignment operator |
| 119 | GPTData & GPTData::operator=(const GPTData & orig) { |
| 120 | uint32_t i; |
| 121 | |
| 122 | mainHeader = orig.mainHeader; |
| 123 | numParts = orig.numParts; |
| 124 | secondHeader = orig.secondHeader; |
| 125 | protectiveMBR = orig.protectiveMBR; |
| 126 | device = orig.device; |
| 127 | blockSize = orig.blockSize; |
| 128 | diskSize = orig.diskSize; |
| 129 | state = orig.state; |
| 130 | justLooking = orig.justLooking; |
| 131 | mainCrcOk = orig.mainCrcOk; |
| 132 | secondCrcOk = orig.secondCrcOk; |
| 133 | mainPartsCrcOk = orig.mainPartsCrcOk; |
| 134 | secondPartsCrcOk = orig.secondPartsCrcOk; |
| 135 | apmFound = orig.apmFound; |
| 136 | bsdFound = orig.bsdFound; |
| 137 | sectorAlignment = orig.sectorAlignment; |
| 138 | beQuiet = orig.beQuiet; |
| 139 | whichWasUsed = orig.whichWasUsed; |
| 140 | |
| 141 | myDisk.OpenForRead(orig.myDisk.GetName()); |
| 142 | |
| 143 | delete[] partitions; |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 144 | partitions = new GPTPart [numParts]; |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 145 | if (partitions == NULL) { |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 146 | cerr << "Error! Could not allocate memory for partitions in GPTData::operator=()!\n" |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 147 | << "Terminating!\n"; |
| 148 | exit(1); |
| 149 | } // if |
| 150 | for (i = 0; i < numParts; i++) { |
| 151 | partitions[i] = orig.partitions[i]; |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 152 | } // for |
| 153 | |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 154 | return *this; |
| 155 | } // GPTData::operator=() |
| 156 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 157 | /********************************************************************* |
| 158 | * * |
| 159 | * Begin functions that verify data, or that adjust the verification * |
| 160 | * information (compute CRCs, rebuild headers) * |
| 161 | * * |
| 162 | *********************************************************************/ |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 163 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 164 | // Perform detailed verification, reporting on any problems found, but |
| 165 | // do *NOT* recover from these problems. Returns the total number of |
| 166 | // problems identified. |
| 167 | int GPTData::Verify(void) { |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 168 | int problems = 0, alignProbs = 0; |
srs5694 | e321d44 | 2010-01-29 17:44:04 -0500 | [diff] [blame] | 169 | uint32_t i, numSegments; |
| 170 | uint64_t totalFree, largestSegment; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 171 | |
| 172 | // First, check for CRC errors in the GPT data.... |
| 173 | if (!mainCrcOk) { |
| 174 | problems++; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 175 | cout << "\nProblem: The CRC for the main GPT header is invalid. The main GPT header may\n" |
| 176 | << "be corrupt. Consider loading the backup GPT header to rebuild the main GPT\n" |
| 177 | << "header ('b' on the recovery & transformation menu). This report may be a false\n" |
| 178 | << "alarm if you've already corrected other problems.\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 179 | } // if |
| 180 | if (!mainPartsCrcOk) { |
| 181 | problems++; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 182 | cout << "\nProblem: The CRC for the main partition table is invalid. This table may be\n" |
| 183 | << "corrupt. Consider loading the backup partition table ('c' on the recovery &\n" |
| 184 | << "transformation menu). This report may be a false alarm if you've already\n" |
| 185 | << "corrected other problems.\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 186 | } // if |
| 187 | if (!secondCrcOk) { |
| 188 | problems++; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 189 | cout << "\nProblem: The CRC for the backup GPT header is invalid. The backup GPT header\n" |
| 190 | << "may be corrupt. Consider using the main GPT header to rebuild the backup GPT\n" |
| 191 | << "header ('d' on the recovery & transformation menu). This report may be a false\n" |
| 192 | << "alarm if you've already corrected other problems.\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 193 | } // if |
| 194 | if (!secondPartsCrcOk) { |
| 195 | problems++; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 196 | cout << "\nCaution: The CRC for the backup partition table is invalid. This table may\n" |
| 197 | << "be corrupt. This program will automatically create a new backup partition\n" |
| 198 | << "table when you save your partitions.\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 199 | } // if |
| 200 | |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 201 | // Now check that the main and backup headers both point to themselves.... |
| 202 | if (mainHeader.currentLBA != 1) { |
| 203 | problems++; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 204 | cout << "\nProblem: The main header's self-pointer doesn't point to itself. This problem\n" |
| 205 | << "is being automatically corrected, but it may be a symptom of more serious\n" |
| 206 | << "problems. Think carefully before saving changes with 'w' or using this disk.\n"; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 207 | mainHeader.currentLBA = 1; |
| 208 | } // if |
| 209 | if (secondHeader.currentLBA != (diskSize - UINT64_C(1))) { |
| 210 | problems++; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 211 | cout << "\nProblem: The secondary header's self-pointer indicates that it doesn't reside\n" |
| 212 | << "at the end of the disk. If you've added a disk to a RAID array, use the 'e'\n" |
| 213 | << "option on the experts' menu to adjust the secondary header's and partition\n" |
| 214 | << "table's locations.\n"; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 215 | } // if |
| 216 | |
| 217 | // Now check that critical main and backup GPT entries match each other |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 218 | if (mainHeader.currentLBA != secondHeader.backupLBA) { |
| 219 | problems++; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 220 | cout << "\nProblem: main GPT header's current LBA pointer (" << mainHeader.currentLBA |
| 221 | << ") doesn't\nmatch the backup GPT header's alternate LBA pointer(" |
| 222 | << secondHeader.backupLBA << ").\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 223 | } // if |
| 224 | if (mainHeader.backupLBA != secondHeader.currentLBA) { |
| 225 | problems++; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 226 | cout << "\nProblem: main GPT header's backup LBA pointer (" << mainHeader.backupLBA |
| 227 | << ") doesn't\nmatch the backup GPT header's current LBA pointer (" |
| 228 | << secondHeader.currentLBA << ").\n" |
| 229 | << "The 'e' option on the experts' menu may fix this problem.\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 230 | } // if |
| 231 | if (mainHeader.firstUsableLBA != secondHeader.firstUsableLBA) { |
| 232 | problems++; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 233 | cout << "\nProblem: main GPT header's first usable LBA pointer (" << mainHeader.firstUsableLBA |
| 234 | << ") doesn't\nmatch the backup GPT header's first usable LBA pointer (" |
| 235 | << secondHeader.firstUsableLBA << ")\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 236 | } // if |
| 237 | if (mainHeader.lastUsableLBA != secondHeader.lastUsableLBA) { |
| 238 | problems++; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 239 | cout << "\nProblem: main GPT header's last usable LBA pointer (" << mainHeader.lastUsableLBA |
| 240 | << ") doesn't\nmatch the backup GPT header's last usable LBA pointer (" |
| 241 | << secondHeader.lastUsableLBA << ")\n" |
| 242 | << "The 'e' option on the experts' menu can probably fix this problem.\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 243 | } // if |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 244 | if ((mainHeader.diskGUID != secondHeader.diskGUID)) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 245 | problems++; |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 246 | cout << "\nProblem: main header's disk GUID (" << mainHeader.diskGUID |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 247 | << ") doesn't\nmatch the backup GPT header's disk GUID (" |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 248 | << secondHeader.diskGUID << ")\n" |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 249 | << "You should use the 'b' or 'd' option on the recovery & transformation menu to\n" |
| 250 | << "select one or the other header.\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 251 | } // if |
| 252 | if (mainHeader.numParts != secondHeader.numParts) { |
| 253 | problems++; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 254 | cout << "\nProblem: main GPT header's number of partitions (" << mainHeader.numParts |
| 255 | << ") doesn't\nmatch the backup GPT header's number of partitions (" |
| 256 | << secondHeader.numParts << ")\n" |
| 257 | << "Resizing the partition table ('s' on the experts' menu) may help.\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 258 | } // if |
| 259 | if (mainHeader.sizeOfPartitionEntries != secondHeader.sizeOfPartitionEntries) { |
| 260 | problems++; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 261 | cout << "\nProblem: main GPT header's size of partition entries (" |
| 262 | << mainHeader.sizeOfPartitionEntries << ") doesn't\n" |
| 263 | << "match the backup GPT header's size of partition entries (" |
| 264 | << secondHeader.sizeOfPartitionEntries << ")\n" |
| 265 | << "You should use the 'b' or 'd' option on the recovery & transformation menu to\n" |
| 266 | << "select one or the other header.\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 267 | } // if |
| 268 | |
| 269 | // Now check for a few other miscellaneous problems... |
| 270 | // Check that the disk size will hold the data... |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 271 | if (mainHeader.backupLBA >= diskSize) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 272 | problems++; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 273 | cout << "\nProblem: Disk is too small to hold all the data!\n" |
| 274 | << "(Disk size is " << diskSize << " sectors, needs to be " |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 275 | << mainHeader.backupLBA + UINT64_C(1) << " sectors.)\n" |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 276 | << "The 'e' option on the experts' menu may fix this problem.\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 277 | } // if |
| 278 | |
srs5694 | d8eed46 | 2012-12-15 01:55:21 -0500 | [diff] [blame] | 279 | if ((mainHeader.lastUsableLBA >= diskSize) || (mainHeader.lastUsableLBA > mainHeader.backupLBA)) { |
| 280 | problems++; |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame] | 281 | cout << "\nProblem: GPT claims the disk is larger than it is! (Claimed last usable\n" |
| 282 | << "sector is " << mainHeader.lastUsableLBA << ", but backup header is at\n" |
| 283 | << mainHeader.backupLBA << " and disk size is " << diskSize << " sectors.\n" |
| 284 | << "The 'e' option on the experts' menu will probably fix this problem\n"; |
srs5694 | d8eed46 | 2012-12-15 01:55:21 -0500 | [diff] [blame] | 285 | } |
| 286 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 287 | // Check for overlapping partitions.... |
| 288 | problems += FindOverlaps(); |
| 289 | |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 290 | // Check for insane partitions (start after end, hugely big, etc.) |
| 291 | problems += FindInsanePartitions(); |
| 292 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 293 | // Check for mismatched MBR and GPT partitions... |
| 294 | problems += FindHybridMismatches(); |
| 295 | |
srs5694 | 327129e | 2010-09-22 01:07:31 -0400 | [diff] [blame] | 296 | // Check for MBR-specific problems.... |
| 297 | problems += VerifyMBR(); |
| 298 | |
Roderick W. Smith | 042f38a | 2013-08-31 17:40:15 -0400 | [diff] [blame] | 299 | // Check for a 0xEE protective partition that's marked as active.... |
| 300 | if (protectiveMBR.IsEEActive()) { |
| 301 | cout << "\nWarning: The 0xEE protective partition in the MBR is marked as active. This is\n" |
| 302 | << "technically a violation of the GPT specification, and can cause some EFIs to\n" |
| 303 | << "ignore the disk, but it is required to boot from a GPT disk on some BIOS-based\n" |
| 304 | << "computers. You can clear this flag by creating a fresh protective MBR using\n" |
| 305 | << "the 'n' option on the experts' menu.\n"; |
| 306 | } |
| 307 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 308 | // Verify that partitions don't run into GPT data areas.... |
| 309 | problems += CheckGPTSize(); |
| 310 | |
Roderick W. Smith | 4a702a2 | 2014-01-25 23:46:42 -0500 | [diff] [blame] | 311 | if (!protectiveMBR.DoTheyFit()) { |
| 312 | cout << "\nPartition(s) in the protective MBR are too big for the disk! Creating a\n" |
| 313 | << "fresh protective or hybrid MBR is recommended.\n"; |
| 314 | problems++; |
| 315 | } |
| 316 | |
srs5694 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame] | 317 | // Check that partitions are aligned on proper boundaries (for WD Advanced |
| 318 | // Format and similar disks).... |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 319 | for (i = 0; i < numParts; i++) { |
srs5694 | e69e680 | 2012-01-20 22:37:12 -0500 | [diff] [blame] | 320 | if ((partitions[i].IsUsed()) && (partitions[i].GetFirstLBA() % sectorAlignment) != 0) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 321 | cout << "\nCaution: Partition " << i + 1 << " doesn't begin on a " |
| 322 | << sectorAlignment << "-sector boundary. This may\nresult " |
| 323 | << "in degraded performance on some modern (2009 and later) hard disks.\n"; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 324 | alignProbs++; |
srs5694 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame] | 325 | } // if |
| 326 | } // for |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 327 | if (alignProbs > 0) |
| 328 | cout << "\nConsult http://www.ibm.com/developerworks/linux/library/l-4kb-sector-disks/\n" |
| 329 | << "for information on disk alignment.\n"; |
srs5694 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame] | 330 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 331 | // Now compute available space, but only if no problems found, since |
| 332 | // problems could affect the results |
| 333 | if (problems == 0) { |
| 334 | totalFree = FindFreeBlocks(&numSegments, &largestSegment); |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 335 | cout << "\nNo problems found. " << totalFree << " free sectors (" |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 336 | << BytesToIeee(totalFree, blockSize) << ") available in " |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 337 | << numSegments << "\nsegments, the largest of which is " |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 338 | << largestSegment << " (" << BytesToIeee(largestSegment, blockSize) |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 339 | << ") in size.\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 340 | } else { |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 341 | cout << "\nIdentified " << problems << " problems!\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 342 | } // if/else |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 343 | |
| 344 | return (problems); |
| 345 | } // GPTData::Verify() |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 346 | |
| 347 | // Checks to see if the GPT tables overrun existing partitions; if they |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 348 | // do, issues a warning but takes no action. Returns number of problems |
| 349 | // detected (0 if OK, 1 to 2 if problems). |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 350 | int GPTData::CheckGPTSize(void) { |
| 351 | uint64_t overlap, firstUsedBlock, lastUsedBlock; |
| 352 | uint32_t i; |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 353 | int numProbs = 0; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 354 | |
| 355 | // first, locate the first & last used blocks |
| 356 | firstUsedBlock = UINT64_MAX; |
| 357 | lastUsedBlock = 0; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 358 | for (i = 0; i < numParts; i++) { |
srs5694 | e69e680 | 2012-01-20 22:37:12 -0500 | [diff] [blame] | 359 | if (partitions[i].IsUsed()) { |
srs5694 | 706e512 | 2012-01-21 13:47:24 -0500 | [diff] [blame] | 360 | if (partitions[i].GetFirstLBA() < firstUsedBlock) |
srs5694 | e69e680 | 2012-01-20 22:37:12 -0500 | [diff] [blame] | 361 | firstUsedBlock = partitions[i].GetFirstLBA(); |
| 362 | if (partitions[i].GetLastLBA() > lastUsedBlock) { |
| 363 | lastUsedBlock = partitions[i].GetLastLBA(); |
| 364 | } // if |
| 365 | } // if |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 366 | } // for |
| 367 | |
| 368 | // If the disk size is 0 (the default), then it means that various |
| 369 | // variables aren't yet set, so the below tests will be useless; |
| 370 | // therefore we should skip everything |
| 371 | if (diskSize != 0) { |
| 372 | if (mainHeader.firstUsableLBA > firstUsedBlock) { |
| 373 | overlap = mainHeader.firstUsableLBA - firstUsedBlock; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 374 | cout << "Warning! Main partition table overlaps the first partition by " |
| 375 | << overlap << " blocks!\n"; |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 376 | if (firstUsedBlock > 2) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 377 | cout << "Try reducing the partition table size by " << overlap * 4 |
| 378 | << " entries.\n(Use the 's' item on the experts' menu.)\n"; |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 379 | } else { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 380 | cout << "You will need to delete this partition or resize it in another utility.\n"; |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 381 | } // if/else |
| 382 | numProbs++; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 383 | } // Problem at start of disk |
| 384 | if (mainHeader.lastUsableLBA < lastUsedBlock) { |
| 385 | overlap = lastUsedBlock - mainHeader.lastUsableLBA; |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 386 | cout << "\nWarning! Secondary partition table overlaps the last partition by\n" |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 387 | << overlap << " blocks!\n"; |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 388 | if (lastUsedBlock > (diskSize - 2)) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 389 | cout << "You will need to delete this partition or resize it in another utility.\n"; |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 390 | } else { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 391 | cout << "Try reducing the partition table size by " << overlap * 4 |
| 392 | << " entries.\n(Use the 's' item on the experts' menu.)\n"; |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 393 | } // if/else |
| 394 | numProbs++; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 395 | } // Problem at end of disk |
| 396 | } // if (diskSize != 0) |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 397 | return numProbs; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 398 | } // GPTData::CheckGPTSize() |
| 399 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 400 | // Check the validity of the GPT header. Returns 1 if the main header |
| 401 | // is valid, 2 if the backup header is valid, 3 if both are valid, and |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 402 | // 0 if neither is valid. Note that this function checks the GPT signature, |
| 403 | // revision value, and CRCs in both headers. |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 404 | int GPTData::CheckHeaderValidity(void) { |
| 405 | int valid = 3; |
| 406 | |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 407 | cout.setf(ios::uppercase); |
| 408 | cout.fill('0'); |
| 409 | |
| 410 | // Note: failed GPT signature checks produce no error message because |
| 411 | // a message is displayed in the ReversePartitionBytes() function |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 412 | if ((mainHeader.signature != GPT_SIGNATURE) || (!CheckHeaderCRC(&mainHeader, 1))) { |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 413 | valid -= 1; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 414 | } else if ((mainHeader.revision != 0x00010000) && valid) { |
| 415 | valid -= 1; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 416 | cout << "Unsupported GPT version in main header; read 0x"; |
| 417 | cout.width(8); |
| 418 | cout << hex << mainHeader.revision << ", should be\n0x"; |
| 419 | cout.width(8); |
| 420 | cout << UINT32_C(0x00010000) << dec << "\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 421 | } // if/else/if |
| 422 | |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 423 | if ((secondHeader.signature != GPT_SIGNATURE) || (!CheckHeaderCRC(&secondHeader))) { |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 424 | valid -= 2; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 425 | } else if ((secondHeader.revision != 0x00010000) && valid) { |
| 426 | valid -= 2; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 427 | cout << "Unsupported GPT version in backup header; read 0x"; |
| 428 | cout.width(8); |
| 429 | cout << hex << secondHeader.revision << ", should be\n0x"; |
| 430 | cout.width(8); |
| 431 | cout << UINT32_C(0x00010000) << dec << "\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 432 | } // if/else/if |
| 433 | |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 434 | // Check for an Apple disk signature |
| 435 | if (((mainHeader.signature << 32) == APM_SIGNATURE1) || |
| 436 | (mainHeader.signature << 32) == APM_SIGNATURE2) { |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 437 | apmFound = 1; // Will display warning message later |
srs5694 | 3f2fe99 | 2009-11-24 18:28:18 -0500 | [diff] [blame] | 438 | } // if |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 439 | cout.fill(' '); |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 440 | |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 441 | return valid; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 442 | } // GPTData::CheckHeaderValidity() |
| 443 | |
| 444 | // Check the header CRC to see if it's OK... |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 445 | // Note: Must be called with header in platform-ordered byte order. |
| 446 | // Returns 1 if header's computed CRC matches the stored value, 0 if the |
| 447 | // computed and stored values don't match |
| 448 | int GPTData::CheckHeaderCRC(struct GPTHeader* header, int warn) { |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 449 | uint32_t oldCRC, newCRC, hSize; |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 450 | uint8_t *temp; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 451 | |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 452 | // Back up old header CRC and then blank it, since it must be 0 for |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 453 | // computation to be valid |
| 454 | oldCRC = header->headerCRC; |
| 455 | header->headerCRC = UINT32_C(0); |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 456 | |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 457 | hSize = header->headerSize; |
| 458 | |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 459 | if (IsLittleEndian() == 0) |
| 460 | ReverseHeaderBytes(header); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 461 | |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 462 | if ((hSize > blockSize) || (hSize < HEADER_SIZE)) { |
| 463 | if (warn) { |
| 464 | cerr << "\aWarning! Header size is specified as " << hSize << ", which is invalid.\n"; |
| 465 | cerr << "Setting the header size for CRC computation to " << HEADER_SIZE << "\n"; |
| 466 | } // if |
| 467 | hSize = HEADER_SIZE; |
| 468 | } else if ((hSize > sizeof(GPTHeader)) && warn) { |
| 469 | cout << "\aCaution! Header size for CRC check is " << hSize << ", which is greater than " << sizeof(GPTHeader) << ".\n"; |
| 470 | cout << "If stray data exists after the header on the header sector, it will be ignored,\n" |
| 471 | << "which may result in a CRC false alarm.\n"; |
| 472 | } // if/elseif |
| 473 | temp = new uint8_t[hSize]; |
| 474 | if (temp != NULL) { |
| 475 | memset(temp, 0, hSize); |
| 476 | if (hSize < sizeof(GPTHeader)) |
| 477 | memcpy(temp, header, hSize); |
| 478 | else |
| 479 | memcpy(temp, header, sizeof(GPTHeader)); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 480 | |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 481 | newCRC = chksum_crc32((unsigned char*) temp, hSize); |
| 482 | delete[] temp; |
| 483 | } else { |
| 484 | cerr << "Could not allocate memory in GPTData::CheckHeaderCRC()! Aborting!\n"; |
| 485 | exit(1); |
| 486 | } |
| 487 | if (IsLittleEndian() == 0) |
| 488 | ReverseHeaderBytes(header); |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 489 | header->headerCRC = oldCRC; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 490 | return (oldCRC == newCRC); |
| 491 | } // GPTData::CheckHeaderCRC() |
| 492 | |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 493 | // Recompute all the CRCs. Must be called before saving if any changes have |
| 494 | // been made. Must be called on platform-ordered data (this function reverses |
| 495 | // byte order and then undoes that reversal.) |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 496 | void GPTData::RecomputeCRCs(void) { |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 497 | uint32_t crc, hSize; |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 498 | int littleEndian = 1; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 499 | |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 500 | // If the header size is bigger than the GPT header data structure, reset it; |
| 501 | // otherwise, set both header sizes to whatever the main one is.... |
| 502 | if (mainHeader.headerSize > sizeof(GPTHeader)) |
| 503 | hSize = secondHeader.headerSize = mainHeader.headerSize = HEADER_SIZE; |
| 504 | else |
| 505 | hSize = secondHeader.headerSize = mainHeader.headerSize; |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 506 | |
| 507 | if ((littleEndian = IsLittleEndian()) == 0) { |
| 508 | ReversePartitionBytes(); |
| 509 | ReverseHeaderBytes(&mainHeader); |
| 510 | ReverseHeaderBytes(&secondHeader); |
| 511 | } // if |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 512 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 513 | // Compute CRC of partition tables & store in main and secondary headers |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 514 | crc = chksum_crc32((unsigned char*) partitions, numParts * GPT_SIZE); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 515 | mainHeader.partitionEntriesCRC = crc; |
| 516 | secondHeader.partitionEntriesCRC = crc; |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 517 | if (littleEndian == 0) { |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 518 | ReverseBytes(&mainHeader.partitionEntriesCRC, 4); |
| 519 | ReverseBytes(&secondHeader.partitionEntriesCRC, 4); |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 520 | } // if |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 521 | |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 522 | // Zero out GPT headers' own CRCs (required for correct computation) |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 523 | mainHeader.headerCRC = 0; |
| 524 | secondHeader.headerCRC = 0; |
| 525 | |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 526 | crc = chksum_crc32((unsigned char*) &mainHeader, hSize); |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 527 | if (littleEndian == 0) |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 528 | ReverseBytes(&crc, 4); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 529 | mainHeader.headerCRC = crc; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 530 | crc = chksum_crc32((unsigned char*) &secondHeader, hSize); |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 531 | if (littleEndian == 0) |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 532 | ReverseBytes(&crc, 4); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 533 | secondHeader.headerCRC = crc; |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 534 | |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 535 | if (littleEndian == 0) { |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 536 | ReverseHeaderBytes(&mainHeader); |
| 537 | ReverseHeaderBytes(&secondHeader); |
| 538 | ReversePartitionBytes(); |
| 539 | } // if |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 540 | } // GPTData::RecomputeCRCs() |
| 541 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 542 | // Rebuild the main GPT header, using the secondary header as a model. |
| 543 | // Typically called when the main header has been found to be corrupt. |
| 544 | void GPTData::RebuildMainHeader(void) { |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 545 | mainHeader.signature = GPT_SIGNATURE; |
| 546 | mainHeader.revision = secondHeader.revision; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 547 | mainHeader.headerSize = secondHeader.headerSize; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 548 | mainHeader.headerCRC = UINT32_C(0); |
| 549 | mainHeader.reserved = secondHeader.reserved; |
| 550 | mainHeader.currentLBA = secondHeader.backupLBA; |
| 551 | mainHeader.backupLBA = secondHeader.currentLBA; |
| 552 | mainHeader.firstUsableLBA = secondHeader.firstUsableLBA; |
| 553 | mainHeader.lastUsableLBA = secondHeader.lastUsableLBA; |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 554 | mainHeader.diskGUID = secondHeader.diskGUID; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 555 | mainHeader.partitionEntriesLBA = UINT64_C(2); |
| 556 | mainHeader.numParts = secondHeader.numParts; |
| 557 | mainHeader.sizeOfPartitionEntries = secondHeader.sizeOfPartitionEntries; |
| 558 | mainHeader.partitionEntriesCRC = secondHeader.partitionEntriesCRC; |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 559 | memcpy(mainHeader.reserved2, secondHeader.reserved2, sizeof(mainHeader.reserved2)); |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 560 | mainCrcOk = secondCrcOk; |
srs5694 | 706e512 | 2012-01-21 13:47:24 -0500 | [diff] [blame] | 561 | SetGPTSize(mainHeader.numParts, 0); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 562 | } // GPTData::RebuildMainHeader() |
| 563 | |
| 564 | // Rebuild the secondary GPT header, using the main header as a model. |
| 565 | void GPTData::RebuildSecondHeader(void) { |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 566 | secondHeader.signature = GPT_SIGNATURE; |
| 567 | secondHeader.revision = mainHeader.revision; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 568 | secondHeader.headerSize = mainHeader.headerSize; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 569 | secondHeader.headerCRC = UINT32_C(0); |
| 570 | secondHeader.reserved = mainHeader.reserved; |
| 571 | secondHeader.currentLBA = mainHeader.backupLBA; |
| 572 | secondHeader.backupLBA = mainHeader.currentLBA; |
| 573 | secondHeader.firstUsableLBA = mainHeader.firstUsableLBA; |
| 574 | secondHeader.lastUsableLBA = mainHeader.lastUsableLBA; |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 575 | secondHeader.diskGUID = mainHeader.diskGUID; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 576 | secondHeader.partitionEntriesLBA = secondHeader.lastUsableLBA + UINT64_C(1); |
| 577 | secondHeader.numParts = mainHeader.numParts; |
| 578 | secondHeader.sizeOfPartitionEntries = mainHeader.sizeOfPartitionEntries; |
| 579 | secondHeader.partitionEntriesCRC = mainHeader.partitionEntriesCRC; |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 580 | memcpy(secondHeader.reserved2, mainHeader.reserved2, sizeof(secondHeader.reserved2)); |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 581 | secondCrcOk = mainCrcOk; |
srs5694 | 706e512 | 2012-01-21 13:47:24 -0500 | [diff] [blame] | 582 | SetGPTSize(secondHeader.numParts, 0); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 583 | } // GPTData::RebuildSecondHeader() |
| 584 | |
| 585 | // Search for hybrid MBR entries that have no corresponding GPT partition. |
| 586 | // Returns number of such mismatches found |
| 587 | int GPTData::FindHybridMismatches(void) { |
srs5694 | e321d44 | 2010-01-29 17:44:04 -0500 | [diff] [blame] | 588 | int i, found, numFound = 0; |
| 589 | uint32_t j; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 590 | uint64_t mbrFirst, mbrLast; |
| 591 | |
| 592 | for (i = 0; i < 4; i++) { |
| 593 | if ((protectiveMBR.GetType(i) != 0xEE) && (protectiveMBR.GetType(i) != 0x00)) { |
| 594 | j = 0; |
| 595 | found = 0; |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 596 | mbrFirst = (uint64_t) protectiveMBR.GetFirstSector(i); |
| 597 | mbrLast = mbrFirst + (uint64_t) protectiveMBR.GetLength(i) - UINT64_C(1); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 598 | do { |
Roderick W. Smith | 24bba6e | 2013-10-12 19:07:16 -0400 | [diff] [blame] | 599 | if ((j < numParts) && (partitions[j].GetFirstLBA() == mbrFirst) && |
srs5694 | e69e680 | 2012-01-20 22:37:12 -0500 | [diff] [blame] | 600 | (partitions[j].GetLastLBA() == mbrLast) && (partitions[j].IsUsed())) |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 601 | found = 1; |
| 602 | j++; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 603 | } while ((!found) && (j < numParts)); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 604 | if (!found) { |
| 605 | numFound++; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 606 | cout << "\nWarning! Mismatched GPT and MBR partition! MBR partition " |
| 607 | << i + 1 << ", of type 0x"; |
| 608 | cout.fill('0'); |
| 609 | cout.setf(ios::uppercase); |
| 610 | cout.width(2); |
| 611 | cout << hex << (int) protectiveMBR.GetType(i) << ",\n" |
| 612 | << "has no corresponding GPT partition! You may continue, but this condition\n" |
| 613 | << "might cause data loss in the future!\a\n" << dec; |
| 614 | cout.fill(' '); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 615 | } // if |
| 616 | } // if |
| 617 | } // for |
| 618 | return numFound; |
| 619 | } // GPTData::FindHybridMismatches |
| 620 | |
| 621 | // Find overlapping partitions and warn user about them. Returns number of |
| 622 | // overlapping partitions. |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 623 | // Returns number of overlapping segments found. |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 624 | int GPTData::FindOverlaps(void) { |
srs5694 | e321d44 | 2010-01-29 17:44:04 -0500 | [diff] [blame] | 625 | int problems = 0; |
| 626 | uint32_t i, j; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 627 | |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 628 | for (i = 1; i < numParts; i++) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 629 | for (j = 0; j < i; j++) { |
srs5694 | e69e680 | 2012-01-20 22:37:12 -0500 | [diff] [blame] | 630 | if ((partitions[i].IsUsed()) && (partitions[j].IsUsed()) && |
| 631 | (partitions[i].DoTheyOverlap(partitions[j]))) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 632 | problems++; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 633 | cout << "\nProblem: partitions " << i + 1 << " and " << j + 1 << " overlap:\n"; |
| 634 | cout << " Partition " << i + 1 << ": " << partitions[i].GetFirstLBA() |
| 635 | << " to " << partitions[i].GetLastLBA() << "\n"; |
| 636 | cout << " Partition " << j + 1 << ": " << partitions[j].GetFirstLBA() |
| 637 | << " to " << partitions[j].GetLastLBA() << "\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 638 | } // if |
| 639 | } // for j... |
| 640 | } // for i... |
| 641 | return problems; |
| 642 | } // GPTData::FindOverlaps() |
| 643 | |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 644 | // Find partitions that are insane -- they start after they end or are too |
| 645 | // big for the disk. (The latter should duplicate detection of overlaps |
| 646 | // with GPT backup data structures, but better to err on the side of |
| 647 | // redundant tests than to miss something....) |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 648 | // Returns number of problems found. |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 649 | int GPTData::FindInsanePartitions(void) { |
| 650 | uint32_t i; |
| 651 | int problems = 0; |
| 652 | |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 653 | for (i = 0; i < numParts; i++) { |
srs5694 | e69e680 | 2012-01-20 22:37:12 -0500 | [diff] [blame] | 654 | if (partitions[i].IsUsed()) { |
| 655 | if (partitions[i].GetFirstLBA() > partitions[i].GetLastLBA()) { |
| 656 | problems++; |
| 657 | cout << "\nProblem: partition " << i + 1 << " ends before it begins.\n"; |
| 658 | } // if |
| 659 | if (partitions[i].GetLastLBA() >= diskSize) { |
| 660 | problems++; |
| 661 | cout << "\nProblem: partition " << i + 1 << " is too big for the disk.\n"; |
| 662 | } // if |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 663 | } // if |
| 664 | } // for |
| 665 | return problems; |
| 666 | } // GPTData::FindInsanePartitions(void) |
| 667 | |
| 668 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 669 | /****************************************************************** |
| 670 | * * |
| 671 | * Begin functions that load data from disk or save data to disk. * |
| 672 | * * |
| 673 | ******************************************************************/ |
| 674 | |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 675 | // Change the filename associated with the GPT. Used for duplicating |
| 676 | // the partition table to a new disk and saving backups. |
| 677 | // Returns 1 on success, 0 on failure. |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 678 | int GPTData::SetDisk(const string & deviceFilename) { |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 679 | int err, allOK = 1; |
| 680 | |
| 681 | device = deviceFilename; |
| 682 | if (allOK && myDisk.OpenForRead(deviceFilename)) { |
| 683 | // store disk information.... |
| 684 | diskSize = myDisk.DiskSize(&err); |
| 685 | blockSize = (uint32_t) myDisk.GetBlockSize(); |
| 686 | } // if |
| 687 | protectiveMBR.SetDisk(&myDisk); |
| 688 | protectiveMBR.SetDiskSize(diskSize); |
| 689 | protectiveMBR.SetBlockSize(blockSize); |
| 690 | return allOK; |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 691 | } // GPTData::SetDisk() |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 692 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 693 | // Scan for partition data. This function loads the MBR data (regular MBR or |
| 694 | // protective MBR) and loads BSD disklabel data (which is probably invalid). |
| 695 | // It also looks for APM data, forces a load of GPT data, and summarizes |
| 696 | // the results. |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 697 | void GPTData::PartitionScan(void) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 698 | BSDData bsdDisklabel; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 699 | |
| 700 | // Read the MBR & check for BSD disklabel |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 701 | protectiveMBR.ReadMBRData(&myDisk); |
| 702 | bsdDisklabel.ReadBSDData(&myDisk, 0, diskSize - 1); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 703 | |
| 704 | // Load the GPT data, whether or not it's valid |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 705 | ForceLoadGPTData(); |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 706 | |
Roderick W. Smith | 4a702a2 | 2014-01-25 23:46:42 -0500 | [diff] [blame] | 707 | // Some tools create a 0xEE partition that's too big. If this is detected, |
| 708 | // normalize it.... |
| 709 | if ((state == gpt_valid) && !protectiveMBR.DoTheyFit() && (protectiveMBR.GetValidity() == gpt)) { |
| 710 | if (!beQuiet) { |
| 711 | cerr << "\aThe protective MBR's 0xEE partition is oversized! Auto-repairing.\n\n"; |
| 712 | } // if |
| 713 | protectiveMBR.MakeProtectiveMBR(); |
| 714 | } // if |
| 715 | |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 716 | if (!beQuiet) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 717 | cout << "Partition table scan:\n"; |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 718 | protectiveMBR.ShowState(); |
| 719 | bsdDisklabel.ShowState(); |
| 720 | ShowAPMState(); // Show whether there's an Apple Partition Map present |
| 721 | ShowGPTState(); // Show GPT status |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 722 | cout << "\n"; |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 723 | } // if |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 724 | |
| 725 | if (apmFound) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 726 | cout << "\n*******************************************************************\n" |
| 727 | << "This disk appears to contain an Apple-format (APM) partition table!\n"; |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 728 | if (!justLooking) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 729 | cout << "It will be destroyed if you continue!\n"; |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 730 | } // if |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 731 | cout << "*******************************************************************\n\n\a"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 732 | } // if |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 733 | } // GPTData::PartitionScan() |
| 734 | |
| 735 | // Read GPT data from a disk. |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 736 | int GPTData::LoadPartitions(const string & deviceFilename) { |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 737 | BSDData bsdDisklabel; |
srs5694 | e321d44 | 2010-01-29 17:44:04 -0500 | [diff] [blame] | 738 | int err, allOK = 1; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 739 | MBRValidity mbrState; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 740 | |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 741 | if (myDisk.OpenForRead(deviceFilename)) { |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 742 | err = myDisk.OpenForWrite(deviceFilename); |
| 743 | if ((err == 0) && (!justLooking)) { |
| 744 | cout << "\aNOTE: Write test failed with error number " << errno |
| 745 | << ". It will be impossible to save\nchanges to this disk's partition table!\n"; |
| 746 | #if defined (__FreeBSD__) || defined (__FreeBSD_kernel__) |
| 747 | cout << "You may be able to enable writes by exiting this program, typing\n" |
| 748 | << "'sysctl kern.geom.debugflags=16' at a shell prompt, and re-running this\n" |
| 749 | << "program.\n"; |
| 750 | #endif |
Aurimas Liutikas | 74b7490 | 2016-05-10 18:53:54 -0700 | [diff] [blame^] | 751 | #if defined (__APPLE__) |
| 752 | cout << "You may need to deactivate System Integrity Protection to use this program. See\n" |
| 753 | << "https://www.quora.com/How-do-I-turn-off-the-rootless-in-OS-X-El-Capitan-10-11\n" |
| 754 | << "for more information.\n"; |
| 755 | #endif |
| 756 | cout << "\n"; |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 757 | } // if |
| 758 | myDisk.Close(); // Close and re-open read-only in case of bugs |
| 759 | } else allOK = 0; // if |
| 760 | |
| 761 | if (allOK && myDisk.OpenForRead(deviceFilename)) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 762 | // store disk information.... |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 763 | diskSize = myDisk.DiskSize(&err); |
| 764 | blockSize = (uint32_t) myDisk.GetBlockSize(); |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 765 | device = deviceFilename; |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 766 | PartitionScan(); // Check for partition types, load GPT, & print summary |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 767 | |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 768 | whichWasUsed = UseWhichPartitions(); |
| 769 | switch (whichWasUsed) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 770 | case use_mbr: |
| 771 | XFormPartitions(); |
| 772 | break; |
| 773 | case use_bsd: |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 774 | bsdDisklabel.ReadBSDData(&myDisk, 0, diskSize - 1); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 775 | // bsdDisklabel.DisplayBSDData(); |
| 776 | ClearGPTData(); |
| 777 | protectiveMBR.MakeProtectiveMBR(1); // clear boot area (option 1) |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 778 | XFormDisklabel(&bsdDisklabel); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 779 | break; |
| 780 | case use_gpt: |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 781 | mbrState = protectiveMBR.GetValidity(); |
| 782 | if ((mbrState == invalid) || (mbrState == mbr)) |
| 783 | protectiveMBR.MakeProtectiveMBR(); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 784 | break; |
| 785 | case use_new: |
| 786 | ClearGPTData(); |
| 787 | protectiveMBR.MakeProtectiveMBR(); |
| 788 | break; |
srs5694 | 3c0af38 | 2010-01-15 19:19:18 -0500 | [diff] [blame] | 789 | case use_abort: |
| 790 | allOK = 0; |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 791 | cerr << "Invalid partition data!\n"; |
srs5694 | 3c0af38 | 2010-01-15 19:19:18 -0500 | [diff] [blame] | 792 | break; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 793 | } // switch |
| 794 | |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 795 | if (allOK) |
srs5694 | 3c0af38 | 2010-01-15 19:19:18 -0500 | [diff] [blame] | 796 | CheckGPTSize(); |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 797 | myDisk.Close(); |
srs5694 | a8582cf | 2010-03-19 14:21:59 -0400 | [diff] [blame] | 798 | ComputeAlignment(); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 799 | } else { |
| 800 | allOK = 0; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 801 | } // if/else |
| 802 | return (allOK); |
| 803 | } // GPTData::LoadPartitions() |
| 804 | |
| 805 | // Loads the GPT, as much as possible. Returns 1 if this seems to have |
| 806 | // succeeded, 0 if there are obvious problems.... |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 807 | int GPTData::ForceLoadGPTData(void) { |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 808 | int allOK, validHeaders, loadedTable = 1; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 809 | |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 810 | allOK = LoadHeader(&mainHeader, myDisk, 1, &mainCrcOk); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 811 | |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 812 | if (mainCrcOk && (mainHeader.backupLBA < diskSize)) { |
| 813 | allOK = LoadHeader(&secondHeader, myDisk, mainHeader.backupLBA, &secondCrcOk) && allOK; |
| 814 | } else { |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 815 | allOK = LoadHeader(&secondHeader, myDisk, diskSize - UINT64_C(1), &secondCrcOk) && allOK; |
| 816 | if (mainCrcOk && (mainHeader.backupLBA >= diskSize)) |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 817 | cout << "Warning! Disk size is smaller than the main header indicates! Loading\n" |
| 818 | << "secondary header from the last sector of the disk! You should use 'v' to\n" |
| 819 | << "verify disk integrity, and perhaps options on the experts' menu to repair\n" |
| 820 | << "the disk.\n"; |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 821 | } // if/else |
| 822 | if (!allOK) |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 823 | state = gpt_invalid; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 824 | |
| 825 | // Return valid headers code: 0 = both headers bad; 1 = main header |
| 826 | // good, backup bad; 2 = backup header good, main header bad; |
| 827 | // 3 = both headers good. Note these codes refer to valid GPT |
srs5694 | 23d8d54 | 2011-10-01 18:40:10 -0400 | [diff] [blame] | 828 | // signatures, version numbers, and CRCs. |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 829 | validHeaders = CheckHeaderValidity(); |
| 830 | |
| 831 | // Read partitions (from primary array) |
| 832 | if (validHeaders > 0) { // if at least one header is OK.... |
| 833 | // GPT appears to be valid.... |
| 834 | state = gpt_valid; |
| 835 | |
| 836 | // We're calling the GPT valid, but there's a possibility that one |
| 837 | // of the two headers is corrupt. If so, use the one that seems to |
| 838 | // be in better shape to regenerate the bad one |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 839 | if (validHeaders == 1) { // valid main header, invalid backup header |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 840 | cerr << "\aCaution: invalid backup GPT header, but valid main header; regenerating\n" |
| 841 | << "backup header from main header.\n\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 842 | RebuildSecondHeader(); |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 843 | state = gpt_corrupt; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 844 | secondCrcOk = mainCrcOk; // Since regenerated, use CRC validity of main |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 845 | } else if (validHeaders == 2) { // valid backup header, invalid main header |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 846 | cerr << "\aCaution: invalid main GPT header, but valid backup; regenerating main header\n" |
| 847 | << "from backup!\n\n"; |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 848 | RebuildMainHeader(); |
| 849 | state = gpt_corrupt; |
| 850 | mainCrcOk = secondCrcOk; // Since copied, use CRC validity of backup |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 851 | } // if/else/if |
| 852 | |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 853 | // Figure out which partition table to load.... |
| 854 | // Load the main partition table, since either its header's CRC is OK or the |
| 855 | // backup header's CRC is not OK.... |
| 856 | if (mainCrcOk || !secondCrcOk) { |
| 857 | if (LoadMainTable() == 0) |
| 858 | allOK = 0; |
| 859 | } else { // bad main header CRC and backup header CRC is OK |
| 860 | state = gpt_corrupt; |
| 861 | if (LoadSecondTableAsMain()) { |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 862 | loadedTable = 2; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 863 | cerr << "\aWarning: Invalid CRC on main header data; loaded backup partition table.\n"; |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 864 | } else { // backup table bad, bad main header CRC, but try main table in desperation.... |
| 865 | if (LoadMainTable() == 0) { |
| 866 | allOK = 0; |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 867 | loadedTable = 0; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 868 | cerr << "\a\aWarning! Unable to load either main or backup partition table!\n"; |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 869 | } // if |
| 870 | } // if/else (LoadSecondTableAsMain()) |
| 871 | } // if/else (load partition table) |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 872 | |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 873 | if (loadedTable == 1) |
| 874 | secondPartsCrcOk = CheckTable(&secondHeader); |
| 875 | else if (loadedTable == 2) |
| 876 | mainPartsCrcOk = CheckTable(&mainHeader); |
| 877 | else |
| 878 | mainPartsCrcOk = secondPartsCrcOk = 0; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 879 | |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 880 | // Problem with main partition table; if backup is OK, use it instead.... |
| 881 | if (secondPartsCrcOk && secondCrcOk && !mainPartsCrcOk) { |
| 882 | state = gpt_corrupt; |
| 883 | allOK = allOK && LoadSecondTableAsMain(); |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 884 | mainPartsCrcOk = 0; // LoadSecondTableAsMain() resets this, so re-flag as bad |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 885 | cerr << "\aWarning! Main partition table CRC mismatch! Loaded backup " |
| 886 | << "partition table\ninstead of main partition table!\n\n"; |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 887 | } // if */ |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 888 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 889 | // Check for valid CRCs and warn if there are problems |
| 890 | if ((mainCrcOk == 0) || (secondCrcOk == 0) || (mainPartsCrcOk == 0) || |
| 891 | (secondPartsCrcOk == 0)) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 892 | cerr << "Warning! One or more CRCs don't match. You should repair the disk!\n\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 893 | state = gpt_corrupt; |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 894 | } // if |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 895 | } else { |
| 896 | state = gpt_invalid; |
| 897 | } // if/else |
| 898 | return allOK; |
| 899 | } // GPTData::ForceLoadGPTData() |
| 900 | |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 901 | // Loads the partition table pointed to by the main GPT header. The |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 902 | // main GPT header in memory MUST be valid for this call to do anything |
| 903 | // sensible! |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 904 | // Returns 1 on success, 0 on failure. CRC errors do NOT count as failure. |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 905 | int GPTData::LoadMainTable(void) { |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 906 | return LoadPartitionTable(mainHeader, myDisk); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 907 | } // GPTData::LoadMainTable() |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 908 | |
| 909 | // Load the second (backup) partition table as the primary partition |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 910 | // table. Used in repair functions, and when starting up if the main |
| 911 | // partition table is damaged. |
| 912 | // Returns 1 on success, 0 on failure. CRC errors do NOT count as failure. |
| 913 | int GPTData::LoadSecondTableAsMain(void) { |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 914 | return LoadPartitionTable(secondHeader, myDisk); |
| 915 | } // GPTData::LoadSecondTableAsMain() |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 916 | |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 917 | // Load a single GPT header (main or backup) from the specified disk device and |
| 918 | // sector. Applies byte-order corrections on big-endian platforms. Sets crcOk |
| 919 | // value appropriately. |
| 920 | // Returns 1 on success, 0 on failure. Note that CRC errors do NOT qualify as |
| 921 | // failure. |
| 922 | int GPTData::LoadHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector, int *crcOk) { |
| 923 | int allOK = 1; |
srs5694 | 1c6f8b0 | 2010-02-21 11:09:20 -0500 | [diff] [blame] | 924 | GPTHeader tempHeader; |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 925 | |
| 926 | disk.Seek(sector); |
srs5694 | 1c6f8b0 | 2010-02-21 11:09:20 -0500 | [diff] [blame] | 927 | if (disk.Read(&tempHeader, 512) != 512) { |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 928 | cerr << "Warning! Read error " << errno << "; strange behavior now likely!\n"; |
| 929 | allOK = 0; |
| 930 | } // if |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 931 | |
srs5694 | 1c6f8b0 | 2010-02-21 11:09:20 -0500 | [diff] [blame] | 932 | // Reverse byte order, if necessary |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 933 | if (IsLittleEndian() == 0) { |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 934 | ReverseHeaderBytes(&tempHeader); |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 935 | } // if |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 936 | *crcOk = CheckHeaderCRC(&tempHeader); |
srs5694 | 1c6f8b0 | 2010-02-21 11:09:20 -0500 | [diff] [blame] | 937 | |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 938 | if (allOK && (numParts != tempHeader.numParts) && *crcOk) { |
srs5694 | 706e512 | 2012-01-21 13:47:24 -0500 | [diff] [blame] | 939 | allOK = SetGPTSize(tempHeader.numParts, 0); |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 940 | } |
srs5694 | 1c6f8b0 | 2010-02-21 11:09:20 -0500 | [diff] [blame] | 941 | |
| 942 | *header = tempHeader; |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 943 | return allOK; |
| 944 | } // GPTData::LoadHeader |
| 945 | |
| 946 | // Load a partition table (either main or secondary) from the specified disk, |
| 947 | // using header as a reference for what to load. If sector != 0 (the default |
| 948 | // is 0), loads from the specified sector; otherwise loads from the sector |
| 949 | // indicated in header. |
| 950 | // Returns 1 on success, 0 on failure. CRC errors do NOT count as failure. |
| 951 | int GPTData::LoadPartitionTable(const struct GPTHeader & header, DiskIO & disk, uint64_t sector) { |
| 952 | uint32_t sizeOfParts, newCRC; |
| 953 | int retval; |
| 954 | |
| 955 | if (disk.OpenForRead()) { |
| 956 | if (sector == 0) { |
| 957 | retval = disk.Seek(header.partitionEntriesLBA); |
| 958 | } else { |
| 959 | retval = disk.Seek(sector); |
| 960 | } // if/else |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 961 | if (retval == 1) |
srs5694 | 706e512 | 2012-01-21 13:47:24 -0500 | [diff] [blame] | 962 | retval = SetGPTSize(header.numParts, 0); |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 963 | if (retval == 1) { |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 964 | sizeOfParts = header.numParts * header.sizeOfPartitionEntries; |
| 965 | if (disk.Read(partitions, sizeOfParts) != (int) sizeOfParts) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 966 | cerr << "Warning! Read error " << errno << "! Misbehavior now likely!\n"; |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 967 | retval = 0; |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 968 | } // if |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 969 | newCRC = chksum_crc32((unsigned char*) partitions, sizeOfParts); |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 970 | mainPartsCrcOk = secondPartsCrcOk = (newCRC == header.partitionEntriesCRC); |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 971 | if (IsLittleEndian() == 0) |
| 972 | ReversePartitionBytes(); |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 973 | if (!mainPartsCrcOk) { |
| 974 | cout << "Caution! After loading partitions, the CRC doesn't check out!\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 975 | } // if |
| 976 | } else { |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 977 | cerr << "Error! Couldn't seek to partition table!\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 978 | } // if/else |
| 979 | } else { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 980 | cerr << "Error! Couldn't open device " << device |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 981 | << " when reading partition table!\n"; |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 982 | retval = 0; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 983 | } // if/else |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 984 | return retval; |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 985 | } // GPTData::LoadPartitionsTable() |
| 986 | |
| 987 | // Check the partition table pointed to by header, but don't keep it |
| 988 | // around. |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 989 | // Returns 1 if the CRC is OK & this table matches the one already in memory, |
| 990 | // 0 if not or if there was a read error. |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 991 | int GPTData::CheckTable(struct GPTHeader *header) { |
| 992 | uint32_t sizeOfParts, newCRC; |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 993 | GPTPart *partsToCheck; |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 994 | GPTHeader *otherHeader; |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 995 | int allOK = 0; |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 996 | |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 997 | // Load partition table into temporary storage to check |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 998 | // its CRC and store the results, then discard this temporary |
| 999 | // storage, since we don't use it in any but recovery operations |
| 1000 | if (myDisk.Seek(header->partitionEntriesLBA)) { |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 1001 | partsToCheck = new GPTPart[header->numParts]; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1002 | sizeOfParts = header->numParts * header->sizeOfPartitionEntries; |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 1003 | if (partsToCheck == NULL) { |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 1004 | cerr << "Could not allocate memory in GPTData::CheckTable()! Terminating!\n"; |
| 1005 | exit(1); |
| 1006 | } // if |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 1007 | if (myDisk.Read(partsToCheck, sizeOfParts) != (int) sizeOfParts) { |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1008 | cerr << "Warning! Error " << errno << " reading partition table for CRC check!\n"; |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 1009 | } else { |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 1010 | newCRC = chksum_crc32((unsigned char*) partsToCheck, sizeOfParts); |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 1011 | allOK = (newCRC == header->partitionEntriesCRC); |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 1012 | if (header == &mainHeader) |
| 1013 | otherHeader = &secondHeader; |
| 1014 | else |
| 1015 | otherHeader = &mainHeader; |
| 1016 | if (newCRC != otherHeader->partitionEntriesCRC) { |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 1017 | cerr << "Warning! Main and backup partition tables differ! Use the 'c' and 'e' options\n" |
| 1018 | << "on the recovery & transformation menu to examine the two tables.\n\n"; |
| 1019 | allOK = 0; |
| 1020 | } // if |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 1021 | } // if/else |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 1022 | delete[] partsToCheck; |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 1023 | } // if |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 1024 | return allOK; |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 1025 | } // GPTData::CheckTable() |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1026 | |
srs5694 | 4307ef2 | 2012-05-30 12:30:48 -0400 | [diff] [blame] | 1027 | // Writes GPT (and protective MBR) to disk. If quiet==1, moves the second |
| 1028 | // header later on the disk without asking for permission, if necessary, and |
| 1029 | // doesn't confirm the operation before writing. If quiet==0, asks permission |
| 1030 | // before moving the second header and asks for final confirmation of any |
| 1031 | // write. |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 1032 | // Returns 1 on successful write, 0 if there was a problem. |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 1033 | int GPTData::SaveGPTData(int quiet) { |
srs5694 | 4307ef2 | 2012-05-30 12:30:48 -0400 | [diff] [blame] | 1034 | int allOK = 1, syncIt = 1; |
srs5694 | e321d44 | 2010-01-29 17:44:04 -0500 | [diff] [blame] | 1035 | char answer; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1036 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1037 | // First do some final sanity checks.... |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 1038 | |
| 1039 | // This test should only fail on read-only disks.... |
| 1040 | if (justLooking) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1041 | cout << "The justLooking flag is set. This probably means you can't write to the disk.\n"; |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 1042 | allOK = 0; |
| 1043 | } // if |
| 1044 | |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 1045 | // Check that disk is really big enough to handle the second header... |
| 1046 | if (mainHeader.backupLBA >= diskSize) { |
| 1047 | cerr << "Caution! Secondary header was placed beyond the disk's limits! Moving the\n" |
| 1048 | << "header, but other problems may occur!\n"; |
| 1049 | MoveSecondHeaderToEnd(); |
| 1050 | } // if |
| 1051 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1052 | // Is there enough space to hold the GPT headers and partition tables, |
| 1053 | // given the partition sizes? |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 1054 | if (CheckGPTSize() > 0) { |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1055 | allOK = 0; |
| 1056 | } // if |
| 1057 | |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1058 | // Check that second header is properly placed. Warn and ask if this should |
| 1059 | // be corrected if the test fails.... |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 1060 | if (mainHeader.backupLBA < (diskSize - UINT64_C(1))) { |
| 1061 | if (quiet == 0) { |
| 1062 | cout << "Warning! Secondary header is placed too early on the disk! Do you want to\n" |
| 1063 | << "correct this problem? "; |
| 1064 | if (GetYN() == 'Y') { |
| 1065 | MoveSecondHeaderToEnd(); |
| 1066 | cout << "Have moved second header and partition table to correct location.\n"; |
| 1067 | } else { |
| 1068 | cout << "Have not corrected the problem. Strange problems may occur in the future!\n"; |
| 1069 | } // if correction requested |
| 1070 | } else { // Go ahead and do correction automatically |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1071 | MoveSecondHeaderToEnd(); |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 1072 | } // if/else quiet |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1073 | } // if |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1074 | |
srs5694 | d8eed46 | 2012-12-15 01:55:21 -0500 | [diff] [blame] | 1075 | if ((mainHeader.lastUsableLBA >= diskSize) || (mainHeader.lastUsableLBA > mainHeader.backupLBA)) { |
| 1076 | if (quiet == 0) { |
| 1077 | cout << "Warning! The claimed last usable sector is incorrect! Do you want to correct\n" |
| 1078 | << "this problem? "; |
| 1079 | if (GetYN() == 'Y') { |
| 1080 | MoveSecondHeaderToEnd(); |
| 1081 | cout << "Have adjusted the second header and last usable sector value.\n"; |
| 1082 | } else { |
| 1083 | cout << "Have not corrected the problem. Strange problems may occur in the future!\n"; |
| 1084 | } // if correction requested |
| 1085 | } else { // go ahead and do correction automatically |
| 1086 | MoveSecondHeaderToEnd(); |
| 1087 | } // if/else quiet |
| 1088 | } // if |
| 1089 | |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 1090 | // Check for overlapping or insane partitions.... |
| 1091 | if ((FindOverlaps() > 0) || (FindInsanePartitions() > 0)) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1092 | allOK = 0; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1093 | cerr << "Aborting write operation!\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1094 | } // if |
| 1095 | |
Roderick W. Smith | 4a702a2 | 2014-01-25 23:46:42 -0500 | [diff] [blame] | 1096 | // Check that protective MBR fits, and warn if it doesn't.... |
| 1097 | if (!protectiveMBR.DoTheyFit()) { |
| 1098 | cerr << "\nPartition(s) in the protective MBR are too big for the disk! Creating a\n" |
| 1099 | << "fresh protective or hybrid MBR is recommended.\n"; |
| 1100 | } |
| 1101 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1102 | // Check for mismatched MBR and GPT data, but let it pass if found |
| 1103 | // (function displays warning message) |
| 1104 | FindHybridMismatches(); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1105 | |
| 1106 | RecomputeCRCs(); |
| 1107 | |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 1108 | if ((allOK) && (!quiet)) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1109 | cout << "\nFinal checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING\n" |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 1110 | << "PARTITIONS!!\n\nDo you want to proceed? "; |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 1111 | answer = GetYN(); |
| 1112 | if (answer == 'Y') { |
srs5694 | 3488294 | 2012-03-23 12:49:15 -0400 | [diff] [blame] | 1113 | cout << "OK; writing new GUID partition table (GPT) to " << myDisk.GetName() << ".\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1114 | } else { |
| 1115 | allOK = 0; |
| 1116 | } // if/else |
| 1117 | } // if |
| 1118 | |
| 1119 | // Do it! |
| 1120 | if (allOK) { |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 1121 | if (myDisk.OpenForWrite()) { |
srs5694 | 8a4ddfc | 2010-03-21 19:05:49 -0400 | [diff] [blame] | 1122 | // As per UEFI specs, write the secondary table and GPT first.... |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 1123 | allOK = SavePartitionTable(myDisk, secondHeader.partitionEntriesLBA); |
srs5694 | 4307ef2 | 2012-05-30 12:30:48 -0400 | [diff] [blame] | 1124 | if (!allOK) { |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 1125 | cerr << "Unable to save backup partition table! Perhaps the 'e' option on the experts'\n" |
| 1126 | << "menu will resolve this problem.\n"; |
srs5694 | 4307ef2 | 2012-05-30 12:30:48 -0400 | [diff] [blame] | 1127 | syncIt = 0; |
| 1128 | } // if |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1129 | |
| 1130 | // Now write the secondary GPT header... |
srs5694 | 8a4ddfc | 2010-03-21 19:05:49 -0400 | [diff] [blame] | 1131 | allOK = allOK && SaveHeader(&secondHeader, myDisk, mainHeader.backupLBA); |
| 1132 | |
| 1133 | // Now write the main partition tables... |
| 1134 | allOK = allOK && SavePartitionTable(myDisk, mainHeader.partitionEntriesLBA); |
| 1135 | |
| 1136 | // Now write the main GPT header... |
| 1137 | allOK = allOK && SaveHeader(&mainHeader, myDisk, 1); |
| 1138 | |
| 1139 | // To top it off, write the protective MBR... |
| 1140 | allOK = allOK && protectiveMBR.WriteMBRData(&myDisk); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1141 | |
| 1142 | // re-read the partition table |
srs5694 | 4307ef2 | 2012-05-30 12:30:48 -0400 | [diff] [blame] | 1143 | // Note: Done even if some write operations failed, but not if all of them failed. |
| 1144 | // Done this way because I've received one problem report from a user one whose |
| 1145 | // system the MBR write failed but everything else was OK (on a GPT disk under |
| 1146 | // Windows), and the failure to sync therefore caused Windows to restore the |
| 1147 | // original partition table from its cache. OTOH, such restoration might be |
| 1148 | // desirable if the error occurs later; but that seems unlikely unless the initial |
| 1149 | // write fails.... |
| 1150 | if (syncIt) |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 1151 | myDisk.DiskSync(); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1152 | |
| 1153 | if (allOK) { // writes completed OK |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1154 | cout << "The operation has completed successfully.\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1155 | } else { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1156 | cerr << "Warning! An error was reported when writing the partition table! This error\n" |
srs5694 | 4307ef2 | 2012-05-30 12:30:48 -0400 | [diff] [blame] | 1157 | << "MIGHT be harmless, or the disk might be damaged! Checking it is advisable.\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1158 | } // if/else |
srs5694 | 8a4ddfc | 2010-03-21 19:05:49 -0400 | [diff] [blame] | 1159 | |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 1160 | myDisk.Close(); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1161 | } else { |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 1162 | cerr << "Unable to open device '" << myDisk.GetName() << "' for writing! Errno is " |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1163 | << errno << "! Aborting write!\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1164 | allOK = 0; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1165 | } // if/else |
| 1166 | } else { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1167 | cout << "Aborting write of new partition table.\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1168 | } // if |
| 1169 | |
| 1170 | return (allOK); |
| 1171 | } // GPTData::SaveGPTData() |
| 1172 | |
| 1173 | // Save GPT data to a backup file. This function does much less error |
| 1174 | // checking than SaveGPTData(). It can therefore preserve many types of |
| 1175 | // corruption for later analysis; however, it preserves only the MBR, |
| 1176 | // the main GPT header, the backup GPT header, and the main partition |
| 1177 | // table; it discards the backup partition table, since it should be |
| 1178 | // identical to the main partition table on healthy disks. |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 1179 | int GPTData::SaveGPTBackup(const string & filename) { |
| 1180 | int allOK = 1; |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 1181 | DiskIO backupFile; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1182 | |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 1183 | if (backupFile.OpenForWrite(filename)) { |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 1184 | // Recomputing the CRCs is likely to alter them, which could be bad |
| 1185 | // if the intent is to save a potentially bad GPT for later analysis; |
| 1186 | // but if we don't do this, we get bogus errors when we load the |
| 1187 | // backup. I'm favoring misses over false alarms.... |
| 1188 | RecomputeCRCs(); |
| 1189 | |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 1190 | protectiveMBR.WriteMBRData(&backupFile); |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 1191 | protectiveMBR.SetDisk(&myDisk); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1192 | |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 1193 | if (allOK) { |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 1194 | // MBR write closed disk, so re-open and seek to end.... |
| 1195 | backupFile.OpenForWrite(); |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 1196 | allOK = SaveHeader(&mainHeader, backupFile, 1); |
| 1197 | } // if (allOK) |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1198 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1199 | if (allOK) |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 1200 | allOK = SaveHeader(&secondHeader, backupFile, 2); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1201 | |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 1202 | if (allOK) |
| 1203 | allOK = SavePartitionTable(backupFile, 3); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1204 | |
| 1205 | if (allOK) { // writes completed OK |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1206 | cout << "The operation has completed successfully.\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1207 | } else { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1208 | cerr << "Warning! An error was reported when writing the backup file.\n" |
| 1209 | << "It may not be usable!\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1210 | } // if/else |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 1211 | backupFile.Close(); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1212 | } else { |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 1213 | cerr << "Unable to open file '" << filename << "' for writing! Aborting!\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1214 | allOK = 0; |
| 1215 | } // if/else |
| 1216 | return allOK; |
| 1217 | } // GPTData::SaveGPTBackup() |
| 1218 | |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 1219 | // Write a GPT header (main or backup) to the specified sector. Used by both |
| 1220 | // the SaveGPTData() and SaveGPTBackup() functions. |
| 1221 | // Should be passed an architecture-appropriate header (DO NOT call |
| 1222 | // ReverseHeaderBytes() on the header before calling this function) |
| 1223 | // Returns 1 on success, 0 on failure |
| 1224 | int GPTData::SaveHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector) { |
| 1225 | int littleEndian, allOK = 1; |
| 1226 | |
| 1227 | littleEndian = IsLittleEndian(); |
| 1228 | if (!littleEndian) |
| 1229 | ReverseHeaderBytes(header); |
| 1230 | if (disk.Seek(sector)) { |
| 1231 | if (disk.Write(header, 512) == -1) |
| 1232 | allOK = 0; |
| 1233 | } else allOK = 0; // if (disk.Seek()...) |
| 1234 | if (!littleEndian) |
| 1235 | ReverseHeaderBytes(header); |
| 1236 | return allOK; |
| 1237 | } // GPTData::SaveHeader() |
| 1238 | |
| 1239 | // Save the partitions to the specified sector. Used by both the SaveGPTData() |
| 1240 | // and SaveGPTBackup() functions. |
| 1241 | // Should be passed an architecture-appropriate header (DO NOT call |
| 1242 | // ReverseHeaderBytes() on the header before calling this function) |
| 1243 | // Returns 1 on success, 0 on failure |
| 1244 | int GPTData::SavePartitionTable(DiskIO & disk, uint64_t sector) { |
| 1245 | int littleEndian, allOK = 1; |
| 1246 | |
| 1247 | littleEndian = IsLittleEndian(); |
| 1248 | if (disk.Seek(sector)) { |
| 1249 | if (!littleEndian) |
| 1250 | ReversePartitionBytes(); |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1251 | if (disk.Write(partitions, mainHeader.sizeOfPartitionEntries * numParts) == -1) |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 1252 | allOK = 0; |
| 1253 | if (!littleEndian) |
| 1254 | ReversePartitionBytes(); |
| 1255 | } else allOK = 0; // if (myDisk.Seek()...) |
| 1256 | return allOK; |
| 1257 | } // GPTData::SavePartitionTable() |
| 1258 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1259 | // Load GPT data from a backup file created by SaveGPTBackup(). This function |
| 1260 | // does minimal error checking. It returns 1 if it completed successfully, |
| 1261 | // 0 if there was a problem. In the latter case, it creates a new empty |
| 1262 | // set of partitions. |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 1263 | int GPTData::LoadGPTBackup(const string & filename) { |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 1264 | int allOK = 1, val, err; |
srs5694 | 0541b56 | 2011-12-18 16:35:25 -0500 | [diff] [blame] | 1265 | int shortBackup = 0; |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 1266 | DiskIO backupFile; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1267 | |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 1268 | if (backupFile.OpenForRead(filename)) { |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1269 | // Let the MBRData class load the saved MBR... |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 1270 | protectiveMBR.ReadMBRData(&backupFile, 0); // 0 = don't check block size |
srs5694 | 815fb65 | 2011-03-18 12:35:56 -0400 | [diff] [blame] | 1271 | protectiveMBR.SetDisk(&myDisk); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1272 | |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 1273 | LoadHeader(&mainHeader, backupFile, 1, &mainCrcOk); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1274 | |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 1275 | // Check backup file size and rebuild second header if file is right |
| 1276 | // size to be direct dd copy of MBR, main header, and main partition |
| 1277 | // table; if other size, treat it like a GPT fdisk-generated backup |
| 1278 | // file |
| 1279 | shortBackup = ((backupFile.DiskSize(&err) * backupFile.GetBlockSize()) == |
| 1280 | (mainHeader.numParts * mainHeader.sizeOfPartitionEntries) + 1024); |
| 1281 | if (shortBackup) { |
| 1282 | RebuildSecondHeader(); |
| 1283 | secondCrcOk = mainCrcOk; |
| 1284 | } else { |
| 1285 | LoadHeader(&secondHeader, backupFile, 2, &secondCrcOk); |
| 1286 | } // if/else |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 1287 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1288 | // Return valid headers code: 0 = both headers bad; 1 = main header |
| 1289 | // good, backup bad; 2 = backup header good, main header bad; |
| 1290 | // 3 = both headers good. Note these codes refer to valid GPT |
| 1291 | // signatures and version numbers; more subtle problems will elude |
| 1292 | // this check! |
| 1293 | if ((val = CheckHeaderValidity()) > 0) { |
| 1294 | if (val == 2) { // only backup header seems to be good |
srs5694 | 706e512 | 2012-01-21 13:47:24 -0500 | [diff] [blame] | 1295 | SetGPTSize(secondHeader.numParts, 0); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1296 | } else { // main header is OK |
srs5694 | 706e512 | 2012-01-21 13:47:24 -0500 | [diff] [blame] | 1297 | SetGPTSize(mainHeader.numParts, 0); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1298 | } // if/else |
| 1299 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1300 | if (secondHeader.currentLBA != diskSize - UINT64_C(1)) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1301 | cout << "Warning! Current disk size doesn't match that of the backup!\n" |
| 1302 | << "Adjusting sizes to match, but subsequent problems are possible!\n"; |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1303 | MoveSecondHeaderToEnd(); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1304 | } // if |
| 1305 | |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 1306 | if (!LoadPartitionTable(mainHeader, backupFile, (uint64_t) (3 - shortBackup))) |
| 1307 | cerr << "Warning! Read error " << errno |
| 1308 | << " loading partition table; strange behavior now likely!\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1309 | } else { |
| 1310 | allOK = 0; |
| 1311 | } // if/else |
srs5694 | a8582cf | 2010-03-19 14:21:59 -0400 | [diff] [blame] | 1312 | // Something went badly wrong, so blank out partitions |
| 1313 | if (allOK == 0) { |
| 1314 | cerr << "Improper backup file! Clearing all partition data!\n"; |
| 1315 | ClearGPTData(); |
| 1316 | protectiveMBR.MakeProtectiveMBR(); |
| 1317 | } // if |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1318 | } else { |
| 1319 | allOK = 0; |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 1320 | cerr << "Unable to open file '" << filename << "' for reading! Aborting!\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1321 | } // if/else |
| 1322 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1323 | return allOK; |
| 1324 | } // GPTData::LoadGPTBackup() |
| 1325 | |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1326 | int GPTData::SaveMBR(void) { |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 1327 | return protectiveMBR.WriteMBRData(&myDisk); |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1328 | } // GPTData::SaveMBR() |
| 1329 | |
| 1330 | // This function destroys the on-disk GPT structures, but NOT the on-disk |
| 1331 | // MBR. |
| 1332 | // Returns 1 if the operation succeeds, 0 if not. |
| 1333 | int GPTData::DestroyGPT(void) { |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 1334 | int sum, tableSize, allOK = 1; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1335 | uint8_t blankSector[512]; |
| 1336 | uint8_t* emptyTable; |
| 1337 | |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 1338 | memset(blankSector, 0, sizeof(blankSector)); |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame] | 1339 | ClearGPTData(); |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1340 | |
| 1341 | if (myDisk.OpenForWrite()) { |
| 1342 | if (!myDisk.Seek(mainHeader.currentLBA)) |
| 1343 | allOK = 0; |
| 1344 | if (myDisk.Write(blankSector, 512) != 512) { // blank it out |
| 1345 | cerr << "Warning! GPT main header not overwritten! Error is " << errno << "\n"; |
| 1346 | allOK = 0; |
| 1347 | } // if |
| 1348 | if (!myDisk.Seek(mainHeader.partitionEntriesLBA)) |
| 1349 | allOK = 0; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1350 | tableSize = numParts * mainHeader.sizeOfPartitionEntries; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1351 | emptyTable = new uint8_t[tableSize]; |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 1352 | if (emptyTable == NULL) { |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame] | 1353 | cerr << "Could not allocate memory in GPTData::DestroyGPT()! Terminating!\n"; |
| 1354 | exit(1); |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 1355 | } // if |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 1356 | memset(emptyTable, 0, tableSize); |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1357 | if (allOK) { |
| 1358 | sum = myDisk.Write(emptyTable, tableSize); |
| 1359 | if (sum != tableSize) { |
| 1360 | cerr << "Warning! GPT main partition table not overwritten! Error is " << errno << "\n"; |
| 1361 | allOK = 0; |
| 1362 | } // if write failed |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame] | 1363 | } // if |
| 1364 | if (!myDisk.Seek(secondHeader.partitionEntriesLBA)) |
| 1365 | allOK = 0; |
| 1366 | if (allOK) { |
| 1367 | sum = myDisk.Write(emptyTable, tableSize); |
| 1368 | if (sum != tableSize) { |
| 1369 | cerr << "Warning! GPT backup partition table not overwritten! Error is " |
| 1370 | << errno << "\n"; |
| 1371 | allOK = 0; |
| 1372 | } // if wrong size written |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1373 | } // if |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame] | 1374 | if (!myDisk.Seek(secondHeader.currentLBA)) |
| 1375 | allOK = 0; |
| 1376 | if (allOK) { |
| 1377 | if (myDisk.Write(blankSector, 512) != 512) { // blank it out |
| 1378 | cerr << "Warning! GPT backup header not overwritten! Error is " << errno << "\n"; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1379 | allOK = 0; |
| 1380 | } // if |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame] | 1381 | } // if |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1382 | myDisk.DiskSync(); |
| 1383 | myDisk.Close(); |
| 1384 | cout << "GPT data structures destroyed! You may now partition the disk using fdisk or\n" |
| 1385 | << "other utilities.\n"; |
| 1386 | delete[] emptyTable; |
| 1387 | } else { |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 1388 | cerr << "Problem opening '" << device << "' for writing! Program will now terminate.\n"; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1389 | } // if/else (fd != -1) |
| 1390 | return (allOK); |
| 1391 | } // GPTDataTextUI::DestroyGPT() |
| 1392 | |
| 1393 | // Wipe MBR data from the disk (zero it out completely) |
| 1394 | // Returns 1 on success, 0 on failure. |
| 1395 | int GPTData::DestroyMBR(void) { |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 1396 | int allOK; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1397 | uint8_t blankSector[512]; |
| 1398 | |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 1399 | memset(blankSector, 0, sizeof(blankSector)); |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1400 | |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 1401 | allOK = myDisk.OpenForWrite() && myDisk.Seek(0) && (myDisk.Write(blankSector, 512) == 512); |
| 1402 | |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1403 | if (!allOK) |
| 1404 | cerr << "Warning! MBR not overwritten! Error is " << errno << "!\n"; |
| 1405 | return allOK; |
| 1406 | } // GPTData::DestroyMBR(void) |
| 1407 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1408 | // Tell user whether Apple Partition Map (APM) was discovered.... |
| 1409 | void GPTData::ShowAPMState(void) { |
| 1410 | if (apmFound) |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1411 | cout << " APM: present\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1412 | else |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1413 | cout << " APM: not present\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1414 | } // GPTData::ShowAPMState() |
| 1415 | |
| 1416 | // Tell user about the state of the GPT data.... |
| 1417 | void GPTData::ShowGPTState(void) { |
| 1418 | switch (state) { |
| 1419 | case gpt_invalid: |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1420 | cout << " GPT: not present\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1421 | break; |
| 1422 | case gpt_valid: |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1423 | cout << " GPT: present\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1424 | break; |
| 1425 | case gpt_corrupt: |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1426 | cout << " GPT: damaged\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1427 | break; |
| 1428 | default: |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1429 | cout << "\a GPT: unknown -- bug!\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1430 | break; |
| 1431 | } // switch |
| 1432 | } // GPTData::ShowGPTState() |
| 1433 | |
| 1434 | // Display the basic GPT data |
| 1435 | void GPTData::DisplayGPTData(void) { |
srs5694 | e321d44 | 2010-01-29 17:44:04 -0500 | [diff] [blame] | 1436 | uint32_t i; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1437 | uint64_t temp, totalFree; |
| 1438 | |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1439 | cout << "Disk " << device << ": " << diskSize << " sectors, " |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 1440 | << BytesToIeee(diskSize, blockSize) << "\n"; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1441 | cout << "Logical sector size: " << blockSize << " bytes\n"; |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 1442 | cout << "Disk identifier (GUID): " << mainHeader.diskGUID << "\n"; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1443 | cout << "Partition table holds up to " << numParts << " entries\n"; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1444 | cout << "First usable sector is " << mainHeader.firstUsableLBA |
| 1445 | << ", last usable sector is " << mainHeader.lastUsableLBA << "\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1446 | totalFree = FindFreeBlocks(&i, &temp); |
srs5694 | 8a4ddfc | 2010-03-21 19:05:49 -0400 | [diff] [blame] | 1447 | cout << "Partitions will be aligned on " << sectorAlignment << "-sector boundaries\n"; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1448 | cout << "Total free space is " << totalFree << " sectors (" |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 1449 | << BytesToIeee(totalFree, blockSize) << ")\n"; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1450 | cout << "\nNumber Start (sector) End (sector) Size Code Name\n"; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1451 | for (i = 0; i < numParts; i++) { |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 1452 | partitions[i].ShowSummary(i, blockSize); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1453 | } // for |
| 1454 | } // GPTData::DisplayGPTData() |
| 1455 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1456 | // Show detailed information on the specified partition |
Jeff Sharkey | d761ff5 | 2015-02-28 19:18:39 -0800 | [diff] [blame] | 1457 | void GPTData::ShowPartDetails(uint32_t partNum) { |
Roderick W. Smith | 24bba6e | 2013-10-12 19:07:16 -0400 | [diff] [blame] | 1458 | if ((partNum < numParts) && !IsFreePartNum(partNum)) { |
Jeff Sharkey | d761ff5 | 2015-02-28 19:18:39 -0800 | [diff] [blame] | 1459 | partitions[partNum].ShowDetails(blockSize); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1460 | } else { |
Roderick W. Smith | 24bba6e | 2013-10-12 19:07:16 -0400 | [diff] [blame] | 1461 | cout << "Partition #" << partNum + 1 << " does not exist.\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1462 | } // if |
| 1463 | } // GPTData::ShowPartDetails() |
| 1464 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1465 | /************************************************************************** |
| 1466 | * * |
| 1467 | * Partition table transformation functions (MBR or BSD disklabel to GPT) * |
| 1468 | * (some of these functions may require user interaction) * |
| 1469 | * * |
| 1470 | **************************************************************************/ |
| 1471 | |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1472 | // Examines the MBR & GPT data to determine which set of data to use: the |
| 1473 | // MBR (use_mbr), the GPT (use_gpt), the BSD disklabel (use_bsd), or create |
| 1474 | // a new set of partitions (use_new). A return value of use_abort indicates |
| 1475 | // that this function couldn't determine what to do. Overriding functions |
| 1476 | // in derived classes may ask users questions in such cases. |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1477 | WhichToUse GPTData::UseWhichPartitions(void) { |
| 1478 | WhichToUse which = use_new; |
| 1479 | MBRValidity mbrState; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1480 | |
| 1481 | mbrState = protectiveMBR.GetValidity(); |
| 1482 | |
| 1483 | if ((state == gpt_invalid) && ((mbrState == mbr) || (mbrState == hybrid))) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1484 | cout << "\n***************************************************************\n" |
Roderick W. Smith | 1eea9b0 | 2013-07-06 22:52:58 -0400 | [diff] [blame] | 1485 | << "Found invalid GPT and valid MBR; converting MBR to GPT format\n" |
| 1486 | << "in memory. "; |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 1487 | if (!justLooking) { |
Roderick W. Smith | 1eea9b0 | 2013-07-06 22:52:58 -0400 | [diff] [blame] | 1488 | cout << "\aTHIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by\n" |
| 1489 | << "typing 'q' if you don't want to convert your MBR partitions\n" |
| 1490 | << "to GPT format!"; |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 1491 | } // if |
Roderick W. Smith | 1eea9b0 | 2013-07-06 22:52:58 -0400 | [diff] [blame] | 1492 | cout << "\n***************************************************************\n\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1493 | which = use_mbr; |
| 1494 | } // if |
| 1495 | |
| 1496 | if ((state == gpt_invalid) && bsdFound) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1497 | cout << "\n**********************************************************************\n" |
| 1498 | << "Found invalid GPT and valid BSD disklabel; converting BSD disklabel\n" |
| 1499 | << "to GPT format."; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 1500 | if ((!justLooking) && (!beQuiet)) { |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1501 | cout << "\a THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Your first\n" |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1502 | << "BSD partition will likely be unusable. Exit by typing 'q' if you don't\n" |
| 1503 | << "want to convert your BSD partitions to GPT format!"; |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 1504 | } // if |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1505 | cout << "\n**********************************************************************\n\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1506 | which = use_bsd; |
| 1507 | } // if |
| 1508 | |
| 1509 | if ((state == gpt_valid) && (mbrState == gpt)) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1510 | which = use_gpt; |
srs5694 | 3c0af38 | 2010-01-15 19:19:18 -0500 | [diff] [blame] | 1511 | if (!beQuiet) |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1512 | cout << "Found valid GPT with protective MBR; using GPT.\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1513 | } // if |
| 1514 | if ((state == gpt_valid) && (mbrState == hybrid)) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1515 | which = use_gpt; |
srs5694 | 3c0af38 | 2010-01-15 19:19:18 -0500 | [diff] [blame] | 1516 | if (!beQuiet) |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1517 | cout << "Found valid GPT with hybrid MBR; using GPT.\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1518 | } // if |
| 1519 | if ((state == gpt_valid) && (mbrState == invalid)) { |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 1520 | cout << "\aFound valid GPT with corrupt MBR; using GPT and will write new\n" |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1521 | << "protective MBR on save.\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1522 | which = use_gpt; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1523 | } // if |
| 1524 | if ((state == gpt_valid) && (mbrState == mbr)) { |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1525 | which = use_abort; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1526 | } // if |
| 1527 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1528 | if (state == gpt_corrupt) { |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1529 | if (mbrState == gpt) { |
| 1530 | cout << "\a\a****************************************************************************\n" |
| 1531 | << "Caution: Found protective or hybrid MBR and corrupt GPT. Using GPT, but disk\n" |
| 1532 | << "verification and recovery are STRONGLY recommended.\n" |
| 1533 | << "****************************************************************************\n"; |
| 1534 | which = use_gpt; |
srs5694 | 3c0af38 | 2010-01-15 19:19:18 -0500 | [diff] [blame] | 1535 | } else { |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1536 | which = use_abort; |
| 1537 | } // if/else MBR says disk is GPT |
| 1538 | } // if GPT corrupt |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1539 | |
| 1540 | if (which == use_new) |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1541 | cout << "Creating new GPT entries.\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1542 | |
| 1543 | return which; |
| 1544 | } // UseWhichPartitions() |
| 1545 | |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1546 | // Convert MBR partition table into GPT form. |
| 1547 | void GPTData::XFormPartitions(void) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1548 | int i, numToConvert; |
| 1549 | uint8_t origType; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1550 | |
| 1551 | // Clear out old data & prepare basics.... |
| 1552 | ClearGPTData(); |
| 1553 | |
| 1554 | // Convert the smaller of the # of GPT or MBR partitions |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1555 | if (numParts > MAX_MBR_PARTS) |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 1556 | numToConvert = MAX_MBR_PARTS; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1557 | else |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1558 | numToConvert = numParts; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1559 | |
| 1560 | for (i = 0; i < numToConvert; i++) { |
| 1561 | origType = protectiveMBR.GetType(i); |
| 1562 | // don't waste CPU time trying to convert extended, hybrid protective, or |
| 1563 | // null (non-existent) partitions |
srs5694 | e35eb1b | 2009-09-14 00:29:34 -0400 | [diff] [blame] | 1564 | if ((origType != 0x05) && (origType != 0x0f) && (origType != 0x85) && |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 1565 | (origType != 0x00) && (origType != 0xEE)) |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1566 | partitions[i] = protectiveMBR.AsGPT(i); |
| 1567 | } // for |
| 1568 | |
| 1569 | // Convert MBR into protective MBR |
| 1570 | protectiveMBR.MakeProtectiveMBR(); |
| 1571 | |
| 1572 | // Record that all original CRCs were OK so as not to raise flags |
| 1573 | // when doing a disk verification |
| 1574 | mainCrcOk = secondCrcOk = mainPartsCrcOk = secondPartsCrcOk = 1; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1575 | } // GPTData::XFormPartitions() |
| 1576 | |
| 1577 | // Transforms BSD disklabel on the specified partition (numbered from 0). |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1578 | // If an invalid partition number is given, the program does nothing. |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1579 | // Returns the number of new partitions created. |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1580 | int GPTData::XFormDisklabel(uint32_t partNum) { |
| 1581 | uint32_t low, high; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1582 | int goOn = 1, numDone = 0; |
| 1583 | BSDData disklabel; |
| 1584 | |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1585 | if (GetPartRange(&low, &high) == 0) { |
| 1586 | goOn = 0; |
| 1587 | cout << "No partitions!\n"; |
| 1588 | } // if |
| 1589 | if (partNum > high) { |
| 1590 | goOn = 0; |
| 1591 | cout << "Specified partition is invalid!\n"; |
| 1592 | } // if |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1593 | |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1594 | // If all is OK, read the disklabel and convert it. |
| 1595 | if (goOn) { |
| 1596 | goOn = disklabel.ReadBSDData(&myDisk, partitions[partNum].GetFirstLBA(), |
| 1597 | partitions[partNum].GetLastLBA()); |
| 1598 | if ((goOn) && (disklabel.IsDisklabel())) { |
| 1599 | numDone = XFormDisklabel(&disklabel); |
| 1600 | if (numDone == 1) |
| 1601 | cout << "Converted 1 BSD partition.\n"; |
| 1602 | else |
| 1603 | cout << "Converted " << numDone << " BSD partitions.\n"; |
| 1604 | } else { |
| 1605 | cout << "Unable to convert partitions! Unrecognized BSD disklabel.\n"; |
| 1606 | } // if/else |
| 1607 | } // if |
| 1608 | if (numDone > 0) { // converted partitions; delete carrier |
| 1609 | partitions[partNum].BlankPartition(); |
| 1610 | } // if |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1611 | return numDone; |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 1612 | } // GPTData::XFormDisklabel(uint32_t i) |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1613 | |
| 1614 | // Transform the partitions on an already-loaded BSD disklabel... |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1615 | int GPTData::XFormDisklabel(BSDData* disklabel) { |
| 1616 | int i, partNum = 0, numDone = 0; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1617 | |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1618 | if (disklabel->IsDisklabel()) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1619 | for (i = 0; i < disklabel->GetNumParts(); i++) { |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1620 | partNum = FindFirstFreePart(); |
| 1621 | if (partNum >= 0) { |
| 1622 | partitions[partNum] = disklabel->AsGPT(i); |
| 1623 | if (partitions[partNum].IsUsed()) |
| 1624 | numDone++; |
| 1625 | } // if |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1626 | } // for |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1627 | if (partNum == -1) |
| 1628 | cerr << "Warning! Too many partitions to convert!\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1629 | } // if |
| 1630 | |
| 1631 | // Record that all original CRCs were OK so as not to raise flags |
| 1632 | // when doing a disk verification |
| 1633 | mainCrcOk = secondCrcOk = mainPartsCrcOk = secondPartsCrcOk = 1; |
| 1634 | |
| 1635 | return numDone; |
| 1636 | } // GPTData::XFormDisklabel(BSDData* disklabel) |
| 1637 | |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1638 | // Add one GPT partition to MBR. Used by PartsToMBR() functions. Created |
| 1639 | // partition has the active/bootable flag UNset and uses the GPT fdisk |
| 1640 | // type code divided by 0x0100 as the MBR type code. |
| 1641 | // Returns 1 if operation was 100% successful, 0 if there were ANY |
| 1642 | // problems. |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 1643 | int GPTData::OnePartToMBR(uint32_t gptPart, int mbrPart) { |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1644 | int allOK = 1; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1645 | |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 1646 | if ((mbrPart < 0) || (mbrPart > 3)) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1647 | cout << "MBR partition " << mbrPart + 1 << " is out of range; omitting it.\n"; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 1648 | allOK = 0; |
| 1649 | } // if |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1650 | if (gptPart >= numParts) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1651 | cout << "GPT partition " << gptPart + 1 << " is out of range; omitting it.\n"; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 1652 | allOK = 0; |
| 1653 | } // if |
| 1654 | if (allOK && (partitions[gptPart].GetLastLBA() == UINT64_C(0))) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1655 | cout << "GPT partition " << gptPart + 1 << " is undefined; omitting it.\n"; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 1656 | allOK = 0; |
| 1657 | } // if |
| 1658 | if (allOK && (partitions[gptPart].GetFirstLBA() <= UINT32_MAX) && |
| 1659 | (partitions[gptPart].GetLengthLBA() <= UINT32_MAX)) { |
| 1660 | if (partitions[gptPart].GetLastLBA() > UINT32_MAX) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1661 | cout << "Caution: Partition end point past 32-bit pointer boundary;" |
| 1662 | << " some OSes may\nreact strangely.\n"; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1663 | } // if |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 1664 | protectiveMBR.MakePart(mbrPart, (uint32_t) partitions[gptPart].GetFirstLBA(), |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1665 | (uint32_t) partitions[gptPart].GetLengthLBA(), |
| 1666 | partitions[gptPart].GetHexType() / 256, 0); |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 1667 | } else { // partition out of range |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1668 | if (allOK) // Display only if "else" triggered by out-of-bounds condition |
| 1669 | cout << "Partition " << gptPart + 1 << " begins beyond the 32-bit pointer limit of MBR " |
| 1670 | << "partitions, or is\n too big; omitting it.\n"; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 1671 | allOK = 0; |
| 1672 | } // if/else |
| 1673 | return allOK; |
| 1674 | } // GPTData::OnePartToMBR() |
| 1675 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1676 | |
| 1677 | /********************************************************************** |
| 1678 | * * |
| 1679 | * Functions that adjust GPT data structures WITHOUT user interaction * |
| 1680 | * (they may display information for the user's benefit, though) * |
| 1681 | * * |
| 1682 | **********************************************************************/ |
| 1683 | |
| 1684 | // Resizes GPT to specified number of entries. Creates a new table if |
srs5694 | 706e512 | 2012-01-21 13:47:24 -0500 | [diff] [blame] | 1685 | // necessary, copies data if it already exists. If fillGPTSectors is 1 |
| 1686 | // (the default), rounds numEntries to fill all the sectors necessary to |
| 1687 | // hold the GPT. |
| 1688 | // Returns 1 if all goes well, 0 if an error is encountered. |
| 1689 | int GPTData::SetGPTSize(uint32_t numEntries, int fillGPTSectors) { |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1690 | GPTPart* newParts; |
srs5694 | 706e512 | 2012-01-21 13:47:24 -0500 | [diff] [blame] | 1691 | uint32_t i, high, copyNum, entriesPerSector; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1692 | int allOK = 1; |
| 1693 | |
| 1694 | // First, adjust numEntries upward, if necessary, to get a number |
| 1695 | // that fills the allocated sectors |
srs5694 | 706e512 | 2012-01-21 13:47:24 -0500 | [diff] [blame] | 1696 | entriesPerSector = blockSize / GPT_SIZE; |
| 1697 | if (fillGPTSectors && ((numEntries % entriesPerSector) != 0)) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1698 | cout << "Adjusting GPT size from " << numEntries << " to "; |
srs5694 | 706e512 | 2012-01-21 13:47:24 -0500 | [diff] [blame] | 1699 | numEntries = ((numEntries / entriesPerSector) + 1) * entriesPerSector; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1700 | cout << numEntries << " to fill the sector\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1701 | } // if |
| 1702 | |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1703 | // Do the work only if the # of partitions is changing. Along with being |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 1704 | // efficient, this prevents mucking with the location of the secondary |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1705 | // partition table, which causes problems when loading data from a RAID |
| 1706 | // array that's been expanded because this function is called when loading |
| 1707 | // data. |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1708 | if (((numEntries != numParts) || (partitions == NULL)) && (numEntries > 0)) { |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 1709 | newParts = new GPTPart [numEntries]; |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1710 | if (newParts != NULL) { |
| 1711 | if (partitions != NULL) { // existing partitions; copy them over |
| 1712 | GetPartRange(&i, &high); |
| 1713 | if (numEntries < (high + 1)) { // Highest entry too high for new # |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1714 | cout << "The highest-numbered partition is " << high + 1 |
| 1715 | << ", which is greater than the requested\n" |
| 1716 | << "partition table size of " << numEntries |
| 1717 | << "; cannot resize. Perhaps sorting will help.\n"; |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1718 | allOK = 0; |
srs5694 | 815fb65 | 2011-03-18 12:35:56 -0400 | [diff] [blame] | 1719 | delete[] newParts; |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1720 | } else { // go ahead with copy |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1721 | if (numEntries < numParts) |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1722 | copyNum = numEntries; |
| 1723 | else |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1724 | copyNum = numParts; |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1725 | for (i = 0; i < copyNum; i++) { |
| 1726 | newParts[i] = partitions[i]; |
| 1727 | } // for |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 1728 | delete[] partitions; |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1729 | partitions = newParts; |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1730 | } // if |
| 1731 | } else { // No existing partition table; just create it |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1732 | partitions = newParts; |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1733 | } // if/else existing partitions |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1734 | numParts = numEntries; |
srs5694 | 706e512 | 2012-01-21 13:47:24 -0500 | [diff] [blame] | 1735 | mainHeader.firstUsableLBA = ((numEntries * GPT_SIZE) / blockSize) + (((numEntries * GPT_SIZE) % blockSize) != 0) + 2 ; |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1736 | secondHeader.firstUsableLBA = mainHeader.firstUsableLBA; |
| 1737 | MoveSecondHeaderToEnd(); |
| 1738 | if (diskSize > 0) |
| 1739 | CheckGPTSize(); |
| 1740 | } else { // Bad memory allocation |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 1741 | cerr << "Error allocating memory for partition table! Size is unchanged!\n"; |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1742 | allOK = 0; |
| 1743 | } // if/else |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1744 | } // if/else |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1745 | mainHeader.numParts = numParts; |
| 1746 | secondHeader.numParts = numParts; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1747 | return (allOK); |
| 1748 | } // GPTData::SetGPTSize() |
| 1749 | |
| 1750 | // Blank the partition array |
| 1751 | void GPTData::BlankPartitions(void) { |
| 1752 | uint32_t i; |
| 1753 | |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1754 | for (i = 0; i < numParts; i++) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1755 | partitions[i].BlankPartition(); |
| 1756 | } // for |
| 1757 | } // GPTData::BlankPartitions() |
| 1758 | |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 1759 | // Delete a partition by number. Returns 1 if successful, |
| 1760 | // 0 if there was a problem. Returns 1 if partition was in |
| 1761 | // range, 0 if it was out of range. |
| 1762 | int GPTData::DeletePartition(uint32_t partNum) { |
| 1763 | uint64_t startSector, length; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1764 | uint32_t low, high, numUsedParts, retval = 1;; |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 1765 | |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1766 | numUsedParts = GetPartRange(&low, &high); |
| 1767 | if ((numUsedParts > 0) && (partNum >= low) && (partNum <= high)) { |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 1768 | // In case there's a protective MBR, look for & delete matching |
| 1769 | // MBR partition.... |
| 1770 | startSector = partitions[partNum].GetFirstLBA(); |
| 1771 | length = partitions[partNum].GetLengthLBA(); |
| 1772 | protectiveMBR.DeleteByLocation(startSector, length); |
| 1773 | |
| 1774 | // Now delete the GPT partition |
| 1775 | partitions[partNum].BlankPartition(); |
| 1776 | } else { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1777 | cerr << "Partition number " << partNum + 1 << " out of range!\n"; |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 1778 | retval = 0; |
| 1779 | } // if/else |
| 1780 | return retval; |
| 1781 | } // GPTData::DeletePartition(uint32_t partNum) |
| 1782 | |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1783 | // Non-interactively create a partition. |
| 1784 | // Returns 1 if the operation was successful, 0 if a problem was discovered. |
srs5694 | e321d44 | 2010-01-29 17:44:04 -0500 | [diff] [blame] | 1785 | uint32_t GPTData::CreatePartition(uint32_t partNum, uint64_t startSector, uint64_t endSector) { |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 1786 | int retval = 1; // assume there'll be no problems |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 1787 | uint64_t origSector = startSector; |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 1788 | |
| 1789 | if (IsFreePartNum(partNum)) { |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 1790 | if (Align(&startSector)) { |
| 1791 | cout << "Information: Moved requested sector from " << origSector << " to " |
| 1792 | << startSector << " in\norder to align on " << sectorAlignment |
| 1793 | << "-sector boundaries.\n"; |
| 1794 | } // if |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 1795 | if (IsFree(startSector) && (startSector <= endSector)) { |
| 1796 | if (FindLastInFree(startSector) >= endSector) { |
| 1797 | partitions[partNum].SetFirstLBA(startSector); |
| 1798 | partitions[partNum].SetLastLBA(endSector); |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame] | 1799 | partitions[partNum].SetType(DEFAULT_GPT_TYPE); |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 1800 | partitions[partNum].RandomizeUniqueGUID(); |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 1801 | } else retval = 0; // if free space until endSector |
| 1802 | } else retval = 0; // if startSector is free |
| 1803 | } else retval = 0; // if legal partition number |
| 1804 | return retval; |
| 1805 | } // GPTData::CreatePartition(partNum, startSector, endSector) |
| 1806 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1807 | // Sort the GPT entries, eliminating gaps and making for a logical |
srs5694 | 9a46b04 | 2011-03-15 00:34:10 -0400 | [diff] [blame] | 1808 | // ordering. |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1809 | void GPTData::SortGPT(void) { |
srs5694 | 9a46b04 | 2011-03-15 00:34:10 -0400 | [diff] [blame] | 1810 | if (numParts > 0) |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 1811 | sort(partitions, partitions + numParts); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1812 | } // GPTData::SortGPT() |
| 1813 | |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1814 | // Swap the contents of two partitions. |
| 1815 | // Returns 1 if successful, 0 if either partition is out of range |
| 1816 | // (that is, not a legal number; either or both can be empty). |
| 1817 | // Note that if partNum1 = partNum2 and this number is in range, |
| 1818 | // it will be considered successful. |
| 1819 | int GPTData::SwapPartitions(uint32_t partNum1, uint32_t partNum2) { |
| 1820 | GPTPart temp; |
| 1821 | int allOK = 1; |
| 1822 | |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1823 | if ((partNum1 < numParts) && (partNum2 < numParts)) { |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1824 | if (partNum1 != partNum2) { |
| 1825 | temp = partitions[partNum1]; |
| 1826 | partitions[partNum1] = partitions[partNum2]; |
| 1827 | partitions[partNum2] = temp; |
| 1828 | } // if |
| 1829 | } else allOK = 0; // partition numbers are valid |
| 1830 | return allOK; |
| 1831 | } // GPTData::SwapPartitions() |
| 1832 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1833 | // Set up data structures for entirely new set of partitions on the |
| 1834 | // specified device. Returns 1 if OK, 0 if there were problems. |
srs5694 | e35eb1b | 2009-09-14 00:29:34 -0400 | [diff] [blame] | 1835 | // Note that this function does NOT clear the protectiveMBR data |
| 1836 | // structure, since it may hold the original MBR partitions if the |
| 1837 | // program was launched on an MBR disk, and those may need to be |
| 1838 | // converted to GPT format. |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1839 | int GPTData::ClearGPTData(void) { |
srs5694 | e35eb1b | 2009-09-14 00:29:34 -0400 | [diff] [blame] | 1840 | int goOn = 1, i; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1841 | |
| 1842 | // Set up the partition table.... |
srs5694 | 9a46b04 | 2011-03-15 00:34:10 -0400 | [diff] [blame] | 1843 | delete[] partitions; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1844 | partitions = NULL; |
| 1845 | SetGPTSize(NUM_GPT_ENTRIES); |
| 1846 | |
| 1847 | // Now initialize a bunch of stuff that's static.... |
| 1848 | mainHeader.signature = GPT_SIGNATURE; |
| 1849 | mainHeader.revision = 0x00010000; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 1850 | mainHeader.headerSize = HEADER_SIZE; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1851 | mainHeader.reserved = 0; |
| 1852 | mainHeader.currentLBA = UINT64_C(1); |
| 1853 | mainHeader.partitionEntriesLBA = (uint64_t) 2; |
| 1854 | mainHeader.sizeOfPartitionEntries = GPT_SIZE; |
| 1855 | for (i = 0; i < GPT_RESERVED; i++) { |
| 1856 | mainHeader.reserved2[i] = '\0'; |
| 1857 | } // for |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 1858 | if (blockSize > 0) |
| 1859 | sectorAlignment = DEFAULT_ALIGNMENT * SECTOR_SIZE / blockSize; |
| 1860 | else |
| 1861 | sectorAlignment = DEFAULT_ALIGNMENT; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1862 | |
| 1863 | // Now some semi-static items (computed based on end of disk) |
| 1864 | mainHeader.backupLBA = diskSize - UINT64_C(1); |
| 1865 | mainHeader.lastUsableLBA = diskSize - mainHeader.firstUsableLBA; |
| 1866 | |
| 1867 | // Set a unique GUID for the disk, based on random numbers |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 1868 | mainHeader.diskGUID.Randomize(); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1869 | |
| 1870 | // Copy main header to backup header |
| 1871 | RebuildSecondHeader(); |
| 1872 | |
| 1873 | // Blank out the partitions array.... |
| 1874 | BlankPartitions(); |
| 1875 | |
| 1876 | // Flag all CRCs as being OK.... |
| 1877 | mainCrcOk = 1; |
| 1878 | secondCrcOk = 1; |
| 1879 | mainPartsCrcOk = 1; |
| 1880 | secondPartsCrcOk = 1; |
| 1881 | |
| 1882 | return (goOn); |
| 1883 | } // GPTData::ClearGPTData() |
| 1884 | |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1885 | // Set the location of the second GPT header data to the end of the disk. |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 1886 | // If the disk size has actually changed, this also adjusts the protective |
| 1887 | // entry in the MBR, since it's probably no longer correct. |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1888 | // Used internally and called by the 'e' option on the recovery & |
| 1889 | // transformation menu, to help users of RAID arrays who add disk space |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 1890 | // to their arrays or to adjust data structures in restore operations |
| 1891 | // involving unequal-sized disks. |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1892 | void GPTData::MoveSecondHeaderToEnd() { |
srs5694 | 8bb7876 | 2009-11-24 15:43:49 -0500 | [diff] [blame] | 1893 | mainHeader.backupLBA = secondHeader.currentLBA = diskSize - UINT64_C(1); |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 1894 | if (mainHeader.lastUsableLBA != diskSize - mainHeader.firstUsableLBA) { |
| 1895 | if (protectiveMBR.GetValidity() == hybrid) { |
| 1896 | protectiveMBR.OptimizeEESize(); |
| 1897 | RecomputeCHS(); |
| 1898 | } // if |
| 1899 | if (protectiveMBR.GetValidity() == gpt) |
| 1900 | MakeProtectiveMBR(); |
| 1901 | } // if |
srs5694 | 8bb7876 | 2009-11-24 15:43:49 -0500 | [diff] [blame] | 1902 | mainHeader.lastUsableLBA = secondHeader.lastUsableLBA = diskSize - mainHeader.firstUsableLBA; |
| 1903 | secondHeader.partitionEntriesLBA = secondHeader.lastUsableLBA + UINT64_C(1); |
| 1904 | } // GPTData::FixSecondHeaderLocation() |
| 1905 | |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 1906 | // Sets the partition's name to the specified UnicodeString without |
| 1907 | // user interaction. |
| 1908 | // Returns 1 on success, 0 on failure (invalid partition number). |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 1909 | int GPTData::SetName(uint32_t partNum, const UnicodeString & theName) { |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 1910 | int retval = 1; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1911 | |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 1912 | if (IsUsedPartNum(partNum)) |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1913 | partitions[partNum].SetName(theName); |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 1914 | else |
| 1915 | retval = 0; |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 1916 | |
| 1917 | return retval; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1918 | } // GPTData::SetName |
| 1919 | |
| 1920 | // Set the disk GUID to the specified value. Note that the header CRCs must |
| 1921 | // be recomputed after calling this function. |
| 1922 | void GPTData::SetDiskGUID(GUIDData newGUID) { |
| 1923 | mainHeader.diskGUID = newGUID; |
| 1924 | secondHeader.diskGUID = newGUID; |
| 1925 | } // SetDiskGUID() |
| 1926 | |
| 1927 | // Set the unique GUID of the specified partition. Returns 1 on |
| 1928 | // successful completion, 0 if there were problems (invalid |
| 1929 | // partition number). |
| 1930 | int GPTData::SetPartitionGUID(uint32_t pn, GUIDData theGUID) { |
| 1931 | int retval = 0; |
| 1932 | |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1933 | if (pn < numParts) { |
srs5694 | e69e680 | 2012-01-20 22:37:12 -0500 | [diff] [blame] | 1934 | if (partitions[pn].IsUsed()) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1935 | partitions[pn].SetUniqueGUID(theGUID); |
| 1936 | retval = 1; |
| 1937 | } // if |
| 1938 | } // if |
| 1939 | return retval; |
| 1940 | } // GPTData::SetPartitionGUID() |
| 1941 | |
srs5694 | 9ba5421 | 2010-05-18 23:24:02 -0400 | [diff] [blame] | 1942 | // Set new random GUIDs for the disk and all partitions. Intended to be used |
| 1943 | // after disk cloning or similar operations that don't randomize the GUIDs. |
| 1944 | void GPTData::RandomizeGUIDs(void) { |
| 1945 | uint32_t i; |
| 1946 | |
| 1947 | mainHeader.diskGUID.Randomize(); |
| 1948 | secondHeader.diskGUID = mainHeader.diskGUID; |
| 1949 | for (i = 0; i < numParts; i++) |
| 1950 | if (partitions[i].IsUsed()) |
| 1951 | partitions[i].RandomizeUniqueGUID(); |
| 1952 | } // GPTData::RandomizeGUIDs() |
| 1953 | |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 1954 | // Change partition type code non-interactively. Returns 1 if |
| 1955 | // successful, 0 if not.... |
srs5694 | 327129e | 2010-09-22 01:07:31 -0400 | [diff] [blame] | 1956 | int GPTData::ChangePartType(uint32_t partNum, PartType theGUID) { |
| 1957 | int retval = 1; |
| 1958 | |
| 1959 | if (!IsFreePartNum(partNum)) { |
| 1960 | partitions[partNum].SetType(theGUID); |
| 1961 | } else retval = 0; |
| 1962 | return retval; |
| 1963 | } // GPTData::ChangePartType() |
| 1964 | |
srs5694 | 9ba5421 | 2010-05-18 23:24:02 -0400 | [diff] [blame] | 1965 | // Recompute the CHS values of all the MBR partitions. Used to reset |
| 1966 | // CHS values that some BIOSes require, despite the fact that the |
| 1967 | // resulting CHS values violate the GPT standard. |
| 1968 | void GPTData::RecomputeCHS(void) { |
| 1969 | int i; |
| 1970 | |
| 1971 | for (i = 0; i < 4; i++) |
| 1972 | protectiveMBR.RecomputeCHS(i); |
| 1973 | } // GPTData::RecomputeCHS() |
| 1974 | |
srs5694 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame] | 1975 | // Adjust sector number so that it falls on a sector boundary that's a |
| 1976 | // multiple of sectorAlignment. This is done to improve the performance |
| 1977 | // of Western Digital Advanced Format disks and disks with similar |
| 1978 | // technology from other companies, which use 4096-byte sectors |
| 1979 | // internally although they translate to 512-byte sectors for the |
| 1980 | // benefit of the OS. If partitions aren't properly aligned on these |
| 1981 | // disks, some filesystem data structures can span multiple physical |
| 1982 | // sectors, degrading performance. This function should be called |
| 1983 | // only on the FIRST sector of the partition, not the last! |
| 1984 | // This function returns 1 if the alignment was altered, 0 if it |
| 1985 | // was unchanged. |
| 1986 | int GPTData::Align(uint64_t* sector) { |
| 1987 | int retval = 0, sectorOK = 0; |
srs5694 | 00b6d7a | 2011-06-26 22:40:06 -0400 | [diff] [blame] | 1988 | uint64_t earlier, later, testSector; |
srs5694 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame] | 1989 | |
| 1990 | if ((*sector % sectorAlignment) != 0) { |
srs5694 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame] | 1991 | earlier = (*sector / sectorAlignment) * sectorAlignment; |
| 1992 | later = earlier + (uint64_t) sectorAlignment; |
| 1993 | |
| 1994 | // Check to see that every sector between the earlier one and the |
| 1995 | // requested one is clear, and that it's not too early.... |
| 1996 | if (earlier >= mainHeader.firstUsableLBA) { |
srs5694 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame] | 1997 | sectorOK = 1; |
| 1998 | testSector = earlier; |
| 1999 | do { |
| 2000 | sectorOK = IsFree(testSector++); |
| 2001 | } while ((sectorOK == 1) && (testSector < *sector)); |
| 2002 | if (sectorOK == 1) { |
| 2003 | *sector = earlier; |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 2004 | retval = 1; |
srs5694 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame] | 2005 | } // if |
| 2006 | } // if firstUsableLBA check |
| 2007 | |
| 2008 | // If couldn't move the sector earlier, try to move it later instead.... |
| 2009 | if ((sectorOK != 1) && (later <= mainHeader.lastUsableLBA)) { |
| 2010 | sectorOK = 1; |
| 2011 | testSector = later; |
| 2012 | do { |
| 2013 | sectorOK = IsFree(testSector--); |
| 2014 | } while ((sectorOK == 1) && (testSector > *sector)); |
| 2015 | if (sectorOK == 1) { |
| 2016 | *sector = later; |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 2017 | retval = 1; |
srs5694 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame] | 2018 | } // if |
| 2019 | } // if |
srs5694 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame] | 2020 | } // if |
| 2021 | return retval; |
| 2022 | } // GPTData::Align() |
| 2023 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2024 | /******************************************************** |
| 2025 | * * |
| 2026 | * Functions that return data about GPT data structures * |
| 2027 | * (most of these are inline in gpt.h) * |
| 2028 | * * |
| 2029 | ********************************************************/ |
| 2030 | |
| 2031 | // Find the low and high used partition numbers (numbered from 0). |
| 2032 | // Return value is the number of partitions found. Note that the |
| 2033 | // *low and *high values are both set to 0 when no partitions |
| 2034 | // are found, as well as when a single partition in the first |
| 2035 | // position exists. Thus, the return value is the only way to |
| 2036 | // tell when no partitions exist. |
| 2037 | int GPTData::GetPartRange(uint32_t *low, uint32_t *high) { |
| 2038 | uint32_t i; |
| 2039 | int numFound = 0; |
| 2040 | |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 2041 | *low = numParts + 1; // code for "not found" |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2042 | *high = 0; |
srs5694 | 9a46b04 | 2011-03-15 00:34:10 -0400 | [diff] [blame] | 2043 | for (i = 0; i < numParts; i++) { |
srs5694 | e69e680 | 2012-01-20 22:37:12 -0500 | [diff] [blame] | 2044 | if (partitions[i].IsUsed()) { // it exists |
srs5694 | 9a46b04 | 2011-03-15 00:34:10 -0400 | [diff] [blame] | 2045 | *high = i; // since we're counting up, set the high value |
| 2046 | // Set the low value only if it's not yet found... |
| 2047 | if (*low == (numParts + 1)) *low = i; |
| 2048 | numFound++; |
| 2049 | } // if |
| 2050 | } // for |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2051 | |
| 2052 | // Above will leave *low pointing to its "not found" value if no partitions |
| 2053 | // are defined, so reset to 0 if this is the case.... |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 2054 | if (*low == (numParts + 1)) |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2055 | *low = 0; |
| 2056 | return numFound; |
| 2057 | } // GPTData::GetPartRange() |
| 2058 | |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 2059 | // Returns the value of the first free partition, or -1 if none is |
| 2060 | // unused. |
| 2061 | int GPTData::FindFirstFreePart(void) { |
| 2062 | int i = 0; |
| 2063 | |
| 2064 | if (partitions != NULL) { |
srs5694 | 9a46b04 | 2011-03-15 00:34:10 -0400 | [diff] [blame] | 2065 | while ((i < (int) numParts) && (partitions[i].IsUsed())) |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 2066 | i++; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 2067 | if (i >= (int) numParts) |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 2068 | i = -1; |
| 2069 | } else i = -1; |
| 2070 | return i; |
| 2071 | } // GPTData::FindFirstFreePart() |
| 2072 | |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 2073 | // Returns the number of defined partitions. |
| 2074 | uint32_t GPTData::CountParts(void) { |
srs5694 | e321d44 | 2010-01-29 17:44:04 -0500 | [diff] [blame] | 2075 | uint32_t i, counted = 0; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 2076 | |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 2077 | for (i = 0; i < numParts; i++) { |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 2078 | if (partitions[i].IsUsed()) |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 2079 | counted++; |
| 2080 | } // for |
| 2081 | return counted; |
| 2082 | } // GPTData::CountParts() |
| 2083 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2084 | /**************************************************** |
| 2085 | * * |
| 2086 | * Functions that return data about disk free space * |
| 2087 | * * |
| 2088 | ****************************************************/ |
| 2089 | |
| 2090 | // Find the first available block after the starting point; returns 0 if |
| 2091 | // there are no available blocks left |
| 2092 | uint64_t GPTData::FindFirstAvailable(uint64_t start) { |
| 2093 | uint64_t first; |
| 2094 | uint32_t i; |
| 2095 | int firstMoved = 0; |
| 2096 | |
| 2097 | // Begin from the specified starting point or from the first usable |
| 2098 | // LBA, whichever is greater... |
| 2099 | if (start < mainHeader.firstUsableLBA) |
| 2100 | first = mainHeader.firstUsableLBA; |
| 2101 | else |
| 2102 | first = start; |
| 2103 | |
| 2104 | // ...now search through all partitions; if first is within an |
| 2105 | // existing partition, move it to the next sector after that |
| 2106 | // partition and repeat. If first was moved, set firstMoved |
| 2107 | // flag; repeat until firstMoved is not set, so as to catch |
| 2108 | // cases where partitions are out of sequential order.... |
| 2109 | do { |
| 2110 | firstMoved = 0; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 2111 | for (i = 0; i < numParts; i++) { |
srs5694 | e69e680 | 2012-01-20 22:37:12 -0500 | [diff] [blame] | 2112 | if ((partitions[i].IsUsed()) && (first >= partitions[i].GetFirstLBA()) && |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 2113 | (first <= partitions[i].GetLastLBA())) { // in existing part. |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2114 | first = partitions[i].GetLastLBA() + 1; |
| 2115 | firstMoved = 1; |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 2116 | } // if |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2117 | } // for |
| 2118 | } while (firstMoved == 1); |
| 2119 | if (first > mainHeader.lastUsableLBA) |
| 2120 | first = 0; |
| 2121 | return (first); |
| 2122 | } // GPTData::FindFirstAvailable() |
| 2123 | |
| 2124 | // Finds the first available sector in the largest block of unallocated |
| 2125 | // space on the disk. Returns 0 if there are no available blocks left |
| 2126 | uint64_t GPTData::FindFirstInLargest(void) { |
srs5694 | e35eb1b | 2009-09-14 00:29:34 -0400 | [diff] [blame] | 2127 | uint64_t start, firstBlock, lastBlock, segmentSize, selectedSize = 0, selectedSegment = 0; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2128 | |
| 2129 | start = 0; |
| 2130 | do { |
| 2131 | firstBlock = FindFirstAvailable(start); |
| 2132 | if (firstBlock != UINT32_C(0)) { // something's free... |
| 2133 | lastBlock = FindLastInFree(firstBlock); |
| 2134 | segmentSize = lastBlock - firstBlock + UINT32_C(1); |
| 2135 | if (segmentSize > selectedSize) { |
| 2136 | selectedSize = segmentSize; |
| 2137 | selectedSegment = firstBlock; |
| 2138 | } // if |
| 2139 | start = lastBlock + 1; |
| 2140 | } // if |
| 2141 | } while (firstBlock != 0); |
| 2142 | return selectedSegment; |
| 2143 | } // GPTData::FindFirstInLargest() |
| 2144 | |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 2145 | // Find the last available block on the disk. |
srs5694 | f5dfbfa | 2013-02-14 20:47:14 -0500 | [diff] [blame] | 2146 | // Returns 0 if there are no available sectors |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 2147 | uint64_t GPTData::FindLastAvailable(void) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2148 | uint64_t last; |
| 2149 | uint32_t i; |
| 2150 | int lastMoved = 0; |
| 2151 | |
| 2152 | // Start by assuming the last usable LBA is available.... |
| 2153 | last = mainHeader.lastUsableLBA; |
| 2154 | |
| 2155 | // ...now, similar to algorithm in FindFirstAvailable(), search |
| 2156 | // through all partitions, moving last when it's in an existing |
| 2157 | // partition. Set the lastMoved flag so we repeat to catch cases |
| 2158 | // where partitions are out of logical order. |
| 2159 | do { |
| 2160 | lastMoved = 0; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 2161 | for (i = 0; i < numParts; i++) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2162 | if ((last >= partitions[i].GetFirstLBA()) && |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 2163 | (last <= partitions[i].GetLastLBA())) { // in existing part. |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2164 | last = partitions[i].GetFirstLBA() - 1; |
| 2165 | lastMoved = 1; |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 2166 | } // if |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2167 | } // for |
| 2168 | } while (lastMoved == 1); |
| 2169 | if (last < mainHeader.firstUsableLBA) |
| 2170 | last = 0; |
| 2171 | return (last); |
| 2172 | } // GPTData::FindLastAvailable() |
| 2173 | |
| 2174 | // Find the last available block in the free space pointed to by start. |
| 2175 | uint64_t GPTData::FindLastInFree(uint64_t start) { |
| 2176 | uint64_t nearestStart; |
| 2177 | uint32_t i; |
| 2178 | |
| 2179 | nearestStart = mainHeader.lastUsableLBA; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 2180 | for (i = 0; i < numParts; i++) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2181 | if ((nearestStart > partitions[i].GetFirstLBA()) && |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 2182 | (partitions[i].GetFirstLBA() > start)) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2183 | nearestStart = partitions[i].GetFirstLBA() - 1; |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 2184 | } // if |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2185 | } // for |
| 2186 | return (nearestStart); |
| 2187 | } // GPTData::FindLastInFree() |
| 2188 | |
| 2189 | // Finds the total number of free blocks, the number of segments in which |
| 2190 | // they reside, and the size of the largest of those segments |
srs5694 | e321d44 | 2010-01-29 17:44:04 -0500 | [diff] [blame] | 2191 | uint64_t GPTData::FindFreeBlocks(uint32_t *numSegments, uint64_t *largestSegment) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2192 | uint64_t start = UINT64_C(0); // starting point for each search |
| 2193 | uint64_t totalFound = UINT64_C(0); // running total |
| 2194 | uint64_t firstBlock; // first block in a segment |
| 2195 | uint64_t lastBlock; // last block in a segment |
| 2196 | uint64_t segmentSize; // size of segment in blocks |
srs5694 | e321d44 | 2010-01-29 17:44:04 -0500 | [diff] [blame] | 2197 | uint32_t num = 0; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2198 | |
| 2199 | *largestSegment = UINT64_C(0); |
srs5694 | c54e9b4 | 2010-05-01 21:04:23 -0400 | [diff] [blame] | 2200 | if (diskSize > 0) { |
| 2201 | do { |
| 2202 | firstBlock = FindFirstAvailable(start); |
| 2203 | if (firstBlock != UINT64_C(0)) { // something's free... |
| 2204 | lastBlock = FindLastInFree(firstBlock); |
| 2205 | segmentSize = lastBlock - firstBlock + UINT64_C(1); |
| 2206 | if (segmentSize > *largestSegment) { |
| 2207 | *largestSegment = segmentSize; |
| 2208 | } // if |
| 2209 | totalFound += segmentSize; |
| 2210 | num++; |
| 2211 | start = lastBlock + 1; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2212 | } // if |
srs5694 | c54e9b4 | 2010-05-01 21:04:23 -0400 | [diff] [blame] | 2213 | } while (firstBlock != 0); |
| 2214 | } // if |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2215 | *numSegments = num; |
| 2216 | return totalFound; |
| 2217 | } // GPTData::FindFreeBlocks() |
| 2218 | |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 2219 | // Returns 1 if sector is unallocated, 0 if it's allocated to a partition. |
| 2220 | // If it's allocated, return the partition number to which it's allocated |
| 2221 | // in partNum, if that variable is non-NULL. (A value of UINT32_MAX is |
| 2222 | // returned in partNum if the sector is in use by basic GPT data structures.) |
| 2223 | int GPTData::IsFree(uint64_t sector, uint32_t *partNum) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2224 | int isFree = 1; |
| 2225 | uint32_t i; |
| 2226 | |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 2227 | for (i = 0; i < numParts; i++) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2228 | if ((sector >= partitions[i].GetFirstLBA()) && |
| 2229 | (sector <= partitions[i].GetLastLBA())) { |
| 2230 | isFree = 0; |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 2231 | if (partNum != NULL) |
| 2232 | *partNum = i; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 2233 | } // if |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2234 | } // for |
srs5694 | e35eb1b | 2009-09-14 00:29:34 -0400 | [diff] [blame] | 2235 | if ((sector < mainHeader.firstUsableLBA) || |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2236 | (sector > mainHeader.lastUsableLBA)) { |
| 2237 | isFree = 0; |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 2238 | if (partNum != NULL) |
| 2239 | *partNum = UINT32_MAX; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 2240 | } // if |
| 2241 | return (isFree); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2242 | } // GPTData::IsFree() |
| 2243 | |
srs5694 | 815fb65 | 2011-03-18 12:35:56 -0400 | [diff] [blame] | 2244 | // Returns 1 if partNum is unused AND if it's a legal value. |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 2245 | int GPTData::IsFreePartNum(uint32_t partNum) { |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 2246 | return ((partNum < numParts) && (partitions != NULL) && |
| 2247 | (!partitions[partNum].IsUsed())); |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 2248 | } // GPTData::IsFreePartNum() |
| 2249 | |
srs5694 | 815fb65 | 2011-03-18 12:35:56 -0400 | [diff] [blame] | 2250 | // Returns 1 if partNum is in use. |
| 2251 | int GPTData::IsUsedPartNum(uint32_t partNum) { |
| 2252 | return ((partNum < numParts) && (partitions != NULL) && |
| 2253 | (partitions[partNum].IsUsed())); |
| 2254 | } // GPTData::IsUsedPartNum() |
srs5694 | a8582cf | 2010-03-19 14:21:59 -0400 | [diff] [blame] | 2255 | |
| 2256 | /*********************************************************** |
| 2257 | * * |
| 2258 | * Change how functions work or return information on them * |
| 2259 | * * |
| 2260 | ***********************************************************/ |
| 2261 | |
| 2262 | // Set partition alignment value; partitions will begin on multiples of |
| 2263 | // the specified value |
| 2264 | void GPTData::SetAlignment(uint32_t n) { |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 2265 | if (n > 0) |
| 2266 | sectorAlignment = n; |
| 2267 | else |
| 2268 | cerr << "Attempt to set partition alignment to 0!\n"; |
srs5694 | a8582cf | 2010-03-19 14:21:59 -0400 | [diff] [blame] | 2269 | } // GPTData::SetAlignment() |
| 2270 | |
| 2271 | // Compute sector alignment based on the current partitions (if any). Each |
| 2272 | // partition's starting LBA is examined, and if it's divisible by a power-of-2 |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 2273 | // value less than or equal to the DEFAULT_ALIGNMENT value (adjusted for the |
| 2274 | // sector size), but not by the previously-located alignment value, then the |
| 2275 | // alignment value is adjusted down. If the computed alignment is less than 8 |
| 2276 | // and the disk is bigger than SMALLEST_ADVANCED_FORMAT, resets it to 8. This |
srs5694 | d8eed46 | 2012-12-15 01:55:21 -0500 | [diff] [blame] | 2277 | // is a safety measure for Advanced Format drives. If no partitions are |
| 2278 | // defined, the alignment value is set to DEFAULT_ALIGNMENT (2048) (or an |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 2279 | // adjustment of that based on the current sector size). The result is that new |
srs5694 | 8a4ddfc | 2010-03-21 19:05:49 -0400 | [diff] [blame] | 2280 | // drives are aligned to 2048-sector multiples but the program won't complain |
| 2281 | // about other alignments on existing disks unless a smaller-than-8 alignment |
srs5694 | d8eed46 | 2012-12-15 01:55:21 -0500 | [diff] [blame] | 2282 | // is used on big disks (as safety for Advanced Format drives). |
srs5694 | a8582cf | 2010-03-19 14:21:59 -0400 | [diff] [blame] | 2283 | // Returns the computed alignment value. |
| 2284 | uint32_t GPTData::ComputeAlignment(void) { |
| 2285 | uint32_t i = 0, found, exponent = 31; |
srs5694 | ab4b043 | 2010-09-25 20:39:52 -0400 | [diff] [blame] | 2286 | uint32_t align = DEFAULT_ALIGNMENT; |
srs5694 | a8582cf | 2010-03-19 14:21:59 -0400 | [diff] [blame] | 2287 | |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 2288 | if (blockSize > 0) |
| 2289 | align = DEFAULT_ALIGNMENT * SECTOR_SIZE / blockSize; |
| 2290 | exponent = (uint32_t) log2(align); |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 2291 | for (i = 0; i < numParts; i++) { |
srs5694 | a8582cf | 2010-03-19 14:21:59 -0400 | [diff] [blame] | 2292 | if (partitions[i].IsUsed()) { |
| 2293 | found = 0; |
| 2294 | while (!found) { |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 2295 | align = UINT64_C(1) << exponent; |
srs5694 | a8582cf | 2010-03-19 14:21:59 -0400 | [diff] [blame] | 2296 | if ((partitions[i].GetFirstLBA() % align) == 0) { |
| 2297 | found = 1; |
| 2298 | } else { |
| 2299 | exponent--; |
| 2300 | } // if/else |
| 2301 | } // while |
| 2302 | } // if |
| 2303 | } // for |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 2304 | if ((align < MIN_AF_ALIGNMENT) && (diskSize >= SMALLEST_ADVANCED_FORMAT)) |
| 2305 | align = MIN_AF_ALIGNMENT; |
| 2306 | sectorAlignment = align; |
srs5694 | a8582cf | 2010-03-19 14:21:59 -0400 | [diff] [blame] | 2307 | return align; |
| 2308 | } // GPTData::ComputeAlignment() |
| 2309 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2310 | /******************************** |
| 2311 | * * |
| 2312 | * Endianness support functions * |
| 2313 | * * |
| 2314 | ********************************/ |
| 2315 | |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 2316 | void GPTData::ReverseHeaderBytes(struct GPTHeader* header) { |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 2317 | ReverseBytes(&header->signature, 8); |
| 2318 | ReverseBytes(&header->revision, 4); |
| 2319 | ReverseBytes(&header->headerSize, 4); |
| 2320 | ReverseBytes(&header->headerCRC, 4); |
| 2321 | ReverseBytes(&header->reserved, 4); |
| 2322 | ReverseBytes(&header->currentLBA, 8); |
| 2323 | ReverseBytes(&header->backupLBA, 8); |
| 2324 | ReverseBytes(&header->firstUsableLBA, 8); |
| 2325 | ReverseBytes(&header->lastUsableLBA, 8); |
| 2326 | ReverseBytes(&header->partitionEntriesLBA, 8); |
| 2327 | ReverseBytes(&header->numParts, 4); |
| 2328 | ReverseBytes(&header->sizeOfPartitionEntries, 4); |
| 2329 | ReverseBytes(&header->partitionEntriesCRC, 4); |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 2330 | ReverseBytes(header->reserved2, GPT_RESERVED); |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 2331 | } // GPTData::ReverseHeaderBytes() |
| 2332 | |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 2333 | // Reverse byte order for all partitions. |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 2334 | void GPTData::ReversePartitionBytes() { |
| 2335 | uint32_t i; |
| 2336 | |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 2337 | for (i = 0; i < numParts; i++) { |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 2338 | partitions[i].ReversePartBytes(); |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 2339 | } // for |
| 2340 | } // GPTData::ReversePartitionBytes() |
| 2341 | |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 2342 | // Validate partition number |
| 2343 | bool GPTData::ValidPartNum (const uint32_t partNum) { |
| 2344 | if (partNum >= numParts) { |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 2345 | cerr << "Partition number out of range: " << partNum << "\n"; |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 2346 | return false; |
| 2347 | } // if |
| 2348 | return true; |
| 2349 | } // GPTData::ValidPartNum |
| 2350 | |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 2351 | // Return a single partition for inspection (not modification!) by other |
| 2352 | // functions. |
| 2353 | const GPTPart & GPTData::operator[](uint32_t partNum) const { |
| 2354 | if (partNum >= numParts) { |
srs5694 | 815fb65 | 2011-03-18 12:35:56 -0400 | [diff] [blame] | 2355 | cerr << "Partition number out of range (" << partNum << " requested, but only " |
| 2356 | << numParts << " available)\n"; |
| 2357 | exit(1); |
| 2358 | } // if |
| 2359 | if (partitions == NULL) { |
| 2360 | cerr << "No partitions defined in GPTData::operator[]; fatal error!\n"; |
| 2361 | exit(1); |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 2362 | } // if |
| 2363 | return partitions[partNum]; |
| 2364 | } // operator[] |
| 2365 | |
| 2366 | // Return (not for modification!) the disk's GUID value |
| 2367 | const GUIDData & GPTData::GetDiskGUID(void) const { |
| 2368 | return mainHeader.diskGUID; |
| 2369 | } // GPTData::GetDiskGUID() |
| 2370 | |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 2371 | // Manage attributes for a partition, based on commands passed to this function. |
| 2372 | // (Function is non-interactive.) |
| 2373 | // Returns 1 if a modification command succeeded, 0 if the command should not have |
| 2374 | // modified data, and -1 if a modification command failed. |
| 2375 | int GPTData::ManageAttributes(int partNum, const string & command, const string & bits) { |
| 2376 | int retval = 0; |
| 2377 | Attributes theAttr; |
| 2378 | |
Roderick W. Smith | 24bba6e | 2013-10-12 19:07:16 -0400 | [diff] [blame] | 2379 | if (partNum >= (int) numParts) { |
| 2380 | cerr << "Invalid partition number (" << partNum + 1 << ")\n"; |
| 2381 | retval = -1; |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 2382 | } else { |
Roderick W. Smith | 24bba6e | 2013-10-12 19:07:16 -0400 | [diff] [blame] | 2383 | if (command == "show") { |
| 2384 | ShowAttributes(partNum); |
| 2385 | } else if (command == "get") { |
| 2386 | GetAttribute(partNum, bits); |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 2387 | } else { |
Roderick W. Smith | 24bba6e | 2013-10-12 19:07:16 -0400 | [diff] [blame] | 2388 | theAttr = partitions[partNum].GetAttributes(); |
| 2389 | if (theAttr.OperateOnAttributes(partNum, command, bits)) { |
| 2390 | partitions[partNum].SetAttributes(theAttr.GetAttributes()); |
| 2391 | retval = 1; |
| 2392 | } else { |
| 2393 | retval = -1; |
| 2394 | } // if/else |
| 2395 | } // if/elseif/else |
| 2396 | } // if/else invalid partition # |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 2397 | |
| 2398 | return retval; |
| 2399 | } // GPTData::ManageAttributes() |
| 2400 | |
| 2401 | // Show all attributes for a specified partition.... |
| 2402 | void GPTData::ShowAttributes(const uint32_t partNum) { |
Roderick W. Smith | 24bba6e | 2013-10-12 19:07:16 -0400 | [diff] [blame] | 2403 | if ((partNum < numParts) && partitions[partNum].IsUsed()) |
srs5694 | e69e680 | 2012-01-20 22:37:12 -0500 | [diff] [blame] | 2404 | partitions[partNum].ShowAttributes(partNum); |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 2405 | } // GPTData::ShowAttributes |
| 2406 | |
| 2407 | // Show whether a single attribute bit is set (terse output)... |
| 2408 | void GPTData::GetAttribute(const uint32_t partNum, const string& attributeBits) { |
Roderick W. Smith | 24bba6e | 2013-10-12 19:07:16 -0400 | [diff] [blame] | 2409 | if (partNum < numParts) |
| 2410 | partitions[partNum].GetAttributes().OperateOnAttributes(partNum, "get", attributeBits); |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 2411 | } // GPTData::GetAttribute |
| 2412 | |
| 2413 | |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 2414 | /****************************************** |
| 2415 | * * |
| 2416 | * Additional non-class support functions * |
| 2417 | * * |
| 2418 | ******************************************/ |
| 2419 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 2420 | // Check to be sure that data type sizes are correct. The basic types (uint*_t) should |
| 2421 | // never fail these tests, but the struct types may fail depending on compile options. |
| 2422 | // Specifically, the -fpack-struct option to gcc may be required to ensure proper structure |
| 2423 | // sizes. |
| 2424 | int SizesOK(void) { |
| 2425 | int allOK = 1; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 2426 | |
| 2427 | if (sizeof(uint8_t) != 1) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 2428 | cerr << "uint8_t is " << sizeof(uint8_t) << " bytes, should be 1 byte; aborting!\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 2429 | allOK = 0; |
| 2430 | } // if |
| 2431 | if (sizeof(uint16_t) != 2) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 2432 | cerr << "uint16_t is " << sizeof(uint16_t) << " bytes, should be 2 bytes; aborting!\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 2433 | allOK = 0; |
| 2434 | } // if |
| 2435 | if (sizeof(uint32_t) != 4) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 2436 | cerr << "uint32_t is " << sizeof(uint32_t) << " bytes, should be 4 bytes; aborting!\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 2437 | allOK = 0; |
| 2438 | } // if |
| 2439 | if (sizeof(uint64_t) != 8) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 2440 | cerr << "uint64_t is " << sizeof(uint64_t) << " bytes, should be 8 bytes; aborting!\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 2441 | allOK = 0; |
| 2442 | } // if |
| 2443 | if (sizeof(struct MBRRecord) != 16) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 2444 | cerr << "MBRRecord is " << sizeof(MBRRecord) << " bytes, should be 16 bytes; aborting!\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 2445 | allOK = 0; |
| 2446 | } // if |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 2447 | if (sizeof(struct TempMBR) != 512) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 2448 | cerr << "TempMBR is " << sizeof(TempMBR) << " bytes, should be 512 bytes; aborting!\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 2449 | allOK = 0; |
| 2450 | } // if |
| 2451 | if (sizeof(struct GPTHeader) != 512) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 2452 | cerr << "GPTHeader is " << sizeof(GPTHeader) << " bytes, should be 512 bytes; aborting!\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 2453 | allOK = 0; |
| 2454 | } // if |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 2455 | if (sizeof(GPTPart) != 128) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 2456 | cerr << "GPTPart is " << sizeof(GPTPart) << " bytes, should be 128 bytes; aborting!\n"; |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 2457 | allOK = 0; |
| 2458 | } // if |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 2459 | if (sizeof(GUIDData) != 16) { |
| 2460 | cerr << "GUIDData is " << sizeof(GUIDData) << " bytes, should be 16 bytes; aborting!\n"; |
| 2461 | allOK = 0; |
| 2462 | } // if |
| 2463 | if (sizeof(PartType) != 16) { |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame] | 2464 | cerr << "PartType is " << sizeof(PartType) << " bytes, should be 16 bytes; aborting!\n"; |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 2465 | allOK = 0; |
| 2466 | } // if |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 2467 | return (allOK); |
| 2468 | } // SizesOK() |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2469 | |