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