2 This file contains functions required to generate a Firmware File System file.
4 Copyright (c) 2004 - 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.
18 #include <sys/types.h>
26 #include <Common/UefiBaseTypes.h>
27 #include <Common/PiFirmwareFile.h>
28 #include <IndustryStandard/PeImage.h>
29 #include <Guid/FfsSectionAlignmentPadding.h>
31 #include "CommonLib.h"
33 #include "EfiUtilityMsgs.h"
35 #include "PeCoffLib.h"
37 #define UTILITY_NAME "GenFfs"
38 #define UTILITY_MAJOR_VERSION 0
39 #define UTILITY_MINOR_VERSION 1
41 STATIC CHAR8
*mFfsFileType
[] = {
43 "EFI_FV_FILETYPE_RAW", // 0x01
44 "EFI_FV_FILETYPE_FREEFORM", // 0x02
45 "EFI_FV_FILETYPE_SECURITY_CORE", // 0x03
46 "EFI_FV_FILETYPE_PEI_CORE", // 0x04
47 "EFI_FV_FILETYPE_DXE_CORE", // 0x05
48 "EFI_FV_FILETYPE_PEIM", // 0x06
49 "EFI_FV_FILETYPE_DRIVER", // 0x07
50 "EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER", // 0x08
51 "EFI_FV_FILETYPE_APPLICATION", // 0x09
52 "EFI_FV_FILETYPE_SMM", // 0x0A
53 "EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE",// 0x0B
54 "EFI_FV_FILETYPE_COMBINED_SMM_DXE", // 0x0C
55 "EFI_FV_FILETYPE_SMM_CORE", // 0x0D
56 "EFI_FV_FILETYPE_MM_STANDALONE", // 0x0E
57 "EFI_FV_FILETYPE_MM_CORE_STANDALONE" // 0x0F
60 STATIC CHAR8
*mAlignName
[] = {
61 "1", "2", "4", "8", "16", "32", "64", "128", "256", "512",
62 "1K", "2K", "4K", "8K", "16K", "32K", "64K", "128K", "256K",
63 "512K", "1M", "2M", "4M", "8M", "16M"
66 STATIC CHAR8
*mFfsValidAlignName
[] = {
67 "8", "16", "128", "512", "1K", "4K", "32K", "64K", "128K","256K",
68 "512K", "1M", "2M", "4M", "8M", "16M"
71 STATIC UINT32 mFfsValidAlign
[] = {0, 8, 16, 128, 512, 1024, 4096, 32768, 65536, 131072, 262144,
72 524288, 1048576, 2097152, 4194304, 8388608, 16777216};
74 STATIC EFI_GUID mZeroGuid
= {0};
76 STATIC EFI_GUID mEfiFfsSectionAlignmentPaddingGuid
= EFI_FFS_SECTION_ALIGNMENT_PADDING_GUID
;
87 Print out version information for this utility.
99 fprintf (stdout
, "%s Version %d.%d %s \n", UTILITY_NAME
, UTILITY_MAJOR_VERSION
, UTILITY_MINOR_VERSION
, __BUILD_VERSION
);
111 Print Error / Help message.
126 fprintf (stdout
, "\nUsage: %s [options]\n\n", UTILITY_NAME
);
129 // Copyright declaration
131 fprintf (stdout
, "Copyright (c) 2007 - 2017, Intel Corporation. All rights reserved.\n\n");
136 fprintf (stdout
, "Options:\n");
137 fprintf (stdout
, " -o FileName, --outputfile FileName\n\
138 File is FFS file to be created.\n");
139 fprintf (stdout
, " -t Type, --filetype Type\n\
140 Type is one FV file type defined in PI spec, which is\n\
141 EFI_FV_FILETYPE_RAW, EFI_FV_FILETYPE_FREEFORM,\n\
142 EFI_FV_FILETYPE_SECURITY_CORE, EFI_FV_FILETYPE_PEIM,\n\
143 EFI_FV_FILETYPE_PEI_CORE, EFI_FV_FILETYPE_DXE_CORE,\n\
144 EFI_FV_FILETYPE_DRIVER, EFI_FV_FILETYPE_APPLICATION,\n\
145 EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER,\n\
146 EFI_FV_FILETYPE_SMM, EFI_FV_FILETYPE_SMM_CORE,\n\
147 EFI_FV_FILETYPE_MM_STANDALONE,\n\
148 EFI_FV_FILETYPE_MM_CORE_STANDALONE,\n\
149 EFI_FV_FILETYPE_COMBINED_SMM_DXE, \n\
150 EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE.\n");
151 fprintf (stdout
, " -g FileGuid, --fileguid FileGuid\n\
152 FileGuid is one module guid.\n\
153 Its format is xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n");
154 fprintf (stdout
, " -x, --fixed Indicates that the file may not be moved\n\
155 from its present location.\n");
156 fprintf (stdout
, " -s, --checksum Indicates to calculate file checksum.\n");
157 fprintf (stdout
, " -a FileAlign, --align FileAlign\n\
158 FileAlign points to file alignment, which only support\n\
159 the following align: 1,2,4,8,16,128,512,1K,4K,32K,64K\n\
160 128K,256K,512K,1M,2M,4M,8M,16M\n");
161 fprintf (stdout
, " -i SectionFile, --sectionfile SectionFile\n\
162 Section file will be contained in this FFS file.\n");
163 fprintf (stdout
, " -n SectionAlign, --sectionalign SectionAlign\n\
164 SectionAlign points to section alignment, which support\n\
165 the alignment scope 0~16M. If SectionAlign is specified\n\
166 as 0, tool get alignment value from SectionFile. It is\n\
167 specified together with sectionfile to point its\n\
168 alignment in FFS file.\n");
169 fprintf (stdout
, " -v, --verbose Turn on verbose output with informational messages.\n");
170 fprintf (stdout
, " -q, --quiet Disable all messages except key message and fatal error\n");
171 fprintf (stdout
, " -d, --debug level Enable debug messages, at input debug level.\n");
172 fprintf (stdout
, " --version Show program's version number and exit.\n");
173 fprintf (stdout
, " -h, --help Show this help message and exit.\n");
179 IN CHAR8
*AlignBuffer
,
180 OUT UINT32
*AlignNumber
186 Converts Align String to align value (1~16M).
190 AlignBuffer - Pointer to Align string.
191 AlignNumber - Pointer to Align value.
195 EFI_SUCCESS Successfully convert align string to align value.
196 EFI_INVALID_PARAMETER Align string is invalid or align value is not in scope.
204 if (AlignBuffer
== NULL
) {
205 return EFI_INVALID_PARAMETER
;
207 for (Index
= 0; Index
< sizeof (mAlignName
) / sizeof (CHAR8
*); Index
++) {
208 if (stricmp (AlignBuffer
, mAlignName
[Index
]) == 0) {
209 *AlignNumber
= 1 << Index
;
213 return EFI_INVALID_PARAMETER
;
225 Converts File Type String to value. EFI_FV_FILETYPE_ALL indicates that an
226 unrecognized file type was specified.
230 String - File type string
240 if (String
== NULL
) {
241 return EFI_FV_FILETYPE_ALL
;
244 for (Index
= 0; Index
< sizeof (mFfsFileType
) / sizeof (CHAR8
*); Index
++) {
245 if (mFfsFileType
[Index
] != NULL
&& (stricmp (String
, mFfsFileType
[Index
]) == 0)) {
249 return EFI_FV_FILETYPE_ALL
;
255 IN CHAR8
**InputFileName
,
256 IN UINT32
*InputFileAlign
,
257 IN UINT32 InputFileNum
,
258 IN EFI_FFS_FILE_ATTRIBUTES FfsAttrib
,
259 OUT UINT8
*FileBuffer
,
260 OUT UINT32
*BufferLength
,
261 OUT UINT32
*MaxAlignment
,
262 OUT UINT8
*PESectionNum
268 Get the contents of all section files specified in InputFileName
273 InputFileName - Name of the input file.
275 InputFileAlign - Alignment required by the input file data.
277 InputFileNum - Number of input files. Should be at least 1.
279 FileBuffer - Output buffer to contain data
281 BufferLength - On input, this is size of the FileBuffer.
282 On output, this is the actual length of the data.
284 MaxAlignment - The max alignment required by all the input file datas.
286 PeSectionNum - Calculate the number of Pe/Te Section in this FFS file.
290 EFI_SUCCESS on successful return
291 EFI_INVALID_PARAMETER if InputFileNum is less than 1 or BufferLength point is NULL.
292 EFI_ABORTED if unable to open input file.
293 EFI_BUFFER_TOO_SMALL FileBuffer is not enough to contain all file data.
301 EFI_FREEFORM_SUBTYPE_GUID_SECTION
*SectHeader
;
302 EFI_COMMON_SECTION_HEADER2 TempSectHeader
;
303 EFI_TE_IMAGE_HEADER TeHeader
;
305 EFI_GUID_DEFINED_SECTION GuidSectHeader
;
306 EFI_GUID_DEFINED_SECTION2 GuidSectHeader2
;
308 UINT32 MaxEncounteredAlignment
;
313 MaxEncounteredAlignment
= 1;
316 // Go through our array of file names and copy their contents
317 // to the output buffer.
319 for (Index
= 0; Index
< InputFileNum
; Index
++) {
321 // make sure section ends on a DWORD boundary
323 while ((Size
& 0x03) != 0) {
328 // Open file and read contents
330 InFile
= fopen (LongFilePath (InputFileName
[Index
]), "rb");
331 if (InFile
== NULL
) {
332 Error (NULL
, 0, 0001, "Error opening file", InputFileName
[Index
]);
336 fseek (InFile
, 0, SEEK_END
);
337 FileSize
= ftell (InFile
);
338 fseek (InFile
, 0, SEEK_SET
);
339 DebugMsg (NULL
, 0, 9, "Input section files",
340 "the input section name is %s and the size is %u bytes", InputFileName
[Index
], (unsigned) FileSize
);
343 // Check this section is Te/Pe section, and Calculate the numbers of Te/Pe section.
346 if (FileSize
>= MAX_FFS_SIZE
) {
347 HeaderSize
= sizeof (EFI_COMMON_SECTION_HEADER2
);
349 HeaderSize
= sizeof (EFI_COMMON_SECTION_HEADER
);
351 fread (&TempSectHeader
, 1, HeaderSize
, InFile
);
352 if (TempSectHeader
.Type
== EFI_SECTION_TE
) {
354 fread (&TeHeader
, 1, sizeof (TeHeader
), InFile
);
355 if (TeHeader
.Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
356 TeOffset
= TeHeader
.StrippedSize
- sizeof (TeHeader
);
358 } else if (TempSectHeader
.Type
== EFI_SECTION_PE32
) {
360 } else if (TempSectHeader
.Type
== EFI_SECTION_GUID_DEFINED
) {
361 fseek (InFile
, 0, SEEK_SET
);
362 if (FileSize
>= MAX_SECTION_SIZE
) {
363 fread (&GuidSectHeader2
, 1, sizeof (GuidSectHeader2
), InFile
);
364 if ((GuidSectHeader2
.Attributes
& EFI_GUIDED_SECTION_PROCESSING_REQUIRED
) == 0) {
365 HeaderSize
= GuidSectHeader2
.DataOffset
;
368 fread (&GuidSectHeader
, 1, sizeof (GuidSectHeader
), InFile
);
369 if ((GuidSectHeader
.Attributes
& EFI_GUIDED_SECTION_PROCESSING_REQUIRED
) == 0) {
370 HeaderSize
= GuidSectHeader
.DataOffset
;
374 } else if (TempSectHeader
.Type
== EFI_SECTION_COMPRESSION
||
375 TempSectHeader
.Type
== EFI_SECTION_FIRMWARE_VOLUME_IMAGE
) {
377 // for the encapsulated section, assume it contains Pe/Te section
382 fseek (InFile
, 0, SEEK_SET
);
385 // Revert TeOffset to the converse value relative to Alignment
386 // This is to assure the original PeImage Header at Alignment.
388 if ((TeOffset
!= 0) && (InputFileAlign
[Index
] != 0)) {
389 TeOffset
= InputFileAlign
[Index
] - (TeOffset
% InputFileAlign
[Index
]);
390 TeOffset
= TeOffset
% InputFileAlign
[Index
];
394 // make sure section data meet its alignment requirement by adding one pad section.
395 // But the different sections have the different section header. Necessary or not?
396 // Based on section type to adjust offset? Todo
398 if ((InputFileAlign
[Index
] != 0) && (((Size
+ HeaderSize
+ TeOffset
) % InputFileAlign
[Index
]) != 0)) {
399 Offset
= (Size
+ sizeof (EFI_COMMON_SECTION_HEADER
) + HeaderSize
+ TeOffset
+ InputFileAlign
[Index
] - 1) & ~(InputFileAlign
[Index
] - 1);
400 Offset
= Offset
- Size
- HeaderSize
- TeOffset
;
402 if (FileBuffer
!= NULL
&& ((Size
+ Offset
) < *BufferLength
)) {
404 // The maximal alignment is 64K, the raw section size must be less than 0xffffff
406 memset (FileBuffer
+ Size
, 0, Offset
);
407 SectHeader
= (EFI_FREEFORM_SUBTYPE_GUID_SECTION
*) (FileBuffer
+ Size
);
408 SectHeader
->CommonHeader
.Size
[0] = (UINT8
) (Offset
& 0xff);
409 SectHeader
->CommonHeader
.Size
[1] = (UINT8
) ((Offset
& 0xff00) >> 8);
410 SectHeader
->CommonHeader
.Size
[2] = (UINT8
) ((Offset
& 0xff0000) >> 16);
413 // Only add a special reducible padding section if
414 // - this FFS has the FFS_ATTRIB_FIXED attribute,
415 // - none of the preceding sections have alignment requirements,
416 // - the size of the padding is sufficient for the
417 // EFI_SECTION_FREEFORM_SUBTYPE_GUID header.
419 if ((FfsAttrib
& FFS_ATTRIB_FIXED
) != 0 &&
420 MaxEncounteredAlignment
<= 1 &&
421 Offset
>= sizeof (EFI_FREEFORM_SUBTYPE_GUID_SECTION
)) {
422 SectHeader
->CommonHeader
.Type
= EFI_SECTION_FREEFORM_SUBTYPE_GUID
;
423 SectHeader
->SubTypeGuid
= mEfiFfsSectionAlignmentPaddingGuid
;
425 SectHeader
->CommonHeader
.Type
= EFI_SECTION_RAW
;
428 DebugMsg (NULL
, 0, 9, "Pad raw section for section data alignment",
429 "Pad Raw section size is %u", (unsigned) Offset
);
431 Size
= Size
+ Offset
;
435 // Get the Max alignment of all input file datas
437 if (MaxEncounteredAlignment
< InputFileAlign
[Index
]) {
438 MaxEncounteredAlignment
= InputFileAlign
[Index
];
442 // Now read the contents of the file into the buffer
443 // Buffer must be enough to contain the file content.
445 if ((FileSize
> 0) && (FileBuffer
!= NULL
) && ((Size
+ FileSize
) <= *BufferLength
)) {
446 if (fread (FileBuffer
+ Size
, (size_t) FileSize
, 1, InFile
) != 1) {
447 Error (NULL
, 0, 0004, "Error reading file", InputFileName
[Index
]);
457 *MaxAlignment
= MaxEncounteredAlignment
;
460 // Set the actual length of the data.
462 if (Size
> *BufferLength
) {
463 *BufferLength
= Size
;
464 return EFI_BUFFER_TOO_SMALL
;
466 *BufferLength
= Size
;
475 IN OUT UINT32
*ReadSize
,
482 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
486 FileHandle - The handle to the PE/COFF file
488 FileOffset - The offset, in bytes, into the file to read
490 ReadSize - The number of bytes to read from the file starting at FileOffset
492 Buffer - A pointer to the buffer to read the data into.
496 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
504 Destination8
= Buffer
;
505 Source8
= (CHAR8
*) ((UINTN
) FileHandle
+ FileOffset
);
508 *(Destination8
++) = *(Source8
++);
516 GetAlignmentFromFile(char *InFile
, UINT32
*Alignment
)
518 InFile is input file for getting alignment
525 UINT32 CurSecHdrSize
;
526 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
527 EFI_COMMON_SECTION_HEADER
*CommonHeader
;
534 memset (&ImageContext
, 0, sizeof (ImageContext
));
536 InFileHandle
= fopen(LongFilePath(InFile
), "rb");
537 if (InFileHandle
== NULL
){
538 Error (NULL
, 0, 0001, "Error opening file", InFile
);
541 PeFileSize
= _filelength (fileno(InFileHandle
));
542 PeFileBuffer
= (UINT8
*) malloc (PeFileSize
);
543 if (PeFileBuffer
== NULL
) {
544 fclose (InFileHandle
);
545 Error(NULL
, 0, 4001, "Resource", "memory cannot be allocated of %s", InFileHandle
);
546 return EFI_OUT_OF_RESOURCES
;
548 fread (PeFileBuffer
, sizeof (UINT8
), PeFileSize
, InFileHandle
);
549 fclose (InFileHandle
);
550 CommonHeader
= (EFI_COMMON_SECTION_HEADER
*) PeFileBuffer
;
551 CurSecHdrSize
= GetSectionHeaderLength(CommonHeader
);
552 ImageContext
.Handle
= (VOID
*) ((UINTN
)PeFileBuffer
+ CurSecHdrSize
);
553 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
)FfsRebaseImageRead
;
554 Status
= PeCoffLoaderGetImageInfo(&ImageContext
);
555 if (EFI_ERROR (Status
)) {
556 Error (NULL
, 0, 3000, "Invalid PeImage", "The input file is %s and return status is %x", InFile
, (int) Status
);
559 *Alignment
= ImageContext
.SectionAlignment
;
560 // Free the allocated memory resource
561 if (PeFileBuffer
!= NULL
) {
581 argc - Number of command line parameters.
582 argv - Array of pointers to parameter strings.
585 STATUS_SUCCESS - Utility exits successfully.
586 STATUS_ERROR - Some error occurred during execution.
591 EFI_FFS_FILE_ATTRIBUTES FfsAttrib
;
593 EFI_FV_FILETYPE FfsFiletype
;
594 CHAR8
*OutputFileName
;
595 EFI_GUID FileGuid
= {0};
597 UINT32
*InputFileAlign
;
598 CHAR8
**InputFileName
;
602 EFI_FFS_FILE_HEADER2 FfsFileHeader
;
609 CHAR8 AlignmentBuffer
[8];
612 // Init local variables
618 FfsFiletype
= EFI_FV_FILETYPE_ALL
;
619 OutputFileName
= NULL
;
621 InputFileName
= NULL
;
622 InputFileAlign
= NULL
;
627 Status
= EFI_SUCCESS
;
630 SetUtilityName (UTILITY_NAME
);
633 Error (NULL
, 0, 1001, "Missing options", "no options input");
639 // Parse command line
644 if ((stricmp (argv
[0], "-h") == 0) || (stricmp (argv
[0], "--help") == 0)) {
647 return STATUS_SUCCESS
;
650 if (stricmp (argv
[0], "--version") == 0) {
652 return STATUS_SUCCESS
;
656 if ((stricmp (argv
[0], "-t") == 0) || (stricmp (argv
[0], "--filetype") == 0)) {
657 if (argv
[1] == NULL
|| argv
[1][0] == '-') {
658 Error (NULL
, 0, 1003, "Invalid option value", "file type is missing for -t option");
661 FfsFiletype
= StringToType (argv
[1]);
662 if (FfsFiletype
== EFI_FV_FILETYPE_ALL
) {
663 Error (NULL
, 0, 1003, "Invalid option value", "%s is not a valid file type", argv
[1]);
671 if ((stricmp (argv
[0], "-o") == 0) || (stricmp (argv
[0], "--outputfile") == 0)) {
672 if (argv
[1] == NULL
|| argv
[1][0] == '-') {
673 Error (NULL
, 0, 1003, "Invalid option value", "Output file is missing for -o option");
676 OutputFileName
= argv
[1];
682 if ((stricmp (argv
[0], "-g") == 0) || (stricmp (argv
[0], "--fileguid") == 0)) {
683 Status
= StringToGuid (argv
[1], &FileGuid
);
684 if (EFI_ERROR (Status
)) {
685 Error (NULL
, 0, 1003, "Invalid option value", "%s = %s", argv
[0], argv
[1]);
693 if ((stricmp (argv
[0], "-x") == 0) || (stricmp (argv
[0], "--fixed") == 0)) {
694 FfsAttrib
|= FFS_ATTRIB_FIXED
;
700 if ((stricmp (argv
[0], "-s") == 0) || (stricmp (argv
[0], "--checksum") == 0)) {
701 FfsAttrib
|= FFS_ATTRIB_CHECKSUM
;
707 if ((stricmp (argv
[0], "-a") == 0) || (stricmp (argv
[0], "--align") == 0)) {
708 if (argv
[1] == NULL
|| argv
[1][0] == '-') {
709 Error (NULL
, 0, 1003, "Invalid option value", "Align value is missing for -a option");
712 for (Index
= 0; Index
< sizeof (mFfsValidAlignName
) / sizeof (CHAR8
*); Index
++) {
713 if (stricmp (argv
[1], mFfsValidAlignName
[Index
]) == 0) {
717 if (Index
== sizeof (mFfsValidAlignName
) / sizeof (CHAR8
*)) {
718 if ((stricmp (argv
[1], "1") == 0) || (stricmp (argv
[1], "2") == 0) || (stricmp (argv
[1], "4") == 0)) {
720 // 1, 2, 4 byte alignment same to 8 byte alignment
724 Error (NULL
, 0, 1003, "Invalid option value", "%s = %s", argv
[0], argv
[1]);
734 if ((stricmp (argv
[0], "-i") == 0) || (stricmp (argv
[0], "--sectionfile") == 0)) {
736 // Get Input file name and its alignment
738 if (argv
[1] == NULL
|| argv
[1][0] == '-') {
739 Error (NULL
, 0, 1003, "Invalid option value", "input section file is missing for -i option");
744 // Allocate Input file name buffer and its alignment buffer.
746 if ((InputFileNum
== 0) && (InputFileName
== NULL
)) {
747 InputFileName
= (CHAR8
**) malloc (MAXIMUM_INPUT_FILE_NUM
* sizeof (CHAR8
*));
748 if (InputFileName
== NULL
) {
749 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
752 memset (InputFileName
, 0, (MAXIMUM_INPUT_FILE_NUM
* sizeof (CHAR8
*)));
754 InputFileAlign
= (UINT32
*) malloc (MAXIMUM_INPUT_FILE_NUM
* sizeof (UINT32
));
755 if (InputFileAlign
== NULL
) {
756 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
757 free (InputFileName
);
760 memset (InputFileAlign
, 0, MAXIMUM_INPUT_FILE_NUM
* sizeof (UINT32
));
761 } else if (InputFileNum
% MAXIMUM_INPUT_FILE_NUM
== 0) {
763 // InputFileName and alignment buffer too small, need to realloc
765 InputFileName
= (CHAR8
**) realloc (
767 (InputFileNum
+ MAXIMUM_INPUT_FILE_NUM
) * sizeof (CHAR8
*)
770 if (InputFileName
== NULL
) {
771 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
772 free (InputFileAlign
);
775 memset (&(InputFileName
[InputFileNum
]), 0, (MAXIMUM_INPUT_FILE_NUM
* sizeof (CHAR8
*)));
777 InputFileAlign
= (UINT32
*) realloc (
779 (InputFileNum
+ MAXIMUM_INPUT_FILE_NUM
) * sizeof (UINT32
)
782 if (InputFileAlign
== NULL
) {
783 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
784 free (InputFileName
);
787 memset (&(InputFileAlign
[InputFileNum
]), 0, (MAXIMUM_INPUT_FILE_NUM
* sizeof (UINT32
)));
790 InputFileName
[InputFileNum
] = argv
[1];
800 // Section File alignment requirement
802 if ((stricmp (argv
[0], "-n") == 0) || (stricmp (argv
[0], "--sectionalign") == 0)) {
803 if ((argv
[1] != NULL
) && (stricmp("0", argv
[1]) == 0)) {
804 Status
= GetAlignmentFromFile(InputFileName
[InputFileNum
], &Alignment
);
805 if (EFI_ERROR(Status
)) {
806 Error (NULL
, 0, 1003, "Fail to get Alignment from %s", InputFileName
[InputFileNum
]);
809 if (Alignment
< 0x400){
810 sprintf (AlignmentBuffer
, "%d", Alignment
);
812 else if (Alignment
>= 0x400) {
813 if (Alignment
>= 0x100000) {
814 sprintf (AlignmentBuffer
, "%dM", Alignment
/0x100000);
816 sprintf (AlignmentBuffer
, "%dK", Alignment
/0x400);
819 Status
= StringtoAlignment (AlignmentBuffer
, &(InputFileAlign
[InputFileNum
]));
822 Status
= StringtoAlignment (argv
[1], &(InputFileAlign
[InputFileNum
]));
824 if (EFI_ERROR (Status
)) {
825 Error (NULL
, 0, 1003, "Invalid option value", "%s = %s", argv
[0], argv
[1]);
835 if ((stricmp (argv
[0], "-n") == 0) || (stricmp (argv
[0], "--sectionalign") == 0)) {
836 Error (NULL
, 0, 1000, "Unknown option", "SectionAlign option must be specified with section file.");
840 if ((stricmp (argv
[0], "-v") == 0) || (stricmp (argv
[0], "--verbose") == 0)) {
841 SetPrintLevel (VERBOSE_LOG_LEVEL
);
842 VerboseMsg ("Verbose output Mode Set!");
848 if ((stricmp (argv
[0], "-q") == 0) || (stricmp (argv
[0], "--quiet") == 0)) {
849 SetPrintLevel (KEY_LOG_LEVEL
);
850 KeyMsg ("Quiet output Mode Set!");
856 if ((stricmp (argv
[0], "-d") == 0) || (stricmp (argv
[0], "--debug") == 0)) {
857 Status
= AsciiStringToUint64 (argv
[1], FALSE
, &LogLevel
);
858 if (EFI_ERROR (Status
)) {
859 Error (NULL
, 0, 1003, "Invalid option value", "%s = %s", argv
[0], argv
[1]);
863 Error (NULL
, 0, 1003, "Invalid option value", "Debug Level range is 0-9, current input level is %d", (int) LogLevel
);
866 SetPrintLevel (LogLevel
);
867 DebugMsg (NULL
, 0, 9, "Debug Mode Set", "Debug Output Mode Level %s is set!", argv
[1]);
873 Error (NULL
, 0, 1000, "Unknown option", "%s", argv
[0]);
877 VerboseMsg ("%s tool start.", UTILITY_NAME
);
880 // Check the complete input parameters.
882 if (FfsFiletype
== EFI_FV_FILETYPE_ALL
) {
883 Error (NULL
, 0, 1001, "Missing option", "filetype");
887 if (CompareGuid (&FileGuid
, &mZeroGuid
) == 0) {
888 Error (NULL
, 0, 1001, "Missing option", "fileguid");
892 if (InputFileNum
== 0) {
893 Error (NULL
, 0, 1001, "Missing option", "Input files");
898 // Output input parameter information
900 VerboseMsg ("Fv File type is %s", mFfsFileType
[FfsFiletype
]);
901 VerboseMsg ("Output file name is %s", OutputFileName
);
902 VerboseMsg ("FFS File Guid is %08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
903 (unsigned) FileGuid
.Data1
,
914 if ((FfsAttrib
& FFS_ATTRIB_FIXED
) != 0) {
915 VerboseMsg ("FFS File has the fixed file attribute");
917 if ((FfsAttrib
& FFS_ATTRIB_CHECKSUM
) != 0) {
918 VerboseMsg ("FFS File requires the checksum of the whole file");
920 VerboseMsg ("FFS file alignment is %s", mFfsValidAlignName
[FfsAlign
]);
921 for (Index
= 0; Index
< InputFileNum
; Index
++) {
922 if (InputFileAlign
[Index
] == 0) {
924 // Minimum alignment is 1 byte.
926 InputFileAlign
[Index
] = 1;
928 VerboseMsg ("the %dth input section name is %s and section alignment is %u", Index
, InputFileName
[Index
], (unsigned) InputFileAlign
[Index
]);
932 // Calculate the size of all input section files.
934 Status
= GetSectionContents (
945 if ((FfsFiletype
== EFI_FV_FILETYPE_SECURITY_CORE
||
946 FfsFiletype
== EFI_FV_FILETYPE_PEI_CORE
||
947 FfsFiletype
== EFI_FV_FILETYPE_DXE_CORE
) && (PeSectionNum
!= 1)) {
948 Error (NULL
, 0, 2000, "Invalid parameter", "Fv File type %s must have one and only one Pe or Te section, but %u Pe/Te section are input", mFfsFileType
[FfsFiletype
], PeSectionNum
);
952 if ((FfsFiletype
== EFI_FV_FILETYPE_PEIM
||
953 FfsFiletype
== EFI_FV_FILETYPE_DRIVER
||
954 FfsFiletype
== EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER
||
955 FfsFiletype
== EFI_FV_FILETYPE_APPLICATION
) && (PeSectionNum
< 1)) {
956 Error (NULL
, 0, 2000, "Invalid parameter", "Fv File type %s must have at least one Pe or Te section, but no Pe/Te section is input", mFfsFileType
[FfsFiletype
]);
960 if (Status
== EFI_BUFFER_TOO_SMALL
) {
961 FileBuffer
= (UINT8
*) malloc (FileSize
);
962 if (FileBuffer
== NULL
) {
963 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
966 memset (FileBuffer
, 0, FileSize
);
969 // read all input file contents into a buffer
971 Status
= GetSectionContents (
983 if (EFI_ERROR (Status
)) {
987 if (FileBuffer
== NULL
&& FileSize
!= 0) {
988 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
993 // Create Ffs file header.
995 memset (&FfsFileHeader
, 0, sizeof (EFI_FFS_FILE_HEADER2
));
996 memcpy (&FfsFileHeader
.Name
, &FileGuid
, sizeof (EFI_GUID
));
997 FfsFileHeader
.Type
= FfsFiletype
;
999 // Update FFS Alignment based on the max alignment required by input section files
1001 VerboseMsg ("the max alignment of all input sections is %u", (unsigned) MaxAlignment
);
1002 for (Index
= 0; Index
< sizeof (mFfsValidAlign
) / sizeof (UINT32
) - 1; Index
++) {
1003 if ((MaxAlignment
> mFfsValidAlign
[Index
]) && (MaxAlignment
<= mFfsValidAlign
[Index
+ 1])) {
1007 if (FfsAlign
< Index
) {
1010 VerboseMsg ("the alignment of the generated FFS file is %u", (unsigned) mFfsValidAlign
[FfsAlign
+ 1]);
1013 // Now FileSize includes the EFI_FFS_FILE_HEADER
1015 if (FileSize
+ sizeof (EFI_FFS_FILE_HEADER
) >= MAX_FFS_SIZE
) {
1016 HeaderSize
= sizeof (EFI_FFS_FILE_HEADER2
);
1017 FileSize
+= sizeof (EFI_FFS_FILE_HEADER2
);
1018 FfsFileHeader
.ExtendedSize
= FileSize
;
1019 memset(FfsFileHeader
.Size
, 0, sizeof (UINT8
) * 3);
1020 FfsAttrib
|= FFS_ATTRIB_LARGE_FILE
;
1022 HeaderSize
= sizeof (EFI_FFS_FILE_HEADER
);
1023 FileSize
+= sizeof (EFI_FFS_FILE_HEADER
);
1024 FfsFileHeader
.Size
[0] = (UINT8
) (FileSize
& 0xFF);
1025 FfsFileHeader
.Size
[1] = (UINT8
) ((FileSize
& 0xFF00) >> 8);
1026 FfsFileHeader
.Size
[2] = (UINT8
) ((FileSize
& 0xFF0000) >> 16);
1028 VerboseMsg ("the size of the generated FFS file is %u bytes", (unsigned) FileSize
);
1030 //FfsAlign larger than 7, set FFS_ATTRIB_DATA_ALIGNMENT2
1032 FfsFileHeader
.Attributes
= (EFI_FFS_FILE_ATTRIBUTES
) (FfsAttrib
| (FfsAlign
<< 3));
1034 FfsFileHeader
.Attributes
= (EFI_FFS_FILE_ATTRIBUTES
) (FfsAttrib
| ((FfsAlign
& 0x7) << 3) | FFS_ATTRIB_DATA_ALIGNMENT2
);
1038 // Fill in checksums and state, these must be zero for checksumming
1040 // FileHeader.IntegrityCheck.Checksum.Header = 0;
1041 // FileHeader.IntegrityCheck.Checksum.File = 0;
1042 // FileHeader.State = 0;
1044 FfsFileHeader
.IntegrityCheck
.Checksum
.Header
= CalculateChecksum8 (
1045 (UINT8
*) &FfsFileHeader
,
1049 if (FfsFileHeader
.Attributes
& FFS_ATTRIB_CHECKSUM
) {
1051 // Ffs header checksum = zero, so only need to calculate ffs body.
1053 FfsFileHeader
.IntegrityCheck
.Checksum
.File
= CalculateChecksum8 (
1055 FileSize
- HeaderSize
1058 FfsFileHeader
.IntegrityCheck
.Checksum
.File
= FFS_FIXED_CHECKSUM
;
1061 FfsFileHeader
.State
= EFI_FILE_HEADER_CONSTRUCTION
| EFI_FILE_HEADER_VALID
| EFI_FILE_DATA_VALID
;
1064 // Open output file to write ffs data.
1066 if (OutputFileName
!= NULL
) {
1067 remove(OutputFileName
);
1068 FfsFile
= fopen (LongFilePath (OutputFileName
), "wb");
1069 if (FfsFile
== NULL
) {
1070 Error (NULL
, 0, 0001, "Error opening file", OutputFileName
);
1076 fwrite (&FfsFileHeader
, 1, HeaderSize
, FfsFile
);
1080 if (FileBuffer
!= NULL
) {
1081 fwrite (FileBuffer
, 1, FileSize
- HeaderSize
, FfsFile
);
1088 if (InputFileName
!= NULL
) {
1089 free (InputFileName
);
1091 if (InputFileAlign
!= NULL
) {
1092 free (InputFileAlign
);
1094 if (FileBuffer
!= NULL
) {
1098 // If any errors were reported via the standard error reporting
1099 // routines, then the status has been saved. Get the value and
1100 // return it to the caller.
1102 VerboseMsg ("%s tool done with return code is 0x%x.", UTILITY_NAME
, GetUtilityStatus ());
1104 return GetUtilityStatus ();