blob: 1cef7d67ea3ee56591022dff6e2b24091f66a759 [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
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
18using 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.)
25int 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)
50char 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
69uint64_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 = ' ';
srs5694e19ba092009-08-24 14:10:35 -040099 plusFlag = 0;
srs5694e7b4ff92009-08-18 13:16:10 -0400100 } // 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;
srs5694e19ba092009-08-24 14:10:35 -0400128 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400129 } // while
130 return ((uint64_t) response);
131} // GetLastSector()
132
srs5694e7b4ff92009-08-18 13:16:10 -0400133// Takes a size in bytes (in size) and converts this to a size in
134// SI units (KiB, MiB, GiB, TiB, or PiB), returned in C++ string
135// form
136char* BytesToSI(uint64_t size, char theValue[]) {
137 char units[8];
138 float sizeInSI;
139
140 if (theValue != NULL) {
141 sizeInSI = (float) size;
142 strcpy (units, " bytes");
143 if (sizeInSI > 1024.0) {
144 sizeInSI /= 1024.0;
145 strcpy(units, " KiB");
146 } // if
147 if (sizeInSI > 1024.0) {
148 sizeInSI /= 1024.0;
149 strcpy(units, " MiB");
150 } // if
151 if (sizeInSI > 1024.0) {
152 sizeInSI /= 1024.0;
153 strcpy(units, " GiB");
154 } // if
155 if (sizeInSI > 1024.0) {
156 sizeInSI /= 1024.0;
157 strcpy(units, " TiB");
158 } // if
159 if (sizeInSI > 1024.0) {
160 sizeInSI /= 1024.0;
161 strcpy(units, " PiB");
162 } // if
163 if (strcmp(units, " bytes") == 0) { // in bytes, so no decimal point
164 sprintf(theValue, "%1.0f%s", sizeInSI, units);
165 } else {
166 sprintf(theValue, "%1.1f%s", sizeInSI, units);
167 } // if/else
168 } // if
169 return theValue;
170} // BlocksToSI()
171
172// Returns block size of device pointed to by fd file descriptor, or -1
173// if there's a problem
174int GetBlockSize(int fd) {
175 int err, result;
176
177#ifdef __APPLE__
178 err = ioctl(fd, DKIOCGETBLOCKSIZE, &result);
179#else
180 err = ioctl(fd, BLKSSZGET, &result);
181#endif
182
183 if (result != 512) {
srs56942a9f5da2009-08-26 00:48:01 -0400184 printf("\aWARNING! Sector size is not 512 bytes! This program is likely to ");
185 printf("misbehave!\nProceed at your own risk!\n\n");
srs5694e7b4ff92009-08-18 13:16:10 -0400186 } // if
187
188 if (err == -1)
189 result = -1;
190 return (result);
191} // GetBlockSize()
192
srs56942a9f5da2009-08-26 00:48:01 -0400193// Return a plain-text name for a partition type.
srs5694e7b4ff92009-08-18 13:16:10 -0400194// Convert a GUID to a string representation, suitable for display
195// to humans....
196char* 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
227GUIDData 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
srs56942a9f5da2009-08-26 00:48:01 -0400306// Return 1 if the CPU architecture is little endian, 0 if it's big endian....
307int IsLittleEndian(void) {
308 int littleE = 1; // assume little-endian (Intel-style)
309 union {
310 uint32_t num;
311 unsigned char uc[sizeof(uint32_t)];
312 } endian;
313
314 endian.num = 1;
315 if (endian.uc[0] != (unsigned char) 1) {
316 littleE = 0;
317 } // if
318 return (littleE);
319} // IsLittleEndian()
320
321// Reverse the byte order of theValue; numBytes is number of bytes
322void ReverseBytes(char* theValue, int numBytes) {
323 char* tempValue;
324 int i;
325
326 tempValue = (char*) malloc(numBytes);
327 for (i = 0; i < numBytes; i++)
328 tempValue[i] = theValue[i];
329 for (i = 0; i < numBytes; i++)
330 theValue[i] = tempValue[numBytes - i - 1];
331 free(tempValue);
332} // ReverseBytes()
333
srs5694e7b4ff92009-08-18 13:16:10 -0400334// Compute (2 ^ value). Given the return type, value must be 63 or less.
335// Used in some bit-fiddling functions
336uint64_t PowerOf2(int value) {
337 uint64_t retval = 1;
338 int i;
339
340 if ((value < 64) && (value >= 0)) {
341 for (i = 0; i < value; i++) {
342 retval *= 2;
343 } // for
344 } else retval = 0;
345 return retval;
346} // PowerOf2()
347
348/**************************************************************************************
349 * *
350 * Below functions are lifted from various sources, as documented in comments before *
351 * each one. *
352 * *
353 **************************************************************************************/
354
355// The disksize function is taken from the Linux fdisk code and modified
356// to work around a problem returning a uint64_t value on Mac OS.
357uint64_t disksize(int fd, int *err) {
358 long sz; // Do not delete; needed for Linux
359 long long b; // Do not delete; needed for Linux
360 uint64_t sectors;
361
362 // Note to self: I recall testing a simplified version of
363 // this code, similar to what's in the __APPLE__ block,
364 // on Linux, but I had some problems. IIRC, it ran OK on 32-bit
365 // systems but not on 64-bit. Keep this in mind in case of
366 // 32/64-bit issues on MacOS....
367#ifdef __APPLE__
368 *err = ioctl(fd, DKIOCGETBLOCKCOUNT, &sectors);
369#else
370 *err = ioctl(fd, BLKGETSIZE, &sz);
371 if (*err) {
372 sz = 0;
373 if (errno != EFBIG)
374 return sz;
375 }
376 *err = ioctl(fd, BLKGETSIZE64, &b);
377 if (*err || b == 0 || b == sz)
378 sectors = sz;
379 else
380 sectors = (b >> 9);
381#endif
382 return sectors;
383}