2 FFS file access utilities.
4 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "FwVolDriver.h"
21 Get the FFS file state by checking the highest bit set in the header's state field.
23 @param ErasePolarity Erase polarity attribute of the firmware volume
24 @param FfsHeader Points to the FFS file header
26 @return FFS File state
31 IN UINT8 ErasePolarity
,
32 IN EFI_FFS_FILE_HEADER
*FfsHeader
35 EFI_FFS_FILE_STATE FileState
;
38 FileState
= FfsHeader
->State
;
40 if (ErasePolarity
!= 0) {
41 FileState
= (EFI_FFS_FILE_STATE
)~FileState
;
45 while (HighestBit
!= 0 && ((HighestBit
& FileState
) == 0)) {
49 return (EFI_FFS_FILE_STATE
) HighestBit
;
55 Check if a block of buffer is erased.
57 @param ErasePolarity Erase polarity attribute of the firmware volume
58 @param InBuffer The buffer to be checked
59 @param BufferSize Size of the buffer in bytes
61 @retval TRUE The block of buffer is erased
62 @retval FALSE The block of buffer is not erased
67 IN UINT8 ErasePolarity
,
76 if(ErasePolarity
== 1) {
83 for (Count
= 0; Count
< BufferSize
; Count
++) {
84 if (Buffer
[Count
] != EraseByte
) {
95 Verify checksum of the firmware volume header.
97 @param FvHeader Points to the firmware volume header to be checked
99 @retval TRUE Checksum verification passed
100 @retval FALSE Checksum verification failed
104 VerifyFvHeaderChecksum (
105 IN EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
110 Checksum
= CalculateSum16 ((UINT16
*) FvHeader
, FvHeader
->HeaderLength
);
121 Verify checksum of the FFS file header.
123 @param FfsHeader Points to the FFS file header to be checked
125 @retval TRUE Checksum verification passed
126 @retval FALSE Checksum verification failed
130 VerifyHeaderChecksum (
131 IN EFI_FFS_FILE_HEADER
*FfsHeader
134 UINT8 HeaderChecksum
;
136 if (IS_FFS_FILE2 (FfsHeader
)) {
137 HeaderChecksum
= CalculateSum8 ((UINT8
*) FfsHeader
, sizeof (EFI_FFS_FILE_HEADER2
));
139 HeaderChecksum
= CalculateSum8 ((UINT8
*) FfsHeader
, sizeof (EFI_FFS_FILE_HEADER
));
141 HeaderChecksum
= (UINT8
) (HeaderChecksum
- FfsHeader
->State
- FfsHeader
->IntegrityCheck
.Checksum
.File
);
143 if (HeaderChecksum
== 0) {
153 Check if it's a valid FFS file header.
155 @param ErasePolarity Erase polarity attribute of the firmware volume
156 @param FfsHeader Points to the FFS file header to be checked
157 @param FileState FFS file state to be returned
159 @retval TRUE Valid FFS file header
160 @retval FALSE Invalid FFS file header
165 IN UINT8 ErasePolarity
,
166 IN EFI_FFS_FILE_HEADER
*FfsHeader
,
167 OUT EFI_FFS_FILE_STATE
*FileState
170 *FileState
= GetFileState (ErasePolarity
, FfsHeader
);
172 switch (*FileState
) {
173 case EFI_FILE_HEADER_VALID
:
174 case EFI_FILE_DATA_VALID
:
175 case EFI_FILE_MARKED_FOR_UPDATE
:
176 case EFI_FILE_DELETED
:
178 // Here we need to verify header checksum
180 return VerifyHeaderChecksum (FfsHeader
);
182 case EFI_FILE_HEADER_CONSTRUCTION
:
183 case EFI_FILE_HEADER_INVALID
:
191 Check if it's a valid FFS file.
192 Here we are sure that it has a valid FFS file header since we must call IsValidFfsHeader() first.
194 @param ErasePolarity Erase polarity attribute of the firmware volume
195 @param FfsHeader Points to the FFS file to be checked
197 @retval TRUE Valid FFS file
198 @retval FALSE Invalid FFS file
203 IN UINT8 ErasePolarity
,
204 IN EFI_FFS_FILE_HEADER
*FfsHeader
207 EFI_FFS_FILE_STATE FileState
;
210 FileState
= GetFileState (ErasePolarity
, FfsHeader
);
213 case EFI_FILE_DELETED
:
214 case EFI_FILE_DATA_VALID
:
215 case EFI_FILE_MARKED_FOR_UPDATE
:
216 DataCheckSum
= FFS_FIXED_CHECKSUM
;
217 if ((FfsHeader
->Attributes
& FFS_ATTRIB_CHECKSUM
) == FFS_ATTRIB_CHECKSUM
) {
218 if (IS_FFS_FILE2 (FfsHeader
)) {
219 DataCheckSum
= CalculateCheckSum8 ((CONST UINT8
*) FfsHeader
+ sizeof (EFI_FFS_FILE_HEADER2
), FFS_FILE2_SIZE (FfsHeader
) - sizeof(EFI_FFS_FILE_HEADER2
));
221 DataCheckSum
= CalculateCheckSum8 ((CONST UINT8
*) FfsHeader
+ sizeof (EFI_FFS_FILE_HEADER
), FFS_FILE_SIZE (FfsHeader
) - sizeof(EFI_FFS_FILE_HEADER
));
224 if (FfsHeader
->IntegrityCheck
.Checksum
.File
== DataCheckSum
) {