srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1 | // support.cc |
| 2 | // Non-class support functions for gdisk program. |
| 3 | // Primarily by Rod Smith, February 2009, but with a few functions |
| 4 | // copied from other sources (see attributions below). |
| 5 | |
| 6 | #define __STDC_LIMIT_MACROS |
| 7 | #define __STDC_CONSTANT_MACROS |
| 8 | |
| 9 | #include <sys/ioctl.h> |
| 10 | #include <stdio.h> |
| 11 | #include <string.h> |
| 12 | #include <stdint.h> |
| 13 | #include <errno.h> |
| 14 | #include "support.h" |
| 15 | |
| 16 | #include <sys/types.h> |
| 17 | |
| 18 | using namespace std; |
| 19 | |
| 20 | // Get a numeric value from the user, between low and high (inclusive). |
| 21 | // Keeps looping until the user enters a value within that range. |
| 22 | // If user provides no input, def (default value) is returned. |
| 23 | // (If def is outside of the low-high range, an explicit response |
| 24 | // is required.) |
| 25 | int GetNumber(int low, int high, int def, const char prompt[]) { |
| 26 | int response, num; |
| 27 | char line[255]; |
| 28 | |
| 29 | if (low != high) { // bother only if low and high differ... |
| 30 | response = low - 1; // force one loop by setting response outside range |
| 31 | while ((response < low) || (response > high)) { |
| 32 | printf(prompt); |
| 33 | fgets(line, 255, stdin); |
| 34 | num = sscanf(line, "%d", &response); |
| 35 | if (num == 1) { // user provided a response |
| 36 | if ((response < low) || (response > high)) |
| 37 | printf("Value out of range\n"); |
| 38 | } else { // user hit enter; return default |
| 39 | response = def; |
| 40 | } // if/else |
| 41 | } // while |
| 42 | } else { // low == high, so return this value |
| 43 | printf("Using %d\n", low); |
| 44 | response = low; |
| 45 | } // else |
| 46 | return (response); |
| 47 | } // GetNumber() |
| 48 | |
| 49 | // Gets a Y/N response (and converts lowercase to uppercase) |
| 50 | char GetYN(void) { |
| 51 | char line[255]; |
| 52 | char response = '\0'; |
| 53 | |
| 54 | while ((response != 'Y') && (response != 'N')) { |
| 55 | printf("(Y/N): "); |
| 56 | fgets(line, 255, stdin); |
| 57 | sscanf(line, "%c", &response); |
| 58 | if (response == 'y') response = 'Y'; |
| 59 | if (response == 'n') response = 'N'; |
| 60 | } // while |
| 61 | return response; |
| 62 | } // GetYN(void) |
| 63 | |
| 64 | // Obtains the final sector number, between low and high, from the |
| 65 | // user, accepting values prefixed by "+" to add sectors to low, |
| 66 | // or the same with "K", "M", "G", or "T" as suffixes to add |
| 67 | // kilobytes, megabytes, gigabytes, or terabytes, respectively. |
| 68 | // Use the high value as the default if the user just hits Enter |
| 69 | uint64_t GetLastSector(uint64_t low, uint64_t high, char prompt[]) { |
| 70 | unsigned long long response; |
| 71 | int num; |
| 72 | int plusFlag = 0; |
| 73 | uint64_t mult = 1; |
| 74 | char suffix; |
| 75 | char line[255]; |
| 76 | |
| 77 | response = low - 1; // Ensure one pass by setting a too-low initial value |
| 78 | while ((response < low) || (response > high)) { |
| 79 | printf(prompt); |
| 80 | fgets(line, 255, stdin); |
| 81 | |
| 82 | // Remove leading spaces, if present |
| 83 | while (line[0] == ' ') |
| 84 | strcpy(line, &line[1]); |
| 85 | |
| 86 | // If present, flag and remove leading plus sign |
| 87 | if (line[0] == '+') { |
| 88 | plusFlag = 1; |
| 89 | strcpy(line, &line[1]); |
| 90 | } // if |
| 91 | |
| 92 | // Extract numeric response and, if present, suffix |
| 93 | num = sscanf(line, "%llu%c", &response, &suffix); |
| 94 | |
| 95 | // If no response, use default: The high value |
| 96 | if (num <= 0) { |
| 97 | response = (unsigned long long) high; |
| 98 | suffix = ' '; |
srs5694 | e19ba09 | 2009-08-24 14:10:35 -0400 | [diff] [blame^] | 99 | plusFlag = 0; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 100 | } // if |
| 101 | |
| 102 | // Set multiplier based on suffix |
| 103 | switch (suffix) { |
| 104 | case 'K': |
| 105 | case 'k': |
| 106 | mult = (uint64_t) 1024 / SECTOR_SIZE; |
| 107 | break; |
| 108 | case 'M': |
| 109 | case 'm': |
| 110 | mult = (uint64_t) 1048576 / SECTOR_SIZE; |
| 111 | break; |
| 112 | case 'G': |
| 113 | case 'g': |
| 114 | mult = (uint64_t) 1073741824 / SECTOR_SIZE; |
| 115 | break; |
| 116 | case 'T': |
| 117 | case 't': |
| 118 | mult = ((uint64_t) 1073741824 * (uint64_t) 1024) / (uint64_t) SECTOR_SIZE; |
| 119 | break; |
| 120 | default: |
| 121 | mult = 1; |
| 122 | } // switch |
| 123 | |
| 124 | // Adjust response based on multiplier and plus flag, if present |
| 125 | response *= (unsigned long long) mult; |
| 126 | if (plusFlag == 1) { |
| 127 | response = response + (unsigned long long) low - 1; |
srs5694 | e19ba09 | 2009-08-24 14:10:35 -0400 | [diff] [blame^] | 128 | } // if |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 129 | } // while |
| 130 | return ((uint64_t) response); |
| 131 | } // GetLastSector() |
| 132 | |
| 133 | // Return a plain-text name for a partition type. |
| 134 | // Takes a size in bytes (in size) and converts this to a size in |
| 135 | // SI units (KiB, MiB, GiB, TiB, or PiB), returned in C++ string |
| 136 | // form |
| 137 | char* BytesToSI(uint64_t size, char theValue[]) { |
| 138 | char units[8]; |
| 139 | float sizeInSI; |
| 140 | |
| 141 | if (theValue != NULL) { |
| 142 | sizeInSI = (float) size; |
| 143 | strcpy (units, " bytes"); |
| 144 | if (sizeInSI > 1024.0) { |
| 145 | sizeInSI /= 1024.0; |
| 146 | strcpy(units, " KiB"); |
| 147 | } // if |
| 148 | if (sizeInSI > 1024.0) { |
| 149 | sizeInSI /= 1024.0; |
| 150 | strcpy(units, " MiB"); |
| 151 | } // if |
| 152 | if (sizeInSI > 1024.0) { |
| 153 | sizeInSI /= 1024.0; |
| 154 | strcpy(units, " GiB"); |
| 155 | } // if |
| 156 | if (sizeInSI > 1024.0) { |
| 157 | sizeInSI /= 1024.0; |
| 158 | strcpy(units, " TiB"); |
| 159 | } // if |
| 160 | if (sizeInSI > 1024.0) { |
| 161 | sizeInSI /= 1024.0; |
| 162 | strcpy(units, " PiB"); |
| 163 | } // if |
| 164 | if (strcmp(units, " bytes") == 0) { // in bytes, so no decimal point |
| 165 | sprintf(theValue, "%1.0f%s", sizeInSI, units); |
| 166 | } else { |
| 167 | sprintf(theValue, "%1.1f%s", sizeInSI, units); |
| 168 | } // if/else |
| 169 | } // if |
| 170 | return theValue; |
| 171 | } // BlocksToSI() |
| 172 | |
| 173 | // Returns block size of device pointed to by fd file descriptor, or -1 |
| 174 | // if there's a problem |
| 175 | int GetBlockSize(int fd) { |
| 176 | int err, result; |
| 177 | |
| 178 | #ifdef __APPLE__ |
| 179 | err = ioctl(fd, DKIOCGETBLOCKSIZE, &result); |
| 180 | #else |
| 181 | err = ioctl(fd, BLKSSZGET, &result); |
| 182 | #endif |
| 183 | |
| 184 | if (result != 512) { |
| 185 | printf("\aWARNING! Sector size is not 512 bytes! This program is likely to"); |
| 186 | printf("misbehave! Proceed at your own risk!\n\n"); |
| 187 | } // if |
| 188 | |
| 189 | if (err == -1) |
| 190 | result = -1; |
| 191 | return (result); |
| 192 | } // GetBlockSize() |
| 193 | |
| 194 | // Convert a GUID to a string representation, suitable for display |
| 195 | // to humans.... |
| 196 | char* GUIDToStr(struct GUIDData theGUID, char* theString) { |
| 197 | uint64_t block; |
| 198 | |
| 199 | if (theString != NULL) { |
| 200 | block = (theGUID.data1 & UINT64_C(0x00000000FFFFFFFF)); |
| 201 | sprintf(theString, "%08llX-", (unsigned long long) block); |
| 202 | block = (theGUID.data1 & UINT64_C(0x0000FFFF00000000)) >> 32; |
| 203 | sprintf(theString, "%s%04llX-", theString, (unsigned long long) block); |
| 204 | block = (theGUID.data1 & UINT64_C(0xFFFF000000000000)) >> 48; |
| 205 | sprintf(theString, "%s%04llX-", theString, (unsigned long long) block); |
| 206 | block = (theGUID.data2 & UINT64_C(0x00000000000000FF)); |
| 207 | sprintf(theString, "%s%02llX", theString, (unsigned long long) block); |
| 208 | block = (theGUID.data2 & UINT64_C(0x000000000000FF00)) >> 8; |
| 209 | sprintf(theString, "%s%02llX-", theString, (unsigned long long) block); |
| 210 | block = (theGUID.data2 & UINT64_C(0x0000000000FF0000)) >> 16; |
| 211 | sprintf(theString, "%s%02llX", theString, (unsigned long long) block); |
| 212 | block = (theGUID.data2 & UINT64_C(0x00000000FF000000)) >> 24; |
| 213 | sprintf(theString, "%s%02llX", theString, (unsigned long long) block); |
| 214 | block = (theGUID.data2 & UINT64_C(0x000000FF00000000)) >> 32; |
| 215 | sprintf(theString, "%s%02llX", theString, (unsigned long long) block); |
| 216 | block = (theGUID.data2 & UINT64_C(0x0000FF0000000000)) >> 40; |
| 217 | sprintf(theString, "%s%02llX", theString, (unsigned long long) block); |
| 218 | block = (theGUID.data2 & UINT64_C(0x00FF000000000000)) >> 48; |
| 219 | sprintf(theString, "%s%02llX", theString, (unsigned long long) block); |
| 220 | block = (theGUID.data2 & UINT64_C(0xFF00000000000000)) >> 56; |
| 221 | sprintf(theString, "%s%02llX", theString, (unsigned long long) block); |
| 222 | } // if |
| 223 | return theString; |
| 224 | } // GUIDToStr() |
| 225 | |
| 226 | // Get a GUID from the user |
| 227 | GUIDData GetGUID(void) { |
| 228 | uint64_t part1, part2, part3, part4, part5; |
| 229 | int entered = 0; |
| 230 | char temp[255], temp2[255]; |
| 231 | GUIDData theGUID; |
| 232 | |
| 233 | printf("\nA GUID is entered in five segments of from two to six bytes, with\n" |
| 234 | "dashes between segments.\n"); |
| 235 | printf("Enter the entire GUID, a four-byte hexadecimal number for the first segment, or\n" |
| 236 | "'R' to generate the entire GUID randomly: "); |
| 237 | fgets(temp, 255, stdin); |
| 238 | |
| 239 | // If user entered 'r' or 'R', generate GUID randomly.... |
| 240 | if ((temp[0] == 'r') || (temp[0] == 'R')) { |
| 241 | theGUID.data1 = (uint64_t) rand() * (uint64_t) rand(); |
| 242 | theGUID.data2 = (uint64_t) rand() * (uint64_t) rand(); |
| 243 | entered = 1; |
| 244 | } // if user entered 'R' or 'r' |
| 245 | |
| 246 | // If string length is right for whole entry, try to parse it.... |
| 247 | if ((strlen(temp) == 37) && (entered == 0)) { |
| 248 | strncpy(temp2, &temp[0], 8); |
| 249 | temp2[8] = '\0'; |
| 250 | sscanf(temp2, "%llx", &part1); |
| 251 | strncpy(temp2, &temp[9], 4); |
| 252 | temp2[4] = '\0'; |
| 253 | sscanf(temp2, "%llx", &part2); |
| 254 | strncpy(temp2, &temp[14], 4); |
| 255 | temp2[4] = '\0'; |
| 256 | sscanf(temp2, "%llx", &part3); |
| 257 | theGUID.data1 = (part3 << 48) + (part2 << 32) + part1; |
| 258 | strncpy(temp2, &temp[19], 4); |
| 259 | temp2[4] = '\0'; |
| 260 | sscanf(temp2, "%llx", &part4); |
| 261 | strncpy(temp2, &temp[24], 12); |
| 262 | temp2[12] = '\0'; |
| 263 | sscanf(temp2, "%llx", &part5); |
| 264 | theGUID.data2 = ((part4 & UINT64_C(0x000000000000FF00)) >> 8) + |
| 265 | ((part4 & UINT64_C(0x00000000000000FF)) << 8) + |
| 266 | ((part5 & UINT64_C(0x0000FF0000000000)) >> 24) + |
| 267 | ((part5 & UINT64_C(0x000000FF00000000)) >> 8) + |
| 268 | ((part5 & UINT64_C(0x00000000FF000000)) << 8) + |
| 269 | ((part5 & UINT64_C(0x0000000000FF0000)) << 24) + |
| 270 | ((part5 & UINT64_C(0x000000000000FF00)) << 40) + |
| 271 | ((part5 & UINT64_C(0x00000000000000FF)) << 56); |
| 272 | entered = 1; |
| 273 | } // if |
| 274 | |
| 275 | // If neither of the above methods of entry was used, use prompted |
| 276 | // entry.... |
| 277 | if (entered == 0) { |
| 278 | sscanf(temp, "%llx", &part1); |
| 279 | printf("Enter a two-byte hexadecimal number for the second segment: "); |
| 280 | fgets(temp, 255, stdin); |
| 281 | sscanf(temp, "%llx", &part2); |
| 282 | printf("Enter a two-byte hexadecimal number for the third segment: "); |
| 283 | fgets(temp, 255, stdin); |
| 284 | sscanf(temp, "%llx", &part3); |
| 285 | theGUID.data1 = (part3 << 48) + (part2 << 32) + part1; |
| 286 | printf("Enter a two-byte hexadecimal number for the fourth segment: "); |
| 287 | fgets(temp, 255, stdin); |
| 288 | sscanf(temp, "%llx", &part4); |
| 289 | printf("Enter a six-byte hexadecimal number for the fifth segment: "); |
| 290 | fgets(temp, 255, stdin); |
| 291 | sscanf(temp, "%llx", &part5); |
| 292 | theGUID.data2 = ((part4 & UINT64_C(0x000000000000FF00)) >> 8) + |
| 293 | ((part4 & UINT64_C(0x00000000000000FF)) << 8) + |
| 294 | ((part5 & UINT64_C(0x0000FF0000000000)) >> 24) + |
| 295 | ((part5 & UINT64_C(0x000000FF00000000)) >> 8) + |
| 296 | ((part5 & UINT64_C(0x00000000FF000000)) << 8) + |
| 297 | ((part5 & UINT64_C(0x0000000000FF0000)) << 24) + |
| 298 | ((part5 & UINT64_C(0x000000000000FF00)) << 40) + |
| 299 | ((part5 & UINT64_C(0x00000000000000FF)) << 56); |
| 300 | entered = 1; |
| 301 | } // if/else |
| 302 | printf("New GUID: %s\n", GUIDToStr(theGUID, temp)); |
| 303 | return theGUID; |
| 304 | } // GetGUID() |
| 305 | |
| 306 | // Compute (2 ^ value). Given the return type, value must be 63 or less. |
| 307 | // Used in some bit-fiddling functions |
| 308 | uint64_t PowerOf2(int value) { |
| 309 | uint64_t retval = 1; |
| 310 | int i; |
| 311 | |
| 312 | if ((value < 64) && (value >= 0)) { |
| 313 | for (i = 0; i < value; i++) { |
| 314 | retval *= 2; |
| 315 | } // for |
| 316 | } else retval = 0; |
| 317 | return retval; |
| 318 | } // PowerOf2() |
| 319 | |
| 320 | /************************************************************************************** |
| 321 | * * |
| 322 | * Below functions are lifted from various sources, as documented in comments before * |
| 323 | * each one. * |
| 324 | * * |
| 325 | **************************************************************************************/ |
| 326 | |
| 327 | // The disksize function is taken from the Linux fdisk code and modified |
| 328 | // to work around a problem returning a uint64_t value on Mac OS. |
| 329 | uint64_t disksize(int fd, int *err) { |
| 330 | long sz; // Do not delete; needed for Linux |
| 331 | long long b; // Do not delete; needed for Linux |
| 332 | uint64_t sectors; |
| 333 | |
| 334 | // Note to self: I recall testing a simplified version of |
| 335 | // this code, similar to what's in the __APPLE__ block, |
| 336 | // on Linux, but I had some problems. IIRC, it ran OK on 32-bit |
| 337 | // systems but not on 64-bit. Keep this in mind in case of |
| 338 | // 32/64-bit issues on MacOS.... |
| 339 | #ifdef __APPLE__ |
| 340 | *err = ioctl(fd, DKIOCGETBLOCKCOUNT, §ors); |
| 341 | #else |
| 342 | *err = ioctl(fd, BLKGETSIZE, &sz); |
| 343 | if (*err) { |
| 344 | sz = 0; |
| 345 | if (errno != EFBIG) |
| 346 | return sz; |
| 347 | } |
| 348 | *err = ioctl(fd, BLKGETSIZE64, &b); |
| 349 | if (*err || b == 0 || b == sz) |
| 350 | sectors = sz; |
| 351 | else |
| 352 | sectors = (b >> 9); |
| 353 | #endif |
| 354 | return sectors; |
| 355 | } |