]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Dxe/FwVol/Ffs.c
Code Scrub for Dxe Core.
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / FwVol / Ffs.c
1 /** @file
2 FFS file access utilities.
3
4 Copyright (c) 2006 - 2008, Intel Corporation. <BR>
5 All rights reserved. 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
9
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.
12
13 **/
14
15
16 #include <DxeMain.h>
17
18
19 /**
20 Get the FFS file state by checking the highest bit set in the header's state field.
21
22 @param ErasePolarity Erase polarity attribute of the firmware volume
23 @param FfsHeader Points to the FFS file header
24
25 @return FFS File state
26
27 **/
28 EFI_FFS_FILE_STATE
29 GetFileState (
30 IN UINT8 ErasePolarity,
31 IN EFI_FFS_FILE_HEADER *FfsHeader
32 )
33 {
34 EFI_FFS_FILE_STATE FileState;
35 UINT8 HighestBit;
36
37 FileState = FfsHeader->State;
38
39 if (ErasePolarity != 0) {
40 FileState = (EFI_FFS_FILE_STATE)~FileState;
41 }
42
43 HighestBit = 0x80;
44 while (HighestBit != 0 && ((HighestBit & FileState) == 0)) {
45 HighestBit >>= 1;
46 }
47
48 return (EFI_FFS_FILE_STATE) HighestBit;
49 }
50
51
52
53 /**
54 Check if a block of buffer is erased.
55
56 @param ErasePolarity Erase polarity attribute of the firmware volume
57 @param InBuffer The buffer to be checked
58 @param BufferSize Size of the buffer in bytes
59
60 @retval TRUE The block of buffer is erased
61 @retval FALSE The block of buffer is not erased
62
63 **/
64 BOOLEAN
65 IsBufferErased (
66 IN UINT8 ErasePolarity,
67 IN VOID *InBuffer,
68 IN UINTN BufferSize
69 )
70 {
71 UINTN Count;
72 UINT8 EraseByte;
73 UINT8 *Buffer;
74
75 if(ErasePolarity == 1) {
76 EraseByte = 0xFF;
77 } else {
78 EraseByte = 0;
79 }
80
81 Buffer = InBuffer;
82 for (Count = 0; Count < BufferSize; Count++) {
83 if (Buffer[Count] != EraseByte) {
84 return FALSE;
85 }
86 }
87
88 return TRUE;
89 }
90
91
92
93 /**
94 Verify checksum of the firmware volume header.
95
96 @param FvHeader Points to the firmware volume header to be checked
97
98 @retval TRUE Checksum verification passed
99 @retval FALSE Checksum verification failed
100
101 **/
102 BOOLEAN
103 VerifyFvHeaderChecksum (
104 IN EFI_FIRMWARE_VOLUME_HEADER *FvHeader
105 )
106 {
107 UINT32 Index;
108 UINT32 HeaderLength;
109 UINT16 Checksum;
110 UINT16 *Ptr;
111
112 HeaderLength = FvHeader->HeaderLength;
113 Ptr = (UINT16 *)FvHeader;
114 Checksum = 0;
115
116 for (Index = 0; Index < HeaderLength / sizeof (UINT16); Index++) {
117 Checksum = (UINT16)(Checksum + Ptr[Index]);
118 }
119
120 if (Checksum == 0) {
121 return TRUE;
122 } else {
123 return FALSE;
124 }
125 }
126
127
128 /**
129 Verify checksum of the FFS file header.
130
131 @param FfsHeader Points to the FFS file header to be checked
132
133 @retval TRUE Checksum verification passed
134 @retval FALSE Checksum verification failed
135
136 **/
137 BOOLEAN
138 VerifyHeaderChecksum (
139 IN EFI_FFS_FILE_HEADER *FfsHeader
140 )
141 {
142 UINT32 Index;
143 UINT8 *Ptr;
144 UINT8 HeaderChecksum;
145
146 Ptr = (UINT8 *)FfsHeader;
147 HeaderChecksum = 0;
148 for (Index = 0; Index < sizeof(EFI_FFS_FILE_HEADER); Index++) {
149 HeaderChecksum = (UINT8)(HeaderChecksum + Ptr[Index]);
150 }
151
152 HeaderChecksum = (UINT8) (HeaderChecksum - FfsHeader->State - FfsHeader->IntegrityCheck.Checksum.File);
153
154 if (HeaderChecksum == 0) {
155 return TRUE;
156 } else {
157 return FALSE;
158 }
159 }
160
161
162
163 /**
164 Check if it's a valid FFS file header.
165
166 @param ErasePolarity Erase polarity attribute of the firmware volume
167 @param FfsHeader Points to the FFS file header to be checked
168 @param FileState FFS file state to be returned
169
170 @retval TRUE Valid FFS file header
171 @retval FALSE Invalid FFS file header
172
173 **/
174 BOOLEAN
175 IsValidFfsHeader (
176 IN UINT8 ErasePolarity,
177 IN EFI_FFS_FILE_HEADER *FfsHeader,
178 OUT EFI_FFS_FILE_STATE *FileState
179 )
180 {
181 *FileState = GetFileState (ErasePolarity, FfsHeader);
182
183 switch (*FileState) {
184 case EFI_FILE_HEADER_VALID:
185 case EFI_FILE_DATA_VALID:
186 case EFI_FILE_MARKED_FOR_UPDATE:
187 case EFI_FILE_DELETED:
188 //
189 // Here we need to verify header checksum
190 //
191 return VerifyHeaderChecksum (FfsHeader);
192
193 case EFI_FILE_HEADER_CONSTRUCTION:
194 case EFI_FILE_HEADER_INVALID:
195 default:
196 return FALSE;
197 }
198 }
199
200
201 /**
202 Check if it's a valid FFS file.
203 Here we are sure that it has a valid FFS file header since we must call IsValidFfsHeader() first.
204
205 @param ErasePolarity Erase polarity attribute of the firmware volume
206 @param FfsHeader Points to the FFS file to be checked
207
208 @retval TRUE Valid FFS file
209 @retval FALSE Invalid FFS file
210
211 **/
212 BOOLEAN
213 IsValidFfsFile (
214 IN UINT8 ErasePolarity,
215 IN EFI_FFS_FILE_HEADER *FfsHeader
216 )
217 {
218 EFI_FFS_FILE_STATE FileState;
219
220 FileState = GetFileState (ErasePolarity, FfsHeader);
221 switch (FileState) {
222
223 case EFI_FILE_DELETED:
224 case EFI_FILE_DATA_VALID:
225 case EFI_FILE_MARKED_FOR_UPDATE:
226 //
227 // Some other vliadation like file content checksum might be done here.
228 // For performance issue, Tiano only do FileState check.
229 //
230 return TRUE;
231
232 default:
233 return FALSE;
234 }
235 }
236
237