Use VirtualAlloc() to allocate enough memory space for Nt32 emulator in place of original mapped file to memory space.

git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@4068 6f19259b-4bc3-4df7-8a09-765794883524
This commit is contained in:
lgao4 2007-10-10 02:13:43 +00:00
parent 166152e85c
commit 952261d5ef
3 changed files with 36 additions and 52 deletions

View File

@ -17,18 +17,14 @@ Abstract:
WinNt emulator of SEC phase. It's really a Win32 application, but this is WinNt emulator of SEC phase. It's really a Win32 application, but this is
Ok since all the other modules for NT32 are NOT Win32 applications. Ok since all the other modules for NT32 are NOT Win32 applications.
This program processes Windows environment variables and figures out This program gets NT32 PCD setting and figures out what the memory layout
what the memory layout will be, how may FD's will be loaded and also will be, how may FD's will be loaded and also what the boot mode is.
what the boot mode is.
The SEC registers a set of services with the SEC core. gPrivateDispatchTable The SEC registers a set of services with the SEC core. gPrivateDispatchTable
is a list of PPI's produced by the SEC that are availble for usage in PEI. is a list of PPI's produced by the SEC that are availble for usage in PEI.
This code produces 128 K of temporary memory for the PEI stack by opening a This code produces 128 K of temporary memory for the PEI stack by directly
Windows file and mapping it directly to memory addresses. allocate memory space with ReadWrite and Execute attribute.
The system.cmd script is used to set windows environment variables that drive
the configuration opitons of the SEC.
--*/ --*/
@ -99,19 +95,18 @@ EFI_PEI_PPI_DESCRIPTOR gPrivateDispatchTable[] = {
// //
// Default information about where the FD is located. // Default information about where the FD is located.
// This array gets filled in with information from EFI_FIRMWARE_VOLUMES // This array gets filled in with information from PcdWinNtFirmwareVolume
// EFI_FIRMWARE_VOLUMES is a Windows environment variable set by system.cmd.
// The number of array elements is allocated base on parsing // The number of array elements is allocated base on parsing
// EFI_FIRMWARE_VOLUMES and the memory is never freed. // PcdWinNtFirmwareVolume and the memory is never freed.
// //
UINTN gFdInfoCount = 0; UINTN gFdInfoCount = 0;
NT_FD_INFO *gFdInfo; NT_FD_INFO *gFdInfo;
// //
// Array that supports seperate memory rantes. // Array that supports seperate memory rantes.
// The memory ranges are set in system.cmd via the EFI_MEMORY_SIZE variable. // The memory ranges are set by PcdWinNtMemorySizeForSecMain.
// The number of array elements is allocated base on parsing // The number of array elements is allocated base on parsing
// EFI_MEMORY_SIZE and the memory is never freed. // PcdWinNtMemorySizeForSecMain value and the memory is never freed.
// //
UINTN gSystemMemoryCount = 0; UINTN gSystemMemoryCount = 0;
NT_SYSTEM_MEMORY *gSystemMemory; NT_SYSTEM_MEMORY *gSystemMemory;
@ -160,8 +155,8 @@ Returns:
CHAR16 *MemorySizeStr; CHAR16 *MemorySizeStr;
CHAR16 *FirmwareVolumesStr; CHAR16 *FirmwareVolumesStr;
MemorySizeStr = (CHAR16 *)L"64!64"; MemorySizeStr = (CHAR16 *) FixedPcdGetPtr (PcdWinNtMemorySizeForSecMain);
FirmwareVolumesStr = (CHAR16 *)L"..\\Fv\\Fv_Recovery.fd"; FirmwareVolumesStr = (CHAR16 *) FixedPcdGetPtr (PcdWinNtFirmwareVolume);
printf ("\nEDK SEC Main NT Emulation Environment from www.TianoCore.org\n"); printf ("\nEDK SEC Main NT Emulation Environment from www.TianoCore.org\n");
@ -196,21 +191,14 @@ Returns:
printf (" BootMode 0x%02x\n", FixedPcdGet32 (PcdWinNtBootMode)); printf (" BootMode 0x%02x\n", FixedPcdGet32 (PcdWinNtBootMode));
// //
// Open up a 128K file to emulate temp memory for PEI. // Allocate 128K memory to emulate temp memory for PEI.
// on a real platform this would be SRAM, or using the cache as RAM. // on a real platform this would be SRAM, or using the cache as RAM.
// Set InitialStackMemory to zero so WinNtOpenFile will allocate a new mapping // Set InitialStackMemory to zero so WinNtOpenFile will allocate a new mapping
// //
InitialStackMemory = 0; InitialStackMemorySize = STACK_SIZE;
InitialStackMemorySize = 0x20000; InitialStackMemory = (EFI_PHYSICAL_ADDRESS) (UINTN) VirtualAlloc (NULL, (SIZE_T) (InitialStackMemorySize), MEM_COMMIT, PAGE_EXECUTE_READWRITE);
Status = WinNtOpenFile ( if (InitialStackMemory == 0) {
L"SecStack", printf ("ERROR : Can not allocate enough space for SecStack\n");
(UINT32) InitialStackMemorySize,
OPEN_ALWAYS,
&InitialStackMemory,
&InitialStackMemorySize
);
if (EFI_ERROR (Status)) {
printf ("ERROR : Can not open SecStack Exiting\n");
exit (1); exit (1);
} }
@ -286,7 +274,6 @@ Returns:
// Save the size of the memory and make a Unicode filename SystemMemory00, ... // Save the size of the memory and make a Unicode filename SystemMemory00, ...
// //
gSystemMemory[Index].Size = _wtoi (MemorySizeStr) * 0x100000; gSystemMemory[Index].Size = _wtoi (MemorySizeStr) * 0x100000;
_snwprintf (gSystemMemory[Index].FileName, NT_SYSTEM_MEMORY_FILENAME_SIZE, L"SystemMemory%02d", Index);
// //
// Find the next region // Find the next region
@ -646,9 +633,9 @@ Routine Description:
This service is called from Index == 0 until it returns EFI_UNSUPPORTED. This service is called from Index == 0 until it returns EFI_UNSUPPORTED.
It allows discontiguous memory regions to be supported by the emulator. It allows discontiguous memory regions to be supported by the emulator.
It uses gSystemMemory[] and gSystemMemoryCount that were created by It uses gSystemMemory[] and gSystemMemoryCount that were created by
parsing the Windows environment variable EFI_MEMORY_SIZE. parsing PcdWinNtMemorySizeForSecMain value.
The size comes from the varaible and the address comes from the call to The size comes from the Pcd value and the address comes from the memory space
WinNtOpenFile. with ReadWrite and Execute attributes allocated by VirtualAlloc() API.
Arguments: Arguments:
Index - Which memory region to use Index - Which memory region to use
@ -661,24 +648,22 @@ Returns:
--*/ --*/
{ {
EFI_STATUS Status;
if (Index >= gSystemMemoryCount) { if (Index >= gSystemMemoryCount) {
return EFI_UNSUPPORTED; return EFI_UNSUPPORTED;
} }
*MemoryBase = 0; //
Status = WinNtOpenFile ( // Allocate enough memory space for emulator
gSystemMemory[Index].FileName, //
(UINT32) gSystemMemory[Index].Size, gSystemMemory[Index].Memory = (EFI_PHYSICAL_ADDRESS) (UINTN) VirtualAlloc (NULL, (SIZE_T) (gSystemMemory[Index].Size), MEM_COMMIT, PAGE_EXECUTE_READWRITE);
OPEN_ALWAYS, if (gSystemMemory[Index].Memory == 0) {
MemoryBase, return EFI_OUT_OF_RESOURCES;
MemorySize }
);
gSystemMemory[Index].Memory = *MemoryBase; *MemoryBase = gSystemMemory[Index].Memory;
*MemorySize = gSystemMemory[Index].Size;
return Status; return EFI_SUCCESS;
} }
VOID * VOID *
@ -744,9 +729,10 @@ Returns:
return Status; return Status;
} }
// //
// Allocate space in NT (not emulator) memory. Extra space is for alignment // Allocate space in NT (not emulator) memory with ReadWrite and Execute attribue.
// Extra space is for alignment
// //
ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) malloc ((UINTN) (ImageContext.ImageSize + (ImageContext.SectionAlignment * 2))); ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) VirtualAlloc (NULL, (SIZE_T) (ImageContext.ImageSize + (ImageContext.SectionAlignment * 2)), MEM_COMMIT, PAGE_EXECUTE_READWRITE);
if (ImageContext.ImageAddress == 0) { if (ImageContext.ImageAddress == 0) {
return EFI_OUT_OF_RESOURCES; return EFI_OUT_OF_RESOURCES;
} }

