srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1 | /* mbr.cc -- Functions for loading, saving, and manipulating legacy MBR partition |
| 2 | data. */ |
| 3 | |
| 4 | /* By Rod Smith, January to February, 2009 */ |
| 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> |
| 13 | #include <unistd.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <stdint.h> |
| 16 | #include <fcntl.h> |
| 17 | #include <string.h> |
| 18 | #include <time.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <errno.h> |
| 21 | #include "mbr.h" |
| 22 | #include "support.h" |
| 23 | |
| 24 | using namespace std; |
| 25 | |
| 26 | /**************************************** |
| 27 | * * |
| 28 | * MBRData class and related structures * |
| 29 | * * |
| 30 | ****************************************/ |
| 31 | |
| 32 | MBRData::MBRData(void) { |
| 33 | blockSize = SECTOR_SIZE; |
| 34 | diskSize = 0; |
| 35 | strcpy(device, ""); |
| 36 | state = invalid; |
| 37 | srand((unsigned int) time(NULL)); |
| 38 | EmptyMBR(); |
| 39 | } // MBRData default constructor |
| 40 | |
| 41 | MBRData::MBRData(char *filename) { |
| 42 | blockSize = SECTOR_SIZE; |
| 43 | diskSize = 0; |
| 44 | strcpy(device, filename); |
| 45 | state = invalid; |
| 46 | |
| 47 | srand((unsigned int) time(NULL)); |
| 48 | // Try to read the specified partition table, but if it fails.... |
| 49 | if (!ReadMBRData(filename)) { |
| 50 | EmptyMBR(); |
| 51 | strcpy(device, ""); |
| 52 | } // if |
| 53 | } // MBRData(char *filename) constructor |
| 54 | |
| 55 | MBRData::~MBRData(void) { |
| 56 | } // MBRData destructor |
| 57 | |
srs5694 | c0ca8f8 | 2009-08-20 21:35:25 -0400 | [diff] [blame] | 58 | // Empty all data. Meant mainly for calling by constructors, but it's also |
| 59 | // used by the hybrid MBR functions in the GPTData class. |
| 60 | void MBRData::EmptyMBR(int clearBootloader) { |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 61 | int i; |
| 62 | |
srs5694 | c0ca8f8 | 2009-08-20 21:35:25 -0400 | [diff] [blame] | 63 | // Zero out the boot loader section, the disk signature, and the |
| 64 | // 2-byte nulls area only if requested to do so. (This is the |
| 65 | // default.) |
| 66 | if (clearBootloader == 1) { |
| 67 | for (i = 0; i < 440; i++) |
| 68 | code[i] = 0; |
| 69 | diskSignature = (uint32_t) rand(); |
| 70 | nulls = 0; |
| 71 | } // if |
| 72 | |
| 73 | // Blank out the partitions |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 74 | for (i = 0; i < 4; i++) { |
| 75 | partitions[i].status = UINT8_C(0); |
| 76 | partitions[i].firstSector[0] = UINT8_C(0); |
| 77 | partitions[i].firstSector[1] = UINT8_C(0); |
| 78 | partitions[i].firstSector[2] = UINT8_C(0); |
| 79 | partitions[i].partitionType = UINT8_C(0); |
| 80 | partitions[i].lastSector[0] = UINT8_C(0); |
| 81 | partitions[i].lastSector[1] = UINT8_C(0); |
| 82 | partitions[i].lastSector[2] = UINT8_C(0); |
| 83 | partitions[i].firstLBA = UINT32_C(0); |
| 84 | partitions[i].lengthLBA = UINT32_C(0); |
| 85 | } // for |
| 86 | MBRSignature = MBR_SIGNATURE; |
| 87 | |
| 88 | blockSize = SECTOR_SIZE; |
| 89 | diskSize = 0; |
| 90 | for (i = 0; i < NUM_LOGICALS; i++) { |
| 91 | logicals[i].status = UINT8_C(0); |
| 92 | logicals[i].firstSector[0] = UINT8_C(0); |
| 93 | logicals[i].firstSector[1] = UINT8_C(0); |
| 94 | logicals[i].firstSector[2] = UINT8_C(0); |
| 95 | logicals[i].partitionType = UINT8_C(0); |
| 96 | logicals[i].lastSector[0] = UINT8_C(0); |
| 97 | logicals[i].lastSector[1] = UINT8_C(0); |
| 98 | logicals[i].lastSector[2] = UINT8_C(0); |
| 99 | logicals[i].firstLBA = UINT32_C(0); |
| 100 | logicals[i].lengthLBA = UINT32_C(0); |
| 101 | } // for |
| 102 | } // MBRData::EmptyMBR() |
| 103 | |
| 104 | // Read data from MBR. Returns 1 if read was successful (even if the |
| 105 | // data isn't a valid MBR), 0 if the read failed. |
| 106 | int MBRData::ReadMBRData(char* deviceFilename) { |
| 107 | int fd, allOK = 1; |
| 108 | |
| 109 | if ((fd = open(deviceFilename, O_RDONLY)) != -1) { |
| 110 | ReadMBRData(fd); |
| 111 | } else { |
| 112 | allOK = 0; |
| 113 | } // if |
| 114 | |
| 115 | close(fd); |
| 116 | |
| 117 | if (allOK) |
| 118 | strcpy(device, deviceFilename); |
| 119 | |
| 120 | return allOK; |
| 121 | } // MBRData::ReadMBRData(char* deviceFilename) |
| 122 | |
| 123 | // Read data from MBR. |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 124 | void MBRData::ReadMBRData(int fd, int checkBlockSize) { |
| 125 | int allOK = 1, i, j, maxLogicals = 0; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 126 | int err; |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 127 | TempMBR tempMBR; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 128 | |
| 129 | // Clear logical partition array |
| 130 | for (i = 0; i < NUM_LOGICALS; i++) { |
| 131 | logicals[i].status = UINT8_C(0); |
| 132 | logicals[i].firstSector[0] = UINT8_C(0); |
| 133 | logicals[i].firstSector[1] = UINT8_C(0); |
| 134 | logicals[i].firstSector[2] = UINT8_C(0); |
| 135 | logicals[i].partitionType = UINT8_C(0); |
| 136 | logicals[i].lastSector[0] = UINT8_C(0); |
| 137 | logicals[i].lastSector[1] = UINT8_C(0); |
| 138 | logicals[i].lastSector[2] = UINT8_C(0); |
| 139 | logicals[i].firstLBA = UINT32_C(0); |
| 140 | logicals[i].lengthLBA = UINT32_C(0); |
| 141 | } // for |
| 142 | |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 143 | err = lseek64(fd, 0, SEEK_SET); |
| 144 | err = read(fd, &tempMBR, 512); |
| 145 | for (i = 0; i < 440; i++) |
| 146 | code[i] = tempMBR.code[i]; |
| 147 | diskSignature = tempMBR.diskSignature; |
| 148 | nulls = tempMBR.nulls; |
| 149 | for (i = 0; i < 4; i++) { |
| 150 | partitions[i].status = tempMBR.partitions[i].status; |
| 151 | partitions[i].partitionType = tempMBR.partitions[i].partitionType; |
| 152 | partitions[i].firstLBA = tempMBR.partitions[i].firstLBA; |
| 153 | partitions[i].lengthLBA = tempMBR.partitions[i].lengthLBA; |
| 154 | for (j = 0; j < 3; j++) { |
| 155 | partitions[i].firstSector[j] = tempMBR.partitions[i].firstSector[j]; |
| 156 | partitions[i].lastSector[j] = tempMBR.partitions[i].lastSector[j]; |
| 157 | } // for j... |
| 158 | } // for i... |
| 159 | MBRSignature = tempMBR.MBRSignature; |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 160 | |
| 161 | // Reverse the byte order, if necessary |
| 162 | if (IsLittleEndian() == 0) { |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 163 | ReverseBytes(&diskSignature, 4); |
| 164 | ReverseBytes(&nulls, 2); |
| 165 | ReverseBytes(&MBRSignature, 2); |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 166 | for (i = 0; i < 4; i++) { |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 167 | ReverseBytes(&partitions[i].firstLBA, 4); |
| 168 | ReverseBytes(&partitions[i].lengthLBA, 4); |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 169 | } // for |
| 170 | } // if |
| 171 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 172 | if (MBRSignature != MBR_SIGNATURE) { |
| 173 | allOK = 0; |
| 174 | state = invalid; |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 175 | } // if |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 176 | |
| 177 | // Find disk size |
| 178 | diskSize = disksize(fd, &err); |
| 179 | |
| 180 | // Find block size |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 181 | if (checkBlockSize) { |
| 182 | if ((blockSize = GetBlockSize(fd)) == -1) { |
| 183 | blockSize = SECTOR_SIZE; |
| 184 | printf("Unable to determine sector size; assuming %lu bytes!\n", |
| 185 | (unsigned long) SECTOR_SIZE); |
| 186 | } // if |
| 187 | } // if (checkBlockSize) |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 188 | |
| 189 | // Load logical partition data, if any is found.... |
| 190 | if (allOK) { |
| 191 | for (i = 0; i < 4; i++) { |
| 192 | if ((partitions[i].partitionType == 0x05) || (partitions[i].partitionType == 0x0f) |
| 193 | || (partitions[i].partitionType == 0x85)) { |
| 194 | // Found it, so call a recursive algorithm to load everything from them.... |
srs5694 | 86dd784 | 2009-08-26 14:39:40 -0400 | [diff] [blame] | 195 | maxLogicals = ReadLogicalPart(fd, partitions[i].firstLBA, UINT32_C(0), maxLogicals); |
| 196 | if ((maxLogicals < 0) || (maxLogicals > NUM_LOGICALS)) { |
| 197 | allOK = 0; |
| 198 | fprintf(stderr, "Error reading logical partitions! List may be truncated!\n"); |
| 199 | } // if maxLogicals valid |
| 200 | } // if primary partition is extended |
| 201 | } // for primary partition loop |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 202 | if (allOK) { // Loaded logicals OK |
| 203 | state = mbr; |
| 204 | } else { |
| 205 | state = invalid; |
| 206 | } // if |
| 207 | } // if |
| 208 | |
| 209 | /* Check to see if it's in GPT format.... */ |
| 210 | if (allOK) { |
| 211 | for (i = 0; i < 4; i++) { |
| 212 | if (partitions[i].partitionType == UINT8_C(0xEE)) { |
| 213 | state = gpt; |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 214 | } // if |
| 215 | } // for |
| 216 | } // if |
| 217 | |
| 218 | // If there's an EFI GPT partition, look for other partition types, |
| 219 | // to flag as hybrid |
| 220 | if (state == gpt) { |
| 221 | for (i = 0 ; i < 4; i++) { |
| 222 | if ((partitions[i].partitionType != UINT8_C(0xEE)) && |
| 223 | (partitions[i].partitionType != UINT8_C(0x00))) |
| 224 | state = hybrid; |
| 225 | } // for |
| 226 | } // if hybrid |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 227 | |
| 228 | /* // Tell the user what the MBR state is... |
| 229 | switch (state) { |
| 230 | case invalid: |
| 231 | printf("Information: MBR appears to be empty or invalid.\n"); |
| 232 | break; |
| 233 | case gpt: |
| 234 | printf("Information: MBR holds GPT placeholder partitions.\n"); |
| 235 | break; |
| 236 | case hybrid: |
| 237 | printf("Information: MBR holds hybrid GPT/MBR data.\n"); |
| 238 | break; |
| 239 | case mbr: |
| 240 | printf("Information: MBR data appears to be valid.\n"); |
| 241 | break; |
| 242 | } // switch */ |
| 243 | } // MBRData::ReadMBRData(int fd) |
| 244 | |
| 245 | // Write the MBR data to the default defined device. |
| 246 | int MBRData::WriteMBRData(void) { |
| 247 | int allOK = 1, fd; |
| 248 | |
| 249 | if ((fd = open(device, O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH)) != -1) { |
| 250 | WriteMBRData(fd); |
| 251 | } else { |
| 252 | allOK = 0; |
| 253 | } // if/else |
| 254 | close(fd); |
| 255 | return allOK; |
| 256 | } // MBRData::WriteMBRData(void) |
| 257 | |
| 258 | // Save the MBR data to a file. Note that this function writes ONLY the |
| 259 | // MBR data, not the logical partitions (if any are defined). |
| 260 | void MBRData::WriteMBRData(int fd) { |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 261 | int i, j; |
| 262 | TempMBR tempMBR; |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 263 | |
| 264 | // Reverse the byte order, if necessary |
| 265 | if (IsLittleEndian() == 0) { |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 266 | ReverseBytes(&diskSignature, 4); |
| 267 | ReverseBytes(&nulls, 2); |
| 268 | ReverseBytes(&MBRSignature, 2); |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 269 | for (i = 0; i < 4; i++) { |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 270 | ReverseBytes(&partitions[i].firstLBA, 4); |
| 271 | ReverseBytes(&partitions[i].lengthLBA, 4); |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 272 | } // for |
| 273 | } // if |
| 274 | |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 275 | // Copy MBR data to a 512-byte data structure for writing, to |
| 276 | // work around a FreeBSD limitation.... |
| 277 | for (i = 0; i < 440; i++) |
| 278 | tempMBR.code[i] = code[i]; |
| 279 | tempMBR.diskSignature = diskSignature; |
| 280 | tempMBR.nulls = nulls; |
| 281 | tempMBR.MBRSignature = MBRSignature; |
| 282 | for (i = 0; i < 4; i++) { |
| 283 | tempMBR.partitions[i].status = partitions[i].status; |
| 284 | tempMBR.partitions[i].partitionType = partitions[i].partitionType; |
| 285 | tempMBR.partitions[i].firstLBA = partitions[i].firstLBA; |
| 286 | tempMBR.partitions[i].lengthLBA = partitions[i].lengthLBA; |
| 287 | for (j = 0; j < 3; j++) { |
| 288 | tempMBR.partitions[i].firstSector[j] = partitions[i].firstSector[j]; |
| 289 | tempMBR.partitions[i].lastSector[j] = partitions[i].lastSector[j]; |
| 290 | } // for j... |
| 291 | } // for i... |
| 292 | |
| 293 | // Now write that data structure... |
| 294 | write(fd, &tempMBR, 512); |
| 295 | |
| 296 | /* write(fd, code, 440); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 297 | write(fd, &diskSignature, 4); |
| 298 | write(fd, &nulls, 2); |
| 299 | write(fd, partitions, 64); |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 300 | write(fd, &MBRSignature, 2); */ |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 301 | |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 302 | // Reverse the byte order back, if necessary |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 303 | if (IsLittleEndian() == 0) { |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 304 | ReverseBytes(&diskSignature, 4); |
| 305 | ReverseBytes(&nulls, 2); |
| 306 | ReverseBytes(&MBRSignature, 2); |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 307 | for (i = 0; i < 4; i++) { |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 308 | ReverseBytes(&partitions[i].firstLBA, 4); |
| 309 | ReverseBytes(&partitions[i].lengthLBA, 4); |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 310 | } // for |
| 311 | }// if |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 312 | } // MBRData::WriteMBRData(int fd) |
| 313 | |
| 314 | // This is a recursive function to read all the logical partitions, following the |
srs5694 | 86dd784 | 2009-08-26 14:39:40 -0400 | [diff] [blame] | 315 | // logical partition linked list from the disk and storing the basic data in the |
| 316 | // logicals[] array. Returns last index to logicals[] uses, or -1 if there was a |
| 317 | // problem |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 318 | int MBRData::ReadLogicalPart(int fd, uint32_t extendedStart, |
| 319 | uint32_t diskOffset, int partNum) { |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 320 | struct EBRRecord ebr; |
| 321 | off_t offset; |
| 322 | |
srs5694 | 86dd784 | 2009-08-26 14:39:40 -0400 | [diff] [blame] | 323 | if ((partNum < NUM_LOGICALS) && (partNum >= 0)) { |
| 324 | offset = (off_t) (extendedStart + diskOffset) * blockSize; |
| 325 | if (lseek64(fd, offset, SEEK_SET) == (off_t) -1) { // seek to EBR record |
| 326 | fprintf(stderr, "Unable to seek to %lu! Aborting!\n", (unsigned long) offset); |
| 327 | partNum = -1; |
| 328 | } |
| 329 | if (read(fd, &ebr, 512) != 512) { // Load the data.... |
| 330 | fprintf(stderr, "Error seeking to or reading logical partition data from %lu!\nAborting!\n", |
| 331 | (unsigned long) offset); |
| 332 | partNum = -1; |
| 333 | } else if (IsLittleEndian() != 1) { // Reverse byte ordering of some data.... |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 334 | ReverseBytes(&ebr.MBRSignature, 2); |
| 335 | ReverseBytes(&ebr.partitions[0].firstLBA, 4); |
| 336 | ReverseBytes(&ebr.partitions[0].lengthLBA, 4); |
| 337 | ReverseBytes(&ebr.partitions[1].firstLBA, 4); |
| 338 | ReverseBytes(&ebr.partitions[1].lengthLBA, 4); |
srs5694 | 86dd784 | 2009-08-26 14:39:40 -0400 | [diff] [blame] | 339 | } // if/else/if |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 340 | |
srs5694 | 86dd784 | 2009-08-26 14:39:40 -0400 | [diff] [blame] | 341 | if (ebr.MBRSignature != MBR_SIGNATURE) { |
| 342 | partNum = -1; |
| 343 | fprintf(stderr, "MBR signature in logical partition invalid; read 0x%04X, but should be 0x%04X\n", |
| 344 | (unsigned int) ebr.MBRSignature, (unsigned int) MBR_SIGNATURE); |
| 345 | } // if |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 346 | |
srs5694 | 86dd784 | 2009-08-26 14:39:40 -0400 | [diff] [blame] | 347 | // Copy over the basic data.... |
| 348 | logicals[partNum].status = ebr.partitions[0].status; |
| 349 | logicals[partNum].firstLBA = ebr.partitions[0].firstLBA + diskOffset + extendedStart; |
| 350 | logicals[partNum].lengthLBA = ebr.partitions[0].lengthLBA; |
| 351 | logicals[partNum].partitionType = ebr.partitions[0].partitionType; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 352 | |
srs5694 | 86dd784 | 2009-08-26 14:39:40 -0400 | [diff] [blame] | 353 | // Find the next partition (if there is one) and recurse.... |
| 354 | if ((ebr.partitions[1].firstLBA != UINT32_C(0)) && (partNum >= 0) && |
| 355 | (partNum < (NUM_LOGICALS - 1))) { |
| 356 | partNum = ReadLogicalPart(fd, extendedStart, ebr.partitions[1].firstLBA, |
| 357 | partNum + 1); |
| 358 | } else { |
| 359 | partNum++; |
| 360 | } // if another partition |
| 361 | } // Not enough space for all the logicals (or previous error encountered) |
| 362 | return (partNum); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 363 | } // MBRData::ReadLogicalPart() |
| 364 | |
| 365 | // Show the MBR data to the user.... |
| 366 | void MBRData::DisplayMBRData(void) { |
| 367 | int i; |
| 368 | char tempStr[255]; |
srs5694 | e19ba09 | 2009-08-24 14:10:35 -0400 | [diff] [blame] | 369 | char bootCode; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 370 | |
| 371 | printf("MBR disk identifier: 0x%08X\n", (unsigned int) diskSignature); |
| 372 | printf("MBR partitions:\n"); |
srs5694 | e19ba09 | 2009-08-24 14:10:35 -0400 | [diff] [blame] | 373 | printf("Number\t Boot\t Start (sector)\t Length (sectors)\tType\n"); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 374 | for (i = 0; i < 4; i++) { |
| 375 | if (partitions[i].lengthLBA != 0) { |
srs5694 | e19ba09 | 2009-08-24 14:10:35 -0400 | [diff] [blame] | 376 | if (partitions[i].status && 0x80) // it's bootable |
| 377 | bootCode = '*'; |
| 378 | else |
| 379 | bootCode = ' '; |
| 380 | printf("%4d\t %c\t%13lu\t%15lu \t0x%02X\n", i + 1, bootCode, |
| 381 | (unsigned long) partitions[i].firstLBA, |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 382 | (unsigned long) partitions[i].lengthLBA, partitions[i].partitionType); |
| 383 | } // if |
| 384 | } // for |
| 385 | |
| 386 | // Now display logical partition data.... |
| 387 | for (i = 0; i < NUM_LOGICALS; i++) { |
| 388 | if (logicals[i].lengthLBA != 0) { |
| 389 | printf("%4d\t%13lu\t%15lu \t0x%02X\n", i + 5, (unsigned long) logicals[i].firstLBA, |
| 390 | (unsigned long) logicals[i].lengthLBA, logicals[i].partitionType); |
| 391 | } // if |
| 392 | } // for |
| 393 | printf("\nDisk size is %lu sectors (%s)\n", (unsigned long) diskSize, |
| 394 | BytesToSI(diskSize * (uint64_t) blockSize, tempStr)); |
| 395 | } // MBRData::DisplayMBRData() |
| 396 | |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 397 | // Create a protective MBR. Clears the boot loader area if clearBoot > 0. |
| 398 | void MBRData::MakeProtectiveMBR(int clearBoot) { |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 399 | int i; |
| 400 | |
| 401 | // Initialize variables |
| 402 | nulls = 0; |
| 403 | MBRSignature = MBR_SIGNATURE; |
| 404 | |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 405 | if (clearBoot > 0) { |
| 406 | for (i = 0; i < 440; i++) |
| 407 | code[i] = (uint8_t) 0; |
| 408 | } // if |
| 409 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 410 | partitions[0].status = UINT8_C(0); // Flag the protective part. as unbootable |
| 411 | |
| 412 | // Write CHS data. This maxes out the use of the disk, as much as |
| 413 | // possible -- even to the point of exceeding the capacity of sub-8GB |
| 414 | // disks. The EFI spec says to use 0xffffff as the ending value, |
| 415 | // although normal MBR disks max out at 0xfeffff. FWIW, both GNU Parted |
| 416 | // and Apple's Disk Utility use 0xfeffff, and the latter puts that |
| 417 | // value in for the FIRST sector, too! |
| 418 | partitions[0].firstSector[0] = UINT8_C(0); |
| 419 | partitions[0].firstSector[1] = UINT8_C(1); |
| 420 | partitions[0].firstSector[2] = UINT8_C(0); |
| 421 | partitions[0].lastSector[0] = UINT8_C(255); |
| 422 | partitions[0].lastSector[1] = UINT8_C(255); |
| 423 | partitions[0].lastSector[2] = UINT8_C(255); |
| 424 | |
| 425 | partitions[0].partitionType = UINT8_C(0xEE); |
| 426 | partitions[0].firstLBA = UINT32_C(1); |
| 427 | if (diskSize < UINT32_MAX) { // If the disk is under 2TiB |
| 428 | partitions[0].lengthLBA = diskSize - 1; |
| 429 | } else { // disk is too big to represent, so fake it... |
| 430 | partitions[0].lengthLBA = UINT32_MAX; |
| 431 | } // if/else |
| 432 | |
| 433 | // Zero out three unused primary partitions... |
| 434 | for (i = 1; i < 4; i++) { |
| 435 | partitions[i].status = UINT8_C(0); |
| 436 | partitions[i].firstSector[0] = UINT8_C(0); |
| 437 | partitions[i].firstSector[1] = UINT8_C(0); |
| 438 | partitions[i].firstSector[2] = UINT8_C(0); |
| 439 | partitions[i].partitionType = UINT8_C(0); |
| 440 | partitions[i].lastSector[0] = UINT8_C(0); |
| 441 | partitions[i].lastSector[1] = UINT8_C(0); |
| 442 | partitions[i].lastSector[2] = UINT8_C(0); |
| 443 | partitions[i].firstLBA = UINT32_C(0); |
| 444 | partitions[i].lengthLBA = UINT32_C(0); |
| 445 | } // for |
| 446 | |
| 447 | // Zero out all the logical partitions. Not necessary for data |
| 448 | // integrity on write, but eliminates stray entries if user wants |
| 449 | // to view the MBR after converting the disk |
| 450 | for (i = 0; i < NUM_LOGICALS; i++) { |
| 451 | logicals[i].status = UINT8_C(0); |
| 452 | logicals[i].firstSector[0] = UINT8_C(0); |
| 453 | logicals[i].firstSector[1] = UINT8_C(0); |
| 454 | logicals[i].firstSector[2] = UINT8_C(0); |
| 455 | logicals[i].partitionType = UINT8_C(0); |
| 456 | logicals[i].lastSector[0] = UINT8_C(0); |
| 457 | logicals[i].lastSector[1] = UINT8_C(0); |
| 458 | logicals[i].lastSector[2] = UINT8_C(0); |
| 459 | logicals[i].firstLBA = UINT32_C(0); |
| 460 | logicals[i].lengthLBA = UINT32_C(0); |
| 461 | } // for |
| 462 | |
| 463 | state = gpt; |
| 464 | } // MBRData::MakeProtectiveMBR() |
| 465 | |
srs5694 | c0ca8f8 | 2009-08-20 21:35:25 -0400 | [diff] [blame] | 466 | // Create a partition that fills the most available space. Returns |
| 467 | // 1 if partition was created, 0 otherwise. Intended for use in |
| 468 | // creating hybrid MBRs. |
| 469 | int MBRData::MakeBiggestPart(int i, int type) { |
| 470 | uint32_t start = UINT32_C(1); // starting point for each search |
| 471 | uint32_t firstBlock; // first block in a segment |
| 472 | uint32_t lastBlock; // last block in a segment |
| 473 | uint32_t segmentSize; // size of segment in blocks |
| 474 | uint32_t selectedSegment = UINT32_C(0); // location of largest segment |
| 475 | uint32_t selectedSize = UINT32_C(0); // size of largest segment in blocks |
| 476 | int found = 0; |
| 477 | |
| 478 | do { |
| 479 | firstBlock = FindFirstAvailable(start); |
| 480 | if (firstBlock != UINT32_C(0)) { // something's free... |
| 481 | lastBlock = FindLastInFree(firstBlock); |
| 482 | segmentSize = lastBlock - firstBlock + UINT32_C(1); |
| 483 | if (segmentSize > selectedSize) { |
| 484 | selectedSize = segmentSize; |
| 485 | selectedSegment = firstBlock; |
| 486 | } // if |
| 487 | start = lastBlock + 1; |
| 488 | } // if |
| 489 | } while (firstBlock != 0); |
| 490 | if ((selectedSize > UINT32_C(0)) && ((uint64_t) selectedSize < diskSize)) { |
| 491 | found = 1; |
| 492 | MakePart(i, selectedSegment, selectedSize, type, 0); |
| 493 | } else { |
| 494 | found = 0; |
| 495 | } // if/else |
| 496 | return found; |
| 497 | } // MBRData::MakeBiggestPart(int i) |
| 498 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 499 | // Return a pointer to a primary or logical partition, or NULL if |
| 500 | // the partition is out of range.... |
| 501 | struct MBRRecord* MBRData::GetPartition(int i) { |
| 502 | MBRRecord* thePart = NULL; |
| 503 | |
| 504 | if ((i >= 0) && (i < 4)) { // primary partition |
| 505 | thePart = &partitions[i]; |
| 506 | } // if |
| 507 | if ((i >= 4) && (i < (NUM_LOGICALS + 4))) { |
| 508 | thePart = &logicals[i - 4]; |
| 509 | } // if |
| 510 | return thePart; |
| 511 | } // GetPartition() |
| 512 | |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 513 | // Displays the state, as a word, on stdout. Used for debugging & to |
| 514 | // tell the user about the MBR state when the program launches.... |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 515 | void MBRData::ShowState(void) { |
| 516 | switch (state) { |
| 517 | case invalid: |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 518 | printf(" MBR: not present\n"); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 519 | break; |
| 520 | case gpt: |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 521 | printf(" MBR: protective\n"); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 522 | break; |
| 523 | case hybrid: |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 524 | printf(" MBR: hybrid\n"); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 525 | break; |
| 526 | case mbr: |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 527 | printf(" MBR: MBR only\n"); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 528 | break; |
| 529 | default: |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 530 | printf("\a MBR: unknown -- bug!\n"); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 531 | break; |
| 532 | } // switch |
| 533 | } // MBRData::ShowState() |
| 534 | |
| 535 | // Create a primary partition of the specified number, starting LBA, |
| 536 | // and length. This function does *NO* error checking, so it's possible |
| 537 | // to seriously screw up a partition table using this function! It's |
| 538 | // intended as a way to create a hybrid MBR, which is a pretty funky |
| 539 | // setup to begin with.... |
| 540 | void MBRData::MakePart(int num, uint32_t start, uint32_t length, int type, |
| 541 | int bootable) { |
| 542 | |
| 543 | partitions[num].status = (uint8_t) bootable * (uint8_t) 0x80; |
| 544 | partitions[num].firstSector[0] = UINT8_C(0); |
| 545 | partitions[num].firstSector[1] = UINT8_C(0); |
| 546 | partitions[num].firstSector[2] = UINT8_C(0); |
| 547 | partitions[num].partitionType = (uint8_t) type; |
| 548 | partitions[num].lastSector[0] = UINT8_C(0); |
| 549 | partitions[num].lastSector[1] = UINT8_C(0); |
| 550 | partitions[num].lastSector[2] = UINT8_C(0); |
| 551 | partitions[num].firstLBA = start; |
| 552 | partitions[num].lengthLBA = length; |
| 553 | } // MakePart() |
| 554 | |
srs5694 | c0ca8f8 | 2009-08-20 21:35:25 -0400 | [diff] [blame] | 555 | // Finds the first free space on the disk from start onward; returns 0 |
| 556 | // if none available.... |
| 557 | uint32_t MBRData::FindFirstAvailable(uint32_t start) { |
| 558 | uint32_t first; |
| 559 | uint32_t i; |
| 560 | int firstMoved = 0; |
| 561 | |
| 562 | first = start; |
| 563 | |
| 564 | // ...now search through all partitions; if first is within an |
| 565 | // existing partition, move it to the next sector after that |
| 566 | // partition and repeat. If first was moved, set firstMoved |
| 567 | // flag; repeat until firstMoved is not set, so as to catch |
| 568 | // cases where partitions are out of sequential order.... |
| 569 | do { |
| 570 | firstMoved = 0; |
| 571 | for (i = 0; i < 4; i++) { |
| 572 | // Check if it's in the existing partition |
| 573 | if ((first >= partitions[i].firstLBA) && |
| 574 | (first < (partitions[i].firstLBA + partitions[i].lengthLBA))) { |
| 575 | first = partitions[i].firstLBA + partitions[i].lengthLBA; |
| 576 | firstMoved = 1; |
| 577 | } // if |
| 578 | } // for |
| 579 | } while (firstMoved == 1); |
| 580 | if (first >= diskSize) |
| 581 | first = 0; |
| 582 | return (first); |
| 583 | } // MBRData::FindFirstAvailable() |
| 584 | |
| 585 | uint32_t MBRData::FindLastInFree(uint32_t start) { |
| 586 | uint32_t nearestStart; |
| 587 | uint32_t i; |
| 588 | |
| 589 | if (diskSize <= UINT32_MAX) |
| 590 | nearestStart = diskSize - 1; |
| 591 | else |
| 592 | nearestStart = UINT32_MAX - 1; |
| 593 | for (i = 0; i < 4; i++) { |
| 594 | if ((nearestStart > partitions[i].firstLBA) && |
| 595 | (partitions[i].firstLBA > start)) { |
| 596 | nearestStart = partitions[i].firstLBA - 1; |
| 597 | } // if |
| 598 | } // for |
| 599 | return (nearestStart); |
| 600 | } // MBRData::FindLastInFree |
| 601 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 602 | uint8_t MBRData::GetStatus(int i) { |
| 603 | MBRRecord* thePart; |
| 604 | uint8_t retval; |
| 605 | |
| 606 | thePart = GetPartition(i); |
| 607 | if (thePart != NULL) |
| 608 | retval = thePart->status; |
| 609 | else |
| 610 | retval = UINT8_C(0); |
| 611 | return retval; |
| 612 | } // MBRData::GetStatus() |
| 613 | |
| 614 | uint8_t MBRData::GetType(int i) { |
| 615 | MBRRecord* thePart; |
| 616 | uint8_t retval; |
| 617 | |
| 618 | thePart = GetPartition(i); |
| 619 | if (thePart != NULL) |
| 620 | retval = thePart->partitionType; |
| 621 | else |
| 622 | retval = UINT8_C(0); |
| 623 | return retval; |
| 624 | } // MBRData::GetType() |
| 625 | |
| 626 | uint32_t MBRData::GetFirstSector(int i) { |
| 627 | MBRRecord* thePart; |
| 628 | uint32_t retval; |
| 629 | |
| 630 | thePart = GetPartition(i); |
| 631 | if (thePart != NULL) { |
| 632 | retval = thePart->firstLBA; |
| 633 | } else |
| 634 | retval = UINT32_C(0); |
| 635 | return retval; |
| 636 | } // MBRData::GetFirstSector() |
| 637 | |
| 638 | uint32_t MBRData::GetLength(int i) { |
| 639 | MBRRecord* thePart; |
| 640 | uint32_t retval; |
| 641 | |
| 642 | thePart = GetPartition(i); |
| 643 | if (thePart != NULL) { |
| 644 | retval = thePart->lengthLBA; |
| 645 | } else |
| 646 | retval = UINT32_C(0); |
| 647 | return retval; |
| 648 | } // MBRData::GetLength() |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame^] | 649 | |
| 650 | // Return the MBR data as a GPT partition.... |
| 651 | GPTPart MBRData::AsGPT(int i) { |
| 652 | MBRRecord* origPart; |
| 653 | GPTPart newPart; |
| 654 | uint8_t origType; |
| 655 | uint64_t firstSector, lastSector; |
| 656 | char tempStr[NAME_SIZE]; |
| 657 | |
| 658 | newPart.BlankPartition(); |
| 659 | origPart = GetPartition(i); |
| 660 | if (origPart != NULL) { |
| 661 | origType = origPart->partitionType; |
| 662 | |
| 663 | // don't convert extended, hybrid protective, or null (non-existent) |
| 664 | // partitions (Note similar protection is in GPTData::XFormPartitions(), |
| 665 | // but I want it here too in case I call this function in another |
| 666 | // context in the future....) |
| 667 | if ((origType != 0x05) && (origType != 0x0f) && (origType != 0x85) && |
| 668 | (origType != 0x00) && (origType != 0xEE)) { |
| 669 | firstSector = (uint64_t) origPart->firstLBA; |
| 670 | newPart.SetFirstLBA(firstSector); |
| 671 | lastSector = firstSector + (uint64_t) origPart->lengthLBA; |
| 672 | if (lastSector > 0) lastSector--; |
| 673 | newPart.SetLastLBA(lastSector); |
| 674 | newPart.SetType(((uint16_t) origType) * 0x0100); |
| 675 | newPart.SetUniqueGUID(1); |
| 676 | newPart.SetAttributes(0); |
| 677 | newPart.SetName((unsigned char*) newPart.GetNameType(tempStr)); |
| 678 | } // if |
| 679 | } // if |
| 680 | return newPart; |
| 681 | } // MBRData::AsGPT() |