Sebastian Herbszt
2008-Jun-11 20:53 UTC
[syslinux] [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 Index: syslinux-20080611/com32/lib/pci/scan.c ==================================================================--- syslinux-20080611.orig/com32/lib/pci/scan.c 2008-06-11 20:28:35.000000000 +0200 +++ syslinux-20080611/com32/lib/pci/scan.c 2008-06-11 21:19:22.000000000 +0200 @@ -351,8 +351,8 @@ 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 @@ 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; + for (func = 0; func < maxfunc; func++) { a = pci_mkaddr(bus, dev, func, 0); did = pci_readl(a); @@ -374,7 +374,8 @@ hdrtype = pci_readb(a + 0x0e); if (hdrtype & 0x80) - maxfunc = 7; /* Multifunction device */ + /* Multifunction device */ + maxfunc = MAX_PCI_FUNC; rid = pci_readb(a + 0x08); sid = pci_readl(a + 0x2c); @@ -401,7 +402,7 @@ } /* 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++; } Index: syslinux-20080611/com32/include/sys/pci.h ==================================================================--- syslinux-20080611.orig/com32/include/sys/pci.h 2008-06-11 20:28:35.000000000 +0200 +++ syslinux-20080611/com32/include/sys/pci.h 2008-06-11 21:19:22.000000000 +0200 @@ -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;