blob: c6504c11fc2744f91b0890bc871a6aa576b06c87 [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
srs5694fed16d02010-01-27 23:03:40 -050038// Return partition's name field, converted to a C++ ASCII string
srs5694546a9c72010-01-26 16:00:26 -050039string GPTPart::GetName(void) {
40 string theName;
srs5694fed16d02010-01-27 23:03:40 -050041 int i;
srs5694546a9c72010-01-26 16:00:26 -050042
srs5694fed16d02010-01-27 23:03:40 -050043 for (i = 0; i < NAME_SIZE; i += 2) {
44 theName += name[i];
45 } // for
srs5694546a9c72010-01-26 16:00:26 -050046 return theName;
srs5694a0eb11a2009-08-29 15:00:08 -040047} // GPTPart::GetName()
48
49// Return the gdisk-specific two-byte hex code for the partition
50uint16_t GPTPart::GetHexType(void) {
51 return typeHelper.GUIDToID(partitionType);
52} // GPTPart::GetHexType()
53
54// Return a plain-text description of the partition type (e.g., "Linux/Windows
55// data" or "Linux swap").
srs5694546a9c72010-01-26 16:00:26 -050056string GPTPart::GetNameType(void) {
srs5694fed16d02010-01-27 23:03:40 -050057 return typeHelper.GUIDToName(partitionType);
srs5694a0eb11a2009-08-29 15:00:08 -040058} // GPTPart::GetNameType()
59
60// Compute and return the partition's length (or 0 if the end is incorrectly
61// set before the beginning).
62uint64_t GPTPart::GetLengthLBA(void) {
63 uint64_t length = 0;
64 if (firstLBA <= lastLBA)
65 length = lastLBA - firstLBA + UINT64_C(1);
66 return length;
67} // GPTPart::GetLengthLBA()
68
69GPTPart & GPTPart::operator=(const GPTPart & orig) {
70 int i;
71
72 partitionType = orig.partitionType;
73 uniqueGUID = orig.uniqueGUID;
74 firstLBA = orig.firstLBA;
75 lastLBA = orig.lastLBA;
76 attributes = orig.attributes;
77 for (i = 0; i < NAME_SIZE; i++)
78 name[i] = orig.name[i];
srs5694e35eb1b2009-09-14 00:29:34 -040079 return *this;
srs5694a0eb11a2009-08-29 15:00:08 -040080} // assignment operator
81
82// Sets the unique GUID to a value of 0 or a random value,
83// depending on the parameter: 0 = 0, anything else = random
84void GPTPart::SetUniqueGUID(int zeroOrRandom) {
85 if (zeroOrRandom == 0) {
86 uniqueGUID.data1 = 0;
87 uniqueGUID.data2 = 0;
88 } else {
89 // rand() is only 32 bits on 32-bit systems, so multiply together to
90 // fill a 64-bit value.
91 uniqueGUID.data1 = (uint64_t) rand() * (uint64_t) rand();
92 uniqueGUID.data2 = (uint64_t) rand() * (uint64_t) rand();
93 }
94} // GPTPart::SetUniqueGUID()
95
96// Blank (delete) a single partition
97void GPTPart::BlankPartition(void) {
98 int j;
99 GUIDData zeroGUID;
100
101 zeroGUID.data1 = 0;
102 zeroGUID.data2 = 0;
103 uniqueGUID = zeroGUID;
104 partitionType = zeroGUID;
105 firstLBA = 0;
106 lastLBA = 0;
107 attributes = 0;
108 for (j = 0; j < NAME_SIZE; j++)
109 name[j] = '\0';
110} // GPTPart::BlankPartition
111
112// Returns 1 if the two partitions overlap, 0 if they don't
113int GPTPart::DoTheyOverlap(GPTPart* other) {
114 int theyDo = 0;
115
116 // Don't bother checking unless these are defined (both start and end points
117 // are 0 for undefined partitions, so just check the start points)
118 if ((firstLBA != 0) && (other->firstLBA != 0)) {
119 if ((firstLBA < other->lastLBA) && (lastLBA >= other->firstLBA))
120 theyDo = 1;
121 if ((other->firstLBA < lastLBA) && (other->lastLBA >= firstLBA))
122 theyDo = 1;
123 } // if
124 return (theyDo);
125} // GPTPart::DoTheyOverlap()
126
127// Reverse the bytes of integral data types; used on big-endian systems.
128void GPTPart::ReversePartBytes(void) {
129 ReverseBytes(&partitionType.data1, 8);
130 ReverseBytes(&partitionType.data2, 8);
131 ReverseBytes(&uniqueGUID.data1, 8);
132 ReverseBytes(&uniqueGUID.data2, 8);
133 ReverseBytes(&firstLBA, 8);
134 ReverseBytes(&lastLBA, 8);
135 ReverseBytes(&attributes, 8);
136} // GPTPart::ReverseBytes()
137
138// Display summary information; does nothing if the partition is empty.
srs5694978041c2009-09-21 20:51:47 -0400139void GPTPart::ShowSummary(int partNum, uint32_t blockSize) {
srs5694fed16d02010-01-27 23:03:40 -0500140 string sizeInSI;
141 int i;
srs5694a0eb11a2009-08-29 15:00:08 -0400142
143 if (firstLBA != 0) {
srs5694fed16d02010-01-27 23:03:40 -0500144 sizeInSI = BytesToSI(blockSize * (lastLBA - firstLBA + 1));
145 cout.width(4);
146 cout << partNum + 1 << " ";
147 cout.width(14);
148 cout << firstLBA << " ";
149 cout.width(14);
150 cout << lastLBA << " ";
151 cout << BytesToSI(blockSize * (lastLBA - firstLBA + 1)) << " ";
152 for (i = 0; i < 9 - sizeInSI.length(); i++) cout << " ";
153 cout.fill('0');
154 cout.width(4);
155 cout.setf(ios::uppercase);
156 cout << hex << typeHelper.GUIDToID(partitionType) << " " << dec;
157 cout.fill(' ');
158 cout.setf(ios::right);
159 cout << GetName().substr(0, 23) << "\n";
160 cout.fill(' ');
srs5694a0eb11a2009-08-29 15:00:08 -0400161 } // if
162} // GPTPart::ShowSummary()
163
164// Show detailed partition information. Does nothing if the partition is
165// empty (as determined by firstLBA being 0).
166void GPTPart::ShowDetails(uint32_t blockSize) {
srs5694a0eb11a2009-08-29 15:00:08 -0400167 uint64_t size;
168
169 if (firstLBA != 0) {
srs5694fed16d02010-01-27 23:03:40 -0500170 cout << "Partition GUID code: " << GUIDToStr(partitionType);
171 cout << " (" << typeHelper.GUIDToName(partitionType) << ")\n";
172 cout << "Partition unique GUID: " << GUIDToStr(uniqueGUID) << "\n";
srs5694a0eb11a2009-08-29 15:00:08 -0400173
srs5694fed16d02010-01-27 23:03:40 -0500174 cout << "First sector: " << firstLBA << " (at "
175 << BytesToSI(firstLBA * blockSize) << ")\n";
176 cout << "Last sector: " << lastLBA << " (at "
177 << BytesToSI(lastLBA * blockSize) << ")\n";
srs5694a0eb11a2009-08-29 15:00:08 -0400178 size = (lastLBA - firstLBA + 1);
srs5694fed16d02010-01-27 23:03:40 -0500179 cout << "Partition size: " << size << " sectors ("
180 << BytesToSI(size * ((uint64_t) blockSize)) << ")\n";
181 cout << "Attribute flags: ";
182 cout.fill('0');
183 cout.width(16);
184 cout << right;
185 cout << hex;
186 cout << attributes << "\n";
187 cout << left;
188 cout << dec;
189 cout << "Partition name: " << GetName() << "\n";
srs5694a0eb11a2009-08-29 15:00:08 -0400190 } // if
191} // GPTPart::ShowDetails()
192
193/****************************************
194 * Functions requiring user interaction *
195 ****************************************/
196
197// Change the type code on the partition.
198void GPTPart::ChangeType(void) {
srs5694fed16d02010-01-27 23:03:40 -0500199 char line[255];
srs56945d58fe02010-01-03 20:57:08 -0500200 char* junk;
srs5694a0eb11a2009-08-29 15:00:08 -0400201 int typeNum = 0xFFFF;
srs5694a0eb11a2009-08-29 15:00:08 -0400202 GUIDData newType;
203
srs5694fed16d02010-01-27 23:03:40 -0500204 cout << "Current type is '" << GetNameType() << "'\n";
srs5694a0eb11a2009-08-29 15:00:08 -0400205 while ((!typeHelper.Valid(typeNum)) && (typeNum != 0)) {
srs5694fed16d02010-01-27 23:03:40 -0500206 cout << "Hex code (L to show codes, 0 to enter raw code, Enter = 0700): ";
srs56945d58fe02010-01-03 20:57:08 -0500207 junk = fgets(line, 255, stdin);
srs5694a0eb11a2009-08-29 15:00:08 -0400208 sscanf(line, "%X", &typeNum);
209 if ((line[0] == 'L') || (line[0] == 'l'))
210 typeHelper.ShowTypes();
srs5694546a9c72010-01-26 16:00:26 -0500211 if (line[0] == '\n') {
212 typeNum = 0x0700;
213 } // if
srs5694a0eb11a2009-08-29 15:00:08 -0400214 } // while
215 if (typeNum != 0) // user entered a code, so convert it
216 newType = typeHelper.IDToGUID((uint16_t) typeNum);
217 else // user wants to enter the GUID directly, so do that
218 newType = GetGUID();
219 partitionType = newType;
srs5694fed16d02010-01-27 23:03:40 -0500220 cout << "Changed type of partition to '" << typeHelper.GUIDToName(partitionType) << "'\n";
srs5694a0eb11a2009-08-29 15:00:08 -0400221} // GPTPart::ChangeType()
222
223// Set the name for a partition to theName, or prompt for a name if
srs5694fed16d02010-01-27 23:03:40 -0500224// theName is empty. Note that theName is a standard C++-style ASCII
srs5694a0eb11a2009-08-29 15:00:08 -0400225// string, although the GUID partition definition requires a UTF-16LE
226// string. This function creates a simple-minded copy for this.
srs5694fed16d02010-01-27 23:03:40 -0500227void GPTPart::SetName(string theName) {
srs5694a0eb11a2009-08-29 15:00:08 -0400228 char newName[NAME_SIZE]; // New name
srs5694fed16d02010-01-27 23:03:40 -0500229 char *junk;
srs5694a0eb11a2009-08-29 15:00:08 -0400230 int i;
231
232 // Blank out new name string, just to be on the safe side....
233 for (i = 0; i < NAME_SIZE; i++)
234 newName[i] = '\0';
235
srs5694fed16d02010-01-27 23:03:40 -0500236 if (theName == "") { // No name specified, so get one from the user
237 cout << "Enter name: ";
srs56945d58fe02010-01-03 20:57:08 -0500238 junk = fgets(newName, NAME_SIZE / 2, stdin);
srs5694a0eb11a2009-08-29 15:00:08 -0400239
240 // Input is likely to include a newline, so remove it....
241 i = strlen(newName);
242 if (newName[i - 1] == '\n')
243 newName[i - 1] = '\0';
244 } else {
srs5694fed16d02010-01-27 23:03:40 -0500245 strcpy(newName, theName.substr(0, NAME_SIZE / 2).c_str());
srs5694a0eb11a2009-08-29 15:00:08 -0400246 } // if
247
248 // Copy the C-style ASCII string from newName into a form that the GPT
249 // table will accept....
250 for (i = 0; i < NAME_SIZE; i++) {
251 if ((i % 2) == 0) {
252 name[i] = newName[(i / 2)];
253 } else {
254 name[i] = '\0';
255 } // if/else
256 } // for
257} // GPTPart::SetName()
258
259/***********************************
260 * Non-class but related functions *
261 ***********************************/
262
263// Recursive quick sort algorithm for GPT partitions. Note that if there
264// are any empties in the specified range, they'll be sorted to the
265// start, resulting in a sorted set of partitions that begins with
266// partition 2, 3, or higher.
267void QuickSortGPT(GPTPart* partitions, int start, int finish) {
268 uint64_t starterValue; // starting location of median partition
269 int left, right;
270 GPTPart temp;
271
272 left = start;
273 right = finish;
274 starterValue = partitions[(start + finish) / 2].GetFirstLBA();
275 do {
276 while (partitions[left].GetFirstLBA() < starterValue)
277 left++;
278 while (partitions[right].GetFirstLBA() > starterValue)
279 right--;
280 if (left <= right) {
281 temp = partitions[left];
282 partitions[left] = partitions[right];
283 partitions[right] = temp;
284 left++;
285 right--;
286 } // if
287 } while (left <= right);
288 if (start < right) QuickSortGPT(partitions, start, right);
289 if (finish > left) QuickSortGPT(partitions, left, finish);
290} // QuickSortGPT()