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