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> |
| 7 | |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 8 | #if defined (__FreeBSD__) || defined (__APPLE__) |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 9 | // Darwin (Mac OS) only: disk IOCTLs are different, and there is no lseek64 |
srs5694 | da79b85 | 2009-08-18 13:29:54 -0400 | [diff] [blame] | 10 | // This used to use __DARWIN_UNIX03 rather than __APPLE__, but __APPLE__ |
| 11 | // is more general. If the code fails to work on older versions of OS X/ |
| 12 | // Darwin, this may need to be changed back (and in various .cc files). |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 13 | #include <sys/disk.h> |
| 14 | #define lseek64 lseek |
| 15 | #else |
| 16 | |
| 17 | // Linux only.... |
| 18 | #include <linux/fs.h> |
| 19 | #endif |
| 20 | |
srs5694 | e35eb1b | 2009-09-14 00:29:34 -0400 | [diff] [blame^] | 21 | #include <string.h> |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 22 | |
| 23 | #ifndef __GPTSUPPORT |
| 24 | #define __GPTSUPPORT |
| 25 | |
| 26 | // Set this as a default |
| 27 | #define SECTOR_SIZE UINT32_C(512) |
| 28 | |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 29 | // Signatures for Apple (APM) disks, multiplied by 0x100000000 |
| 30 | #define APM_SIGNATURE1 UINT64_C(0x00004D5000000000) |
| 31 | #define APM_SIGNATURE2 UINT64_C(0x0000535400000000) |
| 32 | |
| 33 | /************************** |
| 34 | * Some GPT constants.... * |
| 35 | **************************/ |
| 36 | |
| 37 | #define GPT_SIGNATURE UINT64_C(0x5452415020494645) |
| 38 | |
| 39 | // Number and size of GPT entries... |
| 40 | #define NUM_GPT_ENTRIES 128 |
| 41 | #define GPT_SIZE 128 |
| 42 | #define HEADER_SIZE 92 |
| 43 | #define GPT_RESERVED 420 |
| 44 | #define NAME_SIZE 72 |
| 45 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 46 | using namespace std; |
| 47 | |
| 48 | // a GUID |
| 49 | struct GUIDData { |
| 50 | uint64_t data1; |
| 51 | uint64_t data2; |
| 52 | }; // struct GUIDData |
| 53 | |
| 54 | int GetNumber(int low, int high, int def, const char prompt[]); |
| 55 | char GetYN(void); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 56 | uint64_t GetSectorNum(uint64_t low, uint64_t high, uint64_t def, char prompt[]); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 57 | char* BytesToSI(uint64_t size, char theValue[]); |
| 58 | int GetBlockSize(int fd); |
| 59 | char* GUIDToStr(struct GUIDData theGUID, char* theString); |
| 60 | GUIDData GetGUID(void); |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 61 | 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] | 62 | void ReverseBytes(void* theValue, int numBytes); // Reverses byte-order of theValue |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 63 | uint64_t PowerOf2(int value); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 64 | int OpenForWrite(char* deviceFilename); |
srs5694 | e35eb1b | 2009-09-14 00:29:34 -0400 | [diff] [blame^] | 65 | void DiskSync(int fd); // resync disk caches to use new partitions |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 66 | |
| 67 | uint64_t disksize(int fd, int* err); |
| 68 | |
| 69 | #endif |