vgabios: fix compilation after x86emu changes

This utility links in coreboot code, and has been broken for a while
again after removing some hacks from coreboot. I hadn't realized how
bad it was broken last time, and since most of this stuff is still
in a pretty bad shape, I decided to throw all of the changes together.

Signed-off-by: Stefan Reinauer <stefan.reinauer@coreboot.org>

Change-Id: If3e4399b1b0e947433b97caa29962ef66ea2993d
Reviewed-on: http://review.coreboot.org/11736
Tested-by: build bot (Jenkins)
Reviewed-by: Patrick Georgi <pgeorgi@google.com>
diff --git a/util/vgabios/Makefile b/util/vgabios/Makefile
index 366606c..520779c 100644
--- a/util/vgabios/Makefile
+++ b/util/vgabios/Makefile
@@ -6,35 +6,57 @@
 # /usr/lib/...
 #
 
-CC       =  gcc
-CFLAGS   = -O2 -g -fomit-frame-pointer
+TOP     ?= ../..
+
+CC      ?=  gcc
+CFLAGS  ?= -O2 -g -fomit-frame-pointer
+
 CFLAGS  += -Wall -Wundef -Wstrict-prototypes -Wmissing-prototypes
-CFLAGS  += -Wwrite-strings -Wredundant-decls -Wno-trigraphs
-CFLAGS  += -Wstrict-aliasing -Wshadow -Wextra
+CFLAGS  += -Wwrite-strings -Wredundant-decls -Wstrict-aliasing -Wshadow -Wextra
 
-INCLUDES = -Iinclude -I../../src/device/oprom/include/ -I../../src --include include/stdtypes.h
+# TODO check host architecture
+CBCFLAGS  = -DCONFIG_ARCH_X86=1 -Wno-sign-compare -Wno-unused-but-set-variable -Wno-unused-parameter
 
-INTOBJS  = int10.o int15.o int16.o int1a.o inte6.o
-X86EMUOBJS  = sys.o decode.o ops.o ops2.o prim_ops.o fpu.o debug.o
-OBJS  =  testbios.o helper_exec.o helper_mem.o $(INTOBJS) $(X86EMUOBJS)
+INCLUDES  = -Iinclude -I$(TOP)/src/device/oprom/include/
+CBINCLUDES = -I$(TOP)/src --include include/stdtypes.h
+CBINCLUDES += --include $(TOP)/src/commonlib/include/commonlib/loglevel.h
+CBINCLUDES += -include stdio.h
 
-# user space pci is the only option right now.
-OBJS += pci-userspace.o
+SOURCE  = int10.c int15.c int16.c int1a.c inte6.c testbios.c
+SOURCE += helper_exec.c helper_mem.c pci-userspace.c
+
+X86EMU  = sys.c decode.c ops.c ops2.c prim_ops.c fpu.c debug.c
+X86EMU_DIR = $(TOP)/src/device/oprom/x86emu
+X86EMU_SOURCE = $(addprefix $(X86EMU_DIR)/, $(X86EMU))
+OBJECTS:=$(SOURCE:.c=.o) $(X86EMU:.c=.o)
 
 LIBS=-lpci
 
-all: testbios
+all: dep testbios
 
-testbios: $(OBJS)
+testbios: $(OBJECTS)
+	printf "    LINK  $(notdir $@)\n"
 	$(CC) $(CFLAGS) -o $@ $^ $(LIBS)
 
-helper_exec.o: helper_exec.c test.h
+dep: $(SOURCE) $(X86EMU_SOURCE) Makefile
+	$(CC) $(CFLAGS) $(INCLUDES) -MM $(SOURCE) > .dependencies
+	$(CC) $(CFLAGS) $(INCLUDES) $(CBCFLAGS) $(CBINCLUDES) -MM $(X86EMU_SOURCE) >> .dependencies
 
 clean:
 	rm -f *.o *~ testbios
 
