types.h 10.2 KB
Newer Older
okuji's avatar
okuji committed
1
/*
2
 *  GRUB  --  GRand Unified Bootloader
3
 *  Copyright (C) 2002,2005,2006,2007,2008,2009  Free Software Foundation, Inc.
okuji's avatar
okuji committed
4
 *
5
 *  GRUB is free software: you can redistribute it and/or modify
okuji's avatar
okuji committed
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
okuji's avatar
okuji committed
8 9
 *  (at your option) any later version.
 *
10
 *  GRUB is distributed in the hope that it will be useful,
okuji's avatar
okuji committed
11 12 13 14 15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
16
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
okuji's avatar
okuji committed
17 18
 */

19 20
#ifndef GRUB_TYPES_HEADER
#define GRUB_TYPES_HEADER	1
okuji's avatar
okuji committed
21 22

#include <config.h>
23
#ifndef GRUB_UTIL
24
#include <grub/cpu/types.h>
25
#endif
okuji's avatar
okuji committed
26

27 28 29 30 31 32
#ifdef __MINGW32__
#define GRUB_PACKED __attribute__ ((packed,gcc_struct))
#else
#define GRUB_PACKED __attribute__ ((packed))
#endif

33 34 35 36 37 38 39 40 41
#ifdef GRUB_BUILD
# define GRUB_CPU_SIZEOF_VOID_P	BUILD_SIZEOF_VOID_P
# define GRUB_CPU_SIZEOF_LONG	BUILD_SIZEOF_LONG
# if BUILD_WORDS_BIGENDIAN
#  define GRUB_CPU_WORDS_BIGENDIAN	1
# else
#  undef GRUB_CPU_WORDS_BIGENDIAN
# endif
#elif defined (GRUB_UTIL)
42 43
# define GRUB_CPU_SIZEOF_VOID_P	SIZEOF_VOID_P
# define GRUB_CPU_SIZEOF_LONG	SIZEOF_LONG
okuji's avatar
okuji committed
44
# ifdef WORDS_BIGENDIAN
45
#  define GRUB_CPU_WORDS_BIGENDIAN	1
okuji's avatar
okuji committed
46
# else
47
#  undef GRUB_CPU_WORDS_BIGENDIAN
okuji's avatar
okuji committed
48
# endif
49
#else /* ! GRUB_UTIL */
50 51 52
# define GRUB_CPU_SIZEOF_VOID_P	GRUB_TARGET_SIZEOF_VOID_P
# define GRUB_CPU_SIZEOF_LONG	GRUB_TARGET_SIZEOF_LONG
# ifdef GRUB_TARGET_WORDS_BIGENDIAN
53
#  define GRUB_CPU_WORDS_BIGENDIAN	1
okuji's avatar
okuji committed
54
# else
55
#  undef GRUB_CPU_WORDS_BIGENDIAN
okuji's avatar
okuji committed
56
# endif
57
#endif /* ! GRUB_UTIL */
okuji's avatar
okuji committed
58

59
#if GRUB_CPU_SIZEOF_VOID_P != 4 && GRUB_CPU_SIZEOF_VOID_P != 8
okuji's avatar
okuji committed
60 61 62
# error "This architecture is not supported because sizeof(void *) != 4 and sizeof(void *) != 8"
#endif

63 64 65 66
#if GRUB_CPU_SIZEOF_LONG != 4 && GRUB_CPU_SIZEOF_LONG != 8
# error "This architecture is not supported because sizeof(long) != 4 and sizeof(long) != 8"
#endif

67
#if !defined (GRUB_UTIL) && !defined (GRUB_TARGET_WORDSIZE)
68 69 70 71 72 73 74
# if GRUB_TARGET_SIZEOF_VOID_P == 4
#  define GRUB_TARGET_WORDSIZE 32
# elif GRUB_TARGET_SIZEOF_VOID_P == 8
#  define GRUB_TARGET_WORDSIZE 64
# endif
#endif

okuji's avatar
okuji committed
75
/* Define various wide integers.  */
76 77 78
typedef signed char		grub_int8_t;
typedef short			grub_int16_t;
typedef int			grub_int32_t;
79
#if GRUB_CPU_SIZEOF_LONG == 8
80
typedef long			grub_int64_t;
okuji's avatar
okuji committed
81
#else
82
typedef long long		grub_int64_t;
okuji's avatar
okuji committed
83 84
#endif

