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