+++ /dev/null
-/**@file\r
-\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
- SecMain.c\r
-\r
-Abstract:\r
- WinNt emulator of SEC phase. It's really a Win32 application, but this is\r
- Ok since all the other modules for NT32 are NOT Win32 applications.\r
-\r
- This program gets NT32 PCD setting and figures out what the memory layout \r
- 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 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
-\r
-**/\r
-\r
-#include "SecMain.h"\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
-PEI_NT_AUTOSCAN_PPI mSecNtAutoScanPpi = { SecWinNtPeiAutoScan };\r
-\r
-PEI_NT_THUNK_PPI mSecWinNtThunkPpi = { SecWinNtWinNtThunkAddress };\r
-\r
-EFI_PEI_PROGRESS_CODE_PPI mSecStatusCodePpi = { SecPeiReportStatusCode };\r
-\r
-NT_FWH_PPI mSecFwhInformationPpi = { SecWinNtFdAddress };\r
-\r
-EFI_PEI_TEMPORARY_RAM_SUPPORT_PPI mSecTemporaryRamSupportPpi = {SecTemporaryRamSupport};\r
-\r
-EFI_PEI_PPI_DESCRIPTOR gPrivateDispatchTable[] = {\r
- {\r
- EFI_PEI_PPI_DESCRIPTOR_PPI,\r
- &gNtPeiLoadFilePpiGuid,\r
- &mSecNtLoadFilePpi\r
- },\r
- {\r
- EFI_PEI_PPI_DESCRIPTOR_PPI,\r
- &gPeiNtAutoScanPpiGuid,\r
- &mSecNtAutoScanPpi\r
- },\r
- {\r
- EFI_PEI_PPI_DESCRIPTOR_PPI,\r
- &gPeiNtThunkPpiGuid,\r
- &mSecWinNtThunkPpi\r
- },\r
- {\r
- EFI_PEI_PPI_DESCRIPTOR_PPI,\r
- &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
- &mSecFwhInformationPpi\r
- }\r
-};\r
-\r
-\r
-//\r
-// Default information about where the FD is located.\r
-// This array gets filled in with information from PcdWinNtFirmwareVolume\r
-// 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
-\r
-//\r
-// Array that supports seperate memory rantes.\r
-// The memory ranges are set by PcdWinNtMemorySizeForSecMain.\r
-// 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
-\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
-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
- IN INTN Argc,\r
- IN CHAR8 **Argv,\r
- IN CHAR8 **Envp\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- Main entry point to SEC for WinNt. This is a Windows program\r
-\r
-Arguments:\r
- Argc - Number of command line arguments\r
- Argv - Array of command line argument strings\r
- Envp - Array of environment variable strings\r
-\r
-Returns:\r
- 0 - Normal exit\r
- 1 - Abnormal exit\r
-\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
- UINTN Index1;\r
- UINTN Index2;\r
- CHAR16 *FileName;\r
- CHAR16 *FileNamePtr;\r
- BOOLEAN Done;\r
- VOID *PeiCoreFile;\r
- CHAR16 *MemorySizeStr;\r
- CHAR16 *FirmwareVolumesStr;\r
- UINTN *StackPointer;\r
- UINT32 ProcessAffinityMask;\r
- UINT32 SystemAffinityMask;\r
- INT32 LowBit;\r
-\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
- //\r
- SetPriorityClass (GetCurrentProcess (), IDLE_PRIORITY_CLASS);\r
- SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_HIGHEST);\r
-\r
- //\r
- // Allocate space for gSystemMemory Array\r
- //\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
- exit (1);\r
- }\r
- //\r
- // Allocate space for gSystemMemory Array\r
- //\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
- exit (1);\r
- }\r
- //\r
- // Setup Boot Mode. If BootModeStr == "" then BootMode = 0 (BOOT_WITH_FULL_CONFIGURATION)\r
- //\r
- SecPrint (" BootMode 0x%02x\n", PcdGet32 (PcdWinNtBootMode));\r
-\r
- //\r
- // Allocate 128K memory to emulate temp memory for PEI.\r
- // on a real platform this would be SRAM, or using the cache as RAM.\r
- // Set InitialStackMemory to zero so WinNtOpenFile will allocate a new mapping\r
- //\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
- SecPrint ("ERROR : Can not allocate enough space for SecStack\n");\r
- exit (1);\r
- }\r
-\r
- for (StackPointer = (UINTN*) (UINTN) InitialStackMemory;\r
- StackPointer < (UINTN*) ((UINTN)InitialStackMemory + (SIZE_T) InitialStackMemorySize);\r
- StackPointer ++) {\r
- *StackPointer = PcdGet32 (PcdInitValueInTempStack);\r
- }\r
- \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
- 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, PeiCoreFile = NULL; !Done; Index++) {\r
- FileName = FileNamePtr;\r
- for (Index1 = 0; (FileNamePtr[Index1] != '!') && (FileNamePtr[Index1] != 0); Index1++)\r
- ;\r
- if (FileNamePtr[Index1] == 0) {\r
- Done = TRUE;\r
- } else {\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
- if (EFI_ERROR (Status)) {\r
- SecPrint ("ERROR : Can not open Firmware Device File %S (0x%X). Exiting.\n", FileName, Status);\r
- exit (1);\r
- }\r
-\r
- SecPrint (" FD loaded from");\r
- //\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
- SecPrint ("%c", FileName[Index2]);\r
- }\r
-\r
- if (PeiCoreFile == NULL) {\r
- //\r
- // Assume the beginning of the FD is an FV and look for the PEI Core.\r
- // Load the first one we find.\r
- //\r
- Status = SecFfsFindPeiCore ((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) gFdInfo[Index].Address, &PeiCoreFile);\r
- if (!EFI_ERROR (Status)) {\r
- SecPrint (" contains SEC Core");\r
- }\r
- }\r
-\r
- SecPrint ("\n");\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
- //\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) * 0x100000;\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
- }\r
-\r
- MemorySizeStr = MemorySizeStr + Index1 + 1;\r
- }\r
-\r
- SecPrint ("\n");\r
-\r
- //\r
- // Hand off to PEI Core\r
- //\r
- SecLoadFromCore ((UINTN) InitialStackMemory, (UINTN) InitialStackMemorySize, (UINTN) gFdInfo[0].Address, PeiCoreFile);\r
-\r
- //\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
- SecPrint ("ERROR : PEI Core returned\n");\r
- exit (1);\r
-}\r
-\r
-EFI_STATUS\r
-WinNtOpenFile (\r
- IN CHAR16 *FileName,\r
- IN UINT32 MapSize,\r
- IN DWORD CreationDisposition,\r
- IN OUT EFI_PHYSICAL_ADDRESS *BaseAddress,\r
- OUT UINT64 *Length\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- Opens and memory maps a file using WinNt services. If BaseAddress is non zero\r
- the process will try and allocate the memory starting at BaseAddress.\r
-\r
-Arguments:\r
- FileName - The name of the file to open and map\r
- MapSize - The amount of the file to map in bytes\r
- 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 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
-\r
-Returns:\r
- EFI_SUCCESS - The file was opened and mapped.\r
- EFI_NOT_FOUND - FileName was not found in the current directory\r
- EFI_DEVICE_ERROR - An error occured attempting to map the opened file\r
-\r
---*/\r
-{\r
- HANDLE NtFileHandle;\r
- HANDLE NtMapHandle;\r
- VOID *VirtualAddress;\r
- UINTN FileSize;\r
-\r
- //\r
- // Use Win API to open/create a file\r
- //\r
- NtFileHandle = CreateFile (\r
- FileName,\r
- GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE,\r
- FILE_SHARE_READ,\r
- NULL,\r
- CreationDisposition,\r
- FILE_ATTRIBUTE_NORMAL,\r
- NULL\r
- );\r
- if (NtFileHandle == INVALID_HANDLE_VALUE) {\r
- return EFI_NOT_FOUND;\r
- }\r
- //\r
- // Map the open file into a memory range\r
- //\r
- NtMapHandle = CreateFileMapping (\r
- NtFileHandle,\r
- NULL,\r
- PAGE_EXECUTE_READWRITE,\r
- 0,\r
- MapSize,\r
- NULL\r
- );\r
- if (NtMapHandle == NULL) {\r
- return EFI_DEVICE_ERROR;\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
- (LPVOID) (UINTN) *BaseAddress\r
- );\r
- if (VirtualAddress == NULL) {\r
- return EFI_DEVICE_ERROR;\r
- }\r
-\r
- if (MapSize == 0) {\r
- //\r
- // 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
- if (FileSize == -1) {\r
- return EFI_DEVICE_ERROR;\r
- }\r
-\r
- *Length = (UINT64) FileSize;\r
- } else {\r
- *Length = (UINT64) MapSize;\r
- }\r
-\r
- *BaseAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) VirtualAddress;\r
-\r
- return EFI_SUCCESS;\r
-}\r
-\r
-\r
-#define BYTES_PER_RECORD 512\r
-\r
-EFI_STATUS\r
-EFIAPI\r
-SecPeiReportStatusCode (\r
- IN CONST EFI_PEI_SERVICES **PeiServices,\r
- IN EFI_STATUS_CODE_TYPE CodeType,\r
- IN EFI_STATUS_CODE_VALUE Value,\r
- IN UINT32 Instance,\r
- IN CONST EFI_GUID *CallerId,\r
- IN CONST EFI_STATUS_CODE_DATA *Data OPTIONAL\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- This routine produces the ReportStatusCode PEI service. It's passed\r
- up to the PEI Core via a PPI. T\r
-\r
- This code currently uses the NT clib printf. This does not work the same way\r
- as the EFI Print (), as %t, %g, %s as Unicode are not supported.\r
-\r
-Arguments:\r
- (see EFI_PEI_REPORT_STATUS_CODE)\r
-\r
-Returns:\r
- EFI_SUCCESS - Always return success\r
-\r
---*/\r
-// TODO: PeiServices - add argument and description to function comment\r
-// TODO: CodeType - add argument and description to function comment\r
-// TODO: Value - add argument and description to function comment\r
-// TODO: Instance - add argument and description to function comment\r
-// TODO: CallerId - add argument and description to function comment\r
-// TODO: Data - add argument and description to function comment\r
-{\r
- CHAR8 *Format;\r
- BASE_LIST Marker;\r
- CHAR8 PrintBuffer[BYTES_PER_RECORD * 2];\r
- CHAR8 *Filename;\r
- CHAR8 *Description;\r
- UINT32 LineNumber;\r
- UINT32 ErrorLevel;\r
-\r
-\r
- if (Data == NULL) {\r
- } else if (ReportStatusCodeExtractAssertInfo (CodeType, Value, Data, &Filename, &Description, &LineNumber)) {\r
- //\r
- // Processes ASSERT ()\r
- //\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
- 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
- Transfers control to the function specified by EntryPoint using the new stack\r
- specified by NewStack and passing in the parameters specified by Context1 and\r
- Context2. Context1 and Context2 are optional and may be NULL. The function\r
- EntryPoint must never return.\r
-\r
- If EntryPoint is NULL, then ASSERT().\r
- If NewStack is NULL, then ASSERT().\r
-\r
- @param EntryPoint A pointer to function to call with the new stack.\r
- @param Context1 A pointer to the context to pass into the EntryPoint\r
- function.\r
- @param Context2 A pointer to the context to pass into the EntryPoint\r
- function.\r
- @param NewStack A pointer to the new stack to use for the EntryPoint\r
- function.\r
- @param NewBsp A pointer to the new BSP for the EntryPoint on IPF. It's\r
- Reserved on other architectures.\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
- BASE_LIBRARY_JUMP_BUFFER JumpBuffer;\r
- \r
- ASSERT (EntryPoint != NULL);\r
- ASSERT (NewStack != NULL);\r
-\r
- //\r
- // Stack should be aligned with CPU_STACK_ALIGNMENT\r
- //\r
- ASSERT (((UINTN)NewStack & (CPU_STACK_ALIGNMENT - 1)) == 0);\r
-\r
- JumpBuffer.Eip = (UINTN)EntryPoint;\r
- JumpBuffer.Esp = (UINTN)NewStack - sizeof (VOID*);\r
- JumpBuffer.Esp -= sizeof (Context1) + sizeof (Context2) + sizeof(Context3);\r
- ((VOID**)JumpBuffer.Esp)[1] = Context1;\r
- ((VOID**)JumpBuffer.Esp)[2] = Context2;\r
- ((VOID**)JumpBuffer.Esp)[3] = Context3;\r
-\r
- LongJump (&JumpBuffer, (UINTN)-1);\r
- \r
-\r
- //\r
- // InternalSwitchStack () will never return\r
- //\r
- ASSERT (FALSE); \r
-}\r
-#endif\r
-\r
-VOID\r
-SecLoadFromCore (\r
- IN UINTN LargestRegion,\r
- IN UINTN LargestRegionSize,\r
- IN UINTN BootFirmwareVolumeBase,\r
- IN VOID *PeiCorePe32File\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- This is the service to load the PEI Core from the Firmware Volume\r
-\r
-Arguments:\r
- LargestRegion - Memory to use for PEI.\r
- LargestRegionSize - Size of Memory to use for PEI\r
- BootFirmwareVolumeBase - Start of the Boot FV\r
- PeiCorePe32File - PEI Core PE32\r
-\r
-Returns:\r
- Success means control is transfered and thus we should never return\r
-\r
---*/\r
-{\r
- EFI_STATUS Status;\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
- PeiStackSize = (UINTN)RShiftU64((UINT64)STACK_SIZE,1);\r
-\r
- //\r
- // |-----------| <---- TemporaryRamBase + TemporaryRamSize\r
- // | Heap |\r
- // | |\r
- // |-----------| <---- StackBase / PeiTemporaryMemoryBase\r
- // | |\r
- // | Stack |\r
- // |-----------| <---- TemporaryRamBase\r
- // \r
- TopOfStack = (VOID *)(LargestRegion + PeiStackSize);\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
-\r
- //\r
- // Bind this information into the SEC hand-off state\r
- //\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 = PcdGet32(PcdWinNtFirmwareFdSize);\r
- SecCoreData->TemporaryRamBase = (VOID*)(UINTN)LargestRegion; \r
- SecCoreData->TemporaryRamSize = STACK_SIZE;\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
- //\r
- Status = SecWinNtPeiLoadFile (\r
- PeiCorePe32File,\r
- &PeiImageAddress,\r
- &PeiCoreSize,\r
- &PeiCoreEntryPoint\r
- );\r
- if (EFI_ERROR (Status)) {\r
- return ;\r
- }\r
- \r
- //\r
- // Transfer control to the PEI Core\r
- //\r
- PeiSwitchStacks (\r
- (SWITCH_STACK_ENTRY_POINT) (UINTN) PeiCoreEntryPoint,\r
- SecCoreData,\r
- (VOID *) (UINTN) ((EFI_PEI_PPI_DESCRIPTOR *) &gPrivateDispatchTable),\r
- NULL,\r
- TopOfStack\r
- );\r
- //\r
- // If we get here, then the PEI Core returned. This is an error\r
- //\r
- return ;\r
-}\r
-\r
-EFI_STATUS\r
-EFIAPI\r
-SecWinNtPeiAutoScan (\r
- IN UINTN Index,\r
- OUT EFI_PHYSICAL_ADDRESS *MemoryBase,\r
- OUT UINT64 *MemorySize\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- This service is called from Index == 0 until it returns EFI_UNSUPPORTED.\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
- with ReadWrite and Execute attributes allocated by VirtualAlloc() API.\r
-\r
-Arguments:\r
- Index - Which memory region to use\r
- MemoryBase - Return Base address of memory region\r
- MemorySize - Return size in bytes of the memory region\r
-\r
-Returns:\r
- EFI_SUCCESS - If memory region was mapped\r
- EFI_UNSUPPORTED - If Index is not supported\r
-\r
---*/\r
-{\r
- if (Index >= gSystemMemoryCount) {\r
- 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
- return EFI_SUCCESS;\r
-}\r
-\r
-VOID *\r
-EFIAPI\r
-SecWinNtWinNtThunkAddress (\r
- VOID\r
- )\r
-/*++\r
-\r
-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 initialized in WinNtThunk.c.\r
-\r
-Arguments:\r
- InterfaceSize - sizeof (EFI_WIN_NT_THUNK_PROTOCOL);\r
- InterfaceBase - Address of the gWinNt global\r
-\r
-Returns:\r
- EFI_SUCCESS - Data returned\r
-\r
---*/\r
-{\r
- return gWinNt;\r
-}\r
-\r
-\r
-EFI_STATUS\r
-EFIAPI\r
-SecWinNtPeiLoadFile (\r
- IN VOID *Pe32Data,\r
- IN EFI_PHYSICAL_ADDRESS *ImageAddress,\r
- IN UINT64 *ImageSize,\r
- IN EFI_PHYSICAL_ADDRESS *EntryPoint\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- Loads and relocates a PE/COFF image into memory.\r
-\r
-Arguments:\r
- Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated\r
- ImageAddress - The base address of the relocated PE/COFF image\r
- ImageSize - The size of the relocated PE/COFF image\r
- EntryPoint - The entry point of the relocated PE/COFF image\r
-\r
-Returns:\r
- EFI_SUCCESS - The file was loaded and relocated\r
- EFI_OUT_OF_RESOURCES - There was not enough memory to load and relocate the PE/COFF file\r
-\r
---*/\r
-{\r
- EFI_STATUS Status;\r
- PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
-\r
- ZeroMem (&ImageContext, sizeof (ImageContext));\r
- ImageContext.Handle = Pe32Data;\r
-\r
- ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) SecImageRead;\r
-\r
- Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
- if (EFI_ERROR (Status)) {\r
- return Status;\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
- //\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
- return Status;\r
- }\r
-\r
- Status = SecNt32PeCoffRelocateImage (&ImageContext);\r
- if (EFI_ERROR (Status)) {\r
- return Status;\r
- }\r
-\r
- //\r
- // BugBug: Flush Instruction Cache Here when CPU Lib is ready\r
- //\r
-\r
- *ImageAddress = ImageContext.ImageAddress;\r
- *ImageSize = ImageContext.ImageSize;\r
- *EntryPoint = ImageContext.EntryPoint;\r
-\r
- return EFI_SUCCESS;\r
-}\r
-\r
-EFI_STATUS\r
-EFIAPI\r
-SecWinNtFdAddress (\r
- IN UINTN Index,\r
- IN OUT EFI_PHYSICAL_ADDRESS *FdBase,\r
- IN OUT UINT64 *FdSize\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- Return the FD Size and base address. Since the FD is loaded from a\r
- file into Windows memory only the SEC will know it's address.\r
-\r
-Arguments:\r
- Index - Which FD, starts at zero.\r
- FdSize - Size of the FD in bytes\r
- FdBase - Start address of the FD. Assume it points to an FV Header\r
-\r
-Returns:\r
- EFI_SUCCESS - Return the Base address and size of the FV\r
- EFI_UNSUPPORTED - Index does not map to an FD in the system\r
-\r
---*/\r
-{\r
- if (Index >= gFdInfoCount) {\r
- return EFI_UNSUPPORTED;\r
- }\r
-\r
- *FdBase = gFdInfo[Index].Address;\r
- *FdSize = gFdInfo[Index].Size;\r
-\r
- if (*FdBase == 0 && *FdSize == 0) {\r
- return EFI_UNSUPPORTED;\r
- }\r
-\r
- return EFI_SUCCESS;\r
-}\r
-\r
-EFI_STATUS\r
-EFIAPI\r
-SecImageRead (\r
- IN VOID *FileHandle,\r
- IN UINTN FileOffset,\r
- IN OUT UINTN *ReadSize,\r
- OUT VOID *Buffer\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file\r
-\r
-Arguments:\r
- FileHandle - The handle to the PE/COFF file\r
- FileOffset - The offset, in bytes, into the file to read\r
- ReadSize - The number of bytes to read from the file starting at FileOffset\r
- Buffer - A pointer to the buffer to read the data into.\r
-\r
-Returns:\r
- EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset\r
-\r
---*/\r
-{\r
- CHAR8 *Destination8;\r
- CHAR8 *Source8;\r
- UINTN Length;\r
-\r
- Destination8 = Buffer;\r
- Source8 = (CHAR8 *) ((UINTN) FileHandle + FileOffset);\r
- Length = *ReadSize;\r
- while (Length--) {\r
- *(Destination8++) = *(Source8++);\r
- }\r
-\r
- return EFI_SUCCESS;\r
-}\r
-\r
-CHAR16 *\r
-AsciiToUnicode (\r
- IN CHAR8 *Ascii,\r
- IN UINTN *StrLen OPTIONAL\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- Convert the passed in Ascii string to Unicode.\r
- Optionally return the length of the strings.\r
-\r
-Arguments:\r
- Ascii - Ascii string to convert\r
- StrLen - Length of string\r
-\r
-Returns:\r
- Pointer to malloc'ed Unicode version of Ascii\r
-\r
---*/\r
-{\r
- UINTN Index;\r
- CHAR16 *Unicode;\r
-\r
- //\r
- // Allocate a buffer for unicode string\r
- //\r
- for (Index = 0; Ascii[Index] != '\0'; Index++)\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
- }\r
-\r
- Unicode[Index] = '\0';\r
-\r
- if (StrLen != NULL) {\r
- *StrLen = Index;\r
- }\r
-\r
- return Unicode;\r
-}\r
-\r
-UINTN\r
-CountSeparatorsInString (\r
- IN CONST CHAR16 *String,\r
- IN CHAR16 Separator\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- Count the number of separators in String\r
-\r
-Arguments:\r
- String - String to process\r
- Separator - Item to count\r
-\r
-Returns:\r
- Number of Separator in String\r
-\r
---*/\r
-{\r
- UINTN Count;\r
-\r
- for (Count = 0; *String != '\0'; String++) {\r
- if (*String == Separator) {\r
- Count++;\r
- }\r
- }\r
-\r
- return Count;\r
-}\r
-\r
-\r
-EFI_STATUS\r
-SecNt32PeCoffRelocateImage (\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
-\r
-\r
- Status = PeCoffLoaderRelocateImage (ImageContext);\r
- if (EFI_ERROR (Status)) {\r
- //\r
- // We could not relocated the image in memory properly\r
- //\r
- return Status;\r
- }\r
-\r
- //\r
- // 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 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
-\r
- DllEntryPoint = NULL;\r
-\r
- //\r
- // Load the DLL if it's not an EBC image.\r
- //\r
- if ((ImageContext->PdbPointer != NULL) &&\r
- (ImageContext->Machine != EFI_IMAGE_MACHINE_EBC)) {\r
- //\r
- // Convert filename from ASCII to Unicode\r
- //\r
- DllFileName = AsciiToUnicode (ImageContext->PdbPointer, &Index);\r
-\r
- //\r
- // Check that we have a valid filename\r
- //\r
- if (Index < 5 || DllFileName[Index - 4] != '.') {\r
- free (DllFileName);\r
-\r
- //\r
- // Never return an error if PeCoffLoaderRelocateImage() succeeded.\r
- // The image will run, but we just can't source level debug. If we\r
- // return an error the image will not run.\r
- //\r
- return EFI_SUCCESS;\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
-\r
- //\r
- // Load the .DLL file into the user process's address space for source \r
- // level debug\r
- //\r
- Library = LoadLibraryEx (DllFileName, NULL, DONT_RESOLVE_DLL_REFERENCES);\r
- 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() 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 debugging\r
- //\r
- DllEntryPoint = (VOID *) (UINTN) GetProcAddress (Library, "InitializeDriver");\r
-\r
- }\r
-\r
- if ((Library != NULL) && (DllEntryPoint != NULL)) {\r
- ImageContext->EntryPoint = (EFI_PHYSICAL_ADDRESS) (UINTN) DllEntryPoint;\r
- SecPrint ("LoadLibraryEx (%S,\n NULL, DONT_RESOLVE_DLL_REFERENCES)\n", DllFileName);\r
- } else {\r
- SecPrint ("WARNING: No source level debug %S. \n", DllFileName);\r
- }\r
-\r
- free (DllFileName);\r
- }\r
-\r
- //\r
- // Never return an error if PeCoffLoaderRelocateImage() succeeded.\r
- // The image will run, but we just can't source level debug. If we\r
- // return an error the image will not run.\r
- //\r
- return EFI_SUCCESS;\r
-}\r
-\r
-\r
-\r
-\r
-VOID\r
-_ModuleEntryPoint (\r
- VOID\r
- )\r
-{\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 permanent 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
- // immediately, also we need fixup the stack change caused by new call into \r
- // permanent 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 executed 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