]> git.proxmox.com Git - mirror_edk2.git/blobdiff - InOsEmuPkg/Unix/Sec/SecMain.c
InOsEmuPkg: Make build dir different for IA-32. Fix PCD XIP issues.
[mirror_edk2.git] / InOsEmuPkg / Unix / Sec / SecMain.c
index 8677ab59382ff3c4f2316fbd7990ca173d2a8ad3..bf594892fb6c25a90a65d328bf2f46a466dec7c7 100644 (file)
@@ -27,13 +27,9 @@ char *gGdbWorkingFileName = NULL;
 EMU_THUNK_PPI mSecEmuThunkPpi = {
   GasketSecUnixPeiAutoScan,
   GasketSecUnixFdAddress,
-  GasketSecEmuThunkAddress,
-  GasketSecUnixPeiLoadFile
+  GasketSecEmuThunkAddress
 };
 
-EFI_PEI_TEMPORARY_RAM_SUPPORT_PPI mSecTemporaryRamSupportPpi = { 
-  GasketSecTemporaryRamSupport
-};
 
 
 
@@ -44,8 +40,8 @@ EFI_PEI_TEMPORARY_RAM_SUPPORT_PPI mSecTemporaryRamSupportPpi = {
 //  The number of array elements is allocated base on parsing
 //  EFI_FIRMWARE_VOLUMES and the memory is never freed.
 //
-UINTN                                     gFdInfoCount = 0;
-EMU_FD_INFO                              *gFdInfo;
+UINTN       gFdInfoCount = 0;
+EMU_FD_INFO *gFdInfo;
 
 //
 // Array that supports seperate memory rantes.
@@ -53,43 +49,16 @@ EMU_FD_INFO                              *gFdInfo;
 //  The number of array elements is allocated base on parsing
 //  EFI_MEMORY_SIZE and the memory is never freed.
 //
-UINTN                                     gSystemMemoryCount = 0;
-EMU_SYSTEM_MEMORY                       *gSystemMemory;
+UINTN              gSystemMemoryCount = 0;
+EMU_SYSTEM_MEMORY  *gSystemMemory;
 
 
 
 UINTN                        mImageContextModHandleArraySize = 0;
 IMAGE_CONTEXT_TO_MOD_HANDLE  *mImageContextModHandleArray = NULL;
 
+EFI_PEI_PPI_DESCRIPTOR  *gPpiList;
 
-
-EFI_PHYSICAL_ADDRESS *
-MapMemory (
-  INTN fd,
-  UINT64 length,
-  INTN   prot,
-  INTN   flags);
-
-EFI_STATUS
-MapFile (
-  IN  CHAR8                     *FileName,
-  IN OUT  EFI_PHYSICAL_ADDRESS  *BaseAddress,
-  OUT UINT64                    *Length
-  );
-
-EFI_STATUS
-EFIAPI
-SecNt32PeCoffRelocateImage (
-  IN OUT PE_COFF_LOADER_IMAGE_CONTEXT         *ImageContext
-  );
-
-
-int
-main (
-  IN  int   Argc,
-  IN  char  **Argv,
-  IN  char  **Envp
-  )
 /*++
 
 Routine Description:
@@ -105,6 +74,12 @@ Returns:
   1 - Abnormal exit
 
 **/
+int
+main (
+  IN  int   Argc,
+  IN  char  **Argv,
+  IN  char  **Envp
+  )
 {
   EFI_STATUS            Status;
   EFI_PHYSICAL_ADDRESS  InitialStackMemory;
@@ -115,25 +90,24 @@ Returns:
   UINTN                 PeiIndex;
   CHAR8                 *FileName;
   BOOLEAN               Done;
-  VOID                  *PeiCoreFile;
+  EFI_PEI_FILE_HANDLE   FileHandle;
+  VOID                  *SecFile;
   CHAR16                *MemorySizeStr;
   CHAR16                *FirmwareVolumesStr;
   UINTN                 *StackPointer;
 
-  setbuf(stdout, 0);
-  setbuf(stderr, 0);
+  setbuf (stdout, 0);
+  setbuf (stderr, 0);
 
   MemorySizeStr      = (CHAR16 *) PcdGetPtr (PcdEmuMemorySize);
   FirmwareVolumesStr = (CHAR16 *) PcdGetPtr (PcdEmuFirmwareVolume);
 
-  printf ("\nEDK SEC Main UNIX Emulation Environment from edk2.sourceforge.net\n");
+  printf ("\nEDK II UNIX Emulation Environment from edk2.sourceforge.net\n");
 
   //
   // PPIs pased into PEI_CORE
   //
-  AddThunkPpi (EFI_PEI_PPI_DESCRIPTOR_PPI, &gEfiTemporaryRamSupportPpiGuid, &mSecTemporaryRamSupportPpi);
   AddThunkPpi (EFI_PEI_PPI_DESCRIPTOR_PPI, &gEmuThunkPpiGuid, &mSecEmuThunkPpi);
-  AddThunkPpi (EFI_PEI_PPI_DESCRIPTOR_PPI, &gEmuPeiServicesTableUpdatePpiGuid, NULL);
 
   SecInitThunkProtocol ();
   
@@ -142,13 +116,18 @@ Returns:
   //
   AddThunkProtocol (&gX11ThunkIo, (CHAR16 *)PcdGetPtr (PcdEmuGop), TRUE); 
   AddThunkProtocol (&gPosixFileSystemThunkIo, (CHAR16 *)PcdGetPtr (PcdEmuFileSystem), TRUE); 
-  
+  AddThunkProtocol (&gBlockIoThunkIo, (CHAR16 *)PcdGetPtr (PcdEmuVirtualDisk), TRUE); 
+  AddThunkProtocol (&gSnpThunkIo, (CHAR16 *)PcdGetPtr (PcdEmuNetworkInterface), TRUE); 
+
   //
   // Emulator other Thunks
   //
   AddThunkProtocol (&gPthreadThunkIo, (CHAR16 *)PcdGetPtr (PcdEmuApCount), FALSE); 
 
   // EmuSecLibConstructor ();
+  
+  gPpiList = GetThunkPpiList (); 
+
 
 #ifdef __APPLE__
   //
@@ -185,23 +164,24 @@ Returns:
   printf ("  BootMode 0x%02x\n", (unsigned int)PcdGet32 (PcdEmuBootMode));
 
   //
-  // Open up a 128K file to emulate temp memory for PEI.
+  // Open up a 128K file to emulate temp memory for SEC.
   //  on a real platform this would be SRAM, or using the cache as RAM.
   //  Set InitialStackMemory to zero so UnixOpenFile will allocate a new mapping
   //
   InitialStackMemorySize  = STACK_SIZE;
-  InitialStackMemory = (UINTN)MapMemory(0,
-          (UINT32) InitialStackMemorySize,
-          PROT_READ | PROT_WRITE | PROT_EXEC,
-          MAP_ANONYMOUS | MAP_PRIVATE);
+  InitialStackMemory = (UINTN)MapMemory (
+                                0, (UINT32) InitialStackMemorySize,
+                                PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE
+                                );
   if (InitialStackMemory == 0) {
     printf ("ERROR : Can not open SecStack Exiting\n");
     exit (1);
   }
 
-  printf ("  SEC passing in %u KB of temp RAM at 0x%08lx to PEI\n",
+  printf ("  OS Emulator passing in %u KB of temp RAM at 0x%08lx to SEC\n",
     (unsigned int)(InitialStackMemorySize / 1024),
-    (unsigned long)InitialStackMemory);
+    (unsigned long)InitialStackMemory
+    );
 
   for (StackPointer = (UINTN*) (UINTN) InitialStackMemory;
      StackPointer < (UINTN*)(UINTN)((UINTN) InitialStackMemory + (UINT64) InitialStackMemorySize);
@@ -219,40 +199,59 @@ Returns:
   }
 
   Index2 = 0;
-  for (Done = FALSE, Index = 0, PeiIndex = 0, PeiCoreFile = NULL;
+  for (Done = FALSE, Index = 0, PeiIndex = 0, SecFile = NULL;
        FirmwareVolumesStr[Index2] != 0;
        Index++) {
-    for (Index1 = 0; (FirmwareVolumesStr[Index2] != '!') && (FirmwareVolumesStr[Index2] != 0); Index2++)
+    for (Index1 = 0; (FirmwareVolumesStr[Index2] != '!') && (FirmwareVolumesStr[Index2] != 0); Index2++) {
       FileName[Index1++] = FirmwareVolumesStr[Index2];
-    if (FirmwareVolumesStr[Index2] == '!')
+    }
+    if (FirmwareVolumesStr[Index2] == '!') {
       Index2++;
+    }
     FileName[Index1]  = '\0';
 
-    //
-    // Open the FD and remmeber where it got mapped into our processes address space
-    //
-    Status = MapFile (
-          FileName,
-          &gFdInfo[Index].Address,
-          &gFdInfo[Index].Size
-          );
+    if (Index == 0) {
+      // Map FV Recovery Read Only and other areas Read/Write
+      Status = MapFd0 (
+                FileName,
+                &gFdInfo[0].Address,
+                &gFdInfo[0].Size
+                );
+    } else {
+      //
+      // Open the FD and remmeber where it got mapped into our processes address space
+      // Maps Read Only
+      //
+      Status = MapFile (
+                FileName,
+                &gFdInfo[Index].Address,
+                &gFdInfo[Index].Size
+                );
+    }
     if (EFI_ERROR (Status)) {
       printf ("ERROR : Can not open Firmware Device File %s (%x).  Exiting.\n", FileName, (unsigned int)Status);
       exit (1);
     }
 
-    printf ("  FD loaded from %s at 0x%08lx",
-      FileName, (unsigned long)gFdInfo[Index].Address);
+    printf ("  FD loaded from %s at 0x%08lx",FileName, (unsigned long)gFdInfo[Index].Address);
 
-    if (PeiCoreFile == NULL) {
+    if (SecFile == NULL) {
       //
-      // Assume the beginning of the FD is an FV and look for the PEI Core.
+      // Assume the beginning of the FD is an FV and look for the SEC Core.
       // Load the first one we find.
       //
-      Status = SecFfsFindPeiCore ((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) gFdInfo[Index].Address, &PeiCoreFile);
+      FileHandle = NULL;
+      Status = PeiServicesFfsFindNextFile (
+                  EFI_FV_FILETYPE_SECURITY_CORE, 
+                  (EFI_PEI_FV_HANDLE)(UINTN)gFdInfo[Index].Address, 
+                  &FileHandle
+                  );
       if (!EFI_ERROR (Status)) {
-        PeiIndex = Index;
-        printf (" contains SEC Core");
+        Status = PeiServicesFfsFindSectionData (EFI_SECTION_PE32, FileHandle, &SecFile);
+        if (!EFI_ERROR (Status)) {
+          PeiIndex = Index;
+          printf (" contains SEC Core");
+        }
       }
     }
 
@@ -275,32 +274,35 @@ Returns:
       Index1++;
     }
     gSystemMemory[Index++].Size = val * 0x100000;
-    if (MemorySizeStr[Index1] == 0)
+    if (MemorySizeStr[Index1] == 0) {
       break;
+    }
     Index1++;
   }
 
   printf ("\n");
 
   //
-  // Hand off to PEI Core
+  // Hand off to SEC
   //
-  SecLoadFromCore ((UINTN) InitialStackMemory, (UINTN) InitialStackMemorySize, (UINTN) gFdInfo[0].Address, PeiCoreFile);
+  SecLoadFromCore ((UINTN) InitialStackMemory, (UINTN) InitialStackMemorySize, (UINTN) gFdInfo[0].Address, SecFile);
 
   //
-  // If we get here, then the PEI Core returned. This is an error as PEI should
-  //  always hand off to DXE.
+  // If we get here, then the SEC Core returned. This is an error as SEC should
+  //  always hand off to PEI Core and then on to DXE Core.
   //
-  printf ("ERROR : PEI Core returned\n");
+  printf ("ERROR : SEC returned\n");
   exit (1);
 }
 
+
 EFI_PHYSICAL_ADDRESS *
 MapMemory (
-  INTN fd,
-  UINT64 length,
-  INTN   prot,
-  INTN   flags)
+  IN INTN   fd,
+  IN UINT64 length,
+  IN INTN   prot,
+  IN INTN   flags
+  )
 {
   STATIC UINTN base  = 0x40000000;
   CONST UINTN  align = (1 << 24);
@@ -318,8 +320,7 @@ MapMemory (
     }
     if ((((UINTN)res) & ~(align-1)) == (UINTN)res) {
       isAligned=1;
-    }
-    else {
+    } else {
       munmap(res, length);
       base += align;
     }
@@ -327,12 +328,7 @@ MapMemory (
   return res;
 }
 
-EFI_STATUS
-MapFile (
-  IN  CHAR8                     *FileName,
-  IN OUT  EFI_PHYSICAL_ADDRESS  *BaseAddress,
-  OUT UINT64                    *Length
-  )
+
 /*++
 
 Routine Description:
@@ -356,61 +352,162 @@ Returns:
   EFI_DEVICE_ERROR - An error occured attempting to map the opened file
 
 **/
+EFI_STATUS
+MapFile (
+  IN  CHAR8                     *FileName,
+  IN OUT  EFI_PHYSICAL_ADDRESS  *BaseAddress,
+  OUT UINT64                    *Length
+  )
 {
-  int fd;
+  int     fd;
   VOID    *res;
   UINTN   FileSize;
 
-  fd = open (FileName, O_RDONLY);
-  if (fd < 0)
+  fd = open (FileName, O_RDWR);
+  if (fd < 0) {
     return EFI_NOT_FOUND;
+  }
   FileSize = lseek (fd, 0, SEEK_END);
 
 
-  res = MapMemory(fd, FileSize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE);
+  res = MapMemory (fd, FileSize, PROT_READ | PROT_EXEC, MAP_PRIVATE);
 
   close (fd);
 
-  if (res == MAP_FAILED)
+  if (res == NULL) {
+    perror ("MapFile() Failed");
     return EFI_DEVICE_ERROR;
-
+  }
+      
   *Length = (UINT64) FileSize;
   *BaseAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) res;
 
   return EFI_SUCCESS;
 }
 
+EFI_STATUS
+MapFd0 (
+  IN  CHAR8                     *FileName,
+  IN OUT  EFI_PHYSICAL_ADDRESS  *BaseAddress,
+  OUT UINT64                    *Length
+  )
+{
+  int     fd;
+  void    *res, *res2, *res3;
+  UINTN   FileSize;
+  UINTN   FvSize;
+  void    *EmuMagicPage;
+
+  fd = open (FileName, O_RDWR);
+  if (fd < 0) {
+    return EFI_NOT_FOUND;
+  }
+  FileSize = lseek (fd, 0, SEEK_END);
+  FvSize = FixedPcdGet64 (PcdEmuFlashFvRecoverySize);
+
+  // Assume start of FD is Recovery FV, and make it write protected
+  res = mmap (
+          (void *)(UINTN)FixedPcdGet64 (PcdEmuFlashFvRecoveryBase), 
+          FvSize, 
+          PROT_READ | PROT_EXEC, 
+          MAP_PRIVATE, 
+          fd, 
+          0
+          );
+  if (res == MAP_FAILED) {
+    perror ("MapFd0() Failed res =");
+    close (fd);
+    return EFI_DEVICE_ERROR;
+  } else if (res != (void *)(UINTN)FixedPcdGet64 (PcdEmuFlashFvRecoveryBase)) {
+    // We could not load at the build address, so we need to allow writes
+    munmap (res, FvSize);
+    res = mmap (
+            (void *)(UINTN)FixedPcdGet64 (PcdEmuFlashFvRecoveryBase), 
+            FvSize, 
+            PROT_READ | PROT_WRITE | PROT_EXEC, 
+            MAP_PRIVATE, 
+            fd, 
+            0
+            );
+    if (res == MAP_FAILED) {
+      perror ("MapFd0() Failed res =");
+      close (fd);
+      return EFI_DEVICE_ERROR;
+    }
+  }
+  
+  // Map the rest of the FD as read/write
+  res2 = mmap (
+          (void *)(UINTN)(FixedPcdGet64 (PcdEmuFlashFvRecoveryBase) + FvSize), 
+          FileSize - FvSize, 
+          PROT_READ | PROT_WRITE | PROT_EXEC, 
+          MAP_SHARED,
+          fd, 
+          FvSize
+          );
+  close (fd);
+  if (res2 == MAP_FAILED) {
+    perror ("MapFd0() Failed res2 =");
+    return EFI_DEVICE_ERROR;
+  }
+
+  //
+  // If enabled use the magic page to communicate between modules 
+  // This replaces the PI PeiServicesTable pointer mechanism that
+  // deos not work in the emulator. It also allows the removal of
+  // writable globals from SEC, PEI_CORE (libraries), PEIMs
+  //
+  EmuMagicPage = (void *)(UINTN)FixedPcdGet64 (PcdPeiServicesTablePage);
+  if (EmuMagicPage != NULL) {
+    res3 =  mmap (
+              (void *)EmuMagicPage, 
+              4096, 
+              PROT_READ | PROT_WRITE, 
+              MAP_PRIVATE | MAP_ANONYMOUS,
+              0, 
+              0
+              );
+    if (res3 != EmuMagicPage) {
+      printf ("MapFd0(): Could not allocate PeiServicesTablePage @ %lx\n", (long unsigned int)EmuMagicPage);
+      return EFI_DEVICE_ERROR;
+    }
+  }
+  
+  *Length = (UINT64) FileSize;
+  *BaseAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) res;
+
+  return EFI_SUCCESS;  
+}
 
 
-VOID
-SecLoadFromCore (
-  IN  UINTN   LargestRegion,
-  IN  UINTN   LargestRegionSize,
-  IN  UINTN   BootFirmwareVolumeBase,
-  IN  VOID    *PeiCorePe32File
-  )
 /*++
 
 Routine Description:
-  This is the service to load the PEI Core from the Firmware Volume
+  This is the service to load the SEC Core from the Firmware Volume
 
 Arguments:
-  LargestRegion           - Memory to use for PEI.
+  LargestRegion           - Memory to use for SEC.
   LargestRegionSize       - Size of Memory to use for PEI
   BootFirmwareVolumeBase  - Start of the Boot FV
-  PeiCorePe32File         - PEI Core PE32
+  PeiCorePe32File         - SEC PE32
 
 Returns:
   Success means control is transfered and thus we should never return
 
 **/
