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