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