/* Loads and relocates a PE/COFF image in place. */
EFI_STATUS LoadAndRelocatePeCoffImageInPlace(IN VOID *Pe32Data, IN VOID *ImageAddress)
/* Loads and relocates a PE/COFF image in place. */
EFI_STATUS LoadAndRelocatePeCoffImageInPlace(IN VOID *Pe32Data, IN VOID *ImageAddress)
{
  EFI_STATUS                    Status;
  PE_COFF_LOADER_IMAGE_CONTEXT  ImageContext;
  ZeroMem (&ImageContext, sizeof (ImageContext));
  ImageContext.Handle    = Pe32Data;
  ImageContext.ImageRead = PeiImageRead;
  Status = PeCoffLoaderGetImageInfo (&ImageContext);
  if (EFI_ERROR (Status)) {
    ASSERT_EFI_ERROR (Status);
    return Status;
  }
  ImageContext.ImageAddress = (PHYSICAL_ADDRESS)(UINTN)ImageAddress;
  Status = PeCoffLoaderLoadImage (&ImageContext);
  if (EFI_ERROR (Status)) {
    ASSERT_EFI_ERROR (Status);
    return Status;
  }
  Status = PeCoffLoaderRelocateImage (&ImageContext);
  if (EFI_ERROR (Status)) {
    ASSERT_EFI_ERROR (Status);
    return Status;
  }
  if (ImageContext.ImageAddress != (EFI_PHYSICAL_ADDRESS)(UINTN)Pe32Data) {
    InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);
  }
  return Status;
}