3 Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
4 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.
33 #ifndef SE_TIME_ZONE_NAME
34 #define SE_TIME_ZONE_NAME TEXT("SeTimeZonePrivilege")
37 NT_PEI_LOAD_FILE_PPI mSecNtLoadFilePpi
= { SecWinNtPeiLoadFile
};
39 PEI_NT_AUTOSCAN_PPI mSecNtAutoScanPpi
= { SecWinNtPeiAutoScan
};
41 PEI_NT_THUNK_PPI mSecWinNtThunkPpi
= { SecWinNtWinNtThunkAddress
};
43 EFI_PEI_PROGRESS_CODE_PPI mSecStatusCodePpi
= { SecPeiReportStatusCode
};
45 NT_FWH_PPI mSecFwhInformationPpi
= { SecWinNtFdAddress
};
47 EFI_PEI_TEMPORARY_RAM_SUPPORT_PPI mSecTemporaryRamSupportPpi
= {SecTemporaryRamSupport
};
49 EFI_PEI_PPI_DESCRIPTOR gPrivateDispatchTable
[] = {
51 EFI_PEI_PPI_DESCRIPTOR_PPI
,
52 &gNtPeiLoadFilePpiGuid
,
56 EFI_PEI_PPI_DESCRIPTOR_PPI
,
57 &gPeiNtAutoScanPpiGuid
,
61 EFI_PEI_PPI_DESCRIPTOR_PPI
,
66 EFI_PEI_PPI_DESCRIPTOR_PPI
,
67 &gEfiPeiStatusCodePpiGuid
,
71 EFI_PEI_PPI_DESCRIPTOR_PPI
,
72 &gEfiTemporaryRamSupportPpiGuid
,
73 &mSecTemporaryRamSupportPpi
76 EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
,
78 &mSecFwhInformationPpi
84 // Default information about where the FD is located.
85 // This array gets filled in with information from PcdWinNtFirmwareVolume
86 // The number of array elements is allocated base on parsing
87 // PcdWinNtFirmwareVolume and the memory is never freed.
89 UINTN gFdInfoCount
= 0;
93 // Array that supports seperate memory rantes.
94 // The memory ranges are set by PcdWinNtMemorySizeForSecMain.
95 // The number of array elements is allocated base on parsing
96 // PcdWinNtMemorySizeForSecMain value and the memory is never freed.
98 UINTN gSystemMemoryCount
= 0;
99 NT_SYSTEM_MEMORY
*gSystemMemory
;
104 UINT32 TemporaryMemoryBase
,
105 UINT32 PermenentMemoryBase
108 SecNt32PeCoffRelocateImage (
109 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
115 IN SWITCH_STACK_ENTRY_POINT EntryPoint
,
116 IN VOID
*Context1
, OPTIONAL
117 IN VOID
*Context2
, OPTIONAL
118 IN VOID
*Context3
, OPTIONAL
130 CHAR8 Buffer
[EFI_STATUS_CODE_DATA_MAX_SIZE
];
132 va_start (Marker
, Format
);
134 _vsnprintf (Buffer
, sizeof (Buffer
), Format
, Marker
);
138 CharCount
= strlen (Buffer
);
140 GetStdHandle (STD_OUTPUT_HANDLE
),
158 Main entry point to SEC for WinNt. This is a Windows program
161 Argc - Number of command line arguments
162 Argv - Array of command line argument strings
163 Envp - Array of environmemt variable strings
173 TOKEN_PRIVILEGES TokenPrivileges
;
174 EFI_PHYSICAL_ADDRESS InitialStackMemory
;
175 UINT64 InitialStackMemorySize
;
183 CHAR16
*MemorySizeStr
;
184 CHAR16
*FirmwareVolumesStr
;
188 // Enable the privilege so that RTC driver can successfully run SetTime()
190 OpenProcessToken (GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
|TOKEN_QUERY
, &Token
);
191 if (LookupPrivilegeValue(NULL
, SE_TIME_ZONE_NAME
, &TokenPrivileges
.Privileges
[0].Luid
)) {
192 TokenPrivileges
.PrivilegeCount
= 1;
193 TokenPrivileges
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
194 AdjustTokenPrivileges(Token
, FALSE
, &TokenPrivileges
, 0, (PTOKEN_PRIVILEGES
) NULL
, 0);
197 MemorySizeStr
= (CHAR16
*) PcdGetPtr (PcdWinNtMemorySizeForSecMain
);
198 FirmwareVolumesStr
= (CHAR16
*) PcdGetPtr (PcdWinNtFirmwareVolume
);
200 SecPrint ("\nEDK II SEC Main NT Emulation Environment from www.TianoCore.org\n");
203 // Make some Windows calls to Set the process to the highest priority in the
204 // idle class. We need this to have good performance.
206 SetPriorityClass (GetCurrentProcess (), IDLE_PRIORITY_CLASS
);
207 SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_HIGHEST
);
210 // Allocate space for gSystemMemory Array
212 gSystemMemoryCount
= CountSeperatorsInString (MemorySizeStr
, '!') + 1;
213 gSystemMemory
= calloc (gSystemMemoryCount
, sizeof (NT_SYSTEM_MEMORY
));
214 if (gSystemMemory
== NULL
) {
215 SecPrint ("ERROR : Can not allocate memory for %S. Exiting.\n", MemorySizeStr
);
219 // Allocate space for gSystemMemory Array
221 gFdInfoCount
= CountSeperatorsInString (FirmwareVolumesStr
, '!') + 1;
222 gFdInfo
= calloc (gFdInfoCount
, sizeof (NT_FD_INFO
));
223 if (gFdInfo
== NULL
) {
224 SecPrint ("ERROR : Can not allocate memory for %S. Exiting.\n", FirmwareVolumesStr
);
228 // Setup Boot Mode. If BootModeStr == "" then BootMode = 0 (BOOT_WITH_FULL_CONFIGURATION)
230 SecPrint (" BootMode 0x%02x\n", PcdGet32 (PcdWinNtBootMode
));
233 // Allocate 128K memory to emulate temp memory for PEI.
234 // on a real platform this would be SRAM, or using the cache as RAM.
235 // Set InitialStackMemory to zero so WinNtOpenFile will allocate a new mapping
237 InitialStackMemorySize
= STACK_SIZE
;
238 InitialStackMemory
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VirtualAlloc (NULL
, (SIZE_T
) (InitialStackMemorySize
), MEM_COMMIT
, PAGE_EXECUTE_READWRITE
);
239 if (InitialStackMemory
== 0) {
240 SecPrint ("ERROR : Can not allocate enough space for SecStack\n");
244 for (StackPointer
= (UINTN
*) (UINTN
) InitialStackMemory
;
245 StackPointer
< (UINTN
*) ((UINTN
)InitialStackMemory
+ (SIZE_T
) InitialStackMemorySize
);
247 *StackPointer
= 0x5AA55AA5;
250 SecPrint (" SEC passing in %d bytes of temp RAM to PEI\n", InitialStackMemorySize
);
253 // Open All the firmware volumes and remember the info in the gFdInfo global
255 FileNamePtr
= (CHAR16
*)malloc (StrLen ((CHAR16
*)FirmwareVolumesStr
) * sizeof(CHAR16
));
256 if (FileNamePtr
== NULL
) {
257 SecPrint ("ERROR : Can not allocate memory for firmware volume string\n");
261 StrCpy (FileNamePtr
, (CHAR16
*)FirmwareVolumesStr
);
263 for (Done
= FALSE
, Index
= 0, PeiCoreFile
= NULL
; !Done
; Index
++) {
264 FileName
= FileNamePtr
;
265 for (Index1
= 0; (FileNamePtr
[Index1
] != '!') && (FileNamePtr
[Index1
] != 0); Index1
++)
267 if (FileNamePtr
[Index1
] == 0) {
270 FileNamePtr
[Index1
] = '\0';
271 FileNamePtr
= FileNamePtr
+ Index1
+ 1;
275 // Open the FD and remmeber where it got mapped into our processes address space
277 Status
= WinNtOpenFile (
281 &gFdInfo
[Index
].Address
,
284 if (EFI_ERROR (Status
)) {
285 SecPrint ("ERROR : Can not open Firmware Device File %S (0x%X). Exiting.\n", FileName
, Status
);
289 SecPrint (" FD loaded from");
291 // printf can't print filenames directly as the \ gets interperted as an
294 for (Index2
= 0; FileName
[Index2
] != '\0'; Index2
++) {
295 SecPrint ("%c", FileName
[Index2
]);
298 if (PeiCoreFile
== NULL
) {
300 // Assume the beginning of the FD is an FV and look for the PEI Core.
301 // Load the first one we find.
303 Status
= SecFfsFindPeiCore ((EFI_FIRMWARE_VOLUME_HEADER
*) (UINTN
) gFdInfo
[Index
].Address
, &PeiCoreFile
);
304 if (!EFI_ERROR (Status
)) {
305 SecPrint (" contains SEC Core");
312 // Calculate memory regions and store the information in the gSystemMemory
313 // global for later use. The autosizing code will use this data to
314 // map this memory into the SEC process memory space.
316 for (Index
= 0, Done
= FALSE
; !Done
; Index
++) {
318 // Save the size of the memory and make a Unicode filename SystemMemory00, ...
320 gSystemMemory
[Index
].Size
= _wtoi (MemorySizeStr
) * 0x100000;
323 // Find the next region
325 for (Index1
= 0; MemorySizeStr
[Index1
] != '!' && MemorySizeStr
[Index1
] != 0; Index1
++)
327 if (MemorySizeStr
[Index1
] == 0) {
331 MemorySizeStr
= MemorySizeStr
+ Index1
+ 1;
337 // Hand off to PEI Core
339 SecLoadFromCore ((UINTN
) InitialStackMemory
, (UINTN
) InitialStackMemorySize
, (UINTN
) gFdInfo
[0].Address
, PeiCoreFile
);
342 // If we get here, then the PEI Core returned. This is an error as PEI should
343 // always hand off to DXE.
345 SecPrint ("ERROR : PEI Core returned\n");
353 IN DWORD CreationDisposition
,
354 IN OUT EFI_PHYSICAL_ADDRESS
*BaseAddress
,
360 Opens and memory maps a file using WinNt services. If BaseAddress is non zero
361 the process will try and allocate the memory starting at BaseAddress.
364 FileName - The name of the file to open and map
365 MapSize - The amount of the file to map in bytes
366 CreationDisposition - The flags to pass to CreateFile(). Use to create new files for
367 memory emulation, and exiting files for firmware volume emulation
368 BaseAddress - The base address of the mapped file in the user address space.
369 If passed in as NULL the a new memory region is used.
370 If passed in as non NULL the request memory region is used for
371 the mapping of the file into the process space.
372 Length - The size of the mapped region in bytes
375 EFI_SUCCESS - The file was opened and mapped.
376 EFI_NOT_FOUND - FileName was not found in the current directory
377 EFI_DEVICE_ERROR - An error occured attempting to map the opened file
383 VOID
*VirtualAddress
;
387 // Use Win API to open/create a file
389 NtFileHandle
= CreateFile (
391 GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
,
395 FILE_ATTRIBUTE_NORMAL
,
398 if (NtFileHandle
== INVALID_HANDLE_VALUE
) {
399 return EFI_NOT_FOUND
;
402 // Map the open file into a memory range
404 NtMapHandle
= CreateFileMapping (
407 PAGE_EXECUTE_READWRITE
,
412 if (NtMapHandle
== NULL
) {
413 return EFI_DEVICE_ERROR
;
416 // Get the virtual address (address in the emulator) of the mapped file
418 VirtualAddress
= MapViewOfFileEx (
420 FILE_MAP_EXECUTE
| FILE_MAP_ALL_ACCESS
,
424 (LPVOID
) (UINTN
) *BaseAddress
426 if (VirtualAddress
== NULL
) {
427 return EFI_DEVICE_ERROR
;
432 // Seek to the end of the file to figure out the true file size.
434 FileSize
= SetFilePointer (
440 if (FileSize
== -1) {
441 return EFI_DEVICE_ERROR
;
444 *Length
= (UINT64
) FileSize
;
446 *Length
= (UINT64
) MapSize
;
449 *BaseAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VirtualAddress
;
455 #define BYTES_PER_RECORD 512
459 SecPeiReportStatusCode (
460 IN CONST EFI_PEI_SERVICES
**PeiServices
,
461 IN EFI_STATUS_CODE_TYPE CodeType
,
462 IN EFI_STATUS_CODE_VALUE Value
,
464 IN CONST EFI_GUID
*CallerId
,
465 IN CONST EFI_STATUS_CODE_DATA
*Data OPTIONAL
471 This routine produces the ReportStatusCode PEI service. It's passed
472 up to the PEI Core via a PPI. T
474 This code currently uses the NT clib printf. This does not work the same way
475 as the EFI Print (), as %t, %g, %s as Unicode are not supported.
478 (see EFI_PEI_REPORT_STATUS_CODE)
481 EFI_SUCCESS - Always return success
484 // TODO: PeiServices - add argument and description to function comment
485 // TODO: CodeType - add argument and description to function comment
486 // TODO: Value - add argument and description to function comment
487 // TODO: Instance - add argument and description to function comment
488 // TODO: CallerId - add argument and description to function comment
489 // TODO: Data - add argument and description to function comment
493 CHAR8 PrintBuffer
[BYTES_PER_RECORD
* 2];
501 } else if (ReportStatusCodeExtractAssertInfo (CodeType
, Value
, Data
, &Filename
, &Description
, &LineNumber
)) {
503 // Processes ASSERT ()
505 SecPrint ("ASSERT %s(%d): %s\n", Filename
, (int)LineNumber
, Description
);
507 } else if (ReportStatusCodeExtractDebugInfo (Data
, &ErrorLevel
, &Marker
, &Format
)) {
509 // Process DEBUG () macro
511 AsciiBSPrint (PrintBuffer
, BYTES_PER_RECORD
, Format
, Marker
);
512 SecPrint (PrintBuffer
);
518 #if defined (MDE_CPU_IA32)
520 Transfers control to a function starting with a new stack.
522 Transfers control to the function specified by EntryPoint using the new stack
523 specified by NewStack and passing in the parameters specified by Context1 and
524 Context2. Context1 and Context2 are optional and may be NULL. The function
525 EntryPoint must never return.
527 If EntryPoint is NULL, then ASSERT().
528 If NewStack is NULL, then ASSERT().
530 @param EntryPoint A pointer to function to call with the new stack.
531 @param Context1 A pointer to the context to pass into the EntryPoint
533 @param Context2 A pointer to the context to pass into the EntryPoint
535 @param NewStack A pointer to the new stack to use for the EntryPoint
537 @param NewBsp A pointer to the new BSP for the EntryPoint on IPF. It's
538 Reserved on other architectures.
544 IN SWITCH_STACK_ENTRY_POINT EntryPoint
,
545 IN VOID
*Context1
, OPTIONAL
546 IN VOID
*Context2
, OPTIONAL
547 IN VOID
*Context3
, OPTIONAL
551 BASE_LIBRARY_JUMP_BUFFER JumpBuffer
;
553 ASSERT (EntryPoint
!= NULL
);
554 ASSERT (NewStack
!= NULL
);
557 // Stack should be aligned with CPU_STACK_ALIGNMENT
559 ASSERT (((UINTN
)NewStack
& (CPU_STACK_ALIGNMENT
- 1)) == 0);
561 JumpBuffer
.Eip
= (UINTN
)EntryPoint
;
562 JumpBuffer
.Esp
= (UINTN
)NewStack
- sizeof (VOID
*);
563 JumpBuffer
.Esp
-= sizeof (Context1
) + sizeof (Context2
) + sizeof(Context3
);
564 ((VOID
**)JumpBuffer
.Esp
)[1] = Context1
;
565 ((VOID
**)JumpBuffer
.Esp
)[2] = Context2
;
566 ((VOID
**)JumpBuffer
.Esp
)[3] = Context3
;
568 LongJump (&JumpBuffer
, (UINTN
)-1);
572 // InternalSwitchStack () will never return
580 IN UINTN LargestRegion
,
581 IN UINTN LargestRegionSize
,
582 IN UINTN BootFirmwareVolumeBase
,
583 IN VOID
*PeiCorePe32File
588 This is the service to load the PEI Core from the Firmware Volume
591 LargestRegion - Memory to use for PEI.
592 LargestRegionSize - Size of Memory to use for PEI
593 BootFirmwareVolumeBase - Start of the Boot FV
594 PeiCorePe32File - PEI Core PE32
597 Success means control is transfered and thus we should never return
604 EFI_PHYSICAL_ADDRESS PeiCoreEntryPoint
;
605 EFI_PHYSICAL_ADDRESS PeiImageAddress
;
606 EFI_SEC_PEI_HAND_OFF
*SecCoreData
;
610 // Compute Top Of Memory for Stack and PEI Core Allocations
612 PeiStackSize
= (UINTN
)RShiftU64((UINT64
)STACK_SIZE
,1);
615 // |-----------| <---- TemporaryRamBase + TemporaryRamSize
618 // |-----------| <---- StackBase / PeiTemporaryMemoryBase
621 // |-----------| <---- TemporaryRamBase
623 TopOfStack
= (VOID
*)(LargestRegion
+ PeiStackSize
);
626 // Reservet space for storing PeiCore's parament in stack.
628 TopOfStack
= (VOID
*)((UINTN
)TopOfStack
- sizeof (EFI_SEC_PEI_HAND_OFF
) - CPU_STACK_ALIGNMENT
);
629 TopOfStack
= ALIGN_POINTER (TopOfStack
, CPU_STACK_ALIGNMENT
);
632 // Bind this information into the SEC hand-off state
634 SecCoreData
= (EFI_SEC_PEI_HAND_OFF
*)(UINTN
) TopOfStack
;
635 SecCoreData
->DataSize
= sizeof(EFI_SEC_PEI_HAND_OFF
);
636 SecCoreData
->BootFirmwareVolumeBase
= (VOID
*)BootFirmwareVolumeBase
;
637 SecCoreData
->BootFirmwareVolumeSize
= PcdGet32(PcdWinNtFirmwareFdSize
);
638 SecCoreData
->TemporaryRamBase
= (VOID
*)(UINTN
)LargestRegion
;
639 SecCoreData
->TemporaryRamSize
= STACK_SIZE
;
640 SecCoreData
->StackBase
= SecCoreData
->TemporaryRamBase
;
641 SecCoreData
->StackSize
= PeiStackSize
;
642 SecCoreData
->PeiTemporaryRamBase
= (VOID
*) ((UINTN
) SecCoreData
->TemporaryRamBase
+ PeiStackSize
);
643 SecCoreData
->PeiTemporaryRamSize
= STACK_SIZE
- PeiStackSize
;
646 // Load the PEI Core from a Firmware Volume
648 Status
= SecWinNtPeiLoadFile (
654 if (EFI_ERROR (Status
)) {
659 // Transfer control to the PEI Core
662 (SWITCH_STACK_ENTRY_POINT
) (UINTN
) PeiCoreEntryPoint
,
664 (VOID
*) (UINTN
) ((EFI_PEI_PPI_DESCRIPTOR
*) &gPrivateDispatchTable
),
669 // If we get here, then the PEI Core returned. This is an error
676 SecWinNtPeiAutoScan (
678 OUT EFI_PHYSICAL_ADDRESS
*MemoryBase
,
679 OUT UINT64
*MemorySize
684 This service is called from Index == 0 until it returns EFI_UNSUPPORTED.
685 It allows discontiguous memory regions to be supported by the emulator.
686 It uses gSystemMemory[] and gSystemMemoryCount that were created by
687 parsing PcdWinNtMemorySizeForSecMain value.
688 The size comes from the Pcd value and the address comes from the memory space
689 with ReadWrite and Execute attributes allocated by VirtualAlloc() API.
692 Index - Which memory region to use
693 MemoryBase - Return Base address of memory region
694 MemorySize - Return size in bytes of the memory region
697 EFI_SUCCESS - If memory region was mapped
698 EFI_UNSUPPORTED - If Index is not supported
702 if (Index
>= gSystemMemoryCount
) {
703 return EFI_UNSUPPORTED
;
707 // Allocate enough memory space for emulator
709 gSystemMemory
[Index
].Memory
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VirtualAlloc (NULL
, (SIZE_T
) (gSystemMemory
[Index
].Size
), MEM_COMMIT
, PAGE_EXECUTE_READWRITE
);
710 if (gSystemMemory
[Index
].Memory
== 0) {
711 return EFI_OUT_OF_RESOURCES
;
714 *MemoryBase
= gSystemMemory
[Index
].Memory
;
715 *MemorySize
= gSystemMemory
[Index
].Size
;
722 SecWinNtWinNtThunkAddress (
728 Since the SEC is the only Windows program in stack it must export
729 an interface to do Win API calls. That's what the WinNtThunk address
730 is for. gWinNt is initailized in WinNtThunk.c.
733 InterfaceSize - sizeof (EFI_WIN_NT_THUNK_PROTOCOL);
734 InterfaceBase - Address of the gWinNt global
737 EFI_SUCCESS - Data returned
747 SecWinNtPeiLoadFile (
749 IN EFI_PHYSICAL_ADDRESS
*ImageAddress
,
750 IN UINT64
*ImageSize
,
751 IN EFI_PHYSICAL_ADDRESS
*EntryPoint
756 Loads and relocates a PE/COFF image into memory.
759 Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated
760 ImageAddress - The base address of the relocated PE/COFF image
761 ImageSize - The size of the relocated PE/COFF image
762 EntryPoint - The entry point of the relocated PE/COFF image
765 EFI_SUCCESS - The file was loaded and relocated
766 EFI_OUT_OF_RESOURCES - There was not enough memory to load and relocate the PE/COFF file
771 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
773 ZeroMem (&ImageContext
, sizeof (ImageContext
));
774 ImageContext
.Handle
= Pe32Data
;
776 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) SecImageRead
;
778 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
779 if (EFI_ERROR (Status
)) {
783 // Allocate space in NT (not emulator) memory with ReadWrite and Execute attribue.
784 // Extra space is for alignment
786 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) VirtualAlloc (NULL
, (SIZE_T
) (ImageContext
.ImageSize
+ (ImageContext
.SectionAlignment
* 2)), MEM_COMMIT
, PAGE_EXECUTE_READWRITE
);
787 if (ImageContext
.ImageAddress
== 0) {
788 return EFI_OUT_OF_RESOURCES
;
791 // Align buffer on section boundry
793 ImageContext
.ImageAddress
+= ImageContext
.SectionAlignment
- 1;
794 ImageContext
.ImageAddress
&= ~(ImageContext
.SectionAlignment
- 1);
796 Status
= PeCoffLoaderLoadImage (&ImageContext
);
797 if (EFI_ERROR (Status
)) {
801 Status
= SecNt32PeCoffRelocateImage (&ImageContext
);
802 if (EFI_ERROR (Status
)) {
807 // BugBug: Flush Instruction Cache Here when CPU Lib is ready
810 *ImageAddress
= ImageContext
.ImageAddress
;
811 *ImageSize
= ImageContext
.ImageSize
;
812 *EntryPoint
= ImageContext
.EntryPoint
;
821 IN OUT EFI_PHYSICAL_ADDRESS
*FdBase
,
822 IN OUT UINT64
*FdSize
827 Return the FD Size and base address. Since the FD is loaded from a
828 file into Windows memory only the SEC will know it's address.
831 Index - Which FD, starts at zero.
832 FdSize - Size of the FD in bytes
833 FdBase - Start address of the FD. Assume it points to an FV Header
836 EFI_SUCCESS - Return the Base address and size of the FV
837 EFI_UNSUPPORTED - Index does nto map to an FD in the system
841 if (Index
>= gFdInfoCount
) {
842 return EFI_UNSUPPORTED
;
845 *FdBase
= gFdInfo
[Index
].Address
;
846 *FdSize
= gFdInfo
[Index
].Size
;
848 if (*FdBase
== 0 && *FdSize
== 0) {
849 return EFI_UNSUPPORTED
;
860 IN OUT UINTN
*ReadSize
,
866 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
869 FileHandle - The handle to the PE/COFF file
870 FileOffset - The offset, in bytes, into the file to read
871 ReadSize - The number of bytes to read from the file starting at FileOffset
872 Buffer - A pointer to the buffer to read the data into.
875 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
883 Destination8
= Buffer
;
884 Source8
= (CHAR8
*) ((UINTN
) FileHandle
+ FileOffset
);
887 *(Destination8
++) = *(Source8
++);
896 IN UINTN
*StrLen OPTIONAL
901 Convert the passed in Ascii string to Unicode.
902 Optionally return the length of the strings.
905 Ascii - Ascii string to convert
906 StrLen - Length of string
909 Pointer to malloc'ed Unicode version of Ascii
917 // Allocate a buffer for unicode string
919 for (Index
= 0; Ascii
[Index
] != '\0'; Index
++)
921 Unicode
= malloc ((Index
+ 1) * sizeof (CHAR16
));
922 if (Unicode
== NULL
) {
926 for (Index
= 0; Ascii
[Index
] != '\0'; Index
++) {
927 Unicode
[Index
] = (CHAR16
) Ascii
[Index
];
930 Unicode
[Index
] = '\0';
932 if (StrLen
!= NULL
) {
940 CountSeperatorsInString (
941 IN CONST CHAR16
*String
,
947 Count the number of seperators in String
950 String - String to process
951 Seperator - Item to count
954 Number of Seperator in String
960 for (Count
= 0; *String
!= '\0'; String
++) {
961 if (*String
== Seperator
) {
971 SecNt32PeCoffRelocateImage (
972 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
982 Status
= PeCoffLoaderRelocateImage (ImageContext
);
983 if (EFI_ERROR (Status
)) {
985 // We could not relocated the image in memory properly
991 // If we load our own PE COFF images the Windows debugger can not source
992 // level debug our code. If a valid PDB pointer exists usw it to load
993 // the *.dll file as a library using Windows* APIs. This allows
994 // source level debug. The image is still loaded and reloaced
995 // in the Framework memory space like on a real system (by the code above),
996 // but the entry point points into the DLL loaded by the code bellow.
999 DllEntryPoint
= NULL
;
1002 // Load the DLL if it's not an EBC image.
1004 if ((ImageContext
->PdbPointer
!= NULL
) &&
1005 (ImageContext
->Machine
!= EFI_IMAGE_MACHINE_EBC
)) {
1007 // Convert filename from ASCII to Unicode
1009 DllFileName
= AsciiToUnicode (ImageContext
->PdbPointer
, &Index
);
1012 // Check that we have a valid filename
1014 if (Index
< 5 || DllFileName
[Index
- 4] != '.') {
1018 // Never return an error if PeCoffLoaderRelocateImage() succeeded.
1019 // The image will run, but we just can't source level debug. If we
1020 // return an error the image will not run.
1025 // Replace .PDB with .DLL on the filename
1027 DllFileName
[Index
- 3] = 'D';
1028 DllFileName
[Index
- 2] = 'L';
1029 DllFileName
[Index
- 1] = 'L';
1032 // Load the .DLL file into the user process's address space for source
1035 Library
= LoadLibraryEx (DllFileName
, NULL
, DONT_RESOLVE_DLL_REFERENCES
);
1036 if (Library
!= NULL
) {
1038 // InitializeDriver is the entry point we put in all our EFI DLL's. The
1039 // DONT_RESOLVE_DLL_REFERENCES argument to LoadLIbraryEx() supresses the
1040 // normal DLL entry point of DllMain, and prevents other modules that are
1041 // referenced in side the DllFileName from being loaded. There is no error
1042 // checking as the we can point to the PE32 image loaded by Tiano. This
1043 // step is only needed for source level debuging
1045 DllEntryPoint
= (VOID
*) (UINTN
) GetProcAddress (Library
, "InitializeDriver");
1049 if ((Library
!= NULL
) && (DllEntryPoint
!= NULL
)) {
1050 ImageContext
->EntryPoint
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) DllEntryPoint
;
1051 SecPrint ("LoadLibraryEx (%S,\n NULL, DONT_RESOLVE_DLL_REFERENCES)\n", DllFileName
);
1053 SecPrint ("WARNING: No source level debug %S. \n", DllFileName
);
1060 // Never return an error if PeCoffLoaderRelocateImage() succeeded.
1061 // The image will run, but we just can't source level debug. If we
1062 // return an error the image will not run.
1079 SecTemporaryRamSupport (
1080 IN CONST EFI_PEI_SERVICES
**PeiServices
,
1081 IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase
,
1082 IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase
,
1087 // Migrate the whole temporary memory to permenent memory.
1090 (VOID
*)(UINTN
)PermanentMemoryBase
,
1091 (VOID
*)(UINTN
)TemporaryMemoryBase
,
1096 // SecSwitchStack function must be invoked after the memory migration
1097 // immediatly, also we need fixup the stack change caused by new call into
1098 // permenent memory.
1101 (UINT32
) TemporaryMemoryBase
,
1102 (UINT32
) PermanentMemoryBase
1106 // We need *not* fix the return address because currently,
1107 // The PeiCore is excuted in flash.
1111 // Simulate to invalid temporary memory, terminate temporary memory
1113 //ZeroMem ((VOID*)(UINTN)TemporaryMemoryBase, CopySize);