X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=MdeModulePkg%2FCore%2FDxe%2FImage%2FImage.c;h=eadcae488c3242e24c13a47632b7c214c3b5fb38;hb=775455633400aaba9161b5bb97b48f0df059bd88;hp=7c66f07d760dc4f56f2b0c9797504b75a9d08e12;hpb=162ed594438ab8d39f89b43e6d645ca24e1e1e65;p=mirror_edk2.git diff --git a/MdeModulePkg/Core/Dxe/Image/Image.c b/MdeModulePkg/Core/Dxe/Image/Image.c index 7c66f07d76..eadcae488c 100644 --- a/MdeModulePkg/Core/Dxe/Image/Image.c +++ b/MdeModulePkg/Core/Dxe/Image/Image.c @@ -1,9 +1,8 @@ -/** @file - +/** @file Core image handling services to load and unload PeImage. -Copyright (c) 2006 - 2008, Intel Corporation -All rights reserved. This program and the accompanying materials +Copyright (c) 2006 - 2010, 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 http://opensource.org/licenses/bsd-license.php @@ -13,11 +12,12 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. **/ -#include +#include "DxeMain.h" +#include "Image.h" + // // Module Globals // - LOADED_IMAGE_PRIVATE_DATA *mCurrentImage = NULL; LOAD_PE32_IMAGE_PRIVATE_DATA mLoadPe32PrivateData = { @@ -70,14 +70,18 @@ LOADED_IMAGE_PRIVATE_DATA mCorePrivateImage = { NULL, // RuntimeData NULL // LoadedImageDevicePath }; - - +// +// The field is define for Loading modules at fixed address feature to tracker the PEI code +// memory range usage. It is a bit mapped array in which every bit indicates the correspoding memory page +// available or not. +// +GLOBAL_REMOVE_IF_UNREFERENCED UINT64 *mDxeCodeMemoryRangeUsageBitMap=NULL; /** Add the Image Services to EFI Boot Services Table and install the protocol interfaces for this image. - @param HobStart The HOB to initialize + @param HobStart The HOB to initialize @return Status code. @@ -93,6 +97,8 @@ CoreInitializeImageServices ( UINT64 DxeCoreImageLength; VOID *DxeCoreEntryPoint; EFI_PEI_HOB_POINTERS DxeCoreHob; + PE_COFF_LOADER_IMAGE_CONTEXT ImageContext; + // // Searching for image hob // @@ -112,6 +118,14 @@ CoreInitializeImageServices ( DxeCoreImageLength = DxeCoreHob.MemoryAllocationModule->MemoryAllocationHeader.MemoryLength; DxeCoreEntryPoint = (VOID *) (UINTN) DxeCoreHob.MemoryAllocationModule->EntryPoint; gDxeCoreFileName = &DxeCoreHob.MemoryAllocationModule->ModuleName; + + // + // Report DXE Core image information to the PE/COFF Extra Action Library + // + ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)DxeCoreImageBaseAddress; + ImageContext.PdbPointer = PeCoffLoaderGetPdbPointer ((VOID*) (UINTN) ImageContext.ImageAddress); + PeCoffLoaderRelocateImageExtraAction (&ImageContext); + // // Initialize the fields for an internal driver // @@ -144,42 +158,252 @@ CoreInitializeImageServices ( gDxeCoreImageHandle = Image->Handle; gDxeCoreLoadedImage = &Image->Info; + if (FeaturePcdGet (PcdFrameworkCompatibilitySupport)) { + // + // Export DXE Core PE Loader functionality for backward compatibility. + // + Status = CoreInstallProtocolInterface ( + &mLoadPe32PrivateData.Handle, + &gEfiLoadPeImageProtocolGuid, + EFI_NATIVE_INTERFACE, + &mLoadPe32PrivateData.Pe32Image + ); + } + + return Status; +} + +/** + Read image file (specified by UserHandle) into user specified buffer with specified offset + and length. + + @param UserHandle Image file handle + @param Offset Offset to the source file + @param ReadSize For input, pointer of size to read; For output, + pointer of size actually read. + @param Buffer Buffer to write into + + @retval EFI_SUCCESS Successfully read the specified part of file + into buffer. + +**/ +EFI_STATUS +EFIAPI +CoreReadImageFile ( + IN VOID *UserHandle, + IN UINTN Offset, + IN OUT UINTN *ReadSize, + OUT VOID *Buffer + ) +{ + UINTN EndPosition; + IMAGE_FILE_HANDLE *FHand; + + FHand = (IMAGE_FILE_HANDLE *)UserHandle; + ASSERT (FHand->Signature == IMAGE_FILE_HANDLE_SIGNATURE); + // - // Export DXE Core PE Loader functionality + // Move data from our local copy of the file // - return CoreInstallProtocolInterface ( - &mLoadPe32PrivateData.Handle, - &gEfiLoadPeImageProtocolGuid, - EFI_NATIVE_INTERFACE, - &mLoadPe32PrivateData.Pe32Image - ); + EndPosition = Offset + *ReadSize; + if (EndPosition > FHand->SourceSize) { + *ReadSize = (UINT32)(FHand->SourceSize - Offset); + } + if (Offset >= FHand->SourceSize) { + *ReadSize = 0; + } + + CopyMem (Buffer, (CHAR8 *)FHand->Source + Offset, *ReadSize); + 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. + 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 ImageSize The size of the image + + @retval EFI_SUCCESS The memory range the image will be loaded in is available + @retval EFI_NOT_FOUND The memory range the image will be loaded in is not available +**/ +EFI_STATUS +CheckAndMarkFixLoadingMemoryUsageBitMap ( + IN EFI_PHYSICAL_ADDRESS ImageBase, + IN UINTN ImageSize + ) +{ + UINT32 DxeCodePageNumber; + UINT64 DxeCodeSize; + EFI_PHYSICAL_ADDRESS DxeCodeBase; + UINTN BaseOffsetPageNumber; + UINTN TopOffsetPageNumber; + UINTN Index; + // + // The DXE code range includes RuntimeCodePage range and Boot time code range. + // + DxeCodePageNumber = PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber); + DxeCodePageNumber += PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber); + DxeCodeSize = EFI_PAGES_TO_SIZE(DxeCodePageNumber); + DxeCodeBase = gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress - DxeCodeSize; + + // + // If the memory usage bit map is not initialized, do it. Every bit in the array + // indicate the status of the corresponding memory page, available or not + // + if (mDxeCodeMemoryRangeUsageBitMap == NULL) { + mDxeCodeMemoryRangeUsageBitMap = AllocateZeroPool(((DxeCodePageNumber/64) + 1)*sizeof(UINT64)); + } + // + // If the Dxe code memory range is not allocated or the bit map array allocation failed, return EFI_NOT_FOUND + // + if (!gLoadFixedAddressCodeMemoryReady || mDxeCodeMemoryRangeUsageBitMap == NULL) { + return EFI_NOT_FOUND; + } + // + // Test the memory range for loading the image in the DXE code range. + // + if (gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress < ImageBase + ImageSize || + DxeCodeBase > ImageBase) { + return EFI_NOT_FOUND; + } + // + // Test if the memory is avalaible or not. + // + BaseOffsetPageNumber = (UINTN)EFI_SIZE_TO_PAGES((UINT32)(ImageBase - DxeCodeBase)); + TopOffsetPageNumber = (UINTN)EFI_SIZE_TO_PAGES((UINT32)(ImageBase + ImageSize - DxeCodeBase)); + for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) { + if ((mDxeCodeMemoryRangeUsageBitMap[Index / 64] & LShiftU64(1, (Index % 64))) != 0) { + // + // This page is already used. + // + return EFI_NOT_FOUND; + } + } + + // + // Being here means the memory range is available. So mark the bits for the memory range + // + for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) { + mDxeCodeMemoryRangeUsageBitMap[Index / 64] |= LShiftU64(1, (Index % 64)); + } + return EFI_SUCCESS; } +/** + Get the fixed loadding 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. + +**/ +EFI_STATUS +GetPeCoffImageFixLoadingAssignedAddress( + IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext + ) +{ + UINTN SectionHeaderOffset; + EFI_STATUS Status; + EFI_IMAGE_SECTION_HEADER SectionHeader; + EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr; + UINT16 Index; + UINTN Size; + UINT16 NumberOfSections; + IMAGE_FILE_HANDLE *Handle; + UINT64 ValueInSectionHeader; + + + Status = EFI_NOT_FOUND; + + // + // Get PeHeader pointer + // + Handle = (IMAGE_FILE_HANDLE*)ImageContext->Handle; + ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )Handle->Source + ImageContext->PeCoffHeaderOffset); + SectionHeaderOffset = (UINTN)( + ImageContext->PeCoffHeaderOffset + + sizeof (UINT32) + + sizeof (EFI_IMAGE_FILE_HEADER) + + ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader + ); + NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections; + + // + // Get base address from the first section header that doesn't point to code section. + // + for (Index = 0; Index < NumberOfSections; Index++) { + // + // Read section header from file + // + Size = sizeof (EFI_IMAGE_SECTION_HEADER); + Status = ImageContext->ImageRead ( + ImageContext->Handle, + SectionHeaderOffset, + &Size, + &SectionHeader + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = EFI_NOT_FOUND; + + if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) { + // + // 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 + // + ValueInSectionHeader = ReadUnaligned64((UINT64*)&SectionHeader.PointerToRelocations); + if (ValueInSectionHeader != 0) { + // + // When the feature is configured as load module at fixed absolute address, the ImageAddress field of ImageContext + // hold the spcified address. If the feature is configured as load module at fixed offset, ImageAddress hold an offset + // relative to top address + // + if ((INT64)PcdGet64(PcdLoadModuleAtFixAddressEnable) < 0) { + ImageContext->ImageAddress = gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress + (INT64)(INTN)ImageContext->ImageAddress; + } + // + // Check if the memory range is avaliable. + // + Status = CheckAndMarkFixLoadingMemoryUsageBitMap (ImageContext->ImageAddress, (UINTN)(ImageContext->ImageSize + ImageContext->SectionAlignment)); + } + break; + } + SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER); + } + DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address 0x%11p. Status = %r \n", (VOID *)(UINTN)(ImageContext->ImageAddress), Status)); + return Status; +} /** Loads, relocates, and invokes a PE/COFF image - @param BootPolicy If TRUE, indicates that the request originates - from the boot manager, and that the boot - manager is attempting to load FilePath as a - boot selection. - @param Pe32Handle The handle of PE32 image - @param Image PE image to be loaded - @param DstBuffer The buffer to store the image - @param EntryPoint A pointer to the entry point - @param Attribute The bit mask of attributes to set for the load - PE image - - @retval EFI_SUCCESS The file was loaded, relocated, and invoked - @retval EFI_OUT_OF_RESOURCES There was not enough memory to load and - relocate the PE/COFF file - @retval EFI_INVALID_PARAMETER Invalid parameter + @param BootPolicy If TRUE, indicates that the request originates + from the boot manager, and that the boot + manager is attempting to load FilePath as a + boot selection. + @param Pe32Handle The handle of PE32 image + @param Image PE image to be loaded + @param DstBuffer The buffer to store the image + @param EntryPoint A pointer to the entry point + @param Attribute The bit mask of attributes to set for the load + PE image + + @retval EFI_SUCCESS The file was loaded, relocated, and invoked + @retval EFI_OUT_OF_RESOURCES There was not enough memory to load and + relocate the PE/COFF file + @retval EFI_INVALID_PARAMETER Invalid parameter @retval EFI_BUFFER_TOO_SMALL Buffer for image is too small **/ EFI_STATUS CoreLoadPeImage ( - IN BOOLEAN BootPolicy, + IN BOOLEAN BootPolicy, IN VOID *Pe32Handle, IN LOADED_IMAGE_PRIVATE_DATA *Image, IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL, @@ -190,11 +414,7 @@ CoreLoadPeImage ( EFI_STATUS Status; BOOLEAN DstBufAlocated; UINTN Size; - UINTN LinkTimeBase; - EFI_TCG_PLATFORM_PROTOCOL *TcgPlatformProtocol; - IMAGE_FILE_HANDLE *FHandle; - FHandle = NULL; ZeroMem (&Image->ImageContext, sizeof (Image->ImageContext)); Image->ImageContext.Handle = Pe32Handle; @@ -217,7 +437,7 @@ CoreLoadPeImage ( return EFI_UNSUPPORTED; } } - + // // Set EFI memory type based on ImageType // @@ -239,10 +459,6 @@ CoreLoadPeImage ( Image->ImageContext.ImageError = IMAGE_ERROR_INVALID_SUBSYSTEM; return EFI_UNSUPPORTED; } - // - // Get the image base address in the original PeImage. - // - LinkTimeBase = (UINTN) Image->ImageContext.ImageAddress; // // Allocate memory of the correct memory type aligned on the required image boundry @@ -269,21 +485,43 @@ CoreLoadPeImage ( // no modules whose preferred load addresses are below 1MB. // Status = EFI_OUT_OF_RESOURCES; - if (Image->ImageContext.ImageAddress >= 0x100000 || Image->ImageContext.RelocationsStripped) { - Status = CoreAllocatePages ( - AllocateAddress, - (EFI_MEMORY_TYPE) (Image->ImageContext.ImageCodeMemoryType), - Image->NumberOfPages, - &Image->ImageContext.ImageAddress - ); - } - if (EFI_ERROR (Status) && !Image->ImageContext.RelocationsStripped) { - Status = CoreAllocatePages ( - AllocateAnyPages, - (EFI_MEMORY_TYPE) (Image->ImageContext.ImageCodeMemoryType), - Image->NumberOfPages, - &Image->ImageContext.ImageAddress - ); + // + // If Loading Module At Fixed Address feature is enabled, the module should be loaded to + // a specified address. + // + if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0 ) { + Status = GetPeCoffImageFixLoadingAssignedAddress (&(Image->ImageContext)); + + if (EFI_ERROR (Status)) { + // + // If the code memory is not ready, invoke CoreAllocatePage with AllocateAnyPages to load the driver. + // + DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED ERROR: Loading module at fixed address failed since specified memory is not available.\n")); + + Status = CoreAllocatePages ( + AllocateAnyPages, + (EFI_MEMORY_TYPE) (Image->ImageContext.ImageCodeMemoryType), + Image->NumberOfPages, + &Image->ImageContext.ImageAddress + ); + } + } else { + if (Image->ImageContext.ImageAddress >= 0x100000 || Image->ImageContext.RelocationsStripped) { + Status = CoreAllocatePages ( + AllocateAddress, + (EFI_MEMORY_TYPE) (Image->ImageContext.ImageCodeMemoryType), + Image->NumberOfPages, + &Image->ImageContext.ImageAddress + ); + } + if (EFI_ERROR (Status) && !Image->ImageContext.RelocationsStripped) { + Status = CoreAllocatePages ( + AllocateAnyPages, + (EFI_MEMORY_TYPE) (Image->ImageContext.ImageCodeMemoryType), + Image->NumberOfPages, + &Image->ImageContext.ImageAddress + ); + } } if (EFI_ERROR (Status)) { return Status; @@ -315,9 +553,11 @@ CoreLoadPeImage ( } Image->ImageBasePage = Image->ImageContext.ImageAddress; - Image->ImageContext.ImageAddress = - (Image->ImageContext.ImageAddress + Image->ImageContext.SectionAlignment - 1) & - ~((UINTN)Image->ImageContext.SectionAlignment - 1); + if (!Image->ImageContext.IsTeImage) { + Image->ImageContext.ImageAddress = + (Image->ImageContext.ImageAddress + Image->ImageContext.SectionAlignment - 1) & + ~((UINTN)Image->ImageContext.SectionAlignment - 1); + } // // Load the image from the file into the allocated memory @@ -332,9 +572,9 @@ CoreLoadPeImage ( // is used to relocate the image when SetVirtualAddressMap() is called. The // relocation is done by the Runtime AP. // - if (Attribute & EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION) { + if ((Attribute & EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION) != 0) { if (Image->ImageContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER) { - Image->ImageContext.FixupData = CoreAllocateRuntimePool ((UINTN)(Image->ImageContext.FixupDataSize)); + Image->ImageContext.FixupData = AllocateRuntimePool ((UINTN)(Image->ImageContext.FixupDataSize)); if (Image->ImageContext.FixupData == NULL) { Status = EFI_OUT_OF_RESOURCES; goto Done; @@ -342,29 +582,6 @@ CoreLoadPeImage ( } } - // - // Measure the image before applying fixup - // - Status = CoreLocateProtocol ( - &gEfiTcgPlatformProtocolGuid, - NULL, - (VOID **) &TcgPlatformProtocol - ); - if (!EFI_ERROR (Status)) { - FHandle = (IMAGE_FILE_HANDLE *) Image->ImageContext.Handle; - Status = TcgPlatformProtocol->MeasurePeImage ( - BootPolicy, - (EFI_PHYSICAL_ADDRESS) (UINTN) FHandle->Source, - FHandle->SourceSize, - LinkTimeBase, - Image->ImageContext.ImageType, - Image->Info.DeviceHandle, - Image->Info.FilePath - ); - - ASSERT_EFI_ERROR (Status); - } - // // Relocate the image in memory // @@ -396,7 +613,8 @@ CoreLoadPeImage ( // Locate the EBC interpreter protocol // Status = CoreLocateProtocol (&gEfiEbcProtocolGuid, NULL, (VOID **)&Image->Ebc); - if (EFI_ERROR(Status)) { + if (EFI_ERROR(Status) || Image->Ebc == NULL) { + DEBUG ((DEBUG_LOAD | DEBUG_ERROR, "CoreLoadPeImage: There is no EBC interpreter for an EBC image.\n")); goto Done; } @@ -416,8 +634,8 @@ CoreLoadPeImage ( Status = Image->Ebc->CreateThunk ( Image->Ebc, Image->Handle, - (VOID *)(UINTN)Image->ImageContext.EntryPoint, - (VOID **)&Image->EntryPoint + (VOID *)(UINTN) Image->ImageContext.EntryPoint, + (VOID **) &Image->EntryPoint ); if (EFI_ERROR(Status)) { goto Done; @@ -432,12 +650,12 @@ CoreLoadPeImage ( Image->Info.ImageSize = Image->ImageContext.ImageSize; Image->Info.ImageCodeType = (EFI_MEMORY_TYPE) (Image->ImageContext.ImageCodeMemoryType); Image->Info.ImageDataType = (EFI_MEMORY_TYPE) (Image->ImageContext.ImageDataMemoryType); - if (Attribute & EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION) { + if ((Attribute & EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION) != 0) { if (Image->ImageContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER) { // // Make a list off all the RT images so we can let the RT AP know about them. // - Image->RuntimeData = CoreAllocateRuntimePool (sizeof(EFI_RUNTIME_IMAGE_ENTRY)); + Image->RuntimeData = AllocateRuntimePool (sizeof(EFI_RUNTIME_IMAGE_ENTRY)); if (Image->RuntimeData == NULL) { goto Done; } @@ -465,36 +683,31 @@ CoreLoadPeImage ( UINTN Index; UINTN StartIndex; CHAR8 EfiFileName[256]; - - if (Image->ImageContext.Machine != IMAGE_FILE_MACHINE_IA64) { - DEBUG ((DEBUG_INFO | DEBUG_LOAD, - "Loading driver at 0x%10p EntryPoint=0x%10p ", - (VOID *)(UINTN)Image->ImageContext.ImageAddress, - (VOID *)(UINTN)Image->ImageContext.EntryPoint)); - } else { - // - // For IPF Image, the real entry point should be print. - // - DEBUG ((DEBUG_INFO | DEBUG_LOAD, - "Loading driver at 0x%10p EntryPoint=0x%10p ", - (VOID *)(UINTN)Image->ImageContext.ImageAddress, - (VOID *)(UINTN)(*(UINT64 *)(UINTN)Image->ImageContext.EntryPoint))); - } - + + + DEBUG ((DEBUG_INFO | DEBUG_LOAD, + "Loading driver at 0x%11p EntryPoint=0x%11p ", + (VOID *)(UINTN) Image->ImageContext.ImageAddress, + FUNCTION_ENTRY_POINT (Image->ImageContext.EntryPoint))); + + // - // Print Module Name by Pdb file path + // Print Module Name by Pdb file path. + // Windows and Unix style file path are all trimmed correctly. // if (Image->ImageContext.PdbPointer != NULL) { StartIndex = 0; for (Index = 0; Image->ImageContext.PdbPointer[Index] != 0; Index++) { - if (Image->ImageContext.PdbPointer[Index] == '\\') { + if ((Image->ImageContext.PdbPointer[Index] == '\\') || (Image->ImageContext.PdbPointer[Index] == '/')) { StartIndex = Index + 1; } } // // Copy the PDB file name to our temporary string, and replace .pdb with .efi + // The PDB file name is limited in the range of 0~255. + // If the length is bigger than 255, trim the redudant characters to avoid overflow in array boundary. // - for (Index = 0; Index < sizeof (EfiFileName); Index++) { + for (Index = 0; Index < sizeof (EfiFileName) - 4; Index++) { EfiFileName[Index] = Image->ImageContext.PdbPointer[Index + StartIndex]; if (EfiFileName[Index] == 0) { EfiFileName[Index] = '.'; @@ -507,6 +720,10 @@ CoreLoadPeImage ( break; } } + + if (Index == sizeof (EfiFileName) - 4) { + EfiFileName[Index] = 0; + } DEBUG ((DEBUG_INFO | DEBUG_LOAD, "%a", EfiFileName)); // &Image->ImageContext.PdbPointer[StartIndex])); } DEBUG ((DEBUG_INFO | DEBUG_LOAD, "\n")); @@ -537,7 +754,7 @@ Done: /** Get the image's private data from its handle. - @param ImageHandle The image handle + @param ImageHandle The image handle @return Return the image private data associated with ImageHandle. @@ -559,7 +776,7 @@ CoreLoadedImageInfo ( if (!EFI_ERROR (Status)) { Image = LOADED_IMAGE_PRIVATE_DATA_FROM_THIS (LoadedImage); } else { - DEBUG ((DEBUG_LOAD, "CoreLoadedImageInfo: Not an ImageHandle %x\n", ImageHandle)); + DEBUG ((DEBUG_LOAD, "CoreLoadedImageInfo: Not an ImageHandle %p\n", ImageHandle)); Image = NULL; } @@ -567,43 +784,191 @@ CoreLoadedImageInfo ( } +/** + Unloads EFI image from memory. + + @param Image EFI image + @param FreePage Free allocated pages + +**/ +VOID +CoreUnloadAndCloseImage ( + IN LOADED_IMAGE_PRIVATE_DATA *Image, + IN BOOLEAN FreePage + ) +{ + EFI_STATUS Status; + UINTN HandleCount; + EFI_HANDLE *HandleBuffer; + UINTN HandleIndex; + EFI_GUID **ProtocolGuidArray; + UINTN ArrayCount; + UINTN ProtocolIndex; + EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfo; + UINTN OpenInfoCount; + UINTN OpenInfoIndex; + + if (Image->Ebc != NULL) { + // + // If EBC protocol exists we must perform cleanups for this image. + // + Image->Ebc->UnloadImage (Image->Ebc, Image->Handle); + } + + // + // Unload image, free Image->ImageContext->ModHandle + // + PeCoffLoaderUnloadImage (&Image->ImageContext); + + // + // Free our references to the image handle + // + if (Image->Handle != NULL) { + + Status = CoreLocateHandleBuffer ( + AllHandles, + NULL, + NULL, + &HandleCount, + &HandleBuffer + ); + if (!EFI_ERROR (Status)) { + for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) { + Status = CoreProtocolsPerHandle ( + HandleBuffer[HandleIndex], + &ProtocolGuidArray, + &ArrayCount + ); + if (!EFI_ERROR (Status)) { + for (ProtocolIndex = 0; ProtocolIndex < ArrayCount; ProtocolIndex++) { + Status = CoreOpenProtocolInformation ( + HandleBuffer[HandleIndex], + ProtocolGuidArray[ProtocolIndex], + &OpenInfo, + &OpenInfoCount + ); + if (!EFI_ERROR (Status)) { + for (OpenInfoIndex = 0; OpenInfoIndex < OpenInfoCount; OpenInfoIndex++) { + if (OpenInfo[OpenInfoIndex].AgentHandle == Image->Handle) { + Status = CoreCloseProtocol ( + HandleBuffer[HandleIndex], + ProtocolGuidArray[ProtocolIndex], + Image->Handle, + OpenInfo[OpenInfoIndex].ControllerHandle + ); + } + } + if (OpenInfo != NULL) { + CoreFreePool(OpenInfo); + } + } + } + if (ProtocolGuidArray != NULL) { + CoreFreePool(ProtocolGuidArray); + } + } + } + if (HandleBuffer != NULL) { + CoreFreePool (HandleBuffer); + } + } + + CoreRemoveDebugImageInfoEntry (Image->Handle); + + Status = CoreUninstallProtocolInterface ( + Image->Handle, + &gEfiLoadedImageDevicePathProtocolGuid, + Image->LoadedImageDevicePath + ); + + Status = CoreUninstallProtocolInterface ( + Image->Handle, + &gEfiLoadedImageProtocolGuid, + &Image->Info + ); + + if (Image->ImageContext.HiiResourceData != 0) { + Status = CoreUninstallProtocolInterface ( + Image->Handle, + &gEfiHiiPackageListProtocolGuid, + (VOID *) (UINTN) Image->ImageContext.HiiResourceData + ); + } + + } + + if (Image->RuntimeData != NULL) { + if (Image->RuntimeData->Link.ForwardLink != NULL) { + // + // Remove the Image from the Runtime Image list as we are about to Free it! + // + RemoveEntryList (&Image->RuntimeData->Link); + } + CoreFreePool (Image->RuntimeData); + } + + // + // Free the Image from memory + // + if ((Image->ImageBasePage != 0) && FreePage) { + CoreFreePages (Image->ImageBasePage, Image->NumberOfPages); + } + + // + // Done with the Image structure + // + if (Image->Info.FilePath != NULL) { + CoreFreePool (Image->Info.FilePath); + } + + if (Image->LoadedImageDevicePath != NULL) { + CoreFreePool (Image->LoadedImageDevicePath); + } + + if (Image->FixupData != NULL) { + CoreFreePool (Image->FixupData); + } + + CoreFreePool (Image); +} + + /** Loads an EFI image into memory and returns a handle to the image. - @param BootPolicy If TRUE, indicates that the request originates - from the boot manager, and that the boot - manager is attempting to load FilePath as a - boot selection. - @param ParentImageHandle The caller's image handle. - @param FilePath The specific file path from which the image is - loaded. - @param SourceBuffer If not NULL, a pointer to the memory location - containing a copy of the image to be loaded. - @param SourceSize The size in bytes of SourceBuffer. - @param DstBuffer The buffer to store the image - @param NumberOfPages If not NULL, it inputs a pointer to the page - number of DstBuffer and outputs a pointer to - the page number of the image. If this number is - not enough, return EFI_BUFFER_TOO_SMALL and - this parameter contains the required number. - @param ImageHandle Pointer to the returned image handle that is - created when the image is successfully loaded. - @param EntryPoint A pointer to the entry point - @param Attribute The bit mask of attributes to set for the load - PE image - - @retval EFI_SUCCESS The image was loaded into memory. - @retval EFI_NOT_FOUND The FilePath was not found. - @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value. - @retval EFI_BUFFER_TOO_SMALL The buffer is too small - @retval EFI_UNSUPPORTED The image type is not supported, or the device - path cannot be parsed to locate the proper - protocol for loading the file. - @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient + @param BootPolicy If TRUE, indicates that the request originates + from the boot manager, and that the boot + manager is attempting to load FilePath as a + boot selection. + @param ParentImageHandle The caller's image handle. + @param FilePath The specific file path from which the image is + loaded. + @param SourceBuffer If not NULL, a pointer to the memory location + containing a copy of the image to be loaded. + @param SourceSize The size in bytes of SourceBuffer. + @param DstBuffer The buffer to store the image + @param NumberOfPages If not NULL, it inputs a pointer to the page + number of DstBuffer and outputs a pointer to + the page number of the image. If this number is + not enough, return EFI_BUFFER_TOO_SMALL and + this parameter contains the required number. + @param ImageHandle Pointer to the returned image handle that is + created when the image is successfully loaded. + @param EntryPoint A pointer to the entry point + @param Attribute The bit mask of attributes to set for the load + PE image + + @retval EFI_SUCCESS The image was loaded into memory. + @retval EFI_NOT_FOUND The FilePath was not found. + @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value. + @retval EFI_BUFFER_TOO_SMALL The buffer is too small + @retval EFI_UNSUPPORTED The image type is not supported, or the device + path cannot be parsed to locate the proper + protocol for loading the file. + @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient resources. **/ -STATIC EFI_STATUS CoreLoadImageCommon ( IN BOOLEAN BootPolicy, @@ -647,19 +1012,63 @@ CoreLoadImageCommon ( return EFI_INVALID_PARAMETER; } - // - // Get simple read access to the source file - // + ZeroMem (&FHand, sizeof (IMAGE_FILE_HANDLE)); + FHand.Signature = IMAGE_FILE_HANDLE_SIGNATURE; OriginalFilePath = FilePath; - Status = CoreOpenImageFile ( - BootPolicy, - SourceBuffer, - SourceSize, - &FilePath, - &DeviceHandle, - &FHand, - &AuthenticationStatus - ); + HandleFilePath = FilePath; + DeviceHandle = NULL; + Status = EFI_SUCCESS; + AuthenticationStatus = 0; + // + // If the caller passed a copy of the file, then just use it + // + if (SourceBuffer != NULL) { + FHand.Source = SourceBuffer; + FHand.SourceSize = SourceSize; + CoreLocateDevicePath (&gEfiDevicePathProtocolGuid, &HandleFilePath, &DeviceHandle); + if (SourceSize > 0) { + Status = EFI_SUCCESS; + } else { + Status = EFI_LOAD_ERROR; + } + } else { + if (FilePath == NULL) { + return EFI_INVALID_PARAMETER; + } + // + // Get the source file buffer by its device path. + // + FHand.Source = GetFileBufferByFilePath ( + BootPolicy, + FilePath, + &FHand.SourceSize, + &AuthenticationStatus + ); + 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)) { + 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 (Status == EFI_ALREADY_STARTED) { Image = NULL; goto Done; @@ -687,7 +1096,7 @@ CoreLoadImageCommon ( // // Allocate a new image structure // - Image = CoreAllocateZeroBootServicesPool (sizeof(LOADED_IMAGE_PRIVATE_DATA)); + Image = AllocateZeroPool (sizeof(LOADED_IMAGE_PRIVATE_DATA)); if (Image == NULL) { return EFI_OUT_OF_RESOURCES; } @@ -696,12 +1105,13 @@ CoreLoadImageCommon ( // Pull out just the file portion of the DevicePath for the LoadedImage FilePath // FilePath = OriginalFilePath; - Status = CoreHandleProtocol (DeviceHandle, &gEfiDevicePathProtocolGuid, (VOID **)&HandleFilePath); - if (!EFI_ERROR (Status)) { - FilePathSize = CoreDevicePathSize (HandleFilePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL); - FilePath = (EFI_DEVICE_PATH_PROTOCOL *) ( ((UINT8 *)FilePath) + FilePathSize ); + if (DeviceHandle != NULL) { + Status = CoreHandleProtocol (DeviceHandle, &gEfiDevicePathProtocolGuid, (VOID **)&HandleFilePath); + if (!EFI_ERROR (Status)) { + FilePathSize = GetDevicePathSize (HandleFilePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL); + FilePath = (EFI_DEVICE_PATH_PROTOCOL *) (((UINT8 *)FilePath) + FilePathSize ); + } } - // // Initialize the fields for an internal driver // @@ -709,7 +1119,7 @@ CoreLoadImageCommon ( Image->Info.SystemTable = gDxeCoreST; Image->Info.DeviceHandle = DeviceHandle; Image->Info.Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION; - Image->Info.FilePath = CoreDuplicateDevicePath (FilePath); + Image->Info.FilePath = DuplicateDevicePath (FilePath); Image->Info.ParentHandle = ParentImageHandle; @@ -749,12 +1159,12 @@ CoreLoadImageCommon ( if (NumberOfPages != NULL) { *NumberOfPages = Image->NumberOfPages; - } + } // // Register the image in the Debug Image Info Table if the attribute is set // - if (Attribute & EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION) { + if ((Attribute & EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION) != 0) { CoreNewDebugImageInfoEntry (EFI_DEBUG_IMAGE_INFO_TYPE_NORMAL, &Image->Info, Image->Handle); } @@ -776,7 +1186,7 @@ CoreLoadImageCommon ( // otherwise Loaded Image Device Path Protocol is installed with a NULL interface pointer. // if (OriginalFilePath != NULL) { - Image->LoadedImageDevicePath = CoreDuplicateDevicePath (OriginalFilePath); + Image->LoadedImageDevicePath = DuplicateDevicePath (OriginalFilePath); } // @@ -792,6 +1202,21 @@ CoreLoadImageCommon ( goto Done; } + // + // Install HII Package List Protocol onto the image handle + // + if (Image->ImageContext.HiiResourceData != 0) { + Status = CoreInstallProtocolInterface ( + &Image->Handle, + &gEfiHiiPackageListProtocolGuid, + EFI_NATIVE_INTERFACE, + (VOID *) (UINTN) Image->ImageContext.HiiResourceData + ); + if (EFI_ERROR (Status)) { + goto Done; + } + } + // // Success. Return the image handle // @@ -827,26 +1252,26 @@ Done: /** Loads an EFI image into memory and returns a handle to the image. - @param BootPolicy If TRUE, indicates that the request originates - from the boot manager, and that the boot - manager is attempting to load FilePath as a - boot selection. - @param ParentImageHandle The caller's image handle. - @param FilePath The specific file path from which the image is - loaded. - @param SourceBuffer If not NULL, a pointer to the memory location - containing a copy of the image to be loaded. - @param SourceSize The size in bytes of SourceBuffer. - @param ImageHandle Pointer to the returned image handle that is - created when the image is successfully loaded. - - @retval EFI_SUCCESS The image was loaded into memory. - @retval EFI_NOT_FOUND The FilePath was not found. - @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value. - @retval EFI_UNSUPPORTED The image type is not supported, or the device - path cannot be parsed to locate the proper - protocol for loading the file. - @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient + @param BootPolicy If TRUE, indicates that the request originates + from the boot manager, and that the boot + manager is attempting to load FilePath as a + boot selection. + @param ParentImageHandle The caller's image handle. + @param FilePath The specific file path from which the image is + loaded. + @param SourceBuffer If not NULL, a pointer to the memory location + containing a copy of the image to be loaded. + @param SourceSize The size in bytes of SourceBuffer. + @param ImageHandle Pointer to the returned image handle that is + created when the image is successfully loaded. + + @retval EFI_SUCCESS The image was loaded into memory. + @retval EFI_NOT_FOUND The FilePath was not found. + @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value. + @retval EFI_UNSUPPORTED The image type is not supported, or the device + path cannot be parsed to locate the proper + protocol for loading the file. + @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient resources. **/ @@ -862,8 +1287,12 @@ CoreLoadImage ( ) { EFI_STATUS Status; + UINT64 Tick; - PERF_START (NULL, "LoadImage", NULL, 0); + Tick = 0; + PERF_CODE ( + Tick = GetPerformanceCounter (); + ); Status = CoreLoadImageCommon ( BootPolicy, @@ -878,7 +1307,8 @@ CoreLoadImage ( EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION | EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION ); - PERF_END (NULL, "LoadImage", NULL, 0); + PERF_START (*ImageHandle, "LoadImage:", NULL, Tick); + PERF_END (*ImageHandle, "LoadImage:", NULL, 0); return Status; } @@ -888,29 +1318,29 @@ CoreLoadImage ( /** Loads an EFI image into memory and returns a handle to the image with extended parameters. - @param This Calling context - @param ParentImageHandle The caller's image handle. - @param FilePath The specific file path from which the image is - loaded. - @param SourceBuffer If not NULL, a pointer to the memory location - containing a copy of the image to be loaded. - @param SourceSize The size in bytes of SourceBuffer. - @param DstBuffer The buffer to store the image. - @param NumberOfPages For input, specifies the space size of the - image by caller if not NULL. For output, - specifies the actual space size needed. - @param ImageHandle Image handle for output. - @param EntryPoint Image entry point for output. - @param Attribute The bit mask of attributes to set for the load - PE image. - - @retval EFI_SUCCESS The image was loaded into memory. - @retval EFI_NOT_FOUND The FilePath was not found. - @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value. - @retval EFI_UNSUPPORTED The image type is not supported, or the device - path cannot be parsed to locate the proper - protocol for loading the file. - @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient + @param This Calling context + @param ParentImageHandle The caller's image handle. + @param FilePath The specific file path from which the image is + loaded. + @param SourceBuffer If not NULL, a pointer to the memory location + containing a copy of the image to be loaded. + @param SourceSize The size in bytes of SourceBuffer. + @param DstBuffer The buffer to store the image. + @param NumberOfPages For input, specifies the space size of the + image by caller if not NULL. For output, + specifies the actual space size needed. + @param ImageHandle Image handle for output. + @param EntryPoint Image entry point for output. + @param Attribute The bit mask of attributes to set for the load + PE image. + + @retval EFI_SUCCESS The image was loaded into memory. + @retval EFI_NOT_FOUND The FilePath was not found. + @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value. + @retval EFI_UNSUPPORTED The image type is not supported, or the device + path cannot be parsed to locate the proper + protocol for loading the file. + @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient resources. **/ @@ -947,18 +1377,18 @@ CoreLoadImageEx ( /** Transfer control to a loaded image's entry point. - @param ImageHandle Handle of image to be started. - @param ExitDataSize Pointer of the size to ExitData - @param ExitData Pointer to a pointer to a data buffer that - includes a Null-terminated Unicode string, - optionally followed by additional binary data. - The string is a description that the caller may - use to further indicate the reason for the - image's exit. - - @retval EFI_INVALID_PARAMETER Invalid parameter - @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate - @retval EFI_SUCCESS Successfully transfer control to the image's + @param ImageHandle Handle of image to be started. + @param ExitDataSize Pointer of the size to ExitData + @param ExitData Pointer to a pointer to a data buffer that + includes a Null-terminated Unicode string, + optionally followed by additional binary data. + The string is a description that the caller may + use to further indicate the reason for the + image's exit. + + @retval EFI_INVALID_PARAMETER Invalid parameter + @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate + @retval EFI_SUCCESS Successfully transfer control to the image's entry point. **/ @@ -977,14 +1407,22 @@ CoreStartImage ( UINTN SetJumpFlag; Image = CoreLoadedImageInfo (ImageHandle); - if (Image == NULL_HANDLE || Image->Started) { + if (Image == NULL || Image->Started) { return EFI_INVALID_PARAMETER; } + // + // The image to be started must have the machine type supported by DxeCore. + // + ASSERT (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image->Machine)); + if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Image->Machine)) { + return EFI_UNSUPPORTED; + } + // // Don't profile Objects or invalid start requests // - PERF_START (ImageHandle, START_IMAGE_TOK, NULL, 0); + PERF_START (ImageHandle, "StartImage:", NULL, 0); // @@ -1002,9 +1440,9 @@ CoreStartImage ( // JumpContext must be aligned on a CPU specific boundary. // Overallocate the buffer and force the required alignment // - Image->JumpBuffer = CoreAllocateBootServicesPool (sizeof (BASE_LIBRARY_JUMP_BUFFER) + BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT); + Image->JumpBuffer = AllocatePool (sizeof (BASE_LIBRARY_JUMP_BUFFER) + BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT); if (Image->JumpBuffer == NULL) { - PERF_END (ImageHandle, START_IMAGE_TOK, NULL, 0); + PERF_END (ImageHandle, "StartImage:", NULL, 0); return EFI_OUT_OF_RESOURCES; } Image->JumpContext = ALIGN_POINTER (Image->JumpBuffer, BASE_LIBRARY_JUMP_BUFFER_ALIGNMENT); @@ -1014,7 +1452,7 @@ CoreStartImage ( // The initial call to SetJump() must always return 0. // Subsequent calls to LongJump() cause a non-zero value to be returned by SetJump(). // - if (!SetJumpFlag) { + if (SetJumpFlag == 0) { // // Call the image's entry point // @@ -1028,7 +1466,7 @@ CoreStartImage ( // DEBUG_CODE_BEGIN (); if (EFI_ERROR (Image->Status)) { - DEBUG ((DEBUG_ERROR, "Error: Image at %10p start failed: %r\n", Image->Info.ImageBase, Image->Status)); + DEBUG ((DEBUG_ERROR, "Error: Image at %11p start failed: %r\n", Image->Info.ImageBase, Image->Status)); } DEBUG_CODE_END (); @@ -1062,12 +1500,7 @@ CoreStartImage ( DEBUG_CODE_BEGIN (); if (Image->ExitDataSize != 0 || Image->ExitData != NULL) { - DEBUG ( - (DEBUG_LOAD, - "StartImage: ExitDataSize %d, ExitData %x", - Image->ExitDataSize, - Image->ExitData) - ); + DEBUG ((DEBUG_LOAD, "StartImage: ExitDataSize %d, ExitData %p", (UINT32)Image->ExitDataSize, Image->ExitData)); if (Image->ExitData != NULL) { DEBUG ((DEBUG_LOAD, " (%hs)", Image->ExitData)); } @@ -1105,175 +1538,30 @@ CoreStartImage ( // // Done // - PERF_END (ImageHandle, START_IMAGE_TOK, NULL, 0); + PERF_END (ImageHandle, "StartImage:", NULL, 0); return Status; } - - -/** - Unloads EFI image from memory. - - @param Image EFI image - @param FreePage Free allocated pages - -**/ -VOID -CoreUnloadAndCloseImage ( - IN LOADED_IMAGE_PRIVATE_DATA *Image, - IN BOOLEAN FreePage - ) -{ - EFI_STATUS Status; - UINTN HandleCount; - EFI_HANDLE *HandleBuffer; - UINTN HandleIndex; - EFI_GUID **ProtocolGuidArray; - UINTN ArrayCount; - UINTN ProtocolIndex; - EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfo; - UINTN OpenInfoCount; - UINTN OpenInfoIndex; - - if (Image->Ebc != NULL) { - // - // If EBC protocol exists we must perform cleanups for this image. - // - Image->Ebc->UnloadImage (Image->Ebc, Image->Handle); - } - - // - // Unload image, free Image->ImageContext->ModHandle - // - PeCoffLoaderUnloadImage (&Image->ImageContext); - - // - // Free our references to the image handle - // - if (Image->Handle != NULL_HANDLE) { - - Status = CoreLocateHandleBuffer ( - AllHandles, - NULL, - NULL, - &HandleCount, - &HandleBuffer - ); - if (!EFI_ERROR (Status)) { - for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) { - Status = CoreProtocolsPerHandle ( - HandleBuffer[HandleIndex], - &ProtocolGuidArray, - &ArrayCount - ); - if (!EFI_ERROR (Status)) { - for (ProtocolIndex = 0; ProtocolIndex < ArrayCount; ProtocolIndex++) { - Status = CoreOpenProtocolInformation ( - HandleBuffer[HandleIndex], - ProtocolGuidArray[ProtocolIndex], - &OpenInfo, - &OpenInfoCount - ); - if (!EFI_ERROR (Status)) { - for (OpenInfoIndex = 0; OpenInfoIndex < OpenInfoCount; OpenInfoIndex++) { - if (OpenInfo[OpenInfoIndex].AgentHandle == Image->Handle) { - Status = CoreCloseProtocol ( - HandleBuffer[HandleIndex], - ProtocolGuidArray[ProtocolIndex], - Image->Handle, - OpenInfo[OpenInfoIndex].ControllerHandle - ); - } - } - if (OpenInfo != NULL) { - CoreFreePool(OpenInfo); - } - } - } - if (ProtocolGuidArray != NULL) { - CoreFreePool(ProtocolGuidArray); - } - } - } - if (HandleBuffer != NULL) { - CoreFreePool (HandleBuffer); - } - } - - CoreRemoveDebugImageInfoEntry (Image->Handle); - - Status = CoreUninstallProtocolInterface ( - Image->Handle, - &gEfiLoadedImageDevicePathProtocolGuid, - Image->LoadedImageDevicePath - ); - - Status = CoreUninstallProtocolInterface ( - Image->Handle, - &gEfiLoadedImageProtocolGuid, - &Image->Info - ); - - } - - if (Image->RuntimeData != NULL) { - if (Image->RuntimeData->Link.ForwardLink != NULL) { - // - // Remove the Image from the Runtime Image list as we are about to Free it! - // - RemoveEntryList (&Image->RuntimeData->Link); - } - CoreFreePool (Image->RuntimeData); - } - - // - // Free the Image from memory - // - if ((Image->ImageBasePage != 0) && FreePage) { - CoreFreePages (Image->ImageBasePage, Image->NumberOfPages); - } - - // - // Done with the Image structure - // - if (Image->Info.FilePath != NULL) { - CoreFreePool (Image->Info.FilePath); - } - - if (Image->LoadedImageDevicePath != NULL) { - CoreFreePool (Image->LoadedImageDevicePath); - } - - if (Image->FixupData != NULL) { - CoreFreePool (Image->FixupData); - } - - CoreFreePool (Image); -} - - - - /** Terminates the currently loaded EFI image and returns control to boot services. - @param ImageHandle Handle that identifies the image. This - parameter is passed to the image on entry. - @param Status The image's exit code. - @param ExitDataSize The size, in bytes, of ExitData. Ignored if - ExitStatus is EFI_SUCCESS. - @param ExitData Pointer to a data buffer that includes a - Null-terminated Unicode string, optionally - followed by additional binary data. The string - is a description that the caller may use to - further indicate the reason for the image's - exit. - - @retval EFI_INVALID_PARAMETER Image handle is NULL or it is not current - image. - @retval EFI_SUCCESS Successfully terminates the currently loaded - EFI image. - @retval EFI_ACCESS_DENIED Should never reach there. + @param ImageHandle Handle that identifies the image. This + parameter is passed to the image on entry. + @param Status The image's exit code. + @param ExitDataSize The size, in bytes, of ExitData. Ignored if + ExitStatus is EFI_SUCCESS. + @param ExitData Pointer to a data buffer that includes a + Null-terminated Unicode string, optionally + followed by additional binary data. The string + is a description that the caller may use to + further indicate the reason for the image's + exit. + + @retval EFI_INVALID_PARAMETER Image handle is NULL or it is not current + image. + @retval EFI_SUCCESS Successfully terminates the currently loaded + EFI image. + @retval EFI_ACCESS_DENIED Should never reach there. @retval EFI_OUT_OF_RESOURCES Could not allocate pool **/ @@ -1292,11 +1580,11 @@ CoreExit ( // // Prevent possible reentrance to this function // for the same ImageHandle - // - OldTpl = CoreRaiseTpl (TPL_NOTIFY); - + // + OldTpl = CoreRaiseTpl (TPL_NOTIFY); + Image = CoreLoadedImageInfo (ImageHandle); - if (Image == NULL_HANDLE) { + if (Image == NULL) { Status = EFI_INVALID_PARAMETER; goto Done; } @@ -1329,7 +1617,7 @@ CoreExit ( // if (ExitData != NULL) { Image->ExitDataSize = ExitDataSize; - Image->ExitData = CoreAllocateBootServicesPool (Image->ExitDataSize); + Image->ExitData = AllocatePool (Image->ExitDataSize); if (Image->ExitData == NULL) { Status = EFI_OUT_OF_RESOURCES; goto Done; @@ -1359,12 +1647,12 @@ Done: /** Unloads an image. - @param ImageHandle Handle that identifies the image to be - unloaded. + @param ImageHandle Handle that identifies the image to be + unloaded. - @retval EFI_SUCCESS The image has been unloaded. - @retval EFI_UNSUPPORTED The image has been sarted, and does not support - unload. + @retval EFI_SUCCESS The image has been unloaded. + @retval EFI_UNSUPPORTED The image has been sarted, and does not support + unload. @retval EFI_INVALID_PARAMPETER ImageHandle is not a valid image handle. **/ @@ -1376,14 +1664,7 @@ CoreUnloadImage ( { EFI_STATUS Status; LOADED_IMAGE_PRIVATE_DATA *Image; - EFI_TPL OldTpl; - // - // Prevent possible reentrance to this function - // for the same ImageHandle - // - OldTpl = CoreRaiseTpl (TPL_NOTIFY); - Image = CoreLoadedImageInfo (ImageHandle); if (Image == NULL ) { // @@ -1418,7 +1699,6 @@ CoreUnloadImage ( } Done: - CoreRestoreTpl (OldTpl); return Status; } @@ -1427,11 +1707,11 @@ Done: /** Unload the specified image. - @param This Indicates the calling context. - @param ImageHandle The specified image handle. + @param This Indicates the calling context. + @param ImageHandle The specified image handle. - @retval EFI_INVALID_PARAMETER Image handle is NULL. - @retval EFI_UNSUPPORTED Attempt to unload an unsupported image. + @retval EFI_INVALID_PARAMETER Image handle is NULL. + @retval EFI_UNSUPPORTED Attempt to unload an unsupported image. @retval EFI_SUCCESS Image successfully unloaded. **/