]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/DxeIplPeim/DxeLoad.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Core / DxeIplPeim / DxeLoad.c
1 /** @file
2 Last PEIM.
3 Responsibility of this module is to load the DXE Core from a Firmware Volume.
4
5 Copyright (c) 2016 HP Development Company, L.P.
6 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10
11 #include "DxeIpl.h"
12
13
14 //
15 // Module Globals used in the DXE to PEI hand off
16 // These must be module globals, so the stack can be switched
17 //
18 CONST EFI_DXE_IPL_PPI mDxeIplPpi = {
19 DxeLoadCore
20 };
21
22 CONST EFI_PEI_PPI_DESCRIPTOR mDxeIplPpiList = {
23 EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
24 &gEfiDxeIplPpiGuid,
25 (VOID *) &mDxeIplPpi
26 };
27
28 CONST EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI mCustomGuidedSectionExtractionPpi = {
29 CustomGuidedSectionExtract
30 };
31
32 CONST EFI_PEI_DECOMPRESS_PPI mDecompressPpi = {
33 Decompress
34 };
35
36 CONST EFI_PEI_PPI_DESCRIPTOR mDecompressPpiList = {
37 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
38 &gEfiPeiDecompressPpiGuid,
39 (VOID *) &mDecompressPpi
40 };
41
42 CONST EFI_PEI_PPI_DESCRIPTOR gEndOfPeiSignalPpi = {
43 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
44 &gEfiEndOfPeiSignalPpiGuid,
45 NULL
46 };
47
48 CONST EFI_PEI_NOTIFY_DESCRIPTOR mMemoryDiscoveredNotifyList = {
49 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_DISPATCH | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
50 &gEfiPeiMemoryDiscoveredPpiGuid,
51 InstallIplPermanentMemoryPpis
52 };
53
54 /**
55 Entry point of DXE IPL PEIM.
56
57 This function installs DXE IPL PPI. It also reloads
58 itself to memory on non-S3 resume boot path.
59
60 @param FileHandle Handle of the file being invoked.
61 @param PeiServices Describes the list of possible PEI Services.
62
63 @retval EFI_SUCESS The entry point of DXE IPL PEIM executes successfully.
64 @retval Others Some error occurs during the execution of this function.
65
66 **/
67 EFI_STATUS
68 EFIAPI
69 PeimInitializeDxeIpl (
70 IN EFI_PEI_FILE_HANDLE FileHandle,
71 IN CONST EFI_PEI_SERVICES **PeiServices
72 )
73 {
74 EFI_STATUS Status;
75 EFI_BOOT_MODE BootMode;
76 VOID *Dummy;
77
78 BootMode = GetBootModeHob ();
79
80 if (BootMode != BOOT_ON_S3_RESUME) {
81 Status = PeiServicesRegisterForShadow (FileHandle);
82 if (Status == EFI_SUCCESS) {
83 //
84 // EFI_SUCESS means it is the first time to call register for shadow.
85 //
86 return Status;
87 }
88
89 //
90 // Ensure that DXE IPL is shadowed to permanent memory.
91 //
92 ASSERT (Status == EFI_ALREADY_STARTED);
93
94 //
95 // DXE core load requires permanent memory.
96 //
97 Status = PeiServicesLocatePpi (
98 &gEfiPeiMemoryDiscoveredPpiGuid,
99 0,
100 NULL,
101 (VOID **) &Dummy
102 );
103 ASSERT_EFI_ERROR (Status);
104 if (EFI_ERROR (Status)) {
105 return Status;
106 }
107
108 //
109 // Now the permanent memory exists, install the PPIs for decompression
110 // and section extraction.
111 //
112 Status = InstallIplPermanentMemoryPpis (NULL, NULL, NULL);
113 ASSERT_EFI_ERROR (Status);
114 } else {
115 //
116 // Install memory discovered PPI notification to install PPIs for
117 // decompression and section extraction.
118 //
119 Status = PeiServicesNotifyPpi (&mMemoryDiscoveredNotifyList);
120 ASSERT_EFI_ERROR (Status);
121 }
122
123 //
124 // Install DxeIpl PPI.
125 //
126 Status = PeiServicesInstallPpi (&mDxeIplPpiList);
127 ASSERT_EFI_ERROR(Status);
128
129 return Status;
130 }
131
132 /**
133 This function installs the PPIs that require permanent memory.
134
135 @param PeiServices Indirect reference to the PEI Services Table.
136 @param NotifyDescriptor Address of the notification descriptor data structure.
137 @param Ppi Address of the PPI that was installed.
138
139 @return EFI_SUCCESS The PPIs were installed successfully.
140 @return Others Some error occurs during the execution of this function.
141
142 **/
143 EFI_STATUS
144 EFIAPI
145 InstallIplPermanentMemoryPpis (
146 IN EFI_PEI_SERVICES **PeiServices,
147 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
148 IN VOID *Ppi
149 )
150 {
151 EFI_STATUS Status;
152 EFI_GUID *ExtractHandlerGuidTable;
153 UINTN ExtractHandlerNumber;
154 EFI_PEI_PPI_DESCRIPTOR *GuidPpi;
155
156 //
157 // Get custom extract guided section method guid list
158 //
159 ExtractHandlerNumber = ExtractGuidedSectionGetGuidList (&ExtractHandlerGuidTable);
160
161 //
162 // Install custom guided section extraction PPI
163 //
164 if (ExtractHandlerNumber > 0) {
165 GuidPpi = (EFI_PEI_PPI_DESCRIPTOR *) AllocatePool (ExtractHandlerNumber * sizeof (EFI_PEI_PPI_DESCRIPTOR));
166 ASSERT (GuidPpi != NULL);
167 while (ExtractHandlerNumber-- > 0) {
168 GuidPpi->Flags = EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST;
169 GuidPpi->Ppi = (VOID *) &mCustomGuidedSectionExtractionPpi;
170 GuidPpi->Guid = &ExtractHandlerGuidTable[ExtractHandlerNumber];
171 Status = PeiServicesInstallPpi (GuidPpi++);
172 ASSERT_EFI_ERROR(Status);
173 }
174 }
175
176 //
177 // Install Decompress PPI.
178 //
179 Status = PeiServicesInstallPpi (&mDecompressPpiList);
180 ASSERT_EFI_ERROR(Status);
181
182 return Status;
183 }
184
185 /**
186 Validate variable data for the MemoryTypeInformation.
187
188 @param MemoryData Variable data.
189 @param MemoryDataSize Variable data length.
190
191 @return TRUE The variable data is valid.
192 @return FALSE The variable data is invalid.
193
194 **/
195 BOOLEAN
196 ValidateMemoryTypeInfoVariable (
197 IN EFI_MEMORY_TYPE_INFORMATION *MemoryData,
198 IN UINTN MemoryDataSize
199 )
200 {
201 UINTN Count;
202 UINTN Index;
203
204 // Check the input parameter.
205 if (MemoryData == NULL) {
206 return FALSE;
207 }
208
209 // Get Count
210 Count = MemoryDataSize / sizeof (*MemoryData);
211
212 // Check Size
213 if (Count * sizeof(*MemoryData) != MemoryDataSize) {
214 return FALSE;
215 }
216
217 // Check last entry type filed.
218 if (MemoryData[Count - 1].Type != EfiMaxMemoryType) {
219 return FALSE;
220 }
221
222 // Check the type filed.
223 for (Index = 0; Index < Count - 1; Index++) {
224 if (MemoryData[Index].Type >= EfiMaxMemoryType) {
225 return FALSE;
226 }
227 }
228
229 return TRUE;
230 }
231
232 /**
233 Main entry point to last PEIM.
234
235 This function finds DXE Core in the firmware volume and transfer the control to
236 DXE core.
237
238 @param This Entry point for DXE IPL PPI.
239 @param PeiServices General purpose services available to every PEIM.
240 @param HobList Address to the Pei HOB list.
241
242 @return EFI_SUCCESS DXE core was successfully loaded.
243 @return EFI_OUT_OF_RESOURCES There are not enough resources to load DXE core.
244
245 **/
246 EFI_STATUS
247 EFIAPI
248 DxeLoadCore (
249 IN CONST EFI_DXE_IPL_PPI *This,
250 IN EFI_PEI_SERVICES **PeiServices,
251 IN EFI_PEI_HOB_POINTERS HobList
252 )
253 {
254 EFI_STATUS Status;
255 EFI_FV_FILE_INFO DxeCoreFileInfo;
256 EFI_PHYSICAL_ADDRESS DxeCoreAddress;
257 UINT64 DxeCoreSize;
258 EFI_PHYSICAL_ADDRESS DxeCoreEntryPoint;
259 EFI_BOOT_MODE BootMode;
260 EFI_PEI_FILE_HANDLE FileHandle;
261 EFI_PEI_READ_ONLY_VARIABLE2_PPI *Variable;
262 EFI_PEI_LOAD_FILE_PPI *LoadFile;
263 UINTN Instance;
264 UINT32 AuthenticationState;
265 UINTN DataSize;
266 EFI_PEI_S3_RESUME2_PPI *S3Resume;
267 EFI_PEI_RECOVERY_MODULE_PPI *PeiRecovery;
268 EDKII_PEI_CAPSULE_ON_DISK_PPI *PeiCapsuleOnDisk;
269 EFI_MEMORY_TYPE_INFORMATION MemoryData[EfiMaxMemoryType + 1];
270 VOID *CapsuleOnDiskModePpi;
271
272 //
273 // if in S3 Resume, restore configure
274 //
275 BootMode = GetBootModeHob ();
276
277 if (BootMode == BOOT_ON_S3_RESUME) {
278 Status = PeiServicesLocatePpi (
279 &gEfiPeiS3Resume2PpiGuid,
280 0,
281 NULL,
282 (VOID **) &S3Resume
283 );
284 if (EFI_ERROR (Status)) {
285 //
286 // Report Status code that S3Resume PPI can not be found
287 //
288 REPORT_STATUS_CODE (
289 EFI_ERROR_CODE | EFI_ERROR_MAJOR,
290 (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_EC_S3_RESUME_PPI_NOT_FOUND)
291 );
292 }
293 ASSERT_EFI_ERROR (Status);
294
295 Status = S3Resume->S3RestoreConfig2 (S3Resume);
296 ASSERT_EFI_ERROR (Status);
297 } else if (BootMode == BOOT_IN_RECOVERY_MODE) {
298 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_PC_RECOVERY_BEGIN));
299 Status = PeiServicesLocatePpi (
300 &gEfiPeiRecoveryModulePpiGuid,
301 0,
302 NULL,
303 (VOID **) &PeiRecovery
304 );
305
306 if (EFI_ERROR (Status)) {
307 DEBUG ((DEBUG_ERROR, "Locate Recovery PPI Failed.(Status = %r)\n", Status));
308 //
309 // Report Status code the failure of locating Recovery PPI
310 //
311 REPORT_STATUS_CODE (
312 EFI_ERROR_CODE | EFI_ERROR_MAJOR,
313 (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_EC_RECOVERY_PPI_NOT_FOUND)
314 );
315 CpuDeadLoop ();
316 }
317
318 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_PC_CAPSULE_LOAD));
319 Status = PeiRecovery->LoadRecoveryCapsule (PeiServices, PeiRecovery);
320 if (EFI_ERROR (Status)) {
321 DEBUG ((DEBUG_ERROR, "Load Recovery Capsule Failed.(Status = %r)\n", Status));
322 //
323 // Report Status code that recovery image can not be found
324 //
325 REPORT_STATUS_CODE (
326 EFI_ERROR_CODE | EFI_ERROR_MAJOR,
327 (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_EC_NO_RECOVERY_CAPSULE)
328 );
329 CpuDeadLoop ();
330 }
331 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_PC_CAPSULE_START));
332 //
333 // Now should have a HOB with the DXE core
334 //
335 } else if (BootMode == BOOT_ON_FLASH_UPDATE) {
336 //
337 // If Capsule On Disk mode, call storage stack to read Capsule Relocation file
338 // IoMmmu is highly recommmended to enable before reading
339 //
340 Status = PeiServicesLocatePpi (
341 &gEdkiiPeiBootInCapsuleOnDiskModePpiGuid,
342 0,
343 NULL,
344 &CapsuleOnDiskModePpi
345 );
346 if (!EFI_ERROR(Status)) {
347 Status = PeiServicesLocatePpi (
348 &gEdkiiPeiCapsuleOnDiskPpiGuid,
349 0,
350 NULL,
351 (VOID **) &PeiCapsuleOnDisk
352 );
353
354 //
355 // Whether failed, still goes to Firmware Update boot path. BDS will clear corresponding indicator and reboot later on
356 //
357 if (!EFI_ERROR (Status)) {
358 Status = PeiCapsuleOnDisk->LoadCapsuleOnDisk (PeiServices, PeiCapsuleOnDisk);
359 }
360 }
361 }
362
363 if (GetFirstGuidHob ((CONST EFI_GUID *)&gEfiMemoryTypeInformationGuid) == NULL) {
364 //
365 // Don't build GuidHob if GuidHob has been installed.
366 //
367 Status = PeiServicesLocatePpi (
368 &gEfiPeiReadOnlyVariable2PpiGuid,
369 0,
370 NULL,
371 (VOID **)&Variable
372 );
373 if (!EFI_ERROR (Status)) {
374 DataSize = sizeof (MemoryData);
375 Status = Variable->GetVariable (
376 Variable,
377 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,
378 &gEfiMemoryTypeInformationGuid,
379 NULL,
380 &DataSize,
381 &MemoryData
382 );
383 if (!EFI_ERROR (Status) && ValidateMemoryTypeInfoVariable(MemoryData, DataSize)) {
384 //
385 // Build the GUID'd HOB for DXE
386 //
387 BuildGuidDataHob (
388 &gEfiMemoryTypeInformationGuid,
389 MemoryData,
390 DataSize
391 );
392 }
393 }
394 }
395
396 //
397 // Look in all the FVs present in PEI and find the DXE Core FileHandle
398 //
399 FileHandle = DxeIplFindDxeCore ();
400
401 //
402 // Load the DXE Core from a Firmware Volume.
403 //
404 Instance = 0;
405 do {
406 Status = PeiServicesLocatePpi (&gEfiPeiLoadFilePpiGuid, Instance++, NULL, (VOID **) &LoadFile);
407 //
408 // These must exist an instance of EFI_PEI_LOAD_FILE_PPI to support to load DxeCore file handle successfully.
409 //
410 ASSERT_EFI_ERROR (Status);
411
412 Status = LoadFile->LoadFile (
413 LoadFile,
414 FileHandle,
415 &DxeCoreAddress,
416 &DxeCoreSize,
417 &DxeCoreEntryPoint,
418 &AuthenticationState
419 );
420 } while (EFI_ERROR (Status));
421
422 //
423 // Get the DxeCore File Info from the FileHandle for the DxeCore GUID file name.
424 //
425 Status = PeiServicesFfsGetFileInfo (FileHandle, &DxeCoreFileInfo);
426 ASSERT_EFI_ERROR (Status);
427
428 //
429 // Add HOB for the DXE Core
430 //
431 BuildModuleHob (
432 &DxeCoreFileInfo.FileName,
433 DxeCoreAddress,
434 ALIGN_VALUE (DxeCoreSize, EFI_PAGE_SIZE),
435 DxeCoreEntryPoint
436 );
437
438 //
439 // Report Status Code EFI_SW_PEI_PC_HANDOFF_TO_NEXT
440 //
441 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_PEI_CORE | EFI_SW_PEI_CORE_PC_HANDOFF_TO_NEXT));
442
443 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Loading DXE CORE at 0x%11p EntryPoint=0x%11p\n", (VOID *)(UINTN)DxeCoreAddress, FUNCTION_ENTRY_POINT (DxeCoreEntryPoint)));
444
445 //
446 // Transfer control to the DXE Core
447 // The hand off state is simply a pointer to the HOB list
448 //
449 HandOffToDxeCore (DxeCoreEntryPoint, HobList);
450 //
451 // If we get here, then the DXE Core returned. This is an error
452 // DxeCore should not return.
453 //
454 ASSERT (FALSE);
455 CpuDeadLoop ();
456
457 return EFI_OUT_OF_RESOURCES;
458 }
459
460
461 /**
462 Searches DxeCore in all firmware Volumes and loads the first
463 instance that contains DxeCore.
464
465 @return FileHandle of DxeCore to load DxeCore.
466
467 **/
468 EFI_PEI_FILE_HANDLE
469 DxeIplFindDxeCore (
470 VOID
471 )
472 {
473 EFI_STATUS Status;
474 UINTN Instance;
475 EFI_PEI_FV_HANDLE VolumeHandle;
476 EFI_PEI_FILE_HANDLE FileHandle;
477
478 Instance = 0;
479 while (TRUE) {
480 //
481 // Traverse all firmware volume instances
482 //
483 Status = PeiServicesFfsFindNextVolume (Instance, &VolumeHandle);
484 //
485 // If some error occurs here, then we cannot find any firmware
486 // volume that may contain DxeCore.
487 //
488 if (EFI_ERROR (Status)) {
489 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_CORE_EC_DXE_CORRUPT));
490 }
491 ASSERT_EFI_ERROR (Status);
492
493 //
494 // Find the DxeCore file type from the beginning in this firmware volume.
495 //
496 FileHandle = NULL;
497 Status = PeiServicesFfsFindNextFile (EFI_FV_FILETYPE_DXE_CORE, VolumeHandle, &FileHandle);
498 if (!EFI_ERROR (Status)) {
499 //
500 // Find DxeCore FileHandle in this volume, then we skip other firmware volume and
501 // return the FileHandle.
502 //
503 return FileHandle;
504 }
505 //
506 // We cannot find DxeCore in this firmware volume, then search the next volume.
507 //
508 Instance++;
509 }
510 }
511
512
513
514 /**
515 The ExtractSection() function processes the input section and
516 returns a pointer to the section contents. If the section being
517 extracted does not require processing (if the section
518 GuidedSectionHeader.Attributes has the
519 EFI_GUIDED_SECTION_PROCESSING_REQUIRED field cleared), then
520 OutputBuffer is just updated to point to the start of the
521 section's contents. Otherwise, *Buffer must be allocated
522 from PEI permanent memory.
523
524 @param This Indicates the
525 EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI instance.
526 Buffer containing the input GUIDed section to be
527 processed. OutputBuffer OutputBuffer is
528 allocated from PEI permanent memory and contains
529 the new section stream.
530 @param InputSection A pointer to the input buffer, which contains
531 the input section to be processed.
532 @param OutputBuffer A pointer to a caller-allocated buffer, whose
533 size is specified by the contents of OutputSize.
534 @param OutputSize A pointer to a caller-allocated
535 UINTN in which the size of *OutputBuffer
536 allocation is stored. If the function
537 returns anything other than EFI_SUCCESS,
538 the value of OutputSize is undefined.
539 @param AuthenticationStatus A pointer to a caller-allocated
540 UINT32 that indicates the
541 authentication status of the
542 output buffer. If the input
543 section's GuidedSectionHeader.
544 Attributes field has the
545 EFI_GUIDED_SECTION_AUTH_STATUS_VALID
546 bit as clear,
547 AuthenticationStatus must return
548 zero. These bits reflect the
549 status of the extraction
550 operation. If the function
551 returns anything other than
552 EFI_SUCCESS, the value of
553 AuthenticationStatus is
554 undefined.
555
556 @retval EFI_SUCCESS The InputSection was
557 successfully processed and the
558 section contents were returned.
559
560 @retval EFI_OUT_OF_RESOURCES The system has insufficient
561 resources to process the request.
562
563 @retval EFI_INVALID_PARAMETER The GUID in InputSection does
564 not match this instance of the
565 GUIDed Section Extraction PPI.
566
567 **/
568 EFI_STATUS
569 EFIAPI
570 CustomGuidedSectionExtract (
571 IN CONST EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI *This,
572 IN CONST VOID *InputSection,
573 OUT VOID **OutputBuffer,
574 OUT UINTN *OutputSize,
575 OUT UINT32 *AuthenticationStatus
576 )
577 {
578 EFI_STATUS Status;
579 UINT8 *ScratchBuffer;
580 UINT32 ScratchBufferSize;
581 UINT32 OutputBufferSize;
582 UINT16 SectionAttribute;
583
584 //
585 // Init local variable
586 //
587 ScratchBuffer = NULL;
588
589 //
590 // Call GetInfo to get the size and attribute of input guided section data.
591 //
592 Status = ExtractGuidedSectionGetInfo (
593 InputSection,
594 &OutputBufferSize,
595 &ScratchBufferSize,
596 &SectionAttribute
597 );
598
599 if (EFI_ERROR (Status)) {
600 DEBUG ((DEBUG_ERROR, "GetInfo from guided section Failed - %r\n", Status));
601 return Status;
602 }
603
604 if (ScratchBufferSize != 0) {
605 //
606 // Allocate scratch buffer
607 //
608 ScratchBuffer = AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize));
609 if (ScratchBuffer == NULL) {
610 return EFI_OUT_OF_RESOURCES;
611 }
612 }
613
614 if (((SectionAttribute & EFI_GUIDED_SECTION_PROCESSING_REQUIRED) != 0) && OutputBufferSize > 0) {
615 //
616 // Allocate output buffer
617 //
618 *OutputBuffer = AllocatePages (EFI_SIZE_TO_PAGES (OutputBufferSize));
619 if (*OutputBuffer == NULL) {
620 return EFI_OUT_OF_RESOURCES;
621 }
622 DEBUG ((DEBUG_INFO, "Customized Guided section Memory Size required is 0x%x and address is 0x%p\n", OutputBufferSize, *OutputBuffer));
623 }
624
625 Status = ExtractGuidedSectionDecode (
626 InputSection,
627 OutputBuffer,
628 ScratchBuffer,
629 AuthenticationStatus
630 );
631 if (EFI_ERROR (Status)) {
632 //
633 // Decode failed
634 //
635 DEBUG ((DEBUG_ERROR, "Extract guided section Failed - %r\n", Status));
636 return Status;
637 }
638
639 *OutputSize = (UINTN) OutputBufferSize;
640
641 return EFI_SUCCESS;
642 }
643
644
645
646 /**
647 Decompresses a section to the output buffer.
648
649 This function looks up the compression type field in the input section and
650 applies the appropriate compression algorithm to compress the section to a
651 callee allocated buffer.
652
653 @param This Points to this instance of the
654 EFI_PEI_DECOMPRESS_PEI PPI.
655 @param CompressionSection Points to the compressed section.
656 @param OutputBuffer Holds the returned pointer to the decompressed
657 sections.
658 @param OutputSize Holds the returned size of the decompress
659 section streams.
660
661 @retval EFI_SUCCESS The section was decompressed successfully.
662 OutputBuffer contains the resulting data and
663 OutputSize contains the resulting size.
664
665 **/
666 EFI_STATUS
667 EFIAPI
668 Decompress (
669 IN CONST EFI_PEI_DECOMPRESS_PPI *This,
670 IN CONST EFI_COMPRESSION_SECTION *CompressionSection,
671 OUT VOID **OutputBuffer,
672 OUT UINTN *OutputSize
673 )
674 {
675 EFI_STATUS Status;
676 UINT8 *DstBuffer;
677 UINT8 *ScratchBuffer;
678 UINT32 DstBufferSize;
679 UINT32 ScratchBufferSize;
680 VOID *CompressionSource;
681 UINT32 CompressionSourceSize;
682 UINT32 UncompressedLength;
683 UINT8 CompressionType;
684
685 if (CompressionSection->CommonHeader.Type != EFI_SECTION_COMPRESSION) {
686 ASSERT (FALSE);
687 return EFI_INVALID_PARAMETER;
688 }
689
690 if (IS_SECTION2 (CompressionSection)) {
691 CompressionSource = (VOID *) ((UINT8 *) CompressionSection + sizeof (EFI_COMPRESSION_SECTION2));
692 CompressionSourceSize = (UINT32) (SECTION2_SIZE (CompressionSection) - sizeof (EFI_COMPRESSION_SECTION2));
693 UncompressedLength = ((EFI_COMPRESSION_SECTION2 *) CompressionSection)->UncompressedLength;
694 CompressionType = ((EFI_COMPRESSION_SECTION2 *) CompressionSection)->CompressionType;
695 } else {
696 CompressionSource = (VOID *) ((UINT8 *) CompressionSection + sizeof (EFI_COMPRESSION_SECTION));
697 CompressionSourceSize = (UINT32) (SECTION_SIZE (CompressionSection) - sizeof (EFI_COMPRESSION_SECTION));
698 UncompressedLength = CompressionSection->UncompressedLength;
699 CompressionType = CompressionSection->CompressionType;
700 }
701
702 //
703 // This is a compression set, expand it
704 //
705 switch (CompressionType) {
706 case EFI_STANDARD_COMPRESSION:
707 if (FeaturePcdGet(PcdDxeIplSupportUefiDecompress)) {
708 //
709 // Load EFI standard compression.
710 // For compressed data, decompress them to destination buffer.
711 //
712 Status = UefiDecompressGetInfo (
713 CompressionSource,
714 CompressionSourceSize,
715 &DstBufferSize,
716 &ScratchBufferSize
717 );
718 if (EFI_ERROR (Status)) {
719 //
720 // GetInfo failed
721 //
722 DEBUG ((DEBUG_ERROR, "Decompress GetInfo Failed - %r\n", Status));
723 return EFI_NOT_FOUND;
724 }
725 //
726 // Allocate scratch buffer
727 //
728 ScratchBuffer = AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize));
729 if (ScratchBuffer == NULL) {
730 return EFI_OUT_OF_RESOURCES;
731 }
732 //
733 // Allocate destination buffer
734 //
735 DstBuffer = AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize));
736 if (DstBuffer == NULL) {
737 return EFI_OUT_OF_RESOURCES;
738 }
739 //
740 // Call decompress function
741 //
742 Status = UefiDecompress (
743 CompressionSource,
744 DstBuffer,
745 ScratchBuffer
746 );
747 if (EFI_ERROR (Status)) {
748 //
749 // Decompress failed
750 //
751 DEBUG ((DEBUG_ERROR, "Decompress Failed - %r\n", Status));
752 return EFI_NOT_FOUND;
753 }
754 break;
755 } else {
756 //
757 // PcdDxeIplSupportUefiDecompress is FALSE
758 // Don't support UEFI decompression algorithm.
759 //
760 ASSERT (FALSE);
761 return EFI_NOT_FOUND;
762 }
763
764 case EFI_NOT_COMPRESSED:
765 //
766 // Allocate destination buffer
767 //
768 DstBufferSize = UncompressedLength;
769 DstBuffer = AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize));
770 if (DstBuffer == NULL) {
771 return EFI_OUT_OF_RESOURCES;
772 }
773 //
774 // stream is not actually compressed, just encapsulated. So just copy it.
775 //
776 CopyMem (DstBuffer, CompressionSource, DstBufferSize);
777 break;
778
779 default:
780 //
781 // Don't support other unknown compression type.
782 //
783 ASSERT (FALSE);
784 return EFI_NOT_FOUND;
785 }
786
787 *OutputSize = DstBufferSize;
788 *OutputBuffer = DstBuffer;
789
790 return EFI_SUCCESS;
791 }
792
793
794 /**
795 Updates the Stack HOB passed to DXE phase.
796
797 This function traverses the whole HOB list and update the stack HOB to
798 reflect the real stack that is used by DXE core.
799
800 @param BaseAddress The lower address of stack used by DxeCore.
801 @param Length The length of stack used by DxeCore.
802
803 **/
804 VOID
805 UpdateStackHob (
806 IN EFI_PHYSICAL_ADDRESS BaseAddress,
807 IN UINT64 Length
808 )
809 {
810 EFI_PEI_HOB_POINTERS Hob;
811
812 Hob.Raw = GetHobList ();
813 while ((Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw)) != NULL) {
814 if (CompareGuid (&gEfiHobMemoryAllocStackGuid, &(Hob.MemoryAllocationStack->AllocDescriptor.Name))) {
815 //
816 // Build a new memory allocation HOB with old stack info with EfiBootServicesData type. Need to
817 // avoid this region be reclaimed by DXE core as the IDT built in SEC might be on stack, and some
818 // PEIMs may also keep key information on stack
819 //
820 BuildMemoryAllocationHob (
821 Hob.MemoryAllocationStack->AllocDescriptor.MemoryBaseAddress,
822 Hob.MemoryAllocationStack->AllocDescriptor.MemoryLength,
823 EfiBootServicesData
824 );
825 //
826 // Update the BSP Stack Hob to reflect the new stack info.
827 //
828 Hob.MemoryAllocationStack->AllocDescriptor.MemoryBaseAddress = BaseAddress;
829 Hob.MemoryAllocationStack->AllocDescriptor.MemoryLength = Length;
830 break;
831 }
832 Hob.Raw = GET_NEXT_HOB (Hob);
833 }
834 }
835