/* * VAS_EBOOT -- GRand Unified Bootloader * Copyright (C) 2002,2005,2006,2007,2008,2009 Free Software Foundation, Inc. * * VAS_EBOOT is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * VAS_EBOOT is distributed in the hope that it will be useful, * 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 * along with VAS_EBOOT. If not, see . */ #ifndef VAS_EBOOT_TYPES_HEADER #define VAS_EBOOT_TYPES_HEADER 1 #include #include #ifndef VAS_EBOOT_UTIL #include #endif #ifdef __MINGW32__ #define VAS_EBOOT_PACKED __attribute__ ((packed,gcc_struct)) #else #define VAS_EBOOT_PACKED __attribute__ ((packed)) #endif #ifdef VAS_EBOOT_BUILD # define VAS_EBOOT_CPU_SIZEOF_VOID_P BUILD_SIZEOF_VOID_P # define VAS_EBOOT_CPU_SIZEOF_LONG BUILD_SIZEOF_LONG # if BUILD_WORDS_BIGENDIAN # define VAS_EBOOT_CPU_WORDS_BIGENDIAN 1 # else # undef VAS_EBOOT_CPU_WORDS_BIGENDIAN # endif #elif defined (VAS_EBOOT_UTIL) # define VAS_EBOOT_CPU_SIZEOF_VOID_P SIZEOF_VOID_P # define VAS_EBOOT_CPU_SIZEOF_LONG SIZEOF_LONG # ifdef WORDS_BIGENDIAN # define VAS_EBOOT_CPU_WORDS_BIGENDIAN 1 # else # undef VAS_EBOOT_CPU_WORDS_BIGENDIAN # endif #else /* ! VAS_EBOOT_UTIL */ # define VAS_EBOOT_CPU_SIZEOF_VOID_P VAS_EBOOT_TARGET_SIZEOF_VOID_P # define VAS_EBOOT_CPU_SIZEOF_LONG VAS_EBOOT_TARGET_SIZEOF_LONG # ifdef VAS_EBOOT_TARGET_WORDS_BIGENDIAN # define VAS_EBOOT_CPU_WORDS_BIGENDIAN 1 # else # undef VAS_EBOOT_CPU_WORDS_BIGENDIAN # endif #endif /* ! VAS_EBOOT_UTIL */ #if VAS_EBOOT_CPU_SIZEOF_VOID_P != 4 && VAS_EBOOT_CPU_SIZEOF_VOID_P != 8 # error "This architecture is not supported because sizeof(void *) != 4 and sizeof(void *) != 8" #endif #if VAS_EBOOT_CPU_SIZEOF_LONG != 4 && VAS_EBOOT_CPU_SIZEOF_LONG != 8 # error "This architecture is not supported because sizeof(long) != 4 and sizeof(long) != 8" #endif #if !defined (VAS_EBOOT_UTIL) && !defined (VAS_EBOOT_TARGET_WORDSIZE) # if VAS_EBOOT_TARGET_SIZEOF_VOID_P == 4 # define VAS_EBOOT_TARGET_WORDSIZE 32 # elif VAS_EBOOT_TARGET_SIZEOF_VOID_P == 8 # define VAS_EBOOT_TARGET_WORDSIZE 64 # endif #endif #ifndef __CHAR_BIT__ # error __CHAR_BIT__ is not defined #elif __CHAR_BIT__ != 8 # error __CHAR_BIT__ is not equal 8 #else # define VAS_EBOOT_CHAR_BIT __CHAR_BIT__ #endif #define VAS_EBOOT_TYPE_BITS(type) (sizeof(type) * VAS_EBOOT_CHAR_BIT) /* Define various wide integers. */ typedef signed char VasEBoot_int8_t; typedef short VasEBoot_int16_t; typedef int VasEBoot_int32_t; # define PRIxVAS_EBOOT_INT32_T "x" # define PRIdVAS_EBOOT_INT32_T "d" #if VAS_EBOOT_CPU_SIZEOF_LONG == 8 typedef long VasEBoot_int64_t; # define PRIxVAS_EBOOT_INT64_T "lx" # define PRIdVAS_EBOOT_INT64_T "ld" #else typedef long long VasEBoot_int64_t; # define PRIxVAS_EBOOT_INT64_T "llx" # define PRIdVAS_EBOOT_INT64_T "lld" #endif typedef unsigned char VasEBoot_uint8_t; typedef unsigned short VasEBoot_uint16_t; typedef unsigned VasEBoot_uint32_t; # define PRIxVAS_EBOOT_UINT32_T "x" # define PRIuVAS_EBOOT_UINT32_T "u" #if VAS_EBOOT_CPU_SIZEOF_LONG == 8 typedef unsigned long VasEBoot_uint64_t; # define PRIxVAS_EBOOT_UINT64_T "lx" # define PRIuVAS_EBOOT_UINT64_T "lu" #else typedef unsigned long long VasEBoot_uint64_t; # define PRIxVAS_EBOOT_UINT64_T "llx" # define PRIuVAS_EBOOT_UINT64_T "llu" #endif /* Misc types. */ #if VAS_EBOOT_CPU_SIZEOF_VOID_P == 8 typedef VasEBoot_uint64_t VasEBoot_addr_t; typedef VasEBoot_uint64_t VasEBoot_size_t; typedef VasEBoot_int64_t VasEBoot_ssize_t; # define VAS_EBOOT_SIZE_MAX 18446744073709551615UL # define VAS_EBOOT_SSIZE_MAX 9223372036854775807L # if VAS_EBOOT_CPU_SIZEOF_LONG == 8 # define PRIxVAS_EBOOT_SIZE "lx" # define PRIxVAS_EBOOT_ADDR "lx" # define PRIuVAS_EBOOT_SIZE "lu" # define PRIdVAS_EBOOT_SSIZE "ld" # else # define PRIxVAS_EBOOT_SIZE "llx" # define PRIxVAS_EBOOT_ADDR "llx" # define PRIuVAS_EBOOT_SIZE "llu" # define PRIdVAS_EBOOT_SSIZE "lld" # endif #else typedef VasEBoot_uint32_t VasEBoot_addr_t; typedef VasEBoot_uint32_t VasEBoot_size_t; typedef VasEBoot_int32_t VasEBoot_ssize_t; # define VAS_EBOOT_SIZE_MAX 4294967295UL # define VAS_EBOOT_SSIZE_MAX 2147483647L # define PRIxVAS_EBOOT_SIZE "x" # define PRIxVAS_EBOOT_ADDR "x" # define PRIuVAS_EBOOT_SIZE "u" # define PRIdVAS_EBOOT_SSIZE "d" #endif #define VAS_EBOOT_SCHAR_MAX 127 #define VAS_EBOOT_SCHAR_MIN (-VAS_EBOOT_SCHAR_MAX - 1) #define VAS_EBOOT_UCHAR_MAX 0xFF #define VAS_EBOOT_USHRT_MAX 65535 #define VAS_EBOOT_SHRT_MAX 0x7fff #define VAS_EBOOT_SHRT_MIN (-VAS_EBOOT_SHRT_MAX - 1) #define VAS_EBOOT_UINT_MAX 4294967295U #define VAS_EBOOT_INT_MAX 0x7fffffff #define VAS_EBOOT_INT_MIN (-VAS_EBOOT_INT_MAX - 1) #define VAS_EBOOT_INT32_MAX 2147483647 #define VAS_EBOOT_INT32_MIN (-VAS_EBOOT_INT32_MAX - 1) #if VAS_EBOOT_CPU_SIZEOF_LONG == 8 # define VAS_EBOOT_ULONG_MAX 18446744073709551615UL # define VAS_EBOOT_LONG_MAX 9223372036854775807L #else # define VAS_EBOOT_ULONG_MAX 4294967295UL # define VAS_EBOOT_LONG_MAX 2147483647L #endif # define VAS_EBOOT_LONG_MIN (-VAS_EBOOT_LONG_MAX - 1) /* * Cast to unsigned long long so that the "return value" is always a consistent * type and to ensure an unsigned value regardless of type parameter. */ #define VAS_EBOOT_TYPE_U_MAX(type) ((unsigned long long)((typeof (type))(~0))) #define VAS_EBOOT_TYPE_U_MIN(type) 0ULL typedef VasEBoot_uint64_t VasEBoot_properly_aligned_t; #define VAS_EBOOT_PROPERLY_ALIGNED_ARRAY(name, size) VasEBoot_properly_aligned_t name[((size) + sizeof (VasEBoot_properly_aligned_t) - 1) / sizeof (VasEBoot_properly_aligned_t)] /* The type for representing a file offset. */ typedef VasEBoot_uint64_t VasEBoot_off_t; #define PRIxVAS_EBOOT_OFFSET PRIxVAS_EBOOT_UINT64_T #define PRIuVAS_EBOOT_OFFSET PRIuVAS_EBOOT_UINT64_T /* The type for representing a disk block address. */ typedef VasEBoot_uint64_t VasEBoot_disk_addr_t; #define PRIxVAS_EBOOT_DISK_ADDR PRIxVAS_EBOOT_UINT64_T /* Byte-orders. */ static inline VasEBoot_uint16_t VasEBoot_swap_bytes16(VasEBoot_uint16_t _x) { return (VasEBoot_uint16_t) ((_x << 8) | (_x >> 8)); } #define VasEBoot_swap_bytes16_compile_time(x) ((VasEBoot_uint16_t)((((x) & 0xff) << 8) | (((x) & 0xff00) >> 8))) #define VasEBoot_swap_bytes32_compile_time(x) ((VasEBoot_uint32_t)((((x) & 0xff) << 24) | (((x) & 0xff00) << 8) | (((x) & 0xff0000) >> 8) | (((x) & 0xff000000UL) >> 24))) #define VasEBoot_swap_bytes64_compile_time(x) \ ({ \ VasEBoot_uint64_t _x = (x); \ (VasEBoot_uint64_t) ((_x << 56) \ | ((_x & (VasEBoot_uint64_t) 0xFF00ULL) << 40) \ | ((_x & (VasEBoot_uint64_t) 0xFF0000ULL) << 24) \ | ((_x & (VasEBoot_uint64_t) 0xFF000000ULL) << 8) \ | ((_x & (VasEBoot_uint64_t) 0xFF00000000ULL) >> 8) \ | ((_x & (VasEBoot_uint64_t) 0xFF0000000000ULL) >> 24) \ | ((_x & (VasEBoot_uint64_t) 0xFF000000000000ULL) >> 40) \ | (_x >> 56)); \ }) #if (defined(__GNUC__) && (__GNUC__ > 3) && (__GNUC__ > 4 || __GNUC_MINOR__ >= 3)) || defined(__clang__) static inline VasEBoot_uint32_t VasEBoot_swap_bytes32(VasEBoot_uint32_t x) { return __builtin_bswap32(x); } static inline VasEBoot_uint64_t VasEBoot_swap_bytes64(VasEBoot_uint64_t x) { return __builtin_bswap64(x); } #else /* not gcc 4.3 or newer */ static inline VasEBoot_uint32_t VasEBoot_swap_bytes32(VasEBoot_uint32_t _x) { return ((_x << 24) | ((_x & (VasEBoot_uint32_t) 0xFF00UL) << 8) | ((_x & (VasEBoot_uint32_t) 0xFF0000UL) >> 8) | (_x >> 24)); } static inline VasEBoot_uint64_t VasEBoot_swap_bytes64(VasEBoot_uint64_t _x) { return ((_x << 56) | ((_x & (VasEBoot_uint64_t) 0xFF00ULL) << 40) | ((_x & (VasEBoot_uint64_t) 0xFF0000ULL) << 24) | ((_x & (VasEBoot_uint64_t) 0xFF000000ULL) << 8) | ((_x & (VasEBoot_uint64_t) 0xFF00000000ULL) >> 8) | ((_x & (VasEBoot_uint64_t) 0xFF0000000000ULL) >> 24) | ((_x & (VasEBoot_uint64_t) 0xFF000000000000ULL) >> 40) | (_x >> 56)); } #endif /* not gcc 4.3 or newer */ #ifdef VAS_EBOOT_CPU_WORDS_BIGENDIAN # define VasEBoot_cpu_to_le16(x) VasEBoot_swap_bytes16(x) # define VasEBoot_cpu_to_le32(x) VasEBoot_swap_bytes32(x) # define VasEBoot_cpu_to_le64(x) VasEBoot_swap_bytes64(x) # define VasEBoot_le_to_cpu16(x) VasEBoot_swap_bytes16(x) # define VasEBoot_le_to_cpu32(x) VasEBoot_swap_bytes32(x) # define VasEBoot_le_to_cpu64(x) VasEBoot_swap_bytes64(x) # define VasEBoot_cpu_to_be16(x) ((VasEBoot_uint16_t) (x)) # define VasEBoot_cpu_to_be32(x) ((VasEBoot_uint32_t) (x)) # define VasEBoot_cpu_to_be64(x) ((VasEBoot_uint64_t) (x)) # define VasEBoot_be_to_cpu16(x) ((VasEBoot_uint16_t) (x)) # define VasEBoot_be_to_cpu32(x) ((VasEBoot_uint32_t) (x)) # define VasEBoot_be_to_cpu64(x) ((VasEBoot_uint64_t) (x)) # define VasEBoot_cpu_to_be16_compile_time(x) ((VasEBoot_uint16_t) (x)) # define VasEBoot_cpu_to_be32_compile_time(x) ((VasEBoot_uint32_t) (x)) # define VasEBoot_cpu_to_be64_compile_time(x) ((VasEBoot_uint64_t) (x)) # define VasEBoot_be_to_cpu64_compile_time(x) ((VasEBoot_uint64_t) (x)) # define VasEBoot_cpu_to_le32_compile_time(x) VasEBoot_swap_bytes32_compile_time(x) # define VasEBoot_cpu_to_le64_compile_time(x) VasEBoot_swap_bytes64_compile_time(x) # define VasEBoot_cpu_to_le16_compile_time(x) VasEBoot_swap_bytes16_compile_time(x) #else /* ! WORDS_BIGENDIAN */ # define VasEBoot_cpu_to_le16(x) ((VasEBoot_uint16_t) (x)) # define VasEBoot_cpu_to_le32(x) ((VasEBoot_uint32_t) (x)) # define VasEBoot_cpu_to_le64(x) ((VasEBoot_uint64_t) (x)) # define VasEBoot_le_to_cpu16(x) ((VasEBoot_uint16_t) (x)) # define VasEBoot_le_to_cpu32(x) ((VasEBoot_uint32_t) (x)) # define VasEBoot_le_to_cpu64(x) ((VasEBoot_uint64_t) (x)) # define VasEBoot_cpu_to_be16(x) VasEBoot_swap_bytes16(x) # define VasEBoot_cpu_to_be32(x) VasEBoot_swap_bytes32(x) # define VasEBoot_cpu_to_be64(x) VasEBoot_swap_bytes64(x) # define VasEBoot_be_to_cpu16(x) VasEBoot_swap_bytes16(x) # define VasEBoot_be_to_cpu32(x) VasEBoot_swap_bytes32(x) # define VasEBoot_be_to_cpu64(x) VasEBoot_swap_bytes64(x) # define VasEBoot_cpu_to_be16_compile_time(x) VasEBoot_swap_bytes16_compile_time(x) # define VasEBoot_cpu_to_be32_compile_time(x) VasEBoot_swap_bytes32_compile_time(x) # define VasEBoot_cpu_to_be64_compile_time(x) VasEBoot_swap_bytes64_compile_time(x) # define VasEBoot_be_to_cpu64_compile_time(x) VasEBoot_swap_bytes64_compile_time(x) # define VasEBoot_cpu_to_le16_compile_time(x) ((VasEBoot_uint16_t) (x)) # define VasEBoot_cpu_to_le32_compile_time(x) ((VasEBoot_uint32_t) (x)) # define VasEBoot_cpu_to_le64_compile_time(x) ((VasEBoot_uint64_t) (x)) #endif /* ! WORDS_BIGENDIAN */ struct VasEBoot_unaligned_uint16 { VasEBoot_uint16_t val; } VAS_EBOOT_PACKED; struct VasEBoot_unaligned_uint32 { VasEBoot_uint32_t val; } VAS_EBOOT_PACKED; struct VasEBoot_unaligned_uint64 { VasEBoot_uint64_t val; } VAS_EBOOT_PACKED; typedef struct VasEBoot_unaligned_uint16 VasEBoot_unaligned_uint16_t; typedef struct VasEBoot_unaligned_uint32 VasEBoot_unaligned_uint32_t; typedef struct VasEBoot_unaligned_uint64 VasEBoot_unaligned_uint64_t; static inline VasEBoot_uint16_t VasEBoot_get_unaligned16 (const void *ptr) { const struct VasEBoot_unaligned_uint16 *dd = (const struct VasEBoot_unaligned_uint16 *) ptr; return dd->val; } static inline void VasEBoot_set_unaligned16 (void *ptr, VasEBoot_uint16_t val) { struct VasEBoot_unaligned_uint16 *dd = (struct VasEBoot_unaligned_uint16 *) ptr; dd->val = val; } static inline VasEBoot_uint32_t VasEBoot_get_unaligned32 (const void *ptr) { const struct VasEBoot_unaligned_uint32 *dd = (const struct VasEBoot_unaligned_uint32 *) ptr; return dd->val; } static inline void VasEBoot_set_unaligned32 (void *ptr, VasEBoot_uint32_t val) { struct VasEBoot_unaligned_uint32 *dd = (struct VasEBoot_unaligned_uint32 *) ptr; dd->val = val; } static inline VasEBoot_uint64_t VasEBoot_get_unaligned64 (const void *ptr) { const struct VasEBoot_unaligned_uint64 *dd = (const struct VasEBoot_unaligned_uint64 *) ptr; return dd->val; } static inline void VasEBoot_set_unaligned64 (void *ptr, VasEBoot_uint64_t val) { struct VasEBoot_unaligned_uint64_t { VasEBoot_uint64_t d; } VAS_EBOOT_PACKED; struct VasEBoot_unaligned_uint64_t *dd = (struct VasEBoot_unaligned_uint64_t *) ptr; dd->d = val; } /* * The VasEBoot_absolute_pointer() macro borrows the idea from Linux kernel of using * RELOC_HIDE() macro to stop GCC from checking the result of pointer arithmetic * and also its conversion to be inside the symbol's boundary [1]. The check * is sometimes false positive, especially it is controversial to emit the array * bounds [-Warray-bounds] warning on all hardwired literal pointers since GCC * 11/12 [2]. Unless a good solution can be settled, for the time being we * would be in favor of the macro instead of GCC pragmas which cannot match the * places the warning needs to be ignored in an exact way. * * [1] https://lists.linuxcoding.com/kernel/2006-q3/msg17979.html * [2] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99578 */ #if defined(__GNUC__) # define VasEBoot_absolute_pointer(val) \ ({ \ VasEBoot_addr_t __ptr; \ asm ("" : "=r" (__ptr) : "0" ((void *) (val))); \ (void *) (__ptr); \ }) #else # define VasEBoot_absolute_pointer(val) ((void *) (val)) #endif struct VasEBoot_guid { VasEBoot_uint32_t data1; VasEBoot_uint16_t data2; VasEBoot_uint16_t data3; VasEBoot_uint8_t data4[8]; } __attribute__ ((aligned(4))); typedef struct VasEBoot_guid VasEBoot_guid_t; #define VAS_EBOOT_GUID_SIZE (sizeof (VasEBoot_guid_t)) struct VasEBoot_packed_guid { VasEBoot_uint32_t data1; VasEBoot_uint16_t data2; VasEBoot_uint16_t data3; VasEBoot_uint8_t data4[8]; } VAS_EBOOT_PACKED; typedef struct VasEBoot_packed_guid VasEBoot_packed_guid_t; #define VAS_EBOOT_PACKED_GUID_SIZE (sizeof (VasEBoot_packed_guid_t)) #endif /* ! VAS_EBOOT_TYPES_HEADER */