]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Image/Image.c
MdeModulePkg: Refine casting expression result to bigger size
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Image / Image.c
index 33686a66c4c168c551dff7ecfb405e54106f7f28..80128e729b43112548acd57793de12327c529800 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Core image handling services to load and unload PeImage.\r
 \r
-Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<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
@@ -203,6 +203,8 @@ CoreInitializeImageServices (
                );\r
   }\r
 \r
+  ProtectUefiImage (&Image->Info, Image->LoadedImageDevicePath);\r
+\r
   return Status;\r
 }\r
 \r
@@ -258,11 +260,11 @@ CoreReadImageFile (
   return EFI_SUCCESS;\r
 }\r
 /**\r
-  To check memory usage bit map arry to figure out if the memory range the image will be loaded in is available or not. If \r
-  memory range is avaliable, the function will mark the correponding bits to 1 which indicates the memory range is used.\r
+  To check memory usage bit map array to figure out if the memory range the image will be loaded in is available or not. If\r
+  memory range is available, the function will mark the corresponding bits to 1 which indicates the memory range is used.\r
   The function is only invoked when load modules at fixed address feature is enabled. \r
   \r
-  @param  ImageBase                The base addres the image will be loaded at.\r
+  @param  ImageBase                The base address the image will be loaded at.\r
   @param  ImageSize                The size of the image\r
   \r
   @retval EFI_SUCCESS              The memory range the image will be loaded in is available\r
@@ -311,8 +313,8 @@ CheckAndMarkFixLoadingMemoryUsageBitMap (
    //\r
    // Test if the memory is avalaible or not.\r
    // \r
-   BaseOffsetPageNumber = (UINTN)EFI_SIZE_TO_PAGES((UINT32)(ImageBase - DxeCodeBase));\r
-   TopOffsetPageNumber  = (UINTN)EFI_SIZE_TO_PAGES((UINT32)(ImageBase + ImageSize - DxeCodeBase));\r
+   BaseOffsetPageNumber = EFI_SIZE_TO_PAGES((UINT32)(ImageBase - DxeCodeBase));\r
+   TopOffsetPageNumber  = EFI_SIZE_TO_PAGES((UINT32)(ImageBase + ImageSize - DxeCodeBase));\r
    for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {\r
      if ((mDxeCodeMemoryRangeUsageBitMap[Index / 64] & LShiftU64(1, (Index % 64))) != 0) {\r
        //\r
@@ -332,13 +334,13 @@ CheckAndMarkFixLoadingMemoryUsageBitMap (
 }\r
 /**\r
 \r
-  Get the fixed loadding address from image header assigned by build tool. This function only be called\r
+  Get the fixed loading address from image header assigned by build tool. This function only be called\r
   when Loading module at Fixed address feature enabled.\r
 \r
   @param  ImageContext              Pointer to the image context structure that describes the PE/COFF\r
                                     image that needs to be examined by this function.\r
   @retval EFI_SUCCESS               An fixed loading address is assigned to this image by build tools .\r
-  @retval EFI_NOT_FOUND             The image has no assigned fixed loadding address.\r
+  @retval EFI_NOT_FOUND             The image has no assigned fixed loading address.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -364,12 +366,10 @@ GetPeCoffImageFixLoadingAssignedAddress(
    //\r
    Handle = (IMAGE_FILE_HANDLE*)ImageContext->Handle;\r
    ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )Handle->Source + ImageContext->PeCoffHeaderOffset);\r
-   SectionHeaderOffset = (UINTN)(\r
-                                 ImageContext->PeCoffHeaderOffset +\r
-                                 sizeof (UINT32) +\r
-                                 sizeof (EFI_IMAGE_FILE_HEADER) +\r
-                                 ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader\r
-                                 );\r
+   SectionHeaderOffset = ImageContext->PeCoffHeaderOffset +\r
+                         sizeof (UINT32) +\r
+                         sizeof (EFI_IMAGE_FILE_HEADER) +\r
+                         ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader;\r
    NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;\r
 \r
    //\r
@@ -400,7 +400,7 @@ GetPeCoffImageFixLoadingAssignedAddress(
        // Build tool will save the address in PointerToRelocations & PointerToLineNumbers fields in the first section header\r
        // that doesn't point to code section in image header, as well as ImageBase field of image header. And there is an \r
        // assumption that when the feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations  \r
-       // & PointerToLineNumbers fields should NOT be Zero, or else, these 2 fileds should be set to Zero\r
+       // & PointerToLineNumbers fields should NOT be Zero, or else, these 2 fields should be set to Zero\r
        //\r
        ValueInSectionHeader = ReadUnaligned64((UINT64*)&SectionHeader.PointerToRelocations);\r
        if (ValueInSectionHeader != 0) {\r
@@ -413,7 +413,7 @@ GetPeCoffImageFixLoadingAssignedAddress(
                 ImageContext->ImageAddress = gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress + (INT64)(INTN)ImageContext->ImageAddress;\r
          }\r
          //\r
-         // Check if the memory range is avaliable.\r
+         // Check if the memory range is available.\r
          //\r
          Status = CheckAndMarkFixLoadingMemoryUsageBitMap (ImageContext->ImageAddress, (UINTN)(ImageContext->ImageSize + ImageContext->SectionAlignment));\r
        }\r
@@ -507,7 +507,7 @@ CoreLoadPeImage (
   }\r
 \r
   //\r
-  // Allocate memory of the correct memory type aligned on the required image boundry\r
+  // Allocate memory of the correct memory type aligned on the required image boundary\r
   //\r
   DstBufAlocated = FALSE;\r
   if (DstBuffer == 0) {\r
@@ -858,6 +858,12 @@ CoreUnloadAndCloseImage (
   HandleBuffer = NULL;\r
   ProtocolGuidArray = NULL;\r
 \r
+  if (Image->Started) {\r
+    UnregisterMemoryProfileImage (Image);\r
+  }\r
+\r
+  UnprotectUefiImage (&Image->Info, Image->LoadedImageDevicePath);\r
+\r
   if (Image->Ebc != NULL) {\r
     //\r
     // If EBC protocol exists we must perform cleanups for this image.\r
@@ -1051,8 +1057,11 @@ CoreLoadImageCommon (
   UINT32                     AuthenticationStatus;\r
   EFI_DEVICE_PATH_PROTOCOL   *OriginalFilePath;\r
   EFI_DEVICE_PATH_PROTOCOL   *HandleFilePath;\r
+  EFI_DEVICE_PATH_PROTOCOL   *InputFilePath;\r
+  EFI_DEVICE_PATH_PROTOCOL   *Node;\r
   UINTN                      FilePathSize;\r
   BOOLEAN                    ImageIsFromFv;\r
+  BOOLEAN                    ImageIsFromLoadFile;\r
 \r
   SecurityStatus = EFI_SUCCESS;\r
 \r
@@ -1075,11 +1084,13 @@ CoreLoadImageCommon (
   ZeroMem (&FHand, sizeof (IMAGE_FILE_HANDLE));\r
   FHand.Signature  = IMAGE_FILE_HANDLE_SIGNATURE;\r
   OriginalFilePath = FilePath;\r
+  InputFilePath    = FilePath;\r
   HandleFilePath   = FilePath;\r
   DeviceHandle     = NULL;\r
   Status           = EFI_SUCCESS;\r
   AuthenticationStatus = 0;\r
-  ImageIsFromFv    = FALSE;\r
+  ImageIsFromFv        = FALSE;\r
+  ImageIsFromLoadFile  = FALSE;\r
 \r
   //\r
   // If the caller passed a copy of the file, then just use it\r
@@ -1100,6 +1111,33 @@ CoreLoadImageCommon (
     if (FilePath == NULL) {\r
       return EFI_INVALID_PARAMETER;\r
     }\r
+\r
+    //\r
+    // Try to get the image device handle by checking the match protocol.\r
+    //\r
+    Node   = NULL;\r
+    Status = CoreLocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &HandleFilePath, &DeviceHandle);\r
+    if (!EFI_ERROR (Status)) {\r
+      ImageIsFromFv = TRUE;\r
+    } else {\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
+          if (!EFI_ERROR (Status)) {\r
+            ImageIsFromLoadFile = TRUE;\r
+            Node = HandleFilePath;\r
+          }\r
+        }\r
+      }\r
+    }\r
+\r
     //\r
     // Get the source file buffer by its device path.\r
     //\r
@@ -1112,26 +1150,12 @@ CoreLoadImageCommon (
     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
-        ImageIsFromFv = TRUE;\r
-      } else {\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
+      if (ImageIsFromLoadFile) {\r
+        //\r
+        // LoadFile () may cause the device path of the Handle be updated.\r
+        //\r
+        OriginalFilePath = AppendDevicePath (DevicePathFromHandle (DeviceHandle), Node);\r
       }\r
     }\r
   }\r
@@ -1319,6 +1343,7 @@ CoreLoadImageCommon (
       goto Done;\r
     }\r
   }\r
+  ProtectUefiImage (&Image->Info, Image->LoadedImageDevicePath);\r
 \r
   //\r
   // Success.  Return the image handle\r
@@ -1333,6 +1358,9 @@ Done:
   if (FHand.FreeBuffer) {\r
     CoreFreePool (FHand.Source);\r
   }\r
+  if (OriginalFilePath != InputFilePath) {\r
+    CoreFreePool (OriginalFilePath);\r
+  }\r
 \r
   //\r
   // There was an error.  If there's an Image structure, free it\r
@@ -1832,7 +1860,7 @@ Done:
                                   unloaded.\r
 \r
   @retval EFI_SUCCESS             The image has been unloaded.\r
-  @retval EFI_UNSUPPORTED         The image has been sarted, and does not support\r
+  @retval EFI_UNSUPPORTED         The image has been started, and does not support\r
                                   unload.\r
   @retval EFI_INVALID_PARAMPETER  ImageHandle is not a valid image handle.\r
 \r
@@ -1854,7 +1882,6 @@ CoreUnloadImage (
     Status = EFI_INVALID_PARAMETER;\r
     goto Done;\r
   }\r
-  UnregisterMemoryProfileImage (Image);\r
 \r
   if (Image->Started) {\r
     //\r