blob: 65fe8588134ade8e3f7223ced28004b31d267811 [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
Khem Raja9a03cc2018-03-17 23:08:29 -070072#elif defined (__riscv)
73 #define __FLASHROM_ARCH__ "riscv"
74 #define IS_RISCV 1
Carl-Daniel Hailfinger8d0d53f2016-02-25 20:10:26 +000075#elif defined (__sh__)
76 #define __FLASHROM_ARCH__ "sh"
77 #define IS_SH 1
78#elif defined(__s390__) || defined(__s390x__) || defined(__zarch__)
79 #define __FLASHROM_ARCH__ "s390"
80 #define IS_S390 1
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000081#endif
82
Khem Raja9a03cc2018-03-17 23:08:29 -070083#if !(IS_X86 || IS_MIPS || IS_PPC || IS_ARM || IS_SPARC || IS_ALPHA || IS_HPPA || IS_M68K || IS_RISCV || IS_SH || IS_S390)
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000084#error Unknown architecture
85#endif
86
Nico Huber095522c2017-12-01 18:33:02 +000087/* The next big hunk tries to guess endianess from various preprocessor macros */
88/* First some error checking in case some weird header has defined both.
89 * NB: OpenBSD always defines _BIG_ENDIAN and _LITTLE_ENDIAN. */
90#if defined (__LITTLE_ENDIAN__) && defined (__BIG_ENDIAN__)
91#error Conflicting endianness #define
92#endif
93
94#if IS_X86
95
96/* All x86 is little-endian. */
97#define __FLASHROM_LITTLE_ENDIAN__ 1
98
99#elif IS_MIPS
100
101/* MIPS can be either endian. */
102#if defined (__MIPSEL) || defined (__MIPSEL__) || defined (_MIPSEL) || defined (MIPSEL)
103#define __FLASHROM_LITTLE_ENDIAN__ 1
104#elif defined (__MIPSEB) || defined (__MIPSEB__) || defined (_MIPSEB) || defined (MIPSEB)
105#define __FLASHROM_BIG_ENDIAN__ 1
106#endif
107
108#elif IS_PPC
109
110/* PowerPC can be either endian. */
111#if defined (_BIG_ENDIAN) || defined (__BIG_ENDIAN__)
112#define __FLASHROM_BIG_ENDIAN__ 1
113#elif defined (_LITTLE_ENDIAN) || defined (__LITTLE_ENDIAN__)
114#define __FLASHROM_LITTLE_ENDIAN__ 1
115#endif
116
117#elif IS_ARM
118
119/* ARM can be either endian. */
120#if defined (__ARMEB__)
121#define __FLASHROM_BIG_ENDIAN__ 1
122#elif defined (__ARMEL__)
123#define __FLASHROM_LITTLE_ENDIAN__ 1
124#endif
125
126#elif IS_SPARC
127/* SPARC is big endian in general (but allows to access data in little endian too). */
128#define __FLASHROM_BIG_ENDIAN__ 1
129
130#endif /* IS_? */
131
132#if !defined (__FLASHROM_BIG_ENDIAN__) && !defined (__FLASHROM_LITTLE_ENDIAN__)
133
134/* If architecture-specific approaches fail try generic variants. First: BSD (works about everywhere). */
135#if !IS_WINDOWS
136#include <sys/param.h>
137
138#if defined (__BYTE_ORDER)
139#if __BYTE_ORDER == __LITTLE_ENDIAN
140#define __FLASHROM_LITTLE_ENDIAN__
141#elif __BYTE_ORDER == __BIG_ENDIAN
142#define __FLASHROM_BIG_ENDIAN__
143#else
144#error Unknown byte order!
145#endif
146#endif /* defined __BYTE_ORDER */
147#endif /* !IS_WINDOWS */
148
149#if !defined (__FLASHROM_BIG_ENDIAN__) && !defined (__FLASHROM_LITTLE_ENDIAN__)
150
151/* Nonstandard libc-specific macros for determining endianness. */
152/* musl provides an endian.h as well... but it can not be detected from within C. */
153#if defined(__GLIBC__)
154#include <endian.h>
155#if BYTE_ORDER == LITTLE_ENDIAN
156#define __FLASHROM_LITTLE_ENDIAN__ 1
157#elif BYTE_ORDER == BIG_ENDIAN
158#define __FLASHROM_BIG_ENDIAN__ 1
159#endif
160#endif
161#endif
162
163#endif
164
165#if !defined (__FLASHROM_BIG_ENDIAN__) && !defined (__FLASHROM_LITTLE_ENDIAN__)
166#error Unable to determine endianness.
167#endif
168
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000169#endif /* !__PLATFORM_H__ */