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