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