blob: bcc053a9784489c49cbd7afe29080ed86581f844 [file] [log] [blame]
srs5694e7b4ff92009-08-18 13:16:10 -04001/* gpt.cc -- Functions for loading, saving, and manipulating legacy MBR and GPT partition
2 data. */
3
srs5694e4ac11e2009-08-31 10:13:04 -04004/* By Rod Smith, initial coding January to February, 2009 */
srs5694e7b4ff92009-08-18 13:16:10 -04005
srs5694221e0872009-08-29 15:00:31 -04006/* This program is copyright (c) 2009 by Roderick W. Smith. It is distributed
7 under the terms of the GNU GPL version 2, as detailed in the COPYING file. */
8
srs5694e7b4ff92009-08-18 13:16:10 -04009#define __STDC_LIMIT_MACROS
10#define __STDC_CONSTANT_MACROS
11
12#include <stdio.h>
srs5694e7b4ff92009-08-18 13:16:10 -040013#include <stdlib.h>
14#include <stdint.h>
15#include <fcntl.h>
16#include <string.h>
srs5694a8582cf2010-03-19 14:21:59 -040017#include <math.h>
srs5694e7b4ff92009-08-18 13:16:10 -040018#include <time.h>
19#include <sys/stat.h>
20#include <errno.h>
srs5694fed16d02010-01-27 23:03:40 -050021#include <iostream>
srs5694e7b4ff92009-08-18 13:16:10 -040022#include "crc32.h"
23#include "gpt.h"
srs5694221e0872009-08-29 15:00:31 -040024#include "bsd.h"
srs5694e7b4ff92009-08-18 13:16:10 -040025#include "support.h"
26#include "parttypes.h"
27#include "attributes.h"
srs5694546a9c72010-01-26 16:00:26 -050028#include "diskio.h"
srs569455d92612010-03-07 22:16:07 -050029#include "partnotes.h"
srs5694e7b4ff92009-08-18 13:16:10 -040030
31using namespace std;
32
srs56948f1b2d62010-05-23 13:07:19 -040033#ifdef __FreeBSD__
srs56949ba54212010-05-18 23:24:02 -040034#define log2(x) (log(x) / M_LN2)
35#endif // __FreeBSD__
36
srs56948f1b2d62010-05-23 13:07:19 -040037#ifdef _MSC_VER
38#define log2(x) (log((double) x) / log(2.0))
39#endif // Microsoft Visual C++
srs56949ba54212010-05-18 23:24:02 -040040
srs5694e7b4ff92009-08-18 13:16:10 -040041/****************************************
42 * *
43 * GPTData class and related structures *
44 * *
45 ****************************************/
46
srs5694e4ac11e2009-08-31 10:13:04 -040047// Default constructor
srs5694e7b4ff92009-08-18 13:16:10 -040048GPTData::GPTData(void) {
49 blockSize = SECTOR_SIZE; // set a default
50 diskSize = 0;
51 partitions = NULL;
52 state = gpt_valid;
srs5694fed16d02010-01-27 23:03:40 -050053 device = "";
srs56945d58fe02010-01-03 20:57:08 -050054 justLooking = 0;
srs5694e7b4ff92009-08-18 13:16:10 -040055 mainCrcOk = 0;
56 secondCrcOk = 0;
57 mainPartsCrcOk = 0;
58 secondPartsCrcOk = 0;
srs5694221e0872009-08-29 15:00:31 -040059 apmFound = 0;
60 bsdFound = 0;
srs56940873e9d2010-10-07 13:00:45 -040061 sectorAlignment = MIN_AF_ALIGNMENT; // Align partitions on 4096-byte boundaries by default
srs5694ba00fed2010-01-12 18:18:36 -050062 beQuiet = 0;
63 whichWasUsed = use_new;
srs5694e7b4ff92009-08-18 13:16:10 -040064 srand((unsigned int) time(NULL));
srs56941e093722010-01-05 00:14:19 -050065 mainHeader.numParts = 0;
srs56940283dae2010-04-28 16:44:34 -040066 numParts = 0;
srs5694e7b4ff92009-08-18 13:16:10 -040067 SetGPTSize(NUM_GPT_ENTRIES);
68} // GPTData default constructor
69
70// The following constructor loads GPT data from a device file
srs5694fed16d02010-01-27 23:03:40 -050071GPTData::GPTData(string filename) {
srs5694e7b4ff92009-08-18 13:16:10 -040072 blockSize = SECTOR_SIZE; // set a default
73 diskSize = 0;
74 partitions = NULL;
75 state = gpt_invalid;
srs5694fed16d02010-01-27 23:03:40 -050076 device = "";
srs56945d58fe02010-01-03 20:57:08 -050077 justLooking = 0;
srs5694e7b4ff92009-08-18 13:16:10 -040078 mainCrcOk = 0;
79 secondCrcOk = 0;
80 mainPartsCrcOk = 0;
81 secondPartsCrcOk = 0;
srs5694221e0872009-08-29 15:00:31 -040082 apmFound = 0;
83 bsdFound = 0;
srs56940873e9d2010-10-07 13:00:45 -040084 sectorAlignment = MIN_AF_ALIGNMENT; // Align partitions on 4096-byte boundaries by default
srs5694ba00fed2010-01-12 18:18:36 -050085 beQuiet = 0;
86 whichWasUsed = use_new;
srs5694e7b4ff92009-08-18 13:16:10 -040087 srand((unsigned int) time(NULL));
srs56941e093722010-01-05 00:14:19 -050088 mainHeader.numParts = 0;
srs56940283dae2010-04-28 16:44:34 -040089 numParts = 0;
srs56943c0af382010-01-15 19:19:18 -050090 if (!LoadPartitions(filename))
91 exit(2);
srs5694fed16d02010-01-27 23:03:40 -050092} // GPTData(string filename) constructor
srs5694e7b4ff92009-08-18 13:16:10 -040093
srs5694e4ac11e2009-08-31 10:13:04 -040094// Destructor
srs5694e7b4ff92009-08-18 13:16:10 -040095GPTData::~GPTData(void) {
srs5694cb76c672010-02-11 22:22:22 -050096 delete[] partitions;
srs5694e7b4ff92009-08-18 13:16:10 -040097} // GPTData destructor
98
srs5694e4ac11e2009-08-31 10:13:04 -040099/*********************************************************************
100 * *
101 * Begin functions that verify data, or that adjust the verification *
102 * information (compute CRCs, rebuild headers) *
103 * *
104 *********************************************************************/
srs5694e7b4ff92009-08-18 13:16:10 -0400105
srs5694e4ac11e2009-08-31 10:13:04 -0400106// Perform detailed verification, reporting on any problems found, but
107// do *NOT* recover from these problems. Returns the total number of
108// problems identified.
109int GPTData::Verify(void) {
srs5694e321d442010-01-29 17:44:04 -0500110 int problems = 0;
111 uint32_t i, numSegments;
112 uint64_t totalFree, largestSegment;
srs5694e4ac11e2009-08-31 10:13:04 -0400113
114 // First, check for CRC errors in the GPT data....
115 if (!mainCrcOk) {
116 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500117 cout << "\nProblem: The CRC for the main GPT header is invalid. The main GPT header may\n"
118 << "be corrupt. Consider loading the backup GPT header to rebuild the main GPT\n"
119 << "header ('b' on the recovery & transformation menu). This report may be a false\n"
120 << "alarm if you've already corrected other problems.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400121 } // if
122 if (!mainPartsCrcOk) {
123 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500124 cout << "\nProblem: The CRC for the main partition table is invalid. This table may be\n"
125 << "corrupt. Consider loading the backup partition table ('c' on the recovery &\n"
126 << "transformation menu). This report may be a false alarm if you've already\n"
127 << "corrected other problems.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400128 } // if
129 if (!secondCrcOk) {
130 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500131 cout << "\nProblem: The CRC for the backup GPT header is invalid. The backup GPT header\n"
132 << "may be corrupt. Consider using the main GPT header to rebuild the backup GPT\n"
133 << "header ('d' on the recovery & transformation menu). This report may be a false\n"
134 << "alarm if you've already corrected other problems.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400135 } // if
136 if (!secondPartsCrcOk) {
137 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500138 cout << "\nCaution: The CRC for the backup partition table is invalid. This table may\n"
139 << "be corrupt. This program will automatically create a new backup partition\n"
140 << "table when you save your partitions.\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400141 } // if
142
srs5694978041c2009-09-21 20:51:47 -0400143 // Now check that the main and backup headers both point to themselves....
144 if (mainHeader.currentLBA != 1) {
145 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500146 cout << "\nProblem: The main header's self-pointer doesn't point to itself. This problem\n"
147 << "is being automatically corrected, but it may be a symptom of more serious\n"
148 << "problems. Think carefully before saving changes with 'w' or using this disk.\n";
srs5694978041c2009-09-21 20:51:47 -0400149 mainHeader.currentLBA = 1;
150 } // if
151 if (secondHeader.currentLBA != (diskSize - UINT64_C(1))) {
152 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500153 cout << "\nProblem: The secondary header's self-pointer indicates that it doesn't reside\n"
154 << "at the end of the disk. If you've added a disk to a RAID array, use the 'e'\n"
155 << "option on the experts' menu to adjust the secondary header's and partition\n"
156 << "table's locations.\n";
srs5694978041c2009-09-21 20:51:47 -0400157 } // if
158
159 // Now check that critical main and backup GPT entries match each other
srs5694e4ac11e2009-08-31 10:13:04 -0400160 if (mainHeader.currentLBA != secondHeader.backupLBA) {
161 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500162 cout << "\nProblem: main GPT header's current LBA pointer (" << mainHeader.currentLBA
163 << ") doesn't\nmatch the backup GPT header's alternate LBA pointer("
164 << secondHeader.backupLBA << ").\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400165 } // if
166 if (mainHeader.backupLBA != secondHeader.currentLBA) {
167 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500168 cout << "\nProblem: main GPT header's backup LBA pointer (" << mainHeader.backupLBA
169 << ") doesn't\nmatch the backup GPT header's current LBA pointer ("
170 << secondHeader.currentLBA << ").\n"
171 << "The 'e' option on the experts' menu may fix this problem.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400172 } // if
173 if (mainHeader.firstUsableLBA != secondHeader.firstUsableLBA) {
174 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500175 cout << "\nProblem: main GPT header's first usable LBA pointer (" << mainHeader.firstUsableLBA
176 << ") doesn't\nmatch the backup GPT header's first usable LBA pointer ("
177 << secondHeader.firstUsableLBA << ")\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400178 } // if
179 if (mainHeader.lastUsableLBA != secondHeader.lastUsableLBA) {
180 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500181 cout << "\nProblem: main GPT header's last usable LBA pointer (" << mainHeader.lastUsableLBA
182 << ") doesn't\nmatch the backup GPT header's last usable LBA pointer ("
183 << secondHeader.lastUsableLBA << ")\n"
184 << "The 'e' option on the experts' menu can probably fix this problem.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400185 } // if
srs56946699b012010-02-04 00:55:30 -0500186 if ((mainHeader.diskGUID != secondHeader.diskGUID)) {
srs5694e4ac11e2009-08-31 10:13:04 -0400187 problems++;
srs56945a081752010-09-24 20:39:41 -0400188 cout << "\nProblem: main header's disk GUID (" << mainHeader.diskGUID
srs5694fed16d02010-01-27 23:03:40 -0500189 << ") doesn't\nmatch the backup GPT header's disk GUID ("
srs56945a081752010-09-24 20:39:41 -0400190 << secondHeader.diskGUID << ")\n"
srs5694fed16d02010-01-27 23:03:40 -0500191 << "You should use the 'b' or 'd' option on the recovery & transformation menu to\n"
192 << "select one or the other header.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400193 } // if
194 if (mainHeader.numParts != secondHeader.numParts) {
195 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500196 cout << "\nProblem: main GPT header's number of partitions (" << mainHeader.numParts
197 << ") doesn't\nmatch the backup GPT header's number of partitions ("
198 << secondHeader.numParts << ")\n"
199 << "Resizing the partition table ('s' on the experts' menu) may help.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400200 } // if
201 if (mainHeader.sizeOfPartitionEntries != secondHeader.sizeOfPartitionEntries) {
202 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500203 cout << "\nProblem: main GPT header's size of partition entries ("
204 << mainHeader.sizeOfPartitionEntries << ") doesn't\n"
205 << "match the backup GPT header's size of partition entries ("
206 << secondHeader.sizeOfPartitionEntries << ")\n"
207 << "You should use the 'b' or 'd' option on the recovery & transformation menu to\n"
208 << "select one or the other header.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400209 } // if
210
211 // Now check for a few other miscellaneous problems...
212 // Check that the disk size will hold the data...
213 if (mainHeader.backupLBA > diskSize) {
214 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500215 cout << "\nProblem: Disk is too small to hold all the data!\n"
216 << "(Disk size is " << diskSize << " sectors, needs to be "
217 << mainHeader.backupLBA << " sectors.)\n"
218 << "The 'e' option on the experts' menu may fix this problem.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400219 } // if
220
221 // Check for overlapping partitions....
222 problems += FindOverlaps();
223
srs569455d92612010-03-07 22:16:07 -0500224 // Check for insane partitions (start after end, hugely big, etc.)
225 problems += FindInsanePartitions();
226
srs5694e4ac11e2009-08-31 10:13:04 -0400227 // Check for mismatched MBR and GPT partitions...
228 problems += FindHybridMismatches();
229
srs5694327129e2010-09-22 01:07:31 -0400230 // Check for MBR-specific problems....
231 problems += VerifyMBR();
232
srs5694e4ac11e2009-08-31 10:13:04 -0400233 // Verify that partitions don't run into GPT data areas....
234 problems += CheckGPTSize();
235
srs56941d1448a2009-12-31 21:20:19 -0500236 // Check that partitions are aligned on proper boundaries (for WD Advanced
237 // Format and similar disks)....
srs56940283dae2010-04-28 16:44:34 -0400238 for (i = 0; i < numParts; i++) {
srs56941d1448a2009-12-31 21:20:19 -0500239 if ((partitions[i].GetFirstLBA() % sectorAlignment) != 0) {
srs5694fed16d02010-01-27 23:03:40 -0500240 cout << "\nCaution: Partition " << i + 1 << " doesn't begin on a "
241 << sectorAlignment << "-sector boundary. This may\nresult "
242 << "in degraded performance on some modern (2009 and later) hard disks.\n";
srs56941d1448a2009-12-31 21:20:19 -0500243 } // if
244 } // for
245
srs5694e4ac11e2009-08-31 10:13:04 -0400246 // Now compute available space, but only if no problems found, since
247 // problems could affect the results
248 if (problems == 0) {
249 totalFree = FindFreeBlocks(&numSegments, &largestSegment);
srs5694fed16d02010-01-27 23:03:40 -0500250 cout << "No problems found. " << totalFree << " free sectors ("
srs56940873e9d2010-10-07 13:00:45 -0400251 << BytesToSI(totalFree, blockSize) << ") available in "
srs5694fed16d02010-01-27 23:03:40 -0500252 << numSegments << "\nsegments, the largest of which is "
srs56940873e9d2010-10-07 13:00:45 -0400253 << largestSegment << " (" << BytesToSI(largestSegment, blockSize)
srs56940283dae2010-04-28 16:44:34 -0400254 << ") in size.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400255 } else {
srs56940a697312010-01-28 21:10:52 -0500256 cout << "\nIdentified " << problems << " problems!\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400257 } // if/else
srs5694e4ac11e2009-08-31 10:13:04 -0400258
259 return (problems);
260} // GPTData::Verify()
srs5694e7b4ff92009-08-18 13:16:10 -0400261
262// Checks to see if the GPT tables overrun existing partitions; if they
srs5694221e0872009-08-29 15:00:31 -0400263// do, issues a warning but takes no action. Returns number of problems
264// detected (0 if OK, 1 to 2 if problems).
srs5694e7b4ff92009-08-18 13:16:10 -0400265int GPTData::CheckGPTSize(void) {
266 uint64_t overlap, firstUsedBlock, lastUsedBlock;
267 uint32_t i;
srs5694221e0872009-08-29 15:00:31 -0400268 int numProbs = 0;
srs5694e7b4ff92009-08-18 13:16:10 -0400269
270 // first, locate the first & last used blocks
271 firstUsedBlock = UINT64_MAX;
272 lastUsedBlock = 0;
srs56940283dae2010-04-28 16:44:34 -0400273 for (i = 0; i < numParts; i++) {
srs5694221e0872009-08-29 15:00:31 -0400274 if ((partitions[i].GetFirstLBA() < firstUsedBlock) &&
srs5694e4ac11e2009-08-31 10:13:04 -0400275 (partitions[i].GetFirstLBA() != 0))
srs5694221e0872009-08-29 15:00:31 -0400276 firstUsedBlock = partitions[i].GetFirstLBA();
277 if (partitions[i].GetLastLBA() > lastUsedBlock)
278 lastUsedBlock = partitions[i].GetLastLBA();
srs5694e7b4ff92009-08-18 13:16:10 -0400279 } // for
280
281 // If the disk size is 0 (the default), then it means that various
282 // variables aren't yet set, so the below tests will be useless;
283 // therefore we should skip everything
284 if (diskSize != 0) {
285 if (mainHeader.firstUsableLBA > firstUsedBlock) {
286 overlap = mainHeader.firstUsableLBA - firstUsedBlock;
srs5694fed16d02010-01-27 23:03:40 -0500287 cout << "Warning! Main partition table overlaps the first partition by "
288 << overlap << " blocks!\n";
srs5694221e0872009-08-29 15:00:31 -0400289 if (firstUsedBlock > 2) {
srs5694fed16d02010-01-27 23:03:40 -0500290 cout << "Try reducing the partition table size by " << overlap * 4
291 << " entries.\n(Use the 's' item on the experts' menu.)\n";
srs5694221e0872009-08-29 15:00:31 -0400292 } else {
srs5694fed16d02010-01-27 23:03:40 -0500293 cout << "You will need to delete this partition or resize it in another utility.\n";
srs5694221e0872009-08-29 15:00:31 -0400294 } // if/else
295 numProbs++;
srs5694e7b4ff92009-08-18 13:16:10 -0400296 } // Problem at start of disk
297 if (mainHeader.lastUsableLBA < lastUsedBlock) {
298 overlap = lastUsedBlock - mainHeader.lastUsableLBA;
srs569455d92612010-03-07 22:16:07 -0500299 cout << "\nWarning! Secondary partition table overlaps the last partition by\n"
srs5694fed16d02010-01-27 23:03:40 -0500300 << overlap << " blocks!\n";
srs5694221e0872009-08-29 15:00:31 -0400301 if (lastUsedBlock > (diskSize - 2)) {
srs5694fed16d02010-01-27 23:03:40 -0500302 cout << "You will need to delete this partition or resize it in another utility.\n";
srs5694221e0872009-08-29 15:00:31 -0400303 } else {
srs5694fed16d02010-01-27 23:03:40 -0500304 cout << "Try reducing the partition table size by " << overlap * 4
305 << " entries.\n(Use the 's' item on the experts' menu.)\n";
srs5694221e0872009-08-29 15:00:31 -0400306 } // if/else
307 numProbs++;
srs5694e7b4ff92009-08-18 13:16:10 -0400308 } // Problem at end of disk
309 } // if (diskSize != 0)
srs5694221e0872009-08-29 15:00:31 -0400310 return numProbs;
srs5694e7b4ff92009-08-18 13:16:10 -0400311} // GPTData::CheckGPTSize()
312
srs5694e7b4ff92009-08-18 13:16:10 -0400313// Check the validity of the GPT header. Returns 1 if the main header
314// is valid, 2 if the backup header is valid, 3 if both are valid, and
315// 0 if neither is valid. Note that this function just checks the GPT
316// signature and revision numbers, not CRCs or other data.
317int GPTData::CheckHeaderValidity(void) {
318 int valid = 3;
319
srs5694fed16d02010-01-27 23:03:40 -0500320 cout.setf(ios::uppercase);
321 cout.fill('0');
322
323 // Note: failed GPT signature checks produce no error message because
324 // a message is displayed in the ReversePartitionBytes() function
srs5694e7b4ff92009-08-18 13:16:10 -0400325 if (mainHeader.signature != GPT_SIGNATURE) {
326 valid -= 1;
srs5694e7b4ff92009-08-18 13:16:10 -0400327 } else if ((mainHeader.revision != 0x00010000) && valid) {
328 valid -= 1;
srs5694fed16d02010-01-27 23:03:40 -0500329 cout << "Unsupported GPT version in main header; read 0x";
330 cout.width(8);
331 cout << hex << mainHeader.revision << ", should be\n0x";
332 cout.width(8);
333 cout << UINT32_C(0x00010000) << dec << "\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400334 } // if/else/if
335
336 if (secondHeader.signature != GPT_SIGNATURE) {
337 valid -= 2;
srs5694e7b4ff92009-08-18 13:16:10 -0400338 } else if ((secondHeader.revision != 0x00010000) && valid) {
339 valid -= 2;
srs5694fed16d02010-01-27 23:03:40 -0500340 cout << "Unsupported GPT version in backup header; read 0x";
341 cout.width(8);
342 cout << hex << secondHeader.revision << ", should be\n0x";
343 cout.width(8);
344 cout << UINT32_C(0x00010000) << dec << "\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400345 } // if/else/if
346
srs5694df9d3632011-01-08 18:33:24 -0500347 // Check for an Apple disk signature
348 if (((mainHeader.signature << 32) == APM_SIGNATURE1) ||
349 (mainHeader.signature << 32) == APM_SIGNATURE2) {
srs5694221e0872009-08-29 15:00:31 -0400350 apmFound = 1; // Will display warning message later
srs56943f2fe992009-11-24 18:28:18 -0500351 } // if
srs5694fed16d02010-01-27 23:03:40 -0500352 cout.fill(' ');
srs56942a9f5da2009-08-26 00:48:01 -0400353
srs5694fed16d02010-01-27 23:03:40 -0500354 return valid;
srs5694e7b4ff92009-08-18 13:16:10 -0400355} // GPTData::CheckHeaderValidity()
356
357// Check the header CRC to see if it's OK...
srs5694cb76c672010-02-11 22:22:22 -0500358// Note: Must be called with header in LITTLE-ENDIAN
359// (x86, x86-64, etc.) byte order.
srs5694e7b4ff92009-08-18 13:16:10 -0400360int GPTData::CheckHeaderCRC(struct GPTHeader* header) {
srs5694978041c2009-09-21 20:51:47 -0400361 uint32_t oldCRC, newCRC, hSize;
srs5694e7b4ff92009-08-18 13:16:10 -0400362
srs56942a9f5da2009-08-26 00:48:01 -0400363 // Back up old header CRC and then blank it, since it must be 0 for
srs5694e7b4ff92009-08-18 13:16:10 -0400364 // computation to be valid
365 oldCRC = header->headerCRC;
366 header->headerCRC = UINT32_C(0);
srs5694978041c2009-09-21 20:51:47 -0400367 hSize = header->headerSize;
368
369 // If big-endian system, reverse byte order
370 if (IsLittleEndian() == 0) {
371 ReverseBytes(&oldCRC, 4);
372 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400373
374 // Initialize CRC functions...
375 chksum_crc32gentab();
376
377 // Compute CRC, restore original, and return result of comparison
378 newCRC = chksum_crc32((unsigned char*) header, HEADER_SIZE);
srs5694978041c2009-09-21 20:51:47 -0400379 header->headerCRC = oldCRC;
srs5694e7b4ff92009-08-18 13:16:10 -0400380 return (oldCRC == newCRC);
381} // GPTData::CheckHeaderCRC()
382
srs56946699b012010-02-04 00:55:30 -0500383// Recompute all the CRCs. Must be called before saving if any changes have
384// been made. Must be called on platform-ordered data (this function reverses
385// byte order and then undoes that reversal.)
srs5694e7b4ff92009-08-18 13:16:10 -0400386void GPTData::RecomputeCRCs(void) {
srs56940283dae2010-04-28 16:44:34 -0400387 uint32_t crc, hSize;
srs56942a9f5da2009-08-26 00:48:01 -0400388 int littleEndian = 1;
srs5694e7b4ff92009-08-18 13:16:10 -0400389
390 // Initialize CRC functions...
391 chksum_crc32gentab();
392
srs56946699b012010-02-04 00:55:30 -0500393 // Save some key data from header before reversing byte order....
srs5694978041c2009-09-21 20:51:47 -0400394 hSize = mainHeader.headerSize;
srs56946699b012010-02-04 00:55:30 -0500395
396 if ((littleEndian = IsLittleEndian()) == 0) {
397 ReversePartitionBytes();
398 ReverseHeaderBytes(&mainHeader);
399 ReverseHeaderBytes(&secondHeader);
400 } // if
srs56942a9f5da2009-08-26 00:48:01 -0400401
srs5694e7b4ff92009-08-18 13:16:10 -0400402 // Compute CRC of partition tables & store in main and secondary headers
srs56940283dae2010-04-28 16:44:34 -0400403 crc = chksum_crc32((unsigned char*) partitions, numParts * GPT_SIZE);
srs5694e7b4ff92009-08-18 13:16:10 -0400404 mainHeader.partitionEntriesCRC = crc;
405 secondHeader.partitionEntriesCRC = crc;
srs56942a9f5da2009-08-26 00:48:01 -0400406 if (littleEndian == 0) {
srs5694221e0872009-08-29 15:00:31 -0400407 ReverseBytes(&mainHeader.partitionEntriesCRC, 4);
408 ReverseBytes(&secondHeader.partitionEntriesCRC, 4);
srs56942a9f5da2009-08-26 00:48:01 -0400409 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400410
411 // Zero out GPT tables' own CRCs (required for correct computation)
412 mainHeader.headerCRC = 0;
413 secondHeader.headerCRC = 0;
414
415 // Compute & store CRCs of main & secondary headers...
srs5694978041c2009-09-21 20:51:47 -0400416 crc = chksum_crc32((unsigned char*) &mainHeader, hSize);
srs56942a9f5da2009-08-26 00:48:01 -0400417 if (littleEndian == 0)
srs5694221e0872009-08-29 15:00:31 -0400418 ReverseBytes(&crc, 4);
srs5694e7b4ff92009-08-18 13:16:10 -0400419 mainHeader.headerCRC = crc;
srs5694978041c2009-09-21 20:51:47 -0400420 crc = chksum_crc32((unsigned char*) &secondHeader, hSize);
srs56942a9f5da2009-08-26 00:48:01 -0400421 if (littleEndian == 0)
srs5694221e0872009-08-29 15:00:31 -0400422 ReverseBytes(&crc, 4);
srs5694e7b4ff92009-08-18 13:16:10 -0400423 secondHeader.headerCRC = crc;
srs56946699b012010-02-04 00:55:30 -0500424
425 if ((littleEndian = IsLittleEndian()) == 0) {
426 ReverseHeaderBytes(&mainHeader);
427 ReverseHeaderBytes(&secondHeader);
428 ReversePartitionBytes();
429 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400430} // GPTData::RecomputeCRCs()
431
srs5694e7b4ff92009-08-18 13:16:10 -0400432// Rebuild the main GPT header, using the secondary header as a model.
433// Typically called when the main header has been found to be corrupt.
434void GPTData::RebuildMainHeader(void) {
435 int i;
436
437 mainHeader.signature = GPT_SIGNATURE;
438 mainHeader.revision = secondHeader.revision;
srs5694978041c2009-09-21 20:51:47 -0400439 mainHeader.headerSize = secondHeader.headerSize;
srs5694e7b4ff92009-08-18 13:16:10 -0400440 mainHeader.headerCRC = UINT32_C(0);
441 mainHeader.reserved = secondHeader.reserved;
442 mainHeader.currentLBA = secondHeader.backupLBA;
443 mainHeader.backupLBA = secondHeader.currentLBA;
444 mainHeader.firstUsableLBA = secondHeader.firstUsableLBA;
445 mainHeader.lastUsableLBA = secondHeader.lastUsableLBA;
srs56946699b012010-02-04 00:55:30 -0500446 mainHeader.diskGUID = secondHeader.diskGUID;
srs5694e7b4ff92009-08-18 13:16:10 -0400447 mainHeader.partitionEntriesLBA = UINT64_C(2);
448 mainHeader.numParts = secondHeader.numParts;
449 mainHeader.sizeOfPartitionEntries = secondHeader.sizeOfPartitionEntries;
450 mainHeader.partitionEntriesCRC = secondHeader.partitionEntriesCRC;
451 for (i = 0 ; i < GPT_RESERVED; i++)
452 mainHeader.reserved2[i] = secondHeader.reserved2[i];
srs5694546a9c72010-01-26 16:00:26 -0500453 mainCrcOk = secondCrcOk;
srs56940283dae2010-04-28 16:44:34 -0400454 SetGPTSize(mainHeader.numParts);
srs5694e7b4ff92009-08-18 13:16:10 -0400455} // GPTData::RebuildMainHeader()
456
457// Rebuild the secondary GPT header, using the main header as a model.
458void GPTData::RebuildSecondHeader(void) {
459 int i;
460
461 secondHeader.signature = GPT_SIGNATURE;
462 secondHeader.revision = mainHeader.revision;
srs5694978041c2009-09-21 20:51:47 -0400463 secondHeader.headerSize = mainHeader.headerSize;
srs5694e7b4ff92009-08-18 13:16:10 -0400464 secondHeader.headerCRC = UINT32_C(0);
465 secondHeader.reserved = mainHeader.reserved;
466 secondHeader.currentLBA = mainHeader.backupLBA;
467 secondHeader.backupLBA = mainHeader.currentLBA;
468 secondHeader.firstUsableLBA = mainHeader.firstUsableLBA;
469 secondHeader.lastUsableLBA = mainHeader.lastUsableLBA;
srs56946699b012010-02-04 00:55:30 -0500470 secondHeader.diskGUID = mainHeader.diskGUID;
srs5694e7b4ff92009-08-18 13:16:10 -0400471 secondHeader.partitionEntriesLBA = secondHeader.lastUsableLBA + UINT64_C(1);
472 secondHeader.numParts = mainHeader.numParts;
473 secondHeader.sizeOfPartitionEntries = mainHeader.sizeOfPartitionEntries;
474 secondHeader.partitionEntriesCRC = mainHeader.partitionEntriesCRC;
475 for (i = 0 ; i < GPT_RESERVED; i++)
476 secondHeader.reserved2[i] = mainHeader.reserved2[i];
srs5694546a9c72010-01-26 16:00:26 -0500477 secondCrcOk = mainCrcOk;
srs56940283dae2010-04-28 16:44:34 -0400478 SetGPTSize(secondHeader.numParts);
srs5694e4ac11e2009-08-31 10:13:04 -0400479} // GPTData::RebuildSecondHeader()
480
481// Search for hybrid MBR entries that have no corresponding GPT partition.
482// Returns number of such mismatches found
483int GPTData::FindHybridMismatches(void) {
srs5694e321d442010-01-29 17:44:04 -0500484 int i, found, numFound = 0;
485 uint32_t j;
srs5694e4ac11e2009-08-31 10:13:04 -0400486 uint64_t mbrFirst, mbrLast;
487
488 for (i = 0; i < 4; i++) {
489 if ((protectiveMBR.GetType(i) != 0xEE) && (protectiveMBR.GetType(i) != 0x00)) {
490 j = 0;
491 found = 0;
492 do {
493 mbrFirst = (uint64_t) protectiveMBR.GetFirstSector(i);
494 mbrLast = mbrFirst + (uint64_t) protectiveMBR.GetLength(i) - UINT64_C(1);
495 if ((partitions[j].GetFirstLBA() == mbrFirst) &&
496 (partitions[j].GetLastLBA() == mbrLast))
497 found = 1;
498 j++;
srs56940283dae2010-04-28 16:44:34 -0400499 } while ((!found) && (j < numParts));
srs5694e4ac11e2009-08-31 10:13:04 -0400500 if (!found) {
501 numFound++;
srs5694fed16d02010-01-27 23:03:40 -0500502 cout << "\nWarning! Mismatched GPT and MBR partition! MBR partition "
503 << i + 1 << ", of type 0x";
504 cout.fill('0');
505 cout.setf(ios::uppercase);
506 cout.width(2);
507 cout << hex << (int) protectiveMBR.GetType(i) << ",\n"
508 << "has no corresponding GPT partition! You may continue, but this condition\n"
509 << "might cause data loss in the future!\a\n" << dec;
510 cout.fill(' ');
srs5694e4ac11e2009-08-31 10:13:04 -0400511 } // if
512 } // if
513 } // for
514 return numFound;
515} // GPTData::FindHybridMismatches
516
517// Find overlapping partitions and warn user about them. Returns number of
518// overlapping partitions.
519int GPTData::FindOverlaps(void) {
srs5694e321d442010-01-29 17:44:04 -0500520 int problems = 0;
521 uint32_t i, j;
srs5694e4ac11e2009-08-31 10:13:04 -0400522
srs56940283dae2010-04-28 16:44:34 -0400523 for (i = 1; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -0400524 for (j = 0; j < i; j++) {
srs56940a697312010-01-28 21:10:52 -0500525 if (partitions[i].DoTheyOverlap(partitions[j])) {
srs5694e4ac11e2009-08-31 10:13:04 -0400526 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500527 cout << "\nProblem: partitions " << i + 1 << " and " << j + 1 << " overlap:\n";
528 cout << " Partition " << i + 1 << ": " << partitions[i].GetFirstLBA()
529 << " to " << partitions[i].GetLastLBA() << "\n";
530 cout << " Partition " << j + 1 << ": " << partitions[j].GetFirstLBA()
531 << " to " << partitions[j].GetLastLBA() << "\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400532 } // if
533 } // for j...
534 } // for i...
535 return problems;
536} // GPTData::FindOverlaps()
537
srs569455d92612010-03-07 22:16:07 -0500538// Find partitions that are insane -- they start after they end or are too
539// big for the disk. (The latter should duplicate detection of overlaps
540// with GPT backup data structures, but better to err on the side of
541// redundant tests than to miss something....)
542int GPTData::FindInsanePartitions(void) {
543 uint32_t i;
544 int problems = 0;
545
srs56940283dae2010-04-28 16:44:34 -0400546 for (i = 0; i < numParts; i++) {
srs569455d92612010-03-07 22:16:07 -0500547 if (partitions[i].GetFirstLBA() > partitions[i].GetLastLBA()) {
548 problems++;
srs56940283dae2010-04-28 16:44:34 -0400549 cout << "\nProblem: partition " << i + 1 << " ends before it begins.\n";
srs569455d92612010-03-07 22:16:07 -0500550 } // if
551 if (partitions[i].GetLastLBA() >= diskSize) {
552 problems++;
srs56940873e9d2010-10-07 13:00:45 -0400553 cout << "\nProblem: partition " << i + 1 << " is too big for the disk.\n";
srs569455d92612010-03-07 22:16:07 -0500554 } // if
555 } // for
556 return problems;
557} // GPTData::FindInsanePartitions(void)
558
559
srs5694e4ac11e2009-08-31 10:13:04 -0400560/******************************************************************
561 * *
562 * Begin functions that load data from disk or save data to disk. *
563 * *
564 ******************************************************************/
565
566// Scan for partition data. This function loads the MBR data (regular MBR or
567// protective MBR) and loads BSD disklabel data (which is probably invalid).
568// It also looks for APM data, forces a load of GPT data, and summarizes
569// the results.
srs5694546a9c72010-01-26 16:00:26 -0500570void GPTData::PartitionScan(void) {
srs5694e4ac11e2009-08-31 10:13:04 -0400571 BSDData bsdDisklabel;
srs5694e4ac11e2009-08-31 10:13:04 -0400572
573 // Read the MBR & check for BSD disklabel
srs5694546a9c72010-01-26 16:00:26 -0500574 protectiveMBR.ReadMBRData(&myDisk);
575 bsdDisklabel.ReadBSDData(&myDisk, 0, diskSize - 1);
srs5694e4ac11e2009-08-31 10:13:04 -0400576
577 // Load the GPT data, whether or not it's valid
srs5694546a9c72010-01-26 16:00:26 -0500578 ForceLoadGPTData();
srs5694ba00fed2010-01-12 18:18:36 -0500579
580 if (!beQuiet) {
srs5694fed16d02010-01-27 23:03:40 -0500581 cout << "Partition table scan:\n";
srs5694ba00fed2010-01-12 18:18:36 -0500582 protectiveMBR.ShowState();
583 bsdDisklabel.ShowState();
584 ShowAPMState(); // Show whether there's an Apple Partition Map present
585 ShowGPTState(); // Show GPT status
srs5694fed16d02010-01-27 23:03:40 -0500586 cout << "\n";
srs5694ba00fed2010-01-12 18:18:36 -0500587 } // if
srs5694e4ac11e2009-08-31 10:13:04 -0400588
589 if (apmFound) {
srs5694fed16d02010-01-27 23:03:40 -0500590 cout << "\n*******************************************************************\n"
591 << "This disk appears to contain an Apple-format (APM) partition table!\n";
srs56945d58fe02010-01-03 20:57:08 -0500592 if (!justLooking) {
srs5694fed16d02010-01-27 23:03:40 -0500593 cout << "It will be destroyed if you continue!\n";
srs56945d58fe02010-01-03 20:57:08 -0500594 } // if
srs5694fed16d02010-01-27 23:03:40 -0500595 cout << "*******************************************************************\n\n\a";
srs5694e4ac11e2009-08-31 10:13:04 -0400596 } // if
srs5694e4ac11e2009-08-31 10:13:04 -0400597} // GPTData::PartitionScan()
598
599// Read GPT data from a disk.
srs56940a697312010-01-28 21:10:52 -0500600int GPTData::LoadPartitions(const string & deviceFilename) {
srs569408bb0da2010-02-19 17:19:55 -0500601 BSDData bsdDisklabel;
srs5694e321d442010-01-29 17:44:04 -0500602 int err, allOK = 1;
srs5694fed16d02010-01-27 23:03:40 -0500603 MBRValidity mbrState;
srs5694e4ac11e2009-08-31 10:13:04 -0400604
srs5694546a9c72010-01-26 16:00:26 -0500605 if (myDisk.OpenForRead(deviceFilename)) {
srs569455d92612010-03-07 22:16:07 -0500606 err = myDisk.OpenForWrite(deviceFilename);
607 if ((err == 0) && (!justLooking)) {
608 cout << "\aNOTE: Write test failed with error number " << errno
609 << ". It will be impossible to save\nchanges to this disk's partition table!\n";
610#if defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
611 cout << "You may be able to enable writes by exiting this program, typing\n"
612 << "'sysctl kern.geom.debugflags=16' at a shell prompt, and re-running this\n"
613 << "program.\n";
614#endif
615 cout << "\n";
616 } // if
617 myDisk.Close(); // Close and re-open read-only in case of bugs
618 } else allOK = 0; // if
619
620 if (allOK && myDisk.OpenForRead(deviceFilename)) {
srs5694e4ac11e2009-08-31 10:13:04 -0400621 // store disk information....
srs5694546a9c72010-01-26 16:00:26 -0500622 diskSize = myDisk.DiskSize(&err);
623 blockSize = (uint32_t) myDisk.GetBlockSize();
srs5694fed16d02010-01-27 23:03:40 -0500624 device = deviceFilename;
srs5694546a9c72010-01-26 16:00:26 -0500625 PartitionScan(); // Check for partition types, load GPT, & print summary
srs5694e4ac11e2009-08-31 10:13:04 -0400626
srs5694ba00fed2010-01-12 18:18:36 -0500627 whichWasUsed = UseWhichPartitions();
628 switch (whichWasUsed) {
srs5694e4ac11e2009-08-31 10:13:04 -0400629 case use_mbr:
630 XFormPartitions();
631 break;
632 case use_bsd:
srs5694546a9c72010-01-26 16:00:26 -0500633 bsdDisklabel.ReadBSDData(&myDisk, 0, diskSize - 1);
srs5694e4ac11e2009-08-31 10:13:04 -0400634// bsdDisklabel.DisplayBSDData();
635 ClearGPTData();
636 protectiveMBR.MakeProtectiveMBR(1); // clear boot area (option 1)
srs569408bb0da2010-02-19 17:19:55 -0500637 XFormDisklabel(&bsdDisklabel);
srs5694e4ac11e2009-08-31 10:13:04 -0400638 break;
639 case use_gpt:
srs5694fed16d02010-01-27 23:03:40 -0500640 mbrState = protectiveMBR.GetValidity();
641 if ((mbrState == invalid) || (mbrState == mbr))
642 protectiveMBR.MakeProtectiveMBR();
srs5694e4ac11e2009-08-31 10:13:04 -0400643 break;
644 case use_new:
645 ClearGPTData();
646 protectiveMBR.MakeProtectiveMBR();
647 break;
srs56943c0af382010-01-15 19:19:18 -0500648 case use_abort:
649 allOK = 0;
srs56949ddc14b2010-08-22 22:44:42 -0400650 cerr << "Invalid partition data!\n";
srs56943c0af382010-01-15 19:19:18 -0500651 break;
srs5694e4ac11e2009-08-31 10:13:04 -0400652 } // switch
653
srs569455d92612010-03-07 22:16:07 -0500654 if (allOK)
srs56943c0af382010-01-15 19:19:18 -0500655 CheckGPTSize();
srs569455d92612010-03-07 22:16:07 -0500656 myDisk.Close();
srs5694a8582cf2010-03-19 14:21:59 -0400657 ComputeAlignment();
srs5694e4ac11e2009-08-31 10:13:04 -0400658 } else {
659 allOK = 0;
srs5694e4ac11e2009-08-31 10:13:04 -0400660 } // if/else
661 return (allOK);
662} // GPTData::LoadPartitions()
663
664// Loads the GPT, as much as possible. Returns 1 if this seems to have
665// succeeded, 0 if there are obvious problems....
srs5694546a9c72010-01-26 16:00:26 -0500666int GPTData::ForceLoadGPTData(void) {
srs5694cb76c672010-02-11 22:22:22 -0500667 int allOK, validHeaders, loadedTable = 1;
srs5694e4ac11e2009-08-31 10:13:04 -0400668
srs5694cb76c672010-02-11 22:22:22 -0500669 allOK = LoadHeader(&mainHeader, myDisk, 1, &mainCrcOk);
srs5694e4ac11e2009-08-31 10:13:04 -0400670
srs5694cb76c672010-02-11 22:22:22 -0500671 if (mainCrcOk && (mainHeader.backupLBA < diskSize)) {
672 allOK = LoadHeader(&secondHeader, myDisk, mainHeader.backupLBA, &secondCrcOk) && allOK;
673 } else {
srs569408bb0da2010-02-19 17:19:55 -0500674 allOK = LoadHeader(&secondHeader, myDisk, diskSize - UINT64_C(1), &secondCrcOk) && allOK;
675 if (mainCrcOk && (mainHeader.backupLBA >= diskSize))
srs5694fed16d02010-01-27 23:03:40 -0500676 cout << "Warning! Disk size is smaller than the main header indicates! Loading\n"
677 << "secondary header from the last sector of the disk! You should use 'v' to\n"
678 << "verify disk integrity, and perhaps options on the experts' menu to repair\n"
679 << "the disk.\n";
srs5694cb76c672010-02-11 22:22:22 -0500680 } // if/else
681 if (!allOK)
srs5694e4ac11e2009-08-31 10:13:04 -0400682 state = gpt_invalid;
srs5694e4ac11e2009-08-31 10:13:04 -0400683
684 // Return valid headers code: 0 = both headers bad; 1 = main header
685 // good, backup bad; 2 = backup header good, main header bad;
686 // 3 = both headers good. Note these codes refer to valid GPT
687 // signatures and version numbers; more subtle problems will elude
688 // this check!
689 validHeaders = CheckHeaderValidity();
690
691 // Read partitions (from primary array)
692 if (validHeaders > 0) { // if at least one header is OK....
693 // GPT appears to be valid....
694 state = gpt_valid;
695
696 // We're calling the GPT valid, but there's a possibility that one
697 // of the two headers is corrupt. If so, use the one that seems to
698 // be in better shape to regenerate the bad one
srs5694546a9c72010-01-26 16:00:26 -0500699 if (validHeaders == 1) { // valid main header, invalid backup header
srs5694fed16d02010-01-27 23:03:40 -0500700 cerr << "\aCaution: invalid backup GPT header, but valid main header; regenerating\n"
701 << "backup header from main header.\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400702 RebuildSecondHeader();
srs5694546a9c72010-01-26 16:00:26 -0500703 state = gpt_corrupt;
srs5694e4ac11e2009-08-31 10:13:04 -0400704 secondCrcOk = mainCrcOk; // Since regenerated, use CRC validity of main
srs5694546a9c72010-01-26 16:00:26 -0500705 } else if (validHeaders == 2) { // valid backup header, invalid main header
srs5694fed16d02010-01-27 23:03:40 -0500706 cerr << "\aCaution: invalid main GPT header, but valid backup; regenerating main header\n"
707 << "from backup!\n\n";
srs5694546a9c72010-01-26 16:00:26 -0500708 RebuildMainHeader();
709 state = gpt_corrupt;
710 mainCrcOk = secondCrcOk; // Since copied, use CRC validity of backup
srs5694e4ac11e2009-08-31 10:13:04 -0400711 } // if/else/if
712
srs5694546a9c72010-01-26 16:00:26 -0500713 // Figure out which partition table to load....
714 // Load the main partition table, since either its header's CRC is OK or the
715 // backup header's CRC is not OK....
716 if (mainCrcOk || !secondCrcOk) {
717 if (LoadMainTable() == 0)
718 allOK = 0;
719 } else { // bad main header CRC and backup header CRC is OK
720 state = gpt_corrupt;
721 if (LoadSecondTableAsMain()) {
srs5694cb76c672010-02-11 22:22:22 -0500722 loadedTable = 2;
srs5694fed16d02010-01-27 23:03:40 -0500723 cerr << "\aWarning: Invalid CRC on main header data; loaded backup partition table.\n";
srs5694546a9c72010-01-26 16:00:26 -0500724 } else { // backup table bad, bad main header CRC, but try main table in desperation....
725 if (LoadMainTable() == 0) {
726 allOK = 0;
srs5694cb76c672010-02-11 22:22:22 -0500727 loadedTable = 0;
srs5694fed16d02010-01-27 23:03:40 -0500728 cerr << "\a\aWarning! Unable to load either main or backup partition table!\n";
srs5694546a9c72010-01-26 16:00:26 -0500729 } // if
730 } // if/else (LoadSecondTableAsMain())
731 } // if/else (load partition table)
srs5694e4ac11e2009-08-31 10:13:04 -0400732
srs5694cb76c672010-02-11 22:22:22 -0500733 if (loadedTable == 1)
734 secondPartsCrcOk = CheckTable(&secondHeader);
735 else if (loadedTable == 2)
736 mainPartsCrcOk = CheckTable(&mainHeader);
737 else
738 mainPartsCrcOk = secondPartsCrcOk = 0;
srs5694e4ac11e2009-08-31 10:13:04 -0400739
srs5694546a9c72010-01-26 16:00:26 -0500740 // Problem with main partition table; if backup is OK, use it instead....
741 if (secondPartsCrcOk && secondCrcOk && !mainPartsCrcOk) {
742 state = gpt_corrupt;
743 allOK = allOK && LoadSecondTableAsMain();
srs5694cb76c672010-02-11 22:22:22 -0500744 mainPartsCrcOk = 0; // LoadSecondTableAsMain() resets this, so re-flag as bad
srs5694fed16d02010-01-27 23:03:40 -0500745 cerr << "\aWarning! Main partition table CRC mismatch! Loaded backup "
746 << "partition table\ninstead of main partition table!\n\n";
srs5694cb76c672010-02-11 22:22:22 -0500747 } // if */
srs5694546a9c72010-01-26 16:00:26 -0500748
srs5694e4ac11e2009-08-31 10:13:04 -0400749 // Check for valid CRCs and warn if there are problems
750 if ((mainCrcOk == 0) || (secondCrcOk == 0) || (mainPartsCrcOk == 0) ||
751 (secondPartsCrcOk == 0)) {
srs5694fed16d02010-01-27 23:03:40 -0500752 cerr << "Warning! One or more CRCs don't match. You should repair the disk!\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400753 state = gpt_corrupt;
srs5694ba00fed2010-01-12 18:18:36 -0500754 } // if
srs5694e4ac11e2009-08-31 10:13:04 -0400755 } else {
756 state = gpt_invalid;
757 } // if/else
758 return allOK;
759} // GPTData::ForceLoadGPTData()
760
srs5694247657a2009-11-26 18:36:12 -0500761// Loads the partition table pointed to by the main GPT header. The
srs5694e4ac11e2009-08-31 10:13:04 -0400762// main GPT header in memory MUST be valid for this call to do anything
763// sensible!
srs5694546a9c72010-01-26 16:00:26 -0500764// Returns 1 on success, 0 on failure. CRC errors do NOT count as failure.
srs5694e4ac11e2009-08-31 10:13:04 -0400765int GPTData::LoadMainTable(void) {
srs5694cb76c672010-02-11 22:22:22 -0500766 return LoadPartitionTable(mainHeader, myDisk);
srs5694e4ac11e2009-08-31 10:13:04 -0400767} // GPTData::LoadMainTable()
srs5694e7b4ff92009-08-18 13:16:10 -0400768
769// Load the second (backup) partition table as the primary partition
srs5694546a9c72010-01-26 16:00:26 -0500770// table. Used in repair functions, and when starting up if the main
771// partition table is damaged.
772// Returns 1 on success, 0 on failure. CRC errors do NOT count as failure.
773int GPTData::LoadSecondTableAsMain(void) {
srs5694cb76c672010-02-11 22:22:22 -0500774 return LoadPartitionTable(secondHeader, myDisk);
775} // GPTData::LoadSecondTableAsMain()
srs5694e7b4ff92009-08-18 13:16:10 -0400776
srs5694cb76c672010-02-11 22:22:22 -0500777// Load a single GPT header (main or backup) from the specified disk device and
778// sector. Applies byte-order corrections on big-endian platforms. Sets crcOk
779// value appropriately.
780// Returns 1 on success, 0 on failure. Note that CRC errors do NOT qualify as
781// failure.
782int GPTData::LoadHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector, int *crcOk) {
783 int allOK = 1;
srs56941c6f8b02010-02-21 11:09:20 -0500784 GPTHeader tempHeader;
srs5694cb76c672010-02-11 22:22:22 -0500785
786 disk.Seek(sector);
srs56941c6f8b02010-02-21 11:09:20 -0500787 if (disk.Read(&tempHeader, 512) != 512) {
srs5694cb76c672010-02-11 22:22:22 -0500788 cerr << "Warning! Read error " << errno << "; strange behavior now likely!\n";
789 allOK = 0;
790 } // if
srs56941c6f8b02010-02-21 11:09:20 -0500791 *crcOk = CheckHeaderCRC(&tempHeader);
srs5694cb76c672010-02-11 22:22:22 -0500792
srs56941c6f8b02010-02-21 11:09:20 -0500793 // Reverse byte order, if necessary
srs5694cb76c672010-02-11 22:22:22 -0500794 if (IsLittleEndian() == 0) {
srs569455d92612010-03-07 22:16:07 -0500795 ReverseHeaderBytes(&tempHeader);
srs5694cb76c672010-02-11 22:22:22 -0500796 } // if
srs56941c6f8b02010-02-21 11:09:20 -0500797
srs56940283dae2010-04-28 16:44:34 -0400798 if (allOK && (numParts != tempHeader.numParts) && *crcOk) {
srs56941c6f8b02010-02-21 11:09:20 -0500799 allOK = SetGPTSize(tempHeader.numParts);
srs569455d92612010-03-07 22:16:07 -0500800 }
srs56941c6f8b02010-02-21 11:09:20 -0500801
802 *header = tempHeader;
srs5694cb76c672010-02-11 22:22:22 -0500803 return allOK;
804} // GPTData::LoadHeader
805
806// Load a partition table (either main or secondary) from the specified disk,
807// using header as a reference for what to load. If sector != 0 (the default
808// is 0), loads from the specified sector; otherwise loads from the sector
809// indicated in header.
810// Returns 1 on success, 0 on failure. CRC errors do NOT count as failure.
811int GPTData::LoadPartitionTable(const struct GPTHeader & header, DiskIO & disk, uint64_t sector) {
812 uint32_t sizeOfParts, newCRC;
813 int retval;
814
815 if (disk.OpenForRead()) {
816 if (sector == 0) {
817 retval = disk.Seek(header.partitionEntriesLBA);
818 } else {
819 retval = disk.Seek(sector);
820 } // if/else
srs569455d92612010-03-07 22:16:07 -0500821 if (retval == 1)
822 retval = SetGPTSize(header.numParts);
srs5694546a9c72010-01-26 16:00:26 -0500823 if (retval == 1) {
srs5694cb76c672010-02-11 22:22:22 -0500824 sizeOfParts = header.numParts * header.sizeOfPartitionEntries;
825 if (disk.Read(partitions, sizeOfParts) != (int) sizeOfParts) {
srs5694fed16d02010-01-27 23:03:40 -0500826 cerr << "Warning! Read error " << errno << "! Misbehavior now likely!\n";
srs5694546a9c72010-01-26 16:00:26 -0500827 retval = 0;
srs56945d58fe02010-01-03 20:57:08 -0500828 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400829 newCRC = chksum_crc32((unsigned char*) partitions, sizeOfParts);
srs5694cb76c672010-02-11 22:22:22 -0500830 mainPartsCrcOk = secondPartsCrcOk = (newCRC == header.partitionEntriesCRC);
srs56942a9f5da2009-08-26 00:48:01 -0400831 if (IsLittleEndian() == 0)
832 ReversePartitionBytes();
srs5694cb76c672010-02-11 22:22:22 -0500833 if (!mainPartsCrcOk) {
834 cout << "Caution! After loading partitions, the CRC doesn't check out!\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400835 } // if
836 } else {
srs5694cb76c672010-02-11 22:22:22 -0500837 cerr << "Error! Couldn't seek to partition table!\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400838 } // if/else
839 } else {
srs5694fed16d02010-01-27 23:03:40 -0500840 cerr << "Error! Couldn't open device " << device
srs5694cb76c672010-02-11 22:22:22 -0500841 << " when reading partition table!\n";
srs5694546a9c72010-01-26 16:00:26 -0500842 retval = 0;
srs5694e7b4ff92009-08-18 13:16:10 -0400843 } // if/else
srs5694546a9c72010-01-26 16:00:26 -0500844 return retval;
srs5694cb76c672010-02-11 22:22:22 -0500845} // GPTData::LoadPartitionsTable()
846
847// Check the partition table pointed to by header, but don't keep it
848// around.
849// Returns 1 if the CRC is OK, 0 if not or if there was a read error.
850int GPTData::CheckTable(struct GPTHeader *header) {
851 uint32_t sizeOfParts, newCRC;
852 uint8_t *storage;
853 int newCrcOk = 0;
854
srs56940283dae2010-04-28 16:44:34 -0400855 // Load partition table into temporary storage to check
srs5694cb76c672010-02-11 22:22:22 -0500856 // its CRC and store the results, then discard this temporary
857 // storage, since we don't use it in any but recovery operations
858 if (myDisk.Seek(header->partitionEntriesLBA)) {
srs56940283dae2010-04-28 16:44:34 -0400859 sizeOfParts = header->numParts * header->sizeOfPartitionEntries;
srs5694cb76c672010-02-11 22:22:22 -0500860 storage = new uint8_t[sizeOfParts];
861 if (myDisk.Read(storage, sizeOfParts) != (int) sizeOfParts) {
srs56940283dae2010-04-28 16:44:34 -0400862 cerr << "Warning! Error " << errno << " reading partition table for CRC check!\n";
srs5694cb76c672010-02-11 22:22:22 -0500863 } else {
864 newCRC = chksum_crc32((unsigned char*) storage, sizeOfParts);
865 newCrcOk = (newCRC == header->partitionEntriesCRC);
866 } // if/else
867 delete[] storage;
868 } // if
869 return newCrcOk;
870} // GPTData::CheckTable()
srs5694e7b4ff92009-08-18 13:16:10 -0400871
srs5694e7b4ff92009-08-18 13:16:10 -0400872// Writes GPT (and protective MBR) to disk. Returns 1 on successful
873// write, 0 if there was a problem.
srs5694f9312b02010-07-06 15:39:51 -0400874int GPTData::SaveGPTData(int quiet, string filename) {
srs56946699b012010-02-04 00:55:30 -0500875 int allOK = 1, littleEndian;
srs5694e321d442010-01-29 17:44:04 -0500876 char answer;
srs5694e7b4ff92009-08-18 13:16:10 -0400877
srs56946699b012010-02-04 00:55:30 -0500878 littleEndian = IsLittleEndian();
879
srs56940873e9d2010-10-07 13:00:45 -0400880 if (filename == "")
881 filename = device;
srs5694f9312b02010-07-06 15:39:51 -0400882 if (filename == "") {
srs5694fed16d02010-01-27 23:03:40 -0500883 cerr << "Device not defined.\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400884 } // if
885
886 // First do some final sanity checks....
srs56945d58fe02010-01-03 20:57:08 -0500887
888 // This test should only fail on read-only disks....
889 if (justLooking) {
srs5694fed16d02010-01-27 23:03:40 -0500890 cout << "The justLooking flag is set. This probably means you can't write to the disk.\n";
srs56945d58fe02010-01-03 20:57:08 -0500891 allOK = 0;
892 } // if
893
srs5694e7b4ff92009-08-18 13:16:10 -0400894 // Is there enough space to hold the GPT headers and partition tables,
895 // given the partition sizes?
srs5694221e0872009-08-29 15:00:31 -0400896 if (CheckGPTSize() > 0) {
srs5694e7b4ff92009-08-18 13:16:10 -0400897 allOK = 0;
898 } // if
899
900 // Check that disk is really big enough to handle this...
901 if (mainHeader.backupLBA > diskSize) {
srs5694fed16d02010-01-27 23:03:40 -0500902 cerr << "Error! Disk is too small! The 'e' option on the experts' menu might fix the\n"
903 << "problem (or it might not). Aborting!\n(Disk size is "
904 << diskSize << " sectors, needs to be " << mainHeader.backupLBA << " sectors.)\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400905 allOK = 0;
906 } // if
srs5694247657a2009-11-26 18:36:12 -0500907 // Check that second header is properly placed. Warn and ask if this should
908 // be corrected if the test fails....
srs5694ba00fed2010-01-12 18:18:36 -0500909 if ((mainHeader.backupLBA < (diskSize - UINT64_C(1))) && (quiet == 0)) {
srs5694fed16d02010-01-27 23:03:40 -0500910 cout << "Warning! Secondary header is placed too early on the disk! Do you want to\n"
911 << "correct this problem? ";
srs5694247657a2009-11-26 18:36:12 -0500912 if (GetYN() == 'Y') {
913 MoveSecondHeaderToEnd();
srs5694fed16d02010-01-27 23:03:40 -0500914 cout << "Have moved second header and partition table to correct location.\n";
srs5694247657a2009-11-26 18:36:12 -0500915 } else {
srs5694fed16d02010-01-27 23:03:40 -0500916 cout << "Have not corrected the problem. Strange problems may occur in the future!\n";
srs5694247657a2009-11-26 18:36:12 -0500917 } // if correction requested
918 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400919
srs569455d92612010-03-07 22:16:07 -0500920 // Check for overlapping or insane partitions....
921 if ((FindOverlaps() > 0) || (FindInsanePartitions() > 0)) {
srs5694e4ac11e2009-08-31 10:13:04 -0400922 allOK = 0;
srs5694fed16d02010-01-27 23:03:40 -0500923 cerr << "Aborting write operation!\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400924 } // if
925
926 // Check for mismatched MBR and GPT data, but let it pass if found
927 // (function displays warning message)
928 FindHybridMismatches();
srs5694e7b4ff92009-08-18 13:16:10 -0400929
930 RecomputeCRCs();
931
srs5694ba00fed2010-01-12 18:18:36 -0500932 if ((allOK) && (!quiet)) {
srs5694fed16d02010-01-27 23:03:40 -0500933 cout << "\nFinal checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING\n"
934 << "PARTITIONS!!\n\nDo you want to proceed, possibly destroying your data? ";
srs56945d58fe02010-01-03 20:57:08 -0500935 answer = GetYN();
936 if (answer == 'Y') {
srs5694fed16d02010-01-27 23:03:40 -0500937 cout << "OK; writing new GUID partition table (GPT).\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400938 } else {
939 allOK = 0;
940 } // if/else
941 } // if
942
943 // Do it!
944 if (allOK) {
srs5694f9312b02010-07-06 15:39:51 -0400945 if (myDisk.OpenForWrite(filename)) {
srs56948a4ddfc2010-03-21 19:05:49 -0400946 // As per UEFI specs, write the secondary table and GPT first....
srs5694cb76c672010-02-11 22:22:22 -0500947 allOK = SavePartitionTable(myDisk, secondHeader.partitionEntriesLBA);
948 if (!allOK)
949 cerr << "Unable to save backup partition table! Perhaps the 'e' option on the experts'\n"
950 << "menu will resolve this problem.\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400951
952 // Now write the secondary GPT header...
srs56948a4ddfc2010-03-21 19:05:49 -0400953 allOK = allOK && SaveHeader(&secondHeader, myDisk, mainHeader.backupLBA);
954
955 // Now write the main partition tables...
956 allOK = allOK && SavePartitionTable(myDisk, mainHeader.partitionEntriesLBA);
957
958 // Now write the main GPT header...
959 allOK = allOK && SaveHeader(&mainHeader, myDisk, 1);
960
961 // To top it off, write the protective MBR...
962 allOK = allOK && protectiveMBR.WriteMBRData(&myDisk);
srs5694e7b4ff92009-08-18 13:16:10 -0400963
964 // re-read the partition table
965 if (allOK) {
srs5694546a9c72010-01-26 16:00:26 -0500966 myDisk.DiskSync();
srs5694e7b4ff92009-08-18 13:16:10 -0400967 } // if
968
969 if (allOK) { // writes completed OK
srs5694fed16d02010-01-27 23:03:40 -0500970 cout << "The operation has completed successfully.\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400971 } else {
srs5694fed16d02010-01-27 23:03:40 -0500972 cerr << "Warning! An error was reported when writing the partition table! This error\n"
srs56948a4ddfc2010-03-21 19:05:49 -0400973 << "MIGHT be harmless, but you may have trashed the disk!\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400974 } // if/else
srs56948a4ddfc2010-03-21 19:05:49 -0400975
srs5694546a9c72010-01-26 16:00:26 -0500976 myDisk.Close();
srs5694e7b4ff92009-08-18 13:16:10 -0400977 } else {
srs5694f9312b02010-07-06 15:39:51 -0400978 cerr << "Unable to open device " << filename << " for writing! Errno is "
srs5694fed16d02010-01-27 23:03:40 -0500979 << errno << "! Aborting write!\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400980 allOK = 0;
srs5694e7b4ff92009-08-18 13:16:10 -0400981 } // if/else
982 } else {
srs5694fed16d02010-01-27 23:03:40 -0500983 cout << "Aborting write of new partition table.\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400984 } // if
985
986 return (allOK);
987} // GPTData::SaveGPTData()
988
989// Save GPT data to a backup file. This function does much less error
990// checking than SaveGPTData(). It can therefore preserve many types of
991// corruption for later analysis; however, it preserves only the MBR,
992// the main GPT header, the backup GPT header, and the main partition
993// table; it discards the backup partition table, since it should be
994// identical to the main partition table on healthy disks.
srs56940a697312010-01-28 21:10:52 -0500995int GPTData::SaveGPTBackup(const string & filename) {
996 int allOK = 1;
srs5694546a9c72010-01-26 16:00:26 -0500997 DiskIO backupFile;
srs5694e7b4ff92009-08-18 13:16:10 -0400998
srs5694546a9c72010-01-26 16:00:26 -0500999 if (backupFile.OpenForWrite(filename)) {
srs56946699b012010-02-04 00:55:30 -05001000 // Recomputing the CRCs is likely to alter them, which could be bad
1001 // if the intent is to save a potentially bad GPT for later analysis;
1002 // but if we don't do this, we get bogus errors when we load the
1003 // backup. I'm favoring misses over false alarms....
1004 RecomputeCRCs();
1005
srs5694546a9c72010-01-26 16:00:26 -05001006 protectiveMBR.WriteMBRData(&backupFile);
srs5694e7b4ff92009-08-18 13:16:10 -04001007
srs5694cb76c672010-02-11 22:22:22 -05001008 if (allOK) {
srs5694546a9c72010-01-26 16:00:26 -05001009 // MBR write closed disk, so re-open and seek to end....
1010 backupFile.OpenForWrite();
srs5694cb76c672010-02-11 22:22:22 -05001011 allOK = SaveHeader(&mainHeader, backupFile, 1);
1012 } // if (allOK)
srs5694e7b4ff92009-08-18 13:16:10 -04001013
srs5694e7b4ff92009-08-18 13:16:10 -04001014 if (allOK)
srs5694cb76c672010-02-11 22:22:22 -05001015 allOK = SaveHeader(&secondHeader, backupFile, 2);
srs5694e7b4ff92009-08-18 13:16:10 -04001016
srs5694cb76c672010-02-11 22:22:22 -05001017 if (allOK)
1018 allOK = SavePartitionTable(backupFile, 3);
srs5694e7b4ff92009-08-18 13:16:10 -04001019
1020 if (allOK) { // writes completed OK
srs5694fed16d02010-01-27 23:03:40 -05001021 cout << "The operation has completed successfully.\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001022 } else {
srs5694fed16d02010-01-27 23:03:40 -05001023 cerr << "Warning! An error was reported when writing the backup file.\n"
1024 << "It may not be usable!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001025 } // if/else
srs5694546a9c72010-01-26 16:00:26 -05001026 backupFile.Close();
srs5694e7b4ff92009-08-18 13:16:10 -04001027 } else {
srs5694fed16d02010-01-27 23:03:40 -05001028 cerr << "Unable to open file " << filename << " for writing! Aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001029 allOK = 0;
1030 } // if/else
1031 return allOK;
1032} // GPTData::SaveGPTBackup()
1033
srs5694cb76c672010-02-11 22:22:22 -05001034// Write a GPT header (main or backup) to the specified sector. Used by both
1035// the SaveGPTData() and SaveGPTBackup() functions.
1036// Should be passed an architecture-appropriate header (DO NOT call
1037// ReverseHeaderBytes() on the header before calling this function)
1038// Returns 1 on success, 0 on failure
1039int GPTData::SaveHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector) {
1040 int littleEndian, allOK = 1;
1041
1042 littleEndian = IsLittleEndian();
1043 if (!littleEndian)
1044 ReverseHeaderBytes(header);
1045 if (disk.Seek(sector)) {
1046 if (disk.Write(header, 512) == -1)
1047 allOK = 0;
1048 } else allOK = 0; // if (disk.Seek()...)
1049 if (!littleEndian)
1050 ReverseHeaderBytes(header);
1051 return allOK;
1052} // GPTData::SaveHeader()
1053
1054// Save the partitions to the specified sector. Used by both the SaveGPTData()
1055// and SaveGPTBackup() functions.
1056// Should be passed an architecture-appropriate header (DO NOT call
1057// ReverseHeaderBytes() on the header before calling this function)
1058// Returns 1 on success, 0 on failure
1059int GPTData::SavePartitionTable(DiskIO & disk, uint64_t sector) {
1060 int littleEndian, allOK = 1;
1061
1062 littleEndian = IsLittleEndian();
1063 if (disk.Seek(sector)) {
1064 if (!littleEndian)
1065 ReversePartitionBytes();
srs56940283dae2010-04-28 16:44:34 -04001066 if (disk.Write(partitions, mainHeader.sizeOfPartitionEntries * numParts) == -1)
srs5694cb76c672010-02-11 22:22:22 -05001067 allOK = 0;
1068 if (!littleEndian)
1069 ReversePartitionBytes();
1070 } else allOK = 0; // if (myDisk.Seek()...)
1071 return allOK;
1072} // GPTData::SavePartitionTable()
1073
srs5694e7b4ff92009-08-18 13:16:10 -04001074// Load GPT data from a backup file created by SaveGPTBackup(). This function
1075// does minimal error checking. It returns 1 if it completed successfully,
1076// 0 if there was a problem. In the latter case, it creates a new empty
1077// set of partitions.
srs56940a697312010-01-28 21:10:52 -05001078int GPTData::LoadGPTBackup(const string & filename) {
srs5694cb76c672010-02-11 22:22:22 -05001079 int allOK = 1, val, err;
srs56940283dae2010-04-28 16:44:34 -04001080 uint32_t sizeOfEntries;
srs5694cb76c672010-02-11 22:22:22 -05001081 int littleEndian = 1, shortBackup = 0;
srs5694546a9c72010-01-26 16:00:26 -05001082 DiskIO backupFile;
srs5694e7b4ff92009-08-18 13:16:10 -04001083
srs5694546a9c72010-01-26 16:00:26 -05001084 if (backupFile.OpenForRead(filename)) {
srs56942a9f5da2009-08-26 00:48:01 -04001085 if (IsLittleEndian() == 0)
1086 littleEndian = 0;
1087
srs5694e7b4ff92009-08-18 13:16:10 -04001088 // Let the MBRData class load the saved MBR...
srs5694546a9c72010-01-26 16:00:26 -05001089 protectiveMBR.ReadMBRData(&backupFile, 0); // 0 = don't check block size
srs5694e7b4ff92009-08-18 13:16:10 -04001090
srs5694cb76c672010-02-11 22:22:22 -05001091 LoadHeader(&mainHeader, backupFile, 1, &mainCrcOk);
srs5694e7b4ff92009-08-18 13:16:10 -04001092
srs5694cb76c672010-02-11 22:22:22 -05001093 // Check backup file size and rebuild second header if file is right
1094 // size to be direct dd copy of MBR, main header, and main partition
1095 // table; if other size, treat it like a GPT fdisk-generated backup
1096 // file
1097 shortBackup = ((backupFile.DiskSize(&err) * backupFile.GetBlockSize()) ==
1098 (mainHeader.numParts * mainHeader.sizeOfPartitionEntries) + 1024);
1099 if (shortBackup) {
1100 RebuildSecondHeader();
1101 secondCrcOk = mainCrcOk;
1102 } else {
1103 LoadHeader(&secondHeader, backupFile, 2, &secondCrcOk);
1104 } // if/else
srs56942a9f5da2009-08-26 00:48:01 -04001105
srs5694e7b4ff92009-08-18 13:16:10 -04001106 // Return valid headers code: 0 = both headers bad; 1 = main header
1107 // good, backup bad; 2 = backup header good, main header bad;
1108 // 3 = both headers good. Note these codes refer to valid GPT
1109 // signatures and version numbers; more subtle problems will elude
1110 // this check!
1111 if ((val = CheckHeaderValidity()) > 0) {
1112 if (val == 2) { // only backup header seems to be good
srs56940283dae2010-04-28 16:44:34 -04001113 SetGPTSize(secondHeader.numParts);
srs5694e4ac11e2009-08-31 10:13:04 -04001114 sizeOfEntries = secondHeader.sizeOfPartitionEntries;
srs5694e7b4ff92009-08-18 13:16:10 -04001115 } else { // main header is OK
srs56940283dae2010-04-28 16:44:34 -04001116 SetGPTSize(mainHeader.numParts);
srs5694e7b4ff92009-08-18 13:16:10 -04001117 sizeOfEntries = mainHeader.sizeOfPartitionEntries;
1118 } // if/else
1119
srs5694e7b4ff92009-08-18 13:16:10 -04001120 if (secondHeader.currentLBA != diskSize - UINT64_C(1)) {
srs5694fed16d02010-01-27 23:03:40 -05001121 cout << "Warning! Current disk size doesn't match that of the backup!\n"
1122 << "Adjusting sizes to match, but subsequent problems are possible!\n";
srs5694247657a2009-11-26 18:36:12 -05001123 MoveSecondHeaderToEnd();
srs5694e7b4ff92009-08-18 13:16:10 -04001124 } // if
1125
srs5694cb76c672010-02-11 22:22:22 -05001126 if (!LoadPartitionTable(mainHeader, backupFile, (uint64_t) (3 - shortBackup)))
1127 cerr << "Warning! Read error " << errno
1128 << " loading partition table; strange behavior now likely!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001129 } else {
1130 allOK = 0;
1131 } // if/else
srs5694a8582cf2010-03-19 14:21:59 -04001132 // Something went badly wrong, so blank out partitions
1133 if (allOK == 0) {
1134 cerr << "Improper backup file! Clearing all partition data!\n";
1135 ClearGPTData();
1136 protectiveMBR.MakeProtectiveMBR();
1137 } // if
srs5694e7b4ff92009-08-18 13:16:10 -04001138 } else {
1139 allOK = 0;
srs5694fed16d02010-01-27 23:03:40 -05001140 cerr << "Unable to open file " << filename << " for reading! Aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001141 } // if/else
1142
srs5694e7b4ff92009-08-18 13:16:10 -04001143 return allOK;
1144} // GPTData::LoadGPTBackup()
1145
srs569408bb0da2010-02-19 17:19:55 -05001146int GPTData::SaveMBR(void) {
srs569455d92612010-03-07 22:16:07 -05001147 return protectiveMBR.WriteMBRData(&myDisk);
srs569408bb0da2010-02-19 17:19:55 -05001148} // GPTData::SaveMBR()
1149
1150// This function destroys the on-disk GPT structures, but NOT the on-disk
1151// MBR.
1152// Returns 1 if the operation succeeds, 0 if not.
1153int GPTData::DestroyGPT(void) {
1154 int i, sum, tableSize, allOK = 1;
1155 uint8_t blankSector[512];
1156 uint8_t* emptyTable;
1157
1158 for (i = 0; i < 512; i++) {
1159 blankSector[i] = 0;
1160 } // for
1161
1162 if (myDisk.OpenForWrite()) {
1163 if (!myDisk.Seek(mainHeader.currentLBA))
1164 allOK = 0;
1165 if (myDisk.Write(blankSector, 512) != 512) { // blank it out
1166 cerr << "Warning! GPT main header not overwritten! Error is " << errno << "\n";
1167 allOK = 0;
1168 } // if
1169 if (!myDisk.Seek(mainHeader.partitionEntriesLBA))
1170 allOK = 0;
srs56940283dae2010-04-28 16:44:34 -04001171 tableSize = numParts * mainHeader.sizeOfPartitionEntries;
srs569408bb0da2010-02-19 17:19:55 -05001172 emptyTable = new uint8_t[tableSize];
1173 for (i = 0; i < tableSize; i++)
1174 emptyTable[i] = 0;
1175 if (allOK) {
1176 sum = myDisk.Write(emptyTable, tableSize);
1177 if (sum != tableSize) {
1178 cerr << "Warning! GPT main partition table not overwritten! Error is " << errno << "\n";
1179 allOK = 0;
1180 } // if write failed
1181 } // if
1182 if (!myDisk.Seek(secondHeader.partitionEntriesLBA))
1183 allOK = 0;
1184 if (allOK) {
1185 sum = myDisk.Write(emptyTable, tableSize);
1186 if (sum != tableSize) {
1187 cerr << "Warning! GPT backup partition table not overwritten! Error is "
1188 << errno << "\n";
1189 allOK = 0;
1190 } // if wrong size written
1191 } // if
1192 if (!myDisk.Seek(secondHeader.currentLBA))
1193 allOK = 0;
1194 if (allOK) {
1195 if (myDisk.Write(blankSector, 512) != 512) { // blank it out
1196 cerr << "Warning! GPT backup header not overwritten! Error is " << errno << "\n";
1197 allOK = 0;
1198 } // if
1199 } // if
1200 myDisk.DiskSync();
1201 myDisk.Close();
1202 cout << "GPT data structures destroyed! You may now partition the disk using fdisk or\n"
1203 << "other utilities.\n";
1204 delete[] emptyTable;
1205 } else {
1206 cerr << "Problem opening " << device << " for writing! Program will now terminate.\n";
1207 } // if/else (fd != -1)
1208 return (allOK);
1209} // GPTDataTextUI::DestroyGPT()
1210
1211// Wipe MBR data from the disk (zero it out completely)
1212// Returns 1 on success, 0 on failure.
1213int GPTData::DestroyMBR(void) {
1214 int allOK = 1, i;
1215 uint8_t blankSector[512];
1216
1217 for (i = 0; i < 512; i++)
1218 blankSector[i] = 0;
1219
1220 if (myDisk.OpenForWrite()) {
1221 if (myDisk.Seek(0)) {
1222 if (myDisk.Write(blankSector, 512) != 512)
1223 allOK = 0;
1224 } else allOK = 0;
1225 } else allOK = 0;
1226 if (!allOK)
1227 cerr << "Warning! MBR not overwritten! Error is " << errno << "!\n";
1228 return allOK;
1229} // GPTData::DestroyMBR(void)
1230
srs5694e4ac11e2009-08-31 10:13:04 -04001231// Tell user whether Apple Partition Map (APM) was discovered....
1232void GPTData::ShowAPMState(void) {
1233 if (apmFound)
srs5694fed16d02010-01-27 23:03:40 -05001234 cout << " APM: present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001235 else
srs5694fed16d02010-01-27 23:03:40 -05001236 cout << " APM: not present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001237} // GPTData::ShowAPMState()
1238
1239// Tell user about the state of the GPT data....
1240void GPTData::ShowGPTState(void) {
1241 switch (state) {
1242 case gpt_invalid:
srs5694fed16d02010-01-27 23:03:40 -05001243 cout << " GPT: not present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001244 break;
1245 case gpt_valid:
srs5694fed16d02010-01-27 23:03:40 -05001246 cout << " GPT: present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001247 break;
1248 case gpt_corrupt:
srs5694fed16d02010-01-27 23:03:40 -05001249 cout << " GPT: damaged\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001250 break;
1251 default:
srs5694fed16d02010-01-27 23:03:40 -05001252 cout << "\a GPT: unknown -- bug!\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001253 break;
1254 } // switch
1255} // GPTData::ShowGPTState()
1256
1257// Display the basic GPT data
1258void GPTData::DisplayGPTData(void) {
srs5694e321d442010-01-29 17:44:04 -05001259 uint32_t i;
srs5694e4ac11e2009-08-31 10:13:04 -04001260 uint64_t temp, totalFree;
1261
srs5694fed16d02010-01-27 23:03:40 -05001262 cout << "Disk " << device << ": " << diskSize << " sectors, "
srs56940873e9d2010-10-07 13:00:45 -04001263 << BytesToSI(diskSize, blockSize) << "\n";
srs5694fed16d02010-01-27 23:03:40 -05001264 cout << "Logical sector size: " << blockSize << " bytes\n";
srs56945a081752010-09-24 20:39:41 -04001265 cout << "Disk identifier (GUID): " << mainHeader.diskGUID << "\n";
srs56940283dae2010-04-28 16:44:34 -04001266 cout << "Partition table holds up to " << numParts << " entries\n";
srs5694fed16d02010-01-27 23:03:40 -05001267 cout << "First usable sector is " << mainHeader.firstUsableLBA
1268 << ", last usable sector is " << mainHeader.lastUsableLBA << "\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001269 totalFree = FindFreeBlocks(&i, &temp);
srs56948a4ddfc2010-03-21 19:05:49 -04001270 cout << "Partitions will be aligned on " << sectorAlignment << "-sector boundaries\n";
srs5694fed16d02010-01-27 23:03:40 -05001271 cout << "Total free space is " << totalFree << " sectors ("
srs56940873e9d2010-10-07 13:00:45 -04001272 << BytesToSI(totalFree, blockSize) << ")\n";
srs5694fed16d02010-01-27 23:03:40 -05001273 cout << "\nNumber Start (sector) End (sector) Size Code Name\n";
srs56940283dae2010-04-28 16:44:34 -04001274 for (i = 0; i < numParts; i++) {
srs5694978041c2009-09-21 20:51:47 -04001275 partitions[i].ShowSummary(i, blockSize);
srs5694e4ac11e2009-08-31 10:13:04 -04001276 } // for
1277} // GPTData::DisplayGPTData()
1278
srs5694e4ac11e2009-08-31 10:13:04 -04001279// Show detailed information on the specified partition
1280void GPTData::ShowPartDetails(uint32_t partNum) {
srs56940873e9d2010-10-07 13:00:45 -04001281 if (!IsFreePartNum(partNum)) {
srs5694e4ac11e2009-08-31 10:13:04 -04001282 partitions[partNum].ShowDetails(blockSize);
1283 } else {
srs5694fed16d02010-01-27 23:03:40 -05001284 cout << "Partition #" << partNum + 1 << " does not exist.";
srs5694e4ac11e2009-08-31 10:13:04 -04001285 } // if
1286} // GPTData::ShowPartDetails()
1287
srs5694e4ac11e2009-08-31 10:13:04 -04001288/**************************************************************************
1289 * *
1290 * Partition table transformation functions (MBR or BSD disklabel to GPT) *
1291 * (some of these functions may require user interaction) *
1292 * *
1293 **************************************************************************/
1294
srs569408bb0da2010-02-19 17:19:55 -05001295// Examines the MBR & GPT data to determine which set of data to use: the
1296// MBR (use_mbr), the GPT (use_gpt), the BSD disklabel (use_bsd), or create
1297// a new set of partitions (use_new). A return value of use_abort indicates
1298// that this function couldn't determine what to do. Overriding functions
1299// in derived classes may ask users questions in such cases.
srs5694e4ac11e2009-08-31 10:13:04 -04001300WhichToUse GPTData::UseWhichPartitions(void) {
1301 WhichToUse which = use_new;
1302 MBRValidity mbrState;
srs5694e4ac11e2009-08-31 10:13:04 -04001303
1304 mbrState = protectiveMBR.GetValidity();
1305
1306 if ((state == gpt_invalid) && ((mbrState == mbr) || (mbrState == hybrid))) {
srs5694fed16d02010-01-27 23:03:40 -05001307 cout << "\n***************************************************************\n"
1308 << "Found invalid GPT and valid MBR; converting MBR to GPT format.\n";
srs56945d58fe02010-01-03 20:57:08 -05001309 if (!justLooking) {
srs56940283dae2010-04-28 16:44:34 -04001310 cout << "\aTHIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by typing 'q' if\n"
srs5694fed16d02010-01-27 23:03:40 -05001311 << "you don't want to convert your MBR partitions to GPT format!\n";
srs56945d58fe02010-01-03 20:57:08 -05001312 } // if
srs5694fed16d02010-01-27 23:03:40 -05001313 cout << "***************************************************************\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001314 which = use_mbr;
1315 } // if
1316
1317 if ((state == gpt_invalid) && bsdFound) {
srs5694fed16d02010-01-27 23:03:40 -05001318 cout << "\n**********************************************************************\n"
1319 << "Found invalid GPT and valid BSD disklabel; converting BSD disklabel\n"
1320 << "to GPT format.";
srs56940a697312010-01-28 21:10:52 -05001321 if ((!justLooking) && (!beQuiet)) {
srs56940283dae2010-04-28 16:44:34 -04001322 cout << "\a THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Your first\n"
srs5694fed16d02010-01-27 23:03:40 -05001323 << "BSD partition will likely be unusable. Exit by typing 'q' if you don't\n"
1324 << "want to convert your BSD partitions to GPT format!";
srs56945d58fe02010-01-03 20:57:08 -05001325 } // if
srs5694fed16d02010-01-27 23:03:40 -05001326 cout << "\n**********************************************************************\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001327 which = use_bsd;
1328 } // if
1329
1330 if ((state == gpt_valid) && (mbrState == gpt)) {
srs5694e4ac11e2009-08-31 10:13:04 -04001331 which = use_gpt;
srs56943c0af382010-01-15 19:19:18 -05001332 if (!beQuiet)
srs5694fed16d02010-01-27 23:03:40 -05001333 cout << "Found valid GPT with protective MBR; using GPT.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001334 } // if
1335 if ((state == gpt_valid) && (mbrState == hybrid)) {
srs5694e4ac11e2009-08-31 10:13:04 -04001336 which = use_gpt;
srs56943c0af382010-01-15 19:19:18 -05001337 if (!beQuiet)
srs5694fed16d02010-01-27 23:03:40 -05001338 cout << "Found valid GPT with hybrid MBR; using GPT.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001339 } // if
1340 if ((state == gpt_valid) && (mbrState == invalid)) {
srs56940a697312010-01-28 21:10:52 -05001341 cout << "\aFound valid GPT with corrupt MBR; using GPT and will write new\n"
srs5694fed16d02010-01-27 23:03:40 -05001342 << "protective MBR on save.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001343 which = use_gpt;
srs5694e4ac11e2009-08-31 10:13:04 -04001344 } // if
1345 if ((state == gpt_valid) && (mbrState == mbr)) {
srs569408bb0da2010-02-19 17:19:55 -05001346 which = use_abort;
srs5694e4ac11e2009-08-31 10:13:04 -04001347 } // if
1348
srs5694e4ac11e2009-08-31 10:13:04 -04001349 if (state == gpt_corrupt) {
srs569408bb0da2010-02-19 17:19:55 -05001350 if (mbrState == gpt) {
1351 cout << "\a\a****************************************************************************\n"
1352 << "Caution: Found protective or hybrid MBR and corrupt GPT. Using GPT, but disk\n"
1353 << "verification and recovery are STRONGLY recommended.\n"
1354 << "****************************************************************************\n";
1355 which = use_gpt;
srs56943c0af382010-01-15 19:19:18 -05001356 } else {
srs569408bb0da2010-02-19 17:19:55 -05001357 which = use_abort;
1358 } // if/else MBR says disk is GPT
1359 } // if GPT corrupt
srs5694e4ac11e2009-08-31 10:13:04 -04001360
1361 if (which == use_new)
srs5694fed16d02010-01-27 23:03:40 -05001362 cout << "Creating new GPT entries.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001363
1364 return which;
1365} // UseWhichPartitions()
1366
srs569408bb0da2010-02-19 17:19:55 -05001367// Convert MBR partition table into GPT form.
1368void GPTData::XFormPartitions(void) {
srs5694e4ac11e2009-08-31 10:13:04 -04001369 int i, numToConvert;
1370 uint8_t origType;
srs5694e4ac11e2009-08-31 10:13:04 -04001371
1372 // Clear out old data & prepare basics....
1373 ClearGPTData();
1374
1375 // Convert the smaller of the # of GPT or MBR partitions
srs56940283dae2010-04-28 16:44:34 -04001376 if (numParts > MAX_MBR_PARTS)
srs5694978041c2009-09-21 20:51:47 -04001377 numToConvert = MAX_MBR_PARTS;
srs5694e4ac11e2009-08-31 10:13:04 -04001378 else
srs56940283dae2010-04-28 16:44:34 -04001379 numToConvert = numParts;
srs5694e4ac11e2009-08-31 10:13:04 -04001380
1381 for (i = 0; i < numToConvert; i++) {
1382 origType = protectiveMBR.GetType(i);
1383 // don't waste CPU time trying to convert extended, hybrid protective, or
1384 // null (non-existent) partitions
srs5694e35eb1b2009-09-14 00:29:34 -04001385 if ((origType != 0x05) && (origType != 0x0f) && (origType != 0x85) &&
srs56946699b012010-02-04 00:55:30 -05001386 (origType != 0x00) && (origType != 0xEE))
srs5694e4ac11e2009-08-31 10:13:04 -04001387 partitions[i] = protectiveMBR.AsGPT(i);
1388 } // for
1389
1390 // Convert MBR into protective MBR
1391 protectiveMBR.MakeProtectiveMBR();
1392
1393 // Record that all original CRCs were OK so as not to raise flags
1394 // when doing a disk verification
1395 mainCrcOk = secondCrcOk = mainPartsCrcOk = secondPartsCrcOk = 1;
srs5694e4ac11e2009-08-31 10:13:04 -04001396} // GPTData::XFormPartitions()
1397
1398// Transforms BSD disklabel on the specified partition (numbered from 0).
srs569408bb0da2010-02-19 17:19:55 -05001399// If an invalid partition number is given, the program does nothing.
srs5694e4ac11e2009-08-31 10:13:04 -04001400// Returns the number of new partitions created.
srs569408bb0da2010-02-19 17:19:55 -05001401int GPTData::XFormDisklabel(uint32_t partNum) {
1402 uint32_t low, high;
srs5694e4ac11e2009-08-31 10:13:04 -04001403 int goOn = 1, numDone = 0;
1404 BSDData disklabel;
1405
srs569408bb0da2010-02-19 17:19:55 -05001406 if (GetPartRange(&low, &high) == 0) {
1407 goOn = 0;
1408 cout << "No partitions!\n";
1409 } // if
1410 if (partNum > high) {
1411 goOn = 0;
1412 cout << "Specified partition is invalid!\n";
1413 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04001414
srs569408bb0da2010-02-19 17:19:55 -05001415 // If all is OK, read the disklabel and convert it.
1416 if (goOn) {
1417 goOn = disklabel.ReadBSDData(&myDisk, partitions[partNum].GetFirstLBA(),
1418 partitions[partNum].GetLastLBA());
1419 if ((goOn) && (disklabel.IsDisklabel())) {
1420 numDone = XFormDisklabel(&disklabel);
1421 if (numDone == 1)
1422 cout << "Converted 1 BSD partition.\n";
1423 else
1424 cout << "Converted " << numDone << " BSD partitions.\n";
1425 } else {
1426 cout << "Unable to convert partitions! Unrecognized BSD disklabel.\n";
1427 } // if/else
1428 } // if
1429 if (numDone > 0) { // converted partitions; delete carrier
1430 partitions[partNum].BlankPartition();
1431 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04001432 return numDone;
srs569455d92612010-03-07 22:16:07 -05001433} // GPTData::XFormDisklabel(uint32_t i)
srs5694e4ac11e2009-08-31 10:13:04 -04001434
1435// Transform the partitions on an already-loaded BSD disklabel...
srs569408bb0da2010-02-19 17:19:55 -05001436int GPTData::XFormDisklabel(BSDData* disklabel) {
1437 int i, partNum = 0, numDone = 0;
srs5694e4ac11e2009-08-31 10:13:04 -04001438
srs569408bb0da2010-02-19 17:19:55 -05001439 if (disklabel->IsDisklabel()) {
srs5694e4ac11e2009-08-31 10:13:04 -04001440 for (i = 0; i < disklabel->GetNumParts(); i++) {
srs569408bb0da2010-02-19 17:19:55 -05001441 partNum = FindFirstFreePart();
1442 if (partNum >= 0) {
1443 partitions[partNum] = disklabel->AsGPT(i);
1444 if (partitions[partNum].IsUsed())
1445 numDone++;
1446 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04001447 } // for
srs569408bb0da2010-02-19 17:19:55 -05001448 if (partNum == -1)
1449 cerr << "Warning! Too many partitions to convert!\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001450 } // if
1451
1452 // Record that all original CRCs were OK so as not to raise flags
1453 // when doing a disk verification
1454 mainCrcOk = secondCrcOk = mainPartsCrcOk = secondPartsCrcOk = 1;
1455
1456 return numDone;
1457} // GPTData::XFormDisklabel(BSDData* disklabel)
1458
srs569408bb0da2010-02-19 17:19:55 -05001459// Add one GPT partition to MBR. Used by PartsToMBR() functions. Created
1460// partition has the active/bootable flag UNset and uses the GPT fdisk
1461// type code divided by 0x0100 as the MBR type code.
1462// Returns 1 if operation was 100% successful, 0 if there were ANY
1463// problems.
srs5694978041c2009-09-21 20:51:47 -04001464int GPTData::OnePartToMBR(uint32_t gptPart, int mbrPart) {
srs569408bb0da2010-02-19 17:19:55 -05001465 int allOK = 1;
srs5694fed16d02010-01-27 23:03:40 -05001466
srs5694978041c2009-09-21 20:51:47 -04001467 if ((mbrPart < 0) || (mbrPart > 3)) {
srs5694fed16d02010-01-27 23:03:40 -05001468 cout << "MBR partition " << mbrPart + 1 << " is out of range; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001469 allOK = 0;
1470 } // if
srs56940283dae2010-04-28 16:44:34 -04001471 if (gptPart >= numParts) {
srs5694fed16d02010-01-27 23:03:40 -05001472 cout << "GPT partition " << gptPart + 1 << " is out of range; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001473 allOK = 0;
1474 } // if
1475 if (allOK && (partitions[gptPart].GetLastLBA() == UINT64_C(0))) {
srs5694fed16d02010-01-27 23:03:40 -05001476 cout << "GPT partition " << gptPart + 1 << " is undefined; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001477 allOK = 0;
1478 } // if
1479 if (allOK && (partitions[gptPart].GetFirstLBA() <= UINT32_MAX) &&
1480 (partitions[gptPart].GetLengthLBA() <= UINT32_MAX)) {
1481 if (partitions[gptPart].GetLastLBA() > UINT32_MAX) {
srs5694fed16d02010-01-27 23:03:40 -05001482 cout << "Caution: Partition end point past 32-bit pointer boundary;"
1483 << " some OSes may\nreact strangely.\n";
srs569408bb0da2010-02-19 17:19:55 -05001484 } // if
srs5694978041c2009-09-21 20:51:47 -04001485 protectiveMBR.MakePart(mbrPart, (uint32_t) partitions[gptPart].GetFirstLBA(),
srs569408bb0da2010-02-19 17:19:55 -05001486 (uint32_t) partitions[gptPart].GetLengthLBA(),
1487 partitions[gptPart].GetHexType() / 256, 0);
srs5694978041c2009-09-21 20:51:47 -04001488 } else { // partition out of range
srs569408bb0da2010-02-19 17:19:55 -05001489 if (allOK) // Display only if "else" triggered by out-of-bounds condition
1490 cout << "Partition " << gptPart + 1 << " begins beyond the 32-bit pointer limit of MBR "
1491 << "partitions, or is\n too big; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001492 allOK = 0;
1493 } // if/else
1494 return allOK;
1495} // GPTData::OnePartToMBR()
1496
srs569455d92612010-03-07 22:16:07 -05001497// Convert partitions to MBR form (primary and logical) and return
1498// the number done. Partitions are specified in a PartNotes variable,
1499// which includes pointers to GPT partition numbers. A partition number
1500// of MBR_EFI_GPT means to place an EFI GPT protective partition in that
1501// location in the table, and MBR_EMPTY means not to create a partition
1502// in that table position. If the partition type entry for a partition
1503// is 0, a default entry is used, based on the GPT partition type code.
srs569408bb0da2010-02-19 17:19:55 -05001504// Returns the number of partitions converted, NOT counting EFI GPT
srs569455d92612010-03-07 22:16:07 -05001505// protective partitions or extended partitions.
srs5694058d4a52010-10-12 12:42:47 -04001506int GPTData::PartsToMBR(PartNotes * notes) {
srs569455d92612010-03-07 22:16:07 -05001507 int mbrNum = 0, numConverted = 0;
1508 struct PartInfo convInfo;
srs5694978041c2009-09-21 20:51:47 -04001509
srs56949ddc14b2010-08-22 22:44:42 -04001510 protectiveMBR.EmptyMBR(0);
srs569455d92612010-03-07 22:16:07 -05001511 protectiveMBR.SetDiskSize(diskSize);
srs5694058d4a52010-10-12 12:42:47 -04001512 if (!notes->IsLegal())
1513 notes->MakeItLegal();
1514 notes->Rewind();
1515 while (notes->GetNextInfo(&convInfo) >= 0) {
srs569455d92612010-03-07 22:16:07 -05001516 if ((convInfo.gptPartNum >= 0) && (convInfo.type == PRIMARY)) {
1517 numConverted += OnePartToMBR((uint32_t) convInfo.gptPartNum, mbrNum);
1518 if (convInfo.hexCode != 0)
1519 protectiveMBR.SetPartType(mbrNum, convInfo.hexCode);
1520 if (convInfo.active)
1521 protectiveMBR.SetPartBootable(mbrNum);
1522 mbrNum++;
1523 } // if
srs569461768bc2010-07-04 01:54:00 -04001524 if (convInfo.gptPartNum == MBR_EFI_GPT)
1525 mbrNum++;
1526 } // for
1527 // Now go through and set sizes for MBR_EFI_GPT partitions....
srs5694058d4a52010-10-12 12:42:47 -04001528 notes->Rewind();
srs569461768bc2010-07-04 01:54:00 -04001529 mbrNum = 0;
srs5694058d4a52010-10-12 12:42:47 -04001530 while (notes->GetNextInfo(&convInfo) >= 0) {
srs569461768bc2010-07-04 01:54:00 -04001531 if ((convInfo.gptPartNum >= 0) && (convInfo.type == PRIMARY))
1532 mbrNum++;
srs569455d92612010-03-07 22:16:07 -05001533 if (convInfo.gptPartNum == MBR_EFI_GPT) {
1534 if (protectiveMBR.FindFirstAvailable() == UINT32_C(1)) {
1535 protectiveMBR.MakePart(mbrNum, 1, protectiveMBR.FindLastInFree(1), convInfo.hexCode);
1536 protectiveMBR.SetHybrid();
1537 } else {
1538 protectiveMBR.MakeBiggestPart(mbrNum, convInfo.hexCode);
1539 } // if/else
1540 mbrNum++;
srs569461768bc2010-07-04 01:54:00 -04001541 } // if
1542 } // while
srs569455d92612010-03-07 22:16:07 -05001543 // Now do logical partition(s)...
1544 protectiveMBR.SetDisk(&myDisk);
1545 numConverted += protectiveMBR.CreateLogicals(notes);
srs569408bb0da2010-02-19 17:19:55 -05001546 return numConverted;
1547} // GPTData::PartsToMBR()
1548
srs5694e4ac11e2009-08-31 10:13:04 -04001549
1550/**********************************************************************
1551 * *
1552 * Functions that adjust GPT data structures WITHOUT user interaction *
1553 * (they may display information for the user's benefit, though) *
1554 * *
1555 **********************************************************************/
1556
1557// Resizes GPT to specified number of entries. Creates a new table if
srs5694ba00fed2010-01-12 18:18:36 -05001558// necessary, copies data if it already exists. Returns 1 if all goes
1559// well, 0 if an error is encountered.
srs5694e4ac11e2009-08-31 10:13:04 -04001560int GPTData::SetGPTSize(uint32_t numEntries) {
srs569408bb0da2010-02-19 17:19:55 -05001561 GPTPart* newParts;
1562 GPTPart* trash;
srs5694e4ac11e2009-08-31 10:13:04 -04001563 uint32_t i, high, copyNum;
1564 int allOK = 1;
1565
1566 // First, adjust numEntries upward, if necessary, to get a number
1567 // that fills the allocated sectors
1568 i = blockSize / GPT_SIZE;
1569 if ((numEntries % i) != 0) {
srs5694fed16d02010-01-27 23:03:40 -05001570 cout << "Adjusting GPT size from " << numEntries << " to ";
srs5694e4ac11e2009-08-31 10:13:04 -04001571 numEntries = ((numEntries / i) + 1) * i;
srs5694fed16d02010-01-27 23:03:40 -05001572 cout << numEntries << " to fill the sector\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001573 } // if
1574
srs5694247657a2009-11-26 18:36:12 -05001575 // Do the work only if the # of partitions is changing. Along with being
srs569455d92612010-03-07 22:16:07 -05001576 // efficient, this prevents mucking with the location of the secondary
srs5694247657a2009-11-26 18:36:12 -05001577 // partition table, which causes problems when loading data from a RAID
1578 // array that's been expanded because this function is called when loading
1579 // data.
srs56940283dae2010-04-28 16:44:34 -04001580 if (((numEntries != numParts) || (partitions == NULL)) && (numEntries > 0)) {
srs5694cb76c672010-02-11 22:22:22 -05001581 newParts = new GPTPart [numEntries * sizeof (GPTPart)];
srs5694247657a2009-11-26 18:36:12 -05001582 if (newParts != NULL) {
1583 if (partitions != NULL) { // existing partitions; copy them over
1584 GetPartRange(&i, &high);
1585 if (numEntries < (high + 1)) { // Highest entry too high for new #
srs5694fed16d02010-01-27 23:03:40 -05001586 cout << "The highest-numbered partition is " << high + 1
1587 << ", which is greater than the requested\n"
1588 << "partition table size of " << numEntries
1589 << "; cannot resize. Perhaps sorting will help.\n";
srs5694247657a2009-11-26 18:36:12 -05001590 allOK = 0;
1591 } else { // go ahead with copy
srs56940283dae2010-04-28 16:44:34 -04001592 if (numEntries < numParts)
srs5694247657a2009-11-26 18:36:12 -05001593 copyNum = numEntries;
1594 else
srs56940283dae2010-04-28 16:44:34 -04001595 copyNum = numParts;
srs5694247657a2009-11-26 18:36:12 -05001596 for (i = 0; i < copyNum; i++) {
1597 newParts[i] = partitions[i];
1598 } // for
1599 trash = partitions;
1600 partitions = newParts;
srs5694cb76c672010-02-11 22:22:22 -05001601 delete[] trash;
srs5694247657a2009-11-26 18:36:12 -05001602 } // if
1603 } else { // No existing partition table; just create it
srs5694e4ac11e2009-08-31 10:13:04 -04001604 partitions = newParts;
srs5694247657a2009-11-26 18:36:12 -05001605 } // if/else existing partitions
srs56940283dae2010-04-28 16:44:34 -04001606 numParts = numEntries;
srs5694247657a2009-11-26 18:36:12 -05001607 mainHeader.firstUsableLBA = ((numEntries * GPT_SIZE) / blockSize) + 2 ;
1608 secondHeader.firstUsableLBA = mainHeader.firstUsableLBA;
1609 MoveSecondHeaderToEnd();
1610 if (diskSize > 0)
1611 CheckGPTSize();
1612 } else { // Bad memory allocation
srs5694fed16d02010-01-27 23:03:40 -05001613 cerr << "Error allocating memory for partition table!\n";
srs5694247657a2009-11-26 18:36:12 -05001614 allOK = 0;
1615 } // if/else
srs5694e4ac11e2009-08-31 10:13:04 -04001616 } // if/else
srs56940283dae2010-04-28 16:44:34 -04001617 mainHeader.numParts = numParts;
1618 secondHeader.numParts = numParts;
srs5694e4ac11e2009-08-31 10:13:04 -04001619 return (allOK);
1620} // GPTData::SetGPTSize()
1621
1622// Blank the partition array
1623void GPTData::BlankPartitions(void) {
1624 uint32_t i;
1625
srs56940283dae2010-04-28 16:44:34 -04001626 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04001627 partitions[i].BlankPartition();
1628 } // for
1629} // GPTData::BlankPartitions()
1630
srs5694ba00fed2010-01-12 18:18:36 -05001631// Delete a partition by number. Returns 1 if successful,
1632// 0 if there was a problem. Returns 1 if partition was in
1633// range, 0 if it was out of range.
1634int GPTData::DeletePartition(uint32_t partNum) {
1635 uint64_t startSector, length;
srs56940283dae2010-04-28 16:44:34 -04001636 uint32_t low, high, numUsedParts, retval = 1;;
srs5694ba00fed2010-01-12 18:18:36 -05001637
srs56940283dae2010-04-28 16:44:34 -04001638 numUsedParts = GetPartRange(&low, &high);
1639 if ((numUsedParts > 0) && (partNum >= low) && (partNum <= high)) {
srs5694ba00fed2010-01-12 18:18:36 -05001640 // In case there's a protective MBR, look for & delete matching
1641 // MBR partition....
1642 startSector = partitions[partNum].GetFirstLBA();
1643 length = partitions[partNum].GetLengthLBA();
1644 protectiveMBR.DeleteByLocation(startSector, length);
1645
1646 // Now delete the GPT partition
1647 partitions[partNum].BlankPartition();
1648 } else {
srs5694fed16d02010-01-27 23:03:40 -05001649 cerr << "Partition number " << partNum + 1 << " out of range!\n";
srs5694ba00fed2010-01-12 18:18:36 -05001650 retval = 0;
1651 } // if/else
1652 return retval;
1653} // GPTData::DeletePartition(uint32_t partNum)
1654
srs569408bb0da2010-02-19 17:19:55 -05001655// Non-interactively create a partition.
1656// Returns 1 if the operation was successful, 0 if a problem was discovered.
srs5694e321d442010-01-29 17:44:04 -05001657uint32_t GPTData::CreatePartition(uint32_t partNum, uint64_t startSector, uint64_t endSector) {
srs5694ba00fed2010-01-12 18:18:36 -05001658 int retval = 1; // assume there'll be no problems
srs56945a081752010-09-24 20:39:41 -04001659 uint64_t origSector = startSector;
srs5694ba00fed2010-01-12 18:18:36 -05001660
1661 if (IsFreePartNum(partNum)) {
srs56945a081752010-09-24 20:39:41 -04001662 if (Align(&startSector)) {
1663 cout << "Information: Moved requested sector from " << origSector << " to "
1664 << startSector << " in\norder to align on " << sectorAlignment
1665 << "-sector boundaries.\n";
1666 } // if
srs5694ba00fed2010-01-12 18:18:36 -05001667 if (IsFree(startSector) && (startSector <= endSector)) {
1668 if (FindLastInFree(startSector) >= endSector) {
1669 partitions[partNum].SetFirstLBA(startSector);
1670 partitions[partNum].SetLastLBA(endSector);
1671 partitions[partNum].SetType(0x0700);
srs56946699b012010-02-04 00:55:30 -05001672 partitions[partNum].RandomizeUniqueGUID();
srs5694ba00fed2010-01-12 18:18:36 -05001673 } else retval = 0; // if free space until endSector
1674 } else retval = 0; // if startSector is free
1675 } else retval = 0; // if legal partition number
1676 return retval;
1677} // GPTData::CreatePartition(partNum, startSector, endSector)
1678
srs5694e4ac11e2009-08-31 10:13:04 -04001679// Sort the GPT entries, eliminating gaps and making for a logical
1680// ordering. Relies on QuickSortGPT() for the bulk of the work
1681void GPTData::SortGPT(void) {
srs5694546a9c72010-01-26 16:00:26 -05001682 uint32_t i, numFound, firstPart, lastPart;
srs5694e4ac11e2009-08-31 10:13:04 -04001683
1684 // First, find the last partition with data, so as not to
1685 // spend needless time sorting empty entries....
srs5694546a9c72010-01-26 16:00:26 -05001686 numFound = GetPartRange(&firstPart, &lastPart);
srs5694e4ac11e2009-08-31 10:13:04 -04001687
1688 // Now swap empties with the last partitions, to simplify the logic
1689 // in the Quicksort function....
1690 i = 0;
1691 while (i < lastPart) {
1692 if (partitions[i].GetFirstLBA() == 0) {
srs569408bb0da2010-02-19 17:19:55 -05001693 SwapPartitions(i, lastPart);
srs5694546a9c72010-01-26 16:00:26 -05001694 do {
1695 lastPart--;
1696 } while ((lastPart > 0) && (partitions[lastPart].GetFirstLBA() == 0));
srs5694e4ac11e2009-08-31 10:13:04 -04001697 } // if
1698 i++;
1699 } // while
1700
srs5694546a9c72010-01-26 16:00:26 -05001701 // If there are more empties than partitions in the range from 0 to lastPart,
1702 // the above leaves lastPart set too high, so we've got to adjust it to
1703 // prevent empties from migrating to the top of the list....
1704 GetPartRange(&firstPart, &lastPart);
1705
srs5694e4ac11e2009-08-31 10:13:04 -04001706 // Now call the recursive quick sort routine to do the real work....
srs569408bb0da2010-02-19 17:19:55 -05001707 QuickSortGPT(0, lastPart);
srs5694e4ac11e2009-08-31 10:13:04 -04001708} // GPTData::SortGPT()
1709
srs569408bb0da2010-02-19 17:19:55 -05001710// Recursive quick sort algorithm for GPT partitions. Note that if there
1711// are any empties in the specified range, they'll be sorted to the
1712// start, resulting in a sorted set of partitions that begins with
1713// partition 2, 3, or higher.
1714void GPTData::QuickSortGPT(int start, int finish) {
1715 uint64_t starterValue; // starting location of median partition
1716 int left, right;
1717
1718 left = start;
1719 right = finish;
1720 starterValue = partitions[(start + finish) / 2].GetFirstLBA();
1721 do {
1722 while (partitions[left].GetFirstLBA() < starterValue)
1723 left++;
1724 while (partitions[right].GetFirstLBA() > starterValue)
1725 right--;
1726 if (left <= right)
1727 SwapPartitions(left++, right--);
1728 } while (left <= right);
1729 if (start < right) QuickSortGPT(start, right);
1730 if (finish > left) QuickSortGPT(left, finish);
1731} // GPTData::QuickSortGPT()
1732
1733// Swap the contents of two partitions.
1734// Returns 1 if successful, 0 if either partition is out of range
1735// (that is, not a legal number; either or both can be empty).
1736// Note that if partNum1 = partNum2 and this number is in range,
1737// it will be considered successful.
1738int GPTData::SwapPartitions(uint32_t partNum1, uint32_t partNum2) {
1739 GPTPart temp;
1740 int allOK = 1;
1741
srs56940283dae2010-04-28 16:44:34 -04001742 if ((partNum1 < numParts) && (partNum2 < numParts)) {
srs569408bb0da2010-02-19 17:19:55 -05001743 if (partNum1 != partNum2) {
1744 temp = partitions[partNum1];
1745 partitions[partNum1] = partitions[partNum2];
1746 partitions[partNum2] = temp;
1747 } // if
1748 } else allOK = 0; // partition numbers are valid
1749 return allOK;
1750} // GPTData::SwapPartitions()
1751
srs5694e4ac11e2009-08-31 10:13:04 -04001752// Set up data structures for entirely new set of partitions on the
1753// specified device. Returns 1 if OK, 0 if there were problems.
srs5694e35eb1b2009-09-14 00:29:34 -04001754// Note that this function does NOT clear the protectiveMBR data
1755// structure, since it may hold the original MBR partitions if the
1756// program was launched on an MBR disk, and those may need to be
1757// converted to GPT format.
srs5694e4ac11e2009-08-31 10:13:04 -04001758int GPTData::ClearGPTData(void) {
srs5694e35eb1b2009-09-14 00:29:34 -04001759 int goOn = 1, i;
srs5694e4ac11e2009-08-31 10:13:04 -04001760
1761 // Set up the partition table....
srs5694fed16d02010-01-27 23:03:40 -05001762 if (partitions != NULL)
srs5694cb76c672010-02-11 22:22:22 -05001763 delete[] partitions;
srs5694e4ac11e2009-08-31 10:13:04 -04001764 partitions = NULL;
1765 SetGPTSize(NUM_GPT_ENTRIES);
1766
1767 // Now initialize a bunch of stuff that's static....
1768 mainHeader.signature = GPT_SIGNATURE;
1769 mainHeader.revision = 0x00010000;
srs5694978041c2009-09-21 20:51:47 -04001770 mainHeader.headerSize = HEADER_SIZE;
srs5694e4ac11e2009-08-31 10:13:04 -04001771 mainHeader.reserved = 0;
1772 mainHeader.currentLBA = UINT64_C(1);
1773 mainHeader.partitionEntriesLBA = (uint64_t) 2;
1774 mainHeader.sizeOfPartitionEntries = GPT_SIZE;
1775 for (i = 0; i < GPT_RESERVED; i++) {
1776 mainHeader.reserved2[i] = '\0';
1777 } // for
srs56940873e9d2010-10-07 13:00:45 -04001778 if (blockSize > 0)
1779 sectorAlignment = DEFAULT_ALIGNMENT * SECTOR_SIZE / blockSize;
1780 else
1781 sectorAlignment = DEFAULT_ALIGNMENT;
srs5694e4ac11e2009-08-31 10:13:04 -04001782
1783 // Now some semi-static items (computed based on end of disk)
1784 mainHeader.backupLBA = diskSize - UINT64_C(1);
1785 mainHeader.lastUsableLBA = diskSize - mainHeader.firstUsableLBA;
1786
1787 // Set a unique GUID for the disk, based on random numbers
srs56946699b012010-02-04 00:55:30 -05001788 mainHeader.diskGUID.Randomize();
srs5694e4ac11e2009-08-31 10:13:04 -04001789
1790 // Copy main header to backup header
1791 RebuildSecondHeader();
1792
1793 // Blank out the partitions array....
1794 BlankPartitions();
1795
1796 // Flag all CRCs as being OK....
1797 mainCrcOk = 1;
1798 secondCrcOk = 1;
1799 mainPartsCrcOk = 1;
1800 secondPartsCrcOk = 1;
1801
1802 return (goOn);
1803} // GPTData::ClearGPTData()
1804
srs5694247657a2009-11-26 18:36:12 -05001805// Set the location of the second GPT header data to the end of the disk.
1806// Used internally and called by the 'e' option on the recovery &
1807// transformation menu, to help users of RAID arrays who add disk space
1808// to their arrays.
1809void GPTData::MoveSecondHeaderToEnd() {
srs56948bb78762009-11-24 15:43:49 -05001810 mainHeader.backupLBA = secondHeader.currentLBA = diskSize - UINT64_C(1);
1811 mainHeader.lastUsableLBA = secondHeader.lastUsableLBA = diskSize - mainHeader.firstUsableLBA;
1812 secondHeader.partitionEntriesLBA = secondHeader.lastUsableLBA + UINT64_C(1);
1813} // GPTData::FixSecondHeaderLocation()
1814
srs56940a697312010-01-28 21:10:52 -05001815int GPTData::SetName(uint32_t partNum, const string & theName) {
srs5694ba00fed2010-01-12 18:18:36 -05001816 int retval = 1;
srs5694fed16d02010-01-27 23:03:40 -05001817
1818 if (!IsFreePartNum(partNum)) {
1819 partitions[partNum].SetName(theName);
1820 } else retval = 0;
srs5694ba00fed2010-01-12 18:18:36 -05001821
1822 return retval;
srs5694e4ac11e2009-08-31 10:13:04 -04001823} // GPTData::SetName
1824
1825// Set the disk GUID to the specified value. Note that the header CRCs must
1826// be recomputed after calling this function.
1827void GPTData::SetDiskGUID(GUIDData newGUID) {
1828 mainHeader.diskGUID = newGUID;
1829 secondHeader.diskGUID = newGUID;
1830} // SetDiskGUID()
1831
1832// Set the unique GUID of the specified partition. Returns 1 on
1833// successful completion, 0 if there were problems (invalid
1834// partition number).
1835int GPTData::SetPartitionGUID(uint32_t pn, GUIDData theGUID) {
1836 int retval = 0;
1837
srs56940283dae2010-04-28 16:44:34 -04001838 if (pn < numParts) {
srs5694e4ac11e2009-08-31 10:13:04 -04001839 if (partitions[pn].GetFirstLBA() != UINT64_C(0)) {
1840 partitions[pn].SetUniqueGUID(theGUID);
1841 retval = 1;
1842 } // if
1843 } // if
1844 return retval;
1845} // GPTData::SetPartitionGUID()
1846
srs56949ba54212010-05-18 23:24:02 -04001847// Set new random GUIDs for the disk and all partitions. Intended to be used
1848// after disk cloning or similar operations that don't randomize the GUIDs.
1849void GPTData::RandomizeGUIDs(void) {
1850 uint32_t i;
1851
1852 mainHeader.diskGUID.Randomize();
1853 secondHeader.diskGUID = mainHeader.diskGUID;
1854 for (i = 0; i < numParts; i++)
1855 if (partitions[i].IsUsed())
1856 partitions[i].RandomizeUniqueGUID();
1857} // GPTData::RandomizeGUIDs()
1858
srs5694ba00fed2010-01-12 18:18:36 -05001859// Change partition type code non-interactively. Returns 1 if
1860// successful, 0 if not....
srs5694327129e2010-09-22 01:07:31 -04001861int GPTData::ChangePartType(uint32_t partNum, PartType theGUID) {
1862 int retval = 1;
1863
1864 if (!IsFreePartNum(partNum)) {
1865 partitions[partNum].SetType(theGUID);
1866 } else retval = 0;
1867 return retval;
1868} // GPTData::ChangePartType()
1869
srs56949ba54212010-05-18 23:24:02 -04001870// Recompute the CHS values of all the MBR partitions. Used to reset
1871// CHS values that some BIOSes require, despite the fact that the
1872// resulting CHS values violate the GPT standard.
1873void GPTData::RecomputeCHS(void) {
1874 int i;
1875
1876 for (i = 0; i < 4; i++)
1877 protectiveMBR.RecomputeCHS(i);
1878} // GPTData::RecomputeCHS()
1879
srs56941d1448a2009-12-31 21:20:19 -05001880// Adjust sector number so that it falls on a sector boundary that's a
1881// multiple of sectorAlignment. This is done to improve the performance
1882// of Western Digital Advanced Format disks and disks with similar
1883// technology from other companies, which use 4096-byte sectors
1884// internally although they translate to 512-byte sectors for the
1885// benefit of the OS. If partitions aren't properly aligned on these
1886// disks, some filesystem data structures can span multiple physical
1887// sectors, degrading performance. This function should be called
1888// only on the FIRST sector of the partition, not the last!
1889// This function returns 1 if the alignment was altered, 0 if it
1890// was unchanged.
1891int GPTData::Align(uint64_t* sector) {
1892 int retval = 0, sectorOK = 0;
1893 uint64_t earlier, later, testSector, original;
1894
1895 if ((*sector % sectorAlignment) != 0) {
1896 original = *sector;
srs56941d1448a2009-12-31 21:20:19 -05001897 earlier = (*sector / sectorAlignment) * sectorAlignment;
1898 later = earlier + (uint64_t) sectorAlignment;
1899
1900 // Check to see that every sector between the earlier one and the
1901 // requested one is clear, and that it's not too early....
1902 if (earlier >= mainHeader.firstUsableLBA) {
srs56941d1448a2009-12-31 21:20:19 -05001903 sectorOK = 1;
1904 testSector = earlier;
1905 do {
1906 sectorOK = IsFree(testSector++);
1907 } while ((sectorOK == 1) && (testSector < *sector));
1908 if (sectorOK == 1) {
1909 *sector = earlier;
srs56945a081752010-09-24 20:39:41 -04001910 retval = 1;
srs56941d1448a2009-12-31 21:20:19 -05001911 } // if
1912 } // if firstUsableLBA check
1913
1914 // If couldn't move the sector earlier, try to move it later instead....
1915 if ((sectorOK != 1) && (later <= mainHeader.lastUsableLBA)) {
1916 sectorOK = 1;
1917 testSector = later;
1918 do {
1919 sectorOK = IsFree(testSector--);
1920 } while ((sectorOK == 1) && (testSector > *sector));
1921 if (sectorOK == 1) {
1922 *sector = later;
srs56945a081752010-09-24 20:39:41 -04001923 retval = 1;
srs56941d1448a2009-12-31 21:20:19 -05001924 } // if
1925 } // if
srs56941d1448a2009-12-31 21:20:19 -05001926 } // if
1927 return retval;
1928} // GPTData::Align()
1929
srs5694e4ac11e2009-08-31 10:13:04 -04001930/********************************************************
1931 * *
1932 * Functions that return data about GPT data structures *
1933 * (most of these are inline in gpt.h) *
1934 * *
1935 ********************************************************/
1936
1937// Find the low and high used partition numbers (numbered from 0).
1938// Return value is the number of partitions found. Note that the
1939// *low and *high values are both set to 0 when no partitions
1940// are found, as well as when a single partition in the first
1941// position exists. Thus, the return value is the only way to
1942// tell when no partitions exist.
1943int GPTData::GetPartRange(uint32_t *low, uint32_t *high) {
1944 uint32_t i;
1945 int numFound = 0;
1946
srs56940283dae2010-04-28 16:44:34 -04001947 *low = numParts + 1; // code for "not found"
srs5694e4ac11e2009-08-31 10:13:04 -04001948 *high = 0;
srs56940283dae2010-04-28 16:44:34 -04001949 if (numParts > 0) { // only try if partition table exists...
1950 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04001951 if (partitions[i].GetFirstLBA() != UINT64_C(0)) { // it exists
1952 *high = i; // since we're counting up, set the high value
srs569408bb0da2010-02-19 17:19:55 -05001953 // Set the low value only if it's not yet found...
srs56940283dae2010-04-28 16:44:34 -04001954 if (*low == (numParts + 1)) *low = i;
srs569408bb0da2010-02-19 17:19:55 -05001955 numFound++;
srs5694e4ac11e2009-08-31 10:13:04 -04001956 } // if
1957 } // for
1958 } // if
1959
1960 // Above will leave *low pointing to its "not found" value if no partitions
1961 // are defined, so reset to 0 if this is the case....
srs56940283dae2010-04-28 16:44:34 -04001962 if (*low == (numParts + 1))
srs5694e4ac11e2009-08-31 10:13:04 -04001963 *low = 0;
1964 return numFound;
1965} // GPTData::GetPartRange()
1966
srs569408bb0da2010-02-19 17:19:55 -05001967// Returns the value of the first free partition, or -1 if none is
1968// unused.
1969int GPTData::FindFirstFreePart(void) {
1970 int i = 0;
1971
1972 if (partitions != NULL) {
srs56940283dae2010-04-28 16:44:34 -04001973 while ((partitions[i].IsUsed()) && (i < (int) numParts))
srs569408bb0da2010-02-19 17:19:55 -05001974 i++;
srs56940283dae2010-04-28 16:44:34 -04001975 if (i >= (int) numParts)
srs569408bb0da2010-02-19 17:19:55 -05001976 i = -1;
1977 } else i = -1;
1978 return i;
1979} // GPTData::FindFirstFreePart()
1980
srs5694978041c2009-09-21 20:51:47 -04001981// Returns the number of defined partitions.
1982uint32_t GPTData::CountParts(void) {
srs5694e321d442010-01-29 17:44:04 -05001983 uint32_t i, counted = 0;
srs5694978041c2009-09-21 20:51:47 -04001984
srs56940283dae2010-04-28 16:44:34 -04001985 for (i = 0; i < numParts; i++) {
srs569408bb0da2010-02-19 17:19:55 -05001986 if (partitions[i].IsUsed())
srs5694978041c2009-09-21 20:51:47 -04001987 counted++;
1988 } // for
1989 return counted;
1990} // GPTData::CountParts()
1991
srs5694e4ac11e2009-08-31 10:13:04 -04001992/****************************************************
1993 * *
1994 * Functions that return data about disk free space *
1995 * *
1996 ****************************************************/
1997
1998// Find the first available block after the starting point; returns 0 if
1999// there are no available blocks left
2000uint64_t GPTData::FindFirstAvailable(uint64_t start) {
2001 uint64_t first;
2002 uint32_t i;
2003 int firstMoved = 0;
2004
2005 // Begin from the specified starting point or from the first usable
2006 // LBA, whichever is greater...
2007 if (start < mainHeader.firstUsableLBA)
2008 first = mainHeader.firstUsableLBA;
2009 else
2010 first = start;
2011
2012 // ...now search through all partitions; if first is within an
2013 // existing partition, move it to the next sector after that
2014 // partition and repeat. If first was moved, set firstMoved
2015 // flag; repeat until firstMoved is not set, so as to catch
2016 // cases where partitions are out of sequential order....
2017 do {
2018 firstMoved = 0;
srs56940283dae2010-04-28 16:44:34 -04002019 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04002020 if ((first >= partitions[i].GetFirstLBA()) &&
srs569455d92612010-03-07 22:16:07 -05002021 (first <= partitions[i].GetLastLBA())) { // in existing part.
srs5694e4ac11e2009-08-31 10:13:04 -04002022 first = partitions[i].GetLastLBA() + 1;
2023 firstMoved = 1;
srs569455d92612010-03-07 22:16:07 -05002024 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002025 } // for
2026 } while (firstMoved == 1);
2027 if (first > mainHeader.lastUsableLBA)
2028 first = 0;
2029 return (first);
2030} // GPTData::FindFirstAvailable()
2031
2032// Finds the first available sector in the largest block of unallocated
2033// space on the disk. Returns 0 if there are no available blocks left
2034uint64_t GPTData::FindFirstInLargest(void) {
srs5694e35eb1b2009-09-14 00:29:34 -04002035 uint64_t start, firstBlock, lastBlock, segmentSize, selectedSize = 0, selectedSegment = 0;
srs5694e4ac11e2009-08-31 10:13:04 -04002036
2037 start = 0;
2038 do {
2039 firstBlock = FindFirstAvailable(start);
2040 if (firstBlock != UINT32_C(0)) { // something's free...
2041 lastBlock = FindLastInFree(firstBlock);
2042 segmentSize = lastBlock - firstBlock + UINT32_C(1);
2043 if (segmentSize > selectedSize) {
2044 selectedSize = segmentSize;
2045 selectedSegment = firstBlock;
2046 } // if
2047 start = lastBlock + 1;
2048 } // if
2049 } while (firstBlock != 0);
2050 return selectedSegment;
2051} // GPTData::FindFirstInLargest()
2052
srs5694cb76c672010-02-11 22:22:22 -05002053// Find the last available block on the disk.
2054// Returns 0 if there are no available partitions
2055uint64_t GPTData::FindLastAvailable(void) {
srs5694e4ac11e2009-08-31 10:13:04 -04002056 uint64_t last;
2057 uint32_t i;
2058 int lastMoved = 0;
2059
2060 // Start by assuming the last usable LBA is available....
2061 last = mainHeader.lastUsableLBA;
2062
2063 // ...now, similar to algorithm in FindFirstAvailable(), search
2064 // through all partitions, moving last when it's in an existing
2065 // partition. Set the lastMoved flag so we repeat to catch cases
2066 // where partitions are out of logical order.
2067 do {
2068 lastMoved = 0;
srs56940283dae2010-04-28 16:44:34 -04002069 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04002070 if ((last >= partitions[i].GetFirstLBA()) &&
srs569455d92612010-03-07 22:16:07 -05002071 (last <= partitions[i].GetLastLBA())) { // in existing part.
srs5694e4ac11e2009-08-31 10:13:04 -04002072 last = partitions[i].GetFirstLBA() - 1;
2073 lastMoved = 1;
srs569455d92612010-03-07 22:16:07 -05002074 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002075 } // for
2076 } while (lastMoved == 1);
2077 if (last < mainHeader.firstUsableLBA)
2078 last = 0;
2079 return (last);
2080} // GPTData::FindLastAvailable()
2081
2082// Find the last available block in the free space pointed to by start.
2083uint64_t GPTData::FindLastInFree(uint64_t start) {
2084 uint64_t nearestStart;
2085 uint32_t i;
2086
2087 nearestStart = mainHeader.lastUsableLBA;
srs56940283dae2010-04-28 16:44:34 -04002088 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04002089 if ((nearestStart > partitions[i].GetFirstLBA()) &&
srs569455d92612010-03-07 22:16:07 -05002090 (partitions[i].GetFirstLBA() > start)) {
srs5694e4ac11e2009-08-31 10:13:04 -04002091 nearestStart = partitions[i].GetFirstLBA() - 1;
srs569455d92612010-03-07 22:16:07 -05002092 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002093 } // for
2094 return (nearestStart);
2095} // GPTData::FindLastInFree()
2096
2097// Finds the total number of free blocks, the number of segments in which
2098// they reside, and the size of the largest of those segments
srs5694e321d442010-01-29 17:44:04 -05002099uint64_t GPTData::FindFreeBlocks(uint32_t *numSegments, uint64_t *largestSegment) {
srs5694e4ac11e2009-08-31 10:13:04 -04002100 uint64_t start = UINT64_C(0); // starting point for each search
2101 uint64_t totalFound = UINT64_C(0); // running total
2102 uint64_t firstBlock; // first block in a segment
2103 uint64_t lastBlock; // last block in a segment
2104 uint64_t segmentSize; // size of segment in blocks
srs5694e321d442010-01-29 17:44:04 -05002105 uint32_t num = 0;
srs5694e4ac11e2009-08-31 10:13:04 -04002106
2107 *largestSegment = UINT64_C(0);
srs5694c54e9b42010-05-01 21:04:23 -04002108 if (diskSize > 0) {
2109 do {
2110 firstBlock = FindFirstAvailable(start);
2111 if (firstBlock != UINT64_C(0)) { // something's free...
2112 lastBlock = FindLastInFree(firstBlock);
2113 segmentSize = lastBlock - firstBlock + UINT64_C(1);
2114 if (segmentSize > *largestSegment) {
2115 *largestSegment = segmentSize;
2116 } // if
2117 totalFound += segmentSize;
2118 num++;
2119 start = lastBlock + 1;
srs5694e4ac11e2009-08-31 10:13:04 -04002120 } // if
srs5694c54e9b42010-05-01 21:04:23 -04002121 } while (firstBlock != 0);
2122 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002123 *numSegments = num;
2124 return totalFound;
2125} // GPTData::FindFreeBlocks()
2126
srs569455d92612010-03-07 22:16:07 -05002127// Returns 1 if sector is unallocated, 0 if it's allocated to a partition.
2128// If it's allocated, return the partition number to which it's allocated
2129// in partNum, if that variable is non-NULL. (A value of UINT32_MAX is
2130// returned in partNum if the sector is in use by basic GPT data structures.)
2131int GPTData::IsFree(uint64_t sector, uint32_t *partNum) {
srs5694e4ac11e2009-08-31 10:13:04 -04002132 int isFree = 1;
2133 uint32_t i;
2134
srs56940283dae2010-04-28 16:44:34 -04002135 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04002136 if ((sector >= partitions[i].GetFirstLBA()) &&
2137 (sector <= partitions[i].GetLastLBA())) {
2138 isFree = 0;
srs569455d92612010-03-07 22:16:07 -05002139 if (partNum != NULL)
2140 *partNum = i;
srs569408bb0da2010-02-19 17:19:55 -05002141 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002142 } // for
srs5694e35eb1b2009-09-14 00:29:34 -04002143 if ((sector < mainHeader.firstUsableLBA) ||
srs5694e4ac11e2009-08-31 10:13:04 -04002144 (sector > mainHeader.lastUsableLBA)) {
2145 isFree = 0;
srs569455d92612010-03-07 22:16:07 -05002146 if (partNum != NULL)
2147 *partNum = UINT32_MAX;
srs569408bb0da2010-02-19 17:19:55 -05002148 } // if
2149 return (isFree);
srs5694e4ac11e2009-08-31 10:13:04 -04002150} // GPTData::IsFree()
2151
srs5694ba00fed2010-01-12 18:18:36 -05002152// Returns 1 if partNum is unused.
2153int GPTData::IsFreePartNum(uint32_t partNum) {
2154 int retval = 1;
2155
srs56940283dae2010-04-28 16:44:34 -04002156 if ((partNum < numParts) && (partitions != NULL)) {
srs569408bb0da2010-02-19 17:19:55 -05002157 if (partitions[partNum].IsUsed()) {
srs5694ba00fed2010-01-12 18:18:36 -05002158 retval = 0;
2159 } // if partition is in use
2160 } else retval = 0;
2161
2162 return retval;
2163} // GPTData::IsFreePartNum()
2164
srs5694a8582cf2010-03-19 14:21:59 -04002165
2166/***********************************************************
2167 * *
2168 * Change how functions work or return information on them *
2169 * *
2170 ***********************************************************/
2171
2172// Set partition alignment value; partitions will begin on multiples of
2173// the specified value
2174void GPTData::SetAlignment(uint32_t n) {
srs56940873e9d2010-10-07 13:00:45 -04002175 if (n > 0)
2176 sectorAlignment = n;
2177 else
2178 cerr << "Attempt to set partition alignment to 0!\n";
srs5694a8582cf2010-03-19 14:21:59 -04002179} // GPTData::SetAlignment()
2180
2181// Compute sector alignment based on the current partitions (if any). Each
2182// partition's starting LBA is examined, and if it's divisible by a power-of-2
srs56940873e9d2010-10-07 13:00:45 -04002183// value less than or equal to the DEFAULT_ALIGNMENT value (adjusted for the
2184// sector size), but not by the previously-located alignment value, then the
2185// alignment value is adjusted down. If the computed alignment is less than 8
2186// and the disk is bigger than SMALLEST_ADVANCED_FORMAT, resets it to 8. This
2187// is a safety measure for WD Advanced Format and similar drives. If no partitions
2188// are defined, the alignment value is set to DEFAULT_ALIGNMENT (2048) (or an
2189// adjustment of that based on the current sector size). The result is that new
srs56948a4ddfc2010-03-21 19:05:49 -04002190// drives are aligned to 2048-sector multiples but the program won't complain
2191// about other alignments on existing disks unless a smaller-than-8 alignment
srs56940873e9d2010-10-07 13:00:45 -04002192// is used on big disks (as safety for WD Advanced Format drives).
srs5694a8582cf2010-03-19 14:21:59 -04002193// Returns the computed alignment value.
2194uint32_t GPTData::ComputeAlignment(void) {
2195 uint32_t i = 0, found, exponent = 31;
srs5694ab4b0432010-09-25 20:39:52 -04002196 uint32_t align = DEFAULT_ALIGNMENT;
srs5694a8582cf2010-03-19 14:21:59 -04002197
srs56940873e9d2010-10-07 13:00:45 -04002198 if (blockSize > 0)
2199 align = DEFAULT_ALIGNMENT * SECTOR_SIZE / blockSize;
2200 exponent = (uint32_t) log2(align);
srs56940283dae2010-04-28 16:44:34 -04002201 for (i = 0; i < numParts; i++) {
srs5694a8582cf2010-03-19 14:21:59 -04002202 if (partitions[i].IsUsed()) {
2203 found = 0;
2204 while (!found) {
srs56940873e9d2010-10-07 13:00:45 -04002205 align = UINT64_C(1) << exponent;
srs5694a8582cf2010-03-19 14:21:59 -04002206 if ((partitions[i].GetFirstLBA() % align) == 0) {
2207 found = 1;
2208 } else {
2209 exponent--;
2210 } // if/else
2211 } // while
2212 } // if
2213 } // for
srs56940873e9d2010-10-07 13:00:45 -04002214 if ((align < MIN_AF_ALIGNMENT) && (diskSize >= SMALLEST_ADVANCED_FORMAT))
2215 align = MIN_AF_ALIGNMENT;
2216 sectorAlignment = align;
srs5694a8582cf2010-03-19 14:21:59 -04002217 return align;
2218} // GPTData::ComputeAlignment()
2219
srs5694e4ac11e2009-08-31 10:13:04 -04002220/********************************
2221 * *
2222 * Endianness support functions *
2223 * *
2224 ********************************/
2225
srs56942a9f5da2009-08-26 00:48:01 -04002226void GPTData::ReverseHeaderBytes(struct GPTHeader* header) {
srs5694221e0872009-08-29 15:00:31 -04002227 ReverseBytes(&header->signature, 8);
2228 ReverseBytes(&header->revision, 4);
2229 ReverseBytes(&header->headerSize, 4);
2230 ReverseBytes(&header->headerCRC, 4);
2231 ReverseBytes(&header->reserved, 4);
2232 ReverseBytes(&header->currentLBA, 8);
2233 ReverseBytes(&header->backupLBA, 8);
2234 ReverseBytes(&header->firstUsableLBA, 8);
2235 ReverseBytes(&header->lastUsableLBA, 8);
2236 ReverseBytes(&header->partitionEntriesLBA, 8);
2237 ReverseBytes(&header->numParts, 4);
2238 ReverseBytes(&header->sizeOfPartitionEntries, 4);
2239 ReverseBytes(&header->partitionEntriesCRC, 4);
srs569408bb0da2010-02-19 17:19:55 -05002240 ReverseBytes(header->reserved2, GPT_RESERVED);
srs56942a9f5da2009-08-26 00:48:01 -04002241} // GPTData::ReverseHeaderBytes()
2242
srs56940283dae2010-04-28 16:44:34 -04002243// Reverse byte order for all partitions.
srs56942a9f5da2009-08-26 00:48:01 -04002244void GPTData::ReversePartitionBytes() {
2245 uint32_t i;
2246
srs56940283dae2010-04-28 16:44:34 -04002247 for (i = 0; i < numParts; i++) {
srs5694221e0872009-08-29 15:00:31 -04002248 partitions[i].ReversePartBytes();
srs56942a9f5da2009-08-26 00:48:01 -04002249 } // for
2250} // GPTData::ReversePartitionBytes()
2251
srs56949ddc14b2010-08-22 22:44:42 -04002252// Validate partition number
2253bool GPTData::ValidPartNum (const uint32_t partNum) {
2254 if (partNum >= numParts) {
srs56945a081752010-09-24 20:39:41 -04002255 cerr << "Partition number out of range: " << partNum << "\n";
srs56949ddc14b2010-08-22 22:44:42 -04002256 return false;
2257 } // if
2258 return true;
2259} // GPTData::ValidPartNum
2260
srs56945a081752010-09-24 20:39:41 -04002261// Return a single partition for inspection (not modification!) by other
2262// functions.
2263const GPTPart & GPTData::operator[](uint32_t partNum) const {
2264 if (partNum >= numParts) {
2265 cerr << "Partition number out of range: " << partNum << "\n";
2266 partNum = 0;
2267 } // if
2268 return partitions[partNum];
2269} // operator[]
2270
2271// Return (not for modification!) the disk's GUID value
2272const GUIDData & GPTData::GetDiskGUID(void) const {
2273 return mainHeader.diskGUID;
2274} // GPTData::GetDiskGUID()
2275
srs56949ddc14b2010-08-22 22:44:42 -04002276// Manage attributes for a partition, based on commands passed to this function.
2277// (Function is non-interactive.)
2278// Returns 1 if a modification command succeeded, 0 if the command should not have
2279// modified data, and -1 if a modification command failed.
2280int GPTData::ManageAttributes(int partNum, const string & command, const string & bits) {
2281 int retval = 0;
2282 Attributes theAttr;
2283
2284 if (command == "show") {
2285 ShowAttributes(partNum);
2286 } else if (command == "get") {
2287 GetAttribute(partNum, bits);
2288 } else {
2289 theAttr = partitions[partNum].GetAttributes();
2290 if (theAttr.OperateOnAttributes(partNum, command, bits)) {
2291 partitions[partNum].SetAttributes(theAttr.GetAttributes());
2292 retval = 1;
2293 } else {
2294 retval = -1;
2295 } // if/else
2296 } // if/elseif/else
2297
2298 return retval;
2299} // GPTData::ManageAttributes()
2300
2301// Show all attributes for a specified partition....
2302void GPTData::ShowAttributes(const uint32_t partNum) {
srs56940873e9d2010-10-07 13:00:45 -04002303 partitions[partNum].ShowAttributes(partNum);
srs56949ddc14b2010-08-22 22:44:42 -04002304} // GPTData::ShowAttributes
2305
2306// Show whether a single attribute bit is set (terse output)...
2307void GPTData::GetAttribute(const uint32_t partNum, const string& attributeBits) {
srs56940873e9d2010-10-07 13:00:45 -04002308 partitions[partNum].GetAttributes().OperateOnAttributes(partNum, "get", attributeBits);
srs56949ddc14b2010-08-22 22:44:42 -04002309} // GPTData::GetAttribute
2310
2311
srs56942a9f5da2009-08-26 00:48:01 -04002312/******************************************
2313 * *
2314 * Additional non-class support functions *
2315 * *
2316 ******************************************/
2317
srs5694e7b4ff92009-08-18 13:16:10 -04002318// Check to be sure that data type sizes are correct. The basic types (uint*_t) should
2319// never fail these tests, but the struct types may fail depending on compile options.
2320// Specifically, the -fpack-struct option to gcc may be required to ensure proper structure
2321// sizes.
2322int SizesOK(void) {
2323 int allOK = 1;
srs5694e7b4ff92009-08-18 13:16:10 -04002324
2325 if (sizeof(uint8_t) != 1) {
srs5694fed16d02010-01-27 23:03:40 -05002326 cerr << "uint8_t is " << sizeof(uint8_t) << " bytes, should be 1 byte; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002327 allOK = 0;
2328 } // if
2329 if (sizeof(uint16_t) != 2) {
srs5694fed16d02010-01-27 23:03:40 -05002330 cerr << "uint16_t is " << sizeof(uint16_t) << " bytes, should be 2 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002331 allOK = 0;
2332 } // if
2333 if (sizeof(uint32_t) != 4) {
srs5694fed16d02010-01-27 23:03:40 -05002334 cerr << "uint32_t is " << sizeof(uint32_t) << " bytes, should be 4 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002335 allOK = 0;
2336 } // if
2337 if (sizeof(uint64_t) != 8) {
srs5694fed16d02010-01-27 23:03:40 -05002338 cerr << "uint64_t is " << sizeof(uint64_t) << " bytes, should be 8 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002339 allOK = 0;
2340 } // if
2341 if (sizeof(struct MBRRecord) != 16) {
srs5694fed16d02010-01-27 23:03:40 -05002342 cerr << "MBRRecord is " << sizeof(MBRRecord) << " bytes, should be 16 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002343 allOK = 0;
2344 } // if
srs5694978041c2009-09-21 20:51:47 -04002345 if (sizeof(struct TempMBR) != 512) {
srs5694fed16d02010-01-27 23:03:40 -05002346 cerr << "TempMBR is " << sizeof(TempMBR) << " bytes, should be 512 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002347 allOK = 0;
2348 } // if
2349 if (sizeof(struct GPTHeader) != 512) {
srs5694fed16d02010-01-27 23:03:40 -05002350 cerr << "GPTHeader is " << sizeof(GPTHeader) << " bytes, should be 512 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002351 allOK = 0;
2352 } // if
srs5694221e0872009-08-29 15:00:31 -04002353 if (sizeof(GPTPart) != 128) {
srs5694fed16d02010-01-27 23:03:40 -05002354 cerr << "GPTPart is " << sizeof(GPTPart) << " bytes, should be 128 bytes; aborting!\n";
srs5694221e0872009-08-29 15:00:31 -04002355 allOK = 0;
2356 } // if
srs56946699b012010-02-04 00:55:30 -05002357 if (sizeof(GUIDData) != 16) {
2358 cerr << "GUIDData is " << sizeof(GUIDData) << " bytes, should be 16 bytes; aborting!\n";
2359 allOK = 0;
2360 } // if
2361 if (sizeof(PartType) != 16) {
2362 cerr << "PartType is " << sizeof(GUIDData) << " bytes, should be 16 bytes; aborting!\n";
2363 allOK = 0;
2364 } // if
srs5694fed16d02010-01-27 23:03:40 -05002365 // Determine endianness; warn user if running on big-endian (PowerPC, etc.) hardware
srs56940873e9d2010-10-07 13:00:45 -04002366// if (IsLittleEndian() == 0) {
2367// cerr << "\aRunning on big-endian hardware. Big-endian support is new and poorly"
2368// " tested!\n";
2369// } // if
srs5694e7b4ff92009-08-18 13:16:10 -04002370 return (allOK);
2371} // SizesOK()
srs5694e4ac11e2009-08-31 10:13:04 -04002372