]> git.proxmox.com Git - mirror_edk2.git/blobdiff - IntelFrameworkModulePkg/Bus/Pci/PciBusDxe/PciOptionRomSupport.c
Code scrub for IdeBusDxe driver and PeiS3Lib.(undergoing)
[mirror_edk2.git] / IntelFrameworkModulePkg / Bus / Pci / PciBusDxe / PciOptionRomSupport.c
index 05339f3afb9359cf7f62fbb027ac644ac78d3c9e..f81bb92e3f295e7dfc95ee2d02dc4843a80c2e10 100644 (file)
@@ -1,6 +1,6 @@
-/**@file\r
+/** @file\r
 \r
-Copyright (c) 2006 - 2007, Intel Corporation\r
+Copyright (c) 2006 - 2009, Intel Corporation\r
 All rights reserved. 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
@@ -11,11 +11,206 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 **/\r
 \r
-#include "pcibus.h"\r
+#include "PciBus.h"\r
 #include "PciResourceSupport.h"\r
 \r
 #include <IndustryStandard/Pci23.h>\r
 \r
+/**\r
+  Load the EFI Image from Option ROM\r
+  \r
+  @param PciIoDevice   PCI IO Device\r
+  @param FilePath      The file path of the EFI Image\r
+  @param BufferSize    On input the size of Buffer in bytes. On output with a return \r
+                       code of EFI_SUCCESS, the amount of data transferred to Buffer. \r
+                       On output with a return code of EFI_BUFFER_TOO_SMALL, \r
+                       the size of Buffer required to retrieve the requested file. \r
+  @param Buffer        The memory buffer to transfer the file to. If Buffer is NULL, \r
+                       then no the size of the requested file is returned in BufferSize.\r
+\r
+  @retval EFI_SUCCESS           The file was loaded. \r
+  @retval EFI_UNSUPPORTED       BootPolicy is TRUE.\r
+  @retval EFI_BUFFER_TOO_SMALL  The BufferSize is too small to read the current directory entry.\r
+                                BufferSize has been updated with the size needed to complete the request.\r
+**/\r
+EFI_STATUS\r
+LocalLoadFile2 (\r
+  IN PCI_IO_DEVICE            *PciIoDevice,\r
+  IN EFI_DEVICE_PATH_PROTOCOL *FilePath,\r
+  IN OUT UINTN                *BufferSize,\r
+  IN VOID                     *Buffer      OPTIONAL\r
+  )\r
+{\r
+  EFI_STATUS                                Status;\r
+  MEDIA_RELATIVE_OFFSET_RANGE_DEVICE_PATH   *EfiOpRomImageNode;\r
+  EFI_PCI_EXPANSION_ROM_HEADER              *EfiRomHeader;\r
+  PCI_DATA_STRUCTURE                        *Pcir;\r
+  UINT32                                    ImageSize;\r
+  UINT8                                     *ImageBuffer;\r
+  UINT32                                    ImageLength;\r
+  UINT32                                    DestinationSize;\r
+  UINT32                                    ScratchSize;\r
+  VOID                                      *Scratch;\r
+  EFI_DECOMPRESS_PROTOCOL                   *Decompress;\r
+\r
+  EfiOpRomImageNode = (MEDIA_RELATIVE_OFFSET_RANGE_DEVICE_PATH *) FilePath;\r
+  if ((EfiOpRomImageNode == NULL) ||\r
+      (DevicePathType (FilePath) != MEDIA_DEVICE_PATH) ||\r
+      (DevicePathSubType (FilePath) != MEDIA_RELATIVE_OFFSET_RANGE_DP) ||\r
+      (DevicePathNodeLength (FilePath) != sizeof (MEDIA_RELATIVE_OFFSET_RANGE_DEVICE_PATH)) ||\r
+      (!IsDevicePathEnd (NextDevicePathNode (FilePath))) ||\r
+      (EfiOpRomImageNode->StartingOffset > EfiOpRomImageNode->EndingOffset) ||\r
+      (EfiOpRomImageNode->EndingOffset >= PciIoDevice->RomSize) ||\r
+      (BufferSize == NULL)\r
+      ) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  \r
+  EfiRomHeader = (EFI_PCI_EXPANSION_ROM_HEADER *) (\r
+      (UINT8 *) PciIoDevice->PciIo.RomImage + EfiOpRomImageNode->StartingOffset\r
+      );\r
+  if (EfiRomHeader->Signature != PCI_EXPANSION_ROM_HEADER_SIGNATURE) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  \r
+  Pcir = (PCI_DATA_STRUCTURE *) ((UINT8 *) EfiRomHeader + EfiRomHeader->PcirOffset);\r
+\r
+  \r
+  if ((Pcir->CodeType == PCI_CODE_TYPE_EFI_IMAGE) && \r
+      (EfiRomHeader->EfiSignature == EFI_PCI_EXPANSION_ROM_HEADER_EFISIGNATURE) &&\r
+      ((EfiRomHeader->EfiSubsystem == EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER) ||\r
+       (EfiRomHeader->EfiSubsystem == EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER)) &&\r
+      (EfiRomHeader->CompressionType <= EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED)\r
+       ) {\r
+\r
+    ImageSize               = (UINT32) EfiRomHeader->InitializationSize * 512;\r
+    ImageBuffer             = (UINT8 *) EfiRomHeader + EfiRomHeader->EfiImageHeaderOffset;\r
+    ImageLength             = ImageSize - EfiRomHeader->EfiImageHeaderOffset;\r
+\r
+    if (EfiRomHeader->CompressionType != EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED) {\r
+      //\r
+      // Uncompressed: Copy the EFI Image directly to user's buffer\r
+      //\r
+      if (Buffer == NULL || *BufferSize < ImageLength) {\r
+        *BufferSize = ImageLength;\r
+        return EFI_BUFFER_TOO_SMALL;\r
+      }\r
+\r
+      *BufferSize = ImageLength;\r
+      CopyMem (Buffer, ImageBuffer, ImageLength);\r
+      return EFI_SUCCESS;\r
+\r
+    } else {\r
+      //\r
+      // Compressed: Uncompress before copying\r
+      //\r
+      Status = gBS->LocateProtocol (&gEfiDecompressProtocolGuid, NULL, (VOID **) &Decompress);\r
+      if (EFI_ERROR (Status)) {\r
+        return EFI_DEVICE_ERROR;\r
+      }\r
+      Status = Decompress->GetInfo (\r
+                             Decompress,\r
+                             ImageBuffer,\r
+                             ImageLength,\r
+                             &DestinationSize,\r
+                             &ScratchSize\r
+                             );\r
+      if (EFI_ERROR (Status)) {\r
+        return EFI_DEVICE_ERROR;\r
+      } \r
+      \r
+      if (Buffer == NULL || *BufferSize < DestinationSize) {\r
+        *BufferSize = DestinationSize;\r
+        return EFI_BUFFER_TOO_SMALL;\r
+      }      \r
+\r
+      *BufferSize = DestinationSize;\r
+      Scratch = AllocatePool (ScratchSize);\r
+      if (Scratch == NULL) {\r
+        return EFI_DEVICE_ERROR;\r
+      }\r
+      \r
+      Status = Decompress->Decompress (\r
+                             Decompress,\r
+                             ImageBuffer,\r
+                             ImageLength,\r
+                             Buffer,\r
+                             DestinationSize,\r
+                             Scratch,\r
+                             ScratchSize\r
+                             );\r
+      gBS->FreePool (Scratch);\r
+      \r
+      if (EFI_ERROR (Status)) {\r
+        return EFI_DEVICE_ERROR;\r
+      }\r
+      return EFI_SUCCESS;\r
+    }\r
+  }\r
+\r
+  return EFI_NOT_FOUND;\r
+}\r
+\r
+/**\r
+  Initialize a PCI LoadFile2 instance.\r
+  \r
+  @param PciIoDevice   PCI IO Device.\r
+\r
+**/\r
+VOID\r
+InitializePciLoadFile2 (\r
+  PCI_IO_DEVICE       *PciIoDevice\r
+  )\r
+{\r
+  PciIoDevice->LoadFile2.LoadFile = LoadFile2;\r
+}\r
+\r
+/**\r
+  Causes the driver to load a specified file.\r
+  \r
+  @param This        Indicates a pointer to the calling context.\r
+  @param FilePath    The device specific path of the file to load.\r
+  @param BootPolicy  Should always be FALSE.\r
+  @param BufferSize  On input the size of Buffer in bytes. On output with a return \r
+                     code of EFI_SUCCESS, the amount of data transferred to Buffer. \r
+                     On output with a return code of EFI_BUFFER_TOO_SMALL, \r
+                     the size of Buffer required to retrieve the requested file. \r
+  @param Buffer      The memory buffer to transfer the file to. If Buffer is NULL, \r
+                    then no the size of the requested file is returned in BufferSize.\r
+\r
+  @retval EFI_SUCCESS           The file was loaded. \r
+  @retval EFI_UNSUPPORTED       BootPolicy is TRUE.\r
+  @retval EFI_BUFFER_TOO_SMALL  The BufferSize is too small to read the current directory entry.\r
+                                BufferSize has been updated with the size needed to complete the request.\r
+  \r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+LoadFile2 (\r
+  IN EFI_LOAD_FILE2_PROTOCOL  *This,\r
+  IN EFI_DEVICE_PATH_PROTOCOL *FilePath,\r
+  IN BOOLEAN                  BootPolicy,\r
+  IN OUT UINTN                *BufferSize,\r
+  IN VOID                     *Buffer      OPTIONAL\r
+  )\r
+{\r
+  PCI_IO_DEVICE                             *PciIoDevice;\r
+\r
+  if (BootPolicy) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+  PciIoDevice = PCI_IO_DEVICE_FROM_LOAD_FILE2_THIS (This);\r
+\r
+  return LocalLoadFile2 (\r
+           PciIoDevice,\r
+           FilePath,\r
+           BufferSize,\r
+           Buffer\r
+           );\r
+}\r
+\r
+\r
 //\r
 // Module global for a template of the PCI option ROM Image Device Path Node\r
 //\r
@@ -23,29 +218,28 @@ MEMMAP_DEVICE_PATH  mPciOptionRomImageDevicePathNodeTemplate = {
   {\r
     HARDWARE_DEVICE_PATH,\r
     HW_MEMMAP_DP,\r
-    sizeof (MEMMAP_DEVICE_PATH)\r
+    {\r
+      (UINT8) (sizeof (MEMMAP_DEVICE_PATH)),\r
+      (UINT8) ((sizeof (MEMMAP_DEVICE_PATH)) >> 8)\r
+    }\r
   },\r
   EfiMemoryMappedIO,\r
   0,\r
   0\r
 };\r
 \r
+/**\r
+  Get Pci device's oprom infor bits.\r
+  \r
+  @param PciIoDevice Pci device instance\r
+\r
+  @retval EFI_NOT_FOUND Pci device has not oprom\r
+  @retval EFI_SUCCESS   Pci device has oprom\r
+**/\r
 EFI_STATUS\r
 GetOpRomInfo (\r
   IN PCI_IO_DEVICE    *PciIoDevice\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-Arguments:\r
-\r
-Returns:\r
-\r
---*/\r
-// TODO:    PciIoDevice - add argument and description to function comment\r
-// TODO:    EFI_NOT_FOUND - add return value to function comment\r
-// TODO:    EFI_SUCCESS - add return value to function comment\r
 {\r
   UINT8                           RomBarIndex;\r
   UINT32                          AllOnes;\r
@@ -69,7 +263,7 @@ Returns:
   //\r
   // 0x30\r
   //\r
-  RomBarIndex = PCI_DEVICE_ROMBAR;\r
+  RomBarIndex = PCI_EXPANSION_ROM_BASE;\r
 \r
   if (IS_PCI_BRIDGE (&PciIoDevice->Pci)) {\r
     //\r
@@ -125,34 +319,75 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
-EFI_STATUS\r
-LoadOpRomImage (\r
-  IN PCI_IO_DEVICE   *PciDevice,\r
-  IN UINT64          RomBase\r
-  )\r
-/*++\r
+/**\r
 \r
-Routine Description:\r
+  Check if the RomImage contains EFI Images.\r
 \r
-    Load option rom image for specified PCI device\r
+  @param  RomImage  The ROM address of Image for check. \r
+  @param  RomSize   Size of ROM for check.\r
 \r
-Arguments:\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
+  BOOLEAN                   FirstCheck;\r
 \r
-Returns:\r
+  FirstCheck = TRUE;\r
+  RomHeader  = RomImage;\r
+  \r
+  while ((UINT8 *) RomHeader < (UINT8 *) RomImage + RomSize) {\r
+    if (RomHeader->Signature != PCI_EXPANSION_ROM_HEADER_SIGNATURE) {\r
+      if (FirstCheck) {\r
+        return FALSE;\r
+      } else {\r
+        RomHeader = (PCI_EXPANSION_ROM_HEADER *) ((UINT8 *) RomHeader + 512);\r
+        continue;\r
+      }\r
+    }\r
+\r
+    FirstCheck = FALSE;\r
+    RomPcir    = (PCI_DATA_STRUCTURE *) ((UINT8 *) RomHeader + RomHeader->PcirOffset);\r
+    \r
+    if (RomPcir->CodeType == PCI_CODE_TYPE_EFI_IMAGE) {\r
+      return TRUE;\r
+    }\r
 \r
---*/\r
-// TODO:    PciDevice - add argument and description to function comment\r
-// TODO:    RomBase - add argument and description to function comment\r
-// TODO:    EFI_OUT_OF_RESOURCES - add return value to function comment\r
-// TODO:    EFI_OUT_OF_RESOURCES - add return value to function comment\r
-// TODO:    EFI_OUT_OF_RESOURCES - add return value to function comment\r
+    RomHeader = (PCI_EXPANSION_ROM_HEADER *) ((UINT8 *) RomHeader + RomPcir->Length * 512);\r
+  }\r
+\r
+  return FALSE;\r
+}\r
+\r
+\r
+/**\r
+  Load option rom image for specified PCI device\r
+  \r
+  @param PciDevice Pci device instance\r
+  @param RomBase   Base address of oprom.\r
+  \r
+  @retval EFI_OUT_OF_RESOURCES not enough memory to hold image\r
+  @retval EFI_SUCESS           Success\r
+**/\r
+EFI_STATUS\r
+LoadOpRomImage (\r
+  IN PCI_IO_DEVICE   *PciDevice,\r
+  IN UINT64          RomBase\r
+  )\r
 {\r
   UINT8                     RomBarIndex;\r
   UINT8                     Indicator;\r
   UINT16                    OffsetPcir;\r
   UINT32                    RomBarOffset;\r
   UINT32                    RomBar;\r
-  EFI_STATUS                retStatus;\r
+  EFI_STATUS                RetStatus;\r
   BOOLEAN                   FirstCheck;\r
   UINT8                     *Image;\r
   PCI_EXPANSION_ROM_HEADER  *RomHeader;\r
@@ -176,7 +411,7 @@ Returns:
   //\r
   // 0x30\r
   //\r
-  RomBarIndex = PCI_DEVICE_ROMBAR;\r
+  RomBarIndex = PCI_EXPANSION_ROM_BASE;\r
   if (IS_PCI_BRIDGE (&(PciDevice->Pci))) {\r
     //\r
     // if is ppb\r
@@ -209,7 +444,7 @@ Returns:
   RomDecode (PciDevice, RomBarIndex, RomBar, TRUE);\r
 \r
   RomBarOffset  = RomBar;\r
-  retStatus     = EFI_NOT_FOUND;\r
+  RetStatus     = EFI_NOT_FOUND;\r
   FirstCheck    = TRUE;\r
 \r
   do {\r
@@ -257,7 +492,7 @@ Returns:
   }\r
 \r
   if (RomImageSize > 0) {\r
-    retStatus = EFI_SUCCESS;\r
+    RetStatus = EFI_SUCCESS;\r
     Image     = AllocatePool ((UINT32) RomImageSize);\r
     if (Image == NULL) {\r
       RomDecode (PciDevice, RomBarIndex, RomBar, FALSE);\r
@@ -304,9 +539,20 @@ Returns:
   gBS->FreePool (RomHeader);\r
   gBS->FreePool (RomPcir);\r
 \r
-  return retStatus;\r
+  return RetStatus;\r
 }\r
 \r
+/**\r
+  enable/disable oprom decode\r
+  \r
+  @param PciDevice    pci device instance\r
+  @param RomBarIndex  The BAR index of the standard PCI Configuration header to use as the\r
+                      base address for resource range. The legal range for this field is 0..5.\r
+  @param RomBar       Base address of rom\r
+  @param Enable       Flag for enable/disable decode.\r
+  \r
+  @retval EFI_SUCCESS Success\r
+**/\r
 EFI_STATUS\r
 RomDecode (\r
   IN PCI_IO_DEVICE   *PciDevice,\r
@@ -314,20 +560,6 @@ RomDecode (
   IN UINT32          RomBar,\r
   IN BOOLEAN         Enable\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-Arguments:\r
-\r
-Returns:\r
-\r
---*/\r
-// TODO:    PciDevice - add argument and description to function comment\r
-// TODO:    RomBarIndex - add argument and description to function comment\r
-// TODO:    RomBar - add argument and description to function comment\r
-// TODO:    Enable - add argument and description to function comment\r
-// TODO:    EFI_SUCCESS - add return value to function comment\r
 {\r
   UINT32              Value32;\r
   UINT32              Offset;\r
@@ -363,14 +595,14 @@ Returns:
     //\r
     // Setting the memory space bit in the function's command register\r
     //\r
-    PciEnableCommandRegister(PciDevice, EFI_PCI_COMMAND_MEMORY_SPACE);\r
+    PCI_ENABLE_COMMAND_REGISTER(PciDevice, EFI_PCI_COMMAND_MEMORY_SPACE);\r
 \r
   } else {\r
 \r
     //\r
     // disable command register decode to memory\r
     //\r
-    PciDisableCommandRegister(PciDevice, EFI_PCI_COMMAND_MEMORY_SPACE);\r
+    PCI_DISABLE_COMMAND_REGISTER(PciDevice, EFI_PCI_COMMAND_MEMORY_SPACE);\r
 \r
     //\r
     // Destroy the programmed bar in all the upstream bridge.\r
@@ -395,46 +627,32 @@ Returns:
 \r
 }\r
 \r
+/**\r
+  Process the oprom image.\r
+  \r
+  @param PciDevice Pci device instance\r
+**/\r
 EFI_STATUS\r
 ProcessOpRomImage (\r
   PCI_IO_DEVICE   *PciDevice\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Process the oprom image.\r
-\r
-Arguments:\r
-  PciDevice       A pointer to a pci device.\r
-\r
-Returns:\r
-\r
-  EFI Status.\r
-\r
---*/\r
 {\r
   UINT8                         Indicator;\r
   UINT32                        ImageSize;\r
-  UINT16                        ImageOffset;\r
   VOID                          *RomBar;\r
   UINT8                         *RomBarOffset;\r
   EFI_HANDLE                    ImageHandle;\r
   EFI_STATUS                    Status;\r
-  EFI_STATUS                    retStatus;\r
+  EFI_STATUS                    RetStatus;\r
   BOOLEAN                       FirstCheck;\r
-  BOOLEAN                       SkipImage;\r
-  UINT32                        DestinationSize;\r
-  UINT32                        ScratchSize;\r
-  UINT8                         *Scratch;\r
-  VOID                          *ImageBuffer;\r
-  VOID                          *DecompressedImageBuffer;\r
-  UINT32                        ImageLength;\r
-  EFI_DECOMPRESS_PROTOCOL       *Decompress;\r
   EFI_PCI_EXPANSION_ROM_HEADER  *EfiRomHeader;\r
   PCI_DATA_STRUCTURE            *Pcir;\r
   EFI_DEVICE_PATH_PROTOCOL      *PciOptionRomImageDevicePath;\r
 \r
+  MEDIA_RELATIVE_OFFSET_RANGE_DEVICE_PATH  EfiOpRomImageNode;                            \r
+  VOID                          *Buffer;\r
+  UINTN                         BufferSize;\r
+\r
   Indicator = 0;\r
 \r
   //\r
@@ -442,13 +660,13 @@ Returns:
   //\r
   RomBar        = PciDevice->PciIo.RomImage;\r
   RomBarOffset  = (UINT8 *) RomBar;\r
-  retStatus     = EFI_NOT_FOUND;\r
+  RetStatus     = EFI_NOT_FOUND;\r
   FirstCheck    = TRUE;\r
 \r
   do {\r
     EfiRomHeader = (EFI_PCI_EXPANSION_ROM_HEADER *) RomBarOffset;\r
     if (EfiRomHeader->Signature != PCI_EXPANSION_ROM_HEADER_SIGNATURE) {\r
-      RomBarOffset = RomBarOffset + 512;\r
+      RomBarOffset += 512;\r
       if (FirstCheck) {\r
         break;\r
       } else {\r
@@ -461,122 +679,76 @@ Returns:
     ImageSize   = (UINT32) (Pcir->ImageLength * 512);\r
     Indicator   = Pcir->Indicator;\r
 \r
-    if ((Pcir->CodeType == PCI_CODE_TYPE_EFI_IMAGE) &&\r
-        (EfiRomHeader->EfiSignature == EFI_PCI_EXPANSION_ROM_HEADER_EFISIGNATURE)) {\r
-\r
-      if ((EfiRomHeader->EfiSubsystem == EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER)  ||\r
-          (EfiRomHeader->EfiSubsystem == EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER)) {\r
-\r
-        ImageOffset             = EfiRomHeader->EfiImageHeaderOffset;\r
-        ImageSize               = (UINT32) (EfiRomHeader->InitializationSize * 512);\r
-\r
-        ImageBuffer             = (VOID *) (RomBarOffset + ImageOffset);\r
-        ImageLength             = ImageSize - (UINT32)ImageOffset;\r
-        DecompressedImageBuffer = NULL;\r
-\r
-        //\r
-        // decompress here if needed\r
-        //\r
-        SkipImage = FALSE;\r
-        if (EfiRomHeader->CompressionType > EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED) {\r
-          SkipImage = TRUE;\r
-        }\r
-\r
-        if (EfiRomHeader->CompressionType == EFI_PCI_EXPANSION_ROM_HEADER_COMPRESSED) {\r
-          Status = gBS->LocateProtocol (&gEfiDecompressProtocolGuid, NULL, (VOID **) &Decompress);\r
-          if (EFI_ERROR (Status)) {\r
-            SkipImage = TRUE;\r
-          } else {\r
-            SkipImage = TRUE;\r
-            Status = Decompress->GetInfo (\r
-                                  Decompress,\r
-                                  ImageBuffer,\r
-                                  ImageLength,\r
-                                  &DestinationSize,\r
-                                  &ScratchSize\r
-                                  );\r
-            if (!EFI_ERROR (Status)) {\r
-              DecompressedImageBuffer = NULL;\r
-              DecompressedImageBuffer = AllocatePool (DestinationSize);\r
-              if (DecompressedImageBuffer != NULL) {\r
-                Scratch = AllocatePool (ScratchSize);\r
-                if (Scratch != NULL) {\r
-                  Status = Decompress->Decompress (\r
-                                        Decompress,\r
-                                        ImageBuffer,\r
-                                        ImageLength,\r
-                                        DecompressedImageBuffer,\r
-                                        DestinationSize,\r
-                                        Scratch,\r
-                                        ScratchSize\r
-                                        );\r
-                  if (!EFI_ERROR (Status)) {\r
-                    ImageBuffer = DecompressedImageBuffer;\r
-                    ImageLength = DestinationSize;\r
-                    SkipImage   = FALSE;\r
-                  }\r
-\r
-                  gBS->FreePool (Scratch);\r
-                }\r
-              }\r
-            }\r
-          }\r
-        }\r
-\r
-        if (!SkipImage) {\r
-          //\r
-          // Build Memory Mapped device path node to record the image offset into the PCI Option ROM\r
-          //\r
-          mPciOptionRomImageDevicePathNodeTemplate.StartingAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) (RomBarOffset - (UINT8 *) RomBar);\r
-          mPciOptionRomImageDevicePathNodeTemplate.EndingAddress   = (EFI_PHYSICAL_ADDRESS) (UINTN) (RomBarOffset + ImageSize - 1 - (UINT8 *) RomBar);\r
-          PciOptionRomImageDevicePath = AppendDevicePathNode (PciDevice->DevicePath, (const EFI_DEVICE_PATH_PROTOCOL *)&mPciOptionRomImageDevicePathNodeTemplate);\r
-          ASSERT (PciOptionRomImageDevicePath != NULL);\r
-\r
-          //\r
-          // load image and start image\r
-          //\r
-          Status = gBS->LoadImage (\r
-                          FALSE,\r
-                          gPciBusDriverBinding.DriverBindingHandle,\r
-                          PciOptionRomImageDevicePath,\r
-                          ImageBuffer,\r
-                          ImageLength,\r
-                          &ImageHandle\r
-                          );\r
-\r
-          //\r
-          // Free the device path after it has been used by LoadImage\r
-          //\r
-          gBS->FreePool (PciOptionRomImageDevicePath);\r
-\r
-          if (!EFI_ERROR (Status)) {\r
-            Status = gBS->StartImage (ImageHandle, NULL, NULL);\r
-            if (!EFI_ERROR (Status)) {\r
-              AddDriver (PciDevice, ImageHandle);\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.RomSize\r
-                );\r
-              retStatus = EFI_SUCCESS;\r
-            }\r
-          }\r
-        }\r
+    //\r
+    // Create Pci Option Rom Image device path header\r
+    //\r
+    EfiOpRomImageNode.Header.Type     = MEDIA_DEVICE_PATH;\r
+    EfiOpRomImageNode.Header.SubType  = MEDIA_RELATIVE_OFFSET_RANGE_DP;\r
+    SetDevicePathNodeLength (&EfiOpRomImageNode.Header, sizeof (EfiOpRomImageNode));\r
+    EfiOpRomImageNode.StartingOffset  = (UINTN) RomBarOffset - (UINTN) RomBar;\r
+    EfiOpRomImageNode.EndingOffset    = (UINTN) RomBarOffset + ImageSize - 1 - (UINTN) RomBar;\r
 \r
-        RomBarOffset = RomBarOffset + ImageSize;\r
-      } else {\r
-        RomBarOffset = RomBarOffset + ImageSize;\r
+    PciOptionRomImageDevicePath = AppendDevicePathNode (PciDevice->DevicePath, &EfiOpRomImageNode.Header);\r
+    ASSERT (PciOptionRomImageDevicePath != NULL);\r
+\r
+    //\r
+    // load image and start image\r
+    //\r
+\r
+    BufferSize  = 0;\r
+    Buffer      = NULL;\r
+    Status      = EFI_SUCCESS;\r
+    ImageHandle = NULL;\r
+\r
+    if (!EFI_ERROR (Status)) {\r
+      Status = gBS->LoadImage (\r
+                      FALSE,\r
+                      gPciBusDriverBinding.DriverBindingHandle,\r
+                      PciOptionRomImageDevicePath,\r
+                      Buffer,\r
+                      BufferSize,\r
+                      &ImageHandle\r
+                      );\r
+    }\r
+\r
+    //\r
+    // load image and start image\r
+    //\r
+     if (!EFI_ERROR (Status)) {\r
+      Status = gBS->LoadImage (\r
+                      FALSE,\r
+                      gPciBusDriverBinding.DriverBindingHandle,\r
+                      PciOptionRomImageDevicePath,\r
+                      Buffer,\r
+                      BufferSize,\r
+                      &ImageHandle\r
+                      );\r
+    }\r
+\r
+    gBS->FreePool (PciOptionRomImageDevicePath);\r
+\r
+    if (!EFI_ERROR (Status)) {\r
+      Status = gBS->StartImage (ImageHandle, NULL, NULL);\r
+      if (!EFI_ERROR (Status)) {\r
+        AddDriver (PciDevice, ImageHandle);\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.RomSize\r
+          );\r
+        RetStatus = EFI_SUCCESS;\r
       }\r
-    } else {\r
-      RomBarOffset = RomBarOffset + ImageSize;\r
     }\r
 \r
+    RomBarOffset += ImageSize;\r
+\r
   } while (((Indicator & 0x80) == 0x00) && ((UINTN) (RomBarOffset - (UINT8 *) RomBar) < PciDevice->RomSize));\r
 \r
-  return retStatus;\r
+  return RetStatus;\r
 \r
 }\r
+\r