]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/DxeServicesLib/DxeServicesLib.c
MdePkg DxeServicesLib: Handle potential NULL FvHandle
[mirror_edk2.git] / MdePkg / Library / DxeServicesLib / DxeServicesLib.c
index 5dca51a4638e5901758d09d2258d4bdd4f2506dc..1827c9216fbc231c98b756b0bc66e7c7b13c7b58 100644 (file)
@@ -1,11 +1,13 @@
 /** @file\r
-  Mde PI library functions.\r
+  MDE DXE Services Library provides functions that simplify the development of DXE Drivers.  \r
+  These functions help access data from sections of FFS files or from file path.\r
 \r
-  Copyright (c) 2007 - 2008, Intel Corporation<BR>\r
-  All rights reserved. This program and the accompanying materials\r
+  Copyright (c) 2007 - 2017, Intel Corporation. All rights reserved.<BR>\r
+  (C) Copyright 2015 Hewlett Packard Enterprise Development LP<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
+  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
 #include <Library/DebugLib.h>\r
 #include <Library/MemoryAllocationLib.h>\r
 #include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/DevicePathLib.h>\r
+#include <Library/UefiLib.h>\r
 #include <Library/DxeServicesLib.h>\r
 #include <Protocol/FirmwareVolume2.h>\r
 #include <Protocol/LoadedImage.h>\r
-\r
+#include <Protocol/LoadFile2.h>\r
+#include <Protocol/LoadFile.h>\r
+#include <Protocol/SimpleFileSystem.h>\r
+#include <Guid/FileInfo.h>\r
 \r
 /**\r
   Identify the device handle from which the Image is loaded from. As this device handle is passed to\r
@@ -34,7 +41,7 @@
   \r
   @param  ImageHandle         The firmware allocated handle for UEFI image.\r
 \r
-  @retval  EFI_HANDLE          The device handle from which the Image is loaded from.\r
+  @retval  EFI_HANDLE         The device handle from which the Image is loaded from.\r
 \r
 **/\r
 EFI_HANDLE\r
@@ -55,46 +62,57 @@ InternalImageHandleToFvHandle (
 \r
   ASSERT_EFI_ERROR (Status);\r
 \r
+  //\r
+  // The LoadedImage->DeviceHandle may be NULL.\r
+  // For example for DxeCore, there is LoadedImage protocol installed for it, but the\r
+  // LoadedImage->DeviceHandle could not be initialized before the FV2 (contain DxeCore)\r
+  // protocol is installed.\r
+  //\r
   return LoadedImage->DeviceHandle;\r
 \r
 }\r
 \r
 /**\r
-    Allocate and fill a buffer from a Firmware Section identified by a Firmware File GUID name, a Firmware \r
-    Section type and instance number from the specified Firmware Volume.\r
+  Allocate and fill a buffer from a Firmware Section identified by a Firmware File GUID name, a Firmware \r
+  Section type and instance number from the specified Firmware Volume.\r
+\r
+  This functions first locate the EFI_FIRMWARE_VOLUME2_PROTOCOL protocol instance on FvHandle in order to \r
+  carry out the Firmware Volume read operation. The function then reads the Firmware Section found sepcifed \r
+  by NameGuid, SectionType and SectionInstance. \r
   \r
-    This functions first locate the EFI_FIRMWARE_VOLUME2_PROTOCOL protocol instance on FvHandle in order to \r
-    carry out the Firmware Volume read operation. The function then reads the Firmware Section found sepcifed \r
-    by NameGuid, SectionType and SectionInstance. \r
-    \r
-    The details of this search order is defined in description of EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection () \r
-    found in PI Specification.\r
-    \r
-    If SectionType is EFI_SECTION_TE, EFI_SECTION_TE is used as section type to start the search. If EFI_SECTION_TE section \r
-    is not found, EFI_SECTION_PE32 will be used to try the search again. If no EFI_SECTION_PE32 section is found, EFI_NOT_FOUND \r
-    is returned.\r
-    \r
-    The data and size is returned by Buffer and Size. The caller is responsible to free the Buffer allocated \r
-    by this function. This function can be only called at TPL_NOTIFY and below.\r
-    \r
-    If FvHandle is NULL, then ASSERT ();\r
-    If NameGuid is NULL, then ASSERT();\r
-    If Buffer is NULL, then ASSERT();\r
-    If Size is NULL, then ASSERT().\r
-\r
-    @param  FvHandle                The device handle that contains a instance of EFI_FIRMWARE_VOLUME2_PROTOCOL instance.\r
-    @param  NameGuid                The GUID name of a Firmware File.\r
-    @param  SectionType             The Firmware Section type.\r
-    @param  SectionInstance         The instance number of Firmware Section to read from starting from 0.\r
-    @param  Buffer                  On output, Buffer contains the the data read from the section in the Firmware File found.\r
-    @param  Size                    On output, the size of Buffer.\r
+  The details of this search order is defined in description of EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection () \r
+  found in PI Specification.\r
   \r
-    @retval  EFI_SUCCESS            The image is found and data and size is returned.\r
-    @retval  EFI_UNSUPPORTED        FvHandle does not support EFI_FIRMWARE_VOLUME2_PROTOCOL.\r
-    @retval  EFI_NOT_FOUND          The image specified by NameGuid and SectionType can't be found.\r
-    @retval  EFI_OUT_OF_RESOURCES   There were not enough resources to allocate the output data buffer or complete the operations.\r
-    @retval  EFI_DEVICE_ERROR       A hardware error occurs during reading from the Firmware Volume.\r
-    @retval  EFI_ACCESS_DENIED      The firmware volume containing the searched Firmware File is configured to disallow reads.\r
+  If SectionType is EFI_SECTION_TE, EFI_SECTION_TE is used as section type to start the search. If EFI_SECTION_TE section \r
+  is not found, EFI_SECTION_PE32 will be used to try the search again. If no EFI_SECTION_PE32 section is found, EFI_NOT_FOUND \r
+  is returned.\r
+  \r
+  The data and size is returned by Buffer and Size. The caller is responsible to free the Buffer allocated \r
+  by this function. This function can be only called at TPL_NOTIFY and below.\r
+  \r
+  If NameGuid is NULL, then ASSERT();\r
+  If Buffer is NULL, then ASSERT();\r
+  If Size is NULL, then ASSERT().\r
+\r
+  @param  FvHandle                The device handle that contains a instance of \r
+                                  EFI_FIRMWARE_VOLUME2_PROTOCOL instance.\r
+  @param  NameGuid                The GUID name of a Firmware File.\r
+  @param  SectionType             The Firmware Section type.\r
+  @param  SectionInstance         The instance number of Firmware Section to  \r
+                                  read from starting from 0.\r
+  @param  Buffer                  On output, Buffer contains the the data read \r
+                                  from the section in the Firmware File found.\r
+  @param  Size                    On output, the size of Buffer.\r
+\r
+  @retval  EFI_SUCCESS            The image is found and data and size is returned.\r
+  @retval  EFI_NOT_FOUND          The image specified by NameGuid and SectionType \r
+                                  can't be found.\r
+  @retval  EFI_OUT_OF_RESOURCES   There were not enough resources to allocate the \r
+                                  output data buffer or complete the operations.\r
+  @retval  EFI_DEVICE_ERROR       A hardware error occurs during reading from the \r
+                                  Firmware Volume.\r
+  @retval  EFI_ACCESS_DENIED      The firmware volume containing the searched \r
+                                  Firmware File is configured to disallow reads.\r
   \r
 **/\r
 EFI_STATUS\r
