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