]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UnixPkg/Sec/SecMain.c
Port UnixPkg to also support X64. Currently only supports Unix x86_64 ABI. In the...
[mirror_edk2.git] / UnixPkg / Sec / SecMain.c
index 98a7a4db9df9499578ba1c5215230afdc5b3fab6..4d66bfa30125355c8b9e5a40c0880bbd39374b86 100644 (file)
@@ -1,8 +1,8 @@
 /*++
 
-Copyright (c) 2006 - 2009 Intel Corporation.
-Portions copyright (c) 2008-2009 Apple Inc.
-All rights reserved. This program and the accompanying materials
+Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
+Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
+This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD License
 which accompanies this distribution.  The full text of the license may be found at
 http://opensource.org/licenses/bsd-license.php
@@ -36,7 +36,6 @@ Abstract:
 #include "SecMain.h"
 #include <sys/mman.h>
 #include <Ppi/UnixPeiLoadFile.h>
-#include <Framework/StatusCode.h>
 #include <Ppi/TemporaryRamSupport.h>
 #include <dlfcn.h>
 
@@ -89,7 +88,6 @@ EFI_PEI_PPI_DESCRIPTOR  gPrivateDispatchTable[] = {
     &mSecTemporaryRamSupportPpi
   },
   {
-
     EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
     &gUnixFwhPpiGuid,
     &mSecFwhInformationPpi
@@ -142,6 +140,7 @@ MapFile (
   IN OUT  EFI_PHYSICAL_ADDRESS  *BaseAddress,
   OUT UINT64                    *Length
   );
+  
 EFI_STATUS
 EFIAPI
 SecNt32PeCoffRelocateImage (
@@ -188,10 +187,10 @@ Returns:
   setbuf(stdout, 0);
   setbuf(stderr, 0);
 
-  MemorySizeStr      = (CHAR16 *) FixedPcdGetPtr (PcdUnixMemorySizeForSecMain);
-  FirmwareVolumesStr = (CHAR16 *) FixedPcdGetPtr (PcdUnixFirmwareVolume);
+  MemorySizeStr      = (CHAR16 *) PcdGetPtr (PcdUnixMemorySizeForSecMain);
+  FirmwareVolumesStr = (CHAR16 *) PcdGetPtr (PcdUnixFirmwareVolume);
 
-  printf ("\nEDK SEC Main UNIX Emulation Environment from www.TianoCore.org\n");
+  printf ("\nEDK SEC Main UNIX Emulation Environment from edk2.sourceforge.net\n");
 
 #ifdef __APPLE__
   //
@@ -200,7 +199,7 @@ Returns:
   // symbols when we load every PE/COFF image.
   //
   Index = strlen (*Argv);
-  gGdbWorkingFileName = malloc (Index + strlen(".gdb"));
+  gGdbWorkingFileName = malloc (Index + strlen(".gdb") + 1);
   strcpy (gGdbWorkingFileName, *Argv);
   strcat (gGdbWorkingFileName, ".gdb");
 #endif
@@ -227,7 +226,7 @@ Returns:
   //
   // Setup Boot Mode. If BootModeStr == "" then BootMode = 0 (BOOT_WITH_FULL_CONFIGURATION)
   //
-  printf ("  BootMode 0x%02x\n", (unsigned int)FixedPcdGet32 (PcdUnixBootMode));
+  printf ("  BootMode 0x%02x\n", (unsigned int)PcdGet32 (PcdUnixBootMode));
 
   //
   // Open up a 128K file to emulate temp memory for PEI.
@@ -237,7 +236,7 @@ Returns:
   InitialStackMemorySize  = STACK_SIZE;
   InitialStackMemory = (UINTN)MapMemory(0, 
           (UINT32) InitialStackMemorySize,
-          PROT_READ | PROT_WRITE,
+          PROT_READ | PROT_WRITE | PROT_EXEC,
           MAP_ANONYMOUS | MAP_PRIVATE);
   if (InitialStackMemory == 0) {
     printf ("ERROR : Can not open SecStack Exiting\n");
@@ -500,28 +499,6 @@ Returns:
   return EFI_SUCCESS;
 }
 
-/**
-  Transfers control to a function starting with a new stack.
-
-  Transfers control to the function specified by EntryPoint using the new stack
-  specified by NewStack and passing in the parameters specified by Context1 and
-  Context2. Context1 and Context2 are optional and may be NULL. The function
-  EntryPoint must never return.
-
-  If EntryPoint is NULL, then ASSERT().
-  If NewStack is NULL, then ASSERT().
-
-  @param  EntryPoint  A pointer to function to call with the new stack.
-  @param  Context1    A pointer to the context to pass into the EntryPoint
-                      function.
-  @param  Context2    A pointer to the context to pass into the EntryPoint
-                      function.
-  @param  NewStack    A pointer to the new stack to use for the EntryPoint
-                      function.
-  @param  NewBsp      A pointer to the new BSP for the EntryPoint on IPF. It's
-                      Reserved on other architectures.
-
-**/
 VOID
 EFIAPI
 PeiSwitchStacks (
@@ -530,33 +507,7 @@ PeiSwitchStacks (
   IN      VOID                      *Context2,  OPTIONAL
   IN      VOID                      *Context3,  OPTIONAL
   IN      VOID                      *NewStack
-  )
-{
-  BASE_LIBRARY_JUMP_BUFFER  JumpBuffer;
-  
-  ASSERT (EntryPoint != NULL);
-  ASSERT (NewStack != NULL);
-
-  //
-  // Stack should be aligned with CPU_STACK_ALIGNMENT
-  //
-  ASSERT (((UINTN)NewStack & (CPU_STACK_ALIGNMENT - 1)) == 0);
-
-  JumpBuffer.Eip = (UINTN)EntryPoint;
-  JumpBuffer.Esp = (UINTN)NewStack - sizeof (VOID*);
-  JumpBuffer.Esp -= sizeof (Context1) + sizeof (Context2) + sizeof(Context3);
-  ((VOID**)JumpBuffer.Esp)[1] = Context1;
-  ((VOID**)JumpBuffer.Esp)[2] = Context2;
-  ((VOID**)JumpBuffer.Esp)[3] = Context3;
-
-  LongJump (&JumpBuffer, (UINTN)-1);
-  
-
-  //
-  // InternalSwitchStack () will never return
-  //
-  ASSERT (FALSE);  
-}
+  );
 
 VOID
 SecLoadFromCore (
@@ -621,7 +572,7 @@ Returns:
   SecCoreData                        = (EFI_SEC_PEI_HAND_OFF*)(UINTN) TopOfStack;
   SecCoreData->DataSize               = sizeof(EFI_SEC_PEI_HAND_OFF);
   SecCoreData->BootFirmwareVolumeBase = (VOID*)BootFirmwareVolumeBase;
-  SecCoreData->BootFirmwareVolumeSize = FixedPcdGet32(PcdUnixFirmwareFdSize);
+  SecCoreData->BootFirmwareVolumeSize = PcdGet32 (PcdUnixFirmwareFdSize);
   SecCoreData->TemporaryRamBase       = (VOID*)(UINTN)LargestRegion; 
   SecCoreData->TemporaryRamSize       = STACK_SIZE;
   SecCoreData->StackBase              = SecCoreData->TemporaryRamBase;
@@ -765,24 +716,38 @@ Returns:
   if (EFI_ERROR (Status)) {
     return Status;
   }
+  
+  
   //
   // Allocate space in UNIX (not emulator) memory. Extra space is for alignment
   //
-  ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) malloc ((UINTN) (ImageContext.ImageSize + (ImageContext.SectionAlignment * 2)));
+  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 &= ~(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;
+  }
+       
 
   SecPeCoffRelocateImageExtraAction (&ImageContext);
 
