]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/DxeIplPeim/DxeLoad.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[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 - 2018, 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 EFI_MEMORY_TYPE_INFORMATION MemoryData[EfiMaxMemoryType + 1];
269
270 //
271 // if in S3 Resume, restore configure
272 //
273 BootMode = GetBootModeHob ();
274
275 if (BootMode == BOOT_ON_S3_RESUME) {
276 Status = PeiServicesLocatePpi (
277 &gEfiPeiS3Resume2PpiGuid,
278 0,
279 NULL,
280 (VOID **) &S3Resume
281 );
282 if (EFI_ERROR (Status)) {
283 //
284 // Report Status code that S3Resume PPI can not be found
285 //
286 REPORT_STATUS_CODE (
287 EFI_ERROR_CODE | EFI_ERROR_MAJOR,
288 (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_EC_S3_RESUME_PPI_NOT_FOUND)
289 );
290 }
291 ASSERT_EFI_ERROR (Status);
292
293 Status = S3Resume->S3RestoreConfig2 (S3Resume);
294 ASSERT_EFI_ERROR (Status);
295 } else if (BootMode == BOOT_IN_RECOVERY_MODE) {
296 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_PC_RECOVERY_BEGIN));
297 Status = PeiServicesLocatePpi (
298 &gEfiPeiRecoveryModulePpiGuid,
299 0,
300 NULL,
301 (VOID **) &PeiRecovery
302 );
303
304 if (EFI_ERROR (Status)) {
305 DEBUG ((DEBUG_ERROR, "Locate Recovery PPI Failed.(Status = %r)\n", Status));
306 //
307 // Report Status code the failure of locating Recovery PPI
308 //
309 REPORT_STATUS_CODE (
310 EFI_ERROR_CODE | EFI_ERROR_MAJOR,
311 (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_EC_RECOVERY_PPI_NOT_FOUND)
312 );
313 CpuDeadLoop ();
314 }
315
316 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_PC_CAPSULE_LOAD));
317 Status = PeiRecovery->LoadRecoveryCapsule (PeiServices, PeiRecovery);
318 if (EFI_ERROR (Status)) {
319 DEBUG ((DEBUG_ERROR, "Load Recovery Capsule Failed.(Status = %r)\n", Status));
320 //
321 // Report Status code that recovery image can not be found
322 //
323 REPORT_STATUS_CODE (
324 EFI_ERROR_CODE | EFI_ERROR_MAJOR,
325 (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_EC_NO_RECOVERY_CAPSULE)
326 );
327 CpuDeadLoop ();
328 }
329 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_PC_CAPSULE_START));
330 //
331 // Now should have a HOB with the DXE core
332 //
333 }
334
335 if (GetFirstGuidHob ((CONST EFI_GUID *)&gEfiMemoryTypeInformationGuid) == NULL) {
336 //
337 // Don't build GuidHob if GuidHob has been installed.
338 //
339 Status = PeiServicesLocatePpi (
340 &gEfiPeiReadOnlyVariable2PpiGuid,
341 0,
342 NULL,
343 (VOID **)&Variable
344 );
345 if (!EFI_ERROR (Status)) {
346 DataSize = sizeof (MemoryData);
347 Status = Variable->GetVariable (
348 Variable,
349 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,
350 &gEfiMemoryTypeInformationGuid,
351 NULL,
352 &DataSize,
353 &MemoryData
354 );
355 if (!EFI_ERROR (Status) && ValidateMemoryTypeInfoVariable(MemoryData, DataSize)) {
356 //
357 // Build the GUID'd HOB for DXE
358 //
359 BuildGuidDataHob (
360 &gEfiMemoryTypeInformationGuid,
361 MemoryData,
362 DataSize
363 );
364 }
365 }
366 }
367
368 //
369 // Look in all the FVs present in PEI and find the DXE Core FileHandle
370 //
371 FileHandle = DxeIplFindDxeCore ();
372
373 //
374 // Load the DXE Core from a Firmware Volume.
375 //
376 Instance = 0;
377 do {
378 Status = PeiServicesLocatePpi (&gEfiPeiLoadFilePpiGuid, Instance++, NULL, (VOID **) &LoadFile);
379 //
380 // These must exist an instance of EFI_PEI_LOAD_FILE_PPI to support to load DxeCore file handle successfully.
381 //
382 ASSERT_EFI_ERROR (Status);
383
384 Status = LoadFile->LoadFile (
385 LoadFile,
386 FileHandle,
387 &DxeCoreAddress,
388 &DxeCoreSize,
389 &DxeCoreEntryPoint,
390 &AuthenticationState
391 );
392 } while (EFI_ERROR (Status));
393
394 //
395 // Get the DxeCore File Info from the FileHandle for the DxeCore GUID file name.
396 //
397 Status = PeiServicesFfsGetFileInfo (FileHandle, &DxeCoreFileInfo);
398 ASSERT_EFI_ERROR (Status);
399
400 //
401 // Add HOB for the DXE Core
402 //
403 BuildModuleHob (
404 &DxeCoreFileInfo.FileName,
405 DxeCoreAddress,
406 ALIGN_VALUE (DxeCoreSize, EFI_PAGE_SIZE),
407 DxeCoreEntryPoint
408 );
409
410 //
411 // Report Status Code EFI_SW_PEI_PC_HANDOFF_TO_NEXT
412 //
413 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_PEI_CORE | EFI_SW_PEI_CORE_PC_HANDOFF_TO_NEXT));
414
415 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Loading DXE CORE at 0x%11p EntryPoint=0x%11p\n", (VOID *)(UINTN)DxeCoreAddress, FUNCTION_ENTRY_POINT (DxeCoreEntryPoint)));
416
417 //
418 // Transfer control to the DXE Core
419 // The hand off state is simply a pointer to the HOB list
420 //
421 HandOffToDxeCore (DxeCoreEntryPoint, HobList);
422 //
423 // If we get here, then the DXE Core returned. This is an error
424 // DxeCore should not return.
425 //
426 ASSERT (FALSE);
427 CpuDeadLoop ();
428
429 return EFI_OUT_OF_RESOURCES;
430 }
431
432
433 /**
434 Searches DxeCore in all firmware Volumes and loads the first
435 instance that contains DxeCore.
436
437 @return FileHandle of DxeCore to load DxeCore.
438
439 **/
440 EFI_PEI_FILE_HANDLE
441 DxeIplFindDxeCore (
442 VOID
443 )
444 {
445 EFI_STATUS Status;
446 UINTN Instance;
447 EFI_PEI_FV_HANDLE VolumeHandle;
448 EFI_PEI_FILE_HANDLE FileHandle;
449
450 Instance = 0;
451 while (TRUE) {
452 //
453 // Traverse all firmware volume instances
454 //
455 Status = PeiServicesFfsFindNextVolume (Instance, &VolumeHandle);
456 //
457 // If some error occurs here, then we cannot find any firmware
458 // volume that may contain DxeCore.
459 //
460 if (EFI_ERROR (Status)) {
461 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_CORE_EC_DXE_CORRUPT));
462 }
463 ASSERT_EFI_ERROR (Status);
464
465 //
466 // Find the DxeCore file type from the beginning in this firmware volume.
467 //
468 FileHandle = NULL;
469 Status = PeiServicesFfsFindNextFile (EFI_FV_FILETYPE_DXE_CORE, VolumeHandle, &FileHandle);
470 if (!EFI_ERROR (Status)) {
471 //
472 // Find DxeCore FileHandle in this volume, then we skip other firmware volume and
473 // return the FileHandle.
474 //
475 return FileHandle;
476 }
477 //
478 // We cannot find DxeCore in this firmware volume, then search the next volume.
479 //
480 Instance++;
481 }
482 }
483
484
485
486 /**
487 The ExtractSection() function processes the input section and
488 returns a pointer to the section contents. If the section being
489 extracted does not require processing (if the section
490 GuidedSectionHeader.Attributes has the
491 EFI_GUIDED_SECTION_PROCESSING_REQUIRED field cleared), then
492 OutputBuffer is just updated to point to the start of the
493 section's contents. Otherwise, *Buffer must be allocated
494 from PEI permanent memory.
495
496 @param This Indicates the
497 EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI instance.
498 Buffer containing the input GUIDed section to be
499 processed. OutputBuffer OutputBuffer is
500 allocated from PEI permanent memory and contains
501 the new section stream.
502 @param InputSection A pointer to the input buffer, which contains
503 the input section to be processed.
504 @param OutputBuffer A pointer to a caller-allocated buffer, whose
505 size is specified by the contents of OutputSize.
506 @param OutputSize A pointer to a caller-allocated
507 UINTN in which the size of *OutputBuffer
508 allocation is stored. If the function
509 returns anything other than EFI_SUCCESS,
510 the value of OutputSize is undefined.
511 @param AuthenticationStatus A pointer to a caller-allocated
512 UINT32 that indicates the
513 authentication status of the
514 output buffer. If the input
515 section's GuidedSectionHeader.
516 Attributes field has the
517 EFI_GUIDED_SECTION_AUTH_STATUS_VALID
518 bit as clear,
519 AuthenticationStatus must return
520 zero. These bits reflect the
521 status of the extraction
522 operation. If the function
523 returns anything other than
524 EFI_SUCCESS, the value of
525 AuthenticationStatus is
526 undefined.
527
528 @retval EFI_SUCCESS The InputSection was
529 successfully processed and the
530 section contents were returned.
531
532 @retval EFI_OUT_OF_RESOURCES The system has insufficient
533 resources to process the request.
534
535 @retval EFI_INVALID_PARAMETER The GUID in InputSection does
536 not match this instance of the
537 GUIDed Section Extraction PPI.
538
539 **/
540 EFI_STATUS
541 EFIAPI
542 CustomGuidedSectionExtract (
543 IN CONST EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI *This,
544 IN CONST VOID *InputSection,
545 OUT VOID **OutputBuffer,
546 OUT UINTN *OutputSize,
547 OUT UINT32 *AuthenticationStatus
548 )
549 {
550 EFI_STATUS Status;
551 UINT8 *ScratchBuffer;
552 UINT32 ScratchBufferSize;
553 UINT32 OutputBufferSize;
554 UINT16 SectionAttribute;
555
556 //
557 // Init local variable
558 //
559 ScratchBuffer = NULL;
560
561 //
562 // Call GetInfo to get the size and attribute of input guided section data.
563 //
564 Status = ExtractGuidedSectionGetInfo (
565 InputSection,
566 &OutputBufferSize,
567 &ScratchBufferSize,
568 &SectionAttribute
569 );
570
571 if (EFI_ERROR (Status)) {
572 DEBUG ((DEBUG_ERROR, "GetInfo from guided section Failed - %r\n", Status));
573 return Status;
574 }
575
576 if (ScratchBufferSize != 0) {
577 //
578 // Allocate scratch buffer
579 //
580 ScratchBuffer = AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize));
581 if (ScratchBuffer == NULL) {
582 return EFI_OUT_OF_RESOURCES;
583 }
584 }
585
586 if (((SectionAttribute & EFI_GUIDED_SECTION_PROCESSING_REQUIRED) != 0) && OutputBufferSize > 0) {
587 //
588 // Allocate output buffer
589 //
590 *OutputBuffer = AllocatePages (EFI_SIZE_TO_PAGES (OutputBufferSize));
591 if (*OutputBuffer == NULL) {
592 return EFI_OUT_OF_RESOURCES;
593 }
594 DEBUG ((DEBUG_INFO, "Customized Guided section Memory Size required is 0x%x and address is 0x%p\n", OutputBufferSize, *OutputBuffer));
595 }
596
597 Status = ExtractGuidedSectionDecode (
598 InputSection,
599 OutputBuffer,
600 ScratchBuffer,
601 AuthenticationStatus
602 );
603 if (EFI_ERROR (Status)) {
604 //
605 // Decode failed
606 //
607 DEBUG ((DEBUG_ERROR, "Extract guided section Failed - %r\n", Status));
608 return Status;
609 }
610
611 *OutputSize = (UINTN) OutputBufferSize;
612
613 return EFI_SUCCESS;
614 }
615
616
617
618 /**
619 Decompresses a section to the output buffer.
620
621 This function looks up the compression type field in the input section and
622 applies the appropriate compression algorithm to compress the section to a
623 callee allocated buffer.
624
625 @param This Points to this instance of the
626 EFI_PEI_DECOMPRESS_PEI PPI.
627 @param CompressionSection Points to the compressed section.
628 @param OutputBuffer Holds the returned pointer to the decompressed
629 sections.
630 @param OutputSize Holds the returned size of the decompress
631 section streams.
632
633 @retval EFI_SUCCESS The section was decompressed successfully.
634 OutputBuffer contains the resulting data and
635 OutputSize contains the resulting size.
636
637 **/
638 EFI_STATUS
639 EFIAPI
640 Decompress (
641 IN CONST EFI_PEI_DECOMPRESS_PPI *This,
642 IN CONST EFI_COMPRESSION_SECTION *CompressionSection,
643 OUT VOID **OutputBuffer,
644 OUT UINTN *OutputSize
645 )
646 {
647 EFI_STATUS Status;
648 UINT8 *DstBuffer;
649 UINT8 *ScratchBuffer;
650 UINT32 DstBufferSize;
651 UINT32 ScratchBufferSize;
652 VOID *CompressionSource;
653 UINT32 CompressionSourceSize;
654 UINT32 UncompressedLength;
655 UINT8 CompressionType;
656
657 if (CompressionSection->CommonHeader.Type != EFI_SECTION_COMPRESSION) {
658 ASSERT (FALSE);
659 return EFI_INVALID_PARAMETER;
660 }
661
662 if (IS_SECTION2 (CompressionSection)) {
663 CompressionSource = (VOID *) ((UINT8 *) CompressionSection + sizeof (EFI_COMPRESSION_SECTION2));
664 CompressionSourceSize = (UINT32) (SECTION2_SIZE (CompressionSection) - sizeof (EFI_COMPRESSION_SECTION2));
665 UncompressedLength = ((EFI_COMPRESSION_SECTION2 *) CompressionSection)->UncompressedLength;
666 CompressionType = ((EFI_COMPRESSION_SECTION2 *) CompressionSection)->CompressionType;
667 } else {
668 CompressionSource = (VOID *) ((UINT8 *) CompressionSection + sizeof (EFI_COMPRESSION_SECTION));
669 CompressionSourceSize = (UINT32) (SECTION_SIZE (CompressionSection) - sizeof (EFI_COMPRESSION_SECTION));
670 UncompressedLength = CompressionSection->UncompressedLength;
671 CompressionType = CompressionSection->CompressionType;
672 }
673
674 //
675 // This is a compression set, expand it
676 //
677 switch (CompressionType) {
678 case EFI_STANDARD_COMPRESSION:
679 if (FeaturePcdGet(PcdDxeIplSupportUefiDecompress)) {
680 //
681 // Load EFI standard compression.
682 // For compressed data, decompress them to destination buffer.
683 //
684 Status = UefiDecompressGetInfo (
685 CompressionSource,
686 CompressionSourceSize,
687 &DstBufferSize,
688 &ScratchBufferSize
689 );
690 if (EFI_ERROR (Status)) {
691 //
692 // GetInfo failed
693 //
694 DEBUG ((DEBUG_ERROR, "Decompress GetInfo Failed - %r\n", Status));
695 return EFI_NOT_FOUND;
696 }
697 //
698 // Allocate scratch buffer
699 //
700 ScratchBuffer = AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize));
701 if (ScratchBuffer == NULL) {
702 return EFI_OUT_OF_RESOURCES;
703 }
704 //
705 // Allocate destination buffer
706 //
707 DstBuffer = AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize));
708 if (DstBuffer == NULL) {
709 return EFI_OUT_OF_RESOURCES;
710 }
711 //
712 // Call decompress function
713 //
714 Status = UefiDecompress (
715 CompressionSource,
716 DstBuffer,
717 ScratchBuffer
718 );
719 if (EFI_ERROR (Status)) {
720 //
721 // Decompress failed
722 //
723 DEBUG ((DEBUG_ERROR, "Decompress Failed - %r\n", Status));
724 return EFI_NOT_FOUND;
725 }
726 break;
727 } else {
728 //
729 // PcdDxeIplSupportUefiDecompress is FALSE
730 // Don't support UEFI decompression algorithm.
731 //
732 ASSERT (FALSE);
733 return EFI_NOT_FOUND;
734 }
735
736 case EFI_NOT_COMPRESSED:
737 //
738 // Allocate destination buffer
739 //
740 DstBufferSize = UncompressedLength;
741 DstBuffer = AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize));
742 if (DstBuffer == NULL) {
743 return EFI_OUT_OF_RESOURCES;
744 }
745 //
746 // stream is not actually compressed, just encapsulated. So just copy it.
747 //
748 CopyMem (DstBuffer, CompressionSource, DstBufferSize);
749 break;
750
751 default:
752 //
753 // Don't support other unknown compression type.
754 //
755 ASSERT (FALSE);
756 return EFI_NOT_FOUND;
757 }
758
759 *OutputSize = DstBufferSize;
760 *OutputBuffer = DstBuffer;
761
762 return EFI_SUCCESS;
763 }
764
765
766 /**
767 Updates the Stack HOB passed to DXE phase.
768
769 This function traverses the whole HOB list and update the stack HOB to
770 reflect the real stack that is used by DXE core.
771
772 @param BaseAddress The lower address of stack used by DxeCore.
773 @param Length The length of stack used by DxeCore.
774
775 **/
776 VOID
777 UpdateStackHob (
778 IN EFI_PHYSICAL_ADDRESS BaseAddress,
779 IN UINT64 Length
780 )
781 {
782 EFI_PEI_HOB_POINTERS Hob;
783
784 Hob.Raw = GetHobList ();
785 while ((Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw)) != NULL) {
786 if (CompareGuid (&gEfiHobMemoryAllocStackGuid, &(Hob.MemoryAllocationStack->AllocDescriptor.Name))) {
787 //
788 // Build a new memory allocation HOB with old stack info with EfiBootServicesData type. Need to
789 // avoid this region be reclaimed by DXE core as the IDT built in SEC might be on stack, and some
790 // PEIMs may also keep key information on stack
791 //
792 BuildMemoryAllocationHob (
793 Hob.MemoryAllocationStack->AllocDescriptor.MemoryBaseAddress,
794 Hob.MemoryAllocationStack->AllocDescriptor.MemoryLength,
795 EfiBootServicesData
796 );
797 //
798 // Update the BSP Stack Hob to reflect the new stack info.
799 //
800 Hob.MemoryAllocationStack->AllocDescriptor.MemoryBaseAddress = BaseAddress;
801 Hob.MemoryAllocationStack->AllocDescriptor.MemoryLength = Length;
802 break;
803 }
804 Hob.Raw = GET_NEXT_HOB (Hob);
805 }
806 }
807