]> code.delx.au - refind/blob - gptsync/gptsync.h
2a1bbf01bf67e48b6844a95e9b421a04420acf4b
[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.6.9"
39
40 //
41 // config
42 //
43
44 #if defined(EFI32) || defined(EFIX64)
45 #define CONFIG_EFI
46 #endif
47
48 //
49 // platform-dependent types
50 //
51
52 #ifdef CONFIG_EFI
53
54 #ifdef __MAKEWITH_GNUEFI
55 #include "efi.h"
56 #include "efilib.h"
57 #else
58 #include "../include/tiano_includes.h"
59 #endif
60
61 #define copy_guid(destguid, srcguid) (CopyMem(destguid, srcguid, 16))
62 #define guids_are_equal(guid1, guid2) (CompareMem(guid1, guid2, 16) == 0)
63
64 typedef CHAR16 CHARN;
65 #define STR(x) L##x
66
67 #endif
68
69
70 #ifndef CONFIG_EFI
71
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <string.h>
75 #include <unistd.h>
76 #include <errno.h>
77
78 #include <sys/types.h>
79 #include <sys/stat.h>
80 #include <sys/time.h>
81 #include <fcntl.h>
82
83 typedef int INTN;
84 typedef unsigned int UINTN;
85 typedef unsigned char UINT8;
86 typedef unsigned short UINT16;
87 typedef unsigned long UINT32;
88 typedef unsigned long long UINT64;
89 typedef void VOID;
90
91 typedef int BOOLEAN;
92 #ifndef FALSE
93 #define FALSE (0)
94 #endif
95 #ifndef TRUE
96 #define TRUE (1)
97 #endif
98
99 typedef unsigned short CHAR16;
100 typedef char CHARN;
101 #define STR(x) x
102
103 void Print(wchar_t *format, ...);
104
105 // FUTURE: use STR(), #define Print printf
106
107 #define CopyMem memcpy
108 #define SetMem memset
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 //
120 // platform-independent types
121 //
122
123 typedef struct {
124 UINT8 flags;
125 UINT8 start_chs[3];
126 UINT8 type;
127 UINT8 end_chs[3];
128 UINT32 start_lba;
129 UINT32 size;
130 } MBR_PART_INFO;
131
132 typedef struct {
133 UINT8 type;
134 CHARN *name;
135 } MBR_PARTTYPE;
136
137 typedef struct {
138 UINT64 signature;
139 UINT32 spec_revision;
140 UINT32 header_size;
141 UINT32 header_crc32;
142 UINT32 reserved;
143 UINT64 header_lba;
144 UINT64 alternate_header_lba;
145 UINT64 first_usable_lba;
146 UINT64 last_usable_lba;
147 UINT8 disk_guid[16];
148 UINT64 entry_lba;
149 UINT32 entry_count;
150 UINT32 entry_size;
151 UINT32 entry_crc32;
152 } GPT_HEADER;
153
154 typedef struct {
155 UINT8 type_guid[16];
156 UINT8 partition_guid[16];
157 UINT64 start_lba;
158 UINT64 end_lba;
159 UINT64 attributes;
160 CHAR16 name[36];
161 } GPT_ENTRY;
162
163 #define GPT_KIND_SYSTEM (0)
164 #define GPT_KIND_DATA (1)
165 #define GPT_KIND_BASIC_DATA (2)
166 #define GPT_KIND_FATAL (3)
167
168 typedef struct {
169 UINT8 guid[16];
170 UINT8 mbr_type;
171 CHARN *name;
172 UINTN kind;
173 } GPT_PARTTYPE;
174
175 typedef struct {
176 UINTN index;
177 UINT64 start_lba;
178 UINT64 end_lba;
179 UINTN mbr_type;
180 UINT8 gpt_type[16];
181 GPT_PARTTYPE *gpt_parttype;
182 BOOLEAN active;
183 } PARTITION_INFO;
184
185 //
186 // functions provided by the OS-specific module
187 //
188
189 UINT64 disk_size(VOID);
190 UINTN read_sector(UINT64 lba, UINT8 *buffer);
191 UINTN write_sector(UINT64 lba, UINT8 *buffer);
192 UINTN input_boolean(CHARN *prompt, BOOLEAN *bool_out);
193
194 //
195 // vars and functions provided by the common lib module
196 //
197
198 extern UINT8 empty_guid[16];
199
200 extern PARTITION_INFO mbr_parts[4];
201 extern UINTN mbr_part_count;
202 extern PARTITION_INFO gpt_parts[128];
203 extern UINTN gpt_part_count;
204
205 extern PARTITION_INFO new_mbr_parts[4];
206 extern UINTN new_mbr_part_count;
207
208 extern UINT8 sector[512];
209
210 extern MBR_PARTTYPE mbr_types[];
211 extern GPT_PARTTYPE gpt_types[];
212 extern GPT_PARTTYPE gpt_dummy_type;
213
214 CHARN * mbr_parttype_name(UINT8 type);
215 UINTN read_mbr(VOID);
216
217 GPT_PARTTYPE * gpt_parttype(UINT8 *type_guid);
218 UINTN read_gpt(VOID);
219
220 UINTN detect_mbrtype_fs(UINT64 partlba, UINTN *parttype, CHARN **fsname);
221
222 //
223 // actual platform-independent programs
224 //
225
226 UINTN gptsync(VOID);
227 UINTN showpart(VOID);
228
229 /* EOF */