blob: c28e39d072f561215537c0f2941920fb3efb98d6 [file] [log] [blame]
srs5694e7b4ff92009-08-18 13:16:10 -04001/* gpt.cc -- Functions for loading, saving, and manipulating legacy MBR and GPT partition
2 data. */
3
srs5694e4ac11e2009-08-31 10:13:04 -04004/* By Rod Smith, initial coding January to February, 2009 */
srs5694e7b4ff92009-08-18 13:16:10 -04005
srs569464cbd172011-03-01 22:03:54 -05006/* This program is copyright (c) 2009-2011 by Roderick W. Smith. It is distributed
srs5694221e0872009-08-29 15:00:31 -04007 under the terms of the GNU GPL version 2, as detailed in the COPYING file. */
8
srs5694e7b4ff92009-08-18 13:16:10 -04009#define __STDC_LIMIT_MACROS
10#define __STDC_CONSTANT_MACROS
11
12#include <stdio.h>
srs5694e7b4ff92009-08-18 13:16:10 -040013#include <stdlib.h>
14#include <stdint.h>
15#include <fcntl.h>
16#include <string.h>
srs5694a8582cf2010-03-19 14:21:59 -040017#include <math.h>
srs5694e7b4ff92009-08-18 13:16:10 -040018#include <time.h>
19#include <sys/stat.h>
20#include <errno.h>
srs5694fed16d02010-01-27 23:03:40 -050021#include <iostream>
srs56949a46b042011-03-15 00:34:10 -040022#include <algorithm>
srs5694e7b4ff92009-08-18 13:16:10 -040023#include "crc32.h"
24#include "gpt.h"
srs5694221e0872009-08-29 15:00:31 -040025#include "bsd.h"
srs5694e7b4ff92009-08-18 13:16:10 -040026#include "support.h"
27#include "parttypes.h"
28#include "attributes.h"
srs5694546a9c72010-01-26 16:00:26 -050029#include "diskio.h"
srs5694bf8950c2011-03-12 01:23:12 -050030//#include "partnotes.h"
srs5694e7b4ff92009-08-18 13:16:10 -040031
32using namespace std;
33
srs56948f1b2d62010-05-23 13:07:19 -040034#ifdef __FreeBSD__
srs56949ba54212010-05-18 23:24:02 -040035#define log2(x) (log(x) / M_LN2)
36#endif // __FreeBSD__
37
srs56948f1b2d62010-05-23 13:07:19 -040038#ifdef _MSC_VER
39#define log2(x) (log((double) x) / log(2.0))
40#endif // Microsoft Visual C++
srs56949ba54212010-05-18 23:24:02 -040041
srs5694e7b4ff92009-08-18 13:16:10 -040042/****************************************
43 * *
44 * GPTData class and related structures *
45 * *
46 ****************************************/
47
srs5694e4ac11e2009-08-31 10:13:04 -040048// Default constructor
srs5694e7b4ff92009-08-18 13:16:10 -040049GPTData::GPTData(void) {
50 blockSize = SECTOR_SIZE; // set a default
51 diskSize = 0;
52 partitions = NULL;
53 state = gpt_valid;
srs5694fed16d02010-01-27 23:03:40 -050054 device = "";
srs56945d58fe02010-01-03 20:57:08 -050055 justLooking = 0;
srs5694e7b4ff92009-08-18 13:16:10 -040056 mainCrcOk = 0;
57 secondCrcOk = 0;
58 mainPartsCrcOk = 0;
59 secondPartsCrcOk = 0;
srs5694221e0872009-08-29 15:00:31 -040060 apmFound = 0;
61 bsdFound = 0;
srs56940873e9d2010-10-07 13:00:45 -040062 sectorAlignment = MIN_AF_ALIGNMENT; // Align partitions on 4096-byte boundaries by default
srs5694ba00fed2010-01-12 18:18:36 -050063 beQuiet = 0;
64 whichWasUsed = use_new;
srs56941e093722010-01-05 00:14:19 -050065 mainHeader.numParts = 0;
srs56940283dae2010-04-28 16:44:34 -040066 numParts = 0;
srs5694e7b4ff92009-08-18 13:16:10 -040067 SetGPTSize(NUM_GPT_ENTRIES);
68} // GPTData default constructor
69
70// The following constructor loads GPT data from a device file
srs5694fed16d02010-01-27 23:03:40 -050071GPTData::GPTData(string filename) {
srs5694e7b4ff92009-08-18 13:16:10 -040072 blockSize = SECTOR_SIZE; // set a default
73 diskSize = 0;
74 partitions = NULL;
75 state = gpt_invalid;
srs5694fed16d02010-01-27 23:03:40 -050076 device = "";
srs56945d58fe02010-01-03 20:57:08 -050077 justLooking = 0;
srs5694e7b4ff92009-08-18 13:16:10 -040078 mainCrcOk = 0;
79 secondCrcOk = 0;
80 mainPartsCrcOk = 0;
81 secondPartsCrcOk = 0;
srs5694221e0872009-08-29 15:00:31 -040082 apmFound = 0;
83 bsdFound = 0;
srs56940873e9d2010-10-07 13:00:45 -040084 sectorAlignment = MIN_AF_ALIGNMENT; // Align partitions on 4096-byte boundaries by default
srs5694ba00fed2010-01-12 18:18:36 -050085 beQuiet = 0;
86 whichWasUsed = use_new;
srs56941e093722010-01-05 00:14:19 -050087 mainHeader.numParts = 0;
srs56940283dae2010-04-28 16:44:34 -040088 numParts = 0;
srs56943c0af382010-01-15 19:19:18 -050089 if (!LoadPartitions(filename))
90 exit(2);
srs5694fed16d02010-01-27 23:03:40 -050091} // GPTData(string filename) constructor
srs5694e7b4ff92009-08-18 13:16:10 -040092
srs5694e4ac11e2009-08-31 10:13:04 -040093// Destructor
srs5694e7b4ff92009-08-18 13:16:10 -040094GPTData::~GPTData(void) {
srs5694cb76c672010-02-11 22:22:22 -050095 delete[] partitions;
srs5694e7b4ff92009-08-18 13:16:10 -040096} // GPTData destructor
97
srs569464cbd172011-03-01 22:03:54 -050098// Assignment operator
99GPTData & GPTData::operator=(const GPTData & orig) {
100 uint32_t i;
101
102 mainHeader = orig.mainHeader;
103 numParts = orig.numParts;
104 secondHeader = orig.secondHeader;
105 protectiveMBR = orig.protectiveMBR;
106 device = orig.device;
107 blockSize = orig.blockSize;
108 diskSize = orig.diskSize;
109 state = orig.state;
110 justLooking = orig.justLooking;
111 mainCrcOk = orig.mainCrcOk;
112 secondCrcOk = orig.secondCrcOk;
113 mainPartsCrcOk = orig.mainPartsCrcOk;
114 secondPartsCrcOk = orig.secondPartsCrcOk;
115 apmFound = orig.apmFound;
116 bsdFound = orig.bsdFound;
117 sectorAlignment = orig.sectorAlignment;
118 beQuiet = orig.beQuiet;
119 whichWasUsed = orig.whichWasUsed;
120
121 myDisk.OpenForRead(orig.myDisk.GetName());
122
123 delete[] partitions;
srs569401f7f082011-03-15 23:53:31 -0400124 partitions = new GPTPart [numParts];
srs56946aae2a92011-06-10 01:16:51 -0400125 if (partitions == NULL) {
srs569464cbd172011-03-01 22:03:54 -0500126 cerr << "Error! Could not allocate memory for partitions in GPTData::operator=()!\n"
srs56946aae2a92011-06-10 01:16:51 -0400127 << "Terminating!\n";
128 exit(1);
129 } // if
130 for (i = 0; i < numParts; i++) {
131 partitions[i] = orig.partitions[i];
132 }
srs569464cbd172011-03-01 22:03:54 -0500133 return *this;
134} // GPTData::operator=()
135
srs5694e4ac11e2009-08-31 10:13:04 -0400136/*********************************************************************
137 * *
138 * Begin functions that verify data, or that adjust the verification *
139 * information (compute CRCs, rebuild headers) *
140 * *
141 *********************************************************************/
srs5694e7b4ff92009-08-18 13:16:10 -0400142
srs5694e4ac11e2009-08-31 10:13:04 -0400143// Perform detailed verification, reporting on any problems found, but
144// do *NOT* recover from these problems. Returns the total number of
145// problems identified.
146int GPTData::Verify(void) {
srs569464cbd172011-03-01 22:03:54 -0500147 int problems = 0, alignProbs = 0;
srs5694e321d442010-01-29 17:44:04 -0500148 uint32_t i, numSegments;
149 uint64_t totalFree, largestSegment;
srs5694e4ac11e2009-08-31 10:13:04 -0400150
151 // First, check for CRC errors in the GPT data....
152 if (!mainCrcOk) {
153 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500154 cout << "\nProblem: The CRC for the main GPT header is invalid. The main GPT header may\n"
155 << "be corrupt. Consider loading the backup GPT header to rebuild the main GPT\n"
156 << "header ('b' on the recovery & transformation menu). This report may be a false\n"
157 << "alarm if you've already corrected other problems.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400158 } // if
159 if (!mainPartsCrcOk) {
160 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500161 cout << "\nProblem: The CRC for the main partition table is invalid. This table may be\n"
162 << "corrupt. Consider loading the backup partition table ('c' on the recovery &\n"
163 << "transformation menu). This report may be a false alarm if you've already\n"
164 << "corrected other problems.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400165 } // if
166 if (!secondCrcOk) {
167 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500168 cout << "\nProblem: The CRC for the backup GPT header is invalid. The backup GPT header\n"
169 << "may be corrupt. Consider using the main GPT header to rebuild the backup GPT\n"
170 << "header ('d' on the recovery & transformation menu). This report may be a false\n"
171 << "alarm if you've already corrected other problems.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400172 } // if
173 if (!secondPartsCrcOk) {
174 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500175 cout << "\nCaution: The CRC for the backup partition table is invalid. This table may\n"
176 << "be corrupt. This program will automatically create a new backup partition\n"
177 << "table when you save your partitions.\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400178 } // if
179
srs5694978041c2009-09-21 20:51:47 -0400180 // Now check that the main and backup headers both point to themselves....
181 if (mainHeader.currentLBA != 1) {
182 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500183 cout << "\nProblem: The main header's self-pointer doesn't point to itself. This problem\n"
184 << "is being automatically corrected, but it may be a symptom of more serious\n"
185 << "problems. Think carefully before saving changes with 'w' or using this disk.\n";
srs5694978041c2009-09-21 20:51:47 -0400186 mainHeader.currentLBA = 1;
187 } // if
188 if (secondHeader.currentLBA != (diskSize - UINT64_C(1))) {
189 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500190 cout << "\nProblem: The secondary header's self-pointer indicates that it doesn't reside\n"
191 << "at the end of the disk. If you've added a disk to a RAID array, use the 'e'\n"
192 << "option on the experts' menu to adjust the secondary header's and partition\n"
193 << "table's locations.\n";
srs5694978041c2009-09-21 20:51:47 -0400194 } // if
195
196 // Now check that critical main and backup GPT entries match each other
srs5694e4ac11e2009-08-31 10:13:04 -0400197 if (mainHeader.currentLBA != secondHeader.backupLBA) {
198 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500199 cout << "\nProblem: main GPT header's current LBA pointer (" << mainHeader.currentLBA
200 << ") doesn't\nmatch the backup GPT header's alternate LBA pointer("
201 << secondHeader.backupLBA << ").\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400202 } // if
203 if (mainHeader.backupLBA != secondHeader.currentLBA) {
204 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500205 cout << "\nProblem: main GPT header's backup LBA pointer (" << mainHeader.backupLBA
206 << ") doesn't\nmatch the backup GPT header's current LBA pointer ("
207 << secondHeader.currentLBA << ").\n"
208 << "The 'e' option on the experts' menu may fix this problem.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400209 } // if
210 if (mainHeader.firstUsableLBA != secondHeader.firstUsableLBA) {
211 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500212 cout << "\nProblem: main GPT header's first usable LBA pointer (" << mainHeader.firstUsableLBA
213 << ") doesn't\nmatch the backup GPT header's first usable LBA pointer ("
214 << secondHeader.firstUsableLBA << ")\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400215 } // if
216 if (mainHeader.lastUsableLBA != secondHeader.lastUsableLBA) {
217 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500218 cout << "\nProblem: main GPT header's last usable LBA pointer (" << mainHeader.lastUsableLBA
219 << ") doesn't\nmatch the backup GPT header's last usable LBA pointer ("
220 << secondHeader.lastUsableLBA << ")\n"
221 << "The 'e' option on the experts' menu can probably fix this problem.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400222 } // if
srs56946699b012010-02-04 00:55:30 -0500223 if ((mainHeader.diskGUID != secondHeader.diskGUID)) {
srs5694e4ac11e2009-08-31 10:13:04 -0400224 problems++;
srs56945a081752010-09-24 20:39:41 -0400225 cout << "\nProblem: main header's disk GUID (" << mainHeader.diskGUID
srs5694fed16d02010-01-27 23:03:40 -0500226 << ") doesn't\nmatch the backup GPT header's disk GUID ("
srs56945a081752010-09-24 20:39:41 -0400227 << secondHeader.diskGUID << ")\n"
srs5694fed16d02010-01-27 23:03:40 -0500228 << "You should use the 'b' or 'd' option on the recovery & transformation menu to\n"
229 << "select one or the other header.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400230 } // if
231 if (mainHeader.numParts != secondHeader.numParts) {
232 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500233 cout << "\nProblem: main GPT header's number of partitions (" << mainHeader.numParts
234 << ") doesn't\nmatch the backup GPT header's number of partitions ("
235 << secondHeader.numParts << ")\n"
236 << "Resizing the partition table ('s' on the experts' menu) may help.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400237 } // if
238 if (mainHeader.sizeOfPartitionEntries != secondHeader.sizeOfPartitionEntries) {
239 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500240 cout << "\nProblem: main GPT header's size of partition entries ("
241 << mainHeader.sizeOfPartitionEntries << ") doesn't\n"
242 << "match the backup GPT header's size of partition entries ("
243 << secondHeader.sizeOfPartitionEntries << ")\n"
244 << "You should use the 'b' or 'd' option on the recovery & transformation menu to\n"
245 << "select one or the other header.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400246 } // if
247
248 // Now check for a few other miscellaneous problems...
249 // Check that the disk size will hold the data...
srs569464cbd172011-03-01 22:03:54 -0500250 if (mainHeader.backupLBA >= diskSize) {
srs5694e4ac11e2009-08-31 10:13:04 -0400251 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500252 cout << "\nProblem: Disk is too small to hold all the data!\n"
253 << "(Disk size is " << diskSize << " sectors, needs to be "
srs569464cbd172011-03-01 22:03:54 -0500254 << mainHeader.backupLBA + UINT64_C(1) << " sectors.)\n"
srs5694fed16d02010-01-27 23:03:40 -0500255 << "The 'e' option on the experts' menu may fix this problem.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400256 } // if
257
258 // Check for overlapping partitions....
259 problems += FindOverlaps();
260
srs569455d92612010-03-07 22:16:07 -0500261 // Check for insane partitions (start after end, hugely big, etc.)
262 problems += FindInsanePartitions();
263
srs5694e4ac11e2009-08-31 10:13:04 -0400264 // Check for mismatched MBR and GPT partitions...
265 problems += FindHybridMismatches();
266
srs5694327129e2010-09-22 01:07:31 -0400267 // Check for MBR-specific problems....
268 problems += VerifyMBR();
269
srs5694e4ac11e2009-08-31 10:13:04 -0400270 // Verify that partitions don't run into GPT data areas....
271 problems += CheckGPTSize();
272
srs56941d1448a2009-12-31 21:20:19 -0500273 // Check that partitions are aligned on proper boundaries (for WD Advanced
274 // Format and similar disks)....
srs56940283dae2010-04-28 16:44:34 -0400275 for (i = 0; i < numParts; i++) {
srs56941d1448a2009-12-31 21:20:19 -0500276 if ((partitions[i].GetFirstLBA() % sectorAlignment) != 0) {
srs5694fed16d02010-01-27 23:03:40 -0500277 cout << "\nCaution: Partition " << i + 1 << " doesn't begin on a "
278 << sectorAlignment << "-sector boundary. This may\nresult "
279 << "in degraded performance on some modern (2009 and later) hard disks.\n";
srs569464cbd172011-03-01 22:03:54 -0500280 alignProbs++;
srs56941d1448a2009-12-31 21:20:19 -0500281 } // if
282 } // for
srs569464cbd172011-03-01 22:03:54 -0500283 if (alignProbs > 0)
284 cout << "\nConsult http://www.ibm.com/developerworks/linux/library/l-4kb-sector-disks/\n"
285 << "for information on disk alignment.\n";
srs56941d1448a2009-12-31 21:20:19 -0500286
srs5694e4ac11e2009-08-31 10:13:04 -0400287 // Now compute available space, but only if no problems found, since
288 // problems could affect the results
289 if (problems == 0) {
290 totalFree = FindFreeBlocks(&numSegments, &largestSegment);
srs569464cbd172011-03-01 22:03:54 -0500291 cout << "\nNo problems found. " << totalFree << " free sectors ("
srs569401f7f082011-03-15 23:53:31 -0400292 << BytesToIeee(totalFree, blockSize) << ") available in "
srs5694fed16d02010-01-27 23:03:40 -0500293 << numSegments << "\nsegments, the largest of which is "
srs569401f7f082011-03-15 23:53:31 -0400294 << largestSegment << " (" << BytesToIeee(largestSegment, blockSize)
srs56940283dae2010-04-28 16:44:34 -0400295 << ") in size.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400296 } else {
srs56940a697312010-01-28 21:10:52 -0500297 cout << "\nIdentified " << problems << " problems!\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400298 } // if/else
srs5694e4ac11e2009-08-31 10:13:04 -0400299
300 return (problems);
301} // GPTData::Verify()
srs5694e7b4ff92009-08-18 13:16:10 -0400302
303// Checks to see if the GPT tables overrun existing partitions; if they
srs5694221e0872009-08-29 15:00:31 -0400304// do, issues a warning but takes no action. Returns number of problems
305// detected (0 if OK, 1 to 2 if problems).
srs5694e7b4ff92009-08-18 13:16:10 -0400306int GPTData::CheckGPTSize(void) {
307 uint64_t overlap, firstUsedBlock, lastUsedBlock;
308 uint32_t i;
srs5694221e0872009-08-29 15:00:31 -0400309 int numProbs = 0;
srs5694e7b4ff92009-08-18 13:16:10 -0400310
311 // first, locate the first & last used blocks
312 firstUsedBlock = UINT64_MAX;
313 lastUsedBlock = 0;
srs56940283dae2010-04-28 16:44:34 -0400314 for (i = 0; i < numParts; i++) {
srs5694221e0872009-08-29 15:00:31 -0400315 if ((partitions[i].GetFirstLBA() < firstUsedBlock) &&
srs5694e4ac11e2009-08-31 10:13:04 -0400316 (partitions[i].GetFirstLBA() != 0))
srs5694221e0872009-08-29 15:00:31 -0400317 firstUsedBlock = partitions[i].GetFirstLBA();
318 if (partitions[i].GetLastLBA() > lastUsedBlock)
319 lastUsedBlock = partitions[i].GetLastLBA();
srs5694e7b4ff92009-08-18 13:16:10 -0400320 } // for
321
322 // If the disk size is 0 (the default), then it means that various
323 // variables aren't yet set, so the below tests will be useless;
324 // therefore we should skip everything
325 if (diskSize != 0) {
326 if (mainHeader.firstUsableLBA > firstUsedBlock) {
327 overlap = mainHeader.firstUsableLBA - firstUsedBlock;
srs5694fed16d02010-01-27 23:03:40 -0500328 cout << "Warning! Main partition table overlaps the first partition by "
329 << overlap << " blocks!\n";
srs5694221e0872009-08-29 15:00:31 -0400330 if (firstUsedBlock > 2) {
srs5694fed16d02010-01-27 23:03:40 -0500331 cout << "Try reducing the partition table size by " << overlap * 4
332 << " entries.\n(Use the 's' item on the experts' menu.)\n";
srs5694221e0872009-08-29 15:00:31 -0400333 } else {
srs5694fed16d02010-01-27 23:03:40 -0500334 cout << "You will need to delete this partition or resize it in another utility.\n";
srs5694221e0872009-08-29 15:00:31 -0400335 } // if/else
336 numProbs++;
srs5694e7b4ff92009-08-18 13:16:10 -0400337 } // Problem at start of disk
338 if (mainHeader.lastUsableLBA < lastUsedBlock) {
339 overlap = lastUsedBlock - mainHeader.lastUsableLBA;
srs569455d92612010-03-07 22:16:07 -0500340 cout << "\nWarning! Secondary partition table overlaps the last partition by\n"
srs5694fed16d02010-01-27 23:03:40 -0500341 << overlap << " blocks!\n";
srs5694221e0872009-08-29 15:00:31 -0400342 if (lastUsedBlock > (diskSize - 2)) {
srs5694fed16d02010-01-27 23:03:40 -0500343 cout << "You will need to delete this partition or resize it in another utility.\n";
srs5694221e0872009-08-29 15:00:31 -0400344 } else {
srs5694fed16d02010-01-27 23:03:40 -0500345 cout << "Try reducing the partition table size by " << overlap * 4
346 << " entries.\n(Use the 's' item on the experts' menu.)\n";
srs5694221e0872009-08-29 15:00:31 -0400347 } // if/else
348 numProbs++;
srs5694e7b4ff92009-08-18 13:16:10 -0400349 } // Problem at end of disk
350 } // if (diskSize != 0)
srs5694221e0872009-08-29 15:00:31 -0400351 return numProbs;
srs5694e7b4ff92009-08-18 13:16:10 -0400352} // GPTData::CheckGPTSize()
353
srs5694e7b4ff92009-08-18 13:16:10 -0400354// Check the validity of the GPT header. Returns 1 if the main header
355// is valid, 2 if the backup header is valid, 3 if both are valid, and
356// 0 if neither is valid. Note that this function just checks the GPT
357// signature and revision numbers, not CRCs or other data.
358int GPTData::CheckHeaderValidity(void) {
359 int valid = 3;
360
srs5694fed16d02010-01-27 23:03:40 -0500361 cout.setf(ios::uppercase);
362 cout.fill('0');
363
364 // Note: failed GPT signature checks produce no error message because
365 // a message is displayed in the ReversePartitionBytes() function
srs5694e7b4ff92009-08-18 13:16:10 -0400366 if (mainHeader.signature != GPT_SIGNATURE) {
367 valid -= 1;
srs5694e7b4ff92009-08-18 13:16:10 -0400368 } else if ((mainHeader.revision != 0x00010000) && valid) {
369 valid -= 1;
srs5694fed16d02010-01-27 23:03:40 -0500370 cout << "Unsupported GPT version in main header; read 0x";
371 cout.width(8);
372 cout << hex << mainHeader.revision << ", should be\n0x";
373 cout.width(8);
374 cout << UINT32_C(0x00010000) << dec << "\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400375 } // if/else/if
376
377 if (secondHeader.signature != GPT_SIGNATURE) {
378 valid -= 2;
srs5694e7b4ff92009-08-18 13:16:10 -0400379 } else if ((secondHeader.revision != 0x00010000) && valid) {
380 valid -= 2;
srs5694fed16d02010-01-27 23:03:40 -0500381 cout << "Unsupported GPT version in backup header; read 0x";
382 cout.width(8);
383 cout << hex << secondHeader.revision << ", should be\n0x";
384 cout.width(8);
385 cout << UINT32_C(0x00010000) << dec << "\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400386 } // if/else/if
387
srs5694df9d3632011-01-08 18:33:24 -0500388 // Check for an Apple disk signature
389 if (((mainHeader.signature << 32) == APM_SIGNATURE1) ||
390 (mainHeader.signature << 32) == APM_SIGNATURE2) {
srs5694221e0872009-08-29 15:00:31 -0400391 apmFound = 1; // Will display warning message later
srs56943f2fe992009-11-24 18:28:18 -0500392 } // if
srs5694fed16d02010-01-27 23:03:40 -0500393 cout.fill(' ');
srs56942a9f5da2009-08-26 00:48:01 -0400394
srs5694fed16d02010-01-27 23:03:40 -0500395 return valid;
srs5694e7b4ff92009-08-18 13:16:10 -0400396} // GPTData::CheckHeaderValidity()
397
398// Check the header CRC to see if it's OK...
srs5694cb76c672010-02-11 22:22:22 -0500399// Note: Must be called with header in LITTLE-ENDIAN
400// (x86, x86-64, etc.) byte order.
srs5694e7b4ff92009-08-18 13:16:10 -0400401int GPTData::CheckHeaderCRC(struct GPTHeader* header) {
srs5694978041c2009-09-21 20:51:47 -0400402 uint32_t oldCRC, newCRC, hSize;
srs5694e7b4ff92009-08-18 13:16:10 -0400403
srs56942a9f5da2009-08-26 00:48:01 -0400404 // Back up old header CRC and then blank it, since it must be 0 for
srs5694e7b4ff92009-08-18 13:16:10 -0400405 // computation to be valid
406 oldCRC = header->headerCRC;
407 header->headerCRC = UINT32_C(0);
srs5694978041c2009-09-21 20:51:47 -0400408 hSize = header->headerSize;
409
410 // If big-endian system, reverse byte order
411 if (IsLittleEndian() == 0) {
412 ReverseBytes(&oldCRC, 4);
413 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400414
415 // Initialize CRC functions...
416 chksum_crc32gentab();
417
418 // Compute CRC, restore original, and return result of comparison
419 newCRC = chksum_crc32((unsigned char*) header, HEADER_SIZE);
srs5694978041c2009-09-21 20:51:47 -0400420 header->headerCRC = oldCRC;
srs5694e7b4ff92009-08-18 13:16:10 -0400421 return (oldCRC == newCRC);
422} // GPTData::CheckHeaderCRC()
423
srs56946699b012010-02-04 00:55:30 -0500424// Recompute all the CRCs. Must be called before saving if any changes have
425// been made. Must be called on platform-ordered data (this function reverses
426// byte order and then undoes that reversal.)
srs5694e7b4ff92009-08-18 13:16:10 -0400427void GPTData::RecomputeCRCs(void) {
srs56940283dae2010-04-28 16:44:34 -0400428 uint32_t crc, hSize;
srs56942a9f5da2009-08-26 00:48:01 -0400429 int littleEndian = 1;
srs5694e7b4ff92009-08-18 13:16:10 -0400430
431 // Initialize CRC functions...
432 chksum_crc32gentab();
433
srs56946699b012010-02-04 00:55:30 -0500434 // Save some key data from header before reversing byte order....
srs5694978041c2009-09-21 20:51:47 -0400435 hSize = mainHeader.headerSize;
srs56946699b012010-02-04 00:55:30 -0500436
437 if ((littleEndian = IsLittleEndian()) == 0) {
438 ReversePartitionBytes();
439 ReverseHeaderBytes(&mainHeader);
440 ReverseHeaderBytes(&secondHeader);
441 } // if
srs56942a9f5da2009-08-26 00:48:01 -0400442
srs5694e7b4ff92009-08-18 13:16:10 -0400443 // Compute CRC of partition tables & store in main and secondary headers
srs56940283dae2010-04-28 16:44:34 -0400444 crc = chksum_crc32((unsigned char*) partitions, numParts * GPT_SIZE);
srs5694e7b4ff92009-08-18 13:16:10 -0400445 mainHeader.partitionEntriesCRC = crc;
446 secondHeader.partitionEntriesCRC = crc;
srs56942a9f5da2009-08-26 00:48:01 -0400447 if (littleEndian == 0) {
srs5694221e0872009-08-29 15:00:31 -0400448 ReverseBytes(&mainHeader.partitionEntriesCRC, 4);
449 ReverseBytes(&secondHeader.partitionEntriesCRC, 4);
srs56942a9f5da2009-08-26 00:48:01 -0400450 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400451
452 // Zero out GPT tables' own CRCs (required for correct computation)
453 mainHeader.headerCRC = 0;
454 secondHeader.headerCRC = 0;
455
456 // Compute & store CRCs of main & secondary headers...
srs5694978041c2009-09-21 20:51:47 -0400457 crc = chksum_crc32((unsigned char*) &mainHeader, hSize);
srs56942a9f5da2009-08-26 00:48:01 -0400458 if (littleEndian == 0)
srs5694221e0872009-08-29 15:00:31 -0400459 ReverseBytes(&crc, 4);
srs5694e7b4ff92009-08-18 13:16:10 -0400460 mainHeader.headerCRC = crc;
srs5694978041c2009-09-21 20:51:47 -0400461 crc = chksum_crc32((unsigned char*) &secondHeader, hSize);
srs56942a9f5da2009-08-26 00:48:01 -0400462 if (littleEndian == 0)
srs5694221e0872009-08-29 15:00:31 -0400463 ReverseBytes(&crc, 4);
srs5694e7b4ff92009-08-18 13:16:10 -0400464 secondHeader.headerCRC = crc;
srs56946699b012010-02-04 00:55:30 -0500465
466 if ((littleEndian = IsLittleEndian()) == 0) {
467 ReverseHeaderBytes(&mainHeader);
468 ReverseHeaderBytes(&secondHeader);
469 ReversePartitionBytes();
470 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400471} // GPTData::RecomputeCRCs()
472
srs5694e7b4ff92009-08-18 13:16:10 -0400473// Rebuild the main GPT header, using the secondary header as a model.
474// Typically called when the main header has been found to be corrupt.
475void GPTData::RebuildMainHeader(void) {
srs5694e7b4ff92009-08-18 13:16:10 -0400476 mainHeader.signature = GPT_SIGNATURE;
477 mainHeader.revision = secondHeader.revision;
srs5694978041c2009-09-21 20:51:47 -0400478 mainHeader.headerSize = secondHeader.headerSize;
srs5694e7b4ff92009-08-18 13:16:10 -0400479 mainHeader.headerCRC = UINT32_C(0);
480 mainHeader.reserved = secondHeader.reserved;
481 mainHeader.currentLBA = secondHeader.backupLBA;
482 mainHeader.backupLBA = secondHeader.currentLBA;
483 mainHeader.firstUsableLBA = secondHeader.firstUsableLBA;
484 mainHeader.lastUsableLBA = secondHeader.lastUsableLBA;
srs56946699b012010-02-04 00:55:30 -0500485 mainHeader.diskGUID = secondHeader.diskGUID;
srs5694e7b4ff92009-08-18 13:16:10 -0400486 mainHeader.partitionEntriesLBA = UINT64_C(2);
487 mainHeader.numParts = secondHeader.numParts;
488 mainHeader.sizeOfPartitionEntries = secondHeader.sizeOfPartitionEntries;
489 mainHeader.partitionEntriesCRC = secondHeader.partitionEntriesCRC;
srs569401f7f082011-03-15 23:53:31 -0400490 memcpy(mainHeader.reserved2, secondHeader.reserved2, sizeof(mainHeader.reserved2));
srs5694546a9c72010-01-26 16:00:26 -0500491 mainCrcOk = secondCrcOk;
srs56940283dae2010-04-28 16:44:34 -0400492 SetGPTSize(mainHeader.numParts);
srs5694e7b4ff92009-08-18 13:16:10 -0400493} // GPTData::RebuildMainHeader()
494
495// Rebuild the secondary GPT header, using the main header as a model.
496void GPTData::RebuildSecondHeader(void) {
srs5694e7b4ff92009-08-18 13:16:10 -0400497 secondHeader.signature = GPT_SIGNATURE;
498 secondHeader.revision = mainHeader.revision;
srs5694978041c2009-09-21 20:51:47 -0400499 secondHeader.headerSize = mainHeader.headerSize;
srs5694e7b4ff92009-08-18 13:16:10 -0400500 secondHeader.headerCRC = UINT32_C(0);
501 secondHeader.reserved = mainHeader.reserved;
502 secondHeader.currentLBA = mainHeader.backupLBA;
503 secondHeader.backupLBA = mainHeader.currentLBA;
504 secondHeader.firstUsableLBA = mainHeader.firstUsableLBA;
505 secondHeader.lastUsableLBA = mainHeader.lastUsableLBA;
srs56946699b012010-02-04 00:55:30 -0500506 secondHeader.diskGUID = mainHeader.diskGUID;
srs5694e7b4ff92009-08-18 13:16:10 -0400507 secondHeader.partitionEntriesLBA = secondHeader.lastUsableLBA + UINT64_C(1);
508 secondHeader.numParts = mainHeader.numParts;
509 secondHeader.sizeOfPartitionEntries = mainHeader.sizeOfPartitionEntries;
510 secondHeader.partitionEntriesCRC = mainHeader.partitionEntriesCRC;
srs569401f7f082011-03-15 23:53:31 -0400511 memcpy(secondHeader.reserved2, mainHeader.reserved2, sizeof(secondHeader.reserved2));
srs5694546a9c72010-01-26 16:00:26 -0500512 secondCrcOk = mainCrcOk;
srs56940283dae2010-04-28 16:44:34 -0400513 SetGPTSize(secondHeader.numParts);
srs5694e4ac11e2009-08-31 10:13:04 -0400514} // GPTData::RebuildSecondHeader()
515
516// Search for hybrid MBR entries that have no corresponding GPT partition.
517// Returns number of such mismatches found
518int GPTData::FindHybridMismatches(void) {
srs5694e321d442010-01-29 17:44:04 -0500519 int i, found, numFound = 0;
520 uint32_t j;
srs5694e4ac11e2009-08-31 10:13:04 -0400521 uint64_t mbrFirst, mbrLast;
522
523 for (i = 0; i < 4; i++) {
524 if ((protectiveMBR.GetType(i) != 0xEE) && (protectiveMBR.GetType(i) != 0x00)) {
525 j = 0;
526 found = 0;
527 do {
528 mbrFirst = (uint64_t) protectiveMBR.GetFirstSector(i);
529 mbrLast = mbrFirst + (uint64_t) protectiveMBR.GetLength(i) - UINT64_C(1);
530 if ((partitions[j].GetFirstLBA() == mbrFirst) &&
531 (partitions[j].GetLastLBA() == mbrLast))
532 found = 1;
533 j++;
srs56940283dae2010-04-28 16:44:34 -0400534 } while ((!found) && (j < numParts));
srs5694e4ac11e2009-08-31 10:13:04 -0400535 if (!found) {
536 numFound++;
srs5694fed16d02010-01-27 23:03:40 -0500537 cout << "\nWarning! Mismatched GPT and MBR partition! MBR partition "
538 << i + 1 << ", of type 0x";
539 cout.fill('0');
540 cout.setf(ios::uppercase);
541 cout.width(2);
542 cout << hex << (int) protectiveMBR.GetType(i) << ",\n"
543 << "has no corresponding GPT partition! You may continue, but this condition\n"
544 << "might cause data loss in the future!\a\n" << dec;
545 cout.fill(' ');
srs5694e4ac11e2009-08-31 10:13:04 -0400546 } // if
547 } // if
548 } // for
549 return numFound;
550} // GPTData::FindHybridMismatches
551
552// Find overlapping partitions and warn user about them. Returns number of
553// overlapping partitions.
554int GPTData::FindOverlaps(void) {
srs5694e321d442010-01-29 17:44:04 -0500555 int problems = 0;
556 uint32_t i, j;
srs5694e4ac11e2009-08-31 10:13:04 -0400557
srs56940283dae2010-04-28 16:44:34 -0400558 for (i = 1; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -0400559 for (j = 0; j < i; j++) {
srs56940a697312010-01-28 21:10:52 -0500560 if (partitions[i].DoTheyOverlap(partitions[j])) {
srs5694e4ac11e2009-08-31 10:13:04 -0400561 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500562 cout << "\nProblem: partitions " << i + 1 << " and " << j + 1 << " overlap:\n";
563 cout << " Partition " << i + 1 << ": " << partitions[i].GetFirstLBA()
564 << " to " << partitions[i].GetLastLBA() << "\n";
565 cout << " Partition " << j + 1 << ": " << partitions[j].GetFirstLBA()
566 << " to " << partitions[j].GetLastLBA() << "\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400567 } // if
568 } // for j...
569 } // for i...
570 return problems;
571} // GPTData::FindOverlaps()
572
srs569455d92612010-03-07 22:16:07 -0500573// Find partitions that are insane -- they start after they end or are too
574// big for the disk. (The latter should duplicate detection of overlaps
575// with GPT backup data structures, but better to err on the side of
576// redundant tests than to miss something....)
577int GPTData::FindInsanePartitions(void) {
578 uint32_t i;
579 int problems = 0;
580
srs56940283dae2010-04-28 16:44:34 -0400581 for (i = 0; i < numParts; i++) {
srs569455d92612010-03-07 22:16:07 -0500582 if (partitions[i].GetFirstLBA() > partitions[i].GetLastLBA()) {
583 problems++;
srs56940283dae2010-04-28 16:44:34 -0400584 cout << "\nProblem: partition " << i + 1 << " ends before it begins.\n";
srs569455d92612010-03-07 22:16:07 -0500585 } // if
586 if (partitions[i].GetLastLBA() >= diskSize) {
587 problems++;
srs56940873e9d2010-10-07 13:00:45 -0400588 cout << "\nProblem: partition " << i + 1 << " is too big for the disk.\n";
srs569455d92612010-03-07 22:16:07 -0500589 } // if
590 } // for
591 return problems;
592} // GPTData::FindInsanePartitions(void)
593
594
srs5694e4ac11e2009-08-31 10:13:04 -0400595/******************************************************************
596 * *
597 * Begin functions that load data from disk or save data to disk. *
598 * *
599 ******************************************************************/
600
srs569464cbd172011-03-01 22:03:54 -0500601// Change the filename associated with the GPT. Used for duplicating
602// the partition table to a new disk and saving backups.
603// Returns 1 on success, 0 on failure.
srs5694bf8950c2011-03-12 01:23:12 -0500604int GPTData::SetDisk(const string & deviceFilename) {
srs569464cbd172011-03-01 22:03:54 -0500605 int err, allOK = 1;
606
607 device = deviceFilename;
608 if (allOK && myDisk.OpenForRead(deviceFilename)) {
609 // store disk information....
610 diskSize = myDisk.DiskSize(&err);
611 blockSize = (uint32_t) myDisk.GetBlockSize();
612 } // if
613 protectiveMBR.SetDisk(&myDisk);
614 protectiveMBR.SetDiskSize(diskSize);
615 protectiveMBR.SetBlockSize(blockSize);
616 return allOK;
srs5694bf8950c2011-03-12 01:23:12 -0500617} // GPTData::SetDisk()
srs569464cbd172011-03-01 22:03:54 -0500618
srs5694e4ac11e2009-08-31 10:13:04 -0400619// Scan for partition data. This function loads the MBR data (regular MBR or
620// protective MBR) and loads BSD disklabel data (which is probably invalid).
621// It also looks for APM data, forces a load of GPT data, and summarizes
622// the results.
srs5694546a9c72010-01-26 16:00:26 -0500623void GPTData::PartitionScan(void) {
srs5694e4ac11e2009-08-31 10:13:04 -0400624 BSDData bsdDisklabel;
srs5694e4ac11e2009-08-31 10:13:04 -0400625
626 // Read the MBR & check for BSD disklabel
srs5694546a9c72010-01-26 16:00:26 -0500627 protectiveMBR.ReadMBRData(&myDisk);
628 bsdDisklabel.ReadBSDData(&myDisk, 0, diskSize - 1);
srs5694e4ac11e2009-08-31 10:13:04 -0400629
630 // Load the GPT data, whether or not it's valid
srs5694546a9c72010-01-26 16:00:26 -0500631 ForceLoadGPTData();
srs5694ba00fed2010-01-12 18:18:36 -0500632
633 if (!beQuiet) {
srs5694fed16d02010-01-27 23:03:40 -0500634 cout << "Partition table scan:\n";
srs5694ba00fed2010-01-12 18:18:36 -0500635 protectiveMBR.ShowState();
636 bsdDisklabel.ShowState();
637 ShowAPMState(); // Show whether there's an Apple Partition Map present
638 ShowGPTState(); // Show GPT status
srs5694fed16d02010-01-27 23:03:40 -0500639 cout << "\n";
srs5694ba00fed2010-01-12 18:18:36 -0500640 } // if
srs5694e4ac11e2009-08-31 10:13:04 -0400641
642 if (apmFound) {
srs5694fed16d02010-01-27 23:03:40 -0500643 cout << "\n*******************************************************************\n"
644 << "This disk appears to contain an Apple-format (APM) partition table!\n";
srs56945d58fe02010-01-03 20:57:08 -0500645 if (!justLooking) {
srs5694fed16d02010-01-27 23:03:40 -0500646 cout << "It will be destroyed if you continue!\n";
srs56945d58fe02010-01-03 20:57:08 -0500647 } // if
srs5694fed16d02010-01-27 23:03:40 -0500648 cout << "*******************************************************************\n\n\a";
srs5694e4ac11e2009-08-31 10:13:04 -0400649 } // if
srs5694e4ac11e2009-08-31 10:13:04 -0400650} // GPTData::PartitionScan()
651
652// Read GPT data from a disk.
srs56940a697312010-01-28 21:10:52 -0500653int GPTData::LoadPartitions(const string & deviceFilename) {
srs569408bb0da2010-02-19 17:19:55 -0500654 BSDData bsdDisklabel;
srs5694e321d442010-01-29 17:44:04 -0500655 int err, allOK = 1;
srs5694fed16d02010-01-27 23:03:40 -0500656 MBRValidity mbrState;
srs5694e4ac11e2009-08-31 10:13:04 -0400657
srs5694546a9c72010-01-26 16:00:26 -0500658 if (myDisk.OpenForRead(deviceFilename)) {
srs569455d92612010-03-07 22:16:07 -0500659 err = myDisk.OpenForWrite(deviceFilename);
660 if ((err == 0) && (!justLooking)) {
661 cout << "\aNOTE: Write test failed with error number " << errno
662 << ". It will be impossible to save\nchanges to this disk's partition table!\n";
663#if defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
664 cout << "You may be able to enable writes by exiting this program, typing\n"
665 << "'sysctl kern.geom.debugflags=16' at a shell prompt, and re-running this\n"
666 << "program.\n";
667#endif
668 cout << "\n";
669 } // if
670 myDisk.Close(); // Close and re-open read-only in case of bugs
671 } else allOK = 0; // if
672
673 if (allOK && myDisk.OpenForRead(deviceFilename)) {
srs5694e4ac11e2009-08-31 10:13:04 -0400674 // store disk information....
srs5694546a9c72010-01-26 16:00:26 -0500675 diskSize = myDisk.DiskSize(&err);
676 blockSize = (uint32_t) myDisk.GetBlockSize();
srs5694fed16d02010-01-27 23:03:40 -0500677 device = deviceFilename;
srs5694546a9c72010-01-26 16:00:26 -0500678 PartitionScan(); // Check for partition types, load GPT, & print summary
srs5694e4ac11e2009-08-31 10:13:04 -0400679
srs5694ba00fed2010-01-12 18:18:36 -0500680 whichWasUsed = UseWhichPartitions();
681 switch (whichWasUsed) {
srs5694e4ac11e2009-08-31 10:13:04 -0400682 case use_mbr:
683 XFormPartitions();
684 break;
685 case use_bsd:
srs5694546a9c72010-01-26 16:00:26 -0500686 bsdDisklabel.ReadBSDData(&myDisk, 0, diskSize - 1);
srs5694e4ac11e2009-08-31 10:13:04 -0400687// bsdDisklabel.DisplayBSDData();
688 ClearGPTData();
689 protectiveMBR.MakeProtectiveMBR(1); // clear boot area (option 1)
srs569408bb0da2010-02-19 17:19:55 -0500690 XFormDisklabel(&bsdDisklabel);
srs5694e4ac11e2009-08-31 10:13:04 -0400691 break;
692 case use_gpt:
srs5694fed16d02010-01-27 23:03:40 -0500693 mbrState = protectiveMBR.GetValidity();
694 if ((mbrState == invalid) || (mbrState == mbr))
695 protectiveMBR.MakeProtectiveMBR();
srs5694e4ac11e2009-08-31 10:13:04 -0400696 break;
697 case use_new:
698 ClearGPTData();
699 protectiveMBR.MakeProtectiveMBR();
700 break;
srs56943c0af382010-01-15 19:19:18 -0500701 case use_abort:
702 allOK = 0;
srs56949ddc14b2010-08-22 22:44:42 -0400703 cerr << "Invalid partition data!\n";
srs56943c0af382010-01-15 19:19:18 -0500704 break;
srs5694e4ac11e2009-08-31 10:13:04 -0400705 } // switch
706
srs569455d92612010-03-07 22:16:07 -0500707 if (allOK)
srs56943c0af382010-01-15 19:19:18 -0500708 CheckGPTSize();
srs569455d92612010-03-07 22:16:07 -0500709 myDisk.Close();
srs5694a8582cf2010-03-19 14:21:59 -0400710 ComputeAlignment();
srs5694e4ac11e2009-08-31 10:13:04 -0400711 } else {
712 allOK = 0;
srs5694e4ac11e2009-08-31 10:13:04 -0400713 } // if/else
714 return (allOK);
715} // GPTData::LoadPartitions()
716
717// Loads the GPT, as much as possible. Returns 1 if this seems to have
718// succeeded, 0 if there are obvious problems....
srs5694546a9c72010-01-26 16:00:26 -0500719int GPTData::ForceLoadGPTData(void) {
srs5694cb76c672010-02-11 22:22:22 -0500720 int allOK, validHeaders, loadedTable = 1;
srs5694e4ac11e2009-08-31 10:13:04 -0400721
srs5694cb76c672010-02-11 22:22:22 -0500722 allOK = LoadHeader(&mainHeader, myDisk, 1, &mainCrcOk);
srs5694e4ac11e2009-08-31 10:13:04 -0400723
srs5694cb76c672010-02-11 22:22:22 -0500724 if (mainCrcOk && (mainHeader.backupLBA < diskSize)) {
725 allOK = LoadHeader(&secondHeader, myDisk, mainHeader.backupLBA, &secondCrcOk) && allOK;
726 } else {
srs569408bb0da2010-02-19 17:19:55 -0500727 allOK = LoadHeader(&secondHeader, myDisk, diskSize - UINT64_C(1), &secondCrcOk) && allOK;
728 if (mainCrcOk && (mainHeader.backupLBA >= diskSize))
srs5694fed16d02010-01-27 23:03:40 -0500729 cout << "Warning! Disk size is smaller than the main header indicates! Loading\n"
730 << "secondary header from the last sector of the disk! You should use 'v' to\n"
731 << "verify disk integrity, and perhaps options on the experts' menu to repair\n"
732 << "the disk.\n";
srs5694cb76c672010-02-11 22:22:22 -0500733 } // if/else
734 if (!allOK)
srs5694e4ac11e2009-08-31 10:13:04 -0400735 state = gpt_invalid;
srs5694e4ac11e2009-08-31 10:13:04 -0400736
737 // Return valid headers code: 0 = both headers bad; 1 = main header
738 // good, backup bad; 2 = backup header good, main header bad;
739 // 3 = both headers good. Note these codes refer to valid GPT
740 // signatures and version numbers; more subtle problems will elude
741 // this check!
742 validHeaders = CheckHeaderValidity();
743
744 // Read partitions (from primary array)
745 if (validHeaders > 0) { // if at least one header is OK....
746 // GPT appears to be valid....
747 state = gpt_valid;
748
749 // We're calling the GPT valid, but there's a possibility that one
750 // of the two headers is corrupt. If so, use the one that seems to
751 // be in better shape to regenerate the bad one
srs5694546a9c72010-01-26 16:00:26 -0500752 if (validHeaders == 1) { // valid main header, invalid backup header
srs5694fed16d02010-01-27 23:03:40 -0500753 cerr << "\aCaution: invalid backup GPT header, but valid main header; regenerating\n"
754 << "backup header from main header.\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400755 RebuildSecondHeader();
srs5694546a9c72010-01-26 16:00:26 -0500756 state = gpt_corrupt;
srs5694e4ac11e2009-08-31 10:13:04 -0400757 secondCrcOk = mainCrcOk; // Since regenerated, use CRC validity of main
srs5694546a9c72010-01-26 16:00:26 -0500758 } else if (validHeaders == 2) { // valid backup header, invalid main header
srs5694fed16d02010-01-27 23:03:40 -0500759 cerr << "\aCaution: invalid main GPT header, but valid backup; regenerating main header\n"
760 << "from backup!\n\n";
srs5694546a9c72010-01-26 16:00:26 -0500761 RebuildMainHeader();
762 state = gpt_corrupt;
763 mainCrcOk = secondCrcOk; // Since copied, use CRC validity of backup
srs5694e4ac11e2009-08-31 10:13:04 -0400764 } // if/else/if
765
srs5694546a9c72010-01-26 16:00:26 -0500766 // Figure out which partition table to load....
767 // Load the main partition table, since either its header's CRC is OK or the
768 // backup header's CRC is not OK....
769 if (mainCrcOk || !secondCrcOk) {
770 if (LoadMainTable() == 0)
771 allOK = 0;
772 } else { // bad main header CRC and backup header CRC is OK
773 state = gpt_corrupt;
774 if (LoadSecondTableAsMain()) {
srs5694cb76c672010-02-11 22:22:22 -0500775 loadedTable = 2;
srs5694fed16d02010-01-27 23:03:40 -0500776 cerr << "\aWarning: Invalid CRC on main header data; loaded backup partition table.\n";
srs5694546a9c72010-01-26 16:00:26 -0500777 } else { // backup table bad, bad main header CRC, but try main table in desperation....
778 if (LoadMainTable() == 0) {
779 allOK = 0;
srs5694cb76c672010-02-11 22:22:22 -0500780 loadedTable = 0;
srs5694fed16d02010-01-27 23:03:40 -0500781 cerr << "\a\aWarning! Unable to load either main or backup partition table!\n";
srs5694546a9c72010-01-26 16:00:26 -0500782 } // if
783 } // if/else (LoadSecondTableAsMain())
784 } // if/else (load partition table)
srs5694e4ac11e2009-08-31 10:13:04 -0400785
srs5694cb76c672010-02-11 22:22:22 -0500786 if (loadedTable == 1)
787 secondPartsCrcOk = CheckTable(&secondHeader);
788 else if (loadedTable == 2)
789 mainPartsCrcOk = CheckTable(&mainHeader);
790 else
791 mainPartsCrcOk = secondPartsCrcOk = 0;
srs5694e4ac11e2009-08-31 10:13:04 -0400792
srs5694546a9c72010-01-26 16:00:26 -0500793 // Problem with main partition table; if backup is OK, use it instead....
794 if (secondPartsCrcOk && secondCrcOk && !mainPartsCrcOk) {
795 state = gpt_corrupt;
796 allOK = allOK && LoadSecondTableAsMain();
srs5694cb76c672010-02-11 22:22:22 -0500797 mainPartsCrcOk = 0; // LoadSecondTableAsMain() resets this, so re-flag as bad
srs5694fed16d02010-01-27 23:03:40 -0500798 cerr << "\aWarning! Main partition table CRC mismatch! Loaded backup "
799 << "partition table\ninstead of main partition table!\n\n";
srs5694cb76c672010-02-11 22:22:22 -0500800 } // if */
srs5694546a9c72010-01-26 16:00:26 -0500801
srs5694e4ac11e2009-08-31 10:13:04 -0400802 // Check for valid CRCs and warn if there are problems
803 if ((mainCrcOk == 0) || (secondCrcOk == 0) || (mainPartsCrcOk == 0) ||
804 (secondPartsCrcOk == 0)) {
srs5694fed16d02010-01-27 23:03:40 -0500805 cerr << "Warning! One or more CRCs don't match. You should repair the disk!\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400806 state = gpt_corrupt;
srs5694ba00fed2010-01-12 18:18:36 -0500807 } // if
srs5694e4ac11e2009-08-31 10:13:04 -0400808 } else {
809 state = gpt_invalid;
810 } // if/else
811 return allOK;
812} // GPTData::ForceLoadGPTData()
813
srs5694247657a2009-11-26 18:36:12 -0500814// Loads the partition table pointed to by the main GPT header. The
srs5694e4ac11e2009-08-31 10:13:04 -0400815// main GPT header in memory MUST be valid for this call to do anything
816// sensible!
srs5694546a9c72010-01-26 16:00:26 -0500817// Returns 1 on success, 0 on failure. CRC errors do NOT count as failure.
srs5694e4ac11e2009-08-31 10:13:04 -0400818int GPTData::LoadMainTable(void) {
srs5694cb76c672010-02-11 22:22:22 -0500819 return LoadPartitionTable(mainHeader, myDisk);
srs5694e4ac11e2009-08-31 10:13:04 -0400820} // GPTData::LoadMainTable()
srs5694e7b4ff92009-08-18 13:16:10 -0400821
822// Load the second (backup) partition table as the primary partition
srs5694546a9c72010-01-26 16:00:26 -0500823// table. Used in repair functions, and when starting up if the main
824// partition table is damaged.
825// Returns 1 on success, 0 on failure. CRC errors do NOT count as failure.
826int GPTData::LoadSecondTableAsMain(void) {
srs5694cb76c672010-02-11 22:22:22 -0500827 return LoadPartitionTable(secondHeader, myDisk);
828} // GPTData::LoadSecondTableAsMain()
srs5694e7b4ff92009-08-18 13:16:10 -0400829
srs5694cb76c672010-02-11 22:22:22 -0500830// Load a single GPT header (main or backup) from the specified disk device and
831// sector. Applies byte-order corrections on big-endian platforms. Sets crcOk
832// value appropriately.
833// Returns 1 on success, 0 on failure. Note that CRC errors do NOT qualify as
834// failure.
835int GPTData::LoadHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector, int *crcOk) {
836 int allOK = 1;
srs56941c6f8b02010-02-21 11:09:20 -0500837 GPTHeader tempHeader;
srs5694cb76c672010-02-11 22:22:22 -0500838
839 disk.Seek(sector);
srs56941c6f8b02010-02-21 11:09:20 -0500840 if (disk.Read(&tempHeader, 512) != 512) {
srs5694cb76c672010-02-11 22:22:22 -0500841 cerr << "Warning! Read error " << errno << "; strange behavior now likely!\n";
842 allOK = 0;
843 } // if
srs56941c6f8b02010-02-21 11:09:20 -0500844 *crcOk = CheckHeaderCRC(&tempHeader);
srs5694cb76c672010-02-11 22:22:22 -0500845
srs56941c6f8b02010-02-21 11:09:20 -0500846 // Reverse byte order, if necessary
srs5694cb76c672010-02-11 22:22:22 -0500847 if (IsLittleEndian() == 0) {
srs569455d92612010-03-07 22:16:07 -0500848 ReverseHeaderBytes(&tempHeader);
srs5694cb76c672010-02-11 22:22:22 -0500849 } // if
srs56941c6f8b02010-02-21 11:09:20 -0500850
srs56940283dae2010-04-28 16:44:34 -0400851 if (allOK && (numParts != tempHeader.numParts) && *crcOk) {
srs56941c6f8b02010-02-21 11:09:20 -0500852 allOK = SetGPTSize(tempHeader.numParts);
srs569455d92612010-03-07 22:16:07 -0500853 }
srs56941c6f8b02010-02-21 11:09:20 -0500854
855 *header = tempHeader;
srs5694cb76c672010-02-11 22:22:22 -0500856 return allOK;
857} // GPTData::LoadHeader
858
859// Load a partition table (either main or secondary) from the specified disk,
860// using header as a reference for what to load. If sector != 0 (the default
861// is 0), loads from the specified sector; otherwise loads from the sector
862// indicated in header.
863// Returns 1 on success, 0 on failure. CRC errors do NOT count as failure.
864int GPTData::LoadPartitionTable(const struct GPTHeader & header, DiskIO & disk, uint64_t sector) {
865 uint32_t sizeOfParts, newCRC;
866 int retval;
867
868 if (disk.OpenForRead()) {
869 if (sector == 0) {
870 retval = disk.Seek(header.partitionEntriesLBA);
871 } else {
872 retval = disk.Seek(sector);
873 } // if/else
srs569455d92612010-03-07 22:16:07 -0500874 if (retval == 1)
875 retval = SetGPTSize(header.numParts);
srs5694546a9c72010-01-26 16:00:26 -0500876 if (retval == 1) {
srs5694cb76c672010-02-11 22:22:22 -0500877 sizeOfParts = header.numParts * header.sizeOfPartitionEntries;
878 if (disk.Read(partitions, sizeOfParts) != (int) sizeOfParts) {
srs5694fed16d02010-01-27 23:03:40 -0500879 cerr << "Warning! Read error " << errno << "! Misbehavior now likely!\n";
srs5694546a9c72010-01-26 16:00:26 -0500880 retval = 0;
srs56945d58fe02010-01-03 20:57:08 -0500881 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400882 newCRC = chksum_crc32((unsigned char*) partitions, sizeOfParts);
srs5694cb76c672010-02-11 22:22:22 -0500883 mainPartsCrcOk = secondPartsCrcOk = (newCRC == header.partitionEntriesCRC);
srs56942a9f5da2009-08-26 00:48:01 -0400884 if (IsLittleEndian() == 0)
885 ReversePartitionBytes();
srs5694cb76c672010-02-11 22:22:22 -0500886 if (!mainPartsCrcOk) {
887 cout << "Caution! After loading partitions, the CRC doesn't check out!\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400888 } // if
889 } else {
srs5694cb76c672010-02-11 22:22:22 -0500890 cerr << "Error! Couldn't seek to partition table!\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400891 } // if/else
892 } else {
srs5694fed16d02010-01-27 23:03:40 -0500893 cerr << "Error! Couldn't open device " << device
srs5694cb76c672010-02-11 22:22:22 -0500894 << " when reading partition table!\n";
srs5694546a9c72010-01-26 16:00:26 -0500895 retval = 0;
srs5694e7b4ff92009-08-18 13:16:10 -0400896 } // if/else
srs5694546a9c72010-01-26 16:00:26 -0500897 return retval;
srs5694cb76c672010-02-11 22:22:22 -0500898} // GPTData::LoadPartitionsTable()
899
900// Check the partition table pointed to by header, but don't keep it
901// around.
902// Returns 1 if the CRC is OK, 0 if not or if there was a read error.
903int GPTData::CheckTable(struct GPTHeader *header) {
904 uint32_t sizeOfParts, newCRC;
905 uint8_t *storage;
906 int newCrcOk = 0;
907
srs56940283dae2010-04-28 16:44:34 -0400908 // Load partition table into temporary storage to check
srs5694cb76c672010-02-11 22:22:22 -0500909 // its CRC and store the results, then discard this temporary
910 // storage, since we don't use it in any but recovery operations
911 if (myDisk.Seek(header->partitionEntriesLBA)) {
srs56940283dae2010-04-28 16:44:34 -0400912 sizeOfParts = header->numParts * header->sizeOfPartitionEntries;
srs5694cb76c672010-02-11 22:22:22 -0500913 storage = new uint8_t[sizeOfParts];
srs56946aae2a92011-06-10 01:16:51 -0400914 if (storage == NULL) {
915 cerr << "Could not allocate memory in GPTData::CheckTable()! Terminating!\n";
916 exit(1);
917 } // if
srs5694cb76c672010-02-11 22:22:22 -0500918 if (myDisk.Read(storage, sizeOfParts) != (int) sizeOfParts) {
srs56940283dae2010-04-28 16:44:34 -0400919 cerr << "Warning! Error " << errno << " reading partition table for CRC check!\n";
srs5694cb76c672010-02-11 22:22:22 -0500920 } else {
921 newCRC = chksum_crc32((unsigned char*) storage, sizeOfParts);
922 newCrcOk = (newCRC == header->partitionEntriesCRC);
923 } // if/else
924 delete[] storage;
925 } // if
926 return newCrcOk;
927} // GPTData::CheckTable()
srs5694e7b4ff92009-08-18 13:16:10 -0400928
srs569464cbd172011-03-01 22:03:54 -0500929// Writes GPT (and protective MBR) to disk. If quiet==1,
930// Returns 1 on successful
srs5694e7b4ff92009-08-18 13:16:10 -0400931// write, 0 if there was a problem.
srs569464cbd172011-03-01 22:03:54 -0500932int GPTData::SaveGPTData(int quiet) {
srs56946699b012010-02-04 00:55:30 -0500933 int allOK = 1, littleEndian;
srs5694e321d442010-01-29 17:44:04 -0500934 char answer;
srs5694e7b4ff92009-08-18 13:16:10 -0400935
srs56946699b012010-02-04 00:55:30 -0500936 littleEndian = IsLittleEndian();
937
srs5694e7b4ff92009-08-18 13:16:10 -0400938 // First do some final sanity checks....
srs56945d58fe02010-01-03 20:57:08 -0500939
940 // This test should only fail on read-only disks....
941 if (justLooking) {
srs5694fed16d02010-01-27 23:03:40 -0500942 cout << "The justLooking flag is set. This probably means you can't write to the disk.\n";
srs56945d58fe02010-01-03 20:57:08 -0500943 allOK = 0;
944 } // if
945
srs569464cbd172011-03-01 22:03:54 -0500946 // Check that disk is really big enough to handle the second header...
947 if (mainHeader.backupLBA >= diskSize) {
948 cerr << "Caution! Secondary header was placed beyond the disk's limits! Moving the\n"
949 << "header, but other problems may occur!\n";
950 MoveSecondHeaderToEnd();
951 } // if
952
srs5694e7b4ff92009-08-18 13:16:10 -0400953 // Is there enough space to hold the GPT headers and partition tables,
954 // given the partition sizes?
srs5694221e0872009-08-29 15:00:31 -0400955 if (CheckGPTSize() > 0) {
srs5694e7b4ff92009-08-18 13:16:10 -0400956 allOK = 0;
957 } // if
958
srs5694247657a2009-11-26 18:36:12 -0500959 // Check that second header is properly placed. Warn and ask if this should
960 // be corrected if the test fails....
srs569464cbd172011-03-01 22:03:54 -0500961 if (mainHeader.backupLBA < (diskSize - UINT64_C(1))) {
962 if (quiet == 0) {
963 cout << "Warning! Secondary header is placed too early on the disk! Do you want to\n"
964 << "correct this problem? ";
965 if (GetYN() == 'Y') {
966 MoveSecondHeaderToEnd();
967 cout << "Have moved second header and partition table to correct location.\n";
968 } else {
969 cout << "Have not corrected the problem. Strange problems may occur in the future!\n";
970 } // if correction requested
971 } else { // Go ahead and do correction automatically
srs5694247657a2009-11-26 18:36:12 -0500972 MoveSecondHeaderToEnd();
srs569464cbd172011-03-01 22:03:54 -0500973 } // if/else quiet
srs5694247657a2009-11-26 18:36:12 -0500974 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400975
srs569455d92612010-03-07 22:16:07 -0500976 // Check for overlapping or insane partitions....
977 if ((FindOverlaps() > 0) || (FindInsanePartitions() > 0)) {
srs5694e4ac11e2009-08-31 10:13:04 -0400978 allOK = 0;
srs5694fed16d02010-01-27 23:03:40 -0500979 cerr << "Aborting write operation!\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400980 } // if
981
982 // Check for mismatched MBR and GPT data, but let it pass if found
983 // (function displays warning message)
984 FindHybridMismatches();
srs5694e7b4ff92009-08-18 13:16:10 -0400985
986 RecomputeCRCs();
987
srs5694ba00fed2010-01-12 18:18:36 -0500988 if ((allOK) && (!quiet)) {
srs5694fed16d02010-01-27 23:03:40 -0500989 cout << "\nFinal checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING\n"
srs5694bf8950c2011-03-12 01:23:12 -0500990 << "PARTITIONS!!\n\nDo you want to proceed? ";
srs56945d58fe02010-01-03 20:57:08 -0500991 answer = GetYN();
992 if (answer == 'Y') {
srs5694fed16d02010-01-27 23:03:40 -0500993 cout << "OK; writing new GUID partition table (GPT).\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400994 } else {
995 allOK = 0;
996 } // if/else
997 } // if
998
999 // Do it!
1000 if (allOK) {
srs569464cbd172011-03-01 22:03:54 -05001001 if (myDisk.OpenForWrite()) {
srs56948a4ddfc2010-03-21 19:05:49 -04001002 // As per UEFI specs, write the secondary table and GPT first....
srs5694cb76c672010-02-11 22:22:22 -05001003 allOK = SavePartitionTable(myDisk, secondHeader.partitionEntriesLBA);
1004 if (!allOK)
1005 cerr << "Unable to save backup partition table! Perhaps the 'e' option on the experts'\n"
1006 << "menu will resolve this problem.\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001007
1008 // Now write the secondary GPT header...
srs56948a4ddfc2010-03-21 19:05:49 -04001009 allOK = allOK && SaveHeader(&secondHeader, myDisk, mainHeader.backupLBA);
1010
1011 // Now write the main partition tables...
1012 allOK = allOK && SavePartitionTable(myDisk, mainHeader.partitionEntriesLBA);
1013
1014 // Now write the main GPT header...
1015 allOK = allOK && SaveHeader(&mainHeader, myDisk, 1);
1016
1017 // To top it off, write the protective MBR...
1018 allOK = allOK && protectiveMBR.WriteMBRData(&myDisk);
srs5694e7b4ff92009-08-18 13:16:10 -04001019
1020 // re-read the partition table
1021 if (allOK) {
srs5694546a9c72010-01-26 16:00:26 -05001022 myDisk.DiskSync();
srs5694e7b4ff92009-08-18 13:16:10 -04001023 } // if
1024
1025 if (allOK) { // writes completed OK
srs5694fed16d02010-01-27 23:03:40 -05001026 cout << "The operation has completed successfully.\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001027 } else {
srs5694fed16d02010-01-27 23:03:40 -05001028 cerr << "Warning! An error was reported when writing the partition table! This error\n"
srs56948a4ddfc2010-03-21 19:05:49 -04001029 << "MIGHT be harmless, but you may have trashed the disk!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001030 } // if/else
srs56948a4ddfc2010-03-21 19:05:49 -04001031
srs5694546a9c72010-01-26 16:00:26 -05001032 myDisk.Close();
srs5694e7b4ff92009-08-18 13:16:10 -04001033 } else {
srs56945a608532011-03-17 13:53:01 -04001034 cerr << "Unable to open device '" << myDisk.GetName() << "' for writing! Errno is "
srs5694fed16d02010-01-27 23:03:40 -05001035 << errno << "! Aborting write!\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001036 allOK = 0;
srs5694e7b4ff92009-08-18 13:16:10 -04001037 } // if/else
1038 } else {
srs5694fed16d02010-01-27 23:03:40 -05001039 cout << "Aborting write of new partition table.\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001040 } // if
1041
1042 return (allOK);
1043} // GPTData::SaveGPTData()
1044
1045// Save GPT data to a backup file. This function does much less error
1046// checking than SaveGPTData(). It can therefore preserve many types of
1047// corruption for later analysis; however, it preserves only the MBR,
1048// the main GPT header, the backup GPT header, and the main partition
1049// table; it discards the backup partition table, since it should be
1050// identical to the main partition table on healthy disks.
srs56940a697312010-01-28 21:10:52 -05001051int GPTData::SaveGPTBackup(const string & filename) {
1052 int allOK = 1;
srs5694546a9c72010-01-26 16:00:26 -05001053 DiskIO backupFile;
srs5694e7b4ff92009-08-18 13:16:10 -04001054
srs5694546a9c72010-01-26 16:00:26 -05001055 if (backupFile.OpenForWrite(filename)) {
srs56946699b012010-02-04 00:55:30 -05001056 // Recomputing the CRCs is likely to alter them, which could be bad
1057 // if the intent is to save a potentially bad GPT for later analysis;
1058 // but if we don't do this, we get bogus errors when we load the
1059 // backup. I'm favoring misses over false alarms....
1060 RecomputeCRCs();
1061
srs5694546a9c72010-01-26 16:00:26 -05001062 protectiveMBR.WriteMBRData(&backupFile);
srs5694699941e2011-03-21 21:33:57 -04001063 protectiveMBR.SetDisk(&myDisk);
srs5694e7b4ff92009-08-18 13:16:10 -04001064
srs5694cb76c672010-02-11 22:22:22 -05001065 if (allOK) {
srs5694546a9c72010-01-26 16:00:26 -05001066 // MBR write closed disk, so re-open and seek to end....
1067 backupFile.OpenForWrite();
srs5694cb76c672010-02-11 22:22:22 -05001068 allOK = SaveHeader(&mainHeader, backupFile, 1);
1069 } // if (allOK)
srs5694e7b4ff92009-08-18 13:16:10 -04001070
srs5694e7b4ff92009-08-18 13:16:10 -04001071 if (allOK)
srs5694cb76c672010-02-11 22:22:22 -05001072 allOK = SaveHeader(&secondHeader, backupFile, 2);
srs5694e7b4ff92009-08-18 13:16:10 -04001073
srs5694cb76c672010-02-11 22:22:22 -05001074 if (allOK)
1075 allOK = SavePartitionTable(backupFile, 3);
srs5694e7b4ff92009-08-18 13:16:10 -04001076
1077 if (allOK) { // writes completed OK
srs5694fed16d02010-01-27 23:03:40 -05001078 cout << "The operation has completed successfully.\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001079 } else {
srs5694fed16d02010-01-27 23:03:40 -05001080 cerr << "Warning! An error was reported when writing the backup file.\n"
1081 << "It may not be usable!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001082 } // if/else
srs5694546a9c72010-01-26 16:00:26 -05001083 backupFile.Close();
srs5694e7b4ff92009-08-18 13:16:10 -04001084 } else {
srs56945a608532011-03-17 13:53:01 -04001085 cerr << "Unable to open file '" << filename << "' for writing! Aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001086 allOK = 0;
1087 } // if/else
1088 return allOK;
1089} // GPTData::SaveGPTBackup()
1090
srs5694cb76c672010-02-11 22:22:22 -05001091// Write a GPT header (main or backup) to the specified sector. Used by both
1092// the SaveGPTData() and SaveGPTBackup() functions.
1093// Should be passed an architecture-appropriate header (DO NOT call
1094// ReverseHeaderBytes() on the header before calling this function)
1095// Returns 1 on success, 0 on failure
1096int GPTData::SaveHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector) {
1097 int littleEndian, allOK = 1;
1098
1099 littleEndian = IsLittleEndian();
1100 if (!littleEndian)
1101 ReverseHeaderBytes(header);
1102 if (disk.Seek(sector)) {
1103 if (disk.Write(header, 512) == -1)
1104 allOK = 0;
1105 } else allOK = 0; // if (disk.Seek()...)
1106 if (!littleEndian)
1107 ReverseHeaderBytes(header);
1108 return allOK;
1109} // GPTData::SaveHeader()
1110
1111// Save the partitions to the specified sector. Used by both the SaveGPTData()
1112// and SaveGPTBackup() functions.
1113// Should be passed an architecture-appropriate header (DO NOT call
1114// ReverseHeaderBytes() on the header before calling this function)
1115// Returns 1 on success, 0 on failure
1116int GPTData::SavePartitionTable(DiskIO & disk, uint64_t sector) {
1117 int littleEndian, allOK = 1;
1118
1119 littleEndian = IsLittleEndian();
1120 if (disk.Seek(sector)) {
1121 if (!littleEndian)
1122 ReversePartitionBytes();
srs56940283dae2010-04-28 16:44:34 -04001123 if (disk.Write(partitions, mainHeader.sizeOfPartitionEntries * numParts) == -1)
srs5694cb76c672010-02-11 22:22:22 -05001124 allOK = 0;
1125 if (!littleEndian)
1126 ReversePartitionBytes();
1127 } else allOK = 0; // if (myDisk.Seek()...)
1128 return allOK;
1129} // GPTData::SavePartitionTable()
1130
srs5694e7b4ff92009-08-18 13:16:10 -04001131// Load GPT data from a backup file created by SaveGPTBackup(). This function
1132// does minimal error checking. It returns 1 if it completed successfully,
1133// 0 if there was a problem. In the latter case, it creates a new empty
1134// set of partitions.
srs56940a697312010-01-28 21:10:52 -05001135int GPTData::LoadGPTBackup(const string & filename) {
srs5694cb76c672010-02-11 22:22:22 -05001136 int allOK = 1, val, err;
srs56940283dae2010-04-28 16:44:34 -04001137 uint32_t sizeOfEntries;
srs5694cb76c672010-02-11 22:22:22 -05001138 int littleEndian = 1, shortBackup = 0;
srs5694546a9c72010-01-26 16:00:26 -05001139 DiskIO backupFile;
srs5694e7b4ff92009-08-18 13:16:10 -04001140
srs5694546a9c72010-01-26 16:00:26 -05001141 if (backupFile.OpenForRead(filename)) {
srs56942a9f5da2009-08-26 00:48:01 -04001142 if (IsLittleEndian() == 0)
1143 littleEndian = 0;
1144
srs5694e7b4ff92009-08-18 13:16:10 -04001145 // Let the MBRData class load the saved MBR...
srs5694546a9c72010-01-26 16:00:26 -05001146 protectiveMBR.ReadMBRData(&backupFile, 0); // 0 = don't check block size
srs5694815fb652011-03-18 12:35:56 -04001147 protectiveMBR.SetDisk(&myDisk);
srs5694e7b4ff92009-08-18 13:16:10 -04001148
srs5694cb76c672010-02-11 22:22:22 -05001149 LoadHeader(&mainHeader, backupFile, 1, &mainCrcOk);
srs5694e7b4ff92009-08-18 13:16:10 -04001150
srs5694cb76c672010-02-11 22:22:22 -05001151 // Check backup file size and rebuild second header if file is right
1152 // size to be direct dd copy of MBR, main header, and main partition
1153 // table; if other size, treat it like a GPT fdisk-generated backup
1154 // file
1155 shortBackup = ((backupFile.DiskSize(&err) * backupFile.GetBlockSize()) ==
1156 (mainHeader.numParts * mainHeader.sizeOfPartitionEntries) + 1024);
1157 if (shortBackup) {
1158 RebuildSecondHeader();
1159 secondCrcOk = mainCrcOk;
1160 } else {
1161 LoadHeader(&secondHeader, backupFile, 2, &secondCrcOk);
1162 } // if/else
srs56942a9f5da2009-08-26 00:48:01 -04001163
srs5694e7b4ff92009-08-18 13:16:10 -04001164 // Return valid headers code: 0 = both headers bad; 1 = main header
1165 // good, backup bad; 2 = backup header good, main header bad;
1166 // 3 = both headers good. Note these codes refer to valid GPT
1167 // signatures and version numbers; more subtle problems will elude
1168 // this check!
1169 if ((val = CheckHeaderValidity()) > 0) {
1170 if (val == 2) { // only backup header seems to be good
srs56940283dae2010-04-28 16:44:34 -04001171 SetGPTSize(secondHeader.numParts);
srs5694e4ac11e2009-08-31 10:13:04 -04001172 sizeOfEntries = secondHeader.sizeOfPartitionEntries;
srs5694e7b4ff92009-08-18 13:16:10 -04001173 } else { // main header is OK
srs56940283dae2010-04-28 16:44:34 -04001174 SetGPTSize(mainHeader.numParts);
srs5694e7b4ff92009-08-18 13:16:10 -04001175 sizeOfEntries = mainHeader.sizeOfPartitionEntries;
1176 } // if/else
1177
srs5694e7b4ff92009-08-18 13:16:10 -04001178 if (secondHeader.currentLBA != diskSize - UINT64_C(1)) {
srs5694fed16d02010-01-27 23:03:40 -05001179 cout << "Warning! Current disk size doesn't match that of the backup!\n"
1180 << "Adjusting sizes to match, but subsequent problems are possible!\n";
srs5694247657a2009-11-26 18:36:12 -05001181 MoveSecondHeaderToEnd();
srs5694e7b4ff92009-08-18 13:16:10 -04001182 } // if
1183
srs5694cb76c672010-02-11 22:22:22 -05001184 if (!LoadPartitionTable(mainHeader, backupFile, (uint64_t) (3 - shortBackup)))
1185 cerr << "Warning! Read error " << errno
1186 << " loading partition table; strange behavior now likely!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001187 } else {
1188 allOK = 0;
1189 } // if/else
srs5694a8582cf2010-03-19 14:21:59 -04001190 // Something went badly wrong, so blank out partitions
1191 if (allOK == 0) {
1192 cerr << "Improper backup file! Clearing all partition data!\n";
1193 ClearGPTData();
1194 protectiveMBR.MakeProtectiveMBR();
1195 } // if
srs5694e7b4ff92009-08-18 13:16:10 -04001196 } else {
1197 allOK = 0;
srs56945a608532011-03-17 13:53:01 -04001198 cerr << "Unable to open file '" << filename << "' for reading! Aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001199 } // if/else
1200
srs5694e7b4ff92009-08-18 13:16:10 -04001201 return allOK;
1202} // GPTData::LoadGPTBackup()
1203
srs569408bb0da2010-02-19 17:19:55 -05001204int GPTData::SaveMBR(void) {
srs569455d92612010-03-07 22:16:07 -05001205 return protectiveMBR.WriteMBRData(&myDisk);
srs569408bb0da2010-02-19 17:19:55 -05001206} // GPTData::SaveMBR()
1207
1208// This function destroys the on-disk GPT structures, but NOT the on-disk
1209// MBR.
1210// Returns 1 if the operation succeeds, 0 if not.
1211int GPTData::DestroyGPT(void) {
srs569401f7f082011-03-15 23:53:31 -04001212 int sum, tableSize, allOK = 1;
srs569408bb0da2010-02-19 17:19:55 -05001213 uint8_t blankSector[512];
1214 uint8_t* emptyTable;
1215
srs569401f7f082011-03-15 23:53:31 -04001216 memset(blankSector, 0, sizeof(blankSector));
srs569408bb0da2010-02-19 17:19:55 -05001217
1218 if (myDisk.OpenForWrite()) {
1219 if (!myDisk.Seek(mainHeader.currentLBA))
1220 allOK = 0;
1221 if (myDisk.Write(blankSector, 512) != 512) { // blank it out
1222 cerr << "Warning! GPT main header not overwritten! Error is " << errno << "\n";
1223 allOK = 0;
1224 } // if
1225 if (!myDisk.Seek(mainHeader.partitionEntriesLBA))
1226 allOK = 0;
srs56940283dae2010-04-28 16:44:34 -04001227 tableSize = numParts * mainHeader.sizeOfPartitionEntries;
srs569408bb0da2010-02-19 17:19:55 -05001228 emptyTable = new uint8_t[tableSize];
srs56946aae2a92011-06-10 01:16:51 -04001229 if (emptyTable == NULL) {
1230 cerr << "Could not allocate memory in GPTData::CheckTable()! Terminating!\n";
1231 exit(1);
1232 } // if
srs569401f7f082011-03-15 23:53:31 -04001233 memset(emptyTable, 0, tableSize);
srs569408bb0da2010-02-19 17:19:55 -05001234 if (allOK) {
1235 sum = myDisk.Write(emptyTable, tableSize);
1236 if (sum != tableSize) {
1237 cerr << "Warning! GPT main partition table not overwritten! Error is " << errno << "\n";
1238 allOK = 0;
1239 } // if write failed
1240 } // if
1241 if (!myDisk.Seek(secondHeader.partitionEntriesLBA))
1242 allOK = 0;
1243 if (allOK) {
1244 sum = myDisk.Write(emptyTable, tableSize);
1245 if (sum != tableSize) {
1246 cerr << "Warning! GPT backup partition table not overwritten! Error is "
1247 << errno << "\n";
1248 allOK = 0;
1249 } // if wrong size written
1250 } // if
1251 if (!myDisk.Seek(secondHeader.currentLBA))
1252 allOK = 0;
1253 if (allOK) {
1254 if (myDisk.Write(blankSector, 512) != 512) { // blank it out
1255 cerr << "Warning! GPT backup header not overwritten! Error is " << errno << "\n";
1256 allOK = 0;
1257 } // if
1258 } // if
1259 myDisk.DiskSync();
1260 myDisk.Close();
1261 cout << "GPT data structures destroyed! You may now partition the disk using fdisk or\n"
1262 << "other utilities.\n";
1263 delete[] emptyTable;
1264 } else {
srs56945a608532011-03-17 13:53:01 -04001265 cerr << "Problem opening '" << device << "' for writing! Program will now terminate.\n";
srs569408bb0da2010-02-19 17:19:55 -05001266 } // if/else (fd != -1)
1267 return (allOK);
1268} // GPTDataTextUI::DestroyGPT()
1269
1270// Wipe MBR data from the disk (zero it out completely)
1271// Returns 1 on success, 0 on failure.
1272int GPTData::DestroyMBR(void) {
srs569401f7f082011-03-15 23:53:31 -04001273 int allOK;
srs569408bb0da2010-02-19 17:19:55 -05001274 uint8_t blankSector[512];
1275
srs569401f7f082011-03-15 23:53:31 -04001276 memset(blankSector, 0, sizeof(blankSector));
srs569408bb0da2010-02-19 17:19:55 -05001277
srs569401f7f082011-03-15 23:53:31 -04001278 allOK = myDisk.OpenForWrite() && myDisk.Seek(0) && (myDisk.Write(blankSector, 512) == 512);
1279
srs569408bb0da2010-02-19 17:19:55 -05001280 if (!allOK)
1281 cerr << "Warning! MBR not overwritten! Error is " << errno << "!\n";
1282 return allOK;
1283} // GPTData::DestroyMBR(void)
1284
srs5694e4ac11e2009-08-31 10:13:04 -04001285// Tell user whether Apple Partition Map (APM) was discovered....
1286void GPTData::ShowAPMState(void) {
1287 if (apmFound)
srs5694fed16d02010-01-27 23:03:40 -05001288 cout << " APM: present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001289 else
srs5694fed16d02010-01-27 23:03:40 -05001290 cout << " APM: not present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001291} // GPTData::ShowAPMState()
1292
1293// Tell user about the state of the GPT data....
1294void GPTData::ShowGPTState(void) {
1295 switch (state) {
1296 case gpt_invalid:
srs5694fed16d02010-01-27 23:03:40 -05001297 cout << " GPT: not present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001298 break;
1299 case gpt_valid:
srs5694fed16d02010-01-27 23:03:40 -05001300 cout << " GPT: present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001301 break;
1302 case gpt_corrupt:
srs5694fed16d02010-01-27 23:03:40 -05001303 cout << " GPT: damaged\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001304 break;
1305 default:
srs5694fed16d02010-01-27 23:03:40 -05001306 cout << "\a GPT: unknown -- bug!\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001307 break;
1308 } // switch
1309} // GPTData::ShowGPTState()
1310
1311// Display the basic GPT data
1312void GPTData::DisplayGPTData(void) {
srs5694e321d442010-01-29 17:44:04 -05001313 uint32_t i;
srs5694e4ac11e2009-08-31 10:13:04 -04001314 uint64_t temp, totalFree;
1315
srs5694fed16d02010-01-27 23:03:40 -05001316 cout << "Disk " << device << ": " << diskSize << " sectors, "
srs569401f7f082011-03-15 23:53:31 -04001317 << BytesToIeee(diskSize, blockSize) << "\n";
srs5694fed16d02010-01-27 23:03:40 -05001318 cout << "Logical sector size: " << blockSize << " bytes\n";
srs56945a081752010-09-24 20:39:41 -04001319 cout << "Disk identifier (GUID): " << mainHeader.diskGUID << "\n";
srs56940283dae2010-04-28 16:44:34 -04001320 cout << "Partition table holds up to " << numParts << " entries\n";
srs5694fed16d02010-01-27 23:03:40 -05001321 cout << "First usable sector is " << mainHeader.firstUsableLBA
1322 << ", last usable sector is " << mainHeader.lastUsableLBA << "\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001323 totalFree = FindFreeBlocks(&i, &temp);
srs56948a4ddfc2010-03-21 19:05:49 -04001324 cout << "Partitions will be aligned on " << sectorAlignment << "-sector boundaries\n";
srs5694fed16d02010-01-27 23:03:40 -05001325 cout << "Total free space is " << totalFree << " sectors ("
srs569401f7f082011-03-15 23:53:31 -04001326 << BytesToIeee(totalFree, blockSize) << ")\n";
srs5694fed16d02010-01-27 23:03:40 -05001327 cout << "\nNumber Start (sector) End (sector) Size Code Name\n";
srs56940283dae2010-04-28 16:44:34 -04001328 for (i = 0; i < numParts; i++) {
srs5694978041c2009-09-21 20:51:47 -04001329 partitions[i].ShowSummary(i, blockSize);
srs5694e4ac11e2009-08-31 10:13:04 -04001330 } // for
1331} // GPTData::DisplayGPTData()
1332
srs5694e4ac11e2009-08-31 10:13:04 -04001333// Show detailed information on the specified partition
1334void GPTData::ShowPartDetails(uint32_t partNum) {
srs56940873e9d2010-10-07 13:00:45 -04001335 if (!IsFreePartNum(partNum)) {
srs5694e4ac11e2009-08-31 10:13:04 -04001336 partitions[partNum].ShowDetails(blockSize);
1337 } else {
srs5694fed16d02010-01-27 23:03:40 -05001338 cout << "Partition #" << partNum + 1 << " does not exist.";
srs5694e4ac11e2009-08-31 10:13:04 -04001339 } // if
1340} // GPTData::ShowPartDetails()
1341
srs5694e4ac11e2009-08-31 10:13:04 -04001342/**************************************************************************
1343 * *
1344 * Partition table transformation functions (MBR or BSD disklabel to GPT) *
1345 * (some of these functions may require user interaction) *
1346 * *
1347 **************************************************************************/
1348
srs569408bb0da2010-02-19 17:19:55 -05001349// Examines the MBR & GPT data to determine which set of data to use: the
1350// MBR (use_mbr), the GPT (use_gpt), the BSD disklabel (use_bsd), or create
1351// a new set of partitions (use_new). A return value of use_abort indicates
1352// that this function couldn't determine what to do. Overriding functions
1353// in derived classes may ask users questions in such cases.
srs5694e4ac11e2009-08-31 10:13:04 -04001354WhichToUse GPTData::UseWhichPartitions(void) {
1355 WhichToUse which = use_new;
1356 MBRValidity mbrState;
srs5694e4ac11e2009-08-31 10:13:04 -04001357
1358 mbrState = protectiveMBR.GetValidity();
1359
1360 if ((state == gpt_invalid) && ((mbrState == mbr) || (mbrState == hybrid))) {
srs5694fed16d02010-01-27 23:03:40 -05001361 cout << "\n***************************************************************\n"
1362 << "Found invalid GPT and valid MBR; converting MBR to GPT format.\n";
srs56945d58fe02010-01-03 20:57:08 -05001363 if (!justLooking) {
srs56940283dae2010-04-28 16:44:34 -04001364 cout << "\aTHIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by typing 'q' if\n"
srs5694fed16d02010-01-27 23:03:40 -05001365 << "you don't want to convert your MBR partitions to GPT format!\n";
srs56945d58fe02010-01-03 20:57:08 -05001366 } // if
srs5694fed16d02010-01-27 23:03:40 -05001367 cout << "***************************************************************\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001368 which = use_mbr;
1369 } // if
1370
1371 if ((state == gpt_invalid) && bsdFound) {
srs5694fed16d02010-01-27 23:03:40 -05001372 cout << "\n**********************************************************************\n"
1373 << "Found invalid GPT and valid BSD disklabel; converting BSD disklabel\n"
1374 << "to GPT format.";
srs56940a697312010-01-28 21:10:52 -05001375 if ((!justLooking) && (!beQuiet)) {
srs56940283dae2010-04-28 16:44:34 -04001376 cout << "\a THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Your first\n"
srs5694fed16d02010-01-27 23:03:40 -05001377 << "BSD partition will likely be unusable. Exit by typing 'q' if you don't\n"
1378 << "want to convert your BSD partitions to GPT format!";
srs56945d58fe02010-01-03 20:57:08 -05001379 } // if
srs5694fed16d02010-01-27 23:03:40 -05001380 cout << "\n**********************************************************************\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001381 which = use_bsd;
1382 } // if
1383
1384 if ((state == gpt_valid) && (mbrState == gpt)) {
srs5694e4ac11e2009-08-31 10:13:04 -04001385 which = use_gpt;
srs56943c0af382010-01-15 19:19:18 -05001386 if (!beQuiet)
srs5694fed16d02010-01-27 23:03:40 -05001387 cout << "Found valid GPT with protective MBR; using GPT.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001388 } // if
1389 if ((state == gpt_valid) && (mbrState == hybrid)) {
srs5694e4ac11e2009-08-31 10:13:04 -04001390 which = use_gpt;
srs56943c0af382010-01-15 19:19:18 -05001391 if (!beQuiet)
srs5694fed16d02010-01-27 23:03:40 -05001392 cout << "Found valid GPT with hybrid MBR; using GPT.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001393 } // if
1394 if ((state == gpt_valid) && (mbrState == invalid)) {
srs56940a697312010-01-28 21:10:52 -05001395 cout << "\aFound valid GPT with corrupt MBR; using GPT and will write new\n"
srs5694fed16d02010-01-27 23:03:40 -05001396 << "protective MBR on save.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001397 which = use_gpt;
srs5694e4ac11e2009-08-31 10:13:04 -04001398 } // if
1399 if ((state == gpt_valid) && (mbrState == mbr)) {
srs569408bb0da2010-02-19 17:19:55 -05001400 which = use_abort;
srs5694e4ac11e2009-08-31 10:13:04 -04001401 } // if
1402
srs5694e4ac11e2009-08-31 10:13:04 -04001403 if (state == gpt_corrupt) {
srs569408bb0da2010-02-19 17:19:55 -05001404 if (mbrState == gpt) {
1405 cout << "\a\a****************************************************************************\n"
1406 << "Caution: Found protective or hybrid MBR and corrupt GPT. Using GPT, but disk\n"
1407 << "verification and recovery are STRONGLY recommended.\n"
1408 << "****************************************************************************\n";
1409 which = use_gpt;
srs56943c0af382010-01-15 19:19:18 -05001410 } else {
srs569408bb0da2010-02-19 17:19:55 -05001411 which = use_abort;
1412 } // if/else MBR says disk is GPT
1413 } // if GPT corrupt
srs5694e4ac11e2009-08-31 10:13:04 -04001414
1415 if (which == use_new)
srs5694fed16d02010-01-27 23:03:40 -05001416 cout << "Creating new GPT entries.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001417
1418 return which;
1419} // UseWhichPartitions()
1420
srs569408bb0da2010-02-19 17:19:55 -05001421// Convert MBR partition table into GPT form.
1422void GPTData::XFormPartitions(void) {
srs5694e4ac11e2009-08-31 10:13:04 -04001423 int i, numToConvert;
1424 uint8_t origType;
srs5694e4ac11e2009-08-31 10:13:04 -04001425
1426 // Clear out old data & prepare basics....
1427 ClearGPTData();
1428
1429 // Convert the smaller of the # of GPT or MBR partitions
srs56940283dae2010-04-28 16:44:34 -04001430 if (numParts > MAX_MBR_PARTS)
srs5694978041c2009-09-21 20:51:47 -04001431 numToConvert = MAX_MBR_PARTS;
srs5694e4ac11e2009-08-31 10:13:04 -04001432 else
srs56940283dae2010-04-28 16:44:34 -04001433 numToConvert = numParts;
srs5694e4ac11e2009-08-31 10:13:04 -04001434
1435 for (i = 0; i < numToConvert; i++) {
1436 origType = protectiveMBR.GetType(i);
1437 // don't waste CPU time trying to convert extended, hybrid protective, or
1438 // null (non-existent) partitions
srs5694e35eb1b2009-09-14 00:29:34 -04001439 if ((origType != 0x05) && (origType != 0x0f) && (origType != 0x85) &&
srs56946699b012010-02-04 00:55:30 -05001440 (origType != 0x00) && (origType != 0xEE))
srs5694e4ac11e2009-08-31 10:13:04 -04001441 partitions[i] = protectiveMBR.AsGPT(i);
1442 } // for
1443
1444 // Convert MBR into protective MBR
1445 protectiveMBR.MakeProtectiveMBR();
1446
1447 // Record that all original CRCs were OK so as not to raise flags
1448 // when doing a disk verification
1449 mainCrcOk = secondCrcOk = mainPartsCrcOk = secondPartsCrcOk = 1;
srs5694e4ac11e2009-08-31 10:13:04 -04001450} // GPTData::XFormPartitions()
1451
1452// Transforms BSD disklabel on the specified partition (numbered from 0).
srs569408bb0da2010-02-19 17:19:55 -05001453// If an invalid partition number is given, the program does nothing.
srs5694e4ac11e2009-08-31 10:13:04 -04001454// Returns the number of new partitions created.
srs569408bb0da2010-02-19 17:19:55 -05001455int GPTData::XFormDisklabel(uint32_t partNum) {
1456 uint32_t low, high;
srs5694e4ac11e2009-08-31 10:13:04 -04001457 int goOn = 1, numDone = 0;
1458 BSDData disklabel;
1459
srs569408bb0da2010-02-19 17:19:55 -05001460 if (GetPartRange(&low, &high) == 0) {
1461 goOn = 0;
1462 cout << "No partitions!\n";
1463 } // if
1464 if (partNum > high) {
1465 goOn = 0;
1466 cout << "Specified partition is invalid!\n";
1467 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04001468
srs569408bb0da2010-02-19 17:19:55 -05001469 // If all is OK, read the disklabel and convert it.
1470 if (goOn) {
1471 goOn = disklabel.ReadBSDData(&myDisk, partitions[partNum].GetFirstLBA(),
1472 partitions[partNum].GetLastLBA());
1473 if ((goOn) && (disklabel.IsDisklabel())) {
1474 numDone = XFormDisklabel(&disklabel);
1475 if (numDone == 1)
1476 cout << "Converted 1 BSD partition.\n";
1477 else
1478 cout << "Converted " << numDone << " BSD partitions.\n";
1479 } else {
1480 cout << "Unable to convert partitions! Unrecognized BSD disklabel.\n";
1481 } // if/else
1482 } // if
1483 if (numDone > 0) { // converted partitions; delete carrier
1484 partitions[partNum].BlankPartition();
1485 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04001486 return numDone;
srs569455d92612010-03-07 22:16:07 -05001487} // GPTData::XFormDisklabel(uint32_t i)
srs5694e4ac11e2009-08-31 10:13:04 -04001488
1489// Transform the partitions on an already-loaded BSD disklabel...
srs569408bb0da2010-02-19 17:19:55 -05001490int GPTData::XFormDisklabel(BSDData* disklabel) {
1491 int i, partNum = 0, numDone = 0;
srs5694e4ac11e2009-08-31 10:13:04 -04001492
srs569408bb0da2010-02-19 17:19:55 -05001493 if (disklabel->IsDisklabel()) {
srs5694e4ac11e2009-08-31 10:13:04 -04001494 for (i = 0; i < disklabel->GetNumParts(); i++) {
srs569408bb0da2010-02-19 17:19:55 -05001495 partNum = FindFirstFreePart();
1496 if (partNum >= 0) {
1497 partitions[partNum] = disklabel->AsGPT(i);
1498 if (partitions[partNum].IsUsed())
1499 numDone++;
1500 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04001501 } // for
srs569408bb0da2010-02-19 17:19:55 -05001502 if (partNum == -1)
1503 cerr << "Warning! Too many partitions to convert!\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001504 } // if
1505
1506 // Record that all original CRCs were OK so as not to raise flags
1507 // when doing a disk verification
1508 mainCrcOk = secondCrcOk = mainPartsCrcOk = secondPartsCrcOk = 1;
1509
1510 return numDone;
1511} // GPTData::XFormDisklabel(BSDData* disklabel)
1512
srs569408bb0da2010-02-19 17:19:55 -05001513// Add one GPT partition to MBR. Used by PartsToMBR() functions. Created
1514// partition has the active/bootable flag UNset and uses the GPT fdisk
1515// type code divided by 0x0100 as the MBR type code.
1516// Returns 1 if operation was 100% successful, 0 if there were ANY
1517// problems.
srs5694978041c2009-09-21 20:51:47 -04001518int GPTData::OnePartToMBR(uint32_t gptPart, int mbrPart) {
srs569408bb0da2010-02-19 17:19:55 -05001519 int allOK = 1;
srs5694fed16d02010-01-27 23:03:40 -05001520
srs5694978041c2009-09-21 20:51:47 -04001521 if ((mbrPart < 0) || (mbrPart > 3)) {
srs5694fed16d02010-01-27 23:03:40 -05001522 cout << "MBR partition " << mbrPart + 1 << " is out of range; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001523 allOK = 0;
1524 } // if
srs56940283dae2010-04-28 16:44:34 -04001525 if (gptPart >= numParts) {
srs5694fed16d02010-01-27 23:03:40 -05001526 cout << "GPT partition " << gptPart + 1 << " is out of range; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001527 allOK = 0;
1528 } // if
1529 if (allOK && (partitions[gptPart].GetLastLBA() == UINT64_C(0))) {
srs5694fed16d02010-01-27 23:03:40 -05001530 cout << "GPT partition " << gptPart + 1 << " is undefined; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001531 allOK = 0;
1532 } // if
1533 if (allOK && (partitions[gptPart].GetFirstLBA() <= UINT32_MAX) &&
1534 (partitions[gptPart].GetLengthLBA() <= UINT32_MAX)) {
1535 if (partitions[gptPart].GetLastLBA() > UINT32_MAX) {
srs5694fed16d02010-01-27 23:03:40 -05001536 cout << "Caution: Partition end point past 32-bit pointer boundary;"
1537 << " some OSes may\nreact strangely.\n";
srs569408bb0da2010-02-19 17:19:55 -05001538 } // if
srs5694978041c2009-09-21 20:51:47 -04001539 protectiveMBR.MakePart(mbrPart, (uint32_t) partitions[gptPart].GetFirstLBA(),
srs569408bb0da2010-02-19 17:19:55 -05001540 (uint32_t) partitions[gptPart].GetLengthLBA(),
1541 partitions[gptPart].GetHexType() / 256, 0);
srs5694978041c2009-09-21 20:51:47 -04001542 } else { // partition out of range
srs569408bb0da2010-02-19 17:19:55 -05001543 if (allOK) // Display only if "else" triggered by out-of-bounds condition
1544 cout << "Partition " << gptPart + 1 << " begins beyond the 32-bit pointer limit of MBR "
1545 << "partitions, or is\n too big; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001546 allOK = 0;
1547 } // if/else
1548 return allOK;
1549} // GPTData::OnePartToMBR()
1550
srs5694e4ac11e2009-08-31 10:13:04 -04001551
1552/**********************************************************************
1553 * *
1554 * Functions that adjust GPT data structures WITHOUT user interaction *
1555 * (they may display information for the user's benefit, though) *
1556 * *
1557 **********************************************************************/
1558
1559// Resizes GPT to specified number of entries. Creates a new table if
srs5694ba00fed2010-01-12 18:18:36 -05001560// necessary, copies data if it already exists. Returns 1 if all goes
1561// well, 0 if an error is encountered.
srs5694e4ac11e2009-08-31 10:13:04 -04001562int GPTData::SetGPTSize(uint32_t numEntries) {
srs569408bb0da2010-02-19 17:19:55 -05001563 GPTPart* newParts;
srs5694e4ac11e2009-08-31 10:13:04 -04001564 uint32_t i, high, copyNum;
1565 int allOK = 1;
1566
1567 // First, adjust numEntries upward, if necessary, to get a number
1568 // that fills the allocated sectors
1569 i = blockSize / GPT_SIZE;
1570 if ((numEntries % i) != 0) {
srs5694fed16d02010-01-27 23:03:40 -05001571 cout << "Adjusting GPT size from " << numEntries << " to ";
srs5694e4ac11e2009-08-31 10:13:04 -04001572 numEntries = ((numEntries / i) + 1) * i;
srs5694fed16d02010-01-27 23:03:40 -05001573 cout << numEntries << " to fill the sector\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001574 } // if
1575
srs5694247657a2009-11-26 18:36:12 -05001576 // Do the work only if the # of partitions is changing. Along with being
srs569455d92612010-03-07 22:16:07 -05001577 // efficient, this prevents mucking with the location of the secondary
srs5694247657a2009-11-26 18:36:12 -05001578 // partition table, which causes problems when loading data from a RAID
1579 // array that's been expanded because this function is called when loading
1580 // data.
srs56940283dae2010-04-28 16:44:34 -04001581 if (((numEntries != numParts) || (partitions == NULL)) && (numEntries > 0)) {
srs569401f7f082011-03-15 23:53:31 -04001582 newParts = new GPTPart [numEntries];
srs5694247657a2009-11-26 18:36:12 -05001583 if (newParts != NULL) {
1584 if (partitions != NULL) { // existing partitions; copy them over
1585 GetPartRange(&i, &high);
1586 if (numEntries < (high + 1)) { // Highest entry too high for new #
srs5694fed16d02010-01-27 23:03:40 -05001587 cout << "The highest-numbered partition is " << high + 1
1588 << ", which is greater than the requested\n"
1589 << "partition table size of " << numEntries
1590 << "; cannot resize. Perhaps sorting will help.\n";
srs5694247657a2009-11-26 18:36:12 -05001591 allOK = 0;
srs5694815fb652011-03-18 12:35:56 -04001592 delete[] newParts;
srs5694247657a2009-11-26 18:36:12 -05001593 } else { // go ahead with copy
srs56940283dae2010-04-28 16:44:34 -04001594 if (numEntries < numParts)
srs5694247657a2009-11-26 18:36:12 -05001595 copyNum = numEntries;
1596 else
srs56940283dae2010-04-28 16:44:34 -04001597 copyNum = numParts;
srs5694247657a2009-11-26 18:36:12 -05001598 for (i = 0; i < copyNum; i++) {
1599 newParts[i] = partitions[i];
1600 } // for
srs569401f7f082011-03-15 23:53:31 -04001601 delete[] partitions;
srs5694247657a2009-11-26 18:36:12 -05001602 partitions = newParts;
srs5694247657a2009-11-26 18:36:12 -05001603 } // if
1604 } else { // No existing partition table; just create it
srs5694e4ac11e2009-08-31 10:13:04 -04001605 partitions = newParts;
srs5694247657a2009-11-26 18:36:12 -05001606 } // if/else existing partitions
srs56940283dae2010-04-28 16:44:34 -04001607 numParts = numEntries;
srs5694247657a2009-11-26 18:36:12 -05001608 mainHeader.firstUsableLBA = ((numEntries * GPT_SIZE) / blockSize) + 2 ;
1609 secondHeader.firstUsableLBA = mainHeader.firstUsableLBA;
1610 MoveSecondHeaderToEnd();
1611 if (diskSize > 0)
1612 CheckGPTSize();
1613 } else { // Bad memory allocation
srs56946aae2a92011-06-10 01:16:51 -04001614 cerr << "Error allocating memory for partition table! Size is unchanged!\n";
srs5694247657a2009-11-26 18:36:12 -05001615 allOK = 0;
1616 } // if/else
srs5694e4ac11e2009-08-31 10:13:04 -04001617 } // if/else
srs56940283dae2010-04-28 16:44:34 -04001618 mainHeader.numParts = numParts;
1619 secondHeader.numParts = numParts;
srs5694e4ac11e2009-08-31 10:13:04 -04001620 return (allOK);
1621} // GPTData::SetGPTSize()
1622
1623// Blank the partition array
1624void GPTData::BlankPartitions(void) {
1625 uint32_t i;
1626
srs56940283dae2010-04-28 16:44:34 -04001627 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04001628 partitions[i].BlankPartition();
1629 } // for
1630} // GPTData::BlankPartitions()
1631
srs5694ba00fed2010-01-12 18:18:36 -05001632// Delete a partition by number. Returns 1 if successful,
1633// 0 if there was a problem. Returns 1 if partition was in
1634// range, 0 if it was out of range.
1635int GPTData::DeletePartition(uint32_t partNum) {
1636 uint64_t startSector, length;
srs56940283dae2010-04-28 16:44:34 -04001637 uint32_t low, high, numUsedParts, retval = 1;;
srs5694ba00fed2010-01-12 18:18:36 -05001638
srs56940283dae2010-04-28 16:44:34 -04001639 numUsedParts = GetPartRange(&low, &high);
1640 if ((numUsedParts > 0) && (partNum >= low) && (partNum <= high)) {
srs5694ba00fed2010-01-12 18:18:36 -05001641 // In case there's a protective MBR, look for & delete matching
1642 // MBR partition....
1643 startSector = partitions[partNum].GetFirstLBA();
1644 length = partitions[partNum].GetLengthLBA();
1645 protectiveMBR.DeleteByLocation(startSector, length);
1646
1647 // Now delete the GPT partition
1648 partitions[partNum].BlankPartition();
1649 } else {
srs5694fed16d02010-01-27 23:03:40 -05001650 cerr << "Partition number " << partNum + 1 << " out of range!\n";
srs5694ba00fed2010-01-12 18:18:36 -05001651 retval = 0;
1652 } // if/else
1653 return retval;
1654} // GPTData::DeletePartition(uint32_t partNum)
1655
srs569408bb0da2010-02-19 17:19:55 -05001656// Non-interactively create a partition.
1657// Returns 1 if the operation was successful, 0 if a problem was discovered.
srs5694e321d442010-01-29 17:44:04 -05001658uint32_t GPTData::CreatePartition(uint32_t partNum, uint64_t startSector, uint64_t endSector) {
srs5694ba00fed2010-01-12 18:18:36 -05001659 int retval = 1; // assume there'll be no problems
srs56945a081752010-09-24 20:39:41 -04001660 uint64_t origSector = startSector;
srs5694ba00fed2010-01-12 18:18:36 -05001661
1662 if (IsFreePartNum(partNum)) {
srs56945a081752010-09-24 20:39:41 -04001663 if (Align(&startSector)) {
1664 cout << "Information: Moved requested sector from " << origSector << " to "
1665 << startSector << " in\norder to align on " << sectorAlignment
1666 << "-sector boundaries.\n";
1667 } // if
srs5694ba00fed2010-01-12 18:18:36 -05001668 if (IsFree(startSector) && (startSector <= endSector)) {
1669 if (FindLastInFree(startSector) >= endSector) {
1670 partitions[partNum].SetFirstLBA(startSector);
1671 partitions[partNum].SetLastLBA(endSector);
srs569400b6d7a2011-06-26 22:40:06 -04001672 partitions[partNum].SetType(DEFAULT_TYPE);
srs56946699b012010-02-04 00:55:30 -05001673 partitions[partNum].RandomizeUniqueGUID();
srs5694ba00fed2010-01-12 18:18:36 -05001674 } else retval = 0; // if free space until endSector
1675 } else retval = 0; // if startSector is free
1676 } else retval = 0; // if legal partition number
1677 return retval;
1678} // GPTData::CreatePartition(partNum, startSector, endSector)
1679
srs5694e4ac11e2009-08-31 10:13:04 -04001680// Sort the GPT entries, eliminating gaps and making for a logical
srs56949a46b042011-03-15 00:34:10 -04001681// ordering.
srs5694e4ac11e2009-08-31 10:13:04 -04001682void GPTData::SortGPT(void) {
srs56949a46b042011-03-15 00:34:10 -04001683 if (numParts > 0)
srs569401f7f082011-03-15 23:53:31 -04001684 sort(partitions, partitions + numParts);
srs5694e4ac11e2009-08-31 10:13:04 -04001685} // GPTData::SortGPT()
1686
srs569408bb0da2010-02-19 17:19:55 -05001687// Swap the contents of two partitions.
1688// Returns 1 if successful, 0 if either partition is out of range
1689// (that is, not a legal number; either or both can be empty).
1690// Note that if partNum1 = partNum2 and this number is in range,
1691// it will be considered successful.
1692int GPTData::SwapPartitions(uint32_t partNum1, uint32_t partNum2) {
1693 GPTPart temp;
1694 int allOK = 1;
1695
srs56940283dae2010-04-28 16:44:34 -04001696 if ((partNum1 < numParts) && (partNum2 < numParts)) {
srs569408bb0da2010-02-19 17:19:55 -05001697 if (partNum1 != partNum2) {
1698 temp = partitions[partNum1];
1699 partitions[partNum1] = partitions[partNum2];
1700 partitions[partNum2] = temp;
1701 } // if
1702 } else allOK = 0; // partition numbers are valid
1703 return allOK;
1704} // GPTData::SwapPartitions()
1705
srs5694e4ac11e2009-08-31 10:13:04 -04001706// Set up data structures for entirely new set of partitions on the
1707// specified device. Returns 1 if OK, 0 if there were problems.
srs5694e35eb1b2009-09-14 00:29:34 -04001708// Note that this function does NOT clear the protectiveMBR data
1709// structure, since it may hold the original MBR partitions if the
1710// program was launched on an MBR disk, and those may need to be
1711// converted to GPT format.
srs5694e4ac11e2009-08-31 10:13:04 -04001712int GPTData::ClearGPTData(void) {
srs5694e35eb1b2009-09-14 00:29:34 -04001713 int goOn = 1, i;
srs5694e4ac11e2009-08-31 10:13:04 -04001714
1715 // Set up the partition table....
srs56949a46b042011-03-15 00:34:10 -04001716 delete[] partitions;
srs5694e4ac11e2009-08-31 10:13:04 -04001717 partitions = NULL;
1718 SetGPTSize(NUM_GPT_ENTRIES);
1719
1720 // Now initialize a bunch of stuff that's static....
1721 mainHeader.signature = GPT_SIGNATURE;
1722 mainHeader.revision = 0x00010000;
srs5694978041c2009-09-21 20:51:47 -04001723 mainHeader.headerSize = HEADER_SIZE;
srs5694e4ac11e2009-08-31 10:13:04 -04001724 mainHeader.reserved = 0;
1725 mainHeader.currentLBA = UINT64_C(1);
1726 mainHeader.partitionEntriesLBA = (uint64_t) 2;
1727 mainHeader.sizeOfPartitionEntries = GPT_SIZE;
1728 for (i = 0; i < GPT_RESERVED; i++) {
1729 mainHeader.reserved2[i] = '\0';
1730 } // for
srs56940873e9d2010-10-07 13:00:45 -04001731 if (blockSize > 0)
1732 sectorAlignment = DEFAULT_ALIGNMENT * SECTOR_SIZE / blockSize;
1733 else
1734 sectorAlignment = DEFAULT_ALIGNMENT;
srs5694e4ac11e2009-08-31 10:13:04 -04001735
1736 // Now some semi-static items (computed based on end of disk)
1737 mainHeader.backupLBA = diskSize - UINT64_C(1);
1738 mainHeader.lastUsableLBA = diskSize - mainHeader.firstUsableLBA;
1739
1740 // Set a unique GUID for the disk, based on random numbers
srs56946699b012010-02-04 00:55:30 -05001741 mainHeader.diskGUID.Randomize();
srs5694e4ac11e2009-08-31 10:13:04 -04001742
1743 // Copy main header to backup header
1744 RebuildSecondHeader();
1745
1746 // Blank out the partitions array....
1747 BlankPartitions();
1748
1749 // Flag all CRCs as being OK....
1750 mainCrcOk = 1;
1751 secondCrcOk = 1;
1752 mainPartsCrcOk = 1;
1753 secondPartsCrcOk = 1;
1754
1755 return (goOn);
1756} // GPTData::ClearGPTData()
1757
srs5694247657a2009-11-26 18:36:12 -05001758// Set the location of the second GPT header data to the end of the disk.
srs569464cbd172011-03-01 22:03:54 -05001759// If the disk size has actually changed, this also adjusts the protective
1760// entry in the MBR, since it's probably no longer correct.
srs5694247657a2009-11-26 18:36:12 -05001761// Used internally and called by the 'e' option on the recovery &
1762// transformation menu, to help users of RAID arrays who add disk space
srs569464cbd172011-03-01 22:03:54 -05001763// to their arrays or to adjust data structures in restore operations
1764// involving unequal-sized disks.
srs5694247657a2009-11-26 18:36:12 -05001765void GPTData::MoveSecondHeaderToEnd() {
srs56948bb78762009-11-24 15:43:49 -05001766 mainHeader.backupLBA = secondHeader.currentLBA = diskSize - UINT64_C(1);
srs569464cbd172011-03-01 22:03:54 -05001767 if (mainHeader.lastUsableLBA != diskSize - mainHeader.firstUsableLBA) {
1768 if (protectiveMBR.GetValidity() == hybrid) {
1769 protectiveMBR.OptimizeEESize();
1770 RecomputeCHS();
1771 } // if
1772 if (protectiveMBR.GetValidity() == gpt)
1773 MakeProtectiveMBR();
1774 } // if
srs56948bb78762009-11-24 15:43:49 -05001775 mainHeader.lastUsableLBA = secondHeader.lastUsableLBA = diskSize - mainHeader.firstUsableLBA;
1776 secondHeader.partitionEntriesLBA = secondHeader.lastUsableLBA + UINT64_C(1);
1777} // GPTData::FixSecondHeaderLocation()
1778
srs5694699941e2011-03-21 21:33:57 -04001779// Sets the partition's name to the specified UnicodeString without
1780// user interaction.
1781// Returns 1 on success, 0 on failure (invalid partition number).
srs56945a608532011-03-17 13:53:01 -04001782int GPTData::SetName(uint32_t partNum, const UnicodeString & theName) {
srs5694ba00fed2010-01-12 18:18:36 -05001783 int retval = 1;
srs5694fed16d02010-01-27 23:03:40 -05001784
srs5694699941e2011-03-21 21:33:57 -04001785 if (IsUsedPartNum(partNum))
srs5694fed16d02010-01-27 23:03:40 -05001786 partitions[partNum].SetName(theName);
srs5694699941e2011-03-21 21:33:57 -04001787 else
1788 retval = 0;
srs5694ba00fed2010-01-12 18:18:36 -05001789
1790 return retval;
srs5694e4ac11e2009-08-31 10:13:04 -04001791} // GPTData::SetName
1792
1793// Set the disk GUID to the specified value. Note that the header CRCs must
1794// be recomputed after calling this function.
1795void GPTData::SetDiskGUID(GUIDData newGUID) {
1796 mainHeader.diskGUID = newGUID;
1797 secondHeader.diskGUID = newGUID;
1798} // SetDiskGUID()
1799
1800// Set the unique GUID of the specified partition. Returns 1 on
1801// successful completion, 0 if there were problems (invalid
1802// partition number).
1803int GPTData::SetPartitionGUID(uint32_t pn, GUIDData theGUID) {
1804 int retval = 0;
1805
srs56940283dae2010-04-28 16:44:34 -04001806 if (pn < numParts) {
srs5694e4ac11e2009-08-31 10:13:04 -04001807 if (partitions[pn].GetFirstLBA() != UINT64_C(0)) {
1808 partitions[pn].SetUniqueGUID(theGUID);
1809 retval = 1;
1810 } // if
1811 } // if
1812 return retval;
1813} // GPTData::SetPartitionGUID()
1814
srs56949ba54212010-05-18 23:24:02 -04001815// Set new random GUIDs for the disk and all partitions. Intended to be used
1816// after disk cloning or similar operations that don't randomize the GUIDs.
1817void GPTData::RandomizeGUIDs(void) {
1818 uint32_t i;
1819
1820 mainHeader.diskGUID.Randomize();
1821 secondHeader.diskGUID = mainHeader.diskGUID;
1822 for (i = 0; i < numParts; i++)
1823 if (partitions[i].IsUsed())
1824 partitions[i].RandomizeUniqueGUID();
1825} // GPTData::RandomizeGUIDs()
1826
srs5694ba00fed2010-01-12 18:18:36 -05001827// Change partition type code non-interactively. Returns 1 if
1828// successful, 0 if not....
srs5694327129e2010-09-22 01:07:31 -04001829int GPTData::ChangePartType(uint32_t partNum, PartType theGUID) {
1830 int retval = 1;
1831
1832 if (!IsFreePartNum(partNum)) {
1833 partitions[partNum].SetType(theGUID);
1834 } else retval = 0;
1835 return retval;
1836} // GPTData::ChangePartType()
1837
srs56949ba54212010-05-18 23:24:02 -04001838// Recompute the CHS values of all the MBR partitions. Used to reset
1839// CHS values that some BIOSes require, despite the fact that the
1840// resulting CHS values violate the GPT standard.
1841void GPTData::RecomputeCHS(void) {
1842 int i;
1843
1844 for (i = 0; i < 4; i++)
1845 protectiveMBR.RecomputeCHS(i);
1846} // GPTData::RecomputeCHS()
1847
srs56941d1448a2009-12-31 21:20:19 -05001848// Adjust sector number so that it falls on a sector boundary that's a
1849// multiple of sectorAlignment. This is done to improve the performance
1850// of Western Digital Advanced Format disks and disks with similar
1851// technology from other companies, which use 4096-byte sectors
1852// internally although they translate to 512-byte sectors for the
1853// benefit of the OS. If partitions aren't properly aligned on these
1854// disks, some filesystem data structures can span multiple physical
1855// sectors, degrading performance. This function should be called
1856// only on the FIRST sector of the partition, not the last!
1857// This function returns 1 if the alignment was altered, 0 if it
1858// was unchanged.
1859int GPTData::Align(uint64_t* sector) {
1860 int retval = 0, sectorOK = 0;
srs569400b6d7a2011-06-26 22:40:06 -04001861 uint64_t earlier, later, testSector;
srs56941d1448a2009-12-31 21:20:19 -05001862
1863 if ((*sector % sectorAlignment) != 0) {
srs56941d1448a2009-12-31 21:20:19 -05001864 earlier = (*sector / sectorAlignment) * sectorAlignment;
1865 later = earlier + (uint64_t) sectorAlignment;
1866
1867 // Check to see that every sector between the earlier one and the
1868 // requested one is clear, and that it's not too early....
1869 if (earlier >= mainHeader.firstUsableLBA) {
srs56941d1448a2009-12-31 21:20:19 -05001870 sectorOK = 1;
1871 testSector = earlier;
1872 do {
1873 sectorOK = IsFree(testSector++);
1874 } while ((sectorOK == 1) && (testSector < *sector));
1875 if (sectorOK == 1) {
1876 *sector = earlier;
srs56945a081752010-09-24 20:39:41 -04001877 retval = 1;
srs56941d1448a2009-12-31 21:20:19 -05001878 } // if
1879 } // if firstUsableLBA check
1880
1881 // If couldn't move the sector earlier, try to move it later instead....
1882 if ((sectorOK != 1) && (later <= mainHeader.lastUsableLBA)) {
1883 sectorOK = 1;
1884 testSector = later;
1885 do {
1886 sectorOK = IsFree(testSector--);
1887 } while ((sectorOK == 1) && (testSector > *sector));
1888 if (sectorOK == 1) {
1889 *sector = later;
srs56945a081752010-09-24 20:39:41 -04001890 retval = 1;
srs56941d1448a2009-12-31 21:20:19 -05001891 } // if
1892 } // if
srs56941d1448a2009-12-31 21:20:19 -05001893 } // if
1894 return retval;
1895} // GPTData::Align()
1896
srs5694e4ac11e2009-08-31 10:13:04 -04001897/********************************************************
1898 * *
1899 * Functions that return data about GPT data structures *
1900 * (most of these are inline in gpt.h) *
1901 * *
1902 ********************************************************/
1903
1904// Find the low and high used partition numbers (numbered from 0).
1905// Return value is the number of partitions found. Note that the
1906// *low and *high values are both set to 0 when no partitions
1907// are found, as well as when a single partition in the first
1908// position exists. Thus, the return value is the only way to
1909// tell when no partitions exist.
1910int GPTData::GetPartRange(uint32_t *low, uint32_t *high) {
1911 uint32_t i;
1912 int numFound = 0;
1913
srs56940283dae2010-04-28 16:44:34 -04001914 *low = numParts + 1; // code for "not found"
srs5694e4ac11e2009-08-31 10:13:04 -04001915 *high = 0;
srs56949a46b042011-03-15 00:34:10 -04001916 for (i = 0; i < numParts; i++) {
1917 if (partitions[i].GetFirstLBA() != UINT64_C(0)) { // it exists
1918 *high = i; // since we're counting up, set the high value
1919 // Set the low value only if it's not yet found...
1920 if (*low == (numParts + 1)) *low = i;
1921 numFound++;
1922 } // if
1923 } // for
srs5694e4ac11e2009-08-31 10:13:04 -04001924
1925 // Above will leave *low pointing to its "not found" value if no partitions
1926 // are defined, so reset to 0 if this is the case....
srs56940283dae2010-04-28 16:44:34 -04001927 if (*low == (numParts + 1))
srs5694e4ac11e2009-08-31 10:13:04 -04001928 *low = 0;
1929 return numFound;
1930} // GPTData::GetPartRange()
1931
srs569408bb0da2010-02-19 17:19:55 -05001932// Returns the value of the first free partition, or -1 if none is
1933// unused.
1934int GPTData::FindFirstFreePart(void) {
1935 int i = 0;
1936
1937 if (partitions != NULL) {
srs56949a46b042011-03-15 00:34:10 -04001938 while ((i < (int) numParts) && (partitions[i].IsUsed()))
srs569408bb0da2010-02-19 17:19:55 -05001939 i++;
srs56940283dae2010-04-28 16:44:34 -04001940 if (i >= (int) numParts)
srs569408bb0da2010-02-19 17:19:55 -05001941 i = -1;
1942 } else i = -1;
1943 return i;
1944} // GPTData::FindFirstFreePart()
1945
srs5694978041c2009-09-21 20:51:47 -04001946// Returns the number of defined partitions.
1947uint32_t GPTData::CountParts(void) {
srs5694e321d442010-01-29 17:44:04 -05001948 uint32_t i, counted = 0;
srs5694978041c2009-09-21 20:51:47 -04001949
srs56940283dae2010-04-28 16:44:34 -04001950 for (i = 0; i < numParts; i++) {
srs569408bb0da2010-02-19 17:19:55 -05001951 if (partitions[i].IsUsed())
srs5694978041c2009-09-21 20:51:47 -04001952 counted++;
1953 } // for
1954 return counted;
1955} // GPTData::CountParts()
1956
srs5694e4ac11e2009-08-31 10:13:04 -04001957/****************************************************
1958 * *
1959 * Functions that return data about disk free space *
1960 * *
1961 ****************************************************/
1962
1963// Find the first available block after the starting point; returns 0 if
1964// there are no available blocks left
1965uint64_t GPTData::FindFirstAvailable(uint64_t start) {
1966 uint64_t first;
1967 uint32_t i;
1968 int firstMoved = 0;
1969
1970 // Begin from the specified starting point or from the first usable
1971 // LBA, whichever is greater...
1972 if (start < mainHeader.firstUsableLBA)
1973 first = mainHeader.firstUsableLBA;
1974 else
1975 first = start;
1976
1977 // ...now search through all partitions; if first is within an
1978 // existing partition, move it to the next sector after that
1979 // partition and repeat. If first was moved, set firstMoved
1980 // flag; repeat until firstMoved is not set, so as to catch
1981 // cases where partitions are out of sequential order....
1982 do {
1983 firstMoved = 0;
srs56940283dae2010-04-28 16:44:34 -04001984 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04001985 if ((first >= partitions[i].GetFirstLBA()) &&
srs569455d92612010-03-07 22:16:07 -05001986 (first <= partitions[i].GetLastLBA())) { // in existing part.
srs5694e4ac11e2009-08-31 10:13:04 -04001987 first = partitions[i].GetLastLBA() + 1;
1988 firstMoved = 1;
srs569455d92612010-03-07 22:16:07 -05001989 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04001990 } // for
1991 } while (firstMoved == 1);
1992 if (first > mainHeader.lastUsableLBA)
1993 first = 0;
1994 return (first);
1995} // GPTData::FindFirstAvailable()
1996
1997// Finds the first available sector in the largest block of unallocated
1998// space on the disk. Returns 0 if there are no available blocks left
1999uint64_t GPTData::FindFirstInLargest(void) {
srs5694e35eb1b2009-09-14 00:29:34 -04002000 uint64_t start, firstBlock, lastBlock, segmentSize, selectedSize = 0, selectedSegment = 0;
srs5694e4ac11e2009-08-31 10:13:04 -04002001
2002 start = 0;
2003 do {
2004 firstBlock = FindFirstAvailable(start);
2005 if (firstBlock != UINT32_C(0)) { // something's free...
2006 lastBlock = FindLastInFree(firstBlock);
2007 segmentSize = lastBlock - firstBlock + UINT32_C(1);
2008 if (segmentSize > selectedSize) {
2009 selectedSize = segmentSize;
2010 selectedSegment = firstBlock;
2011 } // if
2012 start = lastBlock + 1;
2013 } // if
2014 } while (firstBlock != 0);
2015 return selectedSegment;
2016} // GPTData::FindFirstInLargest()
2017
srs5694cb76c672010-02-11 22:22:22 -05002018// Find the last available block on the disk.
2019// Returns 0 if there are no available partitions
2020uint64_t GPTData::FindLastAvailable(void) {
srs5694e4ac11e2009-08-31 10:13:04 -04002021 uint64_t last;
2022 uint32_t i;
2023 int lastMoved = 0;
2024
2025 // Start by assuming the last usable LBA is available....
2026 last = mainHeader.lastUsableLBA;
2027
2028 // ...now, similar to algorithm in FindFirstAvailable(), search
2029 // through all partitions, moving last when it's in an existing
2030 // partition. Set the lastMoved flag so we repeat to catch cases
2031 // where partitions are out of logical order.
2032 do {
2033 lastMoved = 0;
srs56940283dae2010-04-28 16:44:34 -04002034 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04002035 if ((last >= partitions[i].GetFirstLBA()) &&
srs569455d92612010-03-07 22:16:07 -05002036 (last <= partitions[i].GetLastLBA())) { // in existing part.
srs5694e4ac11e2009-08-31 10:13:04 -04002037 last = partitions[i].GetFirstLBA() - 1;
2038 lastMoved = 1;
srs569455d92612010-03-07 22:16:07 -05002039 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002040 } // for
2041 } while (lastMoved == 1);
2042 if (last < mainHeader.firstUsableLBA)
2043 last = 0;
2044 return (last);
2045} // GPTData::FindLastAvailable()
2046
2047// Find the last available block in the free space pointed to by start.
2048uint64_t GPTData::FindLastInFree(uint64_t start) {
2049 uint64_t nearestStart;
2050 uint32_t i;
2051
2052 nearestStart = mainHeader.lastUsableLBA;
srs56940283dae2010-04-28 16:44:34 -04002053 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04002054 if ((nearestStart > partitions[i].GetFirstLBA()) &&
srs569455d92612010-03-07 22:16:07 -05002055 (partitions[i].GetFirstLBA() > start)) {
srs5694e4ac11e2009-08-31 10:13:04 -04002056 nearestStart = partitions[i].GetFirstLBA() - 1;
srs569455d92612010-03-07 22:16:07 -05002057 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002058 } // for
2059 return (nearestStart);
2060} // GPTData::FindLastInFree()
2061
2062// Finds the total number of free blocks, the number of segments in which
2063// they reside, and the size of the largest of those segments
srs5694e321d442010-01-29 17:44:04 -05002064uint64_t GPTData::FindFreeBlocks(uint32_t *numSegments, uint64_t *largestSegment) {
srs5694e4ac11e2009-08-31 10:13:04 -04002065 uint64_t start = UINT64_C(0); // starting point for each search
2066 uint64_t totalFound = UINT64_C(0); // running total
2067 uint64_t firstBlock; // first block in a segment
2068 uint64_t lastBlock; // last block in a segment
2069 uint64_t segmentSize; // size of segment in blocks
srs5694e321d442010-01-29 17:44:04 -05002070 uint32_t num = 0;
srs5694e4ac11e2009-08-31 10:13:04 -04002071
2072 *largestSegment = UINT64_C(0);
srs5694c54e9b42010-05-01 21:04:23 -04002073 if (diskSize > 0) {
2074 do {
2075 firstBlock = FindFirstAvailable(start);
2076 if (firstBlock != UINT64_C(0)) { // something's free...
2077 lastBlock = FindLastInFree(firstBlock);
2078 segmentSize = lastBlock - firstBlock + UINT64_C(1);
2079 if (segmentSize > *largestSegment) {
2080 *largestSegment = segmentSize;
2081 } // if
2082 totalFound += segmentSize;
2083 num++;
2084 start = lastBlock + 1;
srs5694e4ac11e2009-08-31 10:13:04 -04002085 } // if
srs5694c54e9b42010-05-01 21:04:23 -04002086 } while (firstBlock != 0);
2087 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002088 *numSegments = num;
2089 return totalFound;
2090} // GPTData::FindFreeBlocks()
2091
srs569455d92612010-03-07 22:16:07 -05002092// Returns 1 if sector is unallocated, 0 if it's allocated to a partition.
2093// If it's allocated, return the partition number to which it's allocated
2094// in partNum, if that variable is non-NULL. (A value of UINT32_MAX is
2095// returned in partNum if the sector is in use by basic GPT data structures.)
2096int GPTData::IsFree(uint64_t sector, uint32_t *partNum) {
srs5694e4ac11e2009-08-31 10:13:04 -04002097 int isFree = 1;
2098 uint32_t i;
2099
srs56940283dae2010-04-28 16:44:34 -04002100 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04002101 if ((sector >= partitions[i].GetFirstLBA()) &&
2102 (sector <= partitions[i].GetLastLBA())) {
2103 isFree = 0;
srs569455d92612010-03-07 22:16:07 -05002104 if (partNum != NULL)
2105 *partNum = i;
srs569408bb0da2010-02-19 17:19:55 -05002106 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002107 } // for
srs5694e35eb1b2009-09-14 00:29:34 -04002108 if ((sector < mainHeader.firstUsableLBA) ||
srs5694e4ac11e2009-08-31 10:13:04 -04002109 (sector > mainHeader.lastUsableLBA)) {
2110 isFree = 0;
srs569455d92612010-03-07 22:16:07 -05002111 if (partNum != NULL)
2112 *partNum = UINT32_MAX;
srs569408bb0da2010-02-19 17:19:55 -05002113 } // if
2114 return (isFree);
srs5694e4ac11e2009-08-31 10:13:04 -04002115} // GPTData::IsFree()
2116
srs5694815fb652011-03-18 12:35:56 -04002117// Returns 1 if partNum is unused AND if it's a legal value.
srs5694ba00fed2010-01-12 18:18:36 -05002118int GPTData::IsFreePartNum(uint32_t partNum) {
srs569401f7f082011-03-15 23:53:31 -04002119 return ((partNum < numParts) && (partitions != NULL) &&
2120 (!partitions[partNum].IsUsed()));
srs5694ba00fed2010-01-12 18:18:36 -05002121} // GPTData::IsFreePartNum()
2122
srs5694815fb652011-03-18 12:35:56 -04002123// Returns 1 if partNum is in use.
2124int GPTData::IsUsedPartNum(uint32_t partNum) {
2125 return ((partNum < numParts) && (partitions != NULL) &&
2126 (partitions[partNum].IsUsed()));
2127} // GPTData::IsUsedPartNum()
srs5694a8582cf2010-03-19 14:21:59 -04002128
2129/***********************************************************
2130 * *
2131 * Change how functions work or return information on them *
2132 * *
2133 ***********************************************************/
2134
2135// Set partition alignment value; partitions will begin on multiples of
2136// the specified value
2137void GPTData::SetAlignment(uint32_t n) {
srs56940873e9d2010-10-07 13:00:45 -04002138 if (n > 0)
2139 sectorAlignment = n;
2140 else
2141 cerr << "Attempt to set partition alignment to 0!\n";
srs5694a8582cf2010-03-19 14:21:59 -04002142} // GPTData::SetAlignment()
2143
2144// Compute sector alignment based on the current partitions (if any). Each
2145// partition's starting LBA is examined, and if it's divisible by a power-of-2
srs56940873e9d2010-10-07 13:00:45 -04002146// value less than or equal to the DEFAULT_ALIGNMENT value (adjusted for the
2147// sector size), but not by the previously-located alignment value, then the
2148// alignment value is adjusted down. If the computed alignment is less than 8
2149// and the disk is bigger than SMALLEST_ADVANCED_FORMAT, resets it to 8. This
2150// is a safety measure for WD Advanced Format and similar drives. If no partitions
2151// are defined, the alignment value is set to DEFAULT_ALIGNMENT (2048) (or an
2152// adjustment of that based on the current sector size). The result is that new
srs56948a4ddfc2010-03-21 19:05:49 -04002153// drives are aligned to 2048-sector multiples but the program won't complain
2154// about other alignments on existing disks unless a smaller-than-8 alignment
srs56940873e9d2010-10-07 13:00:45 -04002155// is used on big disks (as safety for WD Advanced Format drives).
srs5694a8582cf2010-03-19 14:21:59 -04002156// Returns the computed alignment value.
2157uint32_t GPTData::ComputeAlignment(void) {
2158 uint32_t i = 0, found, exponent = 31;
srs5694ab4b0432010-09-25 20:39:52 -04002159 uint32_t align = DEFAULT_ALIGNMENT;
srs5694a8582cf2010-03-19 14:21:59 -04002160
srs56940873e9d2010-10-07 13:00:45 -04002161 if (blockSize > 0)
2162 align = DEFAULT_ALIGNMENT * SECTOR_SIZE / blockSize;
2163 exponent = (uint32_t) log2(align);
srs56940283dae2010-04-28 16:44:34 -04002164 for (i = 0; i < numParts; i++) {
srs5694a8582cf2010-03-19 14:21:59 -04002165 if (partitions[i].IsUsed()) {
2166 found = 0;
2167 while (!found) {
srs56940873e9d2010-10-07 13:00:45 -04002168 align = UINT64_C(1) << exponent;
srs5694a8582cf2010-03-19 14:21:59 -04002169 if ((partitions[i].GetFirstLBA() % align) == 0) {
2170 found = 1;
2171 } else {
2172 exponent--;
2173 } // if/else
2174 } // while
2175 } // if
2176 } // for
srs56940873e9d2010-10-07 13:00:45 -04002177 if ((align < MIN_AF_ALIGNMENT) && (diskSize >= SMALLEST_ADVANCED_FORMAT))
2178 align = MIN_AF_ALIGNMENT;
2179 sectorAlignment = align;
srs5694a8582cf2010-03-19 14:21:59 -04002180 return align;
2181} // GPTData::ComputeAlignment()
2182
srs5694e4ac11e2009-08-31 10:13:04 -04002183/********************************
2184 * *
2185 * Endianness support functions *
2186 * *
2187 ********************************/
2188
srs56942a9f5da2009-08-26 00:48:01 -04002189void GPTData::ReverseHeaderBytes(struct GPTHeader* header) {
srs5694221e0872009-08-29 15:00:31 -04002190 ReverseBytes(&header->signature, 8);
2191 ReverseBytes(&header->revision, 4);
2192 ReverseBytes(&header->headerSize, 4);
2193 ReverseBytes(&header->headerCRC, 4);
2194 ReverseBytes(&header->reserved, 4);
2195 ReverseBytes(&header->currentLBA, 8);
2196 ReverseBytes(&header->backupLBA, 8);
2197 ReverseBytes(&header->firstUsableLBA, 8);
2198 ReverseBytes(&header->lastUsableLBA, 8);
2199 ReverseBytes(&header->partitionEntriesLBA, 8);
2200 ReverseBytes(&header->numParts, 4);
2201 ReverseBytes(&header->sizeOfPartitionEntries, 4);
2202 ReverseBytes(&header->partitionEntriesCRC, 4);
srs569408bb0da2010-02-19 17:19:55 -05002203 ReverseBytes(header->reserved2, GPT_RESERVED);
srs56942a9f5da2009-08-26 00:48:01 -04002204} // GPTData::ReverseHeaderBytes()
2205
srs56940283dae2010-04-28 16:44:34 -04002206// Reverse byte order for all partitions.
srs56942a9f5da2009-08-26 00:48:01 -04002207void GPTData::ReversePartitionBytes() {
2208 uint32_t i;
2209
srs56940283dae2010-04-28 16:44:34 -04002210 for (i = 0; i < numParts; i++) {
srs5694221e0872009-08-29 15:00:31 -04002211 partitions[i].ReversePartBytes();
srs56942a9f5da2009-08-26 00:48:01 -04002212 } // for
2213} // GPTData::ReversePartitionBytes()
2214
srs56949ddc14b2010-08-22 22:44:42 -04002215// Validate partition number
2216bool GPTData::ValidPartNum (const uint32_t partNum) {
2217 if (partNum >= numParts) {
srs56945a081752010-09-24 20:39:41 -04002218 cerr << "Partition number out of range: " << partNum << "\n";
srs56949ddc14b2010-08-22 22:44:42 -04002219 return false;
2220 } // if
2221 return true;
2222} // GPTData::ValidPartNum
2223
srs56945a081752010-09-24 20:39:41 -04002224// Return a single partition for inspection (not modification!) by other
2225// functions.
2226const GPTPart & GPTData::operator[](uint32_t partNum) const {
2227 if (partNum >= numParts) {
srs5694815fb652011-03-18 12:35:56 -04002228 cerr << "Partition number out of range (" << partNum << " requested, but only "
2229 << numParts << " available)\n";
2230 exit(1);
2231 } // if
2232 if (partitions == NULL) {
2233 cerr << "No partitions defined in GPTData::operator[]; fatal error!\n";
2234 exit(1);
srs56945a081752010-09-24 20:39:41 -04002235 } // if
2236 return partitions[partNum];
2237} // operator[]
2238
2239// Return (not for modification!) the disk's GUID value
2240const GUIDData & GPTData::GetDiskGUID(void) const {
2241 return mainHeader.diskGUID;
2242} // GPTData::GetDiskGUID()
2243
srs56949ddc14b2010-08-22 22:44:42 -04002244// Manage attributes for a partition, based on commands passed to this function.
2245// (Function is non-interactive.)
2246// Returns 1 if a modification command succeeded, 0 if the command should not have
2247// modified data, and -1 if a modification command failed.
2248int GPTData::ManageAttributes(int partNum, const string & command, const string & bits) {
2249 int retval = 0;
2250 Attributes theAttr;
2251
2252 if (command == "show") {
2253 ShowAttributes(partNum);
2254 } else if (command == "get") {
2255 GetAttribute(partNum, bits);
2256 } else {
2257 theAttr = partitions[partNum].GetAttributes();
2258 if (theAttr.OperateOnAttributes(partNum, command, bits)) {
2259 partitions[partNum].SetAttributes(theAttr.GetAttributes());
2260 retval = 1;
2261 } else {
2262 retval = -1;
2263 } // if/else
2264 } // if/elseif/else
2265
2266 return retval;
2267} // GPTData::ManageAttributes()
2268
2269// Show all attributes for a specified partition....
2270void GPTData::ShowAttributes(const uint32_t partNum) {
srs56940873e9d2010-10-07 13:00:45 -04002271 partitions[partNum].ShowAttributes(partNum);
srs56949ddc14b2010-08-22 22:44:42 -04002272} // GPTData::ShowAttributes
2273
2274// Show whether a single attribute bit is set (terse output)...
2275void GPTData::GetAttribute(const uint32_t partNum, const string& attributeBits) {
srs56940873e9d2010-10-07 13:00:45 -04002276 partitions[partNum].GetAttributes().OperateOnAttributes(partNum, "get", attributeBits);
srs56949ddc14b2010-08-22 22:44:42 -04002277} // GPTData::GetAttribute
2278
2279
srs56942a9f5da2009-08-26 00:48:01 -04002280/******************************************
2281 * *
2282 * Additional non-class support functions *
2283 * *
2284 ******************************************/
2285
srs5694e7b4ff92009-08-18 13:16:10 -04002286// Check to be sure that data type sizes are correct. The basic types (uint*_t) should
2287// never fail these tests, but the struct types may fail depending on compile options.
2288// Specifically, the -fpack-struct option to gcc may be required to ensure proper structure
2289// sizes.
2290int SizesOK(void) {
2291 int allOK = 1;
srs5694e7b4ff92009-08-18 13:16:10 -04002292
2293 if (sizeof(uint8_t) != 1) {
srs5694fed16d02010-01-27 23:03:40 -05002294 cerr << "uint8_t is " << sizeof(uint8_t) << " bytes, should be 1 byte; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002295 allOK = 0;
2296 } // if
2297 if (sizeof(uint16_t) != 2) {
srs5694fed16d02010-01-27 23:03:40 -05002298 cerr << "uint16_t is " << sizeof(uint16_t) << " bytes, should be 2 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002299 allOK = 0;
2300 } // if
2301 if (sizeof(uint32_t) != 4) {
srs5694fed16d02010-01-27 23:03:40 -05002302 cerr << "uint32_t is " << sizeof(uint32_t) << " bytes, should be 4 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002303 allOK = 0;
2304 } // if
2305 if (sizeof(uint64_t) != 8) {
srs5694fed16d02010-01-27 23:03:40 -05002306 cerr << "uint64_t is " << sizeof(uint64_t) << " bytes, should be 8 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002307 allOK = 0;
2308 } // if
2309 if (sizeof(struct MBRRecord) != 16) {
srs5694fed16d02010-01-27 23:03:40 -05002310 cerr << "MBRRecord is " << sizeof(MBRRecord) << " bytes, should be 16 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002311 allOK = 0;
2312 } // if
srs5694978041c2009-09-21 20:51:47 -04002313 if (sizeof(struct TempMBR) != 512) {
srs5694fed16d02010-01-27 23:03:40 -05002314 cerr << "TempMBR is " << sizeof(TempMBR) << " bytes, should be 512 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002315 allOK = 0;
2316 } // if
2317 if (sizeof(struct GPTHeader) != 512) {
srs5694fed16d02010-01-27 23:03:40 -05002318 cerr << "GPTHeader is " << sizeof(GPTHeader) << " bytes, should be 512 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002319 allOK = 0;
2320 } // if
srs5694221e0872009-08-29 15:00:31 -04002321 if (sizeof(GPTPart) != 128) {
srs5694fed16d02010-01-27 23:03:40 -05002322 cerr << "GPTPart is " << sizeof(GPTPart) << " bytes, should be 128 bytes; aborting!\n";
srs5694221e0872009-08-29 15:00:31 -04002323 allOK = 0;
2324 } // if
srs56946699b012010-02-04 00:55:30 -05002325 if (sizeof(GUIDData) != 16) {
2326 cerr << "GUIDData is " << sizeof(GUIDData) << " bytes, should be 16 bytes; aborting!\n";
2327 allOK = 0;
2328 } // if
2329 if (sizeof(PartType) != 16) {
2330 cerr << "PartType is " << sizeof(GUIDData) << " bytes, should be 16 bytes; aborting!\n";
2331 allOK = 0;
2332 } // if
srs5694e7b4ff92009-08-18 13:16:10 -04002333 return (allOK);
2334} // SizesOK()
srs5694e4ac11e2009-08-31 10:13:04 -04002335