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