Portability fixes and cleanups

Move Mac OS X IOKit/DirectHW availability checks in the Makefile from
compiler check to pciutils check.

Print the compiler error messages for feature detection.

Add DOS libpci in the Makefile includes only if a PCI-based programmer
was requested.

Restrict mmap usage in ich_descriptors_tool to Unix style systems.

Build ich_descriptors_tool with the correct .exe extension on
DOS/Windows.

Build ich_descriptors_tool by default on x86. (Patch by Stefan Tauner)

Print the Windows version instead of "unknown machine" on Windows.

Don't #define our own __DARWIN__, use the standard OS X detection
method.

Update the README.

Add more generated files to svn:ignore

Corresponding to flashrom svn r1567.

Signed-off-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>
Acked-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>
diff --git a/Makefile b/Makefile
index 7d2fda2..774d5b3 100644
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,7 @@
 # This file is part of the flashrom project.
 #
 # Copyright (C) 2005 coresystems GmbH <stepan@coresystems.de>
-# Copyright (C) 2009,2010 Carl-Daniel Hailfinger
+# Copyright (C) 2009,2010,2012 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
@@ -20,6 +20,12 @@
 
 PROGRAM = flashrom
 
+###############################################################################
+# Defaults for the toolchain.
+
+# If you want to cross-compile, just run e.g.
+# make CC=i586-pc-msdosdjgpp-gcc
+# You may have to specify STRIP/AR/RANLIB as well.
 CC      ?= gcc
 STRIP   ?= strip
 INSTALL = install
@@ -31,12 +37,17 @@
 AR      ?= ar
 RANLIB  ?= ranlib
 
+# If your compiler spits out excessive warnings, run make WARNERROR=no
+# You shouldn't have to change this flag.
 WARNERROR ?= yes
 
 ifeq ($(WARNERROR), yes)
 CFLAGS += -Werror
 endif
 
+###############################################################################
+# General OS/architecture specific settings.
+
 # HOST_OS is only used to work around local toolchain issues.
 HOST_OS	?= $(shell uname)
 ifeq ($(HOST_OS), MINGW32_NT-5.1)
@@ -47,7 +58,7 @@
 STRIP_ARGS = -s
 endif
 
-# Determine the destination processor architecture.
+# Determine the destination OS.
 # IMPORTANT: The following line must be placed before TARGET_OS is ever used
 # (of course), but should come after any lines setting CC because the line
 # below uses CC itself.
@@ -55,25 +66,27 @@
 
 ifeq ($(TARGET_OS), Darwin)
 CPPFLAGS += -I/opt/local/include -I/usr/local/include
-# DirectHW framework can be found in the DirectHW library.
-LDFLAGS += -framework IOKit -framework DirectHW -L/opt/local/lib -L/usr/local/lib
+LDFLAGS += -L/opt/local/lib -L/usr/local/lib
 endif
+
 ifeq ($(TARGET_OS), FreeBSD)
 CPPFLAGS += -I/usr/local/include
 LDFLAGS += -L/usr/local/lib
 endif
+
 ifeq ($(TARGET_OS), OpenBSD)
 CPPFLAGS += -I/usr/local/include
 LDFLAGS += -L/usr/local/lib
 endif
+
 ifeq ($(TARGET_OS), DOS)
 EXEC_SUFFIX := .exe
-CPPFLAGS += -I../libgetopt -I../libpci/include
+CPPFLAGS += -I../libgetopt
 # DJGPP has odd uint*_t definitions which cause lots of format string warnings.
 CPPFLAGS += -Wno-format
 # FIXME Check if we can achieve the same effect with -L../libgetopt -lgetopt
 LIBS += ../libgetopt/libgetopt.a
-# Bus Pirate, Serprog and Pony-SPI are not supported under DOS (missing serial support).
+# Bus Pirate, Serprog and PonyProg are not supported under DOS (missing serial support).
 ifeq ($(CONFIG_BUSPIRATE_SPI), yes)
 UNSUPPORTED_FEATURES += CONFIG_BUSPIRATE_SPI=yes
 else
