blob: d7954403c82d1136651f5c954f2b60f65e64c417 [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
Roderick W. Smithe3ee7332013-09-24 12:56:11 -04006/* This program is copyright (c) 2009-2013 by Roderick W. Smith. It is distributed
srs5694221e0872009-08-29 15:00:31 -04007 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>
srs569408bb0da2010-02-19 17:19:55 -050020#include <sstream>
srs5694e7b4ff92009-08-18 13:16:10 -040021#include "support.h"
22
23#include <sys/types.h>
24
srs56945d58fe02010-01-03 20:57:08 -050025// As of 1/2010, BLKPBSZGET is very new, so I'm explicitly defining it if
26// it's not already defined. This should become unnecessary in the future.
27// Note that this is a Linux-only ioctl....
28#ifndef BLKPBSZGET
29#define BLKPBSZGET _IO(0x12,123)
30#endif
31
srs5694e7b4ff92009-08-18 13:16:10 -040032using namespace std;
33
srs56945a608532011-03-17 13:53:01 -040034// Reads a string from stdin, returning it as a C++-style string.
35// Note that the returned string will NOT include the carriage return
36// entered by the user.
37string ReadString(void) {
38 string inString;
39
40 getline(cin, inString);
srs5694a6297b82012-03-25 16:13:16 -040041 if (!cin.good())
42 exit(5);
srs56945a608532011-03-17 13:53:01 -040043 return inString;
44} // ReadString()
srs5694bf8950c2011-03-12 01:23:12 -050045
srs5694e7b4ff92009-08-18 13:16:10 -040046// Get a numeric value from the user, between low and high (inclusive).
47// Keeps looping until the user enters a value within that range.
48// If user provides no input, def (default value) is returned.
49// (If def is outside of the low-high range, an explicit response
50// is required.)
srs5694fed16d02010-01-27 23:03:40 -050051int GetNumber(int low, int high, int def, const string & prompt) {
srs5694e7b4ff92009-08-18 13:16:10 -040052 int response, num;
53 char line[255];
54
55 if (low != high) { // bother only if low and high differ...
srs56940873e9d2010-10-07 13:00:45 -040056 do {
srs5694fed16d02010-01-27 23:03:40 -050057 cout << prompt;
58 cin.getline(line, 255);
srs5694a6297b82012-03-25 16:13:16 -040059 if (!cin.good())
60 exit(5);
srs5694e7b4ff92009-08-18 13:16:10 -040061 num = sscanf(line, "%d", &response);
62 if (num == 1) { // user provided a response
63 if ((response < low) || (response > high))
srs5694fed16d02010-01-27 23:03:40 -050064 cout << "Value out of range\n";
srs5694e7b4ff92009-08-18 13:16:10 -040065 } else { // user hit enter; return default
66 response = def;
67 } // if/else
srs56940873e9d2010-10-07 13:00:45 -040068 } while ((response < low) || (response > high));
srs5694e7b4ff92009-08-18 13:16:10 -040069 } else { // low == high, so return this value
srs5694fed16d02010-01-27 23:03:40 -050070 cout << "Using " << low << "\n";
srs5694e7b4ff92009-08-18 13:16:10 -040071 response = low;
72 } // else
73 return (response);
74} // GetNumber()
75
76// Gets a Y/N response (and converts lowercase to uppercase)
77char GetYN(void) {
srs56940873e9d2010-10-07 13:00:45 -040078 char response;
srs56945a608532011-03-17 13:53:01 -040079 string line;
srs5694e7b4ff92009-08-18 13:16:10 -040080
srs56940873e9d2010-10-07 13:00:45 -040081 do {
srs5694fed16d02010-01-27 23:03:40 -050082 cout << "(Y/N): ";
srs56945a608532011-03-17 13:53:01 -040083 line = ReadString();
srs569401f7f082011-03-15 23:53:31 -040084 response = toupper(line[0]);
srs56940873e9d2010-10-07 13:00:45 -040085 } while ((response != 'Y') && (response != 'N'));
srs5694e7b4ff92009-08-18 13:16:10 -040086 return response;
87} // GetYN(void)
88
srs5694e4ac11e2009-08-31 10:13:04 -040089// Obtains a sector number, between low and high, from the
srs5694e7b4ff92009-08-18 13:16:10 -040090// user, accepting values prefixed by "+" to add sectors to low,
srs56940873e9d2010-10-07 13:00:45 -040091// or the same with "K", "M", "G", "T", or "P" as suffixes to add
92// kilobytes, megabytes, gigabytes, terabytes, or petabytes,
93// respectively. If a "-" prefix is used, use the high value minus
94// the user-specified number of sectors (or KiB, MiB, etc.). Use the
95// def value as the default if the user just hits Enter. The sSize is
96// the sector size of the device.
srs5694df9d3632011-01-08 18:33:24 -050097uint64_t GetSectorNum(uint64_t low, uint64_t high, uint64_t def, uint64_t sSize,
98 const string & prompt) {
99 uint64_t response;
100 char line[255];
srs56940873e9d2010-10-07 13:00:45 -0400101
102 do {
srs5694fed16d02010-01-27 23:03:40 -0500103 cout << prompt;
104 cin.getline(line, 255);
srs5694a6297b82012-03-25 16:13:16 -0400105 if (!cin.good())
106 exit(5);
srs569401f7f082011-03-15 23:53:31 -0400107 response = IeeeToInt(line, sSize, low, high, def);
srs56940873e9d2010-10-07 13:00:45 -0400108 } while ((response < low) || (response > high));
srs569455d92612010-03-07 22:16:07 -0500109 return response;
srs5694e4ac11e2009-08-31 10:13:04 -0400110} // GetSectorNum()
srs5694e7b4ff92009-08-18 13:16:10 -0400111
srs569401f7f082011-03-15 23:53:31 -0400112// Convert an IEEE-1541-2002 value (K, M, G, T, P, or E) to its equivalent in
srs5694df9d3632011-01-08 18:33:24 -0500113// number of sectors. If no units are appended, interprets as the number
114// of sectors; otherwise, interprets as number of specified units and
115// converts to sectors. For instance, with 512-byte sectors, "1K" converts
116// to 2. If value includes a "+", adds low and subtracts 1; if SIValue
srs569401f7f082011-03-15 23:53:31 -0400117// inclues a "-", subtracts from high. If IeeeValue is empty, returns def.
srs56946aae2a92011-06-10 01:16:51 -0400118// Returns final sector value. In case inValue is invalid, returns 0 (a
srs5694a17fe692011-09-10 20:30:20 -0400119// sector value that's always in use on GPT and therefore invalid); and if
srs56946aae2a92011-06-10 01:16:51 -0400120// inValue works out to something outside the range low-high, returns the
121// computed value; the calling function is responsible for checking the
122// validity of this value.
123// NOTE: There's a difference in how GCC and VC++ treat oversized values
124// (say, "999999999999999999999") read via the ">>" operator; GCC turns
125// them into the maximum value for the type, whereas VC++ turns them into
126// 0 values. The result is that IeeeToInt() returns UINT64_MAX when
127// compiled with GCC (and so the value is rejected), whereas when VC++
128// is used, the default value is returned.
srs569401f7f082011-03-15 23:53:31 -0400129uint64_t IeeeToInt(string inValue, uint64_t sSize, uint64_t low, uint64_t high, uint64_t def) {
130 uint64_t response = def, bytesPerUnit = 1, mult = 1, divide = 1;
131 size_t foundAt = 0;
132 char suffix, plusFlag = ' ';
133 string suffixes = "KMGTPE";
srs56946aae2a92011-06-10 01:16:51 -0400134 int badInput = 0; // flag bad input; once this goes to 1, other values are irrelevant
srs5694df9d3632011-01-08 18:33:24 -0500135
136 if (sSize == 0) {
137 sSize = SECTOR_SIZE;
138 cerr << "Bug: Sector size invalid in SIToInt()!\n";
139 } // if
140
141 // Remove leading spaces, if present
srs569401f7f082011-03-15 23:53:31 -0400142 while (inValue[0] == ' ')
143 inValue.erase(0, 1);
srs5694df9d3632011-01-08 18:33:24 -0500144
srs569401f7f082011-03-15 23:53:31 -0400145 // If present, flag and remove leading plus or minus sign
146 if ((inValue[0] == '+') || (inValue[0] == '-')) {
147 plusFlag = inValue[0];
148 inValue.erase(0, 1);
srs5694df9d3632011-01-08 18:33:24 -0500149 } // if
150
151 // Extract numeric response and, if present, suffix
srs569401f7f082011-03-15 23:53:31 -0400152 istringstream inString(inValue);
srs56946aae2a92011-06-10 01:16:51 -0400153 if (((inString.peek() < '0') || (inString.peek() > '9')) && (inString.peek() != -1))
154 badInput = 1;
155 inString >> response >> suffix;
156 suffix = toupper(suffix);
157
158 // If no response, or if response == 0, use default (def)
159 if ((inValue.length() == 0) || (response == 0)) {
160 response = def;
161 suffix = ' ';
162 plusFlag = ' ';
163 } // if
164
165 // Find multiplication and division factors for the suffix
166 foundAt = suffixes.find(suffix);
167 if (foundAt != string::npos) {
168 bytesPerUnit = UINT64_C(1) << (10 * (foundAt + 1));
169 mult = bytesPerUnit / sSize;
170 divide = sSize / bytesPerUnit;
171 } // if
172
173 // Adjust response based on multiplier and plus flag, if present
174 if (mult > 1) {
175 if (response > (UINT64_MAX / mult))
176 badInput = 1;
177 else
srs569401f7f082011-03-15 23:53:31 -0400178 response *= mult;
srs56946aae2a92011-06-10 01:16:51 -0400179 } else if (divide > 1) {
srs569401f7f082011-03-15 23:53:31 -0400180 response /= divide;
srs56946aae2a92011-06-10 01:16:51 -0400181 } // if/elseif
182
183 if (plusFlag == '+') {
184 // Recompute response based on low part of range (if default == high
185 // value, which should be the case when prompting for the end of a
186 // range) or the defaut value (if default != high, which should be
187 // the case for the first sector of a partition).
188 if (def == high) {
189 if (response > 0)
190 response--;
191 if (response > (UINT64_MAX - low))
192 badInput = 1;
193 else
194 response = response + low;
195 } else {
196 if (response > (UINT64_MAX - def))
197 badInput = 1;
srs569401f7f082011-03-15 23:53:31 -0400198 else
199 response = response + def;
srs56946aae2a92011-06-10 01:16:51 -0400200 } // if/else
201 } else if (plusFlag == '-') {
202 if (response > high)
203 badInput = 1;
204 else
srs569401f7f082011-03-15 23:53:31 -0400205 response = high - response;
srs56946aae2a92011-06-10 01:16:51 -0400206 } // if
207
208 if (badInput)
209 response = UINT64_C(0);
srs5694df9d3632011-01-08 18:33:24 -0500210
211 return response;
srs569401f7f082011-03-15 23:53:31 -0400212} // IeeeToInt()
srs5694df9d3632011-01-08 18:33:24 -0500213
srs569401f7f082011-03-15 23:53:31 -0400214// Takes a size and converts this to a size in IEEE-1541-2002 units (KiB, MiB,
215// GiB, TiB, PiB, or EiB), returned in C++ string form. The size is either in
216// units of the sector size or, if that parameter is omitted, in bytes.
srs56940873e9d2010-10-07 13:00:45 -0400217// (sectorSize defaults to 1).
srs569401f7f082011-03-15 23:53:31 -0400218string BytesToIeee(uint64_t size, uint32_t sectorSize) {
219 float sizeInIeee;
srs56945a608532011-03-17 13:53:01 -0400220 unsigned int index = 0;
srs56946aae2a92011-06-10 01:16:51 -0400221 string units, prefixes = " KMGTPEZ";
srs569408bb0da2010-02-19 17:19:55 -0500222 ostringstream theValue;
srs5694e7b4ff92009-08-18 13:16:10 -0400223
srs569401f7f082011-03-15 23:53:31 -0400224 sizeInIeee = size * (float) sectorSize;
225 while ((sizeInIeee > 1024.0) && (index < (prefixes.length() - 1))) {
226 index++;
227 sizeInIeee /= 1024.0;
228 } // while
srs569408bb0da2010-02-19 17:19:55 -0500229 theValue.setf(ios::fixed);
srs569401f7f082011-03-15 23:53:31 -0400230 if (prefixes[index] == ' ') {
231 units = " bytes";
srs569408bb0da2010-02-19 17:19:55 -0500232 theValue.precision(0);
srs5694fed16d02010-01-27 23:03:40 -0500233 } else {
srs569401f7f082011-03-15 23:53:31 -0400234 units = " iB";
235 units[1] = prefixes[index];
srs569408bb0da2010-02-19 17:19:55 -0500236 theValue.precision(1);
srs5694fed16d02010-01-27 23:03:40 -0500237 } // if/else
srs569401f7f082011-03-15 23:53:31 -0400238 theValue << sizeInIeee << units;
srs569408bb0da2010-02-19 17:19:55 -0500239 return theValue.str();
srs569401f7f082011-03-15 23:53:31 -0400240} // BlocksToIeee()
srs5694e7b4ff92009-08-18 13:16:10 -0400241
srs56946699b012010-02-04 00:55:30 -0500242// Converts two consecutive characters in the input string into a
243// number, interpreting the string as a hexadecimal number, starting
244// at the specified position.
245unsigned char StrToHex(const string & input, unsigned int position) {
246 unsigned char retval = 0x00;
247 unsigned int temp;
srs5694e7b4ff92009-08-18 13:16:10 -0400248
srs56945a608532011-03-17 13:53:01 -0400249 if (input.length() > position) {
srs56946699b012010-02-04 00:55:30 -0500250 sscanf(input.substr(position, 2).c_str(), "%x", &temp);
251 retval = (unsigned char) temp;
srs5694e7b4ff92009-08-18 13:16:10 -0400252 } // if
srs56946699b012010-02-04 00:55:30 -0500253 return retval;
254} // StrToHex()
srs5694e7b4ff92009-08-18 13:16:10 -0400255
srs56940873e9d2010-10-07 13:00:45 -0400256// Returns 1 if input can be interpreted as a hexadecimal number --
257// all characters must be spaces, digits, or letters A-F (upper- or
srs56946aae2a92011-06-10 01:16:51 -0400258// lower-case), with at least one valid hexadecimal digit; with the
259// exception of the first two characters, which may be "0x"; otherwise
srs56940873e9d2010-10-07 13:00:45 -0400260// returns 0.
srs56946aae2a92011-06-10 01:16:51 -0400261int IsHex(string input) {
srs56940873e9d2010-10-07 13:00:45 -0400262 int isHex = 1, foundHex = 0, i;
263
srs56946aae2a92011-06-10 01:16:51 -0400264 if (input.substr(0, 2) == "0x")
265 input.erase(0, 2);
srs56940873e9d2010-10-07 13:00:45 -0400266 for (i = 0; i < (int) input.length(); i++) {
267 if ((input[i] < '0') || (input[i] > '9')) {
268 if ((input[i] < 'A') || (input[i] > 'F')) {
269 if ((input[i] < 'a') || (input[i] > 'f')) {
270 if ((input[i] != ' ') && (input[i] != '\n')) {
271 isHex = 0;
272 }
273 } else foundHex = 1;
274 } else foundHex = 1;
275 } else foundHex = 1;
276 } // for
277 if (!foundHex)
278 isHex = 0;
279 return isHex;
280} // IsHex()
281
srs56942a9f5da2009-08-26 00:48:01 -0400282// Return 1 if the CPU architecture is little endian, 0 if it's big endian....
283int IsLittleEndian(void) {
284 int littleE = 1; // assume little-endian (Intel-style)
285 union {
286 uint32_t num;
287 unsigned char uc[sizeof(uint32_t)];
288 } endian;
289
290 endian.num = 1;
291 if (endian.uc[0] != (unsigned char) 1) {
292 littleE = 0;
293 } // if
294 return (littleE);
295} // IsLittleEndian()
296
297// Reverse the byte order of theValue; numBytes is number of bytes
srs5694221e0872009-08-29 15:00:31 -0400298void ReverseBytes(void* theValue, int numBytes) {
srs5694fed16d02010-01-27 23:03:40 -0500299 char* tempValue = NULL;
srs56942a9f5da2009-08-26 00:48:01 -0400300 int i;
301
srs5694cb76c672010-02-11 22:22:22 -0500302 tempValue = new char [numBytes];
srs5694fed16d02010-01-27 23:03:40 -0500303 if (tempValue != NULL) {
304 memcpy(tempValue, theValue, numBytes);
305 for (i = 0; i < numBytes; i++)
306 ((char*) theValue)[i] = tempValue[numBytes - i - 1];
srs5694cb76c672010-02-11 22:22:22 -0500307 delete[] tempValue;
srs56946aae2a92011-06-10 01:16:51 -0400308 } else {
309 cerr << "Could not allocate memory in ReverseBytes()! Terminating\n";
310 exit(1);
311 } // if/else
srs56942a9f5da2009-08-26 00:48:01 -0400312} // ReverseBytes()
srs5694a17fe692011-09-10 20:30:20 -0400313
314// On Windows, display a warning and ask whether to continue. If the user elects
315// not to continue, exit immediately.
316void WinWarning(void) {
317 #ifdef _WIN32
318 cout << "\a************************************************************************\n"
319 << "Most versions of Windows cannot boot from a GPT disk, and most varieties\n"
320 << "prior to Vista cannot read GPT disks. Therefore, you should exit now\n"
321 << "unless you understand the implications of converting MBR to GPT or creating\n"
322 << "a new GPT disk layout!\n"
323 << "************************************************************************\n\n";
324 cout << "Are you SURE you want to continue? ";
325 if (GetYN() != 'Y')
326 exit(0);
327 #endif
328} // WinWarning()