85 86 87
typedef unsigned char		grub_uint8_t;
typedef unsigned short		grub_uint16_t;
typedef unsigned		grub_uint32_t;
88 89
# define PRIxGRUB_UINT32_T	"x"
# define PRIuGRUB_UINT32_T	"u"
90
#if GRUB_CPU_SIZEOF_LONG == 8
91
typedef unsigned long		grub_uint64_t;
92
# define PRIxGRUB_UINT64_T	"lx"
93
# define PRIuGRUB_UINT64_T	"lu"
okuji's avatar
okuji committed
94
#else
95
typedef unsigned long long	grub_uint64_t;
96
# define PRIxGRUB_UINT64_T	"llx"
97
# define PRIuGRUB_UINT64_T	"llu"
okuji's avatar
okuji committed
98 99 100
#endif

/* Misc types.  */
101 102

#if GRUB_CPU_SIZEOF_VOID_P == 8
103 104 105
typedef grub_uint64_t	grub_addr_t;
typedef grub_uint64_t	grub_size_t;
typedef grub_int64_t	grub_ssize_t;
106

107 108
# define GRUB_SIZE_MAX 18446744073709551615UL

109
# if GRUB_CPU_SIZEOF_LONG == 8
110 111 112 113
#  define PRIxGRUB_SIZE	 "lx"
#  define PRIxGRUB_ADDR	 "lx"
#  define PRIuGRUB_SIZE	 "lu"
#  define PRIdGRUB_SSIZE "ld"
114
# else
115 116 117 118
#  define PRIxGRUB_SIZE	 "llx"
#  define PRIxGRUB_ADDR	 "llx"
#  define PRIuGRUB_SIZE  "llu"
#  define PRIdGRUB_SSIZE "lld"
119
# endif
okuji's avatar
okuji committed
120
#else
121 122 123
typedef grub_uint32_t	grub_addr_t;
typedef grub_uint32_t	grub_size_t;
typedef grub_int32_t	grub_ssize_t;
124

125 126
# define GRUB_SIZE_MAX 4294967295UL

127
# define PRIxGRUB_SIZE	"x"
128
# define PRIxGRUB_ADDR	"x"
129
# define PRIuGRUB_SIZE	"u"
130
# define PRIdGRUB_SSIZE	"d"
okuji's avatar
okuji committed
131 132
#endif

133 134
#define GRUB_UCHAR_MAX 0xFF
#define GRUB_USHRT_MAX 65535
135
#define GRUB_SHRT_MAX 0x7fff
136
#define GRUB_UINT_MAX 4294967295U
137
#define GRUB_INT_MAX 0x7fffffff
138 139
#define GRUB_INT32_MIN (-2147483647 - 1)
#define GRUB_INT32_MAX 2147483647
140

141
#if GRUB_CPU_SIZEOF_LONG == 8
142
# define GRUB_ULONG_MAX 18446744073709551615UL
143 144
# define GRUB_LONG_MAX 9223372036854775807L
# define GRUB_LONG_MIN (-9223372036854775807L - 1)
145
#else
146
# define GRUB_ULONG_MAX 4294967295UL
147 148
# define GRUB_LONG_MAX 2147483647L
# define GRUB_LONG_MIN (-2147483647L - 1)
149 150
#endif

151 152
typedef grub_uint64_t grub_properly_aligned_t;

153
#define GRUB_PROPERLY_ALIGNED_ARRAY(name, size) grub_properly_aligned_t name[((size) + sizeof (grub_properly_aligned_t) - 1) / sizeof (grub_properly_aligned_t)]
154

155 156 157 158 159
/* The type for representing a file offset.  */
typedef grub_uint64_t	grub_off_t;

/* The type for representing a disk block address.  */
typedef grub_uint64_t	grub_disk_addr_t;
160

okuji's avatar
okuji committed
161
/* Byte-orders.  */
162 163 164 165
static inline grub_uint16_t grub_swap_bytes16(grub_uint16_t _x)
{
   return (grub_uint16_t) ((_x << 8) | (_x >> 8));
}
okuji's avatar
okuji committed
166