@@ -263,18 +276,23 @@
 endif
 endif
 
+###############################################################################
+# Flash chip drivers and bus support infrastructure.
+
 CHIP_OBJS = jedec.o stm50flw0x0x.o w39.o w29ee011.o \
 	sst28sf040.o m29f400bt.o 82802ab.o pm49fl00x.o \
 	sst49lfxxxc.o sst_fwhub.o flashchips.o spi.o spi25.o \
 	a25.o at25.o opaque.o sfdp.o en29lv640b.o
 
-LIB_OBJS = layout.o
+###############################################################################
+# Library code.
 
-CLI_OBJS = flashrom.o cli_classic.o cli_output.o print.o
+LIB_OBJS = layout.o flashrom.o udelay.o programmer.o
 
-PROGRAMMER_OBJS = udelay.o programmer.o
+###############################################################################
+# Frontend related stuff.
 
-all: pciutils features $(PROGRAM)$(EXEC_SUFFIX)
+CLI_OBJS = cli_classic.o cli_output.o print.o
 
 # Set the flashrom version string from the highest revision number
 # of the checked out flashrom files.
@@ -375,6 +393,9 @@
 endif
 endif
 
+###############################################################################
+# Programmer drivers and programmer support infrastructure.
+
 ifeq ($(CONFIG_INTERNAL), yes)
 FEATURE_CFLAGS += -D'CONFIG_INTERNAL=1'
 PROGRAMMER_OBJS += processor_enable.o chipset_enable.o board_enable.o cbtable.o dmi.o internal.o
@@ -530,12 +551,19 @@
 else
 ifeq ($(TARGET_OS), DOS)
 # FIXME There needs to be a better way to do this
+CPPFLAGS += -I../libpci/include
 LIBS += ../libpci/lib/libpci.a
 else
 LIBS += -lpci
 ifeq ($(TARGET_OS), OpenBSD)
 # For (i386|amd64)_iopl(2).
 LIBS += -l$(shell uname -m)
+else
+ifeq ($(TARGET_OS), Darwin)
+# DirectHW framework can be found in the DirectHW library.
+LIBS += -framework IOKit -framework DirectHW 
+else
+endif
 endif
 endif
 endif
@@ -554,6 +582,11 @@
 LIBFLASHROM_OBJS = $(CHIP_OBJS) $(PROGRAMMER_OBJS) $(LIB_OBJS)
 OBJS = $(CLI_OBJS) $(LIBFLASHROM_OBJS)
 
+all: pciutils features $(PROGRAM)$(EXEC_SUFFIX)
+ifeq ($(ARCH), x86)
+	@+$(MAKE) -C util/ich_descriptors_tool/ TARGET_OS=$(TARGET_OS) EXEC_SUFFIX=$(EXEC_SUFFIX)
+endif
+
 $(PROGRAM)$(EXEC_SUFFIX): $(OBJS)
 	$(CC) $(LDFLAGS) -o $(PROGRAM)$(EXEC_SUFFIX) $(OBJS) $(FEATURE_LIBS) $(LIBS)
 
@@ -574,6 +607,7 @@
 # We don't use EXEC_SUFFIX here because we want to clean everything.
 clean:
 	rm -f $(PROGRAM) $(PROGRAM).exe libflashrom.a *.o *.d
+	@+$(MAKE) -C util/ich_descriptors_tool/ clean
 
 distclean: clean
 	rm -f .features .libdeps
@@ -597,7 +631,7 @@
 compiler: featuresavailable
 	@printf "Checking for a C compiler... "
 	@echo "$$COMPILER_TEST" > .test.c
