18#ifndef SE_TIME_ZONE_NAME
19#define SE_TIME_ZONE_NAME TEXT("SeTimeZonePrivilege")
25#define MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE 0x100
39UINTN mPdbNameModHandleArraySize = 0;
47UINTN gFdInfoCount = 0;
56UINTN gSystemMemoryCount = 0;
59BASE_LIBRARY_JUMP_BUFFER mResetJumpBuffer;
60CHAR8 *mResetTypeStr[] = {
64 "EfiResetPlatformSpecific"
91 OUT UINT64 *MemorySize
94 if (Index >= gSystemMemoryCount) {
95 return EFI_UNSUPPORTED;
98 *MemoryBase = gSystemMemory[Index].Memory;
99 *MemorySize = gSystemMemory[Index].Size;
125 IN OUT UINT64 *FdSize,
129 if (Index >= gFdInfoCount) {
130 return EFI_UNSUPPORTED;
134 *FdSize = (UINT64)gFdInfo[Index].Size;
137 if ((*FdBase == 0) && (*FdSize == 0)) {
138 return EFI_UNSUPPORTED;
147 *FixUp = *FdBase -
PcdGet64 (PcdEmuFdBaseAddress);
172 return &gEmuThunkProtocol;
189 CHAR8 Buffer[0x1000];
191 va_start (Marker, Format);
193 _vsnprintf (Buffer,
sizeof (Buffer), Format, Marker);
197 CharCount = strlen (Buffer);
199 GetStdHandle (STD_OUTPUT_HANDLE),
226 IN VOID *ResetData OPTIONAL
232 SecPrint (
" Emu ResetSystem is called: ResetType = %s\n", mResetTypeStr[ResetType]);
240 for (Index = 0; Index < mPdbNameModHandleArraySize; Index++) {
241 if (mPdbNameModHandleArray[Index].PdbPointer !=
NULL) {
242 SecPrint (
" Emu Unload DLL: %s\n", mPdbNameModHandleArray[Index].PdbPointer);
243 FreeLibrary (mPdbNameModHandleArray[Index].ModHandle);
244 HeapFree (GetProcessHeap (), 0, mPdbNameModHandleArray[Index].PdbPointer);
245 mPdbNameModHandleArray[Index].PdbPointer =
NULL;
252 LongJump (&mResetJumpBuffer, ResetType + 1);
280EfiSystemMemoryRange (
281 IN VOID *MemoryAddress
288 for (Index = 0; Index < gSystemMemoryCount; Index++) {
289 if ((MemoryBase >= gSystemMemory[Index].Memory) &&
290 (MemoryBase < (gSystemMemory[Index].Memory + gSystemMemory[Index].Size)))
301 IN CHAR16 *FileName OPTIONAL,
303 IN DWORD CreationDisposition,
304 IN OUT VOID **BaseAddress,
334 VOID *VirtualAddress;
340 NtFileHandle = INVALID_HANDLE_VALUE;
341 if (FileName !=
NULL) {
342 NtFileHandle = CreateFile (
344 GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE,
348 FILE_ATTRIBUTE_NORMAL,
351 if (NtFileHandle == INVALID_HANDLE_VALUE) {
352 return EFI_NOT_FOUND;
359 NtMapHandle = CreateFileMapping (
362 PAGE_EXECUTE_READWRITE,
367 if (NtMapHandle ==
NULL) {
368 return EFI_DEVICE_ERROR;
374 VirtualAddress = MapViewOfFileEx (
376 FILE_MAP_EXECUTE | FILE_MAP_ALL_ACCESS,
382 if (VirtualAddress ==
NULL) {
383 return EFI_DEVICE_ERROR;
390 FileSize = SetFilePointer (
396 if (FileSize == -1) {
397 return EFI_DEVICE_ERROR;
405 *BaseAddress = VirtualAddress;
436 TOKEN_PRIVILEGES TokenPrivileges;
438 UINT32 TemporaryRamSize;
447 CHAR16 *MemorySizeStr;
448 CHAR16 *FirmwareVolumesStr;
449 UINTN ProcessAffinityMask;
450 UINTN SystemAffinityMask;
458 OpenProcessToken (GetCurrentProcess (), TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &Token);
459 if (LookupPrivilegeValue (
NULL, SE_TIME_ZONE_NAME, &TokenPrivileges.Privileges[0].Luid)) {
460 TokenPrivileges.PrivilegeCount = 1;
461 TokenPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
462 AdjustTokenPrivileges (Token,
FALSE, &TokenPrivileges, 0, (PTOKEN_PRIVILEGES)
NULL, 0);
465 MemorySizeStr = (CHAR16 *)
PcdGetPtr (PcdEmuMemorySize);
466 FirmwareVolumesStr = (CHAR16 *)
PcdGetPtr (PcdEmuFirmwareVolume);
468 SecPrint (
"\n\rEDK II WIN Host Emulation Environment from http://www.tianocore.org/edk2/\n\r");
473 if (GetProcessAffinityMask (GetCurrentProcess (), &ProcessAffinityMask, &SystemAffinityMask)) {
474 LowBit = (INT32)
LowBitSet32 ((UINT32)ProcessAffinityMask);
480 SetProcessAffinityMask (GetCurrentProcess (), (
INTN)(BIT0 << LowBit));
488 SetPriorityClass (GetCurrentProcess (), IDLE_PRIORITY_CLASS);
489 SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_HIGHEST);
491 SecInitializeThunk ();
496 if (SecEmuThunkPpi ==
NULL) {
497 SecPrint (
"ERROR : Can not allocate memory for SecEmuThunkPpi. Exiting.\n");
502 SecEmuThunkPpi->Argc = Argc;
503 SecEmuThunkPpi->Argv = Argv;
504 SecEmuThunkPpi->Envp = Envp;
505 SecEmuThunkPpi->PersistentMemorySize =
FixedPcdGet32 (PcdPersistentMemorySize);
506 AddThunkPpi (EFI_PEI_PPI_DESCRIPTOR_PPI, &gEmuThunkPpiGuid, SecEmuThunkPpi);
507 AddThunkPpi (EFI_PEI_PPI_DESCRIPTOR_PPI, &gEfiPeiReset2PpiGuid, &mEmuReset2Ppi);
512 AddThunkProtocol (&mWinNtWndThunkIo, (CHAR16 *)
PcdGetPtr (PcdEmuGop),
TRUE);
513 AddThunkProtocol (&mWinNtFileSystemThunkIo, (CHAR16 *)
PcdGetPtr (PcdEmuFileSystem),
TRUE);
514 AddThunkProtocol (&mWinNtBlockIoThunkIo, (CHAR16 *)
PcdGetPtr (PcdEmuVirtualDisk),
TRUE);
515 AddThunkProtocol (&mWinNtSnpThunkIo, (CHAR16 *)
PcdGetPtr (PcdEmuNetworkInterface),
TRUE);
520 gSystemMemoryCount = CountSeparatorsInString (MemorySizeStr,
'!') + 1;
522 if (gSystemMemory ==
NULL) {
523 SecPrint (
"ERROR : Can not allocate memory for %S. Exiting.\n\r", MemorySizeStr);
530 for (Index = 0, Done =
FALSE; !Done; Index++) {
531 ASSERT (Index < gSystemMemoryCount);
532 gSystemMemory[Index].Size = ((UINT64)_wtoi (MemorySizeStr)) * ((UINT64)SIZE_1MB);
533 gSystemMemory[Index].Memory = (
EFI_PHYSICAL_ADDRESS)(
UINTN)VirtualAlloc (
NULL, (SIZE_T)(gSystemMemory[Index].Size), MEM_COMMIT, PAGE_EXECUTE_READWRITE);
534 if (gSystemMemory[Index].Memory == 0) {
535 return EFI_OUT_OF_RESOURCES;
541 for (Index1 = 0; MemorySizeStr[Index1] !=
'!' && MemorySizeStr[Index1] != 0; Index1++) {
544 if (MemorySizeStr[Index1] == 0) {
548 MemorySizeStr = MemorySizeStr + Index1 + 1;
554 gFdInfoCount = CountSeparatorsInString (FirmwareVolumesStr,
'!') + 1;
555 gFdInfo = calloc (gFdInfoCount,
sizeof (
NT_FD_INFO));
556 if (gFdInfo ==
NULL) {
557 SecPrint (
"ERROR : Can not allocate memory for %S. Exiting.\n\r", FirmwareVolumesStr);
564 SecPrint (
" BootMode 0x%02x\n\r",
PcdGet32 (PcdEmuBootMode));
571 TemporaryRamSize = TEMPORARY_RAM_SIZE;
572 TemporaryRam = VirtualAlloc (
NULL, (SIZE_T)(TemporaryRamSize), MEM_COMMIT, PAGE_EXECUTE_READWRITE);
573 if (TemporaryRam ==
NULL) {
574 SecPrint (
"ERROR : Can not allocate enough space for SecStack\n\r");
585 if (EmuMagicPage !=
NULL) {
587 Status = WinNtOpenFile (
594 if (EFI_ERROR (Status)) {
595 SecPrint (
"ERROR : Could not allocate PeiServicesTablePage @ %p\n\r", EmuMagicPage);
596 return EFI_DEVICE_ERROR;
605 if (FileNamePtr ==
NULL) {
606 SecPrint (
"ERROR : Can not allocate memory for firmware volume string\n\r");
610 for (Done =
FALSE, Index = 0, SecFile =
NULL; !Done; Index++) {
611 FileName = FileNamePtr;
612 for (Index1 = 0; (FileNamePtr[Index1] !=
'!') && (FileNamePtr[Index1] != 0); Index1++) {
615 if (FileNamePtr[Index1] == 0) {
618 FileNamePtr[Index1] =
'\0';
619 FileNamePtr = &FileNamePtr[Index1 + 1];
625 Status = WinNtOpenFile (
629 &gFdInfo[Index].Address,
632 if (EFI_ERROR (Status)) {
633 SecPrint (
"ERROR : Can not open Firmware Device File %S (0x%X). Exiting.\n\r", FileName, Status);
637 SecPrint (
" FD loaded from %S", FileName);
639 if (SecFile ==
NULL) {
646 EFI_FV_FILETYPE_SECURITY_CORE,
650 if (!EFI_ERROR (Status)) {
652 if (!EFI_ERROR (Status)) {
653 SecPrint (
" contains SEC Core");
661 ResetJumpCode =
SetJump (&mResetJumpBuffer);
667 SecPrint (
" OS Emulator clearing temp RAM and physical RAM (to be discovered later)......\n\r");
668 SetMem32 (TemporaryRam, TemporaryRamSize,
PcdGet32 (PcdInitValueInTempStack));
669 for (Index = 0; Index < gSystemMemoryCount; Index++) {
675 " OS Emulator passing in %u KB of temp RAM at 0x%08lx to SEC\n\r",
676 TemporaryRamSize / SIZE_1KB,
682 SecLoadSecCore ((
UINTN)TemporaryRam, TemporaryRamSize, gFdInfo[0].Address, gFdInfo[0].Size, SecFile);
688 SecPrint (
"ERROR : SEC returned\n\r");
696 IN VOID *BootFirmwareVolumeBase,
697 IN UINTN BootFirmwareVolumeSize,
698 IN VOID *SecCorePe32File
719 VOID *SecCoreEntryPoint;
726 SecStackSize = TemporaryRamSize >> 1;
737 TopOfStack = (VOID *)(TemporaryRam + SecStackSize);
762 Status = SecPeCoffGetEntryPoint (
766 if (EFI_ERROR (Status)) {
787SecPeCoffGetEntryPoint (
789 IN OUT VOID **EntryPoint
795 ZeroMem (&ImageContext,
sizeof (ImageContext));
796 ImageContext.
Handle = Pe32Data;
801 if (EFI_ERROR (Status)) {
811 if (EFI_ERROR (Status)) {
816 if (EFI_ERROR (Status)) {
854 Destination8 = Buffer;
855 Source8 = (CHAR8 *)((
UINTN)FileHandle + FileOffset);
858 *(Destination8++) = *(Source8++);
891 for (Index = 0; Ascii[Index] !=
'\0'; Index++) {
894 Unicode = malloc ((Index + 1) *
sizeof (CHAR16));
895 if (Unicode ==
NULL) {
899 for (Index = 0; Ascii[Index] !=
'\0'; Index++) {
900 Unicode[Index] = (CHAR16)Ascii[Index];
903 Unicode[Index] =
'\0';
913CountSeparatorsInString (
934 for (Count = 0; *String !=
'\0'; String++) {
935 if (*String == Separator) {
969 Array = mPdbNameModHandleArray;
970 for (Index = 0; Index < mPdbNameModHandleArraySize; Index++, Array++) {
971 if ((Array->PdbPointer !=
NULL) && (Array->ModHandle == ModHandle)) {
972 return EFI_ALREADY_STARTED;
976 Array = mPdbNameModHandleArray;
977 for (Index = 0; Index < mPdbNameModHandleArraySize; Index++, Array++) {
978 if (Array->PdbPointer ==
NULL) {
982 Handle = GetProcessHeap ();
984 Array->PdbPointer = HeapAlloc (Handle, HEAP_ZERO_MEMORY, Size);
985 ASSERT (Array->PdbPointer !=
NULL);
987 AsciiStrCpyS (Array->PdbPointer, Size, ImageContext->PdbPointer);
988 Array->ModHandle = ModHandle;
998 mPdbNameModHandleArraySize += MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE;
1002 TempArray = HeapAlloc (
1008 CopyMem ((VOID *)(
UINTN)TempArray, (VOID *)(
UINTN)mPdbNameModHandleArray, PreviousSize);
1010 HeapFree (GetProcessHeap (), 0, mPdbNameModHandleArray);
1012 mPdbNameModHandleArray = TempArray;
1013 if (mPdbNameModHandleArray ==
NULL) {
1015 return EFI_OUT_OF_RESOURCES;
1037 if (ImageContext->PdbPointer ==
NULL) {
1044 Array = mPdbNameModHandleArray;
1045 for (Index = 0; Index < mPdbNameModHandleArraySize; Index++, Array++) {
1046 if ((Array->PdbPointer !=
NULL) && (
AsciiStrCmp (Array->PdbPointer, ImageContext->PdbPointer) == 0)) {
1050 HeapFree (GetProcessHeap (), 0, Array->PdbPointer);
1051 Array->PdbPointer =
NULL;
1052 return Array->ModHandle;
1066 VOID *DllEntryPoint;
1067 CHAR16 *DllFileName;
1071 ASSERT (ImageContext !=
NULL);
1081 DllEntryPoint =
NULL;
1086 if ((ImageContext->PdbPointer !=
NULL) &&
1092 DllFileName = AsciiToUnicode (ImageContext->PdbPointer, &Index);
1097 if ((Index < 5) || (DllFileName[Index - 4] !=
'.')) {
1111 DllFileName[Index - 3] =
'D';
1112 DllFileName[Index - 2] =
'L';
1113 DllFileName[Index - 1] =
'L';
1119 Library = LoadLibraryEx (DllFileName,
NULL, DONT_RESOLVE_DLL_REFERENCES);
1120 if (Library !=
NULL) {
1129 DllEntryPoint = (VOID *)(
UINTN)GetProcAddress (Library,
"InitializeDriver");
1132 if ((Library !=
NULL) && (DllEntryPoint !=
NULL)) {
1134 if ((Status ==
EFI_SUCCESS) || (Status == EFI_ALREADY_STARTED)) {
1139 SecPrint (
"LoadLibraryEx (\n\r %S,\n\r NULL, DONT_RESOLVE_DLL_REFERENCES)\n\r", DllFileName);
1142 SecPrint (
"WARNING: No source level debug %S. \n\r", DllFileName);
1157 ASSERT (ImageContext !=
NULL);
1160 if (ModHandle !=
NULL) {
1161 FreeLibrary (ModHandle);
1162 SecPrint (
"FreeLibrary (\n\r %s)\n\r", ImageContext->PdbPointer);
1164 SecPrint (
"WARNING: Unload image without source level debug\n\r");
#define CPU_STACK_ALIGNMENT
UINTN EFIAPI StrSize(IN CONST CHAR16 *String)
INTN EFIAPI LowBitSet32(IN UINT32 Operand)
RETURNS_TWICE UINTN EFIAPI SetJump(OUT BASE_LIBRARY_JUMP_BUFFER *JumpBuffer)
VOID EFIAPI SwitchStack(IN SWITCH_STACK_ENTRY_POINT EntryPoint, IN VOID *Context1 OPTIONAL, IN VOID *Context2 OPTIONAL, IN VOID *NewStack,...)
UINTN EFIAPI AsciiStrLen(IN CONST CHAR8 *String)
INTN EFIAPI AsciiStrCmp(IN CONST CHAR8 *FirstString, IN CONST CHAR8 *SecondString)
VOID(EFIAPI * SWITCH_STACK_ENTRY_POINT)(IN VOID *Context1 OPTIONAL, IN VOID *Context2 OPTIONAL)
VOID EFIAPI LongJump(IN BASE_LIBRARY_JUMP_BUFFER *JumpBuffer, IN UINTN Value)
UINTN EFIAPI StrLen(IN CONST CHAR16 *String)
RETURN_STATUS EFIAPI AsciiStrCpyS(OUT CHAR8 *Destination, IN UINTN DestMax, IN CONST CHAR8 *Source)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI SetMem32(OUT VOID *Buffer, IN UINTN Length, IN UINT32 Value)
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
int main()
=== TEST ENGINE ================================================================================
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID *EFIAPI AllocateCopyPool(IN UINTN AllocationSize, IN CONST VOID *Buffer)
EFI_STATUS EFIAPI PeiServicesFfsFindSectionData(IN EFI_SECTION_TYPE SectionType, IN EFI_PEI_FILE_HANDLE FileHandle, OUT VOID **SectionData)
EFI_STATUS EFIAPI PeiServicesFfsFindNextFile(IN EFI_FV_FILETYPE SearchType, IN EFI_PEI_FV_HANDLE VolumeHandle, IN OUT EFI_PEI_FILE_HANDLE *FileHandle)
#define ALIGN_POINTER(Pointer, Alignment)
#define PcdGet64(TokenName)
#define FixedPcdGet32(TokenName)
#define FixedPcdGet64(TokenName)
#define PcdGet32(TokenName)
#define PcdGetPtr(TokenName)
RETURN_STATUS EFIAPI PeCoffLoaderLoadImage(IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext)
RETURN_STATUS EFIAPI PeCoffLoaderRelocateImage(IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext)
RETURN_STATUS(EFIAPI * PE_COFF_LOADER_READ_FILE)(IN VOID *FileHandle, IN UINTN FileOffset, IN OUT UINTN *ReadSize, OUT VOID *Buffer)
RETURN_STATUS EFIAPI PeCoffLoaderGetImageInfo(IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext)
VOID * EFI_PEI_FILE_HANDLE
struct _EFI_SEC_PEI_HAND_OFF EFI_SEC_PEI_HAND_OFF
UINT64 EFI_PHYSICAL_ADDRESS
#define EFI_IMAGE_MACHINE_EBC
@ EfiResetPlatformSpecific
VOID EFIAPI WinReset(IN EFI_RESET_TYPE ResetType, IN EFI_STATUS ResetStatus, IN UINTN DataSize, IN VOID *ResetData OPTIONAL)
VOID EFIAPI PeCoffLoaderRelocateImageExtraAction(IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext)
EFI_STATUS AddModHandle(IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext, IN VOID *ModHandle)
VOID * RemoveModHandle(IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext)
UINTN PeiTemporaryRamSize
VOID * BootFirmwareVolumeBase
UINTN BootFirmwareVolumeSize
VOID * PeiTemporaryRamBase
PE_COFF_LOADER_READ_FILE ImageRead
PHYSICAL_ADDRESS EntryPoint
PHYSICAL_ADDRESS ImageAddress