]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/DxeIplPeim/DxeLoad.c
Install CustomGuidedSectionExtractionPpi for Tiano or Lzma DeCompression on S3 boot...
[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
37623a5c 5Copyright (c) 2006 - 2012, 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
25bc8326 95 }\r
b98da1b1 96 \r
25bc8326
LG
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
d8c79a81 114 }\r
95276127 115 }\r
116 \r
b0d803fe 117 //\r
288f9b38 118 // Install DxeIpl and Decompress PPIs.\r
b0d803fe 119 //\r
120 Status = PeiServicesInstallPpi (mPpiList);\r
288f9b38
LG
121 ASSERT_EFI_ERROR(Status);\r
122\r
95276127 123 return Status;\r
124}\r
125\r
a95b6045
ED
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
b0d803fe 173/**\r
b98da1b1 174 Main entry point to last PEIM. \r
48557c65 175\r
176 This function finds DXE Core in the firmware volume and transfer the control to\r
177 DXE core.\r
b0d803fe 178 \r
b98da1b1 179 @param This Entry point for DXE IPL PPI.\r
b0d803fe 180 @param PeiServices General purpose services available to every PEIM.\r
b98da1b1 181 @param HobList Address to the Pei HOB list.\r
b0d803fe 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
91d92e25 185\r
b0d803fe 186**/\r
95276127 187EFI_STATUS\r
188EFIAPI\r
189DxeLoadCore (\r
1f3a753e 190 IN CONST EFI_DXE_IPL_PPI *This,\r
95276127 191 IN EFI_PEI_SERVICES **PeiServices,\r
192 IN EFI_PEI_HOB_POINTERS HobList\r
193 )\r
95276127 194{\r
195 EFI_STATUS Status;\r
b6b98e91 196 EFI_FV_FILE_INFO DxeCoreFileInfo;\r
95276127 197 EFI_PHYSICAL_ADDRESS DxeCoreAddress;\r
198 UINT64 DxeCoreSize;\r
199 EFI_PHYSICAL_ADDRESS DxeCoreEntryPoint;\r
95276127 200 EFI_BOOT_MODE BootMode;\r
b0d803fe 201 EFI_PEI_FILE_HANDLE FileHandle;\r
b74350e9 202 EFI_PEI_READ_ONLY_VARIABLE2_PPI *Variable;\r
28efc722 203 EFI_PEI_LOAD_FILE_PPI *LoadFile;\r
204 UINTN Instance;\r
205 UINT32 AuthenticationState;\r
b74350e9 206 UINTN DataSize;\r
77215d10 207 EFI_PEI_S3_RESUME2_PPI *S3Resume;\r
28efc722 208 EFI_PEI_RECOVERY_MODULE_PPI *PeiRecovery;\r
708919be 209 EFI_MEMORY_TYPE_INFORMATION MemoryData[EfiMaxMemoryType + 1];\r
95276127 210\r
211 //\r
212 // if in S3 Resume, restore configure\r
213 //\r
b98da1b1 214 BootMode = GetBootModeHob ();\r
95276127 215\r
216 if (BootMode == BOOT_ON_S3_RESUME) {\r
28efc722 217 Status = PeiServicesLocatePpi (\r
77215d10 218 &gEfiPeiS3Resume2PpiGuid,\r
28efc722 219 0,\r
220 NULL,\r
221 (VOID **) &S3Resume\r
222 );\r
37623a5c 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
28efc722 232 ASSERT_EFI_ERROR (Status);\r
233 \r
77215d10 234 Status = S3Resume->S3RestoreConfig2 (S3Resume);\r
95276127 235 ASSERT_EFI_ERROR (Status);\r
236 } else if (BootMode == BOOT_IN_RECOVERY_MODE) {\r
37623a5c 237 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_PC_RECOVERY_BEGIN));\r
28efc722 238 Status = PeiServicesLocatePpi (\r
239 &gEfiPeiRecoveryModulePpiGuid,\r
240 0,\r
241 NULL,\r
242 (VOID **) &PeiRecovery\r
243 );\r
37623a5c 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
28efc722 251 ASSERT_EFI_ERROR (Status);\r
37623a5c 252 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_PC_CAPSULE_LOAD));\r
28efc722 253 Status = PeiRecovery->LoadRecoveryCapsule (PeiServices, PeiRecovery);\r
95276127 254 if (EFI_ERROR (Status)) {\r
91d92e25 255 DEBUG ((DEBUG_ERROR, "Load Recovery Capsule Failed.(Status = %r)\n", Status));\r
37623a5c 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
95276127 263 CpuDeadLoop ();\r
264 }\r
37623a5c 265 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_PC_CAPSULE_START));\r
95276127 266 //\r
48557c65 267 // Now should have a HOB with the DXE core\r
95276127 268 //\r
269 }\r
288f9b38 270\r
b74350e9 271 Status = PeiServicesLocatePpi (\r
272 &gEfiPeiReadOnlyVariable2PpiGuid,\r
273 0,\r
274 NULL,\r
275 (VOID **)&Variable\r
276 );\r
b74350e9 277 if (!EFI_ERROR (Status)) {\r
1ad76c34 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
a95b6045 287 if (!EFI_ERROR (Status) && ValidateMemoryTypeInfoVariable(MemoryData, DataSize)) {\r
1ad76c34 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
b74350e9 297 }\r
288f9b38 298\r
95276127 299 //\r
b6b98e91 300 // Look in all the FVs present in PEI and find the DXE Core FileHandle\r
95276127 301 //\r
b6b98e91 302 FileHandle = DxeIplFindDxeCore ();\r
b0d803fe 303\r
95276127 304 //\r
28efc722 305 // Load the DXE Core from a Firmware Volume.\r
95276127 306 //\r
28efc722 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
95276127 324\r
b6b98e91 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
95276127 331 //\r
332 // Add HOB for the DXE Core\r
333 //\r
334 BuildModuleHob (\r
b6b98e91 335 &DxeCoreFileInfo.FileName,\r
95276127 336 DxeCoreAddress,\r
48557c65 337 ALIGN_VALUE (DxeCoreSize, EFI_PAGE_SIZE),\r
95276127 338 DxeCoreEntryPoint\r
339 );\r
340\r
341 //\r
342 // Report Status Code EFI_SW_PEI_PC_HANDOFF_TO_NEXT\r
343 //\r
f9876ecf 344 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_PEI_CORE | EFI_SW_PEI_CORE_PC_HANDOFF_TO_NEXT));\r
95276127 345\r
4e2dd553 346 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Loading DXE CORE at 0x%11p EntryPoint=0x%11p\n", (VOID *)(UINTN)DxeCoreAddress, FUNCTION_ENTRY_POINT (DxeCoreEntryPoint)));\r
e98cd821 347\r
95276127 348 //\r
349 // Transfer control to the DXE Core\r
48557c65 350 // The hand off state is simply a pointer to the HOB list\r
95276127 351 //\r
9b937a73 352 HandOffToDxeCore (DxeCoreEntryPoint, HobList);\r
95276127 353 //\r
354 // If we get here, then the DXE Core returned. This is an error\r
48557c65 355 // DxeCore should not return.\r
95276127 356 //\r
357 ASSERT (FALSE);\r
358 CpuDeadLoop ();\r
359\r
360 return EFI_OUT_OF_RESOURCES;\r
361}\r
362\r
91d92e25 363\r
b0d803fe 364/**\r
9b937a73 365 Searches DxeCore in all firmware Volumes and loads the first\r
366 instance that contains DxeCore.\r
b0d803fe 367\r
9b937a73 368 @return FileHandle of DxeCore to load DxeCore.\r
369 \r
91d92e25 370**/\r
9b937a73 371EFI_PEI_FILE_HANDLE\r
288f9b38 372DxeIplFindDxeCore (\r
b6b98e91 373 VOID\r
b0d803fe 374 )\r
95276127 375{\r
9b937a73 376 EFI_STATUS Status;\r
377 UINTN Instance;\r
378 EFI_PEI_FV_HANDLE VolumeHandle;\r
379 EFI_PEI_FILE_HANDLE FileHandle;\r
288f9b38
LG
380 \r
381 Instance = 0;\r
9b937a73 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
3d0a2385 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
9b937a73 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
288f9b38 401 if (!EFI_ERROR (Status)) {\r
9b937a73 402 //\r
b6b98e91 403 // Find DxeCore FileHandle in this volume, then we skip other firmware volume and\r
404 // return the FileHandle.\r
9b937a73 405 //\r
b6b98e91 406 return FileHandle;\r
95276127 407 }\r
9b937a73 408 //\r
409 // We cannot find DxeCore in this firmware volume, then search the next volume.\r
410 //\r
411 Instance++;\r
412 }\r
95276127 413}\r
414\r
91d92e25 415\r
91d92e25 416\r
d8c79a81
LG
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
91d92e25 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
d8c79a81
LG
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
d8c79a81
LG
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
708919be 466 @retval EFI_INVALID_PARAMETER The GUID in InputSection does\r
d8c79a81
LG
467 not match this instance of the\r
468 GUIDed Section Extraction PPI.\r
91d92e25 469\r
d8c79a81
LG
470**/\r
471EFI_STATUS\r
6d3ea23f 472EFIAPI\r
18fd8d65 473CustomGuidedSectionExtract (\r
d8c79a81
LG
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
18fd8d65
LG
483 UINT32 ScratchBufferSize;\r
484 UINT32 OutputBufferSize;\r
485 UINT16 SectionAttribute;\r
d8c79a81
LG
486 \r
487 //\r
18fd8d65 488 // Init local variable\r
d8c79a81 489 //\r
18fd8d65
LG
490 ScratchBuffer = NULL;\r
491\r
d8c79a81 492 //\r
18fd8d65 493 // Call GetInfo to get the size and attribute of input guided section data.\r
d8c79a81 494 //\r
18fd8d65 495 Status = ExtractGuidedSectionGetInfo (\r
b6b98e91 496 InputSection,\r
497 &OutputBufferSize,\r
498 &ScratchBufferSize,\r
499 &SectionAttribute\r
500 );\r
18fd8d65 501 \r
d8c79a81 502 if (EFI_ERROR (Status)) {\r
91d92e25 503 DEBUG ((DEBUG_ERROR, "GetInfo from guided section Failed - %r\n", Status));\r
18fd8d65
LG
504 return Status;\r
505 }\r
506 \r
507 if (ScratchBufferSize != 0) {\r
95276127 508 //\r
18fd8d65 509 // Allocate scratch buffer\r
95276127 510 //\r
18fd8d65
LG
511 ScratchBuffer = AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize));\r
512 if (ScratchBuffer == NULL) {\r
513 return EFI_OUT_OF_RESOURCES;\r
514 }\r
d8c79a81 515 }\r
95276127 516\r
708919be 517 if (((SectionAttribute & EFI_GUIDED_SECTION_PROCESSING_REQUIRED) != 0) && OutputBufferSize > 0) { \r
18fd8d65
LG
518 //\r
519 // Allocate output buffer\r
520 //\r
288f9b38 521 *OutputBuffer = AllocatePages (EFI_SIZE_TO_PAGES (OutputBufferSize) + 1);\r
18fd8d65
LG
522 if (*OutputBuffer == NULL) {\r
523 return EFI_OUT_OF_RESOURCES;\r
524 }\r
48557c65 525 DEBUG ((DEBUG_INFO, "Customized Guided section Memory Size required is 0x%x and address is 0x%p\n", OutputBufferSize, *OutputBuffer));\r
288f9b38
LG
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
95276127 531 }\r
18fd8d65
LG
532 \r
533 Status = ExtractGuidedSectionDecode (\r
534 InputSection, \r
535 OutputBuffer,\r
536 ScratchBuffer,\r
537 AuthenticationStatus\r
48557c65 538 );\r
d8c79a81
LG
539 if (EFI_ERROR (Status)) {\r
540 //\r
18fd8d65 541 // Decode failed\r
d8c79a81 542 //\r
91d92e25 543 DEBUG ((DEBUG_ERROR, "Extract guided section Failed - %r\n", Status));\r
d8c79a81
LG
544 return Status;\r
545 }\r
546 \r
18fd8d65
LG
547 *OutputSize = (UINTN) OutputBufferSize;\r
548 \r
95276127 549 return EFI_SUCCESS;\r
550}\r
b0d803fe 551\r
91d92e25 552\r
553\r
554/**\r
555 Decompresses a section to the output buffer.\r
556\r
48557c65 557 This function looks up the compression type field in the input section and\r
91d92e25 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
b0d803fe 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
635021c5 586 UINT32 DstBufferSize;\r
b0d803fe 587 UINT32 ScratchBufferSize;\r
890e5417
SZ
588 VOID *CompressionSource;\r
589 UINT32 CompressionSourceSize;\r
590 UINT32 UncompressedLength;\r
591 UINT8 CompressionType;\r
b0d803fe 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
890e5417
SZ
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
b0d803fe 609 \r
610 //\r
611 // This is a compression set, expand it\r
612 //\r
890e5417 613 switch (CompressionType) {\r
b0d803fe 614 case EFI_STANDARD_COMPRESSION:\r
873b7997 615 if (FeaturePcdGet(PcdDxeIplSupportUefiDecompress)) {\r
b0d803fe 616 //\r
873b7997 617 // Load EFI standard compression.\r
618 // For compressed data, decompress them to destination buffer.\r
b0d803fe 619 //\r
873b7997 620 Status = UefiDecompressGetInfo (\r
890e5417
SZ
621 CompressionSource,\r
622 CompressionSourceSize,\r
635021c5 623 &DstBufferSize,\r
873b7997 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
b0d803fe 635 //\r
873b7997 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
b0d803fe 647 //\r
873b7997 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
890e5417 656 CompressionSource,\r
873b7997 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
12b3e61e
LG
669 //\r
670 // PcdDxeIplSupportUefiDecompress is FALSE\r
671 // Don't support UEFI decompression algorithm.\r
672 //\r
673 ASSERT (FALSE);\r
b0d803fe 674 return EFI_NOT_FOUND;\r
675 }\r
b0d803fe 676\r
b0d803fe 677 case EFI_NOT_COMPRESSED:\r
678 //\r
679 // Allocate destination buffer\r
680 //\r
890e5417 681 DstBufferSize = UncompressedLength;\r
288f9b38 682 DstBuffer = AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize) + 1);\r
b0d803fe 683 if (DstBuffer == NULL) {\r
684 return EFI_OUT_OF_RESOURCES;\r
685 }\r
686 //\r
288f9b38
LG
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
b0d803fe 692 // stream is not actually compressed, just encapsulated. So just copy it.\r
693 //\r
890e5417 694 CopyMem (DstBuffer, CompressionSource, DstBufferSize);\r
b0d803fe 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
91d92e25 711\r
91d92e25 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
30c8f861 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
9a43bc39 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
30c8f861 737 //\r
738 BuildMemoryAllocationHob (\r
739 Hob.MemoryAllocationStack->AllocDescriptor.MemoryBaseAddress,\r
740 Hob.MemoryAllocationStack->AllocDescriptor.MemoryLength,\r
9a43bc39 741 EfiBootServicesData\r
30c8f861 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