]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Pei/FwVol/FwVol.c
Following 3 updates are for spec update published in PI 1.2.
[mirror_edk2.git] / MdeModulePkg / Core / Pei / FwVol / FwVol.c
1 /** @file
2 Pei Core Firmware File System service routines.
3
4 Copyright (c) 2006 - 2009, Intel Corporation
5 All rights reserved. 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 // Post a call-back for the FvInfoPPI services to expose
315 // additional Fvs to PeiCore.
316 //
317 Status = PeiServicesNotifyPpi (&mNotifyOnFvInfoList);
318 ASSERT_EFI_ERROR (Status);
319
320 }
321
322 /**
323 Process Firmware Volum Information once FvInfoPPI install.
324 The FV Info will be registered into PeiCore private data structure.
325 And search the inside FV image, if found, the new FV INFO PPI will be installed.
326
327 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
328 @param NotifyDescriptor Address of the notification descriptor data structure.
329 @param Ppi Address of the PPI that was installed.
330
331 @retval EFI_SUCCESS The FV Info is registered into PeiCore private data structure.
332 @return if not EFI_SUCESS, fail to verify FV.
333
334 **/
335 EFI_STATUS
336 EFIAPI
337 FirmwareVolmeInfoPpiNotifyCallback (
338 IN EFI_PEI_SERVICES **PeiServices,
339 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
340 IN VOID *Ppi
341 )
342 {
343 EFI_PEI_FIRMWARE_VOLUME_INFO_PPI *FvInfoPpi;
344 EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi;
345 PEI_CORE_INSTANCE *PrivateData;
346 EFI_STATUS Status;
347 EFI_PEI_FV_HANDLE FvHandle;
348 UINTN FvIndex;
349
350 Status = EFI_SUCCESS;
351 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);
352
353 if (PrivateData->FvCount >= FixedPcdGet32 (PcdPeiCoreMaxFvSupported)) {
354 DEBUG ((EFI_D_ERROR, "The number of Fv Images (%d) exceed the max supported FVs (%d) in Pei", PrivateData->FvCount + 1, FixedPcdGet32 (PcdPeiCoreMaxFvSupported)));
355 DEBUG ((EFI_D_ERROR, "PcdPeiCoreMaxFvSupported value need be reconfigurated in DSC"));
356 ASSERT (FALSE);
357 }
358
359 FvInfoPpi = (EFI_PEI_FIRMWARE_VOLUME_INFO_PPI *)Ppi;
360
361 //
362 // Locate the corresponding FV_PPI according to founded FV's format guid
363 //
364 Status = PeiServicesLocatePpi (
365 &FvInfoPpi->FvFormat,
366 0,
367 NULL,
368 (VOID**)&FvPpi
369 );
370 if (!EFI_ERROR (Status)) {
371 //
372 // Process new found FV and get FV handle.
373 //
374 Status = FvPpi->ProcessVolume (FvPpi, FvInfoPpi->FvInfo, FvInfoPpi->FvInfoSize, &FvHandle);
375 if (EFI_ERROR (Status)) {
376 DEBUG ((EFI_D_ERROR, "Fail to process new found FV, FV may be corrupted!"));
377 return Status;
378 }
379 DEBUG ((EFI_D_INFO, "Found and process new FV %p, all fv's count is %d\n", FvHandle, PrivateData->FvCount));
380 } else {
381 DEBUG ((EFI_D_ERROR, "Fail to process FV %p because no corresponding EFI_FIRMWARE_VOLUME_PPI is found!\n", FvInfoPpi->FvInfo));
382
383 //
384 // If can not find EFI_FIRMWARE_VOLUME_PPI to process firmware to get FvHandle,
385 // use the address of FV buffer as its handle.
386 //
387 FvHandle = FvInfoPpi->FvInfo;
388
389 //
390 // Check whether the FV has already been processed.
391 //
392 for (FvIndex = 0; FvIndex < PrivateData->FvCount; FvIndex ++) {
393 if (PrivateData->Fv[FvIndex].FvHandle == FvHandle) {
394 DEBUG ((EFI_D_INFO, "The Fv %p has already been processed!\n", FvHandle));
395 return EFI_SUCCESS;
396 }
397 }
398
399 PrivateData->Fv[PrivateData->FvCount].FvHeader = (EFI_FIRMWARE_VOLUME_HEADER*) FvInfoPpi->FvInfo;
400 PrivateData->Fv[PrivateData->FvCount].FvPpi = NULL;
401 PrivateData->Fv[PrivateData->FvCount].FvHandle = FvHandle;
402 PrivateData->FvCount ++;
403 }
404
405 return EFI_SUCCESS;
406 }
407
408 /**
409 Go through the file to search SectionType section.
410 Search within encapsulation sections (compression and GUIDed) recursively,
411 until the match section is found.
412
413 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
414 @param SectionType Filter to find only section of this type.
415 @param Section From where to search.
416 @param SectionSize The file size to search.
417 @param OutputBuffer A pointer to the discovered section, if successful.
418 NULL if section not found
419
420 @return EFI_NOT_FOUND The match section is not found.
421 @return EFI_SUCCESS The match section is found.
422
423 **/
424 EFI_STATUS
425 ProcessSection (
426 IN CONST EFI_PEI_SERVICES **PeiServices,
427 IN EFI_SECTION_TYPE SectionType,
428 IN EFI_COMMON_SECTION_HEADER *Section,
429 IN UINTN SectionSize,
430 OUT VOID **OutputBuffer
431 )
432 {
433 EFI_STATUS Status;
434 UINT32 SectionLength;
435 UINT32 ParsedLength;
436 EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI *GuidSectionPpi;
437 EFI_PEI_DECOMPRESS_PPI *DecompressPpi;
438 VOID *PpiOutput;
439 UINTN PpiOutputSize;
440 UINTN Index;
441 UINT32 Authentication;
442 PEI_CORE_INSTANCE *PrivateData;
443
444 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);
445 *OutputBuffer = NULL;
446 ParsedLength = 0;
447 Index = 0;
448 Status = EFI_NOT_FOUND;
449 PpiOutput = NULL;
450 PpiOutputSize = 0;
451 while (ParsedLength < SectionSize) {
452 if (Section->Type == SectionType) {
453 *OutputBuffer = (VOID *)(Section + 1);
454 return EFI_SUCCESS;
455 } else if ((Section->Type == EFI_SECTION_GUID_DEFINED) || (Section->Type == EFI_SECTION_COMPRESSION)) {
456 //
457 // Check the encapsulated section is extracted into the cache data.
458 //
459 for (Index = 0; Index < PrivateData->CacheSection.AllSectionCount; Index ++) {
460 if (Section == PrivateData->CacheSection.Section[Index]) {
461 PpiOutput = PrivateData->CacheSection.SectionData[Index];
462 PpiOutputSize = PrivateData->CacheSection.SectionSize[Index];
463 //
464 // Search section directly from the cache data.
465 //
466 return ProcessSection (
467 PeiServices,
468 SectionType,
469 PpiOutput,
470 PpiOutputSize,
471 OutputBuffer
472 );
473 }
474 }
475
476 Status = EFI_NOT_FOUND;
477 if (Section->Type == EFI_SECTION_GUID_DEFINED) {
478 Status = PeiServicesLocatePpi (
479 &((EFI_GUID_DEFINED_SECTION *)Section)->SectionDefinitionGuid,
480 0,
481 NULL,
482 (VOID **) &GuidSectionPpi
483 );
484 if (!EFI_ERROR (Status)) {
485 Status = GuidSectionPpi->ExtractSection (
486 GuidSectionPpi,
487 Section,
488 &PpiOutput,
489 &PpiOutputSize,
490 &Authentication
491 );
492 }
493 } else if (Section->Type == EFI_SECTION_COMPRESSION) {
494 Status = PeiServicesLocatePpi (&gEfiPeiDecompressPpiGuid, 0, NULL, (VOID **) &DecompressPpi);
495 if (!EFI_ERROR (Status)) {
496 Status = DecompressPpi->Decompress (
497 DecompressPpi,
498 (CONST EFI_COMPRESSION_SECTION*) Section,
499 &PpiOutput,
500 &PpiOutputSize
501 );
502 }
503 }
504
505 if (!EFI_ERROR (Status)) {
506 //
507 // Update cache section data.
508 //
509 if (PrivateData->CacheSection.AllSectionCount < CACHE_SETION_MAX_NUMBER) {
510 PrivateData->CacheSection.AllSectionCount ++;
511 }
512 PrivateData->CacheSection.Section [PrivateData->CacheSection.SectionIndex] = Section;
513 PrivateData->CacheSection.SectionData [PrivateData->CacheSection.SectionIndex] = PpiOutput;
514 PrivateData->CacheSection.SectionSize [PrivateData->CacheSection.SectionIndex] = PpiOutputSize;
515 PrivateData->CacheSection.SectionIndex = (PrivateData->CacheSection.SectionIndex + 1)%CACHE_SETION_MAX_NUMBER;
516
517 return ProcessSection (
518 PeiServices,
519 SectionType,
520 PpiOutput,
521 PpiOutputSize,
522 OutputBuffer
523 );
524 }
525 }
526
527 //
528 // Size is 24 bits wide so mask upper 8 bits.
529 // SectionLength is adjusted it is 4 byte aligned.
530 // Go to the next section
531 //
532 SectionLength = *(UINT32 *)Section->Size & 0x00FFFFFF;
533 SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);
534 ASSERT (SectionLength != 0);
535 ParsedLength += SectionLength;
536 Section = (EFI_COMMON_SECTION_HEADER *)((UINT8 *)Section + SectionLength);
537 }
538
539 return EFI_NOT_FOUND;
540 }
541
542
543 /**
544 Searches for the next matching section within the specified file.
545
546 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
547 @param SectionType Filter to find only sections of this type.
548 @param FileHandle Pointer to the current file to search.
549 @param SectionData A pointer to the discovered section, if successful.
550 NULL if section not found
551
552 @retval EFI_NOT_FOUND The section was not found.
553 @retval EFI_SUCCESS The section was found.
554
555 **/
556 EFI_STATUS
557 EFIAPI
558 PeiFfsFindSectionData (
559 IN CONST EFI_PEI_SERVICES **PeiServices,
560 IN EFI_SECTION_TYPE SectionType,
561 IN EFI_PEI_FILE_HANDLE FileHandle,
562 OUT VOID **SectionData
563 )
564 {
565 PEI_CORE_FV_HANDLE *CoreFvHandle;
566
567 CoreFvHandle = FileHandleToVolume (FileHandle);
568 if ((CoreFvHandle == NULL) || (CoreFvHandle->FvPpi == NULL)) {
569 return EFI_NOT_FOUND;
570 }
571
572 return CoreFvHandle->FvPpi->FindSectionByType (CoreFvHandle->FvPpi, SectionType, FileHandle, SectionData);
573 }
574
575 /**
576 Searches for the next matching file in the firmware volume.
577
578 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
579 @param SearchType Filter to find only files of this type.
580 Type EFI_FV_FILETYPE_ALL causes no filtering to be done.
581 @param VolumeHandle Handle of firmware volume in which to search.
582 @param FileHandle On entry, points to the current handle from which to begin searching or NULL to start
583 at the beginning of the firmware volume. On exit, points the file handle of the next file
584 in the volume or NULL if there are no more files.
585
586 @retval EFI_NOT_FOUND The file was not found.
587 @retval EFI_NOT_FOUND The header checksum was not zero.
588 @retval EFI_SUCCESS The file was found.
589
590 **/
591 EFI_STATUS
592 EFIAPI
593 PeiFfsFindNextFile (
594 IN CONST EFI_PEI_SERVICES **PeiServices,
595 IN UINT8 SearchType,
596 IN EFI_PEI_FV_HANDLE FvHandle,
597 IN OUT EFI_PEI_FILE_HANDLE *FileHandle
598 )
599 {
600 PEI_CORE_FV_HANDLE *CoreFvHandle;
601
602 CoreFvHandle = FvHandleToCoreHandle (FvHandle);
603
604 //
605 // To make backward compatiblity, if can not find corresponding the handle of FV
606 // then treat FV as build-in FFS2 format and memory mapped FV that FV handle is pointed
607 // to the address of first byte of FV.
608 //
609 if ((CoreFvHandle == NULL) && FeaturePcdGet (PcdFrameworkCompatibilitySupport)) {
610 return FindFileEx (FvHandle, NULL, SearchType, FileHandle, NULL);
611 }
612
613 if ((CoreFvHandle == NULL) || CoreFvHandle->FvPpi == NULL) {
614 return EFI_NOT_FOUND;
615 }
616
617 return CoreFvHandle->FvPpi->FindFileByType (CoreFvHandle->FvPpi, SearchType, FvHandle, FileHandle);
618 }
619
620
621 /**
622 Search the firmware volumes by index
623
624 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
625 @param Instance This instance of the firmware volume to find. The value 0 is the Boot Firmware
626 Volume (BFV).
627 @param VolumeHandle On exit, points to the next volume handle or NULL if it does not exist.
628
629 @retval EFI_INVALID_PARAMETER VolumeHandle is NULL
630 @retval EFI_NOT_FOUND The volume was not found.
631 @retval EFI_SUCCESS The volume was found.
632
633 **/
634 EFI_STATUS
635 EFIAPI
636 PeiFfsFindNextVolume (
637 IN CONST EFI_PEI_SERVICES **PeiServices,
638 IN UINTN Instance,
639 IN OUT EFI_PEI_FV_HANDLE *VolumeHandle
640 )
641 {
642 PEI_CORE_INSTANCE *Private;
643 PEI_CORE_FV_HANDLE *CoreFvHandle;
644
645 Private = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);
646
647 CoreFvHandle = FindNextCoreFvHandle (Private, Instance);
648 if (CoreFvHandle == NULL) {
649 *VolumeHandle = NULL;
650 return EFI_NOT_FOUND;
651 }
652
653 *VolumeHandle = CoreFvHandle->FvHandle;
654
655 return EFI_SUCCESS;
656 }
657
658
659 /**
660 Find a file within a volume by its name.
661
662 @param FileName A pointer to the name of the file to find within the firmware volume.
663 @param VolumeHandle The firmware volume to search
664 @param FileHandle Upon exit, points to the found file's handle
665 or NULL if it could not be found.
666
667 @retval EFI_SUCCESS File was found.
668 @retval EFI_NOT_FOUND File was not found.
669 @retval EFI_INVALID_PARAMETER VolumeHandle or FileHandle or FileName was NULL.
670
671 **/
672 EFI_STATUS
673 EFIAPI
674 PeiFfsFindFileByName (
675 IN CONST EFI_GUID *FileName,
676 IN EFI_PEI_FV_HANDLE VolumeHandle,
677 OUT EFI_PEI_FILE_HANDLE *FileHandle
678 )
679 {
680 PEI_CORE_FV_HANDLE *CoreFvHandle;
681
682 if ((VolumeHandle == NULL) || (FileName == NULL) || (FileHandle == NULL)) {
683 return EFI_INVALID_PARAMETER;
684 }
685
686 CoreFvHandle = FvHandleToCoreHandle (VolumeHandle);
687 if ((CoreFvHandle == NULL) || (CoreFvHandle->FvPpi == NULL)) {
688 return EFI_NOT_FOUND;
689 }
690
691 return CoreFvHandle->FvPpi->FindFileByName (CoreFvHandle->FvPpi, FileName, &VolumeHandle, FileHandle);
692 }
693
694 /**
695 Returns information about a specific file.
696
697 @param FileHandle Handle of the file.
698 @param FileInfo Upon exit, points to the file’s information.
699
700 @retval EFI_INVALID_PARAMETER If FileInfo is NULL.
701 @retval EFI_INVALID_PARAMETER If FileHandle does not represent a valid file.
702 @retval EFI_SUCCESS File information returned.
703
704 **/
705 EFI_STATUS
706 EFIAPI
707 PeiFfsGetFileInfo (
708 IN EFI_PEI_FILE_HANDLE FileHandle,
709 OUT EFI_FV_FILE_INFO *FileInfo
710 )
711 {
712 PEI_CORE_FV_HANDLE *CoreFvHandle;
713
714 if ((FileHandle == NULL) || (FileInfo == NULL)) {
715 return EFI_INVALID_PARAMETER;
716 }
717
718 //
719 // Retrieve the FirmwareVolume which the file resides in.
720 //
721 CoreFvHandle = FileHandleToVolume (FileHandle);
722 if ((CoreFvHandle == NULL) || (CoreFvHandle->FvPpi == NULL)) {
723 return EFI_INVALID_PARAMETER;
724 }
725
726 return CoreFvHandle->FvPpi->GetFileInfo (CoreFvHandle->FvPpi, FileHandle, FileInfo);
727 }
728
729
730 /**
731 Returns information about the specified volume.
732
733 This function returns information about a specific firmware
734 volume, including its name, type, attributes, starting address
735 and size.
736
737 @param VolumeHandle Handle of the volume.
738 @param VolumeInfo Upon exit, points to the volume's information.
739
740 @retval EFI_SUCCESS Volume information returned.
741 @retval EFI_INVALID_PARAMETER If VolumeHandle does not represent a valid volume.
742 @retval EFI_INVALID_PARAMETER If VolumeHandle is NULL.
743 @retval EFI_SUCCESS Information successfully returned.
744 @retval EFI_INVALID_PARAMETER The volume designated by the VolumeHandle is not available.
745
746 **/
747 EFI_STATUS
748 EFIAPI
749 PeiFfsGetVolumeInfo (
750 IN EFI_PEI_FV_HANDLE VolumeHandle,
751 OUT EFI_FV_INFO *VolumeInfo
752 )
753 {
754 PEI_CORE_FV_HANDLE *CoreHandle;
755
756 if (VolumeInfo == NULL) {
757 return EFI_INVALID_PARAMETER;
758 }
759
760 CoreHandle = FvHandleToCoreHandle (VolumeHandle);
761
762 if ((CoreHandle == NULL) || (CoreHandle->FvPpi == NULL)) {
763 return EFI_INVALID_PARAMETER;
764 }
765
766 return CoreHandle->FvPpi->GetVolumeInfo (CoreHandle->FvPpi, VolumeHandle, VolumeInfo);
767 }
768
769 /**
770 Get Fv image from the FV type file, then install FV INFO ppi, Build FV hob.
771
772 @param ParentFvCoreHandle Pointer of EFI_CORE_FV_HANDLE to parent Fv image that contain this Fv image.
773 @param ParentFvFileHandle File handle of a Fv type file that contain this Fv image.
774
775 @retval EFI_NOT_FOUND FV image can't be found.
776 @retval EFI_SUCCESS Successfully to process it.
777 @retval EFI_OUT_OF_RESOURCES Can not allocate page when aligning FV image
778 @retval Others Can not find EFI_SECTION_FIRMWARE_VOLUME_IMAGE section
779
780 **/
781 EFI_STATUS
782 ProcessFvFile (
783 IN PEI_CORE_FV_HANDLE *ParentFvCoreHandle,
784 IN EFI_PEI_FILE_HANDLE ParentFvFileHandle
785 )
786 {
787 EFI_STATUS Status;
788 EFI_FV_INFO ParentFvImageInfo;
789 UINT32 FvAlignment;
790 VOID *NewFvBuffer;
791 EFI_PEI_HOB_POINTERS HobPtr;
792 EFI_PEI_FIRMWARE_VOLUME_PPI *ParentFvPpi;
793 EFI_PEI_FV_HANDLE ParentFvHandle;
794 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;
795 EFI_FV_FILE_INFO FileInfo;
796
797 //
798 // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has already
799 // been extracted.
800 //
801 HobPtr.Raw = GetHobList ();
802 while ((HobPtr.Raw = GetNextHob (EFI_HOB_TYPE_FV2, HobPtr.Raw)) != NULL) {
803 if (CompareGuid (&(((EFI_FFS_FILE_HEADER *)ParentFvFileHandle)->Name), &HobPtr.FirmwareVolume2->FileName)) {
804 //
805 // this FILE has been dispatched, it will not be dispatched again.
806 //
807 DEBUG ((EFI_D_INFO, "FV file %p has been dispatched!\r\n", ParentFvFileHandle));
808 return EFI_SUCCESS;
809 }
810 HobPtr.Raw = GET_NEXT_HOB (HobPtr);
811 }
812
813 ParentFvHandle = ParentFvCoreHandle->FvHandle;
814 ParentFvPpi = ParentFvCoreHandle->FvPpi;
815
816 //
817 // Find FvImage in FvFile
818 //
819 Status = ParentFvPpi->FindSectionByType (
820 ParentFvPpi,
821 EFI_SECTION_FIRMWARE_VOLUME_IMAGE,
822 ParentFvFileHandle,
823 (VOID **)&FvHeader
824 );
825
826 if (EFI_ERROR (Status)) {
827 return Status;
828 }
829
830 //
831 // FvAlignment must be more than 8 bytes required by FvHeader structure.
832 //
833 FvAlignment = 1 << ((FvHeader->Attributes & EFI_FVB2_ALIGNMENT) >> 16);
834 if (FvAlignment < 8) {
835 FvAlignment = 8;
836 }
837
838 //
839 // Check FvImage
840 //
841 if ((UINTN) FvHeader % FvAlignment != 0) {
842 NewFvBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES ((UINT32) FvHeader->FvLength), FvAlignment);
843 if (NewFvBuffer == NULL) {
844 return EFI_OUT_OF_RESOURCES;
845 }
846 CopyMem (NewFvBuffer, FvHeader, (UINTN) FvHeader->FvLength);
847 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER*) NewFvBuffer;
848 }
849
850 Status = ParentFvPpi->GetVolumeInfo (ParentFvPpi, ParentFvHandle, &ParentFvImageInfo);
851 ASSERT_EFI_ERROR (Status);
852
853 Status = ParentFvPpi->GetFileInfo (ParentFvPpi, ParentFvFileHandle, &FileInfo);
854 ASSERT_EFI_ERROR (Status);
855
856 //
857 // Install FvPpi and Build FvHob
858 //
859 PeiServicesInstallFvInfoPpi (
860 &FvHeader->FileSystemGuid,
861 (VOID**) FvHeader,
862 (UINT32) FvHeader->FvLength,
863 &ParentFvImageInfo.FvName,
864 &FileInfo.FileName
865 );
866
867 //
868 // Inform the extracted FvImage to Fv HOB consumer phase, i.e. DXE phase
869 //
870 BuildFvHob (
871 (EFI_PHYSICAL_ADDRESS) (UINTN) FvHeader,
872 FvHeader->FvLength
873 );
874
875 //
876 // Makes the encapsulated volume show up in DXE phase to skip processing of
877 // encapsulated file again.
878 //
879 BuildFv2Hob (
880 (EFI_PHYSICAL_ADDRESS) (UINTN) FvHeader,
881 FvHeader->FvLength,
882 &ParentFvImageInfo.FvName,
883 &FileInfo.FileName
884 );
885
886 return EFI_SUCCESS;
887 }
888
889 /**
890 Process a firmware volume and create a volume handle.
891
892 Create a volume handle from the information in the buffer. For
893 memory-mapped firmware volumes, Buffer and BufferSize refer to
894 the start of the firmware volume and the firmware volume size.
895 For non memory-mapped firmware volumes, this points to a
896 buffer which contains the necessary information for creating
897 the firmware volume handle. Normally, these values are derived
898 from the EFI_FIRMWARE_VOLUME_INFO_PPI.
899
900
901 @param This Points to this instance of the
902 EFI_PEI_FIRMWARE_VOLUME_PPI.
903 @param Buffer Points to the start of the buffer.
904 @param BufferSize Size of the buffer.
905 @param FvHandle Points to the returned firmware volume
906 handle. The firmware volume handle must
907 be unique within the system.
908
909 @retval EFI_SUCCESS Firmware volume handle created.
910 @retval EFI_VOLUME_CORRUPTED Volume was corrupt.
911
912 **/
913 EFI_STATUS
914 EFIAPI
915 PeiFfs2FvPpiProcessVolume (
916 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,
917 IN VOID *Buffer,
918 IN UINTN BufferSize,
919 OUT EFI_PEI_FV_HANDLE *FvHandle
920 )
921 {
922 EFI_STATUS Status;
923 PEI_CORE_INSTANCE *PrivateData;
924 EFI_PEI_FILE_HANDLE FileHandle;
925 VOID *DepexData;
926 EFI_PEI_SERVICES **PeiServices;
927 UINTN FvIndex;
928
929 PeiServices = (EFI_PEI_SERVICES**) GetPeiServicesTablePointer ();
930 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);
931
932 //
933 // The build-in EFI_PEI_FIRMWARE_VOLUME_PPI for FFS2 support memory-mapped
934 // FV image and the handle is pointed to Fv image's buffer.
935 //
936 *FvHandle = (EFI_PEI_FV_HANDLE) Buffer;
937
938 //
939 // Do verify for given FV buffer.
940 //
941 Status = VerifyFv ((EFI_FIRMWARE_VOLUME_HEADER*) Buffer);
942 if (EFI_ERROR(Status)) {
943 DEBUG ((EFI_D_ERROR, "Fail to verify FV which address is 0x%11p", Buffer));
944 return EFI_VOLUME_CORRUPTED;
945 }
946
947 //
948 // Check whether the FV has already been processed.
949 //
950 for (FvIndex = 0; FvIndex < PrivateData->FvCount; FvIndex ++) {
951 if (PrivateData->Fv[FvIndex].FvHandle == *FvHandle) {
952 DEBUG ((EFI_D_INFO, "The Fv %p has already been processed!", Buffer));
953 return EFI_SUCCESS;
954 }
955 }
956
957 //
958 // Update internal PEI_CORE_FV array.
959 //
960 PrivateData->Fv[PrivateData->FvCount].FvHeader = (EFI_FIRMWARE_VOLUME_HEADER*) Buffer;
961 PrivateData->Fv[PrivateData->FvCount].FvPpi = (EFI_PEI_FIRMWARE_VOLUME_PPI*) This;
962 PrivateData->Fv[PrivateData->FvCount].FvHandle = *FvHandle;
963
964 DEBUG ((EFI_D_INFO,
965 "The %dth FV start address is 0x%11p and size is 0x%08x\n",
966 (UINT32) PrivateData->FvCount,
967 (VOID *) Buffer,
968 BufferSize
969 ));
970 PrivateData->FvCount ++;
971
972 FileHandle = NULL;
973
974 do {
975 Status = This->FindFileByType (
976 This,
977 EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE,
978 *FvHandle,
979 &FileHandle
980 );
981 if (!EFI_ERROR (Status)) {
982 Status = This->FindSectionByType (
983 This,
984 EFI_SECTION_PEI_DEPEX,
985 FileHandle,
986 (VOID**)&DepexData
987 );
988 if (!EFI_ERROR (Status)) {
989 if (!PeimDispatchReadiness (PeiServices, DepexData)) {
990 //
991 // Dependency is not satisfied.
992 //
993 continue;
994 }
995 }
996
997 DEBUG ((EFI_D_INFO, "Found firmware volume Image File %p in FV[%d] %p", FileHandle, PrivateData->FvCount - 1, *FvHandle));
998 ProcessFvFile (&PrivateData->Fv[PrivateData->FvCount - 1], FileHandle);
999 }
1000 } while (FileHandle != NULL);
1001
1002 return EFI_SUCCESS;
1003 }
1004
1005 /**
1006 Finds the next file of the specified type.
1007
1008 This service enables PEI modules to discover additional firmware files.
1009 The FileHandle must be unique within the system.
1010
1011 @param This Points to this instance of the
1012 EFI_PEI_FIRMWARE_VOLUME_PPI.
1013 @param SearchType A filter to find only files of this type. Type
1014 EFI_FV_FILETYPE_ALL causes no filtering to be
1015 done.
1016 @param FvHandle Handle of firmware volume in which to
1017 search.
1018 @param FileHandle Points to the current handle from which to
1019 begin searching or NULL to start at the
1020 beginning of the firmware volume. Updated
1021 upon return to reflect the file found.
1022
1023 @retval EFI_SUCCESS The file was found.
1024 @retval EFI_NOT_FOUND The file was not found. FileHandle contains NULL.
1025
1026 **/
1027 EFI_STATUS
1028 EFIAPI
1029 PeiFfs2FvPpiFindFileByType (
1030 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,
1031 IN EFI_FV_FILETYPE SearchType,
1032 IN EFI_PEI_FV_HANDLE FvHandle,
1033 IN OUT EFI_PEI_FILE_HANDLE *FileHandle
1034 )
1035 {
1036 return FindFileEx (FvHandle, NULL, SearchType, FileHandle, NULL);
1037 }
1038
1039 /**
1040 Find a file within a volume by its name.
1041
1042 This service searches for files with a specific name, within
1043 either the specified firmware volume or all firmware volumes.
1044
1045 @param This Points to this instance of the
1046 EFI_PEI_FIRMWARE_VOLUME_PPI.
1047 @param FileName A pointer to the name of the file to find
1048 within the firmware volume.
1049 @param FvHandle Upon entry, the pointer to the firmware
1050 volume to search or NULL if all firmware
1051 volumes should be searched. Upon exit, the
1052 actual firmware volume in which the file was
1053 found.
1054 @param FileHandle Upon exit, points to the found file's
1055 handle or NULL if it could not be found.
1056
1057 @retval EFI_SUCCESS File was found.
1058 @retval EFI_NOT_FOUND File was not found.
1059 @retval EFI_INVALID_PARAMETER FvHandle or FileHandle or
1060 FileName was NULL.
1061
1062
1063 **/
1064 EFI_STATUS
1065 EFIAPI
1066 PeiFfs2FvPpiFindFileByName (
1067 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,
1068 IN CONST EFI_GUID *FileName,
1069 IN EFI_PEI_FV_HANDLE *FvHandle,
1070 OUT EFI_PEI_FILE_HANDLE *FileHandle
1071 )
1072 {
1073 EFI_STATUS Status;
1074 PEI_CORE_INSTANCE *PrivateData;
1075 UINTN Index;
1076
1077 if ((FvHandle == NULL) || (FileName == NULL) || (FileHandle == NULL)) {
1078 return EFI_INVALID_PARAMETER;
1079 }
1080
1081 if (*FvHandle != NULL) {
1082 Status = FindFileEx (*FvHandle, FileName, 0, FileHandle, NULL);
1083 if (Status == EFI_NOT_FOUND) {
1084 *FileHandle = NULL;
1085 }
1086 } else {
1087 //
1088 // If *FvHandle = NULL, so search all FV for given filename
1089 //
1090 Status = EFI_NOT_FOUND;
1091
1092 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer());
1093 for (Index = 0; Index < PrivateData->FvCount; Index ++) {
1094 //
1095 // Only search the FV which is associated with a EFI_PEI_FIRMWARE_VOLUME_PPI instance.
1096 //
1097 if (PrivateData->Fv[Index].FvPpi != NULL) {
1098 Status = FindFileEx (PrivateData->Fv[Index].FvHandle, FileName, 0, FileHandle, NULL);
1099 if (!EFI_ERROR (Status)) {
1100 *FvHandle = PrivateData->Fv[Index].FvHandle;
1101 }
1102 }
1103 }
1104 }
1105
1106 return Status;
1107 }
1108
1109 /**
1110 Returns information about a specific file.
1111
1112 This function returns information about a specific
1113 file, including its file name, type, attributes, starting
1114 address and size.
1115
1116 @param This Points to this instance of the
1117 EFI_PEI_FIRMWARE_VOLUME_PPI.
1118 @param FileHandle Handle of the file.
1119 @param FileInfo Upon exit, points to the file's
1120 information.
1121
1122 @retval EFI_SUCCESS File information returned.
1123 @retval EFI_INVALID_PARAMETER If FileHandle does not
1124 represent a valid file.
1125 @retval EFI_INVALID_PARAMETER If FileInfo is NULL.
1126
1127 **/
1128 EFI_STATUS
1129 EFIAPI
1130 PeiFfs2FvPpiGetFileInfo (
1131 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,
1132 IN EFI_PEI_FILE_HANDLE FileHandle,
1133 OUT EFI_FV_FILE_INFO *FileInfo
1134 )
1135 {
1136 UINT8 FileState;
1137 UINT8 ErasePolarity;
1138 EFI_FFS_FILE_HEADER *FileHeader;
1139 PEI_CORE_FV_HANDLE *CoreFvHandle;
1140
1141 if ((FileHandle == NULL) || (FileInfo == NULL)) {
1142 return EFI_INVALID_PARAMETER;
1143 }
1144
1145 //
1146 // Retrieve the FirmwareVolume which the file resides in.
1147 //
1148 CoreFvHandle = FileHandleToVolume (FileHandle);
1149 if (CoreFvHandle == NULL) {
1150 return EFI_INVALID_PARAMETER;
1151 }
1152
1153 if (CoreFvHandle->FvHeader->Attributes & EFI_FVB2_ERASE_POLARITY) {
1154 ErasePolarity = 1;
1155 } else {
1156 ErasePolarity = 0;
1157 }
1158
1159 //
1160 // Get FileState which is the highest bit of the State
1161 //
1162 FileState = GetFileState (ErasePolarity, (EFI_FFS_FILE_HEADER*)FileHandle);
1163
1164 switch (FileState) {
1165 case EFI_FILE_DATA_VALID:
1166 case EFI_FILE_MARKED_FOR_UPDATE:
1167 break;
1168 default:
1169 return EFI_INVALID_PARAMETER;
1170 }
1171
1172 FileHeader = (EFI_FFS_FILE_HEADER *)FileHandle;
1173 CopyMem (&FileInfo->FileName, &FileHeader->Name, sizeof(EFI_GUID));
1174 FileInfo->FileType = FileHeader->Type;
1175 FileInfo->FileAttributes = FileHeader->Attributes;
1176 FileInfo->BufferSize = ((*(UINT32 *)FileHeader->Size) & 0x00FFFFFF) - sizeof (EFI_FFS_FILE_HEADER);
1177 FileInfo->Buffer = (FileHeader + 1);
1178 return EFI_SUCCESS;
1179 }
1180
1181 /**
1182 This function returns information about the firmware volume.
1183
1184 @param This Points to this instance of the
1185 EFI_PEI_FIRMWARE_VOLUME_PPI.
1186 @param FvHandle Handle to the firmware handle.
1187 @param VolumeInfo Points to the returned firmware volume
1188 information.
1189
1190 @retval EFI_SUCCESS Information returned successfully.
1191 @retval EFI_INVALID_PARAMETER FvHandle does not indicate a valid
1192 firmware volume or VolumeInfo is NULL.
1193
1194 **/
1195 EFI_STATUS
1196 EFIAPI
1197 PeiFfs2FvPpiGetVolumeInfo (
1198 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,
1199 IN EFI_PEI_FV_HANDLE FvHandle,
1200 OUT EFI_FV_INFO *VolumeInfo
1201 )
1202 {
1203 EFI_FIRMWARE_VOLUME_HEADER FwVolHeader;
1204 EFI_FIRMWARE_VOLUME_EXT_HEADER *FwVolExHeaderInfo;
1205
1206 if (VolumeInfo == NULL) {
1207 return EFI_INVALID_PARAMETER;
1208 }
1209
1210 //
1211 // VolumeHandle may not align at 8 byte,
1212 // but FvLength is UINT64 type, which requires FvHeader align at least 8 byte.
1213 // So, Copy FvHeader into the local FvHeader structure.
1214 //
1215 CopyMem (&FwVolHeader, FvHandle, sizeof (EFI_FIRMWARE_VOLUME_HEADER));
1216
1217 //
1218 // Check Fv Image Signature
1219 //
1220 if (FwVolHeader.Signature != EFI_FVH_SIGNATURE) {
1221 return EFI_INVALID_PARAMETER;
1222 }
1223
1224 ZeroMem (VolumeInfo, sizeof (EFI_FV_INFO));
1225 VolumeInfo->FvAttributes = FwVolHeader.Attributes;
1226 VolumeInfo->FvStart = (VOID *) FvHandle;
1227 VolumeInfo->FvSize = FwVolHeader.FvLength;
1228 CopyMem (&VolumeInfo->FvFormat, &FwVolHeader.FileSystemGuid, sizeof(EFI_GUID));
1229
1230 if (FwVolHeader.ExtHeaderOffset != 0) {
1231 FwVolExHeaderInfo = (EFI_FIRMWARE_VOLUME_EXT_HEADER*)(((UINT8 *)FvHandle) + FwVolHeader.ExtHeaderOffset);
1232 CopyMem (&VolumeInfo->FvName, &FwVolExHeaderInfo->FvName, sizeof(EFI_GUID));
1233 }
1234
1235 return EFI_SUCCESS;
1236 }
1237
1238 /**
1239 Find the next matching section in the firmware file.
1240
1241 This service enables PEI modules to discover sections
1242 of a given type within a valid file.
1243
1244 @param This Points to this instance of the
1245 EFI_PEI_FIRMWARE_VOLUME_PPI.
1246 @param SearchType A filter to find only sections of this
1247 type.
1248 @param FileHandle Handle of firmware file in which to
1249 search.
1250 @param SectionData Updated upon return to point to the
1251 section found.
1252
1253 @retval EFI_SUCCESS Section was found.
1254 @retval EFI_NOT_FOUND Section of the specified type was not
1255 found. SectionData contains NULL.
1256 **/
1257 EFI_STATUS
1258 EFIAPI
1259 PeiFfs2FvPpiFindSectionByType (
1260 IN CONST EFI_PEI_FIRMWARE_VOLUME_PPI *This,
1261 IN EFI_SECTION_TYPE SearchType,
1262 IN EFI_PEI_FILE_HANDLE FileHandle,
1263 OUT VOID **SectionData
1264 )
1265 {
1266 EFI_FFS_FILE_HEADER *FfsFileHeader;
1267 UINT32 FileSize;
1268 EFI_COMMON_SECTION_HEADER *Section;
1269
1270 FfsFileHeader = (EFI_FFS_FILE_HEADER *)(FileHandle);
1271
1272 //
1273 // Size is 24 bits wide so mask upper 8 bits.
1274 // Does not include FfsFileHeader header size
1275 // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.
1276 //
1277 Section = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);
1278 FileSize = *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF;
1279 FileSize -= sizeof (EFI_FFS_FILE_HEADER);
1280
1281 return ProcessSection (
1282 GetPeiServicesTablePointer (),
1283 SearchType,
1284 Section,
1285 FileSize,
1286 SectionData
1287 );
1288 }
1289
1290 /**
1291 Convert the handle of FV to pointer of corresponding PEI_CORE_FV_HANDLE.
1292
1293 @param FvHandle The handle of a FV.
1294
1295 @retval NULL if can not find.
1296 @return Pointer of corresponding PEI_CORE_FV_HANDLE.
1297 **/
1298 PEI_CORE_FV_HANDLE *
1299 FvHandleToCoreHandle (
1300 IN EFI_PEI_FV_HANDLE FvHandle
1301 )
1302 {
1303 UINTN Index;
1304 PEI_CORE_INSTANCE *PrivateData;
1305
1306 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer());
1307 for (Index = 0; Index < PrivateData->FvCount; Index ++) {
1308 if (FvHandle == PrivateData->Fv[Index].FvHandle) {
1309 return &PrivateData->Fv[Index];
1310 }
1311 }
1312
1313 return NULL;
1314 }
1315
1316 /**
1317 Get instance of PEI_CORE_FV_HANDLE for next volume according to given index.
1318
1319 This routine also will install FvInfo ppi for FV hob in PI ways.
1320
1321 @param Private Pointer of PEI_CORE_INSTANCE
1322 @param Instance The index of FV want to be searched.
1323
1324 @return Instance of PEI_CORE_FV_HANDLE.
1325 **/
1326 PEI_CORE_FV_HANDLE *
1327 FindNextCoreFvHandle (
1328 IN PEI_CORE_INSTANCE *Private,
1329 IN UINTN Instance
1330 )
1331 {
1332 UINTN Index;
1333 BOOLEAN Match;
1334 EFI_HOB_FIRMWARE_VOLUME *FvHob;
1335
1336 //
1337 // Handle Framework FvHob and Install FvInfo Ppi for it.
1338 //
1339 if (FeaturePcdGet (PcdFrameworkCompatibilitySupport)) {
1340 //
1341 // Loop to search the wanted FirmwareVolume which supports FFS
1342 //
1343 FvHob = (EFI_HOB_FIRMWARE_VOLUME *)GetFirstHob (EFI_HOB_TYPE_FV);
1344 while (FvHob != NULL) {
1345 for (Index = 0, Match = FALSE; Index < Private->FvCount; Index++) {
1346 if ((EFI_PEI_FV_HANDLE)(UINTN)FvHob->BaseAddress == Private->Fv[Index].FvHeader) {
1347 Match = TRUE;
1348 break;
1349 }
1350 }
1351 //
1352 // If Not Found, Install FvInfo Ppi for it.
1353 //
1354 if (!Match) {
1355 PeiServicesInstallFvInfoPpi (
1356 &(((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)FvHob->BaseAddress)->FileSystemGuid),
1357 (VOID *)(UINTN)FvHob->BaseAddress,
1358 (UINT32)FvHob->Length,
1359 NULL,
1360 NULL
1361 );
1362 }
1363 FvHob = (EFI_HOB_FIRMWARE_VOLUME *)GetNextHob (EFI_HOB_TYPE_FV, (VOID *)((UINTN)FvHob + FvHob->Header.HobLength));
1364 }
1365 }
1366
1367 if (Instance >= Private->FvCount) {
1368 return NULL;
1369 }
1370
1371 return &Private->Fv[Instance];
1372 }