X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=MdeModulePkg%2FCore%2FDxe%2FImage%2FImage.c;h=652da8bf1075031986192eac214ba2316724ee3e;hb=95ba3d92dca2616715e2af89d2bbeca9577a3e2c;hp=a3cffcefc18622da2027f436aade49a32d3c9790;hpb=bc2dfdbcfc11dc785f0cc0ad2f519a63b98f88bc;p=mirror_edk2.git diff --git a/MdeModulePkg/Core/Dxe/Image/Image.c b/MdeModulePkg/Core/Dxe/Image/Image.c index a3cffcefc1..652da8bf10 100644 --- a/MdeModulePkg/Core/Dxe/Image/Image.c +++ b/MdeModulePkg/Core/Dxe/Image/Image.c @@ -1,7 +1,7 @@ /** @file Core image handling services to load and unload PeImage. -Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.
+Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -203,6 +203,8 @@ CoreInitializeImageServices ( ); } + ProtectUefiImage (&Image->Info, Image->LoadedImageDevicePath); + return Status; } @@ -258,11 +260,11 @@ CoreReadImageFile ( return EFI_SUCCESS; } /** - 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 - memory range is avaliable, the function will mark the correponding bits to 1 which indicates the memory range is used. + 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 + memory range is available, the function will mark the corresponding bits to 1 which indicates the memory range is used. The function is only invoked when load modules at fixed address feature is enabled. - @param ImageBase The base addres the image will be loaded at. + @param ImageBase The base address the image will be loaded at. @param ImageSize The size of the image @retval EFI_SUCCESS The memory range the image will be loaded in is available @@ -332,13 +334,13 @@ CheckAndMarkFixLoadingMemoryUsageBitMap ( } /** - Get the fixed loadding address from image header assigned by build tool. This function only be called + Get the fixed loading address from image header assigned by build tool. This function only be called when Loading module at Fixed address feature enabled. @param ImageContext Pointer to the image context structure that describes the PE/COFF image that needs to be examined by this function. @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools . - @retval EFI_NOT_FOUND The image has no assigned fixed loadding address. + @retval EFI_NOT_FOUND The image has no assigned fixed loading address. **/ EFI_STATUS @@ -389,7 +391,10 @@ GetPeCoffImageFixLoadingAssignedAddress( if (EFI_ERROR (Status)) { return Status; } - + if (Size != sizeof (EFI_IMAGE_SECTION_HEADER)) { + return EFI_NOT_FOUND; + } + Status = EFI_NOT_FOUND; if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) { @@ -397,7 +402,7 @@ GetPeCoffImageFixLoadingAssignedAddress( // Build tool will save the address in PointerToRelocations & PointerToLineNumbers fields in the first section header // that doesn't point to code section in image header, as well as ImageBase field of image header. And there is an // assumption that when the feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations - // & PointerToLineNumbers fields should NOT be Zero, or else, these 2 fileds should be set to Zero + // & PointerToLineNumbers fields should NOT be Zero, or else, these 2 fields should be set to Zero // ValueInSectionHeader = ReadUnaligned64((UINT64*)&SectionHeader.PointerToRelocations); if (ValueInSectionHeader != 0) { @@ -410,7 +415,7 @@ GetPeCoffImageFixLoadingAssignedAddress( ImageContext->ImageAddress = gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress + (INT64)(INTN)ImageContext->ImageAddress; } // - // Check if the memory range is avaliable. + // Check if the memory range is available. // Status = CheckAndMarkFixLoadingMemoryUsageBitMap (ImageContext->ImageAddress, (UINTN)(ImageContext->ImageSize + ImageContext->SectionAlignment)); } @@ -504,7 +509,7 @@ CoreLoadPeImage ( } // - // Allocate memory of the correct memory type aligned on the required image boundry + // Allocate memory of the correct memory type aligned on the required image boundary // DstBufAlocated = FALSE; if (DstBuffer == 0) { @@ -707,6 +712,7 @@ CoreLoadPeImage ( Image->RuntimeData->RelocationData = Image->ImageContext.FixupData; Image->RuntimeData->Handle = Image->Handle; InsertTailList (&gRuntime->ImageHead, &Image->RuntimeData->Link); + InsertImageRecord (Image->RuntimeData); } } @@ -851,6 +857,15 @@ CoreUnloadAndCloseImage ( UINTN OpenInfoCount; UINTN OpenInfoIndex; + HandleBuffer = NULL; + ProtocolGuidArray = NULL; + + if (Image->Started) { + UnregisterMemoryProfileImage (Image); + } + + UnprotectUefiImage (&Image->Info, Image->LoadedImageDevicePath); + if (Image->Ebc != NULL) { // // If EBC protocol exists we must perform cleanups for this image. @@ -946,6 +961,7 @@ CoreUnloadAndCloseImage ( // Remove the Image from the Runtime Image list as we are about to Free it! // RemoveEntryList (&Image->RuntimeData->Link); + RemoveImageRecord (Image->RuntimeData); } CoreFreePool (Image->RuntimeData); } @@ -1043,8 +1059,11 @@ CoreLoadImageCommon ( UINT32 AuthenticationStatus; EFI_DEVICE_PATH_PROTOCOL *OriginalFilePath; EFI_DEVICE_PATH_PROTOCOL *HandleFilePath; + EFI_DEVICE_PATH_PROTOCOL *InputFilePath; + EFI_DEVICE_PATH_PROTOCOL *Node; UINTN FilePathSize; BOOLEAN ImageIsFromFv; + BOOLEAN ImageIsFromLoadFile; SecurityStatus = EFI_SUCCESS; @@ -1067,11 +1086,13 @@ CoreLoadImageCommon ( ZeroMem (&FHand, sizeof (IMAGE_FILE_HANDLE)); FHand.Signature = IMAGE_FILE_HANDLE_SIGNATURE; OriginalFilePath = FilePath; + InputFilePath = FilePath; HandleFilePath = FilePath; DeviceHandle = NULL; Status = EFI_SUCCESS; AuthenticationStatus = 0; - ImageIsFromFv = FALSE; + ImageIsFromFv = FALSE; + ImageIsFromLoadFile = FALSE; // // If the caller passed a copy of the file, then just use it @@ -1079,7 +1100,10 @@ CoreLoadImageCommon ( if (SourceBuffer != NULL) { FHand.Source = SourceBuffer; FHand.SourceSize = SourceSize; - CoreLocateDevicePath (&gEfiDevicePathProtocolGuid, &HandleFilePath, &DeviceHandle); + Status = CoreLocateDevicePath (&gEfiDevicePathProtocolGuid, &HandleFilePath, &DeviceHandle); + if (EFI_ERROR (Status)) { + DeviceHandle = NULL; + } if (SourceSize > 0) { Status = EFI_SUCCESS; } else { @@ -1089,6 +1113,33 @@ CoreLoadImageCommon ( if (FilePath == NULL) { return EFI_INVALID_PARAMETER; } + + // + // Try to get the image device handle by checking the match protocol. + // + Node = NULL; + Status = CoreLocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &HandleFilePath, &DeviceHandle); + if (!EFI_ERROR (Status)) { + ImageIsFromFv = TRUE; + } else { + HandleFilePath = FilePath; + Status = CoreLocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &HandleFilePath, &DeviceHandle); + if (EFI_ERROR (Status)) { + if (!BootPolicy) { + HandleFilePath = FilePath; + Status = CoreLocateDevicePath (&gEfiLoadFile2ProtocolGuid, &HandleFilePath, &DeviceHandle); + } + if (EFI_ERROR (Status)) { + HandleFilePath = FilePath; + Status = CoreLocateDevicePath (&gEfiLoadFileProtocolGuid, &HandleFilePath, &DeviceHandle); + if (!EFI_ERROR (Status)) { + ImageIsFromLoadFile = TRUE; + Node = HandleFilePath; + } + } + } + } + // // Get the source file buffer by its device path. // @@ -1101,35 +1152,19 @@ CoreLoadImageCommon ( if (FHand.Source == NULL) { Status = EFI_NOT_FOUND; } else { - // - // Try to get the image device handle by checking the match protocol. - // FHand.FreeBuffer = TRUE; - Status = CoreLocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &HandleFilePath, &DeviceHandle); - if (!EFI_ERROR (Status)) { - ImageIsFromFv = TRUE; - } else { - HandleFilePath = FilePath; - Status = CoreLocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &HandleFilePath, &DeviceHandle); - if (EFI_ERROR (Status)) { - if (!BootPolicy) { - HandleFilePath = FilePath; - Status = CoreLocateDevicePath (&gEfiLoadFile2ProtocolGuid, &HandleFilePath, &DeviceHandle); - } - if (EFI_ERROR (Status)) { - HandleFilePath = FilePath; - Status = CoreLocateDevicePath (&gEfiLoadFileProtocolGuid, &HandleFilePath, &DeviceHandle); - } - } + if (ImageIsFromLoadFile) { + // + // LoadFile () may cause the device path of the Handle be updated. + // + OriginalFilePath = AppendDevicePath (DevicePathFromHandle (DeviceHandle), Node); } } } - if (Status == EFI_ALREADY_STARTED) { + if (EFI_ERROR (Status)) { Image = NULL; goto Done; - } else if (EFI_ERROR (Status)) { - return Status; } if (gSecurity2 != NULL) { @@ -1191,7 +1226,8 @@ CoreLoadImageCommon ( // Image = AllocateZeroPool (sizeof(LOADED_IMAGE_PRIVATE_DATA)); if (Image == NULL) { - return EFI_OUT_OF_RESOURCES; + Status = EFI_OUT_OF_RESOURCES; + goto Done; } // @@ -1309,6 +1345,7 @@ CoreLoadImageCommon ( goto Done; } } + ProtectUefiImage (&Image->Info, Image->LoadedImageDevicePath); // // Success. Return the image handle @@ -1323,6 +1360,9 @@ Done: if (FHand.FreeBuffer) { CoreFreePool (FHand.Source); } + if (OriginalFilePath != InputFilePath) { + CoreFreePool (OriginalFilePath); + } // // There was an error. If there's an Image structure, free it @@ -1618,6 +1658,7 @@ CoreStartImage ( // Subsequent calls to LongJump() cause a non-zero value to be returned by SetJump(). // if (SetJumpFlag == 0) { + RegisterMemoryProfileImage (Image, (Image->ImageContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION ? EFI_FV_FILETYPE_APPLICATION : EFI_FV_FILETYPE_DRIVER)); // // Call the image's entry point // @@ -1821,7 +1862,7 @@ Done: unloaded. @retval EFI_SUCCESS The image has been unloaded. - @retval EFI_UNSUPPORTED The image has been sarted, and does not support + @retval EFI_UNSUPPORTED The image has been started, and does not support unload. @retval EFI_INVALID_PARAMPETER ImageHandle is not a valid image handle.