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