]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Dxe/FwVol/Ffs.c
MdeModulePkg: Apply uncrustify changes
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / FwVol / Ffs.c
1 /** @file
2 FFS file access utilities.
3
4 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include "DxeMain.h"
10 #include "FwVolDriver.h"
11
12 /**
13 Get the FFS file state by checking the highest bit set in the header's state field.
14
15 @param ErasePolarity Erase polarity attribute of the firmware volume
16 @param FfsHeader Points to the FFS file header
17
18 @return FFS File state
19
20 **/
21 EFI_FFS_FILE_STATE
22 GetFileState (
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
44 /**
45 Check if a block of buffer is erased.
46
47 @param ErasePolarity Erase polarity attribute of the firmware volume
48 @param InBuffer The buffer to be checked
49 @param BufferSize Size of the buffer in bytes
50
51 @retval TRUE The block of buffer is erased
52 @retval FALSE The block of buffer is not erased
53
54 **/
55 BOOLEAN
56 IsBufferErased (
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
82 /**
83 Verify checksum of the firmware volume header.
84
85 @param FvHeader Points to the firmware volume header to be checked
86
87 @retval TRUE Checksum verification passed
88 @retval FALSE Checksum verification failed
89
90 **/
91 BOOLEAN
92 VerifyFvHeaderChecksum (
93 IN EFI_FIRMWARE_VOLUME_HEADER *FvHeader
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
107 /**
108 Verify checksum of the FFS file header.
109
110 @param FfsHeader Points to the FFS file header to be checked
111
112 @retval TRUE Checksum verification passed
113 @retval FALSE Checksum verification failed
114
115 **/
116 BOOLEAN
117 VerifyHeaderChecksum (
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
138 /**
139 Check if it's a valid FFS file header.
140
141 @param ErasePolarity Erase polarity attribute of the firmware volume
142 @param FfsHeader Points to the FFS file header to be checked
143 @param FileState FFS file state to be returned
144
145 @retval TRUE Valid FFS file header
146 @retval FALSE Invalid FFS file header
147
148 **/
149 BOOLEAN
150 IsValidFfsHeader (
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
168 case EFI_FILE_HEADER_CONSTRUCTION:
169 case EFI_FILE_HEADER_INVALID:
170 default:
171 return FALSE;
172 }
173 }
174
175 /**
176 Check if it's a valid FFS file.
177 Here we are sure that it has a valid FFS file header since we must call IsValidFfsHeader() first.
178
179 @param ErasePolarity Erase polarity attribute of the firmware volume
180 @param FfsHeader Points to the FFS file to be checked
181
182 @retval TRUE Valid FFS file
183 @retval FALSE Invalid FFS file
184
185 **/
186 BOOLEAN
187 IsValidFfsFile (
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 }