blob: d3a6357f248e495de25fb3010b4b5207d7721707 [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 = ' ';
99 } // if
100
101 // Set multiplier based on suffix
102 switch (suffix) {
103 case 'K':
104 case 'k':
105 mult = (uint64_t) 1024 / SECTOR_SIZE;
106 break;
107 case 'M':
108 case 'm':
109 mult = (uint64_t) 1048576 / SECTOR_SIZE;
110 break;
111 case 'G':
112 case 'g':
113 mult = (uint64_t) 1073741824 / SECTOR_SIZE;
114 break;
115 case 'T':
116 case 't':
117 mult = ((uint64_t) 1073741824 * (uint64_t) 1024) / (uint64_t) SECTOR_SIZE;
118 break;
119 default:
120 mult = 1;
121 } // switch
122
123 // Adjust response based on multiplier and plus flag, if present
124 response *= (unsigned long long) mult;
125 if (plusFlag == 1) {
126 response = response + (unsigned long long) low - 1;
127 } // if/else
128 } // while
129 return ((uint64_t) response);
130} // GetLastSector()
131
132// Return a plain-text name for a partition type.
133// 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) {
184 printf("\aWARNING! Sector size is not 512 bytes! This program is likely to");
185 printf("misbehave! Proceed at your own risk!\n\n");
186 } // if
187
188 if (err == -1)
189 result = -1;
190 return (result);
191} // GetBlockSize()
192
193// Convert a GUID to a string representation, suitable for display
194// to humans....
195char* GUIDToStr(struct GUIDData theGUID, char* theString) {
196 uint64_t block;
197
198 if (theString != NULL) {
199 block = (theGUID.data1 & UINT64_C(0x00000000FFFFFFFF));
200 sprintf(theString, "%08llX-", (unsigned long long) block);
201 block = (theGUID.data1 & UINT64_C(0x0000FFFF00000000)) >> 32;
202 sprintf(theString, "%s%04llX-", theString, (unsigned long long) block);
203 block = (theGUID.data1 & UINT64_C(0xFFFF000000000000)) >> 48;
204 sprintf(theString, "%s%04llX-", theString, (unsigned long long) block);
205 block = (theGUID.data2 & UINT64_C(0x00000000000000FF));
206 sprintf(theString, "%s%02llX", theString, (unsigned long long) block);
207 block = (theGUID.data2 & UINT64_C(0x000000000000FF00)) >> 8;
208 sprintf(theString, "%s%02llX-", theString, (unsigned long long) block);
209 block = (theGUID.data2 & UINT64_C(0x0000000000FF0000)) >> 16;
210 sprintf(theString, "%s%02llX", theString, (unsigned long long) block);
211 block = (theGUID.data2 & UINT64_C(0x00000000FF000000)) >> 24;
212 sprintf(theString, "%s%02llX", theString, (unsigned long long) block);
213 block = (theGUID.data2 & UINT64_C(0x000000FF00000000)) >> 32;
214 sprintf(theString, "%s%02llX", theString, (unsigned long long) block);
215 block = (theGUID.data2 & UINT64_C(0x0000FF0000000000)) >> 40;
216 sprintf(theString, "%s%02llX", theString, (unsigned long long) block);
217 block = (theGUID.data2 & UINT64_C(0x00FF000000000000)) >> 48;
218 sprintf(theString, "%s%02llX", theString, (unsigned long long) block);
219 block = (theGUID.data2 & UINT64_C(0xFF00000000000000)) >> 56;
220 sprintf(theString, "%s%02llX", theString, (unsigned long long) block);
221 } // if
222 return theString;
223} // GUIDToStr()
224
225// Get a GUID from the user
226GUIDData GetGUID(void) {
227 uint64_t part1, part2, part3, part4, part5;
228 int entered = 0;
229 char temp[255], temp2[255];
230 GUIDData theGUID;
231
232 printf("\nA GUID is entered in five segments of from two to six bytes, with\n"
233 "dashes between segments.\n");
234 printf("Enter the entire GUID, a four-byte hexadecimal number for the first segment, or\n"
235 "'R' to generate the entire GUID randomly: ");
236 fgets(temp, 255, stdin);
237
238 // If user entered 'r' or 'R', generate GUID randomly....
239 if ((temp[0] == 'r') || (temp[0] == 'R')) {
240 theGUID.data1 = (uint64_t) rand() * (uint64_t) rand();
241 theGUID.data2 = (uint64_t) rand() * (uint64_t) rand();
242 entered = 1;
243 } // if user entered 'R' or 'r'
244
245 // If string length is right for whole entry, try to parse it....
246 if ((strlen(temp) == 37) && (entered == 0)) {
247 strncpy(temp2, &temp[0], 8);
248 temp2[8] = '\0';
249 sscanf(temp2, "%llx", &part1);
250 strncpy(temp2, &temp[9], 4);
251 temp2[4] = '\0';
252 sscanf(temp2, "%llx", &part2);
253 strncpy(temp2, &temp[14], 4);
254 temp2[4] = '\0';
255 sscanf(temp2, "%llx", &part3);
256 theGUID.data1 = (part3 << 48) + (part2 << 32) + part1;
257 strncpy(temp2, &temp[19], 4);
258 temp2[4] = '\0';
259 sscanf(temp2, "%llx", &part4);
260 strncpy(temp2, &temp[24], 12);
261 temp2[12] = '\0';
262 sscanf(temp2, "%llx", &part5);
263 theGUID.data2 = ((part4 & UINT64_C(0x000000000000FF00)) >> 8) +
264 ((part4 & UINT64_C(0x00000000000000FF)) << 8) +
265 ((part5 & UINT64_C(0x0000FF0000000000)) >> 24) +
266 ((part5 & UINT64_C(0x000000FF00000000)) >> 8) +
267 ((part5 & UINT64_C(0x00000000FF000000)) << 8) +
268 ((part5 & UINT64_C(0x0000000000FF0000)) << 24) +
269 ((part5 & UINT64_C(0x000000000000FF00)) << 40) +
270 ((part5 & UINT64_C(0x00000000000000FF)) << 56);
271 entered = 1;
272 } // if
273
274 // If neither of the above methods of entry was used, use prompted
275 // entry....
276 if (entered == 0) {
277 sscanf(temp, "%llx", &part1);
278 printf("Enter a two-byte hexadecimal number for the second segment: ");
279 fgets(temp, 255, stdin);
280 sscanf(temp, "%llx", &part2);
281 printf("Enter a two-byte hexadecimal number for the third segment: ");
282 fgets(temp, 255, stdin);
283 sscanf(temp, "%llx", &part3);
284 theGUID.data1 = (part3 << 48) + (part2 << 32) + part1;
285 printf("Enter a two-byte hexadecimal number for the fourth segment: ");
286 fgets(temp, 255, stdin);
287 sscanf(temp, "%llx", &part4);
288 printf("Enter a six-byte hexadecimal number for the fifth segment: ");
289 fgets(temp, 255, stdin);
290 sscanf(temp, "%llx", &part5);
291 theGUID.data2 = ((part4 & UINT64_C(0x000000000000FF00)) >> 8) +
292 ((part4 & UINT64_C(0x00000000000000FF)) << 8) +
293 ((part5 & UINT64_C(0x0000FF0000000000)) >> 24) +
294 ((part5 & UINT64_C(0x000000FF00000000)) >> 8) +
295 ((part5 & UINT64_C(0x00000000FF000000)) << 8) +
296 ((part5 & UINT64_C(0x0000000000FF0000)) << 24) +
297 ((part5 & UINT64_C(0x000000000000FF00)) << 40) +
298 ((part5 & UINT64_C(0x00000000000000FF)) << 56);
299 entered = 1;
300 } // if/else
301 printf("New GUID: %s\n", GUIDToStr(theGUID, temp));
302 return theGUID;
303} // GetGUID()
304
305// Compute (2 ^ value). Given the return type, value must be 63 or less.
306// Used in some bit-fiddling functions
307uint64_t PowerOf2(int value) {
308 uint64_t retval = 1;
309 int i;
310
311 if ((value < 64) && (value >= 0)) {
312 for (i = 0; i < value; i++) {
313 retval *= 2;
314 } // for
315 } else retval = 0;
316 return retval;
317} // PowerOf2()
318
319/**************************************************************************************
320 * *
321 * Below functions are lifted from various sources, as documented in comments before *
322 * each one. *
323 * *
324 **************************************************************************************/
325
326// The disksize function is taken from the Linux fdisk code and modified
327// to work around a problem returning a uint64_t value on Mac OS.
328uint64_t disksize(int fd, int *err) {
329 long sz; // Do not delete; needed for Linux
330 long long b; // Do not delete; needed for Linux
331 uint64_t sectors;
332
333 // Note to self: I recall testing a simplified version of
334 // this code, similar to what's in the __APPLE__ block,
335 // on Linux, but I had some problems. IIRC, it ran OK on 32-bit
336 // systems but not on 64-bit. Keep this in mind in case of
337 // 32/64-bit issues on MacOS....
338#ifdef __APPLE__
339 *err = ioctl(fd, DKIOCGETBLOCKCOUNT, &sectors);
340#else
341 *err = ioctl(fd, BLKGETSIZE, &sz);
342 if (*err) {
343 sz = 0;
344 if (errno != EFBIG)
345 return sz;
346 }
347 *err = ioctl(fd, BLKGETSIZE64, &b);
348 if (*err || b == 0 || b == sz)
349 sectors = sz;
350 else
351 sectors = (b >> 9);
352#endif
353 return sectors;
354}