1 | /*
|
---|
2 | * Copyright (c) 2009 Jiri Svoboda, 2011 Dominik Taborsky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libgpt
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #ifndef __GPT_H__
|
---|
36 | #define __GPT_H__
|
---|
37 |
|
---|
38 | #define NAME "libgpt"
|
---|
39 |
|
---|
40 | #include <sys/types.h>
|
---|
41 |
|
---|
42 | /** Block address of GPT header. */
|
---|
43 | #define GPT_HDR_BA 1
|
---|
44 | /** Block size of GPT header. */
|
---|
45 | #define GPT_HDR_BS 1
|
---|
46 | /** Minimum number of GPT partition entries */
|
---|
47 | #define GPT_MIN_PART_NUM 128
|
---|
48 | /** Basic number of GPT partition entries */
|
---|
49 | #define GPT_BASE_PART_NUM (GPT_MIN_PART_NUM)
|
---|
50 |
|
---|
51 | /** GPT header signature ("EFI PART" in ASCII) */
|
---|
52 | const uint8_t efi_signature[8] = {
|
---|
53 | 0x45, 0x46, 0x49, 0x20, 0x50, 0x41, 0x52, 0x54
|
---|
54 | };
|
---|
55 |
|
---|
56 | /** GPT header
|
---|
57 | * - all in little endian.
|
---|
58 | */
|
---|
59 | typedef struct {
|
---|
60 | uint8_t efi_signature[8];
|
---|
61 | uint32_t revision;
|
---|
62 | uint32_t header_size;
|
---|
63 | uint32_t header_crc32;
|
---|
64 | uint32_t reserved;
|
---|
65 | uint64_t my_lba;
|
---|
66 | uint64_t alternate_lba;
|
---|
67 | uint64_t first_usable_lba;
|
---|
68 | uint64_t last_usable_lba;
|
---|
69 | uint8_t disk_guid[16];
|
---|
70 | uint64_t entry_lba;
|
---|
71 | uint32_t num_entries;
|
---|
72 | uint32_t entry_size;
|
---|
73 | uint32_t pe_array_crc32;
|
---|
74 | } __attribute__((packed)) gpt_header_t;
|
---|
75 |
|
---|
76 | typedef struct {
|
---|
77 | /** Raw header. Has more bytes alloced than sizeof(gpt_header_t)!
|
---|
78 | * See gpt_read_gpt_header() to know why. */
|
---|
79 | gpt_header_t * raw_data;
|
---|
80 | /** Device where the data are from */
|
---|
81 | service_id_t device;
|
---|
82 | /** Linked list of partitions (initially NULL) */
|
---|
83 | } gpt_t;
|
---|
84 |
|
---|
85 | /** GPT partition entry */
|
---|
86 | typedef struct {
|
---|
87 | uint8_t part_type[16];
|
---|
88 | uint8_t part_id[16];
|
---|
89 | uint64_t start_lba;
|
---|
90 | uint64_t end_lba;
|
---|
91 | uint64_t attributes;
|
---|
92 | uint8_t part_name[72];
|
---|
93 | } __attribute__((packed)) gpt_entry_t;
|
---|
94 |
|
---|
95 | typedef struct g_part {
|
---|
96 | /** Partition entry is in use */
|
---|
97 | bool present;
|
---|
98 | /** Address of first block */
|
---|
99 | aoff64_t start_addr;
|
---|
100 | /** Number of blocks */
|
---|
101 | aoff64_t length;
|
---|
102 | /** Raw data access */
|
---|
103 | gpt_entry_t raw_data; //TODO: a pointer or just a member?
|
---|
104 | }g_part_t;
|
---|
105 |
|
---|
106 | typedef struct gpt_parts {
|
---|
107 | /** Number of entries */
|
---|
108 | unsigned int num_ent;
|
---|
109 | /** Size of the array */
|
---|
110 | unsigned int arr_size;
|
---|
111 | /** Resizable partition array */
|
---|
112 | gpt_entry_t * part_array;
|
---|
113 | } gpt_parts_t;
|
---|
114 |
|
---|
115 | struct partition_type {
|
---|
116 | const char * desc;
|
---|
117 | const char * guid;
|
---|
118 | };
|
---|
119 |
|
---|
120 | struct partition_type gpt_ptypes[] = {
|
---|
121 | { "Unused entry", "00000000-0000-0000-0000-000000000000" },
|
---|
122 | { "MBR partition scheme", "024DEE41-33E7-11D3-9D69-0008C781F39F" },
|
---|
123 | { "EFI System", "C12A7328-F81F-11D2-BA4B-00A0C93EC93B" },
|
---|
124 | { "BIOS Boot", "21686148-6449-6E6F-744E-656564454649" },
|
---|
125 | { "Windows Reserved", "E3C9E316-0B5C-4DB8-817D-F92DF00215AE" },
|
---|
126 | { "Windows Basic data", "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7" },
|
---|
127 | { "Windows LDM metadata", "5808C8AA-7E8F-42E0-85D2-E1E90434CFB3" },
|
---|
128 | { "Windows LDM data", "AF9B60A0-1431-4F62-BC68-3311714A69AD" },
|
---|
129 | { "Windows Recovery Environment", "DE94BBA4-06D1-4D40-A16A-BFD50179D6AC" },
|
---|
130 | { "Windows IBM GPFS", "37AFFC90-EF7D-4E96-91C3-2D7AE055B174" },
|
---|
131 | { "Windows Cluster metadata", "DB97DBA9-0840-4BAE-97F0-FFB9A327C7E1" },
|
---|
132 | { "HP-UX Data", "75894C1E-3AEB-11D3-B7C1-7B03A0000000" },
|
---|
133 | { "HP-UX Service", "E2A1E728-32E3-11D6-A682-7B03A0000000" },
|
---|
134 | { "Linux filesystem data", "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7" },
|
---|
135 | { "Linux filesystem data", "0FC63DAF-8483-4772-8E79-3D69D8477DE4" },
|
---|
136 | { "Linux RAID", "A19D880F-05FC-4D3B-A006-743F0F84911E" },
|
---|
137 | { "Linux Swap", "0657FD6D-A4AB-43C4-84E5-0933C84B4F4F" },
|
---|
138 | { "Linux LVM", "E6D6D379-F507-44C2-A23C-238F2A3DF928" },
|
---|
139 | { "Linux Reserved", "8DA63339-0007-60C0-C436-083AC8230908" },
|
---|
140 | { "FreeBSD Boot", "83BD6B9D-7F41-11DC-BE0B-001560B84F0F" },
|
---|
141 | { "FreeBSD Data", "516E7CB4-6ECF-11D6-8FF8-00022D09712B" },
|
---|
142 | { "FreeBSD Swap", "516E7CB5-6ECF-11D6-8FF8-00022D09712B" },
|
---|
143 | { "FreeBSD UFS", "516E7CB6-6ECF-11D6-8FF8-00022D09712B" },
|
---|
144 | { "FreeBSD Vinum VM", "516E7CB8-6ECF-11D6-8FF8-00022D09712B" },
|
---|
145 | { "FreeBSD ZFS", "516E7CBA-6ECF-11D6-8FF8-00022D09712B" },
|
---|
146 | { "Mac OS X HFS+", "48465300-0000-11AA-AA11-00306543ECAC" },
|
---|
147 | { "Mac OS X UFS", "55465300-0000-11AA-AA11-00306543ECAC" },
|
---|
148 | { "Mac OS X ZFS", "6A898CC3-1DD2-11B2-99A6-080020736631" },
|
---|
149 | { "Mac OS X RAID", "52414944-0000-11AA-AA11-00306543ECAC" },
|
---|
150 | { "Mac OS X RAID, offline", "52414944-5F4F-11AA-AA11-00306543ECAC" },
|
---|
151 | { "Mac OS X Boot", "426F6F74-0000-11AA-AA11-00306543ECAC" },
|
---|
152 | { "Mac OS X Label", "4C616265-6C00-11AA-AA11-00306543ECAC" },
|
---|
153 | { "Mac OS X TV Recovery", "5265636F-7665-11AA-AA11-00306543ECAC" },
|
---|
154 | { "Mac OS X Core Storage", "53746F72-6167-11AA-AA11-00306543ECAC" },
|
---|
155 | { "Solaris Boot", "6A82CB45-1DD2-11B2-99A6-080020736631" },
|
---|
156 | { "Solaris Root", "6A85CF4D-1DD2-11B2-99A6-080020736631" },
|
---|
157 | { "Solaris Swap", "6A87C46F-1DD2-11B2-99A6-080020736631" },
|
---|
158 | { "Solaris Backup", "6A8B642B-1DD2-11B2-99A6-080020736631" },
|
---|
159 | { "Solaris /usr", "6A898CC3-1DD2-11B2-99A6-080020736631" },
|
---|
160 | { "Solaris /var", "6A8EF2E9-1DD2-11B2-99A6-080020736631" },
|
---|
161 | { "Solaris /home", "6A90BA39-1DD2-11B2-99A6-080020736631" },
|
---|
162 | { "Solaris Alternate sector", "6A9283A5-1DD2-11B2-99A6-080020736631" },
|
---|
163 | { "Solaris Reserved", "6A945A3B-1DD2-11B2-99A6-080020736631" },
|
---|
164 | { "Solaris Reserved", "6A9630D1-1DD2-11B2-99A6-080020736631" },
|
---|
165 | { "Solaris Reserved", "6A980767-1DD2-11B2-99A6-080020736631" },
|
---|
166 | { "Solaris Reserved", "6A96237F-1DD2-11B2-99A6-080020736631" },
|
---|
167 | { "Solaris Reserved", "6A8D2AC7-1DD2-11B2-99A6-080020736631" },
|
---|
168 | { "NetBSD Swap", "49F48D32-B10E-11DC-B99B-0019D1879648" },
|
---|
169 | { "NetBSD FFS", "49F48D5A-B10E-11DC-B99B-0019D1879648" },
|
---|
170 | { "NetBSD LFS", "49F48D82-B10E-11DC-B99B-0019D1879648" },
|
---|
171 | { "NetBSD RAID", "49F48DAA-B10E-11DC-B99B-0019D1879648" },
|
---|
172 | { "NetBSD Concatenated", "2DB519C4-B10F-11DC-B99B-0019D1879648" },
|
---|
173 | { "NetBSD Encrypted", "2DB519EC-B10F-11DC-B99B-0019D1879648" },
|
---|
174 | { "ChromeOS ChromeOS kernel", "FE3A2A5D-4F32-41A7-B725-ACCC3285A309" },
|
---|
175 | { "ChromeOS rootfs", "3CB8E202-3B7E-47DD-8A3C-7FF2A13CFCEC" },
|
---|
176 | { "ChromeOS future use", "2E0A753D-9E48-43B0-8337-B15192CB1B5E" },
|
---|
177 | { "MidnightBSD Boot", "85D5E45E-237C-11E1-B4B3-E89A8F7FC3A7" },
|
---|
178 | { "MidnightBSD Data", "85D5E45A-237C-11E1-B4B3-E89A8F7FC3A7" },
|
---|
179 | { "MidnightBSD Swap", "85D5E45B-237C-11E1-B4B3-E89A8F7FC3A7" },
|
---|
180 | { "MidnightBSD UFS", "0394Ef8B-237E-11E1-B4B3-E89A8F7FC3A7" },
|
---|
181 | { "MidnightBSD Vinum VM", "85D5E45C-237C-11E1-B4B3-E89A8F7FC3A7" },
|
---|
182 | { "MidnightBSD ZFS", "85D5E45D-237C-11E1-B4B3-E89A8F7FC3A7" }
|
---|
183 | };
|
---|
184 |
|
---|
185 |
|
---|
186 | extern gpt_t * gpt_read_gpt_header(service_id_t dev_handle);
|
---|
187 | extern int gpt_write_gpt_header(gpt_t * header, service_id_t dev_handle);
|
---|
188 |
|
---|
189 | extern gpt_parts_t * gpt_read_partitions(gpt_t * gpt);
|
---|
190 | extern int gpt_write_partitions(gpt_parts_t * parts, gpt_t * header, service_id_t dev_handle);
|
---|
191 | extern int gpt_add_partition(gpt_parts_t * parts, g_part_t * partition);
|
---|
192 | extern void gpt_remove_partition(gpt_parts_t * parts, int idx);
|
---|
193 | extern void gpt_set_part_type(g_part_t * p, int type);
|
---|
194 | extern void gpt_set_part_name(gpt_entry_t * p, char * name[], size_t length);
|
---|
195 |
|
---|
196 | extern void gpt_free_gpt(gpt_t * gpt);
|
---|
197 | extern void gpt_free_partitions(gpt_parts_t * parts);
|
---|
198 |
|
---|
199 | #endif
|
---|