+\r
+/**\r
+ Unloads EFI image from memory.\r
+\r
+ @param Image EFI image\r
+ @param FreePage Free allocated pages\r
+\r
+**/\r
+VOID\r
+CoreUnloadAndCloseImage (\r
+ IN LOADED_IMAGE_PRIVATE_DATA *Image,\r
+ IN BOOLEAN FreePage\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ UINTN HandleCount;\r
+ EFI_HANDLE *HandleBuffer;\r
+ UINTN HandleIndex;\r
+ EFI_GUID **ProtocolGuidArray;\r
+ UINTN ArrayCount;\r
+ UINTN ProtocolIndex;\r
+ EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfo;\r
+ UINTN OpenInfoCount;\r
+ UINTN OpenInfoIndex;\r
+\r
+ if (Image->Ebc != NULL) {\r
+ //\r
+ // If EBC protocol exists we must perform cleanups for this image.\r
+ //\r
+ Image->Ebc->UnloadImage (Image->Ebc, Image->Handle);\r
+ }\r
+\r
+ //\r
+ // Unload image, free Image->ImageContext->ModHandle\r
+ //\r
+ PeCoffLoaderUnloadImage (&Image->ImageContext);\r
+\r
+ //\r
+ // Free our references to the image handle\r
+ //\r
+ if (Image->Handle != NULL) {\r
+\r
+ Status = CoreLocateHandleBuffer (\r
+ AllHandles,\r
+ NULL,\r
+ NULL,\r
+ &HandleCount,\r
+ &HandleBuffer\r
+ );\r
+ if (!EFI_ERROR (Status)) {\r
+ for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) {\r
+ Status = CoreProtocolsPerHandle (\r
+ HandleBuffer[HandleIndex],\r
+ &ProtocolGuidArray,\r
+ &ArrayCount\r
+ );\r
+ if (!EFI_ERROR (Status)) {\r
+ for (ProtocolIndex = 0; ProtocolIndex < ArrayCount; ProtocolIndex++) {\r
+ Status = CoreOpenProtocolInformation (\r
+ HandleBuffer[HandleIndex],\r
+ ProtocolGuidArray[ProtocolIndex],\r
+ &OpenInfo,\r
+ &OpenInfoCount\r
+ );\r
+ if (!EFI_ERROR (Status)) {\r
+ for (OpenInfoIndex = 0; OpenInfoIndex < OpenInfoCount; OpenInfoIndex++) {\r
+ if (OpenInfo[OpenInfoIndex].AgentHandle == Image->Handle) {\r
+ Status = CoreCloseProtocol (\r
+ HandleBuffer[HandleIndex],\r
+ ProtocolGuidArray[ProtocolIndex],\r
+ Image->Handle,\r
+ OpenInfo[OpenInfoIndex].ControllerHandle\r
+ );\r
+ }\r
+ }\r
+ if (OpenInfo != NULL) {\r
+ CoreFreePool(OpenInfo);\r
+ }\r
+ }\r
+ }\r
+ if (ProtocolGuidArray != NULL) {\r
+ CoreFreePool(ProtocolGuidArray);\r
+ }\r
+ }\r
+ }\r
+ if (HandleBuffer != NULL) {\r
+ CoreFreePool (HandleBuffer);\r
+ }\r
+ }\r
+\r
+ CoreRemoveDebugImageInfoEntry (Image->Handle);\r
+\r
+ Status = CoreUninstallProtocolInterface (\r
+ Image->Handle,\r
+ &gEfiLoadedImageDevicePathProtocolGuid,\r
+ Image->LoadedImageDevicePath\r
+ );\r
+\r
+ Status = CoreUninstallProtocolInterface (\r
+ Image->Handle,\r
+ &gEfiLoadedImageProtocolGuid,\r
+ &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
+ if (Image->RuntimeData->Link.ForwardLink != NULL) {\r
+ //\r
+ // Remove the Image from the Runtime Image list as we are about to Free it!\r
+ //\r
+ RemoveEntryList (&Image->RuntimeData->Link);\r
+ }\r
+ CoreFreePool (Image->RuntimeData);\r
+ }\r
+\r
+ //\r
+ // Free the Image from memory\r
+ //\r
+ if ((Image->ImageBasePage != 0) && FreePage) {\r
+ CoreFreePages (Image->ImageBasePage, Image->NumberOfPages);\r
+ }\r
+\r
+ //\r
+ // Done with the Image structure\r
+ //\r
+ if (Image->Info.FilePath != NULL) {\r
+ CoreFreePool (Image->Info.FilePath);\r
+ }\r
+\r
+ if (Image->LoadedImageDevicePath != NULL) {\r
+ CoreFreePool (Image->LoadedImageDevicePath);\r
+ }\r
+\r
+ if (Image->FixupData != NULL) {\r
+ CoreFreePool (Image->FixupData);\r
+ }\r
+\r
+ CoreFreePool (Image);\r
+}\r
+\r
+\r
+/**\r
+ Loads an EFI image into memory and returns a handle to the image.\r
+\r
+ @param BootPolicy If TRUE, indicates that the request originates\r
+ from the boot manager, and that the boot\r
+ manager is attempting to load FilePath as a\r
+ boot selection.\r
+ @param ParentImageHandle The caller's image handle.\r
+ @param FilePath The specific file path from which the image is\r
+ loaded.\r
+ @param SourceBuffer If not NULL, a pointer to the memory location\r
+ containing a copy of the image to be loaded.\r
+ @param SourceSize The size in bytes of SourceBuffer.\r
+ @param DstBuffer The buffer to store the image\r
+ @param NumberOfPages If not NULL, it inputs a pointer to the page\r
+ number of DstBuffer and outputs a pointer to\r
+ the page number of the image. If this number is\r
+ not enough, return EFI_BUFFER_TOO_SMALL and\r
+ this parameter contains the required number.\r
+ @param ImageHandle Pointer to the returned image handle that is\r
+ created when the image is successfully loaded.\r
+ @param EntryPoint A pointer to the entry point\r
+ @param Attribute The bit mask of attributes to set for the load\r
+ PE image\r
+\r
+ @retval EFI_SUCCESS The image was loaded into memory.\r
+ @retval EFI_NOT_FOUND The FilePath was not found.\r
+ @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
+ @retval EFI_BUFFER_TOO_SMALL The buffer is too small\r
+ @retval EFI_UNSUPPORTED The image type is not supported, or the device\r
+ path cannot be parsed to locate the proper\r
+ protocol for loading the file.\r
+ @retval EFI_OUT_OF_RESOURCES Image was not loaded due to insufficient\r
+ resources.\r
+\r
+**/\r