this commit should fix Ticket #122 (proper log files for all builds)
[coreboot.git] / util / getpir / getpir.c
1 /* getpir.c : This software is released under GPL
2  * For coreboot use only
3  * Aug 26 2001 , Nikolai Vladychevski, <niko@isl.net.mx>
4  * 2007.04.09 Jeremy Jackson <jerj@coplanar.net>
5  *     updated for amd64 and general 64 bit portability
6  */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <fcntl.h>
13 #include <sys/mman.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16
17 #include "pirq_routing.h"
18 #include "checksum.h"
19 #include "code_gen.h"
20
21 #if defined (__sun) && (defined(__i386) || defined(__amd64))
22 #  define MEM_DEV "/dev/xsvc"
23 #else
24 #  define MEM_DEV "/dev/mem"
25 #endif
26
27 static struct irq_routing_table *probe_table(int fd_mem)
28 {
29         char *ptr, signature[] = "$PIR";
30         struct irq_routing_table *rt;
31
32         ptr =  mmap(0, 0x10000, PROT_READ, MAP_SHARED,
33                     fd_mem, (off_t) 0xf0000);
34
35         if (ptr == MAP_FAILED) {
36                 perror("Mapping system memory failed: ");
37                 exit(1);
38         }
39
40         rt = (struct irq_routing_table *) memmem(ptr, 0xFFFF, signature, 4);
41
42         if (rt != NULL) {
43                 printf("Found PCI IRQ routing table signature at %p.\n",
44                        (void *)((char *)rt - ptr + 0xf0000));
45         } else {
46                 printf("No PCI IRQ routing table signature found.\n");
47                 exit(1);
48         }
49         return rt;
50 }
51
52 int main(void)
53 {
54         int fd_mem;
55         struct irq_routing_table *rt;
56
57         if (getuid()) {
58                 fprintf(stderr, "Run me as root, I need access to " MEM_DEV ".\n");
59         }
60
61         fd_mem = open(MEM_DEV, O_RDONLY);
62         if (fd_mem < 0) {
63                 perror("Could not open " MEM_DEV ":");
64                 exit(1);
65         }
66
67         printf("Probing PIRQ table in memory.\n");
68         rt = probe_table(fd_mem);
69
70         printf("Validating... ");
71         if (!calc_checksum(rt))
72                 printf("checksum is ok.\n");
73         else
74                 printf("checksum is wrong.\n");
75
76         printf("Creating irq_tables.c ...\n");
77         code_gen("irq_tables.c", rt);
78
79         close(fd_mem);
80
81         printf("Done, you can move the file to the coreboot tree now.\n");
82
83         return 0;
84 }