]> git.proxmox.com Git - mirror_edk2.git/blame - EdkModulePkg/Core/DxeIplPeim/DxeLoad.c
Add null pointer check to drop-down list of platform Pcd editor.
[mirror_edk2.git] / EdkModulePkg / Core / DxeIplPeim / DxeLoad.c
CommitLineData
878ddf1f 1/*++\r
2\r
3Copyright (c) 2006, Intel Corporation\r
4All rights reserved. This program and the accompanying materials\r
5are licensed and made available under the terms and conditions of the BSD License\r
6which accompanies this distribution. The full text of the license may be found at\r
7http://opensource.org/licenses/bsd-license.php\r
8\r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
12Module Name:\r
13\r
14 DxeLoad.c\r
15\r
16Abstract:\r
17\r
18 Last PEIM.\r
19 Responsibility of this module is to load the DXE Core from a Firmware Volume.\r
20\r
21--*/\r
22\r
abb26634 23#include "DxeIpl.h"\r
24\r
878ddf1f 25BOOLEAN gInMemory = FALSE;\r
26\r
27//\r
28// Module Globals used in the DXE to PEI handoff\r
29// These must be module globals, so the stack can be switched\r
30//\r
31static EFI_DXE_IPL_PPI mDxeIplPpi = {\r
32 DxeLoadCore\r
33};\r
34\r
35static EFI_PEI_FV_FILE_LOADER_PPI mLoadFilePpi = {\r
36 DxeIplLoadFile\r
37};\r
38\r
abb26634 39static EFI_PEI_PPI_DESCRIPTOR mPpiList[] = {\r
40 {\r
41 EFI_PEI_PPI_DESCRIPTOR_PPI,\r
878ddf1f 42 &gEfiPeiFvFileLoaderPpiGuid,\r
43 &mLoadFilePpi\r
abb26634 44 },\r
45 {\r
878ddf1f 46 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
47 &gEfiDxeIplPpiGuid,\r
48 &mDxeIplPpi\r
abb26634 49 }\r
878ddf1f 50};\r
51\r
52static EFI_PEI_PPI_DESCRIPTOR mPpiSignal = {\r
53 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
54 &gEfiEndOfPeiSignalPpiGuid,\r
55 NULL\r
56};\r
57\r
abb26634 58GLOBAL_REMOVE_IF_UNREFERENCED DECOMPRESS_LIBRARY gEfiDecompress = {\r
878ddf1f 59 UefiDecompressGetInfo,\r
60 UefiDecompress\r
61};\r
62\r
abb26634 63GLOBAL_REMOVE_IF_UNREFERENCED DECOMPRESS_LIBRARY gTianoDecompress = {\r
878ddf1f 64 TianoDecompressGetInfo,\r
65 TianoDecompress\r
66};\r
67\r
abb26634 68GLOBAL_REMOVE_IF_UNREFERENCED DECOMPRESS_LIBRARY gCustomDecompress = {\r
878ddf1f 69 CustomDecompressGetInfo,\r
70 CustomDecompress\r
71};\r
72\r
878ddf1f 73EFI_STATUS\r
74EFIAPI\r
75PeimInitializeDxeIpl (\r
76 IN EFI_FFS_FILE_HEADER *FfsHeader,\r
77 IN EFI_PEI_SERVICES **PeiServices\r
78 )\r
79/*++\r
80\r
81Routine Description:\r
82\r
83 Initializes the Dxe Ipl PPI\r
84\r
85Arguments:\r
86\r
87 FfsHeader - Pointer to FFS file header\r
88 PeiServices - General purpose services available to every PEIM.\r
89\r
90Returns:\r
91\r
92 EFI_SUCCESS\r
93\r
94--*/\r
95{\r
96 EFI_STATUS Status;\r
97 EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader;\r
98 EFI_BOOT_MODE BootMode;\r
99\r
84a99d48 100 Status = PeiServicesGetBootMode (&BootMode);\r
878ddf1f 101 ASSERT_EFI_ERROR (Status);\r
102\r
abb26634 103 if (!gInMemory && (BootMode != BOOT_ON_S3_RESUME)) { \r
878ddf1f 104 //\r
105 // The DxeIpl has not yet been shadowed\r
106 //\r
107 PeiEfiPeiPeCoffLoader = (EFI_PEI_PE_COFF_LOADER_PROTOCOL *)GetPeCoffLoaderProtocol ();\r
108\r
109 //\r
110 // Shadow DxeIpl and then re-run its entry point\r
111 //\r
112 Status = ShadowDxeIpl (FfsHeader, PeiEfiPeiPeCoffLoader);\r
878ddf1f 113 } else {\r
878ddf1f 114 //\r
abb26634 115 // Install FvFileLoader and DxeIpl PPIs.\r
878ddf1f 116 //\r
abb26634 117 Status = PeiServicesInstallPpi (mPpiList);\r
118 ASSERT_EFI_ERROR(Status);\r
878ddf1f 119 }\r
abb26634 120 \r
121 return Status;\r
878ddf1f 122}\r
123\r
124EFI_STATUS\r
125EFIAPI\r
126DxeLoadCore (\r
127 IN EFI_DXE_IPL_PPI *This,\r
128 IN EFI_PEI_SERVICES **PeiServices,\r
129 IN EFI_PEI_HOB_POINTERS HobList\r
130 )\r
131/*++\r
132\r
133Routine Description:\r
134\r
135 Main entry point to last PEIM\r
136\r
137Arguments:\r
138 This - Entry point for DXE IPL PPI\r
139 PeiServices - General purpose services available to every PEIM.\r
140 HobList - Address to the Pei HOB list\r
141\r
142Returns:\r
143\r
144 EFI_SUCCESS - DEX core was successfully loaded.\r
145 EFI_OUT_OF_RESOURCES - There are not enough resources to load DXE core.\r
146\r
147--*/\r
148{\r
149 EFI_STATUS Status;\r
878ddf1f 150 EFI_GUID DxeCoreFileName;\r
151 EFI_GUID FirmwareFileName;\r
152 VOID *Pe32Data;\r
abb26634 153 VOID *FvImageData; \r
878ddf1f 154 EFI_PHYSICAL_ADDRESS DxeCoreAddress;\r
155 UINT64 DxeCoreSize;\r
156 EFI_PHYSICAL_ADDRESS DxeCoreEntryPoint;\r
157 EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader;\r
158 EFI_BOOT_MODE BootMode;\r
159 EFI_PEI_RECOVERY_MODULE_PPI *PeiRecovery;\r
160 EFI_PEI_S3_RESUME_PPI *S3Resume;\r
161\r
162// PERF_START (PeiServices, L"DxeIpl", NULL, 0);\r
878ddf1f 163\r
164 //\r
165 // if in S3 Resume, restore configure\r
166 //\r
84a99d48 167 Status = PeiServicesGetBootMode (&BootMode);\r
abb26634 168 ASSERT_EFI_ERROR(Status);\r
878ddf1f 169\r
abb26634 170 if (BootMode == BOOT_ON_S3_RESUME) {\r
84a99d48 171 Status = PeiServicesLocatePpi (\r
878ddf1f 172 &gEfiPeiS3ResumePpiGuid,\r
173 0,\r
174 NULL,\r
175 (VOID **)&S3Resume\r
176 );\r
878ddf1f 177 ASSERT_EFI_ERROR (Status);\r
178\r
179 Status = S3Resume->S3RestoreConfig (PeiServices);\r
878ddf1f 180 ASSERT_EFI_ERROR (Status);\r
abb26634 181 } else if (BootMode == BOOT_IN_RECOVERY_MODE) {\r
878ddf1f 182\r
84a99d48 183 Status = PeiServicesLocatePpi (\r
878ddf1f 184 &gEfiPeiRecoveryModulePpiGuid,\r
185 0,\r
186 NULL,\r
187 (VOID **)&PeiRecovery\r
188 );\r
878ddf1f 189 ASSERT_EFI_ERROR (Status);\r
abb26634 190\r
878ddf1f 191 Status = PeiRecovery->LoadRecoveryCapsule (PeiServices, PeiRecovery);\r
f78797d5
LG
192 if (EFI_ERROR (Status)) {\r
193 DEBUG ((EFI_D_ERROR, "Load Recovery Capsule Failed.(Status = %r)\n", Status));\r
194 CpuDeadLoop ();\r
195 }\r
878ddf1f 196\r
197 //\r
198 // Now should have a HOB with the DXE core w/ the old HOB destroyed\r
199 //\r
200 }\r
201\r
202 //\r
abb26634 203 // Install the PEI Protocols that are shared between PEI and DXE\r
204 //\r
205 PeiEfiPeiPeCoffLoader = (EFI_PEI_PE_COFF_LOADER_PROTOCOL *)GetPeCoffLoaderProtocol ();\r
206 ASSERT (PeiEfiPeiPeCoffLoader != NULL);\r
207\r
abb26634 208\r
209 //\r
210 // Find the EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE type compressed Firmware Volume file\r
878ddf1f 211 // The file found will be processed by PeiProcessFile: It will first be decompressed to\r
abb26634 212 // a normal FV, then a corresponding FV type hob will be built. \r
878ddf1f 213 //\r
214 Status = PeiFindFile (\r
abb26634 215 EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE,\r
216 EFI_SECTION_FIRMWARE_VOLUME_IMAGE,\r
217 &FirmwareFileName,\r
218 &FvImageData\r
219 );\r
878ddf1f 220\r
221 //\r
222 // Find the DXE Core in a Firmware Volume\r
223 //\r
224 Status = PeiFindFile (\r
225 EFI_FV_FILETYPE_DXE_CORE,\r
226 EFI_SECTION_PE32,\r
227 &DxeCoreFileName,\r
228 &Pe32Data\r
229 );\r
878ddf1f 230 ASSERT_EFI_ERROR (Status);\r
231\r
232 //\r
233 // Load the DXE Core from a Firmware Volume\r
234 //\r
235 Status = PeiLoadFile (\r
abb26634 236 PeiEfiPeiPeCoffLoader,\r
237 Pe32Data,\r
238 &DxeCoreAddress,\r
239 &DxeCoreSize,\r
240 &DxeCoreEntryPoint\r
241 );\r
878ddf1f 242 ASSERT_EFI_ERROR (Status);\r
243\r
878ddf1f 244 //\r
245 // Add HOB for the DXE Core\r
246 //\r
247 BuildModuleHob (\r
248 &DxeCoreFileName,\r
249 DxeCoreAddress,\r
250 DxeCoreSize,\r
251 DxeCoreEntryPoint\r
252 );\r
253\r
254 //\r
255 // Report Status Code EFI_SW_PEI_PC_HANDOFF_TO_NEXT\r
256 //\r
257 REPORT_STATUS_CODE (\r
258 EFI_PROGRESS_CODE,\r
259 EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_CORE_PC_HANDOFF_TO_NEXT\r
260 );\r
261\r
657d3918 262 if (FeaturePcdGet (PcdDxeIplBuildShareCodeHobs)) {\r
263 if (FeaturePcdGet (PcdDxeIplSupportEfiDecompress)) {\r
264 //\r
265 // Add HOB for the EFI Decompress Protocol\r
266 //\r
267 BuildGuidDataHob (\r
268 &gEfiDecompressProtocolGuid,\r
269 (VOID *)&gEfiDecompress,\r
270 sizeof (gEfiDecompress)\r
271 );\r
272 }\r
273 if (FeaturePcdGet (PcdDxeIplSupportTianoDecompress)) {\r
274 //\r
275 // Add HOB for the Tiano Decompress Protocol\r
276 //\r
277 BuildGuidDataHob (\r
278 &gEfiTianoDecompressProtocolGuid,\r
279 (VOID *)&gTianoDecompress,\r
280 sizeof (gTianoDecompress)\r
281 );\r
282 }\r
283 if (FeaturePcdGet (PcdDxeIplSupportCustomDecompress)) {\r
284 //\r
285 // Add HOB for the user customized Decompress Protocol\r
286 //\r
287 BuildGuidDataHob (\r
288 &gEfiCustomizedDecompressProtocolGuid,\r
289 (VOID *)&gCustomDecompress,\r
290 sizeof (gCustomDecompress)\r
291 );\r
292 }\r
abb26634 293\r
294 //\r
295 // Add HOB for the PE/COFF Loader Protocol\r
296 //\r
297 BuildGuidDataHob (\r
298 &gEfiPeiPeCoffLoaderGuid,\r
299 (VOID *)&PeiEfiPeiPeCoffLoader,\r
300 sizeof (VOID *)\r
301 );\r
657d3918 302 }\r
303\r
3d109491 304 //\r
305 // Transfer control to the DXE Core\r
306 // The handoff state is simply a pointer to the HOB list\r
307 //\r
308\r
b32a39b3 309 DEBUG ((EFI_D_INFO, "DXE Core Entry Point 0x%08x\n", (UINTN) DxeCoreEntryPoint));\r
3d109491 310 HandOffToDxeCore (DxeCoreEntryPoint, HobList, &mPpiSignal);\r
878ddf1f 311 //\r
312 // If we get here, then the DXE Core returned. This is an error\r
f78797d5 313 // Dxe Core should not return.\r
878ddf1f 314 //\r
f78797d5
LG
315 ASSERT (FALSE);\r
316 CpuDeadLoop ();\r
878ddf1f 317\r
318 return EFI_OUT_OF_RESOURCES;\r
319}\r
320\r
321EFI_STATUS\r
322PeiFindFile (\r
323 IN UINT8 Type,\r
324 IN UINT16 SectionType,\r
325 OUT EFI_GUID *FileName,\r
326 OUT VOID **Pe32Data\r
327 )\r
328/*++\r
329\r
330Routine Description:\r
331\r
332 Finds a PE/COFF of a specific Type and SectionType in the Firmware Volumes\r
333 described in the HOB list. Able to search in a compression set in a FFS file.\r
334 But only one level of compression is supported, that is, not able to search\r
335 in a compression set that is within another compression set.\r
336\r
337Arguments:\r
338\r
339 Type - The Type of file to retrieve\r
340\r
341 SectionType - The type of section to retrieve from a file\r
342\r
343 FileName - The name of the file found in the Firmware Volume\r
344\r
345 Pe32Data - Pointer to the beginning of the PE/COFF file found in the Firmware Volume\r
346\r
347Returns:\r
348\r
349 EFI_SUCCESS - The file was found, and the name is returned in FileName, and a pointer to\r
350 the PE/COFF image is returned in Pe32Data\r
351\r
352 EFI_NOT_FOUND - The file was not found in the Firmware Volumes present in the HOB List\r
353\r
354--*/\r
355{\r
356 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
357 EFI_FFS_FILE_HEADER *FfsFileHeader;\r
358 VOID *SectionData;\r
359 EFI_STATUS Status;\r
360 EFI_PEI_HOB_POINTERS Hob;\r
361\r
362\r
363 FwVolHeader = NULL;\r
364 FfsFileHeader = NULL;\r
365 SectionData = NULL;\r
abb26634 366 Status = EFI_SUCCESS;\r
878ddf1f 367\r
368 //\r
abb26634 369 // For each Firmware Volume, look for a specified type\r
370 // of file and break out until no one is found \r
878ddf1f 371 //\r
372 Hob.Raw = GetHobList ();\r
373 while ((Hob.Raw = GetNextHob (EFI_HOB_TYPE_FV, Hob.Raw)) != NULL) {\r
374 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (Hob.FirmwareVolume->BaseAddress);\r
84a99d48 375 Status = PeiServicesFfsFindNextFile (\r
878ddf1f 376 Type,\r
377 FwVolHeader,\r
378 &FfsFileHeader\r
379 );\r
380 if (!EFI_ERROR (Status)) {\r
878ddf1f 381 Status = PeiProcessFile (\r
382 SectionType,\r
abb26634 383 FfsFileHeader,\r
384 Pe32Data,\r
385 &Hob\r
878ddf1f 386 );\r
6326ee4e 387 CopyMem (FileName, &FfsFileHeader->Name, sizeof (EFI_GUID));\r
abb26634 388 if (!EFI_ERROR (Status)) {\r
389 return EFI_SUCCESS;\r
390 }\r
878ddf1f 391 }\r
392 Hob.Raw = GET_NEXT_HOB (Hob);\r
393 }\r
394 return EFI_NOT_FOUND;\r
395}\r
396\r
397EFI_STATUS\r
398PeiLoadFile (\r
399 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader,\r
400 IN VOID *Pe32Data,\r
401 OUT EFI_PHYSICAL_ADDRESS *ImageAddress,\r
402 OUT UINT64 *ImageSize,\r
403 OUT EFI_PHYSICAL_ADDRESS *EntryPoint\r
404 )\r
405/*++\r
406\r
407Routine Description:\r
408\r
409 Loads and relocates a PE/COFF image into memory.\r
410\r
411Arguments:\r
412\r
413 PeiEfiPeiPeCoffLoader - Pointer to a PE COFF loader protocol\r
414\r
415 Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated\r
416\r
417 ImageAddress - The base address of the relocated PE/COFF image\r
418\r
419 ImageSize - The size of the relocated PE/COFF image\r
420\r
421 EntryPoint - The entry point of the relocated PE/COFF image\r
422\r
423Returns:\r
424\r
425 EFI_SUCCESS - The file was loaded and relocated\r
426\r
427 EFI_OUT_OF_RESOURCES - There was not enough memory to load and relocate the PE/COFF file\r
428\r
429--*/\r
430{\r
431 EFI_STATUS Status;\r
432 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
433\r
434 ZeroMem (&ImageContext, sizeof (ImageContext));\r
435 ImageContext.Handle = Pe32Data;\r
436 Status = GetImageReadFunction (&ImageContext);\r
437\r
438 ASSERT_EFI_ERROR (Status);\r
439\r
440 Status = PeiEfiPeiPeCoffLoader->GetImageInfo (PeiEfiPeiPeCoffLoader, &ImageContext);\r
441 if (EFI_ERROR (Status)) {\r
442 return Status;\r
443 }\r
444 //\r
445 // Allocate Memory for the image\r
446 //\r
447 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32) ImageContext.ImageSize));\r
448 ASSERT (ImageContext.ImageAddress != 0);\r
449\r
450 //\r
451 // Load the image to our new buffer\r
452 //\r
453 Status = PeiEfiPeiPeCoffLoader->LoadImage (PeiEfiPeiPeCoffLoader, &ImageContext);\r
454 if (EFI_ERROR (Status)) {\r
455 return Status;\r
456 }\r
457 //\r
458 // Relocate the image in our new buffer\r
459 //\r
460 Status = PeiEfiPeiPeCoffLoader->RelocateImage (PeiEfiPeiPeCoffLoader, &ImageContext);\r
461 if (EFI_ERROR (Status)) {\r
462 return Status;\r
463 }\r
464\r
465 //\r
466 // Flush the instruction cache so the image data is written before we execute it\r
467 //\r
468 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r
469\r
470 *ImageAddress = ImageContext.ImageAddress;\r
471 *ImageSize = ImageContext.ImageSize;\r
472 *EntryPoint = ImageContext.EntryPoint;\r
473\r
474 return EFI_SUCCESS;\r
475}\r
476\r
477EFI_STATUS\r
478ShadowDxeIpl (\r
479 IN EFI_FFS_FILE_HEADER *DxeIplFileHeader,\r
480 IN EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader\r
481 )\r
482/*++\r
483\r
484Routine Description:\r
485\r
486 Shadow the DXE IPL to a different memory location. This occurs after permanent\r
487 memory has been discovered.\r
488\r
489Arguments:\r
490\r
491 DxeIplFileHeader - Pointer to the FFS file header of the DXE IPL driver\r
492\r
493 PeiEfiPeiPeCoffLoader - Pointer to a PE COFF loader protocol\r
494\r
495Returns:\r
496\r
497 EFI_SUCCESS - DXE IPL was successfully shadowed to a different memory location.\r
498\r
499 EFI_ ERROR - The shadow was unsuccessful.\r
500\r
501\r
502--*/\r
503{\r
504 UINTN SectionLength;\r
505 UINTN OccupiedSectionLength;\r
506 EFI_PHYSICAL_ADDRESS DxeIplAddress;\r
507 UINT64 DxeIplSize;\r
508 EFI_PHYSICAL_ADDRESS DxeIplEntryPoint;\r
509 EFI_STATUS Status;\r
510 EFI_COMMON_SECTION_HEADER *Section;\r
511\r
512 Section = (EFI_COMMON_SECTION_HEADER *) (DxeIplFileHeader + 1);\r
513\r
514 while ((Section->Type != EFI_SECTION_PE32) && (Section->Type != EFI_SECTION_TE)) {\r
515 SectionLength = *(UINT32 *) (Section->Size) & 0x00ffffff;\r
abb26634 516 OccupiedSectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);\r
878ddf1f 517 Section = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) Section + OccupiedSectionLength);\r
518 }\r
519 //\r
520 // Relocate DxeIpl into memory by using loadfile service\r
521 //\r
522 Status = PeiLoadFile (\r
523 PeiEfiPeiPeCoffLoader,\r
524 (VOID *) (Section + 1),\r
525 &DxeIplAddress,\r
526 &DxeIplSize,\r
527 &DxeIplEntryPoint\r
528 );\r
529\r
530 if (Status == EFI_SUCCESS) {\r
531 //\r
abb26634 532 // Set gInMemory global variable to TRUE to indicate the dxeipl is shadowed.\r
878ddf1f 533 //\r
abb26634 534 *(BOOLEAN *) ((UINTN) &gInMemory + (UINTN) DxeIplEntryPoint - (UINTN) _ModuleEntryPoint) = TRUE;\r
878ddf1f 535 Status = ((EFI_PEIM_ENTRY_POINT) (UINTN) DxeIplEntryPoint) (DxeIplFileHeader, GetPeiServicesTablePointer());\r
536 }\r
537\r
538 return Status;\r
539}\r
540\r
541EFI_STATUS\r
542EFIAPI\r
543DxeIplLoadFile (\r
544 IN EFI_PEI_FV_FILE_LOADER_PPI *This,\r
545 IN EFI_FFS_FILE_HEADER *FfsHeader,\r
546 OUT EFI_PHYSICAL_ADDRESS *ImageAddress,\r
547 OUT UINT64 *ImageSize,\r
548 OUT EFI_PHYSICAL_ADDRESS *EntryPoint\r
549 )\r
550/*++\r
551\r
552Routine Description:\r
553\r
554 Given a pointer to an FFS file containing a PE32 image, get the\r
555 information on the PE32 image, and then "load" it so that it\r
556 can be executed.\r
557\r
558Arguments:\r
559\r
560 This - pointer to our file loader protocol\r
561\r
562 FfsHeader - pointer to the FFS file header of the FFS file that\r
563 contains the PE32 image we want to load\r
564\r
565 ImageAddress - returned address where the PE32 image is loaded\r
566\r
567 ImageSize - returned size of the loaded PE32 image\r
568\r
569 EntryPoint - entry point to the loaded PE32 image\r
570\r
571Returns:\r
572\r
573 EFI_SUCCESS - The FFS file was successfully loaded.\r
574\r
575 EFI_ERROR - Unable to load the FFS file.\r
576\r
577--*/\r
578{\r
579 EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader;\r
580 EFI_STATUS Status;\r
581 VOID *Pe32Data;\r
582\r
583 Pe32Data = NULL;\r
584 PeiEfiPeiPeCoffLoader = (EFI_PEI_PE_COFF_LOADER_PROTOCOL *)GetPeCoffLoaderProtocol ();\r
585\r
586 //\r
587 // Preprocess the FFS file to get a pointer to the PE32 information\r
588 // in the enclosed PE32 image.\r
589 //\r
590 Status = PeiProcessFile (\r
591 EFI_SECTION_PE32,\r
abb26634 592 FfsHeader,\r
593 &Pe32Data,\r
594 NULL\r
878ddf1f 595 );\r
596\r
597 if (EFI_ERROR (Status)) {\r
598 return Status;\r
599 }\r
600 //\r
601 // Load the PE image from the FFS file\r
602 //\r
603 Status = PeiLoadFile (\r
604 PeiEfiPeiPeCoffLoader,\r
605 Pe32Data,\r
606 ImageAddress,\r
607 ImageSize,\r
608 EntryPoint\r
609 );\r
610\r
611 return Status;\r
612}\r
613\r
614EFI_STATUS\r
615PeiProcessFile (\r
6326ee4e 616 IN UINT16 SectionType,\r
abb26634 617 IN EFI_FFS_FILE_HEADER *FfsFileHeader,\r
618 OUT VOID **Pe32Data,\r
619 IN EFI_PEI_HOB_POINTERS *OrigHob\r
878ddf1f 620 )\r
621/*++\r
622\r
623Routine Description:\r
624\r
625Arguments:\r
626\r
627 SectionType - The type of section in the FFS file to process.\r
628\r
629 FfsFileHeader - Pointer to the FFS file to process, looking for the\r
630 specified SectionType\r
631\r
632 Pe32Data - returned pointer to the start of the PE32 image found\r
633 in the FFS file.\r
634\r
635Returns:\r
636\r
637 EFI_SUCCESS - found the PE32 section in the FFS file\r
638\r
639--*/\r
640{\r
641 EFI_STATUS Status;\r
642 VOID *SectionData;\r
643 DECOMPRESS_LIBRARY *DecompressLibrary;\r
644 UINT8 *DstBuffer;\r
645 UINT8 *ScratchBuffer;\r
646 UINT32 DstBufferSize;\r
647 UINT32 ScratchBufferSize;\r
648 EFI_COMMON_SECTION_HEADER *CmpSection;\r
649 UINTN CmpSectionLength;\r
650 UINTN OccupiedCmpSectionLength;\r
651 VOID *CmpFileData;\r
652 UINTN CmpFileSize;\r
653 EFI_COMMON_SECTION_HEADER *Section;\r
654 UINTN SectionLength;\r
655 UINTN OccupiedSectionLength;\r
656 UINT64 FileSize;\r
657 EFI_GUID_DEFINED_SECTION *GuidedSectionHeader;\r
658 UINT32 AuthenticationStatus;\r
659 EFI_PEI_SECTION_EXTRACTION_PPI *SectionExtract;\r
660 UINT32 BufferSize;\r
661 UINT8 *Buffer;\r
662 EFI_PEI_SECURITY_PPI *Security;\r
663 BOOLEAN StartCrisisRecovery;\r
664 EFI_GUID TempGuid;\r
665 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;\r
666 EFI_COMPRESSION_SECTION *CompressionSection;\r
abb26634 667 UINT32 FvAlignment;\r
878ddf1f 668\r
4afc6a7b
LG
669 //\r
670 // Initialize local variables.\r
671 //\r
672 DecompressLibrary = NULL;\r
673 DstBuffer = NULL;\r
674 DstBufferSize = 0;\r
675\r
84a99d48 676 Status = PeiServicesFfsFindSectionData (\r
878ddf1f 677 EFI_SECTION_COMPRESSION,\r
678 FfsFileHeader,\r
679 &SectionData\r
680 );\r
681\r
682 //\r
abb26634 683 // First process the compression section\r
878ddf1f 684 //\r
685 if (!EFI_ERROR (Status)) {\r
686 //\r
687 // Yes, there is a compression section, so extract the contents\r
688 // Decompress the image here\r
689 //\r
690 Section = (EFI_COMMON_SECTION_HEADER *) (UINTN) (VOID *) ((UINT8 *) (FfsFileHeader) + (UINTN) sizeof (EFI_FFS_FILE_HEADER));\r
691\r
692 do {\r
693 SectionLength = *(UINT32 *) (Section->Size) & 0x00ffffff;\r
abb26634 694 OccupiedSectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);\r
878ddf1f 695\r
696 //\r
697 // Was the DXE Core file encapsulated in a GUID'd section?\r
698 //\r
699 if (Section->Type == EFI_SECTION_GUID_DEFINED) {\r
700 //\r
701 // Locate the GUID'd Section Extractor\r
702 //\r
703 GuidedSectionHeader = (VOID *) (Section + 1);\r
704\r
705 //\r
706 // This following code constitutes the addition of the security model\r
707 // to the DXE IPL.\r
708 //\r
709 //\r
710 // Set a default authenticatino state\r
711 //\r
712 AuthenticationStatus = 0;\r
713\r
84a99d48 714 Status = PeiServicesLocatePpi (\r
878ddf1f 715 &gEfiPeiSectionExtractionPpiGuid,\r
716 0,\r
717 NULL,\r
718 (VOID **)&SectionExtract\r
719 );\r
720\r
721 if (EFI_ERROR (Status)) {\r
722 return Status;\r
723 }\r
724 //\r
725 // Verify Authentication State\r
726 //\r
727 CopyMem (&TempGuid, Section + 1, sizeof (EFI_GUID));\r
728\r
729 Status = SectionExtract->PeiGetSection (\r
730 GetPeiServicesTablePointer(),\r
731 SectionExtract,\r
732 (EFI_SECTION_TYPE *) &SectionType,\r
733 &TempGuid,\r
734 0,\r
735 (VOID **) &Buffer,\r
736 &BufferSize,\r
737 &AuthenticationStatus\r
738 );\r
739\r
740 if (EFI_ERROR (Status)) {\r
741 return Status;\r
742 }\r
743 //\r
744 // If not ask the Security PPI, if exists, for disposition\r
745 //\r
746 //\r
84a99d48 747 Status = PeiServicesLocatePpi (\r
878ddf1f 748 &gEfiPeiSecurityPpiGuid,\r
749 0,\r
750 NULL,\r
751 (VOID **)&Security\r
752 );\r
753 if (EFI_ERROR (Status)) {\r
754 return Status;\r
755 }\r
756\r
757 Status = Security->AuthenticationState (\r
758 GetPeiServicesTablePointer(),\r
759 (struct _EFI_PEI_SECURITY_PPI *) Security,\r
760 AuthenticationStatus,\r
761 FfsFileHeader,\r
762 &StartCrisisRecovery\r
763 );\r
764\r
765 if (EFI_ERROR (Status)) {\r
766 return Status;\r
767 }\r
768 //\r
769 // If there is a security violation, report to caller and have\r
770 // the upper-level logic possible engender a crisis recovery\r
771 //\r
772 if (StartCrisisRecovery) {\r
773 return EFI_SECURITY_VIOLATION;\r
774 }\r
775 }\r
776\r
777 if (Section->Type == EFI_SECTION_PE32) {\r
778 //\r
779 // This is what we want\r
780 //\r
781 *Pe32Data = (VOID *) (Section + 1);\r
782 return EFI_SUCCESS;\r
783 } else if (Section->Type == EFI_SECTION_COMPRESSION) {\r
784 //\r
785 // This is a compression set, expand it\r
786 //\r
787 CompressionSection = (EFI_COMPRESSION_SECTION *) Section;\r
788\r
789 switch (CompressionSection->CompressionType) {\r
790 case EFI_STANDARD_COMPRESSION:\r
4afc6a7b
LG
791 //\r
792 // Load EFI standard compression.\r
793 //\r
abb26634 794 if (FeaturePcdGet (PcdDxeIplSupportTianoDecompress)) {\r
4afc6a7b 795 DecompressLibrary = &gEfiDecompress;\r
abb26634 796 } else {\r
797 ASSERT (FALSE);\r
798 return EFI_NOT_FOUND;\r
799 }\r
878ddf1f 800 break;\r
801\r
802 case EFI_CUSTOMIZED_COMPRESSION:\r
803 //\r
4afc6a7b 804 // Load user customized compression.\r
878ddf1f 805 //\r
abb26634 806 if (FeaturePcdGet (PcdDxeIplSupportCustomDecompress)) {\r
807 DecompressLibrary = &gCustomDecompress;\r
808 } else {\r
809 ASSERT (FALSE);\r
810 return EFI_NOT_FOUND;\r
811 }\r
878ddf1f 812 break;\r
813\r
814 case EFI_NOT_COMPRESSED:\r
4afc6a7b
LG
815 //\r
816 // Allocate destination buffer\r
817 //\r
818 DstBufferSize = CompressionSection->UncompressedLength;\r
819 DstBuffer = AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize));\r
820 if (DstBuffer == NULL) {\r
821 return EFI_OUT_OF_RESOURCES;\r
822 }\r
823 //\r
824 // stream is not actually compressed, just encapsulated. So just copy it.\r
825 //\r
826 CopyMem (DstBuffer, CompressionSection + 1, DstBufferSize);\r
827 break;\r
828\r
878ddf1f 829 default:\r
830 //\r
4afc6a7b 831 // Don't support other unknown compression type.\r
878ddf1f 832 //\r
833 ASSERT_EFI_ERROR (Status);\r
834 return EFI_NOT_FOUND;\r
835 }\r
4afc6a7b
LG
836 \r
837 if (CompressionSection->CompressionType != EFI_NOT_COMPRESSED) {\r
878ddf1f 838 //\r
4afc6a7b 839 // For compressed data, decompress them to dstbuffer.\r
878ddf1f 840 //\r
4afc6a7b
LG
841 Status = DecompressLibrary->GetInfo (\r
842 (UINT8 *) ((EFI_COMPRESSION_SECTION *) Section + 1),\r
843 (UINT32) SectionLength - sizeof (EFI_COMPRESSION_SECTION),\r
844 &DstBufferSize,\r
845 &ScratchBufferSize\r
846 );\r
847 if (EFI_ERROR (Status)) {\r
848 //\r
849 // GetInfo failed\r
850 //\r
851 return EFI_NOT_FOUND;\r
852 }\r
853 \r
854 //\r
855 // Allocate scratch buffer\r
856 //\r
857 ScratchBuffer = AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize));\r
858 if (ScratchBuffer == NULL) {\r
859 return EFI_OUT_OF_RESOURCES;\r
860 }\r
861 \r
862 //\r
863 // Allocate destination buffer\r
864 //\r
865 DstBuffer = AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize));\r
866 if (DstBuffer == NULL) {\r
867 return EFI_OUT_OF_RESOURCES;\r
868 }\r
869 \r
870 //\r
871 // Call decompress function\r
872 //\r
873 Status = DecompressLibrary->Decompress (\r
874 (CHAR8 *) ((EFI_COMPRESSION_SECTION *) Section + 1),\r
875 DstBuffer,\r
876 ScratchBuffer\r
877 );\r
878ddf1f 878 }\r
4afc6a7b 879 \r
878ddf1f 880 CmpSection = (EFI_COMMON_SECTION_HEADER *) DstBuffer;\r
abb26634 881 if (CmpSection->Type == EFI_SECTION_FIRMWARE_VOLUME_IMAGE) {\r
882 // \r
883 // Firmware Volume Image in this Section\r
884 // Skip the section header to get FvHeader\r
878ddf1f 885 //\r
abb26634 886 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (CmpSection + 1);\r
878ddf1f 887\r
abb26634 888 if (FvHeader->Signature == EFI_FVH_SIGNATURE) { \r
889 //\r
890 // Adjust Fv Base Address Alignment based on Align Attributes in Fv Header\r
891 //\r
892 \r
893 //\r
894 // When FvImage support Alignment, we need to check whether \r
895 // its alignment is correct. \r
896 //\r
897 if (FvHeader->Attributes | EFI_FVB_ALIGNMENT_CAP) {\r
898 \r
899 //\r
900 // Calculate the mini alignment for this FvImage\r
901 //\r
902 FvAlignment = 1 << (LowBitSet32 (FvHeader->Attributes >> 16) + 1);\r
903 \r
904 //\r
905 // If current FvImage base address doesn't meet the its alignment,\r
906 // we need to reload this FvImage to another correct memory address.\r
907 //\r
908 if (((UINTN) FvHeader % FvAlignment) != 0) {\r
909 DstBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES ((UINTN) FvHeader->FvLength), FvAlignment);\r
910 if (DstBuffer == NULL) {\r
911 return EFI_OUT_OF_RESOURCES;\r
912 }\r
913 CopyMem (DstBuffer, FvHeader, (UINTN) FvHeader->FvLength);\r
914 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) DstBuffer; \r
915 }\r
916 }\r
917 //\r
918 // Build new FvHob for new decompressed Fv image.\r
919 //\r
878ddf1f 920 BuildFvHob ((EFI_PHYSICAL_ADDRESS) (UINTN) FvHeader, FvHeader->FvLength);\r
abb26634 921 \r
922 //\r
923 // Set the original FvHob to unused.\r
924 //\r
925 if (OrigHob != NULL) {\r
926 OrigHob->Header->HobType = EFI_HOB_TYPE_UNUSED;\r
878ddf1f 927 }\r
abb26634 928 \r
6326ee4e 929 //\r
abb26634 930 // when search FvImage Section return true.\r
6326ee4e 931 //\r
abb26634 932 if (SectionType == EFI_SECTION_FIRMWARE_VOLUME_IMAGE) {\r
933 *Pe32Data = (VOID *) FvHeader;\r
934 return EFI_SUCCESS;\r
935 } else {\r
936 return EFI_NOT_FOUND;\r
937 }\r
878ddf1f 938 }\r
939 }\r
940 //\r
941 // Decompress successfully.\r
942 // Loop the decompressed data searching for expected section.\r
943 //\r
944 CmpFileData = (VOID *) DstBuffer;\r
945 CmpFileSize = DstBufferSize;\r
946 do {\r
947 CmpSectionLength = *(UINT32 *) (CmpSection->Size) & 0x00ffffff;\r
948 if (CmpSection->Type == EFI_SECTION_PE32) {\r
949 //\r
950 // This is what we want\r
951 //\r
952 *Pe32Data = (VOID *) (CmpSection + 1);\r
953 return EFI_SUCCESS;\r
954 }\r
955\r
abb26634 956 OccupiedCmpSectionLength = GET_OCCUPIED_SIZE (CmpSectionLength, 4);\r
878ddf1f 957 CmpSection = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) CmpSection + OccupiedCmpSectionLength);\r
958 } while (CmpSection->Type != 0 && (UINTN) ((UINT8 *) CmpSection - (UINT8 *) CmpFileData) < CmpFileSize);\r
959 }\r
abb26634 960 //\r
961 // End of the decompression activity\r
962 //\r
878ddf1f 963\r
964 Section = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) Section + OccupiedSectionLength);\r
965 FileSize = FfsFileHeader->Size[0] & 0xFF;\r
966 FileSize += (FfsFileHeader->Size[1] << 8) & 0xFF00;\r
967 FileSize += (FfsFileHeader->Size[2] << 16) & 0xFF0000;\r
968 FileSize &= 0x00FFFFFF;\r
969 } while (Section->Type != 0 && (UINTN) ((UINT8 *) Section - (UINT8 *) FfsFileHeader) < FileSize);\r
abb26634 970 \r
878ddf1f 971 //\r
abb26634 972 // search all sections (compression and non compression) in this FFS, don't \r
973 // find expected section.\r
878ddf1f 974 //\r
abb26634 975 return EFI_NOT_FOUND;\r
878ddf1f 976 } else {\r
abb26634 977 //\r
978 // For those FFS that doesn't contain compression section, directly search \r
979 // PE or TE section in this FFS.\r
980 //\r
878ddf1f 981\r
84a99d48 982 Status = PeiServicesFfsFindSectionData (\r
878ddf1f 983 EFI_SECTION_PE32,\r
984 FfsFileHeader,\r
985 &SectionData\r
986 );\r
987\r
988 if (EFI_ERROR (Status)) {\r
84a99d48 989 Status = PeiServicesFfsFindSectionData (\r
878ddf1f 990 EFI_SECTION_TE,\r
991 FfsFileHeader,\r
992 &SectionData\r
993 );\r
994 if (EFI_ERROR (Status)) {\r
995 return Status;\r
996 }\r
997 }\r
998 }\r
999\r
1000 *Pe32Data = SectionData;\r
1001\r
1002 return EFI_SUCCESS;\r
1003}\r
abb26634 1004\r