blob: fd24dfaba2e609e261a85c1959b9e8024eb398ee [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
38unsigned char* GPTPart::GetName(unsigned char* ref) {
39 if (ref == NULL)
40 ref = (unsigned char*) malloc(NAME_SIZE * sizeof (unsigned char));
41 strcpy((char*) ref, (char*) name);
42 return ref;
43} // GPTPart::GetName()
44
45// Return the gdisk-specific two-byte hex code for the partition
46uint16_t GPTPart::GetHexType(void) {
47 return typeHelper.GUIDToID(partitionType);
48} // GPTPart::GetHexType()
49
50// Return a plain-text description of the partition type (e.g., "Linux/Windows
51// data" or "Linux swap").
52char* GPTPart::GetNameType(char* theName) {
53 return typeHelper.GUIDToName(partitionType, theName);
54} // GPTPart::GetNameType()
55
56// Compute and return the partition's length (or 0 if the end is incorrectly
57// set before the beginning).
58uint64_t GPTPart::GetLengthLBA(void) {
59 uint64_t length = 0;
60 if (firstLBA <= lastLBA)
61 length = lastLBA - firstLBA + UINT64_C(1);
62 return length;
63} // GPTPart::GetLengthLBA()
64
65GPTPart & GPTPart::operator=(const GPTPart & orig) {
66 int i;
67
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];
srs5694e35eb1b2009-09-14 00:29:34 -040075 return *this;
srs5694a0eb11a2009-08-29 15:00:08 -040076} // assignment operator
77
78// Sets the unique GUID to a value of 0 or a random value,
79// depending on the parameter: 0 = 0, anything else = random
80void GPTPart::SetUniqueGUID(int zeroOrRandom) {
81 if (zeroOrRandom == 0) {
82 uniqueGUID.data1 = 0;
83 uniqueGUID.data2 = 0;
84 } else {
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();
89 }
90} // GPTPart::SetUniqueGUID()
91
92// Blank (delete) a single partition
93void GPTPart::BlankPartition(void) {
94 int j;
95 GUIDData zeroGUID;
96
97 zeroGUID.data1 = 0;
98 zeroGUID.data2 = 0;
99 uniqueGUID = zeroGUID;
100 partitionType = zeroGUID;
101 firstLBA = 0;
102 lastLBA = 0;
103 attributes = 0;
104 for (j = 0; j < NAME_SIZE; j++)
105 name[j] = '\0';
106} // GPTPart::BlankPartition
107
108// Returns 1 if the two partitions overlap, 0 if they don't
109int GPTPart::DoTheyOverlap(GPTPart* other) {
110 int theyDo = 0;
111
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))
116 theyDo = 1;
117 if ((other->firstLBA < lastLBA) && (other->lastLBA >= firstLBA))
118 theyDo = 1;
119 } // if
120 return (theyDo);
121} // GPTPart::DoTheyOverlap()
122
123// Reverse the bytes of integral data types; used on big-endian systems.
124void 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()
133
134// Display summary information; does nothing if the partition is empty.
srs5694978041c2009-09-21 20:51:47 -0400135void GPTPart::ShowSummary(int partNum, uint32_t blockSize) {
136 char sizeInSI[255];
137 int j = 0;
srs5694a0eb11a2009-08-29 15:00:08 -0400138
139 if (firstLBA != 0) {
140 BytesToSI(blockSize * (lastLBA - firstLBA + 1), sizeInSI);
srs5694978041c2009-09-21 20:51:47 -0400141 printf("%4d %14lu %14lu", partNum + 1, (unsigned long) firstLBA,
srs5694a0eb11a2009-08-29 15:00:08 -0400142 (unsigned long) lastLBA);
143 printf(" %-10s %04X ", sizeInSI,
144 typeHelper.GUIDToID(partitionType));
srs5694a0eb11a2009-08-29 15:00:08 -0400145 while ((name[j] != '\0') && (j < 44)) {
146 printf("%c", name[j]);
147 j += 2;
148 } // while
149 printf("\n");
150 } // if
151} // GPTPart::ShowSummary()
152
153// Show detailed partition information. Does nothing if the partition is
154// empty (as determined by firstLBA being 0).
155void GPTPart::ShowDetails(uint32_t blockSize) {
156 char temp[255];
157 int i;
158 uint64_t size;
159
160 if (firstLBA != 0) {
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));
164
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: ");
174 i = 0;
175 while ((name[i] != '\0') && (i < NAME_SIZE)) {
176 printf("%c", name[i]);
177 i += 2;
178 } // while
179 printf("\n");
180 } // if
181} // GPTPart::ShowDetails()
182
183/****************************************
184 * Functions requiring user interaction *
185 ****************************************/
186
187// Change the type code on the partition.
188void GPTPart::ChangeType(void) {
189 char typeName[255], line[255];
srs56945d58fe02010-01-03 20:57:08 -0500190 char* junk;
srs5694a0eb11a2009-08-29 15:00:08 -0400191 int typeNum = 0xFFFF;
192// uint16_t typeNum = 0xFFFF;
193 GUIDData newType;
194
195 printf("Current type is '%s'\n", GetNameType(line));
196// printf("Current type is '%s'\n", typeHelper.GUIDToName(partitionType, typeName));
197 while ((!typeHelper.Valid(typeNum)) && (typeNum != 0)) {
198 printf("Hex code (L to show codes, 0 to enter raw code): ");
srs56945d58fe02010-01-03 20:57:08 -0500199 junk = fgets(line, 255, stdin);
srs5694a0eb11a2009-08-29 15:00:08 -0400200 sscanf(line, "%X", &typeNum);
201 if ((line[0] == 'L') || (line[0] == 'l'))
202 typeHelper.ShowTypes();
203 } // while
204 if (typeNum != 0) // user entered a code, so convert it
205 newType = typeHelper.IDToGUID((uint16_t) typeNum);
206 else // user wants to enter the GUID directly, so do that
207 newType = GetGUID();
208 partitionType = newType;
209 printf("Changed system type of partition to '%s'\n",
210 typeHelper.GUIDToName(partitionType, typeName));
211} // GPTPart::ChangeType()
212
213// Set the name for a partition to theName, or prompt for a name if
214// theName is a NULL pointer. Note that theName is a standard C-style
215// string, although the GUID partition definition requires a UTF-16LE
216// string. This function creates a simple-minded copy for this.
217void GPTPart::SetName(unsigned char* theName) {
218 char newName[NAME_SIZE]; // New name
srs56945d58fe02010-01-03 20:57:08 -0500219 char* junk;
srs5694a0eb11a2009-08-29 15:00:08 -0400220 int i;
221
222 // Blank out new name string, just to be on the safe side....
223 for (i = 0; i < NAME_SIZE; i++)
224 newName[i] = '\0';
225
226 if (theName == NULL) { // No name specified, so get one from the user
227 printf("Enter name: ");
srs56945d58fe02010-01-03 20:57:08 -0500228 junk = fgets(newName, NAME_SIZE / 2, stdin);
srs5694a0eb11a2009-08-29 15:00:08 -0400229
230 // Input is likely to include a newline, so remove it....
231 i = strlen(newName);
232 if (newName[i - 1] == '\n')
233 newName[i - 1] = '\0';
234 } else {
235 strcpy(newName, (char*) theName);
236 } // if
237
238 // Copy the C-style ASCII string from newName into a form that the GPT
239 // table will accept....
240 for (i = 0; i < NAME_SIZE; i++) {
241 if ((i % 2) == 0) {
242 name[i] = newName[(i / 2)];
243 } else {
244 name[i] = '\0';
245 } // if/else
246 } // for
247} // GPTPart::SetName()
248
249/***********************************
250 * Non-class but related functions *
251 ***********************************/
252
253// Recursive quick sort algorithm for GPT partitions. Note that if there
254// are any empties in the specified range, they'll be sorted to the
255// start, resulting in a sorted set of partitions that begins with
256// partition 2, 3, or higher.
257void QuickSortGPT(GPTPart* partitions, int start, int finish) {
258 uint64_t starterValue; // starting location of median partition
259 int left, right;
260 GPTPart temp;
261
262 left = start;
263 right = finish;
264 starterValue = partitions[(start + finish) / 2].GetFirstLBA();
265 do {
266 while (partitions[left].GetFirstLBA() < starterValue)
267 left++;
268 while (partitions[right].GetFirstLBA() > starterValue)
269 right--;
270 if (left <= right) {
271 temp = partitions[left];
272 partitions[left] = partitions[right];
273 partitions[right] = temp;
274 left++;
275 right--;
276 } // if
277 } while (left <= right);
278 if (start < right) QuickSortGPT(partitions, start, right);
279 if (finish > left) QuickSortGPT(partitions, left, finish);
280} // QuickSortGPT()
281