+VOID
+SecLoadFromCore (
+  IN  UINTN   LargestRegion,
+  IN  UINTN   LargestRegionSize,
+  IN  UINTN   BootFirmwareVolumeBase,
+  IN  VOID    *PeiCorePe32File
+  )
 {
   EFI_STATUS                  Status;
   EFI_PHYSICAL_ADDRESS        TopOfMemory;
   VOID                        *TopOfStack;
-  UINT64                      PeiCoreSize;
   EFI_PHYSICAL_ADDRESS        PeiCoreEntryPoint;
-  EFI_PHYSICAL_ADDRESS        PeiImageAddress;
   EFI_SEC_PEI_HAND_OFF        *SecCoreData;
   UINTN                       PeiStackSize;
 
@@ -442,7 +539,7 @@ Returns:
   //
   // Bind this information into the SEC hand-off state
   //
-  SecCoreData                        = (EFI_SEC_PEI_HAND_OFF*)(UINTN) TopOfStack;
+  SecCoreData                         = (EFI_SEC_PEI_HAND_OFF*)(UINTN) TopOfStack;
   SecCoreData->DataSize               = sizeof(EFI_SEC_PEI_HAND_OFF);
   SecCoreData->BootFirmwareVolumeBase = (VOID*)BootFirmwareVolumeBase;
   SecCoreData->BootFirmwareVolumeSize = PcdGet32 (PcdEmuFirmwareFdSize);
@@ -454,41 +551,29 @@ Returns:
   SecCoreData->PeiTemporaryRamSize    = STACK_SIZE - PeiStackSize;
 
   //
-  // Load the PEI Core from a Firmware Volume
+  // Find the SEC Core Entry Point
   //
-  Status = SecUnixPeiLoadFile (
-            PeiCorePe32File,
-            &PeiImageAddress,
-            &PeiCoreSize,
-            &PeiCoreEntryPoint
-            );
+  Status = SecPeCoffGetEntryPoint (PeiCorePe32File, (VOID **)&PeiCoreEntryPoint);
   if (EFI_ERROR (Status)) {
     return ;
   }
 
   //
-  // Transfer control to the PEI Core
+  // Transfer control to the SEC Core
   //
   PeiSwitchStacks (
     (SWITCH_STACK_ENTRY_POINT) (UINTN) PeiCoreEntryPoint,
     SecCoreData,
-    (VOID *)GetThunkPpiList (),
-    NULL,
+    (VOID *)gPpiList,
     TopOfStack
     );
   //
-  // If we get here, then the PEI Core returned.  This is an error
+  // If we get here, then the SEC Core returned.  This is an error
   //
   return ;
 }
 
