]> git.proxmox.com Git - mirror_edk2.git/blobdiff - EmulatorPkg/Win/Host/WinHost.c
EmulatorPkg: Record Argc, Argv and Envp in EmuThunk Ppi
[mirror_edk2.git] / EmulatorPkg / Win / Host / WinHost.c
index 0838c56ddea80ff7d784ec4190617e32fb2418ba..193a947fbdaac9aff16e5ea626b51cfb46e187b4 100644 (file)
@@ -8,7 +8,7 @@
   This code produces 128 K of temporary memory for the SEC stack by directly\r
   allocate memory space with ReadWrite and Execute attribute.\r
 \r
-Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2022, Intel Corporation. All rights reserved.<BR>\r
 (C) Copyright 2016-2020 Hewlett Packard Enterprise Development LP<BR>\r
 SPDX-License-Identifier: BSD-2-Clause-Patent\r
 **/\r
@@ -16,26 +16,26 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #include "WinHost.h"\r
 \r
 #ifndef SE_TIME_ZONE_NAME\r
-#define SE_TIME_ZONE_NAME                 TEXT("SeTimeZonePrivilege")\r
+#define SE_TIME_ZONE_NAME  TEXT("SeTimeZonePrivilege")\r
 #endif\r
 \r
 //\r
 // The growth size for array of module handle entries\r
 //\r
-#define MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE 0x100\r
+#define MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE  0x100\r
 \r
 //\r
 // Module handle entry structure\r
 //\r
 typedef struct {\r
-  CHAR8   *PdbPointer;\r
-  VOID    *ModHandle;\r
+  CHAR8    *PdbPointer;\r
+  VOID     *ModHandle;\r
 } PDB_NAME_TO_MOD_HANDLE;\r
 \r
 //\r
 // An Array to hold the module handles\r
 //\r
-PDB_NAME_TO_MOD_HANDLE  *mPdbNameModHandleArray = NULL;\r
+PDB_NAME_TO_MOD_HANDLE  *mPdbNameModHandleArray    = NULL;\r
 UINTN                   mPdbNameModHandleArraySize = 0;\r
 \r
 //\r
@@ -44,8 +44,8 @@ UINTN                   mPdbNameModHandleArraySize = 0;
 //  The number of array elements is allocated base on parsing\r
 //  PcdWinNtFirmwareVolume and the memory is never freed.\r
 //\r
-UINTN                                     gFdInfoCount = 0;\r
-NT_FD_INFO                                *gFdInfo;\r
+UINTN       gFdInfoCount = 0;\r
+NT_FD_INFO  *gFdInfo;\r
 \r
 //\r
 // Array that supports separate memory ranges.\r
@@ -53,8 +53,16 @@ NT_FD_INFO                                *gFdInfo;
 //  The number of array elements is allocated base on parsing\r
 //  PcdWinNtMemorySizeForSecMain value and the memory is never freed.\r
 //\r
