]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/VfrCompile/VfrCompiler.cpp
dd34a1b5333fc76a2b273127e0e03ef1243d3c38
[mirror_edk2.git] / BaseTools / Source / C / VfrCompile / VfrCompiler.cpp
1 /** @file
2
3 VfrCompiler main class and main function.
4
5 Copyright (c) 2004 - 2013, 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
10
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.
13
14 **/
15
16 #include "stdio.h"
17 #include "stdlib.h"
18 #include "string.h"
19 #include "VfrCompiler.h"
20 #include "CommonLib.h"
21 #include "EfiUtilityMsgs.h"
22
23 PACKAGE_DATA gCBuffer;
24 PACKAGE_DATA gRBuffer;
25 CVfrStringDB gCVfrStringDB;
26
27 VOID
28 CVfrCompiler::DebugError (
29 IN CHAR8 *FileName,
30 IN UINT32 LineNumber,
31 IN UINT32 MessageCode,
32 IN CONST CHAR8 *Text,
33 IN CONST CHAR8 *MsgFmt,
34 ...
35 )
36 {
37 va_list List;
38 va_start (List, MsgFmt);
39 PrintMessage ((CHAR8 *) "ERROR", FileName, LineNumber, MessageCode, (CHAR8 *) Text, (CHAR8 *) MsgFmt, List);
40 va_end (List);
41 }
42
43 VOID
44 CVfrCompiler::SET_RUN_STATUS (
45 IN COMPILER_RUN_STATUS Status
46 )
47 {
48 mRunStatus = Status;
49 }
50
51 BOOLEAN
52 CVfrCompiler::IS_RUN_STATUS (
53 IN COMPILER_RUN_STATUS Status
54 )
55 {
56 return mRunStatus == Status;
57 }
58
59 VOID
60 CVfrCompiler::OptionInitialization (
61 IN INT32 Argc,
62 IN CHAR8 **Argv
63 )
64 {
65 INT32 Index;
66 EFI_STATUS Status;
67
68 Status = EFI_SUCCESS;
69 SetUtilityName ((CHAR8*) PROGRAM_NAME);
70
71 mOptions.VfrFileName[0] = '\0';
72 mOptions.RecordListFile[0] = '\0';
73 mOptions.CreateRecordListFile = FALSE;
74 mOptions.CreateIfrPkgFile = FALSE;
75 mOptions.PkgOutputFileName[0] = '\0';
76 mOptions.COutputFileName[0] = '\0';
77 mOptions.OutputDirectory[0] = '\0';
78 mOptions.PreprocessorOutputFileName[0] = '\0';
79 mOptions.VfrBaseFileName[0] = '\0';
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 memset (&mOptions.OverrideClassGuid, 0, sizeof (EFI_GUID));
87
88 if (Argc == 1) {
89 Usage ();
90 SET_RUN_STATUS (STATUS_DEAD);
91 return;
92 }
93
94 for (Index = 1; (Index < Argc) && (Argv[Index][0] == '-'); Index++) {
95 if ((stricmp(Argv[Index], "-h") == 0) || (stricmp(Argv[Index], "--help") == 0)) {
96 Usage ();
97 SET_RUN_STATUS (STATUS_DEAD);
98 return;
99 } else if (stricmp(Argv[Index], "-l") == 0) {
100 mOptions.CreateRecordListFile = TRUE;
101 gCIfrRecordInfoDB.TurnOn ();
102 } else if (stricmp(Argv[Index], "-i") == 0) {
103 Index++;
104 if ((Index >= Argc) || (Argv[Index][0] == '-')) {
105 DebugError (NULL, 0, 1001, "Missing option", "-i missing path argument");
106 goto Fail;
107 }
108
109 AppendIncludePath(Argv[Index]);
110 } else if (stricmp(Argv[Index], "-o") == 0 || stricmp(Argv[Index], "--output-directory") == 0 || stricmp(Argv[Index], "-od") == 0) {
111 Index++;
112 if ((Index >= Argc) || (Argv[Index][0] == '-')) {
113 DebugError (NULL, 0, 1001, "Missing option", "-o missing output directory name");
114 goto Fail;
115 }
116 strcpy (mOptions.OutputDirectory, Argv[Index]);
117
118 CHAR8 lastChar = mOptions.OutputDirectory[strlen(mOptions.OutputDirectory) - 1];
119 if ((lastChar != '/') && (lastChar != '\\')) {
120 if (strchr(mOptions.OutputDirectory, '/') != NULL) {
121 strcat (mOptions.OutputDirectory, "/");
122 } else {
123 strcat (mOptions.OutputDirectory, "\\");
124 }
125 }
126 DebugMsg (NULL, 0, 9, (CHAR8 *) "Output Directory", mOptions.OutputDirectory);
127 } else if (stricmp(Argv[Index], "-b") == 0 || stricmp(Argv[Index], "--create-ifr-package") == 0 || stricmp(Argv[Index], "-ibin") == 0) {
128 mOptions.CreateIfrPkgFile = TRUE;
129 } else if (stricmp(Argv[Index], "-n") == 0 || stricmp(Argv[Index], "--no-pre-processing") == 0 || stricmp(Argv[Index], "-nopp") == 0) {
130 mOptions.SkipCPreprocessor = TRUE;
131 } else if (stricmp(Argv[Index], "-f") == 0 || stricmp(Argv[Index], "--pre-processing-flag") == 0 || stricmp(Argv[Index], "-ppflag") == 0) {
132 Index++;
133 if ((Index >= Argc) || (Argv[Index][0] == '-')) {
134 DebugError (NULL, 0, 1001, "Missing option", "-od - missing C-preprocessor argument");
135 goto Fail;
136 }
137
138 AppendCPreprocessorOptions (Argv[Index]);
139 } else if (stricmp(Argv[Index], "-c") == 0 || stricmp(Argv[Index], "--compatible-framework") == 0) {
140 mOptions.CompatibleMode = TRUE;
141 } else if (stricmp(Argv[Index], "-s") == 0|| stricmp(Argv[Index], "--string-db") == 0) {
142 Index++;
143 if ((Index >= Argc) || (Argv[Index][0] == '-')) {
144 DebugError (NULL, 0, 1001, "Missing option", "-s missing input string file name");
145 goto Fail;
146 }
147 gCVfrStringDB.SetStringFileName(Argv[Index]);
148 DebugMsg (NULL, 0, 9, (CHAR8 *) "Input string file path", Argv[Index]);
149 } else if ((stricmp (Argv[Index], "-g") == 0) || (stricmp (Argv[Index], "--guid") == 0)) {
150 Index++;
151 Status = StringToGuid (Argv[Index], &mOptions.OverrideClassGuid);
152 if (EFI_ERROR (Status)) {
153 DebugError (NULL, 0, 1000, "Invalid format:", "%s", Argv[Index]);
154 goto Fail;
155 }
156 mOptions.HasOverrideClassGuid = TRUE;
157 } else if (stricmp(Argv[Index], "-w") == 0 || stricmp(Argv[Index], "--warning-as-error") == 0) {
158 mOptions.WarningAsError = TRUE;
159 } else {
160 DebugError (NULL, 0, 1000, "Unknown option", "unrecognized option %s", Argv[Index]);
161 goto Fail;
162 }
163 }
164
165 if (Index != Argc - 1) {
166 DebugError (NULL, 0, 1001, "Missing option", "VFR file name is not specified.");
167 goto Fail;
168 } else {
169 strcpy (mOptions.VfrFileName, Argv[Index]);
170 }
171
172 if (SetBaseFileName() != 0) {
173 goto Fail;
174 }
175 if (SetPkgOutputFileName () != 0) {
176 goto Fail;
177 }
178 if (SetCOutputFileName() != 0) {
179 goto Fail;
180 }
181 if (SetPreprocessorOutputFileName () != 0) {
182 goto Fail;
183 }
184 if (SetRecordListFileName () != 0) {
185 goto Fail;
186 }
187 return;
188
189 Fail:
190 SET_RUN_STATUS (STATUS_DEAD);
191
192 mOptions.VfrFileName[0] = '\0';
193 mOptions.RecordListFile[0] = '\0';
194 mOptions.CreateRecordListFile = FALSE;
195 mOptions.CreateIfrPkgFile = FALSE;
196 mOptions.PkgOutputFileName[0] = '\0';
197 mOptions.COutputFileName[0] = '\0';
198 mOptions.OutputDirectory[0] = '\0';
199 mOptions.PreprocessorOutputFileName[0] = '\0';
200 mOptions.VfrBaseFileName[0] = '\0';
201 if (mOptions.IncludePaths != NULL) {
202 delete mOptions.IncludePaths;
203 mOptions.IncludePaths = NULL;
204 }
205 if (mOptions.CPreprocessorOptions != NULL) {
206 delete mOptions.CPreprocessorOptions;
207 mOptions.CPreprocessorOptions = NULL;
208 }
209 }
210
211 VOID
212 CVfrCompiler::AppendIncludePath (
213 IN CHAR8 *PathStr
214 )
215 {
216 UINT32 Len = 0;
217 CHAR8 *IncludePaths = NULL;
218
219 Len = strlen (" -I ") + strlen (PathStr) + 1;
220 if (mOptions.IncludePaths != NULL) {
221 Len += strlen (mOptions.IncludePaths);
222 }
223 IncludePaths = new CHAR8[Len];
224 if (IncludePaths == NULL) {
225 DebugError (NULL, 0, 4001, "Resource: memory can't be allocated", NULL);
226 return;
227 }
228 IncludePaths[0] = '\0';
229 if (mOptions.IncludePaths != NULL) {
230 strcat (IncludePaths, mOptions.IncludePaths);
231 }
232 strcat (IncludePaths, " -I ");
233 strcat (IncludePaths, PathStr);
234 if (mOptions.IncludePaths != NULL) {
235 delete mOptions.IncludePaths;
236 }
237 mOptions.IncludePaths = IncludePaths;
238 }
239
240 VOID
241 CVfrCompiler::AppendCPreprocessorOptions (
242 IN CHAR8 *Options
243 )
244 {
245 UINT32 Len = 0;
246 CHAR8 *Opt = NULL;
247
248 Len = strlen (Options) + strlen (" ") + 1;
249 if (mOptions.CPreprocessorOptions != NULL) {
250 Len += strlen (mOptions.CPreprocessorOptions);
251 }
252 Opt = new CHAR8[Len];
253 if (Opt == NULL) {
254 DebugError (NULL, 0, 4001, "Resource: memory can't be allocated", NULL);
255 return;
256 }
257 Opt[0] = 0;
258 if (mOptions.CPreprocessorOptions != NULL) {
259 strcat (Opt, mOptions.CPreprocessorOptions);
260 }
261 strcat (Opt, " ");
262 strcat (Opt, Options);
263 if (mOptions.CPreprocessorOptions != NULL) {
264 delete mOptions.CPreprocessorOptions;
265 }
266 mOptions.CPreprocessorOptions = Opt;
267 }
268
269 INT8
270 CVfrCompiler::SetBaseFileName (
271 VOID
272 )
273 {
274 CHAR8 *pFileName, *pPath, *pExt;
275
276 if (mOptions.VfrFileName[0] == '\0') {
277 return -1;
278 }
279
280 pFileName = mOptions.VfrFileName;
281 while (
282 ((pPath = strchr (pFileName, '\\')) != NULL) ||
283 ((pPath = strchr (pFileName, '/')) != NULL)
284 )
285 {
286 pFileName = pPath + 1;
287 }
288
289 if (pFileName == NULL) {
290 return -1;
291 }
292
293 if ((pExt = strchr (pFileName, '.')) == NULL) {
294 return -1;
295 }
296
297 strncpy (mOptions.VfrBaseFileName, pFileName, pExt - pFileName);
298 mOptions.VfrBaseFileName[pExt - pFileName] = '\0';
299
300 return 0;
301 }
302
303 INT8
304 CVfrCompiler::SetPkgOutputFileName (
305 VOID
306 )
307 {
308 if (mOptions.VfrBaseFileName[0] == '\0') {
309 return -1;
310 }
311
312 strcpy (mOptions.PkgOutputFileName, mOptions.OutputDirectory);
313 strcat (mOptions.PkgOutputFileName, mOptions.VfrBaseFileName);
314 strcat (mOptions.PkgOutputFileName, VFR_PACKAGE_FILENAME_EXTENSION);
315
316 return 0;
317 }
318
319 INT8
320 CVfrCompiler::SetCOutputFileName (
321 VOID
322 )
323 {
324 if (mOptions.VfrBaseFileName[0] == '\0') {
325 return -1;
326 }
327
328 strcpy (mOptions.COutputFileName, mOptions.OutputDirectory);
329 strcat (mOptions.COutputFileName, mOptions.VfrBaseFileName);
330 strcat (mOptions.COutputFileName, ".c");
331
332 return 0;
333 }
334
335 INT8
336 CVfrCompiler::SetPreprocessorOutputFileName (
337 VOID
338 )
339 {
340 if (mOptions.VfrBaseFileName[0] == '\0') {
341 return -1;
342 }
343
344 strcpy (mOptions.PreprocessorOutputFileName, mOptions.OutputDirectory);
345 strcat (mOptions.PreprocessorOutputFileName, mOptions.VfrBaseFileName);
346 strcat (mOptions.PreprocessorOutputFileName, VFR_PREPROCESS_FILENAME_EXTENSION);
347
348 return 0;
349 }
350
351 INT8
352 CVfrCompiler::SetRecordListFileName (
353 VOID
354 )
355 {
356 if (mOptions.VfrBaseFileName[0] == '\0') {
357 return -1;
358 }
359
360 strcpy (mOptions.RecordListFile, mOptions.OutputDirectory);
361 strcat (mOptions.RecordListFile, mOptions.VfrBaseFileName);
362 strcat (mOptions.RecordListFile, VFR_RECORDLIST_FILENAME_EXTENSION);
363
364 return 0;
365 }
366
367 CVfrCompiler::CVfrCompiler (
368 IN INT32 Argc,
369 IN CHAR8 **Argv
370 )
371 {
372 mPreProcessCmd = (CHAR8 *) PREPROCESSOR_COMMAND;
373 mPreProcessOpt = (CHAR8 *) PREPROCESSOR_OPTIONS;
374
375 OptionInitialization(Argc, Argv);
376
377 if ((IS_RUN_STATUS(STATUS_FAILED)) || (IS_RUN_STATUS(STATUS_DEAD))) {
378 return;
379 }
380
381 SET_RUN_STATUS(STATUS_INITIALIZED);
382 }
383
384 CVfrCompiler::~CVfrCompiler (
385 VOID
386 )
387 {
388 if (mOptions.IncludePaths != NULL) {
389 delete mOptions.IncludePaths;
390 mOptions.IncludePaths = NULL;
391 }
392
393 if (mOptions.CPreprocessorOptions != NULL) {
394 delete mOptions.CPreprocessorOptions;
395 mOptions.CPreprocessorOptions = NULL;
396 }
397
398 SET_RUN_STATUS(STATUS_DEAD);
399 }
400
401 VOID
402 CVfrCompiler::Usage (
403 VOID
404 )
405 {
406 UINT32 Index;
407 CONST CHAR8 *Help[] = {
408 " ",
409 "VfrCompile version " VFR_COMPILER_VERSION __BUILD_VERSION,
410 "Copyright (c) 2004-2013 Intel Corporation. All rights reserved.",
411 " ",
412 "Usage: VfrCompile [options] VfrFile",
413 " ",
414 "Options:",
415 " -h, --help prints this help",
416 " -l create an output IFR listing file",
417 " -o DIR, --output-directory DIR",
418 " deposit all output files to directory OutputDir",
419 " default is current directory",
420 " -b, --create-ifr-package",
421 " create an IFR HII pack file",
422 " -n, --no-pre-processing",
423 " do not preprocessing input file",
424 " -c, --compatible-framework",
425 " compatible framework vfr file",
426 " -s, --string-db",
427 " input uni string package file",
428 " -g, --guid",
429 " override class guid input",
430 " format is xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
431 " -w --warning-as-error",
432 " treat warning as an error",
433 NULL
434 };
435 for (Index = 0; Help[Index] != NULL; Index++) {
436 fprintf (stdout, "%s\n", Help[Index]);
437 }
438 }
439
440 VOID
441 CVfrCompiler::PreProcess (
442 VOID
443 )
444 {
445 FILE *pVfrFile = NULL;
446 UINT32 CmdLen = 0;
447 CHAR8 *PreProcessCmd = NULL;
448
449 if (!IS_RUN_STATUS(STATUS_INITIALIZED)) {
450 goto Fail;
451 }
452
453 if (mOptions.SkipCPreprocessor == TRUE) {
454 goto Out;
455 }
456
457 if ((pVfrFile = fopen (mOptions.VfrFileName, "r")) == NULL) {
458 DebugError (NULL, 0, 0001, "Error opening the input VFR file", mOptions.VfrFileName);
459 goto Fail;
460 }
461 fclose (pVfrFile);
462
463 CmdLen = strlen (mPreProcessCmd) + strlen (mPreProcessOpt) +
464 strlen (mOptions.VfrFileName) + strlen (mOptions.PreprocessorOutputFileName);
465 if (mOptions.CPreprocessorOptions != NULL) {
466 CmdLen += strlen (mOptions.CPreprocessorOptions);
467 }
468 if (mOptions.IncludePaths != NULL) {
469 CmdLen += strlen (mOptions.IncludePaths);
470 }
471
472 PreProcessCmd = new CHAR8[CmdLen + 10];
473 if (PreProcessCmd == NULL) {
474 DebugError (NULL, 0, 4001, "Resource: memory can't be allocated", NULL);
475 goto Fail;
476 }
477 strcpy (PreProcessCmd, mPreProcessCmd), strcat (PreProcessCmd, " ");
478 strcat (PreProcessCmd, mPreProcessOpt), strcat (PreProcessCmd, " ");
479 if (mOptions.IncludePaths != NULL) {
480 strcat (PreProcessCmd, mOptions.IncludePaths), strcat (PreProcessCmd, " ");
481 }
482 if (mOptions.CPreprocessorOptions != NULL) {
483 strcat (PreProcessCmd, mOptions.CPreprocessorOptions), strcat (PreProcessCmd, " ");
484 }
485 strcat (PreProcessCmd, mOptions.VfrFileName), strcat (PreProcessCmd, " > ");
486 strcat (PreProcessCmd, mOptions.PreprocessorOutputFileName);
487
488 if (system (PreProcessCmd) != 0) {
489 DebugError (NULL, 0, 0003, "Error parsing file", "failed to spawn C preprocessor on VFR file %s\n", PreProcessCmd);
490 goto Fail;
491 }
492
493 delete PreProcessCmd;
494
495 Out:
496 SET_RUN_STATUS (STATUS_PREPROCESSED);
497 return;
498
499 Fail:
500 if (!IS_RUN_STATUS(STATUS_DEAD)) {
501 SET_RUN_STATUS (STATUS_FAILED);
502 }
503 delete PreProcessCmd;
504 }
505
506 extern UINT8 VfrParserStart (IN FILE *, IN INPUT_INFO_TO_SYNTAX *);
507
508 VOID
509 CVfrCompiler::Compile (
510 VOID
511 )
512 {
513 FILE *pInFile = NULL;
514 CHAR8 *InFileName = NULL;
515 INPUT_INFO_TO_SYNTAX InputInfo;
516
517 if (!IS_RUN_STATUS(STATUS_PREPROCESSED)) {
518 goto Fail;
519 }
520
521 InFileName = (mOptions.SkipCPreprocessor == TRUE) ? mOptions.VfrFileName : mOptions.PreprocessorOutputFileName;
522
523 gCVfrErrorHandle.SetInputFile (InFileName);
524 gCVfrErrorHandle.SetWarningAsError(mOptions.WarningAsError);
525
526 if ((pInFile = fopen (InFileName, "r")) == NULL) {
527 DebugError (NULL, 0, 0001, "Error opening the input file", InFileName);
528 goto Fail;
529 }
530
531 InputInfo.CompatibleMode = mOptions.CompatibleMode;
532 if (mOptions.HasOverrideClassGuid) {
533 InputInfo.OverrideClassGuid = &mOptions.OverrideClassGuid;
534 } else {
535 InputInfo.OverrideClassGuid = NULL;
536 }
537
538 if (VfrParserStart (pInFile, &InputInfo) != 0) {
539 goto Fail;
540 }
541
542 fclose (pInFile);
543
544 if (gCFormPkg.HavePendingUnassigned () == TRUE) {
545 gCFormPkg.PendingAssignPrintAll ();
546 goto Fail;
547 }
548
549 SET_RUN_STATUS (STATUS_COMPILEED);
550 return;
551
552 Fail:
553 if (!IS_RUN_STATUS(STATUS_DEAD)) {
554 DebugError (NULL, 0, 0003, "Error parsing", "compile error in file %s", InFileName);
555 SET_RUN_STATUS (STATUS_FAILED);
556 }
557 if (pInFile != NULL) {
558 fclose (pInFile);
559 }
560 }
561
562 VOID
563 CVfrCompiler::UpdateInfoForDynamicOpcode (
564 VOID
565 )
566 {
567 SIfrRecord *pRecord;
568
569 if (!gNeedAdjustOpcode) {
570 return;
571 }
572
573 //
574 // Base on the original offset info to update the record list.
575 //
576 if (!gCIfrRecordInfoDB.IfrAdjustDynamicOpcodeInRecords()) {
577 DebugError (NULL, 0, 1001, "Error parsing vfr file", "Can find the offset in the record.");
578 }
579
580 //
581 // Base on the opcode binary length to recalculate the offset for each opcode.
582 //
583 gCIfrRecordInfoDB.IfrAdjustOffsetForRecord();
584
585 //
586 // Base on the offset to find the binary address.
587 //
588 pRecord = gCIfrRecordInfoDB.GetRecordInfoFromOffset(gAdjustOpcodeOffset);
589 while (pRecord != NULL) {
590 pRecord->mIfrBinBuf = gCFormPkg.GetBufAddrBaseOnOffset(pRecord->mOffset);
591 if (pRecord->mIfrBinBuf == NULL) {
592 DebugError (NULL, 0, 0001, "Error parsing vfr file", " 0x%X. offset not allocated.", pRecord->mOffset);
593 }
594 pRecord = pRecord->mNext;
595 }
596 }
597
598 VOID
599 CVfrCompiler::AdjustBin (
600 VOID
601 )
602 {
603 EFI_VFR_RETURN_CODE Status;
604
605 if (!IS_RUN_STATUS(STATUS_COMPILEED)) {
606 return;
607 }
608
609 UpdateInfoForDynamicOpcode ();
610
611 //
612 // Check Binary Code consistent between Form and IfrRecord
613 //
614
615 //
616 // Get Package Data and IfrRecord Data
617 //
618 gCFormPkg.BuildPkg (gCBuffer);
619 gCIfrRecordInfoDB.IfrRecordOutput (gRBuffer);
620
621 //
622 // Compare Form and Record data
623 //
624 if (gCBuffer.Buffer != NULL && gRBuffer.Buffer != NULL) {
625 UINT32 Index;
626 if (gCBuffer.Size != gRBuffer.Size) {
627 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);
628 }
629 for (Index = 0; Index < gCBuffer.Size; Index ++) {
630 if (gCBuffer.Buffer[Index] != gRBuffer.Buffer[Index]) {
631 break;
632 }
633 }
634 if (Index != gCBuffer.Size) {
635 DebugError (NULL, 0, 0001, "Error parsing vfr file", " %s. the 0x%X byte is different between Form and Record", mOptions.VfrFileName, Index);
636 }
637 DebugMsg (NULL, 0, 9, (CHAR8 *) "IFR Buffer", (CHAR8 *) "Form Buffer same to Record Buffer and Size is 0x%X", Index);
638 } else if (gCBuffer.Buffer == NULL && gRBuffer.Buffer == NULL) {
639 //ok
640 } else {
641 DebugError (NULL, 0, 0001, "Error parsing vfr file", " %s.Buffer not allocated.", mOptions.VfrFileName);
642 }
643
644 //
645 // For UEFI mode, not do OpCode Adjust
646 //
647 if (mOptions.CompatibleMode) {
648 //
649 // Adjust Opcode to be compatible with framework vfr
650 //
651 Status = gCIfrRecordInfoDB.IfrRecordAdjust ();
652 if (Status != VFR_RETURN_SUCCESS) {
653 //
654 // Record List Adjust Failed
655 //
656 SET_RUN_STATUS (STATUS_FAILED);
657 return;
658 }
659 //
660 // Re get the IfrRecord Buffer.
661 //
662 gCIfrRecordInfoDB.IfrRecordOutput (gRBuffer);
663 }
664
665 return;
666 }
667
668 VOID
669 CVfrCompiler::GenBinary (
670 VOID
671 )
672 {
673 FILE *pFile = NULL;
674
675 if (!IS_RUN_STATUS(STATUS_COMPILEED)) {
676 goto Fail;
677 }
678
679 if (mOptions.CreateIfrPkgFile == TRUE) {
680 if ((pFile = fopen (mOptions.PkgOutputFileName, "wb")) == NULL) {
681 DebugError (NULL, 0, 0001, "Error opening file", mOptions.PkgOutputFileName);
682 goto Fail;
683 }
684 if (gCFormPkg.BuildPkg (pFile, &gRBuffer) != VFR_RETURN_SUCCESS) {
685 fclose (pFile);
686 goto Fail;
687 }
688 fclose (pFile);
689 }
690
691 SET_RUN_STATUS (STATUS_GENBINARY);
692
693 return;
694
695 Fail:
696 if (!IS_RUN_STATUS(STATUS_DEAD)) {
697 SET_RUN_STATUS (STATUS_FAILED);
698 }
699 }
700
701 static const char *gSourceFileHeader[] = {
702 "//",
703 "// DO NOT EDIT -- auto-generated file",
704 "//",
705 "// This file is generated by the vfrcompiler utility",
706 "//",
707 NULL
708 };
709
710 VOID
711 CVfrCompiler::GenCFile (
712 VOID
713 )
714 {
715 FILE *pFile;
716 UINT32 Index;
717
718 if (!IS_RUN_STATUS(STATUS_GENBINARY)) {
719 goto Fail;
720 }
721
722 if (!mOptions.CreateIfrPkgFile || mOptions.CompatibleMode) {
723 if ((pFile = fopen (mOptions.COutputFileName, "w")) == NULL) {
724 DebugError (NULL, 0, 0001, "Error opening output C file", mOptions.COutputFileName);
725 goto Fail;
726 }
727
728 for (Index = 0; gSourceFileHeader[Index] != NULL; Index++) {
729 fprintf (pFile, "%s\n", gSourceFileHeader[Index]);
730 }
731
732 if (mOptions.CompatibleMode) {
733 gCVfrBufferConfig.OutputCFile (pFile, mOptions.VfrBaseFileName);
734 }
735
736 if (gCFormPkg.GenCFile (mOptions.VfrBaseFileName, pFile, &gRBuffer) != VFR_RETURN_SUCCESS) {
737 fclose (pFile);
738 goto Fail;
739 }
740 fclose (pFile);
741 }
742
743 SET_RUN_STATUS (STATUS_FINISHED);
744 return;
745
746 Fail:
747 if (!IS_RUN_STATUS(STATUS_DEAD)) {
748 SET_RUN_STATUS (STATUS_FAILED);
749 }
750 }
751
752 VOID
753 CVfrCompiler::GenRecordListFile (
754 VOID
755 )
756 {
757 CHAR8 *InFileName = NULL;
758 FILE *pInFile = NULL;
759 FILE *pOutFile = NULL;
760 CHAR8 LineBuf[MAX_VFR_LINE_LEN];
761 UINT32 LineNo;
762
763 InFileName = (mOptions.SkipCPreprocessor == TRUE) ? mOptions.VfrFileName : mOptions.PreprocessorOutputFileName;
764
765 if (mOptions.CreateRecordListFile == TRUE) {
766 if ((InFileName[0] == '\0') || (mOptions.RecordListFile[0] == '\0')) {
767 return;
768 }
769
770 if ((pInFile = fopen (InFileName, "r")) == NULL) {
771 DebugError (NULL, 0, 0001, "Error opening the input VFR preprocessor output file", InFileName);
772 return;
773 }
774
775 if ((pOutFile = fopen (mOptions.RecordListFile, "w")) == NULL) {
776 DebugError (NULL, 0, 0001, "Error opening the record list file", mOptions.RecordListFile);
777 goto Err1;
778 }
779
780 fprintf (pOutFile, "//\n// VFR compiler version " VFR_COMPILER_VERSION __BUILD_VERSION "\n//\n");
781 LineNo = 0;
782 while (!feof (pInFile)) {
783 if (fgets (LineBuf, MAX_VFR_LINE_LEN, pInFile) != NULL) {
784 fprintf (pOutFile, "%s", LineBuf);
785 LineNo++;
786 gCIfrRecordInfoDB.IfrRecordOutput (pOutFile, LineNo);
787 }
788 }
789
790 fprintf (pOutFile, "\n//\n// All Opcode Record List \n//\n");
791 gCIfrRecordInfoDB.IfrRecordOutput (pOutFile, 0);
792 gCVfrVarDataTypeDB.Dump(pOutFile);
793
794 fclose (pOutFile);
795 fclose (pInFile);
796 }
797
798 return;
799
800 Err1:
801 fclose (pInFile);
802 }
803
804 int
805 main (
806 IN int Argc,
807 IN char **Argv
808 )
809 {
810 COMPILER_RUN_STATUS Status;
811
812 SetPrintLevel(WARNING_LOG_LEVEL);
813 CVfrCompiler Compiler(Argc, Argv);
814
815 Compiler.PreProcess();
816 Compiler.Compile();
817 Compiler.AdjustBin();
818 Compiler.GenBinary();
819 Compiler.GenCFile();
820 Compiler.GenRecordListFile ();
821
822 Status = Compiler.RunStatus ();
823 if ((Status == STATUS_DEAD) || (Status == STATUS_FAILED)) {
824 return 2;
825 }
826
827 if (gCBuffer.Buffer != NULL) {
828 delete gCBuffer.Buffer;
829 }
830
831 if (gRBuffer.Buffer != NULL) {
832 delete gRBuffer.Buffer;
833 }
834
835 return GetUtilityStatus ();
836 }
837
838