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