]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Pci/PciBusDxe/PciOptionRomSupport.c
MdeModulePkg/PciBusDxe: dispatch option ROMs for foreign architectures
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / PciBusDxe / PciOptionRomSupport.c
index 33dbfa4af72db94cf9c9e750afb43769f68aa690..c994ed5fe34b8cad0c1aac80230f5cd8df1952c4 100644 (file)
@@ -1,14 +1,8 @@
 /** @file\r
   PCI Rom supporting funtions implementation for PCI Bus module.\r
 \r
-Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>\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
+Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
@@ -305,10 +299,67 @@ GetOpRomInfo (
     return EFI_NOT_FOUND;\r
   }\r
 \r
-  PciIoDevice->RomSize = (UINT64) ((~AllOnes) + 1);\r
+  PciIoDevice->RomSize = (~AllOnes) + 1;\r
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Check if the RomImage contains EFI Images.\r
+\r
+  @param  RomImage  The ROM address of Image for check.\r
+  @param  RomSize   Size of ROM for check.\r
+\r
+  @retval TRUE     ROM contain EFI Image.\r
+  @retval FALSE    ROM not contain EFI Image.\r
+\r
+**/\r
+BOOLEAN\r
+ContainEfiImage (\r
+  IN VOID            *RomImage,\r
+  IN UINT64          RomSize\r
+  )\r
+{\r
+  PCI_EXPANSION_ROM_HEADER  *RomHeader;\r
+  PCI_DATA_STRUCTURE        *RomPcir;\r
+  UINT8                     Indicator;\r
+\r
+  Indicator = 0;\r
+  RomHeader = RomImage;\r
+  if (RomHeader == NULL) {\r
+    return FALSE;\r
+  }\r
+\r
+  do {\r
+    if (RomHeader->Signature != PCI_EXPANSION_ROM_HEADER_SIGNATURE) {\r
+      RomHeader = (PCI_EXPANSION_ROM_HEADER *) ((UINT8 *) RomHeader + 512);\r
+      continue;\r
+    }\r
+\r
+    //\r
+    // The PCI Data Structure must be DWORD aligned.\r
+    //\r
+    if (RomHeader->PcirOffset == 0 ||\r
+        (RomHeader->PcirOffset & 3) != 0 ||\r
+        (UINT8 *) RomHeader + RomHeader->PcirOffset + sizeof (PCI_DATA_STRUCTURE) > (UINT8 *) RomImage + RomSize) {\r
+      break;\r
+    }\r
+\r
+    RomPcir = (PCI_DATA_STRUCTURE *) ((UINT8 *) RomHeader + RomHeader->PcirOffset);\r
+    if (RomPcir->Signature != PCI_DATA_STRUCTURE_SIGNATURE) {\r
+      break;\r
+    }\r
+\r
+    if (RomPcir->CodeType == PCI_CODE_TYPE_EFI_IMAGE) {\r
+      return TRUE;\r
+    }\r
+\r
+    Indicator = RomPcir->Indicator;\r
+    RomHeader = (PCI_EXPANSION_ROM_HEADER *) ((UINT8 *) RomHeader + RomPcir->ImageLength * 512);\r
+  } while (((UINT8 *) RomHeader < (UINT8 *) RomImage + RomSize) && ((Indicator & 0x80) == 0x00));\r
+\r
+  return FALSE;\r
+}\r
+\r
 /**\r
   Load Option Rom image for specified PCI device.\r
 \r
@@ -340,7 +391,6 @@ LoadOpRomImage (
   UINT32                    LegacyImageLength;\r
   UINT8                     *RomInMemory;\r
   UINT8                     CodeType;\r
-  BOOLEAN                   HasEfiOpRom;\r
 \r
   RomSize       = PciDevice->RomSize;\r
 \r
@@ -392,7 +442,6 @@ LoadOpRomImage (
   RetStatus     = EFI_NOT_FOUND;\r
   FirstCheck    = TRUE;\r
   LegacyImageLength = 0;\r
-  HasEfiOpRom   = FALSE;\r
 \r
   do {\r
     PciDevice->PciRootBridgeIo->Mem.Read (\r
@@ -416,8 +465,8 @@ LoadOpRomImage (
     FirstCheck  = FALSE;\r
     OffsetPcir  = RomHeader->PcirOffset;\r
     //\r
-    // If the pointer to the PCI Data Structure is invalid, no further images can be located. \r
-    // The PCI Data Structure must be DWORD aligned. \r
+    // If the pointer to the PCI Data Structure is invalid, no further images can be located.\r
+    // The PCI Data Structure must be DWORD aligned.\r
     //\r
     if (OffsetPcir == 0 ||\r
         (OffsetPcir & 3) != 0 ||\r
@@ -443,8 +492,6 @@ LoadOpRomImage (
     if (RomPcir->CodeType == PCI_CODE_TYPE_PCAT_IMAGE) {\r
       CodeType = PCI_CODE_TYPE_PCAT_IMAGE;\r
       LegacyImageLength = ((UINT32)((EFI_LEGACY_EXPANSION_ROM_HEADER *)RomHeader)->Size512) * 512;\r
-    } else if (RomPcir->CodeType == PCI_CODE_TYPE_EFI_IMAGE) {\r
-      HasEfiOpRom = TRUE;\r
     }\r
     Indicator     = RomPcir->Indicator;\r
     RomImageSize  = RomImageSize + RomPcir->ImageLength * 512;\r
@@ -484,7 +531,6 @@ LoadOpRomImage (
 \r
   RomDecode (PciDevice, RomBarIndex, RomBar, FALSE);\r
 \r
-  PciDevice->HasEfiOpRom    = HasEfiOpRom;\r
   PciDevice->EmbeddedRom    = TRUE;\r
   PciDevice->PciIo.RomSize  = RomImageSize;\r
   PciDevice->PciIo.RomImage = RomInMemory;\r
@@ -499,7 +545,7 @@ LoadOpRomImage (
     PciDevice->BusNumber,\r
     PciDevice->DeviceNumber,\r
     PciDevice->FunctionNumber,\r
-    (UINT64) (UINTN) PciDevice->PciIo.RomImage,\r
+    PciDevice->PciIo.RomImage,\r
     PciDevice->PciIo.RomSize\r
     );\r
 \r
@@ -531,23 +577,10 @@ RomDecode (
   )\r
 {\r
   UINT32              Value32;\r
-  UINT32              Offset;\r
-  UINT32              OffsetMax;\r
   EFI_PCI_IO_PROTOCOL *PciIo;\r
 \r
   PciIo = &PciDevice->PciIo;\r
   if (Enable) {\r
-    //\r
-    // Clear all bars\r
-    //\r
-    OffsetMax = 0x24;\r
-    if (IS_PCI_BRIDGE(&PciDevice->Pci)) {\r
-      OffsetMax = 0x14;\r
-    }\r
-\r
-    for (Offset = 0x10; Offset <= OffsetMax; Offset += sizeof (UINT32)) {\r
-      PciIo->Pci.Write (PciIo, EfiPciIoWidthUint32, Offset, 1, &gAllZero);\r
-    }\r
 \r
     //\r
     // set the Rom base address: now is hardcode\r
@@ -565,7 +598,7 @@ RomDecode (
     //\r
     // Programe all upstream bridge\r
     //\r
-    ProgrameUpstreamBridgeForRom(PciDevice, RomBar, TRUE);\r
+    ProgramUpstreamBridgeForRom (PciDevice, RomBar, TRUE);\r
 \r
     //\r
     // Setting the memory space bit in the function's command register\r
@@ -582,7 +615,7 @@ RomDecode (
     //\r
     // Destroy the programmed bar in all the upstream bridge.\r
     //\r
-    ProgrameUpstreamBridgeForRom(PciDevice, RomBar, FALSE);\r
+    ProgramUpstreamBridgeForRom (PciDevice, RomBar, FALSE);\r
 \r
     //\r
     // disable rom decode\r
@@ -660,13 +693,6 @@ ProcessOpRomImage (
       goto NextImage;\r
     }\r
 \r
-    //\r
-    // Skip the EFI PCI Option ROM image if its machine type is not supported\r
-    //\r
-    if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (EfiRomHeader->EfiMachineType)) {\r
-      goto NextImage;\r
-    }\r
-\r
     //\r
     // Ignore the EFI PCI Option ROM image if it is an EFI application\r
     //\r
@@ -701,30 +727,37 @@ ProcessOpRomImage (
                     BufferSize,\r
                     &ImageHandle\r
                     );\r
-\r
-    FreePool (PciOptionRomImageDevicePath);\r
-\r
-    if (!EFI_ERROR (Status)) {\r
+    if (EFI_ERROR (Status)) {\r
+      //\r
+      // Record the Option ROM Image device path when LoadImage fails.\r
+      // PciOverride.GetDriver() will try to look for the Image Handle using the device path later.\r
+      //\r
+      AddDriver (PciDevice, NULL, PciOptionRomImageDevicePath);\r
+    } else {\r
       Status = gBS->StartImage (ImageHandle, NULL, NULL);\r
       if (!EFI_ERROR (Status)) {\r
-        AddDriver (PciDevice, ImageHandle);\r
+        //\r
+        // Record the Option ROM Image Handle\r
+        //\r
+        AddDriver (PciDevice, ImageHandle, NULL);\r
         PciRomAddImageMapping (\r
           ImageHandle,\r
           PciDevice->PciRootBridgeIo->SegmentNumber,\r
           PciDevice->BusNumber,\r
           PciDevice->DeviceNumber,\r
           PciDevice->FunctionNumber,\r
-          (UINT64) (UINTN) PciDevice->PciIo.RomImage,\r
+          PciDevice->PciIo.RomImage,\r
           PciDevice->PciIo.RomSize\r
           );\r
         RetStatus = EFI_SUCCESS;\r
       }\r
     }\r
+    FreePool (PciOptionRomImageDevicePath);\r
 \r
 NextImage:\r
     RomBarOffset += ImageSize;\r
 \r
-  } while (((Indicator & 0x80) == 0x00) && ((UINTN) (RomBarOffset - (UINT8 *) RomBar) < PciDevice->RomSize));\r
+  } while (((Indicator & 0x80) == 0x00) && (((UINTN) RomBarOffset - (UINTN) RomBar) < PciDevice->RomSize));\r
 \r
   return RetStatus;\r
 }\r