3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 WinNt emulator of SEC phase. It's really a Win32 application, but this is
18 Ok since all the other modules for NT32 are NOT Win32 applications.
20 This program processes Windows environment variables and figures out
21 what the memory layout will be, how may FD's will be loaded and also
22 what the boot mode is.
24 The SEC registers a set of services with the SEC core. gPrivateDispatchTable
25 is a list of PPI's produced by the SEC that are availble for usage in PEI.
27 This code produces 128 K of temporary memory for the PEI stack by opening a
28 Windows file and mapping it directly to memory addresses.
30 The system.cmd script is used to set windows environment variables that drive
31 the configuration opitons of the SEC.
40 EFI_PEI_PE_COFF_LOADER_PROTOCOL_INSTANCE mPeiEfiPeiPeCoffLoaderInstance
= {
42 SecNt32PeCoffGetImageInfo
,
43 SecNt32PeCoffLoadImage
,
44 SecNt32PeCoffRelocateImage
,
45 SecNt32PeCoffUnloadimage
52 EFI_PEI_PE_COFF_LOADER_PROTOCOL
*gPeiEfiPeiPeCoffLoader
= &mPeiEfiPeiPeCoffLoaderInstance
.PeCoff
;
54 NT_PEI_LOAD_FILE_PPI mSecNtLoadFilePpi
= { SecWinNtPeiLoadFile
};
56 PEI_NT_AUTOSCAN_PPI mSecNtAutoScanPpi
= { SecWinNtPeiAutoScan
};
58 PEI_NT_THUNK_PPI mSecWinNtThunkPpi
= { SecWinNtWinNtThunkAddress
};
60 EFI_PEI_PROGRESS_CODE_PPI mSecStatusCodePpi
= { SecPeiReportStatusCode
};
62 NT_FWH_PPI mSecFwhInformationPpi
= { SecWinNtFdAddress
};
65 EFI_PEI_PPI_DESCRIPTOR gPrivateDispatchTable
[] = {
67 EFI_PEI_PPI_DESCRIPTOR_PPI
,
68 &gEfiPeiPeCoffLoaderGuid
,
72 EFI_PEI_PPI_DESCRIPTOR_PPI
,
73 &gNtPeiLoadFilePpiGuid
,
77 EFI_PEI_PPI_DESCRIPTOR_PPI
,
78 &gPeiNtAutoScanPpiGuid
,
82 EFI_PEI_PPI_DESCRIPTOR_PPI
,
87 EFI_PEI_PPI_DESCRIPTOR_PPI
,
88 &gEfiPeiStatusCodePpiGuid
,
92 EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
,
94 &mSecFwhInformationPpi
100 // Default information about where the FD is located.
101 // This array gets filled in with information from EFI_FIRMWARE_VOLUMES
102 // EFI_FIRMWARE_VOLUMES is a Windows environment variable set by system.cmd.
103 // The number of array elements is allocated base on parsing
104 // EFI_FIRMWARE_VOLUMES and the memory is never freed.
106 UINTN gFdInfoCount
= 0;
110 // Array that supports seperate memory rantes.
111 // The memory ranges are set in system.cmd via the EFI_MEMORY_SIZE variable.
112 // The number of array elements is allocated base on parsing
113 // EFI_MEMORY_SIZE and the memory is never freed.
115 UINTN gSystemMemoryCount
= 0;
116 NT_SYSTEM_MEMORY
*gSystemMemory
;
119 UINTN mPdbNameModHandleArraySize
= 0;
120 PDB_NAME_TO_MOD_HANDLE
*mPdbNameModHandleArray
= NULL
;
135 Main entry point to SEC for WinNt. This is a Windows program
138 Argc - Number of command line arguments
139 Argv - Array of command line argument strings
140 Envp - Array of environmemt variable strings
149 EFI_PHYSICAL_ADDRESS InitialStackMemory
;
150 UINT64 InitialStackMemorySize
;
159 CHAR16
*MemorySizeStr
;
160 CHAR16
*FirmwareVolumesStr
;
162 MemorySizeStr
= (CHAR16
*)PcdGetPtr (PcdWinNtMemorySizeForSecMain
);
163 FirmwareVolumesStr
= (CHAR16
*)PcdGetPtr (PcdWinNtFirmwareVolume
);
165 printf ("\nEDK SEC Main NT Emulation Environment from www.TianoCore.org\n");
168 // Make some Windows calls to Set the process to the highest priority in the
169 // idle class. We need this to have good performance.
171 SetPriorityClass (GetCurrentProcess (), IDLE_PRIORITY_CLASS
);
172 SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_HIGHEST
);
175 // Allocate space for gSystemMemory Array
177 gSystemMemoryCount
= CountSeperatorsInString (MemorySizeStr
, '!') + 1;
178 gSystemMemory
= calloc (gSystemMemoryCount
, sizeof (NT_SYSTEM_MEMORY
));
179 if (gSystemMemory
== NULL
) {
180 printf ("ERROR : Can not allocate memory for %s. Exiting.\n", MemorySizeStr
);
184 // Allocate space for gSystemMemory Array
186 gFdInfoCount
= CountSeperatorsInString (FirmwareVolumesStr
, '!') + 1;
187 gFdInfo
= calloc (gFdInfoCount
, sizeof (NT_FD_INFO
));
188 if (gFdInfo
== NULL
) {
189 printf ("ERROR : Can not allocate memory for %s. Exiting.\n", FirmwareVolumesStr
);
193 // Setup Boot Mode. If BootModeStr == "" then BootMode = 0 (BOOT_WITH_FULL_CONFIGURATION)
195 printf (" BootMode 0x%02x\n", FixedPcdGet32 (PcdWinNtBootMode
));
198 // Open up a 128K file to emulate temp memory for PEI.
199 // on a real platform this would be SRAM, or using the cache as RAM.
200 // Set InitialStackMemory to zero so WinNtOpenFile will allocate a new mapping
202 InitialStackMemory
= 0;
203 InitialStackMemorySize
= 0x20000;
204 Status
= WinNtOpenFile (
206 (UINT32
) InitialStackMemorySize
,
209 &InitialStackMemorySize
211 if (EFI_ERROR (Status
)) {
212 printf ("ERROR : Can not open SecStack Exiting\n");
216 printf (" SEC passing in %d bytes of temp RAM to PEI\n", InitialStackMemorySize
);
219 // Open All the firmware volumes and remember the info in the gFdInfo global
221 FileNamePtr
= (CHAR16
*)malloc (StrLen ((CHAR16
*)FirmwareVolumesStr
) * sizeof(CHAR16
));
222 if (FileNamePtr
== NULL
) {
223 printf ("ERROR : Can not allocate memory for firmware volume string\n");
227 StrCpy (FileNamePtr
, (CHAR16
*)FirmwareVolumesStr
);
229 for (Done
= FALSE
, Index
= 0, PeiIndex
= 0, PeiCoreFile
= NULL
; !Done
; Index
++) {
230 FileName
= FileNamePtr
;
231 for (Index1
= 0; (FileNamePtr
[Index1
] != '!') && (FileNamePtr
[Index1
] != 0); Index1
++)
233 if (FileNamePtr
[Index1
] == 0) {
236 FileNamePtr
[Index1
] = '\0';
237 FileNamePtr
= FileNamePtr
+ Index1
+ 1;
241 // Open the FD and remmeber where it got mapped into our processes address space
243 Status
= WinNtOpenFile (
247 &gFdInfo
[Index
].Address
,
250 if (EFI_ERROR (Status
)) {
251 printf ("ERROR : Can not open Firmware Device File %S (%r). Exiting.\n", FileName
, Status
);
255 printf (" FD loaded from");
257 // printf can't print filenames directly as the \ gets interperted as an
260 for (Index2
= 0; FileName
[Index2
] != '\0'; Index2
++) {
261 printf ("%c", FileName
[Index2
]);
264 if (PeiCoreFile
== NULL
) {
266 // Assume the beginning of the FD is an FV and look for the PEI Core.
267 // Load the first one we find.
269 Status
= SecFfsFindPeiCore ((EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) gFdInfo
[Index
].Address
, &PeiCoreFile
);
270 if (!EFI_ERROR (Status
)) {
272 printf (" contains SEC Core");
279 // Calculate memory regions and store the information in the gSystemMemory
280 // global for later use. The autosizing code will use this data to
281 // map this memory into the SEC process memory space.
283 for (Index
= 0, Done
= FALSE
; !Done
; Index
++) {
285 // Save the size of the memory and make a Unicode filename SystemMemory00, ...
287 gSystemMemory
[Index
].Size
= _wtoi (MemorySizeStr
) * 0x100000;
288 _snwprintf (gSystemMemory
[Index
].FileName
, NT_SYSTEM_MEMORY_FILENAME_SIZE
, L
"SystemMemory%02d", Index
);
291 // Find the next region
293 for (Index1
= 0; MemorySizeStr
[Index1
] != '!' && MemorySizeStr
[Index1
] != 0; Index1
++)
295 if (MemorySizeStr
[Index1
] == 0) {
299 MemorySizeStr
= MemorySizeStr
+ Index1
+ 1;
305 // Hand off to PEI Core
307 SecLoadFromCore ((UINTN
) InitialStackMemory
, (UINTN
) InitialStackMemorySize
, (UINTN
) gFdInfo
[0].Address
, PeiCoreFile
);
310 // If we get here, then the PEI Core returned. This is an error as PEI should
311 // always hand off to DXE.
313 printf ("ERROR : PEI Core returned\n");
321 IN DWORD CreationDisposition
,
322 IN OUT EFI_PHYSICAL_ADDRESS
*BaseAddress
,
328 Opens and memory maps a file using WinNt services. If BaseAddress is non zero
329 the process will try and allocate the memory starting at BaseAddress.
332 FileName - The name of the file to open and map
333 MapSize - The amount of the file to map in bytes
334 CreationDisposition - The flags to pass to CreateFile(). Use to create new files for
335 memory emulation, and exiting files for firmware volume emulation
336 BaseAddress - The base address of the mapped file in the user address space.
337 If passed in as NULL the a new memory region is used.
338 If passed in as non NULL the request memory region is used for
339 the mapping of the file into the process space.
340 Length - The size of the mapped region in bytes
343 EFI_SUCCESS - The file was opened and mapped.
344 EFI_NOT_FOUND - FileName was not found in the current directory
345 EFI_DEVICE_ERROR - An error occured attempting to map the opened file
351 VOID
*VirtualAddress
;
355 // Use Win API to open/create a file
357 NtFileHandle
= CreateFile (
359 GENERIC_READ
| GENERIC_WRITE
,
363 FILE_ATTRIBUTE_NORMAL
,
366 if (NtFileHandle
== INVALID_HANDLE_VALUE
) {
367 return EFI_NOT_FOUND
;
370 // Map the open file into a memory range
372 NtMapHandle
= CreateFileMapping (
380 if (NtMapHandle
== NULL
) {
381 return EFI_DEVICE_ERROR
;
384 // Get the virtual address (address in the emulator) of the mapped file
386 VirtualAddress
= MapViewOfFileEx (
392 (LPVOID
) (UINTN
) *BaseAddress
394 if (VirtualAddress
== NULL
) {
395 return EFI_DEVICE_ERROR
;
400 // Seek to the end of the file to figure out the true file size.
402 FileSize
= SetFilePointer (
408 if (FileSize
== -1) {
409 return EFI_DEVICE_ERROR
;
412 *Length
= (UINT64
) FileSize
;
414 *Length
= (UINT64
) MapSize
;
417 *BaseAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VirtualAddress
;
422 #define BYTES_PER_RECORD 512
426 SecPeiReportStatusCode (
427 IN EFI_PEI_SERVICES
**PeiServices
,
428 IN EFI_STATUS_CODE_TYPE CodeType
,
429 IN EFI_STATUS_CODE_VALUE Value
,
431 IN EFI_GUID
* CallerId
,
432 IN EFI_STATUS_CODE_DATA
* Data OPTIONAL
438 This routine produces the ReportStatusCode PEI service. It's passed
439 up to the PEI Core via a PPI. T
441 This code currently uses the NT clib printf. This does not work the same way
442 as the EFI Print (), as %t, %g, %s as Unicode are not supported.
445 (see EFI_PEI_REPORT_STATUS_CODE)
448 EFI_SUCCESS - Always return success
451 // TODO: PeiServices - add argument and description to function comment
452 // TODO: CodeType - add argument and description to function comment
453 // TODO: Value - add argument and description to function comment
454 // TODO: Instance - add argument and description to function comment
455 // TODO: CallerId - add argument and description to function comment
456 // TODO: Data - add argument and description to function comment
459 EFI_DEBUG_INFO
*DebugInfo
;
461 CHAR8 PrintBuffer
[BYTES_PER_RECORD
* 2];
466 if ((CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_DEBUG_CODE
) {
468 // This supports DEBUG () marcos
470 // EFI_STATUS_CODE_DATA
473 // The first 12 * UINT64 bytes of the string are really an
474 // arguement stack to support varargs on the Format string.
476 DebugInfo
= (EFI_DEBUG_INFO
*) (Data
+ 1);
477 Marker
= (VA_LIST
) (DebugInfo
+ 1);
478 Format
= (CHAR8
*) (((UINT64
*) Marker
) + 12);
480 AsciiVSPrint (PrintBuffer
, BYTES_PER_RECORD
, Format
, Marker
);
481 printf (PrintBuffer
);
484 if (((CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_ERROR_CODE
) &&
485 ((CodeType
& EFI_STATUS_CODE_SEVERITY_MASK
) == EFI_ERROR_UNRECOVERED
)
487 if (ReportStatusCodeExtractAssertInfo (CodeType
, Value
, Data
, &Filename
, &Description
, &LineNumber
)) {
489 // Support ASSERT () macro
491 printf ("ASSERT %s(%d): %s\n", Filename
, LineNumber
, Description
);
502 IN UINTN LargestRegion
,
503 IN UINTN LargestRegionSize
,
504 IN UINTN BootFirmwareVolumeBase
,
505 IN VOID
*PeiCorePe32File
510 This is the service to load the PEI Core from the Firmware Volume
513 LargestRegion - Memory to use for PEI.
514 LargestRegionSize - Size of Memory to use for PEI
515 BootFirmwareVolumeBase - Start of the Boot FV
516 PeiCorePe32File - PEI Core PE32
519 Success means control is transfered and thus we should never return
524 EFI_PHYSICAL_ADDRESS TopOfMemory
;
527 EFI_PHYSICAL_ADDRESS PeiCoreEntryPoint
;
528 EFI_PHYSICAL_ADDRESS PeiImageAddress
;
529 EFI_PEI_STARTUP_DESCRIPTOR
*PeiStartup
;
532 // Compute Top Of Memory for Stack and PEI Core Allocations
534 TopOfMemory
= LargestRegion
+ ((LargestRegionSize
) & (~15));
537 // Allocate 128KB for the Stack
539 TopOfStack
= (VOID
*) (UINTN
) (TopOfMemory
- sizeof (EFI_PEI_STARTUP_DESCRIPTOR
));
540 TopOfMemory
= TopOfMemory
- STACK_SIZE
;
543 // Patch value in dispatch table values
545 gPrivateDispatchTable
[0].Ppi
= gPeiEfiPeiPeCoffLoader
;
548 // Bind this information into the SEC hand-off state
550 PeiStartup
= (EFI_PEI_STARTUP_DESCRIPTOR
*) (UINTN
) TopOfStack
;
551 PeiStartup
->DispatchTable
= (EFI_PEI_PPI_DESCRIPTOR
*) &gPrivateDispatchTable
;
552 PeiStartup
->SizeOfCacheAsRam
= STACK_SIZE
;
553 PeiStartup
->BootFirmwareVolume
= BootFirmwareVolumeBase
;
556 // Load the PEI Core from a Firmware Volume
558 Status
= SecWinNtPeiLoadFile (
564 if (EFI_ERROR (Status
)) {
568 // Transfer control to the PEI Core
571 (SWITCH_STACK_ENTRY_POINT
) (UINTN
) PeiCoreEntryPoint
,
577 // If we get here, then the PEI Core returned. This is an error
584 SecWinNtPeiAutoScan (
586 OUT EFI_PHYSICAL_ADDRESS
*MemoryBase
,
587 OUT UINT64
*MemorySize
592 This service is called from Index == 0 until it returns EFI_UNSUPPORTED.
593 It allows discontiguous memory regions to be supported by the emulator.
594 It uses gSystemMemory[] and gSystemMemoryCount that were created by
595 parsing the Windows environment variable EFI_MEMORY_SIZE.
596 The size comes from the varaible and the address comes from the call to
600 Index - Which memory region to use
601 MemoryBase - Return Base address of memory region
602 MemorySize - Return size in bytes of the memory region
605 EFI_SUCCESS - If memory region was mapped
606 EFI_UNSUPPORTED - If Index is not supported
612 if (Index
>= gSystemMemoryCount
) {
613 return EFI_UNSUPPORTED
;
617 Status
= WinNtOpenFile (
618 gSystemMemory
[Index
].FileName
,
619 (UINT32
) gSystemMemory
[Index
].Size
,
625 gSystemMemory
[Index
].Memory
= *MemoryBase
;
632 SecWinNtWinNtThunkAddress (
638 Since the SEC is the only Windows program in stack it must export
639 an interface to do Win API calls. That's what the WinNtThunk address
640 is for. gWinNt is initailized in WinNtThunk.c.
643 InterfaceSize - sizeof (EFI_WIN_NT_THUNK_PROTOCOL);
644 InterfaceBase - Address of the gWinNt global
647 EFI_SUCCESS - Data returned
657 SecWinNtPeiLoadFile (
659 IN EFI_PHYSICAL_ADDRESS
*ImageAddress
,
660 IN UINT64
*ImageSize
,
661 IN EFI_PHYSICAL_ADDRESS
*EntryPoint
666 Loads and relocates a PE/COFF image into memory.
669 Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated
670 ImageAddress - The base address of the relocated PE/COFF image
671 ImageSize - The size of the relocated PE/COFF image
672 EntryPoint - The entry point of the relocated PE/COFF image
675 EFI_SUCCESS - The file was loaded and relocated
676 EFI_OUT_OF_RESOURCES - There was not enough memory to load and relocate the PE/COFF file
681 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
683 ZeroMem (&ImageContext
, sizeof (ImageContext
));
684 ImageContext
.Handle
= Pe32Data
;
686 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) SecImageRead
;
688 Status
= gPeiEfiPeiPeCoffLoader
->GetImageInfo (gPeiEfiPeiPeCoffLoader
, &ImageContext
);
689 if (EFI_ERROR (Status
)) {
693 // Allocate space in NT (not emulator) memory. Extra space is for alignment
695 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) malloc ((UINTN
) (ImageContext
.ImageSize
+ (ImageContext
.SectionAlignment
* 2)));
696 if (ImageContext
.ImageAddress
== 0) {
697 return EFI_OUT_OF_RESOURCES
;
700 // Align buffer on section boundry
702 ImageContext
.ImageAddress
+= ImageContext
.SectionAlignment
;
703 ImageContext
.ImageAddress
&= ~(ImageContext
.SectionAlignment
- 1);
705 Status
= gPeiEfiPeiPeCoffLoader
->LoadImage (gPeiEfiPeiPeCoffLoader
, &ImageContext
);
706 if (EFI_ERROR (Status
)) {
710 Status
= gPeiEfiPeiPeCoffLoader
->RelocateImage (gPeiEfiPeiPeCoffLoader
, &ImageContext
);
711 if (EFI_ERROR (Status
)) {
716 // BugBug: Flush Instruction Cache Here when CPU Lib is ready
719 *ImageAddress
= ImageContext
.ImageAddress
;
720 *ImageSize
= ImageContext
.ImageSize
;
721 *EntryPoint
= ImageContext
.EntryPoint
;
730 IN OUT EFI_PHYSICAL_ADDRESS
*FdBase
,
731 IN OUT UINT64
*FdSize
736 Return the FD Size and base address. Since the FD is loaded from a
737 file into Windows memory only the SEC will know it's address.
740 Index - Which FD, starts at zero.
741 FdSize - Size of the FD in bytes
742 FdBase - Start address of the FD. Assume it points to an FV Header
745 EFI_SUCCESS - Return the Base address and size of the FV
746 EFI_UNSUPPORTED - Index does nto map to an FD in the system
750 if (Index
>= gFdInfoCount
) {
751 return EFI_UNSUPPORTED
;
754 *FdBase
= gFdInfo
[Index
].Address
;
755 *FdSize
= gFdInfo
[Index
].Size
;
757 if (*FdBase
== 0 && *FdSize
== 0) {
758 return EFI_UNSUPPORTED
;
769 IN OUT UINTN
*ReadSize
,
775 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
778 FileHandle - The handle to the PE/COFF file
779 FileOffset - The offset, in bytes, into the file to read
780 ReadSize - The number of bytes to read from the file starting at FileOffset
781 Buffer - A pointer to the buffer to read the data into.
784 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
792 Destination8
= Buffer
;
793 Source8
= (CHAR8
*) ((UINTN
) FileHandle
+ FileOffset
);
796 *(Destination8
++) = *(Source8
++);
805 IN UINTN
*StrLen OPTIONAL
810 Convert the passed in Ascii string to Unicode.
811 Optionally return the length of the strings.
814 Ascii - Ascii string to convert
815 StrLen - Length of string
818 Pointer to malloc'ed Unicode version of Ascii
826 // Allocate a buffer for unicode string
828 for (Index
= 0; Ascii
[Index
] != '\0'; Index
++)
830 Unicode
= malloc ((Index
+ 1) * sizeof (CHAR16
));
831 if (Unicode
== NULL
) {
835 for (Index
= 0; Ascii
[Index
] != '\0'; Index
++) {
836 Unicode
[Index
] = (CHAR16
) Ascii
[Index
];
839 Unicode
[Index
] = '\0';
841 if (StrLen
!= NULL
) {
849 CountSeperatorsInString (
850 IN
const CHAR16
*String
,
856 Count the number of seperators in String
859 String - String to process
860 Seperator - Item to count
863 Number of Seperator in String
869 for (Count
= 0; *String
!= '\0'; String
++) {
870 if (*String
== Seperator
) {
881 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
887 Store the ModHandle in an array indexed by the Pdb File name.
888 The ModHandle is needed to unload the image.
891 ImageContext - Input data returned from PE Laoder Library. Used to find the
892 .PDB file name of the PE Image.
893 ModHandle - Returned from LoadLibraryEx() and stored for call to
897 EFI_SUCCESS - ModHandle was stored.
902 PDB_NAME_TO_MOD_HANDLE
*Array
;
906 Array
= mPdbNameModHandleArray
;
907 for (Index
= 0; Index
< mPdbNameModHandleArraySize
; Index
++, Array
++) {
908 if (Array
->PdbPointer
== NULL
) {
910 // Make a copy of the stirng and store the ModHandle
912 Array
->PdbPointer
= malloc (strlen (ImageContext
->PdbPointer
) + 1);
913 ASSERT (Array
->PdbPointer
!= NULL
);
915 strcpy (Array
->PdbPointer
, ImageContext
->PdbPointer
);
916 Array
->ModHandle
= ModHandle
;
922 // No free space in mPdbNameModHandleArray so grow it by
923 // MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE entires. realloc will
924 // copy the old values to the new locaiton. But it does
925 // not zero the new memory area.
927 PreviousSize
= mPdbNameModHandleArraySize
* sizeof (PDB_NAME_TO_MOD_HANDLE
);
928 mPdbNameModHandleArraySize
+= MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE
;
930 mPdbNameModHandleArray
= realloc (mPdbNameModHandleArray
, mPdbNameModHandleArraySize
* sizeof (PDB_NAME_TO_MOD_HANDLE
));
931 if (mPdbNameModHandleArray
== NULL
) {
933 return EFI_OUT_OF_RESOURCES
;
936 memset (mPdbNameModHandleArray
+ PreviousSize
, 0, MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE
* sizeof (PDB_NAME_TO_MOD_HANDLE
));
938 return AddModHandle (ImageContext
, ModHandle
);
944 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
949 Return the ModHandle and delete the entry in the array.
952 ImageContext - Input data returned from PE Laoder Library. Used to find the
953 .PDB file name of the PE Image.
956 ModHandle - ModHandle assoicated with ImageContext is returned
957 NULL - No ModHandle associated with ImageContext
962 PDB_NAME_TO_MOD_HANDLE
*Array
;
964 if (ImageContext
->PdbPointer
== NULL
) {
966 // If no PDB pointer there is no ModHandle so return NULL
971 Array
= mPdbNameModHandleArray
;
972 for (Index
= 0; Index
< mPdbNameModHandleArraySize
; Index
++, Array
++) {
973 if ((Array
->PdbPointer
!= NULL
) && (strcmp(Array
->PdbPointer
, ImageContext
->PdbPointer
) == 0)) {
975 // If you find a match return it and delete the entry
977 free (Array
->PdbPointer
);
978 Array
->PdbPointer
= NULL
;
979 return Array
->ModHandle
;
990 SecNt32PeCoffGetImageInfo (
991 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL
*This
,
992 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
997 Status
= PeCoffLoaderGetImageInfo (ImageContext
);
998 if (EFI_ERROR (Status
)) {
1002 switch (ImageContext
->ImageType
) {
1004 case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
:
1005 ImageContext
->ImageCodeMemoryType
= EfiLoaderCode
;
1006 ImageContext
->ImageDataMemoryType
= EfiLoaderData
;
1009 case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
:
1010 ImageContext
->ImageCodeMemoryType
= EfiBootServicesCode
;
1011 ImageContext
->ImageDataMemoryType
= EfiBootServicesData
;
1014 case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
:
1015 case EFI_IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER
:
1016 ImageContext
->ImageCodeMemoryType
= EfiRuntimeServicesCode
;
1017 ImageContext
->ImageDataMemoryType
= EfiRuntimeServicesData
;
1021 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_SUBSYSTEM
;
1022 return RETURN_UNSUPPORTED
;
1030 SecNt32PeCoffLoadImage (
1031 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL
*This
,
1032 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1037 Status
= PeCoffLoaderLoadImage (ImageContext
);
1043 SecNt32PeCoffRelocateImage (
1044 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL
*This
,
1045 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1049 VOID
*DllEntryPoint
;
1050 CHAR16
*DllFileName
;
1055 Status
= PeCoffLoaderRelocateImage (ImageContext
);
1056 if (EFI_ERROR (Status
)) {
1058 // We could not relocated the image in memory properly
1064 // If we load our own PE COFF images the Windows debugger can not source
1065 // level debug our code. If a valid PDB pointer exists usw it to load
1066 // the *.dll file as a library using Windows* APIs. This allows
1067 // source level debug. The image is still loaded and reloaced
1068 // in the Framework memory space like on a real system (by the code above),
1069 // but the entry point points into the DLL loaded by the code bellow.
1072 DllEntryPoint
= NULL
;
1075 // Load the DLL if it's not an EBC image.
1077 if ((ImageContext
->PdbPointer
!= NULL
) &&
1078 (ImageContext
->Machine
!= EFI_IMAGE_MACHINE_EBC
)) {
1080 // Convert filename from ASCII to Unicode
1082 DllFileName
= AsciiToUnicode (ImageContext
->PdbPointer
, &Index
);
1085 // Check that we have a valid filename
1087 if (Index
< 5 || DllFileName
[Index
- 4] != '.') {
1091 // Never return an error if PeCoffLoaderRelocateImage() succeeded.
1092 // The image will run, but we just can't source level debug. If we
1093 // return an error the image will not run.
1098 // Replace .PDB with .DLL on the filename
1100 DllFileName
[Index
- 3] = 'D';
1101 DllFileName
[Index
- 2] = 'L';
1102 DllFileName
[Index
- 1] = 'L';
1105 // Load the .DLL file into the user process's address space for source
1108 Library
= LoadLibraryEx (DllFileName
, NULL
, DONT_RESOLVE_DLL_REFERENCES
);
1109 if (Library
!= NULL
) {
1111 // InitializeDriver is the entry point we put in all our EFI DLL's. The
1112 // DONT_RESOLVE_DLL_REFERENCES argument to LoadLIbraryEx() supresses the
1113 // normal DLL entry point of DllMain, and prevents other modules that are
1114 // referenced in side the DllFileName from being loaded. There is no error
1115 // checking as the we can point to the PE32 image loaded by Tiano. This
1116 // step is only needed for source level debuging
1118 DllEntryPoint
= (VOID
*) (UINTN
) GetProcAddress (Library
, "InitializeDriver");
1122 if ((Library
!= NULL
) && (DllEntryPoint
!= NULL
)) {
1123 AddModHandle (ImageContext
, Library
);
1124 ImageContext
->EntryPoint
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) DllEntryPoint
;
1125 wprintf (L
"LoadLibraryEx (%s,\n NULL, DONT_RESOLVE_DLL_REFERENCES)\n", DllFileName
);
1127 wprintf (L
"WARNING: No source level debug %s. \n", DllFileName
);
1134 // Never return an error if PeCoffLoaderRelocateImage() succeeded.
1135 // The image will run, but we just can't source level debug. If we
1136 // return an error the image will not run.
1144 SecNt32PeCoffUnloadimage (
1145 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL
*This
,
1146 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1151 ModHandle
= RemoveModeHandle (ImageContext
);
1152 if (ModHandle
!= NULL
) {
1153 FreeLibrary (ModHandle
);