2
// C++ Implementation: gptpart
4
// Description: Class to implement a SINGLE GPT partition
7
// Author: Rod Smith <rodsmith@rodsbooks.com>, (C) 2009
9
// Copyright: See COPYING file that comes with this distribution
12
// 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.
15
#define __STDC_LIMIT_MACROS
16
#define __STDC_CONSTANT_MACROS
21
#include "attributes.h"
25
PartTypes GPTPart::typeHelper;
27
GPTPart::GPTPart(void) {
30
for (i = 0; i < NAME_SIZE; i++)
32
} // Default constructor
34
GPTPart::~GPTPart(void) {
37
// Return partition's name field
38
unsigned char* GPTPart::GetName(unsigned char* ref) {
40
ref = (unsigned char*) malloc(NAME_SIZE * sizeof (unsigned char));
41
strcpy((char*) ref, (char*) name);
43
} // GPTPart::GetName()
45
// Return the gdisk-specific two-byte hex code for the partition
46
uint16_t GPTPart::GetHexType(void) {
47
return typeHelper.GUIDToID(partitionType);
48
} // GPTPart::GetHexType()
50
// Return a plain-text description of the partition type (e.g., "Linux/Windows
51
// data" or "Linux swap").
52
char* GPTPart::GetNameType(char* theName) {
53
return typeHelper.GUIDToName(partitionType, theName);
54
} // GPTPart::GetNameType()
56
// Compute and return the partition's length (or 0 if the end is incorrectly
57
// set before the beginning).
58
uint64_t GPTPart::GetLengthLBA(void) {
60
if (firstLBA <= lastLBA)
61
length = lastLBA - firstLBA + UINT64_C(1);
63
} // GPTPart::GetLengthLBA()
65
GPTPart & GPTPart::operator=(const GPTPart & orig) {
68
partitionType = orig.partitionType;
69
uniqueGUID = orig.uniqueGUID;
70
firstLBA = orig.firstLBA;
71
lastLBA = orig.lastLBA;
72
attributes = orig.attributes;
73
for (i = 0; i < NAME_SIZE; i++)
74
name[i] = orig.name[i];
76
} // assignment operator
78
// Sets the unique GUID to a value of 0 or a random value,
79
// depending on the parameter: 0 = 0, anything else = random
80
void GPTPart::SetUniqueGUID(int zeroOrRandom) {
81
if (zeroOrRandom == 0) {
85
// rand() is only 32 bits on 32-bit systems, so multiply together to
86
// fill a 64-bit value.
87
uniqueGUID.data1 = (uint64_t) rand() * (uint64_t) rand();
88
uniqueGUID.data2 = (uint64_t) rand() * (uint64_t) rand();
90
} // GPTPart::SetUniqueGUID()
92
// Blank (delete) a single partition
93
void GPTPart::BlankPartition(void) {
99
uniqueGUID = zeroGUID;
100
partitionType = zeroGUID;
104
for (j = 0; j < NAME_SIZE; j++)
106
} // GPTPart::BlankPartition
108
// Returns 1 if the two partitions overlap, 0 if they don't
109
int GPTPart::DoTheyOverlap(GPTPart* other) {
112
// Don't bother checking unless these are defined (both start and end points
113
// are 0 for undefined partitions, so just check the start points)
114
if ((firstLBA != 0) && (other->firstLBA != 0)) {
115
if ((firstLBA < other->lastLBA) && (lastLBA >= other->firstLBA))
117
if ((other->firstLBA < lastLBA) && (other->lastLBA >= firstLBA))
121
} // GPTPart::DoTheyOverlap()
123
// Reverse the bytes of integral data types; used on big-endian systems.
124
void GPTPart::ReversePartBytes(void) {
125
ReverseBytes(&partitionType.data1, 8);
126
ReverseBytes(&partitionType.data2, 8);
127
ReverseBytes(&uniqueGUID.data1, 8);
128
ReverseBytes(&uniqueGUID.data2, 8);
129
ReverseBytes(&firstLBA, 8);
130
ReverseBytes(&lastLBA, 8);
131
ReverseBytes(&attributes, 8);
132
} // GPTPart::ReverseBytes()
134
// Display summary information; does nothing if the partition is empty.
135
void GPTPart::ShowSummary(int partNum, uint32_t blockSize) {
140
BytesToSI(blockSize * (lastLBA - firstLBA + 1), sizeInSI);
141
printf("%4d %14lu %14lu", partNum + 1, (unsigned long) firstLBA,
142
(unsigned long) lastLBA);
143
printf(" %-10s %04X ", sizeInSI,
144
typeHelper.GUIDToID(partitionType));
145
while ((name[j] != '\0') && (j < 44)) {
146
printf("%c", name[j]);
151
} // GPTPart::ShowSummary()
153
// Show detailed partition information. Does nothing if the partition is
154
// empty (as determined by firstLBA being 0).
155
void GPTPart::ShowDetails(uint32_t blockSize) {
161
printf("Partition GUID code: %s ", GUIDToStr(partitionType, temp));
162
printf("(%s)\n", typeHelper.GUIDToName(partitionType, temp));
163
printf("Partition unique GUID: %s\n", GUIDToStr(uniqueGUID, temp));
165
printf("First sector: %llu (at %s)\n", (unsigned long long) firstLBA,
166
BytesToSI(firstLBA * blockSize, temp));
167
printf("Last sector: %llu (at %s)\n", (unsigned long long) lastLBA,
168
BytesToSI(lastLBA * blockSize, temp));
169
size = (lastLBA - firstLBA + 1);
170
printf("Partition size: %llu sectors (%s)\n", (unsigned long long)
171
size, BytesToSI(size * ((uint64_t) blockSize), temp));
172
printf("Attribute flags: %016llx\n", (unsigned long long) attributes);
173
printf("Partition name: ");
175
while ((name[i] != '\0') && (i < NAME_SIZE)) {
176
printf("%c", name[i]);
181
} // GPTPart::ShowDetails()
183
/****************************************
184
* Functions requiring user interaction *
185
****************************************/
187
// Change the type code on the partition.
188
void GPTPart::ChangeType(void) {
189
char typeName[255], line[255];
190
int typeNum = 0xFFFF;
191
// uint16_t typeNum = 0xFFFF;
194
printf("Current type is '%s'\n", GetNameType(line));
195
// printf("Current type is '%s'\n", typeHelper.GUIDToName(partitionType, typeName));
196
while ((!typeHelper.Valid(typeNum)) && (typeNum != 0)) {
197
printf("Hex code (L to show codes, 0 to enter raw code): ");
198
fgets(line, 255, stdin);
199
sscanf(line, "%X", &typeNum);
200
if ((line[0] == 'L') || (line[0] == 'l'))
201
typeHelper.ShowTypes();
203
if (typeNum != 0) // user entered a code, so convert it
204
newType = typeHelper.IDToGUID((uint16_t) typeNum);
205
else // user wants to enter the GUID directly, so do that
207
partitionType = newType;
208
printf("Changed system type of partition to '%s'\n",
209
typeHelper.GUIDToName(partitionType, typeName));
210
} // GPTPart::ChangeType()
212
// Set the name for a partition to theName, or prompt for a name if
213
// theName is a NULL pointer. Note that theName is a standard C-style
214
// string, although the GUID partition definition requires a UTF-16LE
215
// string. This function creates a simple-minded copy for this.
216
void GPTPart::SetName(unsigned char* theName) {
217
char newName[NAME_SIZE]; // New name
220
// Blank out new name string, just to be on the safe side....
221
for (i = 0; i < NAME_SIZE; i++)
224
if (theName == NULL) { // No name specified, so get one from the user
225
printf("Enter name: ");
226
fgets(newName, NAME_SIZE / 2, stdin);
228
// Input is likely to include a newline, so remove it....
230
if (newName[i - 1] == '\n')
231
newName[i - 1] = '\0';
233
strcpy(newName, (char*) theName);
236
// Copy the C-style ASCII string from newName into a form that the GPT
237
// table will accept....
238
for (i = 0; i < NAME_SIZE; i++) {
240
name[i] = newName[(i / 2)];
245
} // GPTPart::SetName()
247
/***********************************
248
* Non-class but related functions *
249
***********************************/
251
// Recursive quick sort algorithm for GPT partitions. Note that if there
252
// are any empties in the specified range, they'll be sorted to the
253
// start, resulting in a sorted set of partitions that begins with
254
// partition 2, 3, or higher.
255
void QuickSortGPT(GPTPart* partitions, int start, int finish) {
256
uint64_t starterValue; // starting location of median partition
262
starterValue = partitions[(start + finish) / 2].GetFirstLBA();
264
while (partitions[left].GetFirstLBA() < starterValue)
266
while (partitions[right].GetFirstLBA() > starterValue)
269
temp = partitions[left];
270
partitions[left] = partitions[right];
271
partitions[right] = temp;
275
} while (left <= right);
276
if (start < right) QuickSortGPT(partitions, start, right);
277
if (finish > left) QuickSortGPT(partitions, left, finish);