]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Pei/FwVol/FwVol.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Core / Pei / FwVol / FwVol.c
1 /** @file
2 Pei Core Firmware File System service routines.
3
4 Copyright (c) 2015 HP Development Company, L.P.
5 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include "FwVol.h"
11
12 EFI_PEI_NOTIFY_DESCRIPTOR mNotifyOnFvInfoList[] = {
13 {
14 EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK,
15 &gEfiPeiFirmwareVolumeInfoPpiGuid,
16 FirmwareVolmeInfoPpiNotifyCallback
17 },
18 {
19 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
20 &gEfiPeiFirmwareVolumeInfo2PpiGuid,
21 FirmwareVolmeInfoPpiNotifyCallback
22 }
23 };
24
25 PEI_FW_VOL_INSTANCE mPeiFfs2FwVol = {
26 PEI_FW_VOL_SIGNATURE,
27 FALSE,
28 {
29 PeiFfsFvPpiProcessVolume,
30 PeiFfsFvPpiFindFileByType,
31 PeiFfsFvPpiFindFileByName,
32 PeiFfsFvPpiGetFileInfo,
33 PeiFfsFvPpiGetVolumeInfo,
34 PeiFfsFvPpiFindSectionByType,
35 PeiFfsFvPpiGetFileInfo2,
36 PeiFfsFvPpiFindSectionByType2,
37 EFI_PEI_FIRMWARE_VOLUME_PPI_SIGNATURE,
38 EFI_PEI_FIRMWARE_VOLUME_PPI_REVISION
39 }
40 };
41
42 PEI_FW_VOL_INSTANCE mPeiFfs3FwVol = {
43 PEI_FW_VOL_SIGNATURE,
44 TRUE,
45 {
46 PeiFfsFvPpiProcessVolume,
47 PeiFfsFvPpiFindFileByType,
48 PeiFfsFvPpiFindFileByName,
49 PeiFfsFvPpiGetFileInfo,
50 PeiFfsFvPpiGetVolumeInfo,
51 PeiFfsFvPpiFindSectionByType,
52 PeiFfsFvPpiGetFileInfo2,
53 PeiFfsFvPpiFindSectionByType2,
54 EFI_PEI_FIRMWARE_VOLUME_PPI_SIGNATURE,
55 EFI_PEI_FIRMWARE_VOLUME_PPI_REVISION
56 }
57 };
58
59 EFI_PEI_PPI_DESCRIPTOR mPeiFfs2FvPpiList = {
60 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
61 &gEfiFirmwareFileSystem2Guid,
62 &mPeiFfs2FwVol.Fv
63 };
64
65 EFI_PEI_PPI_DESCRIPTOR mPeiFfs3FvPpiList = {
66 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
67 &gEfiFirmwareFileSystem3Guid,
68 &mPeiFfs3FwVol.Fv
69 };
70
71 /**
72 Required Alignment Alignment Value in FFS FFS_ATTRIB_DATA_ALIGNMENT2 Alignment Value in
73 (bytes) Attributes Field in FFS Attributes Field Firmware Volume Interfaces
74 1 0 0 0
75 16 1 0 4
76 128 2 0 7
77 512 3 0 9
78 1 KB 4 0 10
79 4 KB 5 0 12
80 32 KB 6 0 15
81 64 KB 7 0 16
82 128 KB 0 1 17
83 256 KB 1 1 18
84 512 KB 2 1 19
85 1 MB 3 1 20
86 2 MB 4 1 21
87 4 MB 5 1 22
88 8 MB 6 1 23
89 16 MB 7 1 24
90 **/
91 UINT8 mFvAttributes[] = {0, 4, 7, 9, 10, 12, 15, 16};
92 UINT8 mFvAttributes2[] = {17, 18, 19, 20, 21, 22, 23, 24};
93
94 /**
95 Convert the FFS File Attributes to FV File Attributes
96
97 @param FfsAttributes The attributes of UINT8 type.
98
99 @return The attributes of EFI_FV_FILE_ATTRIBUTES
100
101 **/
102 EFI_FV_FILE_ATTRIBUTES
103 FfsAttributes2FvFileAttributes (
104 IN EFI_FFS_FILE_ATTRIBUTES FfsAttributes
105 )
106 {
107 UINT8 DataAlignment;
108 EFI_FV_FILE_ATTRIBUTES FileAttribute;
109
110 DataAlignment = (UINT8) ((FfsAttributes & FFS_ATTRIB_DATA_ALIGNMENT) >> 3);
111 ASSERT (DataAlignment < 8);
112
113 if ((FfsAttributes & FFS_ATTRIB_DATA_ALIGNMENT_2) != 0) {
114 FileAttribute = (EFI_FV_FILE_ATTRIBUTES) mFvAttributes2[DataAlignment];
115 } else {
116 FileAttribute = (EFI_FV_FILE_ATTRIBUTES) mFvAttributes[DataAlignment];
117 }
118
119 if ((FfsAttributes & FFS_ATTRIB_FIXED) == FFS_ATTRIB_FIXED) {
120 FileAttribute |= EFI_FV_FILE_ATTRIB_FIXED;
121 }
122
123 return FileAttribute;
124 }
125
126 /**
127 Returns the file state set by the highest zero bit in the State field
128
129 @param ErasePolarity Erase Polarity as defined by EFI_FVB2_ERASE_POLARITY
130 in the Attributes field.
131 @param FfsHeader Pointer to FFS File Header.
132
133 @retval EFI_FFS_FILE_STATE File state is set by the highest none zero bit
134 in the header State field.
135 **/
136 EFI_FFS_FILE_STATE
137 GetFileState(
138 IN UINT8 ErasePolarity,
139 IN EFI_FFS_FILE_HEADER *FfsHeader
140 )
141 {
142 EFI_FFS_FILE_STATE FileState;
143 EFI_FFS_FILE_STATE HighestBit;
144
145 FileState = FfsHeader->State;
146
147 if (ErasePolarity != 0) {
148 FileState = (EFI_FFS_FILE_STATE)~FileState;
149 }
150
151 //
152 // Get file state set by its highest none zero bit.
153 //
154 HighestBit = 0x80;
155 while (HighestBit != 0 && (HighestBit & FileState) == 0) {
156 HighestBit >>= 1;
157 }
158
159 return HighestBit;
160 }
161
162 /**
163 Calculates the checksum of the header of a file.
164
165 @param FileHeader Pointer to FFS File Header.
166
167 @return Checksum of the header.
168 Zero means the header is good.
169 Non-zero means the header is bad.
170 **/
171 UINT8
172 CalculateHeaderChecksum (
173 IN EFI_FFS_FILE_HEADER *FileHeader
174 )
175 {
176 EFI_FFS_FILE_HEADER2 TestFileHeader;
177
178 if (IS_FFS_FILE2 (FileHeader)) {
179 CopyMem (&TestFileHeader, FileHeader, sizeof (EFI_FFS_FILE_HEADER2));
180 //
181 // Ingore State and File field in FFS header.
182 //
183 TestFileHeader.State = 0;
184 TestFileHeader.IntegrityCheck.Checksum.File = 0;
185
186 return CalculateSum8 ((CONST UINT8 *) &TestFileHeader, sizeof (EFI_FFS_FILE_HEADER2));
187 } else {
188 CopyMem (&TestFileHeader, FileHeader, sizeof (EFI_FFS_FILE_HEADER));
189 //
190 // Ingore State and File field in FFS header.
191 //
192 TestFileHeader.State = 0;
193 TestFileHeader.IntegrityCheck.Checksum.File = 0;
194
195 return CalculateSum8 ((CONST UINT8 *) &TestFileHeader, sizeof (EFI_FFS_FILE_HEADER));
196 }
197 }
198
199 /**
200 Find FV handler according to FileHandle in that FV.
201
202 @param FileHandle Handle of file image
203
204 @return Pointer to instance of PEI_CORE_FV_HANDLE.
205 **/
206 PEI_CORE_FV_HANDLE*
207 FileHandleToVolume (
208 IN EFI_PEI_FILE_HANDLE FileHandle
209 )
210 {
211 UINTN Index;
212 PEI_CORE_INSTANCE *PrivateData;
213 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
214 UINTN BestIndex;
215
216 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());
217 BestIndex = PrivateData->FvCount;
218
219 //
220 // Find the best matched FV image that includes this FileHandle.
221 // FV may include the child FV, and they are in the same continuous space.
222 // If FileHandle is from the child FV, the updated logic can find its matched FV.
223 //
224 for (Index = 0; Index < PrivateData->FvCount; Index++) {
225 FwVolHeader = PrivateData->Fv[Index].FvHeader;
226 if (((UINT64) (UINTN) FileHandle > (UINT64) (UINTN) FwVolHeader ) && \
227 ((UINT64) (UINTN) FileHandle <= ((UINT64) (UINTN) FwVolHeader + FwVolHeader->FvLength - 1))) {
228 if (BestIndex == PrivateData->FvCount) {
229 BestIndex = Index;
230 } else {
231 if ((UINT64) (UINTN) PrivateData->Fv[BestIndex].FvHeader < (UINT64) (UINTN) FwVolHeader) {
232 BestIndex = Index;
233 }
234 }
235 }
236 }
237
238 if (BestIndex < PrivateData->FvCount) {
239 return &PrivateData->Fv[BestIndex];
240 }
241
242 return NULL;
243 }
244
245 /**
246 Given the input file pointer, search for the first matching file in the
247 FFS volume as defined by SearchType. The search starts from FileHeader inside
248 the Firmware Volume defined by FwVolHeader.
249 If SearchType is EFI_FV_FILETYPE_ALL, the first FFS file will return without check its file type.
250 If SearchType is PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE,
251 the first PEIM, or COMBINED PEIM or FV file type FFS file will return.
252
253 @param FvHandle Pointer to the FV header of the volume to search
254 @param FileName File name
255 @param SearchType Filter to find only files of this type.
256 Type EFI_FV_FILETYPE_ALL causes no filtering to be done.
257 @param FileHandle This parameter must point to a valid FFS volume.
258 @param AprioriFile Pointer to AprioriFile image in this FV if has
259
260 @return EFI_NOT_FOUND No files matching the search criteria were found
261 @retval EFI_SUCCESS Success to search given file
262
263 **/
264 EFI_STATUS
265 FindFileEx (
266 IN CONST EFI_PEI_FV_HANDLE FvHandle,
267 IN CONST EFI_GUID *FileName, OPTIONAL
268 IN EFI_FV_FILETYPE SearchType,
269 IN OUT EFI_PEI_FILE_HANDLE *FileHandle,
270 IN OUT EFI_PEI_FILE_HANDLE *AprioriFile OPTIONAL
271 )
272 {
273 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
274 EFI_FIRMWARE_VOLUME_EXT_HEADER *FwVolExtHeader;
275 EFI_FFS_FILE_HEADER **FileHeader;
276 EFI_FFS_FILE_HEADER *FfsFileHeader;
277 UINT32 FileLength;
278 UINT32 FileOccupiedSize;
279 UINT32 FileOffset;
280 UINT64 FvLength;
281 UINT8 ErasePolarity;
282 UINT8 FileState;
283 UINT8 DataCheckSum;
284 BOOLEAN IsFfs3Fv;
285
286 //
287 // Convert the handle of FV to FV header for memory-mapped firmware volume
288 //
289 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) FvHandle;
290 FileHeader = (EFI_FFS_FILE_HEADER **)FileHandle;
291
292 IsFfs3Fv = CompareGuid (&FwVolHeader->FileSystemGuid, &gEfiFirmwareFileSystem3Guid);
293
294 FvLength = FwVolHeader->FvLength;
295 if ((FwVolHeader->Attributes & EFI_FVB2_ERASE_POLARITY) != 0) {
296 ErasePolarity = 1;
297 } else {
298 ErasePolarity = 0;
299 }
300
301 //
302 // If FileHeader is not specified (NULL) or FileName is not NULL,
303 // start with the first file in the firmware volume. Otherwise,
304 // start from the FileHeader.
305 //
306 if ((*FileHeader == NULL) || (FileName != NULL)) {
307 if (FwVolHeader->ExtHeaderOffset != 0) {
308 //
309 // Searching for files starts on an 8 byte aligned boundary after the end of the Extended Header if it exists.
310 //
311 FwVolExtHeader = (EFI_FIRMWARE_VOLUME_EXT_HEADER *) ((UINT8 *) FwVolHeader + FwVolHeader->ExtHeaderOffset);
312 FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FwVolExtHeader + FwVolExtHeader->ExtHeaderSize);
313 } else {
314 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *) FwVolHeader + FwVolHeader->HeaderLength);
315 }
316 FfsFileHeader = (EFI_FFS_FILE_HEADER *) ALIGN_POINTER (FfsFileHeader, 8);
317 } else {
318 if (IS_FFS_FILE2 (*FileHeader)) {
319 if (!IsFfs3Fv) {
320 DEBUG ((EFI_D_ERROR, "It is a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &(*FileHeader)->Name));
321 }
322 FileLength = FFS_FILE2_SIZE (*FileHeader);
323 ASSERT (FileLength > 0x00FFFFFF);
324 } else {
325 FileLength = FFS_FILE_SIZE (*FileHeader);
326 }
327 //
328 // FileLength is adjusted to FileOccupiedSize as it is 8 byte aligned.
329 //
330 FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
331 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)*FileHeader + FileOccupiedSize);
332 }
333
334 FileOffset = (UINT32) ((UINT8 *)FfsFileHeader - (UINT8 *)FwVolHeader);
335 ASSERT (FileOffset <= 0xFFFFFFFF);
336
337 while (FileOffset < (FvLength - sizeof (EFI_FFS_FILE_HEADER))) {
338 //
339 // Get FileState which is the highest bit of the State
340 //
341 FileState = GetFileState (ErasePolarity, FfsFileHeader);
342 switch (FileState) {
343
344 case EFI_FILE_HEADER_CONSTRUCTION:
345 case EFI_FILE_HEADER_INVALID:
346 if (IS_FFS_FILE2 (FfsFileHeader)) {
347 if (!IsFfs3Fv) {
348 DEBUG ((EFI_D_ERROR, "Found a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &FfsFileHeader->Name));
349 }
350 FileOffset += sizeof (EFI_FFS_FILE_HEADER2);
351 FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER2));
352 } else {
353 FileOffset += sizeof (EFI_FFS_FILE_HEADER);
354 FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER));
355 }
356 break;
357
358 case EFI_FILE_DATA_VALID:
359 case EFI_FILE_MARKED_FOR_UPDATE:
360 if (CalculateHeaderChecksum (FfsFileHeader) != 0) {
361 ASSERT (FALSE);
362 *FileHeader = NULL;
363 return EFI_NOT_FOUND;
364 }
365
366 if (IS_FFS_FILE2 (FfsFileHeader)) {
367 FileLength = FFS_FILE2_SIZE (FfsFileHeader);
368 ASSERT (FileLength > 0x00FFFFFF);
369 FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
370 if (!IsFfs3Fv) {
371 DEBUG ((EFI_D_ERROR, "Found a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &FfsFileHeader->Name));
372 FileOffset += FileOccupiedSize;
373 FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsFileHeader + FileOccupiedSize);
374 break;
375 }
376 } else {
377 FileLength = FFS_FILE_SIZE (FfsFileHeader);
378 FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
379 }
380
381 DataCheckSum = FFS_FIXED_CHECKSUM;
382 if ((FfsFileHeader->Attributes & FFS_ATTRIB_CHECKSUM) == FFS_ATTRIB_CHECKSUM) {
383 if (IS_FFS_FILE2 (FfsFileHeader)) {
384 DataCheckSum = CalculateCheckSum8 ((CONST UINT8 *) FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER2), FileLength - sizeof(EFI_FFS_FILE_HEADER2));
385 } else {
386 DataCheckSum = CalculateCheckSum8 ((CONST UINT8 *) FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER), FileLength - sizeof(EFI_FFS_FILE_HEADER));
387 }
388 }
389 if (FfsFileHeader->IntegrityCheck.Checksum.File != DataCheckSum) {
390 ASSERT (FALSE);
391 *FileHeader = NULL;
392 return EFI_NOT_FOUND;
393 }
394
395 if (FileName != NULL) {
396 if (CompareGuid (&FfsFileHeader->Name, (EFI_GUID*)FileName)) {
397 *FileHeader = FfsFileHeader;
398 return EFI_SUCCESS;
399 }
400 } else if (SearchType == PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE) {
401 if ((FfsFileHeader->Type == EFI_FV_FILETYPE_PEIM) ||
402 (FfsFileHeader->Type == EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER) ||
403 (FfsFileHeader->Type == EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE)) {
404
405 *FileHeader = FfsFileHeader;
406 return EFI_SUCCESS;
407 } else if (AprioriFile != NULL) {
408 if (FfsFileHeader->Type == EFI_FV_FILETYPE_FREEFORM) {
409 if (CompareGuid (&FfsFileHeader->Name, &gPeiAprioriFileNameGuid)) {
410 *AprioriFile = FfsFileHeader;
411 }
412 }
413 }
414 } else if (((SearchType == FfsFileHeader->Type) || (SearchType == EFI_FV_FILETYPE_ALL)) &&
415 (FfsFileHeader->Type != EFI_FV_FILETYPE_FFS_PAD)) {
416 *FileHeader = FfsFileHeader;
417 return EFI_SUCCESS;
418 }
419
420 FileOffset += FileOccupiedSize;
421 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize);
422 break;
423
424 case EFI_FILE_DELETED:
425 if (IS_FFS_FILE2 (FfsFileHeader)) {
426 if (!IsFfs3Fv) {
427 DEBUG ((EFI_D_ERROR, "Found a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &FfsFileHeader->Name));
428 }
429 FileLength = FFS_FILE2_SIZE (FfsFileHeader);
430 ASSERT (FileLength > 0x00FFFFFF);
431 } else {
432 FileLength = FFS_FILE_SIZE (FfsFileHeader);
433 }
434 FileOccupiedSize = GET_OCCUPIED_SIZE(FileLength, 8);
435 FileOffset += FileOccupiedSize;
436 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize);
437 break;
438
439 default:
440 *FileHeader = NULL;
441 return EFI_NOT_FOUND;
442 }
443 }
444
445 *FileHeader = NULL;
446 return EFI_NOT_FOUND;
447 }
448
449 /**
450 Initialize PeiCore Fv List.
451
452 @param PrivateData - Pointer to PEI_CORE_INSTANCE.
453 @param SecCoreData - Pointer to EFI_SEC_PEI_HAND_OFF.
454 **/
455 VOID
456 PeiInitializeFv (
457 IN PEI_CORE_INSTANCE *PrivateData,
458 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData
459 )
460 {
461 EFI_STATUS Status;
462 EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi;
463 EFI_PEI_FV_HANDLE FvHandle;
464 EFI_FIRMWARE_VOLUME_HEADER *BfvHeader;
465
466 //
467 // Install FV_PPI for FFS2 file system.
468 //
469 PeiServicesInstallPpi (&mPeiFfs2FvPpiList);
470
471 //
472 // Install FV_PPI for FFS3 file system.
473 //
474 PeiServicesInstallPpi (&mPeiFfs3FvPpiList);
475
476 BfvHeader = (EFI_FIRMWARE_VOLUME_HEADER *)SecCoreData->BootFirmwareVolumeBase;
477
478 //
479 // The FV_PPI in BFV's format should be installed.
480 //
481 Status = PeiServicesLocatePpi (
482 &BfvHeader->FileSystemGuid,
483 0,
484 NULL,
485 (VOID**)&FvPpi
486 );
487 ASSERT_EFI_ERROR (Status);
488
489 //
490 // Get handle of BFV
491 //
492 Status = FvPpi->ProcessVolume (
493 FvPpi,
494 SecCoreData->BootFirmwareVolumeBase,
495 (UINTN)BfvHeader->FvLength,
496 &FvHandle
497 );
498 ASSERT_EFI_ERROR (Status);
499
500 PrivateData->Fv = AllocateZeroPool (sizeof (PEI_CORE_FV_HANDLE) * FV_GROWTH_STEP);
501 ASSERT (PrivateData->Fv != NULL);
502 PrivateData->MaxFvCount = FV_GROWTH_STEP;
503
504 //
505 // Update internal PEI_CORE_FV array.
506 //
507 PrivateData->Fv[PrivateData->FvCount].FvHeader = BfvHeader;
508 PrivateData->Fv[PrivateData->FvCount].FvPpi = FvPpi;
509 PrivateData->Fv[PrivateData->FvCount].FvHandle = FvHandle;
510 PrivateData->Fv[PrivateData->FvCount].AuthenticationStatus = 0;
511 DEBUG ((
512 EFI_D_INFO,
513 "The %dth FV start address is 0x%11p, size is 0x%08x, handle is 0x%p\n",
514 (UINT32) PrivateData->FvCount,
515 (VOID *) BfvHeader,
516 (UINT32) BfvHeader->FvLength,
517 FvHandle
518 ));
519 PrivateData->FvCount ++;
520
521 //
522 // Post a call-back for the FvInfoPPI and FvInfo2PPI services to expose
523 // additional Fvs to PeiCore.
524 //
525 Status = PeiServicesNotifyPpi (mNotifyOnFvInfoList);
526 ASSERT_EFI_ERROR (Status);
527
528 }
529
530 /**
531 Process Firmware Volum Information once FvInfoPPI or FvInfo2PPI install.
532 The FV Info will be registered into PeiCore private data structure.
533 And search the inside FV image, if found, the new FV INFO(2) PPI will be installed.
534
535 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
536 @param NotifyDescriptor Address of the notification descriptor data structure.
537 @param Ppi Address of the PPI that was installed.
538
539 @retval EFI_SUCCESS The FV Info is registered into PeiCore private data structure.
540 @return if not EFI_SUCESS, fail to verify FV.
541
542 **/
543 EFI_STATUS
544 EFIAPI
545 FirmwareVolmeInfoPpiNotifyCallback (
546 IN EFI_PEI_SERVICES **PeiServices,
547 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
548 IN VOID *Ppi
549 )
550 {
551 EFI_PEI_FIRMWARE_VOLUME_INFO2_PPI FvInfo2Ppi;
552 EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi;
553 PEI_CORE_INSTANCE *PrivateData;
554 EFI_STATUS Status;
555 EFI_PEI_FV_HANDLE FvHandle;
556 UINTN FvIndex;
557 EFI_PEI_FILE_HANDLE FileHandle;
558 VOID *DepexData;
559 BOOLEAN IsFvInfo2;
560 UINTN CurFvCount;
561 VOID *TempPtr;
562
563 Status = EFI_SUCCESS;
564 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);
565
566 if (CompareGuid (NotifyDescriptor->Guid, &gEfiPeiFirmwareVolumeInfo2PpiGuid)) {
567 //
568 // It is FvInfo2PPI.
569 //
570 CopyMem (&FvInfo2Ppi, Ppi, sizeof (EFI_PEI_FIRMWARE_VOLUME_INFO2_PPI));
571 IsFvInfo2 = TRUE;
572 } else {
573 //
574 // It is FvInfoPPI.
575 //
576 CopyMem (&FvInfo2Ppi, Ppi, sizeof (EFI_PEI_FIRMWARE_VOLUME_INFO_PPI));
577 FvInfo2Ppi.AuthenticationStatus = 0;
578 IsFvInfo2 = FALSE;
579 }
580
581 if (CompareGuid (&FvInfo2Ppi.FvFormat, &gEfiFirmwareFileSystem2Guid)) {
582 //
583 // gEfiFirmwareFileSystem2Guid is specified for FvFormat, then here to check the
584 // FileSystemGuid pointed by FvInfo against gEfiFirmwareFileSystem2Guid to make sure
585 // FvInfo has the firmware file system 2 format.
586 //
587 // If the ASSERT really appears, FvFormat needs to be specified correctly, for example,
588 // gEfiFirmwareFileSystem3Guid can be used for firmware file system 3 format, or
589 // ((EFI_FIRMWARE_VOLUME_HEADER *) FvInfo)->FileSystemGuid can be just used for both
590 // firmware file system 2 and 3 format.
591 //
592 ASSERT (CompareGuid (&(((EFI_FIRMWARE_VOLUME_HEADER *) FvInfo2Ppi.FvInfo)->FileSystemGuid), &gEfiFirmwareFileSystem2Guid));
593 }
594
595 //
596 // Locate the corresponding FV_PPI according to founded FV's format guid
597 //
598 Status = PeiServicesLocatePpi (
599 &FvInfo2Ppi.FvFormat,
600 0,
601 NULL,
602 (VOID**)&FvPpi
603 );
604 if (!EFI_ERROR (Status)) {
605 //
606 // Process new found FV and get FV handle.
607 //
608 Status = FvPpi->ProcessVolume (FvPpi, FvInfo2Ppi.FvInfo, FvInfo2Ppi.FvInfoSize, &FvHandle);
609 if (EFI_ERROR (Status)) {
610 DEBUG ((EFI_D_ERROR, "Fail to process new found FV, FV may be corrupted!\n"));
611 return Status;
612 }
613
614 //
615 // Check whether the FV has already been processed.
616 //
617 for (FvIndex = 0; FvIndex < PrivateData->FvCount; FvIndex ++) {
618 if (PrivateData->Fv[FvIndex].FvHandle == FvHandle) {
619 if (IsFvInfo2 && (FvInfo2Ppi.AuthenticationStatus != PrivateData->Fv[FvIndex].AuthenticationStatus)) {
620 PrivateData->Fv[FvIndex].AuthenticationStatus = FvInfo2Ppi.AuthenticationStatus;
621 DEBUG ((EFI_D_INFO, "Update AuthenticationStatus of the %dth FV to 0x%x!\n", FvIndex, FvInfo2Ppi.AuthenticationStatus));
622 }
623 DEBUG ((EFI_D_INFO, "The Fv %p has already been processed!\n", FvInfo2Ppi.FvInfo));
624 return EFI_SUCCESS;
625 }
626 }
627
628 if (PrivateData->FvCount >= PrivateData->MaxFvCount) {
629 //
630 // Run out of room, grow the buffer.
631 //
632 TempPtr = AllocateZeroPool (
633 sizeof (PEI_CORE_FV_HANDLE) * (PrivateData->MaxFvCount + FV_GROWTH_STEP)
634 );
635 ASSERT (TempPtr != NULL);
636 CopyMem (
637 TempPtr,
638 PrivateData->Fv,
639 sizeof (PEI_CORE_FV_HANDLE) * PrivateData->MaxFvCount
640 );
641 PrivateData->Fv = TempPtr;
642 PrivateData->MaxFvCount = PrivateData->MaxFvCount + FV_GROWTH_STEP;
643 }
644
645 //
646 // Update internal PEI_CORE_FV array.
647 //
648 PrivateData->Fv[PrivateData->FvCount].FvHeader = (EFI_FIRMWARE_VOLUME_HEADER*) FvInfo2Ppi.FvInfo;
649 PrivateData->Fv[PrivateData->FvCount].FvPpi = FvPpi;
650 PrivateData->Fv[PrivateData->FvCount].FvHandle = FvHandle;
651 PrivateData->Fv[PrivateData->FvCount].AuthenticationStatus = FvInfo2Ppi.AuthenticationStatus;
652 CurFvCount = PrivateData->FvCount;
653 DEBUG ((
654 EFI_D_INFO,
655 "The %dth FV start address is 0x%11p, size is 0x%08x, handle is 0x%p\n",
656 (UINT32) CurFvCount,
657 (VOID *) FvInfo2Ppi.FvInfo,
658 FvInfo2Ppi.FvInfoSize,
659 FvHandle
660 ));
661 PrivateData->FvCount ++;
662
663 //
664 // Scan and process the new discoveried FV for EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
665 //
666 FileHandle = NULL;
667 do {
668 Status = FvPpi->FindFileByType (
669 FvPpi,
670 EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE,
671 FvHandle,
672 &FileHandle
673 );
674 if (!EFI_ERROR (Status)) {
675 Status = FvPpi->FindSectionByType (
676 FvPpi,
677 EFI_SECTION_PEI_DEPEX,
678 FileHandle,
679 (VOID**)&DepexData
680 );
681 if (!EFI_ERROR (Status)) {
682 if (!PeimDispatchReadiness (PeiServices, DepexData)) {
683 //
684 // Dependency is not satisfied.
685 //
686 continue;
687 }
688 }
689
690 DEBUG ((EFI_D_INFO, "Found firmware volume Image File %p in FV[%d] %p\n", FileHandle, CurFvCount, FvHandle));
691 ProcessFvFile (PrivateData, &PrivateData->Fv[CurFvCount], FileHandle);
692 }
693 } while (FileHandle != NULL);
694 } else {
695 DEBUG ((EFI_D_ERROR, "Fail to process FV %p because no corresponding EFI_FIRMWARE_VOLUME_PPI is found!\n", FvInfo2Ppi.FvInfo));
696
697 AddUnknownFormatFvInfo (PrivateData, &FvInfo2Ppi);
698 }
699
700 return EFI_SUCCESS;
701 }
702
703 /**
704 Verify the Guided Section GUID by checking if there is the Guided Section GUID HOB recorded the GUID itself.
705
706 @param GuidedSectionGuid The Guided Section GUID.
707 @param GuidedSectionExtraction A pointer to the pointer to the supported Guided Section Extraction Ppi
708 for the Guided Section.
709
710 @return TRUE The GuidedSectionGuid could be identified, and the pointer to
711 the Guided Section Extraction Ppi will be returned to *GuidedSectionExtraction.
712 @return FALSE The GuidedSectionGuid could not be identified, or
713 the Guided Section Extraction Ppi has not been installed yet.
714
715 **/
716 BOOLEAN
717 VerifyGuidedSectionGuid (
718 IN EFI_GUID *GuidedSectionGuid,
719 OUT EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI **GuidedSectionExtraction
720 )
721 {
722 EFI_PEI_HOB_POINTERS Hob;
723 EFI_GUID *GuidRecorded;
724 VOID *Interface;
725 EFI_STATUS Status;
726
727 //
728 // Check if there is the Guided Section GUID HOB recorded the GUID itself.
729 //
730 Hob.Raw = GetFirstGuidHob (GuidedSectionGuid);
731 if (Hob.Raw != NULL) {
732 GuidRecorded = (EFI_GUID *) GET_GUID_HOB_DATA (Hob);
733 if (CompareGuid (GuidRecorded, GuidedSectionGuid)) {
734 //
735 // Found the recorded GuidedSectionGuid.
736 //
737 Status = PeiServicesLocatePpi (GuidedSectionGuid, 0, NULL, (VOID **) &Interface);
738 if (!EFI_ERROR (Status) && Interface != NULL) {
739 //
740 // Found the supported Guided Section Extraction Ppi for the Guided Section.
741 //
742 *GuidedSectionExtraction = (EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI *) Interface;
743 return TRUE;
744 }
745 return FALSE;
746 }
747 }
748
749 return FALSE;
750 }
751
752 /**
753 Go through the file to search SectionType section.
754 Search within encapsulation sections (compression and GUIDed) recursively,
755 until the match section is found.
756
757 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
758 @param SectionType Filter to find only section of this type.
759 @param SectionInstance Pointer to the filter to find the specific instance of section.
760 @param Section From where to search.
761 @param SectionSize The file size to search.
762 @param OutputBuffer A pointer to the discovered section, if successful.
763 NULL if section not found
764 @param AuthenticationStatus Updated upon return to point to the authentication status for this section.
765 @param IsFfs3Fv Indicates the FV format.
766
767 @return EFI_NOT_FOUND The match section is not found.
768 @return EFI_SUCCESS The match section is found.
769
770 **/
771 EFI_STATUS
772 ProcessSection (
773 IN CONST EFI_PEI_SERVICES **PeiServices,
774 IN EFI_SECTION_TYPE SectionType,
775 IN OUT UINTN *SectionInstance,
776 IN EFI_COMMON_SECTION_HEADER *Section,
777 IN UINTN SectionSize,
778 OUT VOID **OutputBuffer,
779 OUT UINT32 *AuthenticationStatus,
780 IN BOOLEAN IsFfs3Fv
781 )
782 {
783 EFI_STATUS Status;
784 UINT32 SectionLength;
785 UINT32 ParsedLength;
786 EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI *GuidSectionPpi;
787 EFI_PEI_DECOMPRESS_PPI *DecompressPpi;
788 VOID *PpiOutput;
789 UINTN PpiOutputSize;
790 UINTN Index;
791 UINT32 Authentication;
792 PEI_CORE_INSTANCE *PrivateData;
793 EFI_GUID *SectionDefinitionGuid;
794 BOOLEAN SectionCached;
795 VOID *TempOutputBuffer;
796 UINT32 TempAuthenticationStatus;
797 UINT16 GuidedSectionAttributes;
798
799 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);
800 *OutputBuffer = NULL;
801 ParsedLength = 0;
802 Index = 0;
803 Status = EFI_NOT_FOUND;
804 PpiOutput = NULL;
805 PpiOutputSize = 0;
806 while (ParsedLength < SectionSize) {
807
808 if (IS_SECTION2 (Section)) {
809 ASSERT (SECTION2_SIZE (Section) > 0x00FFFFFF);
810 if (!IsFfs3Fv) {
811 DEBUG ((EFI_D_ERROR, "Found a FFS3 formatted section in a non-FFS3 formatted FV.\n"));
812 SectionLength = SECTION2_SIZE (Section);
813 //
814 // SectionLength is adjusted it is 4 byte aligned.
815 // Go to the next section
816 //
817 SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);
818 ASSERT (SectionLength != 0);
819 ParsedLength += SectionLength;
820 Section = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) Section + SectionLength);
821 continue;
822 }
823 }
824
825 if (Section->Type == SectionType) {
826 //
827 // The type matches, so check the instance count to see if it's the one we want.
828 //
829 (*SectionInstance)--;
830 if (*SectionInstance == 0) {
831 //
832 // Got it!
833 //
834 if (IS_SECTION2 (Section)) {
835 *OutputBuffer = (VOID *)((UINT8 *) Section + sizeof (EFI_COMMON_SECTION_HEADER2));
836 } else {
837 *OutputBuffer = (VOID *)((UINT8 *) Section + sizeof (EFI_COMMON_SECTION_HEADER));
838 }
839 return EFI_SUCCESS;
840 } else {
841 if (IS_SECTION2 (Section)) {
842 SectionLength = SECTION2_SIZE (Section);
843 } else {
844 SectionLength = SECTION_SIZE (Section);
845 }
846 //
847 // SectionLength is adjusted it is 4 byte aligned.
848 // Go to the next section
849 //
850 SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);
851 ASSERT (SectionLength != 0);
852 ParsedLength += SectionLength;
853 Section = (EFI_COMMON_SECTION_HEADER *)((UINT8 *)Section + SectionLength);
854 continue;
855 }
856 } else if ((Section->Type == EFI_SECTION_GUID_DEFINED) || (Section->Type == EFI_SECTION_COMPRESSION)) {
857 //
858 // Check the encapsulated section is extracted into the cache data.
859 //
860 SectionCached = FALSE;
861 for (Index = 0; Index < PrivateData->CacheSection.AllSectionCount; Index ++) {
862 if (Section == PrivateData->CacheSection.Section[Index]) {
863 SectionCached = TRUE;
864 PpiOutput = PrivateData->CacheSection.SectionData[Index];
865 PpiOutputSize = PrivateData->CacheSection.SectionSize[Index];
866 Authentication = PrivateData->CacheSection.AuthenticationStatus[Index];
867 //
868 // Search section directly from the cache data.
869 //
870 TempAuthenticationStatus = 0;
871 Status = ProcessSection (
872 PeiServices,
873 SectionType,
874 SectionInstance,
875 PpiOutput,
876 PpiOutputSize,
877 &TempOutputBuffer,
878 &TempAuthenticationStatus,
879 IsFfs3Fv
880 );
881 if (!EFI_ERROR (Status)) {
882 *OutputBuffer = TempOutputBuffer;
883 *AuthenticationStatus = TempAuthenticationStatus | Authentication;
884 return EFI_SUCCESS;
885 }
886 }
887 }
888
889 //
890 // If SectionCached is TRUE, the section data has been cached and scanned.
891 //
892 if (!SectionCached) {
893 Status = EFI_NOT_FOUND;
894 Authentication = 0;
895 if (Section->Type == EFI_SECTION_GUID_DEFINED) {
896 if (IS_SECTION2 (Section)) {
897 SectionDefinitionGuid = &((EFI_GUID_DEFINED_SECTION2 *)Section)->SectionDefinitionGuid;
898 GuidedSectionAttributes = ((EFI_GUID_DEFINED_SECTION2 *)Section)->Attributes;
899 } else {
900 SectionDefinitionGuid = &((EFI_GUID_DEFINED_SECTION *)Section)->SectionDefinitionGuid;
901 GuidedSectionAttributes = ((EFI_GUID_DEFINED_SECTION *)Section)->Attributes;
902 }
903 if (VerifyGuidedSectionGuid (SectionDefinitionGuid, &GuidSectionPpi)) {
904 Status = GuidSectionPpi->ExtractSection (
905 GuidSectionPpi,
906 Section,
907 &PpiOutput,
908 &PpiOutputSize,
909 &Authentication
910 );
911 } else if ((GuidedSectionAttributes & EFI_GUIDED_SECTION_PROCESSING_REQUIRED) == 0) {
912 //
913 // Figure out the proper authentication status for GUIDED section without processing required
914 //
915 Status = EFI_SUCCESS;
916 if ((GuidedSectionAttributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID) == EFI_GUIDED_SECTION_AUTH_STATUS_VALID) {
917 Authentication |= EFI_AUTH_STATUS_IMAGE_SIGNED | EFI_AUTH_STATUS_NOT_TESTED;
918 }
919 if (IS_SECTION2 (Section)) {
920 PpiOutputSize = SECTION2_SIZE (Section) - ((EFI_GUID_DEFINED_SECTION2 *) Section)->DataOffset;
921 PpiOutput = (UINT8 *) Section + ((EFI_GUID_DEFINED_SECTION2 *) Section)->DataOffset;
922 } else {
923 PpiOutputSize = SECTION_SIZE (Section) - ((EFI_GUID_DEFINED_SECTION *) Section)->DataOffset;
924 PpiOutput = (UINT8 *) Section + ((EFI_GUID_DEFINED_SECTION *) Section)->DataOffset;
925 }
926 }
927 } else if (Section->Type == EFI_SECTION_COMPRESSION) {
928 Status = PeiServicesLocatePpi (&gEfiPeiDecompressPpiGuid, 0, NULL, (VOID **) &DecompressPpi);
929 if (!EFI_ERROR (Status)) {
930 Status = DecompressPpi->Decompress (
931 DecompressPpi,
932 (CONST EFI_COMPRESSION_SECTION*) Section,
933 &PpiOutput,
934 &PpiOutputSize
935 );
936 }
937 }
938
939 if (!EFI_ERROR (Status)) {
940 if ((Authentication & EFI_AUTH_STATUS_NOT_TESTED) == 0) {
941 //
942 // Update cache section data.
943 //
944 if (PrivateData->CacheSection.AllSectionCount < CACHE_SETION_MAX_NUMBER) {
945 PrivateData->CacheSection.AllSectionCount ++;
946 }
947 PrivateData->CacheSection.Section [PrivateData->CacheSection.SectionIndex] = Section;
948 PrivateData->CacheSection.SectionData [PrivateData->CacheSection.SectionIndex] = PpiOutput;
949 PrivateData->CacheSection.SectionSize [PrivateData->CacheSection.SectionIndex] = PpiOutputSize;
950 PrivateData->CacheSection.AuthenticationStatus [PrivateData->CacheSection.SectionIndex] = Authentication;
951 PrivateData->CacheSection.SectionIndex = (PrivateData->CacheSection.SectionIndex + 1)%CACHE_SETION_MAX_NUMBER;
952 }
953
954 TempAuthenticationStatus = 0;
955 Status = ProcessSection (
956 PeiServices,
957 SectionType,
958 SectionInstance,
959 PpiOutput,
960 PpiOutputSize,
961 &TempOutputBuffer,
962 &TempAuthenticationStatus,
963 IsFfs3Fv
964 );
965 if (!EFI_ERROR (Status)) {
966 *OutputBuffer = TempOutputBuffer;
967 *AuthenticationStatus = TempAuthenticationStatus | Authentication;
968 return EFI_SUCCESS;
969 }
970 }
971 }
972 }
973
974 if (IS_SECTION2 (Section)) {
975 SectionLength = SECTION2_SIZE (Section);
976 } else {
977 SectionLength = SECTION_SIZE (Section);
978 }
979 //
980 // SectionLength is adjusted it is 4 byte aligned.
981 // Go to the next section
982 //
983 SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);
984 ASSERT (SectionLength != 0);
985 ParsedLength += SectionLength;
986 Section = (EFI_COMMON_SECTION_HEADER *)((UINT8 *)Section + SectionLength);
987 }
988
989 return EFI_NOT_FOUND;
990 }
991
992
993 /**
994 Searches for the next matching section within the specified file.
995
996 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
997 @param SectionType Filter to find only sections of this type.
998 @param FileHandle Pointer to the current file to search.
999 @param SectionData A pointer to the discovered section, if successful.
1000 NULL if section not found
1001
1002 @retval EFI_NOT_FOUND The section was not found.
1003 @retval EFI_SUCCESS The section was found.
1004
1005 **/
1006 EFI_STATUS
1007 EFIAPI
1008 PeiFfsFindSectionData (
1009 IN CONST EFI_PEI_SERVICES **PeiServices,
1010 IN EFI_SECTION_TYPE SectionType,
1011 IN EFI_PEI_FILE_HANDLE FileHandle,
1012 OUT VOID **SectionData
1013 )
1014 {
1015 PEI_CORE_FV_HANDLE *CoreFvHandle;
1016
1017 CoreFvHandle = FileHandleToVolume (FileHandle);
1018 if ((CoreFvHandle == NULL) || (CoreFvHandle->FvPpi == NULL)) {
1019 return EFI_NOT_FOUND;
1020 }
1021
1022 return CoreFvHandle->FvPpi->FindSectionByType (CoreFvHandle->FvPpi, SectionType, FileHandle, SectionData);
1023 }
1024
1025 /**
1026 Searches for the next matching section within the specified file.
1027
1028 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
1029 @param SectionType The value of the section type to find.
1030 @param SectionInstance Section instance to find.
1031 @param FileHandle Handle of the firmware file to search.
1032 @param SectionData A pointer to the discovered section, if successful.
1033 @param AuthenticationStatus A pointer to the authentication status for this section.
1034
1035 @retval EFI_SUCCESS The section was found.
1036 @retval EFI_NOT_FOUND The section was not found.
1037
1038 **/
1039 EFI_STATUS
1040 EFIAPI
1041 PeiFfsFindSectionData3 (
1042 IN CONST EFI_PEI_SERVICES **PeiServices,
1043 IN EFI_SECTION_TYPE SectionType,
1044 IN UINTN SectionInstance,
1045 IN EFI_PEI_FILE_HANDLE FileHandle,
1046 OUT VOID **SectionData,
1047 OUT UINT32 *AuthenticationStatus
1048 )
1049 {
1050 PEI_CORE_FV_HANDLE *CoreFvHandle;
1051
1052 CoreFvHandle = FileHandleToVolume (FileHandle);
1053 if ((CoreFvHandle == NULL) || (CoreFvHandle->FvPpi == NULL)) {
1054 return EFI_NOT_FOUND;
1055 }
1056
1057 if ((CoreFvHandle->FvPpi->Signature == EFI_PEI_FIRMWARE_VOLUME_PPI_SIGNATURE) &&
1058 (CoreFvHandle->FvPpi->Revision == EFI_PEI_FIRMWARE_VOLUME_PPI_REVISION)) {
1059 return CoreFvHandle->FvPpi->FindSectionByType2 (CoreFvHandle->FvPpi, SectionType, SectionInstance, FileHandle, SectionData, AuthenticationStatus);
1060 }
1061 //
1062 // The old FvPpi doesn't support to find section by section instance
1063 // and return authentication status, so return EFI_UNSUPPORTED.
1064 //
1065 return EFI_UNSUPPORTED;
1066 }
1067
1068 /**
1069 Searches for the next matching file in the firmware volume.
1070
1071 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
1072 @param SearchType Filter to find only files of this type.
1073 Type EFI_FV_FILETYPE_ALL causes no filtering to be done.
1074 @param FvHandle Handle of firmware volume in which to search.
1075 @param FileHandle On entry, points to the current handle from which to begin searching or NULL to start
1076 at the beginning of the firmware volume. On exit, points the file handle of the next file
1077 in the volume or NULL if there are no more files.
1078
1079 @retval EFI_NOT_FOUND The file was not found.
1080 @retval EFI_NOT_FOUND The header checksum was not zero.
1081 @retval EFI_SUCCESS The file was found.
1082
1083 **/
1084 EFI_STATUS
1085 EFIAPI
1086 PeiFfsFindNextFile (
1087 IN CONST EFI_PEI_SERVICES **PeiServices,
1088 IN UINT8 SearchType,
1089 IN EFI_PEI_FV_HANDLE FvHandle,
1090 IN OUT EFI_PEI_FILE_HANDLE *FileHandle
1091 )
1092 {
1093 PEI_CORE_FV_HANDLE *CoreFvHandle;
1094
1095 CoreFvHandle = FvHandleToCoreHandle (FvHandle);
1096
1097 //
1098 // To make backward compatiblity, if can not find corresponding the handle of FV
1099 // then treat FV as build-in FFS2/FFS3 format and memory mapped FV that FV handle is pointed
1100 // to the address of first byte of FV.
1101 //
1102 if ((CoreFvHandle == NULL) && FeaturePcdGet (PcdFrameworkCompatibilitySupport)) {
1103 return FindFileEx (FvHandle, NULL, SearchType, FileHandle, NULL);
1104 }
1105
1106 if ((CoreFvHandle == NULL) || CoreFvHandle->FvPpi == NULL) {
1107 return EFI_NOT_FOUND;
1108 }
1109
1110 return CoreFvHandle->FvPpi->FindFileByType (CoreFvHandle->FvPpi, SearchType, FvHandle, FileHandle);
1111 }
1112
1113
1114 /**
1115 Search the firmware volumes by index
1116
1117 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
1118 @param Instance This instance of the firmware volume to find. The value 0 is the Boot Firmware
1119 Volume (BFV).
1120 @param VolumeHandle On exit, points to the next volume handle or NULL if it does not exist.
1121
1122 @retval EFI_INVALID_PARAMETER VolumeHandle is NULL
1123 @retval EFI_NOT_FOUND The volume was not found.
1124 @retval EFI_SUCCESS The volume was found.
1125
1126 **/
1127 EFI_STATUS
1128 EFIAPI
1129 PeiFfsFindNextVolume (
1130 IN CONST EFI_PEI_SERVICES **PeiServices,
1131 IN UINTN Instance,
1132 IN OUT EFI_PEI_FV_HANDLE *VolumeHandle
1133 )
1134 {
1135 PEI_CORE_INSTANCE *Private;
1136 PEI_CORE_FV_HANDLE *CoreFvHandle;
1137
1138 if (VolumeHandle == NULL) {
1139 return EFI_INVALID_PARAMETER;
1140 }
1141
1142 Private = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);
1143
1144 CoreFvHandle = FindNextCoreFvHandle (Private, Instance);
1145 if (CoreFvHandle == NULL) {
1146 *VolumeHandle = NULL;
1147 return EFI_NOT_FOUND;
1148 }
1149
1150 *VolumeHandle = CoreFvHandle->FvHandle;
1151
1152 return EFI_SUCCESS;
1153 }
1154
1155
1156 /**
1157 Find a file within a volume by its name.
1158
1159 @param FileName A pointer to the name of the file to find within the firmware volume.
1160 @param VolumeHandle The firmware volume to search
1161 @param FileHandle Upon exit, points to the found file's handle
1162 or NULL if it could not be found.
1163
1164 @retval EFI_SUCCESS File was found.
1165 @retval EFI_NOT_FOUND File was not found.
1166 @retval EFI_INVALID_PARAMETER VolumeHandle or FileHandle or FileName was NULL.
1167
1168 **/
1169 EFI_STATUS
1170 EFIAPI
1171 PeiFfsFindFileByName (
1172 IN CONST EFI_GUID *FileName,
1173 IN EFI_PEI_FV_HANDLE VolumeHandle,
1174 OUT EFI_PEI_FILE_HANDLE *FileHandle
1175 )
1176 {
1177 PEI_CORE_FV_HANDLE *CoreFvHandle;
1178
1179 if ((VolumeHandle == NULL) || (FileName == NULL) || (FileHandle == NULL)) {
1180 return EFI_INVALID_PARAMETER;
1181 }
1182
1183 CoreFvHandle = FvHandleToCoreHandle (VolumeHandle);
1184 if ((CoreFvHandle == NULL) || (CoreFvHandle->FvPpi == NULL)) {
1185 return EFI_NOT_FOUND;
1186 }
1187
1188 return CoreFvHandle->FvPpi->FindFileByName (CoreFvHandle->FvPpi, FileName, &VolumeHandle, FileHandle);
1189 }
1190
1191 /**
1192 Returns information about a specific file.
1193
1194 @param FileHandle Handle of the file.
1195 @param FileInfo Upon exit, points to the file's information.
1196
1197 @retval EFI_INVALID_PARAMETER If FileInfo is NULL.
1198 @retval EFI_INVALID_PARAMETER If FileHandle does not represent a valid file.
1199 @retval EFI_SUCCESS File information returned.
1200
1201 **/
1202 EFI_STATUS
1203 EFIAPI
1204 PeiFfsGetFileInfo (
1205 IN EFI_PEI_FILE_HANDLE FileHandle,
1206 OUT EFI_FV_FILE_INFO *FileInfo
1207 )
1208 {
1209 PEI_CORE_FV_HANDLE *CoreFvHandle;
1210
1211 if ((FileHandle == NULL) || (FileInfo == NULL)) {
1212 return EFI_INVALID_PARAMETER;
1213 }
1214
1215 //
1216 // Retrieve the FirmwareVolume which the file resides in.
1217 //
1218 CoreFvHandle = FileHandleToVolume (FileHandle);
1219 if ((CoreFvHandle == NULL) || (CoreFvHandle->FvPpi == NULL)) {
1220 return EFI_INVALID_PARAMETER;
1221 }
1222
1223 return CoreFvHandle->FvPpi->GetFileInfo (CoreFvHandle->FvPpi, FileHandle, FileInfo);
1224 }
1225
1226 /**
1227 Returns information about a specific file.
1228
1229 @param FileHandle Handle of the file.
1230 @param FileInfo Upon exit, points to the file's information.
1231
1232 @retval EFI_INVALID_PARAMETER If FileInfo is NULL.
1233 @retval EFI_INVALID_PARAMETER If FileHandle does not represent a valid file.
1234 @retval EFI_SUCCESS File information returned.
1235
1236 **/
1237 EFI_STATUS
1238 EFIAPI
1239 PeiFfsGetFileInfo2 (
1240 IN EFI_PEI_FILE_HANDLE FileHandle,
1241 OUT EFI_FV_FILE_INFO2 *FileInfo
1242 )
1243 {
1244 PEI_CORE_FV_HANDLE *CoreFvHandle;
1245
1246 if ((FileHandle == NULL) || (FileInfo == NULL)) {
1247 return EFI_INVALID_PARAMETER;
1248 }
1249
1250 //
1251 // Retrieve the FirmwareVolume which the file resides in.
1252 //
1253 CoreFvHandle = FileHandleToVolume (FileHandle);
1254 if ((CoreFvHandle == NULL) || (CoreFvHandle->FvPpi == NULL)) {
1255 return EFI_INVALID_PARAMETER;
1256 }
1257
1258 if ((CoreFvHandle->FvPpi->Signature == EFI_PEI_FIRMWARE_VOLUME_PPI_SIGNATURE) &&
1259 (CoreFvHandle->FvPpi->Revision == EFI_PEI_FIRMWARE_VOLUME_PPI_REVISION)) {
1260 return CoreFvHandle->FvPpi->GetFileInfo2 (CoreFvHandle->FvPpi, FileHandle, FileInfo);
1261 }
1262 //
1263 // The old FvPpi doesn't support to return file info with authentication status,
1264 // so return EFI_UNSUPPORTED.
1265 //
1266 return EFI_UNSUPPORTED;
1267 }
1268
1269 /**
1270 Returns information about the specified volume.
1271
1272 This function returns information about a specific firmware
1273 volume, including its name, type, attributes, starting address
1274 and size.
1275
1276 @param VolumeHandle Handle of the volume.
1277 @param VolumeInfo Upon exit, points to the volume's information.
1278
1279 @retval EFI_SUCCESS Volume information returned.
1280 @retval EFI_INVALID_PARAMETER If VolumeHandle does not represent a valid volume.
1281 @retval EFI_INVALID_PARAMETER If VolumeHandle is NULL.
1282 @retval EFI_SUCCESS Information successfully returned.
1283 @retval EFI_INVALID_PARAMETER The volume designated by the VolumeHandle is not available.
1284
1285 **/
1286 EFI_STATUS
1287 EFIAPI
1288 PeiFfsGetVolumeInfo (
1289 IN EFI_PEI_FV_HANDLE VolumeHandle,
1290 OUT EFI_FV_INFO *VolumeInfo
1291 )
1292 {
1293 PEI_CORE_FV_HANDLE *CoreHandle;
1294
1295 if ((VolumeInfo == NULL) || (VolumeHandle == NULL)) {
1296 return EFI_INVALID_PARAMETER;
1297 }
1298
1299 CoreHandle = FvHandleToCoreHandle (VolumeHandle);
1300
1301 if ((CoreHandle == NULL) || (CoreHandle->FvPpi == NULL)) {
1302 return EFI_INVALID_PARAMETER;
1303 }
1304
1305 return CoreHandle->FvPpi->GetVolumeInfo (CoreHandle->FvPpi, VolumeHandle, VolumeInfo);
1306 }
1307
1308 /**
1309 Find USED_SIZE FV_EXT_TYPE entry in FV extension header and get the FV used size.
1310
1311 @param[in] FvHeader Pointer to FV header.
1312 @param[out] FvUsedSize Pointer to FV used size returned,
1313 only valid if USED_SIZE FV_EXT_TYPE entry is found.
1314 @param[out] EraseByte Pointer to erase byte returned,
1315 only valid if USED_SIZE FV_EXT_TYPE entry is found.
1316
1317 @retval TRUE USED_SIZE FV_EXT_TYPE entry is found,
1318 FV used size and erase byte are returned.
1319 @retval FALSE No USED_SIZE FV_EXT_TYPE entry found.
1320
1321 **/
1322 BOOLEAN
1323 GetFvUsedSize (
1324 IN EFI_FIRMWARE_VOLUME_HEADER *FvHeader,
1325 OUT UINT32 *FvUsedSize,
1326 OUT UINT8 *EraseByte
1327 )
1328 {
1329 UINT16 ExtHeaderOffset;
1330 EFI_FIRMWARE_VOLUME_EXT_HEADER *ExtHeader;
1331 EFI_FIRMWARE_VOLUME_EXT_ENTRY *ExtEntryList;
1332 EFI_FIRMWARE_VOLUME_EXT_ENTRY_USED_SIZE_TYPE *ExtEntryUsedSize;
1333
1334 ExtHeaderOffset = ReadUnaligned16 (&FvHeader->ExtHeaderOffset);
1335 if (ExtHeaderOffset != 0) {
1336 ExtHeader = (EFI_FIRMWARE_VOLUME_EXT_HEADER *) ((UINT8 *) FvHeader + ExtHeaderOffset);
1337 ExtEntryList = (EFI_FIRMWARE_VOLUME_EXT_ENTRY *) (ExtHeader + 1);
1338 while ((UINTN) ExtEntryList < ((UINTN) ExtHeader + ReadUnaligned32 (&ExtHeader->ExtHeaderSize))) {
1339 if (ReadUnaligned16 (&ExtEntryList->ExtEntryType) == EFI_FV_EXT_TYPE_USED_SIZE_TYPE) {
1340 //
1341 // USED_SIZE FV_EXT_TYPE entry is found.
1342 //
1343 ExtEntryUsedSize = (EFI_FIRMWARE_VOLUME_EXT_ENTRY_USED_SIZE_TYPE *) ExtEntryList;
1344 *FvUsedSize = ReadUnaligned32 (&ExtEntryUsedSize->UsedSize);
1345 if ((ReadUnaligned32 (&FvHeader->Attributes) & EFI_FVB2_ERASE_POLARITY) != 0) {
1346 *EraseByte = 0xFF;
1347 } else {
1348 *EraseByte = 0;
1349 }
1350 DEBUG ((
1351 DEBUG_INFO,
1352 "FV at 0x%x has 0x%x used size, and erase byte is 0x%02x\n",
1353 FvHeader,
1354 *FvUsedSize,
1355 *EraseByte
1356 ));
1357 return TRUE;
1358 }
1359 ExtEntryList = (EFI_FIRMWARE_VOLUME_EXT_ENTRY *)
1360 ((UINT8 *) ExtEntryList + ReadUnaligned16 (&ExtEntryList->ExtEntrySize));
1361 }
1362 }
1363
1364 //
1365 // No USED_SIZE FV_EXT_TYPE entry found.
1366 //
1367 return FALSE;
1368 }
1369
1370 /**
1371 Get Fv image(s) from the FV type file, then install FV INFO(2) ppi, Build FV(2, 3) hob.
1372
1373 @param PrivateData PeiCore's private data structure
1374 @param ParentFvCoreHandle Pointer of EFI_CORE_FV_HANDLE to parent Fv image that contain this Fv image.
1375 @param ParentFvFileHandle File handle of a Fv type file that contain this Fv image.
1376
1377 @retval EFI_NOT_FOUND FV image can't be found.
1378 @retval EFI_SUCCESS Successfully to process it.
1379 @retval EFI_OUT_OF_RESOURCES Can not allocate page when aligning FV image
1380 @retval EFI_SECURITY_VIOLATION Image is illegal
1381 @retval Others Can not find EFI_SECTION_FIRMWARE_VOLUME_IMAGE section
1382
1383 **/
1384 EFI_STATUS
1385 ProcessFvFile (
1386 IN PEI_CORE_INSTANCE *PrivateData,
1387 IN PEI_CORE_FV_HANDLE *ParentFvCoreHandle,
1388 IN EFI_PEI_FILE_HANDLE ParentFvFileHandle
1389 )
1390 {
1391 EFI_STATUS Status;
1392 EFI_FV_INFO ParentFvImageInfo;
1393 UINT32 FvAlignment;
1394 VOID *NewFvBuffer;
1395 EFI_PEI_HOB_POINTERS HobPtr;
1396 EFI_PEI_FIRMWARE_VOLUME_PPI *ParentFvPpi;
1397 EFI_PEI_FV_HANDLE ParentFvHandle;
1398 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;
1399 EFI_FV_FILE_INFO FileInfo;
1400 UINT64 FvLength;
1401 UINT32 AuthenticationStatus;
1402 UINT32 FvUsedSize;
1403 UINT8 EraseByte;
1404 UINTN Index;
1405
1406 //
1407 // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has already
1408 // been extracted.
1409 //
1410 HobPtr.Raw = GetHobList ();
1411 while ((HobPtr.Raw = GetNextHob (EFI_HOB_TYPE_FV2, HobPtr.Raw)) != NULL) {
1412 if (CompareGuid (&(((EFI_FFS_FILE_HEADER *)ParentFvFileHandle)->Name), &HobPtr.FirmwareVolume2->FileName)) {
1413 //
1414 // this FILE has been dispatched, it will not be dispatched again.
1415 //
1416 DEBUG ((EFI_D_INFO, "FV file %p has been dispatched!\r\n", ParentFvFileHandle));
1417 return EFI_SUCCESS;
1418 }
1419 HobPtr.Raw = GET_NEXT_HOB (HobPtr);
1420 }
1421
1422 ParentFvHandle = ParentFvCoreHandle->FvHandle;
1423 ParentFvPpi = ParentFvCoreHandle->FvPpi;
1424
1425 Status = EFI_SUCCESS;
1426
1427 //
1428 // Find FvImage(s) in FvFile
1429 //
1430 Index = 0;
1431 do {
1432 AuthenticationStatus = 0;
1433 if ((ParentFvPpi->Signature == EFI_PEI_FIRMWARE_VOLUME_PPI_SIGNATURE) &&
1434 (ParentFvPpi->Revision == EFI_PEI_FIRMWARE_VOLUME_PPI_REVISION)) {
1435 Status = ParentFvPpi->FindSectionByType2 (
1436 ParentFvPpi,
1437 EFI_SECTION_FIRMWARE_VOLUME_IMAGE,
1438 Index,
1439 ParentFvFileHandle,
1440 (VOID **)&FvHeader,
1441 &AuthenticationStatus
1442 );
1443 } else {
1444 //
1445 // Old FvPpi has no parameter to input SearchInstance,
1446 // only one instance is supported.
1447 //
1448 if (Index > 0) {
1449 break;
1450 }
1451 Status = ParentFvPpi->FindSectionByType (
1452 ParentFvPpi,
1453 EFI_SECTION_FIRMWARE_VOLUME_IMAGE,
1454 ParentFvFileHandle,
1455 (VOID **)&FvHeader
1456 );
1457 }
1458 if (EFI_ERROR (Status)) {
1459 break;
1460 }
1461
1462 Status = VerifyPeim (PrivateData, ParentFvHandle, ParentFvFileHandle, AuthenticationStatus);
1463 if (Status == EFI_SECURITY_VIOLATION) {
1464 break;
1465 }
1466
1467 //
1468 // If EFI_FVB2_WEAK_ALIGNMENT is set in the volume header then the first byte of the volume
1469 // can be aligned on any power-of-two boundary. A weakly aligned volume can not be moved from
1470 // its initial linked location and maintain its alignment.
1471 //
1472 if ((ReadUnaligned32 (&FvHeader->Attributes) & EFI_FVB2_WEAK_ALIGNMENT) != EFI_FVB2_WEAK_ALIGNMENT) {
1473 //
1474 // FvAlignment must be greater than or equal to 8 bytes of the minimum FFS alignment value.
1475 //
1476 FvAlignment = 1 << ((ReadUnaligned32 (&FvHeader->Attributes) & EFI_FVB2_ALIGNMENT) >> 16);
1477 if (FvAlignment < 8) {
1478 FvAlignment = 8;
1479 }
1480
1481 DEBUG ((
1482 DEBUG_INFO,
1483 "%a() FV at 0x%x, FvAlignment required is 0x%x\n",
1484 __FUNCTION__,
1485 FvHeader,
1486 FvAlignment
1487 ));
1488
1489 //
1490 // Check FvImage alignment.
1491 //
1492 if ((UINTN) FvHeader % FvAlignment != 0) {
1493 FvLength = ReadUnaligned64 (&FvHeader->FvLength);
1494 NewFvBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES ((UINT32) FvLength), FvAlignment);
1495 if (NewFvBuffer == NULL) {
1496 Status = EFI_OUT_OF_RESOURCES;
1497 break;
1498 }
1499 if (GetFvUsedSize (FvHeader, &FvUsedSize, &EraseByte)) {
1500 //
1501 // Copy the used bytes and fill the rest with the erase value.
1502 //
1503 CopyMem (NewFvBuffer, FvHeader, (UINTN) FvUsedSize);
1504 SetMem (
1505 (UINT8 *) NewFvBuffer + FvUsedSize,
1506 (UINTN) (FvLength - FvUsedSize),
1507 EraseByte
1508 );
1509 } else {
1510 CopyMem (NewFvBuffer, FvHeader, (UINTN) FvLength);
1511 }
1512 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER*) NewFvBuffer;
1513 }
1514 }
1515
1516 Status = ParentFvPpi->GetVolumeInfo (ParentFvPpi, ParentFvHandle, &ParentFvImageInfo);
1517 ASSERT_EFI_ERROR (Status);
1518
1519 Status = ParentFvPpi->GetFileInfo (ParentFvPpi, ParentFvFileHandle, &FileInfo);
1520 ASSERT_EFI_ERROR (Status);
1521
1522 //
1523 // Install FvInfo(2) Ppi
1524 // NOTE: FvInfo2 must be installed before FvInfo so that recursive processing of encapsulated
1525 // FVs inherit the proper AuthenticationStatus.
1526 //
1527 PeiServicesInstallFvInfo2Ppi(
1528 &FvHeader->FileSystemGuid,
1529 (VOID**)FvHeader,
1530 (UINT32)FvHeader->FvLength,
1531 &ParentFvImageInfo.FvName,
1532 &FileInfo.FileName,
1533 AuthenticationStatus
1534 );
1535
1536 PeiServicesInstallFvInfoPpi (
1537 &FvHeader->FileSystemGuid,
1538 (VOID**) FvHeader,
1539 (UINT32) FvHeader->FvLength,
1540 &ParentFvImageInfo.FvName,
1541 &FileInfo.FileName
1542 );
1543
1544 //
1545 // Inform the extracted FvImage to Fv HOB consumer phase, i.e. DXE phase
1546 //
1547 BuildFvHob (
1548 (EFI_PHYSICAL_ADDRESS) (UINTN) FvHeader,
1549 FvHeader->FvLength
1550 );
1551
1552 //
1553 // Makes the encapsulated volume show up in DXE phase to skip processing of
1554 // encapsulated file again.
1555 //
1556 BuildFv2Hob (
1557 (EFI_PHYSICAL_ADDRESS) (UINTN) FvHeader,
1558 FvHeader->FvLength,
1559 &ParentFvImageInfo.FvName,
1560 &FileInfo.FileName
1561 );
1562
1563 //
1564 // Build FV3 HOB with authentication status to be propagated to DXE.
1565 //
1566 BuildFv3Hob (
1567 (EFI_PHYSICAL_ADDRESS) (UINTN) FvHeader,
1568 FvHeader->FvLength,
1569 AuthenticationStatus,
1570 TRUE,
1571 &ParentFvImageInfo.FvName,
1572 &FileInfo.FileName
1573 );
1574
1575 Index++;
1576 } while (TRUE);
1577
1578 if (Index > 0) {
1579 //
1580 // At least one FvImage has been processed successfully.
1581 //
1582 return EFI_SUCCESS;
1583 } else {
1584 return Status;
1585 }
1586 }
1587
1588 /**
1589 Process a firmware volume and create a volume handle.
1590
1591 Create a volume handle from the information in the buffer. For
1592 memory-mapped firmware volumes, Buffer and BufferSize refer to
1593 the start of the firmware volume and the firmware volume size.
1594 For non memory-mapped firmware volumes, this points to a
1595 buffer which contains the necessary information for creating
1596 the firmware volume handle. Normally, these values are derived
1597 from the EFI_FIRMWARE_VOLUME_INFO_PPI.
1598
1599
1600 @param This Points to this instance of the
1601 EFI_PEI_FIRMWARE_VOLUME_PPI.
1602 @param Buffer Points to the start of the buffer.
1603 @param BufferSize Size of the buffer.
1604 @param FvHandle Points to the returned firmware volume
1605 handle. The firmware volume handle must
1606 be unique within the system.
1607
1608 @retval EFI_SUCCESS Firmware volume handle created.
1609 @retval EFI_VOLUME_CORRUPTED Volume was corrupt.
1610
1611 **/
1612 EFI_STATUS
1613 EFIAPI
1614 PeiFfsFvPpiProcessVolume (
1615 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,
1616 IN VOID *Buffer,
1617 IN UINTN BufferSize,
1618 OUT EFI_PEI_FV_HANDLE *FvHandle
1619 )
1620 {
1621 EFI_STATUS Status;
1622
1623 ASSERT (FvHandle != NULL);
1624
1625 if (Buffer == NULL) {
1626 return EFI_VOLUME_CORRUPTED;
1627 }
1628
1629 //
1630 // The build-in EFI_PEI_FIRMWARE_VOLUME_PPI for FFS2/FFS3 support memory-mapped
1631 // FV image and the handle is pointed to Fv image's buffer.
1632 //
1633 *FvHandle = (EFI_PEI_FV_HANDLE) Buffer;
1634
1635 //
1636 // Do verify for given FV buffer.
1637 //
1638 Status = VerifyFv ((EFI_FIRMWARE_VOLUME_HEADER*) Buffer);
1639 if (EFI_ERROR(Status)) {
1640 DEBUG ((EFI_D_ERROR, "Fail to verify FV which address is 0x%11p", Buffer));
1641 return EFI_VOLUME_CORRUPTED;
1642 }
1643
1644 return EFI_SUCCESS;
1645 }
1646
1647 /**
1648 Finds the next file of the specified type.
1649
1650 This service enables PEI modules to discover additional firmware files.
1651 The FileHandle must be unique within the system.
1652
1653 @param This Points to this instance of the
1654 EFI_PEI_FIRMWARE_VOLUME_PPI.
1655 @param SearchType A filter to find only files of this type. Type
1656 EFI_FV_FILETYPE_ALL causes no filtering to be
1657 done.
1658 @param FvHandle Handle of firmware volume in which to
1659 search.
1660 @param FileHandle Points to the current handle from which to
1661 begin searching or NULL to start at the
1662 beginning of the firmware volume. Updated
1663 upon return to reflect the file found.
1664
1665 @retval EFI_SUCCESS The file was found.
1666 @retval EFI_NOT_FOUND The file was not found. FileHandle contains NULL.
1667
1668 **/
1669 EFI_STATUS
1670 EFIAPI
1671 PeiFfsFvPpiFindFileByType (
1672 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,
1673 IN EFI_FV_FILETYPE SearchType,
1674 IN EFI_PEI_FV_HANDLE FvHandle,
1675 IN OUT EFI_PEI_FILE_HANDLE *FileHandle
1676 )
1677 {
1678 return FindFileEx (FvHandle, NULL, SearchType, FileHandle, NULL);
1679 }
1680
1681 /**
1682 Find a file within a volume by its name.
1683
1684 This service searches for files with a specific name, within
1685 either the specified firmware volume or all firmware volumes.
1686
1687 @param This Points to this instance of the
1688 EFI_PEI_FIRMWARE_VOLUME_PPI.
1689 @param FileName A pointer to the name of the file to find
1690 within the firmware volume.
1691 @param FvHandle Upon entry, the pointer to the firmware
1692 volume to search or NULL if all firmware
1693 volumes should be searched. Upon exit, the
1694 actual firmware volume in which the file was
1695 found.
1696 @param FileHandle Upon exit, points to the found file's
1697 handle or NULL if it could not be found.
1698
1699 @retval EFI_SUCCESS File was found.
1700 @retval EFI_NOT_FOUND File was not found.
1701 @retval EFI_INVALID_PARAMETER FvHandle or FileHandle or
1702 FileName was NULL.
1703
1704
1705 **/
1706 EFI_STATUS
1707 EFIAPI
1708 PeiFfsFvPpiFindFileByName (
1709 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,
1710 IN CONST EFI_GUID *FileName,
1711 IN EFI_PEI_FV_HANDLE *FvHandle,
1712 OUT EFI_PEI_FILE_HANDLE *FileHandle
1713 )
1714 {
1715 EFI_STATUS Status;
1716 PEI_CORE_INSTANCE *PrivateData;
1717 UINTN Index;
1718
1719 if ((FvHandle == NULL) || (FileName == NULL) || (FileHandle == NULL)) {
1720 return EFI_INVALID_PARAMETER;
1721 }
1722
1723 if (*FvHandle != NULL) {
1724 Status = FindFileEx (*FvHandle, FileName, 0, FileHandle, NULL);
1725 if (Status == EFI_NOT_FOUND) {
1726 *FileHandle = NULL;
1727 }
1728 } else {
1729 //
1730 // If *FvHandle = NULL, so search all FV for given filename
1731 //
1732 Status = EFI_NOT_FOUND;
1733
1734 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer());
1735 for (Index = 0; Index < PrivateData->FvCount; Index ++) {
1736 //
1737 // Only search the FV which is associated with a EFI_PEI_FIRMWARE_VOLUME_PPI instance.
1738 //
1739 if (PrivateData->Fv[Index].FvPpi != NULL) {
1740 Status = FindFileEx (PrivateData->Fv[Index].FvHandle, FileName, 0, FileHandle, NULL);
1741 if (!EFI_ERROR (Status)) {
1742 *FvHandle = PrivateData->Fv[Index].FvHandle;
1743 break;
1744 }
1745 }
1746 }
1747 }
1748
1749 return Status;
1750 }
1751
1752 /**
1753 Returns information about a specific file.
1754
1755 This function returns information about a specific
1756 file, including its file name, type, attributes, starting
1757 address and size.
1758
1759 @param This Points to this instance of the
1760 EFI_PEI_FIRMWARE_VOLUME_PPI.
1761 @param FileHandle Handle of the file.
1762 @param FileInfo Upon exit, points to the file's
1763 information.
1764
1765 @retval EFI_SUCCESS File information returned.
1766 @retval EFI_INVALID_PARAMETER If FileHandle does not
1767 represent a valid file.
1768 @retval EFI_INVALID_PARAMETER If FileInfo is NULL.
1769
1770 **/
1771 EFI_STATUS
1772 EFIAPI
1773 PeiFfsFvPpiGetFileInfo (
1774 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,
1775 IN EFI_PEI_FILE_HANDLE FileHandle,
1776 OUT EFI_FV_FILE_INFO *FileInfo
1777 )
1778 {
1779 UINT8 FileState;
1780 UINT8 ErasePolarity;
1781 EFI_FFS_FILE_HEADER *FileHeader;
1782 PEI_CORE_FV_HANDLE *CoreFvHandle;
1783 PEI_FW_VOL_INSTANCE *FwVolInstance;
1784
1785 if ((FileHandle == NULL) || (FileInfo == NULL)) {
1786 return EFI_INVALID_PARAMETER;
1787 }
1788
1789 //
1790 // Retrieve the FirmwareVolume which the file resides in.
1791 //
1792 CoreFvHandle = FileHandleToVolume (FileHandle);
1793 if (CoreFvHandle == NULL) {
1794 return EFI_INVALID_PARAMETER;
1795 }
1796
1797 FwVolInstance = PEI_FW_VOL_INSTANCE_FROM_FV_THIS (This);
1798
1799 if ((CoreFvHandle->FvHeader->Attributes & EFI_FVB2_ERASE_POLARITY) != 0) {
1800 ErasePolarity = 1;
1801 } else {
1802 ErasePolarity = 0;
1803 }
1804
1805 //
1806 // Get FileState which is the highest bit of the State
1807 //
1808 FileState = GetFileState (ErasePolarity, (EFI_FFS_FILE_HEADER*)FileHandle);
1809
1810 switch (FileState) {
1811 case EFI_FILE_DATA_VALID:
1812 case EFI_FILE_MARKED_FOR_UPDATE:
1813 break;
1814 default:
1815 return EFI_INVALID_PARAMETER;
1816 }
1817
1818 FileHeader = (EFI_FFS_FILE_HEADER *)FileHandle;
1819 if (IS_FFS_FILE2 (FileHeader)) {
1820 ASSERT (FFS_FILE2_SIZE (FileHeader) > 0x00FFFFFF);
1821 if (!FwVolInstance->IsFfs3Fv) {
1822 DEBUG ((EFI_D_ERROR, "It is a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &FileHeader->Name));
1823 return EFI_INVALID_PARAMETER;
1824 }
1825 FileInfo->BufferSize = FFS_FILE2_SIZE (FileHeader) - sizeof (EFI_FFS_FILE_HEADER2);
1826 FileInfo->Buffer = (UINT8 *) FileHeader + sizeof (EFI_FFS_FILE_HEADER2);
1827 } else {
1828 FileInfo->BufferSize = FFS_FILE_SIZE (FileHeader) - sizeof (EFI_FFS_FILE_HEADER);
1829 FileInfo->Buffer = (UINT8 *) FileHeader + sizeof (EFI_FFS_FILE_HEADER);
1830 }
1831 CopyMem (&FileInfo->FileName, &FileHeader->Name, sizeof(EFI_GUID));
1832 FileInfo->FileType = FileHeader->Type;
1833 FileInfo->FileAttributes = FfsAttributes2FvFileAttributes (FileHeader->Attributes);
1834 if ((CoreFvHandle->FvHeader->Attributes & EFI_FVB2_MEMORY_MAPPED) == EFI_FVB2_MEMORY_MAPPED) {
1835 FileInfo->FileAttributes |= EFI_FV_FILE_ATTRIB_MEMORY_MAPPED;
1836 }
1837 return EFI_SUCCESS;
1838 }
1839
1840 /**
1841 Returns information about a specific file.
1842
1843 This function returns information about a specific
1844 file, including its file name, type, attributes, starting
1845 address, size and authentication status.
1846
1847 @param This Points to this instance of the
1848 EFI_PEI_FIRMWARE_VOLUME_PPI.
1849 @param FileHandle Handle of the file.
1850 @param FileInfo Upon exit, points to the file's
1851 information.
1852
1853 @retval EFI_SUCCESS File information returned.
1854 @retval EFI_INVALID_PARAMETER If FileHandle does not
1855 represent a valid file.
1856 @retval EFI_INVALID_PARAMETER If FileInfo is NULL.
1857
1858 **/
1859 EFI_STATUS
1860 EFIAPI
1861 PeiFfsFvPpiGetFileInfo2 (
1862 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,
1863 IN EFI_PEI_FILE_HANDLE FileHandle,
1864 OUT EFI_FV_FILE_INFO2 *FileInfo
1865 )
1866 {
1867 EFI_STATUS Status;
1868 PEI_CORE_FV_HANDLE *CoreFvHandle;
1869
1870 if ((FileHandle == NULL) || (FileInfo == NULL)) {
1871 return EFI_INVALID_PARAMETER;
1872 }
1873
1874 //
1875 // Retrieve the FirmwareVolume which the file resides in.
1876 //
1877 CoreFvHandle = FileHandleToVolume (FileHandle);
1878 if (CoreFvHandle == NULL) {
1879 return EFI_INVALID_PARAMETER;
1880 }
1881
1882 Status = PeiFfsFvPpiGetFileInfo (This, FileHandle, (EFI_FV_FILE_INFO *) FileInfo);
1883 if (!EFI_ERROR (Status)) {
1884 FileInfo->AuthenticationStatus = CoreFvHandle->AuthenticationStatus;
1885 }
1886
1887 return Status;
1888 }
1889
1890 /**
1891 This function returns information about the firmware volume.
1892
1893 @param This Points to this instance of the
1894 EFI_PEI_FIRMWARE_VOLUME_PPI.
1895 @param FvHandle Handle to the firmware handle.
1896 @param VolumeInfo Points to the returned firmware volume
1897 information.
1898
1899 @retval EFI_SUCCESS Information returned successfully.
1900 @retval EFI_INVALID_PARAMETER FvHandle does not indicate a valid
1901 firmware volume or VolumeInfo is NULL.
1902
1903 **/
1904 EFI_STATUS
1905 EFIAPI
1906 PeiFfsFvPpiGetVolumeInfo (
1907 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,
1908 IN EFI_PEI_FV_HANDLE FvHandle,
1909 OUT EFI_FV_INFO *VolumeInfo
1910 )
1911 {
1912 EFI_FIRMWARE_VOLUME_HEADER FwVolHeader;
1913 EFI_FIRMWARE_VOLUME_EXT_HEADER *FwVolExHeaderInfo;
1914
1915 if ((VolumeInfo == NULL) || (FvHandle == NULL)) {
1916 return EFI_INVALID_PARAMETER;
1917 }
1918
1919 //
1920 // VolumeHandle may not align at 8 byte,
1921 // but FvLength is UINT64 type, which requires FvHeader align at least 8 byte.
1922 // So, Copy FvHeader into the local FvHeader structure.
1923 //
1924 CopyMem (&FwVolHeader, FvHandle, sizeof (EFI_FIRMWARE_VOLUME_HEADER));
1925
1926 //
1927 // Check Fv Image Signature
1928 //
1929 if (FwVolHeader.Signature != EFI_FVH_SIGNATURE) {
1930 return EFI_INVALID_PARAMETER;
1931 }
1932
1933 ZeroMem (VolumeInfo, sizeof (EFI_FV_INFO));
1934 VolumeInfo->FvAttributes = FwVolHeader.Attributes;
1935 VolumeInfo->FvStart = (VOID *) FvHandle;
1936 VolumeInfo->FvSize = FwVolHeader.FvLength;
1937 CopyMem (&VolumeInfo->FvFormat, &FwVolHeader.FileSystemGuid, sizeof(EFI_GUID));
1938
1939 if (FwVolHeader.ExtHeaderOffset != 0) {
1940 FwVolExHeaderInfo = (EFI_FIRMWARE_VOLUME_EXT_HEADER*)(((UINT8 *)FvHandle) + FwVolHeader.ExtHeaderOffset);
1941 CopyMem (&VolumeInfo->FvName, &FwVolExHeaderInfo->FvName, sizeof(EFI_GUID));
1942 }
1943
1944 return EFI_SUCCESS;
1945 }
1946
1947 /**
1948 Find the next matching section in the firmware file.
1949
1950 This service enables PEI modules to discover sections
1951 of a given type within a valid file.
1952
1953 @param This Points to this instance of the
1954 EFI_PEI_FIRMWARE_VOLUME_PPI.
1955 @param SearchType A filter to find only sections of this
1956 type.
1957 @param FileHandle Handle of firmware file in which to
1958 search.
1959 @param SectionData Updated upon return to point to the
1960 section found.
1961
1962 @retval EFI_SUCCESS Section was found.
1963 @retval EFI_NOT_FOUND Section of the specified type was not
1964 found. SectionData contains NULL.
1965 **/
1966 EFI_STATUS
1967 EFIAPI
1968 PeiFfsFvPpiFindSectionByType (
1969 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,
1970 IN EFI_SECTION_TYPE SearchType,
1971 IN EFI_PEI_FILE_HANDLE FileHandle,
1972 OUT VOID **SectionData
1973 )
1974 {
1975 UINT32 AuthenticationStatus;
1976 return PeiFfsFvPpiFindSectionByType2 (This, SearchType, 0, FileHandle, SectionData, &AuthenticationStatus);
1977 }
1978
1979 /**
1980 Find the next matching section in the firmware file.
1981
1982 This service enables PEI modules to discover sections
1983 of a given instance and type within a valid file.
1984
1985 @param This Points to this instance of the
1986 EFI_PEI_FIRMWARE_VOLUME_PPI.
1987 @param SearchType A filter to find only sections of this
1988 type.
1989 @param SearchInstance A filter to find the specific instance
1990 of sections.
1991 @param FileHandle Handle of firmware file in which to
1992 search.
1993 @param SectionData Updated upon return to point to the
1994 section found.
1995 @param AuthenticationStatus Updated upon return to point to the
1996 authentication status for this section.
1997
1998 @retval EFI_SUCCESS Section was found.
1999 @retval EFI_NOT_FOUND Section of the specified type was not
2000 found. SectionData contains NULL.
2001 **/
2002 EFI_STATUS
2003 EFIAPI
2004 PeiFfsFvPpiFindSectionByType2 (
2005 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,
2006 IN EFI_SECTION_TYPE SearchType,
2007 IN UINTN SearchInstance,
2008 IN EFI_PEI_FILE_HANDLE FileHandle,
2009 OUT VOID **SectionData,
2010 OUT UINT32 *AuthenticationStatus
2011 )
2012 {
2013 EFI_STATUS Status;
2014 EFI_FFS_FILE_HEADER *FfsFileHeader;
2015 UINT32 FileSize;
2016 EFI_COMMON_SECTION_HEADER *Section;
2017 PEI_FW_VOL_INSTANCE *FwVolInstance;
2018 PEI_CORE_FV_HANDLE *CoreFvHandle;
2019 UINTN Instance;
2020 UINT32 ExtractedAuthenticationStatus;
2021
2022 if (SectionData == NULL) {
2023 return EFI_NOT_FOUND;
2024 }
2025
2026 FwVolInstance = PEI_FW_VOL_INSTANCE_FROM_FV_THIS (This);
2027
2028 //
2029 // Retrieve the FirmwareVolume which the file resides in.
2030 //
2031 CoreFvHandle = FileHandleToVolume (FileHandle);
2032 if (CoreFvHandle == NULL) {
2033 return EFI_NOT_FOUND;
2034 }
2035
2036 FfsFileHeader = (EFI_FFS_FILE_HEADER *)(FileHandle);
2037
2038 if (IS_FFS_FILE2 (FfsFileHeader)) {
2039 ASSERT (FFS_FILE2_SIZE (FfsFileHeader) > 0x00FFFFFF);
2040 if (!FwVolInstance->IsFfs3Fv) {
2041 DEBUG ((EFI_D_ERROR, "It is a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &FfsFileHeader->Name));
2042 return EFI_NOT_FOUND;
2043 }
2044 Section = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER2));
2045 FileSize = FFS_FILE2_SIZE (FfsFileHeader) - sizeof (EFI_FFS_FILE_HEADER2);
2046 } else {
2047 Section = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER));
2048 FileSize = FFS_FILE_SIZE (FfsFileHeader) - sizeof (EFI_FFS_FILE_HEADER);
2049 }
2050
2051 Instance = SearchInstance + 1;
2052 ExtractedAuthenticationStatus = 0;
2053 Status = ProcessSection (
2054 GetPeiServicesTablePointer (),
2055 SearchType,
2056 &Instance,
2057 Section,
2058 FileSize,
2059 SectionData,
2060 &ExtractedAuthenticationStatus,
2061 FwVolInstance->IsFfs3Fv
2062 );
2063 if (!EFI_ERROR (Status)) {
2064 //
2065 // Inherit the authentication status.
2066 //
2067 *AuthenticationStatus = ExtractedAuthenticationStatus | CoreFvHandle->AuthenticationStatus;
2068 }
2069 return Status;
2070 }
2071
2072 /**
2073 Convert the handle of FV to pointer of corresponding PEI_CORE_FV_HANDLE.
2074
2075 @param FvHandle The handle of a FV.
2076
2077 @retval NULL if can not find.
2078 @return Pointer of corresponding PEI_CORE_FV_HANDLE.
2079 **/
2080 PEI_CORE_FV_HANDLE *
2081 FvHandleToCoreHandle (
2082 IN EFI_PEI_FV_HANDLE FvHandle
2083 )
2084 {
2085 UINTN Index;
2086 PEI_CORE_INSTANCE *PrivateData;
2087
2088 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer());
2089 for (Index = 0; Index < PrivateData->FvCount; Index ++) {
2090 if (FvHandle == PrivateData->Fv[Index].FvHandle) {
2091 return &PrivateData->Fv[Index];
2092 }
2093 }
2094
2095 return NULL;
2096 }
2097
2098 /**
2099 Get instance of PEI_CORE_FV_HANDLE for next volume according to given index.
2100
2101 This routine also will install FvInfo ppi for FV hob in PI ways.
2102
2103 @param Private Pointer of PEI_CORE_INSTANCE
2104 @param Instance The index of FV want to be searched.
2105
2106 @return Instance of PEI_CORE_FV_HANDLE.
2107 **/
2108 PEI_CORE_FV_HANDLE *
2109 FindNextCoreFvHandle (
2110 IN PEI_CORE_INSTANCE *Private,
2111 IN UINTN Instance
2112 )
2113 {
2114 UINTN Index;
2115 BOOLEAN Match;
2116 EFI_HOB_FIRMWARE_VOLUME *FvHob;
2117
2118 //
2119 // Handle Framework FvHob and Install FvInfo Ppi for it.
2120 //
2121 if (FeaturePcdGet (PcdFrameworkCompatibilitySupport)) {
2122 //
2123 // Loop to search the wanted FirmwareVolume which supports FFS
2124 //
2125 FvHob = (EFI_HOB_FIRMWARE_VOLUME *)GetFirstHob (EFI_HOB_TYPE_FV);
2126 while (FvHob != NULL) {
2127 //
2128 // Search whether FvHob has been installed into PeiCore's FV database.
2129 // If found, no need install new FvInfoPpi for it.
2130 //
2131 for (Index = 0, Match = FALSE; Index < Private->FvCount; Index++) {
2132 if ((EFI_PEI_FV_HANDLE)(UINTN)FvHob->BaseAddress == Private->Fv[Index].FvHeader) {
2133 Match = TRUE;
2134 break;
2135 }
2136 }
2137
2138 //
2139 // Search whether FvHob has been cached into PeiCore's Unknown FV database.
2140 // If found, no need install new FvInfoPpi for it.
2141 //
2142 if (!Match) {
2143 for (Index = 0; Index < Private->UnknownFvInfoCount; Index ++) {
2144 if ((UINTN)FvHob->BaseAddress == (UINTN)Private->UnknownFvInfo[Index].FvInfo) {
2145 Match = TRUE;
2146 break;
2147 }
2148 }
2149 }
2150
2151 //
2152 // If the Fv in FvHob has not been installed into PeiCore's FV database and has
2153 // not been cached into PeiCore's Unknown FV database, install a new FvInfoPpi
2154 // for it then PeiCore will dispatch it in callback of FvInfoPpi.
2155 //
2156 if (!Match) {
2157 PeiServicesInstallFvInfoPpi (
2158 &(((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)FvHob->BaseAddress)->FileSystemGuid),
2159 (VOID *)(UINTN)FvHob->BaseAddress,
2160 (UINT32)FvHob->Length,
2161 NULL,
2162 NULL
2163 );
2164 }
2165
2166 FvHob = (EFI_HOB_FIRMWARE_VOLUME *)GetNextHob (EFI_HOB_TYPE_FV, (VOID *)((UINTN)FvHob + FvHob->Header.HobLength));
2167 }
2168 }
2169
2170 if (Instance >= Private->FvCount) {
2171 return NULL;
2172 }
2173
2174 return &Private->Fv[Instance];
2175 }
2176
2177 /**
2178 After PeiCore image is shadowed into permanent memory, all build-in FvPpi should
2179 be re-installed with the instance in permanent memory and all cached FvPpi pointers in
2180 PrivateData->Fv[] array should be fixed up to be pointed to the one in permanent
2181 memory.
2182
2183 @param PrivateData Pointer to PEI_CORE_INSTANCE.
2184 **/
2185 VOID
2186 PeiReinitializeFv (
2187 IN PEI_CORE_INSTANCE *PrivateData
2188 )
2189 {
2190 VOID *OldFfsFvPpi;
2191 EFI_PEI_PPI_DESCRIPTOR *OldDescriptor;
2192 UINTN Index;
2193 EFI_STATUS Status;
2194
2195 //
2196 // Locate old build-in Ffs2 EFI_PEI_FIRMWARE_VOLUME_PPI which
2197 // in flash.
2198 //
2199 Status = PeiServicesLocatePpi (
2200 &gEfiFirmwareFileSystem2Guid,
2201 0,
2202 &OldDescriptor,
2203 &OldFfsFvPpi
2204 );
2205 ASSERT_EFI_ERROR (Status);
2206
2207 //
2208 // Re-install the EFI_PEI_FIRMWARE_VOLUME_PPI for build-in Ffs2
2209 // which is shadowed from flash to permanent memory within PeiCore image.
2210 //
2211 Status = PeiServicesReInstallPpi (OldDescriptor, &mPeiFfs2FvPpiList);
2212 ASSERT_EFI_ERROR (Status);
2213
2214 //
2215 // Fixup all FvPpi pointers for the implementation in flash to permanent memory.
2216 //
2217 for (Index = 0; Index < PrivateData->FvCount; Index ++) {
2218 if (PrivateData->Fv[Index].FvPpi == OldFfsFvPpi) {
2219 PrivateData->Fv[Index].FvPpi = &mPeiFfs2FwVol.Fv;
2220 }
2221 }
2222
2223 //
2224 // Locate old build-in Ffs3 EFI_PEI_FIRMWARE_VOLUME_PPI which
2225 // in flash.
2226 //
2227 Status = PeiServicesLocatePpi (
2228 &gEfiFirmwareFileSystem3Guid,
2229 0,
2230 &OldDescriptor,
2231 &OldFfsFvPpi
2232 );
2233 ASSERT_EFI_ERROR (Status);
2234
2235 //
2236 // Re-install the EFI_PEI_FIRMWARE_VOLUME_PPI for build-in Ffs3
2237 // which is shadowed from flash to permanent memory within PeiCore image.
2238 //
2239 Status = PeiServicesReInstallPpi (OldDescriptor, &mPeiFfs3FvPpiList);
2240 ASSERT_EFI_ERROR (Status);
2241
2242 //
2243 // Fixup all FvPpi pointers for the implementation in flash to permanent memory.
2244 //
2245 for (Index = 0; Index < PrivateData->FvCount; Index ++) {
2246 if (PrivateData->Fv[Index].FvPpi == OldFfsFvPpi) {
2247 PrivateData->Fv[Index].FvPpi = &mPeiFfs3FwVol.Fv;
2248 }
2249 }
2250 }
2251
2252 /**
2253 Report the information for a new discoveried FV in unknown third-party format.
2254
2255 If the EFI_PEI_FIRMWARE_VOLUME_PPI has not been installed for third-party FV format, but
2256 the FV in this format has been discoveried, then this FV's information will be cached into
2257 PEI_CORE_INSTANCE's UnknownFvInfo array.
2258 Also a notification would be installed for unknown third-party FV format guid, if EFI_PEI_FIRMWARE_VOLUME_PPI
2259 is installed later by platform's PEIM, the original unknown third-party FV will be processed by
2260 using new installed EFI_PEI_FIRMWARE_VOLUME_PPI.
2261
2262 @param PrivateData Point to instance of PEI_CORE_INSTANCE
2263 @param FvInfo2Ppi Point to FvInfo2 PPI.
2264
2265 @retval EFI_OUT_OF_RESOURCES The FV info array in PEI_CORE_INSTANCE has no more spaces.
2266 @retval EFI_SUCCESS Success to add the information for unknown FV.
2267 **/
2268 EFI_STATUS
2269 AddUnknownFormatFvInfo (
2270 IN PEI_CORE_INSTANCE *PrivateData,
2271 IN EFI_PEI_FIRMWARE_VOLUME_INFO2_PPI *FvInfo2Ppi
2272 )
2273 {
2274 PEI_CORE_UNKNOW_FORMAT_FV_INFO *NewUnknownFv;
2275 VOID *TempPtr;
2276
2277 if (PrivateData->UnknownFvInfoCount >= PrivateData->MaxUnknownFvInfoCount) {
2278 //
2279 // Run out of room, grow the buffer.
2280 //
2281 TempPtr = AllocateZeroPool (
2282 sizeof (PEI_CORE_UNKNOW_FORMAT_FV_INFO) * (PrivateData->MaxUnknownFvInfoCount + FV_GROWTH_STEP)
2283 );
2284 ASSERT (TempPtr != NULL);
2285 CopyMem (
2286 TempPtr,
2287 PrivateData->UnknownFvInfo,
2288 sizeof (PEI_CORE_UNKNOW_FORMAT_FV_INFO) * PrivateData->MaxUnknownFvInfoCount
2289 );
2290 PrivateData->UnknownFvInfo = TempPtr;
2291 PrivateData->MaxUnknownFvInfoCount = PrivateData->MaxUnknownFvInfoCount + FV_GROWTH_STEP;
2292 }
2293
2294 NewUnknownFv = &PrivateData->UnknownFvInfo[PrivateData->UnknownFvInfoCount];
2295 PrivateData->UnknownFvInfoCount ++;
2296
2297 CopyGuid (&NewUnknownFv->FvFormat, &FvInfo2Ppi->FvFormat);
2298 NewUnknownFv->FvInfo = FvInfo2Ppi->FvInfo;
2299 NewUnknownFv->FvInfoSize = FvInfo2Ppi->FvInfoSize;
2300 NewUnknownFv->AuthenticationStatus = FvInfo2Ppi->AuthenticationStatus;
2301 NewUnknownFv->NotifyDescriptor.Flags = (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST);
2302 NewUnknownFv->NotifyDescriptor.Guid = &NewUnknownFv->FvFormat;
2303 NewUnknownFv->NotifyDescriptor.Notify = ThirdPartyFvPpiNotifyCallback;
2304
2305 PeiServicesNotifyPpi (&NewUnknownFv->NotifyDescriptor);
2306 return EFI_SUCCESS;
2307 }
2308
2309 /**
2310 Find the FV information according to third-party FV format guid.
2311
2312 This routine also will remove the FV information found by given FV format guid from
2313 PrivateData->UnknownFvInfo[].
2314
2315 @param PrivateData Point to instance of PEI_CORE_INSTANCE
2316 @param Format Point to given FV format guid
2317 @param FvInfo On return, the pointer of FV information buffer
2318 @param FvInfoSize On return, the size of FV information buffer.
2319 @param AuthenticationStatus On return, the authentication status of FV information buffer.
2320
2321 @retval EFI_NOT_FOUND The FV is not found for new installed EFI_PEI_FIRMWARE_VOLUME_PPI
2322 @retval EFI_SUCCESS Success to find a FV which could be processed by new installed EFI_PEI_FIRMWARE_VOLUME_PPI.
2323 **/
2324 EFI_STATUS
2325 FindUnknownFormatFvInfo (
2326 IN PEI_CORE_INSTANCE *PrivateData,
2327 IN EFI_GUID *Format,
2328 OUT VOID **FvInfo,
2329 OUT UINT32 *FvInfoSize,
2330 OUT UINT32 *AuthenticationStatus
2331 )
2332 {
2333 UINTN Index;
2334 UINTN Index2;
2335
2336 Index = 0;
2337 for (; Index < PrivateData->UnknownFvInfoCount; Index ++) {
2338 if (CompareGuid (Format, &PrivateData->UnknownFvInfo[Index].FvFormat)) {
2339 break;
2340 }
2341 }
2342
2343 if (Index == PrivateData->UnknownFvInfoCount) {
2344 return EFI_NOT_FOUND;
2345 }
2346
2347 *FvInfo = PrivateData->UnknownFvInfo[Index].FvInfo;
2348 *FvInfoSize = PrivateData->UnknownFvInfo[Index].FvInfoSize;
2349 *AuthenticationStatus = PrivateData->UnknownFvInfo[Index].AuthenticationStatus;
2350
2351 //
2352 // Remove an entry from UnknownFvInfo array.
2353 //
2354 Index2 = Index + 1;
2355 for (;Index2 < PrivateData->UnknownFvInfoCount; Index2 ++, Index ++) {
2356 CopyMem (&PrivateData->UnknownFvInfo[Index], &PrivateData->UnknownFvInfo[Index2], sizeof (PEI_CORE_UNKNOW_FORMAT_FV_INFO));
2357 }
2358 PrivateData->UnknownFvInfoCount --;
2359 return EFI_SUCCESS;
2360 }
2361
2362 /**
2363 Notification callback function for EFI_PEI_FIRMWARE_VOLUME_PPI.
2364
2365 When a EFI_PEI_FIRMWARE_VOLUME_PPI is installed to support new FV format, this
2366 routine is called to process all discoveried FVs in this format.
2367
2368 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
2369 @param NotifyDescriptor Address of the notification descriptor data structure.
2370 @param Ppi Address of the PPI that was installed.
2371
2372 @retval EFI_SUCCESS The notification callback is processed correctly.
2373 **/
2374 EFI_STATUS
2375 EFIAPI
2376 ThirdPartyFvPpiNotifyCallback (
2377 IN EFI_PEI_SERVICES **PeiServices,
2378 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
2379 IN VOID *Ppi
2380 )
2381 {
2382 PEI_CORE_INSTANCE *PrivateData;
2383 EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi;
2384 VOID *FvInfo;
2385 UINT32 FvInfoSize;
2386 UINT32 AuthenticationStatus;
2387 EFI_STATUS Status;
2388 EFI_PEI_FV_HANDLE FvHandle;
2389 BOOLEAN IsProcessed;
2390 UINTN FvIndex;
2391 EFI_PEI_FILE_HANDLE FileHandle;
2392 VOID *DepexData;
2393 UINTN CurFvCount;
2394 VOID *TempPtr;
2395
2396 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);
2397 FvPpi = (EFI_PEI_FIRMWARE_VOLUME_PPI*) Ppi;
2398
2399 do {
2400 Status = FindUnknownFormatFvInfo (PrivateData, NotifyDescriptor->Guid, &FvInfo, &FvInfoSize, &AuthenticationStatus);
2401 if (EFI_ERROR (Status)) {
2402 return EFI_SUCCESS;
2403 }
2404
2405 //
2406 // Process new found FV and get FV handle.
2407 //
2408 Status = FvPpi->ProcessVolume (FvPpi, FvInfo, FvInfoSize, &FvHandle);
2409 if (EFI_ERROR (Status)) {
2410 DEBUG ((EFI_D_ERROR, "Fail to process the FV 0x%p, FV may be corrupted!\n", FvInfo));
2411 continue;
2412 }
2413
2414 //
2415 // Check whether the FV has already been processed.
2416 //
2417 IsProcessed = FALSE;
2418 for (FvIndex = 0; FvIndex < PrivateData->FvCount; FvIndex ++) {
2419 if (PrivateData->Fv[FvIndex].FvHandle == FvHandle) {
2420 DEBUG ((EFI_D_INFO, "The Fv %p has already been processed!\n", FvInfo));
2421 IsProcessed = TRUE;
2422 break;
2423 }
2424 }
2425
2426 if (IsProcessed) {
2427 continue;
2428 }
2429
2430 if (PrivateData->FvCount >= PrivateData->MaxFvCount) {
2431 //
2432 // Run out of room, grow the buffer.
2433 //
2434 TempPtr = AllocateZeroPool (
2435 sizeof (PEI_CORE_FV_HANDLE) * (PrivateData->MaxFvCount + FV_GROWTH_STEP)
2436 );
2437 ASSERT (TempPtr != NULL);
2438 CopyMem (
2439 TempPtr,
2440 PrivateData->Fv,
2441 sizeof (PEI_CORE_FV_HANDLE) * PrivateData->MaxFvCount
2442 );
2443 PrivateData->Fv = TempPtr;
2444 PrivateData->MaxFvCount = PrivateData->MaxFvCount + FV_GROWTH_STEP;
2445 }
2446
2447 //
2448 // Update internal PEI_CORE_FV array.
2449 //
2450 PrivateData->Fv[PrivateData->FvCount].FvHeader = (EFI_FIRMWARE_VOLUME_HEADER*) FvInfo;
2451 PrivateData->Fv[PrivateData->FvCount].FvPpi = FvPpi;
2452 PrivateData->Fv[PrivateData->FvCount].FvHandle = FvHandle;
2453 PrivateData->Fv[PrivateData->FvCount].AuthenticationStatus = AuthenticationStatus;
2454 CurFvCount = PrivateData->FvCount;
2455 DEBUG ((
2456 EFI_D_INFO,
2457 "The %dth FV start address is 0x%11p, size is 0x%08x, handle is 0x%p\n",
2458 (UINT32) CurFvCount,
2459 (VOID *) FvInfo,
2460 FvInfoSize,
2461 FvHandle
2462 ));
2463 PrivateData->FvCount ++;
2464
2465 //
2466 // Scan and process the new discoveried FV for EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
2467 //
2468 FileHandle = NULL;
2469 do {
2470 Status = FvPpi->FindFileByType (
2471 FvPpi,
2472 EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE,
2473 FvHandle,
2474 &FileHandle
2475 );
2476 if (!EFI_ERROR (Status)) {
2477 Status = FvPpi->FindSectionByType (
2478 FvPpi,
2479 EFI_SECTION_PEI_DEPEX,
2480 FileHandle,
2481 (VOID**)&DepexData
2482 );
2483 if (!EFI_ERROR (Status)) {
2484 if (!PeimDispatchReadiness (PeiServices, DepexData)) {
2485 //
2486 // Dependency is not satisfied.
2487 //
2488 continue;
2489 }
2490 }
2491
2492 DEBUG ((EFI_D_INFO, "Found firmware volume Image File %p in FV[%d] %p\n", FileHandle, CurFvCount, FvHandle));
2493 ProcessFvFile (PrivateData, &PrivateData->Fv[CurFvCount], FileHandle);
2494 }
2495 } while (FileHandle != NULL);
2496 } while (TRUE);
2497 }