blob: 8551e17567013effbef4b41550e12a3746bd28d3 [file] [log] [blame]
srs5694221e0872009-08-29 15:00:31 -04001/* 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
srs5694e7b4ff92009-08-18 13:16:10 -04004#include <stdint.h>
5#include <unistd.h>
6#include <stdlib.h>
srs5694978041c2009-09-21 20:51:47 -04007#include <string.h>
8
9#ifndef __GPTSUPPORT
10#define __GPTSUPPORT
srs5694e7b4ff92009-08-18 13:16:10 -040011
srs5694221e0872009-08-29 15:00:31 -040012#if defined (__FreeBSD__) || defined (__APPLE__)
srs5694e7b4ff92009-08-18 13:16:10 -040013// Darwin (Mac OS) only: disk IOCTLs are different, and there is no lseek64
srs5694da79b852009-08-18 13:29:54 -040014// 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).
srs5694e7b4ff92009-08-18 13:16:10 -040017#include <sys/disk.h>
18#define lseek64 lseek
19#else
20
21// Linux only....
22#include <linux/fs.h>
23#endif
24
srs5694978041c2009-09-21 20:51:47 -040025#ifdef __FreeBSD__
26#define fstat64 fstat
27#define stat64 stat
28#endif
srs5694e7b4ff92009-08-18 13:16:10 -040029
30// Set this as a default
31#define SECTOR_SIZE UINT32_C(512)
32
srs5694221e0872009-08-29 15:00:31 -040033// Signatures for Apple (APM) disks, multiplied by 0x100000000
34#define APM_SIGNATURE1 UINT64_C(0x00004D5000000000)
35#define APM_SIGNATURE2 UINT64_C(0x0000535400000000)
36
37/**************************
38 * Some GPT constants.... *
39 **************************/
40
41#define GPT_SIGNATURE UINT64_C(0x5452415020494645)
42
43// Number and size of GPT entries...
44#define NUM_GPT_ENTRIES 128
45#define GPT_SIZE 128
srs5694978041c2009-09-21 20:51:47 -040046#define HEADER_SIZE UINT32_C(92)
srs5694221e0872009-08-29 15:00:31 -040047#define GPT_RESERVED 420
48#define NAME_SIZE 72
49
srs5694e7b4ff92009-08-18 13:16:10 -040050using namespace std;
51
52// a GUID
53struct GUIDData {
54 uint64_t data1;
55 uint64_t data2;
56}; // struct GUIDData
57
58int GetNumber(int low, int high, int def, const char prompt[]);
59char GetYN(void);
srs5694e4ac11e2009-08-31 10:13:04 -040060uint64_t GetSectorNum(uint64_t low, uint64_t high, uint64_t def, char prompt[]);
srs5694e7b4ff92009-08-18 13:16:10 -040061char* BytesToSI(uint64_t size, char theValue[]);
62int GetBlockSize(int fd);
63char* GUIDToStr(struct GUIDData theGUID, char* theString);
64GUIDData GetGUID(void);
srs56942a9f5da2009-08-26 00:48:01 -040065int IsLittleEndian(void); // Returns 1 if CPU is little-endian, 0 if it's big-endian
srs5694221e0872009-08-29 15:00:31 -040066void ReverseBytes(void* theValue, int numBytes); // Reverses byte-order of theValue
srs5694e7b4ff92009-08-18 13:16:10 -040067uint64_t PowerOf2(int value);
srs5694e4ac11e2009-08-31 10:13:04 -040068int OpenForWrite(char* deviceFilename);
srs5694e35eb1b2009-09-14 00:29:34 -040069void DiskSync(int fd); // resync disk caches to use new partitions
srs5694e7b4ff92009-08-18 13:16:10 -040070
71uint64_t disksize(int fd, int* err);
72
73#endif