seperate checksum and code generating code.
use mmap instead of file io


git-svn-id: svn://svn.coreboot.org/coreboot/trunk@1504 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
diff --git a/util/getpir/Makefile b/util/getpir/Makefile
index fffb138..07a6625 100644
--- a/util/getpir/Makefile
+++ b/util/getpir/Makefile
@@ -5,20 +5,25 @@
 INCLUDEPATH=$(LINUXBIOSROOT)/src/arch/i386/include
 INCLUDE2=$(LINUXBIOSROOT)/src/include
 
-getpir: getpir.c
-	gcc -o getpir -I$(INCLUDEPATH) -I$(INCLUDE2) getpir.c
+getpir: getpir.c checksum.o code_gen.o
+	gcc -o getpir -I$(INCLUDEPATH) -I$(INCLUDE2) getpir.c checksum.o code_gen.o
+
+code_gen.o: code_gen.c
+	gcc -c -I$(INCLUDEPATH) -I$(INCLUDE2) code_gen.c
 
 all: getpir checkpir
 
-checkpir: checkpir.c irq_tables.o
-	gcc -o checkpir -I$(INCLUDEPATH) -I$(INCLUDE2) irq_tables.o checkpir.c
+checkpir: checkpir.c checksum.o irq_tables.o
+	gcc -o checkpir -I$(INCLUDEPATH) -I$(INCLUDE2) irq_tables.o checksum.o checkpir.c
 
+checksum.o: checksum.c
+	gcc -c -I$(INCLUDEPATH) -I$(INCLUDE2) checksum.c 
 
 irq_tables.o: irq_tables.c  
 	gcc -c -I$(INCLUDEPATH) -I$(INCLUDE2) irq_tables.c
 
 clean:
-	rm -f irq_tables.o getpir checkpir *~
+	rm -f getpir checkpir *.o *~
 
 cleantable:
 	rm -f irq_table.o
diff --git a/util/getpir/checkpir.c b/util/getpir/checkpir.c
index efcc918..2a738e7 100644
--- a/util/getpir/checkpir.c
+++ b/util/getpir/checkpir.c
@@ -4,24 +4,12 @@
 */
 
 #include <stdio.h>
-
-#include <device/pci.h>
 #include <arch/pirq_routing.h>
 
-struct irq_info se_arr[50];
+#include "checksum.h"
+
 struct irq_routing_table *rt;
 
