Enable -Werror for romcc
[coreboot.git] / util / getpir / getpir.c
index 8c868fe02817ef26ce4a2b5775ca98dbf8406635..9853d7ca9498f22f0cc196c31fcb4abe54ee6aeb 100644 (file)
 /* getpir.c : This software is released under GPL
-   For Linuxbios use only
-   Aug 26 2001 , Nikolai Vladychevski, <niko@isl.net.mx>
-*/
+ * For coreboot use only
+ * Aug 26 2001 , Nikolai Vladychevski, <niko@isl.net.mx>
+ * 2007.04.09 Jeremy Jackson <jerj@coplanar.net>
+ *     updated for amd64 and general 64 bit portability
+ * 2010.04.24 Marc Bertens <mbertens@xs4all.nl>
+ *     Added functionality to read a image file for checking the checksum of the PIR
+ */
 
-#include <unistd.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 
-#include <device/pci.h>
-#include <pirq_routing.h>
-
-#define PIRQ_SIGNATURE  (('$' << 0) + ('P' << 8) + ('I' << 16) + ('R' << 24))
-#define PIRQ_VERS 0x0100
-
-struct irq_info se_arr[50];
-struct irq_routing_table rt;
-unsigned short ts;
+#include "pirq_routing.h"
+#include "checksum.h"
+#include "code_gen.h"
 
+#if defined (__sun) && (defined(__i386) || defined(__amd64))
+#  define MEM_DEV "/dev/xsvc"
+#else
+#  define MEM_DEV "/dev/mem"
+#endif
 
-int calc_checksum()
+/**
+ *     The probe_table() is now called with the pointer to the memory,
+ *     this is to handle both the assessing memory and a file.
+ *
+ *     This function now dumps the table found to the stdout, with
+ *     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)
 {
-       long sum = 0, i, j;
-       uint8_t *addr, sum2 = 0, data;
-
-       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];
+       /**
+        * Signature to search for $PIR<2-byte-version>
+        *
+        *      this is to be sure that we find the correct table.
+        */
+       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");
+                       }
+               }
+               size += 16;
+       } while (size < 0xFFFF);
+       if (size >= 0xFFFF) {
+               /** When the functions comes here there is no PIRQ table found. */
+               printf("No PCI IRQ routing table signature found.\n");
+               return NULL;
        }
-       return (sum2);
+
+       return rt;
 }
 
-main()
+int main(int argc, char* argv[])
 {
-       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;
-
-       if (getuid()) {
-               perror("Run me as root, I need access to /dev/mem");
-               exit(1);
-       }
-
-       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_VERS) {
-                                       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;
-                               }
+       char* ptr;
+       int fd_mem = -1;
+       struct irq_routing_table* rt = NULL;
+       void* bios_image = NULL;
+       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);
+                       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);
+                               /** set the pointer for the probe function */
+                               ptr = (char*)bios_image;
                        } else {
-                               count = count + rcount;
+                               /* no memory available ? */
+                               perror("Failed to open imagefile\n");
+                               return (-3);
                        }
+               } else {
+                       /** An error occourd, just exit with a message */
+                       perror("Failed to open imagefile");
+                       return (-2);
+               }
+       } else {
+               /** No paramaters means that the program will access the system memory */
+               printf("Accessing memory\n");
+               if (getuid()) {
+                       /** i'm not root message !!!! */
+                       fprintf(stderr, "Run me as root, I need access to " MEM_DEV ".\n");
+               }
+               /** open the system memory */
+               fd_mem = open(MEM_DEV, O_RDONLY);
+               if (fd_mem < 0) {
+                       /** could not open the system memory, exit with a message */
+                       perror("Could not open " MEM_DEV ":");
+                       exit(1);
+               }
+               printf("Probing PIRQ table in memory.\n");
+               ptr = mmap(0, 0x10000, 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);
                }
-       } while (rcount > 0);
-
-       if (!calc_checksum())
-               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");
-       if (!fpir) {
-               printf("Failed creating file!\n");
-               exit(2);
+       if (ptr) {
+               /** now do the actual probe function */
+               rt = probe_table(ptr);
+               if (rt != NULL && bios_image == NULL) {
+                       /** when probing system memory we write the 'irq_tables.c' code file */
+                       printf("Creating irq_tables.c ...\n");
+                       code_gen("irq_tables.c", rt);
+                       printf("Done, you can move the file to the coreboot tree now.\n");
+               }
+       } else {
+               printf("invalid memory pointer\n");
        }
-
-       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);
+       if (bios_image) {
+               /** when we are reading from a file the memory must be released */
+               free(bios_image);
+       } else {
+               /** when reading from the system memory, it must be unmapped */
+               munmap(ptr, 0x10000);
        }
-       fprintf(fpir, "\t}\n");
-       fprintf(fpir, "};\n");
-       fclose(fpir);
-       printf("Done, you can move the file to the LinuxBios tree now.\n");
+       /** Close the file handle */
+       close(fd_mem);
+       /** return 0 as OK ) */
+       return 0;
 }