]> git.proxmox.com Git - mirror_edk2.git/blobdiff - Nt32Pkg/Sec/SecMain.c
Remove unnecessary use of FixedPcdxxx() functions and [FixedPcd] INF sections. These...
[mirror_edk2.git] / Nt32Pkg / Sec / SecMain.c
index 3058269d383d0caa4c02d0bc3a3c914ef3963976..2c6715d84aa52270de91a2c18e2ededc342d9796 100644 (file)
@@ -1,6 +1,6 @@
-/*++\r
+/**@file\r
 \r
-Copyright (c) 2006, Intel Corporation\r
+Copyright (c) 2006 - 2010, Intel Corporation\r
 All rights reserved. This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -26,27 +26,11 @@ Abstract:
   This code produces 128 K of temporary memory for the PEI stack by directly\r
   allocate memory space with ReadWrite and Execute attribute.\r
 \r
---*/\r
+**/\r
 \r
 #include "SecMain.h"\r
 \r
 \r
-//\r
-// Globals\r
-//\r
-EFI_PEI_PE_COFF_LOADER_PROTOCOL_INSTANCE  mPeiEfiPeiPeCoffLoaderInstance = {\r
-  {\r
-    SecNt32PeCoffGetImageInfo,\r
-    SecNt32PeCoffLoadImage,\r
-    SecNt32PeCoffRelocateImage,\r
-    SecNt32PeCoffUnloadimage\r
-  },\r
-  NULL\r
-};\r
-\r
-\r
-\r
-EFI_PEI_PE_COFF_LOADER_PROTOCOL           *gPeiEfiPeiPeCoffLoader = &mPeiEfiPeiPeCoffLoaderInstance.PeCoff;\r
 \r
 NT_PEI_LOAD_FILE_PPI                      mSecNtLoadFilePpi     = { SecWinNtPeiLoadFile };\r
 \r
