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