blob: 40c426c4a95ecbe8655c81ba468653e8d9422ae3 [file] [log] [blame]
srs5694a0eb11a2009-08-29 15:00:08 -04001//
2// C++ Implementation: gptpart
3//
4// Description: Class to implement a SINGLE GPT partition
5//
6//
7// Author: Rod Smith <rodsmith@rodsbooks.com>, (C) 2009
8//
9// Copyright: See COPYING file that comes with this distribution
10//
11//
srs5694978041c2009-09-21 20:51:47 -040012// This program is copyright (c) 2009 by Roderick W. Smith. It is distributed
13// under the terms of the GNU GPL version 2, as detailed in the COPYING file.
srs5694a0eb11a2009-08-29 15:00:08 -040014
15#define __STDC_LIMIT_MACROS
16#define __STDC_CONSTANT_MACROS
17
srs5694a0eb11a2009-08-29 15:00:08 -040018#include <string.h>
srs5694fed16d02010-01-27 23:03:40 -050019#include <stdio.h>
20#include <iostream>
srs5694a0eb11a2009-08-29 15:00:08 -040021#include "gptpart.h"
22#include "attributes.h"
23
24using namespace std;
25
26PartTypes GPTPart::typeHelper;
27
28GPTPart::GPTPart(void) {
srs5694978041c2009-09-21 20:51:47 -040029 int i;
30
31 for (i = 0; i < NAME_SIZE; i++)
32 name[i] = '\0';
srs5694a0eb11a2009-08-29 15:00:08 -040033} // Default constructor
34
35GPTPart::~GPTPart(void) {
36} // destructor
37
srs5694a0eb11a2009-08-29 15:00:08 -040038// Return the gdisk-specific two-byte hex code for the partition
39uint16_t GPTPart::GetHexType(void) {
40 return typeHelper.GUIDToID(partitionType);
41} // GPTPart::GetHexType()
42
43// Return a plain-text description of the partition type (e.g., "Linux/Windows
44// data" or "Linux swap").
srs5694546a9c72010-01-26 16:00:26 -050045string GPTPart::GetNameType(void) {
srs5694fed16d02010-01-27 23:03:40 -050046 return typeHelper.GUIDToName(partitionType);
srs5694a0eb11a2009-08-29 15:00:08 -040047} // GPTPart::GetNameType()
48
49// Compute and return the partition's length (or 0 if the end is incorrectly
50// set before the beginning).
51uint64_t GPTPart::GetLengthLBA(void) {
52 uint64_t length = 0;
53 if (firstLBA <= lastLBA)
54 length = lastLBA - firstLBA + UINT64_C(1);
55 return length;
56} // GPTPart::GetLengthLBA()
57
srs56940a697312010-01-28 21:10:52 -050058// Return partition's name field, converted to a C++ ASCII string
59string GPTPart::GetName(void) {
60 string theName;
srs5694a0eb11a2009-08-29 15:00:08 -040061 int i;
62
srs56940a697312010-01-28 21:10:52 -050063 theName = "";
64 for (i = 0; i < NAME_SIZE; i += 2) {
65 if (name[i] != '\0')
66 theName += name[i];
67 } // for
68 return theName;
69} // GPTPart::GetName()
70
71// Set the type code to the specified one. Also changes the partition
72// name *IF* the current name is the generic one for the current partition
73// type.
74void GPTPart::SetType(struct GUIDData t) {
srs56940a697312010-01-28 21:10:52 -050075 if (GetName() == typeHelper.GUIDToName(partitionType)) {
76 SetName(typeHelper.GUIDToName(t));
77 } // if
78 partitionType = t;
79} // GPTPart::SetType()
srs5694a0eb11a2009-08-29 15:00:08 -040080
81// Sets the unique GUID to a value of 0 or a random value,
82// depending on the parameter: 0 = 0, anything else = random
83void GPTPart::SetUniqueGUID(int zeroOrRandom) {
84 if (zeroOrRandom == 0) {
85 uniqueGUID.data1 = 0;
86 uniqueGUID.data2 = 0;
87 } else {
88 // rand() is only 32 bits on 32-bit systems, so multiply together to
89 // fill a 64-bit value.
90 uniqueGUID.data1 = (uint64_t) rand() * (uint64_t) rand();
91 uniqueGUID.data2 = (uint64_t) rand() * (uint64_t) rand();
92 }
93} // GPTPart::SetUniqueGUID()
94
srs5694a0eb11a2009-08-29 15:00:08 -040095// Set the name for a partition to theName, or prompt for a name if
srs5694fed16d02010-01-27 23:03:40 -050096// theName is empty. Note that theName is a standard C++-style ASCII
srs5694a0eb11a2009-08-29 15:00:08 -040097// string, although the GUID partition definition requires a UTF-16LE
98// string. This function creates a simple-minded copy for this.
srs56940a697312010-01-28 21:10:52 -050099void GPTPart::SetName(const string & theName) {
srs5694a0eb11a2009-08-29 15:00:08 -0400100 char newName[NAME_SIZE]; // New name
srs5694fed16d02010-01-27 23:03:40 -0500101 char *junk;
srs5694a0eb11a2009-08-29 15:00:08 -0400102 int i;
103
104 // Blank out new name string, just to be on the safe side....
105 for (i = 0; i < NAME_SIZE; i++)
106 newName[i] = '\0';
107
srs5694fed16d02010-01-27 23:03:40 -0500108 if (theName == "") { // No name specified, so get one from the user
109 cout << "Enter name: ";
srs56945d58fe02010-01-03 20:57:08 -0500110 junk = fgets(newName, NAME_SIZE / 2, stdin);
srs5694a0eb11a2009-08-29 15:00:08 -0400111
112 // Input is likely to include a newline, so remove it....
113 i = strlen(newName);
114 if (newName[i - 1] == '\n')
115 newName[i - 1] = '\0';
116 } else {
srs5694fed16d02010-01-27 23:03:40 -0500117 strcpy(newName, theName.substr(0, NAME_SIZE / 2).c_str());
srs5694a0eb11a2009-08-29 15:00:08 -0400118 } // if
119
120 // Copy the C-style ASCII string from newName into a form that the GPT
121 // table will accept....
122 for (i = 0; i < NAME_SIZE; i++) {
123 if ((i % 2) == 0) {
124 name[i] = newName[(i / 2)];
125 } else {
126 name[i] = '\0';
127 } // if/else
128 } // for
129} // GPTPart::SetName()
130
srs56940a697312010-01-28 21:10:52 -0500131GPTPart & GPTPart::operator=(const GPTPart & orig) {
132 int i;
133
134 partitionType = orig.partitionType;
135 uniqueGUID = orig.uniqueGUID;
136 firstLBA = orig.firstLBA;
137 lastLBA = orig.lastLBA;
138 attributes = orig.attributes;
139 for (i = 0; i < NAME_SIZE; i++)
140 name[i] = orig.name[i];
141 return *this;
142} // assignment operator
143
144// Display summary information; does nothing if the partition is empty.
145void GPTPart::ShowSummary(int partNum, uint32_t blockSize) {
146 string sizeInSI;
147 int i;
148
149 if (firstLBA != 0) {
150 sizeInSI = BytesToSI(blockSize * (lastLBA - firstLBA + 1));
151 cout.width(4);
152 cout << partNum + 1 << " ";
153 cout.width(14);
154 cout << firstLBA << " ";
155 cout.width(14);
156 cout << lastLBA << " ";
157 cout << BytesToSI(blockSize * (lastLBA - firstLBA + 1)) << " ";
srs5694e321d442010-01-29 17:44:04 -0500158 for (i = 0; i < 9 - (int) sizeInSI.length(); i++) cout << " ";
srs56940a697312010-01-28 21:10:52 -0500159 cout.fill('0');
160 cout.width(4);
161 cout.setf(ios::uppercase);
162 cout << hex << typeHelper.GUIDToID(partitionType) << " " << dec;
163 cout.fill(' ');
164// cout.setf(ios::right);
165 cout << GetName().substr(0, 23) << "\n";
166 cout.fill(' ');
167 } // if
168} // GPTPart::ShowSummary()
169
170// Show detailed partition information. Does nothing if the partition is
171// empty (as determined by firstLBA being 0).
172void GPTPart::ShowDetails(uint32_t blockSize) {
173 uint64_t size;
174
175 if (firstLBA != 0) {
176 cout << "Partition GUID code: " << GUIDToStr(partitionType);
177 cout << " (" << typeHelper.GUIDToName(partitionType) << ")\n";
178 cout << "Partition unique GUID: " << GUIDToStr(uniqueGUID) << "\n";
179
180 cout << "First sector: " << firstLBA << " (at "
181 << BytesToSI(firstLBA * blockSize) << ")\n";
182 cout << "Last sector: " << lastLBA << " (at "
183 << BytesToSI(lastLBA * blockSize) << ")\n";
184 size = (lastLBA - firstLBA + 1);
185 cout << "Partition size: " << size << " sectors ("
186 << BytesToSI(size * ((uint64_t) blockSize)) << ")\n";
187 cout << "Attribute flags: ";
188 cout.fill('0');
189 cout.width(16);
190 cout << hex;
191 cout << attributes << "\n";
192 cout << dec;
193 cout << "Partition name: " << GetName() << "\n";
194 cout.fill(' ');
195 } // if
196} // GPTPart::ShowDetails()
197
198// Blank (delete) a single partition
199void GPTPart::BlankPartition(void) {
200 int j;
201 GUIDData zeroGUID;
202
203 zeroGUID.data1 = 0;
204 zeroGUID.data2 = 0;
205 uniqueGUID = zeroGUID;
206 partitionType = zeroGUID;
207 firstLBA = 0;
208 lastLBA = 0;
209 attributes = 0;
210 for (j = 0; j < NAME_SIZE; j++)
211 name[j] = '\0';
212} // GPTPart::BlankPartition
213
214// Returns 1 if the two partitions overlap, 0 if they don't
215int GPTPart::DoTheyOverlap(const GPTPart & other) {
216 int theyDo = 0;
217
218 // Don't bother checking unless these are defined (both start and end points
219 // are 0 for undefined partitions, so just check the start points)
220 if ((firstLBA != 0) && (other.firstLBA != 0)) {
221 if ((firstLBA < other.lastLBA) && (lastLBA >= other.firstLBA))
222 theyDo = 1;
223 if ((other.firstLBA < lastLBA) && (other.lastLBA >= firstLBA))
224 theyDo = 1;
225 } // if
226 return (theyDo);
227} // GPTPart::DoTheyOverlap()
228
229// Reverse the bytes of integral data types; used on big-endian systems.
230void GPTPart::ReversePartBytes(void) {
231 ReverseBytes(&partitionType.data1, 8);
232 ReverseBytes(&partitionType.data2, 8);
233 ReverseBytes(&uniqueGUID.data1, 8);
234 ReverseBytes(&uniqueGUID.data2, 8);
235 ReverseBytes(&firstLBA, 8);
236 ReverseBytes(&lastLBA, 8);
237 ReverseBytes(&attributes, 8);
238} // GPTPart::ReverseBytes()
239
240/****************************************
241 * Functions requiring user interaction *
242 ****************************************/
243
244// Change the type code on the partition.
245void GPTPart::ChangeType(void) {
246 char line[255];
247 char* junk;
248 int typeNum = 0xFFFF;
249 GUIDData newType;
250
251 cout << "Current type is '" << GetNameType() << "'\n";
252 while ((!typeHelper.Valid(typeNum)) && (typeNum != 0)) {
253 cout << "Hex code (L to show codes, 0 to enter raw code, Enter = 0700): ";
254 junk = fgets(line, 255, stdin);
255 sscanf(line, "%X", &typeNum);
256 if ((line[0] == 'L') || (line[0] == 'l'))
257 typeHelper.ShowTypes();
258 if (line[0] == '\n') {
259 typeNum = 0x0700;
260 } // if
261 } // while
262 if (typeNum != 0) // user entered a code, so convert it
263 newType = typeHelper.IDToGUID((uint16_t) typeNum);
264 else // user wants to enter the GUID directly, so do that
265 newType = GetGUID();
266 SetType(newType);
267 cout << "Changed type of partition to '" << typeHelper.GUIDToName(partitionType) << "'\n";
268} // GPTPart::ChangeType()
269
srs5694a0eb11a2009-08-29 15:00:08 -0400270/***********************************
271 * Non-class but related functions *
272 ***********************************/
273
274// Recursive quick sort algorithm for GPT partitions. Note that if there
275// are any empties in the specified range, they'll be sorted to the
276// start, resulting in a sorted set of partitions that begins with
277// partition 2, 3, or higher.
278void QuickSortGPT(GPTPart* partitions, int start, int finish) {
279 uint64_t starterValue; // starting location of median partition
280 int left, right;
281 GPTPart temp;
282
283 left = start;
284 right = finish;
285 starterValue = partitions[(start + finish) / 2].GetFirstLBA();
286 do {
287 while (partitions[left].GetFirstLBA() < starterValue)
288 left++;
289 while (partitions[right].GetFirstLBA() > starterValue)
290 right--;
291 if (left <= right) {
292 temp = partitions[left];
293 partitions[left] = partitions[right];
294 partitions[right] = temp;
295 left++;
296 right--;
297 } // if
298 } while (left <= right);
299 if (start < right) QuickSortGPT(partitions, start, right);
300 if (finish > left) QuickSortGPT(partitions, left, finish);
301} // QuickSortGPT()