@@ -58,13 +42,9 @@ EFI_PEI_PROGRESS_CODE_PPI                 mSecStatusCodePpi     = { SecPeiReport
 \r
 NT_FWH_PPI                                mSecFwhInformationPpi = { SecWinNtFdAddress };\r
 \r
+TEMPORARY_RAM_SUPPORT_PPI                 mSecTemporaryRamSupportPpi = {SecTemporaryRamSupport};\r
 \r
 EFI_PEI_PPI_DESCRIPTOR  gPrivateDispatchTable[] = {\r
-  {\r
-    EFI_PEI_PPI_DESCRIPTOR_PPI,\r
-    &gEfiPeiPeCoffLoaderGuid,\r
-    NULL\r
-  },\r
   {\r
     EFI_PEI_PPI_DESCRIPTOR_PPI,\r
     &gNtPeiLoadFilePpiGuid,\r
@@ -85,6 +65,11 @@ EFI_PEI_PPI_DESCRIPTOR  gPrivateDispatchTable[] = {
     &gEfiPeiStatusCodePpiGuid,\r
     &mSecStatusCodePpi\r
   },\r
+  {\r
+    EFI_PEI_PPI_DESCRIPTOR_PPI,\r
+    &gEfiTemporaryRamSupportPpiGuid,\r
+    &mSecTemporaryRamSupportPpi\r
+  },\r
   {\r
     EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,\r
     &gNtFwhPpiGuid,\r
@@ -111,12 +96,40 @@ NT_FD_INFO                                *gFdInfo;
 UINTN                                     gSystemMemoryCount = 0;\r
 NT_SYSTEM_MEMORY                          *gSystemMemory;\r
 \r
+VOID\r
+EFIAPI\r
+SecSwitchStack (\r
+  UINT32   TemporaryMemoryBase,\r
+  UINT32   PermenentMemoryBase\r
+  );\r
+EFI_STATUS\r
+SecNt32PeCoffRelocateImage (\r
+  IN OUT PE_COFF_LOADER_IMAGE_CONTEXT         *ImageContext\r
+  );\r
 \r
-UINTN                   mPdbNameModHandleArraySize = 0;\r
-PDB_NAME_TO_MOD_HANDLE  *mPdbNameModHandleArray = NULL;\r
-\r
-\r
+VOID\r
+SecPrint (\r
+  CHAR8  *Format,\r
+  ...\r
+  )\r
+{\r
+  va_list  Marker;\r
+  UINTN    CharCount;\r
+  CHAR8    Buffer[EFI_STATUS_CODE_DATA_MAX_SIZE];\r
 \r
+  va_start (Marker, Format);\r
+  \r
+  _vsnprintf (Buffer, sizeof (Buffer), Format, Marker);\r
+\r
+  CharCount = strlen (Buffer);\r
+  WriteFile (\r
+    GetStdHandle (STD_OUTPUT_HANDLE), \r
+    Buffer,\r
+    CharCount,\r
+    (LPDWORD)&CharCount,\r
+    NULL\r
+    );\r
+}\r
 \r
 INTN\r
 EFIAPI\r
@@ -147,7 +160,6 @@ Returns:
   UINTN                 Index;\r
   UINTN                 Index1;\r
   UINTN                 Index2;\r
-  UINTN                 PeiIndex;\r
   CHAR16                *FileName;\r
   CHAR16                *FileNamePtr;\r
   BOOLEAN               Done;\r
@@ -155,11 +167,11 @@ Returns:
   CHAR16                *MemorySizeStr;\r
   CHAR16                *FirmwareVolumesStr;\r
   UINTN                 *StackPointer;\r
-  \r
-  MemorySizeStr      = (CHAR16 *) FixedPcdGetPtr (PcdWinNtMemorySizeForSecMain);\r
-  FirmwareVolumesStr = (CHAR16 *) FixedPcdGetPtr (PcdWinNtFirmwareVolume);\r
 \r
-  printf ("\nEDK SEC Main NT Emulation Environment from www.TianoCore.org\n");\r
+  MemorySizeStr      = (CHAR16 *) PcdGetPtr (PcdWinNtMemorySizeForSecMain);\r
+  FirmwareVolumesStr = (CHAR16 *) PcdGetPtr (PcdWinNtFirmwareVolume);\r
+\r
+  SecPrint ("\nEDK II SEC Main NT Emulation Environment from www.TianoCore.org\n");\r
 \r
   //\r
   // Make some Windows calls to Set the process to the highest priority in the\r
@@ -174,7 +186,7 @@ Returns:
   gSystemMemoryCount  = CountSeperatorsInString (MemorySizeStr, '!') + 1;\r
   gSystemMemory       = calloc (gSystemMemoryCount, sizeof (NT_SYSTEM_MEMORY));\r
   if (gSystemMemory == NULL) {\r
-    printf ("ERROR : Can not allocate memory for %s.  Exiting.\n", MemorySizeStr);\r
+    SecPrint ("ERROR : Can not allocate memory for %S.  Exiting.\n", MemorySizeStr);\r
     exit (1);\r
   }\r
   //\r
@@ -183,13 +195,13 @@ Returns:
   gFdInfoCount  = CountSeperatorsInString (FirmwareVolumesStr, '!') + 1;\r
   gFdInfo       = calloc (gFdInfoCount, sizeof (NT_FD_INFO));\r
   if (gFdInfo == NULL) {\r
-    printf ("ERROR : Can not allocate memory for %s.  Exiting.\n", FirmwareVolumesStr);\r
+    SecPrint ("ERROR : Can not allocate memory for %S.  Exiting.\n", FirmwareVolumesStr);\r
     exit (1);\r
   }\r
   //\r
   // Setup Boot Mode. If BootModeStr == "" then BootMode = 0 (BOOT_WITH_FULL_CONFIGURATION)\r
   //\r
-  printf ("  BootMode 0x%02x\n", FixedPcdGet32 (PcdWinNtBootMode));\r
+  SecPrint ("  BootMode 0x%02x\n", PcdGet32 (PcdWinNtBootMode));\r
 \r
   //\r
   //  Allocate 128K memory to emulate temp memory for PEI.\r
@@ -199,7 +211,7 @@ Returns:
   InitialStackMemorySize  = STACK_SIZE;\r
   InitialStackMemory = (EFI_PHYSICAL_ADDRESS) (UINTN) VirtualAlloc (NULL, (SIZE_T) (InitialStackMemorySize), MEM_COMMIT, PAGE_EXECUTE_READWRITE);\r
   if (InitialStackMemory == 0) {\r
-    printf ("ERROR : Can not allocate enough space for SecStack\n");\r
+    SecPrint ("ERROR : Can not allocate enough space for SecStack\n");\r
     exit (1);\r
   }\r
 \r
@@ -209,20 +221,20 @@ Returns:
     *StackPointer = 0x5AA55AA5;\r
   }\r
   \r
-  printf ("  SEC passing in %d bytes of temp RAM to PEI\n", InitialStackMemorySize);\r
+  SecPrint ("  SEC passing in %d bytes of temp RAM to PEI\n", InitialStackMemorySize);\r
 \r
   //\r
   // Open All the firmware volumes and remember the info in the gFdInfo global\r
   //\r
   FileNamePtr = (CHAR16 *)malloc (StrLen ((CHAR16 *)FirmwareVolumesStr) * sizeof(CHAR16));\r
   if (FileNamePtr == NULL) {\r
-    printf ("ERROR : Can not allocate memory for firmware volume string\n");\r
+    SecPrint ("ERROR : Can not allocate memory for firmware volume string\n");\r
     exit (1);\r
   }\r
 \r
   StrCpy (FileNamePtr, (CHAR16*)FirmwareVolumesStr);\r
 \r
-  for (Done = FALSE, Index = 0, PeiIndex = 0, PeiCoreFile = NULL; !Done; Index++) {\r
+  for (Done = FALSE, Index = 0, PeiCoreFile = NULL; !Done; Index++) {\r
     FileName = FileNamePtr;\r
     for (Index1 = 0; (FileNamePtr[Index1] != '!') && (FileNamePtr[Index1] != 0); Index1++)\r
       ;\r
@@ -244,17 +256,17 @@ Returns:
               &gFdInfo[Index].Size\r
               );\r
     if (EFI_ERROR (Status)) {\r
-      printf ("ERROR : Can not open Firmware Device File %S (%r).  Exiting.\n", FileName, Status);\r
+      SecPrint ("ERROR : Can not open Firmware Device File %S (0x%X).  Exiting.\n", FileName, Status);\r
       exit (1);\r
     }\r
 \r
-    printf ("  FD loaded from");\r
+    SecPrint ("  FD loaded from");\r
     //\r
     // printf can't print filenames directly as the \ gets interperted as an\r
     //  escape character.\r
     //\r
     for (Index2 = 0; FileName[Index2] != '\0'; Index2++) {\r
-      printf ("%c", FileName[Index2]);\r
+      SecPrint ("%c", FileName[Index2]);\r
     }\r
 \r
     if (PeiCoreFile == NULL) {\r
@@ -264,12 +276,11 @@ Returns:
       //\r
       Status = SecFfsFindPeiCore ((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) gFdInfo[Index].Address, &PeiCoreFile);\r
       if (!EFI_ERROR (Status)) {\r
-        PeiIndex = Index;\r
-        printf (" contains SEC Core");\r
+        SecPrint (" contains SEC Core");\r
       }\r
     }\r
 \r
-    printf ("\n");\r
+    SecPrint ("\n");\r
   }\r
   //\r
   // Calculate memory regions and store the information in the gSystemMemory\r
@@ -294,7 +305,7 @@ Returns:
     MemorySizeStr = MemorySizeStr + Index1 + 1;\r
   }\r
 \r
-  printf ("\n");\r
+  SecPrint ("\n");\r
 \r
   //\r
   // Hand off to PEI Core\r
@@ -305,7 +316,7 @@ Returns:
   // If we get here, then the PEI Core returned. This is an error as PEI should\r
   //  always hand off to DXE.\r
   //\r
-  printf ("ERROR : PEI Core returned\n");\r
+  SecPrint ("ERROR : PEI Core returned\n");\r
   exit (1);\r
 }\r
 \r
@@ -452,7 +463,7 @@ Returns:
 // TODO:    Data - add argument and description to function comment\r
 {\r
   CHAR8           *Format;\r
-  VA_LIST         Marker;\r
+  BASE_LIST       Marker;\r
   CHAR8           PrintBuffer[BYTES_PER_RECORD * 2];\r
   CHAR8           *Filename;\r
   CHAR8           *Description;\r
@@ -465,14 +476,14 @@ Returns:
     //\r
     // Processes ASSERT ()\r
     //\r
-    printf ("ASSERT %s(%d): %s\n", Filename, LineNumber, Description);\r
+    SecPrint ("ASSERT %s(%d): %s\n", Filename, (int)LineNumber, Description);\r
 \r
   } else if (ReportStatusCodeExtractDebugInfo (Data, &ErrorLevel, &Marker, &Format)) {\r
     //\r
     // Process DEBUG () macro \r
     //\r
-    AsciiVSPrint (PrintBuffer, BYTES_PER_RECORD, Format, Marker);\r
-    printf (PrintBuffer);\r
+    AsciiBSPrint (PrintBuffer, BYTES_PER_RECORD, Format, Marker);\r
+    SecPrint (PrintBuffer);\r
   }\r
 \r
   return EFI_SUCCESS;\r
@@ -560,29 +571,34 @@ Returns:
 --*/\r
 {\r
   EFI_STATUS                  Status;\r
-  EFI_PHYSICAL_ADDRESS        TopOfMemory;\r
   VOID                        *TopOfStack;\r
   UINT64                      PeiCoreSize;\r
   EFI_PHYSICAL_ADDRESS        PeiCoreEntryPoint;\r
   EFI_PHYSICAL_ADDRESS        PeiImageAddress;\r
   EFI_SEC_PEI_HAND_OFF        *SecCoreData;\r
+  UINTN                       PeiStackSize;\r
 \r
   //\r
   // Compute Top Of Memory for Stack and PEI Core Allocations\r
   //\r
-  TopOfMemory = LargestRegion + LargestRegionSize;\r
+  PeiStackSize = (UINTN)RShiftU64((UINT64)STACK_SIZE,1);\r
 \r
   //\r
-  // Allocate 128KB for the Stack\r
-  //\r
-  TopOfStack  = (VOID *)((UINTN)TopOfMemory - sizeof (EFI_SEC_PEI_HAND_OFF) - CPU_STACK_ALIGNMENT);\r
-  TopOfStack  = ALIGN_POINTER (TopOfStack, CPU_STACK_ALIGNMENT);\r
-  TopOfMemory = TopOfMemory - STACK_SIZE;\r
+  // |-----------| <---- TemporaryRamBase + TemporaryRamSize\r
+  // |   Heap    |\r
+  // |           |\r
+  // |-----------| <---- StackBase / PeiTemporaryMemoryBase\r
+  // |           |\r
+  // |  Stack    |\r
+  // |-----------| <---- TemporaryRamBase\r
+  // \r
+  TopOfStack  = (VOID *)(LargestRegion + PeiStackSize);\r
 \r
   //\r
-  // Patch value in dispatch table values\r
-  //\r
-  gPrivateDispatchTable[0].Ppi = gPeiEfiPeiPeCoffLoader;\r
+  // Reservet space for storing PeiCore's parament in stack.\r
+  // \r
+  TopOfStack  = (VOID *)((UINTN)TopOfStack - sizeof (EFI_SEC_PEI_HAND_OFF) - CPU_STACK_ALIGNMENT);\r
+  TopOfStack  = ALIGN_POINTER (TopOfStack, CPU_STACK_ALIGNMENT);\r
 \r
   //\r
   // Bind this information into the SEC hand-off state\r
@@ -590,13 +606,13 @@ Returns:
   SecCoreData                        = (EFI_SEC_PEI_HAND_OFF*)(UINTN) TopOfStack;\r
   SecCoreData->DataSize               = sizeof(EFI_SEC_PEI_HAND_OFF);\r
   SecCoreData->BootFirmwareVolumeBase = (VOID*)BootFirmwareVolumeBase;\r
-  SecCoreData->BootFirmwareVolumeSize = FixedPcdGet32(PcdWinNtFirmwareFdSize);\r
-  SecCoreData->TemporaryRamBase       = (VOID*)(UINTN)TopOfMemory\r
+  SecCoreData->BootFirmwareVolumeSize = PcdGet32(PcdWinNtFirmwareFdSize);\r
+  SecCoreData->TemporaryRamBase       = (VOID*)(UINTN)LargestRegion\r
   SecCoreData->TemporaryRamSize       = STACK_SIZE;\r
-  SecCoreData->PeiTemporaryRamBase    = SecCoreData->TemporaryRamBase;\r
-  SecCoreData->PeiTemporaryRamSize    = (UINTN)RShiftU64((UINT64)STACK_SIZE,1);\r
-  SecCoreData->StackBase              = (VOID*)((UINTN)SecCoreData->TemporaryRamBase + (UINTN)SecCoreData->TemporaryRamSize);\r
-  SecCoreData->StackSize              = (UINTN)RShiftU64((UINT64)STACK_SIZE,1);\r
+  SecCoreData->StackBase              = SecCoreData->TemporaryRamBase;\r
+  SecCoreData->StackSize              = PeiStackSize;\r
+  SecCoreData->PeiTemporaryRamBase    = (VOID*) ((UINTN) SecCoreData->TemporaryRamBase + PeiStackSize);\r
+  SecCoreData->PeiTemporaryRamSize    = STACK_SIZE - PeiStackSize;\r
 \r
   //\r
   // Load the PEI Core from a Firmware Volume\r
@@ -731,7 +747,7 @@ Returns:
 \r
   ImageContext.ImageRead  = (PE_COFF_LOADER_READ_FILE) SecImageRead;\r
 \r
-  Status                  = gPeiEfiPeiPeCoffLoader->GetImageInfo (gPeiEfiPeiPeCoffLoader, &ImageContext);\r
+  Status                  = PeCoffLoaderGetImageInfo (&ImageContext);\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
@@ -746,15 +762,15 @@ Returns:
   //\r
   // Align buffer on section boundry\r
   //\r
-  ImageContext.ImageAddress += ImageContext.SectionAlignment;\r
+  ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;\r
   ImageContext.ImageAddress &= ~(ImageContext.SectionAlignment - 1);\r
 \r
-  Status = gPeiEfiPeiPeCoffLoader->LoadImage (gPeiEfiPeiPeCoffLoader, &ImageContext);\r
+  Status = PeCoffLoaderLoadImage (&ImageContext);\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
 \r
-  Status = gPeiEfiPeiPeCoffLoader->RelocateImage (gPeiEfiPeiPeCoffLoader, &ImageContext);\r
+  Status = SecNt32PeCoffRelocateImage (&ImageContext);\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
@@ -894,7 +910,7 @@ Returns:
 \r
 UINTN\r
 CountSeperatorsInString (\r
-  IN  const CHAR16   *String,\r
+  IN  CONST CHAR16   *String,\r
   IN  CHAR16         Seperator\r
   )\r
 /*++\r
@@ -924,171 +940,7 @@ Returns:
 \r
 \r
 EFI_STATUS\r
-AddModHandle (\r
-  IN  PE_COFF_LOADER_IMAGE_CONTEXT         *ImageContext,\r
-  IN  VOID                                 *ModHandle\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-  Store the ModHandle in an array indexed by the Pdb File name.\r
-  The ModHandle is needed to unload the image. \r
-\r
-Arguments:\r
-  ImageContext - Input data returned from PE Laoder Library. Used to find the \r
-                 .PDB file name of the PE Image.\r
-  ModHandle    - Returned from LoadLibraryEx() and stored for call to \r
-                 FreeLibrary().\r
-\r
-Returns:\r
-  EFI_SUCCESS - ModHandle was stored. \r
-\r
---*/\r
-{\r
-  UINTN                   Index;\r
-  PDB_NAME_TO_MOD_HANDLE  *Array;\r
-  UINTN                   PreviousSize;\r
-\r
-\r
-  Array = mPdbNameModHandleArray;\r
-  for (Index = 0; Index < mPdbNameModHandleArraySize; Index++, Array++) {\r
-    if (Array->PdbPointer == NULL) {\r
-      //\r
-      // Make a copy of the stirng and store the ModHandle\r
-      //\r
-      Array->PdbPointer = malloc (strlen (ImageContext->PdbPointer) + 1);\r
-      ASSERT (Array->PdbPointer != NULL);\r
-\r
-      strcpy (Array->PdbPointer, ImageContext->PdbPointer);\r
-      Array->ModHandle = ModHandle;\r
-      return EFI_SUCCESS;\r
-    }\r
-  }\r
-  \r
-  //\r
-  // No free space in mPdbNameModHandleArray so grow it by \r
-  // MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE entires. realloc will\r
-  // copy the old values to the new locaiton. But it does\r
-  // not zero the new memory area.\r
-  //\r
-  PreviousSize = mPdbNameModHandleArraySize * sizeof (PDB_NAME_TO_MOD_HANDLE);\r
-  mPdbNameModHandleArraySize += MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE;\r
-\r
-  mPdbNameModHandleArray = realloc (mPdbNameModHandleArray, mPdbNameModHandleArraySize * sizeof (PDB_NAME_TO_MOD_HANDLE));\r
-  if (mPdbNameModHandleArray == NULL) {\r
-    ASSERT (FALSE);\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-  \r
-  memset (mPdbNameModHandleArray + PreviousSize, 0, MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE * sizeof (PDB_NAME_TO_MOD_HANDLE));\r
\r
-  return AddModHandle (ImageContext, ModHandle);\r
-}\r
-\r
-\r
-VOID *\r
-RemoveModeHandle (\r
-  IN  PE_COFF_LOADER_IMAGE_CONTEXT         *ImageContext\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-  Return the ModHandle and delete the entry in the array.\r
-\r
-Arguments:\r
-  ImageContext - Input data returned from PE Laoder Library. Used to find the \r
-                 .PDB file name of the PE Image.\r
-\r
-Returns:\r
-  ModHandle - ModHandle assoicated with ImageContext is returned\r
-  NULL      - No ModHandle associated with ImageContext\r
-\r
---*/\r
-{\r
-  UINTN                   Index;\r
-  PDB_NAME_TO_MOD_HANDLE  *Array;\r
-\r
-  if (ImageContext->PdbPointer == NULL) {\r
-    //\r
-    // If no PDB pointer there is no ModHandle so return NULL\r
-    //\r
-    return NULL;\r
-  }\r
-\r
-  Array = mPdbNameModHandleArray;\r
-  for (Index = 0; Index < mPdbNameModHandleArraySize; Index++, Array++) {\r
-    if ((Array->PdbPointer != NULL) && (strcmp(Array->PdbPointer, ImageContext->PdbPointer) == 0)) {\r
-      //\r
-      // If you find a match return it and delete the entry\r
-      //\r
-      free (Array->PdbPointer);\r
-      Array->PdbPointer = NULL;\r
-      return Array->ModHandle;\r
-    }\r
-  }\r
-\r
-  return NULL;\r
-}\r
-\r
-\r
-\r
-EFI_STATUS\r
-EFIAPI\r
-SecNt32PeCoffGetImageInfo (\r
-  IN EFI_PEI_PE_COFF_LOADER_PROTOCOL          *This,\r
-  IN OUT PE_COFF_LOADER_IMAGE_CONTEXT         *ImageContext\r
-  )\r
-{\r
-  EFI_STATUS  Status;\r
-\r
-  Status = PeCoffLoaderGetImageInfo (ImageContext);\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
-  }\r
-\r
-  switch (ImageContext->ImageType) {\r
-\r
-  case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION:\r
-    ImageContext->ImageCodeMemoryType = EfiLoaderCode;\r
-    ImageContext->ImageDataMemoryType = EfiLoaderData;\r
-    break;\r
-\r
-  case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:\r
-    ImageContext->ImageCodeMemoryType = EfiBootServicesCode;\r
-    ImageContext->ImageDataMemoryType = EfiBootServicesData;\r
-    break;\r
-\r
-  case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:\r
-  case EFI_IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER:\r
-    ImageContext->ImageCodeMemoryType = EfiRuntimeServicesCode;\r
-    ImageContext->ImageDataMemoryType = EfiRuntimeServicesData;\r
-    break;\r
-\r
-  default:\r
-    ImageContext->ImageError = IMAGE_ERROR_INVALID_SUBSYSTEM;\r
-    return RETURN_UNSUPPORTED;\r
-  }\r
-\r
-  return Status;\r
-}\r
-\r
-EFI_STATUS\r
-EFIAPI\r
-SecNt32PeCoffLoadImage (\r
-  IN EFI_PEI_PE_COFF_LOADER_PROTOCOL          *This,\r
-  IN OUT PE_COFF_LOADER_IMAGE_CONTEXT         *ImageContext\r
-  )\r
-{\r
-  EFI_STATUS  Status;\r
-\r
-  Status = PeCoffLoaderLoadImage (ImageContext);\r
-  return Status;\r
-}\r
-\r
-EFI_STATUS\r
-EFIAPI\r
 SecNt32PeCoffRelocateImage (\r
-  IN EFI_PEI_PE_COFF_LOADER_PROTOCOL          *This,\r
   IN OUT PE_COFF_LOADER_IMAGE_CONTEXT         *ImageContext\r
   )\r
 {\r
@@ -1167,11 +1019,10 @@ SecNt32PeCoffRelocateImage (
     }\r
 \r
     if ((Library != NULL) && (DllEntryPoint != NULL)) {\r
-      AddModHandle (ImageContext, Library);\r
       ImageContext->EntryPoint  = (EFI_PHYSICAL_ADDRESS) (UINTN) DllEntryPoint;\r
-      wprintf (L"LoadLibraryEx (%s,\n               NULL, DONT_RESOLVE_DLL_REFERENCES)\n", DllFileName);\r
+      SecPrint ("LoadLibraryEx (%S,\n               NULL, DONT_RESOLVE_DLL_REFERENCES)\n", DllFileName);\r
     } else {\r
-      wprintf (L"WARNING: No source level debug %s. \n", DllFileName);\r
+      SecPrint ("WARNING: No source level debug %S. \n", DllFileName);\r
     }\r
 \r
     free (DllFileName);\r
@@ -1186,21 +1037,7 @@ SecNt32PeCoffRelocateImage (
 }\r
 \r
 \r
-EFI_STATUS\r
-EFIAPI\r
-SecNt32PeCoffUnloadimage (\r
-  IN EFI_PEI_PE_COFF_LOADER_PROTOCOL      *This,\r
-  IN PE_COFF_LOADER_IMAGE_CONTEXT         *ImageContext\r
-  )\r
-{\r
-  VOID *ModHandle;\r
 \r
-  ModHandle = RemoveModeHandle (ImageContext);\r
-  if (ModHandle != NULL) {\r
-    FreeLibrary (ModHandle);\r
-  }\r
-  return EFI_SUCCESS;\r
-}\r
 \r
 VOID\r
 _ModuleEntryPoint (\r
@@ -1209,3 +1046,44 @@ _ModuleEntryPoint (
 {\r
 }\r
 \r
+EFI_STATUS\r
+EFIAPI\r
+SecTemporaryRamSupport (\r
+  IN CONST EFI_PEI_SERVICES   **PeiServices,\r
+  IN EFI_PHYSICAL_ADDRESS     TemporaryMemoryBase,\r
+  IN EFI_PHYSICAL_ADDRESS     PermanentMemoryBase,\r
+  IN UINTN                    CopySize\r
+  )\r
+{\r
+  //\r
+  // Migrate the whole temporary memory to permenent memory.\r
+  // \r
+  CopyMem (\r
+    (VOID*)(UINTN)PermanentMemoryBase, \r
+    (VOID*)(UINTN)TemporaryMemoryBase, \r
+    CopySize\r
+    );\r
+\r
+  //\r
+  // SecSwitchStack function must be invoked after the memory migration\r
+  // immediatly, also we need fixup the stack change caused by new call into \r
+  // permenent memory.\r
+  // \r
+  SecSwitchStack (\r
+    (UINT32) TemporaryMemoryBase,\r
+    (UINT32) PermanentMemoryBase\r
+    );\r
+\r
+  //\r
+  // We need *not* fix the return address because currently, \r
+  // The PeiCore is excuted in flash.\r
+  //\r
+\r
+  //\r
+  // Simulate to invalid temporary memory, terminate temporary memory\r
+  // \r
+  //ZeroMem ((VOID*)(UINTN)TemporaryMemoryBase, CopySize);\r
+  \r
+  return EFI_SUCCESS;\r
+}\r
+\r