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 |
| 39 | uint16_t GPTPart::GetHexType(void) { |
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). |
| 51 | uint64_t GPTPart::GetLengthLBA(void) { |
| 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 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 101 | if (!fgets(newName, NAME_SIZE / 2 + 1, stdin)) { |
| 102 | cerr << "Critical error! Failed fgets() in GPTPart::SetName()!\n"; |
| 103 | exit(1); |
| 104 | } |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 105 | |
| 106 | // Input is likely to include a newline, so remove it.... |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 107 | i = strlen(newName); |
| 108 | if (i && newName[i - 1] == '\n') |
| 109 | newName[i - 1] = '\0'; |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 110 | } else { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 111 | strcpy(newName, theName.substr(0, NAME_SIZE / 2).c_str()); |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 112 | } // if |
| 113 | |
| 114 | // Copy the C-style ASCII string from newName into a form that the GPT |
| 115 | // table will accept.... |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 116 | memset(name, 0, NAME_SIZE); |
| 117 | for (i = 0; i < NAME_SIZE / 2; i++) |
| 118 | name[i * 2] = newName[i]; |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 119 | } // GPTPart::SetName() |
| 120 | |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 121 | // Set the name for the partition based on the current GUID partition type |
| 122 | // code's associated name |
| 123 | void GPTPart::SetDefaultDescription(void) { |
| 124 | SetName(partitionType.TypeName()); |
| 125 | } // GPTPart::SetDefaultDescription() |
| 126 | |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 127 | GPTPart & GPTPart::operator=(const GPTPart & orig) { |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 128 | partitionType = orig.partitionType; |
| 129 | uniqueGUID = orig.uniqueGUID; |
| 130 | firstLBA = orig.firstLBA; |
| 131 | lastLBA = orig.lastLBA; |
| 132 | attributes = orig.attributes; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 133 | memcpy(name, orig.name, NAME_SIZE); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 134 | return *this; |
| 135 | } // assignment operator |
| 136 | |
| 137 | // Display summary information; does nothing if the partition is empty. |
| 138 | void GPTPart::ShowSummary(int partNum, uint32_t blockSize) { |
| 139 | string sizeInSI; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 140 | size_t i; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 141 | |
| 142 | if (firstLBA != 0) { |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 143 | sizeInSI = BytesToSI(lastLBA - firstLBA + 1, blockSize); |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 144 | cout.fill(' '); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 145 | cout.width(4); |
| 146 | cout << partNum + 1 << " "; |
| 147 | cout.width(14); |
| 148 | cout << firstLBA << " "; |
| 149 | cout.width(14); |
| 150 | cout << lastLBA << " "; |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 151 | cout << BytesToSI(lastLBA - firstLBA + 1, blockSize) << " "; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 152 | for (i = 0; i < 10 - sizeInSI.length(); i++) |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 153 | cout << " "; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 154 | cout.fill('0'); |
| 155 | cout.width(4); |
| 156 | cout.setf(ios::uppercase); |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 157 | cout << hex << partitionType.GetHexType() << " " << dec; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 158 | cout.fill(' '); |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 159 | cout << GetDescription().substr(0, 23) << "\n"; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 160 | cout.fill(' '); |
| 161 | } // if |
| 162 | } // GPTPart::ShowSummary() |
| 163 | |
| 164 | // Show detailed partition information. Does nothing if the partition is |
| 165 | // empty (as determined by firstLBA being 0). |
| 166 | void GPTPart::ShowDetails(uint32_t blockSize) { |
| 167 | uint64_t size; |
| 168 | |
| 169 | if (firstLBA != 0) { |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 170 | cout << "Partition GUID code: " << partitionType; |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 171 | cout << " (" << partitionType.TypeName() << ")\n"; |
srs5694 | 5a08175 | 2010-09-24 20:39:41 -0400 | [diff] [blame] | 172 | cout << "Partition unique GUID: " << uniqueGUID << "\n"; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 173 | |
| 174 | cout << "First sector: " << firstLBA << " (at " |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 175 | << BytesToSI(firstLBA, blockSize) << ")\n"; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 176 | cout << "Last sector: " << lastLBA << " (at " |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 177 | << BytesToSI(lastLBA, blockSize) << ")\n"; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 178 | size = (lastLBA - firstLBA + 1); |
| 179 | cout << "Partition size: " << size << " sectors (" |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 180 | << BytesToSI(size, blockSize) << ")\n"; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 181 | cout << "Attribute flags: "; |
| 182 | cout.fill('0'); |
| 183 | cout.width(16); |
| 184 | cout << hex; |
| 185 | cout << attributes << "\n"; |
| 186 | cout << dec; |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 187 | cout << "Partition name: " << GetDescription() << "\n"; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 188 | cout.fill(' '); |
| 189 | } // if |
| 190 | } // GPTPart::ShowDetails() |
| 191 | |
| 192 | // Blank (delete) a single partition |
| 193 | void GPTPart::BlankPartition(void) { |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 194 | uniqueGUID.Zero(); |
| 195 | partitionType.Zero(); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 196 | firstLBA = 0; |
| 197 | lastLBA = 0; |
| 198 | attributes = 0; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 199 | memset(name, 0, NAME_SIZE); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 200 | } // GPTPart::BlankPartition |
| 201 | |
| 202 | // Returns 1 if the two partitions overlap, 0 if they don't |
| 203 | int GPTPart::DoTheyOverlap(const GPTPart & other) { |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 204 | // Don't bother checking unless these are defined (both start and end points |
| 205 | // are 0 for undefined partitions, so just check the start points) |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 206 | // cout << "Entering GPTPart::DoTheyOverlap()\n"; |
| 207 | return firstLBA && other.firstLBA && |
| 208 | (firstLBA <= other.lastLBA) != (lastLBA < other.firstLBA); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 209 | } // GPTPart::DoTheyOverlap() |
| 210 | |
| 211 | // Reverse the bytes of integral data types; used on big-endian systems. |
| 212 | void GPTPart::ReversePartBytes(void) { |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 213 | ReverseBytes(&firstLBA, 8); |
| 214 | ReverseBytes(&lastLBA, 8); |
| 215 | ReverseBytes(&attributes, 8); |
| 216 | } // GPTPart::ReverseBytes() |
| 217 | |
| 218 | /**************************************** |
| 219 | * Functions requiring user interaction * |
| 220 | ****************************************/ |
| 221 | |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 222 | // Change the type code on the partition. Also changes the name if the original |
| 223 | // name is the generic one for the partition type. |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 224 | void GPTPart::ChangeType(void) { |
| 225 | char line[255]; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 226 | int changeName; |
srs5694 | 82f3f0b | 2010-09-22 10:50:24 -0400 | [diff] [blame] | 227 | PartType tempType = (GUIDData) "00000000-0000-0000-0000-000000000000"; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 228 | |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 229 | changeName = (GetDescription() == GetTypeName()); |
srs5694 | 55d9261 | 2010-03-07 22:16:07 -0500 | [diff] [blame] | 230 | |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 231 | cout << "Current type is '" << GetTypeName() << "'\n"; |
srs5694 | 82f3f0b | 2010-09-22 10:50:24 -0400 | [diff] [blame] | 232 | do { |
| 233 | cout << "Hex code or GUID (L to show codes, Enter = 0700): "; |
srs5694 | 64cbd17 | 2011-03-01 22:03:54 -0500 | [diff] [blame] | 234 | if (!fgets(line, sizeof(line), stdin)) { |
| 235 | cerr << "Critical error! Failed fgets() in GPTPart::ChangeType()!\n"; |
| 236 | exit(1); |
| 237 | } // if |
srs5694 | 82f3f0b | 2010-09-22 10:50:24 -0400 | [diff] [blame] | 238 | if ((line[0] == 'L') || (line[0] == 'l')) { |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 239 | partitionType.ShowAllTypes(); |
srs5694 | 82f3f0b | 2010-09-22 10:50:24 -0400 | [diff] [blame] | 240 | } else { |
| 241 | if (strlen(line) == 1) |
| 242 | tempType = 0x0700; |
| 243 | else |
| 244 | tempType = line; |
| 245 | } // if/else |
| 246 | } while (tempType == (GUIDData) "00000000-0000-0000-0000-000000000000"); |
| 247 | partitionType = tempType; |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 248 | cout << "Changed type of partition to '" << partitionType.TypeName() << "'\n"; |
| 249 | if (changeName) { |
| 250 | SetDefaultDescription(); |
| 251 | } // if |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 252 | } // GPTPart::ChangeType() |