]> git.proxmox.com Git - mirror_edk2.git/blobdiff - Nt32Pkg/Sec/SecMain.c
UefiCpuPkg: Remove double \r
[mirror_edk2.git] / Nt32Pkg / Sec / SecMain.c
index f1cf33910cbac69327da7d0107967bea050ffeb6..c3bfe470248389d9448b74b45f196a43ed932ec0 100644 (file)
@@ -1,13 +1,8 @@
 /**@file\r
 \r
-Copyright (c) 2006, 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
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>\r
+(C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 Module Name:\r
 \r
@@ -21,7 +16,7 @@ Abstract:
   will be, how may FD's will be loaded and also what the boot mode is.\r
 \r
   The SEC registers a set of services with the SEC core. gPrivateDispatchTable\r
-  is a list of PPI's produced by the SEC that are availble for usage in PEI.\r
+  is a list of PPI's produced by the SEC that are available for usage in PEI.\r
 \r
   This code produces 128 K of temporary memory for the PEI stack by directly\r
   allocate memory space with ReadWrite and Execute attribute.\r
@@ -30,7 +25,9 @@ Abstract:
 \r
 #include "SecMain.h"\r
 \r
-\r
+#ifndef SE_TIME_ZONE_NAME\r
+#define SE_TIME_ZONE_NAME                 TEXT("SeTimeZonePrivilege")\r
+#endif\r
 \r
 NT_PEI_LOAD_FILE_PPI                      mSecNtLoadFilePpi     = { SecWinNtPeiLoadFile };\r
 \r
@@ -42,7 +39,7 @@ EFI_PEI_PROGRESS_CODE_PPI                 mSecStatusCodePpi     = { SecPeiReport
 \r
 NT_FWH_PPI                                mSecFwhInformationPpi = { SecWinNtFdAddress };\r
 \r
-TEMPORARY_RAM_SUPPORT_PPI                 mSecTemporaryRamSupportPpi = {SecTemporaryRamSupport};\r
+EFI_PEI_TEMPORARY_RAM_SUPPORT_PPI         mSecTemporaryRamSupportPpi = {SecTemporaryRamSupport};\r
 \r
 EFI_PEI_PPI_DESCRIPTOR  gPrivateDispatchTable[] = {\r
   {\r
@@ -107,6 +104,16 @@ SecNt32PeCoffRelocateImage (
   IN OUT PE_COFF_LOADER_IMAGE_CONTEXT         *ImageContext\r
   );\r
 \r
+VOID\r
+EFIAPI\r
+PeiSwitchStacks (\r
+  IN      SWITCH_STACK_ENTRY_POINT  EntryPoint,\r
+  IN      VOID                      *Context1,  OPTIONAL\r
+  IN      VOID                      *Context2,  OPTIONAL\r
+  IN      VOID                      *Context3,  OPTIONAL\r
+  IN      VOID                      *NewStack\r
+  );\r
+\r
 VOID\r
 SecPrint (\r
   CHAR8  *Format,\r
@@ -119,14 +126,15 @@ SecPrint (
 \r
   va_start (Marker, Format);\r
   \r
-//  vsprintf (Buffer, Format, Marker);\r
   _vsnprintf (Buffer, sizeof (Buffer), Format, Marker);\r
 \r
+  va_end (Marker);\r
+\r
   CharCount = strlen (Buffer);\r
   WriteFile (\r
     GetStdHandle (STD_OUTPUT_HANDLE), \r
     Buffer,\r
-    CharCount,\r
+    (DWORD)CharCount,\r
     (LPDWORD)&CharCount,\r
     NULL\r
     );\r
@@ -147,7 +155,7 @@ Routine Description:
 Arguments:\r
   Argc - Number of command line arguments\r
   Argv - Array of command line argument strings\r
-  Envp - Array of environmemt variable strings\r
+  Envp - Array of environment variable strings\r
 \r
 Returns:\r
   0 - Normal exit\r
@@ -156,6 +164,8 @@ Returns:
 --*/\r
 {\r
   EFI_STATUS            Status;\r
+  HANDLE                Token;\r
+  TOKEN_PRIVILEGES      TokenPrivileges;\r
   EFI_PHYSICAL_ADDRESS  InitialStackMemory;\r
   UINT64                InitialStackMemorySize;\r
   UINTN                 Index;\r
@@ -168,12 +178,40 @@ Returns:
   CHAR16                *MemorySizeStr;\r
   CHAR16                *FirmwareVolumesStr;\r
   UINTN                 *StackPointer;\r
+  UINT32                ProcessAffinityMask;\r
+  UINT32                SystemAffinityMask;\r
+  INT32                 LowBit;\r
 \r
-  MemorySizeStr      = (CHAR16 *) FixedPcdGetPtr (PcdWinNtMemorySizeForSecMain);\r
-  FirmwareVolumesStr = (CHAR16 *) FixedPcdGetPtr (PcdWinNtFirmwareVolume);\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
+    TokenPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;\r
+    AdjustTokenPrivileges(Token, FALSE, &TokenPrivileges, 0, (PTOKEN_PRIVILEGES) NULL, 0);\r
+  }\r
+\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
+  // Determine the first thread available to this process.\r
+  //\r
+  if (GetProcessAffinityMask (GetCurrentProcess (), &ProcessAffinityMask, &SystemAffinityMask)) {\r
+    LowBit = (INT32)LowBitSet32 (ProcessAffinityMask);\r
+    if (LowBit != -1) {\r
+      //\r
+      // Force the system to bind the process to a single thread to work\r
+      // around odd semaphore type crashes.\r
+      //\r
+      SetProcessAffinityMask (GetCurrentProcess (), (INTN)(BIT0 << LowBit));\r
+    }\r
+  }\r
+\r
   //\r
   // Make some Windows calls to Set the process to the highest priority in the\r
   //  idle class. We need this to have good performance.\r
