2 This contains all code necessary to build the GenFvImage.exe utility.
3 This utility relies heavily on the GenFvImage Lib. Definitions for both
4 can be found in the Tiano Firmware Volume Generation Utility
5 Specification, review draft.
7 Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 // File included in build
24 #include "GenFvInternalLib.h"
29 #define UTILITY_NAME "GenFv"
32 // Utility version information
34 #define UTILITY_MAJOR_VERSION 0
35 #define UTILITY_MINOR_VERSION 1
37 EFI_GUID mEfiFirmwareFileSystem2Guid
= EFI_FIRMWARE_FILE_SYSTEM2_GUID
;
38 EFI_GUID mEfiFirmwareFileSystem3Guid
= EFI_FIRMWARE_FILE_SYSTEM3_GUID
;
49 Displays the standard utility information to SDTOUT
61 fprintf (stdout
, "%s Version %d.%d %s \n", UTILITY_NAME
, UTILITY_MAJOR_VERSION
, UTILITY_MINOR_VERSION
, __BUILD_VERSION
);
73 Displays the utility usage syntax to STDOUT
88 fprintf (stdout
, "\nUsage: %s [options]\n\n", UTILITY_NAME
);
91 // Copyright declaration
93 fprintf (stdout
, "Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.\n\n");
98 fprintf (stdout
, "Options:\n");
99 fprintf (stdout
, " -o FileName, --outputfile FileName\n\
100 File is the FvImage or CapImage to be created.\n");
101 fprintf (stdout
, " -i FileName, --inputfile FileName\n\
102 File is the input FV.inf or Cap.inf to specify\n\
103 how to construct FvImage or CapImage.\n");
104 fprintf (stdout
, " -b BlockSize, --blocksize BlockSize\n\
105 BlockSize is one HEX or DEC format value\n\
106 BlockSize is required by Fv Image.\n");
107 fprintf (stdout
, " -n NumberBlock, --numberblock NumberBlock\n\
108 NumberBlock is one HEX or DEC format value\n\
109 NumberBlock is one optional parameter.\n");
110 fprintf (stdout
, " -f FfsFile, --ffsfile FfsFile\n\
111 FfsFile is placed into Fv Image\n\
112 multi files can input one by one\n");
113 fprintf (stdout
, " -s FileTakenSize, --filetakensize FileTakenSize\n\
114 FileTakenSize specifies the size of the required\n\
115 space that the input file is placed in Fvimage.\n\
116 It is specified together with the input file.\n");
117 fprintf (stdout
, " -r Address, --baseaddr Address\n\
118 Address is the rebase start address for drivers that\n\
119 run in Flash. It supports DEC or HEX digital format.\n\
120 If it is set to zero, no rebase action will be taken\n");
121 fprintf (stdout
, " -F ForceRebase, --force-rebase ForceRebase\n\
122 If value is TRUE, will always take rebase action\n\
123 If value is FALSE, will always not take reabse action\n\
124 If not specified, will take rebase action if rebase address greater than zero, \n\
125 will not take rebase action if rebase address is zero.\n");
126 fprintf (stdout
, " -a AddressFile, --addrfile AddressFile\n\
127 AddressFile is one file used to record the child\n\
128 FV base address when current FV base address is set.\n");
129 fprintf (stdout
, " -m logfile, --map logfile\n\
130 Logfile is the output fv map file name. if it is not\n\
131 given, the FvName.map will be the default map file name\n");
132 fprintf (stdout
, " -g Guid, --guid Guid\n\
133 GuidValue is one specific capsule guid value\n\
134 or fv file system guid value.\n\
135 Its format is xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n");
136 fprintf (stdout
, " --FvNameGuid Guid Guid is used to specify Fv Name.\n\
137 Its format is xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n");
138 fprintf (stdout
, " --capflag CapFlag Capsule Reset Flag can be PersistAcrossReset,\n\
139 or PopulateSystemTable or InitiateReset or not set\n");
140 fprintf (stdout
, " --capoemflag CapOEMFlag\n\
141 Capsule OEM Flag is an integer between 0x0000 and 0xffff\n");
142 fprintf (stdout
, " --capheadsize HeadSize\n\
143 HeadSize is one HEX or DEC format value\n\
144 HeadSize is required by Capsule Image.\n");
145 fprintf (stdout
, " -c, --capsule Create Capsule Image.\n");
146 fprintf (stdout
, " -p, --dump Dump Capsule Image header.\n");
147 fprintf (stdout
, " -v, --verbose Turn on verbose output with informational messages.\n");
148 fprintf (stdout
, " -q, --quiet Disable all messages except key message and fatal error\n");
149 fprintf (stdout
, " -d, --debug level Enable debug messages, at input debug level.\n");
150 fprintf (stdout
, " --version Show program's version number and exit.\n");
151 fprintf (stdout
, " -h, --help Show this help message and exit.\n");
166 This utility uses GenFvImage.Lib to build a firmware volume image.
170 FvInfFileName The name of an FV image description file or Capsule Image.
172 Arguments come in pair in any order.
177 EFI_SUCCESS No error conditions detected.
178 EFI_INVALID_PARAMETER One or more of the input parameters is invalid.
179 EFI_OUT_OF_RESOURCES A resource required by the utility was unavailable.
180 Most commonly this will be memory allocation
182 EFI_LOAD_ERROR GenFvImage.lib could not be loaded.
183 EFI_ABORTED Error executing the GenFvImage lib.
197 EFI_CAPSULE_HEADER
*CapsuleHeader
;
198 UINT64 LogLevel
, TempNumber
;
210 CapsuleHeader
= NULL
;
216 Status
= EFI_SUCCESS
;
218 SetUtilityName (UTILITY_NAME
);
221 Error (NULL
, 0, 1001, "Missing options", "No input options specified.");
227 // Init global data to Zero
229 memset (&mFvDataInfo
, 0, sizeof (FV_INFO
));
230 memset (&mCapDataInfo
, 0, sizeof (CAP_INFO
));
232 // Set the default FvGuid
234 memcpy (&mFvDataInfo
.FvFileSystemGuid
, &mEfiFirmwareFileSystem2Guid
, sizeof (EFI_GUID
));
235 mFvDataInfo
.ForceRebase
= -1;
238 // Parse command line
243 if ((stricmp (argv
[0], "-h") == 0) || (stricmp (argv
[0], "--help") == 0)) {
246 return STATUS_SUCCESS
;
249 if (stricmp (argv
[0], "--version") == 0) {
251 return STATUS_SUCCESS
;
255 if ((stricmp (argv
[0], "-i") == 0) || (stricmp (argv
[0], "--inputfile") == 0)) {
256 InfFileName
= argv
[1];
257 if (InfFileName
== NULL
) {
258 Error (NULL
, 0, 1003, "Invalid option value", "Input file can't be null");
266 if ((stricmp (argv
[0], "-a") == 0) || (stricmp (argv
[0], "--addrfile") == 0)) {
267 AddrFileName
= argv
[1];
268 if (AddrFileName
== NULL
) {
269 Error (NULL
, 0, 1003, "Invalid option value", "Address file can't be null");
277 if ((stricmp (argv
[0], "-o") == 0) || (stricmp (argv
[0], "--outputfile") == 0)) {
278 OutFileName
= argv
[1];
279 if (OutFileName
== NULL
) {
280 Error (NULL
, 0, 1003, "Invalid option value", "Output file can't be null");
288 if ((stricmp (argv
[0], "-r") == 0) || (stricmp (argv
[0], "--baseaddr") == 0)) {
289 Status
= AsciiStringToUint64 (argv
[1], FALSE
, &TempNumber
);
290 if (EFI_ERROR (Status
)) {
291 Error (NULL
, 0, 1003, "Invalid option value", "%s = %s", argv
[0], argv
[1]);
294 mFvDataInfo
.BaseAddress
= TempNumber
;
295 mFvDataInfo
.BaseAddressSet
= TRUE
;
301 if ((stricmp (argv
[0], "-b") == 0) || (stricmp (argv
[0], "--blocksize") == 0)) {
302 Status
= AsciiStringToUint64 (argv
[1], FALSE
, &TempNumber
);
303 if (EFI_ERROR (Status
)) {
304 Error (NULL
, 0, 1003, "Invalid option value", "%s = %s", argv
[0], argv
[1]);
307 if (TempNumber
== 0) {
308 Error (NULL
, 0, 1003, "Invalid option value", "Fv block size can't be be set to zero");
311 mFvDataInfo
.FvBlocks
[0].Length
= (UINT32
) TempNumber
;
312 DebugMsg (NULL
, 0, 9, "FV Block Size", "%s = 0x%llx", EFI_BLOCK_SIZE_STRING
, (unsigned long long) TempNumber
);
318 if ((stricmp (argv
[0], "-n") == 0) || (stricmp (argv
[0], "--numberblock") == 0)) {
319 Status
= AsciiStringToUint64 (argv
[1], FALSE
, &TempNumber
);
320 if (EFI_ERROR (Status
)) {
321 Error (NULL
, 0, 1003, "Invalid option value", "%s = %s", argv
[0], argv
[1]);
324 if (TempNumber
== 0) {
325 Error (NULL
, 0, 1003, "Invalid option value", "Fv block number can't be set to zero");
328 mFvDataInfo
.FvBlocks
[0].NumBlocks
= (UINT32
) TempNumber
;
329 DebugMsg (NULL
, 0, 9, "FV Number Block", "%s = 0x%llx", EFI_NUM_BLOCKS_STRING
, (unsigned long long) TempNumber
);
335 if ((strcmp (argv
[0], "-f") == 0) || (stricmp (argv
[0], "--ffsfile") == 0)) {
336 if (argv
[1] == NULL
) {
337 Error (NULL
, 0, 1003, "Invalid option value", "Input Ffsfile can't be null");
340 strcpy (mFvDataInfo
.FvFiles
[Index
], argv
[1]);
341 DebugMsg (NULL
, 0, 9, "FV component file", "the %uth name is %s", (unsigned) Index
+ 1, argv
[1]);
346 if ((stricmp (argv
[0], "-s") == 0) || (stricmp (argv
[0], "--filetakensize") == 0)) {
347 if (argv
[1] == NULL
) {
348 Error (NULL
, 0, 1003, "Invalid option value", "Ffsfile Size can't be null");
351 Status
= AsciiStringToUint64 (argv
[1], FALSE
, &TempNumber
);
352 if (EFI_ERROR (Status
)) {
353 Error (NULL
, 0, 1003, "Invalid option value", "%s = %s", argv
[0], argv
[1]);
356 mFvDataInfo
.SizeofFvFiles
[Index
] = (UINT32
) TempNumber
;
357 DebugMsg (NULL
, 0, 9, "FV component file size", "the %uth size is %s", (unsigned) Index
+ 1, argv
[1]);
366 if ((stricmp (argv
[0], "-s") == 0) || (stricmp (argv
[0], "--filetakensize") == 0)) {
367 Error (NULL
, 0, 1003, "Invalid option", "It must be specified together with -f option to specify the file size.");
371 if ((stricmp (argv
[0], "-c") == 0) || (stricmp (argv
[0], "--capsule") == 0)) {
378 if ((strcmp (argv
[0], "-F") == 0) || (stricmp (argv
[0], "--force-rebase") == 0)) {
379 if (argv
[1] == NULL
) {
380 Error (NULL
, 0, 1003, "Invalid option value", "Froce rebase flag can't be null");
384 if (stricmp (argv
[1], "TRUE") == 0) {
385 mFvDataInfo
.ForceRebase
= 1;
386 } else if (stricmp (argv
[1], "FALSE") == 0) {
387 mFvDataInfo
.ForceRebase
= 0;
389 Error (NULL
, 0, 1003, "Invalid option value", "froce rebase flag value must be \"TRUE\" or \"FALSE\"");
398 if (stricmp (argv
[0], "--capheadsize") == 0) {
400 // Get Capsule Image Header Size
402 Status
= AsciiStringToUint64 (argv
[1], FALSE
, &TempNumber
);
403 if (EFI_ERROR (Status
)) {
404 Error (NULL
, 0, 1003, "Invalid option value", "%s = %s", argv
[0], argv
[1]);
407 mCapDataInfo
.HeaderSize
= (UINT32
) TempNumber
;
408 DebugMsg (NULL
, 0, 9, "Capsule Header size", "%s = 0x%llx", EFI_CAPSULE_HEADER_SIZE_STRING
, (unsigned long long) TempNumber
);
414 if (stricmp (argv
[0], "--capflag") == 0) {
416 // Get Capsule Header
418 if (argv
[1] == NULL
) {
419 Error (NULL
, 0, 1003, "Option value is not set", "%s = %s", argv
[0], argv
[1]);
422 if (strcmp (argv
[1], "PopulateSystemTable") == 0) {
423 mCapDataInfo
.Flags
|= CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
;
424 } else if (strcmp (argv
[1], "PersistAcrossReset") == 0) {
425 mCapDataInfo
.Flags
|= CAPSULE_FLAGS_PERSIST_ACROSS_RESET
;
426 } else if (strcmp (argv
[1], "InitiateReset") == 0) {
427 mCapDataInfo
.Flags
|= CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_INITIATE_RESET
;
429 Error (NULL
, 0, 1003, "Invalid option value", "%s = %s", argv
[0], argv
[1]);
432 DebugMsg (NULL
, 0, 9, "Capsule Flag", argv
[1]);
438 if (stricmp (argv
[0], "--capoemflag") == 0) {
439 if (argv
[1] == NULL
) {
440 Error (NULL
, 0, 1003, "Invalid option value", "Capsule OEM flag can't be null");
442 Status
= AsciiStringToUint64(argv
[1], FALSE
, &TempNumber
);
443 if (EFI_ERROR (Status
) || TempNumber
> 0xffff) {
444 Error (NULL
, 0, 1003, "Invalid option value", "Capsule OEM flag value must be integer value between 0x0000 and 0xffff");
447 mCapDataInfo
.Flags
|= TempNumber
;
448 DebugMsg( NULL
, 0, 9, "Capsule OEM Flags", argv
[1]);
454 if (stricmp (argv
[0], "--capguid") == 0) {
456 // Get the Capsule Guid
458 Status
= StringToGuid (argv
[1], &mCapDataInfo
.CapGuid
);
459 if (EFI_ERROR (Status
)) {
460 Error (NULL
, 0, 1003, "Invalid option value", "%s = %s", EFI_CAPSULE_GUID_STRING
, argv
[1]);
463 DebugMsg (NULL
, 0, 9, "Capsule Guid", "%s = %s", EFI_CAPSULE_GUID_STRING
, argv
[1]);
469 if ((stricmp (argv
[0], "-g") == 0) || (stricmp (argv
[0], "--guid") == 0)) {
471 // Get the Capsule or Fv Guid
473 Status
= StringToGuid (argv
[1], &mCapDataInfo
.CapGuid
);
474 if (EFI_ERROR (Status
)) {
475 Error (NULL
, 0, 1003, "Invalid option value", "%s = %s", EFI_GUID_STRING
, argv
[1]);
478 memcpy (&mFvDataInfo
.FvFileSystemGuid
, &mCapDataInfo
.CapGuid
, sizeof (EFI_GUID
));
479 mFvDataInfo
.FvFileSystemGuidSet
= TRUE
;
480 DebugMsg (NULL
, 0, 9, "Capsule Guid", "%s = %s", EFI_CAPSULE_GUID_STRING
, argv
[1]);
481 DebugMsg (NULL
, 0, 9, "FV Guid", "%s = %s", EFI_FV_FILESYSTEMGUID_STRING
, argv
[1]);
487 if (stricmp (argv
[0], "--FvNameGuid") == 0) {
491 Status
= StringToGuid (argv
[1], &mFvDataInfo
.FvNameGuid
);
492 if (EFI_ERROR (Status
)) {
493 Error (NULL
, 0, 1003, "Invalid option value", "%s = %s", EFI_GUID_STRING
, argv
[1]);
496 mFvDataInfo
.FvNameGuidSet
= TRUE
;
497 DebugMsg (NULL
, 0, 9, "FV Name Guid", "%s = %s", EFI_FV_NAMEGUID_STRING
, argv
[1]);
503 if ((stricmp (argv
[0], "-p") == 0) || (stricmp (argv
[0], "--dump") == 0)) {
510 if ((stricmp (argv
[0], "-m") == 0) || (stricmp (argv
[0], "--map") == 0)) {
511 MapFileName
= argv
[1];
512 if (MapFileName
== NULL
) {
513 Error (NULL
, 0, 1003, "Invalid option value", "Map file can't be null");
521 if ((stricmp (argv
[0], "-v") == 0) || (stricmp (argv
[0], "--verbose") == 0)) {
522 SetPrintLevel (VERBOSE_LOG_LEVEL
);
523 VerboseMsg ("Verbose output Mode Set!");
529 if ((stricmp (argv
[0], "-q") == 0) || (stricmp (argv
[0], "--quiet") == 0)) {
530 SetPrintLevel (KEY_LOG_LEVEL
);
531 KeyMsg ("Quiet output Mode Set!");
537 if ((stricmp (argv
[0], "-d") == 0) || (stricmp (argv
[0], "--debug") == 0)) {
538 Status
= AsciiStringToUint64 (argv
[1], FALSE
, &LogLevel
);
539 if (EFI_ERROR (Status
)) {
540 Error (NULL
, 0, 1003, "Invalid option value", "%s = %s", argv
[0], argv
[1]);
544 Error (NULL
, 0, 1003, "Invalid option value", "Debug Level range is 0-9, current input level is %d", (int) LogLevel
);
547 SetPrintLevel (LogLevel
);
548 DebugMsg (NULL
, 0, 9, "Debug Mode Set", "Debug Output Mode Level %s is set!", argv
[1]);
555 // Don't recognize the parameter.
557 Error (NULL
, 0, 1000, "Unknown option", "%s", argv
[0]);
561 VerboseMsg ("%s tool start.", UTILITY_NAME
);
564 // check input parameter, InfFileName can be NULL
566 if (InfFileName
== NULL
&& DumpCapsule
) {
567 Error (NULL
, 0, 1001, "Missing option", "Input Capsule Image");
570 VerboseMsg ("the input FvInf or CapInf file name is %s", InfFileName
);
572 if (!DumpCapsule
&& OutFileName
== NULL
) {
573 Error (NULL
, 0, 1001, "Missing option", "Output File");
576 if (OutFileName
!= NULL
) {
577 VerboseMsg ("the output file name is %s", OutFileName
);
581 // Read the INF file image
583 if (InfFileName
!= NULL
) {
584 Status
= GetFileImage (InfFileName
, &InfFileImage
, &InfFileSize
);
585 if (EFI_ERROR (Status
)) {
591 VerboseMsg ("Dump the capsule header information for the input capsule image %s", InfFileName
);
593 // Dump Capsule Image Header Information
595 CapsuleHeader
= (EFI_CAPSULE_HEADER
*) InfFileImage
;
596 if (OutFileName
== NULL
) {
599 FpFile
= fopen (LongFilePath (OutFileName
), "w");
600 if (FpFile
== NULL
) {
601 Error (NULL
, 0, 0001, "Error opening file", OutFileName
);
605 fprintf (FpFile
, "Capsule %s Image Header Information\n", InfFileName
);
606 fprintf (FpFile
, " GUID %08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",
607 (unsigned) CapsuleHeader
->CapsuleGuid
.Data1
,
608 (unsigned) CapsuleHeader
->CapsuleGuid
.Data2
,
609 (unsigned) CapsuleHeader
->CapsuleGuid
.Data3
,
610 (unsigned) CapsuleHeader
->CapsuleGuid
.Data4
[0],
611 (unsigned) CapsuleHeader
->CapsuleGuid
.Data4
[1],
612 (unsigned) CapsuleHeader
->CapsuleGuid
.Data4
[2],
613 (unsigned) CapsuleHeader
->CapsuleGuid
.Data4
[3],
614 (unsigned) CapsuleHeader
->CapsuleGuid
.Data4
[4],
615 (unsigned) CapsuleHeader
->CapsuleGuid
.Data4
[5],
616 (unsigned) CapsuleHeader
->CapsuleGuid
.Data4
[6],
617 (unsigned) CapsuleHeader
->CapsuleGuid
.Data4
[7]);
618 fprintf (FpFile
, " Header size 0x%08X\n", (unsigned) CapsuleHeader
->HeaderSize
);
619 fprintf (FpFile
, " Flags 0x%08X\n", (unsigned) CapsuleHeader
->Flags
);
620 fprintf (FpFile
, " Capsule image size 0x%08X\n", (unsigned) CapsuleHeader
->CapsuleImageSize
);
622 } else if (CapsuleFlag
) {
623 VerboseMsg ("Create capsule image");
625 // Call the GenerateCapImage to generate Capsule Image
627 for (Index
= 0; mFvDataInfo
.FvFiles
[Index
][0] != '\0'; Index
++) {
628 strcpy (mCapDataInfo
.CapFiles
[Index
], mFvDataInfo
.FvFiles
[Index
]);
631 Status
= GenerateCapImage (
637 VerboseMsg ("Create Fv image and its map file");
639 // Will take rebase action at below situation:
640 // 1. ForceRebase Flag specified to TRUE;
641 // 2. ForceRebase Flag not specified, BaseAddress greater than zero.
643 if (((mFvDataInfo
.BaseAddress
> 0) && (mFvDataInfo
.ForceRebase
== -1)) || (mFvDataInfo
.ForceRebase
== 1)) {
644 VerboseMsg ("FvImage Rebase Address is 0x%llX", (unsigned long long) mFvDataInfo
.BaseAddress
);
647 // Call the GenerateFvImage to generate Fv Image
649 Status
= GenerateFvImage (
658 // free InfFileImage memory
660 if (InfFileImage
!= NULL
) {
665 // update boot driver address and runtime driver address in address file
667 if (Status
== EFI_SUCCESS
&& AddrFileName
!= NULL
&& mFvBaseAddressNumber
> 0) {
668 FpFile
= fopen (LongFilePath (AddrFileName
), "w");
669 if (FpFile
== NULL
) {
670 Error (NULL
, 0, 0001, "Error opening file", AddrFileName
);
673 fprintf (FpFile
, FV_BASE_ADDRESS_STRING
);
674 fprintf (FpFile
, "\n");
675 for (Index
= 0; Index
< mFvBaseAddressNumber
; Index
++) {
679 (unsigned long long)mFvBaseAddress
[Index
]
686 if (Status
== EFI_SUCCESS
) {
687 DebugMsg (NULL
, 0, 9, "The Total Fv Size", "%s = 0x%x", EFI_FV_TOTAL_SIZE_STRING
, (unsigned) mFvTotalSize
);
688 DebugMsg (NULL
, 0, 9, "The used Fv Size", "%s = 0x%x", EFI_FV_TAKEN_SIZE_STRING
, (unsigned) mFvTakenSize
);
689 DebugMsg (NULL
, 0, 9, "The space Fv size", "%s = 0x%x", EFI_FV_SPACE_SIZE_STRING
, (unsigned) (mFvTotalSize
- mFvTakenSize
));
692 VerboseMsg ("%s tool done with return code is 0x%x.", UTILITY_NAME
, GetUtilityStatus ());
694 return GetUtilityStatus ();