]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/DxeIplPeim/DxeLoad.c
Adding GCC cross-compiler information to BaseTools folder
[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
0a7d0741 227 EFI_SIZE_TO_PAGES ((UINT32) DxeCoreSize) * EFI_PAGE_SIZE,\r
95276127 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
10dde96f 348 PiLibInstallFvInfoPpi (\r
349 NULL,\r
350 FvHeader,\r
351 (UINT32) FvHeader->FvLength,\r
b0d803fe 352 &(VolumeInfo.FvName),\r
64253026 353 &(((EFI_FFS_FILE_HEADER*)FileHandle)->Name)\r
b0d803fe 354 );\r
10dde96f 355\r
356 //\r
357 // Inform HOB consumer phase, i.e. DXE core, the existance of this FV\r
358 //\r
359 BuildFvHob (\r
360 (EFI_PHYSICAL_ADDRESS) (UINTN) FvHeader,\r
361 FvHeader->FvLength\r
362 );\r
64253026 363 \r
b0d803fe 364 ASSERT_EFI_ERROR (Status);\r
95276127 365\r
b0d803fe 366 //\r
367 // Makes the encapsulated volume show up in DXE phase to skip processing of\r
368 // encapsulated file again.\r
369 //\r
370 BuildFv2Hob (\r
371 (EFI_PHYSICAL_ADDRESS)(UINTN)FvHeader,\r
372 FvHeader->FvLength, \r
373 &VolumeInfo.FvName,\r
374 &(((EFI_FFS_FILE_HEADER *)FileHandle)->Name)\r
375 );\r
376 return Status;\r
377 }\r
378 }\r
379 }\r
380 } while (!EFI_ERROR (VolumeStatus));\r
381 \r
382 return Status;\r
383}\r
95276127 384\r
b0d803fe 385/**\r
386 Find the First Volume that contains the first FileType.\r
387\r
388 @param Instance The Fv instance.\r
389 @param SeachType The type of file to search.\r
390 @param VolumeHandle Pointer to Fv which contains the file to search. \r
391 @param FileHandle Pointer to FFS file to search.\r
392 \r
393 @return EFI_SUCESS Success to find the FFS in specificed FV\r
394 @return others Fail to find the FFS in specificed FV\r
395 */\r
396EFI_STATUS\r
397DxeIplFindFirmwareVolumeInstance (\r
398 IN OUT UINTN *Instance,\r
399 IN EFI_FV_FILETYPE SeachType,\r
400 OUT EFI_PEI_FV_HANDLE *VolumeHandle,\r
401 OUT EFI_PEI_FILE_HANDLE *FileHandle\r
402 )\r
95276127 403{\r
b0d803fe 404 EFI_STATUS Status;\r
405 EFI_STATUS VolumeStatus;\r
95276127 406\r
b0d803fe 407 do {\r
408 VolumeStatus = PeiServicesFfsFindNextVolume (*Instance, VolumeHandle);\r
409 if (!EFI_ERROR (VolumeStatus)) {\r
410 *FileHandle = NULL;\r
411 Status = PeiServicesFfsFindNextFile (SeachType, *VolumeHandle, FileHandle);\r
412 if (!EFI_ERROR (Status)) {\r
413 return Status;\r
95276127 414 }\r
415 }\r
b0d803fe 416 *Instance += 1;\r
417 } while (!EFI_ERROR (VolumeStatus));\r
418\r
419 return VolumeStatus;\r
95276127 420}\r
421\r
b0d803fe 422/**\r
423 Loads and relocates a PE/COFF image into memory.\r
424\r
425 @param FileHandle The image file handle\r
426 @param ImageAddress The base address of the relocated PE/COFF image\r
427 @param ImageSize The size of the relocated PE/COFF image\r
428 @param EntryPoint The entry point of the relocated PE/COFF image\r
429 \r
430 @return EFI_SUCCESS The file was loaded and relocated\r
431 @return EFI_OUT_OF_RESOURCES There was not enough memory to load and relocate the PE/COFF file\r
432**/\r
95276127 433EFI_STATUS\r
434PeiLoadFile (\r
b0d803fe 435 IN EFI_PEI_FILE_HANDLE FileHandle,\r
95276127 436 OUT EFI_PHYSICAL_ADDRESS *ImageAddress,\r
437 OUT UINT64 *ImageSize,\r
438 OUT EFI_PHYSICAL_ADDRESS *EntryPoint\r
439 )\r
b0d803fe 440{\r
95276127 441\r
b0d803fe 442 EFI_STATUS Status;\r
443 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
444 VOID *Pe32Data;\r
b0d803fe 445 //\r
446 // First try to find the required section in this ffs file.\r
447 //\r
448 Status = PeiServicesFfsFindSectionData (\r
449 EFI_SECTION_PE32,\r
450 FileHandle,\r
451 &Pe32Data\r
452 );\r
95276127 453\r
b0d803fe 454 if (EFI_ERROR (Status)) {\r
455 Status = PeiServicesFfsFindSectionData (\r
456 EFI_SECTION_TE,\r
457 FileHandle,\r
458 &Pe32Data\r
459 );\r
460 }\r
461 \r
462 if (EFI_ERROR (Status)) {\r
463 //\r
464 // NO image types we support so exit.\r
465 //\r
466 return Status;\r
467 }\r
95276127 468\r
469 ZeroMem (&ImageContext, sizeof (ImageContext));\r
470 ImageContext.Handle = Pe32Data;\r
471 Status = GetImageReadFunction (&ImageContext);\r
472\r
473 ASSERT_EFI_ERROR (Status);\r
474\r
3d7b0992 475 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
95276127 476 if (EFI_ERROR (Status)) {\r
477 return Status;\r
478 }\r
479 //\r
480 // Allocate Memory for the image\r
481 //\r
482 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32) ImageContext.ImageSize));\r
483 ASSERT (ImageContext.ImageAddress != 0);\r
484\r
4e844595
LG
485 //\r
486 // Skip the reserved space for the stripped PeHeader when load TeImage into memory.\r
487 //\r
488 if (ImageContext.IsTeImage) {\r
489 ImageContext.ImageAddress = ImageContext.ImageAddress + \r
490 ((EFI_TE_IMAGE_HEADER *) Pe32Data)->StrippedSize -\r
491 sizeof (EFI_TE_IMAGE_HEADER);\r
492 }\r
493\r
95276127 494 //\r
495 // Load the image to our new buffer\r
496 //\r
3d7b0992 497 Status = PeCoffLoaderLoadImage (&ImageContext);\r
95276127 498 if (EFI_ERROR (Status)) {\r
499 return Status;\r
500 }\r
501 //\r
502 // Relocate the image in our new buffer\r
503 //\r
3d7b0992 504 Status = PeCoffLoaderRelocateImage (&ImageContext);\r
95276127 505 if (EFI_ERROR (Status)) {\r
506 return Status;\r
507 }\r
508\r
509 //\r
510 // Flush the instruction cache so the image data is written before we execute it\r
511 //\r
512 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r
513\r
514 *ImageAddress = ImageContext.ImageAddress;\r
515 *ImageSize = ImageContext.ImageSize;\r
516 *EntryPoint = ImageContext.EntryPoint;\r
517\r
518 return EFI_SUCCESS;\r
519}\r
520\r
d8c79a81
LG
521/**\r
522 The ExtractSection() function processes the input section and\r
523 returns a pointer to the section contents. If the section being\r
524 extracted does not require processing (if the section\r
525 GuidedSectionHeader.Attributes has the\r
526 EFI_GUIDED_SECTION_PROCESSING_REQUIRED field cleared), then\r
527 OutputBuffer is just updated to point to the start of the\r
528 section's contents. Otherwise, *Buffer must be allocated\r
529 from PEI permanent memory.\r
530\r
531 @param This Indicates the\r
532 EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI instance.\r
533 Buffer containing the input GUIDed section to be\r
534 processed. OutputBuffer OutputBuffer is\r
535 allocated from PEI permanent memory and contains\r
536 the new section stream.\r
537 \r
538 @param OutputSize A pointer to a caller-allocated\r
539 UINTN in which the size of *OutputBuffer\r
540 allocation is stored. If the function\r
541 returns anything other than EFI_SUCCESS,\r
542 the value of OutputSize is undefined.\r
543 \r
544 @param AuthenticationStatus A pointer to a caller-allocated\r
545 UINT32 that indicates the\r
546 authentication status of the\r
547 output buffer. If the input\r
548 section's GuidedSectionHeader.\r
549 Attributes field has the\r
550 EFI_GUIDED_SECTION_AUTH_STATUS_VALID \r
551 bit as clear,\r
552 AuthenticationStatus must return\r
553 zero. These bits reflect the\r
554 status of the extraction\r
555 operation. If the function\r
556 returns anything other than\r
557 EFI_SUCCESS, the value of\r
558 AuthenticationStatus is\r
559 undefined.\r
560 \r
561 @retval EFI_SUCCESS The InputSection was\r
562 successfully processed and the\r
563 section contents were returned.\r
564 \r
565 @retval EFI_OUT_OF_RESOURCES The system has insufficient\r
566 resources to process the request.\r
567 \r
568 @reteval EFI_INVALID_PARAMETER The GUID in InputSection does\r
569 not match this instance of the\r
570 GUIDed Section Extraction PPI.\r
571**/\r
572EFI_STATUS\r
18fd8d65 573CustomGuidedSectionExtract (\r
d8c79a81
LG
574 IN CONST EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI *This,\r
575 IN CONST VOID *InputSection,\r
576 OUT VOID **OutputBuffer,\r
577 OUT UINTN *OutputSize,\r
578 OUT UINT32 *AuthenticationStatus\r
579)\r
580{\r
581 EFI_STATUS Status;\r
582 UINT8 *ScratchBuffer;\r
18fd8d65
LG
583 UINT32 ScratchBufferSize;\r
584 UINT32 OutputBufferSize;\r
585 UINT16 SectionAttribute;\r
d8c79a81
LG
586 \r
587 //\r
18fd8d65 588 // Init local variable\r
d8c79a81 589 //\r
18fd8d65
LG
590 ScratchBuffer = NULL;\r
591\r
d8c79a81 592 //\r
18fd8d65 593 // Call GetInfo to get the size and attribute of input guided section data.\r
d8c79a81 594 //\r
18fd8d65
LG
595 Status = ExtractGuidedSectionGetInfo (\r
596 InputSection,\r
597 &OutputBufferSize,\r
598 &ScratchBufferSize,\r
599 &SectionAttribute\r
600 );\r
601 \r
d8c79a81 602 if (EFI_ERROR (Status)) {\r
18fd8d65
LG
603 DEBUG ((EFI_D_ERROR, "GetInfo from guided section Failed - %r\n", Status));\r
604 return Status;\r
605 }\r
606 \r
607 if (ScratchBufferSize != 0) {\r
95276127 608 //\r
18fd8d65 609 // Allocate scratch buffer\r
95276127 610 //\r
18fd8d65
LG
611 ScratchBuffer = AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize));\r
612 if (ScratchBuffer == NULL) {\r
613 return EFI_OUT_OF_RESOURCES;\r
614 }\r
d8c79a81 615 }\r
95276127 616\r
18fd8d65
LG
617 if ((SectionAttribute & EFI_GUIDED_SECTION_PROCESSING_REQUIRED) && OutputBufferSize > 0) { \r
618 //\r
619 // Allocate output buffer\r
620 //\r
621 *OutputBuffer = AllocatePages (EFI_SIZE_TO_PAGES (OutputBufferSize));\r
622 if (*OutputBuffer == NULL) {\r
623 return EFI_OUT_OF_RESOURCES;\r
624 }\r
95276127 625 }\r
18fd8d65
LG
626 \r
627 Status = ExtractGuidedSectionDecode (\r
628 InputSection, \r
629 OutputBuffer,\r
630 ScratchBuffer,\r
631 AuthenticationStatus\r
632 );\r
95276127 633\r
d8c79a81
LG
634 if (EFI_ERROR (Status)) {\r
635 //\r
18fd8d65 636 // Decode failed\r
d8c79a81
LG
637 //\r
638 DEBUG ((EFI_D_ERROR, "Extract guided section Failed - %r\n", Status));\r
639 return Status;\r
640 }\r
641 \r
18fd8d65
LG
642 *OutputSize = (UINTN) OutputBufferSize;\r
643 \r
95276127 644 return EFI_SUCCESS;\r
645}\r
b0d803fe 646\r
647STATIC\r
648EFI_STATUS\r
649EFIAPI \r
650Decompress (\r
651 IN CONST EFI_PEI_DECOMPRESS_PPI *This,\r
652 IN CONST EFI_COMPRESSION_SECTION *CompressionSection,\r
653 OUT VOID **OutputBuffer,\r
654 OUT UINTN *OutputSize\r
655 )\r
656{\r
657 EFI_STATUS Status;\r
658 UINT8 *DstBuffer;\r
659 UINT8 *ScratchBuffer;\r
660 UINTN DstBufferSize;\r
661 UINT32 ScratchBufferSize;\r
662 EFI_COMMON_SECTION_HEADER *Section;\r
663 UINTN SectionLength;\r
664\r
665 if (CompressionSection->CommonHeader.Type != EFI_SECTION_COMPRESSION) {\r
666 ASSERT (FALSE);\r
667 return EFI_INVALID_PARAMETER;\r
668 }\r
669\r
670 Section = (EFI_COMMON_SECTION_HEADER *) CompressionSection;\r
671 SectionLength = *(UINT32 *) (Section->Size) & 0x00ffffff;\r
672 \r
673 //\r
674 // This is a compression set, expand it\r
675 //\r
676 switch (CompressionSection->CompressionType) {\r
677 case EFI_STANDARD_COMPRESSION:\r
678 //\r
679 // Load EFI standard compression.\r
680 // For compressed data, decompress them to dstbuffer.\r
681 //\r
682 Status = UefiDecompressGetInfo (\r
683 (UINT8 *) ((EFI_COMPRESSION_SECTION *) Section + 1),\r
684 (UINT32) SectionLength - sizeof (EFI_COMPRESSION_SECTION),\r
685 (UINT32 *) &DstBufferSize,\r
686 &ScratchBufferSize\r
687 );\r
688 if (EFI_ERROR (Status)) {\r
689 //\r
690 // GetInfo failed\r
691 //\r
692 DEBUG ((EFI_D_ERROR, "Decompress GetInfo Failed - %r\n", Status));\r
693 return EFI_NOT_FOUND;\r
694 }\r
695 //\r
696 // Allocate scratch buffer\r
697 //\r
698 ScratchBuffer = AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize));\r
699 if (ScratchBuffer == NULL) {\r
700 return EFI_OUT_OF_RESOURCES;\r
701 }\r
702 //\r
703 // Allocate destination buffer\r
704 //\r
705 DstBuffer = AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize));\r
706 if (DstBuffer == NULL) {\r
707 return EFI_OUT_OF_RESOURCES;\r
708 }\r
709 //\r
710 // Call decompress function\r
711 //\r
712 Status = UefiDecompress (\r
713 (CHAR8 *) ((EFI_COMPRESSION_SECTION *) Section + 1),\r
714 DstBuffer,\r
715 ScratchBuffer\r
716 );\r
717 if (EFI_ERROR (Status)) {\r
718 //\r
719 // Decompress failed\r
720 //\r
721 DEBUG ((EFI_D_ERROR, "Decompress Failed - %r\n", Status));\r
722 return EFI_NOT_FOUND;\r
723 }\r
724 break;\r
725\r
726 // porting note the original branch for customized compress is removed, it should be change to use GUID compress\r
727\r
728 case EFI_NOT_COMPRESSED:\r
729 //\r
730 // Allocate destination buffer\r
731 //\r
732 DstBufferSize = CompressionSection->UncompressedLength;\r
733 DstBuffer = AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize));\r
734 if (DstBuffer == NULL) {\r
735 return EFI_OUT_OF_RESOURCES;\r
736 }\r
737 //\r
738 // stream is not actually compressed, just encapsulated. So just copy it.\r
739 //\r
740 CopyMem (DstBuffer, CompressionSection + 1, DstBufferSize);\r
741 break;\r
742\r
743 default:\r
744 //\r
745 // Don't support other unknown compression type.\r
746 //\r
747 ASSERT (FALSE);\r
748 return EFI_NOT_FOUND;\r
749 }\r
750\r
751 *OutputSize = DstBufferSize;\r
752 *OutputBuffer = DstBuffer;\r
753\r
754 return EFI_SUCCESS;\r
755}\r
756\r