@@ -184,7 +222,7 @@ Returns:
   //\r
   // Allocate space for gSystemMemory Array\r
   //\r
-  gSystemMemoryCount  = CountSeperatorsInString (MemorySizeStr, '!') + 1;\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", MemorySizeStr);\r
@@ -193,7 +231,7 @@ Returns:
   //\r
   // Allocate space for gSystemMemory Array\r
   //\r
-  gFdInfoCount  = CountSeperatorsInString (FirmwareVolumesStr, '!') + 1;\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", FirmwareVolumesStr);\r
@@ -202,7 +240,7 @@ Returns:
   //\r
   // Setup Boot Mode. If BootModeStr == "" then BootMode = 0 (BOOT_WITH_FULL_CONFIGURATION)\r
   //\r
-  SecPrint ("  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
@@ -219,7 +257,7 @@ Returns:
   for (StackPointer = (UINTN*) (UINTN) InitialStackMemory;\r
        StackPointer < (UINTN*) ((UINTN)InitialStackMemory + (SIZE_T) InitialStackMemorySize);\r
        StackPointer ++) {\r
-    *StackPointer = 0x5AA55AA5;\r
+    *StackPointer = PcdGet32 (PcdInitValueInTempStack);\r
   }\r
   \r
   SecPrint ("  SEC passing in %d bytes of temp RAM to PEI\n", InitialStackMemorySize);\r
@@ -247,7 +285,7 @@ Returns:
     }\r
 \r
     //\r