-UINTN                                     gSystemMemoryCount = 0;\r
-NT_SYSTEM_MEMORY                          *gSystemMemory;\r
+UINTN             gSystemMemoryCount = 0;\r
+NT_SYSTEM_MEMORY  *gSystemMemory;\r
+\r
+BASE_LIBRARY_JUMP_BUFFER  mResetJumpBuffer;\r
+CHAR8                     *mResetTypeStr[] = {\r
+  "EfiResetCold",\r
+  "EfiResetWarm",\r
+  "EfiResetShutdown",\r
+  "EfiResetPlatformSpecific"\r
+};\r
 \r
 /*++\r
 \r
@@ -87,14 +95,6 @@ WinPeiAutoScan (
     return EFI_UNSUPPORTED;\r
   }\r
 \r
-  //\r
-  // Allocate enough memory space for emulator\r
-  //\r
-  gSystemMemory[Index].Memory = (EFI_PHYSICAL_ADDRESS) (UINTN) VirtualAlloc (NULL, (SIZE_T) (gSystemMemory[Index].Size), MEM_COMMIT, PAGE_EXECUTE_READWRITE);\r
-  if (gSystemMemory[Index].Memory == 0) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-\r
   *MemoryBase = gSystemMemory[Index].Memory;\r
   *MemorySize = gSystemMemory[Index].Size;\r
 \r
@@ -130,12 +130,11 @@ WinFdAddress (
     return EFI_UNSUPPORTED;\r
   }\r
 \r
-\r
   *FdBase = (EFI_PHYSICAL_ADDRESS)(UINTN)gFdInfo[Index].Address;\r
   *FdSize = (UINT64)gFdInfo[Index].Size;\r
   *FixUp  = 0;\r
 \r
-  if (*FdBase == 0 && *FdSize == 0) {\r
+  if ((*FdBase == 0) && (*FdSize == 0)) {\r
     return EFI_UNSUPPORTED;\r
   }\r
 \r
@@ -173,8 +172,7 @@ WinThunk (
   return &gEmuThunkProtocol;\r
 }\r
 \r
-\r
-EMU_THUNK_PPI mSecEmuThunkPpi = {\r
+EMU_THUNK_PPI  mSecEmuThunkPpi = {\r
   WinPeiAutoScan,\r
   WinFdAddress,\r
   WinThunk\r
@@ -206,6 +204,59 @@ SecPrint (
     );\r
 }\r
 \r
+/**\r
+  Resets the entire platform.\r
+\r
+  @param[in] ResetType      The type of reset to perform.\r
+  @param[in] ResetStatus    The status code for the reset.\r
+  @param[in] DataSize       The size, in bytes, of ResetData.\r
+  @param[in] ResetData      For a ResetType of EfiResetCold, EfiResetWarm, or EfiResetShutdown\r
+                            the data buffer starts with a Null-terminated string, optionally\r
+                            followed by additional binary data. The string is a description\r
+                            that the caller may use to further indicate the reason for the\r
+                            system reset.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+WinReset (\r
+  IN EFI_RESET_TYPE  ResetType,\r
+  IN EFI_STATUS      ResetStatus,\r
+  IN UINTN           DataSize,\r
+  IN VOID            *ResetData OPTIONAL\r
+  )\r
+{\r
+  UINTN  Index;\r
+\r
+  ASSERT (ResetType <= EfiResetPlatformSpecific);\r
+  SecPrint ("  Emu ResetSystem is called: ResetType = %s\n", mResetTypeStr[ResetType]);\r
+\r
+  if (ResetType == EfiResetShutdown) {\r
+    exit (0);\r
+  } else {\r
+    //\r
+    // Unload all DLLs\r
+    //\r
+    for (Index = 0; Index < mPdbNameModHandleArraySize; Index++) {\r
+      if (mPdbNameModHandleArray[Index].PdbPointer != NULL) {\r
+        SecPrint ("  Emu Unload DLL: %s\n", mPdbNameModHandleArray[Index].PdbPointer);\r
+        FreeLibrary (mPdbNameModHandleArray[Index].ModHandle);\r
+        HeapFree (GetProcessHeap (), 0, mPdbNameModHandleArray[Index].PdbPointer);\r
+        mPdbNameModHandleArray[Index].PdbPointer = NULL;\r
+      }\r
+    }\r
+\r
+    //\r
+    // Jump back to SetJump with jump code = ResetType + 1\r
+    //\r
+    LongJump (&mResetJumpBuffer, ResetType + 1);\r
+  }\r
+}\r
+\r
+EFI_PEI_RESET2_PPI  mEmuReset2Ppi = {\r
+  WinReset\r
+};\r
+\r
 /*++\r
 \r
 Routine Description:\r
@@ -227,7 +278,7 @@ Returns:
 **/\r
 BOOLEAN\r
 EfiSystemMemoryRange (\r
-  IN  VOID *MemoryAddress\r
+  IN  VOID  *MemoryAddress\r
   )\r
 {\r
   UINTN                 Index;\r
@@ -236,7 +287,8 @@ EfiSystemMemoryRange (
   MemoryBase = (EFI_PHYSICAL_ADDRESS)(UINTN)MemoryAddress;\r
   for (Index = 0; Index < gSystemMemoryCount; Index++) {\r
     if ((MemoryBase >= gSystemMemory[Index].Memory) &&\r
-        (MemoryBase < (gSystemMemory[Index].Memory + gSystemMemory[Index].Size)) ) {\r
+        (MemoryBase < (gSystemMemory[Index].Memory + gSystemMemory[Index].Size)))\r
+    {\r
       return TRUE;\r
     }\r
   }\r
@@ -244,15 +296,15 @@ EfiSystemMemoryRange (
   return FALSE;\r
 }\r
 \r
-\r
 EFI_STATUS\r
 WinNtOpenFile (\r
-  IN  CHAR16                    *FileName,            OPTIONAL\r
-  IN  UINT32                    MapSize,\r
-  IN  DWORD                     CreationDisposition,\r
-  IN OUT  VOID                  **BaseAddress,\r
-  OUT UINTN                     *Length\r
+  IN  CHAR16    *FileName             OPTIONAL,\r
+  IN  UINT32    MapSize,\r
+  IN  DWORD     CreationDisposition,\r
+  IN OUT  VOID  **BaseAddress,\r
+  OUT UINTN     *Length\r
   )\r
+\r
 /*++\r
 \r
 Routine Description:\r
@@ -300,6 +352,7 @@ Returns:
       return EFI_NOT_FOUND;\r
     }\r
   }\r
+\r
   //\r
   // Map the open file into a memory range\r
   //\r
@@ -314,17 +367,18 @@ Returns:
   if (NtMapHandle == NULL) {\r
     return EFI_DEVICE_ERROR;\r
   }\r
+\r
   //\r
   // Get the virtual address (address in the emulator) of the mapped file\r
   //\r
   VirtualAddress = MapViewOfFileEx (\r
-                    NtMapHandle,\r
-                    FILE_MAP_EXECUTE | FILE_MAP_ALL_ACCESS,\r
-                    0,\r
-                    0,\r
-                    MapSize,\r
-                    *BaseAddress\r
-                    );\r
+                     NtMapHandle,\r
+                     FILE_MAP_EXECUTE | FILE_MAP_ALL_ACCESS,\r
+                     0,\r
+                     0,\r
+                     MapSize,\r
+                     *BaseAddress\r
+                     );\r
   if (VirtualAddress == NULL) {\r
     return EFI_DEVICE_ERROR;\r
   }\r
@@ -334,11 +388,11 @@ Returns:
     // Seek to the end of the file to figure out the true file size.\r
     //\r
     FileSize = SetFilePointer (\r
-                NtFileHandle,\r
-                0,\r
-                NULL,\r
-                FILE_END\r
-                );\r
+                 NtFileHandle,\r
+                 0,\r
+                 NULL,\r
+                 FILE_END\r
+                 );\r
     if (FileSize == -1) {\r
       return EFI_DEVICE_ERROR;\r
     }\r
@@ -356,10 +410,11 @@ Returns:
 INTN\r
 EFIAPI\r
 main (\r
-  IN  INT  Argc,\r
-  IN  CHAR8 **Argv,\r
-  IN  CHAR8 **Envp\r
+  IN  INT    Argc,\r
+  IN  CHAR8  **Argv,\r
+  IN  CHAR8  **Envp\r
   )\r
+\r
 /*++\r
 \r
 Routine Description:\r
@@ -376,37 +431,39 @@ Returns:
 \r
 --*/\r
 {\r
-  EFI_STATUS            Status;\r
-  HANDLE                Token;\r
-  TOKEN_PRIVILEGES      TokenPrivileges;\r
-  VOID                  *TemporaryRam;\r
-  UINT32                TemporaryRamSize;\r
-  VOID                  *EmuMagicPage;\r
-  UINTN                 Index;\r
-  UINTN                 Index1;\r
-  CHAR16                *FileName;\r
-  CHAR16                *FileNamePtr;\r
-  BOOLEAN               Done;\r
-  EFI_PEI_FILE_HANDLE   FileHandle;\r
-  VOID                  *SecFile;\r
-  CHAR16                *MemorySizeStr;\r
-  CHAR16                *FirmwareVolumesStr;\r
-  UINTN                 ProcessAffinityMask;\r
-  UINTN                 SystemAffinityMask;\r
-  INT32                 LowBit;\r
+  EFI_STATUS           Status;\r
+  HANDLE               Token;\r
+  TOKEN_PRIVILEGES     TokenPrivileges;\r
+  VOID                 *TemporaryRam;\r
+  UINT32               TemporaryRamSize;\r
+  VOID                 *EmuMagicPage;\r
+  UINTN                Index;\r
+  UINTN                Index1;\r
+  CHAR16               *FileName;\r
+  CHAR16               *FileNamePtr;\r
+  BOOLEAN              Done;\r
+  EFI_PEI_FILE_HANDLE  FileHandle;\r
+  VOID                 *SecFile;\r
+  CHAR16               *MemorySizeStr;\r
+  CHAR16               *FirmwareVolumesStr;\r
+  UINTN                ProcessAffinityMask;\r
+  UINTN                SystemAffinityMask;\r
+  INT32                LowBit;\r
+  UINTN                ResetJumpCode;\r
+  EMU_THUNK_PPI        *SecEmuThunkPpi;\r
 \r
   //\r
   // Enable the privilege so that RTC driver can successfully run SetTime()\r
   //\r
-  OpenProcessToken (GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &Token);\r
-  if (LookupPrivilegeValue(NULL, SE_TIME_ZONE_NAME, &TokenPrivileges.Privileges[0].Luid)) {\r
-    TokenPrivileges.PrivilegeCount = 1;\r
+  OpenProcessToken (GetCurrentProcess (), TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &Token);\r
+  if (LookupPrivilegeValue (NULL, SE_TIME_ZONE_NAME, &TokenPrivileges.Privileges[0].Luid)) {\r
+    TokenPrivileges.PrivilegeCount           = 1;\r
     TokenPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;\r
-    AdjustTokenPrivileges(Token, FALSE, &TokenPrivileges, 0, (PTOKEN_PRIVILEGES) NULL, 0);\r
+    AdjustTokenPrivileges (Token, FALSE, &TokenPrivileges, 0, (PTOKEN_PRIVILEGES)NULL, 0);\r
   }\r
 \r
-  MemorySizeStr      = (CHAR16 *) PcdGetPtr (PcdEmuMemorySize);\r
-  FirmwareVolumesStr = (CHAR16 *) PcdGetPtr (PcdEmuFirmwareVolume);\r
+  MemorySizeStr      = (CHAR16 *)PcdGetPtr (PcdEmuMemorySize);\r
+  FirmwareVolumesStr = (CHAR16 *)PcdGetPtr (PcdEmuFirmwareVolume);\r
 \r
   SecPrint ("\n\rEDK II WIN Host Emulation Environment from http://www.tianocore.org/edk2/\n\r");\r
 \r
@@ -435,7 +492,19 @@ Returns:
   //\r
   // PPIs pased into PEI_CORE\r
   //\r
-  AddThunkPpi (EFI_PEI_PPI_DESCRIPTOR_PPI, &gEmuThunkPpiGuid, &mSecEmuThunkPpi);\r
+  SecEmuThunkPpi = AllocateZeroPool (sizeof (EMU_THUNK_PPI) + FixedPcdGet32 (PcdPersistentMemorySize));\r
+  if (SecEmuThunkPpi == NULL) {\r
+    SecPrint ("ERROR : Can not allocate memory for SecEmuThunkPpi.  Exiting.\n");\r
+    exit (1);\r
+  }\r
+\r
+  CopyMem (SecEmuThunkPpi, &mSecEmuThunkPpi, sizeof (EMU_THUNK_PPI));\r
+  SecEmuThunkPpi->Argc                 = Argc;\r
+  SecEmuThunkPpi->Argv                 = Argv;\r
+  SecEmuThunkPpi->Envp                 = Envp;\r
+  SecEmuThunkPpi->PersistentMemorySize = FixedPcdGet32 (PcdPersistentMemorySize);\r
+  AddThunkPpi (EFI_PEI_PPI_DESCRIPTOR_PPI, &gEmuThunkPpiGuid, SecEmuThunkPpi);\r
+  AddThunkPpi (EFI_PEI_PPI_DESCRIPTOR_PPI, &gEfiPeiReset2PpiGuid, &mEmuReset2Ppi);\r
 \r
   //\r
   // Emulator Bus Driver Thunks\r
@@ -448,22 +517,47 @@ Returns:
   //\r
   // Allocate space for gSystemMemory Array\r
   //\r
-  gSystemMemoryCount  = CountSeparatorsInString (MemorySizeStr, '!') + 1;\r
-  gSystemMemory       = calloc (gSystemMemoryCount, sizeof (NT_SYSTEM_MEMORY));\r
+  gSystemMemoryCount = CountSeparatorsInString (MemorySizeStr, '!') + 1;\r
+  gSystemMemory      = calloc (gSystemMemoryCount, sizeof (NT_SYSTEM_MEMORY));\r
   if (gSystemMemory == NULL) {\r
     SecPrint ("ERROR : Can not allocate memory for %S.  Exiting.\n\r", MemorySizeStr);\r
     exit (1);\r
   }\r
 \r
+  //\r
+  // Allocate "physical" memory space for emulator. It will be reported out later throuth MemoryAutoScan()\r
+  //\r
+  for (Index = 0, Done = FALSE; !Done; Index++) {\r
+    ASSERT (Index < gSystemMemoryCount);\r
+    gSystemMemory[Index].Size   = ((UINT64)_wtoi (MemorySizeStr)) * ((UINT64)SIZE_1MB);\r
+    gSystemMemory[Index].Memory = (EFI_PHYSICAL_ADDRESS)(UINTN)VirtualAlloc (NULL, (SIZE_T)(gSystemMemory[Index].Size), MEM_COMMIT, PAGE_EXECUTE_READWRITE);\r
+    if (gSystemMemory[Index].Memory == 0) {\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+\r
+    //\r
+    // Find the next region\r
+    //\r
+    for (Index1 = 0; MemorySizeStr[Index1] != '!' && MemorySizeStr[Index1] != 0; Index1++) {\r
+    }\r
+\r
+    if (MemorySizeStr[Index1] == 0) {\r
+      Done = TRUE;\r
+    }\r
+\r
+    MemorySizeStr = MemorySizeStr + Index1 + 1;\r
+  }\r
+\r
   //\r
   // Allocate space for gSystemMemory Array\r
   //\r
-  gFdInfoCount  = CountSeparatorsInString (FirmwareVolumesStr, '!') + 1;\r
-  gFdInfo       = calloc (gFdInfoCount, sizeof (NT_FD_INFO));\r
+  gFdInfoCount = CountSeparatorsInString (FirmwareVolumesStr, '!') + 1;\r
+  gFdInfo      = calloc (gFdInfoCount, sizeof (NT_FD_INFO));\r
   if (gFdInfo == NULL) {\r
     SecPrint ("ERROR : Can not allocate memory for %S.  Exiting.\n\r", FirmwareVolumesStr);\r
     exit (1);\r
   }\r
+\r
   //\r
   // Setup Boot Mode.\r
   //\r
@@ -475,17 +569,11 @@ Returns:
   //  Set TemporaryRam to zero so WinNtOpenFile will allocate a new mapping\r
   //\r
   TemporaryRamSize = TEMPORARY_RAM_SIZE;\r
-  TemporaryRam     = VirtualAlloc (NULL, (SIZE_T) (TemporaryRamSize), MEM_COMMIT, PAGE_EXECUTE_READWRITE);\r
+  TemporaryRam     = VirtualAlloc (NULL, (SIZE_T)(TemporaryRamSize), MEM_COMMIT, PAGE_EXECUTE_READWRITE);\r
   if (TemporaryRam == NULL) {\r
     SecPrint ("ERROR : Can not allocate enough space for SecStack\n\r");\r
     exit (1);\r
   }\r
-  SetMem32 (TemporaryRam, TemporaryRamSize, PcdGet32 (PcdInitValueInTempStack));\r
-\r
-  SecPrint ("  OS Emulator passing in %u KB of temp RAM at 0x%08lx to SEC\n\r",\r
-    TemporaryRamSize / SIZE_1KB,\r
-    TemporaryRam\r
-    );\r
 \r
   //\r
   // If enabled use the magic page to communicate between modules\r
@@ -497,12 +585,12 @@ Returns:
   if (EmuMagicPage != NULL) {\r
     UINT64  Size;\r
     Status = WinNtOpenFile (\r
-              NULL,\r
-              SIZE_4KB,\r
-              0,\r
-              &EmuMagicPage,\r
-              &Size\r
-              );\r
+               NULL,\r
+               SIZE_4KB,\r
+               0,\r
+               &EmuMagicPage,\r
+               &Size\r
+               );\r
     if (EFI_ERROR (Status)) {\r
       SecPrint ("ERROR : Could not allocate PeiServicesTablePage @ %p\n\r", EmuMagicPage);\r
       return EFI_DEVICE_ERROR;\r
@@ -521,25 +609,26 @@ Returns:
 \r
   for (Done = FALSE, Index = 0, SecFile = NULL; !Done; Index++) {\r
     FileName = FileNamePtr;\r
-    for (Index1 = 0; (FileNamePtr[Index1] != '!') && (FileNamePtr[Index1] != 0); Index1++)\r
-      ;\r
+    for (Index1 = 0; (FileNamePtr[Index1] != '!') && (FileNamePtr[Index1] != 0); Index1++) {\r
+    }\r
+\r
     if (FileNamePtr[Index1] == 0) {\r
       Done = TRUE;\r
     } else {\r
-      FileNamePtr[Index1]  = '\0';\r
-      FileNamePtr = &FileNamePtr[Index1 + 1];\r
+      FileNamePtr[Index1] = '\0';\r
+      FileNamePtr         = &FileNamePtr[Index1 + 1];\r
     }\r
 \r
     //\r
     // Open the FD and remember where it got mapped into our processes address space\r
     //\r
     Status = WinNtOpenFile (\r
-              FileName,\r
-              0,\r
-              OPEN_EXISTING,\r
-              &gFdInfo[Index].Address,\r
-              &gFdInfo[Index].Size\r
-              );\r
+               FileName,\r
+               0,\r
+               OPEN_EXISTING,\r
+               &gFdInfo[Index].Address,\r
+               &gFdInfo[Index].Size\r
+               );\r
     if (EFI_ERROR (Status)) {\r
       SecPrint ("ERROR : Can not open Firmware Device File %S (0x%X).  Exiting.\n\r", FileName, Status);\r
       exit (1);\r
@@ -553,11 +642,11 @@ Returns:
       // Load the first one we find.\r
       //\r
       FileHandle = NULL;\r
-      Status = PeiServicesFfsFindNextFile (\r
-                  EFI_FV_FILETYPE_SECURITY_CORE,\r
-                  (EFI_PEI_FV_HANDLE)gFdInfo[Index].Address,\r
-                  &FileHandle\r
-                  );\r
+      Status     = PeiServicesFfsFindNextFile (\r
+                     EFI_FV_FILETYPE_SECURITY_CORE,\r
+                     (EFI_PEI_FV_HANDLE)gFdInfo[Index].Address,\r
+                     &FileHandle\r
+                     );\r
       if (!EFI_ERROR (Status)) {\r
         Status = PeiServicesFfsFindSectionData (EFI_SECTION_PE32, FileHandle, &SecFile);\r
         if (!EFI_ERROR (Status)) {\r
@@ -568,31 +657,25 @@ Returns:
 \r
     SecPrint ("\n\r");\r
   }\r
+\r
+  ResetJumpCode = SetJump (&mResetJumpBuffer);\r
+\r
   //\r
-  // Calculate memory regions and store the information in the gSystemMemory\r
-  //  global for later use. The autosizing code will use this data to\r
-  //  map this memory into the SEC process memory space.\r
+  // Do not clear memory content for warm reset.\r
   //\r
-  for (Index = 0, Done = FALSE; !Done; Index++) {\r
-    //\r
-    // Save the size of the memory and make a Unicode filename SystemMemory00, ...\r
-    //\r
-    gSystemMemory[Index].Size = _wtoi (MemorySizeStr) * SIZE_1MB;\r
-\r
-    //\r
-    // Find the next region\r
-    //\r
-    for (Index1 = 0; MemorySizeStr[Index1] != '!' && MemorySizeStr[Index1] != 0; Index1++)\r
-      ;\r
-    if (MemorySizeStr[Index1] == 0) {\r
-      Done = TRUE;\r
+  if (ResetJumpCode != EfiResetWarm + 1) {\r
+    SecPrint ("  OS Emulator clearing temp RAM and physical RAM (to be discovered later)......\n\r");\r
+    SetMem32 (TemporaryRam, TemporaryRamSize, PcdGet32 (PcdInitValueInTempStack));\r
+    for (Index = 0; Index < gSystemMemoryCount; Index++) {\r
+      SetMem32 ((VOID *)(UINTN)gSystemMemory[Index].Memory, (UINTN)gSystemMemory[Index].Size, PcdGet32 (PcdInitValueInTempStack));\r
     }\r
-\r
-    MemorySizeStr = MemorySizeStr + Index1 + 1;\r
   }\r
 \r
-  SecPrint ("\n\r");\r
-\r
+  SecPrint (\r
+    "  OS Emulator passing in %u KB of temp RAM at 0x%08lx to SEC\n\r",\r
+    TemporaryRamSize / SIZE_1KB,\r
+    TemporaryRam\r
+    );\r
   //\r
   // Hand off to SEC Core\r
   //\r
@@ -608,12 +691,13 @@ Returns:
 \r
 VOID\r
 SecLoadSecCore (\r
-  IN  UINTN   TemporaryRam,\r
-  IN  UINTN   TemporaryRamSize,\r
-  IN  VOID    *BootFirmwareVolumeBase,\r
-  IN  UINTN   BootFirmwareVolumeSize,\r
-  IN  VOID    *SecCorePe32File\r
+  IN  UINTN  TemporaryRam,\r
+  IN  UINTN  TemporaryRamSize,\r
+  IN  VOID   *BootFirmwareVolumeBase,\r
+  IN  UINTN  BootFirmwareVolumeSize,\r
+  IN  VOID   *SecCorePe32File\r
   )\r
+\r
 /*++\r
 \r
 Routine Description:\r
@@ -630,11 +714,11 @@ Returns:
 \r
 --*/\r
 {\r
-  EFI_STATUS                  Status;\r
-  VOID                        *TopOfStack;\r
-  VOID                        *SecCoreEntryPoint;\r
-  EFI_SEC_PEI_HAND_OFF        *SecCoreData;\r
-  UINTN                       SecStackSize;\r
+  EFI_STATUS            Status;\r
+  VOID                  *TopOfStack;\r
+  VOID                  *SecCoreEntryPoint;\r
+  EFI_SEC_PEI_HAND_OFF  *SecCoreData;\r
+  UINTN                 SecStackSize;\r
 \r
   //\r
   // Compute Top Of Memory for Stack and PEI Core Allocations\r
@@ -650,37 +734,37 @@ Returns:
   // |  Stack    |\r
   // |-----------| <---- TemporaryRamBase\r
   //\r
-  TopOfStack  = (VOID *)(TemporaryRam + SecStackSize);\r
+  TopOfStack = (VOID *)(TemporaryRam + SecStackSize);\r
 \r
   //\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
+  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
   //\r
-  SecCoreData                         = (EFI_SEC_PEI_HAND_OFF*)(UINTN)TopOfStack;\r
+  SecCoreData                         = (EFI_SEC_PEI_HAND_OFF *)(UINTN)TopOfStack;\r
   SecCoreData->DataSize               = sizeof (EFI_SEC_PEI_HAND_OFF);\r
   SecCoreData->BootFirmwareVolumeBase = BootFirmwareVolumeBase;\r
   SecCoreData->BootFirmwareVolumeSize = BootFirmwareVolumeSize;\r
-  SecCoreData->TemporaryRamBase       = (VOID*)TemporaryRam;\r
+  SecCoreData->TemporaryRamBase       = (VOID *)TemporaryRam;\r
   SecCoreData->TemporaryRamSize       = TemporaryRamSize;\r
   SecCoreData->StackBase              = SecCoreData->TemporaryRamBase;\r
   SecCoreData->StackSize              = SecStackSize;\r
-  SecCoreData->PeiTemporaryRamBase    = (VOID*) ((UINTN) SecCoreData->TemporaryRamBase + SecStackSize);\r
+  SecCoreData->PeiTemporaryRamBase    = (VOID *)((UINTN)SecCoreData->TemporaryRamBase + SecStackSize);\r
   SecCoreData->PeiTemporaryRamSize    = TemporaryRamSize - SecStackSize;\r
 \r
   //\r
   // Load the PEI Core from a Firmware Volume\r
   //\r
   Status = SecPeCoffGetEntryPoint (\r
-            SecCorePe32File,\r
-            &SecCoreEntryPoint\r
-            );\r
+             SecCorePe32File,\r
+             &SecCoreEntryPoint\r
+             );\r
   if (EFI_ERROR (Status)) {\r
-    return ;\r
+    return;\r
   }\r
 \r
   //\r
@@ -695,7 +779,7 @@ Returns:
   //\r
   // If we get here, then the SEC Core returned.  This is an error\r
   //\r
-  return ;\r
+  return;\r
 }\r
 \r
 RETURN_STATUS\r
@@ -705,31 +789,23 @@ SecPeCoffGetEntryPoint (
   IN OUT VOID  **EntryPoint\r
   )\r
 {\r
-  EFI_STATUS                            Status;\r
-  PE_COFF_LOADER_IMAGE_CONTEXT          ImageContext;\r
+  EFI_STATUS                    Status;\r
+  PE_COFF_LOADER_IMAGE_CONTEXT  ImageContext;\r
 \r
   ZeroMem (&ImageContext, sizeof (ImageContext));\r
-  ImageContext.Handle     = Pe32Data;\r
+  ImageContext.Handle = Pe32Data;\r
 \r
-  ImageContext.ImageRead  = (PE_COFF_LOADER_READ_FILE) SecImageRead;\r
+  ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE)SecImageRead;\r
 \r
-  Status                  = PeCoffLoaderGetImageInfo (&ImageContext);\r
+  Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
+\r
   //\r
-  // Allocate space in NT (not emulator) memory with ReadWrite and Execute attribute.\r
-  // Extra space is for alignment\r
-  //\r
-  ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) VirtualAlloc (NULL, (SIZE_T) (ImageContext.ImageSize + (ImageContext.SectionAlignment * 2)), MEM_COMMIT, PAGE_EXECUTE_READWRITE);\r
-  if (ImageContext.ImageAddress == 0) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-  //\r
-  // Align buffer on section boundary\r
+  // XIP for SEC and PEI_CORE\r
   //\r
-  ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;\r
-  ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)ImageContext.SectionAlignment - 1);\r
+  ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)Pe32Data;\r
 \r
   Status = PeCoffLoaderLoadImage (&ImageContext);\r
   if (EFI_ERROR (Status)) {\r
@@ -741,7 +817,7 @@ SecPeCoffGetEntryPoint (
     return Status;\r
   }\r
 \r
-  *EntryPoint   = (VOID *)(UINTN)ImageContext.EntryPoint;\r
+  *EntryPoint = (VOID *)(UINTN)ImageContext.EntryPoint;\r
 \r
   return EFI_SUCCESS;\r
 }\r
@@ -749,11 +825,12 @@ SecPeCoffGetEntryPoint (
 EFI_STATUS\r
 EFIAPI\r
 SecImageRead (\r
-  IN     VOID    *FileHandle,\r
-  IN     UINTN   FileOffset,\r
-  IN OUT UINTN   *ReadSize,\r
-  OUT    VOID    *Buffer\r
+  IN     VOID   *FileHandle,\r
+  IN     UINTN  FileOffset,\r
+  IN OUT UINTN  *ReadSize,\r
+  OUT    VOID   *Buffer\r
   )\r
+\r
 /*++\r
 \r
 Routine Description:\r
@@ -770,13 +847,13 @@ Returns:
 \r
 --*/\r
 {\r
-  CHAR8 *Destination8;\r
-  CHAR8 *Source8;\r
-  UINTN Length;\r
+  CHAR8  *Destination8;\r
+  CHAR8  *Source8;\r
+  UINTN  Length;\r
 \r
-  Destination8  = Buffer;\r
-  Source8       = (CHAR8 *) ((UINTN) FileHandle + FileOffset);\r
-  Length        = *ReadSize;\r
+  Destination8 = Buffer;\r
+  Source8      = (CHAR8 *)((UINTN)FileHandle + FileOffset);\r
+  Length       = *ReadSize;\r
   while (Length--) {\r
     *(Destination8++) = *(Source8++);\r
   }\r
@@ -786,9 +863,10 @@ Returns:
 \r
 CHAR16 *\r
 AsciiToUnicode (\r
-  IN  CHAR8   *Ascii,\r
-  IN  UINTN   *StrLen OPTIONAL\r
+  IN  CHAR8  *Ascii,\r
+  IN  UINTN  *StrLen OPTIONAL\r
   )\r
+\r
 /*++\r
 \r
 Routine Description:\r
@@ -810,15 +888,16 @@ Returns:
   //\r
   // Allocate a buffer for unicode string\r
   //\r
-  for (Index = 0; Ascii[Index] != '\0'; Index++)\r
-    ;\r
+  for (Index = 0; Ascii[Index] != '\0'; Index++) {\r
+  }\r
+\r
   Unicode = malloc ((Index + 1) * sizeof (CHAR16));\r
   if (Unicode == NULL) {\r
     return NULL;\r
   }\r
 \r
   for (Index = 0; Ascii[Index] != '\0'; Index++) {\r
-    Unicode[Index] = (CHAR16) Ascii[Index];\r
+    Unicode[Index] = (CHAR16)Ascii[Index];\r
   }\r
 \r
   Unicode[Index] = '\0';\r
@@ -832,9 +911,10 @@ Returns:
 \r
 UINTN\r
 CountSeparatorsInString (\r
-  IN  CONST CHAR16   *String,\r
-  IN  CHAR16         Separator\r
+  IN  CONST CHAR16  *String,\r
+  IN  CHAR16        Separator\r
   )\r
+\r
 /*++\r
 \r
 Routine Description:\r
@@ -849,7 +929,7 @@ Returns:
 \r
 --*/\r
 {\r
-  UINTN Count;\r
+  UINTN  Count;\r
 \r
   for (Count = 0; *String != '\0'; String++) {\r
     if (*String == Separator) {\r
@@ -871,8 +951,8 @@ Returns:
 --*/\r
 EFI_STATUS\r
 AddModHandle (\r
-  IN  PE_COFF_LOADER_IMAGE_CONTEXT         *ImageContext,\r
-  IN  VOID                                 *ModHandle\r
+  IN  PE_COFF_LOADER_IMAGE_CONTEXT  *ImageContext,\r
+  IN  VOID                          *ModHandle\r
   )\r
 \r
 {\r
@@ -888,7 +968,7 @@ AddModHandle (
   //\r
   Array = mPdbNameModHandleArray;\r
   for (Index = 0; Index < mPdbNameModHandleArraySize; Index++, Array++) {\r
-    if (Array->PdbPointer != NULL && Array->ModHandle == ModHandle) {\r
+    if ((Array->PdbPointer != NULL) && (Array->ModHandle == ModHandle)) {\r
       return EFI_ALREADY_STARTED;\r
     }\r
   }\r
@@ -899,9 +979,9 @@ AddModHandle (
       //\r
       // Make a copy of the stirng and store the ModHandle\r
       //\r
-      Handle = GetProcessHeap ();\r
-      Size = AsciiStrLen (ImageContext->PdbPointer) + 1;\r
-      Array->PdbPointer = HeapAlloc ( Handle, HEAP_ZERO_MEMORY, Size);\r
+      Handle            = GetProcessHeap ();\r
+      Size              = AsciiStrLen (ImageContext->PdbPointer) + 1;\r
+      Array->PdbPointer = HeapAlloc (Handle, HEAP_ZERO_MEMORY, Size);\r
       ASSERT (Array->PdbPointer != NULL);\r
 \r
       AsciiStrCpyS (Array->PdbPointer, Size, ImageContext->PdbPointer);\r
@@ -914,17 +994,18 @@ AddModHandle (
   // No free space in mPdbNameModHandleArray so grow it by\r
   // MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE entires.\r
   //\r
-  PreviousSize = mPdbNameModHandleArraySize * sizeof (PDB_NAME_TO_MOD_HANDLE);\r
+  PreviousSize                = mPdbNameModHandleArraySize * sizeof (PDB_NAME_TO_MOD_HANDLE);\r
   mPdbNameModHandleArraySize += MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE;\r
   //\r
   // re-allocate a new buffer and copy the old values to the new locaiton.\r
   //\r
-  TempArray = HeapAlloc (GetProcessHeap (),\r
-                                HEAP_ZERO_MEMORY,\r
-                                mPdbNameModHandleArraySize * sizeof (PDB_NAME_TO_MOD_HANDLE)\r
-                               );\r
+  TempArray = HeapAlloc (\r
+                GetProcessHeap (),\r
+                HEAP_ZERO_MEMORY,\r
+                mPdbNameModHandleArraySize * sizeof (PDB_NAME_TO_MOD_HANDLE)\r
+                );\r
 \r
-  CopyMem ((VOID *) (UINTN) TempArray, (VOID *) (UINTN)mPdbNameModHandleArray, PreviousSize);\r
+  CopyMem ((VOID *)(UINTN)TempArray, (VOID *)(UINTN)mPdbNameModHandleArray, PreviousSize);\r
 \r
   HeapFree (GetProcessHeap (), 0, mPdbNameModHandleArray);\r
 \r
@@ -947,7 +1028,7 @@ AddModHandle (
 **/\r
 VOID *\r
 RemoveModHandle (\r
-  IN  PE_COFF_LOADER_IMAGE_CONTEXT         *ImageContext\r
+  IN  PE_COFF_LOADER_IMAGE_CONTEXT  *ImageContext\r
   )\r
 {\r
   UINTN                   Index;\r
@@ -962,7 +1043,7 @@ RemoveModHandle (
 \r
   Array = mPdbNameModHandleArray;\r
   for (Index = 0; Index < mPdbNameModHandleArraySize; Index++, Array++) {\r
-    if ((Array->PdbPointer != NULL) && (AsciiStrCmp(Array->PdbPointer, ImageContext->PdbPointer) == 0)) {\r
+    if ((Array->PdbPointer != NULL) && (AsciiStrCmp (Array->PdbPointer, ImageContext->PdbPointer) == 0)) {\r
       //\r
       // If you find a match return it and delete the entry\r
       //\r
@@ -978,14 +1059,14 @@ RemoveModHandle (
 VOID\r
 EFIAPI\r
 PeCoffLoaderRelocateImageExtraAction (\r
-  IN OUT PE_COFF_LOADER_IMAGE_CONTEXT         *ImageContext\r
+  IN OUT PE_COFF_LOADER_IMAGE_CONTEXT  *ImageContext\r
   )\r
 {\r
-  EFI_STATUS        Status;\r
-  VOID              *DllEntryPoint;\r
-  CHAR16            *DllFileName;\r
-  HMODULE           Library;\r
-  UINTN             Index;\r
+  EFI_STATUS  Status;\r
+  VOID        *DllEntryPoint;\r
+  CHAR16      *DllFileName;\r
+  HMODULE     Library;\r
+  UINTN       Index;\r
 \r
   ASSERT (ImageContext != NULL);\r
   //\r
@@ -1003,7 +1084,8 @@ PeCoffLoaderRelocateImageExtraAction (
   // Load the DLL if it's not an EBC image.\r
   //\r
   if ((ImageContext->PdbPointer != NULL) &&\r
-      (ImageContext->Machine != EFI_IMAGE_MACHINE_EBC)) {\r
+      (ImageContext->Machine != EFI_IMAGE_MACHINE_EBC))\r
+  {\r
     //\r
     // Convert filename from ASCII to Unicode\r
     //\r
@@ -1012,7 +1094,7 @@ PeCoffLoaderRelocateImageExtraAction (
     //\r
     // Check that we have a valid filename\r
     //\r
-    if (Index < 5 || DllFileName[Index - 4] != '.') {\r
+    if ((Index < 5) || (DllFileName[Index - 4] != '.')) {\r
       free (DllFileName);\r
 \r
       //\r
@@ -1022,12 +1104,13 @@ PeCoffLoaderRelocateImageExtraAction (
       //\r
       return;\r
     }\r
+\r
     //\r
     // Replace .PDB with .DLL on the filename\r
     //\r
-    DllFileName[Index - 3]  = 'D';\r
-    DllFileName[Index - 2]  = 'L';\r
-    DllFileName[Index - 1]  = 'L';\r
+    DllFileName[Index - 3] = 'D';\r
+    DllFileName[Index - 2] = 'L';\r
+    DllFileName[Index - 1] = 'L';\r
 \r
     //\r
     // Load the .DLL file into the user process's address space for source\r
@@ -1043,8 +1126,7 @@ PeCoffLoaderRelocateImageExtraAction (
       // checking as the we can point to the PE32 image loaded by Tiano. This\r
       // step is only needed for source level debugging\r
       //\r
-      DllEntryPoint = (VOID *) (UINTN) GetProcAddress (Library, "InitializeDriver");\r
-\r
+      DllEntryPoint = (VOID *)(UINTN)GetProcAddress (Library, "InitializeDriver");\r
     }\r
 \r
     if ((Library != NULL) && (DllEntryPoint != NULL)) {\r
@@ -1059,7 +1141,7 @@ PeCoffLoaderRelocateImageExtraAction (
         //\r
         // This DLL is not already loaded, so source level debugging is supported.\r
         //\r
-        ImageContext->EntryPoint  = (EFI_PHYSICAL_ADDRESS) (UINTN) DllEntryPoint;\r
+        ImageContext->EntryPoint = (EFI_PHYSICAL_ADDRESS)(UINTN)DllEntryPoint;\r
         SecPrint ("LoadLibraryEx (\n\r  %S,\n\r  NULL, DONT_RESOLVE_DLL_REFERENCES)\n\r", DllFileName);\r
       }\r
     } else {\r
@@ -1074,7 +1156,7 @@ VOID
 EFIAPI\r
 PeCoffLoaderUnloadImageExtraAction (\r
   IN PE_COFF_LOADER_IMAGE_CONTEXT  *ImageContext\r
-)\r
+  )\r
 {\r
   VOID  *ModHandle;\r
 \r