]>
Commit | Line | Data |
---|---|---|
6a161fa9 | 1 | /* |
4b13b216 | 2 | * GRUB -- GRand Unified Bootloader |
58bc8bd5 | 3 | * Copyright (C) 2002,2005,2006,2007,2008,2009 Free Software Foundation, Inc. |
6a161fa9 | 4 | * |
5a79f472 | 5 | * GRUB is free software: you can redistribute it and/or modify |
6a161fa9 | 6 | * it under the terms of the GNU General Public License as published by |
5a79f472 | 7 | * the Free Software Foundation, either version 3 of the License, or |
6a161fa9 | 8 | * (at your option) any later version. |
9 | * | |
5a79f472 | 10 | * GRUB is distributed in the hope that it will be useful, |
6a161fa9 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
5a79f472 | 16 | * along with GRUB. If not, see <http://www.gnu.org/licenses/>. |
6a161fa9 | 17 | */ |
18 | ||
4b13b216 | 19 | #ifndef GRUB_TYPES_HEADER |
20 | #define GRUB_TYPES_HEADER 1 | |
6a161fa9 | 21 | |
22 | #include <config.h> | |
c36e5cd1 | 23 | #ifndef GRUB_UTIL |
4b13b216 | 24 | #include <grub/cpu/types.h> |
c36e5cd1 | 25 | #endif |
6a161fa9 | 26 | |
7e47e27b VS |
27 | #ifdef __MINGW32__ |
28 | #define GRUB_PACKED __attribute__ ((packed,gcc_struct)) | |
29 | #else | |
30 | #define GRUB_PACKED __attribute__ ((packed)) | |
31 | #endif | |
32 | ||
7b780018 VS |
33 | #ifdef GRUB_BUILD |
34 | # define GRUB_CPU_SIZEOF_VOID_P BUILD_SIZEOF_VOID_P | |
35 | # define GRUB_CPU_SIZEOF_LONG BUILD_SIZEOF_LONG | |
36 | # if BUILD_WORDS_BIGENDIAN | |
37 | # define GRUB_CPU_WORDS_BIGENDIAN 1 | |
38 | # else | |
39 | # undef GRUB_CPU_WORDS_BIGENDIAN | |
40 | # endif | |
41 | #elif defined (GRUB_UTIL) | |
4b13b216 | 42 | # define GRUB_CPU_SIZEOF_VOID_P SIZEOF_VOID_P |
43 | # define GRUB_CPU_SIZEOF_LONG SIZEOF_LONG | |
6a161fa9 | 44 | # ifdef WORDS_BIGENDIAN |
4b13b216 | 45 | # define GRUB_CPU_WORDS_BIGENDIAN 1 |
6a161fa9 | 46 | # else |
4b13b216 | 47 | # undef GRUB_CPU_WORDS_BIGENDIAN |
6a161fa9 | 48 | # endif |
4b13b216 | 49 | #else /* ! GRUB_UTIL */ |
b977bf01 | 50 | # define GRUB_CPU_SIZEOF_VOID_P GRUB_TARGET_SIZEOF_VOID_P |
51 | # define GRUB_CPU_SIZEOF_LONG GRUB_TARGET_SIZEOF_LONG | |
52 | # ifdef GRUB_TARGET_WORDS_BIGENDIAN | |
4b13b216 | 53 | # define GRUB_CPU_WORDS_BIGENDIAN 1 |
6a161fa9 | 54 | # else |
4b13b216 | 55 | # undef GRUB_CPU_WORDS_BIGENDIAN |
6a161fa9 | 56 | # endif |
4b13b216 | 57 | #endif /* ! GRUB_UTIL */ |
6a161fa9 | 58 | |
4b13b216 | 59 | #if GRUB_CPU_SIZEOF_VOID_P != 4 && GRUB_CPU_SIZEOF_VOID_P != 8 |
6a161fa9 | 60 | # error "This architecture is not supported because sizeof(void *) != 4 and sizeof(void *) != 8" |
61 | #endif | |
62 | ||
4b4eeb3c VS |
63 | #if GRUB_CPU_SIZEOF_LONG != 4 && GRUB_CPU_SIZEOF_LONG != 8 |
64 | # error "This architecture is not supported because sizeof(long) != 4 and sizeof(long) != 8" | |
65 | #endif | |
66 | ||
2f9f40ba | 67 | #if !defined (GRUB_UTIL) && !defined (GRUB_TARGET_WORDSIZE) |
c402ab17 | 68 | # if GRUB_TARGET_SIZEOF_VOID_P == 4 |
69 | # define GRUB_TARGET_WORDSIZE 32 | |
70 | # elif GRUB_TARGET_SIZEOF_VOID_P == 8 | |
71 | # define GRUB_TARGET_WORDSIZE 64 | |
72 | # endif | |
73 | #endif | |
74 | ||
f0715577 GW |
75 | #ifndef __CHAR_BIT__ |
76 | # error __CHAR_BIT__ is not defined | |
77 | #elif __CHAR_BIT__ != 8 | |
78 | # error __CHAR_BIT__ is not equal 8 | |
79 | #else | |
80 | # define GRUB_CHAR_BIT __CHAR_BIT__ | |
81 | #endif | |
82 | ||
3e5f7f51 GW |
83 | #define GRUB_TYPE_BITS(type) (sizeof(type) * GRUB_CHAR_BIT) |
84 | ||
6a161fa9 | 85 | /* Define various wide integers. */ |
4b13b216 | 86 | typedef signed char grub_int8_t; |
87 | typedef short grub_int16_t; | |
88 | typedef int grub_int32_t; | |
af54062b JMC |
89 | # define PRIxGRUB_INT32_T "x" |
90 | # define PRIdGRUB_INT32_T "d" | |
1c8f0f8d | 91 | #if GRUB_CPU_SIZEOF_LONG == 8 |
4b13b216 | 92 | typedef long grub_int64_t; |
af54062b JMC |
93 | # define PRIxGRUB_INT64_T "lx" |
94 | # define PRIdGRUB_INT64_T "ld" | |
6a161fa9 | 95 | #else |
4b13b216 | 96 | typedef long long grub_int64_t; |
af54062b JMC |
97 | # define PRIxGRUB_INT64_T "llx" |
98 | # define PRIdGRUB_INT64_T "lld" | |
6a161fa9 | 99 | #endif |
100 | ||
4b13b216 | 101 | typedef unsigned char grub_uint8_t; |
102 | typedef unsigned short grub_uint16_t; | |
103 | typedef unsigned grub_uint32_t; | |
1782b135 TG |
104 | # define PRIxGRUB_UINT32_T "x" |
105 | # define PRIuGRUB_UINT32_T "u" | |
1c8f0f8d | 106 | #if GRUB_CPU_SIZEOF_LONG == 8 |
4b13b216 | 107 | typedef unsigned long grub_uint64_t; |
1782b135 | 108 | # define PRIxGRUB_UINT64_T "lx" |
105de6a7 | 109 | # define PRIuGRUB_UINT64_T "lu" |
6a161fa9 | 110 | #else |
4b13b216 | 111 | typedef unsigned long long grub_uint64_t; |
1782b135 | 112 | # define PRIxGRUB_UINT64_T "llx" |
105de6a7 | 113 | # define PRIuGRUB_UINT64_T "llu" |
6a161fa9 | 114 | #endif |
115 | ||
116 | /* Misc types. */ | |
59b8208a | 117 | |
118 | #if GRUB_CPU_SIZEOF_VOID_P == 8 | |
4b13b216 | 119 | typedef grub_uint64_t grub_addr_t; |
4b13b216 | 120 | typedef grub_uint64_t grub_size_t; |
121 | typedef grub_int64_t grub_ssize_t; | |
50f0bcda | 122 | |
053cfcdd VS |
123 | # define GRUB_SIZE_MAX 18446744073709551615UL |
124 | ||
50f0bcda | 125 | # if GRUB_CPU_SIZEOF_LONG == 8 |
9aad3cd9 VS |
126 | # define PRIxGRUB_SIZE "lx" |
127 | # define PRIxGRUB_ADDR "lx" | |
128 | # define PRIuGRUB_SIZE "lu" | |
129 | # define PRIdGRUB_SSIZE "ld" | |
50f0bcda | 130 | # else |
9aad3cd9 VS |
131 | # define PRIxGRUB_SIZE "llx" |
132 | # define PRIxGRUB_ADDR "llx" | |
133 | # define PRIuGRUB_SIZE "llu" | |
134 | # define PRIdGRUB_SSIZE "lld" | |
50f0bcda | 135 | # endif |
6a161fa9 | 136 | #else |
4b13b216 | 137 | typedef grub_uint32_t grub_addr_t; |
4b13b216 | 138 | typedef grub_uint32_t grub_size_t; |
139 | typedef grub_int32_t grub_ssize_t; | |
50f0bcda | 140 | |
053cfcdd VS |
141 | # define GRUB_SIZE_MAX 4294967295UL |
142 | ||
50f0bcda | 143 | # define PRIxGRUB_SIZE "x" |
f9c30af6 | 144 | # define PRIxGRUB_ADDR "x" |
9af6dac3 | 145 | # define PRIuGRUB_SIZE "u" |
8530b00a | 146 | # define PRIdGRUB_SSIZE "d" |
6a161fa9 | 147 | #endif |
148 | ||
bcd29eea CW |
149 | #define GRUB_SCHAR_MAX 127 |
150 | #define GRUB_SCHAR_MIN (-GRUB_SCHAR_MAX - 1) | |
1f1a380b SJ |
151 | #define GRUB_UCHAR_MAX 0xFF |
152 | #define GRUB_USHRT_MAX 65535 | |
0b3b3b38 | 153 | #define GRUB_SHRT_MAX 0x7fff |
bcd29eea | 154 | #define GRUB_SHRT_MIN (-GRUB_SHRT_MAX - 1) |
1f1a380b | 155 | #define GRUB_UINT_MAX 4294967295U |
0b3b3b38 | 156 | #define GRUB_INT_MAX 0x7fffffff |
bcd29eea | 157 | #define GRUB_INT_MIN (-GRUB_INT_MAX - 1) |
10bafa1c | 158 | #define GRUB_INT32_MAX 2147483647 |
bcd29eea | 159 | #define GRUB_INT32_MIN (-GRUB_INT32_MAX - 1) |
1f1a380b | 160 | |
1c8f0f8d | 161 | #if GRUB_CPU_SIZEOF_LONG == 8 |
41bb0fe9 | 162 | # define GRUB_ULONG_MAX 18446744073709551615UL |
c94b18a9 | 163 | # define GRUB_LONG_MAX 9223372036854775807L |
5cd7dd46 | 164 | #else |
41bb0fe9 | 165 | # define GRUB_ULONG_MAX 4294967295UL |
c94b18a9 | 166 | # define GRUB_LONG_MAX 2147483647L |
5cd7dd46 | 167 | #endif |
bcd29eea | 168 | # define GRUB_LONG_MIN (-GRUB_LONG_MAX - 1) |
5cd7dd46 | 169 | |
b34cb387 GW |
170 | /* |
171 | * Cast to unsigned long long so that the "return value" is always a consistent | |
172 | * type and to ensure an unsigned value regardless of type parameter. | |
173 | */ | |
174 | #define GRUB_TYPE_U_MAX(type) ((unsigned long long)((typeof (type))(~0))) | |
175 | #define GRUB_TYPE_U_MIN(type) 0ULL | |
176 | ||
1f313b94 VS |
177 | typedef grub_uint64_t grub_properly_aligned_t; |
178 | ||
a14b16d1 | 179 | #define GRUB_PROPERLY_ALIGNED_ARRAY(name, size) grub_properly_aligned_t name[((size) + sizeof (grub_properly_aligned_t) - 1) / sizeof (grub_properly_aligned_t)] |
09d842b9 | 180 | |
524a1e6a | 181 | /* The type for representing a file offset. */ |
182 | typedef grub_uint64_t grub_off_t; | |
183 | ||
184 | /* The type for representing a disk block address. */ | |
185 | typedef grub_uint64_t grub_disk_addr_t; | |
7f67dc13 | 186 | |
6a161fa9 | 187 | /* Byte-orders. */ |
85eb579a VS |
188 | static inline grub_uint16_t grub_swap_bytes16(grub_uint16_t _x) |
189 | { | |
190 | return (grub_uint16_t) ((_x << 8) | (_x >> 8)); | |
191 | } | |
6a161fa9 | 192 | |
49d3ab46 VS |
193 | #define grub_swap_bytes16_compile_time(x) ((((x) & 0xff) << 8) | (((x) & 0xff00) >> 8)) |
194 | #define grub_swap_bytes32_compile_time(x) ((((x) & 0xff) << 24) | (((x) & 0xff00) << 8) | (((x) & 0xff0000) >> 8) | (((x) & 0xff000000UL) >> 24)) | |
7c006811 VS |
195 | #define grub_swap_bytes64_compile_time(x) \ |
196 | ({ \ | |
197 | grub_uint64_t _x = (x); \ | |
198 | (grub_uint64_t) ((_x << 56) \ | |
199 | | ((_x & (grub_uint64_t) 0xFF00ULL) << 40) \ | |
200 | | ((_x & (grub_uint64_t) 0xFF0000ULL) << 24) \ | |
201 | | ((_x & (grub_uint64_t) 0xFF000000ULL) << 8) \ | |
202 | | ((_x & (grub_uint64_t) 0xFF00000000ULL) >> 8) \ | |
203 | | ((_x & (grub_uint64_t) 0xFF0000000000ULL) >> 24) \ | |
204 | | ((_x & (grub_uint64_t) 0xFF000000000000ULL) >> 40) \ | |
205 | | (_x >> 56)); \ | |
206 | }) | |
49d3ab46 | 207 | |
77697d14 | 208 | #if (defined(__GNUC__) && (__GNUC__ > 3) && (__GNUC__ > 4 || __GNUC_MINOR__ >= 3)) || defined(__clang__) |
01453bfc | 209 | static inline grub_uint32_t grub_swap_bytes32(grub_uint32_t x) |
210 | { | |
211 | return __builtin_bswap32(x); | |
212 | } | |
213 | ||
214 | static inline grub_uint64_t grub_swap_bytes64(grub_uint64_t x) | |
215 | { | |
216 | return __builtin_bswap64(x); | |
217 | } | |
218 | #else /* not gcc 4.3 or newer */ | |
85eb579a VS |
219 | static inline grub_uint32_t grub_swap_bytes32(grub_uint32_t _x) |
220 | { | |
221 | return ((_x << 24) | |
222 | | ((_x & (grub_uint32_t) 0xFF00UL) << 8) | |
223 | | ((_x & (grub_uint32_t) 0xFF0000UL) >> 8) | |
224 | | (_x >> 24)); | |
225 | } | |
6a161fa9 | 226 | |
85eb579a VS |
227 | static inline grub_uint64_t grub_swap_bytes64(grub_uint64_t _x) |
228 | { | |
229 | return ((_x << 56) | |
230 | | ((_x & (grub_uint64_t) 0xFF00ULL) << 40) | |
231 | | ((_x & (grub_uint64_t) 0xFF0000ULL) << 24) | |
232 | | ((_x & (grub_uint64_t) 0xFF000000ULL) << 8) | |
233 | | ((_x & (grub_uint64_t) 0xFF00000000ULL) >> 8) | |
234 | | ((_x & (grub_uint64_t) 0xFF0000000000ULL) >> 24) | |
235 | | ((_x & (grub_uint64_t) 0xFF000000000000ULL) >> 40) | |
236 | | (_x >> 56)); | |
237 | } | |
01453bfc | 238 | #endif /* not gcc 4.3 or newer */ |
6a161fa9 | 239 | |
4b13b216 | 240 | #ifdef GRUB_CPU_WORDS_BIGENDIAN |
241 | # define grub_cpu_to_le16(x) grub_swap_bytes16(x) | |
242 | # define grub_cpu_to_le32(x) grub_swap_bytes32(x) | |
243 | # define grub_cpu_to_le64(x) grub_swap_bytes64(x) | |
244 | # define grub_le_to_cpu16(x) grub_swap_bytes16(x) | |
245 | # define grub_le_to_cpu32(x) grub_swap_bytes32(x) | |
246 | # define grub_le_to_cpu64(x) grub_swap_bytes64(x) | |
247 | # define grub_cpu_to_be16(x) ((grub_uint16_t) (x)) | |
248 | # define grub_cpu_to_be32(x) ((grub_uint32_t) (x)) | |
249 | # define grub_cpu_to_be64(x) ((grub_uint64_t) (x)) | |
250 | # define grub_be_to_cpu16(x) ((grub_uint16_t) (x)) | |
251 | # define grub_be_to_cpu32(x) ((grub_uint32_t) (x)) | |
252 | # define grub_be_to_cpu64(x) ((grub_uint64_t) (x)) | |
bd40efbf | 253 | # define grub_cpu_to_be16_compile_time(x) ((grub_uint16_t) (x)) |
7c006811 VS |
254 | # define grub_cpu_to_be32_compile_time(x) ((grub_uint32_t) (x)) |
255 | # define grub_cpu_to_be64_compile_time(x) ((grub_uint64_t) (x)) | |
256 | # define grub_be_to_cpu64_compile_time(x) ((grub_uint64_t) (x)) | |
49d3ab46 | 257 | # define grub_cpu_to_le32_compile_time(x) grub_swap_bytes32_compile_time(x) |
bbb39a45 | 258 | # define grub_cpu_to_le64_compile_time(x) grub_swap_bytes64_compile_time(x) |
49d3ab46 | 259 | # define grub_cpu_to_le16_compile_time(x) grub_swap_bytes16_compile_time(x) |
6a161fa9 | 260 | #else /* ! WORDS_BIGENDIAN */ |
4b13b216 | 261 | # define grub_cpu_to_le16(x) ((grub_uint16_t) (x)) |
262 | # define grub_cpu_to_le32(x) ((grub_uint32_t) (x)) | |
263 | # define grub_cpu_to_le64(x) ((grub_uint64_t) (x)) | |
264 | # define grub_le_to_cpu16(x) ((grub_uint16_t) (x)) | |
265 | # define grub_le_to_cpu32(x) ((grub_uint32_t) (x)) | |
266 | # define grub_le_to_cpu64(x) ((grub_uint64_t) (x)) | |
267 | # define grub_cpu_to_be16(x) grub_swap_bytes16(x) | |
268 | # define grub_cpu_to_be32(x) grub_swap_bytes32(x) | |
269 | # define grub_cpu_to_be64(x) grub_swap_bytes64(x) | |
270 | # define grub_be_to_cpu16(x) grub_swap_bytes16(x) | |
271 | # define grub_be_to_cpu32(x) grub_swap_bytes32(x) | |
272 | # define grub_be_to_cpu64(x) grub_swap_bytes64(x) | |
bd40efbf | 273 | # define grub_cpu_to_be16_compile_time(x) grub_swap_bytes16_compile_time(x) |
7c006811 VS |
274 | # define grub_cpu_to_be32_compile_time(x) grub_swap_bytes32_compile_time(x) |
275 | # define grub_cpu_to_be64_compile_time(x) grub_swap_bytes64_compile_time(x) | |
276 | # define grub_be_to_cpu64_compile_time(x) grub_swap_bytes64_compile_time(x) | |
49d3ab46 VS |
277 | # define grub_cpu_to_le16_compile_time(x) ((grub_uint16_t) (x)) |
278 | # define grub_cpu_to_le32_compile_time(x) ((grub_uint32_t) (x)) | |
bbb39a45 | 279 | # define grub_cpu_to_le64_compile_time(x) ((grub_uint64_t) (x)) |
7c006811 | 280 | |
6a161fa9 | 281 | #endif /* ! WORDS_BIGENDIAN */ |
282 | ||
8ad190ca VS |
283 | struct grub_unaligned_uint16 |
284 | { | |
285 | grub_uint16_t val; | |
286 | } GRUB_PACKED; | |
287 | struct grub_unaligned_uint32 | |
288 | { | |
289 | grub_uint32_t val; | |
290 | } GRUB_PACKED; | |
291 | struct grub_unaligned_uint64 | |
292 | { | |
293 | grub_uint64_t val; | |
294 | } GRUB_PACKED; | |
295 | ||
296 | typedef struct grub_unaligned_uint16 grub_unaligned_uint16_t; | |
297 | typedef struct grub_unaligned_uint32 grub_unaligned_uint32_t; | |
298 | typedef struct grub_unaligned_uint64 grub_unaligned_uint64_t; | |
299 | ||
1f313b94 VS |
300 | static inline grub_uint16_t grub_get_unaligned16 (const void *ptr) |
301 | { | |
8ad190ca VS |
302 | const struct grub_unaligned_uint16 *dd |
303 | = (const struct grub_unaligned_uint16 *) ptr; | |
304 | return dd->val; | |
1f313b94 VS |
305 | } |
306 | ||
307 | static inline void grub_set_unaligned16 (void *ptr, grub_uint16_t val) | |
308 | { | |
8ad190ca VS |
309 | struct grub_unaligned_uint16 *dd = (struct grub_unaligned_uint16 *) ptr; |
310 | dd->val = val; | |
1f313b94 VS |
311 | } |
312 | ||
313 | static inline grub_uint32_t grub_get_unaligned32 (const void *ptr) | |
b2d7e331 | 314 | { |
8ad190ca VS |
315 | const struct grub_unaligned_uint32 *dd |
316 | = (const struct grub_unaligned_uint32 *) ptr; | |
317 | return dd->val; | |
b2d7e331 SJ |
318 | } |
319 | ||
1f313b94 | 320 | static inline void grub_set_unaligned32 (void *ptr, grub_uint32_t val) |
b2d7e331 | 321 | { |
8ad190ca VS |
322 | struct grub_unaligned_uint32 *dd = (struct grub_unaligned_uint32 *) ptr; |
323 | dd->val = val; | |
b2d7e331 SJ |
324 | } |
325 | ||
1f313b94 | 326 | static inline grub_uint64_t grub_get_unaligned64 (const void *ptr) |
b2d7e331 | 327 | { |
d28e1163 VS |
328 | const struct grub_unaligned_uint64 *dd |
329 | = (const struct grub_unaligned_uint64 *) ptr; | |
330 | return dd->val; | |
b2d7e331 SJ |
331 | } |
332 | ||
24bd8838 VS |
333 | static inline void grub_set_unaligned64 (void *ptr, grub_uint64_t val) |
334 | { | |
335 | struct grub_unaligned_uint64_t | |
336 | { | |
337 | grub_uint64_t d; | |
7e47e27b | 338 | } GRUB_PACKED; |
24bd8838 VS |
339 | struct grub_unaligned_uint64_t *dd = (struct grub_unaligned_uint64_t *) ptr; |
340 | dd->d = val; | |
341 | } | |
342 | ||
4b13b216 | 343 | #endif /* ! GRUB_TYPES_HEADER */ |