blob: 7c6156913a668e50eb66216fab4bc0021f0c5453 [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//
Roderick W. Smithe3ee7332013-09-24 12:56:11 -04007// Author: Rod Smith <rodsmith@rodsbooks.com>, (C) 2009-2013
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;
Roderick W. Smith84aaff62014-02-17 16:17:11 -050038 memset(name, 0, NAME_SIZE * sizeof(name[0]) );
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
Roderick W. Smith84aaff62014-02-17 16:17:11 -050055#ifdef USE_UTF16
srs56945a608532011-03-17 13:53:01 -040056// Return a Unicode description of the partition type (e.g., "Linux/Windows
57// data" or "Linux swap").
58UnicodeString GPTPart::GetUTypeName(void) {
59 return partitionType.UTypeName();
srs56945a608532011-03-17 13:53:01 -040060} // GPTPart::GetNameType()
Roderick W. Smith84aaff62014-02-17 16:17:11 -050061#endif
srs56945a608532011-03-17 13:53:01 -040062
srs5694a0eb11a2009-08-29 15:00:08 -040063// Compute and return the partition's length (or 0 if the end is incorrectly
64// set before the beginning).
srs5694bf8950c2011-03-12 01:23:12 -050065uint64_t GPTPart::GetLengthLBA(void) const {
srs5694a0eb11a2009-08-29 15:00:08 -040066 uint64_t length = 0;
srs569455d92612010-03-07 22:16:07 -050067
srs5694a0eb11a2009-08-29 15:00:08 -040068 if (firstLBA <= lastLBA)
69 length = lastLBA - firstLBA + UINT64_C(1);
70 return length;
71} // GPTPart::GetLengthLBA()
72
srs569400b6d7a2011-06-26 22:40:06 -040073#ifdef USE_UTF16
srs5694699941e2011-03-21 21:33:57 -040074// Return partition's name field, converted to a Unicode string
75UnicodeString GPTPart::GetDescription(void) {
76 return (UChar*) name;
77} // GPTPart::GetDescription()
78#else
Roderick W. Smith84aaff62014-02-17 16:17:11 -050079// Return partition's name field, converted to a C++ UTF-8 string
srs56946699b012010-02-04 00:55:30 -050080string GPTPart::GetDescription(void) {
Roderick W. Smith84aaff62014-02-17 16:17:11 -050081 // convert name to utf32 then to utf8
82 string utf8 ;
83 size_t pos = 0 ;
84 while ( ( pos < NAME_SIZE ) && ( name[ pos ] != 0 ) ) {
85 uint16_t cp = name[ pos ++ ] ;
86 if ( ! IsLittleEndian() ) ReverseBytes( & cp , 2 ) ;
87 // first to utf32
88 uint32_t uni ;
89 if ( cp < 0xd800 || cp > 0xdfff ) {
90 uni = cp ;
91 } // if
92 else if ( cp < 0xdc00 ) {
93 // lead surrogate
94 uni = ( (uint32_t)( cp & 0x3ff ) ) << 10 ;
95 if ( pos >= NAME_SIZE ) {
96 // missing trail surrogate, name[] is invalid
97 break ;
98 } // if
99 cp = name[ pos ++ ] ;
100 if ( cp < 0xdc00 || cp > 0xdfff ) {
101 // invalid trail surrogate, name[] is invalid
102 break ;
103 } // if
104 // trail surrogate
105 uni |= cp & 0x3ff ;
106 uni += 0x10000 ;
107 } // if
108 else {
109 // unexpected trail surrogate, name[] is invalid
110 break ;
111 } // if
112 // then to utf8
113 if ( uni < 0x80 ) {
114 utf8 += (char) uni ;
115 } // if
116 else if ( uni < 0x800 ) {
117 utf8 += (char) ( 0xc0 | ( uni >> 6 ) ) ;
118 utf8 += (char) ( 0x80 | ( uni & 0x3f ) ) ;
119 } // if
120 else if ( uni < 0x10000 ) {
121 utf8 += (char) ( 0xe0 | ( uni >> 12 ) ) ;
122 utf8 += (char) ( 0x80 | ( ( uni >> 6 ) & 0x3f ) ) ;
123 utf8 += (char) ( 0x80 | ( uni & 0x3f ) ) ;
124 } // if
125 else {
126 utf8 += (char) ( 0xf0 | ( uni >> 18 ) ) ;
127 utf8 += (char) ( 0xe0 | ( ( uni >> 12 ) & 0x3f ) ) ;
128 utf8 += (char) ( 0x80 | ( ( uni >> 6 ) & 0x3f ) ) ;
129 utf8 += (char) ( 0x80 | ( uni & 0x3f ) ) ;
130 } // if
131 }
132 return utf8 ;
133} // GPTPart::GetDescription(), UTF-8 version
srs5694699941e2011-03-21 21:33:57 -0400134#endif
srs56940a697312010-01-28 21:10:52 -0500135
srs569408bb0da2010-02-19 17:19:55 -0500136// Return 1 if the partition is in use
137int GPTPart::IsUsed(void) {
srs5694e69e6802012-01-20 22:37:12 -0500138 return (partitionType != GUIDData("0x00"));
srs569408bb0da2010-02-19 17:19:55 -0500139} // GPTPart::IsUsed()
140
srs56940a697312010-01-28 21:10:52 -0500141// Set the type code to the specified one. Also changes the partition
142// name *IF* the current name is the generic one for the current partition
143// type.
srs56946699b012010-02-04 00:55:30 -0500144void GPTPart::SetType(PartType t) {
Roderick W. Smith84aaff62014-02-17 16:17:11 -0500145#ifdef USE_UTF16
srs56945a608532011-03-17 13:53:01 -0400146 if (GetDescription() == partitionType.UTypeName()) {
Roderick W. Smith84aaff62014-02-17 16:17:11 -0500147#else
148 if (GetDescription() == partitionType.TypeName()) {
149#endif
srs56946699b012010-02-04 00:55:30 -0500150 SetName(t.TypeName());
srs56940a697312010-01-28 21:10:52 -0500151 } // if
152 partitionType = t;
153} // GPTPart::SetType()
srs5694a0eb11a2009-08-29 15:00:08 -0400154
srs569400b6d7a2011-06-26 22:40:06 -0400155#ifdef USE_UTF16
srs5694699941e2011-03-21 21:33:57 -0400156// Set the name for a partition to theName, using a C++-style string as
157// input.
158void GPTPart::SetName(const string & theName) {
159 SetName((UnicodeString) theName.c_str());
srs56945a608532011-03-17 13:53:01 -0400160} // GPTPart::SetName()
161
srs5694699941e2011-03-21 21:33:57 -0400162// Set the name for a partition to theName, using a Unicode string as
163// input.
164void GPTPart::SetName(const UnicodeString & theName) {
165 if (theName.isBogus()) {
166 cerr << "Bogus UTF-16 name found in GPTPart::SetName()! Name not changed!\n";
167 } else {
Roderick W. Smith84aaff62014-02-17 16:17:11 -0500168 memset(name, 0, NAME_SIZE * sizeof(name[0]) );
169 theName.extractBetween(0, NAME_SIZE, (UChar*) name);
srs5694699941e2011-03-21 21:33:57 -0400170 } // if/else
srs5694a0eb11a2009-08-29 15:00:08 -0400171} // GPTPart::SetName()
srs569400b6d7a2011-06-26 22:40:06 -0400172
srs5694699941e2011-03-21 21:33:57 -0400173#else
srs569400b6d7a2011-06-26 22:40:06 -0400174
srs5694699941e2011-03-21 21:33:57 -0400175// Set the name for a partition to theName. Note that theName is a
176// standard C++-style ASCII string, although the GUID partition definition
177// requires a UTF-16LE string. This function creates a simple-minded copy
178// for this.
179void GPTPart::SetName(const string & theName) {
Roderick W. Smith84aaff62014-02-17 16:17:11 -0500180 // convert utf8 to utf32 then to utf16le
181 size_t len = theName.length() ;
182 size_t pos = 0 ;
183 for ( size_t i = 0 ; pos < NAME_SIZE && i < len ; ) {
184 uint32_t uni ;
185 uint8_t cp = theName[ i ++ ] ;
186 int todo ;
187 if ( cp < 0x80 ) {
188 uni = cp ;
189 todo = 0 ;
190 } // if
191 else if ( cp < 0xc0 || cp > 0xf7 ) {
192 // invalid byte, theName is broken
193 break ;
194 } // if
195 else if ( cp < 0xe0 ) {
196 uni = cp & 0x1f ;
197 todo = 1 ;
198 } // if
199 else if ( cp < 0xf0 ) {
200 uni = cp & 0x0f ;
201 todo = 2 ;
202 } // if
203 else {
204 uni = cp & 0x7 ;
205 todo = 3 ;
206 } // if
207 while ( todo > 0 ) {
208 if ( i >= len ) {
209 // missing continuation byte, theName is broken
210 goto break_converter ;
211 } // if
212 cp = theName[ i ++ ] ;
213 if ( cp > 0xbf || cp < 0x80 ) {
214 // invalid continuation byte, theName is broken
215 goto break_converter ;
216 } // if
217 uni <<= 6 ;
218 uni |= cp & 0x3f ;
219 todo -- ;
220 } // while
221 // then to utf16le
222 if ( uni < 0x10000 ) {
223 name[ pos ] = (uint16_t) uni ;
224 if ( ! IsLittleEndian() ) ReverseBytes( name + pos , 2 ) ;
225 pos ++ ;
226 } // if
227 else {
228 if ( pos > NAME_SIZE - 2 ) {
229 // not enough room for two surrogates, truncate
230 break ;
231 } // if
232 uni -= 0x10000 ;
233 name[ pos ] = (uint16_t)( uni >> 10 ) | 0xd800 ;
234 if ( ! IsLittleEndian() ) ReverseBytes( name + pos , 2 ) ;
235 pos ++ ;
236 name[ pos ] = (uint16_t)( uni & 0x3ff ) | 0xdc00 ;
237 if ( ! IsLittleEndian() ) ReverseBytes( name + pos , 2 ) ;
238 pos ++ ;
239 }
240 } // for
241 break_converter : ;
242 // finally fill with zeroes
243 while ( pos < NAME_SIZE ) {
244 name[ pos ++ ] = 0 ;
245 } // while
246} // GPTPart::SetName(), UTF-8 version
srs5694699941e2011-03-21 21:33:57 -0400247#endif
srs5694a0eb11a2009-08-29 15:00:08 -0400248
srs56946699b012010-02-04 00:55:30 -0500249// Set the name for the partition based on the current GUID partition type
250// code's associated name
251void GPTPart::SetDefaultDescription(void) {
252 SetName(partitionType.TypeName());
253} // GPTPart::SetDefaultDescription()
254
srs56940a697312010-01-28 21:10:52 -0500255GPTPart & GPTPart::operator=(const GPTPart & orig) {
srs56940a697312010-01-28 21:10:52 -0500256 partitionType = orig.partitionType;
257 uniqueGUID = orig.uniqueGUID;
258 firstLBA = orig.firstLBA;
259 lastLBA = orig.lastLBA;
260 attributes = orig.attributes;
Roderick W. Smith84aaff62014-02-17 16:17:11 -0500261 memcpy(name, orig.name, NAME_SIZE * sizeof( name[ 0 ] ) );
srs56940a697312010-01-28 21:10:52 -0500262 return *this;
263} // assignment operator
264
srs56949a46b042011-03-15 00:34:10 -0400265// Compare the values, and return a bool result.
266// Because this is intended for sorting and a firstLBA value of 0 denotes
267// a partition that's not in use and so that should be sorted upwards,
268// we return the opposite of the usual arithmetic result when either
269// firstLBA value is 0.
270bool GPTPart::operator<(const GPTPart &other) const {
srs56949a46b042011-03-15 00:34:10 -0400271 if (firstLBA && other.firstLBA)
272 return (firstLBA < other.firstLBA);
273 else
274 return (other.firstLBA < firstLBA);
275} // GPTPart::operator<()
276
srs56940a697312010-01-28 21:10:52 -0500277// Display summary information; does nothing if the partition is empty.
278void GPTPart::ShowSummary(int partNum, uint32_t blockSize) {
srs569401f7f082011-03-15 23:53:31 -0400279 string sizeInIeee;
srs56945a608532011-03-17 13:53:01 -0400280 UnicodeString description;
srs569464cbd172011-03-01 22:03:54 -0500281 size_t i;
srs56940a697312010-01-28 21:10:52 -0500282
283 if (firstLBA != 0) {
srs569401f7f082011-03-15 23:53:31 -0400284 sizeInIeee = BytesToIeee(lastLBA - firstLBA + 1, blockSize);
srs569408bb0da2010-02-19 17:19:55 -0500285 cout.fill(' ');
srs56940a697312010-01-28 21:10:52 -0500286 cout.width(4);
287 cout << partNum + 1 << " ";
288 cout.width(14);
289 cout << firstLBA << " ";
290 cout.width(14);
291 cout << lastLBA << " ";
srs569401f7f082011-03-15 23:53:31 -0400292 cout << BytesToIeee(lastLBA - firstLBA + 1, blockSize) << " ";
293 if (sizeInIeee.length() < 10)
294 for (i = 0; i < 10 - sizeInIeee.length(); i++)
295 cout << " ";
srs56940a697312010-01-28 21:10:52 -0500296 cout.fill('0');
297 cout.width(4);
298 cout.setf(ios::uppercase);
srs56946699b012010-02-04 00:55:30 -0500299 cout << hex << partitionType.GetHexType() << " " << dec;
srs56940a697312010-01-28 21:10:52 -0500300 cout.fill(' ');
srs569400b6d7a2011-06-26 22:40:06 -0400301#ifdef USE_UTF16
srs5694699941e2011-03-21 21:33:57 -0400302 GetDescription().extractBetween(0, 23, description);
srs56945a608532011-03-17 13:53:01 -0400303 cout << description << "\n";
srs5694699941e2011-03-21 21:33:57 -0400304#else
Roderick W. Smith84aaff62014-02-17 16:17:11 -0500305 string desc = GetDescription() ;
306 size_t n = 0 ;
307 size_t i = 0 ;
308 size_t len = desc.length() ;
309 while ( n < 22 && i < len ) {
310 i ++ ;
311 if ( i >= len ) {
312 // short description
313 break ;
314 } // if
315 // skip continuation bytes
316 while ( i < len && ( ( desc[ i ] & 0xC0 ) == 0x80 ) ) {
317 // utf8 continuation byte
318 i ++ ;
319 } // while
320 n ++ ;
321 } // while
322 if ( i < len ) {
323 n = 0 ;
324 i = 0 ;
325 // description is long we will truncate it
326 while ( n < 19 && i < len ) {
327 i ++ ;
328 if ( i >= len ) {
329 // should not happen
330 break ;
331 } // if
332 // skip continuation bytes
333 while ( i < len && ( ( desc[ i ] & 0xC0 ) == 0x80 ) ) {
334 // utf8 continuation byte
335 i ++ ;
336 } // while
337 n ++ ;
338 } // while
339 } // for
340 cout << GetDescription().substr( 0 , i ) ;
341 if ( i < len ) cout << "..." ;
342 cout << "\n";
srs5694699941e2011-03-21 21:33:57 -0400343#endif
srs56940a697312010-01-28 21:10:52 -0500344 cout.fill(' ');
345 } // if
346} // GPTPart::ShowSummary()
347
348// Show detailed partition information. Does nothing if the partition is
349// empty (as determined by firstLBA being 0).
350void GPTPart::ShowDetails(uint32_t blockSize) {
351 uint64_t size;
352
353 if (firstLBA != 0) {
srs56945a081752010-09-24 20:39:41 -0400354 cout << "Partition GUID code: " << partitionType;
srs56946699b012010-02-04 00:55:30 -0500355 cout << " (" << partitionType.TypeName() << ")\n";
srs56945a081752010-09-24 20:39:41 -0400356 cout << "Partition unique GUID: " << uniqueGUID << "\n";
srs56940a697312010-01-28 21:10:52 -0500357
358 cout << "First sector: " << firstLBA << " (at "
Roderick W. Smithaf39cb42013-08-06 15:23:46 -0400359 << BytesToIeee(firstLBA, blockSize) << ")\n";
srs56940a697312010-01-28 21:10:52 -0500360 cout << "Last sector: " << lastLBA << " (at "
Roderick W. Smithaf39cb42013-08-06 15:23:46 -0400361 << BytesToIeee(lastLBA, blockSize) << ")\n";
srs56940a697312010-01-28 21:10:52 -0500362 size = (lastLBA - firstLBA + 1);
363 cout << "Partition size: " << size << " sectors ("
Roderick W. Smithaf39cb42013-08-06 15:23:46 -0400364 << BytesToIeee(size, blockSize) << ")\n";
srs56940a697312010-01-28 21:10:52 -0500365 cout << "Attribute flags: ";
366 cout.fill('0');
367 cout.width(16);
368 cout << hex;
369 cout << attributes << "\n";
370 cout << dec;
srs5694699941e2011-03-21 21:33:57 -0400371 cout << "Partition name: '" << GetDescription() << "'\n";
srs56940a697312010-01-28 21:10:52 -0500372 cout.fill(' ');
373 } // if
374} // GPTPart::ShowDetails()
375
376// Blank (delete) a single partition
377void GPTPart::BlankPartition(void) {
srs56946699b012010-02-04 00:55:30 -0500378 uniqueGUID.Zero();
379 partitionType.Zero();
srs56940a697312010-01-28 21:10:52 -0500380 firstLBA = 0;
381 lastLBA = 0;
382 attributes = 0;
Roderick W. Smith84aaff62014-02-17 16:17:11 -0500383 memset(name, 0, NAME_SIZE * sizeof( name[0]) );
srs56940a697312010-01-28 21:10:52 -0500384} // GPTPart::BlankPartition
385
386// Returns 1 if the two partitions overlap, 0 if they don't
387int GPTPart::DoTheyOverlap(const GPTPart & other) {
srs56940a697312010-01-28 21:10:52 -0500388 // Don't bother checking unless these are defined (both start and end points
389 // are 0 for undefined partitions, so just check the start points)
srs569464cbd172011-03-01 22:03:54 -0500390 return firstLBA && other.firstLBA &&
391 (firstLBA <= other.lastLBA) != (lastLBA < other.firstLBA);
srs56940a697312010-01-28 21:10:52 -0500392} // GPTPart::DoTheyOverlap()
393
srs56945a608532011-03-17 13:53:01 -0400394// Reverse the bytes of integral data types and of the UTF-16LE name;
395// used on big-endian systems.
srs56940a697312010-01-28 21:10:52 -0500396void GPTPart::ReversePartBytes(void) {
srs56945a608532011-03-17 13:53:01 -0400397 int i;
398
srs56940a697312010-01-28 21:10:52 -0500399 ReverseBytes(&firstLBA, 8);
400 ReverseBytes(&lastLBA, 8);
401 ReverseBytes(&attributes, 8);
Roderick W. Smith84aaff62014-02-17 16:17:11 -0500402 for (i = 0; i < NAME_SIZE; i ++ )
srs56945a608532011-03-17 13:53:01 -0400403 ReverseBytes(name + i, 2);
srs56940a697312010-01-28 21:10:52 -0500404} // GPTPart::ReverseBytes()
405
406/****************************************
407 * Functions requiring user interaction *
408 ****************************************/
409
srs56946699b012010-02-04 00:55:30 -0500410// Change the type code on the partition. Also changes the name if the original
411// name is the generic one for the partition type.
srs56940a697312010-01-28 21:10:52 -0500412void GPTPart::ChangeType(void) {
srs56945a608532011-03-17 13:53:01 -0400413 string line;
srs569464cbd172011-03-01 22:03:54 -0500414 int changeName;
srs569482f3f0b2010-09-22 10:50:24 -0400415 PartType tempType = (GUIDData) "00000000-0000-0000-0000-000000000000";
srs56940a697312010-01-28 21:10:52 -0500416
Roderick W. Smith84aaff62014-02-17 16:17:11 -0500417#ifdef USE_UTF16
srs56945a608532011-03-17 13:53:01 -0400418 changeName = (GetDescription() == GetUTypeName());
Roderick W. Smith84aaff62014-02-17 16:17:11 -0500419#else
420 changeName = (GetDescription() == GetTypeName());
421#endif
srs569455d92612010-03-07 22:16:07 -0500422
srs56946699b012010-02-04 00:55:30 -0500423 cout << "Current type is '" << GetTypeName() << "'\n";
srs569482f3f0b2010-09-22 10:50:24 -0400424 do {
srs56940741fa22013-01-09 12:55:40 -0500425 cout << "Hex code or GUID (L to show codes, Enter = " << hex << DEFAULT_GPT_TYPE << dec << "): ";
srs56945a608532011-03-17 13:53:01 -0400426 line = ReadString();
srs569482f3f0b2010-09-22 10:50:24 -0400427 if ((line[0] == 'L') || (line[0] == 'l')) {
srs56946699b012010-02-04 00:55:30 -0500428 partitionType.ShowAllTypes();
srs569482f3f0b2010-09-22 10:50:24 -0400429 } else {
srs56945a608532011-03-17 13:53:01 -0400430 if (line.length() == 0)
srs56940741fa22013-01-09 12:55:40 -0500431 tempType = DEFAULT_GPT_TYPE;
srs569482f3f0b2010-09-22 10:50:24 -0400432 else
433 tempType = line;
434 } // if/else
435 } while (tempType == (GUIDData) "00000000-0000-0000-0000-000000000000");
436 partitionType = tempType;
srs56946699b012010-02-04 00:55:30 -0500437 cout << "Changed type of partition to '" << partitionType.TypeName() << "'\n";
438 if (changeName) {
439 SetDefaultDescription();
440 } // if
srs56940a697312010-01-28 21:10:52 -0500441} // GPTPart::ChangeType()