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