blob: 388b71aa736638727f89b193b505466ff5b1cfba [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
srs5694e7b4ff92009-08-18 13:16:10 -040012#include <stdio.h>
srs5694546a9c72010-01-26 16:00:26 -050013#include <string>
srs5694e7b4ff92009-08-18 13:16:10 -040014#include <stdint.h>
15#include <errno.h>
srs5694e4ac11e2009-08-31 10:13:04 -040016#include <fcntl.h>
srs5694e35eb1b2009-09-14 00:29:34 -040017#include <sys/stat.h>
srs5694e7b4ff92009-08-18 13:16:10 -040018#include "support.h"
19
20#include <sys/types.h>
21
srs56945d58fe02010-01-03 20:57:08 -050022// As of 1/2010, BLKPBSZGET is very new, so I'm explicitly defining it if
23// it's not already defined. This should become unnecessary in the future.
24// Note that this is a Linux-only ioctl....
25#ifndef BLKPBSZGET
26#define BLKPBSZGET _IO(0x12,123)
27#endif
28
srs5694e7b4ff92009-08-18 13:16:10 -040029using namespace std;
30
31// Get a numeric value from the user, between low and high (inclusive).
32// Keeps looping until the user enters a value within that range.
33// If user provides no input, def (default value) is returned.
34// (If def is outside of the low-high range, an explicit response
35// is required.)
36int GetNumber(int low, int high, int def, const char prompt[]) {
37 int response, num;
38 char line[255];
srs56945d58fe02010-01-03 20:57:08 -050039 char* junk;
srs5694e7b4ff92009-08-18 13:16:10 -040040
41 if (low != high) { // bother only if low and high differ...
42 response = low - 1; // force one loop by setting response outside range
43 while ((response < low) || (response > high)) {
srs56941d1448a2009-12-31 21:20:19 -050044 printf("%s", prompt);
srs56945d58fe02010-01-03 20:57:08 -050045 junk = fgets(line, 255, stdin);
srs5694e7b4ff92009-08-18 13:16:10 -040046 num = sscanf(line, "%d", &response);
47 if (num == 1) { // user provided a response
48 if ((response < low) || (response > high))
49 printf("Value out of range\n");
50 } else { // user hit enter; return default
51 response = def;
52 } // if/else
53 } // while
54 } else { // low == high, so return this value
55 printf("Using %d\n", low);
56 response = low;
57 } // else
58 return (response);
59} // GetNumber()
60
61// Gets a Y/N response (and converts lowercase to uppercase)
62char GetYN(void) {
63 char line[255];
64 char response = '\0';
srs56945d58fe02010-01-03 20:57:08 -050065 char* junk;
srs5694e7b4ff92009-08-18 13:16:10 -040066
67 while ((response != 'Y') && (response != 'N')) {
68 printf("(Y/N): ");
srs56945d58fe02010-01-03 20:57:08 -050069 junk = fgets(line, 255, stdin);
srs5694e7b4ff92009-08-18 13:16:10 -040070 sscanf(line, "%c", &response);
71 if (response == 'y') response = 'Y';
72 if (response == 'n') response = 'N';
73 } // while
74 return response;
75} // GetYN(void)
76
srs5694e4ac11e2009-08-31 10:13:04 -040077// Obtains a sector number, between low and high, from the
srs5694e7b4ff92009-08-18 13:16:10 -040078// user, accepting values prefixed by "+" to add sectors to low,
79// or the same with "K", "M", "G", or "T" as suffixes to add
80// kilobytes, megabytes, gigabytes, or terabytes, respectively.
srs5694e4ac11e2009-08-31 10:13:04 -040081// If a "-" prefix is used, use the high value minus the user-
82// specified number of sectors (or KiB, MiB, etc.). Use the def
83 //value as the default if the user just hits Enter
84uint64_t GetSectorNum(uint64_t low, uint64_t high, uint64_t def, char prompt[]) {
srs5694e7b4ff92009-08-18 13:16:10 -040085 unsigned long long response;
srs5694ba00fed2010-01-12 18:18:36 -050086 int num, plusFlag = 0;
srs5694e7b4ff92009-08-18 13:16:10 -040087 uint64_t mult = 1;
88 char suffix;
89 char line[255];
srs56945d58fe02010-01-03 20:57:08 -050090 char* junk;
srs5694e7b4ff92009-08-18 13:16:10 -040091
92 response = low - 1; // Ensure one pass by setting a too-low initial value
93 while ((response < low) || (response > high)) {
srs56941d1448a2009-12-31 21:20:19 -050094 printf("%s", prompt);
srs56945d58fe02010-01-03 20:57:08 -050095 junk = fgets(line, 255, stdin);
srs5694e7b4ff92009-08-18 13:16:10 -040096
97 // Remove leading spaces, if present
98 while (line[0] == ' ')
99 strcpy(line, &line[1]);
100
101 // If present, flag and remove leading plus sign
102 if (line[0] == '+') {
103 plusFlag = 1;
104 strcpy(line, &line[1]);
105 } // if
106
srs5694e4ac11e2009-08-31 10:13:04 -0400107 // If present, flag and remove leading minus sign
108 if (line[0] == '-') {
109 plusFlag = -1;
110 strcpy(line, &line[1]);
111 } // if
112
srs5694e7b4ff92009-08-18 13:16:10 -0400113 // Extract numeric response and, if present, suffix
114 num = sscanf(line, "%llu%c", &response, &suffix);
115
srs5694e4ac11e2009-08-31 10:13:04 -0400116 // If no response, use default (def)
srs5694e7b4ff92009-08-18 13:16:10 -0400117 if (num <= 0) {
srs5694e4ac11e2009-08-31 10:13:04 -0400118 response = (unsigned long long) def;
srs5694e7b4ff92009-08-18 13:16:10 -0400119 suffix = ' ';
srs5694e19ba092009-08-24 14:10:35 -0400120 plusFlag = 0;
srs5694e7b4ff92009-08-18 13:16:10 -0400121 } // if
122
123 // Set multiplier based on suffix
124 switch (suffix) {
125 case 'K':
126 case 'k':
127 mult = (uint64_t) 1024 / SECTOR_SIZE;
128 break;
129 case 'M':
130 case 'm':
131 mult = (uint64_t) 1048576 / SECTOR_SIZE;
132 break;
133 case 'G':
134 case 'g':
135 mult = (uint64_t) 1073741824 / SECTOR_SIZE;
136 break;
137 case 'T':
138 case 't':
139 mult = ((uint64_t) 1073741824 * (uint64_t) 1024) / (uint64_t) SECTOR_SIZE;
140 break;
141 default:
142 mult = 1;
143 } // switch
144
145 // Adjust response based on multiplier and plus flag, if present
146 response *= (unsigned long long) mult;
147 if (plusFlag == 1) {
srs5694ba00fed2010-01-12 18:18:36 -0500148 // Recompute response based on low part of range (if default = high
149 // value, which should be the case when prompting for the end of a
150 // range) or the defaut value (if default != high, which should be
151 // the case for the first sector of a partition).
152 if (def == high)
153 response = response + (unsigned long long) low - UINT64_C(1);
154 else
155 response = response + (unsigned long long) def - UINT64_C(1);
srs5694e4ac11e2009-08-31 10:13:04 -0400156 } // if
157 if (plusFlag == -1) {
158 response = (unsigned long long) high - response;
srs5694e19ba092009-08-24 14:10:35 -0400159 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400160 } // while
161 return ((uint64_t) response);
srs5694e4ac11e2009-08-31 10:13:04 -0400162} // GetSectorNum()
srs5694e7b4ff92009-08-18 13:16:10 -0400163
srs5694e7b4ff92009-08-18 13:16:10 -0400164// Takes a size in bytes (in size) and converts this to a size in
165// SI units (KiB, MiB, GiB, TiB, or PiB), returned in C++ string
166// form
167char* BytesToSI(uint64_t size, char theValue[]) {
168 char units[8];
169 float sizeInSI;
170
171 if (theValue != NULL) {
172 sizeInSI = (float) size;
173 strcpy (units, " bytes");
174 if (sizeInSI > 1024.0) {
175 sizeInSI /= 1024.0;
176 strcpy(units, " KiB");
177 } // if
178 if (sizeInSI > 1024.0) {
179 sizeInSI /= 1024.0;
180 strcpy(units, " MiB");
181 } // if
182 if (sizeInSI > 1024.0) {
183 sizeInSI /= 1024.0;
184 strcpy(units, " GiB");
185 } // if
186 if (sizeInSI > 1024.0) {
187 sizeInSI /= 1024.0;
188 strcpy(units, " TiB");
189 } // if
190 if (sizeInSI > 1024.0) {
191 sizeInSI /= 1024.0;
192 strcpy(units, " PiB");
193 } // if
194 if (strcmp(units, " bytes") == 0) { // in bytes, so no decimal point
195 sprintf(theValue, "%1.0f%s", sizeInSI, units);
196 } else {
197 sprintf(theValue, "%1.1f%s", sizeInSI, units);
198 } // if/else
199 } // if
200 return theValue;
201} // BlocksToSI()
202
srs56942a9f5da2009-08-26 00:48:01 -0400203// Return a plain-text name for a partition type.
srs5694e7b4ff92009-08-18 13:16:10 -0400204// Convert a GUID to a string representation, suitable for display
205// to humans....
206char* GUIDToStr(struct GUIDData theGUID, char* theString) {
srs56945d58fe02010-01-03 20:57:08 -0500207 unsigned long long blocks[11], block;
srs5694e7b4ff92009-08-18 13:16:10 -0400208
srs56945d58fe02010-01-03 20:57:08 -0500209 if (theString != NULL) {
210 blocks[0] = (theGUID.data1 & UINT64_C(0x00000000FFFFFFFF));
211 blocks[1] = (theGUID.data1 & UINT64_C(0x0000FFFF00000000)) >> 32;
212 blocks[2] = (theGUID.data1 & UINT64_C(0xFFFF000000000000)) >> 48;
213 blocks[3] = (theGUID.data2 & UINT64_C(0x00000000000000FF));
214 blocks[4] = (theGUID.data2 & UINT64_C(0x000000000000FF00)) >> 8;
215 blocks[5] = (theGUID.data2 & UINT64_C(0x0000000000FF0000)) >> 16;
216 blocks[6] = (theGUID.data2 & UINT64_C(0x00000000FF000000)) >> 24;
217 blocks[7] = (theGUID.data2 & UINT64_C(0x000000FF00000000)) >> 32;
218 blocks[8] = (theGUID.data2 & UINT64_C(0x0000FF0000000000)) >> 40;
219 blocks[9] = (theGUID.data2 & UINT64_C(0x00FF000000000000)) >> 48;
220 blocks[10] = (theGUID.data2 & UINT64_C(0xFF00000000000000)) >> 56;
221 sprintf(theString,
222 "%08llX-%04llX-%04llX-%02llX%02llX-%02llX%02llX%02llX%02llX%02llX%02llX",
223 blocks[0], blocks[1], blocks[2], blocks[3], blocks[4], blocks[5],
224 blocks[6], blocks[7], blocks[8], blocks[9], blocks[10]);
225 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400226 return theString;
227} // GUIDToStr()
228
229// Get a GUID from the user
230GUIDData GetGUID(void) {
srs56945d58fe02010-01-03 20:57:08 -0500231 unsigned long long part1, part2, part3, part4, part5;
srs5694e7b4ff92009-08-18 13:16:10 -0400232 int entered = 0;
233 char temp[255], temp2[255];
srs56945d58fe02010-01-03 20:57:08 -0500234 char* junk;
srs5694e7b4ff92009-08-18 13:16:10 -0400235 GUIDData theGUID;
236
237 printf("\nA GUID is entered in five segments of from two to six bytes, with\n"
238 "dashes between segments.\n");
239 printf("Enter the entire GUID, a four-byte hexadecimal number for the first segment, or\n"
240 "'R' to generate the entire GUID randomly: ");
srs56945d58fe02010-01-03 20:57:08 -0500241 junk = fgets(temp, 255, stdin);
srs5694e7b4ff92009-08-18 13:16:10 -0400242
243 // If user entered 'r' or 'R', generate GUID randomly....
244 if ((temp[0] == 'r') || (temp[0] == 'R')) {
245 theGUID.data1 = (uint64_t) rand() * (uint64_t) rand();
246 theGUID.data2 = (uint64_t) rand() * (uint64_t) rand();
247 entered = 1;
248 } // if user entered 'R' or 'r'
249
250 // If string length is right for whole entry, try to parse it....
251 if ((strlen(temp) == 37) && (entered == 0)) {
252 strncpy(temp2, &temp[0], 8);
253 temp2[8] = '\0';
254 sscanf(temp2, "%llx", &part1);
255 strncpy(temp2, &temp[9], 4);
256 temp2[4] = '\0';
257 sscanf(temp2, "%llx", &part2);
258 strncpy(temp2, &temp[14], 4);
259 temp2[4] = '\0';
260 sscanf(temp2, "%llx", &part3);
261 theGUID.data1 = (part3 << 48) + (part2 << 32) + part1;
262 strncpy(temp2, &temp[19], 4);
263 temp2[4] = '\0';
264 sscanf(temp2, "%llx", &part4);
265 strncpy(temp2, &temp[24], 12);
266 temp2[12] = '\0';
267 sscanf(temp2, "%llx", &part5);
268 theGUID.data2 = ((part4 & UINT64_C(0x000000000000FF00)) >> 8) +
269 ((part4 & UINT64_C(0x00000000000000FF)) << 8) +
270 ((part5 & UINT64_C(0x0000FF0000000000)) >> 24) +
271 ((part5 & UINT64_C(0x000000FF00000000)) >> 8) +
272 ((part5 & UINT64_C(0x00000000FF000000)) << 8) +
273 ((part5 & UINT64_C(0x0000000000FF0000)) << 24) +
274 ((part5 & UINT64_C(0x000000000000FF00)) << 40) +
275 ((part5 & UINT64_C(0x00000000000000FF)) << 56);
276 entered = 1;
277 } // if
278
279 // If neither of the above methods of entry was used, use prompted
280 // entry....
281 if (entered == 0) {
282 sscanf(temp, "%llx", &part1);
283 printf("Enter a two-byte hexadecimal number for the second segment: ");
srs56945d58fe02010-01-03 20:57:08 -0500284 junk = fgets(temp, 255, stdin);
srs5694e7b4ff92009-08-18 13:16:10 -0400285 sscanf(temp, "%llx", &part2);
286 printf("Enter a two-byte hexadecimal number for the third segment: ");
srs56945d58fe02010-01-03 20:57:08 -0500287 junk = fgets(temp, 255, stdin);
srs5694e7b4ff92009-08-18 13:16:10 -0400288 sscanf(temp, "%llx", &part3);
289 theGUID.data1 = (part3 << 48) + (part2 << 32) + part1;
290 printf("Enter a two-byte hexadecimal number for the fourth segment: ");
srs56945d58fe02010-01-03 20:57:08 -0500291 junk = fgets(temp, 255, stdin);
srs5694e7b4ff92009-08-18 13:16:10 -0400292 sscanf(temp, "%llx", &part4);
293 printf("Enter a six-byte hexadecimal number for the fifth segment: ");
srs56945d58fe02010-01-03 20:57:08 -0500294 junk = fgets(temp, 255, stdin);
srs5694e7b4ff92009-08-18 13:16:10 -0400295 sscanf(temp, "%llx", &part5);
296 theGUID.data2 = ((part4 & UINT64_C(0x000000000000FF00)) >> 8) +
297 ((part4 & UINT64_C(0x00000000000000FF)) << 8) +
298 ((part5 & UINT64_C(0x0000FF0000000000)) >> 24) +
299 ((part5 & UINT64_C(0x000000FF00000000)) >> 8) +
300 ((part5 & UINT64_C(0x00000000FF000000)) << 8) +
301 ((part5 & UINT64_C(0x0000000000FF0000)) << 24) +
302 ((part5 & UINT64_C(0x000000000000FF00)) << 40) +
303 ((part5 & UINT64_C(0x00000000000000FF)) << 56);
304 entered = 1;
305 } // if/else
306 printf("New GUID: %s\n", GUIDToStr(theGUID, temp));
307 return theGUID;
308} // GetGUID()
309
srs56942a9f5da2009-08-26 00:48:01 -0400310// Return 1 if the CPU architecture is little endian, 0 if it's big endian....
311int IsLittleEndian(void) {
312 int littleE = 1; // assume little-endian (Intel-style)
313 union {
314 uint32_t num;
315 unsigned char uc[sizeof(uint32_t)];
316 } endian;
317
318 endian.num = 1;
319 if (endian.uc[0] != (unsigned char) 1) {
320 littleE = 0;
321 } // if
322 return (littleE);
323} // IsLittleEndian()
324
325// Reverse the byte order of theValue; numBytes is number of bytes
srs5694221e0872009-08-29 15:00:31 -0400326void ReverseBytes(void* theValue, int numBytes) {
327 char* origValue;
srs56942a9f5da2009-08-26 00:48:01 -0400328 char* tempValue;
329 int i;
330
srs5694221e0872009-08-29 15:00:31 -0400331 origValue = (char*) theValue;
srs56942a9f5da2009-08-26 00:48:01 -0400332 tempValue = (char*) malloc(numBytes);
333 for (i = 0; i < numBytes; i++)
srs5694221e0872009-08-29 15:00:31 -0400334 tempValue[i] = origValue[i];
srs56942a9f5da2009-08-26 00:48:01 -0400335 for (i = 0; i < numBytes; i++)
srs5694221e0872009-08-29 15:00:31 -0400336 origValue[i] = tempValue[numBytes - i - 1];
srs56942a9f5da2009-08-26 00:48:01 -0400337 free(tempValue);
338} // ReverseBytes()
339
srs5694e7b4ff92009-08-18 13:16:10 -0400340// Compute (2 ^ value). Given the return type, value must be 63 or less.
341// Used in some bit-fiddling functions
342uint64_t PowerOf2(int value) {
343 uint64_t retval = 1;
344 int i;
345
346 if ((value < 64) && (value >= 0)) {
347 for (i = 0; i < value; i++) {
348 retval *= 2;
349 } // for
350 } else retval = 0;
351 return retval;
352} // PowerOf2()