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