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 | |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 141 | // Set the type code to the specified one. Also changes the partition |
| 142 | // name *IF* the current name is the generic one for the current partition |
| 143 | // type. |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 144 | void GPTPart::SetType(PartType t) { |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame^] | 145 | #ifdef USE_UTF16 |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 146 | if (GetDescription() == partitionType.UTypeName()) { |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame^] | 147 | #else |
| 148 | if (GetDescription() == partitionType.TypeName()) { |
| 149 | #endif |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 150 | SetName(t.TypeName()); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 151 | } // if |
| 152 | partitionType = t; |
| 153 | } // GPTPart::SetType() |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 154 | |
srs5694 | 00b6d7a | 2011-06-26 22:40:06 -0400 | [diff] [blame] | 155 | #ifdef USE_UTF16 |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 156 | // Set the name for a partition to theName, using a C++-style string as |
| 157 | // input. |
| 158 | void GPTPart::SetName(const string & theName) { |
| 159 | SetName((UnicodeString) theName.c_str()); |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 160 | } // GPTPart::SetName() |
| 161 | |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 162 | // Set the name for a partition to theName, using a Unicode string as |
| 163 | // input. |
| 164 | void GPTPart::SetName(const UnicodeString & theName) { |
| 165 | if (theName.isBogus()) { |
| 166 | cerr << "Bogus UTF-16 name found in GPTPart::SetName()! Name not changed!\n"; |
| 167 | } else { |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame^] | 168 | memset(name, 0, NAME_SIZE * sizeof(name[0]) ); |
| 169 | theName.extractBetween(0, NAME_SIZE, (UChar*) name); |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 170 | } // if/else |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 171 | } // GPTPart::SetName() |
srs5694 | 00b6d7a | 2011-06-26 22:40:06 -0400 | [diff] [blame] | 172 | |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 173 | #else |
srs5694 | 00b6d7a | 2011-06-26 22:40:06 -0400 | [diff] [blame] | 174 | |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 175 | // Set the name for a partition to theName. Note that theName is a |
| 176 | // standard C++-style ASCII string, although the GUID partition definition |
| 177 | // requires a UTF-16LE string. This function creates a simple-minded copy |
| 178 | // for this. |
| 179 | void GPTPart::SetName(const string & theName) { |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame^] | 180 | // convert utf8 to utf32 then to utf16le |
| 181 | size_t len = theName.length() ; |
| 182 | size_t pos = 0 ; |
| 183 | for ( size_t i = 0 ; pos < NAME_SIZE && i < len ; ) { |
| 184 | uint32_t uni ; |
| 185 | uint8_t cp = theName[ i ++ ] ; |
| 186 | int todo ; |
| 187 | if ( cp < 0x80 ) { |
| 188 | uni = cp ; |
| 189 | todo = 0 ; |
| 190 | } // if |
| 191 | else if ( cp < 0xc0 || cp > 0xf7 ) { |
| 192 | // invalid byte, theName is broken |
| 193 | break ; |
| 194 | } // if |
| 195 | else if ( cp < 0xe0 ) { |
| 196 | uni = cp & 0x1f ; |
| 197 | todo = 1 ; |
| 198 | } // if |
| 199 | else if ( cp < 0xf0 ) { |
| 200 | uni = cp & 0x0f ; |
| 201 | todo = 2 ; |
| 202 | } // if |
| 203 | else { |
| 204 | uni = cp & 0x7 ; |
| 205 | todo = 3 ; |
| 206 | } // if |
| 207 | while ( todo > 0 ) { |
| 208 | if ( i >= len ) { |
| 209 | // missing continuation byte, theName is broken |
| 210 | goto break_converter ; |
| 211 | } // if |
| 212 | cp = theName[ i ++ ] ; |
| 213 | if ( cp > 0xbf || cp < 0x80 ) { |
| 214 | // invalid continuation byte, theName is broken |
| 215 | goto break_converter ; |
| 216 | } // if |
| 217 | uni <<= 6 ; |
| 218 | uni |= cp & 0x3f ; |
| 219 | todo -- ; |
| 220 | } // while |
| 221 | // then to utf16le |
| 222 | if ( uni < 0x10000 ) { |
| 223 | name[ pos ] = (uint16_t) uni ; |
| 224 | if ( ! IsLittleEndian() ) ReverseBytes( name + pos , 2 ) ; |
| 225 | pos ++ ; |
| 226 | } // if |
| 227 | else { |
| 228 | if ( pos > NAME_SIZE - 2 ) { |
| 229 | // not enough room for two surrogates, truncate |
| 230 | break ; |
| 231 | } // if |
| 232 | uni -= 0x10000 ; |
| 233 | name[ pos ] = (uint16_t)( uni >> 10 ) | 0xd800 ; |
| 234 | if ( ! IsLittleEndian() ) ReverseBytes( name + pos , 2 ) ; |
| 235 | pos ++ ; |
| 236 | name[ pos ] = (uint16_t)( uni & 0x3ff ) | 0xdc00 ; |
| 237 | if ( ! IsLittleEndian() ) ReverseBytes( name + pos , 2 ) ; |
| 238 | pos ++ ; |
| 239 | } |
| 240 | } // for |
| 241 | break_converter : ; |
| 242 | // finally fill with zeroes |
| 243 | while ( pos < NAME_SIZE ) { |
| 244 | name[ pos ++ ] = 0 ; |
| 245 | } // while |
| 246 | } // GPTPart::SetName(), UTF-8 version |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 247 | #endif |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 248 | |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 249 | // Set the name for the partition based on the current GUID partition type |
| 250 | // code's associated name |
| 251 | void GPTPart::SetDefaultDescription(void) { |
| 252 | SetName(partitionType.TypeName()); |
| 253 | } // GPTPart::SetDefaultDescription() |
| 254 | |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 255 | GPTPart & GPTPart::operator=(const GPTPart & orig) { |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 256 | partitionType = orig.partitionType; |
| 257 | uniqueGUID = orig.uniqueGUID; |
| 258 | firstLBA = orig.firstLBA; |
| 259 | lastLBA = orig.lastLBA; |
| 260 | attributes = orig.attributes; |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame^] | 261 | memcpy(name, orig.name, NAME_SIZE * sizeof( name[ 0 ] ) ); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 262 | return *this; |
| 263 | } // assignment operator |
| 264 | |
srs5694 | 9a46b04 | 2011-03-15 00:34:10 -0400 | [diff] [blame] | 265 | // Compare the values, and return a bool result. |
| 266 | // Because this is intended for sorting and a firstLBA value of 0 denotes |
| 267 | // a partition that's not in use and so that should be sorted upwards, |
| 268 | // we return the opposite of the usual arithmetic result when either |
| 269 | // firstLBA value is 0. |
| 270 | bool GPTPart::operator<(const GPTPart &other) const { |
srs5694 | 9a46b04 | 2011-03-15 00:34:10 -0400 | [diff] [blame] | 271 | if (firstLBA && other.firstLBA) |
| 272 | return (firstLBA < other.firstLBA); |
| 273 | else |
| 274 | return (other.firstLBA < firstLBA); |
| 275 | } // GPTPart::operator<() |
| 276 | |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 277 | // Display summary information; does nothing if the partition is empty. |
| 278 | void GPTPart::ShowSummary(int partNum, uint32_t blockSize) { |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 279 | string sizeInIeee; |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 280 | UnicodeString description; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 281 | size_t i; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 282 | |
| 283 | if (firstLBA != 0) { |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 284 | sizeInIeee = BytesToIeee(lastLBA - firstLBA + 1, blockSize); |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 285 | cout.fill(' '); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 286 | cout.width(4); |
| 287 | cout << partNum + 1 << " "; |
| 288 | cout.width(14); |
| 289 | cout << firstLBA << " "; |
| 290 | cout.width(14); |
| 291 | cout << lastLBA << " "; |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 292 | cout << BytesToIeee(lastLBA - firstLBA + 1, blockSize) << " "; |
| 293 | if (sizeInIeee.length() < 10) |
| 294 | for (i = 0; i < 10 - sizeInIeee.length(); i++) |
| 295 | cout << " "; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 296 | cout.fill('0'); |
| 297 | cout.width(4); |
| 298 | cout.setf(ios::uppercase); |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 299 | cout << hex << partitionType.GetHexType() << " " << dec; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 300 | cout.fill(' '); |
srs5694 | 00b6d7a | 2011-06-26 22:40:06 -0400 | [diff] [blame] | 301 | #ifdef USE_UTF16 |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 302 | GetDescription().extractBetween(0, 23, description); |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 303 | cout << description << "\n"; |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 304 | #else |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame^] | 305 | string desc = GetDescription() ; |
| 306 | size_t n = 0 ; |
| 307 | size_t i = 0 ; |
| 308 | size_t len = desc.length() ; |
| 309 | while ( n < 22 && i < len ) { |
| 310 | i ++ ; |
| 311 | if ( i >= len ) { |
| 312 | // short description |
| 313 | break ; |
| 314 | } // if |
| 315 | // skip continuation bytes |
| 316 | while ( i < len && ( ( desc[ i ] & 0xC0 ) == 0x80 ) ) { |
| 317 | // utf8 continuation byte |
| 318 | i ++ ; |
| 319 | } // while |
| 320 | n ++ ; |
| 321 | } // while |
| 322 | if ( i < len ) { |
| 323 | n = 0 ; |
| 324 | i = 0 ; |
| 325 | // description is long we will truncate it |
| 326 | while ( n < 19 && i < len ) { |
| 327 | i ++ ; |
| 328 | if ( i >= len ) { |
| 329 | // should not happen |
| 330 | break ; |
| 331 | } // if |
| 332 | // skip continuation bytes |
| 333 | while ( i < len && ( ( desc[ i ] & 0xC0 ) == 0x80 ) ) { |
| 334 | // utf8 continuation byte |
| 335 | i ++ ; |
| 336 | } // while |
| 337 | n ++ ; |
| 338 | } // while |
| 339 | } // for |
| 340 | cout << GetDescription().substr( 0 , i ) ; |
| 341 | if ( i < len ) cout << "..." ; |
| 342 | cout << "\n"; |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 343 | #endif |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 344 | cout.fill(' '); |
| 345 | } // if |
| 346 | } // GPTPart::ShowSummary() |
| 347 | |
| 348 | // Show detailed partition information. Does nothing if the partition is |
| 349 | // empty (as determined by firstLBA being 0). |
| 350 | void GPTPart::ShowDetails(uint32_t blockSize) { |
| 351 | uint64_t size; |
| 352 | |
| 353 | if (firstLBA != 0) { |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 354 | cout << "Partition GUID code: " << partitionType; |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 355 | cout << " (" << partitionType.TypeName() << ")\n"; |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 356 | cout << "Partition unique GUID: " << uniqueGUID << "\n"; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 357 | |
| 358 | cout << "First sector: " << firstLBA << " (at " |
Roderick W. Smith | af39cb4 | 2013-08-06 15:23:46 -0400 | [diff] [blame] | 359 | << BytesToIeee(firstLBA, blockSize) << ")\n"; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 360 | cout << "Last sector: " << lastLBA << " (at " |
Roderick W. Smith | af39cb4 | 2013-08-06 15:23:46 -0400 | [diff] [blame] | 361 | << BytesToIeee(lastLBA, blockSize) << ")\n"; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 362 | size = (lastLBA - firstLBA + 1); |
| 363 | cout << "Partition size: " << size << " sectors (" |
Roderick W. Smith | af39cb4 | 2013-08-06 15:23:46 -0400 | [diff] [blame] | 364 | << BytesToIeee(size, blockSize) << ")\n"; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 365 | cout << "Attribute flags: "; |
| 366 | cout.fill('0'); |
| 367 | cout.width(16); |
| 368 | cout << hex; |
| 369 | cout << attributes << "\n"; |
| 370 | cout << dec; |
srs5694 | 699941e | 2011-03-21 21:33:57 -0400 | [diff] [blame] | 371 | cout << "Partition name: '" << GetDescription() << "'\n"; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 372 | cout.fill(' '); |
| 373 | } // if |
| 374 | } // GPTPart::ShowDetails() |
| 375 | |
| 376 | // Blank (delete) a single partition |
| 377 | void GPTPart::BlankPartition(void) { |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 378 | uniqueGUID.Zero(); |
| 379 | partitionType.Zero(); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 380 | firstLBA = 0; |
| 381 | lastLBA = 0; |
| 382 | attributes = 0; |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame^] | 383 | memset(name, 0, NAME_SIZE * sizeof( name[0]) ); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 384 | } // GPTPart::BlankPartition |
| 385 | |
| 386 | // Returns 1 if the two partitions overlap, 0 if they don't |
| 387 | int GPTPart::DoTheyOverlap(const GPTPart & other) { |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 388 | // Don't bother checking unless these are defined (both start and end points |
| 389 | // are 0 for undefined partitions, so just check the start points) |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 390 | return firstLBA && other.firstLBA && |
| 391 | (firstLBA <= other.lastLBA) != (lastLBA < other.firstLBA); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 392 | } // GPTPart::DoTheyOverlap() |
| 393 | |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 394 | // Reverse the bytes of integral data types and of the UTF-16LE name; |
| 395 | // used on big-endian systems. |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 396 | void GPTPart::ReversePartBytes(void) { |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 397 | int i; |
| 398 | |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 399 | ReverseBytes(&firstLBA, 8); |
| 400 | ReverseBytes(&lastLBA, 8); |
| 401 | ReverseBytes(&attributes, 8); |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame^] | 402 | for (i = 0; i < NAME_SIZE; i ++ ) |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 403 | ReverseBytes(name + i, 2); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 404 | } // GPTPart::ReverseBytes() |
| 405 | |
| 406 | /**************************************** |
| 407 | * Functions requiring user interaction * |
| 408 | ****************************************/ |
| 409 | |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 410 | // Change the type code on the partition. Also changes the name if the original |
| 411 | // name is the generic one for the partition type. |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 412 | void GPTPart::ChangeType(void) { |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 413 | string line; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 414 | int changeName; |
srs5694 | 82f3f0b | 2010-09-22 10:50:24 -0400 | [diff] [blame] | 415 | PartType tempType = (GUIDData) "00000000-0000-0000-0000-000000000000"; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 416 | |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame^] | 417 | #ifdef USE_UTF16 |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 418 | changeName = (GetDescription() == GetUTypeName()); |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame^] | 419 | #else |
| 420 | changeName = (GetDescription() == GetTypeName()); |
| 421 | #endif |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 422 | |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 423 | cout << "Current type is '" << GetTypeName() << "'\n"; |
srs5694 | 82f3f0b | 2010-09-22 10:50:24 -0400 | [diff] [blame] | 424 | do { |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame] | 425 | 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] | 426 | line = ReadString(); |
srs5694 | 82f3f0b | 2010-09-22 10:50:24 -0400 | [diff] [blame] | 427 | if ((line[0] == 'L') || (line[0] == 'l')) { |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 428 | partitionType.ShowAllTypes(); |
srs5694 | 82f3f0b | 2010-09-22 10:50:24 -0400 | [diff] [blame] | 429 | } else { |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 430 | if (line.length() == 0) |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame] | 431 | tempType = DEFAULT_GPT_TYPE; |
srs5694 | 82f3f0b | 2010-09-22 10:50:24 -0400 | [diff] [blame] | 432 | else |
| 433 | tempType = line; |
| 434 | } // if/else |
| 435 | } while (tempType == (GUIDData) "00000000-0000-0000-0000-000000000000"); |
| 436 | partitionType = tempType; |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 437 | cout << "Changed type of partition to '" << partitionType.TypeName() << "'\n"; |
| 438 | if (changeName) { |
| 439 | SetDefaultDescription(); |
| 440 | } // if |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 441 | } // GPTPart::ChangeType() |