Roderick W. Smith | e3ee733 | 2013-09-24 12:56:11 -0400 | [diff] [blame] | 1 | /* This program is copyright (c) 2009-2013 by Roderick W. Smith. It is distributed |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 2 | under the terms of the GNU GPL version 2, as detailed in the COPYING file. */ |
| 3 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 4 | #include <stdint.h> |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 5 | #include <stdlib.h> |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 6 | #include <string> |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 7 | |
| 8 | #ifndef __GPTSUPPORT |
| 9 | #define __GPTSUPPORT |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 10 | |
Aurimas Liutikas | 74b7490 | 2016-05-10 18:53:54 -0700 | [diff] [blame] | 11 | #define GPTFDISK_VERSION "1.0.1" |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 12 | |
srs5694 | 08bb0da | 2010-02-19 17:19:55 -0500 | [diff] [blame] | 13 | #if defined (__FreeBSD__) || defined (__FreeBSD_kernel__) || defined (__APPLE__) |
srs5694 | 00b6d7a | 2011-06-26 22:40:06 -0400 | [diff] [blame] | 14 | // Darwin (Mac OS) & FreeBSD: disk IOCTLs are different, and there is no lseek64 |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 15 | #include <sys/disk.h> |
| 16 | #define lseek64 lseek |
srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 17 | #endif |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 18 | |
srs5694 | 00b6d7a | 2011-06-26 22:40:06 -0400 | [diff] [blame] | 19 | #if defined (__FreeBSD__) || defined (__FreeBSD_kernel__) |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame] | 20 | #define DEFAULT_GPT_TYPE 0xA503 |
srs5694 | 00b6d7a | 2011-06-26 22:40:06 -0400 | [diff] [blame] | 21 | #endif |
| 22 | |
| 23 | #ifdef __APPLE__ |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame] | 24 | #define DEFAULT_GPT_TYPE 0xAF00 |
srs5694 | 00b6d7a | 2011-06-26 22:40:06 -0400 | [diff] [blame] | 25 | #endif |
| 26 | |
| 27 | #ifdef _WIN32 |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame] | 28 | #define DEFAULT_GPT_TYPE 0x0700 |
| 29 | #endif |
| 30 | |
| 31 | #ifdef __sun__ |
| 32 | #define DEFAULT_GPT_TYPE 0xbf01 |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 33 | #endif |
| 34 | |
srs5694 | bf8950c | 2011-03-12 01:23:12 -0500 | [diff] [blame] | 35 | // Microsoft Visual C++ only |
| 36 | #if defined (_MSC_VER) |
| 37 | #define sscanf sscanf_s |
| 38 | #define strcpy strcpy_s |
| 39 | #define sprintf sprintf_s |
| 40 | #endif |
| 41 | |
srs5694 | 00b6d7a | 2011-06-26 22:40:06 -0400 | [diff] [blame] | 42 | // Linux only.... |
| 43 | #ifdef __linux__ |
| 44 | #include <linux/fs.h> |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame] | 45 | #define DEFAULT_GPT_TYPE 0x8300 |
srs5694 | 00b6d7a | 2011-06-26 22:40:06 -0400 | [diff] [blame] | 46 | #endif |
| 47 | |
srs5694 | 0741fa2 | 2013-01-09 12:55:40 -0500 | [diff] [blame] | 48 | #ifndef DEFAULT_GPT_TYPE |
| 49 | #define DEFAULT_GPT_TYPE 0x8300 |
srs5694 | 00b6d7a | 2011-06-26 22:40:06 -0400 | [diff] [blame] | 50 | #endif |
| 51 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 52 | // Set this as a default |
| 53 | #define SECTOR_SIZE UINT32_C(512) |
| 54 | |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 55 | // Signatures for Apple (APM) disks, multiplied by 0x100000000 |
| 56 | #define APM_SIGNATURE1 UINT64_C(0x00004D5000000000) |
| 57 | #define APM_SIGNATURE2 UINT64_C(0x0000535400000000) |
| 58 | |
| 59 | /************************** |
| 60 | * Some GPT constants.... * |
| 61 | **************************/ |
| 62 | |
| 63 | #define GPT_SIGNATURE UINT64_C(0x5452415020494645) |
| 64 | |
| 65 | // Number and size of GPT entries... |
| 66 | #define NUM_GPT_ENTRIES 128 |
| 67 | #define GPT_SIZE 128 |
srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 68 | #define HEADER_SIZE UINT32_C(92) |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 69 | #define GPT_RESERVED 420 |
Roderick W. Smith | 84aaff6 | 2014-02-17 16:17:11 -0500 | [diff] [blame] | 70 | #define NAME_SIZE 36 // GPT allows 36 UTF-16LE code units for a name in a 128 byte partition entry |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 71 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 72 | using namespace std; |
| 73 | |
srs5694 | 5a60853 | 2011-03-17 13:53:01 -0400 | [diff] [blame] | 74 | string ReadString(void); |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 75 | int GetNumber(int low, int high, int def, const string & prompt); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 76 | char GetYN(void); |
srs5694 | 0873e9d | 2010-10-07 13:00:45 -0400 | [diff] [blame] | 77 | uint64_t GetSectorNum(uint64_t low, uint64_t high, uint64_t def, uint64_t sSize, const std::string& prompt); |
srs5694 | 01f7f08 | 2011-03-15 23:53:31 -0400 | [diff] [blame] | 78 | uint64_t IeeeToInt(string IeeeValue, uint64_t sSize, uint64_t low, uint64_t high, uint64_t def = 0); |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 79 | string BytesToIeee(uint64_t size, uint32_t sectorSize); |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 80 | unsigned char StrToHex(const string & input, unsigned int position); |
srs5694 | 6aae2a9 | 2011-06-10 01:16:51 -0400 | [diff] [blame] | 81 | int IsHex(string input); // Returns 1 if input can be hexadecimal number.... |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 82 | int IsLittleEndian(void); // Returns 1 if CPU is little-endian, 0 if it's big-endian |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 83 | void ReverseBytes(void* theValue, int numBytes); // Reverses byte-order of theValue |
srs5694 | a17fe69 | 2011-09-10 20:30:20 -0400 | [diff] [blame] | 84 | void WinWarning(void); |
srs5694 | 9ddc14b | 2010-08-22 22:44:42 -0400 | [diff] [blame] | 85 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 86 | #endif |