-    // Open the FD and remmeber where it got mapped into our processes address space\r
+    // Open the FD and remember where it got mapped into our processes address space\r
     //\r
     Status = WinNtOpenFile (\r
               FileName,\r
@@ -263,7 +301,7 @@ Returns:
 \r
     SecPrint ("  FD loaded from");\r
     //\r
-    // printf can't print filenames directly as the \ gets interperted as an\r
+    // printf can't print filenames directly as the \ gets interpreted as an\r
     //  escape character.\r
     //\r
     for (Index2 = 0; FileName[Index2] != '\0'; Index2++) {\r
@@ -341,7 +379,7 @@ Arguments:
   CreationDisposition - The flags to pass to CreateFile().  Use to create new files for\r
                         memory emulation, and exiting files for firmware volume emulation\r
   BaseAddress         - The base address of the mapped file in the user address space.\r
-                         If passed in as NULL the new memory region is used.\r
+                         If passed in as NULL the new memory region is used.\r
                          If passed in as non NULL the request memory region is used for\r
                           the mapping of the file into the process space.\r
   Length              - The size of the mapped region in bytes\r
@@ -363,7 +401,7 @@ Returns:
   //\r
   NtFileHandle = CreateFile (\r
                   FileName,\r
-                  GENERIC_READ | GENERIC_WRITE,\r
+                  GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE,\r
                   FILE_SHARE_READ,\r
                   NULL,\r
                   CreationDisposition,\r
@@ -379,7 +417,7 @@ Returns:
   NtMapHandle = CreateFileMapping (\r
                   NtFileHandle,\r
                   NULL,\r
-                  PAGE_READWRITE,\r
+                  PAGE_EXECUTE_READWRITE,\r
                   0,\r
                   MapSize,\r
                   NULL\r
@@ -392,7 +430,7 @@ Returns:
   //\r
   VirtualAddress = MapViewOfFileEx (\r
                     NtMapHandle,\r
-                    FILE_MAP_ALL_ACCESS,\r
+                    FILE_MAP_EXECUTE | FILE_MAP_ALL_ACCESS,\r
                     0,\r
                     0,\r
                     MapSize,\r
@@ -490,6 +528,7 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
+#if defined (MDE_CPU_IA32)\r
 /**\r
   Transfers control to a function starting with a new stack.\r
 \r
@@ -547,6 +586,7 @@ PeiSwitchStacks (
   //\r
   ASSERT (FALSE);  \r
 }\r
+#endif\r
 \r
 VOID\r
 SecLoadFromCore (\r
@@ -607,7 +647,7 @@ 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->BootFirmwareVolumeSize = PcdGet32(PcdWinNtFirmwareFdSize);\r
   SecCoreData->TemporaryRamBase       = (VOID*)(UINTN)LargestRegion; \r
   SecCoreData->TemporaryRamSize       = STACK_SIZE;\r
   SecCoreData->StackBase              = SecCoreData->TemporaryRamBase;\r
@@ -655,7 +695,7 @@ SecWinNtPeiAutoScan (
 \r
 Routine Description:\r
   This service is called from Index == 0 until it returns EFI_UNSUPPORTED.\r
-  It allows discontiguous memory regions to be supported by the emulator.\r
+  It allows discontinuous memory regions to be supported by the emulator.\r
   It uses gSystemMemory[] and gSystemMemoryCount that were created by\r
   parsing PcdWinNtMemorySizeForSecMain value.\r
   The size comes from the Pcd value and the address comes from the memory space \r
@@ -700,7 +740,7 @@ SecWinNtWinNtThunkAddress (
 Routine Description:\r
   Since the SEC is the only Windows program in stack it must export\r
   an interface to do Win API calls. That's what the WinNtThunk address\r
-  is for. gWinNt is initailized in WinNtThunk.c.\r
+  is for. gWinNt is initialized in WinNtThunk.c.\r
 \r
 Arguments:\r
   InterfaceSize - sizeof (EFI_WIN_NT_THUNK_PROTOCOL);\r
@@ -753,7 +793,7 @@ Returns:
     return Status;\r
   }\r
   //\r
-  // Allocate space in NT (not emulator) memory with ReadWrite and Execute attribue. \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
@@ -761,10 +801,10 @@ Returns:
     return EFI_OUT_OF_RESOURCES;\r
   }\r
   //\r
-  // Align buffer on section boundry\r
+  // Align buffer on section boundary\r
   //\r
   ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;\r
-  ImageContext.ImageAddress &= ~(ImageContext.SectionAlignment - 1);\r
+  ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)ImageContext.SectionAlignment - 1);\r
 \r
   Status = PeCoffLoaderLoadImage (&ImageContext);\r
   if (EFI_ERROR (Status)) {\r
@@ -807,7 +847,7 @@ Arguments:
 \r
 Returns:\r
   EFI_SUCCESS     - Return the Base address and size of the FV\r
-  EFI_UNSUPPORTED - Index does nto map to an FD in the system\r
+  EFI_UNSUPPORTED - Index does not map to an FD in the system\r
 \r
 --*/\r
 {\r
@@ -910,28 +950,28 @@ Returns:
 }\r
 \r
 UINTN\r
-CountSeperatorsInString (\r
+CountSeparatorsInString (\r
   IN  CONST CHAR16   *String,\r
-  IN  CHAR16         Seperator\r
+  IN  CHAR16         Separator\r
   )\r
 /*++\r
 \r
 Routine Description:\r
-  Count the number of seperators in String\r
+  Count the number of separators in String\r
 \r
 Arguments:\r
   String    - String to process\r
-  Seperator - Item to count\r
+  Separator - Item to count\r
 \r
 Returns:\r
-  Number of Seperator in String\r
+  Number of Separator in String\r
 \r
 --*/\r
 {\r
   UINTN Count;\r
 \r
   for (Count = 0; *String != '\0'; String++) {\r
-    if (*String == Seperator) {\r
+    if (*String == Separator) {\r
       Count++;\r
     }\r
   }\r
@@ -964,7 +1004,7 @@ SecNt32PeCoffRelocateImage (
   // If we load our own PE COFF images the Windows debugger can not source\r
   //  level debug our code. If a valid PDB pointer exists usw it to load\r
   //  the *.dll file as a library using Windows* APIs. This allows \r
-  //  source level debug. The image is still loaded and reloaced\r
+  //  source level debug. The image is still loaded and relocated\r
   //  in the Framework memory space like on a real system (by the code above),\r
   //  but the entry point points into the DLL loaded by the code bellow. \r
   //\r
@@ -1009,11 +1049,11 @@ SecNt32PeCoffRelocateImage (
     if (Library != NULL) {\r
       //\r
       // InitializeDriver is the entry point we put in all our EFI DLL's. The\r
-      // DONT_RESOLVE_DLL_REFERENCES argument to LoadLIbraryEx() supresses the \r
+      // DONT_RESOLVE_DLL_REFERENCES argument to LoadLIbraryEx() suppresses the \r
       // normal DLL entry point of DllMain, and prevents other modules that are\r
       // referenced in side the DllFileName from being loaded. There is no error \r
       // checking as the we can point to the PE32 image loaded by Tiano. This \r
-      // step is only needed for source level debuging\r
+      // step is only needed for source level debugging\r
       //\r
       DllEntryPoint = (VOID *) (UINTN) GetProcAddress (Library, "InitializeDriver");\r
 \r
@@ -1057,7 +1097,7 @@ SecTemporaryRamSupport (
   )\r
 {\r
   //\r
-  // Migrate the whole temporary memory to permenent memory.\r
+  // Migrate the whole temporary memory to permanent memory.\r
   // \r
   CopyMem (\r
     (VOID*)(UINTN)PermanentMemoryBase, \r
@@ -1067,8 +1107,8 @@ SecTemporaryRamSupport (
 \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
+  // immediately, also we need fixup the stack change caused by new call into \r
+  // permanent memory.\r
   // \r
   SecSwitchStack (\r
     (UINT32) TemporaryMemoryBase,\r
@@ -1077,7 +1117,7 @@ SecTemporaryRamSupport (
 \r
   //\r
   // We need *not* fix the return address because currently, \r
-  // The PeiCore is excuted in flash.\r
+  // The PeiCore is executed in flash.\r
   //\r
 \r
   //\r