]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Image/Image.c
Produce LoadPeImage protocol only in framework compatibility mode.
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Image / Image.c
index 27dd610ccbc3ea81988e310d9e1f2919cb528015..7e5cea7b76ad2d27a861e59f52d51c8f72febb4f 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Core image handling services to load and unload PeImage.\r
 \r
-Copyright (c) 2006 - 2008, Intel Corporation. <BR>\r
+Copyright (c) 2006 - 2009, Intel Corporation. <BR>\r
 All rights reserved. This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -144,17 +144,64 @@ CoreInitializeImageServices (
   gDxeCoreImageHandle = Image->Handle;\r
   gDxeCoreLoadedImage = &Image->Info;\r
 \r
+  if (FeaturePcdGet (PcdFrameworkCompatibilitySupport)) {\r
+    //\r
+    // Export DXE Core PE Loader functionality for backward compatibility.\r
+    //\r
+    Status = CoreInstallProtocolInterface (\r
+      &mLoadPe32PrivateData.Handle,\r
+      &gEfiLoadPeImageProtocolGuid,\r
+      EFI_NATIVE_INTERFACE,\r
+      &mLoadPe32PrivateData.Pe32Image\r
+      );\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Read image file (specified by UserHandle) into user specified buffer with specified offset\r
+  and length.\r
+\r
+  @param  UserHandle             Image file handle\r
+  @param  Offset                 Offset to the source file\r
+  @param  ReadSize               For input, pointer of size to read; For output,\r
+                                 pointer of size actually read.\r
+  @param  Buffer                 Buffer to write into\r
+\r
+  @retval EFI_SUCCESS            Successfully read the specified part of file\r
+                                 into buffer.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+CoreReadImageFile (\r
+  IN     VOID    *UserHandle,\r
+  IN     UINTN   Offset,\r
+  IN OUT UINTN   *ReadSize,\r
+  OUT    VOID    *Buffer\r
+  )\r
+{\r
+  UINTN               EndPosition;\r
+  IMAGE_FILE_HANDLE  *FHand;\r
+\r
+  FHand = (IMAGE_FILE_HANDLE  *)UserHandle;\r
+  ASSERT (FHand->Signature == IMAGE_FILE_HANDLE_SIGNATURE);\r
+\r
   //\r
-  // Export DXE Core PE Loader functionality\r
+  // Move data from our local copy of the file\r
   //\r
-  return CoreInstallProtocolInterface (\r
-           &mLoadPe32PrivateData.Handle,\r
-           &gEfiLoadPeImageProtocolGuid,\r
-           EFI_NATIVE_INTERFACE,\r
-           &mLoadPe32PrivateData.Pe32Image\r
-           );\r
-}\r
+  EndPosition = Offset + *ReadSize;\r
+  if (EndPosition > FHand->SourceSize) {\r
+    *ReadSize = (UINT32)(FHand->SourceSize - Offset);\r
+  }\r
+  if (Offset >= FHand->SourceSize) {\r
+      *ReadSize = 0;\r
+  }\r
 \r
+  CopyMem (Buffer, (CHAR8 *)FHand->Source + Offset, *ReadSize);\r
+  return EFI_SUCCESS;\r
+}\r
 \r
 /**\r
   Loads, relocates, and invokes a PE/COFF image\r
@@ -190,7 +237,6 @@ CoreLoadPeImage (
   EFI_STATUS                Status;\r
   BOOLEAN                   DstBufAlocated;\r
   UINTN                     Size;\r
-  UINTN                     LinkTimeBase;\r
 \r
   ZeroMem (&Image->ImageContext, sizeof (Image->ImageContext));\r
 \r
@@ -642,6 +688,14 @@ CoreUnloadAndCloseImage (
                &Image->Info\r
                );\r
 \r
+    if (Image->ImageContext.HiiResourceData != 0) {\r
+      Status = CoreUninstallProtocolInterface (\r
+                 Image->Handle,\r
+                 &gEfiHiiPackageListProtocolGuid,\r
+                 (VOID *) (UINTN) Image->ImageContext.HiiResourceData\r
+                 );\r
+    }\r
+\r
   }\r
 \r
   if (Image->RuntimeData != NULL) {\r
@@ -759,19 +813,63 @@ CoreLoadImageCommon (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  //\r
-  // Get simple read access to the source file\r
-  //\r
+  ZeroMem (&FHand, sizeof (IMAGE_FILE_HANDLE));\r
+  FHand.Signature  = IMAGE_FILE_HANDLE_SIGNATURE;\r
   OriginalFilePath = FilePath;\r
-  Status = CoreOpenImageFile (\r
-             BootPolicy,\r
-             SourceBuffer,\r
-             SourceSize,\r
-             &FilePath,\r
-             &DeviceHandle,\r
-             &FHand,\r
-             &AuthenticationStatus\r
-             );\r
+  HandleFilePath   = FilePath;\r
+  DeviceHandle     = NULL;\r
+  Status           = EFI_SUCCESS;\r
+  AuthenticationStatus = 0;\r
+  //\r
+  // If the caller passed a copy of the file, then just use it\r
+  //\r
+  if (SourceBuffer != NULL) {\r
+    FHand.Source     = SourceBuffer;\r
+    FHand.SourceSize = SourceSize;\r
+    CoreLocateDevicePath (&gEfiDevicePathProtocolGuid, &HandleFilePath, &DeviceHandle);\r
+    if (SourceSize > 0) {\r
+      Status = EFI_SUCCESS;\r
+    } else {\r
+      Status = EFI_LOAD_ERROR;\r
+    }\r
+  } else {\r
+    if (FilePath == NULL) {\r
+      return EFI_INVALID_PARAMETER;\r
+    }\r
+    //\r
+    // Get the source file buffer by its device path.\r
+    //\r
+    FHand.Source = GetFileBufferByFilePath (\r
+                      BootPolicy, \r
+                      FilePath,\r
+                      &FHand.SourceSize,\r
+                      &AuthenticationStatus\r
+                      );\r
+    if (FHand.Source == NULL) {\r
+      Status = EFI_NOT_FOUND;\r
+    } else {\r
+      //\r
+      // Try to get the image device handle by checking the match protocol.\r
+      //\r
+      FHand.FreeBuffer = TRUE;\r
+      Status = CoreLocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &HandleFilePath, &DeviceHandle);\r
+      if (EFI_ERROR (Status)) {\r
+        HandleFilePath = FilePath;\r
+        Status = CoreLocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &HandleFilePath, &DeviceHandle);\r
+        if (EFI_ERROR (Status)) {\r
+          if (!BootPolicy) {\r
+            HandleFilePath = FilePath;\r
+            Status = CoreLocateDevicePath (&gEfiLoadFile2ProtocolGuid, &HandleFilePath, &DeviceHandle);\r
+          }\r
+          if (EFI_ERROR (Status)) {\r
+            HandleFilePath = FilePath;\r
+            Status = CoreLocateDevicePath (&gEfiLoadFileProtocolGuid, &HandleFilePath, &DeviceHandle);\r
+          }\r
+        }\r
+      }\r
+    }\r
+  }\r
+\r
   if (Status == EFI_ALREADY_STARTED) {\r
     Image = NULL;\r
     goto Done;\r
@@ -904,6 +1002,21 @@ CoreLoadImageCommon (
     goto Done;\r
   }\r
 \r
+  //\r
+  // Install HII Package List Protocol onto the image handle\r
+  //\r
+  if (Image->ImageContext.HiiResourceData != 0) {\r
+    Status = CoreInstallProtocolInterface (\r
+               &Image->Handle,\r
+               &gEfiHiiPackageListProtocolGuid,\r
+               EFI_NATIVE_INTERFACE,\r
+               (VOID *) (UINTN) Image->ImageContext.HiiResourceData\r
+               );\r
+    if (EFI_ERROR (Status)) {\r
+      goto Done;\r
+    }\r
+  }\r
+\r
   //\r
   // Success.  Return the image handle\r
   //\r
@@ -1098,6 +1211,14 @@ CoreStartImage (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  //\r
+  // The image to be started must have the machine type supported by DxeCore.\r
+  //\r
+  ASSERT (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image->Machine));\r
+  if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image->Machine)) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
   //\r
   // Don't profile Objects or invalid start requests\r
   //\r