2 WinNt emulator of pre-SEC phase. It's really a Win32 application, but this is
3 Ok since all the other modules for NT32 are NOT Win32 applications.
5 This program gets NT32 PCD setting and figures out what the memory layout
6 will be, how may FD's will be loaded and also what the boot mode is.
8 This code produces 128 K of temporary memory for the SEC stack by directly
9 allocate memory space with ReadWrite and Execute attribute.
11 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
12 (C) Copyright 2016-2020 Hewlett Packard Enterprise Development LP<BR>
13 SPDX-License-Identifier: BSD-2-Clause-Patent
18 #ifndef SE_TIME_ZONE_NAME
19 #define SE_TIME_ZONE_NAME TEXT("SeTimeZonePrivilege")
23 // The growth size for array of module handle entries
25 #define MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE 0x100
28 // Module handle entry structure
33 } PDB_NAME_TO_MOD_HANDLE
;
36 // An Array to hold the module handles
38 PDB_NAME_TO_MOD_HANDLE
*mPdbNameModHandleArray
= NULL
;
39 UINTN mPdbNameModHandleArraySize
= 0;
42 // Default information about where the FD is located.
43 // This array gets filled in with information from PcdWinNtFirmwareVolume
44 // The number of array elements is allocated base on parsing
45 // PcdWinNtFirmwareVolume and the memory is never freed.
47 UINTN gFdInfoCount
= 0;
51 // Array that supports separate memory ranges.
52 // The memory ranges are set by PcdWinNtMemorySizeForSecMain.
53 // The number of array elements is allocated base on parsing
54 // PcdWinNtMemorySizeForSecMain value and the memory is never freed.
56 UINTN gSystemMemoryCount
= 0;
57 NT_SYSTEM_MEMORY
*gSystemMemory
;
62 This service is called from Index == 0 until it returns EFI_UNSUPPORTED.
63 It allows discontinuous memory regions to be supported by the emulator.
64 It uses gSystemMemory[] and gSystemMemoryCount that were created by
65 parsing the host environment variable EFI_MEMORY_SIZE.
66 The size comes from the varaible and the address comes from the call to
70 Index - Which memory region to use
71 MemoryBase - Return Base address of memory region
72 MemorySize - Return size in bytes of the memory region
75 EFI_SUCCESS - If memory region was mapped
76 EFI_UNSUPPORTED - If Index is not supported
82 OUT EFI_PHYSICAL_ADDRESS
*MemoryBase
,
83 OUT UINT64
*MemorySize
86 if (Index
>= gSystemMemoryCount
) {
87 return EFI_UNSUPPORTED
;
91 // Allocate enough memory space for emulator
93 gSystemMemory
[Index
].Memory
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VirtualAlloc (NULL
, (SIZE_T
) (gSystemMemory
[Index
].Size
), MEM_COMMIT
, PAGE_EXECUTE_READWRITE
);
94 if (gSystemMemory
[Index
].Memory
== 0) {
95 return EFI_OUT_OF_RESOURCES
;
98 *MemoryBase
= gSystemMemory
[Index
].Memory
;
99 *MemorySize
= gSystemMemory
[Index
].Size
;
107 Return the FD Size and base address. Since the FD is loaded from a
108 file into host memory only the SEC will know its address.
111 Index - Which FD, starts at zero.
112 FdSize - Size of the FD in bytes
113 FdBase - Start address of the FD. Assume it points to an FV Header
114 FixUp - Difference between actual FD address and build address
117 EFI_SUCCESS - Return the Base address and size of the FV
118 EFI_UNSUPPORTED - Index does nto map to an FD in the system
124 IN OUT EFI_PHYSICAL_ADDRESS
*FdBase
,
125 IN OUT UINT64
*FdSize
,
126 IN OUT EFI_PHYSICAL_ADDRESS
*FixUp
129 if (Index
>= gFdInfoCount
) {
130 return EFI_UNSUPPORTED
;
134 *FdBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)gFdInfo
[Index
].Address
;
135 *FdSize
= (UINT64
)gFdInfo
[Index
].Size
;
138 if (*FdBase
== 0 && *FdSize
== 0) {
139 return EFI_UNSUPPORTED
;
144 // FD 0 has XIP code and well known PCD values
145 // If the memory buffer could not be allocated at the FD build address
146 // the Fixup is the difference.
148 *FixUp
= *FdBase
- PcdGet64 (PcdEmuFdBaseAddress
);
157 Since the SEC is the only Unix program in stack it must export
158 an interface to do POSIX calls. gUnix is initialized in UnixThunk.c.
161 InterfaceSize - sizeof (EFI_WIN_NT_THUNK_PROTOCOL);
162 InterfaceBase - Address of the gUnix global
165 EFI_SUCCESS - Data returned
173 return &gEmuThunkProtocol
;
177 EMU_THUNK_PPI mSecEmuThunkPpi
= {
191 CHAR8 Buffer
[0x1000];
193 va_start (Marker
, Format
);
195 _vsnprintf (Buffer
, sizeof (Buffer
), Format
, Marker
);
199 CharCount
= strlen (Buffer
);
201 GetStdHandle (STD_OUTPUT_HANDLE
),
212 Check to see if an address range is in the EFI GCD memory map.
214 This is all of GCD for system memory passed to DXE Core. FV
215 mapping and other device mapped into system memory are not
216 inlcuded in the check.
219 Index - Which memory region to use
220 MemoryBase - Return Base address of memory region
221 MemorySize - Return size in bytes of the memory region
224 TRUE - Address is in the EFI GCD memory map
225 FALSE - Address is NOT in memory map
229 EfiSystemMemoryRange (
230 IN VOID
*MemoryAddress
234 EFI_PHYSICAL_ADDRESS MemoryBase
;
236 MemoryBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)MemoryAddress
;
237 for (Index
= 0; Index
< gSystemMemoryCount
; Index
++) {
238 if ((MemoryBase
>= gSystemMemory
[Index
].Memory
) &&
239 (MemoryBase
< (gSystemMemory
[Index
].Memory
+ gSystemMemory
[Index
].Size
)) ) {
250 IN CHAR16
*FileName
, OPTIONAL
252 IN DWORD CreationDisposition
,
253 IN OUT VOID
**BaseAddress
,
259 Opens and memory maps a file using WinNt services. If *BaseAddress is non zero
260 the process will try and allocate the memory starting at BaseAddress.
263 FileName - The name of the file to open and map
264 MapSize - The amount of the file to map in bytes
265 CreationDisposition - The flags to pass to CreateFile(). Use to create new files for
266 memory emulation, and exiting files for firmware volume emulation
267 BaseAddress - The base address of the mapped file in the user address space.
268 If *BaseAddress is 0, the new memory region is used.
269 If *BaseAddress is not 0, the request memory region is used for
270 the mapping of the file into the process space.
271 Length - The size of the mapped region in bytes
274 EFI_SUCCESS - The file was opened and mapped.
275 EFI_NOT_FOUND - FileName was not found in the current directory
276 EFI_DEVICE_ERROR - An error occurred attempting to map the opened file
282 VOID
*VirtualAddress
;
286 // Use Win API to open/create a file
288 NtFileHandle
= INVALID_HANDLE_VALUE
;
289 if (FileName
!= NULL
) {
290 NtFileHandle
= CreateFile (
292 GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
,
296 FILE_ATTRIBUTE_NORMAL
,
299 if (NtFileHandle
== INVALID_HANDLE_VALUE
) {
300 return EFI_NOT_FOUND
;
304 // Map the open file into a memory range
306 NtMapHandle
= CreateFileMapping (
309 PAGE_EXECUTE_READWRITE
,
314 if (NtMapHandle
== NULL
) {
315 return EFI_DEVICE_ERROR
;
318 // Get the virtual address (address in the emulator) of the mapped file
320 VirtualAddress
= MapViewOfFileEx (
322 FILE_MAP_EXECUTE
| FILE_MAP_ALL_ACCESS
,
328 if (VirtualAddress
== NULL
) {
329 return EFI_DEVICE_ERROR
;
334 // Seek to the end of the file to figure out the true file size.
336 FileSize
= SetFilePointer (
342 if (FileSize
== -1) {
343 return EFI_DEVICE_ERROR
;
351 *BaseAddress
= VirtualAddress
;
366 Main entry point to SEC for WinNt. This is a Windows program
369 Argc - Number of command line arguments
370 Argv - Array of command line argument strings
371 Envp - Array of environment variable strings
381 TOKEN_PRIVILEGES TokenPrivileges
;
383 UINT32 TemporaryRamSize
;
390 EFI_PEI_FILE_HANDLE FileHandle
;
392 CHAR16
*MemorySizeStr
;
393 CHAR16
*FirmwareVolumesStr
;
394 UINTN ProcessAffinityMask
;
395 UINTN SystemAffinityMask
;
399 // Enable the privilege so that RTC driver can successfully run SetTime()
401 OpenProcessToken (GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
|TOKEN_QUERY
, &Token
);
402 if (LookupPrivilegeValue(NULL
, SE_TIME_ZONE_NAME
, &TokenPrivileges
.Privileges
[0].Luid
)) {
403 TokenPrivileges
.PrivilegeCount
= 1;
404 TokenPrivileges
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
405 AdjustTokenPrivileges(Token
, FALSE
, &TokenPrivileges
, 0, (PTOKEN_PRIVILEGES
) NULL
, 0);
408 MemorySizeStr
= (CHAR16
*) PcdGetPtr (PcdEmuMemorySize
);
409 FirmwareVolumesStr
= (CHAR16
*) PcdGetPtr (PcdEmuFirmwareVolume
);
411 SecPrint ("\n\rEDK II WIN Host Emulation Environment from http://www.tianocore.org/edk2/\n\r");
414 // Determine the first thread available to this process.
416 if (GetProcessAffinityMask (GetCurrentProcess (), &ProcessAffinityMask
, &SystemAffinityMask
)) {
417 LowBit
= (INT32
)LowBitSet32 ((UINT32
)ProcessAffinityMask
);
420 // Force the system to bind the process to a single thread to work
421 // around odd semaphore type crashes.
423 SetProcessAffinityMask (GetCurrentProcess (), (INTN
)(BIT0
<< LowBit
));
428 // Make some Windows calls to Set the process to the highest priority in the
429 // idle class. We need this to have good performance.
431 SetPriorityClass (GetCurrentProcess (), IDLE_PRIORITY_CLASS
);
432 SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_HIGHEST
);
434 SecInitializeThunk ();
436 // PPIs pased into PEI_CORE
438 AddThunkPpi (EFI_PEI_PPI_DESCRIPTOR_PPI
, &gEmuThunkPpiGuid
, &mSecEmuThunkPpi
);
441 // Emulator Bus Driver Thunks
443 AddThunkProtocol (&mWinNtWndThunkIo
, (CHAR16
*)PcdGetPtr (PcdEmuGop
), TRUE
);
444 AddThunkProtocol (&mWinNtFileSystemThunkIo
, (CHAR16
*)PcdGetPtr (PcdEmuFileSystem
), TRUE
);
445 AddThunkProtocol (&mWinNtBlockIoThunkIo
, (CHAR16
*)PcdGetPtr (PcdEmuVirtualDisk
), TRUE
);
446 AddThunkProtocol (&mWinNtSnpThunkIo
, (CHAR16
*)PcdGetPtr (PcdEmuNetworkInterface
), TRUE
);
449 // Allocate space for gSystemMemory Array
451 gSystemMemoryCount
= CountSeparatorsInString (MemorySizeStr
, '!') + 1;
452 gSystemMemory
= calloc (gSystemMemoryCount
, sizeof (NT_SYSTEM_MEMORY
));
453 if (gSystemMemory
== NULL
) {
454 SecPrint ("ERROR : Can not allocate memory for %S. Exiting.\n\r", MemorySizeStr
);
459 // Allocate space for gSystemMemory Array
461 gFdInfoCount
= CountSeparatorsInString (FirmwareVolumesStr
, '!') + 1;
462 gFdInfo
= calloc (gFdInfoCount
, sizeof (NT_FD_INFO
));
463 if (gFdInfo
== NULL
) {
464 SecPrint ("ERROR : Can not allocate memory for %S. Exiting.\n\r", FirmwareVolumesStr
);
470 SecPrint (" BootMode 0x%02x\n\r", PcdGet32 (PcdEmuBootMode
));
473 // Allocate 128K memory to emulate temp memory for PEI.
474 // on a real platform this would be SRAM, or using the cache as RAM.
475 // Set TemporaryRam to zero so WinNtOpenFile will allocate a new mapping
477 TemporaryRamSize
= TEMPORARY_RAM_SIZE
;
478 TemporaryRam
= VirtualAlloc (NULL
, (SIZE_T
) (TemporaryRamSize
), MEM_COMMIT
, PAGE_EXECUTE_READWRITE
);
479 if (TemporaryRam
== NULL
) {
480 SecPrint ("ERROR : Can not allocate enough space for SecStack\n\r");
483 SetMem32 (TemporaryRam
, TemporaryRamSize
, PcdGet32 (PcdInitValueInTempStack
));
485 SecPrint (" OS Emulator passing in %u KB of temp RAM at 0x%08lx to SEC\n\r",
486 TemporaryRamSize
/ SIZE_1KB
,
491 // If enabled use the magic page to communicate between modules
492 // This replaces the PI PeiServicesTable pointer mechanism that
493 // deos not work in the emulator. It also allows the removal of
494 // writable globals from SEC, PEI_CORE (libraries), PEIMs
496 EmuMagicPage
= (VOID
*)(UINTN
)(FixedPcdGet64 (PcdPeiServicesTablePage
) & MAX_UINTN
);
497 if (EmuMagicPage
!= NULL
) {
499 Status
= WinNtOpenFile (
506 if (EFI_ERROR (Status
)) {
507 SecPrint ("ERROR : Could not allocate PeiServicesTablePage @ %p\n\r", EmuMagicPage
);
508 return EFI_DEVICE_ERROR
;
513 // Open All the firmware volumes and remember the info in the gFdInfo global
514 // Meanwhile, find the SEC Core.
516 FileNamePtr
= AllocateCopyPool (StrSize (FirmwareVolumesStr
), FirmwareVolumesStr
);
517 if (FileNamePtr
== NULL
) {
518 SecPrint ("ERROR : Can not allocate memory for firmware volume string\n\r");
522 for (Done
= FALSE
, Index
= 0, SecFile
= NULL
; !Done
; Index
++) {
523 FileName
= FileNamePtr
;
524 for (Index1
= 0; (FileNamePtr
[Index1
] != '!') && (FileNamePtr
[Index1
] != 0); Index1
++)
526 if (FileNamePtr
[Index1
] == 0) {
529 FileNamePtr
[Index1
] = '\0';
530 FileNamePtr
= &FileNamePtr
[Index1
+ 1];
534 // Open the FD and remember where it got mapped into our processes address space
536 Status
= WinNtOpenFile (
540 &gFdInfo
[Index
].Address
,
543 if (EFI_ERROR (Status
)) {
544 SecPrint ("ERROR : Can not open Firmware Device File %S (0x%X). Exiting.\n\r", FileName
, Status
);
548 SecPrint (" FD loaded from %S", FileName
);
550 if (SecFile
== NULL
) {
552 // Assume the beginning of the FD is an FV and look for the SEC Core.
553 // Load the first one we find.
556 Status
= PeiServicesFfsFindNextFile (
557 EFI_FV_FILETYPE_SECURITY_CORE
,
558 (EFI_PEI_FV_HANDLE
)gFdInfo
[Index
].Address
,
561 if (!EFI_ERROR (Status
)) {
562 Status
= PeiServicesFfsFindSectionData (EFI_SECTION_PE32
, FileHandle
, &SecFile
);
563 if (!EFI_ERROR (Status
)) {
564 SecPrint (" contains SEC Core");
572 // Calculate memory regions and store the information in the gSystemMemory
573 // global for later use. The autosizing code will use this data to
574 // map this memory into the SEC process memory space.
576 for (Index
= 0, Done
= FALSE
; !Done
; Index
++) {
578 // Save the size of the memory and make a Unicode filename SystemMemory00, ...
580 gSystemMemory
[Index
].Size
= _wtoi (MemorySizeStr
) * SIZE_1MB
;
583 // Find the next region
585 for (Index1
= 0; MemorySizeStr
[Index1
] != '!' && MemorySizeStr
[Index1
] != 0; Index1
++)
587 if (MemorySizeStr
[Index1
] == 0) {
591 MemorySizeStr
= MemorySizeStr
+ Index1
+ 1;
597 // Hand off to SEC Core
599 SecLoadSecCore ((UINTN
)TemporaryRam
, TemporaryRamSize
, gFdInfo
[0].Address
, gFdInfo
[0].Size
, SecFile
);
602 // If we get here, then the SEC Core returned. This is an error as SEC should
603 // always hand off to PEI Core and then on to DXE Core.
605 SecPrint ("ERROR : SEC returned\n\r");
611 IN UINTN TemporaryRam
,
612 IN UINTN TemporaryRamSize
,
613 IN VOID
*BootFirmwareVolumeBase
,
614 IN UINTN BootFirmwareVolumeSize
,
615 IN VOID
*SecCorePe32File
620 This is the service to load the SEC Core from the Firmware Volume
623 TemporaryRam - Memory to use for SEC.
624 TemporaryRamSize - Size of Memory to use for SEC
625 BootFirmwareVolumeBase - Start of the Boot FV
626 SecCorePe32File - SEC Core PE32
629 Success means control is transferred and thus we should never return
635 VOID
*SecCoreEntryPoint
;
636 EFI_SEC_PEI_HAND_OFF
*SecCoreData
;
640 // Compute Top Of Memory for Stack and PEI Core Allocations
642 SecStackSize
= TemporaryRamSize
>> 1;
645 // |-----------| <---- TemporaryRamBase + TemporaryRamSize
648 // |-----------| <---- StackBase / PeiTemporaryMemoryBase
651 // |-----------| <---- TemporaryRamBase
653 TopOfStack
= (VOID
*)(TemporaryRam
+ SecStackSize
);
656 // Reservet space for storing PeiCore's parament in stack.
658 TopOfStack
= (VOID
*)((UINTN
)TopOfStack
- sizeof (EFI_SEC_PEI_HAND_OFF
) - CPU_STACK_ALIGNMENT
);
659 TopOfStack
= ALIGN_POINTER (TopOfStack
, CPU_STACK_ALIGNMENT
);
662 // Bind this information into the SEC hand-off state
664 SecCoreData
= (EFI_SEC_PEI_HAND_OFF
*)(UINTN
)TopOfStack
;
665 SecCoreData
->DataSize
= sizeof (EFI_SEC_PEI_HAND_OFF
);
666 SecCoreData
->BootFirmwareVolumeBase
= BootFirmwareVolumeBase
;
667 SecCoreData
->BootFirmwareVolumeSize
= BootFirmwareVolumeSize
;
668 SecCoreData
->TemporaryRamBase
= (VOID
*)TemporaryRam
;
669 SecCoreData
->TemporaryRamSize
= TemporaryRamSize
;
670 SecCoreData
->StackBase
= SecCoreData
->TemporaryRamBase
;
671 SecCoreData
->StackSize
= SecStackSize
;
672 SecCoreData
->PeiTemporaryRamBase
= (VOID
*) ((UINTN
) SecCoreData
->TemporaryRamBase
+ SecStackSize
);
673 SecCoreData
->PeiTemporaryRamSize
= TemporaryRamSize
- SecStackSize
;
676 // Load the PEI Core from a Firmware Volume
678 Status
= SecPeCoffGetEntryPoint (
682 if (EFI_ERROR (Status
)) {
687 // Transfer control to the SEC Core
690 (SWITCH_STACK_ENTRY_POINT
)(UINTN
)SecCoreEntryPoint
,
696 // If we get here, then the SEC Core returned. This is an error
703 SecPeCoffGetEntryPoint (
705 IN OUT VOID
**EntryPoint
709 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
711 ZeroMem (&ImageContext
, sizeof (ImageContext
));
712 ImageContext
.Handle
= Pe32Data
;
714 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) SecImageRead
;
716 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
717 if (EFI_ERROR (Status
)) {
721 // Allocate space in NT (not emulator) memory with ReadWrite and Execute attribute.
722 // Extra space is for alignment
724 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VirtualAlloc (NULL
, (SIZE_T
) (ImageContext
.ImageSize
+ (ImageContext
.SectionAlignment
* 2)), MEM_COMMIT
, PAGE_EXECUTE_READWRITE
);
725 if (ImageContext
.ImageAddress
== 0) {
726 return EFI_OUT_OF_RESOURCES
;
729 // Align buffer on section boundary
731 ImageContext
.ImageAddress
+= ImageContext
.SectionAlignment
- 1;
732 ImageContext
.ImageAddress
&= ~((EFI_PHYSICAL_ADDRESS
)ImageContext
.SectionAlignment
- 1);
734 Status
= PeCoffLoaderLoadImage (&ImageContext
);
735 if (EFI_ERROR (Status
)) {
739 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
740 if (EFI_ERROR (Status
)) {
744 *EntryPoint
= (VOID
*)(UINTN
)ImageContext
.EntryPoint
;
754 IN OUT UINTN
*ReadSize
,
760 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
763 FileHandle - The handle to the PE/COFF file
764 FileOffset - The offset, in bytes, into the file to read
765 ReadSize - The number of bytes to read from the file starting at FileOffset
766 Buffer - A pointer to the buffer to read the data into.
769 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
777 Destination8
= Buffer
;
778 Source8
= (CHAR8
*) ((UINTN
) FileHandle
+ FileOffset
);
781 *(Destination8
++) = *(Source8
++);
790 IN UINTN
*StrLen OPTIONAL
795 Convert the passed in Ascii string to Unicode.
796 Optionally return the length of the strings.
799 Ascii - Ascii string to convert
800 StrLen - Length of string
803 Pointer to malloc'ed Unicode version of Ascii
811 // Allocate a buffer for unicode string
813 for (Index
= 0; Ascii
[Index
] != '\0'; Index
++)
815 Unicode
= malloc ((Index
+ 1) * sizeof (CHAR16
));
816 if (Unicode
== NULL
) {
820 for (Index
= 0; Ascii
[Index
] != '\0'; Index
++) {
821 Unicode
[Index
] = (CHAR16
) Ascii
[Index
];
824 Unicode
[Index
] = '\0';
826 if (StrLen
!= NULL
) {
834 CountSeparatorsInString (
835 IN CONST CHAR16
*String
,
841 Count the number of separators in String
844 String - String to process
845 Separator - Item to count
848 Number of Separator in String
854 for (Count
= 0; *String
!= '\0'; String
++) {
855 if (*String
== Separator
) {
864 Store the ModHandle in an array indexed by the Pdb File name.
865 The ModHandle is needed to unload the image.
866 @param ImageContext - Input data returned from PE Laoder Library. Used to find the
867 .PDB file name of the PE Image.
868 @param ModHandle - Returned from LoadLibraryEx() and stored for call to
870 @return return EFI_SUCCESS when ModHandle was stored.
874 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
880 PDB_NAME_TO_MOD_HANDLE
*Array
;
882 PDB_NAME_TO_MOD_HANDLE
*TempArray
;
887 // Return EFI_ALREADY_STARTED if this DLL has already been loaded
889 Array
= mPdbNameModHandleArray
;
890 for (Index
= 0; Index
< mPdbNameModHandleArraySize
; Index
++, Array
++) {
891 if (Array
->PdbPointer
!= NULL
&& Array
->ModHandle
== ModHandle
) {
892 return EFI_ALREADY_STARTED
;
896 Array
= mPdbNameModHandleArray
;
897 for (Index
= 0; Index
< mPdbNameModHandleArraySize
; Index
++, Array
++) {
898 if (Array
->PdbPointer
== NULL
) {
900 // Make a copy of the stirng and store the ModHandle
902 Handle
= GetProcessHeap ();
903 Size
= AsciiStrLen (ImageContext
->PdbPointer
) + 1;
904 Array
->PdbPointer
= HeapAlloc ( Handle
, HEAP_ZERO_MEMORY
, Size
);
905 ASSERT (Array
->PdbPointer
!= NULL
);
907 AsciiStrCpyS (Array
->PdbPointer
, Size
, ImageContext
->PdbPointer
);
908 Array
->ModHandle
= ModHandle
;
914 // No free space in mPdbNameModHandleArray so grow it by
915 // MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE entires.
917 PreviousSize
= mPdbNameModHandleArraySize
* sizeof (PDB_NAME_TO_MOD_HANDLE
);
918 mPdbNameModHandleArraySize
+= MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE
;
920 // re-allocate a new buffer and copy the old values to the new locaiton.
922 TempArray
= HeapAlloc (GetProcessHeap (),
924 mPdbNameModHandleArraySize
* sizeof (PDB_NAME_TO_MOD_HANDLE
)
927 CopyMem ((VOID
*) (UINTN
) TempArray
, (VOID
*) (UINTN
)mPdbNameModHandleArray
, PreviousSize
);
929 HeapFree (GetProcessHeap (), 0, mPdbNameModHandleArray
);
931 mPdbNameModHandleArray
= TempArray
;
932 if (mPdbNameModHandleArray
== NULL
) {
934 return EFI_OUT_OF_RESOURCES
;
937 return AddModHandle (ImageContext
, ModHandle
);
941 Return the ModHandle and delete the entry in the array.
942 @param ImageContext - Input data returned from PE Laoder Library. Used to find the
943 .PDB file name of the PE Image.
945 ModHandle - ModHandle assoicated with ImageContext is returned
946 NULL - No ModHandle associated with ImageContext
950 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
954 PDB_NAME_TO_MOD_HANDLE
*Array
;
956 if (ImageContext
->PdbPointer
== NULL
) {
958 // If no PDB pointer there is no ModHandle so return NULL
963 Array
= mPdbNameModHandleArray
;
964 for (Index
= 0; Index
< mPdbNameModHandleArraySize
; Index
++, Array
++) {
965 if ((Array
->PdbPointer
!= NULL
) && (AsciiStrCmp(Array
->PdbPointer
, ImageContext
->PdbPointer
) == 0)) {
967 // If you find a match return it and delete the entry
969 HeapFree (GetProcessHeap (), 0, Array
->PdbPointer
);
970 Array
->PdbPointer
= NULL
;
971 return Array
->ModHandle
;
980 PeCoffLoaderRelocateImageExtraAction (
981 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
990 ASSERT (ImageContext
!= NULL
);
992 // If we load our own PE COFF images the Windows debugger can not source
993 // level debug our code. If a valid PDB pointer exists use it to load
994 // the *.dll file as a library using Windows* APIs. This allows
995 // source level debug. The image is still loaded and relocated
996 // in the Framework memory space like on a real system (by the code above),
997 // but the entry point points into the DLL loaded by the code below.
1000 DllEntryPoint
= NULL
;
1003 // Load the DLL if it's not an EBC image.
1005 if ((ImageContext
->PdbPointer
!= NULL
) &&
1006 (ImageContext
->Machine
!= EFI_IMAGE_MACHINE_EBC
)) {
1008 // Convert filename from ASCII to Unicode
1010 DllFileName
= AsciiToUnicode (ImageContext
->PdbPointer
, &Index
);
1013 // Check that we have a valid filename
1015 if (Index
< 5 || DllFileName
[Index
- 4] != '.') {
1019 // Never return an error if PeCoffLoaderRelocateImage() succeeded.
1020 // The image will run, but we just can't source level debug. If we
1021 // return an error the image will not run.
1026 // Replace .PDB with .DLL on the filename
1028 DllFileName
[Index
- 3] = 'D';
1029 DllFileName
[Index
- 2] = 'L';
1030 DllFileName
[Index
- 1] = 'L';
1033 // Load the .DLL file into the user process's address space for source
1036 Library
= LoadLibraryEx (DllFileName
, NULL
, DONT_RESOLVE_DLL_REFERENCES
);
1037 if (Library
!= NULL
) {
1039 // InitializeDriver is the entry point we put in all our EFI DLL's. The
1040 // DONT_RESOLVE_DLL_REFERENCES argument to LoadLIbraryEx() suppresses the
1041 // normal DLL entry point of DllMain, and prevents other modules that are
1042 // referenced in side the DllFileName from being loaded. There is no error
1043 // checking as the we can point to the PE32 image loaded by Tiano. This
1044 // step is only needed for source level debugging
1046 DllEntryPoint
= (VOID
*) (UINTN
) GetProcAddress (Library
, "InitializeDriver");
1050 if ((Library
!= NULL
) && (DllEntryPoint
!= NULL
)) {
1051 Status
= AddModHandle (ImageContext
, Library
);
1052 if (Status
== EFI_ALREADY_STARTED
) {
1054 // If the DLL has already been loaded before, then this instance of the DLL can not be debugged.
1056 ImageContext
->PdbPointer
= NULL
;
1057 SecPrint ("WARNING: DLL already loaded. No source level debug %S.\n\r", DllFileName
);
1060 // This DLL is not already loaded, so source level debugging is supported.
1062 ImageContext
->EntryPoint
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) DllEntryPoint
;
1063 SecPrint ("LoadLibraryEx (\n\r %S,\n\r NULL, DONT_RESOLVE_DLL_REFERENCES)\n\r", DllFileName
);
1066 SecPrint ("WARNING: No source level debug %S. \n\r", DllFileName
);
1075 PeCoffLoaderUnloadImageExtraAction (
1076 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1081 ASSERT (ImageContext
!= NULL
);
1083 ModHandle
= RemoveModHandle (ImageContext
);
1084 if (ModHandle
!= NULL
) {
1085 FreeLibrary (ModHandle
);
1086 SecPrint ("FreeLibrary (\n\r %s)\n\r", ImageContext
->PdbPointer
);
1088 SecPrint ("WARNING: Unload image without source level debug\n\r");