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