blob: 3e4ad6ca047ba4fc6c7f5dbb751a2913f40ee10f [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>
srs5694e7b4ff92009-08-18 13:16:10 -040013#include <stdint.h>
14#include <errno.h>
srs5694e4ac11e2009-08-31 10:13:04 -040015#include <fcntl.h>
srs5694fed16d02010-01-27 23:03:40 -050016#include <string.h>
srs5694e35eb1b2009-09-14 00:29:34 -040017#include <sys/stat.h>
srs5694fed16d02010-01-27 23:03:40 -050018#include <string>
19#include <iostream>
srs5694e7b4ff92009-08-18 13:16:10 -040020#include "support.h"
21
22#include <sys/types.h>
23
srs56945d58fe02010-01-03 20:57:08 -050024// As of 1/2010, BLKPBSZGET is very new, so I'm explicitly defining it if
25// it's not already defined. This should become unnecessary in the future.
26// Note that this is a Linux-only ioctl....
27#ifndef BLKPBSZGET
28#define BLKPBSZGET _IO(0x12,123)
29#endif
30
srs5694e7b4ff92009-08-18 13:16:10 -040031using namespace std;
32
33// Get a numeric value from the user, between low and high (inclusive).
34// Keeps looping until the user enters a value within that range.
35// If user provides no input, def (default value) is returned.
36// (If def is outside of the low-high range, an explicit response
37// is required.)
srs5694fed16d02010-01-27 23:03:40 -050038int GetNumber(int low, int high, int def, const string & prompt) {
srs5694e7b4ff92009-08-18 13:16:10 -040039 int response, num;
40 char line[255];
41
42 if (low != high) { // bother only if low and high differ...
43 response = low - 1; // force one loop by setting response outside range
44 while ((response < low) || (response > high)) {
srs5694fed16d02010-01-27 23:03:40 -050045 cout << prompt;
46 cin.getline(line, 255);
srs5694e7b4ff92009-08-18 13:16:10 -040047 num = sscanf(line, "%d", &response);
48 if (num == 1) { // user provided a response
49 if ((response < low) || (response > high))
srs5694fed16d02010-01-27 23:03:40 -050050 cout << "Value out of range\n";
srs5694e7b4ff92009-08-18 13:16:10 -040051 } else { // user hit enter; return default
52 response = def;
53 } // if/else
54 } // while
55 } else { // low == high, so return this value
srs5694fed16d02010-01-27 23:03:40 -050056 cout << "Using " << low << "\n";
srs5694e7b4ff92009-08-18 13:16:10 -040057 response = low;
58 } // else
59 return (response);
60} // GetNumber()
61
62// Gets a Y/N response (and converts lowercase to uppercase)
63char GetYN(void) {
64 char line[255];
65 char response = '\0';
srs5694fed16d02010-01-27 23:03:40 -050066 char *junk;
srs5694e7b4ff92009-08-18 13:16:10 -040067
68 while ((response != 'Y') && (response != 'N')) {
srs5694fed16d02010-01-27 23:03:40 -050069 cout << "(Y/N): ";
srs56945d58fe02010-01-03 20:57:08 -050070 junk = fgets(line, 255, stdin);
srs5694e7b4ff92009-08-18 13:16:10 -040071 sscanf(line, "%c", &response);
72 if (response == 'y') response = 'Y';
73 if (response == 'n') response = 'N';
74 } // while
75 return response;
76} // GetYN(void)
77
srs5694e4ac11e2009-08-31 10:13:04 -040078// Obtains a sector number, between low and high, from the
srs5694e7b4ff92009-08-18 13:16:10 -040079// user, accepting values prefixed by "+" to add sectors to low,
80// or the same with "K", "M", "G", or "T" as suffixes to add
81// kilobytes, megabytes, gigabytes, or terabytes, respectively.
srs5694e4ac11e2009-08-31 10:13:04 -040082// If a "-" prefix is used, use the high value minus the user-
83// specified number of sectors (or KiB, MiB, etc.). Use the def
84 //value as the default if the user just hits Enter
srs5694fed16d02010-01-27 23:03:40 -050085uint64_t GetSectorNum(uint64_t low, uint64_t high, uint64_t def, const string & prompt) {
srs5694e7b4ff92009-08-18 13:16:10 -040086 unsigned long long response;
srs5694ba00fed2010-01-12 18:18:36 -050087 int num, plusFlag = 0;
srs5694e7b4ff92009-08-18 13:16:10 -040088 uint64_t mult = 1;
89 char suffix;
90 char line[255];
91
92 response = low - 1; // Ensure one pass by setting a too-low initial value
93 while ((response < low) || (response > high)) {
srs5694fed16d02010-01-27 23:03:40 -050094 cout << prompt;
95 cin.getline(line, 255);
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
srs5694fed16d02010-01-27 23:03:40 -0500167string BytesToSI(uint64_t size) {
168 string units;
169 char theValue[99];
srs5694e7b4ff92009-08-18 13:16:10 -0400170 float sizeInSI;
171
srs5694fed16d02010-01-27 23:03:40 -0500172 theValue[0] = '\0';
173 sizeInSI = (float) size;
174 units = " bytes";
175 if (sizeInSI > 1024.0) {
176 sizeInSI /= 1024.0;
177 units = " KiB";
srs5694e7b4ff92009-08-18 13:16:10 -0400178 } // if
srs5694fed16d02010-01-27 23:03:40 -0500179 if (sizeInSI > 1024.0) {
180 sizeInSI /= 1024.0;
181 units = " MiB";
182 } // if
183 if (sizeInSI > 1024.0) {
184 sizeInSI /= 1024.0;
185 units = " GiB";
186 } // if
187 if (sizeInSI > 1024.0) {
188 sizeInSI /= 1024.0;
189 units = " TiB";
190 } // if
191 if (sizeInSI > 1024.0) {
192 sizeInSI /= 1024.0;
193 units = " PiB";
194 } // if
195 if (units == " bytes") { // in bytes, so no decimal point
196 sprintf(theValue, "%1.0f%s", sizeInSI, units.c_str());
197 } else {
198 sprintf(theValue, "%1.1f%s", sizeInSI, units.c_str());
199 } // if/else
srs5694e7b4ff92009-08-18 13:16:10 -0400200 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....
srs5694fed16d02010-01-27 23:03:40 -0500206string GUIDToStr(struct GUIDData theGUID) {
srs56945d58fe02010-01-03 20:57:08 -0500207 unsigned long long blocks[11], block;
srs5694fed16d02010-01-27 23:03:40 -0500208 char theString[40];
srs5694e7b4ff92009-08-18 13:16:10 -0400209
srs5694fed16d02010-01-27 23:03:40 -0500210 theString[0] = '\0';;
211 blocks[0] = (theGUID.data1 & UINT64_C(0x00000000FFFFFFFF));
212 blocks[1] = (theGUID.data1 & UINT64_C(0x0000FFFF00000000)) >> 32;
213 blocks[2] = (theGUID.data1 & UINT64_C(0xFFFF000000000000)) >> 48;
214 blocks[3] = (theGUID.data2 & UINT64_C(0x00000000000000FF));
215 blocks[4] = (theGUID.data2 & UINT64_C(0x000000000000FF00)) >> 8;
216 blocks[5] = (theGUID.data2 & UINT64_C(0x0000000000FF0000)) >> 16;
217 blocks[6] = (theGUID.data2 & UINT64_C(0x00000000FF000000)) >> 24;
218 blocks[7] = (theGUID.data2 & UINT64_C(0x000000FF00000000)) >> 32;
219 blocks[8] = (theGUID.data2 & UINT64_C(0x0000FF0000000000)) >> 40;
220 blocks[9] = (theGUID.data2 & UINT64_C(0x00FF000000000000)) >> 48;
221 blocks[10] = (theGUID.data2 & UINT64_C(0xFF00000000000000)) >> 56;
222 sprintf(theString,
223 "%08llX-%04llX-%04llX-%02llX%02llX-%02llX%02llX%02llX%02llX%02llX%02llX",
224 blocks[0], blocks[1], blocks[2], blocks[3], blocks[4], blocks[5],
225 blocks[6], blocks[7], blocks[8], blocks[9], blocks[10]);
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
srs5694fed16d02010-01-27 23:03:40 -0500237 cout << "\nA GUID is entered in five segments of from two to six bytes, with\n"
238 << "dashes between segments.\n";
239 cout << "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);
srs5694fed16d02010-01-27 23:03:40 -0500283 cout << "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);
srs5694fed16d02010-01-27 23:03:40 -0500286 cout << "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;
srs5694fed16d02010-01-27 23:03:40 -0500290 cout << "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);
srs5694fed16d02010-01-27 23:03:40 -0500293 cout << "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
srs5694fed16d02010-01-27 23:03:40 -0500306 cout << "New GUID: " << GUIDToStr(theGUID) << "\n";
srs5694e7b4ff92009-08-18 13:16:10 -0400307 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) {
srs5694fed16d02010-01-27 23:03:40 -0500327 char* tempValue = NULL;
srs56942a9f5da2009-08-26 00:48:01 -0400328 int i;
329
330 tempValue = (char*) malloc(numBytes);
srs5694fed16d02010-01-27 23:03:40 -0500331 if (tempValue != NULL) {
332 memcpy(tempValue, theValue, numBytes);
333 for (i = 0; i < numBytes; i++)
334 ((char*) theValue)[i] = tempValue[numBytes - i - 1];
335 free(tempValue);
336 } // if
srs56942a9f5da2009-08-26 00:48:01 -0400337} // ReverseBytes()
338
srs5694e7b4ff92009-08-18 13:16:10 -0400339// Compute (2 ^ value). Given the return type, value must be 63 or less.
340// Used in some bit-fiddling functions
341uint64_t PowerOf2(int value) {
342 uint64_t retval = 1;
343 int i;
344
345 if ((value < 64) && (value >= 0)) {
346 for (i = 0; i < value; i++) {
347 retval *= 2;
348 } // for
349 } else retval = 0;
350 return retval;
351} // PowerOf2()