167 168
#define grub_swap_bytes16_compile_time(x) ((((x) & 0xff) << 8) | (((x) & 0xff00) >> 8))
#define grub_swap_bytes32_compile_time(x) ((((x) & 0xff) << 24) | (((x) & 0xff00) << 8) | (((x) & 0xff0000) >> 8) | (((x) & 0xff000000UL) >> 24))
169 170 171 172 173 174 175 176 177 178 179 180
#define grub_swap_bytes64_compile_time(x)	\
({ \
   grub_uint64_t _x = (x); \
   (grub_uint64_t) ((_x << 56) \
                    | ((_x & (grub_uint64_t) 0xFF00ULL) << 40) \
                    | ((_x & (grub_uint64_t) 0xFF0000ULL) << 24) \
                    | ((_x & (grub_uint64_t) 0xFF000000ULL) << 8) \
                    | ((_x & (grub_uint64_t) 0xFF00000000ULL) >> 8) \
                    | ((_x & (grub_uint64_t) 0xFF0000000000ULL) >> 24) \
                    | ((_x & (grub_uint64_t) 0xFF000000000000ULL) >> 40) \
                    | (_x >> 56)); \
})
181

182
#if (defined(__GNUC__) && (__GNUC__ > 3) && (__GNUC__ > 4 || __GNUC_MINOR__ >= 3)) || defined(__clang__)
183 184 185 186 187 188 189 190 191 192
static inline grub_uint32_t grub_swap_bytes32(grub_uint32_t x)
{
	return __builtin_bswap32(x);
}

static inline grub_uint64_t grub_swap_bytes64(grub_uint64_t x)
{
	return __builtin_bswap64(x);
}
#else					/* not gcc 4.3 or newer */
193 194 195 196 197 198 199
static inline grub_uint32_t grub_swap_bytes32(grub_uint32_t _x)
{
   return ((_x << 24)
	   | ((_x & (grub_uint32_t) 0xFF00UL) << 8)
	   | ((_x & (grub_uint32_t) 0xFF0000UL) >> 8)
	   | (_x >> 24));
}
okuji's avatar
okuji committed
200

201 202 203 204 205 206 207 208 209 210 211
static inline grub_uint64_t grub_swap_bytes64(grub_uint64_t _x)
{
   return ((_x << 56)
	   | ((_x & (grub_uint64_t) 0xFF00ULL) << 40)
	   | ((_x & (grub_uint64_t) 0xFF0000ULL) << 24)
	   | ((_x & (grub_uint64_t) 0xFF000000ULL) << 8)
	   | ((_x & (grub_uint64_t) 0xFF00000000ULL) >> 8)
	   | ((_x & (grub_uint64_t) 0xFF0000000000ULL) >> 24)
	   | ((_x & (grub_uint64_t) 0xFF000000000000ULL) >> 40)
	   | (_x >> 56));
}
212
#endif					/* not gcc 4.3 or newer */
okuji's avatar
okuji committed
213