-EFI_STATUS
-EFIAPI
-SecUnixPeiAutoScan (
-  IN  UINTN                 Index,
-  OUT EFI_PHYSICAL_ADDRESS  *MemoryBase,
-  OUT UINT64                *MemorySize
-  )
+
 /*++
 
 Routine Description:
@@ -509,6 +594,13 @@ Returns:
   EFI_UNSUPPORTED - If Index is not supported
 
 **/
+EFI_STATUS
+EFIAPI
+SecUnixPeiAutoScan (
+  IN  UINTN                 Index,
+  OUT EFI_PHYSICAL_ADDRESS  *MemoryBase,
+  OUT UINT64                *MemorySize
+  )
 {
   void *res;
 
@@ -517,11 +609,14 @@ Returns:
   }
 
   *MemoryBase = 0;
-  res = MapMemory(0, gSystemMemory[Index].Size,
-      PROT_READ | PROT_WRITE | PROT_EXEC,
-      MAP_PRIVATE | MAP_ANONYMOUS);
-  if (res == MAP_FAILED)
+  res = MapMemory (
+          0, gSystemMemory[Index].Size,
+          PROT_READ | PROT_WRITE | PROT_EXEC,
+          MAP_PRIVATE | MAP_ANONYMOUS
+          );
+  if (res == MAP_FAILED) {
     return EFI_DEVICE_ERROR;
+  }
   *MemorySize = gSystemMemory[Index].Size;
   *MemoryBase = (UINTN)res;
   gSystemMemory[Index].Memory = *MemoryBase;
