| 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 | |
| srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 6 | /* This program is copyright (c) 2009 by Roderick W. Smith. It is distributed |
| 7 | under the terms of the GNU GPL version 2, as detailed in the COPYING file. */ |
| 8 | |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 9 | #define __STDC_LIMIT_MACROS |
| 10 | #define __STDC_CONSTANT_MACROS |
| 11 | |
| 12 | #include <sys/ioctl.h> |
| 13 | #include <stdio.h> |
| 14 | #include <string.h> |
| 15 | #include <stdint.h> |
| 16 | #include <errno.h> |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 17 | #include <fcntl.h> |
| srs5694 | e35eb1b | 2009-09-14 00:29:34 -0400 | [diff] [blame] | 18 | #include <sys/stat.h> |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 19 | #include "support.h" |
| 20 | |
| 21 | #include <sys/types.h> |
| 22 | |
| srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 23 | // As of 1/2010, BLKPBSZGET is very new, so I'm explicitly defining it if |
| 24 | // it's not already defined. This should become unnecessary in the future. |
| 25 | // Note that this is a Linux-only ioctl.... |
| 26 | #ifndef BLKPBSZGET |
| 27 | #define BLKPBSZGET _IO(0x12,123) |
| 28 | #endif |
| 29 | |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 30 | using namespace std; |
| 31 | |
| 32 | // Get a numeric value from the user, between low and high (inclusive). |
| 33 | // Keeps looping until the user enters a value within that range. |
| 34 | // If user provides no input, def (default value) is returned. |
| 35 | // (If def is outside of the low-high range, an explicit response |
| 36 | // is required.) |
| 37 | int GetNumber(int low, int high, int def, const char prompt[]) { |
| 38 | int response, num; |
| 39 | char line[255]; |
| srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 40 | char* junk; |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 41 | |
| 42 | if (low != high) { // bother only if low and high differ... |
| 43 | response = low - 1; // force one loop by setting response outside range |
| 44 | while ((response < low) || (response > high)) { |
| srs5694 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame] | 45 | printf("%s", prompt); |
| srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 46 | junk = fgets(line, 255, stdin); |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 47 | num = sscanf(line, "%d", &response); |
| 48 | if (num == 1) { // user provided a response |
| 49 | if ((response < low) || (response > high)) |
| 50 | printf("Value out of range\n"); |
| 51 | } else { // user hit enter; return default |
| 52 | response = def; |
| 53 | } // if/else |
| 54 | } // while |
| 55 | } else { // low == high, so return this value |
| 56 | printf("Using %d\n", low); |
| 57 | response = low; |
| 58 | } // else |
| 59 | return (response); |
| 60 | } // GetNumber() |
| 61 | |
| 62 | // Gets a Y/N response (and converts lowercase to uppercase) |
| 63 | char GetYN(void) { |
| 64 | char line[255]; |
| 65 | char response = '\0'; |
| srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 66 | char* junk; |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 67 | |
| 68 | while ((response != 'Y') && (response != 'N')) { |
| 69 | printf("(Y/N): "); |
| srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 70 | junk = fgets(line, 255, stdin); |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 71 | sscanf(line, "%c", &response); |
| 72 | if (response == 'y') response = 'Y'; |
| 73 | if (response == 'n') response = 'N'; |
| 74 | } // while |
| 75 | return response; |
| 76 | } // GetYN(void) |
| 77 | |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 78 | // Obtains a sector number, between low and high, from the |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 79 | // user, accepting values prefixed by "+" to add sectors to low, |
| 80 | // or the same with "K", "M", "G", or "T" as suffixes to add |
| 81 | // kilobytes, megabytes, gigabytes, or terabytes, respectively. |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 82 | // If a "-" prefix is used, use the high value minus the user- |
| 83 | // specified number of sectors (or KiB, MiB, etc.). Use the def |
| 84 | //value as the default if the user just hits Enter |
| 85 | uint64_t GetSectorNum(uint64_t low, uint64_t high, uint64_t def, char prompt[]) { |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 86 | unsigned long long response; |
| 87 | int num; |
| 88 | int plusFlag = 0; |
| 89 | uint64_t mult = 1; |
| 90 | char suffix; |
| 91 | char line[255]; |
| srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 92 | char* junk; |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 93 | |
| 94 | response = low - 1; // Ensure one pass by setting a too-low initial value |
| 95 | while ((response < low) || (response > high)) { |
| srs5694 | 1d1448a | 2009-12-31 21:20:19 -0500 | [diff] [blame] | 96 | printf("%s", prompt); |
| srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 97 | junk = fgets(line, 255, stdin); |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 98 | |
| 99 | // Remove leading spaces, if present |
| 100 | while (line[0] == ' ') |
| 101 | strcpy(line, &line[1]); |
| 102 | |
| 103 | // If present, flag and remove leading plus sign |
| 104 | if (line[0] == '+') { |
| 105 | plusFlag = 1; |
| 106 | strcpy(line, &line[1]); |
| 107 | } // if |
| 108 | |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 109 | // If present, flag and remove leading minus sign |
| 110 | if (line[0] == '-') { |
| 111 | plusFlag = -1; |
| 112 | strcpy(line, &line[1]); |
| 113 | } // if |
| 114 | |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 115 | // Extract numeric response and, if present, suffix |
| 116 | num = sscanf(line, "%llu%c", &response, &suffix); |
| 117 | |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 118 | // If no response, use default (def) |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 119 | if (num <= 0) { |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 120 | response = (unsigned long long) def; |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 121 | suffix = ' '; |
| srs5694 | e19ba09 | 2009-08-24 14:10:35 -0400 | [diff] [blame] | 122 | plusFlag = 0; |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 123 | } // if |
| 124 | |
| 125 | // Set multiplier based on suffix |
| 126 | switch (suffix) { |
| 127 | case 'K': |
| 128 | case 'k': |
| 129 | mult = (uint64_t) 1024 / SECTOR_SIZE; |
| 130 | break; |
| 131 | case 'M': |
| 132 | case 'm': |
| 133 | mult = (uint64_t) 1048576 / SECTOR_SIZE; |
| 134 | break; |
| 135 | case 'G': |
| 136 | case 'g': |
| 137 | mult = (uint64_t) 1073741824 / SECTOR_SIZE; |
| 138 | break; |
| 139 | case 'T': |
| 140 | case 't': |
| 141 | mult = ((uint64_t) 1073741824 * (uint64_t) 1024) / (uint64_t) SECTOR_SIZE; |
| 142 | break; |
| 143 | default: |
| 144 | mult = 1; |
| 145 | } // switch |
| 146 | |
| 147 | // Adjust response based on multiplier and plus flag, if present |
| 148 | response *= (unsigned long long) mult; |
| 149 | if (plusFlag == 1) { |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 150 | response = response + (unsigned long long) low - UINT64_C(1); |
| 151 | } // if |
| 152 | if (plusFlag == -1) { |
| 153 | response = (unsigned long long) high - response; |
| srs5694 | e19ba09 | 2009-08-24 14:10:35 -0400 | [diff] [blame] | 154 | } // if |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 155 | } // while |
| 156 | return ((uint64_t) response); |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 157 | } // GetSectorNum() |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 158 | |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 159 | // Takes a size in bytes (in size) and converts this to a size in |
| 160 | // SI units (KiB, MiB, GiB, TiB, or PiB), returned in C++ string |
| 161 | // form |
| 162 | char* BytesToSI(uint64_t size, char theValue[]) { |
| 163 | char units[8]; |
| 164 | float sizeInSI; |
| 165 | |
| 166 | if (theValue != NULL) { |
| 167 | sizeInSI = (float) size; |
| 168 | strcpy (units, " bytes"); |
| 169 | if (sizeInSI > 1024.0) { |
| 170 | sizeInSI /= 1024.0; |
| 171 | strcpy(units, " KiB"); |
| 172 | } // if |
| 173 | if (sizeInSI > 1024.0) { |
| 174 | sizeInSI /= 1024.0; |
| 175 | strcpy(units, " MiB"); |
| 176 | } // if |
| 177 | if (sizeInSI > 1024.0) { |
| 178 | sizeInSI /= 1024.0; |
| 179 | strcpy(units, " GiB"); |
| 180 | } // if |
| 181 | if (sizeInSI > 1024.0) { |
| 182 | sizeInSI /= 1024.0; |
| 183 | strcpy(units, " TiB"); |
| 184 | } // if |
| 185 | if (sizeInSI > 1024.0) { |
| 186 | sizeInSI /= 1024.0; |
| 187 | strcpy(units, " PiB"); |
| 188 | } // if |
| 189 | if (strcmp(units, " bytes") == 0) { // in bytes, so no decimal point |
| 190 | sprintf(theValue, "%1.0f%s", sizeInSI, units); |
| 191 | } else { |
| 192 | sprintf(theValue, "%1.1f%s", sizeInSI, units); |
| 193 | } // if/else |
| 194 | } // if |
| 195 | return theValue; |
| 196 | } // BlocksToSI() |
| 197 | |
| srs5694 | e35eb1b | 2009-09-14 00:29:34 -0400 | [diff] [blame] | 198 | // Returns block size of device pointed to by fd file descriptor. If the ioctl |
| 199 | // returns an error condition, print a warning but return a value of SECTOR_SIZE |
| 200 | // (512).. |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 201 | int GetBlockSize(int fd) { |
| srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 202 | int err = -1, result; |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 203 | |
| 204 | #ifdef __APPLE__ |
| 205 | err = ioctl(fd, DKIOCGETBLOCKSIZE, &result); |
| srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 206 | #endif |
| srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 207 | #ifdef __FreeBSD__ |
| 208 | err = ioctl(fd, DIOCGSECTORSIZE, &result); |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 209 | #endif |
| srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 210 | #ifdef __linux__ |
| 211 | err = ioctl(fd, BLKSSZGET, &result); |
| srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 212 | #endif |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 213 | |
| srs5694 | e35eb1b | 2009-09-14 00:29:34 -0400 | [diff] [blame] | 214 | if (err == -1) { |
| 215 | result = SECTOR_SIZE; |
| 216 | // ENOTTY = inappropriate ioctl; probably being called on a disk image |
| 217 | // file, so don't display the warning message.... |
| srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 218 | // 32-bit code returns EINVAL, I don't know why. I know I'm treading on |
| 219 | // thin ice here, but it should be OK in all but very weird cases.... |
| 220 | if ((errno != ENOTTY) && (errno != EINVAL)) { |
| srs5694 | e35eb1b | 2009-09-14 00:29:34 -0400 | [diff] [blame] | 221 | printf("\aError %d when determining sector size! Setting sector size to %d\n", |
| 222 | errno, SECTOR_SIZE); |
| 223 | } // if |
| 224 | } // if |
| 225 | |
| srs5694 | 1e09372 | 2010-01-05 00:14:19 -0500 | [diff] [blame^] | 226 | /* if (result != 512) { |
| srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 227 | printf("\aWARNING! Sector size is not 512 bytes! This program is likely to "); |
| 228 | printf("misbehave!\nProceed at your own risk!\n\n"); |
| srs5694 | 1e09372 | 2010-01-05 00:14:19 -0500 | [diff] [blame^] | 229 | } // if */ |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 230 | |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 231 | return (result); |
| 232 | } // GetBlockSize() |
| 233 | |
| srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 234 | // Return the partition alignment value in sectors. Right now this works |
| 235 | // only for Linux 2.6.32 and later, since I can't find equivalent ioctl()s |
| 236 | // for OS X or FreeBSD, and the Linux ioctl is new |
| 237 | int FindAlignment(int fd) { |
| 238 | int err = -2, result = 8, physicalSectorSize = 4096; |
| 239 | |
| 240 | #if defined (__linux__) && defined (BLKPBSZGET) |
| 241 | err = ioctl(fd, BLKPBSZGET, &physicalSectorSize); |
| 242 | // printf("Tried to get hardware alignment; err is %d, sector size is %d\n", err, physicalSectorSize); |
| 243 | #else |
| 244 | err = -1; |
| 245 | #endif |
| 246 | |
| srs5694 | 1e09372 | 2010-01-05 00:14:19 -0500 | [diff] [blame^] | 247 | if (err < 0) { // ioctl didn't work; have to guess.... |
| 248 | if (GetBlockSize(fd) == 512) |
| 249 | result = 8; // play it safe; align for 4096-byte sectors |
| 250 | else |
| 251 | result = 1; // unusual sector size; assume it's the real physical size |
| 252 | } else { // ioctl worked; compute alignment |
| srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 253 | result = physicalSectorSize / GetBlockSize(fd); |
| 254 | } // if/else |
| 255 | return result; |
| 256 | } // FindAlignment(int) |
| 257 | |
| 258 | // The same as FindAlignment(int), but opens and closes a device by filename |
| 259 | int FindAlignment(char deviceFilename[]) { |
| 260 | int fd; |
| 261 | int retval = 1; |
| 262 | |
| 263 | if ((fd = open(deviceFilename, O_RDONLY)) != -1) { |
| 264 | retval = FindAlignment(fd); |
| 265 | close(fd); |
| 266 | } // if |
| 267 | return retval; |
| 268 | } // FindAlignment(char) |
| 269 | |
| srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 270 | // Return a plain-text name for a partition type. |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 271 | // Convert a GUID to a string representation, suitable for display |
| 272 | // to humans.... |
| 273 | char* GUIDToStr(struct GUIDData theGUID, char* theString) { |
| srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 274 | unsigned long long blocks[11], block; |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 275 | |
| srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 276 | if (theString != NULL) { |
| 277 | blocks[0] = (theGUID.data1 & UINT64_C(0x00000000FFFFFFFF)); |
| 278 | blocks[1] = (theGUID.data1 & UINT64_C(0x0000FFFF00000000)) >> 32; |
| 279 | blocks[2] = (theGUID.data1 & UINT64_C(0xFFFF000000000000)) >> 48; |
| 280 | blocks[3] = (theGUID.data2 & UINT64_C(0x00000000000000FF)); |
| 281 | blocks[4] = (theGUID.data2 & UINT64_C(0x000000000000FF00)) >> 8; |
| 282 | blocks[5] = (theGUID.data2 & UINT64_C(0x0000000000FF0000)) >> 16; |
| 283 | blocks[6] = (theGUID.data2 & UINT64_C(0x00000000FF000000)) >> 24; |
| 284 | blocks[7] = (theGUID.data2 & UINT64_C(0x000000FF00000000)) >> 32; |
| 285 | blocks[8] = (theGUID.data2 & UINT64_C(0x0000FF0000000000)) >> 40; |
| 286 | blocks[9] = (theGUID.data2 & UINT64_C(0x00FF000000000000)) >> 48; |
| 287 | blocks[10] = (theGUID.data2 & UINT64_C(0xFF00000000000000)) >> 56; |
| 288 | sprintf(theString, |
| 289 | "%08llX-%04llX-%04llX-%02llX%02llX-%02llX%02llX%02llX%02llX%02llX%02llX", |
| 290 | blocks[0], blocks[1], blocks[2], blocks[3], blocks[4], blocks[5], |
| 291 | blocks[6], blocks[7], blocks[8], blocks[9], blocks[10]); |
| 292 | } // if |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 293 | return theString; |
| 294 | } // GUIDToStr() |
| 295 | |
| 296 | // Get a GUID from the user |
| 297 | GUIDData GetGUID(void) { |
| srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 298 | unsigned long long part1, part2, part3, part4, part5; |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 299 | int entered = 0; |
| 300 | char temp[255], temp2[255]; |
| srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 301 | char* junk; |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 302 | GUIDData theGUID; |
| 303 | |
| 304 | printf("\nA GUID is entered in five segments of from two to six bytes, with\n" |
| 305 | "dashes between segments.\n"); |
| 306 | printf("Enter the entire GUID, a four-byte hexadecimal number for the first segment, or\n" |
| 307 | "'R' to generate the entire GUID randomly: "); |
| srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 308 | junk = fgets(temp, 255, stdin); |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 309 | |
| 310 | // If user entered 'r' or 'R', generate GUID randomly.... |
| 311 | if ((temp[0] == 'r') || (temp[0] == 'R')) { |
| 312 | theGUID.data1 = (uint64_t) rand() * (uint64_t) rand(); |
| 313 | theGUID.data2 = (uint64_t) rand() * (uint64_t) rand(); |
| 314 | entered = 1; |
| 315 | } // if user entered 'R' or 'r' |
| 316 | |
| 317 | // If string length is right for whole entry, try to parse it.... |
| 318 | if ((strlen(temp) == 37) && (entered == 0)) { |
| 319 | strncpy(temp2, &temp[0], 8); |
| 320 | temp2[8] = '\0'; |
| 321 | sscanf(temp2, "%llx", &part1); |
| 322 | strncpy(temp2, &temp[9], 4); |
| 323 | temp2[4] = '\0'; |
| 324 | sscanf(temp2, "%llx", &part2); |
| 325 | strncpy(temp2, &temp[14], 4); |
| 326 | temp2[4] = '\0'; |
| 327 | sscanf(temp2, "%llx", &part3); |
| 328 | theGUID.data1 = (part3 << 48) + (part2 << 32) + part1; |
| 329 | strncpy(temp2, &temp[19], 4); |
| 330 | temp2[4] = '\0'; |
| 331 | sscanf(temp2, "%llx", &part4); |
| 332 | strncpy(temp2, &temp[24], 12); |
| 333 | temp2[12] = '\0'; |
| 334 | sscanf(temp2, "%llx", &part5); |
| 335 | theGUID.data2 = ((part4 & UINT64_C(0x000000000000FF00)) >> 8) + |
| 336 | ((part4 & UINT64_C(0x00000000000000FF)) << 8) + |
| 337 | ((part5 & UINT64_C(0x0000FF0000000000)) >> 24) + |
| 338 | ((part5 & UINT64_C(0x000000FF00000000)) >> 8) + |
| 339 | ((part5 & UINT64_C(0x00000000FF000000)) << 8) + |
| 340 | ((part5 & UINT64_C(0x0000000000FF0000)) << 24) + |
| 341 | ((part5 & UINT64_C(0x000000000000FF00)) << 40) + |
| 342 | ((part5 & UINT64_C(0x00000000000000FF)) << 56); |
| 343 | entered = 1; |
| 344 | } // if |
| 345 | |
| 346 | // If neither of the above methods of entry was used, use prompted |
| 347 | // entry.... |
| 348 | if (entered == 0) { |
| 349 | sscanf(temp, "%llx", &part1); |
| 350 | printf("Enter a two-byte hexadecimal number for the second segment: "); |
| srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 351 | junk = fgets(temp, 255, stdin); |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 352 | sscanf(temp, "%llx", &part2); |
| 353 | printf("Enter a two-byte hexadecimal number for the third segment: "); |
| srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 354 | junk = fgets(temp, 255, stdin); |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 355 | sscanf(temp, "%llx", &part3); |
| 356 | theGUID.data1 = (part3 << 48) + (part2 << 32) + part1; |
| 357 | printf("Enter a two-byte hexadecimal number for the fourth segment: "); |
| srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 358 | junk = fgets(temp, 255, stdin); |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 359 | sscanf(temp, "%llx", &part4); |
| 360 | printf("Enter a six-byte hexadecimal number for the fifth segment: "); |
| srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 361 | junk = fgets(temp, 255, stdin); |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 362 | sscanf(temp, "%llx", &part5); |
| 363 | theGUID.data2 = ((part4 & UINT64_C(0x000000000000FF00)) >> 8) + |
| 364 | ((part4 & UINT64_C(0x00000000000000FF)) << 8) + |
| 365 | ((part5 & UINT64_C(0x0000FF0000000000)) >> 24) + |
| 366 | ((part5 & UINT64_C(0x000000FF00000000)) >> 8) + |
| 367 | ((part5 & UINT64_C(0x00000000FF000000)) << 8) + |
| 368 | ((part5 & UINT64_C(0x0000000000FF0000)) << 24) + |
| 369 | ((part5 & UINT64_C(0x000000000000FF00)) << 40) + |
| 370 | ((part5 & UINT64_C(0x00000000000000FF)) << 56); |
| 371 | entered = 1; |
| 372 | } // if/else |
| 373 | printf("New GUID: %s\n", GUIDToStr(theGUID, temp)); |
| 374 | return theGUID; |
| 375 | } // GetGUID() |
| 376 | |
| srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 377 | // Return 1 if the CPU architecture is little endian, 0 if it's big endian.... |
| 378 | int IsLittleEndian(void) { |
| 379 | int littleE = 1; // assume little-endian (Intel-style) |
| 380 | union { |
| 381 | uint32_t num; |
| 382 | unsigned char uc[sizeof(uint32_t)]; |
| 383 | } endian; |
| 384 | |
| 385 | endian.num = 1; |
| 386 | if (endian.uc[0] != (unsigned char) 1) { |
| 387 | littleE = 0; |
| 388 | } // if |
| 389 | return (littleE); |
| 390 | } // IsLittleEndian() |
| 391 | |
| 392 | // Reverse the byte order of theValue; numBytes is number of bytes |
| srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 393 | void ReverseBytes(void* theValue, int numBytes) { |
| 394 | char* origValue; |
| srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 395 | char* tempValue; |
| 396 | int i; |
| 397 | |
| srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 398 | origValue = (char*) theValue; |
| srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 399 | tempValue = (char*) malloc(numBytes); |
| 400 | for (i = 0; i < numBytes; i++) |
| srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 401 | tempValue[i] = origValue[i]; |
| srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 402 | for (i = 0; i < numBytes; i++) |
| srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 403 | origValue[i] = tempValue[numBytes - i - 1]; |
| srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 404 | free(tempValue); |
| 405 | } // ReverseBytes() |
| 406 | |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 407 | // Compute (2 ^ value). Given the return type, value must be 63 or less. |
| 408 | // Used in some bit-fiddling functions |
| 409 | uint64_t PowerOf2(int value) { |
| 410 | uint64_t retval = 1; |
| 411 | int i; |
| 412 | |
| 413 | if ((value < 64) && (value >= 0)) { |
| 414 | for (i = 0; i < value; i++) { |
| 415 | retval *= 2; |
| 416 | } // for |
| 417 | } else retval = 0; |
| 418 | return retval; |
| 419 | } // PowerOf2() |
| 420 | |
| srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 421 | // An extended file-open function. This includes some system-specific checks. |
| 422 | // I want them in a function because I use these calls twice and I don't want |
| 423 | // to forget to change them in one location if I need to change them in |
| 424 | // the other.... |
| 425 | int OpenForWrite(char* deviceFilename) { |
| 426 | int fd; |
| 427 | |
| 428 | fd = open(deviceFilename, O_WRONLY); // try to open the device; may fail.... |
| 429 | #ifdef __APPLE__ |
| 430 | // MacOS X requires a shared lock under some circumstances.... |
| 431 | if (fd < 0) { |
| 432 | fd = open(deviceFilename, O_WRONLY|O_SHLOCK); |
| 433 | } // if |
| 434 | #endif |
| srs5694 | e35eb1b | 2009-09-14 00:29:34 -0400 | [diff] [blame] | 435 | return fd; |
| 436 | } // OpenForWrite() |
| 437 | |
| 438 | // Resync disk caches so the OS uses the new partition table. This code varies |
| 439 | // a lot from one OS to another. |
| 440 | void DiskSync(int fd) { |
| 441 | int i; |
| 442 | |
| 443 | sync(); |
| 444 | #ifdef __APPLE__ |
| 445 | printf("Warning: The kernel may continue to use old or deleted partitions.\n" |
| 446 | "You should reboot or remove the drive.\n"); |
| 447 | /* don't know if this helps |
| 448 | * it definitely will get things on disk though: |
| 449 | * http://topiks.org/mac-os-x/0321278542/ch12lev1sec8.html */ |
| 450 | i = ioctl(fd, DKIOCSYNCHRONIZECACHE); |
| 451 | #else |
| 452 | #ifdef __FreeBSD__ |
| 453 | sleep(2); |
| 454 | i = ioctl(fd, DIOCGFLUSH); |
| 455 | printf("Warning: The kernel may continue to use old or deleted partitions.\n" |
| 456 | "You should reboot or remove the drive.\n"); |
| 457 | #else |
| 458 | sleep(2); |
| 459 | i = ioctl(fd, BLKRRPART); |
| 460 | if (i) |
| 461 | printf("Warning: The kernel is still using the old partition table.\n" |
| 462 | "The new table will be used at the next reboot.\n"); |
| 463 | #endif |
| 464 | #endif |
| 465 | } // DiskSync() |
| srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 466 | |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 467 | /************************************************************************************** |
| 468 | * * |
| 469 | * Below functions are lifted from various sources, as documented in comments before * |
| 470 | * each one. * |
| 471 | * * |
| 472 | **************************************************************************************/ |
| 473 | |
| 474 | // The disksize function is taken from the Linux fdisk code and modified |
| 475 | // to work around a problem returning a uint64_t value on Mac OS. |
| 476 | uint64_t disksize(int fd, int *err) { |
| srs5694 | e35eb1b | 2009-09-14 00:29:34 -0400 | [diff] [blame] | 477 | long sz; // Do not delete; needed for Linux |
| 478 | long long b; // Do not delete; needed for Linux |
| srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 479 | uint64_t sectors = 0; // size in sectors |
| 480 | off_t bytes = 0; // size in bytes |
| 481 | struct stat64 st; |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 482 | |
| srs5694 | e35eb1b | 2009-09-14 00:29:34 -0400 | [diff] [blame] | 483 | // Note to self: I recall testing a simplified version of |
| 484 | // this code, similar to what's in the __APPLE__ block, |
| 485 | // on Linux, but I had some problems. IIRC, it ran OK on 32-bit |
| 486 | // systems but not on 64-bit. Keep this in mind in case of |
| 487 | // 32/64-bit issues on MacOS.... |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 488 | #ifdef __APPLE__ |
| srs5694 | e35eb1b | 2009-09-14 00:29:34 -0400 | [diff] [blame] | 489 | *err = ioctl(fd, DKIOCGETBLOCKCOUNT, §ors); |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 490 | #else |
| srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 491 | #ifdef __FreeBSD__ |
| srs5694 | e35eb1b | 2009-09-14 00:29:34 -0400 | [diff] [blame] | 492 | *err = ioctl(fd, DIOCGMEDIASIZE, &sz); |
| 493 | b = GetBlockSize(fd); |
| 494 | sectors = sz / b; |
| srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 495 | #else |
| srs5694 | e35eb1b | 2009-09-14 00:29:34 -0400 | [diff] [blame] | 496 | *err = ioctl(fd, BLKGETSIZE, &sz); |
| 497 | if (*err) { |
| 498 | sectors = sz = 0; |
| 499 | } // if |
| 500 | if ((errno == EFBIG) || (!*err)) { |
| 501 | *err = ioctl(fd, BLKGETSIZE64, &b); |
| 502 | if (*err || b == 0 || b == sz) |
| 503 | sectors = sz; |
| 504 | else |
| 505 | sectors = (b >> 9); |
| 506 | } // if |
| srs5694 | 1e09372 | 2010-01-05 00:14:19 -0500 | [diff] [blame^] | 507 | // Unintuitively, the above returns values in 512-byte blocks, no |
| 508 | // matter what the underlying device's block size. Correct for this.... |
| 509 | sectors /= (GetBlockSize(fd) / 512); |
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 510 | #endif |
| srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 511 | #endif |
| srs5694 | e35eb1b | 2009-09-14 00:29:34 -0400 | [diff] [blame] | 512 | |
| 513 | // The above methods have failed (or it's a bum filename reference), |
| 514 | // so let's assume it's a regular file (a QEMU image, dd backup, or |
| 515 | // what have you) and see what stat() gives us.... |
| 516 | if (sectors == 0) { |
| srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 517 | if (fstat64(fd, &st) == 0) { |
| srs5694 | e35eb1b | 2009-09-14 00:29:34 -0400 | [diff] [blame] | 518 | bytes = (uint64_t) st.st_size; |
| 519 | if ((bytes % UINT64_C(512)) != 0) |
| 520 | fprintf(stderr, "Warning: File size is not a multiple of 512 bytes!" |
| 521 | " Misbehavior is likely!\n\a"); |
| 522 | sectors = bytes / UINT64_C(512); |
| 523 | } // if |
| 524 | } // if |
| srs5694 | e35eb1b | 2009-09-14 00:29:34 -0400 | [diff] [blame] | 525 | return sectors; |
| srs5694 | 1e09372 | 2010-01-05 00:14:19 -0500 | [diff] [blame^] | 526 | } // disksize() |
| 527 | |
| 528 | // A variant on the standard read() function. Done to work around |
| 529 | // limitations in FreeBSD concerning the matching of the sector |
| 530 | // size with the number of bytes read |
| 531 | int myRead(int fd, char* buffer, int numBytes) { |
| 532 | int blockSize = 512, i, numBlocks, retval; |
| 533 | char* tempSpace; |
| 534 | |
| 535 | // Compute required space and allocate memory |
| 536 | blockSize = GetBlockSize(fd); |
| 537 | if (numBytes <= blockSize) { |
| 538 | numBlocks = 1; |
| 539 | tempSpace = (char*) malloc(blockSize); |
| 540 | } else { |
| 541 | numBlocks = numBytes / blockSize; |
| 542 | if ((numBytes % blockSize) != 0) numBlocks++; |
| 543 | tempSpace = (char*) malloc(numBlocks * blockSize); |
| 544 | } // if/else |
| 545 | |
| 546 | // Read the data into temporary space, then copy it to buffer |
| 547 | retval = read(fd, tempSpace, numBlocks * blockSize); |
| 548 | for (i = 0; i < numBytes; i++) { |
| 549 | buffer[i] = tempSpace[i]; |
| 550 | } // for |
| 551 | |
| 552 | // Adjust the return value, if necessary.... |
| 553 | if (((numBlocks * blockSize) != numBytes) && (retval > 0)) |
| 554 | retval = numBytes; |
| 555 | |
| 556 | free(tempSpace); |
| 557 | return retval; |
| 558 | } // myRead() |
| 559 | |
| 560 | // A variant on the standard write() function. Done to work around |
| 561 | // limitations in FreeBSD concerning the matching of the sector |
| 562 | // size with the number of bytes read |
| 563 | int myWrite(int fd, char* buffer, int numBytes) { |
| 564 | int blockSize = 512, i, numBlocks, retval; |
| 565 | char* tempSpace; |
| 566 | |
| 567 | // Compute required space and allocate memory |
| 568 | blockSize = GetBlockSize(fd); |
| 569 | if (numBytes <= blockSize) { |
| 570 | numBlocks = 1; |
| 571 | tempSpace = (char*) malloc(blockSize); |
| 572 | } else { |
| 573 | numBlocks = numBytes / blockSize; |
| 574 | if ((numBytes % blockSize) != 0) numBlocks++; |
| 575 | tempSpace = (char*) malloc(numBlocks * blockSize); |
| 576 | } // if/else |
| 577 | |
| 578 | // Copy the data to my own buffer, then write it |
| 579 | for (i = 0; i < numBytes; i++) { |
| 580 | tempSpace[i] = buffer[i]; |
| 581 | } // for |
| 582 | for (i = numBytes; i < numBlocks * blockSize; i++) { |
| 583 | tempSpace[i] = 0; |
| 584 | } // for |
| 585 | retval = write(fd, tempSpace, numBlocks * blockSize); |
| 586 | |
| 587 | // Adjust the return value, if necessary.... |
| 588 | if (((numBlocks * blockSize) != numBytes) && (retval > 0)) |
| 589 | retval = numBytes; |
| 590 | |
| 591 | free(tempSpace); |
| 592 | return retval; |
| 593 | } // myRead() |