blob: bdb46932dd29c892f90649ea27378b476978fc10 [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"},
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", ""},
Aurimas Liutikas74b74902016-05-10 18:53:54 -070097 {"print-mbr", 'O', POPT_ARG_NONE, NULL, 'O', "print MBR partition table", ""},
srs56943860cbe2011-09-10 20:29:53 -040098 {"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", ""},
Aurimas Liutikasfcad0602016-05-10 19:16:10 -0700112 POPT_AUTOHELP { NULL, 0, 0, NULL, 0, NULL, NULL }
srs56943860cbe2011-09-10 20:29:53 -0400113 };
114
115 // Create popt context...
116 poptCon = poptGetContext(NULL, argc, (const char**) argv, theOptions, 0);
srs56940741fa22013-01-09 12:55:40 -0500117
srs56943860cbe2011-09-10 20:29:53 -0400118 poptSetOtherOptionHelp(poptCon, " [OPTION...] <device>");
srs56940741fa22013-01-09 12:55:40 -0500119
srs56943860cbe2011-09-10 20:29:53 -0400120 if (argc < 2) {
121 poptPrintUsage(poptCon, stderr, 0);
srs5694d1b11e82011-09-18 21:12:28 -0400122 return 1;
srs56943860cbe2011-09-10 20:29:53 -0400123 }
srs56940741fa22013-01-09 12:55:40 -0500124
srs56943860cbe2011-09-10 20:29:53 -0400125 // 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
Roderick W. Smithd28495e2014-03-02 11:37:20 -0500127 // (o, z, or Z) options, and to flag presence of a --pretend/-P option
srs56943860cbe2011-09-10 20:29:53 -0400128 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':
Roderick W. Smithe3ee7332013-09-24 12:56:11 -0400136 typeHelper.ShowAllTypes(0);
srs56943860cbe2011-09-10 20:29:53 -0400137 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
srs56940741fa22013-01-09 12:55:40 -0500149
srs56943860cbe2011-09-10 20:29:53 -0400150 // Assume first non-option argument is the device filename....
151 device = (char*) poptGetArg(poptCon);
152 poptResetContext(poptCon);
srs56940741fa22013-01-09 12:55:40 -0500153
srs56943860cbe2011-09-10 20:29:53 -0400154 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;
Roderick W. Smithd28495e2014-03-02 11:37:20 -0500166 if (partNum < 0)
167 partNum = newPartNum;
srs56943860cbe2011-09-10 20:29:53 -0400168 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':
Roderick W. Smithd28495e2014-03-02 11:37:20 -0500198 cout << "Setting name!\n";
srs56943860cbe2011-09-10 20:29:53 -0400199 JustLooking(0);
200 partNum = (int) GetInt(partName, 1) - 1;
Roderick W. Smithd28495e2014-03-02 11:37:20 -0500201 if (partNum < 0)
202 partNum = newPartNum;
203 cout << "partNum is " << partNum << "\n";
srs56940741fa22013-01-09 12:55:40 -0500204 if ((partNum >= 0) && (partNum < (int) GetNumParts())) {
Roderick W. Smithd28495e2014-03-02 11:37:20 -0500205 cout << "REALLY setting name!\n";
srs56940741fa22013-01-09 12:55:40 -0500206 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 }
srs56943860cbe2011-09-10 20:29:53 -0400216 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':
Jeff Sharkeyd761ff52015-02-28 19:18:39 -0800264 ShowPartDetails(infoPartNum - 1);
srs56943860cbe2011-09-10 20:29:53 -0400265 break;
266 case 'l':
267 LoadBackupFile(backupFile, saveData, neverSaveData);
268 free(backupFile);
269 break;
270 case 'L':
271 break;
272 case 'm':
273 JustLooking(0);
274 if (BuildMBR(mbrParts, 0) == 1) {
275 if (!pretend) {
276 if (SaveMBR()) {
277 DestroyGPT();
278 } else
279 cerr << "Problem saving MBR!\n";
280 } // if
281 saveNonGPT = 0;
282 pretend = 1; // Not really, but works around problem if -g is used with this...
283 saveData = 0;
284 } // if
285 break;
286 case 'n':
287 JustLooking(0);
Roderick W. Smithd28495e2014-03-02 11:37:20 -0500288 newPartNum = (int) GetInt(newPartInfo, 1) - 1;
289 if (newPartNum < 0)
290 newPartNum = FindFirstFreePart();
srs56943860cbe2011-09-10 20:29:53 -0400291 low = FindFirstInLargest();
srs5694f5dfbfa2013-02-14 20:47:14 -0500292 Align(&low);
srs56943860cbe2011-09-10 20:29:53 -0400293 high = FindLastInFree(low);
294 startSector = IeeeToInt(GetString(newPartInfo, 2), sSize, low, high, low);
295 endSector = IeeeToInt(GetString(newPartInfo, 3), sSize, startSector, high, high);
Roderick W. Smithd28495e2014-03-02 11:37:20 -0500296 if (CreatePartition(newPartNum, startSector, endSector)) {
srs56943860cbe2011-09-10 20:29:53 -0400297 saveData = 1;
298 } else {
Roderick W. Smithd28495e2014-03-02 11:37:20 -0500299 cerr << "Could not create partition " << newPartNum + 1 << " from "
300 << startSector << " to " << endSector << "\n";
srs56943860cbe2011-09-10 20:29:53 -0400301 neverSaveData = 1;
302 } // if/else
303 free(newPartInfo);
304 break;
305 case 'N':
306 JustLooking(0);
307 startSector = FindFirstInLargest();
srs5694f5dfbfa2013-02-14 20:47:14 -0500308 Align(&startSector);
srs56943860cbe2011-09-10 20:29:53 -0400309 endSector = FindLastInFree(startSector);
Aurimas Liutikas74b74902016-05-10 18:53:54 -0700310 if (largestPartNum <= 0)
311 largestPartNum = FindFirstFreePart() + 1;
srs56943860cbe2011-09-10 20:29:53 -0400312 if (CreatePartition(largestPartNum - 1, startSector, endSector)) {
313 saveData = 1;
314 } else {
315 cerr << "Could not create partition " << largestPartNum << " from "
316 << startSector << " to " << endSector << "\n";
317 neverSaveData = 1;
318 } // if/else
319 break;
320 case 'o':
321 JustLooking(0);
322 ClearGPTData();
323 saveData = 1;
324 break;
Aurimas Liutikas74b74902016-05-10 18:53:54 -0700325 case 'O':
326 DisplayMBRData();
327 break;
srs56943860cbe2011-09-10 20:29:53 -0400328 case 'p':
329 DisplayGPTData();
330 break;
331 case 'P':
332 pretend = 1;
333 break;
334 case 'r':
335 JustLooking(0);
336 uint64_t p1, p2;
337 p1 = GetInt(twoParts, 1) - 1;
338 p2 = GetInt(twoParts, 2) - 1;
339 if (SwapPartitions((uint32_t) p1, (uint32_t) p2) == 0) {
340 neverSaveData = 1;
341 cerr << "Cannot swap partitions " << p1 + 1 << " and " << p2 + 1 << "\n";
342 } else saveData = 1;
343 break;
344 case 'R':
345 secondDevice = *this;
346 secondDevice.SetDisk(outDevice);
347 secondDevice.JustLooking(0);
348 if (!secondDevice.SaveGPTData(1))
349 retval = 8;
350 break;
351 case 's':
352 JustLooking(0);
353 SortGPT();
354 saveData = 1;
355 break;
356 case 'S':
357 JustLooking(0);
358 if (SetGPTSize(tableSize) == 0)
359 neverSaveData = 1;
360 else
361 saveData = 1;
362 break;
363 case 't':
364 JustLooking(0);
365 partNum = (int) GetInt(typeCode, 1) - 1;
Roderick W. Smithd28495e2014-03-02 11:37:20 -0500366 if (partNum < 0)
367 partNum = newPartNum;
srs56940741fa22013-01-09 12:55:40 -0500368 if ((partNum >= 0) && (partNum < (int) GetNumParts())) {
369 typeHelper = GetString(typeCode, 2);
370 if ((typeHelper != (GUIDData) "00000000-0000-0000-0000-000000000000") &&
371 (ChangePartType(partNum, typeHelper))) {
372 saveData = 1;
373 } else {
374 cerr << "Could not change partition " << partNum + 1
375 << "'s type code to " << GetString(typeCode, 2) << "!\n";
376 neverSaveData = 1;
377 } // if/else
srs56943860cbe2011-09-10 20:29:53 -0400378 free(typeCode);
srs56940741fa22013-01-09 12:55:40 -0500379 }
srs56943860cbe2011-09-10 20:29:53 -0400380 break;
381 case 'T':
382 JustLooking(0);
383 XFormDisklabel(bsdPartNum - 1);
384 saveData = 1;
385 break;
386 case 'u':
387 JustLooking(0);
388 saveData = 1;
srs56940741fa22013-01-09 12:55:40 -0500389 partNum = (int) GetInt(partGUID, 1) - 1;
Roderick W. Smithd28495e2014-03-02 11:37:20 -0500390 if (partNum < 0)
391 partNum = newPartNum;
srs56940741fa22013-01-09 12:55:40 -0500392 if ((partNum >= 0) && (partNum < (int) GetNumParts())) {
393 SetPartitionGUID(partNum, GetString(partGUID, 2).c_str());
394 }
srs56943860cbe2011-09-10 20:29:53 -0400395 break;
396 case 'U':
397 JustLooking(0);
398 saveData = 1;
399 SetDiskGUID(diskGUID);
400 break;
401 case 'v':
402 Verify();
403 break;
404 case 'z':
405 if (!pretend) {
406 DestroyGPT();
407 } // if
Aurimas Liutikas74b74902016-05-10 18:53:54 -0700408 saveNonGPT = 1;
srs56943860cbe2011-09-10 20:29:53 -0400409 saveData = 0;
410 break;
411 case 'Z':
412 if (!pretend) {
413 DestroyGPT();
414 DestroyMBR();
415 } // if
Aurimas Liutikas74b74902016-05-10 18:53:54 -0700416 saveNonGPT = 1;
srs56943860cbe2011-09-10 20:29:53 -0400417 saveData = 0;
418 break;
419 default:
420 cerr << "Unknown option (-" << opt << ")!\n";
421 break;
422 } // switch
423 } // while
424 } else { // if loaded OK
425 poptResetContext(poptCon);
426 // Do a few types of operations even if there are problems....
427 while ((opt = poptGetNextOpt(poptCon)) > 0) {
428 switch (opt) {
429 case 'l':
430 LoadBackupFile(backupFile, saveData, neverSaveData);
431 cout << "Information: Loading backup partition table; will override earlier problems!\n";
432 free(backupFile);
433 retval = 0;
434 break;
435 case 'o':
436 JustLooking(0);
437 ClearGPTData();
438 saveData = 1;
439 cout << "Information: Creating fresh partition table; will override earlier problems!\n";
440 retval = 0;
441 break;
442 case 'v':
443 cout << "Verification may miss some problems or report too many!\n";
444 Verify();
445 break;
446 case 'z':
447 if (!pretend) {
448 DestroyGPT();
449 } // if
Aurimas Liutikas74b74902016-05-10 18:53:54 -0700450 saveNonGPT = 1;
srs56943860cbe2011-09-10 20:29:53 -0400451 saveData = 0;
452 break;
453 case 'Z':
454 if (!pretend) {
455 DestroyGPT();
456 DestroyMBR();
457 } // if
Aurimas Liutikas74b74902016-05-10 18:53:54 -0700458 saveNonGPT = 1;
srs56943860cbe2011-09-10 20:29:53 -0400459 saveData = 0;
460 break;
461 } // switch
462 } // while
463 retval = 2;
464 } // if/else loaded OK
465 if ((saveData) && (!neverSaveData) && (saveNonGPT) && (!pretend)) {
Aurimas Liutikas74b74902016-05-10 18:53:54 -0700466 if (!SaveGPTData(1))
467 retval = 4;
srs56943860cbe2011-09-10 20:29:53 -0400468 }
469 if (saveData && (!saveNonGPT)) {
470 cout << "Non-GPT disk; not saving changes. Use -g to override.\n";
471 retval = 3;
472 } // if
473 if (neverSaveData) {
474 cerr << "Error encountered; not saving changes.\n";
475 retval = 4;
476 } // if
477 } // if (device != NULL)
478 poptFreeContext(poptCon);
479 return retval;
480} // GPTDataCL::DoOptions()
481
482// Create a hybrid or regular MBR from GPT data structures
483int GPTDataCL::BuildMBR(char* argument, int isHybrid) {
484 int numParts, allOK = 1, i, origPartNum;
485 MBRPart newPart;
486 BasicMBRData newMBR;
Roderick W. Smith820d1d02014-02-20 13:25:22 -0500487
srs56943860cbe2011-09-10 20:29:53 -0400488 if (argument != NULL) {
489 numParts = CountColons(argument) + 1;
490 if (numParts <= (4 - isHybrid)) {
491 newMBR.SetDisk(GetDisk());
492 for (i = 0; i < numParts; i++) {
493 origPartNum = GetInt(argument, i + 1) - 1;
Roderick W. Smitha345a922014-02-22 12:12:32 -0500494 if (IsUsedPartNum(origPartNum) && (partitions[origPartNum].IsSizedForMBR() == MBR_SIZED_GOOD)) {
srs56943860cbe2011-09-10 20:29:53 -0400495 newPart.SetInclusion(PRIMARY);
496 newPart.SetLocation(operator[](origPartNum).GetFirstLBA(),
497 operator[](origPartNum).GetLengthLBA());
498 newPart.SetStatus(0);
499 newPart.SetType((uint8_t)(operator[](origPartNum).GetHexType() / 0x0100));
500 newMBR.AddPart(i + isHybrid, newPart);
501 } else {
Roderick W. Smith820d1d02014-02-20 13:25:22 -0500502 cerr << "Original partition " << origPartNum + 1 << " does not exist or is too big! Aborting operation!\n";
srs56943860cbe2011-09-10 20:29:53 -0400503 allOK = 0;
504 } // if/else
505 } // for
506 if (isHybrid) {
507 newPart.SetInclusion(PRIMARY);
508 newPart.SetLocation(1, newMBR.FindLastInFree(1));
509 newPart.SetStatus(0);
510 newPart.SetType(0xEE);
511 newMBR.AddPart(0, newPart);
512 } // if
Roderick W. Smitha345a922014-02-22 12:12:32 -0500513 if (allOK)
514 SetProtectiveMBR(newMBR);
srs56943860cbe2011-09-10 20:29:53 -0400515 } else allOK = 0;
516 } else allOK = 0;
517 if (!allOK)
518 cerr << "Problem creating MBR!\n";
519 return allOK;
520} // GPTDataCL::BuildMBR()
521
522// Returns the number of colons in argument string, ignoring the
523// first character (thus, a leading colon is ignored, as GetString()
524// does).
525int CountColons(char* argument) {
526 int num = 0;
srs56940741fa22013-01-09 12:55:40 -0500527
srs56943860cbe2011-09-10 20:29:53 -0400528 while ((argument[0] != '\0') && (argument = strchr(&argument[1], ':')))
529 num++;
srs56940741fa22013-01-09 12:55:40 -0500530
srs56943860cbe2011-09-10 20:29:53 -0400531 return num;
532} // GPTDataCL::CountColons()
533
534// Extract integer data from argument string, which should be colon-delimited
535uint64_t GetInt(const string & argument, int itemNum) {
536 uint64_t retval;
srs56940741fa22013-01-09 12:55:40 -0500537
srs56943860cbe2011-09-10 20:29:53 -0400538 istringstream inString(GetString(argument, itemNum));
539 inString >> retval;
540 return retval;
541} // GPTDataCL::GetInt()
542
543// Extract string data from argument string, which should be colon-delimited
544// If string begins with a colon, that colon is skipped in the counting. If an
545// invalid itemNum is specified, returns an empty string.
546string GetString(string argument, int itemNum) {
547 size_t startPos = 0, endPos = 0;
548 string retVal = "";
549 int foundLast = 0;
550 int numFound = 0;
srs56940741fa22013-01-09 12:55:40 -0500551
srs56943860cbe2011-09-10 20:29:53 -0400552 if (argument[0] == ':')
553 argument.erase(0, 1);
554 while ((numFound < itemNum) && (!foundLast)) {
555 endPos = argument.find(':', startPos);
556 numFound++;
557 if (endPos == string::npos) {
558 foundLast = 1;
559 endPos = argument.length();
560 } else if (numFound < itemNum) {
561 startPos = endPos + 1;
562 } // if/elseif
563 } // while
564 if ((numFound == itemNum) && (numFound > 0))
565 retVal = argument.substr(startPos, endPos - startPos);
srs56940741fa22013-01-09 12:55:40 -0500566
srs56943860cbe2011-09-10 20:29:53 -0400567 return retVal;
568} // GetString()