Patchwork [3/5] Unify target OS and CPU architecture checks.

login
register
about
Submitter Stefan Tauner
Date 2015-01-05 00:12:41
Message ID <1420416763-11808-4-git-send-email-stefan.tauner@alumni.tuwien.ac.at>
Download mbox | patch
Permalink /patch/4268/
State Accepted
Headers show

Comments

Stefan Tauner - 2015-01-05 00:12:41
We do CPU architecture checks once for the makefile in arch.h and
once for HW access abstraction in hwaccess.c. This patch unifies
related files so that they can share the checks to improve
maintainability and reduce the chance of inconsistencies.
Furthermore, it refines some of the definitions, which
 - adds "support" for AARCH64 and PPC64,
 - adds big-endian handling on arm as well as LE handling on PPC64,
 - fixes compilation of internal.c on AARCH64 and PPC64.

Additionally, this patch continues to unify all OS checks in
flashrom by adding a new helper macro IS_WINDOWS.

The old header file for architecture checking is renamed to platform.h
to reflect its broader scope and all new macros are add in there.

Signed-off-by: Stefan Tauner <stefan.tauner@alumni.tuwien.ac.at>
---
 Makefile                                         |  2 +-
 arch.h                                           | 33 ---------
 archtest.c                                       |  2 +
 flash.h                                          |  4 +-
 flashrom.c                                       |  2 +-
 hwaccess.c                                       | 20 ++----
 hwaccess.h                                       | 87 +++++++++++++++---------
 internal.c                                       | 16 ++---
 os.h                                             |  2 +
 platform.h                                       | 54 +++++++++++++++
 programmer.h                                     |  2 +-
 serial.c                                         | 40 +++++------
 serprog.c                                        | 10 +--
 udelay.c                                         |  2 +-
 util/ich_descriptors_tool/ich_descriptors_tool.c |  2 +-
 15 files changed, 163 insertions(+), 115 deletions(-)
 delete mode 100644 arch.h
 create mode 100644 archtest.c
 create mode 100644 platform.h

Patch

diff --git a/Makefile b/Makefile
index 7d3da08..96a51a3 100644
--- a/Makefile
+++ b/Makefile
@@ -316,7 +316,7 @@  endif
 # IMPORTANT: The following line must be placed before ARCH is ever used
 # (of course), but should come after any lines setting CC because the line
 # below uses CC itself.
-override ARCH := $(strip $(shell LC_ALL=C $(CC) $(CPPFLAGS) -E arch.h 2>/dev/null | grep -v '^\#' | grep '"' | cut -f 2 -d'"'))
+override ARCH := $(strip $(shell LC_ALL=C $(CC) $(CPPFLAGS) -E archtest.c 2>/dev/null | grep -v '^\#' | grep '"' | cut -f 2 -d'"'))
 
 # PCI port I/O support is unimplemented on PPC/MIPS and unavailable on ARM.
 # Right now this means the drivers below only work on x86.
diff --git a/arch.h b/arch.h
deleted file mode 100644
index f3f3b0d..0000000
--- a/arch.h
+++ /dev/null
@@ -1,33 +0,0 @@ 
-/*
- * This file is part of the flashrom project.
- *
- * Copyright (C) 2011 Carl-Daniel Hailfinger
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
- */
-
-/*
- * Header file for CPU architecture checking.
- */
-
-#if defined (__i386__) || defined (__x86_64__)
-#define __FLASHROM_ARCH__ "x86"
-#elif defined (__mips) || defined (__mips__) || defined (_mips) || defined (mips)
-#define __FLASHROM_ARCH__ "mips"
-#elif defined(__powerpc__) || defined(__powerpc64__) || defined(__ppc__) || defined(__ppc64__)
-#define __FLASHROM_ARCH__ "ppc"
-#elif defined(__arm__)
-#define __FLASHROM_ARCH__ "arm"
-#endif
-__FLASHROM_ARCH__
diff --git a/archtest.c b/archtest.c
new file mode 100644
index 0000000..791f1a3
--- /dev/null
+++ b/archtest.c
@@ -0,0 +1,2 @@ 
+#include "platform.h"
+__FLASHROM_ARCH__
diff --git a/flash.h b/flash.h
index 81fd61b..56d9e4b 100644
--- a/flash.h
+++ b/flash.h
@@ -24,12 +24,14 @@ 
 #ifndef __FLASH_H__
 #define __FLASH_H__ 1
 
