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