From eab6f0763744c964eded45b48ec41cb6c1f65cdb Mon Sep 17 00:00:00 2001 From: Sebastian Herbszt Date: Wed, 11 Jun 2008 22:53:01 +0200 Subject: [PATCH] pci: fix off-by-one error and introduce MAX_PCI_FUNC In include/sys/pci.h we have #define MAX_PCI_BUSES 255 and struct pci_bus_list { struct pci_bus pci_bus[MAX_PCI_BUSES]; uint8_t count; }; And in lib/pci/scan.c for (bus = 0; bus <= MAX_PCI_BUSES; bus++) { pci_bus_list->pci_bus[bus].pci_device_count = 0; Fix possible overflows and introduce MAX_PCI_FUNC. - Sebastian --- com32/include/sys/pci.h | 3 ++- com32/lib/pci/scan.c | 14 +++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/com32/include/sys/pci.h b/com32/include/sys/pci.h index a49475b..3b07ae7 100644 --- a/com32/include/sys/pci.h +++ b/com32/include/sys/pci.h @@ -4,8 +4,9 @@ #include #include +#define MAX_PCI_FUNC 8 #define MAX_PCI_DEVICES 32 -#define MAX_PCI_BUSES 255 +#define MAX_PCI_BUSES 256 typedef uint32_t pciaddr_t; diff --git a/com32/lib/pci/scan.c b/com32/lib/pci/scan.c index 94c83bb..be1a722 100644 --- a/com32/lib/pci/scan.c +++ b/com32/lib/pci/scan.c @@ -351,8 +351,8 @@ int pci_scan(struct pci_bus_list * pci_bus_list, struct pci_device_list * pci_de dprintf("PCI configuration type %d\n", cfgtype); dprintf("Scanning PCI Buses\n"); - /* We try to detect 255 buses */ - for (bus = 0; bus <= MAX_PCI_BUSES; bus++) { + /* We try to detect 256 buses */ + for (bus = 0; bus < MAX_PCI_BUSES; bus++) { dprintf("Probing bus 0x%02x... \n", bus); @@ -360,9 +360,9 @@ int pci_scan(struct pci_bus_list * pci_bus_list, struct pci_device_list * pci_de pci_bus_list->pci_bus[bus].pci_device_count = 0; pci_bus_list->count = 0;; - for (dev = 0; dev <= 0x1f; dev++) { - maxfunc = 0; - for (func = 0; func <= maxfunc; func++) { + for (dev = 0; dev < MAX_PCI_DEVICES ; dev++) { + maxfunc = 1; /* Assume a single-function device */ + for (func = 0; func < maxfunc; func++) { a = pci_mkaddr(bus, dev, func, 0); did = pci_readl(a); @@ -374,7 +374,7 @@ int pci_scan(struct pci_bus_list * pci_bus_list, struct pci_device_list * pci_de hdrtype = pci_readb(a + 0x0e); if (hdrtype & 0x80) - maxfunc = 7; /* Multifunction device */ + maxfunc = MAX_PCI_FUNC; /* Multifunction device */ rid = pci_readb(a + 0x08); sid = pci_readl(a + 0x2c); @@ -401,7 +401,7 @@ int pci_scan(struct pci_bus_list * pci_bus_list, struct pci_device_list * pci_de } /* Detecting pci buses that have pci devices connected */ - for (bus = 0; bus <= 0xff; bus++) { + for (bus = 0; bus < MAX_PCI_BUSES; bus++) { if (pci_bus_list->pci_bus[bus].pci_device_count > 0) { pci_bus_list->count++; } -- 2.7.4