]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Core/Pei/FwVol/FwVol.c
Enhance HiiThunkGetString to handle the case that PlatformLanguage is not set.
[mirror_edk2.git] / MdeModulePkg / Core / Pei / FwVol / FwVol.c
... / ...
CommitLineData
1/** @file\r
2 Pei Core Firmware File System service routines.\r
3 \r
4Copyright (c) 2006 - 2009, Intel Corporation \r
5All rights reserved. This program and the accompanying materials \r
6are licensed and made available under the terms and conditions of the BSD License \r
7which accompanies this distribution. The full text of the license may be found at \r
8http://opensource.org/licenses/bsd-license.php \r
9 \r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
12\r
13**/\r
14\r
15#include "FwVol.h"\r
16\r
17EFI_PEI_NOTIFY_DESCRIPTOR mNotifyOnFvInfoList = {\r
18 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
19 &gEfiPeiFirmwareVolumeInfoPpiGuid,\r
20 FirmwareVolmeInfoPpiNotifyCallback \r
21};\r
22\r
23EFI_PEI_FIRMWARE_VOLUME_PPI mPeiFfs2FvPpi = {\r
24 PeiFfs2FvPpiProcessVolume,\r
25 PeiFfs2FvPpiFindFileByType,\r
26 PeiFfs2FvPpiFindFileByName,\r
27 PeiFfs2FvPpiGetFileInfo,\r
28 PeiFfs2FvPpiGetVolumeInfo,\r
29 PeiFfs2FvPpiFindSectionByType\r
30};\r
31 \r
32EFI_PEI_PPI_DESCRIPTOR mPeiFfs2FvPpiList = {\r
33 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
34 &gEfiFirmwareFileSystem2Guid,\r
35 &mPeiFfs2FvPpi\r
36};\r
37 \r
38/**\r
39 Returns the file state set by the highest zero bit in the State field\r
40\r
41 @param ErasePolarity Erase Polarity as defined by EFI_FVB2_ERASE_POLARITY\r
42 in the Attributes field.\r
43 @param FfsHeader Pointer to FFS File Header.\r
44\r
45 @retval EFI_FFS_FILE_STATE File state is set by the highest none zero bit \r
46 in the header State field.\r
47**/\r
48EFI_FFS_FILE_STATE\r
49GetFileState(\r
50 IN UINT8 ErasePolarity,\r
51 IN EFI_FFS_FILE_HEADER *FfsHeader\r
52 )\r
53{\r
54 EFI_FFS_FILE_STATE FileState;\r
55 EFI_FFS_FILE_STATE HighestBit;\r
56\r
57 FileState = FfsHeader->State;\r
58\r
59 if (ErasePolarity != 0) {\r
60 FileState = (EFI_FFS_FILE_STATE)~FileState;\r
61 }\r
62 \r
63 //\r
64 // Get file state set by its highest none zero bit.\r
65 //\r
66 HighestBit = 0x80;\r
67 while (HighestBit != 0 && (HighestBit & FileState) == 0) {\r
68 HighestBit >>= 1;\r
69 }\r
70\r
71 return HighestBit;\r
72} \r
73\r
74/**\r
75 Calculates the checksum of the header of a file.\r
76\r
77 @param FileHeader Pointer to FFS File Header.\r
78\r
79 @return Checksum of the header.\r
80 Zero means the header is good.\r
81 Non-zero means the header is bad.\r
82**/\r
83UINT8\r
84CalculateHeaderChecksum (\r
85 IN EFI_FFS_FILE_HEADER *FileHeader\r
86 )\r
87{\r
88 EFI_FFS_FILE_HEADER TestFileHeader;\r
89 \r
90 CopyMem (&TestFileHeader, FileHeader, sizeof (EFI_FFS_FILE_HEADER));\r
91 //\r
92 // Ingore State and File field in FFS header.\r
93 //\r
94 TestFileHeader.State = 0;\r
95 TestFileHeader.IntegrityCheck.Checksum.File = 0;\r
96\r
97 return CalculateSum8 ((CONST UINT8 *) &TestFileHeader, sizeof (EFI_FFS_FILE_HEADER));\r
98}\r
99\r
100/**\r
101 Find FV handler according to FileHandle in that FV.\r
102\r
103 @param FileHandle Handle of file image\r
104 \r
105 @return Pointer to instance of PEI_CORE_FV_HANDLE.\r
106**/\r
107PEI_CORE_FV_HANDLE*\r
108FileHandleToVolume (\r
109 IN EFI_PEI_FILE_HANDLE FileHandle\r
110 )\r
111{\r
112 UINTN Index;\r
113 PEI_CORE_INSTANCE *PrivateData;\r
114 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
115\r
116 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r
117 \r
118 for (Index = 0; Index < PrivateData->FvCount; Index++) {\r
119 FwVolHeader = PrivateData->Fv[Index].FvHeader;\r
120 if (((UINT64) (UINTN) FileHandle > (UINT64) (UINTN) FwVolHeader ) && \\r
121 ((UINT64) (UINTN) FileHandle <= ((UINT64) (UINTN) FwVolHeader + FwVolHeader->FvLength - 1))) {\r
122 return &PrivateData->Fv[Index];\r
123 }\r
124 }\r
125 return NULL;\r
126}\r
127\r
128/**\r
129 Given the input file pointer, search for the first matching file in the\r
130 FFS volume as defined by SearchType. The search starts from FileHeader inside\r
131 the Firmware Volume defined by FwVolHeader.\r
132 If SearchType is EFI_FV_FILETYPE_ALL, the first FFS file will return without check its file type.\r
133 If SearchType is PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE, \r
134 the first PEIM, or COMBINED PEIM or FV file type FFS file will return. \r
135\r
136 @param FvHandle Pointer to the FV header of the volume to search\r
137 @param FileName File name\r
138 @param SearchType Filter to find only files of this type.\r
139 Type EFI_FV_FILETYPE_ALL causes no filtering to be done.\r
140 @param FileHandle This parameter must point to a valid FFS volume.\r
141 @param AprioriFile Pointer to AprioriFile image in this FV if has\r
142\r
143 @return EFI_NOT_FOUND No files matching the search criteria were found\r
144 @retval EFI_SUCCESS Success to search given file\r
145\r
146**/\r
147EFI_STATUS\r
148FindFileEx (\r
149 IN CONST EFI_PEI_FV_HANDLE FvHandle,\r
150 IN CONST EFI_GUID *FileName, OPTIONAL\r
151 IN EFI_FV_FILETYPE SearchType,\r
152 IN OUT EFI_PEI_FILE_HANDLE *FileHandle,\r
153 IN OUT EFI_PEI_FV_HANDLE *AprioriFile OPTIONAL\r
154 )\r
155{\r
156 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r
157 EFI_FFS_FILE_HEADER **FileHeader;\r
158 EFI_FFS_FILE_HEADER *FfsFileHeader;\r
159 UINT32 FileLength;\r
160 UINT32 FileOccupiedSize;\r
161 UINT32 FileOffset;\r
162 UINT64 FvLength;\r
163 UINT8 ErasePolarity;\r
164 UINT8 FileState;\r
165 \r
166 //\r
167 // Convert the handle of FV to FV header for memory-mapped firmware volume\r
168 //\r
169 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) FvHandle;\r
170 FileHeader = (EFI_FFS_FILE_HEADER **)FileHandle;\r
171\r
172 FvLength = FwVolHeader->FvLength;\r
173 if ((FwVolHeader->Attributes & EFI_FVB2_ERASE_POLARITY) != 0) {\r
174 ErasePolarity = 1;\r
175 } else {\r
176 ErasePolarity = 0;\r
177 }\r
178 \r
179 //\r
180 // If FileHeader is not specified (NULL) or FileName is not NULL,\r
181 // start with the first file in the firmware volume. Otherwise,\r
182 // start from the FileHeader.\r
183 //\r
184 if ((*FileHeader == NULL) || (FileName != NULL)) {\r
185 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FwVolHeader + FwVolHeader->HeaderLength);\r
186 } else {\r
187 //\r
188 // Length is 24 bits wide so mask upper 8 bits\r
189 // FileLength is adjusted to FileOccupiedSize as it is 8 byte aligned.\r
190 //\r
191 FileLength = *(UINT32 *)(*FileHeader)->Size & 0x00FFFFFF;\r
192 FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);\r
193 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)*FileHeader + FileOccupiedSize);\r
194 }\r
195 \r
196 FileOffset = (UINT32) ((UINT8 *)FfsFileHeader - (UINT8 *)FwVolHeader);\r
197 ASSERT (FileOffset <= 0xFFFFFFFF);\r
198\r
199 while (FileOffset < (FvLength - sizeof (EFI_FFS_FILE_HEADER))) {\r
200 //\r
201 // Get FileState which is the highest bit of the State \r
202 //\r
203 FileState = GetFileState (ErasePolarity, FfsFileHeader);\r
204 switch (FileState) {\r
205\r
206 case EFI_FILE_HEADER_INVALID:\r
207 FileOffset += sizeof(EFI_FFS_FILE_HEADER);\r
208 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + sizeof(EFI_FFS_FILE_HEADER));\r
209 break;\r
210 \r
211 case EFI_FILE_DATA_VALID:\r
212 case EFI_FILE_MARKED_FOR_UPDATE:\r
213 if (CalculateHeaderChecksum (FfsFileHeader) != 0) {\r
214 ASSERT (FALSE);\r
215 *FileHeader = NULL;\r
216 return EFI_NOT_FOUND;\r
217 }\r
218\r
219 FileLength = *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF;\r
220 FileOccupiedSize = GET_OCCUPIED_SIZE(FileLength, 8);\r
221\r
222 if (FileName != NULL) {\r
223 if (CompareGuid (&FfsFileHeader->Name, (EFI_GUID*)FileName)) {\r
224 *FileHeader = FfsFileHeader;\r
225 return EFI_SUCCESS;\r
226 }\r
227 } else if (SearchType == PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE) {\r
228 if ((FfsFileHeader->Type == EFI_FV_FILETYPE_PEIM) || \r
229 (FfsFileHeader->Type == EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER) ||\r
230 (FfsFileHeader->Type == EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE)) { \r
231 \r
232 *FileHeader = FfsFileHeader;\r
233 return EFI_SUCCESS;\r
234 } else if (AprioriFile != NULL) {\r
235 if (FfsFileHeader->Type == EFI_FV_FILETYPE_FREEFORM) {\r
236 if (CompareGuid (&FfsFileHeader->Name, &gPeiAprioriFileNameGuid)) {\r
237 *AprioriFile = FfsFileHeader;\r
238 } \r
239 } \r
240 }\r
241 } else if (((SearchType == FfsFileHeader->Type) || (SearchType == EFI_FV_FILETYPE_ALL)) && \r
242 (FfsFileHeader->Type != EFI_FV_FILETYPE_FFS_PAD)) { \r
243 *FileHeader = FfsFileHeader;\r
244 return EFI_SUCCESS;\r
245 }\r
246\r
247 FileOffset += FileOccupiedSize; \r
248 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize);\r
249 break;\r
250 \r
251 case EFI_FILE_DELETED:\r
252 FileLength = *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF;\r
253 FileOccupiedSize = GET_OCCUPIED_SIZE(FileLength, 8);\r
254 FileOffset += FileOccupiedSize;\r
255 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize);\r
256 break;\r
257\r
258 default:\r
259 *FileHeader = NULL;\r
260 return EFI_NOT_FOUND;\r
261 } \r
262 }\r
263 \r
264 *FileHeader = NULL;\r
265 return EFI_NOT_FOUND; \r
266}\r
267\r
268/**\r
269 Initialize PeiCore Fv List.\r
270\r
271 @param PrivateData - Pointer to PEI_CORE_INSTANCE.\r
272 @param SecCoreData - Pointer to EFI_SEC_PEI_HAND_OFF.\r
273**/\r
274VOID \r
275PeiInitializeFv (\r
276 IN PEI_CORE_INSTANCE *PrivateData,\r
277 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData\r
278 )\r
279{\r
280 EFI_STATUS Status;\r
281 EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi;\r
282 EFI_PEI_FV_HANDLE FvHandle;\r
283 EFI_FIRMWARE_VOLUME_HEADER *BfvHeader;\r
284 \r
285 //\r
286 // Install FV_PPI for FFS2 file system.\r
287 //\r
288 PeiServicesInstallPpi (&mPeiFfs2FvPpiList);\r
289 \r
290 BfvHeader = (EFI_FIRMWARE_VOLUME_HEADER *)SecCoreData->BootFirmwareVolumeBase;\r
291 \r
292 //\r
293 // The FV_PPI in BFV's format should be installed.\r
294 //\r
295 Status = PeiServicesLocatePpi (\r
296 &BfvHeader->FileSystemGuid,\r
297 0,\r
298 NULL,\r
299 (VOID**)&FvPpi\r
300 );\r
301 ASSERT_EFI_ERROR (Status);\r
302 \r
303 //\r
304 // Get handle of BFV\r
305 //\r
306 FvPpi->ProcessVolume (\r
307 FvPpi, \r
308 SecCoreData->BootFirmwareVolumeBase,\r
309 (UINTN)BfvHeader->FvLength,\r
310 &FvHandle\r
311 );\r
312 \r
313 //\r
314 // Post a call-back for the FvInfoPPI services to expose\r
315 // additional Fvs to PeiCore.\r
316 //\r
317 Status = PeiServicesNotifyPpi (&mNotifyOnFvInfoList);\r
318 ASSERT_EFI_ERROR (Status);\r
319\r
320}\r
321 \r
322/**\r
323 Process Firmware Volum Information once FvInfoPPI install.\r
324 The FV Info will be registered into PeiCore private data structure.\r
325 And search the inside FV image, if found, the new FV INFO PPI will be installed.\r
326\r
327 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
328 @param NotifyDescriptor Address of the notification descriptor data structure.\r
329 @param Ppi Address of the PPI that was installed.\r
330\r
331 @retval EFI_SUCCESS The FV Info is registered into PeiCore private data structure.\r
332 @return if not EFI_SUCESS, fail to verify FV.\r
333\r
334**/\r
335EFI_STATUS\r
336EFIAPI\r
337FirmwareVolmeInfoPpiNotifyCallback (\r
338 IN EFI_PEI_SERVICES **PeiServices,\r
339 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,\r
340 IN VOID *Ppi\r
341 )\r
342{\r
343 EFI_PEI_FIRMWARE_VOLUME_INFO_PPI *FvInfoPpi;\r
344 EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi;\r
345 PEI_CORE_INSTANCE *PrivateData;\r
346 EFI_STATUS Status;\r
347 EFI_PEI_FV_HANDLE FvHandle;\r
348 UINTN FvIndex;\r
349 \r
350 Status = EFI_SUCCESS;\r
351 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
352\r
353 if (PrivateData->FvCount >= FixedPcdGet32 (PcdPeiCoreMaxFvSupported)) {\r
354 DEBUG ((EFI_D_ERROR, "The number of Fv Images (%d) exceed the max supported FVs (%d) in Pei", PrivateData->FvCount + 1, FixedPcdGet32 (PcdPeiCoreMaxFvSupported)));\r
355 DEBUG ((EFI_D_ERROR, "PcdPeiCoreMaxFvSupported value need be reconfigurated in DSC"));\r
356 ASSERT (FALSE);\r
357 }\r
358\r
359 FvInfoPpi = (EFI_PEI_FIRMWARE_VOLUME_INFO_PPI *)Ppi;\r
360\r
361 //\r
362 // Locate the corresponding FV_PPI according to founded FV's format guid\r
363 //\r
364 Status = PeiServicesLocatePpi (\r
365 &FvInfoPpi->FvFormat, \r
366 0, \r
367 NULL,\r
368 (VOID**)&FvPpi\r
369 );\r
370 if (!EFI_ERROR (Status)) {\r
371 //\r
372 // Process new found FV and get FV handle.\r
373 //\r
374 Status = FvPpi->ProcessVolume (FvPpi, FvInfoPpi->FvInfo, FvInfoPpi->FvInfoSize, &FvHandle);\r
375 if (EFI_ERROR (Status)) {\r
376 DEBUG ((EFI_D_ERROR, "Fail to process new found FV, FV may be corrupted!"));\r
377 return Status;\r
378 }\r
379 DEBUG ((EFI_D_INFO, "Found and process new FV %p, all fv's count is %d\n", FvHandle, PrivateData->FvCount));\r
380 } else {\r
381 DEBUG ((EFI_D_ERROR, "Fail to process FV %p because no corresponding EFI_FIRMWARE_VOLUME_PPI is found!\n", FvInfoPpi->FvInfo));\r
382 \r
383 //\r
384 // If can not find EFI_FIRMWARE_VOLUME_PPI to process firmware to get FvHandle, \r
385 // use the address of FV buffer as its handle.\r
386 //\r
387 FvHandle = FvInfoPpi->FvInfo;\r
388 \r
389 //\r
390 // Check whether the FV has already been processed.\r
391 //\r
392 for (FvIndex = 0; FvIndex < PrivateData->FvCount; FvIndex ++) {\r
393 if (PrivateData->Fv[FvIndex].FvHandle == FvHandle) {\r
394 DEBUG ((EFI_D_INFO, "The Fv %p has already been processed!\n", FvHandle));\r
395 return EFI_SUCCESS;\r
396 }\r
397 } \r
398 \r
399 PrivateData->Fv[PrivateData->FvCount].FvHeader = (EFI_FIRMWARE_VOLUME_HEADER*) FvInfoPpi->FvInfo;\r
400 PrivateData->Fv[PrivateData->FvCount].FvPpi = NULL;\r
401 PrivateData->Fv[PrivateData->FvCount].FvHandle = FvHandle;\r
402 PrivateData->FvCount ++;\r
403 }\r
404 \r
405 return EFI_SUCCESS;\r
406}\r
407\r
408/**\r
409 Go through the file to search SectionType section. \r
410 Search within encapsulation sections (compression and GUIDed) recursively, \r
411 until the match section is found.\r
412 \r
413 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
414 @param SectionType Filter to find only section of this type.\r
415 @param Section From where to search.\r
416 @param SectionSize The file size to search.\r
417 @param OutputBuffer A pointer to the discovered section, if successful.\r
418 NULL if section not found\r
419\r
420 @return EFI_NOT_FOUND The match section is not found.\r
421 @return EFI_SUCCESS The match section is found.\r
422\r
423**/\r
424EFI_STATUS\r
425ProcessSection (\r
426 IN CONST EFI_PEI_SERVICES **PeiServices,\r
427 IN EFI_SECTION_TYPE SectionType,\r
428 IN EFI_COMMON_SECTION_HEADER *Section,\r
429 IN UINTN SectionSize,\r
430 OUT VOID **OutputBuffer\r
431 )\r
432{\r
433 EFI_STATUS Status;\r
434 UINT32 SectionLength;\r
435 UINT32 ParsedLength;\r
436 EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI *GuidSectionPpi;\r
437 EFI_PEI_DECOMPRESS_PPI *DecompressPpi;\r
438 VOID *PpiOutput;\r
439 UINTN PpiOutputSize;\r
440 UINTN Index;\r
441 UINT32 Authentication;\r
442 PEI_CORE_INSTANCE *PrivateData;\r
443\r
444 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
445 *OutputBuffer = NULL;\r
446 ParsedLength = 0;\r
447 Index = 0;\r
448 Status = EFI_NOT_FOUND;\r
449 PpiOutput = NULL;\r
450 PpiOutputSize = 0;\r
451 while (ParsedLength < SectionSize) {\r
452 if (Section->Type == SectionType) {\r
453 *OutputBuffer = (VOID *)(Section + 1);\r
454 return EFI_SUCCESS;\r
455 } else if ((Section->Type == EFI_SECTION_GUID_DEFINED) || (Section->Type == EFI_SECTION_COMPRESSION)) {\r
456 //\r
457 // Check the encapsulated section is extracted into the cache data.\r
458 //\r
459 for (Index = 0; Index < PrivateData->CacheSection.AllSectionCount; Index ++) {\r
460 if (Section == PrivateData->CacheSection.Section[Index]) {\r
461 PpiOutput = PrivateData->CacheSection.SectionData[Index];\r
462 PpiOutputSize = PrivateData->CacheSection.SectionSize[Index];\r
463 //\r
464 // Search section directly from the cache data.\r
465 //\r
466 return ProcessSection (\r
467 PeiServices,\r
468 SectionType, \r
469 PpiOutput, \r
470 PpiOutputSize, \r
471 OutputBuffer \r
472 );\r
473 }\r
474 }\r
475 \r
476 Status = EFI_NOT_FOUND;\r
477 if (Section->Type == EFI_SECTION_GUID_DEFINED) {\r
478 Status = PeiServicesLocatePpi (\r
479 &((EFI_GUID_DEFINED_SECTION *)Section)->SectionDefinitionGuid, \r
480 0, \r
481 NULL, \r
482 (VOID **) &GuidSectionPpi\r
483 );\r
484 if (!EFI_ERROR (Status)) {\r
485 Status = GuidSectionPpi->ExtractSection (\r
486 GuidSectionPpi,\r
487 Section,\r
488 &PpiOutput,\r
489 &PpiOutputSize,\r
490 &Authentication\r
491 );\r
492 }\r
493 } else if (Section->Type == EFI_SECTION_COMPRESSION) {\r
494 Status = PeiServicesLocatePpi (&gEfiPeiDecompressPpiGuid, 0, NULL, (VOID **) &DecompressPpi);\r
495 if (!EFI_ERROR (Status)) {\r
496 Status = DecompressPpi->Decompress (\r
497 DecompressPpi,\r
498 (CONST EFI_COMPRESSION_SECTION*) Section,\r
499 &PpiOutput,\r
500 &PpiOutputSize\r
501 );\r
502 }\r
503 }\r
504 \r
505 if (!EFI_ERROR (Status)) {\r
506 //\r
507 // Update cache section data.\r
508 //\r
509 if (PrivateData->CacheSection.AllSectionCount < CACHE_SETION_MAX_NUMBER) {\r
510 PrivateData->CacheSection.AllSectionCount ++;\r
511 }\r
512 PrivateData->CacheSection.Section [PrivateData->CacheSection.SectionIndex] = Section;\r
513 PrivateData->CacheSection.SectionData [PrivateData->CacheSection.SectionIndex] = PpiOutput;\r
514 PrivateData->CacheSection.SectionSize [PrivateData->CacheSection.SectionIndex] = PpiOutputSize;\r
515 PrivateData->CacheSection.SectionIndex = (PrivateData->CacheSection.SectionIndex + 1)%CACHE_SETION_MAX_NUMBER;\r
516 \r
517 return ProcessSection (\r
518 PeiServices,\r
519 SectionType, \r
520 PpiOutput, \r
521 PpiOutputSize, \r
522 OutputBuffer \r
523 );\r
524 }\r
525 }\r
526\r
527 //\r
528 // Size is 24 bits wide so mask upper 8 bits. \r
529 // SectionLength is adjusted it is 4 byte aligned.\r
530 // Go to the next section\r
531 //\r
532 SectionLength = *(UINT32 *)Section->Size & 0x00FFFFFF;\r
533 SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);\r
534 ASSERT (SectionLength != 0);\r
535 ParsedLength += SectionLength;\r
536 Section = (EFI_COMMON_SECTION_HEADER *)((UINT8 *)Section + SectionLength);\r
537 }\r
538 \r
539 return EFI_NOT_FOUND;\r
540}\r
541\r
542\r
543/**\r
544 Searches for the next matching section within the specified file.\r
545\r
546 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
547 @param SectionType Filter to find only sections of this type.\r
548 @param FileHandle Pointer to the current file to search.\r
549 @param SectionData A pointer to the discovered section, if successful.\r
550 NULL if section not found\r
551\r
552 @retval EFI_NOT_FOUND The section was not found.\r
553 @retval EFI_SUCCESS The section was found.\r
554\r
555**/\r
556EFI_STATUS\r
557EFIAPI\r
558PeiFfsFindSectionData (\r
559 IN CONST EFI_PEI_SERVICES **PeiServices,\r
560 IN EFI_SECTION_TYPE SectionType,\r
561 IN EFI_PEI_FILE_HANDLE FileHandle,\r
562 OUT VOID **SectionData\r
563 )\r
564{\r
565 PEI_CORE_FV_HANDLE *CoreFvHandle;\r
566 \r
567 CoreFvHandle = FileHandleToVolume (FileHandle);\r
568 if ((CoreFvHandle == NULL) || (CoreFvHandle->FvPpi == NULL)) {\r
569 return EFI_NOT_FOUND;\r
570 }\r
571 \r
572 return CoreFvHandle->FvPpi->FindSectionByType (CoreFvHandle->FvPpi, SectionType, FileHandle, SectionData);\r
573}\r
574\r
575/**\r
576 Searches for the next matching file in the firmware volume.\r
577\r
578 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
579 @param SearchType Filter to find only files of this type.\r
580 Type EFI_FV_FILETYPE_ALL causes no filtering to be done.\r
581 @param VolumeHandle Handle of firmware volume in which to search.\r
582 @param FileHandle On entry, points to the current handle from which to begin searching or NULL to start\r
583 at the beginning of the firmware volume. On exit, points the file handle of the next file\r
584 in the volume or NULL if there are no more files.\r
585\r
586 @retval EFI_NOT_FOUND The file was not found.\r
587 @retval EFI_NOT_FOUND The header checksum was not zero.\r
588 @retval EFI_SUCCESS The file was found.\r
589\r
590**/\r
591EFI_STATUS\r
592EFIAPI\r
593PeiFfsFindNextFile (\r
594 IN CONST EFI_PEI_SERVICES **PeiServices,\r
595 IN UINT8 SearchType,\r
596 IN EFI_PEI_FV_HANDLE FvHandle,\r
597 IN OUT EFI_PEI_FILE_HANDLE *FileHandle\r
598 )\r
599{\r
600 PEI_CORE_FV_HANDLE *CoreFvHandle;\r
601 \r
602 CoreFvHandle = FvHandleToCoreHandle (FvHandle);\r
603 \r
604 if ((CoreFvHandle == NULL) || CoreFvHandle->FvPpi == NULL) {\r
605 return EFI_NOT_FOUND;\r
606 }\r
607 \r
608 return CoreFvHandle->FvPpi->FindFileByType (CoreFvHandle->FvPpi, SearchType, FvHandle, FileHandle);\r
609}\r
610\r
611\r
612/**\r
613 Search the firmware volumes by index\r
614\r
615 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
616 @param Instance This instance of the firmware volume to find. The value 0 is the Boot Firmware\r
617 Volume (BFV).\r
618 @param VolumeHandle On exit, points to the next volume handle or NULL if it does not exist.\r
619\r
620 @retval EFI_INVALID_PARAMETER VolumeHandle is NULL\r
621 @retval EFI_NOT_FOUND The volume was not found.\r
622 @retval EFI_SUCCESS The volume was found.\r
623\r
624**/\r
625EFI_STATUS \r
626EFIAPI\r
627PeiFfsFindNextVolume (\r
628 IN CONST EFI_PEI_SERVICES **PeiServices,\r
629 IN UINTN Instance,\r
630 IN OUT EFI_PEI_FV_HANDLE *VolumeHandle\r
631 )\r
632{\r
633 PEI_CORE_INSTANCE *Private;\r
634 PEI_CORE_FV_HANDLE *CoreFvHandle;\r
635 \r
636 Private = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
637 \r
638 CoreFvHandle = FindNextCoreFvHandle (Private, Instance);\r
639 if (CoreFvHandle == NULL) {\r
640 *VolumeHandle = NULL;\r
641 return EFI_NOT_FOUND;\r
642 }\r
643 \r
644 *VolumeHandle = CoreFvHandle->FvHandle;\r
645 \r
646 return EFI_SUCCESS;\r
647}\r
648\r
649\r
650/**\r
651 Find a file within a volume by its name.\r
652\r
653 @param FileName A pointer to the name of the file to find within the firmware volume.\r
654 @param VolumeHandle The firmware volume to search\r
655 @param FileHandle Upon exit, points to the found file's handle \r
656 or NULL if it could not be found.\r
657\r
658 @retval EFI_SUCCESS File was found.\r
659 @retval EFI_NOT_FOUND File was not found.\r
660 @retval EFI_INVALID_PARAMETER VolumeHandle or FileHandle or FileName was NULL.\r
661\r
662**/\r
663EFI_STATUS\r
664EFIAPI \r
665PeiFfsFindFileByName (\r
666 IN CONST EFI_GUID *FileName,\r
667 IN EFI_PEI_FV_HANDLE VolumeHandle,\r
668 OUT EFI_PEI_FILE_HANDLE *FileHandle\r
669 )\r
670{\r
671 PEI_CORE_FV_HANDLE *CoreFvHandle;\r
672 \r
673 if ((VolumeHandle == NULL) || (FileName == NULL) || (FileHandle == NULL)) {\r
674 return EFI_INVALID_PARAMETER;\r
675 }\r
676 \r
677 CoreFvHandle = FvHandleToCoreHandle (VolumeHandle);\r
678 if ((CoreFvHandle == NULL) || (CoreFvHandle->FvPpi == NULL)) {\r
679 return EFI_NOT_FOUND;\r
680 }\r
681 \r
682 return CoreFvHandle->FvPpi->FindFileByName (CoreFvHandle->FvPpi, FileName, &VolumeHandle, FileHandle);\r
683}\r
684\r
685/**\r
686 Returns information about a specific file.\r
687\r
688 @param FileHandle Handle of the file.\r
689 @param FileInfo Upon exit, points to the file’s information.\r
690\r
691 @retval EFI_INVALID_PARAMETER If FileInfo is NULL.\r
692 @retval EFI_INVALID_PARAMETER If FileHandle does not represent a valid file.\r
693 @retval EFI_SUCCESS File information returned.\r
694\r
695**/\r
696EFI_STATUS\r
697EFIAPI \r
698PeiFfsGetFileInfo (\r
699 IN EFI_PEI_FILE_HANDLE FileHandle,\r
700 OUT EFI_FV_FILE_INFO *FileInfo\r
701 )\r
702{\r
703 PEI_CORE_FV_HANDLE *CoreFvHandle;\r
704 \r
705 if ((FileHandle == NULL) || (FileInfo == NULL)) {\r
706 return EFI_INVALID_PARAMETER;\r
707 }\r
708\r
709 //\r
710 // Retrieve the FirmwareVolume which the file resides in.\r
711 //\r
712 CoreFvHandle = FileHandleToVolume (FileHandle);\r
713 if ((CoreFvHandle == NULL) || (CoreFvHandle->FvPpi == NULL)) {\r
714 return EFI_INVALID_PARAMETER;\r
715 }\r
716\r
717 return CoreFvHandle->FvPpi->GetFileInfo (CoreFvHandle->FvPpi, FileHandle, FileInfo);\r
718}\r
719\r
720\r
721/**\r
722 Returns information about the specified volume.\r
723\r
724 @param VolumeHandle Handle of the volume.\r
725 @param VolumeInfo Upon exit, points to the volume’s information.\r
726\r
727 @retval EFI_INVALID_PARAMETER If VolumeHandle does not represent a valid volume.\r
728 @retval EFI_INVALID_PARAMETER If VolumeInfo is NULL.\r
729 @retval EFI_SUCCESS Volume information returned.\r
730**/\r
731EFI_STATUS\r
732EFIAPI \r
733PeiFfsGetVolumeInfo (\r
734 IN EFI_PEI_FV_HANDLE VolumeHandle,\r
735 OUT EFI_FV_INFO *VolumeInfo\r
736 )\r
737{\r
738 PEI_CORE_FV_HANDLE *CoreHandle;\r
739 \r
740 if (VolumeInfo == NULL) {\r
741 return EFI_INVALID_PARAMETER;\r
742 }\r
743 \r
744 CoreHandle = FvHandleToCoreHandle (VolumeHandle);\r
745 \r
746 if ((CoreHandle == NULL) || (CoreHandle->FvPpi == NULL)) {\r
747 return EFI_INVALID_PARAMETER;\r
748 }\r
749 \r
750 return CoreHandle->FvPpi->GetVolumeInfo (CoreHandle->FvPpi, VolumeHandle, VolumeInfo);\r
751}\r
752\r
753/**\r
754 Get Fv image from the FV type file, then install FV INFO ppi, Build FV hob.\r
755\r
756 @param ParentFvCoreHandle Pointer of EFI_CORE_FV_HANDLE to parent Fv image that contain this Fv image.\r
757 @param ParentFvFileHandle File handle of a Fv type file that contain this Fv image.\r
758\r
759 @retval EFI_NOT_FOUND FV image can't be found.\r
760 @retval EFI_SUCCESS Successfully to process it.\r
761 @retval EFI_OUT_OF_RESOURCES Can not allocate page when aligning FV image\r
762 @retval Others Can not find EFI_SECTION_FIRMWARE_VOLUME_IMAGE section\r
763 \r
764**/\r
765EFI_STATUS\r
766ProcessFvFile (\r
767 IN PEI_CORE_FV_HANDLE *ParentFvCoreHandle,\r
768 IN EFI_PEI_FILE_HANDLE ParentFvFileHandle\r
769 )\r
770{\r
771 EFI_STATUS Status;\r
772 EFI_FV_INFO ParentFvImageInfo;\r
773 UINT32 FvAlignment;\r
774 VOID *NewFvBuffer;\r
775 EFI_PEI_HOB_POINTERS HobPtr;\r
776 EFI_PEI_FIRMWARE_VOLUME_PPI *ParentFvPpi;\r
777 EFI_PEI_FV_HANDLE ParentFvHandle;\r
778 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;\r
779 EFI_FV_FILE_INFO FileInfo;\r
780 \r
781 //\r
782 // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has already\r
783 // been extracted.\r
784 //\r
785 HobPtr.Raw = GetHobList ();\r
786 while ((HobPtr.Raw = GetNextHob (EFI_HOB_TYPE_FV2, HobPtr.Raw)) != NULL) {\r
787 if (CompareGuid (&(((EFI_FFS_FILE_HEADER *)ParentFvFileHandle)->Name), &HobPtr.FirmwareVolume2->FileName)) {\r
788 //\r
789 // this FILE has been dispatched, it will not be dispatched again.\r
790 //\r
791 DEBUG ((EFI_D_INFO, "FV file %p has been dispatched!\r\n", ParentFvFileHandle));\r
792 return EFI_SUCCESS;\r
793 }\r
794 HobPtr.Raw = GET_NEXT_HOB (HobPtr);\r
795 }\r
796\r
797 ParentFvHandle = ParentFvCoreHandle->FvHandle;\r
798 ParentFvPpi = ParentFvCoreHandle->FvPpi;\r
799 \r
800 //\r
801 // Find FvImage in FvFile\r
802 //\r
803 Status = ParentFvPpi->FindSectionByType (\r
804 ParentFvPpi,\r
805 EFI_SECTION_FIRMWARE_VOLUME_IMAGE,\r
806 ParentFvFileHandle,\r
807 (VOID **)&FvHeader\r
808 );\r
809 \r
810 if (EFI_ERROR (Status)) {\r
811 return Status;\r
812 }\r
813\r
814 //\r
815 // FvAlignment must be more than 8 bytes required by FvHeader structure.\r
816 //\r
817 FvAlignment = 1 << ((FvHeader->Attributes & EFI_FVB2_ALIGNMENT) >> 16);\r
818 if (FvAlignment < 8) {\r
819 FvAlignment = 8;\r
820 }\r
821 \r
822 //\r
823 // Check FvImage\r
824 //\r
825 if ((UINTN) FvHeader % FvAlignment != 0) {\r
826 NewFvBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES ((UINT32) FvHeader->FvLength), FvAlignment);\r
827 if (NewFvBuffer == NULL) {\r
828 return EFI_OUT_OF_RESOURCES;\r
829 }\r
830 CopyMem (NewFvBuffer, FvHeader, (UINTN) FvHeader->FvLength);\r
831 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER*) NewFvBuffer;\r
832 }\r
833 \r
834 Status = ParentFvPpi->GetVolumeInfo (ParentFvPpi, ParentFvHandle, &ParentFvImageInfo);\r
835 ASSERT_EFI_ERROR (Status);\r
836 \r
837 Status = ParentFvPpi->GetFileInfo (ParentFvPpi, ParentFvFileHandle, &FileInfo);\r
838 ASSERT_EFI_ERROR (Status);\r
839 \r
840 //\r
841 // Install FvPpi and Build FvHob\r
842 //\r
843 PeiServicesInstallFvInfoPpi (\r
844 &FvHeader->FileSystemGuid,\r
845 (VOID**) FvHeader,\r
846 (UINT32) FvHeader->FvLength,\r
847 &ParentFvImageInfo.FvName,\r
848 &FileInfo.FileName\r
849 );\r
850\r
851 //\r
852 // Inform the extracted FvImage to Fv HOB consumer phase, i.e. DXE phase\r
853 //\r
854 BuildFvHob (\r
855 (EFI_PHYSICAL_ADDRESS) (UINTN) FvHeader,\r
856 FvHeader->FvLength\r
857 );\r
858\r
859 //\r
860 // Makes the encapsulated volume show up in DXE phase to skip processing of\r
861 // encapsulated file again.\r
862 //\r
863 BuildFv2Hob (\r
864 (EFI_PHYSICAL_ADDRESS) (UINTN) FvHeader,\r
865 FvHeader->FvLength,\r
866 &ParentFvImageInfo.FvName,\r
867 &FileInfo.FileName\r
868 );\r
869\r
870 return EFI_SUCCESS;\r
871}\r
872\r
873/**\r
874 Process a firmware volume and create a volume handle.\r
875\r
876 Create a volume handle from the information in the buffer. For\r
877 memory-mapped firmware volumes, Buffer and BufferSize refer to\r
878 the start of the firmware volume and the firmware volume size.\r
879 For non memory-mapped firmware volumes, this points to a\r
880 buffer which contains the necessary information for creating\r
881 the firmware volume handle. Normally, these values are derived\r
882 from the EFI_FIRMWARE_VOLUME_INFO_PPI.\r
883 \r
884 \r
885 @param This Points to this instance of the\r
886 EFI_PEI_FIRMWARE_VOLUME_PPI.\r
887 @param Buffer Points to the start of the buffer.\r
888 @param BufferSize Size of the buffer.\r
889 @param FvHandle Points to the returned firmware volume\r
890 handle. The firmware volume handle must\r
891 be unique within the system. \r
892\r
893 @retval EFI_SUCCESS Firmware volume handle created.\r
894 @retval EFI_VOLUME_CORRUPTED Volume was corrupt.\r
895\r
896**/\r
897EFI_STATUS\r
898EFIAPI\r
899PeiFfs2FvPpiProcessVolume (\r
900 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,\r
901 IN VOID *Buffer,\r
902 IN UINTN BufferSize,\r
903 OUT EFI_PEI_FV_HANDLE *FvHandle\r
904 )\r
905{\r
906 EFI_STATUS Status;\r
907 PEI_CORE_INSTANCE *PrivateData;\r
908 EFI_PEI_FILE_HANDLE FileHandle;\r
909 VOID *DepexData;\r
910 EFI_PEI_SERVICES **PeiServices;\r
911 UINTN FvIndex;\r
912 \r
913 PeiServices = (EFI_PEI_SERVICES**) GetPeiServicesTablePointer ();\r
914 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
915 \r
916 //\r
917 // The build-in EFI_PEI_FIRMWARE_VOLUME_PPI for FFS2 support memory-mapped\r
918 // FV image and the handle is pointed to Fv image's buffer.\r
919 //\r
920 *FvHandle = (EFI_PEI_FV_HANDLE) Buffer;\r
921 \r
922 //\r
923 // Do verify for given FV buffer.\r
924 //\r
925 Status = VerifyFv ((EFI_FIRMWARE_VOLUME_HEADER*) Buffer);\r
926 if (EFI_ERROR(Status)) {\r
927 DEBUG ((EFI_D_ERROR, "Fail to verify FV which address is 0x%11p", Buffer));\r
928 return EFI_VOLUME_CORRUPTED;\r
929 }\r
930\r
931 //\r
932 // Check whether the FV has already been processed.\r
933 //\r
934 for (FvIndex = 0; FvIndex < PrivateData->FvCount; FvIndex ++) {\r
935 if (PrivateData->Fv[FvIndex].FvHandle == *FvHandle) {\r
936 DEBUG ((EFI_D_INFO, "The Fv %p has already been processed!", Buffer));\r
937 return EFI_SUCCESS;\r
938 }\r
939 }\r
940 \r
941 //\r
942 // Update internal PEI_CORE_FV array.\r
943 //\r
944 PrivateData->Fv[PrivateData->FvCount].FvHeader = (EFI_FIRMWARE_VOLUME_HEADER*) Buffer;\r
945 PrivateData->Fv[PrivateData->FvCount].FvPpi = (EFI_PEI_FIRMWARE_VOLUME_PPI*) This;\r
946 PrivateData->Fv[PrivateData->FvCount].FvHandle = *FvHandle;\r
947 \r
948 DEBUG ((EFI_D_INFO, \r
949 "The %dth FV start address is 0x%11p and size is 0x%08x\n", \r
950 (UINT32) PrivateData->FvCount, \r
951 (VOID *) Buffer, \r
952 BufferSize\r
953 ));\r
954 PrivateData->FvCount ++;\r
955 \r
956 do {\r
957 Status = This->FindFileByType (\r
958 This,\r
959 EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE,\r
960 *FvHandle,\r
961 &FileHandle\r
962 );\r
963 if (!EFI_ERROR (Status)) {\r
964 Status = This->FindSectionByType (\r
965 This,\r
966 EFI_SECTION_PEI_DEPEX,\r
967 FileHandle,\r
968 (VOID**)&DepexData\r
969 );\r
970 if (!EFI_ERROR (Status)) {\r
971 if (!PeimDispatchReadiness (PeiServices, DepexData)) {\r
972 //\r
973 // Dependency is not satisfied.\r
974 //\r
975 continue;\r
976 }\r
977 }\r
978 \r
979 DEBUG ((EFI_D_INFO, "Found firmware volume Image File %p in FV[%d] %p", FileHandle, PrivateData->FvCount - 1, *FvHandle));\r
980 ProcessFvFile (&PrivateData->Fv[PrivateData->FvCount - 1], FileHandle);\r
981 }\r
982 } while (FileHandle != NULL);\r
983\r
984 return EFI_SUCCESS;\r
985} \r
986\r
987/**\r
988 Finds the next file of the specified type.\r
989\r
990 This service enables PEI modules to discover additional firmware files. \r
991 The FileHandle must be unique within the system.\r
992\r
993 @param This Points to this instance of the\r
994 EFI_PEI_FIRMWARE_VOLUME_PPI.\r
995 @param SearchType A filter to find only files of this type. Type\r
996 EFI_FV_FILETYPE_ALL causes no filtering to be\r
997 done.\r
998 @param FvHandle Handle of firmware volume in which to\r
999 search.\r
1000 @param FileHandle Points to the current handle from which to\r
1001 begin searching or NULL to start at the\r
1002 beginning of the firmware volume. Updated\r
1003 upon return to reflect the file found.\r
1004\r
1005 @retval EFI_SUCCESS The file was found.\r
1006 @retval EFI_NOT_FOUND The file was not found. FileHandle contains NULL.\r
1007\r
1008**/\r
1009EFI_STATUS\r
1010EFIAPI\r
1011PeiFfs2FvPpiFindFileByType (\r
1012 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,\r
1013 IN EFI_FV_FILETYPE SearchType,\r
1014 IN EFI_PEI_FV_HANDLE FvHandle,\r
1015 IN OUT EFI_PEI_FILE_HANDLE *FileHandle\r
1016 )\r
1017{ \r
1018 return FindFileEx (FvHandle, NULL, SearchType, FileHandle, NULL);\r
1019}\r
1020\r
1021/**\r
1022 Find a file within a volume by its name. \r
1023 \r
1024 This service searches for files with a specific name, within\r
1025 either the specified firmware volume or all firmware volumes.\r
1026\r
1027 @param This Points to this instance of the\r
1028 EFI_PEI_FIRMWARE_VOLUME_PPI.\r
1029 @param FileName A pointer to the name of the file to find\r
1030 within the firmware volume.\r
1031 @param FvHandle Upon entry, the pointer to the firmware\r
1032 volume to search or NULL if all firmware\r
1033 volumes should be searched. Upon exit, the\r
1034 actual firmware volume in which the file was\r
1035 found.\r
1036 @param FileHandle Upon exit, points to the found file's\r
1037 handle or NULL if it could not be found.\r
1038\r
1039 @retval EFI_SUCCESS File was found.\r
1040 @retval EFI_NOT_FOUND File was not found.\r
1041 @retval EFI_INVALID_PARAMETER FvHandle or FileHandle or\r
1042 FileName was NULL.\r
1043\r
1044\r
1045**/\r
1046EFI_STATUS\r
1047EFIAPI\r
1048PeiFfs2FvPpiFindFileByName (\r
1049 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,\r
1050 IN CONST EFI_GUID *FileName,\r
1051 IN EFI_PEI_FV_HANDLE *FvHandle,\r
1052 OUT EFI_PEI_FILE_HANDLE *FileHandle \r
1053 )\r
1054{\r
1055 EFI_STATUS Status;\r
1056 PEI_CORE_INSTANCE *PrivateData;\r
1057 UINTN Index;\r
1058 \r
1059 if ((FvHandle == NULL) || (FileName == NULL) || (FileHandle == NULL)) {\r
1060 return EFI_INVALID_PARAMETER;\r
1061 }\r
1062 \r
1063 if (*FvHandle != NULL) {\r
1064 Status = FindFileEx (*FvHandle, FileName, 0, FileHandle, NULL);\r
1065 if (Status == EFI_NOT_FOUND) {\r
1066 *FileHandle = NULL;\r
1067 }\r
1068 } else { \r
1069 //\r
1070 // If *FvHandle = NULL, so search all FV for given filename\r
1071 //\r
1072 Status = EFI_NOT_FOUND;\r
1073 \r
1074 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer());\r
1075 for (Index = 0; Index < PrivateData->FvCount; Index ++) {\r
1076 //\r
1077 // Only search the FV which is associated with a EFI_PEI_FIRMWARE_VOLUME_PPI instance.\r
1078 //\r
1079 if (PrivateData->Fv[Index].FvPpi != NULL) {\r
1080 Status = FindFileEx (PrivateData->Fv[Index].FvHandle, FileName, 0, FileHandle, NULL);\r
1081 if (!EFI_ERROR (Status)) {\r
1082 *FvHandle = PrivateData->Fv[Index].FvHandle;\r
1083 }\r
1084 }\r
1085 }\r
1086 }\r
1087 \r
1088 return Status; \r
1089} \r
1090\r
1091/**\r
1092 Returns information about a specific file.\r
1093\r
1094 This function returns information about a specific\r
1095 file, including its file name, type, attributes, starting\r
1096 address and size. \r
1097 \r
1098 @param This Points to this instance of the\r
1099 EFI_PEI_FIRMWARE_VOLUME_PPI.\r
1100 @param FileHandle Handle of the file.\r
1101 @param FileInfo Upon exit, points to the file's\r
1102 information.\r
1103\r
1104 @retval EFI_SUCCESS File information returned.\r
1105 @retval EFI_INVALID_PARAMETER If FileHandle does not\r
1106 represent a valid file.\r
1107 @retval EFI_INVALID_PARAMETER If FileInfo is NULL.\r
1108 \r
1109**/ \r
1110EFI_STATUS\r
1111EFIAPI\r
1112PeiFfs2FvPpiGetFileInfo (\r
1113 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This, \r
1114 IN EFI_PEI_FILE_HANDLE FileHandle, \r
1115 OUT EFI_FV_FILE_INFO *FileInfo\r
1116 )\r
1117{\r
1118 UINT8 FileState;\r
1119 UINT8 ErasePolarity;\r
1120 EFI_FFS_FILE_HEADER *FileHeader;\r
1121 PEI_CORE_FV_HANDLE *CoreFvHandle;\r
1122 \r
1123 if ((FileHandle == NULL) || (FileInfo == NULL)) {\r
1124 return EFI_INVALID_PARAMETER;\r
1125 }\r
1126\r
1127 //\r
1128 // Retrieve the FirmwareVolume which the file resides in.\r
1129 //\r
1130 CoreFvHandle = FileHandleToVolume (FileHandle);\r
1131 if (CoreFvHandle == NULL) {\r
1132 return EFI_INVALID_PARAMETER;\r
1133 }\r
1134\r
1135 if (CoreFvHandle->FvHeader->Attributes & EFI_FVB2_ERASE_POLARITY) {\r
1136 ErasePolarity = 1;\r
1137 } else {\r
1138 ErasePolarity = 0;\r
1139 }\r
1140\r
1141 //\r
1142 // Get FileState which is the highest bit of the State \r
1143 //\r
1144 FileState = GetFileState (ErasePolarity, (EFI_FFS_FILE_HEADER*)FileHandle);\r
1145\r
1146 switch (FileState) {\r
1147 case EFI_FILE_DATA_VALID:\r
1148 case EFI_FILE_MARKED_FOR_UPDATE:\r
1149 break; \r
1150 default:\r
1151 return EFI_INVALID_PARAMETER;\r
1152 }\r
1153\r
1154 FileHeader = (EFI_FFS_FILE_HEADER *)FileHandle;\r
1155 CopyMem (&FileInfo->FileName, &FileHeader->Name, sizeof(EFI_GUID));\r
1156 FileInfo->FileType = FileHeader->Type;\r
1157 FileInfo->FileAttributes = FileHeader->Attributes;\r
1158 FileInfo->BufferSize = ((*(UINT32 *)FileHeader->Size) & 0x00FFFFFF) - sizeof (EFI_FFS_FILE_HEADER);\r
1159 FileInfo->Buffer = (FileHeader + 1);\r
1160 return EFI_SUCCESS; \r
1161} \r
1162 \r
1163/**\r
1164 This function returns information about the firmware volume.\r
1165 \r
1166 @param This Points to this instance of the\r
1167 EFI_PEI_FIRMWARE_VOLUME_PPI.\r
1168 @param FvHandle Handle to the firmware handle.\r
1169 @param VolumeInfo Points to the returned firmware volume\r
1170 information.\r
1171\r
1172 @retval EFI_SUCCESS Information returned successfully.\r
1173 @retval EFI_INVALID_PARAMETER FvHandle does not indicate a valid\r
1174 firmware volume or VolumeInfo is NULL.\r
1175\r
1176**/ \r
1177EFI_STATUS\r
1178EFIAPI\r
1179PeiFfs2FvPpiGetVolumeInfo (\r
1180 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This, \r
1181 IN EFI_PEI_FV_HANDLE FvHandle, \r
1182 OUT EFI_FV_INFO *VolumeInfo\r
1183 )\r
1184{\r
1185 EFI_FIRMWARE_VOLUME_HEADER FwVolHeader;\r
1186 EFI_FIRMWARE_VOLUME_EXT_HEADER *FwVolExHeaderInfo;\r
1187\r
1188 if (VolumeInfo == NULL) {\r
1189 return EFI_INVALID_PARAMETER;\r
1190 }\r
1191 \r
1192 //\r
1193 // VolumeHandle may not align at 8 byte, \r
1194 // but FvLength is UINT64 type, which requires FvHeader align at least 8 byte. \r
1195 // So, Copy FvHeader into the local FvHeader structure.\r
1196 //\r
1197 CopyMem (&FwVolHeader, FvHandle, sizeof (EFI_FIRMWARE_VOLUME_HEADER));\r
1198\r
1199 //\r
1200 // Check Fv Image Signature\r
1201 //\r
1202 if (FwVolHeader.Signature != EFI_FVH_SIGNATURE) {\r
1203 return EFI_INVALID_PARAMETER;\r
1204 }\r
1205\r
1206 ZeroMem (VolumeInfo, sizeof (EFI_FV_INFO));\r
1207 VolumeInfo->FvAttributes = FwVolHeader.Attributes;\r
1208 VolumeInfo->FvStart = (VOID *) FvHandle;\r
1209 VolumeInfo->FvSize = FwVolHeader.FvLength;\r
1210 CopyMem (&VolumeInfo->FvFormat, &FwVolHeader.FileSystemGuid, sizeof(EFI_GUID));\r
1211\r
1212 if (FwVolHeader.ExtHeaderOffset != 0) {\r
1213 FwVolExHeaderInfo = (EFI_FIRMWARE_VOLUME_EXT_HEADER*)(((UINT8 *)FvHandle) + FwVolHeader.ExtHeaderOffset);\r
1214 CopyMem (&VolumeInfo->FvName, &FwVolExHeaderInfo->FvName, sizeof(EFI_GUID));\r
1215 }\r
1216 \r
1217 return EFI_SUCCESS; \r
1218} \r
1219\r
1220/**\r
1221 Find the next matching section in the firmware file.\r
1222 \r
1223 This service enables PEI modules to discover sections\r
1224 of a given type within a valid file.\r
1225 \r
1226 @param This Points to this instance of the\r
1227 EFI_PEI_FIRMWARE_VOLUME_PPI.\r
1228 @param SearchType A filter to find only sections of this\r
1229 type.\r
1230 @param FileHandle Handle of firmware file in which to\r
1231 search.\r
1232 @param SectionData Updated upon return to point to the\r
1233 section found.\r
1234 \r
1235 @retval EFI_SUCCESS Section was found.\r
1236 @retval EFI_NOT_FOUND Section of the specified type was not\r
1237 found. SectionData contains NULL.\r
1238**/\r
1239EFI_STATUS\r
1240EFIAPI\r
1241PeiFfs2FvPpiFindSectionByType (\r
1242 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,\r
1243 IN EFI_SECTION_TYPE SearchType,\r
1244 IN EFI_PEI_FILE_HANDLE FileHandle,\r
1245 OUT VOID **SectionData\r
1246 )\r
1247{\r
1248 EFI_FFS_FILE_HEADER *FfsFileHeader;\r
1249 UINT32 FileSize;\r
1250 EFI_COMMON_SECTION_HEADER *Section;\r
1251 \r
1252 FfsFileHeader = (EFI_FFS_FILE_HEADER *)(FileHandle);\r
1253\r
1254 //\r
1255 // Size is 24 bits wide so mask upper 8 bits. \r
1256 // Does not include FfsFileHeader header size\r
1257 // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.\r
1258 //\r
1259 Section = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);\r
1260 FileSize = *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF;\r
1261 FileSize -= sizeof (EFI_FFS_FILE_HEADER);\r
1262\r
1263 return ProcessSection (\r
1264 GetPeiServicesTablePointer (),\r
1265 SearchType, \r
1266 Section, \r
1267 FileSize, \r
1268 SectionData\r
1269 ); \r
1270} \r
1271\r
1272/**\r
1273 Convert the handle of FV to pointer of corresponding PEI_CORE_FV_HANDLE.\r
1274 \r
1275 @param FvHandle The handle of a FV.\r
1276 \r
1277 @retval NULL if can not find.\r
1278 @return Pointer of corresponding PEI_CORE_FV_HANDLE. \r
1279**/\r
1280PEI_CORE_FV_HANDLE *\r
1281FvHandleToCoreHandle (\r
1282 IN EFI_PEI_FV_HANDLE FvHandle\r
1283 )\r
1284{\r
1285 UINTN Index;\r
1286 PEI_CORE_INSTANCE *PrivateData;\r
1287 \r
1288 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer()); \r
1289 for (Index = 0; Index < PrivateData->FvCount; Index ++) {\r
1290 if (FvHandle == PrivateData->Fv[Index].FvHandle) {\r
1291 return &PrivateData->Fv[Index];\r
1292 }\r
1293 }\r
1294 \r
1295 return NULL;\r
1296} \r
1297\r
1298/**\r
1299 Get instance of PEI_CORE_FV_HANDLE for next volume according to given index.\r
1300 \r
1301 This routine also will install FvInfo ppi for FV hob in PI ways.\r
1302 \r
1303 @param Private Pointer of PEI_CORE_INSTANCE\r
1304 @param Instance The index of FV want to be searched.\r
1305 \r
1306 @return Instance of PEI_CORE_FV_HANDLE.\r
1307**/\r
1308PEI_CORE_FV_HANDLE *\r
1309FindNextCoreFvHandle (\r
1310 IN PEI_CORE_INSTANCE *Private,\r
1311 IN UINTN Instance\r
1312 )\r
1313{\r
1314 UINTN Index;\r
1315 BOOLEAN Match;\r
1316 EFI_HOB_FIRMWARE_VOLUME *FvHob;\r
1317 \r
1318 //\r
1319 // Handle Framework FvHob and Install FvInfo Ppi for it.\r
1320 //\r
1321 if (FeaturePcdGet (PcdFrameworkCompatibilitySupport)) {\r
1322 //\r
1323 // Loop to search the wanted FirmwareVolume which supports FFS\r
1324 //\r
1325 FvHob = (EFI_HOB_FIRMWARE_VOLUME *)GetFirstHob (EFI_HOB_TYPE_FV);\r
1326 while (FvHob != NULL) {\r
1327 for (Index = 0, Match = FALSE; Index < Private->FvCount; Index++) {\r
1328 if ((EFI_PEI_FV_HANDLE)(UINTN)FvHob->BaseAddress == Private->Fv[Index].FvHeader) {\r
1329 Match = TRUE;\r
1330 break;\r
1331 }\r
1332 }\r
1333 //\r
1334 // If Not Found, Install FvInfo Ppi for it.\r
1335 //\r
1336 if (!Match) {\r
1337 PeiServicesInstallFvInfoPpi (\r
1338 &(((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)FvHob->BaseAddress)->FileSystemGuid),\r
1339 (VOID *)(UINTN)FvHob->BaseAddress,\r
1340 (UINT32)FvHob->Length,\r
1341 NULL,\r
1342 NULL\r
1343 );\r
1344 }\r
1345 FvHob = (EFI_HOB_FIRMWARE_VOLUME *)GetNextHob (EFI_HOB_TYPE_FV, (VOID *)((UINTN)FvHob + FvHob->Header.HobLength)); \r
1346 }\r
1347 }\r
1348\r
1349 if (Instance >= Private->FvCount) {\r
1350 return NULL;\r
1351 }\r
1352\r
1353 return &Private->Fv[Instance];\r
1354} \r