1 /*
2     Implementation of GPTData class derivative with popt-based command
3     line processing
4     Copyright (C) 2010-2014 Roderick W. Smith
5 
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20 
21 #include <string.h>
22 #include <string>
23 #include <iostream>
24 #include <sstream>
25 #include <errno.h>
26 #include "gptcl.h"
27 
28 GPTDataCL::GPTDataCL(void) {
29    attributeOperation = backupFile = partName = hybrids = newPartInfo = NULL;
30    mbrParts = twoParts = outDevice = typeCode = partGUID = diskGUID = NULL;
31    alignment = DEFAULT_ALIGNMENT;
32    deletePartNum = infoPartNum = largestPartNum = bsdPartNum = 0;
33    tableSize = GPT_SIZE;
34 } // GPTDataCL constructor
35 
36 GPTDataCL::GPTDataCL(string filename) {
37 } // GPTDataCL constructor with filename
38 
39 GPTDataCL::~GPTDataCL(void) {
40 } // GPTDataCL destructor
41 
42 void GPTDataCL::LoadBackupFile(string backupFile, int &saveData, int &neverSaveData) {
43    if (LoadGPTBackup(backupFile) == 1) {
44       JustLooking(0);
45       saveData = 1;
46    } else {
47       saveData = 0;
48       neverSaveData = 1;
49       cerr << "Error loading backup file!\n";
50    } // else
51 } // GPTDataCL::LoadBackupFile()
52 
53 // Perform the actions specified on the command line. This is necessarily one
54 // monster of a function!
55 // Returns values:
56 // 0 = success
57 // 1 = too few arguments
58 // 2 = error when reading partition table
59 // 3 = non-GPT disk and no -g option
60 // 4 = unable to save changes
61 // 8 = disk replication operation (-R) failed
62 int GPTDataCL::DoOptions(int argc, char* argv[]) {
63    GPTData secondDevice;
64    int opt, numOptions = 0, saveData = 0, neverSaveData = 0;
65    int partNum = 0, newPartNum = -1, saveNonGPT = 1, retval = 0, pretend = 0;
66    uint64_t low, high, startSector, endSector, sSize, mainTableLBA;
67    uint64_t temp; // temporary variable; free to use in any case
68    char *device;
69    string cmd, typeGUID, name;
70    PartType typeHelper;
71 
72    struct poptOption theOptions[] =
73    {
74       {"attributes", 'A', POPT_ARG_STRING, &attributeOperation, 'A', "operate on partition attributes",
75           "list|[partnum:show|or|nand|xor|=|set|clear|toggle|get[:bitnum|hexbitmask]]"},
76       {"set-alignment", 'a', POPT_ARG_INT, &alignment, 'a', "set sector alignment", "value"},
77       {"backup", 'b', POPT_ARG_STRING, &backupFile, 'b', "backup GPT to file", "file"},
78       {"change-name", 'c', POPT_ARG_STRING, &partName, 'c', "change partition's name", "partnum:name"},
79       {"recompute-chs", 'C', POPT_ARG_NONE, NULL, 'C', "recompute CHS values in protective/hybrid MBR", ""},
80       {"delete", 'd', POPT_ARG_INT, &deletePartNum, 'd', "delete a partition", "partnum"},
81       {"display-alignment", 'D', POPT_ARG_NONE, NULL, 'D', "show number of sectors per allocation block", ""},
82       {"move-second-header", 'e', POPT_ARG_NONE, NULL, 'e', "move second header to end of disk", ""},
83       {"end-of-largest", 'E', POPT_ARG_NONE, NULL, 'E', "show end of largest free block", ""},
84       {"first-in-largest", 'f', POPT_ARG_NONE, NULL, 'f', "show start of the largest free block", ""},
85       {"first-aligned-in-largest", 'F', POPT_ARG_NONE, NULL, 'F', "show start of the largest free block, aligned", ""},
86       {"mbrtogpt", 'g', POPT_ARG_NONE, NULL, 'g', "convert MBR to GPT", ""},
87       {"randomize-guids", 'G', POPT_ARG_NONE, NULL, 'G', "randomize disk and partition GUIDs", ""},
88       {"hybrid", 'h', POPT_ARG_STRING, &hybrids, 'h', "create hybrid MBR", "partnum[:partnum...][:EE]"},
89       {"info", 'i', POPT_ARG_INT, &infoPartNum, 'i', "show detailed information on partition", "partnum"},
90       {"move-main-table", 'j', POPT_ARG_INT, &mainTableLBA, 'j', "adjust the location of the main partition table", "sector"},
91       {"load-backup", 'l', POPT_ARG_STRING, &backupFile, 'l', "load GPT backup from file", "file"},
92       {"list-types", 'L', POPT_ARG_NONE, NULL, 'L', "list known partition types", ""},
93       {"gpttombr", 'm', POPT_ARG_STRING, &mbrParts, 'm', "convert GPT to MBR", "partnum[:partnum...]"},
94       {"new", 'n', POPT_ARG_STRING, &newPartInfo, 'n', "create new partition", "partnum:start:end"},
95       {"largest-new", 'N', POPT_ARG_INT, &largestPartNum, 'N', "create largest possible new partition", "partnum"},
96       {"clear", 'o', POPT_ARG_NONE, NULL, 'o', "clear partition table", ""},
97       {"print-mbr", 'O', POPT_ARG_NONE, NULL, 'O', "print MBR partition table", ""},
98       {"print", 'p', POPT_ARG_NONE, NULL, 'p', "print partition table", ""},
99       {"pretend", 'P', POPT_ARG_NONE, NULL, 'P', "make changes in memory, but don't write them", ""},
100       {"transpose", 'r', POPT_ARG_STRING, &twoParts, 'r', "transpose two partitions", "partnum:partnum"},
101       {"replicate", 'R', POPT_ARG_STRING, &outDevice, 'R', "replicate partition table", "device_filename"},
102       {"sort", 's', POPT_ARG_NONE, NULL, 's', "sort partition table entries", ""},
103       {"resize-table", 'S', POPT_ARG_INT, &tableSize, 'S', "resize partition table", "numparts"},
104       {"typecode", 't', POPT_ARG_STRING, &typeCode, 't', "change partition type code", "partnum:{hexcode|GUID}"},
105       {"transform-bsd", 'T', POPT_ARG_INT, &bsdPartNum, 'T', "transform BSD disklabel partition to GPT", "partnum"},
106       {"partition-guid", 'u', POPT_ARG_STRING, &partGUID, 'u', "set partition GUID", "partnum:guid"},
107       {"disk-guid", 'U', POPT_ARG_STRING, &diskGUID, 'U', "set disk GUID", "guid"},
108       {"verify", 'v', POPT_ARG_NONE, NULL, 'v', "check partition table integrity", ""},
109       {"version", 'V', POPT_ARG_NONE, NULL, 'V', "display version information", ""},
110       {"zap", 'z', POPT_ARG_NONE, NULL, 'z', "zap (destroy) GPT (but not MBR) data structures", ""},
111       {"zap-all", 'Z', POPT_ARG_NONE, NULL, 'Z', "zap (destroy) GPT and MBR data structures", ""},
112       POPT_AUTOHELP { NULL, 0, 0, NULL, 0, NULL, NULL }
113    };
114 
115    // Create popt context...
116    poptCon = poptGetContext(NULL, argc, (const char**) argv, theOptions, 0);
117 
118    poptSetOtherOptionHelp(poptCon, " [OPTION...] <device>");
119 
120    if (argc < 2) {
121       poptPrintUsage(poptCon, stderr, 0);
122       return 1;
123    }
124 
125    // Do one loop through the options to find the device filename and deal
126    // with options that don't require a device filename, to flag destructive
127    // (o, z, or Z) options, and to flag presence of a --pretend/-P option
128    while ((opt = poptGetNextOpt(poptCon)) > 0) {
129       switch (opt) {
130          case 'A':
131             cmd = GetString(attributeOperation, 1);
132             if (cmd == "list")
133                Attributes::ListAttributes();
134             break;
135          case 'L':
136             typeHelper.ShowAllTypes(0);
137             break;
138          case 'P':
139             pretend = 1;
140             break;
141          case 'V':
142             cout << "GPT fdisk (sgdisk) version " << GPTFDISK_VERSION << "\n\n";
143             break;
144          default:
145             break;
146       } // switch
147       numOptions++;
148    } // while
149 
150    // Assume first non-option argument is the device filename....
151    device = (char*) poptGetArg(poptCon);
152    poptResetContext(poptCon);
153 
154    if (device != NULL) {
155       JustLooking(); // reset as necessary
156       BeQuiet(); // Tell called functions to be less verbose & interactive
157       if (LoadPartitions((string) device)) {
158          if ((WhichWasUsed() == use_mbr) || (WhichWasUsed() == use_bsd))
159             saveNonGPT = 0; // flag so we don't overwrite unless directed to do so
160          sSize = GetBlockSize();
161          while ((opt = poptGetNextOpt(poptCon)) > 0) {
162             switch (opt) {
163                case 'A': {
164                   if (cmd != "list") {
165                      partNum = (int) GetInt(attributeOperation, 1) - 1;
166                      if (partNum < 0)
167                         partNum = newPartNum;
168                      if ((partNum >= 0) && (partNum < (int) GetNumParts())) {
169                         switch (ManageAttributes(partNum, GetString(attributeOperation, 2),
170                            GetString(attributeOperation, 3))) {
171                            case -1:
172                               saveData = 0;
173                               neverSaveData = 1;
174                               break;
175                            case 1:
176                               JustLooking(0);
177                               saveData = 1;
178                               break;
179                            default:
180                               break;
181                         } // switch
182                      } else {
183                         cerr << "Error: Invalid partition number " << partNum + 1 << "\n";
184                         saveData = 0;
185                         neverSaveData = 1;
186                      } // if/else reasonable partition #
187                   } // if (cmd != "list")
188                   break;
189                } // case 'A':
190                case 'a':
191                   SetAlignment(alignment);
192                   break;
193                case 'b':
194                   SaveGPTBackup(backupFile);
195                   free(backupFile);
196                   break;
197                case 'c':
198                   cout << "Setting name!\n";
199                   JustLooking(0);
200                   partNum = (int) GetInt(partName, 1) - 1;
201                   if (partNum < 0)
202                      partNum = newPartNum;
203                   cout << "partNum is " << partNum << "\n";
204                   if ((partNum >= 0) && (partNum < (int) GetNumParts())) {
205                      name = GetString(partName, 2);
206                      if (SetName(partNum, (UnicodeString) name.c_str())) {
207                         saveData = 1;
208                      } else {
209                         cerr << "Unable to set partition " << partNum + 1
210                              << "'s name to '" << GetString(partName, 2) << "'!\n";
211                         neverSaveData = 1;
212                      } // if/else
213                      free(partName);
214                   }
215                   break;
216                case 'C':
217                   JustLooking(0);
218                   RecomputeCHS();
219                   saveData = 1;
220                   break;
221                case 'd':
222                   JustLooking(0);
223                   if (DeletePartition(deletePartNum - 1) == 0) {
224                      cerr << "Error " << errno << " deleting partition!\n";
225                      neverSaveData = 1;
226                   } else saveData = 1;
227                                                       break;
228                case 'D':
229                   cout << GetAlignment() << "\n";
230                   break;
231                case 'e':
232                   JustLooking(0);
233                   MoveSecondHeaderToEnd();
234                   saveData = 1;
235                   break;
236                case 'E':
237                   cout << FindLastInFree(FindFirstInLargest()) << "\n";
238                   break;
239                case 'f':
240                   cout << FindFirstInLargest() << "\n";
241                   break;
242                case 'F':
243                   temp = FindFirstInLargest();
244                   Align(&temp);
245                   cout << temp << "\n";
246                   break;
247                case 'g':
248                   JustLooking(0);
249                   saveData = 1;
250                   saveNonGPT = 1;
251                   break;
252                case 'G':
253                   JustLooking(0);
254                   saveData = 1;
255                   RandomizeGUIDs();
256                   break;
257                case 'h':
258                   JustLooking(0);
259                   if (BuildMBR(hybrids, 1) == 1)
260                      saveData = 1;
261                   break;
262                case 'i':
263                   ShowPartDetails(infoPartNum - 1);
264                   break;
265                case 'j':
266                    if (MoveMainTable(mainTableLBA)) {
267                        JustLooking(0);
268                        saveData = 1;
269                    } else {
270                        neverSaveData = 1;
271                    } // if/else
272                    break;
273                case 'l':
274                   LoadBackupFile(backupFile, saveData, neverSaveData);
275                   free(backupFile);
276                   break;
277                case 'L':
278                   break;
279                case 'm':
280                   JustLooking(0);
281                   if (BuildMBR(mbrParts, 0) == 1) {
282                      if (!pretend) {
283                         if (SaveMBR()) {
284                            DestroyGPT();
285                         } else
286                            cerr << "Problem saving MBR!\n";
287                      } // if
288                      saveNonGPT = 0;
289                      pretend = 1; // Not really, but works around problem if -g is used with this...
290                      saveData = 0;
291                   } // if
292                   break;
293                case 'n':
294                   JustLooking(0);
295                   newPartNum = (int) GetInt(newPartInfo, 1) - 1;
296                   if (newPartNum < 0)
297                      newPartNum = FindFirstFreePart();
298                   low = FindFirstInLargest();
299                   Align(&low);
300                   high = FindLastInFree(low);
301                   startSector = IeeeToInt(GetString(newPartInfo, 2), sSize, low, high, low);
302                   endSector = IeeeToInt(GetString(newPartInfo, 3), sSize, startSector, high, high);
303                   if (CreatePartition(newPartNum, startSector, endSector)) {
304                      saveData = 1;
305                   } else {
306                      cerr << "Could not create partition " << newPartNum + 1 << " from "
307                           << startSector << " to " << endSector << "\n";
308                      neverSaveData = 1;
309                   } // if/else
310                   free(newPartInfo);
311                   break;
312                case 'N':
313                   JustLooking(0);
314                   startSector = FindFirstInLargest();
315                   Align(&startSector);
316                   endSector = FindLastInFree(startSector);
317                   if (largestPartNum <= 0)
318                      largestPartNum = FindFirstFreePart() + 1;
319                   if (CreatePartition(largestPartNum - 1, startSector, endSector)) {
320                      saveData = 1;
321                   } else {
322                      cerr << "Could not create partition " << largestPartNum << " from "
323                      << startSector << " to " << endSector << "\n";
324                      neverSaveData = 1;
325                   } // if/else
326                   break;
327                case 'o':
328                   JustLooking(0);
329                   ClearGPTData();
330                   saveData = 1;
331                   break;
332                case 'O':
333                    DisplayMBRData();
334                    break;
335                case 'p':
336                   DisplayGPTData();
337                   break;
338                case 'P':
339                   pretend = 1;
340                   break;
341                case 'r':
342                   JustLooking(0);
343                   uint64_t p1, p2;
344                   p1 = GetInt(twoParts, 1) - 1;
345                   p2 = GetInt(twoParts, 2) - 1;
346                   if (SwapPartitions((uint32_t) p1, (uint32_t) p2) == 0) {
347                      neverSaveData = 1;
348                      cerr << "Cannot swap partitions " << p1 + 1 << " and " << p2 + 1 << "\n";
349                   } else saveData = 1;
350                                                       break;
351                case 'R':
352                   secondDevice = *this;
353                   secondDevice.SetDisk(outDevice);
354                   secondDevice.JustLooking(0);
355                   if (!secondDevice.SaveGPTData(1))
356                      retval = 8;
357                   break;
358                case 's':
359                   JustLooking(0);
360                   SortGPT();
361                   saveData = 1;
362                   break;
363                case 'S':
364                   JustLooking(0);
365                   if (SetGPTSize(tableSize) == 0)
366                      neverSaveData = 1;
367                   else
368                      saveData = 1;
369                   break;
370                case 't':
371                   JustLooking(0);
372                   partNum = (int) GetInt(typeCode, 1) - 1;
373                   if (partNum < 0)
374                      partNum = newPartNum;
375                   if ((partNum >= 0) && (partNum < (int) GetNumParts())) {
376                      // Remember the original hex value requested
377                      string raw = GetString(typeCode, 2);
378                      if (raw.size() == 4) {
379                         typeRaw[partNum] = StrToHex(raw, 0);
380                      }
381                      typeHelper = GetString(typeCode, 2);
382                      if ((typeHelper != (GUIDData) "00000000-0000-0000-0000-000000000000") &&
383                          (ChangePartType(partNum, typeHelper))) {
384                         saveData = 1;
385                         } else {
386                            cerr << "Could not change partition " << partNum + 1
387                            << "'s type code to " << GetString(typeCode, 2) << "!\n";
388                            neverSaveData = 1;
389                         } // if/else
390                      free(typeCode);
391                   }
392                   break;
393                case 'T':
394                   JustLooking(0);
395                   XFormDisklabel(bsdPartNum - 1);
396                   saveData = 1;
397                   break;
398                case 'u':
399                   JustLooking(0);
400                   saveData = 1;
401                   partNum = (int) GetInt(partGUID, 1) - 1;
402                   if (partNum < 0)
403                      partNum = newPartNum;
404                   if ((partNum >= 0) && (partNum < (int) GetNumParts())) {
405                      SetPartitionGUID(partNum, GetString(partGUID, 2).c_str());
406                   }
407                   break;
408                case 'U':
409                   JustLooking(0);
410                   saveData = 1;
411                   SetDiskGUID(diskGUID);
412                   break;
413                case 'v':
414                   Verify();
415                   break;
416                case 'z':
417                   if (!pretend) {
418                      DestroyGPT();
419                   } // if
420                   saveNonGPT = 1;
421                   saveData = 0;
422                   break;
423                case 'Z':
424                   if (!pretend) {
425                      DestroyGPT();
426                      DestroyMBR();
427                   } // if
428                   saveNonGPT = 1;
429                   saveData = 0;
430                   break;
431                default:
432                   cerr << "Unknown option (-" << opt << ")!\n";
433                   break;
434                } // switch
435          } // while
436       } else { // if loaded OK
437          poptResetContext(poptCon);
438          // Do a few types of operations even if there are problems....
439          while ((opt = poptGetNextOpt(poptCon)) > 0) {
440             switch (opt) {
441                case 'l':
442                   LoadBackupFile(backupFile, saveData, neverSaveData);
443                   cout << "Information: Loading backup partition table; will override earlier problems!\n";
444                   free(backupFile);
445                   retval = 0;
446                   break;
447                case 'o':
448                   JustLooking(0);
449                   ClearGPTData();
450                   saveData = 1;
451                   cout << "Information: Creating fresh partition table; will override earlier problems!\n";
452                   retval = 0;
453                   break;
454                case 'v':
455                   cout << "Verification may miss some problems or report too many!\n";
456                   Verify();
457                   break;
458                case 'z':
459                   if (!pretend) {
460                      DestroyGPT();
461                   } // if
462                   saveNonGPT = 1;
463                   saveData = 0;
464                   break;
465                case 'Z':
466                   if (!pretend) {
467                      DestroyGPT();
468                      DestroyMBR();
469                   } // if
470                   saveNonGPT = 1;
471                   saveData = 0;
472                   break;
473             } // switch
474          } // while
475          retval = 2;
476       } // if/else loaded OK
477       if ((saveData) && (!neverSaveData) && (saveNonGPT) && (!pretend)) {
478          if (!SaveGPTData(1))
479             retval = 4;
480       }
481       if (saveData && (!saveNonGPT)) {
482          cout << "Non-GPT disk; not saving changes. Use -g to override.\n";
483          retval = 3;
484       } // if
485       if (neverSaveData) {
486          cerr << "Error encountered; not saving changes.\n";
487          retval = 4;
488       } // if
489    } // if (device != NULL)
490    poptFreeContext(poptCon);
491    return retval;
492 } // GPTDataCL::DoOptions()
493 
494 // Create a hybrid or regular MBR from GPT data structures
495 int GPTDataCL::BuildMBR(char* argument, int isHybrid) {
496    int numParts, allOK = 1, i, origPartNum;
497    int eeLast, mbrNum = 0;
498    MBRPart newPart;
499    BasicMBRData newMBR;
500 
501    if (argument != NULL) {
502       numParts = CountColons(argument) + 1;
503       if (isHybrid) {
504          eeLast = GetString(argument, numParts) == "EE";
505          if (eeLast) {
506             numParts--;
507          }
508       }
509 
510       if (numParts <= (4 - isHybrid)) {
511          newMBR.SetDisk(GetDisk());
512          for (i = 0; i < numParts; i++) {
513             origPartNum = GetInt(argument, i + 1) - 1;
514             if (IsUsedPartNum(origPartNum) && (partitions[origPartNum].IsSizedForMBR() == MBR_SIZED_GOOD)) {
515                mbrNum = i + (isHybrid && ! eeLast);
516                newPart.SetInclusion(PRIMARY);
517                newPart.SetLocation(operator[](origPartNum).GetFirstLBA(),
518                                    operator[](origPartNum).GetLengthLBA());
519                newPart.SetStatus(0);
520                newPart.SetType((uint8_t)(operator[](origPartNum).GetHexType() / 0x0100));
521                // If we were created with a specific hex type, use that instead
522                // of risking fidelity loss by doing a GUID-based lookup
523                if (typeRaw.count(origPartNum) == 1) {
524                   newPart.SetType(typeRaw[origPartNum]);
525                }
526                newMBR.AddPart(mbrNum, newPart);
527             } else {
528                cerr << "Original partition " << origPartNum + 1 << " does not exist or is too big! Aborting operation!\n";
529                allOK = 0;
530             } // if/else
531          } // for
532          if (isHybrid) {
533             if (eeLast) {
534                mbrNum = i;
535             } else {
536                mbrNum = 0;
537             }
538             newPart.SetInclusion(PRIMARY);
539             newPart.SetLocation(1, newMBR.FindLastInFree(1));
540             newPart.SetStatus(0);
541             newPart.SetType(0xEE);
542             newMBR.AddPart(mbrNum, newPart);
543          } // if
544          if (allOK)
545             SetProtectiveMBR(newMBR);
546       } else allOK = 0;
547    } else allOK = 0;
548    if (!allOK)
549       cerr << "Problem creating MBR!\n";
550    return allOK;
551 } // GPTDataCL::BuildMBR()
552 
553 // Returns the number of colons in argument string, ignoring the
554 // first character (thus, a leading colon is ignored, as GetString()
555 // does).
556 int CountColons(char* argument) {
557    int num = 0;
558 
559    while ((argument[0] != '\0') && (argument = strchr(&argument[1], ':')))
560       num++;
561 
562    return num;
563 } // GPTDataCL::CountColons()
564 
565 // Extract integer data from argument string, which should be colon-delimited
566 uint64_t GetInt(const string & argument, int itemNum) {
567    uint64_t retval;
568 
569    istringstream inString(GetString(argument, itemNum));
570    inString >> retval;
571    return retval;
572 } // GPTDataCL::GetInt()
573 
574 // Extract string data from argument string, which should be colon-delimited
575 // If string begins with a colon, that colon is skipped in the counting. If an
576 // invalid itemNum is specified, returns an empty string.
577 string GetString(string argument, int itemNum) {
578    size_t startPos = 0, endPos = 0;
579    string retVal = "";
580    int foundLast = 0;
581    int numFound = 0;
582 
583    if (argument[0] == ':')
584       argument.erase(0, 1);
585    while ((numFound < itemNum) && (!foundLast)) {
586       endPos = argument.find(':', startPos);
587       numFound++;
588       if (endPos == string::npos) {
589          foundLast = 1;
590          endPos = argument.length();
591       } else if (numFound < itemNum) {
592          startPos = endPos + 1;
593       } // if/elseif
594    } // while
595    if ((numFound == itemNum) && (numFound > 0))
596       retVal = argument.substr(startPos, endPos - startPos);
597 
598    return retVal;
599 } // GetString()
600