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