]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Pci/NonDiscoverablePciDeviceDxe/NonDiscoverablePciDeviceIo.c
MdeModulePkg/Pci: Fixed Asserts in SCT PCIIO Protocol Test.
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / NonDiscoverablePciDeviceDxe / NonDiscoverablePciDeviceIo.c
index c836ad6a9162fa7b2b346598cb42932263a7f9c7..c3e83003a01c5e79c7b04e82d84722a2bd885ad1 100644 (file)
@@ -3,13 +3,7 @@
   Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r
   Copyright (c) 2016, Linaro, Ltd. All rights reserved.<BR>\r
 \r
-  This program and the accompanying materials\r
-  are licensed and made available under the terms and conditions of the BSD License\r
-  which accompanies this distribution.  The full text of the license may be found at\r
-  http://opensource.org/licenses/bsd-license.php\r
-\r
-  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
@@ -52,6 +46,10 @@ GetBarResource (
 \r
   BarIndex -= (UINT8)Dev->BarOffset;\r
 \r
+  if (BarIndex >= Dev->BarCount) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
   for (Desc = Dev->Device->Resources;\r
        Desc->Desc != ACPI_END_TAG_DESCRIPTOR;\r
        Desc = (VOID *)((UINT8 *)Desc + Desc->Len + 3)) {\r
@@ -95,6 +93,31 @@ PciIoPollMem (
   OUT UINT64                      *Result\r
   )\r
 {\r
+  NON_DISCOVERABLE_PCI_DEVICE         *Dev;\r
+  EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR   *Desc;\r
+  UINTN                               Count;\r
+  EFI_STATUS                          Status;\r
+\r
+  if ((UINT32)Width > EfiPciIoWidthUint64) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Result == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Dev = NON_DISCOVERABLE_PCI_DEVICE_FROM_PCI_IO(This);\r
+  Count = 1;\r
+\r
+  Status = GetBarResource (Dev, BarIndex, &Desc);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  if (Offset + (Count << (Width & 0x3)) > Desc->AddrLen) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
   ASSERT (FALSE);\r
   return EFI_UNSUPPORTED;\r
 }\r
@@ -128,6 +151,31 @@ PciIoPollIo (
   OUT UINT64                      *Result\r
   )\r
 {\r
+  NON_DISCOVERABLE_PCI_DEVICE         *Dev;\r
+  EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR   *Desc;\r
+  UINTN                               Count;\r
+  EFI_STATUS                          Status;\r
+\r
+  if ((UINT32)Width > EfiPciIoWidthUint64) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Result == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Dev = NON_DISCOVERABLE_PCI_DEVICE_FROM_PCI_IO(This);\r
+  Count = 1;\r
+\r
+  Status = GetBarResource (Dev, BarIndex, &Desc);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  if (Offset + (Count << (Width & 0x3)) > Desc->AddrLen) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
   ASSERT (FALSE);\r
   return EFI_UNSUPPORTED;\r
 }\r
@@ -398,6 +446,29 @@ PciIoIoRead (
   IN OUT VOID                         *Buffer\r
   )\r
 {\r
+  NON_DISCOVERABLE_PCI_DEVICE         *Dev;\r
+  EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR   *Desc;\r
+  EFI_STATUS                          Status;\r
+\r
+  if ((UINT32)Width >= EfiPciIoWidthMaximum) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Buffer == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Dev = NON_DISCOVERABLE_PCI_DEVICE_FROM_PCI_IO(This);\r
+\r
+  Status = GetBarResource (Dev, BarIndex, &Desc);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  if (Offset + (Count << (Width & 0x3)) > Desc->AddrLen) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
   ASSERT (FALSE);\r
   return EFI_UNSUPPORTED;\r
 }\r
@@ -427,6 +498,29 @@ PciIoIoWrite (
   IN OUT VOID                         *Buffer\r
   )\r
 {\r
+  NON_DISCOVERABLE_PCI_DEVICE         *Dev;\r
+  EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR   *Desc;\r
+  EFI_STATUS                          Status;\r
+\r
+  if ((UINT32)Width >= EfiPciIoWidthMaximum) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Buffer == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Dev = NON_DISCOVERABLE_PCI_DEVICE_FROM_PCI_IO(This);\r
+\r
+  Status = GetBarResource (Dev, BarIndex, &Desc);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  if (Offset + (Count << (Width & 0x3)) > Desc->AddrLen) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
   ASSERT (FALSE);\r
   return EFI_UNSUPPORTED;\r
 }\r
@@ -465,6 +559,11 @@ PciIoPciRead (
   Address = (UINT8 *)&Dev->ConfigSpace + Offset;\r
   Length = Count << ((UINTN)Width & 0x3);\r
 \r
+  if (Offset >= sizeof (Dev->ConfigSpace)) {\r
+    ZeroMem (Buffer, Length);\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
   if (Offset + Length > sizeof (Dev->ConfigSpace)) {\r
     //\r
     // Read all zeroes for config space accesses beyond the first\r
@@ -553,6 +652,35 @@ PciIoCopyMem (
   IN     UINTN                        Count\r
   )\r
 {\r
+  NON_DISCOVERABLE_PCI_DEVICE         *Dev;\r
+  EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR   *DestDesc;\r
+  EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR   *SrcDesc;\r
+  EFI_STATUS                          Status;\r
+\r
+  if ((UINT32)Width > EfiPciIoWidthUint64) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Dev = NON_DISCOVERABLE_PCI_DEVICE_FROM_PCI_IO(This);\r
+\r
+  Status = GetBarResource (Dev, DestBarIndex, &DestDesc);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  if (DestOffset + (Count << (Width & 0x3)) > DestDesc->AddrLen) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
+  Status = GetBarResource (Dev, SrcBarIndex, &SrcDesc);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  if (SrcOffset + (Count << (Width & 0x3)) > SrcDesc->AddrLen) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
   ASSERT (FALSE);\r
   return EFI_UNSUPPORTED;\r
 }\r
@@ -592,6 +720,19 @@ CoherentPciIoMap (
   EFI_STATUS                            Status;\r
   NON_DISCOVERABLE_PCI_DEVICE_MAP_INFO  *MapInfo;\r
 \r
+  if (Operation != EfiPciIoOperationBusMasterRead &&\r
+      Operation != EfiPciIoOperationBusMasterWrite &&\r
+      Operation != EfiPciIoOperationBusMasterCommonBuffer) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (HostAddress   == NULL ||\r
+      NumberOfBytes == NULL ||\r
+      DeviceAddress == NULL ||\r
+      Mapping       == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   //\r
   // If HostAddress exceeds 4 GB, and this device does not support 64-bit DMA\r
   // addressing, we need to allocate a bounce buffer and copy over the data.\r
@@ -715,6 +856,11 @@ CoherentPciIoAllocateBuffer (
     return EFI_UNSUPPORTED;\r
   }\r
 \r
+  if ((MemoryType != EfiBootServicesData) &&\r
+      (MemoryType != EfiRuntimeServicesData)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   //\r
   // Allocate below 4 GB if the dual address cycle attribute has not\r
   // been set. If the system has no memory available below 4 GB, there\r
@@ -872,6 +1018,10 @@ NonCoherentPciIoAllocateBuffer (
   NON_DISCOVERABLE_DEVICE_UNCACHED_ALLOCATION *Alloc;\r
   VOID                                        *AllocAddress;\r
 \r
+  if (HostAddress == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   Dev = NON_DISCOVERABLE_PCI_DEVICE_FROM_PCI_IO(This);\r
 \r
   Status = CoherentPciIoAllocateBuffer (This, Type, MemoryType, Pages,\r
@@ -990,6 +1140,19 @@ NonCoherentPciIoMap (
   EFI_GCD_MEMORY_SPACE_DESCRIPTOR       GcdDescriptor;\r
   BOOLEAN                               Bounce;\r
 \r
+  if (HostAddress   == NULL ||\r
+      NumberOfBytes == NULL ||\r
+      DeviceAddress == NULL ||\r
+      Mapping       == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Operation != EfiPciIoOperationBusMasterRead &&\r
+      Operation != EfiPciIoOperationBusMasterWrite &&\r
+      Operation != EfiPciIoOperationBusMasterCommonBuffer) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   MapInfo = AllocatePool (sizeof *MapInfo);\r
   if (MapInfo == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
@@ -1176,6 +1339,8 @@ PciIoGetLocation (
   OUT  UINTN                *FunctionNumber\r
   )\r
 {\r
+  NON_DISCOVERABLE_PCI_DEVICE         *Dev;\r
+\r
   if (SegmentNumber == NULL ||\r
       BusNumber == NULL ||\r
       DeviceNumber == NULL ||\r
@@ -1183,9 +1348,11 @@ PciIoGetLocation (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  *SegmentNumber  = 0;\r
-  *BusNumber      = 0xff;\r
-  *DeviceNumber   = 0;\r
+  Dev = NON_DISCOVERABLE_PCI_DEVICE_FROM_PCI_IO(This);\r
+\r
+  *SegmentNumber  = 0xff;\r
+  *BusNumber      = Dev->UniqueId >> 5;\r
+  *DeviceNumber   = Dev->UniqueId & 0x1f;\r
   *FunctionNumber = 0;\r
 \r
   return EFI_SUCCESS;\r
@@ -1225,6 +1392,10 @@ PciIoAttributes (
 \r
   Dev = NON_DISCOVERABLE_PCI_DEVICE_FROM_PCI_IO(This);\r
 \r
+  if ((Attributes & (~(DEV_SUPPORTED_ATTRIBUTES))) != 0) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
   Enable = FALSE;\r
   switch (Operation) {\r
   case EfiPciIoAttributeOperationGet:\r
@@ -1238,7 +1409,7 @@ PciIoAttributes (
     if (Result == NULL) {\r
       return EFI_INVALID_PARAMETER;\r
     }\r
-    *Result = EFI_PCI_DEVICE_ENABLE | EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE;\r
+    *Result = DEV_SUPPORTED_ATTRIBUTES;\r
     break;\r
 \r
   case EfiPciIoAttributeOperationEnable:\r
@@ -1365,6 +1536,33 @@ PciIoSetBarAttributes (
   IN OUT UINT64                       *Length\r
   )\r
 {\r
+  NON_DISCOVERABLE_PCI_DEVICE         *Dev;\r
+  EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR   *Desc;\r
+  EFI_PCI_IO_PROTOCOL_WIDTH           Width;\r
+  UINTN                               Count;\r
+  EFI_STATUS                          Status;\r
+\r
+  if ((Attributes & (~DEV_SUPPORTED_ATTRIBUTES)) != 0) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
+  if (Offset == NULL || Length == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Dev = NON_DISCOVERABLE_PCI_DEVICE_FROM_PCI_IO(This);\r
+  Width = EfiPciIoWidthUint8;\r
+  Count = (UINT32) *Length;\r
+\r
+  Status = GetBarResource(Dev, BarIndex, &Desc);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  if (*Offset + (Count << (Width & 0x3)) > Desc->AddrLen) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
   ASSERT (FALSE);\r
   return EFI_UNSUPPORTED;\r
 }\r