]> mj.ucw.cz Git - pciutils.git/blob - lib/dump.c
0915b005e503265fbd45f1d02a50b98db7e89c33
[pciutils.git] / lib / dump.c
1 /*
2  *      The PCI Library -- Reading of Bus Dumps
3  *
4  *      Copyright (c) 1997--2005 Martin Mares <mj@ucw.cz>
5  *
6  *      Can be freely distributed and used under the terms of the GNU GPL.
7  */
8
9 #include <stdio.h>
10 #include <ctype.h>
11 #include <string.h>
12 #include <errno.h>
13
14 #include "internal.h"
15
16 struct dump_data {
17   int len, allocated;
18   byte data[1];
19 };
20
21 static int
22 dump_detect(struct pci_access *a)
23 {
24   return !!a->method_params[PCI_ACCESS_DUMP];
25 }
26
27 static void
28 dump_alloc_data(struct pci_dev *dev, int len)
29 {
30   struct dump_data *dd = pci_malloc(dev->access, sizeof(struct dump_data) + len - 1);
31   dd->allocated = len;
32   dd->len = 0;
33   memset(dd->data, 0xff, len);
34   dev->aux = dd;
35 }
36
37 static int
38 dump_validate(char *s, char *fmt)
39 {
40   while (*fmt)
41     {
42       if (*fmt == '#' ? !isxdigit(*s) : *fmt != *s)
43         return 0;
44       *fmt++, *s++;
45     }
46   return 1;
47 }
48
49 static void
50 dump_init(struct pci_access *a)
51 {
52   char *name = a->method_params[PCI_ACCESS_DUMP];
53   FILE *f;
54   char buf[256];
55   struct pci_dev *dev = NULL;
56   int len, mn, bn, dn, fn, i, j;
57
58   if (!a)
59     a->error("dump: File name not given.");
60   if (!(f = fopen(name, "r")))
61     a->error("dump: Cannot open %s: %s", name, strerror(errno));
62   while (fgets(buf, sizeof(buf)-1, f))
63     {
64       char *z = strchr(buf, '\n');
65       if (!z)
66         a->error("dump: line too long or unterminated");
67       *z-- = 0;
68       if (z >= buf && *z == '\r')
69         *z-- = 0;
70       len = z - buf + 1;
71       mn = 0;
72       if (dump_validate(buf, "##:##.# ") && sscanf(buf, "%x:%x.%d", &bn, &dn, &fn) == 3 ||
73           dump_validate(buf, "####:##:##.# ") && sscanf(buf, "%x:%x:%x.%d", &mn, &bn, &dn, &fn) == 4)
74         {
75           dev = pci_get_dev(a, mn, bn, dn, fn);
76           dump_alloc_data(dev, 256);
77           pci_link_dev(a, dev);
78         }
79       else if (!len)
80         dev = NULL;
81       else if (dev &&
82                (dump_validate(buf, "##: ") || dump_validate(buf, "###: ")) &&
83                sscanf(buf, "%x: ", &i) == 1)
84         {
85           struct dump_data *dd = dev->aux;
86           z = strchr(buf, ' ') + 1;
87           while (isxdigit(z[0]) && isxdigit(z[1]) && (!z[2] || z[2] == ' ') &&
88                  sscanf(z, "%x", &j) == 1 && j < 256)
89             {
90               if (i >= 4096)
91                 a->error("dump: At most 4096 bytes of config space are supported");
92               if (i >= dd->allocated)   /* Need to re-allocate the buffer */
93                 {
94                   dump_alloc_data(dev, 4096);
95                   memcpy(((struct dump_data *) dev->aux)->data, dd->data, 256);
96                   pci_mfree(dd);
97                   dd = dev->aux;
98                 }
99               dd->data[i++] = j;
100               if (i > dd->len)
101                 dd->len = i;
102               z += 2;
103               if (*z)
104                 z++;
105             }
106           if (*z)
107             a->error("dump: Malformed line");
108         }
109     }
110 }
111
112 static void
113 dump_cleanup(struct pci_access *a UNUSED)
114 {
115 }
116
117 static void
118 dump_scan(struct pci_access *a UNUSED)
119 {
120 }
121
122 static int
123 dump_read(struct pci_dev *d, int pos, byte *buf, int len)
124 {
125   struct dump_data *dd;
126   if (!(dd = d->aux))
127     {
128       struct pci_dev *e = d->access->devices;
129       while (e && (e->domain != d->domain || e->bus != d->bus || e->dev != d->dev || e->func != d->func))
130         e = e->next;
131       if (!e)
132         return 0;
133       dd = e->aux;
134     }
135   if (pos + len > dd->len)
136     return 0;
137   memcpy(buf, dd->data + pos, len);
138   return 1;
139 }
140
141 static int
142 dump_write(struct pci_dev *d UNUSED, int pos UNUSED, byte *buf UNUSED, int len UNUSED)
143 {
144   d->access->error("Writing to dump files is not supported.");
145   return 0;
146 }
147
148 static void
149 dump_cleanup_dev(struct pci_dev *d)
150 {
151   if (d->aux)
152     {
153       pci_mfree(d->aux);
154       d->aux = NULL;
155     }
156 }
157
158 struct pci_methods pm_dump = {
159   "dump",
160   NULL,                                 /* config */
161   dump_detect,
162   dump_init,
163   dump_cleanup,
164   dump_scan,
165   pci_generic_fill_info,
166   dump_read,
167   dump_write,
168   NULL,                                 /* init_dev */
169   dump_cleanup_dev
170 };