+#include "platform.h"
+
 #include <inttypes.h>
 #include <stdio.h>
 #include <stdint.h>
 #include <stddef.h>
 #include <stdbool.h>
-#ifdef _WIN32
+#if IS_WINDOWS
 #include <windows.h>
 #undef min
 #undef max
diff --git a/flashrom.c b/flashrom.c
index 104ecb8..9df1478 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -1661,7 +1661,7 @@  void list_programmers_linebreak(int startcol, int cols, int paren)
 
 void print_sysinfo(void)
 {
-#ifdef _WIN32
+#if IS_WINDOWS
 	SYSTEM_INFO si;
 	OSVERSIONINFOEX osvi;
 
diff --git a/hwaccess.c b/hwaccess.c
index 5b18c32..c90490b 100644
--- a/hwaccess.c
+++ b/hwaccess.c
@@ -18,19 +18,7 @@ 
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
-#define IS_X86	(defined(__i386__) || defined(__x86_64__) || defined(__amd64__))
-#define IS_MIPS	(defined (__mips) || defined (__mips__) || defined (__MIPS__) || defined (mips))
-#define IS_PPC	(defined(__powerpc__) || defined(__powerpc64__) || defined(__ppc__) || defined(__ppc64__))
-#define IS_ARM	(defined (__arm__) || defined (_ARM))
-#if !(IS_X86 || IS_MIPS || IS_PPC || IS_ARM)
-#error Unknown architecture
-#endif
-
-#define IS_LINUX	(defined(__gnu_linux__) || defined(__linux__))
-#define IS_MACOSX	(defined(__APPLE__) && defined(__MACH__))
-#if !(IS_LINUX || IS_MACOSX || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__DJGPP__) || defined(__LIBPAYLOAD__) || defined(__sun))
-#error "Unknown operating system"
-#endif
+#include "platform.h"
 
 #include <stdint.h>
 #include <string.h>
@@ -45,6 +33,10 @@ 
 #include "flash.h"
 #include "hwaccess.h"
 
+#if !(IS_LINUX || IS_MACOSX || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__DJGPP__) || defined(__LIBPAYLOAD__) || defined(__sun))
+#error "Unknown operating system"
+#endif
+
 #define USE_IOPL	(IS_LINUX || IS_MACOSX || defined(__NetBSD__) || defined(__OpenBSD__))
 #define USE_DEV_IO	(defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__))
 
@@ -62,7 +54,7 @@  static inline void sync_primitive(void)
  * - MIPS uses uncached accesses in mode 2 on /dev/mem which has also a strongly ordered memory model
  * - ARM uses a strongly ordered memory model for device memories.
  */
-#if IS_PPC
+#if IS_PPC // cf. http://lxr.free-electrons.com/source/arch/powerpc/include/asm/barrier.h
 	asm("eieio" : : : "memory");
 #endif
 }
diff --git a/hwaccess.h b/hwaccess.h
index 83f349e..f2b14df 100644
--- a/hwaccess.h
+++ b/hwaccess.h
@@ -24,7 +24,9 @@ 
 #ifndef __HWACCESS_H__
 #define __HWACCESS_H__ 1
 
-#if defined (__i386__) || defined (__x86_64__)
+#include "platform.h"
+
+#if IS_X86
 #if defined(__GLIBC__)
 #include <sys/io.h>
 #endif
@@ -45,14 +47,22 @@ 
 #endif
 
 #undef index
+#endif /* NEED_PCI == 1 */
+
+
+/* The next big hunk tries to guess endianess from various preprocessor macros */
+/* First some error checking in case some weird header has defines both.
+ * NB: OpenBSD always defines _BIG_ENDIAN and _LITTLE_ENDIAN. */
+#if defined (__LITTLE_ENDIAN__) && defined (__BIG_ENDIAN__)
+#error Conflicting endianness #define
 #endif
 
-#if defined (__i386__) || defined (__x86_64__)
+#if IS_X86
 
 /* All x86 is little-endian. */
 #define __FLASHROM_LITTLE_ENDIAN__ 1
 
