Host.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*++ @file
  2. Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
  3. Portions copyright (c) 2008 - 2011, Apple Inc. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #ifndef _SEC_MAIN_H__
  7. #define _SEC_MAIN_H__
  8. //
  9. // Name mangle to prevent build errors. I.e conflicts between EFI and OS
  10. //
  11. #define NTOHL _UNIX_EFI_NAME_MANGLE_NTOHL_
  12. #define NTOHLL _UNIX_EFI_NAME_MANGLE_NTOHLL_
  13. #define HTONL _UNIX_EFI_NAME_MANGLE_HTONL_
  14. #define HTONLL _UNIX_EFI_NAME_MANGLE_HTONLL_
  15. #define NTOHS _UNIX_EFI_NAME_MANGLE_NTOHS_
  16. #define HTONS _UNIX_EFI_NAME_MANGLE_HTOHS_
  17. #define B0 _UNIX_EFI_NAME_MANGLE_B0_
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <sys/termios.h>
  23. #include <sys/time.h>
  24. #if __CYGWIN__
  25. #include <sys/dirent.h>
  26. #else
  27. #include <sys/dir.h>
  28. #endif
  29. #include <sys/mman.h>
  30. #include <dlfcn.h>
  31. #include <unistd.h>
  32. #include <poll.h>
  33. #include <fcntl.h>
  34. #include <time.h>
  35. #include <signal.h>
  36. #include <errno.h>
  37. #include <string.h>
  38. #include <stdlib.h>
  39. #include <sys/ioctl.h>
  40. #include <sys/statvfs.h>
  41. #include <sys/socket.h>
  42. #include <netdb.h>
  43. #include <netinet/in.h>
  44. #include <net/if.h>
  45. #include <ifaddrs.h>
  46. #ifdef __APPLE__
  47. #include <net/if_dl.h>
  48. #include <net/bpf.h>
  49. #include <sys/param.h>
  50. #include <sys/mount.h>
  51. #include <sys/disk.h>
  52. #define _XOPEN_SOURCE
  53. #ifndef _Bool
  54. #define _Bool char // for clang debug
  55. #endif
  56. #else
  57. #include <termio.h>
  58. #include <sys/vfs.h>
  59. #include <linux/fs.h>
  60. #endif
  61. #include <utime.h>
  62. #undef NTOHL
  63. #undef NTOHLL
  64. #undef HTONL
  65. #undef HTONLL
  66. #undef NTOHS
  67. #undef HTONS
  68. #undef B0
  69. #undef CR3
  70. #include <PiPei.h>
  71. #include <Uefi.h>
  72. #include <Library/PeCoffLib.h>
  73. #include <Library/BaseLib.h>
  74. #include <Library/BaseMemoryLib.h>
  75. #include <Library/MemoryAllocationLib.h>
  76. #include <Library/PrintLib.h>
  77. #include <Library/PcdLib.h>
  78. #include <Library/DebugLib.h>
  79. #include <Library/ReportStatusCodeLib.h>
  80. #include <Library/ThunkPpiList.h>
  81. #include <Library/ThunkProtocolList.h>
  82. #include <Library/PeiServicesLib.h>
  83. #include <Library/PeCoffGetEntryPointLib.h>
  84. #include <Library/EmuMagicPageLib.h>
  85. #include <Ppi/EmuThunk.h>
  86. #include <Ppi/StatusCode.h>
  87. #include <Protocol/SimplePointer.h>
  88. #include <Protocol/SimpleTextIn.h>
  89. #include <Protocol/SimpleTextInEx.h>
  90. #include <Protocol/UgaDraw.h>
  91. #include <Protocol/SimpleFileSystem.h>
  92. #include <Protocol/EmuThunk.h>
  93. #include <Protocol/EmuIoThunk.h>
  94. #include <Protocol/EmuGraphicsWindow.h>
  95. #include <Protocol/EmuThread.h>
  96. #include <Protocol/EmuBlockIo.h>
  97. #include <Protocol/EmuSnp.h>
  98. #include <Guid/FileInfo.h>
  99. #include <Guid/FileSystemInfo.h>
  100. #include <Guid/FileSystemVolumeLabelInfo.h>
  101. #include "Gasket.h"
  102. #define STACK_SIZE 0x20000
  103. typedef struct {
  104. EFI_PHYSICAL_ADDRESS Address;
  105. UINT64 Size;
  106. } EMU_FD_INFO;
  107. typedef struct {
  108. EFI_PHYSICAL_ADDRESS Memory;
  109. UINT64 Size;
  110. } EMU_SYSTEM_MEMORY;
  111. #define MAX_IMAGE_CONTEXT_TO_MOD_HANDLE_ARRAY_SIZE 0x100
  112. typedef struct {
  113. PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext;
  114. VOID *ModHandle;
  115. } IMAGE_CONTEXT_TO_MOD_HANDLE;
  116. EFI_STATUS
  117. EFIAPI
  118. SecUnixPeiLoadFile (
  119. VOID *Pe32Data,
  120. EFI_PHYSICAL_ADDRESS *ImageAddress,
  121. UINT64 *ImageSize,
  122. EFI_PHYSICAL_ADDRESS *EntryPoint
  123. );
  124. int
  125. main (
  126. IN int Argc,
  127. IN char **Argv,
  128. IN char **Envp
  129. );
  130. VOID
  131. SecLoadFromCore (
  132. IN UINTN LargestRegion,
  133. IN UINTN LargestRegionSize,
  134. IN UINTN BootFirmwareVolumeBase,
  135. IN VOID *PeiCoreFile
  136. );
  137. EFI_STATUS
  138. SecLoadFile (
  139. IN VOID *Pe32Data,
  140. IN EFI_PHYSICAL_ADDRESS *ImageAddress,
  141. IN UINT64 *ImageSize,
  142. IN EFI_PHYSICAL_ADDRESS *EntryPoint
  143. );
  144. EFI_STATUS
  145. SecFfsFindPeiCore (
  146. IN EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader,
  147. OUT VOID **Pe32Data
  148. );
  149. EFI_STATUS
  150. SecFfsFindNextFile (
  151. IN EFI_FV_FILETYPE SearchType,
  152. IN EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader,
  153. IN OUT EFI_FFS_FILE_HEADER **FileHeader
  154. );
  155. EFI_STATUS
  156. SecFfsFindSectionData (
  157. IN EFI_SECTION_TYPE SectionType,
  158. IN EFI_FFS_FILE_HEADER *FfsFileHeader,
  159. IN OUT VOID **SectionData
  160. );
  161. EFI_STATUS
  162. EFIAPI
  163. SecUnixPeCoffLoaderLoadAsDll (
  164. IN CHAR8 *PdbFileName,
  165. IN VOID **ImageEntryPoint,
  166. OUT VOID **ModHandle
  167. );
  168. EFI_STATUS
  169. EFIAPI
  170. SecUnixPeCoffLoaderFreeLibrary (
  171. OUT VOID *ModHandle
  172. );
  173. EFI_STATUS
  174. SecUnixFdAddress (
  175. IN UINTN Index,
  176. IN OUT EFI_PHYSICAL_ADDRESS *FdBase,
  177. IN OUT UINT64 *FdSize,
  178. IN OUT EFI_PHYSICAL_ADDRESS *FixUp
  179. )
  180. ;
  181. EFI_STATUS
  182. EFIAPI
  183. GasketSecUnixFdAddress (
  184. IN UINTN Index,
  185. IN OUT EFI_PHYSICAL_ADDRESS *FdBase,
  186. IN OUT UINT64 *FdSize,
  187. IN OUT EFI_PHYSICAL_ADDRESS *FixUp
  188. )
  189. ;
  190. EFI_STATUS
  191. GetImageReadFunction (
  192. IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext,
  193. IN EFI_PHYSICAL_ADDRESS *TopOfMemory
  194. );
  195. EFI_STATUS
  196. EFIAPI
  197. SecImageRead (
  198. IN VOID *FileHandle,
  199. IN UINTN FileOffset,
  200. IN OUT UINTN *ReadSize,
  201. OUT VOID *Buffer
  202. );
  203. CHAR16 *
  204. AsciiToUnicode (
  205. IN CHAR8 *Ascii,
  206. IN UINTN *StrLen OPTIONAL
  207. );
  208. UINTN
  209. CountSeparatorsInString (
  210. IN const CHAR16 *String,
  211. IN CHAR16 Separator
  212. );
  213. EFI_STATUS
  214. EFIAPI
  215. SecTemporaryRamSupport (
  216. IN CONST EFI_PEI_SERVICES **PeiServices,
  217. IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase,
  218. IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase,
  219. IN UINTN CopySize
  220. );
  221. EFI_STATUS
  222. EFIAPI
  223. GasketSecTemporaryRamSupport (
  224. IN CONST EFI_PEI_SERVICES **PeiServices,
  225. IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase,
  226. IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase,
  227. IN UINTN CopySize
  228. );
  229. RETURN_STATUS
  230. EFIAPI
  231. SecPeCoffGetEntryPoint (
  232. IN VOID *Pe32Data,
  233. IN OUT VOID **EntryPoint
  234. );
  235. VOID
  236. EFIAPI
  237. SecPeCoffRelocateImageExtraAction (
  238. IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
  239. );
  240. VOID
  241. EFIAPI
  242. SecPeCoffLoaderUnloadImageExtraAction (
  243. IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
  244. );
  245. VOID
  246. PeiSwitchStacks (
  247. IN SWITCH_STACK_ENTRY_POINT EntryPoint,
  248. IN VOID *Context1 OPTIONAL,
  249. IN VOID *Context2 OPTIONAL,
  250. IN VOID *NewStack
  251. );
  252. VOID
  253. SecInitThunkProtocol (
  254. VOID
  255. );
  256. EFI_PHYSICAL_ADDRESS *
  257. MapMemory (
  258. INTN fd,
  259. UINT64 length,
  260. INTN prot,
  261. INTN flags
  262. );
  263. EFI_STATUS
  264. MapFile (
  265. IN CHAR8 *FileName,
  266. IN OUT EFI_PHYSICAL_ADDRESS *BaseAddress,
  267. OUT UINT64 *Length
  268. );
  269. EFI_STATUS
  270. MapFd0 (
  271. IN CHAR8 *FileName,
  272. IN OUT EFI_PHYSICAL_ADDRESS *BaseAddress,
  273. OUT UINT64 *Length
  274. );
  275. BOOLEAN
  276. EfiSystemMemoryRange (
  277. IN VOID *MemoryAddress
  278. );
  279. VOID
  280. SecSleep (
  281. UINT64 Nanoseconds
  282. );
  283. VOID
  284. SecEnableInterrupt (
  285. VOID
  286. );
  287. VOID
  288. SecDisableInterrupt (
  289. VOID
  290. );
  291. BOOLEAN
  292. SecInterruptEanbled (
  293. VOID
  294. );
  295. extern EMU_THUNK_PROTOCOL gEmuThunkProtocol;
  296. extern EMU_IO_THUNK_PROTOCOL gX11ThunkIo;
  297. extern EMU_IO_THUNK_PROTOCOL gPosixFileSystemThunkIo;
  298. extern EMU_IO_THUNK_PROTOCOL gPthreadThunkIo;
  299. extern EMU_IO_THUNK_PROTOCOL gBlockIoThunkIo;
  300. extern EMU_IO_THUNK_PROTOCOL gSnpThunkIo;
  301. #endif