summaryrefslogtreecommitdiff
path: root/com32/sysdump/pci.c
blob: 1d6872799631102c58a36a7848c0bf03707bb4dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
 * Dump PCI device headers
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/pci.h>
#include "sysdump.h"
#include "backend.h"

static void dump_pci_device(struct backend *be, pciaddr_t a, uint8_t hdrtype)
{
    unsigned int bus  = pci_bus(a);
    unsigned int dev  = pci_dev(a);
    unsigned int func = pci_func(a);
    uint8_t data[256];
    unsigned int i;
    char filename[32];

    hdrtype &= 0x7f;

    printf("Scanning PCI bus... %02x:%02x.%x\r", bus, dev, func);

    /* Assume doing a full device dump is actually safe... */
    for (i = 0; i < sizeof data; i += 4)
	*(uint32_t *)(data+i) = pci_readl(a + i);

    snprintf(filename, sizeof filename, "pci/%02x:%02x.%x",
	     bus, dev, func);
    cpio_writefile(be, filename, data, sizeof data);
}

void dump_pci(struct backend *be)
{
    int cfgtype;
    unsigned int nbus, ndev, nfunc, maxfunc;
    pciaddr_t a;
    uint32_t did;
    uint8_t hdrtype;

    cfgtype = pci_set_config_type(PCI_CFG_AUTO);
    if (cfgtype == PCI_CFG_NONE)
	return;

    cpio_mkdir(be, "pci");

    for (nbus = 0; nbus < MAX_PCI_BUSES; nbus++) {
	for (ndev = 0; ndev < MAX_PCI_DEVICES; ndev++) {
	    maxfunc = 1;	/* Assume a single-function device */

	    for (nfunc = 0; nfunc < maxfunc; nfunc++) {
		a = pci_mkaddr(nbus, ndev, nfunc, 0);
		did = pci_readl(a);

		if (did == 0xffffffff || did == 0xffff0000 ||
		    did == 0x0000ffff || did == 0x00000000)
		    continue;

		hdrtype = pci_readb(a + 0x0e);
		if (hdrtype & 0x80)
		    maxfunc = MAX_PCI_FUNC;	/* Multifunction device */

		dump_pci_device(be, a, hdrtype);
	    }
	}
    }

    printf("Scanning PCI bus... done.  \n");
}