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