1 //
2 // C++ Implementation: gptpart
3 //
4 // Description: Class to implement a SINGLE GPT partition
5 //
6 //
7 // Author: Rod Smith <[email protected]>, (C) 2009-2018
8 //
9 // Copyright: See COPYING file that comes with this distribution
10 //
11 //
12 // 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.
14
15 #define __STDC_LIMIT_MACROS
16 #define __STDC_CONSTANT_MACROS
17
18 #include <string.h>
19 #include <stdio.h>
20 #include <iostream>
21 #include "gptpart.h"
22 #include "attributes.h"
23 #ifdef USE_UTF16
24 #include <unicode/ustdio.h>
25 #else
26 #define UnicodeString std::string
27 #endif
28
29 using namespace std;
30
GPTPart(void)31 GPTPart::GPTPart(void) {
32 partitionType.Zero();
33 uniqueGUID.Zero();
34 firstLBA = 0;
35 lastLBA = 0;
36 attributes = 0;
37 memset(name, 0, NAME_SIZE * sizeof(name[0]) );
38 } // Default constructor
39
GPTPart(const GPTPart & orig)40 GPTPart::GPTPart(const GPTPart & orig) {
41 partitionType = orig.partitionType;
42 uniqueGUID = orig.uniqueGUID;
43 firstLBA = orig.firstLBA;
44 lastLBA = orig.lastLBA;
45 attributes = orig.attributes;
46 memcpy(name, orig.name, NAME_SIZE * sizeof( name[ 0 ] ) );
47 } // Copy constructor
48
~GPTPart(void)49 GPTPart::~GPTPart(void) {
50 } // destructor
51
52 // Return the gdisk-specific two-byte hex code for the partition
GetHexType(void) const53 uint16_t GPTPart::GetHexType(void) const {
54 return partitionType.GetHexType();
55 } // GPTPart::GetHexType()
56
57 // Return a plain-text description of the partition type (e.g., "Linux/Windows
58 // data" or "Linux swap").
GetTypeName(void)59 string GPTPart::GetTypeName(void) {
60 return partitionType.TypeName();
61 } // GPTPart::GetNameType()
62
63 #ifdef USE_UTF16
64 // Return a Unicode description of the partition type (e.g., "Linux/Windows
65 // data" or "Linux swap").
GetUTypeName(void)66 UnicodeString GPTPart::GetUTypeName(void) {
67 return partitionType.UTypeName();
68 } // GPTPart::GetNameType()
69 #endif
70
71 // Compute and return the partition's length (or 0 if the end is incorrectly
72 // set before the beginning).
GetLengthLBA(void) const73 uint64_t GPTPart::GetLengthLBA(void) const {
74 uint64_t length = 0;
75
76 if (firstLBA <= lastLBA)
77 length = lastLBA - firstLBA + UINT64_C(1);
78 return length;
79 } // GPTPart::GetLengthLBA()
80
81 #ifdef USE_UTF16
82 // Return partition's name field, converted to a Unicode string
GetDescription(void)83 UnicodeString GPTPart::GetDescription(void) {
84 return (UChar*) name;
85 } // GPTPart::GetDescription()
86 #else
87 // Return partition's name field, converted to a C++ UTF-8 string
GetDescription(void)88 string GPTPart::GetDescription(void) {
89 // convert name to utf32 then to utf8
90 string utf8 ;
91 size_t pos = 0 ;
92 while ( ( pos < NAME_SIZE ) && ( name[ pos ] != 0 ) ) {
93 uint16_t cp = name[ pos ++ ] ;
94 // first to utf32
95 uint32_t uni ;
96 if ( cp < 0xd800 || cp > 0xdfff ) {
97 uni = cp ;
98 } // if
99 else if ( cp < 0xdc00 ) {
100 // lead surrogate
101 uni = ( (uint32_t)( cp & 0x3ff ) ) << 10 ;
102 if ( pos >= NAME_SIZE ) {
103 // missing trail surrogate, name[] is invalid
104 break ;
105 } // if
106 cp = name[ pos ++ ] ;
107 if ( cp < 0xdc00 || cp > 0xdfff ) {
108 // invalid trail surrogate, name[] is invalid
109 break ;
110 } // if
111 // trail surrogate
112 uni |= cp & 0x3ff ;
113 uni += 0x10000 ;
114 } // if
115 else {
116 // unexpected trail surrogate, name[] is invalid
117 break ;
118 } // if
119 // then to utf8
120 if ( uni < 0x80 ) {
121 utf8 += (char) uni ;
122 } // if
123 else if ( uni < 0x800 ) {
124 utf8 += (char) ( 0xc0 | ( uni >> 6 ) ) ;
125 utf8 += (char) ( 0x80 | ( uni & 0x3f ) ) ;
126 } // if
127 else if ( uni < 0x10000 ) {
128 utf8 += (char) ( 0xe0 | ( uni >> 12 ) ) ;
129 utf8 += (char) ( 0x80 | ( ( uni >> 6 ) & 0x3f ) ) ;
130 utf8 += (char) ( 0x80 | ( uni & 0x3f ) ) ;
131 } // if
132 else {
133 utf8 += (char) ( 0xf0 | ( uni >> 18 ) ) ;
134 utf8 += (char) ( 0xe0 | ( ( uni >> 12 ) & 0x3f ) ) ;
135 utf8 += (char) ( 0x80 | ( ( uni >> 6 ) & 0x3f ) ) ;
136 utf8 += (char) ( 0x80 | ( uni & 0x3f ) ) ;
137 } // if
138 }
139 return utf8 ;
140 } // GPTPart::GetDescription(), UTF-8 version
141 #endif
142
143 // Return 1 if the partition is in use
IsUsed(void)144 int GPTPart::IsUsed(void) {
145 return (partitionType != GUIDData("0x00"));
146 } // GPTPart::IsUsed()
147
148 // Returns MBR_SIZED_GOOD, MBR_SIZED_IFFY, or MBR_SIZED_BAD; see comments
149 // in header file for details.
IsSizedForMBR(void)150 int GPTPart::IsSizedForMBR(void) {
151 int retval = MBR_SIZED_GOOD;
152
153 if ((firstLBA > UINT32_MAX) || ((lastLBA - firstLBA) > UINT32_MAX) || (firstLBA > lastLBA))
154 retval = MBR_SIZED_BAD;
155 else if (lastLBA > UINT32_MAX)
156 retval = MBR_SIZED_IFFY;
157
158 return (retval);
159 } // GPTPart::IsSizedForMBR()
160
161 // Set the type code to the specified one. Also changes the partition
162 // name *IF* the current name is the generic one for the current partition
163 // type.
SetType(PartType t)164 void GPTPart::SetType(PartType t) {
165 #ifdef USE_UTF16
166 if (GetDescription() == partitionType.UTypeName()) {
167 #else
168 if (GetDescription() == partitionType.TypeName()) {
169 #endif
170 SetName(t.TypeName());
171 } // if
172 partitionType = t;
173 } // GPTPart::SetType()
174
175 #ifdef USE_UTF16
176 // Set the name for a partition to theName, using a C++-style string as
177 // input.
178 void GPTPart::SetName(const string & theName) {
179 SetName((UnicodeString) theName.c_str());
180 } // GPTPart::SetName()
181
182 // Set the name for a partition to theName, using a Unicode string as
183 // input.
184 void GPTPart::SetName(const UnicodeString & theName) {
185 if (theName.isBogus()) {
186 cerr << "Bogus UTF-16 name found in GPTPart::SetName()! Name not changed!\n";
187 } else {
188 memset(name, 0, NAME_SIZE * sizeof(name[0]) );
189 theName.extractBetween(0, NAME_SIZE, (UChar*) name);
190 } // if/else
191 } // GPTPart::SetName()
192
193 #else
194
195 // Set the name for a partition to theName. Note that theName is a
196 // standard C++-style ASCII string, although the GUID partition definition
197 // requires a UTF-16LE string. This function creates a simple-minded copy
198 // for this.
199 void GPTPart::SetName(const string & theName) {
200 // convert utf8 to utf32 then to utf16le
201 size_t len = theName.length() ;
202 size_t pos = 0 ;
203 for ( size_t i = 0 ; pos < NAME_SIZE && i < len ; ) {
204 uint32_t uni ;
205 uint8_t cp = theName[ i ++ ] ;
206 int todo ;
207 if ( cp < 0x80 ) {
208 uni = cp ;
209 todo = 0 ;
210 } // if
211 else if ( cp < 0xc0 || cp > 0xf7 ) {
212 // invalid byte, theName is broken
213 break ;
214 } // if
215 else if ( cp < 0xe0 ) {
216 uni = cp & 0x1f ;
217 todo = 1 ;
218 } // if
219 else if ( cp < 0xf0 ) {
220 uni = cp & 0x0f ;
221 todo = 2 ;
222 } // if
223 else {
224 uni = cp & 0x7 ;
225 todo = 3 ;
226 } // if
227 while ( todo > 0 ) {
228 if ( i >= len ) {
229 // missing continuation byte, theName is broken
230 goto break_converter ;
231 } // if
232 cp = theName[ i ++ ] ;
233 if ( cp > 0xbf || cp < 0x80 ) {
234 // invalid continuation byte, theName is broken
235 goto break_converter ;
236 } // if
237 uni <<= 6 ;
238 uni |= cp & 0x3f ;
239 todo -- ;
240 } // while
241 // then to utf16le
242 if ( uni < 0x10000 ) {
243 name[ pos ] = (uint16_t) uni ;
244 pos ++ ;
245 } // if
246 else {
247 if ( pos > NAME_SIZE - 2 ) {
248 // not enough room for two surrogates, truncate
249 break ;
250 } // if
251 uni -= 0x10000 ;
252 name[ pos ] = (uint16_t)( uni >> 10 ) | 0xd800 ;
253 pos ++ ;
254 name[ pos ] = (uint16_t)( uni & 0x3ff ) | 0xdc00 ;
255 pos ++ ;
256 }
257 } // for
258 break_converter : ;
259 // finally fill with zeroes
260 while ( pos < NAME_SIZE ) {
261 name[ pos ++ ] = 0 ;
262 } // while
263 } // GPTPart::SetName(), UTF-8 version
264 #endif
265
266 // Set the name for the partition based on the current GUID partition type
267 // code's associated name
268 void GPTPart::SetDefaultDescription(void) {
269 SetName(partitionType.TypeName());
270 } // GPTPart::SetDefaultDescription()
271
272 GPTPart & GPTPart::operator=(const GPTPart & orig) {
273 partitionType = orig.partitionType;
274 uniqueGUID = orig.uniqueGUID;
275 firstLBA = orig.firstLBA;
276 lastLBA = orig.lastLBA;
277 attributes = orig.attributes;
278 memcpy(name, orig.name, NAME_SIZE * sizeof( name[ 0 ] ) );
279 return *this;
280 } // assignment operator
281
282 // Compare the values, and return a bool result.
283 // Because this is intended for sorting and a firstLBA value of 0 denotes
284 // a partition that's not in use and so that should be sorted upwards,
285 // we return the opposite of the usual arithmetic result when either
286 // firstLBA value is 0.
287 bool GPTPart::operator<(const GPTPart &other) const {
288 if (firstLBA && other.firstLBA)
289 return (firstLBA < other.firstLBA);
290 else
291 return (other.firstLBA < firstLBA);
292 } // GPTPart::operator<()
293
294 // Display summary information; does nothing if the partition is empty.
295 void GPTPart::ShowSummary(int partNum, uint32_t blockSize) {
296 string sizeInIeee;
297 UnicodeString description;
298 size_t i;
299
300 if (firstLBA != 0) {
301 sizeInIeee = BytesToIeee(lastLBA - firstLBA + 1, blockSize);
302 cout.fill(' ');
303 cout.width(4);
304 cout << partNum + 1 << " ";
305 cout.width(14);
306 cout << firstLBA << " ";
307 cout.width(14);
308 cout << lastLBA << " ";
309 cout << sizeInIeee << " ";
310 if (sizeInIeee.length() < 10)
311 for (i = 0; i < 10 - sizeInIeee.length(); i++)
312 cout << " ";
313 cout.fill('0');
314 cout.width(4);
315 cout.setf(ios::uppercase);
316 cout << hex << partitionType.GetHexType() << " " << dec;
317 cout.fill(' ');
318 #ifdef USE_UTF16
319 GetDescription().extractBetween(0, 23, description);
320 cout << description << "\n";
321 #else
322 string desc = GetDescription() ;
323 size_t n = 0 ;
324 size_t i = 0 ;
325 size_t len = desc.length() ;
326 while ( n < 22 && i < len ) {
327 i ++ ;
328 if ( i >= len ) {
329 // short description
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 if ( i < len ) {
340 n = 0 ;
341 i = 0 ;
342 // description is long we will truncate it
343 while ( n < 19 && i < len ) {
344 i ++ ;
345 if ( i >= len ) {
346 // should not happen
347 break ;
348 } // if
349 // skip continuation bytes
350 while ( i < len && ( ( desc[ i ] & 0xC0 ) == 0x80 ) ) {
351 // utf8 continuation byte
352 i ++ ;
353 } // while
354 n ++ ;
355 } // while
356 } // for
357 cout << GetDescription().substr( 0 , i ) ;
358 if ( i < len ) cout << "..." ;
359 cout << "\n";
360 #endif
361 cout.fill(' ');
362 } // if
363 } // GPTPart::ShowSummary()
364
365 // Show detailed partition information. Does nothing if the partition is
366 // empty (as determined by firstLBA being 0).
367 void GPTPart::ShowDetails(uint32_t blockSize) {
368 uint64_t size;
369
370 if (firstLBA != 0) {
371 cout << "Partition GUID code: " << partitionType;
372 cout << " (" << partitionType.TypeName() << ")\n";
373 cout << "Partition unique GUID: " << uniqueGUID << "\n";
374
375 cout << "First sector: " << firstLBA << " (at "
376 << BytesToIeee(firstLBA, blockSize) << ")\n";
377 cout << "Last sector: " << lastLBA << " (at "
378 << BytesToIeee(lastLBA, blockSize) << ")\n";
379 size = (lastLBA - firstLBA + 1);
380 cout << "Partition size: " << size << " sectors ("
381 << BytesToIeee(size, blockSize) << ")\n";
382 cout << "Attribute flags: ";
383 cout.fill('0');
384 cout.width(16);
385 cout << hex;
386 cout << attributes << "\n";
387 cout << dec;
388 cout << "Partition name: '" << GetDescription() << "'\n";
389 cout.fill(' ');
390 } // if
391 } // GPTPart::ShowDetails()
392
393 // Blank (delete) a single partition
394 void GPTPart::BlankPartition(void) {
395 uniqueGUID.Zero();
396 partitionType.Zero();
397 firstLBA = 0;
398 lastLBA = 0;
399 attributes = 0;
400 memset(name, 0, NAME_SIZE * sizeof( name[0]) );
401 } // GPTPart::BlankPartition
402
403 // Returns 1 if the two partitions overlap, 0 if they don't
404 int GPTPart::DoTheyOverlap(const GPTPart & other) {
405 // Don't bother checking unless these are defined (both start and end points
406 // are 0 for undefined partitions, so just check the start points)
407 return firstLBA && other.firstLBA &&
408 (firstLBA <= other.lastLBA) != (lastLBA < other.firstLBA);
409 } // GPTPart::DoTheyOverlap()
410
411 // Reverse the bytes of integral data types and of the UTF-16LE name;
412 // used on big-endian systems.
413 void GPTPart::ReversePartBytes(void) {
414 ReverseBytes(&firstLBA, 8);
415 ReverseBytes(&lastLBA, 8);
416 ReverseBytes(&attributes, 8);
417 ReverseNameBytes();
418 } // GPTPart::ReversePartBytes()
419
420 void GPTPart::ReverseNameBytes(void) {
421 int i;
422
423 for (i = 0; i < NAME_SIZE; i ++ )
424 ReverseBytes(name + i, 2);
425 } // GPTPart::ReverseNameBytes()
426
427 /****************************************
428 * Functions requiring user interaction *
429 ****************************************/
430
431 // Change the type code on the partition. Also changes the name if the original
432 // name is the generic one for the partition type.
433 void GPTPart::ChangeType(void) {
434 string line;
435 int changeName;
436 PartType tempType = PartType::unusedPartType;
437
438 #ifdef USE_UTF16
439 changeName = (GetDescription() == GetUTypeName());
440 #else
441 changeName = (GetDescription() == GetTypeName());
442 #endif
443
444 cout << "Current type is " << hex << GetHexType() << dec << " (" << GetTypeName() << ")\n";
445 do {
446 cout << "Hex code or GUID (L to show codes, Enter = " << hex << GetHexType() << dec << "): ";
447 line = ReadString();
448 if ((line[0] == 'L') || (line[0] == 'l')) {
449 partitionType.ShowAllTypes();
450 } else {
451 if (line.length() == 0)
452 tempType = GetHexType();
453 else
454 tempType = line;
455 } // if/else
456 } while (tempType == PartType::unusedPartType);
457 partitionType = tempType;
458 cout << "Changed type of partition to '" << partitionType.TypeName() << "'\n";
459 if (changeName) {
460 SetDefaultDescription();
461 } // if
462 } // GPTPart::ChangeType()
463