blob: 0f5ef02b23fac1b1b3ed701aae0f1107417c5034 [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//
srs5694699941e2011-03-21 21:33:57 -04007// Author: Rod Smith <rodsmith@rodsbooks.com>, (C) 2009-2011
srs5694a0eb11a2009-08-29 15:00:08 -04008//
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
srs569400b6d7a2011-06-26 22:40:06 -040018#ifdef USE_UTF16
srs5694699941e2011-03-21 21:33:57 -040019#include <unicode/ustdio.h>
20#else
21#define UnicodeString string
22#endif
23
srs5694a0eb11a2009-08-29 15:00:08 -040024#include <string.h>
srs5694fed16d02010-01-27 23:03:40 -050025#include <stdio.h>
26#include <iostream>
srs5694a0eb11a2009-08-29 15:00:08 -040027#include "gptpart.h"
28#include "attributes.h"
29
30using namespace std;
31
srs5694a0eb11a2009-08-29 15:00:08 -040032GPTPart::GPTPart(void) {
srs569455d92612010-03-07 22:16:07 -050033 partitionType.Zero();
34 uniqueGUID.Zero();
35 firstLBA = 0;
36 lastLBA = 0;
37 attributes = 0;
srs569464cbd172011-03-01 22:03:54 -050038 memset(name, 0, NAME_SIZE);
srs5694a0eb11a2009-08-29 15:00:08 -040039} // Default constructor
40
41GPTPart::~GPTPart(void) {
42} // destructor
43
srs5694a0eb11a2009-08-29 15:00:08 -040044// Return the gdisk-specific two-byte hex code for the partition
srs5694bf8950c2011-03-12 01:23:12 -050045uint16_t GPTPart::GetHexType(void) const {
srs56946699b012010-02-04 00:55:30 -050046 return partitionType.GetHexType();
srs5694a0eb11a2009-08-29 15:00:08 -040047} // GPTPart::GetHexType()
48
49// Return a plain-text description of the partition type (e.g., "Linux/Windows
50// data" or "Linux swap").
srs56946699b012010-02-04 00:55:30 -050051string GPTPart::GetTypeName(void) {
52 return partitionType.TypeName();
srs5694a0eb11a2009-08-29 15:00:08 -040053} // GPTPart::GetNameType()
54
srs56945a608532011-03-17 13:53:01 -040055// Return a Unicode description of the partition type (e.g., "Linux/Windows
56// data" or "Linux swap").
57UnicodeString GPTPart::GetUTypeName(void) {
58 return partitionType.UTypeName();
srs56945a608532011-03-17 13:53:01 -040059} // GPTPart::GetNameType()
60
srs5694a0eb11a2009-08-29 15:00:08 -040061// Compute and return the partition's length (or 0 if the end is incorrectly
62// set before the beginning).
srs5694bf8950c2011-03-12 01:23:12 -050063uint64_t GPTPart::GetLengthLBA(void) const {
srs5694a0eb11a2009-08-29 15:00:08 -040064 uint64_t length = 0;
srs569455d92612010-03-07 22:16:07 -050065
srs5694a0eb11a2009-08-29 15:00:08 -040066 if (firstLBA <= lastLBA)
67 length = lastLBA - firstLBA + UINT64_C(1);
68 return length;
69} // GPTPart::GetLengthLBA()
70
srs569400b6d7a2011-06-26 22:40:06 -040071#ifdef USE_UTF16
srs5694699941e2011-03-21 21:33:57 -040072// Return partition's name field, converted to a Unicode string
73UnicodeString GPTPart::GetDescription(void) {
74 return (UChar*) name;
75} // GPTPart::GetDescription()
76#else
77// Return partition's name field, converted to a C++ ASCII string
srs56946699b012010-02-04 00:55:30 -050078string GPTPart::GetDescription(void) {
srs56940a697312010-01-28 21:10:52 -050079 string theName;
srs569400b6d7a2011-06-26 22:40:06 -040080 int i = 0;
srs5694a0eb11a2009-08-29 15:00:08 -040081
srs56940a697312010-01-28 21:10:52 -050082 theName = "";
srs569400b6d7a2011-06-26 22:40:06 -040083 while ((i < NAME_SIZE) && (name[i] != '\0')) {
84 theName += name[i];
85 i+=2;
86 } // while
srs56940a697312010-01-28 21:10:52 -050087 return theName;
srs5694699941e2011-03-21 21:33:57 -040088} // GPTPart::GetDescription() (Windows version)
89#endif
srs56940a697312010-01-28 21:10:52 -050090
srs569408bb0da2010-02-19 17:19:55 -050091// Return 1 if the partition is in use
92int GPTPart::IsUsed(void) {
srs5694e69e6802012-01-20 22:37:12 -050093 return (partitionType != GUIDData("0x00"));
94// return (firstLBA != UINT64_C(0));
srs569408bb0da2010-02-19 17:19:55 -050095} // GPTPart::IsUsed()
96
srs56940a697312010-01-28 21:10:52 -050097// Set the type code to the specified one. Also changes the partition
98// name *IF* the current name is the generic one for the current partition
99// type.
srs56946699b012010-02-04 00:55:30 -0500100void GPTPart::SetType(PartType t) {
srs56945a608532011-03-17 13:53:01 -0400101 if (GetDescription() == partitionType.UTypeName()) {
srs56946699b012010-02-04 00:55:30 -0500102 SetName(t.TypeName());
srs56940a697312010-01-28 21:10:52 -0500103 } // if
104 partitionType = t;
105} // GPTPart::SetType()
srs5694a0eb11a2009-08-29 15:00:08 -0400106
srs569400b6d7a2011-06-26 22:40:06 -0400107#ifdef USE_UTF16
srs5694699941e2011-03-21 21:33:57 -0400108// Set the name for a partition to theName, using a C++-style string as
109// input.
110void GPTPart::SetName(const string & theName) {
111 SetName((UnicodeString) theName.c_str());
srs56945a608532011-03-17 13:53:01 -0400112} // GPTPart::SetName()
113
srs5694699941e2011-03-21 21:33:57 -0400114// Set the name for a partition to theName, using a Unicode string as
115// input.
116void GPTPart::SetName(const UnicodeString & theName) {
srs5694d8eed462012-12-15 01:55:21 -0500117 cout << "Entering GPTPart::SetName(const UnicodeString...)\n";
srs5694699941e2011-03-21 21:33:57 -0400118 if (theName.isBogus()) {
119 cerr << "Bogus UTF-16 name found in GPTPart::SetName()! Name not changed!\n";
120 } else {
121 memset(name, 0, NAME_SIZE);
122 theName.extractBetween(0, NAME_SIZE / 2 - 1, (UChar*) name);
123 } // if/else
srs5694a0eb11a2009-08-29 15:00:08 -0400124} // GPTPart::SetName()
srs569400b6d7a2011-06-26 22:40:06 -0400125
srs5694699941e2011-03-21 21:33:57 -0400126#else
srs569400b6d7a2011-06-26 22:40:06 -0400127
srs5694699941e2011-03-21 21:33:57 -0400128// Set the name for a partition to theName. Note that theName is a
129// standard C++-style ASCII string, although the GUID partition definition
130// requires a UTF-16LE string. This function creates a simple-minded copy
131// for this.
132void GPTPart::SetName(const string & theName) {
133 int i, length;
134
135 if (theName.length() < (NAME_SIZE / 2))
136 length = theName.length();
137 else
138 length = NAME_SIZE / 2;
139 memset(name, 0, NAME_SIZE);
140 for (i = 0; i < length; i++)
141 name[i * 2] = theName[i];
srs5694d8eed462012-12-15 01:55:21 -0500142} // GPTPart::SetName(), ASCII version
srs5694699941e2011-03-21 21:33:57 -0400143#endif
srs5694a0eb11a2009-08-29 15:00:08 -0400144
srs56946699b012010-02-04 00:55:30 -0500145// Set the name for the partition based on the current GUID partition type
146// code's associated name
147void GPTPart::SetDefaultDescription(void) {
148 SetName(partitionType.TypeName());
149} // GPTPart::SetDefaultDescription()
150
srs56940a697312010-01-28 21:10:52 -0500151GPTPart & GPTPart::operator=(const GPTPart & orig) {
srs56940a697312010-01-28 21:10:52 -0500152 partitionType = orig.partitionType;
153 uniqueGUID = orig.uniqueGUID;
154 firstLBA = orig.firstLBA;
155 lastLBA = orig.lastLBA;
156 attributes = orig.attributes;
srs569464cbd172011-03-01 22:03:54 -0500157 memcpy(name, orig.name, NAME_SIZE);
srs56940a697312010-01-28 21:10:52 -0500158 return *this;
159} // assignment operator
160
srs56949a46b042011-03-15 00:34:10 -0400161// Compare the values, and return a bool result.
162// Because this is intended for sorting and a firstLBA value of 0 denotes
163// a partition that's not in use and so that should be sorted upwards,
164// we return the opposite of the usual arithmetic result when either
165// firstLBA value is 0.
166bool GPTPart::operator<(const GPTPart &other) const {
srs56949a46b042011-03-15 00:34:10 -0400167 if (firstLBA && other.firstLBA)
168 return (firstLBA < other.firstLBA);
169 else
170 return (other.firstLBA < firstLBA);
171} // GPTPart::operator<()
172
srs56940a697312010-01-28 21:10:52 -0500173// Display summary information; does nothing if the partition is empty.
174void GPTPart::ShowSummary(int partNum, uint32_t blockSize) {
srs569401f7f082011-03-15 23:53:31 -0400175 string sizeInIeee;
srs56945a608532011-03-17 13:53:01 -0400176 UnicodeString description;
srs569464cbd172011-03-01 22:03:54 -0500177 size_t i;
srs56940a697312010-01-28 21:10:52 -0500178
179 if (firstLBA != 0) {
srs569401f7f082011-03-15 23:53:31 -0400180 sizeInIeee = BytesToIeee(lastLBA - firstLBA + 1, blockSize);
srs569408bb0da2010-02-19 17:19:55 -0500181 cout.fill(' ');
srs56940a697312010-01-28 21:10:52 -0500182 cout.width(4);
183 cout << partNum + 1 << " ";
184 cout.width(14);
185 cout << firstLBA << " ";
186 cout.width(14);
187 cout << lastLBA << " ";
srs569401f7f082011-03-15 23:53:31 -0400188 cout << BytesToIeee(lastLBA - firstLBA + 1, blockSize) << " ";
189 if (sizeInIeee.length() < 10)
190 for (i = 0; i < 10 - sizeInIeee.length(); i++)
191 cout << " ";
srs56940a697312010-01-28 21:10:52 -0500192 cout.fill('0');
193 cout.width(4);
194 cout.setf(ios::uppercase);
srs56946699b012010-02-04 00:55:30 -0500195 cout << hex << partitionType.GetHexType() << " " << dec;
srs56940a697312010-01-28 21:10:52 -0500196 cout.fill(' ');
srs569400b6d7a2011-06-26 22:40:06 -0400197#ifdef USE_UTF16
srs5694699941e2011-03-21 21:33:57 -0400198 GetDescription().extractBetween(0, 23, description);
srs56945a608532011-03-17 13:53:01 -0400199 cout << description << "\n";
srs5694699941e2011-03-21 21:33:57 -0400200#else
201 cout << GetDescription().substr(0, 23) << "\n";
202#endif
srs56940a697312010-01-28 21:10:52 -0500203 cout.fill(' ');
204 } // if
205} // GPTPart::ShowSummary()
206
207// Show detailed partition information. Does nothing if the partition is
208// empty (as determined by firstLBA being 0).
209void GPTPart::ShowDetails(uint32_t blockSize) {
210 uint64_t size;
211
212 if (firstLBA != 0) {
srs56945a081752010-09-24 20:39:41 -0400213 cout << "Partition GUID code: " << partitionType;
srs56946699b012010-02-04 00:55:30 -0500214 cout << " (" << partitionType.TypeName() << ")\n";
srs56945a081752010-09-24 20:39:41 -0400215 cout << "Partition unique GUID: " << uniqueGUID << "\n";
srs56940a697312010-01-28 21:10:52 -0500216
217 cout << "First sector: " << firstLBA << " (at "
srs569401f7f082011-03-15 23:53:31 -0400218 << BytesToIeee(firstLBA, blockSize) << ")\n";
srs56940a697312010-01-28 21:10:52 -0500219 cout << "Last sector: " << lastLBA << " (at "
srs569401f7f082011-03-15 23:53:31 -0400220 << BytesToIeee(lastLBA, blockSize) << ")\n";
srs56940a697312010-01-28 21:10:52 -0500221 size = (lastLBA - firstLBA + 1);
222 cout << "Partition size: " << size << " sectors ("
srs569401f7f082011-03-15 23:53:31 -0400223 << BytesToIeee(size, blockSize) << ")\n";
srs56940a697312010-01-28 21:10:52 -0500224 cout << "Attribute flags: ";
225 cout.fill('0');
226 cout.width(16);
227 cout << hex;
228 cout << attributes << "\n";
229 cout << dec;
srs5694699941e2011-03-21 21:33:57 -0400230 cout << "Partition name: '" << GetDescription() << "'\n";
srs56940a697312010-01-28 21:10:52 -0500231 cout.fill(' ');
232 } // if
233} // GPTPart::ShowDetails()
234
235// Blank (delete) a single partition
236void GPTPart::BlankPartition(void) {
srs56946699b012010-02-04 00:55:30 -0500237 uniqueGUID.Zero();
238 partitionType.Zero();
srs56940a697312010-01-28 21:10:52 -0500239 firstLBA = 0;
240 lastLBA = 0;
241 attributes = 0;
srs569464cbd172011-03-01 22:03:54 -0500242 memset(name, 0, NAME_SIZE);
srs56940a697312010-01-28 21:10:52 -0500243} // GPTPart::BlankPartition
244
245// Returns 1 if the two partitions overlap, 0 if they don't
246int GPTPart::DoTheyOverlap(const GPTPart & other) {
srs56940a697312010-01-28 21:10:52 -0500247 // Don't bother checking unless these are defined (both start and end points
248 // are 0 for undefined partitions, so just check the start points)
srs569464cbd172011-03-01 22:03:54 -0500249 return firstLBA && other.firstLBA &&
250 (firstLBA <= other.lastLBA) != (lastLBA < other.firstLBA);
srs56940a697312010-01-28 21:10:52 -0500251} // GPTPart::DoTheyOverlap()
252
srs56945a608532011-03-17 13:53:01 -0400253// Reverse the bytes of integral data types and of the UTF-16LE name;
254// used on big-endian systems.
srs56940a697312010-01-28 21:10:52 -0500255void GPTPart::ReversePartBytes(void) {
srs56945a608532011-03-17 13:53:01 -0400256 int i;
257
srs56940a697312010-01-28 21:10:52 -0500258 ReverseBytes(&firstLBA, 8);
259 ReverseBytes(&lastLBA, 8);
260 ReverseBytes(&attributes, 8);
srs56945a608532011-03-17 13:53:01 -0400261 for (i = 0; i < NAME_SIZE; i += 2)
262 ReverseBytes(name + i, 2);
srs56940a697312010-01-28 21:10:52 -0500263} // GPTPart::ReverseBytes()
264
265/****************************************
266 * Functions requiring user interaction *
267 ****************************************/
268
srs56946699b012010-02-04 00:55:30 -0500269// Change the type code on the partition. Also changes the name if the original
270// name is the generic one for the partition type.
srs56940a697312010-01-28 21:10:52 -0500271void GPTPart::ChangeType(void) {
srs56945a608532011-03-17 13:53:01 -0400272 string line;
srs569464cbd172011-03-01 22:03:54 -0500273 int changeName;
srs569482f3f0b2010-09-22 10:50:24 -0400274 PartType tempType = (GUIDData) "00000000-0000-0000-0000-000000000000";
srs56940a697312010-01-28 21:10:52 -0500275
srs56945a608532011-03-17 13:53:01 -0400276 changeName = (GetDescription() == GetUTypeName());
srs569455d92612010-03-07 22:16:07 -0500277
srs56946699b012010-02-04 00:55:30 -0500278 cout << "Current type is '" << GetTypeName() << "'\n";
srs569482f3f0b2010-09-22 10:50:24 -0400279 do {
srs569400b6d7a2011-06-26 22:40:06 -0400280 cout << "Hex code or GUID (L to show codes, Enter = " << hex << DEFAULT_TYPE << dec << "): ";
srs56945a608532011-03-17 13:53:01 -0400281 line = ReadString();
srs569482f3f0b2010-09-22 10:50:24 -0400282 if ((line[0] == 'L') || (line[0] == 'l')) {
srs56946699b012010-02-04 00:55:30 -0500283 partitionType.ShowAllTypes();
srs569482f3f0b2010-09-22 10:50:24 -0400284 } else {
srs56945a608532011-03-17 13:53:01 -0400285 if (line.length() == 0)
srs569400b6d7a2011-06-26 22:40:06 -0400286 tempType= DEFAULT_TYPE;
srs569482f3f0b2010-09-22 10:50:24 -0400287 else
288 tempType = line;
289 } // if/else
290 } while (tempType == (GUIDData) "00000000-0000-0000-0000-000000000000");
291 partitionType = tempType;
srs56946699b012010-02-04 00:55:30 -0500292 cout << "Changed type of partition to '" << partitionType.TypeName() << "'\n";
293 if (changeName) {
294 SetDefaultDescription();
295 } // if
srs56940a697312010-01-28 21:10:52 -0500296} // GPTPart::ChangeType()