-#elif defined (__mips) || defined (__mips__) || defined (_mips) || defined (mips)
+#elif IS_MIPS
 
 /* MIPS can be either endian. */
 #if defined (__MIPSEL) || defined (__MIPSEL__) || defined (_MIPSEL) || defined (MIPSEL)
@@ -61,29 +71,45 @@ 
 #define __FLASHROM_BIG_ENDIAN__ 1
 #endif
 
-#elif defined(__powerpc__) || defined(__powerpc64__) || defined(__ppc__) || defined(__ppc64__)
+#elif IS_PPC
 
 /* PowerPC can be either endian. */
 #if defined (_BIG_ENDIAN) || defined (__BIG_ENDIAN__)
 #define __FLASHROM_BIG_ENDIAN__ 1
-/* Error checking in case some weird header has #defines for LE as well. */
-#if defined (_LITTLE_ENDIAN) || defined (__LITTLE_ENDIAN__)
-#error Conflicting endianness #define
-#endif
-#else
-#error Little-endian PowerPC #defines are unknown
+#elif defined (_LITTLE_ENDIAN) || defined (__LITTLE_ENDIAN__)
+#define __FLASHROM_LITTLE_ENDIAN__ 1
 #endif
 
-#elif defined (__arm__)
-#if defined (__ARMEL__)
+#elif IS_ARM
+
+/* ARM can be either endian. */
+#if defined (__ARMEB__)
+#define __FLASHROM_BIG_ENDIAN__ 1
+#elif defined (__ARMEL__)
 #define __FLASHROM_LITTLE_ENDIAN__ 1
-#else
-#error Big-endian ARM #defines are unknown
 #endif
 
+#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 <sys/param.h>
+
+#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. */
 #if defined(__GLIBC__)
 #include <endian.h>
@@ -95,8 +121,10 @@ 
 #endif
 #endif
 
+#endif
+
 #if !defined (__FLASHROM_BIG_ENDIAN__) && !defined (__FLASHROM_LITTLE_ENDIAN__)
-#error Unable to determine endianness. Please add support for your arch or libc.
+#error Unable to determine endianness.
 #endif
 
 #define ___constant_swab8(x) ((uint8_t) (				\
@@ -158,11 +186,7 @@  cpu_to_be(64)
 #define cpu_to_le32
 #define cpu_to_le64
 
-#else
-
-#error Could not determine endianness.
-
-#endif
+#endif /* __FLASHROM_BIG_ENDIAN__ / __FLASHROM_LITTLE_ENDIAN__ */
 
 #define be_to_cpu8 cpu_to_be8
 #define be_to_cpu16 cpu_to_be16
@@ -174,12 +198,12 @@  cpu_to_be(64)
 #define le_to_cpu64 cpu_to_le64
 
 #if NEED_PCI == 1
-#if defined (__i386__) || defined (__x86_64__)
+#if IS_X86
 
 #define __FLASHROM_HAVE_OUTB__ 1
 
 /* for iopl and outb under Solaris */
-#if defined (__sun) && (defined(__i386) || defined(__amd64))
+#if defined (__sun)
 #include <sys/sysi86.h>
 #include <sys/psw.h>
 #include <asm/sunddi.h>
@@ -202,11 +226,13 @@  cpu_to_be(64)
   #define INW(x) __extension__ ({ u_int inw_tmp = (x); inw(inw_tmp); })
   #define INL(x) __extension__ ({ u_int inl_tmp = (x); inl(inl_tmp); })
 #else
+
 #if defined(__MACH__) && defined(__APPLE__)
     /* Header is part of the DirectHW library. */
     #include <DirectHW/DirectHW.h>
-#endif
-#if defined (__sun) && (defined(__i386) || defined(__amd64))
+#else
+
+#if defined (__sun)
   /* Note different order for outb */
   #define OUTB(x,y) outb(y, x)
   #define OUTW(x,y) outw(y, x)
@@ -236,9 +262,8 @@  cpu_to_be(64)
   #define INB  inb
   #define INW  inw
   #define INL  inl
-
 #endif
-
+#endif
 #endif
 #endif
 
@@ -324,15 +349,15 @@  int libpayload_wrmsr(int addr, msr_t msr);
 #define wrmsr libpayload_wrmsr
 #endif
 
