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