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