]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/VfrCompile/VfrCompiler.cpp
BaseTools/VfrCompile: Explicitly state format string for DebugMsg()
[mirror_edk2.git] / BaseTools / Source / C / VfrCompile / VfrCompiler.cpp
index 695ce6cc484c846aaf5261df9d0566d72b97d291..ff7057a64f4ca751f8da07bd45de8f9e2c5e6a50 100644 (file)
@@ -68,21 +68,23 @@ CVfrCompiler::OptionInitialization (
   Status = EFI_SUCCESS;\r
   SetUtilityName ((CHAR8*) PROGRAM_NAME);\r
 \r
-  mOptions.VfrFileName[0]                = '\0';\r
-  mOptions.RecordListFile[0]             = '\0';\r
+  mOptions.VfrFileName                   = NULL;\r
+  mOptions.RecordListFile                = NULL;\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.PkgOutputFileName             = NULL;\r
+  mOptions.COutputFileName               = NULL;\r
+  mOptions.OutputDirectory               = NULL;\r
+  mOptions.PreprocessorOutputFileName    = NULL;\r
+  mOptions.VfrBaseFileName               = NULL;\r
   mOptions.IncludePaths                  = NULL;\r
   mOptions.SkipCPreprocessor             = TRUE;\r
   mOptions.CPreprocessorOptions          = NULL;\r
   mOptions.CompatibleMode                = FALSE;\r
   mOptions.HasOverrideClassGuid          = FALSE;\r
   mOptions.WarningAsError                = FALSE;\r
+  mOptions.AutoDefault                   = FALSE;\r
+  mOptions.CheckDefault                  = FALSE;\r
   memset (&mOptions.OverrideClassGuid, 0, sizeof (EFI_GUID));\r
   \r
   if (Argc == 1) {\r
@@ -117,6 +119,16 @@ CVfrCompiler::OptionInitialization (
         DebugError (NULL, 0, 1001, "Missing option", "-o missing output directory name");\r
         goto Fail;\r
       }\r
+      if (strlen (Argv[Index]) > MAX_PATH - 1) {\r
+        DebugError (NULL, 0, 1003, "Invalid option value", "Output directory name %s is too long", Argv[Index]);\r
+        goto Fail;\r
+      }\r
+\r
+      mOptions.OutputDirectory = (CHAR8 *) malloc (strlen (Argv[Index]) + strlen ("\\") + 1);\r
+      if (mOptions.OutputDirectory == NULL) {\r
+        DebugError (NULL, 0, 4001, "Resource: memory can't be allocated", NULL);\r
+        goto Fail;\r
+      }\r
       strcpy (mOptions.OutputDirectory, Argv[Index]);\r
       \r
       CHAR8 lastChar = mOptions.OutputDirectory[strlen(mOptions.OutputDirectory) - 1];\r
@@ -127,7 +139,7 @@ CVfrCompiler::OptionInitialization (
           strcat (mOptions.OutputDirectory, "\\");\r
         }\r
       }\r
-      DebugMsg (NULL, 0, 9, (CHAR8 *) "Output Directory", mOptions.OutputDirectory);\r
+      DebugMsg (NULL, 0, 9, (CHAR8 *) "Output Directory", (CHAR8 *) "%s", mOptions.OutputDirectory);\r
     } else if (stricmp(Argv[Index], "-b") == 0 || stricmp(Argv[Index], "--create-ifr-package") == 0 || stricmp(Argv[Index], "-ibin") == 0) {\r
       mOptions.CreateIfrPkgFile = TRUE;\r
     } else if (stricmp(Argv[Index], "-n") == 0 || stricmp(Argv[Index], "--no-pre-processing") == 0 || stricmp(Argv[Index], "-nopp") == 0) {\r
@@ -149,7 +161,7 @@ CVfrCompiler::OptionInitialization (
         goto Fail;\r
       }\r
       gCVfrStringDB.SetStringFileName(Argv[Index]);\r
-      DebugMsg (NULL, 0, 9, (CHAR8 *) "Input string file path", Argv[Index]);\r
+      DebugMsg (NULL, 0, 9, (CHAR8 *) "Input string file path", (CHAR8 *) "%s", Argv[Index]);\r
     } else if ((stricmp (Argv[Index], "-g") == 0) || (stricmp (Argv[Index], "--guid") == 0)) {\r
       Index++;\r
       Status = StringToGuid (Argv[Index], &mOptions.OverrideClassGuid);\r
@@ -160,6 +172,10 @@ CVfrCompiler::OptionInitialization (
       mOptions.HasOverrideClassGuid = TRUE;\r
     } else if (stricmp(Argv[Index], "-w") == 0 || stricmp(Argv[Index], "--warning-as-error") == 0) {\r
       mOptions.WarningAsError = TRUE;\r
+    } else if (stricmp(Argv[Index], "-a") == 0 ||stricmp(Argv[Index], "--autodefault") == 0) {\r
+      mOptions.AutoDefault = TRUE;\r
+    } else if (stricmp(Argv[Index], "-d") == 0 ||stricmp(Argv[Index], "--checkdefault") == 0) {\r
+      mOptions.CheckDefault = TRUE;\r
     } else {\r
       DebugError (NULL, 0, 1000, "Unknown option", "unrecognized option %s", Argv[Index]);\r
       goto Fail;\r
@@ -170,7 +186,25 @@ CVfrCompiler::OptionInitialization (
     DebugError (NULL, 0, 1001, "Missing option", "VFR file name is not specified.");\r
     goto Fail;\r
   } else {\r
+    if (strlen (Argv[Index]) > MAX_PATH) {\r
+      DebugError (NULL, 0, 1003, "Invalid option value", "VFR file name %s is too long.", Argv[Index]);\r
+      goto Fail;\r
+    }\r
+    mOptions.VfrFileName = (CHAR8 *) malloc (strlen (Argv[Index]) + 1);\r
+    if (mOptions.VfrFileName == NULL) {\r
+      DebugError (NULL, 0, 4001, "Resource: memory can't be allocated", NULL);\r
+      goto Fail;\r
+    }\r
     strcpy (mOptions.VfrFileName, Argv[Index]);\r
+\r
+    if (mOptions.OutputDirectory == NULL) {\r
+      mOptions.OutputDirectory = (CHAR8 *) malloc (1);\r
+      if (mOptions.OutputDirectory == NULL) {\r
+        DebugError (NULL, 0, 4001, "Resource: memory can't be allocated", NULL);\r
+        goto Fail;\r
+      }\r
+      mOptions.OutputDirectory[0] = '\0';\r
+    }\r
   }\r
 \r
   if (SetBaseFileName() != 0) {\r
@@ -193,15 +227,37 @@ CVfrCompiler::OptionInitialization (
 Fail:\r
   SET_RUN_STATUS (STATUS_DEAD);\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
+\r
+  if (mOptions.VfrFileName != NULL) {\r
+    free (mOptions.VfrFileName);\r
+    mOptions.VfrFileName                 = NULL;\r
+  }\r
+  if (mOptions.VfrBaseFileName != NULL) {\r
+    free (mOptions.VfrBaseFileName);\r
+    mOptions.VfrBaseFileName             = NULL;\r
+  }\r
+  if (mOptions.OutputDirectory != NULL) {\r
+    free (mOptions.OutputDirectory);\r
+    mOptions.OutputDirectory             = NULL;\r
+  }\r
+  if (mOptions.PkgOutputFileName != NULL) {\r
+    free (mOptions.PkgOutputFileName);\r
+    mOptions.PkgOutputFileName           = NULL;\r
+  }\r
+  if (mOptions.COutputFileName != NULL) {\r
+    free (mOptions.COutputFileName);\r
+    mOptions.COutputFileName             = NULL;\r
+  }\r
+  if (mOptions.PreprocessorOutputFileName != NULL) {\r
+    free (mOptions.PreprocessorOutputFileName);\r
+    mOptions.PreprocessorOutputFileName  = NULL;\r
+  }\r
+  if (mOptions.RecordListFile != NULL) {\r
+    free (mOptions.RecordListFile);\r
+    mOptions.RecordListFile              = NULL;\r
+  }\r
   if (mOptions.IncludePaths != NULL) {\r
     delete mOptions.IncludePaths;\r
     mOptions.IncludePaths                = NULL;\r
@@ -277,7 +333,7 @@ CVfrCompiler::SetBaseFileName (
 {\r
   CHAR8         *pFileName, *pPath, *pExt;\r
 \r
-  if (mOptions.VfrFileName[0] == '\0') {\r
+  if (mOptions.VfrFileName == NULL) {\r
     return -1;\r
   }\r
 \r
@@ -298,8 +354,20 @@ CVfrCompiler::SetBaseFileName (
     return -1;\r
   }\r
 \r
-  strncpy (mOptions.VfrBaseFileName, pFileName, pExt - pFileName);\r
-  mOptions.VfrBaseFileName[pExt - pFileName] = '\0';\r
+  *pExt = '\0';\r
+  if (strlen (pFileName) > MAX_PATH - 1) {\r
+    *pExt = '.';\r
+    return -1;\r
+  }\r
+\r
+  mOptions.VfrBaseFileName = (CHAR8 *) malloc (strlen (pFileName) + 1);\r
+  if (mOptions.VfrBaseFileName == NULL) {\r
+    *pExt = '.';\r
+    return -1;\r
+  }\r
+\r
+  strcpy (mOptions.VfrBaseFileName, pFileName);\r
+  *pExt = '.';\r
 \r
   return 0;\r
 }\r
@@ -309,7 +377,22 @@ CVfrCompiler::SetPkgOutputFileName (
   VOID\r
   )\r
 {\r
-  if (mOptions.VfrBaseFileName[0] == '\0') {\r
+  INTN Length;\r
+\r
+  if (mOptions.VfrBaseFileName == NULL) {\r
+    return -1;\r
+  }\r
+\r
+  Length = strlen (mOptions.OutputDirectory) +\r
+           strlen (mOptions.VfrBaseFileName) +\r
+           strlen (VFR_PACKAGE_FILENAME_EXTENSION) +\r
+           1;\r
+  if (Length > MAX_PATH) {\r
+    return -1;\r
+  }\r
+\r
+  mOptions.PkgOutputFileName = (CHAR8 *) malloc (Length);\r
+  if (mOptions.PkgOutputFileName == NULL) {\r
     return -1;\r
   }\r
 \r
@@ -325,7 +408,22 @@ CVfrCompiler::SetCOutputFileName (
   VOID\r
   )\r
 {\r
-  if (mOptions.VfrBaseFileName[0] == '\0') {\r
+  INTN Length;\r
+\r
+  if (mOptions.VfrBaseFileName == NULL) {\r
+    return -1;\r
+  }\r
+\r
+  Length = strlen (mOptions.OutputDirectory) +\r
+           strlen (mOptions.VfrBaseFileName) +\r
+           strlen (".c") +\r
+           1;\r
+  if (Length > MAX_PATH) {\r
+    return -1;\r
+  }\r
+\r
+  mOptions.COutputFileName = (CHAR8 *) malloc (Length);\r
+  if (mOptions.COutputFileName == NULL) {\r
     return -1;\r
   }\r
 \r
@@ -341,7 +439,22 @@ CVfrCompiler::SetPreprocessorOutputFileName (
   VOID\r
   )\r
 {\r
-  if (mOptions.VfrBaseFileName[0] == '\0') {\r
+  INTN Length;\r
+\r
+  if (mOptions.VfrBaseFileName == NULL) {\r
+    return -1;\r
+  }\r
+\r
+  Length = strlen (mOptions.OutputDirectory) +\r
+           strlen (mOptions.VfrBaseFileName) +\r
+           strlen (VFR_PREPROCESS_FILENAME_EXTENSION) +\r
+           1;\r
+  if (Length > MAX_PATH) {\r
+    return -1;\r
+  }\r
+\r
+  mOptions.PreprocessorOutputFileName = (CHAR8 *) malloc (Length);\r
+  if (mOptions.PreprocessorOutputFileName == NULL) {\r
     return -1;\r
   }\r
 \r
@@ -357,7 +470,22 @@ CVfrCompiler::SetRecordListFileName (
   VOID\r
   )\r
 {\r
-  if (mOptions.VfrBaseFileName[0] == '\0') {\r
+  INTN Length;\r
+\r
+  if (mOptions.VfrBaseFileName == NULL) {\r
+    return -1;\r
+  }\r
+\r
+  Length = strlen (mOptions.OutputDirectory) +\r
+           strlen (mOptions.VfrBaseFileName) +\r
+           strlen (VFR_RECORDLIST_FILENAME_EXTENSION) +\r
+           1;\r
+  if (Length > MAX_PATH) {\r
+    return -1;\r
+  }\r
+\r
+  mOptions.RecordListFile = (CHAR8 *) malloc (Length);\r
+  if (mOptions.RecordListFile == NULL) {\r
     return -1;\r
   }\r
 \r
@@ -391,6 +519,41 @@ CVfrCompiler::~CVfrCompiler (
   VOID\r
   )\r
 {\r
+  if (mOptions.VfrFileName != NULL) {\r
+    free (mOptions.VfrFileName);\r
+    mOptions.VfrFileName = NULL;\r
+  }\r
+\r
+  if (mOptions.VfrBaseFileName != NULL) {\r
+    free (mOptions.VfrBaseFileName);\r
+    mOptions.VfrBaseFileName = NULL;\r
+  }\r
+\r
+  if (mOptions.OutputDirectory != NULL) {\r
+    free (mOptions.OutputDirectory);\r
+    mOptions.OutputDirectory = NULL;\r
+  }\r
+\r
+  if (mOptions.PkgOutputFileName != NULL) {\r
+    free (mOptions.PkgOutputFileName);\r
+    mOptions.PkgOutputFileName = NULL;\r
+  }\r
+\r
+  if (mOptions.COutputFileName != NULL) {\r
+    free (mOptions.COutputFileName);\r
+    mOptions.COutputFileName = NULL;\r
+  }\r
+\r
+  if (mOptions.PreprocessorOutputFileName != NULL) {\r
+    free (mOptions.PreprocessorOutputFileName);\r
+    mOptions.PreprocessorOutputFileName = NULL;\r
+  }\r
+\r
+  if (mOptions.RecordListFile != NULL) {\r
+    free (mOptions.RecordListFile);\r
+    mOptions.RecordListFile = NULL;\r
+  }\r
+\r
   if (mOptions.IncludePaths != NULL) {\r
     delete mOptions.IncludePaths;\r
     mOptions.IncludePaths = NULL;\r
@@ -437,6 +600,8 @@ CVfrCompiler::Usage (
     "                 format is xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",\r
     "  -w  --warning-as-error",\r
     "                 treat warning as an error",\r
+    "  -a  --autodefaut    generate default value for question opcode if some default is missing",\r
+    "  -d  --checkdefault  check the default information in a question opcode",\r
     NULL\r
     };\r
   for (Index = 0; Help[Index] != NULL; Index++) {\r
@@ -512,7 +677,7 @@ CVfrCompiler::PreProcess (
     goto Fail;\r
   }\r
 \r
-  delete PreProcessCmd;\r
+  delete[] PreProcessCmd;\r
 \r
 Out:\r
   SET_RUN_STATUS (STATUS_PREPROCESSED);\r
@@ -522,7 +687,7 @@ Fail:
   if (!IS_RUN_STATUS(STATUS_DEAD)) {\r
     SET_RUN_STATUS (STATUS_FAILED);\r
   }\r
-  delete PreProcessCmd;\r
+  delete[] PreProcessCmd;\r
 }\r
 \r
 extern UINT8 VfrParserStart (IN FILE *, IN INPUT_INFO_TO_SYNTAX *);\r
@@ -582,53 +747,29 @@ Fail:
 }\r
 \r
 VOID\r
-CVfrCompiler::UpdateInfoForDynamicOpcode (\r
+CVfrCompiler::AdjustBin (\r
   VOID\r
   )\r
 {\r
-  SIfrRecord          *pRecord;\r
+  EFI_VFR_RETURN_CODE Status;\r
 \r
-  if (!gNeedAdjustOpcode) {\r
+  if (!IS_RUN_STATUS(STATUS_COMPILEED)) {\r
     return;\r
   }\r
-  \r
-  //\r
-  // Base on the original offset info to update the record list.\r
-  //\r
-  if (!gCIfrRecordInfoDB.IfrAdjustDynamicOpcodeInRecords()) {\r
-    DebugError (NULL, 0, 1001, "Error parsing vfr file", "Can find the offset in the record.");\r
-  }\r
 \r
-  //\r
-  // Base on the opcode binary length to recalculate the offset for each opcode.\r
-  //\r
-  gCIfrRecordInfoDB.IfrAdjustOffsetForRecord();\r
+  if (gNeedAdjustOpcode) {\r
+    //\r
+    // When parsing the Vfr, has created some opcodes, now need to update the record info.\r
+    //\r
+    gCIfrRecordInfoDB.IfrUpdateRecordInfoForDynamicOpcode (FALSE);\r
+  }\r
 \r
   //\r
-  // Base on the offset to find the binary address.\r
+  // Check whether need to check default info for question or auto add default for question.\r
   //\r
-  pRecord = gCIfrRecordInfoDB.GetRecordInfoFromOffset(gAdjustOpcodeOffset);\r
-  while (pRecord != NULL) {\r
-    pRecord->mIfrBinBuf = gCFormPkg.GetBufAddrBaseOnOffset(pRecord->mOffset);\r
-    if (pRecord->mIfrBinBuf == NULL) {\r
-      DebugError (NULL, 0, 0001, "Error parsing vfr file", " 0x%X. offset not allocated.", pRecord->mOffset);\r
-    }\r
-    pRecord = pRecord->mNext;\r
+  if (mOptions.AutoDefault || mOptions.CheckDefault) {\r
+    gCIfrRecordInfoDB.IfrCheckAddDefaultRecord (mOptions.AutoDefault, mOptions.CheckDefault);\r
   }\r
-}\r
-\r
-VOID\r
-CVfrCompiler::AdjustBin (\r
-  VOID\r
-  )\r
-{\r
-  EFI_VFR_RETURN_CODE Status;\r
-\r
-  if (!IS_RUN_STATUS(STATUS_COMPILEED)) {\r
-    return;\r
-  }\r
-\r
-  UpdateInfoForDynamicOpcode ();\r
 \r
   //\r
   // Check Binary Code consistent between Form and IfrRecord\r