-#elif defined(__powerpc__) || defined(__powerpc64__) || defined(__ppc__) || defined(__ppc64__)
+#elif IS_PPC
 
 /* PCI port I/O is not yet implemented on PowerPC. */
 
-#elif defined (__mips) || defined (__mips__) || defined (_mips) || defined (mips)
+#elif IS_MIPS
 
 /* PCI port I/O is not yet implemented on MIPS. */
 
-#elif defined(__arm__)
+#elif IS_ARM
 
 /* Non memory mapped I/O is not supported on ARM. */
 
@@ -340,7 +365,7 @@  int libpayload_wrmsr(int addr, msr_t msr);
 
 #error Unknown architecture, please check if it supports PCI port IO.
 
-#endif
-#endif
+#endif /* IS_* */
+#endif /* NEED_PCI == 1 */
 
 #endif /* !__HWACCESS_H__ */
diff --git a/internal.c b/internal.c
index e646ca8..f6146d1 100644
--- a/internal.c
+++ b/internal.c
@@ -160,14 +160,14 @@  enum chipbustype internal_buses_supported = BUS_NONE;
 
 int internal_init(void)
 {
-#if __FLASHROM_LITTLE_ENDIAN__
+#if defined __FLASHROM_LITTLE_ENDIAN__
 	int ret = 0;
 #endif
 	int force_laptop = 0;
 	int not_a_laptop = 0;
 	const char *board_vendor = NULL;
 	const char *board_model = NULL;
-#if defined (__i386__) || defined (__x86_64__) || defined (__arm__)
+#if IS_X86 || IS_ARM
 	const char *cb_vendor = NULL;
 	const char *cb_model = NULL;
 #endif
@@ -249,7 +249,7 @@  int internal_init(void)
 		return 1;
 	}
 
-#if defined(__i386__) || defined(__x86_64__) || defined (__arm__)
+#if IS_X86 || IS_ARM
 	if ((cb_parse_table(&cb_vendor, &cb_model) == 0) && (board_vendor != NULL) && (board_model != NULL)) {
 		if (strcasecmp(board_vendor, cb_vendor) || strcasecmp(board_model, cb_model)) {
 			msg_pwarn("Warning: The mainboard IDs set by -p internal:mainboard (%s:%s) do not\n"
@@ -262,7 +262,7 @@  int internal_init(void)
 	}
 #endif
 
-#if defined(__i386__) || defined(__x86_64__)
+#if IS_X86
 	dmi_init();
 
 	/* In case Super I/O probing would cause pretty explosions. */
@@ -312,7 +312,7 @@  int internal_init(void)
 		}
 	}
 
-#if __FLASHROM_LITTLE_ENDIAN__
+#ifdef __FLASHROM_LITTLE_ENDIAN__
 	/* try to enable it. Failure IS an option, since not all motherboards
 	 * really need this to be done, etc., etc.
 	 */
@@ -323,18 +323,18 @@  int internal_init(void)
 	} else if (ret == ERROR_FATAL)
 		return ret;
 
-#if defined(__i386__) || defined(__x86_64__)
+#if IS_X86
 	/* Probe unconditionally for ITE Super I/O chips. This enables LPC->SPI translation on IT87* and
 	 * parallel writes on IT8705F. Also, this handles the manual chip select for Gigabyte's DualBIOS. */
 	init_superio_ite();
-#endif
 
 	if (board_flash_enable(board_vendor, board_model, cb_vendor, cb_model)) {
 		msg_perr("Aborting to be safe.\n");
 		return 1;
 	}
+#endif
 
-#if defined(__i386__) || defined(__x86_64__) || defined (__mips)
+#if IS_X86 || IS_MIPS
 	register_par_master(&par_master_internal, internal_buses_supported);
 	return 0;
 #else
diff --git a/os.h b/os.h
index f1b366f..2e6dbaa 100644
--- a/os.h
+++ b/os.h
@@ -21,6 +21,8 @@ 
  * Header file for OS checking.
  */
 
+#include "platform.h"
+
 // Solaris
 #if defined (__sun) && (defined(__i386) || defined(__amd64))
 #define __FLASHROM_OS__ "SunOS"
