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