2 // Program to fix certain types of damaged Master Boot Record (MBR) partition
5 // Copyright 2011 by Roderick W. Smith
7 // This program is distributed under the terms of the GNU GPL, as described
8 // in the COPYING file.
10 // Based on C++ classes originally created for GPT fdisk (gdisk and sgdisk)
23 void DoMBR(BasicMBRData & mbrTable);
25 int main(int argc, char* argv[]) {
26 BasicMBRData mbrTable;
29 cout << "FixParts " << GPTFDISK_VERSION << "\n";
33 cout << "Type device filename, or press <Enter> to exit: ";
34 device = ReadString();
35 if (device.length() == 0)
42 cerr << "Usage: " << argv[0] << " device_filename\n";
46 cout << "\nLoading MBR data from " << device << "\n";
47 if (!mbrTable.ReadMBRData(device)) {
48 cerr << "\nUnable to read MBR data from '" << device << "'! Exiting!\n\n";
52 // This switch() statement weeds out disks with GPT signatures and non-MBR
53 // disks so we don't accidentally damage them....
54 switch(mbrTable.GetValidity()) {
55 case hybrid: case gpt:
56 cerr << "\nThis disk appears to be a GPT disk. Use GNU Parted or GPT fdisk on it!\n";
57 cerr << "Exiting!\n\n";
61 cerr << "\nCannot find valid MBR data on '" << device << "'! Exiting!\n\n";
68 cerr << "\nCannot determine the validity of the disk on '" << device
76 // Do the bulk of the processing on actual MBR disks. First checks for old
77 // GPT data (note this is different from the earlier check; this one only
78 // looks for the GPT signatures in the main and backup GPT area, not for
79 // a protective partition in the MBR, which we know is NOT present, since
80 // if it were, this function would NOT be called!) and offers to destroy
81 // it, if found; then makes sure the partitions are in a consistent and
82 // legal state; then presents the MBR menu and, if it returns a "1" value
83 // (meaning the user opted to write changes), writes the table to disk.
84 void DoMBR(BasicMBRData & mbrTable) {
87 if (mbrTable.CheckForGPT() > 0) {
88 cout << "\nNOTICE: GPT signatures detected on the disk, but no 0xEE protective "
89 << "partition!\nThe GPT signatures are probably left over from a previous "
90 << "partition table.\nDo you want to delete them (if you answer 'Y', this "
91 << "will happen\nimmediately)? ";
93 cout << "Erasing GPT data!\n";
94 if (mbrTable.BlankGPTData() != 1)
95 cerr << "GPT signature erasure failed!\n";
99 mbrTable.MakeItLegal();
102 if (mbrTable.DoMenu() > 0) {
103 cout << "\nFinal checks complete. About to write MBR data. THIS WILL OVERWRITE "
104 << "EXISTING\nPARTITIONS!!\n\nDo you want to proceed? ";
105 if (GetYN() == 'Y') {
106 mbrTable.WriteMBRData();