blob: 25ca53aa186791d2f029710f11142da3e3db92a9 [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.
srs5694a17fe692011-09-10 20:30:20 -0400902// Returns 1 if the CRC is OK & this table matches the one already in memory,
903// 0 if not or if there was a read error.
srs5694cb76c672010-02-11 22:22:22 -0500904int GPTData::CheckTable(struct GPTHeader *header) {
905 uint32_t sizeOfParts, newCRC;
srs5694a17fe692011-09-10 20:30:20 -0400906 GPTPart *partsToCheck;
907 int allOK = 0;
srs5694cb76c672010-02-11 22:22:22 -0500908
srs56940283dae2010-04-28 16:44:34 -0400909 // Load partition table into temporary storage to check
srs5694cb76c672010-02-11 22:22:22 -0500910 // its CRC and store the results, then discard this temporary
911 // storage, since we don't use it in any but recovery operations
912 if (myDisk.Seek(header->partitionEntriesLBA)) {
srs5694a17fe692011-09-10 20:30:20 -0400913 partsToCheck = new GPTPart[header->numParts];
srs56940283dae2010-04-28 16:44:34 -0400914 sizeOfParts = header->numParts * header->sizeOfPartitionEntries;
srs5694a17fe692011-09-10 20:30:20 -0400915 if (partsToCheck == NULL) {
srs56946aae2a92011-06-10 01:16:51 -0400916 cerr << "Could not allocate memory in GPTData::CheckTable()! Terminating!\n";
917 exit(1);
918 } // if
srs5694a17fe692011-09-10 20:30:20 -0400919 if (myDisk.Read(partsToCheck, sizeOfParts) != (int) sizeOfParts) {
srs56940283dae2010-04-28 16:44:34 -0400920 cerr << "Warning! Error " << errno << " reading partition table for CRC check!\n";
srs5694cb76c672010-02-11 22:22:22 -0500921 } else {
srs5694a17fe692011-09-10 20:30:20 -0400922 newCRC = chksum_crc32((unsigned char*) partsToCheck, sizeOfParts);
923 allOK = (newCRC == header->partitionEntriesCRC);
924 if (memcmp(partitions, partsToCheck, sizeOfParts) != 0) {
925 cerr << "Warning! Main and backup partition tables differ! Use the 'c' and 'e' options\n"
926 << "on the recovery & transformation menu to examine the two tables.\n\n";
927 allOK = 0;
928 } // if
srs5694cb76c672010-02-11 22:22:22 -0500929 } // if/else
srs5694a17fe692011-09-10 20:30:20 -0400930 delete[] partsToCheck;
srs5694cb76c672010-02-11 22:22:22 -0500931 } // if
srs5694a17fe692011-09-10 20:30:20 -0400932 return allOK;
srs5694cb76c672010-02-11 22:22:22 -0500933} // GPTData::CheckTable()
srs5694e7b4ff92009-08-18 13:16:10 -0400934
srs569464cbd172011-03-01 22:03:54 -0500935// Writes GPT (and protective MBR) to disk. If quiet==1,
srs5694a17fe692011-09-10 20:30:20 -0400936// Returns 1 on successful write, 0 if there was a problem.
srs569464cbd172011-03-01 22:03:54 -0500937int GPTData::SaveGPTData(int quiet) {
srs56946699b012010-02-04 00:55:30 -0500938 int allOK = 1, littleEndian;
srs5694e321d442010-01-29 17:44:04 -0500939 char answer;
srs5694e7b4ff92009-08-18 13:16:10 -0400940
srs56946699b012010-02-04 00:55:30 -0500941 littleEndian = IsLittleEndian();
942
srs5694e7b4ff92009-08-18 13:16:10 -0400943 // First do some final sanity checks....
srs56945d58fe02010-01-03 20:57:08 -0500944
945 // This test should only fail on read-only disks....
946 if (justLooking) {
srs5694fed16d02010-01-27 23:03:40 -0500947 cout << "The justLooking flag is set. This probably means you can't write to the disk.\n";
srs56945d58fe02010-01-03 20:57:08 -0500948 allOK = 0;
949 } // if
950
srs569464cbd172011-03-01 22:03:54 -0500951 // Check that disk is really big enough to handle the second header...
952 if (mainHeader.backupLBA >= diskSize) {
953 cerr << "Caution! Secondary header was placed beyond the disk's limits! Moving the\n"
954 << "header, but other problems may occur!\n";
955 MoveSecondHeaderToEnd();
956 } // if
957
srs5694e7b4ff92009-08-18 13:16:10 -0400958 // Is there enough space to hold the GPT headers and partition tables,
959 // given the partition sizes?
srs5694221e0872009-08-29 15:00:31 -0400960 if (CheckGPTSize() > 0) {
srs5694e7b4ff92009-08-18 13:16:10 -0400961 allOK = 0;
962 } // if
963
srs5694247657a2009-11-26 18:36:12 -0500964 // Check that second header is properly placed. Warn and ask if this should
965 // be corrected if the test fails....
srs569464cbd172011-03-01 22:03:54 -0500966 if (mainHeader.backupLBA < (diskSize - UINT64_C(1))) {
967 if (quiet == 0) {
968 cout << "Warning! Secondary header is placed too early on the disk! Do you want to\n"
969 << "correct this problem? ";
970 if (GetYN() == 'Y') {
971 MoveSecondHeaderToEnd();
972 cout << "Have moved second header and partition table to correct location.\n";
973 } else {
974 cout << "Have not corrected the problem. Strange problems may occur in the future!\n";
975 } // if correction requested
976 } else { // Go ahead and do correction automatically
srs5694247657a2009-11-26 18:36:12 -0500977 MoveSecondHeaderToEnd();
srs569464cbd172011-03-01 22:03:54 -0500978 } // if/else quiet
srs5694247657a2009-11-26 18:36:12 -0500979 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400980
srs569455d92612010-03-07 22:16:07 -0500981 // Check for overlapping or insane partitions....
982 if ((FindOverlaps() > 0) || (FindInsanePartitions() > 0)) {
srs5694e4ac11e2009-08-31 10:13:04 -0400983 allOK = 0;
srs5694fed16d02010-01-27 23:03:40 -0500984 cerr << "Aborting write operation!\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400985 } // if
986
987 // Check for mismatched MBR and GPT data, but let it pass if found
988 // (function displays warning message)
989 FindHybridMismatches();
srs5694e7b4ff92009-08-18 13:16:10 -0400990
991 RecomputeCRCs();
992
srs5694ba00fed2010-01-12 18:18:36 -0500993 if ((allOK) && (!quiet)) {
srs5694fed16d02010-01-27 23:03:40 -0500994 cout << "\nFinal checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING\n"
srs5694bf8950c2011-03-12 01:23:12 -0500995 << "PARTITIONS!!\n\nDo you want to proceed? ";
srs56945d58fe02010-01-03 20:57:08 -0500996 answer = GetYN();
997 if (answer == 'Y') {
srs5694fed16d02010-01-27 23:03:40 -0500998 cout << "OK; writing new GUID partition table (GPT).\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400999 } else {
1000 allOK = 0;
1001 } // if/else
1002 } // if
1003
1004 // Do it!
1005 if (allOK) {
srs569464cbd172011-03-01 22:03:54 -05001006 if (myDisk.OpenForWrite()) {
srs56948a4ddfc2010-03-21 19:05:49 -04001007 // As per UEFI specs, write the secondary table and GPT first....
srs5694cb76c672010-02-11 22:22:22 -05001008 allOK = SavePartitionTable(myDisk, secondHeader.partitionEntriesLBA);
1009 if (!allOK)
1010 cerr << "Unable to save backup partition table! Perhaps the 'e' option on the experts'\n"
1011 << "menu will resolve this problem.\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001012
1013 // Now write the secondary GPT header...
srs56948a4ddfc2010-03-21 19:05:49 -04001014 allOK = allOK && SaveHeader(&secondHeader, myDisk, mainHeader.backupLBA);
1015
1016 // Now write the main partition tables...
1017 allOK = allOK && SavePartitionTable(myDisk, mainHeader.partitionEntriesLBA);
1018
1019 // Now write the main GPT header...
1020 allOK = allOK && SaveHeader(&mainHeader, myDisk, 1);
1021
1022 // To top it off, write the protective MBR...
1023 allOK = allOK && protectiveMBR.WriteMBRData(&myDisk);
srs5694e7b4ff92009-08-18 13:16:10 -04001024
1025 // re-read the partition table
1026 if (allOK) {
srs5694546a9c72010-01-26 16:00:26 -05001027 myDisk.DiskSync();
srs5694e7b4ff92009-08-18 13:16:10 -04001028 } // if
1029
1030 if (allOK) { // writes completed OK
srs5694fed16d02010-01-27 23:03:40 -05001031 cout << "The operation has completed successfully.\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001032 } else {
srs5694fed16d02010-01-27 23:03:40 -05001033 cerr << "Warning! An error was reported when writing the partition table! This error\n"
srs56948a4ddfc2010-03-21 19:05:49 -04001034 << "MIGHT be harmless, but you may have trashed the disk!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001035 } // if/else
srs56948a4ddfc2010-03-21 19:05:49 -04001036
srs5694546a9c72010-01-26 16:00:26 -05001037 myDisk.Close();
srs5694e7b4ff92009-08-18 13:16:10 -04001038 } else {
srs56945a608532011-03-17 13:53:01 -04001039 cerr << "Unable to open device '" << myDisk.GetName() << "' for writing! Errno is "
srs5694fed16d02010-01-27 23:03:40 -05001040 << errno << "! Aborting write!\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001041 allOK = 0;
srs5694e7b4ff92009-08-18 13:16:10 -04001042 } // if/else
1043 } else {
srs5694fed16d02010-01-27 23:03:40 -05001044 cout << "Aborting write of new partition table.\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001045 } // if
1046
1047 return (allOK);
1048} // GPTData::SaveGPTData()
1049
1050// Save GPT data to a backup file. This function does much less error
1051// checking than SaveGPTData(). It can therefore preserve many types of
1052// corruption for later analysis; however, it preserves only the MBR,
1053// the main GPT header, the backup GPT header, and the main partition
1054// table; it discards the backup partition table, since it should be
1055// identical to the main partition table on healthy disks.
srs56940a697312010-01-28 21:10:52 -05001056int GPTData::SaveGPTBackup(const string & filename) {
1057 int allOK = 1;
srs5694546a9c72010-01-26 16:00:26 -05001058 DiskIO backupFile;
srs5694e7b4ff92009-08-18 13:16:10 -04001059
srs5694546a9c72010-01-26 16:00:26 -05001060 if (backupFile.OpenForWrite(filename)) {
srs56946699b012010-02-04 00:55:30 -05001061 // Recomputing the CRCs is likely to alter them, which could be bad
1062 // if the intent is to save a potentially bad GPT for later analysis;
1063 // but if we don't do this, we get bogus errors when we load the
1064 // backup. I'm favoring misses over false alarms....
1065 RecomputeCRCs();
1066
srs5694546a9c72010-01-26 16:00:26 -05001067 protectiveMBR.WriteMBRData(&backupFile);
srs5694699941e2011-03-21 21:33:57 -04001068 protectiveMBR.SetDisk(&myDisk);
srs5694e7b4ff92009-08-18 13:16:10 -04001069
srs5694cb76c672010-02-11 22:22:22 -05001070 if (allOK) {
srs5694546a9c72010-01-26 16:00:26 -05001071 // MBR write closed disk, so re-open and seek to end....
1072 backupFile.OpenForWrite();
srs5694cb76c672010-02-11 22:22:22 -05001073 allOK = SaveHeader(&mainHeader, backupFile, 1);
1074 } // if (allOK)
srs5694e7b4ff92009-08-18 13:16:10 -04001075
srs5694e7b4ff92009-08-18 13:16:10 -04001076 if (allOK)
srs5694cb76c672010-02-11 22:22:22 -05001077 allOK = SaveHeader(&secondHeader, backupFile, 2);
srs5694e7b4ff92009-08-18 13:16:10 -04001078
srs5694cb76c672010-02-11 22:22:22 -05001079 if (allOK)
1080 allOK = SavePartitionTable(backupFile, 3);
srs5694e7b4ff92009-08-18 13:16:10 -04001081
1082 if (allOK) { // writes completed OK
srs5694fed16d02010-01-27 23:03:40 -05001083 cout << "The operation has completed successfully.\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001084 } else {
srs5694fed16d02010-01-27 23:03:40 -05001085 cerr << "Warning! An error was reported when writing the backup file.\n"
1086 << "It may not be usable!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001087 } // if/else
srs5694546a9c72010-01-26 16:00:26 -05001088 backupFile.Close();
srs5694e7b4ff92009-08-18 13:16:10 -04001089 } else {
srs56945a608532011-03-17 13:53:01 -04001090 cerr << "Unable to open file '" << filename << "' for writing! Aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001091 allOK = 0;
1092 } // if/else
1093 return allOK;
1094} // GPTData::SaveGPTBackup()
1095
srs5694cb76c672010-02-11 22:22:22 -05001096// Write a GPT header (main or backup) to the specified sector. Used by both
1097// the SaveGPTData() and SaveGPTBackup() functions.
1098// Should be passed an architecture-appropriate header (DO NOT call
1099// ReverseHeaderBytes() on the header before calling this function)
1100// Returns 1 on success, 0 on failure
1101int GPTData::SaveHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector) {
1102 int littleEndian, allOK = 1;
1103
1104 littleEndian = IsLittleEndian();
1105 if (!littleEndian)
1106 ReverseHeaderBytes(header);
1107 if (disk.Seek(sector)) {
1108 if (disk.Write(header, 512) == -1)
1109 allOK = 0;
1110 } else allOK = 0; // if (disk.Seek()...)
1111 if (!littleEndian)
1112 ReverseHeaderBytes(header);
1113 return allOK;
1114} // GPTData::SaveHeader()
1115
1116// Save the partitions to the specified sector. Used by both the SaveGPTData()
1117// and SaveGPTBackup() functions.
1118// Should be passed an architecture-appropriate header (DO NOT call
1119// ReverseHeaderBytes() on the header before calling this function)
1120// Returns 1 on success, 0 on failure
1121int GPTData::SavePartitionTable(DiskIO & disk, uint64_t sector) {
1122 int littleEndian, allOK = 1;
1123
1124 littleEndian = IsLittleEndian();
1125 if (disk.Seek(sector)) {
1126 if (!littleEndian)
1127 ReversePartitionBytes();
srs56940283dae2010-04-28 16:44:34 -04001128 if (disk.Write(partitions, mainHeader.sizeOfPartitionEntries * numParts) == -1)
srs5694cb76c672010-02-11 22:22:22 -05001129 allOK = 0;
1130 if (!littleEndian)
1131 ReversePartitionBytes();
1132 } else allOK = 0; // if (myDisk.Seek()...)
1133 return allOK;
1134} // GPTData::SavePartitionTable()
1135
srs5694e7b4ff92009-08-18 13:16:10 -04001136// Load GPT data from a backup file created by SaveGPTBackup(). This function
1137// does minimal error checking. It returns 1 if it completed successfully,
1138// 0 if there was a problem. In the latter case, it creates a new empty
1139// set of partitions.
srs56940a697312010-01-28 21:10:52 -05001140int GPTData::LoadGPTBackup(const string & filename) {
srs5694cb76c672010-02-11 22:22:22 -05001141 int allOK = 1, val, err;
srs56940283dae2010-04-28 16:44:34 -04001142 uint32_t sizeOfEntries;
srs5694cb76c672010-02-11 22:22:22 -05001143 int littleEndian = 1, shortBackup = 0;
srs5694546a9c72010-01-26 16:00:26 -05001144 DiskIO backupFile;
srs5694e7b4ff92009-08-18 13:16:10 -04001145
srs5694546a9c72010-01-26 16:00:26 -05001146 if (backupFile.OpenForRead(filename)) {
srs56942a9f5da2009-08-26 00:48:01 -04001147 if (IsLittleEndian() == 0)
1148 littleEndian = 0;
1149
srs5694e7b4ff92009-08-18 13:16:10 -04001150 // Let the MBRData class load the saved MBR...
srs5694546a9c72010-01-26 16:00:26 -05001151 protectiveMBR.ReadMBRData(&backupFile, 0); // 0 = don't check block size
srs5694815fb652011-03-18 12:35:56 -04001152 protectiveMBR.SetDisk(&myDisk);
srs5694e7b4ff92009-08-18 13:16:10 -04001153
srs5694cb76c672010-02-11 22:22:22 -05001154 LoadHeader(&mainHeader, backupFile, 1, &mainCrcOk);
srs5694e7b4ff92009-08-18 13:16:10 -04001155
srs5694cb76c672010-02-11 22:22:22 -05001156 // Check backup file size and rebuild second header if file is right
1157 // size to be direct dd copy of MBR, main header, and main partition
1158 // table; if other size, treat it like a GPT fdisk-generated backup
1159 // file
1160 shortBackup = ((backupFile.DiskSize(&err) * backupFile.GetBlockSize()) ==
1161 (mainHeader.numParts * mainHeader.sizeOfPartitionEntries) + 1024);
1162 if (shortBackup) {
1163 RebuildSecondHeader();
1164 secondCrcOk = mainCrcOk;
1165 } else {
1166 LoadHeader(&secondHeader, backupFile, 2, &secondCrcOk);
1167 } // if/else
srs56942a9f5da2009-08-26 00:48:01 -04001168
srs5694e7b4ff92009-08-18 13:16:10 -04001169 // Return valid headers code: 0 = both headers bad; 1 = main header
1170 // good, backup bad; 2 = backup header good, main header bad;
1171 // 3 = both headers good. Note these codes refer to valid GPT
1172 // signatures and version numbers; more subtle problems will elude
1173 // this check!
1174 if ((val = CheckHeaderValidity()) > 0) {
1175 if (val == 2) { // only backup header seems to be good
srs56940283dae2010-04-28 16:44:34 -04001176 SetGPTSize(secondHeader.numParts);
srs5694e4ac11e2009-08-31 10:13:04 -04001177 sizeOfEntries = secondHeader.sizeOfPartitionEntries;
srs5694e7b4ff92009-08-18 13:16:10 -04001178 } else { // main header is OK
srs56940283dae2010-04-28 16:44:34 -04001179 SetGPTSize(mainHeader.numParts);
srs5694e7b4ff92009-08-18 13:16:10 -04001180 sizeOfEntries = mainHeader.sizeOfPartitionEntries;
1181 } // if/else
1182
srs5694e7b4ff92009-08-18 13:16:10 -04001183 if (secondHeader.currentLBA != diskSize - UINT64_C(1)) {
srs5694fed16d02010-01-27 23:03:40 -05001184 cout << "Warning! Current disk size doesn't match that of the backup!\n"
1185 << "Adjusting sizes to match, but subsequent problems are possible!\n";
srs5694247657a2009-11-26 18:36:12 -05001186 MoveSecondHeaderToEnd();
srs5694e7b4ff92009-08-18 13:16:10 -04001187 } // if
1188
srs5694cb76c672010-02-11 22:22:22 -05001189 if (!LoadPartitionTable(mainHeader, backupFile, (uint64_t) (3 - shortBackup)))
1190 cerr << "Warning! Read error " << errno
1191 << " loading partition table; strange behavior now likely!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001192 } else {
1193 allOK = 0;
1194 } // if/else
srs5694a8582cf2010-03-19 14:21:59 -04001195 // Something went badly wrong, so blank out partitions
1196 if (allOK == 0) {
1197 cerr << "Improper backup file! Clearing all partition data!\n";
1198 ClearGPTData();
1199 protectiveMBR.MakeProtectiveMBR();
1200 } // if
srs5694e7b4ff92009-08-18 13:16:10 -04001201 } else {
1202 allOK = 0;
srs56945a608532011-03-17 13:53:01 -04001203 cerr << "Unable to open file '" << filename << "' for reading! Aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001204 } // if/else
1205
srs5694e7b4ff92009-08-18 13:16:10 -04001206 return allOK;
1207} // GPTData::LoadGPTBackup()
1208
srs569408bb0da2010-02-19 17:19:55 -05001209int GPTData::SaveMBR(void) {
srs569455d92612010-03-07 22:16:07 -05001210 return protectiveMBR.WriteMBRData(&myDisk);
srs569408bb0da2010-02-19 17:19:55 -05001211} // GPTData::SaveMBR()
1212
1213// This function destroys the on-disk GPT structures, but NOT the on-disk
1214// MBR.
1215// Returns 1 if the operation succeeds, 0 if not.
1216int GPTData::DestroyGPT(void) {
srs569401f7f082011-03-15 23:53:31 -04001217 int sum, tableSize, allOK = 1;
srs569408bb0da2010-02-19 17:19:55 -05001218 uint8_t blankSector[512];
1219 uint8_t* emptyTable;
1220
srs569401f7f082011-03-15 23:53:31 -04001221 memset(blankSector, 0, sizeof(blankSector));
srs569408bb0da2010-02-19 17:19:55 -05001222
1223 if (myDisk.OpenForWrite()) {
1224 if (!myDisk.Seek(mainHeader.currentLBA))
1225 allOK = 0;
1226 if (myDisk.Write(blankSector, 512) != 512) { // blank it out
1227 cerr << "Warning! GPT main header not overwritten! Error is " << errno << "\n";
1228 allOK = 0;
1229 } // if
1230 if (!myDisk.Seek(mainHeader.partitionEntriesLBA))
1231 allOK = 0;
srs56940283dae2010-04-28 16:44:34 -04001232 tableSize = numParts * mainHeader.sizeOfPartitionEntries;
srs569408bb0da2010-02-19 17:19:55 -05001233 emptyTable = new uint8_t[tableSize];
srs56946aae2a92011-06-10 01:16:51 -04001234 if (emptyTable == NULL) {
srs5694a17fe692011-09-10 20:30:20 -04001235 cerr << "Could not allocate memory in GPTData::DestroyGPT()! Terminating!\n";
srs56946aae2a92011-06-10 01:16:51 -04001236 exit(1);
1237 } // if
srs569401f7f082011-03-15 23:53:31 -04001238 memset(emptyTable, 0, tableSize);
srs569408bb0da2010-02-19 17:19:55 -05001239 if (allOK) {
1240 sum = myDisk.Write(emptyTable, tableSize);
1241 if (sum != tableSize) {
1242 cerr << "Warning! GPT main partition table not overwritten! Error is " << errno << "\n";
1243 allOK = 0;
1244 } // if write failed
1245 } // if
1246 if (!myDisk.Seek(secondHeader.partitionEntriesLBA))
1247 allOK = 0;
1248 if (allOK) {
1249 sum = myDisk.Write(emptyTable, tableSize);
1250 if (sum != tableSize) {
1251 cerr << "Warning! GPT backup partition table not overwritten! Error is "
1252 << errno << "\n";
1253 allOK = 0;
1254 } // if wrong size written
1255 } // if
1256 if (!myDisk.Seek(secondHeader.currentLBA))
1257 allOK = 0;
1258 if (allOK) {
1259 if (myDisk.Write(blankSector, 512) != 512) { // blank it out
1260 cerr << "Warning! GPT backup header not overwritten! Error is " << errno << "\n";
1261 allOK = 0;
1262 } // if
1263 } // if
1264 myDisk.DiskSync();
1265 myDisk.Close();
1266 cout << "GPT data structures destroyed! You may now partition the disk using fdisk or\n"
1267 << "other utilities.\n";
1268 delete[] emptyTable;
1269 } else {
srs56945a608532011-03-17 13:53:01 -04001270 cerr << "Problem opening '" << device << "' for writing! Program will now terminate.\n";
srs569408bb0da2010-02-19 17:19:55 -05001271 } // if/else (fd != -1)
1272 return (allOK);
1273} // GPTDataTextUI::DestroyGPT()
1274
1275// Wipe MBR data from the disk (zero it out completely)
1276// Returns 1 on success, 0 on failure.
1277int GPTData::DestroyMBR(void) {
srs569401f7f082011-03-15 23:53:31 -04001278 int allOK;
srs569408bb0da2010-02-19 17:19:55 -05001279 uint8_t blankSector[512];
1280
srs569401f7f082011-03-15 23:53:31 -04001281 memset(blankSector, 0, sizeof(blankSector));
srs569408bb0da2010-02-19 17:19:55 -05001282
srs569401f7f082011-03-15 23:53:31 -04001283 allOK = myDisk.OpenForWrite() && myDisk.Seek(0) && (myDisk.Write(blankSector, 512) == 512);
1284
srs569408bb0da2010-02-19 17:19:55 -05001285 if (!allOK)
1286 cerr << "Warning! MBR not overwritten! Error is " << errno << "!\n";
1287 return allOK;
1288} // GPTData::DestroyMBR(void)
1289
srs5694e4ac11e2009-08-31 10:13:04 -04001290// Tell user whether Apple Partition Map (APM) was discovered....
1291void GPTData::ShowAPMState(void) {
1292 if (apmFound)
srs5694fed16d02010-01-27 23:03:40 -05001293 cout << " APM: present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001294 else
srs5694fed16d02010-01-27 23:03:40 -05001295 cout << " APM: not present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001296} // GPTData::ShowAPMState()
1297
1298// Tell user about the state of the GPT data....
1299void GPTData::ShowGPTState(void) {
1300 switch (state) {
1301 case gpt_invalid:
srs5694fed16d02010-01-27 23:03:40 -05001302 cout << " GPT: not present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001303 break;
1304 case gpt_valid:
srs5694fed16d02010-01-27 23:03:40 -05001305 cout << " GPT: present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001306 break;
1307 case gpt_corrupt:
srs5694fed16d02010-01-27 23:03:40 -05001308 cout << " GPT: damaged\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001309 break;
1310 default:
srs5694fed16d02010-01-27 23:03:40 -05001311 cout << "\a GPT: unknown -- bug!\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001312 break;
1313 } // switch
1314} // GPTData::ShowGPTState()
1315
1316// Display the basic GPT data
1317void GPTData::DisplayGPTData(void) {
srs5694e321d442010-01-29 17:44:04 -05001318 uint32_t i;
srs5694e4ac11e2009-08-31 10:13:04 -04001319 uint64_t temp, totalFree;
1320
srs5694fed16d02010-01-27 23:03:40 -05001321 cout << "Disk " << device << ": " << diskSize << " sectors, "
srs569401f7f082011-03-15 23:53:31 -04001322 << BytesToIeee(diskSize, blockSize) << "\n";
srs5694fed16d02010-01-27 23:03:40 -05001323 cout << "Logical sector size: " << blockSize << " bytes\n";
srs56945a081752010-09-24 20:39:41 -04001324 cout << "Disk identifier (GUID): " << mainHeader.diskGUID << "\n";
srs56940283dae2010-04-28 16:44:34 -04001325 cout << "Partition table holds up to " << numParts << " entries\n";
srs5694fed16d02010-01-27 23:03:40 -05001326 cout << "First usable sector is " << mainHeader.firstUsableLBA
1327 << ", last usable sector is " << mainHeader.lastUsableLBA << "\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001328 totalFree = FindFreeBlocks(&i, &temp);
srs56948a4ddfc2010-03-21 19:05:49 -04001329 cout << "Partitions will be aligned on " << sectorAlignment << "-sector boundaries\n";
srs5694fed16d02010-01-27 23:03:40 -05001330 cout << "Total free space is " << totalFree << " sectors ("
srs569401f7f082011-03-15 23:53:31 -04001331 << BytesToIeee(totalFree, blockSize) << ")\n";
srs5694fed16d02010-01-27 23:03:40 -05001332 cout << "\nNumber Start (sector) End (sector) Size Code Name\n";
srs56940283dae2010-04-28 16:44:34 -04001333 for (i = 0; i < numParts; i++) {
srs5694978041c2009-09-21 20:51:47 -04001334 partitions[i].ShowSummary(i, blockSize);
srs5694e4ac11e2009-08-31 10:13:04 -04001335 } // for
1336} // GPTData::DisplayGPTData()
1337
srs5694e4ac11e2009-08-31 10:13:04 -04001338// Show detailed information on the specified partition
1339void GPTData::ShowPartDetails(uint32_t partNum) {
srs56940873e9d2010-10-07 13:00:45 -04001340 if (!IsFreePartNum(partNum)) {
srs5694e4ac11e2009-08-31 10:13:04 -04001341 partitions[partNum].ShowDetails(blockSize);
1342 } else {
srs5694fed16d02010-01-27 23:03:40 -05001343 cout << "Partition #" << partNum + 1 << " does not exist.";
srs5694e4ac11e2009-08-31 10:13:04 -04001344 } // if
1345} // GPTData::ShowPartDetails()
1346
srs5694e4ac11e2009-08-31 10:13:04 -04001347/**************************************************************************
1348 * *
1349 * Partition table transformation functions (MBR or BSD disklabel to GPT) *
1350 * (some of these functions may require user interaction) *
1351 * *
1352 **************************************************************************/
1353
srs569408bb0da2010-02-19 17:19:55 -05001354// Examines the MBR & GPT data to determine which set of data to use: the
1355// MBR (use_mbr), the GPT (use_gpt), the BSD disklabel (use_bsd), or create
1356// a new set of partitions (use_new). A return value of use_abort indicates
1357// that this function couldn't determine what to do. Overriding functions
1358// in derived classes may ask users questions in such cases.
srs5694e4ac11e2009-08-31 10:13:04 -04001359WhichToUse GPTData::UseWhichPartitions(void) {
1360 WhichToUse which = use_new;
1361 MBRValidity mbrState;
srs5694e4ac11e2009-08-31 10:13:04 -04001362
1363 mbrState = protectiveMBR.GetValidity();
1364
1365 if ((state == gpt_invalid) && ((mbrState == mbr) || (mbrState == hybrid))) {
srs5694fed16d02010-01-27 23:03:40 -05001366 cout << "\n***************************************************************\n"
1367 << "Found invalid GPT and valid MBR; converting MBR to GPT format.\n";
srs56945d58fe02010-01-03 20:57:08 -05001368 if (!justLooking) {
srs56940283dae2010-04-28 16:44:34 -04001369 cout << "\aTHIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by typing 'q' if\n"
srs5694fed16d02010-01-27 23:03:40 -05001370 << "you don't want to convert your MBR partitions to GPT format!\n";
srs56945d58fe02010-01-03 20:57:08 -05001371 } // if
srs5694fed16d02010-01-27 23:03:40 -05001372 cout << "***************************************************************\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001373 which = use_mbr;
1374 } // if
1375
1376 if ((state == gpt_invalid) && bsdFound) {
srs5694fed16d02010-01-27 23:03:40 -05001377 cout << "\n**********************************************************************\n"
1378 << "Found invalid GPT and valid BSD disklabel; converting BSD disklabel\n"
1379 << "to GPT format.";
srs56940a697312010-01-28 21:10:52 -05001380 if ((!justLooking) && (!beQuiet)) {
srs56940283dae2010-04-28 16:44:34 -04001381 cout << "\a THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Your first\n"
srs5694fed16d02010-01-27 23:03:40 -05001382 << "BSD partition will likely be unusable. Exit by typing 'q' if you don't\n"
1383 << "want to convert your BSD partitions to GPT format!";
srs56945d58fe02010-01-03 20:57:08 -05001384 } // if
srs5694fed16d02010-01-27 23:03:40 -05001385 cout << "\n**********************************************************************\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001386 which = use_bsd;
1387 } // if
1388
1389 if ((state == gpt_valid) && (mbrState == gpt)) {
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 protective MBR; using GPT.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001393 } // if
1394 if ((state == gpt_valid) && (mbrState == hybrid)) {
srs5694e4ac11e2009-08-31 10:13:04 -04001395 which = use_gpt;
srs56943c0af382010-01-15 19:19:18 -05001396 if (!beQuiet)
srs5694fed16d02010-01-27 23:03:40 -05001397 cout << "Found valid GPT with hybrid MBR; using GPT.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001398 } // if
1399 if ((state == gpt_valid) && (mbrState == invalid)) {
srs56940a697312010-01-28 21:10:52 -05001400 cout << "\aFound valid GPT with corrupt MBR; using GPT and will write new\n"
srs5694fed16d02010-01-27 23:03:40 -05001401 << "protective MBR on save.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001402 which = use_gpt;
srs5694e4ac11e2009-08-31 10:13:04 -04001403 } // if
1404 if ((state == gpt_valid) && (mbrState == mbr)) {
srs569408bb0da2010-02-19 17:19:55 -05001405 which = use_abort;
srs5694e4ac11e2009-08-31 10:13:04 -04001406 } // if
1407
srs5694e4ac11e2009-08-31 10:13:04 -04001408 if (state == gpt_corrupt) {
srs569408bb0da2010-02-19 17:19:55 -05001409 if (mbrState == gpt) {
1410 cout << "\a\a****************************************************************************\n"
1411 << "Caution: Found protective or hybrid MBR and corrupt GPT. Using GPT, but disk\n"
1412 << "verification and recovery are STRONGLY recommended.\n"
1413 << "****************************************************************************\n";
1414 which = use_gpt;
srs56943c0af382010-01-15 19:19:18 -05001415 } else {
srs569408bb0da2010-02-19 17:19:55 -05001416 which = use_abort;
1417 } // if/else MBR says disk is GPT
1418 } // if GPT corrupt
srs5694e4ac11e2009-08-31 10:13:04 -04001419
1420 if (which == use_new)
srs5694fed16d02010-01-27 23:03:40 -05001421 cout << "Creating new GPT entries.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001422
1423 return which;
1424} // UseWhichPartitions()
1425
srs569408bb0da2010-02-19 17:19:55 -05001426// Convert MBR partition table into GPT form.
1427void GPTData::XFormPartitions(void) {
srs5694e4ac11e2009-08-31 10:13:04 -04001428 int i, numToConvert;
1429 uint8_t origType;
srs5694e4ac11e2009-08-31 10:13:04 -04001430
1431 // Clear out old data & prepare basics....
1432 ClearGPTData();
1433
1434 // Convert the smaller of the # of GPT or MBR partitions
srs56940283dae2010-04-28 16:44:34 -04001435 if (numParts > MAX_MBR_PARTS)
srs5694978041c2009-09-21 20:51:47 -04001436 numToConvert = MAX_MBR_PARTS;
srs5694e4ac11e2009-08-31 10:13:04 -04001437 else
srs56940283dae2010-04-28 16:44:34 -04001438 numToConvert = numParts;
srs5694e4ac11e2009-08-31 10:13:04 -04001439
1440 for (i = 0; i < numToConvert; i++) {
1441 origType = protectiveMBR.GetType(i);
1442 // don't waste CPU time trying to convert extended, hybrid protective, or
1443 // null (non-existent) partitions
srs5694e35eb1b2009-09-14 00:29:34 -04001444 if ((origType != 0x05) && (origType != 0x0f) && (origType != 0x85) &&
srs56946699b012010-02-04 00:55:30 -05001445 (origType != 0x00) && (origType != 0xEE))
srs5694e4ac11e2009-08-31 10:13:04 -04001446 partitions[i] = protectiveMBR.AsGPT(i);
1447 } // for
1448
1449 // Convert MBR into protective MBR
1450 protectiveMBR.MakeProtectiveMBR();
1451
1452 // Record that all original CRCs were OK so as not to raise flags
1453 // when doing a disk verification
1454 mainCrcOk = secondCrcOk = mainPartsCrcOk = secondPartsCrcOk = 1;
srs5694e4ac11e2009-08-31 10:13:04 -04001455} // GPTData::XFormPartitions()
1456
1457// Transforms BSD disklabel on the specified partition (numbered from 0).
srs569408bb0da2010-02-19 17:19:55 -05001458// If an invalid partition number is given, the program does nothing.
srs5694e4ac11e2009-08-31 10:13:04 -04001459// Returns the number of new partitions created.
srs569408bb0da2010-02-19 17:19:55 -05001460int GPTData::XFormDisklabel(uint32_t partNum) {
1461 uint32_t low, high;
srs5694e4ac11e2009-08-31 10:13:04 -04001462 int goOn = 1, numDone = 0;
1463 BSDData disklabel;
1464
srs569408bb0da2010-02-19 17:19:55 -05001465 if (GetPartRange(&low, &high) == 0) {
1466 goOn = 0;
1467 cout << "No partitions!\n";
1468 } // if
1469 if (partNum > high) {
1470 goOn = 0;
1471 cout << "Specified partition is invalid!\n";
1472 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04001473
srs569408bb0da2010-02-19 17:19:55 -05001474 // If all is OK, read the disklabel and convert it.
1475 if (goOn) {
1476 goOn = disklabel.ReadBSDData(&myDisk, partitions[partNum].GetFirstLBA(),
1477 partitions[partNum].GetLastLBA());
1478 if ((goOn) && (disklabel.IsDisklabel())) {
1479 numDone = XFormDisklabel(&disklabel);
1480 if (numDone == 1)
1481 cout << "Converted 1 BSD partition.\n";
1482 else
1483 cout << "Converted " << numDone << " BSD partitions.\n";
1484 } else {
1485 cout << "Unable to convert partitions! Unrecognized BSD disklabel.\n";
1486 } // if/else
1487 } // if
1488 if (numDone > 0) { // converted partitions; delete carrier
1489 partitions[partNum].BlankPartition();
1490 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04001491 return numDone;
srs569455d92612010-03-07 22:16:07 -05001492} // GPTData::XFormDisklabel(uint32_t i)
srs5694e4ac11e2009-08-31 10:13:04 -04001493
1494// Transform the partitions on an already-loaded BSD disklabel...
srs569408bb0da2010-02-19 17:19:55 -05001495int GPTData::XFormDisklabel(BSDData* disklabel) {
1496 int i, partNum = 0, numDone = 0;
srs5694e4ac11e2009-08-31 10:13:04 -04001497
srs569408bb0da2010-02-19 17:19:55 -05001498 if (disklabel->IsDisklabel()) {
srs5694e4ac11e2009-08-31 10:13:04 -04001499 for (i = 0; i < disklabel->GetNumParts(); i++) {
srs569408bb0da2010-02-19 17:19:55 -05001500 partNum = FindFirstFreePart();
1501 if (partNum >= 0) {
1502 partitions[partNum] = disklabel->AsGPT(i);
1503 if (partitions[partNum].IsUsed())
1504 numDone++;
1505 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04001506 } // for
srs569408bb0da2010-02-19 17:19:55 -05001507 if (partNum == -1)
1508 cerr << "Warning! Too many partitions to convert!\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001509 } // if
1510
1511 // Record that all original CRCs were OK so as not to raise flags
1512 // when doing a disk verification
1513 mainCrcOk = secondCrcOk = mainPartsCrcOk = secondPartsCrcOk = 1;
1514
1515 return numDone;
1516} // GPTData::XFormDisklabel(BSDData* disklabel)
1517
srs569408bb0da2010-02-19 17:19:55 -05001518// Add one GPT partition to MBR. Used by PartsToMBR() functions. Created
1519// partition has the active/bootable flag UNset and uses the GPT fdisk
1520// type code divided by 0x0100 as the MBR type code.
1521// Returns 1 if operation was 100% successful, 0 if there were ANY
1522// problems.
srs5694978041c2009-09-21 20:51:47 -04001523int GPTData::OnePartToMBR(uint32_t gptPart, int mbrPart) {
srs569408bb0da2010-02-19 17:19:55 -05001524 int allOK = 1;
srs5694fed16d02010-01-27 23:03:40 -05001525
srs5694978041c2009-09-21 20:51:47 -04001526 if ((mbrPart < 0) || (mbrPart > 3)) {
srs5694fed16d02010-01-27 23:03:40 -05001527 cout << "MBR partition " << mbrPart + 1 << " is out of range; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001528 allOK = 0;
1529 } // if
srs56940283dae2010-04-28 16:44:34 -04001530 if (gptPart >= numParts) {
srs5694fed16d02010-01-27 23:03:40 -05001531 cout << "GPT partition " << gptPart + 1 << " is out of range; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001532 allOK = 0;
1533 } // if
1534 if (allOK && (partitions[gptPart].GetLastLBA() == UINT64_C(0))) {
srs5694fed16d02010-01-27 23:03:40 -05001535 cout << "GPT partition " << gptPart + 1 << " is undefined; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001536 allOK = 0;
1537 } // if
1538 if (allOK && (partitions[gptPart].GetFirstLBA() <= UINT32_MAX) &&
1539 (partitions[gptPart].GetLengthLBA() <= UINT32_MAX)) {
1540 if (partitions[gptPart].GetLastLBA() > UINT32_MAX) {
srs5694fed16d02010-01-27 23:03:40 -05001541 cout << "Caution: Partition end point past 32-bit pointer boundary;"
1542 << " some OSes may\nreact strangely.\n";
srs569408bb0da2010-02-19 17:19:55 -05001543 } // if
srs5694978041c2009-09-21 20:51:47 -04001544 protectiveMBR.MakePart(mbrPart, (uint32_t) partitions[gptPart].GetFirstLBA(),
srs569408bb0da2010-02-19 17:19:55 -05001545 (uint32_t) partitions[gptPart].GetLengthLBA(),
1546 partitions[gptPart].GetHexType() / 256, 0);
srs5694978041c2009-09-21 20:51:47 -04001547 } else { // partition out of range
srs569408bb0da2010-02-19 17:19:55 -05001548 if (allOK) // Display only if "else" triggered by out-of-bounds condition
1549 cout << "Partition " << gptPart + 1 << " begins beyond the 32-bit pointer limit of MBR "
1550 << "partitions, or is\n too big; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001551 allOK = 0;
1552 } // if/else
1553 return allOK;
1554} // GPTData::OnePartToMBR()
1555
srs5694e4ac11e2009-08-31 10:13:04 -04001556
1557/**********************************************************************
1558 * *
1559 * Functions that adjust GPT data structures WITHOUT user interaction *
1560 * (they may display information for the user's benefit, though) *
1561 * *
1562 **********************************************************************/
1563
1564// Resizes GPT to specified number of entries. Creates a new table if
srs5694ba00fed2010-01-12 18:18:36 -05001565// necessary, copies data if it already exists. Returns 1 if all goes
1566// well, 0 if an error is encountered.
srs5694e4ac11e2009-08-31 10:13:04 -04001567int GPTData::SetGPTSize(uint32_t numEntries) {
srs569408bb0da2010-02-19 17:19:55 -05001568 GPTPart* newParts;
srs5694e4ac11e2009-08-31 10:13:04 -04001569 uint32_t i, high, copyNum;
1570 int allOK = 1;
1571
1572 // First, adjust numEntries upward, if necessary, to get a number
1573 // that fills the allocated sectors
1574 i = blockSize / GPT_SIZE;
1575 if ((numEntries % i) != 0) {
srs5694fed16d02010-01-27 23:03:40 -05001576 cout << "Adjusting GPT size from " << numEntries << " to ";
srs5694e4ac11e2009-08-31 10:13:04 -04001577 numEntries = ((numEntries / i) + 1) * i;
srs5694fed16d02010-01-27 23:03:40 -05001578 cout << numEntries << " to fill the sector\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001579 } // if
1580
srs5694247657a2009-11-26 18:36:12 -05001581 // Do the work only if the # of partitions is changing. Along with being
srs569455d92612010-03-07 22:16:07 -05001582 // efficient, this prevents mucking with the location of the secondary
srs5694247657a2009-11-26 18:36:12 -05001583 // partition table, which causes problems when loading data from a RAID
1584 // array that's been expanded because this function is called when loading
1585 // data.
srs56940283dae2010-04-28 16:44:34 -04001586 if (((numEntries != numParts) || (partitions == NULL)) && (numEntries > 0)) {
srs569401f7f082011-03-15 23:53:31 -04001587 newParts = new GPTPart [numEntries];
srs5694247657a2009-11-26 18:36:12 -05001588 if (newParts != NULL) {
1589 if (partitions != NULL) { // existing partitions; copy them over
1590 GetPartRange(&i, &high);
1591 if (numEntries < (high + 1)) { // Highest entry too high for new #
srs5694fed16d02010-01-27 23:03:40 -05001592 cout << "The highest-numbered partition is " << high + 1
1593 << ", which is greater than the requested\n"
1594 << "partition table size of " << numEntries
1595 << "; cannot resize. Perhaps sorting will help.\n";
srs5694247657a2009-11-26 18:36:12 -05001596 allOK = 0;
srs5694815fb652011-03-18 12:35:56 -04001597 delete[] newParts;
srs5694247657a2009-11-26 18:36:12 -05001598 } else { // go ahead with copy
srs56940283dae2010-04-28 16:44:34 -04001599 if (numEntries < numParts)
srs5694247657a2009-11-26 18:36:12 -05001600 copyNum = numEntries;
1601 else
srs56940283dae2010-04-28 16:44:34 -04001602 copyNum = numParts;
srs5694247657a2009-11-26 18:36:12 -05001603 for (i = 0; i < copyNum; i++) {
1604 newParts[i] = partitions[i];
1605 } // for
srs569401f7f082011-03-15 23:53:31 -04001606 delete[] partitions;
srs5694247657a2009-11-26 18:36:12 -05001607 partitions = newParts;
srs5694247657a2009-11-26 18:36:12 -05001608 } // if
1609 } else { // No existing partition table; just create it
srs5694e4ac11e2009-08-31 10:13:04 -04001610 partitions = newParts;
srs5694247657a2009-11-26 18:36:12 -05001611 } // if/else existing partitions
srs56940283dae2010-04-28 16:44:34 -04001612 numParts = numEntries;
srs5694247657a2009-11-26 18:36:12 -05001613 mainHeader.firstUsableLBA = ((numEntries * GPT_SIZE) / blockSize) + 2 ;
1614 secondHeader.firstUsableLBA = mainHeader.firstUsableLBA;
1615 MoveSecondHeaderToEnd();
1616 if (diskSize > 0)
1617 CheckGPTSize();
1618 } else { // Bad memory allocation
srs56946aae2a92011-06-10 01:16:51 -04001619 cerr << "Error allocating memory for partition table! Size is unchanged!\n";
srs5694247657a2009-11-26 18:36:12 -05001620 allOK = 0;
1621 } // if/else
srs5694e4ac11e2009-08-31 10:13:04 -04001622 } // if/else
srs56940283dae2010-04-28 16:44:34 -04001623 mainHeader.numParts = numParts;
1624 secondHeader.numParts = numParts;
srs5694e4ac11e2009-08-31 10:13:04 -04001625 return (allOK);
1626} // GPTData::SetGPTSize()
1627
1628// Blank the partition array
1629void GPTData::BlankPartitions(void) {
1630 uint32_t i;
1631
srs56940283dae2010-04-28 16:44:34 -04001632 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04001633 partitions[i].BlankPartition();
1634 } // for
1635} // GPTData::BlankPartitions()
1636
srs5694ba00fed2010-01-12 18:18:36 -05001637// Delete a partition by number. Returns 1 if successful,
1638// 0 if there was a problem. Returns 1 if partition was in
1639// range, 0 if it was out of range.
1640int GPTData::DeletePartition(uint32_t partNum) {
1641 uint64_t startSector, length;
srs56940283dae2010-04-28 16:44:34 -04001642 uint32_t low, high, numUsedParts, retval = 1;;
srs5694ba00fed2010-01-12 18:18:36 -05001643
srs56940283dae2010-04-28 16:44:34 -04001644 numUsedParts = GetPartRange(&low, &high);
1645 if ((numUsedParts > 0) && (partNum >= low) && (partNum <= high)) {
srs5694ba00fed2010-01-12 18:18:36 -05001646 // In case there's a protective MBR, look for & delete matching
1647 // MBR partition....
1648 startSector = partitions[partNum].GetFirstLBA();
1649 length = partitions[partNum].GetLengthLBA();
1650 protectiveMBR.DeleteByLocation(startSector, length);
1651
1652 // Now delete the GPT partition
1653 partitions[partNum].BlankPartition();
1654 } else {
srs5694fed16d02010-01-27 23:03:40 -05001655 cerr << "Partition number " << partNum + 1 << " out of range!\n";
srs5694ba00fed2010-01-12 18:18:36 -05001656 retval = 0;
1657 } // if/else
1658 return retval;
1659} // GPTData::DeletePartition(uint32_t partNum)
1660
srs569408bb0da2010-02-19 17:19:55 -05001661// Non-interactively create a partition.
1662// Returns 1 if the operation was successful, 0 if a problem was discovered.
srs5694e321d442010-01-29 17:44:04 -05001663uint32_t GPTData::CreatePartition(uint32_t partNum, uint64_t startSector, uint64_t endSector) {
srs5694ba00fed2010-01-12 18:18:36 -05001664 int retval = 1; // assume there'll be no problems
srs56945a081752010-09-24 20:39:41 -04001665 uint64_t origSector = startSector;
srs5694ba00fed2010-01-12 18:18:36 -05001666
1667 if (IsFreePartNum(partNum)) {
srs56945a081752010-09-24 20:39:41 -04001668 if (Align(&startSector)) {
1669 cout << "Information: Moved requested sector from " << origSector << " to "
1670 << startSector << " in\norder to align on " << sectorAlignment
1671 << "-sector boundaries.\n";
1672 } // if
srs5694ba00fed2010-01-12 18:18:36 -05001673 if (IsFree(startSector) && (startSector <= endSector)) {
1674 if (FindLastInFree(startSector) >= endSector) {
1675 partitions[partNum].SetFirstLBA(startSector);
1676 partitions[partNum].SetLastLBA(endSector);
srs569400b6d7a2011-06-26 22:40:06 -04001677 partitions[partNum].SetType(DEFAULT_TYPE);
srs56946699b012010-02-04 00:55:30 -05001678 partitions[partNum].RandomizeUniqueGUID();
srs5694ba00fed2010-01-12 18:18:36 -05001679 } else retval = 0; // if free space until endSector
1680 } else retval = 0; // if startSector is free
1681 } else retval = 0; // if legal partition number
1682 return retval;
1683} // GPTData::CreatePartition(partNum, startSector, endSector)
1684
srs5694e4ac11e2009-08-31 10:13:04 -04001685// Sort the GPT entries, eliminating gaps and making for a logical
srs56949a46b042011-03-15 00:34:10 -04001686// ordering.
srs5694e4ac11e2009-08-31 10:13:04 -04001687void GPTData::SortGPT(void) {
srs56949a46b042011-03-15 00:34:10 -04001688 if (numParts > 0)
srs569401f7f082011-03-15 23:53:31 -04001689 sort(partitions, partitions + numParts);
srs5694e4ac11e2009-08-31 10:13:04 -04001690} // GPTData::SortGPT()
1691
srs569408bb0da2010-02-19 17:19:55 -05001692// Swap the contents of two partitions.
1693// Returns 1 if successful, 0 if either partition is out of range
1694// (that is, not a legal number; either or both can be empty).
1695// Note that if partNum1 = partNum2 and this number is in range,
1696// it will be considered successful.
1697int GPTData::SwapPartitions(uint32_t partNum1, uint32_t partNum2) {
1698 GPTPart temp;
1699 int allOK = 1;
1700
srs56940283dae2010-04-28 16:44:34 -04001701 if ((partNum1 < numParts) && (partNum2 < numParts)) {
srs569408bb0da2010-02-19 17:19:55 -05001702 if (partNum1 != partNum2) {
1703 temp = partitions[partNum1];
1704 partitions[partNum1] = partitions[partNum2];
1705 partitions[partNum2] = temp;
1706 } // if
1707 } else allOK = 0; // partition numbers are valid
1708 return allOK;
1709} // GPTData::SwapPartitions()
1710
srs5694e4ac11e2009-08-31 10:13:04 -04001711// Set up data structures for entirely new set of partitions on the
1712// specified device. Returns 1 if OK, 0 if there were problems.
srs5694e35eb1b2009-09-14 00:29:34 -04001713// Note that this function does NOT clear the protectiveMBR data
1714// structure, since it may hold the original MBR partitions if the
1715// program was launched on an MBR disk, and those may need to be
1716// converted to GPT format.
srs5694e4ac11e2009-08-31 10:13:04 -04001717int GPTData::ClearGPTData(void) {
srs5694e35eb1b2009-09-14 00:29:34 -04001718 int goOn = 1, i;
srs5694e4ac11e2009-08-31 10:13:04 -04001719
1720 // Set up the partition table....
srs56949a46b042011-03-15 00:34:10 -04001721 delete[] partitions;
srs5694e4ac11e2009-08-31 10:13:04 -04001722 partitions = NULL;
1723 SetGPTSize(NUM_GPT_ENTRIES);
1724
1725 // Now initialize a bunch of stuff that's static....
1726 mainHeader.signature = GPT_SIGNATURE;
1727 mainHeader.revision = 0x00010000;
srs5694978041c2009-09-21 20:51:47 -04001728 mainHeader.headerSize = HEADER_SIZE;
srs5694e4ac11e2009-08-31 10:13:04 -04001729 mainHeader.reserved = 0;
1730 mainHeader.currentLBA = UINT64_C(1);
1731 mainHeader.partitionEntriesLBA = (uint64_t) 2;
1732 mainHeader.sizeOfPartitionEntries = GPT_SIZE;
1733 for (i = 0; i < GPT_RESERVED; i++) {
1734 mainHeader.reserved2[i] = '\0';
1735 } // for
srs56940873e9d2010-10-07 13:00:45 -04001736 if (blockSize > 0)
1737 sectorAlignment = DEFAULT_ALIGNMENT * SECTOR_SIZE / blockSize;
1738 else
1739 sectorAlignment = DEFAULT_ALIGNMENT;
srs5694e4ac11e2009-08-31 10:13:04 -04001740
1741 // Now some semi-static items (computed based on end of disk)
1742 mainHeader.backupLBA = diskSize - UINT64_C(1);
1743 mainHeader.lastUsableLBA = diskSize - mainHeader.firstUsableLBA;
1744
1745 // Set a unique GUID for the disk, based on random numbers
srs56946699b012010-02-04 00:55:30 -05001746 mainHeader.diskGUID.Randomize();
srs5694e4ac11e2009-08-31 10:13:04 -04001747
1748 // Copy main header to backup header
1749 RebuildSecondHeader();
1750
1751 // Blank out the partitions array....
1752 BlankPartitions();
1753
1754 // Flag all CRCs as being OK....
1755 mainCrcOk = 1;
1756 secondCrcOk = 1;
1757 mainPartsCrcOk = 1;
1758 secondPartsCrcOk = 1;
1759
1760 return (goOn);
1761} // GPTData::ClearGPTData()
1762
srs5694247657a2009-11-26 18:36:12 -05001763// Set the location of the second GPT header data to the end of the disk.
srs569464cbd172011-03-01 22:03:54 -05001764// If the disk size has actually changed, this also adjusts the protective
1765// entry in the MBR, since it's probably no longer correct.
srs5694247657a2009-11-26 18:36:12 -05001766// Used internally and called by the 'e' option on the recovery &
1767// transformation menu, to help users of RAID arrays who add disk space
srs569464cbd172011-03-01 22:03:54 -05001768// to their arrays or to adjust data structures in restore operations
1769// involving unequal-sized disks.
srs5694247657a2009-11-26 18:36:12 -05001770void GPTData::MoveSecondHeaderToEnd() {
srs56948bb78762009-11-24 15:43:49 -05001771 mainHeader.backupLBA = secondHeader.currentLBA = diskSize - UINT64_C(1);
srs569464cbd172011-03-01 22:03:54 -05001772 if (mainHeader.lastUsableLBA != diskSize - mainHeader.firstUsableLBA) {
1773 if (protectiveMBR.GetValidity() == hybrid) {
1774 protectiveMBR.OptimizeEESize();
1775 RecomputeCHS();
1776 } // if
1777 if (protectiveMBR.GetValidity() == gpt)
1778 MakeProtectiveMBR();
1779 } // if
srs56948bb78762009-11-24 15:43:49 -05001780 mainHeader.lastUsableLBA = secondHeader.lastUsableLBA = diskSize - mainHeader.firstUsableLBA;
1781 secondHeader.partitionEntriesLBA = secondHeader.lastUsableLBA + UINT64_C(1);
1782} // GPTData::FixSecondHeaderLocation()
1783
srs5694699941e2011-03-21 21:33:57 -04001784// Sets the partition's name to the specified UnicodeString without
1785// user interaction.
1786// Returns 1 on success, 0 on failure (invalid partition number).
srs56945a608532011-03-17 13:53:01 -04001787int GPTData::SetName(uint32_t partNum, const UnicodeString & theName) {
srs5694ba00fed2010-01-12 18:18:36 -05001788 int retval = 1;
srs5694fed16d02010-01-27 23:03:40 -05001789
srs5694699941e2011-03-21 21:33:57 -04001790 if (IsUsedPartNum(partNum))
srs5694fed16d02010-01-27 23:03:40 -05001791 partitions[partNum].SetName(theName);
srs5694699941e2011-03-21 21:33:57 -04001792 else
1793 retval = 0;
srs5694ba00fed2010-01-12 18:18:36 -05001794
1795 return retval;
srs5694e4ac11e2009-08-31 10:13:04 -04001796} // GPTData::SetName
1797
1798// Set the disk GUID to the specified value. Note that the header CRCs must
1799// be recomputed after calling this function.
1800void GPTData::SetDiskGUID(GUIDData newGUID) {
1801 mainHeader.diskGUID = newGUID;
1802 secondHeader.diskGUID = newGUID;
1803} // SetDiskGUID()
1804
1805// Set the unique GUID of the specified partition. Returns 1 on
1806// successful completion, 0 if there were problems (invalid
1807// partition number).
1808int GPTData::SetPartitionGUID(uint32_t pn, GUIDData theGUID) {
1809 int retval = 0;
1810
srs56940283dae2010-04-28 16:44:34 -04001811 if (pn < numParts) {
srs5694e4ac11e2009-08-31 10:13:04 -04001812 if (partitions[pn].GetFirstLBA() != UINT64_C(0)) {
1813 partitions[pn].SetUniqueGUID(theGUID);
1814 retval = 1;
1815 } // if
1816 } // if
1817 return retval;
1818} // GPTData::SetPartitionGUID()
1819
srs56949ba54212010-05-18 23:24:02 -04001820// Set new random GUIDs for the disk and all partitions. Intended to be used
1821// after disk cloning or similar operations that don't randomize the GUIDs.
1822void GPTData::RandomizeGUIDs(void) {
1823 uint32_t i;
1824
1825 mainHeader.diskGUID.Randomize();
1826 secondHeader.diskGUID = mainHeader.diskGUID;
1827 for (i = 0; i < numParts; i++)
1828 if (partitions[i].IsUsed())
1829 partitions[i].RandomizeUniqueGUID();
1830} // GPTData::RandomizeGUIDs()
1831
srs5694ba00fed2010-01-12 18:18:36 -05001832// Change partition type code non-interactively. Returns 1 if
1833// successful, 0 if not....
srs5694327129e2010-09-22 01:07:31 -04001834int GPTData::ChangePartType(uint32_t partNum, PartType theGUID) {
1835 int retval = 1;
1836
1837 if (!IsFreePartNum(partNum)) {
1838 partitions[partNum].SetType(theGUID);
1839 } else retval = 0;
1840 return retval;
1841} // GPTData::ChangePartType()
1842
srs56949ba54212010-05-18 23:24:02 -04001843// Recompute the CHS values of all the MBR partitions. Used to reset
1844// CHS values that some BIOSes require, despite the fact that the
1845// resulting CHS values violate the GPT standard.
1846void GPTData::RecomputeCHS(void) {
1847 int i;
1848
1849 for (i = 0; i < 4; i++)
1850 protectiveMBR.RecomputeCHS(i);
1851} // GPTData::RecomputeCHS()
1852
srs56941d1448a2009-12-31 21:20:19 -05001853// Adjust sector number so that it falls on a sector boundary that's a
1854// multiple of sectorAlignment. This is done to improve the performance
1855// of Western Digital Advanced Format disks and disks with similar
1856// technology from other companies, which use 4096-byte sectors
1857// internally although they translate to 512-byte sectors for the
1858// benefit of the OS. If partitions aren't properly aligned on these
1859// disks, some filesystem data structures can span multiple physical
1860// sectors, degrading performance. This function should be called
1861// only on the FIRST sector of the partition, not the last!
1862// This function returns 1 if the alignment was altered, 0 if it
1863// was unchanged.
1864int GPTData::Align(uint64_t* sector) {
1865 int retval = 0, sectorOK = 0;
srs569400b6d7a2011-06-26 22:40:06 -04001866 uint64_t earlier, later, testSector;
srs56941d1448a2009-12-31 21:20:19 -05001867
1868 if ((*sector % sectorAlignment) != 0) {
srs56941d1448a2009-12-31 21:20:19 -05001869 earlier = (*sector / sectorAlignment) * sectorAlignment;
1870 later = earlier + (uint64_t) sectorAlignment;
1871
1872 // Check to see that every sector between the earlier one and the
1873 // requested one is clear, and that it's not too early....
1874 if (earlier >= mainHeader.firstUsableLBA) {
srs56941d1448a2009-12-31 21:20:19 -05001875 sectorOK = 1;
1876 testSector = earlier;
1877 do {
1878 sectorOK = IsFree(testSector++);
1879 } while ((sectorOK == 1) && (testSector < *sector));
1880 if (sectorOK == 1) {
1881 *sector = earlier;
srs56945a081752010-09-24 20:39:41 -04001882 retval = 1;
srs56941d1448a2009-12-31 21:20:19 -05001883 } // if
1884 } // if firstUsableLBA check
1885
1886 // If couldn't move the sector earlier, try to move it later instead....
1887 if ((sectorOK != 1) && (later <= mainHeader.lastUsableLBA)) {
1888 sectorOK = 1;
1889 testSector = later;
1890 do {
1891 sectorOK = IsFree(testSector--);
1892 } while ((sectorOK == 1) && (testSector > *sector));
1893 if (sectorOK == 1) {
1894 *sector = later;
srs56945a081752010-09-24 20:39:41 -04001895 retval = 1;
srs56941d1448a2009-12-31 21:20:19 -05001896 } // if
1897 } // if
srs56941d1448a2009-12-31 21:20:19 -05001898 } // if
1899 return retval;
1900} // GPTData::Align()
1901
srs5694e4ac11e2009-08-31 10:13:04 -04001902/********************************************************
1903 * *
1904 * Functions that return data about GPT data structures *
1905 * (most of these are inline in gpt.h) *
1906 * *
1907 ********************************************************/
1908
1909// Find the low and high used partition numbers (numbered from 0).
1910// Return value is the number of partitions found. Note that the
1911// *low and *high values are both set to 0 when no partitions
1912// are found, as well as when a single partition in the first
1913// position exists. Thus, the return value is the only way to
1914// tell when no partitions exist.
1915int GPTData::GetPartRange(uint32_t *low, uint32_t *high) {
1916 uint32_t i;
1917 int numFound = 0;
1918
srs56940283dae2010-04-28 16:44:34 -04001919 *low = numParts + 1; // code for "not found"
srs5694e4ac11e2009-08-31 10:13:04 -04001920 *high = 0;
srs56949a46b042011-03-15 00:34:10 -04001921 for (i = 0; i < numParts; i++) {
1922 if (partitions[i].GetFirstLBA() != UINT64_C(0)) { // it exists
1923 *high = i; // since we're counting up, set the high value
1924 // Set the low value only if it's not yet found...
1925 if (*low == (numParts + 1)) *low = i;
1926 numFound++;
1927 } // if
1928 } // for
srs5694e4ac11e2009-08-31 10:13:04 -04001929
1930 // Above will leave *low pointing to its "not found" value if no partitions
1931 // are defined, so reset to 0 if this is the case....
srs56940283dae2010-04-28 16:44:34 -04001932 if (*low == (numParts + 1))
srs5694e4ac11e2009-08-31 10:13:04 -04001933 *low = 0;
1934 return numFound;
1935} // GPTData::GetPartRange()
1936
srs569408bb0da2010-02-19 17:19:55 -05001937// Returns the value of the first free partition, or -1 if none is
1938// unused.
1939int GPTData::FindFirstFreePart(void) {
1940 int i = 0;
1941
1942 if (partitions != NULL) {
srs56949a46b042011-03-15 00:34:10 -04001943 while ((i < (int) numParts) && (partitions[i].IsUsed()))
srs569408bb0da2010-02-19 17:19:55 -05001944 i++;
srs56940283dae2010-04-28 16:44:34 -04001945 if (i >= (int) numParts)
srs569408bb0da2010-02-19 17:19:55 -05001946 i = -1;
1947 } else i = -1;
1948 return i;
1949} // GPTData::FindFirstFreePart()
1950
srs5694978041c2009-09-21 20:51:47 -04001951// Returns the number of defined partitions.
1952uint32_t GPTData::CountParts(void) {
srs5694e321d442010-01-29 17:44:04 -05001953 uint32_t i, counted = 0;
srs5694978041c2009-09-21 20:51:47 -04001954
srs56940283dae2010-04-28 16:44:34 -04001955 for (i = 0; i < numParts; i++) {
srs569408bb0da2010-02-19 17:19:55 -05001956 if (partitions[i].IsUsed())
srs5694978041c2009-09-21 20:51:47 -04001957 counted++;
1958 } // for
1959 return counted;
1960} // GPTData::CountParts()
1961
srs5694e4ac11e2009-08-31 10:13:04 -04001962/****************************************************
1963 * *
1964 * Functions that return data about disk free space *
1965 * *
1966 ****************************************************/
1967
1968// Find the first available block after the starting point; returns 0 if
1969// there are no available blocks left
1970uint64_t GPTData::FindFirstAvailable(uint64_t start) {
1971 uint64_t first;
1972 uint32_t i;
1973 int firstMoved = 0;
1974
1975 // Begin from the specified starting point or from the first usable
1976 // LBA, whichever is greater...
1977 if (start < mainHeader.firstUsableLBA)
1978 first = mainHeader.firstUsableLBA;
1979 else
1980 first = start;
1981
1982 // ...now search through all partitions; if first is within an
1983 // existing partition, move it to the next sector after that
1984 // partition and repeat. If first was moved, set firstMoved
1985 // flag; repeat until firstMoved is not set, so as to catch
1986 // cases where partitions are out of sequential order....
1987 do {
1988 firstMoved = 0;
srs56940283dae2010-04-28 16:44:34 -04001989 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04001990 if ((first >= partitions[i].GetFirstLBA()) &&
srs569455d92612010-03-07 22:16:07 -05001991 (first <= partitions[i].GetLastLBA())) { // in existing part.
srs5694e4ac11e2009-08-31 10:13:04 -04001992 first = partitions[i].GetLastLBA() + 1;
1993 firstMoved = 1;
srs569455d92612010-03-07 22:16:07 -05001994 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04001995 } // for
1996 } while (firstMoved == 1);
1997 if (first > mainHeader.lastUsableLBA)
1998 first = 0;
1999 return (first);
2000} // GPTData::FindFirstAvailable()
2001
2002// Finds the first available sector in the largest block of unallocated
2003// space on the disk. Returns 0 if there are no available blocks left
2004uint64_t GPTData::FindFirstInLargest(void) {
srs5694e35eb1b2009-09-14 00:29:34 -04002005 uint64_t start, firstBlock, lastBlock, segmentSize, selectedSize = 0, selectedSegment = 0;
srs5694e4ac11e2009-08-31 10:13:04 -04002006
2007 start = 0;
2008 do {
2009 firstBlock = FindFirstAvailable(start);
2010 if (firstBlock != UINT32_C(0)) { // something's free...
2011 lastBlock = FindLastInFree(firstBlock);
2012 segmentSize = lastBlock - firstBlock + UINT32_C(1);
2013 if (segmentSize > selectedSize) {
2014 selectedSize = segmentSize;
2015 selectedSegment = firstBlock;
2016 } // if
2017 start = lastBlock + 1;
2018 } // if
2019 } while (firstBlock != 0);
2020 return selectedSegment;
2021} // GPTData::FindFirstInLargest()
2022
srs5694cb76c672010-02-11 22:22:22 -05002023// Find the last available block on the disk.
2024// Returns 0 if there are no available partitions
2025uint64_t GPTData::FindLastAvailable(void) {
srs5694e4ac11e2009-08-31 10:13:04 -04002026 uint64_t last;
2027 uint32_t i;
2028 int lastMoved = 0;
2029
2030 // Start by assuming the last usable LBA is available....
2031 last = mainHeader.lastUsableLBA;
2032
2033 // ...now, similar to algorithm in FindFirstAvailable(), search
2034 // through all partitions, moving last when it's in an existing
2035 // partition. Set the lastMoved flag so we repeat to catch cases
2036 // where partitions are out of logical order.
2037 do {
2038 lastMoved = 0;
srs56940283dae2010-04-28 16:44:34 -04002039 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04002040 if ((last >= partitions[i].GetFirstLBA()) &&
srs569455d92612010-03-07 22:16:07 -05002041 (last <= partitions[i].GetLastLBA())) { // in existing part.
srs5694e4ac11e2009-08-31 10:13:04 -04002042 last = partitions[i].GetFirstLBA() - 1;
2043 lastMoved = 1;
srs569455d92612010-03-07 22:16:07 -05002044 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002045 } // for
2046 } while (lastMoved == 1);
2047 if (last < mainHeader.firstUsableLBA)
2048 last = 0;
2049 return (last);
2050} // GPTData::FindLastAvailable()
2051
2052// Find the last available block in the free space pointed to by start.
2053uint64_t GPTData::FindLastInFree(uint64_t start) {
2054 uint64_t nearestStart;
2055 uint32_t i;
2056
2057 nearestStart = mainHeader.lastUsableLBA;
srs56940283dae2010-04-28 16:44:34 -04002058 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04002059 if ((nearestStart > partitions[i].GetFirstLBA()) &&
srs569455d92612010-03-07 22:16:07 -05002060 (partitions[i].GetFirstLBA() > start)) {
srs5694e4ac11e2009-08-31 10:13:04 -04002061 nearestStart = partitions[i].GetFirstLBA() - 1;
srs569455d92612010-03-07 22:16:07 -05002062 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002063 } // for
2064 return (nearestStart);
2065} // GPTData::FindLastInFree()
2066
2067// Finds the total number of free blocks, the number of segments in which
2068// they reside, and the size of the largest of those segments
srs5694e321d442010-01-29 17:44:04 -05002069uint64_t GPTData::FindFreeBlocks(uint32_t *numSegments, uint64_t *largestSegment) {
srs5694e4ac11e2009-08-31 10:13:04 -04002070 uint64_t start = UINT64_C(0); // starting point for each search
2071 uint64_t totalFound = UINT64_C(0); // running total
2072 uint64_t firstBlock; // first block in a segment
2073 uint64_t lastBlock; // last block in a segment
2074 uint64_t segmentSize; // size of segment in blocks
srs5694e321d442010-01-29 17:44:04 -05002075 uint32_t num = 0;
srs5694e4ac11e2009-08-31 10:13:04 -04002076
2077 *largestSegment = UINT64_C(0);
srs5694c54e9b42010-05-01 21:04:23 -04002078 if (diskSize > 0) {
2079 do {
2080 firstBlock = FindFirstAvailable(start);
2081 if (firstBlock != UINT64_C(0)) { // something's free...
2082 lastBlock = FindLastInFree(firstBlock);
2083 segmentSize = lastBlock - firstBlock + UINT64_C(1);
2084 if (segmentSize > *largestSegment) {
2085 *largestSegment = segmentSize;
2086 } // if
2087 totalFound += segmentSize;
2088 num++;
2089 start = lastBlock + 1;
srs5694e4ac11e2009-08-31 10:13:04 -04002090 } // if
srs5694c54e9b42010-05-01 21:04:23 -04002091 } while (firstBlock != 0);
2092 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002093 *numSegments = num;
2094 return totalFound;
2095} // GPTData::FindFreeBlocks()
2096
srs569455d92612010-03-07 22:16:07 -05002097// Returns 1 if sector is unallocated, 0 if it's allocated to a partition.
2098// If it's allocated, return the partition number to which it's allocated
2099// in partNum, if that variable is non-NULL. (A value of UINT32_MAX is
2100// returned in partNum if the sector is in use by basic GPT data structures.)
2101int GPTData::IsFree(uint64_t sector, uint32_t *partNum) {
srs5694e4ac11e2009-08-31 10:13:04 -04002102 int isFree = 1;
2103 uint32_t i;
2104
srs56940283dae2010-04-28 16:44:34 -04002105 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04002106 if ((sector >= partitions[i].GetFirstLBA()) &&
2107 (sector <= partitions[i].GetLastLBA())) {
2108 isFree = 0;
srs569455d92612010-03-07 22:16:07 -05002109 if (partNum != NULL)
2110 *partNum = i;
srs569408bb0da2010-02-19 17:19:55 -05002111 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002112 } // for
srs5694e35eb1b2009-09-14 00:29:34 -04002113 if ((sector < mainHeader.firstUsableLBA) ||
srs5694e4ac11e2009-08-31 10:13:04 -04002114 (sector > mainHeader.lastUsableLBA)) {
2115 isFree = 0;
srs569455d92612010-03-07 22:16:07 -05002116 if (partNum != NULL)
2117 *partNum = UINT32_MAX;
srs569408bb0da2010-02-19 17:19:55 -05002118 } // if
2119 return (isFree);
srs5694e4ac11e2009-08-31 10:13:04 -04002120} // GPTData::IsFree()
2121
srs5694815fb652011-03-18 12:35:56 -04002122// Returns 1 if partNum is unused AND if it's a legal value.
srs5694ba00fed2010-01-12 18:18:36 -05002123int GPTData::IsFreePartNum(uint32_t partNum) {
srs569401f7f082011-03-15 23:53:31 -04002124 return ((partNum < numParts) && (partitions != NULL) &&
2125 (!partitions[partNum].IsUsed()));
srs5694ba00fed2010-01-12 18:18:36 -05002126} // GPTData::IsFreePartNum()
2127
srs5694815fb652011-03-18 12:35:56 -04002128// Returns 1 if partNum is in use.
2129int GPTData::IsUsedPartNum(uint32_t partNum) {
2130 return ((partNum < numParts) && (partitions != NULL) &&
2131 (partitions[partNum].IsUsed()));
2132} // GPTData::IsUsedPartNum()
srs5694a8582cf2010-03-19 14:21:59 -04002133
2134/***********************************************************
2135 * *
2136 * Change how functions work or return information on them *
2137 * *
2138 ***********************************************************/
2139
2140// Set partition alignment value; partitions will begin on multiples of
2141// the specified value
2142void GPTData::SetAlignment(uint32_t n) {
srs56940873e9d2010-10-07 13:00:45 -04002143 if (n > 0)
2144 sectorAlignment = n;
2145 else
2146 cerr << "Attempt to set partition alignment to 0!\n";
srs5694a8582cf2010-03-19 14:21:59 -04002147} // GPTData::SetAlignment()
2148
2149// Compute sector alignment based on the current partitions (if any). Each
2150// partition's starting LBA is examined, and if it's divisible by a power-of-2
srs56940873e9d2010-10-07 13:00:45 -04002151// value less than or equal to the DEFAULT_ALIGNMENT value (adjusted for the
2152// sector size), but not by the previously-located alignment value, then the
2153// alignment value is adjusted down. If the computed alignment is less than 8
2154// and the disk is bigger than SMALLEST_ADVANCED_FORMAT, resets it to 8. This
2155// is a safety measure for WD Advanced Format and similar drives. If no partitions
2156// are defined, the alignment value is set to DEFAULT_ALIGNMENT (2048) (or an
2157// adjustment of that based on the current sector size). The result is that new
srs56948a4ddfc2010-03-21 19:05:49 -04002158// drives are aligned to 2048-sector multiples but the program won't complain
2159// about other alignments on existing disks unless a smaller-than-8 alignment
srs56940873e9d2010-10-07 13:00:45 -04002160// is used on big disks (as safety for WD Advanced Format drives).
srs5694a8582cf2010-03-19 14:21:59 -04002161// Returns the computed alignment value.
2162uint32_t GPTData::ComputeAlignment(void) {
2163 uint32_t i = 0, found, exponent = 31;
srs5694ab4b0432010-09-25 20:39:52 -04002164 uint32_t align = DEFAULT_ALIGNMENT;
srs5694a8582cf2010-03-19 14:21:59 -04002165
srs56940873e9d2010-10-07 13:00:45 -04002166 if (blockSize > 0)
2167 align = DEFAULT_ALIGNMENT * SECTOR_SIZE / blockSize;
2168 exponent = (uint32_t) log2(align);
srs56940283dae2010-04-28 16:44:34 -04002169 for (i = 0; i < numParts; i++) {
srs5694a8582cf2010-03-19 14:21:59 -04002170 if (partitions[i].IsUsed()) {
2171 found = 0;
2172 while (!found) {
srs56940873e9d2010-10-07 13:00:45 -04002173 align = UINT64_C(1) << exponent;
srs5694a8582cf2010-03-19 14:21:59 -04002174 if ((partitions[i].GetFirstLBA() % align) == 0) {
2175 found = 1;
2176 } else {
2177 exponent--;
2178 } // if/else
2179 } // while
2180 } // if
2181 } // for
srs56940873e9d2010-10-07 13:00:45 -04002182 if ((align < MIN_AF_ALIGNMENT) && (diskSize >= SMALLEST_ADVANCED_FORMAT))
2183 align = MIN_AF_ALIGNMENT;
2184 sectorAlignment = align;
srs5694a8582cf2010-03-19 14:21:59 -04002185 return align;
2186} // GPTData::ComputeAlignment()
2187
srs5694e4ac11e2009-08-31 10:13:04 -04002188/********************************
2189 * *
2190 * Endianness support functions *
2191 * *
2192 ********************************/
2193
srs56942a9f5da2009-08-26 00:48:01 -04002194void GPTData::ReverseHeaderBytes(struct GPTHeader* header) {
srs5694221e0872009-08-29 15:00:31 -04002195 ReverseBytes(&header->signature, 8);
2196 ReverseBytes(&header->revision, 4);
2197 ReverseBytes(&header->headerSize, 4);
2198 ReverseBytes(&header->headerCRC, 4);
2199 ReverseBytes(&header->reserved, 4);
2200 ReverseBytes(&header->currentLBA, 8);
2201 ReverseBytes(&header->backupLBA, 8);
2202 ReverseBytes(&header->firstUsableLBA, 8);
2203 ReverseBytes(&header->lastUsableLBA, 8);
2204 ReverseBytes(&header->partitionEntriesLBA, 8);
2205 ReverseBytes(&header->numParts, 4);
2206 ReverseBytes(&header->sizeOfPartitionEntries, 4);
2207 ReverseBytes(&header->partitionEntriesCRC, 4);
srs569408bb0da2010-02-19 17:19:55 -05002208 ReverseBytes(header->reserved2, GPT_RESERVED);
srs56942a9f5da2009-08-26 00:48:01 -04002209} // GPTData::ReverseHeaderBytes()
2210
srs56940283dae2010-04-28 16:44:34 -04002211// Reverse byte order for all partitions.
srs56942a9f5da2009-08-26 00:48:01 -04002212void GPTData::ReversePartitionBytes() {
2213 uint32_t i;
2214
srs56940283dae2010-04-28 16:44:34 -04002215 for (i = 0; i < numParts; i++) {
srs5694221e0872009-08-29 15:00:31 -04002216 partitions[i].ReversePartBytes();
srs56942a9f5da2009-08-26 00:48:01 -04002217 } // for
2218} // GPTData::ReversePartitionBytes()
2219
srs56949ddc14b2010-08-22 22:44:42 -04002220// Validate partition number
2221bool GPTData::ValidPartNum (const uint32_t partNum) {
2222 if (partNum >= numParts) {
srs56945a081752010-09-24 20:39:41 -04002223 cerr << "Partition number out of range: " << partNum << "\n";
srs56949ddc14b2010-08-22 22:44:42 -04002224 return false;
2225 } // if
2226 return true;
2227} // GPTData::ValidPartNum
2228
srs56945a081752010-09-24 20:39:41 -04002229// Return a single partition for inspection (not modification!) by other
2230// functions.
2231const GPTPart & GPTData::operator[](uint32_t partNum) const {
2232 if (partNum >= numParts) {
srs5694815fb652011-03-18 12:35:56 -04002233 cerr << "Partition number out of range (" << partNum << " requested, but only "
2234 << numParts << " available)\n";
2235 exit(1);
2236 } // if
2237 if (partitions == NULL) {
2238 cerr << "No partitions defined in GPTData::operator[]; fatal error!\n";
2239 exit(1);
srs56945a081752010-09-24 20:39:41 -04002240 } // if
2241 return partitions[partNum];
2242} // operator[]
2243
2244// Return (not for modification!) the disk's GUID value
2245const GUIDData & GPTData::GetDiskGUID(void) const {
2246 return mainHeader.diskGUID;
2247} // GPTData::GetDiskGUID()
2248
srs56949ddc14b2010-08-22 22:44:42 -04002249// Manage attributes for a partition, based on commands passed to this function.
2250// (Function is non-interactive.)
2251// Returns 1 if a modification command succeeded, 0 if the command should not have
2252// modified data, and -1 if a modification command failed.
2253int GPTData::ManageAttributes(int partNum, const string & command, const string & bits) {
2254 int retval = 0;
2255 Attributes theAttr;
2256
2257 if (command == "show") {
2258 ShowAttributes(partNum);
2259 } else if (command == "get") {
2260 GetAttribute(partNum, bits);
2261 } else {
2262 theAttr = partitions[partNum].GetAttributes();
2263 if (theAttr.OperateOnAttributes(partNum, command, bits)) {
2264 partitions[partNum].SetAttributes(theAttr.GetAttributes());
2265 retval = 1;
2266 } else {
2267 retval = -1;
2268 } // if/else
2269 } // if/elseif/else
2270
2271 return retval;
2272} // GPTData::ManageAttributes()
2273
2274// Show all attributes for a specified partition....
2275void GPTData::ShowAttributes(const uint32_t partNum) {
srs56940873e9d2010-10-07 13:00:45 -04002276 partitions[partNum].ShowAttributes(partNum);
srs56949ddc14b2010-08-22 22:44:42 -04002277} // GPTData::ShowAttributes
2278
2279// Show whether a single attribute bit is set (terse output)...
2280void GPTData::GetAttribute(const uint32_t partNum, const string& attributeBits) {
srs56940873e9d2010-10-07 13:00:45 -04002281 partitions[partNum].GetAttributes().OperateOnAttributes(partNum, "get", attributeBits);
srs56949ddc14b2010-08-22 22:44:42 -04002282} // GPTData::GetAttribute
2283
2284
srs56942a9f5da2009-08-26 00:48:01 -04002285/******************************************
2286 * *
2287 * Additional non-class support functions *
2288 * *
2289 ******************************************/
2290
srs5694e7b4ff92009-08-18 13:16:10 -04002291// Check to be sure that data type sizes are correct. The basic types (uint*_t) should
2292// never fail these tests, but the struct types may fail depending on compile options.
2293// Specifically, the -fpack-struct option to gcc may be required to ensure proper structure
2294// sizes.
2295int SizesOK(void) {
2296 int allOK = 1;
srs5694e7b4ff92009-08-18 13:16:10 -04002297
2298 if (sizeof(uint8_t) != 1) {
srs5694fed16d02010-01-27 23:03:40 -05002299 cerr << "uint8_t is " << sizeof(uint8_t) << " bytes, should be 1 byte; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002300 allOK = 0;
2301 } // if
2302 if (sizeof(uint16_t) != 2) {
srs5694fed16d02010-01-27 23:03:40 -05002303 cerr << "uint16_t is " << sizeof(uint16_t) << " bytes, should be 2 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002304 allOK = 0;
2305 } // if
2306 if (sizeof(uint32_t) != 4) {
srs5694fed16d02010-01-27 23:03:40 -05002307 cerr << "uint32_t is " << sizeof(uint32_t) << " bytes, should be 4 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002308 allOK = 0;
2309 } // if
2310 if (sizeof(uint64_t) != 8) {
srs5694fed16d02010-01-27 23:03:40 -05002311 cerr << "uint64_t is " << sizeof(uint64_t) << " bytes, should be 8 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002312 allOK = 0;
2313 } // if
2314 if (sizeof(struct MBRRecord) != 16) {
srs5694fed16d02010-01-27 23:03:40 -05002315 cerr << "MBRRecord is " << sizeof(MBRRecord) << " bytes, should be 16 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002316 allOK = 0;
2317 } // if
srs5694978041c2009-09-21 20:51:47 -04002318 if (sizeof(struct TempMBR) != 512) {
srs5694fed16d02010-01-27 23:03:40 -05002319 cerr << "TempMBR is " << sizeof(TempMBR) << " bytes, should be 512 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002320 allOK = 0;
2321 } // if
2322 if (sizeof(struct GPTHeader) != 512) {
srs5694fed16d02010-01-27 23:03:40 -05002323 cerr << "GPTHeader is " << sizeof(GPTHeader) << " bytes, should be 512 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002324 allOK = 0;
2325 } // if
srs5694221e0872009-08-29 15:00:31 -04002326 if (sizeof(GPTPart) != 128) {
srs5694fed16d02010-01-27 23:03:40 -05002327 cerr << "GPTPart is " << sizeof(GPTPart) << " bytes, should be 128 bytes; aborting!\n";
srs5694221e0872009-08-29 15:00:31 -04002328 allOK = 0;
2329 } // if
srs56946699b012010-02-04 00:55:30 -05002330 if (sizeof(GUIDData) != 16) {
2331 cerr << "GUIDData is " << sizeof(GUIDData) << " bytes, should be 16 bytes; aborting!\n";
2332 allOK = 0;
2333 } // if
2334 if (sizeof(PartType) != 16) {
2335 cerr << "PartType is " << sizeof(GUIDData) << " bytes, should be 16 bytes; aborting!\n";
2336 allOK = 0;
2337 } // if
srs5694e7b4ff92009-08-18 13:16:10 -04002338 return (allOK);
2339} // SizesOK()
srs5694e4ac11e2009-08-31 10:13:04 -04002340