]>
Commit | Line | Data |
---|---|---|
30fdf114 | 1 | /** @file\r |
97fa0ee9 | 2 | These functions assist in parsing and manipulating a Firmware Volume.\r |
30fdf114 | 3 | \r |
6eff5d22 | 4 | Copyright (c) 2004 - 2015, Intel Corporation. All rights reserved.<BR>\r |
40d841f6 | 5 | This program and the accompanying materials \r |
30fdf114 LG |
6 | are licensed and made available under the terms and conditions of the BSD License \r |
7 | which accompanies this distribution. The full text of the license may be found at \r | |
8 | http://opensource.org/licenses/bsd-license.php \r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r | |
12 | \r | |
30fdf114 LG |
13 | **/\r |
14 | \r | |
15 | //\r | |
16 | // Include files\r | |
17 | //\r | |
18 | #include "FvLib.h"\r | |
19 | #include "CommonLib.h"\r | |
20 | #include "EfiUtilityMsgs.h"\r | |
21 | \r | |
22 | //\r | |
23 | // Module global variables\r | |
24 | //\r | |
25 | EFI_FIRMWARE_VOLUME_HEADER *mFvHeader = NULL;\r | |
26 | UINT32 mFvLength = 0;\r | |
27 | \r | |
28 | //\r | |
29 | // External function implementations\r | |
30 | //\r | |
31 | EFI_STATUS\r | |
32 | InitializeFvLib (\r | |
33 | IN VOID *Fv,\r | |
34 | IN UINT32 FvLength\r | |
35 | )\r | |
36 | /*++\r | |
37 | \r | |
38 | Routine Description:\r | |
39 | \r | |
40 | This initializes the FV lib with a pointer to the FV and length. It does not\r | |
41 | verify the FV in any way.\r | |
42 | \r | |
43 | Arguments:\r | |
44 | \r | |
45 | Fv Buffer containing the FV.\r | |
46 | FvLength Length of the FV\r | |
47 | \r | |
48 | Returns:\r | |
49 | \r | |
50 | EFI_SUCCESS Function Completed successfully.\r | |
51 | EFI_INVALID_PARAMETER A required parameter was NULL.\r | |
52 | \r | |
53 | --*/\r | |
54 | {\r | |
55 | //\r | |
56 | // Verify input arguments\r | |
57 | //\r | |
58 | if (Fv == NULL) {\r | |
59 | return EFI_INVALID_PARAMETER;\r | |
60 | }\r | |
61 | \r | |
62 | mFvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) Fv;\r | |
63 | mFvLength = FvLength;\r | |
64 | \r | |
65 | return EFI_SUCCESS;\r | |
66 | }\r | |
67 | \r | |
68 | EFI_STATUS\r | |
69 | GetFvHeader (\r | |
70 | OUT EFI_FIRMWARE_VOLUME_HEADER **FvHeader,\r | |
71 | OUT UINT32 *FvLength\r | |
72 | )\r | |
73 | /*++\r | |
74 | \r | |
75 | Routine Description:\r | |
76 | \r | |
77 | This function returns a pointer to the current FV and the size.\r | |
78 | \r | |
79 | Arguments:\r | |
80 | \r | |
81 | FvHeader Pointer to the FV buffer.\r | |
82 | FvLength Length of the FV\r | |
83 | \r | |
84 | Returns:\r | |
85 | \r | |
86 | EFI_SUCCESS Function Completed successfully.\r | |
87 | EFI_INVALID_PARAMETER A required parameter was NULL.\r | |
88 | EFI_ABORTED The library needs to be initialized.\r | |
89 | \r | |
90 | --*/\r | |
91 | {\r | |
92 | //\r | |
93 | // Verify library has been initialized.\r | |
94 | //\r | |
95 | if (mFvHeader == NULL || mFvLength == 0) {\r | |
96 | return EFI_ABORTED;\r | |
97 | }\r | |
98 | //\r | |
99 | // Verify input arguments\r | |
100 | //\r | |
101 | if (FvHeader == NULL) {\r | |
102 | return EFI_INVALID_PARAMETER;\r | |
103 | }\r | |
104 | \r | |
105 | *FvHeader = mFvHeader;\r | |
a709adfa | 106 | *FvLength = mFvLength;\r |
30fdf114 LG |
107 | return EFI_SUCCESS;\r |
108 | }\r | |
109 | \r | |
110 | EFI_STATUS\r | |
111 | GetNextFile (\r | |
112 | IN EFI_FFS_FILE_HEADER *CurrentFile,\r | |
113 | OUT EFI_FFS_FILE_HEADER **NextFile\r | |
114 | )\r | |
115 | /*++\r | |
116 | \r | |
117 | Routine Description:\r | |
118 | \r | |
119 | This function returns the next file. If the current file is NULL, it returns\r | |
120 | the first file in the FV. If the function returns EFI_SUCCESS and the file \r | |
121 | pointer is NULL, then there are no more files in the FV.\r | |
122 | \r | |
123 | Arguments:\r | |
124 | \r | |
125 | CurrentFile Pointer to the current file, must be within the current FV.\r | |
126 | NextFile Pointer to the next file in the FV.\r | |
127 | \r | |
128 | Returns:\r | |
129 | \r | |
130 | EFI_SUCCESS Function completed successfully.\r | |
131 | EFI_INVALID_PARAMETER A required parameter was NULL or is out of range.\r | |
132 | EFI_ABORTED The library needs to be initialized.\r | |
133 | \r | |
134 | --*/\r | |
135 | {\r | |
136 | EFI_STATUS Status;\r | |
137 | \r | |
138 | //\r | |
139 | // Verify library has been initialized.\r | |
140 | //\r | |
141 | if (mFvHeader == NULL || mFvLength == 0) {\r | |
142 | return EFI_ABORTED;\r | |
143 | }\r | |
144 | //\r | |
145 | // Verify input arguments\r | |
146 | //\r | |
147 | if (NextFile == NULL) {\r | |
148 | return EFI_INVALID_PARAMETER;\r | |
149 | }\r | |
150 | //\r | |
151 | // Verify FV header\r | |
152 | //\r | |
153 | Status = VerifyFv (mFvHeader);\r | |
154 | if (EFI_ERROR (Status)) {\r | |
155 | return EFI_ABORTED;\r | |
156 | }\r | |
157 | //\r | |
158 | // Get first file\r | |
159 | //\r | |
160 | if (CurrentFile == NULL) {\r | |
161 | CurrentFile = (EFI_FFS_FILE_HEADER *) ((UINTN) mFvHeader + mFvHeader->HeaderLength);\r | |
162 | \r | |
163 | //\r | |
164 | // Verify file is valid\r | |
165 | //\r | |
166 | Status = VerifyFfsFile (CurrentFile);\r | |
167 | if (EFI_ERROR (Status)) {\r | |
168 | //\r | |
169 | // no files in this FV\r | |
170 | //\r | |
171 | *NextFile = NULL;\r | |
172 | return EFI_SUCCESS;\r | |
173 | } else {\r | |
174 | //\r | |
175 | // Verify file is in this FV.\r | |
176 | //\r | |
e8a47801 | 177 | if ((UINTN) CurrentFile + GetFfsFileLength(CurrentFile) > (UINTN) mFvHeader + mFvLength) {\r |
30fdf114 LG |
178 | *NextFile = NULL;\r |
179 | return EFI_SUCCESS;\r | |
180 | }\r | |
181 | \r | |
182 | *NextFile = CurrentFile;\r | |
183 | return EFI_SUCCESS;\r | |
184 | }\r | |
185 | }\r | |
186 | //\r | |
187 | // Verify current file is in range\r | |
188 | //\r | |
189 | if (((UINTN) CurrentFile < (UINTN) mFvHeader + mFvHeader->HeaderLength) ||\r | |
e8a47801 | 190 | ((UINTN) CurrentFile + GetFfsFileLength(CurrentFile) > (UINTN) mFvHeader + mFvLength)\r |
30fdf114 LG |
191 | ) {\r |
192 | return EFI_INVALID_PARAMETER;\r | |
193 | }\r | |
194 | //\r | |
195 | // Get next file, compensate for 8 byte alignment if necessary.\r | |
196 | //\r | |
d3bb7118 | 197 | *NextFile = (EFI_FFS_FILE_HEADER *) ((((UINTN) CurrentFile - (UINTN) mFvHeader + GetFfsFileLength(CurrentFile) + 0x07) & (~(UINTN) 7)) + (UINT8 *) mFvHeader);\r |
30fdf114 LG |
198 | \r |
199 | //\r | |
200 | // Verify file is in this FV.\r | |
201 | //\r | |
e8a47801 LG |
202 | if (((UINTN) *NextFile + GetFfsHeaderLength(*NextFile) >= (UINTN) mFvHeader + mFvLength) ||\r |
203 | ((UINTN) *NextFile + GetFfsFileLength (*NextFile) > (UINTN) mFvHeader + mFvLength)\r | |
30fdf114 LG |
204 | ) {\r |
205 | *NextFile = NULL;\r | |
206 | return EFI_SUCCESS;\r | |
207 | }\r | |
208 | //\r | |
209 | // Verify file is valid\r | |
210 | //\r | |
211 | Status = VerifyFfsFile (*NextFile);\r | |
212 | if (EFI_ERROR (Status)) {\r | |
213 | //\r | |
214 | // no more files in this FV\r | |
215 | //\r | |
216 | *NextFile = NULL;\r | |
217 | return EFI_SUCCESS;\r | |
218 | }\r | |
219 | \r | |
220 | return EFI_SUCCESS;\r | |
221 | }\r | |
222 | \r | |
223 | EFI_STATUS\r | |
224 | GetFileByName (\r | |
225 | IN EFI_GUID *FileName,\r | |
226 | OUT EFI_FFS_FILE_HEADER **File\r | |
227 | )\r | |
228 | /*++\r | |
229 | \r | |
230 | Routine Description:\r | |
231 | \r | |
232 | Find a file by name. The function will return NULL if the file is not found.\r | |
233 | \r | |
234 | Arguments:\r | |
235 | \r | |
236 | FileName The GUID file name of the file to search for.\r | |
237 | File Return pointer. In the case of an error, contents are undefined.\r | |
238 | \r | |
239 | Returns:\r | |
240 | \r | |
241 | EFI_SUCCESS The function completed successfully.\r | |
242 | EFI_ABORTED An error was encountered.\r | |
243 | EFI_INVALID_PARAMETER One of the parameters was NULL.\r | |
244 | \r | |
245 | --*/\r | |
246 | {\r | |
247 | EFI_FFS_FILE_HEADER *CurrentFile;\r | |
248 | EFI_STATUS Status;\r | |
249 | CHAR8 FileGuidString[80];\r | |
250 | \r | |
251 | //\r | |
252 | // Verify library has been initialized.\r | |
253 | //\r | |
254 | if (mFvHeader == NULL || mFvLength == 0) {\r | |
255 | return EFI_ABORTED;\r | |
256 | }\r | |
257 | //\r | |
258 | // Verify input parameters\r | |
259 | //\r | |
260 | if (FileName == NULL || File == NULL) {\r | |
261 | return EFI_INVALID_PARAMETER;\r | |
262 | }\r | |
263 | //\r | |
264 | // File Guid String Name\r | |
265 | //\r | |
266 | PrintGuidToBuffer (FileName, (UINT8 *)FileGuidString, sizeof (FileGuidString), TRUE);\r | |
267 | //\r | |
268 | // Verify FV header\r | |
269 | //\r | |
270 | Status = VerifyFv (mFvHeader);\r | |
271 | if (EFI_ERROR (Status)) {\r | |
272 | return EFI_ABORTED;\r | |
273 | }\r | |
274 | //\r | |
275 | // Get the first file\r | |
276 | //\r | |
277 | Status = GetNextFile (NULL, &CurrentFile);\r | |
278 | if (EFI_ERROR (Status)) {\r | |
279 | Error (NULL, 0, 0003, "error parsing FV image", "FFS file with Guid %s can't be found", FileGuidString);\r | |
280 | return EFI_ABORTED;\r | |
281 | }\r | |
282 | //\r | |
283 | // Loop as long as we have a valid file\r | |
284 | //\r | |
285 | while (CurrentFile) {\r | |
286 | if (!CompareGuid (&CurrentFile->Name, FileName)) {\r | |
287 | *File = CurrentFile;\r | |
288 | return EFI_SUCCESS;\r | |
289 | }\r | |
290 | \r | |
291 | Status = GetNextFile (CurrentFile, &CurrentFile);\r | |
292 | if (EFI_ERROR (Status)) {\r | |
293 | Error (NULL, 0, 0003, "error parsing FV image", "FFS file with Guid %s can't be found", FileGuidString);\r | |
294 | return EFI_ABORTED;\r | |
295 | }\r | |
296 | }\r | |
297 | //\r | |
298 | // File not found in this FV.\r | |
299 | //\r | |
300 | *File = NULL;\r | |
301 | return EFI_SUCCESS;\r | |
302 | }\r | |
303 | \r | |
304 | EFI_STATUS\r | |
305 | GetFileByType (\r | |
306 | IN EFI_FV_FILETYPE FileType,\r | |
307 | IN UINTN Instance,\r | |
308 | OUT EFI_FFS_FILE_HEADER **File\r | |
309 | )\r | |
310 | /*++\r | |
311 | \r | |
312 | Routine Description:\r | |
313 | \r | |
314 | Find a file by type and instance. An instance of 1 is the first instance.\r | |
315 | The function will return NULL if a matching file cannot be found.\r | |
316 | File type EFI_FV_FILETYPE_ALL means any file type is valid.\r | |
317 | \r | |
318 | Arguments:\r | |
319 | \r | |
320 | FileType Type of file to search for.\r | |
321 | Instance Instace of the file type to return.\r | |
322 | File Return pointer. In the case of an error, contents are undefined.\r | |
323 | \r | |
324 | Returns:\r | |
325 | \r | |
326 | EFI_SUCCESS The function completed successfully.\r | |
327 | EFI_ABORTED An error was encountered.\r | |
328 | EFI_INVALID_PARAMETER One of the parameters was NULL.\r | |
329 | \r | |
330 | --*/\r | |
331 | {\r | |
332 | EFI_FFS_FILE_HEADER *CurrentFile;\r | |
333 | EFI_STATUS Status;\r | |
334 | UINTN FileCount;\r | |
335 | \r | |
336 | //\r | |
337 | // Verify library has been initialized.\r | |
338 | //\r | |
339 | if (mFvHeader == NULL || mFvLength == 0) {\r | |
340 | return EFI_ABORTED;\r | |
341 | }\r | |
342 | //\r | |
343 | // Verify input parameters\r | |
344 | //\r | |
345 | if (File == NULL) {\r | |
346 | return EFI_INVALID_PARAMETER;\r | |
347 | }\r | |
348 | //\r | |
349 | // Verify FV header\r | |
350 | //\r | |
351 | Status = VerifyFv (mFvHeader);\r | |
352 | if (EFI_ERROR (Status)) {\r | |
353 | return EFI_ABORTED;\r | |
354 | }\r | |
355 | //\r | |
356 | // Initialize the number of matching files found.\r | |
357 | //\r | |
358 | FileCount = 0;\r | |
359 | \r | |
360 | //\r | |
361 | // Get the first file\r | |
362 | //\r | |
363 | Status = GetNextFile (NULL, &CurrentFile);\r | |
364 | if (EFI_ERROR (Status)) {\r | |
365 | Error (NULL, 0, 0003, "error parsing FV image", "FFS file with FileType 0x%x can't be found", FileType);\r | |
366 | return EFI_ABORTED;\r | |
367 | }\r | |
368 | //\r | |
369 | // Loop as long as we have a valid file\r | |
370 | //\r | |
371 | while (CurrentFile) {\r | |
372 | if (FileType == EFI_FV_FILETYPE_ALL || CurrentFile->Type == FileType) {\r | |
373 | FileCount++;\r | |
374 | }\r | |
375 | \r | |
376 | if (FileCount == Instance) {\r | |
377 | *File = CurrentFile;\r | |
378 | return EFI_SUCCESS;\r | |
379 | }\r | |
380 | \r | |
381 | Status = GetNextFile (CurrentFile, &CurrentFile);\r | |
382 | if (EFI_ERROR (Status)) {\r | |
383 | Error (NULL, 0, 0003, "error parsing FV image", "FFS file with FileType 0x%x can't be found", FileType);\r | |
384 | return EFI_ABORTED;\r | |
385 | }\r | |
386 | }\r | |
387 | \r | |
388 | *File = NULL;\r | |
389 | return EFI_SUCCESS;\r | |
390 | }\r | |
391 | \r | |
392 | EFI_STATUS\r | |
393 | SearchSectionByType (\r | |
394 | IN EFI_FILE_SECTION_POINTER FirstSection,\r | |
395 | IN UINT8 *SearchEnd,\r | |
396 | IN EFI_SECTION_TYPE SectionType,\r | |
397 | IN OUT UINTN *StartIndex,\r | |
398 | IN UINTN Instance,\r | |
399 | OUT EFI_FILE_SECTION_POINTER *Section\r | |
400 | )\r | |
401 | /*++\r | |
402 | \r | |
403 | Routine Description:\r | |
404 | \r | |
405 | Helper function to search a sequence of sections from the section pointed\r | |
406 | by FirstSection to SearchEnd for the Instance-th section of type SectionType.\r | |
407 | The current counter is saved in StartIndex and when the section is found, it's\r | |
408 | saved in Section. GUID-defined sections, if special processing is not required,\r | |
409 | are searched recursively in a depth-first manner.\r | |
410 | \r | |
411 | Arguments:\r | |
412 | \r | |
413 | FirstSection The first section to start searching from.\r | |
414 | SearchEnd The end address to stop search.\r | |
415 | SectionType The type of section to search.\r | |
416 | StartIndex The current counter is saved.\r | |
417 | Instance The requested n-th section number.\r | |
418 | Section The found section returned.\r | |
419 | \r | |
420 | Returns:\r | |
421 | \r | |
422 | EFI_SUCCESS The function completed successfully.\r | |
423 | EFI_NOT_FOUND The section is not found.\r | |
424 | --*/\r | |
425 | {\r | |
426 | EFI_FILE_SECTION_POINTER CurrentSection;\r | |
427 | EFI_FILE_SECTION_POINTER InnerSection;\r | |
428 | EFI_STATUS Status;\r | |
429 | UINTN SectionSize;\r | |
e8a47801 LG |
430 | UINT16 GuidSecAttr;\r |
431 | UINT16 GuidDataOffset;\r | |
30fdf114 | 432 | \r |
e8a47801 LG |
433 | GuidSecAttr = 0;\r |
434 | GuidDataOffset = 0;\r | |
30fdf114 LG |
435 | CurrentSection = FirstSection;\r |
436 | \r | |
437 | while ((UINTN) CurrentSection.CommonHeader < (UINTN) SearchEnd) {\r | |
438 | if (CurrentSection.CommonHeader->Type == SectionType) {\r | |
439 | (*StartIndex)++;\r | |
440 | }\r | |
441 | \r | |
442 | if (*StartIndex == Instance) {\r | |
443 | *Section = CurrentSection;\r | |
444 | return EFI_SUCCESS;\r | |
445 | }\r | |
446 | //\r | |
447 | // If the requesting section is not GUID-defined and\r | |
448 | // we find a GUID-defined section that doesn't need\r | |
449 | // special processing, go ahead to search the requesting\r | |
450 | // section inside the GUID-defined section.\r | |
451 | //\r | |
e8a47801 LG |
452 | if (CurrentSection.CommonHeader->Type == EFI_SECTION_GUID_DEFINED) {\r |
453 | if (GetLength(CurrentSection.CommonHeader->Size) == 0xffffff) {\r | |
454 | GuidSecAttr = CurrentSection.GuidDefinedSection2->Attributes;\r | |
455 | GuidDataOffset = CurrentSection.GuidDefinedSection2->DataOffset;\r | |
456 | } else {\r | |
457 | GuidSecAttr = CurrentSection.GuidDefinedSection->Attributes;\r | |
458 | GuidDataOffset = CurrentSection.GuidDefinedSection->DataOffset;\r | |
459 | }\r | |
460 | }\r | |
30fdf114 LG |
461 | if (SectionType != EFI_SECTION_GUID_DEFINED &&\r |
462 | CurrentSection.CommonHeader->Type == EFI_SECTION_GUID_DEFINED &&\r | |
e8a47801 | 463 | !(GuidSecAttr & EFI_GUIDED_SECTION_PROCESSING_REQUIRED)) {\r |
30fdf114 | 464 | InnerSection.CommonHeader = (EFI_COMMON_SECTION_HEADER *)\r |
e8a47801 LG |
465 | ((UINTN) CurrentSection.CommonHeader + GuidDataOffset);\r |
466 | SectionSize = GetSectionFileLength(CurrentSection.CommonHeader);\r | |
30fdf114 LG |
467 | Status = SearchSectionByType (\r |
468 | InnerSection,\r | |
469 | (UINT8 *) ((UINTN) CurrentSection.CommonHeader + SectionSize),\r | |
470 | SectionType,\r | |
471 | StartIndex,\r | |
472 | Instance,\r | |
473 | Section\r | |
474 | );\r | |
475 | if (!EFI_ERROR (Status)) {\r | |
476 | return EFI_SUCCESS;\r | |
477 | }\r | |
478 | }\r | |
479 | //\r | |
480 | // Find next section (including compensating for alignment issues.\r | |
481 | //\r | |
d3bb7118 | 482 | CurrentSection.CommonHeader = (EFI_COMMON_SECTION_HEADER *) ((((UINTN) CurrentSection.CommonHeader) + GetSectionFileLength(CurrentSection.CommonHeader) + 0x03) & (~(UINTN) 3));\r |
30fdf114 LG |
483 | }\r |
484 | \r | |
485 | return EFI_NOT_FOUND;\r | |
486 | }\r | |
487 | \r | |
488 | EFI_STATUS\r | |
489 | GetSectionByType (\r | |
490 | IN EFI_FFS_FILE_HEADER *File,\r | |
491 | IN EFI_SECTION_TYPE SectionType,\r | |
492 | IN UINTN Instance,\r | |
493 | OUT EFI_FILE_SECTION_POINTER *Section\r | |
494 | )\r | |
495 | /*++\r | |
496 | \r | |
497 | Routine Description:\r | |
498 | \r | |
499 | Find a section in a file by type and instance. An instance of 1 is the first \r | |
500 | instance. The function will return NULL if a matching section cannot be found.\r | |
501 | GUID-defined sections, if special processing is not needed, are handled in a\r | |
502 | depth-first manner.\r | |
503 | \r | |
504 | Arguments:\r | |
505 | \r | |
506 | File The file to search.\r | |
507 | SectionType Type of file to search for.\r | |
508 | Instance Instace of the section to return.\r | |
509 | Section Return pointer. In the case of an error, contents are undefined.\r | |
510 | \r | |
511 | Returns:\r | |
512 | \r | |
513 | EFI_SUCCESS The function completed successfully.\r | |
514 | EFI_ABORTED An error was encountered.\r | |
515 | EFI_INVALID_PARAMETER One of the parameters was NULL.\r | |
516 | EFI_NOT_FOUND No found.\r | |
517 | --*/\r | |
518 | {\r | |
519 | EFI_FILE_SECTION_POINTER CurrentSection;\r | |
520 | EFI_STATUS Status;\r | |
521 | UINTN SectionCount;\r | |
522 | \r | |
523 | //\r | |
524 | // Verify input parameters\r | |
525 | //\r | |
526 | if (File == NULL || Instance == 0) {\r | |
527 | return EFI_INVALID_PARAMETER;\r | |
528 | }\r | |
529 | //\r | |
530 | // Verify FFS header\r | |
531 | //\r | |
532 | Status = VerifyFfsFile (File);\r | |
533 | if (EFI_ERROR (Status)) {\r | |
534 | Error (NULL, 0, 0006, "invalid FFS file", NULL);\r | |
535 | return EFI_ABORTED;\r | |
536 | }\r | |
537 | //\r | |
538 | // Initialize the number of matching sections found.\r | |
539 | //\r | |
540 | SectionCount = 0;\r | |
541 | \r | |
542 | //\r | |
543 | // Get the first section\r | |
544 | //\r | |
e8a47801 | 545 | CurrentSection.CommonHeader = (EFI_COMMON_SECTION_HEADER *) ((UINTN) File + GetFfsHeaderLength(File));\r |
30fdf114 LG |
546 | \r |
547 | //\r | |
548 | // Depth-first manner to find section file.\r | |
549 | //\r | |
550 | Status = SearchSectionByType (\r | |
551 | CurrentSection,\r | |
e8a47801 | 552 | (UINT8 *) ((UINTN) File + GetFfsFileLength (File)),\r |
30fdf114 LG |
553 | SectionType,\r |
554 | &SectionCount,\r | |
555 | Instance,\r | |
556 | Section\r | |
557 | );\r | |
558 | \r | |
559 | if (!EFI_ERROR (Status)) {\r | |
560 | return EFI_SUCCESS;\r | |
561 | } else {\r | |
562 | //\r | |
563 | // Section not found\r | |
564 | //\r | |
565 | (*Section).Code16Section = NULL;\r | |
566 | return EFI_NOT_FOUND;\r | |
567 | }\r | |
568 | }\r | |
569 | //\r | |
570 | // will not parse compressed sections\r | |
571 | //\r | |
572 | EFI_STATUS\r | |
573 | VerifyFv (\r | |
574 | IN EFI_FIRMWARE_VOLUME_HEADER *FvHeader\r | |
575 | )\r | |
576 | /*++\r | |
577 | \r | |
578 | Routine Description:\r | |
579 | \r | |
580 | Verify the current pointer points to a valid FV header.\r | |
581 | \r | |
582 | Arguments:\r | |
583 | \r | |
584 | FvHeader Pointer to an alleged FV file.\r | |
585 | \r | |
586 | Returns:\r | |
587 | \r | |
588 | EFI_SUCCESS The FV header is valid.\r | |
589 | EFI_VOLUME_CORRUPTED The FV header is not valid.\r | |
590 | EFI_INVALID_PARAMETER A required parameter was NULL.\r | |
591 | EFI_ABORTED Operation aborted.\r | |
592 | \r | |
593 | --*/\r | |
594 | {\r | |
595 | UINT16 Checksum;\r | |
596 | \r | |
597 | //\r | |
598 | // Verify input parameters\r | |
599 | //\r | |
600 | if (FvHeader == NULL) {\r | |
601 | return EFI_INVALID_PARAMETER;\r | |
602 | }\r | |
603 | \r | |
604 | if (FvHeader->Signature != EFI_FVH_SIGNATURE) {\r | |
605 | Error (NULL, 0, 0006, "invalid FV header signature", NULL);\r | |
606 | return EFI_VOLUME_CORRUPTED;\r | |
607 | }\r | |
608 | //\r | |
609 | // Verify header checksum\r | |
610 | //\r | |
611 | Checksum = CalculateSum16 ((UINT16 *) FvHeader, FvHeader->HeaderLength / sizeof (UINT16));\r | |
612 | \r | |
613 | if (Checksum != 0) {\r | |
614 | Error (NULL, 0, 0006, "invalid FV header checksum", NULL);\r | |
615 | return EFI_ABORTED;\r | |
616 | }\r | |
617 | \r | |
618 | return EFI_SUCCESS;\r | |
619 | }\r | |
620 | \r | |
621 | EFI_STATUS\r | |
622 | VerifyFfsFile (\r | |
623 | IN EFI_FFS_FILE_HEADER *FfsHeader\r | |
624 | )\r | |
625 | /*++\r | |
626 | \r | |
627 | Routine Description:\r | |
628 | \r | |
629 | Verify the current pointer points to a FFS file header.\r | |
630 | \r | |
631 | Arguments:\r | |
632 | \r | |
633 | FfsHeader Pointer to an alleged FFS file.\r | |
634 | \r | |
635 | Returns:\r | |
636 | \r | |
637 | EFI_SUCCESS The Ffs header is valid.\r | |
638 | EFI_NOT_FOUND This "file" is the beginning of free space.\r | |
639 | EFI_VOLUME_CORRUPTED The Ffs header is not valid.\r | |
640 | EFI_ABORTED The erase polarity is not known.\r | |
641 | \r | |
642 | --*/\r | |
643 | {\r | |
644 | BOOLEAN ErasePolarity;\r | |
645 | EFI_STATUS Status;\r | |
e8a47801 | 646 | EFI_FFS_FILE_HEADER2 BlankHeader;\r |
30fdf114 LG |
647 | UINT8 Checksum;\r |
648 | UINT32 FileLength;\r | |
649 | UINT8 SavedChecksum;\r | |
650 | UINT8 SavedState;\r | |
651 | UINT8 FileGuidString[80];\r | |
e8a47801 LG |
652 | UINT32 FfsHeaderSize;\r |
653 | \r | |
30fdf114 LG |
654 | //\r |
655 | // Verify library has been initialized.\r | |
656 | //\r | |
657 | if (mFvHeader == NULL || mFvLength == 0) {\r | |
658 | return EFI_ABORTED;\r | |
659 | }\r | |
660 | //\r | |
661 | // Verify FV header\r | |
662 | //\r | |
663 | Status = VerifyFv (mFvHeader);\r | |
664 | if (EFI_ERROR (Status)) {\r | |
665 | return EFI_ABORTED;\r | |
666 | }\r | |
667 | //\r | |
668 | // Get the erase polarity.\r | |
669 | //\r | |
670 | Status = GetErasePolarity (&ErasePolarity);\r | |
671 | if (EFI_ERROR (Status)) {\r | |
672 | return EFI_ABORTED;\r | |
673 | }\r | |
e8a47801 LG |
674 | \r |
675 | FfsHeaderSize = GetFfsHeaderLength(FfsHeader);\r | |
30fdf114 LG |
676 | //\r |
677 | // Check if we have free space\r | |
678 | //\r | |
679 | if (ErasePolarity) {\r | |
e8a47801 | 680 | memset (&BlankHeader, -1, FfsHeaderSize);\r |
30fdf114 | 681 | } else {\r |
e8a47801 | 682 | memset (&BlankHeader, 0, FfsHeaderSize);\r |
30fdf114 LG |
683 | }\r |
684 | \r | |
e8a47801 | 685 | if (memcmp (&BlankHeader, FfsHeader, FfsHeaderSize) == 0) {\r |
30fdf114 LG |
686 | return EFI_NOT_FOUND;\r |
687 | }\r | |
688 | //\r | |
689 | // Convert the GUID to a string so we can at least report which file\r | |
690 | // if we find an error.\r | |
691 | //\r | |
692 | PrintGuidToBuffer (&FfsHeader->Name, FileGuidString, sizeof (FileGuidString), TRUE);\r | |
693 | //\r | |
694 | // Verify file header checksum\r | |
695 | //\r | |
696 | SavedState = FfsHeader->State;\r | |
697 | FfsHeader->State = 0;\r | |
698 | SavedChecksum = FfsHeader->IntegrityCheck.Checksum.File;\r | |
699 | FfsHeader->IntegrityCheck.Checksum.File = 0;\r | |
e8a47801 | 700 | Checksum = CalculateSum8 ((UINT8 *) FfsHeader, FfsHeaderSize);\r |
30fdf114 LG |
701 | FfsHeader->State = SavedState;\r |
702 | FfsHeader->IntegrityCheck.Checksum.File = SavedChecksum;\r | |
703 | if (Checksum != 0) {\r | |
704 | Error (NULL, 0, 0006, "invalid FFS file header checksum", "Ffs file with Guid %s", FileGuidString);\r | |
705 | return EFI_ABORTED;\r | |
706 | }\r | |
707 | //\r | |
708 | // Verify file checksum\r | |
709 | //\r | |
710 | if (FfsHeader->Attributes & FFS_ATTRIB_CHECKSUM) {\r | |
711 | //\r | |
712 | // Verify file data checksum\r | |
713 | //\r | |
e8a47801 LG |
714 | FileLength = GetFfsFileLength (FfsHeader);\r |
715 | Checksum = CalculateSum8 ((UINT8 *) ((UINT8 *)FfsHeader + FfsHeaderSize), FileLength - FfsHeaderSize);\r | |
b303ea72 | 716 | Checksum = Checksum + FfsHeader->IntegrityCheck.Checksum.File;\r |
30fdf114 LG |
717 | if (Checksum != 0) {\r |
718 | Error (NULL, 0, 0006, "invalid FFS file checksum", "Ffs file with Guid %s", FileGuidString);\r | |
719 | return EFI_ABORTED;\r | |
720 | }\r | |
721 | } else {\r | |
722 | //\r | |
723 | // File does not have a checksum\r | |
b303ea72 | 724 | // Verify contents are 0xAA as spec'd\r |
30fdf114 LG |
725 | //\r |
726 | if (FfsHeader->IntegrityCheck.Checksum.File != FFS_FIXED_CHECKSUM) {\r | |
727 | Error (NULL, 0, 0006, "invalid fixed FFS file header checksum", "Ffs file with Guid %s", FileGuidString);\r | |
728 | return EFI_ABORTED;\r | |
729 | }\r | |
730 | }\r | |
731 | \r | |
732 | return EFI_SUCCESS;\r | |
733 | }\r | |
734 | \r | |
e8a47801 LG |
735 | UINT32\r |
736 | GetFfsHeaderLength(\r | |
737 | IN EFI_FFS_FILE_HEADER *FfsHeader\r | |
738 | )\r | |
739 | {\r | |
740 | if (FfsHeader == NULL) {\r | |
741 | return 0;\r | |
742 | }\r | |
743 | if (FfsHeader->Attributes & FFS_ATTRIB_LARGE_FILE) {\r | |
744 | return sizeof(EFI_FFS_FILE_HEADER2);\r | |
745 | }\r | |
746 | return sizeof(EFI_FFS_FILE_HEADER);\r | |
747 | }\r | |
748 | \r | |
749 | UINT32\r | |
750 | GetSectionHeaderLength(\r | |
751 | IN EFI_COMMON_SECTION_HEADER *SectionHeader\r | |
752 | )\r | |
753 | {\r | |
754 | if (SectionHeader == NULL) {\r | |
755 | return 0;\r | |
756 | }\r | |
757 | if (GetLength(SectionHeader->Size) == 0xffffff) {\r | |
758 | return sizeof(EFI_COMMON_SECTION_HEADER2);\r | |
759 | }\r | |
760 | return sizeof(EFI_COMMON_SECTION_HEADER);\r | |
761 | }\r | |
762 | \r | |
763 | UINT32\r | |
764 | GetFfsFileLength (\r | |
765 | EFI_FFS_FILE_HEADER *FfsHeader\r | |
766 | )\r | |
767 | /*++\r | |
768 | \r | |
769 | Routine Description:\r | |
770 | \r | |
771 | Get FFS file length including FFS header.\r | |
772 | \r | |
773 | Arguments:\r | |
774 | \r | |
775 | FfsHeader Pointer to EFI_FFS_FILE_HEADER.\r | |
776 | \r | |
777 | Returns:\r | |
778 | \r | |
779 | UINT32 Length of FFS file header.\r | |
780 | \r | |
781 | --*/\r | |
782 | {\r | |
783 | if (FfsHeader == NULL) {\r | |
784 | return 0;\r | |
785 | }\r | |
786 | if (FfsHeader->Attributes & FFS_ATTRIB_LARGE_FILE) {\r | |
6eff5d22 | 787 | return (UINT32) ((EFI_FFS_FILE_HEADER2 *)FfsHeader)->ExtendedSize;\r |
e8a47801 LG |
788 | } else {\r |
789 | return GetLength(FfsHeader->Size);\r | |
790 | }\r | |
791 | }\r | |
792 | \r | |
793 | UINT32\r | |
794 | GetSectionFileLength (\r | |
795 | EFI_COMMON_SECTION_HEADER *SectionHeader\r | |
796 | )\r | |
797 | {\r | |
798 | UINT32 Length;\r | |
799 | if (SectionHeader == NULL) {\r | |
800 | return 0;\r | |
801 | }\r | |
802 | Length = GetLength(SectionHeader->Size);\r | |
803 | if (Length == 0xffffff) {\r | |
804 | Length = ((EFI_COMMON_SECTION_HEADER2 *)SectionHeader)->ExtendedSize;\r | |
805 | }\r | |
806 | return Length;\r | |
807 | }\r | |
808 | \r | |
30fdf114 LG |
809 | UINT32\r |
810 | GetLength (\r | |
811 | UINT8 *ThreeByteLength\r | |
812 | )\r | |
813 | /*++\r | |
814 | \r | |
815 | Routine Description:\r | |
816 | \r | |
817 | Converts a three byte length value into a UINT32.\r | |
818 | \r | |
819 | Arguments:\r | |
820 | \r | |
821 | ThreeByteLength Pointer to the first of the 3 byte length.\r | |
822 | \r | |
823 | Returns:\r | |
824 | \r | |
825 | UINT32 Size of the section\r | |
826 | \r | |
827 | --*/\r | |
828 | {\r | |
829 | UINT32 Length;\r | |
830 | \r | |
831 | if (ThreeByteLength == NULL) {\r | |
832 | return 0;\r | |
833 | }\r | |
834 | \r | |
835 | Length = *((UINT32 *) ThreeByteLength);\r | |
836 | Length = Length & 0x00FFFFFF;\r | |
837 | \r | |
838 | return Length;\r | |
839 | }\r | |
840 | \r | |
841 | EFI_STATUS\r | |
842 | GetErasePolarity (\r | |
843 | OUT BOOLEAN *ErasePolarity\r | |
844 | )\r | |
845 | /*++\r | |
846 | \r | |
847 | Routine Description:\r | |
848 | \r | |
849 | This function returns with the FV erase polarity. If the erase polarity\r | |
850 | for a bit is 1, the function return TRUE.\r | |
851 | \r | |
852 | Arguments:\r | |
853 | \r | |
854 | ErasePolarity A pointer to the erase polarity.\r | |
855 | \r | |
856 | Returns:\r | |
857 | \r | |
858 | EFI_SUCCESS The function completed successfully.\r | |
859 | EFI_INVALID_PARAMETER One of the input parameters was invalid.\r | |
860 | EFI_ABORTED Operation aborted.\r | |
861 | \r | |
862 | --*/\r | |
863 | {\r | |
864 | EFI_STATUS Status;\r | |
865 | \r | |
866 | //\r | |
867 | // Verify library has been initialized.\r | |
868 | //\r | |
869 | if (mFvHeader == NULL || mFvLength == 0) {\r | |
870 | return EFI_ABORTED;\r | |
871 | }\r | |
872 | //\r | |
873 | // Verify FV header\r | |
874 | //\r | |
875 | Status = VerifyFv (mFvHeader);\r | |
876 | if (EFI_ERROR (Status)) {\r | |
877 | return EFI_ABORTED;\r | |
878 | }\r | |
879 | //\r | |
880 | // Verify input parameters.\r | |
881 | //\r | |
882 | if (ErasePolarity == NULL) {\r | |
883 | return EFI_INVALID_PARAMETER;\r | |
884 | }\r | |
885 | \r | |
886 | if (mFvHeader->Attributes & EFI_FVB2_ERASE_POLARITY) {\r | |
887 | *ErasePolarity = TRUE;\r | |
888 | } else {\r | |
889 | *ErasePolarity = FALSE;\r | |
890 | }\r | |
891 | \r | |
892 | return EFI_SUCCESS;\r | |
893 | }\r | |
894 | \r | |
895 | UINT8\r | |
896 | GetFileState (\r | |
897 | IN BOOLEAN ErasePolarity,\r | |
898 | IN EFI_FFS_FILE_HEADER *FfsHeader\r | |
899 | )\r | |
900 | /*++\r | |
901 | \r | |
902 | Routine Description:\r | |
903 | \r | |
904 | This function returns a the highest state bit in the FFS that is set.\r | |
905 | It in no way validate the FFS file.\r | |
906 | \r | |
907 | Arguments:\r | |
908 | \r | |
909 | ErasePolarity The erase polarity for the file state bits.\r | |
910 | FfsHeader Pointer to a FFS file.\r | |
911 | \r | |
912 | Returns:\r | |
913 | \r | |
914 | UINT8 The hightest set state of the file.\r | |
915 | \r | |
916 | --*/\r | |
917 | {\r | |
918 | UINT8 FileState;\r | |
919 | UINT8 HighestBit;\r | |
920 | \r | |
921 | FileState = FfsHeader->State;\r | |
922 | \r | |
923 | if (ErasePolarity) {\r | |
924 | FileState = (UINT8)~FileState;\r | |
925 | }\r | |
926 | \r | |
927 | HighestBit = 0x80;\r | |
928 | while (HighestBit != 0 && (HighestBit & FileState) == 0) {\r | |
929 | HighestBit >>= 1;\r | |
930 | }\r | |
931 | \r | |
932 | return HighestBit;\r | |
933 | }\r |