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