2 The tool dumps the contents of a firmware volume
4 Copyright (c) 1999 - 2017, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
27 #include <Common/UefiBaseTypes.h>
28 #include <Common/UefiCapsule.h>
29 #include <Common/PiFirmwareFile.h>
30 #include <Common/PiFirmwareVolume.h>
31 #include <Guid/PiFirmwareFileSystem.h>
32 #include <IndustryStandard/PeImage.h>
33 #include <Protocol/GuidedSectionExtraction.h>
36 #include "Decompress.h"
38 #include "CommonLib.h"
39 #include "EfiUtilityMsgs.h"
40 #include "FirmwareVolumeBufferLib.h"
42 #include "ParseGuidedSectionTools.h"
43 #include "StringFuncs.h"
45 #include "PeCoffLib.h"
48 // Utility global variables
51 EFI_GUID gEfiCrc32GuidedSectionExtractionProtocolGuid
= EFI_CRC32_GUIDED_SECTION_EXTRACTION_PROTOCOL_GUID
;
53 #define UTILITY_MAJOR_VERSION 1
54 #define UTILITY_MINOR_VERSION 0
56 #define UTILITY_NAME "VolInfo"
58 #define EFI_SECTION_ERROR EFIERR (100)
60 #define MAX_BASENAME_LEN 60 // not good to hardcode, but let's be reasonable
63 // Structure to keep a list of guid-to-basenames
65 typedef struct _GUID_TO_BASENAME
{
66 struct _GUID_TO_BASENAME
*Next
;
67 INT8 Guid
[PRINTED_GUID_BUFFER_SIZE
];
68 INT8 BaseName
[MAX_BASENAME_LEN
];
71 static GUID_TO_BASENAME
*mGuidBaseNameList
= NULL
;
74 // Store GUIDed Section guid->tool mapping
76 EFI_HANDLE mParsedGuidedSectionTools
= NULL
;
78 CHAR8
* mUtilityFilename
= NULL
;
80 BOOLEAN EnableHash
= FALSE
;
81 CHAR8
*OpenSslPath
= NULL
;
84 ParseGuidBaseNameFile (
89 FreeGuidBaseNameList (
100 IN UINT8
*SectionBuffer
,
101 IN UINT32 BufferLength
107 IN UINT32 SectionLength
115 OUT BOOLEAN
*ErasePolarity
121 EFI_FIRMWARE_VOLUME_HEADER
*FvImage
,
122 EFI_FFS_FILE_HEADER
*FileHeader
,
123 BOOLEAN ErasePolarity
135 LoadGuidedSectionToolsTxt (
136 IN CHAR8
* FirmwareVolumeFilename
141 IN CHAR8
* DefaultPath
,
142 IN CHAR8
* AppendPath
,
159 Returns the length of a null-terminated unicode string.
163 String - The pointer to a null-terminated unicode string.
173 for (Length
= 0; *String
!= L
'\0'; String
++, Length
++) {
180 Unicode2AsciiString (
182 OUT CHAR8
*Destination
188 Convert a null-terminated unicode string to a null-terminated ascii string.
192 Source - The pointer to the null-terminated input unicode string.
193 Destination - The pointer to the null-terminated output ascii string.
201 while (*Source
!= '\0') {
202 *(Destination
++) = (CHAR8
) *(Source
++);
205 // End the ascii with a NULL.
219 GC_TODO: Add function description
223 argc - GC_TODO: add argument description
224 ] - GC_TODO: add argument description
228 GC_TODO: add return values
234 EFI_FIRMWARE_VOLUME_HEADER
*FvImage
;
238 BOOLEAN ErasePolarity
;
241 CHAR8
*OpenSslCommand
;
243 SetUtilityName (UTILITY_NAME
);
245 // Print utility header
247 printf ("%s Version %d.%d Build %s\n",
249 UTILITY_MAJOR_VERSION
,
250 UTILITY_MINOR_VERSION
,
265 // Look for help options
267 if ((strcmp(argv
[0], "-h") == 0) || (strcmp(argv
[0], "--help") == 0) ||
268 (strcmp(argv
[0], "-?") == 0) || (strcmp(argv
[0], "/?") == 0)) {
270 return STATUS_SUCCESS
;
273 // Version has already be printed, so just return success
275 if (strcmp(argv
[0], "--version") == 0) {
276 return STATUS_SUCCESS
;
280 // If they specified -x xref guid/basename cross-reference files, process it.
281 // This will print the basename beside each file guid. To use it, specify
282 // -x xref_filename to processdsc, then use xref_filename as a parameter
286 if ((strcmp(argv
[0], "-x") == 0) || (strcmp(argv
[0], "--xref") == 0)) {
287 ParseGuidBaseNameFile (argv
[1]);
288 printf("ParseGuidBaseNameFile: %s\n", argv
[1]);
293 if (strcmp(argv
[0], "--offset") == 0) {
297 if ((argv
[1][0] == '0') && (tolower ((int)argv
[1][1]) == 'x')) {
298 if (sscanf (argv
[1], "%x", &Offset
) != 1) {
299 Error (NULL
, 0, 1003, "Invalid option value", "Offset = %s", argv
[1]);
300 return GetUtilityStatus ();
303 if (sscanf (argv
[1], "%d", &Offset
) != 1) {
304 Error (NULL
, 0, 1003, "Invalid option value", "Offset = %s", argv
[1]);
305 return GetUtilityStatus ();
308 // See if they said something like "64K"
310 if (tolower ((int)argv
[1][strlen (argv
[1]) - 1]) == 'k') {
319 if ((stricmp (argv
[0], "--hash") == 0)) {
320 if (EnableHash
== TRUE
) {
322 // --hash already given in the option, ignore this one
329 OpenSslCommand
= "openssl";
330 OpenSslEnv
= getenv("OPENSSL_PATH");
331 if (OpenSslEnv
== NULL
) {
332 OpenSslPath
= OpenSslCommand
;
335 // We add quotes to the Openssl Path in case it has space characters
337 OpenSslPath
= malloc(2+strlen(OpenSslEnv
)+strlen(OpenSslCommand
)+1);
338 if (OpenSslPath
== NULL
) {
339 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
340 return GetUtilityStatus ();
342 CombinePath(OpenSslEnv
, OpenSslCommand
, OpenSslPath
);
344 if (OpenSslPath
== NULL
){
345 Error (NULL
, 0, 3000, "Open SSL command not available. Please verify PATH or set OPENSSL_PATH.", NULL
);
346 return GetUtilityStatus ();
353 if ((stricmp (argv
[0], "-v") == 0) || (stricmp (argv
[0], "--verbose") == 0)) {
354 SetPrintLevel (VERBOSE_LOG_LEVEL
);
360 if ((stricmp (argv
[0], "-q") == 0) || (stricmp (argv
[0], "--quiet") == 0)) {
361 SetPrintLevel (KEY_LOG_LEVEL
);
367 if ((stricmp (argv
[0], "-d") == 0) || (stricmp (argv
[0], "--debug") == 0)) {
368 Status
= AsciiStringToUint64 (argv
[1], FALSE
, &LogLevel
);
369 if (EFI_ERROR (Status
)) {
370 Error (NULL
, 0, 1003, "Invalid option value", "%s = %s", argv
[0], argv
[1]);
374 Error (NULL
, 0, 1003, "Invalid option value", "Debug Level range is 0-9, current input level is %d", (int) LogLevel
);
377 SetPrintLevel (LogLevel
);
378 DebugMsg (NULL
, 0, 9, "Debug Mode Set", "Debug Output Mode Level %s is set!", argv
[1]);
384 mUtilityFilename
= argv
[0];
390 // Open the file containing the FV
392 if (mUtilityFilename
== NULL
) {
393 Error (NULL
, 0, 1001, "Missing option", "Input files are not specified");
394 return GetUtilityStatus ();
396 InputFile
= fopen (LongFilePath (mUtilityFilename
), "rb");
397 if (InputFile
== NULL
) {
398 Error (NULL
, 0, 0001, "Error opening the input file", mUtilityFilename
);
399 return GetUtilityStatus ();
402 // Skip over pad bytes if specified. This is used if they prepend 0xff
403 // data to the FV image binary.
406 fseek (InputFile
, Offset
, SEEK_SET
);
409 // Determine size of FV
411 Status
= ReadHeader (InputFile
, &FvSize
, &ErasePolarity
);
412 if (EFI_ERROR (Status
)) {
413 Error (NULL
, 0, 0003, "error parsing FV image", "%s Header is invalid", mUtilityFilename
);
415 return GetUtilityStatus ();
418 // Allocate a buffer for the FV image
420 FvImage
= malloc (FvSize
);
421 if (FvImage
== NULL
) {
422 Error (NULL
, 0, 4001, "Resource: Memory can't be allocated", NULL
);
424 return GetUtilityStatus ();
427 // Seek to the start of the image, then read the entire FV to the buffer
429 fseek (InputFile
, Offset
, SEEK_SET
);
430 BytesRead
= fread (FvImage
, 1, FvSize
, InputFile
);
432 if ((unsigned int) BytesRead
!= FvSize
) {
433 Error (NULL
, 0, 0004, "error reading FvImage from", mUtilityFilename
);
435 return GetUtilityStatus ();
438 LoadGuidedSectionToolsTxt (mUtilityFilename
);
440 PrintFvInfo (FvImage
, FALSE
);
446 FreeGuidBaseNameList ();
447 return GetUtilityStatus ();
461 GC_TODO: Add function description
465 Fv - Firmware Volume to print information about
466 IsChildFv - Flag specifies whether the input FV is a child FV.
476 BOOLEAN ErasePolarity
;
478 EFI_FFS_FILE_HEADER
*CurrentFile
;
481 Status
= FvBufGetSize (Fv
, &FvSize
);
485 (((EFI_FIRMWARE_VOLUME_HEADER
*)Fv
)->Attributes
& EFI_FVB2_ERASE_POLARITY
) ?
489 // Get the first file
492 Status
= FvBufFindNextFile (Fv
, &Key
, (VOID
**) &CurrentFile
);
493 if (EFI_ERROR (Status
)) {
494 Error (NULL
, 0, 0003, "error parsing FV image", "cannot find the first file in the FV image");
495 return GetUtilityStatus ();
498 // Display information about files found
500 while (CurrentFile
!= NULL
) {
502 // Increment the number of files counter
507 // Display info about this file
509 Status
= PrintFileInfo (Fv
, CurrentFile
, ErasePolarity
);
510 if (EFI_ERROR (Status
)) {
511 Error (NULL
, 0, 0003, "error parsing FV image", "failed to parse a file in the FV");
512 return GetUtilityStatus ();
517 Status
= FvBufFindNextFile (Fv
, &Key
, (VOID
**) &CurrentFile
);
518 if (Status
== EFI_NOT_FOUND
) {
520 } else if (EFI_ERROR (Status
)) {
521 Error (NULL
, 0, 0003, "error parsing FV image", "cannot find the next file in the FV image");
522 return GetUtilityStatus ();
527 printf ("There are a total of %d files in the child FV\n", (int) NumberOfFiles
);
529 printf ("There are a total of %d files in this FV\n", (int) NumberOfFiles
);
537 IN UINT32 ActualSize
,
544 This function returns the next larger size that meets the alignment
545 requirement specified.
550 Alignment The desired alignment.
554 EFI_SUCCESS Function completed successfully.
555 EFI_ABORTED The function encountered an error.
561 OccupiedSize
= ActualSize
;
562 while ((OccupiedSize
& (Alignment
- 1)) != 0) {
572 IN EFI_SECTION_TYPE Type
578 Converts EFI Section names to Strings
582 Type - The EFI Section type
586 CHAR8* - Pointer to the String containing the section name.
591 CHAR8
*SectionTypeStringTable
[] = {
599 "EFI_SECTION_COMPRESSION",
603 "EFI_SECTION_GUID_DEFINED",
607 "Unknown section type - Reserved 0x03",
611 "Unknown section type - Reserved 0x04",
615 "Unknown section type - Reserved 0x05",
619 "Unknown section type - Reserved 0x06",
623 "Unknown section type - Reserved 0x07",
627 "Unknown section type - Reserved 0x08",
631 "Unknown section type - Reserved 0x09",
635 "Unknown section type - Reserved 0x0A",
639 "Unknown section type - Reserved 0x0B",
643 "Unknown section type - Reserved 0x0C",
647 "Unknown section type - Reserved 0x0D",
651 "Unknown section type - Reserved 0x0E",
655 "Unknown section type - Reserved 0x0E",
671 "EFI_SECTION_DXE_DEPEX",
675 "EFI_SECTION_VERSION",
679 "EFI_SECTION_USER_INTERFACE",
683 "EFI_SECTION_COMPATIBILITY16",
687 "EFI_SECTION_FIRMWARE_VOLUME_IMAGE ",
691 "EFI_SECTION_FREEFORM_SUBTYPE_GUID ",
699 "Unknown section type - 0x1A",
703 "EFI_SECTION_PEI_DEPEX",
707 "EFI_SECTION_SMM_DEPEX",
711 "Unknown section type - Reserved - beyond last defined section"
714 if (Type
> EFI_SECTION_LAST_SECTION_TYPE
) {
715 Type
= EFI_SECTION_LAST_SECTION_TYPE
+ 1;
718 SectionStr
= malloc (100);
719 if (SectionStr
== NULL
) {
720 printf ("Error: Out of memory resources.\n");
723 strcpy (SectionStr
, SectionTypeStringTable
[Type
]);
732 OUT BOOLEAN
*ErasePolarity
738 This function determines the size of the FV and the erase polarity. The
739 erase polarity is the FALSE value for file state.
743 InputFile The file that contains the FV image.
744 FvSize The size of the FV.
745 ErasePolarity The FV erase polarity.
749 EFI_SUCCESS Function completed successfully.
750 EFI_INVALID_PARAMETER A required parameter was NULL or is out of range.
751 EFI_ABORTED The function encountered an error.
755 EFI_FIRMWARE_VOLUME_HEADER VolumeHeader
;
756 EFI_FV_BLOCK_MAP_ENTRY BlockMap
;
764 // Check input parameters
766 if (InputFile
== NULL
|| FvSize
== NULL
|| ErasePolarity
== NULL
) {
767 Error (__FILE__
, __LINE__
, 0, "application error", "invalid parameter to function");
768 return EFI_INVALID_PARAMETER
;
773 fread (&VolumeHeader
, sizeof (EFI_FIRMWARE_VOLUME_HEADER
) - sizeof (EFI_FV_BLOCK_MAP_ENTRY
), 1, InputFile
);
774 BytesRead
= sizeof (EFI_FIRMWARE_VOLUME_HEADER
) - sizeof (EFI_FV_BLOCK_MAP_ENTRY
);
775 Signature
[0] = VolumeHeader
.Signature
;
779 // Print FV header information
781 printf ("Signature: %s (%X)\n", (char *) Signature
, (unsigned) VolumeHeader
.Signature
);
782 printf ("Attributes: %X\n", (unsigned) VolumeHeader
.Attributes
);
784 if (VolumeHeader
.Attributes
& EFI_FVB2_READ_DISABLED_CAP
) {
785 printf (" EFI_FVB2_READ_DISABLED_CAP\n");
788 if (VolumeHeader
.Attributes
& EFI_FVB2_READ_ENABLED_CAP
) {
789 printf (" EFI_FVB2_READ_ENABLED_CAP\n");
792 if (VolumeHeader
.Attributes
& EFI_FVB2_READ_STATUS
) {
793 printf (" EFI_FVB2_READ_STATUS\n");
796 if (VolumeHeader
.Attributes
& EFI_FVB2_WRITE_DISABLED_CAP
) {
797 printf (" EFI_FVB2_WRITE_DISABLED_CAP\n");
800 if (VolumeHeader
.Attributes
& EFI_FVB2_WRITE_ENABLED_CAP
) {
801 printf (" EFI_FVB2_WRITE_ENABLED_CAP\n");
804 if (VolumeHeader
.Attributes
& EFI_FVB2_WRITE_STATUS
) {
805 printf (" EFI_FVB2_WRITE_STATUS\n");
808 if (VolumeHeader
.Attributes
& EFI_FVB2_LOCK_CAP
) {
809 printf (" EFI_FVB2_LOCK_CAP\n");
812 if (VolumeHeader
.Attributes
& EFI_FVB2_LOCK_STATUS
) {
813 printf (" EFI_FVB2_LOCK_STATUS\n");
816 if (VolumeHeader
.Attributes
& EFI_FVB2_STICKY_WRITE
) {
817 printf (" EFI_FVB2_STICKY_WRITE\n");
820 if (VolumeHeader
.Attributes
& EFI_FVB2_MEMORY_MAPPED
) {
821 printf (" EFI_FVB2_MEMORY_MAPPED\n");
824 if (VolumeHeader
.Attributes
& EFI_FVB2_ERASE_POLARITY
) {
825 printf (" EFI_FVB2_ERASE_POLARITY\n");
826 *ErasePolarity
= TRUE
;
829 #if (PI_SPECIFICATION_VERSION < 0x00010000)
830 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT
) {
831 printf (" EFI_FVB2_ALIGNMENT\n");
834 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_2
) {
835 printf (" EFI_FVB2_ALIGNMENT_2\n");
838 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_4
) {
839 printf (" EFI_FVB2_ALIGNMENT_4\n");
842 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_8
) {
843 printf (" EFI_FVB2_ALIGNMENT_8\n");
846 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_16
) {
847 printf (" EFI_FVB2_ALIGNMENT_16\n");
850 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_32
) {
851 printf (" EFI_FVB2_ALIGNMENT_32\n");
854 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_64
) {
855 printf (" EFI_FVB2_ALIGNMENT_64\n");
858 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_128
) {
859 printf (" EFI_FVB2_ALIGNMENT_128\n");
862 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_256
) {
863 printf (" EFI_FVB2_ALIGNMENT_256\n");
866 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_512
) {
867 printf (" EFI_FVB2_ALIGNMENT_512\n");
870 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_1K
) {
871 printf (" EFI_FVB2_ALIGNMENT_1K\n");
874 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_2K
) {
875 printf (" EFI_FVB2_ALIGNMENT_2K\n");
878 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_4K
) {
879 printf (" EFI_FVB2_ALIGNMENT_4K\n");
882 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_8K
) {
883 printf (" EFI_FVB2_ALIGNMENT_8K\n");
886 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_16K
) {
887 printf (" EFI_FVB2_ALIGNMENT_16K\n");
890 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_32K
) {
891 printf (" EFI_FVB2_ALIGNMENT_32K\n");
894 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_64K
) {
895 printf (" EFI_FVB2_ALIGNMENT_64K\n");
900 if (VolumeHeader
.Attributes
& EFI_FVB2_READ_LOCK_CAP
) {
901 printf (" EFI_FVB2_READ_LOCK_CAP\n");
904 if (VolumeHeader
.Attributes
& EFI_FVB2_READ_LOCK_STATUS
) {
905 printf (" EFI_FVB2_READ_LOCK_STATUS\n");
908 if (VolumeHeader
.Attributes
& EFI_FVB2_WRITE_LOCK_CAP
) {
909 printf (" EFI_FVB2_WRITE_LOCK_CAP\n");
912 if (VolumeHeader
.Attributes
& EFI_FVB2_WRITE_LOCK_STATUS
) {
913 printf (" EFI_FVB2_WRITE_LOCK_STATUS\n");
916 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_1
) {
917 printf (" EFI_FVB2_ALIGNMENT_1\n");
920 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_2
) {
921 printf (" EFI_FVB2_ALIGNMENT_2\n");
924 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_4
) {
925 printf (" EFI_FVB2_ALIGNMENT_4\n");
928 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_8
) {
929 printf (" EFI_FVB2_ALIGNMENT_8\n");
932 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_16
) {
933 printf (" EFI_FVB2_ALIGNMENT_16\n");
936 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_32
) {
937 printf (" EFI_FVB2_ALIGNMENT_32\n");
940 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_64
) {
941 printf (" EFI_FVB2_ALIGNMENT_64\n");
944 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_128
) {
945 printf (" EFI_FVB2_ALIGNMENT_128\n");
948 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_256
) {
949 printf (" EFI_FVB2_ALIGNMENT_256\n");
952 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_512
) {
953 printf (" EFI_FVB2_ALIGNMENT_512\n");
956 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_1K
) {
957 printf (" EFI_FVB2_ALIGNMENT_1K\n");
960 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_2K
) {
961 printf (" EFI_FVB2_ALIGNMENT_2K\n");
964 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_4K
) {
965 printf (" EFI_FVB2_ALIGNMENT_4K\n");
968 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_8K
) {
969 printf (" EFI_FVB2_ALIGNMENT_8K\n");
972 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_16K
) {
973 printf (" EFI_FVB2_ALIGNMENT_16K\n");
976 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_32K
) {
977 printf (" EFI_FVB2_ALIGNMENT_32K\n");
980 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_64K
) {
981 printf (" EFI_FVB2_ALIGNMENT_64K\n");
984 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_128K
) {
985 printf (" EFI_FVB2_ALIGNMENT_128K\n");
988 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_256K
) {
989 printf (" EFI_FVB2_ALIGNMENT_256K\n");
992 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_512K
) {
993 printf (" EFI_FVB2_ALIGNMENT_512K\n");
996 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_1M
) {
997 printf (" EFI_FVB2_ALIGNMENT_1M\n");
1000 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_2M
) {
1001 printf (" EFI_FVB2_ALIGNMENT_2M\n");
1004 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_4M
) {
1005 printf (" EFI_FVB2_ALIGNMENT_4M\n");
1008 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_8M
) {
1009 printf (" EFI_FVB2_ALIGNMENT_8M\n");
1012 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_16M
) {
1013 printf (" EFI_FVB2_ALIGNMENT_16M\n");
1016 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_32M
) {
1017 printf (" EFI_FVB2_ALIGNMENT_32M\n");
1020 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_64M
) {
1021 printf (" EFI_FVB2_ALIGNMENT_64M\n");
1024 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_128M
) {
1025 printf (" EFI_FVB2_ALIGNMENT_128M\n");
1028 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_64M
) {
1029 printf (" EFI_FVB2_ALIGNMENT_64M\n");
1032 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_128M
) {
1033 printf (" EFI_FVB2_ALIGNMENT_128M\n");
1036 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_256M
) {
1037 printf (" EFI_FVB2_ALIGNMENT_256M\n");
1040 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_512M
) {
1041 printf (" EFI_FVB2_ALIGNMENT_512M\n");
1044 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_1G
) {
1045 printf (" EFI_FVB2_ALIGNMENT_1G\n");
1048 if (VolumeHeader
.Attributes
& EFI_FVB2_ALIGNMENT_2G
) {
1049 printf (" EFI_FVB2_ALIGNMENT_2G\n");
1053 printf ("Header Length: 0x%08X\n", VolumeHeader
.HeaderLength
);
1054 printf ("File System ID: ");
1055 PrintGuid (&VolumeHeader
.FileSystemGuid
);
1059 printf ("Revision: 0x%04X\n", VolumeHeader
.Revision
);
1062 fread (&BlockMap
, sizeof (EFI_FV_BLOCK_MAP_ENTRY
), 1, InputFile
);
1063 BytesRead
+= sizeof (EFI_FV_BLOCK_MAP_ENTRY
);
1065 if (BlockMap
.NumBlocks
!= 0) {
1066 printf ("Number of Blocks: 0x%08X\n", (unsigned) BlockMap
.NumBlocks
);
1067 printf ("Block Length: 0x%08X\n", (unsigned) BlockMap
.Length
);
1068 Size
+= BlockMap
.NumBlocks
* BlockMap
.Length
;
1071 } while (!(BlockMap
.NumBlocks
== 0 && BlockMap
.Length
== 0));
1073 if (BytesRead
!= VolumeHeader
.HeaderLength
) {
1074 printf ("ERROR: Header length not consistent with Block Maps!\n");
1078 if (VolumeHeader
.FvLength
!= Size
) {
1079 printf ("ERROR: Volume Size not consistant with Block Maps!\n");
1083 printf ("Total Volume Size: 0x%08X\n", (unsigned) Size
);
1088 // rewind (InputFile);
1096 EFI_FIRMWARE_VOLUME_HEADER
*FvImage
,
1097 EFI_FFS_FILE_HEADER
*FileHeader
,
1098 BOOLEAN ErasePolarity
1102 Routine Description:
1104 GC_TODO: Add function description
1108 FvImage - GC_TODO: add argument description
1109 FileHeader - GC_TODO: add argument description
1110 ErasePolarity - GC_TODO: add argument description
1114 EFI_SUCCESS - GC_TODO: Add description for return value
1115 EFI_ABORTED - GC_TODO: Add description for return value
1122 EFI_FFS_FILE_HEADER2 BlankHeader
;
1124 UINT8 GuidBuffer
[PRINTED_GUID_BUFFER_SIZE
];
1126 #if (PI_SPECIFICATION_VERSION < 0x00010000)
1130 // Check if we have free space
1132 HeaderSize
= FvBufGetFfsHeaderSize(FileHeader
);
1133 if (ErasePolarity
) {
1134 memset (&BlankHeader
, -1, HeaderSize
);
1136 memset (&BlankHeader
, 0, HeaderSize
);
1139 if (memcmp (&BlankHeader
, FileHeader
, HeaderSize
) == 0) {
1143 // Print file information.
1145 printf ("============================================================\n");
1147 printf ("File Name: ");
1148 PrintGuidToBuffer (&FileHeader
->Name
, GuidBuffer
, sizeof (GuidBuffer
), TRUE
);
1149 printf ("%s ", GuidBuffer
);
1150 PrintGuidName (GuidBuffer
);
1154 // PrintGuid (&FileHeader->Name);
1157 FileLength
= FvBufGetFfsFileSize (FileHeader
);
1158 printf ("File Offset: 0x%08X\n", (unsigned) ((UINTN
) FileHeader
- (UINTN
) FvImage
));
1159 printf ("File Length: 0x%08X\n", (unsigned) FileLength
);
1160 printf ("File Attributes: 0x%02X\n", FileHeader
->Attributes
);
1161 printf ("File State: 0x%02X\n", FileHeader
->State
);
1166 FileState
= GetFileState (ErasePolarity
, FileHeader
);
1168 switch (FileState
) {
1170 case EFI_FILE_HEADER_CONSTRUCTION
:
1171 printf (" EFI_FILE_HEADER_CONSTRUCTION\n");
1174 case EFI_FILE_HEADER_INVALID
:
1175 printf (" EFI_FILE_HEADER_INVALID\n");
1178 case EFI_FILE_HEADER_VALID
:
1179 printf (" EFI_FILE_HEADER_VALID\n");
1180 Checksum
= CalculateSum8 ((UINT8
*) FileHeader
, HeaderSize
);
1181 Checksum
= (UINT8
) (Checksum
- FileHeader
->IntegrityCheck
.Checksum
.File
);
1182 Checksum
= (UINT8
) (Checksum
- FileHeader
->State
);
1183 if (Checksum
!= 0) {
1184 printf ("ERROR: Header checksum invalid.\n");
1190 case EFI_FILE_DELETED
:
1191 printf (" EFI_FILE_DELETED\n");
1193 case EFI_FILE_MARKED_FOR_UPDATE
:
1194 printf (" EFI_FILE_MARKED_FOR_UPDATE\n");
1196 case EFI_FILE_DATA_VALID
:
1197 printf (" EFI_FILE_DATA_VALID\n");
1200 // Calculate header checksum
1202 Checksum
= CalculateSum8 ((UINT8
*) FileHeader
, HeaderSize
);
1203 Checksum
= (UINT8
) (Checksum
- FileHeader
->IntegrityCheck
.Checksum
.File
);
1204 Checksum
= (UINT8
) (Checksum
- FileHeader
->State
);
1205 if (Checksum
!= 0) {
1206 Error (NULL
, 0, 0003, "error parsing FFS file", "FFS file with Guid %s has invalid header checksum", GuidBuffer
);
1210 FileLength
= FvBufGetFfsFileSize (FileHeader
);
1212 if (FileHeader
->Attributes
& FFS_ATTRIB_CHECKSUM
) {
1214 // Calculate file checksum
1216 Checksum
= CalculateSum8 ((UINT8
*)FileHeader
+ HeaderSize
, FileLength
- HeaderSize
);
1217 Checksum
= Checksum
+ FileHeader
->IntegrityCheck
.Checksum
.File
;
1218 if (Checksum
!= 0) {
1219 Error (NULL
, 0, 0003, "error parsing FFS file", "FFS file with Guid %s has invalid file checksum", GuidBuffer
);
1223 if (FileHeader
->IntegrityCheck
.Checksum
.File
!= FFS_FIXED_CHECKSUM
) {
1224 Error (NULL
, 0, 0003, "error parsing FFS file", "FFS file with Guid %s has invalid header checksum -- not set to fixed value of 0xAA", GuidBuffer
);
1228 #if (PI_SPECIFICATION_VERSION < 0x00010000)
1230 // Verify tail if present
1232 if (FileHeader
->Attributes
& FFS_ATTRIB_TAIL_PRESENT
) {
1234 // Verify tail is complement of integrity check field in the header.
1236 Tail
= (UINT16
*) ((UINTN
) FileHeader
+ GetLength (FileHeader
->Size
) - sizeof (EFI_FFS_INTEGRITY_CHECK
));
1237 if (FileHeader
->IntegrityCheck
.TailReference
!= (UINT16
)~(*Tail
)) {
1238 Error (NULL
, 0, 0003, "error parsing FFS file", \
1239 "FFS file with Guid %s failed in the integrity check, tail is not the complement of the header field", GuidBuffer
);
1247 Error (NULL
, 0, 0003, "error parsing FFS file", "FFS file with Guid %s has the invalid/unrecognized file state bits", GuidBuffer
);
1251 printf ("File Type: 0x%02X ", FileHeader
->Type
);
1253 switch (FileHeader
->Type
) {
1255 case EFI_FV_FILETYPE_RAW
:
1256 printf ("EFI_FV_FILETYPE_RAW\n");
1259 case EFI_FV_FILETYPE_FREEFORM
:
1260 printf ("EFI_FV_FILETYPE_FREEFORM\n");
1263 case EFI_FV_FILETYPE_SECURITY_CORE
:
1264 printf ("EFI_FV_FILETYPE_SECURITY_CORE\n");
1267 case EFI_FV_FILETYPE_PEI_CORE
:
1268 printf ("EFI_FV_FILETYPE_PEI_CORE\n");
1271 case EFI_FV_FILETYPE_DXE_CORE
:
1272 printf ("EFI_FV_FILETYPE_DXE_CORE\n");
1275 case EFI_FV_FILETYPE_PEIM
:
1276 printf ("EFI_FV_FILETYPE_PEIM\n");
1279 case EFI_FV_FILETYPE_DRIVER
:
1280 printf ("EFI_FV_FILETYPE_DRIVER\n");
1283 case EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
:
1284 printf ("EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER\n");
1287 case EFI_FV_FILETYPE_APPLICATION
:
1288 printf ("EFI_FV_FILETYPE_APPLICATION\n");
1291 case EFI_FV_FILETYPE_SMM
:
1292 printf ("EFI_FV_FILETYPE_SMM\n");
1295 case EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
:
1296 printf ("EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE\n");
1299 case EFI_FV_FILETYPE_COMBINED_SMM_DXE
:
1300 printf ("EFI_FV_FILETYPE_COMBINED_SMM_DXE\n");
1303 case EFI_FV_FILETYPE_SMM_CORE
:
1304 printf ("EFI_FV_FILETYPE_SMM_CORE\n");
1307 case EFI_FV_FILETYPE_MM_STANDALONE
:
1308 printf ("EFI_FV_FILETYPE_MM_STANDALONE\n");
1311 case EFI_FV_FILETYPE_MM_CORE_STANDALONE
:
1312 printf ("EFI_FV_FILETYPE_MM_CORE_STANDALONE\n");
1315 case EFI_FV_FILETYPE_FFS_PAD
:
1316 printf ("EFI_FV_FILETYPE_FFS_PAD\n");
1320 printf ("\nERROR: Unrecognized file type %X.\n", FileHeader
->Type
);
1325 switch (FileHeader
->Type
) {
1327 case EFI_FV_FILETYPE_ALL
:
1328 case EFI_FV_FILETYPE_RAW
:
1329 case EFI_FV_FILETYPE_FFS_PAD
:
1334 // All other files have sections
1336 Status
= ParseSection (
1337 (UINT8
*) ((UINTN
) FileHeader
+ HeaderSize
),
1338 FvBufGetFfsFileSize (FileHeader
) - HeaderSize
1340 if (EFI_ERROR (Status
)) {
1342 // printf ("ERROR: Parsing the FFS file.\n");
1354 IN VOID
*FileHandle
,
1355 IN UINTN FileOffset
,
1356 IN OUT UINT32
*ReadSize
,
1361 Routine Description:
1363 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
1367 FileHandle - The handle to the PE/COFF file
1369 FileOffset - The offset, in bytes, into the file to read
1371 ReadSize - The number of bytes to read from the file starting at FileOffset
1373 Buffer - A pointer to the buffer to read the data into.
1377 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
1381 CHAR8
*Destination8
;
1385 Destination8
= Buffer
;
1386 Source8
= (CHAR8
*) ((UINTN
) FileHandle
+ FileOffset
);
1389 *(Destination8
++) = *(Source8
++);
1396 SetAddressToSectionHeader (
1398 IN OUT UINT8
*FileBuffer
,
1399 IN UINT64 NewPe32BaseAddress
1403 Routine Description:
1405 Set new base address into the section header of PeImage
1409 FileName - Name of file
1410 FileBuffer - Pointer to PeImage.
1411 NewPe32BaseAddress - New Base Address for PE image.
1415 EFI_SUCCESS Set new base address into this image successfully.
1420 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
1422 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
1423 EFI_IMAGE_SECTION_HEADER
*SectionHeader
;
1426 // Initialize context
1428 memset (&ImageContext
, 0, sizeof (ImageContext
));
1429 ImageContext
.Handle
= (VOID
*) FileBuffer
;
1430 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) RebaseImageRead
;
1431 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
1432 if (EFI_ERROR (Status
)) {
1433 Error (NULL
, 0, 3000, "Invalid", "The input PeImage %s is not valid", FileName
);
1437 if (ImageContext
.RelocationsStripped
) {
1438 Error (NULL
, 0, 3000, "Invalid", "The input PeImage %s has no relocation to be fixed up", FileName
);
1443 // Get PeHeader pointer
1445 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(FileBuffer
+ ImageContext
.PeCoffHeaderOffset
);
1448 // Get section header list
1450 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (
1453 sizeof (EFI_IMAGE_FILE_HEADER
) +
1454 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
1458 // Set base address into the first section header that doesn't point to code section.
1460 for (Index
= 0; Index
< ImgHdr
->Pe32
.FileHeader
.NumberOfSections
; Index
++, SectionHeader
++) {
1461 if ((SectionHeader
->Characteristics
& EFI_IMAGE_SCN_CNT_CODE
) == 0) {
1462 *(UINT64
*) &SectionHeader
->PointerToRelocations
= NewPe32BaseAddress
;
1468 // BaseAddress is set to section header.
1476 IN OUT UINT8
*FileBuffer
,
1477 IN UINT64 NewPe32BaseAddress
1481 Routine Description:
1483 Set new base address into PeImage, and fix up PeImage based on new address.
1487 FileName - Name of file
1488 FileBuffer - Pointer to PeImage.
1489 NewPe32BaseAddress - New Base Address for PE image.
1493 EFI_INVALID_PARAMETER - BaseAddress is not valid.
1494 EFI_SUCCESS - Update PeImage is correctly.
1499 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
1501 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
1502 UINT8
*MemoryImagePointer
;
1503 EFI_IMAGE_SECTION_HEADER
*SectionHeader
;
1506 // Initialize context
1508 memset (&ImageContext
, 0, sizeof (ImageContext
));
1509 ImageContext
.Handle
= (VOID
*) FileBuffer
;
1510 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) RebaseImageRead
;
1511 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
1512 if (EFI_ERROR (Status
)) {
1513 Error (NULL
, 0, 3000, "Invalid", "The input PeImage %s is not valid", FileName
);
1517 if (ImageContext
.RelocationsStripped
) {
1518 Error (NULL
, 0, 3000, "Invalid", "The input PeImage %s has no relocation to be fixed up", FileName
);
1523 // Get PeHeader pointer
1525 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(FileBuffer
+ ImageContext
.PeCoffHeaderOffset
);
1528 // Load and Relocate Image Data
1530 MemoryImagePointer
= (UINT8
*) malloc ((UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
1531 if (MemoryImagePointer
== NULL
) {
1532 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
1533 return EFI_OUT_OF_RESOURCES
;
1535 memset ((VOID
*) MemoryImagePointer
, 0, (UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
1536 ImageContext
.ImageAddress
= ((UINTN
) MemoryImagePointer
+ ImageContext
.SectionAlignment
- 1) & (~((INT64
)ImageContext
.SectionAlignment
- 1));
1538 Status
= PeCoffLoaderLoadImage (&ImageContext
);
1539 if (EFI_ERROR (Status
)) {
1540 Error (NULL
, 0, 3000, "Invalid", "LocateImage() call failed on rebase of %s", FileName
);
1541 free ((VOID
*) MemoryImagePointer
);
1545 ImageContext
.DestinationAddress
= NewPe32BaseAddress
;
1546 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
1547 if (EFI_ERROR (Status
)) {
1548 Error (NULL
, 0, 3000, "Invalid", "RelocateImage() call failed on rebase of %s", FileName
);
1549 free ((VOID
*) MemoryImagePointer
);
1554 // Copy Relocated data to raw image file.
1556 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (
1559 sizeof (EFI_IMAGE_FILE_HEADER
) +
1560 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
1563 for (Index
= 0; Index
< ImgHdr
->Pe32
.FileHeader
.NumberOfSections
; Index
++, SectionHeader
++) {
1565 FileBuffer
+ SectionHeader
->PointerToRawData
,
1566 (VOID
*) (UINTN
) (ImageContext
.ImageAddress
+ SectionHeader
->VirtualAddress
),
1567 SectionHeader
->SizeOfRawData
1571 free ((VOID
*) MemoryImagePointer
);
1574 // Update Image Base Address
1576 if ((ImgHdr
->Pe32
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) && (ImgHdr
->Pe32
.FileHeader
.Machine
!= IMAGE_FILE_MACHINE_IA64
)) {
1577 ImgHdr
->Pe32
.OptionalHeader
.ImageBase
= (UINT32
) NewPe32BaseAddress
;
1578 } else if (ImgHdr
->Pe32Plus
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
1579 ImgHdr
->Pe32Plus
.OptionalHeader
.ImageBase
= NewPe32BaseAddress
;
1581 Error (NULL
, 0, 3000, "Invalid", "unknown PE magic signature %X in PE32 image %s",
1582 ImgHdr
->Pe32
.OptionalHeader
.Magic
,
1589 // Set new base address into section header
1591 Status
= SetAddressToSectionHeader (FileName
, FileBuffer
, NewPe32BaseAddress
);
1598 IN CHAR8
* DefaultPath
,
1599 IN CHAR8
* AppendPath
,
1603 UINT32 DefaultPathLen
;
1605 CHAR8 QuotesStr
[] = "\"";
1606 strcpy(NewPath
, QuotesStr
);
1607 DefaultPathLen
= strlen(DefaultPath
);
1608 strcat(NewPath
, DefaultPath
);
1610 for (; Index
< DefaultPathLen
+ 1; Index
++) {
1611 if (NewPath
[Index
] == '\\' || NewPath
[Index
] == '/') {
1612 if (NewPath
[Index
+ 1] != '\0') {
1613 NewPath
[Index
] = '/';
1617 if (NewPath
[Index
-1] != '/') {
1618 NewPath
[Index
] = '/';
1619 NewPath
[Index
+ 1] = '\0';
1621 strcat(NewPath
, AppendPath
);
1622 strcat(NewPath
, QuotesStr
);
1628 IN UINT8
*SectionBuffer
,
1629 IN UINT32 BufferLength
1633 Routine Description:
1639 SectionBuffer - Buffer containing the section to parse.
1640 BufferLength - Length of SectionBuffer
1644 EFI_SECTION_ERROR - Problem with section parsing.
1645 (a) compression errors
1646 (b) unrecognized section
1647 EFI_UNSUPPORTED - Do not know how to parse the section.
1648 EFI_SUCCESS - Section successfully parsed.
1649 EFI_OUT_OF_RESOURCES - Memory allocation failed.
1653 EFI_SECTION_TYPE Type
;
1655 UINT32 SectionLength
;
1656 UINT32 SectionHeaderLen
;
1659 UINT32 ParsedLength
;
1660 UINT8
*CompressedBuffer
;
1661 UINT32 CompressedLength
;
1662 UINT8
*UncompressedBuffer
;
1663 UINT32 UncompressedLength
;
1664 UINT8
*ToolOutputBuffer
;
1665 UINT32 ToolOutputLength
;
1666 UINT8 CompressionType
;
1669 UINT8
*ScratchBuffer
;
1670 DECOMPRESS_FUNCTION DecompressFunction
;
1671 GETINFO_FUNCTION GetInfoFunction
;
1673 CHAR8
*ExtractionTool
;
1674 CHAR8
*ToolInputFile
;
1675 CHAR8
*ToolOutputFile
;
1676 CHAR8
*SystemCommand
;
1681 CHAR8
*ToolInputFileName
;
1682 CHAR8
*ToolOutputFileName
;
1686 ToolInputFileName
= NULL
;
1687 ToolOutputFileName
= NULL
;
1689 while (ParsedLength
< BufferLength
) {
1690 Ptr
= SectionBuffer
+ ParsedLength
;
1692 SectionLength
= GetLength (((EFI_COMMON_SECTION_HEADER
*) Ptr
)->Size
);
1693 Type
= ((EFI_COMMON_SECTION_HEADER
*) Ptr
)->Type
;
1696 // This is sort of an odd check, but is necessary because FFS files are
1697 // padded to a QWORD boundary, meaning there is potentially a whole section
1698 // header worth of 0xFF bytes.
1700 if (SectionLength
== 0xffffff && Type
== 0xff) {
1706 // Get real section file size
1708 SectionLength
= GetSectionFileLength ((EFI_COMMON_SECTION_HEADER
*) Ptr
);
1709 SectionHeaderLen
= GetSectionHeaderLength((EFI_COMMON_SECTION_HEADER
*)Ptr
);
1711 SectionName
= SectionNameToStr (Type
);
1712 if (SectionName
!= NULL
) {
1713 printf ("------------------------------------------------------------\n");
1714 printf (" Type: %s\n Size: 0x%08X\n", SectionName
, (unsigned) SectionLength
);
1719 case EFI_SECTION_RAW
:
1720 case EFI_SECTION_PIC
:
1721 case EFI_SECTION_TE
:
1722 // default is no more information
1725 case EFI_SECTION_PE32
:
1727 ToolInputFileName
= "edk2Temp_InputEfi.tmp";
1728 ToolOutputFileName
= "edk2Temp_OutputHash.tmp";
1729 RebaseImage(ToolInputFileName
, (UINT8
*)Ptr
+ SectionHeaderLen
, 0);
1732 (CHAR8
*)Ptr
+ SectionHeaderLen
,
1733 SectionLength
- SectionHeaderLen
1736 SystemCommand
= malloc (
1737 strlen (OPENSSL_COMMAND_FORMAT_STRING
) +
1738 strlen (OpenSslPath
) +
1739 strlen (ToolInputFileName
) +
1740 strlen (ToolOutputFileName
) +
1743 if (SystemCommand
== NULL
) {
1744 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
1745 return EFI_OUT_OF_RESOURCES
;
1749 OPENSSL_COMMAND_FORMAT_STRING
,
1755 if (system (SystemCommand
) != EFI_SUCCESS
) {
1756 Error (NULL
, 0, 3000, "Open SSL command not available. Please verify PATH or set OPENSSL_PATH.", NULL
);
1763 if((fp
= fopen(ToolOutputFileName
,"r")) == NULL
) {
1764 Error (NULL
, 0, 0004, "Hash the PE32 image failed.", NULL
);
1767 fseek(fp
,0,SEEK_SET
);
1768 fseek(fp
,0,SEEK_END
);
1769 nFileLen
= ftell(fp
);
1770 fseek(fp
,0,SEEK_SET
);
1771 StrLine
= malloc(nFileLen
);
1772 if (StrLine
== NULL
) {
1774 free (SystemCommand
);
1775 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
1776 return EFI_OUT_OF_RESOURCES
;
1778 fgets(StrLine
, nFileLen
, fp
);
1779 NewStr
= strrchr (StrLine
, '=');
1780 printf (" SHA1: %s\n", NewStr
+ 1);
1785 remove(ToolInputFileName
);
1786 remove(ToolOutputFileName
);
1787 free (SystemCommand
);
1791 case EFI_SECTION_USER_INTERFACE
:
1792 UIFileName
= (CHAR8
*) malloc (UnicodeStrLen (((EFI_USER_INTERFACE_SECTION
*) Ptr
)->FileNameString
) + 1);
1793 if (UIFileName
== NULL
) {
1794 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
1795 return EFI_OUT_OF_RESOURCES
;
1797 Unicode2AsciiString (((EFI_USER_INTERFACE_SECTION
*) Ptr
)->FileNameString
, UIFileName
);
1798 printf (" String: %s\n", UIFileName
);
1802 case EFI_SECTION_FIRMWARE_VOLUME_IMAGE
:
1803 Status
= PrintFvInfo (Ptr
+ SectionHeaderLen
, TRUE
);
1804 if (EFI_ERROR (Status
)) {
1805 Error (NULL
, 0, 0003, "printing of FV section contents failed", NULL
);
1806 return EFI_SECTION_ERROR
;
1810 case EFI_SECTION_COMPATIBILITY16
:
1811 case EFI_SECTION_FREEFORM_SUBTYPE_GUID
:
1813 // Section does not contain any further header information.
1817 case EFI_SECTION_PEI_DEPEX
:
1818 case EFI_SECTION_DXE_DEPEX
:
1819 case EFI_SECTION_SMM_DEPEX
:
1820 DumpDepexSection (Ptr
, SectionLength
);
1823 case EFI_SECTION_VERSION
:
1824 printf (" Build Number: 0x%02X\n", *(UINT16
*)(Ptr
+ SectionHeaderLen
));
1825 printf (" Version Strg: %s\n", (char*) (Ptr
+ SectionHeaderLen
+ sizeof (UINT16
)));
1828 case EFI_SECTION_COMPRESSION
:
1829 UncompressedBuffer
= NULL
;
1830 if (SectionHeaderLen
== sizeof (EFI_COMMON_SECTION_HEADER
)) {
1831 RealHdrLen
= sizeof(EFI_COMPRESSION_SECTION
);
1832 UncompressedLength
= ((EFI_COMPRESSION_SECTION
*)Ptr
)->UncompressedLength
;
1833 CompressionType
= ((EFI_COMPRESSION_SECTION
*)Ptr
)->CompressionType
;
1835 RealHdrLen
= sizeof(EFI_COMPRESSION_SECTION2
);
1836 UncompressedLength
= ((EFI_COMPRESSION_SECTION2
*)Ptr
)->UncompressedLength
;
1837 CompressionType
= ((EFI_COMPRESSION_SECTION2
*)Ptr
)->CompressionType
;
1839 CompressedLength
= SectionLength
- RealHdrLen
;
1840 printf (" Uncompressed Length: 0x%08X\n", (unsigned) UncompressedLength
);
1842 if (CompressionType
== EFI_NOT_COMPRESSED
) {
1843 printf (" Compression Type: EFI_NOT_COMPRESSED\n");
1844 if (CompressedLength
!= UncompressedLength
) {
1849 "file is not compressed, but the compressed length does not match the uncompressed length",
1852 return EFI_SECTION_ERROR
;
1855 UncompressedBuffer
= Ptr
+ RealHdrLen
;
1856 } else if (CompressionType
== EFI_STANDARD_COMPRESSION
) {
1857 GetInfoFunction
= EfiGetInfo
;
1858 DecompressFunction
= EfiDecompress
;
1859 printf (" Compression Type: EFI_STANDARD_COMPRESSION\n");
1861 CompressedBuffer
= Ptr
+ RealHdrLen
;
1863 Status
= GetInfoFunction (CompressedBuffer
, CompressedLength
, &DstSize
, &ScratchSize
);
1864 if (EFI_ERROR (Status
)) {
1865 Error (NULL
, 0, 0003, "error getting compression info from compression section", NULL
);
1866 return EFI_SECTION_ERROR
;
1869 if (DstSize
!= UncompressedLength
) {
1870 Error (NULL
, 0, 0003, "compression error in the compression section", NULL
);
1871 return EFI_SECTION_ERROR
;
1874 ScratchBuffer
= malloc (ScratchSize
);
1875 if (ScratchBuffer
== NULL
) {
1876 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
1877 return EFI_OUT_OF_RESOURCES
;
1879 UncompressedBuffer
= malloc (UncompressedLength
);
1880 if (UncompressedBuffer
== NULL
) {
1881 free (ScratchBuffer
);
1882 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
1883 return EFI_OUT_OF_RESOURCES
;
1885 Status
= DecompressFunction (
1893 free (ScratchBuffer
);
1894 if (EFI_ERROR (Status
)) {
1895 Error (NULL
, 0, 0003, "decompress failed", NULL
);
1896 free (UncompressedBuffer
);
1897 return EFI_SECTION_ERROR
;
1900 Error (NULL
, 0, 0003, "unrecognized compression type", "type 0x%X", CompressionType
);
1901 return EFI_SECTION_ERROR
;
1904 Status
= ParseSection (UncompressedBuffer
, UncompressedLength
);
1906 if (CompressionType
== EFI_STANDARD_COMPRESSION
) {
1908 // We need to deallocate Buffer
1910 free (UncompressedBuffer
);
1913 if (EFI_ERROR (Status
)) {
1914 Error (NULL
, 0, 0003, "failed to parse section", NULL
);
1915 return EFI_SECTION_ERROR
;
1919 case EFI_SECTION_GUID_DEFINED
:
1920 if (SectionHeaderLen
== sizeof(EFI_COMMON_SECTION_HEADER
)) {
1921 EfiGuid
= &((EFI_GUID_DEFINED_SECTION
*) Ptr
)->SectionDefinitionGuid
;
1922 DataOffset
= ((EFI_GUID_DEFINED_SECTION
*) Ptr
)->DataOffset
;
1923 Attributes
= ((EFI_GUID_DEFINED_SECTION
*) Ptr
)->Attributes
;
1925 EfiGuid
= &((EFI_GUID_DEFINED_SECTION2
*) Ptr
)->SectionDefinitionGuid
;
1926 DataOffset
= ((EFI_GUID_DEFINED_SECTION2
*) Ptr
)->DataOffset
;
1927 Attributes
= ((EFI_GUID_DEFINED_SECTION2
*) Ptr
)->Attributes
;
1929 printf (" SectionDefinitionGuid: ");
1930 PrintGuid (EfiGuid
);
1932 printf (" DataOffset: 0x%04X\n", (unsigned) DataOffset
);
1933 printf (" Attributes: 0x%04X\n", (unsigned) Attributes
);
1936 LookupGuidedSectionToolPath (
1937 mParsedGuidedSectionTools
,
1941 if (ExtractionTool
!= NULL
) {
1943 ToolInputFile
= CloneString (tmpnam (NULL
));
1944 ToolOutputFile
= CloneString (tmpnam (NULL
));
1946 char tmp1
[] = "/tmp/fileXXXXXX";
1947 char tmp2
[] = "/tmp/fileXXXXXX";
1950 fd1
= mkstemp(tmp1
);
1951 fd2
= mkstemp(tmp2
);
1952 ToolInputFile
= CloneString(tmp1
);
1953 ToolOutputFile
= CloneString(tmp2
);
1958 if ((ToolInputFile
== NULL
) || (ToolOutputFile
== NULL
)) {
1959 if (ToolInputFile
!= NULL
) {
1960 free (ToolInputFile
);
1962 if (ToolOutputFile
!= NULL
) {
1963 free (ToolOutputFile
);
1965 free (ExtractionTool
);
1967 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
1968 return EFI_OUT_OF_RESOURCES
;
1972 // Construction 'system' command string
1974 SystemCommand
= malloc (
1975 strlen (EXTRACT_COMMAND_FORMAT_STRING
) +
1976 strlen (ExtractionTool
) +
1977 strlen (ToolInputFile
) +
1978 strlen (ToolOutputFile
) +
1981 if (SystemCommand
== NULL
) {
1982 free (ToolInputFile
);
1983 free (ToolOutputFile
);
1984 free (ExtractionTool
);
1986 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
1987 return EFI_OUT_OF_RESOURCES
;
1991 EXTRACT_COMMAND_FORMAT_STRING
,
1996 free (ExtractionTool
);
2001 (CHAR8
*) SectionBuffer
+ DataOffset
,
2002 BufferLength
- DataOffset
2005 system (SystemCommand
);
2006 remove (ToolInputFile
);
2007 free (ToolInputFile
);
2012 (CHAR8
**)&ToolOutputBuffer
,
2015 remove (ToolOutputFile
);
2016 free (ToolOutputFile
);
2017 free (SystemCommand
);
2018 if (EFI_ERROR (Status
)) {
2019 Error (NULL
, 0, 0004, "unable to read decoded GUIDED section", NULL
);
2020 return EFI_SECTION_ERROR
;
2023 Status
= ParseSection (
2027 if (EFI_ERROR (Status
)) {
2028 Error (NULL
, 0, 0003, "parse of decoded GUIDED section failed", NULL
);
2029 return EFI_SECTION_ERROR
;
2033 // Check for CRC32 sections which we can handle internally if needed.
2035 } else if (!CompareGuid (
2037 &gEfiCrc32GuidedSectionExtractionProtocolGuid
2041 // CRC32 guided section
2043 Status
= ParseSection (
2044 SectionBuffer
+ DataOffset
,
2045 BufferLength
- DataOffset
2047 if (EFI_ERROR (Status
)) {
2048 Error (NULL
, 0, 0003, "parse of CRC32 GUIDED section failed", NULL
);
2049 return EFI_SECTION_ERROR
;
2053 // We don't know how to parse it now.
2055 Error (NULL
, 0, 0003, "Error parsing section", \
2056 "EFI_SECTION_GUID_DEFINED cannot be parsed at this time. Tool to decode this section should have been defined in GuidedSectionTools.txt (built in the FV directory).");
2057 return EFI_UNSUPPORTED
;
2063 // Unknown section, return error
2065 Error (NULL
, 0, 0003, "unrecognized section type found", "section type = 0x%X", Type
);
2066 return EFI_SECTION_ERROR
;
2069 ParsedLength
+= SectionLength
;
2071 // We make then next section begin on a 4-byte boundary
2073 ParsedLength
= GetOccupiedSize (ParsedLength
, 4);
2076 if (ParsedLength
< BufferLength
) {
2077 Error (NULL
, 0, 0003, "sections do not completely fill the sectioned buffer being parsed", NULL
);
2078 return EFI_SECTION_ERROR
;
2087 IN UINT32 SectionLength
2091 Routine Description:
2093 GC_TODO: Add function description
2097 Ptr - GC_TODO: add argument description
2098 SectionLength - GC_TODO: add argument description
2102 EFI_SUCCESS - GC_TODO: Add description for return value
2106 UINT8 GuidBuffer
[PRINTED_GUID_BUFFER_SIZE
];
2109 // Need at least a section header + data
2111 if (SectionLength
<= sizeof (EFI_COMMON_SECTION_HEADER
)) {
2115 Ptr
+= GetSectionHeaderLength((EFI_COMMON_SECTION_HEADER
*)Ptr
);
2116 SectionLength
-= GetSectionHeaderLength((EFI_COMMON_SECTION_HEADER
*)Ptr
);
2117 while (SectionLength
> 0) {
2120 case EFI_DEP_BEFORE
:
2121 printf ("BEFORE\n");
2134 PrintGuidToBuffer ((EFI_GUID
*) (Ptr
+ 1), GuidBuffer
, sizeof (GuidBuffer
), TRUE
);
2135 printf ("%s ", GuidBuffer
);
2136 PrintGuidName (GuidBuffer
);
2139 // PrintGuid ((EFI_GUID *)(Ptr + 1));
2142 SectionLength
-= 17;
2176 printf ("END DEPEX\n");
2188 printf ("Unrecognized byte in depex: 0x%X\n", *Ptr
);
2202 Routine Description:
2204 GC_TODO: Add function description
2208 GuidStr - GC_TODO: add argument description
2212 EFI_SUCCESS - GC_TODO: Add description for return value
2213 EFI_INVALID_PARAMETER - GC_TODO: Add description for return value
2217 GUID_TO_BASENAME
*GPtr
;
2219 // If we have a list of guid-to-basenames, then go through the list to
2220 // look for a guid string match. If found, print the basename to stdout,
2221 // otherwise return a failure.
2223 GPtr
= mGuidBaseNameList
;
2224 while (GPtr
!= NULL
) {
2225 if (_stricmp ((CHAR8
*) GuidStr
, (CHAR8
*) GPtr
->Guid
) == 0) {
2226 printf ("%s", GPtr
->BaseName
);
2233 return EFI_INVALID_PARAMETER
;
2237 ParseGuidBaseNameFile (
2242 Routine Description:
2244 GC_TODO: Add function description
2248 FileName - GC_TODO: add argument description
2252 EFI_DEVICE_ERROR - GC_TODO: Add description for return value
2253 EFI_OUT_OF_RESOURCES - GC_TODO: Add description for return value
2254 EFI_SUCCESS - GC_TODO: Add description for return value
2259 CHAR8 Line
[MAX_LINE_LEN
];
2260 CHAR8 FormatString
[MAX_LINE_LEN
];
2261 GUID_TO_BASENAME
*GPtr
;
2263 if ((Fptr
= fopen (LongFilePath (FileName
), "r")) == NULL
) {
2264 printf ("ERROR: Failed to open input cross-reference file '%s'\n", FileName
);
2265 return EFI_DEVICE_ERROR
;
2269 // Generate the format string for fscanf
2274 (unsigned) sizeof (GPtr
->Guid
) - 1,
2275 (unsigned) sizeof (GPtr
->BaseName
) - 1
2278 while (fgets (Line
, sizeof (Line
), Fptr
) != NULL
) {
2280 // Allocate space for another guid/basename element
2282 GPtr
= malloc (sizeof (GUID_TO_BASENAME
));
2285 return EFI_OUT_OF_RESOURCES
;
2288 memset ((char *) GPtr
, 0, sizeof (GUID_TO_BASENAME
));
2289 if (sscanf (Line
, FormatString
, GPtr
->Guid
, GPtr
->BaseName
) == 2) {
2290 GPtr
->Next
= mGuidBaseNameList
;
2291 mGuidBaseNameList
= GPtr
;
2294 // Some sort of error. Just continue.
2305 FreeGuidBaseNameList (
2310 Routine Description:
2312 GC_TODO: Add function description
2320 EFI_SUCCESS - GC_TODO: Add description for return value
2324 GUID_TO_BASENAME
*Next
;
2326 while (mGuidBaseNameList
!= NULL
) {
2327 Next
= mGuidBaseNameList
->Next
;
2328 free (mGuidBaseNameList
);
2329 mGuidBaseNameList
= Next
;
2338 LoadGuidedSectionToolsTxt (
2339 IN CHAR8
* FirmwareVolumeFilename
2342 CHAR8
* PeerFilename
;
2349 Places
[0] = FirmwareVolumeFilename
;
2350 //Places[1] = mUtilityFilename;
2352 mParsedGuidedSectionTools
= NULL
;
2354 for (Index
= 0; Index
< (sizeof(Places
)/sizeof(Places
[0])); Index
++) {
2355 PeerFilename
= OsPathPeerFilePath (Places
[Index
], "GuidedSectionTools.txt");
2356 //printf("Loading %s...\n", PeerFilename);
2357 if (OsPathExists (PeerFilename
)) {
2358 mParsedGuidedSectionTools
= ParseGuidedSectionToolsFile (PeerFilename
);
2360 free (PeerFilename
);
2361 if (mParsedGuidedSectionTools
!= NULL
) {
2374 Routine Description:
2376 GC_TODO: Add function description
2384 GC_TODO: add return values
2391 fprintf (stdout
, "Usage: %s [options] <input_file>\n\n", UTILITY_NAME
);
2394 // Copyright declaration
2396 fprintf (stdout
, "Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.\n\n");
2397 fprintf (stdout
, " Display Tiano Firmware Volume FFS image information\n\n");
2402 fprintf (stdout
, "optional arguments:\n");
2403 fprintf (stdout
, " -h, --help\n\
2404 Show this help message and exit\n");
2405 fprintf (stdout
, " --version\n\
2406 Show program's version number and exit\n");
2407 fprintf (stdout
, " -d [DEBUG], --debug [DEBUG]\n\
2408 Output DEBUG statements, where DEBUG_LEVEL is 0 (min) - 9 (max)\n");
2409 fprintf (stdout
, " -v, --verbose\n\
2410 Print informational statements\n");
2411 fprintf (stdout
, " -q, --quiet\n\
2412 Returns the exit code, error messages will be displayed\n");
2413 fprintf (stdout
, " -s, --silent\n\
2414 Returns only the exit code; informational and error\n\
2415 messages are not displayed\n");
2416 fprintf (stdout
, " -x XREF_FILENAME, --xref XREF_FILENAME\n\
2417 Parse the basename to file-guid cross reference file(s)\n");
2418 fprintf (stdout
, " -f OFFSET, --offset OFFSET\n\
2419 The offset from the start of the input file to start \n\
2420 processing an FV\n");
2421 fprintf (stdout
, " --hash\n\
2422 Generate HASH value of the entire PE image\n");
2423 fprintf (stdout
, " --sfo\n\
2424 Reserved for future use\n");