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 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #define GET_OCCUPIED_SIZE(ActualSize, Alignment) \
19 (ActualSize) + (((Alignment) - ((ActualSize) & ((Alignment) - 1))) & ((Alignment) - 1))
23 IN UINT8 ErasePolarity
,
24 IN EFI_FFS_FILE_HEADER
*FfsHeader
29 Returns the highest bit set of the State field
32 ErasePolarity - Erase Polarity as defined by EFI_FVB2_ERASE_POLARITY
33 in the Attributes field.
34 FfsHeader - Pointer to FFS File Header.
37 Returns the highest bit in the State field
41 EFI_FFS_FILE_STATE FileState
;
42 EFI_FFS_FILE_STATE HighestBit
;
44 FileState
= FfsHeader
->State
;
46 if (ErasePolarity
!= 0) {
47 FileState
= (EFI_FFS_FILE_STATE
)~FileState
;
51 while (HighestBit
!= 0 && (HighestBit
& FileState
) == 0) {
59 CalculateHeaderChecksum (
60 IN EFI_FFS_FILE_HEADER
*FileHeader
65 Calculates the checksum of the header of a file.
68 FileHeader - Pointer to FFS File Header.
71 Checksum of the header.
80 ptr
= (UINT8
*) FileHeader
;
82 for (Index
= 0; Index
< sizeof (EFI_FFS_FILE_HEADER
) - 3; Index
+= 4) {
83 Sum
= (UINT8
) (Sum
+ ptr
[Index
]);
84 Sum
= (UINT8
) (Sum
+ ptr
[Index
+ 1]);
85 Sum
= (UINT8
) (Sum
+ ptr
[Index
+ 2]);
86 Sum
= (UINT8
) (Sum
+ ptr
[Index
+ 3]);
89 for (; Index
< sizeof (EFI_FFS_FILE_HEADER
); Index
++) {
90 Sum
= (UINT8
) (Sum
+ ptr
[Index
]);
93 // State field (since this indicates the different state of file).
95 Sum
= (UINT8
) (Sum
- FileHeader
->State
);
97 // Checksum field of the file is not part of the header checksum.
99 Sum
= (UINT8
) (Sum
- FileHeader
->IntegrityCheck
.Checksum
.File
);
106 IN EFI_FV_FILETYPE SearchType
,
107 IN EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
,
108 IN OUT EFI_FFS_FILE_HEADER
**FileHeader
113 Given the input file pointer, search for the next matching file in the
114 FFS volume as defined by SearchType. The search starts from FileHeader inside
115 the Firmware Volume defined by FwVolHeader.
118 SearchType - Filter to find only files of this type.
119 Type EFI_FV_FILETYPE_ALL causes no filtering to be done.
120 FwVolHeader - Pointer to the FV header of the volume to search.
121 This parameter must point to a valid FFS volume.
122 FileHeader - Pointer to the current file from which to begin searching.
123 This pointer will be updated upon return to reflect the file
127 EFI_NOT_FOUND - No files matching the search criteria were found
132 EFI_FFS_FILE_HEADER
*FfsFileHeader
;
134 UINT32 FileOccupiedSize
;
140 FvLength
= FwVolHeader
->FvLength
;
141 if (FwVolHeader
->Attributes
& EFI_FVB2_ERASE_POLARITY
) {
147 // If FileHeader is not specified (NULL) start with the first file in the
148 // firmware volume. Otherwise, start from the FileHeader.
150 if (*FileHeader
== NULL
) {
151 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*) ((UINT8
*) FwVolHeader
+ FwVolHeader
->HeaderLength
);
154 // Length is 24 bits wide so mask upper 8 bits
155 // FileLength is adjusted to FileOccupiedSize as it is 8 byte aligned.
157 FileLength
= *(UINT32
*) (*FileHeader
)->Size
& 0x00FFFFFF;
158 FileOccupiedSize
= GET_OCCUPIED_SIZE (FileLength
, 8);
159 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*) ((UINT8
*) *FileHeader
+ FileOccupiedSize
);
162 FileOffset
= (UINT32
) ((UINT8
*) FfsFileHeader
- (UINT8
*) FwVolHeader
);
164 while (FileOffset
< (FvLength
- sizeof (EFI_FFS_FILE_HEADER
))) {
166 // Get FileState which is the highest bit of the State
168 FileState
= GetFileState (ErasePolarity
, FfsFileHeader
);
172 case EFI_FILE_HEADER_INVALID
:
173 FileOffset
+= sizeof (EFI_FFS_FILE_HEADER
);
174 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*) ((UINT8
*) FfsFileHeader
+ sizeof (EFI_FFS_FILE_HEADER
));
177 case EFI_FILE_DATA_VALID
:
178 case EFI_FILE_MARKED_FOR_UPDATE
:
179 if (CalculateHeaderChecksum (FfsFileHeader
) == 0) {
180 FileLength
= *(UINT32
*) (FfsFileHeader
->Size
) & 0x00FFFFFF;
181 FileOccupiedSize
= GET_OCCUPIED_SIZE (FileLength
, 8);
183 if ((SearchType
== FfsFileHeader
->Type
) || (SearchType
== EFI_FV_FILETYPE_ALL
)) {
185 *FileHeader
= FfsFileHeader
;
190 FileOffset
+= FileOccupiedSize
;
191 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*) ((UINT8
*) FfsFileHeader
+ FileOccupiedSize
);
193 return EFI_NOT_FOUND
;
197 case EFI_FILE_DELETED
:
198 FileLength
= *(UINT32
*) (FfsFileHeader
->Size
) & 0x00FFFFFF;
199 FileOccupiedSize
= GET_OCCUPIED_SIZE (FileLength
, 8);
200 FileOffset
+= FileOccupiedSize
;
201 FfsFileHeader
= (EFI_FFS_FILE_HEADER
*) ((UINT8
*) FfsFileHeader
+ FileOccupiedSize
);
205 return EFI_NOT_FOUND
;
210 return EFI_NOT_FOUND
;
214 SecFfsFindSectionData (
215 IN EFI_SECTION_TYPE SectionType
,
216 IN EFI_FFS_FILE_HEADER
*FfsFileHeader
,
217 IN OUT VOID
**SectionData
222 Given the input file pointer, search for the next matching section in the
226 SearchType - Filter to find only sections of this type.
227 FfsFileHeader - Pointer to the current file to search.
228 SectionData - Pointer to the Section matching SectionType in FfsFileHeader.
229 NULL if section not found
232 EFI_NOT_FOUND - No files matching the search criteria were found
238 EFI_COMMON_SECTION_HEADER
*Section
;
239 UINT32 SectionLength
;
243 // Size is 24 bits wide so mask upper 8 bits.
244 // Does not include FfsFileHeader header size
245 // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.
247 Section
= (EFI_COMMON_SECTION_HEADER
*) (FfsFileHeader
+ 1);
248 FileSize
= *(UINT32
*) (FfsFileHeader
->Size
) & 0x00FFFFFF;
249 FileSize
-= sizeof (EFI_FFS_FILE_HEADER
);
253 while (ParsedLength
< FileSize
) {
254 if (Section
->Type
== SectionType
) {
255 *SectionData
= (VOID
*) (Section
+ 1);
259 // Size is 24 bits wide so mask upper 8 bits.
260 // SectionLength is adjusted it is 4 byte aligned.
261 // Go to the next section
263 SectionLength
= *(UINT32
*) Section
->Size
& 0x00FFFFFF;
264 SectionLength
= GET_OCCUPIED_SIZE (SectionLength
, 4);
266 ParsedLength
+= SectionLength
;
267 Section
= (EFI_COMMON_SECTION_HEADER
*) ((UINT8
*) Section
+ SectionLength
);
270 return EFI_NOT_FOUND
;
275 IN EFI_FIRMWARE_VOLUME_HEADER
*FwVolHeader
,
281 Given the pointer to the Firmware Volume Header find the SEC
282 core and return it's PE32 image.
285 FwVolHeader - Pointer to memory mapped FV
286 Pe32Data - Pointer to SEC PE32 iamge.
289 EFI_SUCCESS - Pe32Data is valid
295 EFI_FFS_FILE_HEADER
*FileHeader
;
296 EFI_FV_FILETYPE SearchType
;
298 SearchType
= EFI_FV_FILETYPE_PEI_CORE
;
301 Status
= SecFfsFindNextFile (SearchType
, FwVolHeader
, &FileHeader
);
302 if (!EFI_ERROR (Status
)) {
303 Status
= SecFfsFindSectionData (EFI_SECTION_PE32
, FileHeader
, Pe32Data
);
306 } while (!EFI_ERROR (Status
));