]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Image/ImageFile.c
move header files in MdeModulePkg\Core\Dxe except DxeMain.h into their corresponding...
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Image / ImageFile.c
index 3776e57513f94989370931a25eba2a7127b9a1a9..453e88b940aca19604d658889d41fe3c8f001588 100644 (file)
@@ -12,7 +12,40 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 **/\r
 \r
-#include <DxeMain.h>\r
+#include "DxeMain.h"\r
+#include "Image.h"\r
+\r
+/**\r
+  Search a handle to a device on a specified device path that supports a specified protocol,\r
+  interface of that protocol on that handle is another output.\r
+\r
+\r
+\r
+  @param  Protocol               The protocol to search for\r
+  @param  FilePath               The specified device path\r
+  @param  Interface              Interface of the protocol on the handle\r
+  @param  Handle                 The handle to the device on the specified device\r
+                                 path that supports the protocol.\r
+\r
+  @return Status code.\r
+\r
+**/\r
+EFI_STATUS\r
+CoreDevicePathToInterface (\r
+  IN EFI_GUID                     *Protocol,\r
+  IN EFI_DEVICE_PATH_PROTOCOL     **FilePath,\r
+  OUT VOID                        **Interface,\r
+  OUT EFI_HANDLE                  *Handle\r
+  )\r
+{\r
+  EFI_STATUS                      Status;\r
+\r
+  Status = CoreLocateDevicePath (Protocol, FilePath, Handle);\r
+  if (!EFI_ERROR (Status)) {\r
+    Status = CoreHandleProtocol (*Handle, Protocol, Interface);\r
+  }\r
+  return Status;\r
+}\r
 \r
 \r
 /**\r
@@ -177,7 +210,7 @@ CoreOpenImageFile (
       // 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
-      FilePathNode = (FILEPATH_DEVICE_PATH *)CoreDuplicateDevicePath((EFI_DEVICE_PATH_PROTOCOL *)(UINTN)FilePathNode);\r
+      FilePathNode = (FILEPATH_DEVICE_PATH *)DuplicateDevicePath((EFI_DEVICE_PATH_PROTOCOL *)(UINTN)FilePathNode);\r
       if (FilePathNode == NULL) {\r
         FileHandle->Close (FileHandle);\r
         Status = EFI_OUT_OF_RESOURCES;\r
@@ -231,23 +264,33 @@ CoreOpenImageFile (
         // figure out how big the file is.\r
         //\r
         FileInfo = NULL;\r
-        FileInfoSize = sizeof (EFI_FILE_INFO);\r
-        while (CoreGrowBuffer (&Status, (VOID **)&FileInfo, FileInfoSize)) {\r
-          //\r
-          // Automatically allocate buffer of the correct size and make the call\r
-          //\r
-          Status = FileHandle->GetInfo (\r
-                                FileHandle,\r
-                                &gEfiFileInfoGuid,\r
-                                &FileInfoSize,\r
-                                FileInfo\r
-                                );\r
+        FileInfoSize = 0;\r
+        Status = FileHandle->GetInfo (\r
+                              FileHandle,\r
+                              &gEfiFileInfoGuid,\r
+                              &FileInfoSize,\r
+                              FileInfo\r
+                              );\r
+        if (Status == EFI_BUFFER_TOO_SMALL) {\r
+          FileInfo = AllocatePool (FileInfoSize);\r
+          if (FileInfo != NULL) {\r
+            Status = FileHandle->GetInfo (\r
+                                  FileHandle,\r
+                                  &gEfiFileInfoGuid,\r
+                                  &FileInfoSize,\r
+                                  FileInfo\r
+                                  );\r
+          } else {\r
+            Status = EFI_OUT_OF_RESOURCES;\r
+            goto Done;\r
+          }\r
         }\r
+        \r
         if (!EFI_ERROR (Status)) {\r
           //\r
           // Allocate space for the file\r
           //\r
-          ImageFileHandle->Source = CoreAllocateBootServicesPool ((UINTN)FileInfo->FileSize);\r
+          ImageFileHandle->Source = AllocatePool ((UINTN)FileInfo->FileSize);\r
           if (ImageFileHandle->Source != NULL) {\r
             //\r
             // Read the file into the buffer we allocated\r
@@ -260,6 +303,7 @@ CoreOpenImageFile (
             // Close the file since we are done\r
             //\r
             FileHandle->Close (FileHandle);\r
+            CoreFreePool (FileInfo);\r
           } else {\r
             Status = EFI_OUT_OF_RESOURCES;\r
           }\r
@@ -286,23 +330,31 @@ CoreOpenImageFile (
     //\r
     // Call LoadFile with the correct buffer size\r
     //\r
-    while (CoreGrowBuffer (&Status, (VOID **)&ImageFileHandle->Source, ImageFileHandle->SourceSize)) {\r
-      Status = LoadFile->LoadFile (\r
-                           LoadFile,\r
-                           TempFilePath,\r
-                           BootPolicy,\r
-                           &ImageFileHandle->SourceSize,\r
-                           ImageFileHandle->Source\r
-                           );\r
-      //\r
-      // If success or other error happens, stop loop\r
-      //\r
-      if (Status != EFI_BUFFER_TOO_SMALL) {\r
-        break;\r
+    ASSERT (ImageFileHandle->SourceSize == 0);\r
+    ASSERT (ImageFileHandle->Source == NULL);\r
+    Status = LoadFile->LoadFile (\r
+                         LoadFile,\r
+                         TempFilePath,\r
+                         BootPolicy,\r
+                         &ImageFileHandle->SourceSize,\r
+                         ImageFileHandle->Source\r
+                         );\r
+    if (Status == EFI_BUFFER_TOO_SMALL) {\r
+      ImageFileHandle->Source = AllocatePool (ImageFileHandle->SourceSize);\r
+      if (ImageFileHandle->Source == NULL) {\r
+        Status = EFI_OUT_OF_RESOURCES;\r
+      } else {\r
+        Status = LoadFile->LoadFile (\r
+                             LoadFile,\r
+                             TempFilePath,\r
+                             BootPolicy,\r
+                             &ImageFileHandle->SourceSize,\r
+                             ImageFileHandle->Source\r
+                             );\r
       }\r
     }\r
 \r
-    if (!EFI_ERROR (Status) || Status == EFI_ALREADY_STARTED) {\r
+    if (!EFI_ERROR (Status)) {\r
       ImageFileHandle->FreeBuffer = TRUE;\r
       goto Done;\r
     }\r
@@ -315,11 +367,10 @@ CoreOpenImageFile (
   Status = EFI_NOT_FOUND;\r
 \r
 Done:\r
-\r
   //\r
   // If the file was not accessed, clean up\r
   //\r
-  if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {\r
+  if (EFI_ERROR (Status)) {\r
     if (ImageFileHandle->FreeBuffer) {\r
       //\r
       // Free the source buffer if we allocated it\r
@@ -377,99 +428,3 @@ CoreReadImageFile (
   return EFI_SUCCESS;\r
 }\r
 \r
-\r
-/**\r
-  Search a handle to a device on a specified device path that supports a specified protocol,\r
-  interface of that protocol on that handle is another output.\r
-\r
-  @param  Protocol               The protocol to search for\r
-  @param  FilePath               The specified device path\r
-  @param  Interface              Interface of the protocol on the handle\r
-  @param  Handle                 The handle to the device on the specified device\r
-                                 path that supports the protocol.\r
-\r
-  @return Status code.\r
-\r
-**/\r
-EFI_STATUS\r
-CoreDevicePathToInterface (\r
-  IN EFI_GUID                     *Protocol,\r
-  IN EFI_DEVICE_PATH_PROTOCOL     **FilePath,\r
-  OUT VOID                        **Interface,\r
-  OUT EFI_HANDLE                  *Handle\r
-  )\r
-{\r
-  EFI_STATUS                      Status;\r
-\r
-  Status = CoreLocateDevicePath (Protocol, FilePath, Handle);\r
-  if (!EFI_ERROR (Status)) {\r
-    Status = CoreHandleProtocol (*Handle, Protocol, Interface);\r
-  }\r
-  return Status;\r
-}\r
-\r
-\r
-/**\r
-  Helper function called as part of the code needed\r
-  to allocate the proper sized buffer for various\r
-  EFI interfaces.\r
-\r
-  @param  Status                 Current status\r
-  @param  Buffer                 Current allocated buffer, or NULL\r
-  @param  BufferSize             Current buffer size needed\r
-\r
-  @retval TRUE                   if the buffer was reallocated and the caller\r
-                                 should try the API again.\r
-  @retval FALSE                  buffer could not be allocated and the caller\r
-                                 should not try the API again.\r
-\r
-**/\r
-BOOLEAN\r
-CoreGrowBuffer (\r
-  IN OUT EFI_STATUS   *Status,\r
-  IN OUT VOID         **Buffer,\r
-  IN UINTN            BufferSize\r
-  )\r
-{\r
-  BOOLEAN         TryAgain;\r
-\r
-  TryAgain = FALSE;\r
-  //\r
-  // If this is an initial request, buffer will be null with a new buffer size\r
-  //\r
-  if (*Buffer == NULL) {\r
-    *Status = EFI_BUFFER_TOO_SMALL;\r
-  }\r
-\r
-  if (BufferSize == 0) {\r
-    return TRUE;\r
-  }\r
-\r
-  //\r
-  // If the status code is "buffer too small", resize the buffer\r
-  //\r
-  if (*Status == EFI_BUFFER_TOO_SMALL) {\r
-    if (*Buffer != NULL) {\r
-      CoreFreePool (*Buffer);\r
-    }\r
-\r
-    *Buffer = CoreAllocateBootServicesPool (BufferSize);\r
-    if (*Buffer != NULL) {\r
-      TryAgain = TRUE;\r
-    } else {\r
-      *Status = EFI_OUT_OF_RESOURCES;\r
-    }\r
-  }\r
-\r
-  //\r
-  // If there's an error, free the buffer\r
-  //\r
-  if ((!TryAgain) && (EFI_ERROR (*Status)) && (*Buffer != NULL)) {\r
-    CoreFreePool (*Buffer);\r
-    *Buffer = NULL;\r
-  }\r
-\r
-  return TryAgain;\r
-}\r
-\r
-\r