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