]> 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 49c9ad492ed9874eddd7d1bd11ca52005456530b..1827c9216fbc231c98b756b0bc66e7c7b13c7b58 100644 (file)
@@ -2,11 +2,12 @@
   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 - 2010, Intel Corporation. All rights reserved.<BR>\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
@@ -40,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
@@ -61,6 +62,12 @@ 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
@@ -83,23 +90,29 @@ InternalImageHandleToFvHandle (
   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  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 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  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 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_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
@@ -120,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
@@ -166,7 +184,150 @@ 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
+  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
+  @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
+  @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
+  //\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
+  //\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
@@ -189,19 +350,26 @@ InternalGetSectionFromFv (
   If Size is NULL, then ASSERT().\r
 \r
 \r
-  @param  NameGuid             A pointer to to the FFS filename GUID to search for within \r
-                               any of the firmware volumes in the platform. \r
-  @param  SectionType          Indicates the FFS section type to search for within the FFS file specified by NameGuid.\r
-  @param  SectionInstance      Indicates which section instance within the FFS file specified by NameGuid to retrieve.\r
-  @param  Buffer               On output, a pointer to a callee allocated buffer containing the FFS file section that was found.  \r
-                               Is it the caller's responsibility to free this buffer using FreePool().\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 retrieve the matching FFS section.\r
-  @retval  EFI_DEVICE_ERROR     The FFS section could not be retrieves due to a device error.\r
-  @retval  EFI_ACCESS_DENIED    The FFS section could not be retrieves because the firmware volume that \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
@@ -306,21 +474,29 @@ Done:
   If Buffer is NULL, then ASSERT().\r
   If Size is NULL, then ASSERT().\r
 \r
-  @param  NameGuid             A pointer to to the FFS filename GUID to search for within \r
-                               the firmware volumes that the currently executing module was loaded from.\r
-  @param  SectionType          Indicates the FFS section type to search for within the FFS file specified by NameGuid.\r
-  @param  SectionInstance      Indicates which section instance within the FFS file specified by NameGuid to retrieve.\r
-  @param  Buffer               On output, a pointer to a callee allocated buffer containing the FFS file section that was found.  \r
-                               Is it the caller's responsibility to free this buffer using FreePool().\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
 \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 the matching FFS section.\r
-  @retval  EFI_DEVICE_ERROR     The FFS section could not be retrieves due to a device error.\r
-  @retval  EFI_ACCESS_DENIED    The FFS section could not be retrieves because the firmware volume that \r
-                                contains the matching FFS section does not allow reads.  \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
@@ -362,20 +538,27 @@ GetSectionFromFv (
   If Size is NULL, then ASSERT().\r
 \r
 \r
-  @param  SectionType          Indicates the FFS section type to search for within the FFS file \r
-                               that the currently executing module was loaded from.\r
-  @param  SectionInstance      Indicates which section instance to retrieve within the FFS file \r
-                               that the currently executing module was loaded from.\r
-  @param  Buffer               On output, a pointer to a callee allocated buffer containing the FFS file section that was found.  \r
-                               Is it the caller's responsibility to free this buffer using FreePool().\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
   @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 the matching FFS section.\r
-  @retval  EFI_DEVICE_ERROR     The FFS section could not be retrieves due to a device error.\r
-  @retval  EFI_ACCESS_DENIED    The FFS section could not be retrieves because the firmware volume that \r
-                                contains the matching FFS section does not allow reads.  \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,21 +589,23 @@ GetSectionFromFfs (
   \r
   Allocate memory to store the found image. The caller is responsible to free memory.\r
 \r
-  If File is NULL, then NULL is returned.\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 that the request \r
-                                        originates from the boot manager, and that the boot manager is\r
-                                        attempting to load FilePath as a boot selection. If FALSE, \r
-                                        then FilePath must match an exact file to be loaded.\r
-  @param[in]       FilePath             Pointer to the device path of the file that is absracted to\r
-                                        the file buffer.\r
-  @param[out]      FileSize             Pointer to the size of the abstracted file buffer.\r
-  @param[out]      AuthenticationStatus Pointer to a caller-allocated UINT32 in which the authentication\r
-                                        status is returned.\r
-\r
-  @retval NULL   File is NULL, or FileSize is NULL. Or the file can't be found.\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
@@ -529,7 +714,9 @@ GetFileBufferByFilePath (
         }\r
       }\r
     }\r
-    goto Finish;\r
+    if (!EFI_ERROR (Status)) {\r
+      goto Finish;\r
+    }\r
   }\r
 \r
   //\r
@@ -553,8 +740,11 @@ GetFileBufferByFilePath (
         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
-          goto Finish;\r
         }\r
         //\r
         // Parse each MEDIA_FILEPATH_DP node. There may be more than one, since the\r
@@ -562,7 +752,7 @@ GetFileBufferByFilePath (
         // our way down each device path node and close the previous node\r
         //\r
         DevicePathNode = TempDevicePathNode;\r
-        while (!IsDevicePathEnd (DevicePathNode) && !EFI_ERROR (Status)) {\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
@@ -617,18 +807,20 @@ GetFileBufferByFilePath (
           }\r
           \r
           if (!EFI_ERROR (Status) && (FileInfo != NULL)) {\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
+            if ((FileInfo->Attribute & EFI_FILE_DIRECTORY) == 0) {\r
               //\r
-              // Read the file into the buffer we allocated\r
+              // Allocate space for the file\r
               //\r
-              ImageBufferSize = (UINTN)FileInfo->FileSize;\r
-              Status          = FileHandle->Read (FileHandle, &ImageBufferSize, ImageBuffer);\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
@@ -641,10 +833,14 @@ GetFileBufferByFilePath (
         if (FileHandle != NULL) {\r
           FileHandle->Close (FileHandle);\r
         }\r
-        FreePool (TempDevicePathNode);\r
+        if (TempDevicePathNode != NULL) {\r
+          FreePool (TempDevicePathNode);\r
+        }\r
       }\r
     }\r
-    goto Finish;\r
+    if (!EFI_ERROR (Status)) {\r
+      goto Finish;\r
+    }\r
   }\r
 \r
   //\r
@@ -683,7 +879,9 @@ GetFileBufferByFilePath (
           }\r
         }\r
       }\r
-      goto Finish;\r
+      if (!EFI_ERROR (Status)) {\r
+        goto Finish;\r
+      }\r
     }\r
   }\r
 \r
@@ -740,3 +938,158 @@ Finish:
 \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