/**@file\r
\r
-Copyright (c) 2006, Intel Corporation\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>\r
+(C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
+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
#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
\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
SecNt32PeCoffRelocateImage (\r
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
+ ...\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
+ va_end (Marker);\r
+\r
+ CharCount = strlen (Buffer);\r
+ WriteFile (\r
+ GetStdHandle (STD_OUTPUT_HANDLE), \r
+ Buffer,\r
+ (DWORD)CharCount,\r
+ (LPDWORD)&CharCount,\r
+ NULL\r
+ );\r
+}\r
+\r
INTN\r
EFIAPI\r
main (\r
--*/\r
{\r
EFI_STATUS Status;\r
+ HANDLE Token;\r
+ TOKEN_PRIVILEGES TokenPrivileges;\r
EFI_PHYSICAL_ADDRESS InitialStackMemory;\r
UINT64 InitialStackMemorySize;\r
UINTN Index;\r
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
- printf ("\nEDK SEC Main NT Emulation Environment from www.TianoCore.org\n");\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
gSystemMemoryCount = CountSeperatorsInString (MemorySizeStr, '!') + 1;\r
gSystemMemory = calloc (gSystemMemoryCount, sizeof (NT_SYSTEM_MEMORY));\r
if (gSystemMemory == NULL) {\r
- wprintf (L"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
gFdInfoCount = CountSeperatorsInString (FirmwareVolumesStr, '!') + 1;\r
gFdInfo = calloc (gFdInfoCount, sizeof (NT_FD_INFO));\r
if (gFdInfo == NULL) {\r
- wprintf (L"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
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
*StackPointer = 0x5AA55AA5;\r
}\r
\r
- wprintf (L" 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
&gFdInfo[Index].Size\r
);\r
if (EFI_ERROR (Status)) {\r
- printf ("ERROR : Can not open Firmware Device File %S (0x%X). 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
//\r
Status = SecFfsFindPeiCore ((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) gFdInfo[Index].Address, &PeiCoreFile);\r
if (!EFI_ERROR (Status)) {\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
MemorySizeStr = MemorySizeStr + Index1 + 1;\r
}\r
\r
- printf ("\n");\r
+ SecPrint ("\n");\r
\r
//\r
// Hand off to PEI Core\r
// 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
//\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
NtMapHandle = CreateFileMapping (\r
NtFileHandle,\r
NULL,\r
- PAGE_READWRITE,\r
+ PAGE_EXECUTE_READWRITE,\r
0,\r
MapSize,\r
NULL\r
//\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
// 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
//\r
// Processes ASSERT ()\r
//\r
- printf ("ASSERT %s(%d): %s\n", Filename, (int)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
}\r
\r
+#if defined (MDE_CPU_IA32)\r
/**\r
Transfers control to a function starting with a new stack.\r
\r
//\r
ASSERT (FALSE); \r
}\r
+#endif\r
\r
VOID\r
SecLoadFromCore (\r
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
//\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
+ ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)ImageContext.SectionAlignment - 1);\r
\r
Status = PeCoffLoaderLoadImage (&ImageContext);\r
if (EFI_ERROR (Status)) {\r
\r
if ((Library != NULL) && (DllEntryPoint != NULL)) {\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
//\r
\r
//\r
- // Simulate to invalid CAR, terminate CAR\r
+ // Simulate to invalid temporary memory, terminate temporary memory\r
// \r
//ZeroMem ((VOID*)(UINTN)TemporaryMemoryBase, CopySize);\r
\r