]> 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 9cf845d6fc1bac276ea94401f66b2cf66ec8039b..1827c9216fbc231c98b756b0bc66e7c7b13c7b58 100644 (file)
@@ -2,7 +2,8 @@
   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
@@ -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,7 +90,6 @@ 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
@@ -122,13 +128,17 @@ InternalGetSectionFromFv (
   EFI_STATUS                    Status;\r
   EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
   UINT32                        AuthenticationStatus;\r
-  VOID*                         TempBuffer;\r
 \r
   ASSERT (NameGuid != NULL);\r
   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
@@ -171,17 +181,6 @@ InternalGetSectionFromFv (
                     );\r
   }\r
 \r
-  if (!EFI_ERROR (Status)) {\r
-    //\r
-    // The found buffer by FV protocol is allocated by gBS AllocatePool() service. \r
-    // Copy the found buffer to the allocated buffer by AllocatePool().\r
-    // So, the returned buffer can be freed by FreePool().\r
-    //\r
-    TempBuffer = AllocateCopyPool (*Size, *Buffer);\r
-    gBS->FreePool (*Buffer);\r
-    *Buffer = TempBuffer;\r
-  }\r
-\r
   return Status;\r
 }\r
 \r
@@ -324,11 +323,7 @@ GetSectionFromAnyFvByFileType  (
 \r
 Done:\r
   if (HandleBuffer != NULL) {  \r
-    //\r
-    // HandleBuffer is allocated by gBS AllocatePool() service. \r
-    // So, gBS FreePool() service is used to free HandleBuffer.\r
-    //\r
-    gBS->FreePool (HandleBuffer);\r
+    FreePool(HandleBuffer);\r
   }\r
 \r
   return Status;\r
@@ -451,12 +446,8 @@ GetSectionFromAnyFv  (
 \r
 Done:\r
   \r
-  if (HandleBuffer != NULL) {\r
-    //\r
-    // HandleBuffer is allocated by gBS AllocatePool() service. \r
-    // So, gBS FreePool() service is used to free HandleBuffer.\r
-    //\r
-    gBS->FreePool (HandleBuffer);\r
+  if (HandleBuffer != NULL) {  \r
+    FreePool(HandleBuffer);\r
   }\r
   return Status;\r
   \r
@@ -598,7 +589,7 @@ 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
@@ -612,10 +603,9 @@ GetSectionFromFfs (
                                         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 The pointer to a caller-allocated UINT32 \r
-                                        in which the authentication status is returned.\r
+  @param[out]      AuthenticationStatus Pointer to the authentication status.\r
 \r
-  @retval NULL   File is NULL, or FileSize is NULL. Or, the file can't be found.\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
@@ -635,7 +625,6 @@ GetFileBufferByFilePath (
   EFI_FIRMWARE_VOLUME2_PROTOCOL     *FwVol;\r
   EFI_SECTION_TYPE                  SectionType;\r
   UINT8                             *ImageBuffer;\r
-  UINT8                             *TempBuffer;\r
   UINTN                             ImageBufferSize;\r
   EFI_FV_FILETYPE                   Type;\r
   EFI_FV_FILE_ATTRIBUTES            Attrib;\r
@@ -663,7 +652,6 @@ GetFileBufferByFilePath (
   FileInfo            = NULL;\r
   FileHandle          = NULL;\r
   ImageBuffer         = NULL;\r
-  TempBuffer          = NULL;\r
   ImageBufferSize     = 0;\r
   *AuthenticationStatus = 0;\r
   \r
@@ -724,19 +712,11 @@ GetFileBufferByFilePath (
                             AuthenticationStatus\r
                             );\r
         }\r
-        if (!EFI_ERROR (Status)) {\r
-          //\r
-          // The found buffer by FV protocol is allocated by gBS AllocatePool() service. \r
-          // Copy the found buffer to the allocated buffer by AllocatePool().\r
-          // Then, this returned buffer can be freed by FreePool().\r
-          //\r
-          TempBuffer = AllocateCopyPool (ImageBufferSize, ImageBuffer);\r
-          gBS->FreePool (ImageBuffer);\r
-          ImageBuffer = TempBuffer;\r
-        }\r
       }\r
     }\r
-    goto Finish;\r
+    if (!EFI_ERROR (Status)) {\r
+      goto Finish;\r
+    }\r
   }\r
 \r
   //\r
@@ -760,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
@@ -769,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
@@ -824,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
@@ -848,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
@@ -890,7 +879,9 @@ GetFileBufferByFilePath (
           }\r
         }\r
       }\r
-      goto Finish;\r
+      if (!EFI_ERROR (Status)) {\r
+        goto Finish;\r
+      }\r
     }\r
   }\r
 \r
@@ -947,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