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 gets NT32 PCD setting and figures out what the memory layout
21 will be, how may FD's will be loaded and also what the boot mode is.
23 The SEC registers a set of services with the SEC core. gPrivateDispatchTable
24 is a list of PPI's produced by the SEC that are availble for usage in PEI.
26 This code produces 128 K of temporary memory for the PEI stack by directly
27 allocate memory space with ReadWrite and Execute attribute.
37 EFI_PEI_PE_COFF_LOADER_PROTOCOL_INSTANCE mPeiEfiPeiPeCoffLoaderInstance
= {
39 SecNt32PeCoffGetImageInfo
,
40 SecNt32PeCoffLoadImage
,
41 SecNt32PeCoffRelocateImage
,
42 SecNt32PeCoffUnloadimage
49 EFI_PEI_PE_COFF_LOADER_PROTOCOL
*gPeiEfiPeiPeCoffLoader
= &mPeiEfiPeiPeCoffLoaderInstance
.PeCoff
;
51 NT_PEI_LOAD_FILE_PPI mSecNtLoadFilePpi
= { SecWinNtPeiLoadFile
};
53 PEI_NT_AUTOSCAN_PPI mSecNtAutoScanPpi
= { SecWinNtPeiAutoScan
};
55 PEI_NT_THUNK_PPI mSecWinNtThunkPpi
= { SecWinNtWinNtThunkAddress
};
57 EFI_PEI_PROGRESS_CODE_PPI mSecStatusCodePpi
= { SecPeiReportStatusCode
};
59 NT_FWH_PPI mSecFwhInformationPpi
= { SecWinNtFdAddress
};
61 TEMPORARY_RAM_SUPPORT_PPI mSecTemporaryRamSupportPpi
= {SecTemporaryRamSupport
};
63 EFI_PEI_PPI_DESCRIPTOR gPrivateDispatchTable
[] = {
65 EFI_PEI_PPI_DESCRIPTOR_PPI
,
66 &gEfiPeiPeCoffLoaderGuid
,
70 EFI_PEI_PPI_DESCRIPTOR_PPI
,
71 &gNtPeiLoadFilePpiGuid
,
75 EFI_PEI_PPI_DESCRIPTOR_PPI
,
76 &gPeiNtAutoScanPpiGuid
,
80 EFI_PEI_PPI_DESCRIPTOR_PPI
,
85 EFI_PEI_PPI_DESCRIPTOR_PPI
,
86 &gEfiPeiStatusCodePpiGuid
,
90 EFI_PEI_PPI_DESCRIPTOR_PPI
,
91 &gEfiTemporaryRamSupportPpiGuid
,
92 &mSecTemporaryRamSupportPpi
95 EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
,
97 &mSecFwhInformationPpi
103 // Default information about where the FD is located.
104 // This array gets filled in with information from PcdWinNtFirmwareVolume
105 // The number of array elements is allocated base on parsing
106 // PcdWinNtFirmwareVolume and the memory is never freed.
108 UINTN gFdInfoCount
= 0;
112 // Array that supports seperate memory rantes.
113 // The memory ranges are set by PcdWinNtMemorySizeForSecMain.
114 // The number of array elements is allocated base on parsing
115 // PcdWinNtMemorySizeForSecMain value and the memory is never freed.
117 UINTN gSystemMemoryCount
= 0;
118 NT_SYSTEM_MEMORY
*gSystemMemory
;
121 UINTN mPdbNameModHandleArraySize
= 0;
122 PDB_NAME_TO_MOD_HANDLE
*mPdbNameModHandleArray
= NULL
;
128 UINT32 TemporaryMemoryBase
,
129 UINT32 PermenentMemoryBase
142 Main entry point to SEC for WinNt. This is a Windows program
145 Argc - Number of command line arguments
146 Argv - Array of command line argument strings
147 Envp - Array of environmemt variable strings
156 EFI_PHYSICAL_ADDRESS InitialStackMemory
;
157 UINT64 InitialStackMemorySize
;
165 CHAR16
*MemorySizeStr
;
166 CHAR16
*FirmwareVolumesStr
;
169 MemorySizeStr
= (CHAR16
*) FixedPcdGetPtr (PcdWinNtMemorySizeForSecMain
);
170 FirmwareVolumesStr
= (CHAR16
*) FixedPcdGetPtr (PcdWinNtFirmwareVolume
);
172 printf ("\nEDK SEC Main NT Emulation Environment from www.TianoCore.org\n");
175 // Make some Windows calls to Set the process to the highest priority in the
176 // idle class. We need this to have good performance.
178 SetPriorityClass (GetCurrentProcess (), IDLE_PRIORITY_CLASS
);
179 SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_HIGHEST
);
182 // Allocate space for gSystemMemory Array
184 gSystemMemoryCount
= CountSeperatorsInString (MemorySizeStr
, '!') + 1;
185 gSystemMemory
= calloc (gSystemMemoryCount
, sizeof (NT_SYSTEM_MEMORY
));
186 if (gSystemMemory
== NULL
) {
187 wprintf (L
"ERROR : Can not allocate memory for %s. Exiting.\n", MemorySizeStr
);
191 // Allocate space for gSystemMemory Array
193 gFdInfoCount
= CountSeperatorsInString (FirmwareVolumesStr
, '!') + 1;
194 gFdInfo
= calloc (gFdInfoCount
, sizeof (NT_FD_INFO
));
195 if (gFdInfo
== NULL
) {
196 wprintf (L
"ERROR : Can not allocate memory for %s. Exiting.\n", FirmwareVolumesStr
);
200 // Setup Boot Mode. If BootModeStr == "" then BootMode = 0 (BOOT_WITH_FULL_CONFIGURATION)
202 printf (" BootMode 0x%02x\n", FixedPcdGet32 (PcdWinNtBootMode
));
205 // Allocate 128K memory to emulate temp memory for PEI.
206 // on a real platform this would be SRAM, or using the cache as RAM.
207 // Set InitialStackMemory to zero so WinNtOpenFile will allocate a new mapping
209 InitialStackMemorySize
= STACK_SIZE
;
210 InitialStackMemory
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VirtualAlloc (NULL
, (SIZE_T
) (InitialStackMemorySize
), MEM_COMMIT
, PAGE_EXECUTE_READWRITE
);
211 if (InitialStackMemory
== 0) {
212 printf ("ERROR : Can not allocate enough space for SecStack\n");
216 for (StackPointer
= (UINTN
*) (UINTN
) InitialStackMemory
;
217 StackPointer
< (UINTN
*) ((UINTN
)InitialStackMemory
+ (SIZE_T
) InitialStackMemorySize
);
219 *StackPointer
= 0x5AA55AA5;
222 wprintf (L
" SEC passing in %d bytes of temp RAM to PEI\n", InitialStackMemorySize
);
225 // Open All the firmware volumes and remember the info in the gFdInfo global
227 FileNamePtr
= (CHAR16
*)malloc (StrLen ((CHAR16
*)FirmwareVolumesStr
) * sizeof(CHAR16
));
228 if (FileNamePtr
== NULL
) {
229 printf ("ERROR : Can not allocate memory for firmware volume string\n");
233 StrCpy (FileNamePtr
, (CHAR16
*)FirmwareVolumesStr
);
235 for (Done
= FALSE
, Index
= 0, PeiCoreFile
= NULL
; !Done
; Index
++) {
236 FileName
= FileNamePtr
;
237 for (Index1
= 0; (FileNamePtr
[Index1
] != '!') && (FileNamePtr
[Index1
] != 0); Index1
++)
239 if (FileNamePtr
[Index1
] == 0) {
242 FileNamePtr
[Index1
] = '\0';
243 FileNamePtr
= FileNamePtr
+ Index1
+ 1;
247 // Open the FD and remmeber where it got mapped into our processes address space
249 Status
= WinNtOpenFile (
253 &gFdInfo
[Index
].Address
,
256 if (EFI_ERROR (Status
)) {
257 printf ("ERROR : Can not open Firmware Device File %S (0x%X). Exiting.\n", FileName
, Status
);
261 printf (" FD loaded from");
263 // printf can't print filenames directly as the \ gets interperted as an
266 for (Index2
= 0; FileName
[Index2
] != '\0'; Index2
++) {
267 printf ("%c", FileName
[Index2
]);
270 if (PeiCoreFile
== NULL
) {
272 // Assume the beginning of the FD is an FV and look for the PEI Core.
273 // Load the first one we find.
275 Status
= SecFfsFindPeiCore ((EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) gFdInfo
[Index
].Address
, &PeiCoreFile
);
276 if (!EFI_ERROR (Status
)) {
277 printf (" contains SEC Core");
284 // Calculate memory regions and store the information in the gSystemMemory
285 // global for later use. The autosizing code will use this data to
286 // map this memory into the SEC process memory space.
288 for (Index
= 0, Done
= FALSE
; !Done
; Index
++) {
290 // Save the size of the memory and make a Unicode filename SystemMemory00, ...
292 gSystemMemory
[Index
].Size
= _wtoi (MemorySizeStr
) * 0x100000;
295 // Find the next region
297 for (Index1
= 0; MemorySizeStr
[Index1
] != '!' && MemorySizeStr
[Index1
] != 0; Index1
++)
299 if (MemorySizeStr
[Index1
] == 0) {
303 MemorySizeStr
= MemorySizeStr
+ Index1
+ 1;
309 // Hand off to PEI Core
311 SecLoadFromCore ((UINTN
) InitialStackMemory
, (UINTN
) InitialStackMemorySize
, (UINTN
) gFdInfo
[0].Address
, PeiCoreFile
);
314 // If we get here, then the PEI Core returned. This is an error as PEI should
315 // always hand off to DXE.
317 printf ("ERROR : PEI Core returned\n");
325 IN DWORD CreationDisposition
,
326 IN OUT EFI_PHYSICAL_ADDRESS
*BaseAddress
,
332 Opens and memory maps a file using WinNt services. If BaseAddress is non zero
333 the process will try and allocate the memory starting at BaseAddress.
336 FileName - The name of the file to open and map
337 MapSize - The amount of the file to map in bytes
338 CreationDisposition - The flags to pass to CreateFile(). Use to create new files for
339 memory emulation, and exiting files for firmware volume emulation
340 BaseAddress - The base address of the mapped file in the user address space.
341 If passed in as NULL the a new memory region is used.
342 If passed in as non NULL the request memory region is used for
343 the mapping of the file into the process space.
344 Length - The size of the mapped region in bytes
347 EFI_SUCCESS - The file was opened and mapped.
348 EFI_NOT_FOUND - FileName was not found in the current directory
349 EFI_DEVICE_ERROR - An error occured attempting to map the opened file
355 VOID
*VirtualAddress
;
359 // Use Win API to open/create a file
361 NtFileHandle
= CreateFile (
363 GENERIC_READ
| GENERIC_WRITE
,
367 FILE_ATTRIBUTE_NORMAL
,
370 if (NtFileHandle
== INVALID_HANDLE_VALUE
) {
371 return EFI_NOT_FOUND
;
374 // Map the open file into a memory range
376 NtMapHandle
= CreateFileMapping (
384 if (NtMapHandle
== NULL
) {
385 return EFI_DEVICE_ERROR
;
388 // Get the virtual address (address in the emulator) of the mapped file
390 VirtualAddress
= MapViewOfFileEx (
396 (LPVOID
) (UINTN
) *BaseAddress
398 if (VirtualAddress
== NULL
) {
399 return EFI_DEVICE_ERROR
;
404 // Seek to the end of the file to figure out the true file size.
406 FileSize
= SetFilePointer (
412 if (FileSize
== -1) {
413 return EFI_DEVICE_ERROR
;
416 *Length
= (UINT64
) FileSize
;
418 *Length
= (UINT64
) MapSize
;
421 *BaseAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VirtualAddress
;
427 #define BYTES_PER_RECORD 512
431 SecPeiReportStatusCode (
432 IN CONST EFI_PEI_SERVICES
**PeiServices
,
433 IN EFI_STATUS_CODE_TYPE CodeType
,
434 IN EFI_STATUS_CODE_VALUE Value
,
436 IN CONST EFI_GUID
*CallerId
,
437 IN CONST EFI_STATUS_CODE_DATA
*Data OPTIONAL
443 This routine produces the ReportStatusCode PEI service. It's passed
444 up to the PEI Core via a PPI. T
446 This code currently uses the NT clib printf. This does not work the same way
447 as the EFI Print (), as %t, %g, %s as Unicode are not supported.
450 (see EFI_PEI_REPORT_STATUS_CODE)
453 EFI_SUCCESS - Always return success
456 // TODO: PeiServices - add argument and description to function comment
457 // TODO: CodeType - add argument and description to function comment
458 // TODO: Value - add argument and description to function comment
459 // TODO: Instance - add argument and description to function comment
460 // TODO: CallerId - add argument and description to function comment
461 // TODO: Data - add argument and description to function comment
465 CHAR8 PrintBuffer
[BYTES_PER_RECORD
* 2];
473 } else if (ReportStatusCodeExtractAssertInfo (CodeType
, Value
, Data
, &Filename
, &Description
, &LineNumber
)) {
475 // Processes ASSERT ()
477 printf ("ASSERT %s(%d): %s\n", Filename
, (int)LineNumber
, Description
);
479 } else if (ReportStatusCodeExtractDebugInfo (Data
, &ErrorLevel
, &Marker
, &Format
)) {
481 // Process DEBUG () macro
483 AsciiVSPrint (PrintBuffer
, BYTES_PER_RECORD
, Format
, Marker
);
484 printf (PrintBuffer
);
491 Transfers control to a function starting with a new stack.
493 Transfers control to the function specified by EntryPoint using the new stack
494 specified by NewStack and passing in the parameters specified by Context1 and
495 Context2. Context1 and Context2 are optional and may be NULL. The function
496 EntryPoint must never return.
498 If EntryPoint is NULL, then ASSERT().
499 If NewStack is NULL, then ASSERT().
501 @param EntryPoint A pointer to function to call with the new stack.
502 @param Context1 A pointer to the context to pass into the EntryPoint
504 @param Context2 A pointer to the context to pass into the EntryPoint
506 @param NewStack A pointer to the new stack to use for the EntryPoint
508 @param NewBsp A pointer to the new BSP for the EntryPoint on IPF. It's
509 Reserved on other architectures.
515 IN SWITCH_STACK_ENTRY_POINT EntryPoint
,
516 IN VOID
*Context1
, OPTIONAL
517 IN VOID
*Context2
, OPTIONAL
518 IN VOID
*Context3
, OPTIONAL
522 BASE_LIBRARY_JUMP_BUFFER JumpBuffer
;
524 ASSERT (EntryPoint
!= NULL
);
525 ASSERT (NewStack
!= NULL
);
528 // Stack should be aligned with CPU_STACK_ALIGNMENT
530 ASSERT (((UINTN
)NewStack
& (CPU_STACK_ALIGNMENT
- 1)) == 0);
532 JumpBuffer
.Eip
= (UINTN
)EntryPoint
;
533 JumpBuffer
.Esp
= (UINTN
)NewStack
- sizeof (VOID
*);
534 JumpBuffer
.Esp
-= sizeof (Context1
) + sizeof (Context2
) + sizeof(Context3
);
535 ((VOID
**)JumpBuffer
.Esp
)[1] = Context1
;
536 ((VOID
**)JumpBuffer
.Esp
)[2] = Context2
;
537 ((VOID
**)JumpBuffer
.Esp
)[3] = Context3
;
539 LongJump (&JumpBuffer
, (UINTN
)-1);
543 // InternalSwitchStack () will never return
550 IN UINTN LargestRegion
,
551 IN UINTN LargestRegionSize
,
552 IN UINTN BootFirmwareVolumeBase
,
553 IN VOID
*PeiCorePe32File
558 This is the service to load the PEI Core from the Firmware Volume
561 LargestRegion - Memory to use for PEI.
562 LargestRegionSize - Size of Memory to use for PEI
563 BootFirmwareVolumeBase - Start of the Boot FV
564 PeiCorePe32File - PEI Core PE32
567 Success means control is transfered and thus we should never return
574 EFI_PHYSICAL_ADDRESS PeiCoreEntryPoint
;
575 EFI_PHYSICAL_ADDRESS PeiImageAddress
;
576 EFI_SEC_PEI_HAND_OFF
*SecCoreData
;
580 // Compute Top Of Memory for Stack and PEI Core Allocations
582 PeiStackSize
= (UINTN
)RShiftU64((UINT64
)STACK_SIZE
,1);
585 // |-----------| <---- TemporaryRamBase + TemporaryRamSize
588 // |-----------| <---- StackBase / PeiTemporaryMemoryBase
591 // |-----------| <---- TemporaryRamBase
593 TopOfStack
= (VOID
*)(LargestRegion
+ PeiStackSize
);
596 // Reservet space for storing PeiCore's parament in stack.
598 TopOfStack
= (VOID
*)((UINTN
)TopOfStack
- sizeof (EFI_SEC_PEI_HAND_OFF
) - CPU_STACK_ALIGNMENT
);
599 TopOfStack
= ALIGN_POINTER (TopOfStack
, CPU_STACK_ALIGNMENT
);
602 // Patch value in dispatch table values
604 gPrivateDispatchTable
[0].Ppi
= gPeiEfiPeiPeCoffLoader
;
607 // Bind this information into the SEC hand-off state
609 SecCoreData
= (EFI_SEC_PEI_HAND_OFF
*)(UINTN
) TopOfStack
;
610 SecCoreData
->DataSize
= sizeof(EFI_SEC_PEI_HAND_OFF
);
611 SecCoreData
->BootFirmwareVolumeBase
= (VOID
*)BootFirmwareVolumeBase
;
612 SecCoreData
->BootFirmwareVolumeSize
= FixedPcdGet32(PcdWinNtFirmwareFdSize
);
613 SecCoreData
->TemporaryRamBase
= (VOID
*)(UINTN
)LargestRegion
;
614 SecCoreData
->TemporaryRamSize
= STACK_SIZE
;
615 SecCoreData
->StackBase
= SecCoreData
->TemporaryRamBase
;
616 SecCoreData
->StackSize
= PeiStackSize
;
617 SecCoreData
->PeiTemporaryRamBase
= (VOID
*) ((UINTN
) SecCoreData
->TemporaryRamBase
+ PeiStackSize
);
618 SecCoreData
->PeiTemporaryRamSize
= STACK_SIZE
- PeiStackSize
;
621 // Load the PEI Core from a Firmware Volume
623 Status
= SecWinNtPeiLoadFile (
629 if (EFI_ERROR (Status
)) {
634 // Transfer control to the PEI Core
637 (SWITCH_STACK_ENTRY_POINT
) (UINTN
) PeiCoreEntryPoint
,
639 (VOID
*) (UINTN
) ((EFI_PEI_PPI_DESCRIPTOR
*) &gPrivateDispatchTable
),
644 // If we get here, then the PEI Core returned. This is an error
651 SecWinNtPeiAutoScan (
653 OUT EFI_PHYSICAL_ADDRESS
*MemoryBase
,
654 OUT UINT64
*MemorySize
659 This service is called from Index == 0 until it returns EFI_UNSUPPORTED.
660 It allows discontiguous memory regions to be supported by the emulator.
661 It uses gSystemMemory[] and gSystemMemoryCount that were created by
662 parsing PcdWinNtMemorySizeForSecMain value.
663 The size comes from the Pcd value and the address comes from the memory space
664 with ReadWrite and Execute attributes allocated by VirtualAlloc() API.
667 Index - Which memory region to use
668 MemoryBase - Return Base address of memory region
669 MemorySize - Return size in bytes of the memory region
672 EFI_SUCCESS - If memory region was mapped
673 EFI_UNSUPPORTED - If Index is not supported
677 if (Index
>= gSystemMemoryCount
) {
678 return EFI_UNSUPPORTED
;
682 // Allocate enough memory space for emulator
684 gSystemMemory
[Index
].Memory
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VirtualAlloc (NULL
, (SIZE_T
) (gSystemMemory
[Index
].Size
), MEM_COMMIT
, PAGE_EXECUTE_READWRITE
);
685 if (gSystemMemory
[Index
].Memory
== 0) {
686 return EFI_OUT_OF_RESOURCES
;
689 *MemoryBase
= gSystemMemory
[Index
].Memory
;
690 *MemorySize
= gSystemMemory
[Index
].Size
;
697 SecWinNtWinNtThunkAddress (
703 Since the SEC is the only Windows program in stack it must export
704 an interface to do Win API calls. That's what the WinNtThunk address
705 is for. gWinNt is initailized in WinNtThunk.c.
708 InterfaceSize - sizeof (EFI_WIN_NT_THUNK_PROTOCOL);
709 InterfaceBase - Address of the gWinNt global
712 EFI_SUCCESS - Data returned
722 SecWinNtPeiLoadFile (
724 IN EFI_PHYSICAL_ADDRESS
*ImageAddress
,
725 IN UINT64
*ImageSize
,
726 IN EFI_PHYSICAL_ADDRESS
*EntryPoint
731 Loads and relocates a PE/COFF image into memory.
734 Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated
735 ImageAddress - The base address of the relocated PE/COFF image
736 ImageSize - The size of the relocated PE/COFF image
737 EntryPoint - The entry point of the relocated PE/COFF image
740 EFI_SUCCESS - The file was loaded and relocated
741 EFI_OUT_OF_RESOURCES - There was not enough memory to load and relocate the PE/COFF file
746 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
748 ZeroMem (&ImageContext
, sizeof (ImageContext
));
749 ImageContext
.Handle
= Pe32Data
;
751 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) SecImageRead
;
753 Status
= gPeiEfiPeiPeCoffLoader
->GetImageInfo (gPeiEfiPeiPeCoffLoader
, &ImageContext
);
754 if (EFI_ERROR (Status
)) {
758 // Allocate space in NT (not emulator) memory with ReadWrite and Execute attribue.
759 // Extra space is for alignment
761 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VirtualAlloc (NULL
, (SIZE_T
) (ImageContext
.ImageSize
+ (ImageContext
.SectionAlignment
* 2)), MEM_COMMIT
, PAGE_EXECUTE_READWRITE
);
762 if (ImageContext
.ImageAddress
== 0) {
763 return EFI_OUT_OF_RESOURCES
;
766 // Align buffer on section boundry
768 ImageContext
.ImageAddress
+= ImageContext
.SectionAlignment
;
769 ImageContext
.ImageAddress
&= ~(ImageContext
.SectionAlignment
- 1);
771 Status
= gPeiEfiPeiPeCoffLoader
->LoadImage (gPeiEfiPeiPeCoffLoader
, &ImageContext
);
772 if (EFI_ERROR (Status
)) {
776 Status
= gPeiEfiPeiPeCoffLoader
->RelocateImage (gPeiEfiPeiPeCoffLoader
, &ImageContext
);
777 if (EFI_ERROR (Status
)) {
782 // BugBug: Flush Instruction Cache Here when CPU Lib is ready
785 *ImageAddress
= ImageContext
.ImageAddress
;
786 *ImageSize
= ImageContext
.ImageSize
;
787 *EntryPoint
= ImageContext
.EntryPoint
;
796 IN OUT EFI_PHYSICAL_ADDRESS
*FdBase
,
797 IN OUT UINT64
*FdSize
802 Return the FD Size and base address. Since the FD is loaded from a
803 file into Windows memory only the SEC will know it's address.
806 Index - Which FD, starts at zero.
807 FdSize - Size of the FD in bytes
808 FdBase - Start address of the FD. Assume it points to an FV Header
811 EFI_SUCCESS - Return the Base address and size of the FV
812 EFI_UNSUPPORTED - Index does nto map to an FD in the system
816 if (Index
>= gFdInfoCount
) {
817 return EFI_UNSUPPORTED
;
820 *FdBase
= gFdInfo
[Index
].Address
;
821 *FdSize
= gFdInfo
[Index
].Size
;
823 if (*FdBase
== 0 && *FdSize
== 0) {
824 return EFI_UNSUPPORTED
;
835 IN OUT UINTN
*ReadSize
,
841 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
844 FileHandle - The handle to the PE/COFF file
845 FileOffset - The offset, in bytes, into the file to read
846 ReadSize - The number of bytes to read from the file starting at FileOffset
847 Buffer - A pointer to the buffer to read the data into.
850 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
858 Destination8
= Buffer
;
859 Source8
= (CHAR8
*) ((UINTN
) FileHandle
+ FileOffset
);
862 *(Destination8
++) = *(Source8
++);
871 IN UINTN
*StrLen OPTIONAL
876 Convert the passed in Ascii string to Unicode.
877 Optionally return the length of the strings.
880 Ascii - Ascii string to convert
881 StrLen - Length of string
884 Pointer to malloc'ed Unicode version of Ascii
892 // Allocate a buffer for unicode string
894 for (Index
= 0; Ascii
[Index
] != '\0'; Index
++)
896 Unicode
= malloc ((Index
+ 1) * sizeof (CHAR16
));
897 if (Unicode
== NULL
) {
901 for (Index
= 0; Ascii
[Index
] != '\0'; Index
++) {
902 Unicode
[Index
] = (CHAR16
) Ascii
[Index
];
905 Unicode
[Index
] = '\0';
907 if (StrLen
!= NULL
) {
915 CountSeperatorsInString (
916 IN CONST CHAR16
*String
,
922 Count the number of seperators in String
925 String - String to process
926 Seperator - Item to count
929 Number of Seperator in String
935 for (Count
= 0; *String
!= '\0'; String
++) {
936 if (*String
== Seperator
) {
947 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
953 Store the ModHandle in an array indexed by the Pdb File name.
954 The ModHandle is needed to unload the image.
957 ImageContext - Input data returned from PE Laoder Library. Used to find the
958 .PDB file name of the PE Image.
959 ModHandle - Returned from LoadLibraryEx() and stored for call to
963 EFI_SUCCESS - ModHandle was stored.
968 PDB_NAME_TO_MOD_HANDLE
*Array
;
972 Array
= mPdbNameModHandleArray
;
973 for (Index
= 0; Index
< mPdbNameModHandleArraySize
; Index
++, Array
++) {
974 if (Array
->PdbPointer
== NULL
) {
976 // Make a copy of the stirng and store the ModHandle
978 Array
->PdbPointer
= malloc (strlen (ImageContext
->PdbPointer
) + 1);
979 ASSERT (Array
->PdbPointer
!= NULL
);
981 strcpy (Array
->PdbPointer
, ImageContext
->PdbPointer
);
982 Array
->ModHandle
= ModHandle
;
988 // No free space in mPdbNameModHandleArray so grow it by
989 // MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE entires. realloc will
990 // copy the old values to the new locaiton. But it does
991 // not zero the new memory area.
993 PreviousSize
= mPdbNameModHandleArraySize
* sizeof (PDB_NAME_TO_MOD_HANDLE
);
994 mPdbNameModHandleArraySize
+= MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE
;
996 mPdbNameModHandleArray
= realloc (mPdbNameModHandleArray
, mPdbNameModHandleArraySize
* sizeof (PDB_NAME_TO_MOD_HANDLE
));
997 if (mPdbNameModHandleArray
== NULL
) {
999 return EFI_OUT_OF_RESOURCES
;
1002 memset (mPdbNameModHandleArray
+ PreviousSize
, 0, MAX_PDB_NAME_TO_MOD_HANDLE_ARRAY_SIZE
* sizeof (PDB_NAME_TO_MOD_HANDLE
));
1004 return AddModHandle (ImageContext
, ModHandle
);
1010 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1014 Routine Description:
1015 Return the ModHandle and delete the entry in the array.
1018 ImageContext - Input data returned from PE Laoder Library. Used to find the
1019 .PDB file name of the PE Image.
1022 ModHandle - ModHandle assoicated with ImageContext is returned
1023 NULL - No ModHandle associated with ImageContext
1028 PDB_NAME_TO_MOD_HANDLE
*Array
;
1030 if (ImageContext
->PdbPointer
== NULL
) {
1032 // If no PDB pointer there is no ModHandle so return NULL
1037 Array
= mPdbNameModHandleArray
;
1038 for (Index
= 0; Index
< mPdbNameModHandleArraySize
; Index
++, Array
++) {
1039 if ((Array
->PdbPointer
!= NULL
) && (strcmp(Array
->PdbPointer
, ImageContext
->PdbPointer
) == 0)) {
1041 // If you find a match return it and delete the entry
1043 free (Array
->PdbPointer
);
1044 Array
->PdbPointer
= NULL
;
1045 return Array
->ModHandle
;
1056 SecNt32PeCoffGetImageInfo (
1057 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL
*This
,
1058 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1061 return PeCoffLoaderGetImageInfo (ImageContext
);
1066 SecNt32PeCoffLoadImage (
1067 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL
*This
,
1068 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1071 return PeCoffLoaderLoadImage (ImageContext
);
1076 SecNt32PeCoffRelocateImage (
1077 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL
*This
,
1078 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1082 VOID
*DllEntryPoint
;
1083 CHAR16
*DllFileName
;
1088 Status
= PeCoffLoaderRelocateImage (ImageContext
);
1089 if (EFI_ERROR (Status
)) {
1091 // We could not relocated the image in memory properly
1097 // If we load our own PE COFF images the Windows debugger can not source
1098 // level debug our code. If a valid PDB pointer exists usw it to load
1099 // the *.dll file as a library using Windows* APIs. This allows
1100 // source level debug. The image is still loaded and reloaced
1101 // in the Framework memory space like on a real system (by the code above),
1102 // but the entry point points into the DLL loaded by the code bellow.
1105 DllEntryPoint
= NULL
;
1108 // Load the DLL if it's not an EBC image.
1110 if ((ImageContext
->PdbPointer
!= NULL
) &&
1111 (ImageContext
->Machine
!= EFI_IMAGE_MACHINE_EBC
)) {
1113 // Convert filename from ASCII to Unicode
1115 DllFileName
= AsciiToUnicode (ImageContext
->PdbPointer
, &Index
);
1118 // Check that we have a valid filename
1120 if (Index
< 5 || DllFileName
[Index
- 4] != '.') {
1124 // Never return an error if PeCoffLoaderRelocateImage() succeeded.
1125 // The image will run, but we just can't source level debug. If we
1126 // return an error the image will not run.
1131 // Replace .PDB with .DLL on the filename
1133 DllFileName
[Index
- 3] = 'D';
1134 DllFileName
[Index
- 2] = 'L';
1135 DllFileName
[Index
- 1] = 'L';
1138 // Load the .DLL file into the user process's address space for source
1141 Library
= LoadLibraryEx (DllFileName
, NULL
, DONT_RESOLVE_DLL_REFERENCES
);
1142 if (Library
!= NULL
) {
1144 // InitializeDriver is the entry point we put in all our EFI DLL's. The
1145 // DONT_RESOLVE_DLL_REFERENCES argument to LoadLIbraryEx() supresses the
1146 // normal DLL entry point of DllMain, and prevents other modules that are
1147 // referenced in side the DllFileName from being loaded. There is no error
1148 // checking as the we can point to the PE32 image loaded by Tiano. This
1149 // step is only needed for source level debuging
1151 DllEntryPoint
= (VOID
*) (UINTN
) GetProcAddress (Library
, "InitializeDriver");
1155 if ((Library
!= NULL
) && (DllEntryPoint
!= NULL
)) {
1156 AddModHandle (ImageContext
, Library
);
1157 ImageContext
->EntryPoint
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) DllEntryPoint
;
1158 wprintf (L
"LoadLibraryEx (%s,\n NULL, DONT_RESOLVE_DLL_REFERENCES)\n", DllFileName
);
1160 wprintf (L
"WARNING: No source level debug %s. \n", DllFileName
);
1167 // Never return an error if PeCoffLoaderRelocateImage() succeeded.
1168 // The image will run, but we just can't source level debug. If we
1169 // return an error the image will not run.
1177 SecNt32PeCoffUnloadimage (
1178 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL
*This
,
1179 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
1184 ModHandle
= RemoveModeHandle (ImageContext
);
1185 if (ModHandle
!= NULL
) {
1186 FreeLibrary (ModHandle
);
1200 SecTemporaryRamSupport (
1201 IN CONST EFI_PEI_SERVICES
**PeiServices
,
1202 IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase
,
1203 IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase
,
1208 // Migrate the whole temporary memory to permenent memory.
1211 (VOID
*)(UINTN
)PermanentMemoryBase
,
1212 (VOID
*)(UINTN
)TemporaryMemoryBase
,
1217 // SecSwitchStack function must be invoked after the memory migration
1218 // immediatly, also we need fixup the stack change caused by new call into
1219 // permenent memory.
1222 (UINT32
) TemporaryMemoryBase
,
1223 (UINT32
) PermanentMemoryBase
1227 // We need *not* fix the return address because currently,
1228 // The PeiCore is excuted in flash.
1232 // Simulate to invalid CAR, terminate CAR
1234 //ZeroMem ((VOID*)(UINTN)TemporaryMemoryBase, CopySize);