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