blob: 3c9b84395572ea13d6a277379804fff19892ee3c [file] [log] [blame]
srs5694e7b4ff92009-08-18 13:16:10 -04001/* gpt.cc -- Functions for loading, saving, and manipulating legacy MBR and GPT partition
2 data. */
3
srs5694e4ac11e2009-08-31 10:13:04 -04004/* By Rod Smith, initial coding January to February, 2009 */
srs5694e7b4ff92009-08-18 13:16:10 -04005
srs5694221e0872009-08-29 15:00:31 -04006/* This program is copyright (c) 2009 by Roderick W. Smith. It is distributed
7 under the terms of the GNU GPL version 2, as detailed in the COPYING file. */
8
srs5694e7b4ff92009-08-18 13:16:10 -04009#define __STDC_LIMIT_MACROS
10#define __STDC_CONSTANT_MACROS
11
12#include <stdio.h>
srs5694e7b4ff92009-08-18 13:16:10 -040013#include <stdlib.h>
14#include <stdint.h>
15#include <fcntl.h>
16#include <string.h>
srs5694a8582cf2010-03-19 14:21:59 -040017#include <math.h>
srs5694e7b4ff92009-08-18 13:16:10 -040018#include <time.h>
19#include <sys/stat.h>
20#include <errno.h>
srs5694fed16d02010-01-27 23:03:40 -050021#include <iostream>
srs5694e7b4ff92009-08-18 13:16:10 -040022#include "crc32.h"
23#include "gpt.h"
srs5694221e0872009-08-29 15:00:31 -040024#include "bsd.h"
srs5694e7b4ff92009-08-18 13:16:10 -040025#include "support.h"
26#include "parttypes.h"
27#include "attributes.h"
srs5694546a9c72010-01-26 16:00:26 -050028#include "diskio.h"
srs569455d92612010-03-07 22:16:07 -050029#include "partnotes.h"
srs5694e7b4ff92009-08-18 13:16:10 -040030
31using namespace std;
32
srs56948f1b2d62010-05-23 13:07:19 -040033#ifdef __FreeBSD__
srs56949ba54212010-05-18 23:24:02 -040034#define log2(x) (log(x) / M_LN2)
35#endif // __FreeBSD__
36
srs56948f1b2d62010-05-23 13:07:19 -040037#ifdef _MSC_VER
38#define log2(x) (log((double) x) / log(2.0))
39#endif // Microsoft Visual C++
srs56949ba54212010-05-18 23:24:02 -040040
srs5694e7b4ff92009-08-18 13:16:10 -040041/****************************************
42 * *
43 * GPTData class and related structures *
44 * *
45 ****************************************/
46
srs5694e4ac11e2009-08-31 10:13:04 -040047// Default constructor
srs5694e7b4ff92009-08-18 13:16:10 -040048GPTData::GPTData(void) {
49 blockSize = SECTOR_SIZE; // set a default
50 diskSize = 0;
51 partitions = NULL;
52 state = gpt_valid;
srs5694fed16d02010-01-27 23:03:40 -050053 device = "";
srs56945d58fe02010-01-03 20:57:08 -050054 justLooking = 0;
srs5694e7b4ff92009-08-18 13:16:10 -040055 mainCrcOk = 0;
56 secondCrcOk = 0;
57 mainPartsCrcOk = 0;
58 secondPartsCrcOk = 0;
srs5694221e0872009-08-29 15:00:31 -040059 apmFound = 0;
60 bsdFound = 0;
srs56940873e9d2010-10-07 13:00:45 -040061 sectorAlignment = MIN_AF_ALIGNMENT; // Align partitions on 4096-byte boundaries by default
srs5694ba00fed2010-01-12 18:18:36 -050062 beQuiet = 0;
63 whichWasUsed = use_new;
srs5694e7b4ff92009-08-18 13:16:10 -040064 srand((unsigned int) time(NULL));
srs56941e093722010-01-05 00:14:19 -050065 mainHeader.numParts = 0;
srs56940283dae2010-04-28 16:44:34 -040066 numParts = 0;
srs5694e7b4ff92009-08-18 13:16:10 -040067 SetGPTSize(NUM_GPT_ENTRIES);
68} // GPTData default constructor
69
70// The following constructor loads GPT data from a device file
srs5694fed16d02010-01-27 23:03:40 -050071GPTData::GPTData(string filename) {
srs5694e7b4ff92009-08-18 13:16:10 -040072 blockSize = SECTOR_SIZE; // set a default
73 diskSize = 0;
74 partitions = NULL;
75 state = gpt_invalid;
srs5694fed16d02010-01-27 23:03:40 -050076 device = "";
srs56945d58fe02010-01-03 20:57:08 -050077 justLooking = 0;
srs5694e7b4ff92009-08-18 13:16:10 -040078 mainCrcOk = 0;
79 secondCrcOk = 0;
80 mainPartsCrcOk = 0;
81 secondPartsCrcOk = 0;
srs5694221e0872009-08-29 15:00:31 -040082 apmFound = 0;
83 bsdFound = 0;
srs56940873e9d2010-10-07 13:00:45 -040084 sectorAlignment = MIN_AF_ALIGNMENT; // Align partitions on 4096-byte boundaries by default
srs5694ba00fed2010-01-12 18:18:36 -050085 beQuiet = 0;
86 whichWasUsed = use_new;
srs5694e7b4ff92009-08-18 13:16:10 -040087 srand((unsigned int) time(NULL));
srs56941e093722010-01-05 00:14:19 -050088 mainHeader.numParts = 0;
srs56940283dae2010-04-28 16:44:34 -040089 numParts = 0;
srs56943c0af382010-01-15 19:19:18 -050090 if (!LoadPartitions(filename))
91 exit(2);
srs5694fed16d02010-01-27 23:03:40 -050092} // GPTData(string filename) constructor
srs5694e7b4ff92009-08-18 13:16:10 -040093
srs5694e4ac11e2009-08-31 10:13:04 -040094// Destructor
srs5694e7b4ff92009-08-18 13:16:10 -040095GPTData::~GPTData(void) {
srs5694cb76c672010-02-11 22:22:22 -050096 delete[] partitions;
srs5694e7b4ff92009-08-18 13:16:10 -040097} // GPTData destructor
98
srs5694e4ac11e2009-08-31 10:13:04 -040099/*********************************************************************
100 * *
101 * Begin functions that verify data, or that adjust the verification *
102 * information (compute CRCs, rebuild headers) *
103 * *
104 *********************************************************************/
srs5694e7b4ff92009-08-18 13:16:10 -0400105
srs5694e4ac11e2009-08-31 10:13:04 -0400106// Perform detailed verification, reporting on any problems found, but
107// do *NOT* recover from these problems. Returns the total number of
108// problems identified.
109int GPTData::Verify(void) {
srs5694e321d442010-01-29 17:44:04 -0500110 int problems = 0;
111 uint32_t i, numSegments;
112 uint64_t totalFree, largestSegment;
srs5694e4ac11e2009-08-31 10:13:04 -0400113
114 // First, check for CRC errors in the GPT data....
115 if (!mainCrcOk) {
116 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500117 cout << "\nProblem: The CRC for the main GPT header is invalid. The main GPT header may\n"
118 << "be corrupt. Consider loading the backup GPT header to rebuild the main GPT\n"
119 << "header ('b' on the recovery & transformation menu). This report may be a false\n"
120 << "alarm if you've already corrected other problems.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400121 } // if
122 if (!mainPartsCrcOk) {
123 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500124 cout << "\nProblem: The CRC for the main partition table is invalid. This table may be\n"
125 << "corrupt. Consider loading the backup partition table ('c' on the recovery &\n"
126 << "transformation menu). This report may be a false alarm if you've already\n"
127 << "corrected other problems.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400128 } // if
129 if (!secondCrcOk) {
130 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500131 cout << "\nProblem: The CRC for the backup GPT header is invalid. The backup GPT header\n"
132 << "may be corrupt. Consider using the main GPT header to rebuild the backup GPT\n"
133 << "header ('d' on the recovery & transformation menu). This report may be a false\n"
134 << "alarm if you've already corrected other problems.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400135 } // if
136 if (!secondPartsCrcOk) {
137 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500138 cout << "\nCaution: The CRC for the backup partition table is invalid. This table may\n"
139 << "be corrupt. This program will automatically create a new backup partition\n"
140 << "table when you save your partitions.\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400141 } // if
142
srs5694978041c2009-09-21 20:51:47 -0400143 // Now check that the main and backup headers both point to themselves....
144 if (mainHeader.currentLBA != 1) {
145 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500146 cout << "\nProblem: The main header's self-pointer doesn't point to itself. This problem\n"
147 << "is being automatically corrected, but it may be a symptom of more serious\n"
148 << "problems. Think carefully before saving changes with 'w' or using this disk.\n";
srs5694978041c2009-09-21 20:51:47 -0400149 mainHeader.currentLBA = 1;
150 } // if
151 if (secondHeader.currentLBA != (diskSize - UINT64_C(1))) {
152 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500153 cout << "\nProblem: The secondary header's self-pointer indicates that it doesn't reside\n"
154 << "at the end of the disk. If you've added a disk to a RAID array, use the 'e'\n"
155 << "option on the experts' menu to adjust the secondary header's and partition\n"
156 << "table's locations.\n";
srs5694978041c2009-09-21 20:51:47 -0400157 } // if
158
159 // Now check that critical main and backup GPT entries match each other
srs5694e4ac11e2009-08-31 10:13:04 -0400160 if (mainHeader.currentLBA != secondHeader.backupLBA) {
161 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500162 cout << "\nProblem: main GPT header's current LBA pointer (" << mainHeader.currentLBA
163 << ") doesn't\nmatch the backup GPT header's alternate LBA pointer("
164 << secondHeader.backupLBA << ").\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400165 } // if
166 if (mainHeader.backupLBA != secondHeader.currentLBA) {
167 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500168 cout << "\nProblem: main GPT header's backup LBA pointer (" << mainHeader.backupLBA
169 << ") doesn't\nmatch the backup GPT header's current LBA pointer ("
170 << secondHeader.currentLBA << ").\n"
171 << "The 'e' option on the experts' menu may fix this problem.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400172 } // if
173 if (mainHeader.firstUsableLBA != secondHeader.firstUsableLBA) {
174 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500175 cout << "\nProblem: main GPT header's first usable LBA pointer (" << mainHeader.firstUsableLBA
176 << ") doesn't\nmatch the backup GPT header's first usable LBA pointer ("
177 << secondHeader.firstUsableLBA << ")\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400178 } // if
179 if (mainHeader.lastUsableLBA != secondHeader.lastUsableLBA) {
180 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500181 cout << "\nProblem: main GPT header's last usable LBA pointer (" << mainHeader.lastUsableLBA
182 << ") doesn't\nmatch the backup GPT header's last usable LBA pointer ("
183 << secondHeader.lastUsableLBA << ")\n"
184 << "The 'e' option on the experts' menu can probably fix this problem.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400185 } // if
srs56946699b012010-02-04 00:55:30 -0500186 if ((mainHeader.diskGUID != secondHeader.diskGUID)) {
srs5694e4ac11e2009-08-31 10:13:04 -0400187 problems++;
srs56945a081752010-09-24 20:39:41 -0400188 cout << "\nProblem: main header's disk GUID (" << mainHeader.diskGUID
srs5694fed16d02010-01-27 23:03:40 -0500189 << ") doesn't\nmatch the backup GPT header's disk GUID ("
srs56945a081752010-09-24 20:39:41 -0400190 << secondHeader.diskGUID << ")\n"
srs5694fed16d02010-01-27 23:03:40 -0500191 << "You should use the 'b' or 'd' option on the recovery & transformation menu to\n"
192 << "select one or the other header.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400193 } // if
194 if (mainHeader.numParts != secondHeader.numParts) {
195 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500196 cout << "\nProblem: main GPT header's number of partitions (" << mainHeader.numParts
197 << ") doesn't\nmatch the backup GPT header's number of partitions ("
198 << secondHeader.numParts << ")\n"
199 << "Resizing the partition table ('s' on the experts' menu) may help.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400200 } // if
201 if (mainHeader.sizeOfPartitionEntries != secondHeader.sizeOfPartitionEntries) {
202 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500203 cout << "\nProblem: main GPT header's size of partition entries ("
204 << mainHeader.sizeOfPartitionEntries << ") doesn't\n"
205 << "match the backup GPT header's size of partition entries ("
206 << secondHeader.sizeOfPartitionEntries << ")\n"
207 << "You should use the 'b' or 'd' option on the recovery & transformation menu to\n"
208 << "select one or the other header.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400209 } // if
210
211 // Now check for a few other miscellaneous problems...
212 // Check that the disk size will hold the data...
213 if (mainHeader.backupLBA > diskSize) {
214 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500215 cout << "\nProblem: Disk is too small to hold all the data!\n"
216 << "(Disk size is " << diskSize << " sectors, needs to be "
217 << mainHeader.backupLBA << " sectors.)\n"
218 << "The 'e' option on the experts' menu may fix this problem.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400219 } // if
220
221 // Check for overlapping partitions....
222 problems += FindOverlaps();
223
srs569455d92612010-03-07 22:16:07 -0500224 // Check for insane partitions (start after end, hugely big, etc.)
225 problems += FindInsanePartitions();
226
srs5694e4ac11e2009-08-31 10:13:04 -0400227 // Check for mismatched MBR and GPT partitions...
228 problems += FindHybridMismatches();
229
srs5694327129e2010-09-22 01:07:31 -0400230 // Check for MBR-specific problems....
231 problems += VerifyMBR();
232
srs5694e4ac11e2009-08-31 10:13:04 -0400233 // Verify that partitions don't run into GPT data areas....
234 problems += CheckGPTSize();
235
srs56941d1448a2009-12-31 21:20:19 -0500236 // Check that partitions are aligned on proper boundaries (for WD Advanced
237 // Format and similar disks)....
srs56940283dae2010-04-28 16:44:34 -0400238 for (i = 0; i < numParts; i++) {
srs56941d1448a2009-12-31 21:20:19 -0500239 if ((partitions[i].GetFirstLBA() % sectorAlignment) != 0) {
srs5694fed16d02010-01-27 23:03:40 -0500240 cout << "\nCaution: Partition " << i + 1 << " doesn't begin on a "
241 << sectorAlignment << "-sector boundary. This may\nresult "
242 << "in degraded performance on some modern (2009 and later) hard disks.\n";
srs56941d1448a2009-12-31 21:20:19 -0500243 } // if
244 } // for
245
srs5694e4ac11e2009-08-31 10:13:04 -0400246 // Now compute available space, but only if no problems found, since
247 // problems could affect the results
248 if (problems == 0) {
249 totalFree = FindFreeBlocks(&numSegments, &largestSegment);
srs5694fed16d02010-01-27 23:03:40 -0500250 cout << "No problems found. " << totalFree << " free sectors ("
srs56940873e9d2010-10-07 13:00:45 -0400251 << BytesToSI(totalFree, blockSize) << ") available in "
srs5694fed16d02010-01-27 23:03:40 -0500252 << numSegments << "\nsegments, the largest of which is "
srs56940873e9d2010-10-07 13:00:45 -0400253 << largestSegment << " (" << BytesToSI(largestSegment, blockSize)
srs56940283dae2010-04-28 16:44:34 -0400254 << ") in size.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400255 } else {
srs56940a697312010-01-28 21:10:52 -0500256 cout << "\nIdentified " << problems << " problems!\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400257 } // if/else
srs5694e4ac11e2009-08-31 10:13:04 -0400258
259 return (problems);
260} // GPTData::Verify()
srs5694e7b4ff92009-08-18 13:16:10 -0400261
262// Checks to see if the GPT tables overrun existing partitions; if they
srs5694221e0872009-08-29 15:00:31 -0400263// do, issues a warning but takes no action. Returns number of problems
264// detected (0 if OK, 1 to 2 if problems).
srs5694e7b4ff92009-08-18 13:16:10 -0400265int GPTData::CheckGPTSize(void) {
266 uint64_t overlap, firstUsedBlock, lastUsedBlock;
267 uint32_t i;
srs5694221e0872009-08-29 15:00:31 -0400268 int numProbs = 0;
srs5694e7b4ff92009-08-18 13:16:10 -0400269
270 // first, locate the first & last used blocks
271 firstUsedBlock = UINT64_MAX;
272 lastUsedBlock = 0;
srs56940283dae2010-04-28 16:44:34 -0400273 for (i = 0; i < numParts; i++) {
srs5694221e0872009-08-29 15:00:31 -0400274 if ((partitions[i].GetFirstLBA() < firstUsedBlock) &&
srs5694e4ac11e2009-08-31 10:13:04 -0400275 (partitions[i].GetFirstLBA() != 0))
srs5694221e0872009-08-29 15:00:31 -0400276 firstUsedBlock = partitions[i].GetFirstLBA();
277 if (partitions[i].GetLastLBA() > lastUsedBlock)
278 lastUsedBlock = partitions[i].GetLastLBA();
srs5694e7b4ff92009-08-18 13:16:10 -0400279 } // for
280
281 // If the disk size is 0 (the default), then it means that various
282 // variables aren't yet set, so the below tests will be useless;
283 // therefore we should skip everything
284 if (diskSize != 0) {
285 if (mainHeader.firstUsableLBA > firstUsedBlock) {
286 overlap = mainHeader.firstUsableLBA - firstUsedBlock;
srs5694fed16d02010-01-27 23:03:40 -0500287 cout << "Warning! Main partition table overlaps the first partition by "
288 << overlap << " blocks!\n";
srs5694221e0872009-08-29 15:00:31 -0400289 if (firstUsedBlock > 2) {
srs5694fed16d02010-01-27 23:03:40 -0500290 cout << "Try reducing the partition table size by " << overlap * 4
291 << " entries.\n(Use the 's' item on the experts' menu.)\n";
srs5694221e0872009-08-29 15:00:31 -0400292 } else {
srs5694fed16d02010-01-27 23:03:40 -0500293 cout << "You will need to delete this partition or resize it in another utility.\n";
srs5694221e0872009-08-29 15:00:31 -0400294 } // if/else
295 numProbs++;
srs5694e7b4ff92009-08-18 13:16:10 -0400296 } // Problem at start of disk
297 if (mainHeader.lastUsableLBA < lastUsedBlock) {
298 overlap = lastUsedBlock - mainHeader.lastUsableLBA;
srs569455d92612010-03-07 22:16:07 -0500299 cout << "\nWarning! Secondary partition table overlaps the last partition by\n"
srs5694fed16d02010-01-27 23:03:40 -0500300 << overlap << " blocks!\n";
srs5694221e0872009-08-29 15:00:31 -0400301 if (lastUsedBlock > (diskSize - 2)) {
srs5694fed16d02010-01-27 23:03:40 -0500302 cout << "You will need to delete this partition or resize it in another utility.\n";
srs5694221e0872009-08-29 15:00:31 -0400303 } else {
srs5694fed16d02010-01-27 23:03:40 -0500304 cout << "Try reducing the partition table size by " << overlap * 4
305 << " entries.\n(Use the 's' item on the experts' menu.)\n";
srs5694221e0872009-08-29 15:00:31 -0400306 } // if/else
307 numProbs++;
srs5694e7b4ff92009-08-18 13:16:10 -0400308 } // Problem at end of disk
309 } // if (diskSize != 0)
srs5694221e0872009-08-29 15:00:31 -0400310 return numProbs;
srs5694e7b4ff92009-08-18 13:16:10 -0400311} // GPTData::CheckGPTSize()
312
srs5694e7b4ff92009-08-18 13:16:10 -0400313// Check the validity of the GPT header. Returns 1 if the main header
314// is valid, 2 if the backup header is valid, 3 if both are valid, and
315// 0 if neither is valid. Note that this function just checks the GPT
316// signature and revision numbers, not CRCs or other data.
317int GPTData::CheckHeaderValidity(void) {
318 int valid = 3;
319
srs5694fed16d02010-01-27 23:03:40 -0500320 cout.setf(ios::uppercase);
321 cout.fill('0');
322
323 // Note: failed GPT signature checks produce no error message because
324 // a message is displayed in the ReversePartitionBytes() function
srs5694e7b4ff92009-08-18 13:16:10 -0400325 if (mainHeader.signature != GPT_SIGNATURE) {
326 valid -= 1;
srs5694e7b4ff92009-08-18 13:16:10 -0400327 } else if ((mainHeader.revision != 0x00010000) && valid) {
328 valid -= 1;
srs5694fed16d02010-01-27 23:03:40 -0500329 cout << "Unsupported GPT version in main header; read 0x";
330 cout.width(8);
331 cout << hex << mainHeader.revision << ", should be\n0x";
332 cout.width(8);
333 cout << UINT32_C(0x00010000) << dec << "\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400334 } // if/else/if
335
336 if (secondHeader.signature != GPT_SIGNATURE) {
337 valid -= 2;
srs5694e7b4ff92009-08-18 13:16:10 -0400338 } else if ((secondHeader.revision != 0x00010000) && valid) {
339 valid -= 2;
srs5694fed16d02010-01-27 23:03:40 -0500340 cout << "Unsupported GPT version in backup header; read 0x";
341 cout.width(8);
342 cout << hex << secondHeader.revision << ", should be\n0x";
343 cout.width(8);
344 cout << UINT32_C(0x00010000) << dec << "\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400345 } // if/else/if
346
srs56942a9f5da2009-08-26 00:48:01 -0400347 // If MBR bad, check for an Apple disk signature
srs5694e35eb1b2009-09-14 00:29:34 -0400348 if ((protectiveMBR.GetValidity() == invalid) &&
srs5694e4ac11e2009-08-31 10:13:04 -0400349 (((mainHeader.signature << 32) == APM_SIGNATURE1) ||
srs56942a9f5da2009-08-26 00:48:01 -0400350 (mainHeader.signature << 32) == APM_SIGNATURE2)) {
srs5694221e0872009-08-29 15:00:31 -0400351 apmFound = 1; // Will display warning message later
srs56943f2fe992009-11-24 18:28:18 -0500352 } // if
srs5694fed16d02010-01-27 23:03:40 -0500353 cout.fill(' ');
srs56942a9f5da2009-08-26 00:48:01 -0400354
srs5694fed16d02010-01-27 23:03:40 -0500355 return valid;
srs5694e7b4ff92009-08-18 13:16:10 -0400356} // GPTData::CheckHeaderValidity()
357
358// Check the header CRC to see if it's OK...
srs5694cb76c672010-02-11 22:22:22 -0500359// Note: Must be called with header in LITTLE-ENDIAN
360// (x86, x86-64, etc.) byte order.
srs5694e7b4ff92009-08-18 13:16:10 -0400361int GPTData::CheckHeaderCRC(struct GPTHeader* header) {
srs5694978041c2009-09-21 20:51:47 -0400362 uint32_t oldCRC, newCRC, hSize;
srs5694e7b4ff92009-08-18 13:16:10 -0400363
srs56942a9f5da2009-08-26 00:48:01 -0400364 // Back up old header CRC and then blank it, since it must be 0 for
srs5694e7b4ff92009-08-18 13:16:10 -0400365 // computation to be valid
366 oldCRC = header->headerCRC;
367 header->headerCRC = UINT32_C(0);
srs5694978041c2009-09-21 20:51:47 -0400368 hSize = header->headerSize;
369
370 // If big-endian system, reverse byte order
371 if (IsLittleEndian() == 0) {
372 ReverseBytes(&oldCRC, 4);
373 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400374
375 // Initialize CRC functions...
376 chksum_crc32gentab();
377
378 // Compute CRC, restore original, and return result of comparison
379 newCRC = chksum_crc32((unsigned char*) header, HEADER_SIZE);
srs5694978041c2009-09-21 20:51:47 -0400380 header->headerCRC = oldCRC;
srs5694e7b4ff92009-08-18 13:16:10 -0400381 return (oldCRC == newCRC);
382} // GPTData::CheckHeaderCRC()
383
srs56946699b012010-02-04 00:55:30 -0500384// Recompute all the CRCs. Must be called before saving if any changes have
385// been made. Must be called on platform-ordered data (this function reverses
386// byte order and then undoes that reversal.)
srs5694e7b4ff92009-08-18 13:16:10 -0400387void GPTData::RecomputeCRCs(void) {
srs56940283dae2010-04-28 16:44:34 -0400388 uint32_t crc, hSize;
srs56942a9f5da2009-08-26 00:48:01 -0400389 int littleEndian = 1;
srs5694e7b4ff92009-08-18 13:16:10 -0400390
391 // Initialize CRC functions...
392 chksum_crc32gentab();
393
srs56946699b012010-02-04 00:55:30 -0500394 // Save some key data from header before reversing byte order....
srs5694978041c2009-09-21 20:51:47 -0400395 hSize = mainHeader.headerSize;
srs56946699b012010-02-04 00:55:30 -0500396
397 if ((littleEndian = IsLittleEndian()) == 0) {
398 ReversePartitionBytes();
399 ReverseHeaderBytes(&mainHeader);
400 ReverseHeaderBytes(&secondHeader);
401 } // if
srs56942a9f5da2009-08-26 00:48:01 -0400402
srs5694e7b4ff92009-08-18 13:16:10 -0400403 // Compute CRC of partition tables & store in main and secondary headers
srs56940283dae2010-04-28 16:44:34 -0400404 crc = chksum_crc32((unsigned char*) partitions, numParts * GPT_SIZE);
srs5694e7b4ff92009-08-18 13:16:10 -0400405 mainHeader.partitionEntriesCRC = crc;
406 secondHeader.partitionEntriesCRC = crc;
srs56942a9f5da2009-08-26 00:48:01 -0400407 if (littleEndian == 0) {
srs5694221e0872009-08-29 15:00:31 -0400408 ReverseBytes(&mainHeader.partitionEntriesCRC, 4);
409 ReverseBytes(&secondHeader.partitionEntriesCRC, 4);
srs56942a9f5da2009-08-26 00:48:01 -0400410 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400411
412 // Zero out GPT tables' own CRCs (required for correct computation)
413 mainHeader.headerCRC = 0;
414 secondHeader.headerCRC = 0;
415
416 // Compute & store CRCs of main & secondary headers...
srs5694978041c2009-09-21 20:51:47 -0400417 crc = chksum_crc32((unsigned char*) &mainHeader, hSize);
srs56942a9f5da2009-08-26 00:48:01 -0400418 if (littleEndian == 0)
srs5694221e0872009-08-29 15:00:31 -0400419 ReverseBytes(&crc, 4);
srs5694e7b4ff92009-08-18 13:16:10 -0400420 mainHeader.headerCRC = crc;
srs5694978041c2009-09-21 20:51:47 -0400421 crc = chksum_crc32((unsigned char*) &secondHeader, hSize);
srs56942a9f5da2009-08-26 00:48:01 -0400422 if (littleEndian == 0)
srs5694221e0872009-08-29 15:00:31 -0400423 ReverseBytes(&crc, 4);
srs5694e7b4ff92009-08-18 13:16:10 -0400424 secondHeader.headerCRC = crc;
srs56946699b012010-02-04 00:55:30 -0500425
426 if ((littleEndian = IsLittleEndian()) == 0) {
427 ReverseHeaderBytes(&mainHeader);
428 ReverseHeaderBytes(&secondHeader);
429 ReversePartitionBytes();
430 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400431} // GPTData::RecomputeCRCs()
432
srs5694e7b4ff92009-08-18 13:16:10 -0400433// Rebuild the main GPT header, using the secondary header as a model.
434// Typically called when the main header has been found to be corrupt.
435void GPTData::RebuildMainHeader(void) {
436 int i;
437
438 mainHeader.signature = GPT_SIGNATURE;
439 mainHeader.revision = secondHeader.revision;
srs5694978041c2009-09-21 20:51:47 -0400440 mainHeader.headerSize = secondHeader.headerSize;
srs5694e7b4ff92009-08-18 13:16:10 -0400441 mainHeader.headerCRC = UINT32_C(0);
442 mainHeader.reserved = secondHeader.reserved;
443 mainHeader.currentLBA = secondHeader.backupLBA;
444 mainHeader.backupLBA = secondHeader.currentLBA;
445 mainHeader.firstUsableLBA = secondHeader.firstUsableLBA;
446 mainHeader.lastUsableLBA = secondHeader.lastUsableLBA;
srs56946699b012010-02-04 00:55:30 -0500447 mainHeader.diskGUID = secondHeader.diskGUID;
srs5694e7b4ff92009-08-18 13:16:10 -0400448 mainHeader.partitionEntriesLBA = UINT64_C(2);
449 mainHeader.numParts = secondHeader.numParts;
450 mainHeader.sizeOfPartitionEntries = secondHeader.sizeOfPartitionEntries;
451 mainHeader.partitionEntriesCRC = secondHeader.partitionEntriesCRC;
452 for (i = 0 ; i < GPT_RESERVED; i++)
453 mainHeader.reserved2[i] = secondHeader.reserved2[i];
srs5694546a9c72010-01-26 16:00:26 -0500454 mainCrcOk = secondCrcOk;
srs56940283dae2010-04-28 16:44:34 -0400455 SetGPTSize(mainHeader.numParts);
srs5694e7b4ff92009-08-18 13:16:10 -0400456} // GPTData::RebuildMainHeader()
457
458// Rebuild the secondary GPT header, using the main header as a model.
459void GPTData::RebuildSecondHeader(void) {
460 int i;
461
462 secondHeader.signature = GPT_SIGNATURE;
463 secondHeader.revision = mainHeader.revision;
srs5694978041c2009-09-21 20:51:47 -0400464 secondHeader.headerSize = mainHeader.headerSize;
srs5694e7b4ff92009-08-18 13:16:10 -0400465 secondHeader.headerCRC = UINT32_C(0);
466 secondHeader.reserved = mainHeader.reserved;
467 secondHeader.currentLBA = mainHeader.backupLBA;
468 secondHeader.backupLBA = mainHeader.currentLBA;
469 secondHeader.firstUsableLBA = mainHeader.firstUsableLBA;
470 secondHeader.lastUsableLBA = mainHeader.lastUsableLBA;
srs56946699b012010-02-04 00:55:30 -0500471 secondHeader.diskGUID = mainHeader.diskGUID;
srs5694e7b4ff92009-08-18 13:16:10 -0400472 secondHeader.partitionEntriesLBA = secondHeader.lastUsableLBA + UINT64_C(1);
473 secondHeader.numParts = mainHeader.numParts;
474 secondHeader.sizeOfPartitionEntries = mainHeader.sizeOfPartitionEntries;
475 secondHeader.partitionEntriesCRC = mainHeader.partitionEntriesCRC;
476 for (i = 0 ; i < GPT_RESERVED; i++)
477 secondHeader.reserved2[i] = mainHeader.reserved2[i];
srs5694546a9c72010-01-26 16:00:26 -0500478 secondCrcOk = mainCrcOk;
srs56940283dae2010-04-28 16:44:34 -0400479 SetGPTSize(secondHeader.numParts);
srs5694e4ac11e2009-08-31 10:13:04 -0400480} // GPTData::RebuildSecondHeader()
481
482// Search for hybrid MBR entries that have no corresponding GPT partition.
483// Returns number of such mismatches found
484int GPTData::FindHybridMismatches(void) {
srs5694e321d442010-01-29 17:44:04 -0500485 int i, found, numFound = 0;
486 uint32_t j;
srs5694e4ac11e2009-08-31 10:13:04 -0400487 uint64_t mbrFirst, mbrLast;
488
489 for (i = 0; i < 4; i++) {
490 if ((protectiveMBR.GetType(i) != 0xEE) && (protectiveMBR.GetType(i) != 0x00)) {
491 j = 0;
492 found = 0;
493 do {
494 mbrFirst = (uint64_t) protectiveMBR.GetFirstSector(i);
495 mbrLast = mbrFirst + (uint64_t) protectiveMBR.GetLength(i) - UINT64_C(1);
496 if ((partitions[j].GetFirstLBA() == mbrFirst) &&
497 (partitions[j].GetLastLBA() == mbrLast))
498 found = 1;
499 j++;
srs56940283dae2010-04-28 16:44:34 -0400500 } while ((!found) && (j < numParts));
srs5694e4ac11e2009-08-31 10:13:04 -0400501 if (!found) {
502 numFound++;
srs5694fed16d02010-01-27 23:03:40 -0500503 cout << "\nWarning! Mismatched GPT and MBR partition! MBR partition "
504 << i + 1 << ", of type 0x";
505 cout.fill('0');
506 cout.setf(ios::uppercase);
507 cout.width(2);
508 cout << hex << (int) protectiveMBR.GetType(i) << ",\n"
509 << "has no corresponding GPT partition! You may continue, but this condition\n"
510 << "might cause data loss in the future!\a\n" << dec;
511 cout.fill(' ');
srs5694e4ac11e2009-08-31 10:13:04 -0400512 } // if
513 } // if
514 } // for
515 return numFound;
516} // GPTData::FindHybridMismatches
517
518// Find overlapping partitions and warn user about them. Returns number of
519// overlapping partitions.
520int GPTData::FindOverlaps(void) {
srs5694e321d442010-01-29 17:44:04 -0500521 int problems = 0;
522 uint32_t i, j;
srs5694e4ac11e2009-08-31 10:13:04 -0400523
srs56940283dae2010-04-28 16:44:34 -0400524 for (i = 1; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -0400525 for (j = 0; j < i; j++) {
srs56940a697312010-01-28 21:10:52 -0500526 if (partitions[i].DoTheyOverlap(partitions[j])) {
srs5694e4ac11e2009-08-31 10:13:04 -0400527 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500528 cout << "\nProblem: partitions " << i + 1 << " and " << j + 1 << " overlap:\n";
529 cout << " Partition " << i + 1 << ": " << partitions[i].GetFirstLBA()
530 << " to " << partitions[i].GetLastLBA() << "\n";
531 cout << " Partition " << j + 1 << ": " << partitions[j].GetFirstLBA()
532 << " to " << partitions[j].GetLastLBA() << "\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400533 } // if
534 } // for j...
535 } // for i...
536 return problems;
537} // GPTData::FindOverlaps()
538
srs569455d92612010-03-07 22:16:07 -0500539// Find partitions that are insane -- they start after they end or are too
540// big for the disk. (The latter should duplicate detection of overlaps
541// with GPT backup data structures, but better to err on the side of
542// redundant tests than to miss something....)
543int GPTData::FindInsanePartitions(void) {
544 uint32_t i;
545 int problems = 0;
546
srs56940283dae2010-04-28 16:44:34 -0400547 for (i = 0; i < numParts; i++) {
srs569455d92612010-03-07 22:16:07 -0500548 if (partitions[i].GetFirstLBA() > partitions[i].GetLastLBA()) {
549 problems++;
srs56940283dae2010-04-28 16:44:34 -0400550 cout << "\nProblem: partition " << i + 1 << " ends before it begins.\n";
srs569455d92612010-03-07 22:16:07 -0500551 } // if
552 if (partitions[i].GetLastLBA() >= diskSize) {
553 problems++;
srs56940873e9d2010-10-07 13:00:45 -0400554 cout << "\nProblem: partition " << i + 1 << " is too big for the disk.\n";
srs569455d92612010-03-07 22:16:07 -0500555 } // if
556 } // for
557 return problems;
558} // GPTData::FindInsanePartitions(void)
559
560
srs5694e4ac11e2009-08-31 10:13:04 -0400561/******************************************************************
562 * *
563 * Begin functions that load data from disk or save data to disk. *
564 * *
565 ******************************************************************/
566
567// Scan for partition data. This function loads the MBR data (regular MBR or
568// protective MBR) and loads BSD disklabel data (which is probably invalid).
569// It also looks for APM data, forces a load of GPT data, and summarizes
570// the results.
srs5694546a9c72010-01-26 16:00:26 -0500571void GPTData::PartitionScan(void) {
srs5694e4ac11e2009-08-31 10:13:04 -0400572 BSDData bsdDisklabel;
srs5694e4ac11e2009-08-31 10:13:04 -0400573
574 // Read the MBR & check for BSD disklabel
srs5694546a9c72010-01-26 16:00:26 -0500575 protectiveMBR.ReadMBRData(&myDisk);
576 bsdDisklabel.ReadBSDData(&myDisk, 0, diskSize - 1);
srs5694e4ac11e2009-08-31 10:13:04 -0400577
578 // Load the GPT data, whether or not it's valid
srs5694546a9c72010-01-26 16:00:26 -0500579 ForceLoadGPTData();
srs5694ba00fed2010-01-12 18:18:36 -0500580
581 if (!beQuiet) {
srs5694fed16d02010-01-27 23:03:40 -0500582 cout << "Partition table scan:\n";
srs5694ba00fed2010-01-12 18:18:36 -0500583 protectiveMBR.ShowState();
584 bsdDisklabel.ShowState();
585 ShowAPMState(); // Show whether there's an Apple Partition Map present
586 ShowGPTState(); // Show GPT status
srs5694fed16d02010-01-27 23:03:40 -0500587 cout << "\n";
srs5694ba00fed2010-01-12 18:18:36 -0500588 } // if
srs5694e4ac11e2009-08-31 10:13:04 -0400589
590 if (apmFound) {
srs5694fed16d02010-01-27 23:03:40 -0500591 cout << "\n*******************************************************************\n"
592 << "This disk appears to contain an Apple-format (APM) partition table!\n";
srs56945d58fe02010-01-03 20:57:08 -0500593 if (!justLooking) {
srs5694fed16d02010-01-27 23:03:40 -0500594 cout << "It will be destroyed if you continue!\n";
srs56945d58fe02010-01-03 20:57:08 -0500595 } // if
srs5694fed16d02010-01-27 23:03:40 -0500596 cout << "*******************************************************************\n\n\a";
srs5694e4ac11e2009-08-31 10:13:04 -0400597 } // if
srs5694e4ac11e2009-08-31 10:13:04 -0400598} // GPTData::PartitionScan()
599
600// Read GPT data from a disk.
srs56940a697312010-01-28 21:10:52 -0500601int GPTData::LoadPartitions(const string & deviceFilename) {
srs569408bb0da2010-02-19 17:19:55 -0500602 BSDData bsdDisklabel;
srs5694e321d442010-01-29 17:44:04 -0500603 int err, allOK = 1;
srs5694fed16d02010-01-27 23:03:40 -0500604 MBRValidity mbrState;
srs5694e4ac11e2009-08-31 10:13:04 -0400605
srs5694546a9c72010-01-26 16:00:26 -0500606 if (myDisk.OpenForRead(deviceFilename)) {
srs569455d92612010-03-07 22:16:07 -0500607 err = myDisk.OpenForWrite(deviceFilename);
608 if ((err == 0) && (!justLooking)) {
609 cout << "\aNOTE: Write test failed with error number " << errno
610 << ". It will be impossible to save\nchanges to this disk's partition table!\n";
611#if defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
612 cout << "You may be able to enable writes by exiting this program, typing\n"
613 << "'sysctl kern.geom.debugflags=16' at a shell prompt, and re-running this\n"
614 << "program.\n";
615#endif
616 cout << "\n";
617 } // if
618 myDisk.Close(); // Close and re-open read-only in case of bugs
619 } else allOK = 0; // if
620
621 if (allOK && myDisk.OpenForRead(deviceFilename)) {
srs5694e4ac11e2009-08-31 10:13:04 -0400622 // store disk information....
srs5694546a9c72010-01-26 16:00:26 -0500623 diskSize = myDisk.DiskSize(&err);
624 blockSize = (uint32_t) myDisk.GetBlockSize();
srs5694fed16d02010-01-27 23:03:40 -0500625 device = deviceFilename;
srs5694546a9c72010-01-26 16:00:26 -0500626 PartitionScan(); // Check for partition types, load GPT, & print summary
srs5694e4ac11e2009-08-31 10:13:04 -0400627
srs5694ba00fed2010-01-12 18:18:36 -0500628 whichWasUsed = UseWhichPartitions();
629 switch (whichWasUsed) {
srs5694e4ac11e2009-08-31 10:13:04 -0400630 case use_mbr:
631 XFormPartitions();
632 break;
633 case use_bsd:
srs5694546a9c72010-01-26 16:00:26 -0500634 bsdDisklabel.ReadBSDData(&myDisk, 0, diskSize - 1);
srs5694e4ac11e2009-08-31 10:13:04 -0400635// bsdDisklabel.DisplayBSDData();
636 ClearGPTData();
637 protectiveMBR.MakeProtectiveMBR(1); // clear boot area (option 1)
srs569408bb0da2010-02-19 17:19:55 -0500638 XFormDisklabel(&bsdDisklabel);
srs5694e4ac11e2009-08-31 10:13:04 -0400639 break;
640 case use_gpt:
srs5694fed16d02010-01-27 23:03:40 -0500641 mbrState = protectiveMBR.GetValidity();
642 if ((mbrState == invalid) || (mbrState == mbr))
643 protectiveMBR.MakeProtectiveMBR();
srs5694e4ac11e2009-08-31 10:13:04 -0400644 break;
645 case use_new:
646 ClearGPTData();
647 protectiveMBR.MakeProtectiveMBR();
648 break;
srs56943c0af382010-01-15 19:19:18 -0500649 case use_abort:
650 allOK = 0;
srs56949ddc14b2010-08-22 22:44:42 -0400651 cerr << "Invalid partition data!\n";
srs56943c0af382010-01-15 19:19:18 -0500652 break;
srs5694e4ac11e2009-08-31 10:13:04 -0400653 } // switch
654
srs569455d92612010-03-07 22:16:07 -0500655 if (allOK)
srs56943c0af382010-01-15 19:19:18 -0500656 CheckGPTSize();
srs569455d92612010-03-07 22:16:07 -0500657 myDisk.Close();
srs5694a8582cf2010-03-19 14:21:59 -0400658 ComputeAlignment();
srs5694e4ac11e2009-08-31 10:13:04 -0400659 } else {
660 allOK = 0;
srs5694e4ac11e2009-08-31 10:13:04 -0400661 } // if/else
662 return (allOK);
663} // GPTData::LoadPartitions()
664
665// Loads the GPT, as much as possible. Returns 1 if this seems to have
666// succeeded, 0 if there are obvious problems....
srs5694546a9c72010-01-26 16:00:26 -0500667int GPTData::ForceLoadGPTData(void) {
srs5694cb76c672010-02-11 22:22:22 -0500668 int allOK, validHeaders, loadedTable = 1;
srs5694e4ac11e2009-08-31 10:13:04 -0400669
srs5694cb76c672010-02-11 22:22:22 -0500670 allOK = LoadHeader(&mainHeader, myDisk, 1, &mainCrcOk);
srs5694e4ac11e2009-08-31 10:13:04 -0400671
srs5694cb76c672010-02-11 22:22:22 -0500672 if (mainCrcOk && (mainHeader.backupLBA < diskSize)) {
673 allOK = LoadHeader(&secondHeader, myDisk, mainHeader.backupLBA, &secondCrcOk) && allOK;
674 } else {
srs569408bb0da2010-02-19 17:19:55 -0500675 allOK = LoadHeader(&secondHeader, myDisk, diskSize - UINT64_C(1), &secondCrcOk) && allOK;
676 if (mainCrcOk && (mainHeader.backupLBA >= diskSize))
srs5694fed16d02010-01-27 23:03:40 -0500677 cout << "Warning! Disk size is smaller than the main header indicates! Loading\n"
678 << "secondary header from the last sector of the disk! You should use 'v' to\n"
679 << "verify disk integrity, and perhaps options on the experts' menu to repair\n"
680 << "the disk.\n";
srs5694cb76c672010-02-11 22:22:22 -0500681 } // if/else
682 if (!allOK)
srs5694e4ac11e2009-08-31 10:13:04 -0400683 state = gpt_invalid;
srs5694e4ac11e2009-08-31 10:13:04 -0400684
685 // Return valid headers code: 0 = both headers bad; 1 = main header
686 // good, backup bad; 2 = backup header good, main header bad;
687 // 3 = both headers good. Note these codes refer to valid GPT
688 // signatures and version numbers; more subtle problems will elude
689 // this check!
690 validHeaders = CheckHeaderValidity();
691
692 // Read partitions (from primary array)
693 if (validHeaders > 0) { // if at least one header is OK....
694 // GPT appears to be valid....
695 state = gpt_valid;
696
697 // We're calling the GPT valid, but there's a possibility that one
698 // of the two headers is corrupt. If so, use the one that seems to
699 // be in better shape to regenerate the bad one
srs5694546a9c72010-01-26 16:00:26 -0500700 if (validHeaders == 1) { // valid main header, invalid backup header
srs5694fed16d02010-01-27 23:03:40 -0500701 cerr << "\aCaution: invalid backup GPT header, but valid main header; regenerating\n"
702 << "backup header from main header.\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400703 RebuildSecondHeader();
srs5694546a9c72010-01-26 16:00:26 -0500704 state = gpt_corrupt;
srs5694e4ac11e2009-08-31 10:13:04 -0400705 secondCrcOk = mainCrcOk; // Since regenerated, use CRC validity of main
srs5694546a9c72010-01-26 16:00:26 -0500706 } else if (validHeaders == 2) { // valid backup header, invalid main header
srs5694fed16d02010-01-27 23:03:40 -0500707 cerr << "\aCaution: invalid main GPT header, but valid backup; regenerating main header\n"
708 << "from backup!\n\n";
srs5694546a9c72010-01-26 16:00:26 -0500709 RebuildMainHeader();
710 state = gpt_corrupt;
711 mainCrcOk = secondCrcOk; // Since copied, use CRC validity of backup
srs5694e4ac11e2009-08-31 10:13:04 -0400712 } // if/else/if
713
srs5694546a9c72010-01-26 16:00:26 -0500714 // Figure out which partition table to load....
715 // Load the main partition table, since either its header's CRC is OK or the
716 // backup header's CRC is not OK....
717 if (mainCrcOk || !secondCrcOk) {
718 if (LoadMainTable() == 0)
719 allOK = 0;
720 } else { // bad main header CRC and backup header CRC is OK
721 state = gpt_corrupt;
722 if (LoadSecondTableAsMain()) {
srs5694cb76c672010-02-11 22:22:22 -0500723 loadedTable = 2;
srs5694fed16d02010-01-27 23:03:40 -0500724 cerr << "\aWarning: Invalid CRC on main header data; loaded backup partition table.\n";
srs5694546a9c72010-01-26 16:00:26 -0500725 } else { // backup table bad, bad main header CRC, but try main table in desperation....
726 if (LoadMainTable() == 0) {
727 allOK = 0;
srs5694cb76c672010-02-11 22:22:22 -0500728 loadedTable = 0;
srs5694fed16d02010-01-27 23:03:40 -0500729 cerr << "\a\aWarning! Unable to load either main or backup partition table!\n";
srs5694546a9c72010-01-26 16:00:26 -0500730 } // if
731 } // if/else (LoadSecondTableAsMain())
732 } // if/else (load partition table)
srs5694e4ac11e2009-08-31 10:13:04 -0400733
srs5694cb76c672010-02-11 22:22:22 -0500734 if (loadedTable == 1)
735 secondPartsCrcOk = CheckTable(&secondHeader);
736 else if (loadedTable == 2)
737 mainPartsCrcOk = CheckTable(&mainHeader);
738 else
739 mainPartsCrcOk = secondPartsCrcOk = 0;
srs5694e4ac11e2009-08-31 10:13:04 -0400740
srs5694546a9c72010-01-26 16:00:26 -0500741 // Problem with main partition table; if backup is OK, use it instead....
742 if (secondPartsCrcOk && secondCrcOk && !mainPartsCrcOk) {
743 state = gpt_corrupt;
744 allOK = allOK && LoadSecondTableAsMain();
srs5694cb76c672010-02-11 22:22:22 -0500745 mainPartsCrcOk = 0; // LoadSecondTableAsMain() resets this, so re-flag as bad
srs5694fed16d02010-01-27 23:03:40 -0500746 cerr << "\aWarning! Main partition table CRC mismatch! Loaded backup "
747 << "partition table\ninstead of main partition table!\n\n";
srs5694cb76c672010-02-11 22:22:22 -0500748 } // if */
srs5694546a9c72010-01-26 16:00:26 -0500749
srs5694e4ac11e2009-08-31 10:13:04 -0400750 // Check for valid CRCs and warn if there are problems
751 if ((mainCrcOk == 0) || (secondCrcOk == 0) || (mainPartsCrcOk == 0) ||
752 (secondPartsCrcOk == 0)) {
srs5694fed16d02010-01-27 23:03:40 -0500753 cerr << "Warning! One or more CRCs don't match. You should repair the disk!\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400754 state = gpt_corrupt;
srs5694ba00fed2010-01-12 18:18:36 -0500755 } // if
srs5694e4ac11e2009-08-31 10:13:04 -0400756 } else {
757 state = gpt_invalid;
758 } // if/else
759 return allOK;
760} // GPTData::ForceLoadGPTData()
761
srs5694247657a2009-11-26 18:36:12 -0500762// Loads the partition table pointed to by the main GPT header. The
srs5694e4ac11e2009-08-31 10:13:04 -0400763// main GPT header in memory MUST be valid for this call to do anything
764// sensible!
srs5694546a9c72010-01-26 16:00:26 -0500765// Returns 1 on success, 0 on failure. CRC errors do NOT count as failure.
srs5694e4ac11e2009-08-31 10:13:04 -0400766int GPTData::LoadMainTable(void) {
srs5694cb76c672010-02-11 22:22:22 -0500767 return LoadPartitionTable(mainHeader, myDisk);
srs5694e4ac11e2009-08-31 10:13:04 -0400768} // GPTData::LoadMainTable()
srs5694e7b4ff92009-08-18 13:16:10 -0400769
770// Load the second (backup) partition table as the primary partition
srs5694546a9c72010-01-26 16:00:26 -0500771// table. Used in repair functions, and when starting up if the main
772// partition table is damaged.
773// Returns 1 on success, 0 on failure. CRC errors do NOT count as failure.
774int GPTData::LoadSecondTableAsMain(void) {
srs5694cb76c672010-02-11 22:22:22 -0500775 return LoadPartitionTable(secondHeader, myDisk);
776} // GPTData::LoadSecondTableAsMain()
srs5694e7b4ff92009-08-18 13:16:10 -0400777
srs5694cb76c672010-02-11 22:22:22 -0500778// Load a single GPT header (main or backup) from the specified disk device and
779// sector. Applies byte-order corrections on big-endian platforms. Sets crcOk
780// value appropriately.
781// Returns 1 on success, 0 on failure. Note that CRC errors do NOT qualify as
782// failure.
783int GPTData::LoadHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector, int *crcOk) {
784 int allOK = 1;
srs56941c6f8b02010-02-21 11:09:20 -0500785 GPTHeader tempHeader;
srs5694cb76c672010-02-11 22:22:22 -0500786
787 disk.Seek(sector);
srs56941c6f8b02010-02-21 11:09:20 -0500788 if (disk.Read(&tempHeader, 512) != 512) {
srs5694cb76c672010-02-11 22:22:22 -0500789 cerr << "Warning! Read error " << errno << "; strange behavior now likely!\n";
790 allOK = 0;
791 } // if
srs56941c6f8b02010-02-21 11:09:20 -0500792 *crcOk = CheckHeaderCRC(&tempHeader);
srs5694cb76c672010-02-11 22:22:22 -0500793
srs56941c6f8b02010-02-21 11:09:20 -0500794 // Reverse byte order, if necessary
srs5694cb76c672010-02-11 22:22:22 -0500795 if (IsLittleEndian() == 0) {
srs569455d92612010-03-07 22:16:07 -0500796 ReverseHeaderBytes(&tempHeader);
srs5694cb76c672010-02-11 22:22:22 -0500797 } // if
srs56941c6f8b02010-02-21 11:09:20 -0500798
srs56940283dae2010-04-28 16:44:34 -0400799 if (allOK && (numParts != tempHeader.numParts) && *crcOk) {
srs56941c6f8b02010-02-21 11:09:20 -0500800 allOK = SetGPTSize(tempHeader.numParts);
srs569455d92612010-03-07 22:16:07 -0500801 }
srs56941c6f8b02010-02-21 11:09:20 -0500802
803 *header = tempHeader;
srs5694cb76c672010-02-11 22:22:22 -0500804 return allOK;
805} // GPTData::LoadHeader
806
807// Load a partition table (either main or secondary) from the specified disk,
808// using header as a reference for what to load. If sector != 0 (the default
809// is 0), loads from the specified sector; otherwise loads from the sector
810// indicated in header.
811// Returns 1 on success, 0 on failure. CRC errors do NOT count as failure.
812int GPTData::LoadPartitionTable(const struct GPTHeader & header, DiskIO & disk, uint64_t sector) {
813 uint32_t sizeOfParts, newCRC;
814 int retval;
815
816 if (disk.OpenForRead()) {
817 if (sector == 0) {
818 retval = disk.Seek(header.partitionEntriesLBA);
819 } else {
820 retval = disk.Seek(sector);
821 } // if/else
srs569455d92612010-03-07 22:16:07 -0500822 if (retval == 1)
823 retval = SetGPTSize(header.numParts);
srs5694546a9c72010-01-26 16:00:26 -0500824 if (retval == 1) {
srs5694cb76c672010-02-11 22:22:22 -0500825 sizeOfParts = header.numParts * header.sizeOfPartitionEntries;
826 if (disk.Read(partitions, sizeOfParts) != (int) sizeOfParts) {
srs5694fed16d02010-01-27 23:03:40 -0500827 cerr << "Warning! Read error " << errno << "! Misbehavior now likely!\n";
srs5694546a9c72010-01-26 16:00:26 -0500828 retval = 0;
srs56945d58fe02010-01-03 20:57:08 -0500829 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400830 newCRC = chksum_crc32((unsigned char*) partitions, sizeOfParts);
srs5694cb76c672010-02-11 22:22:22 -0500831 mainPartsCrcOk = secondPartsCrcOk = (newCRC == header.partitionEntriesCRC);
srs56942a9f5da2009-08-26 00:48:01 -0400832 if (IsLittleEndian() == 0)
833 ReversePartitionBytes();
srs5694cb76c672010-02-11 22:22:22 -0500834 if (!mainPartsCrcOk) {
835 cout << "Caution! After loading partitions, the CRC doesn't check out!\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400836 } // if
837 } else {
srs5694cb76c672010-02-11 22:22:22 -0500838 cerr << "Error! Couldn't seek to partition table!\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400839 } // if/else
840 } else {
srs5694fed16d02010-01-27 23:03:40 -0500841 cerr << "Error! Couldn't open device " << device
srs5694cb76c672010-02-11 22:22:22 -0500842 << " when reading partition table!\n";
srs5694546a9c72010-01-26 16:00:26 -0500843 retval = 0;
srs5694e7b4ff92009-08-18 13:16:10 -0400844 } // if/else
srs5694546a9c72010-01-26 16:00:26 -0500845 return retval;
srs5694cb76c672010-02-11 22:22:22 -0500846} // GPTData::LoadPartitionsTable()
847
848// Check the partition table pointed to by header, but don't keep it
849// around.
850// Returns 1 if the CRC is OK, 0 if not or if there was a read error.
851int GPTData::CheckTable(struct GPTHeader *header) {
852 uint32_t sizeOfParts, newCRC;
853 uint8_t *storage;
854 int newCrcOk = 0;
855
srs56940283dae2010-04-28 16:44:34 -0400856 // Load partition table into temporary storage to check
srs5694cb76c672010-02-11 22:22:22 -0500857 // its CRC and store the results, then discard this temporary
858 // storage, since we don't use it in any but recovery operations
859 if (myDisk.Seek(header->partitionEntriesLBA)) {
srs56940283dae2010-04-28 16:44:34 -0400860 sizeOfParts = header->numParts * header->sizeOfPartitionEntries;
srs5694cb76c672010-02-11 22:22:22 -0500861 storage = new uint8_t[sizeOfParts];
862 if (myDisk.Read(storage, sizeOfParts) != (int) sizeOfParts) {
srs56940283dae2010-04-28 16:44:34 -0400863 cerr << "Warning! Error " << errno << " reading partition table for CRC check!\n";
srs5694cb76c672010-02-11 22:22:22 -0500864 } else {
865 newCRC = chksum_crc32((unsigned char*) storage, sizeOfParts);
866 newCrcOk = (newCRC == header->partitionEntriesCRC);
867 } // if/else
868 delete[] storage;
869 } // if
870 return newCrcOk;
871} // GPTData::CheckTable()
srs5694e7b4ff92009-08-18 13:16:10 -0400872
srs5694e7b4ff92009-08-18 13:16:10 -0400873// Writes GPT (and protective MBR) to disk. Returns 1 on successful
874// write, 0 if there was a problem.
srs5694f9312b02010-07-06 15:39:51 -0400875int GPTData::SaveGPTData(int quiet, string filename) {
srs56946699b012010-02-04 00:55:30 -0500876 int allOK = 1, littleEndian;
srs5694e321d442010-01-29 17:44:04 -0500877 char answer;
srs5694e7b4ff92009-08-18 13:16:10 -0400878
srs56946699b012010-02-04 00:55:30 -0500879 littleEndian = IsLittleEndian();
880
srs56940873e9d2010-10-07 13:00:45 -0400881 if (filename == "")
882 filename = device;
srs5694f9312b02010-07-06 15:39:51 -0400883 if (filename == "") {
srs5694fed16d02010-01-27 23:03:40 -0500884 cerr << "Device not defined.\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400885 } // if
886
887 // First do some final sanity checks....
srs56945d58fe02010-01-03 20:57:08 -0500888
889 // This test should only fail on read-only disks....
890 if (justLooking) {
srs5694fed16d02010-01-27 23:03:40 -0500891 cout << "The justLooking flag is set. This probably means you can't write to the disk.\n";
srs56945d58fe02010-01-03 20:57:08 -0500892 allOK = 0;
893 } // if
894
srs5694e7b4ff92009-08-18 13:16:10 -0400895 // Is there enough space to hold the GPT headers and partition tables,
896 // given the partition sizes?
srs5694221e0872009-08-29 15:00:31 -0400897 if (CheckGPTSize() > 0) {
srs5694e7b4ff92009-08-18 13:16:10 -0400898 allOK = 0;
899 } // if
900
901 // Check that disk is really big enough to handle this...
902 if (mainHeader.backupLBA > diskSize) {
srs5694fed16d02010-01-27 23:03:40 -0500903 cerr << "Error! Disk is too small! The 'e' option on the experts' menu might fix the\n"
904 << "problem (or it might not). Aborting!\n(Disk size is "
905 << diskSize << " sectors, needs to be " << mainHeader.backupLBA << " sectors.)\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400906 allOK = 0;
907 } // if
srs5694247657a2009-11-26 18:36:12 -0500908 // Check that second header is properly placed. Warn and ask if this should
909 // be corrected if the test fails....
srs5694ba00fed2010-01-12 18:18:36 -0500910 if ((mainHeader.backupLBA < (diskSize - UINT64_C(1))) && (quiet == 0)) {
srs5694fed16d02010-01-27 23:03:40 -0500911 cout << "Warning! Secondary header is placed too early on the disk! Do you want to\n"
912 << "correct this problem? ";
srs5694247657a2009-11-26 18:36:12 -0500913 if (GetYN() == 'Y') {
914 MoveSecondHeaderToEnd();
srs5694fed16d02010-01-27 23:03:40 -0500915 cout << "Have moved second header and partition table to correct location.\n";
srs5694247657a2009-11-26 18:36:12 -0500916 } else {
srs5694fed16d02010-01-27 23:03:40 -0500917 cout << "Have not corrected the problem. Strange problems may occur in the future!\n";
srs5694247657a2009-11-26 18:36:12 -0500918 } // if correction requested
919 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400920
srs569455d92612010-03-07 22:16:07 -0500921 // Check for overlapping or insane partitions....
922 if ((FindOverlaps() > 0) || (FindInsanePartitions() > 0)) {
srs5694e4ac11e2009-08-31 10:13:04 -0400923 allOK = 0;
srs5694fed16d02010-01-27 23:03:40 -0500924 cerr << "Aborting write operation!\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400925 } // if
926
927 // Check for mismatched MBR and GPT data, but let it pass if found
928 // (function displays warning message)
929 FindHybridMismatches();
srs5694e7b4ff92009-08-18 13:16:10 -0400930
931 RecomputeCRCs();
932
srs5694ba00fed2010-01-12 18:18:36 -0500933 if ((allOK) && (!quiet)) {
srs5694fed16d02010-01-27 23:03:40 -0500934 cout << "\nFinal checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING\n"
935 << "PARTITIONS!!\n\nDo you want to proceed, possibly destroying your data? ";
srs56945d58fe02010-01-03 20:57:08 -0500936 answer = GetYN();
937 if (answer == 'Y') {
srs5694fed16d02010-01-27 23:03:40 -0500938 cout << "OK; writing new GUID partition table (GPT).\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400939 } else {
940 allOK = 0;
941 } // if/else
942 } // if
943
944 // Do it!
945 if (allOK) {
srs5694f9312b02010-07-06 15:39:51 -0400946 if (myDisk.OpenForWrite(filename)) {
srs56948a4ddfc2010-03-21 19:05:49 -0400947 // As per UEFI specs, write the secondary table and GPT first....
srs5694cb76c672010-02-11 22:22:22 -0500948 allOK = SavePartitionTable(myDisk, secondHeader.partitionEntriesLBA);
949 if (!allOK)
950 cerr << "Unable to save backup partition table! Perhaps the 'e' option on the experts'\n"
951 << "menu will resolve this problem.\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400952
953 // Now write the secondary GPT header...
srs56948a4ddfc2010-03-21 19:05:49 -0400954 allOK = allOK && SaveHeader(&secondHeader, myDisk, mainHeader.backupLBA);
955
956 // Now write the main partition tables...
957 allOK = allOK && SavePartitionTable(myDisk, mainHeader.partitionEntriesLBA);
958
959 // Now write the main GPT header...
960 allOK = allOK && SaveHeader(&mainHeader, myDisk, 1);
961
962 // To top it off, write the protective MBR...
963 allOK = allOK && protectiveMBR.WriteMBRData(&myDisk);
srs5694e7b4ff92009-08-18 13:16:10 -0400964
965 // re-read the partition table
966 if (allOK) {
srs5694546a9c72010-01-26 16:00:26 -0500967 myDisk.DiskSync();
srs5694e7b4ff92009-08-18 13:16:10 -0400968 } // if
969
970 if (allOK) { // writes completed OK
srs5694fed16d02010-01-27 23:03:40 -0500971 cout << "The operation has completed successfully.\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400972 } else {
srs5694fed16d02010-01-27 23:03:40 -0500973 cerr << "Warning! An error was reported when writing the partition table! This error\n"
srs56948a4ddfc2010-03-21 19:05:49 -0400974 << "MIGHT be harmless, but you may have trashed the disk!\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400975 } // if/else
srs56948a4ddfc2010-03-21 19:05:49 -0400976
srs5694546a9c72010-01-26 16:00:26 -0500977 myDisk.Close();
srs5694e7b4ff92009-08-18 13:16:10 -0400978 } else {
srs5694f9312b02010-07-06 15:39:51 -0400979 cerr << "Unable to open device " << filename << " for writing! Errno is "
srs5694fed16d02010-01-27 23:03:40 -0500980 << errno << "! Aborting write!\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400981 allOK = 0;
srs5694e7b4ff92009-08-18 13:16:10 -0400982 } // if/else
983 } else {
srs5694fed16d02010-01-27 23:03:40 -0500984 cout << "Aborting write of new partition table.\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400985 } // if
986
987 return (allOK);
988} // GPTData::SaveGPTData()
989
990// Save GPT data to a backup file. This function does much less error
991// checking than SaveGPTData(). It can therefore preserve many types of
992// corruption for later analysis; however, it preserves only the MBR,
993// the main GPT header, the backup GPT header, and the main partition
994// table; it discards the backup partition table, since it should be
995// identical to the main partition table on healthy disks.
srs56940a697312010-01-28 21:10:52 -0500996int GPTData::SaveGPTBackup(const string & filename) {
997 int allOK = 1;
srs5694546a9c72010-01-26 16:00:26 -0500998 DiskIO backupFile;
srs5694e7b4ff92009-08-18 13:16:10 -0400999
srs5694546a9c72010-01-26 16:00:26 -05001000 if (backupFile.OpenForWrite(filename)) {
srs56946699b012010-02-04 00:55:30 -05001001 // Recomputing the CRCs is likely to alter them, which could be bad
1002 // if the intent is to save a potentially bad GPT for later analysis;
1003 // but if we don't do this, we get bogus errors when we load the
1004 // backup. I'm favoring misses over false alarms....
1005 RecomputeCRCs();
1006
srs5694546a9c72010-01-26 16:00:26 -05001007 protectiveMBR.WriteMBRData(&backupFile);
srs5694e7b4ff92009-08-18 13:16:10 -04001008
srs5694cb76c672010-02-11 22:22:22 -05001009 if (allOK) {
srs5694546a9c72010-01-26 16:00:26 -05001010 // MBR write closed disk, so re-open and seek to end....
1011 backupFile.OpenForWrite();
srs5694cb76c672010-02-11 22:22:22 -05001012 allOK = SaveHeader(&mainHeader, backupFile, 1);
1013 } // if (allOK)
srs5694e7b4ff92009-08-18 13:16:10 -04001014
srs5694e7b4ff92009-08-18 13:16:10 -04001015 if (allOK)
srs5694cb76c672010-02-11 22:22:22 -05001016 allOK = SaveHeader(&secondHeader, backupFile, 2);
srs5694e7b4ff92009-08-18 13:16:10 -04001017
srs5694cb76c672010-02-11 22:22:22 -05001018 if (allOK)
1019 allOK = SavePartitionTable(backupFile, 3);
srs5694e7b4ff92009-08-18 13:16:10 -04001020
1021 if (allOK) { // writes completed OK
srs5694fed16d02010-01-27 23:03:40 -05001022 cout << "The operation has completed successfully.\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001023 } else {
srs5694fed16d02010-01-27 23:03:40 -05001024 cerr << "Warning! An error was reported when writing the backup file.\n"
1025 << "It may not be usable!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001026 } // if/else
srs5694546a9c72010-01-26 16:00:26 -05001027 backupFile.Close();
srs5694e7b4ff92009-08-18 13:16:10 -04001028 } else {
srs5694fed16d02010-01-27 23:03:40 -05001029 cerr << "Unable to open file " << filename << " for writing! Aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001030 allOK = 0;
1031 } // if/else
1032 return allOK;
1033} // GPTData::SaveGPTBackup()
1034
srs5694cb76c672010-02-11 22:22:22 -05001035// Write a GPT header (main or backup) to the specified sector. Used by both
1036// the SaveGPTData() and SaveGPTBackup() functions.
1037// Should be passed an architecture-appropriate header (DO NOT call
1038// ReverseHeaderBytes() on the header before calling this function)
1039// Returns 1 on success, 0 on failure
1040int GPTData::SaveHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector) {
1041 int littleEndian, allOK = 1;
1042
1043 littleEndian = IsLittleEndian();
1044 if (!littleEndian)
1045 ReverseHeaderBytes(header);
1046 if (disk.Seek(sector)) {
1047 if (disk.Write(header, 512) == -1)
1048 allOK = 0;
1049 } else allOK = 0; // if (disk.Seek()...)
1050 if (!littleEndian)
1051 ReverseHeaderBytes(header);
1052 return allOK;
1053} // GPTData::SaveHeader()
1054
1055// Save the partitions to the specified sector. Used by both the SaveGPTData()
1056// and SaveGPTBackup() functions.
1057// Should be passed an architecture-appropriate header (DO NOT call
1058// ReverseHeaderBytes() on the header before calling this function)
1059// Returns 1 on success, 0 on failure
1060int GPTData::SavePartitionTable(DiskIO & disk, uint64_t sector) {
1061 int littleEndian, allOK = 1;
1062
1063 littleEndian = IsLittleEndian();
1064 if (disk.Seek(sector)) {
1065 if (!littleEndian)
1066 ReversePartitionBytes();
srs56940283dae2010-04-28 16:44:34 -04001067 if (disk.Write(partitions, mainHeader.sizeOfPartitionEntries * numParts) == -1)
srs5694cb76c672010-02-11 22:22:22 -05001068 allOK = 0;
1069 if (!littleEndian)
1070 ReversePartitionBytes();
1071 } else allOK = 0; // if (myDisk.Seek()...)
1072 return allOK;
1073} // GPTData::SavePartitionTable()
1074
srs5694e7b4ff92009-08-18 13:16:10 -04001075// Load GPT data from a backup file created by SaveGPTBackup(). This function
1076// does minimal error checking. It returns 1 if it completed successfully,
1077// 0 if there was a problem. In the latter case, it creates a new empty
1078// set of partitions.
srs56940a697312010-01-28 21:10:52 -05001079int GPTData::LoadGPTBackup(const string & filename) {
srs5694cb76c672010-02-11 22:22:22 -05001080 int allOK = 1, val, err;
srs56940283dae2010-04-28 16:44:34 -04001081 uint32_t sizeOfEntries;
srs5694cb76c672010-02-11 22:22:22 -05001082 int littleEndian = 1, shortBackup = 0;
srs5694546a9c72010-01-26 16:00:26 -05001083 DiskIO backupFile;
srs5694e7b4ff92009-08-18 13:16:10 -04001084
srs5694546a9c72010-01-26 16:00:26 -05001085 if (backupFile.OpenForRead(filename)) {
srs56942a9f5da2009-08-26 00:48:01 -04001086 if (IsLittleEndian() == 0)
1087 littleEndian = 0;
1088
srs5694e7b4ff92009-08-18 13:16:10 -04001089 // Let the MBRData class load the saved MBR...
srs5694546a9c72010-01-26 16:00:26 -05001090 protectiveMBR.ReadMBRData(&backupFile, 0); // 0 = don't check block size
srs5694e7b4ff92009-08-18 13:16:10 -04001091
srs5694cb76c672010-02-11 22:22:22 -05001092 LoadHeader(&mainHeader, backupFile, 1, &mainCrcOk);
srs5694e7b4ff92009-08-18 13:16:10 -04001093
srs5694cb76c672010-02-11 22:22:22 -05001094 // Check backup file size and rebuild second header if file is right
1095 // size to be direct dd copy of MBR, main header, and main partition
1096 // table; if other size, treat it like a GPT fdisk-generated backup
1097 // file
1098 shortBackup = ((backupFile.DiskSize(&err) * backupFile.GetBlockSize()) ==
1099 (mainHeader.numParts * mainHeader.sizeOfPartitionEntries) + 1024);
1100 if (shortBackup) {
1101 RebuildSecondHeader();
1102 secondCrcOk = mainCrcOk;
1103 } else {
1104 LoadHeader(&secondHeader, backupFile, 2, &secondCrcOk);
1105 } // if/else
srs56942a9f5da2009-08-26 00:48:01 -04001106
srs5694e7b4ff92009-08-18 13:16:10 -04001107 // Return valid headers code: 0 = both headers bad; 1 = main header
1108 // good, backup bad; 2 = backup header good, main header bad;
1109 // 3 = both headers good. Note these codes refer to valid GPT
1110 // signatures and version numbers; more subtle problems will elude
1111 // this check!
1112 if ((val = CheckHeaderValidity()) > 0) {
1113 if (val == 2) { // only backup header seems to be good
srs56940283dae2010-04-28 16:44:34 -04001114 SetGPTSize(secondHeader.numParts);
srs5694e4ac11e2009-08-31 10:13:04 -04001115 sizeOfEntries = secondHeader.sizeOfPartitionEntries;
srs5694e7b4ff92009-08-18 13:16:10 -04001116 } else { // main header is OK
srs56940283dae2010-04-28 16:44:34 -04001117 SetGPTSize(mainHeader.numParts);
srs5694e7b4ff92009-08-18 13:16:10 -04001118 sizeOfEntries = mainHeader.sizeOfPartitionEntries;
1119 } // if/else
1120
srs5694e7b4ff92009-08-18 13:16:10 -04001121 if (secondHeader.currentLBA != diskSize - UINT64_C(1)) {
srs5694fed16d02010-01-27 23:03:40 -05001122 cout << "Warning! Current disk size doesn't match that of the backup!\n"
1123 << "Adjusting sizes to match, but subsequent problems are possible!\n";
srs5694247657a2009-11-26 18:36:12 -05001124 MoveSecondHeaderToEnd();
srs5694e7b4ff92009-08-18 13:16:10 -04001125 } // if
1126
srs5694cb76c672010-02-11 22:22:22 -05001127 if (!LoadPartitionTable(mainHeader, backupFile, (uint64_t) (3 - shortBackup)))
1128 cerr << "Warning! Read error " << errno
1129 << " loading partition table; strange behavior now likely!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001130 } else {
1131 allOK = 0;
1132 } // if/else
srs5694a8582cf2010-03-19 14:21:59 -04001133 // Something went badly wrong, so blank out partitions
1134 if (allOK == 0) {
1135 cerr << "Improper backup file! Clearing all partition data!\n";
1136 ClearGPTData();
1137 protectiveMBR.MakeProtectiveMBR();
1138 } // if
srs5694e7b4ff92009-08-18 13:16:10 -04001139 } else {
1140 allOK = 0;
srs5694fed16d02010-01-27 23:03:40 -05001141 cerr << "Unable to open file " << filename << " for reading! Aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001142 } // if/else
1143
srs5694e7b4ff92009-08-18 13:16:10 -04001144 return allOK;
1145} // GPTData::LoadGPTBackup()
1146
srs569408bb0da2010-02-19 17:19:55 -05001147int GPTData::SaveMBR(void) {
srs569455d92612010-03-07 22:16:07 -05001148 return protectiveMBR.WriteMBRData(&myDisk);
srs569408bb0da2010-02-19 17:19:55 -05001149} // GPTData::SaveMBR()
1150
1151// This function destroys the on-disk GPT structures, but NOT the on-disk
1152// MBR.
1153// Returns 1 if the operation succeeds, 0 if not.
1154int GPTData::DestroyGPT(void) {
1155 int i, sum, tableSize, allOK = 1;
1156 uint8_t blankSector[512];
1157 uint8_t* emptyTable;
1158
1159 for (i = 0; i < 512; i++) {
1160 blankSector[i] = 0;
1161 } // for
1162
1163 if (myDisk.OpenForWrite()) {
1164 if (!myDisk.Seek(mainHeader.currentLBA))
1165 allOK = 0;
1166 if (myDisk.Write(blankSector, 512) != 512) { // blank it out
1167 cerr << "Warning! GPT main header not overwritten! Error is " << errno << "\n";
1168 allOK = 0;
1169 } // if
1170 if (!myDisk.Seek(mainHeader.partitionEntriesLBA))
1171 allOK = 0;
srs56940283dae2010-04-28 16:44:34 -04001172 tableSize = numParts * mainHeader.sizeOfPartitionEntries;
srs569408bb0da2010-02-19 17:19:55 -05001173 emptyTable = new uint8_t[tableSize];
1174 for (i = 0; i < tableSize; i++)
1175 emptyTable[i] = 0;
1176 if (allOK) {
1177 sum = myDisk.Write(emptyTable, tableSize);
1178 if (sum != tableSize) {
1179 cerr << "Warning! GPT main partition table not overwritten! Error is " << errno << "\n";
1180 allOK = 0;
1181 } // if write failed
1182 } // if
1183 if (!myDisk.Seek(secondHeader.partitionEntriesLBA))
1184 allOK = 0;
1185 if (allOK) {
1186 sum = myDisk.Write(emptyTable, tableSize);
1187 if (sum != tableSize) {
1188 cerr << "Warning! GPT backup partition table not overwritten! Error is "
1189 << errno << "\n";
1190 allOK = 0;
1191 } // if wrong size written
1192 } // if
1193 if (!myDisk.Seek(secondHeader.currentLBA))
1194 allOK = 0;
1195 if (allOK) {
1196 if (myDisk.Write(blankSector, 512) != 512) { // blank it out
1197 cerr << "Warning! GPT backup header not overwritten! Error is " << errno << "\n";
1198 allOK = 0;
1199 } // if
1200 } // if
1201 myDisk.DiskSync();
1202 myDisk.Close();
1203 cout << "GPT data structures destroyed! You may now partition the disk using fdisk or\n"
1204 << "other utilities.\n";
1205 delete[] emptyTable;
1206 } else {
1207 cerr << "Problem opening " << device << " for writing! Program will now terminate.\n";
1208 } // if/else (fd != -1)
1209 return (allOK);
1210} // GPTDataTextUI::DestroyGPT()
1211
1212// Wipe MBR data from the disk (zero it out completely)
1213// Returns 1 on success, 0 on failure.
1214int GPTData::DestroyMBR(void) {
1215 int allOK = 1, i;
1216 uint8_t blankSector[512];
1217
1218 for (i = 0; i < 512; i++)
1219 blankSector[i] = 0;
1220
1221 if (myDisk.OpenForWrite()) {
1222 if (myDisk.Seek(0)) {
1223 if (myDisk.Write(blankSector, 512) != 512)
1224 allOK = 0;
1225 } else allOK = 0;
1226 } else allOK = 0;
1227 if (!allOK)
1228 cerr << "Warning! MBR not overwritten! Error is " << errno << "!\n";
1229 return allOK;
1230} // GPTData::DestroyMBR(void)
1231
srs5694e4ac11e2009-08-31 10:13:04 -04001232// Tell user whether Apple Partition Map (APM) was discovered....
1233void GPTData::ShowAPMState(void) {
1234 if (apmFound)
srs5694fed16d02010-01-27 23:03:40 -05001235 cout << " APM: present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001236 else
srs5694fed16d02010-01-27 23:03:40 -05001237 cout << " APM: not present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001238} // GPTData::ShowAPMState()
1239
1240// Tell user about the state of the GPT data....
1241void GPTData::ShowGPTState(void) {
1242 switch (state) {
1243 case gpt_invalid:
srs5694fed16d02010-01-27 23:03:40 -05001244 cout << " GPT: not present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001245 break;
1246 case gpt_valid:
srs5694fed16d02010-01-27 23:03:40 -05001247 cout << " GPT: present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001248 break;
1249 case gpt_corrupt:
srs5694fed16d02010-01-27 23:03:40 -05001250 cout << " GPT: damaged\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001251 break;
1252 default:
srs5694fed16d02010-01-27 23:03:40 -05001253 cout << "\a GPT: unknown -- bug!\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001254 break;
1255 } // switch
1256} // GPTData::ShowGPTState()
1257
1258// Display the basic GPT data
1259void GPTData::DisplayGPTData(void) {
srs5694e321d442010-01-29 17:44:04 -05001260 uint32_t i;
srs5694e4ac11e2009-08-31 10:13:04 -04001261 uint64_t temp, totalFree;
1262
srs5694fed16d02010-01-27 23:03:40 -05001263 cout << "Disk " << device << ": " << diskSize << " sectors, "
srs56940873e9d2010-10-07 13:00:45 -04001264 << BytesToSI(diskSize, blockSize) << "\n";
srs5694fed16d02010-01-27 23:03:40 -05001265 cout << "Logical sector size: " << blockSize << " bytes\n";
srs56945a081752010-09-24 20:39:41 -04001266 cout << "Disk identifier (GUID): " << mainHeader.diskGUID << "\n";
srs56940283dae2010-04-28 16:44:34 -04001267 cout << "Partition table holds up to " << numParts << " entries\n";
srs5694fed16d02010-01-27 23:03:40 -05001268 cout << "First usable sector is " << mainHeader.firstUsableLBA
1269 << ", last usable sector is " << mainHeader.lastUsableLBA << "\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001270 totalFree = FindFreeBlocks(&i, &temp);
srs56948a4ddfc2010-03-21 19:05:49 -04001271 cout << "Partitions will be aligned on " << sectorAlignment << "-sector boundaries\n";
srs5694fed16d02010-01-27 23:03:40 -05001272 cout << "Total free space is " << totalFree << " sectors ("
srs56940873e9d2010-10-07 13:00:45 -04001273 << BytesToSI(totalFree, blockSize) << ")\n";
srs5694fed16d02010-01-27 23:03:40 -05001274 cout << "\nNumber Start (sector) End (sector) Size Code Name\n";
srs56940283dae2010-04-28 16:44:34 -04001275 for (i = 0; i < numParts; i++) {
srs5694978041c2009-09-21 20:51:47 -04001276 partitions[i].ShowSummary(i, blockSize);
srs5694e4ac11e2009-08-31 10:13:04 -04001277 } // for
1278} // GPTData::DisplayGPTData()
1279
srs5694e4ac11e2009-08-31 10:13:04 -04001280// Show detailed information on the specified partition
1281void GPTData::ShowPartDetails(uint32_t partNum) {
srs56940873e9d2010-10-07 13:00:45 -04001282 if (!IsFreePartNum(partNum)) {
srs5694e4ac11e2009-08-31 10:13:04 -04001283 partitions[partNum].ShowDetails(blockSize);
1284 } else {
srs5694fed16d02010-01-27 23:03:40 -05001285 cout << "Partition #" << partNum + 1 << " does not exist.";
srs5694e4ac11e2009-08-31 10:13:04 -04001286 } // if
1287} // GPTData::ShowPartDetails()
1288
srs5694e4ac11e2009-08-31 10:13:04 -04001289/**************************************************************************
1290 * *
1291 * Partition table transformation functions (MBR or BSD disklabel to GPT) *
1292 * (some of these functions may require user interaction) *
1293 * *
1294 **************************************************************************/
1295
srs569408bb0da2010-02-19 17:19:55 -05001296// Examines the MBR & GPT data to determine which set of data to use: the
1297// MBR (use_mbr), the GPT (use_gpt), the BSD disklabel (use_bsd), or create
1298// a new set of partitions (use_new). A return value of use_abort indicates
1299// that this function couldn't determine what to do. Overriding functions
1300// in derived classes may ask users questions in such cases.
srs5694e4ac11e2009-08-31 10:13:04 -04001301WhichToUse GPTData::UseWhichPartitions(void) {
1302 WhichToUse which = use_new;
1303 MBRValidity mbrState;
srs5694e4ac11e2009-08-31 10:13:04 -04001304
1305 mbrState = protectiveMBR.GetValidity();
1306
1307 if ((state == gpt_invalid) && ((mbrState == mbr) || (mbrState == hybrid))) {
srs5694fed16d02010-01-27 23:03:40 -05001308 cout << "\n***************************************************************\n"
1309 << "Found invalid GPT and valid MBR; converting MBR to GPT format.\n";
srs56945d58fe02010-01-03 20:57:08 -05001310 if (!justLooking) {
srs56940283dae2010-04-28 16:44:34 -04001311 cout << "\aTHIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by typing 'q' if\n"
srs5694fed16d02010-01-27 23:03:40 -05001312 << "you don't want to convert your MBR partitions to GPT format!\n";
srs56945d58fe02010-01-03 20:57:08 -05001313 } // if
srs5694fed16d02010-01-27 23:03:40 -05001314 cout << "***************************************************************\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001315 which = use_mbr;
1316 } // if
1317
1318 if ((state == gpt_invalid) && bsdFound) {
srs5694fed16d02010-01-27 23:03:40 -05001319 cout << "\n**********************************************************************\n"
1320 << "Found invalid GPT and valid BSD disklabel; converting BSD disklabel\n"
1321 << "to GPT format.";
srs56940a697312010-01-28 21:10:52 -05001322 if ((!justLooking) && (!beQuiet)) {
srs56940283dae2010-04-28 16:44:34 -04001323 cout << "\a THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Your first\n"
srs5694fed16d02010-01-27 23:03:40 -05001324 << "BSD partition will likely be unusable. Exit by typing 'q' if you don't\n"
1325 << "want to convert your BSD partitions to GPT format!";
srs56945d58fe02010-01-03 20:57:08 -05001326 } // if
srs5694fed16d02010-01-27 23:03:40 -05001327 cout << "\n**********************************************************************\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001328 which = use_bsd;
1329 } // if
1330
1331 if ((state == gpt_valid) && (mbrState == gpt)) {
srs5694e4ac11e2009-08-31 10:13:04 -04001332 which = use_gpt;
srs56943c0af382010-01-15 19:19:18 -05001333 if (!beQuiet)
srs5694fed16d02010-01-27 23:03:40 -05001334 cout << "Found valid GPT with protective MBR; using GPT.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001335 } // if
1336 if ((state == gpt_valid) && (mbrState == hybrid)) {
srs5694e4ac11e2009-08-31 10:13:04 -04001337 which = use_gpt;
srs56943c0af382010-01-15 19:19:18 -05001338 if (!beQuiet)
srs5694fed16d02010-01-27 23:03:40 -05001339 cout << "Found valid GPT with hybrid MBR; using GPT.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001340 } // if
1341 if ((state == gpt_valid) && (mbrState == invalid)) {
srs56940a697312010-01-28 21:10:52 -05001342 cout << "\aFound valid GPT with corrupt MBR; using GPT and will write new\n"
srs5694fed16d02010-01-27 23:03:40 -05001343 << "protective MBR on save.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001344 which = use_gpt;
srs5694e4ac11e2009-08-31 10:13:04 -04001345 } // if
1346 if ((state == gpt_valid) && (mbrState == mbr)) {
srs569408bb0da2010-02-19 17:19:55 -05001347 which = use_abort;
srs5694e4ac11e2009-08-31 10:13:04 -04001348 } // if
1349
srs5694e4ac11e2009-08-31 10:13:04 -04001350 if (state == gpt_corrupt) {
srs569408bb0da2010-02-19 17:19:55 -05001351 if (mbrState == gpt) {
1352 cout << "\a\a****************************************************************************\n"
1353 << "Caution: Found protective or hybrid MBR and corrupt GPT. Using GPT, but disk\n"
1354 << "verification and recovery are STRONGLY recommended.\n"
1355 << "****************************************************************************\n";
1356 which = use_gpt;
srs56943c0af382010-01-15 19:19:18 -05001357 } else {
srs569408bb0da2010-02-19 17:19:55 -05001358 which = use_abort;
1359 } // if/else MBR says disk is GPT
1360 } // if GPT corrupt
srs5694e4ac11e2009-08-31 10:13:04 -04001361
1362 if (which == use_new)
srs5694fed16d02010-01-27 23:03:40 -05001363 cout << "Creating new GPT entries.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001364
1365 return which;
1366} // UseWhichPartitions()
1367
srs569408bb0da2010-02-19 17:19:55 -05001368// Convert MBR partition table into GPT form.
1369void GPTData::XFormPartitions(void) {
srs5694e4ac11e2009-08-31 10:13:04 -04001370 int i, numToConvert;
1371 uint8_t origType;
srs5694e4ac11e2009-08-31 10:13:04 -04001372
1373 // Clear out old data & prepare basics....
1374 ClearGPTData();
1375
1376 // Convert the smaller of the # of GPT or MBR partitions
srs56940283dae2010-04-28 16:44:34 -04001377 if (numParts > MAX_MBR_PARTS)
srs5694978041c2009-09-21 20:51:47 -04001378 numToConvert = MAX_MBR_PARTS;
srs5694e4ac11e2009-08-31 10:13:04 -04001379 else
srs56940283dae2010-04-28 16:44:34 -04001380 numToConvert = numParts;
srs5694e4ac11e2009-08-31 10:13:04 -04001381
1382 for (i = 0; i < numToConvert; i++) {
1383 origType = protectiveMBR.GetType(i);
1384 // don't waste CPU time trying to convert extended, hybrid protective, or
1385 // null (non-existent) partitions
srs5694e35eb1b2009-09-14 00:29:34 -04001386 if ((origType != 0x05) && (origType != 0x0f) && (origType != 0x85) &&
srs56946699b012010-02-04 00:55:30 -05001387 (origType != 0x00) && (origType != 0xEE))
srs5694e4ac11e2009-08-31 10:13:04 -04001388 partitions[i] = protectiveMBR.AsGPT(i);
1389 } // for
1390
1391 // Convert MBR into protective MBR
1392 protectiveMBR.MakeProtectiveMBR();
1393
1394 // Record that all original CRCs were OK so as not to raise flags
1395 // when doing a disk verification
1396 mainCrcOk = secondCrcOk = mainPartsCrcOk = secondPartsCrcOk = 1;
srs5694e4ac11e2009-08-31 10:13:04 -04001397} // GPTData::XFormPartitions()
1398
1399// Transforms BSD disklabel on the specified partition (numbered from 0).
srs569408bb0da2010-02-19 17:19:55 -05001400// If an invalid partition number is given, the program does nothing.
srs5694e4ac11e2009-08-31 10:13:04 -04001401// Returns the number of new partitions created.
srs569408bb0da2010-02-19 17:19:55 -05001402int GPTData::XFormDisklabel(uint32_t partNum) {
1403 uint32_t low, high;
srs5694e4ac11e2009-08-31 10:13:04 -04001404 int goOn = 1, numDone = 0;
1405 BSDData disklabel;
1406
srs569408bb0da2010-02-19 17:19:55 -05001407 if (GetPartRange(&low, &high) == 0) {
1408 goOn = 0;
1409 cout << "No partitions!\n";
1410 } // if
1411 if (partNum > high) {
1412 goOn = 0;
1413 cout << "Specified partition is invalid!\n";
1414 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04001415
srs569408bb0da2010-02-19 17:19:55 -05001416 // If all is OK, read the disklabel and convert it.
1417 if (goOn) {
1418 goOn = disklabel.ReadBSDData(&myDisk, partitions[partNum].GetFirstLBA(),
1419 partitions[partNum].GetLastLBA());
1420 if ((goOn) && (disklabel.IsDisklabel())) {
1421 numDone = XFormDisklabel(&disklabel);
1422 if (numDone == 1)
1423 cout << "Converted 1 BSD partition.\n";
1424 else
1425 cout << "Converted " << numDone << " BSD partitions.\n";
1426 } else {
1427 cout << "Unable to convert partitions! Unrecognized BSD disklabel.\n";
1428 } // if/else
1429 } // if
1430 if (numDone > 0) { // converted partitions; delete carrier
1431 partitions[partNum].BlankPartition();
1432 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04001433 return numDone;
srs569455d92612010-03-07 22:16:07 -05001434} // GPTData::XFormDisklabel(uint32_t i)
srs5694e4ac11e2009-08-31 10:13:04 -04001435
1436// Transform the partitions on an already-loaded BSD disklabel...
srs569408bb0da2010-02-19 17:19:55 -05001437int GPTData::XFormDisklabel(BSDData* disklabel) {
1438 int i, partNum = 0, numDone = 0;
srs5694e4ac11e2009-08-31 10:13:04 -04001439
srs569408bb0da2010-02-19 17:19:55 -05001440 if (disklabel->IsDisklabel()) {
srs5694e4ac11e2009-08-31 10:13:04 -04001441 for (i = 0; i < disklabel->GetNumParts(); i++) {
srs569408bb0da2010-02-19 17:19:55 -05001442 partNum = FindFirstFreePart();
1443 if (partNum >= 0) {
1444 partitions[partNum] = disklabel->AsGPT(i);
1445 if (partitions[partNum].IsUsed())
1446 numDone++;
1447 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04001448 } // for
srs569408bb0da2010-02-19 17:19:55 -05001449 if (partNum == -1)
1450 cerr << "Warning! Too many partitions to convert!\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001451 } // if
1452
1453 // Record that all original CRCs were OK so as not to raise flags
1454 // when doing a disk verification
1455 mainCrcOk = secondCrcOk = mainPartsCrcOk = secondPartsCrcOk = 1;
1456
1457 return numDone;
1458} // GPTData::XFormDisklabel(BSDData* disklabel)
1459
srs569408bb0da2010-02-19 17:19:55 -05001460// Add one GPT partition to MBR. Used by PartsToMBR() functions. Created
1461// partition has the active/bootable flag UNset and uses the GPT fdisk
1462// type code divided by 0x0100 as the MBR type code.
1463// Returns 1 if operation was 100% successful, 0 if there were ANY
1464// problems.
srs5694978041c2009-09-21 20:51:47 -04001465int GPTData::OnePartToMBR(uint32_t gptPart, int mbrPart) {
srs569408bb0da2010-02-19 17:19:55 -05001466 int allOK = 1;
srs5694fed16d02010-01-27 23:03:40 -05001467
srs5694978041c2009-09-21 20:51:47 -04001468 if ((mbrPart < 0) || (mbrPart > 3)) {
srs5694fed16d02010-01-27 23:03:40 -05001469 cout << "MBR partition " << mbrPart + 1 << " is out of range; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001470 allOK = 0;
1471 } // if
srs56940283dae2010-04-28 16:44:34 -04001472 if (gptPart >= numParts) {
srs5694fed16d02010-01-27 23:03:40 -05001473 cout << "GPT partition " << gptPart + 1 << " is out of range; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001474 allOK = 0;
1475 } // if
1476 if (allOK && (partitions[gptPart].GetLastLBA() == UINT64_C(0))) {
srs5694fed16d02010-01-27 23:03:40 -05001477 cout << "GPT partition " << gptPart + 1 << " is undefined; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001478 allOK = 0;
1479 } // if
1480 if (allOK && (partitions[gptPart].GetFirstLBA() <= UINT32_MAX) &&
1481 (partitions[gptPart].GetLengthLBA() <= UINT32_MAX)) {
1482 if (partitions[gptPart].GetLastLBA() > UINT32_MAX) {
srs5694fed16d02010-01-27 23:03:40 -05001483 cout << "Caution: Partition end point past 32-bit pointer boundary;"
1484 << " some OSes may\nreact strangely.\n";
srs569408bb0da2010-02-19 17:19:55 -05001485 } // if
srs5694978041c2009-09-21 20:51:47 -04001486 protectiveMBR.MakePart(mbrPart, (uint32_t) partitions[gptPart].GetFirstLBA(),
srs569408bb0da2010-02-19 17:19:55 -05001487 (uint32_t) partitions[gptPart].GetLengthLBA(),
1488 partitions[gptPart].GetHexType() / 256, 0);
srs5694978041c2009-09-21 20:51:47 -04001489 } else { // partition out of range
srs569408bb0da2010-02-19 17:19:55 -05001490 if (allOK) // Display only if "else" triggered by out-of-bounds condition
1491 cout << "Partition " << gptPart + 1 << " begins beyond the 32-bit pointer limit of MBR "
1492 << "partitions, or is\n too big; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001493 allOK = 0;
1494 } // if/else
1495 return allOK;
1496} // GPTData::OnePartToMBR()
1497
srs569455d92612010-03-07 22:16:07 -05001498// Convert partitions to MBR form (primary and logical) and return
1499// the number done. Partitions are specified in a PartNotes variable,
1500// which includes pointers to GPT partition numbers. A partition number
1501// of MBR_EFI_GPT means to place an EFI GPT protective partition in that
1502// location in the table, and MBR_EMPTY means not to create a partition
1503// in that table position. If the partition type entry for a partition
1504// is 0, a default entry is used, based on the GPT partition type code.
srs569408bb0da2010-02-19 17:19:55 -05001505// Returns the number of partitions converted, NOT counting EFI GPT
srs569455d92612010-03-07 22:16:07 -05001506// protective partitions or extended partitions.
1507int GPTData::PartsToMBR(PartNotes & notes) {
1508 int mbrNum = 0, numConverted = 0;
1509 struct PartInfo convInfo;
srs5694978041c2009-09-21 20:51:47 -04001510
srs56949ddc14b2010-08-22 22:44:42 -04001511 protectiveMBR.EmptyMBR(0);
srs569455d92612010-03-07 22:16:07 -05001512 protectiveMBR.SetDiskSize(diskSize);
srs5694327129e2010-09-22 01:07:31 -04001513 notes.MakeItLegal();
srs569455d92612010-03-07 22:16:07 -05001514 notes.Rewind();
1515 while (notes.GetNextInfo(&convInfo) >= 0) {
1516 if ((convInfo.gptPartNum >= 0) && (convInfo.type == PRIMARY)) {
1517 numConverted += OnePartToMBR((uint32_t) convInfo.gptPartNum, mbrNum);
1518 if (convInfo.hexCode != 0)
1519 protectiveMBR.SetPartType(mbrNum, convInfo.hexCode);
1520 if (convInfo.active)
1521 protectiveMBR.SetPartBootable(mbrNum);
1522 mbrNum++;
1523 } // if
srs569461768bc2010-07-04 01:54:00 -04001524 if (convInfo.gptPartNum == MBR_EFI_GPT)
1525 mbrNum++;
1526 } // for
1527 // Now go through and set sizes for MBR_EFI_GPT partitions....
1528 notes.Rewind();
1529 mbrNum = 0;
1530 while (notes.GetNextInfo(&convInfo) >= 0) {
1531 if ((convInfo.gptPartNum >= 0) && (convInfo.type == PRIMARY))
1532 mbrNum++;
srs569455d92612010-03-07 22:16:07 -05001533 if (convInfo.gptPartNum == MBR_EFI_GPT) {
1534 if (protectiveMBR.FindFirstAvailable() == UINT32_C(1)) {
1535 protectiveMBR.MakePart(mbrNum, 1, protectiveMBR.FindLastInFree(1), convInfo.hexCode);
1536 protectiveMBR.SetHybrid();
1537 } else {
1538 protectiveMBR.MakeBiggestPart(mbrNum, convInfo.hexCode);
1539 } // if/else
1540 mbrNum++;
srs569461768bc2010-07-04 01:54:00 -04001541 } // if
1542 } // while
srs569455d92612010-03-07 22:16:07 -05001543 // Now do logical partition(s)...
1544 protectiveMBR.SetDisk(&myDisk);
1545 numConverted += protectiveMBR.CreateLogicals(notes);
srs569408bb0da2010-02-19 17:19:55 -05001546 return numConverted;
1547} // GPTData::PartsToMBR()
1548
srs5694e4ac11e2009-08-31 10:13:04 -04001549
1550/**********************************************************************
1551 * *
1552 * Functions that adjust GPT data structures WITHOUT user interaction *
1553 * (they may display information for the user's benefit, though) *
1554 * *
1555 **********************************************************************/
1556
1557// Resizes GPT to specified number of entries. Creates a new table if
srs5694ba00fed2010-01-12 18:18:36 -05001558// necessary, copies data if it already exists. Returns 1 if all goes
1559// well, 0 if an error is encountered.
srs5694e4ac11e2009-08-31 10:13:04 -04001560int GPTData::SetGPTSize(uint32_t numEntries) {
srs569408bb0da2010-02-19 17:19:55 -05001561 GPTPart* newParts;
1562 GPTPart* trash;
srs5694e4ac11e2009-08-31 10:13:04 -04001563 uint32_t i, high, copyNum;
1564 int allOK = 1;
1565
1566 // First, adjust numEntries upward, if necessary, to get a number
1567 // that fills the allocated sectors
1568 i = blockSize / GPT_SIZE;
1569 if ((numEntries % i) != 0) {
srs5694fed16d02010-01-27 23:03:40 -05001570 cout << "Adjusting GPT size from " << numEntries << " to ";
srs5694e4ac11e2009-08-31 10:13:04 -04001571 numEntries = ((numEntries / i) + 1) * i;
srs5694fed16d02010-01-27 23:03:40 -05001572 cout << numEntries << " to fill the sector\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001573 } // if
1574
srs5694247657a2009-11-26 18:36:12 -05001575 // Do the work only if the # of partitions is changing. Along with being
srs569455d92612010-03-07 22:16:07 -05001576 // efficient, this prevents mucking with the location of the secondary
srs5694247657a2009-11-26 18:36:12 -05001577 // partition table, which causes problems when loading data from a RAID
1578 // array that's been expanded because this function is called when loading
1579 // data.
srs56940283dae2010-04-28 16:44:34 -04001580 if (((numEntries != numParts) || (partitions == NULL)) && (numEntries > 0)) {
srs5694cb76c672010-02-11 22:22:22 -05001581 newParts = new GPTPart [numEntries * sizeof (GPTPart)];
srs5694247657a2009-11-26 18:36:12 -05001582 if (newParts != NULL) {
1583 if (partitions != NULL) { // existing partitions; copy them over
1584 GetPartRange(&i, &high);
1585 if (numEntries < (high + 1)) { // Highest entry too high for new #
srs5694fed16d02010-01-27 23:03:40 -05001586 cout << "The highest-numbered partition is " << high + 1
1587 << ", which is greater than the requested\n"
1588 << "partition table size of " << numEntries
1589 << "; cannot resize. Perhaps sorting will help.\n";
srs5694247657a2009-11-26 18:36:12 -05001590 allOK = 0;
1591 } else { // go ahead with copy
srs56940283dae2010-04-28 16:44:34 -04001592 if (numEntries < numParts)
srs5694247657a2009-11-26 18:36:12 -05001593 copyNum = numEntries;
1594 else
srs56940283dae2010-04-28 16:44:34 -04001595 copyNum = numParts;
srs5694247657a2009-11-26 18:36:12 -05001596 for (i = 0; i < copyNum; i++) {
1597 newParts[i] = partitions[i];
1598 } // for
1599 trash = partitions;
1600 partitions = newParts;
srs5694cb76c672010-02-11 22:22:22 -05001601 delete[] trash;
srs5694247657a2009-11-26 18:36:12 -05001602 } // if
1603 } else { // No existing partition table; just create it
srs5694e4ac11e2009-08-31 10:13:04 -04001604 partitions = newParts;
srs5694247657a2009-11-26 18:36:12 -05001605 } // if/else existing partitions
srs56940283dae2010-04-28 16:44:34 -04001606 numParts = numEntries;
srs5694247657a2009-11-26 18:36:12 -05001607 mainHeader.firstUsableLBA = ((numEntries * GPT_SIZE) / blockSize) + 2 ;
1608 secondHeader.firstUsableLBA = mainHeader.firstUsableLBA;
1609 MoveSecondHeaderToEnd();
1610 if (diskSize > 0)
1611 CheckGPTSize();
1612 } else { // Bad memory allocation
srs5694fed16d02010-01-27 23:03:40 -05001613 cerr << "Error allocating memory for partition table!\n";
srs5694247657a2009-11-26 18:36:12 -05001614 allOK = 0;
1615 } // if/else
srs5694e4ac11e2009-08-31 10:13:04 -04001616 } // if/else
srs56940283dae2010-04-28 16:44:34 -04001617 mainHeader.numParts = numParts;
1618 secondHeader.numParts = numParts;
srs5694e4ac11e2009-08-31 10:13:04 -04001619 return (allOK);
1620} // GPTData::SetGPTSize()
1621
1622// Blank the partition array
1623void GPTData::BlankPartitions(void) {
1624 uint32_t i;
1625
srs56940283dae2010-04-28 16:44:34 -04001626 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04001627 partitions[i].BlankPartition();
1628 } // for
1629} // GPTData::BlankPartitions()
1630
srs5694ba00fed2010-01-12 18:18:36 -05001631// Delete a partition by number. Returns 1 if successful,
1632// 0 if there was a problem. Returns 1 if partition was in
1633// range, 0 if it was out of range.
1634int GPTData::DeletePartition(uint32_t partNum) {
1635 uint64_t startSector, length;
srs56940283dae2010-04-28 16:44:34 -04001636 uint32_t low, high, numUsedParts, retval = 1;;
srs5694ba00fed2010-01-12 18:18:36 -05001637
srs56940283dae2010-04-28 16:44:34 -04001638 numUsedParts = GetPartRange(&low, &high);
1639 if ((numUsedParts > 0) && (partNum >= low) && (partNum <= high)) {
srs5694ba00fed2010-01-12 18:18:36 -05001640 // In case there's a protective MBR, look for & delete matching
1641 // MBR partition....
1642 startSector = partitions[partNum].GetFirstLBA();
1643 length = partitions[partNum].GetLengthLBA();
1644 protectiveMBR.DeleteByLocation(startSector, length);
1645
1646 // Now delete the GPT partition
1647 partitions[partNum].BlankPartition();
1648 } else {
srs5694fed16d02010-01-27 23:03:40 -05001649 cerr << "Partition number " << partNum + 1 << " out of range!\n";
srs5694ba00fed2010-01-12 18:18:36 -05001650 retval = 0;
1651 } // if/else
1652 return retval;
1653} // GPTData::DeletePartition(uint32_t partNum)
1654
srs569408bb0da2010-02-19 17:19:55 -05001655// Non-interactively create a partition.
1656// Returns 1 if the operation was successful, 0 if a problem was discovered.
srs5694e321d442010-01-29 17:44:04 -05001657uint32_t GPTData::CreatePartition(uint32_t partNum, uint64_t startSector, uint64_t endSector) {
srs5694ba00fed2010-01-12 18:18:36 -05001658 int retval = 1; // assume there'll be no problems
srs56945a081752010-09-24 20:39:41 -04001659 uint64_t origSector = startSector;
srs5694ba00fed2010-01-12 18:18:36 -05001660
1661 if (IsFreePartNum(partNum)) {
srs56945a081752010-09-24 20:39:41 -04001662 if (Align(&startSector)) {
1663 cout << "Information: Moved requested sector from " << origSector << " to "
1664 << startSector << " in\norder to align on " << sectorAlignment
1665 << "-sector boundaries.\n";
1666 } // if
srs5694ba00fed2010-01-12 18:18:36 -05001667 if (IsFree(startSector) && (startSector <= endSector)) {
1668 if (FindLastInFree(startSector) >= endSector) {
1669 partitions[partNum].SetFirstLBA(startSector);
1670 partitions[partNum].SetLastLBA(endSector);
1671 partitions[partNum].SetType(0x0700);
srs56946699b012010-02-04 00:55:30 -05001672 partitions[partNum].RandomizeUniqueGUID();
srs5694ba00fed2010-01-12 18:18:36 -05001673 } else retval = 0; // if free space until endSector
1674 } else retval = 0; // if startSector is free
1675 } else retval = 0; // if legal partition number
1676 return retval;
1677} // GPTData::CreatePartition(partNum, startSector, endSector)
1678
srs5694e4ac11e2009-08-31 10:13:04 -04001679// Sort the GPT entries, eliminating gaps and making for a logical
1680// ordering. Relies on QuickSortGPT() for the bulk of the work
1681void GPTData::SortGPT(void) {
srs5694546a9c72010-01-26 16:00:26 -05001682 uint32_t i, numFound, firstPart, lastPart;
srs5694e4ac11e2009-08-31 10:13:04 -04001683
1684 // First, find the last partition with data, so as not to
1685 // spend needless time sorting empty entries....
srs5694546a9c72010-01-26 16:00:26 -05001686 numFound = GetPartRange(&firstPart, &lastPart);
srs5694e4ac11e2009-08-31 10:13:04 -04001687
1688 // Now swap empties with the last partitions, to simplify the logic
1689 // in the Quicksort function....
1690 i = 0;
1691 while (i < lastPart) {
1692 if (partitions[i].GetFirstLBA() == 0) {
srs569408bb0da2010-02-19 17:19:55 -05001693 SwapPartitions(i, lastPart);
srs5694546a9c72010-01-26 16:00:26 -05001694 do {
1695 lastPart--;
1696 } while ((lastPart > 0) && (partitions[lastPart].GetFirstLBA() == 0));
srs5694e4ac11e2009-08-31 10:13:04 -04001697 } // if
1698 i++;
1699 } // while
1700
srs5694546a9c72010-01-26 16:00:26 -05001701 // If there are more empties than partitions in the range from 0 to lastPart,
1702 // the above leaves lastPart set too high, so we've got to adjust it to
1703 // prevent empties from migrating to the top of the list....
1704 GetPartRange(&firstPart, &lastPart);
1705
srs5694e4ac11e2009-08-31 10:13:04 -04001706 // Now call the recursive quick sort routine to do the real work....
srs569408bb0da2010-02-19 17:19:55 -05001707 QuickSortGPT(0, lastPart);
srs5694e4ac11e2009-08-31 10:13:04 -04001708} // GPTData::SortGPT()
1709
srs569408bb0da2010-02-19 17:19:55 -05001710// Recursive quick sort algorithm for GPT partitions. Note that if there
1711// are any empties in the specified range, they'll be sorted to the
1712// start, resulting in a sorted set of partitions that begins with
1713// partition 2, 3, or higher.
1714void GPTData::QuickSortGPT(int start, int finish) {
1715 uint64_t starterValue; // starting location of median partition
1716 int left, right;
1717
1718 left = start;
1719 right = finish;
1720 starterValue = partitions[(start + finish) / 2].GetFirstLBA();
1721 do {
1722 while (partitions[left].GetFirstLBA() < starterValue)
1723 left++;
1724 while (partitions[right].GetFirstLBA() > starterValue)
1725 right--;
1726 if (left <= right)
1727 SwapPartitions(left++, right--);
1728 } while (left <= right);
1729 if (start < right) QuickSortGPT(start, right);
1730 if (finish > left) QuickSortGPT(left, finish);
1731} // GPTData::QuickSortGPT()
1732
1733// Swap the contents of two partitions.
1734// Returns 1 if successful, 0 if either partition is out of range
1735// (that is, not a legal number; either or both can be empty).
1736// Note that if partNum1 = partNum2 and this number is in range,
1737// it will be considered successful.
1738int GPTData::SwapPartitions(uint32_t partNum1, uint32_t partNum2) {
1739 GPTPart temp;
1740 int allOK = 1;
1741
srs56940283dae2010-04-28 16:44:34 -04001742 if ((partNum1 < numParts) && (partNum2 < numParts)) {
srs569408bb0da2010-02-19 17:19:55 -05001743 if (partNum1 != partNum2) {
1744 temp = partitions[partNum1];
1745 partitions[partNum1] = partitions[partNum2];
1746 partitions[partNum2] = temp;
1747 } // if
1748 } else allOK = 0; // partition numbers are valid
1749 return allOK;
1750} // GPTData::SwapPartitions()
1751
srs5694e4ac11e2009-08-31 10:13:04 -04001752// Set up data structures for entirely new set of partitions on the
1753// specified device. Returns 1 if OK, 0 if there were problems.
srs5694e35eb1b2009-09-14 00:29:34 -04001754// Note that this function does NOT clear the protectiveMBR data
1755// structure, since it may hold the original MBR partitions if the
1756// program was launched on an MBR disk, and those may need to be
1757// converted to GPT format.
srs5694e4ac11e2009-08-31 10:13:04 -04001758int GPTData::ClearGPTData(void) {
srs5694e35eb1b2009-09-14 00:29:34 -04001759 int goOn = 1, i;
srs5694e4ac11e2009-08-31 10:13:04 -04001760
1761 // Set up the partition table....
srs5694fed16d02010-01-27 23:03:40 -05001762 if (partitions != NULL)
srs5694cb76c672010-02-11 22:22:22 -05001763 delete[] partitions;
srs5694e4ac11e2009-08-31 10:13:04 -04001764 partitions = NULL;
1765 SetGPTSize(NUM_GPT_ENTRIES);
1766
1767 // Now initialize a bunch of stuff that's static....
1768 mainHeader.signature = GPT_SIGNATURE;
1769 mainHeader.revision = 0x00010000;
srs5694978041c2009-09-21 20:51:47 -04001770 mainHeader.headerSize = HEADER_SIZE;
srs5694e4ac11e2009-08-31 10:13:04 -04001771 mainHeader.reserved = 0;
1772 mainHeader.currentLBA = UINT64_C(1);
1773 mainHeader.partitionEntriesLBA = (uint64_t) 2;
1774 mainHeader.sizeOfPartitionEntries = GPT_SIZE;
1775 for (i = 0; i < GPT_RESERVED; i++) {
1776 mainHeader.reserved2[i] = '\0';
1777 } // for
srs56940873e9d2010-10-07 13:00:45 -04001778 if (blockSize > 0)
1779 sectorAlignment = DEFAULT_ALIGNMENT * SECTOR_SIZE / blockSize;
1780 else
1781 sectorAlignment = DEFAULT_ALIGNMENT;
srs5694e4ac11e2009-08-31 10:13:04 -04001782
1783 // Now some semi-static items (computed based on end of disk)
1784 mainHeader.backupLBA = diskSize - UINT64_C(1);
1785 mainHeader.lastUsableLBA = diskSize - mainHeader.firstUsableLBA;
1786
1787 // Set a unique GUID for the disk, based on random numbers
srs56946699b012010-02-04 00:55:30 -05001788 mainHeader.diskGUID.Randomize();
srs5694e4ac11e2009-08-31 10:13:04 -04001789
1790 // Copy main header to backup header
1791 RebuildSecondHeader();
1792
1793 // Blank out the partitions array....
1794 BlankPartitions();
1795
1796 // Flag all CRCs as being OK....
1797 mainCrcOk = 1;
1798 secondCrcOk = 1;
1799 mainPartsCrcOk = 1;
1800 secondPartsCrcOk = 1;
1801
1802 return (goOn);
1803} // GPTData::ClearGPTData()
1804
srs5694247657a2009-11-26 18:36:12 -05001805// Set the location of the second GPT header data to the end of the disk.
1806// Used internally and called by the 'e' option on the recovery &
1807// transformation menu, to help users of RAID arrays who add disk space
1808// to their arrays.
1809void GPTData::MoveSecondHeaderToEnd() {
srs56948bb78762009-11-24 15:43:49 -05001810 mainHeader.backupLBA = secondHeader.currentLBA = diskSize - UINT64_C(1);
1811 mainHeader.lastUsableLBA = secondHeader.lastUsableLBA = diskSize - mainHeader.firstUsableLBA;
1812 secondHeader.partitionEntriesLBA = secondHeader.lastUsableLBA + UINT64_C(1);
1813} // GPTData::FixSecondHeaderLocation()
1814
srs56940a697312010-01-28 21:10:52 -05001815int GPTData::SetName(uint32_t partNum, const string & theName) {
srs5694ba00fed2010-01-12 18:18:36 -05001816 int retval = 1;
srs5694fed16d02010-01-27 23:03:40 -05001817
1818 if (!IsFreePartNum(partNum)) {
1819 partitions[partNum].SetName(theName);
1820 } else retval = 0;
srs5694ba00fed2010-01-12 18:18:36 -05001821
1822 return retval;
srs5694e4ac11e2009-08-31 10:13:04 -04001823} // GPTData::SetName
1824
1825// Set the disk GUID to the specified value. Note that the header CRCs must
1826// be recomputed after calling this function.
1827void GPTData::SetDiskGUID(GUIDData newGUID) {
1828 mainHeader.diskGUID = newGUID;
1829 secondHeader.diskGUID = newGUID;
1830} // SetDiskGUID()
1831
1832// Set the unique GUID of the specified partition. Returns 1 on
1833// successful completion, 0 if there were problems (invalid
1834// partition number).
1835int GPTData::SetPartitionGUID(uint32_t pn, GUIDData theGUID) {
1836 int retval = 0;
1837
srs56940283dae2010-04-28 16:44:34 -04001838 if (pn < numParts) {
srs5694e4ac11e2009-08-31 10:13:04 -04001839 if (partitions[pn].GetFirstLBA() != UINT64_C(0)) {
1840 partitions[pn].SetUniqueGUID(theGUID);
1841 retval = 1;
1842 } // if
1843 } // if
1844 return retval;
1845} // GPTData::SetPartitionGUID()
1846
srs56949ba54212010-05-18 23:24:02 -04001847// Set new random GUIDs for the disk and all partitions. Intended to be used
1848// after disk cloning or similar operations that don't randomize the GUIDs.
1849void GPTData::RandomizeGUIDs(void) {
1850 uint32_t i;
1851
1852 mainHeader.diskGUID.Randomize();
1853 secondHeader.diskGUID = mainHeader.diskGUID;
1854 for (i = 0; i < numParts; i++)
1855 if (partitions[i].IsUsed())
1856 partitions[i].RandomizeUniqueGUID();
1857} // GPTData::RandomizeGUIDs()
1858
srs5694ba00fed2010-01-12 18:18:36 -05001859// Change partition type code non-interactively. Returns 1 if
1860// successful, 0 if not....
srs5694327129e2010-09-22 01:07:31 -04001861int GPTData::ChangePartType(uint32_t partNum, PartType theGUID) {
1862 int retval = 1;
1863
1864 if (!IsFreePartNum(partNum)) {
1865 partitions[partNum].SetType(theGUID);
1866 } else retval = 0;
1867 return retval;
1868} // GPTData::ChangePartType()
1869
srs56949ba54212010-05-18 23:24:02 -04001870// Recompute the CHS values of all the MBR partitions. Used to reset
1871// CHS values that some BIOSes require, despite the fact that the
1872// resulting CHS values violate the GPT standard.
1873void GPTData::RecomputeCHS(void) {
1874 int i;
1875
1876 for (i = 0; i < 4; i++)
1877 protectiveMBR.RecomputeCHS(i);
1878} // GPTData::RecomputeCHS()
1879
srs56941d1448a2009-12-31 21:20:19 -05001880// Adjust sector number so that it falls on a sector boundary that's a
1881// multiple of sectorAlignment. This is done to improve the performance
1882// of Western Digital Advanced Format disks and disks with similar
1883// technology from other companies, which use 4096-byte sectors
1884// internally although they translate to 512-byte sectors for the
1885// benefit of the OS. If partitions aren't properly aligned on these
1886// disks, some filesystem data structures can span multiple physical
1887// sectors, degrading performance. This function should be called
1888// only on the FIRST sector of the partition, not the last!
1889// This function returns 1 if the alignment was altered, 0 if it
1890// was unchanged.
1891int GPTData::Align(uint64_t* sector) {
1892 int retval = 0, sectorOK = 0;
1893 uint64_t earlier, later, testSector, original;
1894
1895 if ((*sector % sectorAlignment) != 0) {
1896 original = *sector;
srs56941d1448a2009-12-31 21:20:19 -05001897 earlier = (*sector / sectorAlignment) * sectorAlignment;
1898 later = earlier + (uint64_t) sectorAlignment;
1899
1900 // Check to see that every sector between the earlier one and the
1901 // requested one is clear, and that it's not too early....
1902 if (earlier >= mainHeader.firstUsableLBA) {
srs56941d1448a2009-12-31 21:20:19 -05001903 sectorOK = 1;
1904 testSector = earlier;
1905 do {
1906 sectorOK = IsFree(testSector++);
1907 } while ((sectorOK == 1) && (testSector < *sector));
1908 if (sectorOK == 1) {
1909 *sector = earlier;
srs56945a081752010-09-24 20:39:41 -04001910 retval = 1;
srs56941d1448a2009-12-31 21:20:19 -05001911 } // if
1912 } // if firstUsableLBA check
1913
1914 // If couldn't move the sector earlier, try to move it later instead....
1915 if ((sectorOK != 1) && (later <= mainHeader.lastUsableLBA)) {
1916 sectorOK = 1;
1917 testSector = later;
1918 do {
1919 sectorOK = IsFree(testSector--);
1920 } while ((sectorOK == 1) && (testSector > *sector));
1921 if (sectorOK == 1) {
1922 *sector = later;
srs56945a081752010-09-24 20:39:41 -04001923 retval = 1;
srs56941d1448a2009-12-31 21:20:19 -05001924 } // if
1925 } // if
srs56941d1448a2009-12-31 21:20:19 -05001926 } // if
1927 return retval;
1928} // GPTData::Align()
1929
srs5694e4ac11e2009-08-31 10:13:04 -04001930/********************************************************
1931 * *
1932 * Functions that return data about GPT data structures *
1933 * (most of these are inline in gpt.h) *
1934 * *
1935 ********************************************************/
1936
1937// Find the low and high used partition numbers (numbered from 0).
1938// Return value is the number of partitions found. Note that the
1939// *low and *high values are both set to 0 when no partitions
1940// are found, as well as when a single partition in the first
1941// position exists. Thus, the return value is the only way to
1942// tell when no partitions exist.
1943int GPTData::GetPartRange(uint32_t *low, uint32_t *high) {
1944 uint32_t i;
1945 int numFound = 0;
1946
srs56940283dae2010-04-28 16:44:34 -04001947 *low = numParts + 1; // code for "not found"
srs5694e4ac11e2009-08-31 10:13:04 -04001948 *high = 0;
srs56940283dae2010-04-28 16:44:34 -04001949 if (numParts > 0) { // only try if partition table exists...
1950 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04001951 if (partitions[i].GetFirstLBA() != UINT64_C(0)) { // it exists
1952 *high = i; // since we're counting up, set the high value
srs569408bb0da2010-02-19 17:19:55 -05001953 // Set the low value only if it's not yet found...
srs56940283dae2010-04-28 16:44:34 -04001954 if (*low == (numParts + 1)) *low = i;
srs569408bb0da2010-02-19 17:19:55 -05001955 numFound++;
srs5694e4ac11e2009-08-31 10:13:04 -04001956 } // if
1957 } // for
1958 } // if
1959
1960 // Above will leave *low pointing to its "not found" value if no partitions
1961 // are defined, so reset to 0 if this is the case....
srs56940283dae2010-04-28 16:44:34 -04001962 if (*low == (numParts + 1))
srs5694e4ac11e2009-08-31 10:13:04 -04001963 *low = 0;
1964 return numFound;
1965} // GPTData::GetPartRange()
1966
srs569408bb0da2010-02-19 17:19:55 -05001967// Returns the value of the first free partition, or -1 if none is
1968// unused.
1969int GPTData::FindFirstFreePart(void) {
1970 int i = 0;
1971
1972 if (partitions != NULL) {
srs56940283dae2010-04-28 16:44:34 -04001973 while ((partitions[i].IsUsed()) && (i < (int) numParts))
srs569408bb0da2010-02-19 17:19:55 -05001974 i++;
srs56940283dae2010-04-28 16:44:34 -04001975 if (i >= (int) numParts)
srs569408bb0da2010-02-19 17:19:55 -05001976 i = -1;
1977 } else i = -1;
1978 return i;
1979} // GPTData::FindFirstFreePart()
1980
srs5694978041c2009-09-21 20:51:47 -04001981// Returns the number of defined partitions.
1982uint32_t GPTData::CountParts(void) {
srs5694e321d442010-01-29 17:44:04 -05001983 uint32_t i, counted = 0;
srs5694978041c2009-09-21 20:51:47 -04001984
srs56940283dae2010-04-28 16:44:34 -04001985 for (i = 0; i < numParts; i++) {
srs569408bb0da2010-02-19 17:19:55 -05001986 if (partitions[i].IsUsed())
srs5694978041c2009-09-21 20:51:47 -04001987 counted++;
1988 } // for
1989 return counted;
1990} // GPTData::CountParts()
1991
srs5694e4ac11e2009-08-31 10:13:04 -04001992/****************************************************
1993 * *
1994 * Functions that return data about disk free space *
1995 * *
1996 ****************************************************/
1997
1998// Find the first available block after the starting point; returns 0 if
1999// there are no available blocks left
2000uint64_t GPTData::FindFirstAvailable(uint64_t start) {
2001 uint64_t first;
2002 uint32_t i;
2003 int firstMoved = 0;
2004
2005 // Begin from the specified starting point or from the first usable
2006 // LBA, whichever is greater...
2007 if (start < mainHeader.firstUsableLBA)
2008 first = mainHeader.firstUsableLBA;
2009 else
2010 first = start;
2011
2012 // ...now search through all partitions; if first is within an
2013 // existing partition, move it to the next sector after that
2014 // partition and repeat. If first was moved, set firstMoved
2015 // flag; repeat until firstMoved is not set, so as to catch
2016 // cases where partitions are out of sequential order....
2017 do {
2018 firstMoved = 0;
srs56940283dae2010-04-28 16:44:34 -04002019 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04002020 if ((first >= partitions[i].GetFirstLBA()) &&
srs569455d92612010-03-07 22:16:07 -05002021 (first <= partitions[i].GetLastLBA())) { // in existing part.
srs5694e4ac11e2009-08-31 10:13:04 -04002022 first = partitions[i].GetLastLBA() + 1;
2023 firstMoved = 1;
srs569455d92612010-03-07 22:16:07 -05002024 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002025 } // for
2026 } while (firstMoved == 1);
2027 if (first > mainHeader.lastUsableLBA)
2028 first = 0;
2029 return (first);
2030} // GPTData::FindFirstAvailable()
2031
2032// Finds the first available sector in the largest block of unallocated
2033// space on the disk. Returns 0 if there are no available blocks left
2034uint64_t GPTData::FindFirstInLargest(void) {
srs5694e35eb1b2009-09-14 00:29:34 -04002035 uint64_t start, firstBlock, lastBlock, segmentSize, selectedSize = 0, selectedSegment = 0;
srs5694e4ac11e2009-08-31 10:13:04 -04002036
2037 start = 0;
2038 do {
2039 firstBlock = FindFirstAvailable(start);
2040 if (firstBlock != UINT32_C(0)) { // something's free...
2041 lastBlock = FindLastInFree(firstBlock);
2042 segmentSize = lastBlock - firstBlock + UINT32_C(1);
2043 if (segmentSize > selectedSize) {
2044 selectedSize = segmentSize;
2045 selectedSegment = firstBlock;
2046 } // if
2047 start = lastBlock + 1;
2048 } // if
2049 } while (firstBlock != 0);
2050 return selectedSegment;
2051} // GPTData::FindFirstInLargest()
2052
srs5694cb76c672010-02-11 22:22:22 -05002053// Find the last available block on the disk.
2054// Returns 0 if there are no available partitions
2055uint64_t GPTData::FindLastAvailable(void) {
srs5694e4ac11e2009-08-31 10:13:04 -04002056 uint64_t last;
2057 uint32_t i;
2058 int lastMoved = 0;
2059
2060 // Start by assuming the last usable LBA is available....
2061 last = mainHeader.lastUsableLBA;
2062
2063 // ...now, similar to algorithm in FindFirstAvailable(), search
2064 // through all partitions, moving last when it's in an existing
2065 // partition. Set the lastMoved flag so we repeat to catch cases
2066 // where partitions are out of logical order.
2067 do {
2068 lastMoved = 0;
srs56940283dae2010-04-28 16:44:34 -04002069 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04002070 if ((last >= partitions[i].GetFirstLBA()) &&
srs569455d92612010-03-07 22:16:07 -05002071 (last <= partitions[i].GetLastLBA())) { // in existing part.
srs5694e4ac11e2009-08-31 10:13:04 -04002072 last = partitions[i].GetFirstLBA() - 1;
2073 lastMoved = 1;
srs569455d92612010-03-07 22:16:07 -05002074 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002075 } // for
2076 } while (lastMoved == 1);
2077 if (last < mainHeader.firstUsableLBA)
2078 last = 0;
2079 return (last);
2080} // GPTData::FindLastAvailable()
2081
2082// Find the last available block in the free space pointed to by start.
2083uint64_t GPTData::FindLastInFree(uint64_t start) {
2084 uint64_t nearestStart;
2085 uint32_t i;
2086
2087 nearestStart = mainHeader.lastUsableLBA;
srs56940283dae2010-04-28 16:44:34 -04002088 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04002089 if ((nearestStart > partitions[i].GetFirstLBA()) &&
srs569455d92612010-03-07 22:16:07 -05002090 (partitions[i].GetFirstLBA() > start)) {
srs5694e4ac11e2009-08-31 10:13:04 -04002091 nearestStart = partitions[i].GetFirstLBA() - 1;
srs569455d92612010-03-07 22:16:07 -05002092 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002093 } // for
2094 return (nearestStart);
2095} // GPTData::FindLastInFree()
2096
2097// Finds the total number of free blocks, the number of segments in which
2098// they reside, and the size of the largest of those segments
srs5694e321d442010-01-29 17:44:04 -05002099uint64_t GPTData::FindFreeBlocks(uint32_t *numSegments, uint64_t *largestSegment) {
srs5694e4ac11e2009-08-31 10:13:04 -04002100 uint64_t start = UINT64_C(0); // starting point for each search
2101 uint64_t totalFound = UINT64_C(0); // running total
2102 uint64_t firstBlock; // first block in a segment
2103 uint64_t lastBlock; // last block in a segment
2104 uint64_t segmentSize; // size of segment in blocks
srs5694e321d442010-01-29 17:44:04 -05002105 uint32_t num = 0;
srs5694e4ac11e2009-08-31 10:13:04 -04002106
2107 *largestSegment = UINT64_C(0);
srs5694c54e9b42010-05-01 21:04:23 -04002108 if (diskSize > 0) {
2109 do {
2110 firstBlock = FindFirstAvailable(start);
2111 if (firstBlock != UINT64_C(0)) { // something's free...
2112 lastBlock = FindLastInFree(firstBlock);
2113 segmentSize = lastBlock - firstBlock + UINT64_C(1);
2114 if (segmentSize > *largestSegment) {
2115 *largestSegment = segmentSize;
2116 } // if
2117 totalFound += segmentSize;
2118 num++;
2119 start = lastBlock + 1;
srs5694e4ac11e2009-08-31 10:13:04 -04002120 } // if
srs5694c54e9b42010-05-01 21:04:23 -04002121 } while (firstBlock != 0);
2122 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002123 *numSegments = num;
2124 return totalFound;
2125} // GPTData::FindFreeBlocks()
2126
srs569455d92612010-03-07 22:16:07 -05002127// Returns 1 if sector is unallocated, 0 if it's allocated to a partition.
2128// If it's allocated, return the partition number to which it's allocated
2129// in partNum, if that variable is non-NULL. (A value of UINT32_MAX is
2130// returned in partNum if the sector is in use by basic GPT data structures.)
2131int GPTData::IsFree(uint64_t sector, uint32_t *partNum) {
srs5694e4ac11e2009-08-31 10:13:04 -04002132 int isFree = 1;
2133 uint32_t i;
2134
srs56940283dae2010-04-28 16:44:34 -04002135 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04002136 if ((sector >= partitions[i].GetFirstLBA()) &&
2137 (sector <= partitions[i].GetLastLBA())) {
2138 isFree = 0;
srs569455d92612010-03-07 22:16:07 -05002139 if (partNum != NULL)
2140 *partNum = i;
srs569408bb0da2010-02-19 17:19:55 -05002141 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002142 } // for
srs5694e35eb1b2009-09-14 00:29:34 -04002143 if ((sector < mainHeader.firstUsableLBA) ||
srs5694e4ac11e2009-08-31 10:13:04 -04002144 (sector > mainHeader.lastUsableLBA)) {
2145 isFree = 0;
srs569455d92612010-03-07 22:16:07 -05002146 if (partNum != NULL)
2147 *partNum = UINT32_MAX;
srs569408bb0da2010-02-19 17:19:55 -05002148 } // if
2149 return (isFree);
srs5694e4ac11e2009-08-31 10:13:04 -04002150} // GPTData::IsFree()
2151
srs5694ba00fed2010-01-12 18:18:36 -05002152// Returns 1 if partNum is unused.
2153int GPTData::IsFreePartNum(uint32_t partNum) {
2154 int retval = 1;
2155
srs56940283dae2010-04-28 16:44:34 -04002156 if ((partNum < numParts) && (partitions != NULL)) {
srs569408bb0da2010-02-19 17:19:55 -05002157 if (partitions[partNum].IsUsed()) {
srs5694ba00fed2010-01-12 18:18:36 -05002158 retval = 0;
2159 } // if partition is in use
2160 } else retval = 0;
2161
2162 return retval;
2163} // GPTData::IsFreePartNum()
2164
srs5694a8582cf2010-03-19 14:21:59 -04002165
2166/***********************************************************
2167 * *
2168 * Change how functions work or return information on them *
2169 * *
2170 ***********************************************************/
2171
2172// Set partition alignment value; partitions will begin on multiples of
2173// the specified value
2174void GPTData::SetAlignment(uint32_t n) {
srs56940873e9d2010-10-07 13:00:45 -04002175 if (n > 0)
2176 sectorAlignment = n;
2177 else
2178 cerr << "Attempt to set partition alignment to 0!\n";
srs5694a8582cf2010-03-19 14:21:59 -04002179} // GPTData::SetAlignment()
2180
2181// Compute sector alignment based on the current partitions (if any). Each
2182// partition's starting LBA is examined, and if it's divisible by a power-of-2
srs56940873e9d2010-10-07 13:00:45 -04002183// value less than or equal to the DEFAULT_ALIGNMENT value (adjusted for the
2184// sector size), but not by the previously-located alignment value, then the
2185// alignment value is adjusted down. If the computed alignment is less than 8
2186// and the disk is bigger than SMALLEST_ADVANCED_FORMAT, resets it to 8. This
2187// is a safety measure for WD Advanced Format and similar drives. If no partitions
2188// are defined, the alignment value is set to DEFAULT_ALIGNMENT (2048) (or an
2189// adjustment of that based on the current sector size). The result is that new
srs56948a4ddfc2010-03-21 19:05:49 -04002190// drives are aligned to 2048-sector multiples but the program won't complain
2191// about other alignments on existing disks unless a smaller-than-8 alignment
srs56940873e9d2010-10-07 13:00:45 -04002192// is used on big disks (as safety for WD Advanced Format drives).
srs5694a8582cf2010-03-19 14:21:59 -04002193// Returns the computed alignment value.
2194uint32_t GPTData::ComputeAlignment(void) {
2195 uint32_t i = 0, found, exponent = 31;
srs5694ab4b0432010-09-25 20:39:52 -04002196 uint32_t align = DEFAULT_ALIGNMENT;
srs5694a8582cf2010-03-19 14:21:59 -04002197
srs56940873e9d2010-10-07 13:00:45 -04002198 if (blockSize > 0)
2199 align = DEFAULT_ALIGNMENT * SECTOR_SIZE / blockSize;
2200 exponent = (uint32_t) log2(align);
srs56940283dae2010-04-28 16:44:34 -04002201 for (i = 0; i < numParts; i++) {
srs5694a8582cf2010-03-19 14:21:59 -04002202 if (partitions[i].IsUsed()) {
2203 found = 0;
2204 while (!found) {
srs56940873e9d2010-10-07 13:00:45 -04002205 align = UINT64_C(1) << exponent;
srs5694a8582cf2010-03-19 14:21:59 -04002206 if ((partitions[i].GetFirstLBA() % align) == 0) {
2207 found = 1;
2208 } else {
2209 exponent--;
2210 } // if/else
2211 } // while
2212 } // if
2213 } // for
srs56940873e9d2010-10-07 13:00:45 -04002214 if ((align < MIN_AF_ALIGNMENT) && (diskSize >= SMALLEST_ADVANCED_FORMAT))
2215 align = MIN_AF_ALIGNMENT;
2216 sectorAlignment = align;
srs5694a8582cf2010-03-19 14:21:59 -04002217 return align;
2218} // GPTData::ComputeAlignment()
2219
srs5694e4ac11e2009-08-31 10:13:04 -04002220/********************************
2221 * *
2222 * Endianness support functions *
2223 * *
2224 ********************************/
2225
srs56942a9f5da2009-08-26 00:48:01 -04002226void GPTData::ReverseHeaderBytes(struct GPTHeader* header) {
srs5694221e0872009-08-29 15:00:31 -04002227 ReverseBytes(&header->signature, 8);
2228 ReverseBytes(&header->revision, 4);
2229 ReverseBytes(&header->headerSize, 4);
2230 ReverseBytes(&header->headerCRC, 4);
2231 ReverseBytes(&header->reserved, 4);
2232 ReverseBytes(&header->currentLBA, 8);
2233 ReverseBytes(&header->backupLBA, 8);
2234 ReverseBytes(&header->firstUsableLBA, 8);
2235 ReverseBytes(&header->lastUsableLBA, 8);
2236 ReverseBytes(&header->partitionEntriesLBA, 8);
2237 ReverseBytes(&header->numParts, 4);
2238 ReverseBytes(&header->sizeOfPartitionEntries, 4);
2239 ReverseBytes(&header->partitionEntriesCRC, 4);
srs569408bb0da2010-02-19 17:19:55 -05002240 ReverseBytes(header->reserved2, GPT_RESERVED);
srs56942a9f5da2009-08-26 00:48:01 -04002241} // GPTData::ReverseHeaderBytes()
2242
srs56940283dae2010-04-28 16:44:34 -04002243// Reverse byte order for all partitions.
srs56942a9f5da2009-08-26 00:48:01 -04002244void GPTData::ReversePartitionBytes() {
2245 uint32_t i;
2246
srs56940283dae2010-04-28 16:44:34 -04002247 for (i = 0; i < numParts; i++) {
srs5694221e0872009-08-29 15:00:31 -04002248 partitions[i].ReversePartBytes();
srs56942a9f5da2009-08-26 00:48:01 -04002249 } // for
2250} // GPTData::ReversePartitionBytes()
2251
srs56949ddc14b2010-08-22 22:44:42 -04002252// Validate partition number
2253bool GPTData::ValidPartNum (const uint32_t partNum) {
2254 if (partNum >= numParts) {
srs56945a081752010-09-24 20:39:41 -04002255 cerr << "Partition number out of range: " << partNum << "\n";
srs56949ddc14b2010-08-22 22:44:42 -04002256 return false;
2257 } // if
2258 return true;
2259} // GPTData::ValidPartNum
2260
srs56945a081752010-09-24 20:39:41 -04002261// Return a single partition for inspection (not modification!) by other
2262// functions.
2263const GPTPart & GPTData::operator[](uint32_t partNum) const {
2264 if (partNum >= numParts) {
2265 cerr << "Partition number out of range: " << partNum << "\n";
2266 partNum = 0;
2267 } // if
2268 return partitions[partNum];
2269} // operator[]
2270
2271// Return (not for modification!) the disk's GUID value
2272const GUIDData & GPTData::GetDiskGUID(void) const {
2273 return mainHeader.diskGUID;
2274} // GPTData::GetDiskGUID()
2275
srs56949ddc14b2010-08-22 22:44:42 -04002276// Manage attributes for a partition, based on commands passed to this function.
2277// (Function is non-interactive.)
2278// Returns 1 if a modification command succeeded, 0 if the command should not have
2279// modified data, and -1 if a modification command failed.
2280int GPTData::ManageAttributes(int partNum, const string & command, const string & bits) {
2281 int retval = 0;
2282 Attributes theAttr;
2283
2284 if (command == "show") {
2285 ShowAttributes(partNum);
2286 } else if (command == "get") {
2287 GetAttribute(partNum, bits);
2288 } else {
2289 theAttr = partitions[partNum].GetAttributes();
2290 if (theAttr.OperateOnAttributes(partNum, command, bits)) {
2291 partitions[partNum].SetAttributes(theAttr.GetAttributes());
2292 retval = 1;
2293 } else {
2294 retval = -1;
2295 } // if/else
2296 } // if/elseif/else
2297
2298 return retval;
2299} // GPTData::ManageAttributes()
2300
2301// Show all attributes for a specified partition....
2302void GPTData::ShowAttributes(const uint32_t partNum) {
srs56940873e9d2010-10-07 13:00:45 -04002303 partitions[partNum].ShowAttributes(partNum);
srs56949ddc14b2010-08-22 22:44:42 -04002304} // GPTData::ShowAttributes
2305
2306// Show whether a single attribute bit is set (terse output)...
2307void GPTData::GetAttribute(const uint32_t partNum, const string& attributeBits) {
srs56940873e9d2010-10-07 13:00:45 -04002308 partitions[partNum].GetAttributes().OperateOnAttributes(partNum, "get", attributeBits);
srs56949ddc14b2010-08-22 22:44:42 -04002309} // GPTData::GetAttribute
2310
2311
srs56942a9f5da2009-08-26 00:48:01 -04002312/******************************************
2313 * *
2314 * Additional non-class support functions *
2315 * *
2316 ******************************************/
2317
srs5694e7b4ff92009-08-18 13:16:10 -04002318// Check to be sure that data type sizes are correct. The basic types (uint*_t) should
2319// never fail these tests, but the struct types may fail depending on compile options.
2320// Specifically, the -fpack-struct option to gcc may be required to ensure proper structure
2321// sizes.
2322int SizesOK(void) {
2323 int allOK = 1;
srs5694e7b4ff92009-08-18 13:16:10 -04002324
2325 if (sizeof(uint8_t) != 1) {
srs5694fed16d02010-01-27 23:03:40 -05002326 cerr << "uint8_t is " << sizeof(uint8_t) << " bytes, should be 1 byte; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002327 allOK = 0;
2328 } // if
2329 if (sizeof(uint16_t) != 2) {
srs5694fed16d02010-01-27 23:03:40 -05002330 cerr << "uint16_t is " << sizeof(uint16_t) << " bytes, should be 2 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002331 allOK = 0;
2332 } // if
2333 if (sizeof(uint32_t) != 4) {
srs5694fed16d02010-01-27 23:03:40 -05002334 cerr << "uint32_t is " << sizeof(uint32_t) << " bytes, should be 4 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002335 allOK = 0;
2336 } // if
2337 if (sizeof(uint64_t) != 8) {
srs5694fed16d02010-01-27 23:03:40 -05002338 cerr << "uint64_t is " << sizeof(uint64_t) << " bytes, should be 8 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002339 allOK = 0;
2340 } // if
2341 if (sizeof(struct MBRRecord) != 16) {
srs5694fed16d02010-01-27 23:03:40 -05002342 cerr << "MBRRecord is " << sizeof(MBRRecord) << " bytes, should be 16 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002343 allOK = 0;
2344 } // if
srs5694978041c2009-09-21 20:51:47 -04002345 if (sizeof(struct TempMBR) != 512) {
srs5694fed16d02010-01-27 23:03:40 -05002346 cerr << "TempMBR is " << sizeof(TempMBR) << " bytes, should be 512 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002347 allOK = 0;
2348 } // if
2349 if (sizeof(struct GPTHeader) != 512) {
srs5694fed16d02010-01-27 23:03:40 -05002350 cerr << "GPTHeader is " << sizeof(GPTHeader) << " bytes, should be 512 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002351 allOK = 0;
2352 } // if
srs5694221e0872009-08-29 15:00:31 -04002353 if (sizeof(GPTPart) != 128) {
srs5694fed16d02010-01-27 23:03:40 -05002354 cerr << "GPTPart is " << sizeof(GPTPart) << " bytes, should be 128 bytes; aborting!\n";
srs5694221e0872009-08-29 15:00:31 -04002355 allOK = 0;
2356 } // if
srs56946699b012010-02-04 00:55:30 -05002357 if (sizeof(GUIDData) != 16) {
2358 cerr << "GUIDData is " << sizeof(GUIDData) << " bytes, should be 16 bytes; aborting!\n";
2359 allOK = 0;
2360 } // if
2361 if (sizeof(PartType) != 16) {
2362 cerr << "PartType is " << sizeof(GUIDData) << " bytes, should be 16 bytes; aborting!\n";
2363 allOK = 0;
2364 } // if
srs5694fed16d02010-01-27 23:03:40 -05002365 // Determine endianness; warn user if running on big-endian (PowerPC, etc.) hardware
srs56940873e9d2010-10-07 13:00:45 -04002366// if (IsLittleEndian() == 0) {
2367// cerr << "\aRunning on big-endian hardware. Big-endian support is new and poorly"
2368// " tested!\n";
2369// } // if
srs5694e7b4ff92009-08-18 13:16:10 -04002370 return (allOK);
2371} // SizesOK()
srs5694e4ac11e2009-08-31 10:13:04 -04002372