srs5694 | 886ac6c | 2011-03-13 11:50:59 -0400 | [diff] [blame^] | 1 | // fixparts |
| 2 | // Program to fix certain types of damaged Master Boot Record (MBR) partition |
| 3 | // tables |
| 4 | // |
| 5 | // Copyright 2011 by Roderick W. Smith |
| 6 | // |
| 7 | // This program is distributed under the terms of the GNU GPL, as described |
| 8 | // in the COPYING file. |
| 9 | // |
| 10 | // Based on C++ classes originally created for GPT fdisk (gdisk and sgdisk) |
| 11 | // programs |
| 12 | |
| 13 | #include <stdio.h> |
| 14 | #include <string.h> |
| 15 | #include <string> |
| 16 | #include <iostream> |
| 17 | #include <sstream> |
| 18 | #include "basicmbr.h" |
| 19 | #include "support.h" |
| 20 | |
| 21 | using namespace std; |
| 22 | |
| 23 | int main(int argc, char* argv[]) { |
| 24 | BasicMBRData mbrTable; |
| 25 | char *device = NULL; |
| 26 | int i, doItAgain; |
| 27 | |
| 28 | switch (argc) { |
| 29 | case 1: |
| 30 | cout << "Type device filename, or press <Enter> to exit: "; |
| 31 | device = new char[255]; |
| 32 | ReadCString(device, 255); |
| 33 | i = strlen(device); |
| 34 | if (i && device[i - 1] == '\n') |
| 35 | device[i - 1] = '\0'; |
| 36 | break; |
| 37 | case 2: |
| 38 | device = new char[strlen(argv[1]) + 1]; |
| 39 | strcpy(device, argv[1]); |
| 40 | break; |
| 41 | default: |
| 42 | cerr << "Usage: " << argv[0] << " device_filename\n"; |
| 43 | exit(1); |
| 44 | } // switch |
| 45 | |
| 46 | cout << "FixParts " << GPTFDISK_VERSION << "\n"; |
| 47 | cout << "\nLoading MBR data from " << device << "\n"; |
| 48 | mbrTable.ReadMBRData(device); |
| 49 | if (mbrTable.CheckForGPT() > 0) { |
| 50 | if ((mbrTable.GetValidity() == hybrid) || (mbrTable.GetValidity() == gpt)) { |
| 51 | cerr << "\nThis disk appears to be a GPT disk. Use GNU Parted or GPT fdisk on it!\n"; |
| 52 | cerr << "Exiting!\n\n"; |
| 53 | exit(1); |
| 54 | } else { |
| 55 | cout << "\nNOTICE: GPT signatures detected on the disk, but no 0xEE protective " |
| 56 | << "partition!\nThe GPT signatures are probably left over from a previous " |
| 57 | << "partition table.\nDo you want to delete them (if you answer 'Y', this " |
| 58 | << "will happen\nimmediately)? "; |
| 59 | if (GetYN() == 'Y') { |
| 60 | cout << "Erasing GPT data!\n"; |
| 61 | if (mbrTable.BlankGPTData() != 1) |
| 62 | cerr << "GPT signature erasure failed!\n"; |
| 63 | } // if |
| 64 | } // if/else |
| 65 | } // if |
| 66 | mbrTable.MakeItLegal(); |
| 67 | do { |
| 68 | doItAgain = 0; |
| 69 | if (mbrTable.DoMenu() > 0) { |
| 70 | cout << "\nFinal checks complete. About to write MBR data. THIS WILL OVERWRITE EXISTING\n" |
| 71 | << "PARTITIONS!!\n\nDo you want to proceed? "; |
| 72 | if (GetYN() == 'Y') { |
| 73 | mbrTable.WriteMBRData(); |
| 74 | mbrTable.DiskSync(); |
| 75 | doItAgain = 0; |
| 76 | } else { |
| 77 | doItAgain = 1; |
| 78 | } // else |
| 79 | } // if |
| 80 | } while (doItAgain); |
| 81 | delete[] device; |
| 82 | return 0; |
| 83 | } // main() |