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