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