blob: 652fab4ca5aacd972454b8e1d396cf86c9bd96d7 [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>
srs5694e7b4ff92009-08-18 13:16:10 -040022#include "crc32.h"
23#include "gpt.h"
srs5694221e0872009-08-29 15:00:31 -040024#include "bsd.h"
srs5694e7b4ff92009-08-18 13:16:10 -040025#include "support.h"
26#include "parttypes.h"
27#include "attributes.h"
srs5694546a9c72010-01-26 16:00:26 -050028#include "diskio.h"
srs5694bf8950c2011-03-12 01:23:12 -050029//#include "partnotes.h"
srs5694e7b4ff92009-08-18 13:16:10 -040030
31using namespace std;
32
srs56948f1b2d62010-05-23 13:07:19 -040033#ifdef __FreeBSD__
srs56949ba54212010-05-18 23:24:02 -040034#define log2(x) (log(x) / M_LN2)
35#endif // __FreeBSD__
36
srs56948f1b2d62010-05-23 13:07:19 -040037#ifdef _MSC_VER
38#define log2(x) (log((double) x) / log(2.0))
39#endif // Microsoft Visual C++
srs56949ba54212010-05-18 23:24:02 -040040
srs5694e7b4ff92009-08-18 13:16:10 -040041/****************************************
42 * *
43 * GPTData class and related structures *
44 * *
45 ****************************************/
46
srs5694e4ac11e2009-08-31 10:13:04 -040047// Default constructor
srs5694e7b4ff92009-08-18 13:16:10 -040048GPTData::GPTData(void) {
49 blockSize = SECTOR_SIZE; // set a default
50 diskSize = 0;
51 partitions = NULL;
52 state = gpt_valid;
srs5694fed16d02010-01-27 23:03:40 -050053 device = "";
srs56945d58fe02010-01-03 20:57:08 -050054 justLooking = 0;
srs5694e7b4ff92009-08-18 13:16:10 -040055 mainCrcOk = 0;
56 secondCrcOk = 0;
57 mainPartsCrcOk = 0;
58 secondPartsCrcOk = 0;
srs5694221e0872009-08-29 15:00:31 -040059 apmFound = 0;
60 bsdFound = 0;
srs56940873e9d2010-10-07 13:00:45 -040061 sectorAlignment = MIN_AF_ALIGNMENT; // Align partitions on 4096-byte boundaries by default
srs5694ba00fed2010-01-12 18:18:36 -050062 beQuiet = 0;
63 whichWasUsed = use_new;
srs56941e093722010-01-05 00:14:19 -050064 mainHeader.numParts = 0;
srs56940283dae2010-04-28 16:44:34 -040065 numParts = 0;
srs5694e7b4ff92009-08-18 13:16:10 -040066 SetGPTSize(NUM_GPT_ENTRIES);
67} // GPTData default constructor
68
69// The following constructor loads GPT data from a device file
srs5694fed16d02010-01-27 23:03:40 -050070GPTData::GPTData(string filename) {
srs5694e7b4ff92009-08-18 13:16:10 -040071 blockSize = SECTOR_SIZE; // set a default
72 diskSize = 0;
73 partitions = NULL;
74 state = gpt_invalid;
srs5694fed16d02010-01-27 23:03:40 -050075 device = "";
srs56945d58fe02010-01-03 20:57:08 -050076 justLooking = 0;
srs5694e7b4ff92009-08-18 13:16:10 -040077 mainCrcOk = 0;
78 secondCrcOk = 0;
79 mainPartsCrcOk = 0;
80 secondPartsCrcOk = 0;
srs5694221e0872009-08-29 15:00:31 -040081 apmFound = 0;
82 bsdFound = 0;
srs56940873e9d2010-10-07 13:00:45 -040083 sectorAlignment = MIN_AF_ALIGNMENT; // Align partitions on 4096-byte boundaries by default
srs5694ba00fed2010-01-12 18:18:36 -050084 beQuiet = 0;
85 whichWasUsed = use_new;
srs56941e093722010-01-05 00:14:19 -050086 mainHeader.numParts = 0;
srs56940283dae2010-04-28 16:44:34 -040087 numParts = 0;
srs56943c0af382010-01-15 19:19:18 -050088 if (!LoadPartitions(filename))
89 exit(2);
srs5694fed16d02010-01-27 23:03:40 -050090} // GPTData(string filename) constructor
srs5694e7b4ff92009-08-18 13:16:10 -040091
srs5694e4ac11e2009-08-31 10:13:04 -040092// Destructor
srs5694e7b4ff92009-08-18 13:16:10 -040093GPTData::~GPTData(void) {
srs5694cb76c672010-02-11 22:22:22 -050094 delete[] partitions;
srs5694e7b4ff92009-08-18 13:16:10 -040095} // GPTData destructor
96
srs569464cbd172011-03-01 22:03:54 -050097// Assignment operator
98GPTData & GPTData::operator=(const GPTData & orig) {
99 uint32_t i;
100
101 mainHeader = orig.mainHeader;
102 numParts = orig.numParts;
103 secondHeader = orig.secondHeader;
104 protectiveMBR = orig.protectiveMBR;
105 device = orig.device;
106 blockSize = orig.blockSize;
107 diskSize = orig.diskSize;
108 state = orig.state;
109 justLooking = orig.justLooking;
110 mainCrcOk = orig.mainCrcOk;
111 secondCrcOk = orig.secondCrcOk;
112 mainPartsCrcOk = orig.mainPartsCrcOk;
113 secondPartsCrcOk = orig.secondPartsCrcOk;
114 apmFound = orig.apmFound;
115 bsdFound = orig.bsdFound;
116 sectorAlignment = orig.sectorAlignment;
117 beQuiet = orig.beQuiet;
118 whichWasUsed = orig.whichWasUsed;
119
120 myDisk.OpenForRead(orig.myDisk.GetName());
121
122 delete[] partitions;
123 partitions = new GPTPart [numParts * sizeof (GPTPart)];
124 if (partitions != NULL) {
125 for (i = 0; i < numParts; i++) {
126 partitions[i] = orig.partitions[i];
127 }
128 } else {
129 numParts = 0;
130 cerr << "Error! Could not allocate memory for partitions in GPTData::operator=()!\n"
131 << "Continuing, but strange problems may occur!\n";
132 } // if/else
133 return *this;
134} // GPTData::operator=()
135
srs5694e4ac11e2009-08-31 10:13:04 -0400136/*********************************************************************
137 * *
138 * Begin functions that verify data, or that adjust the verification *
139 * information (compute CRCs, rebuild headers) *
140 * *
141 *********************************************************************/
srs5694e7b4ff92009-08-18 13:16:10 -0400142
srs5694e4ac11e2009-08-31 10:13:04 -0400143// Perform detailed verification, reporting on any problems found, but
144// do *NOT* recover from these problems. Returns the total number of
145// problems identified.
146int GPTData::Verify(void) {
srs569464cbd172011-03-01 22:03:54 -0500147 int problems = 0, alignProbs = 0;
srs5694e321d442010-01-29 17:44:04 -0500148 uint32_t i, numSegments;
149 uint64_t totalFree, largestSegment;
srs5694e4ac11e2009-08-31 10:13:04 -0400150
151 // First, check for CRC errors in the GPT data....
152 if (!mainCrcOk) {
153 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500154 cout << "\nProblem: The CRC for the main GPT header is invalid. The main GPT header may\n"
155 << "be corrupt. Consider loading the backup GPT header to rebuild the main GPT\n"
156 << "header ('b' on the recovery & transformation menu). This report may be a false\n"
157 << "alarm if you've already corrected other problems.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400158 } // if
159 if (!mainPartsCrcOk) {
160 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500161 cout << "\nProblem: The CRC for the main partition table is invalid. This table may be\n"
162 << "corrupt. Consider loading the backup partition table ('c' on the recovery &\n"
163 << "transformation menu). This report may be a false alarm if you've already\n"
164 << "corrected other problems.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400165 } // if
166 if (!secondCrcOk) {
167 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500168 cout << "\nProblem: The CRC for the backup GPT header is invalid. The backup GPT header\n"
169 << "may be corrupt. Consider using the main GPT header to rebuild the backup GPT\n"
170 << "header ('d' on the recovery & transformation menu). This report may be a false\n"
171 << "alarm if you've already corrected other problems.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400172 } // if
173 if (!secondPartsCrcOk) {
174 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500175 cout << "\nCaution: The CRC for the backup partition table is invalid. This table may\n"
176 << "be corrupt. This program will automatically create a new backup partition\n"
177 << "table when you save your partitions.\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400178 } // if
179
srs5694978041c2009-09-21 20:51:47 -0400180 // Now check that the main and backup headers both point to themselves....
181 if (mainHeader.currentLBA != 1) {
182 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500183 cout << "\nProblem: The main header's self-pointer doesn't point to itself. This problem\n"
184 << "is being automatically corrected, but it may be a symptom of more serious\n"
185 << "problems. Think carefully before saving changes with 'w' or using this disk.\n";
srs5694978041c2009-09-21 20:51:47 -0400186 mainHeader.currentLBA = 1;
187 } // if
188 if (secondHeader.currentLBA != (diskSize - UINT64_C(1))) {
189 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500190 cout << "\nProblem: The secondary header's self-pointer indicates that it doesn't reside\n"
191 << "at the end of the disk. If you've added a disk to a RAID array, use the 'e'\n"
192 << "option on the experts' menu to adjust the secondary header's and partition\n"
193 << "table's locations.\n";
srs5694978041c2009-09-21 20:51:47 -0400194 } // if
195
196 // Now check that critical main and backup GPT entries match each other
srs5694e4ac11e2009-08-31 10:13:04 -0400197 if (mainHeader.currentLBA != secondHeader.backupLBA) {
198 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500199 cout << "\nProblem: main GPT header's current LBA pointer (" << mainHeader.currentLBA
200 << ") doesn't\nmatch the backup GPT header's alternate LBA pointer("
201 << secondHeader.backupLBA << ").\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400202 } // if
203 if (mainHeader.backupLBA != secondHeader.currentLBA) {
204 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500205 cout << "\nProblem: main GPT header's backup LBA pointer (" << mainHeader.backupLBA
206 << ") doesn't\nmatch the backup GPT header's current LBA pointer ("
207 << secondHeader.currentLBA << ").\n"
208 << "The 'e' option on the experts' menu may fix this problem.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400209 } // if
210 if (mainHeader.firstUsableLBA != secondHeader.firstUsableLBA) {
211 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500212 cout << "\nProblem: main GPT header's first usable LBA pointer (" << mainHeader.firstUsableLBA
213 << ") doesn't\nmatch the backup GPT header's first usable LBA pointer ("
214 << secondHeader.firstUsableLBA << ")\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400215 } // if
216 if (mainHeader.lastUsableLBA != secondHeader.lastUsableLBA) {
217 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500218 cout << "\nProblem: main GPT header's last usable LBA pointer (" << mainHeader.lastUsableLBA
219 << ") doesn't\nmatch the backup GPT header's last usable LBA pointer ("
220 << secondHeader.lastUsableLBA << ")\n"
221 << "The 'e' option on the experts' menu can probably fix this problem.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400222 } // if
srs56946699b012010-02-04 00:55:30 -0500223 if ((mainHeader.diskGUID != secondHeader.diskGUID)) {
srs5694e4ac11e2009-08-31 10:13:04 -0400224 problems++;
srs56945a081752010-09-24 20:39:41 -0400225 cout << "\nProblem: main header's disk GUID (" << mainHeader.diskGUID
srs5694fed16d02010-01-27 23:03:40 -0500226 << ") doesn't\nmatch the backup GPT header's disk GUID ("
srs56945a081752010-09-24 20:39:41 -0400227 << secondHeader.diskGUID << ")\n"
srs5694fed16d02010-01-27 23:03:40 -0500228 << "You should use the 'b' or 'd' option on the recovery & transformation menu to\n"
229 << "select one or the other header.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400230 } // if
231 if (mainHeader.numParts != secondHeader.numParts) {
232 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500233 cout << "\nProblem: main GPT header's number of partitions (" << mainHeader.numParts
234 << ") doesn't\nmatch the backup GPT header's number of partitions ("
235 << secondHeader.numParts << ")\n"
236 << "Resizing the partition table ('s' on the experts' menu) may help.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400237 } // if
238 if (mainHeader.sizeOfPartitionEntries != secondHeader.sizeOfPartitionEntries) {
239 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500240 cout << "\nProblem: main GPT header's size of partition entries ("
241 << mainHeader.sizeOfPartitionEntries << ") doesn't\n"
242 << "match the backup GPT header's size of partition entries ("
243 << secondHeader.sizeOfPartitionEntries << ")\n"
244 << "You should use the 'b' or 'd' option on the recovery & transformation menu to\n"
245 << "select one or the other header.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400246 } // if
247
248 // Now check for a few other miscellaneous problems...
249 // Check that the disk size will hold the data...
srs569464cbd172011-03-01 22:03:54 -0500250 if (mainHeader.backupLBA >= diskSize) {
srs5694e4ac11e2009-08-31 10:13:04 -0400251 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500252 cout << "\nProblem: Disk is too small to hold all the data!\n"
253 << "(Disk size is " << diskSize << " sectors, needs to be "
srs569464cbd172011-03-01 22:03:54 -0500254 << mainHeader.backupLBA + UINT64_C(1) << " sectors.)\n"
srs5694fed16d02010-01-27 23:03:40 -0500255 << "The 'e' option on the experts' menu may fix this problem.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400256 } // if
257
258 // Check for overlapping partitions....
259 problems += FindOverlaps();
260
srs569455d92612010-03-07 22:16:07 -0500261 // Check for insane partitions (start after end, hugely big, etc.)
262 problems += FindInsanePartitions();
263
srs5694e4ac11e2009-08-31 10:13:04 -0400264 // Check for mismatched MBR and GPT partitions...
265 problems += FindHybridMismatches();
266
srs5694327129e2010-09-22 01:07:31 -0400267 // Check for MBR-specific problems....
268 problems += VerifyMBR();
269
srs5694e4ac11e2009-08-31 10:13:04 -0400270 // Verify that partitions don't run into GPT data areas....
271 problems += CheckGPTSize();
272
srs56941d1448a2009-12-31 21:20:19 -0500273 // Check that partitions are aligned on proper boundaries (for WD Advanced
274 // Format and similar disks)....
srs56940283dae2010-04-28 16:44:34 -0400275 for (i = 0; i < numParts; i++) {
srs56941d1448a2009-12-31 21:20:19 -0500276 if ((partitions[i].GetFirstLBA() % sectorAlignment) != 0) {
srs5694fed16d02010-01-27 23:03:40 -0500277 cout << "\nCaution: Partition " << i + 1 << " doesn't begin on a "
278 << sectorAlignment << "-sector boundary. This may\nresult "
279 << "in degraded performance on some modern (2009 and later) hard disks.\n";
srs569464cbd172011-03-01 22:03:54 -0500280 alignProbs++;
srs56941d1448a2009-12-31 21:20:19 -0500281 } // if
282 } // for
srs569464cbd172011-03-01 22:03:54 -0500283 if (alignProbs > 0)
284 cout << "\nConsult http://www.ibm.com/developerworks/linux/library/l-4kb-sector-disks/\n"
285 << "for information on disk alignment.\n";
srs56941d1448a2009-12-31 21:20:19 -0500286
srs5694e4ac11e2009-08-31 10:13:04 -0400287 // Now compute available space, but only if no problems found, since
288 // problems could affect the results
289 if (problems == 0) {
290 totalFree = FindFreeBlocks(&numSegments, &largestSegment);
srs569464cbd172011-03-01 22:03:54 -0500291 cout << "\nNo problems found. " << totalFree << " free sectors ("
srs56940873e9d2010-10-07 13:00:45 -0400292 << BytesToSI(totalFree, blockSize) << ") available in "
srs5694fed16d02010-01-27 23:03:40 -0500293 << numSegments << "\nsegments, the largest of which is "
srs56940873e9d2010-10-07 13:00:45 -0400294 << largestSegment << " (" << BytesToSI(largestSegment, blockSize)
srs56940283dae2010-04-28 16:44:34 -0400295 << ") in size.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400296 } else {
srs56940a697312010-01-28 21:10:52 -0500297 cout << "\nIdentified " << problems << " problems!\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400298 } // if/else
srs5694e4ac11e2009-08-31 10:13:04 -0400299
300 return (problems);
301} // GPTData::Verify()
srs5694e7b4ff92009-08-18 13:16:10 -0400302
303// Checks to see if the GPT tables overrun existing partitions; if they
srs5694221e0872009-08-29 15:00:31 -0400304// do, issues a warning but takes no action. Returns number of problems
305// detected (0 if OK, 1 to 2 if problems).
srs5694e7b4ff92009-08-18 13:16:10 -0400306int GPTData::CheckGPTSize(void) {
307 uint64_t overlap, firstUsedBlock, lastUsedBlock;
308 uint32_t i;
srs5694221e0872009-08-29 15:00:31 -0400309 int numProbs = 0;
srs5694e7b4ff92009-08-18 13:16:10 -0400310
311 // first, locate the first & last used blocks
312 firstUsedBlock = UINT64_MAX;
313 lastUsedBlock = 0;
srs56940283dae2010-04-28 16:44:34 -0400314 for (i = 0; i < numParts; i++) {
srs5694221e0872009-08-29 15:00:31 -0400315 if ((partitions[i].GetFirstLBA() < firstUsedBlock) &&
srs5694e4ac11e2009-08-31 10:13:04 -0400316 (partitions[i].GetFirstLBA() != 0))
srs5694221e0872009-08-29 15:00:31 -0400317 firstUsedBlock = partitions[i].GetFirstLBA();
318 if (partitions[i].GetLastLBA() > lastUsedBlock)
319 lastUsedBlock = partitions[i].GetLastLBA();
srs5694e7b4ff92009-08-18 13:16:10 -0400320 } // for
321
322 // If the disk size is 0 (the default), then it means that various
323 // variables aren't yet set, so the below tests will be useless;
324 // therefore we should skip everything
325 if (diskSize != 0) {
326 if (mainHeader.firstUsableLBA > firstUsedBlock) {
327 overlap = mainHeader.firstUsableLBA - firstUsedBlock;
srs5694fed16d02010-01-27 23:03:40 -0500328 cout << "Warning! Main partition table overlaps the first partition by "
329 << overlap << " blocks!\n";
srs5694221e0872009-08-29 15:00:31 -0400330 if (firstUsedBlock > 2) {
srs5694fed16d02010-01-27 23:03:40 -0500331 cout << "Try reducing the partition table size by " << overlap * 4
332 << " entries.\n(Use the 's' item on the experts' menu.)\n";
srs5694221e0872009-08-29 15:00:31 -0400333 } else {
srs5694fed16d02010-01-27 23:03:40 -0500334 cout << "You will need to delete this partition or resize it in another utility.\n";
srs5694221e0872009-08-29 15:00:31 -0400335 } // if/else
336 numProbs++;
srs5694e7b4ff92009-08-18 13:16:10 -0400337 } // Problem at start of disk
338 if (mainHeader.lastUsableLBA < lastUsedBlock) {
339 overlap = lastUsedBlock - mainHeader.lastUsableLBA;
srs569455d92612010-03-07 22:16:07 -0500340 cout << "\nWarning! Secondary partition table overlaps the last partition by\n"
srs5694fed16d02010-01-27 23:03:40 -0500341 << overlap << " blocks!\n";
srs5694221e0872009-08-29 15:00:31 -0400342 if (lastUsedBlock > (diskSize - 2)) {
srs5694fed16d02010-01-27 23:03:40 -0500343 cout << "You will need to delete this partition or resize it in another utility.\n";
srs5694221e0872009-08-29 15:00:31 -0400344 } else {
srs5694fed16d02010-01-27 23:03:40 -0500345 cout << "Try reducing the partition table size by " << overlap * 4
346 << " entries.\n(Use the 's' item on the experts' menu.)\n";
srs5694221e0872009-08-29 15:00:31 -0400347 } // if/else
348 numProbs++;
srs5694e7b4ff92009-08-18 13:16:10 -0400349 } // Problem at end of disk
350 } // if (diskSize != 0)
srs5694221e0872009-08-29 15:00:31 -0400351 return numProbs;
srs5694e7b4ff92009-08-18 13:16:10 -0400352} // GPTData::CheckGPTSize()
353
srs5694e7b4ff92009-08-18 13:16:10 -0400354// Check the validity of the GPT header. Returns 1 if the main header
355// is valid, 2 if the backup header is valid, 3 if both are valid, and
356// 0 if neither is valid. Note that this function just checks the GPT
357// signature and revision numbers, not CRCs or other data.
358int GPTData::CheckHeaderValidity(void) {
359 int valid = 3;
360
srs5694fed16d02010-01-27 23:03:40 -0500361 cout.setf(ios::uppercase);
362 cout.fill('0');
363
364 // Note: failed GPT signature checks produce no error message because
365 // a message is displayed in the ReversePartitionBytes() function
srs5694e7b4ff92009-08-18 13:16:10 -0400366 if (mainHeader.signature != GPT_SIGNATURE) {
367 valid -= 1;
srs5694e7b4ff92009-08-18 13:16:10 -0400368 } else if ((mainHeader.revision != 0x00010000) && valid) {
369 valid -= 1;
srs5694fed16d02010-01-27 23:03:40 -0500370 cout << "Unsupported GPT version in main header; read 0x";
371 cout.width(8);
372 cout << hex << mainHeader.revision << ", should be\n0x";
373 cout.width(8);
374 cout << UINT32_C(0x00010000) << dec << "\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400375 } // if/else/if
376
377 if (secondHeader.signature != GPT_SIGNATURE) {
378 valid -= 2;
srs5694e7b4ff92009-08-18 13:16:10 -0400379 } else if ((secondHeader.revision != 0x00010000) && valid) {
380 valid -= 2;
srs5694fed16d02010-01-27 23:03:40 -0500381 cout << "Unsupported GPT version in backup header; read 0x";
382 cout.width(8);
383 cout << hex << secondHeader.revision << ", should be\n0x";
384 cout.width(8);
385 cout << UINT32_C(0x00010000) << dec << "\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400386 } // if/else/if
387
srs5694df9d3632011-01-08 18:33:24 -0500388 // Check for an Apple disk signature
389 if (((mainHeader.signature << 32) == APM_SIGNATURE1) ||
390 (mainHeader.signature << 32) == APM_SIGNATURE2) {
srs5694221e0872009-08-29 15:00:31 -0400391 apmFound = 1; // Will display warning message later
srs56943f2fe992009-11-24 18:28:18 -0500392 } // if
srs5694fed16d02010-01-27 23:03:40 -0500393 cout.fill(' ');
srs56942a9f5da2009-08-26 00:48:01 -0400394
srs5694fed16d02010-01-27 23:03:40 -0500395 return valid;
srs5694e7b4ff92009-08-18 13:16:10 -0400396} // GPTData::CheckHeaderValidity()
397
398// Check the header CRC to see if it's OK...
srs5694cb76c672010-02-11 22:22:22 -0500399// Note: Must be called with header in LITTLE-ENDIAN
400// (x86, x86-64, etc.) byte order.
srs5694e7b4ff92009-08-18 13:16:10 -0400401int GPTData::CheckHeaderCRC(struct GPTHeader* header) {
srs5694978041c2009-09-21 20:51:47 -0400402 uint32_t oldCRC, newCRC, hSize;
srs5694e7b4ff92009-08-18 13:16:10 -0400403
srs56942a9f5da2009-08-26 00:48:01 -0400404 // Back up old header CRC and then blank it, since it must be 0 for
srs5694e7b4ff92009-08-18 13:16:10 -0400405 // computation to be valid
406 oldCRC = header->headerCRC;
407 header->headerCRC = UINT32_C(0);
srs5694978041c2009-09-21 20:51:47 -0400408 hSize = header->headerSize;
409
410 // If big-endian system, reverse byte order
411 if (IsLittleEndian() == 0) {
412 ReverseBytes(&oldCRC, 4);
413 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400414
415 // Initialize CRC functions...
416 chksum_crc32gentab();
417
418 // Compute CRC, restore original, and return result of comparison
419 newCRC = chksum_crc32((unsigned char*) header, HEADER_SIZE);
srs5694978041c2009-09-21 20:51:47 -0400420 header->headerCRC = oldCRC;
srs5694e7b4ff92009-08-18 13:16:10 -0400421 return (oldCRC == newCRC);
422} // GPTData::CheckHeaderCRC()
423
srs56946699b012010-02-04 00:55:30 -0500424// Recompute all the CRCs. Must be called before saving if any changes have
425// been made. Must be called on platform-ordered data (this function reverses
426// byte order and then undoes that reversal.)
srs5694e7b4ff92009-08-18 13:16:10 -0400427void GPTData::RecomputeCRCs(void) {
srs56940283dae2010-04-28 16:44:34 -0400428 uint32_t crc, hSize;
srs56942a9f5da2009-08-26 00:48:01 -0400429 int littleEndian = 1;
srs5694e7b4ff92009-08-18 13:16:10 -0400430
431 // Initialize CRC functions...
432 chksum_crc32gentab();
433
srs56946699b012010-02-04 00:55:30 -0500434 // Save some key data from header before reversing byte order....
srs5694978041c2009-09-21 20:51:47 -0400435 hSize = mainHeader.headerSize;
srs56946699b012010-02-04 00:55:30 -0500436
437 if ((littleEndian = IsLittleEndian()) == 0) {
438 ReversePartitionBytes();
439 ReverseHeaderBytes(&mainHeader);
440 ReverseHeaderBytes(&secondHeader);
441 } // if
srs56942a9f5da2009-08-26 00:48:01 -0400442
srs5694e7b4ff92009-08-18 13:16:10 -0400443 // Compute CRC of partition tables & store in main and secondary headers
srs56940283dae2010-04-28 16:44:34 -0400444 crc = chksum_crc32((unsigned char*) partitions, numParts * GPT_SIZE);
srs5694e7b4ff92009-08-18 13:16:10 -0400445 mainHeader.partitionEntriesCRC = crc;
446 secondHeader.partitionEntriesCRC = crc;
srs56942a9f5da2009-08-26 00:48:01 -0400447 if (littleEndian == 0) {
srs5694221e0872009-08-29 15:00:31 -0400448 ReverseBytes(&mainHeader.partitionEntriesCRC, 4);
449 ReverseBytes(&secondHeader.partitionEntriesCRC, 4);
srs56942a9f5da2009-08-26 00:48:01 -0400450 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400451
452 // Zero out GPT tables' own CRCs (required for correct computation)
453 mainHeader.headerCRC = 0;
454 secondHeader.headerCRC = 0;
455
456 // Compute & store CRCs of main & secondary headers...
srs5694978041c2009-09-21 20:51:47 -0400457 crc = chksum_crc32((unsigned char*) &mainHeader, hSize);
srs56942a9f5da2009-08-26 00:48:01 -0400458 if (littleEndian == 0)
srs5694221e0872009-08-29 15:00:31 -0400459 ReverseBytes(&crc, 4);
srs5694e7b4ff92009-08-18 13:16:10 -0400460 mainHeader.headerCRC = crc;
srs5694978041c2009-09-21 20:51:47 -0400461 crc = chksum_crc32((unsigned char*) &secondHeader, hSize);
srs56942a9f5da2009-08-26 00:48:01 -0400462 if (littleEndian == 0)
srs5694221e0872009-08-29 15:00:31 -0400463 ReverseBytes(&crc, 4);
srs5694e7b4ff92009-08-18 13:16:10 -0400464 secondHeader.headerCRC = crc;
srs56946699b012010-02-04 00:55:30 -0500465
466 if ((littleEndian = IsLittleEndian()) == 0) {
467 ReverseHeaderBytes(&mainHeader);
468 ReverseHeaderBytes(&secondHeader);
469 ReversePartitionBytes();
470 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400471} // GPTData::RecomputeCRCs()
472
srs5694e7b4ff92009-08-18 13:16:10 -0400473// Rebuild the main GPT header, using the secondary header as a model.
474// Typically called when the main header has been found to be corrupt.
475void GPTData::RebuildMainHeader(void) {
476 int i;
477
478 mainHeader.signature = GPT_SIGNATURE;
479 mainHeader.revision = secondHeader.revision;
srs5694978041c2009-09-21 20:51:47 -0400480 mainHeader.headerSize = secondHeader.headerSize;
srs5694e7b4ff92009-08-18 13:16:10 -0400481 mainHeader.headerCRC = UINT32_C(0);
482 mainHeader.reserved = secondHeader.reserved;
483 mainHeader.currentLBA = secondHeader.backupLBA;
484 mainHeader.backupLBA = secondHeader.currentLBA;
485 mainHeader.firstUsableLBA = secondHeader.firstUsableLBA;
486 mainHeader.lastUsableLBA = secondHeader.lastUsableLBA;
srs56946699b012010-02-04 00:55:30 -0500487 mainHeader.diskGUID = secondHeader.diskGUID;
srs5694e7b4ff92009-08-18 13:16:10 -0400488 mainHeader.partitionEntriesLBA = UINT64_C(2);
489 mainHeader.numParts = secondHeader.numParts;
490 mainHeader.sizeOfPartitionEntries = secondHeader.sizeOfPartitionEntries;
491 mainHeader.partitionEntriesCRC = secondHeader.partitionEntriesCRC;
492 for (i = 0 ; i < GPT_RESERVED; i++)
493 mainHeader.reserved2[i] = secondHeader.reserved2[i];
srs5694546a9c72010-01-26 16:00:26 -0500494 mainCrcOk = secondCrcOk;
srs56940283dae2010-04-28 16:44:34 -0400495 SetGPTSize(mainHeader.numParts);
srs5694e7b4ff92009-08-18 13:16:10 -0400496} // GPTData::RebuildMainHeader()
497
498// Rebuild the secondary GPT header, using the main header as a model.
499void GPTData::RebuildSecondHeader(void) {
500 int i;
501
502 secondHeader.signature = GPT_SIGNATURE;
503 secondHeader.revision = mainHeader.revision;
srs5694978041c2009-09-21 20:51:47 -0400504 secondHeader.headerSize = mainHeader.headerSize;
srs5694e7b4ff92009-08-18 13:16:10 -0400505 secondHeader.headerCRC = UINT32_C(0);
506 secondHeader.reserved = mainHeader.reserved;
507 secondHeader.currentLBA = mainHeader.backupLBA;
508 secondHeader.backupLBA = mainHeader.currentLBA;
509 secondHeader.firstUsableLBA = mainHeader.firstUsableLBA;
510 secondHeader.lastUsableLBA = mainHeader.lastUsableLBA;
srs56946699b012010-02-04 00:55:30 -0500511 secondHeader.diskGUID = mainHeader.diskGUID;
srs5694e7b4ff92009-08-18 13:16:10 -0400512 secondHeader.partitionEntriesLBA = secondHeader.lastUsableLBA + UINT64_C(1);
513 secondHeader.numParts = mainHeader.numParts;
514 secondHeader.sizeOfPartitionEntries = mainHeader.sizeOfPartitionEntries;
515 secondHeader.partitionEntriesCRC = mainHeader.partitionEntriesCRC;
516 for (i = 0 ; i < GPT_RESERVED; i++)
517 secondHeader.reserved2[i] = mainHeader.reserved2[i];
srs5694546a9c72010-01-26 16:00:26 -0500518 secondCrcOk = mainCrcOk;
srs56940283dae2010-04-28 16:44:34 -0400519 SetGPTSize(secondHeader.numParts);
srs5694e4ac11e2009-08-31 10:13:04 -0400520} // GPTData::RebuildSecondHeader()
521
522// Search for hybrid MBR entries that have no corresponding GPT partition.
523// Returns number of such mismatches found
524int GPTData::FindHybridMismatches(void) {
srs5694e321d442010-01-29 17:44:04 -0500525 int i, found, numFound = 0;
526 uint32_t j;
srs5694e4ac11e2009-08-31 10:13:04 -0400527 uint64_t mbrFirst, mbrLast;
528
529 for (i = 0; i < 4; i++) {
530 if ((protectiveMBR.GetType(i) != 0xEE) && (protectiveMBR.GetType(i) != 0x00)) {
531 j = 0;
532 found = 0;
533 do {
534 mbrFirst = (uint64_t) protectiveMBR.GetFirstSector(i);
535 mbrLast = mbrFirst + (uint64_t) protectiveMBR.GetLength(i) - UINT64_C(1);
536 if ((partitions[j].GetFirstLBA() == mbrFirst) &&
537 (partitions[j].GetLastLBA() == mbrLast))
538 found = 1;
539 j++;
srs56940283dae2010-04-28 16:44:34 -0400540 } while ((!found) && (j < numParts));
srs5694e4ac11e2009-08-31 10:13:04 -0400541 if (!found) {
542 numFound++;
srs5694fed16d02010-01-27 23:03:40 -0500543 cout << "\nWarning! Mismatched GPT and MBR partition! MBR partition "
544 << i + 1 << ", of type 0x";
545 cout.fill('0');
546 cout.setf(ios::uppercase);
547 cout.width(2);
548 cout << hex << (int) protectiveMBR.GetType(i) << ",\n"
549 << "has no corresponding GPT partition! You may continue, but this condition\n"
550 << "might cause data loss in the future!\a\n" << dec;
551 cout.fill(' ');
srs5694e4ac11e2009-08-31 10:13:04 -0400552 } // if
553 } // if
554 } // for
555 return numFound;
556} // GPTData::FindHybridMismatches
557
558// Find overlapping partitions and warn user about them. Returns number of
559// overlapping partitions.
560int GPTData::FindOverlaps(void) {
srs5694e321d442010-01-29 17:44:04 -0500561 int problems = 0;
562 uint32_t i, j;
srs5694e4ac11e2009-08-31 10:13:04 -0400563
srs56940283dae2010-04-28 16:44:34 -0400564 for (i = 1; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -0400565 for (j = 0; j < i; j++) {
srs56940a697312010-01-28 21:10:52 -0500566 if (partitions[i].DoTheyOverlap(partitions[j])) {
srs5694e4ac11e2009-08-31 10:13:04 -0400567 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500568 cout << "\nProblem: partitions " << i + 1 << " and " << j + 1 << " overlap:\n";
569 cout << " Partition " << i + 1 << ": " << partitions[i].GetFirstLBA()
570 << " to " << partitions[i].GetLastLBA() << "\n";
571 cout << " Partition " << j + 1 << ": " << partitions[j].GetFirstLBA()
572 << " to " << partitions[j].GetLastLBA() << "\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400573 } // if
574 } // for j...
575 } // for i...
576 return problems;
577} // GPTData::FindOverlaps()
578
srs569455d92612010-03-07 22:16:07 -0500579// Find partitions that are insane -- they start after they end or are too
580// big for the disk. (The latter should duplicate detection of overlaps
581// with GPT backup data structures, but better to err on the side of
582// redundant tests than to miss something....)
583int GPTData::FindInsanePartitions(void) {
584 uint32_t i;
585 int problems = 0;
586
srs56940283dae2010-04-28 16:44:34 -0400587 for (i = 0; i < numParts; i++) {
srs569455d92612010-03-07 22:16:07 -0500588 if (partitions[i].GetFirstLBA() > partitions[i].GetLastLBA()) {
589 problems++;
srs56940283dae2010-04-28 16:44:34 -0400590 cout << "\nProblem: partition " << i + 1 << " ends before it begins.\n";
srs569455d92612010-03-07 22:16:07 -0500591 } // if
592 if (partitions[i].GetLastLBA() >= diskSize) {
593 problems++;
srs56940873e9d2010-10-07 13:00:45 -0400594 cout << "\nProblem: partition " << i + 1 << " is too big for the disk.\n";
srs569455d92612010-03-07 22:16:07 -0500595 } // if
596 } // for
597 return problems;
598} // GPTData::FindInsanePartitions(void)
599
600
srs5694e4ac11e2009-08-31 10:13:04 -0400601/******************************************************************
602 * *
603 * Begin functions that load data from disk or save data to disk. *
604 * *
605 ******************************************************************/
606
srs569464cbd172011-03-01 22:03:54 -0500607// Change the filename associated with the GPT. Used for duplicating
608// the partition table to a new disk and saving backups.
609// Returns 1 on success, 0 on failure.
srs5694bf8950c2011-03-12 01:23:12 -0500610int GPTData::SetDisk(const string & deviceFilename) {
srs569464cbd172011-03-01 22:03:54 -0500611 int err, allOK = 1;
612
613 device = deviceFilename;
614 if (allOK && myDisk.OpenForRead(deviceFilename)) {
615 // store disk information....
616 diskSize = myDisk.DiskSize(&err);
617 blockSize = (uint32_t) myDisk.GetBlockSize();
618 } // if
619 protectiveMBR.SetDisk(&myDisk);
620 protectiveMBR.SetDiskSize(diskSize);
621 protectiveMBR.SetBlockSize(blockSize);
622 return allOK;
srs5694bf8950c2011-03-12 01:23:12 -0500623} // GPTData::SetDisk()
srs569464cbd172011-03-01 22:03:54 -0500624
srs5694e4ac11e2009-08-31 10:13:04 -0400625// Scan for partition data. This function loads the MBR data (regular MBR or
626// protective MBR) and loads BSD disklabel data (which is probably invalid).
627// It also looks for APM data, forces a load of GPT data, and summarizes
628// the results.
srs5694546a9c72010-01-26 16:00:26 -0500629void GPTData::PartitionScan(void) {
srs5694e4ac11e2009-08-31 10:13:04 -0400630 BSDData bsdDisklabel;
srs5694e4ac11e2009-08-31 10:13:04 -0400631
632 // Read the MBR & check for BSD disklabel
srs5694546a9c72010-01-26 16:00:26 -0500633 protectiveMBR.ReadMBRData(&myDisk);
634 bsdDisklabel.ReadBSDData(&myDisk, 0, diskSize - 1);
srs5694e4ac11e2009-08-31 10:13:04 -0400635
636 // Load the GPT data, whether or not it's valid
srs5694546a9c72010-01-26 16:00:26 -0500637 ForceLoadGPTData();
srs5694ba00fed2010-01-12 18:18:36 -0500638
639 if (!beQuiet) {
srs5694fed16d02010-01-27 23:03:40 -0500640 cout << "Partition table scan:\n";
srs5694ba00fed2010-01-12 18:18:36 -0500641 protectiveMBR.ShowState();
642 bsdDisklabel.ShowState();
643 ShowAPMState(); // Show whether there's an Apple Partition Map present
644 ShowGPTState(); // Show GPT status
srs5694fed16d02010-01-27 23:03:40 -0500645 cout << "\n";
srs5694ba00fed2010-01-12 18:18:36 -0500646 } // if
srs5694e4ac11e2009-08-31 10:13:04 -0400647
648 if (apmFound) {
srs5694fed16d02010-01-27 23:03:40 -0500649 cout << "\n*******************************************************************\n"
650 << "This disk appears to contain an Apple-format (APM) partition table!\n";
srs56945d58fe02010-01-03 20:57:08 -0500651 if (!justLooking) {
srs5694fed16d02010-01-27 23:03:40 -0500652 cout << "It will be destroyed if you continue!\n";
srs56945d58fe02010-01-03 20:57:08 -0500653 } // if
srs5694fed16d02010-01-27 23:03:40 -0500654 cout << "*******************************************************************\n\n\a";
srs5694e4ac11e2009-08-31 10:13:04 -0400655 } // if
srs5694e4ac11e2009-08-31 10:13:04 -0400656} // GPTData::PartitionScan()
657
658// Read GPT data from a disk.
srs56940a697312010-01-28 21:10:52 -0500659int GPTData::LoadPartitions(const string & deviceFilename) {
srs569408bb0da2010-02-19 17:19:55 -0500660 BSDData bsdDisklabel;
srs5694e321d442010-01-29 17:44:04 -0500661 int err, allOK = 1;
srs5694fed16d02010-01-27 23:03:40 -0500662 MBRValidity mbrState;
srs5694e4ac11e2009-08-31 10:13:04 -0400663
srs5694546a9c72010-01-26 16:00:26 -0500664 if (myDisk.OpenForRead(deviceFilename)) {
srs569455d92612010-03-07 22:16:07 -0500665 err = myDisk.OpenForWrite(deviceFilename);
666 if ((err == 0) && (!justLooking)) {
667 cout << "\aNOTE: Write test failed with error number " << errno
668 << ". It will be impossible to save\nchanges to this disk's partition table!\n";
669#if defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
670 cout << "You may be able to enable writes by exiting this program, typing\n"
671 << "'sysctl kern.geom.debugflags=16' at a shell prompt, and re-running this\n"
672 << "program.\n";
673#endif
674 cout << "\n";
675 } // if
676 myDisk.Close(); // Close and re-open read-only in case of bugs
677 } else allOK = 0; // if
678
679 if (allOK && myDisk.OpenForRead(deviceFilename)) {
srs5694e4ac11e2009-08-31 10:13:04 -0400680 // store disk information....
srs5694546a9c72010-01-26 16:00:26 -0500681 diskSize = myDisk.DiskSize(&err);
682 blockSize = (uint32_t) myDisk.GetBlockSize();
srs5694fed16d02010-01-27 23:03:40 -0500683 device = deviceFilename;
srs5694546a9c72010-01-26 16:00:26 -0500684 PartitionScan(); // Check for partition types, load GPT, & print summary
srs5694e4ac11e2009-08-31 10:13:04 -0400685
srs5694ba00fed2010-01-12 18:18:36 -0500686 whichWasUsed = UseWhichPartitions();
687 switch (whichWasUsed) {
srs5694e4ac11e2009-08-31 10:13:04 -0400688 case use_mbr:
689 XFormPartitions();
690 break;
691 case use_bsd:
srs5694546a9c72010-01-26 16:00:26 -0500692 bsdDisklabel.ReadBSDData(&myDisk, 0, diskSize - 1);
srs5694e4ac11e2009-08-31 10:13:04 -0400693// bsdDisklabel.DisplayBSDData();
694 ClearGPTData();
695 protectiveMBR.MakeProtectiveMBR(1); // clear boot area (option 1)
srs569408bb0da2010-02-19 17:19:55 -0500696 XFormDisklabel(&bsdDisklabel);
srs5694e4ac11e2009-08-31 10:13:04 -0400697 break;
698 case use_gpt:
srs5694fed16d02010-01-27 23:03:40 -0500699 mbrState = protectiveMBR.GetValidity();
700 if ((mbrState == invalid) || (mbrState == mbr))
701 protectiveMBR.MakeProtectiveMBR();
srs5694e4ac11e2009-08-31 10:13:04 -0400702 break;
703 case use_new:
704 ClearGPTData();
705 protectiveMBR.MakeProtectiveMBR();
706 break;
srs56943c0af382010-01-15 19:19:18 -0500707 case use_abort:
708 allOK = 0;
srs56949ddc14b2010-08-22 22:44:42 -0400709 cerr << "Invalid partition data!\n";
srs56943c0af382010-01-15 19:19:18 -0500710 break;
srs5694e4ac11e2009-08-31 10:13:04 -0400711 } // switch
712
srs569455d92612010-03-07 22:16:07 -0500713 if (allOK)
srs56943c0af382010-01-15 19:19:18 -0500714 CheckGPTSize();
srs569455d92612010-03-07 22:16:07 -0500715 myDisk.Close();
srs5694a8582cf2010-03-19 14:21:59 -0400716 ComputeAlignment();
srs5694e4ac11e2009-08-31 10:13:04 -0400717 } else {
718 allOK = 0;
srs5694e4ac11e2009-08-31 10:13:04 -0400719 } // if/else
720 return (allOK);
721} // GPTData::LoadPartitions()
722
723// Loads the GPT, as much as possible. Returns 1 if this seems to have
724// succeeded, 0 if there are obvious problems....
srs5694546a9c72010-01-26 16:00:26 -0500725int GPTData::ForceLoadGPTData(void) {
srs5694cb76c672010-02-11 22:22:22 -0500726 int allOK, validHeaders, loadedTable = 1;
srs5694e4ac11e2009-08-31 10:13:04 -0400727
srs5694cb76c672010-02-11 22:22:22 -0500728 allOK = LoadHeader(&mainHeader, myDisk, 1, &mainCrcOk);
srs5694e4ac11e2009-08-31 10:13:04 -0400729
srs5694cb76c672010-02-11 22:22:22 -0500730 if (mainCrcOk && (mainHeader.backupLBA < diskSize)) {
731 allOK = LoadHeader(&secondHeader, myDisk, mainHeader.backupLBA, &secondCrcOk) && allOK;
732 } else {
srs569408bb0da2010-02-19 17:19:55 -0500733 allOK = LoadHeader(&secondHeader, myDisk, diskSize - UINT64_C(1), &secondCrcOk) && allOK;
734 if (mainCrcOk && (mainHeader.backupLBA >= diskSize))
srs5694fed16d02010-01-27 23:03:40 -0500735 cout << "Warning! Disk size is smaller than the main header indicates! Loading\n"
736 << "secondary header from the last sector of the disk! You should use 'v' to\n"
737 << "verify disk integrity, and perhaps options on the experts' menu to repair\n"
738 << "the disk.\n";
srs5694cb76c672010-02-11 22:22:22 -0500739 } // if/else
740 if (!allOK)
srs5694e4ac11e2009-08-31 10:13:04 -0400741 state = gpt_invalid;
srs5694e4ac11e2009-08-31 10:13:04 -0400742
743 // Return valid headers code: 0 = both headers bad; 1 = main header
744 // good, backup bad; 2 = backup header good, main header bad;
745 // 3 = both headers good. Note these codes refer to valid GPT
746 // signatures and version numbers; more subtle problems will elude
747 // this check!
748 validHeaders = CheckHeaderValidity();
749
750 // Read partitions (from primary array)
751 if (validHeaders > 0) { // if at least one header is OK....
752 // GPT appears to be valid....
753 state = gpt_valid;
754
755 // We're calling the GPT valid, but there's a possibility that one
756 // of the two headers is corrupt. If so, use the one that seems to
757 // be in better shape to regenerate the bad one
srs5694546a9c72010-01-26 16:00:26 -0500758 if (validHeaders == 1) { // valid main header, invalid backup header
srs5694fed16d02010-01-27 23:03:40 -0500759 cerr << "\aCaution: invalid backup GPT header, but valid main header; regenerating\n"
760 << "backup header from main header.\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400761 RebuildSecondHeader();
srs5694546a9c72010-01-26 16:00:26 -0500762 state = gpt_corrupt;
srs5694e4ac11e2009-08-31 10:13:04 -0400763 secondCrcOk = mainCrcOk; // Since regenerated, use CRC validity of main
srs5694546a9c72010-01-26 16:00:26 -0500764 } else if (validHeaders == 2) { // valid backup header, invalid main header
srs5694fed16d02010-01-27 23:03:40 -0500765 cerr << "\aCaution: invalid main GPT header, but valid backup; regenerating main header\n"
766 << "from backup!\n\n";
srs5694546a9c72010-01-26 16:00:26 -0500767 RebuildMainHeader();
768 state = gpt_corrupt;
769 mainCrcOk = secondCrcOk; // Since copied, use CRC validity of backup
srs5694e4ac11e2009-08-31 10:13:04 -0400770 } // if/else/if
771
srs5694546a9c72010-01-26 16:00:26 -0500772 // Figure out which partition table to load....
773 // Load the main partition table, since either its header's CRC is OK or the
774 // backup header's CRC is not OK....
775 if (mainCrcOk || !secondCrcOk) {
776 if (LoadMainTable() == 0)
777 allOK = 0;
778 } else { // bad main header CRC and backup header CRC is OK
779 state = gpt_corrupt;
780 if (LoadSecondTableAsMain()) {
srs5694cb76c672010-02-11 22:22:22 -0500781 loadedTable = 2;
srs5694fed16d02010-01-27 23:03:40 -0500782 cerr << "\aWarning: Invalid CRC on main header data; loaded backup partition table.\n";
srs5694546a9c72010-01-26 16:00:26 -0500783 } else { // backup table bad, bad main header CRC, but try main table in desperation....
784 if (LoadMainTable() == 0) {
785 allOK = 0;
srs5694cb76c672010-02-11 22:22:22 -0500786 loadedTable = 0;
srs5694fed16d02010-01-27 23:03:40 -0500787 cerr << "\a\aWarning! Unable to load either main or backup partition table!\n";
srs5694546a9c72010-01-26 16:00:26 -0500788 } // if
789 } // if/else (LoadSecondTableAsMain())
790 } // if/else (load partition table)
srs5694e4ac11e2009-08-31 10:13:04 -0400791
srs5694cb76c672010-02-11 22:22:22 -0500792 if (loadedTable == 1)
793 secondPartsCrcOk = CheckTable(&secondHeader);
794 else if (loadedTable == 2)
795 mainPartsCrcOk = CheckTable(&mainHeader);
796 else
797 mainPartsCrcOk = secondPartsCrcOk = 0;
srs5694e4ac11e2009-08-31 10:13:04 -0400798
srs5694546a9c72010-01-26 16:00:26 -0500799 // Problem with main partition table; if backup is OK, use it instead....
800 if (secondPartsCrcOk && secondCrcOk && !mainPartsCrcOk) {
801 state = gpt_corrupt;
802 allOK = allOK && LoadSecondTableAsMain();
srs5694cb76c672010-02-11 22:22:22 -0500803 mainPartsCrcOk = 0; // LoadSecondTableAsMain() resets this, so re-flag as bad
srs5694fed16d02010-01-27 23:03:40 -0500804 cerr << "\aWarning! Main partition table CRC mismatch! Loaded backup "
805 << "partition table\ninstead of main partition table!\n\n";
srs5694cb76c672010-02-11 22:22:22 -0500806 } // if */
srs5694546a9c72010-01-26 16:00:26 -0500807
srs5694e4ac11e2009-08-31 10:13:04 -0400808 // Check for valid CRCs and warn if there are problems
809 if ((mainCrcOk == 0) || (secondCrcOk == 0) || (mainPartsCrcOk == 0) ||
810 (secondPartsCrcOk == 0)) {
srs5694fed16d02010-01-27 23:03:40 -0500811 cerr << "Warning! One or more CRCs don't match. You should repair the disk!\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400812 state = gpt_corrupt;
srs5694ba00fed2010-01-12 18:18:36 -0500813 } // if
srs5694e4ac11e2009-08-31 10:13:04 -0400814 } else {
815 state = gpt_invalid;
816 } // if/else
817 return allOK;
818} // GPTData::ForceLoadGPTData()
819
srs5694247657a2009-11-26 18:36:12 -0500820// Loads the partition table pointed to by the main GPT header. The
srs5694e4ac11e2009-08-31 10:13:04 -0400821// main GPT header in memory MUST be valid for this call to do anything
822// sensible!
srs5694546a9c72010-01-26 16:00:26 -0500823// Returns 1 on success, 0 on failure. CRC errors do NOT count as failure.
srs5694e4ac11e2009-08-31 10:13:04 -0400824int GPTData::LoadMainTable(void) {
srs5694cb76c672010-02-11 22:22:22 -0500825 return LoadPartitionTable(mainHeader, myDisk);
srs5694e4ac11e2009-08-31 10:13:04 -0400826} // GPTData::LoadMainTable()
srs5694e7b4ff92009-08-18 13:16:10 -0400827
828// Load the second (backup) partition table as the primary partition
srs5694546a9c72010-01-26 16:00:26 -0500829// table. Used in repair functions, and when starting up if the main
830// partition table is damaged.
831// Returns 1 on success, 0 on failure. CRC errors do NOT count as failure.
832int GPTData::LoadSecondTableAsMain(void) {
srs5694cb76c672010-02-11 22:22:22 -0500833 return LoadPartitionTable(secondHeader, myDisk);
834} // GPTData::LoadSecondTableAsMain()
srs5694e7b4ff92009-08-18 13:16:10 -0400835
srs5694cb76c672010-02-11 22:22:22 -0500836// Load a single GPT header (main or backup) from the specified disk device and
837// sector. Applies byte-order corrections on big-endian platforms. Sets crcOk
838// value appropriately.
839// Returns 1 on success, 0 on failure. Note that CRC errors do NOT qualify as
840// failure.
841int GPTData::LoadHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector, int *crcOk) {
842 int allOK = 1;
srs56941c6f8b02010-02-21 11:09:20 -0500843 GPTHeader tempHeader;
srs5694cb76c672010-02-11 22:22:22 -0500844
845 disk.Seek(sector);
srs56941c6f8b02010-02-21 11:09:20 -0500846 if (disk.Read(&tempHeader, 512) != 512) {
srs5694cb76c672010-02-11 22:22:22 -0500847 cerr << "Warning! Read error " << errno << "; strange behavior now likely!\n";
848 allOK = 0;
849 } // if
srs56941c6f8b02010-02-21 11:09:20 -0500850 *crcOk = CheckHeaderCRC(&tempHeader);
srs5694cb76c672010-02-11 22:22:22 -0500851
srs56941c6f8b02010-02-21 11:09:20 -0500852 // Reverse byte order, if necessary
srs5694cb76c672010-02-11 22:22:22 -0500853 if (IsLittleEndian() == 0) {
srs569455d92612010-03-07 22:16:07 -0500854 ReverseHeaderBytes(&tempHeader);
srs5694cb76c672010-02-11 22:22:22 -0500855 } // if
srs56941c6f8b02010-02-21 11:09:20 -0500856
srs56940283dae2010-04-28 16:44:34 -0400857 if (allOK && (numParts != tempHeader.numParts) && *crcOk) {
srs56941c6f8b02010-02-21 11:09:20 -0500858 allOK = SetGPTSize(tempHeader.numParts);
srs569455d92612010-03-07 22:16:07 -0500859 }
srs56941c6f8b02010-02-21 11:09:20 -0500860
861 *header = tempHeader;
srs5694cb76c672010-02-11 22:22:22 -0500862 return allOK;
863} // GPTData::LoadHeader
864
865// Load a partition table (either main or secondary) from the specified disk,
866// using header as a reference for what to load. If sector != 0 (the default
867// is 0), loads from the specified sector; otherwise loads from the sector
868// indicated in header.
869// Returns 1 on success, 0 on failure. CRC errors do NOT count as failure.
870int GPTData::LoadPartitionTable(const struct GPTHeader & header, DiskIO & disk, uint64_t sector) {
871 uint32_t sizeOfParts, newCRC;
872 int retval;
873
874 if (disk.OpenForRead()) {
875 if (sector == 0) {
876 retval = disk.Seek(header.partitionEntriesLBA);
877 } else {
878 retval = disk.Seek(sector);
879 } // if/else
srs569455d92612010-03-07 22:16:07 -0500880 if (retval == 1)
881 retval = SetGPTSize(header.numParts);
srs5694546a9c72010-01-26 16:00:26 -0500882 if (retval == 1) {
srs5694cb76c672010-02-11 22:22:22 -0500883 sizeOfParts = header.numParts * header.sizeOfPartitionEntries;
884 if (disk.Read(partitions, sizeOfParts) != (int) sizeOfParts) {
srs5694fed16d02010-01-27 23:03:40 -0500885 cerr << "Warning! Read error " << errno << "! Misbehavior now likely!\n";
srs5694546a9c72010-01-26 16:00:26 -0500886 retval = 0;
srs56945d58fe02010-01-03 20:57:08 -0500887 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400888 newCRC = chksum_crc32((unsigned char*) partitions, sizeOfParts);
srs5694cb76c672010-02-11 22:22:22 -0500889 mainPartsCrcOk = secondPartsCrcOk = (newCRC == header.partitionEntriesCRC);
srs56942a9f5da2009-08-26 00:48:01 -0400890 if (IsLittleEndian() == 0)
891 ReversePartitionBytes();
srs5694cb76c672010-02-11 22:22:22 -0500892 if (!mainPartsCrcOk) {
893 cout << "Caution! After loading partitions, the CRC doesn't check out!\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400894 } // if
895 } else {
srs5694cb76c672010-02-11 22:22:22 -0500896 cerr << "Error! Couldn't seek to partition table!\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400897 } // if/else
898 } else {
srs5694fed16d02010-01-27 23:03:40 -0500899 cerr << "Error! Couldn't open device " << device
srs5694cb76c672010-02-11 22:22:22 -0500900 << " when reading partition table!\n";
srs5694546a9c72010-01-26 16:00:26 -0500901 retval = 0;
srs5694e7b4ff92009-08-18 13:16:10 -0400902 } // if/else
srs5694546a9c72010-01-26 16:00:26 -0500903 return retval;
srs5694cb76c672010-02-11 22:22:22 -0500904} // GPTData::LoadPartitionsTable()
905
906// Check the partition table pointed to by header, but don't keep it
907// around.
908// Returns 1 if the CRC is OK, 0 if not or if there was a read error.
909int GPTData::CheckTable(struct GPTHeader *header) {
910 uint32_t sizeOfParts, newCRC;
911 uint8_t *storage;
912 int newCrcOk = 0;
913
srs56940283dae2010-04-28 16:44:34 -0400914 // Load partition table into temporary storage to check
srs5694cb76c672010-02-11 22:22:22 -0500915 // its CRC and store the results, then discard this temporary
916 // storage, since we don't use it in any but recovery operations
917 if (myDisk.Seek(header->partitionEntriesLBA)) {
srs56940283dae2010-04-28 16:44:34 -0400918 sizeOfParts = header->numParts * header->sizeOfPartitionEntries;
srs5694cb76c672010-02-11 22:22:22 -0500919 storage = new uint8_t[sizeOfParts];
920 if (myDisk.Read(storage, sizeOfParts) != (int) sizeOfParts) {
srs56940283dae2010-04-28 16:44:34 -0400921 cerr << "Warning! Error " << errno << " reading partition table for CRC check!\n";
srs5694cb76c672010-02-11 22:22:22 -0500922 } else {
923 newCRC = chksum_crc32((unsigned char*) storage, sizeOfParts);
924 newCrcOk = (newCRC == header->partitionEntriesCRC);
925 } // if/else
926 delete[] storage;
927 } // if
928 return newCrcOk;
929} // GPTData::CheckTable()
srs5694e7b4ff92009-08-18 13:16:10 -0400930
srs569464cbd172011-03-01 22:03:54 -0500931// Writes GPT (and protective MBR) to disk. If quiet==1,
932// Returns 1 on successful
srs5694e7b4ff92009-08-18 13:16:10 -0400933// write, 0 if there was a problem.
srs569464cbd172011-03-01 22:03:54 -0500934int GPTData::SaveGPTData(int quiet) {
srs56946699b012010-02-04 00:55:30 -0500935 int allOK = 1, littleEndian;
srs5694e321d442010-01-29 17:44:04 -0500936 char answer;
srs5694e7b4ff92009-08-18 13:16:10 -0400937
srs56946699b012010-02-04 00:55:30 -0500938 littleEndian = IsLittleEndian();
939
srs5694e7b4ff92009-08-18 13:16:10 -0400940 // First do some final sanity checks....
srs56945d58fe02010-01-03 20:57:08 -0500941
942 // This test should only fail on read-only disks....
943 if (justLooking) {
srs5694fed16d02010-01-27 23:03:40 -0500944 cout << "The justLooking flag is set. This probably means you can't write to the disk.\n";
srs56945d58fe02010-01-03 20:57:08 -0500945 allOK = 0;
946 } // if
947
srs569464cbd172011-03-01 22:03:54 -0500948 // Check that disk is really big enough to handle the second header...
949 if (mainHeader.backupLBA >= diskSize) {
950 cerr << "Caution! Secondary header was placed beyond the disk's limits! Moving the\n"
951 << "header, but other problems may occur!\n";
952 MoveSecondHeaderToEnd();
953 } // if
954
srs5694e7b4ff92009-08-18 13:16:10 -0400955 // Is there enough space to hold the GPT headers and partition tables,
956 // given the partition sizes?
srs5694221e0872009-08-29 15:00:31 -0400957 if (CheckGPTSize() > 0) {
srs5694e7b4ff92009-08-18 13:16:10 -0400958 allOK = 0;
959 } // if
960
srs5694247657a2009-11-26 18:36:12 -0500961 // Check that second header is properly placed. Warn and ask if this should
962 // be corrected if the test fails....
srs569464cbd172011-03-01 22:03:54 -0500963 if (mainHeader.backupLBA < (diskSize - UINT64_C(1))) {
964 if (quiet == 0) {
965 cout << "Warning! Secondary header is placed too early on the disk! Do you want to\n"
966 << "correct this problem? ";
967 if (GetYN() == 'Y') {
968 MoveSecondHeaderToEnd();
969 cout << "Have moved second header and partition table to correct location.\n";
970 } else {
971 cout << "Have not corrected the problem. Strange problems may occur in the future!\n";
972 } // if correction requested
973 } else { // Go ahead and do correction automatically
srs5694247657a2009-11-26 18:36:12 -0500974 MoveSecondHeaderToEnd();
srs569464cbd172011-03-01 22:03:54 -0500975 } // if/else quiet
srs5694247657a2009-11-26 18:36:12 -0500976 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400977
srs569455d92612010-03-07 22:16:07 -0500978 // Check for overlapping or insane partitions....
979 if ((FindOverlaps() > 0) || (FindInsanePartitions() > 0)) {
srs5694e4ac11e2009-08-31 10:13:04 -0400980 allOK = 0;
srs5694fed16d02010-01-27 23:03:40 -0500981 cerr << "Aborting write operation!\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400982 } // if
983
984 // Check for mismatched MBR and GPT data, but let it pass if found
985 // (function displays warning message)
986 FindHybridMismatches();
srs5694e7b4ff92009-08-18 13:16:10 -0400987
988 RecomputeCRCs();
989
srs5694ba00fed2010-01-12 18:18:36 -0500990 if ((allOK) && (!quiet)) {
srs5694fed16d02010-01-27 23:03:40 -0500991 cout << "\nFinal checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING\n"
srs5694bf8950c2011-03-12 01:23:12 -0500992 << "PARTITIONS!!\n\nDo you want to proceed? ";
srs56945d58fe02010-01-03 20:57:08 -0500993 answer = GetYN();
994 if (answer == 'Y') {
srs5694fed16d02010-01-27 23:03:40 -0500995 cout << "OK; writing new GUID partition table (GPT).\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400996 } else {
997 allOK = 0;
998 } // if/else
999 } // if
1000
1001 // Do it!
1002 if (allOK) {
srs569464cbd172011-03-01 22:03:54 -05001003 if (myDisk.OpenForWrite()) {
srs56948a4ddfc2010-03-21 19:05:49 -04001004 // As per UEFI specs, write the secondary table and GPT first....
srs5694cb76c672010-02-11 22:22:22 -05001005 allOK = SavePartitionTable(myDisk, secondHeader.partitionEntriesLBA);
1006 if (!allOK)
1007 cerr << "Unable to save backup partition table! Perhaps the 'e' option on the experts'\n"
1008 << "menu will resolve this problem.\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001009
1010 // Now write the secondary GPT header...
srs56948a4ddfc2010-03-21 19:05:49 -04001011 allOK = allOK && SaveHeader(&secondHeader, myDisk, mainHeader.backupLBA);
1012
1013 // Now write the main partition tables...
1014 allOK = allOK && SavePartitionTable(myDisk, mainHeader.partitionEntriesLBA);
1015
1016 // Now write the main GPT header...
1017 allOK = allOK && SaveHeader(&mainHeader, myDisk, 1);
1018
1019 // To top it off, write the protective MBR...
1020 allOK = allOK && protectiveMBR.WriteMBRData(&myDisk);
srs5694e7b4ff92009-08-18 13:16:10 -04001021
1022 // re-read the partition table
1023 if (allOK) {
srs5694546a9c72010-01-26 16:00:26 -05001024 myDisk.DiskSync();
srs5694e7b4ff92009-08-18 13:16:10 -04001025 } // if
1026
1027 if (allOK) { // writes completed OK
srs5694fed16d02010-01-27 23:03:40 -05001028 cout << "The operation has completed successfully.\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001029 } else {
srs5694fed16d02010-01-27 23:03:40 -05001030 cerr << "Warning! An error was reported when writing the partition table! This error\n"
srs56948a4ddfc2010-03-21 19:05:49 -04001031 << "MIGHT be harmless, but you may have trashed the disk!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001032 } // if/else
srs56948a4ddfc2010-03-21 19:05:49 -04001033
srs5694546a9c72010-01-26 16:00:26 -05001034 myDisk.Close();
srs5694e7b4ff92009-08-18 13:16:10 -04001035 } else {
srs569464cbd172011-03-01 22:03:54 -05001036 cerr << "Unable to open device " << myDisk.GetName() << " for writing! Errno is "
srs5694fed16d02010-01-27 23:03:40 -05001037 << errno << "! Aborting write!\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001038 allOK = 0;
srs5694e7b4ff92009-08-18 13:16:10 -04001039 } // if/else
1040 } else {
srs5694fed16d02010-01-27 23:03:40 -05001041 cout << "Aborting write of new partition table.\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001042 } // if
1043
1044 return (allOK);
1045} // GPTData::SaveGPTData()
1046
1047// Save GPT data to a backup file. This function does much less error
1048// checking than SaveGPTData(). It can therefore preserve many types of
1049// corruption for later analysis; however, it preserves only the MBR,
1050// the main GPT header, the backup GPT header, and the main partition
1051// table; it discards the backup partition table, since it should be
1052// identical to the main partition table on healthy disks.
srs56940a697312010-01-28 21:10:52 -05001053int GPTData::SaveGPTBackup(const string & filename) {
1054 int allOK = 1;
srs5694546a9c72010-01-26 16:00:26 -05001055 DiskIO backupFile;
srs5694e7b4ff92009-08-18 13:16:10 -04001056
srs5694546a9c72010-01-26 16:00:26 -05001057 if (backupFile.OpenForWrite(filename)) {
srs56946699b012010-02-04 00:55:30 -05001058 // Recomputing the CRCs is likely to alter them, which could be bad
1059 // if the intent is to save a potentially bad GPT for later analysis;
1060 // but if we don't do this, we get bogus errors when we load the
1061 // backup. I'm favoring misses over false alarms....
1062 RecomputeCRCs();
1063
srs5694546a9c72010-01-26 16:00:26 -05001064 protectiveMBR.WriteMBRData(&backupFile);
srs5694e7b4ff92009-08-18 13:16:10 -04001065
srs5694cb76c672010-02-11 22:22:22 -05001066 if (allOK) {
srs5694546a9c72010-01-26 16:00:26 -05001067 // MBR write closed disk, so re-open and seek to end....
1068 backupFile.OpenForWrite();
srs5694cb76c672010-02-11 22:22:22 -05001069 allOK = SaveHeader(&mainHeader, backupFile, 1);
1070 } // if (allOK)
srs5694e7b4ff92009-08-18 13:16:10 -04001071
srs5694e7b4ff92009-08-18 13:16:10 -04001072 if (allOK)
srs5694cb76c672010-02-11 22:22:22 -05001073 allOK = SaveHeader(&secondHeader, backupFile, 2);
srs5694e7b4ff92009-08-18 13:16:10 -04001074
srs5694cb76c672010-02-11 22:22:22 -05001075 if (allOK)
1076 allOK = SavePartitionTable(backupFile, 3);
srs5694e7b4ff92009-08-18 13:16:10 -04001077
1078 if (allOK) { // writes completed OK
srs5694fed16d02010-01-27 23:03:40 -05001079 cout << "The operation has completed successfully.\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001080 } else {
srs5694fed16d02010-01-27 23:03:40 -05001081 cerr << "Warning! An error was reported when writing the backup file.\n"
1082 << "It may not be usable!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001083 } // if/else
srs5694546a9c72010-01-26 16:00:26 -05001084 backupFile.Close();
srs5694e7b4ff92009-08-18 13:16:10 -04001085 } else {
srs5694fed16d02010-01-27 23:03:40 -05001086 cerr << "Unable to open file " << filename << " for writing! Aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001087 allOK = 0;
1088 } // if/else
1089 return allOK;
1090} // GPTData::SaveGPTBackup()
1091
srs5694cb76c672010-02-11 22:22:22 -05001092// Write a GPT header (main or backup) to the specified sector. Used by both
1093// the SaveGPTData() and SaveGPTBackup() functions.
1094// Should be passed an architecture-appropriate header (DO NOT call
1095// ReverseHeaderBytes() on the header before calling this function)
1096// Returns 1 on success, 0 on failure
1097int GPTData::SaveHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector) {
1098 int littleEndian, allOK = 1;
1099
1100 littleEndian = IsLittleEndian();
1101 if (!littleEndian)
1102 ReverseHeaderBytes(header);
1103 if (disk.Seek(sector)) {
1104 if (disk.Write(header, 512) == -1)
1105 allOK = 0;
1106 } else allOK = 0; // if (disk.Seek()...)
1107 if (!littleEndian)
1108 ReverseHeaderBytes(header);
1109 return allOK;
1110} // GPTData::SaveHeader()
1111
1112// Save the partitions to the specified sector. Used by both the SaveGPTData()
1113// and SaveGPTBackup() functions.
1114// Should be passed an architecture-appropriate header (DO NOT call
1115// ReverseHeaderBytes() on the header before calling this function)
1116// Returns 1 on success, 0 on failure
1117int GPTData::SavePartitionTable(DiskIO & disk, uint64_t sector) {
1118 int littleEndian, allOK = 1;
1119
1120 littleEndian = IsLittleEndian();
1121 if (disk.Seek(sector)) {
1122 if (!littleEndian)
1123 ReversePartitionBytes();
srs56940283dae2010-04-28 16:44:34 -04001124 if (disk.Write(partitions, mainHeader.sizeOfPartitionEntries * numParts) == -1)
srs5694cb76c672010-02-11 22:22:22 -05001125 allOK = 0;
1126 if (!littleEndian)
1127 ReversePartitionBytes();
1128 } else allOK = 0; // if (myDisk.Seek()...)
1129 return allOK;
1130} // GPTData::SavePartitionTable()
1131
srs5694e7b4ff92009-08-18 13:16:10 -04001132// Load GPT data from a backup file created by SaveGPTBackup(). This function
1133// does minimal error checking. It returns 1 if it completed successfully,
1134// 0 if there was a problem. In the latter case, it creates a new empty
1135// set of partitions.
srs56940a697312010-01-28 21:10:52 -05001136int GPTData::LoadGPTBackup(const string & filename) {
srs5694cb76c672010-02-11 22:22:22 -05001137 int allOK = 1, val, err;
srs56940283dae2010-04-28 16:44:34 -04001138 uint32_t sizeOfEntries;
srs5694cb76c672010-02-11 22:22:22 -05001139 int littleEndian = 1, shortBackup = 0;
srs5694546a9c72010-01-26 16:00:26 -05001140 DiskIO backupFile;
srs5694e7b4ff92009-08-18 13:16:10 -04001141
srs5694546a9c72010-01-26 16:00:26 -05001142 if (backupFile.OpenForRead(filename)) {
srs56942a9f5da2009-08-26 00:48:01 -04001143 if (IsLittleEndian() == 0)
1144 littleEndian = 0;
1145
srs5694e7b4ff92009-08-18 13:16:10 -04001146 // Let the MBRData class load the saved MBR...
srs5694546a9c72010-01-26 16:00:26 -05001147 protectiveMBR.ReadMBRData(&backupFile, 0); // 0 = don't check block size
srs5694e7b4ff92009-08-18 13:16:10 -04001148
srs5694cb76c672010-02-11 22:22:22 -05001149 LoadHeader(&mainHeader, backupFile, 1, &mainCrcOk);
srs5694e7b4ff92009-08-18 13:16:10 -04001150
srs5694cb76c672010-02-11 22:22:22 -05001151 // Check backup file size and rebuild second header if file is right
1152 // size to be direct dd copy of MBR, main header, and main partition
1153 // table; if other size, treat it like a GPT fdisk-generated backup
1154 // file
1155 shortBackup = ((backupFile.DiskSize(&err) * backupFile.GetBlockSize()) ==
1156 (mainHeader.numParts * mainHeader.sizeOfPartitionEntries) + 1024);
1157 if (shortBackup) {
1158 RebuildSecondHeader();
1159 secondCrcOk = mainCrcOk;
1160 } else {
1161 LoadHeader(&secondHeader, backupFile, 2, &secondCrcOk);
1162 } // if/else
srs56942a9f5da2009-08-26 00:48:01 -04001163
srs5694e7b4ff92009-08-18 13:16:10 -04001164 // Return valid headers code: 0 = both headers bad; 1 = main header
1165 // good, backup bad; 2 = backup header good, main header bad;
1166 // 3 = both headers good. Note these codes refer to valid GPT
1167 // signatures and version numbers; more subtle problems will elude
1168 // this check!
1169 if ((val = CheckHeaderValidity()) > 0) {
1170 if (val == 2) { // only backup header seems to be good
srs56940283dae2010-04-28 16:44:34 -04001171 SetGPTSize(secondHeader.numParts);
srs5694e4ac11e2009-08-31 10:13:04 -04001172 sizeOfEntries = secondHeader.sizeOfPartitionEntries;
srs5694e7b4ff92009-08-18 13:16:10 -04001173 } else { // main header is OK
srs56940283dae2010-04-28 16:44:34 -04001174 SetGPTSize(mainHeader.numParts);
srs5694e7b4ff92009-08-18 13:16:10 -04001175 sizeOfEntries = mainHeader.sizeOfPartitionEntries;
1176 } // if/else
1177
srs5694e7b4ff92009-08-18 13:16:10 -04001178 if (secondHeader.currentLBA != diskSize - UINT64_C(1)) {
srs5694fed16d02010-01-27 23:03:40 -05001179 cout << "Warning! Current disk size doesn't match that of the backup!\n"
1180 << "Adjusting sizes to match, but subsequent problems are possible!\n";
srs5694247657a2009-11-26 18:36:12 -05001181 MoveSecondHeaderToEnd();
srs5694e7b4ff92009-08-18 13:16:10 -04001182 } // if
1183
srs5694cb76c672010-02-11 22:22:22 -05001184 if (!LoadPartitionTable(mainHeader, backupFile, (uint64_t) (3 - shortBackup)))
1185 cerr << "Warning! Read error " << errno
1186 << " loading partition table; strange behavior now likely!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001187 } else {
1188 allOK = 0;
1189 } // if/else
srs5694a8582cf2010-03-19 14:21:59 -04001190 // Something went badly wrong, so blank out partitions
1191 if (allOK == 0) {
1192 cerr << "Improper backup file! Clearing all partition data!\n";
1193 ClearGPTData();
1194 protectiveMBR.MakeProtectiveMBR();
1195 } // if
srs5694e7b4ff92009-08-18 13:16:10 -04001196 } else {
1197 allOK = 0;
srs5694fed16d02010-01-27 23:03:40 -05001198 cerr << "Unable to open file " << filename << " for reading! Aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001199 } // if/else
1200
srs5694e7b4ff92009-08-18 13:16:10 -04001201 return allOK;
1202} // GPTData::LoadGPTBackup()
1203
srs569408bb0da2010-02-19 17:19:55 -05001204int GPTData::SaveMBR(void) {
srs569455d92612010-03-07 22:16:07 -05001205 return protectiveMBR.WriteMBRData(&myDisk);
srs569408bb0da2010-02-19 17:19:55 -05001206} // GPTData::SaveMBR()
1207
1208// This function destroys the on-disk GPT structures, but NOT the on-disk
1209// MBR.
1210// Returns 1 if the operation succeeds, 0 if not.
1211int GPTData::DestroyGPT(void) {
1212 int i, sum, tableSize, allOK = 1;
1213 uint8_t blankSector[512];
1214 uint8_t* emptyTable;
1215
1216 for (i = 0; i < 512; i++) {
1217 blankSector[i] = 0;
1218 } // for
1219
1220 if (myDisk.OpenForWrite()) {
1221 if (!myDisk.Seek(mainHeader.currentLBA))
1222 allOK = 0;
1223 if (myDisk.Write(blankSector, 512) != 512) { // blank it out
1224 cerr << "Warning! GPT main header not overwritten! Error is " << errno << "\n";
1225 allOK = 0;
1226 } // if
1227 if (!myDisk.Seek(mainHeader.partitionEntriesLBA))
1228 allOK = 0;
srs56940283dae2010-04-28 16:44:34 -04001229 tableSize = numParts * mainHeader.sizeOfPartitionEntries;
srs569408bb0da2010-02-19 17:19:55 -05001230 emptyTable = new uint8_t[tableSize];
1231 for (i = 0; i < tableSize; i++)
1232 emptyTable[i] = 0;
1233 if (allOK) {
1234 sum = myDisk.Write(emptyTable, tableSize);
1235 if (sum != tableSize) {
1236 cerr << "Warning! GPT main partition table not overwritten! Error is " << errno << "\n";
1237 allOK = 0;
1238 } // if write failed
1239 } // if
1240 if (!myDisk.Seek(secondHeader.partitionEntriesLBA))
1241 allOK = 0;
1242 if (allOK) {
1243 sum = myDisk.Write(emptyTable, tableSize);
1244 if (sum != tableSize) {
1245 cerr << "Warning! GPT backup partition table not overwritten! Error is "
1246 << errno << "\n";
1247 allOK = 0;
1248 } // if wrong size written
1249 } // if
1250 if (!myDisk.Seek(secondHeader.currentLBA))
1251 allOK = 0;
1252 if (allOK) {
1253 if (myDisk.Write(blankSector, 512) != 512) { // blank it out
1254 cerr << "Warning! GPT backup header not overwritten! Error is " << errno << "\n";
1255 allOK = 0;
1256 } // if
1257 } // if
1258 myDisk.DiskSync();
1259 myDisk.Close();
1260 cout << "GPT data structures destroyed! You may now partition the disk using fdisk or\n"
1261 << "other utilities.\n";
1262 delete[] emptyTable;
1263 } else {
1264 cerr << "Problem opening " << device << " for writing! Program will now terminate.\n";
1265 } // if/else (fd != -1)
1266 return (allOK);
1267} // GPTDataTextUI::DestroyGPT()
1268
1269// Wipe MBR data from the disk (zero it out completely)
1270// Returns 1 on success, 0 on failure.
1271int GPTData::DestroyMBR(void) {
1272 int allOK = 1, i;
1273 uint8_t blankSector[512];
1274
1275 for (i = 0; i < 512; i++)
1276 blankSector[i] = 0;
1277
1278 if (myDisk.OpenForWrite()) {
1279 if (myDisk.Seek(0)) {
1280 if (myDisk.Write(blankSector, 512) != 512)
1281 allOK = 0;
1282 } else allOK = 0;
1283 } else allOK = 0;
1284 if (!allOK)
1285 cerr << "Warning! MBR not overwritten! Error is " << errno << "!\n";
1286 return allOK;
1287} // GPTData::DestroyMBR(void)
1288
srs5694e4ac11e2009-08-31 10:13:04 -04001289// Tell user whether Apple Partition Map (APM) was discovered....
1290void GPTData::ShowAPMState(void) {
1291 if (apmFound)
srs5694fed16d02010-01-27 23:03:40 -05001292 cout << " APM: present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001293 else
srs5694fed16d02010-01-27 23:03:40 -05001294 cout << " APM: not present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001295} // GPTData::ShowAPMState()
1296
1297// Tell user about the state of the GPT data....
1298void GPTData::ShowGPTState(void) {
1299 switch (state) {
1300 case gpt_invalid:
srs5694fed16d02010-01-27 23:03:40 -05001301 cout << " GPT: not present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001302 break;
1303 case gpt_valid:
srs5694fed16d02010-01-27 23:03:40 -05001304 cout << " GPT: present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001305 break;
1306 case gpt_corrupt:
srs5694fed16d02010-01-27 23:03:40 -05001307 cout << " GPT: damaged\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001308 break;
1309 default:
srs5694fed16d02010-01-27 23:03:40 -05001310 cout << "\a GPT: unknown -- bug!\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001311 break;
1312 } // switch
1313} // GPTData::ShowGPTState()
1314
1315// Display the basic GPT data
1316void GPTData::DisplayGPTData(void) {
srs5694e321d442010-01-29 17:44:04 -05001317 uint32_t i;
srs5694e4ac11e2009-08-31 10:13:04 -04001318 uint64_t temp, totalFree;
1319
srs5694fed16d02010-01-27 23:03:40 -05001320 cout << "Disk " << device << ": " << diskSize << " sectors, "
srs56940873e9d2010-10-07 13:00:45 -04001321 << BytesToSI(diskSize, blockSize) << "\n";
srs5694fed16d02010-01-27 23:03:40 -05001322 cout << "Logical sector size: " << blockSize << " bytes\n";
srs56945a081752010-09-24 20:39:41 -04001323 cout << "Disk identifier (GUID): " << mainHeader.diskGUID << "\n";
srs56940283dae2010-04-28 16:44:34 -04001324 cout << "Partition table holds up to " << numParts << " entries\n";
srs5694fed16d02010-01-27 23:03:40 -05001325 cout << "First usable sector is " << mainHeader.firstUsableLBA
1326 << ", last usable sector is " << mainHeader.lastUsableLBA << "\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001327 totalFree = FindFreeBlocks(&i, &temp);
srs56948a4ddfc2010-03-21 19:05:49 -04001328 cout << "Partitions will be aligned on " << sectorAlignment << "-sector boundaries\n";
srs5694fed16d02010-01-27 23:03:40 -05001329 cout << "Total free space is " << totalFree << " sectors ("
srs56940873e9d2010-10-07 13:00:45 -04001330 << BytesToSI(totalFree, blockSize) << ")\n";
srs5694fed16d02010-01-27 23:03:40 -05001331 cout << "\nNumber Start (sector) End (sector) Size Code Name\n";
srs56940283dae2010-04-28 16:44:34 -04001332 for (i = 0; i < numParts; i++) {
srs5694978041c2009-09-21 20:51:47 -04001333 partitions[i].ShowSummary(i, blockSize);
srs5694e4ac11e2009-08-31 10:13:04 -04001334 } // for
1335} // GPTData::DisplayGPTData()
1336
srs5694e4ac11e2009-08-31 10:13:04 -04001337// Show detailed information on the specified partition
1338void GPTData::ShowPartDetails(uint32_t partNum) {
srs56940873e9d2010-10-07 13:00:45 -04001339 if (!IsFreePartNum(partNum)) {
srs5694e4ac11e2009-08-31 10:13:04 -04001340 partitions[partNum].ShowDetails(blockSize);
1341 } else {
srs5694fed16d02010-01-27 23:03:40 -05001342 cout << "Partition #" << partNum + 1 << " does not exist.";
srs5694e4ac11e2009-08-31 10:13:04 -04001343 } // if
1344} // GPTData::ShowPartDetails()
1345
srs5694e4ac11e2009-08-31 10:13:04 -04001346/**************************************************************************
1347 * *
1348 * Partition table transformation functions (MBR or BSD disklabel to GPT) *
1349 * (some of these functions may require user interaction) *
1350 * *
1351 **************************************************************************/
1352
srs569408bb0da2010-02-19 17:19:55 -05001353// Examines the MBR & GPT data to determine which set of data to use: the
1354// MBR (use_mbr), the GPT (use_gpt), the BSD disklabel (use_bsd), or create
1355// a new set of partitions (use_new). A return value of use_abort indicates
1356// that this function couldn't determine what to do. Overriding functions
1357// in derived classes may ask users questions in such cases.
srs5694e4ac11e2009-08-31 10:13:04 -04001358WhichToUse GPTData::UseWhichPartitions(void) {
1359 WhichToUse which = use_new;
1360 MBRValidity mbrState;
srs5694e4ac11e2009-08-31 10:13:04 -04001361
1362 mbrState = protectiveMBR.GetValidity();
1363
1364 if ((state == gpt_invalid) && ((mbrState == mbr) || (mbrState == hybrid))) {
srs5694fed16d02010-01-27 23:03:40 -05001365 cout << "\n***************************************************************\n"
1366 << "Found invalid GPT and valid MBR; converting MBR to GPT format.\n";
srs56945d58fe02010-01-03 20:57:08 -05001367 if (!justLooking) {
srs56940283dae2010-04-28 16:44:34 -04001368 cout << "\aTHIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by typing 'q' if\n"
srs5694fed16d02010-01-27 23:03:40 -05001369 << "you don't want to convert your MBR partitions to GPT format!\n";
srs56945d58fe02010-01-03 20:57:08 -05001370 } // if
srs5694fed16d02010-01-27 23:03:40 -05001371 cout << "***************************************************************\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001372 which = use_mbr;
1373 } // if
1374
1375 if ((state == gpt_invalid) && bsdFound) {
srs5694fed16d02010-01-27 23:03:40 -05001376 cout << "\n**********************************************************************\n"
1377 << "Found invalid GPT and valid BSD disklabel; converting BSD disklabel\n"
1378 << "to GPT format.";
srs56940a697312010-01-28 21:10:52 -05001379 if ((!justLooking) && (!beQuiet)) {
srs56940283dae2010-04-28 16:44:34 -04001380 cout << "\a THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Your first\n"
srs5694fed16d02010-01-27 23:03:40 -05001381 << "BSD partition will likely be unusable. Exit by typing 'q' if you don't\n"
1382 << "want to convert your BSD partitions to GPT format!";
srs56945d58fe02010-01-03 20:57:08 -05001383 } // if
srs5694fed16d02010-01-27 23:03:40 -05001384 cout << "\n**********************************************************************\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001385 which = use_bsd;
1386 } // if
1387
1388 if ((state == gpt_valid) && (mbrState == gpt)) {
srs5694e4ac11e2009-08-31 10:13:04 -04001389 which = use_gpt;
srs56943c0af382010-01-15 19:19:18 -05001390 if (!beQuiet)
srs5694fed16d02010-01-27 23:03:40 -05001391 cout << "Found valid GPT with protective MBR; using GPT.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001392 } // if
1393 if ((state == gpt_valid) && (mbrState == hybrid)) {
srs5694e4ac11e2009-08-31 10:13:04 -04001394 which = use_gpt;
srs56943c0af382010-01-15 19:19:18 -05001395 if (!beQuiet)
srs5694fed16d02010-01-27 23:03:40 -05001396 cout << "Found valid GPT with hybrid MBR; using GPT.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001397 } // if
1398 if ((state == gpt_valid) && (mbrState == invalid)) {
srs56940a697312010-01-28 21:10:52 -05001399 cout << "\aFound valid GPT with corrupt MBR; using GPT and will write new\n"
srs5694fed16d02010-01-27 23:03:40 -05001400 << "protective MBR on save.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001401 which = use_gpt;
srs5694e4ac11e2009-08-31 10:13:04 -04001402 } // if
1403 if ((state == gpt_valid) && (mbrState == mbr)) {
srs569408bb0da2010-02-19 17:19:55 -05001404 which = use_abort;
srs5694e4ac11e2009-08-31 10:13:04 -04001405 } // if
1406
srs5694e4ac11e2009-08-31 10:13:04 -04001407 if (state == gpt_corrupt) {
srs569408bb0da2010-02-19 17:19:55 -05001408 if (mbrState == gpt) {
1409 cout << "\a\a****************************************************************************\n"
1410 << "Caution: Found protective or hybrid MBR and corrupt GPT. Using GPT, but disk\n"
1411 << "verification and recovery are STRONGLY recommended.\n"
1412 << "****************************************************************************\n";
1413 which = use_gpt;
srs56943c0af382010-01-15 19:19:18 -05001414 } else {
srs569408bb0da2010-02-19 17:19:55 -05001415 which = use_abort;
1416 } // if/else MBR says disk is GPT
1417 } // if GPT corrupt
srs5694e4ac11e2009-08-31 10:13:04 -04001418
1419 if (which == use_new)
srs5694fed16d02010-01-27 23:03:40 -05001420 cout << "Creating new GPT entries.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001421
1422 return which;
1423} // UseWhichPartitions()
1424
srs569408bb0da2010-02-19 17:19:55 -05001425// Convert MBR partition table into GPT form.
1426void GPTData::XFormPartitions(void) {
srs5694e4ac11e2009-08-31 10:13:04 -04001427 int i, numToConvert;
1428 uint8_t origType;
srs5694e4ac11e2009-08-31 10:13:04 -04001429
1430 // Clear out old data & prepare basics....
1431 ClearGPTData();
1432
1433 // Convert the smaller of the # of GPT or MBR partitions
srs56940283dae2010-04-28 16:44:34 -04001434 if (numParts > MAX_MBR_PARTS)
srs5694978041c2009-09-21 20:51:47 -04001435 numToConvert = MAX_MBR_PARTS;
srs5694e4ac11e2009-08-31 10:13:04 -04001436 else
srs56940283dae2010-04-28 16:44:34 -04001437 numToConvert = numParts;
srs5694e4ac11e2009-08-31 10:13:04 -04001438
1439 for (i = 0; i < numToConvert; i++) {
1440 origType = protectiveMBR.GetType(i);
1441 // don't waste CPU time trying to convert extended, hybrid protective, or
1442 // null (non-existent) partitions
srs5694e35eb1b2009-09-14 00:29:34 -04001443 if ((origType != 0x05) && (origType != 0x0f) && (origType != 0x85) &&
srs56946699b012010-02-04 00:55:30 -05001444 (origType != 0x00) && (origType != 0xEE))
srs5694e4ac11e2009-08-31 10:13:04 -04001445 partitions[i] = protectiveMBR.AsGPT(i);
1446 } // for
1447
1448 // Convert MBR into protective MBR
1449 protectiveMBR.MakeProtectiveMBR();
1450
1451 // Record that all original CRCs were OK so as not to raise flags
1452 // when doing a disk verification
1453 mainCrcOk = secondCrcOk = mainPartsCrcOk = secondPartsCrcOk = 1;
srs5694e4ac11e2009-08-31 10:13:04 -04001454} // GPTData::XFormPartitions()
1455
1456// Transforms BSD disklabel on the specified partition (numbered from 0).
srs569408bb0da2010-02-19 17:19:55 -05001457// If an invalid partition number is given, the program does nothing.
srs5694e4ac11e2009-08-31 10:13:04 -04001458// Returns the number of new partitions created.
srs569408bb0da2010-02-19 17:19:55 -05001459int GPTData::XFormDisklabel(uint32_t partNum) {
1460 uint32_t low, high;
srs5694e4ac11e2009-08-31 10:13:04 -04001461 int goOn = 1, numDone = 0;
1462 BSDData disklabel;
1463
srs569408bb0da2010-02-19 17:19:55 -05001464 if (GetPartRange(&low, &high) == 0) {
1465 goOn = 0;
1466 cout << "No partitions!\n";
1467 } // if
1468 if (partNum > high) {
1469 goOn = 0;
1470 cout << "Specified partition is invalid!\n";
1471 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04001472
srs569408bb0da2010-02-19 17:19:55 -05001473 // If all is OK, read the disklabel and convert it.
1474 if (goOn) {
1475 goOn = disklabel.ReadBSDData(&myDisk, partitions[partNum].GetFirstLBA(),
1476 partitions[partNum].GetLastLBA());
1477 if ((goOn) && (disklabel.IsDisklabel())) {
1478 numDone = XFormDisklabel(&disklabel);
1479 if (numDone == 1)
1480 cout << "Converted 1 BSD partition.\n";
1481 else
1482 cout << "Converted " << numDone << " BSD partitions.\n";
1483 } else {
1484 cout << "Unable to convert partitions! Unrecognized BSD disklabel.\n";
1485 } // if/else
1486 } // if
1487 if (numDone > 0) { // converted partitions; delete carrier
1488 partitions[partNum].BlankPartition();
1489 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04001490 return numDone;
srs569455d92612010-03-07 22:16:07 -05001491} // GPTData::XFormDisklabel(uint32_t i)
srs5694e4ac11e2009-08-31 10:13:04 -04001492
1493// Transform the partitions on an already-loaded BSD disklabel...
srs569408bb0da2010-02-19 17:19:55 -05001494int GPTData::XFormDisklabel(BSDData* disklabel) {
1495 int i, partNum = 0, numDone = 0;
srs5694e4ac11e2009-08-31 10:13:04 -04001496
srs569408bb0da2010-02-19 17:19:55 -05001497 if (disklabel->IsDisklabel()) {
srs5694e4ac11e2009-08-31 10:13:04 -04001498 for (i = 0; i < disklabel->GetNumParts(); i++) {
srs569408bb0da2010-02-19 17:19:55 -05001499 partNum = FindFirstFreePart();
1500 if (partNum >= 0) {
1501 partitions[partNum] = disklabel->AsGPT(i);
1502 if (partitions[partNum].IsUsed())
1503 numDone++;
1504 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04001505 } // for
srs569408bb0da2010-02-19 17:19:55 -05001506 if (partNum == -1)
1507 cerr << "Warning! Too many partitions to convert!\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001508 } // if
1509
1510 // Record that all original CRCs were OK so as not to raise flags
1511 // when doing a disk verification
1512 mainCrcOk = secondCrcOk = mainPartsCrcOk = secondPartsCrcOk = 1;
1513
1514 return numDone;
1515} // GPTData::XFormDisklabel(BSDData* disklabel)
1516
srs569408bb0da2010-02-19 17:19:55 -05001517// Add one GPT partition to MBR. Used by PartsToMBR() functions. Created
1518// partition has the active/bootable flag UNset and uses the GPT fdisk
1519// type code divided by 0x0100 as the MBR type code.
1520// Returns 1 if operation was 100% successful, 0 if there were ANY
1521// problems.
srs5694978041c2009-09-21 20:51:47 -04001522int GPTData::OnePartToMBR(uint32_t gptPart, int mbrPart) {
srs569408bb0da2010-02-19 17:19:55 -05001523 int allOK = 1;
srs5694fed16d02010-01-27 23:03:40 -05001524
srs5694978041c2009-09-21 20:51:47 -04001525 if ((mbrPart < 0) || (mbrPart > 3)) {
srs5694fed16d02010-01-27 23:03:40 -05001526 cout << "MBR partition " << mbrPart + 1 << " is out of range; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001527 allOK = 0;
1528 } // if
srs56940283dae2010-04-28 16:44:34 -04001529 if (gptPart >= numParts) {
srs5694fed16d02010-01-27 23:03:40 -05001530 cout << "GPT partition " << gptPart + 1 << " is out of range; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001531 allOK = 0;
1532 } // if
1533 if (allOK && (partitions[gptPart].GetLastLBA() == UINT64_C(0))) {
srs5694fed16d02010-01-27 23:03:40 -05001534 cout << "GPT partition " << gptPart + 1 << " is undefined; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001535 allOK = 0;
1536 } // if
1537 if (allOK && (partitions[gptPart].GetFirstLBA() <= UINT32_MAX) &&
1538 (partitions[gptPart].GetLengthLBA() <= UINT32_MAX)) {
1539 if (partitions[gptPart].GetLastLBA() > UINT32_MAX) {
srs5694fed16d02010-01-27 23:03:40 -05001540 cout << "Caution: Partition end point past 32-bit pointer boundary;"
1541 << " some OSes may\nreact strangely.\n";
srs569408bb0da2010-02-19 17:19:55 -05001542 } // if
srs5694978041c2009-09-21 20:51:47 -04001543 protectiveMBR.MakePart(mbrPart, (uint32_t) partitions[gptPart].GetFirstLBA(),
srs569408bb0da2010-02-19 17:19:55 -05001544 (uint32_t) partitions[gptPart].GetLengthLBA(),
1545 partitions[gptPart].GetHexType() / 256, 0);
srs5694978041c2009-09-21 20:51:47 -04001546 } else { // partition out of range
srs569408bb0da2010-02-19 17:19:55 -05001547 if (allOK) // Display only if "else" triggered by out-of-bounds condition
1548 cout << "Partition " << gptPart + 1 << " begins beyond the 32-bit pointer limit of MBR "
1549 << "partitions, or is\n too big; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001550 allOK = 0;
1551 } // if/else
1552 return allOK;
1553} // GPTData::OnePartToMBR()
1554
srs5694e4ac11e2009-08-31 10:13:04 -04001555
1556/**********************************************************************
1557 * *
1558 * Functions that adjust GPT data structures WITHOUT user interaction *
1559 * (they may display information for the user's benefit, though) *
1560 * *
1561 **********************************************************************/
1562
1563// Resizes GPT to specified number of entries. Creates a new table if
srs5694ba00fed2010-01-12 18:18:36 -05001564// necessary, copies data if it already exists. Returns 1 if all goes
1565// well, 0 if an error is encountered.
srs5694e4ac11e2009-08-31 10:13:04 -04001566int GPTData::SetGPTSize(uint32_t numEntries) {
srs569408bb0da2010-02-19 17:19:55 -05001567 GPTPart* newParts;
1568 GPTPart* trash;
srs5694e4ac11e2009-08-31 10:13:04 -04001569 uint32_t i, high, copyNum;
1570 int allOK = 1;
1571
1572 // First, adjust numEntries upward, if necessary, to get a number
1573 // that fills the allocated sectors
1574 i = blockSize / GPT_SIZE;
1575 if ((numEntries % i) != 0) {
srs5694fed16d02010-01-27 23:03:40 -05001576 cout << "Adjusting GPT size from " << numEntries << " to ";
srs5694e4ac11e2009-08-31 10:13:04 -04001577 numEntries = ((numEntries / i) + 1) * i;
srs5694fed16d02010-01-27 23:03:40 -05001578 cout << numEntries << " to fill the sector\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001579 } // if
1580
srs5694247657a2009-11-26 18:36:12 -05001581 // Do the work only if the # of partitions is changing. Along with being
srs569455d92612010-03-07 22:16:07 -05001582 // efficient, this prevents mucking with the location of the secondary
srs5694247657a2009-11-26 18:36:12 -05001583 // partition table, which causes problems when loading data from a RAID
1584 // array that's been expanded because this function is called when loading
1585 // data.
srs56940283dae2010-04-28 16:44:34 -04001586 if (((numEntries != numParts) || (partitions == NULL)) && (numEntries > 0)) {
srs5694cb76c672010-02-11 22:22:22 -05001587 newParts = new GPTPart [numEntries * sizeof (GPTPart)];
srs5694247657a2009-11-26 18:36:12 -05001588 if (newParts != NULL) {
1589 if (partitions != NULL) { // existing partitions; copy them over
1590 GetPartRange(&i, &high);
1591 if (numEntries < (high + 1)) { // Highest entry too high for new #
srs5694fed16d02010-01-27 23:03:40 -05001592 cout << "The highest-numbered partition is " << high + 1
1593 << ", which is greater than the requested\n"
1594 << "partition table size of " << numEntries
1595 << "; cannot resize. Perhaps sorting will help.\n";
srs5694247657a2009-11-26 18:36:12 -05001596 allOK = 0;
1597 } else { // go ahead with copy
srs56940283dae2010-04-28 16:44:34 -04001598 if (numEntries < numParts)
srs5694247657a2009-11-26 18:36:12 -05001599 copyNum = numEntries;
1600 else
srs56940283dae2010-04-28 16:44:34 -04001601 copyNum = numParts;
srs5694247657a2009-11-26 18:36:12 -05001602 for (i = 0; i < copyNum; i++) {
1603 newParts[i] = partitions[i];
1604 } // for
1605 trash = partitions;
1606 partitions = newParts;
srs5694cb76c672010-02-11 22:22:22 -05001607 delete[] trash;
srs5694247657a2009-11-26 18:36:12 -05001608 } // if
1609 } else { // No existing partition table; just create it
srs5694e4ac11e2009-08-31 10:13:04 -04001610 partitions = newParts;
srs5694247657a2009-11-26 18:36:12 -05001611 } // if/else existing partitions
srs56940283dae2010-04-28 16:44:34 -04001612 numParts = numEntries;
srs5694247657a2009-11-26 18:36:12 -05001613 mainHeader.firstUsableLBA = ((numEntries * GPT_SIZE) / blockSize) + 2 ;
1614 secondHeader.firstUsableLBA = mainHeader.firstUsableLBA;
1615 MoveSecondHeaderToEnd();
1616 if (diskSize > 0)
1617 CheckGPTSize();
1618 } else { // Bad memory allocation
srs5694fed16d02010-01-27 23:03:40 -05001619 cerr << "Error allocating memory for partition table!\n";
srs5694247657a2009-11-26 18:36:12 -05001620 allOK = 0;
1621 } // if/else
srs5694e4ac11e2009-08-31 10:13:04 -04001622 } // if/else
srs56940283dae2010-04-28 16:44:34 -04001623 mainHeader.numParts = numParts;
1624 secondHeader.numParts = numParts;
srs5694e4ac11e2009-08-31 10:13:04 -04001625 return (allOK);
1626} // GPTData::SetGPTSize()
1627
1628// Blank the partition array
1629void GPTData::BlankPartitions(void) {
1630 uint32_t i;
1631
srs56940283dae2010-04-28 16:44:34 -04001632 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04001633 partitions[i].BlankPartition();
1634 } // for
1635} // GPTData::BlankPartitions()
1636
srs5694ba00fed2010-01-12 18:18:36 -05001637// Delete a partition by number. Returns 1 if successful,
1638// 0 if there was a problem. Returns 1 if partition was in
1639// range, 0 if it was out of range.
1640int GPTData::DeletePartition(uint32_t partNum) {
1641 uint64_t startSector, length;
srs56940283dae2010-04-28 16:44:34 -04001642 uint32_t low, high, numUsedParts, retval = 1;;
srs5694ba00fed2010-01-12 18:18:36 -05001643
srs56940283dae2010-04-28 16:44:34 -04001644 numUsedParts = GetPartRange(&low, &high);
1645 if ((numUsedParts > 0) && (partNum >= low) && (partNum <= high)) {
srs5694ba00fed2010-01-12 18:18:36 -05001646 // In case there's a protective MBR, look for & delete matching
1647 // MBR partition....
1648 startSector = partitions[partNum].GetFirstLBA();
1649 length = partitions[partNum].GetLengthLBA();
1650 protectiveMBR.DeleteByLocation(startSector, length);
1651
1652 // Now delete the GPT partition
1653 partitions[partNum].BlankPartition();
1654 } else {
srs5694fed16d02010-01-27 23:03:40 -05001655 cerr << "Partition number " << partNum + 1 << " out of range!\n";
srs5694ba00fed2010-01-12 18:18:36 -05001656 retval = 0;
1657 } // if/else
1658 return retval;
1659} // GPTData::DeletePartition(uint32_t partNum)
1660
srs569408bb0da2010-02-19 17:19:55 -05001661// Non-interactively create a partition.
1662// Returns 1 if the operation was successful, 0 if a problem was discovered.
srs5694e321d442010-01-29 17:44:04 -05001663uint32_t GPTData::CreatePartition(uint32_t partNum, uint64_t startSector, uint64_t endSector) {
srs5694ba00fed2010-01-12 18:18:36 -05001664 int retval = 1; // assume there'll be no problems
srs56945a081752010-09-24 20:39:41 -04001665 uint64_t origSector = startSector;
srs5694ba00fed2010-01-12 18:18:36 -05001666
1667 if (IsFreePartNum(partNum)) {
srs56945a081752010-09-24 20:39:41 -04001668 if (Align(&startSector)) {
1669 cout << "Information: Moved requested sector from " << origSector << " to "
1670 << startSector << " in\norder to align on " << sectorAlignment
1671 << "-sector boundaries.\n";
1672 } // if
srs5694ba00fed2010-01-12 18:18:36 -05001673 if (IsFree(startSector) && (startSector <= endSector)) {
1674 if (FindLastInFree(startSector) >= endSector) {
1675 partitions[partNum].SetFirstLBA(startSector);
1676 partitions[partNum].SetLastLBA(endSector);
1677 partitions[partNum].SetType(0x0700);
srs56946699b012010-02-04 00:55:30 -05001678 partitions[partNum].RandomizeUniqueGUID();
srs5694ba00fed2010-01-12 18:18:36 -05001679 } else retval = 0; // if free space until endSector
1680 } else retval = 0; // if startSector is free
1681 } else retval = 0; // if legal partition number
1682 return retval;
1683} // GPTData::CreatePartition(partNum, startSector, endSector)
1684
srs5694e4ac11e2009-08-31 10:13:04 -04001685// Sort the GPT entries, eliminating gaps and making for a logical
1686// ordering. Relies on QuickSortGPT() for the bulk of the work
1687void GPTData::SortGPT(void) {
srs5694546a9c72010-01-26 16:00:26 -05001688 uint32_t i, numFound, firstPart, lastPart;
srs5694e4ac11e2009-08-31 10:13:04 -04001689
1690 // First, find the last partition with data, so as not to
1691 // spend needless time sorting empty entries....
srs5694546a9c72010-01-26 16:00:26 -05001692 numFound = GetPartRange(&firstPart, &lastPart);
srs5694e4ac11e2009-08-31 10:13:04 -04001693
1694 // Now swap empties with the last partitions, to simplify the logic
1695 // in the Quicksort function....
1696 i = 0;
1697 while (i < lastPart) {
1698 if (partitions[i].GetFirstLBA() == 0) {
srs569408bb0da2010-02-19 17:19:55 -05001699 SwapPartitions(i, lastPart);
srs5694546a9c72010-01-26 16:00:26 -05001700 do {
1701 lastPart--;
1702 } while ((lastPart > 0) && (partitions[lastPart].GetFirstLBA() == 0));
srs5694e4ac11e2009-08-31 10:13:04 -04001703 } // if
1704 i++;
1705 } // while
1706
srs5694546a9c72010-01-26 16:00:26 -05001707 // If there are more empties than partitions in the range from 0 to lastPart,
1708 // the above leaves lastPart set too high, so we've got to adjust it to
1709 // prevent empties from migrating to the top of the list....
1710 GetPartRange(&firstPart, &lastPart);
1711
srs5694e4ac11e2009-08-31 10:13:04 -04001712 // Now call the recursive quick sort routine to do the real work....
srs569408bb0da2010-02-19 17:19:55 -05001713 QuickSortGPT(0, lastPart);
srs5694e4ac11e2009-08-31 10:13:04 -04001714} // GPTData::SortGPT()
1715
srs569408bb0da2010-02-19 17:19:55 -05001716// Recursive quick sort algorithm for GPT partitions. Note that if there
1717// are any empties in the specified range, they'll be sorted to the
1718// start, resulting in a sorted set of partitions that begins with
1719// partition 2, 3, or higher.
1720void GPTData::QuickSortGPT(int start, int finish) {
1721 uint64_t starterValue; // starting location of median partition
1722 int left, right;
1723
1724 left = start;
1725 right = finish;
1726 starterValue = partitions[(start + finish) / 2].GetFirstLBA();
1727 do {
1728 while (partitions[left].GetFirstLBA() < starterValue)
1729 left++;
1730 while (partitions[right].GetFirstLBA() > starterValue)
1731 right--;
1732 if (left <= right)
1733 SwapPartitions(left++, right--);
1734 } while (left <= right);
1735 if (start < right) QuickSortGPT(start, right);
1736 if (finish > left) QuickSortGPT(left, finish);
1737} // GPTData::QuickSortGPT()
1738
1739// Swap the contents of two partitions.
1740// Returns 1 if successful, 0 if either partition is out of range
1741// (that is, not a legal number; either or both can be empty).
1742// Note that if partNum1 = partNum2 and this number is in range,
1743// it will be considered successful.
1744int GPTData::SwapPartitions(uint32_t partNum1, uint32_t partNum2) {
1745 GPTPart temp;
1746 int allOK = 1;
1747
srs56940283dae2010-04-28 16:44:34 -04001748 if ((partNum1 < numParts) && (partNum2 < numParts)) {
srs569408bb0da2010-02-19 17:19:55 -05001749 if (partNum1 != partNum2) {
1750 temp = partitions[partNum1];
1751 partitions[partNum1] = partitions[partNum2];
1752 partitions[partNum2] = temp;
1753 } // if
1754 } else allOK = 0; // partition numbers are valid
1755 return allOK;
1756} // GPTData::SwapPartitions()
1757
srs5694e4ac11e2009-08-31 10:13:04 -04001758// Set up data structures for entirely new set of partitions on the
1759// specified device. Returns 1 if OK, 0 if there were problems.
srs5694e35eb1b2009-09-14 00:29:34 -04001760// Note that this function does NOT clear the protectiveMBR data
1761// structure, since it may hold the original MBR partitions if the
1762// program was launched on an MBR disk, and those may need to be
1763// converted to GPT format.
srs5694e4ac11e2009-08-31 10:13:04 -04001764int GPTData::ClearGPTData(void) {
srs5694e35eb1b2009-09-14 00:29:34 -04001765 int goOn = 1, i;
srs5694e4ac11e2009-08-31 10:13:04 -04001766
1767 // Set up the partition table....
srs5694fed16d02010-01-27 23:03:40 -05001768 if (partitions != NULL)
srs5694cb76c672010-02-11 22:22:22 -05001769 delete[] partitions;
srs5694e4ac11e2009-08-31 10:13:04 -04001770 partitions = NULL;
1771 SetGPTSize(NUM_GPT_ENTRIES);
1772
1773 // Now initialize a bunch of stuff that's static....
1774 mainHeader.signature = GPT_SIGNATURE;
1775 mainHeader.revision = 0x00010000;
srs5694978041c2009-09-21 20:51:47 -04001776 mainHeader.headerSize = HEADER_SIZE;
srs5694e4ac11e2009-08-31 10:13:04 -04001777 mainHeader.reserved = 0;
1778 mainHeader.currentLBA = UINT64_C(1);
1779 mainHeader.partitionEntriesLBA = (uint64_t) 2;
1780 mainHeader.sizeOfPartitionEntries = GPT_SIZE;
1781 for (i = 0; i < GPT_RESERVED; i++) {
1782 mainHeader.reserved2[i] = '\0';
1783 } // for
srs56940873e9d2010-10-07 13:00:45 -04001784 if (blockSize > 0)
1785 sectorAlignment = DEFAULT_ALIGNMENT * SECTOR_SIZE / blockSize;
1786 else
1787 sectorAlignment = DEFAULT_ALIGNMENT;
srs5694e4ac11e2009-08-31 10:13:04 -04001788
1789 // Now some semi-static items (computed based on end of disk)
1790 mainHeader.backupLBA = diskSize - UINT64_C(1);
1791 mainHeader.lastUsableLBA = diskSize - mainHeader.firstUsableLBA;
1792
1793 // Set a unique GUID for the disk, based on random numbers
srs56946699b012010-02-04 00:55:30 -05001794 mainHeader.diskGUID.Randomize();
srs5694e4ac11e2009-08-31 10:13:04 -04001795
1796 // Copy main header to backup header
1797 RebuildSecondHeader();
1798
1799 // Blank out the partitions array....
1800 BlankPartitions();
1801
1802 // Flag all CRCs as being OK....
1803 mainCrcOk = 1;
1804 secondCrcOk = 1;
1805 mainPartsCrcOk = 1;
1806 secondPartsCrcOk = 1;
1807
1808 return (goOn);
1809} // GPTData::ClearGPTData()
1810
srs5694247657a2009-11-26 18:36:12 -05001811// Set the location of the second GPT header data to the end of the disk.
srs569464cbd172011-03-01 22:03:54 -05001812// If the disk size has actually changed, this also adjusts the protective
1813// entry in the MBR, since it's probably no longer correct.
srs5694247657a2009-11-26 18:36:12 -05001814// Used internally and called by the 'e' option on the recovery &
1815// transformation menu, to help users of RAID arrays who add disk space
srs569464cbd172011-03-01 22:03:54 -05001816// to their arrays or to adjust data structures in restore operations
1817// involving unequal-sized disks.
srs5694247657a2009-11-26 18:36:12 -05001818void GPTData::MoveSecondHeaderToEnd() {
srs56948bb78762009-11-24 15:43:49 -05001819 mainHeader.backupLBA = secondHeader.currentLBA = diskSize - UINT64_C(1);
srs569464cbd172011-03-01 22:03:54 -05001820 if (mainHeader.lastUsableLBA != diskSize - mainHeader.firstUsableLBA) {
1821 if (protectiveMBR.GetValidity() == hybrid) {
1822 protectiveMBR.OptimizeEESize();
1823 RecomputeCHS();
1824 } // if
1825 if (protectiveMBR.GetValidity() == gpt)
1826 MakeProtectiveMBR();
1827 } // if
srs56948bb78762009-11-24 15:43:49 -05001828 mainHeader.lastUsableLBA = secondHeader.lastUsableLBA = diskSize - mainHeader.firstUsableLBA;
1829 secondHeader.partitionEntriesLBA = secondHeader.lastUsableLBA + UINT64_C(1);
1830} // GPTData::FixSecondHeaderLocation()
1831
srs56940a697312010-01-28 21:10:52 -05001832int GPTData::SetName(uint32_t partNum, const string & theName) {
srs5694ba00fed2010-01-12 18:18:36 -05001833 int retval = 1;
srs5694fed16d02010-01-27 23:03:40 -05001834
1835 if (!IsFreePartNum(partNum)) {
1836 partitions[partNum].SetName(theName);
1837 } else retval = 0;
srs5694ba00fed2010-01-12 18:18:36 -05001838
1839 return retval;
srs5694e4ac11e2009-08-31 10:13:04 -04001840} // GPTData::SetName
1841
1842// Set the disk GUID to the specified value. Note that the header CRCs must
1843// be recomputed after calling this function.
1844void GPTData::SetDiskGUID(GUIDData newGUID) {
1845 mainHeader.diskGUID = newGUID;
1846 secondHeader.diskGUID = newGUID;
1847} // SetDiskGUID()
1848
1849// Set the unique GUID of the specified partition. Returns 1 on
1850// successful completion, 0 if there were problems (invalid
1851// partition number).
1852int GPTData::SetPartitionGUID(uint32_t pn, GUIDData theGUID) {
1853 int retval = 0;
1854
srs56940283dae2010-04-28 16:44:34 -04001855 if (pn < numParts) {
srs5694e4ac11e2009-08-31 10:13:04 -04001856 if (partitions[pn].GetFirstLBA() != UINT64_C(0)) {
1857 partitions[pn].SetUniqueGUID(theGUID);
1858 retval = 1;
1859 } // if
1860 } // if
1861 return retval;
1862} // GPTData::SetPartitionGUID()
1863
srs56949ba54212010-05-18 23:24:02 -04001864// Set new random GUIDs for the disk and all partitions. Intended to be used
1865// after disk cloning or similar operations that don't randomize the GUIDs.
1866void GPTData::RandomizeGUIDs(void) {
1867 uint32_t i;
1868
1869 mainHeader.diskGUID.Randomize();
1870 secondHeader.diskGUID = mainHeader.diskGUID;
1871 for (i = 0; i < numParts; i++)
1872 if (partitions[i].IsUsed())
1873 partitions[i].RandomizeUniqueGUID();
1874} // GPTData::RandomizeGUIDs()
1875
srs5694ba00fed2010-01-12 18:18:36 -05001876// Change partition type code non-interactively. Returns 1 if
1877// successful, 0 if not....
srs5694327129e2010-09-22 01:07:31 -04001878int GPTData::ChangePartType(uint32_t partNum, PartType theGUID) {
1879 int retval = 1;
1880
1881 if (!IsFreePartNum(partNum)) {
1882 partitions[partNum].SetType(theGUID);
1883 } else retval = 0;
1884 return retval;
1885} // GPTData::ChangePartType()
1886
srs56949ba54212010-05-18 23:24:02 -04001887// Recompute the CHS values of all the MBR partitions. Used to reset
1888// CHS values that some BIOSes require, despite the fact that the
1889// resulting CHS values violate the GPT standard.
1890void GPTData::RecomputeCHS(void) {
1891 int i;
1892
1893 for (i = 0; i < 4; i++)
1894 protectiveMBR.RecomputeCHS(i);
1895} // GPTData::RecomputeCHS()
1896
srs56941d1448a2009-12-31 21:20:19 -05001897// Adjust sector number so that it falls on a sector boundary that's a
1898// multiple of sectorAlignment. This is done to improve the performance
1899// of Western Digital Advanced Format disks and disks with similar
1900// technology from other companies, which use 4096-byte sectors
1901// internally although they translate to 512-byte sectors for the
1902// benefit of the OS. If partitions aren't properly aligned on these
1903// disks, some filesystem data structures can span multiple physical
1904// sectors, degrading performance. This function should be called
1905// only on the FIRST sector of the partition, not the last!
1906// This function returns 1 if the alignment was altered, 0 if it
1907// was unchanged.
1908int GPTData::Align(uint64_t* sector) {
1909 int retval = 0, sectorOK = 0;
1910 uint64_t earlier, later, testSector, original;
1911
1912 if ((*sector % sectorAlignment) != 0) {
1913 original = *sector;
srs56941d1448a2009-12-31 21:20:19 -05001914 earlier = (*sector / sectorAlignment) * sectorAlignment;
1915 later = earlier + (uint64_t) sectorAlignment;
1916
1917 // Check to see that every sector between the earlier one and the
1918 // requested one is clear, and that it's not too early....
1919 if (earlier >= mainHeader.firstUsableLBA) {
srs56941d1448a2009-12-31 21:20:19 -05001920 sectorOK = 1;
1921 testSector = earlier;
1922 do {
1923 sectorOK = IsFree(testSector++);
1924 } while ((sectorOK == 1) && (testSector < *sector));
1925 if (sectorOK == 1) {
1926 *sector = earlier;
srs56945a081752010-09-24 20:39:41 -04001927 retval = 1;
srs56941d1448a2009-12-31 21:20:19 -05001928 } // if
1929 } // if firstUsableLBA check
1930
1931 // If couldn't move the sector earlier, try to move it later instead....
1932 if ((sectorOK != 1) && (later <= mainHeader.lastUsableLBA)) {
1933 sectorOK = 1;
1934 testSector = later;
1935 do {
1936 sectorOK = IsFree(testSector--);
1937 } while ((sectorOK == 1) && (testSector > *sector));
1938 if (sectorOK == 1) {
1939 *sector = later;
srs56945a081752010-09-24 20:39:41 -04001940 retval = 1;
srs56941d1448a2009-12-31 21:20:19 -05001941 } // if
1942 } // if
srs56941d1448a2009-12-31 21:20:19 -05001943 } // if
1944 return retval;
1945} // GPTData::Align()
1946
srs5694e4ac11e2009-08-31 10:13:04 -04001947/********************************************************
1948 * *
1949 * Functions that return data about GPT data structures *
1950 * (most of these are inline in gpt.h) *
1951 * *
1952 ********************************************************/
1953
1954// Find the low and high used partition numbers (numbered from 0).
1955// Return value is the number of partitions found. Note that the
1956// *low and *high values are both set to 0 when no partitions
1957// are found, as well as when a single partition in the first
1958// position exists. Thus, the return value is the only way to
1959// tell when no partitions exist.
1960int GPTData::GetPartRange(uint32_t *low, uint32_t *high) {
1961 uint32_t i;
1962 int numFound = 0;
1963
srs56940283dae2010-04-28 16:44:34 -04001964 *low = numParts + 1; // code for "not found"
srs5694e4ac11e2009-08-31 10:13:04 -04001965 *high = 0;
srs56940283dae2010-04-28 16:44:34 -04001966 if (numParts > 0) { // only try if partition table exists...
1967 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04001968 if (partitions[i].GetFirstLBA() != UINT64_C(0)) { // it exists
1969 *high = i; // since we're counting up, set the high value
srs569408bb0da2010-02-19 17:19:55 -05001970 // Set the low value only if it's not yet found...
srs56940283dae2010-04-28 16:44:34 -04001971 if (*low == (numParts + 1)) *low = i;
srs569408bb0da2010-02-19 17:19:55 -05001972 numFound++;
srs5694e4ac11e2009-08-31 10:13:04 -04001973 } // if
1974 } // for
1975 } // if
1976
1977 // Above will leave *low pointing to its "not found" value if no partitions
1978 // are defined, so reset to 0 if this is the case....
srs56940283dae2010-04-28 16:44:34 -04001979 if (*low == (numParts + 1))
srs5694e4ac11e2009-08-31 10:13:04 -04001980 *low = 0;
1981 return numFound;
1982} // GPTData::GetPartRange()
1983
srs569408bb0da2010-02-19 17:19:55 -05001984// Returns the value of the first free partition, or -1 if none is
1985// unused.
1986int GPTData::FindFirstFreePart(void) {
1987 int i = 0;
1988
1989 if (partitions != NULL) {
srs56940283dae2010-04-28 16:44:34 -04001990 while ((partitions[i].IsUsed()) && (i < (int) numParts))
srs569408bb0da2010-02-19 17:19:55 -05001991 i++;
srs56940283dae2010-04-28 16:44:34 -04001992 if (i >= (int) numParts)
srs569408bb0da2010-02-19 17:19:55 -05001993 i = -1;
1994 } else i = -1;
1995 return i;
1996} // GPTData::FindFirstFreePart()
1997
srs5694978041c2009-09-21 20:51:47 -04001998// Returns the number of defined partitions.
1999uint32_t GPTData::CountParts(void) {
srs5694e321d442010-01-29 17:44:04 -05002000 uint32_t i, counted = 0;
srs5694978041c2009-09-21 20:51:47 -04002001
srs56940283dae2010-04-28 16:44:34 -04002002 for (i = 0; i < numParts; i++) {
srs569408bb0da2010-02-19 17:19:55 -05002003 if (partitions[i].IsUsed())
srs5694978041c2009-09-21 20:51:47 -04002004 counted++;
2005 } // for
2006 return counted;
2007} // GPTData::CountParts()
2008
srs5694e4ac11e2009-08-31 10:13:04 -04002009/****************************************************
2010 * *
2011 * Functions that return data about disk free space *
2012 * *
2013 ****************************************************/
2014
2015// Find the first available block after the starting point; returns 0 if
2016// there are no available blocks left
2017uint64_t GPTData::FindFirstAvailable(uint64_t start) {
2018 uint64_t first;
2019 uint32_t i;
2020 int firstMoved = 0;
2021
2022 // Begin from the specified starting point or from the first usable
2023 // LBA, whichever is greater...
2024 if (start < mainHeader.firstUsableLBA)
2025 first = mainHeader.firstUsableLBA;
2026 else
2027 first = start;
2028
2029 // ...now search through all partitions; if first is within an
2030 // existing partition, move it to the next sector after that
2031 // partition and repeat. If first was moved, set firstMoved
2032 // flag; repeat until firstMoved is not set, so as to catch
2033 // cases where partitions are out of sequential order....
2034 do {
2035 firstMoved = 0;
srs56940283dae2010-04-28 16:44:34 -04002036 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04002037 if ((first >= partitions[i].GetFirstLBA()) &&
srs569455d92612010-03-07 22:16:07 -05002038 (first <= partitions[i].GetLastLBA())) { // in existing part.
srs5694e4ac11e2009-08-31 10:13:04 -04002039 first = partitions[i].GetLastLBA() + 1;
2040 firstMoved = 1;
srs569455d92612010-03-07 22:16:07 -05002041 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002042 } // for
2043 } while (firstMoved == 1);
2044 if (first > mainHeader.lastUsableLBA)
2045 first = 0;
2046 return (first);
2047} // GPTData::FindFirstAvailable()
2048
2049// Finds the first available sector in the largest block of unallocated
2050// space on the disk. Returns 0 if there are no available blocks left
2051uint64_t GPTData::FindFirstInLargest(void) {
srs5694e35eb1b2009-09-14 00:29:34 -04002052 uint64_t start, firstBlock, lastBlock, segmentSize, selectedSize = 0, selectedSegment = 0;
srs5694e4ac11e2009-08-31 10:13:04 -04002053
2054 start = 0;
2055 do {
2056 firstBlock = FindFirstAvailable(start);
2057 if (firstBlock != UINT32_C(0)) { // something's free...
2058 lastBlock = FindLastInFree(firstBlock);
2059 segmentSize = lastBlock - firstBlock + UINT32_C(1);
2060 if (segmentSize > selectedSize) {
2061 selectedSize = segmentSize;
2062 selectedSegment = firstBlock;
2063 } // if
2064 start = lastBlock + 1;
2065 } // if
2066 } while (firstBlock != 0);
2067 return selectedSegment;
2068} // GPTData::FindFirstInLargest()
2069
srs5694cb76c672010-02-11 22:22:22 -05002070// Find the last available block on the disk.
2071// Returns 0 if there are no available partitions
2072uint64_t GPTData::FindLastAvailable(void) {
srs5694e4ac11e2009-08-31 10:13:04 -04002073 uint64_t last;
2074 uint32_t i;
2075 int lastMoved = 0;
2076
2077 // Start by assuming the last usable LBA is available....
2078 last = mainHeader.lastUsableLBA;
2079
2080 // ...now, similar to algorithm in FindFirstAvailable(), search
2081 // through all partitions, moving last when it's in an existing
2082 // partition. Set the lastMoved flag so we repeat to catch cases
2083 // where partitions are out of logical order.
2084 do {
2085 lastMoved = 0;
srs56940283dae2010-04-28 16:44:34 -04002086 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04002087 if ((last >= partitions[i].GetFirstLBA()) &&
srs569455d92612010-03-07 22:16:07 -05002088 (last <= partitions[i].GetLastLBA())) { // in existing part.
srs5694e4ac11e2009-08-31 10:13:04 -04002089 last = partitions[i].GetFirstLBA() - 1;
2090 lastMoved = 1;
srs569455d92612010-03-07 22:16:07 -05002091 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002092 } // for
2093 } while (lastMoved == 1);
2094 if (last < mainHeader.firstUsableLBA)
2095 last = 0;
2096 return (last);
2097} // GPTData::FindLastAvailable()
2098
2099// Find the last available block in the free space pointed to by start.
2100uint64_t GPTData::FindLastInFree(uint64_t start) {
2101 uint64_t nearestStart;
2102 uint32_t i;
2103
2104 nearestStart = mainHeader.lastUsableLBA;
srs56940283dae2010-04-28 16:44:34 -04002105 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04002106 if ((nearestStart > partitions[i].GetFirstLBA()) &&
srs569455d92612010-03-07 22:16:07 -05002107 (partitions[i].GetFirstLBA() > start)) {
srs5694e4ac11e2009-08-31 10:13:04 -04002108 nearestStart = partitions[i].GetFirstLBA() - 1;
srs569455d92612010-03-07 22:16:07 -05002109 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002110 } // for
2111 return (nearestStart);
2112} // GPTData::FindLastInFree()
2113
2114// Finds the total number of free blocks, the number of segments in which
2115// they reside, and the size of the largest of those segments
srs5694e321d442010-01-29 17:44:04 -05002116uint64_t GPTData::FindFreeBlocks(uint32_t *numSegments, uint64_t *largestSegment) {
srs5694e4ac11e2009-08-31 10:13:04 -04002117 uint64_t start = UINT64_C(0); // starting point for each search
2118 uint64_t totalFound = UINT64_C(0); // running total
2119 uint64_t firstBlock; // first block in a segment
2120 uint64_t lastBlock; // last block in a segment
2121 uint64_t segmentSize; // size of segment in blocks
srs5694e321d442010-01-29 17:44:04 -05002122 uint32_t num = 0;
srs5694e4ac11e2009-08-31 10:13:04 -04002123
2124 *largestSegment = UINT64_C(0);
srs5694c54e9b42010-05-01 21:04:23 -04002125 if (diskSize > 0) {
2126 do {
2127 firstBlock = FindFirstAvailable(start);
2128 if (firstBlock != UINT64_C(0)) { // something's free...
2129 lastBlock = FindLastInFree(firstBlock);
2130 segmentSize = lastBlock - firstBlock + UINT64_C(1);
2131 if (segmentSize > *largestSegment) {
2132 *largestSegment = segmentSize;
2133 } // if
2134 totalFound += segmentSize;
2135 num++;
2136 start = lastBlock + 1;
srs5694e4ac11e2009-08-31 10:13:04 -04002137 } // if
srs5694c54e9b42010-05-01 21:04:23 -04002138 } while (firstBlock != 0);
2139 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002140 *numSegments = num;
2141 return totalFound;
2142} // GPTData::FindFreeBlocks()
2143
srs569455d92612010-03-07 22:16:07 -05002144// Returns 1 if sector is unallocated, 0 if it's allocated to a partition.
2145// If it's allocated, return the partition number to which it's allocated
2146// in partNum, if that variable is non-NULL. (A value of UINT32_MAX is
2147// returned in partNum if the sector is in use by basic GPT data structures.)
2148int GPTData::IsFree(uint64_t sector, uint32_t *partNum) {
srs5694e4ac11e2009-08-31 10:13:04 -04002149 int isFree = 1;
2150 uint32_t i;
2151
srs56940283dae2010-04-28 16:44:34 -04002152 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04002153 if ((sector >= partitions[i].GetFirstLBA()) &&
2154 (sector <= partitions[i].GetLastLBA())) {
2155 isFree = 0;
srs569455d92612010-03-07 22:16:07 -05002156 if (partNum != NULL)
2157 *partNum = i;
srs569408bb0da2010-02-19 17:19:55 -05002158 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002159 } // for
srs5694e35eb1b2009-09-14 00:29:34 -04002160 if ((sector < mainHeader.firstUsableLBA) ||
srs5694e4ac11e2009-08-31 10:13:04 -04002161 (sector > mainHeader.lastUsableLBA)) {
2162 isFree = 0;
srs569455d92612010-03-07 22:16:07 -05002163 if (partNum != NULL)
2164 *partNum = UINT32_MAX;
srs569408bb0da2010-02-19 17:19:55 -05002165 } // if
2166 return (isFree);
srs5694e4ac11e2009-08-31 10:13:04 -04002167} // GPTData::IsFree()
2168
srs5694ba00fed2010-01-12 18:18:36 -05002169// Returns 1 if partNum is unused.
2170int GPTData::IsFreePartNum(uint32_t partNum) {
2171 int retval = 1;
2172
srs56940283dae2010-04-28 16:44:34 -04002173 if ((partNum < numParts) && (partitions != NULL)) {
srs569408bb0da2010-02-19 17:19:55 -05002174 if (partitions[partNum].IsUsed()) {
srs5694ba00fed2010-01-12 18:18:36 -05002175 retval = 0;
2176 } // if partition is in use
2177 } else retval = 0;
2178
2179 return retval;
2180} // GPTData::IsFreePartNum()
2181
srs5694a8582cf2010-03-19 14:21:59 -04002182
2183/***********************************************************
2184 * *
2185 * Change how functions work or return information on them *
2186 * *
2187 ***********************************************************/
2188
2189// Set partition alignment value; partitions will begin on multiples of
2190// the specified value
2191void GPTData::SetAlignment(uint32_t n) {
srs56940873e9d2010-10-07 13:00:45 -04002192 if (n > 0)
2193 sectorAlignment = n;
2194 else
2195 cerr << "Attempt to set partition alignment to 0!\n";
srs5694a8582cf2010-03-19 14:21:59 -04002196} // GPTData::SetAlignment()
2197
2198// Compute sector alignment based on the current partitions (if any). Each
2199// partition's starting LBA is examined, and if it's divisible by a power-of-2
srs56940873e9d2010-10-07 13:00:45 -04002200// value less than or equal to the DEFAULT_ALIGNMENT value (adjusted for the
2201// sector size), but not by the previously-located alignment value, then the
2202// alignment value is adjusted down. If the computed alignment is less than 8
2203// and the disk is bigger than SMALLEST_ADVANCED_FORMAT, resets it to 8. This
2204// is a safety measure for WD Advanced Format and similar drives. If no partitions
2205// are defined, the alignment value is set to DEFAULT_ALIGNMENT (2048) (or an
2206// adjustment of that based on the current sector size). The result is that new
srs56948a4ddfc2010-03-21 19:05:49 -04002207// drives are aligned to 2048-sector multiples but the program won't complain
2208// about other alignments on existing disks unless a smaller-than-8 alignment
srs56940873e9d2010-10-07 13:00:45 -04002209// is used on big disks (as safety for WD Advanced Format drives).
srs5694a8582cf2010-03-19 14:21:59 -04002210// Returns the computed alignment value.
2211uint32_t GPTData::ComputeAlignment(void) {
2212 uint32_t i = 0, found, exponent = 31;
srs5694ab4b0432010-09-25 20:39:52 -04002213 uint32_t align = DEFAULT_ALIGNMENT;
srs5694a8582cf2010-03-19 14:21:59 -04002214
srs56940873e9d2010-10-07 13:00:45 -04002215 if (blockSize > 0)
2216 align = DEFAULT_ALIGNMENT * SECTOR_SIZE / blockSize;
2217 exponent = (uint32_t) log2(align);
srs56940283dae2010-04-28 16:44:34 -04002218 for (i = 0; i < numParts; i++) {
srs5694a8582cf2010-03-19 14:21:59 -04002219 if (partitions[i].IsUsed()) {
2220 found = 0;
2221 while (!found) {
srs56940873e9d2010-10-07 13:00:45 -04002222 align = UINT64_C(1) << exponent;
srs5694a8582cf2010-03-19 14:21:59 -04002223 if ((partitions[i].GetFirstLBA() % align) == 0) {
2224 found = 1;
2225 } else {
2226 exponent--;
2227 } // if/else
2228 } // while
2229 } // if
2230 } // for
srs56940873e9d2010-10-07 13:00:45 -04002231 if ((align < MIN_AF_ALIGNMENT) && (diskSize >= SMALLEST_ADVANCED_FORMAT))
2232 align = MIN_AF_ALIGNMENT;
2233 sectorAlignment = align;
srs5694a8582cf2010-03-19 14:21:59 -04002234 return align;
2235} // GPTData::ComputeAlignment()
2236
srs5694e4ac11e2009-08-31 10:13:04 -04002237/********************************
2238 * *
2239 * Endianness support functions *
2240 * *
2241 ********************************/
2242
srs56942a9f5da2009-08-26 00:48:01 -04002243void GPTData::ReverseHeaderBytes(struct GPTHeader* header) {
srs5694221e0872009-08-29 15:00:31 -04002244 ReverseBytes(&header->signature, 8);
2245 ReverseBytes(&header->revision, 4);
2246 ReverseBytes(&header->headerSize, 4);
2247 ReverseBytes(&header->headerCRC, 4);
2248 ReverseBytes(&header->reserved, 4);
2249 ReverseBytes(&header->currentLBA, 8);
2250 ReverseBytes(&header->backupLBA, 8);
2251 ReverseBytes(&header->firstUsableLBA, 8);
2252 ReverseBytes(&header->lastUsableLBA, 8);
2253 ReverseBytes(&header->partitionEntriesLBA, 8);
2254 ReverseBytes(&header->numParts, 4);
2255 ReverseBytes(&header->sizeOfPartitionEntries, 4);
2256 ReverseBytes(&header->partitionEntriesCRC, 4);
srs569408bb0da2010-02-19 17:19:55 -05002257 ReverseBytes(header->reserved2, GPT_RESERVED);
srs56942a9f5da2009-08-26 00:48:01 -04002258} // GPTData::ReverseHeaderBytes()
2259
srs56940283dae2010-04-28 16:44:34 -04002260// Reverse byte order for all partitions.
srs56942a9f5da2009-08-26 00:48:01 -04002261void GPTData::ReversePartitionBytes() {
2262 uint32_t i;
2263
srs56940283dae2010-04-28 16:44:34 -04002264 for (i = 0; i < numParts; i++) {
srs5694221e0872009-08-29 15:00:31 -04002265 partitions[i].ReversePartBytes();
srs56942a9f5da2009-08-26 00:48:01 -04002266 } // for
2267} // GPTData::ReversePartitionBytes()
2268
srs56949ddc14b2010-08-22 22:44:42 -04002269// Validate partition number
2270bool GPTData::ValidPartNum (const uint32_t partNum) {
2271 if (partNum >= numParts) {
srs56945a081752010-09-24 20:39:41 -04002272 cerr << "Partition number out of range: " << partNum << "\n";
srs56949ddc14b2010-08-22 22:44:42 -04002273 return false;
2274 } // if
2275 return true;
2276} // GPTData::ValidPartNum
2277
srs56945a081752010-09-24 20:39:41 -04002278// Return a single partition for inspection (not modification!) by other
2279// functions.
2280const GPTPart & GPTData::operator[](uint32_t partNum) const {
2281 if (partNum >= numParts) {
2282 cerr << "Partition number out of range: " << partNum << "\n";
2283 partNum = 0;
2284 } // if
2285 return partitions[partNum];
2286} // operator[]
2287
2288// Return (not for modification!) the disk's GUID value
2289const GUIDData & GPTData::GetDiskGUID(void) const {
2290 return mainHeader.diskGUID;
2291} // GPTData::GetDiskGUID()
2292
srs56949ddc14b2010-08-22 22:44:42 -04002293// Manage attributes for a partition, based on commands passed to this function.
2294// (Function is non-interactive.)
2295// Returns 1 if a modification command succeeded, 0 if the command should not have
2296// modified data, and -1 if a modification command failed.
2297int GPTData::ManageAttributes(int partNum, const string & command, const string & bits) {
2298 int retval = 0;
2299 Attributes theAttr;
2300
2301 if (command == "show") {
2302 ShowAttributes(partNum);
2303 } else if (command == "get") {
2304 GetAttribute(partNum, bits);
2305 } else {
2306 theAttr = partitions[partNum].GetAttributes();
2307 if (theAttr.OperateOnAttributes(partNum, command, bits)) {
2308 partitions[partNum].SetAttributes(theAttr.GetAttributes());
2309 retval = 1;
2310 } else {
2311 retval = -1;
2312 } // if/else
2313 } // if/elseif/else
2314
2315 return retval;
2316} // GPTData::ManageAttributes()
2317
2318// Show all attributes for a specified partition....
2319void GPTData::ShowAttributes(const uint32_t partNum) {
srs56940873e9d2010-10-07 13:00:45 -04002320 partitions[partNum].ShowAttributes(partNum);
srs56949ddc14b2010-08-22 22:44:42 -04002321} // GPTData::ShowAttributes
2322
2323// Show whether a single attribute bit is set (terse output)...
2324void GPTData::GetAttribute(const uint32_t partNum, const string& attributeBits) {
srs56940873e9d2010-10-07 13:00:45 -04002325 partitions[partNum].GetAttributes().OperateOnAttributes(partNum, "get", attributeBits);
srs56949ddc14b2010-08-22 22:44:42 -04002326} // GPTData::GetAttribute
2327
2328
srs56942a9f5da2009-08-26 00:48:01 -04002329/******************************************
2330 * *
2331 * Additional non-class support functions *
2332 * *
2333 ******************************************/
2334
srs5694e7b4ff92009-08-18 13:16:10 -04002335// Check to be sure that data type sizes are correct. The basic types (uint*_t) should
2336// never fail these tests, but the struct types may fail depending on compile options.
2337// Specifically, the -fpack-struct option to gcc may be required to ensure proper structure
2338// sizes.
2339int SizesOK(void) {
2340 int allOK = 1;
srs5694e7b4ff92009-08-18 13:16:10 -04002341
2342 if (sizeof(uint8_t) != 1) {
srs5694fed16d02010-01-27 23:03:40 -05002343 cerr << "uint8_t is " << sizeof(uint8_t) << " bytes, should be 1 byte; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002344 allOK = 0;
2345 } // if
2346 if (sizeof(uint16_t) != 2) {
srs5694fed16d02010-01-27 23:03:40 -05002347 cerr << "uint16_t is " << sizeof(uint16_t) << " bytes, should be 2 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002348 allOK = 0;
2349 } // if
2350 if (sizeof(uint32_t) != 4) {
srs5694fed16d02010-01-27 23:03:40 -05002351 cerr << "uint32_t is " << sizeof(uint32_t) << " bytes, should be 4 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002352 allOK = 0;
2353 } // if
2354 if (sizeof(uint64_t) != 8) {
srs5694fed16d02010-01-27 23:03:40 -05002355 cerr << "uint64_t is " << sizeof(uint64_t) << " bytes, should be 8 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002356 allOK = 0;
2357 } // if
2358 if (sizeof(struct MBRRecord) != 16) {
srs5694fed16d02010-01-27 23:03:40 -05002359 cerr << "MBRRecord is " << sizeof(MBRRecord) << " bytes, should be 16 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002360 allOK = 0;
2361 } // if
srs5694978041c2009-09-21 20:51:47 -04002362 if (sizeof(struct TempMBR) != 512) {
srs5694fed16d02010-01-27 23:03:40 -05002363 cerr << "TempMBR is " << sizeof(TempMBR) << " bytes, should be 512 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002364 allOK = 0;
2365 } // if
2366 if (sizeof(struct GPTHeader) != 512) {
srs5694fed16d02010-01-27 23:03:40 -05002367 cerr << "GPTHeader is " << sizeof(GPTHeader) << " bytes, should be 512 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002368 allOK = 0;
2369 } // if
srs5694221e0872009-08-29 15:00:31 -04002370 if (sizeof(GPTPart) != 128) {
srs5694fed16d02010-01-27 23:03:40 -05002371 cerr << "GPTPart is " << sizeof(GPTPart) << " bytes, should be 128 bytes; aborting!\n";
srs5694221e0872009-08-29 15:00:31 -04002372 allOK = 0;
2373 } // if
srs56946699b012010-02-04 00:55:30 -05002374 if (sizeof(GUIDData) != 16) {
2375 cerr << "GUIDData is " << sizeof(GUIDData) << " bytes, should be 16 bytes; aborting!\n";
2376 allOK = 0;
2377 } // if
2378 if (sizeof(PartType) != 16) {
2379 cerr << "PartType is " << sizeof(GUIDData) << " bytes, should be 16 bytes; aborting!\n";
2380 allOK = 0;
2381 } // if
srs5694fed16d02010-01-27 23:03:40 -05002382 // Determine endianness; warn user if running on big-endian (PowerPC, etc.) hardware
srs56940873e9d2010-10-07 13:00:45 -04002383// if (IsLittleEndian() == 0) {
2384// cerr << "\aRunning on big-endian hardware. Big-endian support is new and poorly"
2385// " tested!\n";
2386// } // if
srs5694e7b4ff92009-08-18 13:16:10 -04002387 return (allOK);
2388} // SizesOK()
srs5694e4ac11e2009-08-31 10:13:04 -04002389