]> git.proxmox.com Git - mirror_edk2.git/blobdiff - EdkCompatibilityPkg/Sample/Tools/Source/UefiVfrCompile/VfrCompiler.cpp
1) Sync EdkCompatibilityPkg with EDK 1.04. The changes includes:
[mirror_edk2.git] / EdkCompatibilityPkg / Sample / Tools / Source / UefiVfrCompile / VfrCompiler.cpp
diff --git a/EdkCompatibilityPkg/Sample/Tools/Source/UefiVfrCompile/VfrCompiler.cpp b/EdkCompatibilityPkg/Sample/Tools/Source/UefiVfrCompile/VfrCompiler.cpp
new file mode 100644 (file)
index 0000000..b1b19b9
--- /dev/null
@@ -0,0 +1,594 @@
+#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.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", PROGRAM_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", PROGRAM_NAME);\r
+        goto Fail;\r
+      }\r
+      strcpy (mOptions.OutputDirectory, Argv[Index]);\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], "-ppflag") == 0) {\r
+      Index++;\r
+      if ((Index >= Argc) || (Argv[Index][0] == '-')) {\r
+        printf ("%s -od - missing C-preprocessor argument\n", PROGRAM_NAME);\r
+        goto Fail;\r
+      }\r
+\r
+      AppendCPreprocessorOptions (Argv[Index]);\r
+    } else {\r
+      printf ("%s unrecognized option %s\n", PROGRAM_NAME, Argv[Index]);\r
+      Usage ();\r
+      goto Fail;\r
+    }\r
+  }\r
+\r
+  if (Index != Argc - 1) {\r
+    printf ("%s must specify VFR file name", PROGRAM_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", PROGRAM_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", PROGRAM_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 ((pPath = strchr (pFileName, '\\')) != NULL) {\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
+  UINT32 Index;\r
+  CONST  INT8 *Help[] = {\r
+    " ", \r
+    "VfrCompile version " VFR_COMPILER_VERSION,\r
+    " ",\r
+    "  Usage: VfrCompile {options} [VfrFile]",\r
+    " ",\r
+    "    where options include:",\r
+    "      -? or -h       prints 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        C-preprocessor argument",\r
+    "    where parameters include:",\r
+    "      VfrFile        name of the input VFR script file",\r
+    " ",\r
+    NULL\r
+    };\r
+  for (Index = 0; Help[Index] != NULL; Index++) {\r
+    fprintf (stdout, "%s\n", Help[Index]);\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 ((pVfrFile = fopen (mOptions.VfrFileName, "r")) == NULL) {\r
+    printf ("%s could not open input VFR file - %s\n", PROGRAM_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", PROGRAM_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", PROGRAM_NAME, PreProcessCmd);\r
+    goto Fail;\r
+  }\r
+\r
+  delete PreProcessCmd;\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 *VfrFile = NULL;\r
+\r
+  if (!IS_RUN_STATUS(STATUS_PREPROCESSED)) {\r
+    goto Fail;\r
+  }\r
+\r
+  if ((VfrFile = fopen (mOptions.PreprocessorOutputFileName, "r")) == NULL) {\r
+    printf ("%s failed to open input VFR preprocessor output file - %s\n", PROGRAM_NAME, mOptions.PreprocessorOutputFileName);\r
+    goto Fail;\r
+  }\r
+\r
+  if (VfrParserStart (VfrFile) != 0) {\r
+    goto Fail;\r
+  }\r
+\r
+  fclose (VfrFile);\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", PROGRAM_NAME);\r
+    SET_RUN_STATUS (STATUS_FAILED);\r
+  }\r
+  if (VfrFile != NULL) {\r
+    fclose (VfrFile);\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 PkgFileName\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
+  FILE   *pInFile  = NULL;\r
+  FILE   *pOutFile = NULL;\r
+  INT8   LineBuf[MAX_LINE_LEN];\r
+  UINT32 LineNo;\r
+\r
+  if (mOptions.CreateRecordListFile == TRUE) {\r
+    if ((mOptions.PreprocessorOutputFileName[0] == '\0') || (mOptions.RecordListFile[0] == '\0')) {\r
+      return;\r
+    }\r
+\r
+    if ((pInFile = fopen (mOptions.PreprocessorOutputFileName, "r")) == NULL) {\r
+      printf ("%s failed to open input VFR preprocessor output file - %s\n", PROGRAM_NAME, mOptions.PreprocessorOutputFileName);\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", PROGRAM_NAME, mOptions.RecordListFile);\r
+      goto Err1;\r
+    }\r
+\r
+    fprintf (pOutFile, "//\n//  VFR compiler version " VFR_COMPILER_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