]> git.proxmox.com Git - mirror_edk2.git/blame - EdkModulePkg/Core/DxeIplX64Peim/DxeLoadX64.c
Add comments about why pre-allocate 32 bytes for load 64bit dxe core and Add code...
[mirror_edk2.git] / EdkModulePkg / Core / DxeIplX64Peim / DxeLoadX64.c
CommitLineData
3005648d 1/*++\r
2\r
3Copyright (c) 2006, Intel Corporation \r
4All rights reserved. This program and the accompanying materials \r
5are licensed and made available under the terms and conditions of the BSD License \r
6which accompanies this distribution. The full text of the license may be found at \r
7http://opensource.org/licenses/bsd-license.php \r
8 \r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
11\r
12Module Name:\r
13\r
14 DxeLoad.c\r
15\r
16Abstract:\r
17\r
18 Last PEIM.\r
19 Responsibility of this module is to load the DXE Core from a Firmware Volume.\r
20\r
21--*/\r
22\r
23#include <DxeIpl.h>\r
24\r
25#pragma warning( disable : 4305 )\r
26\r
27BOOLEAN gInMemory = FALSE;\r
28\r
3005648d 29//\r
30// Module Globals used in the DXE to PEI handoff\r
31// These must be module globals, so the stack can be switched\r
32//\r
33static EFI_DXE_IPL_PPI mDxeIplPpi = {\r
34 DxeLoadCore\r
35};\r
36\r
37static EFI_PEI_FV_FILE_LOADER_PPI mLoadFilePpi = {\r
38 DxeIplLoadFile\r
39};\r
40\r
41static EFI_PEI_PPI_DESCRIPTOR mPpiLoadFile = {\r
42 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
43 &gEfiPeiFvFileLoaderPpiGuid,\r
44 &mLoadFilePpi\r
45};\r
46\r
47static EFI_PEI_PPI_DESCRIPTOR mPpiList = {\r
48 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
49 &gEfiDxeIplPpiGuid,\r
50 &mDxeIplPpi\r
51};\r
52\r
53static EFI_PEI_PPI_DESCRIPTOR mPpiPeiInMemory = {\r
54 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
55 &gPeiInMemoryGuid,\r
56 NULL\r
57};\r
58\r
59static EFI_PEI_PPI_DESCRIPTOR mPpiSignal = {\r
60 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
61 &gEfiEndOfPeiSignalPpiGuid,\r
62 NULL\r
63};\r
64\r
e29d7096 65GLOBAL_REMOVE_IF_UNREFERENCED DECOMPRESS_LIBRARY gEfiDecompress = {\r
3005648d 66 UefiDecompressGetInfo,\r
67 UefiDecompress\r
68};\r
69\r
e29d7096 70GLOBAL_REMOVE_IF_UNREFERENCED DECOMPRESS_LIBRARY gTianoDecompress = {\r
3005648d 71 TianoDecompressGetInfo,\r
72 TianoDecompress\r
73};\r
74\r
e29d7096 75GLOBAL_REMOVE_IF_UNREFERENCED DECOMPRESS_LIBRARY gCustomDecompress = {\r
3005648d 76 CustomDecompressGetInfo,\r
77 CustomDecompress\r
78};\r
79\r
80STATIC\r
81UINTN\r
82GetOccupiedSize (\r
83 IN UINTN ActualSize,\r
84 IN UINTN Alignment\r
85 )\r
86{\r
87 UINTN OccupiedSize;\r
88\r
89 OccupiedSize = ActualSize;\r
90 while ((OccupiedSize & (Alignment - 1)) != 0) {\r
91 OccupiedSize++;\r
92 }\r
93\r
94 return OccupiedSize;\r
95}\r
96\r
97EFI_STATUS\r
98EFIAPI\r
99PeimInitializeDxeIpl (\r
100 IN EFI_FFS_FILE_HEADER *FfsHeader,\r
101 IN EFI_PEI_SERVICES **PeiServices\r
102 )\r
103/*++\r
104\r
105Routine Description:\r
106\r
107 Initializes the Dxe Ipl PPI\r
108\r
109Arguments:\r
110\r
111 FfsHeader - Pointer to FFS file header\r
112 PeiServices - General purpose services available to every PEIM.\r
113\r
114Returns:\r
115\r
116 EFI_SUCCESS\r
117\r
118--*/\r
119{\r
120 EFI_STATUS Status;\r
121 EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader;\r
122 EFI_BOOT_MODE BootMode;\r
123\r
84a99d48 124 Status = PeiServicesGetBootMode (&BootMode);\r
3005648d 125\r
126 ASSERT_EFI_ERROR (Status);\r
127\r
84a99d48 128 Status = PeiServicesLocatePpi (\r
3005648d 129 &gPeiInMemoryGuid,\r
130 0,\r
131 NULL,\r
132 NULL\r
133 );\r
134\r
135 if (EFI_ERROR (Status) && (BootMode != BOOT_ON_S3_RESUME)) { \r
136 //\r
137 // The DxeIpl has not yet been shadowed\r
138 //\r
139 PeiEfiPeiPeCoffLoader = (EFI_PEI_PE_COFF_LOADER_PROTOCOL *)GetPeCoffLoaderProtocol ();\r
140\r
141 //\r
142 // Shadow DxeIpl and then re-run its entry point\r
143 //\r
144 Status = ShadowDxeIpl (FfsHeader, PeiEfiPeiPeCoffLoader);\r
145 if (EFI_ERROR (Status)) {\r
146 return Status;\r
147 }\r
148\r
149 } else {\r
150 if (BootMode != BOOT_ON_S3_RESUME) {\r
151 //\r
152 // The DxeIpl has been shadowed\r
153 //\r
154 gInMemory = TRUE;\r
155\r
156 //\r
157 // Install LoadFile PPI\r
158 //\r
84a99d48 159 Status = PeiServicesInstallPpi (&mPpiLoadFile);\r
3005648d 160\r
161 if (EFI_ERROR (Status)) {\r
162 return Status;\r
163 }\r
164 }\r
165 //\r
166 // Install DxeIpl PPI\r
167 //\r
84a99d48 168 PeiServicesInstallPpi (&mPpiList);\r
3005648d 169\r
170 if (EFI_ERROR (Status)) {\r
171 return Status;\r
172 }\r
173\r
174 }\r
175\r
176 return EFI_SUCCESS;\r
177}\r
178\r
179EFI_STATUS\r
180EFIAPI\r
181DxeLoadCore (\r
182 IN EFI_DXE_IPL_PPI *This,\r
183 IN EFI_PEI_SERVICES **PeiServices,\r
184 IN EFI_PEI_HOB_POINTERS HobList\r
185 )\r
186/*++\r
187\r
188Routine Description:\r
189\r
190 Main entry point to last PEIM\r
191\r
192Arguments:\r
193\r
194 This - Entry point for DXE IPL PPI\r
195 PeiServices - General purpose services available to every PEIM.\r
196 HobList - Address to the Pei HOB list\r
197\r
198Returns:\r
199\r
200 EFI_SUCCESS - DEX core was successfully loaded.\r
201 EFI_OUT_OF_RESOURCES - There are not enough resources to load DXE core.\r
202\r
203--*/\r
204{\r
205 EFI_STATUS Status;\r
206 EFI_PHYSICAL_ADDRESS TopOfStack;\r
207 EFI_PHYSICAL_ADDRESS BaseOfStack;\r
208 EFI_PHYSICAL_ADDRESS BspStore;\r
209 EFI_GUID DxeCoreFileName;\r
d6697471 210 EFI_GUID FirmwareFileName;\r
3005648d 211 VOID *DxeCorePe32Data;\r
d6697471 212 VOID *FvImageData; \r
3005648d 213 EFI_PHYSICAL_ADDRESS DxeCoreAddress;\r
214 UINT64 DxeCoreSize;\r
215 EFI_PHYSICAL_ADDRESS DxeCoreEntryPoint;\r
216 EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader;\r
217 EFI_BOOT_MODE BootMode;\r
218 EFI_PEI_RECOVERY_MODULE_PPI *PeiRecovery;\r
219 EFI_PEI_S3_RESUME_PPI *S3Resume;\r
220 EFI_PHYSICAL_ADDRESS PageTables;\r
221 \r
222 TopOfStack = 0;\r
223 BaseOfStack = 0;\r
224 BspStore = 0;\r
225 Status = EFI_SUCCESS;\r
226\r
227 //\r
228 // if in S3 Resume, restore configure\r
229 //\r
84a99d48 230 Status = PeiServicesGetBootMode (&BootMode);\r
3005648d 231\r
232 if (!EFI_ERROR (Status) && (BootMode == BOOT_ON_S3_RESUME)) {\r
84a99d48 233 Status = PeiServicesLocatePpi (\r
3005648d 234 &gEfiPeiS3ResumePpiGuid,\r
235 0,\r
236 NULL,\r
237 (VOID **)&S3Resume\r
238 );\r
239\r
240 ASSERT_EFI_ERROR (Status);\r
241\r
242 Status = S3Resume->S3RestoreConfig (PeiServices);\r
243\r
244 ASSERT_EFI_ERROR (Status);\r
245 }\r
246\r
247 Status = EFI_SUCCESS;\r
248\r
249 //\r
250 // Install the PEI Protocols that are shared between PEI and DXE\r
251 //\r
fc198a79 252 PeiEfiPeiPeCoffLoader = (EFI_PEI_PE_COFF_LOADER_PROTOCOL *)GetPeCoffLoaderProtocol ();\r
a61513d6 253 ASSERT (PeiEfiPeiPeCoffLoader != NULL);\r
3005648d 254\r
3005648d 255 //\r
256 // Allocate 128KB for the Stack\r
257 //\r
84a99d48 258 PeiServicesAllocatePages (EfiBootServicesData, EFI_SIZE_TO_PAGES (STACK_SIZE), &BaseOfStack);\r
3005648d 259 ASSERT (BaseOfStack != 0);\r
260\r
261 //\r
3e025b69
LG
262 // Compute the top of the stack we were allocated, which is used to load X64 dxe core. \r
263 // Pre-allocate a 32 bytes which confroms to x64 calling convention.\r
3005648d 264 //\r
9723bdde
LG
265 // The first four parameters to a function are passed in rcx, rdx, r8 and r9. \r
266 // Any further parameters are pushed on the stack. Furthermore, space (4 * 8bytes) for the \r
267 // register parameters is reserved on the stack, in case the called function \r
268 // wants to spill them; this is important if the function is variadic. \r
269 //\r
270 TopOfStack = BaseOfStack + EFI_SIZE_TO_PAGES (STACK_SIZE) * EFI_PAGE_SIZE - 32;\r
3005648d 271\r
3e025b69
LG
272 //\r
273 // X64 Calling Conventions requires that the stack must be aligned to 16 bytes\r
274 //\r
275 TopOfStack = ALIGN_POINTER (TopOfStack, 16);\r
276\r
3005648d 277 //\r
278 // Add architecture-specifc HOBs (including the BspStore HOB)\r
279 //\r
280 Status = CreateArchSpecificHobs (&BspStore);\r
281 ASSERT_EFI_ERROR (Status);\r
282\r
283 //\r
284 // See if we are in crisis recovery\r
285 //\r
84a99d48 286 Status = PeiServicesGetBootMode (&BootMode);\r
3005648d 287 if (!EFI_ERROR (Status) && (BootMode == BOOT_IN_RECOVERY_MODE)) {\r
84a99d48 288 Status = PeiServicesLocatePpi (\r
3005648d 289 &gEfiPeiRecoveryModulePpiGuid,\r
290 0,\r
291 NULL,\r
292 (VOID **)&PeiRecovery\r
293 );\r
294\r
295 ASSERT_EFI_ERROR (Status);\r
296 Status = PeiRecovery->LoadRecoveryCapsule (PeiServices, PeiRecovery);\r
f78797d5
LG
297 if (EFI_ERROR (Status)) {\r
298 DEBUG ((EFI_D_ERROR, "Load Recovery Capsule Failed.(Status = %r)\n", Status));\r
299 CpuDeadLoop ();\r
300 }\r
3005648d 301 }\r
302\r
d6697471
LG
303 //\r
304 // Find the EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE type compressed Firmware Volume file\r
305 // The file found will be processed by PeiProcessFile: It will first be decompressed to\r
306 // a normal FV, then a corresponding FV type hob will be built. \r
307 //\r
308 Status = PeiFindFile (\r
309 EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE,\r
310 EFI_SECTION_FIRMWARE_VOLUME_IMAGE,\r
311 &FirmwareFileName,\r
312 &FvImageData\r
313 );\r
314\r
3005648d 315 //\r
316 // Find the DXE Core in a Firmware Volume\r
317 //\r
318 Status = PeiFindFile (\r
319 EFI_FV_FILETYPE_DXE_CORE,\r
320 EFI_SECTION_PE32,\r
321 &DxeCoreFileName,\r
322 &DxeCorePe32Data\r
323 );\r
324 ASSERT_EFI_ERROR (Status);\r
325\r
3005648d 326 //\r
327 // Load the GDT of Go64. Since the GDT of 32-bit Tiano locates in the BS_DATA \\r
328 // memory, it may be corrupted when copying FV to high-end memory \r
329 LoadGo64Gdt();\r
330\r
331 //\r
332 // Limit to 36 bits of addressing for debug. Should get it from CPU\r
333 //\r
334 PageTables = CreateIdentityMappingPageTables (36);\r
335\r
336\r
337 //\r
338 // Load the DXE Core from a Firmware Volume\r
339 //\r
fc198a79 340 Status = PeiLoadPeImage (\r
3005648d 341 PeiEfiPeiPeCoffLoader,\r
342 DxeCorePe32Data,\r
343 EfiBootServicesData,\r
344 &DxeCoreAddress,\r
345 &DxeCoreSize,\r
346 &DxeCoreEntryPoint\r
347 );\r
348 ASSERT_EFI_ERROR (Status);\r
349\r
a61513d6
LG
350 //\r
351 // Transfer control to the DXE Core\r
352 // The handoff state is simply a pointer to the HOB list\r
353 //\r
354\r
355 Status = PeiServicesInstallPpi (&mPpiSignal);\r
356 ASSERT_EFI_ERROR (Status);\r
357\r
3005648d 358 //\r
359 //\r
360 // Add HOB for the DXE Core\r
361 //\r
362 BuildModuleHob (\r
363 &DxeCoreFileName,\r
364 DxeCoreAddress,\r
365 DxeCoreSize,\r
366 DxeCoreEntryPoint\r
367 );\r
368\r
369 //\r
370 // Report Status Code EFI_SW_PEI_PC_HANDOFF_TO_NEXT\r
371 //\r
372 REPORT_STATUS_CODE (\r
373 EFI_PROGRESS_CODE,\r
374 EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_CORE_PC_HANDOFF_TO_NEXT\r
375 );\r
376\r
377 DEBUG ((EFI_D_INFO, "DXE Core Entry\n"));\r
378 //\r
379 // Go to Long Mode. Interrupts will not get turned on until the CPU AP is loaded.\r
380 // Call x64 drivers passing in single argument, a pointer to the HOBs.\r
381 //\r
382 ActivateLongMode (\r
383 PageTables, \r
384 (EFI_PHYSICAL_ADDRESS)(UINTN)(HobList.Raw), \r
385 TopOfStack,\r
386 0x00000000,\r
387 DxeCoreEntryPoint\r
388 );\r
389\r
390 //\r
391 // If we get here, then the DXE Core returned. This is an error\r
f78797d5 392 // Dxe Core should not return.\r
3005648d 393 //\r
f78797d5
LG
394 ASSERT (FALSE);\r
395 CpuDeadLoop ();\r
3005648d 396\r
397 return EFI_OUT_OF_RESOURCES;\r
398}\r
399\r
400EFI_STATUS\r
401PeiFindFile (\r
402 IN UINT8 Type,\r
403 IN UINT16 SectionType,\r
404 OUT EFI_GUID *FileName,\r
405 OUT VOID **Pe32Data\r
406 )\r
407/*++\r
408\r
409Routine Description:\r
410\r
411 Finds a PE/COFF of a specific Type and SectionType in the Firmware Volumes\r
412 described in the HOB list. Able to search in a compression set in a FFS file.\r
413 But only one level of compression is supported, that is, not able to search\r
414 in a compression set that is within another compression set.\r
415\r
416Arguments:\r
417\r
418 Type - The Type of file to retrieve\r
419\r
420 SectionType - The type of section to retrieve from a file\r
421\r
422 FileName - The name of the file found in the Firmware Volume\r
423\r
424 Pe32Data - Pointer to the beginning of the PE/COFF file found in the Firmware Volume\r
425\r
426Returns:\r
427\r
428 EFI_SUCCESS - The file was found, and the name is returned in FileName, and a pointer to\r
429 the PE/COFF image is returned in Pe32Data\r
430\r
431 EFI_NOT_FOUND - The file was not found in the Firmware Volumes present in the HOB List\r
432\r
433--*/\r
434{\r
435 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
436 EFI_FFS_FILE_HEADER *FfsFileHeader;\r
437 VOID *SectionData;\r
438 EFI_STATUS Status;\r
439 EFI_PEI_HOB_POINTERS Hob;\r
440\r
441\r
442 FwVolHeader = NULL;\r
443 FfsFileHeader = NULL;\r
444 SectionData = NULL;\r
d6697471 445 Status = EFI_SUCCESS;\r
3005648d 446\r
447 //\r
d6697471
LG
448 // For each Firmware Volume, look for a specified type\r
449 // of file and break out until no one is found \r
3005648d 450 //\r
451 Hob.Raw = GetHobList ();\r
452 while ((Hob.Raw = GetNextHob (EFI_HOB_TYPE_FV, Hob.Raw)) != NULL) {\r
453 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (Hob.FirmwareVolume->BaseAddress);\r
84a99d48 454 Status = PeiServicesFfsFindNextFile (\r
3005648d 455 Type,\r
456 FwVolHeader,\r
457 &FfsFileHeader\r
458 );\r
459 if (!EFI_ERROR (Status)) {\r
3005648d 460 Status = PeiProcessFile (\r
461 SectionType,\r
d6697471 462 FfsFileHeader,\r
2ea767cc 463 Pe32Data,\r
464 &Hob\r
3005648d 465 );\r
6326ee4e 466 CopyMem (FileName, &FfsFileHeader->Name, sizeof (EFI_GUID));\r
d6697471
LG
467 if (!EFI_ERROR (Status)) {\r
468 return EFI_SUCCESS;\r
469 }\r
3005648d 470 }\r
471 Hob.Raw = GET_NEXT_HOB (Hob);\r
472 }\r
473 return EFI_NOT_FOUND;\r
474}\r
475\r
476EFI_STATUS\r
fc198a79 477PeiLoadPeImage (\r
3005648d 478 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader,\r
479 IN VOID *Pe32Data,\r
480 IN EFI_MEMORY_TYPE MemoryType,\r
481 OUT EFI_PHYSICAL_ADDRESS *ImageAddress,\r
482 OUT UINT64 *ImageSize,\r
483 OUT EFI_PHYSICAL_ADDRESS *EntryPoint\r
484 )\r
485/*++\r
486\r
487Routine Description:\r
488\r
489 Loads and relocates a PE/COFF image into memory.\r
490\r
491Arguments:\r
492\r
493 PeiEfiPeiPeCoffLoader - Pointer to a PE COFF loader protocol\r
494\r
495 Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated\r
496\r
497 ImageAddress - The base address of the relocated PE/COFF image\r
498\r
499 ImageSize - The size of the relocated PE/COFF image\r
500\r
501 EntryPoint - The entry point of the relocated PE/COFF image\r
502\r
503Returns:\r
504\r
505 EFI_SUCCESS - The file was loaded and relocated\r
506 EFI_OUT_OF_RESOURCES - There was not enough memory to load and relocate the PE/COFF file\r
507\r
508--*/\r
509{\r
510 EFI_STATUS Status;\r
511 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
512 EFI_PHYSICAL_ADDRESS MemoryBuffer;\r
513\r
514 ZeroMem (&ImageContext, sizeof (ImageContext));\r
515 ImageContext.Handle = Pe32Data;\r
516 Status = GetImageReadFunction (&ImageContext);\r
517\r
518 ASSERT_EFI_ERROR (Status);\r
519\r
520 Status = PeiEfiPeiPeCoffLoader->GetImageInfo (PeiEfiPeiPeCoffLoader, &ImageContext);\r
521 if (EFI_ERROR (Status)) {\r
522 return Status;\r
523 }\r
524 //\r
525 // Allocate Memory for the image\r
526 //\r
527 //\r
528 // Allocate Memory for the image\r
529 //\r
84a99d48 530 PeiServicesAllocatePages (MemoryType, EFI_SIZE_TO_PAGES ((UINT32) ImageContext.ImageSize), &MemoryBuffer);\r
3005648d 531 ImageContext.ImageAddress = MemoryBuffer;\r
532 ASSERT (ImageContext.ImageAddress != 0);\r
533\r
534 //\r
535 // Load the image to our new buffer\r
536 //\r
537\r
538 Status = PeiEfiPeiPeCoffLoader->LoadImage (PeiEfiPeiPeCoffLoader, &ImageContext);\r
539 if (EFI_ERROR (Status)) {\r
540 return Status;\r
541 }\r
542 \r
543 //\r
544 // Relocate the image in our new buffer\r
545 //\r
546 Status = PeiEfiPeiPeCoffLoader->RelocateImage (PeiEfiPeiPeCoffLoader, &ImageContext);\r
547 if (EFI_ERROR (Status)) {\r
548 return Status;\r
549 }\r
550\r
551 //\r
552 // Flush the instruction cache so the image data is written before we execute it\r
553 //\r
554 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r
555\r
556 *ImageAddress = ImageContext.ImageAddress;\r
557 *ImageSize = ImageContext.ImageSize;\r
558 *EntryPoint = ImageContext.EntryPoint;\r
559\r
560 return EFI_SUCCESS;\r
561}\r
562\r
563EFI_STATUS\r
564ShadowDxeIpl (\r
565 IN EFI_FFS_FILE_HEADER *DxeIplFileHeader,\r
566 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader\r
567 )\r
568/*++\r
569\r
570Routine Description:\r
571\r
572 Shadow the DXE IPL to a different memory location. This occurs after permanent\r
573 memory has been discovered.\r
574\r
575Arguments:\r
576\r
577 DxeIplFileHeader - Pointer to the FFS file header of the DXE IPL driver\r
578\r
579 PeiEfiPeiPeCoffLoader - Pointer to a PE COFF loader protocol\r
580\r
581Returns:\r
582\r
583 EFI_SUCCESS - DXE IPL was successfully shadowed to a different memory location.\r
584\r
585 EFI_ ERROR - The shadow was unsuccessful.\r
586\r
587\r
588--*/\r
589{\r
590 UINTN SectionLength;\r
591 UINTN OccupiedSectionLength;\r
592 EFI_PHYSICAL_ADDRESS DxeIplAddress;\r
593 UINT64 DxeIplSize;\r
594 EFI_PHYSICAL_ADDRESS DxeIplEntryPoint;\r
595 EFI_STATUS Status;\r
596 EFI_COMMON_SECTION_HEADER *Section;\r
597\r
598 Section = (EFI_COMMON_SECTION_HEADER *) (DxeIplFileHeader + 1);\r
599\r
600 while ((Section->Type != EFI_SECTION_PE32) && (Section->Type != EFI_SECTION_TE)) {\r
601 SectionLength = *(UINT32 *) (Section->Size) & 0x00ffffff;\r
602 OccupiedSectionLength = GetOccupiedSize (SectionLength, 4);\r
603 Section = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) Section + OccupiedSectionLength);\r
604 }\r
605 \r
606 //\r
607 // Relocate DxeIpl into memory by using loadfile service\r
608 //\r
fc198a79 609 Status = PeiLoadPeImage (\r
3005648d 610 PeiEfiPeiPeCoffLoader,\r
611 (VOID *) (Section + 1),\r
612 EfiBootServicesData,\r
613 &DxeIplAddress,\r
614 &DxeIplSize,\r
615 &DxeIplEntryPoint\r
616 );\r
617 \r
618 if (Status == EFI_SUCCESS) {\r
619 //\r
620 // Install PeiInMemory to indicate the Dxeipl is shadowed\r
621 //\r
84a99d48 622 Status = PeiServicesInstallPpi (&mPpiPeiInMemory);\r
3005648d 623\r
624 if (EFI_ERROR (Status)) {\r
625 return Status;\r
626 }\r
627\r
628 Status = ((EFI_PEIM_ENTRY_POINT) (UINTN) DxeIplEntryPoint) (DxeIplFileHeader, GetPeiServicesTablePointer());\r
629 }\r
630\r
631 return Status;\r
632}\r
633\r
634EFI_STATUS\r
635EFIAPI\r
636DxeIplLoadFile (\r
637 IN EFI_PEI_FV_FILE_LOADER_PPI *This,\r
638 IN EFI_FFS_FILE_HEADER *FfsHeader,\r
639 OUT EFI_PHYSICAL_ADDRESS *ImageAddress,\r
640 OUT UINT64 *ImageSize,\r
641 OUT EFI_PHYSICAL_ADDRESS *EntryPoint\r
642 )\r
643/*++\r
644\r
645Routine Description:\r
646\r
647 Given a pointer to an FFS file containing a PE32 image, get the\r
648 information on the PE32 image, and then "load" it so that it\r
649 can be executed.\r
650\r
651Arguments:\r
652\r
653 This - pointer to our file loader protocol\r
654 FfsHeader - pointer to the FFS file header of the FFS file that\r
655 contains the PE32 image we want to load\r
656 ImageAddress - returned address where the PE32 image is loaded\r
657 ImageSize - returned size of the loaded PE32 image\r
658 EntryPoint - entry point to the loaded PE32 image\r
659\r
660Returns:\r
661 \r
662 EFI_SUCCESS - The FFS file was successfully loaded.\r
663 EFI_ERROR - Unable to load the FFS file.\r
664\r
665--*/\r
666{\r
667 EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader;\r
668 EFI_STATUS Status;\r
669 VOID *Pe32Data;\r
670\r
671 Pe32Data = NULL;\r
672 PeiEfiPeiPeCoffLoader = (EFI_PEI_PE_COFF_LOADER_PROTOCOL *)GetPeCoffLoaderProtocol ();\r
673\r
674 //\r
675 // Preprocess the FFS file to get a pointer to the PE32 information\r
676 // in the enclosed PE32 image.\r
677 //\r
678 Status = PeiProcessFile (\r
679 EFI_SECTION_PE32,\r
d6697471 680 FfsHeader,\r
2ea767cc 681 &Pe32Data,\r
682 NULL\r
3005648d 683 );\r
684\r
685 if (EFI_ERROR (Status)) {\r
686 return Status;\r
687 }\r
688 //\r
689 // Load the PE image from the FFS file\r
690 //\r
fc198a79 691 Status = PeiLoadPeImage (\r
3005648d 692 PeiEfiPeiPeCoffLoader,\r
693 Pe32Data,\r
694 EfiBootServicesData,\r
695 ImageAddress,\r
696 ImageSize,\r
697 EntryPoint\r
698 );\r
699\r
700 return Status;\r
701}\r
702\r
703EFI_STATUS\r
704PeiProcessFile (\r
6326ee4e 705 IN UINT16 SectionType,\r
d6697471 706 IN EFI_FFS_FILE_HEADER *FfsFileHeader,\r
2ea767cc 707 OUT VOID **Pe32Data,\r
708 IN EFI_PEI_HOB_POINTERS *OrigHob\r
3005648d 709 )\r
710/*++\r
711\r
712Routine Description:\r
713\r
714Arguments:\r
715\r
716 SectionType - The type of section in the FFS file to process.\r
717\r
718 FfsFileHeader - Pointer to the FFS file to process, looking for the\r
719 specified SectionType\r
720\r
721 Pe32Data - returned pointer to the start of the PE32 image found\r
722 in the FFS file.\r
723\r
724Returns:\r
725\r
726 EFI_SUCCESS - found the PE32 section in the FFS file\r
727\r
728--*/\r
729{\r
730 EFI_STATUS Status;\r
731 VOID *SectionData;\r
732 DECOMPRESS_LIBRARY *DecompressLibrary;\r
733 UINT8 *DstBuffer;\r
734 UINT8 *ScratchBuffer;\r
735 UINT32 DstBufferSize;\r
736 UINT32 ScratchBufferSize;\r
737 EFI_COMMON_SECTION_HEADER *CmpSection;\r
738 UINTN CmpSectionLength;\r
739 UINTN OccupiedCmpSectionLength;\r
740 VOID *CmpFileData;\r
741 UINTN CmpFileSize;\r
742 EFI_COMMON_SECTION_HEADER *Section;\r
743 UINTN SectionLength;\r
744 UINTN OccupiedSectionLength;\r
745 UINT64 FileSize;\r
746 EFI_GUID_DEFINED_SECTION *GuidedSectionHeader;\r
747 UINT32 AuthenticationStatus;\r
748 EFI_PEI_SECTION_EXTRACTION_PPI *SectionExtract;\r
749 UINT32 BufferSize;\r
750 UINT8 *Buffer;\r
751 EFI_PEI_SECURITY_PPI *Security;\r
752 BOOLEAN StartCrisisRecovery;\r
753 EFI_GUID TempGuid;\r
754 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;\r
755 EFI_COMPRESSION_SECTION *CompressionSection;\r
d6697471 756 UINT32 FvAlignment;\r
3005648d 757\r
84a99d48 758 Status = PeiServicesFfsFindSectionData (\r
3005648d 759 EFI_SECTION_COMPRESSION,\r
760 FfsFileHeader,\r
761 &SectionData\r
762 );\r
763\r
764 //\r
d6697471 765 // First process the compression section\r
3005648d 766 //\r
767 if (!EFI_ERROR (Status)) {\r
768 //\r
769 // Yes, there is a compression section, so extract the contents\r
770 // Decompress the image here\r
771 //\r
772 Section = (EFI_COMMON_SECTION_HEADER *) (UINTN) (VOID *) ((UINT8 *) (FfsFileHeader) + (UINTN) sizeof (EFI_FFS_FILE_HEADER));\r
773\r
774 do {\r
775 SectionLength = *(UINT32 *) (Section->Size) & 0x00ffffff;\r
776 OccupiedSectionLength = GetOccupiedSize (SectionLength, 4);\r
777\r
778 //\r
779 // Was the DXE Core file encapsulated in a GUID'd section?\r
780 //\r
781 if (Section->Type == EFI_SECTION_GUID_DEFINED) {\r
782 //\r
783 // Locate the GUID'd Section Extractor\r
784 //\r
785 GuidedSectionHeader = (VOID *) (Section + 1);\r
786\r
787 //\r
788 // This following code constitutes the addition of the security model\r
789 // to the DXE IPL.\r
790 //\r
791 //\r
792 // Set a default authenticatino state\r
793 //\r
794 AuthenticationStatus = 0;\r
795\r
84a99d48 796 Status = PeiServicesLocatePpi (\r
3005648d 797 &gEfiPeiSectionExtractionPpiGuid,\r
798 0,\r
799 NULL,\r
800 (VOID **)&SectionExtract\r
801 );\r
802\r
803 if (EFI_ERROR (Status)) {\r
804 return Status;\r
805 }\r
806 //\r
807 // Verify Authentication State\r
808 //\r
809 CopyMem (&TempGuid, Section + 1, sizeof (EFI_GUID));\r
810\r
811 Status = SectionExtract->PeiGetSection (\r
812 GetPeiServicesTablePointer(),\r
813 SectionExtract,\r
814 (EFI_SECTION_TYPE *) &SectionType,\r
815 &TempGuid,\r
816 0,\r
817 (VOID **) &Buffer,\r
818 &BufferSize,\r
819 &AuthenticationStatus\r
820 );\r
821\r
822 if (EFI_ERROR (Status)) {\r
823 return Status;\r
824 }\r
825 //\r
826 // If not ask the Security PPI, if exists, for disposition\r
827 //\r
828 //\r
84a99d48 829 Status = PeiServicesLocatePpi (\r
3005648d 830 &gEfiPeiSecurityPpiGuid,\r
831 0,\r
832 NULL,\r
833 (VOID **)&Security\r
834 );\r
835 if (EFI_ERROR (Status)) {\r
836 return Status;\r
837 }\r
838\r
839 Status = Security->AuthenticationState (\r
840 GetPeiServicesTablePointer(),\r
841 (struct _EFI_PEI_SECURITY_PPI *) Security,\r
842 AuthenticationStatus,\r
843 FfsFileHeader,\r
844 &StartCrisisRecovery\r
845 );\r
846\r
847 if (EFI_ERROR (Status)) {\r
848 return Status;\r
849 }\r
850 //\r
851 // If there is a security violation, report to caller and have\r
852 // the upper-level logic possible engender a crisis recovery\r
853 //\r
854 if (StartCrisisRecovery) {\r
855 return EFI_SECURITY_VIOLATION;\r
856 }\r
857 }\r
858\r
859 if (Section->Type == EFI_SECTION_PE32) {\r
860 //\r
861 // This is what we want\r
862 //\r
863 *Pe32Data = (VOID *) (Section + 1);\r
864 return EFI_SUCCESS;\r
865 } else if (Section->Type == EFI_SECTION_COMPRESSION) {\r
866 //\r
867 // This is a compression set, expand it\r
868 //\r
869 CompressionSection = (EFI_COMPRESSION_SECTION *) Section;\r
870\r
871 switch (CompressionSection->CompressionType) {\r
872 case EFI_STANDARD_COMPRESSION:\r
e29d7096 873 if (FeaturePcdGet (PcdDxeIplSupportTianoDecompress)) {\r
874 DecompressLibrary = &gTianoDecompress;\r
875 } else {\r
876 ASSERT (FALSE);\r
877 return EFI_NOT_FOUND;\r
878 }\r
3005648d 879 break;\r
880\r
881 case EFI_CUSTOMIZED_COMPRESSION:\r
882 //\r
883 // Load user customized compression protocol.\r
884 //\r
e29d7096 885 if (FeaturePcdGet (PcdDxeIplSupportCustomDecompress)) {\r
886 DecompressLibrary = &gCustomDecompress;\r
887 } else {\r
888 ASSERT (FALSE);\r
889 return EFI_NOT_FOUND;\r
890 }\r
3005648d 891 break;\r
892\r
893 case EFI_NOT_COMPRESSED:\r
894 default:\r
895 //\r
896 // Need to support not compressed file\r
897 //\r
898 ASSERT_EFI_ERROR (Status);\r
899 return EFI_NOT_FOUND;\r
900 }\r
901\r
902 Status = DecompressLibrary->GetInfo (\r
903 (UINT8 *) ((EFI_COMPRESSION_SECTION *) Section + 1),\r
904 (UINT32) SectionLength - sizeof (EFI_COMPRESSION_SECTION),\r
905 &DstBufferSize,\r
906 &ScratchBufferSize\r
907 );\r
908 if (EFI_ERROR (Status)) {\r
909 //\r
910 // GetInfo failed\r
911 //\r
912 return EFI_NOT_FOUND;\r
913 }\r
914\r
915 //\r
916 // Allocate scratch buffer\r
917 //\r
918 ScratchBuffer = AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize));\r
919 if (ScratchBuffer == NULL) {\r
920 return EFI_OUT_OF_RESOURCES;\r
921 }\r
922\r
923 //\r
924 // Allocate destination buffer\r
925 //\r
926 DstBuffer = AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize));\r
927 if (DstBuffer == NULL) {\r
928 return EFI_OUT_OF_RESOURCES;\r
929 }\r
930\r
931 //\r
932 // Call decompress function\r
933 //\r
934 Status = DecompressLibrary->Decompress (\r
935 (CHAR8 *) ((EFI_COMPRESSION_SECTION *) Section + 1),\r
936 DstBuffer,\r
937 ScratchBuffer\r
938 );\r
939\r
940 CmpSection = (EFI_COMMON_SECTION_HEADER *) DstBuffer;\r
d6697471
LG
941 if (CmpSection->Type == EFI_SECTION_FIRMWARE_VOLUME_IMAGE) {\r
942 // \r
943 // Firmware Volume Image in this Section\r
944 // Skip the section header to get FvHeader\r
3005648d 945 //\r
d6697471 946 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (CmpSection + 1);\r
3005648d 947\r
d6697471
LG
948 if (FvHeader->Signature == EFI_FVH_SIGNATURE) { \r
949 //\r
950 // Adjust Fv Base Address Alignment based on Align Attributes in Fv Header\r
951 //\r
952 \r
953 //\r
954 // When FvImage support Alignment, we need to check whether \r
955 // its alignment is correct. \r
956 //\r
957 if (FvHeader->Attributes | EFI_FVB_ALIGNMENT_CAP) {\r
958 \r
959 //\r
960 // Calculate the mini alignment for this FvImage\r
961 //\r
962 FvAlignment = 1 << (LowBitSet32 (FvHeader->Attributes >> 16) + 1);\r
963 \r
964 //\r
965 // If current FvImage base address doesn't meet the its alignment,\r
966 // we need to reload this FvImage to another correct memory address.\r
967 //\r
968 if (((UINTN) FvHeader % FvAlignment) != 0) {\r
969 DstBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES ((UINTN) FvHeader->FvLength), FvAlignment);\r
970 if (DstBuffer == NULL) {\r
971 return EFI_OUT_OF_RESOURCES;\r
972 }\r
973 CopyMem (DstBuffer, FvHeader, (UINTN) FvHeader->FvLength);\r
974 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) DstBuffer; \r
975 }\r
3005648d 976 }\r
d6697471
LG
977 //\r
978 // Build new FvHob for new decompressed Fv image.\r
979 //\r
980 BuildFvHob ((EFI_PHYSICAL_ADDRESS) (UINTN) FvHeader, FvHeader->FvLength);\r
981 \r
982 //\r
983 // Set the original FvHob to unused.\r
984 //\r
2ea767cc 985 if (OrigHob != NULL) {\r
2ea767cc 986 OrigHob->Header->HobType = EFI_HOB_TYPE_UNUSED;\r
987 }\r
988 \r
6326ee4e 989 //\r
d6697471 990 // when search FvImage Section return true.\r
6326ee4e 991 //\r
d6697471
LG
992 if (SectionType == EFI_SECTION_FIRMWARE_VOLUME_IMAGE) {\r
993 *Pe32Data = (VOID *) FvHeader;\r
994 return EFI_SUCCESS;\r
995 } else {\r
996 return EFI_NOT_FOUND;\r
997 }\r
998\r
3005648d 999 }\r
1000 }\r
1001 //\r
1002 // Decompress successfully.\r
1003 // Loop the decompressed data searching for expected section.\r
1004 //\r
1005 CmpFileData = (VOID *) DstBuffer;\r
1006 CmpFileSize = DstBufferSize;\r
1007 do {\r
1008 CmpSectionLength = *(UINT32 *) (CmpSection->Size) & 0x00ffffff;\r
1009 if (CmpSection->Type == EFI_SECTION_PE32) {\r
1010 //\r
1011 // This is what we want\r
1012 //\r
1013 *Pe32Data = (VOID *) (CmpSection + 1);\r
1014 return EFI_SUCCESS;\r
1015 }\r
1016\r
1017 OccupiedCmpSectionLength = GetOccupiedSize (CmpSectionLength, 4);\r
1018 CmpSection = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) CmpSection + OccupiedCmpSectionLength);\r
1019 } while (CmpSection->Type != 0 && (UINTN) ((UINT8 *) CmpSection - (UINT8 *) CmpFileData) < CmpFileSize);\r
1020 }\r
d6697471
LG
1021 //\r
1022 // End of the decompression activity\r
1023 //\r
3005648d 1024\r
1025 Section = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) Section + OccupiedSectionLength);\r
1026 FileSize = FfsFileHeader->Size[0] & 0xFF;\r
1027 FileSize += (FfsFileHeader->Size[1] << 8) & 0xFF00;\r
1028 FileSize += (FfsFileHeader->Size[2] << 16) & 0xFF0000;\r
1029 FileSize &= 0x00FFFFFF;\r
1030 } while (Section->Type != 0 && (UINTN) ((UINT8 *) Section - (UINT8 *) FfsFileHeader) < FileSize);\r
d6697471 1031 \r
3005648d 1032 //\r
d6697471
LG
1033 // search all sections (compression and non compression) in this FFS, don't \r
1034 // find expected section.\r
3005648d 1035 //\r
d6697471 1036 return EFI_NOT_FOUND;\r
3005648d 1037 } else {\r
d6697471
LG
1038 //\r
1039 // For those FFS that doesn't contain compression section, directly search \r
1040 // PE or TE section in this FFS.\r
1041 //\r
3005648d 1042\r
84a99d48 1043 Status = PeiServicesFfsFindSectionData (\r
3005648d 1044 EFI_SECTION_PE32,\r
1045 FfsFileHeader,\r
1046 &SectionData\r
1047 );\r
1048\r
1049 if (EFI_ERROR (Status)) {\r
84a99d48 1050 Status = PeiServicesFfsFindSectionData (\r
3005648d 1051 EFI_SECTION_TE,\r
1052 FfsFileHeader,\r
1053 &SectionData\r
1054 );\r
1055 if (EFI_ERROR (Status)) {\r
1056 return Status;\r
1057 }\r
1058 }\r
1059 }\r
1060\r
1061 *Pe32Data = SectionData;\r
1062\r
1063 return EFI_SUCCESS;\r
878ddf1f 1064}