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