X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lib%2Fdump.c;h=829071f4b2baf8471ddc7ff3b7393134e8513ef5;hb=7d23054d18402b1891343f090d3cd37d7e83c82f;hp=04837e64f193304b9270cc4cf84df8a6ca9b8d9b;hpb=dc01dd60affb4688453f8b7204af66246f0850db;p=pciutils.git diff --git a/lib/dump.c b/lib/dump.c index 04837e6..829071f 100644 --- a/lib/dump.c +++ b/lib/dump.c @@ -1,9 +1,11 @@ /* * The PCI Library -- Reading of Bus Dumps * - * Copyright (c) 1997--2005 Martin Mares + * Copyright (c) 1997--2008 Martin Mares * - * Can be freely distributed and used under the terms of the GNU GPL. + * Can be freely distributed and used under the terms of the GNU GPL v2+. + * + * SPDX-License-Identifier: GPL-2.0-or-later */ #include @@ -18,10 +20,17 @@ struct dump_data { byte data[1]; }; +static void +dump_config(struct pci_access *a) +{ + pci_define_param(a, "dump.name", "", "Name of the bus dump file to read from"); +} + static int dump_detect(struct pci_access *a) { - return !!a->method_params[PCI_ACCESS_DUMP]; + char *name = pci_get_param(a, "dump.name"); + return name && name[0]; } static void @@ -31,7 +40,7 @@ dump_alloc_data(struct pci_dev *dev, int len) dd->allocated = len; dd->len = 0; memset(dd->data, 0xff, len); - dev->aux = dd; + dev->backend_data = dd; } static int @@ -49,13 +58,13 @@ dump_validate(char *s, char *fmt) static void dump_init(struct pci_access *a) { - char *name = a->method_params[PCI_ACCESS_DUMP]; + char *name = pci_get_param(a, "dump.name"); FILE *f; char buf[256]; struct pci_dev *dev = NULL; int len, mn, bn, dn, fn, i, j; - if (!a) + if (!name) a->error("dump: File name not given."); if (!(f = fopen(name, "r"))) a->error("dump: Cannot open %s: %s", name, strerror(errno)); @@ -63,14 +72,18 @@ dump_init(struct pci_access *a) { char *z = strchr(buf, '\n'); if (!z) - a->error("dump: line too long or unterminated"); + { + fclose(f); + a->error("dump: line too long or unterminated"); + } *z-- = 0; if (z >= buf && *z == '\r') *z-- = 0; len = z - buf + 1; mn = 0; if (dump_validate(buf, "##:##.# ") && sscanf(buf, "%x:%x.%d", &bn, &dn, &fn) == 3 || - dump_validate(buf, "####:##:##.# ") && sscanf(buf, "%x:%x:%x.%d", &mn, &bn, &dn, &fn) == 4) + dump_validate(buf, "####:##:##.# ") && sscanf(buf, "%x:%x:%x.%d", &mn, &bn, &dn, &fn) == 4 || + dump_validate(buf, "#####:##:##.# ") && sscanf(buf, "%x:%x:%x.%d", &mn, &bn, &dn, &fn) == 4) { dev = pci_get_dev(a, mn, bn, dn, fn); dump_alloc_data(dev, 256); @@ -79,22 +92,27 @@ dump_init(struct pci_access *a) else if (!len) dev = NULL; else if (dev && - (dump_validate(buf, "##: ") || dump_validate(buf, "###: ")) && + (dump_validate(buf, "##: ") || dump_validate(buf, "###: ") || dump_validate(buf, "####: ") || + dump_validate(buf, "#####: ") || dump_validate(buf, "######: ") || + dump_validate(buf, "#######: ") || dump_validate(buf, "########: ")) && sscanf(buf, "%x: ", &i) == 1) { - struct dump_data *dd = dev->aux; + struct dump_data *dd = dev->backend_data; z = strchr(buf, ' ') + 1; while (isxdigit(z[0]) && isxdigit(z[1]) && (!z[2] || z[2] == ' ') && sscanf(z, "%x", &j) == 1 && j < 256) { if (i >= 4096) - a->error("dump: At most 4096 bytes of config space are supported"); + { + fclose(f); + a->error("dump: At most 4096 bytes of config space are supported"); + } if (i >= dd->allocated) /* Need to re-allocate the buffer */ { dump_alloc_data(dev, 4096); - memcpy(((struct dump_data *) dev->aux)->data, dd->data, 256); + memcpy(((struct dump_data *) dev->backend_data)->data, dd->data, 256); pci_mfree(dd); - dd = dev->aux; + dd = dev->backend_data; } dd->data[i++] = j; if (i > dd->len) @@ -104,9 +122,13 @@ dump_init(struct pci_access *a) z++; } if (*z) - a->error("dump: Malformed line"); + { + fclose(f); + a->error("dump: Malformed line"); + } } } + fclose(f); } static void @@ -123,14 +145,14 @@ static int dump_read(struct pci_dev *d, int pos, byte *buf, int len) { struct dump_data *dd; - if (!(dd = d->aux)) + if (!(dd = d->backend_data)) { struct pci_dev *e = d->access->devices; while (e && (e->domain != d->domain || e->bus != d->bus || e->dev != d->dev || e->func != d->func)) e = e->next; if (!e) return 0; - dd = e->aux; + dd = e->backend_data; } if (pos + len > dd->len) return 0; @@ -148,16 +170,17 @@ dump_write(struct pci_dev *d UNUSED, int pos UNUSED, byte *buf UNUSED, int len U static void dump_cleanup_dev(struct pci_dev *d) { - if (d->aux) + if (d->backend_data) { - pci_mfree(d->aux); - d->aux = NULL; + pci_mfree(d->backend_data); + d->backend_data = NULL; } } struct pci_methods pm_dump = { "dump", - NULL, /* config */ + "Reading of register dumps (set the `dump.name' parameter)", + dump_config, dump_detect, dump_init, dump_cleanup, @@ -165,6 +188,7 @@ struct pci_methods pm_dump = { pci_generic_fill_info, dump_read, dump_write, + NULL, /* read_vpd */ NULL, /* init_dev */ dump_cleanup_dev };