blob: 6b9429b9f4b2c3a5f064c3e9413b187d06c1a7dd [file] [log] [blame]
srs56941e093722010-01-05 00:14:19 -05001// sgdisk.cc
srs569473ba4792010-01-12 18:18:17 -05002// Command-line-based version of gdisk. This program is named after sfdisk,
3// and it can serve a similar role (easily scripted, etc.), but it's used
4// strictly via command-line arguments, and it doesn't bear much resemblance
5// to sfdisk in actual use.
srs56941e093722010-01-05 00:14:19 -05006//
srs569473ba4792010-01-12 18:18:17 -05007// by Rod Smith, project began February 2009; sgdisk begun January 2010.
srs56941e093722010-01-05 00:14:19 -05008
srs569464cbd172011-03-01 22:03:54 -05009/* This program is copyright (c) 2009-2011 by Roderick W. Smith. It is distributed
srs56941e093722010-01-05 00:14:19 -050010 under the terms of the GNU GPL version 2, as detailed in the COPYING file. */
11
srs56941e093722010-01-05 00:14:19 -050012#include <stdio.h>
srs569473ba4792010-01-12 18:18:17 -050013#include <popt.h>
14#include <errno.h>
srs5694df9d3632011-01-08 18:33:24 -050015#include <stdint.h>
srs56949a46b042011-03-15 00:34:10 -040016#include <string.h>
srs5694df9d3632011-01-08 18:33:24 -050017#include <string>
srs5694fed16d02010-01-27 23:03:40 -050018#include <iostream>
srs569455d92612010-03-07 22:16:07 -050019#include <sstream>
srs56941e093722010-01-05 00:14:19 -050020#include "mbr.h"
21#include "gpt.h"
22#include "support.h"
srs569473ba4792010-01-12 18:18:17 -050023#include "parttypes.h"
srs56949ddc14b2010-08-22 22:44:42 -040024#include "attributes.h"
srs569473ba4792010-01-12 18:18:17 -050025
26using namespace std;
srs56941e093722010-01-05 00:14:19 -050027
28#define MAX_OPTIONS 50
29
srs56945a081752010-09-24 20:39:41 -040030int BuildMBR(GPTData& theGPT, char* argument, int isHybrid);
srs569408bb0da2010-02-19 17:19:55 -050031int CountColons(char* argument);
srs56941e093722010-01-05 00:14:19 -050032
srs56945a608532011-03-17 13:53:01 -040033// Extract colon-separated fields from a string....
34uint64_t GetInt(const string & argument, int itemNum);
35string GetString(string argument, int itemNum);
36
37
srs569473ba4792010-01-12 18:18:17 -050038int main(int argc, char *argv[]) {
srs569464cbd172011-03-01 22:03:54 -050039 GPTData theGPT, secondDevice;
srs5694df9d3632011-01-08 18:33:24 -050040 uint32_t sSize;
41 uint64_t low, high;
srs5694e321d442010-01-29 17:44:04 -050042 int opt, numOptions = 0, saveData = 0, neverSaveData = 0;
srs56949ba54212010-05-18 23:24:02 -040043 int partNum = 0, deletePartNum = 0, infoPartNum = 0, bsdPartNum = 0, largestPartNum = 0;
44 int saveNonGPT = 1;
45 uint32_t gptPartNum = 0;
srs5694a8582cf2010-03-19 14:21:59 -040046 int alignment = DEFAULT_ALIGNMENT, retval = 0, pretend = 0;
srs569473ba4792010-01-12 18:18:17 -050047 uint32_t tableSize = 128;
48 uint64_t startSector, endSector;
srs56945a081752010-09-24 20:39:41 -040049 uint64_t temp; // temporary variable; free to use in any case
srs56949ddc14b2010-08-22 22:44:42 -040050 char *attributeOperation = NULL;
srs5694815fb652011-03-18 12:35:56 -040051 char *device;
srs569408bb0da2010-02-19 17:19:55 -050052 char *newPartInfo = NULL, *typeCode = NULL, *partName = NULL;
53 char *backupFile = NULL, *twoParts = NULL, *hybrids = NULL, *mbrParts;
srs5694f9312b02010-07-06 15:39:51 -040054 char *partGUID = NULL, *diskGUID = NULL, *outDevice = NULL;
srs5694699941e2011-03-21 21:33:57 -040055 string cmd, typeGUID, name;
srs56946699b012010-02-04 00:55:30 -050056 PartType typeHelper;
srs56941e093722010-01-05 00:14:19 -050057
srs569473ba4792010-01-12 18:18:17 -050058 poptContext poptCon;
59 struct poptOption theOptions[] =
60 {
srs56949ddc14b2010-08-22 22:44:42 -040061 {"attributes", 'A', POPT_ARG_STRING, &attributeOperation, 'A', "operate on partition attributes", "list|[partnum:show|or|nand|xor|=|set|clear|toggle|get[:bitnum|hexbitmask]]"},
srs569473ba4792010-01-12 18:18:17 -050062 {"set-alignment", 'a', POPT_ARG_INT, &alignment, 'a', "set sector alignment", "value"},
63 {"backup", 'b', POPT_ARG_STRING, &backupFile, 'b', "backup GPT to file", "file"},
64 {"change-name", 'c', POPT_ARG_STRING, &partName, 'c', "change partition's name", "partnum:name"},
srs56949ba54212010-05-18 23:24:02 -040065 {"recompute-chs", 'C', POPT_ARG_NONE, NULL, 'C', "recompute CHS values in protective/hybrid MBR", ""},
srs569473ba4792010-01-12 18:18:17 -050066 {"delete", 'd', POPT_ARG_INT, &deletePartNum, 'd', "delete a partition", "partnum"},
srs569455d92612010-03-07 22:16:07 -050067 {"display-alignment", 'D', POPT_ARG_NONE, NULL, 'D', "show number of sectors per allocation block", ""},
srs569473ba4792010-01-12 18:18:17 -050068 {"move-second-header", 'e', POPT_ARG_NONE, NULL, 'e', "move second header to end of disk", ""},
69 {"end-of-largest", 'E', POPT_ARG_NONE, NULL, 'E', "show end of largest free block", ""},
70 {"first-in-largest", 'f', POPT_ARG_NONE, NULL, 'f', "show start of the largest free block", ""},
srs56945a081752010-09-24 20:39:41 -040071 {"first-aligned-in-largest", 'F', POPT_ARG_NONE, NULL, 'F', "show start of the largest free block, aligned", ""},
srs569473ba4792010-01-12 18:18:17 -050072 {"mbrtogpt", 'g', POPT_ARG_NONE, NULL, 'g', "convert MBR to GPT", ""},
srs56949ba54212010-05-18 23:24:02 -040073 {"randomize-guids", 'G', POPT_ARG_NONE, NULL, 'G', "randomize disk and partition GUIDs", ""},
srs569408bb0da2010-02-19 17:19:55 -050074 {"hybrid", 'h', POPT_ARG_STRING, &hybrids, 'h', "create hybrid MBR", "partnum[:partnum...]"},
srs569473ba4792010-01-12 18:18:17 -050075 {"info", 'i', POPT_ARG_INT, &infoPartNum, 'i', "show detailed information on partition", "partnum"},
76 {"load-backup", 'l', POPT_ARG_STRING, &backupFile, 'l', "load GPT backup from file", "file"},
77 {"list-types", 'L', POPT_ARG_NONE, NULL, 'L', "list known partition types", ""},
srs569408bb0da2010-02-19 17:19:55 -050078 {"gpttombr", 'm', POPT_ARG_STRING, &mbrParts, 'm', "convert GPT to MBR", "partnum[:partnum...]"},
srs569473ba4792010-01-12 18:18:17 -050079 {"new", 'n', POPT_ARG_STRING, &newPartInfo, 'n', "create new partition", "partnum:start:end"},
srs56949ba54212010-05-18 23:24:02 -040080 {"largest-new", 'N', POPT_ARG_INT, &largestPartNum, 'N', "create largest possible new partition", "partnum"},
srs569473ba4792010-01-12 18:18:17 -050081 {"clear", 'o', POPT_ARG_NONE, NULL, 'o', "clear partition table", ""},
82 {"print", 'p', POPT_ARG_NONE, NULL, 'p', "print partition table", ""},
83 {"pretend", 'P', POPT_ARG_NONE, NULL, 'P', "make changes in memory, but don't write them", ""},
srs569408bb0da2010-02-19 17:19:55 -050084 {"transpose", 'r', POPT_ARG_STRING, &twoParts, 'r', "transpose two partitions", "partnum:partnum"},
srs5694f9312b02010-07-06 15:39:51 -040085 {"replicate", 'R', POPT_ARG_STRING, &outDevice, 'R', "replicate partition table", "device_filename"},
srs569473ba4792010-01-12 18:18:17 -050086 {"sort", 's', POPT_ARG_NONE, NULL, 's', "sort partition table entries", ""},
87 {"resize-table", 'S', POPT_ARG_INT, &tableSize, 'S', "resize partition table", "numparts"},
srs5694327129e2010-09-22 01:07:31 -040088 {"typecode", 't', POPT_ARG_STRING, &typeCode, 't', "change partition type code", "partnum:{hexcode|GUID}"},
srs569473ba4792010-01-12 18:18:17 -050089 {"transform-bsd", 'T', POPT_ARG_INT, &bsdPartNum, 'T', "transform BSD disklabel partition to GPT", "partnum"},
srs56949ba54212010-05-18 23:24:02 -040090 {"partition-guid", 'u', POPT_ARG_STRING, &partGUID, 'u', "set partition GUID", "partnum:guid"},
91 {"disk-guid", 'U', POPT_ARG_STRING, &diskGUID, 'U', "set disk GUID", "guid"},
srs569473ba4792010-01-12 18:18:17 -050092 {"verify", 'v', POPT_ARG_NONE, NULL, 'v', "check partition table integrity", ""},
93 {"version", 'V', POPT_ARG_NONE, NULL, 'V', "display version information", ""},
srs569408bb0da2010-02-19 17:19:55 -050094 {"zap", 'z', POPT_ARG_NONE, NULL, 'z', "zap (destroy) GPT (but not MBR) data structures", ""},
95 {"zap-all", 'Z', POPT_ARG_NONE, NULL, 'Z', "zap (destroy) GPT and MBR data structures", ""},
srs569473ba4792010-01-12 18:18:17 -050096 POPT_AUTOHELP { NULL, 0, 0, NULL, 0 }
97 };
98
99 // Create popt context...
100 poptCon = poptGetContext(NULL, argc, (const char**) argv, theOptions, 0);
101
102 poptSetOtherOptionHelp(poptCon, " [OPTION...] <device>");
103
104 if (argc < 2) {
105 poptPrintUsage(poptCon, stderr, 0);
106 exit(1);
107 }
108
109 // Do one loop through the options to find the device filename and deal
110 // with options that don't require a device filename....
111 while ((opt = poptGetNextOpt(poptCon)) > 0) {
112 switch (opt) {
srs56949ddc14b2010-08-22 22:44:42 -0400113 case 'A':
114 cmd = GetString(attributeOperation, 1);
115 if (cmd == "list")
116 Attributes::ListAttributes();
117 break;
srs569473ba4792010-01-12 18:18:17 -0500118 case 'L':
srs56946699b012010-02-04 00:55:30 -0500119 typeHelper.ShowAllTypes();
srs56941e093722010-01-05 00:14:19 -0500120 break;
srs569473ba4792010-01-12 18:18:17 -0500121 case 'P':
122 pretend = 1;
srs56941e093722010-01-05 00:14:19 -0500123 break;
srs569473ba4792010-01-12 18:18:17 -0500124 case 'V':
srs5694fed16d02010-01-27 23:03:40 -0500125 cout << "GPT fdisk (sgdisk) version " << GPTFDISK_VERSION << "\n\n";
srs56941e093722010-01-05 00:14:19 -0500126 break;
127 default:
srs56941e093722010-01-05 00:14:19 -0500128 break;
129 } // switch
srs569473ba4792010-01-12 18:18:17 -0500130 numOptions++;
131 } // while
srs56941e093722010-01-05 00:14:19 -0500132
srs569473ba4792010-01-12 18:18:17 -0500133 // Assume first non-option argument is the device filename....
134 device = (char*) poptGetArg(poptCon);
135 poptResetContext(poptCon);
srs56941e093722010-01-05 00:14:19 -0500136
srs569473ba4792010-01-12 18:18:17 -0500137 if (device != NULL) {
138 theGPT.JustLooking(); // reset as necessary
139 theGPT.BeQuiet(); // Tell called functions to be less verbose & interactive
srs5694fed16d02010-01-27 23:03:40 -0500140 if (theGPT.LoadPartitions((string) device)) {
srs569473ba4792010-01-12 18:18:17 -0500141 if ((theGPT.WhichWasUsed() == use_mbr) || (theGPT.WhichWasUsed() == use_bsd))
142 saveNonGPT = 0; // flag so we don't overwrite unless directed to do so
srs5694df9d3632011-01-08 18:33:24 -0500143 sSize = theGPT.GetBlockSize();
srs569473ba4792010-01-12 18:18:17 -0500144 while ((opt = poptGetNextOpt(poptCon)) > 0) {
145 switch (opt) {
srs56949ddc14b2010-08-22 22:44:42 -0400146 case 'A': {
147 if (cmd != "list") {
148 partNum = (int) GetInt(attributeOperation, 1) - 1;
srs56940873e9d2010-10-07 13:00:45 -0400149 if ((partNum >= 0) && (partNum < (int) theGPT.GetNumParts())) {
150 switch (theGPT.ManageAttributes(partNum, GetString(attributeOperation, 2),
151 GetString(attributeOperation, 3))) {
152 case -1:
153 saveData = 0;
154 neverSaveData = 1;
155 break;
156 case 1:
157 theGPT.JustLooking(0);
158 saveData = 1;
159 break;
160 default:
161 break;
162 } // switch
163 } else {
164 cerr << "Error: Invalid partition number " << partNum + 1 << "\n";
165 saveData = 0;
166 neverSaveData = 1;
167 } // if/else reasonable partition #
168 } // if (cmd != "list")
srs56949ddc14b2010-08-22 22:44:42 -0400169 break;
170 } // case 'A':
srs569473ba4792010-01-12 18:18:17 -0500171 case 'a':
172 theGPT.SetAlignment(alignment);
173 break;
174 case 'b':
175 theGPT.SaveGPTBackup(backupFile);
176 free(backupFile);
177 break;
178 case 'c':
179 theGPT.JustLooking(0);
180 partNum = (int) GetInt(partName, 1) - 1;
srs5694699941e2011-03-21 21:33:57 -0400181 name = GetString(partName, 2);
182 if (theGPT.SetName(partNum, (UnicodeString) name.c_str())) {
srs569473ba4792010-01-12 18:18:17 -0500183 saveData = 1;
184 } else {
srs56940283dae2010-04-28 16:44:34 -0400185 cerr << "Unable to set partition " << partNum + 1
srs5694fed16d02010-01-27 23:03:40 -0500186 << "'s name to '" << GetString(partName, 2) << "'!\n";
srs569473ba4792010-01-12 18:18:17 -0500187 neverSaveData = 1;
188 } // if/else
189 free(partName);
190 break;
srs5694f9312b02010-07-06 15:39:51 -0400191 case 'C':
192 theGPT.JustLooking(0);
193 theGPT.RecomputeCHS();
194 saveData = 1;
195 break;
srs569473ba4792010-01-12 18:18:17 -0500196 case 'd':
197 theGPT.JustLooking(0);
198 if (theGPT.DeletePartition(deletePartNum - 1) == 0) {
srs5694fed16d02010-01-27 23:03:40 -0500199 cerr << "Error " << errno << " deleting partition!\n";
srs569473ba4792010-01-12 18:18:17 -0500200 neverSaveData = 1;
201 } else saveData = 1;
202 break;
srs569455d92612010-03-07 22:16:07 -0500203 case 'D':
srs56948a4ddfc2010-03-21 19:05:49 -0400204 cout << theGPT.GetAlignment() << "\n";
srs569455d92612010-03-07 22:16:07 -0500205 break;
srs569473ba4792010-01-12 18:18:17 -0500206 case 'e':
207 theGPT.JustLooking(0);
208 theGPT.MoveSecondHeaderToEnd();
209 saveData = 1;
210 break;
211 case 'E':
srs5694cb76c672010-02-11 22:22:22 -0500212 cout << theGPT.FindLastInFree(theGPT.FindFirstInLargest()) << "\n";
srs569473ba4792010-01-12 18:18:17 -0500213 break;
214 case 'f':
srs5694fed16d02010-01-27 23:03:40 -0500215 cout << theGPT.FindFirstInLargest() << "\n";
srs569473ba4792010-01-12 18:18:17 -0500216 break;
srs56945a081752010-09-24 20:39:41 -0400217 case 'F':
218 temp = theGPT.FindFirstInLargest();
219 theGPT.Align(&temp);
220 cout << temp << "\n";
221 break;
srs569473ba4792010-01-12 18:18:17 -0500222 case 'g':
223 theGPT.JustLooking(0);
224 saveData = 1;
225 saveNonGPT = 1;
226 break;
srs56949ba54212010-05-18 23:24:02 -0400227 case 'G':
228 theGPT.JustLooking(0);
229 saveData = 1;
230 theGPT.RandomizeGUIDs();
231 break;
srs569408bb0da2010-02-19 17:19:55 -0500232 case 'h':
233 theGPT.JustLooking(0);
srs56945a081752010-09-24 20:39:41 -0400234 if (BuildMBR(theGPT, hybrids, 1) == 1)
srs569408bb0da2010-02-19 17:19:55 -0500235 saveData = 1;
236 break;
srs569473ba4792010-01-12 18:18:17 -0500237 case 'i':
238 theGPT.ShowPartDetails(infoPartNum - 1);
239 break;
240 case 'l':
srs569464cbd172011-03-01 22:03:54 -0500241 if (theGPT.LoadGPTBackup((string) backupFile) == 1) {
242 theGPT.JustLooking(0);
srs569473ba4792010-01-12 18:18:17 -0500243 saveData = 1;
srs569464cbd172011-03-01 22:03:54 -0500244 } else {
srs569473ba4792010-01-12 18:18:17 -0500245 saveData = 0;
246 neverSaveData = 1;
srs5694fed16d02010-01-27 23:03:40 -0500247 cerr << "Error loading backup file!\n";
srs569473ba4792010-01-12 18:18:17 -0500248 } // else
249 free(backupFile);
250 break;
251 case 'L':
252 break;
srs569408bb0da2010-02-19 17:19:55 -0500253 case 'm':
254 theGPT.JustLooking(0);
srs56945a081752010-09-24 20:39:41 -0400255 if (BuildMBR(theGPT, mbrParts, 0) == 1) {
srs569408bb0da2010-02-19 17:19:55 -0500256 if (!pretend) {
srs5694bf8950c2011-03-12 01:23:12 -0500257 if (theGPT.SaveMBR()) {
srs569408bb0da2010-02-19 17:19:55 -0500258 theGPT.DestroyGPT();
srs5694bf8950c2011-03-12 01:23:12 -0500259 } else
srs569408bb0da2010-02-19 17:19:55 -0500260 cerr << "Problem saving MBR!\n";
261 } // if
262 saveNonGPT = 0;
srs5694bf8950c2011-03-12 01:23:12 -0500263 pretend = 1; // Not really, but works around problem if -g is used with this...
srs569408bb0da2010-02-19 17:19:55 -0500264 saveData = 0;
265 } // if
266 break;
srs569473ba4792010-01-12 18:18:17 -0500267 case 'n':
268 theGPT.JustLooking(0);
269 partNum = (int) GetInt(newPartInfo, 1) - 1;
srs5694df9d3632011-01-08 18:33:24 -0500270 if (partNum < 0)
271 partNum = theGPT.FindFirstFreePart();
272 low = theGPT.FindFirstInLargest();
273 high = theGPT.FindLastInFree(low);
srs569401f7f082011-03-15 23:53:31 -0400274 startSector = IeeeToInt(GetString(newPartInfo, 2), sSize, low, high, low);
275 endSector = IeeeToInt(GetString(newPartInfo, 3), sSize, startSector, high, high);
srs569473ba4792010-01-12 18:18:17 -0500276 if (theGPT.CreatePartition(partNum, startSector, endSector)) {
277 saveData = 1;
278 } else {
srs5694df9d3632011-01-08 18:33:24 -0500279 cerr << "Could not create partition " << partNum + 1 << " from "
srs5694fed16d02010-01-27 23:03:40 -0500280 << startSector << " to " << endSector << "\n";
srs569473ba4792010-01-12 18:18:17 -0500281 neverSaveData = 1;
282 } // if/else
283 free(newPartInfo);
284 break;
srs56949ba54212010-05-18 23:24:02 -0400285 case 'N':
286 theGPT.JustLooking(0);
287 startSector = theGPT.FindFirstInLargest();
288 endSector = theGPT.FindLastInFree(startSector);
srs5694df9d3632011-01-08 18:33:24 -0500289 if (largestPartNum < 0)
290 largestPartNum = theGPT.FindFirstFreePart();
srs56949ba54212010-05-18 23:24:02 -0400291 if (theGPT.CreatePartition(largestPartNum - 1, startSector, endSector)) {
292 saveData = 1;
293 } else {
294 cerr << "Could not create partition " << largestPartNum << " from "
295 << startSector << " to " << endSector << "\n";
296 neverSaveData = 1;
297 } // if/else
298 break;
srs569473ba4792010-01-12 18:18:17 -0500299 case 'o':
300 theGPT.JustLooking(0);
301 theGPT.ClearGPTData();
302 saveData = 1;
303 break;
304 case 'p':
305 theGPT.DisplayGPTData();
306 break;
307 case 'P':
308 pretend = 1;
309 break;
srs569408bb0da2010-02-19 17:19:55 -0500310 case 'r':
311 theGPT.JustLooking(0);
312 uint64_t p1, p2;
313 p1 = GetInt(twoParts, 1) - 1;
314 p2 = GetInt(twoParts, 2) - 1;
315 if (theGPT.SwapPartitions((uint32_t) p1, (uint32_t) p2) == 0) {
316 neverSaveData = 1;
317 cerr << "Cannot swap partitions " << p1 + 1 << " and " << p2 + 1 << "\n";
318 } else saveData = 1;
319 break;
srs56949ba54212010-05-18 23:24:02 -0400320 case 'R':
srs569464cbd172011-03-01 22:03:54 -0500321 secondDevice = theGPT;
srs5694bf8950c2011-03-12 01:23:12 -0500322 secondDevice.SetDisk(outDevice);
srs569464cbd172011-03-01 22:03:54 -0500323 secondDevice.JustLooking(0);
srs569464cbd172011-03-01 22:03:54 -0500324 secondDevice.SaveGPTData(1);
srs56949ba54212010-05-18 23:24:02 -0400325 break;
srs569473ba4792010-01-12 18:18:17 -0500326 case 's':
327 theGPT.JustLooking(0);
328 theGPT.SortGPT();
329 saveData = 1;
330 break;
331 case 'S':
332 theGPT.JustLooking(0);
333 if (theGPT.SetGPTSize(tableSize) == 0)
334 neverSaveData = 1;
335 else
336 saveData = 1;
337 break;
338 case 't':
339 theGPT.JustLooking(0);
340 partNum = (int) GetInt(typeCode, 1) - 1;
srs569482f3f0b2010-09-22 10:50:24 -0400341 typeHelper = GetString(typeCode, 2);
342 if ((typeHelper != (GUIDData) "00000000-0000-0000-0000-000000000000") &&
343 (theGPT.ChangePartType(partNum, typeHelper))) {
srs569473ba4792010-01-12 18:18:17 -0500344 saveData = 1;
345 } else {
srs5694fed16d02010-01-27 23:03:40 -0500346 cerr << "Could not change partition " << partNum + 1
srs5694327129e2010-09-22 01:07:31 -0400347 << "'s type code to " << GetString(typeCode, 2) << "!\n";
srs569473ba4792010-01-12 18:18:17 -0500348 neverSaveData = 1;
349 } // if/else
350 free(typeCode);
351 break;
352 case 'T':
353 theGPT.JustLooking(0);
srs569408bb0da2010-02-19 17:19:55 -0500354 theGPT.XFormDisklabel(bsdPartNum - 1);
srs569473ba4792010-01-12 18:18:17 -0500355 saveData = 1;
356 break;
srs56949ba54212010-05-18 23:24:02 -0400357 case 'u':
358 theGPT.JustLooking(0);
359 saveData = 1;
360 gptPartNum = (int) GetInt(partGUID, 1) - 1;
361 theGPT.SetPartitionGUID(gptPartNum, GetString(partGUID, 2).c_str());
362 break;
363 case 'U':
364 theGPT.JustLooking(0);
365 saveData = 1;
366 theGPT.SetDiskGUID(diskGUID);
367 break;
srs569473ba4792010-01-12 18:18:17 -0500368 case 'v':
369 theGPT.Verify();
370 break;
371 case 'z':
srs569408bb0da2010-02-19 17:19:55 -0500372 if (!pretend) {
373 theGPT.DestroyGPT();
374 } // if
srs569473ba4792010-01-12 18:18:17 -0500375 saveNonGPT = 0;
srs569408bb0da2010-02-19 17:19:55 -0500376 saveData = 0;
377 break;
378 case 'Z':
379 if (!pretend) {
380 theGPT.DestroyGPT();
381 theGPT.DestroyMBR();
382 } // if
383 saveNonGPT = 0;
384 saveData = 0;
srs569473ba4792010-01-12 18:18:17 -0500385 break;
386 default:
srs5694fed16d02010-01-27 23:03:40 -0500387 cerr << "Unknown option (-" << opt << ")!\n";
srs569473ba4792010-01-12 18:18:17 -0500388 break;
389 } // switch
390 } // while
srs5694bf8950c2011-03-12 01:23:12 -0500391 if ((saveData) && (!neverSaveData) && (saveNonGPT) && (!pretend)) {
srs569473ba4792010-01-12 18:18:17 -0500392 theGPT.SaveGPTData(1);
srs5694bf8950c2011-03-12 01:23:12 -0500393 }
srs569473ba4792010-01-12 18:18:17 -0500394 if (saveData && (!saveNonGPT)) {
srs5694fed16d02010-01-27 23:03:40 -0500395 cout << "Non-GPT disk; not saving changes. Use -g to override.\n";
srs569473ba4792010-01-12 18:18:17 -0500396 retval = 3;
397 } // if
398 if (neverSaveData) {
srs5694fed16d02010-01-27 23:03:40 -0500399 cerr << "Error encountered; not saving changes.\n";
srs569473ba4792010-01-12 18:18:17 -0500400 retval = 4;
401 } // if
402 } else { // if loaded OK
srs56949ddc14b2010-08-22 22:44:42 -0400403 poptResetContext(poptCon);
404 // Do a few types of operations even if there are problems....
405 while ((opt = poptGetNextOpt(poptCon)) > 0) {
406 switch (opt) {
407 case 'v':
srs5694327129e2010-09-22 01:07:31 -0400408 cout << "Verification may miss some problems or report too many!\n";
srs56949ddc14b2010-08-22 22:44:42 -0400409 theGPT.Verify();
410 break;
411 case 'z':
412 if (!pretend) {
413 theGPT.DestroyGPT();
414 } // if
415 saveNonGPT = 0;
416 saveData = 0;
417 break;
418 case 'Z':
419 if (!pretend) {
420 theGPT.DestroyGPT();
421 theGPT.DestroyMBR();
422 } // if
423 saveNonGPT = 0;
424 saveData = 0;
425 break;
426 } // switch
427 } // while
srs569473ba4792010-01-12 18:18:17 -0500428 retval = 2;
429 } // if/else loaded OK
430 } // if (device != NULL)
431 poptFreeContext(poptCon);
srs56941e093722010-01-05 00:14:19 -0500432
srs569473ba4792010-01-12 18:18:17 -0500433 return retval;
434} // main
srs56941e093722010-01-05 00:14:19 -0500435
srs569408bb0da2010-02-19 17:19:55 -0500436// Create a hybrid or regular MBR from GPT data structures
srs56945a081752010-09-24 20:39:41 -0400437int BuildMBR(GPTData & theGPT, char* argument, int isHybrid) {
srs5694bf8950c2011-03-12 01:23:12 -0500438 int numParts, allOK = 1, i, origPartNum;
srs5694bf8950c2011-03-12 01:23:12 -0500439 MBRPart newPart;
440 BasicMBRData newMBR;
srs569408bb0da2010-02-19 17:19:55 -0500441
srs56945a081752010-09-24 20:39:41 -0400442 if ((&theGPT != NULL) && (argument != NULL)) {
srs569408bb0da2010-02-19 17:19:55 -0500443 numParts = CountColons(argument) + 1;
444 if (numParts <= (4 - isHybrid)) {
srs5694bf8950c2011-03-12 01:23:12 -0500445 newMBR.SetDisk(theGPT.GetDisk());
srs569408bb0da2010-02-19 17:19:55 -0500446 for (i = 0; i < numParts; i++) {
srs5694bf8950c2011-03-12 01:23:12 -0500447 origPartNum = GetInt(argument, i + 1) - 1;
srs5694815fb652011-03-18 12:35:56 -0400448 if (theGPT.IsUsedPartNum(origPartNum)) {
449 newPart.SetInclusion(PRIMARY);
450 newPart.SetLocation(theGPT[origPartNum].GetFirstLBA(),
451 theGPT[origPartNum].GetLengthLBA());
452 newPart.SetStatus(0);
453 newPart.SetType((uint8_t)(theGPT[origPartNum].GetHexType() / 0x0100));
454 newMBR.AddPart(i + isHybrid, newPart);
455 } else {
456 cerr << "Partition " << origPartNum << " does not exist! Aborting operation!\n";
457 allOK = 0;
458 } // if/else
srs569408bb0da2010-02-19 17:19:55 -0500459 } // for
srs569455d92612010-03-07 22:16:07 -0500460 if (isHybrid) {
srs5694bf8950c2011-03-12 01:23:12 -0500461 newPart.SetInclusion(PRIMARY);
462 newPart.SetLocation(1, newMBR.FindLastInFree(1));
463 newPart.SetStatus(0);
464 newPart.SetType(0xEE);
465 newMBR.AddPart(0, newPart);
srs569455d92612010-03-07 22:16:07 -0500466 } // if
srs5694bf8950c2011-03-12 01:23:12 -0500467 theGPT.SetProtectiveMBR(newMBR);
srs569408bb0da2010-02-19 17:19:55 -0500468 } else allOK = 0;
469 } else allOK = 0;
470 if (!allOK)
471 cerr << "Problem creating MBR!\n";
472 return allOK;
473} // BuildMBR()
474
srs56949a46b042011-03-15 00:34:10 -0400475// Returns the number of colons in argument string, ignoring the
476// first character (thus, a leading colon is ignored, as GetString()
477// does).
srs569408bb0da2010-02-19 17:19:55 -0500478int CountColons(char* argument) {
srs56949a46b042011-03-15 00:34:10 -0400479 int num = 0;
srs569408bb0da2010-02-19 17:19:55 -0500480
srs56949a46b042011-03-15 00:34:10 -0400481 while ((argument[0] != '\0') && (argument = strchr(&argument[1], ':')))
482 num++;
483
srs569408bb0da2010-02-19 17:19:55 -0500484 return num;
485} // CountColons()
srs56945a608532011-03-17 13:53:01 -0400486
487// Extract integer data from argument string, which should be colon-delimited
488uint64_t GetInt(const string & argument, int itemNum) {
489 uint64_t retval;
490
491 istringstream inString(GetString(argument, itemNum));
492 inString >> retval;
493 return retval;
494} // GetInt()
495
496// Extract string data from argument string, which should be colon-delimited
497// If string begins with a colon, that colon is skipped in the counting. If an
498// invalid itemNum is specified, returns an empty string.
499string GetString(string argument, int itemNum) {
500 size_t startPos = 0, endPos = 0;
501 string retVal = "";
502 int foundLast = 0;
503 int numFound = 0;
504
505 if (argument[0] == ':')
506 argument.erase(0, 1);
507 while ((numFound < itemNum) && (!foundLast)) {
508 endPos = argument.find(':', startPos);
509 numFound++;
510 if (endPos == string::npos) {
511 foundLast = 1;
512 endPos = argument.length();
513 } else if (numFound < itemNum) {
514 startPos = endPos + 1;
515 } // if/elseif
516 } // while
517 if ((numFound == itemNum) && (numFound > 0))
518 retVal = argument.substr(startPos, endPos - startPos);
519
520 return retVal;
521} // GetString()