-%.o: ../../src/device/oprom/x86emu/%.c
-	$(CC) $(CFLAGS) $(INCLUDES) -include stdio.h -c -o $@ $^
+distclean: clean
+	rm -f .dependencies
+
+%.o: $(X86EMU_DIR)/%.c
+	printf "    CC    (x86emu) $(notdir $<)\n"
+	$(CC) $(CFLAGS) $(CBCFLAGS) $(INCLUDES) $(CBINCLUDES) -c -o $@ $<
 
 %.o: %.c
+	printf "    CC    $(notdir $<)\n"
 	$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $<
+
+.PHONY: all clean distclean
+.SILENT:
+
+-include .dependencies
diff --git a/util/vgabios/helper_exec.c b/util/vgabios/helper_exec.c
index 73100d9..4d6aae5 100644
--- a/util/vgabios/helper_exec.c
+++ b/util/vgabios/helper_exec.c
@@ -14,21 +14,13 @@
  * in xf86EnableIO(). Otherwise we won't trap
  * on PIO.
  */
-#include <x86emu/x86emu.h>
-#include "helper_exec.h"
-#include "test.h"
 #include <sys/io.h>
 #include <sys/time.h>
 #include <stdio.h>
-
-
-int port_rep_inb(u16 port, u32 base, int d_f, u32 count);
-u8 x_inb(u16 port);
-u16 x_inw(u16 port);
-void x_outb(u16 port, u8 val);
-void x_outw(u16 port, u16 val);
-u32 x_inl(u16 port);
-void x_outl(u16 port, u32 val);
+#include <stdtypes.h>
+#include <x86emu/x86emu.h>
+#include "helper_exec.h"
+#include "testbios.h"
 
 /* general software interrupt handler */
 u32 getIntVect(int num)
@@ -59,6 +51,8 @@
 	return 1;
 }
 
+#if 0
+
 int port_rep_inb(u16 port, u32 base, int d_f, u32 count)
 {
 	register int inc = d_f ? -1 : 1;
@@ -125,6 +119,8 @@
 	return dst - base;
 }
 
+#endif
+
 u8 x_inb(u16 port)
 {
 	u8 val;
@@ -175,6 +171,7 @@
 	outl(val, port);
 }
 
