util: Add a very simple utility to test POST cards.

It was tested with a mini-PCI POST card on a Toshiba
Satellite 1410 laptop with the stock BIOS.

Change-Id: Icdc0860e2c72b17862601c2cc59eaf0f3d8a0e54
Signed-off-by: Denis 'GNUtoo' Carikli <GNUtoo@no-log.org>
Reviewed-on: https://review.coreboot.org/13763
Tested-by: build bot (Jenkins)
Reviewed-by: Stefan Reinauer <stefan.reinauer@coreboot.org>
diff --git a/util/post/.gitignore b/util/post/.gitignore
new file mode 100644
index 0000000..8b1e502
--- /dev/null
+++ b/util/post/.gitignore
@@ -0,0 +1 @@
+post
diff --git a/util/post/Makefile b/util/post/Makefile
new file mode 100644
index 0000000..d0a3eb9
--- /dev/null
+++ b/util/post/Makefile
@@ -0,0 +1,4 @@
+all:
+	$(CC) post.c -o post
+clean:
+	rm -f post
diff --git a/util/post/README b/util/post/README
new file mode 100644
index 0000000..95b33a9
--- /dev/null
+++ b/util/post/README
@@ -0,0 +1 @@
+This is a very simple userspace utility that can be used to test POST cards.
diff --git a/util/post/post.c b/util/post/post.c
new file mode 100644
index 0000000..6d67909
--- /dev/null
+++ b/util/post/post.c
@@ -0,0 +1,91 @@
+/*
+ * Copyright (C) 2016 Denis 'GNUtoo' Carikli <GNUtoo@no-log.org>
+ *
+ * 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; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * 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.
+ */
+
+#include <errno.h>
+#include <limits.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/io.h>
+
+#define POST_DEFAULT_IO_PORT 0x80
+
+void usage(char *progname, const char *error, ...)
+{
+	printf("Usage: %s <VALUE> [PORT]\n", progname);
+	printf("The VALUE argument is an integer between 0x00 and 0xff\n");
+	printf("The PORT argument is an integer between 0x00 and 0xffff\n");
+
+	if (error) {
+		va_list args;
+
+		va_start(args, error);
+		vprintf(error, args);
+		va_end(args);
+	};
+}
+
+void check_int(long val, int min, int max, int err, char *string, char *endptr,
+	       char *progname)
+{
+	if (val < min || val > max) {
+		usage(progname,
+		      "\nError: The value has to be between 0x%x and 0x%x\n",
+		      min, max);
+		exit(EXIT_FAILURE);
+	}
+
+	if (endptr == string || *endptr != '\0') {
+		usage(progname, "\nError: An integer is required\n");
+		exit(EXIT_FAILURE);
+	}
+
+	if ((err) && (!val)) {
+		perror("strtol");
+		exit(EXIT_FAILURE);
+	}
+}
+
+int main(int argc, char *argv[])
+{
+	unsigned long val;
+	unsigned long port = POST_DEFAULT_IO_PORT;
+	char *endptr;
+	int err;
+
+	if (argc != 2 && argc != 3) {
+		usage(argv[0], NULL);
+		exit(EXIT_FAILURE);
+	}
+
+	val = strtol(argv[1], &endptr, 0);
+	err = errno;
+	check_int(val, 0x00, 0xff, err, argv[1], endptr, argv[0]);
+
+	if (argc > 2) {
+		port = strtol(argv[2], &endptr, 0);
+		err = errno;
+		check_int(port, 0x0000, 0xffff, err, argv[2], endptr, argv[0]);
+	}
+
+	err = iopl(3);
+	if (err == -1) {
+		perror("Not root");
+		exit(EXIT_FAILURE);
+	}
+
+	outb(val, port);
+
+	return 0;
+}