diff --git a/platform.h b/platform.h
new file mode 100644
index 0000000..f57fd12
--- /dev/null
+++ b/platform.h
@@ -0,0 +1,54 @@ 
+/*
+ * This file is part of the flashrom project.
+ *
+ * Copyright (C) 2011 Carl-Daniel Hailfinger
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ */
+
+/*
+ * Header file to determine target OS and CPU architecture.
+ */
+
+#ifndef __PLATFORM_H__
+#define __PLATFORM_H__ 1
+
+// Helper defines for operating systems
+#define IS_LINUX	(defined(__gnu_linux__) || defined(__linux__))
+#define IS_MACOSX	(defined(__APPLE__) && defined(__MACH__)) /* yes, both. */
+#define IS_WINDOWS	(defined(_WIN32) || defined(_WIN64) || defined(__WIN32__) || defined(__WINDOWS__))
+
+// Likewise for target architectures
+#if defined (__i386__) || defined (__x86_64__) || defined(__amd64__)
+	#define __FLASHROM_ARCH__ "x86"
+	#define IS_X86 1
+#elif defined (__mips) || defined (__mips__) || defined (__MIPS__) || defined (mips)
+	#define __FLASHROM_ARCH__ "mips"
+	#define IS_MIPS 1
+#elif defined(__powerpc) || defined(__powerpc__) || defined(__powerpc64__) || defined(__POWERPC__) || \
+      defined(__ppc__) || defined(__ppc64__) || defined(_M_PPC) || defined(_ARCH_PPC) || \
+      defined(_ARCH_PPC64) || defined(__ppc)
+	#define __FLASHROM_ARCH__ "ppc"
+	#define IS_PPC 1
+#elif defined(__arm__) || defined(__TARGET_ARCH_ARM) || defined(_ARM) || defined(_M_ARM) || defined(__arm) || \
+      defined(__aarch64__)
+	#define __FLASHROM_ARCH__ "arm"
+	#define IS_ARM 1
+#endif
+
+#if !(IS_X86 || IS_MIPS || IS_PPC || IS_ARM)
+#error Unknown architecture
+#endif
+
+#endif /* !__PLATFORM_H__ */
diff --git a/programmer.h b/programmer.h
index d6e06cb..f82e950 100644
--- a/programmer.h
+++ b/programmer.h
@@ -700,7 +700,7 @@  void serprog_delay(unsigned int usecs);
 #endif
 
 /* serial.c */
-#ifdef _WIN32
+#if IS_WINDOWS
 typedef HANDLE fdtype;
 #define SER_INV_FD	INVALID_HANDLE_VALUE
 #else
diff --git a/serial.c b/serial.c
index e4e911d..94f9b7d 100644
--- a/serial.c
+++ b/serial.c
@@ -19,6 +19,8 @@ 
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
+#include "platform.h"
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -28,7 +30,7 @@ 
 #include <sys/stat.h>
 #include <errno.h>
 #include <inttypes.h>
-#ifdef _WIN32
+#if IS_WINDOWS
 #include <conio.h>
 #else
 #include <termios.h>
@@ -41,7 +43,7 @@ 
 
 fdtype sp_fd = SER_INV_FD;
 
