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