]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Pei/FwVol/FwVol.c
1. According to PI errata 0000840 and PI 1.2c Vol 3 3.2.3, remove description rows...
[mirror_edk2.git] / MdeModulePkg / Core / Pei / FwVol / FwVol.c
1 /** @file
2 Pei Core Firmware File System service routines.
3
4 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "FwVol.h"
16
17 EFI_PEI_NOTIFY_DESCRIPTOR mNotifyOnFvInfoList = {
18 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
19 &gEfiPeiFirmwareVolumeInfoPpiGuid,
20 FirmwareVolmeInfoPpiNotifyCallback
21 };
22
23 PEI_FW_VOL_INSTANCE mPeiFfs2FwVol = {
24 PEI_FW_VOL_SIGNATURE,
25 FALSE,
26 {
27 PeiFfsFvPpiProcessVolume,
28 PeiFfsFvPpiFindFileByType,
29 PeiFfsFvPpiFindFileByName,
30 PeiFfsFvPpiGetFileInfo,
31 PeiFfsFvPpiGetVolumeInfo,
32 PeiFfsFvPpiFindSectionByType
33 }
34 };
35
36 PEI_FW_VOL_INSTANCE mPeiFfs3FwVol = {
37 PEI_FW_VOL_SIGNATURE,
38 TRUE,
39 {
40 PeiFfsFvPpiProcessVolume,
41 PeiFfsFvPpiFindFileByType,
42 PeiFfsFvPpiFindFileByName,
43 PeiFfsFvPpiGetFileInfo,
44 PeiFfsFvPpiGetVolumeInfo,
45 PeiFfsFvPpiFindSectionByType
46 }
47 };
48
49 EFI_PEI_PPI_DESCRIPTOR mPeiFfs2FvPpiList = {
50 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
51 &gEfiFirmwareFileSystem2Guid,
52 &mPeiFfs2FwVol.Fv
53 };
54
55 EFI_PEI_PPI_DESCRIPTOR mPeiFfs3FvPpiList = {
56 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
57 &gEfiFirmwareFileSystem3Guid,
58 &mPeiFfs3FwVol.Fv
59 };
60
61 /**
62 Required Alignment Alignment Value in FFS Alignment Value in
63 (bytes) Attributes Field Firmware Volume Interfaces
64 1 0 0
65 16 1 4
66 128 2 7
67 512 3 9
68 1 KB 4 10
69 4 KB 5 12
70 32 KB 6 15
71 64 KB 7 16
72 **/
73 UINT8 mFvAttributes[] = {0, 4, 7, 9, 10, 12, 15, 16};
74
75 /**
76 Convert the FFS File Attributes to FV File Attributes
77
78 @param FfsAttributes The attributes of UINT8 type.
79
80 @return The attributes of EFI_FV_FILE_ATTRIBUTES
81
82 **/
83 EFI_FV_FILE_ATTRIBUTES
84 FfsAttributes2FvFileAttributes (
85 IN EFI_FFS_FILE_ATTRIBUTES FfsAttributes
86 )
87 {
88 UINT8 DataAlignment;
89 EFI_FV_FILE_ATTRIBUTES FileAttribute;
90
91 DataAlignment = (UINT8) ((FfsAttributes & FFS_ATTRIB_DATA_ALIGNMENT) >> 3);
92 ASSERT (DataAlignment < 8);
93
94 FileAttribute = (EFI_FV_FILE_ATTRIBUTES) mFvAttributes[DataAlignment];
95
96 if ((FfsAttributes & FFS_ATTRIB_FIXED) == FFS_ATTRIB_FIXED) {
97 FileAttribute |= EFI_FV_FILE_ATTRIB_FIXED;
98 }
99
100 return FileAttribute;
101 }
102
103 /**
104 Returns the file state set by the highest zero bit in the State field
105
106 @param ErasePolarity Erase Polarity as defined by EFI_FVB2_ERASE_POLARITY
107 in the Attributes field.
108 @param FfsHeader Pointer to FFS File Header.
109
110 @retval EFI_FFS_FILE_STATE File state is set by the highest none zero bit
111 in the header State field.
112 **/
113 EFI_FFS_FILE_STATE
114 GetFileState(
115 IN UINT8 ErasePolarity,
116 IN EFI_FFS_FILE_HEADER *FfsHeader
117 )
118 {
119 EFI_FFS_FILE_STATE FileState;
120 EFI_FFS_FILE_STATE HighestBit;
121
122 FileState = FfsHeader->State;
123
124 if (ErasePolarity != 0) {
125 FileState = (EFI_FFS_FILE_STATE)~FileState;
126 }
127
128 //
129 // Get file state set by its highest none zero bit.
130 //
131 HighestBit = 0x80;
132 while (HighestBit != 0 && (HighestBit & FileState) == 0) {
133 HighestBit >>= 1;
134 }
135
136 return HighestBit;
137 }
138
139 /**
140 Calculates the checksum of the header of a file.
141
142 @param FileHeader Pointer to FFS File Header.
143
144 @return Checksum of the header.
145 Zero means the header is good.
146 Non-zero means the header is bad.
147 **/
148 UINT8
149 CalculateHeaderChecksum (
150 IN EFI_FFS_FILE_HEADER *FileHeader
151 )
152 {
153 EFI_FFS_FILE_HEADER2 TestFileHeader;
154
155 if (IS_FFS_FILE2 (FileHeader)) {
156 CopyMem (&TestFileHeader, FileHeader, sizeof (EFI_FFS_FILE_HEADER2));
157 //
158 // Ingore State and File field in FFS header.
159 //
160 TestFileHeader.State = 0;
161 TestFileHeader.IntegrityCheck.Checksum.File = 0;
162
163 return CalculateSum8 ((CONST UINT8 *) &TestFileHeader, sizeof (EFI_FFS_FILE_HEADER2));
164 } else {
165 CopyMem (&TestFileHeader, FileHeader, sizeof (EFI_FFS_FILE_HEADER));
166 //
167 // Ingore State and File field in FFS header.
168 //
169 TestFileHeader.State = 0;
170 TestFileHeader.IntegrityCheck.Checksum.File = 0;
171
172 return CalculateSum8 ((CONST UINT8 *) &TestFileHeader, sizeof (EFI_FFS_FILE_HEADER));
173 }
174 }
175
176 /**
177 Find FV handler according to FileHandle in that FV.
178
179 @param FileHandle Handle of file image
180
181 @return Pointer to instance of PEI_CORE_FV_HANDLE.
182 **/
183 PEI_CORE_FV_HANDLE*
184 FileHandleToVolume (
185 IN EFI_PEI_FILE_HANDLE FileHandle
186 )
187 {
188 UINTN Index;
189 PEI_CORE_INSTANCE *PrivateData;
190 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
191
192 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());
193
194 for (Index = 0; Index < PrivateData->FvCount; Index++) {
195 FwVolHeader = PrivateData->Fv[Index].FvHeader;
196 if (((UINT64) (UINTN) FileHandle > (UINT64) (UINTN) FwVolHeader ) && \
197 ((UINT64) (UINTN) FileHandle <= ((UINT64) (UINTN) FwVolHeader + FwVolHeader->FvLength - 1))) {
198 return &PrivateData->Fv[Index];
199 }
200 }
201 return NULL;
202 }
203
204 /**
205 Given the input file pointer, search for the first matching file in the
206 FFS volume as defined by SearchType. The search starts from FileHeader inside
207 the Firmware Volume defined by FwVolHeader.
208 If SearchType is EFI_FV_FILETYPE_ALL, the first FFS file will return without check its file type.
209 If SearchType is PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE,
210 the first PEIM, or COMBINED PEIM or FV file type FFS file will return.
211
212 @param FvHandle Pointer to the FV header of the volume to search
213 @param FileName File name
214 @param SearchType Filter to find only files of this type.
215 Type EFI_FV_FILETYPE_ALL causes no filtering to be done.
216 @param FileHandle This parameter must point to a valid FFS volume.
217 @param AprioriFile Pointer to AprioriFile image in this FV if has
218
219 @return EFI_NOT_FOUND No files matching the search criteria were found
220 @retval EFI_SUCCESS Success to search given file
221
222 **/
223 EFI_STATUS
224 FindFileEx (
225 IN CONST EFI_PEI_FV_HANDLE FvHandle,
226 IN CONST EFI_GUID *FileName, OPTIONAL
227 IN EFI_FV_FILETYPE SearchType,
228 IN OUT EFI_PEI_FILE_HANDLE *FileHandle,
229 IN OUT EFI_PEI_FV_HANDLE *AprioriFile OPTIONAL
230 )
231 {
232 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
233 EFI_FFS_FILE_HEADER **FileHeader;
234 EFI_FFS_FILE_HEADER *FfsFileHeader;
235 UINT32 FileLength;
236 UINT32 FileOccupiedSize;
237 UINT32 FileOffset;
238 UINT64 FvLength;
239 UINT8 ErasePolarity;
240 UINT8 FileState;
241 UINT8 DataCheckSum;
242 BOOLEAN IsFfs3Fv;
243
244 //
245 // Convert the handle of FV to FV header for memory-mapped firmware volume
246 //
247 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) FvHandle;
248 FileHeader = (EFI_FFS_FILE_HEADER **)FileHandle;
249
250 IsFfs3Fv = CompareGuid (&FwVolHeader->FileSystemGuid, &gEfiFirmwareFileSystem3Guid);
251
252 FvLength = FwVolHeader->FvLength;
253 if ((FwVolHeader->Attributes & EFI_FVB2_ERASE_POLARITY) != 0) {
254 ErasePolarity = 1;
255 } else {
256 ErasePolarity = 0;
257 }
258
259 //
260 // If FileHeader is not specified (NULL) or FileName is not NULL,
261 // start with the first file in the firmware volume. Otherwise,
262 // start from the FileHeader.
263 //
264 if ((*FileHeader == NULL) || (FileName != NULL)) {
265 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FwVolHeader + FwVolHeader->HeaderLength);
266 } else {
267 if (IS_FFS_FILE2 (*FileHeader)) {
268 if (!IsFfs3Fv) {
269 DEBUG ((EFI_D_ERROR, "It is a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &(*FileHeader)->Name));
270 }
271 FileLength = FFS_FILE2_SIZE (*FileHeader);
272 ASSERT (FileLength > 0x00FFFFFF);
273 } else {
274 FileLength = FFS_FILE_SIZE (*FileHeader);
275 }
276 //
277 // FileLength is adjusted to FileOccupiedSize as it is 8 byte aligned.
278 //
279 FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
280 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)*FileHeader + FileOccupiedSize);
281 }
282
283 FileOffset = (UINT32) ((UINT8 *)FfsFileHeader - (UINT8 *)FwVolHeader);
284 ASSERT (FileOffset <= 0xFFFFFFFF);
285
286 while (FileOffset < (FvLength - sizeof (EFI_FFS_FILE_HEADER))) {
287 //
288 // Get FileState which is the highest bit of the State
289 //
290 FileState = GetFileState (ErasePolarity, FfsFileHeader);
291 switch (FileState) {
292
293 case EFI_FILE_HEADER_CONSTRUCTION:
294 case EFI_FILE_HEADER_INVALID:
295 if (IS_FFS_FILE2 (FfsFileHeader)) {
296 if (!IsFfs3Fv) {
297 DEBUG ((EFI_D_ERROR, "Found a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &FfsFileHeader->Name));
298 }
299 FileOffset += sizeof (EFI_FFS_FILE_HEADER2);
300 FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER2));
301 } else {
302 FileOffset += sizeof (EFI_FFS_FILE_HEADER);
303 FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER));
304 }
305 break;
306
307 case EFI_FILE_DATA_VALID:
308 case EFI_FILE_MARKED_FOR_UPDATE:
309 if (CalculateHeaderChecksum (FfsFileHeader) != 0) {
310 ASSERT (FALSE);
311 *FileHeader = NULL;
312 return EFI_NOT_FOUND;
313 }
314
315 if (IS_FFS_FILE2 (FfsFileHeader)) {
316 FileLength = FFS_FILE2_SIZE (FfsFileHeader);
317 ASSERT (FileLength > 0x00FFFFFF);
318 FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
319 if (!IsFfs3Fv) {
320 DEBUG ((EFI_D_ERROR, "Found a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &FfsFileHeader->Name));
321 FileOffset += FileOccupiedSize;
322 FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsFileHeader + FileOccupiedSize);
323 break;
324 }
325 } else {
326 FileLength = FFS_FILE_SIZE (FfsFileHeader);
327 FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
328 }
329
330 DataCheckSum = FFS_FIXED_CHECKSUM;
331 if ((FfsFileHeader->Attributes & FFS_ATTRIB_CHECKSUM) == FFS_ATTRIB_CHECKSUM) {
332 if (IS_FFS_FILE2 (FfsFileHeader)) {
333 DataCheckSum = CalculateCheckSum8 ((CONST UINT8 *) FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER2), FileLength - sizeof(EFI_FFS_FILE_HEADER2));
334 } else {
335 DataCheckSum = CalculateCheckSum8 ((CONST UINT8 *) FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER), FileLength - sizeof(EFI_FFS_FILE_HEADER));
336 }
337 }
338 if (FfsFileHeader->IntegrityCheck.Checksum.File != DataCheckSum) {
339 ASSERT (FALSE);
340 *FileHeader = NULL;
341 return EFI_NOT_FOUND;
342 }
343
344 if (FileName != NULL) {
345 if (CompareGuid (&FfsFileHeader->Name, (EFI_GUID*)FileName)) {
346 *FileHeader = FfsFileHeader;
347 return EFI_SUCCESS;
348 }
349 } else if (SearchType == PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE) {
350 if ((FfsFileHeader->Type == EFI_FV_FILETYPE_PEIM) ||
351 (FfsFileHeader->Type == EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER) ||
352 (FfsFileHeader->Type == EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE)) {
353
354 *FileHeader = FfsFileHeader;
355 return EFI_SUCCESS;
356 } else if (AprioriFile != NULL) {
357 if (FfsFileHeader->Type == EFI_FV_FILETYPE_FREEFORM) {
358 if (CompareGuid (&FfsFileHeader->Name, &gPeiAprioriFileNameGuid)) {
359 *AprioriFile = FfsFileHeader;
360 }
361 }
362 }
363 } else if (((SearchType == FfsFileHeader->Type) || (SearchType == EFI_FV_FILETYPE_ALL)) &&
364 (FfsFileHeader->Type != EFI_FV_FILETYPE_FFS_PAD)) {
365 *FileHeader = FfsFileHeader;
366 return EFI_SUCCESS;
367 }
368
369 FileOffset += FileOccupiedSize;
370 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize);
371 break;
372
373 case EFI_FILE_DELETED:
374 if (IS_FFS_FILE2 (FfsFileHeader)) {
375 if (!IsFfs3Fv) {
376 DEBUG ((EFI_D_ERROR, "Found a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &FfsFileHeader->Name));
377 }
378 FileLength = FFS_FILE2_SIZE (FfsFileHeader);
379 ASSERT (FileLength > 0x00FFFFFF);
380 } else {
381 FileLength = FFS_FILE_SIZE (FfsFileHeader);
382 }
383 FileOccupiedSize = GET_OCCUPIED_SIZE(FileLength, 8);
384 FileOffset += FileOccupiedSize;
385 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize);
386 break;
387
388 default:
389 *FileHeader = NULL;
390 return EFI_NOT_FOUND;
391 }
392 }
393
394 *FileHeader = NULL;
395 return EFI_NOT_FOUND;
396 }
397
398 /**
399 Initialize PeiCore Fv List.
400
401 @param PrivateData - Pointer to PEI_CORE_INSTANCE.
402 @param SecCoreData - Pointer to EFI_SEC_PEI_HAND_OFF.
403 **/
404 VOID
405 PeiInitializeFv (
406 IN PEI_CORE_INSTANCE *PrivateData,
407 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData
408 )
409 {
410 EFI_STATUS Status;
411 EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi;
412 EFI_PEI_FV_HANDLE FvHandle;
413 EFI_FIRMWARE_VOLUME_HEADER *BfvHeader;
414
415 //
416 // Install FV_PPI for FFS2 file system.
417 //
418 PeiServicesInstallPpi (&mPeiFfs2FvPpiList);
419
420 //
421 // Install FV_PPI for FFS3 file system.
422 //
423 PeiServicesInstallPpi (&mPeiFfs3FvPpiList);
424
425 BfvHeader = (EFI_FIRMWARE_VOLUME_HEADER *)SecCoreData->BootFirmwareVolumeBase;
426
427 //
428 // The FV_PPI in BFV's format should be installed.
429 //
430 Status = PeiServicesLocatePpi (
431 &BfvHeader->FileSystemGuid,
432 0,
433 NULL,
434 (VOID**)&FvPpi
435 );
436 ASSERT_EFI_ERROR (Status);
437
438 //
439 // Get handle of BFV
440 //
441 FvPpi->ProcessVolume (
442 FvPpi,
443 SecCoreData->BootFirmwareVolumeBase,
444 (UINTN)BfvHeader->FvLength,
445 &FvHandle
446 );
447
448 //
449 // Update internal PEI_CORE_FV array.
450 //
451 PrivateData->Fv[PrivateData->FvCount].FvHeader = BfvHeader;
452 PrivateData->Fv[PrivateData->FvCount].FvPpi = FvPpi;
453 PrivateData->Fv[PrivateData->FvCount].FvHandle = FvHandle;
454 DEBUG ((
455 EFI_D_INFO,
456 "The %dth FV start address is 0x%11p, size is 0x%08x, handle is 0x%p\n",
457 (UINT32) PrivateData->FvCount,
458 (VOID *) BfvHeader,
459 BfvHeader->FvLength,
460 FvHandle
461 ));
462 PrivateData->FvCount ++;
463
464 //
465 // Post a call-back for the FvInfoPPI services to expose
466 // additional Fvs to PeiCore.
467 //
468 Status = PeiServicesNotifyPpi (&mNotifyOnFvInfoList);
469 ASSERT_EFI_ERROR (Status);
470
471 }
472
473 /**
474 Process Firmware Volum Information once FvInfoPPI install.
475 The FV Info will be registered into PeiCore private data structure.
476 And search the inside FV image, if found, the new FV INFO PPI will be installed.
477
478 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
479 @param NotifyDescriptor Address of the notification descriptor data structure.
480 @param Ppi Address of the PPI that was installed.
481
482 @retval EFI_SUCCESS The FV Info is registered into PeiCore private data structure.
483 @return if not EFI_SUCESS, fail to verify FV.
484
485 **/
486 EFI_STATUS
487 EFIAPI
488 FirmwareVolmeInfoPpiNotifyCallback (
489 IN EFI_PEI_SERVICES **PeiServices,
490 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
491 IN VOID *Ppi
492 )
493 {
494 EFI_PEI_FIRMWARE_VOLUME_INFO_PPI *FvInfoPpi;
495 EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi;
496 PEI_CORE_INSTANCE *PrivateData;
497 EFI_STATUS Status;
498 EFI_PEI_FV_HANDLE FvHandle;
499 UINTN FvIndex;
500 EFI_PEI_FILE_HANDLE FileHandle;
501 VOID *DepexData;
502
503 Status = EFI_SUCCESS;
504 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);
505
506 if (PrivateData->FvCount >= FixedPcdGet32 (PcdPeiCoreMaxFvSupported)) {
507 DEBUG ((EFI_D_ERROR, "The number of Fv Images (%d) exceed the max supported FVs (%d) in Pei", PrivateData->FvCount + 1, FixedPcdGet32 (PcdPeiCoreMaxFvSupported)));
508 DEBUG ((EFI_D_ERROR, "PcdPeiCoreMaxFvSupported value need be reconfigurated in DSC"));
509 ASSERT (FALSE);
510 }
511
512 FvInfoPpi = (EFI_PEI_FIRMWARE_VOLUME_INFO_PPI *)Ppi;
513
514 //
515 // Locate the corresponding FV_PPI according to founded FV's format guid
516 //
517 Status = PeiServicesLocatePpi (
518 &FvInfoPpi->FvFormat,
519 0,
520 NULL,
521 (VOID**)&FvPpi
522 );
523 if (!EFI_ERROR (Status)) {
524 //
525 // Process new found FV and get FV handle.
526 //
527 Status = FvPpi->ProcessVolume (FvPpi, FvInfoPpi->FvInfo, FvInfoPpi->FvInfoSize, &FvHandle);
528 if (EFI_ERROR (Status)) {
529 DEBUG ((EFI_D_ERROR, "Fail to process new found FV, FV may be corrupted!\n"));
530 return Status;
531 }
532
533 //
534 // Check whether the FV has already been processed.
535 //
536 for (FvIndex = 0; FvIndex < PrivateData->FvCount; FvIndex ++) {
537 if (PrivateData->Fv[FvIndex].FvHandle == FvHandle) {
538 DEBUG ((EFI_D_INFO, "The Fv %p has already been processed!\n", FvInfoPpi->FvInfo));
539 return EFI_SUCCESS;
540 }
541 }
542
543 //
544 // Update internal PEI_CORE_FV array.
545 //
546 PrivateData->Fv[PrivateData->FvCount].FvHeader = (EFI_FIRMWARE_VOLUME_HEADER*) FvInfoPpi->FvInfo;
547 PrivateData->Fv[PrivateData->FvCount].FvPpi = FvPpi;
548 PrivateData->Fv[PrivateData->FvCount].FvHandle = FvHandle;
549 DEBUG ((
550 EFI_D_INFO,
551 "The %dth FV start address is 0x%11p, size is 0x%08x, handle is 0x%p\n",
552 (UINT32) PrivateData->FvCount,
553 (VOID *) FvInfoPpi->FvInfo,
554 FvInfoPpi->FvInfoSize,
555 FvHandle
556 ));
557 PrivateData->FvCount ++;
558
559 //
560 // Scan and process the new discoveried FV for EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
561 //
562 FileHandle = NULL;
563 do {
564 Status = FvPpi->FindFileByType (
565 FvPpi,
566 EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE,
567 FvHandle,
568 &FileHandle
569 );
570 if (!EFI_ERROR (Status)) {
571 Status = FvPpi->FindSectionByType (
572 FvPpi,
573 EFI_SECTION_PEI_DEPEX,
574 FileHandle,
575 (VOID**)&DepexData
576 );
577 if (!EFI_ERROR (Status)) {
578 if (!PeimDispatchReadiness (PeiServices, DepexData)) {
579 //
580 // Dependency is not satisfied.
581 //
582 continue;
583 }
584 }
585
586 DEBUG ((EFI_D_INFO, "Found firmware volume Image File %p in FV[%d] %p\n", FileHandle, PrivateData->FvCount - 1, FvHandle));
587 ProcessFvFile (&PrivateData->Fv[PrivateData->FvCount - 1], FileHandle);
588 }
589 } while (FileHandle != NULL);
590 } else {
591 DEBUG ((EFI_D_ERROR, "Fail to process FV %p because no corresponding EFI_FIRMWARE_VOLUME_PPI is found!\n", FvInfoPpi->FvInfo));
592
593 AddUnknownFormatFvInfo (PrivateData, &FvInfoPpi->FvFormat, FvInfoPpi->FvInfo, FvInfoPpi->FvInfoSize);
594 }
595
596 return EFI_SUCCESS;
597 }
598
599 /**
600 Go through the file to search SectionType section.
601 Search within encapsulation sections (compression and GUIDed) recursively,
602 until the match section is found.
603
604 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
605 @param SectionType Filter to find only section of this type.
606 @param Section From where to search.
607 @param SectionSize The file size to search.
608 @param OutputBuffer A pointer to the discovered section, if successful.
609 NULL if section not found
610 @param IsFfs3Fv Indicates the FV format.
611
612 @return EFI_NOT_FOUND The match section is not found.
613 @return EFI_SUCCESS The match section is found.
614
615 **/
616 EFI_STATUS
617 ProcessSection (
618 IN CONST EFI_PEI_SERVICES **PeiServices,
619 IN EFI_SECTION_TYPE SectionType,
620 IN EFI_COMMON_SECTION_HEADER *Section,
621 IN UINTN SectionSize,
622 OUT VOID **OutputBuffer,
623 IN BOOLEAN IsFfs3Fv
624 )
625 {
626 EFI_STATUS Status;
627 UINT32 SectionLength;
628 UINT32 ParsedLength;
629 EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI *GuidSectionPpi;
630 EFI_PEI_DECOMPRESS_PPI *DecompressPpi;
631 VOID *PpiOutput;
632 UINTN PpiOutputSize;
633 UINTN Index;
634 UINT32 Authentication;
635 PEI_CORE_INSTANCE *PrivateData;
636
637 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);
638 *OutputBuffer = NULL;
639 ParsedLength = 0;
640 Index = 0;
641 Status = EFI_NOT_FOUND;
642 PpiOutput = NULL;
643 PpiOutputSize = 0;
644 while (ParsedLength < SectionSize) {
645
646 if (IS_SECTION2 (Section)) {
647 ASSERT (SECTION2_SIZE (Section) > 0x00FFFFFF);
648 if (!IsFfs3Fv) {
649 DEBUG ((EFI_D_ERROR, "Found a FFS3 formatted section in a non-FFS3 formatted FV.\n"));
650 SectionLength = SECTION2_SIZE (Section);
651 //
652 // SectionLength is adjusted it is 4 byte aligned.
653 // Go to the next section
654 //
655 SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);
656 ASSERT (SectionLength != 0);
657 ParsedLength += SectionLength;
658 Section = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) Section + SectionLength);
659 continue;
660 }
661 }
662
663 if (Section->Type == SectionType) {
664 if (IS_SECTION2 (Section)) {
665 *OutputBuffer = (VOID *)((UINT8 *) Section + sizeof (EFI_COMMON_SECTION_HEADER2));
666 } else {
667 *OutputBuffer = (VOID *)((UINT8 *) Section + sizeof (EFI_COMMON_SECTION_HEADER));
668 }
669 return EFI_SUCCESS;
670 } else if ((Section->Type == EFI_SECTION_GUID_DEFINED) || (Section->Type == EFI_SECTION_COMPRESSION)) {
671 //
672 // Check the encapsulated section is extracted into the cache data.
673 //
674 for (Index = 0; Index < PrivateData->CacheSection.AllSectionCount; Index ++) {
675 if (Section == PrivateData->CacheSection.Section[Index]) {
676 PpiOutput = PrivateData->CacheSection.SectionData[Index];
677 PpiOutputSize = PrivateData->CacheSection.SectionSize[Index];
678 //
679 // Search section directly from the cache data.
680 //
681 return ProcessSection (
682 PeiServices,
683 SectionType,
684 PpiOutput,
685 PpiOutputSize,
686 OutputBuffer,
687 IsFfs3Fv
688 );
689 }
690 }
691
692 Status = EFI_NOT_FOUND;
693 if (Section->Type == EFI_SECTION_GUID_DEFINED) {
694 if (IS_SECTION2 (Section)) {
695 Status = PeiServicesLocatePpi (
696 &((EFI_GUID_DEFINED_SECTION2 *)Section)->SectionDefinitionGuid,
697 0,
698 NULL,
699 (VOID **) &GuidSectionPpi
700 );
701 } else {
702 Status = PeiServicesLocatePpi (
703 &((EFI_GUID_DEFINED_SECTION *)Section)->SectionDefinitionGuid,
704 0,
705 NULL,
706 (VOID **) &GuidSectionPpi
707 );
708 }
709 if (!EFI_ERROR (Status)) {
710 Status = GuidSectionPpi->ExtractSection (
711 GuidSectionPpi,
712 Section,
713 &PpiOutput,
714 &PpiOutputSize,
715 &Authentication
716 );
717 }
718 } else if (Section->Type == EFI_SECTION_COMPRESSION) {
719 Status = PeiServicesLocatePpi (&gEfiPeiDecompressPpiGuid, 0, NULL, (VOID **) &DecompressPpi);
720 if (!EFI_ERROR (Status)) {
721 Status = DecompressPpi->Decompress (
722 DecompressPpi,
723 (CONST EFI_COMPRESSION_SECTION*) Section,
724 &PpiOutput,
725 &PpiOutputSize
726 );
727 }
728 }
729
730 if (!EFI_ERROR (Status)) {
731 //
732 // Update cache section data.
733 //
734 if (PrivateData->CacheSection.AllSectionCount < CACHE_SETION_MAX_NUMBER) {
735 PrivateData->CacheSection.AllSectionCount ++;
736 }
737 PrivateData->CacheSection.Section [PrivateData->CacheSection.SectionIndex] = Section;
738 PrivateData->CacheSection.SectionData [PrivateData->CacheSection.SectionIndex] = PpiOutput;
739 PrivateData->CacheSection.SectionSize [PrivateData->CacheSection.SectionIndex] = PpiOutputSize;
740 PrivateData->CacheSection.SectionIndex = (PrivateData->CacheSection.SectionIndex + 1)%CACHE_SETION_MAX_NUMBER;
741
742 return ProcessSection (
743 PeiServices,
744 SectionType,
745 PpiOutput,
746 PpiOutputSize,
747 OutputBuffer,
748 IsFfs3Fv
749 );
750 }
751 }
752
753 if (IS_SECTION2 (Section)) {
754 SectionLength = SECTION2_SIZE (Section);
755 } else {
756 SectionLength = SECTION_SIZE (Section);
757 }
758 //
759 // SectionLength is adjusted it is 4 byte aligned.
760 // Go to the next section
761 //
762 SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);
763 ASSERT (SectionLength != 0);
764 ParsedLength += SectionLength;
765 Section = (EFI_COMMON_SECTION_HEADER *)((UINT8 *)Section + SectionLength);
766 }
767
768 return EFI_NOT_FOUND;
769 }
770
771
772 /**
773 Searches for the next matching section within the specified file.
774
775 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
776 @param SectionType Filter to find only sections of this type.
777 @param FileHandle Pointer to the current file to search.
778 @param SectionData A pointer to the discovered section, if successful.
779 NULL if section not found
780
781 @retval EFI_NOT_FOUND The section was not found.
782 @retval EFI_SUCCESS The section was found.
783
784 **/
785 EFI_STATUS
786 EFIAPI
787 PeiFfsFindSectionData (
788 IN CONST EFI_PEI_SERVICES **PeiServices,
789 IN EFI_SECTION_TYPE SectionType,
790 IN EFI_PEI_FILE_HANDLE FileHandle,
791 OUT VOID **SectionData
792 )
793 {
794 PEI_CORE_FV_HANDLE *CoreFvHandle;
795
796 CoreFvHandle = FileHandleToVolume (FileHandle);
797 if ((CoreFvHandle == NULL) || (CoreFvHandle->FvPpi == NULL)) {
798 return EFI_NOT_FOUND;
799 }
800
801 return CoreFvHandle->FvPpi->FindSectionByType (CoreFvHandle->FvPpi, SectionType, FileHandle, SectionData);
802 }
803
804 /**
805 Searches for the next matching file in the firmware volume.
806
807 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
808 @param SearchType Filter to find only files of this type.
809 Type EFI_FV_FILETYPE_ALL causes no filtering to be done.
810 @param FvHandle Handle of firmware volume in which to search.
811 @param FileHandle On entry, points to the current handle from which to begin searching or NULL to start
812 at the beginning of the firmware volume. On exit, points the file handle of the next file
813 in the volume or NULL if there are no more files.
814
815 @retval EFI_NOT_FOUND The file was not found.
816 @retval EFI_NOT_FOUND The header checksum was not zero.
817 @retval EFI_SUCCESS The file was found.
818
819 **/
820 EFI_STATUS
821 EFIAPI
822 PeiFfsFindNextFile (
823 IN CONST EFI_PEI_SERVICES **PeiServices,
824 IN UINT8 SearchType,
825 IN EFI_PEI_FV_HANDLE FvHandle,
826 IN OUT EFI_PEI_FILE_HANDLE *FileHandle
827 )
828 {
829 PEI_CORE_FV_HANDLE *CoreFvHandle;
830
831 CoreFvHandle = FvHandleToCoreHandle (FvHandle);
832
833 //
834 // To make backward compatiblity, if can not find corresponding the handle of FV
835 // then treat FV as build-in FFS2/FFS3 format and memory mapped FV that FV handle is pointed
836 // to the address of first byte of FV.
837 //
838 if ((CoreFvHandle == NULL) && FeaturePcdGet (PcdFrameworkCompatibilitySupport)) {
839 return FindFileEx (FvHandle, NULL, SearchType, FileHandle, NULL);
840 }
841
842 if ((CoreFvHandle == NULL) || CoreFvHandle->FvPpi == NULL) {
843 return EFI_NOT_FOUND;
844 }
845
846 return CoreFvHandle->FvPpi->FindFileByType (CoreFvHandle->FvPpi, SearchType, FvHandle, FileHandle);
847 }
848
849
850 /**
851 Search the firmware volumes by index
852
853 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
854 @param Instance This instance of the firmware volume to find. The value 0 is the Boot Firmware
855 Volume (BFV).
856 @param VolumeHandle On exit, points to the next volume handle or NULL if it does not exist.
857
858 @retval EFI_INVALID_PARAMETER VolumeHandle is NULL
859 @retval EFI_NOT_FOUND The volume was not found.
860 @retval EFI_SUCCESS The volume was found.
861
862 **/
863 EFI_STATUS
864 EFIAPI
865 PeiFfsFindNextVolume (
866 IN CONST EFI_PEI_SERVICES **PeiServices,
867 IN UINTN Instance,
868 IN OUT EFI_PEI_FV_HANDLE *VolumeHandle
869 )
870 {
871 PEI_CORE_INSTANCE *Private;
872 PEI_CORE_FV_HANDLE *CoreFvHandle;
873
874 if (VolumeHandle == NULL) {
875 return EFI_INVALID_PARAMETER;
876 }
877
878 Private = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);
879
880 CoreFvHandle = FindNextCoreFvHandle (Private, Instance);
881 if (CoreFvHandle == NULL) {
882 *VolumeHandle = NULL;
883 return EFI_NOT_FOUND;
884 }
885
886 *VolumeHandle = CoreFvHandle->FvHandle;
887
888 return EFI_SUCCESS;
889 }
890
891
892 /**
893 Find a file within a volume by its name.
894
895 @param FileName A pointer to the name of the file to find within the firmware volume.
896 @param VolumeHandle The firmware volume to search
897 @param FileHandle Upon exit, points to the found file's handle
898 or NULL if it could not be found.
899
900 @retval EFI_SUCCESS File was found.
901 @retval EFI_NOT_FOUND File was not found.
902 @retval EFI_INVALID_PARAMETER VolumeHandle or FileHandle or FileName was NULL.
903
904 **/
905 EFI_STATUS
906 EFIAPI
907 PeiFfsFindFileByName (
908 IN CONST EFI_GUID *FileName,
909 IN EFI_PEI_FV_HANDLE VolumeHandle,
910 OUT EFI_PEI_FILE_HANDLE *FileHandle
911 )
912 {
913 PEI_CORE_FV_HANDLE *CoreFvHandle;
914
915 if ((VolumeHandle == NULL) || (FileName == NULL) || (FileHandle == NULL)) {
916 return EFI_INVALID_PARAMETER;
917 }
918
919 CoreFvHandle = FvHandleToCoreHandle (VolumeHandle);
920 if ((CoreFvHandle == NULL) || (CoreFvHandle->FvPpi == NULL)) {
921 return EFI_NOT_FOUND;
922 }
923
924 return CoreFvHandle->FvPpi->FindFileByName (CoreFvHandle->FvPpi, FileName, &VolumeHandle, FileHandle);
925 }
926
927 /**
928 Returns information about a specific file.
929
930 @param FileHandle Handle of the file.
931 @param FileInfo Upon exit, points to the file's information.
932
933 @retval EFI_INVALID_PARAMETER If FileInfo is NULL.
934 @retval EFI_INVALID_PARAMETER If FileHandle does not represent a valid file.
935 @retval EFI_SUCCESS File information returned.
936
937 **/
938 EFI_STATUS
939 EFIAPI
940 PeiFfsGetFileInfo (
941 IN EFI_PEI_FILE_HANDLE FileHandle,
942 OUT EFI_FV_FILE_INFO *FileInfo
943 )
944 {
945 PEI_CORE_FV_HANDLE *CoreFvHandle;
946
947 if ((FileHandle == NULL) || (FileInfo == NULL)) {
948 return EFI_INVALID_PARAMETER;
949 }
950
951 //
952 // Retrieve the FirmwareVolume which the file resides in.
953 //
954 CoreFvHandle = FileHandleToVolume (FileHandle);
955 if ((CoreFvHandle == NULL) || (CoreFvHandle->FvPpi == NULL)) {
956 return EFI_INVALID_PARAMETER;
957 }
958
959 return CoreFvHandle->FvPpi->GetFileInfo (CoreFvHandle->FvPpi, FileHandle, FileInfo);
960 }
961
962
963 /**
964 Returns information about the specified volume.
965
966 This function returns information about a specific firmware
967 volume, including its name, type, attributes, starting address
968 and size.
969
970 @param VolumeHandle Handle of the volume.
971 @param VolumeInfo Upon exit, points to the volume's information.
972
973 @retval EFI_SUCCESS Volume information returned.
974 @retval EFI_INVALID_PARAMETER If VolumeHandle does not represent a valid volume.
975 @retval EFI_INVALID_PARAMETER If VolumeHandle is NULL.
976 @retval EFI_SUCCESS Information successfully returned.
977 @retval EFI_INVALID_PARAMETER The volume designated by the VolumeHandle is not available.
978
979 **/
980 EFI_STATUS
981 EFIAPI
982 PeiFfsGetVolumeInfo (
983 IN EFI_PEI_FV_HANDLE VolumeHandle,
984 OUT EFI_FV_INFO *VolumeInfo
985 )
986 {
987 PEI_CORE_FV_HANDLE *CoreHandle;
988
989 if ((VolumeInfo == NULL) || (VolumeHandle == NULL)) {
990 return EFI_INVALID_PARAMETER;
991 }
992
993 CoreHandle = FvHandleToCoreHandle (VolumeHandle);
994
995 if ((CoreHandle == NULL) || (CoreHandle->FvPpi == NULL)) {
996 return EFI_INVALID_PARAMETER;
997 }
998
999 return CoreHandle->FvPpi->GetVolumeInfo (CoreHandle->FvPpi, VolumeHandle, VolumeInfo);
1000 }
1001
1002 /**
1003 Get Fv image from the FV type file, then install FV INFO ppi, Build FV hob.
1004
1005 @param ParentFvCoreHandle Pointer of EFI_CORE_FV_HANDLE to parent Fv image that contain this Fv image.
1006 @param ParentFvFileHandle File handle of a Fv type file that contain this Fv image.
1007
1008 @retval EFI_NOT_FOUND FV image can't be found.
1009 @retval EFI_SUCCESS Successfully to process it.
1010 @retval EFI_OUT_OF_RESOURCES Can not allocate page when aligning FV image
1011 @retval Others Can not find EFI_SECTION_FIRMWARE_VOLUME_IMAGE section
1012
1013 **/
1014 EFI_STATUS
1015 ProcessFvFile (
1016 IN PEI_CORE_FV_HANDLE *ParentFvCoreHandle,
1017 IN EFI_PEI_FILE_HANDLE ParentFvFileHandle
1018 )
1019 {
1020 EFI_STATUS Status;
1021 EFI_FV_INFO ParentFvImageInfo;
1022 UINT32 FvAlignment;
1023 VOID *NewFvBuffer;
1024 EFI_PEI_HOB_POINTERS HobPtr;
1025 EFI_PEI_FIRMWARE_VOLUME_PPI *ParentFvPpi;
1026 EFI_PEI_FV_HANDLE ParentFvHandle;
1027 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;
1028 EFI_FV_FILE_INFO FileInfo;
1029 UINT64 FvLength;
1030
1031 //
1032 // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has already
1033 // been extracted.
1034 //
1035 HobPtr.Raw = GetHobList ();
1036 while ((HobPtr.Raw = GetNextHob (EFI_HOB_TYPE_FV2, HobPtr.Raw)) != NULL) {
1037 if (CompareGuid (&(((EFI_FFS_FILE_HEADER *)ParentFvFileHandle)->Name), &HobPtr.FirmwareVolume2->FileName)) {
1038 //
1039 // this FILE has been dispatched, it will not be dispatched again.
1040 //
1041 DEBUG ((EFI_D_INFO, "FV file %p has been dispatched!\r\n", ParentFvFileHandle));
1042 return EFI_SUCCESS;
1043 }
1044 HobPtr.Raw = GET_NEXT_HOB (HobPtr);
1045 }
1046
1047 ParentFvHandle = ParentFvCoreHandle->FvHandle;
1048 ParentFvPpi = ParentFvCoreHandle->FvPpi;
1049
1050 //
1051 // Find FvImage in FvFile
1052 //
1053 Status = ParentFvPpi->FindSectionByType (
1054 ParentFvPpi,
1055 EFI_SECTION_FIRMWARE_VOLUME_IMAGE,
1056 ParentFvFileHandle,
1057 (VOID **)&FvHeader
1058 );
1059
1060 if (EFI_ERROR (Status)) {
1061 return Status;
1062 }
1063
1064 //
1065 // FvAlignment must be more than 8 bytes required by FvHeader structure.
1066 //
1067 FvAlignment = 1 << ((ReadUnaligned32 (&FvHeader->Attributes) & EFI_FVB2_ALIGNMENT) >> 16);
1068 if (FvAlignment < 8) {
1069 FvAlignment = 8;
1070 }
1071
1072 //
1073 // Check FvImage
1074 //
1075 if ((UINTN) FvHeader % FvAlignment != 0) {
1076 FvLength = ReadUnaligned64 (&FvHeader->FvLength);
1077 NewFvBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES ((UINT32) FvLength), FvAlignment);
1078 if (NewFvBuffer == NULL) {
1079 return EFI_OUT_OF_RESOURCES;
1080 }
1081 CopyMem (NewFvBuffer, FvHeader, (UINTN) FvLength);
1082 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER*) NewFvBuffer;
1083 }
1084
1085 Status = ParentFvPpi->GetVolumeInfo (ParentFvPpi, ParentFvHandle, &ParentFvImageInfo);
1086 ASSERT_EFI_ERROR (Status);
1087
1088 Status = ParentFvPpi->GetFileInfo (ParentFvPpi, ParentFvFileHandle, &FileInfo);
1089 ASSERT_EFI_ERROR (Status);
1090
1091 //
1092 // Install FvPpi and Build FvHob
1093 //
1094 PeiServicesInstallFvInfoPpi (
1095 &FvHeader->FileSystemGuid,
1096 (VOID**) FvHeader,
1097 (UINT32) FvHeader->FvLength,
1098 &ParentFvImageInfo.FvName,
1099 &FileInfo.FileName
1100 );
1101
1102 //
1103 // Inform the extracted FvImage to Fv HOB consumer phase, i.e. DXE phase
1104 //
1105 BuildFvHob (
1106 (EFI_PHYSICAL_ADDRESS) (UINTN) FvHeader,
1107 FvHeader->FvLength
1108 );
1109
1110 //
1111 // Makes the encapsulated volume show up in DXE phase to skip processing of
1112 // encapsulated file again.
1113 //
1114 BuildFv2Hob (
1115 (EFI_PHYSICAL_ADDRESS) (UINTN) FvHeader,
1116 FvHeader->FvLength,
1117 &ParentFvImageInfo.FvName,
1118 &FileInfo.FileName
1119 );
1120
1121 return EFI_SUCCESS;
1122 }
1123
1124 /**
1125 Process a firmware volume and create a volume handle.
1126
1127 Create a volume handle from the information in the buffer. For
1128 memory-mapped firmware volumes, Buffer and BufferSize refer to
1129 the start of the firmware volume and the firmware volume size.
1130 For non memory-mapped firmware volumes, this points to a
1131 buffer which contains the necessary information for creating
1132 the firmware volume handle. Normally, these values are derived
1133 from the EFI_FIRMWARE_VOLUME_INFO_PPI.
1134
1135
1136 @param This Points to this instance of the
1137 EFI_PEI_FIRMWARE_VOLUME_PPI.
1138 @param Buffer Points to the start of the buffer.
1139 @param BufferSize Size of the buffer.
1140 @param FvHandle Points to the returned firmware volume
1141 handle. The firmware volume handle must
1142 be unique within the system.
1143
1144 @retval EFI_SUCCESS Firmware volume handle created.
1145 @retval EFI_VOLUME_CORRUPTED Volume was corrupt.
1146
1147 **/
1148 EFI_STATUS
1149 EFIAPI
1150 PeiFfsFvPpiProcessVolume (
1151 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,
1152 IN VOID *Buffer,
1153 IN UINTN BufferSize,
1154 OUT EFI_PEI_FV_HANDLE *FvHandle
1155 )
1156 {
1157 EFI_STATUS Status;
1158
1159 ASSERT (FvHandle != NULL);
1160
1161 if (Buffer == NULL) {
1162 return EFI_VOLUME_CORRUPTED;
1163 }
1164
1165 //
1166 // The build-in EFI_PEI_FIRMWARE_VOLUME_PPI for FFS2/FFS3 support memory-mapped
1167 // FV image and the handle is pointed to Fv image's buffer.
1168 //
1169 *FvHandle = (EFI_PEI_FV_HANDLE) Buffer;
1170
1171 //
1172 // Do verify for given FV buffer.
1173 //
1174 Status = VerifyFv ((EFI_FIRMWARE_VOLUME_HEADER*) Buffer);
1175 if (EFI_ERROR(Status)) {
1176 DEBUG ((EFI_D_ERROR, "Fail to verify FV which address is 0x%11p", Buffer));
1177 return EFI_VOLUME_CORRUPTED;
1178 }
1179
1180 return EFI_SUCCESS;
1181 }
1182
1183 /**
1184 Finds the next file of the specified type.
1185
1186 This service enables PEI modules to discover additional firmware files.
1187 The FileHandle must be unique within the system.
1188
1189 @param This Points to this instance of the
1190 EFI_PEI_FIRMWARE_VOLUME_PPI.
1191 @param SearchType A filter to find only files of this type. Type
1192 EFI_FV_FILETYPE_ALL causes no filtering to be
1193 done.
1194 @param FvHandle Handle of firmware volume in which to
1195 search.
1196 @param FileHandle Points to the current handle from which to
1197 begin searching or NULL to start at the
1198 beginning of the firmware volume. Updated
1199 upon return to reflect the file found.
1200
1201 @retval EFI_SUCCESS The file was found.
1202 @retval EFI_NOT_FOUND The file was not found. FileHandle contains NULL.
1203
1204 **/
1205 EFI_STATUS
1206 EFIAPI
1207 PeiFfsFvPpiFindFileByType (
1208 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,
1209 IN EFI_FV_FILETYPE SearchType,
1210 IN EFI_PEI_FV_HANDLE FvHandle,
1211 IN OUT EFI_PEI_FILE_HANDLE *FileHandle
1212 )
1213 {
1214 return FindFileEx (FvHandle, NULL, SearchType, FileHandle, NULL);
1215 }
1216
1217 /**
1218 Find a file within a volume by its name.
1219
1220 This service searches for files with a specific name, within
1221 either the specified firmware volume or all firmware volumes.
1222
1223 @param This Points to this instance of the
1224 EFI_PEI_FIRMWARE_VOLUME_PPI.
1225 @param FileName A pointer to the name of the file to find
1226 within the firmware volume.
1227 @param FvHandle Upon entry, the pointer to the firmware
1228 volume to search or NULL if all firmware
1229 volumes should be searched. Upon exit, the
1230 actual firmware volume in which the file was
1231 found.
1232 @param FileHandle Upon exit, points to the found file's
1233 handle or NULL if it could not be found.
1234
1235 @retval EFI_SUCCESS File was found.
1236 @retval EFI_NOT_FOUND File was not found.
1237 @retval EFI_INVALID_PARAMETER FvHandle or FileHandle or
1238 FileName was NULL.
1239
1240
1241 **/
1242 EFI_STATUS
1243 EFIAPI
1244 PeiFfsFvPpiFindFileByName (
1245 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,
1246 IN CONST EFI_GUID *FileName,
1247 IN EFI_PEI_FV_HANDLE *FvHandle,
1248 OUT EFI_PEI_FILE_HANDLE *FileHandle
1249 )
1250 {
1251 EFI_STATUS Status;
1252 PEI_CORE_INSTANCE *PrivateData;
1253 UINTN Index;
1254
1255 if ((FvHandle == NULL) || (FileName == NULL) || (FileHandle == NULL)) {
1256 return EFI_INVALID_PARAMETER;
1257 }
1258
1259 if (*FvHandle != NULL) {
1260 Status = FindFileEx (*FvHandle, FileName, 0, FileHandle, NULL);
1261 if (Status == EFI_NOT_FOUND) {
1262 *FileHandle = NULL;
1263 }
1264 } else {
1265 //
1266 // If *FvHandle = NULL, so search all FV for given filename
1267 //
1268 Status = EFI_NOT_FOUND;
1269
1270 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer());
1271 for (Index = 0; Index < PrivateData->FvCount; Index ++) {
1272 //
1273 // Only search the FV which is associated with a EFI_PEI_FIRMWARE_VOLUME_PPI instance.
1274 //
1275 if (PrivateData->Fv[Index].FvPpi != NULL) {
1276 Status = FindFileEx (PrivateData->Fv[Index].FvHandle, FileName, 0, FileHandle, NULL);
1277 if (!EFI_ERROR (Status)) {
1278 *FvHandle = PrivateData->Fv[Index].FvHandle;
1279 break;
1280 }
1281 }
1282 }
1283 }
1284
1285 return Status;
1286 }
1287
1288 /**
1289 Returns information about a specific file.
1290
1291 This function returns information about a specific
1292 file, including its file name, type, attributes, starting
1293 address and size.
1294
1295 @param This Points to this instance of the
1296 EFI_PEI_FIRMWARE_VOLUME_PPI.
1297 @param FileHandle Handle of the file.
1298 @param FileInfo Upon exit, points to the file's
1299 information.
1300
1301 @retval EFI_SUCCESS File information returned.
1302 @retval EFI_INVALID_PARAMETER If FileHandle does not
1303 represent a valid file.
1304 @retval EFI_INVALID_PARAMETER If FileInfo is NULL.
1305
1306 **/
1307 EFI_STATUS
1308 EFIAPI
1309 PeiFfsFvPpiGetFileInfo (
1310 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,
1311 IN EFI_PEI_FILE_HANDLE FileHandle,
1312 OUT EFI_FV_FILE_INFO *FileInfo
1313 )
1314 {
1315 UINT8 FileState;
1316 UINT8 ErasePolarity;
1317 EFI_FFS_FILE_HEADER *FileHeader;
1318 PEI_CORE_FV_HANDLE *CoreFvHandle;
1319 PEI_FW_VOL_INSTANCE *FwVolInstance;
1320
1321 if ((FileHandle == NULL) || (FileInfo == NULL)) {
1322 return EFI_INVALID_PARAMETER;
1323 }
1324
1325 //
1326 // Retrieve the FirmwareVolume which the file resides in.
1327 //
1328 CoreFvHandle = FileHandleToVolume (FileHandle);
1329 if (CoreFvHandle == NULL) {
1330 return EFI_INVALID_PARAMETER;
1331 }
1332
1333 FwVolInstance = PEI_FW_VOL_INSTANCE_FROM_FV_THIS (This);
1334
1335 if ((CoreFvHandle->FvHeader->Attributes & EFI_FVB2_ERASE_POLARITY) != 0) {
1336 ErasePolarity = 1;
1337 } else {
1338 ErasePolarity = 0;
1339 }
1340
1341 //
1342 // Get FileState which is the highest bit of the State
1343 //
1344 FileState = GetFileState (ErasePolarity, (EFI_FFS_FILE_HEADER*)FileHandle);
1345
1346 switch (FileState) {
1347 case EFI_FILE_DATA_VALID:
1348 case EFI_FILE_MARKED_FOR_UPDATE:
1349 break;
1350 default:
1351 return EFI_INVALID_PARAMETER;
1352 }
1353
1354 FileHeader = (EFI_FFS_FILE_HEADER *)FileHandle;
1355 CopyMem (&FileInfo->FileName, &FileHeader->Name, sizeof(EFI_GUID));
1356 FileInfo->FileType = FileHeader->Type;
1357 FileInfo->FileAttributes = FfsAttributes2FvFileAttributes (FileHeader->Attributes);
1358 if ((CoreFvHandle->FvHeader->Attributes & EFI_FVB2_MEMORY_MAPPED) == EFI_FVB2_MEMORY_MAPPED) {
1359 FileInfo->FileAttributes |= EFI_FV_FILE_ATTRIB_MEMORY_MAPPED;
1360 }
1361 if (IS_FFS_FILE2 (FileHeader)) {
1362 ASSERT (FFS_FILE2_SIZE (FileHeader) > 0x00FFFFFF);
1363 if (!FwVolInstance->IsFfs3Fv) {
1364 DEBUG ((EFI_D_ERROR, "It is a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &FileHeader->Name));
1365 return EFI_INVALID_PARAMETER;
1366 }
1367 FileInfo->BufferSize = FFS_FILE2_SIZE (FileHeader) - sizeof (EFI_FFS_FILE_HEADER2);
1368 FileInfo->Buffer = (UINT8 *) FileHeader + sizeof (EFI_FFS_FILE_HEADER2);
1369 } else {
1370 FileInfo->BufferSize = FFS_FILE_SIZE (FileHeader) - sizeof (EFI_FFS_FILE_HEADER);
1371 FileInfo->Buffer = (UINT8 *) FileHeader + sizeof (EFI_FFS_FILE_HEADER);
1372 }
1373 return EFI_SUCCESS;
1374 }
1375
1376 /**
1377 This function returns information about the firmware volume.
1378
1379 @param This Points to this instance of the
1380 EFI_PEI_FIRMWARE_VOLUME_PPI.
1381 @param FvHandle Handle to the firmware handle.
1382 @param VolumeInfo Points to the returned firmware volume
1383 information.
1384
1385 @retval EFI_SUCCESS Information returned successfully.
1386 @retval EFI_INVALID_PARAMETER FvHandle does not indicate a valid
1387 firmware volume or VolumeInfo is NULL.
1388
1389 **/
1390 EFI_STATUS
1391 EFIAPI
1392 PeiFfsFvPpiGetVolumeInfo (
1393 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,
1394 IN EFI_PEI_FV_HANDLE FvHandle,
1395 OUT EFI_FV_INFO *VolumeInfo
1396 )
1397 {
1398 EFI_FIRMWARE_VOLUME_HEADER FwVolHeader;
1399 EFI_FIRMWARE_VOLUME_EXT_HEADER *FwVolExHeaderInfo;
1400
1401 if ((VolumeInfo == NULL) || (FvHandle == NULL)) {
1402 return EFI_INVALID_PARAMETER;
1403 }
1404
1405 //
1406 // VolumeHandle may not align at 8 byte,
1407 // but FvLength is UINT64 type, which requires FvHeader align at least 8 byte.
1408 // So, Copy FvHeader into the local FvHeader structure.
1409 //
1410 CopyMem (&FwVolHeader, FvHandle, sizeof (EFI_FIRMWARE_VOLUME_HEADER));
1411
1412 //
1413 // Check Fv Image Signature
1414 //
1415 if (FwVolHeader.Signature != EFI_FVH_SIGNATURE) {
1416 return EFI_INVALID_PARAMETER;
1417 }
1418
1419 ZeroMem (VolumeInfo, sizeof (EFI_FV_INFO));
1420 VolumeInfo->FvAttributes = FwVolHeader.Attributes;
1421 VolumeInfo->FvStart = (VOID *) FvHandle;
1422 VolumeInfo->FvSize = FwVolHeader.FvLength;
1423 CopyMem (&VolumeInfo->FvFormat, &FwVolHeader.FileSystemGuid, sizeof(EFI_GUID));
1424
1425 if (FwVolHeader.ExtHeaderOffset != 0) {
1426 FwVolExHeaderInfo = (EFI_FIRMWARE_VOLUME_EXT_HEADER*)(((UINT8 *)FvHandle) + FwVolHeader.ExtHeaderOffset);
1427 CopyMem (&VolumeInfo->FvName, &FwVolExHeaderInfo->FvName, sizeof(EFI_GUID));
1428 }
1429
1430 return EFI_SUCCESS;
1431 }
1432
1433 /**
1434 Find the next matching section in the firmware file.
1435
1436 This service enables PEI modules to discover sections
1437 of a given type within a valid file.
1438
1439 @param This Points to this instance of the
1440 EFI_PEI_FIRMWARE_VOLUME_PPI.
1441 @param SearchType A filter to find only sections of this
1442 type.
1443 @param FileHandle Handle of firmware file in which to
1444 search.
1445 @param SectionData Updated upon return to point to the
1446 section found.
1447
1448 @retval EFI_SUCCESS Section was found.
1449 @retval EFI_NOT_FOUND Section of the specified type was not
1450 found. SectionData contains NULL.
1451 **/
1452 EFI_STATUS
1453 EFIAPI
1454 PeiFfsFvPpiFindSectionByType (
1455 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,
1456 IN EFI_SECTION_TYPE SearchType,
1457 IN EFI_PEI_FILE_HANDLE FileHandle,
1458 OUT VOID **SectionData
1459 )
1460 {
1461 EFI_FFS_FILE_HEADER *FfsFileHeader;
1462 UINT32 FileSize;
1463 EFI_COMMON_SECTION_HEADER *Section;
1464 PEI_FW_VOL_INSTANCE *FwVolInstance;
1465
1466 FwVolInstance = PEI_FW_VOL_INSTANCE_FROM_FV_THIS (This);
1467
1468 FfsFileHeader = (EFI_FFS_FILE_HEADER *)(FileHandle);
1469
1470 if (IS_FFS_FILE2 (FfsFileHeader)) {
1471 ASSERT (FFS_FILE2_SIZE (FfsFileHeader) > 0x00FFFFFF);
1472 if (!FwVolInstance->IsFfs3Fv) {
1473 DEBUG ((EFI_D_ERROR, "It is a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &FfsFileHeader->Name));
1474 return EFI_NOT_FOUND;
1475 }
1476 Section = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER2));
1477 FileSize = FFS_FILE2_SIZE (FfsFileHeader) - sizeof (EFI_FFS_FILE_HEADER2);
1478 } else {
1479 Section = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER));
1480 FileSize = FFS_FILE_SIZE (FfsFileHeader) - sizeof (EFI_FFS_FILE_HEADER);
1481 }
1482
1483 return ProcessSection (
1484 GetPeiServicesTablePointer (),
1485 SearchType,
1486 Section,
1487 FileSize,
1488 SectionData,
1489 FwVolInstance->IsFfs3Fv
1490 );
1491 }
1492
1493 /**
1494 Convert the handle of FV to pointer of corresponding PEI_CORE_FV_HANDLE.
1495
1496 @param FvHandle The handle of a FV.
1497
1498 @retval NULL if can not find.
1499 @return Pointer of corresponding PEI_CORE_FV_HANDLE.
1500 **/
1501 PEI_CORE_FV_HANDLE *
1502 FvHandleToCoreHandle (
1503 IN EFI_PEI_FV_HANDLE FvHandle
1504 )
1505 {
1506 UINTN Index;
1507 PEI_CORE_INSTANCE *PrivateData;
1508
1509 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer());
1510 for (Index = 0; Index < PrivateData->FvCount; Index ++) {
1511 if (FvHandle == PrivateData->Fv[Index].FvHandle) {
1512 return &PrivateData->Fv[Index];
1513 }
1514 }
1515
1516 return NULL;
1517 }
1518
1519 /**
1520 Get instance of PEI_CORE_FV_HANDLE for next volume according to given index.
1521
1522 This routine also will install FvInfo ppi for FV hob in PI ways.
1523
1524 @param Private Pointer of PEI_CORE_INSTANCE
1525 @param Instance The index of FV want to be searched.
1526
1527 @return Instance of PEI_CORE_FV_HANDLE.
1528 **/
1529 PEI_CORE_FV_HANDLE *
1530 FindNextCoreFvHandle (
1531 IN PEI_CORE_INSTANCE *Private,
1532 IN UINTN Instance
1533 )
1534 {
1535 UINTN Index;
1536 BOOLEAN Match;
1537 EFI_HOB_FIRMWARE_VOLUME *FvHob;
1538
1539 //
1540 // Handle Framework FvHob and Install FvInfo Ppi for it.
1541 //
1542 if (FeaturePcdGet (PcdFrameworkCompatibilitySupport)) {
1543 //
1544 // Loop to search the wanted FirmwareVolume which supports FFS
1545 //
1546 FvHob = (EFI_HOB_FIRMWARE_VOLUME *)GetFirstHob (EFI_HOB_TYPE_FV);
1547 while (FvHob != NULL) {
1548 //
1549 // Search whether FvHob has been installed into PeiCore's FV database.
1550 // If found, no need install new FvInfoPpi for it.
1551 //
1552 for (Index = 0, Match = FALSE; Index < Private->FvCount; Index++) {
1553 if ((EFI_PEI_FV_HANDLE)(UINTN)FvHob->BaseAddress == Private->Fv[Index].FvHeader) {
1554 Match = TRUE;
1555 break;
1556 }
1557 }
1558
1559 //
1560 // Search whether FvHob has been cached into PeiCore's Unknown FV database.
1561 // If found, no need install new FvInfoPpi for it.
1562 //
1563 if (!Match) {
1564 for (Index = 0; Index < Private->UnknownFvInfoCount; Index ++) {
1565 if ((UINTN)FvHob->BaseAddress == (UINTN)Private->UnknownFvInfo[Index].FvInfo) {
1566 Match = TRUE;
1567 break;
1568 }
1569 }
1570 }
1571
1572 //
1573 // If the Fv in FvHob has not been installed into PeiCore's FV database and has
1574 // not been cached into PeiCore's Unknown FV database, install a new FvInfoPpi
1575 // for it then PeiCore will dispatch it in callback of FvInfoPpi.
1576 //
1577 if (!Match) {
1578 PeiServicesInstallFvInfoPpi (
1579 &(((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)FvHob->BaseAddress)->FileSystemGuid),
1580 (VOID *)(UINTN)FvHob->BaseAddress,
1581 (UINT32)FvHob->Length,
1582 NULL,
1583 NULL
1584 );
1585 }
1586
1587 FvHob = (EFI_HOB_FIRMWARE_VOLUME *)GetNextHob (EFI_HOB_TYPE_FV, (VOID *)((UINTN)FvHob + FvHob->Header.HobLength));
1588 }
1589 }
1590
1591 ASSERT (Private->FvCount <= FixedPcdGet32 (PcdPeiCoreMaxFvSupported));
1592 if (Instance >= Private->FvCount) {
1593 return NULL;
1594 }
1595
1596 return &Private->Fv[Instance];
1597 }
1598
1599 /**
1600 After PeiCore image is shadowed into permanent memory, all build-in FvPpi should
1601 be re-installed with the instance in permanent memory and all cached FvPpi pointers in
1602 PrivateData->Fv[] array should be fixed up to be pointed to the one in permenant
1603 memory.
1604
1605 @param PrivateData Pointer to PEI_CORE_INSTANCE.
1606 **/
1607 VOID
1608 PeiReinitializeFv (
1609 IN PEI_CORE_INSTANCE *PrivateData
1610 )
1611 {
1612 VOID *OldFfsFvPpi;
1613 EFI_PEI_PPI_DESCRIPTOR *OldDescriptor;
1614 UINTN Index;
1615 EFI_STATUS Status;
1616
1617 //
1618 // Locate old build-in Ffs2 EFI_PEI_FIRMWARE_VOLUME_PPI which
1619 // in flash.
1620 //
1621 Status = PeiServicesLocatePpi (
1622 &gEfiFirmwareFileSystem2Guid,
1623 0,
1624 &OldDescriptor,
1625 &OldFfsFvPpi
1626 );
1627 ASSERT_EFI_ERROR (Status);
1628
1629 //
1630 // Re-install the EFI_PEI_FIRMWARE_VOLUME_PPI for build-in Ffs2
1631 // which is shadowed from flash to permanent memory within PeiCore image.
1632 //
1633 Status = PeiServicesReInstallPpi (OldDescriptor, &mPeiFfs2FvPpiList);
1634 ASSERT_EFI_ERROR (Status);
1635
1636 //
1637 // Fixup all FvPpi pointers for the implementation in flash to permanent memory.
1638 //
1639 for (Index = 0; Index < FixedPcdGet32 (PcdPeiCoreMaxFvSupported); Index ++) {
1640 if (PrivateData->Fv[Index].FvPpi == OldFfsFvPpi) {
1641 PrivateData->Fv[Index].FvPpi = &mPeiFfs2FwVol.Fv;
1642 }
1643 }
1644
1645 //
1646 // Locate old build-in Ffs3 EFI_PEI_FIRMWARE_VOLUME_PPI which
1647 // in flash.
1648 //
1649 Status = PeiServicesLocatePpi (
1650 &gEfiFirmwareFileSystem3Guid,
1651 0,
1652 &OldDescriptor,
1653 &OldFfsFvPpi
1654 );
1655 ASSERT_EFI_ERROR (Status);
1656
1657 //
1658 // Re-install the EFI_PEI_FIRMWARE_VOLUME_PPI for build-in Ffs3
1659 // which is shadowed from flash to permanent memory within PeiCore image.
1660 //
1661 Status = PeiServicesReInstallPpi (OldDescriptor, &mPeiFfs3FvPpiList);
1662 ASSERT_EFI_ERROR (Status);
1663
1664 //
1665 // Fixup all FvPpi pointers for the implementation in flash to permanent memory.
1666 //
1667 for (Index = 0; Index < FixedPcdGet32 (PcdPeiCoreMaxFvSupported); Index ++) {
1668 if (PrivateData->Fv[Index].FvPpi == OldFfsFvPpi) {
1669 PrivateData->Fv[Index].FvPpi = &mPeiFfs3FwVol.Fv;
1670 }
1671 }
1672 }
1673
1674 /**
1675 Report the information for a new discoveried FV in unknown third-party format.
1676
1677 If the EFI_PEI_FIRMWARE_VOLUME_PPI has not been installed for third-party FV format, but
1678 the FV in this format has been discoveried, then this FV's information will be cached into
1679 PEI_CORE_INSTANCE's UnknownFvInfo array.
1680 Also a notification would be installed for unknown third-party FV format guid, if EFI_PEI_FIRMWARE_VOLUME_PPI
1681 is installed later by platform's PEIM, the original unknown third-party FV will be processed by
1682 using new installed EFI_PEI_FIRMWARE_VOLUME_PPI.
1683
1684 @param PrivateData Point to instance of PEI_CORE_INSTANCE
1685 @param Format Point to the unknown third-party format guid.
1686 @param FvInfo Point to FvInfo buffer.
1687 @param FvInfoSize The size of FvInfo buffer.
1688
1689 @retval EFI_OUT_OF_RESOURCES The FV info array in PEI_CORE_INSTANCE has no more spaces.
1690 @retval EFI_SUCCESS Success to add the information for unknown FV.
1691 **/
1692 EFI_STATUS
1693 AddUnknownFormatFvInfo (
1694 IN PEI_CORE_INSTANCE *PrivateData,
1695 IN EFI_GUID *Format,
1696 IN VOID *FvInfo,
1697 IN UINT32 FvInfoSize
1698 )
1699 {
1700 PEI_CORE_UNKNOW_FORMAT_FV_INFO *NewUnknownFv;
1701
1702 if (PrivateData->UnknownFvInfoCount + 1 >= FixedPcdGet32 (PcdPeiCoreMaxFvSupported)) {
1703 return EFI_OUT_OF_RESOURCES;
1704 }
1705
1706 NewUnknownFv = &PrivateData->UnknownFvInfo[PrivateData->UnknownFvInfoCount];
1707 PrivateData->UnknownFvInfoCount ++;
1708
1709 CopyGuid (&NewUnknownFv->FvFormat, Format);
1710 NewUnknownFv->FvInfo = FvInfo;
1711 NewUnknownFv->FvInfoSize = FvInfoSize;
1712 NewUnknownFv->NotifyDescriptor.Flags = (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST);
1713 NewUnknownFv->NotifyDescriptor.Guid = &NewUnknownFv->FvFormat;
1714 NewUnknownFv->NotifyDescriptor.Notify = ThirdPartyFvPpiNotifyCallback;
1715
1716 PeiServicesNotifyPpi (&NewUnknownFv->NotifyDescriptor);
1717 return EFI_SUCCESS;
1718 }
1719
1720 /**
1721 Find the FV information according to third-party FV format guid.
1722
1723 This routine also will remove the FV information found by given FV format guid from
1724 PrivateData->UnknownFvInfo[].
1725
1726 @param PrivateData Point to instance of PEI_CORE_INSTANCE
1727 @param Format Point to given FV format guid
1728 @param FvInfo On return, the pointer of FV information buffer
1729 @param FvInfoSize On return, the size of FV information buffer.
1730
1731 @retval EFI_NOT_FOUND The FV is not found for new installed EFI_PEI_FIRMWARE_VOLUME_PPI
1732 @retval EFI_SUCCESS Success to find a FV which could be processed by new installed EFI_PEI_FIRMWARE_VOLUME_PPI.
1733 **/
1734 EFI_STATUS
1735 FindUnknownFormatFvInfo (
1736 IN PEI_CORE_INSTANCE *PrivateData,
1737 IN EFI_GUID *Format,
1738 OUT VOID **FvInfo,
1739 OUT UINT32 *FvInfoSize
1740 )
1741 {
1742 UINTN Index;
1743 UINTN Index2;
1744
1745 Index = 0;
1746 for (; Index < PrivateData->UnknownFvInfoCount; Index ++) {
1747 if (CompareGuid (Format, &PrivateData->UnknownFvInfo[Index].FvFormat)) {
1748 break;
1749 }
1750 }
1751
1752 if (Index == PrivateData->UnknownFvInfoCount) {
1753 return EFI_NOT_FOUND;
1754 }
1755
1756 *FvInfo = PrivateData->UnknownFvInfo[Index].FvInfo;
1757 *FvInfoSize = PrivateData->UnknownFvInfo[Index].FvInfoSize;
1758
1759 //
1760 // Remove an entry from UnknownFvInfo array.
1761 //
1762 Index2 = Index + 1;
1763 for (;Index2 < PrivateData->UnknownFvInfoCount; Index2 ++, Index ++) {
1764 CopyMem (&PrivateData->UnknownFvInfo[Index], &PrivateData->UnknownFvInfo[Index2], sizeof (PEI_CORE_UNKNOW_FORMAT_FV_INFO));
1765 }
1766 PrivateData->UnknownFvInfoCount --;
1767 return EFI_SUCCESS;
1768 }
1769
1770 /**
1771 Notification callback function for EFI_PEI_FIRMWARE_VOLUME_PPI.
1772
1773 When a EFI_PEI_FIRMWARE_VOLUME_PPI is installed to support new FV format, this
1774 routine is called to process all discoveried FVs in this format.
1775
1776 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
1777 @param NotifyDescriptor Address of the notification descriptor data structure.
1778 @param Ppi Address of the PPI that was installed.
1779
1780 @retval EFI_SUCCESS The notification callback is processed correctly.
1781 **/
1782 EFI_STATUS
1783 EFIAPI
1784 ThirdPartyFvPpiNotifyCallback (
1785 IN EFI_PEI_SERVICES **PeiServices,
1786 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
1787 IN VOID *Ppi
1788 )
1789 {
1790 PEI_CORE_INSTANCE *PrivateData;
1791 EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi;
1792 VOID *FvInfo;
1793 UINT32 FvInfoSize;
1794 EFI_STATUS Status;
1795 EFI_PEI_FV_HANDLE FvHandle;
1796 BOOLEAN IsProcessed;
1797 UINTN FvIndex;
1798 EFI_PEI_FILE_HANDLE FileHandle;
1799 VOID *DepexData;
1800
1801 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);
1802 FvPpi = (EFI_PEI_FIRMWARE_VOLUME_PPI*) Ppi;
1803
1804 do {
1805 Status = FindUnknownFormatFvInfo (PrivateData, NotifyDescriptor->Guid, &FvInfo, &FvInfoSize);
1806 if (EFI_ERROR (Status)) {
1807 return EFI_SUCCESS;
1808 }
1809
1810 //
1811 // Process new found FV and get FV handle.
1812 //
1813 Status = FvPpi->ProcessVolume (FvPpi, FvInfo, FvInfoSize, &FvHandle);
1814 if (EFI_ERROR (Status)) {
1815 DEBUG ((EFI_D_ERROR, "Fail to process the FV 0x%p, FV may be corrupted!\n", FvInfo));
1816 continue;
1817 }
1818
1819 //
1820 // Check whether the FV has already been processed.
1821 //
1822 IsProcessed = FALSE;
1823 for (FvIndex = 0; FvIndex < PrivateData->FvCount; FvIndex ++) {
1824 if (PrivateData->Fv[FvIndex].FvHandle == FvHandle) {
1825 DEBUG ((EFI_D_INFO, "The Fv %p has already been processed!\n", FvInfo));
1826 IsProcessed = TRUE;
1827 break;
1828 }
1829 }
1830
1831 if (IsProcessed) {
1832 continue;
1833 }
1834
1835 if (PrivateData->FvCount >= FixedPcdGet32 (PcdPeiCoreMaxFvSupported)) {
1836 DEBUG ((EFI_D_ERROR, "The number of Fv Images (%d) exceed the max supported FVs (%d) in Pei", PrivateData->FvCount + 1, FixedPcdGet32 (PcdPeiCoreMaxFvSupported)));
1837 DEBUG ((EFI_D_ERROR, "PcdPeiCoreMaxFvSupported value need be reconfigurated in DSC"));
1838 ASSERT (FALSE);
1839 }
1840
1841 //
1842 // Update internal PEI_CORE_FV array.
1843 //
1844 PrivateData->Fv[PrivateData->FvCount].FvHeader = (EFI_FIRMWARE_VOLUME_HEADER*) FvInfo;
1845 PrivateData->Fv[PrivateData->FvCount].FvPpi = FvPpi;
1846 PrivateData->Fv[PrivateData->FvCount].FvHandle = FvHandle;
1847 DEBUG ((
1848 EFI_D_INFO,
1849 "The %dth FV start address is 0x%11p, size is 0x%08x, handle is 0x%p\n",
1850 (UINT32) PrivateData->FvCount,
1851 (VOID *) FvInfo,
1852 FvInfoSize,
1853 FvHandle
1854 ));
1855 PrivateData->FvCount ++;
1856
1857 //
1858 // Scan and process the new discoveried FV for EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
1859 //
1860 FileHandle = NULL;
1861 do {
1862 Status = FvPpi->FindFileByType (
1863 FvPpi,
1864 EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE,
1865 FvHandle,
1866 &FileHandle
1867 );
1868 if (!EFI_ERROR (Status)) {
1869 Status = FvPpi->FindSectionByType (
1870 FvPpi,
1871 EFI_SECTION_PEI_DEPEX,
1872 FileHandle,
1873 (VOID**)&DepexData
1874 );
1875 if (!EFI_ERROR (Status)) {
1876 if (!PeimDispatchReadiness (PeiServices, DepexData)) {
1877 //
1878 // Dependency is not satisfied.
1879 //
1880 continue;
1881 }
1882 }
1883
1884 DEBUG ((EFI_D_INFO, "Found firmware volume Image File %p in FV[%d] %p\n", FileHandle, PrivateData->FvCount - 1, FvHandle));
1885 ProcessFvFile (&PrivateData->Fv[PrivateData->FvCount - 1], FileHandle);
1886 }
1887 } while (FileHandle != NULL);
1888 } while (TRUE);
1889 }