srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 1 | // |
| 2 | // C++ Implementation: gptpart |
| 3 | // |
| 4 | // Description: Class to implement a SINGLE GPT partition |
| 5 | // |
| 6 | // |
Rod Smith | 44cda47 | 2018-07-05 09:07:58 -0400 | [diff] [blame] | 7 | // Author: Rod Smith <rodsmith@rodsbooks.com>, (C) 2009-2018 |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 8 | // |
| 9 | // Copyright: See COPYING file that comes with this distribution |
| 10 | // |
| 11 | // |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 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. |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 14 | |
| 15 | #define __STDC_LIMIT_MACROS |
Aurimas Liutikas | fcad060 | 2016-05-10 19:16:10 -0700 | [diff] [blame] | 16 | #ifndef __STDC_CONSTANT_MACROS |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 17 | #define __STDC_CONSTANT_MACROS |
Aurimas Liutikas | fcad060 | 2016-05-10 19:16:10 -0700 | [diff] [blame] | 18 | #endif |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 19 | |
srs5694 | 00b6d7a | 2011-06-26 22:40:06 -0400 | [diff] [blame] | 20 | #ifdef USE_UTF16 |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 21 | #include <unicode/ustdio.h> |
| 22 | #else |
| 23 | #define UnicodeString string |
| 24 | #endif |
| 25 | |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 26 | #include <string.h> |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <iostream> |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 29 | #include "gptpart.h" |
| 30 | #include "attributes.h" |
| 31 | |
| 32 | using namespace std; |
| 33 | |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 34 | GPTPart::GPTPart(void) { |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 35 | partitionType.Zero(); |
| 36 | uniqueGUID.Zero(); |
| 37 | firstLBA = 0; |
| 38 | lastLBA = 0; |
| 39 | attributes = 0; |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame] | 40 | memset(name, 0, NAME_SIZE * sizeof(name[0]) ); |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 41 | } // Default constructor |
| 42 | |
Rod Smith | 9ae6019 | 2018-07-05 16:50:13 -0400 | [diff] [blame] | 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 | |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 52 | GPTPart::~GPTPart(void) { |
| 53 | } // destructor |
| 54 | |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 55 | // Return the gdisk-specific two-byte hex code for the partition |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 56 | uint16_t GPTPart::GetHexType(void) const { |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 57 | return partitionType.GetHexType(); |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 58 | } // GPTPart::GetHexType() |
| 59 | |
| 60 | // Return a plain-text description of the partition type (e.g., "Linux/Windows |
| 61 | // data" or "Linux swap"). |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 62 | string GPTPart::GetTypeName(void) { |
| 63 | return partitionType.TypeName(); |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 64 | } // GPTPart::GetNameType() |
| 65 | |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame] | 66 | #ifdef USE_UTF16 |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 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(); |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 71 | } // GPTPart::GetNameType() |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame] | 72 | #endif |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 73 | |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 74 | // Compute and return the partition's length (or 0 if the end is incorrectly |
| 75 | // set before the beginning). |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 76 | uint64_t GPTPart::GetLengthLBA(void) const { |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 77 | uint64_t length = 0; |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 78 | |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 79 | if (firstLBA <= lastLBA) |
| 80 | length = lastLBA - firstLBA + UINT64_C(1); |
| 81 | return length; |
| 82 | } // GPTPart::GetLengthLBA() |
| 83 | |
srs5694 | 00b6d7a | 2011-06-26 22:40:06 -0400 | [diff] [blame] | 84 | #ifdef USE_UTF16 |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 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 |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame] | 90 | // Return partition's name field, converted to a C++ UTF-8 string |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 91 | string GPTPart::GetDescription(void) { |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame] | 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 |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 145 | #endif |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 146 | |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 147 | // Return 1 if the partition is in use |
| 148 | int GPTPart::IsUsed(void) { |
srs5694 | e69e680 | 2012-01-20 22:37:12 -0500 | [diff] [blame] | 149 | return (partitionType != GUIDData("0x00")); |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 150 | } // GPTPart::IsUsed() |
| 151 | |
Roderick W. Smith | a345a92 | 2014-02-22 12:12:32 -0500 | [diff] [blame] | 152 | // Returns MBR_SIZED_GOOD, MBR_SIZED_IFFY, or MBR_SIZED_BAD; see comments |
| 153 | // in header file for details. |
Roderick W. Smith | 9b338c5 | 2014-02-20 11:13:36 -0500 | [diff] [blame] | 154 | int GPTPart::IsSizedForMBR(void) { |
Roderick W. Smith | a345a92 | 2014-02-22 12:12:32 -0500 | [diff] [blame] | 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); |
Roderick W. Smith | 9b338c5 | 2014-02-20 11:13:36 -0500 | [diff] [blame] | 163 | } // GPTPart::IsSizedForMBR() |
| 164 | |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 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. |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 168 | void GPTPart::SetType(PartType t) { |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame] | 169 | #ifdef USE_UTF16 |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 170 | if (GetDescription() == partitionType.UTypeName()) { |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame] | 171 | #else |
| 172 | if (GetDescription() == partitionType.TypeName()) { |
| 173 | #endif |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 174 | SetName(t.TypeName()); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 175 | } // if |
| 176 | partitionType = t; |
| 177 | } // GPTPart::SetType() |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 178 | |
srs5694 | 00b6d7a | 2011-06-26 22:40:06 -0400 | [diff] [blame] | 179 | #ifdef USE_UTF16 |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 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()); |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 184 | } // GPTPart::SetName() |
| 185 | |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 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 { |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame] | 192 | memset(name, 0, NAME_SIZE * sizeof(name[0]) ); |
| 193 | theName.extractBetween(0, NAME_SIZE, (UChar*) name); |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 194 | } // if/else |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 195 | } // GPTPart::SetName() |
srs5694 | 00b6d7a | 2011-06-26 22:40:06 -0400 | [diff] [blame] | 196 | |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 197 | #else |
srs5694 | 00b6d7a | 2011-06-26 22:40:06 -0400 | [diff] [blame] | 198 | |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 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) { |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame] | 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 |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 271 | #endif |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 272 | |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 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 | |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 279 | GPTPart & GPTPart::operator=(const GPTPart & orig) { |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 280 | partitionType = orig.partitionType; |
| 281 | uniqueGUID = orig.uniqueGUID; |
| 282 | firstLBA = orig.firstLBA; |
| 283 | lastLBA = orig.lastLBA; |
| 284 | attributes = orig.attributes; |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame] | 285 | memcpy(name, orig.name, NAME_SIZE * sizeof( name[ 0 ] ) ); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 286 | return *this; |
| 287 | } // assignment operator |
| 288 | |
srs5694 | 9a46b04 | 2011-03-15 00:34:10 -0400 | [diff] [blame] | 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 { |
srs5694 | 9a46b04 | 2011-03-15 00:34:10 -0400 | [diff] [blame] | 295 | if (firstLBA && other.firstLBA) |
| 296 | return (firstLBA < other.firstLBA); |
| 297 | else |
| 298 | return (other.firstLBA < firstLBA); |
| 299 | } // GPTPart::operator<() |
| 300 | |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 301 | // Display summary information; does nothing if the partition is empty. |
| 302 | void GPTPart::ShowSummary(int partNum, uint32_t blockSize) { |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 303 | string sizeInIeee; |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 304 | UnicodeString description; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 305 | size_t i; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 306 | |
| 307 | if (firstLBA != 0) { |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 308 | sizeInIeee = BytesToIeee(lastLBA - firstLBA + 1, blockSize); |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 309 | cout.fill(' '); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 310 | cout.width(4); |
| 311 | cout << partNum + 1 << " "; |
| 312 | cout.width(14); |
| 313 | cout << firstLBA << " "; |
| 314 | cout.width(14); |
| 315 | cout << lastLBA << " "; |
Roderick W. Smith | f694803 | 2014-03-29 00:27:33 -0400 | [diff] [blame] | 316 | cout << sizeInIeee << " "; |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 317 | if (sizeInIeee.length() < 10) |
| 318 | for (i = 0; i < 10 - sizeInIeee.length(); i++) |
| 319 | cout << " "; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 320 | cout.fill('0'); |
| 321 | cout.width(4); |
| 322 | cout.setf(ios::uppercase); |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 323 | cout << hex << partitionType.GetHexType() << " " << dec; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 324 | cout.fill(' '); |
srs5694 | 00b6d7a | 2011-06-26 22:40:06 -0400 | [diff] [blame] | 325 | #ifdef USE_UTF16 |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 326 | GetDescription().extractBetween(0, 23, description); |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 327 | cout << description << "\n"; |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 328 | #else |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame] | 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"; |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 367 | #endif |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 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). |
Jeff Sharkey | d761ff5 | 2015-02-28 19:18:39 -0800 | [diff] [blame] | 374 | void GPTPart::ShowDetails(uint32_t blockSize) { |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 375 | uint64_t size; |
| 376 | |
| 377 | if (firstLBA != 0) { |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 378 | cout << "Partition GUID code: " << partitionType; |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 379 | cout << " (" << partitionType.TypeName() << ")\n"; |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 380 | cout << "Partition unique GUID: " << uniqueGUID << "\n"; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 381 | |
| 382 | cout << "First sector: " << firstLBA << " (at " |
Roderick W. Smith | af39cb4 | 2013-08-06 15:23:46 -0400 | [diff] [blame] | 383 | << BytesToIeee(firstLBA, blockSize) << ")\n"; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 384 | cout << "Last sector: " << lastLBA << " (at " |
Roderick W. Smith | af39cb4 | 2013-08-06 15:23:46 -0400 | [diff] [blame] | 385 | << BytesToIeee(lastLBA, blockSize) << ")\n"; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 386 | size = (lastLBA - firstLBA + 1); |
| 387 | cout << "Partition size: " << size << " sectors (" |
Roderick W. Smith | af39cb4 | 2013-08-06 15:23:46 -0400 | [diff] [blame] | 388 | << BytesToIeee(size, blockSize) << ")\n"; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 389 | cout << "Attribute flags: "; |
| 390 | cout.fill('0'); |
| 391 | cout.width(16); |
| 392 | cout << hex; |
| 393 | cout << attributes << "\n"; |
| 394 | cout << dec; |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 395 | cout << "Partition name: '" << GetDescription() << "'\n"; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 396 | cout.fill(' '); |
| 397 | } // if |
| 398 | } // GPTPart::ShowDetails() |
| 399 | |
| 400 | // Blank (delete) a single partition |
| 401 | void GPTPart::BlankPartition(void) { |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 402 | uniqueGUID.Zero(); |
| 403 | partitionType.Zero(); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 404 | firstLBA = 0; |
| 405 | lastLBA = 0; |
| 406 | attributes = 0; |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame] | 407 | memset(name, 0, NAME_SIZE * sizeof( name[0]) ); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 408 | } // GPTPart::BlankPartition |
| 409 | |
| 410 | // Returns 1 if the two partitions overlap, 0 if they don't |
| 411 | int GPTPart::DoTheyOverlap(const GPTPart & other) { |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 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) |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 414 | return firstLBA && other.firstLBA && |
| 415 | (firstLBA <= other.lastLBA) != (lastLBA < other.firstLBA); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 416 | } // GPTPart::DoTheyOverlap() |
| 417 | |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 418 | // Reverse the bytes of integral data types and of the UTF-16LE name; |
| 419 | // used on big-endian systems. |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 420 | void GPTPart::ReversePartBytes(void) { |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 421 | int i; |
| 422 | |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 423 | ReverseBytes(&firstLBA, 8); |
| 424 | ReverseBytes(&lastLBA, 8); |
| 425 | ReverseBytes(&attributes, 8); |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame] | 426 | for (i = 0; i < NAME_SIZE; i ++ ) |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 427 | ReverseBytes(name + i, 2); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 428 | } // GPTPart::ReverseBytes() |
| 429 | |
| 430 | /**************************************** |
| 431 | * Functions requiring user interaction * |
| 432 | ****************************************/ |
| 433 | |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 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. |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 436 | void GPTPart::ChangeType(void) { |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 437 | string line; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 438 | int changeName; |
srs5694 | 82f3f0b | 2010-09-22 10:50:24 -0400 | [diff] [blame] | 439 | PartType tempType = (GUIDData) "00000000-0000-0000-0000-000000000000"; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 440 | |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame] | 441 | #ifdef USE_UTF16 |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 442 | changeName = (GetDescription() == GetUTypeName()); |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame] | 443 | #else |
| 444 | changeName = (GetDescription() == GetTypeName()); |
| 445 | #endif |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 446 | |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 447 | cout << "Current type is '" << GetTypeName() << "'\n"; |
srs5694 | 82f3f0b | 2010-09-22 10:50:24 -0400 | [diff] [blame] | 448 | do { |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame] | 449 | cout << "Hex code or GUID (L to show codes, Enter = " << hex << DEFAULT_GPT_TYPE << dec << "): "; |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 450 | line = ReadString(); |
srs5694 | 82f3f0b | 2010-09-22 10:50:24 -0400 | [diff] [blame] | 451 | if ((line[0] == 'L') || (line[0] == 'l')) { |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 452 | partitionType.ShowAllTypes(); |
srs5694 | 82f3f0b | 2010-09-22 10:50:24 -0400 | [diff] [blame] | 453 | } else { |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 454 | if (line.length() == 0) |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame] | 455 | tempType = DEFAULT_GPT_TYPE; |
srs5694 | 82f3f0b | 2010-09-22 10:50:24 -0400 | [diff] [blame] | 456 | else |
| 457 | tempType = line; |
| 458 | } // if/else |
| 459 | } while (tempType == (GUIDData) "00000000-0000-0000-0000-000000000000"); |
| 460 | partitionType = tempType; |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 461 | cout << "Changed type of partition to '" << partitionType.TypeName() << "'\n"; |
| 462 | if (changeName) { |
| 463 | SetDefaultDescription(); |
| 464 | } // if |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 465 | } // GPTPart::ChangeType() |