@@ -529,11 +624,7 @@ Returns:
   return EFI_SUCCESS;
 }
 
-VOID *
-EFIAPI
-SecEmuThunkAddress (
-  VOID
-  )
+
 /*++
 
 Routine Description:
@@ -548,41 +639,29 @@ Returns:
   EFI_SUCCESS - Data returned
 
 **/
+VOID *
+EFIAPI
+SecEmuThunkAddress (
+  VOID
+  )
 {
   return &gEmuThunkProtocol;
 }
 
 
-EFI_STATUS
-SecUnixPeiLoadFile (
-  IN  VOID                    *Pe32Data,
-  OUT EFI_PHYSICAL_ADDRESS    *ImageAddress,
-  OUT UINT64                  *ImageSize,
-  OUT EFI_PHYSICAL_ADDRESS    *EntryPoint
-  )
-/*++
-
-Routine Description:
-  Loads and relocates a PE/COFF image into memory.
 
-Arguments:
-  Pe32Data         - The base address of the PE/COFF file that is to be loaded and relocated
-  ImageAddress     - The base address of the relocated PE/COFF image
-  ImageSize        - The size of the relocated PE/COFF image
-  EntryPoint       - The entry point of the relocated PE/COFF image
-
-Returns:
-  EFI_SUCCESS   - The file was loaded and relocated
-  EFI_OUT_OF_RESOURCES - There was not enough memory to load and relocate the PE/COFF file
-
-**/
+RETURN_STATUS
+EFIAPI
+SecPeCoffGetEntryPoint (
+  IN     VOID  *Pe32Data,
+  IN OUT VOID  **EntryPoint
+  )
 {
-  EFI_STATUS                            Status;
-  PE_COFF_LOADER_IMAGE_CONTEXT          ImageContext;
+  EFI_STATUS                    Status;
+  PE_COFF_LOADER_IMAGE_CONTEXT  ImageContext;
 
   ZeroMem (&ImageContext, sizeof (ImageContext));
   ImageContext.Handle     = Pe32Data;
-
   ImageContext.ImageRead  = (PE_COFF_LOADER_READ_FILE) SecImageRead;
 
   Status                  = PeCoffLoaderGetImageInfo (&ImageContext);
@@ -590,80 +669,41 @@ Returns:
     return Status;
   }
 
