blob: 78b1557010a8825fda315571cb8e8aaf9592bce4 [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;
srs56941d1448a2009-12-31 21:20:19 -050061 sectorAlignment = 8; // 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;
srs56941d1448a2009-12-31 21:20:19 -050084 sectorAlignment = 8; // 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 ("
251 << BytesToSI(totalFree * (uint64_t) blockSize) << ") available in "
252 << numSegments << "\nsegments, the largest of which is "
253 << largestSegment << " (" << BytesToSI(largestSegment * (uint64_t) 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
srs56942a9f5da2009-08-26 00:48:01 -0400347 // If MBR bad, check for an Apple disk signature
srs5694e35eb1b2009-09-14 00:29:34 -0400348 if ((protectiveMBR.GetValidity() == invalid) &&
srs5694e4ac11e2009-08-31 10:13:04 -0400349 (((mainHeader.signature << 32) == APM_SIGNATURE1) ||
srs56942a9f5da2009-08-26 00:48:01 -0400350 (mainHeader.signature << 32) == APM_SIGNATURE2)) {
srs5694221e0872009-08-29 15:00:31 -0400351 apmFound = 1; // Will display warning message later
srs56943f2fe992009-11-24 18:28:18 -0500352 } // if
srs5694fed16d02010-01-27 23:03:40 -0500353 cout.fill(' ');
srs56942a9f5da2009-08-26 00:48:01 -0400354
srs5694fed16d02010-01-27 23:03:40 -0500355 return valid;
srs5694e7b4ff92009-08-18 13:16:10 -0400356} // GPTData::CheckHeaderValidity()
357
358// Check the header CRC to see if it's OK...
srs5694cb76c672010-02-11 22:22:22 -0500359// Note: Must be called with header in LITTLE-ENDIAN
360// (x86, x86-64, etc.) byte order.
srs5694e7b4ff92009-08-18 13:16:10 -0400361int GPTData::CheckHeaderCRC(struct GPTHeader* header) {
srs5694978041c2009-09-21 20:51:47 -0400362 uint32_t oldCRC, newCRC, hSize;
srs5694e7b4ff92009-08-18 13:16:10 -0400363
srs56942a9f5da2009-08-26 00:48:01 -0400364 // Back up old header CRC and then blank it, since it must be 0 for
srs5694e7b4ff92009-08-18 13:16:10 -0400365 // computation to be valid
366 oldCRC = header->headerCRC;
367 header->headerCRC = UINT32_C(0);
srs5694978041c2009-09-21 20:51:47 -0400368 hSize = header->headerSize;
369
370 // If big-endian system, reverse byte order
371 if (IsLittleEndian() == 0) {
372 ReverseBytes(&oldCRC, 4);
373 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400374
375 // Initialize CRC functions...
376 chksum_crc32gentab();
377
378 // Compute CRC, restore original, and return result of comparison
379 newCRC = chksum_crc32((unsigned char*) header, HEADER_SIZE);
srs5694978041c2009-09-21 20:51:47 -0400380 header->headerCRC = oldCRC;
srs5694e7b4ff92009-08-18 13:16:10 -0400381 return (oldCRC == newCRC);
382} // GPTData::CheckHeaderCRC()
383
srs56946699b012010-02-04 00:55:30 -0500384// Recompute all the CRCs. Must be called before saving if any changes have
385// been made. Must be called on platform-ordered data (this function reverses
386// byte order and then undoes that reversal.)
srs5694e7b4ff92009-08-18 13:16:10 -0400387void GPTData::RecomputeCRCs(void) {
srs56940283dae2010-04-28 16:44:34 -0400388 uint32_t crc, hSize;
srs56942a9f5da2009-08-26 00:48:01 -0400389 int littleEndian = 1;
srs5694e7b4ff92009-08-18 13:16:10 -0400390
391 // Initialize CRC functions...
392 chksum_crc32gentab();
393
srs56946699b012010-02-04 00:55:30 -0500394 // Save some key data from header before reversing byte order....
srs5694978041c2009-09-21 20:51:47 -0400395 hSize = mainHeader.headerSize;
srs56946699b012010-02-04 00:55:30 -0500396
397 if ((littleEndian = IsLittleEndian()) == 0) {
398 ReversePartitionBytes();
399 ReverseHeaderBytes(&mainHeader);
400 ReverseHeaderBytes(&secondHeader);
401 } // if
srs56942a9f5da2009-08-26 00:48:01 -0400402
srs5694e7b4ff92009-08-18 13:16:10 -0400403 // Compute CRC of partition tables & store in main and secondary headers
srs56940283dae2010-04-28 16:44:34 -0400404 crc = chksum_crc32((unsigned char*) partitions, numParts * GPT_SIZE);
srs5694e7b4ff92009-08-18 13:16:10 -0400405 mainHeader.partitionEntriesCRC = crc;
406 secondHeader.partitionEntriesCRC = crc;
srs56942a9f5da2009-08-26 00:48:01 -0400407 if (littleEndian == 0) {
srs5694221e0872009-08-29 15:00:31 -0400408 ReverseBytes(&mainHeader.partitionEntriesCRC, 4);
409 ReverseBytes(&secondHeader.partitionEntriesCRC, 4);
srs56942a9f5da2009-08-26 00:48:01 -0400410 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400411
412 // Zero out GPT tables' own CRCs (required for correct computation)
413 mainHeader.headerCRC = 0;
414 secondHeader.headerCRC = 0;
415
416 // Compute & store CRCs of main & secondary headers...
srs5694978041c2009-09-21 20:51:47 -0400417 crc = chksum_crc32((unsigned char*) &mainHeader, hSize);
srs56942a9f5da2009-08-26 00:48:01 -0400418 if (littleEndian == 0)
srs5694221e0872009-08-29 15:00:31 -0400419 ReverseBytes(&crc, 4);
srs5694e7b4ff92009-08-18 13:16:10 -0400420 mainHeader.headerCRC = crc;
srs5694978041c2009-09-21 20:51:47 -0400421 crc = chksum_crc32((unsigned char*) &secondHeader, hSize);
srs56942a9f5da2009-08-26 00:48:01 -0400422 if (littleEndian == 0)
srs5694221e0872009-08-29 15:00:31 -0400423 ReverseBytes(&crc, 4);
srs5694e7b4ff92009-08-18 13:16:10 -0400424 secondHeader.headerCRC = crc;
srs56946699b012010-02-04 00:55:30 -0500425
426 if ((littleEndian = IsLittleEndian()) == 0) {
427 ReverseHeaderBytes(&mainHeader);
428 ReverseHeaderBytes(&secondHeader);
429 ReversePartitionBytes();
430 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400431} // GPTData::RecomputeCRCs()
432
srs5694e7b4ff92009-08-18 13:16:10 -0400433// Rebuild the main GPT header, using the secondary header as a model.
434// Typically called when the main header has been found to be corrupt.
435void GPTData::RebuildMainHeader(void) {
436 int i;
437
438 mainHeader.signature = GPT_SIGNATURE;
439 mainHeader.revision = secondHeader.revision;
srs5694978041c2009-09-21 20:51:47 -0400440 mainHeader.headerSize = secondHeader.headerSize;
srs5694e7b4ff92009-08-18 13:16:10 -0400441 mainHeader.headerCRC = UINT32_C(0);
442 mainHeader.reserved = secondHeader.reserved;
443 mainHeader.currentLBA = secondHeader.backupLBA;
444 mainHeader.backupLBA = secondHeader.currentLBA;
445 mainHeader.firstUsableLBA = secondHeader.firstUsableLBA;
446 mainHeader.lastUsableLBA = secondHeader.lastUsableLBA;
srs56946699b012010-02-04 00:55:30 -0500447 mainHeader.diskGUID = secondHeader.diskGUID;
srs5694e7b4ff92009-08-18 13:16:10 -0400448 mainHeader.partitionEntriesLBA = UINT64_C(2);
449 mainHeader.numParts = secondHeader.numParts;
450 mainHeader.sizeOfPartitionEntries = secondHeader.sizeOfPartitionEntries;
451 mainHeader.partitionEntriesCRC = secondHeader.partitionEntriesCRC;
452 for (i = 0 ; i < GPT_RESERVED; i++)
453 mainHeader.reserved2[i] = secondHeader.reserved2[i];
srs5694546a9c72010-01-26 16:00:26 -0500454 mainCrcOk = secondCrcOk;
srs56940283dae2010-04-28 16:44:34 -0400455 SetGPTSize(mainHeader.numParts);
srs5694e7b4ff92009-08-18 13:16:10 -0400456} // GPTData::RebuildMainHeader()
457
458// Rebuild the secondary GPT header, using the main header as a model.
459void GPTData::RebuildSecondHeader(void) {
460 int i;
461
462 secondHeader.signature = GPT_SIGNATURE;
463 secondHeader.revision = mainHeader.revision;
srs5694978041c2009-09-21 20:51:47 -0400464 secondHeader.headerSize = mainHeader.headerSize;
srs5694e7b4ff92009-08-18 13:16:10 -0400465 secondHeader.headerCRC = UINT32_C(0);
466 secondHeader.reserved = mainHeader.reserved;
467 secondHeader.currentLBA = mainHeader.backupLBA;
468 secondHeader.backupLBA = mainHeader.currentLBA;
469 secondHeader.firstUsableLBA = mainHeader.firstUsableLBA;
470 secondHeader.lastUsableLBA = mainHeader.lastUsableLBA;
srs56946699b012010-02-04 00:55:30 -0500471 secondHeader.diskGUID = mainHeader.diskGUID;
srs5694e7b4ff92009-08-18 13:16:10 -0400472 secondHeader.partitionEntriesLBA = secondHeader.lastUsableLBA + UINT64_C(1);
473 secondHeader.numParts = mainHeader.numParts;
474 secondHeader.sizeOfPartitionEntries = mainHeader.sizeOfPartitionEntries;
475 secondHeader.partitionEntriesCRC = mainHeader.partitionEntriesCRC;
476 for (i = 0 ; i < GPT_RESERVED; i++)
477 secondHeader.reserved2[i] = mainHeader.reserved2[i];
srs5694546a9c72010-01-26 16:00:26 -0500478 secondCrcOk = mainCrcOk;
srs56940283dae2010-04-28 16:44:34 -0400479 SetGPTSize(secondHeader.numParts);
srs5694e4ac11e2009-08-31 10:13:04 -0400480} // GPTData::RebuildSecondHeader()
481
482// Search for hybrid MBR entries that have no corresponding GPT partition.
483// Returns number of such mismatches found
484int GPTData::FindHybridMismatches(void) {
srs5694e321d442010-01-29 17:44:04 -0500485 int i, found, numFound = 0;
486 uint32_t j;
srs5694e4ac11e2009-08-31 10:13:04 -0400487 uint64_t mbrFirst, mbrLast;
488
489 for (i = 0; i < 4; i++) {
490 if ((protectiveMBR.GetType(i) != 0xEE) && (protectiveMBR.GetType(i) != 0x00)) {
491 j = 0;
492 found = 0;
493 do {
494 mbrFirst = (uint64_t) protectiveMBR.GetFirstSector(i);
495 mbrLast = mbrFirst + (uint64_t) protectiveMBR.GetLength(i) - UINT64_C(1);
496 if ((partitions[j].GetFirstLBA() == mbrFirst) &&
497 (partitions[j].GetLastLBA() == mbrLast))
498 found = 1;
499 j++;
srs56940283dae2010-04-28 16:44:34 -0400500 } while ((!found) && (j < numParts));
srs5694e4ac11e2009-08-31 10:13:04 -0400501 if (!found) {
502 numFound++;
srs5694fed16d02010-01-27 23:03:40 -0500503 cout << "\nWarning! Mismatched GPT and MBR partition! MBR partition "
504 << i + 1 << ", of type 0x";
505 cout.fill('0');
506 cout.setf(ios::uppercase);
507 cout.width(2);
508 cout << hex << (int) protectiveMBR.GetType(i) << ",\n"
509 << "has no corresponding GPT partition! You may continue, but this condition\n"
510 << "might cause data loss in the future!\a\n" << dec;
511 cout.fill(' ');
srs5694e4ac11e2009-08-31 10:13:04 -0400512 } // if
513 } // if
514 } // for
515 return numFound;
516} // GPTData::FindHybridMismatches
517
518// Find overlapping partitions and warn user about them. Returns number of
519// overlapping partitions.
520int GPTData::FindOverlaps(void) {
srs5694e321d442010-01-29 17:44:04 -0500521 int problems = 0;
522 uint32_t i, j;
srs5694e4ac11e2009-08-31 10:13:04 -0400523
srs56940283dae2010-04-28 16:44:34 -0400524 for (i = 1; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -0400525 for (j = 0; j < i; j++) {
srs56940a697312010-01-28 21:10:52 -0500526 if (partitions[i].DoTheyOverlap(partitions[j])) {
srs5694e4ac11e2009-08-31 10:13:04 -0400527 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500528 cout << "\nProblem: partitions " << i + 1 << " and " << j + 1 << " overlap:\n";
529 cout << " Partition " << i + 1 << ": " << partitions[i].GetFirstLBA()
530 << " to " << partitions[i].GetLastLBA() << "\n";
531 cout << " Partition " << j + 1 << ": " << partitions[j].GetFirstLBA()
532 << " to " << partitions[j].GetLastLBA() << "\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400533 } // if
534 } // for j...
535 } // for i...
536 return problems;
537} // GPTData::FindOverlaps()
538
srs569455d92612010-03-07 22:16:07 -0500539// Find partitions that are insane -- they start after they end or are too
540// big for the disk. (The latter should duplicate detection of overlaps
541// with GPT backup data structures, but better to err on the side of
542// redundant tests than to miss something....)
543int GPTData::FindInsanePartitions(void) {
544 uint32_t i;
545 int problems = 0;
546
srs56940283dae2010-04-28 16:44:34 -0400547 for (i = 0; i < numParts; i++) {
srs569455d92612010-03-07 22:16:07 -0500548 if (partitions[i].GetFirstLBA() > partitions[i].GetLastLBA()) {
549 problems++;
srs56940283dae2010-04-28 16:44:34 -0400550 cout << "\nProblem: partition " << i + 1 << " ends before it begins.\n";
srs569455d92612010-03-07 22:16:07 -0500551 } // if
552 if (partitions[i].GetLastLBA() >= diskSize) {
553 problems++;
srs56940283dae2010-04-28 16:44:34 -0400554 cout << "\nProblem: partition " << i + 1<< " is too big for the disk.\n";
srs569455d92612010-03-07 22:16:07 -0500555 } // if
556 } // for
557 return problems;
558} // GPTData::FindInsanePartitions(void)
559
560
srs5694e4ac11e2009-08-31 10:13:04 -0400561/******************************************************************
562 * *
563 * Begin functions that load data from disk or save data to disk. *
564 * *
565 ******************************************************************/
566
567// Scan for partition data. This function loads the MBR data (regular MBR or
568// protective MBR) and loads BSD disklabel data (which is probably invalid).
569// It also looks for APM data, forces a load of GPT data, and summarizes
570// the results.
srs5694546a9c72010-01-26 16:00:26 -0500571void GPTData::PartitionScan(void) {
srs5694e4ac11e2009-08-31 10:13:04 -0400572 BSDData bsdDisklabel;
srs5694e4ac11e2009-08-31 10:13:04 -0400573
574 // Read the MBR & check for BSD disklabel
srs5694546a9c72010-01-26 16:00:26 -0500575 protectiveMBR.ReadMBRData(&myDisk);
576 bsdDisklabel.ReadBSDData(&myDisk, 0, diskSize - 1);
srs5694e4ac11e2009-08-31 10:13:04 -0400577
578 // Load the GPT data, whether or not it's valid
srs5694546a9c72010-01-26 16:00:26 -0500579 ForceLoadGPTData();
srs5694ba00fed2010-01-12 18:18:36 -0500580
581 if (!beQuiet) {
srs5694fed16d02010-01-27 23:03:40 -0500582 cout << "Partition table scan:\n";
srs5694ba00fed2010-01-12 18:18:36 -0500583 protectiveMBR.ShowState();
584 bsdDisklabel.ShowState();
585 ShowAPMState(); // Show whether there's an Apple Partition Map present
586 ShowGPTState(); // Show GPT status
srs5694fed16d02010-01-27 23:03:40 -0500587 cout << "\n";
srs5694ba00fed2010-01-12 18:18:36 -0500588 } // if
srs5694e4ac11e2009-08-31 10:13:04 -0400589
590 if (apmFound) {
srs5694fed16d02010-01-27 23:03:40 -0500591 cout << "\n*******************************************************************\n"
592 << "This disk appears to contain an Apple-format (APM) partition table!\n";
srs56945d58fe02010-01-03 20:57:08 -0500593 if (!justLooking) {
srs5694fed16d02010-01-27 23:03:40 -0500594 cout << "It will be destroyed if you continue!\n";
srs56945d58fe02010-01-03 20:57:08 -0500595 } // if
srs5694fed16d02010-01-27 23:03:40 -0500596 cout << "*******************************************************************\n\n\a";
srs5694e4ac11e2009-08-31 10:13:04 -0400597 } // if
srs5694e4ac11e2009-08-31 10:13:04 -0400598} // GPTData::PartitionScan()
599
600// Read GPT data from a disk.
srs56940a697312010-01-28 21:10:52 -0500601int GPTData::LoadPartitions(const string & deviceFilename) {
srs569408bb0da2010-02-19 17:19:55 -0500602 BSDData bsdDisklabel;
srs5694e321d442010-01-29 17:44:04 -0500603 int err, allOK = 1;
srs5694fed16d02010-01-27 23:03:40 -0500604 MBRValidity mbrState;
srs5694e4ac11e2009-08-31 10:13:04 -0400605
srs5694546a9c72010-01-26 16:00:26 -0500606 if (myDisk.OpenForRead(deviceFilename)) {
srs569455d92612010-03-07 22:16:07 -0500607 err = myDisk.OpenForWrite(deviceFilename);
608 if ((err == 0) && (!justLooking)) {
609 cout << "\aNOTE: Write test failed with error number " << errno
610 << ". It will be impossible to save\nchanges to this disk's partition table!\n";
611#if defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
612 cout << "You may be able to enable writes by exiting this program, typing\n"
613 << "'sysctl kern.geom.debugflags=16' at a shell prompt, and re-running this\n"
614 << "program.\n";
615#endif
616 cout << "\n";
617 } // if
618 myDisk.Close(); // Close and re-open read-only in case of bugs
619 } else allOK = 0; // if
620
621 if (allOK && myDisk.OpenForRead(deviceFilename)) {
srs5694e4ac11e2009-08-31 10:13:04 -0400622 // store disk information....
srs5694546a9c72010-01-26 16:00:26 -0500623 diskSize = myDisk.DiskSize(&err);
624 blockSize = (uint32_t) myDisk.GetBlockSize();
srs5694fed16d02010-01-27 23:03:40 -0500625 device = deviceFilename;
srs5694546a9c72010-01-26 16:00:26 -0500626 PartitionScan(); // Check for partition types, load GPT, & print summary
srs5694e4ac11e2009-08-31 10:13:04 -0400627
srs5694ba00fed2010-01-12 18:18:36 -0500628 whichWasUsed = UseWhichPartitions();
629 switch (whichWasUsed) {
srs5694e4ac11e2009-08-31 10:13:04 -0400630 case use_mbr:
631 XFormPartitions();
632 break;
633 case use_bsd:
srs5694546a9c72010-01-26 16:00:26 -0500634 bsdDisklabel.ReadBSDData(&myDisk, 0, diskSize - 1);
srs5694e4ac11e2009-08-31 10:13:04 -0400635// bsdDisklabel.DisplayBSDData();
636 ClearGPTData();
637 protectiveMBR.MakeProtectiveMBR(1); // clear boot area (option 1)
srs569408bb0da2010-02-19 17:19:55 -0500638 XFormDisklabel(&bsdDisklabel);
srs5694e4ac11e2009-08-31 10:13:04 -0400639 break;
640 case use_gpt:
srs5694fed16d02010-01-27 23:03:40 -0500641 mbrState = protectiveMBR.GetValidity();
642 if ((mbrState == invalid) || (mbrState == mbr))
643 protectiveMBR.MakeProtectiveMBR();
srs5694e4ac11e2009-08-31 10:13:04 -0400644 break;
645 case use_new:
646 ClearGPTData();
647 protectiveMBR.MakeProtectiveMBR();
648 break;
srs56943c0af382010-01-15 19:19:18 -0500649 case use_abort:
650 allOK = 0;
srs56949ddc14b2010-08-22 22:44:42 -0400651 cerr << "Invalid partition data!\n";
srs56943c0af382010-01-15 19:19:18 -0500652 break;
srs5694e4ac11e2009-08-31 10:13:04 -0400653 } // switch
654
srs569455d92612010-03-07 22:16:07 -0500655 if (allOK)
srs56943c0af382010-01-15 19:19:18 -0500656 CheckGPTSize();
srs569455d92612010-03-07 22:16:07 -0500657 myDisk.Close();
srs5694a8582cf2010-03-19 14:21:59 -0400658 ComputeAlignment();
srs5694e4ac11e2009-08-31 10:13:04 -0400659 } else {
660 allOK = 0;
srs5694e4ac11e2009-08-31 10:13:04 -0400661 } // if/else
662 return (allOK);
663} // GPTData::LoadPartitions()
664
665// Loads the GPT, as much as possible. Returns 1 if this seems to have
666// succeeded, 0 if there are obvious problems....
srs5694546a9c72010-01-26 16:00:26 -0500667int GPTData::ForceLoadGPTData(void) {
srs5694cb76c672010-02-11 22:22:22 -0500668 int allOK, validHeaders, loadedTable = 1;
srs5694e4ac11e2009-08-31 10:13:04 -0400669
srs5694cb76c672010-02-11 22:22:22 -0500670 allOK = LoadHeader(&mainHeader, myDisk, 1, &mainCrcOk);
srs5694e4ac11e2009-08-31 10:13:04 -0400671
srs5694cb76c672010-02-11 22:22:22 -0500672 if (mainCrcOk && (mainHeader.backupLBA < diskSize)) {
673 allOK = LoadHeader(&secondHeader, myDisk, mainHeader.backupLBA, &secondCrcOk) && allOK;
674 } else {
srs569408bb0da2010-02-19 17:19:55 -0500675 allOK = LoadHeader(&secondHeader, myDisk, diskSize - UINT64_C(1), &secondCrcOk) && allOK;
676 if (mainCrcOk && (mainHeader.backupLBA >= diskSize))
srs5694fed16d02010-01-27 23:03:40 -0500677 cout << "Warning! Disk size is smaller than the main header indicates! Loading\n"
678 << "secondary header from the last sector of the disk! You should use 'v' to\n"
679 << "verify disk integrity, and perhaps options on the experts' menu to repair\n"
680 << "the disk.\n";
srs5694cb76c672010-02-11 22:22:22 -0500681 } // if/else
682 if (!allOK)
srs5694e4ac11e2009-08-31 10:13:04 -0400683 state = gpt_invalid;
srs5694e4ac11e2009-08-31 10:13:04 -0400684
685 // Return valid headers code: 0 = both headers bad; 1 = main header
686 // good, backup bad; 2 = backup header good, main header bad;
687 // 3 = both headers good. Note these codes refer to valid GPT
688 // signatures and version numbers; more subtle problems will elude
689 // this check!
690 validHeaders = CheckHeaderValidity();
691
692 // Read partitions (from primary array)
693 if (validHeaders > 0) { // if at least one header is OK....
694 // GPT appears to be valid....
695 state = gpt_valid;
696
697 // We're calling the GPT valid, but there's a possibility that one
698 // of the two headers is corrupt. If so, use the one that seems to
699 // be in better shape to regenerate the bad one
srs5694546a9c72010-01-26 16:00:26 -0500700 if (validHeaders == 1) { // valid main header, invalid backup header
srs5694fed16d02010-01-27 23:03:40 -0500701 cerr << "\aCaution: invalid backup GPT header, but valid main header; regenerating\n"
702 << "backup header from main header.\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400703 RebuildSecondHeader();
srs5694546a9c72010-01-26 16:00:26 -0500704 state = gpt_corrupt;
srs5694e4ac11e2009-08-31 10:13:04 -0400705 secondCrcOk = mainCrcOk; // Since regenerated, use CRC validity of main
srs5694546a9c72010-01-26 16:00:26 -0500706 } else if (validHeaders == 2) { // valid backup header, invalid main header
srs5694fed16d02010-01-27 23:03:40 -0500707 cerr << "\aCaution: invalid main GPT header, but valid backup; regenerating main header\n"
708 << "from backup!\n\n";
srs5694546a9c72010-01-26 16:00:26 -0500709 RebuildMainHeader();
710 state = gpt_corrupt;
711 mainCrcOk = secondCrcOk; // Since copied, use CRC validity of backup
srs5694e4ac11e2009-08-31 10:13:04 -0400712 } // if/else/if
713
srs5694546a9c72010-01-26 16:00:26 -0500714 // Figure out which partition table to load....
715 // Load the main partition table, since either its header's CRC is OK or the
716 // backup header's CRC is not OK....
717 if (mainCrcOk || !secondCrcOk) {
718 if (LoadMainTable() == 0)
719 allOK = 0;
720 } else { // bad main header CRC and backup header CRC is OK
721 state = gpt_corrupt;
722 if (LoadSecondTableAsMain()) {
srs5694cb76c672010-02-11 22:22:22 -0500723 loadedTable = 2;
srs5694fed16d02010-01-27 23:03:40 -0500724 cerr << "\aWarning: Invalid CRC on main header data; loaded backup partition table.\n";
srs5694546a9c72010-01-26 16:00:26 -0500725 } else { // backup table bad, bad main header CRC, but try main table in desperation....
726 if (LoadMainTable() == 0) {
727 allOK = 0;
srs5694cb76c672010-02-11 22:22:22 -0500728 loadedTable = 0;
srs5694fed16d02010-01-27 23:03:40 -0500729 cerr << "\a\aWarning! Unable to load either main or backup partition table!\n";
srs5694546a9c72010-01-26 16:00:26 -0500730 } // if
731 } // if/else (LoadSecondTableAsMain())
732 } // if/else (load partition table)
srs5694e4ac11e2009-08-31 10:13:04 -0400733
srs5694cb76c672010-02-11 22:22:22 -0500734 if (loadedTable == 1)
735 secondPartsCrcOk = CheckTable(&secondHeader);
736 else if (loadedTable == 2)
737 mainPartsCrcOk = CheckTable(&mainHeader);
738 else
739 mainPartsCrcOk = secondPartsCrcOk = 0;
srs5694e4ac11e2009-08-31 10:13:04 -0400740
srs5694546a9c72010-01-26 16:00:26 -0500741 // Problem with main partition table; if backup is OK, use it instead....
742 if (secondPartsCrcOk && secondCrcOk && !mainPartsCrcOk) {
743 state = gpt_corrupt;
744 allOK = allOK && LoadSecondTableAsMain();
srs5694cb76c672010-02-11 22:22:22 -0500745 mainPartsCrcOk = 0; // LoadSecondTableAsMain() resets this, so re-flag as bad
srs5694fed16d02010-01-27 23:03:40 -0500746 cerr << "\aWarning! Main partition table CRC mismatch! Loaded backup "
747 << "partition table\ninstead of main partition table!\n\n";
srs5694cb76c672010-02-11 22:22:22 -0500748 } // if */
srs5694546a9c72010-01-26 16:00:26 -0500749
srs5694e4ac11e2009-08-31 10:13:04 -0400750 // Check for valid CRCs and warn if there are problems
751 if ((mainCrcOk == 0) || (secondCrcOk == 0) || (mainPartsCrcOk == 0) ||
752 (secondPartsCrcOk == 0)) {
srs5694fed16d02010-01-27 23:03:40 -0500753 cerr << "Warning! One or more CRCs don't match. You should repair the disk!\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400754 state = gpt_corrupt;
srs5694ba00fed2010-01-12 18:18:36 -0500755 } // if
srs5694e4ac11e2009-08-31 10:13:04 -0400756 } else {
757 state = gpt_invalid;
758 } // if/else
759 return allOK;
760} // GPTData::ForceLoadGPTData()
761
srs5694247657a2009-11-26 18:36:12 -0500762// Loads the partition table pointed to by the main GPT header. The
srs5694e4ac11e2009-08-31 10:13:04 -0400763// main GPT header in memory MUST be valid for this call to do anything
764// sensible!
srs5694546a9c72010-01-26 16:00:26 -0500765// Returns 1 on success, 0 on failure. CRC errors do NOT count as failure.
srs5694e4ac11e2009-08-31 10:13:04 -0400766int GPTData::LoadMainTable(void) {
srs5694cb76c672010-02-11 22:22:22 -0500767 return LoadPartitionTable(mainHeader, myDisk);
srs5694e4ac11e2009-08-31 10:13:04 -0400768} // GPTData::LoadMainTable()
srs5694e7b4ff92009-08-18 13:16:10 -0400769
770// Load the second (backup) partition table as the primary partition
srs5694546a9c72010-01-26 16:00:26 -0500771// table. Used in repair functions, and when starting up if the main
772// partition table is damaged.
773// Returns 1 on success, 0 on failure. CRC errors do NOT count as failure.
774int GPTData::LoadSecondTableAsMain(void) {
srs5694cb76c672010-02-11 22:22:22 -0500775 return LoadPartitionTable(secondHeader, myDisk);
776} // GPTData::LoadSecondTableAsMain()
srs5694e7b4ff92009-08-18 13:16:10 -0400777
srs5694cb76c672010-02-11 22:22:22 -0500778// Load a single GPT header (main or backup) from the specified disk device and
779// sector. Applies byte-order corrections on big-endian platforms. Sets crcOk
780// value appropriately.
781// Returns 1 on success, 0 on failure. Note that CRC errors do NOT qualify as
782// failure.
783int GPTData::LoadHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector, int *crcOk) {
784 int allOK = 1;
srs56941c6f8b02010-02-21 11:09:20 -0500785 GPTHeader tempHeader;
srs5694cb76c672010-02-11 22:22:22 -0500786
787 disk.Seek(sector);
srs56941c6f8b02010-02-21 11:09:20 -0500788 if (disk.Read(&tempHeader, 512) != 512) {
srs5694cb76c672010-02-11 22:22:22 -0500789 cerr << "Warning! Read error " << errno << "; strange behavior now likely!\n";
790 allOK = 0;
791 } // if
srs56941c6f8b02010-02-21 11:09:20 -0500792 *crcOk = CheckHeaderCRC(&tempHeader);
srs5694cb76c672010-02-11 22:22:22 -0500793
srs56941c6f8b02010-02-21 11:09:20 -0500794 // Reverse byte order, if necessary
srs5694cb76c672010-02-11 22:22:22 -0500795 if (IsLittleEndian() == 0) {
srs569455d92612010-03-07 22:16:07 -0500796 ReverseHeaderBytes(&tempHeader);
srs5694cb76c672010-02-11 22:22:22 -0500797 } // if
srs56941c6f8b02010-02-21 11:09:20 -0500798
srs56940283dae2010-04-28 16:44:34 -0400799 if (allOK && (numParts != tempHeader.numParts) && *crcOk) {
srs56941c6f8b02010-02-21 11:09:20 -0500800 allOK = SetGPTSize(tempHeader.numParts);
srs569455d92612010-03-07 22:16:07 -0500801 }
srs56941c6f8b02010-02-21 11:09:20 -0500802
803 *header = tempHeader;
srs5694cb76c672010-02-11 22:22:22 -0500804 return allOK;
805} // GPTData::LoadHeader
806
807// Load a partition table (either main or secondary) from the specified disk,
808// using header as a reference for what to load. If sector != 0 (the default
809// is 0), loads from the specified sector; otherwise loads from the sector
810// indicated in header.
811// Returns 1 on success, 0 on failure. CRC errors do NOT count as failure.
812int GPTData::LoadPartitionTable(const struct GPTHeader & header, DiskIO & disk, uint64_t sector) {
813 uint32_t sizeOfParts, newCRC;
814 int retval;
815
816 if (disk.OpenForRead()) {
817 if (sector == 0) {
818 retval = disk.Seek(header.partitionEntriesLBA);
819 } else {
820 retval = disk.Seek(sector);
821 } // if/else
srs569455d92612010-03-07 22:16:07 -0500822 if (retval == 1)
823 retval = SetGPTSize(header.numParts);
srs5694546a9c72010-01-26 16:00:26 -0500824 if (retval == 1) {
srs5694cb76c672010-02-11 22:22:22 -0500825 sizeOfParts = header.numParts * header.sizeOfPartitionEntries;
826 if (disk.Read(partitions, sizeOfParts) != (int) sizeOfParts) {
srs5694fed16d02010-01-27 23:03:40 -0500827 cerr << "Warning! Read error " << errno << "! Misbehavior now likely!\n";
srs5694546a9c72010-01-26 16:00:26 -0500828 retval = 0;
srs56945d58fe02010-01-03 20:57:08 -0500829 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400830 newCRC = chksum_crc32((unsigned char*) partitions, sizeOfParts);
srs5694cb76c672010-02-11 22:22:22 -0500831 mainPartsCrcOk = secondPartsCrcOk = (newCRC == header.partitionEntriesCRC);
srs56942a9f5da2009-08-26 00:48:01 -0400832 if (IsLittleEndian() == 0)
833 ReversePartitionBytes();
srs5694cb76c672010-02-11 22:22:22 -0500834 if (!mainPartsCrcOk) {
835 cout << "Caution! After loading partitions, the CRC doesn't check out!\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400836 } // if
837 } else {
srs5694cb76c672010-02-11 22:22:22 -0500838 cerr << "Error! Couldn't seek to partition table!\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400839 } // if/else
840 } else {
srs5694fed16d02010-01-27 23:03:40 -0500841 cerr << "Error! Couldn't open device " << device
srs5694cb76c672010-02-11 22:22:22 -0500842 << " when reading partition table!\n";
srs5694546a9c72010-01-26 16:00:26 -0500843 retval = 0;
srs5694e7b4ff92009-08-18 13:16:10 -0400844 } // if/else
srs5694546a9c72010-01-26 16:00:26 -0500845 return retval;
srs5694cb76c672010-02-11 22:22:22 -0500846} // GPTData::LoadPartitionsTable()
847
848// Check the partition table pointed to by header, but don't keep it
849// around.
850// Returns 1 if the CRC is OK, 0 if not or if there was a read error.
851int GPTData::CheckTable(struct GPTHeader *header) {
852 uint32_t sizeOfParts, newCRC;
853 uint8_t *storage;
854 int newCrcOk = 0;
855
srs56940283dae2010-04-28 16:44:34 -0400856 // Load partition table into temporary storage to check
srs5694cb76c672010-02-11 22:22:22 -0500857 // its CRC and store the results, then discard this temporary
858 // storage, since we don't use it in any but recovery operations
859 if (myDisk.Seek(header->partitionEntriesLBA)) {
srs56940283dae2010-04-28 16:44:34 -0400860 sizeOfParts = header->numParts * header->sizeOfPartitionEntries;
srs5694cb76c672010-02-11 22:22:22 -0500861 storage = new uint8_t[sizeOfParts];
862 if (myDisk.Read(storage, sizeOfParts) != (int) sizeOfParts) {
srs56940283dae2010-04-28 16:44:34 -0400863 cerr << "Warning! Error " << errno << " reading partition table for CRC check!\n";
srs5694cb76c672010-02-11 22:22:22 -0500864 } else {
865 newCRC = chksum_crc32((unsigned char*) storage, sizeOfParts);
866 newCrcOk = (newCRC == header->partitionEntriesCRC);
867 } // if/else
868 delete[] storage;
869 } // if
870 return newCrcOk;
871} // GPTData::CheckTable()
srs5694e7b4ff92009-08-18 13:16:10 -0400872
srs5694e7b4ff92009-08-18 13:16:10 -0400873// Writes GPT (and protective MBR) to disk. Returns 1 on successful
874// write, 0 if there was a problem.
srs5694f9312b02010-07-06 15:39:51 -0400875int GPTData::SaveGPTData(int quiet, string filename) {
srs56946699b012010-02-04 00:55:30 -0500876 int allOK = 1, littleEndian;
srs5694e321d442010-01-29 17:44:04 -0500877 char answer;
srs5694e7b4ff92009-08-18 13:16:10 -0400878
srs5694f9312b02010-07-06 15:39:51 -0400879 if (filename == "")
880 filename = device;
881
srs56946699b012010-02-04 00:55:30 -0500882 littleEndian = IsLittleEndian();
883
srs5694f9312b02010-07-06 15:39:51 -0400884 if (filename == "") {
srs5694fed16d02010-01-27 23:03:40 -0500885 cerr << "Device not defined.\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400886 } // if
887
888 // First do some final sanity checks....
srs56945d58fe02010-01-03 20:57:08 -0500889
890 // This test should only fail on read-only disks....
891 if (justLooking) {
srs5694fed16d02010-01-27 23:03:40 -0500892 cout << "The justLooking flag is set. This probably means you can't write to the disk.\n";
srs56945d58fe02010-01-03 20:57:08 -0500893 allOK = 0;
894 } // if
895
srs5694e7b4ff92009-08-18 13:16:10 -0400896 // Is there enough space to hold the GPT headers and partition tables,
897 // given the partition sizes?
srs5694221e0872009-08-29 15:00:31 -0400898 if (CheckGPTSize() > 0) {
srs5694e7b4ff92009-08-18 13:16:10 -0400899 allOK = 0;
900 } // if
901
902 // Check that disk is really big enough to handle this...
903 if (mainHeader.backupLBA > diskSize) {
srs5694fed16d02010-01-27 23:03:40 -0500904 cerr << "Error! Disk is too small! The 'e' option on the experts' menu might fix the\n"
905 << "problem (or it might not). Aborting!\n(Disk size is "
906 << diskSize << " sectors, needs to be " << mainHeader.backupLBA << " sectors.)\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400907 allOK = 0;
908 } // if
srs5694247657a2009-11-26 18:36:12 -0500909 // Check that second header is properly placed. Warn and ask if this should
910 // be corrected if the test fails....
srs5694ba00fed2010-01-12 18:18:36 -0500911 if ((mainHeader.backupLBA < (diskSize - UINT64_C(1))) && (quiet == 0)) {
srs5694fed16d02010-01-27 23:03:40 -0500912 cout << "Warning! Secondary header is placed too early on the disk! Do you want to\n"
913 << "correct this problem? ";
srs5694247657a2009-11-26 18:36:12 -0500914 if (GetYN() == 'Y') {
915 MoveSecondHeaderToEnd();
srs5694fed16d02010-01-27 23:03:40 -0500916 cout << "Have moved second header and partition table to correct location.\n";
srs5694247657a2009-11-26 18:36:12 -0500917 } else {
srs5694fed16d02010-01-27 23:03:40 -0500918 cout << "Have not corrected the problem. Strange problems may occur in the future!\n";
srs5694247657a2009-11-26 18:36:12 -0500919 } // if correction requested
920 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400921
srs569455d92612010-03-07 22:16:07 -0500922 // Check for overlapping or insane partitions....
923 if ((FindOverlaps() > 0) || (FindInsanePartitions() > 0)) {
srs5694e4ac11e2009-08-31 10:13:04 -0400924 allOK = 0;
srs5694fed16d02010-01-27 23:03:40 -0500925 cerr << "Aborting write operation!\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400926 } // if
927
928 // Check for mismatched MBR and GPT data, but let it pass if found
929 // (function displays warning message)
930 FindHybridMismatches();
srs5694e7b4ff92009-08-18 13:16:10 -0400931
932 RecomputeCRCs();
933
srs5694ba00fed2010-01-12 18:18:36 -0500934 if ((allOK) && (!quiet)) {
srs5694fed16d02010-01-27 23:03:40 -0500935 cout << "\nFinal checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING\n"
936 << "PARTITIONS!!\n\nDo you want to proceed, possibly destroying your data? ";
srs56945d58fe02010-01-03 20:57:08 -0500937 answer = GetYN();
938 if (answer == 'Y') {
srs5694fed16d02010-01-27 23:03:40 -0500939 cout << "OK; writing new GUID partition table (GPT).\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400940 } else {
941 allOK = 0;
942 } // if/else
943 } // if
944
945 // Do it!
946 if (allOK) {
srs5694f9312b02010-07-06 15:39:51 -0400947 if (myDisk.OpenForWrite(filename)) {
srs56948a4ddfc2010-03-21 19:05:49 -0400948 // As per UEFI specs, write the secondary table and GPT first....
srs5694cb76c672010-02-11 22:22:22 -0500949 allOK = SavePartitionTable(myDisk, secondHeader.partitionEntriesLBA);
950 if (!allOK)
951 cerr << "Unable to save backup partition table! Perhaps the 'e' option on the experts'\n"
952 << "menu will resolve this problem.\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400953
954 // Now write the secondary GPT header...
srs56948a4ddfc2010-03-21 19:05:49 -0400955 allOK = allOK && SaveHeader(&secondHeader, myDisk, mainHeader.backupLBA);
956
957 // Now write the main partition tables...
958 allOK = allOK && SavePartitionTable(myDisk, mainHeader.partitionEntriesLBA);
959
960 // Now write the main GPT header...
961 allOK = allOK && SaveHeader(&mainHeader, myDisk, 1);
962
963 // To top it off, write the protective MBR...
964 allOK = allOK && protectiveMBR.WriteMBRData(&myDisk);
srs5694e7b4ff92009-08-18 13:16:10 -0400965
966 // re-read the partition table
967 if (allOK) {
srs5694546a9c72010-01-26 16:00:26 -0500968 myDisk.DiskSync();
srs5694e7b4ff92009-08-18 13:16:10 -0400969 } // if
970
971 if (allOK) { // writes completed OK
srs5694fed16d02010-01-27 23:03:40 -0500972 cout << "The operation has completed successfully.\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400973 } else {
srs5694fed16d02010-01-27 23:03:40 -0500974 cerr << "Warning! An error was reported when writing the partition table! This error\n"
srs56948a4ddfc2010-03-21 19:05:49 -0400975 << "MIGHT be harmless, but you may have trashed the disk!\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400976 } // if/else
srs56948a4ddfc2010-03-21 19:05:49 -0400977
srs5694546a9c72010-01-26 16:00:26 -0500978 myDisk.Close();
srs5694e7b4ff92009-08-18 13:16:10 -0400979 } else {
srs5694f9312b02010-07-06 15:39:51 -0400980 cerr << "Unable to open device " << filename << " for writing! Errno is "
srs5694fed16d02010-01-27 23:03:40 -0500981 << errno << "! Aborting write!\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400982 allOK = 0;
srs5694e7b4ff92009-08-18 13:16:10 -0400983 } // if/else
984 } else {
srs5694fed16d02010-01-27 23:03:40 -0500985 cout << "Aborting write of new partition table.\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400986 } // if
987
988 return (allOK);
989} // GPTData::SaveGPTData()
990
991// Save GPT data to a backup file. This function does much less error
992// checking than SaveGPTData(). It can therefore preserve many types of
993// corruption for later analysis; however, it preserves only the MBR,
994// the main GPT header, the backup GPT header, and the main partition
995// table; it discards the backup partition table, since it should be
996// identical to the main partition table on healthy disks.
srs56940a697312010-01-28 21:10:52 -0500997int GPTData::SaveGPTBackup(const string & filename) {
998 int allOK = 1;
srs5694546a9c72010-01-26 16:00:26 -0500999 DiskIO backupFile;
srs5694e7b4ff92009-08-18 13:16:10 -04001000
srs5694546a9c72010-01-26 16:00:26 -05001001 if (backupFile.OpenForWrite(filename)) {
srs56946699b012010-02-04 00:55:30 -05001002 // Recomputing the CRCs is likely to alter them, which could be bad
1003 // if the intent is to save a potentially bad GPT for later analysis;
1004 // but if we don't do this, we get bogus errors when we load the
1005 // backup. I'm favoring misses over false alarms....
1006 RecomputeCRCs();
1007
srs5694546a9c72010-01-26 16:00:26 -05001008 protectiveMBR.WriteMBRData(&backupFile);
srs5694e7b4ff92009-08-18 13:16:10 -04001009
srs5694cb76c672010-02-11 22:22:22 -05001010 if (allOK) {
srs5694546a9c72010-01-26 16:00:26 -05001011 // MBR write closed disk, so re-open and seek to end....
1012 backupFile.OpenForWrite();
srs5694cb76c672010-02-11 22:22:22 -05001013 allOK = SaveHeader(&mainHeader, backupFile, 1);
1014 } // if (allOK)
srs5694e7b4ff92009-08-18 13:16:10 -04001015
srs5694e7b4ff92009-08-18 13:16:10 -04001016 if (allOK)
srs5694cb76c672010-02-11 22:22:22 -05001017 allOK = SaveHeader(&secondHeader, backupFile, 2);
srs5694e7b4ff92009-08-18 13:16:10 -04001018
srs5694cb76c672010-02-11 22:22:22 -05001019 if (allOK)
1020 allOK = SavePartitionTable(backupFile, 3);
srs5694e7b4ff92009-08-18 13:16:10 -04001021
1022 if (allOK) { // writes completed OK
srs5694fed16d02010-01-27 23:03:40 -05001023 cout << "The operation has completed successfully.\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001024 } else {
srs5694fed16d02010-01-27 23:03:40 -05001025 cerr << "Warning! An error was reported when writing the backup file.\n"
1026 << "It may not be usable!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001027 } // if/else
srs5694546a9c72010-01-26 16:00:26 -05001028 backupFile.Close();
srs5694e7b4ff92009-08-18 13:16:10 -04001029 } else {
srs5694fed16d02010-01-27 23:03:40 -05001030 cerr << "Unable to open file " << filename << " for writing! Aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001031 allOK = 0;
1032 } // if/else
1033 return allOK;
1034} // GPTData::SaveGPTBackup()
1035
srs5694cb76c672010-02-11 22:22:22 -05001036// Write a GPT header (main or backup) to the specified sector. Used by both
1037// the SaveGPTData() and SaveGPTBackup() functions.
1038// Should be passed an architecture-appropriate header (DO NOT call
1039// ReverseHeaderBytes() on the header before calling this function)
1040// Returns 1 on success, 0 on failure
1041int GPTData::SaveHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector) {
1042 int littleEndian, allOK = 1;
1043
1044 littleEndian = IsLittleEndian();
1045 if (!littleEndian)
1046 ReverseHeaderBytes(header);
1047 if (disk.Seek(sector)) {
1048 if (disk.Write(header, 512) == -1)
1049 allOK = 0;
1050 } else allOK = 0; // if (disk.Seek()...)
1051 if (!littleEndian)
1052 ReverseHeaderBytes(header);
1053 return allOK;
1054} // GPTData::SaveHeader()
1055
1056// Save the partitions to the specified sector. Used by both the SaveGPTData()
1057// and SaveGPTBackup() functions.
1058// Should be passed an architecture-appropriate header (DO NOT call
1059// ReverseHeaderBytes() on the header before calling this function)
1060// Returns 1 on success, 0 on failure
1061int GPTData::SavePartitionTable(DiskIO & disk, uint64_t sector) {
1062 int littleEndian, allOK = 1;
1063
1064 littleEndian = IsLittleEndian();
1065 if (disk.Seek(sector)) {
1066 if (!littleEndian)
1067 ReversePartitionBytes();
srs56940283dae2010-04-28 16:44:34 -04001068 if (disk.Write(partitions, mainHeader.sizeOfPartitionEntries * numParts) == -1)
srs5694cb76c672010-02-11 22:22:22 -05001069 allOK = 0;
1070 if (!littleEndian)
1071 ReversePartitionBytes();
1072 } else allOK = 0; // if (myDisk.Seek()...)
1073 return allOK;
1074} // GPTData::SavePartitionTable()
1075
srs5694e7b4ff92009-08-18 13:16:10 -04001076// Load GPT data from a backup file created by SaveGPTBackup(). This function
1077// does minimal error checking. It returns 1 if it completed successfully,
1078// 0 if there was a problem. In the latter case, it creates a new empty
1079// set of partitions.
srs56940a697312010-01-28 21:10:52 -05001080int GPTData::LoadGPTBackup(const string & filename) {
srs5694cb76c672010-02-11 22:22:22 -05001081 int allOK = 1, val, err;
srs56940283dae2010-04-28 16:44:34 -04001082 uint32_t sizeOfEntries;
srs5694cb76c672010-02-11 22:22:22 -05001083 int littleEndian = 1, shortBackup = 0;
srs5694546a9c72010-01-26 16:00:26 -05001084 DiskIO backupFile;
srs5694e7b4ff92009-08-18 13:16:10 -04001085
srs5694546a9c72010-01-26 16:00:26 -05001086 if (backupFile.OpenForRead(filename)) {
srs56942a9f5da2009-08-26 00:48:01 -04001087 if (IsLittleEndian() == 0)
1088 littleEndian = 0;
1089
srs5694e7b4ff92009-08-18 13:16:10 -04001090 // Let the MBRData class load the saved MBR...
srs5694546a9c72010-01-26 16:00:26 -05001091 protectiveMBR.ReadMBRData(&backupFile, 0); // 0 = don't check block size
srs5694e7b4ff92009-08-18 13:16:10 -04001092
srs5694cb76c672010-02-11 22:22:22 -05001093 LoadHeader(&mainHeader, backupFile, 1, &mainCrcOk);
srs5694e7b4ff92009-08-18 13:16:10 -04001094
srs5694cb76c672010-02-11 22:22:22 -05001095 // Check backup file size and rebuild second header if file is right
1096 // size to be direct dd copy of MBR, main header, and main partition
1097 // table; if other size, treat it like a GPT fdisk-generated backup
1098 // file
1099 shortBackup = ((backupFile.DiskSize(&err) * backupFile.GetBlockSize()) ==
1100 (mainHeader.numParts * mainHeader.sizeOfPartitionEntries) + 1024);
1101 if (shortBackup) {
1102 RebuildSecondHeader();
1103 secondCrcOk = mainCrcOk;
1104 } else {
1105 LoadHeader(&secondHeader, backupFile, 2, &secondCrcOk);
1106 } // if/else
srs56942a9f5da2009-08-26 00:48:01 -04001107
srs5694e7b4ff92009-08-18 13:16:10 -04001108 // Return valid headers code: 0 = both headers bad; 1 = main header
1109 // good, backup bad; 2 = backup header good, main header bad;
1110 // 3 = both headers good. Note these codes refer to valid GPT
1111 // signatures and version numbers; more subtle problems will elude
1112 // this check!
1113 if ((val = CheckHeaderValidity()) > 0) {
1114 if (val == 2) { // only backup header seems to be good
srs56940283dae2010-04-28 16:44:34 -04001115 SetGPTSize(secondHeader.numParts);
1116// numParts = secondHeader.numParts;
srs5694e4ac11e2009-08-31 10:13:04 -04001117 sizeOfEntries = secondHeader.sizeOfPartitionEntries;
srs5694e7b4ff92009-08-18 13:16:10 -04001118 } else { // main header is OK
srs56940283dae2010-04-28 16:44:34 -04001119 SetGPTSize(mainHeader.numParts);
1120// numParts = mainHeader.numParts;
srs5694e7b4ff92009-08-18 13:16:10 -04001121 sizeOfEntries = mainHeader.sizeOfPartitionEntries;
1122 } // if/else
1123
srs56940283dae2010-04-28 16:44:34 -04001124// SetGPTSize(numParts);
srs5694e7b4ff92009-08-18 13:16:10 -04001125
srs5694e7b4ff92009-08-18 13:16:10 -04001126 if (secondHeader.currentLBA != diskSize - UINT64_C(1)) {
srs5694fed16d02010-01-27 23:03:40 -05001127 cout << "Warning! Current disk size doesn't match that of the backup!\n"
1128 << "Adjusting sizes to match, but subsequent problems are possible!\n";
srs5694247657a2009-11-26 18:36:12 -05001129 MoveSecondHeaderToEnd();
srs5694e7b4ff92009-08-18 13:16:10 -04001130 } // if
1131
srs5694cb76c672010-02-11 22:22:22 -05001132 if (!LoadPartitionTable(mainHeader, backupFile, (uint64_t) (3 - shortBackup)))
1133 cerr << "Warning! Read error " << errno
1134 << " loading partition table; strange behavior now likely!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001135 } else {
1136 allOK = 0;
1137 } // if/else
srs5694a8582cf2010-03-19 14:21:59 -04001138 // Something went badly wrong, so blank out partitions
1139 if (allOK == 0) {
1140 cerr << "Improper backup file! Clearing all partition data!\n";
1141 ClearGPTData();
1142 protectiveMBR.MakeProtectiveMBR();
1143 } // if
srs5694e7b4ff92009-08-18 13:16:10 -04001144 } else {
1145 allOK = 0;
srs5694fed16d02010-01-27 23:03:40 -05001146 cerr << "Unable to open file " << filename << " for reading! Aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001147 } // if/else
1148
srs5694e7b4ff92009-08-18 13:16:10 -04001149 return allOK;
1150} // GPTData::LoadGPTBackup()
1151
srs569408bb0da2010-02-19 17:19:55 -05001152int GPTData::SaveMBR(void) {
srs569455d92612010-03-07 22:16:07 -05001153 return protectiveMBR.WriteMBRData(&myDisk);
srs569408bb0da2010-02-19 17:19:55 -05001154} // GPTData::SaveMBR()
1155
1156// This function destroys the on-disk GPT structures, but NOT the on-disk
1157// MBR.
1158// Returns 1 if the operation succeeds, 0 if not.
1159int GPTData::DestroyGPT(void) {
1160 int i, sum, tableSize, allOK = 1;
1161 uint8_t blankSector[512];
1162 uint8_t* emptyTable;
1163
1164 for (i = 0; i < 512; i++) {
1165 blankSector[i] = 0;
1166 } // for
1167
1168 if (myDisk.OpenForWrite()) {
1169 if (!myDisk.Seek(mainHeader.currentLBA))
1170 allOK = 0;
1171 if (myDisk.Write(blankSector, 512) != 512) { // blank it out
1172 cerr << "Warning! GPT main header not overwritten! Error is " << errno << "\n";
1173 allOK = 0;
1174 } // if
1175 if (!myDisk.Seek(mainHeader.partitionEntriesLBA))
1176 allOK = 0;
srs56940283dae2010-04-28 16:44:34 -04001177 tableSize = numParts * mainHeader.sizeOfPartitionEntries;
srs569408bb0da2010-02-19 17:19:55 -05001178 emptyTable = new uint8_t[tableSize];
1179 for (i = 0; i < tableSize; i++)
1180 emptyTable[i] = 0;
1181 if (allOK) {
1182 sum = myDisk.Write(emptyTable, tableSize);
1183 if (sum != tableSize) {
1184 cerr << "Warning! GPT main partition table not overwritten! Error is " << errno << "\n";
1185 allOK = 0;
1186 } // if write failed
1187 } // if
1188 if (!myDisk.Seek(secondHeader.partitionEntriesLBA))
1189 allOK = 0;
1190 if (allOK) {
1191 sum = myDisk.Write(emptyTable, tableSize);
1192 if (sum != tableSize) {
1193 cerr << "Warning! GPT backup partition table not overwritten! Error is "
1194 << errno << "\n";
1195 allOK = 0;
1196 } // if wrong size written
1197 } // if
1198 if (!myDisk.Seek(secondHeader.currentLBA))
1199 allOK = 0;
1200 if (allOK) {
1201 if (myDisk.Write(blankSector, 512) != 512) { // blank it out
1202 cerr << "Warning! GPT backup header not overwritten! Error is " << errno << "\n";
1203 allOK = 0;
1204 } // if
1205 } // if
1206 myDisk.DiskSync();
1207 myDisk.Close();
1208 cout << "GPT data structures destroyed! You may now partition the disk using fdisk or\n"
1209 << "other utilities.\n";
1210 delete[] emptyTable;
1211 } else {
1212 cerr << "Problem opening " << device << " for writing! Program will now terminate.\n";
1213 } // if/else (fd != -1)
1214 return (allOK);
1215} // GPTDataTextUI::DestroyGPT()
1216
1217// Wipe MBR data from the disk (zero it out completely)
1218// Returns 1 on success, 0 on failure.
1219int GPTData::DestroyMBR(void) {
1220 int allOK = 1, i;
1221 uint8_t blankSector[512];
1222
1223 for (i = 0; i < 512; i++)
1224 blankSector[i] = 0;
1225
1226 if (myDisk.OpenForWrite()) {
1227 if (myDisk.Seek(0)) {
1228 if (myDisk.Write(blankSector, 512) != 512)
1229 allOK = 0;
1230 } else allOK = 0;
1231 } else allOK = 0;
1232 if (!allOK)
1233 cerr << "Warning! MBR not overwritten! Error is " << errno << "!\n";
1234 return allOK;
1235} // GPTData::DestroyMBR(void)
1236
srs5694e4ac11e2009-08-31 10:13:04 -04001237// Tell user whether Apple Partition Map (APM) was discovered....
1238void GPTData::ShowAPMState(void) {
1239 if (apmFound)
srs5694fed16d02010-01-27 23:03:40 -05001240 cout << " APM: present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001241 else
srs5694fed16d02010-01-27 23:03:40 -05001242 cout << " APM: not present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001243} // GPTData::ShowAPMState()
1244
1245// Tell user about the state of the GPT data....
1246void GPTData::ShowGPTState(void) {
1247 switch (state) {
1248 case gpt_invalid:
srs5694fed16d02010-01-27 23:03:40 -05001249 cout << " GPT: not present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001250 break;
1251 case gpt_valid:
srs5694fed16d02010-01-27 23:03:40 -05001252 cout << " GPT: present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001253 break;
1254 case gpt_corrupt:
srs5694fed16d02010-01-27 23:03:40 -05001255 cout << " GPT: damaged\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001256 break;
1257 default:
srs5694fed16d02010-01-27 23:03:40 -05001258 cout << "\a GPT: unknown -- bug!\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001259 break;
1260 } // switch
1261} // GPTData::ShowGPTState()
1262
1263// Display the basic GPT data
1264void GPTData::DisplayGPTData(void) {
srs5694e321d442010-01-29 17:44:04 -05001265 uint32_t i;
srs5694e4ac11e2009-08-31 10:13:04 -04001266 uint64_t temp, totalFree;
1267
srs5694fed16d02010-01-27 23:03:40 -05001268 cout << "Disk " << device << ": " << diskSize << " sectors, "
1269 << BytesToSI(diskSize * blockSize) << "\n";
1270 cout << "Logical sector size: " << blockSize << " bytes\n";
srs56945a081752010-09-24 20:39:41 -04001271 cout << "Disk identifier (GUID): " << mainHeader.diskGUID << "\n";
srs56940283dae2010-04-28 16:44:34 -04001272 cout << "Partition table holds up to " << numParts << " entries\n";
srs5694fed16d02010-01-27 23:03:40 -05001273 cout << "First usable sector is " << mainHeader.firstUsableLBA
1274 << ", last usable sector is " << mainHeader.lastUsableLBA << "\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001275 totalFree = FindFreeBlocks(&i, &temp);
srs56948a4ddfc2010-03-21 19:05:49 -04001276 cout << "Partitions will be aligned on " << sectorAlignment << "-sector boundaries\n";
srs5694fed16d02010-01-27 23:03:40 -05001277 cout << "Total free space is " << totalFree << " sectors ("
1278 << BytesToSI(totalFree * (uint64_t) blockSize) << ")\n";
1279 cout << "\nNumber Start (sector) End (sector) Size Code Name\n";
srs56940283dae2010-04-28 16:44:34 -04001280 for (i = 0; i < numParts; i++) {
srs5694978041c2009-09-21 20:51:47 -04001281 partitions[i].ShowSummary(i, blockSize);
srs5694e4ac11e2009-08-31 10:13:04 -04001282 } // for
1283} // GPTData::DisplayGPTData()
1284
srs5694e4ac11e2009-08-31 10:13:04 -04001285// Show detailed information on the specified partition
1286void GPTData::ShowPartDetails(uint32_t partNum) {
1287 if (partitions[partNum].GetFirstLBA() != 0) {
1288 partitions[partNum].ShowDetails(blockSize);
1289 } else {
srs5694fed16d02010-01-27 23:03:40 -05001290 cout << "Partition #" << partNum + 1 << " does not exist.";
srs5694e4ac11e2009-08-31 10:13:04 -04001291 } // if
1292} // GPTData::ShowPartDetails()
1293
srs5694e4ac11e2009-08-31 10:13:04 -04001294/**************************************************************************
1295 * *
1296 * Partition table transformation functions (MBR or BSD disklabel to GPT) *
1297 * (some of these functions may require user interaction) *
1298 * *
1299 **************************************************************************/
1300
srs569408bb0da2010-02-19 17:19:55 -05001301// Examines the MBR & GPT data to determine which set of data to use: the
1302// MBR (use_mbr), the GPT (use_gpt), the BSD disklabel (use_bsd), or create
1303// a new set of partitions (use_new). A return value of use_abort indicates
1304// that this function couldn't determine what to do. Overriding functions
1305// in derived classes may ask users questions in such cases.
srs5694e4ac11e2009-08-31 10:13:04 -04001306WhichToUse GPTData::UseWhichPartitions(void) {
1307 WhichToUse which = use_new;
1308 MBRValidity mbrState;
srs5694e4ac11e2009-08-31 10:13:04 -04001309
1310 mbrState = protectiveMBR.GetValidity();
1311
1312 if ((state == gpt_invalid) && ((mbrState == mbr) || (mbrState == hybrid))) {
srs5694fed16d02010-01-27 23:03:40 -05001313 cout << "\n***************************************************************\n"
1314 << "Found invalid GPT and valid MBR; converting MBR to GPT format.\n";
srs56945d58fe02010-01-03 20:57:08 -05001315 if (!justLooking) {
srs56940283dae2010-04-28 16:44:34 -04001316 cout << "\aTHIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by typing 'q' if\n"
srs5694fed16d02010-01-27 23:03:40 -05001317 << "you don't want to convert your MBR partitions to GPT format!\n";
srs56945d58fe02010-01-03 20:57:08 -05001318 } // if
srs5694fed16d02010-01-27 23:03:40 -05001319 cout << "***************************************************************\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001320 which = use_mbr;
1321 } // if
1322
1323 if ((state == gpt_invalid) && bsdFound) {
srs5694fed16d02010-01-27 23:03:40 -05001324 cout << "\n**********************************************************************\n"
1325 << "Found invalid GPT and valid BSD disklabel; converting BSD disklabel\n"
1326 << "to GPT format.";
srs56940a697312010-01-28 21:10:52 -05001327 if ((!justLooking) && (!beQuiet)) {
srs56940283dae2010-04-28 16:44:34 -04001328 cout << "\a THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Your first\n"
srs5694fed16d02010-01-27 23:03:40 -05001329 << "BSD partition will likely be unusable. Exit by typing 'q' if you don't\n"
1330 << "want to convert your BSD partitions to GPT format!";
srs56945d58fe02010-01-03 20:57:08 -05001331 } // if
srs5694fed16d02010-01-27 23:03:40 -05001332 cout << "\n**********************************************************************\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001333 which = use_bsd;
1334 } // if
1335
1336 if ((state == gpt_valid) && (mbrState == gpt)) {
srs5694e4ac11e2009-08-31 10:13:04 -04001337 which = use_gpt;
srs56943c0af382010-01-15 19:19:18 -05001338 if (!beQuiet)
srs5694fed16d02010-01-27 23:03:40 -05001339 cout << "Found valid GPT with protective MBR; using GPT.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001340 } // if
1341 if ((state == gpt_valid) && (mbrState == hybrid)) {
srs5694e4ac11e2009-08-31 10:13:04 -04001342 which = use_gpt;
srs56943c0af382010-01-15 19:19:18 -05001343 if (!beQuiet)
srs5694fed16d02010-01-27 23:03:40 -05001344 cout << "Found valid GPT with hybrid MBR; using GPT.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001345 } // if
1346 if ((state == gpt_valid) && (mbrState == invalid)) {
srs56940a697312010-01-28 21:10:52 -05001347 cout << "\aFound valid GPT with corrupt MBR; using GPT and will write new\n"
srs5694fed16d02010-01-27 23:03:40 -05001348 << "protective MBR on save.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001349 which = use_gpt;
srs5694e4ac11e2009-08-31 10:13:04 -04001350 } // if
1351 if ((state == gpt_valid) && (mbrState == mbr)) {
srs569408bb0da2010-02-19 17:19:55 -05001352 which = use_abort;
srs5694e4ac11e2009-08-31 10:13:04 -04001353 } // if
1354
srs5694e4ac11e2009-08-31 10:13:04 -04001355 if (state == gpt_corrupt) {
srs569408bb0da2010-02-19 17:19:55 -05001356 if (mbrState == gpt) {
1357 cout << "\a\a****************************************************************************\n"
1358 << "Caution: Found protective or hybrid MBR and corrupt GPT. Using GPT, but disk\n"
1359 << "verification and recovery are STRONGLY recommended.\n"
1360 << "****************************************************************************\n";
1361 which = use_gpt;
srs56943c0af382010-01-15 19:19:18 -05001362 } else {
srs569408bb0da2010-02-19 17:19:55 -05001363 which = use_abort;
1364 } // if/else MBR says disk is GPT
1365 } // if GPT corrupt
srs5694e4ac11e2009-08-31 10:13:04 -04001366
1367 if (which == use_new)
srs5694fed16d02010-01-27 23:03:40 -05001368 cout << "Creating new GPT entries.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001369
1370 return which;
1371} // UseWhichPartitions()
1372
srs569408bb0da2010-02-19 17:19:55 -05001373// Convert MBR partition table into GPT form.
1374void GPTData::XFormPartitions(void) {
srs5694e4ac11e2009-08-31 10:13:04 -04001375 int i, numToConvert;
1376 uint8_t origType;
srs5694e4ac11e2009-08-31 10:13:04 -04001377
1378 // Clear out old data & prepare basics....
1379 ClearGPTData();
1380
1381 // Convert the smaller of the # of GPT or MBR partitions
srs56940283dae2010-04-28 16:44:34 -04001382 if (numParts > MAX_MBR_PARTS)
srs5694978041c2009-09-21 20:51:47 -04001383 numToConvert = MAX_MBR_PARTS;
srs5694e4ac11e2009-08-31 10:13:04 -04001384 else
srs56940283dae2010-04-28 16:44:34 -04001385 numToConvert = numParts;
srs5694e4ac11e2009-08-31 10:13:04 -04001386
1387 for (i = 0; i < numToConvert; i++) {
1388 origType = protectiveMBR.GetType(i);
1389 // don't waste CPU time trying to convert extended, hybrid protective, or
1390 // null (non-existent) partitions
srs5694e35eb1b2009-09-14 00:29:34 -04001391 if ((origType != 0x05) && (origType != 0x0f) && (origType != 0x85) &&
srs56946699b012010-02-04 00:55:30 -05001392 (origType != 0x00) && (origType != 0xEE))
srs5694e4ac11e2009-08-31 10:13:04 -04001393 partitions[i] = protectiveMBR.AsGPT(i);
1394 } // for
1395
1396 // Convert MBR into protective MBR
1397 protectiveMBR.MakeProtectiveMBR();
1398
1399 // Record that all original CRCs were OK so as not to raise flags
1400 // when doing a disk verification
1401 mainCrcOk = secondCrcOk = mainPartsCrcOk = secondPartsCrcOk = 1;
srs5694e4ac11e2009-08-31 10:13:04 -04001402} // GPTData::XFormPartitions()
1403
1404// Transforms BSD disklabel on the specified partition (numbered from 0).
srs569408bb0da2010-02-19 17:19:55 -05001405// If an invalid partition number is given, the program does nothing.
srs5694e4ac11e2009-08-31 10:13:04 -04001406// Returns the number of new partitions created.
srs569408bb0da2010-02-19 17:19:55 -05001407int GPTData::XFormDisklabel(uint32_t partNum) {
1408 uint32_t low, high;
srs5694e4ac11e2009-08-31 10:13:04 -04001409 int goOn = 1, numDone = 0;
1410 BSDData disklabel;
1411
srs569408bb0da2010-02-19 17:19:55 -05001412 if (GetPartRange(&low, &high) == 0) {
1413 goOn = 0;
1414 cout << "No partitions!\n";
1415 } // if
1416 if (partNum > high) {
1417 goOn = 0;
1418 cout << "Specified partition is invalid!\n";
1419 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04001420
srs569408bb0da2010-02-19 17:19:55 -05001421 // If all is OK, read the disklabel and convert it.
1422 if (goOn) {
1423 goOn = disklabel.ReadBSDData(&myDisk, partitions[partNum].GetFirstLBA(),
1424 partitions[partNum].GetLastLBA());
1425 if ((goOn) && (disklabel.IsDisklabel())) {
1426 numDone = XFormDisklabel(&disklabel);
1427 if (numDone == 1)
1428 cout << "Converted 1 BSD partition.\n";
1429 else
1430 cout << "Converted " << numDone << " BSD partitions.\n";
1431 } else {
1432 cout << "Unable to convert partitions! Unrecognized BSD disklabel.\n";
1433 } // if/else
1434 } // if
1435 if (numDone > 0) { // converted partitions; delete carrier
1436 partitions[partNum].BlankPartition();
1437 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04001438 return numDone;
srs569455d92612010-03-07 22:16:07 -05001439} // GPTData::XFormDisklabel(uint32_t i)
srs5694e4ac11e2009-08-31 10:13:04 -04001440
1441// Transform the partitions on an already-loaded BSD disklabel...
srs569408bb0da2010-02-19 17:19:55 -05001442int GPTData::XFormDisklabel(BSDData* disklabel) {
1443 int i, partNum = 0, numDone = 0;
srs5694e4ac11e2009-08-31 10:13:04 -04001444
srs569408bb0da2010-02-19 17:19:55 -05001445 if (disklabel->IsDisklabel()) {
srs5694e4ac11e2009-08-31 10:13:04 -04001446 for (i = 0; i < disklabel->GetNumParts(); i++) {
srs569408bb0da2010-02-19 17:19:55 -05001447 partNum = FindFirstFreePart();
1448 if (partNum >= 0) {
1449 partitions[partNum] = disklabel->AsGPT(i);
1450 if (partitions[partNum].IsUsed())
1451 numDone++;
1452 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04001453 } // for
srs569408bb0da2010-02-19 17:19:55 -05001454 if (partNum == -1)
1455 cerr << "Warning! Too many partitions to convert!\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001456 } // if
1457
1458 // Record that all original CRCs were OK so as not to raise flags
1459 // when doing a disk verification
1460 mainCrcOk = secondCrcOk = mainPartsCrcOk = secondPartsCrcOk = 1;
1461
1462 return numDone;
1463} // GPTData::XFormDisklabel(BSDData* disklabel)
1464
srs569408bb0da2010-02-19 17:19:55 -05001465// Add one GPT partition to MBR. Used by PartsToMBR() functions. Created
1466// partition has the active/bootable flag UNset and uses the GPT fdisk
1467// type code divided by 0x0100 as the MBR type code.
1468// Returns 1 if operation was 100% successful, 0 if there were ANY
1469// problems.
srs5694978041c2009-09-21 20:51:47 -04001470int GPTData::OnePartToMBR(uint32_t gptPart, int mbrPart) {
srs569408bb0da2010-02-19 17:19:55 -05001471 int allOK = 1;
srs5694fed16d02010-01-27 23:03:40 -05001472
srs5694978041c2009-09-21 20:51:47 -04001473 if ((mbrPart < 0) || (mbrPart > 3)) {
srs5694fed16d02010-01-27 23:03:40 -05001474 cout << "MBR partition " << mbrPart + 1 << " is out of range; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001475 allOK = 0;
1476 } // if
srs56940283dae2010-04-28 16:44:34 -04001477 if (gptPart >= numParts) {
srs5694fed16d02010-01-27 23:03:40 -05001478 cout << "GPT partition " << gptPart + 1 << " is out of range; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001479 allOK = 0;
1480 } // if
1481 if (allOK && (partitions[gptPart].GetLastLBA() == UINT64_C(0))) {
srs5694fed16d02010-01-27 23:03:40 -05001482 cout << "GPT partition " << gptPart + 1 << " is undefined; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001483 allOK = 0;
1484 } // if
1485 if (allOK && (partitions[gptPart].GetFirstLBA() <= UINT32_MAX) &&
1486 (partitions[gptPart].GetLengthLBA() <= UINT32_MAX)) {
1487 if (partitions[gptPart].GetLastLBA() > UINT32_MAX) {
srs5694fed16d02010-01-27 23:03:40 -05001488 cout << "Caution: Partition end point past 32-bit pointer boundary;"
1489 << " some OSes may\nreact strangely.\n";
srs569408bb0da2010-02-19 17:19:55 -05001490 } // if
srs5694978041c2009-09-21 20:51:47 -04001491 protectiveMBR.MakePart(mbrPart, (uint32_t) partitions[gptPart].GetFirstLBA(),
srs569408bb0da2010-02-19 17:19:55 -05001492 (uint32_t) partitions[gptPart].GetLengthLBA(),
1493 partitions[gptPart].GetHexType() / 256, 0);
srs5694978041c2009-09-21 20:51:47 -04001494 } else { // partition out of range
srs569408bb0da2010-02-19 17:19:55 -05001495 if (allOK) // Display only if "else" triggered by out-of-bounds condition
1496 cout << "Partition " << gptPart + 1 << " begins beyond the 32-bit pointer limit of MBR "
1497 << "partitions, or is\n too big; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001498 allOK = 0;
1499 } // if/else
1500 return allOK;
1501} // GPTData::OnePartToMBR()
1502
srs569455d92612010-03-07 22:16:07 -05001503// Convert partitions to MBR form (primary and logical) and return
1504// the number done. Partitions are specified in a PartNotes variable,
1505// which includes pointers to GPT partition numbers. A partition number
1506// of MBR_EFI_GPT means to place an EFI GPT protective partition in that
1507// location in the table, and MBR_EMPTY means not to create a partition
1508// in that table position. If the partition type entry for a partition
1509// is 0, a default entry is used, based on the GPT partition type code.
srs569408bb0da2010-02-19 17:19:55 -05001510// Returns the number of partitions converted, NOT counting EFI GPT
srs569455d92612010-03-07 22:16:07 -05001511// protective partitions or extended partitions.
1512int GPTData::PartsToMBR(PartNotes & notes) {
1513 int mbrNum = 0, numConverted = 0;
1514 struct PartInfo convInfo;
srs5694978041c2009-09-21 20:51:47 -04001515
srs56949ddc14b2010-08-22 22:44:42 -04001516 protectiveMBR.EmptyMBR(0);
srs569455d92612010-03-07 22:16:07 -05001517 protectiveMBR.SetDiskSize(diskSize);
srs5694327129e2010-09-22 01:07:31 -04001518 notes.MakeItLegal();
srs569455d92612010-03-07 22:16:07 -05001519 notes.Rewind();
1520 while (notes.GetNextInfo(&convInfo) >= 0) {
1521 if ((convInfo.gptPartNum >= 0) && (convInfo.type == PRIMARY)) {
1522 numConverted += OnePartToMBR((uint32_t) convInfo.gptPartNum, mbrNum);
1523 if (convInfo.hexCode != 0)
1524 protectiveMBR.SetPartType(mbrNum, convInfo.hexCode);
1525 if (convInfo.active)
1526 protectiveMBR.SetPartBootable(mbrNum);
1527 mbrNum++;
1528 } // if
srs569461768bc2010-07-04 01:54:00 -04001529 if (convInfo.gptPartNum == MBR_EFI_GPT)
1530 mbrNum++;
1531 } // for
1532 // Now go through and set sizes for MBR_EFI_GPT partitions....
1533 notes.Rewind();
1534 mbrNum = 0;
1535 while (notes.GetNextInfo(&convInfo) >= 0) {
1536 if ((convInfo.gptPartNum >= 0) && (convInfo.type == PRIMARY))
1537 mbrNum++;
srs569455d92612010-03-07 22:16:07 -05001538 if (convInfo.gptPartNum == MBR_EFI_GPT) {
1539 if (protectiveMBR.FindFirstAvailable() == UINT32_C(1)) {
1540 protectiveMBR.MakePart(mbrNum, 1, protectiveMBR.FindLastInFree(1), convInfo.hexCode);
1541 protectiveMBR.SetHybrid();
1542 } else {
1543 protectiveMBR.MakeBiggestPart(mbrNum, convInfo.hexCode);
1544 } // if/else
1545 mbrNum++;
srs569461768bc2010-07-04 01:54:00 -04001546 } // if
1547 } // while
srs569455d92612010-03-07 22:16:07 -05001548 // Now do logical partition(s)...
1549 protectiveMBR.SetDisk(&myDisk);
1550 numConverted += protectiveMBR.CreateLogicals(notes);
srs569408bb0da2010-02-19 17:19:55 -05001551 return numConverted;
1552} // GPTData::PartsToMBR()
1553
srs5694e4ac11e2009-08-31 10:13:04 -04001554
1555/**********************************************************************
1556 * *
1557 * Functions that adjust GPT data structures WITHOUT user interaction *
1558 * (they may display information for the user's benefit, though) *
1559 * *
1560 **********************************************************************/
1561
1562// Resizes GPT to specified number of entries. Creates a new table if
srs5694ba00fed2010-01-12 18:18:36 -05001563// necessary, copies data if it already exists. Returns 1 if all goes
1564// well, 0 if an error is encountered.
srs5694e4ac11e2009-08-31 10:13:04 -04001565int GPTData::SetGPTSize(uint32_t numEntries) {
srs569408bb0da2010-02-19 17:19:55 -05001566 GPTPart* newParts;
1567 GPTPart* trash;
srs5694e4ac11e2009-08-31 10:13:04 -04001568 uint32_t i, high, copyNum;
1569 int allOK = 1;
1570
1571 // First, adjust numEntries upward, if necessary, to get a number
1572 // that fills the allocated sectors
1573 i = blockSize / GPT_SIZE;
1574 if ((numEntries % i) != 0) {
srs5694fed16d02010-01-27 23:03:40 -05001575 cout << "Adjusting GPT size from " << numEntries << " to ";
srs5694e4ac11e2009-08-31 10:13:04 -04001576 numEntries = ((numEntries / i) + 1) * i;
srs5694fed16d02010-01-27 23:03:40 -05001577 cout << numEntries << " to fill the sector\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001578 } // if
1579
srs5694247657a2009-11-26 18:36:12 -05001580 // Do the work only if the # of partitions is changing. Along with being
srs569455d92612010-03-07 22:16:07 -05001581 // efficient, this prevents mucking with the location of the secondary
srs5694247657a2009-11-26 18:36:12 -05001582 // partition table, which causes problems when loading data from a RAID
1583 // array that's been expanded because this function is called when loading
1584 // data.
srs56940283dae2010-04-28 16:44:34 -04001585 if (((numEntries != numParts) || (partitions == NULL)) && (numEntries > 0)) {
srs5694cb76c672010-02-11 22:22:22 -05001586 newParts = new GPTPart [numEntries * sizeof (GPTPart)];
srs5694247657a2009-11-26 18:36:12 -05001587 if (newParts != NULL) {
1588 if (partitions != NULL) { // existing partitions; copy them over
1589 GetPartRange(&i, &high);
1590 if (numEntries < (high + 1)) { // Highest entry too high for new #
srs5694fed16d02010-01-27 23:03:40 -05001591 cout << "The highest-numbered partition is " << high + 1
1592 << ", which is greater than the requested\n"
1593 << "partition table size of " << numEntries
1594 << "; cannot resize. Perhaps sorting will help.\n";
srs5694247657a2009-11-26 18:36:12 -05001595 allOK = 0;
1596 } else { // go ahead with copy
srs56940283dae2010-04-28 16:44:34 -04001597 if (numEntries < numParts)
srs5694247657a2009-11-26 18:36:12 -05001598 copyNum = numEntries;
1599 else
srs56940283dae2010-04-28 16:44:34 -04001600 copyNum = numParts;
srs5694247657a2009-11-26 18:36:12 -05001601 for (i = 0; i < copyNum; i++) {
1602 newParts[i] = partitions[i];
1603 } // for
1604 trash = partitions;
1605 partitions = newParts;
srs5694cb76c672010-02-11 22:22:22 -05001606 delete[] trash;
srs5694247657a2009-11-26 18:36:12 -05001607 } // if
1608 } else { // No existing partition table; just create it
srs5694e4ac11e2009-08-31 10:13:04 -04001609 partitions = newParts;
srs5694247657a2009-11-26 18:36:12 -05001610 } // if/else existing partitions
srs56940283dae2010-04-28 16:44:34 -04001611 numParts = numEntries;
srs5694247657a2009-11-26 18:36:12 -05001612 mainHeader.firstUsableLBA = ((numEntries * GPT_SIZE) / blockSize) + 2 ;
1613 secondHeader.firstUsableLBA = mainHeader.firstUsableLBA;
1614 MoveSecondHeaderToEnd();
1615 if (diskSize > 0)
1616 CheckGPTSize();
1617 } else { // Bad memory allocation
srs5694fed16d02010-01-27 23:03:40 -05001618 cerr << "Error allocating memory for partition table!\n";
srs5694247657a2009-11-26 18:36:12 -05001619 allOK = 0;
1620 } // if/else
srs5694e4ac11e2009-08-31 10:13:04 -04001621 } // if/else
srs56940283dae2010-04-28 16:44:34 -04001622 mainHeader.numParts = numParts;
1623 secondHeader.numParts = numParts;
srs5694e4ac11e2009-08-31 10:13:04 -04001624 return (allOK);
1625} // GPTData::SetGPTSize()
1626
1627// Blank the partition array
1628void GPTData::BlankPartitions(void) {
1629 uint32_t i;
1630
srs56940283dae2010-04-28 16:44:34 -04001631 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04001632 partitions[i].BlankPartition();
1633 } // for
1634} // GPTData::BlankPartitions()
1635
srs5694ba00fed2010-01-12 18:18:36 -05001636// Delete a partition by number. Returns 1 if successful,
1637// 0 if there was a problem. Returns 1 if partition was in
1638// range, 0 if it was out of range.
1639int GPTData::DeletePartition(uint32_t partNum) {
1640 uint64_t startSector, length;
srs56940283dae2010-04-28 16:44:34 -04001641 uint32_t low, high, numUsedParts, retval = 1;;
srs5694ba00fed2010-01-12 18:18:36 -05001642
srs56940283dae2010-04-28 16:44:34 -04001643 numUsedParts = GetPartRange(&low, &high);
1644 if ((numUsedParts > 0) && (partNum >= low) && (partNum <= high)) {
srs5694ba00fed2010-01-12 18:18:36 -05001645 // In case there's a protective MBR, look for & delete matching
1646 // MBR partition....
1647 startSector = partitions[partNum].GetFirstLBA();
1648 length = partitions[partNum].GetLengthLBA();
1649 protectiveMBR.DeleteByLocation(startSector, length);
1650
1651 // Now delete the GPT partition
1652 partitions[partNum].BlankPartition();
1653 } else {
srs5694fed16d02010-01-27 23:03:40 -05001654 cerr << "Partition number " << partNum + 1 << " out of range!\n";
srs5694ba00fed2010-01-12 18:18:36 -05001655 retval = 0;
1656 } // if/else
1657 return retval;
1658} // GPTData::DeletePartition(uint32_t partNum)
1659
srs569408bb0da2010-02-19 17:19:55 -05001660// Non-interactively create a partition.
1661// Returns 1 if the operation was successful, 0 if a problem was discovered.
srs5694e321d442010-01-29 17:44:04 -05001662uint32_t GPTData::CreatePartition(uint32_t partNum, uint64_t startSector, uint64_t endSector) {
srs5694ba00fed2010-01-12 18:18:36 -05001663 int retval = 1; // assume there'll be no problems
srs56945a081752010-09-24 20:39:41 -04001664 uint64_t origSector = startSector;
srs5694ba00fed2010-01-12 18:18:36 -05001665
1666 if (IsFreePartNum(partNum)) {
srs56945a081752010-09-24 20:39:41 -04001667 if (Align(&startSector)) {
1668 cout << "Information: Moved requested sector from " << origSector << " to "
1669 << startSector << " in\norder to align on " << sectorAlignment
1670 << "-sector boundaries.\n";
1671 } // if
srs5694ba00fed2010-01-12 18:18:36 -05001672 if (IsFree(startSector) && (startSector <= endSector)) {
1673 if (FindLastInFree(startSector) >= endSector) {
1674 partitions[partNum].SetFirstLBA(startSector);
1675 partitions[partNum].SetLastLBA(endSector);
1676 partitions[partNum].SetType(0x0700);
srs56946699b012010-02-04 00:55:30 -05001677 partitions[partNum].RandomizeUniqueGUID();
srs5694ba00fed2010-01-12 18:18:36 -05001678 } else retval = 0; // if free space until endSector
1679 } else retval = 0; // if startSector is free
1680 } else retval = 0; // if legal partition number
1681 return retval;
1682} // GPTData::CreatePartition(partNum, startSector, endSector)
1683
srs5694e4ac11e2009-08-31 10:13:04 -04001684// Sort the GPT entries, eliminating gaps and making for a logical
1685// ordering. Relies on QuickSortGPT() for the bulk of the work
1686void GPTData::SortGPT(void) {
srs5694546a9c72010-01-26 16:00:26 -05001687 uint32_t i, numFound, firstPart, lastPart;
srs5694e4ac11e2009-08-31 10:13:04 -04001688
1689 // First, find the last partition with data, so as not to
1690 // spend needless time sorting empty entries....
srs5694546a9c72010-01-26 16:00:26 -05001691 numFound = GetPartRange(&firstPart, &lastPart);
srs5694e4ac11e2009-08-31 10:13:04 -04001692
1693 // Now swap empties with the last partitions, to simplify the logic
1694 // in the Quicksort function....
1695 i = 0;
1696 while (i < lastPart) {
1697 if (partitions[i].GetFirstLBA() == 0) {
srs569408bb0da2010-02-19 17:19:55 -05001698 SwapPartitions(i, lastPart);
srs5694546a9c72010-01-26 16:00:26 -05001699 do {
1700 lastPart--;
1701 } while ((lastPart > 0) && (partitions[lastPart].GetFirstLBA() == 0));
srs5694e4ac11e2009-08-31 10:13:04 -04001702 } // if
1703 i++;
1704 } // while
1705
srs5694546a9c72010-01-26 16:00:26 -05001706 // If there are more empties than partitions in the range from 0 to lastPart,
1707 // the above leaves lastPart set too high, so we've got to adjust it to
1708 // prevent empties from migrating to the top of the list....
1709 GetPartRange(&firstPart, &lastPart);
1710
srs5694e4ac11e2009-08-31 10:13:04 -04001711 // Now call the recursive quick sort routine to do the real work....
srs569408bb0da2010-02-19 17:19:55 -05001712 QuickSortGPT(0, lastPart);
srs5694e4ac11e2009-08-31 10:13:04 -04001713} // GPTData::SortGPT()
1714
srs569408bb0da2010-02-19 17:19:55 -05001715// Recursive quick sort algorithm for GPT partitions. Note that if there
1716// are any empties in the specified range, they'll be sorted to the
1717// start, resulting in a sorted set of partitions that begins with
1718// partition 2, 3, or higher.
1719void GPTData::QuickSortGPT(int start, int finish) {
1720 uint64_t starterValue; // starting location of median partition
1721 int left, right;
1722
1723 left = start;
1724 right = finish;
1725 starterValue = partitions[(start + finish) / 2].GetFirstLBA();
1726 do {
1727 while (partitions[left].GetFirstLBA() < starterValue)
1728 left++;
1729 while (partitions[right].GetFirstLBA() > starterValue)
1730 right--;
1731 if (left <= right)
1732 SwapPartitions(left++, right--);
1733 } while (left <= right);
1734 if (start < right) QuickSortGPT(start, right);
1735 if (finish > left) QuickSortGPT(left, finish);
1736} // GPTData::QuickSortGPT()
1737
1738// Swap the contents of two partitions.
1739// Returns 1 if successful, 0 if either partition is out of range
1740// (that is, not a legal number; either or both can be empty).
1741// Note that if partNum1 = partNum2 and this number is in range,
1742// it will be considered successful.
1743int GPTData::SwapPartitions(uint32_t partNum1, uint32_t partNum2) {
1744 GPTPart temp;
1745 int allOK = 1;
1746
srs56940283dae2010-04-28 16:44:34 -04001747 if ((partNum1 < numParts) && (partNum2 < numParts)) {
srs569408bb0da2010-02-19 17:19:55 -05001748 if (partNum1 != partNum2) {
1749 temp = partitions[partNum1];
1750 partitions[partNum1] = partitions[partNum2];
1751 partitions[partNum2] = temp;
1752 } // if
1753 } else allOK = 0; // partition numbers are valid
1754 return allOK;
1755} // GPTData::SwapPartitions()
1756
srs5694e4ac11e2009-08-31 10:13:04 -04001757// Set up data structures for entirely new set of partitions on the
1758// specified device. Returns 1 if OK, 0 if there were problems.
srs5694e35eb1b2009-09-14 00:29:34 -04001759// Note that this function does NOT clear the protectiveMBR data
1760// structure, since it may hold the original MBR partitions if the
1761// program was launched on an MBR disk, and those may need to be
1762// converted to GPT format.
srs5694e4ac11e2009-08-31 10:13:04 -04001763int GPTData::ClearGPTData(void) {
srs5694e35eb1b2009-09-14 00:29:34 -04001764 int goOn = 1, i;
srs5694e4ac11e2009-08-31 10:13:04 -04001765
1766 // Set up the partition table....
srs5694fed16d02010-01-27 23:03:40 -05001767 if (partitions != NULL)
srs5694cb76c672010-02-11 22:22:22 -05001768 delete[] partitions;
srs5694e4ac11e2009-08-31 10:13:04 -04001769 partitions = NULL;
1770 SetGPTSize(NUM_GPT_ENTRIES);
1771
1772 // Now initialize a bunch of stuff that's static....
1773 mainHeader.signature = GPT_SIGNATURE;
1774 mainHeader.revision = 0x00010000;
srs5694978041c2009-09-21 20:51:47 -04001775 mainHeader.headerSize = HEADER_SIZE;
srs5694e4ac11e2009-08-31 10:13:04 -04001776 mainHeader.reserved = 0;
1777 mainHeader.currentLBA = UINT64_C(1);
1778 mainHeader.partitionEntriesLBA = (uint64_t) 2;
1779 mainHeader.sizeOfPartitionEntries = GPT_SIZE;
1780 for (i = 0; i < GPT_RESERVED; i++) {
1781 mainHeader.reserved2[i] = '\0';
1782 } // for
srs56948a4ddfc2010-03-21 19:05:49 -04001783 sectorAlignment = DEFAULT_ALIGNMENT;
srs5694e4ac11e2009-08-31 10:13:04 -04001784
1785 // Now some semi-static items (computed based on end of disk)
1786 mainHeader.backupLBA = diskSize - UINT64_C(1);
1787 mainHeader.lastUsableLBA = diskSize - mainHeader.firstUsableLBA;
1788
1789 // Set a unique GUID for the disk, based on random numbers
srs56946699b012010-02-04 00:55:30 -05001790 mainHeader.diskGUID.Randomize();
srs5694e4ac11e2009-08-31 10:13:04 -04001791
1792 // Copy main header to backup header
1793 RebuildSecondHeader();
1794
1795 // Blank out the partitions array....
1796 BlankPartitions();
1797
1798 // Flag all CRCs as being OK....
1799 mainCrcOk = 1;
1800 secondCrcOk = 1;
1801 mainPartsCrcOk = 1;
1802 secondPartsCrcOk = 1;
1803
1804 return (goOn);
1805} // GPTData::ClearGPTData()
1806
srs5694247657a2009-11-26 18:36:12 -05001807// Set the location of the second GPT header data to the end of the disk.
1808// Used internally and called by the 'e' option on the recovery &
1809// transformation menu, to help users of RAID arrays who add disk space
1810// to their arrays.
1811void GPTData::MoveSecondHeaderToEnd() {
srs56948bb78762009-11-24 15:43:49 -05001812 mainHeader.backupLBA = secondHeader.currentLBA = diskSize - UINT64_C(1);
1813 mainHeader.lastUsableLBA = secondHeader.lastUsableLBA = diskSize - mainHeader.firstUsableLBA;
1814 secondHeader.partitionEntriesLBA = secondHeader.lastUsableLBA + UINT64_C(1);
1815} // GPTData::FixSecondHeaderLocation()
1816
srs56940a697312010-01-28 21:10:52 -05001817int GPTData::SetName(uint32_t partNum, const string & theName) {
srs5694ba00fed2010-01-12 18:18:36 -05001818 int retval = 1;
srs5694fed16d02010-01-27 23:03:40 -05001819
1820 if (!IsFreePartNum(partNum)) {
1821 partitions[partNum].SetName(theName);
1822 } else retval = 0;
srs5694ba00fed2010-01-12 18:18:36 -05001823
1824 return retval;
srs5694e4ac11e2009-08-31 10:13:04 -04001825} // GPTData::SetName
1826
1827// Set the disk GUID to the specified value. Note that the header CRCs must
1828// be recomputed after calling this function.
1829void GPTData::SetDiskGUID(GUIDData newGUID) {
1830 mainHeader.diskGUID = newGUID;
1831 secondHeader.diskGUID = newGUID;
1832} // SetDiskGUID()
1833
1834// Set the unique GUID of the specified partition. Returns 1 on
1835// successful completion, 0 if there were problems (invalid
1836// partition number).
1837int GPTData::SetPartitionGUID(uint32_t pn, GUIDData theGUID) {
1838 int retval = 0;
1839
srs56940283dae2010-04-28 16:44:34 -04001840 if (pn < numParts) {
srs5694e4ac11e2009-08-31 10:13:04 -04001841 if (partitions[pn].GetFirstLBA() != UINT64_C(0)) {
1842 partitions[pn].SetUniqueGUID(theGUID);
1843 retval = 1;
1844 } // if
1845 } // if
1846 return retval;
1847} // GPTData::SetPartitionGUID()
1848
srs56949ba54212010-05-18 23:24:02 -04001849// Set new random GUIDs for the disk and all partitions. Intended to be used
1850// after disk cloning or similar operations that don't randomize the GUIDs.
1851void GPTData::RandomizeGUIDs(void) {
1852 uint32_t i;
1853
1854 mainHeader.diskGUID.Randomize();
1855 secondHeader.diskGUID = mainHeader.diskGUID;
1856 for (i = 0; i < numParts; i++)
1857 if (partitions[i].IsUsed())
1858 partitions[i].RandomizeUniqueGUID();
1859} // GPTData::RandomizeGUIDs()
1860
srs5694ba00fed2010-01-12 18:18:36 -05001861// Change partition type code non-interactively. Returns 1 if
1862// successful, 0 if not....
srs5694327129e2010-09-22 01:07:31 -04001863int GPTData::ChangePartType(uint32_t partNum, PartType theGUID) {
1864 int retval = 1;
1865
1866 if (!IsFreePartNum(partNum)) {
1867 partitions[partNum].SetType(theGUID);
1868 } else retval = 0;
1869 return retval;
1870} // GPTData::ChangePartType()
1871
srs56949ba54212010-05-18 23:24:02 -04001872// Recompute the CHS values of all the MBR partitions. Used to reset
1873// CHS values that some BIOSes require, despite the fact that the
1874// resulting CHS values violate the GPT standard.
1875void GPTData::RecomputeCHS(void) {
1876 int i;
1877
1878 for (i = 0; i < 4; i++)
1879 protectiveMBR.RecomputeCHS(i);
1880} // GPTData::RecomputeCHS()
1881
srs56941d1448a2009-12-31 21:20:19 -05001882// Adjust sector number so that it falls on a sector boundary that's a
1883// multiple of sectorAlignment. This is done to improve the performance
1884// of Western Digital Advanced Format disks and disks with similar
1885// technology from other companies, which use 4096-byte sectors
1886// internally although they translate to 512-byte sectors for the
1887// benefit of the OS. If partitions aren't properly aligned on these
1888// disks, some filesystem data structures can span multiple physical
1889// sectors, degrading performance. This function should be called
1890// only on the FIRST sector of the partition, not the last!
1891// This function returns 1 if the alignment was altered, 0 if it
1892// was unchanged.
1893int GPTData::Align(uint64_t* sector) {
1894 int retval = 0, sectorOK = 0;
1895 uint64_t earlier, later, testSector, original;
1896
1897 if ((*sector % sectorAlignment) != 0) {
1898 original = *sector;
srs56941d1448a2009-12-31 21:20:19 -05001899 earlier = (*sector / sectorAlignment) * sectorAlignment;
1900 later = earlier + (uint64_t) sectorAlignment;
1901
1902 // Check to see that every sector between the earlier one and the
1903 // requested one is clear, and that it's not too early....
1904 if (earlier >= mainHeader.firstUsableLBA) {
srs56941d1448a2009-12-31 21:20:19 -05001905 sectorOK = 1;
1906 testSector = earlier;
1907 do {
1908 sectorOK = IsFree(testSector++);
1909 } while ((sectorOK == 1) && (testSector < *sector));
1910 if (sectorOK == 1) {
1911 *sector = earlier;
srs56945a081752010-09-24 20:39:41 -04001912 retval = 1;
srs56941d1448a2009-12-31 21:20:19 -05001913 } // if
1914 } // if firstUsableLBA check
1915
1916 // If couldn't move the sector earlier, try to move it later instead....
1917 if ((sectorOK != 1) && (later <= mainHeader.lastUsableLBA)) {
1918 sectorOK = 1;
1919 testSector = later;
1920 do {
1921 sectorOK = IsFree(testSector--);
1922 } while ((sectorOK == 1) && (testSector > *sector));
1923 if (sectorOK == 1) {
1924 *sector = later;
srs56945a081752010-09-24 20:39:41 -04001925 retval = 1;
srs56941d1448a2009-12-31 21:20:19 -05001926 } // if
1927 } // if
srs56941d1448a2009-12-31 21:20:19 -05001928 } // if
1929 return retval;
1930} // GPTData::Align()
1931
srs5694e4ac11e2009-08-31 10:13:04 -04001932/********************************************************
1933 * *
1934 * Functions that return data about GPT data structures *
1935 * (most of these are inline in gpt.h) *
1936 * *
1937 ********************************************************/
1938
1939// Find the low and high used partition numbers (numbered from 0).
1940// Return value is the number of partitions found. Note that the
1941// *low and *high values are both set to 0 when no partitions
1942// are found, as well as when a single partition in the first
1943// position exists. Thus, the return value is the only way to
1944// tell when no partitions exist.
1945int GPTData::GetPartRange(uint32_t *low, uint32_t *high) {
1946 uint32_t i;
1947 int numFound = 0;
1948
srs56940283dae2010-04-28 16:44:34 -04001949 *low = numParts + 1; // code for "not found"
srs5694e4ac11e2009-08-31 10:13:04 -04001950 *high = 0;
srs56940283dae2010-04-28 16:44:34 -04001951 if (numParts > 0) { // only try if partition table exists...
1952 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04001953 if (partitions[i].GetFirstLBA() != UINT64_C(0)) { // it exists
1954 *high = i; // since we're counting up, set the high value
srs569408bb0da2010-02-19 17:19:55 -05001955 // Set the low value only if it's not yet found...
srs56940283dae2010-04-28 16:44:34 -04001956 if (*low == (numParts + 1)) *low = i;
srs569408bb0da2010-02-19 17:19:55 -05001957 numFound++;
srs5694e4ac11e2009-08-31 10:13:04 -04001958 } // if
1959 } // for
1960 } // if
1961
1962 // Above will leave *low pointing to its "not found" value if no partitions
1963 // are defined, so reset to 0 if this is the case....
srs56940283dae2010-04-28 16:44:34 -04001964 if (*low == (numParts + 1))
srs5694e4ac11e2009-08-31 10:13:04 -04001965 *low = 0;
1966 return numFound;
1967} // GPTData::GetPartRange()
1968
srs569408bb0da2010-02-19 17:19:55 -05001969// Returns the value of the first free partition, or -1 if none is
1970// unused.
1971int GPTData::FindFirstFreePart(void) {
1972 int i = 0;
1973
1974 if (partitions != NULL) {
srs56940283dae2010-04-28 16:44:34 -04001975 while ((partitions[i].IsUsed()) && (i < (int) numParts))
srs569408bb0da2010-02-19 17:19:55 -05001976 i++;
srs56940283dae2010-04-28 16:44:34 -04001977 if (i >= (int) numParts)
srs569408bb0da2010-02-19 17:19:55 -05001978 i = -1;
1979 } else i = -1;
1980 return i;
1981} // GPTData::FindFirstFreePart()
1982
srs5694978041c2009-09-21 20:51:47 -04001983// Returns the number of defined partitions.
1984uint32_t GPTData::CountParts(void) {
srs5694e321d442010-01-29 17:44:04 -05001985 uint32_t i, counted = 0;
srs5694978041c2009-09-21 20:51:47 -04001986
srs56940283dae2010-04-28 16:44:34 -04001987 for (i = 0; i < numParts; i++) {
srs569408bb0da2010-02-19 17:19:55 -05001988 if (partitions[i].IsUsed())
srs5694978041c2009-09-21 20:51:47 -04001989 counted++;
1990 } // for
1991 return counted;
1992} // GPTData::CountParts()
1993
srs5694e4ac11e2009-08-31 10:13:04 -04001994/****************************************************
1995 * *
1996 * Functions that return data about disk free space *
1997 * *
1998 ****************************************************/
1999
2000// Find the first available block after the starting point; returns 0 if
2001// there are no available blocks left
2002uint64_t GPTData::FindFirstAvailable(uint64_t start) {
2003 uint64_t first;
2004 uint32_t i;
2005 int firstMoved = 0;
2006
2007 // Begin from the specified starting point or from the first usable
2008 // LBA, whichever is greater...
2009 if (start < mainHeader.firstUsableLBA)
2010 first = mainHeader.firstUsableLBA;
2011 else
2012 first = start;
2013
2014 // ...now search through all partitions; if first is within an
2015 // existing partition, move it to the next sector after that
2016 // partition and repeat. If first was moved, set firstMoved
2017 // flag; repeat until firstMoved is not set, so as to catch
2018 // cases where partitions are out of sequential order....
2019 do {
2020 firstMoved = 0;
srs56940283dae2010-04-28 16:44:34 -04002021 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04002022 if ((first >= partitions[i].GetFirstLBA()) &&
srs569455d92612010-03-07 22:16:07 -05002023 (first <= partitions[i].GetLastLBA())) { // in existing part.
srs5694e4ac11e2009-08-31 10:13:04 -04002024 first = partitions[i].GetLastLBA() + 1;
2025 firstMoved = 1;
srs569455d92612010-03-07 22:16:07 -05002026 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002027 } // for
2028 } while (firstMoved == 1);
2029 if (first > mainHeader.lastUsableLBA)
2030 first = 0;
2031 return (first);
2032} // GPTData::FindFirstAvailable()
2033
2034// Finds the first available sector in the largest block of unallocated
2035// space on the disk. Returns 0 if there are no available blocks left
2036uint64_t GPTData::FindFirstInLargest(void) {
srs5694e35eb1b2009-09-14 00:29:34 -04002037 uint64_t start, firstBlock, lastBlock, segmentSize, selectedSize = 0, selectedSegment = 0;
srs5694e4ac11e2009-08-31 10:13:04 -04002038
2039 start = 0;
2040 do {
2041 firstBlock = FindFirstAvailable(start);
2042 if (firstBlock != UINT32_C(0)) { // something's free...
2043 lastBlock = FindLastInFree(firstBlock);
2044 segmentSize = lastBlock - firstBlock + UINT32_C(1);
2045 if (segmentSize > selectedSize) {
2046 selectedSize = segmentSize;
2047 selectedSegment = firstBlock;
2048 } // if
2049 start = lastBlock + 1;
2050 } // if
2051 } while (firstBlock != 0);
2052 return selectedSegment;
2053} // GPTData::FindFirstInLargest()
2054
srs5694cb76c672010-02-11 22:22:22 -05002055// Find the last available block on the disk.
2056// Returns 0 if there are no available partitions
2057uint64_t GPTData::FindLastAvailable(void) {
srs5694e4ac11e2009-08-31 10:13:04 -04002058 uint64_t last;
2059 uint32_t i;
2060 int lastMoved = 0;
2061
2062 // Start by assuming the last usable LBA is available....
2063 last = mainHeader.lastUsableLBA;
2064
2065 // ...now, similar to algorithm in FindFirstAvailable(), search
2066 // through all partitions, moving last when it's in an existing
2067 // partition. Set the lastMoved flag so we repeat to catch cases
2068 // where partitions are out of logical order.
2069 do {
2070 lastMoved = 0;
srs56940283dae2010-04-28 16:44:34 -04002071 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04002072 if ((last >= partitions[i].GetFirstLBA()) &&
srs569455d92612010-03-07 22:16:07 -05002073 (last <= partitions[i].GetLastLBA())) { // in existing part.
srs5694e4ac11e2009-08-31 10:13:04 -04002074 last = partitions[i].GetFirstLBA() - 1;
2075 lastMoved = 1;
srs569455d92612010-03-07 22:16:07 -05002076 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002077 } // for
2078 } while (lastMoved == 1);
2079 if (last < mainHeader.firstUsableLBA)
2080 last = 0;
2081 return (last);
2082} // GPTData::FindLastAvailable()
2083
2084// Find the last available block in the free space pointed to by start.
2085uint64_t GPTData::FindLastInFree(uint64_t start) {
2086 uint64_t nearestStart;
2087 uint32_t i;
2088
2089 nearestStart = mainHeader.lastUsableLBA;
srs56940283dae2010-04-28 16:44:34 -04002090 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04002091 if ((nearestStart > partitions[i].GetFirstLBA()) &&
srs569455d92612010-03-07 22:16:07 -05002092 (partitions[i].GetFirstLBA() > start)) {
srs5694e4ac11e2009-08-31 10:13:04 -04002093 nearestStart = partitions[i].GetFirstLBA() - 1;
srs569455d92612010-03-07 22:16:07 -05002094 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002095 } // for
2096 return (nearestStart);
2097} // GPTData::FindLastInFree()
2098
2099// Finds the total number of free blocks, the number of segments in which
2100// they reside, and the size of the largest of those segments
srs5694e321d442010-01-29 17:44:04 -05002101uint64_t GPTData::FindFreeBlocks(uint32_t *numSegments, uint64_t *largestSegment) {
srs5694e4ac11e2009-08-31 10:13:04 -04002102 uint64_t start = UINT64_C(0); // starting point for each search
2103 uint64_t totalFound = UINT64_C(0); // running total
2104 uint64_t firstBlock; // first block in a segment
2105 uint64_t lastBlock; // last block in a segment
2106 uint64_t segmentSize; // size of segment in blocks
srs5694e321d442010-01-29 17:44:04 -05002107 uint32_t num = 0;
srs5694e4ac11e2009-08-31 10:13:04 -04002108
2109 *largestSegment = UINT64_C(0);
srs5694c54e9b42010-05-01 21:04:23 -04002110 if (diskSize > 0) {
2111 do {
2112 firstBlock = FindFirstAvailable(start);
2113 if (firstBlock != UINT64_C(0)) { // something's free...
2114 lastBlock = FindLastInFree(firstBlock);
2115 segmentSize = lastBlock - firstBlock + UINT64_C(1);
2116 if (segmentSize > *largestSegment) {
2117 *largestSegment = segmentSize;
2118 } // if
2119 totalFound += segmentSize;
2120 num++;
2121 start = lastBlock + 1;
srs5694e4ac11e2009-08-31 10:13:04 -04002122 } // if
srs5694c54e9b42010-05-01 21:04:23 -04002123 } while (firstBlock != 0);
2124 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002125 *numSegments = num;
2126 return totalFound;
2127} // GPTData::FindFreeBlocks()
2128
srs569455d92612010-03-07 22:16:07 -05002129// Returns 1 if sector is unallocated, 0 if it's allocated to a partition.
2130// If it's allocated, return the partition number to which it's allocated
2131// in partNum, if that variable is non-NULL. (A value of UINT32_MAX is
2132// returned in partNum if the sector is in use by basic GPT data structures.)
2133int GPTData::IsFree(uint64_t sector, uint32_t *partNum) {
srs5694e4ac11e2009-08-31 10:13:04 -04002134 int isFree = 1;
2135 uint32_t i;
2136
srs56940283dae2010-04-28 16:44:34 -04002137 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04002138 if ((sector >= partitions[i].GetFirstLBA()) &&
2139 (sector <= partitions[i].GetLastLBA())) {
2140 isFree = 0;
srs569455d92612010-03-07 22:16:07 -05002141 if (partNum != NULL)
2142 *partNum = i;
srs569408bb0da2010-02-19 17:19:55 -05002143 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002144 } // for
srs5694e35eb1b2009-09-14 00:29:34 -04002145 if ((sector < mainHeader.firstUsableLBA) ||
srs5694e4ac11e2009-08-31 10:13:04 -04002146 (sector > mainHeader.lastUsableLBA)) {
2147 isFree = 0;
srs569455d92612010-03-07 22:16:07 -05002148 if (partNum != NULL)
2149 *partNum = UINT32_MAX;
srs569408bb0da2010-02-19 17:19:55 -05002150 } // if
2151 return (isFree);
srs5694e4ac11e2009-08-31 10:13:04 -04002152} // GPTData::IsFree()
2153
srs5694ba00fed2010-01-12 18:18:36 -05002154// Returns 1 if partNum is unused.
2155int GPTData::IsFreePartNum(uint32_t partNum) {
2156 int retval = 1;
2157
srs56940283dae2010-04-28 16:44:34 -04002158 if ((partNum < numParts) && (partitions != NULL)) {
srs569408bb0da2010-02-19 17:19:55 -05002159 if (partitions[partNum].IsUsed()) {
srs5694ba00fed2010-01-12 18:18:36 -05002160 retval = 0;
2161 } // if partition is in use
2162 } else retval = 0;
2163
2164 return retval;
2165} // GPTData::IsFreePartNum()
2166
srs5694a8582cf2010-03-19 14:21:59 -04002167
2168/***********************************************************
2169 * *
2170 * Change how functions work or return information on them *
2171 * *
2172 ***********************************************************/
2173
2174// Set partition alignment value; partitions will begin on multiples of
2175// the specified value
2176void GPTData::SetAlignment(uint32_t n) {
srs5694a8582cf2010-03-19 14:21:59 -04002177 sectorAlignment = n;
srs5694a8582cf2010-03-19 14:21:59 -04002178} // GPTData::SetAlignment()
2179
2180// Compute sector alignment based on the current partitions (if any). Each
2181// partition's starting LBA is examined, and if it's divisible by a power-of-2
srs56948a4ddfc2010-03-21 19:05:49 -04002182// value less than or equal to the DEFAULT_ALIGNMENT value, but not by the
2183// previously-located alignment value, then the alignment value is adjusted
2184// down. If the computed alignment is less than 8 and the disk is bigger than
2185// SMALLEST_ADVANCED_FORMAT, resets it to 8. This is a safety measure for WD
2186// Advanced Format and similar drives. If no partitions are defined, the
2187// alignment value is set to DEFAULT_ALIGNMENT (2048). The result is that new
2188// drives are aligned to 2048-sector multiples but the program won't complain
2189// about other alignments on existing disks unless a smaller-than-8 alignment
2190// is used on small disks (as safety for WD Advanced Format drives).
srs5694a8582cf2010-03-19 14:21:59 -04002191// Returns the computed alignment value.
2192uint32_t GPTData::ComputeAlignment(void) {
2193 uint32_t i = 0, found, exponent = 31;
2194 uint64_t align = DEFAULT_ALIGNMENT;
2195
srs56948a4ddfc2010-03-21 19:05:49 -04002196 exponent = (uint32_t) log2(DEFAULT_ALIGNMENT);
srs56940283dae2010-04-28 16:44:34 -04002197 for (i = 0; i < numParts; i++) {
srs5694a8582cf2010-03-19 14:21:59 -04002198 if (partitions[i].IsUsed()) {
2199 found = 0;
2200 while (!found) {
srs56949ddc14b2010-08-22 22:44:42 -04002201 align = UINT64_C(1)<<exponent;
srs5694a8582cf2010-03-19 14:21:59 -04002202 if ((partitions[i].GetFirstLBA() % align) == 0) {
2203 found = 1;
2204 } else {
2205 exponent--;
2206 } // if/else
2207 } // while
2208 } // if
2209 } // for
2210 if ((align < 8) && (diskSize >= SMALLEST_ADVANCED_FORMAT))
2211 align = 8;
srs5694a8582cf2010-03-19 14:21:59 -04002212 SetAlignment(align);
2213 return align;
2214} // GPTData::ComputeAlignment()
2215
srs5694e4ac11e2009-08-31 10:13:04 -04002216/********************************
2217 * *
2218 * Endianness support functions *
2219 * *
2220 ********************************/
2221
srs56942a9f5da2009-08-26 00:48:01 -04002222void GPTData::ReverseHeaderBytes(struct GPTHeader* header) {
srs5694221e0872009-08-29 15:00:31 -04002223 ReverseBytes(&header->signature, 8);
2224 ReverseBytes(&header->revision, 4);
2225 ReverseBytes(&header->headerSize, 4);
2226 ReverseBytes(&header->headerCRC, 4);
2227 ReverseBytes(&header->reserved, 4);
2228 ReverseBytes(&header->currentLBA, 8);
2229 ReverseBytes(&header->backupLBA, 8);
2230 ReverseBytes(&header->firstUsableLBA, 8);
2231 ReverseBytes(&header->lastUsableLBA, 8);
2232 ReverseBytes(&header->partitionEntriesLBA, 8);
2233 ReverseBytes(&header->numParts, 4);
2234 ReverseBytes(&header->sizeOfPartitionEntries, 4);
2235 ReverseBytes(&header->partitionEntriesCRC, 4);
srs569408bb0da2010-02-19 17:19:55 -05002236 ReverseBytes(header->reserved2, GPT_RESERVED);
srs56942a9f5da2009-08-26 00:48:01 -04002237} // GPTData::ReverseHeaderBytes()
2238
srs56940283dae2010-04-28 16:44:34 -04002239// Reverse byte order for all partitions.
srs56942a9f5da2009-08-26 00:48:01 -04002240void GPTData::ReversePartitionBytes() {
2241 uint32_t i;
2242
srs56940283dae2010-04-28 16:44:34 -04002243 for (i = 0; i < numParts; i++) {
srs5694221e0872009-08-29 15:00:31 -04002244 partitions[i].ReversePartBytes();
srs56942a9f5da2009-08-26 00:48:01 -04002245 } // for
2246} // GPTData::ReversePartitionBytes()
2247
srs56949ddc14b2010-08-22 22:44:42 -04002248// Validate partition number
2249bool GPTData::ValidPartNum (const uint32_t partNum) {
2250 if (partNum >= numParts) {
srs56945a081752010-09-24 20:39:41 -04002251 cerr << "Partition number out of range: " << partNum << "\n";
srs56949ddc14b2010-08-22 22:44:42 -04002252 return false;
2253 } // if
2254 return true;
2255} // GPTData::ValidPartNum
2256
srs56945a081752010-09-24 20:39:41 -04002257// Return a single partition for inspection (not modification!) by other
2258// functions.
2259const GPTPart & GPTData::operator[](uint32_t partNum) const {
2260 if (partNum >= numParts) {
2261 cerr << "Partition number out of range: " << partNum << "\n";
2262 partNum = 0;
2263 } // if
2264 return partitions[partNum];
2265} // operator[]
2266
2267// Return (not for modification!) the disk's GUID value
2268const GUIDData & GPTData::GetDiskGUID(void) const {
2269 return mainHeader.diskGUID;
2270} // GPTData::GetDiskGUID()
2271
srs56949ddc14b2010-08-22 22:44:42 -04002272// Manage attributes for a partition, based on commands passed to this function.
2273// (Function is non-interactive.)
2274// Returns 1 if a modification command succeeded, 0 if the command should not have
2275// modified data, and -1 if a modification command failed.
2276int GPTData::ManageAttributes(int partNum, const string & command, const string & bits) {
2277 int retval = 0;
2278 Attributes theAttr;
2279
2280 if (command == "show") {
2281 ShowAttributes(partNum);
2282 } else if (command == "get") {
2283 GetAttribute(partNum, bits);
2284 } else {
2285 theAttr = partitions[partNum].GetAttributes();
2286 if (theAttr.OperateOnAttributes(partNum, command, bits)) {
2287 partitions[partNum].SetAttributes(theAttr.GetAttributes());
2288 retval = 1;
2289 } else {
2290 retval = -1;
2291 } // if/else
2292 } // if/elseif/else
2293
2294 return retval;
2295} // GPTData::ManageAttributes()
2296
2297// Show all attributes for a specified partition....
2298void GPTData::ShowAttributes(const uint32_t partNum) {
2299 Attributes theAttr (partitions[partNum].GetAttributes());
2300 theAttr.ShowAttributes(partNum);
2301} // GPTData::ShowAttributes
2302
2303// Show whether a single attribute bit is set (terse output)...
2304void GPTData::GetAttribute(const uint32_t partNum, const string& attributeBits) {
2305 Attributes theAttr (partitions[partNum].GetAttributes());
2306 theAttr.OperateOnAttributes(partNum, "get", attributeBits);
2307} // GPTData::GetAttribute
2308
2309
srs56942a9f5da2009-08-26 00:48:01 -04002310/******************************************
2311 * *
2312 * Additional non-class support functions *
2313 * *
2314 ******************************************/
2315
srs5694e7b4ff92009-08-18 13:16:10 -04002316// Check to be sure that data type sizes are correct. The basic types (uint*_t) should
2317// never fail these tests, but the struct types may fail depending on compile options.
2318// Specifically, the -fpack-struct option to gcc may be required to ensure proper structure
2319// sizes.
2320int SizesOK(void) {
2321 int allOK = 1;
srs5694e7b4ff92009-08-18 13:16:10 -04002322
2323 if (sizeof(uint8_t) != 1) {
srs5694fed16d02010-01-27 23:03:40 -05002324 cerr << "uint8_t is " << sizeof(uint8_t) << " bytes, should be 1 byte; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002325 allOK = 0;
2326 } // if
2327 if (sizeof(uint16_t) != 2) {
srs5694fed16d02010-01-27 23:03:40 -05002328 cerr << "uint16_t is " << sizeof(uint16_t) << " bytes, should be 2 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002329 allOK = 0;
2330 } // if
2331 if (sizeof(uint32_t) != 4) {
srs5694fed16d02010-01-27 23:03:40 -05002332 cerr << "uint32_t is " << sizeof(uint32_t) << " bytes, should be 4 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002333 allOK = 0;
2334 } // if
2335 if (sizeof(uint64_t) != 8) {
srs5694fed16d02010-01-27 23:03:40 -05002336 cerr << "uint64_t is " << sizeof(uint64_t) << " bytes, should be 8 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002337 allOK = 0;
2338 } // if
2339 if (sizeof(struct MBRRecord) != 16) {
srs5694fed16d02010-01-27 23:03:40 -05002340 cerr << "MBRRecord is " << sizeof(MBRRecord) << " bytes, should be 16 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002341 allOK = 0;
2342 } // if
srs5694978041c2009-09-21 20:51:47 -04002343 if (sizeof(struct TempMBR) != 512) {
srs5694fed16d02010-01-27 23:03:40 -05002344 cerr << "TempMBR is " << sizeof(TempMBR) << " bytes, should be 512 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002345 allOK = 0;
2346 } // if
2347 if (sizeof(struct GPTHeader) != 512) {
srs5694fed16d02010-01-27 23:03:40 -05002348 cerr << "GPTHeader is " << sizeof(GPTHeader) << " bytes, should be 512 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002349 allOK = 0;
2350 } // if
srs5694221e0872009-08-29 15:00:31 -04002351 if (sizeof(GPTPart) != 128) {
srs5694fed16d02010-01-27 23:03:40 -05002352 cerr << "GPTPart is " << sizeof(GPTPart) << " bytes, should be 128 bytes; aborting!\n";
srs5694221e0872009-08-29 15:00:31 -04002353 allOK = 0;
2354 } // if
srs56946699b012010-02-04 00:55:30 -05002355 if (sizeof(GUIDData) != 16) {
2356 cerr << "GUIDData is " << sizeof(GUIDData) << " bytes, should be 16 bytes; aborting!\n";
2357 allOK = 0;
2358 } // if
2359 if (sizeof(PartType) != 16) {
2360 cerr << "PartType is " << sizeof(GUIDData) << " bytes, should be 16 bytes; aborting!\n";
2361 allOK = 0;
2362 } // if
srs5694fed16d02010-01-27 23:03:40 -05002363 // Determine endianness; warn user if running on big-endian (PowerPC, etc.) hardware
srs56942a9f5da2009-08-26 00:48:01 -04002364 if (IsLittleEndian() == 0) {
srs5694fed16d02010-01-27 23:03:40 -05002365 cerr << "\aRunning on big-endian hardware. Big-endian support is new and poorly"
2366 " tested!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002367 } // if
2368 return (allOK);
2369} // SizesOK()
srs5694e4ac11e2009-08-31 10:13:04 -04002370