214 215 216 217 218 219 220 221 222 223 224 225 226
#ifdef GRUB_CPU_WORDS_BIGENDIAN
# define grub_cpu_to_le16(x)	grub_swap_bytes16(x)
# define grub_cpu_to_le32(x)	grub_swap_bytes32(x)
# define grub_cpu_to_le64(x)	grub_swap_bytes64(x)
# define grub_le_to_cpu16(x)	grub_swap_bytes16(x)
# define grub_le_to_cpu32(x)	grub_swap_bytes32(x)
# define grub_le_to_cpu64(x)	grub_swap_bytes64(x)
# define grub_cpu_to_be16(x)	((grub_uint16_t) (x))
# define grub_cpu_to_be32(x)	((grub_uint32_t) (x))
# define grub_cpu_to_be64(x)	((grub_uint64_t) (x))
# define grub_be_to_cpu16(x)	((grub_uint16_t) (x))
# define grub_be_to_cpu32(x)	((grub_uint32_t) (x))
# define grub_be_to_cpu64(x)	((grub_uint64_t) (x))
227
# define grub_cpu_to_be16_compile_time(x)	((grub_uint16_t) (x))
228 229 230
# define grub_cpu_to_be32_compile_time(x)	((grub_uint32_t) (x))
# define grub_cpu_to_be64_compile_time(x)	((grub_uint64_t) (x))
# define grub_be_to_cpu64_compile_time(x)	((grub_uint64_t) (x))
231
# define grub_cpu_to_le32_compile_time(x)	grub_swap_bytes32_compile_time(x)
232
# define grub_cpu_to_le64_compile_time(x)	grub_swap_bytes64_compile_time(x)
233
# define grub_cpu_to_le16_compile_time(x)	grub_swap_bytes16_compile_time(x)
okuji's avatar
okuji committed
234
#else /* ! WORDS_BIGENDIAN */
235 236 237 238 239 240 241 242 243 244 245 246
# define grub_cpu_to_le16(x)	((grub_uint16_t) (x))
# define grub_cpu_to_le32(x)	((grub_uint32_t) (x))
# define grub_cpu_to_le64(x)	((grub_uint64_t) (x))
# define grub_le_to_cpu16(x)	((grub_uint16_t) (x))
# define grub_le_to_cpu32(x)	((grub_uint32_t) (x))
# define grub_le_to_cpu64(x)	((grub_uint64_t) (x))
# define grub_cpu_to_be16(x)	grub_swap_bytes16(x)
# define grub_cpu_to_be32(x)	grub_swap_bytes32(x)
# define grub_cpu_to_be64(x)	grub_swap_bytes64(x)
# define grub_be_to_cpu16(x)	grub_swap_bytes16(x)
# define grub_be_to_cpu32(x)	grub_swap_bytes32(x)
# define grub_be_to_cpu64(x)	grub_swap_bytes64(x)
247
# define grub_cpu_to_be16_compile_time(x)	grub_swap_bytes16_compile_time(x)
248 249 250
# define grub_cpu_to_be32_compile_time(x)	grub_swap_bytes32_compile_time(x)
# define grub_cpu_to_be64_compile_time(x)	grub_swap_bytes64_compile_time(x)
# define grub_be_to_cpu64_compile_time(x)	grub_swap_bytes64_compile_time(x)
251 252
# define grub_cpu_to_le16_compile_time(x)	((grub_uint16_t) (x))
# define grub_cpu_to_le32_compile_time(x)	((grub_uint32_t) (x))
253
# define grub_cpu_to_le64_compile_time(x)	((grub_uint64_t) (x))
254

okuji's avatar
okuji committed
255 256
#endif /* ! WORDS_BIGENDIAN */

257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
struct grub_unaligned_uint16
{
  grub_uint16_t val;
} GRUB_PACKED;
struct grub_unaligned_uint32
{
  grub_uint32_t val;
} GRUB_PACKED;
struct grub_unaligned_uint64
{
  grub_uint64_t val;
} GRUB_PACKED;

typedef struct grub_unaligned_uint16 grub_unaligned_uint16_t;
typedef struct grub_unaligned_uint32 grub_unaligned_uint32_t;
typedef struct grub_unaligned_uint64 grub_unaligned_uint64_t;

274 275
static inline grub_uint16_t grub_get_unaligned16 (const void *ptr)
{
276 277 278
  const struct grub_unaligned_uint16 *dd
    = (const struct grub_unaligned_uint16 *) ptr;
  return dd->val;
279 280 281 282
}

static inline void grub_set_unaligned16 (void *ptr, grub_uint16_t val)
{
283 284
  struct grub_unaligned_uint16 *dd = (struct grub_unaligned_uint16 *) ptr;
  dd->val = val;
285 286 287
}

static inline grub_uint32_t grub_get_unaligned32 (const void *ptr)
288
{
289 290 291
  const struct grub_unaligned_uint32 *dd
    = (const struct grub_unaligned_uint32 *) ptr;
  return dd->val;
292 293
}

294
static inline void grub_set_unaligned32 (void *ptr, grub_uint32_t val)
295
{
296 297
  struct grub_unaligned_uint32 *dd = (struct grub_unaligned_uint32 *) ptr;
  dd->val = val;
298 299
}

300
static inline grub_uint64_t grub_get_unaligned64 (const void *ptr)
301
{
302 303 304
  const struct grub_unaligned_uint64 *dd
    = (const struct grub_unaligned_uint64 *) ptr;
  return dd->val;
305 306
}

307 308 309 310 311
static inline void grub_set_unaligned64 (void *ptr, grub_uint64_t val)
{
  struct grub_unaligned_uint64_t
  {
    grub_uint64_t d;
312
  } GRUB_PACKED;
313 314 315 316
  struct grub_unaligned_uint64_t *dd = (struct grub_unaligned_uint64_t *) ptr;
  dd->d = val;
}

317 318
#define GRUB_CHAR_BIT 8

319
#endif /* ! GRUB_TYPES_HEADER */