+  if (ImageContext.ImageAddress != (UINTN)Pe32Data) {
+    //
+    // Relocate image to match the address where it resides
+    //
+    ImageContext.ImageAddress = (UINTN)Pe32Data;
+    Status = PeCoffLoaderLoadImage (&ImageContext);
+    if (EFI_ERROR (Status)) {
+      return Status;
+    }
 
-  //
-  // Allocate space in UNIX (not emulator) memory. Extra space is for alignment
-  //
-  ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) MapMemory (
-    0,
-    (UINT32) (ImageContext.ImageSize + (ImageContext.SectionAlignment * 2)),
-    PROT_READ | PROT_WRITE | PROT_EXEC,
-    MAP_ANONYMOUS | MAP_PRIVATE
-    );
-  if (ImageContext.ImageAddress == 0) {
-    return EFI_OUT_OF_RESOURCES;
-  }
-
-  //
-  // Align buffer on section boundry
-  //
-  ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;
-  ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)(ImageContext.SectionAlignment - 1));
-
-
-  Status = PeCoffLoaderLoadImage (&ImageContext);
-  if (EFI_ERROR (Status)) {
-    return Status;
-  }
-
-  Status = PeCoffLoaderRelocateImage (&ImageContext);
-  if (EFI_ERROR (Status)) {
-    return Status;
+    Status = PeCoffLoaderRelocateImage (&ImageContext);
+    if (EFI_ERROR (Status)) {
+      return Status;
+    }
+  } else {
+    //
+    // Or just return image entry point
+    //
+    ImageContext.PdbPointer = PeCoffLoaderGetPdbPointer (Pe32Data);
+    Status = PeCoffLoaderGetEntryPoint (Pe32Data, EntryPoint);
+    if (EFI_ERROR (Status)) {
+      return Status;
+    }
+    ImageContext.EntryPoint = (UINTN)*EntryPoint;
   }
 
