blob: 076f11567fabe4a01e547ce0deae96f9a6bd70d2 [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
18#include <stdio.h>
19#include <string.h>
20#include "gptpart.h"
21#include "attributes.h"
22
23using namespace std;
24
25PartTypes GPTPart::typeHelper;
26
27GPTPart::GPTPart(void) {
srs5694978041c2009-09-21 20:51:47 -040028 int i;
29
30 for (i = 0; i < NAME_SIZE; i++)
31 name[i] = '\0';
srs5694a0eb11a2009-08-29 15:00:08 -040032} // Default constructor
33
34GPTPart::~GPTPart(void) {
35} // destructor
36
37// Return partition's name field
srs5694546a9c72010-01-26 16:00:26 -050038string GPTPart::GetName(void) {
39 string theName;
40
41/* if (ref == NULL)
srs5694a0eb11a2009-08-29 15:00:08 -040042 ref = (unsigned char*) malloc(NAME_SIZE * sizeof (unsigned char));
srs5694546a9c72010-01-26 16:00:26 -050043 strcpy((char*) ref, (char*) name); */
44 theName = (const char*) name;
45 return theName;
srs5694a0eb11a2009-08-29 15:00:08 -040046} // GPTPart::GetName()
47
48// Return the gdisk-specific two-byte hex code for the partition
49uint16_t GPTPart::GetHexType(void) {
50 return typeHelper.GUIDToID(partitionType);
51} // GPTPart::GetHexType()
52
53// Return a plain-text description of the partition type (e.g., "Linux/Windows
54// data" or "Linux swap").
srs5694546a9c72010-01-26 16:00:26 -050055string GPTPart::GetNameType(void) {
56 string temp;
57 char theName[255];
58
59 temp = typeHelper.GUIDToName(partitionType, theName);
60
61 return temp;
srs5694a0eb11a2009-08-29 15:00:08 -040062} // GPTPart::GetNameType()
63
64// Compute and return the partition's length (or 0 if the end is incorrectly
65// set before the beginning).
66uint64_t GPTPart::GetLengthLBA(void) {
67 uint64_t length = 0;
68 if (firstLBA <= lastLBA)
69 length = lastLBA - firstLBA + UINT64_C(1);
70 return length;
71} // GPTPart::GetLengthLBA()
72
73GPTPart & GPTPart::operator=(const GPTPart & orig) {
74 int i;
75
76 partitionType = orig.partitionType;
77 uniqueGUID = orig.uniqueGUID;
78 firstLBA = orig.firstLBA;
79 lastLBA = orig.lastLBA;
80 attributes = orig.attributes;
81 for (i = 0; i < NAME_SIZE; i++)
82 name[i] = orig.name[i];
srs5694e35eb1b2009-09-14 00:29:34 -040083 return *this;
srs5694a0eb11a2009-08-29 15:00:08 -040084} // assignment operator
85
86// Sets the unique GUID to a value of 0 or a random value,
87// depending on the parameter: 0 = 0, anything else = random
88void GPTPart::SetUniqueGUID(int zeroOrRandom) {
89 if (zeroOrRandom == 0) {
90 uniqueGUID.data1 = 0;
91 uniqueGUID.data2 = 0;
92 } else {
93 // rand() is only 32 bits on 32-bit systems, so multiply together to
94 // fill a 64-bit value.
95 uniqueGUID.data1 = (uint64_t) rand() * (uint64_t) rand();
96 uniqueGUID.data2 = (uint64_t) rand() * (uint64_t) rand();
97 }
98} // GPTPart::SetUniqueGUID()
99
100// Blank (delete) a single partition
101void GPTPart::BlankPartition(void) {
102 int j;
103 GUIDData zeroGUID;
104
105 zeroGUID.data1 = 0;
106 zeroGUID.data2 = 0;
107 uniqueGUID = zeroGUID;
108 partitionType = zeroGUID;
109 firstLBA = 0;
110 lastLBA = 0;
111 attributes = 0;
112 for (j = 0; j < NAME_SIZE; j++)
113 name[j] = '\0';
114} // GPTPart::BlankPartition
115
116// Returns 1 if the two partitions overlap, 0 if they don't
117int GPTPart::DoTheyOverlap(GPTPart* other) {
118 int theyDo = 0;
119
120 // Don't bother checking unless these are defined (both start and end points
121 // are 0 for undefined partitions, so just check the start points)
122 if ((firstLBA != 0) && (other->firstLBA != 0)) {
123 if ((firstLBA < other->lastLBA) && (lastLBA >= other->firstLBA))
124 theyDo = 1;
125 if ((other->firstLBA < lastLBA) && (other->lastLBA >= firstLBA))
126 theyDo = 1;
127 } // if
128 return (theyDo);
129} // GPTPart::DoTheyOverlap()
130
131// Reverse the bytes of integral data types; used on big-endian systems.
132void GPTPart::ReversePartBytes(void) {
133 ReverseBytes(&partitionType.data1, 8);
134 ReverseBytes(&partitionType.data2, 8);
135 ReverseBytes(&uniqueGUID.data1, 8);
136 ReverseBytes(&uniqueGUID.data2, 8);
137 ReverseBytes(&firstLBA, 8);
138 ReverseBytes(&lastLBA, 8);
139 ReverseBytes(&attributes, 8);
140} // GPTPart::ReverseBytes()
141
142// Display summary information; does nothing if the partition is empty.
srs5694978041c2009-09-21 20:51:47 -0400143void GPTPart::ShowSummary(int partNum, uint32_t blockSize) {
144 char sizeInSI[255];
145 int j = 0;
srs5694a0eb11a2009-08-29 15:00:08 -0400146
147 if (firstLBA != 0) {
148 BytesToSI(blockSize * (lastLBA - firstLBA + 1), sizeInSI);
srs5694978041c2009-09-21 20:51:47 -0400149 printf("%4d %14lu %14lu", partNum + 1, (unsigned long) firstLBA,
srs5694a0eb11a2009-08-29 15:00:08 -0400150 (unsigned long) lastLBA);
151 printf(" %-10s %04X ", sizeInSI,
152 typeHelper.GUIDToID(partitionType));
srs5694a0eb11a2009-08-29 15:00:08 -0400153 while ((name[j] != '\0') && (j < 44)) {
154 printf("%c", name[j]);
155 j += 2;
156 } // while
157 printf("\n");
158 } // if
159} // GPTPart::ShowSummary()
160
161// Show detailed partition information. Does nothing if the partition is
162// empty (as determined by firstLBA being 0).
163void GPTPart::ShowDetails(uint32_t blockSize) {
164 char temp[255];
165 int i;
166 uint64_t size;
167
168 if (firstLBA != 0) {
169 printf("Partition GUID code: %s ", GUIDToStr(partitionType, temp));
170 printf("(%s)\n", typeHelper.GUIDToName(partitionType, temp));
171 printf("Partition unique GUID: %s\n", GUIDToStr(uniqueGUID, temp));
172
srs5694546a9c72010-01-26 16:00:26 -0500173 printf("First sector: %llu ", firstLBA);
174 printf("(at %s)\n", BytesToSI(firstLBA * blockSize, temp));
175 printf("Last sector: %llu ", (unsigned long long) lastLBA);
176 printf("(at %s)\n", BytesToSI(lastLBA * blockSize, temp));
srs5694a0eb11a2009-08-29 15:00:08 -0400177 size = (lastLBA - firstLBA + 1);
srs5694546a9c72010-01-26 16:00:26 -0500178 printf("Partition size: %llu sectors ", (unsigned long long) size);
179 printf("(%s)\n", BytesToSI(size * ((uint64_t) blockSize), temp));
srs5694a0eb11a2009-08-29 15:00:08 -0400180 printf("Attribute flags: %016llx\n", (unsigned long long) attributes);
181 printf("Partition name: ");
182 i = 0;
183 while ((name[i] != '\0') && (i < NAME_SIZE)) {
184 printf("%c", name[i]);
185 i += 2;
186 } // while
187 printf("\n");
188 } // if
189} // GPTPart::ShowDetails()
190
191/****************************************
192 * Functions requiring user interaction *
193 ****************************************/
194
195// Change the type code on the partition.
196void GPTPart::ChangeType(void) {
197 char typeName[255], line[255];
srs56945d58fe02010-01-03 20:57:08 -0500198 char* junk;
srs5694a0eb11a2009-08-29 15:00:08 -0400199 int typeNum = 0xFFFF;
srs5694a0eb11a2009-08-29 15:00:08 -0400200 GUIDData newType;
201
srs5694546a9c72010-01-26 16:00:26 -0500202 printf("Current type is '%s'\n", GetNameType().c_str());
srs5694a0eb11a2009-08-29 15:00:08 -0400203 while ((!typeHelper.Valid(typeNum)) && (typeNum != 0)) {
srs5694546a9c72010-01-26 16:00:26 -0500204 printf("Hex code (L to show codes, 0 to enter raw code, Enter = 0700): ");
srs56945d58fe02010-01-03 20:57:08 -0500205 junk = fgets(line, 255, stdin);
srs5694a0eb11a2009-08-29 15:00:08 -0400206 sscanf(line, "%X", &typeNum);
207 if ((line[0] == 'L') || (line[0] == 'l'))
208 typeHelper.ShowTypes();
srs5694546a9c72010-01-26 16:00:26 -0500209 if (line[0] == '\n') {
210 typeNum = 0x0700;
211 } // if
srs5694a0eb11a2009-08-29 15:00:08 -0400212 } // while
213 if (typeNum != 0) // user entered a code, so convert it
214 newType = typeHelper.IDToGUID((uint16_t) typeNum);
215 else // user wants to enter the GUID directly, so do that
216 newType = GetGUID();
217 partitionType = newType;
srs5694ba00fed2010-01-12 18:18:36 -0500218 printf("Changed type of partition to '%s'\n",
srs5694a0eb11a2009-08-29 15:00:08 -0400219 typeHelper.GUIDToName(partitionType, typeName));
220} // GPTPart::ChangeType()
221
222// Set the name for a partition to theName, or prompt for a name if
223// theName is a NULL pointer. Note that theName is a standard C-style
224// string, although the GUID partition definition requires a UTF-16LE
225// string. This function creates a simple-minded copy for this.
226void GPTPart::SetName(unsigned char* theName) {
227 char newName[NAME_SIZE]; // New name
srs56945d58fe02010-01-03 20:57:08 -0500228 char* junk;
srs5694a0eb11a2009-08-29 15:00:08 -0400229 int i;
230
231 // Blank out new name string, just to be on the safe side....
232 for (i = 0; i < NAME_SIZE; i++)
233 newName[i] = '\0';
234
235 if (theName == NULL) { // No name specified, so get one from the user
236 printf("Enter name: ");
srs56945d58fe02010-01-03 20:57:08 -0500237 junk = fgets(newName, NAME_SIZE / 2, stdin);
srs5694a0eb11a2009-08-29 15:00:08 -0400238
239 // Input is likely to include a newline, so remove it....
240 i = strlen(newName);
241 if (newName[i - 1] == '\n')
242 newName[i - 1] = '\0';
243 } else {
244 strcpy(newName, (char*) theName);
245 } // if
246
247 // Copy the C-style ASCII string from newName into a form that the GPT
248 // table will accept....
249 for (i = 0; i < NAME_SIZE; i++) {
250 if ((i % 2) == 0) {
251 name[i] = newName[(i / 2)];
252 } else {
253 name[i] = '\0';
254 } // if/else
255 } // for
256} // GPTPart::SetName()
257
258/***********************************
259 * Non-class but related functions *
260 ***********************************/
261
262// Recursive quick sort algorithm for GPT partitions. Note that if there
263// are any empties in the specified range, they'll be sorted to the
264// start, resulting in a sorted set of partitions that begins with
265// partition 2, 3, or higher.
266void QuickSortGPT(GPTPart* partitions, int start, int finish) {
267 uint64_t starterValue; // starting location of median partition
268 int left, right;
269 GPTPart temp;
270
271 left = start;
272 right = finish;
273 starterValue = partitions[(start + finish) / 2].GetFirstLBA();
274 do {
275 while (partitions[left].GetFirstLBA() < starterValue)
276 left++;
277 while (partitions[right].GetFirstLBA() > starterValue)
278 right--;
279 if (left <= right) {
280 temp = partitions[left];
281 partitions[left] = partitions[right];
282 partitions[right] = temp;
283 left++;
284 right--;
285 } // if
286 } while (left <= right);
287 if (start < right) QuickSortGPT(partitions, start, right);
288 if (finish > left) QuickSortGPT(partitions, left, finish);
289} // QuickSortGPT()