2 A simple FV stack so the SEC can extract the SEC Core from an
5 Copyright (c) 2006, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
13 #define GET_OCCUPIED_SIZE(ActualSize, Alignment) \
14 (ActualSize) + (((Alignment) - ((ActualSize) & ((Alignment) - 1))) & ((Alignment) - 1))
18 IN UINT8 ErasePolarity
,
19 IN EFI_FFS_FILE_HEADER
*FfsHeader
24 Returns the highest bit set of the State field
27 ErasePolarity - Erase Polarity as defined by EFI_FVB2_ERASE_POLARITY
28 in the Attributes field.
29 FfsHeader - Pointer to FFS File Header.
32 Returns the highest bit in the State field
36 EFI_FFS_FILE_STATE FileState
;
37 EFI_FFS_FILE_STATE HighestBit
;
39 FileState
= FfsHeader
->State
;
41 if (ErasePolarity
!= 0) {
42 FileState
= (EFI_FFS_FILE_STATE
)~FileState
;
46 while (HighestBit
!= 0 && (HighestBit
& FileState
) == 0) {
54 CalculateHeaderChecksum (
55 IN EFI_FFS_FILE_HEADER
*FileHeader
60 Calculates the checksum of the header of a file.
63 FileHeader - Pointer to FFS File Header.
66 Checksum of the header.
75 ptr
= (UINT8
*) FileHeader
;
77 for (Index
= 0; Index
< sizeof (EFI_FFS_FILE_HEADER
) - 3; Index
+= 4) {
78 Sum
= (UINT8
) (Sum
+ ptr
[Index
]);
79 Sum
= (UINT8
) (Sum
+ ptr
[Index
+ 1]);
80 Sum
= (UINT8
) (Sum
+ ptr
[Index
+ 2]);
81 Sum
= (UINT8
) (Sum
+ ptr
[Index
+ 3]);
84 for (; Index
< sizeof (EFI_FFS_FILE_HEADER
); Index
++) {
85 Sum
= (UINT8
) (Sum
+ ptr
[Index
]);
88 // State field (since this indicates the different state of file).
90 Sum
= (UINT8
) (Sum
- FileHeader
->State
);
92 // Checksum field of the file is not part of the header checksum.
94 Sum
= (UINT8
) (Sum
- FileHeader
->IntegrityCheck
.Checksum
.File
);
101 IN EFI_FV_FILETYPE SearchType
,
102 IN EFI_PEI_FV_HANDLE FvHandle
,
103 IN OUT EFI_PEI_FILE_HANDLE
*FileHandle
108 Given the input file pointer, search for the next matching file in the
109 FFS volume as defined by SearchType. The search starts from FileHeader inside
110 the Firmware Volume defined by FwVolHeader.
113 SearchType - Filter to find only files of this type.
114 Type EFI_FV_FILETYPE_ALL causes no filtering to be done.
115 FwVolHeader - Pointer to the FV header of the volume to search.
116 This parameter must point to a valid FFS volume.
117 FileHeader - Pointer to the current file from which to begin searching.
118 This pointer will be updated upon return to reflect the file
122 EFI_NOT_FOUND - No files matching the search criteria were found
127 EFI_FFS_FILE_HEADER
*FfsFileHeader
;
129 UINT32 FileOccupiedSize
;
134 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
135 EFI_FFS_FILE_HEADER
**FileHeader
;
138 // Convert the handle of FV to FV header for memory-mapped firmware volume
140 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) FvHandle
;
141 FileHeader
= (EFI_FFS_FILE_HEADER
**)FileHandle
;
143 FvLength
= FwVolHeader
->FvLength
;
144 if (FwVolHeader
->Attributes
& EFI_FVB2_ERASE_POLARITY
) {
150 // If FileHeader is not specified (NULL) start with the first file in the
151 // firmware volume. Otherwise, start from the FileHeader.
153 if (*FileHeader
== NULL
) {
154 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*) ((UINT8
*) FwVolHeader
+ FwVolHeader
->HeaderLength
);
157 // Length is 24 bits wide so mask upper 8 bits
158 // FileLength is adjusted to FileOccupiedSize as it is 8 byte aligned.
160 FileLength
= *(UINT32
*) (*FileHeader
)->Size
& 0x00FFFFFF;
161 FileOccupiedSize
= GET_OCCUPIED_SIZE (FileLength
, 8);
162 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*) ((UINT8
*) *FileHeader
+ FileOccupiedSize
);
165 FileOffset
= (UINT32
) ((UINT8
*) FfsFileHeader
- (UINT8
*) FwVolHeader
);
167 while (FileOffset
< (FvLength
- sizeof (EFI_FFS_FILE_HEADER
))) {
169 // Get FileState which is the highest bit of the State
171 FileState
= GetFileState (ErasePolarity
, FfsFileHeader
);
175 case EFI_FILE_HEADER_INVALID
:
176 FileOffset
+= sizeof (EFI_FFS_FILE_HEADER
);
177 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*) ((UINT8
*) FfsFileHeader
+ sizeof (EFI_FFS_FILE_HEADER
));
180 case EFI_FILE_DATA_VALID
:
181 case EFI_FILE_MARKED_FOR_UPDATE
:
182 if (CalculateHeaderChecksum (FfsFileHeader
) == 0) {
183 FileLength
= *(UINT32
*) (FfsFileHeader
->Size
) & 0x00FFFFFF;
184 FileOccupiedSize
= GET_OCCUPIED_SIZE (FileLength
, 8);
186 if ((SearchType
== FfsFileHeader
->Type
) || (SearchType
== EFI_FV_FILETYPE_ALL
)) {
188 *FileHeader
= FfsFileHeader
;
193 FileOffset
+= FileOccupiedSize
;
194 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*) ((UINT8
*) FfsFileHeader
+ FileOccupiedSize
);
196 return EFI_NOT_FOUND
;
200 case EFI_FILE_DELETED
:
201 FileLength
= *(UINT32
*) (FfsFileHeader
->Size
) & 0x00FFFFFF;
202 FileOccupiedSize
= GET_OCCUPIED_SIZE (FileLength
, 8);
203 FileOffset
+= FileOccupiedSize
;
204 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*) ((UINT8
*) FfsFileHeader
+ FileOccupiedSize
);
208 return EFI_NOT_FOUND
;
213 return EFI_NOT_FOUND
;
217 SecFfsFindSectionData (
218 IN EFI_SECTION_TYPE SectionType
,
219 IN EFI_FFS_FILE_HEADER
*FfsFileHeader
,
220 IN OUT VOID
**SectionData
225 Given the input file pointer, search for the next matching section in the
229 SearchType - Filter to find only sections of this type.
230 FfsFileHeader - Pointer to the current file to search.
231 SectionData - Pointer to the Section matching SectionType in FfsFileHeader.
232 NULL if section not found
235 EFI_NOT_FOUND - No files matching the search criteria were found
241 EFI_COMMON_SECTION_HEADER
*Section
;
242 UINT32 SectionLength
;
246 // Size is 24 bits wide so mask upper 8 bits.
247 // Does not include FfsFileHeader header size
248 // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.
250 Section
= (EFI_COMMON_SECTION_HEADER
*) (FfsFileHeader
+ 1);
251 FileSize
= *(UINT32
*) (FfsFileHeader
->Size
) & 0x00FFFFFF;
252 FileSize
-= sizeof (EFI_FFS_FILE_HEADER
);
256 while (ParsedLength
< FileSize
) {
257 if (Section
->Type
== SectionType
) {
258 *SectionData
= (VOID
*) (Section
+ 1);
262 // Size is 24 bits wide so mask upper 8 bits.
263 // SectionLength is adjusted it is 4 byte aligned.
264 // Go to the next section
266 SectionLength
= *(UINT32
*) Section
->Size
& 0x00FFFFFF;
267 SectionLength
= GET_OCCUPIED_SIZE (SectionLength
, 4);
269 ParsedLength
+= SectionLength
;
270 Section
= (EFI_COMMON_SECTION_HEADER
*) ((UINT8
*) Section
+ SectionLength
);
273 return EFI_NOT_FOUND
;