[HAL] HalpDebugPciDumpBus: Fix parsing bugs
authorStanislav Motylkov <x86corez@gmail.com>
Mon, 24 May 2021 14:24:24 +0000 (17:24 +0300)
committerStanislav Motylkov <x86corez@gmail.com>
Mon, 24 May 2021 14:24:24 +0000 (17:24 +0300)
- Bridge devices don't have Subsystem ID and Subsystem Vendor ID
- Also they have only two BARs, don't parse data outside of bounds

Addendum to b232efe1. CORE-16319 CORE-16216

hal/halx86/legacy/bussupp.c

index 4b855ec..6e291d5 100644 (file)
@@ -791,6 +791,7 @@ HalpDebugPciDumpBus(IN ULONG i,
                     IN PPCI_COMMON_CONFIG PciData)
 {
     PCHAR p, ClassName, Boundary, SubClassName, VendorName, ProductName, SubVendorName;
+    UCHAR HeaderType;
     ULONG Length;
     CHAR LookupString[16] = "";
     CHAR bSubClassName[64] = "Unknown";
@@ -799,6 +800,8 @@ HalpDebugPciDumpBus(IN ULONG i,
     CHAR bSubVendorName[128] = "Unknown";
     ULONG Size, Mem, b;
 
+    HeaderType = (PciData->HeaderType & ~PCI_MULTIFUNCTION);
+
     /* Isolate the class name */
     sprintf(LookupString, "C %02x  ", PciData->BaseClass);
     ClassName = strstr((PCHAR)ClassTable, LookupString);
@@ -872,16 +875,20 @@ HalpDebugPciDumpBus(IN ULONG i,
                 p += strlen("\r\n");
             }
             Boundary = p;
+            SubVendorName = NULL;
 
-            /* Isolate the subvendor and subsystem name */
-            sprintf(LookupString,
-                    "\t\t%04x %04x  ",
-                    PciData->u.type0.SubVendorID,
-                    PciData->u.type0.SubSystemID);
-            SubVendorName = strstr(ProductName, LookupString);
-            if (Boundary && SubVendorName >= Boundary)
+            if (HeaderType == PCI_DEVICE_TYPE)
             {
-                SubVendorName = NULL;
+                /* Isolate the subvendor and subsystem name */
+                sprintf(LookupString,
+                        "\t\t%04x %04x  ",
+                        PciData->u.type0.SubVendorID,
+                        PciData->u.type0.SubSystemID);
+                SubVendorName = strstr(ProductName, LookupString);
+                if (Boundary && SubVendorName >= Boundary)
+                {
+                    SubVendorName = NULL;
+                }
             }
             if (SubVendorName)
             {
@@ -897,8 +904,7 @@ HalpDebugPciDumpBus(IN ULONG i,
     }
 
     /* Print out the data */
-    DbgPrint("%02x:%02x.%x %s [%02x%02x]: %s %s [%04x:%04x] (rev %02x)\n"
-             "\tSubsystem: %s [%04x:%04x]\n",
+    DbgPrint("%02x:%02x.%x %s [%02x%02x]: %s %s [%04x:%04x] (rev %02x)\n",
              i,
              j,
              k,
@@ -909,10 +915,15 @@ HalpDebugPciDumpBus(IN ULONG i,
              bProductName,
              PciData->VendorID,
              PciData->DeviceID,
-             PciData->RevisionID,
-             bSubVendorName,
-             PciData->u.type0.SubVendorID,
-             PciData->u.type0.SubSystemID);
+             PciData->RevisionID);
+
+    if (HeaderType == PCI_DEVICE_TYPE)
+    {
+        DbgPrint("\tSubsystem: %s [%04x:%04x]\n",
+                 bSubVendorName,
+                 PciData->u.type0.SubVendorID,
+                 PciData->u.type0.SubSystemID);
+    }
 
     /* Print out and decode flags */
     DbgPrint("\tFlags:");
@@ -929,8 +940,7 @@ HalpDebugPciDumpBus(IN ULONG i,
     else if (PciData->u.type0.InterruptPin != 0) DbgPrint(", IRQ assignment required");
     DbgPrint("\n");
 
-    if ((PciData->HeaderType & ~PCI_MULTIFUNCTION) == PCI_BRIDGE_TYPE &&
-        PciData->BaseClass == PCI_CLASS_BRIDGE_DEV)
+    if (HeaderType == PCI_BRIDGE_TYPE)
     {
         DbgPrint("\tBridge:");
         DbgPrint(" primary bus %d,", PciData->u.type1.PrimaryBus);
@@ -942,10 +952,13 @@ HalpDebugPciDumpBus(IN ULONG i,
 
     /* Scan addresses */
     Size = 0;
-    for (b = 0; b < PCI_TYPE0_ADDRESSES; b++)
+    for (b = 0; b < (HeaderType == PCI_DEVICE_TYPE ? PCI_TYPE0_ADDRESSES : PCI_TYPE1_ADDRESSES); b++)
     {
         /* Check for a BAR */
-        Mem = PciData->u.type0.BaseAddresses[b];
+        if (HeaderType != PCI_CARDBUS_BRIDGE_TYPE)
+            Mem = PciData->u.type0.BaseAddresses[b];
+        else
+            Mem = 0;
         if (Mem)
         {
             /* Decode the address type */