+++ /dev/null
-/*++\r
-\r
-Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials\r
-are licensed and made available under the terms and conditions of the BSD License\r
-which accompanies this distribution. The full text of the license may be found at\r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-Module Name:\r
-\r
- VfrCompiler.cpp\r
-\r
-Abstract:\r
-\r
---*/\r
-\r
-#include "stdio.h"\r
-#include "string.h"\r
-#include "process.h"\r
-#include "VfrCompiler.h"\r
-\r
-\r
-VOID\r
-CVfrCompiler::SET_RUN_STATUS (\r
- IN COMPILER_RUN_STATUS Status\r
- )\r
-{\r
- mRunStatus = Status;\r
-}\r
-\r
-BOOLEAN\r
-CVfrCompiler::IS_RUN_STATUS (\r
- IN COMPILER_RUN_STATUS Status\r
- )\r
-{\r
- return mRunStatus == Status;\r
-}\r
-\r
-VOID\r
-CVfrCompiler::OptionInitialization (\r
- IN INT32 Argc,\r
- IN INT8 **Argv\r
- )\r
-{\r
- INT32 Index;\r
-\r
- mOptions.VfrFileName[0] = '\0';\r
- mOptions.RecordListFile[0] = '\0';\r
- mOptions.CreateRecordListFile = FALSE;\r
- mOptions.CreateIfrPkgFile = FALSE;\r
- mOptions.PkgOutputFileName[0] = '\0';\r
- mOptions.COutputFileName[0] = '\0';\r
- mOptions.OutputDirectory[0] = '\0';\r
- mOptions.PreprocessorOutputFileName[0] = '\0';\r
- mOptions.VfrBaseFileName[0] = '\0';\r
- mOptions.IncludePaths = NULL;\r
- mOptions.SkipCPreprocessor = FALSE;\r
- mOptions.CPreprocessorOptions = NULL;\r
-\r
- for (Index = 1; (Index < Argc) && (Argv[Index][0] == '-'); Index++) {\r
- if ((_stricmp(Argv[Index], "-?") == 0) || (_stricmp(Argv[Index], "-h") == 0)) {\r
- Usage ();\r
- SET_RUN_STATUS (STATUS_DEAD);\r
- return;\r
- } else if (_stricmp(Argv[Index], "-l") == 0) {\r
- mOptions.CreateRecordListFile = TRUE;\r
- gCIfrRecordInfoDB.TurnOn ();\r
- } else if (_stricmp(Argv[Index], "-i") == 0) {\r
- Index++;\r
- if ((Index >= Argc) || (Argv[Index][0] == '-')) {\r
- printf ("%s -i - missing path argument\n", UTILITY_NAME);\r
- goto Fail;\r
- }\r
-\r
- AppendIncludePath(Argv[Index]);\r
- } else if (_stricmp(Argv[Index], "-od") == 0) {\r
- Index++;\r
- if ((Index >= Argc) || (Argv[Index][0] == '-')) {\r
- printf ("%s -od - missing output directory name\n", UTILITY_NAME);\r
- goto Fail;\r
- }\r
- strcpy (mOptions.OutputDirectory, Argv[Index]);\r
- strcat (mOptions.OutputDirectory, "\\");\r
- } else if (_stricmp(Argv[Index], "-ibin") == 0) {\r
- mOptions.CreateIfrPkgFile = TRUE;\r
- } else if (_stricmp(Argv[Index], "-nostrings") == 0) {\r
- } else if (_stricmp(Argv[Index], "-nopp") == 0) {\r
- mOptions.SkipCPreprocessor = TRUE;\r
- } else if (_stricmp(Argv[Index], "-ppflag") == 0) {\r
- Index++;\r
- if ((Index >= Argc) || (Argv[Index][0] == '-')) {\r
- printf ("%s -od - missing C-preprocessor argument\n", UTILITY_NAME);\r
- goto Fail;\r
- }\r
-\r
- AppendCPreprocessorOptions (Argv[Index]);\r
- } else {\r
- printf ("%s unrecognized option %s\n", UTILITY_NAME, Argv[Index]);\r
- Usage ();\r
- goto Fail;\r
- }\r
- }\r
-\r
- if (Index != Argc - 1) {\r
- printf ("%s must specify VFR file name\n", UTILITY_NAME);\r
- Usage ();\r
- goto Fail;\r
- } else {\r
- strcpy (mOptions.VfrFileName, Argv[Index]);\r
- }\r
-\r
- if (SetBaseFileName() != 0) {\r
- goto Fail;\r
- }\r
- if (SetPkgOutputFileName () != 0) {\r
- goto Fail;\r
- }\r
- if (SetCOutputFileName() != 0) {\r
- goto Fail;\r
- }\r
- if (SetPreprocessorOutputFileName () != 0) {\r
- goto Fail;\r
- }\r
- if (SetRecordListFileName () != 0) {\r
- goto Fail;\r
- }\r
- return;\r
-\r
-Fail:\r
- SET_RUN_STATUS (STATUS_FAILED);\r
-\r
- mOptions.VfrFileName[0] = '\0';\r
- mOptions.RecordListFile[0] = '\0';\r
- mOptions.CreateRecordListFile = FALSE;\r
- mOptions.CreateIfrPkgFile = FALSE;\r
- mOptions.PkgOutputFileName[0] = '\0';\r
- mOptions.COutputFileName[0] = '\0';\r
- mOptions.OutputDirectory[0] = '\0';\r
- mOptions.PreprocessorOutputFileName[0] = '\0';\r
- mOptions.VfrBaseFileName[0] = '\0';\r
- if (mOptions.IncludePaths != NULL) {\r
- delete mOptions.IncludePaths;\r
- mOptions.IncludePaths = NULL;\r
- }\r
- if (mOptions.CPreprocessorOptions != NULL) {\r
- delete mOptions.CPreprocessorOptions;\r
- mOptions.CPreprocessorOptions = NULL;\r
- }\r
-}\r
-\r
-VOID\r
-CVfrCompiler::AppendIncludePath (\r
- IN INT8 *PathStr\r
- )\r
-{\r
- UINT32 Len = 0;\r
- INT8 *IncludePaths = NULL;\r
-\r
- Len = strlen (" -I ") + strlen (PathStr) + 1;\r
- if (mOptions.IncludePaths != NULL) {\r
- Len += strlen (mOptions.IncludePaths);\r
- }\r
- IncludePaths = new INT8[Len];\r
- if (IncludePaths == NULL) {\r
- printf ("%s memory allocation failure\n", UTILITY_NAME);\r
- return;\r
- }\r
- IncludePaths[0] = '\0';\r
- if (mOptions.IncludePaths != NULL) {\r
- strcat (IncludePaths, mOptions.IncludePaths);\r
- }\r
- strcat (IncludePaths, " -I ");\r
- strcat (IncludePaths, PathStr);\r
- if (mOptions.IncludePaths != NULL) {\r
- delete mOptions.IncludePaths;\r
- }\r
- mOptions.IncludePaths = IncludePaths;\r
-}\r
-\r
-VOID\r
-CVfrCompiler::AppendCPreprocessorOptions (\r
- IN INT8 *Options\r
- )\r
-{\r
- UINT32 Len = 0;\r
- INT8 *Opt = NULL;\r
-\r
- Len = strlen (Options) + strlen (" ") + 1;\r
- if (mOptions.CPreprocessorOptions != NULL) {\r
- Len += strlen (mOptions.CPreprocessorOptions);\r
- }\r
- Opt = new INT8[Len];\r
- if (Opt == NULL) {\r
- printf ("%s memory allocation failure\n", UTILITY_NAME);\r
- return;\r
- }\r
- Opt[0] = 0;\r
- if (mOptions.CPreprocessorOptions != NULL) {\r
- strcat (Opt, mOptions.CPreprocessorOptions);\r
- }\r
- strcat (Opt, " ");\r
- strcat (Opt, Options);\r
- if (mOptions.CPreprocessorOptions != NULL) {\r
- delete mOptions.CPreprocessorOptions;\r
- }\r
- mOptions.CPreprocessorOptions = Opt;\r
-}\r
-\r
-INT8\r
-CVfrCompiler::SetBaseFileName (\r
- VOID\r
- )\r
-{\r
- INT8 *pFileName, *pPath, *pExt;\r
-\r
- if (mOptions.VfrFileName[0] == '\0') {\r
- return -1;\r
- }\r
-\r
- pFileName = mOptions.VfrFileName;\r
- while (\r
- ((pPath = strchr (pFileName, '\\')) != NULL) ||\r
- ((pPath = strchr (pFileName, '/')) != NULL)\r
- )\r
- {\r
- pFileName = pPath + 1;\r
- }\r
-\r
- if (pFileName == NULL) {\r
- return -1;\r
- }\r
-\r
- if ((pExt = strchr (pFileName, '.')) == NULL) {\r
- return -1;\r
- }\r
-\r
- strncpy (mOptions.VfrBaseFileName, pFileName, pExt - pFileName);\r
- mOptions.VfrBaseFileName[pExt - pFileName] = '\0';\r
-\r
- return 0;\r
-}\r
-\r
-INT8\r
-CVfrCompiler::SetPkgOutputFileName (\r
- VOID\r
- )\r
-{\r
- if (mOptions.VfrBaseFileName[0] == '\0') {\r
- return -1;\r
- }\r
-\r
- strcpy (mOptions.PkgOutputFileName, mOptions.OutputDirectory);\r
- strcat (mOptions.PkgOutputFileName, mOptions.VfrBaseFileName);\r
- strcat (mOptions.PkgOutputFileName, VFR_PACKAGE_FILENAME_EXTENSION);\r
-\r
- return 0;\r
-}\r
-\r
-INT8\r
-CVfrCompiler::SetCOutputFileName (\r
- VOID\r
- )\r
-{\r
- if (mOptions.VfrBaseFileName[0] == '\0') {\r
- return -1;\r
- }\r
-\r
- strcpy (mOptions.COutputFileName, mOptions.OutputDirectory);\r
- strcat (mOptions.COutputFileName, mOptions.VfrBaseFileName);\r
- strcat (mOptions.COutputFileName, ".c");\r
-\r
- return 0;\r
-}\r
-\r
-INT8\r
-CVfrCompiler::SetPreprocessorOutputFileName (\r
- VOID\r
- )\r
-{\r
- if (mOptions.VfrBaseFileName[0] == '\0') {\r
- return -1;\r
- }\r
-\r
- strcpy (mOptions.PreprocessorOutputFileName, mOptions.OutputDirectory);\r
- strcat (mOptions.PreprocessorOutputFileName, mOptions.VfrBaseFileName);\r
- strcat (mOptions.PreprocessorOutputFileName, VFR_PREPROCESS_FILENAME_EXTENSION);\r
-\r
- return 0;\r
-}\r
-\r
-INT8\r
-CVfrCompiler::SetRecordListFileName (\r
- VOID\r
- )\r
-{\r
- if (mOptions.VfrBaseFileName[0] == '\0') {\r
- return -1;\r
- }\r
-\r
- strcpy (mOptions.RecordListFile, mOptions.OutputDirectory);\r
- strcat (mOptions.RecordListFile, mOptions.VfrBaseFileName);\r
- strcat (mOptions.RecordListFile, VFR_RECORDLIST_FILENAME_EXTENSION);\r
-\r
- return 0;\r
-}\r
-\r
-CVfrCompiler::CVfrCompiler (\r
- IN INT32 Argc,\r
- IN INT8 **Argv\r
- )\r
-{\r
- mPreProcessCmd = PREPROCESSOR_COMMAND;\r
- mPreProcessOpt = PREPROCESSOR_OPTIONS;\r
-\r
- OptionInitialization(Argc, Argv);\r
-\r
- if ((IS_RUN_STATUS(STATUS_FAILED)) || (IS_RUN_STATUS(STATUS_DEAD))) {\r
- return;\r
- }\r
-\r
- SET_RUN_STATUS(STATUS_INITIALIZED);\r
-}\r
-\r
-CVfrCompiler::~CVfrCompiler (\r
- VOID\r
- )\r
-{\r
- if (mOptions.IncludePaths != NULL) {\r
- delete mOptions.IncludePaths;\r
- mOptions.IncludePaths = NULL;\r
- }\r
-\r
- if (mOptions.CPreprocessorOptions != NULL) {\r
- delete mOptions.CPreprocessorOptions;\r
- mOptions.CPreprocessorOptions = NULL;\r
- }\r
-\r
- SET_RUN_STATUS(STATUS_DEAD);\r
-}\r
-\r
-VOID\r
-CVfrCompiler::Usage (\r
- VOID\r
- )\r
-{\r
- int Index;\r
- const char *Str[] = {\r
- UTILITY_NAME" "UTILITY_VERSION" - Intel UEFI VFR Compiler Utility",\r
- " Copyright (C), 2004 - 2008 Intel Corporation",\r
-#if ( defined(UTILITY_BUILD) && defined(UTILITY_VENDOR) )\r
- " Built from "UTILITY_BUILD", project of "UTILITY_VENDOR,\r
-#endif\r
- "",\r
- "Usage:",\r
- " "UTILITY_NAME" [OPTION] VFRFILE",\r
- "Description:",\r
- " Compile VFRFILE.",\r
- "Options:",\r
- " -? or -h print this help",\r
- " -l create an output IFR listing file",\r
- " -i IncPath add IncPath to the search path for VFR included files",\r
- " -od OutputDir deposit all output files to directory OutputDir (default=cwd)",\r
- " -ibin create an IFR HII pack file",\r
- " -ppflag CFlags pass Flags as C-preprocessor-flag",\r
- " -v or -version print version information",\r
- NULL\r
- };\r
-\r
- for (Index = 0; Str[Index] != NULL; Index++) {\r
- fprintf (stdout, "%s\n", Str[Index]);\r
- }\r
-}\r
-\r
-\r
-VOID\r
-CVfrCompiler::PreProcess (\r
- VOID\r
- )\r
-{\r
- FILE *pVfrFile = NULL;\r
- UINT32 CmdLen = 0;\r
- INT8 *PreProcessCmd = NULL;\r
-\r
- if (!IS_RUN_STATUS(STATUS_INITIALIZED)) {\r
- goto Fail;\r
- }\r
-\r
- if (mOptions.SkipCPreprocessor == TRUE) {\r
- goto Out;\r
- }\r
-\r
- if ((pVfrFile = fopen (mOptions.VfrFileName, "r")) == NULL) {\r
- printf ("%s could not open input VFR file - %s\n", UTILITY_NAME, mOptions.VfrFileName);\r
- goto Fail;\r
- }\r
- fclose (pVfrFile);\r
-\r
- CmdLen = strlen (mPreProcessCmd) + strlen (mPreProcessOpt) + \r
- strlen (mOptions.VfrFileName) + strlen (mOptions.PreprocessorOutputFileName);\r
- if (mOptions.CPreprocessorOptions != NULL) {\r
- CmdLen += strlen (mOptions.CPreprocessorOptions);\r
- }\r
- if (mOptions.IncludePaths != NULL) {\r
- CmdLen += strlen (mOptions.IncludePaths);\r
- }\r
-\r
- PreProcessCmd = new INT8[CmdLen + 10];\r
- if (PreProcessCmd == NULL) {\r
- printf ("%s could not allocate memory\n", UTILITY_NAME);\r
- goto Fail;\r
- }\r
- strcpy (PreProcessCmd, mPreProcessCmd), strcat (PreProcessCmd, " ");\r
- strcat (PreProcessCmd, mPreProcessOpt), strcat (PreProcessCmd, " ");\r
- if (mOptions.IncludePaths != NULL) {\r
- strcat (PreProcessCmd, mOptions.IncludePaths), strcat (PreProcessCmd, " ");\r
- }\r
- if (mOptions.CPreprocessorOptions != NULL) {\r
- strcat (PreProcessCmd, mOptions.CPreprocessorOptions), strcat (PreProcessCmd, " ");\r
- }\r
- strcat (PreProcessCmd, mOptions.VfrFileName), strcat (PreProcessCmd, " > ");\r
- strcat (PreProcessCmd, mOptions.PreprocessorOutputFileName);\r
-\r
- if (system (PreProcessCmd) != 0) {\r
- printf ("%s failed to spawn C preprocessor on VFR file \n\t - %s\n", UTILITY_NAME, PreProcessCmd);\r
- goto Fail;\r
- }\r
-\r
- delete PreProcessCmd;\r
-\r
-Out:\r
- SET_RUN_STATUS (STATUS_PREPROCESSED);\r
- return;\r
-\r
-Fail:\r
- if (!IS_RUN_STATUS(STATUS_DEAD)) {\r
- SET_RUN_STATUS (STATUS_FAILED);\r
- }\r
- delete PreProcessCmd;\r
-}\r
-\r
-extern UINT8 VfrParserStart (IN FILE *);\r
-\r
-VOID\r
-CVfrCompiler::Compile (\r
- VOID\r
- )\r
-{\r
- FILE *pInFile = NULL;\r
- INT8 *InFileName = NULL;\r
-\r
- if (!IS_RUN_STATUS(STATUS_PREPROCESSED)) {\r
- goto Fail;\r
- }\r
-\r
- InFileName = (mOptions.SkipCPreprocessor == TRUE) ? mOptions.VfrFileName : mOptions.PreprocessorOutputFileName;\r
-\r
- gCVfrErrorHandle.SetInputFile (InFileName);\r
-\r
- if ((pInFile = fopen (InFileName, "r")) == NULL) {\r
- printf ("%s failed to open input file - %s\n", UTILITY_NAME, InFileName);\r
- goto Fail;\r
- }\r
-\r
- if (VfrParserStart (pInFile) != 0) {\r
- goto Fail;\r
- }\r
-\r
- fclose (pInFile);\r
-\r
- if (gCFormPkg.HavePendingUnassigned () == TRUE) {\r
- gCFormPkg.PendingAssignPrintAll ();\r
- goto Fail;\r
- }\r
-\r
- SET_RUN_STATUS (STATUS_COMPILEED);\r
- return;\r
-\r
-Fail:\r
- if (!IS_RUN_STATUS(STATUS_DEAD)) {\r
- printf ("%s compile error!\n", UTILITY_NAME);\r
- SET_RUN_STATUS (STATUS_FAILED);\r
- }\r
- if (pInFile != NULL) {\r
- fclose (pInFile);\r
- }\r
-}\r
-\r
-VOID\r
-CVfrCompiler::GenBinary (\r
- VOID\r
- )\r
-{\r
- FILE *pFile = NULL;\r
-\r
- if (!IS_RUN_STATUS(STATUS_COMPILEED)) {\r
- goto Fail;\r
- }\r
-\r
- if (mOptions.CreateIfrPkgFile == TRUE) {\r
- if ((pFile = fopen (mOptions.PkgOutputFileName, "wb")) == NULL) {\r
- printf ("can not open %s\n", mOptions.PkgOutputFileName);\r
- goto Fail;\r
- }\r
- if (gCFormPkg.BuildPkg (pFile) != VFR_RETURN_SUCCESS) {\r
- fclose (pFile);\r
- goto Fail;\r
- }\r
- fclose (pFile);\r
- }\r
-\r
- SET_RUN_STATUS (STATUS_GENBINARY);\r
- return;\r
-\r
-Fail:\r
- if (!IS_RUN_STATUS(STATUS_DEAD)) {\r
- SET_RUN_STATUS (STATUS_FAILED);\r
- }\r
-}\r
-\r
-static const char *gSourceFileHeader[] = {\r
- "//",\r
- "// DO NOT EDIT -- auto-generated file",\r
- "//",\r
- "// This file is generated by the vfrcompiler utility",\r
- "//",\r
- NULL\r
-};\r
-\r
-VOID\r
-CVfrCompiler::GenCFile (\r
- VOID\r
- )\r
-{\r
- FILE *pFile;\r
- UINT32 Index;\r
-\r
- if (!IS_RUN_STATUS(STATUS_GENBINARY)) {\r
- goto Fail;\r
- }\r
-\r
- if ((pFile = fopen (mOptions.COutputFileName, "w")) == NULL) {\r
- printf ("failed to open output C file - %s\n", mOptions.COutputFileName);\r
- goto Fail;\r
- }\r
-\r
- for (Index = 0; gSourceFileHeader[Index] != NULL; Index++) {\r
- fprintf (pFile, "%s\n", gSourceFileHeader[Index]);\r
- }\r
-\r
- gCVfrBufferConfig.OutputCFile (pFile, mOptions.VfrBaseFileName);\r
-\r
- if (gCFormPkg.GenCFile (mOptions.VfrBaseFileName, pFile) != VFR_RETURN_SUCCESS) {\r
- fclose (pFile);\r
- goto Fail;\r
- }\r
- fclose (pFile);\r
-\r
- SET_RUN_STATUS (STATUS_FINISHED);\r
- return;\r
-\r
-Fail:\r
- if (!IS_RUN_STATUS(STATUS_DEAD)) {\r
- SET_RUN_STATUS (STATUS_FAILED);\r
- }\r
-}\r
-\r
-VOID\r
-CVfrCompiler::GenRecordListFile (\r
- VOID\r
- )\r
-{\r
- INT8 *InFileName = NULL;\r
- FILE *pInFile = NULL;\r
- FILE *pOutFile = NULL;\r
- INT8 LineBuf[MAX_LINE_LEN];\r
- UINT32 LineNo;\r
-\r
- InFileName = (mOptions.SkipCPreprocessor == TRUE) ? mOptions.VfrFileName : mOptions.PreprocessorOutputFileName;\r
-\r
- if (mOptions.CreateRecordListFile == TRUE) {\r
- if ((InFileName[0] == '\0') || (mOptions.RecordListFile[0] == '\0')) {\r
- return;\r
- }\r
-\r
- if ((pInFile = fopen (InFileName, "r")) == NULL) {\r
- printf ("%s failed to open input VFR preprocessor output file - %s\n", UTILITY_NAME, InFileName);\r
- return;\r
- }\r
-\r
- if ((pOutFile = fopen (mOptions.RecordListFile, "w")) == NULL) {\r
- printf ("%s failed to open record list file for writing - %s\n", UTILITY_NAME, mOptions.RecordListFile);\r
- goto Err1;\r
- }\r
-\r
- fprintf (pOutFile, "//\n// VFR compiler version " UTILITY_VERSION "\n//\n");\r
- LineNo = 0;\r
- while (!feof (pInFile)) {\r
- if (fgets (LineBuf, MAX_LINE_LEN, pInFile) != NULL) {\r
- fprintf (pOutFile, "%s", LineBuf);\r
- LineNo++;\r
- gCIfrRecordInfoDB.IfrRecordOutput (pOutFile, LineNo);\r
- }\r
- }\r
-\r
- fclose (pOutFile);\r
- fclose (pInFile);\r
- }\r
-\r
- return;\r
-\r
-Err1:\r
- fclose (pInFile);\r
-}\r
-\r
-INT32\r
-main (\r
- IN INT32 Argc,\r
- IN INT8 **Argv\r
- )\r
-{\r
- COMPILER_RUN_STATUS Status;\r
- CVfrCompiler Compiler(Argc, Argv);\r
-\r
- Compiler.PreProcess();\r
- Compiler.Compile();\r
- Compiler.GenBinary();\r
- Compiler.GenCFile();\r
- Compiler.GenRecordListFile ();\r
-\r
- Status = Compiler.RunStatus ();\r
- if ((Status == STATUS_DEAD) || (Status == STATUS_FAILED)) {\r
- return 2;\r
- }\r
-\r
- return 0;\r
-}\r
-\r