Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 1 | /* |
| 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 Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 14 | */ |
| 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 Georgi | d2a03b3 | 2017-03-13 13:48:03 +0100 | [diff] [blame] | 24 | #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 Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 39 | |
| 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 Tauner | fb2d77c | 2015-02-10 08:03:10 +0000 | [diff] [blame] | 56 | #elif defined (__sparc__) || defined (__sparc) |
| 57 | #define __FLASHROM_ARCH__ "sparc" |
| 58 | #define IS_SPARC 1 |
Carl-Daniel Hailfinger | 8d0d53f | 2016-02-25 20:10:26 +0000 | [diff] [blame] | 59 | #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 Raj | a9a03cc | 2018-03-17 23:08:29 -0700 | [diff] [blame] | 68 | #elif defined (__riscv) |
| 69 | #define __FLASHROM_ARCH__ "riscv" |
| 70 | #define IS_RISCV 1 |
Carl-Daniel Hailfinger | 8d0d53f | 2016-02-25 20:10:26 +0000 | [diff] [blame] | 71 | #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 |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 77 | #endif |
| 78 | |
Khem Raj | a9a03cc | 2018-03-17 23:08:29 -0700 | [diff] [blame] | 79 | #if !(IS_X86 || IS_MIPS || IS_PPC || IS_ARM || IS_SPARC || IS_ALPHA || IS_HPPA || IS_M68K || IS_RISCV || IS_SH || IS_S390) |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 80 | #error Unknown architecture |
| 81 | #endif |
| 82 | |
Elyes HAOUAS | e2c90c4 | 2018-08-18 09:04:41 +0200 | [diff] [blame] | 83 | /* The next big hunk tries to guess endianness from various preprocessor macros */ |
Nico Huber | 095522c | 2017-12-01 18:33:02 +0000 | [diff] [blame] | 84 | /* First some error checking in case some weird header has defined both. |
| 85 | * NB: OpenBSD always defines _BIG_ENDIAN and _LITTLE_ENDIAN. */ |
| 86 | #if defined (__LITTLE_ENDIAN__) && defined (__BIG_ENDIAN__) |
| 87 | #error Conflicting endianness #define |
| 88 | #endif |
| 89 | |
| 90 | #if IS_X86 |
| 91 | |
| 92 | /* All x86 is little-endian. */ |
| 93 | #define __FLASHROM_LITTLE_ENDIAN__ 1 |
| 94 | |
| 95 | #elif IS_MIPS |
| 96 | |
| 97 | /* MIPS can be either endian. */ |
| 98 | #if defined (__MIPSEL) || defined (__MIPSEL__) || defined (_MIPSEL) || defined (MIPSEL) |
| 99 | #define __FLASHROM_LITTLE_ENDIAN__ 1 |
| 100 | #elif defined (__MIPSEB) || defined (__MIPSEB__) || defined (_MIPSEB) || defined (MIPSEB) |
| 101 | #define __FLASHROM_BIG_ENDIAN__ 1 |
| 102 | #endif |
| 103 | |
| 104 | #elif IS_PPC |
| 105 | |
| 106 | /* PowerPC can be either endian. */ |
| 107 | #if defined (_BIG_ENDIAN) || defined (__BIG_ENDIAN__) |
| 108 | #define __FLASHROM_BIG_ENDIAN__ 1 |
| 109 | #elif defined (_LITTLE_ENDIAN) || defined (__LITTLE_ENDIAN__) |
| 110 | #define __FLASHROM_LITTLE_ENDIAN__ 1 |
| 111 | #endif |
| 112 | |
| 113 | #elif IS_ARM |
| 114 | |
| 115 | /* ARM can be either endian. */ |
| 116 | #if defined (__ARMEB__) |
| 117 | #define __FLASHROM_BIG_ENDIAN__ 1 |
| 118 | #elif defined (__ARMEL__) |
| 119 | #define __FLASHROM_LITTLE_ENDIAN__ 1 |
| 120 | #endif |
| 121 | |
| 122 | #elif IS_SPARC |
| 123 | /* SPARC is big endian in general (but allows to access data in little endian too). */ |
| 124 | #define __FLASHROM_BIG_ENDIAN__ 1 |
| 125 | |
| 126 | #endif /* IS_? */ |
| 127 | |
| 128 | #if !defined (__FLASHROM_BIG_ENDIAN__) && !defined (__FLASHROM_LITTLE_ENDIAN__) |
| 129 | |
| 130 | /* If architecture-specific approaches fail try generic variants. First: BSD (works about everywhere). */ |
| 131 | #if !IS_WINDOWS |
| 132 | #include <sys/param.h> |
| 133 | |
| 134 | #if defined (__BYTE_ORDER) |
| 135 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 136 | #define __FLASHROM_LITTLE_ENDIAN__ |
| 137 | #elif __BYTE_ORDER == __BIG_ENDIAN |
| 138 | #define __FLASHROM_BIG_ENDIAN__ |
| 139 | #else |
| 140 | #error Unknown byte order! |
| 141 | #endif |
| 142 | #endif /* defined __BYTE_ORDER */ |
| 143 | #endif /* !IS_WINDOWS */ |
| 144 | |
| 145 | #if !defined (__FLASHROM_BIG_ENDIAN__) && !defined (__FLASHROM_LITTLE_ENDIAN__) |
| 146 | |
| 147 | /* Nonstandard libc-specific macros for determining endianness. */ |
| 148 | /* musl provides an endian.h as well... but it can not be detected from within C. */ |
| 149 | #if defined(__GLIBC__) |
| 150 | #include <endian.h> |
| 151 | #if BYTE_ORDER == LITTLE_ENDIAN |
| 152 | #define __FLASHROM_LITTLE_ENDIAN__ 1 |
| 153 | #elif BYTE_ORDER == BIG_ENDIAN |
| 154 | #define __FLASHROM_BIG_ENDIAN__ 1 |
| 155 | #endif |
| 156 | #endif |
| 157 | #endif |
| 158 | |
| 159 | #endif |
| 160 | |
| 161 | #if !defined (__FLASHROM_BIG_ENDIAN__) && !defined (__FLASHROM_LITTLE_ENDIAN__) |
| 162 | #error Unable to determine endianness. |
| 163 | #endif |
| 164 | |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 165 | #endif /* !__PLATFORM_H__ */ |