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