blob: ea0a0a3899a2d860ed0c043e9b8e60dd34d91dae [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
srs569464cbd172011-03-01 22:03:54 -05006/* This program is copyright (c) 2009-2011 by Roderick W. Smith. It is distributed
srs5694221e0872009-08-29 15:00:31 -04007 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>
srs56949a46b042011-03-15 00:34:10 -040022#include <algorithm>
srs5694e7b4ff92009-08-18 13:16:10 -040023#include "crc32.h"
24#include "gpt.h"
srs5694221e0872009-08-29 15:00:31 -040025#include "bsd.h"
srs5694e7b4ff92009-08-18 13:16:10 -040026#include "support.h"
27#include "parttypes.h"
28#include "attributes.h"
srs5694546a9c72010-01-26 16:00:26 -050029#include "diskio.h"
srs5694bf8950c2011-03-12 01:23:12 -050030//#include "partnotes.h"
srs5694e7b4ff92009-08-18 13:16:10 -040031
32using namespace std;
33
srs56948f1b2d62010-05-23 13:07:19 -040034#ifdef __FreeBSD__
srs56949ba54212010-05-18 23:24:02 -040035#define log2(x) (log(x) / M_LN2)
36#endif // __FreeBSD__
37
srs56948f1b2d62010-05-23 13:07:19 -040038#ifdef _MSC_VER
39#define log2(x) (log((double) x) / log(2.0))
40#endif // Microsoft Visual C++
srs56949ba54212010-05-18 23:24:02 -040041
srs5694e7b4ff92009-08-18 13:16:10 -040042/****************************************
43 * *
44 * GPTData class and related structures *
45 * *
46 ****************************************/
47
srs5694e4ac11e2009-08-31 10:13:04 -040048// Default constructor
srs5694e7b4ff92009-08-18 13:16:10 -040049GPTData::GPTData(void) {
50 blockSize = SECTOR_SIZE; // set a default
51 diskSize = 0;
52 partitions = NULL;
53 state = gpt_valid;
srs5694fed16d02010-01-27 23:03:40 -050054 device = "";
srs56945d58fe02010-01-03 20:57:08 -050055 justLooking = 0;
srs5694e7b4ff92009-08-18 13:16:10 -040056 mainCrcOk = 0;
57 secondCrcOk = 0;
58 mainPartsCrcOk = 0;
59 secondPartsCrcOk = 0;
srs5694221e0872009-08-29 15:00:31 -040060 apmFound = 0;
61 bsdFound = 0;
srs56940873e9d2010-10-07 13:00:45 -040062 sectorAlignment = MIN_AF_ALIGNMENT; // Align partitions on 4096-byte boundaries by default
srs5694ba00fed2010-01-12 18:18:36 -050063 beQuiet = 0;
64 whichWasUsed = use_new;
srs56941e093722010-01-05 00:14:19 -050065 mainHeader.numParts = 0;
srs56940283dae2010-04-28 16:44:34 -040066 numParts = 0;
srs5694e7b4ff92009-08-18 13:16:10 -040067 SetGPTSize(NUM_GPT_ENTRIES);
68} // GPTData default constructor
69
70// The following constructor loads GPT data from a device file
srs5694fed16d02010-01-27 23:03:40 -050071GPTData::GPTData(string filename) {
srs5694e7b4ff92009-08-18 13:16:10 -040072 blockSize = SECTOR_SIZE; // set a default
73 diskSize = 0;
74 partitions = NULL;
75 state = gpt_invalid;
srs5694fed16d02010-01-27 23:03:40 -050076 device = "";
srs56945d58fe02010-01-03 20:57:08 -050077 justLooking = 0;
srs5694e7b4ff92009-08-18 13:16:10 -040078 mainCrcOk = 0;
79 secondCrcOk = 0;
80 mainPartsCrcOk = 0;
81 secondPartsCrcOk = 0;
srs5694221e0872009-08-29 15:00:31 -040082 apmFound = 0;
83 bsdFound = 0;
srs56940873e9d2010-10-07 13:00:45 -040084 sectorAlignment = MIN_AF_ALIGNMENT; // Align partitions on 4096-byte boundaries by default
srs5694ba00fed2010-01-12 18:18:36 -050085 beQuiet = 0;
86 whichWasUsed = use_new;
srs56941e093722010-01-05 00:14:19 -050087 mainHeader.numParts = 0;
srs56940283dae2010-04-28 16:44:34 -040088 numParts = 0;
srs56943c0af382010-01-15 19:19:18 -050089 if (!LoadPartitions(filename))
90 exit(2);
srs5694fed16d02010-01-27 23:03:40 -050091} // GPTData(string filename) constructor
srs5694e7b4ff92009-08-18 13:16:10 -040092
srs5694e4ac11e2009-08-31 10:13:04 -040093// Destructor
srs5694e7b4ff92009-08-18 13:16:10 -040094GPTData::~GPTData(void) {
srs5694cb76c672010-02-11 22:22:22 -050095 delete[] partitions;
srs5694e7b4ff92009-08-18 13:16:10 -040096} // GPTData destructor
97
srs569464cbd172011-03-01 22:03:54 -050098// Assignment operator
99GPTData & GPTData::operator=(const GPTData & orig) {
100 uint32_t i;
101
102 mainHeader = orig.mainHeader;
103 numParts = orig.numParts;
104 secondHeader = orig.secondHeader;
105 protectiveMBR = orig.protectiveMBR;
106 device = orig.device;
107 blockSize = orig.blockSize;
108 diskSize = orig.diskSize;
109 state = orig.state;
110 justLooking = orig.justLooking;
111 mainCrcOk = orig.mainCrcOk;
112 secondCrcOk = orig.secondCrcOk;
113 mainPartsCrcOk = orig.mainPartsCrcOk;
114 secondPartsCrcOk = orig.secondPartsCrcOk;
115 apmFound = orig.apmFound;
116 bsdFound = orig.bsdFound;
117 sectorAlignment = orig.sectorAlignment;
118 beQuiet = orig.beQuiet;
119 whichWasUsed = orig.whichWasUsed;
120
121 myDisk.OpenForRead(orig.myDisk.GetName());
122
123 delete[] partitions;
srs569401f7f082011-03-15 23:53:31 -0400124 partitions = new GPTPart [numParts];
srs569464cbd172011-03-01 22:03:54 -0500125 if (partitions != NULL) {
126 for (i = 0; i < numParts; i++) {
127 partitions[i] = orig.partitions[i];
128 }
129 } else {
130 numParts = 0;
131 cerr << "Error! Could not allocate memory for partitions in GPTData::operator=()!\n"
132 << "Continuing, but strange problems may occur!\n";
133 } // if/else
134 return *this;
135} // GPTData::operator=()
136
srs5694e4ac11e2009-08-31 10:13:04 -0400137/*********************************************************************
138 * *
139 * Begin functions that verify data, or that adjust the verification *
140 * information (compute CRCs, rebuild headers) *
141 * *
142 *********************************************************************/
srs5694e7b4ff92009-08-18 13:16:10 -0400143
srs5694e4ac11e2009-08-31 10:13:04 -0400144// Perform detailed verification, reporting on any problems found, but
145// do *NOT* recover from these problems. Returns the total number of
146// problems identified.
147int GPTData::Verify(void) {
srs569464cbd172011-03-01 22:03:54 -0500148 int problems = 0, alignProbs = 0;
srs5694e321d442010-01-29 17:44:04 -0500149 uint32_t i, numSegments;
150 uint64_t totalFree, largestSegment;
srs5694e4ac11e2009-08-31 10:13:04 -0400151
152 // First, check for CRC errors in the GPT data....
153 if (!mainCrcOk) {
154 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500155 cout << "\nProblem: The CRC for the main GPT header is invalid. The main GPT header may\n"
156 << "be corrupt. Consider loading the backup GPT header to rebuild the main GPT\n"
157 << "header ('b' on the recovery & transformation menu). This report may be a false\n"
158 << "alarm if you've already corrected other problems.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400159 } // if
160 if (!mainPartsCrcOk) {
161 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500162 cout << "\nProblem: The CRC for the main partition table is invalid. This table may be\n"
163 << "corrupt. Consider loading the backup partition table ('c' on the recovery &\n"
164 << "transformation menu). This report may be a false alarm if you've already\n"
165 << "corrected other problems.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400166 } // if
167 if (!secondCrcOk) {
168 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500169 cout << "\nProblem: The CRC for the backup GPT header is invalid. The backup GPT header\n"
170 << "may be corrupt. Consider using the main GPT header to rebuild the backup GPT\n"
171 << "header ('d' on the recovery & transformation menu). This report may be a false\n"
172 << "alarm if you've already corrected other problems.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400173 } // if
174 if (!secondPartsCrcOk) {
175 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500176 cout << "\nCaution: The CRC for the backup partition table is invalid. This table may\n"
177 << "be corrupt. This program will automatically create a new backup partition\n"
178 << "table when you save your partitions.\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400179 } // if
180
srs5694978041c2009-09-21 20:51:47 -0400181 // Now check that the main and backup headers both point to themselves....
182 if (mainHeader.currentLBA != 1) {
183 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500184 cout << "\nProblem: The main header's self-pointer doesn't point to itself. This problem\n"
185 << "is being automatically corrected, but it may be a symptom of more serious\n"
186 << "problems. Think carefully before saving changes with 'w' or using this disk.\n";
srs5694978041c2009-09-21 20:51:47 -0400187 mainHeader.currentLBA = 1;
188 } // if
189 if (secondHeader.currentLBA != (diskSize - UINT64_C(1))) {
190 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500191 cout << "\nProblem: The secondary header's self-pointer indicates that it doesn't reside\n"
192 << "at the end of the disk. If you've added a disk to a RAID array, use the 'e'\n"
193 << "option on the experts' menu to adjust the secondary header's and partition\n"
194 << "table's locations.\n";
srs5694978041c2009-09-21 20:51:47 -0400195 } // if
196
197 // Now check that critical main and backup GPT entries match each other
srs5694e4ac11e2009-08-31 10:13:04 -0400198 if (mainHeader.currentLBA != secondHeader.backupLBA) {
199 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500200 cout << "\nProblem: main GPT header's current LBA pointer (" << mainHeader.currentLBA
201 << ") doesn't\nmatch the backup GPT header's alternate LBA pointer("
202 << secondHeader.backupLBA << ").\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400203 } // if
204 if (mainHeader.backupLBA != secondHeader.currentLBA) {
205 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500206 cout << "\nProblem: main GPT header's backup LBA pointer (" << mainHeader.backupLBA
207 << ") doesn't\nmatch the backup GPT header's current LBA pointer ("
208 << secondHeader.currentLBA << ").\n"
209 << "The 'e' option on the experts' menu may fix this problem.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400210 } // if
211 if (mainHeader.firstUsableLBA != secondHeader.firstUsableLBA) {
212 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500213 cout << "\nProblem: main GPT header's first usable LBA pointer (" << mainHeader.firstUsableLBA
214 << ") doesn't\nmatch the backup GPT header's first usable LBA pointer ("
215 << secondHeader.firstUsableLBA << ")\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400216 } // if
217 if (mainHeader.lastUsableLBA != secondHeader.lastUsableLBA) {
218 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500219 cout << "\nProblem: main GPT header's last usable LBA pointer (" << mainHeader.lastUsableLBA
220 << ") doesn't\nmatch the backup GPT header's last usable LBA pointer ("
221 << secondHeader.lastUsableLBA << ")\n"
222 << "The 'e' option on the experts' menu can probably fix this problem.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400223 } // if
srs56946699b012010-02-04 00:55:30 -0500224 if ((mainHeader.diskGUID != secondHeader.diskGUID)) {
srs5694e4ac11e2009-08-31 10:13:04 -0400225 problems++;
srs56945a081752010-09-24 20:39:41 -0400226 cout << "\nProblem: main header's disk GUID (" << mainHeader.diskGUID
srs5694fed16d02010-01-27 23:03:40 -0500227 << ") doesn't\nmatch the backup GPT header's disk GUID ("
srs56945a081752010-09-24 20:39:41 -0400228 << secondHeader.diskGUID << ")\n"
srs5694fed16d02010-01-27 23:03:40 -0500229 << "You should use the 'b' or 'd' option on the recovery & transformation menu to\n"
230 << "select one or the other header.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400231 } // if
232 if (mainHeader.numParts != secondHeader.numParts) {
233 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500234 cout << "\nProblem: main GPT header's number of partitions (" << mainHeader.numParts
235 << ") doesn't\nmatch the backup GPT header's number of partitions ("
236 << secondHeader.numParts << ")\n"
237 << "Resizing the partition table ('s' on the experts' menu) may help.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400238 } // if
239 if (mainHeader.sizeOfPartitionEntries != secondHeader.sizeOfPartitionEntries) {
240 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500241 cout << "\nProblem: main GPT header's size of partition entries ("
242 << mainHeader.sizeOfPartitionEntries << ") doesn't\n"
243 << "match the backup GPT header's size of partition entries ("
244 << secondHeader.sizeOfPartitionEntries << ")\n"
245 << "You should use the 'b' or 'd' option on the recovery & transformation menu to\n"
246 << "select one or the other header.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400247 } // if
248
249 // Now check for a few other miscellaneous problems...
250 // Check that the disk size will hold the data...
srs569464cbd172011-03-01 22:03:54 -0500251 if (mainHeader.backupLBA >= diskSize) {
srs5694e4ac11e2009-08-31 10:13:04 -0400252 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500253 cout << "\nProblem: Disk is too small to hold all the data!\n"
254 << "(Disk size is " << diskSize << " sectors, needs to be "
srs569464cbd172011-03-01 22:03:54 -0500255 << mainHeader.backupLBA + UINT64_C(1) << " sectors.)\n"
srs5694fed16d02010-01-27 23:03:40 -0500256 << "The 'e' option on the experts' menu may fix this problem.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400257 } // if
258
259 // Check for overlapping partitions....
260 problems += FindOverlaps();
261
srs569455d92612010-03-07 22:16:07 -0500262 // Check for insane partitions (start after end, hugely big, etc.)
263 problems += FindInsanePartitions();
264
srs5694e4ac11e2009-08-31 10:13:04 -0400265 // Check for mismatched MBR and GPT partitions...
266 problems += FindHybridMismatches();
267
srs5694327129e2010-09-22 01:07:31 -0400268 // Check for MBR-specific problems....
269 problems += VerifyMBR();
270
srs5694e4ac11e2009-08-31 10:13:04 -0400271 // Verify that partitions don't run into GPT data areas....
272 problems += CheckGPTSize();
273
srs56941d1448a2009-12-31 21:20:19 -0500274 // Check that partitions are aligned on proper boundaries (for WD Advanced
275 // Format and similar disks)....
srs56940283dae2010-04-28 16:44:34 -0400276 for (i = 0; i < numParts; i++) {
srs56941d1448a2009-12-31 21:20:19 -0500277 if ((partitions[i].GetFirstLBA() % sectorAlignment) != 0) {
srs5694fed16d02010-01-27 23:03:40 -0500278 cout << "\nCaution: Partition " << i + 1 << " doesn't begin on a "
279 << sectorAlignment << "-sector boundary. This may\nresult "
280 << "in degraded performance on some modern (2009 and later) hard disks.\n";
srs569464cbd172011-03-01 22:03:54 -0500281 alignProbs++;
srs56941d1448a2009-12-31 21:20:19 -0500282 } // if
283 } // for
srs569464cbd172011-03-01 22:03:54 -0500284 if (alignProbs > 0)
285 cout << "\nConsult http://www.ibm.com/developerworks/linux/library/l-4kb-sector-disks/\n"
286 << "for information on disk alignment.\n";
srs56941d1448a2009-12-31 21:20:19 -0500287
srs5694e4ac11e2009-08-31 10:13:04 -0400288 // Now compute available space, but only if no problems found, since
289 // problems could affect the results
290 if (problems == 0) {
291 totalFree = FindFreeBlocks(&numSegments, &largestSegment);
srs569464cbd172011-03-01 22:03:54 -0500292 cout << "\nNo problems found. " << totalFree << " free sectors ("
srs569401f7f082011-03-15 23:53:31 -0400293 << BytesToIeee(totalFree, blockSize) << ") available in "
srs5694fed16d02010-01-27 23:03:40 -0500294 << numSegments << "\nsegments, the largest of which is "
srs569401f7f082011-03-15 23:53:31 -0400295 << largestSegment << " (" << BytesToIeee(largestSegment, blockSize)
srs56940283dae2010-04-28 16:44:34 -0400296 << ") in size.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400297 } else {
srs56940a697312010-01-28 21:10:52 -0500298 cout << "\nIdentified " << problems << " problems!\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400299 } // if/else
srs5694e4ac11e2009-08-31 10:13:04 -0400300
301 return (problems);
302} // GPTData::Verify()
srs5694e7b4ff92009-08-18 13:16:10 -0400303
304// Checks to see if the GPT tables overrun existing partitions; if they
srs5694221e0872009-08-29 15:00:31 -0400305// do, issues a warning but takes no action. Returns number of problems
306// detected (0 if OK, 1 to 2 if problems).
srs5694e7b4ff92009-08-18 13:16:10 -0400307int GPTData::CheckGPTSize(void) {
308 uint64_t overlap, firstUsedBlock, lastUsedBlock;
309 uint32_t i;
srs5694221e0872009-08-29 15:00:31 -0400310 int numProbs = 0;
srs5694e7b4ff92009-08-18 13:16:10 -0400311
312 // first, locate the first & last used blocks
313 firstUsedBlock = UINT64_MAX;
314 lastUsedBlock = 0;
srs56940283dae2010-04-28 16:44:34 -0400315 for (i = 0; i < numParts; i++) {
srs5694221e0872009-08-29 15:00:31 -0400316 if ((partitions[i].GetFirstLBA() < firstUsedBlock) &&
srs5694e4ac11e2009-08-31 10:13:04 -0400317 (partitions[i].GetFirstLBA() != 0))
srs5694221e0872009-08-29 15:00:31 -0400318 firstUsedBlock = partitions[i].GetFirstLBA();
319 if (partitions[i].GetLastLBA() > lastUsedBlock)
320 lastUsedBlock = partitions[i].GetLastLBA();
srs5694e7b4ff92009-08-18 13:16:10 -0400321 } // for
322
323 // If the disk size is 0 (the default), then it means that various
324 // variables aren't yet set, so the below tests will be useless;
325 // therefore we should skip everything
326 if (diskSize != 0) {
327 if (mainHeader.firstUsableLBA > firstUsedBlock) {
328 overlap = mainHeader.firstUsableLBA - firstUsedBlock;
srs5694fed16d02010-01-27 23:03:40 -0500329 cout << "Warning! Main partition table overlaps the first partition by "
330 << overlap << " blocks!\n";
srs5694221e0872009-08-29 15:00:31 -0400331 if (firstUsedBlock > 2) {
srs5694fed16d02010-01-27 23:03:40 -0500332 cout << "Try reducing the partition table size by " << overlap * 4
333 << " entries.\n(Use the 's' item on the experts' menu.)\n";
srs5694221e0872009-08-29 15:00:31 -0400334 } else {
srs5694fed16d02010-01-27 23:03:40 -0500335 cout << "You will need to delete this partition or resize it in another utility.\n";
srs5694221e0872009-08-29 15:00:31 -0400336 } // if/else
337 numProbs++;
srs5694e7b4ff92009-08-18 13:16:10 -0400338 } // Problem at start of disk
339 if (mainHeader.lastUsableLBA < lastUsedBlock) {
340 overlap = lastUsedBlock - mainHeader.lastUsableLBA;
srs569455d92612010-03-07 22:16:07 -0500341 cout << "\nWarning! Secondary partition table overlaps the last partition by\n"
srs5694fed16d02010-01-27 23:03:40 -0500342 << overlap << " blocks!\n";
srs5694221e0872009-08-29 15:00:31 -0400343 if (lastUsedBlock > (diskSize - 2)) {
srs5694fed16d02010-01-27 23:03:40 -0500344 cout << "You will need to delete this partition or resize it in another utility.\n";
srs5694221e0872009-08-29 15:00:31 -0400345 } else {
srs5694fed16d02010-01-27 23:03:40 -0500346 cout << "Try reducing the partition table size by " << overlap * 4
347 << " entries.\n(Use the 's' item on the experts' menu.)\n";
srs5694221e0872009-08-29 15:00:31 -0400348 } // if/else
349 numProbs++;
srs5694e7b4ff92009-08-18 13:16:10 -0400350 } // Problem at end of disk
351 } // if (diskSize != 0)
srs5694221e0872009-08-29 15:00:31 -0400352 return numProbs;
srs5694e7b4ff92009-08-18 13:16:10 -0400353} // GPTData::CheckGPTSize()
354
srs5694e7b4ff92009-08-18 13:16:10 -0400355// Check the validity of the GPT header. Returns 1 if the main header
356// is valid, 2 if the backup header is valid, 3 if both are valid, and
357// 0 if neither is valid. Note that this function just checks the GPT
358// signature and revision numbers, not CRCs or other data.
359int GPTData::CheckHeaderValidity(void) {
360 int valid = 3;
361
srs5694fed16d02010-01-27 23:03:40 -0500362 cout.setf(ios::uppercase);
363 cout.fill('0');
364
365 // Note: failed GPT signature checks produce no error message because
366 // a message is displayed in the ReversePartitionBytes() function
srs5694e7b4ff92009-08-18 13:16:10 -0400367 if (mainHeader.signature != GPT_SIGNATURE) {
368 valid -= 1;
srs5694e7b4ff92009-08-18 13:16:10 -0400369 } else if ((mainHeader.revision != 0x00010000) && valid) {
370 valid -= 1;
srs5694fed16d02010-01-27 23:03:40 -0500371 cout << "Unsupported GPT version in main header; read 0x";
372 cout.width(8);
373 cout << hex << mainHeader.revision << ", should be\n0x";
374 cout.width(8);
375 cout << UINT32_C(0x00010000) << dec << "\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400376 } // if/else/if
377
378 if (secondHeader.signature != GPT_SIGNATURE) {
379 valid -= 2;
srs5694e7b4ff92009-08-18 13:16:10 -0400380 } else if ((secondHeader.revision != 0x00010000) && valid) {
381 valid -= 2;
srs5694fed16d02010-01-27 23:03:40 -0500382 cout << "Unsupported GPT version in backup header; read 0x";
383 cout.width(8);
384 cout << hex << secondHeader.revision << ", should be\n0x";
385 cout.width(8);
386 cout << UINT32_C(0x00010000) << dec << "\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400387 } // if/else/if
388
srs5694df9d3632011-01-08 18:33:24 -0500389 // Check for an Apple disk signature
390 if (((mainHeader.signature << 32) == APM_SIGNATURE1) ||
391 (mainHeader.signature << 32) == APM_SIGNATURE2) {
srs5694221e0872009-08-29 15:00:31 -0400392 apmFound = 1; // Will display warning message later
srs56943f2fe992009-11-24 18:28:18 -0500393 } // if
srs5694fed16d02010-01-27 23:03:40 -0500394 cout.fill(' ');
srs56942a9f5da2009-08-26 00:48:01 -0400395
srs5694fed16d02010-01-27 23:03:40 -0500396 return valid;
srs5694e7b4ff92009-08-18 13:16:10 -0400397} // GPTData::CheckHeaderValidity()
398
399// Check the header CRC to see if it's OK...
srs5694cb76c672010-02-11 22:22:22 -0500400// Note: Must be called with header in LITTLE-ENDIAN
401// (x86, x86-64, etc.) byte order.
srs5694e7b4ff92009-08-18 13:16:10 -0400402int GPTData::CheckHeaderCRC(struct GPTHeader* header) {
srs5694978041c2009-09-21 20:51:47 -0400403 uint32_t oldCRC, newCRC, hSize;
srs5694e7b4ff92009-08-18 13:16:10 -0400404
srs56942a9f5da2009-08-26 00:48:01 -0400405 // Back up old header CRC and then blank it, since it must be 0 for
srs5694e7b4ff92009-08-18 13:16:10 -0400406 // computation to be valid
407 oldCRC = header->headerCRC;
408 header->headerCRC = UINT32_C(0);
srs5694978041c2009-09-21 20:51:47 -0400409 hSize = header->headerSize;
410
411 // If big-endian system, reverse byte order
412 if (IsLittleEndian() == 0) {
413 ReverseBytes(&oldCRC, 4);
414 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400415
416 // Initialize CRC functions...
417 chksum_crc32gentab();
418
419 // Compute CRC, restore original, and return result of comparison
420 newCRC = chksum_crc32((unsigned char*) header, HEADER_SIZE);
srs5694978041c2009-09-21 20:51:47 -0400421 header->headerCRC = oldCRC;
srs5694e7b4ff92009-08-18 13:16:10 -0400422 return (oldCRC == newCRC);
423} // GPTData::CheckHeaderCRC()
424
srs56946699b012010-02-04 00:55:30 -0500425// Recompute all the CRCs. Must be called before saving if any changes have
426// been made. Must be called on platform-ordered data (this function reverses
427// byte order and then undoes that reversal.)
srs5694e7b4ff92009-08-18 13:16:10 -0400428void GPTData::RecomputeCRCs(void) {
srs56940283dae2010-04-28 16:44:34 -0400429 uint32_t crc, hSize;
srs56942a9f5da2009-08-26 00:48:01 -0400430 int littleEndian = 1;
srs5694e7b4ff92009-08-18 13:16:10 -0400431
432 // Initialize CRC functions...
433 chksum_crc32gentab();
434
srs56946699b012010-02-04 00:55:30 -0500435 // Save some key data from header before reversing byte order....
srs5694978041c2009-09-21 20:51:47 -0400436 hSize = mainHeader.headerSize;
srs56946699b012010-02-04 00:55:30 -0500437
438 if ((littleEndian = IsLittleEndian()) == 0) {
439 ReversePartitionBytes();
440 ReverseHeaderBytes(&mainHeader);
441 ReverseHeaderBytes(&secondHeader);
442 } // if
srs56942a9f5da2009-08-26 00:48:01 -0400443
srs5694e7b4ff92009-08-18 13:16:10 -0400444 // Compute CRC of partition tables & store in main and secondary headers
srs56940283dae2010-04-28 16:44:34 -0400445 crc = chksum_crc32((unsigned char*) partitions, numParts * GPT_SIZE);
srs5694e7b4ff92009-08-18 13:16:10 -0400446 mainHeader.partitionEntriesCRC = crc;
447 secondHeader.partitionEntriesCRC = crc;
srs56942a9f5da2009-08-26 00:48:01 -0400448 if (littleEndian == 0) {
srs5694221e0872009-08-29 15:00:31 -0400449 ReverseBytes(&mainHeader.partitionEntriesCRC, 4);
450 ReverseBytes(&secondHeader.partitionEntriesCRC, 4);
srs56942a9f5da2009-08-26 00:48:01 -0400451 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400452
453 // Zero out GPT tables' own CRCs (required for correct computation)
454 mainHeader.headerCRC = 0;
455 secondHeader.headerCRC = 0;
456
457 // Compute & store CRCs of main & secondary headers...
srs5694978041c2009-09-21 20:51:47 -0400458 crc = chksum_crc32((unsigned char*) &mainHeader, hSize);
srs56942a9f5da2009-08-26 00:48:01 -0400459 if (littleEndian == 0)
srs5694221e0872009-08-29 15:00:31 -0400460 ReverseBytes(&crc, 4);
srs5694e7b4ff92009-08-18 13:16:10 -0400461 mainHeader.headerCRC = crc;
srs5694978041c2009-09-21 20:51:47 -0400462 crc = chksum_crc32((unsigned char*) &secondHeader, hSize);
srs56942a9f5da2009-08-26 00:48:01 -0400463 if (littleEndian == 0)
srs5694221e0872009-08-29 15:00:31 -0400464 ReverseBytes(&crc, 4);
srs5694e7b4ff92009-08-18 13:16:10 -0400465 secondHeader.headerCRC = crc;
srs56946699b012010-02-04 00:55:30 -0500466
467 if ((littleEndian = IsLittleEndian()) == 0) {
468 ReverseHeaderBytes(&mainHeader);
469 ReverseHeaderBytes(&secondHeader);
470 ReversePartitionBytes();
471 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400472} // GPTData::RecomputeCRCs()
473
srs5694e7b4ff92009-08-18 13:16:10 -0400474// Rebuild the main GPT header, using the secondary header as a model.
475// Typically called when the main header has been found to be corrupt.
476void GPTData::RebuildMainHeader(void) {
srs5694e7b4ff92009-08-18 13:16:10 -0400477 mainHeader.signature = GPT_SIGNATURE;
478 mainHeader.revision = secondHeader.revision;
srs5694978041c2009-09-21 20:51:47 -0400479 mainHeader.headerSize = secondHeader.headerSize;
srs5694e7b4ff92009-08-18 13:16:10 -0400480 mainHeader.headerCRC = UINT32_C(0);
481 mainHeader.reserved = secondHeader.reserved;
482 mainHeader.currentLBA = secondHeader.backupLBA;
483 mainHeader.backupLBA = secondHeader.currentLBA;
484 mainHeader.firstUsableLBA = secondHeader.firstUsableLBA;
485 mainHeader.lastUsableLBA = secondHeader.lastUsableLBA;
srs56946699b012010-02-04 00:55:30 -0500486 mainHeader.diskGUID = secondHeader.diskGUID;
srs5694e7b4ff92009-08-18 13:16:10 -0400487 mainHeader.partitionEntriesLBA = UINT64_C(2);
488 mainHeader.numParts = secondHeader.numParts;
489 mainHeader.sizeOfPartitionEntries = secondHeader.sizeOfPartitionEntries;
490 mainHeader.partitionEntriesCRC = secondHeader.partitionEntriesCRC;
srs569401f7f082011-03-15 23:53:31 -0400491 memcpy(mainHeader.reserved2, secondHeader.reserved2, sizeof(mainHeader.reserved2));
srs5694546a9c72010-01-26 16:00:26 -0500492 mainCrcOk = secondCrcOk;
srs56940283dae2010-04-28 16:44:34 -0400493 SetGPTSize(mainHeader.numParts);
srs5694e7b4ff92009-08-18 13:16:10 -0400494} // GPTData::RebuildMainHeader()
495
496// Rebuild the secondary GPT header, using the main header as a model.
497void GPTData::RebuildSecondHeader(void) {
srs5694e7b4ff92009-08-18 13:16:10 -0400498 secondHeader.signature = GPT_SIGNATURE;
499 secondHeader.revision = mainHeader.revision;
srs5694978041c2009-09-21 20:51:47 -0400500 secondHeader.headerSize = mainHeader.headerSize;
srs5694e7b4ff92009-08-18 13:16:10 -0400501 secondHeader.headerCRC = UINT32_C(0);
502 secondHeader.reserved = mainHeader.reserved;
503 secondHeader.currentLBA = mainHeader.backupLBA;
504 secondHeader.backupLBA = mainHeader.currentLBA;
505 secondHeader.firstUsableLBA = mainHeader.firstUsableLBA;
506 secondHeader.lastUsableLBA = mainHeader.lastUsableLBA;
srs56946699b012010-02-04 00:55:30 -0500507 secondHeader.diskGUID = mainHeader.diskGUID;
srs5694e7b4ff92009-08-18 13:16:10 -0400508 secondHeader.partitionEntriesLBA = secondHeader.lastUsableLBA + UINT64_C(1);
509 secondHeader.numParts = mainHeader.numParts;
510 secondHeader.sizeOfPartitionEntries = mainHeader.sizeOfPartitionEntries;
511 secondHeader.partitionEntriesCRC = mainHeader.partitionEntriesCRC;
srs569401f7f082011-03-15 23:53:31 -0400512 memcpy(secondHeader.reserved2, mainHeader.reserved2, sizeof(secondHeader.reserved2));
srs5694546a9c72010-01-26 16:00:26 -0500513 secondCrcOk = mainCrcOk;
srs56940283dae2010-04-28 16:44:34 -0400514 SetGPTSize(secondHeader.numParts);
srs5694e4ac11e2009-08-31 10:13:04 -0400515} // GPTData::RebuildSecondHeader()
516
517// Search for hybrid MBR entries that have no corresponding GPT partition.
518// Returns number of such mismatches found
519int GPTData::FindHybridMismatches(void) {
srs5694e321d442010-01-29 17:44:04 -0500520 int i, found, numFound = 0;
521 uint32_t j;
srs5694e4ac11e2009-08-31 10:13:04 -0400522 uint64_t mbrFirst, mbrLast;
523
524 for (i = 0; i < 4; i++) {
525 if ((protectiveMBR.GetType(i) != 0xEE) && (protectiveMBR.GetType(i) != 0x00)) {
526 j = 0;
527 found = 0;
528 do {
529 mbrFirst = (uint64_t) protectiveMBR.GetFirstSector(i);
530 mbrLast = mbrFirst + (uint64_t) protectiveMBR.GetLength(i) - UINT64_C(1);
531 if ((partitions[j].GetFirstLBA() == mbrFirst) &&
532 (partitions[j].GetLastLBA() == mbrLast))
533 found = 1;
534 j++;
srs56940283dae2010-04-28 16:44:34 -0400535 } while ((!found) && (j < numParts));
srs5694e4ac11e2009-08-31 10:13:04 -0400536 if (!found) {
537 numFound++;
srs5694fed16d02010-01-27 23:03:40 -0500538 cout << "\nWarning! Mismatched GPT and MBR partition! MBR partition "
539 << i + 1 << ", of type 0x";
540 cout.fill('0');
541 cout.setf(ios::uppercase);
542 cout.width(2);
543 cout << hex << (int) protectiveMBR.GetType(i) << ",\n"
544 << "has no corresponding GPT partition! You may continue, but this condition\n"
545 << "might cause data loss in the future!\a\n" << dec;
546 cout.fill(' ');
srs5694e4ac11e2009-08-31 10:13:04 -0400547 } // if
548 } // if
549 } // for
550 return numFound;
551} // GPTData::FindHybridMismatches
552
553// Find overlapping partitions and warn user about them. Returns number of
554// overlapping partitions.
555int GPTData::FindOverlaps(void) {
srs5694e321d442010-01-29 17:44:04 -0500556 int problems = 0;
557 uint32_t i, j;
srs5694e4ac11e2009-08-31 10:13:04 -0400558
srs56940283dae2010-04-28 16:44:34 -0400559 for (i = 1; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -0400560 for (j = 0; j < i; j++) {
srs56940a697312010-01-28 21:10:52 -0500561 if (partitions[i].DoTheyOverlap(partitions[j])) {
srs5694e4ac11e2009-08-31 10:13:04 -0400562 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500563 cout << "\nProblem: partitions " << i + 1 << " and " << j + 1 << " overlap:\n";
564 cout << " Partition " << i + 1 << ": " << partitions[i].GetFirstLBA()
565 << " to " << partitions[i].GetLastLBA() << "\n";
566 cout << " Partition " << j + 1 << ": " << partitions[j].GetFirstLBA()
567 << " to " << partitions[j].GetLastLBA() << "\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400568 } // if
569 } // for j...
570 } // for i...
571 return problems;
572} // GPTData::FindOverlaps()
573
srs569455d92612010-03-07 22:16:07 -0500574// Find partitions that are insane -- they start after they end or are too
575// big for the disk. (The latter should duplicate detection of overlaps
576// with GPT backup data structures, but better to err on the side of
577// redundant tests than to miss something....)
578int GPTData::FindInsanePartitions(void) {
579 uint32_t i;
580 int problems = 0;
581
srs56940283dae2010-04-28 16:44:34 -0400582 for (i = 0; i < numParts; i++) {
srs569455d92612010-03-07 22:16:07 -0500583 if (partitions[i].GetFirstLBA() > partitions[i].GetLastLBA()) {
584 problems++;
srs56940283dae2010-04-28 16:44:34 -0400585 cout << "\nProblem: partition " << i + 1 << " ends before it begins.\n";
srs569455d92612010-03-07 22:16:07 -0500586 } // if
587 if (partitions[i].GetLastLBA() >= diskSize) {
588 problems++;
srs56940873e9d2010-10-07 13:00:45 -0400589 cout << "\nProblem: partition " << i + 1 << " is too big for the disk.\n";
srs569455d92612010-03-07 22:16:07 -0500590 } // if
591 } // for
592 return problems;
593} // GPTData::FindInsanePartitions(void)
594
595
srs5694e4ac11e2009-08-31 10:13:04 -0400596/******************************************************************
597 * *
598 * Begin functions that load data from disk or save data to disk. *
599 * *
600 ******************************************************************/
601
srs569464cbd172011-03-01 22:03:54 -0500602// Change the filename associated with the GPT. Used for duplicating
603// the partition table to a new disk and saving backups.
604// Returns 1 on success, 0 on failure.
srs5694bf8950c2011-03-12 01:23:12 -0500605int GPTData::SetDisk(const string & deviceFilename) {
srs569464cbd172011-03-01 22:03:54 -0500606 int err, allOK = 1;
607
608 device = deviceFilename;
609 if (allOK && myDisk.OpenForRead(deviceFilename)) {
610 // store disk information....
611 diskSize = myDisk.DiskSize(&err);
612 blockSize = (uint32_t) myDisk.GetBlockSize();
613 } // if
614 protectiveMBR.SetDisk(&myDisk);
615 protectiveMBR.SetDiskSize(diskSize);
616 protectiveMBR.SetBlockSize(blockSize);
617 return allOK;
srs5694bf8950c2011-03-12 01:23:12 -0500618} // GPTData::SetDisk()
srs569464cbd172011-03-01 22:03:54 -0500619
srs5694e4ac11e2009-08-31 10:13:04 -0400620// Scan for partition data. This function loads the MBR data (regular MBR or
621// protective MBR) and loads BSD disklabel data (which is probably invalid).
622// It also looks for APM data, forces a load of GPT data, and summarizes
623// the results.
srs5694546a9c72010-01-26 16:00:26 -0500624void GPTData::PartitionScan(void) {
srs5694e4ac11e2009-08-31 10:13:04 -0400625 BSDData bsdDisklabel;
srs5694e4ac11e2009-08-31 10:13:04 -0400626
627 // Read the MBR & check for BSD disklabel
srs5694546a9c72010-01-26 16:00:26 -0500628 protectiveMBR.ReadMBRData(&myDisk);
629 bsdDisklabel.ReadBSDData(&myDisk, 0, diskSize - 1);
srs5694e4ac11e2009-08-31 10:13:04 -0400630
631 // Load the GPT data, whether or not it's valid
srs5694546a9c72010-01-26 16:00:26 -0500632 ForceLoadGPTData();
srs5694ba00fed2010-01-12 18:18:36 -0500633
634 if (!beQuiet) {
srs5694fed16d02010-01-27 23:03:40 -0500635 cout << "Partition table scan:\n";
srs5694ba00fed2010-01-12 18:18:36 -0500636 protectiveMBR.ShowState();
637 bsdDisklabel.ShowState();
638 ShowAPMState(); // Show whether there's an Apple Partition Map present
639 ShowGPTState(); // Show GPT status
srs5694fed16d02010-01-27 23:03:40 -0500640 cout << "\n";
srs5694ba00fed2010-01-12 18:18:36 -0500641 } // if
srs5694e4ac11e2009-08-31 10:13:04 -0400642
643 if (apmFound) {
srs5694fed16d02010-01-27 23:03:40 -0500644 cout << "\n*******************************************************************\n"
645 << "This disk appears to contain an Apple-format (APM) partition table!\n";
srs56945d58fe02010-01-03 20:57:08 -0500646 if (!justLooking) {
srs5694fed16d02010-01-27 23:03:40 -0500647 cout << "It will be destroyed if you continue!\n";
srs56945d58fe02010-01-03 20:57:08 -0500648 } // if
srs5694fed16d02010-01-27 23:03:40 -0500649 cout << "*******************************************************************\n\n\a";
srs5694e4ac11e2009-08-31 10:13:04 -0400650 } // if
srs5694e4ac11e2009-08-31 10:13:04 -0400651} // GPTData::PartitionScan()
652
653// Read GPT data from a disk.
srs56940a697312010-01-28 21:10:52 -0500654int GPTData::LoadPartitions(const string & deviceFilename) {
srs569408bb0da2010-02-19 17:19:55 -0500655 BSDData bsdDisklabel;
srs5694e321d442010-01-29 17:44:04 -0500656 int err, allOK = 1;
srs5694fed16d02010-01-27 23:03:40 -0500657 MBRValidity mbrState;
srs5694e4ac11e2009-08-31 10:13:04 -0400658
srs5694546a9c72010-01-26 16:00:26 -0500659 if (myDisk.OpenForRead(deviceFilename)) {
srs569455d92612010-03-07 22:16:07 -0500660 err = myDisk.OpenForWrite(deviceFilename);
661 if ((err == 0) && (!justLooking)) {
662 cout << "\aNOTE: Write test failed with error number " << errno
663 << ". It will be impossible to save\nchanges to this disk's partition table!\n";
664#if defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
665 cout << "You may be able to enable writes by exiting this program, typing\n"
666 << "'sysctl kern.geom.debugflags=16' at a shell prompt, and re-running this\n"
667 << "program.\n";
668#endif
669 cout << "\n";
670 } // if
671 myDisk.Close(); // Close and re-open read-only in case of bugs
672 } else allOK = 0; // if
673
674 if (allOK && myDisk.OpenForRead(deviceFilename)) {
srs5694e4ac11e2009-08-31 10:13:04 -0400675 // store disk information....
srs5694546a9c72010-01-26 16:00:26 -0500676 diskSize = myDisk.DiskSize(&err);
677 blockSize = (uint32_t) myDisk.GetBlockSize();
srs5694fed16d02010-01-27 23:03:40 -0500678 device = deviceFilename;
srs5694546a9c72010-01-26 16:00:26 -0500679 PartitionScan(); // Check for partition types, load GPT, & print summary
srs5694e4ac11e2009-08-31 10:13:04 -0400680
srs5694ba00fed2010-01-12 18:18:36 -0500681 whichWasUsed = UseWhichPartitions();
682 switch (whichWasUsed) {
srs5694e4ac11e2009-08-31 10:13:04 -0400683 case use_mbr:
684 XFormPartitions();
685 break;
686 case use_bsd:
srs5694546a9c72010-01-26 16:00:26 -0500687 bsdDisklabel.ReadBSDData(&myDisk, 0, diskSize - 1);
srs5694e4ac11e2009-08-31 10:13:04 -0400688// bsdDisklabel.DisplayBSDData();
689 ClearGPTData();
690 protectiveMBR.MakeProtectiveMBR(1); // clear boot area (option 1)
srs569408bb0da2010-02-19 17:19:55 -0500691 XFormDisklabel(&bsdDisklabel);
srs5694e4ac11e2009-08-31 10:13:04 -0400692 break;
693 case use_gpt:
srs5694fed16d02010-01-27 23:03:40 -0500694 mbrState = protectiveMBR.GetValidity();
695 if ((mbrState == invalid) || (mbrState == mbr))
696 protectiveMBR.MakeProtectiveMBR();
srs5694e4ac11e2009-08-31 10:13:04 -0400697 break;
698 case use_new:
699 ClearGPTData();
700 protectiveMBR.MakeProtectiveMBR();
701 break;
srs56943c0af382010-01-15 19:19:18 -0500702 case use_abort:
703 allOK = 0;
srs56949ddc14b2010-08-22 22:44:42 -0400704 cerr << "Invalid partition data!\n";
srs56943c0af382010-01-15 19:19:18 -0500705 break;
srs5694e4ac11e2009-08-31 10:13:04 -0400706 } // switch
707
srs569455d92612010-03-07 22:16:07 -0500708 if (allOK)
srs56943c0af382010-01-15 19:19:18 -0500709 CheckGPTSize();
srs569455d92612010-03-07 22:16:07 -0500710 myDisk.Close();
srs5694a8582cf2010-03-19 14:21:59 -0400711 ComputeAlignment();
srs5694e4ac11e2009-08-31 10:13:04 -0400712 } else {
713 allOK = 0;
srs5694e4ac11e2009-08-31 10:13:04 -0400714 } // if/else
715 return (allOK);
716} // GPTData::LoadPartitions()
717
718// Loads the GPT, as much as possible. Returns 1 if this seems to have
719// succeeded, 0 if there are obvious problems....
srs5694546a9c72010-01-26 16:00:26 -0500720int GPTData::ForceLoadGPTData(void) {
srs5694cb76c672010-02-11 22:22:22 -0500721 int allOK, validHeaders, loadedTable = 1;
srs5694e4ac11e2009-08-31 10:13:04 -0400722
srs5694cb76c672010-02-11 22:22:22 -0500723 allOK = LoadHeader(&mainHeader, myDisk, 1, &mainCrcOk);
srs5694e4ac11e2009-08-31 10:13:04 -0400724
srs5694cb76c672010-02-11 22:22:22 -0500725 if (mainCrcOk && (mainHeader.backupLBA < diskSize)) {
726 allOK = LoadHeader(&secondHeader, myDisk, mainHeader.backupLBA, &secondCrcOk) && allOK;
727 } else {
srs569408bb0da2010-02-19 17:19:55 -0500728 allOK = LoadHeader(&secondHeader, myDisk, diskSize - UINT64_C(1), &secondCrcOk) && allOK;
729 if (mainCrcOk && (mainHeader.backupLBA >= diskSize))
srs5694fed16d02010-01-27 23:03:40 -0500730 cout << "Warning! Disk size is smaller than the main header indicates! Loading\n"
731 << "secondary header from the last sector of the disk! You should use 'v' to\n"
732 << "verify disk integrity, and perhaps options on the experts' menu to repair\n"
733 << "the disk.\n";
srs5694cb76c672010-02-11 22:22:22 -0500734 } // if/else
735 if (!allOK)
srs5694e4ac11e2009-08-31 10:13:04 -0400736 state = gpt_invalid;
srs5694e4ac11e2009-08-31 10:13:04 -0400737
738 // Return valid headers code: 0 = both headers bad; 1 = main header
739 // good, backup bad; 2 = backup header good, main header bad;
740 // 3 = both headers good. Note these codes refer to valid GPT
741 // signatures and version numbers; more subtle problems will elude
742 // this check!
743 validHeaders = CheckHeaderValidity();
744
745 // Read partitions (from primary array)
746 if (validHeaders > 0) { // if at least one header is OK....
747 // GPT appears to be valid....
748 state = gpt_valid;
749
750 // We're calling the GPT valid, but there's a possibility that one
751 // of the two headers is corrupt. If so, use the one that seems to
752 // be in better shape to regenerate the bad one
srs5694546a9c72010-01-26 16:00:26 -0500753 if (validHeaders == 1) { // valid main header, invalid backup header
srs5694fed16d02010-01-27 23:03:40 -0500754 cerr << "\aCaution: invalid backup GPT header, but valid main header; regenerating\n"
755 << "backup header from main header.\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400756 RebuildSecondHeader();
srs5694546a9c72010-01-26 16:00:26 -0500757 state = gpt_corrupt;
srs5694e4ac11e2009-08-31 10:13:04 -0400758 secondCrcOk = mainCrcOk; // Since regenerated, use CRC validity of main
srs5694546a9c72010-01-26 16:00:26 -0500759 } else if (validHeaders == 2) { // valid backup header, invalid main header
srs5694fed16d02010-01-27 23:03:40 -0500760 cerr << "\aCaution: invalid main GPT header, but valid backup; regenerating main header\n"
761 << "from backup!\n\n";
srs5694546a9c72010-01-26 16:00:26 -0500762 RebuildMainHeader();
763 state = gpt_corrupt;
764 mainCrcOk = secondCrcOk; // Since copied, use CRC validity of backup
srs5694e4ac11e2009-08-31 10:13:04 -0400765 } // if/else/if
766
srs5694546a9c72010-01-26 16:00:26 -0500767 // Figure out which partition table to load....
768 // Load the main partition table, since either its header's CRC is OK or the
769 // backup header's CRC is not OK....
770 if (mainCrcOk || !secondCrcOk) {
771 if (LoadMainTable() == 0)
772 allOK = 0;
773 } else { // bad main header CRC and backup header CRC is OK
774 state = gpt_corrupt;
775 if (LoadSecondTableAsMain()) {
srs5694cb76c672010-02-11 22:22:22 -0500776 loadedTable = 2;
srs5694fed16d02010-01-27 23:03:40 -0500777 cerr << "\aWarning: Invalid CRC on main header data; loaded backup partition table.\n";
srs5694546a9c72010-01-26 16:00:26 -0500778 } else { // backup table bad, bad main header CRC, but try main table in desperation....
779 if (LoadMainTable() == 0) {
780 allOK = 0;
srs5694cb76c672010-02-11 22:22:22 -0500781 loadedTable = 0;
srs5694fed16d02010-01-27 23:03:40 -0500782 cerr << "\a\aWarning! Unable to load either main or backup partition table!\n";
srs5694546a9c72010-01-26 16:00:26 -0500783 } // if
784 } // if/else (LoadSecondTableAsMain())
785 } // if/else (load partition table)
srs5694e4ac11e2009-08-31 10:13:04 -0400786
srs5694cb76c672010-02-11 22:22:22 -0500787 if (loadedTable == 1)
788 secondPartsCrcOk = CheckTable(&secondHeader);
789 else if (loadedTable == 2)
790 mainPartsCrcOk = CheckTable(&mainHeader);
791 else
792 mainPartsCrcOk = secondPartsCrcOk = 0;
srs5694e4ac11e2009-08-31 10:13:04 -0400793
srs5694546a9c72010-01-26 16:00:26 -0500794 // Problem with main partition table; if backup is OK, use it instead....
795 if (secondPartsCrcOk && secondCrcOk && !mainPartsCrcOk) {
796 state = gpt_corrupt;
797 allOK = allOK && LoadSecondTableAsMain();
srs5694cb76c672010-02-11 22:22:22 -0500798 mainPartsCrcOk = 0; // LoadSecondTableAsMain() resets this, so re-flag as bad
srs5694fed16d02010-01-27 23:03:40 -0500799 cerr << "\aWarning! Main partition table CRC mismatch! Loaded backup "
800 << "partition table\ninstead of main partition table!\n\n";
srs5694cb76c672010-02-11 22:22:22 -0500801 } // if */
srs5694546a9c72010-01-26 16:00:26 -0500802
srs5694e4ac11e2009-08-31 10:13:04 -0400803 // Check for valid CRCs and warn if there are problems
804 if ((mainCrcOk == 0) || (secondCrcOk == 0) || (mainPartsCrcOk == 0) ||
805 (secondPartsCrcOk == 0)) {
srs5694fed16d02010-01-27 23:03:40 -0500806 cerr << "Warning! One or more CRCs don't match. You should repair the disk!\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400807 state = gpt_corrupt;
srs5694ba00fed2010-01-12 18:18:36 -0500808 } // if
srs5694e4ac11e2009-08-31 10:13:04 -0400809 } else {
810 state = gpt_invalid;
811 } // if/else
812 return allOK;
813} // GPTData::ForceLoadGPTData()
814
srs5694247657a2009-11-26 18:36:12 -0500815// Loads the partition table pointed to by the main GPT header. The
srs5694e4ac11e2009-08-31 10:13:04 -0400816// main GPT header in memory MUST be valid for this call to do anything
817// sensible!
srs5694546a9c72010-01-26 16:00:26 -0500818// Returns 1 on success, 0 on failure. CRC errors do NOT count as failure.
srs5694e4ac11e2009-08-31 10:13:04 -0400819int GPTData::LoadMainTable(void) {
srs5694cb76c672010-02-11 22:22:22 -0500820 return LoadPartitionTable(mainHeader, myDisk);
srs5694e4ac11e2009-08-31 10:13:04 -0400821} // GPTData::LoadMainTable()
srs5694e7b4ff92009-08-18 13:16:10 -0400822
823// Load the second (backup) partition table as the primary partition
srs5694546a9c72010-01-26 16:00:26 -0500824// table. Used in repair functions, and when starting up if the main
825// partition table is damaged.
826// Returns 1 on success, 0 on failure. CRC errors do NOT count as failure.
827int GPTData::LoadSecondTableAsMain(void) {
srs5694cb76c672010-02-11 22:22:22 -0500828 return LoadPartitionTable(secondHeader, myDisk);
829} // GPTData::LoadSecondTableAsMain()
srs5694e7b4ff92009-08-18 13:16:10 -0400830
srs5694cb76c672010-02-11 22:22:22 -0500831// Load a single GPT header (main or backup) from the specified disk device and
832// sector. Applies byte-order corrections on big-endian platforms. Sets crcOk
833// value appropriately.
834// Returns 1 on success, 0 on failure. Note that CRC errors do NOT qualify as
835// failure.
836int GPTData::LoadHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector, int *crcOk) {
837 int allOK = 1;
srs56941c6f8b02010-02-21 11:09:20 -0500838 GPTHeader tempHeader;
srs5694cb76c672010-02-11 22:22:22 -0500839
840 disk.Seek(sector);
srs56941c6f8b02010-02-21 11:09:20 -0500841 if (disk.Read(&tempHeader, 512) != 512) {
srs5694cb76c672010-02-11 22:22:22 -0500842 cerr << "Warning! Read error " << errno << "; strange behavior now likely!\n";
843 allOK = 0;
844 } // if
srs56941c6f8b02010-02-21 11:09:20 -0500845 *crcOk = CheckHeaderCRC(&tempHeader);
srs5694cb76c672010-02-11 22:22:22 -0500846
srs56941c6f8b02010-02-21 11:09:20 -0500847 // Reverse byte order, if necessary
srs5694cb76c672010-02-11 22:22:22 -0500848 if (IsLittleEndian() == 0) {
srs569455d92612010-03-07 22:16:07 -0500849 ReverseHeaderBytes(&tempHeader);
srs5694cb76c672010-02-11 22:22:22 -0500850 } // if
srs56941c6f8b02010-02-21 11:09:20 -0500851
srs56940283dae2010-04-28 16:44:34 -0400852 if (allOK && (numParts != tempHeader.numParts) && *crcOk) {
srs56941c6f8b02010-02-21 11:09:20 -0500853 allOK = SetGPTSize(tempHeader.numParts);
srs569455d92612010-03-07 22:16:07 -0500854 }
srs56941c6f8b02010-02-21 11:09:20 -0500855
856 *header = tempHeader;
srs5694cb76c672010-02-11 22:22:22 -0500857 return allOK;
858} // GPTData::LoadHeader
859
860// Load a partition table (either main or secondary) from the specified disk,
861// using header as a reference for what to load. If sector != 0 (the default
862// is 0), loads from the specified sector; otherwise loads from the sector
863// indicated in header.
864// Returns 1 on success, 0 on failure. CRC errors do NOT count as failure.
865int GPTData::LoadPartitionTable(const struct GPTHeader & header, DiskIO & disk, uint64_t sector) {
866 uint32_t sizeOfParts, newCRC;
867 int retval;
868
869 if (disk.OpenForRead()) {
870 if (sector == 0) {
871 retval = disk.Seek(header.partitionEntriesLBA);
872 } else {
873 retval = disk.Seek(sector);
874 } // if/else
srs569455d92612010-03-07 22:16:07 -0500875 if (retval == 1)
876 retval = SetGPTSize(header.numParts);
srs5694546a9c72010-01-26 16:00:26 -0500877 if (retval == 1) {
srs5694cb76c672010-02-11 22:22:22 -0500878 sizeOfParts = header.numParts * header.sizeOfPartitionEntries;
879 if (disk.Read(partitions, sizeOfParts) != (int) sizeOfParts) {
srs5694fed16d02010-01-27 23:03:40 -0500880 cerr << "Warning! Read error " << errno << "! Misbehavior now likely!\n";
srs5694546a9c72010-01-26 16:00:26 -0500881 retval = 0;
srs56945d58fe02010-01-03 20:57:08 -0500882 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400883 newCRC = chksum_crc32((unsigned char*) partitions, sizeOfParts);
srs5694cb76c672010-02-11 22:22:22 -0500884 mainPartsCrcOk = secondPartsCrcOk = (newCRC == header.partitionEntriesCRC);
srs56942a9f5da2009-08-26 00:48:01 -0400885 if (IsLittleEndian() == 0)
886 ReversePartitionBytes();
srs5694cb76c672010-02-11 22:22:22 -0500887 if (!mainPartsCrcOk) {
888 cout << "Caution! After loading partitions, the CRC doesn't check out!\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400889 } // if
890 } else {
srs5694cb76c672010-02-11 22:22:22 -0500891 cerr << "Error! Couldn't seek to partition table!\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400892 } // if/else
893 } else {
srs5694fed16d02010-01-27 23:03:40 -0500894 cerr << "Error! Couldn't open device " << device
srs5694cb76c672010-02-11 22:22:22 -0500895 << " when reading partition table!\n";
srs5694546a9c72010-01-26 16:00:26 -0500896 retval = 0;
srs5694e7b4ff92009-08-18 13:16:10 -0400897 } // if/else
srs5694546a9c72010-01-26 16:00:26 -0500898 return retval;
srs5694cb76c672010-02-11 22:22:22 -0500899} // GPTData::LoadPartitionsTable()
900
901// Check the partition table pointed to by header, but don't keep it
902// around.
903// Returns 1 if the CRC is OK, 0 if not or if there was a read error.
904int GPTData::CheckTable(struct GPTHeader *header) {
905 uint32_t sizeOfParts, newCRC;
906 uint8_t *storage;
907 int newCrcOk = 0;
908
srs56940283dae2010-04-28 16:44:34 -0400909 // Load partition table into temporary storage to check
srs5694cb76c672010-02-11 22:22:22 -0500910 // its CRC and store the results, then discard this temporary
911 // storage, since we don't use it in any but recovery operations
912 if (myDisk.Seek(header->partitionEntriesLBA)) {
srs56940283dae2010-04-28 16:44:34 -0400913 sizeOfParts = header->numParts * header->sizeOfPartitionEntries;
srs5694cb76c672010-02-11 22:22:22 -0500914 storage = new uint8_t[sizeOfParts];
915 if (myDisk.Read(storage, sizeOfParts) != (int) sizeOfParts) {
srs56940283dae2010-04-28 16:44:34 -0400916 cerr << "Warning! Error " << errno << " reading partition table for CRC check!\n";
srs5694cb76c672010-02-11 22:22:22 -0500917 } else {
918 newCRC = chksum_crc32((unsigned char*) storage, sizeOfParts);
919 newCrcOk = (newCRC == header->partitionEntriesCRC);
920 } // if/else
921 delete[] storage;
922 } // if
923 return newCrcOk;
924} // GPTData::CheckTable()
srs5694e7b4ff92009-08-18 13:16:10 -0400925
srs569464cbd172011-03-01 22:03:54 -0500926// Writes GPT (and protective MBR) to disk. If quiet==1,
927// Returns 1 on successful
srs5694e7b4ff92009-08-18 13:16:10 -0400928// write, 0 if there was a problem.
srs569464cbd172011-03-01 22:03:54 -0500929int GPTData::SaveGPTData(int quiet) {
srs56946699b012010-02-04 00:55:30 -0500930 int allOK = 1, littleEndian;
srs5694e321d442010-01-29 17:44:04 -0500931 char answer;
srs5694e7b4ff92009-08-18 13:16:10 -0400932
srs56946699b012010-02-04 00:55:30 -0500933 littleEndian = IsLittleEndian();
934
srs5694e7b4ff92009-08-18 13:16:10 -0400935 // First do some final sanity checks....
srs56945d58fe02010-01-03 20:57:08 -0500936
937 // This test should only fail on read-only disks....
938 if (justLooking) {
srs5694fed16d02010-01-27 23:03:40 -0500939 cout << "The justLooking flag is set. This probably means you can't write to the disk.\n";
srs56945d58fe02010-01-03 20:57:08 -0500940 allOK = 0;
941 } // if
942
srs569464cbd172011-03-01 22:03:54 -0500943 // Check that disk is really big enough to handle the second header...
944 if (mainHeader.backupLBA >= diskSize) {
945 cerr << "Caution! Secondary header was placed beyond the disk's limits! Moving the\n"
946 << "header, but other problems may occur!\n";
947 MoveSecondHeaderToEnd();
948 } // if
949
srs5694e7b4ff92009-08-18 13:16:10 -0400950 // Is there enough space to hold the GPT headers and partition tables,
951 // given the partition sizes?
srs5694221e0872009-08-29 15:00:31 -0400952 if (CheckGPTSize() > 0) {
srs5694e7b4ff92009-08-18 13:16:10 -0400953 allOK = 0;
954 } // if
955
srs5694247657a2009-11-26 18:36:12 -0500956 // Check that second header is properly placed. Warn and ask if this should
957 // be corrected if the test fails....
srs569464cbd172011-03-01 22:03:54 -0500958 if (mainHeader.backupLBA < (diskSize - UINT64_C(1))) {
959 if (quiet == 0) {
960 cout << "Warning! Secondary header is placed too early on the disk! Do you want to\n"
961 << "correct this problem? ";
962 if (GetYN() == 'Y') {
963 MoveSecondHeaderToEnd();
964 cout << "Have moved second header and partition table to correct location.\n";
965 } else {
966 cout << "Have not corrected the problem. Strange problems may occur in the future!\n";
967 } // if correction requested
968 } else { // Go ahead and do correction automatically
srs5694247657a2009-11-26 18:36:12 -0500969 MoveSecondHeaderToEnd();
srs569464cbd172011-03-01 22:03:54 -0500970 } // if/else quiet
srs5694247657a2009-11-26 18:36:12 -0500971 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400972
srs569455d92612010-03-07 22:16:07 -0500973 // Check for overlapping or insane partitions....
974 if ((FindOverlaps() > 0) || (FindInsanePartitions() > 0)) {
srs5694e4ac11e2009-08-31 10:13:04 -0400975 allOK = 0;
srs5694fed16d02010-01-27 23:03:40 -0500976 cerr << "Aborting write operation!\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400977 } // if
978
979 // Check for mismatched MBR and GPT data, but let it pass if found
980 // (function displays warning message)
981 FindHybridMismatches();
srs5694e7b4ff92009-08-18 13:16:10 -0400982
983 RecomputeCRCs();
984
srs5694ba00fed2010-01-12 18:18:36 -0500985 if ((allOK) && (!quiet)) {
srs5694fed16d02010-01-27 23:03:40 -0500986 cout << "\nFinal checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING\n"
srs5694bf8950c2011-03-12 01:23:12 -0500987 << "PARTITIONS!!\n\nDo you want to proceed? ";
srs56945d58fe02010-01-03 20:57:08 -0500988 answer = GetYN();
989 if (answer == 'Y') {
srs5694fed16d02010-01-27 23:03:40 -0500990 cout << "OK; writing new GUID partition table (GPT).\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400991 } else {
992 allOK = 0;
993 } // if/else
994 } // if
995
996 // Do it!
997 if (allOK) {
srs569464cbd172011-03-01 22:03:54 -0500998 if (myDisk.OpenForWrite()) {
srs56948a4ddfc2010-03-21 19:05:49 -0400999 // As per UEFI specs, write the secondary table and GPT first....
srs5694cb76c672010-02-11 22:22:22 -05001000 allOK = SavePartitionTable(myDisk, secondHeader.partitionEntriesLBA);
1001 if (!allOK)
1002 cerr << "Unable to save backup partition table! Perhaps the 'e' option on the experts'\n"
1003 << "menu will resolve this problem.\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001004
1005 // Now write the secondary GPT header...
srs56948a4ddfc2010-03-21 19:05:49 -04001006 allOK = allOK && SaveHeader(&secondHeader, myDisk, mainHeader.backupLBA);
1007
1008 // Now write the main partition tables...
1009 allOK = allOK && SavePartitionTable(myDisk, mainHeader.partitionEntriesLBA);
1010
1011 // Now write the main GPT header...
1012 allOK = allOK && SaveHeader(&mainHeader, myDisk, 1);
1013
1014 // To top it off, write the protective MBR...
1015 allOK = allOK && protectiveMBR.WriteMBRData(&myDisk);
srs5694e7b4ff92009-08-18 13:16:10 -04001016
1017 // re-read the partition table
1018 if (allOK) {
srs5694546a9c72010-01-26 16:00:26 -05001019 myDisk.DiskSync();
srs5694e7b4ff92009-08-18 13:16:10 -04001020 } // if
1021
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 partition table! This error\n"
srs56948a4ddfc2010-03-21 19:05:49 -04001026 << "MIGHT be harmless, but you may have trashed the disk!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001027 } // if/else
srs56948a4ddfc2010-03-21 19:05:49 -04001028
srs5694546a9c72010-01-26 16:00:26 -05001029 myDisk.Close();
srs5694e7b4ff92009-08-18 13:16:10 -04001030 } else {
srs56945a608532011-03-17 13:53:01 -04001031 cerr << "Unable to open device '" << myDisk.GetName() << "' for writing! Errno is "
srs5694fed16d02010-01-27 23:03:40 -05001032 << errno << "! Aborting write!\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001033 allOK = 0;
srs5694e7b4ff92009-08-18 13:16:10 -04001034 } // if/else
1035 } else {
srs5694fed16d02010-01-27 23:03:40 -05001036 cout << "Aborting write of new partition table.\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001037 } // if
1038
1039 return (allOK);
1040} // GPTData::SaveGPTData()
1041
1042// Save GPT data to a backup file. This function does much less error
1043// checking than SaveGPTData(). It can therefore preserve many types of
1044// corruption for later analysis; however, it preserves only the MBR,
1045// the main GPT header, the backup GPT header, and the main partition
1046// table; it discards the backup partition table, since it should be
1047// identical to the main partition table on healthy disks.
srs56940a697312010-01-28 21:10:52 -05001048int GPTData::SaveGPTBackup(const string & filename) {
1049 int allOK = 1;
srs5694546a9c72010-01-26 16:00:26 -05001050 DiskIO backupFile;
srs5694e7b4ff92009-08-18 13:16:10 -04001051
srs5694546a9c72010-01-26 16:00:26 -05001052 if (backupFile.OpenForWrite(filename)) {
srs56946699b012010-02-04 00:55:30 -05001053 // Recomputing the CRCs is likely to alter them, which could be bad
1054 // if the intent is to save a potentially bad GPT for later analysis;
1055 // but if we don't do this, we get bogus errors when we load the
1056 // backup. I'm favoring misses over false alarms....
1057 RecomputeCRCs();
1058
srs5694546a9c72010-01-26 16:00:26 -05001059 protectiveMBR.WriteMBRData(&backupFile);
srs5694e7b4ff92009-08-18 13:16:10 -04001060
srs5694cb76c672010-02-11 22:22:22 -05001061 if (allOK) {
srs5694546a9c72010-01-26 16:00:26 -05001062 // MBR write closed disk, so re-open and seek to end....
1063 backupFile.OpenForWrite();
srs5694cb76c672010-02-11 22:22:22 -05001064 allOK = SaveHeader(&mainHeader, backupFile, 1);
1065 } // if (allOK)
srs5694e7b4ff92009-08-18 13:16:10 -04001066
srs5694e7b4ff92009-08-18 13:16:10 -04001067 if (allOK)
srs5694cb76c672010-02-11 22:22:22 -05001068 allOK = SaveHeader(&secondHeader, backupFile, 2);
srs5694e7b4ff92009-08-18 13:16:10 -04001069
srs5694cb76c672010-02-11 22:22:22 -05001070 if (allOK)
1071 allOK = SavePartitionTable(backupFile, 3);
srs5694e7b4ff92009-08-18 13:16:10 -04001072
1073 if (allOK) { // writes completed OK
srs5694fed16d02010-01-27 23:03:40 -05001074 cout << "The operation has completed successfully.\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001075 } else {
srs5694fed16d02010-01-27 23:03:40 -05001076 cerr << "Warning! An error was reported when writing the backup file.\n"
1077 << "It may not be usable!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001078 } // if/else
srs5694546a9c72010-01-26 16:00:26 -05001079 backupFile.Close();
srs5694e7b4ff92009-08-18 13:16:10 -04001080 } else {
srs56945a608532011-03-17 13:53:01 -04001081 cerr << "Unable to open file '" << filename << "' for writing! Aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001082 allOK = 0;
1083 } // if/else
1084 return allOK;
1085} // GPTData::SaveGPTBackup()
1086
srs5694cb76c672010-02-11 22:22:22 -05001087// Write a GPT header (main or backup) to the specified sector. Used by both
1088// the SaveGPTData() and SaveGPTBackup() functions.
1089// Should be passed an architecture-appropriate header (DO NOT call
1090// ReverseHeaderBytes() on the header before calling this function)
1091// Returns 1 on success, 0 on failure
1092int GPTData::SaveHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector) {
1093 int littleEndian, allOK = 1;
1094
1095 littleEndian = IsLittleEndian();
1096 if (!littleEndian)
1097 ReverseHeaderBytes(header);
1098 if (disk.Seek(sector)) {
1099 if (disk.Write(header, 512) == -1)
1100 allOK = 0;
1101 } else allOK = 0; // if (disk.Seek()...)
1102 if (!littleEndian)
1103 ReverseHeaderBytes(header);
1104 return allOK;
1105} // GPTData::SaveHeader()
1106
1107// Save the partitions to the specified sector. Used by both the SaveGPTData()
1108// and SaveGPTBackup() functions.
1109// Should be passed an architecture-appropriate header (DO NOT call
1110// ReverseHeaderBytes() on the header before calling this function)
1111// Returns 1 on success, 0 on failure
1112int GPTData::SavePartitionTable(DiskIO & disk, uint64_t sector) {
1113 int littleEndian, allOK = 1;
1114
1115 littleEndian = IsLittleEndian();
1116 if (disk.Seek(sector)) {
1117 if (!littleEndian)
1118 ReversePartitionBytes();
srs56940283dae2010-04-28 16:44:34 -04001119 if (disk.Write(partitions, mainHeader.sizeOfPartitionEntries * numParts) == -1)
srs5694cb76c672010-02-11 22:22:22 -05001120 allOK = 0;
1121 if (!littleEndian)
1122 ReversePartitionBytes();
1123 } else allOK = 0; // if (myDisk.Seek()...)
1124 return allOK;
1125} // GPTData::SavePartitionTable()
1126
srs5694e7b4ff92009-08-18 13:16:10 -04001127// Load GPT data from a backup file created by SaveGPTBackup(). This function
1128// does minimal error checking. It returns 1 if it completed successfully,
1129// 0 if there was a problem. In the latter case, it creates a new empty
1130// set of partitions.
srs56940a697312010-01-28 21:10:52 -05001131int GPTData::LoadGPTBackup(const string & filename) {
srs5694cb76c672010-02-11 22:22:22 -05001132 int allOK = 1, val, err;
srs56940283dae2010-04-28 16:44:34 -04001133 uint32_t sizeOfEntries;
srs5694cb76c672010-02-11 22:22:22 -05001134 int littleEndian = 1, shortBackup = 0;
srs5694546a9c72010-01-26 16:00:26 -05001135 DiskIO backupFile;
srs5694e7b4ff92009-08-18 13:16:10 -04001136
srs5694546a9c72010-01-26 16:00:26 -05001137 if (backupFile.OpenForRead(filename)) {
srs56942a9f5da2009-08-26 00:48:01 -04001138 if (IsLittleEndian() == 0)
1139 littleEndian = 0;
1140
srs5694e7b4ff92009-08-18 13:16:10 -04001141 // Let the MBRData class load the saved MBR...
srs5694546a9c72010-01-26 16:00:26 -05001142 protectiveMBR.ReadMBRData(&backupFile, 0); // 0 = don't check block size
srs5694815fb652011-03-18 12:35:56 -04001143 protectiveMBR.SetDisk(&myDisk);
srs5694e7b4ff92009-08-18 13:16:10 -04001144
srs5694cb76c672010-02-11 22:22:22 -05001145 LoadHeader(&mainHeader, backupFile, 1, &mainCrcOk);
srs5694e7b4ff92009-08-18 13:16:10 -04001146
srs5694cb76c672010-02-11 22:22:22 -05001147 // Check backup file size and rebuild second header if file is right
1148 // size to be direct dd copy of MBR, main header, and main partition
1149 // table; if other size, treat it like a GPT fdisk-generated backup
1150 // file
1151 shortBackup = ((backupFile.DiskSize(&err) * backupFile.GetBlockSize()) ==
1152 (mainHeader.numParts * mainHeader.sizeOfPartitionEntries) + 1024);
1153 if (shortBackup) {
1154 RebuildSecondHeader();
1155 secondCrcOk = mainCrcOk;
1156 } else {
1157 LoadHeader(&secondHeader, backupFile, 2, &secondCrcOk);
1158 } // if/else
srs56942a9f5da2009-08-26 00:48:01 -04001159
srs5694e7b4ff92009-08-18 13:16:10 -04001160 // Return valid headers code: 0 = both headers bad; 1 = main header
1161 // good, backup bad; 2 = backup header good, main header bad;
1162 // 3 = both headers good. Note these codes refer to valid GPT
1163 // signatures and version numbers; more subtle problems will elude
1164 // this check!
1165 if ((val = CheckHeaderValidity()) > 0) {
1166 if (val == 2) { // only backup header seems to be good
srs56940283dae2010-04-28 16:44:34 -04001167 SetGPTSize(secondHeader.numParts);
srs5694e4ac11e2009-08-31 10:13:04 -04001168 sizeOfEntries = secondHeader.sizeOfPartitionEntries;
srs5694e7b4ff92009-08-18 13:16:10 -04001169 } else { // main header is OK
srs56940283dae2010-04-28 16:44:34 -04001170 SetGPTSize(mainHeader.numParts);
srs5694e7b4ff92009-08-18 13:16:10 -04001171 sizeOfEntries = mainHeader.sizeOfPartitionEntries;
1172 } // if/else
1173
srs5694e7b4ff92009-08-18 13:16:10 -04001174 if (secondHeader.currentLBA != diskSize - UINT64_C(1)) {
srs5694fed16d02010-01-27 23:03:40 -05001175 cout << "Warning! Current disk size doesn't match that of the backup!\n"
1176 << "Adjusting sizes to match, but subsequent problems are possible!\n";
srs5694247657a2009-11-26 18:36:12 -05001177 MoveSecondHeaderToEnd();
srs5694e7b4ff92009-08-18 13:16:10 -04001178 } // if
1179
srs5694cb76c672010-02-11 22:22:22 -05001180 if (!LoadPartitionTable(mainHeader, backupFile, (uint64_t) (3 - shortBackup)))
1181 cerr << "Warning! Read error " << errno
1182 << " loading partition table; strange behavior now likely!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001183 } else {
1184 allOK = 0;
1185 } // if/else
srs5694a8582cf2010-03-19 14:21:59 -04001186 // Something went badly wrong, so blank out partitions
1187 if (allOK == 0) {
1188 cerr << "Improper backup file! Clearing all partition data!\n";
1189 ClearGPTData();
1190 protectiveMBR.MakeProtectiveMBR();
1191 } // if
srs5694e7b4ff92009-08-18 13:16:10 -04001192 } else {
1193 allOK = 0;
srs56945a608532011-03-17 13:53:01 -04001194 cerr << "Unable to open file '" << filename << "' for reading! Aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001195 } // if/else
1196
srs5694e7b4ff92009-08-18 13:16:10 -04001197 return allOK;
1198} // GPTData::LoadGPTBackup()
1199
srs569408bb0da2010-02-19 17:19:55 -05001200int GPTData::SaveMBR(void) {
srs569455d92612010-03-07 22:16:07 -05001201 return protectiveMBR.WriteMBRData(&myDisk);
srs569408bb0da2010-02-19 17:19:55 -05001202} // GPTData::SaveMBR()
1203
1204// This function destroys the on-disk GPT structures, but NOT the on-disk
1205// MBR.
1206// Returns 1 if the operation succeeds, 0 if not.
1207int GPTData::DestroyGPT(void) {
srs569401f7f082011-03-15 23:53:31 -04001208 int sum, tableSize, allOK = 1;
srs569408bb0da2010-02-19 17:19:55 -05001209 uint8_t blankSector[512];
1210 uint8_t* emptyTable;
1211
srs569401f7f082011-03-15 23:53:31 -04001212 memset(blankSector, 0, sizeof(blankSector));
srs569408bb0da2010-02-19 17:19:55 -05001213
1214 if (myDisk.OpenForWrite()) {
1215 if (!myDisk.Seek(mainHeader.currentLBA))
1216 allOK = 0;
1217 if (myDisk.Write(blankSector, 512) != 512) { // blank it out
1218 cerr << "Warning! GPT main header not overwritten! Error is " << errno << "\n";
1219 allOK = 0;
1220 } // if
1221 if (!myDisk.Seek(mainHeader.partitionEntriesLBA))
1222 allOK = 0;
srs56940283dae2010-04-28 16:44:34 -04001223 tableSize = numParts * mainHeader.sizeOfPartitionEntries;
srs569408bb0da2010-02-19 17:19:55 -05001224 emptyTable = new uint8_t[tableSize];
srs569401f7f082011-03-15 23:53:31 -04001225 memset(emptyTable, 0, tableSize);
srs569408bb0da2010-02-19 17:19:55 -05001226 if (allOK) {
1227 sum = myDisk.Write(emptyTable, tableSize);
1228 if (sum != tableSize) {
1229 cerr << "Warning! GPT main partition table not overwritten! Error is " << errno << "\n";
1230 allOK = 0;
1231 } // if write failed
1232 } // if
1233 if (!myDisk.Seek(secondHeader.partitionEntriesLBA))
1234 allOK = 0;
1235 if (allOK) {
1236 sum = myDisk.Write(emptyTable, tableSize);
1237 if (sum != tableSize) {
1238 cerr << "Warning! GPT backup partition table not overwritten! Error is "
1239 << errno << "\n";
1240 allOK = 0;
1241 } // if wrong size written
1242 } // if
1243 if (!myDisk.Seek(secondHeader.currentLBA))
1244 allOK = 0;
1245 if (allOK) {
1246 if (myDisk.Write(blankSector, 512) != 512) { // blank it out
1247 cerr << "Warning! GPT backup header not overwritten! Error is " << errno << "\n";
1248 allOK = 0;
1249 } // if
1250 } // if
1251 myDisk.DiskSync();
1252 myDisk.Close();
1253 cout << "GPT data structures destroyed! You may now partition the disk using fdisk or\n"
1254 << "other utilities.\n";
1255 delete[] emptyTable;
1256 } else {
srs56945a608532011-03-17 13:53:01 -04001257 cerr << "Problem opening '" << device << "' for writing! Program will now terminate.\n";
srs569408bb0da2010-02-19 17:19:55 -05001258 } // if/else (fd != -1)
1259 return (allOK);
1260} // GPTDataTextUI::DestroyGPT()
1261
1262// Wipe MBR data from the disk (zero it out completely)
1263// Returns 1 on success, 0 on failure.
1264int GPTData::DestroyMBR(void) {
srs569401f7f082011-03-15 23:53:31 -04001265 int allOK;
srs569408bb0da2010-02-19 17:19:55 -05001266 uint8_t blankSector[512];
1267
srs569401f7f082011-03-15 23:53:31 -04001268 memset(blankSector, 0, sizeof(blankSector));
srs569408bb0da2010-02-19 17:19:55 -05001269
srs569401f7f082011-03-15 23:53:31 -04001270 allOK = myDisk.OpenForWrite() && myDisk.Seek(0) && (myDisk.Write(blankSector, 512) == 512);
1271
srs569408bb0da2010-02-19 17:19:55 -05001272 if (!allOK)
1273 cerr << "Warning! MBR not overwritten! Error is " << errno << "!\n";
1274 return allOK;
1275} // GPTData::DestroyMBR(void)
1276
srs5694e4ac11e2009-08-31 10:13:04 -04001277// Tell user whether Apple Partition Map (APM) was discovered....
1278void GPTData::ShowAPMState(void) {
1279 if (apmFound)
srs5694fed16d02010-01-27 23:03:40 -05001280 cout << " APM: present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001281 else
srs5694fed16d02010-01-27 23:03:40 -05001282 cout << " APM: not present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001283} // GPTData::ShowAPMState()
1284
1285// Tell user about the state of the GPT data....
1286void GPTData::ShowGPTState(void) {
1287 switch (state) {
1288 case gpt_invalid:
srs5694fed16d02010-01-27 23:03:40 -05001289 cout << " GPT: not present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001290 break;
1291 case gpt_valid:
srs5694fed16d02010-01-27 23:03:40 -05001292 cout << " GPT: present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001293 break;
1294 case gpt_corrupt:
srs5694fed16d02010-01-27 23:03:40 -05001295 cout << " GPT: damaged\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001296 break;
1297 default:
srs5694fed16d02010-01-27 23:03:40 -05001298 cout << "\a GPT: unknown -- bug!\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001299 break;
1300 } // switch
1301} // GPTData::ShowGPTState()
1302
1303// Display the basic GPT data
1304void GPTData::DisplayGPTData(void) {
srs5694e321d442010-01-29 17:44:04 -05001305 uint32_t i;
srs5694e4ac11e2009-08-31 10:13:04 -04001306 uint64_t temp, totalFree;
1307
srs5694fed16d02010-01-27 23:03:40 -05001308 cout << "Disk " << device << ": " << diskSize << " sectors, "
srs569401f7f082011-03-15 23:53:31 -04001309 << BytesToIeee(diskSize, blockSize) << "\n";
srs5694fed16d02010-01-27 23:03:40 -05001310 cout << "Logical sector size: " << blockSize << " bytes\n";
srs56945a081752010-09-24 20:39:41 -04001311 cout << "Disk identifier (GUID): " << mainHeader.diskGUID << "\n";
srs56940283dae2010-04-28 16:44:34 -04001312 cout << "Partition table holds up to " << numParts << " entries\n";
srs5694fed16d02010-01-27 23:03:40 -05001313 cout << "First usable sector is " << mainHeader.firstUsableLBA
1314 << ", last usable sector is " << mainHeader.lastUsableLBA << "\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001315 totalFree = FindFreeBlocks(&i, &temp);
srs56948a4ddfc2010-03-21 19:05:49 -04001316 cout << "Partitions will be aligned on " << sectorAlignment << "-sector boundaries\n";
srs5694fed16d02010-01-27 23:03:40 -05001317 cout << "Total free space is " << totalFree << " sectors ("
srs569401f7f082011-03-15 23:53:31 -04001318 << BytesToIeee(totalFree, blockSize) << ")\n";
srs5694fed16d02010-01-27 23:03:40 -05001319 cout << "\nNumber Start (sector) End (sector) Size Code Name\n";
srs56940283dae2010-04-28 16:44:34 -04001320 for (i = 0; i < numParts; i++) {
srs5694978041c2009-09-21 20:51:47 -04001321 partitions[i].ShowSummary(i, blockSize);
srs5694e4ac11e2009-08-31 10:13:04 -04001322 } // for
1323} // GPTData::DisplayGPTData()
1324
srs5694e4ac11e2009-08-31 10:13:04 -04001325// Show detailed information on the specified partition
1326void GPTData::ShowPartDetails(uint32_t partNum) {
srs56940873e9d2010-10-07 13:00:45 -04001327 if (!IsFreePartNum(partNum)) {
srs5694e4ac11e2009-08-31 10:13:04 -04001328 partitions[partNum].ShowDetails(blockSize);
1329 } else {
srs5694fed16d02010-01-27 23:03:40 -05001330 cout << "Partition #" << partNum + 1 << " does not exist.";
srs5694e4ac11e2009-08-31 10:13:04 -04001331 } // if
1332} // GPTData::ShowPartDetails()
1333
srs5694e4ac11e2009-08-31 10:13:04 -04001334/**************************************************************************
1335 * *
1336 * Partition table transformation functions (MBR or BSD disklabel to GPT) *
1337 * (some of these functions may require user interaction) *
1338 * *
1339 **************************************************************************/
1340
srs569408bb0da2010-02-19 17:19:55 -05001341// Examines the MBR & GPT data to determine which set of data to use: the
1342// MBR (use_mbr), the GPT (use_gpt), the BSD disklabel (use_bsd), or create
1343// a new set of partitions (use_new). A return value of use_abort indicates
1344// that this function couldn't determine what to do. Overriding functions
1345// in derived classes may ask users questions in such cases.
srs5694e4ac11e2009-08-31 10:13:04 -04001346WhichToUse GPTData::UseWhichPartitions(void) {
1347 WhichToUse which = use_new;
1348 MBRValidity mbrState;
srs5694e4ac11e2009-08-31 10:13:04 -04001349
1350 mbrState = protectiveMBR.GetValidity();
1351
1352 if ((state == gpt_invalid) && ((mbrState == mbr) || (mbrState == hybrid))) {
srs5694fed16d02010-01-27 23:03:40 -05001353 cout << "\n***************************************************************\n"
1354 << "Found invalid GPT and valid MBR; converting MBR to GPT format.\n";
srs56945d58fe02010-01-03 20:57:08 -05001355 if (!justLooking) {
srs56940283dae2010-04-28 16:44:34 -04001356 cout << "\aTHIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by typing 'q' if\n"
srs5694fed16d02010-01-27 23:03:40 -05001357 << "you don't want to convert your MBR partitions to GPT format!\n";
srs56945d58fe02010-01-03 20:57:08 -05001358 } // if
srs5694fed16d02010-01-27 23:03:40 -05001359 cout << "***************************************************************\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001360 which = use_mbr;
1361 } // if
1362
1363 if ((state == gpt_invalid) && bsdFound) {
srs5694fed16d02010-01-27 23:03:40 -05001364 cout << "\n**********************************************************************\n"
1365 << "Found invalid GPT and valid BSD disklabel; converting BSD disklabel\n"
1366 << "to GPT format.";
srs56940a697312010-01-28 21:10:52 -05001367 if ((!justLooking) && (!beQuiet)) {
srs56940283dae2010-04-28 16:44:34 -04001368 cout << "\a THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Your first\n"
srs5694fed16d02010-01-27 23:03:40 -05001369 << "BSD partition will likely be unusable. Exit by typing 'q' if you don't\n"
1370 << "want to convert your BSD partitions to GPT format!";
srs56945d58fe02010-01-03 20:57:08 -05001371 } // if
srs5694fed16d02010-01-27 23:03:40 -05001372 cout << "\n**********************************************************************\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001373 which = use_bsd;
1374 } // if
1375
1376 if ((state == gpt_valid) && (mbrState == gpt)) {
srs5694e4ac11e2009-08-31 10:13:04 -04001377 which = use_gpt;
srs56943c0af382010-01-15 19:19:18 -05001378 if (!beQuiet)
srs5694fed16d02010-01-27 23:03:40 -05001379 cout << "Found valid GPT with protective MBR; using GPT.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001380 } // if
1381 if ((state == gpt_valid) && (mbrState == hybrid)) {
srs5694e4ac11e2009-08-31 10:13:04 -04001382 which = use_gpt;
srs56943c0af382010-01-15 19:19:18 -05001383 if (!beQuiet)
srs5694fed16d02010-01-27 23:03:40 -05001384 cout << "Found valid GPT with hybrid MBR; using GPT.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001385 } // if
1386 if ((state == gpt_valid) && (mbrState == invalid)) {
srs56940a697312010-01-28 21:10:52 -05001387 cout << "\aFound valid GPT with corrupt MBR; using GPT and will write new\n"
srs5694fed16d02010-01-27 23:03:40 -05001388 << "protective MBR on save.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001389 which = use_gpt;
srs5694e4ac11e2009-08-31 10:13:04 -04001390 } // if
1391 if ((state == gpt_valid) && (mbrState == mbr)) {
srs569408bb0da2010-02-19 17:19:55 -05001392 which = use_abort;
srs5694e4ac11e2009-08-31 10:13:04 -04001393 } // if
1394
srs5694e4ac11e2009-08-31 10:13:04 -04001395 if (state == gpt_corrupt) {
srs569408bb0da2010-02-19 17:19:55 -05001396 if (mbrState == gpt) {
1397 cout << "\a\a****************************************************************************\n"
1398 << "Caution: Found protective or hybrid MBR and corrupt GPT. Using GPT, but disk\n"
1399 << "verification and recovery are STRONGLY recommended.\n"
1400 << "****************************************************************************\n";
1401 which = use_gpt;
srs56943c0af382010-01-15 19:19:18 -05001402 } else {
srs569408bb0da2010-02-19 17:19:55 -05001403 which = use_abort;
1404 } // if/else MBR says disk is GPT
1405 } // if GPT corrupt
srs5694e4ac11e2009-08-31 10:13:04 -04001406
1407 if (which == use_new)
srs5694fed16d02010-01-27 23:03:40 -05001408 cout << "Creating new GPT entries.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001409
1410 return which;
1411} // UseWhichPartitions()
1412
srs569408bb0da2010-02-19 17:19:55 -05001413// Convert MBR partition table into GPT form.
1414void GPTData::XFormPartitions(void) {
srs5694e4ac11e2009-08-31 10:13:04 -04001415 int i, numToConvert;
1416 uint8_t origType;
srs5694e4ac11e2009-08-31 10:13:04 -04001417
1418 // Clear out old data & prepare basics....
1419 ClearGPTData();
1420
1421 // Convert the smaller of the # of GPT or MBR partitions
srs56940283dae2010-04-28 16:44:34 -04001422 if (numParts > MAX_MBR_PARTS)
srs5694978041c2009-09-21 20:51:47 -04001423 numToConvert = MAX_MBR_PARTS;
srs5694e4ac11e2009-08-31 10:13:04 -04001424 else
srs56940283dae2010-04-28 16:44:34 -04001425 numToConvert = numParts;
srs5694e4ac11e2009-08-31 10:13:04 -04001426
1427 for (i = 0; i < numToConvert; i++) {
1428 origType = protectiveMBR.GetType(i);
1429 // don't waste CPU time trying to convert extended, hybrid protective, or
1430 // null (non-existent) partitions
srs5694e35eb1b2009-09-14 00:29:34 -04001431 if ((origType != 0x05) && (origType != 0x0f) && (origType != 0x85) &&
srs56946699b012010-02-04 00:55:30 -05001432 (origType != 0x00) && (origType != 0xEE))
srs5694e4ac11e2009-08-31 10:13:04 -04001433 partitions[i] = protectiveMBR.AsGPT(i);
1434 } // for
1435
1436 // Convert MBR into protective MBR
1437 protectiveMBR.MakeProtectiveMBR();
1438
1439 // Record that all original CRCs were OK so as not to raise flags
1440 // when doing a disk verification
1441 mainCrcOk = secondCrcOk = mainPartsCrcOk = secondPartsCrcOk = 1;
srs5694e4ac11e2009-08-31 10:13:04 -04001442} // GPTData::XFormPartitions()
1443
1444// Transforms BSD disklabel on the specified partition (numbered from 0).
srs569408bb0da2010-02-19 17:19:55 -05001445// If an invalid partition number is given, the program does nothing.
srs5694e4ac11e2009-08-31 10:13:04 -04001446// Returns the number of new partitions created.
srs569408bb0da2010-02-19 17:19:55 -05001447int GPTData::XFormDisklabel(uint32_t partNum) {
1448 uint32_t low, high;
srs5694e4ac11e2009-08-31 10:13:04 -04001449 int goOn = 1, numDone = 0;
1450 BSDData disklabel;
1451
srs569408bb0da2010-02-19 17:19:55 -05001452 if (GetPartRange(&low, &high) == 0) {
1453 goOn = 0;
1454 cout << "No partitions!\n";
1455 } // if
1456 if (partNum > high) {
1457 goOn = 0;
1458 cout << "Specified partition is invalid!\n";
1459 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04001460
srs569408bb0da2010-02-19 17:19:55 -05001461 // If all is OK, read the disklabel and convert it.
1462 if (goOn) {
1463 goOn = disklabel.ReadBSDData(&myDisk, partitions[partNum].GetFirstLBA(),
1464 partitions[partNum].GetLastLBA());
1465 if ((goOn) && (disklabel.IsDisklabel())) {
1466 numDone = XFormDisklabel(&disklabel);
1467 if (numDone == 1)
1468 cout << "Converted 1 BSD partition.\n";
1469 else
1470 cout << "Converted " << numDone << " BSD partitions.\n";
1471 } else {
1472 cout << "Unable to convert partitions! Unrecognized BSD disklabel.\n";
1473 } // if/else
1474 } // if
1475 if (numDone > 0) { // converted partitions; delete carrier
1476 partitions[partNum].BlankPartition();
1477 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04001478 return numDone;
srs569455d92612010-03-07 22:16:07 -05001479} // GPTData::XFormDisklabel(uint32_t i)
srs5694e4ac11e2009-08-31 10:13:04 -04001480
1481// Transform the partitions on an already-loaded BSD disklabel...
srs569408bb0da2010-02-19 17:19:55 -05001482int GPTData::XFormDisklabel(BSDData* disklabel) {
1483 int i, partNum = 0, numDone = 0;
srs5694e4ac11e2009-08-31 10:13:04 -04001484
srs569408bb0da2010-02-19 17:19:55 -05001485 if (disklabel->IsDisklabel()) {
srs5694e4ac11e2009-08-31 10:13:04 -04001486 for (i = 0; i < disklabel->GetNumParts(); i++) {
srs569408bb0da2010-02-19 17:19:55 -05001487 partNum = FindFirstFreePart();
1488 if (partNum >= 0) {
1489 partitions[partNum] = disklabel->AsGPT(i);
1490 if (partitions[partNum].IsUsed())
1491 numDone++;
1492 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04001493 } // for
srs569408bb0da2010-02-19 17:19:55 -05001494 if (partNum == -1)
1495 cerr << "Warning! Too many partitions to convert!\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001496 } // if
1497
1498 // Record that all original CRCs were OK so as not to raise flags
1499 // when doing a disk verification
1500 mainCrcOk = secondCrcOk = mainPartsCrcOk = secondPartsCrcOk = 1;
1501
1502 return numDone;
1503} // GPTData::XFormDisklabel(BSDData* disklabel)
1504
srs569408bb0da2010-02-19 17:19:55 -05001505// Add one GPT partition to MBR. Used by PartsToMBR() functions. Created
1506// partition has the active/bootable flag UNset and uses the GPT fdisk
1507// type code divided by 0x0100 as the MBR type code.
1508// Returns 1 if operation was 100% successful, 0 if there were ANY
1509// problems.
srs5694978041c2009-09-21 20:51:47 -04001510int GPTData::OnePartToMBR(uint32_t gptPart, int mbrPart) {
srs569408bb0da2010-02-19 17:19:55 -05001511 int allOK = 1;
srs5694fed16d02010-01-27 23:03:40 -05001512
srs5694978041c2009-09-21 20:51:47 -04001513 if ((mbrPart < 0) || (mbrPart > 3)) {
srs5694fed16d02010-01-27 23:03:40 -05001514 cout << "MBR partition " << mbrPart + 1 << " is out of range; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001515 allOK = 0;
1516 } // if
srs56940283dae2010-04-28 16:44:34 -04001517 if (gptPart >= numParts) {
srs5694fed16d02010-01-27 23:03:40 -05001518 cout << "GPT partition " << gptPart + 1 << " is out of range; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001519 allOK = 0;
1520 } // if
1521 if (allOK && (partitions[gptPart].GetLastLBA() == UINT64_C(0))) {
srs5694fed16d02010-01-27 23:03:40 -05001522 cout << "GPT partition " << gptPart + 1 << " is undefined; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001523 allOK = 0;
1524 } // if
1525 if (allOK && (partitions[gptPart].GetFirstLBA() <= UINT32_MAX) &&
1526 (partitions[gptPart].GetLengthLBA() <= UINT32_MAX)) {
1527 if (partitions[gptPart].GetLastLBA() > UINT32_MAX) {
srs5694fed16d02010-01-27 23:03:40 -05001528 cout << "Caution: Partition end point past 32-bit pointer boundary;"
1529 << " some OSes may\nreact strangely.\n";
srs569408bb0da2010-02-19 17:19:55 -05001530 } // if
srs5694978041c2009-09-21 20:51:47 -04001531 protectiveMBR.MakePart(mbrPart, (uint32_t) partitions[gptPart].GetFirstLBA(),
srs569408bb0da2010-02-19 17:19:55 -05001532 (uint32_t) partitions[gptPart].GetLengthLBA(),
1533 partitions[gptPart].GetHexType() / 256, 0);
srs5694978041c2009-09-21 20:51:47 -04001534 } else { // partition out of range
srs569408bb0da2010-02-19 17:19:55 -05001535 if (allOK) // Display only if "else" triggered by out-of-bounds condition
1536 cout << "Partition " << gptPart + 1 << " begins beyond the 32-bit pointer limit of MBR "
1537 << "partitions, or is\n too big; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001538 allOK = 0;
1539 } // if/else
1540 return allOK;
1541} // GPTData::OnePartToMBR()
1542
srs5694e4ac11e2009-08-31 10:13:04 -04001543
1544/**********************************************************************
1545 * *
1546 * Functions that adjust GPT data structures WITHOUT user interaction *
1547 * (they may display information for the user's benefit, though) *
1548 * *
1549 **********************************************************************/
1550
1551// Resizes GPT to specified number of entries. Creates a new table if
srs5694ba00fed2010-01-12 18:18:36 -05001552// necessary, copies data if it already exists. Returns 1 if all goes
1553// well, 0 if an error is encountered.
srs5694e4ac11e2009-08-31 10:13:04 -04001554int GPTData::SetGPTSize(uint32_t numEntries) {
srs569408bb0da2010-02-19 17:19:55 -05001555 GPTPart* newParts;
srs5694e4ac11e2009-08-31 10:13:04 -04001556 uint32_t i, high, copyNum;
1557 int allOK = 1;
1558
1559 // First, adjust numEntries upward, if necessary, to get a number
1560 // that fills the allocated sectors
1561 i = blockSize / GPT_SIZE;
1562 if ((numEntries % i) != 0) {
srs5694fed16d02010-01-27 23:03:40 -05001563 cout << "Adjusting GPT size from " << numEntries << " to ";
srs5694e4ac11e2009-08-31 10:13:04 -04001564 numEntries = ((numEntries / i) + 1) * i;
srs5694fed16d02010-01-27 23:03:40 -05001565 cout << numEntries << " to fill the sector\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001566 } // if
1567
srs5694247657a2009-11-26 18:36:12 -05001568 // Do the work only if the # of partitions is changing. Along with being
srs569455d92612010-03-07 22:16:07 -05001569 // efficient, this prevents mucking with the location of the secondary
srs5694247657a2009-11-26 18:36:12 -05001570 // partition table, which causes problems when loading data from a RAID
1571 // array that's been expanded because this function is called when loading
1572 // data.
srs56940283dae2010-04-28 16:44:34 -04001573 if (((numEntries != numParts) || (partitions == NULL)) && (numEntries > 0)) {
srs569401f7f082011-03-15 23:53:31 -04001574 newParts = new GPTPart [numEntries];
srs5694247657a2009-11-26 18:36:12 -05001575 if (newParts != NULL) {
1576 if (partitions != NULL) { // existing partitions; copy them over
1577 GetPartRange(&i, &high);
1578 if (numEntries < (high + 1)) { // Highest entry too high for new #
srs5694fed16d02010-01-27 23:03:40 -05001579 cout << "The highest-numbered partition is " << high + 1
1580 << ", which is greater than the requested\n"
1581 << "partition table size of " << numEntries
1582 << "; cannot resize. Perhaps sorting will help.\n";
srs5694247657a2009-11-26 18:36:12 -05001583 allOK = 0;
srs5694815fb652011-03-18 12:35:56 -04001584 delete[] newParts;
srs5694247657a2009-11-26 18:36:12 -05001585 } else { // go ahead with copy
srs56940283dae2010-04-28 16:44:34 -04001586 if (numEntries < numParts)
srs5694247657a2009-11-26 18:36:12 -05001587 copyNum = numEntries;
1588 else
srs56940283dae2010-04-28 16:44:34 -04001589 copyNum = numParts;
srs5694247657a2009-11-26 18:36:12 -05001590 for (i = 0; i < copyNum; i++) {
1591 newParts[i] = partitions[i];
1592 } // for
srs569401f7f082011-03-15 23:53:31 -04001593 delete[] partitions;
srs5694247657a2009-11-26 18:36:12 -05001594 partitions = newParts;
srs5694247657a2009-11-26 18:36:12 -05001595 } // if
1596 } else { // No existing partition table; just create it
srs5694e4ac11e2009-08-31 10:13:04 -04001597 partitions = newParts;
srs5694247657a2009-11-26 18:36:12 -05001598 } // if/else existing partitions
srs56940283dae2010-04-28 16:44:34 -04001599 numParts = numEntries;
srs5694247657a2009-11-26 18:36:12 -05001600 mainHeader.firstUsableLBA = ((numEntries * GPT_SIZE) / blockSize) + 2 ;
1601 secondHeader.firstUsableLBA = mainHeader.firstUsableLBA;
1602 MoveSecondHeaderToEnd();
1603 if (diskSize > 0)
1604 CheckGPTSize();
1605 } else { // Bad memory allocation
srs5694fed16d02010-01-27 23:03:40 -05001606 cerr << "Error allocating memory for partition table!\n";
srs5694247657a2009-11-26 18:36:12 -05001607 allOK = 0;
1608 } // if/else
srs5694e4ac11e2009-08-31 10:13:04 -04001609 } // if/else
srs56940283dae2010-04-28 16:44:34 -04001610 mainHeader.numParts = numParts;
1611 secondHeader.numParts = numParts;
srs5694e4ac11e2009-08-31 10:13:04 -04001612 return (allOK);
1613} // GPTData::SetGPTSize()
1614
1615// Blank the partition array
1616void GPTData::BlankPartitions(void) {
1617 uint32_t i;
1618
srs56940283dae2010-04-28 16:44:34 -04001619 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04001620 partitions[i].BlankPartition();
1621 } // for
1622} // GPTData::BlankPartitions()
1623
srs5694ba00fed2010-01-12 18:18:36 -05001624// Delete a partition by number. Returns 1 if successful,
1625// 0 if there was a problem. Returns 1 if partition was in
1626// range, 0 if it was out of range.
1627int GPTData::DeletePartition(uint32_t partNum) {
1628 uint64_t startSector, length;
srs56940283dae2010-04-28 16:44:34 -04001629 uint32_t low, high, numUsedParts, retval = 1;;
srs5694ba00fed2010-01-12 18:18:36 -05001630
srs56940283dae2010-04-28 16:44:34 -04001631 numUsedParts = GetPartRange(&low, &high);
1632 if ((numUsedParts > 0) && (partNum >= low) && (partNum <= high)) {
srs5694ba00fed2010-01-12 18:18:36 -05001633 // In case there's a protective MBR, look for & delete matching
1634 // MBR partition....
1635 startSector = partitions[partNum].GetFirstLBA();
1636 length = partitions[partNum].GetLengthLBA();
1637 protectiveMBR.DeleteByLocation(startSector, length);
1638
1639 // Now delete the GPT partition
1640 partitions[partNum].BlankPartition();
1641 } else {
srs5694fed16d02010-01-27 23:03:40 -05001642 cerr << "Partition number " << partNum + 1 << " out of range!\n";
srs5694ba00fed2010-01-12 18:18:36 -05001643 retval = 0;
1644 } // if/else
1645 return retval;
1646} // GPTData::DeletePartition(uint32_t partNum)
1647
srs569408bb0da2010-02-19 17:19:55 -05001648// Non-interactively create a partition.
1649// Returns 1 if the operation was successful, 0 if a problem was discovered.
srs5694e321d442010-01-29 17:44:04 -05001650uint32_t GPTData::CreatePartition(uint32_t partNum, uint64_t startSector, uint64_t endSector) {
srs5694ba00fed2010-01-12 18:18:36 -05001651 int retval = 1; // assume there'll be no problems
srs56945a081752010-09-24 20:39:41 -04001652 uint64_t origSector = startSector;
srs5694ba00fed2010-01-12 18:18:36 -05001653
1654 if (IsFreePartNum(partNum)) {
srs56945a081752010-09-24 20:39:41 -04001655 if (Align(&startSector)) {
1656 cout << "Information: Moved requested sector from " << origSector << " to "
1657 << startSector << " in\norder to align on " << sectorAlignment
1658 << "-sector boundaries.\n";
1659 } // if
srs5694ba00fed2010-01-12 18:18:36 -05001660 if (IsFree(startSector) && (startSector <= endSector)) {
1661 if (FindLastInFree(startSector) >= endSector) {
1662 partitions[partNum].SetFirstLBA(startSector);
1663 partitions[partNum].SetLastLBA(endSector);
1664 partitions[partNum].SetType(0x0700);
srs56946699b012010-02-04 00:55:30 -05001665 partitions[partNum].RandomizeUniqueGUID();
srs5694ba00fed2010-01-12 18:18:36 -05001666 } else retval = 0; // if free space until endSector
1667 } else retval = 0; // if startSector is free
1668 } else retval = 0; // if legal partition number
1669 return retval;
1670} // GPTData::CreatePartition(partNum, startSector, endSector)
1671
srs5694e4ac11e2009-08-31 10:13:04 -04001672// Sort the GPT entries, eliminating gaps and making for a logical
srs56949a46b042011-03-15 00:34:10 -04001673// ordering.
srs5694e4ac11e2009-08-31 10:13:04 -04001674void GPTData::SortGPT(void) {
srs56949a46b042011-03-15 00:34:10 -04001675 if (numParts > 0)
srs569401f7f082011-03-15 23:53:31 -04001676 sort(partitions, partitions + numParts);
srs5694e4ac11e2009-08-31 10:13:04 -04001677} // GPTData::SortGPT()
1678
srs569408bb0da2010-02-19 17:19:55 -05001679// Swap the contents of two partitions.
1680// Returns 1 if successful, 0 if either partition is out of range
1681// (that is, not a legal number; either or both can be empty).
1682// Note that if partNum1 = partNum2 and this number is in range,
1683// it will be considered successful.
1684int GPTData::SwapPartitions(uint32_t partNum1, uint32_t partNum2) {
1685 GPTPart temp;
1686 int allOK = 1;
1687
srs56940283dae2010-04-28 16:44:34 -04001688 if ((partNum1 < numParts) && (partNum2 < numParts)) {
srs569408bb0da2010-02-19 17:19:55 -05001689 if (partNum1 != partNum2) {
1690 temp = partitions[partNum1];
1691 partitions[partNum1] = partitions[partNum2];
1692 partitions[partNum2] = temp;
1693 } // if
1694 } else allOK = 0; // partition numbers are valid
1695 return allOK;
1696} // GPTData::SwapPartitions()
1697
srs5694e4ac11e2009-08-31 10:13:04 -04001698// Set up data structures for entirely new set of partitions on the
1699// specified device. Returns 1 if OK, 0 if there were problems.
srs5694e35eb1b2009-09-14 00:29:34 -04001700// Note that this function does NOT clear the protectiveMBR data
1701// structure, since it may hold the original MBR partitions if the
1702// program was launched on an MBR disk, and those may need to be
1703// converted to GPT format.
srs5694e4ac11e2009-08-31 10:13:04 -04001704int GPTData::ClearGPTData(void) {
srs5694e35eb1b2009-09-14 00:29:34 -04001705 int goOn = 1, i;
srs5694e4ac11e2009-08-31 10:13:04 -04001706
1707 // Set up the partition table....
srs56949a46b042011-03-15 00:34:10 -04001708 delete[] partitions;
srs5694e4ac11e2009-08-31 10:13:04 -04001709 partitions = NULL;
1710 SetGPTSize(NUM_GPT_ENTRIES);
1711
1712 // Now initialize a bunch of stuff that's static....
1713 mainHeader.signature = GPT_SIGNATURE;
1714 mainHeader.revision = 0x00010000;
srs5694978041c2009-09-21 20:51:47 -04001715 mainHeader.headerSize = HEADER_SIZE;
srs5694e4ac11e2009-08-31 10:13:04 -04001716 mainHeader.reserved = 0;
1717 mainHeader.currentLBA = UINT64_C(1);
1718 mainHeader.partitionEntriesLBA = (uint64_t) 2;
1719 mainHeader.sizeOfPartitionEntries = GPT_SIZE;
1720 for (i = 0; i < GPT_RESERVED; i++) {
1721 mainHeader.reserved2[i] = '\0';
1722 } // for
srs56940873e9d2010-10-07 13:00:45 -04001723 if (blockSize > 0)
1724 sectorAlignment = DEFAULT_ALIGNMENT * SECTOR_SIZE / blockSize;
1725 else
1726 sectorAlignment = DEFAULT_ALIGNMENT;
srs5694e4ac11e2009-08-31 10:13:04 -04001727
1728 // Now some semi-static items (computed based on end of disk)
1729 mainHeader.backupLBA = diskSize - UINT64_C(1);
1730 mainHeader.lastUsableLBA = diskSize - mainHeader.firstUsableLBA;
1731
1732 // Set a unique GUID for the disk, based on random numbers
srs56946699b012010-02-04 00:55:30 -05001733 mainHeader.diskGUID.Randomize();
srs5694e4ac11e2009-08-31 10:13:04 -04001734
1735 // Copy main header to backup header
1736 RebuildSecondHeader();
1737
1738 // Blank out the partitions array....
1739 BlankPartitions();
1740
1741 // Flag all CRCs as being OK....
1742 mainCrcOk = 1;
1743 secondCrcOk = 1;
1744 mainPartsCrcOk = 1;
1745 secondPartsCrcOk = 1;
1746
1747 return (goOn);
1748} // GPTData::ClearGPTData()
1749
srs5694247657a2009-11-26 18:36:12 -05001750// Set the location of the second GPT header data to the end of the disk.
srs569464cbd172011-03-01 22:03:54 -05001751// If the disk size has actually changed, this also adjusts the protective
1752// entry in the MBR, since it's probably no longer correct.
srs5694247657a2009-11-26 18:36:12 -05001753// Used internally and called by the 'e' option on the recovery &
1754// transformation menu, to help users of RAID arrays who add disk space
srs569464cbd172011-03-01 22:03:54 -05001755// to their arrays or to adjust data structures in restore operations
1756// involving unequal-sized disks.
srs5694247657a2009-11-26 18:36:12 -05001757void GPTData::MoveSecondHeaderToEnd() {
srs56948bb78762009-11-24 15:43:49 -05001758 mainHeader.backupLBA = secondHeader.currentLBA = diskSize - UINT64_C(1);
srs569464cbd172011-03-01 22:03:54 -05001759 if (mainHeader.lastUsableLBA != diskSize - mainHeader.firstUsableLBA) {
1760 if (protectiveMBR.GetValidity() == hybrid) {
1761 protectiveMBR.OptimizeEESize();
1762 RecomputeCHS();
1763 } // if
1764 if (protectiveMBR.GetValidity() == gpt)
1765 MakeProtectiveMBR();
1766 } // if
srs56948bb78762009-11-24 15:43:49 -05001767 mainHeader.lastUsableLBA = secondHeader.lastUsableLBA = diskSize - mainHeader.firstUsableLBA;
1768 secondHeader.partitionEntriesLBA = secondHeader.lastUsableLBA + UINT64_C(1);
1769} // GPTData::FixSecondHeaderLocation()
1770
srs56945a608532011-03-17 13:53:01 -04001771int GPTData::SetName(uint32_t partNum, const UnicodeString & theName) {
srs5694ba00fed2010-01-12 18:18:36 -05001772 int retval = 1;
srs5694fed16d02010-01-27 23:03:40 -05001773
1774 if (!IsFreePartNum(partNum)) {
1775 partitions[partNum].SetName(theName);
1776 } else retval = 0;
srs5694ba00fed2010-01-12 18:18:36 -05001777
1778 return retval;
srs5694e4ac11e2009-08-31 10:13:04 -04001779} // GPTData::SetName
1780
1781// Set the disk GUID to the specified value. Note that the header CRCs must
1782// be recomputed after calling this function.
1783void GPTData::SetDiskGUID(GUIDData newGUID) {
1784 mainHeader.diskGUID = newGUID;
1785 secondHeader.diskGUID = newGUID;
1786} // SetDiskGUID()
1787
1788// Set the unique GUID of the specified partition. Returns 1 on
1789// successful completion, 0 if there were problems (invalid
1790// partition number).
1791int GPTData::SetPartitionGUID(uint32_t pn, GUIDData theGUID) {
1792 int retval = 0;
1793
srs56940283dae2010-04-28 16:44:34 -04001794 if (pn < numParts) {
srs5694e4ac11e2009-08-31 10:13:04 -04001795 if (partitions[pn].GetFirstLBA() != UINT64_C(0)) {
1796 partitions[pn].SetUniqueGUID(theGUID);
1797 retval = 1;
1798 } // if
1799 } // if
1800 return retval;
1801} // GPTData::SetPartitionGUID()
1802
srs56949ba54212010-05-18 23:24:02 -04001803// Set new random GUIDs for the disk and all partitions. Intended to be used
1804// after disk cloning or similar operations that don't randomize the GUIDs.
1805void GPTData::RandomizeGUIDs(void) {
1806 uint32_t i;
1807
1808 mainHeader.diskGUID.Randomize();
1809 secondHeader.diskGUID = mainHeader.diskGUID;
1810 for (i = 0; i < numParts; i++)
1811 if (partitions[i].IsUsed())
1812 partitions[i].RandomizeUniqueGUID();
1813} // GPTData::RandomizeGUIDs()
1814
srs5694ba00fed2010-01-12 18:18:36 -05001815// Change partition type code non-interactively. Returns 1 if
1816// successful, 0 if not....
srs5694327129e2010-09-22 01:07:31 -04001817int GPTData::ChangePartType(uint32_t partNum, PartType theGUID) {
1818 int retval = 1;
1819
1820 if (!IsFreePartNum(partNum)) {
1821 partitions[partNum].SetType(theGUID);
1822 } else retval = 0;
1823 return retval;
1824} // GPTData::ChangePartType()
1825
srs56949ba54212010-05-18 23:24:02 -04001826// Recompute the CHS values of all the MBR partitions. Used to reset
1827// CHS values that some BIOSes require, despite the fact that the
1828// resulting CHS values violate the GPT standard.
1829void GPTData::RecomputeCHS(void) {
1830 int i;
1831
1832 for (i = 0; i < 4; i++)
1833 protectiveMBR.RecomputeCHS(i);
1834} // GPTData::RecomputeCHS()
1835
srs56941d1448a2009-12-31 21:20:19 -05001836// Adjust sector number so that it falls on a sector boundary that's a
1837// multiple of sectorAlignment. This is done to improve the performance
1838// of Western Digital Advanced Format disks and disks with similar
1839// technology from other companies, which use 4096-byte sectors
1840// internally although they translate to 512-byte sectors for the
1841// benefit of the OS. If partitions aren't properly aligned on these
1842// disks, some filesystem data structures can span multiple physical
1843// sectors, degrading performance. This function should be called
1844// only on the FIRST sector of the partition, not the last!
1845// This function returns 1 if the alignment was altered, 0 if it
1846// was unchanged.
1847int GPTData::Align(uint64_t* sector) {
1848 int retval = 0, sectorOK = 0;
1849 uint64_t earlier, later, testSector, original;
1850
1851 if ((*sector % sectorAlignment) != 0) {
1852 original = *sector;
srs56941d1448a2009-12-31 21:20:19 -05001853 earlier = (*sector / sectorAlignment) * sectorAlignment;
1854 later = earlier + (uint64_t) sectorAlignment;
1855
1856 // Check to see that every sector between the earlier one and the
1857 // requested one is clear, and that it's not too early....
1858 if (earlier >= mainHeader.firstUsableLBA) {
srs56941d1448a2009-12-31 21:20:19 -05001859 sectorOK = 1;
1860 testSector = earlier;
1861 do {
1862 sectorOK = IsFree(testSector++);
1863 } while ((sectorOK == 1) && (testSector < *sector));
1864 if (sectorOK == 1) {
1865 *sector = earlier;
srs56945a081752010-09-24 20:39:41 -04001866 retval = 1;
srs56941d1448a2009-12-31 21:20:19 -05001867 } // if
1868 } // if firstUsableLBA check
1869
1870 // If couldn't move the sector earlier, try to move it later instead....
1871 if ((sectorOK != 1) && (later <= mainHeader.lastUsableLBA)) {
1872 sectorOK = 1;
1873 testSector = later;
1874 do {
1875 sectorOK = IsFree(testSector--);
1876 } while ((sectorOK == 1) && (testSector > *sector));
1877 if (sectorOK == 1) {
1878 *sector = later;
srs56945a081752010-09-24 20:39:41 -04001879 retval = 1;
srs56941d1448a2009-12-31 21:20:19 -05001880 } // if
1881 } // if
srs56941d1448a2009-12-31 21:20:19 -05001882 } // if
1883 return retval;
1884} // GPTData::Align()
1885
srs5694e4ac11e2009-08-31 10:13:04 -04001886/********************************************************
1887 * *
1888 * Functions that return data about GPT data structures *
1889 * (most of these are inline in gpt.h) *
1890 * *
1891 ********************************************************/
1892
1893// Find the low and high used partition numbers (numbered from 0).
1894// Return value is the number of partitions found. Note that the
1895// *low and *high values are both set to 0 when no partitions
1896// are found, as well as when a single partition in the first
1897// position exists. Thus, the return value is the only way to
1898// tell when no partitions exist.
1899int GPTData::GetPartRange(uint32_t *low, uint32_t *high) {
1900 uint32_t i;
1901 int numFound = 0;
1902
srs56940283dae2010-04-28 16:44:34 -04001903 *low = numParts + 1; // code for "not found"
srs5694e4ac11e2009-08-31 10:13:04 -04001904 *high = 0;
srs56949a46b042011-03-15 00:34:10 -04001905 for (i = 0; i < numParts; i++) {
1906 if (partitions[i].GetFirstLBA() != UINT64_C(0)) { // it exists
1907 *high = i; // since we're counting up, set the high value
1908 // Set the low value only if it's not yet found...
1909 if (*low == (numParts + 1)) *low = i;
1910 numFound++;
1911 } // if
1912 } // for
srs5694e4ac11e2009-08-31 10:13:04 -04001913
1914 // Above will leave *low pointing to its "not found" value if no partitions
1915 // are defined, so reset to 0 if this is the case....
srs56940283dae2010-04-28 16:44:34 -04001916 if (*low == (numParts + 1))
srs5694e4ac11e2009-08-31 10:13:04 -04001917 *low = 0;
1918 return numFound;
1919} // GPTData::GetPartRange()
1920
srs569408bb0da2010-02-19 17:19:55 -05001921// Returns the value of the first free partition, or -1 if none is
1922// unused.
1923int GPTData::FindFirstFreePart(void) {
1924 int i = 0;
1925
1926 if (partitions != NULL) {
srs56949a46b042011-03-15 00:34:10 -04001927 while ((i < (int) numParts) && (partitions[i].IsUsed()))
srs569408bb0da2010-02-19 17:19:55 -05001928 i++;
srs56940283dae2010-04-28 16:44:34 -04001929 if (i >= (int) numParts)
srs569408bb0da2010-02-19 17:19:55 -05001930 i = -1;
1931 } else i = -1;
1932 return i;
1933} // GPTData::FindFirstFreePart()
1934
srs5694978041c2009-09-21 20:51:47 -04001935// Returns the number of defined partitions.
1936uint32_t GPTData::CountParts(void) {
srs5694e321d442010-01-29 17:44:04 -05001937 uint32_t i, counted = 0;
srs5694978041c2009-09-21 20:51:47 -04001938
srs56940283dae2010-04-28 16:44:34 -04001939 for (i = 0; i < numParts; i++) {
srs569408bb0da2010-02-19 17:19:55 -05001940 if (partitions[i].IsUsed())
srs5694978041c2009-09-21 20:51:47 -04001941 counted++;
1942 } // for
1943 return counted;
1944} // GPTData::CountParts()
1945
srs5694e4ac11e2009-08-31 10:13:04 -04001946/****************************************************
1947 * *
1948 * Functions that return data about disk free space *
1949 * *
1950 ****************************************************/
1951
1952// Find the first available block after the starting point; returns 0 if
1953// there are no available blocks left
1954uint64_t GPTData::FindFirstAvailable(uint64_t start) {
1955 uint64_t first;
1956 uint32_t i;
1957 int firstMoved = 0;
1958
1959 // Begin from the specified starting point or from the first usable
1960 // LBA, whichever is greater...
1961 if (start < mainHeader.firstUsableLBA)
1962 first = mainHeader.firstUsableLBA;
1963 else
1964 first = start;
1965
1966 // ...now search through all partitions; if first is within an
1967 // existing partition, move it to the next sector after that
1968 // partition and repeat. If first was moved, set firstMoved
1969 // flag; repeat until firstMoved is not set, so as to catch
1970 // cases where partitions are out of sequential order....
1971 do {
1972 firstMoved = 0;
srs56940283dae2010-04-28 16:44:34 -04001973 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04001974 if ((first >= partitions[i].GetFirstLBA()) &&
srs569455d92612010-03-07 22:16:07 -05001975 (first <= partitions[i].GetLastLBA())) { // in existing part.
srs5694e4ac11e2009-08-31 10:13:04 -04001976 first = partitions[i].GetLastLBA() + 1;
1977 firstMoved = 1;
srs569455d92612010-03-07 22:16:07 -05001978 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04001979 } // for
1980 } while (firstMoved == 1);
1981 if (first > mainHeader.lastUsableLBA)
1982 first = 0;
1983 return (first);
1984} // GPTData::FindFirstAvailable()
1985
1986// Finds the first available sector in the largest block of unallocated
1987// space on the disk. Returns 0 if there are no available blocks left
1988uint64_t GPTData::FindFirstInLargest(void) {
srs5694e35eb1b2009-09-14 00:29:34 -04001989 uint64_t start, firstBlock, lastBlock, segmentSize, selectedSize = 0, selectedSegment = 0;
srs5694e4ac11e2009-08-31 10:13:04 -04001990
1991 start = 0;
1992 do {
1993 firstBlock = FindFirstAvailable(start);
1994 if (firstBlock != UINT32_C(0)) { // something's free...
1995 lastBlock = FindLastInFree(firstBlock);
1996 segmentSize = lastBlock - firstBlock + UINT32_C(1);
1997 if (segmentSize > selectedSize) {
1998 selectedSize = segmentSize;
1999 selectedSegment = firstBlock;
2000 } // if
2001 start = lastBlock + 1;
2002 } // if
2003 } while (firstBlock != 0);
2004 return selectedSegment;
2005} // GPTData::FindFirstInLargest()
2006
srs5694cb76c672010-02-11 22:22:22 -05002007// Find the last available block on the disk.
2008// Returns 0 if there are no available partitions
2009uint64_t GPTData::FindLastAvailable(void) {
srs5694e4ac11e2009-08-31 10:13:04 -04002010 uint64_t last;
2011 uint32_t i;
2012 int lastMoved = 0;
2013
2014 // Start by assuming the last usable LBA is available....
2015 last = mainHeader.lastUsableLBA;
2016
2017 // ...now, similar to algorithm in FindFirstAvailable(), search
2018 // through all partitions, moving last when it's in an existing
2019 // partition. Set the lastMoved flag so we repeat to catch cases
2020 // where partitions are out of logical order.
2021 do {
2022 lastMoved = 0;
srs56940283dae2010-04-28 16:44:34 -04002023 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04002024 if ((last >= partitions[i].GetFirstLBA()) &&
srs569455d92612010-03-07 22:16:07 -05002025 (last <= partitions[i].GetLastLBA())) { // in existing part.
srs5694e4ac11e2009-08-31 10:13:04 -04002026 last = partitions[i].GetFirstLBA() - 1;
2027 lastMoved = 1;
srs569455d92612010-03-07 22:16:07 -05002028 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002029 } // for
2030 } while (lastMoved == 1);
2031 if (last < mainHeader.firstUsableLBA)
2032 last = 0;
2033 return (last);
2034} // GPTData::FindLastAvailable()
2035
2036// Find the last available block in the free space pointed to by start.
2037uint64_t GPTData::FindLastInFree(uint64_t start) {
2038 uint64_t nearestStart;
2039 uint32_t i;
2040
2041 nearestStart = mainHeader.lastUsableLBA;
srs56940283dae2010-04-28 16:44:34 -04002042 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04002043 if ((nearestStart > partitions[i].GetFirstLBA()) &&
srs569455d92612010-03-07 22:16:07 -05002044 (partitions[i].GetFirstLBA() > start)) {
srs5694e4ac11e2009-08-31 10:13:04 -04002045 nearestStart = partitions[i].GetFirstLBA() - 1;
srs569455d92612010-03-07 22:16:07 -05002046 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002047 } // for
2048 return (nearestStart);
2049} // GPTData::FindLastInFree()
2050
2051// Finds the total number of free blocks, the number of segments in which
2052// they reside, and the size of the largest of those segments
srs5694e321d442010-01-29 17:44:04 -05002053uint64_t GPTData::FindFreeBlocks(uint32_t *numSegments, uint64_t *largestSegment) {
srs5694e4ac11e2009-08-31 10:13:04 -04002054 uint64_t start = UINT64_C(0); // starting point for each search
2055 uint64_t totalFound = UINT64_C(0); // running total
2056 uint64_t firstBlock; // first block in a segment
2057 uint64_t lastBlock; // last block in a segment
2058 uint64_t segmentSize; // size of segment in blocks
srs5694e321d442010-01-29 17:44:04 -05002059 uint32_t num = 0;
srs5694e4ac11e2009-08-31 10:13:04 -04002060
2061 *largestSegment = UINT64_C(0);
srs5694c54e9b42010-05-01 21:04:23 -04002062 if (diskSize > 0) {
2063 do {
2064 firstBlock = FindFirstAvailable(start);
2065 if (firstBlock != UINT64_C(0)) { // something's free...
2066 lastBlock = FindLastInFree(firstBlock);
2067 segmentSize = lastBlock - firstBlock + UINT64_C(1);
2068 if (segmentSize > *largestSegment) {
2069 *largestSegment = segmentSize;
2070 } // if
2071 totalFound += segmentSize;
2072 num++;
2073 start = lastBlock + 1;
srs5694e4ac11e2009-08-31 10:13:04 -04002074 } // if
srs5694c54e9b42010-05-01 21:04:23 -04002075 } while (firstBlock != 0);
2076 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002077 *numSegments = num;
2078 return totalFound;
2079} // GPTData::FindFreeBlocks()
2080
srs569455d92612010-03-07 22:16:07 -05002081// Returns 1 if sector is unallocated, 0 if it's allocated to a partition.
2082// If it's allocated, return the partition number to which it's allocated
2083// in partNum, if that variable is non-NULL. (A value of UINT32_MAX is
2084// returned in partNum if the sector is in use by basic GPT data structures.)
2085int GPTData::IsFree(uint64_t sector, uint32_t *partNum) {
srs5694e4ac11e2009-08-31 10:13:04 -04002086 int isFree = 1;
2087 uint32_t i;
2088
srs56940283dae2010-04-28 16:44:34 -04002089 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04002090 if ((sector >= partitions[i].GetFirstLBA()) &&
2091 (sector <= partitions[i].GetLastLBA())) {
2092 isFree = 0;
srs569455d92612010-03-07 22:16:07 -05002093 if (partNum != NULL)
2094 *partNum = i;
srs569408bb0da2010-02-19 17:19:55 -05002095 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002096 } // for
srs5694e35eb1b2009-09-14 00:29:34 -04002097 if ((sector < mainHeader.firstUsableLBA) ||
srs5694e4ac11e2009-08-31 10:13:04 -04002098 (sector > mainHeader.lastUsableLBA)) {
2099 isFree = 0;
srs569455d92612010-03-07 22:16:07 -05002100 if (partNum != NULL)
2101 *partNum = UINT32_MAX;
srs569408bb0da2010-02-19 17:19:55 -05002102 } // if
2103 return (isFree);
srs5694e4ac11e2009-08-31 10:13:04 -04002104} // GPTData::IsFree()
2105
srs5694815fb652011-03-18 12:35:56 -04002106// Returns 1 if partNum is unused AND if it's a legal value.
srs5694ba00fed2010-01-12 18:18:36 -05002107int GPTData::IsFreePartNum(uint32_t partNum) {
srs569401f7f082011-03-15 23:53:31 -04002108 return ((partNum < numParts) && (partitions != NULL) &&
2109 (!partitions[partNum].IsUsed()));
srs5694ba00fed2010-01-12 18:18:36 -05002110} // GPTData::IsFreePartNum()
2111
srs5694815fb652011-03-18 12:35:56 -04002112// Returns 1 if partNum is in use.
2113int GPTData::IsUsedPartNum(uint32_t partNum) {
2114 return ((partNum < numParts) && (partitions != NULL) &&
2115 (partitions[partNum].IsUsed()));
2116} // GPTData::IsUsedPartNum()
srs5694a8582cf2010-03-19 14:21:59 -04002117
2118/***********************************************************
2119 * *
2120 * Change how functions work or return information on them *
2121 * *
2122 ***********************************************************/
2123
2124// Set partition alignment value; partitions will begin on multiples of
2125// the specified value
2126void GPTData::SetAlignment(uint32_t n) {
srs56940873e9d2010-10-07 13:00:45 -04002127 if (n > 0)
2128 sectorAlignment = n;
2129 else
2130 cerr << "Attempt to set partition alignment to 0!\n";
srs5694a8582cf2010-03-19 14:21:59 -04002131} // GPTData::SetAlignment()
2132
2133// Compute sector alignment based on the current partitions (if any). Each
2134// partition's starting LBA is examined, and if it's divisible by a power-of-2
srs56940873e9d2010-10-07 13:00:45 -04002135// value less than or equal to the DEFAULT_ALIGNMENT value (adjusted for the
2136// sector size), but not by the previously-located alignment value, then the
2137// alignment value is adjusted down. If the computed alignment is less than 8
2138// and the disk is bigger than SMALLEST_ADVANCED_FORMAT, resets it to 8. This
2139// is a safety measure for WD Advanced Format and similar drives. If no partitions
2140// are defined, the alignment value is set to DEFAULT_ALIGNMENT (2048) (or an
2141// adjustment of that based on the current sector size). The result is that new
srs56948a4ddfc2010-03-21 19:05:49 -04002142// drives are aligned to 2048-sector multiples but the program won't complain
2143// about other alignments on existing disks unless a smaller-than-8 alignment
srs56940873e9d2010-10-07 13:00:45 -04002144// is used on big disks (as safety for WD Advanced Format drives).
srs5694a8582cf2010-03-19 14:21:59 -04002145// Returns the computed alignment value.
2146uint32_t GPTData::ComputeAlignment(void) {
2147 uint32_t i = 0, found, exponent = 31;
srs5694ab4b0432010-09-25 20:39:52 -04002148 uint32_t align = DEFAULT_ALIGNMENT;
srs5694a8582cf2010-03-19 14:21:59 -04002149
srs56940873e9d2010-10-07 13:00:45 -04002150 if (blockSize > 0)
2151 align = DEFAULT_ALIGNMENT * SECTOR_SIZE / blockSize;
2152 exponent = (uint32_t) log2(align);
srs56940283dae2010-04-28 16:44:34 -04002153 for (i = 0; i < numParts; i++) {
srs5694a8582cf2010-03-19 14:21:59 -04002154 if (partitions[i].IsUsed()) {
2155 found = 0;
2156 while (!found) {
srs56940873e9d2010-10-07 13:00:45 -04002157 align = UINT64_C(1) << exponent;
srs5694a8582cf2010-03-19 14:21:59 -04002158 if ((partitions[i].GetFirstLBA() % align) == 0) {
2159 found = 1;
2160 } else {
2161 exponent--;
2162 } // if/else
2163 } // while
2164 } // if
2165 } // for
srs56940873e9d2010-10-07 13:00:45 -04002166 if ((align < MIN_AF_ALIGNMENT) && (diskSize >= SMALLEST_ADVANCED_FORMAT))
2167 align = MIN_AF_ALIGNMENT;
2168 sectorAlignment = align;
srs5694a8582cf2010-03-19 14:21:59 -04002169 return align;
2170} // GPTData::ComputeAlignment()
2171
srs5694e4ac11e2009-08-31 10:13:04 -04002172/********************************
2173 * *
2174 * Endianness support functions *
2175 * *
2176 ********************************/
2177
srs56942a9f5da2009-08-26 00:48:01 -04002178void GPTData::ReverseHeaderBytes(struct GPTHeader* header) {
srs5694221e0872009-08-29 15:00:31 -04002179 ReverseBytes(&header->signature, 8);
2180 ReverseBytes(&header->revision, 4);
2181 ReverseBytes(&header->headerSize, 4);
2182 ReverseBytes(&header->headerCRC, 4);
2183 ReverseBytes(&header->reserved, 4);
2184 ReverseBytes(&header->currentLBA, 8);
2185 ReverseBytes(&header->backupLBA, 8);
2186 ReverseBytes(&header->firstUsableLBA, 8);
2187 ReverseBytes(&header->lastUsableLBA, 8);
2188 ReverseBytes(&header->partitionEntriesLBA, 8);
2189 ReverseBytes(&header->numParts, 4);
2190 ReverseBytes(&header->sizeOfPartitionEntries, 4);
2191 ReverseBytes(&header->partitionEntriesCRC, 4);
srs569408bb0da2010-02-19 17:19:55 -05002192 ReverseBytes(header->reserved2, GPT_RESERVED);
srs56942a9f5da2009-08-26 00:48:01 -04002193} // GPTData::ReverseHeaderBytes()
2194
srs56940283dae2010-04-28 16:44:34 -04002195// Reverse byte order for all partitions.
srs56942a9f5da2009-08-26 00:48:01 -04002196void GPTData::ReversePartitionBytes() {
2197 uint32_t i;
2198
srs56940283dae2010-04-28 16:44:34 -04002199 for (i = 0; i < numParts; i++) {
srs5694221e0872009-08-29 15:00:31 -04002200 partitions[i].ReversePartBytes();
srs56942a9f5da2009-08-26 00:48:01 -04002201 } // for
2202} // GPTData::ReversePartitionBytes()
2203
srs56949ddc14b2010-08-22 22:44:42 -04002204// Validate partition number
2205bool GPTData::ValidPartNum (const uint32_t partNum) {
2206 if (partNum >= numParts) {
srs56945a081752010-09-24 20:39:41 -04002207 cerr << "Partition number out of range: " << partNum << "\n";
srs56949ddc14b2010-08-22 22:44:42 -04002208 return false;
2209 } // if
2210 return true;
2211} // GPTData::ValidPartNum
2212
srs56945a081752010-09-24 20:39:41 -04002213// Return a single partition for inspection (not modification!) by other
2214// functions.
2215const GPTPart & GPTData::operator[](uint32_t partNum) const {
2216 if (partNum >= numParts) {
srs5694815fb652011-03-18 12:35:56 -04002217 cerr << "Partition number out of range (" << partNum << " requested, but only "
2218 << numParts << " available)\n";
2219 exit(1);
2220 } // if
2221 if (partitions == NULL) {
2222 cerr << "No partitions defined in GPTData::operator[]; fatal error!\n";
2223 exit(1);
srs56945a081752010-09-24 20:39:41 -04002224 } // if
2225 return partitions[partNum];
2226} // operator[]
2227
2228// Return (not for modification!) the disk's GUID value
2229const GUIDData & GPTData::GetDiskGUID(void) const {
2230 return mainHeader.diskGUID;
2231} // GPTData::GetDiskGUID()
2232
srs56949ddc14b2010-08-22 22:44:42 -04002233// Manage attributes for a partition, based on commands passed to this function.
2234// (Function is non-interactive.)
2235// Returns 1 if a modification command succeeded, 0 if the command should not have
2236// modified data, and -1 if a modification command failed.
2237int GPTData::ManageAttributes(int partNum, const string & command, const string & bits) {
2238 int retval = 0;
2239 Attributes theAttr;
2240
2241 if (command == "show") {
2242 ShowAttributes(partNum);
2243 } else if (command == "get") {
2244 GetAttribute(partNum, bits);
2245 } else {
2246 theAttr = partitions[partNum].GetAttributes();
2247 if (theAttr.OperateOnAttributes(partNum, command, bits)) {
2248 partitions[partNum].SetAttributes(theAttr.GetAttributes());
2249 retval = 1;
2250 } else {
2251 retval = -1;
2252 } // if/else
2253 } // if/elseif/else
2254
2255 return retval;
2256} // GPTData::ManageAttributes()
2257
2258// Show all attributes for a specified partition....
2259void GPTData::ShowAttributes(const uint32_t partNum) {
srs56940873e9d2010-10-07 13:00:45 -04002260 partitions[partNum].ShowAttributes(partNum);
srs56949ddc14b2010-08-22 22:44:42 -04002261} // GPTData::ShowAttributes
2262
2263// Show whether a single attribute bit is set (terse output)...
2264void GPTData::GetAttribute(const uint32_t partNum, const string& attributeBits) {
srs56940873e9d2010-10-07 13:00:45 -04002265 partitions[partNum].GetAttributes().OperateOnAttributes(partNum, "get", attributeBits);
srs56949ddc14b2010-08-22 22:44:42 -04002266} // GPTData::GetAttribute
2267
2268
srs56942a9f5da2009-08-26 00:48:01 -04002269/******************************************
2270 * *
2271 * Additional non-class support functions *
2272 * *
2273 ******************************************/
2274
srs5694e7b4ff92009-08-18 13:16:10 -04002275// Check to be sure that data type sizes are correct. The basic types (uint*_t) should
2276// never fail these tests, but the struct types may fail depending on compile options.
2277// Specifically, the -fpack-struct option to gcc may be required to ensure proper structure
2278// sizes.
2279int SizesOK(void) {
2280 int allOK = 1;
srs5694e7b4ff92009-08-18 13:16:10 -04002281
2282 if (sizeof(uint8_t) != 1) {
srs5694fed16d02010-01-27 23:03:40 -05002283 cerr << "uint8_t is " << sizeof(uint8_t) << " bytes, should be 1 byte; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002284 allOK = 0;
2285 } // if
2286 if (sizeof(uint16_t) != 2) {
srs5694fed16d02010-01-27 23:03:40 -05002287 cerr << "uint16_t is " << sizeof(uint16_t) << " bytes, should be 2 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002288 allOK = 0;
2289 } // if
2290 if (sizeof(uint32_t) != 4) {
srs5694fed16d02010-01-27 23:03:40 -05002291 cerr << "uint32_t is " << sizeof(uint32_t) << " bytes, should be 4 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002292 allOK = 0;
2293 } // if
2294 if (sizeof(uint64_t) != 8) {
srs5694fed16d02010-01-27 23:03:40 -05002295 cerr << "uint64_t is " << sizeof(uint64_t) << " bytes, should be 8 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002296 allOK = 0;
2297 } // if
2298 if (sizeof(struct MBRRecord) != 16) {
srs5694fed16d02010-01-27 23:03:40 -05002299 cerr << "MBRRecord is " << sizeof(MBRRecord) << " bytes, should be 16 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002300 allOK = 0;
2301 } // if
srs5694978041c2009-09-21 20:51:47 -04002302 if (sizeof(struct TempMBR) != 512) {
srs5694fed16d02010-01-27 23:03:40 -05002303 cerr << "TempMBR is " << sizeof(TempMBR) << " bytes, should be 512 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002304 allOK = 0;
2305 } // if
2306 if (sizeof(struct GPTHeader) != 512) {
srs5694fed16d02010-01-27 23:03:40 -05002307 cerr << "GPTHeader is " << sizeof(GPTHeader) << " bytes, should be 512 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002308 allOK = 0;
2309 } // if
srs5694221e0872009-08-29 15:00:31 -04002310 if (sizeof(GPTPart) != 128) {
srs5694fed16d02010-01-27 23:03:40 -05002311 cerr << "GPTPart is " << sizeof(GPTPart) << " bytes, should be 128 bytes; aborting!\n";
srs5694221e0872009-08-29 15:00:31 -04002312 allOK = 0;
2313 } // if
srs56946699b012010-02-04 00:55:30 -05002314 if (sizeof(GUIDData) != 16) {
2315 cerr << "GUIDData is " << sizeof(GUIDData) << " bytes, should be 16 bytes; aborting!\n";
2316 allOK = 0;
2317 } // if
2318 if (sizeof(PartType) != 16) {
2319 cerr << "PartType is " << sizeof(GUIDData) << " bytes, should be 16 bytes; aborting!\n";
2320 allOK = 0;
2321 } // if
srs5694fed16d02010-01-27 23:03:40 -05002322 // Determine endianness; warn user if running on big-endian (PowerPC, etc.) hardware
srs56940873e9d2010-10-07 13:00:45 -04002323// if (IsLittleEndian() == 0) {
2324// cerr << "\aRunning on big-endian hardware. Big-endian support is new and poorly"
2325// " tested!\n";
2326// } // if
srs5694e7b4ff92009-08-18 13:16:10 -04002327 return (allOK);
2328} // SizesOK()
srs5694e4ac11e2009-08-31 10:13:04 -04002329