-
+  // On Unix a dlopen is done that will change the entry point
   SecPeCoffRelocateImageExtraAction (&ImageContext);
-
-  //
-  // BugBug: Flush Instruction Cache Here when CPU Lib is ready
-  //
-
-  *ImageAddress = ImageContext.ImageAddress;
-  *ImageSize    = ImageContext.ImageSize;
-  *EntryPoint   = ImageContext.EntryPoint;
-
-  return EFI_SUCCESS;
-}
-
-
-RETURN_STATUS
-EFIAPI
-SecPeCoffGetEntryPoint (
-  IN     VOID  *Pe32Data,
-  IN OUT VOID  **EntryPoint
-  )
-{
-  EFI_STATUS              Status;
-  EFI_PHYSICAL_ADDRESS    ImageAddress;
-  UINT64                  ImageSize;
-  EFI_PHYSICAL_ADDRESS    PhysEntryPoint;
-
-  Status = SecUnixPeiLoadFile (Pe32Data, &ImageAddress, &ImageSize, &PhysEntryPoint);
-
-  *EntryPoint = (VOID *)(UINTN)PhysEntryPoint;
+  *EntryPoint = (VOID *)(UINTN)ImageContext.EntryPoint;
+  
   return Status;
 }
 
 
 
-EFI_STATUS
-EFIAPI
-SecUnixFdAddress (
-  IN     UINTN                 Index,
-  IN OUT EFI_PHYSICAL_ADDRESS  *FdBase,
-  IN OUT UINT64                *FdSize,
-  IN OUT EFI_PHYSICAL_ADDRESS  *FixUp
-  )
 /*++
 
 Routine Description:
@@ -681,6 +721,14 @@ Returns:
   EFI_UNSUPPORTED - Index does nto map to an FD in the system
 
 **/
