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
12 #define GET_OCCUPIED_SIZE(ActualSize, Alignment) \
13 (ActualSize) + (((Alignment) - ((ActualSize) & ((Alignment) - 1))) & ((Alignment) - 1))
17 IN UINT8 ErasePolarity
,
18 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
61 Calculates the checksum of the header of a file.
64 FileHeader - Pointer to FFS File Header.
67 Checksum of the header.
76 ptr
= (UINT8
*)FileHeader
;
78 for (Index
= 0; Index
< sizeof (EFI_FFS_FILE_HEADER
) - 3; Index
+= 4) {
79 Sum
= (UINT8
)(Sum
+ ptr
[Index
]);
80 Sum
= (UINT8
)(Sum
+ ptr
[Index
+ 1]);
81 Sum
= (UINT8
)(Sum
+ ptr
[Index
+ 2]);
82 Sum
= (UINT8
)(Sum
+ ptr
[Index
+ 3]);
85 for ( ; Index
< sizeof (EFI_FFS_FILE_HEADER
); Index
++) {
86 Sum
= (UINT8
)(Sum
+ ptr
[Index
]);
90 // State field (since this indicates the different state of file).
92 Sum
= (UINT8
)(Sum
- FileHeader
->State
);
94 // Checksum field of the file is not part of the header checksum.
96 Sum
= (UINT8
)(Sum
- FileHeader
->IntegrityCheck
.Checksum
.File
);
103 IN EFI_FV_FILETYPE SearchType
,
104 IN EFI_PEI_FV_HANDLE FvHandle
,
105 IN OUT EFI_PEI_FILE_HANDLE
*FileHandle
111 Given the input file pointer, search for the next matching file in the
112 FFS volume as defined by SearchType. The search starts from FileHeader inside
113 the Firmware Volume defined by FwVolHeader.
116 SearchType - Filter to find only files of this type.
117 Type EFI_FV_FILETYPE_ALL causes no filtering to be done.
118 FwVolHeader - Pointer to the FV header of the volume to search.
119 This parameter must point to a valid FFS volume.
120 FileHeader - Pointer to the current file from which to begin searching.
121 This pointer will be updated upon return to reflect the file
125 EFI_NOT_FOUND - No files matching the search criteria were found
130 EFI_FFS_FILE_HEADER
*FfsFileHeader
;
132 UINT32 FileOccupiedSize
;
137 EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
;
138 EFI_FFS_FILE_HEADER
**FileHeader
;
141 // Convert the handle of FV to FV header for memory-mapped firmware volume
143 FwVolHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*)FvHandle
;
144 FileHeader
= (EFI_FFS_FILE_HEADER
**)FileHandle
;
146 FvLength
= FwVolHeader
->FvLength
;
147 if (FwVolHeader
->Attributes
& EFI_FVB2_ERASE_POLARITY
) {
154 // If FileHeader is not specified (NULL) start with the first file in the
155 // firmware volume. Otherwise, start from the FileHeader.
157 if (*FileHeader
== NULL
) {
158 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*)((UINT8
*)FwVolHeader
+ FwVolHeader
->HeaderLength
);
161 // Length is 24 bits wide so mask upper 8 bits
162 // FileLength is adjusted to FileOccupiedSize as it is 8 byte aligned.
164 FileLength
= *(UINT32
*)(*FileHeader
)->Size
& 0x00FFFFFF;
165 FileOccupiedSize
= GET_OCCUPIED_SIZE (FileLength
, 8);
166 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*)((UINT8
*)*FileHeader
+ FileOccupiedSize
);
169 FileOffset
= (UINT32
)((UINT8
*)FfsFileHeader
- (UINT8
*)FwVolHeader
);
171 while (FileOffset
< (FvLength
- sizeof (EFI_FFS_FILE_HEADER
))) {
173 // Get FileState which is the highest bit of the State
175 FileState
= GetFileState (ErasePolarity
, FfsFileHeader
);
178 case EFI_FILE_HEADER_INVALID
:
179 FileOffset
+= sizeof (EFI_FFS_FILE_HEADER
);
180 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*)((UINT8
*)FfsFileHeader
+ sizeof (EFI_FFS_FILE_HEADER
));
183 case EFI_FILE_DATA_VALID
:
184 case EFI_FILE_MARKED_FOR_UPDATE
:
185 if (CalculateHeaderChecksum (FfsFileHeader
) == 0) {
186 FileLength
= *(UINT32
*)(FfsFileHeader
->Size
) & 0x00FFFFFF;
187 FileOccupiedSize
= GET_OCCUPIED_SIZE (FileLength
, 8);
189 if ((SearchType
== FfsFileHeader
->Type
) || (SearchType
== EFI_FV_FILETYPE_ALL
)) {
190 *FileHeader
= FfsFileHeader
;
195 FileOffset
+= FileOccupiedSize
;
196 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*)((UINT8
*)FfsFileHeader
+ FileOccupiedSize
);
198 return EFI_NOT_FOUND
;
203 case EFI_FILE_DELETED
:
204 FileLength
= *(UINT32
*)(FfsFileHeader
->Size
) & 0x00FFFFFF;
205 FileOccupiedSize
= GET_OCCUPIED_SIZE (FileLength
, 8);
206 FileOffset
+= FileOccupiedSize
;
207 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*)((UINT8
*)FfsFileHeader
+ FileOccupiedSize
);
211 return EFI_NOT_FOUND
;
215 return EFI_NOT_FOUND
;
219 SecFfsFindSectionData (
220 IN EFI_SECTION_TYPE SectionType
,
221 IN EFI_FFS_FILE_HEADER
*FfsFileHeader
,
222 IN OUT VOID
**SectionData
228 Given the input file pointer, search for the next matching section in the
232 SearchType - Filter to find only sections of this type.
233 FfsFileHeader - Pointer to the current file to search.
234 SectionData - Pointer to the Section matching SectionType in FfsFileHeader.
235 NULL if section not found
238 EFI_NOT_FOUND - No files matching the search criteria were found
244 EFI_COMMON_SECTION_HEADER
*Section
;
245 UINT32 SectionLength
;
249 // Size is 24 bits wide so mask upper 8 bits.
250 // Does not include FfsFileHeader header size
251 // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.
253 Section
= (EFI_COMMON_SECTION_HEADER
*)(FfsFileHeader
+ 1);
254 FileSize
= *(UINT32
*)(FfsFileHeader
->Size
) & 0x00FFFFFF;
255 FileSize
-= sizeof (EFI_FFS_FILE_HEADER
);
259 while (ParsedLength
< FileSize
) {
260 if (Section
->Type
== SectionType
) {
261 *SectionData
= (VOID
*)(Section
+ 1);
266 // Size is 24 bits wide so mask upper 8 bits.
267 // SectionLength is adjusted it is 4 byte aligned.
268 // Go to the next section
270 SectionLength
= *(UINT32
*)Section
->Size
& 0x00FFFFFF;
271 SectionLength
= GET_OCCUPIED_SIZE (SectionLength
, 4);
273 ParsedLength
+= SectionLength
;
274 Section
= (EFI_COMMON_SECTION_HEADER
*)((UINT8
*)Section
+ SectionLength
);
277 return EFI_NOT_FOUND
;