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 | // |
| 7 | // Author: Rod Smith <rodsmith@rodsbooks.com>, (C) 2009 |
| 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 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 18 | #include <string.h> |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <iostream> |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 21 | #include "gptpart.h" |
| 22 | #include "attributes.h" |
| 23 | |
| 24 | using namespace std; |
| 25 | |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 26 | GPTPart::GPTPart(void) { |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 27 | partitionType.Zero(); |
| 28 | uniqueGUID.Zero(); |
| 29 | firstLBA = 0; |
| 30 | lastLBA = 0; |
| 31 | attributes = 0; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 32 | memset(name, 0, NAME_SIZE); |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 33 | } // Default constructor |
| 34 | |
| 35 | GPTPart::~GPTPart(void) { |
| 36 | } // destructor |
| 37 | |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 38 | // Return the gdisk-specific two-byte hex code for the partition |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 39 | uint16_t GPTPart::GetHexType(void) const { |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 40 | return partitionType.GetHexType(); |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 41 | } // GPTPart::GetHexType() |
| 42 | |
| 43 | // Return a plain-text description of the partition type (e.g., "Linux/Windows |
| 44 | // data" or "Linux swap"). |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 45 | string GPTPart::GetTypeName(void) { |
| 46 | return partitionType.TypeName(); |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 47 | } // GPTPart::GetNameType() |
| 48 | |
| 49 | // Compute and return the partition's length (or 0 if the end is incorrectly |
| 50 | // set before the beginning). |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 51 | uint64_t GPTPart::GetLengthLBA(void) const { |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 52 | uint64_t length = 0; |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 53 | |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 54 | if (firstLBA <= lastLBA) |
| 55 | length = lastLBA - firstLBA + UINT64_C(1); |
| 56 | return length; |
| 57 | } // GPTPart::GetLengthLBA() |
| 58 | |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 59 | // Return partition's name field, converted to a C++ ASCII string |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 60 | string GPTPart::GetDescription(void) { |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 61 | string theName; |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 62 | int i; |
| 63 | |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 64 | theName = ""; |
| 65 | for (i = 0; i < NAME_SIZE; i += 2) { |
| 66 | if (name[i] != '\0') |
| 67 | theName += name[i]; |
| 68 | } // for |
| 69 | return theName; |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 70 | } // GPTPart::GetDescription() |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 71 | |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 72 | // Return 1 if the partition is in use |
| 73 | int GPTPart::IsUsed(void) { |
| 74 | return (firstLBA != UINT64_C(0)); |
| 75 | } // GPTPart::IsUsed() |
| 76 | |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 77 | // Set the type code to the specified one. Also changes the partition |
| 78 | // name *IF* the current name is the generic one for the current partition |
| 79 | // type. |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 80 | void GPTPart::SetType(PartType t) { |
| 81 | if (GetDescription() == partitionType.TypeName()) { |
| 82 | SetName(t.TypeName()); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 83 | } // if |
| 84 | partitionType = t; |
| 85 | } // GPTPart::SetType() |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 86 | |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 87 | // Set the name for a partition to theName, or prompt for a name if |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 88 | // theName is empty. Note that theName is a standard C++-style ASCII |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 89 | // string, although the GUID partition definition requires a UTF-16LE |
| 90 | // string. This function creates a simple-minded copy for this. |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 91 | void GPTPart::SetName(const string & theName) { |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 92 | char newName[NAME_SIZE]; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 93 | size_t i; |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 94 | |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 95 | // Blank out new name string, so that it will terminate in a null |
| 96 | // when data are copied to it.... |
| 97 | memset(newName, 0, NAME_SIZE); |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 98 | |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 99 | if (theName == "") { // No name specified, so get one from the user |
| 100 | cout << "Enter name: "; |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 101 | ReadCString(newName, NAME_SIZE / 2 + 1); |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 102 | |
| 103 | // Input is likely to include a newline, so remove it.... |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 104 | i = strlen(newName); |
| 105 | if (i && newName[i - 1] == '\n') |
| 106 | newName[i - 1] = '\0'; |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 107 | } else { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 108 | strcpy(newName, theName.substr(0, NAME_SIZE / 2).c_str()); |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 109 | } // if |
| 110 | |
| 111 | // Copy the C-style ASCII string from newName into a form that the GPT |
| 112 | // table will accept.... |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 113 | memset(name, 0, NAME_SIZE); |
| 114 | for (i = 0; i < NAME_SIZE / 2; i++) |
| 115 | name[i * 2] = newName[i]; |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 116 | } // GPTPart::SetName() |
| 117 | |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 118 | // Set the name for the partition based on the current GUID partition type |
| 119 | // code's associated name |
| 120 | void GPTPart::SetDefaultDescription(void) { |
| 121 | SetName(partitionType.TypeName()); |
| 122 | } // GPTPart::SetDefaultDescription() |
| 123 | |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 124 | GPTPart & GPTPart::operator=(const GPTPart & orig) { |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 125 | partitionType = orig.partitionType; |
| 126 | uniqueGUID = orig.uniqueGUID; |
| 127 | firstLBA = orig.firstLBA; |
| 128 | lastLBA = orig.lastLBA; |
| 129 | attributes = orig.attributes; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 130 | memcpy(name, orig.name, NAME_SIZE); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 131 | return *this; |
| 132 | } // assignment operator |
| 133 | |
srs5694 | 9a46b04 | 2011-03-15 00:34:10 -0400 | [diff] [blame] | 134 | // Compare the values, and return a bool result. |
| 135 | // Because this is intended for sorting and a firstLBA value of 0 denotes |
| 136 | // a partition that's not in use and so that should be sorted upwards, |
| 137 | // we return the opposite of the usual arithmetic result when either |
| 138 | // firstLBA value is 0. |
| 139 | bool GPTPart::operator<(const GPTPart &other) const { |
| 140 | |
| 141 | if (firstLBA && other.firstLBA) |
| 142 | return (firstLBA < other.firstLBA); |
| 143 | else |
| 144 | return (other.firstLBA < firstLBA); |
| 145 | } // GPTPart::operator<() |
| 146 | |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 147 | // Display summary information; does nothing if the partition is empty. |
| 148 | void GPTPart::ShowSummary(int partNum, uint32_t blockSize) { |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame^] | 149 | string sizeInIeee; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 150 | size_t i; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 151 | |
| 152 | if (firstLBA != 0) { |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame^] | 153 | sizeInIeee = BytesToIeee(lastLBA - firstLBA + 1, blockSize); |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 154 | cout.fill(' '); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 155 | cout.width(4); |
| 156 | cout << partNum + 1 << " "; |
| 157 | cout.width(14); |
| 158 | cout << firstLBA << " "; |
| 159 | cout.width(14); |
| 160 | cout << lastLBA << " "; |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame^] | 161 | cout << BytesToIeee(lastLBA - firstLBA + 1, blockSize) << " "; |
| 162 | if (sizeInIeee.length() < 10) |
| 163 | for (i = 0; i < 10 - sizeInIeee.length(); i++) |
| 164 | cout << " "; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 165 | cout.fill('0'); |
| 166 | cout.width(4); |
| 167 | cout.setf(ios::uppercase); |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 168 | cout << hex << partitionType.GetHexType() << " " << dec; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 169 | cout.fill(' '); |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 170 | cout << GetDescription().substr(0, 23) << "\n"; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 171 | cout.fill(' '); |
| 172 | } // if |
| 173 | } // GPTPart::ShowSummary() |
| 174 | |
| 175 | // Show detailed partition information. Does nothing if the partition is |
| 176 | // empty (as determined by firstLBA being 0). |
| 177 | void GPTPart::ShowDetails(uint32_t blockSize) { |
| 178 | uint64_t size; |
| 179 | |
| 180 | if (firstLBA != 0) { |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 181 | cout << "Partition GUID code: " << partitionType; |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 182 | cout << " (" << partitionType.TypeName() << ")\n"; |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 183 | cout << "Partition unique GUID: " << uniqueGUID << "\n"; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 184 | |
| 185 | cout << "First sector: " << firstLBA << " (at " |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame^] | 186 | << BytesToIeee(firstLBA, blockSize) << ")\n"; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 187 | cout << "Last sector: " << lastLBA << " (at " |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame^] | 188 | << BytesToIeee(lastLBA, blockSize) << ")\n"; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 189 | size = (lastLBA - firstLBA + 1); |
| 190 | cout << "Partition size: " << size << " sectors (" |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame^] | 191 | << BytesToIeee(size, blockSize) << ")\n"; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 192 | cout << "Attribute flags: "; |
| 193 | cout.fill('0'); |
| 194 | cout.width(16); |
| 195 | cout << hex; |
| 196 | cout << attributes << "\n"; |
| 197 | cout << dec; |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 198 | cout << "Partition name: " << GetDescription() << "\n"; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 199 | cout.fill(' '); |
| 200 | } // if |
| 201 | } // GPTPart::ShowDetails() |
| 202 | |
| 203 | // Blank (delete) a single partition |
| 204 | void GPTPart::BlankPartition(void) { |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 205 | uniqueGUID.Zero(); |
| 206 | partitionType.Zero(); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 207 | firstLBA = 0; |
| 208 | lastLBA = 0; |
| 209 | attributes = 0; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 210 | memset(name, 0, NAME_SIZE); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 211 | } // GPTPart::BlankPartition |
| 212 | |
| 213 | // Returns 1 if the two partitions overlap, 0 if they don't |
| 214 | int GPTPart::DoTheyOverlap(const GPTPart & other) { |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 215 | // Don't bother checking unless these are defined (both start and end points |
| 216 | // are 0 for undefined partitions, so just check the start points) |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 217 | return firstLBA && other.firstLBA && |
| 218 | (firstLBA <= other.lastLBA) != (lastLBA < other.firstLBA); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 219 | } // GPTPart::DoTheyOverlap() |
| 220 | |
| 221 | // Reverse the bytes of integral data types; used on big-endian systems. |
| 222 | void GPTPart::ReversePartBytes(void) { |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 223 | ReverseBytes(&firstLBA, 8); |
| 224 | ReverseBytes(&lastLBA, 8); |
| 225 | ReverseBytes(&attributes, 8); |
| 226 | } // GPTPart::ReverseBytes() |
| 227 | |
| 228 | /**************************************** |
| 229 | * Functions requiring user interaction * |
| 230 | ****************************************/ |
| 231 | |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 232 | // Change the type code on the partition. Also changes the name if the original |
| 233 | // name is the generic one for the partition type. |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 234 | void GPTPart::ChangeType(void) { |
| 235 | char line[255]; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 236 | int changeName; |
srs5694 | 82f3f0b | 2010-09-22 10:50:24 -0400 | [diff] [blame] | 237 | PartType tempType = (GUIDData) "00000000-0000-0000-0000-000000000000"; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 238 | |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 239 | changeName = (GetDescription() == GetTypeName()); |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 240 | |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 241 | cout << "Current type is '" << GetTypeName() << "'\n"; |
srs5694 | 82f3f0b | 2010-09-22 10:50:24 -0400 | [diff] [blame] | 242 | do { |
| 243 | cout << "Hex code or GUID (L to show codes, Enter = 0700): "; |
srs5694 | 9a46b04 | 2011-03-15 00:34:10 -0400 | [diff] [blame] | 244 | ReadCString(line, sizeof(line)); |
srs5694 | 82f3f0b | 2010-09-22 10:50:24 -0400 | [diff] [blame] | 245 | if ((line[0] == 'L') || (line[0] == 'l')) { |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 246 | partitionType.ShowAllTypes(); |
srs5694 | 82f3f0b | 2010-09-22 10:50:24 -0400 | [diff] [blame] | 247 | } else { |
| 248 | if (strlen(line) == 1) |
| 249 | tempType = 0x0700; |
| 250 | else |
| 251 | tempType = line; |
| 252 | } // if/else |
| 253 | } while (tempType == (GUIDData) "00000000-0000-0000-0000-000000000000"); |
| 254 | partitionType = tempType; |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 255 | cout << "Changed type of partition to '" << partitionType.TypeName() << "'\n"; |
| 256 | if (changeName) { |
| 257 | SetDefaultDescription(); |
| 258 | } // if |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 259 | } // GPTPart::ChangeType() |