3 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
12 // Hack to work in NT32
20 IN EFI_PHYSICAL_ADDRESS
*ImageAddress
,
22 IN EFI_PHYSICAL_ADDRESS
*EntryPoint
33 EFI_PEI_HOB_POINTERS Hob
;
35 Alloc
= AllocatePages (Pages
);
40 // find the HOB we just created, and change the type to EfiBootServicesCode
41 Hob
.Raw
= GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
);
42 while (Hob
.Raw
!= NULL
) {
43 if (Hob
.MemoryAllocation
->AllocDescriptor
.MemoryBaseAddress
== (UINTN
)Alloc
) {
44 Hob
.MemoryAllocation
->AllocDescriptor
.MemoryType
= EfiBootServicesCode
;
48 Hob
.Raw
= GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, GET_NEXT_HOB (Hob
));
53 FreePages (Alloc
, Pages
);
61 OUT EFI_PHYSICAL_ADDRESS
*ImageAddress
,
62 OUT UINT64
*ImageSize
,
63 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
67 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
70 ZeroMem (&ImageContext
, sizeof (ImageContext
));
72 ImageContext
.Handle
= PeCoffImage
;
73 ImageContext
.ImageRead
= PeCoffLoaderImageReadFromMemory
;
75 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
76 ASSERT_EFI_ERROR (Status
);
79 // Allocate Memory for the image
81 Buffer
= AllocateCodePages (EFI_SIZE_TO_PAGES ((UINT32
)ImageContext
.ImageSize
));
84 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)Buffer
;
87 // Load the image to our new buffer
89 Status
= PeCoffLoaderLoadImage (&ImageContext
);
90 ASSERT_EFI_ERROR (Status
);
93 // Relocate the image in our new buffer
95 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
96 ASSERT_EFI_ERROR (Status
);
98 *ImageAddress
= ImageContext
.ImageAddress
;
99 *ImageSize
= ImageContext
.ImageSize
;
100 *EntryPoint
= ImageContext
.EntryPoint
;
103 // Flush not needed for all architectures. We could have a processor specific
104 // function in this library that does the no-op if needed.
106 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)*ImageAddress
, (UINTN
)*ImageSize
);
113 (EFIAPI
*DXE_CORE_ENTRY_POINT
)(
119 LoadDxeCoreFromFfsFile (
120 IN EFI_PEI_FILE_HANDLE FileHandle
,
126 EFI_PHYSICAL_ADDRESS ImageAddress
;
128 EFI_PHYSICAL_ADDRESS EntryPoint
;
132 EFI_FV_FILE_INFO FvFileInfo
;
134 Status
= FfsFindSectionData (EFI_SECTION_PE32
, NULL
, FileHandle
, &PeCoffImage
);
135 if (EFI_ERROR (Status
)) {
139 Status
= LoadPeCoffImage (PeCoffImage
, &ImageAddress
, &ImageSize
, &EntryPoint
);
140 // For NT32 Debug Status = SecWinNtPeiLoadFile (PeCoffImage, &ImageAddress, &ImageSize, &EntryPoint);
141 ASSERT_EFI_ERROR (Status
);
144 // Extract the DxeCore GUID file name.
146 Status
= FfsGetFileInfo (FileHandle
, &FvFileInfo
);
147 ASSERT_EFI_ERROR (Status
);
149 BuildModuleHob (&FvFileInfo
.FileName
, (EFI_PHYSICAL_ADDRESS
)(UINTN
)ImageAddress
, EFI_SIZE_TO_PAGES ((UINT32
)ImageSize
) * EFI_PAGE_SIZE
, EntryPoint
);
151 DEBUG ((DEBUG_INFO
| DEBUG_LOAD
, "Loading DxeCore at 0x%10p EntryPoint=0x%10p\n", (VOID
*)(UINTN
)ImageAddress
, (VOID
*)(UINTN
)EntryPoint
));
154 if (StackSize
== 0) {
155 // User the current stack
157 ((DXE_CORE_ENTRY_POINT
)(UINTN
)EntryPoint
)(Hob
);
160 // Allocate 128KB for the Stack
162 BaseOfStack
= AllocatePages (EFI_SIZE_TO_PAGES (StackSize
));
163 ASSERT (BaseOfStack
!= NULL
);
166 // Compute the top of the stack we were allocated. Pre-allocate a UINTN
169 TopOfStack
= (VOID
*)((UINTN
)BaseOfStack
+ EFI_SIZE_TO_PAGES (StackSize
) * EFI_PAGE_SIZE
- CPU_STACK_ALIGNMENT
);
170 TopOfStack
= ALIGN_POINTER (TopOfStack
, CPU_STACK_ALIGNMENT
);
173 // Update the contents of BSP stack HOB to reflect the real stack info passed to DxeCore.
175 UpdateStackHob ((EFI_PHYSICAL_ADDRESS
)(UINTN
)BaseOfStack
, StackSize
);
178 (SWITCH_STACK_ENTRY_POINT
)(UINTN
)EntryPoint
,
185 // Should never get here as DXE Core does not return
186 DEBUG ((DEBUG_ERROR
, "DxeCore returned\n"));
189 return EFI_DEVICE_ERROR
;
195 IN UINTN
*FvInstance OPTIONAL
,
200 EFI_PEI_FV_HANDLE VolumeHandle
;
201 EFI_PEI_FILE_HANDLE FileHandle
= NULL
;
203 if (FvInstance
!= NULL
) {
205 // Caller passed in a specific FV to try, so only try that one
207 Status
= FfsFindNextVolume (*FvInstance
, &VolumeHandle
);
208 if (!EFI_ERROR (Status
)) {
209 Status
= FfsFindNextFile (EFI_FV_FILETYPE_DXE_CORE
, VolumeHandle
, &FileHandle
);
212 Status
= FfsAnyFvFindFirstFile (EFI_FV_FILETYPE_DXE_CORE
, &VolumeHandle
, &FileHandle
);
215 if (!EFI_ERROR (Status
)) {
216 return LoadDxeCoreFromFfsFile (FileHandle
, StackSize
);
229 EFI_PEI_FV_HANDLE VolumeHandle
;
230 EFI_PEI_FILE_HANDLE FileHandle
;
232 Status
= FfsAnyFvFindFirstFile (EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
, &VolumeHandle
, &FileHandle
);
233 if (!EFI_ERROR (Status
)) {
234 Status
= FfsProcessFvFile (FileHandle
);