+EFI_STATUS
+EFIAPI
+SecUnixFdAddress (
+  IN     UINTN                 Index,
+  IN OUT EFI_PHYSICAL_ADDRESS  *FdBase,
+  IN OUT UINT64                *FdSize,
+  IN OUT EFI_PHYSICAL_ADDRESS  *FixUp
+  )
 {
   if (Index >= gFdInfoCount) {
     return EFI_UNSUPPORTED;
@@ -706,8 +754,39 @@ Returns:
   return EFI_SUCCESS;
 }
 
+
+/*++
+
+Routine Description:
+  Count the number of seperators in String
+
+Arguments:
+  String    - String to process
+  Seperator - Item to count
+
+Returns:
+  Number of Seperator in String
+
+**/
+UINTN
+CountSeperatorsInString (
+  IN  const CHAR16   *String,
+  IN  CHAR16         Seperator
+  )
+{
+  UINTN Count;
+
+  for (Count = 0; *String != '\0'; String++) {
+    if (*String == Seperator) {
+      Count++;
+    }
+  }
+
+  return Count;
+}
+
+
 EFI_STATUS
-EFIAPI
 SecImageRead (
   IN     VOID    *FileHandle,
   IN     UINTN   FileOffset,
@@ -744,42 +823,7 @@ Returns:
   return EFI_SUCCESS;
 }
 
-UINTN
-CountSeperatorsInString (
-  IN  const CHAR16   *String,
-  IN  CHAR16         Seperator
-  )
-/*++
 
-Routine Description:
-  Count the number of seperators in String
-
-Arguments:
-  String    - String to process
-  Seperator - Item to count
-
-Returns:
-  Number of Seperator in String
-
-**/
-{
-  UINTN Count;
-
-  for (Count = 0; *String != '\0'; String++) {
-    if (*String == Seperator) {
-      Count++;
-    }
-  }
-
-  return Count;
-}
-
-
-EFI_STATUS
-AddHandle (
-  IN  PE_COFF_LOADER_IMAGE_CONTEXT         *ImageContext,
-  IN  VOID                                 *ModHandle
-  )
 /*++
 
 Routine Description:
@@ -796,6 +840,11 @@ Returns:
   EFI_SUCCESS - ModHandle was stored.
 
 **/
+EFI_STATUS
+AddHandle (
+  IN  PE_COFF_LOADER_IMAGE_CONTEXT         *ImageContext,
+  IN  VOID                                 *ModHandle
+  )
 {
   UINTN                       Index;
   IMAGE_CONTEXT_TO_MOD_HANDLE *Array;
@@ -835,10 +884,6 @@ Returns:
 }
 
 
-VOID *
-RemoveHandle (
-  IN  PE_COFF_LOADER_IMAGE_CONTEXT         *ImageContext
-  )
 /*++
 
 Routine Description:
@@ -853,6 +898,10 @@ Returns:
   NULL      - No ModHandle associated with ImageContext
 
 **/
+VOID *
+RemoveHandle (
+  IN  PE_COFF_LOADER_IMAGE_CONTEXT         *ImageContext
+  )
 {
   UINTN                        Index;
   IMAGE_CONTEXT_TO_MOD_HANDLE  *Array;
@@ -890,7 +939,7 @@ Returns:
 // b SecGdbScriptBreak
 // command
 // silent
-// source SecMain.dll.gdb
+// source SecMain.gdb
 // c
 // end
 //
@@ -1057,11 +1106,13 @@ SecPeCoffRelocateImageExtraAction (
     return;
   }
 
-  fprintf (stderr,
+  fprintf (
+     stderr,
      "Loading %s 0x%08lx - entry point 0x%08lx\n",
      ImageContext->PdbPointer,
      (unsigned long)ImageContext->ImageAddress,
-     (unsigned long)ImageContext->EntryPoint);
+     (unsigned long)ImageContext->EntryPoint
+     );
 
   Handle = dlopen (ImageContext->PdbPointer, RTLD_NOW);
 
@@ -1073,7 +1124,7 @@ SecPeCoffRelocateImageExtraAction (
 
   if (Entry != NULL) {
     ImageContext->EntryPoint = (UINTN)Entry;
-    printf("Change %s Entrypoint to :0x%08lx\n", ImageContext->PdbPointer, (unsigned long)Entry);
+    printf ("Change %s Entrypoint to :0x%08lx\n", ImageContext->PdbPointer, (unsigned long)Entry);
   }
 
   SecUnixLoaderBreak ();