TianoCore EDK2 master
Loading...
Searching...
No Matches
LoadDxeCore.c
Go to the documentation of this file.
1
9#include "UefiPayloadEntry.h"
10
18VOID *
20 IN UINTN Pages
21 )
22{
23 VOID *Alloc;
25
26 Alloc = AllocatePages (Pages);
27 if (Alloc == NULL) {
28 return NULL;
29 }
30
31 // find the HOB we just created, and change the type to EfiBootServicesCode
32 Hob.Raw = GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION);
33 while (Hob.Raw != NULL) {
34 if (Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress == (UINTN)Alloc) {
35 Hob.MemoryAllocation->AllocDescriptor.MemoryType = EfiBootServicesCode;
36 return Alloc;
37 }
38
39 Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, GET_NEXT_HOB (Hob));
40 }
41
42 ASSERT (FALSE);
43
44 FreePages (Alloc, Pages);
45 return NULL;
46}
47
61 IN VOID *PeCoffImage,
62 OUT EFI_PHYSICAL_ADDRESS *ImageAddress,
63 OUT UINT64 *ImageSize,
64 OUT EFI_PHYSICAL_ADDRESS *EntryPoint
65 )
66{
67 RETURN_STATUS Status;
69 VOID *Buffer;
70
71 ZeroMem (&ImageContext, sizeof (ImageContext));
72
73 ImageContext.Handle = PeCoffImage;
75
76 Status = PeCoffLoaderGetImageInfo (&ImageContext);
77 if (EFI_ERROR (Status)) {
78 ASSERT_EFI_ERROR (Status);
79 return Status;
80 }
81
82 //
83 // Allocate Memory for the image
84 //
85 Buffer = AllocateCodePages (EFI_SIZE_TO_PAGES ((UINT32)ImageContext.ImageSize));
86 if (Buffer == NULL) {
87 return EFI_OUT_OF_RESOURCES;
88 }
89
90 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)Buffer;
91
92 //
93 // Load the image to our new buffer
94 //
95 Status = PeCoffLoaderLoadImage (&ImageContext);
96 if (EFI_ERROR (Status)) {
97 ASSERT_EFI_ERROR (Status);
98 return Status;
99 }
100
101 //
102 // Relocate the image in our new buffer
103 //
104 Status = PeCoffLoaderRelocateImage (&ImageContext);
105 if (EFI_ERROR (Status)) {
106 ASSERT_EFI_ERROR (Status);
107 return Status;
108 }
109
110 *ImageAddress = ImageContext.ImageAddress;
111 *ImageSize = ImageContext.ImageSize;
112 *EntryPoint = ImageContext.EntryPoint;
113
114 return EFI_SUCCESS;
115}
116
133 IN EFI_FV_FILETYPE FileType,
134 IN EFI_GUID *Guid OPTIONAL,
135 OUT EFI_FFS_FILE_HEADER **FileHeader
136 )
137{
138 EFI_PHYSICAL_ADDRESS CurrentAddress;
139 EFI_PHYSICAL_ADDRESS EndOfFirmwareVolume;
141 UINT32 Size;
142 EFI_PHYSICAL_ADDRESS EndOfFile;
143
144 CurrentAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)FvHeader;
145 EndOfFirmwareVolume = CurrentAddress + FvHeader->FvLength;
146
147 //
148 // Loop through the FFS files
149 //
150 for (EndOfFile = CurrentAddress + FvHeader->HeaderLength; ; ) {
151 CurrentAddress = (EndOfFile + 7) & 0xfffffffffffffff8ULL;
152 if (CurrentAddress > EndOfFirmwareVolume) {
153 break;
154 }
155
156 File = (EFI_FFS_FILE_HEADER *)(UINTN)CurrentAddress;
157 if (IS_FFS_FILE2 (File)) {
158 Size = FFS_FILE2_SIZE (File);
159 if (Size <= 0x00FFFFFF) {
160 break;
161 }
162 } else {
163 Size = FFS_FILE_SIZE (File);
164 if (Size < sizeof (EFI_FFS_FILE_HEADER)) {
165 break;
166 }
167 }
168
169 EndOfFile = CurrentAddress + Size;
170 if (EndOfFile > EndOfFirmwareVolume) {
171 break;
172 }
173
174 //
175 // Look for file type
176 //
177 if (File->Type == FileType) {
178 if ((Guid == NULL) || CompareGuid (&File->Name, Guid)) {
179 *FileHeader = File;
180 return EFI_SUCCESS;
181 }
182 }
183 }
184
185 return EFI_NOT_FOUND;
186}
187
202 IN EFI_FFS_FILE_HEADER *FileHeader,
203 IN EFI_SECTION_TYPE SectionType,
204 OUT VOID **SectionData
205 )
206{
207 UINT32 FileSize;
209 UINT32 SectionSize;
210 UINT32 Index;
211
212 if (IS_FFS_FILE2 (FileHeader)) {
213 FileSize = FFS_FILE2_SIZE (FileHeader);
214 Section = (EFI_COMMON_SECTION_HEADER *)(((EFI_FFS_FILE_HEADER2 *)FileHeader) + 1);
215 } else {
216 FileSize = FFS_FILE_SIZE (FileHeader);
217 Section = (EFI_COMMON_SECTION_HEADER *)(FileHeader + 1);
218 }
219
220 FileSize -= sizeof (EFI_FFS_FILE_HEADER);
221
222 Index = 0;
223 while (Index < FileSize) {
224 if (Section->Type == SectionType) {
225 if (IS_SECTION2 (Section)) {
226 *SectionData = (VOID *)((UINT8 *)Section + sizeof (EFI_COMMON_SECTION_HEADER2));
227 } else {
228 *SectionData = (VOID *)((UINT8 *)Section + sizeof (EFI_COMMON_SECTION_HEADER));
229 }
230
231 return EFI_SUCCESS;
232 }
233
234 if (IS_SECTION2 (Section)) {
235 SectionSize = SECTION2_SIZE (Section);
236 } else {
237 SectionSize = SECTION_SIZE (Section);
238 }
239
240 SectionSize = GET_OCCUPIED_SIZE (SectionSize, 4);
241 ASSERT (SectionSize != 0);
242 Index += SectionSize;
243
244 Section = (EFI_COMMON_SECTION_HEADER *)((UINT8 *)Section + SectionSize);
245 }
246
247 return EFI_NOT_FOUND;
248}
249
260 OUT PHYSICAL_ADDRESS *DxeCoreEntryPoint
261 )
262{
263 EFI_STATUS Status;
266 EFI_FFS_FILE_HEADER *FileHeader;
267 VOID *PeCoffImage;
268 EFI_PHYSICAL_ADDRESS ImageAddress;
269 UINT64 ImageSize;
270
271 PayloadFv = (EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)PcdGet32 (PcdPayloadFdMemBase);
272
273 //
274 // DXE FV is inside Payload FV. Here find DXE FV from Payload FV
275 //
276 Status = FvFindFileByTypeGuid (PayloadFv, EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE, NULL, &FileHeader);
277 if (EFI_ERROR (Status)) {
278 return Status;
279 }
280
281 Status = FileFindSection (FileHeader, EFI_SECTION_FIRMWARE_VOLUME_IMAGE, (VOID **)&DxeCoreFv);
282 if (EFI_ERROR (Status)) {
283 return Status;
284 }
285
286 //
287 // Report DXE FV to DXE core
288 //
289 BuildFvHob ((EFI_PHYSICAL_ADDRESS)(UINTN)DxeCoreFv, DxeCoreFv->FvLength);
290
291 //
292 // Find DXE core file from DXE FV
293 //
294 Status = FvFindFileByTypeGuid (DxeCoreFv, EFI_FV_FILETYPE_DXE_CORE, NULL, &FileHeader);
295 if (EFI_ERROR (Status)) {
296 return Status;
297 }
298
299 Status = FileFindSection (FileHeader, EFI_SECTION_PE32, (VOID **)&PeCoffImage);
300 if (EFI_ERROR (Status)) {
301 return Status;
302 }
303
304 //
305 // Get DXE core info
306 //
307 Status = LoadPeCoffImage (PeCoffImage, &ImageAddress, &ImageSize, DxeCoreEntryPoint);
308 if (EFI_ERROR (Status)) {
309 return Status;
310 }
311
312 BuildModuleHob (&FileHeader->Name, ImageAddress, EFI_SIZE_TO_PAGES ((UINT32)ImageSize) * EFI_PAGE_SIZE, *DxeCoreEntryPoint);
313
314 return EFI_SUCCESS;
315}
316
329 OUT PHYSICAL_ADDRESS *DxeCoreEntryPoint
330 )
331{
332 EFI_STATUS Status;
333 EFI_FFS_FILE_HEADER *FileHeader;
334 VOID *PeCoffImage;
335 EFI_PHYSICAL_ADDRESS ImageAddress;
336 UINT64 ImageSize;
337
338 //
339 // Find DXE core file from DXE FV
340 //
341 Status = FvFindFileByTypeGuid (DxeFv, EFI_FV_FILETYPE_DXE_CORE, NULL, &FileHeader);
342 if (EFI_ERROR (Status)) {
343 return Status;
344 }
345
346 Status = FileFindSection (FileHeader, EFI_SECTION_PE32, (VOID **)&PeCoffImage);
347 if (EFI_ERROR (Status)) {
348 return Status;
349 }
350
351 //
352 // Get DXE core info
353 //
354 Status = LoadPeCoffImage (PeCoffImage, &ImageAddress, &ImageSize, DxeCoreEntryPoint);
355 if (EFI_ERROR (Status)) {
356 return Status;
357 }
358
359 BuildModuleHob (&FileHeader->Name, ImageAddress, EFI_SIZE_TO_PAGES ((UINT32)ImageSize) * EFI_PAGE_SIZE, *DxeCoreEntryPoint);
360
361 return EFI_SUCCESS;
362}
UINT64 UINTN
VOID *EFIAPI GetFirstHob(IN UINT16 Type)
Definition: HobLib.c:142
VOID EFIAPI BuildModuleHob(IN CONST EFI_GUID *ModuleName, IN EFI_PHYSICAL_ADDRESS MemoryAllocationModule, IN UINT64 ModuleLength, IN EFI_PHYSICAL_ADDRESS EntryPoint)
Definition: HobLib.c:269
VOID EFIAPI BuildFvHob(IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length)
Definition: HobLib.c:404
VOID *EFIAPI GetNextHob(IN UINT16 Type, IN CONST VOID *HobStart)
Definition: HobLib.c:103
BOOLEAN EFIAPI CompareGuid(IN CONST GUID *Guid1, IN CONST GUID *Guid2)
Definition: MemLibGuid.c:73
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
VOID EFIAPI FreePages(IN VOID *Buffer, IN UINTN Pages)
EFI_STATUS UniversalLoadDxeCore(IN EFI_FIRMWARE_VOLUME_HEADER *DxeFv, OUT PHYSICAL_ADDRESS *DxeCoreEntryPoint)
Definition: LoadDxeCore.c:327
EFI_STATUS FileFindSection(IN EFI_FFS_FILE_HEADER *FileHeader, IN EFI_SECTION_TYPE SectionType, OUT VOID **SectionData)
Definition: LoadDxeCore.c:201
EFI_STATUS LoadDxeCore(OUT PHYSICAL_ADDRESS *DxeCoreEntryPoint)
Definition: LoadDxeCore.c:259
VOID * AllocateCodePages(IN UINTN Pages)
Definition: LoadDxeCore.c:19
EFI_STATUS FvFindFileByTypeGuid(IN EFI_FIRMWARE_VOLUME_HEADER *FvHeader, IN EFI_FV_FILETYPE FileType, IN EFI_GUID *Guid OPTIONAL, OUT EFI_FFS_FILE_HEADER **FileHeader)
Definition: LoadDxeCore.c:131
EFI_STATUS LoadPeCoffImage(IN VOID *PeCoffImage, OUT EFI_PHYSICAL_ADDRESS *ImageAddress, OUT UINT64 *ImageSize, OUT EFI_PHYSICAL_ADDRESS *EntryPoint)
Definition: LoadDxeCore.c:60
#define NULL
Definition: Base.h:319
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
#define PcdGet32(TokenName)
Definition: PcdLib.h:362
RETURN_STATUS EFIAPI PeCoffLoaderLoadImage(IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext)
Definition: BasePeCoff.c:1244
RETURN_STATUS EFIAPI PeCoffLoaderRelocateImage(IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext)
Definition: BasePeCoff.c:956
RETURN_STATUS EFIAPI PeCoffLoaderGetImageInfo(IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext)
Definition: BasePeCoff.c:577
RETURN_STATUS EFIAPI PeCoffLoaderImageReadFromMemory(IN VOID *FileHandle, IN UINTN FileOffset, IN OUT UINTN *ReadSize, OUT VOID *Buffer)
Definition: BasePeCoff.c:1992
#define EFI_SECTION_PE32
#define FFS_FILE_SIZE(FfsFileHeaderPtr)
#define SECTION_SIZE(SectionHeaderPtr)
VOID *EFIAPI AllocatePages(IN UINTN Pages)
UINT64 EFI_PHYSICAL_ADDRESS
Definition: UefiBaseType.h:50
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SIZE_TO_PAGES(Size)
Definition: UefiBaseType.h:200
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
@ EfiBootServicesCode
EFI_FV_FILETYPE Type
EFI_PHYSICAL_ADDRESS MemoryBaseAddress
Definition: PiHob.h:119
EFI_MEMORY_TYPE MemoryType
Definition: PiHob.h:131
EFI_HOB_MEMORY_ALLOCATION_HEADER AllocDescriptor
Definition: PiHob.h:153
Definition: Base.h:213
PE_COFF_LOADER_READ_FILE ImageRead
Definition: PeCoffLib.h:100
PHYSICAL_ADDRESS EntryPoint
Definition: PeCoffLib.h:95
PHYSICAL_ADDRESS ImageAddress
Definition: PeCoffLib.h:79