-int calc_checksum(struct irq_routing_table *rt)
-{
-	long sum = 0, i;
-	uint8_t *addr, sum2 = 0;
-
-	addr = (uint8_t *) rt;
-	for (i = 0; i < rt->size; i++)
-		sum2 += addr[i];
-	return (sum2);
-}
-
 main()
 {
 	uint8_t sum, newsum;
diff --git a/util/getpir/checksum.c b/util/getpir/checksum.c
new file mode 100644
index 0000000..a202f90
--- /dev/null
+++ b/util/getpir/checksum.c
@@ -0,0 +1,14 @@
+#include <arch/pirq_routing.h>
+
+#include "checksum.h"
+
+int calc_checksum(struct irq_routing_table *rt)
+{
+	long i;
+	uint8_t *addr, sum = 0;
+
+	addr = (uint8_t *) rt;
+	for (i = 0; i < rt->size; i++)
+		sum += addr[i];
+	return (sum);
+}
diff --git a/util/getpir/checksum.h b/util/getpir/checksum.h
new file mode 100644
index 0000000..b5de899
--- /dev/null
+++ b/util/getpir/checksum.h
@@ -0,0 +1,6 @@
+#ifndef __CHECKSUM_H__
+#define __CHECKSUM_H__
+
+extern int calc_checksum(struct irq_routing_table *rt);
+
+#endif /* __CHECKSUN_H__ */
diff --git a/util/getpir/code_gen.c b/util/getpir/code_gen.c
new file mode 100644
index 0000000..3252863
--- /dev/null
+++ b/util/getpir/code_gen.c
@@ -0,0 +1,55 @@
+#include <stdio.h>
+#include <arch/pirq_routing.h>
+
+static char *preamble[] = {
+	"/* This file was generated by getpir.c, do not modify! \n   (but if you do, please run checkpir on it to verify)\n",
+	" * Contains the IRQ Routing Table dumped directly from your memory, which BIOS sets up\n",
+	" *\n",
+	" * Documentation at : http://www.microsoft.com/hwdev/busbios/PCIIRQ.HTM\n*/\n\n",
+	"#include <arch/pirq_routing.h>\n\n",
+	"const struct irq_routing_table intel_irq_routing_table = {\n",
+	"\tPIRQ_SIGNATURE,  /* u32 signature */\n",
+	"\tPIRQ_VERSION,    /* u16 version   */\n",
+	0
+};
+
+void code_gen(FILE * fpir, struct irq_routing_table *rt)
+{
+	char **code = preamble;
+	struct irq_info *se_arr = (struct irq_info *) ((char *) rt + 32);
+	int i, ts = (rt->size - 32) / 16;
+
+	while (*code)
+		fprintf(fpir, "%s", *code++);
+
+
+	fprintf(fpir, "\t32+16*%d,	 /* there can be total %d devices on the bus */\n",
+		ts, ts);
+	fprintf(fpir, "\t0x%02x,		 /* Where the interrupt router lies (bus) */\n",
+		rt->rtr_bus);
+	fprintf(fpir, "\t(0x%02x<<3)|0x%01x,   /* Where the interrupt router lies (dev) */\n",
+		rt->rtr_devfn >> 3, rt->rtr_devfn & 7);
+	fprintf(fpir, "\t%#x,		 /* IRQs devoted exclusively to PCI usage */\n",
+		rt->exclusive_irqs);
+	fprintf(fpir, "\t%#x,		 /* Vendor */\n", rt->rtr_vendor);
+	fprintf(fpir, "\t%#x,		 /* Device */\n", rt->rtr_device);
+	fprintf(fpir, "\t%#x,		 /* Crap (miniport) */\n",
+		rt->miniport_data);
+	fprintf(fpir, "\t{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* u8 rfu[11] */\n");
+	fprintf(fpir, "\t%#x,         /*  u8 checksum , this hase to set to some value that would give 0 after the sum of all bytes for this structure (including checksum) */\n",
+		rt->checksum);
+	fprintf(fpir, "\t{\n");
+	fprintf(fpir, "\t\t/* bus,     dev|fn,   {link, bitmap}, {link, bitmap}, {link, bitmap}, {link, bitmap},  slot, rfu */\n");
+	for (i = 0; i < ts; i++) {
+		fprintf(fpir, "\t\t{0x%02x,(0x%02x<<3)|0x%01x, {{0x%02x, 0x%04x}, {0x%02x, 0x%04x}, {0x%02x, 0x%04x}, {0x%02x, 0x0%04x}}, 0x%x, 0x%x},\n",
+			(se_arr+i)->bus, (se_arr+i)->devfn >> 3,
+			(se_arr+i)->devfn & 7, (se_arr+i)->irq[0].link,
+			(se_arr+i)->irq[0].bitmap, (se_arr+i)->irq[1].link,
+			(se_arr+i)->irq[1].bitmap, (se_arr+i)->irq[2].link,
+			(se_arr+i)->irq[2].bitmap, (se_arr+i)->irq[3].link,
+			(se_arr+i)->irq[3].bitmap, (se_arr+i)->slot,
+			(se_arr+i)->rfu);
+	}
+	fprintf(fpir, "\t}\n");
+	fprintf(fpir, "};\n");
+}
diff --git a/util/getpir/getpir.c b/util/getpir/getpir.c
index 110eef9..5bcdd45 100644
--- a/util/getpir/getpir.c
+++ b/util/getpir/getpir.c
@@ -4,101 +4,50 @@
 */
 
 #include <stdio.h>
+#include <sys/mman.h>
 
-#include <device/pci.h>
 #include <arch/pirq_routing.h>
 
-struct irq_info se_arr[50];
-struct irq_routing_table rt;
-unsigned short ts;
+#define O_RDONLY 0x00
 
-
-int calc_checksum()
+static struct irq_routing_table *probe_table(int fd_mem)
 {
-	long sum = 0, i, j;
-	uint8_t *addr, sum2 = 0, data;
+	char *ptr, signature[] = "$PIR";
+	struct irq_routing_table *rt;
 
-	addr = (uint8_t *) & rt;
-	for (i = 0; i < sizeof(struct irq_routing_table); i++)
-		sum2 += addr[i];
-	for (i = 0; i < ts; i++) {
-		addr = (uint8_t *) & se_arr[i];
-		for (j = 0; j < sizeof(struct irq_info); j++)
-			sum2 += addr[j];
+	ptr =  mmap(0, 0x10000, PROT_READ, MAP_SHARED,
+		    fd_mem, (off_t) 0xf0000);
+
+	rt = (struct irq_routing_table *) memmem(ptr, 0xFFFF, signature, 4);
+
+	if (rt != NULL) {
+		printf("Found PCI IRQ Routing table signature at 0x%04x of system memory\n",
+		       (char *) rt - ptr + 0xf0000);
+	} else {
+		printf("No PCI IRQ Routing table signature in the memory\n");		
+		exit(1);
 	}
-	return (sum2);
+	return rt;
 }
 
 main()
 {
-	FILE *fmem, *fpir;
-	size_t rcount = 0;
-	unsigned long b, p, pir = PIRQ_SIGNATURE;
-	unsigned long count;
-	int i, valid = 1, print = 0;
-	char cksum = 0;
-	unsigned char *ptr;
+	int fd_mem;
+	FILE *fpir;
+ 	struct irq_routing_table *rt;
 
 	if (getuid()) {
 		perror("Run me as root, I need access to /dev/mem");
 		exit(1);
 	}
+	fd_mem = open("/dev/mem", O_RDONLY);
 
-	printf("Opening memory...\n");
-	fmem = fopen("/dev/mem", "r");
-	do {
-		rcount = fread(&b, 1, 4, fmem);
-		if (rcount > 0) {
-			if (b == pir) {
-				valid = 1;
-				printf("Found PCI IRQ Routing table signature at %x bytes from top of the memory\nValidating../\n",
-				       count);
-				rt.signature = PIRQ_SIGNATURE;
-				ptr = (char *) &rt;
-				ptr = ptr + 4;	// signature was read, advance 4 bytes
-				rcount = fread(ptr, 1, sizeof(struct irq_routing_table) - 4, fmem);
-				count = count + rcount;
-				printf("Version is:%d Table size:%d\n",
-				       rt.version, rt.size);
-				if (rt.version != PIRQ_VERSION) {
-					printf("Invalid version\n");
-					valid = 0;
-				}
-				if (rt.size < 32) {
-					printf(" Invalid table size\n");
-					valid = 0;
-				}
-				if (rt.size % 16) {
-					printf(" Invalid table size (not a multiple of 16)\n");
-					valid = 0;
-				}
-				if (valid) {
-					//      read slot entries
-					ts = (rt.size - 32) / 16;
-					printf("Reading %d slot entries...\n", ts);
-					for (i = 0; i < ts; i++) {
-						rcount =
-						    fread(&se_arr[i], 1, 16, fmem);
-						count = count + rcount;
-					}
-					print = 1;
-					break;
-				}
-			} else {
-				count = count + rcount;
-			}
-		}
-	} while (rcount > 0);
+	printf("Probing PIRQ table in memory\n");
+	rt = probe_table(fd_mem);
 
-	if (!calc_checksum())
+	printf("Validating..\n");
+	if (!calc_checksum(rt))
 		printf("Checksum is ok!\n");
-	printf("Closing memory\n");
-	fclose(fmem);
-
-	if (!print) {
-		printf("No table for you...\n");
-		return (0);
-	}
 
 	printf("Creating irq_tables.c .....\n");
 	fpir = fopen("irq_tables.c", "w");
@@ -106,43 +55,10 @@
 		printf("Failed creating file!\n");
 		exit(2);
 	}
-
-	fprintf(fpir, "/* This file was generated by getpir.c, do not modify! \n   (but if you do, please run checkpir on it to verify)\n");
-	fprintf(fpir, "   Contains the IRQ Routing Table dumped directly from your memory , wich BIOS sets up\n\n");
-	fprintf(fpir, "   Documentation at : http://www.microsoft.com/hwdev/busbios/PCIIRQ.HTM\n*/\n\n");
-	fprintf(fpir, "#include <arch/pirq_routing.h>\n\n");
-	fprintf(fpir, "const struct irq_routing_table intel_irq_routing_table = {\n");
-	fprintf(fpir, "\tPIRQ_SIGNATURE,  /* u32 signature */\n");
-	fprintf(fpir, "\tPIRQ_VERSION,    /* u16 version   */\n");
-	fprintf(fpir, "\t32+16*%d,	 /* there can be total %d devices on the bus */\n",
-		ts, ts);
-	fprintf(fpir, "\t0x%02x,		 /* Where the interrupt router lies (bus) */\n",
-		rt.rtr_bus);
-	fprintf(fpir, "\t(0x%02x<<3)|0x%01x,   /* Where the interrupt router lies (dev) */\n",
-		rt.rtr_devfn >> 3, rt.rtr_devfn & 7);
-	fprintf(fpir, "\t%#x,		 /* IRQs devoted exclusively to PCI usage */\n",
-		rt.exclusive_irqs);
-	fprintf(fpir, "\t%#x,		 /* Vendor */\n", rt.rtr_vendor);
-	fprintf(fpir, "\t%#x,		 /* Device */\n", rt.rtr_device);
-	fprintf(fpir, "\t%#x,		 /* Crap (miniport) */\n",
-		rt.miniport_data);
-	fprintf(fpir, "\t{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* u8 rfu[11] */\n");
-	fprintf(fpir, "\t%#x,         /*  u8 checksum , this hase to set to some value that would give 0 after the sum of all bytes for this structure (including checksum) */\n",
-		rt.checksum);
-	fprintf(fpir, "\t{\n");
-	fprintf(fpir, "\t\t/* bus,     dev|fn,   {link, bitmap}, {link, bitmap}, {link, bitmap}, {link, bitmap},  slot, rfu */\n");
-	for (i = 0; i < ts; i++) {
-		fprintf(fpir, "\t\t{0x%02x,(0x%02x<<3)|0x%01x, {{0x%02x, 0x%04x}, {0x%02x, 0x%04x}, {0x%02x, 0x%04x}, {0x%02x, 0x0%04x}}, 0x%x, 0x%x},\n",
-			se_arr[i].bus, se_arr[i].devfn >> 3,
-			se_arr[i].devfn & 7, se_arr[i].irq[0].link,
-			se_arr[i].irq[0].bitmap, se_arr[i].irq[1].link,
-			se_arr[i].irq[1].bitmap, se_arr[i].irq[2].link,
-			se_arr[i].irq[2].bitmap, se_arr[i].irq[3].link,
-			se_arr[i].irq[3].bitmap, se_arr[i].slot,
-			se_arr[i].rfu);
-	}
-	fprintf(fpir, "\t}\n");
-	fprintf(fpir, "};\n");
+	code_gen(fpir, rt);
 	fclose(fpir);
+
+	close(fd_mem);
+
 	printf("Done, you can move the file to the LinuxBios tree now.\n");
 }