-#ifdef _WIN32
+#if IS_WINDOWS
 struct baudentry {
 	DWORD flag;
 	unsigned int baud;
@@ -137,7 +139,7 @@  const struct baudentry *round_baud(unsigned int baud)
 static void msg_perr_strerror(const char *msg)
 {
 	msg_perr("Error: %s", msg);
-#ifdef _WIN32
+#if IS_WINDOWS
 	char *lpMsgBuf;
 	DWORD nErr = GetLastError();
 	FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, nErr,
@@ -159,7 +161,7 @@  int serialport_config(fdtype fd, unsigned int baud)
 		return 1;
 	}
 
-#ifdef _WIN32
+#if IS_WINDOWS
 	DCB dcb;
 	if (!GetCommState(fd, &dcb)) {
 		msg_perr_strerror("Could not fetch original serial port configuration: ");
@@ -224,7 +226,7 @@  int serialport_config(fdtype fd, unsigned int baud)
 fdtype sp_openserport(char *dev, unsigned int baud)
 {
 	fdtype fd;
-#ifdef _WIN32
+#if IS_WINDOWS
 	char *dev2 = dev;
 	if ((strlen(dev) > 3) &&
 	    (tolower((unsigned char)dev[0]) == 'c') &&
@@ -266,7 +268,7 @@  fdtype sp_openserport(char *dev, unsigned int baud)
 }
 
 void sp_set_pin(enum SP_PIN pin, int val) {
-#ifdef _WIN32
+#if IS_WINDOWS
 	DWORD ctl;
 
 	if(pin == PIN_TXD) {
@@ -302,7 +304,7 @@  void sp_set_pin(enum SP_PIN pin, int val) {
 
 int sp_get_pin(enum SP_PIN pin) {
 	int s;
-#ifdef _WIN32
+#if IS_WINDOWS
 	DWORD ctl;
 
 	s = (pin == PIN_CTS) ? MS_CTS_ON : MS_DSR_ON;
@@ -319,7 +321,7 @@  int sp_get_pin(enum SP_PIN pin) {
 
 void sp_flush_incoming(void)
 {
-#ifdef _WIN32
+#if IS_WINDOWS
 	PurgeComm(sp_fd, PURGE_RXCLEAR);
 #else
 	/* FIXME: error handling */
@@ -330,7 +332,7 @@  void sp_flush_incoming(void)
 
 int serialport_shutdown(void *data)
 {
-#ifdef _WIN32
+#if IS_WINDOWS
 	CloseHandle(sp_fd);
 #else
 	close(sp_fd);
@@ -340,7 +342,7 @@  int serialport_shutdown(void *data)
 
 int serialport_write(const unsigned char *buf, unsigned int writecnt)
 {
-#ifdef _WIN32
+#if IS_WINDOWS
 	DWORD tmp = 0;
 #else
 	ssize_t tmp = 0;
@@ -348,7 +350,7 @@  int serialport_write(const unsigned char *buf, unsigned int writecnt)
 	unsigned int empty_writes = 250; /* results in a ca. 125ms timeout */
 
 	while (writecnt > 0) {
-#ifdef _WIN32
+#if IS_WINDOWS
 		WriteFile(sp_fd, buf, writecnt, &tmp, NULL);
 #else
 		tmp = write(sp_fd, buf, writecnt);
@@ -375,14 +377,14 @@  int serialport_write(const unsigned char *buf, unsigned int writecnt)
 
 int serialport_read(unsigned char *buf, unsigned int readcnt)
 {
-#ifdef _WIN32
+#if IS_WINDOWS
 	DWORD tmp = 0;
 #else
 	ssize_t tmp = 0;
 #endif
 
 	while (readcnt > 0) {
-#ifdef _WIN32
+#if IS_WINDOWS
 		ReadFile(sp_fd, buf, readcnt, &tmp, NULL);
 #else
 		tmp = read(sp_fd, buf, readcnt);
@@ -407,7 +409,7 @@  int serialport_read_nonblock(unsigned char *c, unsigned int readcnt, unsigned in
 {
 	int ret = 1;
 	/* disable blocked i/o and declare platform-specific variables */
-#ifdef _WIN32
+#if IS_WINDOWS
 	DWORD rv;
 	COMMTIMEOUTS oldTimeout;
 	COMMTIMEOUTS newTimeout = {
@@ -442,7 +444,7 @@  int serialport_read_nonblock(unsigned char *c, unsigned int readcnt, unsigned in
 	int rd_bytes = 0;
 	for (i = 0; i < timeout; i++) {
 		msg_pspew("readcnt %d rd_bytes %d\n", readcnt, rd_bytes);
-#ifdef _WIN32
+#if IS_WINDOWS
 		ReadFile(sp_fd, c + rd_bytes, readcnt - rd_bytes, &rv, NULL);
 		msg_pspew("read %lu bytes\n", rv);
 #else
@@ -466,7 +468,7 @@  int serialport_read_nonblock(unsigned char *c, unsigned int readcnt, unsigned in
 		*really_read = rd_bytes;
 
 	/* restore original blocking behavior */
-#ifdef _WIN32
+#if IS_WINDOWS
 	if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
 		msg_perr_strerror("Could not restore serial port timeout settings: ");
 		ret = -1;
@@ -487,7 +489,7 @@  int serialport_write_nonblock(const unsigned char *buf, unsigned int writecnt, u
 {
 	int ret = 1;
 	/* disable blocked i/o and declare platform-specific variables */
-#ifdef _WIN32
+#if IS_WINDOWS
 	DWORD rv;
 	COMMTIMEOUTS oldTimeout;
 	COMMTIMEOUTS newTimeout = {
@@ -522,7 +524,7 @@  int serialport_write_nonblock(const unsigned char *buf, unsigned int writecnt, u
 	int wr_bytes = 0;
 	for (i = 0; i < timeout; i++) {
 		msg_pspew("writecnt %d wr_bytes %d\n", writecnt, wr_bytes);
-#ifdef _WIN32
+#if IS_WINDOWS
 		WriteFile(sp_fd, buf + wr_bytes, writecnt - wr_bytes, &rv, NULL);
 		msg_pspew("wrote %lu bytes\n", rv);
 #else
@@ -548,7 +550,7 @@  int serialport_write_nonblock(const unsigned char *buf, unsigned int writecnt, u
 		*really_wrote = wr_bytes;
 
 	/* restore original blocking behavior */
-#ifdef _WIN32
+#if IS_WINDOWS
 	if (!SetCommTimeouts(sp_fd, &oldTimeout)) {
 		msg_perr_strerror("Could not restore serial port timeout settings: ");
 		return -1;
diff --git a/serprog.c b/serprog.c
index e52c994..3de0182 100644
--- a/serprog.c
+++ b/serprog.c
@@ -19,8 +19,10 @@ 
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
+#include "platform.h"
+
 #include <stdio.h>
-#ifndef _WIN32 /* stuff (presumably) needed for sockets only */
+#if ! IS_WINDOWS /* stuff (presumably) needed for sockets only */
 #include <stdlib.h>
 #include <unistd.h>
 #include <fcntl.h>
@@ -30,7 +32,7 @@ 
 #include <netinet/tcp.h>
 #include <netdb.h>
 #endif
-#ifdef _WIN32
+#if IS_WINDOWS
 #include <conio.h>
 #else
 #include <termios.h>
@@ -83,7 +85,7 @@  static int sp_opbuf_usage = 0;
 	whether the command is supported before doing it */
 static int sp_check_avail_automatic = 0;
 
-#ifndef WIN32
+#if ! IS_WINDOWS
 static int sp_opensocket(char *ip, unsigned int port)
 {
 	int flag = 1;
@@ -375,7 +377,7 @@  int serprog_init(void)
 	}
 	free(device);
 
-#ifndef _WIN32
+#if !IS_WINDOWS
 	device = extract_programmer_param("ip");
 	if (have_device && device) {
 		msg_perr("Error: Both host and device specified.\n"
diff --git a/udelay.c b/udelay.c
index ee858b8..c03bcc4 100644
--- a/udelay.c
+++ b/udelay.c
@@ -172,7 +172,7 @@  recalibrate:
 /* Not very precise sleep. */
 void internal_sleep(unsigned int usecs)
 {
-#ifdef _WIN32
+#if IS_WINDOWS
 	Sleep((usecs + 999) / 1000);
 #else
 	sleep(usecs / 1000000);
diff --git a/util/ich_descriptors_tool/ich_descriptors_tool.c b/util/ich_descriptors_tool/ich_descriptors_tool.c
index b6c1b12..c3d9ba1 100644
--- a/util/ich_descriptors_tool/ich_descriptors_tool.c
+++ b/util/ich_descriptors_tool/ich_descriptors_tool.c
@@ -35,7 +35,7 @@ 
 /* Some DJGPP builds define __unix__ although they don't support mmap().
  * Cygwin defines __unix__ and supports mmap(), but it does not work well.
  */
-#if !defined(__MSDOS__) && !defined(_WIN32) && (defined(unix) || defined(__unix__) || defined(__unix)) || (defined(__MACH__) && defined(__APPLE__))
+#if !defined(__MSDOS__) && !IS_WINDOWS && (defined(unix) || defined(__unix__) || defined(__unix)) || (defined(__MACH__) && defined(__APPLE__))
 #define HAVE_MMAP 1
 #include <sys/mman.h>
 #endif