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