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