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