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