pci: fix off-by-one error and introduce MAX_PCI_FUNC
authorSebastian Herbszt <herbszt@gmx.de>
Wed, 11 Jun 2008 20:53:01 +0000 (22:53 +0200)
committerH. Peter Anvin <hpa@zytor.com>
Fri, 13 Jun 2008 16:19:57 +0000 (09:19 -0700)
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
com32/lib/pci/scan.c

index a49475b..3b07ae7 100644 (file)
@@ -4,8 +4,9 @@
 #include <inttypes.h>
 #include <sys/io.h>
 
+#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;
 
index 94c83bb..be1a722 100644 (file)
@@ -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++;
     }