1 /*
2  *  GRUB  --  GRand Unified Bootloader
3  *  Copyright (C) 2002,2005,2006,2007,2008,2009  Free Software Foundation, Inc.
4  *
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.
9  *
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.
14  *
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/>.
17  */
18 
19 #ifndef GRUB_TYPES_HEADER
20 #define GRUB_TYPES_HEADER	1
21 
22 #include <grub/cpu/types.h>
23 
24 #ifdef GRUB_UTIL
25 # define GRUB_CPU_SIZEOF_VOID_P	SIZEOF_VOID_P
26 # define GRUB_CPU_SIZEOF_LONG	SIZEOF_LONG
27 # ifdef WORDS_BIGENDIAN
28 #  define GRUB_CPU_WORDS_BIGENDIAN	1
29 # else
30 #  undef GRUB_CPU_WORDS_BIGENDIAN
31 # endif
32 #else /* ! GRUB_UTIL */
33 # define GRUB_CPU_SIZEOF_VOID_P	GRUB_TARGET_SIZEOF_VOID_P
34 # define GRUB_CPU_SIZEOF_LONG	GRUB_TARGET_SIZEOF_LONG
35 # ifdef GRUB_TARGET_WORDS_BIGENDIAN
36 #  define GRUB_CPU_WORDS_BIGENDIAN	1
37 # else
38 #  undef GRUB_CPU_WORDS_BIGENDIAN
39 # endif
40 #endif /* ! GRUB_UTIL */
41 
42 #if GRUB_CPU_SIZEOF_VOID_P != 4 && GRUB_CPU_SIZEOF_VOID_P != 8
43 # error "This architecture is not supported because sizeof(void *) != 4 and sizeof(void *) != 8"
44 #endif
45 
46 #ifndef GRUB_TARGET_WORDSIZE
47 # if GRUB_TARGET_SIZEOF_VOID_P == 4
48 #  define GRUB_TARGET_WORDSIZE 32
49 # elif GRUB_TARGET_SIZEOF_VOID_P == 8
50 #  define GRUB_TARGET_WORDSIZE 64
51 # endif
52 #endif
53 
54 /* Define various wide integers.  */
55 typedef signed char		grub_int8_t;
56 typedef short			grub_int16_t;
57 typedef int			grub_int32_t;
58 #if GRUB_CPU_SIZEOF_LONG == 8
59 typedef long			grub_int64_t;
60 #else
61 typedef long long		grub_int64_t;
62 #endif
63 
64 typedef unsigned char		grub_uint8_t;
65 typedef unsigned short		grub_uint16_t;
66 typedef unsigned		grub_uint32_t;
67 #if GRUB_CPU_SIZEOF_LONG == 8
68 typedef unsigned long		grub_uint64_t;
69 #else
70 typedef unsigned long long	grub_uint64_t;
71 #endif
72 
73 /* Misc types.  */
74 #if GRUB_TARGET_SIZEOF_VOID_P == 8
75 typedef grub_uint64_t	grub_target_addr_t;
76 typedef grub_uint64_t	grub_target_off_t;
77 typedef grub_uint64_t	grub_target_size_t;
78 typedef grub_int64_t	grub_target_ssize_t;
79 #else
80 typedef grub_uint32_t	grub_target_addr_t;
81 typedef grub_uint32_t	grub_target_off_t;
82 typedef grub_uint32_t	grub_target_size_t;
83 typedef grub_int32_t	grub_target_ssize_t;
84 #endif
85 
86 #if GRUB_CPU_SIZEOF_VOID_P == 8
87 typedef grub_uint64_t	grub_addr_t;
88 typedef grub_uint64_t	grub_size_t;
89 typedef grub_int64_t	grub_ssize_t;
90 #else
91 typedef grub_uint32_t	grub_addr_t;
92 typedef grub_uint32_t	grub_size_t;
93 typedef grub_int32_t	grub_ssize_t;
94 #endif
95 
96 #if GRUB_CPU_SIZEOF_VOID_P == 8
97 # define GRUB_ULONG_MAX 18446744073709551615UL
98 # define GRUB_LONG_MAX 9223372036854775807L
99 # define GRUB_LONG_MIN (-9223372036854775807L - 1)
100 #else
101 # define GRUB_ULONG_MAX 4294967295UL
102 # define GRUB_LONG_MAX 2147483647L
103 # define GRUB_LONG_MIN (-2147483647L - 1)
104 #endif
105 
106 #if GRUB_CPU_SIZEOF_VOID_P == 4
107 #define UINT_TO_PTR(x) ((void*)(grub_uint32_t)(x))
108 #define PTR_TO_UINT64(x) ((grub_uint64_t)(grub_uint32_t)(x))
109 #define PTR_TO_UINT32(x) ((grub_uint32_t)(x))
110 #else
111 #define UINT_TO_PTR(x) ((void*)(grub_uint64_t)(x))
112 #define PTR_TO_UINT64(x) ((grub_uint64_t)(x))
113 #define PTR_TO_UINT32(x) ((grub_uint32_t)(grub_uint64_t)(x))
114 #endif
115 
116 /* The type for representing a file offset.  */
117 typedef grub_uint64_t	grub_off_t;
118 
119 /* The type for representing a disk block address.  */
120 typedef grub_uint64_t	grub_disk_addr_t;
121 
122 #ifdef __GNUC__
123 typedef grub_uint64_t grub_unaligned_uint64_t __attribute__((aligned(1)));
124 typedef grub_uint32_t grub_unaligned_uint32_t __attribute__((aligned(1)));
125 typedef grub_uint16_t grub_unaligned_uint16_t __attribute__((aligned(1)));
126 #else
127 typedef grub_uint64_t grub_unaligned_uint64_t;
128 typedef grub_uint32_t grub_unaligned_uint32_t;
129 typedef grub_uint16_t grub_unaligned_uint16_t;
130 #endif
131 
132 /* Byte-orders.  */
133 #ifdef _MSC_VER
grub_swap_bytes16(grub_uint16_t x)134 __inline grub_uint16_t grub_swap_bytes16 (grub_uint16_t x)
135 {
136 	grub_uint16_t _x = (x);
137 	_x = (grub_uint16_t)((_x << 8) | (_x >> 8));
138 	return _x;
139 };
140 #else
141 #define grub_swap_bytes16(x)	\
142 ({ \
143    grub_uint16_t _x = (x); \
144    (grub_uint16_t) ((_x << 8) | (_x >> 8)); \
145 })
146 #endif
147 
148 #if !defined(__ANDROID__) && defined(__GNUC__) && (__GNUC__ > 3) && (__GNUC__ > 4 || __GNUC_MINOR__ >= 3) && defined(GRUB_TARGET_I386)
grub_swap_bytes32(grub_uint32_t x)149 static inline grub_uint32_t grub_swap_bytes32(grub_uint32_t x)
150 {
151 	return __builtin_bswap32(x);
152 }
153 
grub_swap_bytes64(grub_uint64_t x)154 static inline grub_uint64_t grub_swap_bytes64(grub_uint64_t x)
155 {
156 	return __builtin_bswap64(x);
157 }
158 #elif _MSC_VER
grub_swap_bytes32(grub_uint32_t x)159 __inline grub_uint32_t grub_swap_bytes32(grub_uint32_t x)
160 {
161    grub_uint32_t _x = (x);
162    _x = (grub_uint32_t) ((_x << 24)
163                     | ((_x & (grub_uint32_t) 0xFF00UL) << 8)
164                     | ((_x & (grub_uint32_t) 0xFF0000UL) >> 8)
165                     | (_x >> 24));
166    return _x;
167 };
168 
grub_swap_bytes64(grub_uint64_t x)169 __inline grub_uint64_t  grub_swap_bytes64(grub_uint64_t x)
170 {
171    grub_uint64_t _x = (x);
172    _x = (grub_uint64_t) ((_x << 56)
173                     | ((_x & (grub_uint64_t) 0xFF00ULL) << 40)
174                     | ((_x & (grub_uint64_t) 0xFF0000ULL) << 24)
175                     | ((_x & (grub_uint64_t) 0xFF000000ULL) << 8)
176                     | ((_x & (grub_uint64_t) 0xFF00000000ULL) >> 8)
177                     | ((_x & (grub_uint64_t) 0xFF0000000000ULL) >> 24)
178                     | ((_x & (grub_uint64_t) 0xFF000000000000ULL) >> 40)
179                     | (_x >> 56));
180    return _x;
181 };
182 #else					/* not gcc 4.3 or newer */
183 #define grub_swap_bytes32(x)	\
184 ({ \
185    grub_uint32_t _x = (x); \
186    (grub_uint32_t) ((_x << 24) \
187                     | ((_x & (grub_uint32_t) 0xFF00UL) << 8) \
188                     | ((_x & (grub_uint32_t) 0xFF0000UL) >> 8) \
189                     | (_x >> 24)); \
190 })
191 
192 #define grub_swap_bytes64(x)	\
193 ({ \
194    grub_uint64_t _x = (x); \
195    (grub_uint64_t) ((_x << 56) \
196                     | ((_x & (grub_uint64_t) 0xFF00ULL) << 40) \
197                     | ((_x & (grub_uint64_t) 0xFF0000ULL) << 24) \
198                     | ((_x & (grub_uint64_t) 0xFF000000ULL) << 8) \
199                     | ((_x & (grub_uint64_t) 0xFF00000000ULL) >> 8) \
200                     | ((_x & (grub_uint64_t) 0xFF0000000000ULL) >> 24) \
201                     | ((_x & (grub_uint64_t) 0xFF000000000000ULL) >> 40) \
202                     | (_x >> 56)); \
203 })
204 #endif					/* not gcc 4.3 or newer */
205 
206 #ifdef GRUB_CPU_WORDS_BIGENDIAN
207 # define grub_cpu_to_le16(x)	grub_swap_bytes16(x)
208 # define grub_cpu_to_le32(x)	grub_swap_bytes32(x)
209 # define grub_cpu_to_le64(x)	grub_swap_bytes64(x)
210 # define grub_le_to_cpu16(x)	grub_swap_bytes16(x)
211 # define grub_le_to_cpu32(x)	grub_swap_bytes32(x)
212 # define grub_le_to_cpu64(x)	grub_swap_bytes64(x)
213 # define grub_cpu_to_be16(x)	((grub_uint16_t) (x))
214 # define grub_cpu_to_be32(x)	((grub_uint32_t) (x))
215 # define grub_cpu_to_be64(x)	((grub_uint64_t) (x))
216 # define grub_be_to_cpu16(x)	((grub_uint16_t) (x))
217 # define grub_be_to_cpu32(x)	((grub_uint32_t) (x))
218 # define grub_be_to_cpu64(x)	((grub_uint64_t) (x))
219 # ifdef GRUB_TARGET_WORDS_BIGENDIAN
220 #  define grub_target_to_host16(x)	((grub_uint16_t) (x))
221 #  define grub_target_to_host32(x)	((grub_uint32_t) (x))
222 #  define grub_target_to_host64(x)	((grub_uint64_t) (x))
223 #  define grub_host_to_target16(x)	((grub_uint16_t) (x))
224 #  define grub_host_to_target32(x)	((grub_uint32_t) (x))
225 #  define grub_host_to_target64(x)	((grub_uint64_t) (x))
226 # else /* ! GRUB_TARGET_WORDS_BIGENDIAN */
227 #  define grub_target_to_host16(x)	grub_swap_bytes16(x)
228 #  define grub_target_to_host32(x)	grub_swap_bytes32(x)
229 #  define grub_target_to_host64(x)	grub_swap_bytes64(x)
230 #  define grub_host_to_target16(x)	grub_swap_bytes16(x)
231 #  define grub_host_to_target32(x)	grub_swap_bytes32(x)
232 #  define grub_host_to_target64(x)	grub_swap_bytes64(x)
233 # endif
234 #else /* ! WORDS_BIGENDIAN */
235 # define grub_cpu_to_le16(x)	((grub_uint16_t) (x))
236 # define grub_cpu_to_le32(x)	((grub_uint32_t) (x))
237 # define grub_cpu_to_le64(x)	((grub_uint64_t) (x))
238 # define grub_le_to_cpu16(x)	((grub_uint16_t) (x))
239 # define grub_le_to_cpu32(x)	((grub_uint32_t) (x))
240 # define grub_le_to_cpu64(x)	((grub_uint64_t) (x))
241 # define grub_cpu_to_be16(x)	grub_swap_bytes16(x)
242 # define grub_cpu_to_be32(x)	grub_swap_bytes32(x)
243 # define grub_cpu_to_be64(x)	grub_swap_bytes64(x)
244 # define grub_be_to_cpu16(x)	grub_swap_bytes16(x)
245 # define grub_be_to_cpu32(x)	grub_swap_bytes32(x)
246 # define grub_be_to_cpu64(x)	grub_swap_bytes64(x)
247 # ifdef GRUB_TARGET_WORDS_BIGENDIAN
248 #  define grub_target_to_host16(x)	grub_swap_bytes16(x)
249 #  define grub_target_to_host32(x)	grub_swap_bytes32(x)
250 #  define grub_target_to_host64(x)	grub_swap_bytes64(x)
251 #  define grub_host_to_target16(x)	grub_swap_bytes16(x)
252 #  define grub_host_to_target32(x)	grub_swap_bytes32(x)
253 #  define grub_host_to_target64(x)	grub_swap_bytes64(x)
254 # else /* ! GRUB_TARGET_WORDS_BIGENDIAN */
255 #  define grub_target_to_host16(x)	((grub_uint16_t) (x))
256 #  define grub_target_to_host32(x)	((grub_uint32_t) (x))
257 #  define grub_target_to_host64(x)	((grub_uint64_t) (x))
258 #  define grub_host_to_target16(x)	((grub_uint16_t) (x))
259 #  define grub_host_to_target32(x)	((grub_uint32_t) (x))
260 #  define grub_host_to_target64(x)	((grub_uint64_t) (x))
261 # endif
262 #endif /* ! WORDS_BIGENDIAN */
263 
264 #if GRUB_TARGET_SIZEOF_VOID_P == 8
265 #  define grub_host_to_target_addr(x) grub_host_to_target64(x)
266 #else
267 #  define grub_host_to_target_addr(x) grub_host_to_target32(x)
268 #endif
269 
270 #endif /* ! GRUB_TYPES_HEADER */
271