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 | |
| 69 | // Set the type code to the specified one. Also changes the partition |
| 70 | // name *IF* the current name is the generic one for the current partition |
| 71 | // type. |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame^] | 72 | void GPTPart::SetType(PartType t) { |
| 73 | if (GetDescription() == partitionType.TypeName()) { |
| 74 | SetName(t.TypeName()); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 75 | } // if |
| 76 | partitionType = t; |
| 77 | } // GPTPart::SetType() |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 78 | |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 79 | // 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] | 80 | // theName is empty. Note that theName is a standard C++-style ASCII |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 81 | // string, although the GUID partition definition requires a UTF-16LE |
| 82 | // string. This function creates a simple-minded copy for this. |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 83 | void GPTPart::SetName(const string & theName) { |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 84 | char newName[NAME_SIZE]; // New name |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 85 | char *junk; |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 86 | int i; |
| 87 | |
| 88 | // Blank out new name string, just to be on the safe side.... |
| 89 | for (i = 0; i < NAME_SIZE; i++) |
| 90 | newName[i] = '\0'; |
| 91 | |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 92 | if (theName == "") { // No name specified, so get one from the user |
| 93 | cout << "Enter name: "; |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 94 | junk = fgets(newName, NAME_SIZE / 2, stdin); |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 95 | |
| 96 | // Input is likely to include a newline, so remove it.... |
| 97 | i = strlen(newName); |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame^] | 98 | if ((i > 0) && (i <= NAME_SIZE)) |
| 99 | if (newName[i - 1] == '\n') |
| 100 | newName[i - 1] = '\0'; |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 101 | } else { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 102 | strcpy(newName, theName.substr(0, NAME_SIZE / 2).c_str()); |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 103 | } // if |
| 104 | |
| 105 | // Copy the C-style ASCII string from newName into a form that the GPT |
| 106 | // table will accept.... |
| 107 | for (i = 0; i < NAME_SIZE; i++) { |
| 108 | if ((i % 2) == 0) { |
| 109 | name[i] = newName[(i / 2)]; |
| 110 | } else { |
| 111 | name[i] = '\0'; |
| 112 | } // if/else |
| 113 | } // for |
| 114 | } // GPTPart::SetName() |
| 115 | |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame^] | 116 | // Set the name for the partition based on the current GUID partition type |
| 117 | // code's associated name |
| 118 | void GPTPart::SetDefaultDescription(void) { |
| 119 | SetName(partitionType.TypeName()); |
| 120 | } // GPTPart::SetDefaultDescription() |
| 121 | |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 122 | GPTPart & GPTPart::operator=(const GPTPart & orig) { |
| 123 | int i; |
| 124 | |
| 125 | partitionType = orig.partitionType; |
| 126 | uniqueGUID = orig.uniqueGUID; |
| 127 | firstLBA = orig.firstLBA; |
| 128 | lastLBA = orig.lastLBA; |
| 129 | attributes = orig.attributes; |
| 130 | for (i = 0; i < NAME_SIZE; i++) |
| 131 | name[i] = orig.name[i]; |
| 132 | return *this; |
| 133 | } // assignment operator |
| 134 | |
| 135 | // Display summary information; does nothing if the partition is empty. |
| 136 | void GPTPart::ShowSummary(int partNum, uint32_t blockSize) { |
| 137 | string sizeInSI; |
| 138 | int i; |
| 139 | |
| 140 | if (firstLBA != 0) { |
| 141 | sizeInSI = BytesToSI(blockSize * (lastLBA - firstLBA + 1)); |
| 142 | cout.width(4); |
| 143 | cout << partNum + 1 << " "; |
| 144 | cout.width(14); |
| 145 | cout << firstLBA << " "; |
| 146 | cout.width(14); |
| 147 | cout << lastLBA << " "; |
| 148 | cout << BytesToSI(blockSize * (lastLBA - firstLBA + 1)) << " "; |
srs5694 | e321d44 | 2010-01-29 17:44:04 -0500 | [diff] [blame] | 149 | for (i = 0; i < 9 - (int) sizeInSI.length(); i++) cout << " "; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 150 | cout.fill('0'); |
| 151 | cout.width(4); |
| 152 | cout.setf(ios::uppercase); |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame^] | 153 | cout << hex << partitionType.GetHexType() << " " << dec; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 154 | cout.fill(' '); |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame^] | 155 | cout << GetDescription().substr(0, 23) << "\n"; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 156 | cout.fill(' '); |
| 157 | } // if |
| 158 | } // GPTPart::ShowSummary() |
| 159 | |
| 160 | // Show detailed partition information. Does nothing if the partition is |
| 161 | // empty (as determined by firstLBA being 0). |
| 162 | void GPTPart::ShowDetails(uint32_t blockSize) { |
| 163 | uint64_t size; |
| 164 | |
| 165 | if (firstLBA != 0) { |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame^] | 166 | cout << "Partition GUID code: " << partitionType.AsString(); |
| 167 | cout << " (" << partitionType.TypeName() << ")\n"; |
| 168 | cout << "Partition unique GUID: " << uniqueGUID.AsString() << "\n"; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 169 | |
| 170 | cout << "First sector: " << firstLBA << " (at " |
| 171 | << BytesToSI(firstLBA * blockSize) << ")\n"; |
| 172 | cout << "Last sector: " << lastLBA << " (at " |
| 173 | << BytesToSI(lastLBA * blockSize) << ")\n"; |
| 174 | size = (lastLBA - firstLBA + 1); |
| 175 | cout << "Partition size: " << size << " sectors (" |
| 176 | << BytesToSI(size * ((uint64_t) blockSize)) << ")\n"; |
| 177 | cout << "Attribute flags: "; |
| 178 | cout.fill('0'); |
| 179 | cout.width(16); |
| 180 | cout << hex; |
| 181 | cout << attributes << "\n"; |
| 182 | cout << dec; |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame^] | 183 | cout << "Partition name: " << GetDescription() << "\n"; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 184 | cout.fill(' '); |
| 185 | } // if |
| 186 | } // GPTPart::ShowDetails() |
| 187 | |
| 188 | // Blank (delete) a single partition |
| 189 | void GPTPart::BlankPartition(void) { |
| 190 | int j; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 191 | |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame^] | 192 | uniqueGUID.Zero(); |
| 193 | partitionType.Zero(); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 194 | firstLBA = 0; |
| 195 | lastLBA = 0; |
| 196 | attributes = 0; |
| 197 | for (j = 0; j < NAME_SIZE; j++) |
| 198 | name[j] = '\0'; |
| 199 | } // GPTPart::BlankPartition |
| 200 | |
| 201 | // Returns 1 if the two partitions overlap, 0 if they don't |
| 202 | int GPTPart::DoTheyOverlap(const GPTPart & other) { |
| 203 | int theyDo = 0; |
| 204 | |
| 205 | // Don't bother checking unless these are defined (both start and end points |
| 206 | // are 0 for undefined partitions, so just check the start points) |
| 207 | if ((firstLBA != 0) && (other.firstLBA != 0)) { |
| 208 | if ((firstLBA < other.lastLBA) && (lastLBA >= other.firstLBA)) |
| 209 | theyDo = 1; |
| 210 | if ((other.firstLBA < lastLBA) && (other.lastLBA >= firstLBA)) |
| 211 | theyDo = 1; |
| 212 | } // if |
| 213 | return (theyDo); |
| 214 | } // GPTPart::DoTheyOverlap() |
| 215 | |
| 216 | // Reverse the bytes of integral data types; used on big-endian systems. |
| 217 | void GPTPart::ReversePartBytes(void) { |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame^] | 218 | // partitionType.ReverseGUIDBytes(); |
| 219 | // uniqueGUID.ReverseGUIDBytes(); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 220 | ReverseBytes(&firstLBA, 8); |
| 221 | ReverseBytes(&lastLBA, 8); |
| 222 | ReverseBytes(&attributes, 8); |
| 223 | } // GPTPart::ReverseBytes() |
| 224 | |
| 225 | /**************************************** |
| 226 | * Functions requiring user interaction * |
| 227 | ****************************************/ |
| 228 | |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame^] | 229 | // Change the type code on the partition. Also changes the name if the original |
| 230 | // name is the generic one for the partition type. |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 231 | void GPTPart::ChangeType(void) { |
| 232 | char line[255]; |
| 233 | char* junk; |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame^] | 234 | unsigned int typeNum = 0xFFFF, changeName = 0; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 235 | |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame^] | 236 | if (GetDescription() == GetTypeName()) |
| 237 | changeName = 1; |
| 238 | cout << "Current type is '" << GetTypeName() << "'\n"; |
| 239 | while ((!partitionType.Valid(typeNum)) && (typeNum != 0)) { |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 240 | cout << "Hex code (L to show codes, 0 to enter raw code, Enter = 0700): "; |
| 241 | junk = fgets(line, 255, stdin); |
| 242 | sscanf(line, "%X", &typeNum); |
| 243 | if ((line[0] == 'L') || (line[0] == 'l')) |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame^] | 244 | partitionType.ShowAllTypes(); |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 245 | if (line[0] == '\n') { |
| 246 | typeNum = 0x0700; |
| 247 | } // if |
| 248 | } // while |
| 249 | if (typeNum != 0) // user entered a code, so convert it |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame^] | 250 | partitionType = typeNum; |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 251 | else // user wants to enter the GUID directly, so do that |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame^] | 252 | partitionType.GetGUIDFromUser(); |
| 253 | cout << "Changed type of partition to '" << partitionType.TypeName() << "'\n"; |
| 254 | if (changeName) { |
| 255 | SetDefaultDescription(); |
| 256 | } // if |
srs5694 | 0a69731 | 2010-01-28 21:10:52 -0500 | [diff] [blame] | 257 | } // GPTPart::ChangeType() |
| 258 | |
srs5694 | a0eb11a | 2009-08-29 15:00:08 -0400 | [diff] [blame] | 259 | /*********************************** |
| 260 | * Non-class but related functions * |
| 261 | ***********************************/ |
| 262 | |
| 263 | // Recursive quick sort algorithm for GPT partitions. Note that if there |
| 264 | // are any empties in the specified range, they'll be sorted to the |
| 265 | // start, resulting in a sorted set of partitions that begins with |
| 266 | // partition 2, 3, or higher. |
| 267 | void QuickSortGPT(GPTPart* partitions, int start, int finish) { |
| 268 | uint64_t starterValue; // starting location of median partition |
| 269 | int left, right; |
| 270 | GPTPart temp; |
| 271 | |
| 272 | left = start; |
| 273 | right = finish; |
| 274 | starterValue = partitions[(start + finish) / 2].GetFirstLBA(); |
| 275 | do { |
| 276 | while (partitions[left].GetFirstLBA() < starterValue) |
| 277 | left++; |
| 278 | while (partitions[right].GetFirstLBA() > starterValue) |
| 279 | right--; |
| 280 | if (left <= right) { |
| 281 | temp = partitions[left]; |
| 282 | partitions[left] = partitions[right]; |
| 283 | partitions[right] = temp; |
| 284 | left++; |
| 285 | right--; |
| 286 | } // if |
| 287 | } while (left <= right); |
| 288 | if (start < right) QuickSortGPT(partitions, start, right); |
| 289 | if (finish > left) QuickSortGPT(partitions, left, finish); |
| 290 | } // QuickSortGPT() |