blob: d82c0f613120bed909971783e9c0a5a5388af834 [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
srs5694a0eb11a2009-08-29 15:00:08 -040026GPTPart::GPTPart(void) {
srs5694978041c2009-09-21 20:51:47 -040027 int i;
28
29 for (i = 0; i < NAME_SIZE; i++)
30 name[i] = '\0';
srs5694a0eb11a2009-08-29 15:00:08 -040031} // Default constructor
32
33GPTPart::~GPTPart(void) {
34} // destructor
35
srs5694a0eb11a2009-08-29 15:00:08 -040036// Return the gdisk-specific two-byte hex code for the partition
37uint16_t GPTPart::GetHexType(void) {
srs56946699b012010-02-04 00:55:30 -050038 return partitionType.GetHexType();
srs5694a0eb11a2009-08-29 15:00:08 -040039} // GPTPart::GetHexType()
40
41// Return a plain-text description of the partition type (e.g., "Linux/Windows
42// data" or "Linux swap").
srs56946699b012010-02-04 00:55:30 -050043string GPTPart::GetTypeName(void) {
44 return partitionType.TypeName();
srs5694a0eb11a2009-08-29 15:00:08 -040045} // GPTPart::GetNameType()
46
47// Compute and return the partition's length (or 0 if the end is incorrectly
48// set before the beginning).
49uint64_t GPTPart::GetLengthLBA(void) {
50 uint64_t length = 0;
51 if (firstLBA <= lastLBA)
52 length = lastLBA - firstLBA + UINT64_C(1);
53 return length;
54} // GPTPart::GetLengthLBA()
55
srs56940a697312010-01-28 21:10:52 -050056// Return partition's name field, converted to a C++ ASCII string
srs56946699b012010-02-04 00:55:30 -050057string GPTPart::GetDescription(void) {
srs56940a697312010-01-28 21:10:52 -050058 string theName;
srs5694a0eb11a2009-08-29 15:00:08 -040059 int i;
60
srs56940a697312010-01-28 21:10:52 -050061 theName = "";
62 for (i = 0; i < NAME_SIZE; i += 2) {
63 if (name[i] != '\0')
64 theName += name[i];
65 } // for
66 return theName;
srs56946699b012010-02-04 00:55:30 -050067} // GPTPart::GetDescription()
srs56940a697312010-01-28 21:10:52 -050068
srs569408bb0da2010-02-19 17:19:55 -050069// Return 1 if the partition is in use
70int GPTPart::IsUsed(void) {
71 return (firstLBA != UINT64_C(0));
72} // GPTPart::IsUsed()
73
srs56940a697312010-01-28 21:10:52 -050074// Set the type code to the specified one. Also changes the partition
75// name *IF* the current name is the generic one for the current partition
76// type.
srs56946699b012010-02-04 00:55:30 -050077void GPTPart::SetType(PartType t) {
78 if (GetDescription() == partitionType.TypeName()) {
79 SetName(t.TypeName());
srs56940a697312010-01-28 21:10:52 -050080 } // if
81 partitionType = t;
82} // GPTPart::SetType()
srs5694a0eb11a2009-08-29 15:00:08 -040083
srs5694a0eb11a2009-08-29 15:00:08 -040084// Set the name for a partition to theName, or prompt for a name if
srs5694fed16d02010-01-27 23:03:40 -050085// theName is empty. Note that theName is a standard C++-style ASCII
srs5694a0eb11a2009-08-29 15:00:08 -040086// string, although the GUID partition definition requires a UTF-16LE
87// string. This function creates a simple-minded copy for this.
srs56940a697312010-01-28 21:10:52 -050088void GPTPart::SetName(const string & theName) {
srs5694a0eb11a2009-08-29 15:00:08 -040089 char newName[NAME_SIZE]; // New name
srs5694fed16d02010-01-27 23:03:40 -050090 char *junk;
srs5694a0eb11a2009-08-29 15:00:08 -040091 int i;
92
93 // Blank out new name string, just to be on the safe side....
94 for (i = 0; i < NAME_SIZE; i++)
95 newName[i] = '\0';
96
srs5694fed16d02010-01-27 23:03:40 -050097 if (theName == "") { // No name specified, so get one from the user
98 cout << "Enter name: ";
srs56945d58fe02010-01-03 20:57:08 -050099 junk = fgets(newName, NAME_SIZE / 2, stdin);
srs5694a0eb11a2009-08-29 15:00:08 -0400100
101 // Input is likely to include a newline, so remove it....
102 i = strlen(newName);
srs56946699b012010-02-04 00:55:30 -0500103 if ((i > 0) && (i <= NAME_SIZE))
104 if (newName[i - 1] == '\n')
105 newName[i - 1] = '\0';
srs5694a0eb11a2009-08-29 15:00:08 -0400106 } else {
srs5694fed16d02010-01-27 23:03:40 -0500107 strcpy(newName, theName.substr(0, NAME_SIZE / 2).c_str());
srs5694a0eb11a2009-08-29 15:00:08 -0400108 } // if
109
110 // Copy the C-style ASCII string from newName into a form that the GPT
111 // table will accept....
112 for (i = 0; i < NAME_SIZE; i++) {
113 if ((i % 2) == 0) {
114 name[i] = newName[(i / 2)];
115 } else {
116 name[i] = '\0';
117 } // if/else
118 } // for
119} // GPTPart::SetName()
120
srs56946699b012010-02-04 00:55:30 -0500121// Set the name for the partition based on the current GUID partition type
122// code's associated name
123void GPTPart::SetDefaultDescription(void) {
124 SetName(partitionType.TypeName());
125} // GPTPart::SetDefaultDescription()
126
srs56940a697312010-01-28 21:10:52 -0500127GPTPart & GPTPart::operator=(const GPTPart & orig) {
128 int i;
129
130 partitionType = orig.partitionType;
131 uniqueGUID = orig.uniqueGUID;
132 firstLBA = orig.firstLBA;
133 lastLBA = orig.lastLBA;
134 attributes = orig.attributes;
135 for (i = 0; i < NAME_SIZE; i++)
136 name[i] = orig.name[i];
137 return *this;
138} // assignment operator
139
140// Display summary information; does nothing if the partition is empty.
141void GPTPart::ShowSummary(int partNum, uint32_t blockSize) {
142 string sizeInSI;
143 int i;
144
145 if (firstLBA != 0) {
146 sizeInSI = BytesToSI(blockSize * (lastLBA - firstLBA + 1));
srs569408bb0da2010-02-19 17:19:55 -0500147 cout.fill(' ');
srs56940a697312010-01-28 21:10:52 -0500148 cout.width(4);
149 cout << partNum + 1 << " ";
150 cout.width(14);
151 cout << firstLBA << " ";
152 cout.width(14);
153 cout << lastLBA << " ";
srs5694cb76c672010-02-11 22:22:22 -0500154 cout << BytesToSI(blockSize * (lastLBA - firstLBA + 1)) << " ";
155 for (i = 0; i < 10 - (int) sizeInSI.length(); i++)
156 cout << " ";
srs56940a697312010-01-28 21:10:52 -0500157 cout.fill('0');
158 cout.width(4);
159 cout.setf(ios::uppercase);
srs56946699b012010-02-04 00:55:30 -0500160 cout << hex << partitionType.GetHexType() << " " << dec;
srs56940a697312010-01-28 21:10:52 -0500161 cout.fill(' ');
srs56946699b012010-02-04 00:55:30 -0500162 cout << GetDescription().substr(0, 23) << "\n";
srs56940a697312010-01-28 21:10:52 -0500163 cout.fill(' ');
164 } // if
165} // GPTPart::ShowSummary()
166
167// Show detailed partition information. Does nothing if the partition is
168// empty (as determined by firstLBA being 0).
169void GPTPart::ShowDetails(uint32_t blockSize) {
170 uint64_t size;
171
172 if (firstLBA != 0) {
srs56946699b012010-02-04 00:55:30 -0500173 cout << "Partition GUID code: " << partitionType.AsString();
174 cout << " (" << partitionType.TypeName() << ")\n";
175 cout << "Partition unique GUID: " << uniqueGUID.AsString() << "\n";
srs56940a697312010-01-28 21:10:52 -0500176
177 cout << "First sector: " << firstLBA << " (at "
178 << BytesToSI(firstLBA * blockSize) << ")\n";
179 cout << "Last sector: " << lastLBA << " (at "
180 << BytesToSI(lastLBA * blockSize) << ")\n";
181 size = (lastLBA - firstLBA + 1);
182 cout << "Partition size: " << size << " sectors ("
183 << BytesToSI(size * ((uint64_t) blockSize)) << ")\n";
184 cout << "Attribute flags: ";
185 cout.fill('0');
186 cout.width(16);
187 cout << hex;
188 cout << attributes << "\n";
189 cout << dec;
srs56946699b012010-02-04 00:55:30 -0500190 cout << "Partition name: " << GetDescription() << "\n";
srs56940a697312010-01-28 21:10:52 -0500191 cout.fill(' ');
192 } // if
193} // GPTPart::ShowDetails()
194
195// Blank (delete) a single partition
196void GPTPart::BlankPartition(void) {
197 int j;
srs56940a697312010-01-28 21:10:52 -0500198
srs56946699b012010-02-04 00:55:30 -0500199 uniqueGUID.Zero();
200 partitionType.Zero();
srs56940a697312010-01-28 21:10:52 -0500201 firstLBA = 0;
202 lastLBA = 0;
203 attributes = 0;
204 for (j = 0; j < NAME_SIZE; j++)
205 name[j] = '\0';
206} // GPTPart::BlankPartition
207
208// Returns 1 if the two partitions overlap, 0 if they don't
209int GPTPart::DoTheyOverlap(const GPTPart & other) {
210 int theyDo = 0;
211
212 // Don't bother checking unless these are defined (both start and end points
213 // are 0 for undefined partitions, so just check the start points)
214 if ((firstLBA != 0) && (other.firstLBA != 0)) {
215 if ((firstLBA < other.lastLBA) && (lastLBA >= other.firstLBA))
216 theyDo = 1;
217 if ((other.firstLBA < lastLBA) && (other.lastLBA >= firstLBA))
218 theyDo = 1;
219 } // if
220 return (theyDo);
221} // GPTPart::DoTheyOverlap()
222
223// Reverse the bytes of integral data types; used on big-endian systems.
224void GPTPart::ReversePartBytes(void) {
srs56946699b012010-02-04 00:55:30 -0500225// partitionType.ReverseGUIDBytes();
226// uniqueGUID.ReverseGUIDBytes();
srs56940a697312010-01-28 21:10:52 -0500227 ReverseBytes(&firstLBA, 8);
228 ReverseBytes(&lastLBA, 8);
229 ReverseBytes(&attributes, 8);
230} // GPTPart::ReverseBytes()
231
232/****************************************
233 * Functions requiring user interaction *
234 ****************************************/
235
srs56946699b012010-02-04 00:55:30 -0500236// Change the type code on the partition. Also changes the name if the original
237// name is the generic one for the partition type.
srs56940a697312010-01-28 21:10:52 -0500238void GPTPart::ChangeType(void) {
239 char line[255];
240 char* junk;
srs56946699b012010-02-04 00:55:30 -0500241 unsigned int typeNum = 0xFFFF, changeName = 0;
srs56940a697312010-01-28 21:10:52 -0500242
srs56946699b012010-02-04 00:55:30 -0500243 if (GetDescription() == GetTypeName())
244 changeName = 1;
245 cout << "Current type is '" << GetTypeName() << "'\n";
246 while ((!partitionType.Valid(typeNum)) && (typeNum != 0)) {
srs56940a697312010-01-28 21:10:52 -0500247 cout << "Hex code (L to show codes, 0 to enter raw code, Enter = 0700): ";
248 junk = fgets(line, 255, stdin);
249 sscanf(line, "%X", &typeNum);
250 if ((line[0] == 'L') || (line[0] == 'l'))
srs56946699b012010-02-04 00:55:30 -0500251 partitionType.ShowAllTypes();
srs56940a697312010-01-28 21:10:52 -0500252 if (line[0] == '\n') {
253 typeNum = 0x0700;
254 } // if
255 } // while
256 if (typeNum != 0) // user entered a code, so convert it
srs56946699b012010-02-04 00:55:30 -0500257 partitionType = typeNum;
srs56940a697312010-01-28 21:10:52 -0500258 else // user wants to enter the GUID directly, so do that
srs56946699b012010-02-04 00:55:30 -0500259 partitionType.GetGUIDFromUser();
260 cout << "Changed type of partition to '" << partitionType.TypeName() << "'\n";
261 if (changeName) {
262 SetDefaultDescription();
263 } // if
srs56940a697312010-01-28 21:10:52 -0500264} // GPTPart::ChangeType()