3 Copyright (c) 2004 - 2013, Intel Corporation. All rights reserved.<BR>
4 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
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.
18 These functions assist in parsing and manipulating a Firmware Volume.
26 #include "CommonLib.h"
27 #include "EfiUtilityMsgs.h"
30 // Module global variables
32 EFI_FIRMWARE_VOLUME_HEADER
*mFvHeader
= NULL
;
36 // External function implementations
47 This initializes the FV lib with a pointer to the FV and length. It does not
48 verify the FV in any way.
52 Fv Buffer containing the FV.
53 FvLength Length of the FV
57 EFI_SUCCESS Function Completed successfully.
58 EFI_INVALID_PARAMETER A required parameter was NULL.
63 // Verify input arguments
66 return EFI_INVALID_PARAMETER
;
69 mFvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) Fv
;
77 OUT EFI_FIRMWARE_VOLUME_HEADER
**FvHeader
,
84 This function returns a pointer to the current FV and the size.
88 FvHeader Pointer to the FV buffer.
89 FvLength Length of the FV
93 EFI_SUCCESS Function Completed successfully.
94 EFI_INVALID_PARAMETER A required parameter was NULL.
95 EFI_ABORTED The library needs to be initialized.
100 // Verify library has been initialized.
102 if (mFvHeader
== NULL
|| mFvLength
== 0) {
106 // Verify input arguments
108 if (FvHeader
== NULL
) {
109 return EFI_INVALID_PARAMETER
;
112 *FvHeader
= mFvHeader
;
113 *FvLength
= mFvLength
;
119 IN EFI_FFS_FILE_HEADER
*CurrentFile
,
120 OUT EFI_FFS_FILE_HEADER
**NextFile
126 This function returns the next file. If the current file is NULL, it returns
127 the first file in the FV. If the function returns EFI_SUCCESS and the file
128 pointer is NULL, then there are no more files in the FV.
132 CurrentFile Pointer to the current file, must be within the current FV.
133 NextFile Pointer to the next file in the FV.
137 EFI_SUCCESS Function completed successfully.
138 EFI_INVALID_PARAMETER A required parameter was NULL or is out of range.
139 EFI_ABORTED The library needs to be initialized.
146 // Verify library has been initialized.
148 if (mFvHeader
== NULL
|| mFvLength
== 0) {
152 // Verify input arguments
154 if (NextFile
== NULL
) {
155 return EFI_INVALID_PARAMETER
;
160 Status
= VerifyFv (mFvHeader
);
161 if (EFI_ERROR (Status
)) {
167 if (CurrentFile
== NULL
) {
168 CurrentFile
= (EFI_FFS_FILE_HEADER
*) ((UINTN
) mFvHeader
+ mFvHeader
->HeaderLength
);
171 // Verify file is valid
173 Status
= VerifyFfsFile (CurrentFile
);
174 if (EFI_ERROR (Status
)) {
176 // no files in this FV
182 // Verify file is in this FV.
184 if ((UINTN
) CurrentFile
+ GetFfsFileLength(CurrentFile
) > (UINTN
) mFvHeader
+ mFvLength
) {
189 *NextFile
= CurrentFile
;
194 // Verify current file is in range
196 if (((UINTN
) CurrentFile
< (UINTN
) mFvHeader
+ mFvHeader
->HeaderLength
) ||
197 ((UINTN
) CurrentFile
+ GetFfsFileLength(CurrentFile
) > (UINTN
) mFvHeader
+ mFvLength
)
199 return EFI_INVALID_PARAMETER
;
202 // Get next file, compensate for 8 byte alignment if necessary.
204 *NextFile
= (EFI_FFS_FILE_HEADER
*) ((((UINTN
) CurrentFile
- (UINTN
) mFvHeader
+ GetFfsFileLength(CurrentFile
) + 0x07) & (-1 << 3)) + (UINT8
*) mFvHeader
);
207 // Verify file is in this FV.
209 if (((UINTN
) *NextFile
+ GetFfsHeaderLength(*NextFile
) >= (UINTN
) mFvHeader
+ mFvLength
) ||
210 ((UINTN
) *NextFile
+ GetFfsFileLength (*NextFile
) > (UINTN
) mFvHeader
+ mFvLength
)
216 // Verify file is valid
218 Status
= VerifyFfsFile (*NextFile
);
219 if (EFI_ERROR (Status
)) {
221 // no more files in this FV
232 IN EFI_GUID
*FileName
,
233 OUT EFI_FFS_FILE_HEADER
**File
239 Find a file by name. The function will return NULL if the file is not found.
243 FileName The GUID file name of the file to search for.
244 File Return pointer. In the case of an error, contents are undefined.
248 EFI_SUCCESS The function completed successfully.
249 EFI_ABORTED An error was encountered.
250 EFI_INVALID_PARAMETER One of the parameters was NULL.
254 EFI_FFS_FILE_HEADER
*CurrentFile
;
256 CHAR8 FileGuidString
[80];
259 // Verify library has been initialized.
261 if (mFvHeader
== NULL
|| mFvLength
== 0) {
265 // Verify input parameters
267 if (FileName
== NULL
|| File
== NULL
) {
268 return EFI_INVALID_PARAMETER
;
271 // File Guid String Name
273 PrintGuidToBuffer (FileName
, (UINT8
*)FileGuidString
, sizeof (FileGuidString
), TRUE
);
277 Status
= VerifyFv (mFvHeader
);
278 if (EFI_ERROR (Status
)) {
282 // Get the first file
284 Status
= GetNextFile (NULL
, &CurrentFile
);
285 if (EFI_ERROR (Status
)) {
286 Error (NULL
, 0, 0003, "error parsing FV image", "FFS file with Guid %s can't be found", FileGuidString
);
290 // Loop as long as we have a valid file
292 while (CurrentFile
) {
293 if (!CompareGuid (&CurrentFile
->Name
, FileName
)) {
298 Status
= GetNextFile (CurrentFile
, &CurrentFile
);
299 if (EFI_ERROR (Status
)) {
300 Error (NULL
, 0, 0003, "error parsing FV image", "FFS file with Guid %s can't be found", FileGuidString
);
305 // File not found in this FV.
313 IN EFI_FV_FILETYPE FileType
,
315 OUT EFI_FFS_FILE_HEADER
**File
321 Find a file by type and instance. An instance of 1 is the first instance.
322 The function will return NULL if a matching file cannot be found.
323 File type EFI_FV_FILETYPE_ALL means any file type is valid.
327 FileType Type of file to search for.
328 Instance Instace of the file type to return.
329 File Return pointer. In the case of an error, contents are undefined.
333 EFI_SUCCESS The function completed successfully.
334 EFI_ABORTED An error was encountered.
335 EFI_INVALID_PARAMETER One of the parameters was NULL.
339 EFI_FFS_FILE_HEADER
*CurrentFile
;
344 // Verify library has been initialized.
346 if (mFvHeader
== NULL
|| mFvLength
== 0) {
350 // Verify input parameters
353 return EFI_INVALID_PARAMETER
;
358 Status
= VerifyFv (mFvHeader
);
359 if (EFI_ERROR (Status
)) {
363 // Initialize the number of matching files found.
368 // Get the first file
370 Status
= GetNextFile (NULL
, &CurrentFile
);
371 if (EFI_ERROR (Status
)) {
372 Error (NULL
, 0, 0003, "error parsing FV image", "FFS file with FileType 0x%x can't be found", FileType
);
376 // Loop as long as we have a valid file
378 while (CurrentFile
) {
379 if (FileType
== EFI_FV_FILETYPE_ALL
|| CurrentFile
->Type
== FileType
) {
383 if (FileCount
== Instance
) {
388 Status
= GetNextFile (CurrentFile
, &CurrentFile
);
389 if (EFI_ERROR (Status
)) {
390 Error (NULL
, 0, 0003, "error parsing FV image", "FFS file with FileType 0x%x can't be found", FileType
);
400 SearchSectionByType (
401 IN EFI_FILE_SECTION_POINTER FirstSection
,
403 IN EFI_SECTION_TYPE SectionType
,
404 IN OUT UINTN
*StartIndex
,
406 OUT EFI_FILE_SECTION_POINTER
*Section
412 Helper function to search a sequence of sections from the section pointed
413 by FirstSection to SearchEnd for the Instance-th section of type SectionType.
414 The current counter is saved in StartIndex and when the section is found, it's
415 saved in Section. GUID-defined sections, if special processing is not required,
416 are searched recursively in a depth-first manner.
420 FirstSection The first section to start searching from.
421 SearchEnd The end address to stop search.
422 SectionType The type of section to search.
423 StartIndex The current counter is saved.
424 Instance The requested n-th section number.
425 Section The found section returned.
429 EFI_SUCCESS The function completed successfully.
430 EFI_NOT_FOUND The section is not found.
433 EFI_FILE_SECTION_POINTER CurrentSection
;
434 EFI_FILE_SECTION_POINTER InnerSection
;
438 UINT16 GuidDataOffset
;
442 CurrentSection
= FirstSection
;
444 while ((UINTN
) CurrentSection
.CommonHeader
< (UINTN
) SearchEnd
) {
445 if (CurrentSection
.CommonHeader
->Type
== SectionType
) {
449 if (*StartIndex
== Instance
) {
450 *Section
= CurrentSection
;
454 // If the requesting section is not GUID-defined and
455 // we find a GUID-defined section that doesn't need
456 // special processing, go ahead to search the requesting
457 // section inside the GUID-defined section.
459 if (CurrentSection
.CommonHeader
->Type
== EFI_SECTION_GUID_DEFINED
) {
460 if (GetLength(CurrentSection
.CommonHeader
->Size
) == 0xffffff) {
461 GuidSecAttr
= CurrentSection
.GuidDefinedSection2
->Attributes
;
462 GuidDataOffset
= CurrentSection
.GuidDefinedSection2
->DataOffset
;
464 GuidSecAttr
= CurrentSection
.GuidDefinedSection
->Attributes
;
465 GuidDataOffset
= CurrentSection
.GuidDefinedSection
->DataOffset
;
468 if (SectionType
!= EFI_SECTION_GUID_DEFINED
&&
469 CurrentSection
.CommonHeader
->Type
== EFI_SECTION_GUID_DEFINED
&&
470 !(GuidSecAttr
& EFI_GUIDED_SECTION_PROCESSING_REQUIRED
)) {
471 InnerSection
.CommonHeader
= (EFI_COMMON_SECTION_HEADER
*)
472 ((UINTN
) CurrentSection
.CommonHeader
+ GuidDataOffset
);
473 SectionSize
= GetSectionFileLength(CurrentSection
.CommonHeader
);
474 Status
= SearchSectionByType (
476 (UINT8
*) ((UINTN
) CurrentSection
.CommonHeader
+ SectionSize
),
482 if (!EFI_ERROR (Status
)) {
487 // Find next section (including compensating for alignment issues.
489 CurrentSection
.CommonHeader
= (EFI_COMMON_SECTION_HEADER
*) ((((UINTN
) CurrentSection
.CommonHeader
) + GetSectionFileLength(CurrentSection
.CommonHeader
) + 0x03) & (-1 << 2));
492 return EFI_NOT_FOUND
;
497 IN EFI_FFS_FILE_HEADER
*File
,
498 IN EFI_SECTION_TYPE SectionType
,
500 OUT EFI_FILE_SECTION_POINTER
*Section
506 Find a section in a file by type and instance. An instance of 1 is the first
507 instance. The function will return NULL if a matching section cannot be found.
508 GUID-defined sections, if special processing is not needed, are handled in a
513 File The file to search.
514 SectionType Type of file to search for.
515 Instance Instace of the section to return.
516 Section Return pointer. In the case of an error, contents are undefined.
520 EFI_SUCCESS The function completed successfully.
521 EFI_ABORTED An error was encountered.
522 EFI_INVALID_PARAMETER One of the parameters was NULL.
523 EFI_NOT_FOUND No found.
526 EFI_FILE_SECTION_POINTER CurrentSection
;
531 // Verify input parameters
533 if (File
== NULL
|| Instance
== 0) {
534 return EFI_INVALID_PARAMETER
;
539 Status
= VerifyFfsFile (File
);
540 if (EFI_ERROR (Status
)) {
541 Error (NULL
, 0, 0006, "invalid FFS file", NULL
);
545 // Initialize the number of matching sections found.
550 // Get the first section
552 CurrentSection
.CommonHeader
= (EFI_COMMON_SECTION_HEADER
*) ((UINTN
) File
+ GetFfsHeaderLength(File
));
555 // Depth-first manner to find section file.
557 Status
= SearchSectionByType (
559 (UINT8
*) ((UINTN
) File
+ GetFfsFileLength (File
)),
566 if (!EFI_ERROR (Status
)) {
572 (*Section
).Code16Section
= NULL
;
573 return EFI_NOT_FOUND
;
577 // will not parse compressed sections
581 IN EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
587 Verify the current pointer points to a valid FV header.
591 FvHeader Pointer to an alleged FV file.
595 EFI_SUCCESS The FV header is valid.
596 EFI_VOLUME_CORRUPTED The FV header is not valid.
597 EFI_INVALID_PARAMETER A required parameter was NULL.
598 EFI_ABORTED Operation aborted.
605 // Verify input parameters
607 if (FvHeader
== NULL
) {
608 return EFI_INVALID_PARAMETER
;
611 if (FvHeader
->Signature
!= EFI_FVH_SIGNATURE
) {
612 Error (NULL
, 0, 0006, "invalid FV header signature", NULL
);
613 return EFI_VOLUME_CORRUPTED
;
616 // Verify header checksum
618 Checksum
= CalculateSum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
/ sizeof (UINT16
));
621 Error (NULL
, 0, 0006, "invalid FV header checksum", NULL
);
630 IN EFI_FFS_FILE_HEADER
*FfsHeader
636 Verify the current pointer points to a FFS file header.
640 FfsHeader Pointer to an alleged FFS file.
644 EFI_SUCCESS The Ffs header is valid.
645 EFI_NOT_FOUND This "file" is the beginning of free space.
646 EFI_VOLUME_CORRUPTED The Ffs header is not valid.
647 EFI_ABORTED The erase polarity is not known.
651 BOOLEAN ErasePolarity
;
653 EFI_FFS_FILE_HEADER2 BlankHeader
;
658 UINT8 FileGuidString
[80];
659 UINT32 FfsHeaderSize
;
662 // Verify library has been initialized.
664 if (mFvHeader
== NULL
|| mFvLength
== 0) {
670 Status
= VerifyFv (mFvHeader
);
671 if (EFI_ERROR (Status
)) {
675 // Get the erase polarity.
677 Status
= GetErasePolarity (&ErasePolarity
);
678 if (EFI_ERROR (Status
)) {
682 FfsHeaderSize
= GetFfsHeaderLength(FfsHeader
);
684 // Check if we have free space
687 memset (&BlankHeader
, -1, FfsHeaderSize
);
689 memset (&BlankHeader
, 0, FfsHeaderSize
);
692 if (memcmp (&BlankHeader
, FfsHeader
, FfsHeaderSize
) == 0) {
693 return EFI_NOT_FOUND
;
696 // Convert the GUID to a string so we can at least report which file
697 // if we find an error.
699 PrintGuidToBuffer (&FfsHeader
->Name
, FileGuidString
, sizeof (FileGuidString
), TRUE
);
701 // Verify file header checksum
703 SavedState
= FfsHeader
->State
;
704 FfsHeader
->State
= 0;
705 SavedChecksum
= FfsHeader
->IntegrityCheck
.Checksum
.File
;
706 FfsHeader
->IntegrityCheck
.Checksum
.File
= 0;
707 Checksum
= CalculateSum8 ((UINT8
*) FfsHeader
, FfsHeaderSize
);
708 FfsHeader
->State
= SavedState
;
709 FfsHeader
->IntegrityCheck
.Checksum
.File
= SavedChecksum
;
711 Error (NULL
, 0, 0006, "invalid FFS file header checksum", "Ffs file with Guid %s", FileGuidString
);
715 // Verify file checksum
717 if (FfsHeader
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
719 // Verify file data checksum
721 FileLength
= GetFfsFileLength (FfsHeader
);
722 Checksum
= CalculateSum8 ((UINT8
*) ((UINT8
*)FfsHeader
+ FfsHeaderSize
), FileLength
- FfsHeaderSize
);
723 Checksum
= Checksum
+ FfsHeader
->IntegrityCheck
.Checksum
.File
;
725 Error (NULL
, 0, 0006, "invalid FFS file checksum", "Ffs file with Guid %s", FileGuidString
);
730 // File does not have a checksum
731 // Verify contents are 0xAA as spec'd
733 if (FfsHeader
->IntegrityCheck
.Checksum
.File
!= FFS_FIXED_CHECKSUM
) {
734 Error (NULL
, 0, 0006, "invalid fixed FFS file header checksum", "Ffs file with Guid %s", FileGuidString
);
744 IN EFI_FFS_FILE_HEADER
*FfsHeader
747 if (FfsHeader
== NULL
) {
750 if (FfsHeader
->Attributes
& FFS_ATTRIB_LARGE_FILE
) {
751 return sizeof(EFI_FFS_FILE_HEADER2
);
753 return sizeof(EFI_FFS_FILE_HEADER
);
757 GetSectionHeaderLength(
758 IN EFI_COMMON_SECTION_HEADER
*SectionHeader
761 if (SectionHeader
== NULL
) {
764 if (GetLength(SectionHeader
->Size
) == 0xffffff) {
765 return sizeof(EFI_COMMON_SECTION_HEADER2
);
767 return sizeof(EFI_COMMON_SECTION_HEADER
);
772 EFI_FFS_FILE_HEADER
*FfsHeader
778 Get FFS file length including FFS header.
782 FfsHeader Pointer to EFI_FFS_FILE_HEADER.
786 UINT32 Length of FFS file header.
790 if (FfsHeader
== NULL
) {
793 if (FfsHeader
->Attributes
& FFS_ATTRIB_LARGE_FILE
) {
794 return ((EFI_FFS_FILE_HEADER2
*)FfsHeader
)->ExtendedSize
;
796 return GetLength(FfsHeader
->Size
);
801 GetSectionFileLength (
802 EFI_COMMON_SECTION_HEADER
*SectionHeader
806 if (SectionHeader
== NULL
) {
809 Length
= GetLength(SectionHeader
->Size
);
810 if (Length
== 0xffffff) {
811 Length
= ((EFI_COMMON_SECTION_HEADER2
*)SectionHeader
)->ExtendedSize
;
818 UINT8
*ThreeByteLength
824 Converts a three byte length value into a UINT32.
828 ThreeByteLength Pointer to the first of the 3 byte length.
832 UINT32 Size of the section
838 if (ThreeByteLength
== NULL
) {
842 Length
= *((UINT32
*) ThreeByteLength
);
843 Length
= Length
& 0x00FFFFFF;
850 OUT BOOLEAN
*ErasePolarity
856 This function returns with the FV erase polarity. If the erase polarity
857 for a bit is 1, the function return TRUE.
861 ErasePolarity A pointer to the erase polarity.
865 EFI_SUCCESS The function completed successfully.
866 EFI_INVALID_PARAMETER One of the input parameters was invalid.
867 EFI_ABORTED Operation aborted.
874 // Verify library has been initialized.
876 if (mFvHeader
== NULL
|| mFvLength
== 0) {
882 Status
= VerifyFv (mFvHeader
);
883 if (EFI_ERROR (Status
)) {
887 // Verify input parameters.
889 if (ErasePolarity
== NULL
) {
890 return EFI_INVALID_PARAMETER
;
893 if (mFvHeader
->Attributes
& EFI_FVB2_ERASE_POLARITY
) {
894 *ErasePolarity
= TRUE
;
896 *ErasePolarity
= FALSE
;
904 IN BOOLEAN ErasePolarity
,
905 IN EFI_FFS_FILE_HEADER
*FfsHeader
911 This function returns a the highest state bit in the FFS that is set.
912 It in no way validate the FFS file.
916 ErasePolarity The erase polarity for the file state bits.
917 FfsHeader Pointer to a FFS file.
921 UINT8 The hightest set state of the file.
928 FileState
= FfsHeader
->State
;
931 FileState
= (UINT8
)~FileState
;
935 while (HighestBit
!= 0 && (HighestBit
& FileState
) == 0) {