View File

@ -47,15 +47,11 @@ typedef struct {
UINT64 Size; UINT64 Size;
} NT_FD_INFO; } NT_FD_INFO;
#define NT_SYSTEM_MEMORY_FILENAME_SIZE 40
typedef struct { typedef struct {
CHAR16 FileName[NT_SYSTEM_MEMORY_FILENAME_SIZE];
EFI_PHYSICAL_ADDRESS Memory; EFI_PHYSICAL_ADDRESS Memory;
UINT64 Size; UINT64 Size;
} NT_SYSTEM_MEMORY; } NT_SYSTEM_MEMORY;
#define MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE 0x100 #define MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE 0x100
typedef struct { typedef struct {

View File

@ -63,6 +63,8 @@
[FixedPcd.common] [FixedPcd.common]
gEfiNt32PkgTokenSpaceGuid.PcdWinNtBootMode gEfiNt32PkgTokenSpaceGuid.PcdWinNtBootMode
gEfiNt32PkgTokenSpaceGuid.PcdWinNtFirmwareFdSize gEfiNt32PkgTokenSpaceGuid.PcdWinNtFirmwareFdSize
gEfiNt32PkgTokenSpaceGuid.PcdWinNtMemorySizeForSecMain
gEfiNt32PkgTokenSpaceGuid.PcdWinNtFirmwareVolume
[BuildOptions.common] [BuildOptions.common]
MSFT:DEBUG_*_IA32_DLINK_FLAGS = /out:"$(BIN_DIR)\SecMain.exe" /base:0x10000000 /pdb:"$(BIN_DIR)\SecMain.pdb" /LIBPATH:"$(VCINSTALLDIR)\Lib" /LIBPATH:"$(VCINSTALLDIR)\PlatformSdk\Lib" /NOLOGO /SUBSYSTEM:CONSOLE /NODEFAULTLIB /IGNORE:4086 /MAP /OPT:REF /DEBUG /MACHINE:I386 /LTCG Kernel32.lib MSVCRTD.lib Gdi32.lib User32.lib Winmm.lib MSFT:DEBUG_*_IA32_DLINK_FLAGS = /out:"$(BIN_DIR)\SecMain.exe" /base:0x10000000 /pdb:"$(BIN_DIR)\SecMain.pdb" /LIBPATH:"$(VCINSTALLDIR)\Lib" /LIBPATH:"$(VCINSTALLDIR)\PlatformSdk\Lib" /NOLOGO /SUBSYSTEM:CONSOLE /NODEFAULTLIB /IGNORE:4086 /MAP /OPT:REF /DEBUG /MACHINE:I386 /LTCG Kernel32.lib MSVCRTD.lib Gdi32.lib User32.lib Winmm.lib