public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [edk2-devel] [PATCH V1 1/2] MdeModulePkg: Check legal BarIndex/NumberofBars
@ 2024-10-16  6:28 Foster Nong
  2024-10-16  6:28 ` [edk2-devel] [PATCH V1 2/2] MdeModulePkg:New Pcd to platform constrain BarSize Foster Nong
  0 siblings, 1 reply; 2+ messages in thread
From: Foster Nong @ 2024-10-16  6:28 UTC (permalink / raw)
  To: devel; +Cc: Foster Nong, Michael D Kinney, Liming Gao, Ray Ni

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4868
In current PCIE Resizable BAR feature, add below check:
1. Check Register Number of Resizable BARs is legal.
2. Check Register Register BAR Index is legal.

Signed-off-by: Foster Nong <foster.nong@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Ray Ni <ray.ni@intel.com>
---
 MdeModulePkg/Bus/Pci/PciBusDxe/PciLib.c | 41 ++++++++++++++++++-------
 1 file changed, 30 insertions(+), 11 deletions(-)

diff --git a/MdeModulePkg/Bus/Pci/PciBusDxe/PciLib.c b/MdeModulePkg/Bus/Pci/PciBusDxe/PciLib.c
index 84fc0161a1..f15dd6468d 100644
--- a/MdeModulePkg/Bus/Pci/PciBusDxe/PciLib.c
+++ b/MdeModulePkg/Bus/Pci/PciBusDxe/PciLib.c
@@ -1800,7 +1800,6 @@ PciProgramResizableBar (
   UINT32                                                 Index;
   UINT32                                                 Offset;
   INTN                                                   Bit;
-  UINTN                                                  ResizableBarNumber;
   EFI_STATUS                                             Status;
   PCI_EXPRESS_EXTENDED_CAPABILITIES_RESIZABLE_BAR_ENTRY  Entries[PCI_MAX_BAR];
 
@@ -1812,19 +1811,39 @@ PciProgramResizableBar (
     PciIoDevice->ResizableBarOffset,
     PciIoDevice->ResizableBarNumber
     ));
+  if (PciIoDevice->ResizableBarNumber > PCI_MAX_BAR || PciIoDevice->ResizableBarNumber == 0) {
+    DEBUG ((DEBUG_ERROR, "ERROR: Resizable BAR register ResizableBarNumber=0x%X is illegal\n", PciIoDevice->ResizableBarNumber));
+    return EFI_DEVICE_ERROR;
+  }
 
-  ResizableBarNumber = MIN (PciIoDevice->ResizableBarNumber, PCI_MAX_BAR);
-  PciIo              = &PciIoDevice->PciIo;
-  Status             = PciIo->Pci.Read (
-                                    PciIo,
-                                    EfiPciIoWidthUint8,
-                                    PciIoDevice->ResizableBarOffset + sizeof (PCI_EXPRESS_EXTENDED_CAPABILITIES_HEADER),
-                                    sizeof (PCI_EXPRESS_EXTENDED_CAPABILITIES_RESIZABLE_BAR_ENTRY) * ResizableBarNumber,
-                                    (VOID *)(&Entries)
-                                    );
+  PciIo = &PciIoDevice->PciIo;
+  Status = PciIo->Pci.Read (
+                        PciIo,
+                        EfiPciIoWidthUint8,
+                        PciIoDevice->ResizableBarOffset + sizeof (PCI_EXPRESS_EXTENDED_CAPABILITIES_HEADER),
+                        sizeof (PCI_EXPRESS_EXTENDED_CAPABILITIES_RESIZABLE_BAR_ENTRY) * PciIoDevice->ResizableBarNumber,
+                        (VOID *)(&Entries)
+                        );
   ASSERT_EFI_ERROR (Status);
 
-  for (Index = 0; Index < ResizableBarNumber; Index++) {
+  for (Index = 0; Index < PciIoDevice->ResizableBarNumber; Index++) {
+    //
+    // BAR index: encoded value
+    // 0          BAR located at offset 10h
+    // 1          BAR located at offset 14h
+    // 2          BAR located at offset 18h
+    // 3          BAR located at offset 1ch
+    // 4          BAR located at offset 20h
+    // 5          BAR located at offset 24h
+    // Others     Reserved.
+    // Do not configure anything if some BAR info is wrong.
+    //
+    if (Entries[Index].ResizableBarControl.Bits.BarIndex >= PCI_MAX_BAR ) {
+      DEBUG ((DEBUG_ERROR, "ERROR: Resizable BAR Entry[%x].BarIndex=%x is illegal\n", Index, Entries[Index].ResizableBarControl.Bits.BarIndex));
+      return EFI_DEVICE_ERROR;
+    }
+  }
+  for (Index = 0; Index < PciIoDevice->ResizableBarNumber; Index++) {
     //
     // When the bit of Capabilities Set, indicates that the Function supports
     // operating with the BAR sized to (2^Bit) MB.
-- 
2.37.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#120664): https://edk2.groups.io/g/devel/message/120664
Mute This Topic: https://groups.io/mt/109174499/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-10-23 17:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-16  6:28 [edk2-devel] [PATCH V1 1/2] MdeModulePkg: Check legal BarIndex/NumberofBars Foster Nong
2024-10-16  6:28 ` [edk2-devel] [PATCH V1 2/2] MdeModulePkg:New Pcd to platform constrain BarSize Foster Nong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox