| srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 1 | /* This program is copyright (c) 2009 by Roderick W. Smith. It is distributed | 
 | 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> | 
 | 5 | #include <unistd.h> | 
 | 6 | #include <stdlib.h> | 
| srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 7 | #include <string> | 
| srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 8 |  | 
 | 9 | #ifndef __GPTSUPPORT | 
 | 10 | #define __GPTSUPPORT | 
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 11 |  | 
| srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 12 | #if defined (__FreeBSD__) || defined (__APPLE__) | 
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 13 | // Darwin (Mac OS) only: disk IOCTLs are different, and there is no lseek64 | 
| srs5694 | da79b85 | 2009-08-18 13:29:54 -0400 | [diff] [blame] | 14 | // This used to use __DARWIN_UNIX03 rather than __APPLE__, but __APPLE__ | 
 | 15 | // is more general. If the code fails to work on older versions of OS X/ | 
 | 16 | // Darwin, this may need to be changed back (and in various .cc files). | 
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 17 | #include <sys/disk.h> | 
 | 18 | #define lseek64 lseek | 
| srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 19 | #endif | 
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 20 |  | 
 | 21 | // Linux only.... | 
| srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 22 | #ifdef __linux__ | 
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 23 | #include <linux/fs.h> | 
 | 24 | #endif | 
 | 25 |  | 
| srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 26 | /* #ifdef __FreeBSD__ | 
| srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 27 | #define fstat64 fstat | 
 | 28 | #define stat64 stat | 
| srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 29 | #endif */ | 
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 30 |  | 
 | 31 | // Set this as a default | 
 | 32 | #define SECTOR_SIZE UINT32_C(512) | 
 | 33 |  | 
| srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 34 | // Signatures for Apple (APM) disks, multiplied by 0x100000000 | 
 | 35 | #define APM_SIGNATURE1 UINT64_C(0x00004D5000000000) | 
 | 36 | #define APM_SIGNATURE2 UINT64_C(0x0000535400000000) | 
 | 37 |  | 
| srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 38 | // Maximum line length ignored on some input functions | 
 | 39 | #define MAX_IGNORED 999 | 
 | 40 |  | 
| srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 41 | /************************** | 
 | 42 |  * Some GPT constants.... * | 
 | 43 |  **************************/ | 
 | 44 |  | 
 | 45 | #define GPT_SIGNATURE UINT64_C(0x5452415020494645) | 
 | 46 |  | 
 | 47 | // Number and size of GPT entries... | 
 | 48 | #define NUM_GPT_ENTRIES 128 | 
 | 49 | #define GPT_SIZE 128 | 
| srs5694 | 978041c | 2009-09-21 20:51:47 -0400 | [diff] [blame] | 50 | #define HEADER_SIZE UINT32_C(92) | 
| srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 51 | #define GPT_RESERVED 420 | 
 | 52 | #define NAME_SIZE 72 | 
 | 53 |  | 
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 54 | using namespace std; | 
 | 55 |  | 
 | 56 | // a GUID | 
 | 57 | struct GUIDData { | 
 | 58 |    uint64_t data1; | 
 | 59 |    uint64_t data2; | 
 | 60 | }; // struct GUIDData | 
 | 61 |  | 
| srs5694 | 546a9c7 | 2010-01-26 16:00:26 -0500 | [diff] [blame] | 62 | static char theFile[255]; | 
 | 63 |  | 
| srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 64 | int GetNumber(int low, int high, int def, const string & prompt); | 
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 65 | char GetYN(void); | 
| srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame^] | 66 | uint64_t GetSectorNum(uint64_t low, uint64_t high, uint64_t def, const string & prompt); | 
 | 67 | string BytesToSI(uint64_t size); | 
 | 68 | string GUIDToStr(struct GUIDData theGUID); | 
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 69 | GUIDData GetGUID(void); | 
| srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 70 | 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] | 71 | void ReverseBytes(void* theValue, int numBytes); // Reverses byte-order of theValue | 
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 72 | uint64_t PowerOf2(int value); | 
| srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 73 |  | 
 | 74 | #endif |