blob: fdffb25175f388fa8d38e7d3b8a80af18e3a5820 [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
Roderick W. Smithe3ee7332013-09-24 12:56:11 -04006/* This program is copyright (c) 2009-2013 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"
srs5694e7b4ff92009-08-18 13:16:10 -040030
31using namespace std;
32
srs56948f1b2d62010-05-23 13:07:19 -040033#ifdef __FreeBSD__
srs56949ba54212010-05-18 23:24:02 -040034#define log2(x) (log(x) / M_LN2)
35#endif // __FreeBSD__
36
srs56948f1b2d62010-05-23 13:07:19 -040037#ifdef _MSC_VER
38#define log2(x) (log((double) x) / log(2.0))
39#endif // Microsoft Visual C++
srs56949ba54212010-05-18 23:24:02 -040040
srs5694e7b4ff92009-08-18 13:16:10 -040041/****************************************
42 * *
43 * GPTData class and related structures *
44 * *
45 ****************************************/
46
srs5694e4ac11e2009-08-31 10:13:04 -040047// Default constructor
srs5694e7b4ff92009-08-18 13:16:10 -040048GPTData::GPTData(void) {
49 blockSize = SECTOR_SIZE; // set a default
50 diskSize = 0;
51 partitions = NULL;
52 state = gpt_valid;
srs5694fed16d02010-01-27 23:03:40 -050053 device = "";
srs56945d58fe02010-01-03 20:57:08 -050054 justLooking = 0;
srs5694e7b4ff92009-08-18 13:16:10 -040055 mainCrcOk = 0;
56 secondCrcOk = 0;
57 mainPartsCrcOk = 0;
58 secondPartsCrcOk = 0;
srs5694221e0872009-08-29 15:00:31 -040059 apmFound = 0;
60 bsdFound = 0;
srs56940873e9d2010-10-07 13:00:45 -040061 sectorAlignment = MIN_AF_ALIGNMENT; // Align partitions on 4096-byte boundaries by default
srs5694ba00fed2010-01-12 18:18:36 -050062 beQuiet = 0;
63 whichWasUsed = use_new;
srs56941e093722010-01-05 00:14:19 -050064 mainHeader.numParts = 0;
srs56940283dae2010-04-28 16:44:34 -040065 numParts = 0;
srs5694e7b4ff92009-08-18 13:16:10 -040066 SetGPTSize(NUM_GPT_ENTRIES);
srs5694d1b11e82011-09-18 21:12:28 -040067 // Initialize CRC functions...
68 chksum_crc32gentab();
srs5694e7b4ff92009-08-18 13:16:10 -040069} // GPTData default constructor
70
71// The following constructor loads GPT data from a device file
srs5694fed16d02010-01-27 23:03:40 -050072GPTData::GPTData(string filename) {
srs5694e7b4ff92009-08-18 13:16:10 -040073 blockSize = SECTOR_SIZE; // set a default
74 diskSize = 0;
75 partitions = NULL;
76 state = gpt_invalid;
srs5694fed16d02010-01-27 23:03:40 -050077 device = "";
srs56945d58fe02010-01-03 20:57:08 -050078 justLooking = 0;
srs5694e7b4ff92009-08-18 13:16:10 -040079 mainCrcOk = 0;
80 secondCrcOk = 0;
81 mainPartsCrcOk = 0;
82 secondPartsCrcOk = 0;
srs5694221e0872009-08-29 15:00:31 -040083 apmFound = 0;
84 bsdFound = 0;
srs56940873e9d2010-10-07 13:00:45 -040085 sectorAlignment = MIN_AF_ALIGNMENT; // Align partitions on 4096-byte boundaries by default
srs5694ba00fed2010-01-12 18:18:36 -050086 beQuiet = 0;
87 whichWasUsed = use_new;
srs56941e093722010-01-05 00:14:19 -050088 mainHeader.numParts = 0;
srs56940283dae2010-04-28 16:44:34 -040089 numParts = 0;
srs5694d1b11e82011-09-18 21:12:28 -040090 // Initialize CRC functions...
91 chksum_crc32gentab();
srs56943c0af382010-01-15 19:19:18 -050092 if (!LoadPartitions(filename))
93 exit(2);
srs5694fed16d02010-01-27 23:03:40 -050094} // GPTData(string filename) constructor
srs5694e7b4ff92009-08-18 13:16:10 -040095
srs5694e4ac11e2009-08-31 10:13:04 -040096// Destructor
srs5694e7b4ff92009-08-18 13:16:10 -040097GPTData::~GPTData(void) {
srs5694cb76c672010-02-11 22:22:22 -050098 delete[] partitions;
srs5694e7b4ff92009-08-18 13:16:10 -040099} // GPTData destructor
100
srs569464cbd172011-03-01 22:03:54 -0500101// Assignment operator
102GPTData & GPTData::operator=(const GPTData & orig) {
103 uint32_t i;
104
105 mainHeader = orig.mainHeader;
106 numParts = orig.numParts;
107 secondHeader = orig.secondHeader;
108 protectiveMBR = orig.protectiveMBR;
109 device = orig.device;
110 blockSize = orig.blockSize;
111 diskSize = orig.diskSize;
112 state = orig.state;
113 justLooking = orig.justLooking;
114 mainCrcOk = orig.mainCrcOk;
115 secondCrcOk = orig.secondCrcOk;
116 mainPartsCrcOk = orig.mainPartsCrcOk;
117 secondPartsCrcOk = orig.secondPartsCrcOk;
118 apmFound = orig.apmFound;
119 bsdFound = orig.bsdFound;
120 sectorAlignment = orig.sectorAlignment;
121 beQuiet = orig.beQuiet;
122 whichWasUsed = orig.whichWasUsed;
123
124 myDisk.OpenForRead(orig.myDisk.GetName());
125
126 delete[] partitions;
srs569401f7f082011-03-15 23:53:31 -0400127 partitions = new GPTPart [numParts];
srs56946aae2a92011-06-10 01:16:51 -0400128 if (partitions == NULL) {
srs569464cbd172011-03-01 22:03:54 -0500129 cerr << "Error! Could not allocate memory for partitions in GPTData::operator=()!\n"
srs56946aae2a92011-06-10 01:16:51 -0400130 << "Terminating!\n";
131 exit(1);
132 } // if
133 for (i = 0; i < numParts; i++) {
134 partitions[i] = orig.partitions[i];
srs5694d1b11e82011-09-18 21:12:28 -0400135 } // for
136
srs569464cbd172011-03-01 22:03:54 -0500137 return *this;
138} // GPTData::operator=()
139
srs5694e4ac11e2009-08-31 10:13:04 -0400140/*********************************************************************
141 * *
142 * Begin functions that verify data, or that adjust the verification *
143 * information (compute CRCs, rebuild headers) *
144 * *
145 *********************************************************************/
srs5694e7b4ff92009-08-18 13:16:10 -0400146
srs5694e4ac11e2009-08-31 10:13:04 -0400147// Perform detailed verification, reporting on any problems found, but
148// do *NOT* recover from these problems. Returns the total number of
149// problems identified.
150int GPTData::Verify(void) {
srs569464cbd172011-03-01 22:03:54 -0500151 int problems = 0, alignProbs = 0;
srs5694e321d442010-01-29 17:44:04 -0500152 uint32_t i, numSegments;
153 uint64_t totalFree, largestSegment;
srs5694e4ac11e2009-08-31 10:13:04 -0400154
155 // First, check for CRC errors in the GPT data....
156 if (!mainCrcOk) {
157 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500158 cout << "\nProblem: The CRC for the main GPT header is invalid. The main GPT header may\n"
159 << "be corrupt. Consider loading the backup GPT header to rebuild the main GPT\n"
160 << "header ('b' on the recovery & transformation menu). This report may be a false\n"
161 << "alarm if you've already corrected other problems.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400162 } // if
163 if (!mainPartsCrcOk) {
164 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500165 cout << "\nProblem: The CRC for the main partition table is invalid. This table may be\n"
166 << "corrupt. Consider loading the backup partition table ('c' on the recovery &\n"
167 << "transformation menu). This report may be a false alarm if you've already\n"
168 << "corrected other problems.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400169 } // if
170 if (!secondCrcOk) {
171 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500172 cout << "\nProblem: The CRC for the backup GPT header is invalid. The backup GPT header\n"
173 << "may be corrupt. Consider using the main GPT header to rebuild the backup GPT\n"
174 << "header ('d' on the recovery & transformation menu). This report may be a false\n"
175 << "alarm if you've already corrected other problems.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400176 } // if
177 if (!secondPartsCrcOk) {
178 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500179 cout << "\nCaution: The CRC for the backup partition table is invalid. This table may\n"
180 << "be corrupt. This program will automatically create a new backup partition\n"
181 << "table when you save your partitions.\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400182 } // if
183
srs5694978041c2009-09-21 20:51:47 -0400184 // Now check that the main and backup headers both point to themselves....
185 if (mainHeader.currentLBA != 1) {
186 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500187 cout << "\nProblem: The main header's self-pointer doesn't point to itself. This problem\n"
188 << "is being automatically corrected, but it may be a symptom of more serious\n"
189 << "problems. Think carefully before saving changes with 'w' or using this disk.\n";
srs5694978041c2009-09-21 20:51:47 -0400190 mainHeader.currentLBA = 1;
191 } // if
192 if (secondHeader.currentLBA != (diskSize - UINT64_C(1))) {
193 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500194 cout << "\nProblem: The secondary header's self-pointer indicates that it doesn't reside\n"
195 << "at the end of the disk. If you've added a disk to a RAID array, use the 'e'\n"
196 << "option on the experts' menu to adjust the secondary header's and partition\n"
197 << "table's locations.\n";
srs5694978041c2009-09-21 20:51:47 -0400198 } // if
199
200 // Now check that critical main and backup GPT entries match each other
srs5694e4ac11e2009-08-31 10:13:04 -0400201 if (mainHeader.currentLBA != secondHeader.backupLBA) {
202 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500203 cout << "\nProblem: main GPT header's current LBA pointer (" << mainHeader.currentLBA
204 << ") doesn't\nmatch the backup GPT header's alternate LBA pointer("
205 << secondHeader.backupLBA << ").\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400206 } // if
207 if (mainHeader.backupLBA != secondHeader.currentLBA) {
208 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500209 cout << "\nProblem: main GPT header's backup LBA pointer (" << mainHeader.backupLBA
210 << ") doesn't\nmatch the backup GPT header's current LBA pointer ("
211 << secondHeader.currentLBA << ").\n"
212 << "The 'e' option on the experts' menu may fix this problem.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400213 } // if
214 if (mainHeader.firstUsableLBA != secondHeader.firstUsableLBA) {
215 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500216 cout << "\nProblem: main GPT header's first usable LBA pointer (" << mainHeader.firstUsableLBA
217 << ") doesn't\nmatch the backup GPT header's first usable LBA pointer ("
218 << secondHeader.firstUsableLBA << ")\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400219 } // if
220 if (mainHeader.lastUsableLBA != secondHeader.lastUsableLBA) {
221 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500222 cout << "\nProblem: main GPT header's last usable LBA pointer (" << mainHeader.lastUsableLBA
223 << ") doesn't\nmatch the backup GPT header's last usable LBA pointer ("
224 << secondHeader.lastUsableLBA << ")\n"
225 << "The 'e' option on the experts' menu can probably fix this problem.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400226 } // if
srs56946699b012010-02-04 00:55:30 -0500227 if ((mainHeader.diskGUID != secondHeader.diskGUID)) {
srs5694e4ac11e2009-08-31 10:13:04 -0400228 problems++;
srs56945a081752010-09-24 20:39:41 -0400229 cout << "\nProblem: main header's disk GUID (" << mainHeader.diskGUID
srs5694fed16d02010-01-27 23:03:40 -0500230 << ") doesn't\nmatch the backup GPT header's disk GUID ("
srs56945a081752010-09-24 20:39:41 -0400231 << secondHeader.diskGUID << ")\n"
srs5694fed16d02010-01-27 23:03:40 -0500232 << "You should use the 'b' or 'd' option on the recovery & transformation menu to\n"
233 << "select one or the other header.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400234 } // if
235 if (mainHeader.numParts != secondHeader.numParts) {
236 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500237 cout << "\nProblem: main GPT header's number of partitions (" << mainHeader.numParts
238 << ") doesn't\nmatch the backup GPT header's number of partitions ("
239 << secondHeader.numParts << ")\n"
240 << "Resizing the partition table ('s' on the experts' menu) may help.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400241 } // if
242 if (mainHeader.sizeOfPartitionEntries != secondHeader.sizeOfPartitionEntries) {
243 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500244 cout << "\nProblem: main GPT header's size of partition entries ("
245 << mainHeader.sizeOfPartitionEntries << ") doesn't\n"
246 << "match the backup GPT header's size of partition entries ("
247 << secondHeader.sizeOfPartitionEntries << ")\n"
248 << "You should use the 'b' or 'd' option on the recovery & transformation menu to\n"
249 << "select one or the other header.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400250 } // if
251
252 // Now check for a few other miscellaneous problems...
253 // Check that the disk size will hold the data...
srs569464cbd172011-03-01 22:03:54 -0500254 if (mainHeader.backupLBA >= diskSize) {
srs5694e4ac11e2009-08-31 10:13:04 -0400255 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500256 cout << "\nProblem: Disk is too small to hold all the data!\n"
257 << "(Disk size is " << diskSize << " sectors, needs to be "
srs569464cbd172011-03-01 22:03:54 -0500258 << mainHeader.backupLBA + UINT64_C(1) << " sectors.)\n"
srs5694fed16d02010-01-27 23:03:40 -0500259 << "The 'e' option on the experts' menu may fix this problem.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400260 } // if
261
srs5694d8eed462012-12-15 01:55:21 -0500262 if ((mainHeader.lastUsableLBA >= diskSize) || (mainHeader.lastUsableLBA > mainHeader.backupLBA)) {
263 problems++;
srs56940741fa22013-01-09 12:55:40 -0500264 cout << "\nProblem: GPT claims the disk is larger than it is! (Claimed last usable\n"
265 << "sector is " << mainHeader.lastUsableLBA << ", but backup header is at\n"
266 << mainHeader.backupLBA << " and disk size is " << diskSize << " sectors.\n"
267 << "The 'e' option on the experts' menu will probably fix this problem\n";
srs5694d8eed462012-12-15 01:55:21 -0500268 }
269
srs5694e4ac11e2009-08-31 10:13:04 -0400270 // Check for overlapping partitions....
271 problems += FindOverlaps();
272
srs569455d92612010-03-07 22:16:07 -0500273 // Check for insane partitions (start after end, hugely big, etc.)
274 problems += FindInsanePartitions();
275
srs5694e4ac11e2009-08-31 10:13:04 -0400276 // Check for mismatched MBR and GPT partitions...
277 problems += FindHybridMismatches();
278
srs5694327129e2010-09-22 01:07:31 -0400279 // Check for MBR-specific problems....
280 problems += VerifyMBR();
281
Roderick W. Smith042f38a2013-08-31 17:40:15 -0400282 // Check for a 0xEE protective partition that's marked as active....
283 if (protectiveMBR.IsEEActive()) {
284 cout << "\nWarning: The 0xEE protective partition in the MBR is marked as active. This is\n"
285 << "technically a violation of the GPT specification, and can cause some EFIs to\n"
286 << "ignore the disk, but it is required to boot from a GPT disk on some BIOS-based\n"
287 << "computers. You can clear this flag by creating a fresh protective MBR using\n"
288 << "the 'n' option on the experts' menu.\n";
289 }
290
srs5694e4ac11e2009-08-31 10:13:04 -0400291 // Verify that partitions don't run into GPT data areas....
292 problems += CheckGPTSize();
293
Roderick W. Smith4a702a22014-01-25 23:46:42 -0500294 if (!protectiveMBR.DoTheyFit()) {
295 cout << "\nPartition(s) in the protective MBR are too big for the disk! Creating a\n"
296 << "fresh protective or hybrid MBR is recommended.\n";
297 problems++;
298 }
299
srs56941d1448a2009-12-31 21:20:19 -0500300 // Check that partitions are aligned on proper boundaries (for WD Advanced
301 // Format and similar disks)....
srs56940283dae2010-04-28 16:44:34 -0400302 for (i = 0; i < numParts; i++) {
srs5694e69e6802012-01-20 22:37:12 -0500303 if ((partitions[i].IsUsed()) && (partitions[i].GetFirstLBA() % sectorAlignment) != 0) {
srs5694fed16d02010-01-27 23:03:40 -0500304 cout << "\nCaution: Partition " << i + 1 << " doesn't begin on a "
305 << sectorAlignment << "-sector boundary. This may\nresult "
306 << "in degraded performance on some modern (2009 and later) hard disks.\n";
srs569464cbd172011-03-01 22:03:54 -0500307 alignProbs++;
srs56941d1448a2009-12-31 21:20:19 -0500308 } // if
309 } // for
srs569464cbd172011-03-01 22:03:54 -0500310 if (alignProbs > 0)
311 cout << "\nConsult http://www.ibm.com/developerworks/linux/library/l-4kb-sector-disks/\n"
312 << "for information on disk alignment.\n";
srs56941d1448a2009-12-31 21:20:19 -0500313
srs5694e4ac11e2009-08-31 10:13:04 -0400314 // Now compute available space, but only if no problems found, since
315 // problems could affect the results
316 if (problems == 0) {
317 totalFree = FindFreeBlocks(&numSegments, &largestSegment);
srs569464cbd172011-03-01 22:03:54 -0500318 cout << "\nNo problems found. " << totalFree << " free sectors ("
srs569401f7f082011-03-15 23:53:31 -0400319 << BytesToIeee(totalFree, blockSize) << ") available in "
srs5694fed16d02010-01-27 23:03:40 -0500320 << numSegments << "\nsegments, the largest of which is "
srs569401f7f082011-03-15 23:53:31 -0400321 << largestSegment << " (" << BytesToIeee(largestSegment, blockSize)
srs56940283dae2010-04-28 16:44:34 -0400322 << ") in size.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400323 } else {
srs56940a697312010-01-28 21:10:52 -0500324 cout << "\nIdentified " << problems << " problems!\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400325 } // if/else
srs5694e4ac11e2009-08-31 10:13:04 -0400326
327 return (problems);
328} // GPTData::Verify()
srs5694e7b4ff92009-08-18 13:16:10 -0400329
330// Checks to see if the GPT tables overrun existing partitions; if they
srs5694221e0872009-08-29 15:00:31 -0400331// do, issues a warning but takes no action. Returns number of problems
332// detected (0 if OK, 1 to 2 if problems).
srs5694e7b4ff92009-08-18 13:16:10 -0400333int GPTData::CheckGPTSize(void) {
334 uint64_t overlap, firstUsedBlock, lastUsedBlock;
335 uint32_t i;
srs5694221e0872009-08-29 15:00:31 -0400336 int numProbs = 0;
srs5694e7b4ff92009-08-18 13:16:10 -0400337
338 // first, locate the first & last used blocks
339 firstUsedBlock = UINT64_MAX;
340 lastUsedBlock = 0;
srs56940283dae2010-04-28 16:44:34 -0400341 for (i = 0; i < numParts; i++) {
srs5694e69e6802012-01-20 22:37:12 -0500342 if (partitions[i].IsUsed()) {
srs5694706e5122012-01-21 13:47:24 -0500343 if (partitions[i].GetFirstLBA() < firstUsedBlock)
srs5694e69e6802012-01-20 22:37:12 -0500344 firstUsedBlock = partitions[i].GetFirstLBA();
345 if (partitions[i].GetLastLBA() > lastUsedBlock) {
346 lastUsedBlock = partitions[i].GetLastLBA();
347 } // if
348 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400349 } // for
350
351 // If the disk size is 0 (the default), then it means that various
352 // variables aren't yet set, so the below tests will be useless;
353 // therefore we should skip everything
354 if (diskSize != 0) {
355 if (mainHeader.firstUsableLBA > firstUsedBlock) {
356 overlap = mainHeader.firstUsableLBA - firstUsedBlock;
srs5694fed16d02010-01-27 23:03:40 -0500357 cout << "Warning! Main partition table overlaps the first partition by "
358 << overlap << " blocks!\n";
srs5694221e0872009-08-29 15:00:31 -0400359 if (firstUsedBlock > 2) {
srs5694fed16d02010-01-27 23:03:40 -0500360 cout << "Try reducing the partition table size by " << overlap * 4
361 << " entries.\n(Use the 's' item on the experts' menu.)\n";
srs5694221e0872009-08-29 15:00:31 -0400362 } else {
srs5694fed16d02010-01-27 23:03:40 -0500363 cout << "You will need to delete this partition or resize it in another utility.\n";
srs5694221e0872009-08-29 15:00:31 -0400364 } // if/else
365 numProbs++;
srs5694e7b4ff92009-08-18 13:16:10 -0400366 } // Problem at start of disk
367 if (mainHeader.lastUsableLBA < lastUsedBlock) {
368 overlap = lastUsedBlock - mainHeader.lastUsableLBA;
srs569455d92612010-03-07 22:16:07 -0500369 cout << "\nWarning! Secondary partition table overlaps the last partition by\n"
srs5694fed16d02010-01-27 23:03:40 -0500370 << overlap << " blocks!\n";
srs5694221e0872009-08-29 15:00:31 -0400371 if (lastUsedBlock > (diskSize - 2)) {
srs5694fed16d02010-01-27 23:03:40 -0500372 cout << "You will need to delete this partition or resize it in another utility.\n";
srs5694221e0872009-08-29 15:00:31 -0400373 } else {
srs5694fed16d02010-01-27 23:03:40 -0500374 cout << "Try reducing the partition table size by " << overlap * 4
375 << " entries.\n(Use the 's' item on the experts' menu.)\n";
srs5694221e0872009-08-29 15:00:31 -0400376 } // if/else
377 numProbs++;
srs5694e7b4ff92009-08-18 13:16:10 -0400378 } // Problem at end of disk
379 } // if (diskSize != 0)
srs5694221e0872009-08-29 15:00:31 -0400380 return numProbs;
srs5694e7b4ff92009-08-18 13:16:10 -0400381} // GPTData::CheckGPTSize()
382
srs5694e7b4ff92009-08-18 13:16:10 -0400383// Check the validity of the GPT header. Returns 1 if the main header
384// is valid, 2 if the backup header is valid, 3 if both are valid, and
srs5694d1b11e82011-09-18 21:12:28 -0400385// 0 if neither is valid. Note that this function checks the GPT signature,
386// revision value, and CRCs in both headers.
srs5694e7b4ff92009-08-18 13:16:10 -0400387int GPTData::CheckHeaderValidity(void) {
388 int valid = 3;
389
srs5694fed16d02010-01-27 23:03:40 -0500390 cout.setf(ios::uppercase);
391 cout.fill('0');
392
393 // Note: failed GPT signature checks produce no error message because
394 // a message is displayed in the ReversePartitionBytes() function
srs5694d1b11e82011-09-18 21:12:28 -0400395 if ((mainHeader.signature != GPT_SIGNATURE) || (!CheckHeaderCRC(&mainHeader, 1))) {
srs5694e7b4ff92009-08-18 13:16:10 -0400396 valid -= 1;
srs5694e7b4ff92009-08-18 13:16:10 -0400397 } else if ((mainHeader.revision != 0x00010000) && valid) {
398 valid -= 1;
srs5694fed16d02010-01-27 23:03:40 -0500399 cout << "Unsupported GPT version in main header; read 0x";
400 cout.width(8);
401 cout << hex << mainHeader.revision << ", should be\n0x";
402 cout.width(8);
403 cout << UINT32_C(0x00010000) << dec << "\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400404 } // if/else/if
405
srs5694d1b11e82011-09-18 21:12:28 -0400406 if ((secondHeader.signature != GPT_SIGNATURE) || (!CheckHeaderCRC(&secondHeader))) {
srs5694e7b4ff92009-08-18 13:16:10 -0400407 valid -= 2;
srs5694e7b4ff92009-08-18 13:16:10 -0400408 } else if ((secondHeader.revision != 0x00010000) && valid) {
409 valid -= 2;
srs5694fed16d02010-01-27 23:03:40 -0500410 cout << "Unsupported GPT version in backup header; read 0x";
411 cout.width(8);
412 cout << hex << secondHeader.revision << ", should be\n0x";
413 cout.width(8);
414 cout << UINT32_C(0x00010000) << dec << "\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400415 } // if/else/if
416
srs5694df9d3632011-01-08 18:33:24 -0500417 // Check for an Apple disk signature
418 if (((mainHeader.signature << 32) == APM_SIGNATURE1) ||
419 (mainHeader.signature << 32) == APM_SIGNATURE2) {
srs5694221e0872009-08-29 15:00:31 -0400420 apmFound = 1; // Will display warning message later
srs56943f2fe992009-11-24 18:28:18 -0500421 } // if
srs5694fed16d02010-01-27 23:03:40 -0500422 cout.fill(' ');
srs56942a9f5da2009-08-26 00:48:01 -0400423
srs5694fed16d02010-01-27 23:03:40 -0500424 return valid;
srs5694e7b4ff92009-08-18 13:16:10 -0400425} // GPTData::CheckHeaderValidity()
426
427// Check the header CRC to see if it's OK...
srs5694d1b11e82011-09-18 21:12:28 -0400428// Note: Must be called with header in platform-ordered byte order.
429// Returns 1 if header's computed CRC matches the stored value, 0 if the
430// computed and stored values don't match
431int GPTData::CheckHeaderCRC(struct GPTHeader* header, int warn) {
srs5694978041c2009-09-21 20:51:47 -0400432 uint32_t oldCRC, newCRC, hSize;
srs5694d1b11e82011-09-18 21:12:28 -0400433 uint8_t *temp;
srs5694e7b4ff92009-08-18 13:16:10 -0400434
srs56942a9f5da2009-08-26 00:48:01 -0400435 // Back up old header CRC and then blank it, since it must be 0 for
srs5694e7b4ff92009-08-18 13:16:10 -0400436 // computation to be valid
437 oldCRC = header->headerCRC;
438 header->headerCRC = UINT32_C(0);
srs5694d1b11e82011-09-18 21:12:28 -0400439
srs5694978041c2009-09-21 20:51:47 -0400440 hSize = header->headerSize;
441
srs5694d1b11e82011-09-18 21:12:28 -0400442 if (IsLittleEndian() == 0)
443 ReverseHeaderBytes(header);
srs5694e7b4ff92009-08-18 13:16:10 -0400444
srs5694d1b11e82011-09-18 21:12:28 -0400445 if ((hSize > blockSize) || (hSize < HEADER_SIZE)) {
446 if (warn) {
447 cerr << "\aWarning! Header size is specified as " << hSize << ", which is invalid.\n";
448 cerr << "Setting the header size for CRC computation to " << HEADER_SIZE << "\n";
449 } // if
450 hSize = HEADER_SIZE;
451 } else if ((hSize > sizeof(GPTHeader)) && warn) {
452 cout << "\aCaution! Header size for CRC check is " << hSize << ", which is greater than " << sizeof(GPTHeader) << ".\n";
453 cout << "If stray data exists after the header on the header sector, it will be ignored,\n"
454 << "which may result in a CRC false alarm.\n";
455 } // if/elseif
456 temp = new uint8_t[hSize];
457 if (temp != NULL) {
458 memset(temp, 0, hSize);
459 if (hSize < sizeof(GPTHeader))
460 memcpy(temp, header, hSize);
461 else
462 memcpy(temp, header, sizeof(GPTHeader));
srs5694e7b4ff92009-08-18 13:16:10 -0400463
srs5694d1b11e82011-09-18 21:12:28 -0400464 newCRC = chksum_crc32((unsigned char*) temp, hSize);
465 delete[] temp;
466 } else {
467 cerr << "Could not allocate memory in GPTData::CheckHeaderCRC()! Aborting!\n";
468 exit(1);
469 }
470 if (IsLittleEndian() == 0)
471 ReverseHeaderBytes(header);
srs5694978041c2009-09-21 20:51:47 -0400472 header->headerCRC = oldCRC;
srs5694e7b4ff92009-08-18 13:16:10 -0400473 return (oldCRC == newCRC);
474} // GPTData::CheckHeaderCRC()
475
srs56946699b012010-02-04 00:55:30 -0500476// Recompute all the CRCs. Must be called before saving if any changes have
477// been made. Must be called on platform-ordered data (this function reverses
478// byte order and then undoes that reversal.)
srs5694e7b4ff92009-08-18 13:16:10 -0400479void GPTData::RecomputeCRCs(void) {
srs56940283dae2010-04-28 16:44:34 -0400480 uint32_t crc, hSize;
srs56942a9f5da2009-08-26 00:48:01 -0400481 int littleEndian = 1;
srs5694e7b4ff92009-08-18 13:16:10 -0400482
srs5694d1b11e82011-09-18 21:12:28 -0400483 // If the header size is bigger than the GPT header data structure, reset it;
484 // otherwise, set both header sizes to whatever the main one is....
485 if (mainHeader.headerSize > sizeof(GPTHeader))
486 hSize = secondHeader.headerSize = mainHeader.headerSize = HEADER_SIZE;
487 else
488 hSize = secondHeader.headerSize = mainHeader.headerSize;
srs56946699b012010-02-04 00:55:30 -0500489
490 if ((littleEndian = IsLittleEndian()) == 0) {
491 ReversePartitionBytes();
492 ReverseHeaderBytes(&mainHeader);
493 ReverseHeaderBytes(&secondHeader);
494 } // if
srs56942a9f5da2009-08-26 00:48:01 -0400495
srs5694e7b4ff92009-08-18 13:16:10 -0400496 // Compute CRC of partition tables & store in main and secondary headers
srs56940283dae2010-04-28 16:44:34 -0400497 crc = chksum_crc32((unsigned char*) partitions, numParts * GPT_SIZE);
srs5694e7b4ff92009-08-18 13:16:10 -0400498 mainHeader.partitionEntriesCRC = crc;
499 secondHeader.partitionEntriesCRC = crc;
srs56942a9f5da2009-08-26 00:48:01 -0400500 if (littleEndian == 0) {
srs5694221e0872009-08-29 15:00:31 -0400501 ReverseBytes(&mainHeader.partitionEntriesCRC, 4);
502 ReverseBytes(&secondHeader.partitionEntriesCRC, 4);
srs56942a9f5da2009-08-26 00:48:01 -0400503 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400504
srs5694d1b11e82011-09-18 21:12:28 -0400505 // Zero out GPT headers' own CRCs (required for correct computation)
srs5694e7b4ff92009-08-18 13:16:10 -0400506 mainHeader.headerCRC = 0;
507 secondHeader.headerCRC = 0;
508
srs5694978041c2009-09-21 20:51:47 -0400509 crc = chksum_crc32((unsigned char*) &mainHeader, hSize);
srs56942a9f5da2009-08-26 00:48:01 -0400510 if (littleEndian == 0)
srs5694221e0872009-08-29 15:00:31 -0400511 ReverseBytes(&crc, 4);
srs5694e7b4ff92009-08-18 13:16:10 -0400512 mainHeader.headerCRC = crc;
srs5694978041c2009-09-21 20:51:47 -0400513 crc = chksum_crc32((unsigned char*) &secondHeader, hSize);
srs56942a9f5da2009-08-26 00:48:01 -0400514 if (littleEndian == 0)
srs5694221e0872009-08-29 15:00:31 -0400515 ReverseBytes(&crc, 4);
srs5694e7b4ff92009-08-18 13:16:10 -0400516 secondHeader.headerCRC = crc;
srs56946699b012010-02-04 00:55:30 -0500517
srs5694d1b11e82011-09-18 21:12:28 -0400518 if (littleEndian == 0) {
srs56946699b012010-02-04 00:55:30 -0500519 ReverseHeaderBytes(&mainHeader);
520 ReverseHeaderBytes(&secondHeader);
521 ReversePartitionBytes();
522 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400523} // GPTData::RecomputeCRCs()
524
srs5694e7b4ff92009-08-18 13:16:10 -0400525// Rebuild the main GPT header, using the secondary header as a model.
526// Typically called when the main header has been found to be corrupt.
527void GPTData::RebuildMainHeader(void) {
srs5694e7b4ff92009-08-18 13:16:10 -0400528 mainHeader.signature = GPT_SIGNATURE;
529 mainHeader.revision = secondHeader.revision;
srs5694978041c2009-09-21 20:51:47 -0400530 mainHeader.headerSize = secondHeader.headerSize;
srs5694e7b4ff92009-08-18 13:16:10 -0400531 mainHeader.headerCRC = UINT32_C(0);
532 mainHeader.reserved = secondHeader.reserved;
533 mainHeader.currentLBA = secondHeader.backupLBA;
534 mainHeader.backupLBA = secondHeader.currentLBA;
535 mainHeader.firstUsableLBA = secondHeader.firstUsableLBA;
536 mainHeader.lastUsableLBA = secondHeader.lastUsableLBA;
srs56946699b012010-02-04 00:55:30 -0500537 mainHeader.diskGUID = secondHeader.diskGUID;
srs5694e7b4ff92009-08-18 13:16:10 -0400538 mainHeader.partitionEntriesLBA = UINT64_C(2);
539 mainHeader.numParts = secondHeader.numParts;
540 mainHeader.sizeOfPartitionEntries = secondHeader.sizeOfPartitionEntries;
541 mainHeader.partitionEntriesCRC = secondHeader.partitionEntriesCRC;
srs569401f7f082011-03-15 23:53:31 -0400542 memcpy(mainHeader.reserved2, secondHeader.reserved2, sizeof(mainHeader.reserved2));
srs5694546a9c72010-01-26 16:00:26 -0500543 mainCrcOk = secondCrcOk;
srs5694706e5122012-01-21 13:47:24 -0500544 SetGPTSize(mainHeader.numParts, 0);
srs5694e7b4ff92009-08-18 13:16:10 -0400545} // GPTData::RebuildMainHeader()
546
547// Rebuild the secondary GPT header, using the main header as a model.
548void GPTData::RebuildSecondHeader(void) {
srs5694e7b4ff92009-08-18 13:16:10 -0400549 secondHeader.signature = GPT_SIGNATURE;
550 secondHeader.revision = mainHeader.revision;
srs5694978041c2009-09-21 20:51:47 -0400551 secondHeader.headerSize = mainHeader.headerSize;
srs5694e7b4ff92009-08-18 13:16:10 -0400552 secondHeader.headerCRC = UINT32_C(0);
553 secondHeader.reserved = mainHeader.reserved;
554 secondHeader.currentLBA = mainHeader.backupLBA;
555 secondHeader.backupLBA = mainHeader.currentLBA;
556 secondHeader.firstUsableLBA = mainHeader.firstUsableLBA;
557 secondHeader.lastUsableLBA = mainHeader.lastUsableLBA;
srs56946699b012010-02-04 00:55:30 -0500558 secondHeader.diskGUID = mainHeader.diskGUID;
srs5694e7b4ff92009-08-18 13:16:10 -0400559 secondHeader.partitionEntriesLBA = secondHeader.lastUsableLBA + UINT64_C(1);
560 secondHeader.numParts = mainHeader.numParts;
561 secondHeader.sizeOfPartitionEntries = mainHeader.sizeOfPartitionEntries;
562 secondHeader.partitionEntriesCRC = mainHeader.partitionEntriesCRC;
srs569401f7f082011-03-15 23:53:31 -0400563 memcpy(secondHeader.reserved2, mainHeader.reserved2, sizeof(secondHeader.reserved2));
srs5694546a9c72010-01-26 16:00:26 -0500564 secondCrcOk = mainCrcOk;
srs5694706e5122012-01-21 13:47:24 -0500565 SetGPTSize(secondHeader.numParts, 0);
srs5694e4ac11e2009-08-31 10:13:04 -0400566} // GPTData::RebuildSecondHeader()
567
568// Search for hybrid MBR entries that have no corresponding GPT partition.
569// Returns number of such mismatches found
570int GPTData::FindHybridMismatches(void) {
srs5694e321d442010-01-29 17:44:04 -0500571 int i, found, numFound = 0;
572 uint32_t j;
srs5694e4ac11e2009-08-31 10:13:04 -0400573 uint64_t mbrFirst, mbrLast;
574
575 for (i = 0; i < 4; i++) {
576 if ((protectiveMBR.GetType(i) != 0xEE) && (protectiveMBR.GetType(i) != 0x00)) {
577 j = 0;
578 found = 0;
srs5694d1b11e82011-09-18 21:12:28 -0400579 mbrFirst = (uint64_t) protectiveMBR.GetFirstSector(i);
580 mbrLast = mbrFirst + (uint64_t) protectiveMBR.GetLength(i) - UINT64_C(1);
srs5694e4ac11e2009-08-31 10:13:04 -0400581 do {
Roderick W. Smith24bba6e2013-10-12 19:07:16 -0400582 if ((j < numParts) && (partitions[j].GetFirstLBA() == mbrFirst) &&
srs5694e69e6802012-01-20 22:37:12 -0500583 (partitions[j].GetLastLBA() == mbrLast) && (partitions[j].IsUsed()))
srs5694e4ac11e2009-08-31 10:13:04 -0400584 found = 1;
585 j++;
srs56940283dae2010-04-28 16:44:34 -0400586 } while ((!found) && (j < numParts));
srs5694e4ac11e2009-08-31 10:13:04 -0400587 if (!found) {
588 numFound++;
srs5694fed16d02010-01-27 23:03:40 -0500589 cout << "\nWarning! Mismatched GPT and MBR partition! MBR partition "
590 << i + 1 << ", of type 0x";
591 cout.fill('0');
592 cout.setf(ios::uppercase);
593 cout.width(2);
594 cout << hex << (int) protectiveMBR.GetType(i) << ",\n"
595 << "has no corresponding GPT partition! You may continue, but this condition\n"
596 << "might cause data loss in the future!\a\n" << dec;
597 cout.fill(' ');
srs5694e4ac11e2009-08-31 10:13:04 -0400598 } // if
599 } // if
600 } // for
601 return numFound;
602} // GPTData::FindHybridMismatches
603
604// Find overlapping partitions and warn user about them. Returns number of
605// overlapping partitions.
srs5694d1b11e82011-09-18 21:12:28 -0400606// Returns number of overlapping segments found.
srs5694e4ac11e2009-08-31 10:13:04 -0400607int GPTData::FindOverlaps(void) {
srs5694e321d442010-01-29 17:44:04 -0500608 int problems = 0;
609 uint32_t i, j;
srs5694e4ac11e2009-08-31 10:13:04 -0400610
srs56940283dae2010-04-28 16:44:34 -0400611 for (i = 1; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -0400612 for (j = 0; j < i; j++) {
srs5694e69e6802012-01-20 22:37:12 -0500613 if ((partitions[i].IsUsed()) && (partitions[j].IsUsed()) &&
614 (partitions[i].DoTheyOverlap(partitions[j]))) {
srs5694e4ac11e2009-08-31 10:13:04 -0400615 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500616 cout << "\nProblem: partitions " << i + 1 << " and " << j + 1 << " overlap:\n";
617 cout << " Partition " << i + 1 << ": " << partitions[i].GetFirstLBA()
618 << " to " << partitions[i].GetLastLBA() << "\n";
619 cout << " Partition " << j + 1 << ": " << partitions[j].GetFirstLBA()
620 << " to " << partitions[j].GetLastLBA() << "\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400621 } // if
622 } // for j...
623 } // for i...
624 return problems;
625} // GPTData::FindOverlaps()
626
srs569455d92612010-03-07 22:16:07 -0500627// Find partitions that are insane -- they start after they end or are too
628// big for the disk. (The latter should duplicate detection of overlaps
629// with GPT backup data structures, but better to err on the side of
630// redundant tests than to miss something....)
srs5694d1b11e82011-09-18 21:12:28 -0400631// Returns number of problems found.
srs569455d92612010-03-07 22:16:07 -0500632int GPTData::FindInsanePartitions(void) {
633 uint32_t i;
634 int problems = 0;
635
srs56940283dae2010-04-28 16:44:34 -0400636 for (i = 0; i < numParts; i++) {
srs5694e69e6802012-01-20 22:37:12 -0500637 if (partitions[i].IsUsed()) {
638 if (partitions[i].GetFirstLBA() > partitions[i].GetLastLBA()) {
639 problems++;
640 cout << "\nProblem: partition " << i + 1 << " ends before it begins.\n";
641 } // if
642 if (partitions[i].GetLastLBA() >= diskSize) {
643 problems++;
644 cout << "\nProblem: partition " << i + 1 << " is too big for the disk.\n";
645 } // if
srs569455d92612010-03-07 22:16:07 -0500646 } // if
647 } // for
648 return problems;
649} // GPTData::FindInsanePartitions(void)
650
651
srs5694e4ac11e2009-08-31 10:13:04 -0400652/******************************************************************
653 * *
654 * Begin functions that load data from disk or save data to disk. *
655 * *
656 ******************************************************************/
657
srs569464cbd172011-03-01 22:03:54 -0500658// Change the filename associated with the GPT. Used for duplicating
659// the partition table to a new disk and saving backups.
660// Returns 1 on success, 0 on failure.
srs5694bf8950c2011-03-12 01:23:12 -0500661int GPTData::SetDisk(const string & deviceFilename) {
srs569464cbd172011-03-01 22:03:54 -0500662 int err, allOK = 1;
663
664 device = deviceFilename;
665 if (allOK && myDisk.OpenForRead(deviceFilename)) {
666 // store disk information....
667 diskSize = myDisk.DiskSize(&err);
668 blockSize = (uint32_t) myDisk.GetBlockSize();
669 } // if
670 protectiveMBR.SetDisk(&myDisk);
671 protectiveMBR.SetDiskSize(diskSize);
672 protectiveMBR.SetBlockSize(blockSize);
673 return allOK;
srs5694bf8950c2011-03-12 01:23:12 -0500674} // GPTData::SetDisk()
srs569464cbd172011-03-01 22:03:54 -0500675
srs5694e4ac11e2009-08-31 10:13:04 -0400676// Scan for partition data. This function loads the MBR data (regular MBR or
677// protective MBR) and loads BSD disklabel data (which is probably invalid).
678// It also looks for APM data, forces a load of GPT data, and summarizes
679// the results.
srs5694546a9c72010-01-26 16:00:26 -0500680void GPTData::PartitionScan(void) {
srs5694e4ac11e2009-08-31 10:13:04 -0400681 BSDData bsdDisklabel;
srs5694e4ac11e2009-08-31 10:13:04 -0400682
683 // Read the MBR & check for BSD disklabel
srs5694546a9c72010-01-26 16:00:26 -0500684 protectiveMBR.ReadMBRData(&myDisk);
685 bsdDisklabel.ReadBSDData(&myDisk, 0, diskSize - 1);
srs5694e4ac11e2009-08-31 10:13:04 -0400686
687 // Load the GPT data, whether or not it's valid
srs5694546a9c72010-01-26 16:00:26 -0500688 ForceLoadGPTData();
srs5694ba00fed2010-01-12 18:18:36 -0500689
Roderick W. Smith4a702a22014-01-25 23:46:42 -0500690 // Some tools create a 0xEE partition that's too big. If this is detected,
691 // normalize it....
692 if ((state == gpt_valid) && !protectiveMBR.DoTheyFit() && (protectiveMBR.GetValidity() == gpt)) {
693 if (!beQuiet) {
694 cerr << "\aThe protective MBR's 0xEE partition is oversized! Auto-repairing.\n\n";
695 } // if
696 protectiveMBR.MakeProtectiveMBR();
697 } // if
698
srs5694ba00fed2010-01-12 18:18:36 -0500699 if (!beQuiet) {
srs5694fed16d02010-01-27 23:03:40 -0500700 cout << "Partition table scan:\n";
srs5694ba00fed2010-01-12 18:18:36 -0500701 protectiveMBR.ShowState();
702 bsdDisklabel.ShowState();
703 ShowAPMState(); // Show whether there's an Apple Partition Map present
704 ShowGPTState(); // Show GPT status
srs5694fed16d02010-01-27 23:03:40 -0500705 cout << "\n";
srs5694ba00fed2010-01-12 18:18:36 -0500706 } // if
srs5694e4ac11e2009-08-31 10:13:04 -0400707
708 if (apmFound) {
srs5694fed16d02010-01-27 23:03:40 -0500709 cout << "\n*******************************************************************\n"
710 << "This disk appears to contain an Apple-format (APM) partition table!\n";
srs56945d58fe02010-01-03 20:57:08 -0500711 if (!justLooking) {
srs5694fed16d02010-01-27 23:03:40 -0500712 cout << "It will be destroyed if you continue!\n";
srs56945d58fe02010-01-03 20:57:08 -0500713 } // if
srs5694fed16d02010-01-27 23:03:40 -0500714 cout << "*******************************************************************\n\n\a";
srs5694e4ac11e2009-08-31 10:13:04 -0400715 } // if
srs5694e4ac11e2009-08-31 10:13:04 -0400716} // GPTData::PartitionScan()
717
718// Read GPT data from a disk.
srs56940a697312010-01-28 21:10:52 -0500719int GPTData::LoadPartitions(const string & deviceFilename) {
srs569408bb0da2010-02-19 17:19:55 -0500720 BSDData bsdDisklabel;
srs5694e321d442010-01-29 17:44:04 -0500721 int err, allOK = 1;
srs5694fed16d02010-01-27 23:03:40 -0500722 MBRValidity mbrState;
srs5694e4ac11e2009-08-31 10:13:04 -0400723
srs5694546a9c72010-01-26 16:00:26 -0500724 if (myDisk.OpenForRead(deviceFilename)) {
srs569455d92612010-03-07 22:16:07 -0500725 err = myDisk.OpenForWrite(deviceFilename);
726 if ((err == 0) && (!justLooking)) {
727 cout << "\aNOTE: Write test failed with error number " << errno
728 << ". It will be impossible to save\nchanges to this disk's partition table!\n";
729#if defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
730 cout << "You may be able to enable writes by exiting this program, typing\n"
731 << "'sysctl kern.geom.debugflags=16' at a shell prompt, and re-running this\n"
732 << "program.\n";
733#endif
734 cout << "\n";
735 } // if
736 myDisk.Close(); // Close and re-open read-only in case of bugs
737 } else allOK = 0; // if
738
739 if (allOK && myDisk.OpenForRead(deviceFilename)) {
srs5694e4ac11e2009-08-31 10:13:04 -0400740 // store disk information....
srs5694546a9c72010-01-26 16:00:26 -0500741 diskSize = myDisk.DiskSize(&err);
742 blockSize = (uint32_t) myDisk.GetBlockSize();
srs5694fed16d02010-01-27 23:03:40 -0500743 device = deviceFilename;
srs5694546a9c72010-01-26 16:00:26 -0500744 PartitionScan(); // Check for partition types, load GPT, & print summary
srs5694e4ac11e2009-08-31 10:13:04 -0400745
srs5694ba00fed2010-01-12 18:18:36 -0500746 whichWasUsed = UseWhichPartitions();
747 switch (whichWasUsed) {
srs5694e4ac11e2009-08-31 10:13:04 -0400748 case use_mbr:
749 XFormPartitions();
750 break;
751 case use_bsd:
srs5694546a9c72010-01-26 16:00:26 -0500752 bsdDisklabel.ReadBSDData(&myDisk, 0, diskSize - 1);
srs5694e4ac11e2009-08-31 10:13:04 -0400753// bsdDisklabel.DisplayBSDData();
754 ClearGPTData();
755 protectiveMBR.MakeProtectiveMBR(1); // clear boot area (option 1)
srs569408bb0da2010-02-19 17:19:55 -0500756 XFormDisklabel(&bsdDisklabel);
srs5694e4ac11e2009-08-31 10:13:04 -0400757 break;
758 case use_gpt:
srs5694fed16d02010-01-27 23:03:40 -0500759 mbrState = protectiveMBR.GetValidity();
760 if ((mbrState == invalid) || (mbrState == mbr))
761 protectiveMBR.MakeProtectiveMBR();
srs5694e4ac11e2009-08-31 10:13:04 -0400762 break;
763 case use_new:
764 ClearGPTData();
765 protectiveMBR.MakeProtectiveMBR();
766 break;
srs56943c0af382010-01-15 19:19:18 -0500767 case use_abort:
768 allOK = 0;
srs56949ddc14b2010-08-22 22:44:42 -0400769 cerr << "Invalid partition data!\n";
srs56943c0af382010-01-15 19:19:18 -0500770 break;
srs5694e4ac11e2009-08-31 10:13:04 -0400771 } // switch
772
srs569455d92612010-03-07 22:16:07 -0500773 if (allOK)
srs56943c0af382010-01-15 19:19:18 -0500774 CheckGPTSize();
srs569455d92612010-03-07 22:16:07 -0500775 myDisk.Close();
srs5694a8582cf2010-03-19 14:21:59 -0400776 ComputeAlignment();
srs5694e4ac11e2009-08-31 10:13:04 -0400777 } else {
778 allOK = 0;
srs5694e4ac11e2009-08-31 10:13:04 -0400779 } // if/else
780 return (allOK);
781} // GPTData::LoadPartitions()
782
783// Loads the GPT, as much as possible. Returns 1 if this seems to have
784// succeeded, 0 if there are obvious problems....
srs5694546a9c72010-01-26 16:00:26 -0500785int GPTData::ForceLoadGPTData(void) {
srs5694cb76c672010-02-11 22:22:22 -0500786 int allOK, validHeaders, loadedTable = 1;
srs5694e4ac11e2009-08-31 10:13:04 -0400787
srs5694cb76c672010-02-11 22:22:22 -0500788 allOK = LoadHeader(&mainHeader, myDisk, 1, &mainCrcOk);
srs5694e4ac11e2009-08-31 10:13:04 -0400789
srs5694cb76c672010-02-11 22:22:22 -0500790 if (mainCrcOk && (mainHeader.backupLBA < diskSize)) {
791 allOK = LoadHeader(&secondHeader, myDisk, mainHeader.backupLBA, &secondCrcOk) && allOK;
792 } else {
srs569408bb0da2010-02-19 17:19:55 -0500793 allOK = LoadHeader(&secondHeader, myDisk, diskSize - UINT64_C(1), &secondCrcOk) && allOK;
794 if (mainCrcOk && (mainHeader.backupLBA >= diskSize))
srs5694fed16d02010-01-27 23:03:40 -0500795 cout << "Warning! Disk size is smaller than the main header indicates! Loading\n"
796 << "secondary header from the last sector of the disk! You should use 'v' to\n"
797 << "verify disk integrity, and perhaps options on the experts' menu to repair\n"
798 << "the disk.\n";
srs5694cb76c672010-02-11 22:22:22 -0500799 } // if/else
800 if (!allOK)
srs5694e4ac11e2009-08-31 10:13:04 -0400801 state = gpt_invalid;
srs5694e4ac11e2009-08-31 10:13:04 -0400802
803 // Return valid headers code: 0 = both headers bad; 1 = main header
804 // good, backup bad; 2 = backup header good, main header bad;
805 // 3 = both headers good. Note these codes refer to valid GPT
srs569423d8d542011-10-01 18:40:10 -0400806 // signatures, version numbers, and CRCs.
srs5694e4ac11e2009-08-31 10:13:04 -0400807 validHeaders = CheckHeaderValidity();
808
809 // Read partitions (from primary array)
810 if (validHeaders > 0) { // if at least one header is OK....
811 // GPT appears to be valid....
812 state = gpt_valid;
813
814 // We're calling the GPT valid, but there's a possibility that one
815 // of the two headers is corrupt. If so, use the one that seems to
816 // be in better shape to regenerate the bad one
srs5694546a9c72010-01-26 16:00:26 -0500817 if (validHeaders == 1) { // valid main header, invalid backup header
srs5694fed16d02010-01-27 23:03:40 -0500818 cerr << "\aCaution: invalid backup GPT header, but valid main header; regenerating\n"
819 << "backup header from main header.\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400820 RebuildSecondHeader();
srs5694546a9c72010-01-26 16:00:26 -0500821 state = gpt_corrupt;
srs5694e4ac11e2009-08-31 10:13:04 -0400822 secondCrcOk = mainCrcOk; // Since regenerated, use CRC validity of main
srs5694546a9c72010-01-26 16:00:26 -0500823 } else if (validHeaders == 2) { // valid backup header, invalid main header
srs5694fed16d02010-01-27 23:03:40 -0500824 cerr << "\aCaution: invalid main GPT header, but valid backup; regenerating main header\n"
825 << "from backup!\n\n";
srs5694546a9c72010-01-26 16:00:26 -0500826 RebuildMainHeader();
827 state = gpt_corrupt;
828 mainCrcOk = secondCrcOk; // Since copied, use CRC validity of backup
srs5694e4ac11e2009-08-31 10:13:04 -0400829 } // if/else/if
830
srs5694546a9c72010-01-26 16:00:26 -0500831 // Figure out which partition table to load....
832 // Load the main partition table, since either its header's CRC is OK or the
833 // backup header's CRC is not OK....
834 if (mainCrcOk || !secondCrcOk) {
835 if (LoadMainTable() == 0)
836 allOK = 0;
837 } else { // bad main header CRC and backup header CRC is OK
838 state = gpt_corrupt;
839 if (LoadSecondTableAsMain()) {
srs5694cb76c672010-02-11 22:22:22 -0500840 loadedTable = 2;
srs5694fed16d02010-01-27 23:03:40 -0500841 cerr << "\aWarning: Invalid CRC on main header data; loaded backup partition table.\n";
srs5694546a9c72010-01-26 16:00:26 -0500842 } else { // backup table bad, bad main header CRC, but try main table in desperation....
843 if (LoadMainTable() == 0) {
844 allOK = 0;
srs5694cb76c672010-02-11 22:22:22 -0500845 loadedTable = 0;
srs5694fed16d02010-01-27 23:03:40 -0500846 cerr << "\a\aWarning! Unable to load either main or backup partition table!\n";
srs5694546a9c72010-01-26 16:00:26 -0500847 } // if
848 } // if/else (LoadSecondTableAsMain())
849 } // if/else (load partition table)
srs5694e4ac11e2009-08-31 10:13:04 -0400850
srs5694cb76c672010-02-11 22:22:22 -0500851 if (loadedTable == 1)
852 secondPartsCrcOk = CheckTable(&secondHeader);
853 else if (loadedTable == 2)
854 mainPartsCrcOk = CheckTable(&mainHeader);
855 else
856 mainPartsCrcOk = secondPartsCrcOk = 0;
srs5694e4ac11e2009-08-31 10:13:04 -0400857
srs5694546a9c72010-01-26 16:00:26 -0500858 // Problem with main partition table; if backup is OK, use it instead....
859 if (secondPartsCrcOk && secondCrcOk && !mainPartsCrcOk) {
860 state = gpt_corrupt;
861 allOK = allOK && LoadSecondTableAsMain();
srs5694cb76c672010-02-11 22:22:22 -0500862 mainPartsCrcOk = 0; // LoadSecondTableAsMain() resets this, so re-flag as bad
srs5694fed16d02010-01-27 23:03:40 -0500863 cerr << "\aWarning! Main partition table CRC mismatch! Loaded backup "
864 << "partition table\ninstead of main partition table!\n\n";
srs5694cb76c672010-02-11 22:22:22 -0500865 } // if */
srs5694546a9c72010-01-26 16:00:26 -0500866
srs5694e4ac11e2009-08-31 10:13:04 -0400867 // Check for valid CRCs and warn if there are problems
868 if ((mainCrcOk == 0) || (secondCrcOk == 0) || (mainPartsCrcOk == 0) ||
869 (secondPartsCrcOk == 0)) {
srs5694fed16d02010-01-27 23:03:40 -0500870 cerr << "Warning! One or more CRCs don't match. You should repair the disk!\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400871 state = gpt_corrupt;
srs5694ba00fed2010-01-12 18:18:36 -0500872 } // if
srs5694e4ac11e2009-08-31 10:13:04 -0400873 } else {
874 state = gpt_invalid;
875 } // if/else
876 return allOK;
877} // GPTData::ForceLoadGPTData()
878
srs5694247657a2009-11-26 18:36:12 -0500879// Loads the partition table pointed to by the main GPT header. The
srs5694e4ac11e2009-08-31 10:13:04 -0400880// main GPT header in memory MUST be valid for this call to do anything
881// sensible!
srs5694546a9c72010-01-26 16:00:26 -0500882// Returns 1 on success, 0 on failure. CRC errors do NOT count as failure.
srs5694e4ac11e2009-08-31 10:13:04 -0400883int GPTData::LoadMainTable(void) {
srs5694cb76c672010-02-11 22:22:22 -0500884 return LoadPartitionTable(mainHeader, myDisk);
srs5694e4ac11e2009-08-31 10:13:04 -0400885} // GPTData::LoadMainTable()
srs5694e7b4ff92009-08-18 13:16:10 -0400886
887// Load the second (backup) partition table as the primary partition
srs5694546a9c72010-01-26 16:00:26 -0500888// table. Used in repair functions, and when starting up if the main
889// partition table is damaged.
890// Returns 1 on success, 0 on failure. CRC errors do NOT count as failure.
891int GPTData::LoadSecondTableAsMain(void) {
srs5694cb76c672010-02-11 22:22:22 -0500892 return LoadPartitionTable(secondHeader, myDisk);
893} // GPTData::LoadSecondTableAsMain()
srs5694e7b4ff92009-08-18 13:16:10 -0400894
srs5694cb76c672010-02-11 22:22:22 -0500895// Load a single GPT header (main or backup) from the specified disk device and
896// sector. Applies byte-order corrections on big-endian platforms. Sets crcOk
897// value appropriately.
898// Returns 1 on success, 0 on failure. Note that CRC errors do NOT qualify as
899// failure.
900int GPTData::LoadHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector, int *crcOk) {
901 int allOK = 1;
srs56941c6f8b02010-02-21 11:09:20 -0500902 GPTHeader tempHeader;
srs5694cb76c672010-02-11 22:22:22 -0500903
904 disk.Seek(sector);
srs56941c6f8b02010-02-21 11:09:20 -0500905 if (disk.Read(&tempHeader, 512) != 512) {
srs5694cb76c672010-02-11 22:22:22 -0500906 cerr << "Warning! Read error " << errno << "; strange behavior now likely!\n";
907 allOK = 0;
908 } // if
srs5694cb76c672010-02-11 22:22:22 -0500909
srs56941c6f8b02010-02-21 11:09:20 -0500910 // Reverse byte order, if necessary
srs5694cb76c672010-02-11 22:22:22 -0500911 if (IsLittleEndian() == 0) {
srs569455d92612010-03-07 22:16:07 -0500912 ReverseHeaderBytes(&tempHeader);
srs5694cb76c672010-02-11 22:22:22 -0500913 } // if
srs5694d1b11e82011-09-18 21:12:28 -0400914 *crcOk = CheckHeaderCRC(&tempHeader);
srs56941c6f8b02010-02-21 11:09:20 -0500915
srs56940283dae2010-04-28 16:44:34 -0400916 if (allOK && (numParts != tempHeader.numParts) && *crcOk) {
srs5694706e5122012-01-21 13:47:24 -0500917 allOK = SetGPTSize(tempHeader.numParts, 0);
srs569455d92612010-03-07 22:16:07 -0500918 }
srs56941c6f8b02010-02-21 11:09:20 -0500919
920 *header = tempHeader;
srs5694cb76c672010-02-11 22:22:22 -0500921 return allOK;
922} // GPTData::LoadHeader
923
924// Load a partition table (either main or secondary) from the specified disk,
925// using header as a reference for what to load. If sector != 0 (the default
926// is 0), loads from the specified sector; otherwise loads from the sector
927// indicated in header.
928// Returns 1 on success, 0 on failure. CRC errors do NOT count as failure.
929int GPTData::LoadPartitionTable(const struct GPTHeader & header, DiskIO & disk, uint64_t sector) {
930 uint32_t sizeOfParts, newCRC;
931 int retval;
932
933 if (disk.OpenForRead()) {
934 if (sector == 0) {
935 retval = disk.Seek(header.partitionEntriesLBA);
936 } else {
937 retval = disk.Seek(sector);
938 } // if/else
srs569455d92612010-03-07 22:16:07 -0500939 if (retval == 1)
srs5694706e5122012-01-21 13:47:24 -0500940 retval = SetGPTSize(header.numParts, 0);
srs5694546a9c72010-01-26 16:00:26 -0500941 if (retval == 1) {
srs5694cb76c672010-02-11 22:22:22 -0500942 sizeOfParts = header.numParts * header.sizeOfPartitionEntries;
943 if (disk.Read(partitions, sizeOfParts) != (int) sizeOfParts) {
srs5694fed16d02010-01-27 23:03:40 -0500944 cerr << "Warning! Read error " << errno << "! Misbehavior now likely!\n";
srs5694546a9c72010-01-26 16:00:26 -0500945 retval = 0;
srs56945d58fe02010-01-03 20:57:08 -0500946 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400947 newCRC = chksum_crc32((unsigned char*) partitions, sizeOfParts);
srs5694cb76c672010-02-11 22:22:22 -0500948 mainPartsCrcOk = secondPartsCrcOk = (newCRC == header.partitionEntriesCRC);
srs56942a9f5da2009-08-26 00:48:01 -0400949 if (IsLittleEndian() == 0)
950 ReversePartitionBytes();
srs5694cb76c672010-02-11 22:22:22 -0500951 if (!mainPartsCrcOk) {
952 cout << "Caution! After loading partitions, the CRC doesn't check out!\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400953 } // if
954 } else {
srs5694cb76c672010-02-11 22:22:22 -0500955 cerr << "Error! Couldn't seek to partition table!\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400956 } // if/else
957 } else {
srs5694fed16d02010-01-27 23:03:40 -0500958 cerr << "Error! Couldn't open device " << device
srs5694cb76c672010-02-11 22:22:22 -0500959 << " when reading partition table!\n";
srs5694546a9c72010-01-26 16:00:26 -0500960 retval = 0;
srs5694e7b4ff92009-08-18 13:16:10 -0400961 } // if/else
srs5694546a9c72010-01-26 16:00:26 -0500962 return retval;
srs5694cb76c672010-02-11 22:22:22 -0500963} // GPTData::LoadPartitionsTable()
964
965// Check the partition table pointed to by header, but don't keep it
966// around.
srs5694a17fe692011-09-10 20:30:20 -0400967// Returns 1 if the CRC is OK & this table matches the one already in memory,
968// 0 if not or if there was a read error.
srs5694cb76c672010-02-11 22:22:22 -0500969int GPTData::CheckTable(struct GPTHeader *header) {
970 uint32_t sizeOfParts, newCRC;
srs5694a17fe692011-09-10 20:30:20 -0400971 GPTPart *partsToCheck;
srs5694d1b11e82011-09-18 21:12:28 -0400972 GPTHeader *otherHeader;
srs5694a17fe692011-09-10 20:30:20 -0400973 int allOK = 0;
srs5694cb76c672010-02-11 22:22:22 -0500974
srs56940283dae2010-04-28 16:44:34 -0400975 // Load partition table into temporary storage to check
srs5694cb76c672010-02-11 22:22:22 -0500976 // its CRC and store the results, then discard this temporary
977 // storage, since we don't use it in any but recovery operations
978 if (myDisk.Seek(header->partitionEntriesLBA)) {
srs5694a17fe692011-09-10 20:30:20 -0400979 partsToCheck = new GPTPart[header->numParts];
srs56940283dae2010-04-28 16:44:34 -0400980 sizeOfParts = header->numParts * header->sizeOfPartitionEntries;
srs5694a17fe692011-09-10 20:30:20 -0400981 if (partsToCheck == NULL) {
srs56946aae2a92011-06-10 01:16:51 -0400982 cerr << "Could not allocate memory in GPTData::CheckTable()! Terminating!\n";
983 exit(1);
984 } // if
srs5694a17fe692011-09-10 20:30:20 -0400985 if (myDisk.Read(partsToCheck, sizeOfParts) != (int) sizeOfParts) {
srs56940283dae2010-04-28 16:44:34 -0400986 cerr << "Warning! Error " << errno << " reading partition table for CRC check!\n";
srs5694cb76c672010-02-11 22:22:22 -0500987 } else {
srs5694d1b11e82011-09-18 21:12:28 -0400988 newCRC = chksum_crc32((unsigned char*) partsToCheck, sizeOfParts);
srs5694a17fe692011-09-10 20:30:20 -0400989 allOK = (newCRC == header->partitionEntriesCRC);
srs5694d1b11e82011-09-18 21:12:28 -0400990 if (header == &mainHeader)
991 otherHeader = &secondHeader;
992 else
993 otherHeader = &mainHeader;
994 if (newCRC != otherHeader->partitionEntriesCRC) {
srs5694a17fe692011-09-10 20:30:20 -0400995 cerr << "Warning! Main and backup partition tables differ! Use the 'c' and 'e' options\n"
996 << "on the recovery & transformation menu to examine the two tables.\n\n";
997 allOK = 0;
998 } // if
srs5694cb76c672010-02-11 22:22:22 -0500999 } // if/else
srs5694a17fe692011-09-10 20:30:20 -04001000 delete[] partsToCheck;
srs5694cb76c672010-02-11 22:22:22 -05001001 } // if
srs5694a17fe692011-09-10 20:30:20 -04001002 return allOK;
srs5694cb76c672010-02-11 22:22:22 -05001003} // GPTData::CheckTable()
srs5694e7b4ff92009-08-18 13:16:10 -04001004
srs56944307ef22012-05-30 12:30:48 -04001005// Writes GPT (and protective MBR) to disk. If quiet==1, moves the second
1006// header later on the disk without asking for permission, if necessary, and
1007// doesn't confirm the operation before writing. If quiet==0, asks permission
1008// before moving the second header and asks for final confirmation of any
1009// write.
srs5694a17fe692011-09-10 20:30:20 -04001010// Returns 1 on successful write, 0 if there was a problem.
srs569464cbd172011-03-01 22:03:54 -05001011int GPTData::SaveGPTData(int quiet) {
srs56944307ef22012-05-30 12:30:48 -04001012 int allOK = 1, syncIt = 1;
srs5694e321d442010-01-29 17:44:04 -05001013 char answer;
srs5694e7b4ff92009-08-18 13:16:10 -04001014
srs5694e7b4ff92009-08-18 13:16:10 -04001015 // First do some final sanity checks....
srs56945d58fe02010-01-03 20:57:08 -05001016
1017 // This test should only fail on read-only disks....
1018 if (justLooking) {
srs5694fed16d02010-01-27 23:03:40 -05001019 cout << "The justLooking flag is set. This probably means you can't write to the disk.\n";
srs56945d58fe02010-01-03 20:57:08 -05001020 allOK = 0;
1021 } // if
1022
srs569464cbd172011-03-01 22:03:54 -05001023 // Check that disk is really big enough to handle the second header...
1024 if (mainHeader.backupLBA >= diskSize) {
1025 cerr << "Caution! Secondary header was placed beyond the disk's limits! Moving the\n"
1026 << "header, but other problems may occur!\n";
1027 MoveSecondHeaderToEnd();
1028 } // if
1029
srs5694e7b4ff92009-08-18 13:16:10 -04001030 // Is there enough space to hold the GPT headers and partition tables,
1031 // given the partition sizes?
srs5694221e0872009-08-29 15:00:31 -04001032 if (CheckGPTSize() > 0) {
srs5694e7b4ff92009-08-18 13:16:10 -04001033 allOK = 0;
1034 } // if
1035
srs5694247657a2009-11-26 18:36:12 -05001036 // Check that second header is properly placed. Warn and ask if this should
1037 // be corrected if the test fails....
srs569464cbd172011-03-01 22:03:54 -05001038 if (mainHeader.backupLBA < (diskSize - UINT64_C(1))) {
1039 if (quiet == 0) {
1040 cout << "Warning! Secondary header is placed too early on the disk! Do you want to\n"
1041 << "correct this problem? ";
1042 if (GetYN() == 'Y') {
1043 MoveSecondHeaderToEnd();
1044 cout << "Have moved second header and partition table to correct location.\n";
1045 } else {
1046 cout << "Have not corrected the problem. Strange problems may occur in the future!\n";
1047 } // if correction requested
1048 } else { // Go ahead and do correction automatically
srs5694247657a2009-11-26 18:36:12 -05001049 MoveSecondHeaderToEnd();
srs569464cbd172011-03-01 22:03:54 -05001050 } // if/else quiet
srs5694247657a2009-11-26 18:36:12 -05001051 } // if
srs5694e7b4ff92009-08-18 13:16:10 -04001052
srs5694d8eed462012-12-15 01:55:21 -05001053 if ((mainHeader.lastUsableLBA >= diskSize) || (mainHeader.lastUsableLBA > mainHeader.backupLBA)) {
1054 if (quiet == 0) {
1055 cout << "Warning! The claimed last usable sector is incorrect! Do you want to correct\n"
1056 << "this problem? ";
1057 if (GetYN() == 'Y') {
1058 MoveSecondHeaderToEnd();
1059 cout << "Have adjusted the second header and last usable sector value.\n";
1060 } else {
1061 cout << "Have not corrected the problem. Strange problems may occur in the future!\n";
1062 } // if correction requested
1063 } else { // go ahead and do correction automatically
1064 MoveSecondHeaderToEnd();
1065 } // if/else quiet
1066 } // if
1067
srs569455d92612010-03-07 22:16:07 -05001068 // Check for overlapping or insane partitions....
1069 if ((FindOverlaps() > 0) || (FindInsanePartitions() > 0)) {
srs5694e4ac11e2009-08-31 10:13:04 -04001070 allOK = 0;
srs5694fed16d02010-01-27 23:03:40 -05001071 cerr << "Aborting write operation!\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001072 } // if
1073
Roderick W. Smith4a702a22014-01-25 23:46:42 -05001074 // Check that protective MBR fits, and warn if it doesn't....
1075 if (!protectiveMBR.DoTheyFit()) {
1076 cerr << "\nPartition(s) in the protective MBR are too big for the disk! Creating a\n"
1077 << "fresh protective or hybrid MBR is recommended.\n";
1078 }
1079
srs5694e4ac11e2009-08-31 10:13:04 -04001080 // Check for mismatched MBR and GPT data, but let it pass if found
1081 // (function displays warning message)
1082 FindHybridMismatches();
srs5694e7b4ff92009-08-18 13:16:10 -04001083
1084 RecomputeCRCs();
1085
srs5694ba00fed2010-01-12 18:18:36 -05001086 if ((allOK) && (!quiet)) {
srs5694fed16d02010-01-27 23:03:40 -05001087 cout << "\nFinal checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING\n"
srs5694bf8950c2011-03-12 01:23:12 -05001088 << "PARTITIONS!!\n\nDo you want to proceed? ";
srs56945d58fe02010-01-03 20:57:08 -05001089 answer = GetYN();
1090 if (answer == 'Y') {
srs569434882942012-03-23 12:49:15 -04001091 cout << "OK; writing new GUID partition table (GPT) to " << myDisk.GetName() << ".\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001092 } else {
1093 allOK = 0;
1094 } // if/else
1095 } // if
1096
1097 // Do it!
1098 if (allOK) {
srs569464cbd172011-03-01 22:03:54 -05001099 if (myDisk.OpenForWrite()) {
srs56948a4ddfc2010-03-21 19:05:49 -04001100 // As per UEFI specs, write the secondary table and GPT first....
srs5694cb76c672010-02-11 22:22:22 -05001101 allOK = SavePartitionTable(myDisk, secondHeader.partitionEntriesLBA);
srs56944307ef22012-05-30 12:30:48 -04001102 if (!allOK) {
srs5694cb76c672010-02-11 22:22:22 -05001103 cerr << "Unable to save backup partition table! Perhaps the 'e' option on the experts'\n"
1104 << "menu will resolve this problem.\n";
srs56944307ef22012-05-30 12:30:48 -04001105 syncIt = 0;
1106 } // if
srs5694e7b4ff92009-08-18 13:16:10 -04001107
1108 // Now write the secondary GPT header...
srs56948a4ddfc2010-03-21 19:05:49 -04001109 allOK = allOK && SaveHeader(&secondHeader, myDisk, mainHeader.backupLBA);
1110
1111 // Now write the main partition tables...
1112 allOK = allOK && SavePartitionTable(myDisk, mainHeader.partitionEntriesLBA);
1113
1114 // Now write the main GPT header...
1115 allOK = allOK && SaveHeader(&mainHeader, myDisk, 1);
1116
1117 // To top it off, write the protective MBR...
1118 allOK = allOK && protectiveMBR.WriteMBRData(&myDisk);
srs5694e7b4ff92009-08-18 13:16:10 -04001119
1120 // re-read the partition table
srs56944307ef22012-05-30 12:30:48 -04001121 // Note: Done even if some write operations failed, but not if all of them failed.
1122 // Done this way because I've received one problem report from a user one whose
1123 // system the MBR write failed but everything else was OK (on a GPT disk under
1124 // Windows), and the failure to sync therefore caused Windows to restore the
1125 // original partition table from its cache. OTOH, such restoration might be
1126 // desirable if the error occurs later; but that seems unlikely unless the initial
1127 // write fails....
1128 if (syncIt)
srs5694546a9c72010-01-26 16:00:26 -05001129 myDisk.DiskSync();
srs5694e7b4ff92009-08-18 13:16:10 -04001130
1131 if (allOK) { // writes completed OK
srs5694fed16d02010-01-27 23:03:40 -05001132 cout << "The operation has completed successfully.\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001133 } else {
srs5694fed16d02010-01-27 23:03:40 -05001134 cerr << "Warning! An error was reported when writing the partition table! This error\n"
srs56944307ef22012-05-30 12:30:48 -04001135 << "MIGHT be harmless, or the disk might be damaged! Checking it is advisable.\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001136 } // if/else
srs56948a4ddfc2010-03-21 19:05:49 -04001137
srs5694546a9c72010-01-26 16:00:26 -05001138 myDisk.Close();
srs5694e7b4ff92009-08-18 13:16:10 -04001139 } else {
srs56945a608532011-03-17 13:53:01 -04001140 cerr << "Unable to open device '" << myDisk.GetName() << "' for writing! Errno is "
srs5694fed16d02010-01-27 23:03:40 -05001141 << errno << "! Aborting write!\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001142 allOK = 0;
srs5694e7b4ff92009-08-18 13:16:10 -04001143 } // if/else
1144 } else {
srs5694fed16d02010-01-27 23:03:40 -05001145 cout << "Aborting write of new partition table.\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001146 } // if
1147
1148 return (allOK);
1149} // GPTData::SaveGPTData()
1150
1151// Save GPT data to a backup file. This function does much less error
1152// checking than SaveGPTData(). It can therefore preserve many types of
1153// corruption for later analysis; however, it preserves only the MBR,
1154// the main GPT header, the backup GPT header, and the main partition
1155// table; it discards the backup partition table, since it should be
1156// identical to the main partition table on healthy disks.
srs56940a697312010-01-28 21:10:52 -05001157int GPTData::SaveGPTBackup(const string & filename) {
1158 int allOK = 1;
srs5694546a9c72010-01-26 16:00:26 -05001159 DiskIO backupFile;
srs5694e7b4ff92009-08-18 13:16:10 -04001160
srs5694546a9c72010-01-26 16:00:26 -05001161 if (backupFile.OpenForWrite(filename)) {
srs56946699b012010-02-04 00:55:30 -05001162 // Recomputing the CRCs is likely to alter them, which could be bad
1163 // if the intent is to save a potentially bad GPT for later analysis;
1164 // but if we don't do this, we get bogus errors when we load the
1165 // backup. I'm favoring misses over false alarms....
1166 RecomputeCRCs();
1167
srs5694546a9c72010-01-26 16:00:26 -05001168 protectiveMBR.WriteMBRData(&backupFile);
srs5694699941e2011-03-21 21:33:57 -04001169 protectiveMBR.SetDisk(&myDisk);
srs5694e7b4ff92009-08-18 13:16:10 -04001170
srs5694cb76c672010-02-11 22:22:22 -05001171 if (allOK) {
srs5694546a9c72010-01-26 16:00:26 -05001172 // MBR write closed disk, so re-open and seek to end....
1173 backupFile.OpenForWrite();
srs5694cb76c672010-02-11 22:22:22 -05001174 allOK = SaveHeader(&mainHeader, backupFile, 1);
1175 } // if (allOK)
srs5694e7b4ff92009-08-18 13:16:10 -04001176
srs5694e7b4ff92009-08-18 13:16:10 -04001177 if (allOK)
srs5694cb76c672010-02-11 22:22:22 -05001178 allOK = SaveHeader(&secondHeader, backupFile, 2);
srs5694e7b4ff92009-08-18 13:16:10 -04001179
srs5694cb76c672010-02-11 22:22:22 -05001180 if (allOK)
1181 allOK = SavePartitionTable(backupFile, 3);
srs5694e7b4ff92009-08-18 13:16:10 -04001182
1183 if (allOK) { // writes completed OK
srs5694fed16d02010-01-27 23:03:40 -05001184 cout << "The operation has completed successfully.\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001185 } else {
srs5694fed16d02010-01-27 23:03:40 -05001186 cerr << "Warning! An error was reported when writing the backup file.\n"
1187 << "It may not be usable!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001188 } // if/else
srs5694546a9c72010-01-26 16:00:26 -05001189 backupFile.Close();
srs5694e7b4ff92009-08-18 13:16:10 -04001190 } else {
srs56945a608532011-03-17 13:53:01 -04001191 cerr << "Unable to open file '" << filename << "' for writing! Aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001192 allOK = 0;
1193 } // if/else
1194 return allOK;
1195} // GPTData::SaveGPTBackup()
1196
srs5694cb76c672010-02-11 22:22:22 -05001197// Write a GPT header (main or backup) to the specified sector. Used by both
1198// the SaveGPTData() and SaveGPTBackup() functions.
1199// Should be passed an architecture-appropriate header (DO NOT call
1200// ReverseHeaderBytes() on the header before calling this function)
1201// Returns 1 on success, 0 on failure
1202int GPTData::SaveHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector) {
1203 int littleEndian, allOK = 1;
1204
1205 littleEndian = IsLittleEndian();
1206 if (!littleEndian)
1207 ReverseHeaderBytes(header);
1208 if (disk.Seek(sector)) {
1209 if (disk.Write(header, 512) == -1)
1210 allOK = 0;
1211 } else allOK = 0; // if (disk.Seek()...)
1212 if (!littleEndian)
1213 ReverseHeaderBytes(header);
1214 return allOK;
1215} // GPTData::SaveHeader()
1216
1217// Save the partitions to the specified sector. Used by both the SaveGPTData()
1218// and SaveGPTBackup() functions.
1219// Should be passed an architecture-appropriate header (DO NOT call
1220// ReverseHeaderBytes() on the header before calling this function)
1221// Returns 1 on success, 0 on failure
1222int GPTData::SavePartitionTable(DiskIO & disk, uint64_t sector) {
1223 int littleEndian, allOK = 1;
1224
1225 littleEndian = IsLittleEndian();
1226 if (disk.Seek(sector)) {
1227 if (!littleEndian)
1228 ReversePartitionBytes();
srs56940283dae2010-04-28 16:44:34 -04001229 if (disk.Write(partitions, mainHeader.sizeOfPartitionEntries * numParts) == -1)
srs5694cb76c672010-02-11 22:22:22 -05001230 allOK = 0;
1231 if (!littleEndian)
1232 ReversePartitionBytes();
1233 } else allOK = 0; // if (myDisk.Seek()...)
1234 return allOK;
1235} // GPTData::SavePartitionTable()
1236
srs5694e7b4ff92009-08-18 13:16:10 -04001237// Load GPT data from a backup file created by SaveGPTBackup(). This function
1238// does minimal error checking. It returns 1 if it completed successfully,
1239// 0 if there was a problem. In the latter case, it creates a new empty
1240// set of partitions.
srs56940a697312010-01-28 21:10:52 -05001241int GPTData::LoadGPTBackup(const string & filename) {
srs5694cb76c672010-02-11 22:22:22 -05001242 int allOK = 1, val, err;
srs56940541b562011-12-18 16:35:25 -05001243 int shortBackup = 0;
srs5694546a9c72010-01-26 16:00:26 -05001244 DiskIO backupFile;
srs5694e7b4ff92009-08-18 13:16:10 -04001245
srs5694546a9c72010-01-26 16:00:26 -05001246 if (backupFile.OpenForRead(filename)) {
srs5694e7b4ff92009-08-18 13:16:10 -04001247 // Let the MBRData class load the saved MBR...
srs5694546a9c72010-01-26 16:00:26 -05001248 protectiveMBR.ReadMBRData(&backupFile, 0); // 0 = don't check block size
srs5694815fb652011-03-18 12:35:56 -04001249 protectiveMBR.SetDisk(&myDisk);
srs5694e7b4ff92009-08-18 13:16:10 -04001250
srs5694cb76c672010-02-11 22:22:22 -05001251 LoadHeader(&mainHeader, backupFile, 1, &mainCrcOk);
srs5694e7b4ff92009-08-18 13:16:10 -04001252
srs5694cb76c672010-02-11 22:22:22 -05001253 // Check backup file size and rebuild second header if file is right
1254 // size to be direct dd copy of MBR, main header, and main partition
1255 // table; if other size, treat it like a GPT fdisk-generated backup
1256 // file
1257 shortBackup = ((backupFile.DiskSize(&err) * backupFile.GetBlockSize()) ==
1258 (mainHeader.numParts * mainHeader.sizeOfPartitionEntries) + 1024);
1259 if (shortBackup) {
1260 RebuildSecondHeader();
1261 secondCrcOk = mainCrcOk;
1262 } else {
1263 LoadHeader(&secondHeader, backupFile, 2, &secondCrcOk);
1264 } // if/else
srs56942a9f5da2009-08-26 00:48:01 -04001265
srs5694e7b4ff92009-08-18 13:16:10 -04001266 // Return valid headers code: 0 = both headers bad; 1 = main header
1267 // good, backup bad; 2 = backup header good, main header bad;
1268 // 3 = both headers good. Note these codes refer to valid GPT
1269 // signatures and version numbers; more subtle problems will elude
1270 // this check!
1271 if ((val = CheckHeaderValidity()) > 0) {
1272 if (val == 2) { // only backup header seems to be good
srs5694706e5122012-01-21 13:47:24 -05001273 SetGPTSize(secondHeader.numParts, 0);
srs5694e7b4ff92009-08-18 13:16:10 -04001274 } else { // main header is OK
srs5694706e5122012-01-21 13:47:24 -05001275 SetGPTSize(mainHeader.numParts, 0);
srs5694e7b4ff92009-08-18 13:16:10 -04001276 } // if/else
1277
srs5694e7b4ff92009-08-18 13:16:10 -04001278 if (secondHeader.currentLBA != diskSize - UINT64_C(1)) {
srs5694fed16d02010-01-27 23:03:40 -05001279 cout << "Warning! Current disk size doesn't match that of the backup!\n"
1280 << "Adjusting sizes to match, but subsequent problems are possible!\n";
srs5694247657a2009-11-26 18:36:12 -05001281 MoveSecondHeaderToEnd();
srs5694e7b4ff92009-08-18 13:16:10 -04001282 } // if
1283
srs5694cb76c672010-02-11 22:22:22 -05001284 if (!LoadPartitionTable(mainHeader, backupFile, (uint64_t) (3 - shortBackup)))
1285 cerr << "Warning! Read error " << errno
1286 << " loading partition table; strange behavior now likely!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001287 } else {
1288 allOK = 0;
1289 } // if/else
srs5694a8582cf2010-03-19 14:21:59 -04001290 // Something went badly wrong, so blank out partitions
1291 if (allOK == 0) {
1292 cerr << "Improper backup file! Clearing all partition data!\n";
1293 ClearGPTData();
1294 protectiveMBR.MakeProtectiveMBR();
1295 } // if
srs5694e7b4ff92009-08-18 13:16:10 -04001296 } else {
1297 allOK = 0;
srs56945a608532011-03-17 13:53:01 -04001298 cerr << "Unable to open file '" << filename << "' for reading! Aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001299 } // if/else
1300
srs5694e7b4ff92009-08-18 13:16:10 -04001301 return allOK;
1302} // GPTData::LoadGPTBackup()
1303
srs569408bb0da2010-02-19 17:19:55 -05001304int GPTData::SaveMBR(void) {
srs569455d92612010-03-07 22:16:07 -05001305 return protectiveMBR.WriteMBRData(&myDisk);
srs569408bb0da2010-02-19 17:19:55 -05001306} // GPTData::SaveMBR()
1307
1308// This function destroys the on-disk GPT structures, but NOT the on-disk
1309// MBR.
1310// Returns 1 if the operation succeeds, 0 if not.
1311int GPTData::DestroyGPT(void) {
srs569401f7f082011-03-15 23:53:31 -04001312 int sum, tableSize, allOK = 1;
srs569408bb0da2010-02-19 17:19:55 -05001313 uint8_t blankSector[512];
1314 uint8_t* emptyTable;
1315
srs569401f7f082011-03-15 23:53:31 -04001316 memset(blankSector, 0, sizeof(blankSector));
Roderick W. Smith84aaff62014-02-17 16:17:11 -05001317 ClearGPTData();
srs569408bb0da2010-02-19 17:19:55 -05001318
1319 if (myDisk.OpenForWrite()) {
1320 if (!myDisk.Seek(mainHeader.currentLBA))
1321 allOK = 0;
1322 if (myDisk.Write(blankSector, 512) != 512) { // blank it out
1323 cerr << "Warning! GPT main header not overwritten! Error is " << errno << "\n";
1324 allOK = 0;
1325 } // if
1326 if (!myDisk.Seek(mainHeader.partitionEntriesLBA))
1327 allOK = 0;
srs56940283dae2010-04-28 16:44:34 -04001328 tableSize = numParts * mainHeader.sizeOfPartitionEntries;
srs569408bb0da2010-02-19 17:19:55 -05001329 emptyTable = new uint8_t[tableSize];
srs56946aae2a92011-06-10 01:16:51 -04001330 if (emptyTable == NULL) {
Roderick W. Smith84aaff62014-02-17 16:17:11 -05001331 cerr << "Could not allocate memory in GPTData::DestroyGPT()! Terminating!\n";
1332 exit(1);
srs56946aae2a92011-06-10 01:16:51 -04001333 } // if
srs569401f7f082011-03-15 23:53:31 -04001334 memset(emptyTable, 0, tableSize);
srs569408bb0da2010-02-19 17:19:55 -05001335 if (allOK) {
1336 sum = myDisk.Write(emptyTable, tableSize);
1337 if (sum != tableSize) {
1338 cerr << "Warning! GPT main partition table not overwritten! Error is " << errno << "\n";
1339 allOK = 0;
1340 } // if write failed
Roderick W. Smith84aaff62014-02-17 16:17:11 -05001341 } // if
1342 if (!myDisk.Seek(secondHeader.partitionEntriesLBA))
1343 allOK = 0;
1344 if (allOK) {
1345 sum = myDisk.Write(emptyTable, tableSize);
1346 if (sum != tableSize) {
1347 cerr << "Warning! GPT backup partition table not overwritten! Error is "
1348 << errno << "\n";
1349 allOK = 0;
1350 } // if wrong size written
srs569408bb0da2010-02-19 17:19:55 -05001351 } // if
Roderick W. Smith84aaff62014-02-17 16:17:11 -05001352 if (!myDisk.Seek(secondHeader.currentLBA))
1353 allOK = 0;
1354 if (allOK) {
1355 if (myDisk.Write(blankSector, 512) != 512) { // blank it out
1356 cerr << "Warning! GPT backup header not overwritten! Error is " << errno << "\n";
srs569408bb0da2010-02-19 17:19:55 -05001357 allOK = 0;
1358 } // if
Roderick W. Smith84aaff62014-02-17 16:17:11 -05001359 } // if
srs569408bb0da2010-02-19 17:19:55 -05001360 myDisk.DiskSync();
1361 myDisk.Close();
1362 cout << "GPT data structures destroyed! You may now partition the disk using fdisk or\n"
1363 << "other utilities.\n";
1364 delete[] emptyTable;
1365 } else {
srs56945a608532011-03-17 13:53:01 -04001366 cerr << "Problem opening '" << device << "' for writing! Program will now terminate.\n";
srs569408bb0da2010-02-19 17:19:55 -05001367 } // if/else (fd != -1)
1368 return (allOK);
1369} // GPTDataTextUI::DestroyGPT()
1370
1371// Wipe MBR data from the disk (zero it out completely)
1372// Returns 1 on success, 0 on failure.
1373int GPTData::DestroyMBR(void) {
srs569401f7f082011-03-15 23:53:31 -04001374 int allOK;
srs569408bb0da2010-02-19 17:19:55 -05001375 uint8_t blankSector[512];
1376
srs569401f7f082011-03-15 23:53:31 -04001377 memset(blankSector, 0, sizeof(blankSector));
srs569408bb0da2010-02-19 17:19:55 -05001378
srs569401f7f082011-03-15 23:53:31 -04001379 allOK = myDisk.OpenForWrite() && myDisk.Seek(0) && (myDisk.Write(blankSector, 512) == 512);
1380
srs569408bb0da2010-02-19 17:19:55 -05001381 if (!allOK)
1382 cerr << "Warning! MBR not overwritten! Error is " << errno << "!\n";
1383 return allOK;
1384} // GPTData::DestroyMBR(void)
1385
srs5694e4ac11e2009-08-31 10:13:04 -04001386// Tell user whether Apple Partition Map (APM) was discovered....
1387void GPTData::ShowAPMState(void) {
1388 if (apmFound)
srs5694fed16d02010-01-27 23:03:40 -05001389 cout << " APM: present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001390 else
srs5694fed16d02010-01-27 23:03:40 -05001391 cout << " APM: not present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001392} // GPTData::ShowAPMState()
1393
1394// Tell user about the state of the GPT data....
1395void GPTData::ShowGPTState(void) {
1396 switch (state) {
1397 case gpt_invalid:
srs5694fed16d02010-01-27 23:03:40 -05001398 cout << " GPT: not present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001399 break;
1400 case gpt_valid:
srs5694fed16d02010-01-27 23:03:40 -05001401 cout << " GPT: present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001402 break;
1403 case gpt_corrupt:
srs5694fed16d02010-01-27 23:03:40 -05001404 cout << " GPT: damaged\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001405 break;
1406 default:
srs5694fed16d02010-01-27 23:03:40 -05001407 cout << "\a GPT: unknown -- bug!\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001408 break;
1409 } // switch
1410} // GPTData::ShowGPTState()
1411
1412// Display the basic GPT data
1413void GPTData::DisplayGPTData(void) {
srs5694e321d442010-01-29 17:44:04 -05001414 uint32_t i;
srs5694e4ac11e2009-08-31 10:13:04 -04001415 uint64_t temp, totalFree;
1416
srs5694fed16d02010-01-27 23:03:40 -05001417 cout << "Disk " << device << ": " << diskSize << " sectors, "
srs569401f7f082011-03-15 23:53:31 -04001418 << BytesToIeee(diskSize, blockSize) << "\n";
srs5694fed16d02010-01-27 23:03:40 -05001419 cout << "Logical sector size: " << blockSize << " bytes\n";
srs56945a081752010-09-24 20:39:41 -04001420 cout << "Disk identifier (GUID): " << mainHeader.diskGUID << "\n";
srs56940283dae2010-04-28 16:44:34 -04001421 cout << "Partition table holds up to " << numParts << " entries\n";
srs5694fed16d02010-01-27 23:03:40 -05001422 cout << "First usable sector is " << mainHeader.firstUsableLBA
1423 << ", last usable sector is " << mainHeader.lastUsableLBA << "\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001424 totalFree = FindFreeBlocks(&i, &temp);
srs56948a4ddfc2010-03-21 19:05:49 -04001425 cout << "Partitions will be aligned on " << sectorAlignment << "-sector boundaries\n";
srs5694fed16d02010-01-27 23:03:40 -05001426 cout << "Total free space is " << totalFree << " sectors ("
srs569401f7f082011-03-15 23:53:31 -04001427 << BytesToIeee(totalFree, blockSize) << ")\n";
srs5694fed16d02010-01-27 23:03:40 -05001428 cout << "\nNumber Start (sector) End (sector) Size Code Name\n";
srs56940283dae2010-04-28 16:44:34 -04001429 for (i = 0; i < numParts; i++) {
srs5694978041c2009-09-21 20:51:47 -04001430 partitions[i].ShowSummary(i, blockSize);
srs5694e4ac11e2009-08-31 10:13:04 -04001431 } // for
1432} // GPTData::DisplayGPTData()
1433
srs5694e4ac11e2009-08-31 10:13:04 -04001434// Show detailed information on the specified partition
1435void GPTData::ShowPartDetails(uint32_t partNum) {
Roderick W. Smith24bba6e2013-10-12 19:07:16 -04001436 if ((partNum < numParts) && !IsFreePartNum(partNum)) {
srs5694e4ac11e2009-08-31 10:13:04 -04001437 partitions[partNum].ShowDetails(blockSize);
1438 } else {
Roderick W. Smith24bba6e2013-10-12 19:07:16 -04001439 cout << "Partition #" << partNum + 1 << " does not exist.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001440 } // if
1441} // GPTData::ShowPartDetails()
1442
srs5694e4ac11e2009-08-31 10:13:04 -04001443/**************************************************************************
1444 * *
1445 * Partition table transformation functions (MBR or BSD disklabel to GPT) *
1446 * (some of these functions may require user interaction) *
1447 * *
1448 **************************************************************************/
1449
srs569408bb0da2010-02-19 17:19:55 -05001450// Examines the MBR & GPT data to determine which set of data to use: the
1451// MBR (use_mbr), the GPT (use_gpt), the BSD disklabel (use_bsd), or create
1452// a new set of partitions (use_new). A return value of use_abort indicates
1453// that this function couldn't determine what to do. Overriding functions
1454// in derived classes may ask users questions in such cases.
srs5694e4ac11e2009-08-31 10:13:04 -04001455WhichToUse GPTData::UseWhichPartitions(void) {
1456 WhichToUse which = use_new;
1457 MBRValidity mbrState;
srs5694e4ac11e2009-08-31 10:13:04 -04001458
1459 mbrState = protectiveMBR.GetValidity();
1460
1461 if ((state == gpt_invalid) && ((mbrState == mbr) || (mbrState == hybrid))) {
srs5694fed16d02010-01-27 23:03:40 -05001462 cout << "\n***************************************************************\n"
Roderick W. Smith1eea9b02013-07-06 22:52:58 -04001463 << "Found invalid GPT and valid MBR; converting MBR to GPT format\n"
1464 << "in memory. ";
srs56945d58fe02010-01-03 20:57:08 -05001465 if (!justLooking) {
Roderick W. Smith1eea9b02013-07-06 22:52:58 -04001466 cout << "\aTHIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by\n"
1467 << "typing 'q' if you don't want to convert your MBR partitions\n"
1468 << "to GPT format!";
srs56945d58fe02010-01-03 20:57:08 -05001469 } // if
Roderick W. Smith1eea9b02013-07-06 22:52:58 -04001470 cout << "\n***************************************************************\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001471 which = use_mbr;
1472 } // if
1473
1474 if ((state == gpt_invalid) && bsdFound) {
srs5694fed16d02010-01-27 23:03:40 -05001475 cout << "\n**********************************************************************\n"
1476 << "Found invalid GPT and valid BSD disklabel; converting BSD disklabel\n"
1477 << "to GPT format.";
srs56940a697312010-01-28 21:10:52 -05001478 if ((!justLooking) && (!beQuiet)) {
srs56940283dae2010-04-28 16:44:34 -04001479 cout << "\a THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Your first\n"
srs5694fed16d02010-01-27 23:03:40 -05001480 << "BSD partition will likely be unusable. Exit by typing 'q' if you don't\n"
1481 << "want to convert your BSD partitions to GPT format!";
srs56945d58fe02010-01-03 20:57:08 -05001482 } // if
srs5694fed16d02010-01-27 23:03:40 -05001483 cout << "\n**********************************************************************\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001484 which = use_bsd;
1485 } // if
1486
1487 if ((state == gpt_valid) && (mbrState == gpt)) {
srs5694e4ac11e2009-08-31 10:13:04 -04001488 which = use_gpt;
srs56943c0af382010-01-15 19:19:18 -05001489 if (!beQuiet)
srs5694fed16d02010-01-27 23:03:40 -05001490 cout << "Found valid GPT with protective MBR; using GPT.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001491 } // if
1492 if ((state == gpt_valid) && (mbrState == hybrid)) {
srs5694e4ac11e2009-08-31 10:13:04 -04001493 which = use_gpt;
srs56943c0af382010-01-15 19:19:18 -05001494 if (!beQuiet)
srs5694fed16d02010-01-27 23:03:40 -05001495 cout << "Found valid GPT with hybrid MBR; using GPT.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001496 } // if
1497 if ((state == gpt_valid) && (mbrState == invalid)) {
srs56940a697312010-01-28 21:10:52 -05001498 cout << "\aFound valid GPT with corrupt MBR; using GPT and will write new\n"
srs5694fed16d02010-01-27 23:03:40 -05001499 << "protective MBR on save.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001500 which = use_gpt;
srs5694e4ac11e2009-08-31 10:13:04 -04001501 } // if
1502 if ((state == gpt_valid) && (mbrState == mbr)) {
srs569408bb0da2010-02-19 17:19:55 -05001503 which = use_abort;
srs5694e4ac11e2009-08-31 10:13:04 -04001504 } // if
1505
srs5694e4ac11e2009-08-31 10:13:04 -04001506 if (state == gpt_corrupt) {
srs569408bb0da2010-02-19 17:19:55 -05001507 if (mbrState == gpt) {
1508 cout << "\a\a****************************************************************************\n"
1509 << "Caution: Found protective or hybrid MBR and corrupt GPT. Using GPT, but disk\n"
1510 << "verification and recovery are STRONGLY recommended.\n"
1511 << "****************************************************************************\n";
1512 which = use_gpt;
srs56943c0af382010-01-15 19:19:18 -05001513 } else {
srs569408bb0da2010-02-19 17:19:55 -05001514 which = use_abort;
1515 } // if/else MBR says disk is GPT
1516 } // if GPT corrupt
srs5694e4ac11e2009-08-31 10:13:04 -04001517
1518 if (which == use_new)
srs5694fed16d02010-01-27 23:03:40 -05001519 cout << "Creating new GPT entries.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001520
1521 return which;
1522} // UseWhichPartitions()
1523
srs569408bb0da2010-02-19 17:19:55 -05001524// Convert MBR partition table into GPT form.
1525void GPTData::XFormPartitions(void) {
srs5694e4ac11e2009-08-31 10:13:04 -04001526 int i, numToConvert;
1527 uint8_t origType;
srs5694e4ac11e2009-08-31 10:13:04 -04001528
1529 // Clear out old data & prepare basics....
1530 ClearGPTData();
1531
1532 // Convert the smaller of the # of GPT or MBR partitions
srs56940283dae2010-04-28 16:44:34 -04001533 if (numParts > MAX_MBR_PARTS)
srs5694978041c2009-09-21 20:51:47 -04001534 numToConvert = MAX_MBR_PARTS;
srs5694e4ac11e2009-08-31 10:13:04 -04001535 else
srs56940283dae2010-04-28 16:44:34 -04001536 numToConvert = numParts;
srs5694e4ac11e2009-08-31 10:13:04 -04001537
1538 for (i = 0; i < numToConvert; i++) {
1539 origType = protectiveMBR.GetType(i);
1540 // don't waste CPU time trying to convert extended, hybrid protective, or
1541 // null (non-existent) partitions
srs5694e35eb1b2009-09-14 00:29:34 -04001542 if ((origType != 0x05) && (origType != 0x0f) && (origType != 0x85) &&
srs56946699b012010-02-04 00:55:30 -05001543 (origType != 0x00) && (origType != 0xEE))
srs5694e4ac11e2009-08-31 10:13:04 -04001544 partitions[i] = protectiveMBR.AsGPT(i);
1545 } // for
1546
1547 // Convert MBR into protective MBR
1548 protectiveMBR.MakeProtectiveMBR();
1549
1550 // Record that all original CRCs were OK so as not to raise flags
1551 // when doing a disk verification
1552 mainCrcOk = secondCrcOk = mainPartsCrcOk = secondPartsCrcOk = 1;
srs5694e4ac11e2009-08-31 10:13:04 -04001553} // GPTData::XFormPartitions()
1554
1555// Transforms BSD disklabel on the specified partition (numbered from 0).
srs569408bb0da2010-02-19 17:19:55 -05001556// If an invalid partition number is given, the program does nothing.
srs5694e4ac11e2009-08-31 10:13:04 -04001557// Returns the number of new partitions created.
srs569408bb0da2010-02-19 17:19:55 -05001558int GPTData::XFormDisklabel(uint32_t partNum) {
1559 uint32_t low, high;
srs5694e4ac11e2009-08-31 10:13:04 -04001560 int goOn = 1, numDone = 0;
1561 BSDData disklabel;
1562
srs569408bb0da2010-02-19 17:19:55 -05001563 if (GetPartRange(&low, &high) == 0) {
1564 goOn = 0;
1565 cout << "No partitions!\n";
1566 } // if
1567 if (partNum > high) {
1568 goOn = 0;
1569 cout << "Specified partition is invalid!\n";
1570 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04001571
srs569408bb0da2010-02-19 17:19:55 -05001572 // If all is OK, read the disklabel and convert it.
1573 if (goOn) {
1574 goOn = disklabel.ReadBSDData(&myDisk, partitions[partNum].GetFirstLBA(),
1575 partitions[partNum].GetLastLBA());
1576 if ((goOn) && (disklabel.IsDisklabel())) {
1577 numDone = XFormDisklabel(&disklabel);
1578 if (numDone == 1)
1579 cout << "Converted 1 BSD partition.\n";
1580 else
1581 cout << "Converted " << numDone << " BSD partitions.\n";
1582 } else {
1583 cout << "Unable to convert partitions! Unrecognized BSD disklabel.\n";
1584 } // if/else
1585 } // if
1586 if (numDone > 0) { // converted partitions; delete carrier
1587 partitions[partNum].BlankPartition();
1588 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04001589 return numDone;
srs569455d92612010-03-07 22:16:07 -05001590} // GPTData::XFormDisklabel(uint32_t i)
srs5694e4ac11e2009-08-31 10:13:04 -04001591
1592// Transform the partitions on an already-loaded BSD disklabel...
srs569408bb0da2010-02-19 17:19:55 -05001593int GPTData::XFormDisklabel(BSDData* disklabel) {
1594 int i, partNum = 0, numDone = 0;
srs5694e4ac11e2009-08-31 10:13:04 -04001595
srs569408bb0da2010-02-19 17:19:55 -05001596 if (disklabel->IsDisklabel()) {
srs5694e4ac11e2009-08-31 10:13:04 -04001597 for (i = 0; i < disklabel->GetNumParts(); i++) {
srs569408bb0da2010-02-19 17:19:55 -05001598 partNum = FindFirstFreePart();
1599 if (partNum >= 0) {
1600 partitions[partNum] = disklabel->AsGPT(i);
1601 if (partitions[partNum].IsUsed())
1602 numDone++;
1603 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04001604 } // for
srs569408bb0da2010-02-19 17:19:55 -05001605 if (partNum == -1)
1606 cerr << "Warning! Too many partitions to convert!\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001607 } // if
1608
1609 // Record that all original CRCs were OK so as not to raise flags
1610 // when doing a disk verification
1611 mainCrcOk = secondCrcOk = mainPartsCrcOk = secondPartsCrcOk = 1;
1612
1613 return numDone;
1614} // GPTData::XFormDisklabel(BSDData* disklabel)
1615
srs569408bb0da2010-02-19 17:19:55 -05001616// Add one GPT partition to MBR. Used by PartsToMBR() functions. Created
1617// partition has the active/bootable flag UNset and uses the GPT fdisk
1618// type code divided by 0x0100 as the MBR type code.
1619// Returns 1 if operation was 100% successful, 0 if there were ANY
1620// problems.
srs5694978041c2009-09-21 20:51:47 -04001621int GPTData::OnePartToMBR(uint32_t gptPart, int mbrPart) {
srs569408bb0da2010-02-19 17:19:55 -05001622 int allOK = 1;
srs5694fed16d02010-01-27 23:03:40 -05001623
srs5694978041c2009-09-21 20:51:47 -04001624 if ((mbrPart < 0) || (mbrPart > 3)) {
srs5694fed16d02010-01-27 23:03:40 -05001625 cout << "MBR partition " << mbrPart + 1 << " is out of range; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001626 allOK = 0;
1627 } // if
srs56940283dae2010-04-28 16:44:34 -04001628 if (gptPart >= numParts) {
srs5694fed16d02010-01-27 23:03:40 -05001629 cout << "GPT partition " << gptPart + 1 << " is out of range; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001630 allOK = 0;
1631 } // if
1632 if (allOK && (partitions[gptPart].GetLastLBA() == UINT64_C(0))) {
srs5694fed16d02010-01-27 23:03:40 -05001633 cout << "GPT partition " << gptPart + 1 << " is undefined; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001634 allOK = 0;
1635 } // if
1636 if (allOK && (partitions[gptPart].GetFirstLBA() <= UINT32_MAX) &&
1637 (partitions[gptPart].GetLengthLBA() <= UINT32_MAX)) {
1638 if (partitions[gptPart].GetLastLBA() > UINT32_MAX) {
srs5694fed16d02010-01-27 23:03:40 -05001639 cout << "Caution: Partition end point past 32-bit pointer boundary;"
1640 << " some OSes may\nreact strangely.\n";
srs569408bb0da2010-02-19 17:19:55 -05001641 } // if
srs5694978041c2009-09-21 20:51:47 -04001642 protectiveMBR.MakePart(mbrPart, (uint32_t) partitions[gptPart].GetFirstLBA(),
srs569408bb0da2010-02-19 17:19:55 -05001643 (uint32_t) partitions[gptPart].GetLengthLBA(),
1644 partitions[gptPart].GetHexType() / 256, 0);
srs5694978041c2009-09-21 20:51:47 -04001645 } else { // partition out of range
srs569408bb0da2010-02-19 17:19:55 -05001646 if (allOK) // Display only if "else" triggered by out-of-bounds condition
1647 cout << "Partition " << gptPart + 1 << " begins beyond the 32-bit pointer limit of MBR "
1648 << "partitions, or is\n too big; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001649 allOK = 0;
1650 } // if/else
1651 return allOK;
1652} // GPTData::OnePartToMBR()
1653
srs5694e4ac11e2009-08-31 10:13:04 -04001654
1655/**********************************************************************
1656 * *
1657 * Functions that adjust GPT data structures WITHOUT user interaction *
1658 * (they may display information for the user's benefit, though) *
1659 * *
1660 **********************************************************************/
1661
1662// Resizes GPT to specified number of entries. Creates a new table if
srs5694706e5122012-01-21 13:47:24 -05001663// necessary, copies data if it already exists. If fillGPTSectors is 1
1664// (the default), rounds numEntries to fill all the sectors necessary to
1665// hold the GPT.
1666// Returns 1 if all goes well, 0 if an error is encountered.
1667int GPTData::SetGPTSize(uint32_t numEntries, int fillGPTSectors) {
srs569408bb0da2010-02-19 17:19:55 -05001668 GPTPart* newParts;
srs5694706e5122012-01-21 13:47:24 -05001669 uint32_t i, high, copyNum, entriesPerSector;
srs5694e4ac11e2009-08-31 10:13:04 -04001670 int allOK = 1;
1671
1672 // First, adjust numEntries upward, if necessary, to get a number
1673 // that fills the allocated sectors
srs5694706e5122012-01-21 13:47:24 -05001674 entriesPerSector = blockSize / GPT_SIZE;
1675 if (fillGPTSectors && ((numEntries % entriesPerSector) != 0)) {
srs5694fed16d02010-01-27 23:03:40 -05001676 cout << "Adjusting GPT size from " << numEntries << " to ";
srs5694706e5122012-01-21 13:47:24 -05001677 numEntries = ((numEntries / entriesPerSector) + 1) * entriesPerSector;
srs5694fed16d02010-01-27 23:03:40 -05001678 cout << numEntries << " to fill the sector\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001679 } // if
1680
srs5694247657a2009-11-26 18:36:12 -05001681 // Do the work only if the # of partitions is changing. Along with being
srs569455d92612010-03-07 22:16:07 -05001682 // efficient, this prevents mucking with the location of the secondary
srs5694247657a2009-11-26 18:36:12 -05001683 // partition table, which causes problems when loading data from a RAID
1684 // array that's been expanded because this function is called when loading
1685 // data.
srs56940283dae2010-04-28 16:44:34 -04001686 if (((numEntries != numParts) || (partitions == NULL)) && (numEntries > 0)) {
srs569401f7f082011-03-15 23:53:31 -04001687 newParts = new GPTPart [numEntries];
srs5694247657a2009-11-26 18:36:12 -05001688 if (newParts != NULL) {
1689 if (partitions != NULL) { // existing partitions; copy them over
1690 GetPartRange(&i, &high);
1691 if (numEntries < (high + 1)) { // Highest entry too high for new #
srs5694fed16d02010-01-27 23:03:40 -05001692 cout << "The highest-numbered partition is " << high + 1
1693 << ", which is greater than the requested\n"
1694 << "partition table size of " << numEntries
1695 << "; cannot resize. Perhaps sorting will help.\n";
srs5694247657a2009-11-26 18:36:12 -05001696 allOK = 0;
srs5694815fb652011-03-18 12:35:56 -04001697 delete[] newParts;
srs5694247657a2009-11-26 18:36:12 -05001698 } else { // go ahead with copy
srs56940283dae2010-04-28 16:44:34 -04001699 if (numEntries < numParts)
srs5694247657a2009-11-26 18:36:12 -05001700 copyNum = numEntries;
1701 else
srs56940283dae2010-04-28 16:44:34 -04001702 copyNum = numParts;
srs5694247657a2009-11-26 18:36:12 -05001703 for (i = 0; i < copyNum; i++) {
1704 newParts[i] = partitions[i];
1705 } // for
srs569401f7f082011-03-15 23:53:31 -04001706 delete[] partitions;
srs5694247657a2009-11-26 18:36:12 -05001707 partitions = newParts;
srs5694247657a2009-11-26 18:36:12 -05001708 } // if
1709 } else { // No existing partition table; just create it
srs5694e4ac11e2009-08-31 10:13:04 -04001710 partitions = newParts;
srs5694247657a2009-11-26 18:36:12 -05001711 } // if/else existing partitions
srs56940283dae2010-04-28 16:44:34 -04001712 numParts = numEntries;
srs5694706e5122012-01-21 13:47:24 -05001713 mainHeader.firstUsableLBA = ((numEntries * GPT_SIZE) / blockSize) + (((numEntries * GPT_SIZE) % blockSize) != 0) + 2 ;
srs5694247657a2009-11-26 18:36:12 -05001714 secondHeader.firstUsableLBA = mainHeader.firstUsableLBA;
1715 MoveSecondHeaderToEnd();
1716 if (diskSize > 0)
1717 CheckGPTSize();
1718 } else { // Bad memory allocation
srs56946aae2a92011-06-10 01:16:51 -04001719 cerr << "Error allocating memory for partition table! Size is unchanged!\n";
srs5694247657a2009-11-26 18:36:12 -05001720 allOK = 0;
1721 } // if/else
srs5694e4ac11e2009-08-31 10:13:04 -04001722 } // if/else
srs56940283dae2010-04-28 16:44:34 -04001723 mainHeader.numParts = numParts;
1724 secondHeader.numParts = numParts;
srs5694e4ac11e2009-08-31 10:13:04 -04001725 return (allOK);
1726} // GPTData::SetGPTSize()
1727
1728// Blank the partition array
1729void GPTData::BlankPartitions(void) {
1730 uint32_t i;
1731
srs56940283dae2010-04-28 16:44:34 -04001732 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04001733 partitions[i].BlankPartition();
1734 } // for
1735} // GPTData::BlankPartitions()
1736
srs5694ba00fed2010-01-12 18:18:36 -05001737// Delete a partition by number. Returns 1 if successful,
1738// 0 if there was a problem. Returns 1 if partition was in
1739// range, 0 if it was out of range.
1740int GPTData::DeletePartition(uint32_t partNum) {
1741 uint64_t startSector, length;
srs56940283dae2010-04-28 16:44:34 -04001742 uint32_t low, high, numUsedParts, retval = 1;;
srs5694ba00fed2010-01-12 18:18:36 -05001743
srs56940283dae2010-04-28 16:44:34 -04001744 numUsedParts = GetPartRange(&low, &high);
1745 if ((numUsedParts > 0) && (partNum >= low) && (partNum <= high)) {
srs5694ba00fed2010-01-12 18:18:36 -05001746 // In case there's a protective MBR, look for & delete matching
1747 // MBR partition....
1748 startSector = partitions[partNum].GetFirstLBA();
1749 length = partitions[partNum].GetLengthLBA();
1750 protectiveMBR.DeleteByLocation(startSector, length);
1751
1752 // Now delete the GPT partition
1753 partitions[partNum].BlankPartition();
1754 } else {
srs5694fed16d02010-01-27 23:03:40 -05001755 cerr << "Partition number " << partNum + 1 << " out of range!\n";
srs5694ba00fed2010-01-12 18:18:36 -05001756 retval = 0;
1757 } // if/else
1758 return retval;
1759} // GPTData::DeletePartition(uint32_t partNum)
1760
srs569408bb0da2010-02-19 17:19:55 -05001761// Non-interactively create a partition.
1762// Returns 1 if the operation was successful, 0 if a problem was discovered.
srs5694e321d442010-01-29 17:44:04 -05001763uint32_t GPTData::CreatePartition(uint32_t partNum, uint64_t startSector, uint64_t endSector) {
srs5694ba00fed2010-01-12 18:18:36 -05001764 int retval = 1; // assume there'll be no problems
srs56945a081752010-09-24 20:39:41 -04001765 uint64_t origSector = startSector;
srs5694ba00fed2010-01-12 18:18:36 -05001766
1767 if (IsFreePartNum(partNum)) {
srs56945a081752010-09-24 20:39:41 -04001768 if (Align(&startSector)) {
1769 cout << "Information: Moved requested sector from " << origSector << " to "
1770 << startSector << " in\norder to align on " << sectorAlignment
1771 << "-sector boundaries.\n";
1772 } // if
srs5694ba00fed2010-01-12 18:18:36 -05001773 if (IsFree(startSector) && (startSector <= endSector)) {
1774 if (FindLastInFree(startSector) >= endSector) {
1775 partitions[partNum].SetFirstLBA(startSector);
1776 partitions[partNum].SetLastLBA(endSector);
srs56940741fa22013-01-09 12:55:40 -05001777 partitions[partNum].SetType(DEFAULT_GPT_TYPE);
srs56946699b012010-02-04 00:55:30 -05001778 partitions[partNum].RandomizeUniqueGUID();
srs5694ba00fed2010-01-12 18:18:36 -05001779 } else retval = 0; // if free space until endSector
1780 } else retval = 0; // if startSector is free
1781 } else retval = 0; // if legal partition number
1782 return retval;
1783} // GPTData::CreatePartition(partNum, startSector, endSector)
1784
srs5694e4ac11e2009-08-31 10:13:04 -04001785// Sort the GPT entries, eliminating gaps and making for a logical
srs56949a46b042011-03-15 00:34:10 -04001786// ordering.
srs5694e4ac11e2009-08-31 10:13:04 -04001787void GPTData::SortGPT(void) {
srs56949a46b042011-03-15 00:34:10 -04001788 if (numParts > 0)
srs569401f7f082011-03-15 23:53:31 -04001789 sort(partitions, partitions + numParts);
srs5694e4ac11e2009-08-31 10:13:04 -04001790} // GPTData::SortGPT()
1791
srs569408bb0da2010-02-19 17:19:55 -05001792// Swap the contents of two partitions.
1793// Returns 1 if successful, 0 if either partition is out of range
1794// (that is, not a legal number; either or both can be empty).
1795// Note that if partNum1 = partNum2 and this number is in range,
1796// it will be considered successful.
1797int GPTData::SwapPartitions(uint32_t partNum1, uint32_t partNum2) {
1798 GPTPart temp;
1799 int allOK = 1;
1800
srs56940283dae2010-04-28 16:44:34 -04001801 if ((partNum1 < numParts) && (partNum2 < numParts)) {
srs569408bb0da2010-02-19 17:19:55 -05001802 if (partNum1 != partNum2) {
1803 temp = partitions[partNum1];
1804 partitions[partNum1] = partitions[partNum2];
1805 partitions[partNum2] = temp;
1806 } // if
1807 } else allOK = 0; // partition numbers are valid
1808 return allOK;
1809} // GPTData::SwapPartitions()
1810
srs5694e4ac11e2009-08-31 10:13:04 -04001811// Set up data structures for entirely new set of partitions on the
1812// specified device. Returns 1 if OK, 0 if there were problems.
srs5694e35eb1b2009-09-14 00:29:34 -04001813// Note that this function does NOT clear the protectiveMBR data
1814// structure, since it may hold the original MBR partitions if the
1815// program was launched on an MBR disk, and those may need to be
1816// converted to GPT format.
srs5694e4ac11e2009-08-31 10:13:04 -04001817int GPTData::ClearGPTData(void) {
srs5694e35eb1b2009-09-14 00:29:34 -04001818 int goOn = 1, i;
srs5694e4ac11e2009-08-31 10:13:04 -04001819
1820 // Set up the partition table....
srs56949a46b042011-03-15 00:34:10 -04001821 delete[] partitions;
srs5694e4ac11e2009-08-31 10:13:04 -04001822 partitions = NULL;
1823 SetGPTSize(NUM_GPT_ENTRIES);
1824
1825 // Now initialize a bunch of stuff that's static....
1826 mainHeader.signature = GPT_SIGNATURE;
1827 mainHeader.revision = 0x00010000;
srs5694978041c2009-09-21 20:51:47 -04001828 mainHeader.headerSize = HEADER_SIZE;
srs5694e4ac11e2009-08-31 10:13:04 -04001829 mainHeader.reserved = 0;
1830 mainHeader.currentLBA = UINT64_C(1);
1831 mainHeader.partitionEntriesLBA = (uint64_t) 2;
1832 mainHeader.sizeOfPartitionEntries = GPT_SIZE;
1833 for (i = 0; i < GPT_RESERVED; i++) {
1834 mainHeader.reserved2[i] = '\0';
1835 } // for
srs56940873e9d2010-10-07 13:00:45 -04001836 if (blockSize > 0)
1837 sectorAlignment = DEFAULT_ALIGNMENT * SECTOR_SIZE / blockSize;
1838 else
1839 sectorAlignment = DEFAULT_ALIGNMENT;
srs5694e4ac11e2009-08-31 10:13:04 -04001840
1841 // Now some semi-static items (computed based on end of disk)
1842 mainHeader.backupLBA = diskSize - UINT64_C(1);
1843 mainHeader.lastUsableLBA = diskSize - mainHeader.firstUsableLBA;
1844
1845 // Set a unique GUID for the disk, based on random numbers
srs56946699b012010-02-04 00:55:30 -05001846 mainHeader.diskGUID.Randomize();
srs5694e4ac11e2009-08-31 10:13:04 -04001847
1848 // Copy main header to backup header
1849 RebuildSecondHeader();
1850
1851 // Blank out the partitions array....
1852 BlankPartitions();
1853
1854 // Flag all CRCs as being OK....
1855 mainCrcOk = 1;
1856 secondCrcOk = 1;
1857 mainPartsCrcOk = 1;
1858 secondPartsCrcOk = 1;
1859
1860 return (goOn);
1861} // GPTData::ClearGPTData()
1862
srs5694247657a2009-11-26 18:36:12 -05001863// Set the location of the second GPT header data to the end of the disk.
srs569464cbd172011-03-01 22:03:54 -05001864// If the disk size has actually changed, this also adjusts the protective
1865// entry in the MBR, since it's probably no longer correct.
srs5694247657a2009-11-26 18:36:12 -05001866// Used internally and called by the 'e' option on the recovery &
1867// transformation menu, to help users of RAID arrays who add disk space
srs569464cbd172011-03-01 22:03:54 -05001868// to their arrays or to adjust data structures in restore operations
1869// involving unequal-sized disks.
srs5694247657a2009-11-26 18:36:12 -05001870void GPTData::MoveSecondHeaderToEnd() {
srs56948bb78762009-11-24 15:43:49 -05001871 mainHeader.backupLBA = secondHeader.currentLBA = diskSize - UINT64_C(1);
srs569464cbd172011-03-01 22:03:54 -05001872 if (mainHeader.lastUsableLBA != diskSize - mainHeader.firstUsableLBA) {
1873 if (protectiveMBR.GetValidity() == hybrid) {
1874 protectiveMBR.OptimizeEESize();
1875 RecomputeCHS();
1876 } // if
1877 if (protectiveMBR.GetValidity() == gpt)
1878 MakeProtectiveMBR();
1879 } // if
srs56948bb78762009-11-24 15:43:49 -05001880 mainHeader.lastUsableLBA = secondHeader.lastUsableLBA = diskSize - mainHeader.firstUsableLBA;
1881 secondHeader.partitionEntriesLBA = secondHeader.lastUsableLBA + UINT64_C(1);
1882} // GPTData::FixSecondHeaderLocation()
1883
srs5694699941e2011-03-21 21:33:57 -04001884// Sets the partition's name to the specified UnicodeString without
1885// user interaction.
1886// Returns 1 on success, 0 on failure (invalid partition number).
srs56945a608532011-03-17 13:53:01 -04001887int GPTData::SetName(uint32_t partNum, const UnicodeString & theName) {
srs5694ba00fed2010-01-12 18:18:36 -05001888 int retval = 1;
srs5694fed16d02010-01-27 23:03:40 -05001889
srs5694699941e2011-03-21 21:33:57 -04001890 if (IsUsedPartNum(partNum))
srs5694fed16d02010-01-27 23:03:40 -05001891 partitions[partNum].SetName(theName);
srs5694699941e2011-03-21 21:33:57 -04001892 else
1893 retval = 0;
srs5694ba00fed2010-01-12 18:18:36 -05001894
1895 return retval;
srs5694e4ac11e2009-08-31 10:13:04 -04001896} // GPTData::SetName
1897
1898// Set the disk GUID to the specified value. Note that the header CRCs must
1899// be recomputed after calling this function.
1900void GPTData::SetDiskGUID(GUIDData newGUID) {
1901 mainHeader.diskGUID = newGUID;
1902 secondHeader.diskGUID = newGUID;
1903} // SetDiskGUID()
1904
1905// Set the unique GUID of the specified partition. Returns 1 on
1906// successful completion, 0 if there were problems (invalid
1907// partition number).
1908int GPTData::SetPartitionGUID(uint32_t pn, GUIDData theGUID) {
1909 int retval = 0;
1910
srs56940283dae2010-04-28 16:44:34 -04001911 if (pn < numParts) {
srs5694e69e6802012-01-20 22:37:12 -05001912 if (partitions[pn].IsUsed()) {
srs5694e4ac11e2009-08-31 10:13:04 -04001913 partitions[pn].SetUniqueGUID(theGUID);
1914 retval = 1;
1915 } // if
1916 } // if
1917 return retval;
1918} // GPTData::SetPartitionGUID()
1919
srs56949ba54212010-05-18 23:24:02 -04001920// Set new random GUIDs for the disk and all partitions. Intended to be used
1921// after disk cloning or similar operations that don't randomize the GUIDs.
1922void GPTData::RandomizeGUIDs(void) {
1923 uint32_t i;
1924
1925 mainHeader.diskGUID.Randomize();
1926 secondHeader.diskGUID = mainHeader.diskGUID;
1927 for (i = 0; i < numParts; i++)
1928 if (partitions[i].IsUsed())
1929 partitions[i].RandomizeUniqueGUID();
1930} // GPTData::RandomizeGUIDs()
1931
srs5694ba00fed2010-01-12 18:18:36 -05001932// Change partition type code non-interactively. Returns 1 if
1933// successful, 0 if not....
srs5694327129e2010-09-22 01:07:31 -04001934int GPTData::ChangePartType(uint32_t partNum, PartType theGUID) {
1935 int retval = 1;
1936
1937 if (!IsFreePartNum(partNum)) {
1938 partitions[partNum].SetType(theGUID);
1939 } else retval = 0;
1940 return retval;
1941} // GPTData::ChangePartType()
1942
srs56949ba54212010-05-18 23:24:02 -04001943// Recompute the CHS values of all the MBR partitions. Used to reset
1944// CHS values that some BIOSes require, despite the fact that the
1945// resulting CHS values violate the GPT standard.
1946void GPTData::RecomputeCHS(void) {
1947 int i;
1948
1949 for (i = 0; i < 4; i++)
1950 protectiveMBR.RecomputeCHS(i);
1951} // GPTData::RecomputeCHS()
1952
srs56941d1448a2009-12-31 21:20:19 -05001953// Adjust sector number so that it falls on a sector boundary that's a
1954// multiple of sectorAlignment. This is done to improve the performance
1955// of Western Digital Advanced Format disks and disks with similar
1956// technology from other companies, which use 4096-byte sectors
1957// internally although they translate to 512-byte sectors for the
1958// benefit of the OS. If partitions aren't properly aligned on these
1959// disks, some filesystem data structures can span multiple physical
1960// sectors, degrading performance. This function should be called
1961// only on the FIRST sector of the partition, not the last!
1962// This function returns 1 if the alignment was altered, 0 if it
1963// was unchanged.
1964int GPTData::Align(uint64_t* sector) {
1965 int retval = 0, sectorOK = 0;
srs569400b6d7a2011-06-26 22:40:06 -04001966 uint64_t earlier, later, testSector;
srs56941d1448a2009-12-31 21:20:19 -05001967
1968 if ((*sector % sectorAlignment) != 0) {
srs56941d1448a2009-12-31 21:20:19 -05001969 earlier = (*sector / sectorAlignment) * sectorAlignment;
1970 later = earlier + (uint64_t) sectorAlignment;
1971
1972 // Check to see that every sector between the earlier one and the
1973 // requested one is clear, and that it's not too early....
1974 if (earlier >= mainHeader.firstUsableLBA) {
srs56941d1448a2009-12-31 21:20:19 -05001975 sectorOK = 1;
1976 testSector = earlier;
1977 do {
1978 sectorOK = IsFree(testSector++);
1979 } while ((sectorOK == 1) && (testSector < *sector));
1980 if (sectorOK == 1) {
1981 *sector = earlier;
srs56945a081752010-09-24 20:39:41 -04001982 retval = 1;
srs56941d1448a2009-12-31 21:20:19 -05001983 } // if
1984 } // if firstUsableLBA check
1985
1986 // If couldn't move the sector earlier, try to move it later instead....
1987 if ((sectorOK != 1) && (later <= mainHeader.lastUsableLBA)) {
1988 sectorOK = 1;
1989 testSector = later;
1990 do {
1991 sectorOK = IsFree(testSector--);
1992 } while ((sectorOK == 1) && (testSector > *sector));
1993 if (sectorOK == 1) {
1994 *sector = later;
srs56945a081752010-09-24 20:39:41 -04001995 retval = 1;
srs56941d1448a2009-12-31 21:20:19 -05001996 } // if
1997 } // if
srs56941d1448a2009-12-31 21:20:19 -05001998 } // if
1999 return retval;
2000} // GPTData::Align()
2001
srs5694e4ac11e2009-08-31 10:13:04 -04002002/********************************************************
2003 * *
2004 * Functions that return data about GPT data structures *
2005 * (most of these are inline in gpt.h) *
2006 * *
2007 ********************************************************/
2008
2009// Find the low and high used partition numbers (numbered from 0).
2010// Return value is the number of partitions found. Note that the
2011// *low and *high values are both set to 0 when no partitions
2012// are found, as well as when a single partition in the first
2013// position exists. Thus, the return value is the only way to
2014// tell when no partitions exist.
2015int GPTData::GetPartRange(uint32_t *low, uint32_t *high) {
2016 uint32_t i;
2017 int numFound = 0;
2018
srs56940283dae2010-04-28 16:44:34 -04002019 *low = numParts + 1; // code for "not found"
srs5694e4ac11e2009-08-31 10:13:04 -04002020 *high = 0;
srs56949a46b042011-03-15 00:34:10 -04002021 for (i = 0; i < numParts; i++) {
srs5694e69e6802012-01-20 22:37:12 -05002022 if (partitions[i].IsUsed()) { // it exists
srs56949a46b042011-03-15 00:34:10 -04002023 *high = i; // since we're counting up, set the high value
2024 // Set the low value only if it's not yet found...
2025 if (*low == (numParts + 1)) *low = i;
2026 numFound++;
2027 } // if
2028 } // for
srs5694e4ac11e2009-08-31 10:13:04 -04002029
2030 // Above will leave *low pointing to its "not found" value if no partitions
2031 // are defined, so reset to 0 if this is the case....
srs56940283dae2010-04-28 16:44:34 -04002032 if (*low == (numParts + 1))
srs5694e4ac11e2009-08-31 10:13:04 -04002033 *low = 0;
2034 return numFound;
2035} // GPTData::GetPartRange()
2036
srs569408bb0da2010-02-19 17:19:55 -05002037// Returns the value of the first free partition, or -1 if none is
2038// unused.
2039int GPTData::FindFirstFreePart(void) {
2040 int i = 0;
2041
2042 if (partitions != NULL) {
srs56949a46b042011-03-15 00:34:10 -04002043 while ((i < (int) numParts) && (partitions[i].IsUsed()))
srs569408bb0da2010-02-19 17:19:55 -05002044 i++;
srs56940283dae2010-04-28 16:44:34 -04002045 if (i >= (int) numParts)
srs569408bb0da2010-02-19 17:19:55 -05002046 i = -1;
2047 } else i = -1;
2048 return i;
2049} // GPTData::FindFirstFreePart()
2050
srs5694978041c2009-09-21 20:51:47 -04002051// Returns the number of defined partitions.
2052uint32_t GPTData::CountParts(void) {
srs5694e321d442010-01-29 17:44:04 -05002053 uint32_t i, counted = 0;
srs5694978041c2009-09-21 20:51:47 -04002054
srs56940283dae2010-04-28 16:44:34 -04002055 for (i = 0; i < numParts; i++) {
srs569408bb0da2010-02-19 17:19:55 -05002056 if (partitions[i].IsUsed())
srs5694978041c2009-09-21 20:51:47 -04002057 counted++;
2058 } // for
2059 return counted;
2060} // GPTData::CountParts()
2061
srs5694e4ac11e2009-08-31 10:13:04 -04002062/****************************************************
2063 * *
2064 * Functions that return data about disk free space *
2065 * *
2066 ****************************************************/
2067
2068// Find the first available block after the starting point; returns 0 if
2069// there are no available blocks left
2070uint64_t GPTData::FindFirstAvailable(uint64_t start) {
2071 uint64_t first;
2072 uint32_t i;
2073 int firstMoved = 0;
2074
2075 // Begin from the specified starting point or from the first usable
2076 // LBA, whichever is greater...
2077 if (start < mainHeader.firstUsableLBA)
2078 first = mainHeader.firstUsableLBA;
2079 else
2080 first = start;
2081
2082 // ...now search through all partitions; if first is within an
2083 // existing partition, move it to the next sector after that
2084 // partition and repeat. If first was moved, set firstMoved
2085 // flag; repeat until firstMoved is not set, so as to catch
2086 // cases where partitions are out of sequential order....
2087 do {
2088 firstMoved = 0;
srs56940283dae2010-04-28 16:44:34 -04002089 for (i = 0; i < numParts; i++) {
srs5694e69e6802012-01-20 22:37:12 -05002090 if ((partitions[i].IsUsed()) && (first >= partitions[i].GetFirstLBA()) &&
srs569455d92612010-03-07 22:16:07 -05002091 (first <= partitions[i].GetLastLBA())) { // in existing part.
srs5694e4ac11e2009-08-31 10:13:04 -04002092 first = partitions[i].GetLastLBA() + 1;
2093 firstMoved = 1;
srs569455d92612010-03-07 22:16:07 -05002094 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002095 } // for
2096 } while (firstMoved == 1);
2097 if (first > mainHeader.lastUsableLBA)
2098 first = 0;
2099 return (first);
2100} // GPTData::FindFirstAvailable()
2101
2102// Finds the first available sector in the largest block of unallocated
2103// space on the disk. Returns 0 if there are no available blocks left
2104uint64_t GPTData::FindFirstInLargest(void) {
srs5694e35eb1b2009-09-14 00:29:34 -04002105 uint64_t start, firstBlock, lastBlock, segmentSize, selectedSize = 0, selectedSegment = 0;
srs5694e4ac11e2009-08-31 10:13:04 -04002106
2107 start = 0;
2108 do {
2109 firstBlock = FindFirstAvailable(start);
2110 if (firstBlock != UINT32_C(0)) { // something's free...
2111 lastBlock = FindLastInFree(firstBlock);
2112 segmentSize = lastBlock - firstBlock + UINT32_C(1);
2113 if (segmentSize > selectedSize) {
2114 selectedSize = segmentSize;
2115 selectedSegment = firstBlock;
2116 } // if
2117 start = lastBlock + 1;
2118 } // if
2119 } while (firstBlock != 0);
2120 return selectedSegment;
2121} // GPTData::FindFirstInLargest()
2122
srs5694cb76c672010-02-11 22:22:22 -05002123// Find the last available block on the disk.
srs5694f5dfbfa2013-02-14 20:47:14 -05002124// Returns 0 if there are no available sectors
srs5694cb76c672010-02-11 22:22:22 -05002125uint64_t GPTData::FindLastAvailable(void) {
srs5694e4ac11e2009-08-31 10:13:04 -04002126 uint64_t last;
2127 uint32_t i;
2128 int lastMoved = 0;
2129
2130 // Start by assuming the last usable LBA is available....
2131 last = mainHeader.lastUsableLBA;
2132
2133 // ...now, similar to algorithm in FindFirstAvailable(), search
2134 // through all partitions, moving last when it's in an existing
2135 // partition. Set the lastMoved flag so we repeat to catch cases
2136 // where partitions are out of logical order.
2137 do {
2138 lastMoved = 0;
srs56940283dae2010-04-28 16:44:34 -04002139 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04002140 if ((last >= partitions[i].GetFirstLBA()) &&
srs569455d92612010-03-07 22:16:07 -05002141 (last <= partitions[i].GetLastLBA())) { // in existing part.
srs5694e4ac11e2009-08-31 10:13:04 -04002142 last = partitions[i].GetFirstLBA() - 1;
2143 lastMoved = 1;
srs569455d92612010-03-07 22:16:07 -05002144 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002145 } // for
2146 } while (lastMoved == 1);
2147 if (last < mainHeader.firstUsableLBA)
2148 last = 0;
2149 return (last);
2150} // GPTData::FindLastAvailable()
2151
2152// Find the last available block in the free space pointed to by start.
2153uint64_t GPTData::FindLastInFree(uint64_t start) {
2154 uint64_t nearestStart;
2155 uint32_t i;
2156
2157 nearestStart = mainHeader.lastUsableLBA;
srs56940283dae2010-04-28 16:44:34 -04002158 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04002159 if ((nearestStart > partitions[i].GetFirstLBA()) &&
srs569455d92612010-03-07 22:16:07 -05002160 (partitions[i].GetFirstLBA() > start)) {
srs5694e4ac11e2009-08-31 10:13:04 -04002161 nearestStart = partitions[i].GetFirstLBA() - 1;
srs569455d92612010-03-07 22:16:07 -05002162 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002163 } // for
2164 return (nearestStart);
2165} // GPTData::FindLastInFree()
2166
2167// Finds the total number of free blocks, the number of segments in which
2168// they reside, and the size of the largest of those segments
srs5694e321d442010-01-29 17:44:04 -05002169uint64_t GPTData::FindFreeBlocks(uint32_t *numSegments, uint64_t *largestSegment) {
srs5694e4ac11e2009-08-31 10:13:04 -04002170 uint64_t start = UINT64_C(0); // starting point for each search
2171 uint64_t totalFound = UINT64_C(0); // running total
2172 uint64_t firstBlock; // first block in a segment
2173 uint64_t lastBlock; // last block in a segment
2174 uint64_t segmentSize; // size of segment in blocks
srs5694e321d442010-01-29 17:44:04 -05002175 uint32_t num = 0;
srs5694e4ac11e2009-08-31 10:13:04 -04002176
2177 *largestSegment = UINT64_C(0);
srs5694c54e9b42010-05-01 21:04:23 -04002178 if (diskSize > 0) {
2179 do {
2180 firstBlock = FindFirstAvailable(start);
2181 if (firstBlock != UINT64_C(0)) { // something's free...
2182 lastBlock = FindLastInFree(firstBlock);
2183 segmentSize = lastBlock - firstBlock + UINT64_C(1);
2184 if (segmentSize > *largestSegment) {
2185 *largestSegment = segmentSize;
2186 } // if
2187 totalFound += segmentSize;
2188 num++;
2189 start = lastBlock + 1;
srs5694e4ac11e2009-08-31 10:13:04 -04002190 } // if
srs5694c54e9b42010-05-01 21:04:23 -04002191 } while (firstBlock != 0);
2192 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002193 *numSegments = num;
2194 return totalFound;
2195} // GPTData::FindFreeBlocks()
2196
srs569455d92612010-03-07 22:16:07 -05002197// Returns 1 if sector is unallocated, 0 if it's allocated to a partition.
2198// If it's allocated, return the partition number to which it's allocated
2199// in partNum, if that variable is non-NULL. (A value of UINT32_MAX is
2200// returned in partNum if the sector is in use by basic GPT data structures.)
2201int GPTData::IsFree(uint64_t sector, uint32_t *partNum) {
srs5694e4ac11e2009-08-31 10:13:04 -04002202 int isFree = 1;
2203 uint32_t i;
2204
srs56940283dae2010-04-28 16:44:34 -04002205 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04002206 if ((sector >= partitions[i].GetFirstLBA()) &&
2207 (sector <= partitions[i].GetLastLBA())) {
2208 isFree = 0;
srs569455d92612010-03-07 22:16:07 -05002209 if (partNum != NULL)
2210 *partNum = i;
srs569408bb0da2010-02-19 17:19:55 -05002211 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002212 } // for
srs5694e35eb1b2009-09-14 00:29:34 -04002213 if ((sector < mainHeader.firstUsableLBA) ||
srs5694e4ac11e2009-08-31 10:13:04 -04002214 (sector > mainHeader.lastUsableLBA)) {
2215 isFree = 0;
srs569455d92612010-03-07 22:16:07 -05002216 if (partNum != NULL)
2217 *partNum = UINT32_MAX;
srs569408bb0da2010-02-19 17:19:55 -05002218 } // if
2219 return (isFree);
srs5694e4ac11e2009-08-31 10:13:04 -04002220} // GPTData::IsFree()
2221
srs5694815fb652011-03-18 12:35:56 -04002222// Returns 1 if partNum is unused AND if it's a legal value.
srs5694ba00fed2010-01-12 18:18:36 -05002223int GPTData::IsFreePartNum(uint32_t partNum) {
srs569401f7f082011-03-15 23:53:31 -04002224 return ((partNum < numParts) && (partitions != NULL) &&
2225 (!partitions[partNum].IsUsed()));
srs5694ba00fed2010-01-12 18:18:36 -05002226} // GPTData::IsFreePartNum()
2227
srs5694815fb652011-03-18 12:35:56 -04002228// Returns 1 if partNum is in use.
2229int GPTData::IsUsedPartNum(uint32_t partNum) {
2230 return ((partNum < numParts) && (partitions != NULL) &&
2231 (partitions[partNum].IsUsed()));
2232} // GPTData::IsUsedPartNum()
srs5694a8582cf2010-03-19 14:21:59 -04002233
2234/***********************************************************
2235 * *
2236 * Change how functions work or return information on them *
2237 * *
2238 ***********************************************************/
2239
2240// Set partition alignment value; partitions will begin on multiples of
2241// the specified value
2242void GPTData::SetAlignment(uint32_t n) {
srs56940873e9d2010-10-07 13:00:45 -04002243 if (n > 0)
2244 sectorAlignment = n;
2245 else
2246 cerr << "Attempt to set partition alignment to 0!\n";
srs5694a8582cf2010-03-19 14:21:59 -04002247} // GPTData::SetAlignment()
2248
2249// Compute sector alignment based on the current partitions (if any). Each
2250// partition's starting LBA is examined, and if it's divisible by a power-of-2
srs56940873e9d2010-10-07 13:00:45 -04002251// value less than or equal to the DEFAULT_ALIGNMENT value (adjusted for the
2252// sector size), but not by the previously-located alignment value, then the
2253// alignment value is adjusted down. If the computed alignment is less than 8
2254// and the disk is bigger than SMALLEST_ADVANCED_FORMAT, resets it to 8. This
srs5694d8eed462012-12-15 01:55:21 -05002255// is a safety measure for Advanced Format drives. If no partitions are
2256// defined, the alignment value is set to DEFAULT_ALIGNMENT (2048) (or an
srs56940873e9d2010-10-07 13:00:45 -04002257// adjustment of that based on the current sector size). The result is that new
srs56948a4ddfc2010-03-21 19:05:49 -04002258// drives are aligned to 2048-sector multiples but the program won't complain
2259// about other alignments on existing disks unless a smaller-than-8 alignment
srs5694d8eed462012-12-15 01:55:21 -05002260// is used on big disks (as safety for Advanced Format drives).
srs5694a8582cf2010-03-19 14:21:59 -04002261// Returns the computed alignment value.
2262uint32_t GPTData::ComputeAlignment(void) {
2263 uint32_t i = 0, found, exponent = 31;
srs5694ab4b0432010-09-25 20:39:52 -04002264 uint32_t align = DEFAULT_ALIGNMENT;
srs5694a8582cf2010-03-19 14:21:59 -04002265
srs56940873e9d2010-10-07 13:00:45 -04002266 if (blockSize > 0)
2267 align = DEFAULT_ALIGNMENT * SECTOR_SIZE / blockSize;
2268 exponent = (uint32_t) log2(align);
srs56940283dae2010-04-28 16:44:34 -04002269 for (i = 0; i < numParts; i++) {
srs5694a8582cf2010-03-19 14:21:59 -04002270 if (partitions[i].IsUsed()) {
2271 found = 0;
2272 while (!found) {
srs56940873e9d2010-10-07 13:00:45 -04002273 align = UINT64_C(1) << exponent;
srs5694a8582cf2010-03-19 14:21:59 -04002274 if ((partitions[i].GetFirstLBA() % align) == 0) {
2275 found = 1;
2276 } else {
2277 exponent--;
2278 } // if/else
2279 } // while
2280 } // if
2281 } // for
srs56940873e9d2010-10-07 13:00:45 -04002282 if ((align < MIN_AF_ALIGNMENT) && (diskSize >= SMALLEST_ADVANCED_FORMAT))
2283 align = MIN_AF_ALIGNMENT;
2284 sectorAlignment = align;
srs5694a8582cf2010-03-19 14:21:59 -04002285 return align;
2286} // GPTData::ComputeAlignment()
2287
srs5694e4ac11e2009-08-31 10:13:04 -04002288/********************************
2289 * *
2290 * Endianness support functions *
2291 * *
2292 ********************************/
2293
srs56942a9f5da2009-08-26 00:48:01 -04002294void GPTData::ReverseHeaderBytes(struct GPTHeader* header) {
srs5694221e0872009-08-29 15:00:31 -04002295 ReverseBytes(&header->signature, 8);
2296 ReverseBytes(&header->revision, 4);
2297 ReverseBytes(&header->headerSize, 4);
2298 ReverseBytes(&header->headerCRC, 4);
2299 ReverseBytes(&header->reserved, 4);
2300 ReverseBytes(&header->currentLBA, 8);
2301 ReverseBytes(&header->backupLBA, 8);
2302 ReverseBytes(&header->firstUsableLBA, 8);
2303 ReverseBytes(&header->lastUsableLBA, 8);
2304 ReverseBytes(&header->partitionEntriesLBA, 8);
2305 ReverseBytes(&header->numParts, 4);
2306 ReverseBytes(&header->sizeOfPartitionEntries, 4);
2307 ReverseBytes(&header->partitionEntriesCRC, 4);
srs569408bb0da2010-02-19 17:19:55 -05002308 ReverseBytes(header->reserved2, GPT_RESERVED);
srs56942a9f5da2009-08-26 00:48:01 -04002309} // GPTData::ReverseHeaderBytes()
2310
srs56940283dae2010-04-28 16:44:34 -04002311// Reverse byte order for all partitions.
srs56942a9f5da2009-08-26 00:48:01 -04002312void GPTData::ReversePartitionBytes() {
2313 uint32_t i;
2314
srs56940283dae2010-04-28 16:44:34 -04002315 for (i = 0; i < numParts; i++) {
srs5694221e0872009-08-29 15:00:31 -04002316 partitions[i].ReversePartBytes();
srs56942a9f5da2009-08-26 00:48:01 -04002317 } // for
2318} // GPTData::ReversePartitionBytes()
2319
srs56949ddc14b2010-08-22 22:44:42 -04002320// Validate partition number
2321bool GPTData::ValidPartNum (const uint32_t partNum) {
2322 if (partNum >= numParts) {
srs56945a081752010-09-24 20:39:41 -04002323 cerr << "Partition number out of range: " << partNum << "\n";
srs56949ddc14b2010-08-22 22:44:42 -04002324 return false;
2325 } // if
2326 return true;
2327} // GPTData::ValidPartNum
2328
srs56945a081752010-09-24 20:39:41 -04002329// Return a single partition for inspection (not modification!) by other
2330// functions.
2331const GPTPart & GPTData::operator[](uint32_t partNum) const {
2332 if (partNum >= numParts) {
srs5694815fb652011-03-18 12:35:56 -04002333 cerr << "Partition number out of range (" << partNum << " requested, but only "
2334 << numParts << " available)\n";
2335 exit(1);
2336 } // if
2337 if (partitions == NULL) {
2338 cerr << "No partitions defined in GPTData::operator[]; fatal error!\n";
2339 exit(1);
srs56945a081752010-09-24 20:39:41 -04002340 } // if
2341 return partitions[partNum];
2342} // operator[]
2343
2344// Return (not for modification!) the disk's GUID value
2345const GUIDData & GPTData::GetDiskGUID(void) const {
2346 return mainHeader.diskGUID;
2347} // GPTData::GetDiskGUID()
2348
srs56949ddc14b2010-08-22 22:44:42 -04002349// Manage attributes for a partition, based on commands passed to this function.
2350// (Function is non-interactive.)
2351// Returns 1 if a modification command succeeded, 0 if the command should not have
2352// modified data, and -1 if a modification command failed.
2353int GPTData::ManageAttributes(int partNum, const string & command, const string & bits) {
2354 int retval = 0;
2355 Attributes theAttr;
2356
Roderick W. Smith24bba6e2013-10-12 19:07:16 -04002357 if (partNum >= (int) numParts) {
2358 cerr << "Invalid partition number (" << partNum + 1 << ")\n";
2359 retval = -1;
srs56949ddc14b2010-08-22 22:44:42 -04002360 } else {
Roderick W. Smith24bba6e2013-10-12 19:07:16 -04002361 if (command == "show") {
2362 ShowAttributes(partNum);
2363 } else if (command == "get") {
2364 GetAttribute(partNum, bits);
srs56949ddc14b2010-08-22 22:44:42 -04002365 } else {
Roderick W. Smith24bba6e2013-10-12 19:07:16 -04002366 theAttr = partitions[partNum].GetAttributes();
2367 if (theAttr.OperateOnAttributes(partNum, command, bits)) {
2368 partitions[partNum].SetAttributes(theAttr.GetAttributes());
2369 retval = 1;
2370 } else {
2371 retval = -1;
2372 } // if/else
2373 } // if/elseif/else
2374 } // if/else invalid partition #
srs56949ddc14b2010-08-22 22:44:42 -04002375
2376 return retval;
2377} // GPTData::ManageAttributes()
2378
2379// Show all attributes for a specified partition....
2380void GPTData::ShowAttributes(const uint32_t partNum) {
Roderick W. Smith24bba6e2013-10-12 19:07:16 -04002381 if ((partNum < numParts) && partitions[partNum].IsUsed())
srs5694e69e6802012-01-20 22:37:12 -05002382 partitions[partNum].ShowAttributes(partNum);
srs56949ddc14b2010-08-22 22:44:42 -04002383} // GPTData::ShowAttributes
2384
2385// Show whether a single attribute bit is set (terse output)...
2386void GPTData::GetAttribute(const uint32_t partNum, const string& attributeBits) {
Roderick W. Smith24bba6e2013-10-12 19:07:16 -04002387 if (partNum < numParts)
2388 partitions[partNum].GetAttributes().OperateOnAttributes(partNum, "get", attributeBits);
srs56949ddc14b2010-08-22 22:44:42 -04002389} // GPTData::GetAttribute
2390
2391
srs56942a9f5da2009-08-26 00:48:01 -04002392/******************************************
2393 * *
2394 * Additional non-class support functions *
2395 * *
2396 ******************************************/
2397
srs5694e7b4ff92009-08-18 13:16:10 -04002398// Check to be sure that data type sizes are correct. The basic types (uint*_t) should
2399// never fail these tests, but the struct types may fail depending on compile options.
2400// Specifically, the -fpack-struct option to gcc may be required to ensure proper structure
2401// sizes.
2402int SizesOK(void) {
2403 int allOK = 1;
srs5694e7b4ff92009-08-18 13:16:10 -04002404
2405 if (sizeof(uint8_t) != 1) {
srs5694fed16d02010-01-27 23:03:40 -05002406 cerr << "uint8_t is " << sizeof(uint8_t) << " bytes, should be 1 byte; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002407 allOK = 0;
2408 } // if
2409 if (sizeof(uint16_t) != 2) {
srs5694fed16d02010-01-27 23:03:40 -05002410 cerr << "uint16_t is " << sizeof(uint16_t) << " bytes, should be 2 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002411 allOK = 0;
2412 } // if
2413 if (sizeof(uint32_t) != 4) {
srs5694fed16d02010-01-27 23:03:40 -05002414 cerr << "uint32_t is " << sizeof(uint32_t) << " bytes, should be 4 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002415 allOK = 0;
2416 } // if
2417 if (sizeof(uint64_t) != 8) {
srs5694fed16d02010-01-27 23:03:40 -05002418 cerr << "uint64_t is " << sizeof(uint64_t) << " bytes, should be 8 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002419 allOK = 0;
2420 } // if
2421 if (sizeof(struct MBRRecord) != 16) {
srs5694fed16d02010-01-27 23:03:40 -05002422 cerr << "MBRRecord is " << sizeof(MBRRecord) << " bytes, should be 16 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002423 allOK = 0;
2424 } // if
srs5694978041c2009-09-21 20:51:47 -04002425 if (sizeof(struct TempMBR) != 512) {
srs5694fed16d02010-01-27 23:03:40 -05002426 cerr << "TempMBR is " << sizeof(TempMBR) << " bytes, should be 512 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002427 allOK = 0;
2428 } // if
2429 if (sizeof(struct GPTHeader) != 512) {
srs5694fed16d02010-01-27 23:03:40 -05002430 cerr << "GPTHeader is " << sizeof(GPTHeader) << " bytes, should be 512 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002431 allOK = 0;
2432 } // if
srs5694221e0872009-08-29 15:00:31 -04002433 if (sizeof(GPTPart) != 128) {
srs5694fed16d02010-01-27 23:03:40 -05002434 cerr << "GPTPart is " << sizeof(GPTPart) << " bytes, should be 128 bytes; aborting!\n";
srs5694221e0872009-08-29 15:00:31 -04002435 allOK = 0;
2436 } // if
srs56946699b012010-02-04 00:55:30 -05002437 if (sizeof(GUIDData) != 16) {
2438 cerr << "GUIDData is " << sizeof(GUIDData) << " bytes, should be 16 bytes; aborting!\n";
2439 allOK = 0;
2440 } // if
2441 if (sizeof(PartType) != 16) {
Roderick W. Smith84aaff62014-02-17 16:17:11 -05002442 cerr << "PartType is " << sizeof(PartType) << " bytes, should be 16 bytes; aborting!\n";
srs56946699b012010-02-04 00:55:30 -05002443 allOK = 0;
2444 } // if
srs5694e7b4ff92009-08-18 13:16:10 -04002445 return (allOK);
2446} // SizesOK()
srs5694e4ac11e2009-08-31 10:13:04 -04002447