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