2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2002,2005,2006,2007,2008 Free Software Foundation, Inc.
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * GRUB is distributed in the hope that it will be useful,
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.
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19 #ifndef GRUB_TYPES_HEADER
20 #define GRUB_TYPES_HEADER 1
23 #include <grub/cpu/types.h>
25 #define UNUSED __attribute__ ((unused))
28 # define GRUB_CPU_SIZEOF_VOID_P SIZEOF_VOID_P
29 # define GRUB_CPU_SIZEOF_LONG SIZEOF_LONG
30 # ifdef WORDS_BIGENDIAN
31 # define GRUB_CPU_WORDS_BIGENDIAN 1
33 # undef GRUB_CPU_WORDS_BIGENDIAN
35 #else /* ! GRUB_UTIL */
36 # define GRUB_CPU_SIZEOF_VOID_P GRUB_TARGET_SIZEOF_VOID_P
37 # define GRUB_CPU_SIZEOF_LONG GRUB_TARGET_SIZEOF_LONG
38 # ifdef GRUB_TARGET_WORDS_BIGENDIAN
39 # define GRUB_CPU_WORDS_BIGENDIAN 1
41 # undef GRUB_CPU_WORDS_BIGENDIAN
43 #endif /* ! GRUB_UTIL */
45 #if GRUB_CPU_SIZEOF_VOID_P != GRUB_CPU_SIZEOF_LONG
46 # error "This architecture is not supported because sizeof(void *) != sizeof(long)"
49 #if GRUB_CPU_SIZEOF_VOID_P != 4 && GRUB_CPU_SIZEOF_VOID_P != 8
50 # error "This architecture is not supported because sizeof(void *) != 4 and sizeof(void *) != 8"
53 /* Define various wide integers. */
54 typedef signed char grub_int8_t
;
55 typedef short grub_int16_t
;
56 typedef int grub_int32_t
;
57 #if GRUB_CPU_SIZEOF_VOID_P == 8
58 typedef long grub_int64_t
;
60 typedef long long grub_int64_t
;
63 typedef unsigned char grub_uint8_t
;
64 typedef unsigned short grub_uint16_t
;
65 typedef unsigned grub_uint32_t
;
66 #if GRUB_CPU_SIZEOF_VOID_P == 8
67 typedef unsigned long grub_uint64_t
;
69 typedef unsigned long long grub_uint64_t
;
73 #if GRUB_TARGET_SIZEOF_VOID_P == 8
74 typedef grub_uint64_t grub_target_addr_t
;
75 typedef grub_uint64_t grub_target_off_t
;
76 typedef grub_uint64_t grub_target_size_t
;
77 typedef grub_int64_t grub_target_ssize_t
;
79 typedef grub_uint32_t grub_target_addr_t
;
80 typedef grub_uint32_t grub_target_off_t
;
81 typedef grub_uint32_t grub_target_size_t
;
82 typedef grub_int32_t grub_target_ssize_t
;
85 #if GRUB_CPU_SIZEOF_VOID_P == 8
86 typedef grub_uint64_t grub_addr_t
;
87 typedef grub_uint64_t grub_size_t
;
88 typedef grub_int64_t grub_ssize_t
;
90 typedef grub_uint32_t grub_addr_t
;
91 typedef grub_uint32_t grub_size_t
;
92 typedef grub_int32_t grub_ssize_t
;
95 #if GRUB_CPU_SIZEOF_VOID_P == 8
96 # define ULONG_MAX 18446744073709551615UL
97 # define LONG_MAX 9223372036854775807UL
99 # define ULONG_MAX 4294967295UL
100 # define LONG_MAX 2147483647UL
103 /* The type for representing a file offset. */
104 typedef grub_uint64_t grub_off_t
;
106 /* The type for representing a disk block address. */
107 typedef grub_uint64_t grub_disk_addr_t
;
110 #define grub_swap_bytes16(x) \
112 grub_uint16_t _x = (x); \
113 (grub_uint16_t) ((_x << 8) | (_x >> 8)); \
116 #if defined(__GNUC__) && (__GNUC__ > 3) && (__GNUC__ > 4 || __GNUC_MINOR__ >= 3)
117 static inline grub_uint32_t
grub_swap_bytes32(grub_uint32_t x
)
119 return __builtin_bswap32(x
);
122 static inline grub_uint64_t
grub_swap_bytes64(grub_uint64_t x
)
124 return __builtin_bswap64(x
);
126 #else /* not gcc 4.3 or newer */
127 #define grub_swap_bytes32(x) \
129 grub_uint32_t _x = (x); \
130 (grub_uint32_t) ((_x << 24) \
131 | ((_x & (grub_uint32_t) 0xFF00UL) << 8) \
132 | ((_x & (grub_uint32_t) 0xFF0000UL) >> 8) \
136 #define grub_swap_bytes64(x) \
138 grub_uint64_t _x = (x); \
139 (grub_uint64_t) ((_x << 56) \
140 | ((_x & (grub_uint64_t) 0xFF00ULL) << 40) \
141 | ((_x & (grub_uint64_t) 0xFF0000ULL) << 24) \
142 | ((_x & (grub_uint64_t) 0xFF000000ULL) << 8) \
143 | ((_x & (grub_uint64_t) 0xFF00000000ULL) >> 8) \
144 | ((_x & (grub_uint64_t) 0xFF0000000000ULL) >> 24) \
145 | ((_x & (grub_uint64_t) 0xFF000000000000ULL) >> 40) \
148 #endif /* not gcc 4.3 or newer */
150 #ifdef GRUB_CPU_WORDS_BIGENDIAN
151 # define grub_cpu_to_le16(x) grub_swap_bytes16(x)
152 # define grub_cpu_to_le32(x) grub_swap_bytes32(x)
153 # define grub_cpu_to_le64(x) grub_swap_bytes64(x)
154 # define grub_le_to_cpu16(x) grub_swap_bytes16(x)
155 # define grub_le_to_cpu32(x) grub_swap_bytes32(x)
156 # define grub_le_to_cpu64(x) grub_swap_bytes64(x)
157 # define grub_cpu_to_be16(x) ((grub_uint16_t) (x))
158 # define grub_cpu_to_be32(x) ((grub_uint32_t) (x))
159 # define grub_cpu_to_be64(x) ((grub_uint64_t) (x))
160 # define grub_be_to_cpu16(x) ((grub_uint16_t) (x))
161 # define grub_be_to_cpu32(x) ((grub_uint32_t) (x))
162 # define grub_be_to_cpu64(x) ((grub_uint64_t) (x))
163 # ifdef GRUB_TARGET_WORDS_BIGENDIAN
164 # define grub_target_to_host16(x) ((grub_uint16_t) (x))
165 # define grub_target_to_host32(x) ((grub_uint32_t) (x))
166 # define grub_target_to_host64(x) ((grub_uint64_t) (x))
167 # define grub_host_to_target16(x) ((grub_uint16_t) (x))
168 # define grub_host_to_target32(x) ((grub_uint32_t) (x))
169 # define grub_host_to_target64(x) ((grub_uint64_t) (x))
170 # else /* ! GRUB_TARGET_WORDS_BIGENDIAN */
171 # define grub_target_to_host16(x) grub_swap_bytes16(x)
172 # define grub_target_to_host32(x) grub_swap_bytes32(x)
173 # define grub_target_to_host64(x) grub_swap_bytes64(x)
174 # define grub_host_to_target16(x) grub_swap_bytes16(x)
175 # define grub_host_to_target32(x) grub_swap_bytes32(x)
176 # define grub_host_to_target64(x) grub_swap_bytes64(x)
178 #else /* ! WORDS_BIGENDIAN */
179 # define grub_cpu_to_le16(x) ((grub_uint16_t) (x))
180 # define grub_cpu_to_le32(x) ((grub_uint32_t) (x))
181 # define grub_cpu_to_le64(x) ((grub_uint64_t) (x))
182 # define grub_le_to_cpu16(x) ((grub_uint16_t) (x))
183 # define grub_le_to_cpu32(x) ((grub_uint32_t) (x))
184 # define grub_le_to_cpu64(x) ((grub_uint64_t) (x))
185 # define grub_cpu_to_be16(x) grub_swap_bytes16(x)
186 # define grub_cpu_to_be32(x) grub_swap_bytes32(x)
187 # define grub_cpu_to_be64(x) grub_swap_bytes64(x)
188 # define grub_be_to_cpu16(x) grub_swap_bytes16(x)
189 # define grub_be_to_cpu32(x) grub_swap_bytes32(x)
190 # define grub_be_to_cpu64(x) grub_swap_bytes64(x)
191 # ifdef GRUB_TARGET_WORDS_BIGENDIAN
192 # define grub_target_to_host16(x) grub_swap_bytes16(x)
193 # define grub_target_to_host32(x) grub_swap_bytes32(x)
194 # define grub_target_to_host64(x) grub_swap_bytes64(x)
195 # define grub_host_to_target16(x) grub_swap_bytes16(x)
196 # define grub_host_to_target32(x) grub_swap_bytes32(x)
197 # define grub_host_to_target64(x) grub_swap_bytes64(x)
198 # else /* ! GRUB_TARGET_WORDS_BIGENDIAN */
199 # define grub_target_to_host16(x) ((grub_uint16_t) (x))
200 # define grub_target_to_host32(x) ((grub_uint32_t) (x))
201 # define grub_target_to_host64(x) ((grub_uint64_t) (x))
202 # define grub_host_to_target16(x) ((grub_uint16_t) (x))
203 # define grub_host_to_target32(x) ((grub_uint32_t) (x))
204 # define grub_host_to_target64(x) ((grub_uint64_t) (x))
206 #endif /* ! WORDS_BIGENDIAN */
208 #endif /* ! GRUB_TYPES_HEADER */