TianoCore EDK2 master
Loading...
Searching...
No Matches
Ffs.c
Go to the documentation of this file.
1
9#include "DxeMain.h"
10#include "FwVolDriver.h"
11
21EFI_FFS_FILE_STATE
23 IN UINT8 ErasePolarity,
24 IN EFI_FFS_FILE_HEADER *FfsHeader
25 )
26{
27 EFI_FFS_FILE_STATE FileState;
28 UINT8 HighestBit;
29
30 FileState = FfsHeader->State;
31
32 if (ErasePolarity != 0) {
33 FileState = (EFI_FFS_FILE_STATE) ~FileState;
34 }
35
36 HighestBit = 0x80;
37 while (HighestBit != 0 && ((HighestBit & FileState) == 0)) {
38 HighestBit >>= 1;
39 }
40
41 return (EFI_FFS_FILE_STATE)HighestBit;
42}
43
55BOOLEAN
57 IN UINT8 ErasePolarity,
58 IN VOID *InBuffer,
59 IN UINTN BufferSize
60 )
61{
62 UINTN Count;
63 UINT8 EraseByte;
64 UINT8 *Buffer;
65
66 if (ErasePolarity == 1) {
67 EraseByte = 0xFF;
68 } else {
69 EraseByte = 0;
70 }
71
72 Buffer = InBuffer;
73 for (Count = 0; Count < BufferSize; Count++) {
74 if (Buffer[Count] != EraseByte) {
75 return FALSE;
76 }
77 }
78
79 return TRUE;
80}
81
91BOOLEAN
94 )
95{
96 UINT16 Checksum;
97
98 Checksum = CalculateSum16 ((UINT16 *)FvHeader, FvHeader->HeaderLength);
99
100 if (Checksum == 0) {
101 return TRUE;
102 } else {
103 return FALSE;
104 }
105}
106
116BOOLEAN
118 IN EFI_FFS_FILE_HEADER *FfsHeader
119 )
120{
121 UINT8 HeaderChecksum;
122
123 if (IS_FFS_FILE2 (FfsHeader)) {
124 HeaderChecksum = CalculateSum8 ((UINT8 *)FfsHeader, sizeof (EFI_FFS_FILE_HEADER2));
125 } else {
126 HeaderChecksum = CalculateSum8 ((UINT8 *)FfsHeader, sizeof (EFI_FFS_FILE_HEADER));
127 }
128
129 HeaderChecksum = (UINT8)(HeaderChecksum - FfsHeader->State - FfsHeader->IntegrityCheck.Checksum.File);
130
131 if (HeaderChecksum == 0) {
132 return TRUE;
133 } else {
134 return FALSE;
135 }
136}
137
149BOOLEAN
151 IN UINT8 ErasePolarity,
152 IN EFI_FFS_FILE_HEADER *FfsHeader,
153 OUT EFI_FFS_FILE_STATE *FileState
154 )
155{
156 *FileState = GetFileState (ErasePolarity, FfsHeader);
157
158 switch (*FileState) {
159 case EFI_FILE_HEADER_VALID:
160 case EFI_FILE_DATA_VALID:
161 case EFI_FILE_MARKED_FOR_UPDATE:
162 case EFI_FILE_DELETED:
163 //
164 // Here we need to verify header checksum
165 //
166 return VerifyHeaderChecksum (FfsHeader);
167
169 case EFI_FILE_HEADER_INVALID:
170 default:
171 return FALSE;
172 }
173}
174
186BOOLEAN
188 IN UINT8 ErasePolarity,
189 IN EFI_FFS_FILE_HEADER *FfsHeader
190 )
191{
192 EFI_FFS_FILE_STATE FileState;
193 UINT8 DataCheckSum;
194
195 FileState = GetFileState (ErasePolarity, FfsHeader);
196 switch (FileState) {
197 case EFI_FILE_DELETED:
198 case EFI_FILE_DATA_VALID:
199 case EFI_FILE_MARKED_FOR_UPDATE:
200 DataCheckSum = FFS_FIXED_CHECKSUM;
201 if ((FfsHeader->Attributes & FFS_ATTRIB_CHECKSUM) == FFS_ATTRIB_CHECKSUM) {
202 if (IS_FFS_FILE2 (FfsHeader)) {
203 DataCheckSum = CalculateCheckSum8 ((CONST UINT8 *)FfsHeader + sizeof (EFI_FFS_FILE_HEADER2), FFS_FILE2_SIZE (FfsHeader) - sizeof (EFI_FFS_FILE_HEADER2));
204 } else {
205 DataCheckSum = CalculateCheckSum8 ((CONST UINT8 *)FfsHeader + sizeof (EFI_FFS_FILE_HEADER), FFS_FILE_SIZE (FfsHeader) - sizeof (EFI_FFS_FILE_HEADER));
206 }
207 }
208
209 if (FfsHeader->IntegrityCheck.Checksum.File == DataCheckSum) {
210 return TRUE;
211 }
212
213 default:
214 return FALSE;
215 }
216}
UINT64 UINTN
UINT16 EFIAPI CalculateSum16(IN CONST UINT16 *Buffer, IN UINTN Length)
Definition: CheckSum.c:107
UINT8 EFIAPI CalculateSum8(IN CONST UINT8 *Buffer, IN UINTN Length)
Definition: CheckSum.c:33
UINT8 EFIAPI CalculateCheckSum8(IN CONST UINT8 *Buffer, IN UINTN Length)
Definition: CheckSum.c:71
EFI_FFS_FILE_STATE GetFileState(IN UINT8 ErasePolarity, IN EFI_FFS_FILE_HEADER *FfsHeader)
Definition: Ffs.c:22
BOOLEAN IsValidFfsHeader(IN UINT8 ErasePolarity, IN EFI_FFS_FILE_HEADER *FfsHeader, OUT EFI_FFS_FILE_STATE *FileState)
Definition: Ffs.c:150
BOOLEAN IsValidFfsFile(IN UINT8 ErasePolarity, IN EFI_FFS_FILE_HEADER *FfsHeader)
Definition: Ffs.c:187
BOOLEAN VerifyFvHeaderChecksum(IN EFI_FIRMWARE_VOLUME_HEADER *FvHeader)
Definition: Ffs.c:92
BOOLEAN VerifyHeaderChecksum(IN EFI_FFS_FILE_HEADER *FfsHeader)
Definition: Ffs.c:117
BOOLEAN IsBufferErased(IN UINT8 ErasePolarity, IN VOID *InBuffer, IN UINTN BufferSize)
Definition: Ffs.c:56
#define CONST
Definition: Base.h:259
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define EFI_FILE_HEADER_CONSTRUCTION
#define FFS_FIXED_CHECKSUM
#define FFS_FILE_SIZE(FfsFileHeaderPtr)