]> code.delx.au - refind/blob - gptsync/gptsync.h
017f3dc22fcbb5defdc97bf757bbb92365dcd7f9
[refind] / gptsync / gptsync.h
1 /*
2 * gptsync/gptsync.h
3 * Common header for gptsync and showpart
4 *
5 * Copyright (c) 2006 Christoph Pfisterer
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * Neither the name of Christoph Pfisterer nor the names of the
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36 /* Changes copyright (c) 2013 Roderick W. Smith */
37
38 #define VERSION L"0.9.1"
39
40 //
41 // config
42 //
43
44
45 #if defined(EFI32) || defined(EFIX64)
46 #define CONFIG_EFI
47 #endif
48
49 //
50 // platform-dependent types
51 //
52
53 #ifdef CONFIG_EFI
54
55 #ifdef __MAKEWITH_GNUEFI
56 #include "efi.h"
57 #include "efilib.h"
58 #else
59 #include "../include/tiano_includes.h"
60 #endif
61
62 #define copy_guid(destguid, srcguid) (CopyMem(destguid, srcguid, 16))
63 #define guids_are_equal(guid1, guid2) (CompareMem(guid1, guid2, 16) == 0)
64
65 typedef CHAR16 CHARN;
66 #define STR(x) L##x
67
68 #endif
69
70
71 #ifndef CONFIG_EFI
72
73 #include <stdio.h>
74 #include <stdlib.h>
75 #include <string.h>
76 #include <unistd.h>
77 #include <errno.h>
78
79 #include <sys/types.h>
80 #include <sys/stat.h>
81 #include <sys/time.h>
82 #include <fcntl.h>
83
84 typedef int INTN;
85 typedef unsigned int UINTN;
86 typedef unsigned char UINT8;
87 typedef unsigned short UINT16;
88 typedef unsigned long UINT32;
89 typedef unsigned long long UINT64;
90 typedef void VOID;
91
92 typedef int BOOLEAN;
93 #ifndef FALSE
94 #define FALSE (0)
95 #endif
96 #ifndef TRUE
97 #define TRUE (1)
98 #endif
99
100 typedef unsigned short CHAR16;
101 typedef char CHARN;
102 #define STR(x) x
103
104 void Print(wchar_t *format, ...);
105
106 // FUTURE: use STR(), #define Print printf
107
108 #define CopyMem memcpy
109 #define CompareMem memcmp
110
111 #define copy_guid(destguid, srcguid) (memcpy(destguid, srcguid, 16))
112 #define guids_are_equal(guid1, guid2) (memcmp(guid1, guid2, 16) == 0)
113
114 #define EFI_UNSUPPORTED 1
115 #define EFI_ABORTED 2
116
117 #endif
118
119 #define GPT_KIND_SYSTEM (0)
120 #define GPT_KIND_DATA (1)
121 #define GPT_KIND_BASIC_DATA (2)
122 #define GPT_KIND_FATAL (3)
123
124 #define MAX_MBR_LBA 0xFFFFFFFF
125
126 //
127 // platform-independent types
128 //
129
130 typedef struct {
131 UINT8 flags;
132 UINT8 start_chs[3];
133 UINT8 type;
134 UINT8 end_chs[3];
135 UINT32 start_lba;
136 UINT32 size;
137 } MBR_PART_INFO;
138
139 typedef struct {
140 UINT8 type;
141 CHARN *name;
142 } MBR_PARTTYPE;
143
144 typedef struct {
145 UINT64 signature;
146 UINT32 spec_revision;
147 UINT32 header_size;
148 UINT32 header_crc32;
149 UINT32 reserved;
150 UINT64 header_lba;
151 UINT64 alternate_header_lba;
152 UINT64 first_usable_lba;
153 UINT64 last_usable_lba;
154 UINT8 disk_guid[16];
155 UINT64 entry_lba;
156 UINT32 entry_count;
157 UINT32 entry_size;
158 UINT32 entry_crc32;
159 } GPT_HEADER;
160
161 typedef struct {
162 UINT8 type_guid[16];
163 UINT8 partition_guid[16];
164 UINT64 start_lba;
165 UINT64 end_lba;
166 UINT64 attributes;
167 CHAR16 name[36];
168 } GPT_ENTRY;
169
170 typedef struct {
171 UINT8 guid[16];
172 UINT8 mbr_type;
173 CHARN *name;
174 UINTN kind;
175 } GPT_PARTTYPE;
176
177 typedef struct {
178 UINTN index;
179 UINT64 start_lba;
180 UINT64 end_lba;
181 UINTN mbr_type;
182 UINT8 gpt_type[16];
183 GPT_PARTTYPE *gpt_parttype;
184 BOOLEAN active;
185 } PARTITION_INFO;
186
187 //
188 // functions provided by the OS-specific module
189 //
190
191 UINT64 disk_size(VOID);
192 UINTN read_sector(UINT64 lba, UINT8 *buffer);
193 UINTN write_sector(UINT64 lba, UINT8 *buffer);
194 UINTN input_boolean(CHARN *prompt, BOOLEAN *bool_out);
195
196 //
197 // vars and functions provided by the common lib module
198 //
199
200 extern UINT8 empty_guid[16];
201
202 extern PARTITION_INFO mbr_parts[4];
203 extern UINTN mbr_part_count;
204 extern PARTITION_INFO gpt_parts[128];
205 extern UINTN gpt_part_count;
206
207 extern PARTITION_INFO new_mbr_parts[4];
208 extern UINTN new_mbr_part_count;
209
210 extern UINT8 sector[512];
211
212 extern MBR_PARTTYPE mbr_types[];
213 extern GPT_PARTTYPE gpt_types[];
214 extern GPT_PARTTYPE gpt_dummy_type;
215
216 CHARN * mbr_parttype_name(UINT8 type);
217 UINTN read_mbr(VOID);
218
219 GPT_PARTTYPE * gpt_parttype(UINT8 *type_guid);
220 UINTN read_gpt(VOID);
221
222 UINTN detect_mbrtype_fs(UINT64 partlba, UINTN *parttype, CHARN **fsname);
223
224 //
225 // actual platform-independent programs
226 //
227
228 UINTN gptsync(VOID);
229 UINTN showpart(VOID);
230
231 /* EOF */