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