-	@$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) .test.c -o .test$(EXEC_SUFFIX) >/dev/null 2>&1 &&	\
+	@$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) .test.c -o .test$(EXEC_SUFFIX) >/dev/null &&	\
 		echo "found." || ( echo "not found."; \
 		rm -f .test.c .test$(EXEC_SUFFIX); exit 1)
 	@rm -f .test.c .test$(EXEC_SUFFIX)
@@ -631,17 +665,17 @@
 pciutils: compiler
 	@printf "Checking for libpci headers... "
 	@echo "$$LIBPCI_TEST" > .test.c
-	@$(CC) -c $(CPPFLAGS) $(CFLAGS) .test.c -o .test.o >/dev/null 2>&1 &&		\
+	@$(CC) -c $(CPPFLAGS) $(CFLAGS) .test.c -o .test.o >/dev/null &&		\
 		echo "found." || ( echo "not found."; echo;			\
 		echo "Please install libpci headers (package pciutils-devel).";	\
 		echo "See README for more information."; echo;			\
 		rm -f .test.c .test.o; exit 1)
 	@printf "Checking if libpci is present and sufficient... "
 	@printf "" > .libdeps
-	@$(CC) $(LDFLAGS) .test.o -o .test$(EXEC_SUFFIX) $(LIBS) >/dev/null 2>&1 &&				\
+	@$(CC) $(LDFLAGS) .test.o -o .test$(EXEC_SUFFIX) $(LIBS) >/dev/null &&				\
 		echo "yes." || ( echo "no.";							\
 		printf "Checking if libz+libpci are present and sufficient...";	\
-		$(CC) $(LDFLAGS) .test.o -o .test$(EXEC_SUFFIX) $(LIBS) -lz >/dev/null 2>&1 &&		\
+		$(CC) $(LDFLAGS) .test.o -o .test$(EXEC_SUFFIX) $(LIBS) -lz >/dev/null &&		\
 		( echo "yes."; echo "NEEDLIBZ := yes" > .libdeps ) || ( echo "no."; echo;	\
 		echo "Please install libpci (package pciutils) and/or libz.";			\
 		echo "See README for more information."; echo;				\
@@ -657,10 +691,13 @@
 # If a user does not explicitly request a non-working feature, we should
 # silently disable it. However, if a non-working (does not compile) feature
 # is explicitly requested, we should bail out with a descriptive error message.
-ifeq ($(UNSUPPORTED_FEATURES), )
+# We also have to check that at least one programmer driver is enabled.
 featuresavailable:
-else
-featuresavailable:
+ifeq ($(PROGRAMMER_OBJS),)
+	@echo "You have to enable at least one programmer driver!"
+	@false
+endif
+ifneq ($(UNSUPPORTED_FEATURES), )
 	@echo "The following features are unavailable on your machine: $(UNSUPPORTED_FEATURES)"
 	@false
 endif
diff --git a/README b/README
index f88b3b5..8e812be 100644
--- a/README
+++ b/README
@@ -93,14 +93,18 @@
 To compile and run on Darwin/Mac OS X:
 
  Install DirectHW from coresystems GmbH.
- DirectHW is available at http://www.coresystems.de/en/directhw .
+ DirectHW is available at http://www.coreboot.org/DirectHW .
 
 To cross-compile on Linux for DOS:
 
- Get RPMs of the cross compiler from the DJGPP site and install them:
+ Get packages of the DJGPP cross compiler and install them:
+ djgpp-filesystem djgpp-gcc djgpp-cpp djgpp-runtime djgpp-binutils
+ As an alternative, the DJGPP web site offers packages for download as well:
  djcross-binutils-2.19.1-10ap.i386.rpm
  djcross-gcc-4.3.2-8ap.i686.rpm
  djcrx-2.04pre_20090725-13ap.i386.rpm
+ The cross toolchain packages for your distribution may have slightly different
+ names (look for packages named *djgpp*).
  Download pciutils 3.1.5 and apply http://assembler.cz/flashrom/pciutils.patch
  Download and compile http://assembler.cz/flashrom/libgetopt/
  Compile pciutils, see README.DJGPP for instructions.
@@ -108,7 +112,7 @@
  ../libpci should contain pciutils source and binaries.
  ../libgetopt should contain getopt.a from libgetopt.
  Run either (change settings where appropriate)
- make CC=i586-pc-msdosdjgpp-gcc STRIP=i586-pc-msdosdjgpp-strip OS_ARCH=DOS
+ make CC=i586-pc-msdosdjgpp-gcc STRIP=i586-pc-msdosdjgpp-strip
  or (above settings hardcoded)
  make djgpp-dos
  You might have to add WARNERROR=no to the make command line.
@@ -116,17 +120,22 @@
  http://homer.rice.edu/~sandmann/cwsdpmi/csdpmi7b.zip and make sure
  CWSDPMI.EXE is in the current directory.
 
+To cross-compile on Linux for Windows:
+
+ Get packages of the MinGW cross compiler and install them:
+ mingw32-filesystem mingw32-cross-cpp mingw32-cross-binutils mingw32-cross-gcc
+ mingw32-runtime mingw32-headers
+ The cross toolchain packages for your distribution may have slightly different
+ names (look for packages named *mingw*).
+ PCI-based programmers (internal etc.) are not supported on Windows.
+ Run (change CC= and STRIP= settings where appropriate)
+ make CC=i686-w64-mingw32-gcc STRIP=i686-w64-mingw32-strip
+
 Processor architecture dependent features:
 
- On non-x86 architectures you have to disable a few programmers because they
- use port-based I/O which is not directly available on non-x86. Please add
- CONFIG_RAYER_SPI=no CONFIG_NIC3COM=no CONFIG_ATAHPT=no CONFIG_NICREALTEK=no \
- CONFIG_NICNATSEMI=no
- as parameters to the "make" invocation.
- Besides that, the internal programmer is only supported on x86 and MIPS. On
- other architectures, please add
- CONFIG_INTERNAL=no
- as parameter to the "make" invocation.
+ On non-x86 architectures a few programmers don't work (yet) because they
+ use port-based I/O which is not directly available on non-x86. Those
+ programmers will be disabled automatically if you run "make".
 
 Installation
 ------------
diff --git a/cbtable.c b/cbtable.c
index 4495d9d..b4fb809 100644
--- a/cbtable.c
+++ b/cbtable.c
@@ -211,7 +211,7 @@
 	struct lb_header *lb_table;
 	struct lb_record *rec, *last;
 
-#ifdef __DARWIN__
+#if defined(__MACH__) && defined(__APPLE__)
 	/* This is a hack. DirectHW fails to map physical address 0x00000000.
 	 * Why?
 	 */
diff --git a/flashrom.c b/flashrom.c
index 04e9934..a925379 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -60,10 +60,6 @@
 /* Is writing allowed with this programmer? */
 int programmer_may_write;
 
-#if CONFIG_INTERNAL+CONFIG_DUMMY+CONFIG_NIC3COM+CONFIG_NICREALTEK+CONFIG_NICNATSEMI+CONFIG_GFXNVIDIA+CONFIG_DRKAISER+CONFIG_SATASII+CONFIG_ATAHPT+CONFIG_FT2232_SPI+CONFIG_SERPROG+CONFIG_BUSPIRATE_SPI+CONFIG_DEDIPROG+CONFIG_RAYER_SPI+CONFIG_PONY_SPI+CONFIG_NICINTEL+CONFIG_NICINTEL_SPI+CONFIG_OGP_SPI+CONFIG_SATAMV+CONFIG_LINUX_SPI < 1
-#error You have to enable at least one programmer!
-#endif
-
 const struct programmer_entry programmer_table[] = {
 #if CONFIG_INTERNAL == 1
 	{
@@ -1490,10 +1486,35 @@
 
 void print_sysinfo(void)
 {
-#if HAVE_UTSNAME == 1
-	struct utsname osinfo;
-	uname(&osinfo);
+#ifdef _WIN32
+	SYSTEM_INFO si;
+	OSVERSIONINFOEX osvi;
 
+	memset(&si, 0, sizeof(SYSTEM_INFO));
+	memset(&osvi, 0, sizeof(OSVERSIONINFOEX));
+	msg_ginfo(" on Windows");
+	/* Tell Windows which version of the structure we want. */
+	osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
+	if (GetVersionEx((OSVERSIONINFO*) &osvi))
+		msg_ginfo(" %lu.%lu", osvi.dwMajorVersion, osvi.dwMinorVersion);
+	else
+		msg_ginfo(" unknown version");
+	GetSystemInfo(&si);
+	switch (si.wProcessorArchitecture) {
+	case PROCESSOR_ARCHITECTURE_AMD64:
+		msg_ginfo(" (x86_64)");
+		break;
+	case PROCESSOR_ARCHITECTURE_INTEL:
+		msg_ginfo(" (x86)");
+		break;
+	default:
+		msg_ginfo(" (unknown arch)");
+		break;
+	}
+#elif HAVE_UTSNAME == 1
+	struct utsname osinfo;
+
+	uname(&osinfo);
 	msg_ginfo(" on %s %s (%s)", osinfo.sysname, osinfo.release,
 		  osinfo.machine);
 #else
diff --git a/hwaccess.h b/hwaccess.h
index 1e276ec..12496bb 100644
--- a/hwaccess.h
+++ b/hwaccess.h
@@ -180,10 +180,6 @@
 #include <asm/sunddi.h>
 #endif
 
-#if (defined(__MACH__) && defined(__APPLE__))
-#define __DARWIN__
-#endif
-
 /* Clarification about OUTB/OUTW/OUTL argument order:
  * OUT[BWL](val, port)
  */
@@ -203,7 +199,7 @@
   #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(__DARWIN__)
+#if defined(__MACH__) && defined(__APPLE__)
     /* Header is part of the DirectHW library. */
     #include <DirectHW/DirectHW.h>
     #define off64_t off_t
@@ -303,7 +299,7 @@
   #endif
 #endif
 
-#if !defined(__DARWIN__) && !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && !defined(__DragonFly__) && !defined(__LIBPAYLOAD__)
+#if !(defined(__MACH__) && defined(__APPLE__)) && !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && !defined(__DragonFly__) && !defined(__LIBPAYLOAD__)
 typedef struct { uint32_t hi, lo; } msr_t;
 msr_t rdmsr(int addr);
 int wrmsr(int addr, msr_t msr);
diff --git a/physmap.c b/physmap.c
index eca6760..5aa9874 100644
--- a/physmap.c
+++ b/physmap.c
@@ -129,7 +129,7 @@
 void cleanup_cpu_msr(void)
 {
 }
-#elif defined(__DARWIN__)
+#elif defined(__MACH__) && defined(__APPLE__)
 
 #define MEM_DEV "DirectHW"
 
@@ -468,7 +468,7 @@
 
 #else
 
-#ifdef __DARWIN__
+#if defined(__MACH__) && defined(__APPLE__)
 int setup_cpu_msr(int cpu)
 {
 	// Always succeed for now
diff --git a/programmer.h b/programmer.h
index 5109ed9..723374d 100644
--- a/programmer.h
+++ b/programmer.h
@@ -240,6 +240,7 @@
 
 /* print.c */
 #if CONFIG_NIC3COM+CONFIG_NICREALTEK+CONFIG_NICNATSEMI+CONFIG_GFXNVIDIA+CONFIG_DRKAISER+CONFIG_SATASII+CONFIG_ATAHPT+CONFIG_NICINTEL+CONFIG_NICINTEL_SPI+CONFIG_OGP_SPI+CONFIG_SATAMV >= 1
+/* Not needed for CONFIG_INTERNAL, but for all other PCI-based programmers. */
 void print_supported_pcidevs(const struct pcidev_status *devs);
 #endif
 
@@ -637,7 +638,7 @@
 #endif
 
 /* serial.c */
-#if _WIN32
+#ifdef _WIN32
 typedef HANDLE fdtype;
 #else
 typedef int fdtype;
diff --git a/util/ich_descriptors_tool/Makefile b/util/ich_descriptors_tool/Makefile
index 1af90ce..d8a79f5 100644
--- a/util/ich_descriptors_tool/Makefile
+++ b/util/ich_descriptors_tool/Makefile
@@ -15,11 +15,16 @@
 CFLAGS += -D ICH_DESCRIPTORS_FROM_DUMP
 CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
 
+ifeq ($(TARGET_OS), DOS)
+# DJGPP has odd uint*_t definitions which cause lots of format string warnings.
+CFLAGS += -Wno-format
+endif
+
 OBJ = $(OBJATH)/$(SRC:%.c=%.o)
 
 SHAREDOBJ = $(OBJATH)/$(notdir $(SHAREDSRC:%.c=%.o))
 
-all:$(PROGRAM)
+all:$(PROGRAM)$(EXEC_SUFFIX)
 
 $(OBJ): $(OBJATH)/%.o : %.c
 	$(CC) $(CFLAGS) -o $@ -c $<
@@ -29,11 +34,11 @@
 $(SHAREDOBJ): $(OBJATH)/%.o : $(SHAREDSRCDIR)/%.c
 	$(CC) $(CFLAGS) -o $@ -c $<
 
-$(PROGRAM): $(OBJ) $(SHAREDOBJ)
-	$(CC) -o $(PROGRAM) $(OBJ) $(SHAREDOBJ)
+$(PROGRAM)$(EXEC_SUFFIX): $(OBJ) $(SHAREDOBJ)
+	$(CC) -o $(PROGRAM)$(EXEC_SUFFIX) $(OBJ) $(SHAREDOBJ)
 
 clean:
-	rm -f $(PROGRAM)
+	rm -f $(PROGRAM) $(PROGRAM).exe
 	rm -rf $(DEPPATH) $(OBJATH)
 
 # Include the dependency files.
diff --git a/util/ich_descriptors_tool/ich_descriptors_tool.c b/util/ich_descriptors_tool/ich_descriptors_tool.c
index a1bce1b..1ca970a 100644
--- a/util/ich_descriptors_tool/ich_descriptors_tool.c
+++ b/util/ich_descriptors_tool/ich_descriptors_tool.c
@@ -25,7 +25,6 @@
 #include <stdio.h>
 #include <stdint.h>
 #include <stdlib.h>
-#include <sys/mman.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
@@ -33,6 +32,13 @@
 #include <string.h>
 #include <errno.h>
 #include "ich_descriptors.h"
+/* 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__))
+#define HAVE_MMAP 1
+#include <sys/mman.h>
+#endif
 
 static void dump_file(const char *basename, const uint32_t *dump, unsigned int len, struct ich_desc_region *reg, unsigned int i)
 {
@@ -161,16 +167,17 @@
 	if (len < 0)
 		usage(argv, "Seeking to the end of the file failed");
 
+#ifdef HAVE_MMAP
 	buf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
-	if (buf == (void *) -1) {
+	if (buf == (void *) -1)
+#endif
+	{
 		/* fallback for stupid OSes like cygwin */
-		int ret;
 		buf = malloc(len);
 		if (!buf)
 			usage(argv, "Could not allocate memory");
 		lseek(fd, 0, SEEK_SET);
-		ret = read(fd, buf, len);
-		if (ret != len)
+		if (len != read(fd, buf, len))
 			usage(argv, "Seeking to the end of the file failed");
 	}
 	printf("The flash image has a size of %d [0x%x] bytes.\n", len, len);