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 - 2018, 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 - 2018, 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 if (strlen (argv
[1]) > MAX_LONG_FILE_PATH
- 1) {
341 Error (NULL
, 0, 1003, "Invalid option value", "Input Ffsfile name %s is too long!", argv
[1]);
344 strncpy (mFvDataInfo
.FvFiles
[Index
], argv
[1], MAX_LONG_FILE_PATH
- 1);
345 mFvDataInfo
.FvFiles
[Index
][MAX_LONG_FILE_PATH
- 1] = 0;
346 DebugMsg (NULL
, 0, 9, "FV component file", "the %uth name is %s", (unsigned) Index
+ 1, argv
[1]);
351 if ((stricmp (argv
[0], "-s") == 0) || (stricmp (argv
[0], "--filetakensize") == 0)) {
352 if (argv
[1] == NULL
) {
353 Error (NULL
, 0, 1003, "Invalid option value", "Ffsfile Size can't be null");
356 Status
= AsciiStringToUint64 (argv
[1], FALSE
, &TempNumber
);
357 if (EFI_ERROR (Status
)) {
358 Error (NULL
, 0, 1003, "Invalid option value", "%s = %s", argv
[0], argv
[1]);
361 mFvDataInfo
.SizeofFvFiles
[Index
] = (UINT32
) TempNumber
;
362 DebugMsg (NULL
, 0, 9, "FV component file size", "the %uth size is %s", (unsigned) Index
+ 1, argv
[1]);
371 if ((stricmp (argv
[0], "-s") == 0) || (stricmp (argv
[0], "--filetakensize") == 0)) {
372 Error (NULL
, 0, 1003, "Invalid option", "It must be specified together with -f option to specify the file size.");
376 if ((stricmp (argv
[0], "-c") == 0) || (stricmp (argv
[0], "--capsule") == 0)) {
383 if ((strcmp (argv
[0], "-F") == 0) || (stricmp (argv
[0], "--force-rebase") == 0)) {
384 if (argv
[1] == NULL
) {
385 Error (NULL
, 0, 1003, "Invalid option value", "Froce rebase flag can't be null");
389 if (stricmp (argv
[1], "TRUE") == 0) {
390 mFvDataInfo
.ForceRebase
= 1;
391 } else if (stricmp (argv
[1], "FALSE") == 0) {
392 mFvDataInfo
.ForceRebase
= 0;
394 Error (NULL
, 0, 1003, "Invalid option value", "froce rebase flag value must be \"TRUE\" or \"FALSE\"");
403 if (stricmp (argv
[0], "--capheadsize") == 0) {
405 // Get Capsule Image Header Size
407 Status
= AsciiStringToUint64 (argv
[1], FALSE
, &TempNumber
);
408 if (EFI_ERROR (Status
)) {
409 Error (NULL
, 0, 1003, "Invalid option value", "%s = %s", argv
[0], argv
[1]);
412 mCapDataInfo
.HeaderSize
= (UINT32
) TempNumber
;
413 DebugMsg (NULL
, 0, 9, "Capsule Header size", "%s = 0x%llx", EFI_CAPSULE_HEADER_SIZE_STRING
, (unsigned long long) TempNumber
);
419 if (stricmp (argv
[0], "--capflag") == 0) {
421 // Get Capsule Header
423 if (argv
[1] == NULL
) {
424 Error (NULL
, 0, 1003, "Option value is not set", "%s = %s", argv
[0], argv
[1]);
427 if (strcmp (argv
[1], "PopulateSystemTable") == 0) {
428 mCapDataInfo
.Flags
|= CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
;
429 } else if (strcmp (argv
[1], "PersistAcrossReset") == 0) {
430 mCapDataInfo
.Flags
|= CAPSULE_FLAGS_PERSIST_ACROSS_RESET
;
431 } else if (strcmp (argv
[1], "InitiateReset") == 0) {
432 mCapDataInfo
.Flags
|= CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_INITIATE_RESET
;
434 Error (NULL
, 0, 1003, "Invalid option value", "%s = %s", argv
[0], argv
[1]);
437 DebugMsg (NULL
, 0, 9, "Capsule Flag", argv
[1]);
443 if (stricmp (argv
[0], "--capoemflag") == 0) {
444 if (argv
[1] == NULL
) {
445 Error (NULL
, 0, 1003, "Invalid option value", "Capsule OEM flag can't be null");
447 Status
= AsciiStringToUint64(argv
[1], FALSE
, &TempNumber
);
448 if (EFI_ERROR (Status
) || TempNumber
> 0xffff) {
449 Error (NULL
, 0, 1003, "Invalid option value", "Capsule OEM flag value must be integer value between 0x0000 and 0xffff");
452 mCapDataInfo
.Flags
|= TempNumber
;
453 DebugMsg( NULL
, 0, 9, "Capsule OEM Flags", argv
[1]);
459 if (stricmp (argv
[0], "--capguid") == 0) {
461 // Get the Capsule Guid
463 Status
= StringToGuid (argv
[1], &mCapDataInfo
.CapGuid
);
464 if (EFI_ERROR (Status
)) {
465 Error (NULL
, 0, 1003, "Invalid option value", "%s = %s", EFI_CAPSULE_GUID_STRING
, argv
[1]);
468 DebugMsg (NULL
, 0, 9, "Capsule Guid", "%s = %s", EFI_CAPSULE_GUID_STRING
, argv
[1]);
474 if ((stricmp (argv
[0], "-g") == 0) || (stricmp (argv
[0], "--guid") == 0)) {
476 // Get the Capsule or Fv Guid
478 Status
= StringToGuid (argv
[1], &mCapDataInfo
.CapGuid
);
479 if (EFI_ERROR (Status
)) {
480 Error (NULL
, 0, 1003, "Invalid option value", "%s = %s", EFI_GUID_STRING
, argv
[1]);
483 memcpy (&mFvDataInfo
.FvFileSystemGuid
, &mCapDataInfo
.CapGuid
, sizeof (EFI_GUID
));
484 mFvDataInfo
.FvFileSystemGuidSet
= TRUE
;
485 DebugMsg (NULL
, 0, 9, "Capsule Guid", "%s = %s", EFI_CAPSULE_GUID_STRING
, argv
[1]);
486 DebugMsg (NULL
, 0, 9, "FV Guid", "%s = %s", EFI_FV_FILESYSTEMGUID_STRING
, argv
[1]);
492 if (stricmp (argv
[0], "--FvNameGuid") == 0) {
496 Status
= StringToGuid (argv
[1], &mFvDataInfo
.FvNameGuid
);
497 if (EFI_ERROR (Status
)) {
498 Error (NULL
, 0, 1003, "Invalid option value", "%s = %s", EFI_GUID_STRING
, argv
[1]);
501 mFvDataInfo
.FvNameGuidSet
= TRUE
;
502 DebugMsg (NULL
, 0, 9, "FV Name Guid", "%s = %s", EFI_FV_NAMEGUID_STRING
, argv
[1]);
508 if ((stricmp (argv
[0], "-p") == 0) || (stricmp (argv
[0], "--dump") == 0)) {
515 if ((stricmp (argv
[0], "-m") == 0) || (stricmp (argv
[0], "--map") == 0)) {
516 MapFileName
= argv
[1];
517 if (MapFileName
== NULL
) {
518 Error (NULL
, 0, 1003, "Invalid option value", "Map file can't be null");
526 if ((stricmp (argv
[0], "-v") == 0) || (stricmp (argv
[0], "--verbose") == 0)) {
527 SetPrintLevel (VERBOSE_LOG_LEVEL
);
528 VerboseMsg ("Verbose output Mode Set!");
534 if ((stricmp (argv
[0], "-q") == 0) || (stricmp (argv
[0], "--quiet") == 0)) {
535 SetPrintLevel (KEY_LOG_LEVEL
);
536 KeyMsg ("Quiet output Mode Set!");
542 if ((stricmp (argv
[0], "-d") == 0) || (stricmp (argv
[0], "--debug") == 0)) {
543 Status
= AsciiStringToUint64 (argv
[1], FALSE
, &LogLevel
);
544 if (EFI_ERROR (Status
)) {
545 Error (NULL
, 0, 1003, "Invalid option value", "%s = %s", argv
[0], argv
[1]);
549 Error (NULL
, 0, 1003, "Invalid option value", "Debug Level range is 0-9, current input level is %d", (int) LogLevel
);
552 SetPrintLevel (LogLevel
);
553 DebugMsg (NULL
, 0, 9, "Debug Mode Set", "Debug Output Mode Level %s is set!", argv
[1]);
560 // Don't recognize the parameter.
562 Error (NULL
, 0, 1000, "Unknown option", "%s", argv
[0]);
566 VerboseMsg ("%s tool start.", UTILITY_NAME
);
569 // check input parameter, InfFileName can be NULL
571 if (InfFileName
== NULL
&& DumpCapsule
) {
572 Error (NULL
, 0, 1001, "Missing option", "Input Capsule Image");
575 VerboseMsg ("the input FvInf or CapInf file name is %s", InfFileName
);
577 if (!DumpCapsule
&& OutFileName
== NULL
) {
578 Error (NULL
, 0, 1001, "Missing option", "Output File");
581 if (OutFileName
!= NULL
) {
582 VerboseMsg ("the output file name is %s", OutFileName
);
586 // Read the INF file image
588 if (InfFileName
!= NULL
) {
589 Status
= GetFileImage (InfFileName
, &InfFileImage
, &InfFileSize
);
590 if (EFI_ERROR (Status
)) {
596 VerboseMsg ("Dump the capsule header information for the input capsule image %s", InfFileName
);
598 // Dump Capsule Image Header Information
600 CapsuleHeader
= (EFI_CAPSULE_HEADER
*) InfFileImage
;
601 if (OutFileName
== NULL
) {
604 FpFile
= fopen (LongFilePath (OutFileName
), "w");
605 if (FpFile
== NULL
) {
606 Error (NULL
, 0, 0001, "Error opening file", OutFileName
);
610 if (FpFile
!= NULL
) {
611 fprintf (FpFile
, "Capsule %s Image Header Information\n", InfFileName
);
612 fprintf (FpFile
, " GUID %08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",
613 (unsigned) CapsuleHeader
->CapsuleGuid
.Data1
,
614 (unsigned) CapsuleHeader
->CapsuleGuid
.Data2
,
615 (unsigned) CapsuleHeader
->CapsuleGuid
.Data3
,
616 (unsigned) CapsuleHeader
->CapsuleGuid
.Data4
[0],
617 (unsigned) CapsuleHeader
->CapsuleGuid
.Data4
[1],
618 (unsigned) CapsuleHeader
->CapsuleGuid
.Data4
[2],
619 (unsigned) CapsuleHeader
->CapsuleGuid
.Data4
[3],
620 (unsigned) CapsuleHeader
->CapsuleGuid
.Data4
[4],
621 (unsigned) CapsuleHeader
->CapsuleGuid
.Data4
[5],
622 (unsigned) CapsuleHeader
->CapsuleGuid
.Data4
[6],
623 (unsigned) CapsuleHeader
->CapsuleGuid
.Data4
[7]);
624 fprintf (FpFile
, " Header size 0x%08X\n", (unsigned) CapsuleHeader
->HeaderSize
);
625 fprintf (FpFile
, " Flags 0x%08X\n", (unsigned) CapsuleHeader
->Flags
);
626 fprintf (FpFile
, " Capsule image size 0x%08X\n", (unsigned) CapsuleHeader
->CapsuleImageSize
);
629 } else if (CapsuleFlag
) {
630 VerboseMsg ("Create capsule image");
632 // Call the GenerateCapImage to generate Capsule Image
634 for (Index
= 0; mFvDataInfo
.FvFiles
[Index
][0] != '\0'; Index
++) {
635 strcpy (mCapDataInfo
.CapFiles
[Index
], mFvDataInfo
.FvFiles
[Index
]);
638 Status
= GenerateCapImage (
644 VerboseMsg ("Create Fv image and its map file");
646 // Will take rebase action at below situation:
647 // 1. ForceRebase Flag specified to TRUE;
648 // 2. ForceRebase Flag not specified, BaseAddress greater than zero.
650 if (((mFvDataInfo
.BaseAddress
> 0) && (mFvDataInfo
.ForceRebase
== -1)) || (mFvDataInfo
.ForceRebase
== 1)) {
651 VerboseMsg ("FvImage Rebase Address is 0x%llX", (unsigned long long) mFvDataInfo
.BaseAddress
);
654 // Call the GenerateFvImage to generate Fv Image
656 Status
= GenerateFvImage (
665 // free InfFileImage memory
667 if (InfFileImage
!= NULL
) {
672 // update boot driver address and runtime driver address in address file
674 if (Status
== EFI_SUCCESS
&& AddrFileName
!= NULL
&& mFvBaseAddressNumber
> 0) {
675 FpFile
= fopen (LongFilePath (AddrFileName
), "w");
676 if (FpFile
== NULL
) {
677 Error (NULL
, 0, 0001, "Error opening file", AddrFileName
);
680 fprintf (FpFile
, FV_BASE_ADDRESS_STRING
);
681 fprintf (FpFile
, "\n");
682 for (Index
= 0; Index
< mFvBaseAddressNumber
; Index
++) {
686 (unsigned long long)mFvBaseAddress
[Index
]
693 if (Status
== EFI_SUCCESS
) {
694 DebugMsg (NULL
, 0, 9, "The Total Fv Size", "%s = 0x%x", EFI_FV_TOTAL_SIZE_STRING
, (unsigned) mFvTotalSize
);
695 DebugMsg (NULL
, 0, 9, "The used Fv Size", "%s = 0x%x", EFI_FV_TAKEN_SIZE_STRING
, (unsigned) mFvTakenSize
);
696 DebugMsg (NULL
, 0, 9, "The space Fv size", "%s = 0x%x", EFI_FV_SPACE_SIZE_STRING
, (unsigned) (mFvTotalSize
- mFvTakenSize
));
699 VerboseMsg ("%s tool done with return code is 0x%x.", UTILITY_NAME
, GetUtilityStatus ());
701 return GetUtilityStatus ();