+#if 0
 u8 Mem_rb(int addr)
 {
 	return (*current->mem->rb) (current, addr);
@@ -204,6 +201,7 @@
 {
 	(*current->mem->wl) (current, addr, val);
 }
+#endif
 
 void getsecs(unsigned long *sec, unsigned long *usec)
 {
diff --git a/util/vgabios/helper_exec.h b/util/vgabios/helper_exec.h
index 2657b6e..7115ffb 100644
--- a/util/vgabios/helper_exec.h
+++ b/util/vgabios/helper_exec.h
@@ -1,2 +1,32 @@
+#ifndef __HELPER_EXEC_H__
+#define __HELPER_EXEC_H__
+
 u32 getIntVect(int num);
 int run_bios_int(int num);
+void pushw(u16 val);
+
+int port_rep_inb(u16 port, u32 base, int d_f, u32 count);
+int port_rep_inw(u16 port, u32 base, int d_f, u32 count);
+int port_rep_inl(u16 port, u32 base, int d_f, u32 count);
+int port_rep_outb(u16 port, u32 base, int d_f, u32 count);
+int port_rep_outw(u16 port, u32 base, int d_f, u32 count);
+int port_rep_outl(u16 port, u32 base, int d_f, u32 count);
+
+u8 x_inb(u16 port);
+u16 x_inw(u16 port);
+void x_outb(u16 port, u8 val);
+void x_outw(u16 port, u16 val);
+u32 x_inl(u16 port);
+void x_outl(u16 port, u32 val);
+
+u8 Mem_rb(int addr);
+u16 Mem_rw(int addr);
+u32 Mem_rl(int addr);
+
+void Mem_wb(int addr, u8 val);
+void Mem_ww(int addr, u16 val);
+void Mem_wl(int addr, u32 val);
+void getsecs(unsigned long *sec, unsigned long *usec);
+u8 bios_checksum(u8 * start, int size);
+
+#endif
diff --git a/util/vgabios/include/console/console.h b/util/vgabios/include/console/console.h
index e74cbb3..e52dbf7 100644
--- a/util/vgabios/include/console/console.h
+++ b/util/vgabios/include/console/console.h
@@ -1,4 +1,6 @@
 #ifndef _CONSOLE_CONSOLE_H
 #define _CONSOLE_CONSOLE_H
 #define CONFIG_X86EMU_DEBUG 1
+
+int printk(int msg_level, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
 #endif
diff --git a/util/vgabios/int10.c b/util/vgabios/int10.c
index bccb3d9..21ba7fb 100644
--- a/util/vgabios/int10.c
+++ b/util/vgabios/int10.c
@@ -1,9 +1,7 @@
 #include <stdio.h>
-#include "test.h"
-#include "pci.h"
+#include <stdtypes.h>
+#include "testbios.h"
 
-void x86emu_dump_xregs(void);
-extern ptr current;
 extern int verbose;
 
 
@@ -16,7 +14,7 @@
  * arise.  What are "Not Implemented" throughout are video memory accesses.
  * Also, very little input validity checking is done here.
  */
-int int42_handler()
+int int42_handler(void)
 {
 #if 0
 	if (verbose && X86_AH != 0x0e) {
diff --git a/util/vgabios/int15.c b/util/vgabios/int15.c
index 239b6be..a1235bb 100644
--- a/util/vgabios/int15.c
+++ b/util/vgabios/int15.c
@@ -1,12 +1,11 @@
 #include <stdio.h>
-#include "test.h"
-
-void x86emu_dump_xregs();
+#include <stdtypes.h>
+#include "testbios.h"
 
 int int15_handler(void)
 {
 	printf("\nint15 encountered.\n");
-	//x86emu_dump_xregs();
+	x86emu_dump_xregs();
 	X86_EAX = 0;
 	return 1;
 }
diff --git a/util/vgabios/int16.c b/util/vgabios/int16.c
index f3a9719..06df826 100644
--- a/util/vgabios/int16.c
+++ b/util/vgabios/int16.c
@@ -1,7 +1,9 @@
 #include <stdio.h>
+#include "testbios.h"
 
 int int16_handler(void)
 {
 	printf("\nint16: keyboard not supported right now.\n");
+	x86emu_dump_xregs();
 	return 1;
 }
diff --git a/util/vgabios/int1a.c b/util/vgabios/int1a.c
index bd9a24c..9fcff8a 100644
--- a/util/vgabios/int1a.c
+++ b/util/vgabios/int1a.c
@@ -1,5 +1,6 @@
 #include <stdio.h>
-#include "test.h"
+#include <stdtypes.h>
+#include "testbios.h"
 #include "pci-userspace.h"
 
 #define DEBUG_INT1A
@@ -8,13 +9,11 @@
 #define DEVICE_NOT_FOUND        0x86
 #define BAD_REGISTER_NUMBER     0x87
 
-void x86emu_dump_xregs(void);
 extern int verbose;
 
-
-int int1A_handler()
+int int1A_handler(void)
 {
-	PCITAG tag;
+	PCITAG tag = NULL;
 	pciVideoPtr pvp = NULL;
 
 	if (verbose) {
@@ -40,7 +39,7 @@
 		if (X86_DX == pvp->vendor_id && X86_CX == pvp->device_id && X86_ESI == 0) {
 			X86_EAX = X86_AL | (SUCCESSFUL << 8);
 			X86_EFLAGS &= ~((unsigned long) 0x01);	/* clear carry flag */
-			X86_EBX = pciSlotBX(pvp);
+			X86_EBX = pciSlotBX(tag); // XXX used to be pvp, but both are NULL
 		}
 #ifdef SHOW_ALL_DEVICES
 		else if ((pvp = xf86FindPciDeviceVendor(X86_EDX, X86_ECX, X86_ESI, pvp))) {
diff --git a/util/vgabios/inte6.c b/util/vgabios/inte6.c
index 0f6a578..19b6110 100644
--- a/util/vgabios/inte6.c
+++ b/util/vgabios/inte6.c
@@ -1,6 +1,7 @@
 #include <stdio.h>
+#include "testbios.h"
 
-int intE6_handler()
+int intE6_handler(void)
 {
 #if 0
 	pciVideoPtr pvp;
@@ -14,5 +15,6 @@
 	X86_ES = 0;		/* standard pc es */
 #endif
 	printf("intE6 not supported right now.\n");
+	x86emu_dump_xregs();
 	return 1;
 }
diff --git a/util/vgabios/pci-userspace.c b/util/vgabios/pci-userspace.c
index bc71a61..796933e 100644
--- a/util/vgabios/pci-userspace.c
+++ b/util/vgabios/pci-userspace.c
@@ -1,6 +1,5 @@
 #include <stdio.h>
-#include <pci/pci.h>
-#include "pci.h"
+#include "pci-userspace.h"
 
 #ifdef PCI_LIB_VERSION
 #define LIBPCI_CHECK_VERSION(major,minor,micro) \
diff --git a/util/vgabios/pci-userspace.h b/util/vgabios/pci-userspace.h
index 0944330..103a9ef 100644
--- a/util/vgabios/pci-userspace.h
+++ b/util/vgabios/pci-userspace.h
@@ -1,9 +1,13 @@
-#include "pci.h"
+#ifndef __PCI_USERSPACE_H__
+#define __PCI_USERSPACE_H__
+
+#include <pci/pci.h>
 
 typedef unsigned long pciaddr_t;
 typedef u8 byte;
 typedef u16 word;
 
+#if 0
 struct pci_dev {
 	struct pci_dev *next;	/* Next device in the chain */
 	word bus;		/* Higher byte can select host bridges */
@@ -32,7 +36,7 @@
 	int bus, slot, func;	/* -1 = ANY */
 	int vendor, device;
 };
-
+#endif
 
 #define PCITAG struct pci_filter *
 #define pciVideoPtr struct pci_dev *
@@ -44,7 +48,8 @@
 
 
 PCITAG findPci(unsigned short bx);
-u32 pciSlotBX(pciVideoPtr pvp);
+//u32 pciSlotBX(pciVideoPtr pvp);
+u32 pciSlotBX(PCITAG tag);
 
 void pciWriteLong(PCITAG tag, u32 idx, u32 data);
 void pciWriteWord(PCITAG tag, u32 idx, u16 data);
@@ -53,3 +58,5 @@
 u32 pciReadLong(PCITAG tag, u32 idx);
 u16 pciReadWord(PCITAG tag, u32 idx);
 u8 pciReadByte(PCITAG tag, u32 idx);
+
+#endif
diff --git a/util/vgabios/pci.h b/util/vgabios/pci.h
deleted file mode 100644
index e51ebe4..0000000
--- a/util/vgabios/pci.h
+++ /dev/null
@@ -1,2 +0,0 @@
-void x_outb(u16 port, u8 val);
-#define outb x_outb
diff --git a/util/vgabios/testbios.c b/util/vgabios/testbios.c
index a6e121e..8c9783f 100644
--- a/util/vgabios/testbios.c
+++ b/util/vgabios/testbios.c
@@ -6,26 +6,18 @@
 #include <fcntl.h>
 #include <getopt.h>
 #include <string.h>
+#include <stdarg.h>
 
+#include <stdtypes.h>
 #define die(x) { perror(x); exit(1); }
 #define warn(x) { perror(x);  }
 
 #include <x86emu/x86emu.h>
+#include <console/console.h>
 #include "helper_exec.h"
-#include "test.h"
+#include "testbios.h"
 #include "pci-userspace.h"
-
-void x86emu_dump_xregs(void);
-int int15_handler(void);
-int int16_handler(void);
-int int1A_handler(void);
-#ifndef _PC
-int int42_handler(void);
-#endif
-int intE6_handler(void);
-
-void pushw(u16 val);
-
+int X86EMU_set_debug(int debug);
 unsigned short get_device(char *arg_val);
 
 extern int teststart, testend;
@@ -39,7 +31,7 @@
 
 /* Interrupt multiplexer */
 
-void do_int(int num)
+static void do_int(int num)
 {
 	int ret = 0;
 
@@ -84,7 +76,7 @@
 	}
 }
 
-unsigned char *mapitin(char *file, off_t where, size_t size)
+static unsigned char *mapitin(char *file, off_t where, size_t size)
 {
 	void *z;
 
@@ -101,21 +93,13 @@
 
 }
 
-u8 x_inb(u16 port);
-u16 x_inw(u16 port);
-void x_outb(u16 port, u8 val);
-void x_outw(u16 port, u16 val);
-u32 x_inl(u16 port);
-void x_outl(u16 port, u32 val);
-
-
 X86EMU_pioFuncs myfuncs = {
 	x_inb, x_inw, x_inl,
 	x_outb, x_outw, x_outl
 };
 
 
-void usage(char *name)
+static void usage(char *name)
 {
 	printf
 	    ("Usage: %s [-c codesegment] [-s size] [-b base] [-i ip] [-t] <filename> ... \n",
@@ -129,7 +113,7 @@
 	int i, c, trace = 0;
 	unsigned char *cp;
 	char *filename;
-	size_t size = 0;
+	ssize_t size = 0;
 	int base = 0;
 	int have_size = 0, have_base = 0, have_ip = 0, have_cs = 0;
 	int have_devfn = 0;
@@ -138,9 +122,6 @@
 	unsigned char *fsegptr;
 	unsigned short initialip = 0, initialcs = 0, devfn = 0;
 	X86EMU_intrFuncs intFuncs[256];
-	void X86EMU_setMemBase(void *base, size_t size);
-	void x86emu_dump_xregs(void);
-	int X86EMU_set_debug(int debug);
 	int debugflag = 0;
 
 	const char *optstring = "vh?b:i:c:s:tpd:";
@@ -192,7 +173,6 @@
 			have_size = 1;
 			break;
 		case 'p':
-			printf("Parsing rom images not implemented.\n");
 			parse_rom = 1;
 			break;
 		case 'f':
@@ -250,6 +230,9 @@
 		initialip = 0x0003;
 	}
 
+	if (parse_rom)
+		printf("Parsing rom images not implemented.\n");
+
 	//printf("Point 1 int%x vector at %x\n", 0x42, getIntVect(0x42));
 
 	if (initialip == 0x0003) {
@@ -314,7 +297,7 @@
 		for (i = 0; i < 0x10000; i++)
 			wrb(0xf0000 + i, fsegptr[i]);
 	} else {
-		char *date = "01/01/99";
+		const char *date = "01/01/99";
 		for (i = i; date[i]; i++)
 			wrb(0xffff5 + i, date[i]);
 		wrb(0xffff7, '/');
@@ -391,3 +374,16 @@
 
 	return devfn;
 }
+
+int printk(int msg_level, const char *fmt, ...)
+{
+	va_list args;
+	int i;
+
+	printf ("<%d> ", msg_level);
+	va_start(args, fmt);
+	i = vprintf(fmt, args);
+	va_end(args);
+
+	return i;
+}
diff --git a/util/vgabios/test.h b/util/vgabios/testbios.h
similarity index 87%
rename from util/vgabios/test.h
rename to util/vgabios/testbios.h
index 7804ac2..a5a8b0d 100644
--- a/util/vgabios/test.h
+++ b/util/vgabios/testbios.h
@@ -1,11 +1,20 @@
-/* $XFree86: xc/programs/Xserver/hw/xfree86/int10/xf86x86emu.h,v 1.2 2001/01/06 20:19:13 tsi Exp $ */
-/*
+/* Derived from:
  *                   XFree86 int10 module
  *   execute BIOS int 10h calls in x86 real mode environment
  *                 Copyright 1999 Egbert Eich
  */
-#ifndef XF86X86EMU_H_
-#define XF86X86EMU_H_
+
+#ifndef __TESTBIOS_H__
+#define __TESTBIOS_H__
+
+void x86emu_dump_xregs(void);
+int int15_handler(void);
+int int16_handler(void);
+int int1A_handler(void);
+int int42_handler(void);
+int intE6_handler(void);
+
+#include <stdtypes.h>
 #include <x86emu/x86emu.h>
 
 #define M _X86EMU_env