]> mj.ucw.cz Git - pciutils.git/blob - lib/dump.c
4baeae407ecce05b53018b2261dfc39e933d0939
[pciutils.git] / lib / dump.c
1 /*
2  *      The PCI Library -- Reading of Bus Dumps
3  *
4  *      Copyright (c) 1997--2003 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 static int
17 dump_detect(struct pci_access *a)
18 {
19   return !!a->method_params[PCI_ACCESS_DUMP];
20 }
21
22 static void
23 dump_init(struct pci_access *a)
24 {
25   char *name = a->method_params[PCI_ACCESS_DUMP];
26   FILE *f;
27   char buf[256];
28   struct pci_dev *dev = NULL;
29   int len, mn, bn, dn, fn, i, j;
30
31   if (!a)
32     a->error("dump: File name not given.");
33   if (!(f = fopen(name, "r")))
34     a->error("dump: Cannot open %s: %s", name, strerror(errno));
35   while (fgets(buf, sizeof(buf)-1, f))
36     {
37       char *z = strchr(buf, '\n');
38       if (!z)
39         a->error("dump: line too long or unterminated");
40       *z-- = 0;
41       if (z >= buf && *z == '\r')
42         *z-- = 0;
43       len = z - buf + 1;
44       mn = 0;
45       if ((len >= 8 && buf[2] == ':' && buf[5] == '.' && buf[7] == ' ' &&
46            sscanf(buf, "%x:%x.%d ", &bn, &dn, &fn) == 3) ||
47           (len >= 13 && buf[4] == ':' && buf[7] == ':' && buf[10] == '.' && buf[12] == ' ' &&
48            sscanf(buf, "%x:%x:%x.%d", &mn, &bn, &dn, &fn) == 4))
49         {
50           dev = pci_get_dev(a, mn, bn, dn, fn);
51           dev->aux = pci_malloc(a, 256);
52           memset(dev->aux, 0xff, 256);
53           pci_link_dev(a, dev);
54         }
55       else if (!len)
56         dev = NULL;
57       else if (dev && len >= 51 && buf[2] == ':' && buf[3] == ' ' &&
58                sscanf(buf, "%x: ", &i) == 1)
59         {
60           z = buf+3;
61           while (isspace(z[0]) && isxdigit(z[1]) && isxdigit(z[2]))
62             {
63               z++;
64               if (sscanf(z, "%x", &j) != 1 || i >= 256)
65                 a->error("dump: Malformed line");
66               ((byte *) dev->aux)[i++] = j;
67               z += 2;
68             }
69         }
70     }
71 }
72
73 static void
74 dump_cleanup(struct pci_access *a UNUSED)
75 {
76 }
77
78 static void
79 dump_scan(struct pci_access *a UNUSED)
80 {
81 }
82
83 static int
84 dump_read(struct pci_dev *d, int pos, byte *buf, int len)
85 {
86   if (!d->aux)
87     {
88       struct pci_dev *e = d->access->devices;
89       while (e && (e->bus != d->bus || e->dev != d->dev || e->func != d->func))
90         e = e->next;
91       if (e)
92         d = e;
93       else
94         return 0;
95     }
96   memcpy(buf, (byte *) d->aux + pos, len);
97   return 1;
98 }
99
100 static int
101 dump_write(struct pci_dev *d UNUSED, int pos UNUSED, byte *buf UNUSED, int len UNUSED)
102 {
103   d->access->error("Writing to dump files is not supported.");
104   return 0;
105 }
106
107 static void
108 dump_cleanup_dev(struct pci_dev *d)
109 {
110   if (d->aux)
111     {
112       pci_mfree(d->aux);
113       d->aux = NULL;
114     }
115 }
116
117 struct pci_methods pm_dump = {
118   "dump",
119   NULL,                                 /* config */
120   dump_detect,
121   dump_init,
122   dump_cleanup,
123   dump_scan,
124   pci_generic_fill_info,
125   dump_read,
126   dump_write,
127   NULL,                                 /* init_dev */
128   dump_cleanup_dev
129 };