blob: 751957cae4c2bdd9e47270f83062cd0b6aa96e8f [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.
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000014 */
15
16/*
17 * Header file to determine target OS and CPU architecture.
18 */
19
20#ifndef __PLATFORM_H__
21#define __PLATFORM_H__ 1
22
23// Helper defines for operating systems
Patrick Georgid2a03b32017-03-13 13:48:03 +010024#if defined(__gnu_linux__) || defined(__linux__)
25#define IS_LINUX 1
26#else
27#define IS_LINUX 0
28#endif
29#if defined(__APPLE__) && defined(__MACH__) /* yes, both. */
30#define IS_MACOSX 1
31#else
32#define IS_MACOSX 0
33#endif
34#if defined(_WIN32) || defined(_WIN64) || defined(__WIN32__) || defined(__WINDOWS__)
35#define IS_WINDOWS 1
36#else
37#define IS_WINDOWS 0
38#endif
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000039
40// Likewise for target architectures
41#if defined (__i386__) || defined (__x86_64__) || defined(__amd64__)
42 #define __FLASHROM_ARCH__ "x86"
43 #define IS_X86 1
44#elif defined (__mips) || defined (__mips__) || defined (__MIPS__) || defined (mips)
45 #define __FLASHROM_ARCH__ "mips"
46 #define IS_MIPS 1
47#elif defined(__powerpc) || defined(__powerpc__) || defined(__powerpc64__) || defined(__POWERPC__) || \
48 defined(__ppc__) || defined(__ppc64__) || defined(_M_PPC) || defined(_ARCH_PPC) || \
49 defined(_ARCH_PPC64) || defined(__ppc)
50 #define __FLASHROM_ARCH__ "ppc"
51 #define IS_PPC 1
52#elif defined(__arm__) || defined(__TARGET_ARCH_ARM) || defined(_ARM) || defined(_M_ARM) || defined(__arm) || \
53 defined(__aarch64__)
54 #define __FLASHROM_ARCH__ "arm"
55 #define IS_ARM 1
Stefan Taunerfb2d77c2015-02-10 08:03:10 +000056#elif defined (__sparc__) || defined (__sparc)
57 #define __FLASHROM_ARCH__ "sparc"
58 #define IS_SPARC 1
Carl-Daniel Hailfinger8d0d53f2016-02-25 20:10:26 +000059#elif defined (__alpha__)
60 #define __FLASHROM_ARCH__ "alpha"
61 #define IS_ALPHA 1
62#elif defined (__hppa__) || defined (__hppa)
63 #define __FLASHROM_ARCH__ "hppa"
64 #define IS_HPPA 1
65#elif defined (__m68k__)
66 #define __FLASHROM_ARCH__ "m68k"
67 #define IS_M68K 1
Khem Raja9a03cc2018-03-17 23:08:29 -070068#elif defined (__riscv)
69 #define __FLASHROM_ARCH__ "riscv"
70 #define IS_RISCV 1
Carl-Daniel Hailfinger8d0d53f2016-02-25 20:10:26 +000071#elif defined (__sh__)
72 #define __FLASHROM_ARCH__ "sh"
73 #define IS_SH 1
74#elif defined(__s390__) || defined(__s390x__) || defined(__zarch__)
75 #define __FLASHROM_ARCH__ "s390"
76 #define IS_S390 1
Rosen Penev34d07f02019-07-02 00:14:01 -070077#elif defined(__arc__)
78 #define __FLASHROM_ARCH__ "arc"
79 #define IS_ARC 1
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000080#endif
81
Rosen Penev34d07f02019-07-02 00:14:01 -070082#if !(IS_X86 || IS_MIPS || IS_PPC || IS_ARM || IS_SPARC || IS_ALPHA || IS_HPPA || IS_M68K || IS_RISCV || IS_SH || IS_S390 || IS_ARC)
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000083#error Unknown architecture
84#endif
85
Elyes HAOUASe2c90c42018-08-18 09:04:41 +020086/* The next big hunk tries to guess endianness from various preprocessor macros */
Nico Huber095522c2017-12-01 18:33:02 +000087/* First some error checking in case some weird header has defined both.
88 * NB: OpenBSD always defines _BIG_ENDIAN and _LITTLE_ENDIAN. */
89#if defined (__LITTLE_ENDIAN__) && defined (__BIG_ENDIAN__)
90#error Conflicting endianness #define
91#endif
92
93#if IS_X86
94
95/* All x86 is little-endian. */
96#define __FLASHROM_LITTLE_ENDIAN__ 1
97
98#elif IS_MIPS
99
100/* MIPS can be either endian. */
101#if defined (__MIPSEL) || defined (__MIPSEL__) || defined (_MIPSEL) || defined (MIPSEL)
102#define __FLASHROM_LITTLE_ENDIAN__ 1
103#elif defined (__MIPSEB) || defined (__MIPSEB__) || defined (_MIPSEB) || defined (MIPSEB)
104#define __FLASHROM_BIG_ENDIAN__ 1
105#endif
106
107#elif IS_PPC
108
109/* PowerPC can be either endian. */
110#if defined (_BIG_ENDIAN) || defined (__BIG_ENDIAN__)
111#define __FLASHROM_BIG_ENDIAN__ 1
112#elif defined (_LITTLE_ENDIAN) || defined (__LITTLE_ENDIAN__)
113#define __FLASHROM_LITTLE_ENDIAN__ 1
114#endif
115
116#elif IS_ARM
117
118/* ARM can be either endian. */
119#if defined (__ARMEB__)
120#define __FLASHROM_BIG_ENDIAN__ 1
121#elif defined (__ARMEL__)
122#define __FLASHROM_LITTLE_ENDIAN__ 1
123#endif
124
125#elif IS_SPARC
126/* SPARC is big endian in general (but allows to access data in little endian too). */
127#define __FLASHROM_BIG_ENDIAN__ 1
128
Rosen Penev34d07f02019-07-02 00:14:01 -0700129#elif IS_ARC
130#if defined(__BIG_ENDIAN__)
131#define __FLASHROM_BIG_ENDIAN__ 1
132#else
133#define __FLASHROM_LITTLE_ENDIAN__ 1
134#endif
135
Nico Huber095522c2017-12-01 18:33:02 +0000136#endif /* IS_? */
137
138#if !defined (__FLASHROM_BIG_ENDIAN__) && !defined (__FLASHROM_LITTLE_ENDIAN__)
139
140/* If architecture-specific approaches fail try generic variants. First: BSD (works about everywhere). */
141#if !IS_WINDOWS
142#include <sys/param.h>
143
144#if defined (__BYTE_ORDER)
145#if __BYTE_ORDER == __LITTLE_ENDIAN
146#define __FLASHROM_LITTLE_ENDIAN__
147#elif __BYTE_ORDER == __BIG_ENDIAN
148#define __FLASHROM_BIG_ENDIAN__
149#else
150#error Unknown byte order!
151#endif
152#endif /* defined __BYTE_ORDER */
153#endif /* !IS_WINDOWS */
154
155#if !defined (__FLASHROM_BIG_ENDIAN__) && !defined (__FLASHROM_LITTLE_ENDIAN__)
156
157/* Nonstandard libc-specific macros for determining endianness. */
158/* musl provides an endian.h as well... but it can not be detected from within C. */
159#if defined(__GLIBC__)
160#include <endian.h>
161#if BYTE_ORDER == LITTLE_ENDIAN
162#define __FLASHROM_LITTLE_ENDIAN__ 1
163#elif BYTE_ORDER == BIG_ENDIAN
164#define __FLASHROM_BIG_ENDIAN__ 1
165#endif
166#endif
167#endif
168
169#endif
170
171#if !defined (__FLASHROM_BIG_ENDIAN__) && !defined (__FLASHROM_LITTLE_ENDIAN__)
172#error Unable to determine endianness.
173#endif
174
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000175#endif /* !__PLATFORM_H__ */