]> git.proxmox.com Git - mirror_edk2.git/blob - EdkModulePkg/Core/DxeIplX64Peim/DxeLoadX64.c
Identify SupArch for .msa
[mirror_edk2.git] / EdkModulePkg / Core / DxeIplX64Peim / 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 = PeiServicesGetBootMode (&BootMode);
134
135 ASSERT_EFI_ERROR (Status);
136
137 Status = PeiServicesLocatePpi (
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 = PeiServicesInstallPpi (&mPpiLoadFile);
169
170 if (EFI_ERROR (Status)) {
171 return Status;
172 }
173 }
174 //
175 // Install DxeIpl PPI
176 //
177 PeiServicesInstallPpi (&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 EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader;
224 EFI_BOOT_MODE BootMode;
225 EFI_PEI_RECOVERY_MODULE_PPI *PeiRecovery;
226 EFI_PEI_S3_RESUME_PPI *S3Resume;
227 EFI_PHYSICAL_ADDRESS PageTables;
228
229 TopOfStack = 0;
230 BaseOfStack = 0;
231 BspStore = 0;
232 Status = EFI_SUCCESS;
233
234 //
235 // if in S3 Resume, restore configure
236 //
237 Status = PeiServicesGetBootMode (&BootMode);
238
239 if (!EFI_ERROR (Status) && (BootMode == BOOT_ON_S3_RESUME)) {
240 Status = PeiServicesLocatePpi (
241 &gEfiPeiS3ResumePpiGuid,
242 0,
243 NULL,
244 (VOID **)&S3Resume
245 );
246
247 ASSERT_EFI_ERROR (Status);
248
249 Status = S3Resume->S3RestoreConfig (PeiServices);
250
251 ASSERT_EFI_ERROR (Status);
252 }
253
254 Status = EFI_SUCCESS;
255
256 //
257 // Install the PEI Protocols that are shared between PEI and DXE
258 //
259 #ifdef EFI_NT_EMULATOR
260 PeiEfiPeiPeCoffLoader = (EFI_PEI_PE_COFF_LOADER_PROTOCOL *)GetPeCoffLoaderProtocol ();
261 ASSERT (PeiEfiPeiPeCoffLoader != NULL);
262 #else
263 PeiEfiPeiPeCoffLoader = (EFI_PEI_PE_COFF_LOADER_PROTOCOL *)GetPeCoffLoaderX64Protocol ();
264 #endif
265
266 //
267 // Allocate 128KB for the Stack
268 //
269 PeiServicesAllocatePages (EfiBootServicesData, EFI_SIZE_TO_PAGES (STACK_SIZE), &BaseOfStack);
270 ASSERT (BaseOfStack != 0);
271
272 //
273 // Compute the top of the stack we were allocated. Pre-allocate a 32 bytes
274 // for safety (PpisNeededByDxe and DxeCore).
275 //
276 TopOfStack = BaseOfStack + EFI_SIZE_TO_PAGES (STACK_SIZE) * EFI_PAGE_SIZE - 32;
277
278 //
279 // Add architecture-specifc HOBs (including the BspStore HOB)
280 //
281 Status = CreateArchSpecificHobs (&BspStore);
282 ASSERT_EFI_ERROR (Status);
283
284 //
285 // See if we are in crisis recovery
286 //
287 Status = PeiServicesGetBootMode (&BootMode);
288 if (!EFI_ERROR (Status) && (BootMode == BOOT_IN_RECOVERY_MODE)) {
289 Status = PeiServicesLocatePpi (
290 &gEfiPeiRecoveryModulePpiGuid,
291 0,
292 NULL,
293 (VOID **)&PeiRecovery
294 );
295
296 ASSERT_EFI_ERROR (Status);
297 Status = PeiRecovery->LoadRecoveryCapsule (PeiServices, PeiRecovery);
298 ASSERT_EFI_ERROR (Status);
299 }
300
301 //
302 // Find the DXE Core in a Firmware Volume
303 //
304 Status = PeiFindFile (
305 EFI_FV_FILETYPE_DXE_CORE,
306 EFI_SECTION_PE32,
307 &DxeCoreFileName,
308 &DxeCorePe32Data
309 );
310 ASSERT_EFI_ERROR (Status);
311
312 //
313 // Transfer control to the DXE Core
314 // The handoff state is simply a pointer to the HOB list
315 //
316 // PEI_PERF_END (PeiServices, L"DxeIpl", NULL, 0);
317
318 Status = PeiServicesInstallPpi (&mPpiSignal);
319 ASSERT_EFI_ERROR (Status);
320
321 //
322 // Load the GDT of Go64. Since the GDT of 32-bit Tiano locates in the BS_DATA \
323 // memory, it may be corrupted when copying FV to high-end memory
324 LoadGo64Gdt();
325
326 //
327 // Limit to 36 bits of addressing for debug. Should get it from CPU
328 //
329 PageTables = CreateIdentityMappingPageTables (36);
330
331
332 //
333 // Load the DXE Core from a Firmware Volume
334 //
335 Status = PeiLoadx64File (
336 PeiEfiPeiPeCoffLoader,
337 DxeCorePe32Data,
338 EfiBootServicesData,
339 &DxeCoreAddress,
340 &DxeCoreSize,
341 &DxeCoreEntryPoint
342 );
343 ASSERT_EFI_ERROR (Status);
344
345 //
346 //
347 // Add HOB for the DXE Core
348 //
349 BuildModuleHob (
350 &DxeCoreFileName,
351 DxeCoreAddress,
352 DxeCoreSize,
353 DxeCoreEntryPoint
354 );
355
356 //
357 // Report Status Code EFI_SW_PEI_PC_HANDOFF_TO_NEXT
358 //
359 REPORT_STATUS_CODE (
360 EFI_PROGRESS_CODE,
361 EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_CORE_PC_HANDOFF_TO_NEXT
362 );
363
364 DEBUG ((EFI_D_INFO, "DXE Core Entry\n"));
365 //
366 // Go to Long Mode. Interrupts will not get turned on until the CPU AP is loaded.
367 // Call x64 drivers passing in single argument, a pointer to the HOBs.
368 //
369 ActivateLongMode (
370 PageTables,
371 (EFI_PHYSICAL_ADDRESS)(UINTN)(HobList.Raw),
372 TopOfStack,
373 0x00000000,
374 DxeCoreEntryPoint
375 );
376
377 //
378 // If we get here, then the DXE Core returned. This is an error
379 //
380 ASSERT_EFI_ERROR (Status);
381
382 return EFI_OUT_OF_RESOURCES;
383 }
384
385 EFI_STATUS
386 PeiFindFile (
387 IN UINT8 Type,
388 IN UINT16 SectionType,
389 OUT EFI_GUID *FileName,
390 OUT VOID **Pe32Data
391 )
392 /*++
393
394 Routine Description:
395
396 Finds a PE/COFF of a specific Type and SectionType in the Firmware Volumes
397 described in the HOB list. Able to search in a compression set in a FFS file.
398 But only one level of compression is supported, that is, not able to search
399 in a compression set that is within another compression set.
400
401 Arguments:
402
403 Type - The Type of file to retrieve
404
405 SectionType - The type of section to retrieve from a file
406
407 FileName - The name of the file found in the Firmware Volume
408
409 Pe32Data - Pointer to the beginning of the PE/COFF file found in the Firmware Volume
410
411 Returns:
412
413 EFI_SUCCESS - The file was found, and the name is returned in FileName, and a pointer to
414 the PE/COFF image is returned in Pe32Data
415
416 EFI_NOT_FOUND - The file was not found in the Firmware Volumes present in the HOB List
417
418 --*/
419 {
420 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
421 EFI_FFS_FILE_HEADER *FfsFileHeader;
422 VOID *SectionData;
423 EFI_STATUS Status;
424 EFI_PEI_HOB_POINTERS Hob;
425
426
427 FwVolHeader = NULL;
428 FfsFileHeader = NULL;
429 SectionData = NULL;
430
431 //
432 // Foreach Firmware Volume, look for a specified type
433 // of file and break out when one is found
434 //
435 Hob.Raw = GetHobList ();
436 while ((Hob.Raw = GetNextHob (EFI_HOB_TYPE_FV, Hob.Raw)) != NULL) {
437 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (Hob.FirmwareVolume->BaseAddress);
438 Status = PeiServicesFfsFindNextFile (
439 Type,
440 FwVolHeader,
441 &FfsFileHeader
442 );
443 if (!EFI_ERROR (Status)) {
444 Status = PeiProcessFile (
445 SectionType,
446 &FfsFileHeader,
447 Pe32Data
448 );
449 CopyMem (FileName, &FfsFileHeader->Name, sizeof (EFI_GUID));
450 return Status;
451 }
452 Hob.Raw = GET_NEXT_HOB (Hob);
453 }
454 return EFI_NOT_FOUND;
455 }
456
457 EFI_STATUS
458 PeiLoadx64File (
459 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader,
460 IN VOID *Pe32Data,
461 IN EFI_MEMORY_TYPE MemoryType,
462 OUT EFI_PHYSICAL_ADDRESS *ImageAddress,
463 OUT UINT64 *ImageSize,
464 OUT EFI_PHYSICAL_ADDRESS *EntryPoint
465 )
466 /*++
467
468 Routine Description:
469
470 Loads and relocates a PE/COFF image into memory.
471
472 Arguments:
473
474 PeiEfiPeiPeCoffLoader - Pointer to a PE COFF loader protocol
475
476 Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated
477
478 ImageAddress - The base address of the relocated PE/COFF image
479
480 ImageSize - The size of the relocated PE/COFF image
481
482 EntryPoint - The entry point of the relocated PE/COFF image
483
484 Returns:
485
486 EFI_SUCCESS - The file was loaded and relocated
487 EFI_OUT_OF_RESOURCES - There was not enough memory to load and relocate the PE/COFF file
488
489 --*/
490 {
491 EFI_STATUS Status;
492 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
493 EFI_PHYSICAL_ADDRESS MemoryBuffer;
494
495 ZeroMem (&ImageContext, sizeof (ImageContext));
496 ImageContext.Handle = Pe32Data;
497 Status = GetImageReadFunction (&ImageContext);
498
499 ASSERT_EFI_ERROR (Status);
500
501 Status = PeiEfiPeiPeCoffLoader->GetImageInfo (PeiEfiPeiPeCoffLoader, &ImageContext);
502 if (EFI_ERROR (Status)) {
503 return Status;
504 }
505 //
506 // Allocate Memory for the image
507 //
508 //
509 // Allocate Memory for the image
510 //
511 PeiServicesAllocatePages (MemoryType, EFI_SIZE_TO_PAGES ((UINT32) ImageContext.ImageSize), &MemoryBuffer);
512 ImageContext.ImageAddress = MemoryBuffer;
513 ASSERT (ImageContext.ImageAddress != 0);
514
515 //
516 // Load the image to our new buffer
517 //
518
519 Status = PeiEfiPeiPeCoffLoader->LoadImage (PeiEfiPeiPeCoffLoader, &ImageContext);
520 if (EFI_ERROR (Status)) {
521 return Status;
522 }
523
524 //
525 // Relocate the image in our new buffer
526 //
527 Status = PeiEfiPeiPeCoffLoader->RelocateImage (PeiEfiPeiPeCoffLoader, &ImageContext);
528 if (EFI_ERROR (Status)) {
529 return Status;
530 }
531
532 //
533 // Flush the instruction cache so the image data is written before we execute it
534 //
535 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);
536
537 *ImageAddress = ImageContext.ImageAddress;
538 *ImageSize = ImageContext.ImageSize;
539 *EntryPoint = ImageContext.EntryPoint;
540
541 return EFI_SUCCESS;
542 }
543
544 EFI_STATUS
545 ShadowDxeIpl (
546 IN EFI_FFS_FILE_HEADER *DxeIplFileHeader,
547 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader
548 )
549 /*++
550
551 Routine Description:
552
553 Shadow the DXE IPL to a different memory location. This occurs after permanent
554 memory has been discovered.
555
556 Arguments:
557
558 DxeIplFileHeader - Pointer to the FFS file header of the DXE IPL driver
559
560 PeiEfiPeiPeCoffLoader - Pointer to a PE COFF loader protocol
561
562 Returns:
563
564 EFI_SUCCESS - DXE IPL was successfully shadowed to a different memory location.
565
566 EFI_ ERROR - The shadow was unsuccessful.
567
568
569 --*/
570 {
571 UINTN SectionLength;
572 UINTN OccupiedSectionLength;
573 EFI_PHYSICAL_ADDRESS DxeIplAddress;
574 UINT64 DxeIplSize;
575 EFI_PHYSICAL_ADDRESS DxeIplEntryPoint;
576 EFI_STATUS Status;
577 EFI_COMMON_SECTION_HEADER *Section;
578
579 Section = (EFI_COMMON_SECTION_HEADER *) (DxeIplFileHeader + 1);
580
581 while ((Section->Type != EFI_SECTION_PE32) && (Section->Type != EFI_SECTION_TE)) {
582 SectionLength = *(UINT32 *) (Section->Size) & 0x00ffffff;
583 OccupiedSectionLength = GetOccupiedSize (SectionLength, 4);
584 Section = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) Section + OccupiedSectionLength);
585 }
586
587 //
588 // Relocate DxeIpl into memory by using loadfile service
589 //
590 Status = PeiLoadx64File (
591 PeiEfiPeiPeCoffLoader,
592 (VOID *) (Section + 1),
593 EfiBootServicesData,
594 &DxeIplAddress,
595 &DxeIplSize,
596 &DxeIplEntryPoint
597 );
598
599 if (Status == EFI_SUCCESS) {
600 //
601 // Install PeiInMemory to indicate the Dxeipl is shadowed
602 //
603 Status = PeiServicesInstallPpi (&mPpiPeiInMemory);
604
605 if (EFI_ERROR (Status)) {
606 return Status;
607 }
608
609 Status = ((EFI_PEIM_ENTRY_POINT) (UINTN) DxeIplEntryPoint) (DxeIplFileHeader, GetPeiServicesTablePointer());
610 }
611
612 return Status;
613 }
614
615 EFI_STATUS
616 EFIAPI
617 DxeIplLoadFile (
618 IN EFI_PEI_FV_FILE_LOADER_PPI *This,
619 IN EFI_FFS_FILE_HEADER *FfsHeader,
620 OUT EFI_PHYSICAL_ADDRESS *ImageAddress,
621 OUT UINT64 *ImageSize,
622 OUT EFI_PHYSICAL_ADDRESS *EntryPoint
623 )
624 /*++
625
626 Routine Description:
627
628 Given a pointer to an FFS file containing a PE32 image, get the
629 information on the PE32 image, and then "load" it so that it
630 can be executed.
631
632 Arguments:
633
634 This - pointer to our file loader protocol
635 FfsHeader - pointer to the FFS file header of the FFS file that
636 contains the PE32 image we want to load
637 ImageAddress - returned address where the PE32 image is loaded
638 ImageSize - returned size of the loaded PE32 image
639 EntryPoint - entry point to the loaded PE32 image
640
641 Returns:
642
643 EFI_SUCCESS - The FFS file was successfully loaded.
644 EFI_ERROR - Unable to load the FFS file.
645
646 --*/
647 {
648 EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader;
649 EFI_STATUS Status;
650 VOID *Pe32Data;
651
652 Pe32Data = NULL;
653 PeiEfiPeiPeCoffLoader = (EFI_PEI_PE_COFF_LOADER_PROTOCOL *)GetPeCoffLoaderProtocol ();
654
655 //
656 // Preprocess the FFS file to get a pointer to the PE32 information
657 // in the enclosed PE32 image.
658 //
659 Status = PeiProcessFile (
660 EFI_SECTION_PE32,
661 &FfsHeader,
662 &Pe32Data
663 );
664
665 if (EFI_ERROR (Status)) {
666 return Status;
667 }
668 //
669 // Load the PE image from the FFS file
670 //
671 Status = PeiLoadx64File (
672 PeiEfiPeiPeCoffLoader,
673 Pe32Data,
674 EfiBootServicesData,
675 ImageAddress,
676 ImageSize,
677 EntryPoint
678 );
679
680 return Status;
681 }
682
683 EFI_STATUS
684 PeiProcessFile (
685 IN UINT16 SectionType,
686 IN OUT EFI_FFS_FILE_HEADER **RealFfsFileHeader,
687 OUT VOID **Pe32Data
688 )
689 /*++
690
691 Routine Description:
692
693 Arguments:
694
695 SectionType - The type of section in the FFS file to process.
696
697 FfsFileHeader - Pointer to the FFS file to process, looking for the
698 specified SectionType
699
700 Pe32Data - returned pointer to the start of the PE32 image found
701 in the FFS file.
702
703 Returns:
704
705 EFI_SUCCESS - found the PE32 section in the FFS file
706
707 --*/
708 {
709 EFI_STATUS Status;
710 VOID *SectionData;
711 DECOMPRESS_LIBRARY *DecompressLibrary;
712 UINT8 *DstBuffer;
713 UINT8 *ScratchBuffer;
714 UINT32 DstBufferSize;
715 UINT32 ScratchBufferSize;
716 EFI_COMMON_SECTION_HEADER *CmpSection;
717 UINTN CmpSectionLength;
718 UINTN OccupiedCmpSectionLength;
719 VOID *CmpFileData;
720 UINTN CmpFileSize;
721 EFI_COMMON_SECTION_HEADER *Section;
722 UINTN SectionLength;
723 UINTN OccupiedSectionLength;
724 UINT64 FileSize;
725 EFI_GUID_DEFINED_SECTION *GuidedSectionHeader;
726 UINT32 AuthenticationStatus;
727 EFI_PEI_SECTION_EXTRACTION_PPI *SectionExtract;
728 UINT32 BufferSize;
729 UINT8 *Buffer;
730 EFI_PEI_SECURITY_PPI *Security;
731 BOOLEAN StartCrisisRecovery;
732 EFI_GUID TempGuid;
733 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;
734 EFI_COMPRESSION_SECTION *CompressionSection;
735 EFI_FFS_FILE_HEADER *FfsFileHeader;
736
737 FfsFileHeader = *RealFfsFileHeader;
738
739 Status = PeiServicesFfsFindSectionData (
740 EFI_SECTION_COMPRESSION,
741 FfsFileHeader,
742 &SectionData
743 );
744
745 //
746 // Upon finding a DXE Core file, see if there is first a compression section
747 //
748 if (!EFI_ERROR (Status)) {
749 //
750 // Yes, there is a compression section, so extract the contents
751 // Decompress the image here
752 //
753 Section = (EFI_COMMON_SECTION_HEADER *) (UINTN) (VOID *) ((UINT8 *) (FfsFileHeader) + (UINTN) sizeof (EFI_FFS_FILE_HEADER));
754
755 do {
756 SectionLength = *(UINT32 *) (Section->Size) & 0x00ffffff;
757 OccupiedSectionLength = GetOccupiedSize (SectionLength, 4);
758
759 //
760 // Was the DXE Core file encapsulated in a GUID'd section?
761 //
762 if (Section->Type == EFI_SECTION_GUID_DEFINED) {
763 //
764 // Locate the GUID'd Section Extractor
765 //
766 GuidedSectionHeader = (VOID *) (Section + 1);
767
768 //
769 // This following code constitutes the addition of the security model
770 // to the DXE IPL.
771 //
772 //
773 // Set a default authenticatino state
774 //
775 AuthenticationStatus = 0;
776
777 Status = PeiServicesLocatePpi (
778 &gEfiPeiSectionExtractionPpiGuid,
779 0,
780 NULL,
781 (VOID **)&SectionExtract
782 );
783
784 if (EFI_ERROR (Status)) {
785 return Status;
786 }
787 //
788 // Verify Authentication State
789 //
790 CopyMem (&TempGuid, Section + 1, sizeof (EFI_GUID));
791
792 Status = SectionExtract->PeiGetSection (
793 GetPeiServicesTablePointer(),
794 SectionExtract,
795 (EFI_SECTION_TYPE *) &SectionType,
796 &TempGuid,
797 0,
798 (VOID **) &Buffer,
799 &BufferSize,
800 &AuthenticationStatus
801 );
802
803 if (EFI_ERROR (Status)) {
804 return Status;
805 }
806 //
807 // If not ask the Security PPI, if exists, for disposition
808 //
809 //
810 Status = PeiServicesLocatePpi (
811 &gEfiPeiSecurityPpiGuid,
812 0,
813 NULL,
814 (VOID **)&Security
815 );
816 if (EFI_ERROR (Status)) {
817 return Status;
818 }
819
820 Status = Security->AuthenticationState (
821 GetPeiServicesTablePointer(),
822 (struct _EFI_PEI_SECURITY_PPI *) Security,
823 AuthenticationStatus,
824 FfsFileHeader,
825 &StartCrisisRecovery
826 );
827
828 if (EFI_ERROR (Status)) {
829 return Status;
830 }
831 //
832 // If there is a security violation, report to caller and have
833 // the upper-level logic possible engender a crisis recovery
834 //
835 if (StartCrisisRecovery) {
836 return EFI_SECURITY_VIOLATION;
837 }
838 }
839
840 if (Section->Type == EFI_SECTION_PE32) {
841 //
842 // This is what we want
843 //
844 *Pe32Data = (VOID *) (Section + 1);
845 return EFI_SUCCESS;
846 } else if (Section->Type == EFI_SECTION_COMPRESSION) {
847 //
848 // This is a compression set, expand it
849 //
850 CompressionSection = (EFI_COMPRESSION_SECTION *) Section;
851
852 switch (CompressionSection->CompressionType) {
853 case EFI_STANDARD_COMPRESSION:
854 DecompressLibrary = &gTianoDecompress;
855 break;
856
857 case EFI_CUSTOMIZED_COMPRESSION:
858 //
859 // Load user customized compression protocol.
860 //
861 DecompressLibrary = &gCustomDecompress;
862 break;
863
864 case EFI_NOT_COMPRESSED:
865 default:
866 //
867 // Need to support not compressed file
868 //
869 ASSERT_EFI_ERROR (Status);
870 return EFI_NOT_FOUND;
871 }
872
873 Status = DecompressLibrary->GetInfo (
874 (UINT8 *) ((EFI_COMPRESSION_SECTION *) Section + 1),
875 (UINT32) SectionLength - sizeof (EFI_COMPRESSION_SECTION),
876 &DstBufferSize,
877 &ScratchBufferSize
878 );
879 if (EFI_ERROR (Status)) {
880 //
881 // GetInfo failed
882 //
883 return EFI_NOT_FOUND;
884 }
885
886 //
887 // Allocate scratch buffer
888 //
889 ScratchBuffer = AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize));
890 if (ScratchBuffer == NULL) {
891 return EFI_OUT_OF_RESOURCES;
892 }
893
894 //
895 // Allocate destination buffer
896 //
897 DstBuffer = AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize));
898 if (DstBuffer == NULL) {
899 return EFI_OUT_OF_RESOURCES;
900 }
901
902 //
903 // Call decompress function
904 //
905 Status = DecompressLibrary->Decompress (
906 (CHAR8 *) ((EFI_COMPRESSION_SECTION *) Section + 1),
907 DstBuffer,
908 ScratchBuffer
909 );
910
911 CmpSection = (EFI_COMMON_SECTION_HEADER *) DstBuffer;
912 if (CmpSection->Type == EFI_SECTION_RAW) {
913 //
914 // Skip the section header and
915 // adjust the pointer alignment to 16
916 //
917 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (DstBuffer + 16);
918
919 if (FvHeader->Signature == EFI_FVH_SIGNATURE) {
920 FfsFileHeader = NULL;
921 BuildFvHob ((EFI_PHYSICAL_ADDRESS) (UINTN) FvHeader, FvHeader->FvLength);
922 Status = PeiServicesFfsFindNextFile (
923 EFI_FV_FILETYPE_DXE_CORE,
924 FvHeader,
925 &FfsFileHeader
926 );
927
928 if (EFI_ERROR (Status)) {
929 return EFI_NOT_FOUND;
930 }
931
932 //
933 // Reture the FfsHeader that contain Pe32Data.
934 //
935 *RealFfsFileHeader = FfsFileHeader;
936 return PeiProcessFile (SectionType, RealFfsFileHeader, Pe32Data);
937 }
938 }
939 //
940 // Decompress successfully.
941 // Loop the decompressed data searching for expected section.
942 //
943 CmpFileData = (VOID *) DstBuffer;
944 CmpFileSize = DstBufferSize;
945 do {
946 CmpSectionLength = *(UINT32 *) (CmpSection->Size) & 0x00ffffff;
947 if (CmpSection->Type == EFI_SECTION_PE32) {
948 //
949 // This is what we want
950 //
951 *Pe32Data = (VOID *) (CmpSection + 1);
952 return EFI_SUCCESS;
953 }
954
955 OccupiedCmpSectionLength = GetOccupiedSize (CmpSectionLength, 4);
956 CmpSection = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) CmpSection + OccupiedCmpSectionLength);
957 } while (CmpSection->Type != 0 && (UINTN) ((UINT8 *) CmpSection - (UINT8 *) CmpFileData) < CmpFileSize);
958 }
959
960 Section = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) Section + OccupiedSectionLength);
961 FileSize = FfsFileHeader->Size[0] & 0xFF;
962 FileSize += (FfsFileHeader->Size[1] << 8) & 0xFF00;
963 FileSize += (FfsFileHeader->Size[2] << 16) & 0xFF0000;
964 FileSize &= 0x00FFFFFF;
965 } while (Section->Type != 0 && (UINTN) ((UINT8 *) Section - (UINT8 *) FfsFileHeader) < FileSize);
966
967 //
968 // End of the decompression activity
969 //
970 } else {
971
972 Status = PeiServicesFfsFindSectionData (
973 EFI_SECTION_PE32,
974 FfsFileHeader,
975 &SectionData
976 );
977
978 if (EFI_ERROR (Status)) {
979 Status = PeiServicesFfsFindSectionData (
980 EFI_SECTION_TE,
981 FfsFileHeader,
982 &SectionData
983 );
984 if (EFI_ERROR (Status)) {
985 return Status;
986 }
987 }
988 }
989
990 *Pe32Data = SectionData;
991
992 return EFI_SUCCESS;
993 }