blob: e3b7674ae24edf452f6651fb08ab0b4c26eb315a [file] [log] [blame]
Stefan Taunerb0eee9b2015-01-10 09:32:50 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2011 Carl-Daniel Hailfinger
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program 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 this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20/*
21 * Header file to determine target OS and CPU architecture.
22 */
23
24#ifndef __PLATFORM_H__
25#define __PLATFORM_H__ 1
26
27// Helper defines for operating systems
Patrick Georgid2a03b32017-03-13 13:48:03 +010028#if defined(__gnu_linux__) || defined(__linux__)
29#define IS_LINUX 1
30#else
31#define IS_LINUX 0
32#endif
33#if defined(__APPLE__) && defined(__MACH__) /* yes, both. */
34#define IS_MACOSX 1
35#else
36#define IS_MACOSX 0
37#endif
38#if defined(_WIN32) || defined(_WIN64) || defined(__WIN32__) || defined(__WINDOWS__)
39#define IS_WINDOWS 1
40#else
41#define IS_WINDOWS 0
42#endif
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000043
44// Likewise for target architectures
45#if defined (__i386__) || defined (__x86_64__) || defined(__amd64__)
46 #define __FLASHROM_ARCH__ "x86"
47 #define IS_X86 1
48#elif defined (__mips) || defined (__mips__) || defined (__MIPS__) || defined (mips)
49 #define __FLASHROM_ARCH__ "mips"
50 #define IS_MIPS 1
51#elif defined(__powerpc) || defined(__powerpc__) || defined(__powerpc64__) || defined(__POWERPC__) || \
52 defined(__ppc__) || defined(__ppc64__) || defined(_M_PPC) || defined(_ARCH_PPC) || \
53 defined(_ARCH_PPC64) || defined(__ppc)
54 #define __FLASHROM_ARCH__ "ppc"
55 #define IS_PPC 1
56#elif defined(__arm__) || defined(__TARGET_ARCH_ARM) || defined(_ARM) || defined(_M_ARM) || defined(__arm) || \
57 defined(__aarch64__)
58 #define __FLASHROM_ARCH__ "arm"
59 #define IS_ARM 1
Stefan Taunerfb2d77c2015-02-10 08:03:10 +000060#elif defined (__sparc__) || defined (__sparc)
61 #define __FLASHROM_ARCH__ "sparc"
62 #define IS_SPARC 1
Carl-Daniel Hailfinger8d0d53f2016-02-25 20:10:26 +000063#elif defined (__alpha__)
64 #define __FLASHROM_ARCH__ "alpha"
65 #define IS_ALPHA 1
66#elif defined (__hppa__) || defined (__hppa)
67 #define __FLASHROM_ARCH__ "hppa"
68 #define IS_HPPA 1
69#elif defined (__m68k__)
70 #define __FLASHROM_ARCH__ "m68k"
71 #define IS_M68K 1
72#elif defined (__sh__)
73 #define __FLASHROM_ARCH__ "sh"
74 #define IS_SH 1
75#elif defined(__s390__) || defined(__s390x__) || defined(__zarch__)
76 #define __FLASHROM_ARCH__ "s390"
77 #define IS_S390 1
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000078#endif
79
Carl-Daniel Hailfinger8d0d53f2016-02-25 20:10:26 +000080#if !(IS_X86 || IS_MIPS || IS_PPC || IS_ARM || IS_SPARC || IS_ALPHA || IS_HPPA || IS_M68K || IS_SH || IS_S390)
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000081#error Unknown architecture
82#endif
83
Nico Huber095522c2017-12-01 18:33:02 +000084/* The next big hunk tries to guess endianess from various preprocessor macros */
85/* First some error checking in case some weird header has defined both.
86 * NB: OpenBSD always defines _BIG_ENDIAN and _LITTLE_ENDIAN. */
87#if defined (__LITTLE_ENDIAN__) && defined (__BIG_ENDIAN__)
88#error Conflicting endianness #define
89#endif
90
91#if IS_X86
92
93/* All x86 is little-endian. */
94#define __FLASHROM_LITTLE_ENDIAN__ 1
95
96#elif IS_MIPS
97
98/* MIPS can be either endian. */
99#if defined (__MIPSEL) || defined (__MIPSEL__) || defined (_MIPSEL) || defined (MIPSEL)
100#define __FLASHROM_LITTLE_ENDIAN__ 1
101#elif defined (__MIPSEB) || defined (__MIPSEB__) || defined (_MIPSEB) || defined (MIPSEB)
102#define __FLASHROM_BIG_ENDIAN__ 1
103#endif
104
105#elif IS_PPC
106
107/* PowerPC can be either endian. */
108#if defined (_BIG_ENDIAN) || defined (__BIG_ENDIAN__)
109#define __FLASHROM_BIG_ENDIAN__ 1
110#elif defined (_LITTLE_ENDIAN) || defined (__LITTLE_ENDIAN__)
111#define __FLASHROM_LITTLE_ENDIAN__ 1
112#endif
113
114#elif IS_ARM
115
116/* ARM can be either endian. */
117#if defined (__ARMEB__)
118#define __FLASHROM_BIG_ENDIAN__ 1
119#elif defined (__ARMEL__)
120#define __FLASHROM_LITTLE_ENDIAN__ 1
121#endif
122
123#elif IS_SPARC
124/* SPARC is big endian in general (but allows to access data in little endian too). */
125#define __FLASHROM_BIG_ENDIAN__ 1
126
127#endif /* IS_? */
128
129#if !defined (__FLASHROM_BIG_ENDIAN__) && !defined (__FLASHROM_LITTLE_ENDIAN__)
130
131/* If architecture-specific approaches fail try generic variants. First: BSD (works about everywhere). */
132#if !IS_WINDOWS
133#include <sys/param.h>
134
135#if defined (__BYTE_ORDER)
136#if __BYTE_ORDER == __LITTLE_ENDIAN
137#define __FLASHROM_LITTLE_ENDIAN__
138#elif __BYTE_ORDER == __BIG_ENDIAN
139#define __FLASHROM_BIG_ENDIAN__
140#else
141#error Unknown byte order!
142#endif
143#endif /* defined __BYTE_ORDER */
144#endif /* !IS_WINDOWS */
145
146#if !defined (__FLASHROM_BIG_ENDIAN__) && !defined (__FLASHROM_LITTLE_ENDIAN__)
147
148/* Nonstandard libc-specific macros for determining endianness. */
149/* musl provides an endian.h as well... but it can not be detected from within C. */
150#if defined(__GLIBC__)
151#include <endian.h>
152#if BYTE_ORDER == LITTLE_ENDIAN
153#define __FLASHROM_LITTLE_ENDIAN__ 1
154#elif BYTE_ORDER == BIG_ENDIAN
155#define __FLASHROM_BIG_ENDIAN__ 1
156#endif
157#endif
158#endif
159
160#endif
161
162#if !defined (__FLASHROM_BIG_ENDIAN__) && !defined (__FLASHROM_LITTLE_ENDIAN__)
163#error Unable to determine endianness.
164#endif
165
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000166#endif /* !__PLATFORM_H__ */