Index: getpir.c
===================================================================
--- getpir.c	(revision 6218)
+++ getpir.c	(working copy)
@@ -34,7 +34,7 @@
  *	descriptions, it is special handy when building a PIRQ table
  *	for a board to check the checksum.
  */
-static struct irq_routing_table *probe_table(char* ptr)
+static struct irq_routing_table *probe_table(char* ptr, int datasize)
 {
 	/**
 	 * Signature to search for $PIR<2-byte-version>
@@ -44,60 +44,55 @@
 	char signature[] = "$PIR\x00\x01";
 	/** cast the pointer */
 	struct irq_routing_table *rt = (struct irq_routing_table *)ptr;
-	int size = 16;
 	int checksum_result;
-	do {
-		/** find the PIRQ table */
-		rt = (struct irq_routing_table *) memmem(ptr + size, 16, signature, 6);
-		if (rt != NULL) {
-			/** found the table */
-			int i, ts = (rt->size - 32) / 16;
-			struct irq_info *se_arr;
-			se_arr = (struct irq_info *) ((char *) rt + 32);
-			/** Dump the table information to the stdout */
-			printf("Found PCI IRQ routing table signature at %p.\n", (void *) ((char *) rt - ptr + 0xf0000));
-			printf("SIGNATURE            = %s\n", (char*)&rt->signature);
-			printf("VERSION              = %04x\n", rt->version);
-			printf("SIZE                 = %i\n", rt->size);
-			printf("MAX_DEVICES_ON_BUS   = 32 + 16 * %d\n", ts);
-			printf("INT_ROUTER_BUS       = 0x%02x\n", rt->rtr_bus);
-			printf("INT_ROUTER DEVICE    = (0x%02x << 3) | 0x%01x\n", rt->rtr_devfn >> 3, rt->rtr_devfn & 7);
-			printf("IRQ_DEVOTED_TO_PCI   = %#x\n", rt->exclusive_irqs);
-			printf("VENDOR               = %#x\n", rt->rtr_vendor);
-			printf("DEVICE               = %#x\n", rt->rtr_device);
-			printf("MINIPORT             = %#x\n", rt->miniport_data);
-			printf("CHECKSUM             = %#x\n", rt->checksum);
- 			printf("\tbus , dev        | fn,   {link, bitmap}, {link, bitmap}, {link, bitmap}, {link, bitmap}, slot, rfu\n");
-			for (i = 0; i < ts; i++) {
-				printf("\t0x%02x, (0x%02x << 3) | 0x%01x, {{0x%02x, 0x%04x}, {0x%02x, 0x%04x}, {0x%02x, 0x%04x}, {0x%02x, 0x%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);
-			}
-			/** A table should not be over 0x400 bytes */
-			if (rt->size > 0x400) {
-				return NULL;
-			}
-			printf("Validating...\n");
-			/** Calculate the checksum value */
-			checksum_result	= calc_checksum(rt);
-			/** Show the calculatedchecksum value */
-			printf("CHECKSUM             = %#x\n", 0x100-((checksum_result - rt->checksum) & 0xFF));
-			/** and the result of the calculation */
-			if (!checksum_result) {
-				printf("checksum is ok.\n");
-				break;
-			} else {
-				printf("checksum is wrong.\n");
-			}
+	/** find the PIRQ table */
+	printf( "datasize %i\n", datasize );
+	rt = (struct irq_routing_table *) memmem( ptr, datasize, signature, 6 );
+	if ( rt != NULL ) {
+		/** found the table */
+		int i, ts = (rt->size - 32) / 16;
+		struct irq_info *se_arr;
+		se_arr = (struct irq_info *) ((char *) rt + 32);
+		/** Dump the table information to the stdout */
+		printf("Found PCI IRQ routing table signature at %p.\n", (void *) ((char *) rt - ptr + 0xf0000));
+		printf("SIGNATURE            = %s\n", (char*)&rt->signature);
+		printf("VERSION              = %04x\n", rt->version);
+		printf("SIZE                 = %i\n", rt->size);
+		printf("MAX_DEVICES_ON_BUS   = 32 + 16 * %d\n", ts);
+		printf("INT_ROUTER_BUS       = 0x%02x\n", rt->rtr_bus);
+		printf("INT_ROUTER DEVICE    = (0x%02x << 3) | 0x%01x\n", rt->rtr_devfn >> 3, rt->rtr_devfn & 7);
+		printf("IRQ_DEVOTED_TO_PCI   = %#x\n", rt->exclusive_irqs);
+		printf("VENDOR               = %#x\n", rt->rtr_vendor);
+		printf("DEVICE               = %#x\n", rt->rtr_device);
+		printf("MINIPORT             = %#x\n", rt->miniport_data);
+		printf("CHECKSUM             = %#x\n", rt->checksum);
+		printf("\tbus , dev        | fn,   {link, bitmap}, {link, bitmap}, {link, bitmap}, {link, bitmap}, slot, rfu\n");
+		for (i = 0; i < ts; i++) {
+			printf("\t0x%02x, (0x%02x << 3) | 0x%01x, {{0x%02x, 0x%04x}, {0x%02x, 0x%04x}, {0x%02x, 0x%04x}, {0x%02x, 0x%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);
 		}
-		size += 16;
-	} while (size < 0xFFFF);
-	if (size >= 0xFFFF) {
+		/** A table should not be over 0x400 bytes */
+		if (rt->size > 0x400) {
+			return NULL;
+		}
+		printf("Validating...\n");
+		/** Calculate the checksum value */
+		checksum_result	= calc_checksum(rt);
+		/** Show the calculatedchecksum value */
+		printf("CHECKSUM             = %#x\n", 0x100-((checksum_result - rt->checksum) & 0xFF));
+		/** and the result of the calculation */
+		if (!checksum_result) {
+			printf("checksum is ok.\n");
+		} else {
+			printf("checksum is wrong.\n");
+		}
+	} else {
 		/** When the functions comes here there is no PIRQ table found. */
 		printf("No PCI IRQ routing table signature found.\n");
 		return NULL;
@@ -112,22 +107,25 @@
 	int fd_mem = -1;
 	struct irq_routing_table* rt = NULL;
 	void* bios_image = NULL;
-	if ( argc > 1 )
-	{
+	int file_size = 0;
+	if ( argc > 1 )	{
 		/** there a paramater passed to the program, assume that it is a menory file */
 		printf("Opening memory image file '%s'\n", argv[1]);
 		/** Open the file */
 		fd_mem = open(argv[1], O_RDONLY);
 		if (fd_mem > 0) {
 			/** get tyhe size of the file */
-			int file_size = lseek(fd_mem, 0, SEEK_END);
+			file_size = lseek(fd_mem, 0, SEEK_END);
 			printf("Memory image '%i'\n", file_size);
 			/** get a memory block for it. */
 			bios_image = malloc(file_size);
 			if (bios_image) {
 				/** Fill the created buffer */
 				lseek(fd_mem, 0, SEEK_SET);
-				read(fd_mem, bios_image, file_size);
+				if ( read(fd_mem, bios_image, file_size) !=  file_size ){
+					perror("Failed to read all data of imagefile\n");
+					return (-4);;
+				}
 				/** set the pointer for the probe function */
 				ptr = (char*)bios_image;
 			} else {
@@ -141,6 +139,7 @@
 			return (-2);
 		}
 	} else {
+		file_size = 0x10000;
 		/** No paramaters means that the program will access the system memory */
 		printf("Accessing memory\n");
 		if (getuid()) {
@@ -155,17 +154,18 @@
 			exit(1);
 		}
 		printf("Probing PIRQ table in memory.\n");
-		ptr = mmap(0, 0x10000, PROT_READ, MAP_SHARED, fd_mem, (off_t)0xf0000);
+		ptr = mmap( 0, file_size, PROT_READ, MAP_SHARED, fd_mem, (off_t)0xf0000 );
 		if (ptr == MAP_FAILED) {
 			/** could not map the system memory, exit with a message */
 			perror("Mapping system memory failed: ");
 			close(fd_mem);
 			return (1);
 		}
+		
 	}
-	if (ptr) {
+	if ( ptr && file_size ) {
 		/** now do the actual probe function */
-		rt = probe_table(ptr);
+		rt = probe_table( ptr, file_size );
 		if (rt != NULL && bios_image == NULL) {
 			/** when probing system memory we write the 'irq_tables.c' code file */
 			printf("Creating irq_tables.c ...\n");
