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