blob: 9520c8dc427cb49613e1a8298128ec7634cd26ed [file] [log] [blame]
Ethan Yonker66a19492015-12-10 10:19:45 -06001/*
2 gpt.[ch]
3
4 Copyright (C) 2000-2001 Dell Computer Corporation <Matt_Domsch@dell.com>
5
6 EFI GUID Partition Table handling
7 Per Intel EFI Specification v1.02
8 http://developer.intel.com/technology/efi/efi.htm
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23*/
24
25// For TWRP purposes, we'll be opting for version 3 of the GPL
26
27#ifndef _GPT_H
28#define _GPT_H
29
30
31#include <inttypes.h>
32//#include "efi.h"
33
34typedef struct {
35 uint8_t b[16];
36} efi_guid_t;
37typedef uint16_t efi_char16_t; /* UNICODE character */
38
39#define EFI_PMBR_OSTYPE_EFI 0xEF
40#define EFI_PMBR_OSTYPE_EFI_GPT 0xEE
41#define MSDOS_MBR_SIGNATURE 0xaa55
42#define GPT_BLOCK_SIZE 512
43
44static const char* TWGptAndroidExpand = "193d1ea4b3ca11e4b07510604b889dcf";
45
46#define GPT_HEADER_SIGNATURE ((uint64_t)(0x5452415020494645LL))
47#define GPT_HEADER_REVISION_V1_02 0x00010200
48#define GPT_HEADER_REVISION_V1_00 0x00010000
49#define GPT_HEADER_REVISION_V0_99 0x00009900
50#define GPT_PRIMARY_PARTITION_TABLE_LBA 1
51
52#define PARTITION_SYSTEM_GUID \
53 EFI_GUID( 0xC12A7328, 0xF81F, 0x11d2, \
54 0xBA, 0x4B, 0x00, 0xA0, 0xC9, 0x3E, 0xC9, 0x3B)
55#define LEGACY_MBR_PARTITION_GUID \
56 EFI_GUID( 0x024DEE41, 0x33E7, 0x11d3, \
57 0x9D, 0x69, 0x00, 0x08, 0xC7, 0x81, 0xF3, 0x9F)
58#define PARTITION_MSFT_RESERVED_GUID \
59 EFI_GUID( 0xE3C9E316, 0x0B5C, 0x4DB8, \
60 0x81, 0x7D, 0xF9, 0x2D, 0xF0, 0x02, 0x15, 0xAE)
61#define PARTITION_BASIC_DATA_GUID \
62 EFI_GUID( 0xEBD0A0A2, 0xB9E5, 0x4433, \
63 0x87, 0xC0, 0x68, 0xB6, 0xB7, 0x26, 0x99, 0xC7)
64#define PARTITION_LINUX_RAID_GUID \
65 EFI_GUID( 0xa19d880f, 0x05fc, 0x4d3b, \
66 0xa0, 0x06, 0x74, 0x3f, 0x0f, 0x84, 0x91, 0x1e)
67#define PARTITION_LINUX_SWAP_GUID \
68 EFI_GUID( 0x0657fd6d, 0xa4ab, 0x43c4, \
69 0x84, 0xe5, 0x09, 0x33, 0xc8, 0x4b, 0x4f, 0x4f)
70#define PARTITION_LINUX_LVM_GUID \
71 EFI_GUID( 0xe6d6d379, 0xf507, 0x44c2, \
72 0xa2, 0x3c, 0x23, 0x8f, 0x2a, 0x3d, 0xf9, 0x28)
73
74typedef struct _gpt_header {
75 uint64_t signature;
76 uint32_t revision;
77 uint32_t header_size;
78 uint32_t header_crc32;
79 uint32_t reserved1;
80 uint64_t my_lba;
81 uint64_t alternate_lba;
82 uint64_t first_usable_lba;
83 uint64_t last_usable_lba;
84 efi_guid_t disk_guid;
85 uint64_t partition_entry_lba;
86 uint32_t num_partition_entries;
87 uint32_t sizeof_partition_entry;
88 uint32_t partition_entry_array_crc32;
89 uint8_t reserved2[GPT_BLOCK_SIZE - 92];
90} __attribute__ ((packed)) gpt_header;
91
92typedef struct _gpt_entry_attributes {
93 uint64_t required_to_function:1;
94 uint64_t reserved:47;
95 uint64_t type_guid_specific:16;
96} __attribute__ ((packed)) gpt_entry_attributes;
97
98typedef struct _gpt_entry {
99 efi_guid_t partition_type_guid;
100 efi_guid_t unique_partition_guid;
101 uint64_t starting_lba;
102 uint64_t ending_lba;
103 gpt_entry_attributes attributes;
104 efi_char16_t partition_name[72 / sizeof(efi_char16_t)];
105} __attribute__ ((packed)) gpt_entry;
106
107
108/*
109 These values are only defaults. The actual on-disk structures
110 may define different sizes, so use those unless creating a new GPT disk!
111*/
112
113#define GPT_DEFAULT_RESERVED_PARTITION_ENTRY_ARRAY_SIZE 16384
114/*
115 Number of actual partition entries should be calculated
116 as:
117*/
118#define GPT_DEFAULT_RESERVED_PARTITION_ENTRIES \
119 (GPT_DEFAULT_RESERVED_PARTITION_ENTRY_ARRAY_SIZE / \
120 sizeof(gpt_entry))
121
122
123typedef struct _partition_record {
124 uint8_t boot_indicator; /* Not used by EFI firmware. Set to 0x80 to indicate that this
125 is the bootable legacy partition. */
126 uint8_t start_head; /* Start of partition in CHS address, not used by EFI firmware. */
127 uint8_t start_sector; /* Start of partition in CHS address, not used by EFI firmware. */
128 uint8_t start_track; /* Start of partition in CHS address, not used by EFI firmware. */
129 uint8_t os_type; /* OS type. A value of 0xEF defines an EFI system partition.
130 Other values are reserved for legacy operating systems, and
131 allocated independently of the EFI specification. */
132 uint8_t end_head; /* End of partition in CHS address, not used by EFI firmware. */
133 uint8_t end_sector; /* End of partition in CHS address, not used by EFI firmware. */
134 uint8_t end_track; /* End of partition in CHS address, not used by EFI firmware. */
135 uint32_t starting_lba; /* Starting LBA address of the partition on the disk. Used by
136 EFI firmware to define the start of the partition. */
137 uint32_t size_in_lba; /* Size of partition in LBA. Used by EFI firmware to determine
138 the size of the partition. */
139} __attribute__ ((packed)) partition_record;
140
141
142/* Protected Master Boot Record & Legacy MBR share same structure */
143/* Needs to be packed because the u16s force misalignment. */
144
145typedef struct _legacy_mbr {
146 uint8_t bootcode[440];
147 uint32_t unique_mbr_signature;
148 uint16_t unknown;
149 partition_record partition[4];
150 uint16_t signature;
151} __attribute__ ((packed)) legacy_mbr;
152
153
154
155
156#define EFI_GPT_PRIMARY_PARTITION_TABLE_LBA 1
157
158/* Functions */
159int gpt_disk_get_partition_info (int fd, uint32_t num,
160 char *type, char *part);
161
162
163#endif
164
165/*
166 * Overrides for Emacs so that we follow Linus's tabbing style.
167 * Emacs will notice this stuff at the end of the file and automatically
168 * adjust the settings for this buffer only. This must remain at the end
169 * of the file.
170 * ---------------------------------------------------------------------------
171 * Local variables:
172 * c-indent-level: 4
173 * c-brace-imaginary-offset: 0
174 * c-brace-offset: -4
175 * c-argdecl-indent: 4
176 * c-label-offset: -4
177 * c-continued-statement-offset: 4
178 * c-continued-brace-offset: 0
179 * indent-tabs-mode: nil
180 * tab-width: 8
181 * End:
182 */