]> 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 // Module Globals used in the DXE to PEI hand off
15 // These must be module globals, so the stack can be switched
16 //
17 CONST EFI_DXE_IPL_PPI mDxeIplPpi = {
18 DxeLoadCore
19 };
20
21 CONST EFI_PEI_PPI_DESCRIPTOR mDxeIplPpiList = {
22 EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
23 &gEfiDxeIplPpiGuid,
24 (VOID *)&mDxeIplPpi
25 };
26
27 CONST EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI mCustomGuidedSectionExtractionPpi = {
28 CustomGuidedSectionExtract
29 };
30
31 CONST EFI_PEI_DECOMPRESS_PPI mDecompressPpi = {
32 Decompress
33 };
34
35 CONST EFI_PEI_PPI_DESCRIPTOR mDecompressPpiList = {
36 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
37 &gEfiPeiDecompressPpiGuid,
38 (VOID *)&mDecompressPpi
39 };
40
41 CONST EFI_PEI_PPI_DESCRIPTOR gEndOfPeiSignalPpi = {
42 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
43 &gEfiEndOfPeiSignalPpiGuid,
44 NULL
45 };
46
47 CONST EFI_PEI_NOTIFY_DESCRIPTOR mMemoryDiscoveredNotifyList = {
48 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_DISPATCH | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
49 &gEfiPeiMemoryDiscoveredPpiGuid,
50 InstallIplPermanentMemoryPpis
51 };
52
53 /**
54 Entry point of DXE IPL PEIM.
55
56 This function installs DXE IPL PPI. It also reloads
57 itself to memory on non-S3 resume boot path.
58
59 @param FileHandle Handle of the file being invoked.
60 @param PeiServices Describes the list of possible PEI Services.
61
62 @retval EFI_SUCESS The entry point of DXE IPL PEIM executes successfully.
63 @retval Others Some error occurs during the execution of this function.
64
65 **/
66 EFI_STATUS
67 EFIAPI
68 PeimInitializeDxeIpl (
69 IN EFI_PEI_FILE_HANDLE FileHandle,
70 IN CONST EFI_PEI_SERVICES **PeiServices
71 )
72 {
73 EFI_STATUS Status;
74 EFI_BOOT_MODE BootMode;
75 VOID *Dummy;
76
77 BootMode = GetBootModeHob ();
78
79 if (BootMode != BOOT_ON_S3_RESUME) {
80 Status = PeiServicesRegisterForShadow (FileHandle);
81 if (Status == EFI_SUCCESS) {
82 //
83 // EFI_SUCESS means it is the first time to call register for shadow.
84 //
85 return Status;
86 }
87
88 //
89 // Ensure that DXE IPL is shadowed to permanent memory.
90 //
91 ASSERT (Status == EFI_ALREADY_STARTED);
92
93 //
94 // DXE core load requires permanent memory.
95 //
96 Status = PeiServicesLocatePpi (
97 &gEfiPeiMemoryDiscoveredPpiGuid,
98 0,
99 NULL,
100 (VOID **)&Dummy
101 );
102 ASSERT_EFI_ERROR (Status);
103 if (EFI_ERROR (Status)) {
104 return Status;
105 }
106
107 //
108 // Now the permanent memory exists, install the PPIs for decompression
109 // and section extraction.
110 //
111 Status = InstallIplPermanentMemoryPpis (NULL, NULL, NULL);
112 ASSERT_EFI_ERROR (Status);
113 } else {
114 //
115 // Install memory discovered PPI notification to install PPIs for
116 // decompression and section extraction.
117 //
118 Status = PeiServicesNotifyPpi (&mMemoryDiscoveredNotifyList);
119 ASSERT_EFI_ERROR (Status);
120 }
121
122 //
123 // Install DxeIpl PPI.
124 //
125 Status = PeiServicesInstallPpi (&mDxeIplPpiList);
126 ASSERT_EFI_ERROR (Status);
127
128 return Status;
129 }
130
131 /**
132 This function installs the PPIs that require permanent memory.
133
134 @param PeiServices Indirect reference to the PEI Services Table.
135 @param NotifyDescriptor Address of the notification descriptor data structure.
136 @param Ppi Address of the PPI that was installed.
137
138 @return EFI_SUCCESS The PPIs were installed successfully.
139 @return Others Some error occurs during the execution of this function.
140
141 **/
142 EFI_STATUS
143 EFIAPI
144 InstallIplPermanentMemoryPpis (
145 IN EFI_PEI_SERVICES **PeiServices,
146 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
147 IN VOID *Ppi
148 )
149 {
150 EFI_STATUS Status;
151 EFI_GUID *ExtractHandlerGuidTable;
152 UINTN ExtractHandlerNumber;
153 EFI_PEI_PPI_DESCRIPTOR *GuidPpi;
154
155 //
156 // Get custom extract guided section method guid list
157 //
158 ExtractHandlerNumber = ExtractGuidedSectionGetGuidList (&ExtractHandlerGuidTable);
159
160 //
161 // Install custom guided section extraction PPI
162 //
163 if (ExtractHandlerNumber > 0) {
164 GuidPpi = (EFI_PEI_PPI_DESCRIPTOR *)AllocatePool (ExtractHandlerNumber * sizeof (EFI_PEI_PPI_DESCRIPTOR));
165 ASSERT (GuidPpi != NULL);
166 while (ExtractHandlerNumber-- > 0) {
167 GuidPpi->Flags = EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST;
168 GuidPpi->Ppi = (VOID *)&mCustomGuidedSectionExtractionPpi;
169 GuidPpi->Guid = &ExtractHandlerGuidTable[ExtractHandlerNumber];
170 Status = PeiServicesInstallPpi (GuidPpi++);
171 ASSERT_EFI_ERROR (Status);
172 }
173 }
174
175 //
176 // Install Decompress PPI.
177 //
178 Status = PeiServicesInstallPpi (&mDecompressPpiList);
179 ASSERT_EFI_ERROR (Status);
180
181 return Status;
182 }
183
184 /**
185 Validate variable data for the MemoryTypeInformation.
186
187 @param MemoryData Variable data.
188 @param MemoryDataSize Variable data length.
189
190 @return TRUE The variable data is valid.
191 @return FALSE The variable data is invalid.
192
193 **/
194 BOOLEAN
195 ValidateMemoryTypeInfoVariable (
196 IN EFI_MEMORY_TYPE_INFORMATION *MemoryData,
197 IN UINTN MemoryDataSize
198 )
199 {
200 UINTN Count;
201 UINTN Index;
202
203 // Check the input parameter.
204 if (MemoryData == NULL) {
205 return FALSE;
206 }
207
208 // Get Count
209 Count = MemoryDataSize / sizeof (*MemoryData);
210
211 // Check Size
212 if (Count * sizeof (*MemoryData) != MemoryDataSize) {
213 return FALSE;
214 }
215
216 // Check last entry type filed.
217 if (MemoryData[Count - 1].Type != EfiMaxMemoryType) {
218 return FALSE;
219 }
220
221 // Check the type filed.
222 for (Index = 0; Index < Count - 1; Index++) {
223 if (MemoryData[Index].Type >= EfiMaxMemoryType) {
224 return FALSE;
225 }
226 }
227
228 return TRUE;
229 }
230
231 /**
232 Main entry point to last PEIM.
233
234 This function finds DXE Core in the firmware volume and transfer the control to
235 DXE core.
236
237 @param This Entry point for DXE IPL PPI.
238 @param PeiServices General purpose services available to every PEIM.
239 @param HobList Address to the Pei HOB list.
240
241 @return EFI_SUCCESS DXE core was successfully loaded.
242 @return EFI_OUT_OF_RESOURCES There are not enough resources to load DXE core.
243
244 **/
245 EFI_STATUS
246 EFIAPI
247 DxeLoadCore (
248 IN CONST EFI_DXE_IPL_PPI *This,
249 IN EFI_PEI_SERVICES **PeiServices,
250 IN EFI_PEI_HOB_POINTERS HobList
251 )
252 {
253 EFI_STATUS Status;
254 EFI_FV_FILE_INFO DxeCoreFileInfo;
255 EFI_PHYSICAL_ADDRESS DxeCoreAddress;
256 UINT64 DxeCoreSize;
257 EFI_PHYSICAL_ADDRESS DxeCoreEntryPoint;
258 EFI_BOOT_MODE BootMode;
259 EFI_PEI_FILE_HANDLE FileHandle;
260 EFI_PEI_READ_ONLY_VARIABLE2_PPI *Variable;
261 EFI_PEI_LOAD_FILE_PPI *LoadFile;
262 UINTN Instance;
263 UINT32 AuthenticationState;
264 UINTN DataSize;
265 EFI_PEI_S3_RESUME2_PPI *S3Resume;
266 EFI_PEI_RECOVERY_MODULE_PPI *PeiRecovery;
267 EDKII_PEI_CAPSULE_ON_DISK_PPI *PeiCapsuleOnDisk;
268 EFI_MEMORY_TYPE_INFORMATION MemoryData[EfiMaxMemoryType + 1];
269 VOID *CapsuleOnDiskModePpi;
270
271 //
272 // if in S3 Resume, restore configure
273 //
274 BootMode = GetBootModeHob ();
275
276 if (BootMode == BOOT_ON_S3_RESUME) {
277 Status = PeiServicesLocatePpi (
278 &gEfiPeiS3Resume2PpiGuid,
279 0,
280 NULL,
281 (VOID **)&S3Resume
282 );
283 if (EFI_ERROR (Status)) {
284 //
285 // Report Status code that S3Resume PPI can not be found
286 //
287 REPORT_STATUS_CODE (
288 EFI_ERROR_CODE | EFI_ERROR_MAJOR,
289 (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_EC_S3_RESUME_PPI_NOT_FOUND)
290 );
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
332 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_PC_CAPSULE_START));
333 //
334 // Now should have a HOB with the DXE core
335 //
336 } else if (BootMode == BOOT_ON_FLASH_UPDATE) {
337 //
338 // If Capsule On Disk mode, call storage stack to read Capsule Relocation file
339 // IoMmmu is highly recommmended to enable before reading
340 //
341 Status = PeiServicesLocatePpi (
342 &gEdkiiPeiBootInCapsuleOnDiskModePpiGuid,
343 0,
344 NULL,
345 &CapsuleOnDiskModePpi
346 );
347 if (!EFI_ERROR (Status)) {
348 Status = PeiServicesLocatePpi (
349 &gEdkiiPeiCapsuleOnDiskPpiGuid,
350 0,
351 NULL,
352 (VOID **)&PeiCapsuleOnDisk
353 );
354
355 //
356 // Whether failed, still goes to Firmware Update boot path. BDS will clear corresponding indicator and reboot later on
357 //
358 if (!EFI_ERROR (Status)) {
359 Status = PeiCapsuleOnDisk->LoadCapsuleOnDisk (PeiServices, PeiCapsuleOnDisk);
360 }
361 }
362 }
363
364 if (GetFirstGuidHob ((CONST EFI_GUID *)&gEfiMemoryTypeInformationGuid) == NULL) {
365 //
366 // Don't build GuidHob if GuidHob has been installed.
367 //
368 Status = PeiServicesLocatePpi (
369 &gEfiPeiReadOnlyVariable2PpiGuid,
370 0,
371 NULL,
372 (VOID **)&Variable
373 );
374 if (!EFI_ERROR (Status)) {
375 DataSize = sizeof (MemoryData);
376 Status = Variable->GetVariable (
377 Variable,
378 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,
379 &gEfiMemoryTypeInformationGuid,
380 NULL,
381 &DataSize,
382 &MemoryData
383 );
384 if (!EFI_ERROR (Status) && ValidateMemoryTypeInfoVariable (MemoryData, DataSize)) {
385 //
386 // Build the GUID'd HOB for DXE
387 //
388 BuildGuidDataHob (
389 &gEfiMemoryTypeInformationGuid,
390 MemoryData,
391 DataSize
392 );
393 }
394 }
395 }
396
397 //
398 // Look in all the FVs present in PEI and find the DXE Core FileHandle
399 //
400 FileHandle = DxeIplFindDxeCore ();
401
402 //
403 // Load the DXE Core from a Firmware Volume.
404 //
405 Instance = 0;
406 do {
407 Status = PeiServicesLocatePpi (&gEfiPeiLoadFilePpiGuid, Instance++, NULL, (VOID **)&LoadFile);
408 //
409 // These must exist an instance of EFI_PEI_LOAD_FILE_PPI to support to load DxeCore file handle successfully.
410 //
411 ASSERT_EFI_ERROR (Status);
412
413 Status = LoadFile->LoadFile (
414 LoadFile,
415 FileHandle,
416 &DxeCoreAddress,
417 &DxeCoreSize,
418 &DxeCoreEntryPoint,
419 &AuthenticationState
420 );
421 } while (EFI_ERROR (Status));
422
423 //
424 // Get the DxeCore File Info from the FileHandle for the DxeCore GUID file name.
425 //
426 Status = PeiServicesFfsGetFileInfo (FileHandle, &DxeCoreFileInfo);
427 ASSERT_EFI_ERROR (Status);
428
429 //
430 // Add HOB for the DXE Core
431 //
432 BuildModuleHob (
433 &DxeCoreFileInfo.FileName,
434 DxeCoreAddress,
435 ALIGN_VALUE (DxeCoreSize, EFI_PAGE_SIZE),
436 DxeCoreEntryPoint
437 );
438
439 //
440 // Report Status Code EFI_SW_PEI_PC_HANDOFF_TO_NEXT
441 //
442 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_PEI_CORE | EFI_SW_PEI_CORE_PC_HANDOFF_TO_NEXT));
443
444 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Loading DXE CORE at 0x%11p EntryPoint=0x%11p\n", (VOID *)(UINTN)DxeCoreAddress, FUNCTION_ENTRY_POINT (DxeCoreEntryPoint)));
445
446 //
447 // Transfer control to the DXE Core
448 // The hand off state is simply a pointer to the HOB list
449 //
450 HandOffToDxeCore (DxeCoreEntryPoint, HobList);
451 //
452 // If we get here, then the DXE Core returned. This is an error
453 // DxeCore should not return.
454 //
455 ASSERT (FALSE);
456 CpuDeadLoop ();
457
458 return EFI_OUT_OF_RESOURCES;
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
492 ASSERT_EFI_ERROR (Status);
493
494 //
495 // Find the DxeCore file type from the beginning in this firmware volume.
496 //
497 FileHandle = NULL;
498 Status = PeiServicesFfsFindNextFile (EFI_FV_FILETYPE_DXE_CORE, VolumeHandle, &FileHandle);
499 if (!EFI_ERROR (Status)) {
500 //
501 // Find DxeCore FileHandle in this volume, then we skip other firmware volume and
502 // return the FileHandle.
503 //
504 return FileHandle;
505 }
506
507 //
508 // We cannot find DxeCore in this firmware volume, then search the next volume.
509 //
510 Instance++;
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
623 DEBUG ((DEBUG_INFO, "Customized Guided section Memory Size required is 0x%x and address is 0x%p\n", OutputBufferSize, *OutputBuffer));
624 }
625
626 Status = ExtractGuidedSectionDecode (
627 InputSection,
628 OutputBuffer,
629 ScratchBuffer,
630 AuthenticationStatus
631 );
632 if (EFI_ERROR (Status)) {
633 //
634 // Decode failed
635 //
636 DEBUG ((DEBUG_ERROR, "Extract guided section Failed - %r\n", Status));
637 return Status;
638 }
639
640 *OutputSize = (UINTN)OutputBufferSize;
641
642 return EFI_SUCCESS;
643 }
644
645 /**
646 Decompresses a section to the output buffer.
647
648 This function looks up the compression type field in the input section and
649 applies the appropriate compression algorithm to compress the section to a
650 callee allocated buffer.
651
652 @param This Points to this instance of the
653 EFI_PEI_DECOMPRESS_PEI PPI.
654 @param CompressionSection Points to the compressed section.
655 @param OutputBuffer Holds the returned pointer to the decompressed
656 sections.
657 @param OutputSize Holds the returned size of the decompress
658 section streams.
659
660 @retval EFI_SUCCESS The section was decompressed successfully.
661 OutputBuffer contains the resulting data and
662 OutputSize contains the resulting size.
663
664 **/
665 EFI_STATUS
666 EFIAPI
667 Decompress (
668 IN CONST EFI_PEI_DECOMPRESS_PPI *This,
669 IN CONST EFI_COMPRESSION_SECTION *CompressionSection,
670 OUT VOID **OutputBuffer,
671 OUT UINTN *OutputSize
672 )
673 {
674 EFI_STATUS Status;
675 UINT8 *DstBuffer;
676 UINT8 *ScratchBuffer;
677 UINT32 DstBufferSize;
678 UINT32 ScratchBufferSize;
679 VOID *CompressionSource;
680 UINT32 CompressionSourceSize;
681 UINT32 UncompressedLength;
682 UINT8 CompressionType;
683
684 if (CompressionSection->CommonHeader.Type != EFI_SECTION_COMPRESSION) {
685 ASSERT (FALSE);
686 return EFI_INVALID_PARAMETER;
687 }
688
689 if (IS_SECTION2 (CompressionSection)) {
690 CompressionSource = (VOID *)((UINT8 *)CompressionSection + sizeof (EFI_COMPRESSION_SECTION2));
691 CompressionSourceSize = (UINT32)(SECTION2_SIZE (CompressionSection) - sizeof (EFI_COMPRESSION_SECTION2));
692 UncompressedLength = ((EFI_COMPRESSION_SECTION2 *)CompressionSection)->UncompressedLength;
693 CompressionType = ((EFI_COMPRESSION_SECTION2 *)CompressionSection)->CompressionType;
694 } else {
695 CompressionSource = (VOID *)((UINT8 *)CompressionSection + sizeof (EFI_COMPRESSION_SECTION));
696 CompressionSourceSize = (UINT32)(SECTION_SIZE (CompressionSection) - sizeof (EFI_COMPRESSION_SECTION));
697 UncompressedLength = CompressionSection->UncompressedLength;
698 CompressionType = CompressionSection->CompressionType;
699 }
700
701 //
702 // This is a compression set, expand it
703 //
704 switch (CompressionType) {
705 case EFI_STANDARD_COMPRESSION:
706 if (FeaturePcdGet (PcdDxeIplSupportUefiDecompress)) {
707 //
708 // Load EFI standard compression.
709 // For compressed data, decompress them to destination buffer.
710 //
711 Status = UefiDecompressGetInfo (
712 CompressionSource,
713 CompressionSourceSize,
714 &DstBufferSize,
715 &ScratchBufferSize
716 );
717 if (EFI_ERROR (Status)) {
718 //
719 // GetInfo failed
720 //
721 DEBUG ((DEBUG_ERROR, "Decompress GetInfo Failed - %r\n", Status));
722 return EFI_NOT_FOUND;
723 }
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 //
734 // Allocate destination buffer
735 //
736 DstBuffer = AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize));
737 if (DstBuffer == NULL) {
738 return EFI_OUT_OF_RESOURCES;
739 }
740
741 //
742 // Call decompress function
743 //
744 Status = UefiDecompress (
745 CompressionSource,
746 DstBuffer,
747 ScratchBuffer
748 );
749 if (EFI_ERROR (Status)) {
750 //
751 // Decompress failed
752 //
753 DEBUG ((DEBUG_ERROR, "Decompress Failed - %r\n", Status));
754 return EFI_NOT_FOUND;
755 }
756
757 break;
758 } else {
759 //
760 // PcdDxeIplSupportUefiDecompress is FALSE
761 // Don't support UEFI decompression algorithm.
762 //
763 ASSERT (FALSE);
764 return EFI_NOT_FOUND;
765 }
766
767 case EFI_NOT_COMPRESSED:
768 //
769 // Allocate destination buffer
770 //
771 DstBufferSize = UncompressedLength;
772 DstBuffer = AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize));
773 if (DstBuffer == NULL) {
774 return EFI_OUT_OF_RESOURCES;
775 }
776
777 //
778 // stream is not actually compressed, just encapsulated. So just copy it.
779 //
780 CopyMem (DstBuffer, CompressionSource, DstBufferSize);
781 break;
782
783 default:
784 //
785 // Don't support other unknown compression type.
786 //
787 ASSERT (FALSE);
788 return EFI_NOT_FOUND;
789 }
790
791 *OutputSize = DstBufferSize;
792 *OutputBuffer = DstBuffer;
793
794 return EFI_SUCCESS;
795 }
796
797 /**
798 Updates the Stack HOB passed to DXE phase.
799
800 This function traverses the whole HOB list and update the stack HOB to
801 reflect the real stack that is used by DXE core.
802
803 @param BaseAddress The lower address of stack used by DxeCore.
804 @param Length The length of stack used by DxeCore.
805
806 **/
807 VOID
808 UpdateStackHob (
809 IN EFI_PHYSICAL_ADDRESS BaseAddress,
810 IN UINT64 Length
811 )
812 {
813 EFI_PEI_HOB_POINTERS Hob;
814
815 Hob.Raw = GetHobList ();
816 while ((Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw)) != NULL) {
817 if (CompareGuid (&gEfiHobMemoryAllocStackGuid, &(Hob.MemoryAllocationStack->AllocDescriptor.Name))) {
818 //
819 // Build a new memory allocation HOB with old stack info with EfiBootServicesData type. Need to
820 // avoid this region be reclaimed by DXE core as the IDT built in SEC might be on stack, and some
821 // PEIMs may also keep key information on stack
822 //
823 BuildMemoryAllocationHob (
824 Hob.MemoryAllocationStack->AllocDescriptor.MemoryBaseAddress,
825 Hob.MemoryAllocationStack->AllocDescriptor.MemoryLength,
826 EfiBootServicesData
827 );
828 //
829 // Update the BSP Stack Hob to reflect the new stack info.
830 //
831 Hob.MemoryAllocationStack->AllocDescriptor.MemoryBaseAddress = BaseAddress;
832 Hob.MemoryAllocationStack->AllocDescriptor.MemoryLength = Length;
833 break;
834 }
835
836 Hob.Raw = GET_NEXT_HOB (Hob);
837 }
838 }