From 095522cceca4aede4b4a5e8cd74cbbd8f63e1116 Mon Sep 17 00:00:00 2001 From: Nico Huber Date: Fri, 1 Dec 2017 18:33:02 +0000 Subject: Move endianness definitions and provide it inside Makefile Add an `endiantest.c` similar to `archtest.c` to provide the endianness inside the Makefile. The __FLASHROM_(LITTLE|BIG)_ENDIAN__ definitions had to move from `hwaccess.h` into `platform.h`, therefor. This will be used to decide whether to build the internal programmer in a follow- up. Change-Id: I55dcf5a88da48f885cda9ad89ab87395d895a891 Signed-off-by: Nico Huber Reviewed-on: https://review.coreboot.org/22670 Tested-by: build bot (Jenkins) Reviewed-by: David Hendricks --- platform.h | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) (limited to 'platform.h') diff --git a/platform.h b/platform.h index b2fdcd01..e3b7674a 100644 --- a/platform.h +++ b/platform.h @@ -81,4 +81,86 @@ #error Unknown architecture #endif +/* The next big hunk tries to guess endianess from various preprocessor macros */ +/* First some error checking in case some weird header has defined both. + * NB: OpenBSD always defines _BIG_ENDIAN and _LITTLE_ENDIAN. */ +#if defined (__LITTLE_ENDIAN__) && defined (__BIG_ENDIAN__) +#error Conflicting endianness #define +#endif + +#if IS_X86 + +/* All x86 is little-endian. */ +#define __FLASHROM_LITTLE_ENDIAN__ 1 + +#elif IS_MIPS + +/* MIPS can be either endian. */ +#if defined (__MIPSEL) || defined (__MIPSEL__) || defined (_MIPSEL) || defined (MIPSEL) +#define __FLASHROM_LITTLE_ENDIAN__ 1 +#elif defined (__MIPSEB) || defined (__MIPSEB__) || defined (_MIPSEB) || defined (MIPSEB) +#define __FLASHROM_BIG_ENDIAN__ 1 +#endif + +#elif IS_PPC + +/* PowerPC can be either endian. */ +#if defined (_BIG_ENDIAN) || defined (__BIG_ENDIAN__) +#define __FLASHROM_BIG_ENDIAN__ 1 +#elif defined (_LITTLE_ENDIAN) || defined (__LITTLE_ENDIAN__) +#define __FLASHROM_LITTLE_ENDIAN__ 1 +#endif + +#elif IS_ARM + +/* ARM can be either endian. */ +#if defined (__ARMEB__) +#define __FLASHROM_BIG_ENDIAN__ 1 +#elif defined (__ARMEL__) +#define __FLASHROM_LITTLE_ENDIAN__ 1 +#endif + +#elif IS_SPARC +/* SPARC is big endian in general (but allows to access data in little endian too). */ +#define __FLASHROM_BIG_ENDIAN__ 1 + +#endif /* IS_? */ + +#if !defined (__FLASHROM_BIG_ENDIAN__) && !defined (__FLASHROM_LITTLE_ENDIAN__) + +/* If architecture-specific approaches fail try generic variants. First: BSD (works about everywhere). */ +#if !IS_WINDOWS +#include + +#if defined (__BYTE_ORDER) +#if __BYTE_ORDER == __LITTLE_ENDIAN +#define __FLASHROM_LITTLE_ENDIAN__ +#elif __BYTE_ORDER == __BIG_ENDIAN +#define __FLASHROM_BIG_ENDIAN__ +#else +#error Unknown byte order! +#endif +#endif /* defined __BYTE_ORDER */ +#endif /* !IS_WINDOWS */ + +#if !defined (__FLASHROM_BIG_ENDIAN__) && !defined (__FLASHROM_LITTLE_ENDIAN__) + +/* Nonstandard libc-specific macros for determining endianness. */ +/* musl provides an endian.h as well... but it can not be detected from within C. */ +#if defined(__GLIBC__) +#include +#if BYTE_ORDER == LITTLE_ENDIAN +#define __FLASHROM_LITTLE_ENDIAN__ 1 +#elif BYTE_ORDER == BIG_ENDIAN +#define __FLASHROM_BIG_ENDIAN__ 1 +#endif +#endif +#endif + +#endif + +#if !defined (__FLASHROM_BIG_ENDIAN__) && !defined (__FLASHROM_LITTLE_ENDIAN__) +#error Unable to determine endianness. +#endif + #endif /* !__PLATFORM_H__ */ -- cgit v1.2.3