blob: 9ad769ab605fb8c6900a087f955b88a6b3af7cb8 [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
Aurimas Liutikasfcad0602016-05-10 19:16:10 -070010#ifndef __STDC_CONSTANT_MACROS
srs5694e7b4ff92009-08-18 13:16:10 -040011#define __STDC_CONSTANT_MACROS
Aurimas Liutikasfcad0602016-05-10 19:16:10 -070012#endif
srs5694e7b4ff92009-08-18 13:16:10 -040013
14#include <stdio.h>
srs5694e7b4ff92009-08-18 13:16:10 -040015#include <stdlib.h>
16#include <stdint.h>
17#include <fcntl.h>
18#include <string.h>
srs5694a8582cf2010-03-19 14:21:59 -040019#include <math.h>
srs5694e7b4ff92009-08-18 13:16:10 -040020#include <time.h>
21#include <sys/stat.h>
22#include <errno.h>
srs5694fed16d02010-01-27 23:03:40 -050023#include <iostream>
srs56949a46b042011-03-15 00:34:10 -040024#include <algorithm>
srs5694e7b4ff92009-08-18 13:16:10 -040025#include "crc32.h"
26#include "gpt.h"
srs5694221e0872009-08-29 15:00:31 -040027#include "bsd.h"
srs5694e7b4ff92009-08-18 13:16:10 -040028#include "support.h"
29#include "parttypes.h"
30#include "attributes.h"
srs5694546a9c72010-01-26 16:00:26 -050031#include "diskio.h"
srs5694e7b4ff92009-08-18 13:16:10 -040032
33using namespace std;
34
srs56948f1b2d62010-05-23 13:07:19 -040035#ifdef __FreeBSD__
srs56949ba54212010-05-18 23:24:02 -040036#define log2(x) (log(x) / M_LN2)
37#endif // __FreeBSD__
38
srs56948f1b2d62010-05-23 13:07:19 -040039#ifdef _MSC_VER
40#define log2(x) (log((double) x) / log(2.0))
41#endif // Microsoft Visual C++
srs56949ba54212010-05-18 23:24:02 -040042
Roderick W. Smith1f7822e2014-03-28 23:54:21 -040043#ifdef EFI
44// in UEFI mode MMX registers are not yet available so using the
45// x86_64 ABI to move "double" values around is not an option.
46#ifdef log2
47#undef log2
48#endif
49#define log2(x) log2_32( x )
50static inline uint32_t log2_32(uint32_t v) {
51 int r = -1;
52 while (v >= 1) {
53 r++;
54 v >>= 1;
55 }
56 return r;
57}
58#endif
59
srs5694e7b4ff92009-08-18 13:16:10 -040060/****************************************
61 * *
62 * GPTData class and related structures *
63 * *
64 ****************************************/
65
srs5694e4ac11e2009-08-31 10:13:04 -040066// Default constructor
srs5694e7b4ff92009-08-18 13:16:10 -040067GPTData::GPTData(void) {
68 blockSize = SECTOR_SIZE; // set a default
69 diskSize = 0;
70 partitions = NULL;
71 state = gpt_valid;
srs5694fed16d02010-01-27 23:03:40 -050072 device = "";
srs56945d58fe02010-01-03 20:57:08 -050073 justLooking = 0;
Greg Hartman2c2deeb2016-04-21 18:20:25 -070074 syncing = 1;
srs5694e7b4ff92009-08-18 13:16:10 -040075 mainCrcOk = 0;
76 secondCrcOk = 0;
77 mainPartsCrcOk = 0;
78 secondPartsCrcOk = 0;
srs5694221e0872009-08-29 15:00:31 -040079 apmFound = 0;
80 bsdFound = 0;
srs56940873e9d2010-10-07 13:00:45 -040081 sectorAlignment = MIN_AF_ALIGNMENT; // Align partitions on 4096-byte boundaries by default
srs5694ba00fed2010-01-12 18:18:36 -050082 beQuiet = 0;
83 whichWasUsed = use_new;
srs56941e093722010-01-05 00:14:19 -050084 mainHeader.numParts = 0;
srs56940283dae2010-04-28 16:44:34 -040085 numParts = 0;
srs5694e7b4ff92009-08-18 13:16:10 -040086 SetGPTSize(NUM_GPT_ENTRIES);
srs5694d1b11e82011-09-18 21:12:28 -040087 // Initialize CRC functions...
88 chksum_crc32gentab();
srs5694e7b4ff92009-08-18 13:16:10 -040089} // GPTData default constructor
90
91// The following constructor loads GPT data from a device file
srs5694fed16d02010-01-27 23:03:40 -050092GPTData::GPTData(string filename) {
srs5694e7b4ff92009-08-18 13:16:10 -040093 blockSize = SECTOR_SIZE; // set a default
94 diskSize = 0;
95 partitions = NULL;
96 state = gpt_invalid;
srs5694fed16d02010-01-27 23:03:40 -050097 device = "";
srs56945d58fe02010-01-03 20:57:08 -050098 justLooking = 0;
Greg Hartman2c2deeb2016-04-21 18:20:25 -070099 syncing = 1;
srs5694e7b4ff92009-08-18 13:16:10 -0400100 mainCrcOk = 0;
101 secondCrcOk = 0;
102 mainPartsCrcOk = 0;
103 secondPartsCrcOk = 0;
srs5694221e0872009-08-29 15:00:31 -0400104 apmFound = 0;
105 bsdFound = 0;
srs56940873e9d2010-10-07 13:00:45 -0400106 sectorAlignment = MIN_AF_ALIGNMENT; // Align partitions on 4096-byte boundaries by default
srs5694ba00fed2010-01-12 18:18:36 -0500107 beQuiet = 0;
108 whichWasUsed = use_new;
srs56941e093722010-01-05 00:14:19 -0500109 mainHeader.numParts = 0;
srs56940283dae2010-04-28 16:44:34 -0400110 numParts = 0;
srs5694d1b11e82011-09-18 21:12:28 -0400111 // Initialize CRC functions...
112 chksum_crc32gentab();
srs56943c0af382010-01-15 19:19:18 -0500113 if (!LoadPartitions(filename))
114 exit(2);
srs5694fed16d02010-01-27 23:03:40 -0500115} // GPTData(string filename) constructor
srs5694e7b4ff92009-08-18 13:16:10 -0400116
srs5694e4ac11e2009-08-31 10:13:04 -0400117// Destructor
srs5694e7b4ff92009-08-18 13:16:10 -0400118GPTData::~GPTData(void) {
srs5694cb76c672010-02-11 22:22:22 -0500119 delete[] partitions;
srs5694e7b4ff92009-08-18 13:16:10 -0400120} // GPTData destructor
121
srs569464cbd172011-03-01 22:03:54 -0500122// Assignment operator
123GPTData & GPTData::operator=(const GPTData & orig) {
124 uint32_t i;
125
126 mainHeader = orig.mainHeader;
127 numParts = orig.numParts;
128 secondHeader = orig.secondHeader;
129 protectiveMBR = orig.protectiveMBR;
130 device = orig.device;
131 blockSize = orig.blockSize;
132 diskSize = orig.diskSize;
133 state = orig.state;
134 justLooking = orig.justLooking;
Greg Hartman2c2deeb2016-04-21 18:20:25 -0700135 syncing = orig.syncing;
srs569464cbd172011-03-01 22:03:54 -0500136 mainCrcOk = orig.mainCrcOk;
137 secondCrcOk = orig.secondCrcOk;
138 mainPartsCrcOk = orig.mainPartsCrcOk;
139 secondPartsCrcOk = orig.secondPartsCrcOk;
140 apmFound = orig.apmFound;
141 bsdFound = orig.bsdFound;
142 sectorAlignment = orig.sectorAlignment;
143 beQuiet = orig.beQuiet;
144 whichWasUsed = orig.whichWasUsed;
145
146 myDisk.OpenForRead(orig.myDisk.GetName());
147
148 delete[] partitions;
srs569401f7f082011-03-15 23:53:31 -0400149 partitions = new GPTPart [numParts];
srs56946aae2a92011-06-10 01:16:51 -0400150 if (partitions == NULL) {
srs569464cbd172011-03-01 22:03:54 -0500151 cerr << "Error! Could not allocate memory for partitions in GPTData::operator=()!\n"
srs56946aae2a92011-06-10 01:16:51 -0400152 << "Terminating!\n";
153 exit(1);
154 } // if
155 for (i = 0; i < numParts; i++) {
156 partitions[i] = orig.partitions[i];
srs5694d1b11e82011-09-18 21:12:28 -0400157 } // for
158
srs569464cbd172011-03-01 22:03:54 -0500159 return *this;
160} // GPTData::operator=()
161
srs5694e4ac11e2009-08-31 10:13:04 -0400162/*********************************************************************
163 * *
164 * Begin functions that verify data, or that adjust the verification *
165 * information (compute CRCs, rebuild headers) *
166 * *
167 *********************************************************************/
srs5694e7b4ff92009-08-18 13:16:10 -0400168
srs5694e4ac11e2009-08-31 10:13:04 -0400169// Perform detailed verification, reporting on any problems found, but
170// do *NOT* recover from these problems. Returns the total number of
171// problems identified.
172int GPTData::Verify(void) {
srs569464cbd172011-03-01 22:03:54 -0500173 int problems = 0, alignProbs = 0;
srs5694e321d442010-01-29 17:44:04 -0500174 uint32_t i, numSegments;
175 uint64_t totalFree, largestSegment;
srs5694e4ac11e2009-08-31 10:13:04 -0400176
177 // First, check for CRC errors in the GPT data....
178 if (!mainCrcOk) {
179 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500180 cout << "\nProblem: The CRC for the main GPT header is invalid. The main GPT header may\n"
181 << "be corrupt. Consider loading the backup GPT header to rebuild the main GPT\n"
182 << "header ('b' on the recovery & transformation menu). This report may be a false\n"
183 << "alarm if you've already corrected other problems.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400184 } // if
185 if (!mainPartsCrcOk) {
186 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500187 cout << "\nProblem: The CRC for the main partition table is invalid. This table may be\n"
188 << "corrupt. Consider loading the backup partition table ('c' on the recovery &\n"
189 << "transformation menu). This report may be a false alarm if you've already\n"
190 << "corrected other problems.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400191 } // if
192 if (!secondCrcOk) {
193 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500194 cout << "\nProblem: The CRC for the backup GPT header is invalid. The backup GPT header\n"
195 << "may be corrupt. Consider using the main GPT header to rebuild the backup GPT\n"
196 << "header ('d' on the recovery & transformation menu). This report may be a false\n"
197 << "alarm if you've already corrected other problems.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400198 } // if
199 if (!secondPartsCrcOk) {
200 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500201 cout << "\nCaution: The CRC for the backup partition table is invalid. This table may\n"
202 << "be corrupt. This program will automatically create a new backup partition\n"
203 << "table when you save your partitions.\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400204 } // if
205
srs5694978041c2009-09-21 20:51:47 -0400206 // Now check that the main and backup headers both point to themselves....
207 if (mainHeader.currentLBA != 1) {
208 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500209 cout << "\nProblem: The main header's self-pointer doesn't point to itself. This problem\n"
210 << "is being automatically corrected, but it may be a symptom of more serious\n"
211 << "problems. Think carefully before saving changes with 'w' or using this disk.\n";
srs5694978041c2009-09-21 20:51:47 -0400212 mainHeader.currentLBA = 1;
213 } // if
214 if (secondHeader.currentLBA != (diskSize - UINT64_C(1))) {
215 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500216 cout << "\nProblem: The secondary header's self-pointer indicates that it doesn't reside\n"
217 << "at the end of the disk. If you've added a disk to a RAID array, use the 'e'\n"
218 << "option on the experts' menu to adjust the secondary header's and partition\n"
219 << "table's locations.\n";
srs5694978041c2009-09-21 20:51:47 -0400220 } // if
221
222 // Now check that critical main and backup GPT entries match each other
srs5694e4ac11e2009-08-31 10:13:04 -0400223 if (mainHeader.currentLBA != secondHeader.backupLBA) {
224 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500225 cout << "\nProblem: main GPT header's current LBA pointer (" << mainHeader.currentLBA
226 << ") doesn't\nmatch the backup GPT header's alternate LBA pointer("
227 << secondHeader.backupLBA << ").\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400228 } // if
229 if (mainHeader.backupLBA != secondHeader.currentLBA) {
230 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500231 cout << "\nProblem: main GPT header's backup LBA pointer (" << mainHeader.backupLBA
232 << ") doesn't\nmatch the backup GPT header's current LBA pointer ("
233 << secondHeader.currentLBA << ").\n"
234 << "The 'e' option on the experts' menu may fix this problem.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400235 } // if
236 if (mainHeader.firstUsableLBA != secondHeader.firstUsableLBA) {
237 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500238 cout << "\nProblem: main GPT header's first usable LBA pointer (" << mainHeader.firstUsableLBA
239 << ") doesn't\nmatch the backup GPT header's first usable LBA pointer ("
240 << secondHeader.firstUsableLBA << ")\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400241 } // if
242 if (mainHeader.lastUsableLBA != secondHeader.lastUsableLBA) {
243 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500244 cout << "\nProblem: main GPT header's last usable LBA pointer (" << mainHeader.lastUsableLBA
245 << ") doesn't\nmatch the backup GPT header's last usable LBA pointer ("
246 << secondHeader.lastUsableLBA << ")\n"
247 << "The 'e' option on the experts' menu can probably fix this problem.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400248 } // if
srs56946699b012010-02-04 00:55:30 -0500249 if ((mainHeader.diskGUID != secondHeader.diskGUID)) {
srs5694e4ac11e2009-08-31 10:13:04 -0400250 problems++;
srs56945a081752010-09-24 20:39:41 -0400251 cout << "\nProblem: main header's disk GUID (" << mainHeader.diskGUID
srs5694fed16d02010-01-27 23:03:40 -0500252 << ") doesn't\nmatch the backup GPT header's disk GUID ("
srs56945a081752010-09-24 20:39:41 -0400253 << secondHeader.diskGUID << ")\n"
srs5694fed16d02010-01-27 23:03:40 -0500254 << "You should use the 'b' or 'd' option on the recovery & transformation menu to\n"
255 << "select one or the other header.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400256 } // if
257 if (mainHeader.numParts != secondHeader.numParts) {
258 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500259 cout << "\nProblem: main GPT header's number of partitions (" << mainHeader.numParts
260 << ") doesn't\nmatch the backup GPT header's number of partitions ("
261 << secondHeader.numParts << ")\n"
262 << "Resizing the partition table ('s' on the experts' menu) may help.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400263 } // if
264 if (mainHeader.sizeOfPartitionEntries != secondHeader.sizeOfPartitionEntries) {
265 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500266 cout << "\nProblem: main GPT header's size of partition entries ("
267 << mainHeader.sizeOfPartitionEntries << ") doesn't\n"
268 << "match the backup GPT header's size of partition entries ("
269 << secondHeader.sizeOfPartitionEntries << ")\n"
270 << "You should use the 'b' or 'd' option on the recovery & transformation menu to\n"
271 << "select one or the other header.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400272 } // if
273
274 // Now check for a few other miscellaneous problems...
275 // Check that the disk size will hold the data...
srs569464cbd172011-03-01 22:03:54 -0500276 if (mainHeader.backupLBA >= diskSize) {
srs5694e4ac11e2009-08-31 10:13:04 -0400277 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500278 cout << "\nProblem: Disk is too small to hold all the data!\n"
279 << "(Disk size is " << diskSize << " sectors, needs to be "
srs569464cbd172011-03-01 22:03:54 -0500280 << mainHeader.backupLBA + UINT64_C(1) << " sectors.)\n"
srs5694fed16d02010-01-27 23:03:40 -0500281 << "The 'e' option on the experts' menu may fix this problem.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400282 } // if
283
srs5694d8eed462012-12-15 01:55:21 -0500284 if ((mainHeader.lastUsableLBA >= diskSize) || (mainHeader.lastUsableLBA > mainHeader.backupLBA)) {
285 problems++;
srs56940741fa22013-01-09 12:55:40 -0500286 cout << "\nProblem: GPT claims the disk is larger than it is! (Claimed last usable\n"
287 << "sector is " << mainHeader.lastUsableLBA << ", but backup header is at\n"
288 << mainHeader.backupLBA << " and disk size is " << diskSize << " sectors.\n"
289 << "The 'e' option on the experts' menu will probably fix this problem\n";
srs5694d8eed462012-12-15 01:55:21 -0500290 }
291
srs5694e4ac11e2009-08-31 10:13:04 -0400292 // Check for overlapping partitions....
293 problems += FindOverlaps();
294
srs569455d92612010-03-07 22:16:07 -0500295 // Check for insane partitions (start after end, hugely big, etc.)
296 problems += FindInsanePartitions();
297
srs5694e4ac11e2009-08-31 10:13:04 -0400298 // Check for mismatched MBR and GPT partitions...
299 problems += FindHybridMismatches();
300
srs5694327129e2010-09-22 01:07:31 -0400301 // Check for MBR-specific problems....
302 problems += VerifyMBR();
303
Roderick W. Smith042f38a2013-08-31 17:40:15 -0400304 // Check for a 0xEE protective partition that's marked as active....
305 if (protectiveMBR.IsEEActive()) {
306 cout << "\nWarning: The 0xEE protective partition in the MBR is marked as active. This is\n"
307 << "technically a violation of the GPT specification, and can cause some EFIs to\n"
308 << "ignore the disk, but it is required to boot from a GPT disk on some BIOS-based\n"
309 << "computers. You can clear this flag by creating a fresh protective MBR using\n"
310 << "the 'n' option on the experts' menu.\n";
311 }
312
srs5694e4ac11e2009-08-31 10:13:04 -0400313 // Verify that partitions don't run into GPT data areas....
314 problems += CheckGPTSize();
315
Roderick W. Smith4a702a22014-01-25 23:46:42 -0500316 if (!protectiveMBR.DoTheyFit()) {
317 cout << "\nPartition(s) in the protective MBR are too big for the disk! Creating a\n"
318 << "fresh protective or hybrid MBR is recommended.\n";
319 problems++;
320 }
321
srs56941d1448a2009-12-31 21:20:19 -0500322 // Check that partitions are aligned on proper boundaries (for WD Advanced
323 // Format and similar disks)....
srs56940283dae2010-04-28 16:44:34 -0400324 for (i = 0; i < numParts; i++) {
srs5694e69e6802012-01-20 22:37:12 -0500325 if ((partitions[i].IsUsed()) && (partitions[i].GetFirstLBA() % sectorAlignment) != 0) {
srs5694fed16d02010-01-27 23:03:40 -0500326 cout << "\nCaution: Partition " << i + 1 << " doesn't begin on a "
327 << sectorAlignment << "-sector boundary. This may\nresult "
328 << "in degraded performance on some modern (2009 and later) hard disks.\n";
srs569464cbd172011-03-01 22:03:54 -0500329 alignProbs++;
srs56941d1448a2009-12-31 21:20:19 -0500330 } // if
331 } // for
srs569464cbd172011-03-01 22:03:54 -0500332 if (alignProbs > 0)
333 cout << "\nConsult http://www.ibm.com/developerworks/linux/library/l-4kb-sector-disks/\n"
334 << "for information on disk alignment.\n";
srs56941d1448a2009-12-31 21:20:19 -0500335
srs5694e4ac11e2009-08-31 10:13:04 -0400336 // Now compute available space, but only if no problems found, since
337 // problems could affect the results
338 if (problems == 0) {
339 totalFree = FindFreeBlocks(&numSegments, &largestSegment);
srs569464cbd172011-03-01 22:03:54 -0500340 cout << "\nNo problems found. " << totalFree << " free sectors ("
srs569401f7f082011-03-15 23:53:31 -0400341 << BytesToIeee(totalFree, blockSize) << ") available in "
srs5694fed16d02010-01-27 23:03:40 -0500342 << numSegments << "\nsegments, the largest of which is "
srs569401f7f082011-03-15 23:53:31 -0400343 << largestSegment << " (" << BytesToIeee(largestSegment, blockSize)
srs56940283dae2010-04-28 16:44:34 -0400344 << ") in size.\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400345 } else {
srs56940a697312010-01-28 21:10:52 -0500346 cout << "\nIdentified " << problems << " problems!\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400347 } // if/else
srs5694e4ac11e2009-08-31 10:13:04 -0400348
349 return (problems);
350} // GPTData::Verify()
srs5694e7b4ff92009-08-18 13:16:10 -0400351
352// Checks to see if the GPT tables overrun existing partitions; if they
srs5694221e0872009-08-29 15:00:31 -0400353// do, issues a warning but takes no action. Returns number of problems
354// detected (0 if OK, 1 to 2 if problems).
srs5694e7b4ff92009-08-18 13:16:10 -0400355int GPTData::CheckGPTSize(void) {
356 uint64_t overlap, firstUsedBlock, lastUsedBlock;
357 uint32_t i;
srs5694221e0872009-08-29 15:00:31 -0400358 int numProbs = 0;
srs5694e7b4ff92009-08-18 13:16:10 -0400359
360 // first, locate the first & last used blocks
361 firstUsedBlock = UINT64_MAX;
362 lastUsedBlock = 0;
srs56940283dae2010-04-28 16:44:34 -0400363 for (i = 0; i < numParts; i++) {
srs5694e69e6802012-01-20 22:37:12 -0500364 if (partitions[i].IsUsed()) {
srs5694706e5122012-01-21 13:47:24 -0500365 if (partitions[i].GetFirstLBA() < firstUsedBlock)
srs5694e69e6802012-01-20 22:37:12 -0500366 firstUsedBlock = partitions[i].GetFirstLBA();
367 if (partitions[i].GetLastLBA() > lastUsedBlock) {
368 lastUsedBlock = partitions[i].GetLastLBA();
369 } // if
370 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400371 } // for
372
373 // If the disk size is 0 (the default), then it means that various
374 // variables aren't yet set, so the below tests will be useless;
375 // therefore we should skip everything
376 if (diskSize != 0) {
377 if (mainHeader.firstUsableLBA > firstUsedBlock) {
378 overlap = mainHeader.firstUsableLBA - firstUsedBlock;
srs5694fed16d02010-01-27 23:03:40 -0500379 cout << "Warning! Main partition table overlaps the first partition by "
380 << overlap << " blocks!\n";
srs5694221e0872009-08-29 15:00:31 -0400381 if (firstUsedBlock > 2) {
srs5694fed16d02010-01-27 23:03:40 -0500382 cout << "Try reducing the partition table size by " << overlap * 4
383 << " entries.\n(Use the 's' item on the experts' menu.)\n";
srs5694221e0872009-08-29 15:00:31 -0400384 } else {
srs5694fed16d02010-01-27 23:03:40 -0500385 cout << "You will need to delete this partition or resize it in another utility.\n";
srs5694221e0872009-08-29 15:00:31 -0400386 } // if/else
387 numProbs++;
srs5694e7b4ff92009-08-18 13:16:10 -0400388 } // Problem at start of disk
389 if (mainHeader.lastUsableLBA < lastUsedBlock) {
390 overlap = lastUsedBlock - mainHeader.lastUsableLBA;
srs569455d92612010-03-07 22:16:07 -0500391 cout << "\nWarning! Secondary partition table overlaps the last partition by\n"
srs5694fed16d02010-01-27 23:03:40 -0500392 << overlap << " blocks!\n";
srs5694221e0872009-08-29 15:00:31 -0400393 if (lastUsedBlock > (diskSize - 2)) {
srs5694fed16d02010-01-27 23:03:40 -0500394 cout << "You will need to delete this partition or resize it in another utility.\n";
srs5694221e0872009-08-29 15:00:31 -0400395 } else {
srs5694fed16d02010-01-27 23:03:40 -0500396 cout << "Try reducing the partition table size by " << overlap * 4
397 << " entries.\n(Use the 's' item on the experts' menu.)\n";
srs5694221e0872009-08-29 15:00:31 -0400398 } // if/else
399 numProbs++;
srs5694e7b4ff92009-08-18 13:16:10 -0400400 } // Problem at end of disk
401 } // if (diskSize != 0)
srs5694221e0872009-08-29 15:00:31 -0400402 return numProbs;
srs5694e7b4ff92009-08-18 13:16:10 -0400403} // GPTData::CheckGPTSize()
404
srs5694e7b4ff92009-08-18 13:16:10 -0400405// Check the validity of the GPT header. Returns 1 if the main header
406// is valid, 2 if the backup header is valid, 3 if both are valid, and
srs5694d1b11e82011-09-18 21:12:28 -0400407// 0 if neither is valid. Note that this function checks the GPT signature,
408// revision value, and CRCs in both headers.
srs5694e7b4ff92009-08-18 13:16:10 -0400409int GPTData::CheckHeaderValidity(void) {
410 int valid = 3;
411
srs5694fed16d02010-01-27 23:03:40 -0500412 cout.setf(ios::uppercase);
413 cout.fill('0');
414
415 // Note: failed GPT signature checks produce no error message because
416 // a message is displayed in the ReversePartitionBytes() function
srs5694d1b11e82011-09-18 21:12:28 -0400417 if ((mainHeader.signature != GPT_SIGNATURE) || (!CheckHeaderCRC(&mainHeader, 1))) {
srs5694e7b4ff92009-08-18 13:16:10 -0400418 valid -= 1;
srs5694e7b4ff92009-08-18 13:16:10 -0400419 } else if ((mainHeader.revision != 0x00010000) && valid) {
420 valid -= 1;
srs5694fed16d02010-01-27 23:03:40 -0500421 cout << "Unsupported GPT version in main header; read 0x";
422 cout.width(8);
423 cout << hex << mainHeader.revision << ", should be\n0x";
424 cout.width(8);
425 cout << UINT32_C(0x00010000) << dec << "\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400426 } // if/else/if
427
srs5694d1b11e82011-09-18 21:12:28 -0400428 if ((secondHeader.signature != GPT_SIGNATURE) || (!CheckHeaderCRC(&secondHeader))) {
srs5694e7b4ff92009-08-18 13:16:10 -0400429 valid -= 2;
srs5694e7b4ff92009-08-18 13:16:10 -0400430 } else if ((secondHeader.revision != 0x00010000) && valid) {
431 valid -= 2;
srs5694fed16d02010-01-27 23:03:40 -0500432 cout << "Unsupported GPT version in backup header; read 0x";
433 cout.width(8);
434 cout << hex << secondHeader.revision << ", should be\n0x";
435 cout.width(8);
436 cout << UINT32_C(0x00010000) << dec << "\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400437 } // if/else/if
438
srs5694df9d3632011-01-08 18:33:24 -0500439 // Check for an Apple disk signature
440 if (((mainHeader.signature << 32) == APM_SIGNATURE1) ||
441 (mainHeader.signature << 32) == APM_SIGNATURE2) {
srs5694221e0872009-08-29 15:00:31 -0400442 apmFound = 1; // Will display warning message later
srs56943f2fe992009-11-24 18:28:18 -0500443 } // if
srs5694fed16d02010-01-27 23:03:40 -0500444 cout.fill(' ');
srs56942a9f5da2009-08-26 00:48:01 -0400445
srs5694fed16d02010-01-27 23:03:40 -0500446 return valid;
srs5694e7b4ff92009-08-18 13:16:10 -0400447} // GPTData::CheckHeaderValidity()
448
449// Check the header CRC to see if it's OK...
srs5694d1b11e82011-09-18 21:12:28 -0400450// Note: Must be called with header in platform-ordered byte order.
451// Returns 1 if header's computed CRC matches the stored value, 0 if the
452// computed and stored values don't match
453int GPTData::CheckHeaderCRC(struct GPTHeader* header, int warn) {
srs5694978041c2009-09-21 20:51:47 -0400454 uint32_t oldCRC, newCRC, hSize;
srs5694d1b11e82011-09-18 21:12:28 -0400455 uint8_t *temp;
srs5694e7b4ff92009-08-18 13:16:10 -0400456
srs56942a9f5da2009-08-26 00:48:01 -0400457 // Back up old header CRC and then blank it, since it must be 0 for
srs5694e7b4ff92009-08-18 13:16:10 -0400458 // computation to be valid
459 oldCRC = header->headerCRC;
460 header->headerCRC = UINT32_C(0);
srs5694d1b11e82011-09-18 21:12:28 -0400461
srs5694978041c2009-09-21 20:51:47 -0400462 hSize = header->headerSize;
463
srs5694d1b11e82011-09-18 21:12:28 -0400464 if (IsLittleEndian() == 0)
465 ReverseHeaderBytes(header);
srs5694e7b4ff92009-08-18 13:16:10 -0400466
srs5694d1b11e82011-09-18 21:12:28 -0400467 if ((hSize > blockSize) || (hSize < HEADER_SIZE)) {
468 if (warn) {
469 cerr << "\aWarning! Header size is specified as " << hSize << ", which is invalid.\n";
470 cerr << "Setting the header size for CRC computation to " << HEADER_SIZE << "\n";
471 } // if
472 hSize = HEADER_SIZE;
473 } else if ((hSize > sizeof(GPTHeader)) && warn) {
474 cout << "\aCaution! Header size for CRC check is " << hSize << ", which is greater than " << sizeof(GPTHeader) << ".\n";
475 cout << "If stray data exists after the header on the header sector, it will be ignored,\n"
476 << "which may result in a CRC false alarm.\n";
477 } // if/elseif
478 temp = new uint8_t[hSize];
479 if (temp != NULL) {
480 memset(temp, 0, hSize);
481 if (hSize < sizeof(GPTHeader))
482 memcpy(temp, header, hSize);
483 else
484 memcpy(temp, header, sizeof(GPTHeader));
srs5694e7b4ff92009-08-18 13:16:10 -0400485
srs5694d1b11e82011-09-18 21:12:28 -0400486 newCRC = chksum_crc32((unsigned char*) temp, hSize);
487 delete[] temp;
488 } else {
489 cerr << "Could not allocate memory in GPTData::CheckHeaderCRC()! Aborting!\n";
490 exit(1);
491 }
492 if (IsLittleEndian() == 0)
493 ReverseHeaderBytes(header);
srs5694978041c2009-09-21 20:51:47 -0400494 header->headerCRC = oldCRC;
srs5694e7b4ff92009-08-18 13:16:10 -0400495 return (oldCRC == newCRC);
496} // GPTData::CheckHeaderCRC()
497
srs56946699b012010-02-04 00:55:30 -0500498// Recompute all the CRCs. Must be called before saving if any changes have
499// been made. Must be called on platform-ordered data (this function reverses
500// byte order and then undoes that reversal.)
srs5694e7b4ff92009-08-18 13:16:10 -0400501void GPTData::RecomputeCRCs(void) {
srs56940283dae2010-04-28 16:44:34 -0400502 uint32_t crc, hSize;
srs56942a9f5da2009-08-26 00:48:01 -0400503 int littleEndian = 1;
srs5694e7b4ff92009-08-18 13:16:10 -0400504
srs5694d1b11e82011-09-18 21:12:28 -0400505 // If the header size is bigger than the GPT header data structure, reset it;
506 // otherwise, set both header sizes to whatever the main one is....
507 if (mainHeader.headerSize > sizeof(GPTHeader))
508 hSize = secondHeader.headerSize = mainHeader.headerSize = HEADER_SIZE;
509 else
510 hSize = secondHeader.headerSize = mainHeader.headerSize;
srs56946699b012010-02-04 00:55:30 -0500511
512 if ((littleEndian = IsLittleEndian()) == 0) {
513 ReversePartitionBytes();
514 ReverseHeaderBytes(&mainHeader);
515 ReverseHeaderBytes(&secondHeader);
516 } // if
srs56942a9f5da2009-08-26 00:48:01 -0400517
srs5694e7b4ff92009-08-18 13:16:10 -0400518 // Compute CRC of partition tables & store in main and secondary headers
srs56940283dae2010-04-28 16:44:34 -0400519 crc = chksum_crc32((unsigned char*) partitions, numParts * GPT_SIZE);
srs5694e7b4ff92009-08-18 13:16:10 -0400520 mainHeader.partitionEntriesCRC = crc;
521 secondHeader.partitionEntriesCRC = crc;
srs56942a9f5da2009-08-26 00:48:01 -0400522 if (littleEndian == 0) {
srs5694221e0872009-08-29 15:00:31 -0400523 ReverseBytes(&mainHeader.partitionEntriesCRC, 4);
524 ReverseBytes(&secondHeader.partitionEntriesCRC, 4);
srs56942a9f5da2009-08-26 00:48:01 -0400525 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400526
srs5694d1b11e82011-09-18 21:12:28 -0400527 // Zero out GPT headers' own CRCs (required for correct computation)
srs5694e7b4ff92009-08-18 13:16:10 -0400528 mainHeader.headerCRC = 0;
529 secondHeader.headerCRC = 0;
530
srs5694978041c2009-09-21 20:51:47 -0400531 crc = chksum_crc32((unsigned char*) &mainHeader, hSize);
srs56942a9f5da2009-08-26 00:48:01 -0400532 if (littleEndian == 0)
srs5694221e0872009-08-29 15:00:31 -0400533 ReverseBytes(&crc, 4);
srs5694e7b4ff92009-08-18 13:16:10 -0400534 mainHeader.headerCRC = crc;
srs5694978041c2009-09-21 20:51:47 -0400535 crc = chksum_crc32((unsigned char*) &secondHeader, hSize);
srs56942a9f5da2009-08-26 00:48:01 -0400536 if (littleEndian == 0)
srs5694221e0872009-08-29 15:00:31 -0400537 ReverseBytes(&crc, 4);
srs5694e7b4ff92009-08-18 13:16:10 -0400538 secondHeader.headerCRC = crc;
srs56946699b012010-02-04 00:55:30 -0500539
srs5694d1b11e82011-09-18 21:12:28 -0400540 if (littleEndian == 0) {
srs56946699b012010-02-04 00:55:30 -0500541 ReverseHeaderBytes(&mainHeader);
542 ReverseHeaderBytes(&secondHeader);
543 ReversePartitionBytes();
544 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400545} // GPTData::RecomputeCRCs()
546
srs5694e7b4ff92009-08-18 13:16:10 -0400547// Rebuild the main GPT header, using the secondary header as a model.
548// Typically called when the main header has been found to be corrupt.
549void GPTData::RebuildMainHeader(void) {
srs5694e7b4ff92009-08-18 13:16:10 -0400550 mainHeader.signature = GPT_SIGNATURE;
551 mainHeader.revision = secondHeader.revision;
srs5694978041c2009-09-21 20:51:47 -0400552 mainHeader.headerSize = secondHeader.headerSize;
srs5694e7b4ff92009-08-18 13:16:10 -0400553 mainHeader.headerCRC = UINT32_C(0);
554 mainHeader.reserved = secondHeader.reserved;
555 mainHeader.currentLBA = secondHeader.backupLBA;
556 mainHeader.backupLBA = secondHeader.currentLBA;
557 mainHeader.firstUsableLBA = secondHeader.firstUsableLBA;
558 mainHeader.lastUsableLBA = secondHeader.lastUsableLBA;
srs56946699b012010-02-04 00:55:30 -0500559 mainHeader.diskGUID = secondHeader.diskGUID;
srs5694e7b4ff92009-08-18 13:16:10 -0400560 mainHeader.partitionEntriesLBA = UINT64_C(2);
561 mainHeader.numParts = secondHeader.numParts;
562 mainHeader.sizeOfPartitionEntries = secondHeader.sizeOfPartitionEntries;
563 mainHeader.partitionEntriesCRC = secondHeader.partitionEntriesCRC;
srs569401f7f082011-03-15 23:53:31 -0400564 memcpy(mainHeader.reserved2, secondHeader.reserved2, sizeof(mainHeader.reserved2));
srs5694546a9c72010-01-26 16:00:26 -0500565 mainCrcOk = secondCrcOk;
srs5694706e5122012-01-21 13:47:24 -0500566 SetGPTSize(mainHeader.numParts, 0);
srs5694e7b4ff92009-08-18 13:16:10 -0400567} // GPTData::RebuildMainHeader()
568
569// Rebuild the secondary GPT header, using the main header as a model.
570void GPTData::RebuildSecondHeader(void) {
srs5694e7b4ff92009-08-18 13:16:10 -0400571 secondHeader.signature = GPT_SIGNATURE;
572 secondHeader.revision = mainHeader.revision;
srs5694978041c2009-09-21 20:51:47 -0400573 secondHeader.headerSize = mainHeader.headerSize;
srs5694e7b4ff92009-08-18 13:16:10 -0400574 secondHeader.headerCRC = UINT32_C(0);
575 secondHeader.reserved = mainHeader.reserved;
576 secondHeader.currentLBA = mainHeader.backupLBA;
577 secondHeader.backupLBA = mainHeader.currentLBA;
578 secondHeader.firstUsableLBA = mainHeader.firstUsableLBA;
579 secondHeader.lastUsableLBA = mainHeader.lastUsableLBA;
srs56946699b012010-02-04 00:55:30 -0500580 secondHeader.diskGUID = mainHeader.diskGUID;
srs5694e7b4ff92009-08-18 13:16:10 -0400581 secondHeader.partitionEntriesLBA = secondHeader.lastUsableLBA + UINT64_C(1);
582 secondHeader.numParts = mainHeader.numParts;
583 secondHeader.sizeOfPartitionEntries = mainHeader.sizeOfPartitionEntries;
584 secondHeader.partitionEntriesCRC = mainHeader.partitionEntriesCRC;
srs569401f7f082011-03-15 23:53:31 -0400585 memcpy(secondHeader.reserved2, mainHeader.reserved2, sizeof(secondHeader.reserved2));
srs5694546a9c72010-01-26 16:00:26 -0500586 secondCrcOk = mainCrcOk;
srs5694706e5122012-01-21 13:47:24 -0500587 SetGPTSize(secondHeader.numParts, 0);
srs5694e4ac11e2009-08-31 10:13:04 -0400588} // GPTData::RebuildSecondHeader()
589
590// Search for hybrid MBR entries that have no corresponding GPT partition.
591// Returns number of such mismatches found
592int GPTData::FindHybridMismatches(void) {
srs5694e321d442010-01-29 17:44:04 -0500593 int i, found, numFound = 0;
594 uint32_t j;
srs5694e4ac11e2009-08-31 10:13:04 -0400595 uint64_t mbrFirst, mbrLast;
596
597 for (i = 0; i < 4; i++) {
598 if ((protectiveMBR.GetType(i) != 0xEE) && (protectiveMBR.GetType(i) != 0x00)) {
599 j = 0;
600 found = 0;
srs5694d1b11e82011-09-18 21:12:28 -0400601 mbrFirst = (uint64_t) protectiveMBR.GetFirstSector(i);
602 mbrLast = mbrFirst + (uint64_t) protectiveMBR.GetLength(i) - UINT64_C(1);
srs5694e4ac11e2009-08-31 10:13:04 -0400603 do {
Roderick W. Smith24bba6e2013-10-12 19:07:16 -0400604 if ((j < numParts) && (partitions[j].GetFirstLBA() == mbrFirst) &&
srs5694e69e6802012-01-20 22:37:12 -0500605 (partitions[j].GetLastLBA() == mbrLast) && (partitions[j].IsUsed()))
srs5694e4ac11e2009-08-31 10:13:04 -0400606 found = 1;
607 j++;
srs56940283dae2010-04-28 16:44:34 -0400608 } while ((!found) && (j < numParts));
srs5694e4ac11e2009-08-31 10:13:04 -0400609 if (!found) {
610 numFound++;
srs5694fed16d02010-01-27 23:03:40 -0500611 cout << "\nWarning! Mismatched GPT and MBR partition! MBR partition "
612 << i + 1 << ", of type 0x";
613 cout.fill('0');
614 cout.setf(ios::uppercase);
615 cout.width(2);
616 cout << hex << (int) protectiveMBR.GetType(i) << ",\n"
617 << "has no corresponding GPT partition! You may continue, but this condition\n"
618 << "might cause data loss in the future!\a\n" << dec;
619 cout.fill(' ');
srs5694e4ac11e2009-08-31 10:13:04 -0400620 } // if
621 } // if
622 } // for
623 return numFound;
624} // GPTData::FindHybridMismatches
625
626// Find overlapping partitions and warn user about them. Returns number of
627// overlapping partitions.
srs5694d1b11e82011-09-18 21:12:28 -0400628// Returns number of overlapping segments found.
srs5694e4ac11e2009-08-31 10:13:04 -0400629int GPTData::FindOverlaps(void) {
srs5694e321d442010-01-29 17:44:04 -0500630 int problems = 0;
631 uint32_t i, j;
srs5694e4ac11e2009-08-31 10:13:04 -0400632
srs56940283dae2010-04-28 16:44:34 -0400633 for (i = 1; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -0400634 for (j = 0; j < i; j++) {
srs5694e69e6802012-01-20 22:37:12 -0500635 if ((partitions[i].IsUsed()) && (partitions[j].IsUsed()) &&
636 (partitions[i].DoTheyOverlap(partitions[j]))) {
srs5694e4ac11e2009-08-31 10:13:04 -0400637 problems++;
srs5694fed16d02010-01-27 23:03:40 -0500638 cout << "\nProblem: partitions " << i + 1 << " and " << j + 1 << " overlap:\n";
639 cout << " Partition " << i + 1 << ": " << partitions[i].GetFirstLBA()
640 << " to " << partitions[i].GetLastLBA() << "\n";
641 cout << " Partition " << j + 1 << ": " << partitions[j].GetFirstLBA()
642 << " to " << partitions[j].GetLastLBA() << "\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400643 } // if
644 } // for j...
645 } // for i...
646 return problems;
647} // GPTData::FindOverlaps()
648
srs569455d92612010-03-07 22:16:07 -0500649// Find partitions that are insane -- they start after they end or are too
650// big for the disk. (The latter should duplicate detection of overlaps
651// with GPT backup data structures, but better to err on the side of
652// redundant tests than to miss something....)
srs5694d1b11e82011-09-18 21:12:28 -0400653// Returns number of problems found.
srs569455d92612010-03-07 22:16:07 -0500654int GPTData::FindInsanePartitions(void) {
655 uint32_t i;
656 int problems = 0;
657
srs56940283dae2010-04-28 16:44:34 -0400658 for (i = 0; i < numParts; i++) {
srs5694e69e6802012-01-20 22:37:12 -0500659 if (partitions[i].IsUsed()) {
660 if (partitions[i].GetFirstLBA() > partitions[i].GetLastLBA()) {
661 problems++;
662 cout << "\nProblem: partition " << i + 1 << " ends before it begins.\n";
663 } // if
664 if (partitions[i].GetLastLBA() >= diskSize) {
665 problems++;
666 cout << "\nProblem: partition " << i + 1 << " is too big for the disk.\n";
667 } // if
srs569455d92612010-03-07 22:16:07 -0500668 } // if
669 } // for
670 return problems;
671} // GPTData::FindInsanePartitions(void)
672
673
srs5694e4ac11e2009-08-31 10:13:04 -0400674/******************************************************************
675 * *
676 * Begin functions that load data from disk or save data to disk. *
677 * *
678 ******************************************************************/
679
srs569464cbd172011-03-01 22:03:54 -0500680// Change the filename associated with the GPT. Used for duplicating
681// the partition table to a new disk and saving backups.
682// Returns 1 on success, 0 on failure.
srs5694bf8950c2011-03-12 01:23:12 -0500683int GPTData::SetDisk(const string & deviceFilename) {
srs569464cbd172011-03-01 22:03:54 -0500684 int err, allOK = 1;
685
686 device = deviceFilename;
687 if (allOK && myDisk.OpenForRead(deviceFilename)) {
688 // store disk information....
689 diskSize = myDisk.DiskSize(&err);
690 blockSize = (uint32_t) myDisk.GetBlockSize();
691 } // if
692 protectiveMBR.SetDisk(&myDisk);
693 protectiveMBR.SetDiskSize(diskSize);
694 protectiveMBR.SetBlockSize(blockSize);
695 return allOK;
srs5694bf8950c2011-03-12 01:23:12 -0500696} // GPTData::SetDisk()
srs569464cbd172011-03-01 22:03:54 -0500697
srs5694e4ac11e2009-08-31 10:13:04 -0400698// Scan for partition data. This function loads the MBR data (regular MBR or
699// protective MBR) and loads BSD disklabel data (which is probably invalid).
700// It also looks for APM data, forces a load of GPT data, and summarizes
701// the results.
srs5694546a9c72010-01-26 16:00:26 -0500702void GPTData::PartitionScan(void) {
srs5694e4ac11e2009-08-31 10:13:04 -0400703 BSDData bsdDisklabel;
srs5694e4ac11e2009-08-31 10:13:04 -0400704
705 // Read the MBR & check for BSD disklabel
srs5694546a9c72010-01-26 16:00:26 -0500706 protectiveMBR.ReadMBRData(&myDisk);
707 bsdDisklabel.ReadBSDData(&myDisk, 0, diskSize - 1);
srs5694e4ac11e2009-08-31 10:13:04 -0400708
709 // Load the GPT data, whether or not it's valid
srs5694546a9c72010-01-26 16:00:26 -0500710 ForceLoadGPTData();
srs5694ba00fed2010-01-12 18:18:36 -0500711
Roderick W. Smith4a702a22014-01-25 23:46:42 -0500712 // Some tools create a 0xEE partition that's too big. If this is detected,
713 // normalize it....
714 if ((state == gpt_valid) && !protectiveMBR.DoTheyFit() && (protectiveMBR.GetValidity() == gpt)) {
715 if (!beQuiet) {
716 cerr << "\aThe protective MBR's 0xEE partition is oversized! Auto-repairing.\n\n";
717 } // if
718 protectiveMBR.MakeProtectiveMBR();
719 } // if
720
srs5694ba00fed2010-01-12 18:18:36 -0500721 if (!beQuiet) {
srs5694fed16d02010-01-27 23:03:40 -0500722 cout << "Partition table scan:\n";
srs5694ba00fed2010-01-12 18:18:36 -0500723 protectiveMBR.ShowState();
724 bsdDisklabel.ShowState();
725 ShowAPMState(); // Show whether there's an Apple Partition Map present
726 ShowGPTState(); // Show GPT status
srs5694fed16d02010-01-27 23:03:40 -0500727 cout << "\n";
srs5694ba00fed2010-01-12 18:18:36 -0500728 } // if
srs5694e4ac11e2009-08-31 10:13:04 -0400729
730 if (apmFound) {
srs5694fed16d02010-01-27 23:03:40 -0500731 cout << "\n*******************************************************************\n"
732 << "This disk appears to contain an Apple-format (APM) partition table!\n";
srs56945d58fe02010-01-03 20:57:08 -0500733 if (!justLooking) {
srs5694fed16d02010-01-27 23:03:40 -0500734 cout << "It will be destroyed if you continue!\n";
srs56945d58fe02010-01-03 20:57:08 -0500735 } // if
srs5694fed16d02010-01-27 23:03:40 -0500736 cout << "*******************************************************************\n\n\a";
srs5694e4ac11e2009-08-31 10:13:04 -0400737 } // if
srs5694e4ac11e2009-08-31 10:13:04 -0400738} // GPTData::PartitionScan()
739
740// Read GPT data from a disk.
srs56940a697312010-01-28 21:10:52 -0500741int GPTData::LoadPartitions(const string & deviceFilename) {
srs569408bb0da2010-02-19 17:19:55 -0500742 BSDData bsdDisklabel;
srs5694e321d442010-01-29 17:44:04 -0500743 int err, allOK = 1;
srs5694fed16d02010-01-27 23:03:40 -0500744 MBRValidity mbrState;
srs5694e4ac11e2009-08-31 10:13:04 -0400745
srs5694546a9c72010-01-26 16:00:26 -0500746 if (myDisk.OpenForRead(deviceFilename)) {
srs569455d92612010-03-07 22:16:07 -0500747 err = myDisk.OpenForWrite(deviceFilename);
748 if ((err == 0) && (!justLooking)) {
749 cout << "\aNOTE: Write test failed with error number " << errno
750 << ". It will be impossible to save\nchanges to this disk's partition table!\n";
751#if defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
752 cout << "You may be able to enable writes by exiting this program, typing\n"
753 << "'sysctl kern.geom.debugflags=16' at a shell prompt, and re-running this\n"
754 << "program.\n";
755#endif
Aurimas Liutikasbdbab022017-03-07 09:50:36 -0800756 cout << "\n";
srs569455d92612010-03-07 22:16:07 -0500757 } // if
758 myDisk.Close(); // Close and re-open read-only in case of bugs
759 } else allOK = 0; // if
760
761 if (allOK && myDisk.OpenForRead(deviceFilename)) {
srs5694e4ac11e2009-08-31 10:13:04 -0400762 // store disk information....
srs5694546a9c72010-01-26 16:00:26 -0500763 diskSize = myDisk.DiskSize(&err);
764 blockSize = (uint32_t) myDisk.GetBlockSize();
srs5694fed16d02010-01-27 23:03:40 -0500765 device = deviceFilename;
srs5694546a9c72010-01-26 16:00:26 -0500766 PartitionScan(); // Check for partition types, load GPT, & print summary
srs5694e4ac11e2009-08-31 10:13:04 -0400767
srs5694ba00fed2010-01-12 18:18:36 -0500768 whichWasUsed = UseWhichPartitions();
769 switch (whichWasUsed) {
srs5694e4ac11e2009-08-31 10:13:04 -0400770 case use_mbr:
771 XFormPartitions();
772 break;
773 case use_bsd:
srs5694546a9c72010-01-26 16:00:26 -0500774 bsdDisklabel.ReadBSDData(&myDisk, 0, diskSize - 1);
srs5694e4ac11e2009-08-31 10:13:04 -0400775// bsdDisklabel.DisplayBSDData();
776 ClearGPTData();
777 protectiveMBR.MakeProtectiveMBR(1); // clear boot area (option 1)
srs569408bb0da2010-02-19 17:19:55 -0500778 XFormDisklabel(&bsdDisklabel);
srs5694e4ac11e2009-08-31 10:13:04 -0400779 break;
780 case use_gpt:
srs5694fed16d02010-01-27 23:03:40 -0500781 mbrState = protectiveMBR.GetValidity();
782 if ((mbrState == invalid) || (mbrState == mbr))
783 protectiveMBR.MakeProtectiveMBR();
srs5694e4ac11e2009-08-31 10:13:04 -0400784 break;
785 case use_new:
786 ClearGPTData();
787 protectiveMBR.MakeProtectiveMBR();
788 break;
srs56943c0af382010-01-15 19:19:18 -0500789 case use_abort:
790 allOK = 0;
srs56949ddc14b2010-08-22 22:44:42 -0400791 cerr << "Invalid partition data!\n";
srs56943c0af382010-01-15 19:19:18 -0500792 break;
srs5694e4ac11e2009-08-31 10:13:04 -0400793 } // switch
794
srs569455d92612010-03-07 22:16:07 -0500795 if (allOK)
srs56943c0af382010-01-15 19:19:18 -0500796 CheckGPTSize();
srs569455d92612010-03-07 22:16:07 -0500797 myDisk.Close();
srs5694a8582cf2010-03-19 14:21:59 -0400798 ComputeAlignment();
srs5694e4ac11e2009-08-31 10:13:04 -0400799 } else {
800 allOK = 0;
srs5694e4ac11e2009-08-31 10:13:04 -0400801 } // if/else
802 return (allOK);
803} // GPTData::LoadPartitions()
804
805// Loads the GPT, as much as possible. Returns 1 if this seems to have
806// succeeded, 0 if there are obvious problems....
srs5694546a9c72010-01-26 16:00:26 -0500807int GPTData::ForceLoadGPTData(void) {
srs5694cb76c672010-02-11 22:22:22 -0500808 int allOK, validHeaders, loadedTable = 1;
srs5694e4ac11e2009-08-31 10:13:04 -0400809
srs5694cb76c672010-02-11 22:22:22 -0500810 allOK = LoadHeader(&mainHeader, myDisk, 1, &mainCrcOk);
srs5694e4ac11e2009-08-31 10:13:04 -0400811
srs5694cb76c672010-02-11 22:22:22 -0500812 if (mainCrcOk && (mainHeader.backupLBA < diskSize)) {
813 allOK = LoadHeader(&secondHeader, myDisk, mainHeader.backupLBA, &secondCrcOk) && allOK;
814 } else {
srs569408bb0da2010-02-19 17:19:55 -0500815 allOK = LoadHeader(&secondHeader, myDisk, diskSize - UINT64_C(1), &secondCrcOk) && allOK;
816 if (mainCrcOk && (mainHeader.backupLBA >= diskSize))
srs5694fed16d02010-01-27 23:03:40 -0500817 cout << "Warning! Disk size is smaller than the main header indicates! Loading\n"
818 << "secondary header from the last sector of the disk! You should use 'v' to\n"
819 << "verify disk integrity, and perhaps options on the experts' menu to repair\n"
820 << "the disk.\n";
srs5694cb76c672010-02-11 22:22:22 -0500821 } // if/else
822 if (!allOK)
srs5694e4ac11e2009-08-31 10:13:04 -0400823 state = gpt_invalid;
srs5694e4ac11e2009-08-31 10:13:04 -0400824
825 // Return valid headers code: 0 = both headers bad; 1 = main header
826 // good, backup bad; 2 = backup header good, main header bad;
827 // 3 = both headers good. Note these codes refer to valid GPT
srs569423d8d542011-10-01 18:40:10 -0400828 // signatures, version numbers, and CRCs.
srs5694e4ac11e2009-08-31 10:13:04 -0400829 validHeaders = CheckHeaderValidity();
830
831 // Read partitions (from primary array)
832 if (validHeaders > 0) { // if at least one header is OK....
833 // GPT appears to be valid....
834 state = gpt_valid;
835
836 // We're calling the GPT valid, but there's a possibility that one
837 // of the two headers is corrupt. If so, use the one that seems to
838 // be in better shape to regenerate the bad one
srs5694546a9c72010-01-26 16:00:26 -0500839 if (validHeaders == 1) { // valid main header, invalid backup header
srs5694fed16d02010-01-27 23:03:40 -0500840 cerr << "\aCaution: invalid backup GPT header, but valid main header; regenerating\n"
841 << "backup header from main header.\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400842 RebuildSecondHeader();
srs5694546a9c72010-01-26 16:00:26 -0500843 state = gpt_corrupt;
srs5694e4ac11e2009-08-31 10:13:04 -0400844 secondCrcOk = mainCrcOk; // Since regenerated, use CRC validity of main
srs5694546a9c72010-01-26 16:00:26 -0500845 } else if (validHeaders == 2) { // valid backup header, invalid main header
srs5694fed16d02010-01-27 23:03:40 -0500846 cerr << "\aCaution: invalid main GPT header, but valid backup; regenerating main header\n"
847 << "from backup!\n\n";
srs5694546a9c72010-01-26 16:00:26 -0500848 RebuildMainHeader();
849 state = gpt_corrupt;
850 mainCrcOk = secondCrcOk; // Since copied, use CRC validity of backup
srs5694e4ac11e2009-08-31 10:13:04 -0400851 } // if/else/if
852
srs5694546a9c72010-01-26 16:00:26 -0500853 // Figure out which partition table to load....
854 // Load the main partition table, since either its header's CRC is OK or the
855 // backup header's CRC is not OK....
856 if (mainCrcOk || !secondCrcOk) {
857 if (LoadMainTable() == 0)
858 allOK = 0;
859 } else { // bad main header CRC and backup header CRC is OK
860 state = gpt_corrupt;
861 if (LoadSecondTableAsMain()) {
srs5694cb76c672010-02-11 22:22:22 -0500862 loadedTable = 2;
srs5694fed16d02010-01-27 23:03:40 -0500863 cerr << "\aWarning: Invalid CRC on main header data; loaded backup partition table.\n";
srs5694546a9c72010-01-26 16:00:26 -0500864 } else { // backup table bad, bad main header CRC, but try main table in desperation....
865 if (LoadMainTable() == 0) {
866 allOK = 0;
srs5694cb76c672010-02-11 22:22:22 -0500867 loadedTable = 0;
srs5694fed16d02010-01-27 23:03:40 -0500868 cerr << "\a\aWarning! Unable to load either main or backup partition table!\n";
srs5694546a9c72010-01-26 16:00:26 -0500869 } // if
870 } // if/else (LoadSecondTableAsMain())
871 } // if/else (load partition table)
srs5694e4ac11e2009-08-31 10:13:04 -0400872
srs5694cb76c672010-02-11 22:22:22 -0500873 if (loadedTable == 1)
874 secondPartsCrcOk = CheckTable(&secondHeader);
875 else if (loadedTable == 2)
876 mainPartsCrcOk = CheckTable(&mainHeader);
877 else
878 mainPartsCrcOk = secondPartsCrcOk = 0;
srs5694e4ac11e2009-08-31 10:13:04 -0400879
srs5694546a9c72010-01-26 16:00:26 -0500880 // Problem with main partition table; if backup is OK, use it instead....
881 if (secondPartsCrcOk && secondCrcOk && !mainPartsCrcOk) {
882 state = gpt_corrupt;
883 allOK = allOK && LoadSecondTableAsMain();
srs5694cb76c672010-02-11 22:22:22 -0500884 mainPartsCrcOk = 0; // LoadSecondTableAsMain() resets this, so re-flag as bad
srs5694fed16d02010-01-27 23:03:40 -0500885 cerr << "\aWarning! Main partition table CRC mismatch! Loaded backup "
886 << "partition table\ninstead of main partition table!\n\n";
srs5694cb76c672010-02-11 22:22:22 -0500887 } // if */
srs5694546a9c72010-01-26 16:00:26 -0500888
srs5694e4ac11e2009-08-31 10:13:04 -0400889 // Check for valid CRCs and warn if there are problems
890 if ((mainCrcOk == 0) || (secondCrcOk == 0) || (mainPartsCrcOk == 0) ||
891 (secondPartsCrcOk == 0)) {
srs5694fed16d02010-01-27 23:03:40 -0500892 cerr << "Warning! One or more CRCs don't match. You should repair the disk!\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -0400893 state = gpt_corrupt;
srs5694ba00fed2010-01-12 18:18:36 -0500894 } // if
srs5694e4ac11e2009-08-31 10:13:04 -0400895 } else {
896 state = gpt_invalid;
897 } // if/else
898 return allOK;
899} // GPTData::ForceLoadGPTData()
900
srs5694247657a2009-11-26 18:36:12 -0500901// Loads the partition table pointed to by the main GPT header. The
srs5694e4ac11e2009-08-31 10:13:04 -0400902// main GPT header in memory MUST be valid for this call to do anything
903// sensible!
srs5694546a9c72010-01-26 16:00:26 -0500904// Returns 1 on success, 0 on failure. CRC errors do NOT count as failure.
srs5694e4ac11e2009-08-31 10:13:04 -0400905int GPTData::LoadMainTable(void) {
srs5694cb76c672010-02-11 22:22:22 -0500906 return LoadPartitionTable(mainHeader, myDisk);
srs5694e4ac11e2009-08-31 10:13:04 -0400907} // GPTData::LoadMainTable()
srs5694e7b4ff92009-08-18 13:16:10 -0400908
909// Load the second (backup) partition table as the primary partition
srs5694546a9c72010-01-26 16:00:26 -0500910// table. Used in repair functions, and when starting up if the main
911// partition table is damaged.
912// Returns 1 on success, 0 on failure. CRC errors do NOT count as failure.
913int GPTData::LoadSecondTableAsMain(void) {
srs5694cb76c672010-02-11 22:22:22 -0500914 return LoadPartitionTable(secondHeader, myDisk);
915} // GPTData::LoadSecondTableAsMain()
srs5694e7b4ff92009-08-18 13:16:10 -0400916
srs5694cb76c672010-02-11 22:22:22 -0500917// Load a single GPT header (main or backup) from the specified disk device and
918// sector. Applies byte-order corrections on big-endian platforms. Sets crcOk
919// value appropriately.
920// Returns 1 on success, 0 on failure. Note that CRC errors do NOT qualify as
921// failure.
922int GPTData::LoadHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector, int *crcOk) {
923 int allOK = 1;
srs56941c6f8b02010-02-21 11:09:20 -0500924 GPTHeader tempHeader;
srs5694cb76c672010-02-11 22:22:22 -0500925
926 disk.Seek(sector);
srs56941c6f8b02010-02-21 11:09:20 -0500927 if (disk.Read(&tempHeader, 512) != 512) {
srs5694cb76c672010-02-11 22:22:22 -0500928 cerr << "Warning! Read error " << errno << "; strange behavior now likely!\n";
929 allOK = 0;
930 } // if
srs5694cb76c672010-02-11 22:22:22 -0500931
srs56941c6f8b02010-02-21 11:09:20 -0500932 // Reverse byte order, if necessary
srs5694cb76c672010-02-11 22:22:22 -0500933 if (IsLittleEndian() == 0) {
srs569455d92612010-03-07 22:16:07 -0500934 ReverseHeaderBytes(&tempHeader);
srs5694cb76c672010-02-11 22:22:22 -0500935 } // if
srs5694d1b11e82011-09-18 21:12:28 -0400936 *crcOk = CheckHeaderCRC(&tempHeader);
srs56941c6f8b02010-02-21 11:09:20 -0500937
srs56940283dae2010-04-28 16:44:34 -0400938 if (allOK && (numParts != tempHeader.numParts) && *crcOk) {
srs5694706e5122012-01-21 13:47:24 -0500939 allOK = SetGPTSize(tempHeader.numParts, 0);
srs569455d92612010-03-07 22:16:07 -0500940 }
srs56941c6f8b02010-02-21 11:09:20 -0500941
942 *header = tempHeader;
srs5694cb76c672010-02-11 22:22:22 -0500943 return allOK;
944} // GPTData::LoadHeader
945
946// Load a partition table (either main or secondary) from the specified disk,
947// using header as a reference for what to load. If sector != 0 (the default
948// is 0), loads from the specified sector; otherwise loads from the sector
949// indicated in header.
950// Returns 1 on success, 0 on failure. CRC errors do NOT count as failure.
951int GPTData::LoadPartitionTable(const struct GPTHeader & header, DiskIO & disk, uint64_t sector) {
952 uint32_t sizeOfParts, newCRC;
953 int retval;
954
955 if (disk.OpenForRead()) {
956 if (sector == 0) {
957 retval = disk.Seek(header.partitionEntriesLBA);
958 } else {
959 retval = disk.Seek(sector);
960 } // if/else
srs569455d92612010-03-07 22:16:07 -0500961 if (retval == 1)
srs5694706e5122012-01-21 13:47:24 -0500962 retval = SetGPTSize(header.numParts, 0);
srs5694546a9c72010-01-26 16:00:26 -0500963 if (retval == 1) {
srs5694cb76c672010-02-11 22:22:22 -0500964 sizeOfParts = header.numParts * header.sizeOfPartitionEntries;
965 if (disk.Read(partitions, sizeOfParts) != (int) sizeOfParts) {
srs5694fed16d02010-01-27 23:03:40 -0500966 cerr << "Warning! Read error " << errno << "! Misbehavior now likely!\n";
srs5694546a9c72010-01-26 16:00:26 -0500967 retval = 0;
srs56945d58fe02010-01-03 20:57:08 -0500968 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400969 newCRC = chksum_crc32((unsigned char*) partitions, sizeOfParts);
srs5694cb76c672010-02-11 22:22:22 -0500970 mainPartsCrcOk = secondPartsCrcOk = (newCRC == header.partitionEntriesCRC);
srs56942a9f5da2009-08-26 00:48:01 -0400971 if (IsLittleEndian() == 0)
972 ReversePartitionBytes();
srs5694cb76c672010-02-11 22:22:22 -0500973 if (!mainPartsCrcOk) {
974 cout << "Caution! After loading partitions, the CRC doesn't check out!\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400975 } // if
976 } else {
srs5694cb76c672010-02-11 22:22:22 -0500977 cerr << "Error! Couldn't seek to partition table!\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400978 } // if/else
979 } else {
srs5694fed16d02010-01-27 23:03:40 -0500980 cerr << "Error! Couldn't open device " << device
srs5694cb76c672010-02-11 22:22:22 -0500981 << " when reading partition table!\n";
srs5694546a9c72010-01-26 16:00:26 -0500982 retval = 0;
srs5694e7b4ff92009-08-18 13:16:10 -0400983 } // if/else
srs5694546a9c72010-01-26 16:00:26 -0500984 return retval;
srs5694cb76c672010-02-11 22:22:22 -0500985} // GPTData::LoadPartitionsTable()
986
987// Check the partition table pointed to by header, but don't keep it
988// around.
srs5694a17fe692011-09-10 20:30:20 -0400989// Returns 1 if the CRC is OK & this table matches the one already in memory,
990// 0 if not or if there was a read error.
srs5694cb76c672010-02-11 22:22:22 -0500991int GPTData::CheckTable(struct GPTHeader *header) {
992 uint32_t sizeOfParts, newCRC;
srs5694a17fe692011-09-10 20:30:20 -0400993 GPTPart *partsToCheck;
srs5694d1b11e82011-09-18 21:12:28 -0400994 GPTHeader *otherHeader;
srs5694a17fe692011-09-10 20:30:20 -0400995 int allOK = 0;
srs5694cb76c672010-02-11 22:22:22 -0500996
srs56940283dae2010-04-28 16:44:34 -0400997 // Load partition table into temporary storage to check
srs5694cb76c672010-02-11 22:22:22 -0500998 // its CRC and store the results, then discard this temporary
999 // storage, since we don't use it in any but recovery operations
1000 if (myDisk.Seek(header->partitionEntriesLBA)) {
srs5694a17fe692011-09-10 20:30:20 -04001001 partsToCheck = new GPTPart[header->numParts];
srs56940283dae2010-04-28 16:44:34 -04001002 sizeOfParts = header->numParts * header->sizeOfPartitionEntries;
srs5694a17fe692011-09-10 20:30:20 -04001003 if (partsToCheck == NULL) {
srs56946aae2a92011-06-10 01:16:51 -04001004 cerr << "Could not allocate memory in GPTData::CheckTable()! Terminating!\n";
1005 exit(1);
1006 } // if
srs5694a17fe692011-09-10 20:30:20 -04001007 if (myDisk.Read(partsToCheck, sizeOfParts) != (int) sizeOfParts) {
srs56940283dae2010-04-28 16:44:34 -04001008 cerr << "Warning! Error " << errno << " reading partition table for CRC check!\n";
srs5694cb76c672010-02-11 22:22:22 -05001009 } else {
srs5694d1b11e82011-09-18 21:12:28 -04001010 newCRC = chksum_crc32((unsigned char*) partsToCheck, sizeOfParts);
srs5694a17fe692011-09-10 20:30:20 -04001011 allOK = (newCRC == header->partitionEntriesCRC);
srs5694d1b11e82011-09-18 21:12:28 -04001012 if (header == &mainHeader)
1013 otherHeader = &secondHeader;
1014 else
1015 otherHeader = &mainHeader;
1016 if (newCRC != otherHeader->partitionEntriesCRC) {
srs5694a17fe692011-09-10 20:30:20 -04001017 cerr << "Warning! Main and backup partition tables differ! Use the 'c' and 'e' options\n"
1018 << "on the recovery & transformation menu to examine the two tables.\n\n";
1019 allOK = 0;
1020 } // if
srs5694cb76c672010-02-11 22:22:22 -05001021 } // if/else
srs5694a17fe692011-09-10 20:30:20 -04001022 delete[] partsToCheck;
srs5694cb76c672010-02-11 22:22:22 -05001023 } // if
srs5694a17fe692011-09-10 20:30:20 -04001024 return allOK;
srs5694cb76c672010-02-11 22:22:22 -05001025} // GPTData::CheckTable()
srs5694e7b4ff92009-08-18 13:16:10 -04001026
srs56944307ef22012-05-30 12:30:48 -04001027// Writes GPT (and protective MBR) to disk. If quiet==1, moves the second
1028// header later on the disk without asking for permission, if necessary, and
1029// doesn't confirm the operation before writing. If quiet==0, asks permission
1030// before moving the second header and asks for final confirmation of any
1031// write.
srs5694a17fe692011-09-10 20:30:20 -04001032// Returns 1 on successful write, 0 if there was a problem.
srs569464cbd172011-03-01 22:03:54 -05001033int GPTData::SaveGPTData(int quiet) {
srs56944307ef22012-05-30 12:30:48 -04001034 int allOK = 1, syncIt = 1;
srs5694e321d442010-01-29 17:44:04 -05001035 char answer;
srs5694e7b4ff92009-08-18 13:16:10 -04001036
srs5694e7b4ff92009-08-18 13:16:10 -04001037 // First do some final sanity checks....
srs56945d58fe02010-01-03 20:57:08 -05001038
1039 // This test should only fail on read-only disks....
1040 if (justLooking) {
srs5694fed16d02010-01-27 23:03:40 -05001041 cout << "The justLooking flag is set. This probably means you can't write to the disk.\n";
srs56945d58fe02010-01-03 20:57:08 -05001042 allOK = 0;
1043 } // if
1044
srs569464cbd172011-03-01 22:03:54 -05001045 // Check that disk is really big enough to handle the second header...
1046 if (mainHeader.backupLBA >= diskSize) {
1047 cerr << "Caution! Secondary header was placed beyond the disk's limits! Moving the\n"
1048 << "header, but other problems may occur!\n";
1049 MoveSecondHeaderToEnd();
1050 } // if
1051
srs5694e7b4ff92009-08-18 13:16:10 -04001052 // Is there enough space to hold the GPT headers and partition tables,
1053 // given the partition sizes?
srs5694221e0872009-08-29 15:00:31 -04001054 if (CheckGPTSize() > 0) {
srs5694e7b4ff92009-08-18 13:16:10 -04001055 allOK = 0;
1056 } // if
1057
srs5694247657a2009-11-26 18:36:12 -05001058 // Check that second header is properly placed. Warn and ask if this should
1059 // be corrected if the test fails....
srs569464cbd172011-03-01 22:03:54 -05001060 if (mainHeader.backupLBA < (diskSize - UINT64_C(1))) {
1061 if (quiet == 0) {
1062 cout << "Warning! Secondary header is placed too early on the disk! Do you want to\n"
1063 << "correct this problem? ";
1064 if (GetYN() == 'Y') {
1065 MoveSecondHeaderToEnd();
1066 cout << "Have moved second header and partition table to correct location.\n";
1067 } else {
1068 cout << "Have not corrected the problem. Strange problems may occur in the future!\n";
1069 } // if correction requested
1070 } else { // Go ahead and do correction automatically
srs5694247657a2009-11-26 18:36:12 -05001071 MoveSecondHeaderToEnd();
srs569464cbd172011-03-01 22:03:54 -05001072 } // if/else quiet
srs5694247657a2009-11-26 18:36:12 -05001073 } // if
srs5694e7b4ff92009-08-18 13:16:10 -04001074
srs5694d8eed462012-12-15 01:55:21 -05001075 if ((mainHeader.lastUsableLBA >= diskSize) || (mainHeader.lastUsableLBA > mainHeader.backupLBA)) {
1076 if (quiet == 0) {
1077 cout << "Warning! The claimed last usable sector is incorrect! Do you want to correct\n"
1078 << "this problem? ";
1079 if (GetYN() == 'Y') {
1080 MoveSecondHeaderToEnd();
1081 cout << "Have adjusted the second header and last usable sector value.\n";
1082 } else {
1083 cout << "Have not corrected the problem. Strange problems may occur in the future!\n";
1084 } // if correction requested
1085 } else { // go ahead and do correction automatically
1086 MoveSecondHeaderToEnd();
1087 } // if/else quiet
1088 } // if
1089
srs569455d92612010-03-07 22:16:07 -05001090 // Check for overlapping or insane partitions....
1091 if ((FindOverlaps() > 0) || (FindInsanePartitions() > 0)) {
srs5694e4ac11e2009-08-31 10:13:04 -04001092 allOK = 0;
srs5694fed16d02010-01-27 23:03:40 -05001093 cerr << "Aborting write operation!\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001094 } // if
1095
Roderick W. Smith4a702a22014-01-25 23:46:42 -05001096 // Check that protective MBR fits, and warn if it doesn't....
1097 if (!protectiveMBR.DoTheyFit()) {
1098 cerr << "\nPartition(s) in the protective MBR are too big for the disk! Creating a\n"
1099 << "fresh protective or hybrid MBR is recommended.\n";
1100 }
1101
srs5694e4ac11e2009-08-31 10:13:04 -04001102 // Check for mismatched MBR and GPT data, but let it pass if found
1103 // (function displays warning message)
1104 FindHybridMismatches();
srs5694e7b4ff92009-08-18 13:16:10 -04001105
1106 RecomputeCRCs();
1107
srs5694ba00fed2010-01-12 18:18:36 -05001108 if ((allOK) && (!quiet)) {
srs5694fed16d02010-01-27 23:03:40 -05001109 cout << "\nFinal checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING\n"
srs5694bf8950c2011-03-12 01:23:12 -05001110 << "PARTITIONS!!\n\nDo you want to proceed? ";
srs56945d58fe02010-01-03 20:57:08 -05001111 answer = GetYN();
1112 if (answer == 'Y') {
srs569434882942012-03-23 12:49:15 -04001113 cout << "OK; writing new GUID partition table (GPT) to " << myDisk.GetName() << ".\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001114 } else {
1115 allOK = 0;
1116 } // if/else
1117 } // if
1118
1119 // Do it!
1120 if (allOK) {
srs569464cbd172011-03-01 22:03:54 -05001121 if (myDisk.OpenForWrite()) {
srs56948a4ddfc2010-03-21 19:05:49 -04001122 // As per UEFI specs, write the secondary table and GPT first....
srs5694cb76c672010-02-11 22:22:22 -05001123 allOK = SavePartitionTable(myDisk, secondHeader.partitionEntriesLBA);
srs56944307ef22012-05-30 12:30:48 -04001124 if (!allOK) {
srs5694cb76c672010-02-11 22:22:22 -05001125 cerr << "Unable to save backup partition table! Perhaps the 'e' option on the experts'\n"
1126 << "menu will resolve this problem.\n";
srs56944307ef22012-05-30 12:30:48 -04001127 syncIt = 0;
1128 } // if
srs5694e7b4ff92009-08-18 13:16:10 -04001129
1130 // Now write the secondary GPT header...
srs56948a4ddfc2010-03-21 19:05:49 -04001131 allOK = allOK && SaveHeader(&secondHeader, myDisk, mainHeader.backupLBA);
1132
1133 // Now write the main partition tables...
1134 allOK = allOK && SavePartitionTable(myDisk, mainHeader.partitionEntriesLBA);
1135
1136 // Now write the main GPT header...
1137 allOK = allOK && SaveHeader(&mainHeader, myDisk, 1);
1138
1139 // To top it off, write the protective MBR...
1140 allOK = allOK && protectiveMBR.WriteMBRData(&myDisk);
srs5694e7b4ff92009-08-18 13:16:10 -04001141
1142 // re-read the partition table
srs56944307ef22012-05-30 12:30:48 -04001143 // Note: Done even if some write operations failed, but not if all of them failed.
1144 // Done this way because I've received one problem report from a user one whose
1145 // system the MBR write failed but everything else was OK (on a GPT disk under
1146 // Windows), and the failure to sync therefore caused Windows to restore the
1147 // original partition table from its cache. OTOH, such restoration might be
1148 // desirable if the error occurs later; but that seems unlikely unless the initial
1149 // write fails....
Greg Hartman2c2deeb2016-04-21 18:20:25 -07001150 if (syncIt && syncing)
srs5694546a9c72010-01-26 16:00:26 -05001151 myDisk.DiskSync();
srs5694e7b4ff92009-08-18 13:16:10 -04001152
1153 if (allOK) { // writes completed OK
srs5694fed16d02010-01-27 23:03:40 -05001154 cout << "The operation has completed successfully.\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001155 } else {
srs5694fed16d02010-01-27 23:03:40 -05001156 cerr << "Warning! An error was reported when writing the partition table! This error\n"
srs56944307ef22012-05-30 12:30:48 -04001157 << "MIGHT be harmless, or the disk might be damaged! Checking it is advisable.\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001158 } // if/else
srs56948a4ddfc2010-03-21 19:05:49 -04001159
srs5694546a9c72010-01-26 16:00:26 -05001160 myDisk.Close();
srs5694e7b4ff92009-08-18 13:16:10 -04001161 } else {
srs56945a608532011-03-17 13:53:01 -04001162 cerr << "Unable to open device '" << myDisk.GetName() << "' for writing! Errno is "
srs5694fed16d02010-01-27 23:03:40 -05001163 << errno << "! Aborting write!\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001164 allOK = 0;
srs5694e7b4ff92009-08-18 13:16:10 -04001165 } // if/else
1166 } else {
srs5694fed16d02010-01-27 23:03:40 -05001167 cout << "Aborting write of new partition table.\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001168 } // if
1169
1170 return (allOK);
1171} // GPTData::SaveGPTData()
1172
1173// Save GPT data to a backup file. This function does much less error
1174// checking than SaveGPTData(). It can therefore preserve many types of
1175// corruption for later analysis; however, it preserves only the MBR,
1176// the main GPT header, the backup GPT header, and the main partition
1177// table; it discards the backup partition table, since it should be
1178// identical to the main partition table on healthy disks.
srs56940a697312010-01-28 21:10:52 -05001179int GPTData::SaveGPTBackup(const string & filename) {
1180 int allOK = 1;
srs5694546a9c72010-01-26 16:00:26 -05001181 DiskIO backupFile;
srs5694e7b4ff92009-08-18 13:16:10 -04001182
srs5694546a9c72010-01-26 16:00:26 -05001183 if (backupFile.OpenForWrite(filename)) {
srs56946699b012010-02-04 00:55:30 -05001184 // Recomputing the CRCs is likely to alter them, which could be bad
1185 // if the intent is to save a potentially bad GPT for later analysis;
1186 // but if we don't do this, we get bogus errors when we load the
1187 // backup. I'm favoring misses over false alarms....
1188 RecomputeCRCs();
1189
srs5694546a9c72010-01-26 16:00:26 -05001190 protectiveMBR.WriteMBRData(&backupFile);
srs5694699941e2011-03-21 21:33:57 -04001191 protectiveMBR.SetDisk(&myDisk);
srs5694e7b4ff92009-08-18 13:16:10 -04001192
srs5694cb76c672010-02-11 22:22:22 -05001193 if (allOK) {
srs5694546a9c72010-01-26 16:00:26 -05001194 // MBR write closed disk, so re-open and seek to end....
1195 backupFile.OpenForWrite();
srs5694cb76c672010-02-11 22:22:22 -05001196 allOK = SaveHeader(&mainHeader, backupFile, 1);
1197 } // if (allOK)
srs5694e7b4ff92009-08-18 13:16:10 -04001198
srs5694e7b4ff92009-08-18 13:16:10 -04001199 if (allOK)
srs5694cb76c672010-02-11 22:22:22 -05001200 allOK = SaveHeader(&secondHeader, backupFile, 2);
srs5694e7b4ff92009-08-18 13:16:10 -04001201
srs5694cb76c672010-02-11 22:22:22 -05001202 if (allOK)
1203 allOK = SavePartitionTable(backupFile, 3);
srs5694e7b4ff92009-08-18 13:16:10 -04001204
1205 if (allOK) { // writes completed OK
srs5694fed16d02010-01-27 23:03:40 -05001206 cout << "The operation has completed successfully.\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001207 } else {
srs5694fed16d02010-01-27 23:03:40 -05001208 cerr << "Warning! An error was reported when writing the backup file.\n"
1209 << "It may not be usable!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001210 } // if/else
srs5694546a9c72010-01-26 16:00:26 -05001211 backupFile.Close();
srs5694e7b4ff92009-08-18 13:16:10 -04001212 } else {
srs56945a608532011-03-17 13:53:01 -04001213 cerr << "Unable to open file '" << filename << "' for writing! Aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001214 allOK = 0;
1215 } // if/else
1216 return allOK;
1217} // GPTData::SaveGPTBackup()
1218
srs5694cb76c672010-02-11 22:22:22 -05001219// Write a GPT header (main or backup) to the specified sector. Used by both
1220// the SaveGPTData() and SaveGPTBackup() functions.
1221// Should be passed an architecture-appropriate header (DO NOT call
1222// ReverseHeaderBytes() on the header before calling this function)
1223// Returns 1 on success, 0 on failure
1224int GPTData::SaveHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector) {
1225 int littleEndian, allOK = 1;
1226
1227 littleEndian = IsLittleEndian();
1228 if (!littleEndian)
1229 ReverseHeaderBytes(header);
1230 if (disk.Seek(sector)) {
1231 if (disk.Write(header, 512) == -1)
1232 allOK = 0;
1233 } else allOK = 0; // if (disk.Seek()...)
1234 if (!littleEndian)
1235 ReverseHeaderBytes(header);
1236 return allOK;
1237} // GPTData::SaveHeader()
1238
1239// Save the partitions to the specified sector. Used by both the SaveGPTData()
1240// and SaveGPTBackup() functions.
1241// Should be passed an architecture-appropriate header (DO NOT call
1242// ReverseHeaderBytes() on the header before calling this function)
1243// Returns 1 on success, 0 on failure
1244int GPTData::SavePartitionTable(DiskIO & disk, uint64_t sector) {
1245 int littleEndian, allOK = 1;
1246
1247 littleEndian = IsLittleEndian();
1248 if (disk.Seek(sector)) {
1249 if (!littleEndian)
1250 ReversePartitionBytes();
srs56940283dae2010-04-28 16:44:34 -04001251 if (disk.Write(partitions, mainHeader.sizeOfPartitionEntries * numParts) == -1)
srs5694cb76c672010-02-11 22:22:22 -05001252 allOK = 0;
1253 if (!littleEndian)
1254 ReversePartitionBytes();
1255 } else allOK = 0; // if (myDisk.Seek()...)
1256 return allOK;
1257} // GPTData::SavePartitionTable()
1258
srs5694e7b4ff92009-08-18 13:16:10 -04001259// Load GPT data from a backup file created by SaveGPTBackup(). This function
1260// does minimal error checking. It returns 1 if it completed successfully,
1261// 0 if there was a problem. In the latter case, it creates a new empty
1262// set of partitions.
srs56940a697312010-01-28 21:10:52 -05001263int GPTData::LoadGPTBackup(const string & filename) {
srs5694cb76c672010-02-11 22:22:22 -05001264 int allOK = 1, val, err;
srs56940541b562011-12-18 16:35:25 -05001265 int shortBackup = 0;
srs5694546a9c72010-01-26 16:00:26 -05001266 DiskIO backupFile;
srs5694e7b4ff92009-08-18 13:16:10 -04001267
srs5694546a9c72010-01-26 16:00:26 -05001268 if (backupFile.OpenForRead(filename)) {
srs5694e7b4ff92009-08-18 13:16:10 -04001269 // Let the MBRData class load the saved MBR...
srs5694546a9c72010-01-26 16:00:26 -05001270 protectiveMBR.ReadMBRData(&backupFile, 0); // 0 = don't check block size
srs5694815fb652011-03-18 12:35:56 -04001271 protectiveMBR.SetDisk(&myDisk);
srs5694e7b4ff92009-08-18 13:16:10 -04001272
srs5694cb76c672010-02-11 22:22:22 -05001273 LoadHeader(&mainHeader, backupFile, 1, &mainCrcOk);
srs5694e7b4ff92009-08-18 13:16:10 -04001274
srs5694cb76c672010-02-11 22:22:22 -05001275 // Check backup file size and rebuild second header if file is right
1276 // size to be direct dd copy of MBR, main header, and main partition
1277 // table; if other size, treat it like a GPT fdisk-generated backup
1278 // file
1279 shortBackup = ((backupFile.DiskSize(&err) * backupFile.GetBlockSize()) ==
1280 (mainHeader.numParts * mainHeader.sizeOfPartitionEntries) + 1024);
1281 if (shortBackup) {
1282 RebuildSecondHeader();
1283 secondCrcOk = mainCrcOk;
1284 } else {
1285 LoadHeader(&secondHeader, backupFile, 2, &secondCrcOk);
1286 } // if/else
srs56942a9f5da2009-08-26 00:48:01 -04001287
srs5694e7b4ff92009-08-18 13:16:10 -04001288 // Return valid headers code: 0 = both headers bad; 1 = main header
1289 // good, backup bad; 2 = backup header good, main header bad;
1290 // 3 = both headers good. Note these codes refer to valid GPT
1291 // signatures and version numbers; more subtle problems will elude
1292 // this check!
1293 if ((val = CheckHeaderValidity()) > 0) {
1294 if (val == 2) { // only backup header seems to be good
srs5694706e5122012-01-21 13:47:24 -05001295 SetGPTSize(secondHeader.numParts, 0);
srs5694e7b4ff92009-08-18 13:16:10 -04001296 } else { // main header is OK
srs5694706e5122012-01-21 13:47:24 -05001297 SetGPTSize(mainHeader.numParts, 0);
srs5694e7b4ff92009-08-18 13:16:10 -04001298 } // if/else
1299
srs5694e7b4ff92009-08-18 13:16:10 -04001300 if (secondHeader.currentLBA != diskSize - UINT64_C(1)) {
srs5694fed16d02010-01-27 23:03:40 -05001301 cout << "Warning! Current disk size doesn't match that of the backup!\n"
1302 << "Adjusting sizes to match, but subsequent problems are possible!\n";
srs5694247657a2009-11-26 18:36:12 -05001303 MoveSecondHeaderToEnd();
srs5694e7b4ff92009-08-18 13:16:10 -04001304 } // if
1305
srs5694cb76c672010-02-11 22:22:22 -05001306 if (!LoadPartitionTable(mainHeader, backupFile, (uint64_t) (3 - shortBackup)))
1307 cerr << "Warning! Read error " << errno
1308 << " loading partition table; strange behavior now likely!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001309 } else {
1310 allOK = 0;
1311 } // if/else
srs5694a8582cf2010-03-19 14:21:59 -04001312 // Something went badly wrong, so blank out partitions
1313 if (allOK == 0) {
1314 cerr << "Improper backup file! Clearing all partition data!\n";
1315 ClearGPTData();
1316 protectiveMBR.MakeProtectiveMBR();
1317 } // if
srs5694e7b4ff92009-08-18 13:16:10 -04001318 } else {
1319 allOK = 0;
srs56945a608532011-03-17 13:53:01 -04001320 cerr << "Unable to open file '" << filename << "' for reading! Aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04001321 } // if/else
1322
srs5694e7b4ff92009-08-18 13:16:10 -04001323 return allOK;
1324} // GPTData::LoadGPTBackup()
1325
srs569408bb0da2010-02-19 17:19:55 -05001326int GPTData::SaveMBR(void) {
srs569455d92612010-03-07 22:16:07 -05001327 return protectiveMBR.WriteMBRData(&myDisk);
srs569408bb0da2010-02-19 17:19:55 -05001328} // GPTData::SaveMBR()
1329
1330// This function destroys the on-disk GPT structures, but NOT the on-disk
1331// MBR.
1332// Returns 1 if the operation succeeds, 0 if not.
1333int GPTData::DestroyGPT(void) {
srs569401f7f082011-03-15 23:53:31 -04001334 int sum, tableSize, allOK = 1;
srs569408bb0da2010-02-19 17:19:55 -05001335 uint8_t blankSector[512];
1336 uint8_t* emptyTable;
1337
srs569401f7f082011-03-15 23:53:31 -04001338 memset(blankSector, 0, sizeof(blankSector));
Roderick W. Smith84aaff62014-02-17 16:17:11 -05001339 ClearGPTData();
srs569408bb0da2010-02-19 17:19:55 -05001340
1341 if (myDisk.OpenForWrite()) {
1342 if (!myDisk.Seek(mainHeader.currentLBA))
1343 allOK = 0;
1344 if (myDisk.Write(blankSector, 512) != 512) { // blank it out
1345 cerr << "Warning! GPT main header not overwritten! Error is " << errno << "\n";
1346 allOK = 0;
1347 } // if
1348 if (!myDisk.Seek(mainHeader.partitionEntriesLBA))
1349 allOK = 0;
srs56940283dae2010-04-28 16:44:34 -04001350 tableSize = numParts * mainHeader.sizeOfPartitionEntries;
srs569408bb0da2010-02-19 17:19:55 -05001351 emptyTable = new uint8_t[tableSize];
srs56946aae2a92011-06-10 01:16:51 -04001352 if (emptyTable == NULL) {
Roderick W. Smith84aaff62014-02-17 16:17:11 -05001353 cerr << "Could not allocate memory in GPTData::DestroyGPT()! Terminating!\n";
1354 exit(1);
srs56946aae2a92011-06-10 01:16:51 -04001355 } // if
srs569401f7f082011-03-15 23:53:31 -04001356 memset(emptyTable, 0, tableSize);
srs569408bb0da2010-02-19 17:19:55 -05001357 if (allOK) {
1358 sum = myDisk.Write(emptyTable, tableSize);
1359 if (sum != tableSize) {
1360 cerr << "Warning! GPT main partition table not overwritten! Error is " << errno << "\n";
1361 allOK = 0;
1362 } // if write failed
Roderick W. Smith84aaff62014-02-17 16:17:11 -05001363 } // if
1364 if (!myDisk.Seek(secondHeader.partitionEntriesLBA))
1365 allOK = 0;
1366 if (allOK) {
1367 sum = myDisk.Write(emptyTable, tableSize);
1368 if (sum != tableSize) {
1369 cerr << "Warning! GPT backup partition table not overwritten! Error is "
1370 << errno << "\n";
1371 allOK = 0;
1372 } // if wrong size written
srs569408bb0da2010-02-19 17:19:55 -05001373 } // if
Roderick W. Smith84aaff62014-02-17 16:17:11 -05001374 if (!myDisk.Seek(secondHeader.currentLBA))
1375 allOK = 0;
1376 if (allOK) {
1377 if (myDisk.Write(blankSector, 512) != 512) { // blank it out
1378 cerr << "Warning! GPT backup header not overwritten! Error is " << errno << "\n";
srs569408bb0da2010-02-19 17:19:55 -05001379 allOK = 0;
1380 } // if
Roderick W. Smith84aaff62014-02-17 16:17:11 -05001381 } // if
Greg Hartman2c2deeb2016-04-21 18:20:25 -07001382 if (syncing) {
1383 myDisk.DiskSync();
1384 }
srs569408bb0da2010-02-19 17:19:55 -05001385 myDisk.Close();
1386 cout << "GPT data structures destroyed! You may now partition the disk using fdisk or\n"
1387 << "other utilities.\n";
1388 delete[] emptyTable;
1389 } else {
srs56945a608532011-03-17 13:53:01 -04001390 cerr << "Problem opening '" << device << "' for writing! Program will now terminate.\n";
srs569408bb0da2010-02-19 17:19:55 -05001391 } // if/else (fd != -1)
1392 return (allOK);
1393} // GPTDataTextUI::DestroyGPT()
1394
1395// Wipe MBR data from the disk (zero it out completely)
1396// Returns 1 on success, 0 on failure.
1397int GPTData::DestroyMBR(void) {
srs569401f7f082011-03-15 23:53:31 -04001398 int allOK;
srs569408bb0da2010-02-19 17:19:55 -05001399 uint8_t blankSector[512];
1400
srs569401f7f082011-03-15 23:53:31 -04001401 memset(blankSector, 0, sizeof(blankSector));
srs569408bb0da2010-02-19 17:19:55 -05001402
srs569401f7f082011-03-15 23:53:31 -04001403 allOK = myDisk.OpenForWrite() && myDisk.Seek(0) && (myDisk.Write(blankSector, 512) == 512);
1404
srs569408bb0da2010-02-19 17:19:55 -05001405 if (!allOK)
1406 cerr << "Warning! MBR not overwritten! Error is " << errno << "!\n";
1407 return allOK;
1408} // GPTData::DestroyMBR(void)
1409
srs5694e4ac11e2009-08-31 10:13:04 -04001410// Tell user whether Apple Partition Map (APM) was discovered....
1411void GPTData::ShowAPMState(void) {
1412 if (apmFound)
srs5694fed16d02010-01-27 23:03:40 -05001413 cout << " APM: present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001414 else
srs5694fed16d02010-01-27 23:03:40 -05001415 cout << " APM: not present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001416} // GPTData::ShowAPMState()
1417
1418// Tell user about the state of the GPT data....
1419void GPTData::ShowGPTState(void) {
1420 switch (state) {
1421 case gpt_invalid:
srs5694fed16d02010-01-27 23:03:40 -05001422 cout << " GPT: not present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001423 break;
1424 case gpt_valid:
srs5694fed16d02010-01-27 23:03:40 -05001425 cout << " GPT: present\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001426 break;
1427 case gpt_corrupt:
srs5694fed16d02010-01-27 23:03:40 -05001428 cout << " GPT: damaged\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001429 break;
1430 default:
srs5694fed16d02010-01-27 23:03:40 -05001431 cout << "\a GPT: unknown -- bug!\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001432 break;
1433 } // switch
1434} // GPTData::ShowGPTState()
1435
1436// Display the basic GPT data
1437void GPTData::DisplayGPTData(void) {
srs5694e321d442010-01-29 17:44:04 -05001438 uint32_t i;
srs5694e4ac11e2009-08-31 10:13:04 -04001439 uint64_t temp, totalFree;
1440
srs5694fed16d02010-01-27 23:03:40 -05001441 cout << "Disk " << device << ": " << diskSize << " sectors, "
srs569401f7f082011-03-15 23:53:31 -04001442 << BytesToIeee(diskSize, blockSize) << "\n";
srs5694fed16d02010-01-27 23:03:40 -05001443 cout << "Logical sector size: " << blockSize << " bytes\n";
srs56945a081752010-09-24 20:39:41 -04001444 cout << "Disk identifier (GUID): " << mainHeader.diskGUID << "\n";
srs56940283dae2010-04-28 16:44:34 -04001445 cout << "Partition table holds up to " << numParts << " entries\n";
srs5694fed16d02010-01-27 23:03:40 -05001446 cout << "First usable sector is " << mainHeader.firstUsableLBA
1447 << ", last usable sector is " << mainHeader.lastUsableLBA << "\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001448 totalFree = FindFreeBlocks(&i, &temp);
srs56948a4ddfc2010-03-21 19:05:49 -04001449 cout << "Partitions will be aligned on " << sectorAlignment << "-sector boundaries\n";
srs5694fed16d02010-01-27 23:03:40 -05001450 cout << "Total free space is " << totalFree << " sectors ("
srs569401f7f082011-03-15 23:53:31 -04001451 << BytesToIeee(totalFree, blockSize) << ")\n";
srs5694fed16d02010-01-27 23:03:40 -05001452 cout << "\nNumber Start (sector) End (sector) Size Code Name\n";
srs56940283dae2010-04-28 16:44:34 -04001453 for (i = 0; i < numParts; i++) {
srs5694978041c2009-09-21 20:51:47 -04001454 partitions[i].ShowSummary(i, blockSize);
srs5694e4ac11e2009-08-31 10:13:04 -04001455 } // for
1456} // GPTData::DisplayGPTData()
1457
srs5694e4ac11e2009-08-31 10:13:04 -04001458// Show detailed information on the specified partition
Jeff Sharkeyd761ff52015-02-28 19:18:39 -08001459void GPTData::ShowPartDetails(uint32_t partNum) {
Roderick W. Smith24bba6e2013-10-12 19:07:16 -04001460 if ((partNum < numParts) && !IsFreePartNum(partNum)) {
Jeff Sharkeyd761ff52015-02-28 19:18:39 -08001461 partitions[partNum].ShowDetails(blockSize);
srs5694e4ac11e2009-08-31 10:13:04 -04001462 } else {
Roderick W. Smith24bba6e2013-10-12 19:07:16 -04001463 cout << "Partition #" << partNum + 1 << " does not exist.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001464 } // if
1465} // GPTData::ShowPartDetails()
1466
srs5694e4ac11e2009-08-31 10:13:04 -04001467/**************************************************************************
1468 * *
1469 * Partition table transformation functions (MBR or BSD disklabel to GPT) *
1470 * (some of these functions may require user interaction) *
1471 * *
1472 **************************************************************************/
1473
srs569408bb0da2010-02-19 17:19:55 -05001474// Examines the MBR & GPT data to determine which set of data to use: the
1475// MBR (use_mbr), the GPT (use_gpt), the BSD disklabel (use_bsd), or create
1476// a new set of partitions (use_new). A return value of use_abort indicates
1477// that this function couldn't determine what to do. Overriding functions
1478// in derived classes may ask users questions in such cases.
srs5694e4ac11e2009-08-31 10:13:04 -04001479WhichToUse GPTData::UseWhichPartitions(void) {
1480 WhichToUse which = use_new;
1481 MBRValidity mbrState;
srs5694e4ac11e2009-08-31 10:13:04 -04001482
1483 mbrState = protectiveMBR.GetValidity();
1484
1485 if ((state == gpt_invalid) && ((mbrState == mbr) || (mbrState == hybrid))) {
srs5694fed16d02010-01-27 23:03:40 -05001486 cout << "\n***************************************************************\n"
Roderick W. Smith1eea9b02013-07-06 22:52:58 -04001487 << "Found invalid GPT and valid MBR; converting MBR to GPT format\n"
1488 << "in memory. ";
srs56945d58fe02010-01-03 20:57:08 -05001489 if (!justLooking) {
Roderick W. Smith1eea9b02013-07-06 22:52:58 -04001490 cout << "\aTHIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by\n"
1491 << "typing 'q' if you don't want to convert your MBR partitions\n"
1492 << "to GPT format!";
srs56945d58fe02010-01-03 20:57:08 -05001493 } // if
Roderick W. Smith1eea9b02013-07-06 22:52:58 -04001494 cout << "\n***************************************************************\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001495 which = use_mbr;
1496 } // if
1497
1498 if ((state == gpt_invalid) && bsdFound) {
srs5694fed16d02010-01-27 23:03:40 -05001499 cout << "\n**********************************************************************\n"
1500 << "Found invalid GPT and valid BSD disklabel; converting BSD disklabel\n"
1501 << "to GPT format.";
srs56940a697312010-01-28 21:10:52 -05001502 if ((!justLooking) && (!beQuiet)) {
srs56940283dae2010-04-28 16:44:34 -04001503 cout << "\a THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Your first\n"
srs5694fed16d02010-01-27 23:03:40 -05001504 << "BSD partition will likely be unusable. Exit by typing 'q' if you don't\n"
1505 << "want to convert your BSD partitions to GPT format!";
srs56945d58fe02010-01-03 20:57:08 -05001506 } // if
srs5694fed16d02010-01-27 23:03:40 -05001507 cout << "\n**********************************************************************\n\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001508 which = use_bsd;
1509 } // if
1510
1511 if ((state == gpt_valid) && (mbrState == gpt)) {
srs5694e4ac11e2009-08-31 10:13:04 -04001512 which = use_gpt;
srs56943c0af382010-01-15 19:19:18 -05001513 if (!beQuiet)
srs5694fed16d02010-01-27 23:03:40 -05001514 cout << "Found valid GPT with protective MBR; using GPT.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001515 } // if
1516 if ((state == gpt_valid) && (mbrState == hybrid)) {
srs5694e4ac11e2009-08-31 10:13:04 -04001517 which = use_gpt;
srs56943c0af382010-01-15 19:19:18 -05001518 if (!beQuiet)
srs5694fed16d02010-01-27 23:03:40 -05001519 cout << "Found valid GPT with hybrid MBR; using GPT.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001520 } // if
1521 if ((state == gpt_valid) && (mbrState == invalid)) {
srs56940a697312010-01-28 21:10:52 -05001522 cout << "\aFound valid GPT with corrupt MBR; using GPT and will write new\n"
srs5694fed16d02010-01-27 23:03:40 -05001523 << "protective MBR on save.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001524 which = use_gpt;
srs5694e4ac11e2009-08-31 10:13:04 -04001525 } // if
1526 if ((state == gpt_valid) && (mbrState == mbr)) {
srs569408bb0da2010-02-19 17:19:55 -05001527 which = use_abort;
srs5694e4ac11e2009-08-31 10:13:04 -04001528 } // if
1529
srs5694e4ac11e2009-08-31 10:13:04 -04001530 if (state == gpt_corrupt) {
srs569408bb0da2010-02-19 17:19:55 -05001531 if (mbrState == gpt) {
1532 cout << "\a\a****************************************************************************\n"
1533 << "Caution: Found protective or hybrid MBR and corrupt GPT. Using GPT, but disk\n"
1534 << "verification and recovery are STRONGLY recommended.\n"
1535 << "****************************************************************************\n";
1536 which = use_gpt;
srs56943c0af382010-01-15 19:19:18 -05001537 } else {
srs569408bb0da2010-02-19 17:19:55 -05001538 which = use_abort;
1539 } // if/else MBR says disk is GPT
1540 } // if GPT corrupt
srs5694e4ac11e2009-08-31 10:13:04 -04001541
1542 if (which == use_new)
srs5694fed16d02010-01-27 23:03:40 -05001543 cout << "Creating new GPT entries.\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001544
1545 return which;
1546} // UseWhichPartitions()
1547
srs569408bb0da2010-02-19 17:19:55 -05001548// Convert MBR partition table into GPT form.
1549void GPTData::XFormPartitions(void) {
srs5694e4ac11e2009-08-31 10:13:04 -04001550 int i, numToConvert;
1551 uint8_t origType;
srs5694e4ac11e2009-08-31 10:13:04 -04001552
1553 // Clear out old data & prepare basics....
1554 ClearGPTData();
1555
1556 // Convert the smaller of the # of GPT or MBR partitions
srs56940283dae2010-04-28 16:44:34 -04001557 if (numParts > MAX_MBR_PARTS)
srs5694978041c2009-09-21 20:51:47 -04001558 numToConvert = MAX_MBR_PARTS;
srs5694e4ac11e2009-08-31 10:13:04 -04001559 else
srs56940283dae2010-04-28 16:44:34 -04001560 numToConvert = numParts;
srs5694e4ac11e2009-08-31 10:13:04 -04001561
1562 for (i = 0; i < numToConvert; i++) {
1563 origType = protectiveMBR.GetType(i);
1564 // don't waste CPU time trying to convert extended, hybrid protective, or
1565 // null (non-existent) partitions
srs5694e35eb1b2009-09-14 00:29:34 -04001566 if ((origType != 0x05) && (origType != 0x0f) && (origType != 0x85) &&
srs56946699b012010-02-04 00:55:30 -05001567 (origType != 0x00) && (origType != 0xEE))
srs5694e4ac11e2009-08-31 10:13:04 -04001568 partitions[i] = protectiveMBR.AsGPT(i);
1569 } // for
1570
1571 // Convert MBR into protective MBR
1572 protectiveMBR.MakeProtectiveMBR();
1573
1574 // Record that all original CRCs were OK so as not to raise flags
1575 // when doing a disk verification
1576 mainCrcOk = secondCrcOk = mainPartsCrcOk = secondPartsCrcOk = 1;
srs5694e4ac11e2009-08-31 10:13:04 -04001577} // GPTData::XFormPartitions()
1578
1579// Transforms BSD disklabel on the specified partition (numbered from 0).
srs569408bb0da2010-02-19 17:19:55 -05001580// If an invalid partition number is given, the program does nothing.
srs5694e4ac11e2009-08-31 10:13:04 -04001581// Returns the number of new partitions created.
srs569408bb0da2010-02-19 17:19:55 -05001582int GPTData::XFormDisklabel(uint32_t partNum) {
1583 uint32_t low, high;
srs5694e4ac11e2009-08-31 10:13:04 -04001584 int goOn = 1, numDone = 0;
1585 BSDData disklabel;
1586
srs569408bb0da2010-02-19 17:19:55 -05001587 if (GetPartRange(&low, &high) == 0) {
1588 goOn = 0;
1589 cout << "No partitions!\n";
1590 } // if
1591 if (partNum > high) {
1592 goOn = 0;
1593 cout << "Specified partition is invalid!\n";
1594 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04001595
srs569408bb0da2010-02-19 17:19:55 -05001596 // If all is OK, read the disklabel and convert it.
1597 if (goOn) {
1598 goOn = disklabel.ReadBSDData(&myDisk, partitions[partNum].GetFirstLBA(),
1599 partitions[partNum].GetLastLBA());
1600 if ((goOn) && (disklabel.IsDisklabel())) {
1601 numDone = XFormDisklabel(&disklabel);
1602 if (numDone == 1)
1603 cout << "Converted 1 BSD partition.\n";
1604 else
1605 cout << "Converted " << numDone << " BSD partitions.\n";
1606 } else {
1607 cout << "Unable to convert partitions! Unrecognized BSD disklabel.\n";
1608 } // if/else
1609 } // if
1610 if (numDone > 0) { // converted partitions; delete carrier
1611 partitions[partNum].BlankPartition();
1612 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04001613 return numDone;
srs569455d92612010-03-07 22:16:07 -05001614} // GPTData::XFormDisklabel(uint32_t i)
srs5694e4ac11e2009-08-31 10:13:04 -04001615
1616// Transform the partitions on an already-loaded BSD disklabel...
srs569408bb0da2010-02-19 17:19:55 -05001617int GPTData::XFormDisklabel(BSDData* disklabel) {
1618 int i, partNum = 0, numDone = 0;
srs5694e4ac11e2009-08-31 10:13:04 -04001619
srs569408bb0da2010-02-19 17:19:55 -05001620 if (disklabel->IsDisklabel()) {
srs5694e4ac11e2009-08-31 10:13:04 -04001621 for (i = 0; i < disklabel->GetNumParts(); i++) {
srs569408bb0da2010-02-19 17:19:55 -05001622 partNum = FindFirstFreePart();
1623 if (partNum >= 0) {
1624 partitions[partNum] = disklabel->AsGPT(i);
1625 if (partitions[partNum].IsUsed())
1626 numDone++;
1627 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04001628 } // for
srs569408bb0da2010-02-19 17:19:55 -05001629 if (partNum == -1)
1630 cerr << "Warning! Too many partitions to convert!\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001631 } // if
1632
1633 // Record that all original CRCs were OK so as not to raise flags
1634 // when doing a disk verification
1635 mainCrcOk = secondCrcOk = mainPartsCrcOk = secondPartsCrcOk = 1;
1636
1637 return numDone;
1638} // GPTData::XFormDisklabel(BSDData* disklabel)
1639
srs569408bb0da2010-02-19 17:19:55 -05001640// Add one GPT partition to MBR. Used by PartsToMBR() functions. Created
1641// partition has the active/bootable flag UNset and uses the GPT fdisk
1642// type code divided by 0x0100 as the MBR type code.
1643// Returns 1 if operation was 100% successful, 0 if there were ANY
1644// problems.
srs5694978041c2009-09-21 20:51:47 -04001645int GPTData::OnePartToMBR(uint32_t gptPart, int mbrPart) {
srs569408bb0da2010-02-19 17:19:55 -05001646 int allOK = 1;
srs5694fed16d02010-01-27 23:03:40 -05001647
srs5694978041c2009-09-21 20:51:47 -04001648 if ((mbrPart < 0) || (mbrPart > 3)) {
srs5694fed16d02010-01-27 23:03:40 -05001649 cout << "MBR partition " << mbrPart + 1 << " is out of range; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001650 allOK = 0;
1651 } // if
srs56940283dae2010-04-28 16:44:34 -04001652 if (gptPart >= numParts) {
srs5694fed16d02010-01-27 23:03:40 -05001653 cout << "GPT partition " << gptPart + 1 << " is out of range; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001654 allOK = 0;
1655 } // if
1656 if (allOK && (partitions[gptPart].GetLastLBA() == UINT64_C(0))) {
srs5694fed16d02010-01-27 23:03:40 -05001657 cout << "GPT partition " << gptPart + 1 << " is undefined; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001658 allOK = 0;
1659 } // if
1660 if (allOK && (partitions[gptPart].GetFirstLBA() <= UINT32_MAX) &&
1661 (partitions[gptPart].GetLengthLBA() <= UINT32_MAX)) {
1662 if (partitions[gptPart].GetLastLBA() > UINT32_MAX) {
srs5694fed16d02010-01-27 23:03:40 -05001663 cout << "Caution: Partition end point past 32-bit pointer boundary;"
1664 << " some OSes may\nreact strangely.\n";
srs569408bb0da2010-02-19 17:19:55 -05001665 } // if
srs5694978041c2009-09-21 20:51:47 -04001666 protectiveMBR.MakePart(mbrPart, (uint32_t) partitions[gptPart].GetFirstLBA(),
srs569408bb0da2010-02-19 17:19:55 -05001667 (uint32_t) partitions[gptPart].GetLengthLBA(),
1668 partitions[gptPart].GetHexType() / 256, 0);
srs5694978041c2009-09-21 20:51:47 -04001669 } else { // partition out of range
srs569408bb0da2010-02-19 17:19:55 -05001670 if (allOK) // Display only if "else" triggered by out-of-bounds condition
1671 cout << "Partition " << gptPart + 1 << " begins beyond the 32-bit pointer limit of MBR "
1672 << "partitions, or is\n too big; omitting it.\n";
srs5694978041c2009-09-21 20:51:47 -04001673 allOK = 0;
1674 } // if/else
1675 return allOK;
1676} // GPTData::OnePartToMBR()
1677
srs5694e4ac11e2009-08-31 10:13:04 -04001678
1679/**********************************************************************
1680 * *
1681 * Functions that adjust GPT data structures WITHOUT user interaction *
1682 * (they may display information for the user's benefit, though) *
1683 * *
1684 **********************************************************************/
1685
1686// Resizes GPT to specified number of entries. Creates a new table if
srs5694706e5122012-01-21 13:47:24 -05001687// necessary, copies data if it already exists. If fillGPTSectors is 1
1688// (the default), rounds numEntries to fill all the sectors necessary to
1689// hold the GPT.
1690// Returns 1 if all goes well, 0 if an error is encountered.
1691int GPTData::SetGPTSize(uint32_t numEntries, int fillGPTSectors) {
srs569408bb0da2010-02-19 17:19:55 -05001692 GPTPart* newParts;
srs5694706e5122012-01-21 13:47:24 -05001693 uint32_t i, high, copyNum, entriesPerSector;
srs5694e4ac11e2009-08-31 10:13:04 -04001694 int allOK = 1;
1695
1696 // First, adjust numEntries upward, if necessary, to get a number
1697 // that fills the allocated sectors
srs5694706e5122012-01-21 13:47:24 -05001698 entriesPerSector = blockSize / GPT_SIZE;
1699 if (fillGPTSectors && ((numEntries % entriesPerSector) != 0)) {
srs5694fed16d02010-01-27 23:03:40 -05001700 cout << "Adjusting GPT size from " << numEntries << " to ";
srs5694706e5122012-01-21 13:47:24 -05001701 numEntries = ((numEntries / entriesPerSector) + 1) * entriesPerSector;
srs5694fed16d02010-01-27 23:03:40 -05001702 cout << numEntries << " to fill the sector\n";
srs5694e4ac11e2009-08-31 10:13:04 -04001703 } // if
1704
srs5694247657a2009-11-26 18:36:12 -05001705 // Do the work only if the # of partitions is changing. Along with being
srs569455d92612010-03-07 22:16:07 -05001706 // efficient, this prevents mucking with the location of the secondary
srs5694247657a2009-11-26 18:36:12 -05001707 // partition table, which causes problems when loading data from a RAID
1708 // array that's been expanded because this function is called when loading
1709 // data.
srs56940283dae2010-04-28 16:44:34 -04001710 if (((numEntries != numParts) || (partitions == NULL)) && (numEntries > 0)) {
srs569401f7f082011-03-15 23:53:31 -04001711 newParts = new GPTPart [numEntries];
srs5694247657a2009-11-26 18:36:12 -05001712 if (newParts != NULL) {
1713 if (partitions != NULL) { // existing partitions; copy them over
1714 GetPartRange(&i, &high);
1715 if (numEntries < (high + 1)) { // Highest entry too high for new #
srs5694fed16d02010-01-27 23:03:40 -05001716 cout << "The highest-numbered partition is " << high + 1
1717 << ", which is greater than the requested\n"
1718 << "partition table size of " << numEntries
1719 << "; cannot resize. Perhaps sorting will help.\n";
srs5694247657a2009-11-26 18:36:12 -05001720 allOK = 0;
srs5694815fb652011-03-18 12:35:56 -04001721 delete[] newParts;
srs5694247657a2009-11-26 18:36:12 -05001722 } else { // go ahead with copy
srs56940283dae2010-04-28 16:44:34 -04001723 if (numEntries < numParts)
srs5694247657a2009-11-26 18:36:12 -05001724 copyNum = numEntries;
1725 else
srs56940283dae2010-04-28 16:44:34 -04001726 copyNum = numParts;
srs5694247657a2009-11-26 18:36:12 -05001727 for (i = 0; i < copyNum; i++) {
1728 newParts[i] = partitions[i];
1729 } // for
srs569401f7f082011-03-15 23:53:31 -04001730 delete[] partitions;
srs5694247657a2009-11-26 18:36:12 -05001731 partitions = newParts;
srs5694247657a2009-11-26 18:36:12 -05001732 } // if
1733 } else { // No existing partition table; just create it
srs5694e4ac11e2009-08-31 10:13:04 -04001734 partitions = newParts;
srs5694247657a2009-11-26 18:36:12 -05001735 } // if/else existing partitions
srs56940283dae2010-04-28 16:44:34 -04001736 numParts = numEntries;
srs5694706e5122012-01-21 13:47:24 -05001737 mainHeader.firstUsableLBA = ((numEntries * GPT_SIZE) / blockSize) + (((numEntries * GPT_SIZE) % blockSize) != 0) + 2 ;
srs5694247657a2009-11-26 18:36:12 -05001738 secondHeader.firstUsableLBA = mainHeader.firstUsableLBA;
1739 MoveSecondHeaderToEnd();
1740 if (diskSize > 0)
1741 CheckGPTSize();
1742 } else { // Bad memory allocation
srs56946aae2a92011-06-10 01:16:51 -04001743 cerr << "Error allocating memory for partition table! Size is unchanged!\n";
srs5694247657a2009-11-26 18:36:12 -05001744 allOK = 0;
1745 } // if/else
srs5694e4ac11e2009-08-31 10:13:04 -04001746 } // if/else
srs56940283dae2010-04-28 16:44:34 -04001747 mainHeader.numParts = numParts;
1748 secondHeader.numParts = numParts;
srs5694e4ac11e2009-08-31 10:13:04 -04001749 return (allOK);
1750} // GPTData::SetGPTSize()
1751
1752// Blank the partition array
1753void GPTData::BlankPartitions(void) {
1754 uint32_t i;
1755
srs56940283dae2010-04-28 16:44:34 -04001756 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04001757 partitions[i].BlankPartition();
1758 } // for
1759} // GPTData::BlankPartitions()
1760
srs5694ba00fed2010-01-12 18:18:36 -05001761// Delete a partition by number. Returns 1 if successful,
1762// 0 if there was a problem. Returns 1 if partition was in
1763// range, 0 if it was out of range.
1764int GPTData::DeletePartition(uint32_t partNum) {
1765 uint64_t startSector, length;
srs56940283dae2010-04-28 16:44:34 -04001766 uint32_t low, high, numUsedParts, retval = 1;;
srs5694ba00fed2010-01-12 18:18:36 -05001767
srs56940283dae2010-04-28 16:44:34 -04001768 numUsedParts = GetPartRange(&low, &high);
1769 if ((numUsedParts > 0) && (partNum >= low) && (partNum <= high)) {
srs5694ba00fed2010-01-12 18:18:36 -05001770 // In case there's a protective MBR, look for & delete matching
1771 // MBR partition....
1772 startSector = partitions[partNum].GetFirstLBA();
1773 length = partitions[partNum].GetLengthLBA();
1774 protectiveMBR.DeleteByLocation(startSector, length);
1775
1776 // Now delete the GPT partition
1777 partitions[partNum].BlankPartition();
1778 } else {
srs5694fed16d02010-01-27 23:03:40 -05001779 cerr << "Partition number " << partNum + 1 << " out of range!\n";
srs5694ba00fed2010-01-12 18:18:36 -05001780 retval = 0;
1781 } // if/else
1782 return retval;
1783} // GPTData::DeletePartition(uint32_t partNum)
1784
srs569408bb0da2010-02-19 17:19:55 -05001785// Non-interactively create a partition.
1786// Returns 1 if the operation was successful, 0 if a problem was discovered.
srs5694e321d442010-01-29 17:44:04 -05001787uint32_t GPTData::CreatePartition(uint32_t partNum, uint64_t startSector, uint64_t endSector) {
srs5694ba00fed2010-01-12 18:18:36 -05001788 int retval = 1; // assume there'll be no problems
srs56945a081752010-09-24 20:39:41 -04001789 uint64_t origSector = startSector;
srs5694ba00fed2010-01-12 18:18:36 -05001790
1791 if (IsFreePartNum(partNum)) {
srs56945a081752010-09-24 20:39:41 -04001792 if (Align(&startSector)) {
1793 cout << "Information: Moved requested sector from " << origSector << " to "
1794 << startSector << " in\norder to align on " << sectorAlignment
1795 << "-sector boundaries.\n";
1796 } // if
srs5694ba00fed2010-01-12 18:18:36 -05001797 if (IsFree(startSector) && (startSector <= endSector)) {
1798 if (FindLastInFree(startSector) >= endSector) {
1799 partitions[partNum].SetFirstLBA(startSector);
1800 partitions[partNum].SetLastLBA(endSector);
srs56940741fa22013-01-09 12:55:40 -05001801 partitions[partNum].SetType(DEFAULT_GPT_TYPE);
srs56946699b012010-02-04 00:55:30 -05001802 partitions[partNum].RandomizeUniqueGUID();
srs5694ba00fed2010-01-12 18:18:36 -05001803 } else retval = 0; // if free space until endSector
1804 } else retval = 0; // if startSector is free
1805 } else retval = 0; // if legal partition number
1806 return retval;
1807} // GPTData::CreatePartition(partNum, startSector, endSector)
1808
srs5694e4ac11e2009-08-31 10:13:04 -04001809// Sort the GPT entries, eliminating gaps and making for a logical
srs56949a46b042011-03-15 00:34:10 -04001810// ordering.
srs5694e4ac11e2009-08-31 10:13:04 -04001811void GPTData::SortGPT(void) {
srs56949a46b042011-03-15 00:34:10 -04001812 if (numParts > 0)
srs569401f7f082011-03-15 23:53:31 -04001813 sort(partitions, partitions + numParts);
srs5694e4ac11e2009-08-31 10:13:04 -04001814} // GPTData::SortGPT()
1815
srs569408bb0da2010-02-19 17:19:55 -05001816// Swap the contents of two partitions.
1817// Returns 1 if successful, 0 if either partition is out of range
1818// (that is, not a legal number; either or both can be empty).
1819// Note that if partNum1 = partNum2 and this number is in range,
1820// it will be considered successful.
1821int GPTData::SwapPartitions(uint32_t partNum1, uint32_t partNum2) {
1822 GPTPart temp;
1823 int allOK = 1;
1824
srs56940283dae2010-04-28 16:44:34 -04001825 if ((partNum1 < numParts) && (partNum2 < numParts)) {
srs569408bb0da2010-02-19 17:19:55 -05001826 if (partNum1 != partNum2) {
1827 temp = partitions[partNum1];
1828 partitions[partNum1] = partitions[partNum2];
1829 partitions[partNum2] = temp;
1830 } // if
1831 } else allOK = 0; // partition numbers are valid
1832 return allOK;
1833} // GPTData::SwapPartitions()
1834
srs5694e4ac11e2009-08-31 10:13:04 -04001835// Set up data structures for entirely new set of partitions on the
1836// specified device. Returns 1 if OK, 0 if there were problems.
srs5694e35eb1b2009-09-14 00:29:34 -04001837// Note that this function does NOT clear the protectiveMBR data
1838// structure, since it may hold the original MBR partitions if the
1839// program was launched on an MBR disk, and those may need to be
1840// converted to GPT format.
srs5694e4ac11e2009-08-31 10:13:04 -04001841int GPTData::ClearGPTData(void) {
srs5694e35eb1b2009-09-14 00:29:34 -04001842 int goOn = 1, i;
srs5694e4ac11e2009-08-31 10:13:04 -04001843
1844 // Set up the partition table....
srs56949a46b042011-03-15 00:34:10 -04001845 delete[] partitions;
srs5694e4ac11e2009-08-31 10:13:04 -04001846 partitions = NULL;
1847 SetGPTSize(NUM_GPT_ENTRIES);
1848
1849 // Now initialize a bunch of stuff that's static....
1850 mainHeader.signature = GPT_SIGNATURE;
1851 mainHeader.revision = 0x00010000;
srs5694978041c2009-09-21 20:51:47 -04001852 mainHeader.headerSize = HEADER_SIZE;
srs5694e4ac11e2009-08-31 10:13:04 -04001853 mainHeader.reserved = 0;
1854 mainHeader.currentLBA = UINT64_C(1);
1855 mainHeader.partitionEntriesLBA = (uint64_t) 2;
1856 mainHeader.sizeOfPartitionEntries = GPT_SIZE;
1857 for (i = 0; i < GPT_RESERVED; i++) {
1858 mainHeader.reserved2[i] = '\0';
1859 } // for
srs56940873e9d2010-10-07 13:00:45 -04001860 if (blockSize > 0)
1861 sectorAlignment = DEFAULT_ALIGNMENT * SECTOR_SIZE / blockSize;
1862 else
1863 sectorAlignment = DEFAULT_ALIGNMENT;
srs5694e4ac11e2009-08-31 10:13:04 -04001864
1865 // Now some semi-static items (computed based on end of disk)
1866 mainHeader.backupLBA = diskSize - UINT64_C(1);
1867 mainHeader.lastUsableLBA = diskSize - mainHeader.firstUsableLBA;
1868
1869 // Set a unique GUID for the disk, based on random numbers
srs56946699b012010-02-04 00:55:30 -05001870 mainHeader.diskGUID.Randomize();
srs5694e4ac11e2009-08-31 10:13:04 -04001871
1872 // Copy main header to backup header
1873 RebuildSecondHeader();
1874
1875 // Blank out the partitions array....
1876 BlankPartitions();
1877
1878 // Flag all CRCs as being OK....
1879 mainCrcOk = 1;
1880 secondCrcOk = 1;
1881 mainPartsCrcOk = 1;
1882 secondPartsCrcOk = 1;
1883
1884 return (goOn);
1885} // GPTData::ClearGPTData()
1886
srs5694247657a2009-11-26 18:36:12 -05001887// Set the location of the second GPT header data to the end of the disk.
srs569464cbd172011-03-01 22:03:54 -05001888// If the disk size has actually changed, this also adjusts the protective
1889// entry in the MBR, since it's probably no longer correct.
srs5694247657a2009-11-26 18:36:12 -05001890// Used internally and called by the 'e' option on the recovery &
1891// transformation menu, to help users of RAID arrays who add disk space
srs569464cbd172011-03-01 22:03:54 -05001892// to their arrays or to adjust data structures in restore operations
1893// involving unequal-sized disks.
srs5694247657a2009-11-26 18:36:12 -05001894void GPTData::MoveSecondHeaderToEnd() {
srs56948bb78762009-11-24 15:43:49 -05001895 mainHeader.backupLBA = secondHeader.currentLBA = diskSize - UINT64_C(1);
srs569464cbd172011-03-01 22:03:54 -05001896 if (mainHeader.lastUsableLBA != diskSize - mainHeader.firstUsableLBA) {
1897 if (protectiveMBR.GetValidity() == hybrid) {
1898 protectiveMBR.OptimizeEESize();
1899 RecomputeCHS();
1900 } // if
1901 if (protectiveMBR.GetValidity() == gpt)
1902 MakeProtectiveMBR();
1903 } // if
srs56948bb78762009-11-24 15:43:49 -05001904 mainHeader.lastUsableLBA = secondHeader.lastUsableLBA = diskSize - mainHeader.firstUsableLBA;
1905 secondHeader.partitionEntriesLBA = secondHeader.lastUsableLBA + UINT64_C(1);
1906} // GPTData::FixSecondHeaderLocation()
1907
srs5694699941e2011-03-21 21:33:57 -04001908// Sets the partition's name to the specified UnicodeString without
1909// user interaction.
1910// Returns 1 on success, 0 on failure (invalid partition number).
srs56945a608532011-03-17 13:53:01 -04001911int GPTData::SetName(uint32_t partNum, const UnicodeString & theName) {
srs5694ba00fed2010-01-12 18:18:36 -05001912 int retval = 1;
srs5694fed16d02010-01-27 23:03:40 -05001913
srs5694699941e2011-03-21 21:33:57 -04001914 if (IsUsedPartNum(partNum))
srs5694fed16d02010-01-27 23:03:40 -05001915 partitions[partNum].SetName(theName);
srs5694699941e2011-03-21 21:33:57 -04001916 else
1917 retval = 0;
srs5694ba00fed2010-01-12 18:18:36 -05001918
1919 return retval;
srs5694e4ac11e2009-08-31 10:13:04 -04001920} // GPTData::SetName
1921
1922// Set the disk GUID to the specified value. Note that the header CRCs must
1923// be recomputed after calling this function.
1924void GPTData::SetDiskGUID(GUIDData newGUID) {
1925 mainHeader.diskGUID = newGUID;
1926 secondHeader.diskGUID = newGUID;
1927} // SetDiskGUID()
1928
1929// Set the unique GUID of the specified partition. Returns 1 on
1930// successful completion, 0 if there were problems (invalid
1931// partition number).
1932int GPTData::SetPartitionGUID(uint32_t pn, GUIDData theGUID) {
1933 int retval = 0;
1934
srs56940283dae2010-04-28 16:44:34 -04001935 if (pn < numParts) {
srs5694e69e6802012-01-20 22:37:12 -05001936 if (partitions[pn].IsUsed()) {
srs5694e4ac11e2009-08-31 10:13:04 -04001937 partitions[pn].SetUniqueGUID(theGUID);
1938 retval = 1;
1939 } // if
1940 } // if
1941 return retval;
1942} // GPTData::SetPartitionGUID()
1943
srs56949ba54212010-05-18 23:24:02 -04001944// Set new random GUIDs for the disk and all partitions. Intended to be used
1945// after disk cloning or similar operations that don't randomize the GUIDs.
1946void GPTData::RandomizeGUIDs(void) {
1947 uint32_t i;
1948
1949 mainHeader.diskGUID.Randomize();
1950 secondHeader.diskGUID = mainHeader.diskGUID;
1951 for (i = 0; i < numParts; i++)
1952 if (partitions[i].IsUsed())
1953 partitions[i].RandomizeUniqueGUID();
1954} // GPTData::RandomizeGUIDs()
1955
srs5694ba00fed2010-01-12 18:18:36 -05001956// Change partition type code non-interactively. Returns 1 if
1957// successful, 0 if not....
srs5694327129e2010-09-22 01:07:31 -04001958int GPTData::ChangePartType(uint32_t partNum, PartType theGUID) {
1959 int retval = 1;
1960
1961 if (!IsFreePartNum(partNum)) {
1962 partitions[partNum].SetType(theGUID);
1963 } else retval = 0;
1964 return retval;
1965} // GPTData::ChangePartType()
1966
srs56949ba54212010-05-18 23:24:02 -04001967// Recompute the CHS values of all the MBR partitions. Used to reset
1968// CHS values that some BIOSes require, despite the fact that the
1969// resulting CHS values violate the GPT standard.
1970void GPTData::RecomputeCHS(void) {
1971 int i;
1972
1973 for (i = 0; i < 4; i++)
1974 protectiveMBR.RecomputeCHS(i);
1975} // GPTData::RecomputeCHS()
1976
srs56941d1448a2009-12-31 21:20:19 -05001977// Adjust sector number so that it falls on a sector boundary that's a
1978// multiple of sectorAlignment. This is done to improve the performance
1979// of Western Digital Advanced Format disks and disks with similar
1980// technology from other companies, which use 4096-byte sectors
1981// internally although they translate to 512-byte sectors for the
1982// benefit of the OS. If partitions aren't properly aligned on these
1983// disks, some filesystem data structures can span multiple physical
1984// sectors, degrading performance. This function should be called
1985// only on the FIRST sector of the partition, not the last!
1986// This function returns 1 if the alignment was altered, 0 if it
1987// was unchanged.
1988int GPTData::Align(uint64_t* sector) {
1989 int retval = 0, sectorOK = 0;
srs569400b6d7a2011-06-26 22:40:06 -04001990 uint64_t earlier, later, testSector;
srs56941d1448a2009-12-31 21:20:19 -05001991
1992 if ((*sector % sectorAlignment) != 0) {
srs56941d1448a2009-12-31 21:20:19 -05001993 earlier = (*sector / sectorAlignment) * sectorAlignment;
1994 later = earlier + (uint64_t) sectorAlignment;
1995
1996 // Check to see that every sector between the earlier one and the
1997 // requested one is clear, and that it's not too early....
1998 if (earlier >= mainHeader.firstUsableLBA) {
srs56941d1448a2009-12-31 21:20:19 -05001999 sectorOK = 1;
2000 testSector = earlier;
2001 do {
2002 sectorOK = IsFree(testSector++);
2003 } while ((sectorOK == 1) && (testSector < *sector));
2004 if (sectorOK == 1) {
2005 *sector = earlier;
srs56945a081752010-09-24 20:39:41 -04002006 retval = 1;
srs56941d1448a2009-12-31 21:20:19 -05002007 } // if
2008 } // if firstUsableLBA check
2009
2010 // If couldn't move the sector earlier, try to move it later instead....
2011 if ((sectorOK != 1) && (later <= mainHeader.lastUsableLBA)) {
2012 sectorOK = 1;
2013 testSector = later;
2014 do {
2015 sectorOK = IsFree(testSector--);
2016 } while ((sectorOK == 1) && (testSector > *sector));
2017 if (sectorOK == 1) {
2018 *sector = later;
srs56945a081752010-09-24 20:39:41 -04002019 retval = 1;
srs56941d1448a2009-12-31 21:20:19 -05002020 } // if
2021 } // if
srs56941d1448a2009-12-31 21:20:19 -05002022 } // if
2023 return retval;
2024} // GPTData::Align()
2025
srs5694e4ac11e2009-08-31 10:13:04 -04002026/********************************************************
2027 * *
2028 * Functions that return data about GPT data structures *
2029 * (most of these are inline in gpt.h) *
2030 * *
2031 ********************************************************/
2032
2033// Find the low and high used partition numbers (numbered from 0).
2034// Return value is the number of partitions found. Note that the
2035// *low and *high values are both set to 0 when no partitions
2036// are found, as well as when a single partition in the first
2037// position exists. Thus, the return value is the only way to
2038// tell when no partitions exist.
2039int GPTData::GetPartRange(uint32_t *low, uint32_t *high) {
2040 uint32_t i;
2041 int numFound = 0;
2042
srs56940283dae2010-04-28 16:44:34 -04002043 *low = numParts + 1; // code for "not found"
srs5694e4ac11e2009-08-31 10:13:04 -04002044 *high = 0;
srs56949a46b042011-03-15 00:34:10 -04002045 for (i = 0; i < numParts; i++) {
srs5694e69e6802012-01-20 22:37:12 -05002046 if (partitions[i].IsUsed()) { // it exists
srs56949a46b042011-03-15 00:34:10 -04002047 *high = i; // since we're counting up, set the high value
2048 // Set the low value only if it's not yet found...
2049 if (*low == (numParts + 1)) *low = i;
2050 numFound++;
2051 } // if
2052 } // for
srs5694e4ac11e2009-08-31 10:13:04 -04002053
2054 // Above will leave *low pointing to its "not found" value if no partitions
2055 // are defined, so reset to 0 if this is the case....
srs56940283dae2010-04-28 16:44:34 -04002056 if (*low == (numParts + 1))
srs5694e4ac11e2009-08-31 10:13:04 -04002057 *low = 0;
2058 return numFound;
2059} // GPTData::GetPartRange()
2060
srs569408bb0da2010-02-19 17:19:55 -05002061// Returns the value of the first free partition, or -1 if none is
2062// unused.
2063int GPTData::FindFirstFreePart(void) {
2064 int i = 0;
2065
2066 if (partitions != NULL) {
srs56949a46b042011-03-15 00:34:10 -04002067 while ((i < (int) numParts) && (partitions[i].IsUsed()))
srs569408bb0da2010-02-19 17:19:55 -05002068 i++;
srs56940283dae2010-04-28 16:44:34 -04002069 if (i >= (int) numParts)
srs569408bb0da2010-02-19 17:19:55 -05002070 i = -1;
2071 } else i = -1;
2072 return i;
2073} // GPTData::FindFirstFreePart()
2074
srs5694978041c2009-09-21 20:51:47 -04002075// Returns the number of defined partitions.
2076uint32_t GPTData::CountParts(void) {
srs5694e321d442010-01-29 17:44:04 -05002077 uint32_t i, counted = 0;
srs5694978041c2009-09-21 20:51:47 -04002078
srs56940283dae2010-04-28 16:44:34 -04002079 for (i = 0; i < numParts; i++) {
srs569408bb0da2010-02-19 17:19:55 -05002080 if (partitions[i].IsUsed())
srs5694978041c2009-09-21 20:51:47 -04002081 counted++;
2082 } // for
2083 return counted;
2084} // GPTData::CountParts()
2085
srs5694e4ac11e2009-08-31 10:13:04 -04002086/****************************************************
2087 * *
2088 * Functions that return data about disk free space *
2089 * *
2090 ****************************************************/
2091
2092// Find the first available block after the starting point; returns 0 if
2093// there are no available blocks left
2094uint64_t GPTData::FindFirstAvailable(uint64_t start) {
2095 uint64_t first;
2096 uint32_t i;
2097 int firstMoved = 0;
2098
2099 // Begin from the specified starting point or from the first usable
2100 // LBA, whichever is greater...
2101 if (start < mainHeader.firstUsableLBA)
2102 first = mainHeader.firstUsableLBA;
2103 else
2104 first = start;
2105
2106 // ...now search through all partitions; if first is within an
2107 // existing partition, move it to the next sector after that
2108 // partition and repeat. If first was moved, set firstMoved
2109 // flag; repeat until firstMoved is not set, so as to catch
2110 // cases where partitions are out of sequential order....
2111 do {
2112 firstMoved = 0;
srs56940283dae2010-04-28 16:44:34 -04002113 for (i = 0; i < numParts; i++) {
srs5694e69e6802012-01-20 22:37:12 -05002114 if ((partitions[i].IsUsed()) && (first >= partitions[i].GetFirstLBA()) &&
srs569455d92612010-03-07 22:16:07 -05002115 (first <= partitions[i].GetLastLBA())) { // in existing part.
srs5694e4ac11e2009-08-31 10:13:04 -04002116 first = partitions[i].GetLastLBA() + 1;
2117 firstMoved = 1;
srs569455d92612010-03-07 22:16:07 -05002118 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002119 } // for
2120 } while (firstMoved == 1);
2121 if (first > mainHeader.lastUsableLBA)
2122 first = 0;
2123 return (first);
2124} // GPTData::FindFirstAvailable()
2125
2126// Finds the first available sector in the largest block of unallocated
2127// space on the disk. Returns 0 if there are no available blocks left
2128uint64_t GPTData::FindFirstInLargest(void) {
srs5694e35eb1b2009-09-14 00:29:34 -04002129 uint64_t start, firstBlock, lastBlock, segmentSize, selectedSize = 0, selectedSegment = 0;
srs5694e4ac11e2009-08-31 10:13:04 -04002130
2131 start = 0;
2132 do {
2133 firstBlock = FindFirstAvailable(start);
2134 if (firstBlock != UINT32_C(0)) { // something's free...
2135 lastBlock = FindLastInFree(firstBlock);
2136 segmentSize = lastBlock - firstBlock + UINT32_C(1);
2137 if (segmentSize > selectedSize) {
2138 selectedSize = segmentSize;
2139 selectedSegment = firstBlock;
2140 } // if
2141 start = lastBlock + 1;
2142 } // if
2143 } while (firstBlock != 0);
2144 return selectedSegment;
2145} // GPTData::FindFirstInLargest()
2146
srs5694cb76c672010-02-11 22:22:22 -05002147// Find the last available block on the disk.
srs5694f5dfbfa2013-02-14 20:47:14 -05002148// Returns 0 if there are no available sectors
srs5694cb76c672010-02-11 22:22:22 -05002149uint64_t GPTData::FindLastAvailable(void) {
srs5694e4ac11e2009-08-31 10:13:04 -04002150 uint64_t last;
2151 uint32_t i;
2152 int lastMoved = 0;
2153
2154 // Start by assuming the last usable LBA is available....
2155 last = mainHeader.lastUsableLBA;
2156
2157 // ...now, similar to algorithm in FindFirstAvailable(), search
2158 // through all partitions, moving last when it's in an existing
2159 // partition. Set the lastMoved flag so we repeat to catch cases
2160 // where partitions are out of logical order.
2161 do {
2162 lastMoved = 0;
srs56940283dae2010-04-28 16:44:34 -04002163 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04002164 if ((last >= partitions[i].GetFirstLBA()) &&
srs569455d92612010-03-07 22:16:07 -05002165 (last <= partitions[i].GetLastLBA())) { // in existing part.
srs5694e4ac11e2009-08-31 10:13:04 -04002166 last = partitions[i].GetFirstLBA() - 1;
2167 lastMoved = 1;
srs569455d92612010-03-07 22:16:07 -05002168 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002169 } // for
2170 } while (lastMoved == 1);
2171 if (last < mainHeader.firstUsableLBA)
2172 last = 0;
2173 return (last);
2174} // GPTData::FindLastAvailable()
2175
2176// Find the last available block in the free space pointed to by start.
2177uint64_t GPTData::FindLastInFree(uint64_t start) {
2178 uint64_t nearestStart;
2179 uint32_t i;
2180
2181 nearestStart = mainHeader.lastUsableLBA;
srs56940283dae2010-04-28 16:44:34 -04002182 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04002183 if ((nearestStart > partitions[i].GetFirstLBA()) &&
srs569455d92612010-03-07 22:16:07 -05002184 (partitions[i].GetFirstLBA() > start)) {
srs5694e4ac11e2009-08-31 10:13:04 -04002185 nearestStart = partitions[i].GetFirstLBA() - 1;
srs569455d92612010-03-07 22:16:07 -05002186 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002187 } // for
2188 return (nearestStart);
2189} // GPTData::FindLastInFree()
2190
2191// Finds the total number of free blocks, the number of segments in which
2192// they reside, and the size of the largest of those segments
srs5694e321d442010-01-29 17:44:04 -05002193uint64_t GPTData::FindFreeBlocks(uint32_t *numSegments, uint64_t *largestSegment) {
srs5694e4ac11e2009-08-31 10:13:04 -04002194 uint64_t start = UINT64_C(0); // starting point for each search
2195 uint64_t totalFound = UINT64_C(0); // running total
2196 uint64_t firstBlock; // first block in a segment
2197 uint64_t lastBlock; // last block in a segment
2198 uint64_t segmentSize; // size of segment in blocks
srs5694e321d442010-01-29 17:44:04 -05002199 uint32_t num = 0;
srs5694e4ac11e2009-08-31 10:13:04 -04002200
2201 *largestSegment = UINT64_C(0);
srs5694c54e9b42010-05-01 21:04:23 -04002202 if (diskSize > 0) {
2203 do {
2204 firstBlock = FindFirstAvailable(start);
2205 if (firstBlock != UINT64_C(0)) { // something's free...
2206 lastBlock = FindLastInFree(firstBlock);
2207 segmentSize = lastBlock - firstBlock + UINT64_C(1);
2208 if (segmentSize > *largestSegment) {
2209 *largestSegment = segmentSize;
2210 } // if
2211 totalFound += segmentSize;
2212 num++;
2213 start = lastBlock + 1;
srs5694e4ac11e2009-08-31 10:13:04 -04002214 } // if
srs5694c54e9b42010-05-01 21:04:23 -04002215 } while (firstBlock != 0);
2216 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002217 *numSegments = num;
2218 return totalFound;
2219} // GPTData::FindFreeBlocks()
2220
srs569455d92612010-03-07 22:16:07 -05002221// Returns 1 if sector is unallocated, 0 if it's allocated to a partition.
2222// If it's allocated, return the partition number to which it's allocated
2223// in partNum, if that variable is non-NULL. (A value of UINT32_MAX is
2224// returned in partNum if the sector is in use by basic GPT data structures.)
2225int GPTData::IsFree(uint64_t sector, uint32_t *partNum) {
srs5694e4ac11e2009-08-31 10:13:04 -04002226 int isFree = 1;
2227 uint32_t i;
2228
srs56940283dae2010-04-28 16:44:34 -04002229 for (i = 0; i < numParts; i++) {
srs5694e4ac11e2009-08-31 10:13:04 -04002230 if ((sector >= partitions[i].GetFirstLBA()) &&
2231 (sector <= partitions[i].GetLastLBA())) {
2232 isFree = 0;
srs569455d92612010-03-07 22:16:07 -05002233 if (partNum != NULL)
2234 *partNum = i;
srs569408bb0da2010-02-19 17:19:55 -05002235 } // if
srs5694e4ac11e2009-08-31 10:13:04 -04002236 } // for
srs5694e35eb1b2009-09-14 00:29:34 -04002237 if ((sector < mainHeader.firstUsableLBA) ||
srs5694e4ac11e2009-08-31 10:13:04 -04002238 (sector > mainHeader.lastUsableLBA)) {
2239 isFree = 0;
srs569455d92612010-03-07 22:16:07 -05002240 if (partNum != NULL)
2241 *partNum = UINT32_MAX;
srs569408bb0da2010-02-19 17:19:55 -05002242 } // if
2243 return (isFree);
srs5694e4ac11e2009-08-31 10:13:04 -04002244} // GPTData::IsFree()
2245
srs5694815fb652011-03-18 12:35:56 -04002246// Returns 1 if partNum is unused AND if it's a legal value.
srs5694ba00fed2010-01-12 18:18:36 -05002247int GPTData::IsFreePartNum(uint32_t partNum) {
srs569401f7f082011-03-15 23:53:31 -04002248 return ((partNum < numParts) && (partitions != NULL) &&
2249 (!partitions[partNum].IsUsed()));
srs5694ba00fed2010-01-12 18:18:36 -05002250} // GPTData::IsFreePartNum()
2251
srs5694815fb652011-03-18 12:35:56 -04002252// Returns 1 if partNum is in use.
2253int GPTData::IsUsedPartNum(uint32_t partNum) {
2254 return ((partNum < numParts) && (partitions != NULL) &&
2255 (partitions[partNum].IsUsed()));
2256} // GPTData::IsUsedPartNum()
srs5694a8582cf2010-03-19 14:21:59 -04002257
2258/***********************************************************
2259 * *
2260 * Change how functions work or return information on them *
2261 * *
2262 ***********************************************************/
2263
2264// Set partition alignment value; partitions will begin on multiples of
2265// the specified value
2266void GPTData::SetAlignment(uint32_t n) {
srs56940873e9d2010-10-07 13:00:45 -04002267 if (n > 0)
2268 sectorAlignment = n;
2269 else
2270 cerr << "Attempt to set partition alignment to 0!\n";
srs5694a8582cf2010-03-19 14:21:59 -04002271} // GPTData::SetAlignment()
2272
2273// Compute sector alignment based on the current partitions (if any). Each
2274// partition's starting LBA is examined, and if it's divisible by a power-of-2
srs56940873e9d2010-10-07 13:00:45 -04002275// value less than or equal to the DEFAULT_ALIGNMENT value (adjusted for the
2276// sector size), but not by the previously-located alignment value, then the
2277// alignment value is adjusted down. If the computed alignment is less than 8
2278// and the disk is bigger than SMALLEST_ADVANCED_FORMAT, resets it to 8. This
srs5694d8eed462012-12-15 01:55:21 -05002279// is a safety measure for Advanced Format drives. If no partitions are
2280// defined, the alignment value is set to DEFAULT_ALIGNMENT (2048) (or an
srs56940873e9d2010-10-07 13:00:45 -04002281// adjustment of that based on the current sector size). The result is that new
srs56948a4ddfc2010-03-21 19:05:49 -04002282// drives are aligned to 2048-sector multiples but the program won't complain
2283// about other alignments on existing disks unless a smaller-than-8 alignment
srs5694d8eed462012-12-15 01:55:21 -05002284// is used on big disks (as safety for Advanced Format drives).
srs5694a8582cf2010-03-19 14:21:59 -04002285// Returns the computed alignment value.
2286uint32_t GPTData::ComputeAlignment(void) {
2287 uint32_t i = 0, found, exponent = 31;
srs5694ab4b0432010-09-25 20:39:52 -04002288 uint32_t align = DEFAULT_ALIGNMENT;
srs5694a8582cf2010-03-19 14:21:59 -04002289
srs56940873e9d2010-10-07 13:00:45 -04002290 if (blockSize > 0)
2291 align = DEFAULT_ALIGNMENT * SECTOR_SIZE / blockSize;
2292 exponent = (uint32_t) log2(align);
srs56940283dae2010-04-28 16:44:34 -04002293 for (i = 0; i < numParts; i++) {
srs5694a8582cf2010-03-19 14:21:59 -04002294 if (partitions[i].IsUsed()) {
2295 found = 0;
2296 while (!found) {
srs56940873e9d2010-10-07 13:00:45 -04002297 align = UINT64_C(1) << exponent;
srs5694a8582cf2010-03-19 14:21:59 -04002298 if ((partitions[i].GetFirstLBA() % align) == 0) {
2299 found = 1;
2300 } else {
2301 exponent--;
2302 } // if/else
2303 } // while
2304 } // if
2305 } // for
srs56940873e9d2010-10-07 13:00:45 -04002306 if ((align < MIN_AF_ALIGNMENT) && (diskSize >= SMALLEST_ADVANCED_FORMAT))
2307 align = MIN_AF_ALIGNMENT;
2308 sectorAlignment = align;
srs5694a8582cf2010-03-19 14:21:59 -04002309 return align;
2310} // GPTData::ComputeAlignment()
2311
srs5694e4ac11e2009-08-31 10:13:04 -04002312/********************************
2313 * *
2314 * Endianness support functions *
2315 * *
2316 ********************************/
2317
srs56942a9f5da2009-08-26 00:48:01 -04002318void GPTData::ReverseHeaderBytes(struct GPTHeader* header) {
srs5694221e0872009-08-29 15:00:31 -04002319 ReverseBytes(&header->signature, 8);
2320 ReverseBytes(&header->revision, 4);
2321 ReverseBytes(&header->headerSize, 4);
2322 ReverseBytes(&header->headerCRC, 4);
2323 ReverseBytes(&header->reserved, 4);
2324 ReverseBytes(&header->currentLBA, 8);
2325 ReverseBytes(&header->backupLBA, 8);
2326 ReverseBytes(&header->firstUsableLBA, 8);
2327 ReverseBytes(&header->lastUsableLBA, 8);
2328 ReverseBytes(&header->partitionEntriesLBA, 8);
2329 ReverseBytes(&header->numParts, 4);
2330 ReverseBytes(&header->sizeOfPartitionEntries, 4);
2331 ReverseBytes(&header->partitionEntriesCRC, 4);
srs569408bb0da2010-02-19 17:19:55 -05002332 ReverseBytes(header->reserved2, GPT_RESERVED);
srs56942a9f5da2009-08-26 00:48:01 -04002333} // GPTData::ReverseHeaderBytes()
2334
srs56940283dae2010-04-28 16:44:34 -04002335// Reverse byte order for all partitions.
srs56942a9f5da2009-08-26 00:48:01 -04002336void GPTData::ReversePartitionBytes() {
2337 uint32_t i;
2338
srs56940283dae2010-04-28 16:44:34 -04002339 for (i = 0; i < numParts; i++) {
srs5694221e0872009-08-29 15:00:31 -04002340 partitions[i].ReversePartBytes();
srs56942a9f5da2009-08-26 00:48:01 -04002341 } // for
2342} // GPTData::ReversePartitionBytes()
2343
srs56949ddc14b2010-08-22 22:44:42 -04002344// Validate partition number
2345bool GPTData::ValidPartNum (const uint32_t partNum) {
2346 if (partNum >= numParts) {
srs56945a081752010-09-24 20:39:41 -04002347 cerr << "Partition number out of range: " << partNum << "\n";
srs56949ddc14b2010-08-22 22:44:42 -04002348 return false;
2349 } // if
2350 return true;
2351} // GPTData::ValidPartNum
2352
srs56945a081752010-09-24 20:39:41 -04002353// Return a single partition for inspection (not modification!) by other
2354// functions.
2355const GPTPart & GPTData::operator[](uint32_t partNum) const {
2356 if (partNum >= numParts) {
srs5694815fb652011-03-18 12:35:56 -04002357 cerr << "Partition number out of range (" << partNum << " requested, but only "
2358 << numParts << " available)\n";
2359 exit(1);
2360 } // if
2361 if (partitions == NULL) {
2362 cerr << "No partitions defined in GPTData::operator[]; fatal error!\n";
2363 exit(1);
srs56945a081752010-09-24 20:39:41 -04002364 } // if
2365 return partitions[partNum];
2366} // operator[]
2367
2368// Return (not for modification!) the disk's GUID value
2369const GUIDData & GPTData::GetDiskGUID(void) const {
2370 return mainHeader.diskGUID;
2371} // GPTData::GetDiskGUID()
2372
srs56949ddc14b2010-08-22 22:44:42 -04002373// Manage attributes for a partition, based on commands passed to this function.
2374// (Function is non-interactive.)
2375// Returns 1 if a modification command succeeded, 0 if the command should not have
2376// modified data, and -1 if a modification command failed.
2377int GPTData::ManageAttributes(int partNum, const string & command, const string & bits) {
2378 int retval = 0;
2379 Attributes theAttr;
2380
Roderick W. Smith24bba6e2013-10-12 19:07:16 -04002381 if (partNum >= (int) numParts) {
2382 cerr << "Invalid partition number (" << partNum + 1 << ")\n";
2383 retval = -1;
srs56949ddc14b2010-08-22 22:44:42 -04002384 } else {
Roderick W. Smith24bba6e2013-10-12 19:07:16 -04002385 if (command == "show") {
2386 ShowAttributes(partNum);
2387 } else if (command == "get") {
2388 GetAttribute(partNum, bits);
srs56949ddc14b2010-08-22 22:44:42 -04002389 } else {
Roderick W. Smith24bba6e2013-10-12 19:07:16 -04002390 theAttr = partitions[partNum].GetAttributes();
2391 if (theAttr.OperateOnAttributes(partNum, command, bits)) {
2392 partitions[partNum].SetAttributes(theAttr.GetAttributes());
2393 retval = 1;
2394 } else {
2395 retval = -1;
2396 } // if/else
2397 } // if/elseif/else
2398 } // if/else invalid partition #
srs56949ddc14b2010-08-22 22:44:42 -04002399
2400 return retval;
2401} // GPTData::ManageAttributes()
2402
2403// Show all attributes for a specified partition....
2404void GPTData::ShowAttributes(const uint32_t partNum) {
Roderick W. Smith24bba6e2013-10-12 19:07:16 -04002405 if ((partNum < numParts) && partitions[partNum].IsUsed())
srs5694e69e6802012-01-20 22:37:12 -05002406 partitions[partNum].ShowAttributes(partNum);
srs56949ddc14b2010-08-22 22:44:42 -04002407} // GPTData::ShowAttributes
2408
2409// Show whether a single attribute bit is set (terse output)...
2410void GPTData::GetAttribute(const uint32_t partNum, const string& attributeBits) {
Roderick W. Smith24bba6e2013-10-12 19:07:16 -04002411 if (partNum < numParts)
2412 partitions[partNum].GetAttributes().OperateOnAttributes(partNum, "get", attributeBits);
srs56949ddc14b2010-08-22 22:44:42 -04002413} // GPTData::GetAttribute
2414
2415
srs56942a9f5da2009-08-26 00:48:01 -04002416/******************************************
2417 * *
2418 * Additional non-class support functions *
2419 * *
2420 ******************************************/
2421
srs5694e7b4ff92009-08-18 13:16:10 -04002422// Check to be sure that data type sizes are correct. The basic types (uint*_t) should
2423// never fail these tests, but the struct types may fail depending on compile options.
2424// Specifically, the -fpack-struct option to gcc may be required to ensure proper structure
2425// sizes.
2426int SizesOK(void) {
2427 int allOK = 1;
srs5694e7b4ff92009-08-18 13:16:10 -04002428
2429 if (sizeof(uint8_t) != 1) {
srs5694fed16d02010-01-27 23:03:40 -05002430 cerr << "uint8_t is " << sizeof(uint8_t) << " bytes, should be 1 byte; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002431 allOK = 0;
2432 } // if
2433 if (sizeof(uint16_t) != 2) {
srs5694fed16d02010-01-27 23:03:40 -05002434 cerr << "uint16_t is " << sizeof(uint16_t) << " bytes, should be 2 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002435 allOK = 0;
2436 } // if
2437 if (sizeof(uint32_t) != 4) {
srs5694fed16d02010-01-27 23:03:40 -05002438 cerr << "uint32_t is " << sizeof(uint32_t) << " bytes, should be 4 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002439 allOK = 0;
2440 } // if
2441 if (sizeof(uint64_t) != 8) {
srs5694fed16d02010-01-27 23:03:40 -05002442 cerr << "uint64_t is " << sizeof(uint64_t) << " bytes, should be 8 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002443 allOK = 0;
2444 } // if
2445 if (sizeof(struct MBRRecord) != 16) {
srs5694fed16d02010-01-27 23:03:40 -05002446 cerr << "MBRRecord is " << sizeof(MBRRecord) << " bytes, should be 16 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002447 allOK = 0;
2448 } // if
srs5694978041c2009-09-21 20:51:47 -04002449 if (sizeof(struct TempMBR) != 512) {
srs5694fed16d02010-01-27 23:03:40 -05002450 cerr << "TempMBR is " << sizeof(TempMBR) << " bytes, should be 512 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002451 allOK = 0;
2452 } // if
2453 if (sizeof(struct GPTHeader) != 512) {
srs5694fed16d02010-01-27 23:03:40 -05002454 cerr << "GPTHeader is " << sizeof(GPTHeader) << " bytes, should be 512 bytes; aborting!\n";
srs5694e7b4ff92009-08-18 13:16:10 -04002455 allOK = 0;
2456 } // if
srs5694221e0872009-08-29 15:00:31 -04002457 if (sizeof(GPTPart) != 128) {
srs5694fed16d02010-01-27 23:03:40 -05002458 cerr << "GPTPart is " << sizeof(GPTPart) << " bytes, should be 128 bytes; aborting!\n";
srs5694221e0872009-08-29 15:00:31 -04002459 allOK = 0;
2460 } // if
srs56946699b012010-02-04 00:55:30 -05002461 if (sizeof(GUIDData) != 16) {
2462 cerr << "GUIDData is " << sizeof(GUIDData) << " bytes, should be 16 bytes; aborting!\n";
2463 allOK = 0;
2464 } // if
2465 if (sizeof(PartType) != 16) {
Roderick W. Smith84aaff62014-02-17 16:17:11 -05002466 cerr << "PartType is " << sizeof(PartType) << " bytes, should be 16 bytes; aborting!\n";
srs56946699b012010-02-04 00:55:30 -05002467 allOK = 0;
2468 } // if
srs5694e7b4ff92009-08-18 13:16:10 -04002469 return (allOK);
2470} // SizesOK()
srs5694e4ac11e2009-08-31 10:13:04 -04002471