blob: 29b2df9349dba0028230cb11af74da4ff474e664 [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
srs5694a0eb11a2009-08-29 15:00:08 -040018#include <string.h>
srs5694fed16d02010-01-27 23:03:40 -050019#include <stdio.h>
20#include <iostream>
srs5694a0eb11a2009-08-29 15:00:08 -040021#include "gptpart.h"
22#include "attributes.h"
23
24using namespace std;
25
26PartTypes GPTPart::typeHelper;
27
28GPTPart::GPTPart(void) {
srs5694978041c2009-09-21 20:51:47 -040029 int i;
30
31 for (i = 0; i < NAME_SIZE; i++)
32 name[i] = '\0';
srs5694a0eb11a2009-08-29 15:00:08 -040033} // Default constructor
34
35GPTPart::~GPTPart(void) {
36} // destructor
37
srs5694a0eb11a2009-08-29 15:00:08 -040038// Return the gdisk-specific two-byte hex code for the partition
39uint16_t GPTPart::GetHexType(void) {
40 return typeHelper.GUIDToID(partitionType);
41} // GPTPart::GetHexType()
42
43// Return a plain-text description of the partition type (e.g., "Linux/Windows
44// data" or "Linux swap").
srs5694546a9c72010-01-26 16:00:26 -050045string GPTPart::GetNameType(void) {
srs5694fed16d02010-01-27 23:03:40 -050046 return typeHelper.GUIDToName(partitionType);
srs5694a0eb11a2009-08-29 15:00:08 -040047} // GPTPart::GetNameType()
48
49// Compute and return the partition's length (or 0 if the end is incorrectly
50// set before the beginning).
51uint64_t GPTPart::GetLengthLBA(void) {
52 uint64_t length = 0;
53 if (firstLBA <= lastLBA)
54 length = lastLBA - firstLBA + UINT64_C(1);
55 return length;
56} // GPTPart::GetLengthLBA()
57
srs56940a697312010-01-28 21:10:52 -050058// Return partition's name field, converted to a C++ ASCII string
59string GPTPart::GetName(void) {
60 string theName;
srs5694a0eb11a2009-08-29 15:00:08 -040061 int i;
62
srs56940a697312010-01-28 21:10:52 -050063 theName = "";
64 for (i = 0; i < NAME_SIZE; i += 2) {
65 if (name[i] != '\0')
66 theName += name[i];
67 } // for
68 return theName;
69} // GPTPart::GetName()
70
71// Set the type code to the specified one. Also changes the partition
72// name *IF* the current name is the generic one for the current partition
73// type.
74void GPTPart::SetType(struct GUIDData t) {
75 int nameSame = 1, currentLength, i;
76
77 if (GetName() == typeHelper.GUIDToName(partitionType)) {
78 SetName(typeHelper.GUIDToName(t));
79 } // if
80 partitionType = t;
81} // GPTPart::SetType()
srs5694a0eb11a2009-08-29 15:00:08 -040082
83// Sets the unique GUID to a value of 0 or a random value,
84// depending on the parameter: 0 = 0, anything else = random
85void GPTPart::SetUniqueGUID(int zeroOrRandom) {
86 if (zeroOrRandom == 0) {
87 uniqueGUID.data1 = 0;
88 uniqueGUID.data2 = 0;
89 } else {
90 // rand() is only 32 bits on 32-bit systems, so multiply together to
91 // fill a 64-bit value.
92 uniqueGUID.data1 = (uint64_t) rand() * (uint64_t) rand();
93 uniqueGUID.data2 = (uint64_t) rand() * (uint64_t) rand();
94 }
95} // GPTPart::SetUniqueGUID()
96
srs5694a0eb11a2009-08-29 15:00:08 -040097// Set the name for a partition to theName, or prompt for a name if
srs5694fed16d02010-01-27 23:03:40 -050098// theName is empty. Note that theName is a standard C++-style ASCII
srs5694a0eb11a2009-08-29 15:00:08 -040099// string, although the GUID partition definition requires a UTF-16LE
100// string. This function creates a simple-minded copy for this.
srs56940a697312010-01-28 21:10:52 -0500101void GPTPart::SetName(const string & theName) {
srs5694a0eb11a2009-08-29 15:00:08 -0400102 char newName[NAME_SIZE]; // New name
srs5694fed16d02010-01-27 23:03:40 -0500103 char *junk;
srs5694a0eb11a2009-08-29 15:00:08 -0400104 int i;
105
106 // Blank out new name string, just to be on the safe side....
107 for (i = 0; i < NAME_SIZE; i++)
108 newName[i] = '\0';
109
srs5694fed16d02010-01-27 23:03:40 -0500110 if (theName == "") { // No name specified, so get one from the user
111 cout << "Enter name: ";
srs56945d58fe02010-01-03 20:57:08 -0500112 junk = fgets(newName, NAME_SIZE / 2, stdin);
srs5694a0eb11a2009-08-29 15:00:08 -0400113
114 // Input is likely to include a newline, so remove it....
115 i = strlen(newName);
116 if (newName[i - 1] == '\n')
117 newName[i - 1] = '\0';
118 } else {
srs5694fed16d02010-01-27 23:03:40 -0500119 strcpy(newName, theName.substr(0, NAME_SIZE / 2).c_str());
srs5694a0eb11a2009-08-29 15:00:08 -0400120 } // if
121
122 // Copy the C-style ASCII string from newName into a form that the GPT
123 // table will accept....
124 for (i = 0; i < NAME_SIZE; i++) {
125 if ((i % 2) == 0) {
126 name[i] = newName[(i / 2)];
127 } else {
128 name[i] = '\0';
129 } // if/else
130 } // for
131} // GPTPart::SetName()
132
srs56940a697312010-01-28 21:10:52 -0500133GPTPart & GPTPart::operator=(const GPTPart & orig) {
134 int i;
135
136 partitionType = orig.partitionType;
137 uniqueGUID = orig.uniqueGUID;
138 firstLBA = orig.firstLBA;
139 lastLBA = orig.lastLBA;
140 attributes = orig.attributes;
141 for (i = 0; i < NAME_SIZE; i++)
142 name[i] = orig.name[i];
143 return *this;
144} // assignment operator
145
146// Display summary information; does nothing if the partition is empty.
147void GPTPart::ShowSummary(int partNum, uint32_t blockSize) {
148 string sizeInSI;
149 int i;
150
151 if (firstLBA != 0) {
152 sizeInSI = BytesToSI(blockSize * (lastLBA - firstLBA + 1));
153 cout.width(4);
154 cout << partNum + 1 << " ";
155 cout.width(14);
156 cout << firstLBA << " ";
157 cout.width(14);
158 cout << lastLBA << " ";
159 cout << BytesToSI(blockSize * (lastLBA - firstLBA + 1)) << " ";
160 for (i = 0; i < 9 - sizeInSI.length(); i++) cout << " ";
161 cout.fill('0');
162 cout.width(4);
163 cout.setf(ios::uppercase);
164 cout << hex << typeHelper.GUIDToID(partitionType) << " " << dec;
165 cout.fill(' ');
166// cout.setf(ios::right);
167 cout << GetName().substr(0, 23) << "\n";
168 cout.fill(' ');
169 } // if
170} // GPTPart::ShowSummary()
171
172// Show detailed partition information. Does nothing if the partition is
173// empty (as determined by firstLBA being 0).
174void GPTPart::ShowDetails(uint32_t blockSize) {
175 uint64_t size;
176
177 if (firstLBA != 0) {
178 cout << "Partition GUID code: " << GUIDToStr(partitionType);
179 cout << " (" << typeHelper.GUIDToName(partitionType) << ")\n";
180 cout << "Partition unique GUID: " << GUIDToStr(uniqueGUID) << "\n";
181
182 cout << "First sector: " << firstLBA << " (at "
183 << BytesToSI(firstLBA * blockSize) << ")\n";
184 cout << "Last sector: " << lastLBA << " (at "
185 << BytesToSI(lastLBA * blockSize) << ")\n";
186 size = (lastLBA - firstLBA + 1);
187 cout << "Partition size: " << size << " sectors ("
188 << BytesToSI(size * ((uint64_t) blockSize)) << ")\n";
189 cout << "Attribute flags: ";
190 cout.fill('0');
191 cout.width(16);
192 cout << hex;
193 cout << attributes << "\n";
194 cout << dec;
195 cout << "Partition name: " << GetName() << "\n";
196 cout.fill(' ');
197 } // if
198} // GPTPart::ShowDetails()
199
200// Blank (delete) a single partition
201void GPTPart::BlankPartition(void) {
202 int j;
203 GUIDData zeroGUID;
204
205 zeroGUID.data1 = 0;
206 zeroGUID.data2 = 0;
207 uniqueGUID = zeroGUID;
208 partitionType = zeroGUID;
209 firstLBA = 0;
210 lastLBA = 0;
211 attributes = 0;
212 for (j = 0; j < NAME_SIZE; j++)
213 name[j] = '\0';
214} // GPTPart::BlankPartition
215
216// Returns 1 if the two partitions overlap, 0 if they don't
217int GPTPart::DoTheyOverlap(const GPTPart & other) {
218 int theyDo = 0;
219
220 // Don't bother checking unless these are defined (both start and end points
221 // are 0 for undefined partitions, so just check the start points)
222 if ((firstLBA != 0) && (other.firstLBA != 0)) {
223 if ((firstLBA < other.lastLBA) && (lastLBA >= other.firstLBA))
224 theyDo = 1;
225 if ((other.firstLBA < lastLBA) && (other.lastLBA >= firstLBA))
226 theyDo = 1;
227 } // if
228 return (theyDo);
229} // GPTPart::DoTheyOverlap()
230
231// Reverse the bytes of integral data types; used on big-endian systems.
232void GPTPart::ReversePartBytes(void) {
233 ReverseBytes(&partitionType.data1, 8);
234 ReverseBytes(&partitionType.data2, 8);
235 ReverseBytes(&uniqueGUID.data1, 8);
236 ReverseBytes(&uniqueGUID.data2, 8);
237 ReverseBytes(&firstLBA, 8);
238 ReverseBytes(&lastLBA, 8);
239 ReverseBytes(&attributes, 8);
240} // GPTPart::ReverseBytes()
241
242/****************************************
243 * Functions requiring user interaction *
244 ****************************************/
245
246// Change the type code on the partition.
247void GPTPart::ChangeType(void) {
248 char line[255];
249 char* junk;
250 int typeNum = 0xFFFF;
251 GUIDData newType;
252
253 cout << "Current type is '" << GetNameType() << "'\n";
254 while ((!typeHelper.Valid(typeNum)) && (typeNum != 0)) {
255 cout << "Hex code (L to show codes, 0 to enter raw code, Enter = 0700): ";
256 junk = fgets(line, 255, stdin);
257 sscanf(line, "%X", &typeNum);
258 if ((line[0] == 'L') || (line[0] == 'l'))
259 typeHelper.ShowTypes();
260 if (line[0] == '\n') {
261 typeNum = 0x0700;
262 } // if
263 } // while
264 if (typeNum != 0) // user entered a code, so convert it
265 newType = typeHelper.IDToGUID((uint16_t) typeNum);
266 else // user wants to enter the GUID directly, so do that
267 newType = GetGUID();
268 SetType(newType);
269 cout << "Changed type of partition to '" << typeHelper.GUIDToName(partitionType) << "'\n";
270} // GPTPart::ChangeType()
271
srs5694a0eb11a2009-08-29 15:00:08 -0400272/***********************************
273 * Non-class but related functions *
274 ***********************************/
275
276// Recursive quick sort algorithm for GPT partitions. Note that if there
277// are any empties in the specified range, they'll be sorted to the
278// start, resulting in a sorted set of partitions that begins with
279// partition 2, 3, or higher.
280void QuickSortGPT(GPTPart* partitions, int start, int finish) {
281 uint64_t starterValue; // starting location of median partition
282 int left, right;
283 GPTPart temp;
284
285 left = start;
286 right = finish;
287 starterValue = partitions[(start + finish) / 2].GetFirstLBA();
288 do {
289 while (partitions[left].GetFirstLBA() < starterValue)
290 left++;
291 while (partitions[right].GetFirstLBA() > starterValue)
292 right--;
293 if (left <= right) {
294 temp = partitions[left];
295 partitions[left] = partitions[right];
296 partitions[right] = temp;
297 left++;
298 right--;
299 } // if
300 } while (left <= right);
301 if (start < right) QuickSortGPT(partitions, start, right);
302 if (finish > left) QuickSortGPT(partitions, left, finish);
303} // QuickSortGPT()