#ifndef GPT_H
#define GPT_H

#include <stdint.h>

struct uuid {
  uint32_t time_low;
  uint16_t time_mid;
  uint16_t time_hi_and_version;
  uint8_t  clock_seq_hi_and_reserved;
  uint8_t  clock_seq_low;
  uint8_t  node[6];
};

#define GPT_HDR_SIG             "EFI PART"
#define GPT_HDR_REVISION        0x00010000

struct gpt_hdr {
  uint8_t  hdr_sig[8];    // 0x0
  uint32_t hdr_revision;  // 0x8
  uint32_t size;          // 0xC
  uint32_t crc_self;      // 0x10
  uint32_t reserved;      // 0x14
  uint64_t lba_self;      // 0x18
  uint64_t lba_alt;       // 0x20
  uint64_t lba_start;     // 0x28
  uint64_t lba_end;       // 0x30
  struct uuid uuid;       // 0x40
  uint64_t lba_table;     // 0x48
  uint32_t entries;       // 0x50
  uint32_t entsz;         // 0x54
  uint32_t crc_table;     // 0x58
  uint32_t padding;       // 0x5C
};  // 0x60

struct gpt_ent {
  struct uuid type;   // 0x0
  struct uuid uuid;   // 0x10
  uint64_t lba_start; // 0x20
  uint64_t lba_end;   // 0x28
  uint64_t attr;      // 0x30
  uint16_t name[36];  // 0x38
};  // 0x80



#define GPT_ENT_TYPE_UNUSED             \
        {0x00000000,0x0000,0x0000,0x00,0x00,{0x00,0x00,0x00,0x00,0x00,0x00}}
#define GPT_ENT_TYPE_EFI                \
        {0xc12a7328,0xf81f,0x11d2,0xba,0x4b,{0x00,0xa0,0xc9,0x3e,0xc9,0x3b}}
#define GPT_ENT_TYPE_MBR                \
        {0x024dee41,0x33e7,0x11d3,0x9d,0x69,{0x00,0x08,0xc7,0x81,0xf3,0x9f}}
#define GPT_ENT_TYPE_FREEBSD            \
        {0x516e7cb4,0x6ecf,0x11d6,0x8f,0xf8,{0x00,0x02,0x2d,0x09,0x71,0x2b}}
#define GPT_ENT_TYPE_FREEBSD_BOOT       \
        {0x83bd6b9d,0x7f41,0x11dc,0xbe,0x0b,{0x00,0x15,0x60,0xb8,0x4f,0x0f}}
#define GPT_ENT_TYPE_FREEBSD_NANDFS     \
        {0x74ba7dd9,0xa689,0x11e1,0xbd,0x04,{0x00,0xe0,0x81,0x28,0x6a,0xcf}}
#define GPT_ENT_TYPE_FREEBSD_SWAP       \
        {0x516e7cb5,0x6ecf,0x11d6,0x8f,0xf8,{0x00,0x02,0x2d,0x09,0x71,0x2b}}
#define GPT_ENT_TYPE_FREEBSD_UFS        \
        {0x516e7cb6,0x6ecf,0x11d6,0x8f,0xf8,{0x00,0x02,0x2d,0x09,0x71,0x2b}}
#define GPT_ENT_TYPE_FREEBSD_VINUM      \
        {0x516e7cb8,0x6ecf,0x11d6,0x8f,0xf8,{0x00,0x02,0x2d,0x09,0x71,0x2b}}
#define GPT_ENT_TYPE_FREEBSD_ZFS        \
        {0x516e7cba,0x6ecf,0x11d6,0x8f,0xf8,{0x00,0x02,0x2d,0x09,0x71,0x2b}}

#define GPT_ENT_TYPE_ORBIS_PREINST      \
        {0x17800F17,0xB9E1,0x425D,0xB9,0x37,{0x01,0x19,0xA0,0x81,0x31,0x72}}
#define GPT_ENT_TYPE_ORBIS_PREINST2     \
        {0xccb52e94,0xebef,0x48c4,0xa1,0x95,{0x9e,0x2d,0xa5,0xb0,0x29,0x2c}}
#define GPT_ENT_TYPE_ORBIS_EAP_KERN     \
        {0x145268bf,0x63ad,0x47c1,0x93,0x78,{0x9a,0xac,0xd9,0xbe,0xed,0x7c}}
