]> mj.ucw.cz Git - pciutils.git/blob - lib/dump.c
Merge branch 'master' of https://github.com/rohit-mundra/pciutils
[pciutils.git] / lib / dump.c
1 /*
2  *      The PCI Library -- Reading of Bus Dumps
3  *
4  *      Copyright (c) 1997--2008 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 void
22 dump_config(struct pci_access *a)
23 {
24   pci_define_param(a, "dump.name", "", "Name of the bus dump file to read from");
25 }
26
27 static int
28 dump_detect(struct pci_access *a)
29 {
30   char *name = pci_get_param(a, "dump.name");
31   return name && name[0];
32 }
33
34 static void
35 dump_alloc_data(struct pci_dev *dev, int len)
36 {
37   struct dump_data *dd = pci_malloc(dev->access, sizeof(struct dump_data) + len - 1);
38   dd->allocated = len;
39   dd->len = 0;
40   memset(dd->data, 0xff, len);
41   dev->aux = dd;
42 }
43
44 static int
45 dump_validate(char *s, char *fmt)
46 {
47   while (*fmt)
48     {
49       if (*fmt == '#' ? !isxdigit(*s) : *fmt != *s)
50         return 0;
51       fmt++, s++;
52     }
53   return 1;
54 }
55
56 static void
57 dump_init(struct pci_access *a)
58 {
59   char *name = pci_get_param(a, "dump.name");
60   FILE *f;
61   char buf[256];
62   struct pci_dev *dev = NULL;
63   int len, mn, bn, dn, fn, i, j;
64
65   if (!name)
66     a->error("dump: File name not given.");
67   if (!(f = fopen(name, "r")))
68     a->error("dump: Cannot open %s: %s", name, strerror(errno));
69   while (fgets(buf, sizeof(buf)-1, f))
70     {
71       char *z = strchr(buf, '\n');
72       if (!z)
73         {
74           fclose(f);
75           a->error("dump: line too long or unterminated");
76         }
77       *z-- = 0;
78       if (z >= buf && *z == '\r')
79         *z-- = 0;
80       len = z - buf + 1;
81       mn = 0;
82       if (dump_validate(buf, "##:##.# ") && sscanf(buf, "%x:%x.%d", &bn, &dn, &fn) == 3 ||
83           dump_validate(buf, "####:##:##.# ") && sscanf(buf, "%x:%x:%x.%d", &mn, &bn, &dn, &fn) == 4 ||
84           dump_validate(buf, "#####:##:##.# ") && sscanf(buf, "%x:%x:%x.%d", &mn, &bn, &dn, &fn) == 4)
85         {
86           dev = pci_get_dev(a, mn, bn, dn, fn);
87           dump_alloc_data(dev, 256);
88           pci_link_dev(a, dev);
89         }
90       else if (!len)
91         dev = NULL;
92       else if (dev &&
93                (dump_validate(buf, "##: ") || dump_validate(buf, "###: ")) &&
94                sscanf(buf, "%x: ", &i) == 1)
95         {
96           struct dump_data *dd = dev->aux;
97           z = strchr(buf, ' ') + 1;
98           while (isxdigit(z[0]) && isxdigit(z[1]) && (!z[2] || z[2] == ' ') &&
99                  sscanf(z, "%x", &j) == 1 && j < 256)
100             {
101               if (i >= 4096)
102                 {
103                   fclose(f);
104                   a->error("dump: At most 4096 bytes of config space are supported");
105                 }
106               if (i >= dd->allocated)   /* Need to re-allocate the buffer */
107                 {
108                   dump_alloc_data(dev, 4096);
109                   memcpy(((struct dump_data *) dev->aux)->data, dd->data, 256);
110                   pci_mfree(dd);
111                   dd = dev->aux;
112                 }
113               dd->data[i++] = j;
114               if (i > dd->len)
115                 dd->len = i;
116               z += 2;
117               if (*z)
118                 z++;
119             }
120           if (*z)
121             {
122               fclose(f);
123               a->error("dump: Malformed line");
124             }
125         }
126     }
127   fclose(f);
128 }
129
130 static void
131 dump_cleanup(struct pci_access *a UNUSED)
132 {
133 }
134
135 static void
136 dump_scan(struct pci_access *a UNUSED)
137 {
138 }
139
140 static int
141 dump_read(struct pci_dev *d, int pos, byte *buf, int len)
142 {
143   struct dump_data *dd;
144   if (!(dd = d->aux))
145     {
146       struct pci_dev *e = d->access->devices;
147       while (e && (e->domain != d->domain || e->bus != d->bus || e->dev != d->dev || e->func != d->func))
148         e = e->next;
149       if (!e)
150         return 0;
151       dd = e->aux;
152     }
153   if (pos + len > dd->len)
154     return 0;
155   memcpy(buf, dd->data + pos, len);
156   return 1;
157 }
158
159 static int
160 dump_write(struct pci_dev *d UNUSED, int pos UNUSED, byte *buf UNUSED, int len UNUSED)
161 {
162   d->access->error("Writing to dump files is not supported.");
163   return 0;
164 }
165
166 static void
167 dump_cleanup_dev(struct pci_dev *d)
168 {
169   if (d->aux)
170     {
171       pci_mfree(d->aux);
172       d->aux = NULL;
173     }
174 }
175
176 struct pci_methods pm_dump = {
177   "dump",
178   "Reading of register dumps (set the `dump.name' parameter)",
179   dump_config,
180   dump_detect,
181   dump_init,
182   dump_cleanup,
183   dump_scan,
184   pci_generic_fill_info,
185   dump_read,
186   dump_write,
187   NULL,                                 /* read_vpd */
188   NULL,                                 /* init_dev */
189   dump_cleanup_dev
190 };