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