@@ -823,7 +788,8 @@ EFIAPI
 SecUnixFdAddress (
   IN     UINTN                 Index,
   IN OUT EFI_PHYSICAL_ADDRESS  *FdBase,
-  IN OUT UINT64                *FdSize
+  IN OUT UINT64                *FdSize,
+  IN OUT EFI_PHYSICAL_ADDRESS  *FixUp
   )
 /*++
 
@@ -835,6 +801,7 @@ Arguments:
   Index  - Which FD, starts at zero.
   FdSize - Size of the FD in bytes
   FdBase - Start address of the FD. Assume it points to an FV Header
+  FixUp  - Difference between actual FD address and build address
 
 Returns:
   EFI_SUCCESS     - Return the Base address and size of the FV
@@ -848,11 +815,21 @@ Returns:
 
   *FdBase = gFdInfo[Index].Address;
   *FdSize = gFdInfo[Index].Size;
+  *FixUp  = 0;
 
   if (*FdBase == 0 && *FdSize == 0) {
     return EFI_UNSUPPORTED;
   }
 
+  if (Index == 0) {
+    //
+    // FD 0 has XIP code and well known PCD values 
+    // If the memory buffer could not be allocated at the FD build address
+    // the Fixup is the difference.
+    //
+    *FixUp = *FdBase - PcdGet64 (PcdUnixFdBaseAddress);
+  }
+
   return EFI_SUCCESS;
 }
 
@@ -1093,7 +1070,7 @@ PrintLoadAddress (
 {
   fprintf (stderr,
      "0x%08lx Loading %s with entry point 0x%08lx\n",
-     (unsigned long)ImageContext->ImageAddress + ImageContext->SizeOfHeaders,
+     (unsigned long)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders),
      ImageContext->PdbPointer,   
      (unsigned long)ImageContext->EntryPoint
      );
@@ -1109,13 +1086,6 @@ SecPeCoffRelocateImageExtraAction (
   IN OUT PE_COFF_LOADER_IMAGE_CONTEXT         *ImageContext
   )
 {
-  EFI_STATUS Status;
-
-  Status = PeCoffLoaderRelocateImage (ImageContext);
-  if (EFI_ERROR (Status)) {
-    PrintLoadAddress (ImageContext);
-    return;
-  }
     
 #ifdef __APPLE__
   PrintLoadAddress (ImageContext);
@@ -1161,7 +1131,7 @@ SecPeCoffRelocateImageExtraAction (
     //
     GdbTempFile = fopen (gGdbWorkingFileName, "w");
     if (GdbTempFile != NULL) {
-      fprintf (GdbTempFile, "add-symbol-file %s 0x%x\n", ImageContext->PdbPointer, (UINTN)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders));
+      fprintf (GdbTempFile, "add-symbol-file %s 0x%08lx\n", ImageContext->PdbPointer, (long unsigned int)(ImageContext->ImageAddress + ImageContext->SizeOfHeaders));
       fclose (GdbTempFile);
       
       //
@@ -1186,10 +1156,10 @@ SecPeCoffRelocateImageExtraAction (
      (unsigned long)ImageContext->ImageAddress,
      (unsigned long)ImageContext->EntryPoint);
 
-  Handle = dlopen(ImageContext->PdbPointer, RTLD_NOW);
+  Handle = dlopen (ImageContext->PdbPointer, RTLD_NOW);
   
   if (Handle) {
-    Entry = dlsym(Handle, "_ModuleEntryPoint");
+    Entry = dlsym (Handle, "_ModuleEntryPoint");
   } else {
     printf("%s\n", dlerror());  
   }