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.
36 #pragma warning(disable : 4996)
41 EFI_PEI_PE_COFF_LOADER_PROTOCOL_INSTANCE mPeiEfiPeiPeCoffLoaderInstance
= {
43 SecNt32PeCoffGetImageInfo
,
44 SecNt32PeCoffLoadImage
,
45 SecNt32PeCoffRelocateImage
,
46 SecNt32PeCoffUnloadimage
53 EFI_PEI_PE_COFF_LOADER_PROTOCOL
*gPeiEfiPeiPeCoffLoader
= &mPeiEfiPeiPeCoffLoaderInstance
.PeCoff
;
55 NT_PEI_LOAD_FILE_PPI mSecNtLoadFilePpi
= { SecWinNtPeiLoadFile
};
57 PEI_NT_AUTOSCAN_PPI mSecNtAutoScanPpi
= { SecWinNtPeiAutoScan
};
59 PEI_NT_THUNK_PPI mSecWinNtThunkPpi
= { SecWinNtWinNtThunkAddress
};
61 EFI_PEI_PROGRESS_CODE_PPI mSecStatusCodePpi
= { SecPeiReportStatusCode
};
63 NT_FWH_PPI mSecFwhInformationPpi
= { SecWinNtFdAddress
};
66 EFI_PEI_PPI_DESCRIPTOR gPrivateDispatchTable
[] = {
68 EFI_PEI_PPI_DESCRIPTOR_PPI
,
69 &gEfiPeiPeCoffLoaderGuid
,
73 EFI_PEI_PPI_DESCRIPTOR_PPI
,
74 &gNtPeiLoadFilePpiGuid
,
78 EFI_PEI_PPI_DESCRIPTOR_PPI
,
79 &gPeiNtAutoScanPpiGuid
,
83 EFI_PEI_PPI_DESCRIPTOR_PPI
,
88 EFI_PEI_PPI_DESCRIPTOR_PPI
,
89 &gEfiPeiStatusCodePpiGuid
,
93 EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
,
95 &mSecFwhInformationPpi
101 // Default information about where the FD is located.
102 // This array gets filled in with information from EFI_FIRMWARE_VOLUMES
103 // EFI_FIRMWARE_VOLUMES is a Windows environment variable set by system.cmd.
104 // The number of array elements is allocated base on parsing
105 // EFI_FIRMWARE_VOLUMES and the memory is never freed.
107 UINTN gFdInfoCount
= 0;
111 // Array that supports seperate memory rantes.
112 // The memory ranges are set in system.cmd via the EFI_MEMORY_SIZE variable.
113 // The number of array elements is allocated base on parsing
114 // EFI_MEMORY_SIZE and the memory is never freed.
116 UINTN gSystemMemoryCount
= 0;
117 NT_SYSTEM_MEMORY
*gSystemMemory
;
120 UINTN mPdbNameModHandleArraySize
= 0;
121 PDB_NAME_TO_MOD_HANDLE
*mPdbNameModHandleArray
= NULL
;
136 Main entry point to SEC for WinNt. This is a Windows program
139 Argc - Number of command line arguments
140 Argv - Array of command line argument strings
141 Envp - Array of environmemt variable strings
150 EFI_PHYSICAL_ADDRESS InitialStackMemory
;
151 UINT64 InitialStackMemorySize
;
160 CHAR16
*MemorySizeStr
;
161 CHAR16
*FirmwareVolumesStr
;
163 MemorySizeStr
= (CHAR16
*)PcdGetPtr (PcdWinNtMemorySizeForSecMain
);
164 FirmwareVolumesStr
= (CHAR16
*)PcdGetPtr (PcdWinNtFirmwareVolume
);
166 printf ("\nEDK SEC Main NT Emulation Environment from www.TianoCore.org\n");
169 // Make some Windows calls to Set the process to the highest priority in the
170 // idle class. We need this to have good performance.
172 SetPriorityClass (GetCurrentProcess (), IDLE_PRIORITY_CLASS
);
173 SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_HIGHEST
);
176 // Allocate space for gSystemMemory Array
178 gSystemMemoryCount
= CountSeperatorsInString (MemorySizeStr
, '!') + 1;
179 gSystemMemory
= calloc (gSystemMemoryCount
, sizeof (NT_SYSTEM_MEMORY
));
180 if (gSystemMemory
== NULL
) {
181 printf ("ERROR : Can not allocate memory for %s. Exiting.\n", MemorySizeStr
);
185 // Allocate space for gSystemMemory Array
187 gFdInfoCount
= CountSeperatorsInString (FirmwareVolumesStr
, '!') + 1;
188 gFdInfo
= calloc (gFdInfoCount
, sizeof (NT_FD_INFO
));
189 if (gFdInfo
== NULL
) {
190 printf ("ERROR : Can not allocate memory for %s. Exiting.\n", FirmwareVolumesStr
);
194 // Setup Boot Mode. If BootModeStr == "" then BootMode = 0 (BOOT_WITH_FULL_CONFIGURATION)
196 printf (" BootMode 0x%02x\n", FixedPcdGet32 (PcdWinNtBootMode
));
199 // Open up a 128K file to emulate temp memory for PEI.
200 // on a real platform this would be SRAM, or using the cache as RAM.
201 // Set InitialStackMemory to zero so WinNtOpenFile will allocate a new mapping
203 InitialStackMemory
= 0;
204 InitialStackMemorySize
= 0x20000;
205 Status
= WinNtOpenFile (
207 (UINT32
) InitialStackMemorySize
,
210 &InitialStackMemorySize
212 if (EFI_ERROR (Status
)) {
213 printf ("ERROR : Can not open SecStack Exiting\n");
217 printf (" SEC passing in %d bytes of temp RAM to PEI\n", InitialStackMemorySize
);
220 // Open All the firmware volumes and remember the info in the gFdInfo global
222 FileNamePtr
= (CHAR16
*)malloc (StrLen ((CHAR16
*)FirmwareVolumesStr
) * sizeof(CHAR16
));
223 if (FileNamePtr
== NULL
) {
224 printf ("ERROR : Can not allocate memory for firmware volume string\n");
228 StrCpy (FileNamePtr
, (CHAR16
*)FirmwareVolumesStr
);
230 for (Done
= FALSE
, Index
= 0, PeiIndex
= 0, PeiCoreFile
= NULL
; !Done
; Index
++) {
231 FileName
= FileNamePtr
;
232 for (Index1
= 0; (FileNamePtr
[Index1
] != '!') && (FileNamePtr
[Index1
] != 0); Index1
++)
234 if (FileNamePtr
[Index1
] == 0) {
237 FileNamePtr
[Index1
] = '\0';
238 FileNamePtr
= FileNamePtr
+ Index1
+ 1;
242 // Open the FD and remmeber where it got mapped into our processes address space
244 Status
= WinNtOpenFile (
248 &gFdInfo
[Index
].Address
,
251 if (EFI_ERROR (Status
)) {
252 printf ("ERROR : Can not open Firmware Device File %S (%r). Exiting.\n", FileName
, Status
);
256 printf (" FD loaded from");
258 // printf can't print filenames directly as the \ gets interperted as an
261 for (Index2
= 0; FileName
[Index2
] != '\0'; Index2
++) {
262 printf ("%c", FileName
[Index2
]);
265 if (PeiCoreFile
== NULL
) {
267 // Assume the beginning of the FD is an FV and look for the PEI Core.
268 // Load the first one we find.
270 Status
= SecFfsFindPeiCore ((EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) gFdInfo
[Index
].Address
, &PeiCoreFile
);
271 if (!EFI_ERROR (Status
)) {
273 printf (" contains SEC Core");
280 // Calculate memory regions and store the information in the gSystemMemory
281 // global for later use. The autosizing code will use this data to
282 // map this memory into the SEC process memory space.
284 for (Index
= 0, Done
= FALSE
; !Done
; Index
++) {
286 // Save the size of the memory and make a Unicode filename SystemMemory00, ...
288 gSystemMemory
[Index
].Size
= _wtoi (MemorySizeStr
) * 0x100000;
289 _snwprintf (gSystemMemory
[Index
].FileName
, NT_SYSTEM_MEMORY_FILENAME_SIZE
, L
"SystemMemory%02d", Index
);
292 // Find the next region
294 for (Index1
= 0; MemorySizeStr
[Index1
] != '!' && MemorySizeStr
[Index1
] != 0; Index1
++)
296 if (MemorySizeStr
[Index1
] == 0) {
300 MemorySizeStr
= MemorySizeStr
+ Index1
+ 1;
306 // Hand off to PEI Core
308 SecLoadFromCore ((UINTN
) InitialStackMemory
, (UINTN
) InitialStackMemorySize
, (UINTN
) gFdInfo
[0].Address
, PeiCoreFile
);
311 // If we get here, then the PEI Core returned. This is an error as PEI should
312 // always hand off to DXE.
314 printf ("ERROR : PEI Core returned\n");
322 IN DWORD CreationDisposition
,
323 IN OUT EFI_PHYSICAL_ADDRESS
*BaseAddress
,
329 Opens and memory maps a file using WinNt services. If BaseAddress is non zero
330 the process will try and allocate the memory starting at BaseAddress.
333 FileName - The name of the file to open and map
334 MapSize - The amount of the file to map in bytes
335 CreationDisposition - The flags to pass to CreateFile(). Use to create new files for
336 memory emulation, and exiting files for firmware volume emulation
337 BaseAddress - The base address of the mapped file in the user address space.
338 If passed in as NULL the a new memory region is used.
339 If passed in as non NULL the request memory region is used for
340 the mapping of the file into the process space.
341 Length - The size of the mapped region in bytes
344 EFI_SUCCESS - The file was opened and mapped.
345 EFI_NOT_FOUND - FileName was not found in the current directory
346 EFI_DEVICE_ERROR - An error occured attempting to map the opened file
352 VOID
*VirtualAddress
;
356 // Use Win API to open/create a file
358 NtFileHandle
= CreateFile (
360 GENERIC_READ
| GENERIC_WRITE
,
364 FILE_ATTRIBUTE_NORMAL
,
367 if (NtFileHandle
== INVALID_HANDLE_VALUE
) {
368 return EFI_NOT_FOUND
;
371 // Map the open file into a memory range
373 NtMapHandle
= CreateFileMapping (
381 if (NtMapHandle
== NULL
) {
382 return EFI_DEVICE_ERROR
;
385 // Get the virtual address (address in the emulator) of the mapped file
387 VirtualAddress
= MapViewOfFileEx (
393 (LPVOID
) (UINTN
) *BaseAddress
395 if (VirtualAddress
== NULL
) {
396 return EFI_DEVICE_ERROR
;
401 // Seek to the end of the file to figure out the true file size.
403 FileSize
= SetFilePointer (
409 if (FileSize
== -1) {
410 return EFI_DEVICE_ERROR
;
413 *Length
= (UINT64
) FileSize
;
415 *Length
= (UINT64
) MapSize
;
418 *BaseAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VirtualAddress
;
423 #define BYTES_PER_RECORD 512
427 SecPeiReportStatusCode (
428 IN EFI_PEI_SERVICES
**PeiServices
,
429 IN EFI_STATUS_CODE_TYPE CodeType
,
430 IN EFI_STATUS_CODE_VALUE Value
,
432 IN EFI_GUID
* CallerId
,
433 IN EFI_STATUS_CODE_DATA
* Data OPTIONAL
439 This routine produces the ReportStatusCode PEI service. It's passed
440 up to the PEI Core via a PPI. T
442 This code currently uses the NT clib printf. This does not work the same way
443 as the EFI Print (), as %t, %g, %s as Unicode are not supported.
446 (see EFI_PEI_REPORT_STATUS_CODE)
449 EFI_SUCCESS - Always return success
452 // TODO: PeiServices - add argument and description to function comment
453 // TODO: CodeType - add argument and description to function comment
454 // TODO: Value - add argument and description to function comment
455 // TODO: Instance - add argument and description to function comment
456 // TODO: CallerId - add argument and description to function comment
457 // TODO: Data - add argument and description to function comment
460 EFI_DEBUG_INFO
*DebugInfo
;
462 CHAR8 PrintBuffer
[BYTES_PER_RECORD
* 2];
467 if ((CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_DEBUG_CODE
) {
469 // This supports DEBUG () marcos
471 // EFI_STATUS_CODE_DATA
474 // The first 12 * UINT64 bytes of the string are really an
475 // arguement stack to support varargs on the Format string.
478 DebugInfo
= (EFI_DEBUG_INFO
*) (Data
+ 1);
479 Marker
= (VA_LIST
) (DebugInfo
+ 1);
480 Format
= (CHAR8
*) (((UINT64
*) Marker
) + 12);
482 AsciiVSPrint (PrintBuffer
, BYTES_PER_RECORD
, Format
, Marker
);
483 printf (PrintBuffer
);
485 printf ("DEBUG <null>\n");
489 if (((CodeType
& EFI_STATUS_CODE_TYPE_MASK
) == EFI_ERROR_CODE
) &&
490 ((CodeType
& EFI_STATUS_CODE_SEVERITY_MASK
) == EFI_ERROR_UNRECOVERED
)
492 if (Data
!= NULL
&& ReportStatusCodeExtractAssertInfo (CodeType
, Value
, Data
, &Filename
, &Description
, &LineNumber
)) {
494 // Support ASSERT () macro
496 printf ("ASSERT %s(%d): %s\n", Filename
, LineNumber
, Description
);
498 printf ("ASSERT <null>\n");
509 IN UINTN LargestRegion
,
510 IN UINTN LargestRegionSize
,
511 IN UINTN BootFirmwareVolumeBase
,
512 IN VOID
*PeiCorePe32File
517 This is the service to load the PEI Core from the Firmware Volume
520 LargestRegion - Memory to use for PEI.
521 LargestRegionSize - Size of Memory to use for PEI
522 BootFirmwareVolumeBase - Start of the Boot FV
523 PeiCorePe32File - PEI Core PE32
526 Success means control is transfered and thus we should never return
531 EFI_PHYSICAL_ADDRESS TopOfMemory
;
534 EFI_PHYSICAL_ADDRESS PeiCoreEntryPoint
;
535 EFI_PHYSICAL_ADDRESS PeiImageAddress
;
536 EFI_PEI_STARTUP_DESCRIPTOR
*PeiStartup
;
539 // Compute Top Of Memory for Stack and PEI Core Allocations
541 TopOfMemory
= LargestRegion
+ ((LargestRegionSize
) & (~15));
544 // Allocate 128KB for the Stack
546 TopOfStack
= (VOID
*) (UINTN
) (TopOfMemory
- sizeof (EFI_PEI_STARTUP_DESCRIPTOR
));
547 TopOfMemory
= TopOfMemory
- STACK_SIZE
;
550 // Patch value in dispatch table values
552 gPrivateDispatchTable
[0].Ppi
= gPeiEfiPeiPeCoffLoader
;
555 // Bind this information into the SEC hand-off state
557 PeiStartup
= (EFI_PEI_STARTUP_DESCRIPTOR
*) (UINTN
) TopOfStack
;
558 PeiStartup
->DispatchTable
= (EFI_PEI_PPI_DESCRIPTOR
*) &gPrivateDispatchTable
;
559 PeiStartup
->SizeOfCacheAsRam
= STACK_SIZE
;
560 PeiStartup
->BootFirmwareVolume
= BootFirmwareVolumeBase
;
563 // Load the PEI Core from a Firmware Volume
565 Status
= SecWinNtPeiLoadFile (
571 if (EFI_ERROR (Status
)) {
575 // Transfer control to the PEI Core
578 (SWITCH_STACK_ENTRY_POINT
) (UINTN
) PeiCoreEntryPoint
,
584 // If we get here, then the PEI Core returned. This is an error
591 SecWinNtPeiAutoScan (
593 OUT EFI_PHYSICAL_ADDRESS
*MemoryBase
,
594 OUT UINT64
*MemorySize
599 This service is called from Index == 0 until it returns EFI_UNSUPPORTED.
600 It allows discontiguous memory regions to be supported by the emulator.
601 It uses gSystemMemory[] and gSystemMemoryCount that were created by
602 parsing the Windows environment variable EFI_MEMORY_SIZE.
603 The size comes from the varaible and the address comes from the call to
607 Index - Which memory region to use
608 MemoryBase - Return Base address of memory region
609 MemorySize - Return size in bytes of the memory region
612 EFI_SUCCESS - If memory region was mapped
613 EFI_UNSUPPORTED - If Index is not supported
619 if (Index
>= gSystemMemoryCount
) {
620 return EFI_UNSUPPORTED
;
624 Status
= WinNtOpenFile (
625 gSystemMemory
[Index
].FileName
,
626 (UINT32
) gSystemMemory
[Index
].Size
,
632 gSystemMemory
[Index
].Memory
= *MemoryBase
;
639 SecWinNtWinNtThunkAddress (
645 Since the SEC is the only Windows program in stack it must export
646 an interface to do Win API calls. That's what the WinNtThunk address
647 is for. gWinNt is initailized in WinNtThunk.c.
650 InterfaceSize - sizeof (EFI_WIN_NT_THUNK_PROTOCOL);
651 InterfaceBase - Address of the gWinNt global
654 EFI_SUCCESS - Data returned
664 SecWinNtPeiLoadFile (
666 IN EFI_PHYSICAL_ADDRESS
*ImageAddress
,
667 IN UINT64
*ImageSize
,
668 IN EFI_PHYSICAL_ADDRESS
*EntryPoint
673 Loads and relocates a PE/COFF image into memory.
676 Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated
677 ImageAddress - The base address of the relocated PE/COFF image
678 ImageSize - The size of the relocated PE/COFF image
679 EntryPoint - The entry point of the relocated PE/COFF image
682 EFI_SUCCESS - The file was loaded and relocated
683 EFI_OUT_OF_RESOURCES - There was not enough memory to load and relocate the PE/COFF file
688 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
690 ZeroMem (&ImageContext
, sizeof (ImageContext
));
691 ImageContext
.Handle
= Pe32Data
;
693 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) SecImageRead
;
695 Status
= gPeiEfiPeiPeCoffLoader
->GetImageInfo (gPeiEfiPeiPeCoffLoader
, &ImageContext
);
696 if (EFI_ERROR (Status
)) {
700 // Allocate space in NT (not emulator) memory. Extra space is for alignment
702 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) malloc ((UINTN
) (ImageContext
.ImageSize
+ (ImageContext
.SectionAlignment
* 2)));
703 if (ImageContext
.ImageAddress
== 0) {
704 return EFI_OUT_OF_RESOURCES
;
707 // Align buffer on section boundry
709 ImageContext
.ImageAddress
+= ImageContext
.SectionAlignment
;
710 ImageContext
.ImageAddress
&= ~(ImageContext
.SectionAlignment
- 1);
712 Status
= gPeiEfiPeiPeCoffLoader
->LoadImage (gPeiEfiPeiPeCoffLoader
, &ImageContext
);
713 if (EFI_ERROR (Status
)) {
717 Status
= gPeiEfiPeiPeCoffLoader
->RelocateImage (gPeiEfiPeiPeCoffLoader
, &ImageContext
);
718 if (EFI_ERROR (Status
)) {
723 // BugBug: Flush Instruction Cache Here when CPU Lib is ready
726 *ImageAddress
= ImageContext
.ImageAddress
;
727 *ImageSize
= ImageContext
.ImageSize
;
728 *EntryPoint
= ImageContext
.EntryPoint
;
737 IN OUT EFI_PHYSICAL_ADDRESS
*FdBase
,
738 IN OUT UINT64
*FdSize
743 Return the FD Size and base address. Since the FD is loaded from a
744 file into Windows memory only the SEC will know it's address.
747 Index - Which FD, starts at zero.
748 FdSize - Size of the FD in bytes
749 FdBase - Start address of the FD. Assume it points to an FV Header
752 EFI_SUCCESS - Return the Base address and size of the FV
753 EFI_UNSUPPORTED - Index does nto map to an FD in the system
757 if (Index
>= gFdInfoCount
) {
758 return EFI_UNSUPPORTED
;
761 *FdBase
= gFdInfo
[Index
].Address
;
762 *FdSize
= gFdInfo
[Index
].Size
;
764 if (*FdBase
== 0 && *FdSize
== 0) {
765 return EFI_UNSUPPORTED
;
776 IN OUT UINTN
*ReadSize
,
782 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
785 FileHandle - The handle to the PE/COFF file
786 FileOffset - The offset, in bytes, into the file to read
787 ReadSize - The number of bytes to read from the file starting at FileOffset
788 Buffer - A pointer to the buffer to read the data into.
791 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
799 Destination8
= Buffer
;
800 Source8
= (CHAR8
*) ((UINTN
) FileHandle
+ FileOffset
);
803 *(Destination8
++) = *(Source8
++);
812 IN UINTN
*StrLen OPTIONAL
817 Convert the passed in Ascii string to Unicode.
818 Optionally return the length of the strings.
821 Ascii - Ascii string to convert
822 StrLen - Length of string
825 Pointer to malloc'ed Unicode version of Ascii
833 // Allocate a buffer for unicode string
835 for (Index
= 0; Ascii
[Index
] != '\0'; Index
++)
837 Unicode
= malloc ((Index
+ 1) * sizeof (CHAR16
));
838 if (Unicode
== NULL
) {
842 for (Index
= 0; Ascii
[Index
] != '\0'; Index
++) {
843 Unicode
[Index
] = (CHAR16
) Ascii
[Index
];
846 Unicode
[Index
] = '\0';
848 if (StrLen
!= NULL
) {
856 CountSeperatorsInString (
857 IN
const CHAR16
*String
,
863 Count the number of seperators in String
866 String - String to process
867 Seperator - Item to count
870 Number of Seperator in String
876 for (Count
= 0; *String
!= '\0'; String
++) {
877 if (*String
== Seperator
) {
888 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
894 Store the ModHandle in an array indexed by the Pdb File name.
895 The ModHandle is needed to unload the image.
898 ImageContext - Input data returned from PE Laoder Library. Used to find the
899 .PDB file name of the PE Image.
900 ModHandle - Returned from LoadLibraryEx() and stored for call to
904 EFI_SUCCESS - ModHandle was stored.
909 PDB_NAME_TO_MOD_HANDLE
*Array
;
913 Array
= mPdbNameModHandleArray
;
914 for (Index
= 0; Index
< mPdbNameModHandleArraySize
; Index
++, Array
++) {
915 if (Array
->PdbPointer
== NULL
) {
917 // Make a copy of the stirng and store the ModHandle
919 Array
->PdbPointer
= malloc (strlen (ImageContext
->PdbPointer
) + 1);
920 ASSERT (Array
->PdbPointer
!= NULL
);
922 strcpy (Array
->PdbPointer
, ImageContext
->PdbPointer
);
923 Array
->ModHandle
= ModHandle
;
929 // No free space in mPdbNameModHandleArray so grow it by
930 // MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE entires. realloc will
931 // copy the old values to the new locaiton. But it does
932 // not zero the new memory area.
934 PreviousSize
= mPdbNameModHandleArraySize
* sizeof (PDB_NAME_TO_MOD_HANDLE
);
935 mPdbNameModHandleArraySize
+= MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE
;
937 mPdbNameModHandleArray
= realloc (mPdbNameModHandleArray
, mPdbNameModHandleArraySize
* sizeof (PDB_NAME_TO_MOD_HANDLE
));
938 if (mPdbNameModHandleArray
== NULL
) {
940 return EFI_OUT_OF_RESOURCES
;
943 memset (mPdbNameModHandleArray
+ PreviousSize
, 0, MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE
* sizeof (PDB_NAME_TO_MOD_HANDLE
));
945 return AddModHandle (ImageContext
, ModHandle
);
951 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
956 Return the ModHandle and delete the entry in the array.
959 ImageContext - Input data returned from PE Laoder Library. Used to find the
960 .PDB file name of the PE Image.
963 ModHandle - ModHandle assoicated with ImageContext is returned
964 NULL - No ModHandle associated with ImageContext
969 PDB_NAME_TO_MOD_HANDLE
*Array
;
971 if (ImageContext
->PdbPointer
== NULL
) {
973 // If no PDB pointer there is no ModHandle so return NULL
978 Array
= mPdbNameModHandleArray
;
979 for (Index
= 0; Index
< mPdbNameModHandleArraySize
; Index
++, Array
++) {
980 if ((Array
->PdbPointer
!= NULL
) && (strcmp(Array
->PdbPointer
, ImageContext
->PdbPointer
) == 0)) {
982 // If you find a match return it and delete the entry
984 free (Array
->PdbPointer
);
985 Array
->PdbPointer
= NULL
;
986 return Array
->ModHandle
;
997 SecNt32PeCoffGetImageInfo (
998 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL
*This
,
999 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1004 Status
= PeCoffLoaderGetImageInfo (ImageContext
);
1005 if (EFI_ERROR (Status
)) {
1009 switch (ImageContext
->ImageType
) {
1011 case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
:
1012 ImageContext
->ImageCodeMemoryType
= EfiLoaderCode
;
1013 ImageContext
->ImageDataMemoryType
= EfiLoaderData
;
1016 case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
:
1017 ImageContext
->ImageCodeMemoryType
= EfiBootServicesCode
;
1018 ImageContext
->ImageDataMemoryType
= EfiBootServicesData
;
1021 case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
:
1022 case EFI_IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER
:
1023 ImageContext
->ImageCodeMemoryType
= EfiRuntimeServicesCode
;
1024 ImageContext
->ImageDataMemoryType
= EfiRuntimeServicesData
;
1028 ImageContext
->ImageError
= IMAGE_ERROR_INVALID_SUBSYSTEM
;
1029 return RETURN_UNSUPPORTED
;
1037 SecNt32PeCoffLoadImage (
1038 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL
*This
,
1039 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1044 Status
= PeCoffLoaderLoadImage (ImageContext
);
1050 SecNt32PeCoffRelocateImage (
1051 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL
*This
,
1052 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1056 VOID
*DllEntryPoint
;
1057 CHAR16
*DllFileName
;
1062 Status
= PeCoffLoaderRelocateImage (ImageContext
);
1063 if (EFI_ERROR (Status
)) {
1065 // We could not relocated the image in memory properly
1071 // If we load our own PE COFF images the Windows debugger can not source
1072 // level debug our code. If a valid PDB pointer exists usw it to load
1073 // the *.dll file as a library using Windows* APIs. This allows
1074 // source level debug. The image is still loaded and reloaced
1075 // in the Framework memory space like on a real system (by the code above),
1076 // but the entry point points into the DLL loaded by the code bellow.
1079 DllEntryPoint
= NULL
;
1082 // Load the DLL if it's not an EBC image.
1084 if ((ImageContext
->PdbPointer
!= NULL
) &&
1085 (ImageContext
->Machine
!= EFI_IMAGE_MACHINE_EBC
)) {
1087 // Convert filename from ASCII to Unicode
1089 DllFileName
= AsciiToUnicode (ImageContext
->PdbPointer
, &Index
);
1092 // Check that we have a valid filename
1094 if (Index
< 5 || DllFileName
[Index
- 4] != '.') {
1098 // Never return an error if PeCoffLoaderRelocateImage() succeeded.
1099 // The image will run, but we just can't source level debug. If we
1100 // return an error the image will not run.
1105 // Replace .PDB with .DLL on the filename
1107 DllFileName
[Index
- 3] = 'D';
1108 DllFileName
[Index
- 2] = 'L';
1109 DllFileName
[Index
- 1] = 'L';
1112 // Load the .DLL file into the user process's address space for source
1115 Library
= LoadLibraryEx (DllFileName
, NULL
, DONT_RESOLVE_DLL_REFERENCES
);
1116 if (Library
!= NULL
) {
1118 // InitializeDriver is the entry point we put in all our EFI DLL's. The
1119 // DONT_RESOLVE_DLL_REFERENCES argument to LoadLIbraryEx() supresses the
1120 // normal DLL entry point of DllMain, and prevents other modules that are
1121 // referenced in side the DllFileName from being loaded. There is no error
1122 // checking as the we can point to the PE32 image loaded by Tiano. This
1123 // step is only needed for source level debuging
1125 DllEntryPoint
= (VOID
*) (UINTN
) GetProcAddress (Library
, "InitializeDriver");
1129 if ((Library
!= NULL
) && (DllEntryPoint
!= NULL
)) {
1130 AddModHandle (ImageContext
, Library
);
1131 ImageContext
->EntryPoint
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) DllEntryPoint
;
1132 wprintf (L
"LoadLibraryEx (%s,\n NULL, DONT_RESOLVE_DLL_REFERENCES)\n", DllFileName
);
1134 wprintf (L
"WARNING: No source level debug %s. \n", DllFileName
);
1141 // Never return an error if PeCoffLoaderRelocateImage() succeeded.
1142 // The image will run, but we just can't source level debug. If we
1143 // return an error the image will not run.
1151 SecNt32PeCoffUnloadimage (
1152 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL
*This
,
1153 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1158 ModHandle
= RemoveModeHandle (ImageContext
);
1159 if (ModHandle
!= NULL
) {
1160 FreeLibrary (ModHandle
);
1172 #pragma warning(default : 4996)