3 Copyright (c) 2006, 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
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.
16 A simple FV stack so the SEC can extract the SEC Core from an
23 #define GET_OCCUPIED_SIZE(ActualSize, Alignment) \
24 (ActualSize) + (((Alignment) - ((ActualSize) & ((Alignment) - 1))) & ((Alignment) - 1))
28 IN UINT8 ErasePolarity
,
29 IN EFI_FFS_FILE_HEADER
*FfsHeader
34 Returns the highest bit set of the State field
37 ErasePolarity - Erase Polarity as defined by EFI_FVB_ERASE_POLARITY
38 in the Attributes field.
39 FfsHeader - Pointer to FFS File Header.
42 Returns the highest bit in the State field
46 EFI_FFS_FILE_STATE FileState
;
47 EFI_FFS_FILE_STATE HighestBit
;
49 FileState
= FfsHeader
->State
;
51 if (ErasePolarity
!= 0) {
52 FileState
= (EFI_FFS_FILE_STATE
)~FileState
;
56 while (HighestBit
!= 0 && (HighestBit
& FileState
) == 0) {
64 CalculateHeaderChecksum (
65 IN EFI_FFS_FILE_HEADER
*FileHeader
70 Calculates the checksum of the header of a file.
73 FileHeader - Pointer to FFS File Header.
76 Checksum of the header.
85 ptr
= (UINT8
*) FileHeader
;
87 for (Index
= 0; Index
< sizeof (EFI_FFS_FILE_HEADER
) - 3; Index
+= 4) {
88 Sum
= (UINT8
) (Sum
+ ptr
[Index
]);
89 Sum
= (UINT8
) (Sum
+ ptr
[Index
+ 1]);
90 Sum
= (UINT8
) (Sum
+ ptr
[Index
+ 2]);
91 Sum
= (UINT8
) (Sum
+ ptr
[Index
+ 3]);
94 for (; Index
< sizeof (EFI_FFS_FILE_HEADER
); Index
++) {
95 Sum
= (UINT8
) (Sum
+ ptr
[Index
]);
98 // State field (since this indicates the different state of file).
100 Sum
= (UINT8
) (Sum
- FileHeader
->State
);
102 // Checksum field of the file is not part of the header checksum.
104 Sum
= (UINT8
) (Sum
- FileHeader
->IntegrityCheck
.Checksum
.File
);
111 IN EFI_FV_FILETYPE SearchType
,
112 IN EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
,
113 IN OUT EFI_FFS_FILE_HEADER
**FileHeader
118 Given the input file pointer, search for the next matching file in the
119 FFS volume as defined by SearchType. The search starts from FileHeader inside
120 the Firmware Volume defined by FwVolHeader.
123 SearchType - Filter to find only files of this type.
124 Type EFI_FV_FILETYPE_ALL causes no filtering to be done.
125 FwVolHeader - Pointer to the FV header of the volume to search.
126 This parameter must point to a valid FFS volume.
127 FileHeader - Pointer to the current file from which to begin searching.
128 This pointer will be updated upon return to reflect the file
132 EFI_NOT_FOUND - No files matching the search criteria were found
137 EFI_FFS_FILE_HEADER
*FfsFileHeader
;
139 UINT32 FileOccupiedSize
;
145 FvLength
= FwVolHeader
->FvLength
;
146 if (FwVolHeader
->Attributes
& EFI_FVB2_ERASE_POLARITY
) {
152 // If FileHeader is not specified (NULL) start with the first file in the
153 // firmware volume. Otherwise, start from the FileHeader.
155 if (*FileHeader
== NULL
) {
156 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*) ((UINT8
*) FwVolHeader
+ FwVolHeader
->HeaderLength
);
159 // Length is 24 bits wide so mask upper 8 bits
160 // FileLength is adjusted to FileOccupiedSize as it is 8 byte aligned.
162 FileLength
= *(UINT32
*) (*FileHeader
)->Size
& 0x00FFFFFF;
163 FileOccupiedSize
= GET_OCCUPIED_SIZE (FileLength
, 8);
164 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*) ((UINT8
*) *FileHeader
+ FileOccupiedSize
);
167 FileOffset
= (UINT32
) ((UINT8
*) FfsFileHeader
- (UINT8
*) FwVolHeader
);
169 while (FileOffset
< (FvLength
- sizeof (EFI_FFS_FILE_HEADER
))) {
171 // Get FileState which is the highest bit of the State
173 FileState
= GetFileState (ErasePolarity
, FfsFileHeader
);
177 case EFI_FILE_HEADER_INVALID
:
178 FileOffset
+= sizeof (EFI_FFS_FILE_HEADER
);
179 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*) ((UINT8
*) FfsFileHeader
+ sizeof (EFI_FFS_FILE_HEADER
));
182 case EFI_FILE_DATA_VALID
:
183 case EFI_FILE_MARKED_FOR_UPDATE
:
184 if (CalculateHeaderChecksum (FfsFileHeader
) == 0) {
185 FileLength
= *(UINT32
*) (FfsFileHeader
->Size
) & 0x00FFFFFF;
186 FileOccupiedSize
= GET_OCCUPIED_SIZE (FileLength
, 8);
188 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
;
202 case EFI_FILE_DELETED
:
203 FileLength
= *(UINT32
*) (FfsFileHeader
->Size
) & 0x00FFFFFF;
204 FileOccupiedSize
= GET_OCCUPIED_SIZE (FileLength
, 8);
205 FileOffset
+= FileOccupiedSize
;
206 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*) ((UINT8
*) FfsFileHeader
+ FileOccupiedSize
);
210 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
227 Given the input file pointer, search for the next matching section in the
231 SearchType - Filter to find only sections of this type.
232 FfsFileHeader - Pointer to the current file to search.
233 SectionData - Pointer to the Section matching SectionType in FfsFileHeader.
234 NULL if section not found
237 EFI_NOT_FOUND - No files matching the search criteria were found
243 EFI_COMMON_SECTION_HEADER
*Section
;
244 UINT32 SectionLength
;
248 // Size is 24 bits wide so mask upper 8 bits.
249 // Does not include FfsFileHeader header size
250 // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.
252 Section
= (EFI_COMMON_SECTION_HEADER
*) (FfsFileHeader
+ 1);
253 FileSize
= *(UINT32
*) (FfsFileHeader
->Size
) & 0x00FFFFFF;
254 FileSize
-= sizeof (EFI_FFS_FILE_HEADER
);
258 while (ParsedLength
< FileSize
) {
259 if (Section
->Type
== SectionType
) {
260 *SectionData
= (VOID
*) (Section
+ 1);
264 // Size is 24 bits wide so mask upper 8 bits.
265 // SectionLength is adjusted it is 4 byte aligned.
266 // Go to the next section
268 SectionLength
= *(UINT32
*) Section
->Size
& 0x00FFFFFF;
269 SectionLength
= GET_OCCUPIED_SIZE (SectionLength
, 4);
271 ParsedLength
+= SectionLength
;
272 Section
= (EFI_COMMON_SECTION_HEADER
*) ((UINT8
*) Section
+ SectionLength
);
275 return EFI_NOT_FOUND
;
280 IN EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
,
286 Given the pointer to the Firmware Volume Header find the SEC
287 core and return it's PE32 image.
290 FwVolHeader - Pointer to memory mapped FV
291 Pe32Data - Pointer to SEC PE32 iamge.
294 EFI_SUCCESS - Pe32Data is valid
300 EFI_FFS_FILE_HEADER
*FileHeader
;
301 EFI_FV_FILETYPE SearchType
;
303 SearchType
= EFI_FV_FILETYPE_PEI_CORE
;
306 Status
= SecFfsFindNextFile (SearchType
, FwVolHeader
, &FileHeader
);
307 if (!EFI_ERROR (Status
)) {
308 Status
= SecFfsFindSectionData (EFI_SECTION_PE32
, FileHeader
, Pe32Data
);
311 } while (!EFI_ERROR (Status
));