@@ -115,7 +133,12 @@ InternalGetSectionFromFv (
   ASSERT (Buffer != NULL);\r
   ASSERT (Size != NULL);\r
   \r
-  ASSERT (FvHandle != NULL);\r
+  if (FvHandle == NULL) {\r
+    //\r
+    // Return EFI_NOT_FOUND directly for NULL FvHandle.\r
+    //\r
+    return EFI_NOT_FOUND;\r
+  }\r
 \r
   Status = gBS->HandleProtocol (\r
                   FvHandle,\r
@@ -123,7 +146,7 @@ InternalGetSectionFromFv (
                   (VOID **) &Fv\r
                   );\r
   if (EFI_ERROR (Status)) {\r
-    return Status;\r
+    return EFI_NOT_FOUND;\r
   }\r
 \r
   //\r
@@ -161,51 +184,193 @@ InternalGetSectionFromFv (
   return Status;\r
 }\r
 \r
+/**\r
+  Searches all the available firmware volumes and returns the first matching FFS section. \r
 \r
+  This function searches all the firmware volumes for FFS files with FV file type specified by FileType\r
+  The order that the firmware volumes is searched is not deterministic. For each available FV a search \r
+  is made for FFS file of type FileType. If the FV contains more than one FFS file with the same FileType, \r
+  the FileInstance instance will be the matched FFS file. For each FFS file found a search \r
+  is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance instances \r
+  of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer. \r
+  Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size. \r
+  It is the caller's responsibility to use FreePool() to free the allocated buffer.  \r
+  See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections \r
+  are retrieved from an FFS file based on SectionType and SectionInstance.\r
 \r
-/**\r
-  Locates a requested firmware section within a file and returns it to a buffer allocated by this function. \r
+  If SectionType is EFI_SECTION_TE, and the search with an FFS file fails, \r
+  the search will be retried with a section type of EFI_SECTION_PE32.\r
+  This function must be called with a TPL <= TPL_NOTIFY.\r
 \r
-  GetSectionFromAnyFv  () is used to read a specific section from a file within a firmware volume. The function\r
-  will search the first file with the specified name in all firmware volumes in the system. The search order for firmware \r
-  volumes in the system is determistic but abitrary if no new firmware volume is added into the system between \r
-  each calls of this function. \r
+  If Buffer is NULL, then ASSERT().\r
+  If Size is NULL, then ASSERT().\r
 \r
-  After the specific file is located, the function searches the specifc firmware section with type SectionType in this file. \r
-  The details of this search order is defined in description of EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection () \r
-  found in PI Specification.\r
+  @param  FileType             Indicates the FV file type to search for within all \r
+                               available FVs.\r
+  @param  FileInstance         Indicates which file instance within all available \r
+                               FVs specified by FileType.\r
+                               FileInstance starts from zero.\r
+  @param  SectionType          Indicates the FFS section type to search for \r
+                               within the FFS file \r
+                               specified by FileType with FileInstance.\r
+  @param  SectionInstance      Indicates which section instance within the FFS file \r
+                               specified by FileType with FileInstance to retrieve. \r
+                               SectionInstance starts from zero.\r
+  @param  Buffer               On output, a pointer to a callee allocated buffer \r
+                               containing the FFS file section that was found.\r
+                               Is it the caller's responsibility to free this \r
+                               buffer using FreePool().\r
+  @param  Size                 On output, a pointer to the size, in bytes, of Buffer.\r
 \r
-  If SectionType is EFI_SECTION_TE, EFI_SECTION_TE is used as section type to start the search. If EFI_SECTION_TE section \r
-  is not found, EFI_SECTION_PE32 will be used to try the search again. If no EFI_SECTION_PE32 section is found, EFI_NOT_FOUND \r
-  is returned.\r
+  @retval  EFI_SUCCESS          The specified FFS section was returned.\r
+  @retval  EFI_NOT_FOUND        The specified FFS section could not be found.\r
+  @retval  EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve \r
+                                the matching FFS section.\r
+  @retval  EFI_DEVICE_ERROR     The FFS section could not be retrieves due to a \r
+                                device error.\r
+  @retval  EFI_ACCESS_DENIED    The FFS section could not be retrieves because \r
+                                the firmware volume that \r
+                                contains the matching FFS section does not allow reads.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+GetSectionFromAnyFvByFileType  (\r
+  IN  EFI_FV_FILETYPE               FileType,\r
+  IN  UINTN                         FileInstance,\r
+  IN  EFI_SECTION_TYPE              SectionType,\r
+  IN  UINTN                         SectionInstance,\r
+  OUT VOID                          **Buffer,\r
+  OUT UINTN                         *Size\r
+  )\r
+{\r
+  EFI_STATUS                    Status;\r
+  EFI_HANDLE                    *HandleBuffer;\r
+  UINTN                         HandleCount;\r
+  UINTN                         IndexFv;\r
+  UINTN                         IndexFile;\r
+  UINTN                         Key;\r
+  EFI_GUID                      NameGuid;\r
+  EFI_FV_FILE_ATTRIBUTES        Attributes;\r
+  EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
 \r
-  The data and size is returned by Buffer and Size. The caller is responsible to free the Buffer allocated \r
-  by this function. This function can only be called at TPL_NOTIFY and below.\r
+  //\r
+  // Locate all available FVs.\r
+  //\r
+  HandleBuffer = NULL;\r
+  Status = gBS->LocateHandleBuffer (\r
+                  ByProtocol,\r
+                  &gEfiFirmwareVolume2ProtocolGuid,\r
+                  NULL,\r
+                  &HandleCount,\r
+                  &HandleBuffer\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
 \r
-  If NameGuid is NULL, then ASSERT();\r
-  If Buffer is NULL, then ASSERT();\r
+  //\r
+  // Go through FVs one by one to find the required section data.\r
+  //\r
+  for (IndexFv = 0; IndexFv < HandleCount; IndexFv++) {\r
+    Status = gBS->HandleProtocol (\r
+                    HandleBuffer[IndexFv],\r
+                    &gEfiFirmwareVolume2ProtocolGuid,\r
+                    (VOID **)&Fv\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      continue;\r
+    }\r
+\r
+    //\r
+    // Use Firmware Volume 2 Protocol to search for a file of type FileType in all FVs.\r
+    //\r
+    IndexFile = FileInstance + 1;\r
+    Key = 0;\r
+    do {\r
+      Status = Fv->GetNextFile (Fv, &Key, &FileType, &NameGuid, &Attributes, Size);\r
+      if (EFI_ERROR (Status)) {\r
+        break;\r
+      }\r
+      IndexFile --;\r
+    } while (IndexFile > 0);\r
+\r
+    //\r
+    // Fv File with the required FV file type is found.\r
+    // Search the section file in the found FV file.\r
+    //\r
+    if (IndexFile == 0) {\r
+      Status = InternalGetSectionFromFv (\r
+                 HandleBuffer[IndexFv], \r
+                 &NameGuid,\r
+                 SectionType,\r
+                 SectionInstance,\r
+                 Buffer,\r
+                 Size\r
+                 );\r
+\r
+      if (!EFI_ERROR (Status)) {\r
+        goto Done;\r
+      }\r
+    }\r
+  }\r
+\r
+  //\r
+  // The required FFS section file is not found. \r
+  //\r
+  if (IndexFv == HandleCount) {\r
+    Status = EFI_NOT_FOUND;\r
+  }\r
+\r
+Done:\r
+  if (HandleBuffer != NULL) {  \r
+    FreePool(HandleBuffer);\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Searches all the availables firmware volumes and returns the first matching FFS section. \r
+\r
+  This function searches all the firmware volumes for FFS files with an FFS filename specified by NameGuid.  \r
+  The order that the firmware volumes is searched is not deterministic. For each FFS file found a search \r
+  is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance instances \r
+  of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer. \r
+  Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size. \r
+  It is the caller's responsibility to use FreePool() to free the allocated buffer.  \r
+  See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections \r
+  are retrieved from an FFS file based on SectionType and SectionInstance.\r
+\r
+  If SectionType is EFI_SECTION_TE, and the search with an FFS file fails, \r
+  the search will be retried with a section type of EFI_SECTION_PE32.\r
+  This function must be called with a TPL <= TPL_NOTIFY.\r
+\r
+  If NameGuid is NULL, then ASSERT().\r
+  If Buffer is NULL, then ASSERT().\r
   If Size is NULL, then ASSERT().\r
 \r
-  @param  NameGuid             Pointer to an EFI_GUID, which indicates the file name from which the requested \r
-                               section will be read. Type EFI_GUID is defined in \r
-                               InstallProtocolInterface() in the UEFI 2.0 specification. \r
-  @param  SectionType          Indicates the section type to return. SectionType in conjunction with \r
-                               SectionInstance indicates which section to return. Type \r
-                               EFI_SECTION_TYPE is defined in EFI_COMMON_SECTION_HEADER.\r
-  @param  SectionInstance      Indicates which instance of sections with a type of SectionType to return. \r
-                               SectionType in conjunction with SectionInstance indicates which section to \r
-                               return. SectionInstance is zero based.\r
-  @param  Buffer               Pointer to a pointer to a buffer in which the section contents are returned, not \r
-                               including the section header. Caller is responsible to free this memory.\r
-  @param  Size                 Pointer to a caller-allocated UINTN. It indicates the size of the memory represented by \r
-                               *Buffer.\r
-\r
-  @retval  EFI_SUCCESS        The image is found and data and size is returned.\r
-  @retval  EFI_NOT_FOUND      The image specified by NameGuid and SectionType can't be found.\r
-  @retval  EFI_OUT_OF_RESOURCES There were not enough resources to allocate the output data buffer or complete the operations.\r
-  @retval  EFI_DEVICE_ERROR A hardware error occurs during reading from the Firmware Volume.\r
-  @retval  EFI_ACCESS_DENIED  The firmware volume containing the searched Firmware File is configured to disallow reads.\r
 \r
+  @param  NameGuid             A pointer to to the FFS filename GUID to search for  \r
+                               within any of the firmware volumes in the platform. \r
+  @param  SectionType          Indicates the FFS section type to search for within \r
+                               the FFS file specified by NameGuid.\r
+  @param  SectionInstance      Indicates which section instance within the FFS file \r
+                               specified by NameGuid to retrieve.\r
+  @param  Buffer               On output, a pointer to a callee allocated buffer \r
+                               containing the FFS file section that was found.  \r
+                               Is it the caller's responsibility to free this buffer \r
+                               using FreePool().\r
+  @param  Size                 On output, a pointer to the size, in bytes, of Buffer.\r
+\r
+  @retval  EFI_SUCCESS          The specified FFS section was returned.\r
+  @retval  EFI_NOT_FOUND        The specified FFS section could not be found.\r
+  @retval  EFI_OUT_OF_RESOURCES There are not enough resources available to \r
+                                retrieve the matching FFS section.\r
+  @retval  EFI_DEVICE_ERROR     The FFS section could not be retrieves due to a \r
+                                device error.\r
+  @retval  EFI_ACCESS_DENIED    The FFS section could not be retrieves because the \r
+                                firmware volume that \r
+                                contains the matching FFS section does not allow reads.\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -289,46 +454,49 @@ Done:
 }\r
 \r
 /**\r
-  Locates a requested firmware section within a file and returns it to a buffer allocated by this function. \r
+  Searches the firmware volume that the currently executing module was loaded from and returns the first matching FFS section. \r
 \r
-  GetSectionFromFv () is used to read a specific section from a file within the same firmware volume from which\r
-  the running image is loaded. If the specific file is found, the function searches the specifc firmware section with type SectionType. \r
-  The details of this search order is defined in description of EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection () \r
-  found in PI Specification.\r
+  This function searches the firmware volume that the currently executing module was loaded \r
+  from for an FFS file with an FFS filename specified by NameGuid. If the FFS file is found a search \r
+  is made for FFS sections of type SectionType. If the FFS file contains at least SectionInstance \r
+  instances of the FFS section specified by SectionType, then the SectionInstance instance is returned in Buffer.\r
+  Buffer is allocated using AllocatePool(), and the size of the allocated buffer is returned in Size. \r
+  It is the caller's responsibility to use FreePool() to free the allocated buffer. \r
+  See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for details on how sections are retrieved from \r
+  an FFS file based on SectionType and SectionInstance.\r
 \r
-  If SectionType is EFI_SECTION_TE, EFI_SECTION_TE is used as section type to start the search. If EFI_SECTION_TE section \r
-  is not found, EFI_SECTION_PE32 will be used to try the search again. If no EFI_SECTION_PE32 section is found, EFI_NOT_FOUND \r
-  is returned.\r
+  If the currently executing module was not loaded from a firmware volume, then EFI_NOT_FOUND is returned.\r
+  If SectionType is EFI_SECTION_TE, and the search with an FFS file fails, \r
+  the search will be retried with a section type of EFI_SECTION_PE32.\r
+  \r
+  This function must be called with a TPL <= TPL_NOTIFY.\r
+  If NameGuid is NULL, then ASSERT().\r
+  If Buffer is NULL, then ASSERT().\r
+  If Size is NULL, then ASSERT().\r
 \r
-  The data and size is returned by Buffer and Size. The caller is responsible to free the Buffer allocated \r
-  by this function. This function can be only called at TPL_NOTIFY and below.\r
+  @param  NameGuid             A pointer to to the FFS filename GUID to search for \r
+                               within the firmware volumes that the currently \r
+                               executing module was loaded from.\r
+  @param  SectionType          Indicates the FFS section type to search for within \r
+                               the FFS file specified by NameGuid.\r
+  @param  SectionInstance      Indicates which section instance within the FFS file \r
+                               specified by NameGuid to retrieve.\r
+  @param  Buffer               On output, a pointer to a callee allocated buffer \r
+                               containing the FFS file section that was found.  \r
+                               Is it the caller's responsibility to free this buffer \r
+                               using FreePool().\r
+  @param  Size                 On output, a pointer to the size, in bytes, of Buffer.\r
 \r
-  If NameGuid is NULL, then ASSERT();\r
-  If Buffer is NULL, then ASSERT();\r
-  If Size is NULL, then ASSERT().\r
 \r
-  @param  NameGuid             Pointer to an EFI_GUID, which indicates the file name from which the requested \r
-                               section will be read. Type EFI_GUID is defined in \r
-                               InstallProtocolInterface() in the UEFI 2.0 specification. \r
-  @param  SectionType          Indicates the section type to return. SectionType in conjunction with \r
-                               SectionInstance indicates which section to return. Type \r
-                               EFI_SECTION_TYPE is defined in EFI_COMMON_SECTION_HEADER.\r
-  @param  SectionInstance      Indicates which instance of sections with a type of SectionType to return. \r
-                               SectionType in conjunction with SectionInstance indicates which section to \r
-                               return. SectionInstance is zero based.\r
-  @param  Buffer               Pointer to a pointer to a buffer in which the section contents are returned, not \r
-                               including the section header. Caller is responsible to free this memory.\r
-  @param  Size                 Pointer to a caller-allocated UINTN. It indicates the size of the memory represented by \r
-                               *Buffer.\r
-\r
-\r
-  @retval  EFI_SUCCESS        The image is found and data and size is returned.\r
-  @retval  EFI_UNSUPPORTED   FvHandle does not support EFI_FIRMWARE_VOLUME2_PROTOCOL.\r
-  @retval  EFI_NOT_FOUND      The image specified by NameGuid and SectionType can't be found.\r
-  @retval  EFI_OUT_OF_RESOURCES There were not enough resources to allocate the output data buffer or complete the operations.\r
-  @retval  EFI_DEVICE_ERROR A hardware error occurs during reading from the Firmware Volume.\r
-  @retval  EFI_ACCESS_DENIED  The firmware volume containing the searched Firmware File is configured to disallow reads.\r
-  \r
+  @retval  EFI_SUCCESS          The specified FFS section was returned.\r
+  @retval  EFI_NOT_FOUND        The specified FFS section could not be found.\r
+  @retval  EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve \r
+                                the matching FFS section.\r
+  @retval  EFI_DEVICE_ERROR     The FFS section could not be retrieves due to a \r
+                                device error.\r
+  @retval  EFI_ACCESS_DENIED    The FFS section could not be retrieves because the \r
+                                firmware volume that contains the matching FFS \r
+                                section does not allow reads.  \r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -352,39 +520,45 @@ GetSectionFromFv (
 \r
 \r
 /**\r
-  Locates a requested firmware section within a file and returns it to a buffer allocated by this function. \r
+  Searches the FFS file the the currently executing module was loaded from and returns the first matching FFS section.\r
 \r
-  GetSectionFromFfs () searches the specifc firmware section with type SectionType in the same firmware file from\r
-  which the running image is loaded. The details of this search order is defined in description of \r
-  EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection () found in PI Specification.\r
+  This function searches the FFS file that the currently executing module was loaded from for a FFS sections of type SectionType.\r
+  If the FFS file contains at least SectionInstance instances of the FFS section specified by SectionType, \r
+  then the SectionInstance instance is returned in Buffer. Buffer is allocated using AllocatePool(), \r
+  and the size of the allocated buffer is returned in Size. It is the caller's responsibility \r
+  to use FreePool() to free the allocated buffer. See EFI_FIRMWARE_VOLUME2_PROTOCOL.ReadSection() for \r
+  details on how sections are retrieved from an FFS file based on SectionType and SectionInstance.\r
 \r
-  If SectionType is EFI_SECTION_TE, EFI_SECTION_TE is used as section type to start the search. If EFI_SECTION_TE section \r
-  is not found, EFI_SECTION_PE32 will be used to try the search again. If no EFI_SECTION_PE32 section is found, EFI_NOT_FOUND \r
-  is returned.\r
+  If the currently executing module was not loaded from an FFS file, then EFI_NOT_FOUND is returned.\r
+  If SectionType is EFI_SECTION_TE, and the search with an FFS file fails, \r
+  the search will be retried with a section type of EFI_SECTION_PE32.\r
+  This function must be called with a TPL <= TPL_NOTIFY.\r
+  \r
+  If Buffer is NULL, then ASSERT().\r
+  If Size is NULL, then ASSERT().\r
 \r
 \r
-  The data and size is returned by Buffer and Size. The caller is responsible to free the Buffer allocated \r
-  by this function. This function can only be called at TPL_NOTIFY and below.\r
+  @param  SectionType          Indicates the FFS section type to search for within \r
+                               the FFS file that the currently executing module \r
+                               was loaded from.\r
+  @param  SectionInstance      Indicates which section instance to retrieve within \r
+                               the FFS file that the currently executing module \r
+                               was loaded from.\r
+  @param  Buffer               On output, a pointer to a callee allocated buffer \r
+                               containing the FFS file section that was found.  \r
+                               Is it the caller's responsibility to free this buffer \r
+                               using FreePool().\r
+  @param  Size                 On output, a pointer to the size, in bytes, of Buffer.\r
 \r
-  If Buffer is NULL, then ASSERT();\r
-  If Size is NULL, then ASSERT().\r
-\r
-  @param  SectionType          Indicates the section type to return. SectionType in conjunction with \r
-                               SectionInstance indicates which section to return. Type \r
-                               EFI_SECTION_TYPE is defined in EFI_COMMON_SECTION_HEADER.\r
-  @param  SectionInstance      Indicates which instance of sections with a type of SectionType to return. \r
-                               SectionType in conjunction with SectionInstance indicates which section to \r
-                               return. SectionInstance is zero based.\r
-  @param  Buffer               Pointer to a pointer to a buffer in which the section contents are returned, not \r
-                               including the section header. Caller is responsible to free this memory.\r
-  @param  Size                 Pointer to a caller-allocated UINTN. It indicates the size of the memory represented by \r
-                               *Buffer.\r
-\r
-  @retval  EFI_SUCCESS        The image is found and data and size is returned.\r
-  @retval  EFI_NOT_FOUND      The image specified by NameGuid and SectionType can't be found.\r
-  @retval  EFI_OUT_OF_RESOURCES There were not enough resources to allocate the output data buffer or complete the operations.\r
-  @retval  EFI_DEVICE_ERROR A hardware error occurs during reading from the Firmware Volume.\r
-  @retval  EFI_ACCESS_DENIED  The firmware volume containing the searched Firmware File is configured to disallow reads.\r
+  @retval  EFI_SUCCESS          The specified FFS section was returned.\r
+  @retval  EFI_NOT_FOUND        The specified FFS section could not be found.\r
+  @retval  EFI_OUT_OF_RESOURCES There are not enough resources available to retrieve \r
+                                the matching FFS section.\r
+  @retval  EFI_DEVICE_ERROR     The FFS section could not be retrieves due to a \r
+                                device error.\r
+  @retval  EFI_ACCESS_DENIED    The FFS section could not be retrieves because the \r
+                                firmware volume that contains the matching FFS \r
+                                section does not allow reads.  \r
   \r
 **/\r
 EFI_STATUS\r
@@ -406,3 +580,516 @@ GetSectionFromFfs (
            );\r
 }\r
 \r
+\r
+/**\r
+  Get the image file buffer data and buffer size by its device path. \r
+  \r
+  Access the file either from a firmware volume, from a file system interface, \r
+  or from the load file interface.\r
+  \r
+  Allocate memory to store the found image. The caller is responsible to free memory.\r
+\r
+  If FilePath is NULL, then NULL is returned.\r
+  If FileSize is NULL, then NULL is returned.\r
+  If AuthenticationStatus is NULL, then NULL is returned.\r
+\r
+  @param[in]       BootPolicy           Policy for Open Image File.If TRUE, indicates \r
+                                        that the request originates from the boot \r
+                                        manager, and that the boot manager is\r
+                                        attempting to load FilePath as a boot\r
+                                        selection. If FALSE, then FilePath must \r
+                                        match an exact file to be loaded.\r
+  @param[in]       FilePath             The pointer to the device path of the file\r
+                                        that is absracted to the file buffer.\r
+  @param[out]      FileSize             The pointer to the size of the abstracted \r
+                                        file buffer.\r
+  @param[out]      AuthenticationStatus Pointer to the authentication status.\r
+\r
+  @retval NULL   FilePath is NULL, or FileSize is NULL, or AuthenticationStatus is NULL, or the file can't be found.\r
+  @retval other  The abstracted file buffer. The caller is responsible to free memory.\r
+**/\r
+VOID *\r
+EFIAPI\r
+GetFileBufferByFilePath (\r
+  IN BOOLEAN                           BootPolicy,\r
+  IN CONST EFI_DEVICE_PATH_PROTOCOL    *FilePath,\r
+  OUT      UINTN                       *FileSize,\r
+  OUT UINT32                           *AuthenticationStatus\r
+  )\r
+{\r
+  EFI_DEVICE_PATH_PROTOCOL          *DevicePathNode;\r
+  EFI_DEVICE_PATH_PROTOCOL          *OrigDevicePathNode;\r
+  EFI_DEVICE_PATH_PROTOCOL          *TempDevicePathNode;\r
+  EFI_HANDLE                        Handle;\r
+  EFI_GUID                          *FvNameGuid;\r
+  EFI_FIRMWARE_VOLUME2_PROTOCOL     *FwVol;\r
+  EFI_SECTION_TYPE                  SectionType;\r
+  UINT8                             *ImageBuffer;\r
+  UINTN                             ImageBufferSize;\r
+  EFI_FV_FILETYPE                   Type;\r
+  EFI_FV_FILE_ATTRIBUTES            Attrib;\r
+  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL   *Volume;\r
+  EFI_FILE_HANDLE                   FileHandle;\r
+  EFI_FILE_HANDLE                   LastHandle;\r
+  EFI_FILE_INFO                     *FileInfo;\r
+  UINTN                             FileInfoSize;\r
+  EFI_LOAD_FILE_PROTOCOL            *LoadFile;\r
+  EFI_LOAD_FILE2_PROTOCOL           *LoadFile2;\r
+  EFI_STATUS                        Status;\r
+\r
+  //\r
+  // Check input File device path.\r
+  //\r
+  if (FilePath == NULL || FileSize == NULL || AuthenticationStatus == NULL) {\r
+    return NULL;\r
+  }\r
+\r
+  //\r
+  // Init local variable\r
+  //\r
+  TempDevicePathNode  = NULL;\r
+  FvNameGuid          = NULL;\r
+  FileInfo            = NULL;\r
+  FileHandle          = NULL;\r
+  ImageBuffer         = NULL;\r
+  ImageBufferSize     = 0;\r
+  *AuthenticationStatus = 0;\r
+  \r
+  //\r
+  // Copy File Device Path\r
+  //\r
+  OrigDevicePathNode = DuplicateDevicePath (FilePath);\r
+  if (OrigDevicePathNode == NULL) {\r
+    return NULL;\r
+  }\r
+\r
+  //\r
+  // Check whether this device path support FV2 protocol.\r
+  // Is so, this device path may contain a Image.\r
+  //\r
+  DevicePathNode = OrigDevicePathNode;\r
+  Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &DevicePathNode, &Handle);\r
+  if (!EFI_ERROR (Status)) {\r
+    //\r
+    // For FwVol File system there is only a single file name that is a GUID.\r
+    //\r
+    FvNameGuid = EfiGetNameGuidFromFwVolDevicePathNode ((CONST MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) DevicePathNode);\r
+    if (FvNameGuid == NULL) {\r
+      Status = EFI_INVALID_PARAMETER;\r
+    } else {\r
+      //\r
+      // Read image from the firmware file\r
+      //\r
+      Status = gBS->HandleProtocol (Handle, &gEfiFirmwareVolume2ProtocolGuid, (VOID**)&FwVol);\r
+      if (!EFI_ERROR (Status)) {\r
+        SectionType = EFI_SECTION_PE32;\r
+        ImageBuffer = NULL;\r
+        Status = FwVol->ReadSection (\r
+                          FwVol,\r
+                          FvNameGuid,\r
+                          SectionType,\r
+                          0,\r
+                          (VOID **)&ImageBuffer,\r
+                          &ImageBufferSize,\r
+                          AuthenticationStatus\r
+                          );\r
+        if (EFI_ERROR (Status)) {\r
+          //\r
+          // Try a raw file, since a PE32 SECTION does not exist\r
+          //\r
+          if (ImageBuffer != NULL) {\r
+            FreePool (ImageBuffer);\r
+            *AuthenticationStatus = 0;\r
+          }\r
+          ImageBuffer = NULL;\r
+          Status = FwVol->ReadFile (\r
+                            FwVol,\r
+                            FvNameGuid,\r
+                            (VOID **)&ImageBuffer,\r
+                            &ImageBufferSize,\r
+                            &Type,\r
+                            &Attrib,\r
+                            AuthenticationStatus\r
+                            );\r
+        }\r
+      }\r
+    }\r
+    if (!EFI_ERROR (Status)) {\r
+      goto Finish;\r
+    }\r
+  }\r
+\r
+  //\r
+  // Attempt to access the file via a file system interface\r
+  //\r
+  DevicePathNode = OrigDevicePathNode;\r
+  Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &DevicePathNode, &Handle);\r
+  if (!EFI_ERROR (Status)) {\r
+    Status = gBS->HandleProtocol (Handle, &gEfiSimpleFileSystemProtocolGuid, (VOID**)&Volume);\r
+    if (!EFI_ERROR (Status)) {\r
+      //\r
+      // Open the Volume to get the File System handle\r
+      //\r
+      Status = Volume->OpenVolume (Volume, &FileHandle);\r
+      if (!EFI_ERROR (Status)) {\r
+        //\r
+        // Duplicate the device path to avoid the access to unaligned device path node.\r
+        // Because the device path consists of one or more FILE PATH MEDIA DEVICE PATH\r
+        // nodes, It assures the fields in device path nodes are 2 byte aligned.\r
+        //\r
+        TempDevicePathNode = DuplicateDevicePath (DevicePathNode);\r
+        if (TempDevicePathNode == NULL) {\r
+          FileHandle->Close (FileHandle);\r
+          //\r
+          // Setting Status to an EFI_ERROR value will cause the rest of\r
+          // the file system support below to be skipped.\r
+          //\r
+          Status = EFI_OUT_OF_RESOURCES;\r
+        }\r
+        //\r
+        // Parse each MEDIA_FILEPATH_DP node. There may be more than one, since the\r
+        // directory information and filename can be seperate. The goal is to inch\r
+        // our way down each device path node and close the previous node\r
+        //\r
+        DevicePathNode = TempDevicePathNode;\r
+        while (!EFI_ERROR (Status) && !IsDevicePathEnd (DevicePathNode)) {\r
+          if (DevicePathType (DevicePathNode) != MEDIA_DEVICE_PATH ||\r
+              DevicePathSubType (DevicePathNode) != MEDIA_FILEPATH_DP) {\r
+            Status = EFI_UNSUPPORTED;\r
+            break;\r
+          }\r
+  \r
+          LastHandle = FileHandle;\r
+          FileHandle = NULL;\r
+  \r
+          Status = LastHandle->Open (\r
+                                LastHandle,\r
+                                &FileHandle,\r
+                                ((FILEPATH_DEVICE_PATH *) DevicePathNode)->PathName,\r
+                                EFI_FILE_MODE_READ,\r
+                                0\r
+                                );\r
+  \r
+          //\r
+          // Close the previous node\r
+          //\r
+          LastHandle->Close (LastHandle);\r
+  \r
+          DevicePathNode = NextDevicePathNode (DevicePathNode);\r
+        }\r
+  \r
+        if (!EFI_ERROR (Status)) {\r
+          //\r
+          // We have found the file. Now we need to read it. Before we can read the file we need to\r
+          // figure out how big the file is.\r
+          //\r
+          FileInfo = NULL;\r
+          FileInfoSize = 0;\r
+          Status = FileHandle->GetInfo (\r
+                                FileHandle,\r
+                                &gEfiFileInfoGuid,\r
+                                &FileInfoSize,\r
+                                FileInfo\r
+                                );\r
+  \r
+          if (Status == EFI_BUFFER_TOO_SMALL) {\r
+            FileInfo = AllocatePool (FileInfoSize);\r
+            if (FileInfo == NULL) {\r
+              Status = EFI_OUT_OF_RESOURCES;\r
+            } else {\r
+              Status = FileHandle->GetInfo (\r
+                                    FileHandle,\r
+                                    &gEfiFileInfoGuid,\r
+                                    &FileInfoSize,\r
+                                    FileInfo\r
+                                    );\r
+            }\r
+          }\r
+          \r
+          if (!EFI_ERROR (Status) && (FileInfo != NULL)) {\r
+            if ((FileInfo->Attribute & EFI_FILE_DIRECTORY) == 0) {\r
+              //\r
+              // Allocate space for the file\r
+              //\r
+              ImageBuffer = AllocatePool ((UINTN)FileInfo->FileSize);\r
+              if (ImageBuffer == NULL) {\r
+                Status = EFI_OUT_OF_RESOURCES;\r
+              } else {\r
+                //\r
+                // Read the file into the buffer we allocated\r
+                //\r
+                ImageBufferSize = (UINTN)FileInfo->FileSize;\r
+                Status          = FileHandle->Read (FileHandle, &ImageBufferSize, ImageBuffer);\r
+              }\r
+            }\r
+          }\r
+        }\r
+        //\r
+        // Close the file and Free FileInfo and TempDevicePathNode since we are done\r
+        // \r
+        if (FileInfo != NULL) {\r
+          FreePool (FileInfo);\r
+        }\r
+        if (FileHandle != NULL) {\r
+          FileHandle->Close (FileHandle);\r
+        }\r
+        if (TempDevicePathNode != NULL) {\r
+          FreePool (TempDevicePathNode);\r
+        }\r
+      }\r
+    }\r
+    if (!EFI_ERROR (Status)) {\r
+      goto Finish;\r
+    }\r
+  }\r
+\r
+  //\r
+  // Attempt to access the file via LoadFile2 interface\r
+  //\r
+  if (!BootPolicy) {\r
+    DevicePathNode = OrigDevicePathNode;\r
+    Status = gBS->LocateDevicePath (&gEfiLoadFile2ProtocolGuid, &DevicePathNode, &Handle);\r
+    if (!EFI_ERROR (Status)) {\r
+      Status = gBS->HandleProtocol (Handle, &gEfiLoadFile2ProtocolGuid, (VOID**)&LoadFile2);\r
+      if (!EFI_ERROR (Status)) {\r
+        //\r
+        // Call LoadFile2 with the correct buffer size\r
+        //\r
+        ImageBufferSize = 0;\r
+        ImageBuffer     = NULL;\r
+        Status = LoadFile2->LoadFile (\r
+                             LoadFile2,\r
+                             DevicePathNode,\r
+                             FALSE,\r
+                             &ImageBufferSize,\r
+                             ImageBuffer\r
+                             );\r
+        if (Status == EFI_BUFFER_TOO_SMALL) {\r
+          ImageBuffer = AllocatePool (ImageBufferSize);\r
+          if (ImageBuffer == NULL) {\r
+            Status = EFI_OUT_OF_RESOURCES;\r
+          } else {\r
+            Status = LoadFile2->LoadFile (\r
+                                 LoadFile2,\r
+                                 DevicePathNode,\r
+                                 FALSE,\r
+                                 &ImageBufferSize,\r
+                                 ImageBuffer\r
+                                 );\r
+          }\r
+        }\r
+      }\r
+      if (!EFI_ERROR (Status)) {\r
+        goto Finish;\r
+      }\r
+    }\r
+  }\r
+\r
+  //\r
+  // Attempt to access the file via LoadFile interface\r
+  //\r
+  DevicePathNode = OrigDevicePathNode;\r
+  Status = gBS->LocateDevicePath (&gEfiLoadFileProtocolGuid, &DevicePathNode, &Handle);\r
+  if (!EFI_ERROR (Status)) {\r
+    Status = gBS->HandleProtocol (Handle, &gEfiLoadFileProtocolGuid, (VOID**)&LoadFile);\r
+    if (!EFI_ERROR (Status)) {\r
+      //\r
+      // Call LoadFile with the correct buffer size\r
+      //\r
+      ImageBufferSize = 0;\r
+      ImageBuffer     = NULL;\r
+      Status = LoadFile->LoadFile (\r
+                           LoadFile,\r
+                           DevicePathNode,\r
+                           BootPolicy,\r
+                           &ImageBufferSize,\r
+                           ImageBuffer\r
+                           );\r
+      if (Status == EFI_BUFFER_TOO_SMALL) {\r
+        ImageBuffer = AllocatePool (ImageBufferSize);\r
+        if (ImageBuffer == NULL) {\r
+          Status = EFI_OUT_OF_RESOURCES;\r
+        } else {\r
+          Status = LoadFile->LoadFile (\r
+                               LoadFile,\r
+                               DevicePathNode,\r
+                               BootPolicy,\r
+                               &ImageBufferSize,\r
+                               ImageBuffer\r
+                               );\r
+        }\r
+      }\r
+    }\r
+  }\r
+\r
+Finish:\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    if (ImageBuffer != NULL) {\r
+      FreePool (ImageBuffer);\r
+      ImageBuffer = NULL;\r
+    }\r
+    *FileSize = 0;\r
+  } else {\r
+    *FileSize = ImageBufferSize;\r
+  }\r
+\r
+  FreePool (OrigDevicePathNode);\r
+\r
+  return ImageBuffer;\r
+}\r
+\r
+/**\r
+  Searches all the available firmware volumes and returns the file device path of first matching\r
+  FFS section.\r
+\r
+  This function searches all the firmware volumes for FFS files with an FFS filename specified by NameGuid.\r
+  The order that the firmware volumes is searched is not deterministic. For each FFS file found a search\r
+  is made for FFS sections of type SectionType.\r
+\r
+  If SectionType is EFI_SECTION_TE, and the search with an FFS file fails,\r
+  the search will be retried with a section type of EFI_SECTION_PE32.\r
+  This function must be called with a TPL <= TPL_NOTIFY.\r
+\r
+  If NameGuid is NULL, then ASSERT().\r
+\r
+   @param  NameGuid             A pointer to to the FFS filename GUID to search for\r
+                                within any of the firmware volumes in the platform.\r
+   @param  SectionType          Indicates the FFS section type to search for within\r
+                                the FFS file specified by NameGuid.\r
+   @param  SectionInstance      Indicates which section instance within the FFS file\r
+                                specified by NameGuid to retrieve.\r
+   @param  FvFileDevicePath     Device path for the target FFS\r
+                                file.\r
+\r
+   @retval  EFI_SUCCESS           The specified file device path of FFS section was returned.\r
+   @retval  EFI_NOT_FOUND         The specified file device path of FFS section could not be found.\r
+   @retval  EFI_DEVICE_ERROR      The FFS section could not be retrieves due to a\r
+                                  device error.\r
+   @retval  EFI_ACCESS_DENIED     The FFS section could not be retrieves because the\r
+                                  firmware volume that contains the matching FFS section does not\r
+                                  allow reads.\r
+   @retval  EFI_INVALID_PARAMETER FvFileDevicePath is NULL.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+GetFileDevicePathFromAnyFv (\r
+  IN CONST  EFI_GUID                  *NameGuid,\r
+  IN        EFI_SECTION_TYPE          SectionType,\r
+  IN        UINTN                     SectionInstance,\r
+  OUT       EFI_DEVICE_PATH_PROTOCOL  **FvFileDevicePath\r
+  )\r
+{\r
+  EFI_STATUS                        Status;\r
+  EFI_HANDLE                        *HandleBuffer;\r
+  UINTN                             HandleCount;\r
+  UINTN                             Index;\r
+  EFI_HANDLE                        FvHandle;\r
+  EFI_DEVICE_PATH_PROTOCOL          *FvDevicePath;\r
+  MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *TempFvFileDevicePath;\r
+  VOID                              *Buffer;\r
+  UINTN                             Size;\r
+\r
+  if (FvFileDevicePath == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  HandleBuffer         = NULL;\r
+  FvDevicePath         = NULL;\r
+  TempFvFileDevicePath = NULL;\r
+  Buffer               = NULL;\r
+  Size                 = 0;\r
+\r
+  //\r
+  // Search the FV that contain the caller's FFS first.\r
+  // FV builder can choose to build FFS into the this FV\r
+  // so that this implementation of GetSectionFromAnyFv\r
+  // will locate the FFS faster.\r
+  //\r
+  FvHandle = InternalImageHandleToFvHandle (gImageHandle);\r
+  Status = InternalGetSectionFromFv (\r
+             FvHandle,\r
+             NameGuid,\r
+             SectionType,\r
+             SectionInstance,\r
+             &Buffer,\r
+             &Size\r
+             );\r
+  if (!EFI_ERROR (Status)) {\r
+    goto Done;\r
+  }\r
+\r
+  Status = gBS->LocateHandleBuffer (\r
+                  ByProtocol,\r
+                  &gEfiFirmwareVolume2ProtocolGuid,\r
+                  NULL,\r
+                  &HandleCount,\r
+                  &HandleBuffer\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    goto Done;\r
+  }\r
+\r
+  for (Index = 0; Index < HandleCount; Index++) {\r
+    //\r
+    // Skip the FV that contain the caller's FFS\r
+    //\r
+    if (HandleBuffer[Index] != FvHandle) {\r
+      Status = InternalGetSectionFromFv (\r
+                 HandleBuffer[Index],\r
+                 NameGuid,\r
+                 SectionType,\r
+                 SectionInstance,\r
+                 &Buffer,\r
+                 &Size\r
+                 );\r
+\r
+      if (!EFI_ERROR (Status)) {\r
+        //\r
+        // Update FvHandle to the current handle.\r
+        //\r
+        FvHandle = HandleBuffer[Index];\r
+        goto Done;\r
+      }\r
+    }\r
+  }\r
+\r
+  if (Index == HandleCount) {\r
+    Status = EFI_NOT_FOUND;\r
+  }\r
+\r
+Done:\r
+  if (Status == EFI_SUCCESS) {\r
+    //\r
+    // Build a device path to the file in the FV to pass into gBS->LoadImage\r
+    //\r
+    Status = gBS->HandleProtocol (FvHandle, &gEfiDevicePathProtocolGuid, (VOID **)&FvDevicePath);\r
+    if (EFI_ERROR (Status)) {\r
+      *FvFileDevicePath = NULL;\r
+    } else {\r
+      TempFvFileDevicePath = AllocateZeroPool (sizeof (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH) + END_DEVICE_PATH_LENGTH);\r
+      if (TempFvFileDevicePath == NULL) {\r
+        *FvFileDevicePath = NULL;\r
+        return EFI_OUT_OF_RESOURCES;\r
+      }\r
+      EfiInitializeFwVolDevicepathNode ((MEDIA_FW_VOL_FILEPATH_DEVICE_PATH*)TempFvFileDevicePath, NameGuid);\r
+      SetDevicePathEndNode (NextDevicePathNode (TempFvFileDevicePath));\r
+      *FvFileDevicePath = AppendDevicePath (\r
+                            FvDevicePath,\r
+                            (EFI_DEVICE_PATH_PROTOCOL *)TempFvFileDevicePath\r
+                            );\r
+      FreePool (TempFvFileDevicePath);\r
+    }\r
+  }\r
+\r
+  if (Buffer != NULL) {\r
+    FreePool (Buffer);\r
+  }\r
+\r
+  if (HandleBuffer != NULL) {\r
+    FreePool (HandleBuffer);\r
+  }\r
+\r
+  return Status;\r
+}\r