blob: b72227749b0634b437fe7661a4e408c214233e8d [file] [log] [blame]
srs56943860cbe2011-09-10 20:29:53 -04001/*
2 Implementation of GPTData class derivative with popt-based command
3 line processing
Roderick W. Smith820d1d02014-02-20 13:25:22 -05004 Copyright (C) 2010-2014 Roderick W. Smith
srs56943860cbe2011-09-10 20:29:53 -04005
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>
Aurimas Liutikas74b74902016-05-10 18:53:54 -070026#include <popt.h>
srs56943860cbe2011-09-10 20:29:53 -040027#include "gptcl.h"
28
29GPTDataCL::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
37GPTDataCL::GPTDataCL(string filename) {
38} // GPTDataCL constructor with filename
39
40GPTDataCL::~GPTDataCL(void) {
41} // GPTDataCL destructor
42
43void 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
srs56940741fa22013-01-09 12:55:40 -050052} // GPTDataCL::LoadBackupFile()
srs56943860cbe2011-09-10 20:29:53 -040053
srs5694d1b11e82011-09-18 21:12:28 -040054// 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
srs56943860cbe2011-09-10 20:29:53 -040063int GPTDataCL::DoOptions(int argc, char* argv[]) {
64 GPTData secondDevice;
65 int opt, numOptions = 0, saveData = 0, neverSaveData = 0;
Roderick W. Smithd28495e2014-03-02 11:37:20 -050066 int partNum = 0, newPartNum = -1, saveNonGPT = 1, retval = 0, pretend = 0;
srs5694e842bc12012-02-03 11:27:05 -050067 uint64_t low, high, startSector, endSector, sSize;
srs56943860cbe2011-09-10 20:29:53 -040068 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 {
Aurimas Liutikas74b74902016-05-10 18:53:54 -070075 {"attributes", 'A', POPT_ARG_STRING, &attributeOperation, 'A', "operate on partition attributes",
76 "list|[partnum:show|or|nand|xor|=|set|clear|toggle|get[:bitnum|hexbitmask]]"},
srs56943860cbe2011-09-10 20:29:53 -040077 {"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...]"},
90 {"info", 'i', POPT_ARG_INT, &infoPartNum, 'i', "show detailed information on partition", "partnum"},
Greg Hartman2c2deeb2016-04-21 18:20:25 -070091 {"skip-sync", 'j', POPT_ARG_NONE, NULL, 'j', "Don't atempt to sync and update the parittion table", ""},
srs56943860cbe2011-09-10 20:29:53 -040092 {"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", ""},
Aurimas Liutikas74b74902016-05-10 18:53:54 -070098 {"print-mbr", 'O', POPT_ARG_NONE, NULL, 'O', "print MBR partition table", ""},
srs56943860cbe2011-09-10 20:29:53 -040099 {"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);
srs56940741fa22013-01-09 12:55:40 -0500118
srs56943860cbe2011-09-10 20:29:53 -0400119 poptSetOtherOptionHelp(poptCon, " [OPTION...] <device>");
srs56940741fa22013-01-09 12:55:40 -0500120
srs56943860cbe2011-09-10 20:29:53 -0400121 if (argc < 2) {
122 poptPrintUsage(poptCon, stderr, 0);
srs5694d1b11e82011-09-18 21:12:28 -0400123 return 1;
srs56943860cbe2011-09-10 20:29:53 -0400124 }
srs56940741fa22013-01-09 12:55:40 -0500125
srs56943860cbe2011-09-10 20:29:53 -0400126 // 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
Roderick W. Smithd28495e2014-03-02 11:37:20 -0500128 // (o, z, or Z) options, and to flag presence of a --pretend/-P option
srs56943860cbe2011-09-10 20:29:53 -0400129 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':
Roderick W. Smithe3ee7332013-09-24 12:56:11 -0400137 typeHelper.ShowAllTypes(0);
srs56943860cbe2011-09-10 20:29:53 -0400138 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
srs56940741fa22013-01-09 12:55:40 -0500150
srs56943860cbe2011-09-10 20:29:53 -0400151 // Assume first non-option argument is the device filename....
152 device = (char*) poptGetArg(poptCon);
153 poptResetContext(poptCon);
srs56940741fa22013-01-09 12:55:40 -0500154
srs56943860cbe2011-09-10 20:29:53 -0400155 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;
Roderick W. Smithd28495e2014-03-02 11:37:20 -0500167 if (partNum < 0)
168 partNum = newPartNum;
srs56943860cbe2011-09-10 20:29:53 -0400169 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':
Roderick W. Smithd28495e2014-03-02 11:37:20 -0500199 cout << "Setting name!\n";
srs56943860cbe2011-09-10 20:29:53 -0400200 JustLooking(0);
201 partNum = (int) GetInt(partName, 1) - 1;
Roderick W. Smithd28495e2014-03-02 11:37:20 -0500202 if (partNum < 0)
203 partNum = newPartNum;
204 cout << "partNum is " << partNum << "\n";
srs56940741fa22013-01-09 12:55:40 -0500205 if ((partNum >= 0) && (partNum < (int) GetNumParts())) {
Roderick W. Smithd28495e2014-03-02 11:37:20 -0500206 cout << "REALLY setting name!\n";
srs56940741fa22013-01-09 12:55:40 -0500207 name = GetString(partName, 2);
208 if (SetName(partNum, (UnicodeString) name.c_str())) {
209 saveData = 1;
210 } else {
211 cerr << "Unable to set partition " << partNum + 1
212 << "'s name to '" << GetString(partName, 2) << "'!\n";
213 neverSaveData = 1;
214 } // if/else
215 free(partName);
216 }
srs56943860cbe2011-09-10 20:29:53 -0400217 break;
218 case 'C':
219 JustLooking(0);
220 RecomputeCHS();
221 saveData = 1;
222 break;
223 case 'd':
224 JustLooking(0);
225 if (DeletePartition(deletePartNum - 1) == 0) {
226 cerr << "Error " << errno << " deleting partition!\n";
227 neverSaveData = 1;
228 } else saveData = 1;
229 break;
230 case 'D':
231 cout << GetAlignment() << "\n";
232 break;
233 case 'e':
234 JustLooking(0);
235 MoveSecondHeaderToEnd();
236 saveData = 1;
237 break;
238 case 'E':
239 cout << FindLastInFree(FindFirstInLargest()) << "\n";
240 break;
241 case 'f':
242 cout << FindFirstInLargest() << "\n";
243 break;
244 case 'F':
245 temp = FindFirstInLargest();
246 Align(&temp);
247 cout << temp << "\n";
248 break;
249 case 'g':
250 JustLooking(0);
251 saveData = 1;
252 saveNonGPT = 1;
253 break;
254 case 'G':
255 JustLooking(0);
256 saveData = 1;
257 RandomizeGUIDs();
258 break;
259 case 'h':
260 JustLooking(0);
261 if (BuildMBR(hybrids, 1) == 1)
262 saveData = 1;
263 break;
264 case 'i':
Jeff Sharkeyd761ff52015-02-28 19:18:39 -0800265 ShowPartDetails(infoPartNum - 1);
srs56943860cbe2011-09-10 20:29:53 -0400266 break;
Greg Hartman2c2deeb2016-04-21 18:20:25 -0700267 case 'j':
268 TurnOffSyncing();
269 break;
srs56943860cbe2011-09-10 20:29:53 -0400270 case 'l':
271 LoadBackupFile(backupFile, saveData, neverSaveData);
272 free(backupFile);
273 break;
274 case 'L':
275 break;
276 case 'm':
277 JustLooking(0);
278 if (BuildMBR(mbrParts, 0) == 1) {
279 if (!pretend) {
280 if (SaveMBR()) {
281 DestroyGPT();
282 } else
283 cerr << "Problem saving MBR!\n";
284 } // if
285 saveNonGPT = 0;
286 pretend = 1; // Not really, but works around problem if -g is used with this...
287 saveData = 0;
288 } // if
289 break;
290 case 'n':
291 JustLooking(0);
Roderick W. Smithd28495e2014-03-02 11:37:20 -0500292 newPartNum = (int) GetInt(newPartInfo, 1) - 1;
293 if (newPartNum < 0)
294 newPartNum = FindFirstFreePart();
srs56943860cbe2011-09-10 20:29:53 -0400295 low = FindFirstInLargest();
srs5694f5dfbfa2013-02-14 20:47:14 -0500296 Align(&low);
srs56943860cbe2011-09-10 20:29:53 -0400297 high = FindLastInFree(low);
298 startSector = IeeeToInt(GetString(newPartInfo, 2), sSize, low, high, low);
299 endSector = IeeeToInt(GetString(newPartInfo, 3), sSize, startSector, high, high);
Roderick W. Smithd28495e2014-03-02 11:37:20 -0500300 if (CreatePartition(newPartNum, startSector, endSector)) {
srs56943860cbe2011-09-10 20:29:53 -0400301 saveData = 1;
302 } else {
Roderick W. Smithd28495e2014-03-02 11:37:20 -0500303 cerr << "Could not create partition " << newPartNum + 1 << " from "
304 << startSector << " to " << endSector << "\n";
srs56943860cbe2011-09-10 20:29:53 -0400305 neverSaveData = 1;
306 } // if/else
307 free(newPartInfo);
308 break;
309 case 'N':
310 JustLooking(0);
311 startSector = FindFirstInLargest();
srs5694f5dfbfa2013-02-14 20:47:14 -0500312 Align(&startSector);
srs56943860cbe2011-09-10 20:29:53 -0400313 endSector = FindLastInFree(startSector);
Aurimas Liutikas74b74902016-05-10 18:53:54 -0700314 if (largestPartNum <= 0)
315 largestPartNum = FindFirstFreePart() + 1;
srs56943860cbe2011-09-10 20:29:53 -0400316 if (CreatePartition(largestPartNum - 1, startSector, endSector)) {
317 saveData = 1;
318 } else {
319 cerr << "Could not create partition " << largestPartNum << " from "
320 << startSector << " to " << endSector << "\n";
321 neverSaveData = 1;
322 } // if/else
323 break;
324 case 'o':
325 JustLooking(0);
326 ClearGPTData();
327 saveData = 1;
328 break;
Aurimas Liutikas74b74902016-05-10 18:53:54 -0700329 case 'O':
330 DisplayMBRData();
331 break;
srs56943860cbe2011-09-10 20:29:53 -0400332 case 'p':
333 DisplayGPTData();
334 break;
335 case 'P':
336 pretend = 1;
337 break;
338 case 'r':
339 JustLooking(0);
340 uint64_t p1, p2;
341 p1 = GetInt(twoParts, 1) - 1;
342 p2 = GetInt(twoParts, 2) - 1;
343 if (SwapPartitions((uint32_t) p1, (uint32_t) p2) == 0) {
344 neverSaveData = 1;
345 cerr << "Cannot swap partitions " << p1 + 1 << " and " << p2 + 1 << "\n";
346 } else saveData = 1;
347 break;
348 case 'R':
349 secondDevice = *this;
350 secondDevice.SetDisk(outDevice);
351 secondDevice.JustLooking(0);
352 if (!secondDevice.SaveGPTData(1))
353 retval = 8;
354 break;
355 case 's':
356 JustLooking(0);
357 SortGPT();
358 saveData = 1;
359 break;
360 case 'S':
361 JustLooking(0);
362 if (SetGPTSize(tableSize) == 0)
363 neverSaveData = 1;
364 else
365 saveData = 1;
366 break;
367 case 't':
368 JustLooking(0);
369 partNum = (int) GetInt(typeCode, 1) - 1;
Roderick W. Smithd28495e2014-03-02 11:37:20 -0500370 if (partNum < 0)
371 partNum = newPartNum;
srs56940741fa22013-01-09 12:55:40 -0500372 if ((partNum >= 0) && (partNum < (int) GetNumParts())) {
373 typeHelper = GetString(typeCode, 2);
374 if ((typeHelper != (GUIDData) "00000000-0000-0000-0000-000000000000") &&
375 (ChangePartType(partNum, typeHelper))) {
376 saveData = 1;
377 } else {
378 cerr << "Could not change partition " << partNum + 1
379 << "'s type code to " << GetString(typeCode, 2) << "!\n";
380 neverSaveData = 1;
381 } // if/else
srs56943860cbe2011-09-10 20:29:53 -0400382 free(typeCode);
srs56940741fa22013-01-09 12:55:40 -0500383 }
srs56943860cbe2011-09-10 20:29:53 -0400384 break;
385 case 'T':
386 JustLooking(0);
387 XFormDisklabel(bsdPartNum - 1);
388 saveData = 1;
389 break;
390 case 'u':
391 JustLooking(0);
392 saveData = 1;
srs56940741fa22013-01-09 12:55:40 -0500393 partNum = (int) GetInt(partGUID, 1) - 1;
Roderick W. Smithd28495e2014-03-02 11:37:20 -0500394 if (partNum < 0)
395 partNum = newPartNum;
srs56940741fa22013-01-09 12:55:40 -0500396 if ((partNum >= 0) && (partNum < (int) GetNumParts())) {
397 SetPartitionGUID(partNum, GetString(partGUID, 2).c_str());
398 }
srs56943860cbe2011-09-10 20:29:53 -0400399 break;
400 case 'U':
401 JustLooking(0);
402 saveData = 1;
403 SetDiskGUID(diskGUID);
404 break;
405 case 'v':
406 Verify();
407 break;
408 case 'z':
409 if (!pretend) {
410 DestroyGPT();
411 } // if
Aurimas Liutikas74b74902016-05-10 18:53:54 -0700412 saveNonGPT = 1;
srs56943860cbe2011-09-10 20:29:53 -0400413 saveData = 0;
414 break;
415 case 'Z':
416 if (!pretend) {
417 DestroyGPT();
418 DestroyMBR();
419 } // if
Aurimas Liutikas74b74902016-05-10 18:53:54 -0700420 saveNonGPT = 1;
srs56943860cbe2011-09-10 20:29:53 -0400421 saveData = 0;
422 break;
423 default:
424 cerr << "Unknown option (-" << opt << ")!\n";
425 break;
426 } // switch
427 } // while
428 } else { // if loaded OK
429 poptResetContext(poptCon);
430 // Do a few types of operations even if there are problems....
431 while ((opt = poptGetNextOpt(poptCon)) > 0) {
432 switch (opt) {
433 case 'l':
434 LoadBackupFile(backupFile, saveData, neverSaveData);
435 cout << "Information: Loading backup partition table; will override earlier problems!\n";
436 free(backupFile);
437 retval = 0;
438 break;
439 case 'o':
440 JustLooking(0);
441 ClearGPTData();
442 saveData = 1;
443 cout << "Information: Creating fresh partition table; will override earlier problems!\n";
444 retval = 0;
445 break;
446 case 'v':
447 cout << "Verification may miss some problems or report too many!\n";
448 Verify();
449 break;
450 case 'z':
451 if (!pretend) {
452 DestroyGPT();
453 } // if
Aurimas Liutikas74b74902016-05-10 18:53:54 -0700454 saveNonGPT = 1;
srs56943860cbe2011-09-10 20:29:53 -0400455 saveData = 0;
456 break;
457 case 'Z':
458 if (!pretend) {
459 DestroyGPT();
460 DestroyMBR();
461 } // if
Aurimas Liutikas74b74902016-05-10 18:53:54 -0700462 saveNonGPT = 1;
srs56943860cbe2011-09-10 20:29:53 -0400463 saveData = 0;
464 break;
465 } // switch
466 } // while
467 retval = 2;
468 } // if/else loaded OK
469 if ((saveData) && (!neverSaveData) && (saveNonGPT) && (!pretend)) {
Aurimas Liutikas74b74902016-05-10 18:53:54 -0700470 if (!SaveGPTData(1))
471 retval = 4;
srs56943860cbe2011-09-10 20:29:53 -0400472 }
473 if (saveData && (!saveNonGPT)) {
474 cout << "Non-GPT disk; not saving changes. Use -g to override.\n";
475 retval = 3;
476 } // if
477 if (neverSaveData) {
478 cerr << "Error encountered; not saving changes.\n";
479 retval = 4;
480 } // if
481 } // if (device != NULL)
482 poptFreeContext(poptCon);
483 return retval;
484} // GPTDataCL::DoOptions()
485
486// Create a hybrid or regular MBR from GPT data structures
487int GPTDataCL::BuildMBR(char* argument, int isHybrid) {
488 int numParts, allOK = 1, i, origPartNum;
489 MBRPart newPart;
490 BasicMBRData newMBR;
Roderick W. Smith820d1d02014-02-20 13:25:22 -0500491
srs56943860cbe2011-09-10 20:29:53 -0400492 if (argument != NULL) {
493 numParts = CountColons(argument) + 1;
494 if (numParts <= (4 - isHybrid)) {
495 newMBR.SetDisk(GetDisk());
496 for (i = 0; i < numParts; i++) {
497 origPartNum = GetInt(argument, i + 1) - 1;
Roderick W. Smitha345a922014-02-22 12:12:32 -0500498 if (IsUsedPartNum(origPartNum) && (partitions[origPartNum].IsSizedForMBR() == MBR_SIZED_GOOD)) {
srs56943860cbe2011-09-10 20:29:53 -0400499 newPart.SetInclusion(PRIMARY);
500 newPart.SetLocation(operator[](origPartNum).GetFirstLBA(),
501 operator[](origPartNum).GetLengthLBA());
502 newPart.SetStatus(0);
503 newPart.SetType((uint8_t)(operator[](origPartNum).GetHexType() / 0x0100));
504 newMBR.AddPart(i + isHybrid, newPart);
505 } else {
Roderick W. Smith820d1d02014-02-20 13:25:22 -0500506 cerr << "Original partition " << origPartNum + 1 << " does not exist or is too big! Aborting operation!\n";
srs56943860cbe2011-09-10 20:29:53 -0400507 allOK = 0;
508 } // if/else
509 } // for
510 if (isHybrid) {
511 newPart.SetInclusion(PRIMARY);
512 newPart.SetLocation(1, newMBR.FindLastInFree(1));
513 newPart.SetStatus(0);
514 newPart.SetType(0xEE);
515 newMBR.AddPart(0, newPart);
516 } // if
Roderick W. Smitha345a922014-02-22 12:12:32 -0500517 if (allOK)
518 SetProtectiveMBR(newMBR);
srs56943860cbe2011-09-10 20:29:53 -0400519 } else allOK = 0;
520 } else allOK = 0;
521 if (!allOK)
522 cerr << "Problem creating MBR!\n";
523 return allOK;
524} // GPTDataCL::BuildMBR()
525
526// Returns the number of colons in argument string, ignoring the
527// first character (thus, a leading colon is ignored, as GetString()
528// does).
529int CountColons(char* argument) {
530 int num = 0;
srs56940741fa22013-01-09 12:55:40 -0500531
srs56943860cbe2011-09-10 20:29:53 -0400532 while ((argument[0] != '\0') && (argument = strchr(&argument[1], ':')))
533 num++;
srs56940741fa22013-01-09 12:55:40 -0500534
srs56943860cbe2011-09-10 20:29:53 -0400535 return num;
536} // GPTDataCL::CountColons()
537
538// Extract integer data from argument string, which should be colon-delimited
539uint64_t GetInt(const string & argument, int itemNum) {
540 uint64_t retval;
srs56940741fa22013-01-09 12:55:40 -0500541
srs56943860cbe2011-09-10 20:29:53 -0400542 istringstream inString(GetString(argument, itemNum));
543 inString >> retval;
544 return retval;
545} // GPTDataCL::GetInt()
546
547// Extract string data from argument string, which should be colon-delimited
548// If string begins with a colon, that colon is skipped in the counting. If an
549// invalid itemNum is specified, returns an empty string.
550string GetString(string argument, int itemNum) {
551 size_t startPos = 0, endPos = 0;
552 string retVal = "";
553 int foundLast = 0;
554 int numFound = 0;
srs56940741fa22013-01-09 12:55:40 -0500555
srs56943860cbe2011-09-10 20:29:53 -0400556 if (argument[0] == ':')
557 argument.erase(0, 1);
558 while ((numFound < itemNum) && (!foundLast)) {
559 endPos = argument.find(':', startPos);
560 numFound++;
561 if (endPos == string::npos) {
562 foundLast = 1;
563 endPos = argument.length();
564 } else if (numFound < itemNum) {
565 startPos = endPos + 1;
566 } // if/elseif
567 } // while
568 if ((numFound == itemNum) && (numFound > 0))
569 retVal = argument.substr(startPos, endPos - startPos);
srs56940741fa22013-01-09 12:55:40 -0500570
srs56943860cbe2011-09-10 20:29:53 -0400571 return retVal;
572} // GetString()