3 VfrCompiler main class and main function.
5 Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
13 #include "VfrCompiler.h"
14 #include "CommonLib.h"
15 #include "EfiUtilityMsgs.h"
17 PACKAGE_DATA gCBuffer
;
18 PACKAGE_DATA gRBuffer
;
19 CVfrStringDB gCVfrStringDB
;
22 CVfrCompiler::DebugError (
25 IN UINT32 MessageCode
,
27 IN CONST CHAR8
*MsgFmt
,
32 va_start (List
, MsgFmt
);
33 PrintMessage ((CHAR8
*) "ERROR", FileName
, LineNumber
, MessageCode
, (CHAR8
*) Text
, (CHAR8
*) MsgFmt
, List
);
38 CVfrCompiler::SET_RUN_STATUS (
39 IN COMPILER_RUN_STATUS Status
46 CVfrCompiler::IS_RUN_STATUS (
47 IN COMPILER_RUN_STATUS Status
50 return mRunStatus
== Status
;
54 CVfrCompiler::OptionInitialization (
63 SetUtilityName ((CHAR8
*) PROGRAM_NAME
);
65 mOptions
.VfrFileName
= NULL
;
66 mOptions
.RecordListFile
= NULL
;
67 mOptions
.CreateRecordListFile
= FALSE
;
68 mOptions
.CreateIfrPkgFile
= FALSE
;
69 mOptions
.PkgOutputFileName
= NULL
;
70 mOptions
.COutputFileName
= NULL
;
71 mOptions
.OutputDirectory
= NULL
;
72 mOptions
.PreprocessorOutputFileName
= NULL
;
73 mOptions
.VfrBaseFileName
= NULL
;
74 mOptions
.IncludePaths
= NULL
;
75 mOptions
.SkipCPreprocessor
= TRUE
;
76 mOptions
.CPreprocessorOptions
= NULL
;
77 mOptions
.CompatibleMode
= FALSE
;
78 mOptions
.HasOverrideClassGuid
= FALSE
;
79 mOptions
.WarningAsError
= FALSE
;
80 mOptions
.AutoDefault
= FALSE
;
81 mOptions
.CheckDefault
= FALSE
;
82 memset (&mOptions
.OverrideClassGuid
, 0, sizeof (EFI_GUID
));
86 SET_RUN_STATUS (STATUS_DEAD
);
90 for (Index
= 1; (Index
< Argc
) && (Argv
[Index
][0] == '-'); Index
++) {
91 if ((stricmp(Argv
[Index
], "-h") == 0) || (stricmp(Argv
[Index
], "--help") == 0)) {
93 SET_RUN_STATUS (STATUS_DEAD
);
95 } else if (stricmp(Argv
[Index
], "--version") == 0) {
97 SET_RUN_STATUS (STATUS_DEAD
);
99 } else if (stricmp(Argv
[Index
], "-l") == 0) {
100 mOptions
.CreateRecordListFile
= TRUE
;
101 gCIfrRecordInfoDB
.TurnOn ();
102 } else if (stricmp(Argv
[Index
], "-i") == 0) {
104 if ((Index
>= Argc
) || (Argv
[Index
][0] == '-')) {
105 DebugError (NULL
, 0, 1001, "Missing option", "-i missing path argument");
109 AppendIncludePath(Argv
[Index
]);
110 } else if (stricmp(Argv
[Index
], "-o") == 0 || stricmp(Argv
[Index
], "--output-directory") == 0 || stricmp(Argv
[Index
], "-od") == 0) {
112 if ((Index
>= Argc
) || (Argv
[Index
][0] == '-')) {
113 DebugError (NULL
, 0, 1001, "Missing option", "-o missing output directory name");
117 mOptions
.OutputDirectory
= (CHAR8
*) malloc (strlen (Argv
[Index
]) + strlen ("\\") + 1);
118 if (mOptions
.OutputDirectory
== NULL
) {
119 DebugError (NULL
, 0, 4001, "Resource: memory can't be allocated", NULL
);
122 strcpy (mOptions
.OutputDirectory
, Argv
[Index
]);
124 CHAR8 lastChar
= mOptions
.OutputDirectory
[strlen(mOptions
.OutputDirectory
) - 1];
125 if ((lastChar
!= '/') && (lastChar
!= '\\')) {
126 if (strchr(mOptions
.OutputDirectory
, '/') != NULL
) {
127 strcat (mOptions
.OutputDirectory
, "/");
129 strcat (mOptions
.OutputDirectory
, "\\");
132 DebugMsg (NULL
, 0, 9, (CHAR8
*) "Output Directory", (CHAR8
*) "%s", mOptions
.OutputDirectory
);
133 } else if (stricmp(Argv
[Index
], "-b") == 0 || stricmp(Argv
[Index
], "--create-ifr-package") == 0 || stricmp(Argv
[Index
], "-ibin") == 0) {
134 mOptions
.CreateIfrPkgFile
= TRUE
;
135 } else if (stricmp(Argv
[Index
], "-n") == 0 || stricmp(Argv
[Index
], "--no-pre-processing") == 0 || stricmp(Argv
[Index
], "-nopp") == 0) {
136 mOptions
.SkipCPreprocessor
= TRUE
;
137 } else if (stricmp(Argv
[Index
], "-f") == 0 || stricmp(Argv
[Index
], "--pre-processing-flag") == 0 || stricmp(Argv
[Index
], "-ppflag") == 0) {
139 if ((Index
>= Argc
) || (Argv
[Index
][0] == '-')) {
140 DebugError (NULL
, 0, 1001, "Missing option", "-od - missing C-preprocessor argument");
144 AppendCPreprocessorOptions (Argv
[Index
]);
145 } else if (stricmp(Argv
[Index
], "-c") == 0 || stricmp(Argv
[Index
], "--compatible-framework") == 0) {
146 mOptions
.CompatibleMode
= TRUE
;
147 } else if (stricmp(Argv
[Index
], "-s") == 0|| stricmp(Argv
[Index
], "--string-db") == 0) {
149 if ((Index
>= Argc
) || (Argv
[Index
][0] == '-')) {
150 DebugError (NULL
, 0, 1001, "Missing option", "-s missing input string file name");
153 gCVfrStringDB
.SetStringFileName(Argv
[Index
]);
154 DebugMsg (NULL
, 0, 9, (CHAR8
*) "Input string file path", (CHAR8
*) "%s", Argv
[Index
]);
155 } else if ((stricmp (Argv
[Index
], "-g") == 0) || (stricmp (Argv
[Index
], "--guid") == 0)) {
157 Status
= StringToGuid (Argv
[Index
], &mOptions
.OverrideClassGuid
);
158 if (EFI_ERROR (Status
)) {
159 DebugError (NULL
, 0, 1000, "Invalid format:", "%s", Argv
[Index
]);
162 mOptions
.HasOverrideClassGuid
= TRUE
;
163 } else if (stricmp(Argv
[Index
], "-w") == 0 || stricmp(Argv
[Index
], "--warning-as-error") == 0) {
164 mOptions
.WarningAsError
= TRUE
;
165 } else if (stricmp(Argv
[Index
], "-a") == 0 ||stricmp(Argv
[Index
], "--autodefault") == 0) {
166 mOptions
.AutoDefault
= TRUE
;
167 } else if (stricmp(Argv
[Index
], "-d") == 0 ||stricmp(Argv
[Index
], "--checkdefault") == 0) {
168 mOptions
.CheckDefault
= TRUE
;
170 DebugError (NULL
, 0, 1000, "Unknown option", "unrecognized option %s", Argv
[Index
]);
175 if (Index
!= Argc
- 1) {
176 DebugError (NULL
, 0, 1001, "Missing option", "VFR file name is not specified.");
179 mOptions
.VfrFileName
= (CHAR8
*) malloc (strlen (Argv
[Index
]) + 1);
180 if (mOptions
.VfrFileName
== NULL
) {
181 DebugError (NULL
, 0, 4001, "Resource: memory can't be allocated", NULL
);
184 strcpy (mOptions
.VfrFileName
, Argv
[Index
]);
186 if (mOptions
.OutputDirectory
== NULL
) {
187 mOptions
.OutputDirectory
= (CHAR8
*) malloc (1);
188 if (mOptions
.OutputDirectory
== NULL
) {
189 DebugError (NULL
, 0, 4001, "Resource: memory can't be allocated", NULL
);
192 mOptions
.OutputDirectory
[0] = '\0';
196 if (SetBaseFileName() != 0) {
199 if (SetPkgOutputFileName () != 0) {
202 if (SetCOutputFileName() != 0) {
205 if (SetPreprocessorOutputFileName () != 0) {
208 if (SetRecordListFileName () != 0) {
214 SET_RUN_STATUS (STATUS_DEAD
);
216 mOptions
.CreateRecordListFile
= FALSE
;
217 mOptions
.CreateIfrPkgFile
= FALSE
;
219 if (mOptions
.VfrFileName
!= NULL
) {
220 free (mOptions
.VfrFileName
);
221 mOptions
.VfrFileName
= NULL
;
223 if (mOptions
.VfrBaseFileName
!= NULL
) {
224 free (mOptions
.VfrBaseFileName
);
225 mOptions
.VfrBaseFileName
= NULL
;
227 if (mOptions
.OutputDirectory
!= NULL
) {
228 free (mOptions
.OutputDirectory
);
229 mOptions
.OutputDirectory
= NULL
;
231 if (mOptions
.PkgOutputFileName
!= NULL
) {
232 free (mOptions
.PkgOutputFileName
);
233 mOptions
.PkgOutputFileName
= NULL
;
235 if (mOptions
.COutputFileName
!= NULL
) {
236 free (mOptions
.COutputFileName
);
237 mOptions
.COutputFileName
= NULL
;
239 if (mOptions
.PreprocessorOutputFileName
!= NULL
) {
240 free (mOptions
.PreprocessorOutputFileName
);
241 mOptions
.PreprocessorOutputFileName
= NULL
;
243 if (mOptions
.RecordListFile
!= NULL
) {
244 free (mOptions
.RecordListFile
);
245 mOptions
.RecordListFile
= NULL
;
247 if (mOptions
.IncludePaths
!= NULL
) {
248 delete mOptions
.IncludePaths
;
249 mOptions
.IncludePaths
= NULL
;
251 if (mOptions
.CPreprocessorOptions
!= NULL
) {
252 delete mOptions
.CPreprocessorOptions
;
253 mOptions
.CPreprocessorOptions
= NULL
;
258 CVfrCompiler::AppendIncludePath (
263 CHAR8
*IncludePaths
= NULL
;
265 Len
= strlen (" -I ") + strlen (PathStr
) + 1;
266 if (mOptions
.IncludePaths
!= NULL
) {
267 Len
+= strlen (mOptions
.IncludePaths
);
269 IncludePaths
= new CHAR8
[Len
];
270 if (IncludePaths
== NULL
) {
271 DebugError (NULL
, 0, 4001, "Resource: memory can't be allocated", NULL
);
274 IncludePaths
[0] = '\0';
275 if (mOptions
.IncludePaths
!= NULL
) {
276 strcat (IncludePaths
, mOptions
.IncludePaths
);
278 strcat (IncludePaths
, " -I ");
279 strcat (IncludePaths
, PathStr
);
280 if (mOptions
.IncludePaths
!= NULL
) {
281 delete[] mOptions
.IncludePaths
;
283 mOptions
.IncludePaths
= IncludePaths
;
287 CVfrCompiler::AppendCPreprocessorOptions (
294 Len
= strlen (Options
) + strlen (" ") + 1;
295 if (mOptions
.CPreprocessorOptions
!= NULL
) {
296 Len
+= strlen (mOptions
.CPreprocessorOptions
);
298 Opt
= new CHAR8
[Len
];
300 DebugError (NULL
, 0, 4001, "Resource: memory can't be allocated", NULL
);
304 if (mOptions
.CPreprocessorOptions
!= NULL
) {
305 strcat (Opt
, mOptions
.CPreprocessorOptions
);
308 strcat (Opt
, Options
);
309 if (mOptions
.CPreprocessorOptions
!= NULL
) {
310 delete[] mOptions
.CPreprocessorOptions
;
312 mOptions
.CPreprocessorOptions
= Opt
;
316 CVfrCompiler::SetBaseFileName (
320 CHAR8
*pFileName
, *pPath
, *pExt
;
322 if (mOptions
.VfrFileName
== NULL
) {
326 pFileName
= mOptions
.VfrFileName
;
328 ((pPath
= strchr (pFileName
, '\\')) != NULL
) ||
329 ((pPath
= strchr (pFileName
, '/')) != NULL
)
332 pFileName
= pPath
+ 1;
335 if (pFileName
== NULL
) {
339 if ((pExt
= strchr (pFileName
, '.')) == NULL
) {
345 mOptions
.VfrBaseFileName
= (CHAR8
*) malloc (strlen (pFileName
) + 1);
346 if (mOptions
.VfrBaseFileName
== NULL
) {
351 strcpy (mOptions
.VfrBaseFileName
, pFileName
);
358 CVfrCompiler::SetPkgOutputFileName (
364 if (mOptions
.VfrBaseFileName
== NULL
) {
368 Length
= strlen (mOptions
.OutputDirectory
) +
369 strlen (mOptions
.VfrBaseFileName
) +
370 strlen (VFR_PACKAGE_FILENAME_EXTENSION
) +
373 mOptions
.PkgOutputFileName
= (CHAR8
*) malloc (Length
);
374 if (mOptions
.PkgOutputFileName
== NULL
) {
378 strcpy (mOptions
.PkgOutputFileName
, mOptions
.OutputDirectory
);
379 strcat (mOptions
.PkgOutputFileName
, mOptions
.VfrBaseFileName
);
380 strcat (mOptions
.PkgOutputFileName
, VFR_PACKAGE_FILENAME_EXTENSION
);
386 CVfrCompiler::SetCOutputFileName (
392 if (mOptions
.VfrBaseFileName
== NULL
) {
396 Length
= strlen (mOptions
.OutputDirectory
) +
397 strlen (mOptions
.VfrBaseFileName
) +
401 mOptions
.COutputFileName
= (CHAR8
*) malloc (Length
);
402 if (mOptions
.COutputFileName
== NULL
) {
406 strcpy (mOptions
.COutputFileName
, mOptions
.OutputDirectory
);
407 strcat (mOptions
.COutputFileName
, mOptions
.VfrBaseFileName
);
408 strcat (mOptions
.COutputFileName
, ".c");
414 CVfrCompiler::SetPreprocessorOutputFileName (
420 if (mOptions
.VfrBaseFileName
== NULL
) {
424 Length
= strlen (mOptions
.OutputDirectory
) +
425 strlen (mOptions
.VfrBaseFileName
) +
426 strlen (VFR_PREPROCESS_FILENAME_EXTENSION
) +
429 mOptions
.PreprocessorOutputFileName
= (CHAR8
*) malloc (Length
);
430 if (mOptions
.PreprocessorOutputFileName
== NULL
) {
434 strcpy (mOptions
.PreprocessorOutputFileName
, mOptions
.OutputDirectory
);
435 strcat (mOptions
.PreprocessorOutputFileName
, mOptions
.VfrBaseFileName
);
436 strcat (mOptions
.PreprocessorOutputFileName
, VFR_PREPROCESS_FILENAME_EXTENSION
);
442 CVfrCompiler::SetRecordListFileName (
448 if (mOptions
.VfrBaseFileName
== NULL
) {
452 Length
= strlen (mOptions
.OutputDirectory
) +
453 strlen (mOptions
.VfrBaseFileName
) +
454 strlen (VFR_RECORDLIST_FILENAME_EXTENSION
) +
457 mOptions
.RecordListFile
= (CHAR8
*) malloc (Length
);
458 if (mOptions
.RecordListFile
== NULL
) {
462 strcpy (mOptions
.RecordListFile
, mOptions
.OutputDirectory
);
463 strcat (mOptions
.RecordListFile
, mOptions
.VfrBaseFileName
);
464 strcat (mOptions
.RecordListFile
, VFR_RECORDLIST_FILENAME_EXTENSION
);
469 CVfrCompiler::CVfrCompiler (
474 mPreProcessCmd
= (CHAR8
*) PREPROCESSOR_COMMAND
;
475 mPreProcessOpt
= (CHAR8
*) PREPROCESSOR_OPTIONS
;
477 SET_RUN_STATUS (STATUS_STARTED
);
479 OptionInitialization(Argc
, Argv
);
481 if ((IS_RUN_STATUS(STATUS_FAILED
)) || (IS_RUN_STATUS(STATUS_DEAD
))) {
485 SET_RUN_STATUS(STATUS_INITIALIZED
);
488 CVfrCompiler::~CVfrCompiler (
492 if (mOptions
.VfrFileName
!= NULL
) {
493 free (mOptions
.VfrFileName
);
494 mOptions
.VfrFileName
= NULL
;
497 if (mOptions
.VfrBaseFileName
!= NULL
) {
498 free (mOptions
.VfrBaseFileName
);
499 mOptions
.VfrBaseFileName
= NULL
;
502 if (mOptions
.OutputDirectory
!= NULL
) {
503 free (mOptions
.OutputDirectory
);
504 mOptions
.OutputDirectory
= NULL
;
507 if (mOptions
.PkgOutputFileName
!= NULL
) {
508 free (mOptions
.PkgOutputFileName
);
509 mOptions
.PkgOutputFileName
= NULL
;
512 if (mOptions
.COutputFileName
!= NULL
) {
513 free (mOptions
.COutputFileName
);
514 mOptions
.COutputFileName
= NULL
;
517 if (mOptions
.PreprocessorOutputFileName
!= NULL
) {
518 free (mOptions
.PreprocessorOutputFileName
);
519 mOptions
.PreprocessorOutputFileName
= NULL
;
522 if (mOptions
.RecordListFile
!= NULL
) {
523 free (mOptions
.RecordListFile
);
524 mOptions
.RecordListFile
= NULL
;
527 if (mOptions
.IncludePaths
!= NULL
) {
528 delete[] mOptions
.IncludePaths
;
529 mOptions
.IncludePaths
= NULL
;
532 if (mOptions
.CPreprocessorOptions
!= NULL
) {
533 delete[] mOptions
.CPreprocessorOptions
;
534 mOptions
.CPreprocessorOptions
= NULL
;
537 SET_RUN_STATUS(STATUS_DEAD
);
541 CVfrCompiler::Usage (
546 CONST CHAR8
*Help
[] = {
548 "VfrCompile version " VFR_COMPILER_VERSION
"Build " __BUILD_VERSION
,
549 "Copyright (c) 2004-2016 Intel Corporation. All rights reserved.",
551 "Usage: VfrCompile [options] VfrFile",
554 " -h, --help prints this help",
555 " --version prints version info",
556 " -l create an output IFR listing file",
557 " -o DIR, --output-directory DIR",
558 " deposit all output files to directory OutputDir",
559 " default is current directory",
560 " -b, --create-ifr-package",
561 " create an IFR HII pack file",
562 " -n, --no-pre-processing",
563 " do not preprocessing input file",
564 " -c, --compatible-framework",
565 " compatible framework vfr file",
567 " input uni string package file",
569 " override class guid input",
570 " format is xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
571 " -w --warning-as-error",
572 " treat warning as an error",
573 " -a --autodefaut generate default value for question opcode if some default is missing",
574 " -d --checkdefault check the default information in a question opcode",
577 for (Index
= 0; Help
[Index
] != NULL
; Index
++) {
578 fprintf (stdout
, "%s\n", Help
[Index
]);
583 CVfrCompiler::Version (
588 CONST CHAR8
*Help
[] = {
589 "VfrCompile version " VFR_COMPILER_VERSION
"Build " __BUILD_VERSION
,
592 for (Index
= 0; Help
[Index
] != NULL
; Index
++) {
593 fprintf (stdout
, "%s\n", Help
[Index
]);
598 CVfrCompiler::PreProcess (
602 FILE *pVfrFile
= NULL
;
604 CHAR8
*PreProcessCmd
= NULL
;
606 if (!IS_RUN_STATUS(STATUS_INITIALIZED
)) {
610 if (mOptions
.SkipCPreprocessor
== TRUE
) {
614 if ((pVfrFile
= fopen (LongFilePath (mOptions
.VfrFileName
), "r")) == NULL
) {
615 DebugError (NULL
, 0, 0001, "Error opening the input VFR file", "%s", mOptions
.VfrFileName
);
620 CmdLen
= strlen (mPreProcessCmd
) + strlen (mPreProcessOpt
) +
621 strlen (mOptions
.VfrFileName
) + strlen (mOptions
.PreprocessorOutputFileName
);
622 if (mOptions
.CPreprocessorOptions
!= NULL
) {
623 CmdLen
+= strlen (mOptions
.CPreprocessorOptions
);
625 if (mOptions
.IncludePaths
!= NULL
) {
626 CmdLen
+= strlen (mOptions
.IncludePaths
);
629 PreProcessCmd
= new CHAR8
[CmdLen
+ 10];
630 if (PreProcessCmd
== NULL
) {
631 DebugError (NULL
, 0, 4001, "Resource: memory can't be allocated", NULL
);
634 strcpy (PreProcessCmd
, mPreProcessCmd
), strcat (PreProcessCmd
, " ");
635 strcat (PreProcessCmd
, mPreProcessOpt
), strcat (PreProcessCmd
, " ");
636 if (mOptions
.IncludePaths
!= NULL
) {
637 strcat (PreProcessCmd
, mOptions
.IncludePaths
), strcat (PreProcessCmd
, " ");
639 if (mOptions
.CPreprocessorOptions
!= NULL
) {
640 strcat (PreProcessCmd
, mOptions
.CPreprocessorOptions
), strcat (PreProcessCmd
, " ");
642 strcat (PreProcessCmd
, mOptions
.VfrFileName
), strcat (PreProcessCmd
, " > ");
643 strcat (PreProcessCmd
, mOptions
.PreprocessorOutputFileName
);
645 if (system (PreProcessCmd
) != 0) {
646 DebugError (NULL
, 0, 0003, "Error parsing file", "failed to spawn C preprocessor on VFR file %s\n", PreProcessCmd
);
650 delete[] PreProcessCmd
;
653 SET_RUN_STATUS (STATUS_PREPROCESSED
);
657 if (!IS_RUN_STATUS(STATUS_DEAD
)) {
658 SET_RUN_STATUS (STATUS_FAILED
);
660 delete[] PreProcessCmd
;
663 extern UINT8
VfrParserStart (IN
FILE *, IN INPUT_INFO_TO_SYNTAX
*);
666 CVfrCompiler::Compile (
670 FILE *pInFile
= NULL
;
671 CHAR8
*InFileName
= NULL
;
672 INPUT_INFO_TO_SYNTAX InputInfo
;
674 if (!IS_RUN_STATUS(STATUS_PREPROCESSED
)) {
678 InFileName
= (mOptions
.SkipCPreprocessor
== TRUE
) ? mOptions
.VfrFileName
: mOptions
.PreprocessorOutputFileName
;
680 gCVfrErrorHandle
.SetInputFile (InFileName
);
681 gCVfrErrorHandle
.SetWarningAsError(mOptions
.WarningAsError
);
683 if ((pInFile
= fopen (LongFilePath (InFileName
), "r")) == NULL
) {
684 DebugError (NULL
, 0, 0001, "Error opening the input file", "%s", InFileName
);
688 InputInfo
.CompatibleMode
= mOptions
.CompatibleMode
;
689 if (mOptions
.HasOverrideClassGuid
) {
690 InputInfo
.OverrideClassGuid
= &mOptions
.OverrideClassGuid
;
692 InputInfo
.OverrideClassGuid
= NULL
;
695 if (VfrParserStart (pInFile
, &InputInfo
) != 0) {
702 if (gCFormPkg
.HavePendingUnassigned () == TRUE
) {
703 gCFormPkg
.PendingAssignPrintAll ();
707 SET_RUN_STATUS (STATUS_COMPILEED
);
711 if (!IS_RUN_STATUS(STATUS_DEAD
)) {
712 DebugError (NULL
, 0, 0003, "Error parsing", "compile error in file %s", InFileName
);
713 SET_RUN_STATUS (STATUS_FAILED
);
715 if (pInFile
!= NULL
) {
721 CVfrCompiler::AdjustBin (
725 EFI_VFR_RETURN_CODE Status
;
727 if (!IS_RUN_STATUS(STATUS_COMPILEED
)) {
731 if (gNeedAdjustOpcode
) {
733 // When parsing the Vfr, has created some opcodes, now need to update the record info.
735 gCIfrRecordInfoDB
.IfrUpdateRecordInfoForDynamicOpcode (FALSE
);
739 // Check whether need to check default info for question or auto add default for question.
741 if (mOptions
.AutoDefault
|| mOptions
.CheckDefault
) {
742 gCIfrRecordInfoDB
.IfrCheckAddDefaultRecord (mOptions
.AutoDefault
, mOptions
.CheckDefault
);
746 // Check Binary Code consistent between Form and IfrRecord
750 // Get Package Data and IfrRecord Data
752 gCFormPkg
.BuildPkg (gCBuffer
);
753 gCIfrRecordInfoDB
.IfrRecordOutput (gRBuffer
);
756 // Compare Form and Record data
758 if (gCBuffer
.Buffer
!= NULL
&& gRBuffer
.Buffer
!= NULL
) {
760 if (gCBuffer
.Size
!= gRBuffer
.Size
) {
761 DebugError (NULL
, 0, 0001, "Error parsing vfr file", " %s. FormBinary Size 0x%X is not same to RecordBuffer Size 0x%X", mOptions
.VfrFileName
, gCBuffer
.Size
, gRBuffer
.Size
);
763 for (Index
= 0; Index
< gCBuffer
.Size
; Index
++) {
764 if (gCBuffer
.Buffer
[Index
] != gRBuffer
.Buffer
[Index
]) {
768 if (Index
!= gCBuffer
.Size
) {
769 DebugError (NULL
, 0, 0001, "Error parsing vfr file", " %s. the 0x%X byte is different between Form and Record", mOptions
.VfrFileName
, Index
);
771 DebugMsg (NULL
, 0, 9, (CHAR8
*) "IFR Buffer", (CHAR8
*) "Form Buffer same to Record Buffer and Size is 0x%X", Index
);
772 } else if (gCBuffer
.Buffer
== NULL
&& gRBuffer
.Buffer
== NULL
) {
775 DebugError (NULL
, 0, 0001, "Error parsing vfr file", " %s.Buffer not allocated.", mOptions
.VfrFileName
);
779 // For UEFI mode, not do OpCode Adjust
781 if (mOptions
.CompatibleMode
) {
783 // Adjust Opcode to be compatible with framework vfr
785 Status
= gCIfrRecordInfoDB
.IfrRecordAdjust ();
786 if (Status
!= VFR_RETURN_SUCCESS
) {
788 // Record List Adjust Failed
790 SET_RUN_STATUS (STATUS_FAILED
);
794 // Re get the IfrRecord Buffer.
796 gCIfrRecordInfoDB
.IfrRecordOutput (gRBuffer
);
803 CVfrCompiler::GenBinary (
809 if (!IS_RUN_STATUS(STATUS_COMPILEED
)) {
813 if (mOptions
.CreateIfrPkgFile
== TRUE
) {
814 if ((pFile
= fopen (LongFilePath (mOptions
.PkgOutputFileName
), "wb")) == NULL
) {
815 DebugError (NULL
, 0, 0001, "Error opening file", "%s", mOptions
.PkgOutputFileName
);
818 if (gCFormPkg
.BuildPkg (pFile
, &gRBuffer
) != VFR_RETURN_SUCCESS
) {
825 SET_RUN_STATUS (STATUS_GENBINARY
);
830 if (!IS_RUN_STATUS(STATUS_DEAD
)) {
831 SET_RUN_STATUS (STATUS_FAILED
);
835 static const char *gSourceFileHeader
[] = {
837 "// DO NOT EDIT -- auto-generated file",
839 "// This file is generated by the vfrcompiler utility",
845 CVfrCompiler::GenCFile (
852 if (!IS_RUN_STATUS(STATUS_GENBINARY
)) {
856 if (!mOptions
.CreateIfrPkgFile
|| mOptions
.CompatibleMode
) {
857 if ((pFile
= fopen (LongFilePath (mOptions
.COutputFileName
), "w")) == NULL
) {
858 DebugError (NULL
, 0, 0001, "Error opening output C file", "%s", mOptions
.COutputFileName
);
862 for (Index
= 0; gSourceFileHeader
[Index
] != NULL
; Index
++) {
863 fprintf (pFile
, "%s\n", gSourceFileHeader
[Index
]);
866 if (mOptions
.CompatibleMode
) {
867 gCVfrBufferConfig
.OutputCFile (pFile
, mOptions
.VfrBaseFileName
);
870 if (gCFormPkg
.GenCFile (mOptions
.VfrBaseFileName
, pFile
, &gRBuffer
) != VFR_RETURN_SUCCESS
) {
877 SET_RUN_STATUS (STATUS_FINISHED
);
881 if (!IS_RUN_STATUS(STATUS_DEAD
)) {
882 SET_RUN_STATUS (STATUS_FAILED
);
887 CVfrCompiler::GenRecordListFile (
891 CHAR8
*InFileName
= NULL
;
892 FILE *pInFile
= NULL
;
893 FILE *pOutFile
= NULL
;
894 CHAR8 LineBuf
[MAX_VFR_LINE_LEN
];
897 InFileName
= (mOptions
.SkipCPreprocessor
== TRUE
) ? mOptions
.VfrFileName
: mOptions
.PreprocessorOutputFileName
;
899 if (mOptions
.CreateRecordListFile
== TRUE
&& InFileName
!= NULL
&& mOptions
.RecordListFile
!= NULL
) {
900 if ((InFileName
[0] == '\0') || (mOptions
.RecordListFile
[0] == '\0')) {
904 if ((pInFile
= fopen (LongFilePath (InFileName
), "r")) == NULL
) {
905 DebugError (NULL
, 0, 0001, "Error opening the input VFR preprocessor output file", "%s", InFileName
);
909 if ((pOutFile
= fopen (LongFilePath (mOptions
.RecordListFile
), "w")) == NULL
) {
910 DebugError (NULL
, 0, 0001, "Error opening the record list file", "%s", mOptions
.RecordListFile
);
914 fprintf (pOutFile
, "//\n// VFR compiler version " VFR_COMPILER_VERSION __BUILD_VERSION
"\n//\n");
916 while (!feof (pInFile
)) {
917 if (fgets (LineBuf
, MAX_VFR_LINE_LEN
, pInFile
) != NULL
) {
918 fprintf (pOutFile
, "%s", LineBuf
);
920 gCIfrRecordInfoDB
.IfrRecordOutput (pOutFile
, LineNo
);
924 fprintf (pOutFile
, "\n//\n// All Opcode Record List \n//\n");
925 gCIfrRecordInfoDB
.IfrRecordOutput (pOutFile
, 0);
926 gCVfrVarDataTypeDB
.Dump(pOutFile
);
944 COMPILER_RUN_STATUS Status
;
946 SetPrintLevel(WARNING_LOG_LEVEL
);
947 CVfrCompiler
Compiler(Argc
, Argv
);
949 Compiler
.PreProcess();
951 Compiler
.AdjustBin();
952 Compiler
.GenBinary();
954 Compiler
.GenRecordListFile ();
956 Status
= Compiler
.RunStatus ();
957 if ((Status
== STATUS_DEAD
) || (Status
== STATUS_FAILED
)) {
961 if (gCBuffer
.Buffer
!= NULL
) {
962 delete[] gCBuffer
.Buffer
;
965 if (gRBuffer
.Buffer
!= NULL
) {
966 delete[] gRBuffer
.Buffer
;
969 return GetUtilityStatus ();