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