]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/DxeIplPeim/DxeLoad.c
Remove assemble file for different arch,
[mirror_edk2.git] / MdeModulePkg / Core / DxeIplPeim / DxeLoad.c
CommitLineData
b0d803fe 1/**@file\r
2 Last PEIM.\r
3 Responsibility of this module is to load the DXE Core from a Firmware Volume.\r
95276127 4\r
b0d803fe 5Copyright (c) 2006 - 2007 Intel Corporation\r
95276127 6All rights reserved. This 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
b0d803fe 14**/\r
95276127 15\r
95276127 16#include "DxeIpl.h"\r
d8c79a81 17#include <Ppi/GuidedSectionExtraction.h>\r
b0d803fe 18#include <FrameworkPei.h>\r
95276127 19\r
d8c79a81 20EFI_STATUS\r
18fd8d65 21CustomGuidedSectionExtract (\r
d8c79a81
LG
22 IN CONST EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI *This,\r
23 IN CONST VOID *InputSection,\r
24 OUT VOID **OutputBuffer,\r
25 OUT UINTN *OutputSize,\r
26 OUT UINT32 *AuthenticationStatus\r
27);\r
28\r
b0d803fe 29STATIC\r
30EFI_STATUS\r
31EFIAPI \r
32Decompress (\r
33 IN CONST EFI_PEI_DECOMPRESS_PPI *This,\r
34 IN CONST EFI_COMPRESSION_SECTION *InputSection,\r
35 OUT VOID **OutputBuffer,\r
36 OUT UINTN *OutputSize\r
37);\r
38\r
39\r
95276127 40BOOLEAN gInMemory = FALSE;\r
41\r
42//\r
43// Module Globals used in the DXE to PEI handoff\r
44// These must be module globals, so the stack can be switched\r
45//\r
46static EFI_DXE_IPL_PPI mDxeIplPpi = {\r
47 DxeLoadCore\r
48};\r
49\r
18fd8d65
LG
50STATIC EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI mCustomGuidedSectionExtractionPpi = {\r
51 CustomGuidedSectionExtract\r
95276127 52};\r
53\r
b0d803fe 54STATIC EFI_PEI_DECOMPRESS_PPI mDecompressPpi = {\r
55 Decompress\r
d8c79a81
LG
56};\r
57\r
95276127 58static EFI_PEI_PPI_DESCRIPTOR mPpiList[] = {\r
59 {\r
b0d803fe 60 EFI_PEI_PPI_DESCRIPTOR_PPI,\r
61 &gEfiDxeIplPpiGuid,\r
62 &mDxeIplPpi\r
95276127 63 },\r
64 {\r
b0d803fe 65 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
66 &gEfiPeiDecompressPpiGuid,\r
67 &mDecompressPpi\r
95276127 68 }\r
69};\r
70\r
71static EFI_PEI_PPI_DESCRIPTOR mPpiSignal = {\r
72 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
73 &gEfiEndOfPeiSignalPpiGuid,\r
74 NULL\r
75};\r
76\r
b0d803fe 77/**\r
78 Initializes the Dxe Ipl PPI\r
79\r
80 @param FfsHandle The handle of FFS file.\r
81 @param PeiServices General purpose services available to\r
82 every PEIM.\r
83 @return EFI_SUCESS \r
84*/ \r
95276127 85EFI_STATUS\r
86EFIAPI\r
87PeimInitializeDxeIpl (\r
b0d803fe 88 IN EFI_PEI_FILE_HANDLE FfsHandle,\r
95276127 89 IN EFI_PEI_SERVICES **PeiServices\r
90 )\r
95276127 91{\r
92 EFI_STATUS Status;\r
95276127 93 EFI_BOOT_MODE BootMode;\r
18fd8d65
LG
94 EFI_GUID *ExtractHandlerGuidTable;\r
95 UINTN ExtractHandlerNumber;\r
d8c79a81
LG
96 EFI_PEI_PPI_DESCRIPTOR *GuidPpi;\r
97 \r
95276127 98 Status = PeiServicesGetBootMode (&BootMode);\r
99 ASSERT_EFI_ERROR (Status);\r
100\r
b0d803fe 101 if (BootMode != BOOT_ON_S3_RESUME) {\r
102 Status = PeiServicesRegisterForShadow (FfsHandle);\r
103 if (Status == EFI_SUCCESS) {\r
b0d803fe 104 //\r
105 // EFI_SUCESS means the first time call register for shadow \r
106 // \r
107 return Status;\r
108 } else if (Status == EFI_ALREADY_STARTED) {\r
5516d05a 109 \r
110 gInMemory = TRUE;\r
111 \r
b0d803fe 112 //\r
18fd8d65 113 // Get custom extract guided section method guid list \r
b0d803fe 114 //\r
18fd8d65 115 ExtractHandlerNumber = ExtractGuidedSectionGetGuidList (&ExtractHandlerGuidTable);\r
b0d803fe 116 \r
117 //\r
18fd8d65 118 // Install custom extraction guid ppi\r
b0d803fe 119 //\r
18fd8d65 120 if (ExtractHandlerNumber > 0) {\r
b0d803fe 121 GuidPpi = NULL;\r
18fd8d65 122 GuidPpi = (EFI_PEI_PPI_DESCRIPTOR *) AllocatePool (ExtractHandlerNumber * sizeof (EFI_PEI_PPI_DESCRIPTOR));\r
b0d803fe 123 ASSERT (GuidPpi != NULL);\r
18fd8d65 124 while (ExtractHandlerNumber-- > 0) {\r
b0d803fe 125 GuidPpi->Flags = EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST;\r
18fd8d65
LG
126 GuidPpi->Ppi = &mCustomGuidedSectionExtractionPpi;\r
127 GuidPpi->Guid = &(ExtractHandlerGuidTable [ExtractHandlerNumber]);\r
b0d803fe 128 Status = PeiServicesInstallPpi (GuidPpi++);\r
129 ASSERT_EFI_ERROR(Status);\r
130 }\r
131 }\r
132 } else {\r
133 ASSERT_EFI_ERROR (FALSE);\r
d8c79a81 134 }\r
95276127 135 }\r
136 \r
b0d803fe 137 //\r
138 // Install FvFileLoader and DxeIpl PPIs.\r
139 //\r
140 Status = PeiServicesInstallPpi (mPpiList);\r
141 ASSERT_EFI_ERROR(Status); \r
142 \r
95276127 143 return Status;\r
144}\r
145\r
b0d803fe 146/**\r
147 Main entry point to last PEIM \r
148 \r
149 @param This Entry point for DXE IPL PPI\r
150 @param PeiServices General purpose services available to every PEIM.\r
151 @param HobList Address to the Pei HOB list\r
152 \r
153 @return EFI_SUCCESS DXE core was successfully loaded. \r
154 @return EFI_OUT_OF_RESOURCES There are not enough resources to load DXE core.\r
155**/\r
95276127 156EFI_STATUS\r
157EFIAPI\r
158DxeLoadCore (\r
159 IN EFI_DXE_IPL_PPI *This,\r
160 IN EFI_PEI_SERVICES **PeiServices,\r
161 IN EFI_PEI_HOB_POINTERS HobList\r
162 )\r
95276127 163{\r
164 EFI_STATUS Status;\r
165 EFI_GUID DxeCoreFileName;\r
95276127 166 EFI_PHYSICAL_ADDRESS DxeCoreAddress;\r
167 UINT64 DxeCoreSize;\r
168 EFI_PHYSICAL_ADDRESS DxeCoreEntryPoint;\r
95276127 169 EFI_BOOT_MODE BootMode;\r
b0d803fe 170 EFI_PEI_FV_HANDLE VolumeHandle;\r
171 EFI_PEI_FILE_HANDLE FileHandle;\r
172 UINTN Instance;\r
95276127 173\r
174 //\r
175 // if in S3 Resume, restore configure\r
176 //\r
177 Status = PeiServicesGetBootMode (&BootMode);\r
178 ASSERT_EFI_ERROR(Status);\r
179\r
180 if (BootMode == BOOT_ON_S3_RESUME) {\r
e8da1266 181 Status = AcpiS3ResumeOs();\r
95276127 182 ASSERT_EFI_ERROR (Status);\r
183 } else if (BootMode == BOOT_IN_RECOVERY_MODE) {\r
e8da1266 184 Status = PeiRecoverFirmware ();\r
95276127 185 if (EFI_ERROR (Status)) {\r
186 DEBUG ((EFI_D_ERROR, "Load Recovery Capsule Failed.(Status = %r)\n", Status));\r
187 CpuDeadLoop ();\r
188 }\r
189\r
190 //\r
191 // Now should have a HOB with the DXE core w/ the old HOB destroyed\r
192 //\r
193 }\r
b0d803fe 194 \r
95276127 195 //\r
b0d803fe 196 // If any FV contains an encapsulated FV extract that FV\r
95276127 197 //\r
b0d803fe 198 DxeIplAddEncapsulatedFirmwareVolumes ();\r
199 \r
95276127 200 //\r
b0d803fe 201 // Look in all the FVs present in PEI and find the DXE Core\r
95276127 202 //\r
b0d803fe 203 Instance = 0;\r
204 Status = DxeIplFindFirmwareVolumeInstance (&Instance, EFI_FV_FILETYPE_DXE_CORE, &VolumeHandle, &FileHandle);\r
95276127 205 ASSERT_EFI_ERROR (Status);\r
206\r
b0d803fe 207 CopyMem(&DxeCoreFileName, &(((EFI_FFS_FILE_HEADER*)FileHandle)->Name), sizeof (EFI_GUID));\r
208\r
95276127 209 //\r
210 // Load the DXE Core from a Firmware Volume\r
211 //\r
212 Status = PeiLoadFile (\r
b0d803fe 213 FileHandle,\r
214 &DxeCoreAddress,\r
215 &DxeCoreSize,\r
216 &DxeCoreEntryPoint\r
217 );\r
218\r
95276127 219 ASSERT_EFI_ERROR (Status);\r
220\r
221 //\r
222 // Add HOB for the DXE Core\r
223 //\r
224 BuildModuleHob (\r
225 &DxeCoreFileName,\r
226 DxeCoreAddress,\r
227 DxeCoreSize,\r
228 DxeCoreEntryPoint\r
229 );\r
230\r
231 //\r
232 // Report Status Code EFI_SW_PEI_PC_HANDOFF_TO_NEXT\r
233 //\r
234 REPORT_STATUS_CODE (\r
235 EFI_PROGRESS_CODE,\r
236 EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_CORE_PC_HANDOFF_TO_NEXT\r
237 );\r
238\r
95276127 239 //\r
240 // Transfer control to the DXE Core\r
241 // The handoff state is simply a pointer to the HOB list\r
242 //\r
95276127 243 DEBUG ((EFI_D_INFO, "DXE Core Entry Point 0x%08x\n", (UINTN) DxeCoreEntryPoint));\r
244 HandOffToDxeCore (DxeCoreEntryPoint, HobList, &mPpiSignal);\r
245 //\r
246 // If we get here, then the DXE Core returned. This is an error\r
247 // Dxe Core should not return.\r
248 //\r
249 ASSERT (FALSE);\r
250 CpuDeadLoop ();\r
251\r
252 return EFI_OUT_OF_RESOURCES;\r
253}\r
254\r
b0d803fe 255\r
256STATIC\r
95276127 257EFI_STATUS\r
b0d803fe 258GetFvAlignment (\r
259 IN EFI_FIRMWARE_VOLUME_HEADER *FvHeader,\r
260 OUT UINT32 *FvAlignment\r
95276127 261 )\r
b0d803fe 262{\r
263 //\r
264 // Because FvLength in FvHeader is UINT64 type, \r
265 // so FvHeader must meed at least 8 bytes alignment.\r
266 // Get the appropriate alignment requirement.\r
267 // \r
268 if ((FvHeader->Attributes & EFI_FVB2_ALIGNMENT) < EFI_FVB2_ALIGNMENT_8) {\r
269 return EFI_UNSUPPORTED;\r
270 }\r
271 \r
272 *FvAlignment = 1 << ((FvHeader->Attributes & EFI_FVB2_ALIGNMENT) >> 16);\r
273 return EFI_SUCCESS;\r
274}\r
95276127 275\r
b0d803fe 276/**\r
277 Search EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE image and expand \r
278 as memory FV \r
279 \r
280 @return EFI_OUT_OF_RESOURCES There are no memory space to exstract FV\r
281 @return EFI_SUCESS Sucess to find the FV \r
282**/\r
283EFI_STATUS\r
284DxeIplAddEncapsulatedFirmwareVolumes (\r
285 VOID\r
286 )\r
287{\r
288 EFI_STATUS Status;\r
289 EFI_STATUS VolumeStatus;\r
290 UINTN Index;\r
291 EFI_FV_INFO VolumeInfo; \r
292 EFI_PEI_FV_HANDLE VolumeHandle;\r
293 EFI_PEI_FILE_HANDLE FileHandle;\r
294 UINT32 SectionLength;\r
295 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;\r
296 EFI_FIRMWARE_VOLUME_IMAGE_SECTION *SectionHeader;\r
297 VOID *DstBuffer;\r
298 UINT32 FvAlignment;\r
b0d803fe 299\r
300 Status = EFI_NOT_FOUND;\r
301 Index = 0;\r
95276127 302\r
b0d803fe 303 do {\r
304 VolumeStatus = DxeIplFindFirmwareVolumeInstance (\r
305 &Index, \r
306 EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE, \r
307 &VolumeHandle, \r
308 &FileHandle\r
309 );\r
310 \r
311 if (!EFI_ERROR (VolumeStatus)) {\r
312 Status = PeiServicesFfsFindSectionData (\r
313 EFI_SECTION_FIRMWARE_VOLUME_IMAGE, \r
314 (EFI_FFS_FILE_HEADER *)FileHandle, \r
315 (VOID **)&FvHeader\r
316 );\r
317 \r
318 if (!EFI_ERROR (Status)) {\r
319 if (FvHeader->Signature == EFI_FVH_SIGNATURE) {\r
320 //\r
321 // Because FvLength in FvHeader is UINT64 type, \r
322 // so FvHeader must meed at least 8 bytes alignment.\r
323 // If current FvImage base address doesn't meet its alignment,\r
324 // we need to reload this FvImage to another correct memory address.\r
325 //\r
326 Status = GetFvAlignment(FvHeader, &FvAlignment); \r
327 if (EFI_ERROR(Status)) {\r
328 return Status;\r
329 }\r
330 if (((UINTN) FvHeader % FvAlignment) != 0) {\r
331 SectionHeader = (EFI_FIRMWARE_VOLUME_IMAGE_SECTION*)((UINTN)FvHeader - sizeof(EFI_FIRMWARE_VOLUME_IMAGE_SECTION));\r
332 SectionLength = *(UINT32 *)SectionHeader->Size & 0x00FFFFFF;\r
333 \r
334 DstBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES ((UINTN) SectionLength - sizeof (EFI_COMMON_SECTION_HEADER)), FvAlignment);\r
335 if (DstBuffer == NULL) {\r
336 return EFI_OUT_OF_RESOURCES;\r
337 }\r
338 CopyMem (DstBuffer, FvHeader, (UINTN) SectionLength - sizeof (EFI_COMMON_SECTION_HEADER));\r
339 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) DstBuffer; \r
340 }\r
95276127 341\r
b0d803fe 342 //\r
343 // This new Firmware Volume comes from a firmware file within a firmware volume.\r
344 // Record the original Firmware Volume Name.\r
345 //\r
346 PeiServicesFfsGetVolumeInfo (&VolumeHandle, &VolumeInfo);\r
95276127 347\r
64253026 348 PeiPiLibBuildPiFvInfoPpi (\r
67a58d0f 349 (EFI_PHYSICAL_ADDRESS) (UINTN) FvHeader,\r
64253026 350 FvHeader->FvLength,\r
b0d803fe 351 &(VolumeInfo.FvName),\r
64253026 352 &(((EFI_FFS_FILE_HEADER*)FileHandle)->Name)\r
b0d803fe 353 );\r
64253026 354 \r
b0d803fe 355 ASSERT_EFI_ERROR (Status);\r
95276127 356\r
b0d803fe 357 //\r
358 // Makes the encapsulated volume show up in DXE phase to skip processing of\r
359 // encapsulated file again.\r
360 //\r
361 BuildFv2Hob (\r
362 (EFI_PHYSICAL_ADDRESS)(UINTN)FvHeader,\r
363 FvHeader->FvLength, \r
364 &VolumeInfo.FvName,\r
365 &(((EFI_FFS_FILE_HEADER *)FileHandle)->Name)\r
366 );\r
367 return Status;\r
368 }\r
369 }\r
370 }\r
371 } while (!EFI_ERROR (VolumeStatus));\r
372 \r
373 return Status;\r
374}\r
95276127 375\r
b0d803fe 376/**\r
377 Find the First Volume that contains the first FileType.\r
378\r
379 @param Instance The Fv instance.\r
380 @param SeachType The type of file to search.\r
381 @param VolumeHandle Pointer to Fv which contains the file to search. \r
382 @param FileHandle Pointer to FFS file to search.\r
383 \r
384 @return EFI_SUCESS Success to find the FFS in specificed FV\r
385 @return others Fail to find the FFS in specificed FV\r
386 */\r
387EFI_STATUS\r
388DxeIplFindFirmwareVolumeInstance (\r
389 IN OUT UINTN *Instance,\r
390 IN EFI_FV_FILETYPE SeachType,\r
391 OUT EFI_PEI_FV_HANDLE *VolumeHandle,\r
392 OUT EFI_PEI_FILE_HANDLE *FileHandle\r
393 )\r
95276127 394{\r
b0d803fe 395 EFI_STATUS Status;\r
396 EFI_STATUS VolumeStatus;\r
95276127 397\r
b0d803fe 398 do {\r
399 VolumeStatus = PeiServicesFfsFindNextVolume (*Instance, VolumeHandle);\r
400 if (!EFI_ERROR (VolumeStatus)) {\r
401 *FileHandle = NULL;\r
402 Status = PeiServicesFfsFindNextFile (SeachType, *VolumeHandle, FileHandle);\r
403 if (!EFI_ERROR (Status)) {\r
404 return Status;\r
95276127 405 }\r
406 }\r
b0d803fe 407 *Instance += 1;\r
408 } while (!EFI_ERROR (VolumeStatus));\r
409\r
410 return VolumeStatus;\r
95276127 411}\r
412\r
b0d803fe 413/**\r
414 Loads and relocates a PE/COFF image into memory.\r
415\r
416 @param FileHandle The image file handle\r
417 @param ImageAddress The base address of the relocated PE/COFF image\r
418 @param ImageSize The size of the relocated PE/COFF image\r
419 @param EntryPoint The entry point of the relocated PE/COFF image\r
420 \r
421 @return EFI_SUCCESS The file was loaded and relocated\r
422 @return EFI_OUT_OF_RESOURCES There was not enough memory to load and relocate the PE/COFF file\r
423**/\r
95276127 424EFI_STATUS\r
425PeiLoadFile (\r
b0d803fe 426 IN EFI_PEI_FILE_HANDLE FileHandle,\r
95276127 427 OUT EFI_PHYSICAL_ADDRESS *ImageAddress,\r
428 OUT UINT64 *ImageSize,\r
429 OUT EFI_PHYSICAL_ADDRESS *EntryPoint\r
430 )\r
b0d803fe 431{\r
95276127 432\r
b0d803fe 433 EFI_STATUS Status;\r
434 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
435 VOID *Pe32Data;\r
b0d803fe 436 //\r
437 // First try to find the required section in this ffs file.\r
438 //\r
439 Status = PeiServicesFfsFindSectionData (\r
440 EFI_SECTION_PE32,\r
441 FileHandle,\r
442 &Pe32Data\r
443 );\r
95276127 444\r
b0d803fe 445 if (EFI_ERROR (Status)) {\r
446 Status = PeiServicesFfsFindSectionData (\r
447 EFI_SECTION_TE,\r
448 FileHandle,\r
449 &Pe32Data\r
450 );\r
451 }\r
452 \r
453 if (EFI_ERROR (Status)) {\r
454 //\r
455 // NO image types we support so exit.\r
456 //\r
457 return Status;\r
458 }\r
95276127 459\r
460 ZeroMem (&ImageContext, sizeof (ImageContext));\r
461 ImageContext.Handle = Pe32Data;\r
462 Status = GetImageReadFunction (&ImageContext);\r
463\r
464 ASSERT_EFI_ERROR (Status);\r
465\r
3d7b0992 466 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
95276127 467 if (EFI_ERROR (Status)) {\r
468 return Status;\r
469 }\r
470 //\r
471 // Allocate Memory for the image\r
472 //\r
473 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32) ImageContext.ImageSize));\r
474 ASSERT (ImageContext.ImageAddress != 0);\r
475\r
476 //\r
477 // Load the image to our new buffer\r
478 //\r
3d7b0992 479 Status = PeCoffLoaderLoadImage (&ImageContext);\r
95276127 480 if (EFI_ERROR (Status)) {\r
481 return Status;\r
482 }\r
483 //\r
484 // Relocate the image in our new buffer\r
485 //\r
3d7b0992 486 Status = PeCoffLoaderRelocateImage (&ImageContext);\r
95276127 487 if (EFI_ERROR (Status)) {\r
488 return Status;\r
489 }\r
490\r
491 //\r
492 // Flush the instruction cache so the image data is written before we execute it\r
493 //\r
494 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r
495\r
496 *ImageAddress = ImageContext.ImageAddress;\r
497 *ImageSize = ImageContext.ImageSize;\r
498 *EntryPoint = ImageContext.EntryPoint;\r
499\r
500 return EFI_SUCCESS;\r
501}\r
502\r
d8c79a81
LG
503/**\r
504 The ExtractSection() function processes the input section and\r
505 returns a pointer to the section contents. If the section being\r
506 extracted does not require processing (if the section\r
507 GuidedSectionHeader.Attributes has the\r
508 EFI_GUIDED_SECTION_PROCESSING_REQUIRED field cleared), then\r
509 OutputBuffer is just updated to point to the start of the\r
510 section's contents. Otherwise, *Buffer must be allocated\r
511 from PEI permanent memory.\r
512\r
513 @param This Indicates the\r
514 EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI instance.\r
515 Buffer containing the input GUIDed section to be\r
516 processed. OutputBuffer OutputBuffer is\r
517 allocated from PEI permanent memory and contains\r
518 the new section stream.\r
519 \r
520 @param OutputSize A pointer to a caller-allocated\r
521 UINTN in which the size of *OutputBuffer\r
522 allocation is stored. If the function\r
523 returns anything other than EFI_SUCCESS,\r
524 the value of OutputSize is undefined.\r
525 \r
526 @param AuthenticationStatus A pointer to a caller-allocated\r
527 UINT32 that indicates the\r
528 authentication status of the\r
529 output buffer. If the input\r
530 section's GuidedSectionHeader.\r
531 Attributes field has the\r
532 EFI_GUIDED_SECTION_AUTH_STATUS_VALID \r
533 bit as clear,\r
534 AuthenticationStatus must return\r
535 zero. These bits reflect the\r
536 status of the extraction\r
537 operation. If the function\r
538 returns anything other than\r
539 EFI_SUCCESS, the value of\r
540 AuthenticationStatus is\r
541 undefined.\r
542 \r
543 @retval EFI_SUCCESS The InputSection was\r
544 successfully processed and the\r
545 section contents were returned.\r
546 \r
547 @retval EFI_OUT_OF_RESOURCES The system has insufficient\r
548 resources to process the request.\r
549 \r
550 @reteval EFI_INVALID_PARAMETER The GUID in InputSection does\r
551 not match this instance of the\r
552 GUIDed Section Extraction PPI.\r
553**/\r
554EFI_STATUS\r
18fd8d65 555CustomGuidedSectionExtract (\r
d8c79a81
LG
556 IN CONST EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI *This,\r
557 IN CONST VOID *InputSection,\r
558 OUT VOID **OutputBuffer,\r
559 OUT UINTN *OutputSize,\r
560 OUT UINT32 *AuthenticationStatus\r
561)\r
562{\r
563 EFI_STATUS Status;\r
564 UINT8 *ScratchBuffer;\r
18fd8d65
LG
565 UINT32 ScratchBufferSize;\r
566 UINT32 OutputBufferSize;\r
567 UINT16 SectionAttribute;\r
d8c79a81
LG
568 \r
569 //\r
18fd8d65 570 // Init local variable\r
d8c79a81 571 //\r
18fd8d65
LG
572 ScratchBuffer = NULL;\r
573\r
d8c79a81 574 //\r
18fd8d65 575 // Call GetInfo to get the size and attribute of input guided section data.\r
d8c79a81 576 //\r
18fd8d65
LG
577 Status = ExtractGuidedSectionGetInfo (\r
578 InputSection,\r
579 &OutputBufferSize,\r
580 &ScratchBufferSize,\r
581 &SectionAttribute\r
582 );\r
583 \r
d8c79a81 584 if (EFI_ERROR (Status)) {\r
18fd8d65
LG
585 DEBUG ((EFI_D_ERROR, "GetInfo from guided section Failed - %r\n", Status));\r
586 return Status;\r
587 }\r
588 \r
589 if (ScratchBufferSize != 0) {\r
95276127 590 //\r
18fd8d65 591 // Allocate scratch buffer\r
95276127 592 //\r
18fd8d65
LG
593 ScratchBuffer = AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize));\r
594 if (ScratchBuffer == NULL) {\r
595 return EFI_OUT_OF_RESOURCES;\r
596 }\r
d8c79a81 597 }\r
95276127 598\r
18fd8d65
LG
599 if ((SectionAttribute & EFI_GUIDED_SECTION_PROCESSING_REQUIRED) && OutputBufferSize > 0) { \r
600 //\r
601 // Allocate output buffer\r
602 //\r
603 *OutputBuffer = AllocatePages (EFI_SIZE_TO_PAGES (OutputBufferSize));\r
604 if (*OutputBuffer == NULL) {\r
605 return EFI_OUT_OF_RESOURCES;\r
606 }\r
95276127 607 }\r
18fd8d65
LG
608 \r
609 Status = ExtractGuidedSectionDecode (\r
610 InputSection, \r
611 OutputBuffer,\r
612 ScratchBuffer,\r
613 AuthenticationStatus\r
614 );\r
95276127 615\r
d8c79a81
LG
616 if (EFI_ERROR (Status)) {\r
617 //\r
18fd8d65 618 // Decode failed\r
d8c79a81
LG
619 //\r
620 DEBUG ((EFI_D_ERROR, "Extract guided section Failed - %r\n", Status));\r
621 return Status;\r
622 }\r
623 \r
18fd8d65
LG
624 *OutputSize = (UINTN) OutputBufferSize;\r
625 \r
95276127 626 return EFI_SUCCESS;\r
627}\r
b0d803fe 628\r
629STATIC\r
630EFI_STATUS\r
631EFIAPI \r
632Decompress (\r
633 IN CONST EFI_PEI_DECOMPRESS_PPI *This,\r
634 IN CONST EFI_COMPRESSION_SECTION *CompressionSection,\r
635 OUT VOID **OutputBuffer,\r
636 OUT UINTN *OutputSize\r
637 )\r
638{\r
639 EFI_STATUS Status;\r
640 UINT8 *DstBuffer;\r
641 UINT8 *ScratchBuffer;\r
642 UINTN DstBufferSize;\r
643 UINT32 ScratchBufferSize;\r
644 EFI_COMMON_SECTION_HEADER *Section;\r
645 UINTN SectionLength;\r
646\r
647 if (CompressionSection->CommonHeader.Type != EFI_SECTION_COMPRESSION) {\r
648 ASSERT (FALSE);\r
649 return EFI_INVALID_PARAMETER;\r
650 }\r
651\r
652 Section = (EFI_COMMON_SECTION_HEADER *) CompressionSection;\r
653 SectionLength = *(UINT32 *) (Section->Size) & 0x00ffffff;\r
654 \r
655 //\r
656 // This is a compression set, expand it\r
657 //\r
658 switch (CompressionSection->CompressionType) {\r
659 case EFI_STANDARD_COMPRESSION:\r
660 //\r
661 // Load EFI standard compression.\r
662 // For compressed data, decompress them to dstbuffer.\r
663 //\r
664 Status = UefiDecompressGetInfo (\r
665 (UINT8 *) ((EFI_COMPRESSION_SECTION *) Section + 1),\r
666 (UINT32) SectionLength - sizeof (EFI_COMPRESSION_SECTION),\r
667 (UINT32 *) &DstBufferSize,\r
668 &ScratchBufferSize\r
669 );\r
670 if (EFI_ERROR (Status)) {\r
671 //\r
672 // GetInfo failed\r
673 //\r
674 DEBUG ((EFI_D_ERROR, "Decompress GetInfo Failed - %r\n", Status));\r
675 return EFI_NOT_FOUND;\r
676 }\r
677 //\r
678 // Allocate scratch buffer\r
679 //\r
680 ScratchBuffer = AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize));\r
681 if (ScratchBuffer == NULL) {\r
682 return EFI_OUT_OF_RESOURCES;\r
683 }\r
684 //\r
685 // Allocate destination buffer\r
686 //\r
687 DstBuffer = AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize));\r
688 if (DstBuffer == NULL) {\r
689 return EFI_OUT_OF_RESOURCES;\r
690 }\r
691 //\r
692 // Call decompress function\r
693 //\r
694 Status = UefiDecompress (\r
695 (CHAR8 *) ((EFI_COMPRESSION_SECTION *) Section + 1),\r
696 DstBuffer,\r
697 ScratchBuffer\r
698 );\r
699 if (EFI_ERROR (Status)) {\r
700 //\r
701 // Decompress failed\r
702 //\r
703 DEBUG ((EFI_D_ERROR, "Decompress Failed - %r\n", Status));\r
704 return EFI_NOT_FOUND;\r
705 }\r
706 break;\r
707\r
708 // porting note the original branch for customized compress is removed, it should be change to use GUID compress\r
709\r
710 case EFI_NOT_COMPRESSED:\r
711 //\r
712 // Allocate destination buffer\r
713 //\r
714 DstBufferSize = CompressionSection->UncompressedLength;\r
715 DstBuffer = AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize));\r
716 if (DstBuffer == NULL) {\r
717 return EFI_OUT_OF_RESOURCES;\r
718 }\r
719 //\r
720 // stream is not actually compressed, just encapsulated. So just copy it.\r
721 //\r
722 CopyMem (DstBuffer, CompressionSection + 1, DstBufferSize);\r
723 break;\r
724\r
725 default:\r
726 //\r
727 // Don't support other unknown compression type.\r
728 //\r
729 ASSERT (FALSE);\r
730 return EFI_NOT_FOUND;\r
731 }\r
732\r
733 *OutputSize = DstBufferSize;\r
734 *OutputBuffer = DstBuffer;\r
735\r
736 return EFI_SUCCESS;\r
737}\r
738\r