]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Pei/FwVol/FwVol.c
Enable PeiCore to dispatch the encapsulated fv images with depex expression. This...
[mirror_edk2.git] / MdeModulePkg / Core / Pei / FwVol / FwVol.c
1 /*++
2
3 Copyright (c) 2006 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 FwVol.c
15
16 Abstract:
17
18 Pei Core Firmware File System service routines.
19
20 --*/
21
22 #include <PeiMain.h>
23
24 STATIC EFI_PEI_NOTIFY_DESCRIPTOR mNotifyOnFvInfoList = {
25 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
26 &gEfiPeiFirmwareVolumeInfoPpiGuid,
27 FirmwareVolmeInfoPpiNotifyCallback
28 };
29
30
31 #define GET_OCCUPIED_SIZE(ActualSize, Alignment) \
32 (ActualSize) + (((Alignment) - ((ActualSize) & ((Alignment) - 1))) & ((Alignment) - 1))
33
34 STATIC
35 EFI_FFS_FILE_STATE
36 GetFileState(
37 IN UINT8 ErasePolarity,
38 IN EFI_FFS_FILE_HEADER *FfsHeader
39 )
40 /*++
41
42 Routine Description:
43
44 Returns the highest bit set of the State field
45
46 Arguments:
47
48 ErasePolarity - Erase Polarity as defined by EFI_FVB2_ERASE_POLARITY
49 in the Attributes field.
50 FfsHeader - Pointer to FFS File Header.
51
52 Returns:
53 Returns the highest bit in the State field
54
55 --*/
56 {
57 EFI_FFS_FILE_STATE FileState;
58 EFI_FFS_FILE_STATE HighestBit;
59
60 FileState = FfsHeader->State;
61
62 if (ErasePolarity != 0) {
63 FileState = (EFI_FFS_FILE_STATE)~FileState;
64 }
65
66 HighestBit = 0x80;
67 while (HighestBit != 0 && (HighestBit & FileState) == 0) {
68 HighestBit >>= 1;
69 }
70
71 return HighestBit;
72 }
73
74 STATIC
75 UINT8
76 CalculateHeaderChecksum (
77 IN EFI_FFS_FILE_HEADER *FileHeader
78 )
79 /*++
80
81 Routine Description:
82
83 Calculates the checksum of the header of a file.
84
85 Arguments:
86
87 FileHeader - Pointer to FFS File Header.
88
89 Returns:
90 Checksum of the header.
91
92 The header is zero byte checksum.
93 - Zero means the header is good.
94 - Non-zero means the header is bad.
95
96
97 Bugbug: For PEI performance reason, we comments this code at this time.
98 --*/
99 {
100 UINT8 *ptr;
101 UINTN Index;
102 UINT8 Sum;
103
104 Sum = 0;
105 ptr = (UINT8 *)FileHeader;
106
107 for (Index = 0; Index < sizeof(EFI_FFS_FILE_HEADER) - 3; Index += 4) {
108 Sum = (UINT8)(Sum + ptr[Index]);
109 Sum = (UINT8)(Sum + ptr[Index+1]);
110 Sum = (UINT8)(Sum + ptr[Index+2]);
111 Sum = (UINT8)(Sum + ptr[Index+3]);
112 }
113
114 for (; Index < sizeof(EFI_FFS_FILE_HEADER); Index++) {
115 Sum = (UINT8)(Sum + ptr[Index]);
116 }
117
118 //
119 // State field (since this indicates the different state of file).
120 //
121 Sum = (UINT8)(Sum - FileHeader->State);
122 //
123 // Checksum field of the file is not part of the header checksum.
124 //
125 Sum = (UINT8)(Sum - FileHeader->IntegrityCheck.Checksum.File);
126
127 return Sum;
128 }
129
130 STATIC
131 BOOLEAN
132 EFIAPI
133 PeiFileHandleToVolume (
134 IN EFI_PEI_FILE_HANDLE FileHandle,
135 OUT EFI_PEI_FV_HANDLE *VolumeHandle
136 )
137 {
138 UINTN Index;
139 PEI_CORE_INSTANCE *PrivateData;
140 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
141
142 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());
143 for (Index = 0; Index < PrivateData->FvCount; Index++) {
144 FwVolHeader = PrivateData->Fv[Index].FvHeader;
145 if (((UINT64) (UINTN) FileHandle > (UINT64) (UINTN) FwVolHeader ) && \
146 ((UINT64) (UINTN) FileHandle <= ((UINT64) (UINTN) FwVolHeader + FwVolHeader->FvLength - 1))) {
147 *VolumeHandle = (EFI_PEI_FV_HANDLE)FwVolHeader;
148 return TRUE;
149 }
150 }
151 return FALSE;
152 }
153
154
155 EFI_STATUS
156 PeiFindFileEx (
157 IN CONST EFI_PEI_FV_HANDLE FvHandle,
158 IN CONST EFI_GUID *FileName, OPTIONAL
159 IN EFI_FV_FILETYPE SearchType,
160 IN OUT EFI_PEI_FILE_HANDLE *FileHandle,
161 IN OUT EFI_PEI_FV_HANDLE *AprioriFile OPTIONAL
162 )
163 /*++
164
165 Routine Description:
166 Given the input file pointer, search for the next matching file in the
167 FFS volume as defined by SearchType. The search starts from FileHeader inside
168 the Firmware Volume defined by FwVolHeader.
169
170 Arguments:
171 PeiServices - Pointer to the PEI Core Services Table.
172 SearchType - Filter to find only files of this type.
173 Type EFI_FV_FILETYPE_ALL causes no filtering to be done.
174 FwVolHeader - Pointer to the FV header of the volume to search.
175 This parameter must point to a valid FFS volume.
176 FileHeader - Pointer to the current file from which to begin searching.
177 This pointer will be updated upon return to reflect the file found.
178 Flag - Indicator for if this is for PEI Dispath search
179
180 Returns:
181 EFI_NOT_FOUND - No files matching the search criteria were found
182 EFI_SUCCESS
183
184 --*/
185 {
186 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
187 EFI_FFS_FILE_HEADER **FileHeader;
188 EFI_FFS_FILE_HEADER *FfsFileHeader;
189 EFI_FIRMWARE_VOLUME_EXT_HEADER *FwVolExHeaderInfo;
190 UINT32 FileLength;
191 UINT32 FileOccupiedSize;
192 UINT32 FileOffset;
193 UINT64 FvLength;
194 UINT8 ErasePolarity;
195 UINT8 FileState;
196
197 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)FvHandle;
198 FileHeader = (EFI_FFS_FILE_HEADER **)FileHandle;
199
200 FvLength = FwVolHeader->FvLength;
201 if (FwVolHeader->Attributes & EFI_FVB_ERASE_POLARITY) {
202 ErasePolarity = 1;
203 } else {
204 ErasePolarity = 0;
205 }
206
207 //
208 // If FileHeader is not specified (NULL) or FileName is not NULL,
209 // start with the first file in the firmware volume. Otherwise,
210 // start from the FileHeader.
211 //
212 if ((*FileHeader == NULL) || (FileName != NULL)) {
213 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FwVolHeader + FwVolHeader->HeaderLength);
214 if (FwVolHeader->ExtHeaderOffset != 0) {
215 FwVolExHeaderInfo = (EFI_FIRMWARE_VOLUME_EXT_HEADER *)(((UINT8 *)FwVolHeader) + FwVolHeader->ExtHeaderOffset);
216 FfsFileHeader = (EFI_FFS_FILE_HEADER *)(((UINT8 *)FwVolExHeaderInfo) + FwVolExHeaderInfo->ExtHeaderSize);
217 }
218 } else {
219 //
220 // Length is 24 bits wide so mask upper 8 bits
221 // FileLength is adjusted to FileOccupiedSize as it is 8 byte aligned.
222 //
223 FileLength = *(UINT32 *)(*FileHeader)->Size & 0x00FFFFFF;
224 FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
225 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)*FileHeader + FileOccupiedSize);
226 }
227
228 FileOffset = (UINT32) ((UINT8 *)FfsFileHeader - (UINT8 *)FwVolHeader);
229 ASSERT (FileOffset <= 0xFFFFFFFF);
230
231 while (FileOffset < (FvLength - sizeof (EFI_FFS_FILE_HEADER))) {
232 //
233 // Get FileState which is the highest bit of the State
234 //
235 FileState = GetFileState (ErasePolarity, FfsFileHeader);
236 switch (FileState) {
237
238 case EFI_FILE_HEADER_INVALID:
239 FileOffset += sizeof(EFI_FFS_FILE_HEADER);
240 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + sizeof(EFI_FFS_FILE_HEADER));
241 break;
242
243 case EFI_FILE_DATA_VALID:
244 case EFI_FILE_MARKED_FOR_UPDATE:
245 if (CalculateHeaderChecksum (FfsFileHeader) != 0) {
246 ASSERT (FALSE);
247 *FileHeader = NULL;
248 return EFI_NOT_FOUND;
249 }
250
251 FileLength = *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF;
252 FileOccupiedSize = GET_OCCUPIED_SIZE(FileLength, 8);
253
254 if (FileName != NULL) {
255 if (CompareGuid (&FfsFileHeader->Name, (EFI_GUID*)FileName)) {
256 *FileHeader = FfsFileHeader;
257 return EFI_SUCCESS;
258 }
259 } else if (SearchType == PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE) {
260 if ((FfsFileHeader->Type == EFI_FV_FILETYPE_PEIM) ||
261 (FfsFileHeader->Type == EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER) ||
262 (FfsFileHeader->Type == EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE)) {
263
264 *FileHeader = FfsFileHeader;
265 return EFI_SUCCESS;
266 } else if (AprioriFile != NULL) {
267 if (FfsFileHeader->Type == EFI_FV_FILETYPE_FREEFORM) {
268 if (CompareGuid (&FfsFileHeader->Name, &gPeiAprioriFileNameGuid)) {
269 *AprioriFile = FfsFileHeader;
270 }
271 }
272 }
273 } else if (((SearchType == FfsFileHeader->Type) || (SearchType == EFI_FV_FILETYPE_ALL)) &&
274 (FfsFileHeader->Type != EFI_FV_FILETYPE_FFS_PAD)) {
275 *FileHeader = FfsFileHeader;
276 return EFI_SUCCESS;
277 }
278
279 FileOffset += FileOccupiedSize;
280 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize);
281 break;
282
283 case EFI_FILE_DELETED:
284 FileLength = *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF;
285 FileOccupiedSize = GET_OCCUPIED_SIZE(FileLength, 8);
286 FileOffset += FileOccupiedSize;
287 FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize);
288 break;
289
290 default:
291 *FileHeader = NULL;
292 return EFI_NOT_FOUND;
293 }
294 }
295
296 *FileHeader = NULL;
297 return EFI_NOT_FOUND;
298 }
299
300 VOID
301 PeiInitializeFv (
302 IN PEI_CORE_INSTANCE *PrivateData,
303 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData
304 )
305 /*++
306
307 Routine Description:
308
309 Initialize PeiCore Fv List.
310
311 Arguments:
312 PrivateData - Pointer to PEI_CORE_INSTANCE.
313 SecCoreData - Pointer to EFI_SEC_PEI_HAND_OFF.
314
315 Returns:
316 NONE
317
318 --*/
319 {
320 EFI_STATUS Status;
321 //
322 // The BFV must be the first entry. The Core FV support is stateless
323 // The AllFV list has a single entry per FV in PEI.
324 // The Fv list only includes FV that PEIMs will be dispatched from and
325 // its File System Format is PI 1.0 definition.
326 //
327 PrivateData->FvCount = 1;
328 PrivateData->Fv[0].FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *)SecCoreData->BootFirmwareVolumeBase;
329
330 PrivateData->AllFvCount = 1;
331 PrivateData->AllFv[0] = (EFI_PEI_FV_HANDLE)PrivateData->Fv[0].FvHeader;
332
333
334 //
335 // Post a call-back for the FvInfoPPI services to expose
336 // additional Fvs to PeiCore.
337 //
338 Status = PeiServicesNotifyPpi (&mNotifyOnFvInfoList);
339 ASSERT_EFI_ERROR (Status);
340
341 }
342
343 EFI_STATUS
344 EFIAPI
345 FirmwareVolmeInfoPpiNotifyCallback (
346 IN EFI_PEI_SERVICES **PeiServices,
347 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
348 IN VOID *Ppi
349 )
350 /*++
351
352 Routine Description:
353
354 Process Firmware Volum Information once FvInfoPPI install.
355
356 Arguments:
357
358 PeiServices - General purpose services available to every PEIM.
359
360 Returns:
361
362 Status - EFI_SUCCESS if the interface could be successfully
363 installed
364
365 --*/
366 {
367 UINT8 FvCount;
368 EFI_PEI_FIRMWARE_VOLUME_INFO_PPI *Fv;
369 PEI_CORE_INSTANCE *PrivateData;
370 EFI_PEI_FILE_HANDLE FileHandle;
371 VOID *DepexData;
372 UINT32 AuthenticationStatus;
373 EFI_STATUS Status;
374
375 FileHandle = NULL;
376 DepexData = NULL;
377 Status = EFI_SUCCESS;
378 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);
379
380 if (PrivateData->FvCount >= FixedPcdGet32 (PcdPeiCoreMaxFvSupported)) {
381 ASSERT (FALSE);
382 }
383
384 Fv = (EFI_PEI_FIRMWARE_VOLUME_INFO_PPI *)Ppi;
385
386 if (CompareGuid (&Fv->FvFormat, &gEfiFirmwareFileSystem2Guid)) {
387 for (FvCount = 0; FvCount < PrivateData->FvCount; FvCount ++) {
388 if ((UINTN)PrivateData->Fv[FvCount].FvHeader == (UINTN)Fv->FvInfo) {
389 return EFI_SUCCESS;
390 }
391 }
392 PrivateData->Fv[PrivateData->FvCount++].FvHeader = (EFI_FIRMWARE_VOLUME_HEADER*)Fv->FvInfo;
393
394 //
395 // Only add FileSystem2 Fv to the All list
396 //
397 PrivateData->AllFv[PrivateData->AllFvCount++] = (EFI_PEI_FV_HANDLE)Fv->FvInfo;
398
399 DEBUG ((EFI_D_INFO, "The %dth FvImage start address is 0x%10p and size is 0x%08x\n", PrivateData->AllFvCount, (VOID *) Fv->FvInfo, Fv->FvInfoSize));
400 //
401 // Preprocess all FV type files in this new FileSystem2 Fv image
402 //
403 do {
404 Status = PeiFindFileEx (
405 (EFI_PEI_FV_HANDLE)Fv->FvInfo,
406 NULL,
407 EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE,
408 &FileHandle,
409 NULL
410 );
411 if (!EFI_ERROR (Status)) {
412 Status = PeiFfsFindSectionData (
413 (CONST EFI_PEI_SERVICES **) PeiServices,
414 EFI_SECTION_PEI_DEPEX,
415 FileHandle,
416 (VOID **)&DepexData
417 );
418 if (!EFI_ERROR (Status)) {
419 if (!PeimDispatchReadiness (PeiServices, DepexData)) {
420 //
421 // Dependency is not satisfied.
422 //
423 continue;
424 }
425 }
426 //
427 // Process FvFile to install FvInfo ppi and build FvHob
428 //
429 ProcessFvFile (PeiServices, FileHandle, &AuthenticationStatus);
430 }
431 } while (FileHandle != NULL);
432 }
433
434 return EFI_SUCCESS;
435 }
436
437 EFI_STATUS
438 PeiFfsProcessSection (
439 IN CONST EFI_PEI_SERVICES **PeiServices,
440 IN EFI_SECTION_TYPE SectionType,
441 IN EFI_COMMON_SECTION_HEADER *Section,
442 IN UINTN SectionSize,
443 OUT VOID **OutputBuffer
444 )
445 /*++
446
447 Routine Description:
448
449 Go through the file to search SectionType section,
450 when meeting an encapsuled section.
451
452 Arguments:
453 PeiServices - General purpose services available to every PEIM.
454 SearchType - Filter to find only section of this type.
455 Section - From where to search.
456 SectionSize - The file size to search.
457 OutputBuffer - Pointer to the section to search.
458
459 Returns:
460 EFI_STATUS
461
462 --*/
463 {
464 EFI_STATUS Status;
465 UINT32 SectionLength;
466 UINT32 ParsedLength;
467 EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI *GuidSectionPpi;
468 EFI_PEI_DECOMPRESS_PPI *DecompressPpi;
469 VOID *PpiOutput;
470 UINTN PpiOutputSize;
471 UINTN Index;
472 UINT32 Authentication;
473 PEI_CORE_INSTANCE *PrivateData;
474
475 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);
476 *OutputBuffer = NULL;
477 ParsedLength = 0;
478 Index = 0;
479 Status = EFI_NOT_FOUND;
480 PpiOutput = NULL;
481 PpiOutputSize = 0;
482 while (ParsedLength < SectionSize) {
483 if (Section->Type == SectionType) {
484 *OutputBuffer = (VOID *)(Section + 1);
485 return EFI_SUCCESS;
486 } else if ((Section->Type == EFI_SECTION_GUID_DEFINED) || (Section->Type == EFI_SECTION_COMPRESSION)) {
487 //
488 // Check the encapsulated section is extracted into the cache data.
489 //
490 for (Index = 0; Index < PrivateData->CacheSection.AllSectionCount; Index ++) {
491 if (Section == PrivateData->CacheSection.Section[Index]) {
492 PpiOutput = PrivateData->CacheSection.SectionData[Index];
493 PpiOutputSize = PrivateData->CacheSection.SectionSize[Index];
494 //
495 // Search section directly from the cache data.
496 //
497 return PeiFfsProcessSection (
498 PeiServices,
499 SectionType,
500 PpiOutput,
501 PpiOutputSize,
502 OutputBuffer
503 );
504 }
505 }
506
507 Status = EFI_NOT_FOUND;
508 if (Section->Type == EFI_SECTION_GUID_DEFINED) {
509 Status = PeiServicesLocatePpi (
510 &((EFI_GUID_DEFINED_SECTION *)Section)->SectionDefinitionGuid,
511 0,
512 NULL,
513 (VOID **) &GuidSectionPpi
514 );
515 if (!EFI_ERROR (Status)) {
516 Status = GuidSectionPpi->ExtractSection (
517 GuidSectionPpi,
518 Section,
519 &PpiOutput,
520 &PpiOutputSize,
521 &Authentication
522 );
523 }
524 } else if (Section->Type == EFI_SECTION_COMPRESSION) {
525 Status = PeiServicesLocatePpi (&gEfiPeiDecompressPpiGuid, 0, NULL, (VOID **) &DecompressPpi);
526 if (!EFI_ERROR (Status)) {
527 Status = DecompressPpi->Decompress (
528 DecompressPpi,
529 (CONST EFI_COMPRESSION_SECTION*) Section,
530 &PpiOutput,
531 &PpiOutputSize
532 );
533 }
534 }
535
536 if (!EFI_ERROR (Status)) {
537 //
538 // Update cache section data.
539 //
540 if (PrivateData->CacheSection.AllSectionCount < CACHE_SETION_MAX_NUMBER) {
541 PrivateData->CacheSection.AllSectionCount ++;
542 }
543 PrivateData->CacheSection.Section [PrivateData->CacheSection.SectionIndex] = Section;
544 PrivateData->CacheSection.SectionData [PrivateData->CacheSection.SectionIndex] = PpiOutput;
545 PrivateData->CacheSection.SectionSize [PrivateData->CacheSection.SectionIndex] = PpiOutputSize;
546 PrivateData->CacheSection.SectionIndex = (PrivateData->CacheSection.SectionIndex + 1)%CACHE_SETION_MAX_NUMBER;
547
548 return PeiFfsProcessSection (
549 PeiServices,
550 SectionType,
551 PpiOutput,
552 PpiOutputSize,
553 OutputBuffer
554 );
555 }
556 }
557
558 //
559 // Size is 24 bits wide so mask upper 8 bits.
560 // SectionLength is adjusted it is 4 byte aligned.
561 // Go to the next section
562 //
563 SectionLength = *(UINT32 *)Section->Size & 0x00FFFFFF;
564 SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);
565 ASSERT (SectionLength != 0);
566 ParsedLength += SectionLength;
567 Section = (EFI_COMMON_SECTION_HEADER *)((UINT8 *)Section + SectionLength);
568 }
569
570 return EFI_NOT_FOUND;
571 }
572
573
574 EFI_STATUS
575 EFIAPI
576 PeiFfsFindSectionData (
577 IN CONST EFI_PEI_SERVICES **PeiServices,
578 IN EFI_SECTION_TYPE SectionType,
579 IN EFI_PEI_FILE_HANDLE FileHandle,
580 IN OUT VOID **SectionData
581 )
582 /*++
583
584 Routine Description:
585 Given the input file pointer, search for the next matching section in the
586 FFS volume.
587
588 Arguments:
589 PeiServices - Pointer to the PEI Core Services Table.
590 SearchType - Filter to find only sections of this type.
591 FfsFileHeader - Pointer to the current file to search.
592 SectionData - Pointer to the Section matching SectionType in FfsFileHeader.
593 - NULL if section not found
594
595 Returns:
596 EFI_NOT_FOUND - No files matching the search criteria were found
597 EFI_SUCCESS
598
599 --*/
600 {
601 EFI_FFS_FILE_HEADER *FfsFileHeader;
602 UINT32 FileSize;
603 EFI_COMMON_SECTION_HEADER *Section;
604
605 FfsFileHeader = (EFI_FFS_FILE_HEADER *)(FileHandle);
606
607 //
608 // Size is 24 bits wide so mask upper 8 bits.
609 // Does not include FfsFileHeader header size
610 // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.
611 //
612 Section = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);
613 FileSize = *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF;
614 FileSize -= sizeof (EFI_FFS_FILE_HEADER);
615
616 return PeiFfsProcessSection (
617 PeiServices,
618 SectionType,
619 Section,
620 FileSize,
621 SectionData
622 );
623 }
624
625
626 EFI_STATUS
627 EFIAPI
628 PeiFfsFindNextFile (
629 IN CONST EFI_PEI_SERVICES **PeiServices,
630 IN UINT8 SearchType,
631 IN EFI_PEI_FV_HANDLE VolumeHandle,
632 IN OUT EFI_PEI_FILE_HANDLE *FileHandle
633 )
634 /*++
635
636 Routine Description:
637 Given the input file pointer, search for the next matching file in the
638 FFS volume as defined by SearchType. The search starts from FileHeader inside
639 the Firmware Volume defined by FwVolHeader.
640
641 Arguments:
642 PeiServices - Pointer to the PEI Core Services Table.
643
644 SearchType - Filter to find only files of this type.
645 Type EFI_FV_FILETYPE_ALL causes no filtering to be done.
646
647 FwVolHeader - Pointer to the FV header of the volume to search.
648 This parameter must point to a valid FFS volume.
649
650 FileHeader - Pointer to the current file from which to begin searching.
651 This pointer will be updated upon return to reflect the file found.
652
653 Returns:
654 EFI_NOT_FOUND - No files matching the search criteria were found
655 EFI_SUCCESS
656
657 --*/
658 {
659 return PeiFindFileEx (VolumeHandle, NULL, SearchType, FileHandle, NULL);
660 }
661
662
663 EFI_STATUS
664 EFIAPI
665 PeiFvFindNextVolume (
666 IN CONST EFI_PEI_SERVICES **PeiServices,
667 IN UINTN Instance,
668 IN OUT EFI_PEI_FV_HANDLE *VolumeHandle
669 )
670 /*++
671
672 Routine Description:
673
674 Return the firmware volumes.
675
676 BugBug -- Move this to the location of this code to where the
677 other FV and FFS support code lives.
678 Also, update to use FindFV for instances #'s >= 1.
679
680 Arguments:
681
682 PeiServices - The PEI core services table.
683 Instance - Instance of FV to find
684 FwVolHeader - Pointer to contain the data to return
685
686 Returns:
687 Pointer to the Firmware Volume instance requested
688
689 EFI_INVALID_PARAMETER - FwVolHeader is NULL
690
691 EFI_SUCCESS - Firmware volume instance successfully found.
692
693 --*/
694 {
695 PEI_CORE_INSTANCE *Private;
696
697 Private = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);
698 if (VolumeHandle == NULL) {
699 return EFI_INVALID_PARAMETER;
700 }
701
702 if (Instance >= Private->AllFvCount) {
703 VolumeHandle = NULL;
704 return EFI_NOT_FOUND;
705 }
706
707 *VolumeHandle = Private->AllFv[Instance];
708 return EFI_SUCCESS;
709 }
710
711
712 EFI_STATUS
713 EFIAPI
714 PeiFfsFindFileByName (
715 IN CONST EFI_GUID *FileName,
716 IN EFI_PEI_FV_HANDLE VolumeHandle,
717 OUT EFI_PEI_FILE_HANDLE *FileHandle
718 )
719 /*++
720
721 Routine Description:
722
723 Given the input VolumeHandle, search for the next matching name file.
724
725 Arguments:
726
727 FileName - File name to search.
728 VolumeHandle - The current FV to search.
729 FileHandle - Pointer to the file matching name in VolumeHandle.
730 - NULL if file not found
731 Returns:
732 EFI_STATUS
733
734 --*/
735 {
736 EFI_STATUS Status;
737 if ((VolumeHandle == NULL) || (FileName == NULL) || (FileHandle == NULL)) {
738 return EFI_INVALID_PARAMETER;
739 }
740 Status = PeiFindFileEx (VolumeHandle, FileName, 0, FileHandle, NULL);
741 if (Status == EFI_NOT_FOUND) {
742 *FileHandle = NULL;
743 }
744 return Status;
745 }
746
747 EFI_STATUS
748 EFIAPI
749 PeiFfsGetFileInfo (
750 IN EFI_PEI_FILE_HANDLE FileHandle,
751 OUT EFI_FV_FILE_INFO *FileInfo
752 )
753 /*++
754
755 Routine Description:
756
757 Collect information of given file.
758
759 Arguments:
760 FileHandle - The handle to file.
761 FileInfo - Pointer to the file information.
762
763 Returns:
764 EFI_STATUS
765
766 --*/
767 {
768 UINT8 FileState;
769 UINT8 ErasePolarity;
770 EFI_FFS_FILE_HEADER *FileHeader;
771 EFI_PEI_FV_HANDLE VolumeHandle;
772
773 if ((FileHandle == NULL) || (FileInfo == NULL)) {
774 return EFI_INVALID_PARAMETER;
775 }
776
777 //
778 // Retrieve the FirmwareVolume which the file resides in.
779 //
780 if (!PeiFileHandleToVolume(FileHandle, &VolumeHandle)) {
781 return EFI_INVALID_PARAMETER;
782 }
783
784 if (((EFI_FIRMWARE_VOLUME_HEADER*)VolumeHandle)->Attributes & EFI_FVB_ERASE_POLARITY) {
785 ErasePolarity = 1;
786 } else {
787 ErasePolarity = 0;
788 }
789
790 //
791 // Get FileState which is the highest bit of the State
792 //
793 FileState = GetFileState (ErasePolarity, (EFI_FFS_FILE_HEADER*)FileHandle);
794
795 switch (FileState) {
796 case EFI_FILE_DATA_VALID:
797 case EFI_FILE_MARKED_FOR_UPDATE:
798 break;
799 default:
800 return EFI_INVALID_PARAMETER;
801 }
802
803 FileHeader = (EFI_FFS_FILE_HEADER *)FileHandle;
804 CopyMem (&FileInfo->FileName, &FileHeader->Name, sizeof(EFI_GUID));
805 FileInfo->FileType = FileHeader->Type;
806 FileInfo->FileAttributes = FileHeader->Attributes;
807 FileInfo->BufferSize = ((*(UINT32 *)FileHeader->Size) & 0x00FFFFFF) - sizeof (EFI_FFS_FILE_HEADER);
808 FileInfo->Buffer = (FileHeader + 1);
809 return EFI_SUCCESS;
810 }
811
812
813 EFI_STATUS
814 EFIAPI
815 PeiFfsGetVolumeInfo (
816 IN EFI_PEI_FV_HANDLE VolumeHandle,
817 OUT EFI_FV_INFO *VolumeInfo
818 )
819 /*++
820
821 Routine Description:
822
823 Collect information of given Fv Volume.
824
825 Arguments:
826 VolumeHandle - The handle to Fv Volume.
827 VolumeInfo - The pointer to volume information.
828
829 Returns:
830 EFI_STATUS
831
832 --*/
833 {
834 EFI_FIRMWARE_VOLUME_HEADER FwVolHeader;
835 EFI_FIRMWARE_VOLUME_EXT_HEADER *FwVolExHeaderInfo;
836
837 if (VolumeInfo == NULL) {
838 return EFI_INVALID_PARAMETER;
839 }
840
841 //
842 // VolumeHandle may not align at 8 byte,
843 // but FvLength is UINT64 type, which requires FvHeader align at least 8 byte.
844 // So, Copy FvHeader into the local FvHeader structure.
845 //
846 CopyMem (&FwVolHeader, VolumeHandle, sizeof (EFI_FIRMWARE_VOLUME_HEADER));
847 //
848 // Check Fv Image Signature
849 //
850 if (FwVolHeader.Signature != EFI_FVH_SIGNATURE) {
851 return EFI_INVALID_PARAMETER;
852 }
853 VolumeInfo->FvAttributes = FwVolHeader.Attributes;
854 VolumeInfo->FvStart = (VOID *) VolumeHandle;
855 VolumeInfo->FvSize = FwVolHeader.FvLength;
856 CopyMem (&VolumeInfo->FvFormat, &FwVolHeader.FileSystemGuid, sizeof(EFI_GUID));
857
858 if (FwVolHeader.ExtHeaderOffset != 0) {
859 FwVolExHeaderInfo = (EFI_FIRMWARE_VOLUME_EXT_HEADER*)(((UINT8 *)VolumeHandle) + FwVolHeader.ExtHeaderOffset);
860 CopyMem (&VolumeInfo->FvName, &FwVolExHeaderInfo->FvName, sizeof(EFI_GUID));
861 }
862 return EFI_SUCCESS;
863 }
864