8d3c29a5798fce41a5ab2b0689935487a66e984f
[mirror_edk2.git] / MdeModulePkg / Core / DxeIplPeim / DxeLoad.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 #include <Ppi/GuidedSectionExtraction.h>
25
26 // porting note remove later
27 #include "FrameworkPei.h"
28 // end of remove later
29
30 EFI_STATUS
31 CustomDecompressExtractSection (
32 IN CONST EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI *This,
33 IN CONST VOID *InputSection,
34 OUT VOID **OutputBuffer,
35 OUT UINTN *OutputSize,
36 OUT UINT32 *AuthenticationStatus
37 );
38
39 BOOLEAN gInMemory = FALSE;
40
41 //
42 // Module Globals used in the DXE to PEI handoff
43 // These must be module globals, so the stack can be switched
44 //
45 static EFI_DXE_IPL_PPI mDxeIplPpi = {
46 DxeLoadCore
47 };
48
49 static EFI_PEI_FV_FILE_LOADER_PPI mLoadFilePpi = {
50 DxeIplLoadFile
51 };
52
53 static EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI mCustomDecompressExtractiongPpi = {
54 CustomDecompressExtractSection
55 };
56
57 static EFI_PEI_PPI_DESCRIPTOR mPpiList[] = {
58 {
59 EFI_PEI_PPI_DESCRIPTOR_PPI,
60 &gEfiPeiFvFileLoaderPpiGuid,
61 &mLoadFilePpi
62 },
63 {
64 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
65 &gEfiDxeIplPpiGuid,
66 &mDxeIplPpi
67 }
68 };
69
70 static EFI_PEI_PPI_DESCRIPTOR mPpiSignal = {
71 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
72 &gEfiEndOfPeiSignalPpiGuid,
73 NULL
74 };
75
76 EFI_STATUS
77 EFIAPI
78 PeimInitializeDxeIpl (
79 IN EFI_FFS_FILE_HEADER *FfsHeader,
80 IN EFI_PEI_SERVICES **PeiServices
81 )
82 /*++
83
84 Routine Description:
85
86 Initializes the Dxe Ipl PPI
87
88 Arguments:
89
90 FfsHeader - Pointer to FFS file header
91 PeiServices - General purpose services available to every PEIM.
92
93 Returns:
94
95 EFI_SUCCESS
96
97 --*/
98 {
99 EFI_STATUS Status;
100 EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader;
101 EFI_BOOT_MODE BootMode;
102 EFI_GUID **DecompressGuidList;
103 UINT32 DecompressMethodNumber;
104 EFI_PEI_PPI_DESCRIPTOR *GuidPpi;
105
106 Status = PeiServicesGetBootMode (&BootMode);
107 ASSERT_EFI_ERROR (Status);
108
109 if (!gInMemory && (BootMode != BOOT_ON_S3_RESUME)) {
110 //
111 // The DxeIpl has not yet been shadowed
112 //
113 PeiEfiPeiPeCoffLoader = (EFI_PEI_PE_COFF_LOADER_PROTOCOL *)GetPeCoffLoaderProtocol ();
114
115 //
116 // Shadow DxeIpl and then re-run its entry point
117 //
118 Status = ShadowDxeIpl (FfsHeader, PeiEfiPeiPeCoffLoader);
119 } else {
120 //
121 // Get custom decompress method guid list
122 //
123 DecompressGuidList = NULL;
124 DecompressMethodNumber = 0;
125 Status = CustomDecompressGetAlgorithms (DecompressGuidList, &DecompressMethodNumber);
126 if (Status == EFI_OUT_OF_RESOURCES) {
127 DecompressGuidList = (EFI_GUID **) AllocatePages (EFI_SIZE_TO_PAGES (DecompressMethodNumber * sizeof (EFI_GUID *)));
128 ASSERT (DecompressGuidList != NULL);
129 Status = CustomDecompressGetAlgorithms (DecompressGuidList, &DecompressMethodNumber);
130 }
131 ASSERT_EFI_ERROR(Status);
132
133 //
134 // Install custom decompress extraction guid ppi
135 //
136 if (DecompressMethodNumber > 0) {
137 GuidPpi = NULL;
138 GuidPpi = (EFI_PEI_PPI_DESCRIPTOR *) AllocatePages (EFI_SIZE_TO_PAGES (DecompressMethodNumber * sizeof (EFI_PEI_PPI_DESCRIPTOR)));
139 ASSERT (GuidPpi != NULL);
140 while (DecompressMethodNumber-- > 0) {
141 GuidPpi->Flags = EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST;
142 GuidPpi->Ppi = &mCustomDecompressExtractiongPpi;
143 GuidPpi->Guid = DecompressGuidList [DecompressMethodNumber];
144 Status = PeiServicesInstallPpi (GuidPpi++);
145 ASSERT_EFI_ERROR(Status);
146 }
147 }
148
149 //
150 // Install FvFileLoader and DxeIpl PPIs.
151 //
152 Status = PeiServicesInstallPpi (mPpiList);
153 ASSERT_EFI_ERROR(Status);
154 }
155
156 return Status;
157 }
158
159 EFI_STATUS
160 EFIAPI
161 DxeLoadCore (
162 IN EFI_DXE_IPL_PPI *This,
163 IN EFI_PEI_SERVICES **PeiServices,
164 IN EFI_PEI_HOB_POINTERS HobList
165 )
166 /*++
167
168 Routine Description:
169
170 Main entry point to last PEIM
171
172 Arguments:
173 This - Entry point for DXE IPL PPI
174 PeiServices - General purpose services available to every PEIM.
175 HobList - Address to the Pei HOB list
176
177 Returns:
178
179 EFI_SUCCESS - DEX core was successfully loaded.
180 EFI_OUT_OF_RESOURCES - There are not enough resources to load DXE core.
181
182 --*/
183 {
184 EFI_STATUS Status;
185 EFI_GUID DxeCoreFileName;
186 EFI_GUID FirmwareFileName;
187 VOID *Pe32Data;
188 VOID *FvImageData;
189 EFI_PHYSICAL_ADDRESS DxeCoreAddress;
190 UINT64 DxeCoreSize;
191 EFI_PHYSICAL_ADDRESS DxeCoreEntryPoint;
192 EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader;
193 EFI_BOOT_MODE BootMode;
194 EFI_PEI_RECOVERY_MODULE_PPI *PeiRecovery;
195 EFI_PEI_S3_RESUME_PPI *S3Resume;
196
197 // PERF_START (PeiServices, L"DxeIpl", NULL, 0);
198
199 //
200 // if in S3 Resume, restore configure
201 //
202 Status = PeiServicesGetBootMode (&BootMode);
203 ASSERT_EFI_ERROR(Status);
204
205 if (BootMode == BOOT_ON_S3_RESUME) {
206 Status = PeiServicesLocatePpi (
207 &gEfiPeiS3ResumePpiGuid,
208 0,
209 NULL,
210 (VOID **)&S3Resume
211 );
212 ASSERT_EFI_ERROR (Status);
213
214 Status = S3Resume->S3RestoreConfig (PeiServices);
215 ASSERT_EFI_ERROR (Status);
216 } else if (BootMode == BOOT_IN_RECOVERY_MODE) {
217
218 Status = PeiServicesLocatePpi (
219 &gEfiPeiRecoveryModulePpiGuid,
220 0,
221 NULL,
222 (VOID **)&PeiRecovery
223 );
224 ASSERT_EFI_ERROR (Status);
225
226 Status = PeiRecovery->LoadRecoveryCapsule (PeiServices, PeiRecovery);
227 if (EFI_ERROR (Status)) {
228 DEBUG ((EFI_D_ERROR, "Load Recovery Capsule Failed.(Status = %r)\n", Status));
229 CpuDeadLoop ();
230 }
231
232 //
233 // Now should have a HOB with the DXE core w/ the old HOB destroyed
234 //
235 }
236
237 //
238 // Install the PEI Protocols that are shared between PEI and DXE
239 //
240 PeiEfiPeiPeCoffLoader = (EFI_PEI_PE_COFF_LOADER_PROTOCOL *)GetPeCoffLoaderProtocol ();
241 ASSERT (PeiEfiPeiPeCoffLoader != NULL);
242
243 //
244 // Find the EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE type compressed Firmware Volume file
245 // The file found will be processed by PeiProcessFile: It will first be decompressed to
246 // a normal FV, then a corresponding FV type hob will be built.
247 //
248 Status = PeiFindFile (
249 EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE,
250 EFI_SECTION_FIRMWARE_VOLUME_IMAGE,
251 &FirmwareFileName,
252 &FvImageData
253 );
254
255 //
256 // Find the DXE Core in a Firmware Volume
257 //
258 Status = PeiFindFile (
259 EFI_FV_FILETYPE_DXE_CORE,
260 EFI_SECTION_PE32,
261 &DxeCoreFileName,
262 &Pe32Data
263 );
264 ASSERT_EFI_ERROR (Status);
265
266 //
267 // Load the DXE Core from a Firmware Volume
268 //
269 Status = PeiLoadFile (
270 PeiEfiPeiPeCoffLoader,
271 Pe32Data,
272 &DxeCoreAddress,
273 &DxeCoreSize,
274 &DxeCoreEntryPoint
275 );
276 ASSERT_EFI_ERROR (Status);
277
278 //
279 // Add HOB for the DXE Core
280 //
281 BuildModuleHob (
282 &DxeCoreFileName,
283 DxeCoreAddress,
284 DxeCoreSize,
285 DxeCoreEntryPoint
286 );
287
288 //
289 // Add HOB for the PE/COFF Loader Protocol
290 //
291 BuildGuidDataHob (
292 &gEfiPeiPeCoffLoaderGuid,
293 (VOID *)&PeiEfiPeiPeCoffLoader,
294 sizeof (VOID *)
295 );
296 //
297 // Report Status Code EFI_SW_PEI_PC_HANDOFF_TO_NEXT
298 //
299 REPORT_STATUS_CODE (
300 EFI_PROGRESS_CODE,
301 EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_CORE_PC_HANDOFF_TO_NEXT
302 );
303
304 //
305 // Transfer control to the DXE Core
306 // The handoff state is simply a pointer to the HOB list
307 //
308
309 DEBUG ((EFI_D_INFO, "DXE Core Entry Point 0x%08x\n", (UINTN) DxeCoreEntryPoint));
310 HandOffToDxeCore (DxeCoreEntryPoint, HobList, &mPpiSignal);
311 //
312 // If we get here, then the DXE Core returned. This is an error
313 // Dxe Core should not return.
314 //
315 ASSERT (FALSE);
316 CpuDeadLoop ();
317
318 return EFI_OUT_OF_RESOURCES;
319 }
320
321 EFI_STATUS
322 PeiFindFile (
323 IN UINT8 Type,
324 IN EFI_SECTION_TYPE SectionType,
325 OUT EFI_GUID *FileName,
326 OUT VOID **Pe32Data
327 )
328 /*++
329
330 Routine Description:
331
332 Finds a PE/COFF of a specific Type and SectionType in the Firmware Volumes
333 described in the HOB list. Able to search in a compression set in a FFS file.
334 But only one level of compression is supported, that is, not able to search
335 in a compression set that is within another compression set.
336
337 Arguments:
338
339 Type - The Type of file to retrieve
340
341 SectionType - The type of section to retrieve from a file
342
343 FileName - The name of the file found in the Firmware Volume
344
345 Pe32Data - Pointer to the beginning of the PE/COFF file found in the Firmware Volume
346
347 Returns:
348
349 EFI_SUCCESS - The file was found, and the name is returned in FileName, and a pointer to
350 the PE/COFF image is returned in Pe32Data
351
352 EFI_NOT_FOUND - The file was not found in the Firmware Volumes present in the HOB List
353
354 --*/
355 {
356 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
357 EFI_FFS_FILE_HEADER *FfsFileHeader;
358 EFI_STATUS Status;
359 EFI_PEI_HOB_POINTERS Hob;
360
361
362 FwVolHeader = NULL;
363 FfsFileHeader = NULL;
364 Status = EFI_SUCCESS;
365
366 //
367 // For each Firmware Volume, look for a specified type
368 // of file and break out until no one is found
369 //
370 Hob.Raw = GetHobList ();
371 while ((Hob.Raw = GetNextHob (EFI_HOB_TYPE_FV, Hob.Raw)) != NULL) {
372 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (Hob.FirmwareVolume->BaseAddress);
373 //
374 // Make sure the FV HOB does not get corrupted.
375 //
376 ASSERT (FwVolHeader->Signature == EFI_FVH_SIGNATURE);
377
378 Status = PeiServicesFfsFindNextFile (
379 Type,
380 FwVolHeader,
381 &FfsFileHeader
382 );
383 if (!EFI_ERROR (Status)) {
384 Status = PeiProcessFile (
385 SectionType,
386 FfsFileHeader,
387 Pe32Data,
388 &Hob
389 );
390 CopyMem (FileName, &FfsFileHeader->Name, sizeof (EFI_GUID));
391 //
392 // Find all Fv type ffs to get all FvImage and add them into FvHob
393 //
394 if (!EFI_ERROR (Status) && (Type != EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE)) {
395 return EFI_SUCCESS;
396 }
397 }
398 Hob.Raw = GET_NEXT_HOB (Hob);
399 }
400 return EFI_NOT_FOUND;
401 }
402
403 EFI_STATUS
404 PeiLoadFile (
405 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader,
406 IN VOID *Pe32Data,
407 OUT EFI_PHYSICAL_ADDRESS *ImageAddress,
408 OUT UINT64 *ImageSize,
409 OUT EFI_PHYSICAL_ADDRESS *EntryPoint
410 )
411 /*++
412
413 Routine Description:
414
415 Loads and relocates a PE/COFF image into memory.
416
417 Arguments:
418
419 PeiEfiPeiPeCoffLoader - Pointer to a PE COFF loader protocol
420
421 Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated
422
423 ImageAddress - The base address of the relocated PE/COFF image
424
425 ImageSize - The size of the relocated PE/COFF image
426
427 EntryPoint - The entry point of the relocated PE/COFF image
428
429 Returns:
430
431 EFI_SUCCESS - The file was loaded and relocated
432
433 EFI_OUT_OF_RESOURCES - There was not enough memory to load and relocate the PE/COFF file
434
435 --*/
436 {
437 EFI_STATUS Status;
438 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
439
440 ZeroMem (&ImageContext, sizeof (ImageContext));
441 ImageContext.Handle = Pe32Data;
442 Status = GetImageReadFunction (&ImageContext);
443
444 ASSERT_EFI_ERROR (Status);
445
446 Status = PeiEfiPeiPeCoffLoader->GetImageInfo (PeiEfiPeiPeCoffLoader, &ImageContext);
447 if (EFI_ERROR (Status)) {
448 return Status;
449 }
450 //
451 // Allocate Memory for the image
452 //
453 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32) ImageContext.ImageSize));
454 ASSERT (ImageContext.ImageAddress != 0);
455
456 //
457 // Load the image to our new buffer
458 //
459 Status = PeiEfiPeiPeCoffLoader->LoadImage (PeiEfiPeiPeCoffLoader, &ImageContext);
460 if (EFI_ERROR (Status)) {
461 return Status;
462 }
463 //
464 // Relocate the image in our new buffer
465 //
466 Status = PeiEfiPeiPeCoffLoader->RelocateImage (PeiEfiPeiPeCoffLoader, &ImageContext);
467 if (EFI_ERROR (Status)) {
468 return Status;
469 }
470
471 //
472 // Flush the instruction cache so the image data is written before we execute it
473 //
474 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);
475
476 *ImageAddress = ImageContext.ImageAddress;
477 *ImageSize = ImageContext.ImageSize;
478 *EntryPoint = ImageContext.EntryPoint;
479
480 return EFI_SUCCESS;
481 }
482
483 EFI_STATUS
484 ShadowDxeIpl (
485 IN EFI_FFS_FILE_HEADER *DxeIplFileHeader,
486 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader
487 )
488 /*++
489
490 Routine Description:
491
492 Shadow the DXE IPL to a different memory location. This occurs after permanent
493 memory has been discovered.
494
495 Arguments:
496
497 DxeIplFileHeader - Pointer to the FFS file header of the DXE IPL driver
498
499 PeiEfiPeiPeCoffLoader - Pointer to a PE COFF loader protocol
500
501 Returns:
502
503 EFI_SUCCESS - DXE IPL was successfully shadowed to a different memory location.
504
505 EFI_ ERROR - The shadow was unsuccessful.
506
507
508 --*/
509 {
510 UINTN SectionLength;
511 UINTN OccupiedSectionLength;
512 EFI_PHYSICAL_ADDRESS DxeIplAddress;
513 UINT64 DxeIplSize;
514 EFI_PHYSICAL_ADDRESS DxeIplEntryPoint;
515 EFI_STATUS Status;
516 EFI_COMMON_SECTION_HEADER *Section;
517
518 Section = (EFI_COMMON_SECTION_HEADER *) (DxeIplFileHeader + 1);
519
520 while ((Section->Type != EFI_SECTION_PE32) && (Section->Type != EFI_SECTION_TE)) {
521 SectionLength = *(UINT32 *) (Section->Size) & 0x00ffffff;
522 OccupiedSectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);
523 Section = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) Section + OccupiedSectionLength);
524 }
525 //
526 // Relocate DxeIpl into memory by using loadfile service
527 //
528 Status = PeiLoadFile (
529 PeiEfiPeiPeCoffLoader,
530 (VOID *) (Section + 1),
531 &DxeIplAddress,
532 &DxeIplSize,
533 &DxeIplEntryPoint
534 );
535
536 if (Status == EFI_SUCCESS) {
537 //
538 // Set gInMemory global variable to TRUE to indicate the dxeipl is shadowed.
539 //
540 *(BOOLEAN *) ((UINTN) &gInMemory + (UINTN) DxeIplEntryPoint - (UINTN) _ModuleEntryPoint) = TRUE;
541 Status = ((EFI_PEIM_ENTRY_POINT2) (UINTN) DxeIplEntryPoint) ((EFI_PEI_FILE_HANDLE *) DxeIplFileHeader, GetPeiServicesTablePointer());
542 }
543
544 return Status;
545 }
546
547 EFI_STATUS
548 EFIAPI
549 DxeIplLoadFile (
550 IN EFI_PEI_FV_FILE_LOADER_PPI *This,
551 IN EFI_FFS_FILE_HEADER *FfsHeader,
552 OUT EFI_PHYSICAL_ADDRESS *ImageAddress,
553 OUT UINT64 *ImageSize,
554 OUT EFI_PHYSICAL_ADDRESS *EntryPoint
555 )
556 /*++
557
558 Routine Description:
559
560 Given a pointer to an FFS file containing a PE32 image, get the
561 information on the PE32 image, and then "load" it so that it
562 can be executed.
563
564 Arguments:
565
566 This - pointer to our file loader protocol
567
568 FfsHeader - pointer to the FFS file header of the FFS file that
569 contains the PE32 image we want to load
570
571 ImageAddress - returned address where the PE32 image is loaded
572
573 ImageSize - returned size of the loaded PE32 image
574
575 EntryPoint - entry point to the loaded PE32 image
576
577 Returns:
578
579 EFI_SUCCESS - The FFS file was successfully loaded.
580
581 EFI_ERROR - Unable to load the FFS file.
582
583 --*/
584 {
585 EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader;
586 EFI_STATUS Status;
587 VOID *Pe32Data;
588
589 Pe32Data = NULL;
590 PeiEfiPeiPeCoffLoader = (EFI_PEI_PE_COFF_LOADER_PROTOCOL *)GetPeCoffLoaderProtocol ();
591
592 //
593 // Preprocess the FFS file to get a pointer to the PE32 information
594 // in the enclosed PE32 image.
595 //
596 Status = PeiProcessFile (
597 EFI_SECTION_TE,
598 FfsHeader,
599 &Pe32Data,
600 NULL
601 );
602 if (EFI_ERROR (Status)) {
603 Status = PeiProcessFile (
604 EFI_SECTION_PE32,
605 FfsHeader,
606 &Pe32Data,
607 NULL
608 );
609
610 if (EFI_ERROR (Status)) {
611 return Status;
612 }
613 }
614 //
615 // Load the PE image from the FFS file
616 //
617 Status = PeiLoadFile (
618 PeiEfiPeiPeCoffLoader,
619 Pe32Data,
620 ImageAddress,
621 ImageSize,
622 EntryPoint
623 );
624
625 return Status;
626 }
627
628 EFI_STATUS
629 PeiProcessFile (
630 IN EFI_SECTION_TYPE SectionType,
631 IN EFI_FFS_FILE_HEADER *FfsFileHeader,
632 OUT VOID **Pe32Data,
633 IN EFI_PEI_HOB_POINTERS *OrigHob
634 )
635 /*++
636
637 Routine Description:
638
639 Arguments:
640
641 SectionType - The type of section in the FFS file to process.
642
643 FfsFileHeader - Pointer to the FFS file to process, looking for the
644 specified SectionType
645
646 Pe32Data - returned pointer to the start of the PE32 image found
647 in the FFS file.
648
649 Returns:
650
651 EFI_SUCCESS - found the PE32 section in the FFS file
652
653 --*/
654 {
655 EFI_STATUS Status;
656 UINT8 *DstBuffer;
657 UINT8 *ScratchBuffer;
658 UINTN DstBufferSize;
659 UINT32 ScratchBufferSize;
660 EFI_COMMON_SECTION_HEADER *CmpSection;
661 UINTN CmpSectionLength;
662 UINTN OccupiedCmpSectionLength;
663 VOID *CmpFileData;
664 UINTN CmpFileSize;
665 EFI_COMMON_SECTION_HEADER *Section;
666 UINTN SectionLength;
667 UINTN OccupiedSectionLength;
668 UINTN FileSize;
669 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;
670 EFI_COMPRESSION_SECTION *CompressionSection;
671 EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI *SectionExtract;
672 UINT32 AuthenticationStatus;
673
674 //
675 // First try to find the required section in this ffs file.
676 //
677 Status = PeiServicesFfsFindSectionData (
678 SectionType,
679 FfsFileHeader,
680 Pe32Data
681 );
682 if (!EFI_ERROR (Status)) {
683 return Status;
684 }
685
686 //
687 // If not found, the required section may be in guided or compressed section.
688 // So, search guided or compressed section to process
689 //
690 Section = (EFI_COMMON_SECTION_HEADER *) (UINTN) (VOID *) ((UINT8 *) (FfsFileHeader) + (UINTN) sizeof (EFI_FFS_FILE_HEADER));
691 FileSize = FfsFileHeader->Size[0] & 0xFF;
692 FileSize += (FfsFileHeader->Size[1] << 8) & 0xFF00;
693 FileSize += (FfsFileHeader->Size[2] << 16) & 0xFF0000;
694 FileSize &= 0x00FFFFFF;
695 OccupiedSectionLength = 0;
696
697 do {
698 //
699 // Initialize local variables.
700 //
701 DstBuffer = NULL;
702 DstBufferSize = 0;
703
704 Section = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) Section + OccupiedSectionLength);
705 SectionLength = *(UINT32 *) (Section->Size) & 0x00ffffff;
706 OccupiedSectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);
707
708 //
709 // Was the DXE Core file encapsulated in a GUID'd section?
710 //
711 if (Section->Type == EFI_SECTION_GUID_DEFINED) {
712 //
713 // Set a default authenticatino state
714 //
715 AuthenticationStatus = 0;
716 //
717 // Locate extract guid section ppi
718 //
719 Status = PeiServicesLocatePpi (
720 (EFI_GUID *) (Section + 1),
721 0,
722 NULL,
723 (VOID **)&SectionExtract
724 );
725
726 if (EFI_ERROR (Status)) {
727 //
728 // ignore the unknown guid section
729 //
730 continue;
731 }
732 //
733 // Extract the contents from guid section
734 //
735 Status = SectionExtract->ExtractSection (
736 SectionExtract,
737 (VOID *) Section,
738 (VOID **) &DstBuffer,
739 &DstBufferSize,
740 &AuthenticationStatus
741 );
742
743 if (EFI_ERROR (Status)) {
744 DEBUG ((EFI_D_ERROR, "Extract section content failed - %r\n", Status));
745 return Status;
746 }
747
748 //
749 // Todo check AuthenticationStatus and do the verify
750 //
751 } else if (Section->Type == EFI_SECTION_COMPRESSION) {
752 //
753 // This is a compression set, expand it
754 //
755 CompressionSection = (EFI_COMPRESSION_SECTION *) Section;
756
757 switch (CompressionSection->CompressionType) {
758 case EFI_STANDARD_COMPRESSION:
759 //
760 // Load EFI standard compression.
761 // For compressed data, decompress them to dstbuffer.
762 //
763 Status = UefiDecompressGetInfo (
764 (UINT8 *) ((EFI_COMPRESSION_SECTION *) Section + 1),
765 (UINT32) SectionLength - sizeof (EFI_COMPRESSION_SECTION),
766 (UINT32 *) &DstBufferSize,
767 &ScratchBufferSize
768 );
769 if (EFI_ERROR (Status)) {
770 //
771 // GetInfo failed
772 //
773 DEBUG ((EFI_D_ERROR, "Decompress GetInfo Failed - %r\n", Status));
774 return EFI_NOT_FOUND;
775 }
776 //
777 // Allocate scratch buffer
778 //
779 ScratchBuffer = AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize));
780 if (ScratchBuffer == NULL) {
781 return EFI_OUT_OF_RESOURCES;
782 }
783 //
784 // Allocate destination buffer
785 //
786 DstBuffer = AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize));
787 if (DstBuffer == NULL) {
788 return EFI_OUT_OF_RESOURCES;
789 }
790 //
791 // Call decompress function
792 //
793 Status = UefiDecompress (
794 (CHAR8 *) ((EFI_COMPRESSION_SECTION *) Section + 1),
795 DstBuffer,
796 ScratchBuffer
797 );
798 if (EFI_ERROR (Status)) {
799 //
800 // Decompress failed
801 //
802 DEBUG ((EFI_D_ERROR, "Decompress Failed - %r\n", Status));
803 return EFI_NOT_FOUND;
804 }
805 break;
806
807 // porting note the original branch for customized compress is removed, it should be change to use GUID compress
808
809 case EFI_NOT_COMPRESSED:
810 //
811 // Allocate destination buffer
812 //
813 DstBufferSize = CompressionSection->UncompressedLength;
814 DstBuffer = AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize));
815 if (DstBuffer == NULL) {
816 return EFI_OUT_OF_RESOURCES;
817 }
818 //
819 // stream is not actually compressed, just encapsulated. So just copy it.
820 //
821 CopyMem (DstBuffer, CompressionSection + 1, DstBufferSize);
822 break;
823
824 default:
825 //
826 // Don't support other unknown compression type.
827 //
828 ASSERT_EFI_ERROR (Status);
829 return EFI_NOT_FOUND;
830 }
831 } else {
832 //
833 // ignore other type sections
834 //
835 continue;
836 }
837
838 //
839 // Extract contents from guided or compressed sections.
840 // Loop the decompressed data searching for expected section.
841 //
842 CmpSection = (EFI_COMMON_SECTION_HEADER *) DstBuffer;
843 CmpFileData = (VOID *) DstBuffer;
844 CmpFileSize = DstBufferSize;
845 do {
846 CmpSectionLength = *(UINT32 *) (CmpSection->Size) & 0x00ffffff;
847 if (CmpSection->Type == SectionType) {
848 //
849 // This is what we want
850 //
851 if (SectionType == EFI_SECTION_FIRMWARE_VOLUME_IMAGE) {
852 //
853 // Firmware Volume Image in this Section
854 // Skip the section header to get FvHeader
855 //
856 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (CmpSection + 1);
857
858 if (FvHeader->Signature == EFI_FVH_SIGNATURE) {
859 //
860 // Because FvLength in FvHeader is UINT64 type,
861 // so FvHeader must meed at least 8 bytes alignment.
862 // If current FvImage base address doesn't meet its alignment,
863 // we need to reload this FvImage to another correct memory address.
864 //
865 if (((UINTN) FvHeader % sizeof (UINT64)) != 0) {
866 CopyMem (DstBuffer, FvHeader, (UINTN) CmpSectionLength - sizeof (EFI_COMMON_SECTION_HEADER));
867 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) DstBuffer;
868 }
869
870 //
871 // Build new FvHob for new decompressed Fv image.
872 //
873 BuildFvHob ((EFI_PHYSICAL_ADDRESS) (UINTN) FvHeader, FvHeader->FvLength);
874
875 //
876 // Set the original FvHob to unused.
877 //
878 if (OrigHob != NULL) {
879 OrigHob->Header->HobType = EFI_HOB_TYPE_UNUSED;
880 }
881 //
882 // return found FvImage data.
883 //
884 *Pe32Data = (VOID *) FvHeader;
885 return EFI_SUCCESS;
886 }
887 } else {
888 //
889 // direct return the found section.
890 //
891 *Pe32Data = (VOID *) (CmpSection + 1);
892 return EFI_SUCCESS;
893 }
894 }
895 OccupiedCmpSectionLength = GET_OCCUPIED_SIZE (CmpSectionLength, 4);
896 CmpSection = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) CmpSection + OccupiedCmpSectionLength);
897 } while (CmpSection->Type != 0 && (UINTN) ((UINT8 *) CmpSection - (UINT8 *) CmpFileData) < CmpFileSize);
898 } while (Section->Type != 0 && (UINTN) ((UINT8 *) Section + OccupiedSectionLength - (UINT8 *) FfsFileHeader) < FileSize);
899
900 //
901 // search all sections (compression and non compression) in this FFS, don't
902 // find expected section.
903 //
904 return EFI_NOT_FOUND;
905 }
906
907 /**
908 The ExtractSection() function processes the input section and
909 returns a pointer to the section contents. If the section being
910 extracted does not require processing (if the section
911 GuidedSectionHeader.Attributes has the
912 EFI_GUIDED_SECTION_PROCESSING_REQUIRED field cleared), then
913 OutputBuffer is just updated to point to the start of the
914 section's contents. Otherwise, *Buffer must be allocated
915 from PEI permanent memory.
916
917 @param This Indicates the
918 EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI instance.
919 Buffer containing the input GUIDed section to be
920 processed. OutputBuffer OutputBuffer is
921 allocated from PEI permanent memory and contains
922 the new section stream.
923
924 @param OutputSize A pointer to a caller-allocated
925 UINTN in which the size of *OutputBuffer
926 allocation is stored. If the function
927 returns anything other than EFI_SUCCESS,
928 the value of OutputSize is undefined.
929
930 @param AuthenticationStatus A pointer to a caller-allocated
931 UINT32 that indicates the
932 authentication status of the
933 output buffer. If the input
934 section's GuidedSectionHeader.
935 Attributes field has the
936 EFI_GUIDED_SECTION_AUTH_STATUS_VALID
937 bit as clear,
938 AuthenticationStatus must return
939 zero. These bits reflect the
940 status of the extraction
941 operation. If the function
942 returns anything other than
943 EFI_SUCCESS, the value of
944 AuthenticationStatus is
945 undefined.
946
947 @retval EFI_SUCCESS The InputSection was
948 successfully processed and the
949 section contents were returned.
950
951 @retval EFI_OUT_OF_RESOURCES The system has insufficient
952 resources to process the request.
953
954 @reteval EFI_INVALID_PARAMETER The GUID in InputSection does
955 not match this instance of the
956 GUIDed Section Extraction PPI.
957 **/
958 EFI_STATUS
959 CustomDecompressExtractSection (
960 IN CONST EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI *This,
961 IN CONST VOID *InputSection,
962 OUT VOID **OutputBuffer,
963 OUT UINTN *OutputSize,
964 OUT UINT32 *AuthenticationStatus
965 )
966 {
967 EFI_STATUS Status;
968 UINT8 *ScratchBuffer;
969 UINT32 ScratchSize;
970 UINT32 SectionLength;
971 UINT32 DestinationSize;
972
973 //
974 // Set authentic value to zero.
975 //
976 *AuthenticationStatus = 0;
977 //
978 // Calculate Section data Size
979 //
980 SectionLength = *(UINT32 *) (((EFI_COMMON_SECTION_HEADER *) InputSection)->Size) & 0x00ffffff;
981 //
982 // Get compressed data information
983 //
984 Status = CustomDecompressGetInfo (
985 (GUID *) ((UINT8 *) InputSection + sizeof (EFI_COMMON_SECTION_HEADER)),
986 (UINT8 *) InputSection + sizeof (EFI_GUID_DEFINED_SECTION),
987 SectionLength - sizeof (EFI_GUID_DEFINED_SECTION),
988 &DestinationSize,
989 &ScratchSize
990 );
991 if (EFI_ERROR (Status)) {
992 //
993 // GetInfo failed
994 //
995 DEBUG ((EFI_D_ERROR, "Extract guided section Failed - %r\n", Status));
996 return Status;
997 }
998
999 //
1000 // Allocate scratch buffer
1001 //
1002 ScratchBuffer = AllocatePages (EFI_SIZE_TO_PAGES (ScratchSize));
1003 if (ScratchBuffer == NULL) {
1004 return EFI_OUT_OF_RESOURCES;
1005 }
1006 //
1007 // Allocate destination buffer
1008 //
1009 *OutputSize = (UINTN) DestinationSize;
1010 *OutputBuffer = AllocatePages (EFI_SIZE_TO_PAGES (*OutputSize));
1011 if (*OutputBuffer == NULL) {
1012 return EFI_OUT_OF_RESOURCES;
1013 }
1014
1015 //
1016 // Call decompress function
1017 //
1018 Status = CustomDecompress (
1019 (GUID *) ((UINT8 *) InputSection + sizeof (EFI_COMMON_SECTION_HEADER)),
1020 (UINT8 *) InputSection + sizeof (EFI_GUID_DEFINED_SECTION),
1021 *OutputBuffer,
1022 ScratchBuffer
1023 );
1024
1025 if (EFI_ERROR (Status)) {
1026 //
1027 // Decompress failed
1028 //
1029 DEBUG ((EFI_D_ERROR, "Extract guided section Failed - %r\n", Status));
1030 return Status;
1031 }
1032
1033 return EFI_SUCCESS;
1034 }