srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1 | /* gpt.cc -- Functions for loading, saving, and manipulating legacy MBR and GPT partition |
| 2 | data. */ |
| 3 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 4 | /* By Rod Smith, initial coding January to February, 2009 */ |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 5 | |
srs5694 | d8eed46 | 2012-12-15 01:55:21 -0500 | [diff] [blame] | 6 | /* This program is copyright (c) 2009-2012 by Roderick W. Smith. It is distributed |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 7 | under the terms of the GNU GPL version 2, as detailed in the COPYING file. */ |
| 8 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 9 | #define __STDC_LIMIT_MACROS |
| 10 | #define __STDC_CONSTANT_MACROS |
| 11 | |
| 12 | #include <stdio.h> |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 13 | #include <stdlib.h> |
| 14 | #include <stdint.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <string.h> |
srs5694 | a8582cf | 2010-03-19 14:21:59 -0400 | [diff] [blame] | 17 | #include <math.h> |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 18 | #include <time.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <errno.h> |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 21 | #include <iostream> |
srs5694 | 9a46b04 | 2011-03-15 00:34:10 -0400 | [diff] [blame] | 22 | #include <algorithm> |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 23 | #include "crc32.h" |
| 24 | #include "gpt.h" |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 25 | #include "bsd.h" |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 26 | #include "support.h" |
| 27 | #include "parttypes.h" |
| 28 | #include "attributes.h" |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 29 | #include "diskio.h" |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 30 | |
| 31 | using namespace std; |
| 32 | |
srs5694 | 8f1b2d6 | 2010-05-23 13:07:19 -0400 | [diff] [blame] | 33 | #ifdef __FreeBSD__ |
srs5694 | 9ba5421 | 2010-05-18 23:24:02 -0400 | [diff] [blame] | 34 | #define log2(x) (log(x) / M_LN2) |
| 35 | #endif // __FreeBSD__ |
| 36 | |
srs5694 | 8f1b2d6 | 2010-05-23 13:07:19 -0400 | [diff] [blame] | 37 | #ifdef _MSC_VER |
| 38 | #define log2(x) (log((double) x) / log(2.0)) |
| 39 | #endif // Microsoft Visual C++ |
srs5694 | 9ba5421 | 2010-05-18 23:24:02 -0400 | [diff] [blame] | 40 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 41 | /**************************************** |
| 42 | * * |
| 43 | * GPTData class and related structures * |
| 44 | * * |
| 45 | ****************************************/ |
| 46 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 47 | // Default constructor |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 48 | GPTData::GPTData(void) { |
| 49 | blockSize = SECTOR_SIZE; // set a default |
| 50 | diskSize = 0; |
| 51 | partitions = NULL; |
| 52 | state = gpt_valid; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 53 | device = ""; |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 54 | justLooking = 0; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 55 | mainCrcOk = 0; |
| 56 | secondCrcOk = 0; |
| 57 | mainPartsCrcOk = 0; |
| 58 | secondPartsCrcOk = 0; |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 59 | apmFound = 0; |
| 60 | bsdFound = 0; |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 61 | sectorAlignment = MIN_AF_ALIGNMENT; // Align partitions on 4096-byte boundaries by default |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 62 | beQuiet = 0; |
| 63 | whichWasUsed = use_new; |
srs5694 | 1e09372 | 2010-01-05 00:14:19 -0500 | [diff] [blame] | 64 | mainHeader.numParts = 0; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 65 | numParts = 0; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 66 | SetGPTSize(NUM_GPT_ENTRIES); |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 67 | // Initialize CRC functions... |
| 68 | chksum_crc32gentab(); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 69 | } // GPTData default constructor |
| 70 | |
| 71 | // The following constructor loads GPT data from a device file |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 72 | GPTData::GPTData(string filename) { |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 73 | blockSize = SECTOR_SIZE; // set a default |
| 74 | diskSize = 0; |
| 75 | partitions = NULL; |
| 76 | state = gpt_invalid; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 77 | device = ""; |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 78 | justLooking = 0; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 79 | mainCrcOk = 0; |
| 80 | secondCrcOk = 0; |
| 81 | mainPartsCrcOk = 0; |
| 82 | secondPartsCrcOk = 0; |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 83 | apmFound = 0; |
| 84 | bsdFound = 0; |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 85 | sectorAlignment = MIN_AF_ALIGNMENT; // Align partitions on 4096-byte boundaries by default |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 86 | beQuiet = 0; |
| 87 | whichWasUsed = use_new; |
srs5694 | 1e09372 | 2010-01-05 00:14:19 -0500 | [diff] [blame] | 88 | mainHeader.numParts = 0; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 89 | numParts = 0; |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 90 | // Initialize CRC functions... |
| 91 | chksum_crc32gentab(); |
srs5694 | 3c0af38 | 2010-01-15 19:19:18 -0500 | [diff] [blame] | 92 | if (!LoadPartitions(filename)) |
| 93 | exit(2); |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 94 | } // GPTData(string filename) constructor |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 95 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 96 | // Destructor |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 97 | GPTData::~GPTData(void) { |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 98 | delete[] partitions; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 99 | } // GPTData destructor |
| 100 | |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 101 | // Assignment operator |
| 102 | GPTData & GPTData::operator=(const GPTData & orig) { |
| 103 | uint32_t i; |
| 104 | |
| 105 | mainHeader = orig.mainHeader; |
| 106 | numParts = orig.numParts; |
| 107 | secondHeader = orig.secondHeader; |
| 108 | protectiveMBR = orig.protectiveMBR; |
| 109 | device = orig.device; |
| 110 | blockSize = orig.blockSize; |
| 111 | diskSize = orig.diskSize; |
| 112 | state = orig.state; |
| 113 | justLooking = orig.justLooking; |
| 114 | mainCrcOk = orig.mainCrcOk; |
| 115 | secondCrcOk = orig.secondCrcOk; |
| 116 | mainPartsCrcOk = orig.mainPartsCrcOk; |
| 117 | secondPartsCrcOk = orig.secondPartsCrcOk; |
| 118 | apmFound = orig.apmFound; |
| 119 | bsdFound = orig.bsdFound; |
| 120 | sectorAlignment = orig.sectorAlignment; |
| 121 | beQuiet = orig.beQuiet; |
| 122 | whichWasUsed = orig.whichWasUsed; |
| 123 | |
| 124 | myDisk.OpenForRead(orig.myDisk.GetName()); |
| 125 | |
| 126 | delete[] partitions; |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 127 | partitions = new GPTPart [numParts]; |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 128 | if (partitions == NULL) { |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 129 | cerr << "Error! Could not allocate memory for partitions in GPTData::operator=()!\n" |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 130 | << "Terminating!\n"; |
| 131 | exit(1); |
| 132 | } // if |
| 133 | for (i = 0; i < numParts; i++) { |
| 134 | partitions[i] = orig.partitions[i]; |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 135 | } // for |
| 136 | |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 137 | return *this; |
| 138 | } // GPTData::operator=() |
| 139 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 140 | /********************************************************************* |
| 141 | * * |
| 142 | * Begin functions that verify data, or that adjust the verification * |
| 143 | * information (compute CRCs, rebuild headers) * |
| 144 | * * |
| 145 | *********************************************************************/ |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 146 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 147 | // Perform detailed verification, reporting on any problems found, but |
| 148 | // do *NOT* recover from these problems. Returns the total number of |
| 149 | // problems identified. |
| 150 | int GPTData::Verify(void) { |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 151 | int problems = 0, alignProbs = 0; |
srs5694 | e321d44 | 2010-01-29 17:44:04 -0500 | [diff] [blame] | 152 | uint32_t i, numSegments; |
| 153 | uint64_t totalFree, largestSegment; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 154 | |
| 155 | // First, check for CRC errors in the GPT data.... |
| 156 | if (!mainCrcOk) { |
| 157 | problems++; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 158 | cout << "\nProblem: The CRC for the main GPT header is invalid. The main GPT header may\n" |
| 159 | << "be corrupt. Consider loading the backup GPT header to rebuild the main GPT\n" |
| 160 | << "header ('b' on the recovery & transformation menu). This report may be a false\n" |
| 161 | << "alarm if you've already corrected other problems.\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 162 | } // if |
| 163 | if (!mainPartsCrcOk) { |
| 164 | problems++; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 165 | cout << "\nProblem: The CRC for the main partition table is invalid. This table may be\n" |
| 166 | << "corrupt. Consider loading the backup partition table ('c' on the recovery &\n" |
| 167 | << "transformation menu). This report may be a false alarm if you've already\n" |
| 168 | << "corrected other problems.\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 169 | } // if |
| 170 | if (!secondCrcOk) { |
| 171 | problems++; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 172 | cout << "\nProblem: The CRC for the backup GPT header is invalid. The backup GPT header\n" |
| 173 | << "may be corrupt. Consider using the main GPT header to rebuild the backup GPT\n" |
| 174 | << "header ('d' on the recovery & transformation menu). This report may be a false\n" |
| 175 | << "alarm if you've already corrected other problems.\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 176 | } // if |
| 177 | if (!secondPartsCrcOk) { |
| 178 | problems++; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 179 | cout << "\nCaution: The CRC for the backup partition table is invalid. This table may\n" |
| 180 | << "be corrupt. This program will automatically create a new backup partition\n" |
| 181 | << "table when you save your partitions.\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 182 | } // if |
| 183 | |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 184 | // Now check that the main and backup headers both point to themselves.... |
| 185 | if (mainHeader.currentLBA != 1) { |
| 186 | problems++; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 187 | cout << "\nProblem: The main header's self-pointer doesn't point to itself. This problem\n" |
| 188 | << "is being automatically corrected, but it may be a symptom of more serious\n" |
| 189 | << "problems. Think carefully before saving changes with 'w' or using this disk.\n"; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 190 | mainHeader.currentLBA = 1; |
| 191 | } // if |
| 192 | if (secondHeader.currentLBA != (diskSize - UINT64_C(1))) { |
| 193 | problems++; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 194 | cout << "\nProblem: The secondary header's self-pointer indicates that it doesn't reside\n" |
| 195 | << "at the end of the disk. If you've added a disk to a RAID array, use the 'e'\n" |
| 196 | << "option on the experts' menu to adjust the secondary header's and partition\n" |
| 197 | << "table's locations.\n"; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 198 | } // if |
| 199 | |
| 200 | // Now check that critical main and backup GPT entries match each other |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 201 | if (mainHeader.currentLBA != secondHeader.backupLBA) { |
| 202 | problems++; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 203 | cout << "\nProblem: main GPT header's current LBA pointer (" << mainHeader.currentLBA |
| 204 | << ") doesn't\nmatch the backup GPT header's alternate LBA pointer(" |
| 205 | << secondHeader.backupLBA << ").\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 206 | } // if |
| 207 | if (mainHeader.backupLBA != secondHeader.currentLBA) { |
| 208 | problems++; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 209 | cout << "\nProblem: main GPT header's backup LBA pointer (" << mainHeader.backupLBA |
| 210 | << ") doesn't\nmatch the backup GPT header's current LBA pointer (" |
| 211 | << secondHeader.currentLBA << ").\n" |
| 212 | << "The 'e' option on the experts' menu may fix this problem.\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 213 | } // if |
| 214 | if (mainHeader.firstUsableLBA != secondHeader.firstUsableLBA) { |
| 215 | problems++; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 216 | cout << "\nProblem: main GPT header's first usable LBA pointer (" << mainHeader.firstUsableLBA |
| 217 | << ") doesn't\nmatch the backup GPT header's first usable LBA pointer (" |
| 218 | << secondHeader.firstUsableLBA << ")\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 219 | } // if |
| 220 | if (mainHeader.lastUsableLBA != secondHeader.lastUsableLBA) { |
| 221 | problems++; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 222 | cout << "\nProblem: main GPT header's last usable LBA pointer (" << mainHeader.lastUsableLBA |
| 223 | << ") doesn't\nmatch the backup GPT header's last usable LBA pointer (" |
| 224 | << secondHeader.lastUsableLBA << ")\n" |
| 225 | << "The 'e' option on the experts' menu can probably fix this problem.\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 226 | } // if |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 227 | if ((mainHeader.diskGUID != secondHeader.diskGUID)) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 228 | problems++; |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 229 | cout << "\nProblem: main header's disk GUID (" << mainHeader.diskGUID |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 230 | << ") doesn't\nmatch the backup GPT header's disk GUID (" |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 231 | << secondHeader.diskGUID << ")\n" |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 232 | << "You should use the 'b' or 'd' option on the recovery & transformation menu to\n" |
| 233 | << "select one or the other header.\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 234 | } // if |
| 235 | if (mainHeader.numParts != secondHeader.numParts) { |
| 236 | problems++; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 237 | cout << "\nProblem: main GPT header's number of partitions (" << mainHeader.numParts |
| 238 | << ") doesn't\nmatch the backup GPT header's number of partitions (" |
| 239 | << secondHeader.numParts << ")\n" |
| 240 | << "Resizing the partition table ('s' on the experts' menu) may help.\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 241 | } // if |
| 242 | if (mainHeader.sizeOfPartitionEntries != secondHeader.sizeOfPartitionEntries) { |
| 243 | problems++; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 244 | cout << "\nProblem: main GPT header's size of partition entries (" |
| 245 | << mainHeader.sizeOfPartitionEntries << ") doesn't\n" |
| 246 | << "match the backup GPT header's size of partition entries (" |
| 247 | << secondHeader.sizeOfPartitionEntries << ")\n" |
| 248 | << "You should use the 'b' or 'd' option on the recovery & transformation menu to\n" |
| 249 | << "select one or the other header.\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 250 | } // if |
| 251 | |
| 252 | // Now check for a few other miscellaneous problems... |
| 253 | // Check that the disk size will hold the data... |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 254 | if (mainHeader.backupLBA >= diskSize) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 255 | problems++; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 256 | cout << "\nProblem: Disk is too small to hold all the data!\n" |
| 257 | << "(Disk size is " << diskSize << " sectors, needs to be " |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 258 | << mainHeader.backupLBA + UINT64_C(1) << " sectors.)\n" |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 259 | << "The 'e' option on the experts' menu may fix this problem.\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 260 | } // if |
| 261 | |
srs5694 | d8eed46 | 2012-12-15 01:55:21 -0500 | [diff] [blame] | 262 | if ((mainHeader.lastUsableLBA >= diskSize) || (mainHeader.lastUsableLBA > mainHeader.backupLBA)) { |
| 263 | problems++; |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame] | 264 | cout << "\nProblem: GPT claims the disk is larger than it is! (Claimed last usable\n" |
| 265 | << "sector is " << mainHeader.lastUsableLBA << ", but backup header is at\n" |
| 266 | << mainHeader.backupLBA << " and disk size is " << diskSize << " sectors.\n" |
| 267 | << "The 'e' option on the experts' menu will probably fix this problem\n"; |
srs5694 | d8eed46 | 2012-12-15 01:55:21 -0500 | [diff] [blame] | 268 | } |
| 269 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 270 | // Check for overlapping partitions.... |
| 271 | problems += FindOverlaps(); |
| 272 | |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 273 | // Check for insane partitions (start after end, hugely big, etc.) |
| 274 | problems += FindInsanePartitions(); |
| 275 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 276 | // Check for mismatched MBR and GPT partitions... |
| 277 | problems += FindHybridMismatches(); |
| 278 | |
srs5694 | 327129e | 2010-09-22 01:07:31 -0400 | [diff] [blame] | 279 | // Check for MBR-specific problems.... |
| 280 | problems += VerifyMBR(); |
| 281 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 282 | // Verify that partitions don't run into GPT data areas.... |
| 283 | problems += CheckGPTSize(); |
| 284 | |
srs5694 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame] | 285 | // Check that partitions are aligned on proper boundaries (for WD Advanced |
| 286 | // Format and similar disks).... |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 287 | for (i = 0; i < numParts; i++) { |
srs5694 | e69e680 | 2012-01-20 22:37:12 -0500 | [diff] [blame] | 288 | if ((partitions[i].IsUsed()) && (partitions[i].GetFirstLBA() % sectorAlignment) != 0) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 289 | cout << "\nCaution: Partition " << i + 1 << " doesn't begin on a " |
| 290 | << sectorAlignment << "-sector boundary. This may\nresult " |
| 291 | << "in degraded performance on some modern (2009 and later) hard disks.\n"; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 292 | alignProbs++; |
srs5694 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame] | 293 | } // if |
| 294 | } // for |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 295 | if (alignProbs > 0) |
| 296 | cout << "\nConsult http://www.ibm.com/developerworks/linux/library/l-4kb-sector-disks/\n" |
| 297 | << "for information on disk alignment.\n"; |
srs5694 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame] | 298 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 299 | // Now compute available space, but only if no problems found, since |
| 300 | // problems could affect the results |
| 301 | if (problems == 0) { |
| 302 | totalFree = FindFreeBlocks(&numSegments, &largestSegment); |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 303 | cout << "\nNo problems found. " << totalFree << " free sectors (" |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 304 | << BytesToIeee(totalFree, blockSize) << ") available in " |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 305 | << numSegments << "\nsegments, the largest of which is " |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 306 | << largestSegment << " (" << BytesToIeee(largestSegment, blockSize) |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 307 | << ") in size.\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 308 | } else { |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 309 | cout << "\nIdentified " << problems << " problems!\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 310 | } // if/else |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 311 | |
| 312 | return (problems); |
| 313 | } // GPTData::Verify() |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 314 | |
| 315 | // Checks to see if the GPT tables overrun existing partitions; if they |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 316 | // do, issues a warning but takes no action. Returns number of problems |
| 317 | // detected (0 if OK, 1 to 2 if problems). |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 318 | int GPTData::CheckGPTSize(void) { |
| 319 | uint64_t overlap, firstUsedBlock, lastUsedBlock; |
| 320 | uint32_t i; |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 321 | int numProbs = 0; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 322 | |
| 323 | // first, locate the first & last used blocks |
| 324 | firstUsedBlock = UINT64_MAX; |
| 325 | lastUsedBlock = 0; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 326 | for (i = 0; i < numParts; i++) { |
srs5694 | e69e680 | 2012-01-20 22:37:12 -0500 | [diff] [blame] | 327 | if (partitions[i].IsUsed()) { |
srs5694 | 706e512 | 2012-01-21 13:47:24 -0500 | [diff] [blame] | 328 | if (partitions[i].GetFirstLBA() < firstUsedBlock) |
srs5694 | e69e680 | 2012-01-20 22:37:12 -0500 | [diff] [blame] | 329 | firstUsedBlock = partitions[i].GetFirstLBA(); |
| 330 | if (partitions[i].GetLastLBA() > lastUsedBlock) { |
| 331 | lastUsedBlock = partitions[i].GetLastLBA(); |
| 332 | } // if |
| 333 | } // if |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 334 | } // for |
| 335 | |
| 336 | // If the disk size is 0 (the default), then it means that various |
| 337 | // variables aren't yet set, so the below tests will be useless; |
| 338 | // therefore we should skip everything |
| 339 | if (diskSize != 0) { |
| 340 | if (mainHeader.firstUsableLBA > firstUsedBlock) { |
| 341 | overlap = mainHeader.firstUsableLBA - firstUsedBlock; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 342 | cout << "Warning! Main partition table overlaps the first partition by " |
| 343 | << overlap << " blocks!\n"; |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 344 | if (firstUsedBlock > 2) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 345 | cout << "Try reducing the partition table size by " << overlap * 4 |
| 346 | << " entries.\n(Use the 's' item on the experts' menu.)\n"; |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 347 | } else { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 348 | cout << "You will need to delete this partition or resize it in another utility.\n"; |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 349 | } // if/else |
| 350 | numProbs++; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 351 | } // Problem at start of disk |
| 352 | if (mainHeader.lastUsableLBA < lastUsedBlock) { |
| 353 | overlap = lastUsedBlock - mainHeader.lastUsableLBA; |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 354 | cout << "\nWarning! Secondary partition table overlaps the last partition by\n" |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 355 | << overlap << " blocks!\n"; |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 356 | if (lastUsedBlock > (diskSize - 2)) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 357 | cout << "You will need to delete this partition or resize it in another utility.\n"; |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 358 | } else { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 359 | cout << "Try reducing the partition table size by " << overlap * 4 |
| 360 | << " entries.\n(Use the 's' item on the experts' menu.)\n"; |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 361 | } // if/else |
| 362 | numProbs++; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 363 | } // Problem at end of disk |
| 364 | } // if (diskSize != 0) |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 365 | return numProbs; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 366 | } // GPTData::CheckGPTSize() |
| 367 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 368 | // Check the validity of the GPT header. Returns 1 if the main header |
| 369 | // is valid, 2 if the backup header is valid, 3 if both are valid, and |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 370 | // 0 if neither is valid. Note that this function checks the GPT signature, |
| 371 | // revision value, and CRCs in both headers. |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 372 | int GPTData::CheckHeaderValidity(void) { |
| 373 | int valid = 3; |
| 374 | |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 375 | cout.setf(ios::uppercase); |
| 376 | cout.fill('0'); |
| 377 | |
| 378 | // Note: failed GPT signature checks produce no error message because |
| 379 | // a message is displayed in the ReversePartitionBytes() function |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 380 | if ((mainHeader.signature != GPT_SIGNATURE) || (!CheckHeaderCRC(&mainHeader, 1))) { |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 381 | valid -= 1; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 382 | } else if ((mainHeader.revision != 0x00010000) && valid) { |
| 383 | valid -= 1; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 384 | cout << "Unsupported GPT version in main header; read 0x"; |
| 385 | cout.width(8); |
| 386 | cout << hex << mainHeader.revision << ", should be\n0x"; |
| 387 | cout.width(8); |
| 388 | cout << UINT32_C(0x00010000) << dec << "\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 389 | } // if/else/if |
| 390 | |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 391 | if ((secondHeader.signature != GPT_SIGNATURE) || (!CheckHeaderCRC(&secondHeader))) { |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 392 | valid -= 2; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 393 | } else if ((secondHeader.revision != 0x00010000) && valid) { |
| 394 | valid -= 2; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 395 | cout << "Unsupported GPT version in backup header; read 0x"; |
| 396 | cout.width(8); |
| 397 | cout << hex << secondHeader.revision << ", should be\n0x"; |
| 398 | cout.width(8); |
| 399 | cout << UINT32_C(0x00010000) << dec << "\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 400 | } // if/else/if |
| 401 | |
srs5694 | df9d363 | 2011-01-08 18:33:24 -0500 | [diff] [blame] | 402 | // Check for an Apple disk signature |
| 403 | if (((mainHeader.signature << 32) == APM_SIGNATURE1) || |
| 404 | (mainHeader.signature << 32) == APM_SIGNATURE2) { |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 405 | apmFound = 1; // Will display warning message later |
srs5694 | 3f2fe99 | 2009-11-24 18:28:18 -0500 | [diff] [blame] | 406 | } // if |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 407 | cout.fill(' '); |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 408 | |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 409 | return valid; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 410 | } // GPTData::CheckHeaderValidity() |
| 411 | |
| 412 | // Check the header CRC to see if it's OK... |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 413 | // Note: Must be called with header in platform-ordered byte order. |
| 414 | // Returns 1 if header's computed CRC matches the stored value, 0 if the |
| 415 | // computed and stored values don't match |
| 416 | int GPTData::CheckHeaderCRC(struct GPTHeader* header, int warn) { |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 417 | uint32_t oldCRC, newCRC, hSize; |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 418 | uint8_t *temp; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 419 | |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 420 | // Back up old header CRC and then blank it, since it must be 0 for |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 421 | // computation to be valid |
| 422 | oldCRC = header->headerCRC; |
| 423 | header->headerCRC = UINT32_C(0); |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 424 | |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 425 | hSize = header->headerSize; |
| 426 | |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 427 | if (IsLittleEndian() == 0) |
| 428 | ReverseHeaderBytes(header); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 429 | |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 430 | if ((hSize > blockSize) || (hSize < HEADER_SIZE)) { |
| 431 | if (warn) { |
| 432 | cerr << "\aWarning! Header size is specified as " << hSize << ", which is invalid.\n"; |
| 433 | cerr << "Setting the header size for CRC computation to " << HEADER_SIZE << "\n"; |
| 434 | } // if |
| 435 | hSize = HEADER_SIZE; |
| 436 | } else if ((hSize > sizeof(GPTHeader)) && warn) { |
| 437 | cout << "\aCaution! Header size for CRC check is " << hSize << ", which is greater than " << sizeof(GPTHeader) << ".\n"; |
| 438 | cout << "If stray data exists after the header on the header sector, it will be ignored,\n" |
| 439 | << "which may result in a CRC false alarm.\n"; |
| 440 | } // if/elseif |
| 441 | temp = new uint8_t[hSize]; |
| 442 | if (temp != NULL) { |
| 443 | memset(temp, 0, hSize); |
| 444 | if (hSize < sizeof(GPTHeader)) |
| 445 | memcpy(temp, header, hSize); |
| 446 | else |
| 447 | memcpy(temp, header, sizeof(GPTHeader)); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 448 | |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 449 | newCRC = chksum_crc32((unsigned char*) temp, hSize); |
| 450 | delete[] temp; |
| 451 | } else { |
| 452 | cerr << "Could not allocate memory in GPTData::CheckHeaderCRC()! Aborting!\n"; |
| 453 | exit(1); |
| 454 | } |
| 455 | if (IsLittleEndian() == 0) |
| 456 | ReverseHeaderBytes(header); |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 457 | header->headerCRC = oldCRC; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 458 | return (oldCRC == newCRC); |
| 459 | } // GPTData::CheckHeaderCRC() |
| 460 | |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 461 | // Recompute all the CRCs. Must be called before saving if any changes have |
| 462 | // been made. Must be called on platform-ordered data (this function reverses |
| 463 | // byte order and then undoes that reversal.) |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 464 | void GPTData::RecomputeCRCs(void) { |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 465 | uint32_t crc, hSize; |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 466 | int littleEndian = 1; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 467 | |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 468 | // If the header size is bigger than the GPT header data structure, reset it; |
| 469 | // otherwise, set both header sizes to whatever the main one is.... |
| 470 | if (mainHeader.headerSize > sizeof(GPTHeader)) |
| 471 | hSize = secondHeader.headerSize = mainHeader.headerSize = HEADER_SIZE; |
| 472 | else |
| 473 | hSize = secondHeader.headerSize = mainHeader.headerSize; |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 474 | |
| 475 | if ((littleEndian = IsLittleEndian()) == 0) { |
| 476 | ReversePartitionBytes(); |
| 477 | ReverseHeaderBytes(&mainHeader); |
| 478 | ReverseHeaderBytes(&secondHeader); |
| 479 | } // if |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 480 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 481 | // Compute CRC of partition tables & store in main and secondary headers |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 482 | crc = chksum_crc32((unsigned char*) partitions, numParts * GPT_SIZE); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 483 | mainHeader.partitionEntriesCRC = crc; |
| 484 | secondHeader.partitionEntriesCRC = crc; |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 485 | if (littleEndian == 0) { |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 486 | ReverseBytes(&mainHeader.partitionEntriesCRC, 4); |
| 487 | ReverseBytes(&secondHeader.partitionEntriesCRC, 4); |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 488 | } // if |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 489 | |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 490 | // Zero out GPT headers' own CRCs (required for correct computation) |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 491 | mainHeader.headerCRC = 0; |
| 492 | secondHeader.headerCRC = 0; |
| 493 | |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 494 | crc = chksum_crc32((unsigned char*) &mainHeader, hSize); |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 495 | if (littleEndian == 0) |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 496 | ReverseBytes(&crc, 4); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 497 | mainHeader.headerCRC = crc; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 498 | crc = chksum_crc32((unsigned char*) &secondHeader, hSize); |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 499 | if (littleEndian == 0) |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 500 | ReverseBytes(&crc, 4); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 501 | secondHeader.headerCRC = crc; |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 502 | |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 503 | if (littleEndian == 0) { |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 504 | ReverseHeaderBytes(&mainHeader); |
| 505 | ReverseHeaderBytes(&secondHeader); |
| 506 | ReversePartitionBytes(); |
| 507 | } // if |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 508 | } // GPTData::RecomputeCRCs() |
| 509 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 510 | // Rebuild the main GPT header, using the secondary header as a model. |
| 511 | // Typically called when the main header has been found to be corrupt. |
| 512 | void GPTData::RebuildMainHeader(void) { |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 513 | mainHeader.signature = GPT_SIGNATURE; |
| 514 | mainHeader.revision = secondHeader.revision; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 515 | mainHeader.headerSize = secondHeader.headerSize; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 516 | mainHeader.headerCRC = UINT32_C(0); |
| 517 | mainHeader.reserved = secondHeader.reserved; |
| 518 | mainHeader.currentLBA = secondHeader.backupLBA; |
| 519 | mainHeader.backupLBA = secondHeader.currentLBA; |
| 520 | mainHeader.firstUsableLBA = secondHeader.firstUsableLBA; |
| 521 | mainHeader.lastUsableLBA = secondHeader.lastUsableLBA; |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 522 | mainHeader.diskGUID = secondHeader.diskGUID; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 523 | mainHeader.partitionEntriesLBA = UINT64_C(2); |
| 524 | mainHeader.numParts = secondHeader.numParts; |
| 525 | mainHeader.sizeOfPartitionEntries = secondHeader.sizeOfPartitionEntries; |
| 526 | mainHeader.partitionEntriesCRC = secondHeader.partitionEntriesCRC; |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 527 | memcpy(mainHeader.reserved2, secondHeader.reserved2, sizeof(mainHeader.reserved2)); |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 528 | mainCrcOk = secondCrcOk; |
srs5694 | 706e512 | 2012-01-21 13:47:24 -0500 | [diff] [blame] | 529 | SetGPTSize(mainHeader.numParts, 0); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 530 | } // GPTData::RebuildMainHeader() |
| 531 | |
| 532 | // Rebuild the secondary GPT header, using the main header as a model. |
| 533 | void GPTData::RebuildSecondHeader(void) { |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 534 | secondHeader.signature = GPT_SIGNATURE; |
| 535 | secondHeader.revision = mainHeader.revision; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 536 | secondHeader.headerSize = mainHeader.headerSize; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 537 | secondHeader.headerCRC = UINT32_C(0); |
| 538 | secondHeader.reserved = mainHeader.reserved; |
| 539 | secondHeader.currentLBA = mainHeader.backupLBA; |
| 540 | secondHeader.backupLBA = mainHeader.currentLBA; |
| 541 | secondHeader.firstUsableLBA = mainHeader.firstUsableLBA; |
| 542 | secondHeader.lastUsableLBA = mainHeader.lastUsableLBA; |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 543 | secondHeader.diskGUID = mainHeader.diskGUID; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 544 | secondHeader.partitionEntriesLBA = secondHeader.lastUsableLBA + UINT64_C(1); |
| 545 | secondHeader.numParts = mainHeader.numParts; |
| 546 | secondHeader.sizeOfPartitionEntries = mainHeader.sizeOfPartitionEntries; |
| 547 | secondHeader.partitionEntriesCRC = mainHeader.partitionEntriesCRC; |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 548 | memcpy(secondHeader.reserved2, mainHeader.reserved2, sizeof(secondHeader.reserved2)); |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 549 | secondCrcOk = mainCrcOk; |
srs5694 | 706e512 | 2012-01-21 13:47:24 -0500 | [diff] [blame] | 550 | SetGPTSize(secondHeader.numParts, 0); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 551 | } // GPTData::RebuildSecondHeader() |
| 552 | |
| 553 | // Search for hybrid MBR entries that have no corresponding GPT partition. |
| 554 | // Returns number of such mismatches found |
| 555 | int GPTData::FindHybridMismatches(void) { |
srs5694 | e321d44 | 2010-01-29 17:44:04 -0500 | [diff] [blame] | 556 | int i, found, numFound = 0; |
| 557 | uint32_t j; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 558 | uint64_t mbrFirst, mbrLast; |
| 559 | |
| 560 | for (i = 0; i < 4; i++) { |
| 561 | if ((protectiveMBR.GetType(i) != 0xEE) && (protectiveMBR.GetType(i) != 0x00)) { |
| 562 | j = 0; |
| 563 | found = 0; |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 564 | mbrFirst = (uint64_t) protectiveMBR.GetFirstSector(i); |
| 565 | mbrLast = mbrFirst + (uint64_t) protectiveMBR.GetLength(i) - UINT64_C(1); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 566 | do { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 567 | if ((partitions[j].GetFirstLBA() == mbrFirst) && |
srs5694 | e69e680 | 2012-01-20 22:37:12 -0500 | [diff] [blame] | 568 | (partitions[j].GetLastLBA() == mbrLast) && (partitions[j].IsUsed())) |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 569 | found = 1; |
| 570 | j++; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 571 | } while ((!found) && (j < numParts)); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 572 | if (!found) { |
| 573 | numFound++; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 574 | cout << "\nWarning! Mismatched GPT and MBR partition! MBR partition " |
| 575 | << i + 1 << ", of type 0x"; |
| 576 | cout.fill('0'); |
| 577 | cout.setf(ios::uppercase); |
| 578 | cout.width(2); |
| 579 | cout << hex << (int) protectiveMBR.GetType(i) << ",\n" |
| 580 | << "has no corresponding GPT partition! You may continue, but this condition\n" |
| 581 | << "might cause data loss in the future!\a\n" << dec; |
| 582 | cout.fill(' '); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 583 | } // if |
| 584 | } // if |
| 585 | } // for |
| 586 | return numFound; |
| 587 | } // GPTData::FindHybridMismatches |
| 588 | |
| 589 | // Find overlapping partitions and warn user about them. Returns number of |
| 590 | // overlapping partitions. |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 591 | // Returns number of overlapping segments found. |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 592 | int GPTData::FindOverlaps(void) { |
srs5694 | e321d44 | 2010-01-29 17:44:04 -0500 | [diff] [blame] | 593 | int problems = 0; |
| 594 | uint32_t i, j; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 595 | |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 596 | for (i = 1; i < numParts; i++) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 597 | for (j = 0; j < i; j++) { |
srs5694 | e69e680 | 2012-01-20 22:37:12 -0500 | [diff] [blame] | 598 | if ((partitions[i].IsUsed()) && (partitions[j].IsUsed()) && |
| 599 | (partitions[i].DoTheyOverlap(partitions[j]))) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 600 | problems++; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 601 | cout << "\nProblem: partitions " << i + 1 << " and " << j + 1 << " overlap:\n"; |
| 602 | cout << " Partition " << i + 1 << ": " << partitions[i].GetFirstLBA() |
| 603 | << " to " << partitions[i].GetLastLBA() << "\n"; |
| 604 | cout << " Partition " << j + 1 << ": " << partitions[j].GetFirstLBA() |
| 605 | << " to " << partitions[j].GetLastLBA() << "\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 606 | } // if |
| 607 | } // for j... |
| 608 | } // for i... |
| 609 | return problems; |
| 610 | } // GPTData::FindOverlaps() |
| 611 | |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 612 | // Find partitions that are insane -- they start after they end or are too |
| 613 | // big for the disk. (The latter should duplicate detection of overlaps |
| 614 | // with GPT backup data structures, but better to err on the side of |
| 615 | // redundant tests than to miss something....) |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 616 | // Returns number of problems found. |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 617 | int GPTData::FindInsanePartitions(void) { |
| 618 | uint32_t i; |
| 619 | int problems = 0; |
| 620 | |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 621 | for (i = 0; i < numParts; i++) { |
srs5694 | e69e680 | 2012-01-20 22:37:12 -0500 | [diff] [blame] | 622 | if (partitions[i].IsUsed()) { |
| 623 | if (partitions[i].GetFirstLBA() > partitions[i].GetLastLBA()) { |
| 624 | problems++; |
| 625 | cout << "\nProblem: partition " << i + 1 << " ends before it begins.\n"; |
| 626 | } // if |
| 627 | if (partitions[i].GetLastLBA() >= diskSize) { |
| 628 | problems++; |
| 629 | cout << "\nProblem: partition " << i + 1 << " is too big for the disk.\n"; |
| 630 | } // if |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 631 | } // if |
| 632 | } // for |
| 633 | return problems; |
| 634 | } // GPTData::FindInsanePartitions(void) |
| 635 | |
| 636 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 637 | /****************************************************************** |
| 638 | * * |
| 639 | * Begin functions that load data from disk or save data to disk. * |
| 640 | * * |
| 641 | ******************************************************************/ |
| 642 | |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 643 | // Change the filename associated with the GPT. Used for duplicating |
| 644 | // the partition table to a new disk and saving backups. |
| 645 | // Returns 1 on success, 0 on failure. |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 646 | int GPTData::SetDisk(const string & deviceFilename) { |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 647 | int err, allOK = 1; |
| 648 | |
| 649 | device = deviceFilename; |
| 650 | if (allOK && myDisk.OpenForRead(deviceFilename)) { |
| 651 | // store disk information.... |
| 652 | diskSize = myDisk.DiskSize(&err); |
| 653 | blockSize = (uint32_t) myDisk.GetBlockSize(); |
| 654 | } // if |
| 655 | protectiveMBR.SetDisk(&myDisk); |
| 656 | protectiveMBR.SetDiskSize(diskSize); |
| 657 | protectiveMBR.SetBlockSize(blockSize); |
| 658 | return allOK; |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 659 | } // GPTData::SetDisk() |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 660 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 661 | // Scan for partition data. This function loads the MBR data (regular MBR or |
| 662 | // protective MBR) and loads BSD disklabel data (which is probably invalid). |
| 663 | // It also looks for APM data, forces a load of GPT data, and summarizes |
| 664 | // the results. |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 665 | void GPTData::PartitionScan(void) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 666 | BSDData bsdDisklabel; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 667 | |
| 668 | // Read the MBR & check for BSD disklabel |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 669 | protectiveMBR.ReadMBRData(&myDisk); |
| 670 | bsdDisklabel.ReadBSDData(&myDisk, 0, diskSize - 1); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 671 | |
| 672 | // Load the GPT data, whether or not it's valid |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 673 | ForceLoadGPTData(); |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 674 | |
| 675 | if (!beQuiet) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 676 | cout << "Partition table scan:\n"; |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 677 | protectiveMBR.ShowState(); |
| 678 | bsdDisklabel.ShowState(); |
| 679 | ShowAPMState(); // Show whether there's an Apple Partition Map present |
| 680 | ShowGPTState(); // Show GPT status |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 681 | cout << "\n"; |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 682 | } // if |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 683 | |
| 684 | if (apmFound) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 685 | cout << "\n*******************************************************************\n" |
| 686 | << "This disk appears to contain an Apple-format (APM) partition table!\n"; |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 687 | if (!justLooking) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 688 | cout << "It will be destroyed if you continue!\n"; |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 689 | } // if |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 690 | cout << "*******************************************************************\n\n\a"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 691 | } // if |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 692 | } // GPTData::PartitionScan() |
| 693 | |
| 694 | // Read GPT data from a disk. |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 695 | int GPTData::LoadPartitions(const string & deviceFilename) { |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 696 | BSDData bsdDisklabel; |
srs5694 | e321d44 | 2010-01-29 17:44:04 -0500 | [diff] [blame] | 697 | int err, allOK = 1; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 698 | MBRValidity mbrState; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 699 | |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 700 | if (myDisk.OpenForRead(deviceFilename)) { |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 701 | err = myDisk.OpenForWrite(deviceFilename); |
| 702 | if ((err == 0) && (!justLooking)) { |
| 703 | cout << "\aNOTE: Write test failed with error number " << errno |
| 704 | << ". It will be impossible to save\nchanges to this disk's partition table!\n"; |
| 705 | #if defined (__FreeBSD__) || defined (__FreeBSD_kernel__) |
| 706 | cout << "You may be able to enable writes by exiting this program, typing\n" |
| 707 | << "'sysctl kern.geom.debugflags=16' at a shell prompt, and re-running this\n" |
| 708 | << "program.\n"; |
| 709 | #endif |
| 710 | cout << "\n"; |
| 711 | } // if |
| 712 | myDisk.Close(); // Close and re-open read-only in case of bugs |
| 713 | } else allOK = 0; // if |
| 714 | |
| 715 | if (allOK && myDisk.OpenForRead(deviceFilename)) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 716 | // store disk information.... |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 717 | diskSize = myDisk.DiskSize(&err); |
| 718 | blockSize = (uint32_t) myDisk.GetBlockSize(); |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 719 | device = deviceFilename; |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 720 | PartitionScan(); // Check for partition types, load GPT, & print summary |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 721 | |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 722 | whichWasUsed = UseWhichPartitions(); |
| 723 | switch (whichWasUsed) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 724 | case use_mbr: |
| 725 | XFormPartitions(); |
| 726 | break; |
| 727 | case use_bsd: |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 728 | bsdDisklabel.ReadBSDData(&myDisk, 0, diskSize - 1); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 729 | // bsdDisklabel.DisplayBSDData(); |
| 730 | ClearGPTData(); |
| 731 | protectiveMBR.MakeProtectiveMBR(1); // clear boot area (option 1) |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 732 | XFormDisklabel(&bsdDisklabel); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 733 | break; |
| 734 | case use_gpt: |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 735 | mbrState = protectiveMBR.GetValidity(); |
| 736 | if ((mbrState == invalid) || (mbrState == mbr)) |
| 737 | protectiveMBR.MakeProtectiveMBR(); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 738 | break; |
| 739 | case use_new: |
| 740 | ClearGPTData(); |
| 741 | protectiveMBR.MakeProtectiveMBR(); |
| 742 | break; |
srs5694 | 3c0af38 | 2010-01-15 19:19:18 -0500 | [diff] [blame] | 743 | case use_abort: |
| 744 | allOK = 0; |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 745 | cerr << "Invalid partition data!\n"; |
srs5694 | 3c0af38 | 2010-01-15 19:19:18 -0500 | [diff] [blame] | 746 | break; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 747 | } // switch |
| 748 | |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 749 | if (allOK) |
srs5694 | 3c0af38 | 2010-01-15 19:19:18 -0500 | [diff] [blame] | 750 | CheckGPTSize(); |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 751 | myDisk.Close(); |
srs5694 | a8582cf | 2010-03-19 14:21:59 -0400 | [diff] [blame] | 752 | ComputeAlignment(); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 753 | } else { |
| 754 | allOK = 0; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 755 | } // if/else |
| 756 | return (allOK); |
| 757 | } // GPTData::LoadPartitions() |
| 758 | |
| 759 | // Loads the GPT, as much as possible. Returns 1 if this seems to have |
| 760 | // succeeded, 0 if there are obvious problems.... |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 761 | int GPTData::ForceLoadGPTData(void) { |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 762 | int allOK, validHeaders, loadedTable = 1; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 763 | |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 764 | allOK = LoadHeader(&mainHeader, myDisk, 1, &mainCrcOk); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 765 | |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 766 | if (mainCrcOk && (mainHeader.backupLBA < diskSize)) { |
| 767 | allOK = LoadHeader(&secondHeader, myDisk, mainHeader.backupLBA, &secondCrcOk) && allOK; |
| 768 | } else { |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 769 | allOK = LoadHeader(&secondHeader, myDisk, diskSize - UINT64_C(1), &secondCrcOk) && allOK; |
| 770 | if (mainCrcOk && (mainHeader.backupLBA >= diskSize)) |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 771 | cout << "Warning! Disk size is smaller than the main header indicates! Loading\n" |
| 772 | << "secondary header from the last sector of the disk! You should use 'v' to\n" |
| 773 | << "verify disk integrity, and perhaps options on the experts' menu to repair\n" |
| 774 | << "the disk.\n"; |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 775 | } // if/else |
| 776 | if (!allOK) |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 777 | state = gpt_invalid; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 778 | |
| 779 | // Return valid headers code: 0 = both headers bad; 1 = main header |
| 780 | // good, backup bad; 2 = backup header good, main header bad; |
| 781 | // 3 = both headers good. Note these codes refer to valid GPT |
srs5694 | 23d8d54 | 2011-10-01 18:40:10 -0400 | [diff] [blame] | 782 | // signatures, version numbers, and CRCs. |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 783 | validHeaders = CheckHeaderValidity(); |
| 784 | |
| 785 | // Read partitions (from primary array) |
| 786 | if (validHeaders > 0) { // if at least one header is OK.... |
| 787 | // GPT appears to be valid.... |
| 788 | state = gpt_valid; |
| 789 | |
| 790 | // We're calling the GPT valid, but there's a possibility that one |
| 791 | // of the two headers is corrupt. If so, use the one that seems to |
| 792 | // be in better shape to regenerate the bad one |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 793 | if (validHeaders == 1) { // valid main header, invalid backup header |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 794 | cerr << "\aCaution: invalid backup GPT header, but valid main header; regenerating\n" |
| 795 | << "backup header from main header.\n\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 796 | RebuildSecondHeader(); |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 797 | state = gpt_corrupt; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 798 | secondCrcOk = mainCrcOk; // Since regenerated, use CRC validity of main |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 799 | } else if (validHeaders == 2) { // valid backup header, invalid main header |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 800 | cerr << "\aCaution: invalid main GPT header, but valid backup; regenerating main header\n" |
| 801 | << "from backup!\n\n"; |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 802 | RebuildMainHeader(); |
| 803 | state = gpt_corrupt; |
| 804 | mainCrcOk = secondCrcOk; // Since copied, use CRC validity of backup |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 805 | } // if/else/if |
| 806 | |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 807 | // Figure out which partition table to load.... |
| 808 | // Load the main partition table, since either its header's CRC is OK or the |
| 809 | // backup header's CRC is not OK.... |
| 810 | if (mainCrcOk || !secondCrcOk) { |
| 811 | if (LoadMainTable() == 0) |
| 812 | allOK = 0; |
| 813 | } else { // bad main header CRC and backup header CRC is OK |
| 814 | state = gpt_corrupt; |
| 815 | if (LoadSecondTableAsMain()) { |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 816 | loadedTable = 2; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 817 | cerr << "\aWarning: Invalid CRC on main header data; loaded backup partition table.\n"; |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 818 | } else { // backup table bad, bad main header CRC, but try main table in desperation.... |
| 819 | if (LoadMainTable() == 0) { |
| 820 | allOK = 0; |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 821 | loadedTable = 0; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 822 | cerr << "\a\aWarning! Unable to load either main or backup partition table!\n"; |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 823 | } // if |
| 824 | } // if/else (LoadSecondTableAsMain()) |
| 825 | } // if/else (load partition table) |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 826 | |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 827 | if (loadedTable == 1) |
| 828 | secondPartsCrcOk = CheckTable(&secondHeader); |
| 829 | else if (loadedTable == 2) |
| 830 | mainPartsCrcOk = CheckTable(&mainHeader); |
| 831 | else |
| 832 | mainPartsCrcOk = secondPartsCrcOk = 0; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 833 | |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 834 | // Problem with main partition table; if backup is OK, use it instead.... |
| 835 | if (secondPartsCrcOk && secondCrcOk && !mainPartsCrcOk) { |
| 836 | state = gpt_corrupt; |
| 837 | allOK = allOK && LoadSecondTableAsMain(); |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 838 | mainPartsCrcOk = 0; // LoadSecondTableAsMain() resets this, so re-flag as bad |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 839 | cerr << "\aWarning! Main partition table CRC mismatch! Loaded backup " |
| 840 | << "partition table\ninstead of main partition table!\n\n"; |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 841 | } // if */ |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 842 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 843 | // Check for valid CRCs and warn if there are problems |
| 844 | if ((mainCrcOk == 0) || (secondCrcOk == 0) || (mainPartsCrcOk == 0) || |
| 845 | (secondPartsCrcOk == 0)) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 846 | cerr << "Warning! One or more CRCs don't match. You should repair the disk!\n\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 847 | state = gpt_corrupt; |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 848 | } // if |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 849 | } else { |
| 850 | state = gpt_invalid; |
| 851 | } // if/else |
| 852 | return allOK; |
| 853 | } // GPTData::ForceLoadGPTData() |
| 854 | |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 855 | // Loads the partition table pointed to by the main GPT header. The |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 856 | // main GPT header in memory MUST be valid for this call to do anything |
| 857 | // sensible! |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 858 | // Returns 1 on success, 0 on failure. CRC errors do NOT count as failure. |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 859 | int GPTData::LoadMainTable(void) { |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 860 | return LoadPartitionTable(mainHeader, myDisk); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 861 | } // GPTData::LoadMainTable() |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 862 | |
| 863 | // Load the second (backup) partition table as the primary partition |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 864 | // table. Used in repair functions, and when starting up if the main |
| 865 | // partition table is damaged. |
| 866 | // Returns 1 on success, 0 on failure. CRC errors do NOT count as failure. |
| 867 | int GPTData::LoadSecondTableAsMain(void) { |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 868 | return LoadPartitionTable(secondHeader, myDisk); |
| 869 | } // GPTData::LoadSecondTableAsMain() |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 870 | |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 871 | // Load a single GPT header (main or backup) from the specified disk device and |
| 872 | // sector. Applies byte-order corrections on big-endian platforms. Sets crcOk |
| 873 | // value appropriately. |
| 874 | // Returns 1 on success, 0 on failure. Note that CRC errors do NOT qualify as |
| 875 | // failure. |
| 876 | int GPTData::LoadHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector, int *crcOk) { |
| 877 | int allOK = 1; |
srs5694 | 1c6f8b0 | 2010-02-21 11:09:20 -0500 | [diff] [blame] | 878 | GPTHeader tempHeader; |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 879 | |
| 880 | disk.Seek(sector); |
srs5694 | 1c6f8b0 | 2010-02-21 11:09:20 -0500 | [diff] [blame] | 881 | if (disk.Read(&tempHeader, 512) != 512) { |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 882 | cerr << "Warning! Read error " << errno << "; strange behavior now likely!\n"; |
| 883 | allOK = 0; |
| 884 | } // if |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 885 | |
srs5694 | 1c6f8b0 | 2010-02-21 11:09:20 -0500 | [diff] [blame] | 886 | // Reverse byte order, if necessary |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 887 | if (IsLittleEndian() == 0) { |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 888 | ReverseHeaderBytes(&tempHeader); |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 889 | } // if |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 890 | *crcOk = CheckHeaderCRC(&tempHeader); |
srs5694 | 1c6f8b0 | 2010-02-21 11:09:20 -0500 | [diff] [blame] | 891 | |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 892 | if (allOK && (numParts != tempHeader.numParts) && *crcOk) { |
srs5694 | 706e512 | 2012-01-21 13:47:24 -0500 | [diff] [blame] | 893 | allOK = SetGPTSize(tempHeader.numParts, 0); |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 894 | } |
srs5694 | 1c6f8b0 | 2010-02-21 11:09:20 -0500 | [diff] [blame] | 895 | |
| 896 | *header = tempHeader; |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 897 | return allOK; |
| 898 | } // GPTData::LoadHeader |
| 899 | |
| 900 | // Load a partition table (either main or secondary) from the specified disk, |
| 901 | // using header as a reference for what to load. If sector != 0 (the default |
| 902 | // is 0), loads from the specified sector; otherwise loads from the sector |
| 903 | // indicated in header. |
| 904 | // Returns 1 on success, 0 on failure. CRC errors do NOT count as failure. |
| 905 | int GPTData::LoadPartitionTable(const struct GPTHeader & header, DiskIO & disk, uint64_t sector) { |
| 906 | uint32_t sizeOfParts, newCRC; |
| 907 | int retval; |
| 908 | |
| 909 | if (disk.OpenForRead()) { |
| 910 | if (sector == 0) { |
| 911 | retval = disk.Seek(header.partitionEntriesLBA); |
| 912 | } else { |
| 913 | retval = disk.Seek(sector); |
| 914 | } // if/else |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 915 | if (retval == 1) |
srs5694 | 706e512 | 2012-01-21 13:47:24 -0500 | [diff] [blame] | 916 | retval = SetGPTSize(header.numParts, 0); |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 917 | if (retval == 1) { |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 918 | sizeOfParts = header.numParts * header.sizeOfPartitionEntries; |
| 919 | if (disk.Read(partitions, sizeOfParts) != (int) sizeOfParts) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 920 | cerr << "Warning! Read error " << errno << "! Misbehavior now likely!\n"; |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 921 | retval = 0; |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 922 | } // if |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 923 | newCRC = chksum_crc32((unsigned char*) partitions, sizeOfParts); |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 924 | mainPartsCrcOk = secondPartsCrcOk = (newCRC == header.partitionEntriesCRC); |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 925 | if (IsLittleEndian() == 0) |
| 926 | ReversePartitionBytes(); |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 927 | if (!mainPartsCrcOk) { |
| 928 | cout << "Caution! After loading partitions, the CRC doesn't check out!\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 929 | } // if |
| 930 | } else { |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 931 | cerr << "Error! Couldn't seek to partition table!\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 932 | } // if/else |
| 933 | } else { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 934 | cerr << "Error! Couldn't open device " << device |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 935 | << " when reading partition table!\n"; |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 936 | retval = 0; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 937 | } // if/else |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 938 | return retval; |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 939 | } // GPTData::LoadPartitionsTable() |
| 940 | |
| 941 | // Check the partition table pointed to by header, but don't keep it |
| 942 | // around. |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 943 | // Returns 1 if the CRC is OK & this table matches the one already in memory, |
| 944 | // 0 if not or if there was a read error. |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 945 | int GPTData::CheckTable(struct GPTHeader *header) { |
| 946 | uint32_t sizeOfParts, newCRC; |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 947 | GPTPart *partsToCheck; |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 948 | GPTHeader *otherHeader; |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 949 | int allOK = 0; |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 950 | |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 951 | // Load partition table into temporary storage to check |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 952 | // its CRC and store the results, then discard this temporary |
| 953 | // storage, since we don't use it in any but recovery operations |
| 954 | if (myDisk.Seek(header->partitionEntriesLBA)) { |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 955 | partsToCheck = new GPTPart[header->numParts]; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 956 | sizeOfParts = header->numParts * header->sizeOfPartitionEntries; |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 957 | if (partsToCheck == NULL) { |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 958 | cerr << "Could not allocate memory in GPTData::CheckTable()! Terminating!\n"; |
| 959 | exit(1); |
| 960 | } // if |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 961 | if (myDisk.Read(partsToCheck, sizeOfParts) != (int) sizeOfParts) { |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 962 | cerr << "Warning! Error " << errno << " reading partition table for CRC check!\n"; |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 963 | } else { |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 964 | newCRC = chksum_crc32((unsigned char*) partsToCheck, sizeOfParts); |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 965 | allOK = (newCRC == header->partitionEntriesCRC); |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 966 | if (header == &mainHeader) |
| 967 | otherHeader = &secondHeader; |
| 968 | else |
| 969 | otherHeader = &mainHeader; |
| 970 | if (newCRC != otherHeader->partitionEntriesCRC) { |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 971 | cerr << "Warning! Main and backup partition tables differ! Use the 'c' and 'e' options\n" |
| 972 | << "on the recovery & transformation menu to examine the two tables.\n\n"; |
| 973 | allOK = 0; |
| 974 | } // if |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 975 | } // if/else |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 976 | delete[] partsToCheck; |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 977 | } // if |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 978 | return allOK; |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 979 | } // GPTData::CheckTable() |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 980 | |
srs5694 | 4307ef2 | 2012-05-30 12:30:48 -0400 | [diff] [blame] | 981 | // Writes GPT (and protective MBR) to disk. If quiet==1, moves the second |
| 982 | // header later on the disk without asking for permission, if necessary, and |
| 983 | // doesn't confirm the operation before writing. If quiet==0, asks permission |
| 984 | // before moving the second header and asks for final confirmation of any |
| 985 | // write. |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 986 | // Returns 1 on successful write, 0 if there was a problem. |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 987 | int GPTData::SaveGPTData(int quiet) { |
srs5694 | 4307ef2 | 2012-05-30 12:30:48 -0400 | [diff] [blame] | 988 | int allOK = 1, syncIt = 1; |
srs5694 | e321d44 | 2010-01-29 17:44:04 -0500 | [diff] [blame] | 989 | char answer; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 990 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 991 | // First do some final sanity checks.... |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 992 | |
| 993 | // This test should only fail on read-only disks.... |
| 994 | if (justLooking) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 995 | cout << "The justLooking flag is set. This probably means you can't write to the disk.\n"; |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 996 | allOK = 0; |
| 997 | } // if |
| 998 | |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 999 | // Check that disk is really big enough to handle the second header... |
| 1000 | if (mainHeader.backupLBA >= diskSize) { |
| 1001 | cerr << "Caution! Secondary header was placed beyond the disk's limits! Moving the\n" |
| 1002 | << "header, but other problems may occur!\n"; |
| 1003 | MoveSecondHeaderToEnd(); |
| 1004 | } // if |
| 1005 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1006 | // Is there enough space to hold the GPT headers and partition tables, |
| 1007 | // given the partition sizes? |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 1008 | if (CheckGPTSize() > 0) { |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1009 | allOK = 0; |
| 1010 | } // if |
| 1011 | |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1012 | // Check that second header is properly placed. Warn and ask if this should |
| 1013 | // be corrected if the test fails.... |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 1014 | if (mainHeader.backupLBA < (diskSize - UINT64_C(1))) { |
| 1015 | if (quiet == 0) { |
| 1016 | cout << "Warning! Secondary header is placed too early on the disk! Do you want to\n" |
| 1017 | << "correct this problem? "; |
| 1018 | if (GetYN() == 'Y') { |
| 1019 | MoveSecondHeaderToEnd(); |
| 1020 | cout << "Have moved second header and partition table to correct location.\n"; |
| 1021 | } else { |
| 1022 | cout << "Have not corrected the problem. Strange problems may occur in the future!\n"; |
| 1023 | } // if correction requested |
| 1024 | } else { // Go ahead and do correction automatically |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1025 | MoveSecondHeaderToEnd(); |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 1026 | } // if/else quiet |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1027 | } // if |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1028 | |
srs5694 | d8eed46 | 2012-12-15 01:55:21 -0500 | [diff] [blame] | 1029 | if ((mainHeader.lastUsableLBA >= diskSize) || (mainHeader.lastUsableLBA > mainHeader.backupLBA)) { |
| 1030 | if (quiet == 0) { |
| 1031 | cout << "Warning! The claimed last usable sector is incorrect! Do you want to correct\n" |
| 1032 | << "this problem? "; |
| 1033 | if (GetYN() == 'Y') { |
| 1034 | MoveSecondHeaderToEnd(); |
| 1035 | cout << "Have adjusted the second header and last usable sector value.\n"; |
| 1036 | } else { |
| 1037 | cout << "Have not corrected the problem. Strange problems may occur in the future!\n"; |
| 1038 | } // if correction requested |
| 1039 | } else { // go ahead and do correction automatically |
| 1040 | MoveSecondHeaderToEnd(); |
| 1041 | } // if/else quiet |
| 1042 | } // if |
| 1043 | |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 1044 | // Check for overlapping or insane partitions.... |
| 1045 | if ((FindOverlaps() > 0) || (FindInsanePartitions() > 0)) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1046 | allOK = 0; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1047 | cerr << "Aborting write operation!\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1048 | } // if |
| 1049 | |
| 1050 | // Check for mismatched MBR and GPT data, but let it pass if found |
| 1051 | // (function displays warning message) |
| 1052 | FindHybridMismatches(); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1053 | |
| 1054 | RecomputeCRCs(); |
| 1055 | |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 1056 | if ((allOK) && (!quiet)) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1057 | cout << "\nFinal checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING\n" |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 1058 | << "PARTITIONS!!\n\nDo you want to proceed? "; |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 1059 | answer = GetYN(); |
| 1060 | if (answer == 'Y') { |
srs5694 | 3488294 | 2012-03-23 12:49:15 -0400 | [diff] [blame] | 1061 | cout << "OK; writing new GUID partition table (GPT) to " << myDisk.GetName() << ".\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1062 | } else { |
| 1063 | allOK = 0; |
| 1064 | } // if/else |
| 1065 | } // if |
| 1066 | |
| 1067 | // Do it! |
| 1068 | if (allOK) { |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 1069 | if (myDisk.OpenForWrite()) { |
srs5694 | 8a4ddfc | 2010-03-21 19:05:49 -0400 | [diff] [blame] | 1070 | // As per UEFI specs, write the secondary table and GPT first.... |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 1071 | allOK = SavePartitionTable(myDisk, secondHeader.partitionEntriesLBA); |
srs5694 | 4307ef2 | 2012-05-30 12:30:48 -0400 | [diff] [blame] | 1072 | if (!allOK) { |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 1073 | cerr << "Unable to save backup partition table! Perhaps the 'e' option on the experts'\n" |
| 1074 | << "menu will resolve this problem.\n"; |
srs5694 | 4307ef2 | 2012-05-30 12:30:48 -0400 | [diff] [blame] | 1075 | syncIt = 0; |
| 1076 | } // if |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1077 | |
| 1078 | // Now write the secondary GPT header... |
srs5694 | 8a4ddfc | 2010-03-21 19:05:49 -0400 | [diff] [blame] | 1079 | allOK = allOK && SaveHeader(&secondHeader, myDisk, mainHeader.backupLBA); |
| 1080 | |
| 1081 | // Now write the main partition tables... |
| 1082 | allOK = allOK && SavePartitionTable(myDisk, mainHeader.partitionEntriesLBA); |
| 1083 | |
| 1084 | // Now write the main GPT header... |
| 1085 | allOK = allOK && SaveHeader(&mainHeader, myDisk, 1); |
| 1086 | |
| 1087 | // To top it off, write the protective MBR... |
| 1088 | allOK = allOK && protectiveMBR.WriteMBRData(&myDisk); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1089 | |
| 1090 | // re-read the partition table |
srs5694 | 4307ef2 | 2012-05-30 12:30:48 -0400 | [diff] [blame] | 1091 | // Note: Done even if some write operations failed, but not if all of them failed. |
| 1092 | // Done this way because I've received one problem report from a user one whose |
| 1093 | // system the MBR write failed but everything else was OK (on a GPT disk under |
| 1094 | // Windows), and the failure to sync therefore caused Windows to restore the |
| 1095 | // original partition table from its cache. OTOH, such restoration might be |
| 1096 | // desirable if the error occurs later; but that seems unlikely unless the initial |
| 1097 | // write fails.... |
| 1098 | if (syncIt) |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 1099 | myDisk.DiskSync(); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1100 | |
| 1101 | if (allOK) { // writes completed OK |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1102 | cout << "The operation has completed successfully.\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1103 | } else { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1104 | cerr << "Warning! An error was reported when writing the partition table! This error\n" |
srs5694 | 4307ef2 | 2012-05-30 12:30:48 -0400 | [diff] [blame] | 1105 | << "MIGHT be harmless, or the disk might be damaged! Checking it is advisable.\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1106 | } // if/else |
srs5694 | 8a4ddfc | 2010-03-21 19:05:49 -0400 | [diff] [blame] | 1107 | |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 1108 | myDisk.Close(); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1109 | } else { |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 1110 | cerr << "Unable to open device '" << myDisk.GetName() << "' for writing! Errno is " |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1111 | << errno << "! Aborting write!\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1112 | allOK = 0; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1113 | } // if/else |
| 1114 | } else { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1115 | cout << "Aborting write of new partition table.\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1116 | } // if |
| 1117 | |
| 1118 | return (allOK); |
| 1119 | } // GPTData::SaveGPTData() |
| 1120 | |
| 1121 | // Save GPT data to a backup file. This function does much less error |
| 1122 | // checking than SaveGPTData(). It can therefore preserve many types of |
| 1123 | // corruption for later analysis; however, it preserves only the MBR, |
| 1124 | // the main GPT header, the backup GPT header, and the main partition |
| 1125 | // table; it discards the backup partition table, since it should be |
| 1126 | // identical to the main partition table on healthy disks. |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 1127 | int GPTData::SaveGPTBackup(const string & filename) { |
| 1128 | int allOK = 1; |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 1129 | DiskIO backupFile; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1130 | |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 1131 | if (backupFile.OpenForWrite(filename)) { |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 1132 | // Recomputing the CRCs is likely to alter them, which could be bad |
| 1133 | // if the intent is to save a potentially bad GPT for later analysis; |
| 1134 | // but if we don't do this, we get bogus errors when we load the |
| 1135 | // backup. I'm favoring misses over false alarms.... |
| 1136 | RecomputeCRCs(); |
| 1137 | |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 1138 | protectiveMBR.WriteMBRData(&backupFile); |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 1139 | protectiveMBR.SetDisk(&myDisk); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1140 | |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 1141 | if (allOK) { |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 1142 | // MBR write closed disk, so re-open and seek to end.... |
| 1143 | backupFile.OpenForWrite(); |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 1144 | allOK = SaveHeader(&mainHeader, backupFile, 1); |
| 1145 | } // if (allOK) |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1146 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1147 | if (allOK) |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 1148 | allOK = SaveHeader(&secondHeader, backupFile, 2); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1149 | |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 1150 | if (allOK) |
| 1151 | allOK = SavePartitionTable(backupFile, 3); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1152 | |
| 1153 | if (allOK) { // writes completed OK |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1154 | cout << "The operation has completed successfully.\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1155 | } else { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1156 | cerr << "Warning! An error was reported when writing the backup file.\n" |
| 1157 | << "It may not be usable!\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1158 | } // if/else |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 1159 | backupFile.Close(); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1160 | } else { |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 1161 | cerr << "Unable to open file '" << filename << "' for writing! Aborting!\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1162 | allOK = 0; |
| 1163 | } // if/else |
| 1164 | return allOK; |
| 1165 | } // GPTData::SaveGPTBackup() |
| 1166 | |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 1167 | // Write a GPT header (main or backup) to the specified sector. Used by both |
| 1168 | // the SaveGPTData() and SaveGPTBackup() functions. |
| 1169 | // Should be passed an architecture-appropriate header (DO NOT call |
| 1170 | // ReverseHeaderBytes() on the header before calling this function) |
| 1171 | // Returns 1 on success, 0 on failure |
| 1172 | int GPTData::SaveHeader(struct GPTHeader *header, DiskIO & disk, uint64_t sector) { |
| 1173 | int littleEndian, allOK = 1; |
| 1174 | |
| 1175 | littleEndian = IsLittleEndian(); |
| 1176 | if (!littleEndian) |
| 1177 | ReverseHeaderBytes(header); |
| 1178 | if (disk.Seek(sector)) { |
| 1179 | if (disk.Write(header, 512) == -1) |
| 1180 | allOK = 0; |
| 1181 | } else allOK = 0; // if (disk.Seek()...) |
| 1182 | if (!littleEndian) |
| 1183 | ReverseHeaderBytes(header); |
| 1184 | return allOK; |
| 1185 | } // GPTData::SaveHeader() |
| 1186 | |
| 1187 | // Save the partitions to the specified sector. Used by both the SaveGPTData() |
| 1188 | // and SaveGPTBackup() functions. |
| 1189 | // Should be passed an architecture-appropriate header (DO NOT call |
| 1190 | // ReverseHeaderBytes() on the header before calling this function) |
| 1191 | // Returns 1 on success, 0 on failure |
| 1192 | int GPTData::SavePartitionTable(DiskIO & disk, uint64_t sector) { |
| 1193 | int littleEndian, allOK = 1; |
| 1194 | |
| 1195 | littleEndian = IsLittleEndian(); |
| 1196 | if (disk.Seek(sector)) { |
| 1197 | if (!littleEndian) |
| 1198 | ReversePartitionBytes(); |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1199 | if (disk.Write(partitions, mainHeader.sizeOfPartitionEntries * numParts) == -1) |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 1200 | allOK = 0; |
| 1201 | if (!littleEndian) |
| 1202 | ReversePartitionBytes(); |
| 1203 | } else allOK = 0; // if (myDisk.Seek()...) |
| 1204 | return allOK; |
| 1205 | } // GPTData::SavePartitionTable() |
| 1206 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1207 | // Load GPT data from a backup file created by SaveGPTBackup(). This function |
| 1208 | // does minimal error checking. It returns 1 if it completed successfully, |
| 1209 | // 0 if there was a problem. In the latter case, it creates a new empty |
| 1210 | // set of partitions. |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 1211 | int GPTData::LoadGPTBackup(const string & filename) { |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 1212 | int allOK = 1, val, err; |
srs5694 | 0541b56 | 2011-12-18 16:35:25 -0500 | [diff] [blame] | 1213 | int shortBackup = 0; |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 1214 | DiskIO backupFile; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1215 | |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 1216 | if (backupFile.OpenForRead(filename)) { |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1217 | // Let the MBRData class load the saved MBR... |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 1218 | protectiveMBR.ReadMBRData(&backupFile, 0); // 0 = don't check block size |
srs5694 | 815fb65 | 2011-03-18 12:35:56 -0400 | [diff] [blame] | 1219 | protectiveMBR.SetDisk(&myDisk); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1220 | |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 1221 | LoadHeader(&mainHeader, backupFile, 1, &mainCrcOk); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1222 | |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 1223 | // Check backup file size and rebuild second header if file is right |
| 1224 | // size to be direct dd copy of MBR, main header, and main partition |
| 1225 | // table; if other size, treat it like a GPT fdisk-generated backup |
| 1226 | // file |
| 1227 | shortBackup = ((backupFile.DiskSize(&err) * backupFile.GetBlockSize()) == |
| 1228 | (mainHeader.numParts * mainHeader.sizeOfPartitionEntries) + 1024); |
| 1229 | if (shortBackup) { |
| 1230 | RebuildSecondHeader(); |
| 1231 | secondCrcOk = mainCrcOk; |
| 1232 | } else { |
| 1233 | LoadHeader(&secondHeader, backupFile, 2, &secondCrcOk); |
| 1234 | } // if/else |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 1235 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1236 | // Return valid headers code: 0 = both headers bad; 1 = main header |
| 1237 | // good, backup bad; 2 = backup header good, main header bad; |
| 1238 | // 3 = both headers good. Note these codes refer to valid GPT |
| 1239 | // signatures and version numbers; more subtle problems will elude |
| 1240 | // this check! |
| 1241 | if ((val = CheckHeaderValidity()) > 0) { |
| 1242 | if (val == 2) { // only backup header seems to be good |
srs5694 | 706e512 | 2012-01-21 13:47:24 -0500 | [diff] [blame] | 1243 | SetGPTSize(secondHeader.numParts, 0); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1244 | } else { // main header is OK |
srs5694 | 706e512 | 2012-01-21 13:47:24 -0500 | [diff] [blame] | 1245 | SetGPTSize(mainHeader.numParts, 0); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1246 | } // if/else |
| 1247 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1248 | if (secondHeader.currentLBA != diskSize - UINT64_C(1)) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1249 | cout << "Warning! Current disk size doesn't match that of the backup!\n" |
| 1250 | << "Adjusting sizes to match, but subsequent problems are possible!\n"; |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1251 | MoveSecondHeaderToEnd(); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1252 | } // if |
| 1253 | |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 1254 | if (!LoadPartitionTable(mainHeader, backupFile, (uint64_t) (3 - shortBackup))) |
| 1255 | cerr << "Warning! Read error " << errno |
| 1256 | << " loading partition table; strange behavior now likely!\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1257 | } else { |
| 1258 | allOK = 0; |
| 1259 | } // if/else |
srs5694 | a8582cf | 2010-03-19 14:21:59 -0400 | [diff] [blame] | 1260 | // Something went badly wrong, so blank out partitions |
| 1261 | if (allOK == 0) { |
| 1262 | cerr << "Improper backup file! Clearing all partition data!\n"; |
| 1263 | ClearGPTData(); |
| 1264 | protectiveMBR.MakeProtectiveMBR(); |
| 1265 | } // if |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1266 | } else { |
| 1267 | allOK = 0; |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 1268 | cerr << "Unable to open file '" << filename << "' for reading! Aborting!\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1269 | } // if/else |
| 1270 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1271 | return allOK; |
| 1272 | } // GPTData::LoadGPTBackup() |
| 1273 | |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1274 | int GPTData::SaveMBR(void) { |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 1275 | return protectiveMBR.WriteMBRData(&myDisk); |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1276 | } // GPTData::SaveMBR() |
| 1277 | |
| 1278 | // This function destroys the on-disk GPT structures, but NOT the on-disk |
| 1279 | // MBR. |
| 1280 | // Returns 1 if the operation succeeds, 0 if not. |
| 1281 | int GPTData::DestroyGPT(void) { |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 1282 | int sum, tableSize, allOK = 1; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1283 | uint8_t blankSector[512]; |
| 1284 | uint8_t* emptyTable; |
| 1285 | |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 1286 | memset(blankSector, 0, sizeof(blankSector)); |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1287 | |
| 1288 | if (myDisk.OpenForWrite()) { |
| 1289 | if (!myDisk.Seek(mainHeader.currentLBA)) |
| 1290 | allOK = 0; |
| 1291 | if (myDisk.Write(blankSector, 512) != 512) { // blank it out |
| 1292 | cerr << "Warning! GPT main header not overwritten! Error is " << errno << "\n"; |
| 1293 | allOK = 0; |
| 1294 | } // if |
| 1295 | if (!myDisk.Seek(mainHeader.partitionEntriesLBA)) |
| 1296 | allOK = 0; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1297 | tableSize = numParts * mainHeader.sizeOfPartitionEntries; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1298 | emptyTable = new uint8_t[tableSize]; |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 1299 | if (emptyTable == NULL) { |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 1300 | cerr << "Could not allocate memory in GPTData::DestroyGPT()! Terminating!\n"; |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 1301 | exit(1); |
| 1302 | } // if |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 1303 | memset(emptyTable, 0, tableSize); |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1304 | if (allOK) { |
| 1305 | sum = myDisk.Write(emptyTable, tableSize); |
| 1306 | if (sum != tableSize) { |
| 1307 | cerr << "Warning! GPT main partition table not overwritten! Error is " << errno << "\n"; |
| 1308 | allOK = 0; |
| 1309 | } // if write failed |
| 1310 | } // if |
| 1311 | if (!myDisk.Seek(secondHeader.partitionEntriesLBA)) |
| 1312 | allOK = 0; |
| 1313 | if (allOK) { |
| 1314 | sum = myDisk.Write(emptyTable, tableSize); |
| 1315 | if (sum != tableSize) { |
| 1316 | cerr << "Warning! GPT backup partition table not overwritten! Error is " |
| 1317 | << errno << "\n"; |
| 1318 | allOK = 0; |
| 1319 | } // if wrong size written |
| 1320 | } // if |
| 1321 | if (!myDisk.Seek(secondHeader.currentLBA)) |
| 1322 | allOK = 0; |
| 1323 | if (allOK) { |
| 1324 | if (myDisk.Write(blankSector, 512) != 512) { // blank it out |
| 1325 | cerr << "Warning! GPT backup header not overwritten! Error is " << errno << "\n"; |
| 1326 | allOK = 0; |
| 1327 | } // if |
| 1328 | } // if |
| 1329 | myDisk.DiskSync(); |
| 1330 | myDisk.Close(); |
| 1331 | cout << "GPT data structures destroyed! You may now partition the disk using fdisk or\n" |
| 1332 | << "other utilities.\n"; |
| 1333 | delete[] emptyTable; |
| 1334 | } else { |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 1335 | cerr << "Problem opening '" << device << "' for writing! Program will now terminate.\n"; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1336 | } // if/else (fd != -1) |
| 1337 | return (allOK); |
| 1338 | } // GPTDataTextUI::DestroyGPT() |
| 1339 | |
| 1340 | // Wipe MBR data from the disk (zero it out completely) |
| 1341 | // Returns 1 on success, 0 on failure. |
| 1342 | int GPTData::DestroyMBR(void) { |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 1343 | int allOK; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1344 | uint8_t blankSector[512]; |
| 1345 | |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 1346 | memset(blankSector, 0, sizeof(blankSector)); |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1347 | |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 1348 | allOK = myDisk.OpenForWrite() && myDisk.Seek(0) && (myDisk.Write(blankSector, 512) == 512); |
| 1349 | |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1350 | if (!allOK) |
| 1351 | cerr << "Warning! MBR not overwritten! Error is " << errno << "!\n"; |
| 1352 | return allOK; |
| 1353 | } // GPTData::DestroyMBR(void) |
| 1354 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1355 | // Tell user whether Apple Partition Map (APM) was discovered.... |
| 1356 | void GPTData::ShowAPMState(void) { |
| 1357 | if (apmFound) |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1358 | cout << " APM: present\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1359 | else |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1360 | cout << " APM: not present\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1361 | } // GPTData::ShowAPMState() |
| 1362 | |
| 1363 | // Tell user about the state of the GPT data.... |
| 1364 | void GPTData::ShowGPTState(void) { |
| 1365 | switch (state) { |
| 1366 | case gpt_invalid: |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1367 | cout << " GPT: not present\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1368 | break; |
| 1369 | case gpt_valid: |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1370 | cout << " GPT: present\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1371 | break; |
| 1372 | case gpt_corrupt: |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1373 | cout << " GPT: damaged\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1374 | break; |
| 1375 | default: |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1376 | cout << "\a GPT: unknown -- bug!\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1377 | break; |
| 1378 | } // switch |
| 1379 | } // GPTData::ShowGPTState() |
| 1380 | |
| 1381 | // Display the basic GPT data |
| 1382 | void GPTData::DisplayGPTData(void) { |
srs5694 | e321d44 | 2010-01-29 17:44:04 -0500 | [diff] [blame] | 1383 | uint32_t i; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1384 | uint64_t temp, totalFree; |
| 1385 | |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1386 | cout << "Disk " << device << ": " << diskSize << " sectors, " |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 1387 | << BytesToIeee(diskSize, blockSize) << "\n"; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1388 | cout << "Logical sector size: " << blockSize << " bytes\n"; |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 1389 | cout << "Disk identifier (GUID): " << mainHeader.diskGUID << "\n"; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1390 | cout << "Partition table holds up to " << numParts << " entries\n"; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1391 | cout << "First usable sector is " << mainHeader.firstUsableLBA |
| 1392 | << ", last usable sector is " << mainHeader.lastUsableLBA << "\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1393 | totalFree = FindFreeBlocks(&i, &temp); |
srs5694 | 8a4ddfc | 2010-03-21 19:05:49 -0400 | [diff] [blame] | 1394 | cout << "Partitions will be aligned on " << sectorAlignment << "-sector boundaries\n"; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1395 | cout << "Total free space is " << totalFree << " sectors (" |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 1396 | << BytesToIeee(totalFree, blockSize) << ")\n"; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1397 | cout << "\nNumber Start (sector) End (sector) Size Code Name\n"; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1398 | for (i = 0; i < numParts; i++) { |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 1399 | partitions[i].ShowSummary(i, blockSize); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1400 | } // for |
| 1401 | } // GPTData::DisplayGPTData() |
| 1402 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1403 | // Show detailed information on the specified partition |
| 1404 | void GPTData::ShowPartDetails(uint32_t partNum) { |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 1405 | if (!IsFreePartNum(partNum)) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1406 | partitions[partNum].ShowDetails(blockSize); |
| 1407 | } else { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1408 | cout << "Partition #" << partNum + 1 << " does not exist."; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1409 | } // if |
| 1410 | } // GPTData::ShowPartDetails() |
| 1411 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1412 | /************************************************************************** |
| 1413 | * * |
| 1414 | * Partition table transformation functions (MBR or BSD disklabel to GPT) * |
| 1415 | * (some of these functions may require user interaction) * |
| 1416 | * * |
| 1417 | **************************************************************************/ |
| 1418 | |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1419 | // Examines the MBR & GPT data to determine which set of data to use: the |
| 1420 | // MBR (use_mbr), the GPT (use_gpt), the BSD disklabel (use_bsd), or create |
| 1421 | // a new set of partitions (use_new). A return value of use_abort indicates |
| 1422 | // that this function couldn't determine what to do. Overriding functions |
| 1423 | // in derived classes may ask users questions in such cases. |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1424 | WhichToUse GPTData::UseWhichPartitions(void) { |
| 1425 | WhichToUse which = use_new; |
| 1426 | MBRValidity mbrState; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1427 | |
| 1428 | mbrState = protectiveMBR.GetValidity(); |
| 1429 | |
| 1430 | if ((state == gpt_invalid) && ((mbrState == mbr) || (mbrState == hybrid))) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1431 | cout << "\n***************************************************************\n" |
| 1432 | << "Found invalid GPT and valid MBR; converting MBR to GPT format.\n"; |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 1433 | if (!justLooking) { |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1434 | cout << "\aTHIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by typing 'q' if\n" |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1435 | << "you don't want to convert your MBR partitions to GPT format!\n"; |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 1436 | } // if |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1437 | cout << "***************************************************************\n\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1438 | which = use_mbr; |
| 1439 | } // if |
| 1440 | |
| 1441 | if ((state == gpt_invalid) && bsdFound) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1442 | cout << "\n**********************************************************************\n" |
| 1443 | << "Found invalid GPT and valid BSD disklabel; converting BSD disklabel\n" |
| 1444 | << "to GPT format."; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 1445 | if ((!justLooking) && (!beQuiet)) { |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1446 | cout << "\a THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Your first\n" |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1447 | << "BSD partition will likely be unusable. Exit by typing 'q' if you don't\n" |
| 1448 | << "want to convert your BSD partitions to GPT format!"; |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 1449 | } // if |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1450 | cout << "\n**********************************************************************\n\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1451 | which = use_bsd; |
| 1452 | } // if |
| 1453 | |
| 1454 | if ((state == gpt_valid) && (mbrState == gpt)) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1455 | which = use_gpt; |
srs5694 | 3c0af38 | 2010-01-15 19:19:18 -0500 | [diff] [blame] | 1456 | if (!beQuiet) |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1457 | cout << "Found valid GPT with protective MBR; using GPT.\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1458 | } // if |
| 1459 | if ((state == gpt_valid) && (mbrState == hybrid)) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1460 | which = use_gpt; |
srs5694 | 3c0af38 | 2010-01-15 19:19:18 -0500 | [diff] [blame] | 1461 | if (!beQuiet) |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1462 | cout << "Found valid GPT with hybrid MBR; using GPT.\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1463 | } // if |
| 1464 | if ((state == gpt_valid) && (mbrState == invalid)) { |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 1465 | cout << "\aFound valid GPT with corrupt MBR; using GPT and will write new\n" |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1466 | << "protective MBR on save.\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1467 | which = use_gpt; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1468 | } // if |
| 1469 | if ((state == gpt_valid) && (mbrState == mbr)) { |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1470 | which = use_abort; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1471 | } // if |
| 1472 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1473 | if (state == gpt_corrupt) { |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1474 | if (mbrState == gpt) { |
| 1475 | cout << "\a\a****************************************************************************\n" |
| 1476 | << "Caution: Found protective or hybrid MBR and corrupt GPT. Using GPT, but disk\n" |
| 1477 | << "verification and recovery are STRONGLY recommended.\n" |
| 1478 | << "****************************************************************************\n"; |
| 1479 | which = use_gpt; |
srs5694 | 3c0af38 | 2010-01-15 19:19:18 -0500 | [diff] [blame] | 1480 | } else { |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1481 | which = use_abort; |
| 1482 | } // if/else MBR says disk is GPT |
| 1483 | } // if GPT corrupt |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1484 | |
| 1485 | if (which == use_new) |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1486 | cout << "Creating new GPT entries.\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1487 | |
| 1488 | return which; |
| 1489 | } // UseWhichPartitions() |
| 1490 | |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1491 | // Convert MBR partition table into GPT form. |
| 1492 | void GPTData::XFormPartitions(void) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1493 | int i, numToConvert; |
| 1494 | uint8_t origType; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1495 | |
| 1496 | // Clear out old data & prepare basics.... |
| 1497 | ClearGPTData(); |
| 1498 | |
| 1499 | // Convert the smaller of the # of GPT or MBR partitions |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1500 | if (numParts > MAX_MBR_PARTS) |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 1501 | numToConvert = MAX_MBR_PARTS; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1502 | else |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1503 | numToConvert = numParts; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1504 | |
| 1505 | for (i = 0; i < numToConvert; i++) { |
| 1506 | origType = protectiveMBR.GetType(i); |
| 1507 | // don't waste CPU time trying to convert extended, hybrid protective, or |
| 1508 | // null (non-existent) partitions |
srs5694 | e35eb1b | 2009-09-14 00:29:34 -0400 | [diff] [blame] | 1509 | if ((origType != 0x05) && (origType != 0x0f) && (origType != 0x85) && |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 1510 | (origType != 0x00) && (origType != 0xEE)) |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1511 | partitions[i] = protectiveMBR.AsGPT(i); |
| 1512 | } // for |
| 1513 | |
| 1514 | // Convert MBR into protective MBR |
| 1515 | protectiveMBR.MakeProtectiveMBR(); |
| 1516 | |
| 1517 | // Record that all original CRCs were OK so as not to raise flags |
| 1518 | // when doing a disk verification |
| 1519 | mainCrcOk = secondCrcOk = mainPartsCrcOk = secondPartsCrcOk = 1; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1520 | } // GPTData::XFormPartitions() |
| 1521 | |
| 1522 | // Transforms BSD disklabel on the specified partition (numbered from 0). |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1523 | // If an invalid partition number is given, the program does nothing. |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1524 | // Returns the number of new partitions created. |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1525 | int GPTData::XFormDisklabel(uint32_t partNum) { |
| 1526 | uint32_t low, high; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1527 | int goOn = 1, numDone = 0; |
| 1528 | BSDData disklabel; |
| 1529 | |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1530 | if (GetPartRange(&low, &high) == 0) { |
| 1531 | goOn = 0; |
| 1532 | cout << "No partitions!\n"; |
| 1533 | } // if |
| 1534 | if (partNum > high) { |
| 1535 | goOn = 0; |
| 1536 | cout << "Specified partition is invalid!\n"; |
| 1537 | } // if |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1538 | |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1539 | // If all is OK, read the disklabel and convert it. |
| 1540 | if (goOn) { |
| 1541 | goOn = disklabel.ReadBSDData(&myDisk, partitions[partNum].GetFirstLBA(), |
| 1542 | partitions[partNum].GetLastLBA()); |
| 1543 | if ((goOn) && (disklabel.IsDisklabel())) { |
| 1544 | numDone = XFormDisklabel(&disklabel); |
| 1545 | if (numDone == 1) |
| 1546 | cout << "Converted 1 BSD partition.\n"; |
| 1547 | else |
| 1548 | cout << "Converted " << numDone << " BSD partitions.\n"; |
| 1549 | } else { |
| 1550 | cout << "Unable to convert partitions! Unrecognized BSD disklabel.\n"; |
| 1551 | } // if/else |
| 1552 | } // if |
| 1553 | if (numDone > 0) { // converted partitions; delete carrier |
| 1554 | partitions[partNum].BlankPartition(); |
| 1555 | } // if |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1556 | return numDone; |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 1557 | } // GPTData::XFormDisklabel(uint32_t i) |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1558 | |
| 1559 | // Transform the partitions on an already-loaded BSD disklabel... |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1560 | int GPTData::XFormDisklabel(BSDData* disklabel) { |
| 1561 | int i, partNum = 0, numDone = 0; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1562 | |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1563 | if (disklabel->IsDisklabel()) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1564 | for (i = 0; i < disklabel->GetNumParts(); i++) { |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1565 | partNum = FindFirstFreePart(); |
| 1566 | if (partNum >= 0) { |
| 1567 | partitions[partNum] = disklabel->AsGPT(i); |
| 1568 | if (partitions[partNum].IsUsed()) |
| 1569 | numDone++; |
| 1570 | } // if |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1571 | } // for |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1572 | if (partNum == -1) |
| 1573 | cerr << "Warning! Too many partitions to convert!\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1574 | } // if |
| 1575 | |
| 1576 | // Record that all original CRCs were OK so as not to raise flags |
| 1577 | // when doing a disk verification |
| 1578 | mainCrcOk = secondCrcOk = mainPartsCrcOk = secondPartsCrcOk = 1; |
| 1579 | |
| 1580 | return numDone; |
| 1581 | } // GPTData::XFormDisklabel(BSDData* disklabel) |
| 1582 | |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1583 | // Add one GPT partition to MBR. Used by PartsToMBR() functions. Created |
| 1584 | // partition has the active/bootable flag UNset and uses the GPT fdisk |
| 1585 | // type code divided by 0x0100 as the MBR type code. |
| 1586 | // Returns 1 if operation was 100% successful, 0 if there were ANY |
| 1587 | // problems. |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 1588 | int GPTData::OnePartToMBR(uint32_t gptPart, int mbrPart) { |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1589 | int allOK = 1; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1590 | |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 1591 | if ((mbrPart < 0) || (mbrPart > 3)) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1592 | cout << "MBR partition " << mbrPart + 1 << " is out of range; omitting it.\n"; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 1593 | allOK = 0; |
| 1594 | } // if |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1595 | if (gptPart >= numParts) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1596 | cout << "GPT partition " << gptPart + 1 << " is out of range; omitting it.\n"; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 1597 | allOK = 0; |
| 1598 | } // if |
| 1599 | if (allOK && (partitions[gptPart].GetLastLBA() == UINT64_C(0))) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1600 | cout << "GPT partition " << gptPart + 1 << " is undefined; omitting it.\n"; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 1601 | allOK = 0; |
| 1602 | } // if |
| 1603 | if (allOK && (partitions[gptPart].GetFirstLBA() <= UINT32_MAX) && |
| 1604 | (partitions[gptPart].GetLengthLBA() <= UINT32_MAX)) { |
| 1605 | if (partitions[gptPart].GetLastLBA() > UINT32_MAX) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1606 | cout << "Caution: Partition end point past 32-bit pointer boundary;" |
| 1607 | << " some OSes may\nreact strangely.\n"; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1608 | } // if |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 1609 | protectiveMBR.MakePart(mbrPart, (uint32_t) partitions[gptPart].GetFirstLBA(), |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1610 | (uint32_t) partitions[gptPart].GetLengthLBA(), |
| 1611 | partitions[gptPart].GetHexType() / 256, 0); |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 1612 | } else { // partition out of range |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1613 | if (allOK) // Display only if "else" triggered by out-of-bounds condition |
| 1614 | cout << "Partition " << gptPart + 1 << " begins beyond the 32-bit pointer limit of MBR " |
| 1615 | << "partitions, or is\n too big; omitting it.\n"; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 1616 | allOK = 0; |
| 1617 | } // if/else |
| 1618 | return allOK; |
| 1619 | } // GPTData::OnePartToMBR() |
| 1620 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1621 | |
| 1622 | /********************************************************************** |
| 1623 | * * |
| 1624 | * Functions that adjust GPT data structures WITHOUT user interaction * |
| 1625 | * (they may display information for the user's benefit, though) * |
| 1626 | * * |
| 1627 | **********************************************************************/ |
| 1628 | |
| 1629 | // Resizes GPT to specified number of entries. Creates a new table if |
srs5694 | 706e512 | 2012-01-21 13:47:24 -0500 | [diff] [blame] | 1630 | // necessary, copies data if it already exists. If fillGPTSectors is 1 |
| 1631 | // (the default), rounds numEntries to fill all the sectors necessary to |
| 1632 | // hold the GPT. |
| 1633 | // Returns 1 if all goes well, 0 if an error is encountered. |
| 1634 | int GPTData::SetGPTSize(uint32_t numEntries, int fillGPTSectors) { |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1635 | GPTPart* newParts; |
srs5694 | 706e512 | 2012-01-21 13:47:24 -0500 | [diff] [blame] | 1636 | uint32_t i, high, copyNum, entriesPerSector; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1637 | int allOK = 1; |
| 1638 | |
| 1639 | // First, adjust numEntries upward, if necessary, to get a number |
| 1640 | // that fills the allocated sectors |
srs5694 | 706e512 | 2012-01-21 13:47:24 -0500 | [diff] [blame] | 1641 | entriesPerSector = blockSize / GPT_SIZE; |
| 1642 | if (fillGPTSectors && ((numEntries % entriesPerSector) != 0)) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1643 | cout << "Adjusting GPT size from " << numEntries << " to "; |
srs5694 | 706e512 | 2012-01-21 13:47:24 -0500 | [diff] [blame] | 1644 | numEntries = ((numEntries / entriesPerSector) + 1) * entriesPerSector; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1645 | cout << numEntries << " to fill the sector\n"; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1646 | } // if |
| 1647 | |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1648 | // Do the work only if the # of partitions is changing. Along with being |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 1649 | // efficient, this prevents mucking with the location of the secondary |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1650 | // partition table, which causes problems when loading data from a RAID |
| 1651 | // array that's been expanded because this function is called when loading |
| 1652 | // data. |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1653 | if (((numEntries != numParts) || (partitions == NULL)) && (numEntries > 0)) { |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 1654 | newParts = new GPTPart [numEntries]; |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1655 | if (newParts != NULL) { |
| 1656 | if (partitions != NULL) { // existing partitions; copy them over |
| 1657 | GetPartRange(&i, &high); |
| 1658 | if (numEntries < (high + 1)) { // Highest entry too high for new # |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1659 | cout << "The highest-numbered partition is " << high + 1 |
| 1660 | << ", which is greater than the requested\n" |
| 1661 | << "partition table size of " << numEntries |
| 1662 | << "; cannot resize. Perhaps sorting will help.\n"; |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1663 | allOK = 0; |
srs5694 | 815fb65 | 2011-03-18 12:35:56 -0400 | [diff] [blame] | 1664 | delete[] newParts; |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1665 | } else { // go ahead with copy |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1666 | if (numEntries < numParts) |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1667 | copyNum = numEntries; |
| 1668 | else |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1669 | copyNum = numParts; |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1670 | for (i = 0; i < copyNum; i++) { |
| 1671 | newParts[i] = partitions[i]; |
| 1672 | } // for |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 1673 | delete[] partitions; |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1674 | partitions = newParts; |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1675 | } // if |
| 1676 | } else { // No existing partition table; just create it |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1677 | partitions = newParts; |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1678 | } // if/else existing partitions |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1679 | numParts = numEntries; |
srs5694 | 706e512 | 2012-01-21 13:47:24 -0500 | [diff] [blame] | 1680 | mainHeader.firstUsableLBA = ((numEntries * GPT_SIZE) / blockSize) + (((numEntries * GPT_SIZE) % blockSize) != 0) + 2 ; |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1681 | secondHeader.firstUsableLBA = mainHeader.firstUsableLBA; |
| 1682 | MoveSecondHeaderToEnd(); |
| 1683 | if (diskSize > 0) |
| 1684 | CheckGPTSize(); |
| 1685 | } else { // Bad memory allocation |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 1686 | cerr << "Error allocating memory for partition table! Size is unchanged!\n"; |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1687 | allOK = 0; |
| 1688 | } // if/else |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1689 | } // if/else |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1690 | mainHeader.numParts = numParts; |
| 1691 | secondHeader.numParts = numParts; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1692 | return (allOK); |
| 1693 | } // GPTData::SetGPTSize() |
| 1694 | |
| 1695 | // Blank the partition array |
| 1696 | void GPTData::BlankPartitions(void) { |
| 1697 | uint32_t i; |
| 1698 | |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1699 | for (i = 0; i < numParts; i++) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1700 | partitions[i].BlankPartition(); |
| 1701 | } // for |
| 1702 | } // GPTData::BlankPartitions() |
| 1703 | |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 1704 | // Delete a partition by number. Returns 1 if successful, |
| 1705 | // 0 if there was a problem. Returns 1 if partition was in |
| 1706 | // range, 0 if it was out of range. |
| 1707 | int GPTData::DeletePartition(uint32_t partNum) { |
| 1708 | uint64_t startSector, length; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1709 | uint32_t low, high, numUsedParts, retval = 1;; |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 1710 | |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1711 | numUsedParts = GetPartRange(&low, &high); |
| 1712 | if ((numUsedParts > 0) && (partNum >= low) && (partNum <= high)) { |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 1713 | // In case there's a protective MBR, look for & delete matching |
| 1714 | // MBR partition.... |
| 1715 | startSector = partitions[partNum].GetFirstLBA(); |
| 1716 | length = partitions[partNum].GetLengthLBA(); |
| 1717 | protectiveMBR.DeleteByLocation(startSector, length); |
| 1718 | |
| 1719 | // Now delete the GPT partition |
| 1720 | partitions[partNum].BlankPartition(); |
| 1721 | } else { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1722 | cerr << "Partition number " << partNum + 1 << " out of range!\n"; |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 1723 | retval = 0; |
| 1724 | } // if/else |
| 1725 | return retval; |
| 1726 | } // GPTData::DeletePartition(uint32_t partNum) |
| 1727 | |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1728 | // Non-interactively create a partition. |
| 1729 | // Returns 1 if the operation was successful, 0 if a problem was discovered. |
srs5694 | e321d44 | 2010-01-29 17:44:04 -0500 | [diff] [blame] | 1730 | uint32_t GPTData::CreatePartition(uint32_t partNum, uint64_t startSector, uint64_t endSector) { |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 1731 | int retval = 1; // assume there'll be no problems |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 1732 | uint64_t origSector = startSector; |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 1733 | |
| 1734 | if (IsFreePartNum(partNum)) { |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 1735 | if (Align(&startSector)) { |
| 1736 | cout << "Information: Moved requested sector from " << origSector << " to " |
| 1737 | << startSector << " in\norder to align on " << sectorAlignment |
| 1738 | << "-sector boundaries.\n"; |
| 1739 | } // if |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 1740 | if (IsFree(startSector) && (startSector <= endSector)) { |
| 1741 | if (FindLastInFree(startSector) >= endSector) { |
| 1742 | partitions[partNum].SetFirstLBA(startSector); |
| 1743 | partitions[partNum].SetLastLBA(endSector); |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame] | 1744 | partitions[partNum].SetType(DEFAULT_GPT_TYPE); |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 1745 | partitions[partNum].RandomizeUniqueGUID(); |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 1746 | } else retval = 0; // if free space until endSector |
| 1747 | } else retval = 0; // if startSector is free |
| 1748 | } else retval = 0; // if legal partition number |
| 1749 | return retval; |
| 1750 | } // GPTData::CreatePartition(partNum, startSector, endSector) |
| 1751 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1752 | // Sort the GPT entries, eliminating gaps and making for a logical |
srs5694 | 9a46b04 | 2011-03-15 00:34:10 -0400 | [diff] [blame] | 1753 | // ordering. |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1754 | void GPTData::SortGPT(void) { |
srs5694 | 9a46b04 | 2011-03-15 00:34:10 -0400 | [diff] [blame] | 1755 | if (numParts > 0) |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 1756 | sort(partitions, partitions + numParts); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1757 | } // GPTData::SortGPT() |
| 1758 | |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1759 | // Swap the contents of two partitions. |
| 1760 | // Returns 1 if successful, 0 if either partition is out of range |
| 1761 | // (that is, not a legal number; either or both can be empty). |
| 1762 | // Note that if partNum1 = partNum2 and this number is in range, |
| 1763 | // it will be considered successful. |
| 1764 | int GPTData::SwapPartitions(uint32_t partNum1, uint32_t partNum2) { |
| 1765 | GPTPart temp; |
| 1766 | int allOK = 1; |
| 1767 | |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1768 | if ((partNum1 < numParts) && (partNum2 < numParts)) { |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 1769 | if (partNum1 != partNum2) { |
| 1770 | temp = partitions[partNum1]; |
| 1771 | partitions[partNum1] = partitions[partNum2]; |
| 1772 | partitions[partNum2] = temp; |
| 1773 | } // if |
| 1774 | } else allOK = 0; // partition numbers are valid |
| 1775 | return allOK; |
| 1776 | } // GPTData::SwapPartitions() |
| 1777 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1778 | // Set up data structures for entirely new set of partitions on the |
| 1779 | // specified device. Returns 1 if OK, 0 if there were problems. |
srs5694 | e35eb1b | 2009-09-14 00:29:34 -0400 | [diff] [blame] | 1780 | // Note that this function does NOT clear the protectiveMBR data |
| 1781 | // structure, since it may hold the original MBR partitions if the |
| 1782 | // program was launched on an MBR disk, and those may need to be |
| 1783 | // converted to GPT format. |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1784 | int GPTData::ClearGPTData(void) { |
srs5694 | e35eb1b | 2009-09-14 00:29:34 -0400 | [diff] [blame] | 1785 | int goOn = 1, i; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1786 | |
| 1787 | // Set up the partition table.... |
srs5694 | 9a46b04 | 2011-03-15 00:34:10 -0400 | [diff] [blame] | 1788 | delete[] partitions; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1789 | partitions = NULL; |
| 1790 | SetGPTSize(NUM_GPT_ENTRIES); |
| 1791 | |
| 1792 | // Now initialize a bunch of stuff that's static.... |
| 1793 | mainHeader.signature = GPT_SIGNATURE; |
| 1794 | mainHeader.revision = 0x00010000; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 1795 | mainHeader.headerSize = HEADER_SIZE; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1796 | mainHeader.reserved = 0; |
| 1797 | mainHeader.currentLBA = UINT64_C(1); |
| 1798 | mainHeader.partitionEntriesLBA = (uint64_t) 2; |
| 1799 | mainHeader.sizeOfPartitionEntries = GPT_SIZE; |
| 1800 | for (i = 0; i < GPT_RESERVED; i++) { |
| 1801 | mainHeader.reserved2[i] = '\0'; |
| 1802 | } // for |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 1803 | if (blockSize > 0) |
| 1804 | sectorAlignment = DEFAULT_ALIGNMENT * SECTOR_SIZE / blockSize; |
| 1805 | else |
| 1806 | sectorAlignment = DEFAULT_ALIGNMENT; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1807 | |
| 1808 | // Now some semi-static items (computed based on end of disk) |
| 1809 | mainHeader.backupLBA = diskSize - UINT64_C(1); |
| 1810 | mainHeader.lastUsableLBA = diskSize - mainHeader.firstUsableLBA; |
| 1811 | |
| 1812 | // Set a unique GUID for the disk, based on random numbers |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 1813 | mainHeader.diskGUID.Randomize(); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1814 | |
| 1815 | // Copy main header to backup header |
| 1816 | RebuildSecondHeader(); |
| 1817 | |
| 1818 | // Blank out the partitions array.... |
| 1819 | BlankPartitions(); |
| 1820 | |
| 1821 | // Flag all CRCs as being OK.... |
| 1822 | mainCrcOk = 1; |
| 1823 | secondCrcOk = 1; |
| 1824 | mainPartsCrcOk = 1; |
| 1825 | secondPartsCrcOk = 1; |
| 1826 | |
| 1827 | return (goOn); |
| 1828 | } // GPTData::ClearGPTData() |
| 1829 | |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1830 | // Set the location of the second GPT header data to the end of the disk. |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 1831 | // If the disk size has actually changed, this also adjusts the protective |
| 1832 | // entry in the MBR, since it's probably no longer correct. |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1833 | // Used internally and called by the 'e' option on the recovery & |
| 1834 | // transformation menu, to help users of RAID arrays who add disk space |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 1835 | // to their arrays or to adjust data structures in restore operations |
| 1836 | // involving unequal-sized disks. |
srs5694 | 247657a | 2009-11-26 18:36:12 -0500 | [diff] [blame] | 1837 | void GPTData::MoveSecondHeaderToEnd() { |
srs5694 | 8bb7876 | 2009-11-24 15:43:49 -0500 | [diff] [blame] | 1838 | mainHeader.backupLBA = secondHeader.currentLBA = diskSize - UINT64_C(1); |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 1839 | if (mainHeader.lastUsableLBA != diskSize - mainHeader.firstUsableLBA) { |
| 1840 | if (protectiveMBR.GetValidity() == hybrid) { |
| 1841 | protectiveMBR.OptimizeEESize(); |
| 1842 | RecomputeCHS(); |
| 1843 | } // if |
| 1844 | if (protectiveMBR.GetValidity() == gpt) |
| 1845 | MakeProtectiveMBR(); |
| 1846 | } // if |
srs5694 | 8bb7876 | 2009-11-24 15:43:49 -0500 | [diff] [blame] | 1847 | mainHeader.lastUsableLBA = secondHeader.lastUsableLBA = diskSize - mainHeader.firstUsableLBA; |
| 1848 | secondHeader.partitionEntriesLBA = secondHeader.lastUsableLBA + UINT64_C(1); |
| 1849 | } // GPTData::FixSecondHeaderLocation() |
| 1850 | |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 1851 | // Sets the partition's name to the specified UnicodeString without |
| 1852 | // user interaction. |
| 1853 | // Returns 1 on success, 0 on failure (invalid partition number). |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 1854 | int GPTData::SetName(uint32_t partNum, const UnicodeString & theName) { |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 1855 | int retval = 1; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1856 | |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 1857 | if (IsUsedPartNum(partNum)) |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 1858 | partitions[partNum].SetName(theName); |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 1859 | else |
| 1860 | retval = 0; |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 1861 | |
| 1862 | return retval; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1863 | } // GPTData::SetName |
| 1864 | |
| 1865 | // Set the disk GUID to the specified value. Note that the header CRCs must |
| 1866 | // be recomputed after calling this function. |
| 1867 | void GPTData::SetDiskGUID(GUIDData newGUID) { |
| 1868 | mainHeader.diskGUID = newGUID; |
| 1869 | secondHeader.diskGUID = newGUID; |
| 1870 | } // SetDiskGUID() |
| 1871 | |
| 1872 | // Set the unique GUID of the specified partition. Returns 1 on |
| 1873 | // successful completion, 0 if there were problems (invalid |
| 1874 | // partition number). |
| 1875 | int GPTData::SetPartitionGUID(uint32_t pn, GUIDData theGUID) { |
| 1876 | int retval = 0; |
| 1877 | |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1878 | if (pn < numParts) { |
srs5694 | e69e680 | 2012-01-20 22:37:12 -0500 | [diff] [blame] | 1879 | if (partitions[pn].IsUsed()) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1880 | partitions[pn].SetUniqueGUID(theGUID); |
| 1881 | retval = 1; |
| 1882 | } // if |
| 1883 | } // if |
| 1884 | return retval; |
| 1885 | } // GPTData::SetPartitionGUID() |
| 1886 | |
srs5694 | 9ba5421 | 2010-05-18 23:24:02 -0400 | [diff] [blame] | 1887 | // Set new random GUIDs for the disk and all partitions. Intended to be used |
| 1888 | // after disk cloning or similar operations that don't randomize the GUIDs. |
| 1889 | void GPTData::RandomizeGUIDs(void) { |
| 1890 | uint32_t i; |
| 1891 | |
| 1892 | mainHeader.diskGUID.Randomize(); |
| 1893 | secondHeader.diskGUID = mainHeader.diskGUID; |
| 1894 | for (i = 0; i < numParts; i++) |
| 1895 | if (partitions[i].IsUsed()) |
| 1896 | partitions[i].RandomizeUniqueGUID(); |
| 1897 | } // GPTData::RandomizeGUIDs() |
| 1898 | |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 1899 | // Change partition type code non-interactively. Returns 1 if |
| 1900 | // successful, 0 if not.... |
srs5694 | 327129e | 2010-09-22 01:07:31 -0400 | [diff] [blame] | 1901 | int GPTData::ChangePartType(uint32_t partNum, PartType theGUID) { |
| 1902 | int retval = 1; |
| 1903 | |
| 1904 | if (!IsFreePartNum(partNum)) { |
| 1905 | partitions[partNum].SetType(theGUID); |
| 1906 | } else retval = 0; |
| 1907 | return retval; |
| 1908 | } // GPTData::ChangePartType() |
| 1909 | |
srs5694 | 9ba5421 | 2010-05-18 23:24:02 -0400 | [diff] [blame] | 1910 | // Recompute the CHS values of all the MBR partitions. Used to reset |
| 1911 | // CHS values that some BIOSes require, despite the fact that the |
| 1912 | // resulting CHS values violate the GPT standard. |
| 1913 | void GPTData::RecomputeCHS(void) { |
| 1914 | int i; |
| 1915 | |
| 1916 | for (i = 0; i < 4; i++) |
| 1917 | protectiveMBR.RecomputeCHS(i); |
| 1918 | } // GPTData::RecomputeCHS() |
| 1919 | |
srs5694 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame] | 1920 | // Adjust sector number so that it falls on a sector boundary that's a |
| 1921 | // multiple of sectorAlignment. This is done to improve the performance |
| 1922 | // of Western Digital Advanced Format disks and disks with similar |
| 1923 | // technology from other companies, which use 4096-byte sectors |
| 1924 | // internally although they translate to 512-byte sectors for the |
| 1925 | // benefit of the OS. If partitions aren't properly aligned on these |
| 1926 | // disks, some filesystem data structures can span multiple physical |
| 1927 | // sectors, degrading performance. This function should be called |
| 1928 | // only on the FIRST sector of the partition, not the last! |
| 1929 | // This function returns 1 if the alignment was altered, 0 if it |
| 1930 | // was unchanged. |
| 1931 | int GPTData::Align(uint64_t* sector) { |
| 1932 | int retval = 0, sectorOK = 0; |
srs5694 | 00b6d7a | 2011-06-26 22:40:06 -0400 | [diff] [blame] | 1933 | uint64_t earlier, later, testSector; |
srs5694 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame] | 1934 | |
| 1935 | if ((*sector % sectorAlignment) != 0) { |
srs5694 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame] | 1936 | earlier = (*sector / sectorAlignment) * sectorAlignment; |
| 1937 | later = earlier + (uint64_t) sectorAlignment; |
| 1938 | |
| 1939 | // Check to see that every sector between the earlier one and the |
| 1940 | // requested one is clear, and that it's not too early.... |
| 1941 | if (earlier >= mainHeader.firstUsableLBA) { |
srs5694 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame] | 1942 | sectorOK = 1; |
| 1943 | testSector = earlier; |
| 1944 | do { |
| 1945 | sectorOK = IsFree(testSector++); |
| 1946 | } while ((sectorOK == 1) && (testSector < *sector)); |
| 1947 | if (sectorOK == 1) { |
| 1948 | *sector = earlier; |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 1949 | retval = 1; |
srs5694 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame] | 1950 | } // if |
| 1951 | } // if firstUsableLBA check |
| 1952 | |
| 1953 | // If couldn't move the sector earlier, try to move it later instead.... |
| 1954 | if ((sectorOK != 1) && (later <= mainHeader.lastUsableLBA)) { |
| 1955 | sectorOK = 1; |
| 1956 | testSector = later; |
| 1957 | do { |
| 1958 | sectorOK = IsFree(testSector--); |
| 1959 | } while ((sectorOK == 1) && (testSector > *sector)); |
| 1960 | if (sectorOK == 1) { |
| 1961 | *sector = later; |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 1962 | retval = 1; |
srs5694 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame] | 1963 | } // if |
| 1964 | } // if |
srs5694 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame] | 1965 | } // if |
| 1966 | return retval; |
| 1967 | } // GPTData::Align() |
| 1968 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1969 | /******************************************************** |
| 1970 | * * |
| 1971 | * Functions that return data about GPT data structures * |
| 1972 | * (most of these are inline in gpt.h) * |
| 1973 | * * |
| 1974 | ********************************************************/ |
| 1975 | |
| 1976 | // Find the low and high used partition numbers (numbered from 0). |
| 1977 | // Return value is the number of partitions found. Note that the |
| 1978 | // *low and *high values are both set to 0 when no partitions |
| 1979 | // are found, as well as when a single partition in the first |
| 1980 | // position exists. Thus, the return value is the only way to |
| 1981 | // tell when no partitions exist. |
| 1982 | int GPTData::GetPartRange(uint32_t *low, uint32_t *high) { |
| 1983 | uint32_t i; |
| 1984 | int numFound = 0; |
| 1985 | |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1986 | *low = numParts + 1; // code for "not found" |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1987 | *high = 0; |
srs5694 | 9a46b04 | 2011-03-15 00:34:10 -0400 | [diff] [blame] | 1988 | for (i = 0; i < numParts; i++) { |
srs5694 | e69e680 | 2012-01-20 22:37:12 -0500 | [diff] [blame] | 1989 | if (partitions[i].IsUsed()) { // it exists |
srs5694 | 9a46b04 | 2011-03-15 00:34:10 -0400 | [diff] [blame] | 1990 | *high = i; // since we're counting up, set the high value |
| 1991 | // Set the low value only if it's not yet found... |
| 1992 | if (*low == (numParts + 1)) *low = i; |
| 1993 | numFound++; |
| 1994 | } // if |
| 1995 | } // for |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 1996 | |
| 1997 | // Above will leave *low pointing to its "not found" value if no partitions |
| 1998 | // are defined, so reset to 0 if this is the case.... |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 1999 | if (*low == (numParts + 1)) |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2000 | *low = 0; |
| 2001 | return numFound; |
| 2002 | } // GPTData::GetPartRange() |
| 2003 | |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 2004 | // Returns the value of the first free partition, or -1 if none is |
| 2005 | // unused. |
| 2006 | int GPTData::FindFirstFreePart(void) { |
| 2007 | int i = 0; |
| 2008 | |
| 2009 | if (partitions != NULL) { |
srs5694 | 9a46b04 | 2011-03-15 00:34:10 -0400 | [diff] [blame] | 2010 | while ((i < (int) numParts) && (partitions[i].IsUsed())) |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 2011 | i++; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 2012 | if (i >= (int) numParts) |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 2013 | i = -1; |
| 2014 | } else i = -1; |
| 2015 | return i; |
| 2016 | } // GPTData::FindFirstFreePart() |
| 2017 | |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 2018 | // Returns the number of defined partitions. |
| 2019 | uint32_t GPTData::CountParts(void) { |
srs5694 | e321d44 | 2010-01-29 17:44:04 -0500 | [diff] [blame] | 2020 | uint32_t i, counted = 0; |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 2021 | |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 2022 | for (i = 0; i < numParts; i++) { |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 2023 | if (partitions[i].IsUsed()) |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 2024 | counted++; |
| 2025 | } // for |
| 2026 | return counted; |
| 2027 | } // GPTData::CountParts() |
| 2028 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2029 | /**************************************************** |
| 2030 | * * |
| 2031 | * Functions that return data about disk free space * |
| 2032 | * * |
| 2033 | ****************************************************/ |
| 2034 | |
| 2035 | // Find the first available block after the starting point; returns 0 if |
| 2036 | // there are no available blocks left |
| 2037 | uint64_t GPTData::FindFirstAvailable(uint64_t start) { |
| 2038 | uint64_t first; |
| 2039 | uint32_t i; |
| 2040 | int firstMoved = 0; |
| 2041 | |
| 2042 | // Begin from the specified starting point or from the first usable |
| 2043 | // LBA, whichever is greater... |
| 2044 | if (start < mainHeader.firstUsableLBA) |
| 2045 | first = mainHeader.firstUsableLBA; |
| 2046 | else |
| 2047 | first = start; |
| 2048 | |
| 2049 | // ...now search through all partitions; if first is within an |
| 2050 | // existing partition, move it to the next sector after that |
| 2051 | // partition and repeat. If first was moved, set firstMoved |
| 2052 | // flag; repeat until firstMoved is not set, so as to catch |
| 2053 | // cases where partitions are out of sequential order.... |
| 2054 | do { |
| 2055 | firstMoved = 0; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 2056 | for (i = 0; i < numParts; i++) { |
srs5694 | e69e680 | 2012-01-20 22:37:12 -0500 | [diff] [blame] | 2057 | if ((partitions[i].IsUsed()) && (first >= partitions[i].GetFirstLBA()) && |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 2058 | (first <= partitions[i].GetLastLBA())) { // in existing part. |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2059 | first = partitions[i].GetLastLBA() + 1; |
| 2060 | firstMoved = 1; |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 2061 | } // if |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2062 | } // for |
| 2063 | } while (firstMoved == 1); |
| 2064 | if (first > mainHeader.lastUsableLBA) |
| 2065 | first = 0; |
| 2066 | return (first); |
| 2067 | } // GPTData::FindFirstAvailable() |
| 2068 | |
| 2069 | // Finds the first available sector in the largest block of unallocated |
| 2070 | // space on the disk. Returns 0 if there are no available blocks left |
| 2071 | uint64_t GPTData::FindFirstInLargest(void) { |
srs5694 | e35eb1b | 2009-09-14 00:29:34 -0400 | [diff] [blame] | 2072 | uint64_t start, firstBlock, lastBlock, segmentSize, selectedSize = 0, selectedSegment = 0; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2073 | |
| 2074 | start = 0; |
| 2075 | do { |
| 2076 | firstBlock = FindFirstAvailable(start); |
| 2077 | if (firstBlock != UINT32_C(0)) { // something's free... |
| 2078 | lastBlock = FindLastInFree(firstBlock); |
| 2079 | segmentSize = lastBlock - firstBlock + UINT32_C(1); |
| 2080 | if (segmentSize > selectedSize) { |
| 2081 | selectedSize = segmentSize; |
| 2082 | selectedSegment = firstBlock; |
| 2083 | } // if |
| 2084 | start = lastBlock + 1; |
| 2085 | } // if |
| 2086 | } while (firstBlock != 0); |
| 2087 | return selectedSegment; |
| 2088 | } // GPTData::FindFirstInLargest() |
| 2089 | |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 2090 | // Find the last available block on the disk. |
srs5694 | f5dfbfa | 2013-02-14 20:47:14 -0500 | [diff] [blame^] | 2091 | // Returns 0 if there are no available sectors |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 2092 | uint64_t GPTData::FindLastAvailable(void) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2093 | uint64_t last; |
| 2094 | uint32_t i; |
| 2095 | int lastMoved = 0; |
| 2096 | |
| 2097 | // Start by assuming the last usable LBA is available.... |
| 2098 | last = mainHeader.lastUsableLBA; |
| 2099 | |
| 2100 | // ...now, similar to algorithm in FindFirstAvailable(), search |
| 2101 | // through all partitions, moving last when it's in an existing |
| 2102 | // partition. Set the lastMoved flag so we repeat to catch cases |
| 2103 | // where partitions are out of logical order. |
| 2104 | do { |
| 2105 | lastMoved = 0; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 2106 | for (i = 0; i < numParts; i++) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2107 | if ((last >= partitions[i].GetFirstLBA()) && |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 2108 | (last <= partitions[i].GetLastLBA())) { // in existing part. |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2109 | last = partitions[i].GetFirstLBA() - 1; |
| 2110 | lastMoved = 1; |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 2111 | } // if |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2112 | } // for |
| 2113 | } while (lastMoved == 1); |
| 2114 | if (last < mainHeader.firstUsableLBA) |
| 2115 | last = 0; |
| 2116 | return (last); |
| 2117 | } // GPTData::FindLastAvailable() |
| 2118 | |
| 2119 | // Find the last available block in the free space pointed to by start. |
| 2120 | uint64_t GPTData::FindLastInFree(uint64_t start) { |
| 2121 | uint64_t nearestStart; |
| 2122 | uint32_t i; |
| 2123 | |
| 2124 | nearestStart = mainHeader.lastUsableLBA; |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 2125 | for (i = 0; i < numParts; i++) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2126 | if ((nearestStart > partitions[i].GetFirstLBA()) && |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 2127 | (partitions[i].GetFirstLBA() > start)) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2128 | nearestStart = partitions[i].GetFirstLBA() - 1; |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 2129 | } // if |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2130 | } // for |
| 2131 | return (nearestStart); |
| 2132 | } // GPTData::FindLastInFree() |
| 2133 | |
| 2134 | // Finds the total number of free blocks, the number of segments in which |
| 2135 | // they reside, and the size of the largest of those segments |
srs5694 | e321d44 | 2010-01-29 17:44:04 -0500 | [diff] [blame] | 2136 | uint64_t GPTData::FindFreeBlocks(uint32_t *numSegments, uint64_t *largestSegment) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2137 | uint64_t start = UINT64_C(0); // starting point for each search |
| 2138 | uint64_t totalFound = UINT64_C(0); // running total |
| 2139 | uint64_t firstBlock; // first block in a segment |
| 2140 | uint64_t lastBlock; // last block in a segment |
| 2141 | uint64_t segmentSize; // size of segment in blocks |
srs5694 | e321d44 | 2010-01-29 17:44:04 -0500 | [diff] [blame] | 2142 | uint32_t num = 0; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2143 | |
| 2144 | *largestSegment = UINT64_C(0); |
srs5694 | c54e9b4 | 2010-05-01 21:04:23 -0400 | [diff] [blame] | 2145 | if (diskSize > 0) { |
| 2146 | do { |
| 2147 | firstBlock = FindFirstAvailable(start); |
| 2148 | if (firstBlock != UINT64_C(0)) { // something's free... |
| 2149 | lastBlock = FindLastInFree(firstBlock); |
| 2150 | segmentSize = lastBlock - firstBlock + UINT64_C(1); |
| 2151 | if (segmentSize > *largestSegment) { |
| 2152 | *largestSegment = segmentSize; |
| 2153 | } // if |
| 2154 | totalFound += segmentSize; |
| 2155 | num++; |
| 2156 | start = lastBlock + 1; |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2157 | } // if |
srs5694 | c54e9b4 | 2010-05-01 21:04:23 -0400 | [diff] [blame] | 2158 | } while (firstBlock != 0); |
| 2159 | } // if |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2160 | *numSegments = num; |
| 2161 | return totalFound; |
| 2162 | } // GPTData::FindFreeBlocks() |
| 2163 | |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 2164 | // Returns 1 if sector is unallocated, 0 if it's allocated to a partition. |
| 2165 | // If it's allocated, return the partition number to which it's allocated |
| 2166 | // in partNum, if that variable is non-NULL. (A value of UINT32_MAX is |
| 2167 | // returned in partNum if the sector is in use by basic GPT data structures.) |
| 2168 | int GPTData::IsFree(uint64_t sector, uint32_t *partNum) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2169 | int isFree = 1; |
| 2170 | uint32_t i; |
| 2171 | |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 2172 | for (i = 0; i < numParts; i++) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2173 | if ((sector >= partitions[i].GetFirstLBA()) && |
| 2174 | (sector <= partitions[i].GetLastLBA())) { |
| 2175 | isFree = 0; |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 2176 | if (partNum != NULL) |
| 2177 | *partNum = i; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 2178 | } // if |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2179 | } // for |
srs5694 | e35eb1b | 2009-09-14 00:29:34 -0400 | [diff] [blame] | 2180 | if ((sector < mainHeader.firstUsableLBA) || |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2181 | (sector > mainHeader.lastUsableLBA)) { |
| 2182 | isFree = 0; |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 2183 | if (partNum != NULL) |
| 2184 | *partNum = UINT32_MAX; |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 2185 | } // if |
| 2186 | return (isFree); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2187 | } // GPTData::IsFree() |
| 2188 | |
srs5694 | 815fb65 | 2011-03-18 12:35:56 -0400 | [diff] [blame] | 2189 | // Returns 1 if partNum is unused AND if it's a legal value. |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 2190 | int GPTData::IsFreePartNum(uint32_t partNum) { |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 2191 | return ((partNum < numParts) && (partitions != NULL) && |
| 2192 | (!partitions[partNum].IsUsed())); |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 2193 | } // GPTData::IsFreePartNum() |
| 2194 | |
srs5694 | 815fb65 | 2011-03-18 12:35:56 -0400 | [diff] [blame] | 2195 | // Returns 1 if partNum is in use. |
| 2196 | int GPTData::IsUsedPartNum(uint32_t partNum) { |
| 2197 | return ((partNum < numParts) && (partitions != NULL) && |
| 2198 | (partitions[partNum].IsUsed())); |
| 2199 | } // GPTData::IsUsedPartNum() |
srs5694 | a8582cf | 2010-03-19 14:21:59 -0400 | [diff] [blame] | 2200 | |
| 2201 | /*********************************************************** |
| 2202 | * * |
| 2203 | * Change how functions work or return information on them * |
| 2204 | * * |
| 2205 | ***********************************************************/ |
| 2206 | |
| 2207 | // Set partition alignment value; partitions will begin on multiples of |
| 2208 | // the specified value |
| 2209 | void GPTData::SetAlignment(uint32_t n) { |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 2210 | if (n > 0) |
| 2211 | sectorAlignment = n; |
| 2212 | else |
| 2213 | cerr << "Attempt to set partition alignment to 0!\n"; |
srs5694 | a8582cf | 2010-03-19 14:21:59 -0400 | [diff] [blame] | 2214 | } // GPTData::SetAlignment() |
| 2215 | |
| 2216 | // Compute sector alignment based on the current partitions (if any). Each |
| 2217 | // partition's starting LBA is examined, and if it's divisible by a power-of-2 |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 2218 | // value less than or equal to the DEFAULT_ALIGNMENT value (adjusted for the |
| 2219 | // sector size), but not by the previously-located alignment value, then the |
| 2220 | // alignment value is adjusted down. If the computed alignment is less than 8 |
| 2221 | // and the disk is bigger than SMALLEST_ADVANCED_FORMAT, resets it to 8. This |
srs5694 | d8eed46 | 2012-12-15 01:55:21 -0500 | [diff] [blame] | 2222 | // is a safety measure for Advanced Format drives. If no partitions are |
| 2223 | // defined, the alignment value is set to DEFAULT_ALIGNMENT (2048) (or an |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 2224 | // adjustment of that based on the current sector size). The result is that new |
srs5694 | 8a4ddfc | 2010-03-21 19:05:49 -0400 | [diff] [blame] | 2225 | // drives are aligned to 2048-sector multiples but the program won't complain |
| 2226 | // about other alignments on existing disks unless a smaller-than-8 alignment |
srs5694 | d8eed46 | 2012-12-15 01:55:21 -0500 | [diff] [blame] | 2227 | // is used on big disks (as safety for Advanced Format drives). |
srs5694 | a8582cf | 2010-03-19 14:21:59 -0400 | [diff] [blame] | 2228 | // Returns the computed alignment value. |
| 2229 | uint32_t GPTData::ComputeAlignment(void) { |
| 2230 | uint32_t i = 0, found, exponent = 31; |
srs5694 | ab4b043 | 2010-09-25 20:39:52 -0400 | [diff] [blame] | 2231 | uint32_t align = DEFAULT_ALIGNMENT; |
srs5694 | a8582cf | 2010-03-19 14:21:59 -0400 | [diff] [blame] | 2232 | |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 2233 | if (blockSize > 0) |
| 2234 | align = DEFAULT_ALIGNMENT * SECTOR_SIZE / blockSize; |
| 2235 | exponent = (uint32_t) log2(align); |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 2236 | for (i = 0; i < numParts; i++) { |
srs5694 | a8582cf | 2010-03-19 14:21:59 -0400 | [diff] [blame] | 2237 | if (partitions[i].IsUsed()) { |
| 2238 | found = 0; |
| 2239 | while (!found) { |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 2240 | align = UINT64_C(1) << exponent; |
srs5694 | a8582cf | 2010-03-19 14:21:59 -0400 | [diff] [blame] | 2241 | if ((partitions[i].GetFirstLBA() % align) == 0) { |
| 2242 | found = 1; |
| 2243 | } else { |
| 2244 | exponent--; |
| 2245 | } // if/else |
| 2246 | } // while |
| 2247 | } // if |
| 2248 | } // for |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 2249 | if ((align < MIN_AF_ALIGNMENT) && (diskSize >= SMALLEST_ADVANCED_FORMAT)) |
| 2250 | align = MIN_AF_ALIGNMENT; |
| 2251 | sectorAlignment = align; |
srs5694 | a8582cf | 2010-03-19 14:21:59 -0400 | [diff] [blame] | 2252 | return align; |
| 2253 | } // GPTData::ComputeAlignment() |
| 2254 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2255 | /******************************** |
| 2256 | * * |
| 2257 | * Endianness support functions * |
| 2258 | * * |
| 2259 | ********************************/ |
| 2260 | |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 2261 | void GPTData::ReverseHeaderBytes(struct GPTHeader* header) { |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 2262 | ReverseBytes(&header->signature, 8); |
| 2263 | ReverseBytes(&header->revision, 4); |
| 2264 | ReverseBytes(&header->headerSize, 4); |
| 2265 | ReverseBytes(&header->headerCRC, 4); |
| 2266 | ReverseBytes(&header->reserved, 4); |
| 2267 | ReverseBytes(&header->currentLBA, 8); |
| 2268 | ReverseBytes(&header->backupLBA, 8); |
| 2269 | ReverseBytes(&header->firstUsableLBA, 8); |
| 2270 | ReverseBytes(&header->lastUsableLBA, 8); |
| 2271 | ReverseBytes(&header->partitionEntriesLBA, 8); |
| 2272 | ReverseBytes(&header->numParts, 4); |
| 2273 | ReverseBytes(&header->sizeOfPartitionEntries, 4); |
| 2274 | ReverseBytes(&header->partitionEntriesCRC, 4); |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 2275 | ReverseBytes(header->reserved2, GPT_RESERVED); |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 2276 | } // GPTData::ReverseHeaderBytes() |
| 2277 | |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 2278 | // Reverse byte order for all partitions. |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 2279 | void GPTData::ReversePartitionBytes() { |
| 2280 | uint32_t i; |
| 2281 | |
srs5694 | 0283dae | 2010-04-28 16:44:34 -0400 | [diff] [blame] | 2282 | for (i = 0; i < numParts; i++) { |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 2283 | partitions[i].ReversePartBytes(); |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 2284 | } // for |
| 2285 | } // GPTData::ReversePartitionBytes() |
| 2286 | |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 2287 | // Validate partition number |
| 2288 | bool GPTData::ValidPartNum (const uint32_t partNum) { |
| 2289 | if (partNum >= numParts) { |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 2290 | cerr << "Partition number out of range: " << partNum << "\n"; |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 2291 | return false; |
| 2292 | } // if |
| 2293 | return true; |
| 2294 | } // GPTData::ValidPartNum |
| 2295 | |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 2296 | // Return a single partition for inspection (not modification!) by other |
| 2297 | // functions. |
| 2298 | const GPTPart & GPTData::operator[](uint32_t partNum) const { |
| 2299 | if (partNum >= numParts) { |
srs5694 | 815fb65 | 2011-03-18 12:35:56 -0400 | [diff] [blame] | 2300 | cerr << "Partition number out of range (" << partNum << " requested, but only " |
| 2301 | << numParts << " available)\n"; |
| 2302 | exit(1); |
| 2303 | } // if |
| 2304 | if (partitions == NULL) { |
| 2305 | cerr << "No partitions defined in GPTData::operator[]; fatal error!\n"; |
| 2306 | exit(1); |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 2307 | } // if |
| 2308 | return partitions[partNum]; |
| 2309 | } // operator[] |
| 2310 | |
| 2311 | // Return (not for modification!) the disk's GUID value |
| 2312 | const GUIDData & GPTData::GetDiskGUID(void) const { |
| 2313 | return mainHeader.diskGUID; |
| 2314 | } // GPTData::GetDiskGUID() |
| 2315 | |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 2316 | // Manage attributes for a partition, based on commands passed to this function. |
| 2317 | // (Function is non-interactive.) |
| 2318 | // Returns 1 if a modification command succeeded, 0 if the command should not have |
| 2319 | // modified data, and -1 if a modification command failed. |
| 2320 | int GPTData::ManageAttributes(int partNum, const string & command, const string & bits) { |
| 2321 | int retval = 0; |
| 2322 | Attributes theAttr; |
| 2323 | |
| 2324 | if (command == "show") { |
| 2325 | ShowAttributes(partNum); |
| 2326 | } else if (command == "get") { |
| 2327 | GetAttribute(partNum, bits); |
| 2328 | } else { |
| 2329 | theAttr = partitions[partNum].GetAttributes(); |
| 2330 | if (theAttr.OperateOnAttributes(partNum, command, bits)) { |
| 2331 | partitions[partNum].SetAttributes(theAttr.GetAttributes()); |
| 2332 | retval = 1; |
| 2333 | } else { |
| 2334 | retval = -1; |
| 2335 | } // if/else |
| 2336 | } // if/elseif/else |
| 2337 | |
| 2338 | return retval; |
| 2339 | } // GPTData::ManageAttributes() |
| 2340 | |
| 2341 | // Show all attributes for a specified partition.... |
| 2342 | void GPTData::ShowAttributes(const uint32_t partNum) { |
srs5694 | e69e680 | 2012-01-20 22:37:12 -0500 | [diff] [blame] | 2343 | if (partitions[partNum].IsUsed()) |
| 2344 | partitions[partNum].ShowAttributes(partNum); |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 2345 | } // GPTData::ShowAttributes |
| 2346 | |
| 2347 | // Show whether a single attribute bit is set (terse output)... |
| 2348 | void GPTData::GetAttribute(const uint32_t partNum, const string& attributeBits) { |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 2349 | partitions[partNum].GetAttributes().OperateOnAttributes(partNum, "get", attributeBits); |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 2350 | } // GPTData::GetAttribute |
| 2351 | |
| 2352 | |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 2353 | /****************************************** |
| 2354 | * * |
| 2355 | * Additional non-class support functions * |
| 2356 | * * |
| 2357 | ******************************************/ |
| 2358 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 2359 | // Check to be sure that data type sizes are correct. The basic types (uint*_t) should |
| 2360 | // never fail these tests, but the struct types may fail depending on compile options. |
| 2361 | // Specifically, the -fpack-struct option to gcc may be required to ensure proper structure |
| 2362 | // sizes. |
| 2363 | int SizesOK(void) { |
| 2364 | int allOK = 1; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 2365 | |
| 2366 | if (sizeof(uint8_t) != 1) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 2367 | cerr << "uint8_t is " << sizeof(uint8_t) << " bytes, should be 1 byte; aborting!\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 2368 | allOK = 0; |
| 2369 | } // if |
| 2370 | if (sizeof(uint16_t) != 2) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 2371 | cerr << "uint16_t is " << sizeof(uint16_t) << " bytes, should be 2 bytes; aborting!\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 2372 | allOK = 0; |
| 2373 | } // if |
| 2374 | if (sizeof(uint32_t) != 4) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 2375 | cerr << "uint32_t is " << sizeof(uint32_t) << " bytes, should be 4 bytes; aborting!\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 2376 | allOK = 0; |
| 2377 | } // if |
| 2378 | if (sizeof(uint64_t) != 8) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 2379 | cerr << "uint64_t is " << sizeof(uint64_t) << " bytes, should be 8 bytes; aborting!\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 2380 | allOK = 0; |
| 2381 | } // if |
| 2382 | if (sizeof(struct MBRRecord) != 16) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 2383 | cerr << "MBRRecord is " << sizeof(MBRRecord) << " bytes, should be 16 bytes; aborting!\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 2384 | allOK = 0; |
| 2385 | } // if |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 2386 | if (sizeof(struct TempMBR) != 512) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 2387 | cerr << "TempMBR is " << sizeof(TempMBR) << " bytes, should be 512 bytes; aborting!\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 2388 | allOK = 0; |
| 2389 | } // if |
| 2390 | if (sizeof(struct GPTHeader) != 512) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 2391 | cerr << "GPTHeader is " << sizeof(GPTHeader) << " bytes, should be 512 bytes; aborting!\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 2392 | allOK = 0; |
| 2393 | } // if |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 2394 | if (sizeof(GPTPart) != 128) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 2395 | cerr << "GPTPart is " << sizeof(GPTPart) << " bytes, should be 128 bytes; aborting!\n"; |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 2396 | allOK = 0; |
| 2397 | } // if |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 2398 | if (sizeof(GUIDData) != 16) { |
| 2399 | cerr << "GUIDData is " << sizeof(GUIDData) << " bytes, should be 16 bytes; aborting!\n"; |
| 2400 | allOK = 0; |
| 2401 | } // if |
| 2402 | if (sizeof(PartType) != 16) { |
| 2403 | cerr << "PartType is " << sizeof(GUIDData) << " bytes, should be 16 bytes; aborting!\n"; |
| 2404 | allOK = 0; |
| 2405 | } // if |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 2406 | return (allOK); |
| 2407 | } // SizesOK() |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 2408 | |