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