+\r
+\r
+/**\r
+ Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI\r
+ runtime. \r
+ \r
+ This function reapplies relocation fixups to the PE/COFF image specified by ImageBase \r
+ and ImageSize so the image will execute correctly when the PE/COFF image is mapped \r
+ to the address specified by VirtualImageBase. RelocationData must be identical \r
+ to the FiuxupData buffer from the PE_COFF_LOADER_IMAGE_CONTEXT structure \r
+ after this PE/COFF image was relocated with PeCoffLoaderRelocateImage().\r
+\r
+ Note that if the platform does not maintain coherency between the instruction cache(s) and the data\r
+ cache(s) in hardware, then the caller is responsible for performing cache maintenance operations\r
+ prior to transferring control to a PE/COFF image that is loaded using this library.\r
+\r
+ @param ImageBase The base address of a PE/COFF image that has been loaded \r
+ and relocated into system memory.\r
+ @param VirtImageBase The request virtual address that the PE/COFF image is to\r
+ be fixed up for.\r
+ @param ImageSize The size, in bytes, of the PE/COFF image.\r
+ @param RelocationData A pointer to the relocation data that was collected when the PE/COFF \r
+ image was relocated using PeCoffLoaderRelocateImage().\r
+ \r
+**/\r
+VOID\r
+EFIAPI\r
+PeCoffLoaderRelocateImageForRuntime (\r
+ IN PHYSICAL_ADDRESS ImageBase,\r
+ IN PHYSICAL_ADDRESS VirtImageBase,\r
+ IN UINTN ImageSize,\r
+ IN VOID *RelocationData\r
+ )\r
+{\r
+ CHAR8 *OldBase;\r
+ CHAR8 *NewBase;\r
+ EFI_IMAGE_DOS_HEADER *DosHdr;\r
+ EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
+ UINT32 NumberOfRvaAndSizes;\r
+ EFI_IMAGE_DATA_DIRECTORY *DataDirectory;\r
+ EFI_IMAGE_DATA_DIRECTORY *RelocDir;\r
+ EFI_IMAGE_BASE_RELOCATION *RelocBase;\r
+ EFI_IMAGE_BASE_RELOCATION *RelocBaseEnd;\r
+ UINT16 *Reloc;\r
+ UINT16 *RelocEnd;\r
+ CHAR8 *Fixup;\r
+ CHAR8 *FixupBase;\r
+ UINT16 *Fixup16;\r
+ UINT32 *Fixup32;\r
+ UINT64 *Fixup64;\r
+ CHAR8 *FixupData;\r
+ UINTN Adjust;\r
+ RETURN_STATUS Status;\r
+ UINT16 Magic;\r
+\r
+ OldBase = (CHAR8 *)((UINTN)ImageBase);\r
+ NewBase = (CHAR8 *)((UINTN)VirtImageBase);\r
+ Adjust = (UINTN) NewBase - (UINTN) OldBase;\r
+\r
+ //\r
+ // Find the image's relocate dir info\r
+ //\r
+ DosHdr = (EFI_IMAGE_DOS_HEADER *)OldBase;\r
+ if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r
+ //\r
+ // Valid DOS header so get address of PE header\r
+ //\r
+ Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)(((CHAR8 *)DosHdr) + DosHdr->e_lfanew);\r
+ } else {\r
+ //\r
+ // No Dos header so assume image starts with PE header.\r
+ //\r
+ Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)OldBase;\r
+ }\r
+\r
+ if (Hdr.Pe32->Signature != EFI_IMAGE_NT_SIGNATURE) {\r
+ //\r
+ // Not a valid PE image so Exit\r
+ //\r
+ return ;\r
+ }\r
+\r
+ Magic = PeCoffLoaderGetPeHeaderMagicValue (Hdr);\r
+\r
+ if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
+ //\r
+ // Use PE32 offset\r
+ //\r
+ NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;\r
+ DataDirectory = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32->OptionalHeader.DataDirectory[0]);\r
+ } else {\r
+ //\r
+ // Use PE32+ offset\r
+ //\r
+ NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;\r
+ DataDirectory = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32Plus->OptionalHeader.DataDirectory[0]);\r
+ }\r
+\r
+ //\r
+ // Find the relocation block\r
+ //\r
+ // Per the PE/COFF spec, you can't assume that a given data directory\r
+ // is present in the image. You have to check the NumberOfRvaAndSizes in\r
+ // the optional header to verify a desired directory entry is there.\r
+ //\r
+ if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) {\r
+ RelocDir = DataDirectory + EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC;\r
+ RelocBase = (EFI_IMAGE_BASE_RELOCATION *)(UINTN)(ImageBase + RelocDir->VirtualAddress);\r
+ RelocBaseEnd = (EFI_IMAGE_BASE_RELOCATION *)(UINTN)(ImageBase + RelocDir->VirtualAddress + RelocDir->Size);\r
+ } else {\r
+ //\r
+ // Cannot find relocations, cannot continue to relocate the image, ASSERT for this invalid image.\r
+ //\r
+ ASSERT (FALSE);\r
+ return ;\r
+ }\r
+ \r
+ //\r
+ // ASSERT for the invalid image when RelocBase and RelocBaseEnd are both NULL.\r
+ //\r
+ ASSERT (RelocBase != NULL && RelocBaseEnd != NULL);\r
+\r
+ //\r
+ // Run the whole relocation block. And re-fixup data that has not been\r
+ // modified. The FixupData is used to see if the image has been modified\r
+ // since it was relocated. This is so data sections that have been updated\r
+ // by code will not be fixed up, since that would set them back to\r
+ // defaults.\r
+ //\r
+ FixupData = RelocationData;\r
+ while (RelocBase < RelocBaseEnd) {\r
+\r
+ Reloc = (UINT16 *) ((UINT8 *) RelocBase + sizeof (EFI_IMAGE_BASE_RELOCATION));\r
+ RelocEnd = (UINT16 *) ((UINT8 *) RelocBase + RelocBase->SizeOfBlock);\r
+ FixupBase = (CHAR8 *) ((UINTN)ImageBase) + RelocBase->VirtualAddress;\r
+\r
+ //\r
+ // Run this relocation record\r
+ //\r
+ while (Reloc < RelocEnd) {\r
+\r
+ Fixup = FixupBase + (*Reloc & 0xFFF);\r
+ switch ((*Reloc) >> 12) {\r
+\r
+ case EFI_IMAGE_REL_BASED_ABSOLUTE:\r
+ break;\r
+\r
+ case EFI_IMAGE_REL_BASED_HIGH:\r
+ Fixup16 = (UINT16 *) Fixup;\r
+ if (*(UINT16 *) FixupData == *Fixup16) {\r
+ *Fixup16 = (UINT16) (*Fixup16 + ((UINT16) ((UINT32) Adjust >> 16)));\r
+ }\r
+\r
+ FixupData = FixupData + sizeof (UINT16);\r
+ break;\r
+\r
+ case EFI_IMAGE_REL_BASED_LOW:\r
+ Fixup16 = (UINT16 *) Fixup;\r
+ if (*(UINT16 *) FixupData == *Fixup16) {\r
+ *Fixup16 = (UINT16) (*Fixup16 + ((UINT16) Adjust & 0xffff));\r
+ }\r
+\r
+ FixupData = FixupData + sizeof (UINT16);\r
+ break;\r
+\r
+ case EFI_IMAGE_REL_BASED_HIGHLOW:\r
+ Fixup32 = (UINT32 *) Fixup;\r
+ FixupData = ALIGN_POINTER (FixupData, sizeof (UINT32));\r
+ if (*(UINT32 *) FixupData == *Fixup32) {\r
+ *Fixup32 = *Fixup32 + (UINT32) Adjust;\r
+ }\r
+\r
+ FixupData = FixupData + sizeof (UINT32);\r
+ break;\r
+\r
+ case EFI_IMAGE_REL_BASED_DIR64:\r
+ Fixup64 = (UINT64 *)Fixup;\r
+ FixupData = ALIGN_POINTER (FixupData, sizeof (UINT64));\r
+ if (*(UINT64 *) FixupData == *Fixup64) {\r
+ *Fixup64 = *Fixup64 + (UINT64)Adjust;\r
+ }\r
+\r
+ FixupData = FixupData + sizeof (UINT64);\r
+ break;\r
+\r
+ case EFI_IMAGE_REL_BASED_HIGHADJ:\r
+ //\r
+ // Not valid Relocation type for UEFI image, ASSERT\r
+ //\r
+ ASSERT (FALSE);\r
+ break;\r
+\r
+ default:\r
+ //\r
+ // Only Itanium requires ConvertPeImage_Ex\r
+ //\r
+ Status = PeHotRelocateImageEx (Reloc, Fixup, &FixupData, Adjust);\r
+ if (RETURN_ERROR (Status)) {\r
+ return ;\r
+ }\r
+ }\r
+ //\r
+ // Next relocation record\r
+ //\r
+ Reloc += 1;\r
+ }\r
+ //\r
+ // next reloc block\r
+ //\r
+ RelocBase = (EFI_IMAGE_BASE_RELOCATION *) RelocEnd;\r
+ }\r
+}\r
+\r
+\r
+/**\r
+ Reads contents of a PE/COFF image from a buffer in system memory.\r
+ \r
+ This is the default implementation of a PE_COFF_LOADER_READ_FILE function \r
+ that assumes FileHandle pointer to the beginning of a PE/COFF image. \r
+ This function reads contents of the PE/COFF image that starts at the system memory \r
+ address specified by FileHandle. The read operation copies ReadSize bytes from the \r
+ PE/COFF image starting at byte offset FileOffset into the buffer specified by Buffer. \r
+ The size of the buffer actually read is returned in ReadSize.\r
+ \r
+ If FileHandle is NULL, then ASSERT().\r
+ If ReadSize is NULL, then ASSERT().\r
+ If Buffer is NULL, then ASSERT().\r
+\r
+ @param FileHandle The pointer to base of the input stream\r
+ @param FileOffset Offset into the PE/COFF image to begin the read operation.\r
+ @param ReadSize On input, the size in bytes of the requested read operation. \r
+ On output, the number of bytes actually read.\r
+ @param Buffer Output buffer that contains the data read from the PE/COFF image.\r
+\r
+ @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into \r
+ the buffer.\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+PeCoffLoaderImageReadFromMemory (\r
+ IN VOID *FileHandle,\r
+ IN UINTN FileOffset,\r
+ IN OUT UINTN *ReadSize,\r
+ OUT VOID *Buffer\r
+ )\r
+{\r
+ ASSERT (ReadSize != NULL);\r
+ ASSERT (FileHandle != NULL);\r
+ ASSERT (Buffer != NULL);\r
+\r
+ CopyMem (Buffer, ((UINT8 *)FileHandle) + FileOffset, *ReadSize);\r
+ return RETURN_SUCCESS;\r
+}\r
+\r
+/**\r
+ Unloads a loaded PE/COFF image from memory and releases its taken resource.\r
+ Releases any environment specific resources that were allocated when the image \r
+ specified by ImageContext was loaded using PeCoffLoaderLoadImage(). \r
+ \r
+ For NT32 emulator, the PE/COFF image loaded by system needs to release.\r
+ For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded, \r
+ this function can simply return RETURN_SUCCESS.\r
+ \r
+ If ImageContext is NULL, then ASSERT().\r
+ \r
+ @param ImageContext The pointer to the image context structure that describes the PE/COFF\r
+ image to be unloaded.\r
+\r
+ @retval RETURN_SUCCESS The PE/COFF image was unloaded successfully.\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+PeCoffLoaderUnloadImage (\r
+ IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
+ )\r
+{\r
+ //\r
+ // Applies additional environment specific actions to unload a \r
+ // PE/COFF image if needed\r
+ //\r
+ PeCoffLoaderUnloadImageExtraAction (ImageContext);\r
+ return RETURN_SUCCESS;\r
+}\r