support.h revision 221e08768de7fe42ba533ca22baf671420569c07
1221e08768de7fe42ba533ca22baf671420569c07srs/* This program is copyright (c) 2009 by Roderick W. Smith. It is distributed
2221e08768de7fe42ba533ca22baf671420569c07srs  under the terms of the GNU GPL version 2, as detailed in the COPYING file. */
3221e08768de7fe42ba533ca22baf671420569c07srs
4e7b4ff9317fc4e551cf974684eaa88697de5a28srs#include <stdint.h>
5e7b4ff9317fc4e551cf974684eaa88697de5a28srs#include <unistd.h>
6e7b4ff9317fc4e551cf974684eaa88697de5a28srs#include <stdlib.h>
7e7b4ff9317fc4e551cf974684eaa88697de5a28srs
8221e08768de7fe42ba533ca22baf671420569c07srs#if defined (__FreeBSD__) || defined (__APPLE__)
9e7b4ff9317fc4e551cf974684eaa88697de5a28srs// Darwin (Mac OS) only: disk IOCTLs are different, and there is no lseek64
10da79b858c9b64823f5c039dc13a3e05e3f2df69asrs// This used to use __DARWIN_UNIX03 rather than __APPLE__, but __APPLE__
11da79b858c9b64823f5c039dc13a3e05e3f2df69asrs// is more general. If the code fails to work on older versions of OS X/
12da79b858c9b64823f5c039dc13a3e05e3f2df69asrs// Darwin, this may need to be changed back (and in various .cc files).
13e7b4ff9317fc4e551cf974684eaa88697de5a28srs#include <sys/disk.h>
14e7b4ff9317fc4e551cf974684eaa88697de5a28srs#define lseek64 lseek
15e7b4ff9317fc4e551cf974684eaa88697de5a28srs#else
16e7b4ff9317fc4e551cf974684eaa88697de5a28srs
17e7b4ff9317fc4e551cf974684eaa88697de5a28srs// Linux only....
18e7b4ff9317fc4e551cf974684eaa88697de5a28srs#include <linux/fs.h>
19e7b4ff9317fc4e551cf974684eaa88697de5a28srs#endif
20e7b4ff9317fc4e551cf974684eaa88697de5a28srs
21e7b4ff9317fc4e551cf974684eaa88697de5a28srs#include <string>
22e7b4ff9317fc4e551cf974684eaa88697de5a28srs
23e7b4ff9317fc4e551cf974684eaa88697de5a28srs#ifndef __GPTSUPPORT
24e7b4ff9317fc4e551cf974684eaa88697de5a28srs#define __GPTSUPPORT
25e7b4ff9317fc4e551cf974684eaa88697de5a28srs
26e7b4ff9317fc4e551cf974684eaa88697de5a28srs// Set this as a default
27e7b4ff9317fc4e551cf974684eaa88697de5a28srs#define SECTOR_SIZE UINT32_C(512)
28e7b4ff9317fc4e551cf974684eaa88697de5a28srs
29221e08768de7fe42ba533ca22baf671420569c07srs// Signatures for Apple (APM) disks, multiplied by 0x100000000
30221e08768de7fe42ba533ca22baf671420569c07srs#define APM_SIGNATURE1 UINT64_C(0x00004D5000000000)
31221e08768de7fe42ba533ca22baf671420569c07srs#define APM_SIGNATURE2 UINT64_C(0x0000535400000000)
32221e08768de7fe42ba533ca22baf671420569c07srs
33221e08768de7fe42ba533ca22baf671420569c07srs/**************************
34221e08768de7fe42ba533ca22baf671420569c07srs * Some GPT constants.... *
35221e08768de7fe42ba533ca22baf671420569c07srs **************************/
36221e08768de7fe42ba533ca22baf671420569c07srs
37221e08768de7fe42ba533ca22baf671420569c07srs#define GPT_SIGNATURE UINT64_C(0x5452415020494645)
38221e08768de7fe42ba533ca22baf671420569c07srs
39221e08768de7fe42ba533ca22baf671420569c07srs// Number and size of GPT entries...
40221e08768de7fe42ba533ca22baf671420569c07srs#define NUM_GPT_ENTRIES 128
41221e08768de7fe42ba533ca22baf671420569c07srs#define GPT_SIZE 128
42221e08768de7fe42ba533ca22baf671420569c07srs#define HEADER_SIZE 92
43221e08768de7fe42ba533ca22baf671420569c07srs#define GPT_RESERVED 420
44221e08768de7fe42ba533ca22baf671420569c07srs#define NAME_SIZE 72
45221e08768de7fe42ba533ca22baf671420569c07srs
46e7b4ff9317fc4e551cf974684eaa88697de5a28srsusing namespace std;
47e7b4ff9317fc4e551cf974684eaa88697de5a28srs
48e7b4ff9317fc4e551cf974684eaa88697de5a28srs// a GUID
49e7b4ff9317fc4e551cf974684eaa88697de5a28srsstruct GUIDData {
50e7b4ff9317fc4e551cf974684eaa88697de5a28srs   uint64_t data1;
51e7b4ff9317fc4e551cf974684eaa88697de5a28srs   uint64_t data2;
52e7b4ff9317fc4e551cf974684eaa88697de5a28srs}; // struct GUIDData
53e7b4ff9317fc4e551cf974684eaa88697de5a28srs
54e7b4ff9317fc4e551cf974684eaa88697de5a28srsint GetNumber(int low, int high, int def, const char prompt[]);
55e7b4ff9317fc4e551cf974684eaa88697de5a28srschar GetYN(void);
56e7b4ff9317fc4e551cf974684eaa88697de5a28srsuint64_t GetLastSector(uint64_t low, uint64_t high, char prompt[]);
57e7b4ff9317fc4e551cf974684eaa88697de5a28srschar* BytesToSI(uint64_t size, char theValue[]);
58e7b4ff9317fc4e551cf974684eaa88697de5a28srsint GetBlockSize(int fd);
59e7b4ff9317fc4e551cf974684eaa88697de5a28srschar* GUIDToStr(struct GUIDData theGUID, char* theString);
60e7b4ff9317fc4e551cf974684eaa88697de5a28srsGUIDData GetGUID(void);
612a9f5da3c3c4ccccd291462bda9d2aefcd485ff8srsint IsLittleEndian(void); // Returns 1 if CPU is little-endian, 0 if it's big-endian
62221e08768de7fe42ba533ca22baf671420569c07srsvoid ReverseBytes(void* theValue, int numBytes); // Reverses byte-order of theValue
63e7b4ff9317fc4e551cf974684eaa88697de5a28srsuint64_t PowerOf2(int value);
64e7b4ff9317fc4e551cf974684eaa88697de5a28srs
65e7b4ff9317fc4e551cf974684eaa88697de5a28srsuint64_t disksize(int fd, int* err);
66e7b4ff9317fc4e551cf974684eaa88697de5a28srs
67e7b4ff9317fc4e551cf974684eaa88697de5a28srs#endif
68