#define GPT_ENT_TYPE_ORBIS_EAP_VSH      \
        {0x6e0c5310,0x8445,0x4066,0xb5,0x71,{0x9b,0x65,0xfd,0xb7,0x59,0x35}}
#define GPT_ENT_TYPE_ORBIS_UNKNOWN      \
        {0xeabbf00b,0xc299,0x4488,0x9d,0xe9,{0xb2,0x83,0x9b,0xce,0x75,0x46}}
#define GPT_ENT_TYPE_ORBIS_SYSTEM       \
        {0x757A614B,0x6179,0x5361,0x6B,0x61,{0x6B,0x69,0x68,0x61,0x72,0x61}}
#define GPT_ENT_TYPE_ORBIS_SYSTEM_EX    \
        {0xdc85025f,0xa694,0x4109,0xbe,0x44,{0xfa,0x0c,0x06,0x3e,0x8b,0x81}}
#define GPT_ENT_TYPE_ORBIS_SWAP         \
        {0x76a9a5b4,0x44b0,0x472a,0xbd,0xe3,{0x31,0x07,0x47,0x2a,0xde,0xe2}}
#define GPT_ENT_TYPE_ORBIS_APP_TMP      \
        {0x80dd49e3,0xa985,0x4887,0x81,0xde,{0x1d,0xac,0xa4,0x7a,0xed,0x90}}
#define GPT_ENT_TYPE_ORBIS_SYSTEM_DATA  \
        {0xa71ff62d,0x1421,0x4dd9,0x93,0x5d,{0x25,0xda,0xbd,0x81,0xbe,0xc5}}
#define GPT_ENT_TYPE_ORBIS_UPDATE       \
        {0xfdb5ede1,0x73c3,0x4c43,0x8c,0x5b,{0x2d,0x3d,0xcf,0xcd,0xdf,0xf8}}
#define GPT_ENT_TYPE_ORBIS_USER         \
        {0xc638477a,0xe002,0x4b57,0xa4,0x54,{0xa2,0x7f,0xb6,0x3a,0x33,0xa8}}
#define GPT_ENT_TYPE_ORBIS_EAP_USER     \
        {0x21e4dfb4,0x0040,0x4934,0xa0,0x37,{0xea,0x9d,0xc0,0x58,0xee,0xa6}}
#define GPT_ENT_TYPE_ORBIS_APP_RESERVED \
        {0x3ef7290a,0xde81,0x4887,0xa1,0x1f,{0x46,0xfb,0xa7,0x65,0xc7,0x1c}}

/*
 * The following are unused but documented here to avoid reuse.
 *
 * GPT_ENT_TYPE_FREEBSD_UFS2    \
 *      {0x516e7cb7,0x6ecf,0x11d6,0x8f,0xf8,{0x00,0x02,0x2d,0x09,0x71,0x2b}}
 */

/*
 * Foreign partition types that we're likely to encounter. Note that Linux
 * apparently choose to share data partitions with MS. I don't what the
 * advantage might be. I can see how sharing swap partitions is advantageous
 * though.
 */
#define GPT_ENT_TYPE_MS_RESERVED        \
        {0xe3c9e316,0x0b5c,0x4db8,0x81,0x7d,{0xf9,0x2d,0xf0,0x02,0x15,0xae}}
#define GPT_ENT_TYPE_MS_BASIC_DATA      \
        {0xebd0a0a2,0xb9e5,0x4433,0x87,0xc0,{0x68,0xb6,0xb7,0x26,0x99,0xc7}}
#define GPT_ENT_TYPE_MS_LDM_METADATA    \
        {0x5808c8aa,0x7e8f,0x42e0,0x85,0xd2,{0xe1,0xe9,0x04,0x34,0xcf,0xb3}}
#define GPT_ENT_TYPE_MS_LDM_DATA        \
        {0xaf9b60a0,0x1431,0x4f62,0xbc,0x68,{0x33,0x11,0x71,0x4a,0x69,0xad}}

#define GPT_ENT_TYPE_LINUX_DATA         GPT_ENT_TYPE_MS_BASIC_DATA
#define GPT_ENT_TYPE_LINUX_RAID         \
        {0xa19d880f,0x05fc,0x4d3b,0xa0,0x06,{0x74,0x3f,0x0f,0x84,0x91,0x1e}}
#define GPT_ENT_TYPE_LINUX_SWAP         \
        {0x0657fd6d,0xa4ab,0x43c4,0x84,0xe5,{0x09,0x33,0xc8,0x4b,0x4f,0x4f}}
