3 VfrCompiler main class and main function.
5 Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 #include "VfrCompiler.h"
20 #include "CommonLib.h"
21 #include "EfiUtilityMsgs.h"
23 PACKAGE_DATA gCBuffer
;
24 PACKAGE_DATA gRBuffer
;
25 CVfrStringDB gCVfrStringDB
;
28 CVfrCompiler::DebugError (
31 IN UINT32 MessageCode
,
33 IN CONST CHAR8
*MsgFmt
,
38 va_start (List
, MsgFmt
);
39 PrintMessage ((CHAR8
*) "ERROR", FileName
, LineNumber
, MessageCode
, (CHAR8
*) Text
, (CHAR8
*) MsgFmt
, List
);
44 CVfrCompiler::SET_RUN_STATUS (
45 IN COMPILER_RUN_STATUS Status
52 CVfrCompiler::IS_RUN_STATUS (
53 IN COMPILER_RUN_STATUS Status
56 return mRunStatus
== Status
;
60 CVfrCompiler::OptionInitialization (
69 SetUtilityName ((CHAR8
*) PROGRAM_NAME
);
71 mOptions
.VfrFileName
= NULL
;
72 mOptions
.RecordListFile
= NULL
;
73 mOptions
.CreateRecordListFile
= FALSE
;
74 mOptions
.CreateIfrPkgFile
= FALSE
;
75 mOptions
.PkgOutputFileName
= NULL
;
76 mOptions
.COutputFileName
= NULL
;
77 mOptions
.OutputDirectory
= NULL
;
78 mOptions
.PreprocessorOutputFileName
= NULL
;
79 mOptions
.VfrBaseFileName
= NULL
;
80 mOptions
.IncludePaths
= NULL
;
81 mOptions
.SkipCPreprocessor
= TRUE
;
82 mOptions
.CPreprocessorOptions
= NULL
;
83 mOptions
.CompatibleMode
= FALSE
;
84 mOptions
.HasOverrideClassGuid
= FALSE
;
85 mOptions
.WarningAsError
= FALSE
;
86 mOptions
.AutoDefault
= FALSE
;
87 mOptions
.CheckDefault
= FALSE
;
88 memset (&mOptions
.OverrideClassGuid
, 0, sizeof (EFI_GUID
));
92 SET_RUN_STATUS (STATUS_DEAD
);
96 for (Index
= 1; (Index
< Argc
) && (Argv
[Index
][0] == '-'); Index
++) {
97 if ((stricmp(Argv
[Index
], "-h") == 0) || (stricmp(Argv
[Index
], "--help") == 0)) {
99 SET_RUN_STATUS (STATUS_DEAD
);
101 } else if (stricmp(Argv
[Index
], "--version") == 0) {
103 SET_RUN_STATUS (STATUS_DEAD
);
105 } else if (stricmp(Argv
[Index
], "-l") == 0) {
106 mOptions
.CreateRecordListFile
= TRUE
;
107 gCIfrRecordInfoDB
.TurnOn ();
108 } else if (stricmp(Argv
[Index
], "-i") == 0) {
110 if ((Index
>= Argc
) || (Argv
[Index
][0] == '-')) {
111 DebugError (NULL
, 0, 1001, "Missing option", "-i missing path argument");
115 AppendIncludePath(Argv
[Index
]);
116 } else if (stricmp(Argv
[Index
], "-o") == 0 || stricmp(Argv
[Index
], "--output-directory") == 0 || stricmp(Argv
[Index
], "-od") == 0) {
118 if ((Index
>= Argc
) || (Argv
[Index
][0] == '-')) {
119 DebugError (NULL
, 0, 1001, "Missing option", "-o missing output directory name");
123 mOptions
.OutputDirectory
= (CHAR8
*) malloc (strlen (Argv
[Index
]) + strlen ("\\") + 1);
124 if (mOptions
.OutputDirectory
== NULL
) {
125 DebugError (NULL
, 0, 4001, "Resource: memory can't be allocated", NULL
);
128 strcpy (mOptions
.OutputDirectory
, Argv
[Index
]);
130 CHAR8 lastChar
= mOptions
.OutputDirectory
[strlen(mOptions
.OutputDirectory
) - 1];
131 if ((lastChar
!= '/') && (lastChar
!= '\\')) {
132 if (strchr(mOptions
.OutputDirectory
, '/') != NULL
) {
133 strcat (mOptions
.OutputDirectory
, "/");
135 strcat (mOptions
.OutputDirectory
, "\\");
138 DebugMsg (NULL
, 0, 9, (CHAR8
*) "Output Directory", (CHAR8
*) "%s", mOptions
.OutputDirectory
);
139 } else if (stricmp(Argv
[Index
], "-b") == 0 || stricmp(Argv
[Index
], "--create-ifr-package") == 0 || stricmp(Argv
[Index
], "-ibin") == 0) {
140 mOptions
.CreateIfrPkgFile
= TRUE
;
141 } else if (stricmp(Argv
[Index
], "-n") == 0 || stricmp(Argv
[Index
], "--no-pre-processing") == 0 || stricmp(Argv
[Index
], "-nopp") == 0) {
142 mOptions
.SkipCPreprocessor
= TRUE
;
143 } else if (stricmp(Argv
[Index
], "-f") == 0 || stricmp(Argv
[Index
], "--pre-processing-flag") == 0 || stricmp(Argv
[Index
], "-ppflag") == 0) {
145 if ((Index
>= Argc
) || (Argv
[Index
][0] == '-')) {
146 DebugError (NULL
, 0, 1001, "Missing option", "-od - missing C-preprocessor argument");
150 AppendCPreprocessorOptions (Argv
[Index
]);
151 } else if (stricmp(Argv
[Index
], "-c") == 0 || stricmp(Argv
[Index
], "--compatible-framework") == 0) {
152 mOptions
.CompatibleMode
= TRUE
;
153 } else if (stricmp(Argv
[Index
], "-s") == 0|| stricmp(Argv
[Index
], "--string-db") == 0) {
155 if ((Index
>= Argc
) || (Argv
[Index
][0] == '-')) {
156 DebugError (NULL
, 0, 1001, "Missing option", "-s missing input string file name");
159 gCVfrStringDB
.SetStringFileName(Argv
[Index
]);
160 DebugMsg (NULL
, 0, 9, (CHAR8
*) "Input string file path", (CHAR8
*) "%s", Argv
[Index
]);
161 } else if ((stricmp (Argv
[Index
], "-g") == 0) || (stricmp (Argv
[Index
], "--guid") == 0)) {
163 Status
= StringToGuid (Argv
[Index
], &mOptions
.OverrideClassGuid
);
164 if (EFI_ERROR (Status
)) {
165 DebugError (NULL
, 0, 1000, "Invalid format:", "%s", Argv
[Index
]);
168 mOptions
.HasOverrideClassGuid
= TRUE
;
169 } else if (stricmp(Argv
[Index
], "-w") == 0 || stricmp(Argv
[Index
], "--warning-as-error") == 0) {
170 mOptions
.WarningAsError
= TRUE
;
171 } else if (stricmp(Argv
[Index
], "-a") == 0 ||stricmp(Argv
[Index
], "--autodefault") == 0) {
172 mOptions
.AutoDefault
= TRUE
;
173 } else if (stricmp(Argv
[Index
], "-d") == 0 ||stricmp(Argv
[Index
], "--checkdefault") == 0) {
174 mOptions
.CheckDefault
= TRUE
;
176 DebugError (NULL
, 0, 1000, "Unknown option", "unrecognized option %s", Argv
[Index
]);
181 if (Index
!= Argc
- 1) {
182 DebugError (NULL
, 0, 1001, "Missing option", "VFR file name is not specified.");
185 mOptions
.VfrFileName
= (CHAR8
*) malloc (strlen (Argv
[Index
]) + 1);
186 if (mOptions
.VfrFileName
== NULL
) {
187 DebugError (NULL
, 0, 4001, "Resource: memory can't be allocated", NULL
);
190 strcpy (mOptions
.VfrFileName
, Argv
[Index
]);
192 if (mOptions
.OutputDirectory
== NULL
) {
193 mOptions
.OutputDirectory
= (CHAR8
*) malloc (1);
194 if (mOptions
.OutputDirectory
== NULL
) {
195 DebugError (NULL
, 0, 4001, "Resource: memory can't be allocated", NULL
);
198 mOptions
.OutputDirectory
[0] = '\0';
202 if (SetBaseFileName() != 0) {
205 if (SetPkgOutputFileName () != 0) {
208 if (SetCOutputFileName() != 0) {
211 if (SetPreprocessorOutputFileName () != 0) {
214 if (SetRecordListFileName () != 0) {
220 SET_RUN_STATUS (STATUS_DEAD
);
222 mOptions
.CreateRecordListFile
= FALSE
;
223 mOptions
.CreateIfrPkgFile
= FALSE
;
225 if (mOptions
.VfrFileName
!= NULL
) {
226 free (mOptions
.VfrFileName
);
227 mOptions
.VfrFileName
= NULL
;
229 if (mOptions
.VfrBaseFileName
!= NULL
) {
230 free (mOptions
.VfrBaseFileName
);
231 mOptions
.VfrBaseFileName
= NULL
;
233 if (mOptions
.OutputDirectory
!= NULL
) {
234 free (mOptions
.OutputDirectory
);
235 mOptions
.OutputDirectory
= NULL
;
237 if (mOptions
.PkgOutputFileName
!= NULL
) {
238 free (mOptions
.PkgOutputFileName
);
239 mOptions
.PkgOutputFileName
= NULL
;
241 if (mOptions
.COutputFileName
!= NULL
) {
242 free (mOptions
.COutputFileName
);
243 mOptions
.COutputFileName
= NULL
;
245 if (mOptions
.PreprocessorOutputFileName
!= NULL
) {
246 free (mOptions
.PreprocessorOutputFileName
);
247 mOptions
.PreprocessorOutputFileName
= NULL
;
249 if (mOptions
.RecordListFile
!= NULL
) {
250 free (mOptions
.RecordListFile
);
251 mOptions
.RecordListFile
= NULL
;
253 if (mOptions
.IncludePaths
!= NULL
) {
254 delete mOptions
.IncludePaths
;
255 mOptions
.IncludePaths
= NULL
;
257 if (mOptions
.CPreprocessorOptions
!= NULL
) {
258 delete mOptions
.CPreprocessorOptions
;
259 mOptions
.CPreprocessorOptions
= NULL
;
264 CVfrCompiler::AppendIncludePath (
269 CHAR8
*IncludePaths
= NULL
;
271 Len
= strlen (" -I ") + strlen (PathStr
) + 1;
272 if (mOptions
.IncludePaths
!= NULL
) {
273 Len
+= strlen (mOptions
.IncludePaths
);
275 IncludePaths
= new CHAR8
[Len
];
276 if (IncludePaths
== NULL
) {
277 DebugError (NULL
, 0, 4001, "Resource: memory can't be allocated", NULL
);
280 IncludePaths
[0] = '\0';
281 if (mOptions
.IncludePaths
!= NULL
) {
282 strcat (IncludePaths
, mOptions
.IncludePaths
);
284 strcat (IncludePaths
, " -I ");
285 strcat (IncludePaths
, PathStr
);
286 if (mOptions
.IncludePaths
!= NULL
) {
287 delete[] mOptions
.IncludePaths
;
289 mOptions
.IncludePaths
= IncludePaths
;
293 CVfrCompiler::AppendCPreprocessorOptions (
300 Len
= strlen (Options
) + strlen (" ") + 1;
301 if (mOptions
.CPreprocessorOptions
!= NULL
) {
302 Len
+= strlen (mOptions
.CPreprocessorOptions
);
304 Opt
= new CHAR8
[Len
];
306 DebugError (NULL
, 0, 4001, "Resource: memory can't be allocated", NULL
);
310 if (mOptions
.CPreprocessorOptions
!= NULL
) {
311 strcat (Opt
, mOptions
.CPreprocessorOptions
);
314 strcat (Opt
, Options
);
315 if (mOptions
.CPreprocessorOptions
!= NULL
) {
316 delete[] mOptions
.CPreprocessorOptions
;
318 mOptions
.CPreprocessorOptions
= Opt
;
322 CVfrCompiler::SetBaseFileName (
326 CHAR8
*pFileName
, *pPath
, *pExt
;
328 if (mOptions
.VfrFileName
== NULL
) {
332 pFileName
= mOptions
.VfrFileName
;
334 ((pPath
= strchr (pFileName
, '\\')) != NULL
) ||
335 ((pPath
= strchr (pFileName
, '/')) != NULL
)
338 pFileName
= pPath
+ 1;
341 if (pFileName
== NULL
) {
345 if ((pExt
= strchr (pFileName
, '.')) == NULL
) {
351 mOptions
.VfrBaseFileName
= (CHAR8
*) malloc (strlen (pFileName
) + 1);
352 if (mOptions
.VfrBaseFileName
== NULL
) {
357 strcpy (mOptions
.VfrBaseFileName
, pFileName
);
364 CVfrCompiler::SetPkgOutputFileName (
370 if (mOptions
.VfrBaseFileName
== NULL
) {
374 Length
= strlen (mOptions
.OutputDirectory
) +
375 strlen (mOptions
.VfrBaseFileName
) +
376 strlen (VFR_PACKAGE_FILENAME_EXTENSION
) +
379 mOptions
.PkgOutputFileName
= (CHAR8
*) malloc (Length
);
380 if (mOptions
.PkgOutputFileName
== NULL
) {
384 strcpy (mOptions
.PkgOutputFileName
, mOptions
.OutputDirectory
);
385 strcat (mOptions
.PkgOutputFileName
, mOptions
.VfrBaseFileName
);
386 strcat (mOptions
.PkgOutputFileName
, VFR_PACKAGE_FILENAME_EXTENSION
);
392 CVfrCompiler::SetCOutputFileName (
398 if (mOptions
.VfrBaseFileName
== NULL
) {
402 Length
= strlen (mOptions
.OutputDirectory
) +
403 strlen (mOptions
.VfrBaseFileName
) +
407 mOptions
.COutputFileName
= (CHAR8
*) malloc (Length
);
408 if (mOptions
.COutputFileName
== NULL
) {
412 strcpy (mOptions
.COutputFileName
, mOptions
.OutputDirectory
);
413 strcat (mOptions
.COutputFileName
, mOptions
.VfrBaseFileName
);
414 strcat (mOptions
.COutputFileName
, ".c");
420 CVfrCompiler::SetPreprocessorOutputFileName (
426 if (mOptions
.VfrBaseFileName
== NULL
) {
430 Length
= strlen (mOptions
.OutputDirectory
) +
431 strlen (mOptions
.VfrBaseFileName
) +
432 strlen (VFR_PREPROCESS_FILENAME_EXTENSION
) +
435 mOptions
.PreprocessorOutputFileName
= (CHAR8
*) malloc (Length
);
436 if (mOptions
.PreprocessorOutputFileName
== NULL
) {
440 strcpy (mOptions
.PreprocessorOutputFileName
, mOptions
.OutputDirectory
);
441 strcat (mOptions
.PreprocessorOutputFileName
, mOptions
.VfrBaseFileName
);
442 strcat (mOptions
.PreprocessorOutputFileName
, VFR_PREPROCESS_FILENAME_EXTENSION
);
448 CVfrCompiler::SetRecordListFileName (
454 if (mOptions
.VfrBaseFileName
== NULL
) {
458 Length
= strlen (mOptions
.OutputDirectory
) +
459 strlen (mOptions
.VfrBaseFileName
) +
460 strlen (VFR_RECORDLIST_FILENAME_EXTENSION
) +
463 mOptions
.RecordListFile
= (CHAR8
*) malloc (Length
);
464 if (mOptions
.RecordListFile
== NULL
) {
468 strcpy (mOptions
.RecordListFile
, mOptions
.OutputDirectory
);
469 strcat (mOptions
.RecordListFile
, mOptions
.VfrBaseFileName
);
470 strcat (mOptions
.RecordListFile
, VFR_RECORDLIST_FILENAME_EXTENSION
);
475 CVfrCompiler::CVfrCompiler (
480 mPreProcessCmd
= (CHAR8
*) PREPROCESSOR_COMMAND
;
481 mPreProcessOpt
= (CHAR8
*) PREPROCESSOR_OPTIONS
;
483 SET_RUN_STATUS (STATUS_STARTED
);
485 OptionInitialization(Argc
, Argv
);
487 if ((IS_RUN_STATUS(STATUS_FAILED
)) || (IS_RUN_STATUS(STATUS_DEAD
))) {
491 SET_RUN_STATUS(STATUS_INITIALIZED
);
494 CVfrCompiler::~CVfrCompiler (
498 if (mOptions
.VfrFileName
!= NULL
) {
499 free (mOptions
.VfrFileName
);
500 mOptions
.VfrFileName
= NULL
;
503 if (mOptions
.VfrBaseFileName
!= NULL
) {
504 free (mOptions
.VfrBaseFileName
);
505 mOptions
.VfrBaseFileName
= NULL
;
508 if (mOptions
.OutputDirectory
!= NULL
) {
509 free (mOptions
.OutputDirectory
);
510 mOptions
.OutputDirectory
= NULL
;
513 if (mOptions
.PkgOutputFileName
!= NULL
) {
514 free (mOptions
.PkgOutputFileName
);
515 mOptions
.PkgOutputFileName
= NULL
;
518 if (mOptions
.COutputFileName
!= NULL
) {
519 free (mOptions
.COutputFileName
);
520 mOptions
.COutputFileName
= NULL
;
523 if (mOptions
.PreprocessorOutputFileName
!= NULL
) {
524 free (mOptions
.PreprocessorOutputFileName
);
525 mOptions
.PreprocessorOutputFileName
= NULL
;
528 if (mOptions
.RecordListFile
!= NULL
) {
529 free (mOptions
.RecordListFile
);
530 mOptions
.RecordListFile
= NULL
;
533 if (mOptions
.IncludePaths
!= NULL
) {
534 delete[] mOptions
.IncludePaths
;
535 mOptions
.IncludePaths
= NULL
;
538 if (mOptions
.CPreprocessorOptions
!= NULL
) {
539 delete[] mOptions
.CPreprocessorOptions
;
540 mOptions
.CPreprocessorOptions
= NULL
;
543 SET_RUN_STATUS(STATUS_DEAD
);
547 CVfrCompiler::Usage (
552 CONST CHAR8
*Help
[] = {
554 "VfrCompile version " VFR_COMPILER_VERSION
"Build " __BUILD_VERSION
,
555 "Copyright (c) 2004-2016 Intel Corporation. All rights reserved.",
557 "Usage: VfrCompile [options] VfrFile",
560 " -h, --help prints this help",
561 " --version prints version info",
562 " -l create an output IFR listing file",
563 " -o DIR, --output-directory DIR",
564 " deposit all output files to directory OutputDir",
565 " default is current directory",
566 " -b, --create-ifr-package",
567 " create an IFR HII pack file",
568 " -n, --no-pre-processing",
569 " do not preprocessing input file",
570 " -c, --compatible-framework",
571 " compatible framework vfr file",
573 " input uni string package file",
575 " override class guid input",
576 " format is xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
577 " -w --warning-as-error",
578 " treat warning as an error",
579 " -a --autodefaut generate default value for question opcode if some default is missing",
580 " -d --checkdefault check the default information in a question opcode",
583 for (Index
= 0; Help
[Index
] != NULL
; Index
++) {
584 fprintf (stdout
, "%s\n", Help
[Index
]);
589 CVfrCompiler::Version (
594 CONST CHAR8
*Help
[] = {
595 "VfrCompile version " VFR_COMPILER_VERSION
"Build " __BUILD_VERSION
,
598 for (Index
= 0; Help
[Index
] != NULL
; Index
++) {
599 fprintf (stdout
, "%s\n", Help
[Index
]);
604 CVfrCompiler::PreProcess (
608 FILE *pVfrFile
= NULL
;
610 CHAR8
*PreProcessCmd
= NULL
;
612 if (!IS_RUN_STATUS(STATUS_INITIALIZED
)) {
616 if (mOptions
.SkipCPreprocessor
== TRUE
) {
620 if ((pVfrFile
= fopen (LongFilePath (mOptions
.VfrFileName
), "r")) == NULL
) {
621 DebugError (NULL
, 0, 0001, "Error opening the input VFR file", "%s", mOptions
.VfrFileName
);
626 CmdLen
= strlen (mPreProcessCmd
) + strlen (mPreProcessOpt
) +
627 strlen (mOptions
.VfrFileName
) + strlen (mOptions
.PreprocessorOutputFileName
);
628 if (mOptions
.CPreprocessorOptions
!= NULL
) {
629 CmdLen
+= strlen (mOptions
.CPreprocessorOptions
);
631 if (mOptions
.IncludePaths
!= NULL
) {
632 CmdLen
+= strlen (mOptions
.IncludePaths
);
635 PreProcessCmd
= new CHAR8
[CmdLen
+ 10];
636 if (PreProcessCmd
== NULL
) {
637 DebugError (NULL
, 0, 4001, "Resource: memory can't be allocated", NULL
);
640 strcpy (PreProcessCmd
, mPreProcessCmd
), strcat (PreProcessCmd
, " ");
641 strcat (PreProcessCmd
, mPreProcessOpt
), strcat (PreProcessCmd
, " ");
642 if (mOptions
.IncludePaths
!= NULL
) {
643 strcat (PreProcessCmd
, mOptions
.IncludePaths
), strcat (PreProcessCmd
, " ");
645 if (mOptions
.CPreprocessorOptions
!= NULL
) {
646 strcat (PreProcessCmd
, mOptions
.CPreprocessorOptions
), strcat (PreProcessCmd
, " ");
648 strcat (PreProcessCmd
, mOptions
.VfrFileName
), strcat (PreProcessCmd
, " > ");
649 strcat (PreProcessCmd
, mOptions
.PreprocessorOutputFileName
);
651 if (system (PreProcessCmd
) != 0) {
652 DebugError (NULL
, 0, 0003, "Error parsing file", "failed to spawn C preprocessor on VFR file %s\n", PreProcessCmd
);
656 delete[] PreProcessCmd
;
659 SET_RUN_STATUS (STATUS_PREPROCESSED
);
663 if (!IS_RUN_STATUS(STATUS_DEAD
)) {
664 SET_RUN_STATUS (STATUS_FAILED
);
666 delete[] PreProcessCmd
;
669 extern UINT8
VfrParserStart (IN
FILE *, IN INPUT_INFO_TO_SYNTAX
*);
672 CVfrCompiler::Compile (
676 FILE *pInFile
= NULL
;
677 CHAR8
*InFileName
= NULL
;
678 INPUT_INFO_TO_SYNTAX InputInfo
;
680 if (!IS_RUN_STATUS(STATUS_PREPROCESSED
)) {
684 InFileName
= (mOptions
.SkipCPreprocessor
== TRUE
) ? mOptions
.VfrFileName
: mOptions
.PreprocessorOutputFileName
;
686 gCVfrErrorHandle
.SetInputFile (InFileName
);
687 gCVfrErrorHandle
.SetWarningAsError(mOptions
.WarningAsError
);
689 if ((pInFile
= fopen (LongFilePath (InFileName
), "r")) == NULL
) {
690 DebugError (NULL
, 0, 0001, "Error opening the input file", "%s", InFileName
);
694 InputInfo
.CompatibleMode
= mOptions
.CompatibleMode
;
695 if (mOptions
.HasOverrideClassGuid
) {
696 InputInfo
.OverrideClassGuid
= &mOptions
.OverrideClassGuid
;
698 InputInfo
.OverrideClassGuid
= NULL
;
701 if (VfrParserStart (pInFile
, &InputInfo
) != 0) {
708 if (gCFormPkg
.HavePendingUnassigned () == TRUE
) {
709 gCFormPkg
.PendingAssignPrintAll ();
713 SET_RUN_STATUS (STATUS_COMPILEED
);
717 if (!IS_RUN_STATUS(STATUS_DEAD
)) {
718 DebugError (NULL
, 0, 0003, "Error parsing", "compile error in file %s", InFileName
);
719 SET_RUN_STATUS (STATUS_FAILED
);
721 if (pInFile
!= NULL
) {
727 CVfrCompiler::AdjustBin (
731 EFI_VFR_RETURN_CODE Status
;
733 if (!IS_RUN_STATUS(STATUS_COMPILEED
)) {
737 if (gNeedAdjustOpcode
) {
739 // When parsing the Vfr, has created some opcodes, now need to update the record info.
741 gCIfrRecordInfoDB
.IfrUpdateRecordInfoForDynamicOpcode (FALSE
);
745 // Check whether need to check default info for question or auto add default for question.
747 if (mOptions
.AutoDefault
|| mOptions
.CheckDefault
) {
748 gCIfrRecordInfoDB
.IfrCheckAddDefaultRecord (mOptions
.AutoDefault
, mOptions
.CheckDefault
);
752 // Check Binary Code consistent between Form and IfrRecord
756 // Get Package Data and IfrRecord Data
758 gCFormPkg
.BuildPkg (gCBuffer
);
759 gCIfrRecordInfoDB
.IfrRecordOutput (gRBuffer
);
762 // Compare Form and Record data
764 if (gCBuffer
.Buffer
!= NULL
&& gRBuffer
.Buffer
!= NULL
) {
766 if (gCBuffer
.Size
!= gRBuffer
.Size
) {
767 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
);
769 for (Index
= 0; Index
< gCBuffer
.Size
; Index
++) {
770 if (gCBuffer
.Buffer
[Index
] != gRBuffer
.Buffer
[Index
]) {
774 if (Index
!= gCBuffer
.Size
) {
775 DebugError (NULL
, 0, 0001, "Error parsing vfr file", " %s. the 0x%X byte is different between Form and Record", mOptions
.VfrFileName
, Index
);
777 DebugMsg (NULL
, 0, 9, (CHAR8
*) "IFR Buffer", (CHAR8
*) "Form Buffer same to Record Buffer and Size is 0x%X", Index
);
778 } else if (gCBuffer
.Buffer
== NULL
&& gRBuffer
.Buffer
== NULL
) {
781 DebugError (NULL
, 0, 0001, "Error parsing vfr file", " %s.Buffer not allocated.", mOptions
.VfrFileName
);
785 // For UEFI mode, not do OpCode Adjust
787 if (mOptions
.CompatibleMode
) {
789 // Adjust Opcode to be compatible with framework vfr
791 Status
= gCIfrRecordInfoDB
.IfrRecordAdjust ();
792 if (Status
!= VFR_RETURN_SUCCESS
) {
794 // Record List Adjust Failed
796 SET_RUN_STATUS (STATUS_FAILED
);
800 // Re get the IfrRecord Buffer.
802 gCIfrRecordInfoDB
.IfrRecordOutput (gRBuffer
);
809 CVfrCompiler::GenBinary (
815 if (!IS_RUN_STATUS(STATUS_COMPILEED
)) {
819 if (mOptions
.CreateIfrPkgFile
== TRUE
) {
820 if ((pFile
= fopen (LongFilePath (mOptions
.PkgOutputFileName
), "wb")) == NULL
) {
821 DebugError (NULL
, 0, 0001, "Error opening file", "%s", mOptions
.PkgOutputFileName
);
824 if (gCFormPkg
.BuildPkg (pFile
, &gRBuffer
) != VFR_RETURN_SUCCESS
) {
831 SET_RUN_STATUS (STATUS_GENBINARY
);
836 if (!IS_RUN_STATUS(STATUS_DEAD
)) {
837 SET_RUN_STATUS (STATUS_FAILED
);
841 static const char *gSourceFileHeader
[] = {
843 "// DO NOT EDIT -- auto-generated file",
845 "// This file is generated by the vfrcompiler utility",
851 CVfrCompiler::GenCFile (
858 if (!IS_RUN_STATUS(STATUS_GENBINARY
)) {
862 if (!mOptions
.CreateIfrPkgFile
|| mOptions
.CompatibleMode
) {
863 if ((pFile
= fopen (LongFilePath (mOptions
.COutputFileName
), "w")) == NULL
) {
864 DebugError (NULL
, 0, 0001, "Error opening output C file", "%s", mOptions
.COutputFileName
);
868 for (Index
= 0; gSourceFileHeader
[Index
] != NULL
; Index
++) {
869 fprintf (pFile
, "%s\n", gSourceFileHeader
[Index
]);
872 if (mOptions
.CompatibleMode
) {
873 gCVfrBufferConfig
.OutputCFile (pFile
, mOptions
.VfrBaseFileName
);
876 if (gCFormPkg
.GenCFile (mOptions
.VfrBaseFileName
, pFile
, &gRBuffer
) != VFR_RETURN_SUCCESS
) {
883 SET_RUN_STATUS (STATUS_FINISHED
);
887 if (!IS_RUN_STATUS(STATUS_DEAD
)) {
888 SET_RUN_STATUS (STATUS_FAILED
);
893 CVfrCompiler::GenRecordListFile (
897 CHAR8
*InFileName
= NULL
;
898 FILE *pInFile
= NULL
;
899 FILE *pOutFile
= NULL
;
900 CHAR8 LineBuf
[MAX_VFR_LINE_LEN
];
903 InFileName
= (mOptions
.SkipCPreprocessor
== TRUE
) ? mOptions
.VfrFileName
: mOptions
.PreprocessorOutputFileName
;
905 if (mOptions
.CreateRecordListFile
== TRUE
&& InFileName
!= NULL
&& mOptions
.RecordListFile
!= NULL
) {
906 if ((InFileName
[0] == '\0') || (mOptions
.RecordListFile
[0] == '\0')) {
910 if ((pInFile
= fopen (LongFilePath (InFileName
), "r")) == NULL
) {
911 DebugError (NULL
, 0, 0001, "Error opening the input VFR preprocessor output file", "%s", InFileName
);
915 if ((pOutFile
= fopen (LongFilePath (mOptions
.RecordListFile
), "w")) == NULL
) {
916 DebugError (NULL
, 0, 0001, "Error opening the record list file", "%s", mOptions
.RecordListFile
);
920 fprintf (pOutFile
, "//\n// VFR compiler version " VFR_COMPILER_VERSION __BUILD_VERSION
"\n//\n");
922 while (!feof (pInFile
)) {
923 if (fgets (LineBuf
, MAX_VFR_LINE_LEN
, pInFile
) != NULL
) {
924 fprintf (pOutFile
, "%s", LineBuf
);
926 gCIfrRecordInfoDB
.IfrRecordOutput (pOutFile
, LineNo
);
930 fprintf (pOutFile
, "\n//\n// All Opcode Record List \n//\n");
931 gCIfrRecordInfoDB
.IfrRecordOutput (pOutFile
, 0);
932 gCVfrVarDataTypeDB
.Dump(pOutFile
);
950 COMPILER_RUN_STATUS Status
;
952 SetPrintLevel(WARNING_LOG_LEVEL
);
953 CVfrCompiler
Compiler(Argc
, Argv
);
955 Compiler
.PreProcess();
957 Compiler
.AdjustBin();
958 Compiler
.GenBinary();
960 Compiler
.GenRecordListFile ();
962 Status
= Compiler
.RunStatus ();
963 if ((Status
== STATUS_DEAD
) || (Status
== STATUS_FAILED
)) {
967 if (gCBuffer
.Buffer
!= NULL
) {
968 delete[] gCBuffer
.Buffer
;
971 if (gRBuffer
.Buffer
!= NULL
) {
972 delete[] gRBuffer
.Buffer
;
975 return GetUtilityStatus ();