imagep.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*++
  2. Copyright (c) 2014 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. imagep.h
  5. Abstract:
  6. This header contains internal UEFI image loading definitions.
  7. Author:
  8. Evan Green 12-Mar-2014
  9. --*/
  10. //
  11. // ------------------------------------------------------------------- Includes
  12. //
  13. #include "peimage.h"
  14. #include <minoca/uefi/protocol/loadimg.h>
  15. //
  16. // ---------------------------------------------------------------- Definitions
  17. //
  18. #define EFI_IMAGE_FILE_HANDLE_MAGIC 0x46676D49 // 'FgmI'
  19. #define EFI_IMAGE_DATA_MAGIC 0x67616D49 // 'gamI'
  20. #define EFI_LOAD_PE_IMAGE_ATTRIBUTE_NONE 0x00
  21. #define EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION 0x01
  22. #define EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION 0x02
  23. //
  24. // Return status codes from the PE/COFF Loader services
  25. //
  26. #define IMAGE_ERROR_SUCCESS 0
  27. #define IMAGE_ERROR_IMAGE_READ 1
  28. #define IMAGE_ERROR_INVALID_PE_HEADER_SIGNATURE 2
  29. #define IMAGE_ERROR_INVALID_MACHINE_TYPE 3
  30. #define IMAGE_ERROR_INVALID_SUBSYSTEM 4
  31. #define IMAGE_ERROR_INVALID_IMAGE_ADDRESS 5
  32. #define IMAGE_ERROR_INVALID_IMAGE_SIZE 6
  33. #define IMAGE_ERROR_INVALID_SECTION_ALIGNMENT 7
  34. #define IMAGE_ERROR_SECTION_NOT_LOADED 8
  35. #define IMAGE_ERROR_FAILED_RELOCATION 9
  36. #define IMAGE_ERROR_FAILED_ICACHE_FLUSH 10
  37. #define IMAGE_ERROR_UNSUPPORTED 11
  38. //
  39. // ------------------------------------------------------ Data Type Definitions
  40. //
  41. /*++
  42. Structure Description:
  43. This structure stores the internal information about loaded image.
  44. Members:
  45. Magic - Stores the magic constant EFI_IMAGE_FILE_HANDLE_MAGIC.
  46. FreeBuffer - Stores a boolean indicating if the buffer needs to be freed
  47. or not.
  48. Source - Stores a pointer to the file buffer.
  49. SourceSize - Stores the size of the buffer in bytes.
  50. --*/
  51. typedef struct _EFI_IMAGE_FILE_HANDLE {
  52. UINT32 Magic;
  53. BOOLEAN FreeBuffer;
  54. VOID *Source;
  55. UINTN SourceSize;
  56. } EFI_IMAGE_FILE_HANDLE, *PEFI_IMAGE_FILE_HANDLE;
  57. typedef
  58. RETURN_STATUS
  59. (EFIAPI *PE_COFF_LOADER_READ_FILE) (
  60. VOID *FileHandle,
  61. UINTN FileOffset,
  62. UINTN *ReadSize,
  63. VOID *Buffer
  64. );
  65. /*++
  66. Routine Description:
  67. This routine reads contents of the PE/COFF image specified by the given
  68. file handle. The read operation copies the given number of bytes from the
  69. PE/COFF image starting at the given file offset into the given buffer. The
  70. size of the buffer actually read is returned in the read size parameter. If
  71. the file offset specifies an offset past the end of the PE/COFF image, a
  72. read size of 0 is returned. This function abstracts access to a PE/COFF
  73. image so it can be implemented in an environment specific manner.
  74. For example, SEC and PEI environments may access memory directly to read
  75. the contents of a PE/COFF image, and DXE or UEFI environments may require
  76. protocol services to read the contents of PE/COFF image stored on FLASH,
  77. disk, or network devices.
  78. Arguments:
  79. FileHandle - Supplies a pointer to the file handle to read from.
  80. FileOffset - Supplies an offset in bytes from the beginning of the file to
  81. read.
  82. ReadSize - Supplies a pointer that on input contains the number of bytes to
  83. read. On output, returns the number of bytes read.
  84. Buffer - Supplies the buffer where the read data will be returned.
  85. Return Value:
  86. EFI_SUCCESS on success.
  87. EFI_DEVICE_ERROR if the image could not be read.
  88. --*/
  89. /*++
  90. Structure Description:
  91. This structure stores the internal information about loaded image.
  92. Members:
  93. ImageAddress - Stores the base address of the image from the PE/COFF header.
  94. ImageSize - Stores the image size from the PE/COFF header.
  95. DestinatinAddress - Stores the optional address to relocate to. If zero,
  96. the image address will be used as the base.
  97. EntryPoint - Stores the address of the image entry point.
  98. ImageRead - Stores a pointer to a function used to access the image.
  99. Handle - Stores the handle passed in to the image read function.
  100. FixupData - Stores a pointer to the relocation information.
  101. SectionAlignment - Stores the section alignment from the PE/COFF header.
  102. PeCoffHeaderOffset - Stores the offset to the PE/COFF header.
  103. DebugDirectoryEntryRva - Stores the Relative Virtual Address of the debug
  104. directory, if it exists.
  105. CodeView - Stores a pointer to the CodeView area of the PE/COFF debug
  106. directory.
  107. PdbPointer - Stores a pointer to the PDB entry contained in the CodeView
  108. area, which describes a filename of the PDB used for source-level
  109. debug of an image.
  110. SizeOfHeaders - Stores the size of the headers from the PE/COFF header.
  111. ImageCodeMemoryType - Stores the type of memory to use for code.
  112. ImageDataMemoryType - Stores the type of memory to use for data.
  113. ImageError - Stores the error code encountered while loading the image.
  114. FixupDataSize - Stores the size of the fixup data that the caller must
  115. allocate before calling the relocate image function.
  116. Machine - Stores the machine type from the PE/COFF header.
  117. RelocationsStripped - Stores a boolean indicating if the image does not
  118. have any relocation information.
  119. IsTeImage - Stores a boolean indicating if this is a TE image.
  120. HiiResourceData - Stores the address of the HII resource data.
  121. Context - Stores implementation specific context.
  122. --*/
  123. typedef struct _EFI_PE_LOADER_CONTEXT {
  124. PHYSICAL_ADDRESS ImageAddress;
  125. UINT64 ImageSize;
  126. PHYSICAL_ADDRESS DestinationAddress;
  127. PHYSICAL_ADDRESS EntryPoint;
  128. PE_COFF_LOADER_READ_FILE ImageRead;
  129. VOID *Handle;
  130. VOID *FixupData;
  131. UINT32 SectionAlignment;
  132. UINT32 PeCoffHeaderOffset;
  133. UINT32 DebugDirectoryEntryRva;
  134. VOID *CodeView;
  135. CHAR8 *PdbPointer;
  136. UINTN SizeOfHeaders;
  137. UINT32 ImageCodeMemoryType;
  138. UINT32 ImageDataMemoryType;
  139. UINT32 ImageError;
  140. UINTN FixupDataSize;
  141. UINT16 Machine;
  142. UINT16 ImageType;
  143. BOOLEAN RelocationsStripped;
  144. BOOLEAN IsTeImage;
  145. PHYSICAL_ADDRESS HiiResourceData;
  146. UINT64 Context;
  147. } EFI_PE_LOADER_CONTEXT, *PEFI_PE_LOADER_CONTEXT;
  148. /*++
  149. Structure Description:
  150. This structure stores the internal information about loaded image.
  151. Members:
  152. Magic - Stores the magic constant EFI_IMAGE_DATA_MAGIC.
  153. Handle - Stores the image handle.
  154. Type - Stores the image type.
  155. Started - Stores a boolean indicating if the entry point has been invoked.
  156. EntryPoint - Stores the address of the image entry point.
  157. Information - Stores the loaded image protocol data.
  158. ImageBasePage - Stores the address where the image was loaded.
  159. ImagePageCount - Stores the size of the in-memory image in pages.
  160. FixupData - Stores a pointer to the relocation information.
  161. Tpl - Stores the TPL of the started image.
  162. Status - Stores the status returned by the started image.
  163. ExitDataSize - Stores the size of the exit data returned from the started
  164. image.
  165. ExitData - Stores the exit data returned from the started image.
  166. JumpBuffer - Stores a pointer to pool allocation for context save and
  167. restore.
  168. JumpContext - Stores a pointer to a buffer used for context save and
  169. restore.
  170. Machine - Stores the machine type from the PE image.
  171. RuntimeData - Stores a pointer to the runtime image entry.
  172. LoadedImageDevicePath - Stores a pointer to the loaded image device path
  173. protocol.
  174. ImageContext - Stores the PE/COFF loader image data.
  175. DebuggerData - Stores a pointer to the debugger image context.
  176. LoadImageStatus - Stores the status returned by the LoadImage service.
  177. --*/
  178. typedef struct _EFI_IMAGE_DATA {
  179. UINT32 Magic;
  180. EFI_HANDLE Handle;
  181. UINTN Type;
  182. BOOLEAN Started;
  183. EFI_IMAGE_ENTRY_POINT EntryPoint;
  184. EFI_LOADED_IMAGE_PROTOCOL Information;
  185. EFI_PHYSICAL_ADDRESS ImageBasePage;
  186. UINTN ImagePageCount;
  187. CHAR8 *FixupData;
  188. EFI_TPL Tpl;
  189. EFI_STATUS Status;
  190. UINTN ExitDataSize;
  191. VOID *ExitData;
  192. VOID *JumpBuffer;
  193. VOID *JumpContext;
  194. UINT16 Machine;
  195. EFI_RUNTIME_IMAGE_ENTRY *RuntimeData;
  196. EFI_DEVICE_PATH_PROTOCOL *LoadedImageDevicePath;
  197. EFI_PE_LOADER_CONTEXT ImageContext;
  198. PVOID DebuggerData;
  199. EFI_STATUS LoadImageStatus;
  200. } EFI_IMAGE_DATA, *PEFI_IMAGE_DATA;
  201. //
  202. // -------------------------------------------------------------------- Globals
  203. //
  204. //
  205. // -------------------------------------------------------- Function Prototypes
  206. //
  207. EFIAPI
  208. RETURN_STATUS
  209. EfiPeLoaderLoadImage (
  210. PEFI_PE_LOADER_CONTEXT Context
  211. );
  212. /*++
  213. Routine Description:
  214. This routine loads a PE/COFF image.
  215. Arguments:
  216. Context - Supplies a pointer to the image context. Before calling this
  217. function the caller must have allocated the load buffer and filled in
  218. the image address and size fields.
  219. Return Value:
  220. RETURN_SUCCESS on success.
  221. RETURN_INVALID_PARAMETER if the image address is invalid.
  222. RETURN_LOAD_ERROR if the image is a PE/COFF runtime image with no
  223. relocations.
  224. RETURN_BUFFER_TOO_SMALL if the caller provided buffer was not large enough.
  225. --*/
  226. EFIAPI
  227. RETURN_STATUS
  228. EfiPeLoaderRelocateImage (
  229. PEFI_PE_LOADER_CONTEXT Context
  230. );
  231. /*++
  232. Routine Description:
  233. This routine relocates a loaded PE image.
  234. Arguments:
  235. Context - Supplies a pointer to the image context.
  236. Return Value:
  237. RETURN_SUCCESS on success.
  238. RETURN_LOAD_ERROR if the image is not valid.
  239. RETURN_UNSUPPORTED if an unsupported relocation type was encountered.
  240. --*/
  241. EFIAPI
  242. RETURN_STATUS
  243. EfiPeLoaderGetImageInfo (
  244. PEFI_PE_LOADER_CONTEXT Context
  245. );
  246. /*++
  247. Routine Description:
  248. This routine extracts information about the given PE/COFF image.
  249. Arguments:
  250. Context - Supplies a pointer to the image context.
  251. Return Value:
  252. RETURN_SUCCESS on success.
  253. RETURN_INVALID_PARAMETER if the image context is NULL.
  254. RETURN_UNSUPPORTED if the image format is not supported.
  255. --*/
  256. EFIAPI
  257. RETURN_STATUS
  258. EfiPeLoaderUnloadImage (
  259. PEFI_PE_LOADER_CONTEXT Context
  260. );
  261. /*++
  262. Routine Description:
  263. This routine unloads the PE/COFF image.
  264. Arguments:
  265. Context - Supplies a pointer to the image context.
  266. Return Value:
  267. RETURN_* status code.
  268. --*/
  269. UINT16
  270. EfiPeLoaderGetPeHeaderMagicValue (
  271. EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Header
  272. );
  273. /*++
  274. Routine Description:
  275. This routine returns the magic value out of the PE/COFF header.
  276. Arguments:
  277. Header - Supplies a pointer to the header.
  278. Return Value:
  279. Returns the magic value from the header.
  280. --*/
  281. RETURN_STATUS
  282. EfiPeLoaderGetPeHeader (
  283. PEFI_PE_LOADER_CONTEXT Context,
  284. EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Header
  285. );
  286. /*++
  287. Routine Description:
  288. This routine retrieves the PE or TE header from a PE/COFF or TE image.
  289. Arguments:
  290. Context - Supplies a pointer to the loader context.
  291. Header - Supplies a pointer to the header.
  292. Return Value:
  293. RETURN_* error code.
  294. --*/