#define GPT_ENT_TYPE_LINUX_LVM          \
        {0xe6d6d379,0xf507,0x44c2,0xa2,0x3c,{0x23,0x8f,0x2a,0x3d,0xf9,0x28}}

#define GPT_ENT_TYPE_VMFS               \
        {0xaa31e02a,0x400f,0x11db,0x95,0x90,{0x00,0x0c,0x29,0x11,0xd1,0xb8}}
#define GPT_ENT_TYPE_VMKDIAG            \
        {0x9d275380,0x40ad,0x11db,0xbf,0x97,{0x00,0x0c,0x29,0x11,0xd1,0xb8}}
#define GPT_ENT_TYPE_VMRESERVED         \
        {0x9198effc,0x31c0,0x11db,0x8f,0x78,{0x00,0x0c,0x29,0x11,0xd1,0xb8}}
#define GPT_ENT_TYPE_VMVSANHDR          \
        {0x381cfccc,0x7288,0x11e0,0x92,0xee,{0x00,0x0c,0x29,0x11,0xd0,0xb2}}

#define GPT_ENT_TYPE_APPLE_BOOT         \
        {0x426F6F74,0x0000,0x11aa,0xaa,0x11,{0x00,0x30,0x65,0x43,0xec,0xac}}
#define GPT_ENT_TYPE_APPLE_HFS          \
        {0x48465300,0x0000,0x11aa,0xaa,0x11,{0x00,0x30,0x65,0x43,0xec,0xac}}
#define GPT_ENT_TYPE_APPLE_UFS          \
        {0x55465300,0x0000,0x11aa,0xaa,0x11,{0x00,0x30,0x65,0x43,0xec,0xac}}
#define GPT_ENT_TYPE_APPLE_ZFS          \
        {0x6a898cc3,0x1dd2,0x11b2,0x99,0xa6,{0x08,0x00,0x20,0x73,0x66,0x31}}
#define GPT_ENT_TYPE_APPLE_RAID         \
        {0x52414944,0x0000,0x11aa,0xaa,0x22,{0x00,0x30,0x65,0x43,0xec,0xac}}
#define GPT_ENT_TYPE_APPLE_RAID_OFFLINE \
        {0x52414944,0x5f4f,0x11aa,0xaa,0x22,{0x00,0x30,0x65,0x43,0xec,0xac}}
#define GPT_ENT_TYPE_APPLE_LABEL        \
        {0x4C616265,0x6c00,0x11aa,0xaa,0x11,{0x00,0x30,0x65,0x43,0xec,0xac}}
#define GPT_ENT_TYPE_APPLE_TV_RECOVERY  \
        {0x5265636f,0x7665,0x11AA,0xaa,0x11,{0x00,0x30,0x65,0x43,0xec,0xac}}

#define GPT_ENT_TYPE_NETBSD_FFS         \
        {0x49f48d5a,0xb10e,0x11dc,0xb9,0x9b,{0x00,0x19,0xd1,0x87,0x96,0x48}}
#define GPT_ENT_TYPE_NETBSD_LFS         \
        {0x49f48d82,0xb10e,0x11dc,0xb9,0x9b,{0x00,0x19,0xd1,0x87,0x96,0x48}}
#define GPT_ENT_TYPE_NETBSD_SWAP        \
        {0x49f48d32,0xb10e,0x11dc,0xB9,0x9B,{0x00,0x19,0xd1,0x87,0x96,0x48}}
#define GPT_ENT_TYPE_NETBSD_RAID        \
        {0x49f48daa,0xb10e,0x11dc,0xb9,0x9b,{0x00,0x19,0xd1,0x87,0x96,0x48}}
#define GPT_ENT_TYPE_NETBSD_CCD         \
        {0x2db519c4,0xb10f,0x11dc,0xb9,0x9b,{0x00,0x19,0xd1,0x87,0x96,0x48}}
#define GPT_ENT_TYPE_NETBSD_CGD         \
        {0x2db519ec,0xb10f,0x11dc,0xb9,0x9b,{0x00,0x19,0xd1,0x87,0x96,0x48}}

/*
 * Boot partition used by GRUB 2.
 */
#define GPT_ENT_TYPE_BIOS_BOOT          \
        {0x21686148,0x6449,0x6e6f,0x74,0x4e,{0x65,0x65,0x64,0x45,0x46,0x49}}

#endif /* GPT_H */
