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