]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/GenVtf/GenVtf.c
BaseTools/GenVtf: Avoid possible NULL pointer dereference
[mirror_edk2.git] / BaseTools / Source / C / GenVtf / GenVtf.c
index 035c268a2ca77989d9260284c390ac91cf63172f..b68d86a97eb935a16760237d0cb83e2a0378a245 100644 (file)
@@ -1,6 +1,8 @@
-/**\r
+/** @file\r
+This file contains functions required to generate a boot strap file (BSF) also \r
+known as the Volume Top File (VTF)\r
 \r
-Copyright (c) 1999 - 2010, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 1999 - 2016, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials are licensed and made available \r
 under the terms and conditions of the BSD License which accompanies this \r
 distribution.  The full text of the license may be found at\r
@@ -9,16 +11,6 @@ http://opensource.org/licenses/bsd-license.php
 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
-\r
-Module Name:\r
-\r
-  GenVtf.c\r
-\r
-Abstract:\r
-\r
-  This file contains functions required to generate a boot strap file (BSF)\r
-  also known as the Volume Top File (VTF)\r
-\r
 **/\r
 \r
 //\r
@@ -804,6 +796,7 @@ Returns:
 \r
   TmpFitPtr         = (FIT_TABLE *) RelativeAddress;\r
   NumFitComponents  = TmpFitPtr->CompSize;\r
+  *FitPtr           = NULL;\r
 \r
   for (Index = 0; Index < NumFitComponents; Index++) {\r
     if ((TmpFitPtr->CvAndType & FIT_TYPE_MASK) == COMP_TYPE_FIT_UNUSED) {\r
@@ -1070,7 +1063,7 @@ Returns:
   CHAR8   Buff5[10];\r
   CHAR8   Token[50];\r
 \r
-  Fp = fopen (VtfInfo->CompSymName, "rb");\r
+  Fp = fopen (LongFilePath (VtfInfo->CompSymName), "rb");\r
 \r
   if (Fp == NULL) {\r
     Error (NULL, 0, 0001, "Error opening file", VtfInfo->CompSymName);\r
@@ -1132,6 +1125,7 @@ Returns:
   EFI_ABORTED      - Aborted due to one of the many reasons like:\r
                       (a) Component Size greater than the specified size.\r
                       (b) Error opening files.\r
+                      (c) Fail to get the FIT table address.\r
 \r
   EFI_INVALID_PARAMETER     Value returned from call to UpdateEntryPoint()\r
   EFI_OUT_OF_RESOURCES      Memory allocation failure.\r
@@ -1141,7 +1135,6 @@ Returns:
   EFI_STATUS  Status;\r
   UINT64      CompStartAddress;\r
   UINT64      FileSize;\r
-  UINT64      NumByteRead;\r
   UINT64      NumAdjustByte;\r
   UINT8       *Buffer;\r
   FILE        *Fp;\r
@@ -1153,7 +1146,7 @@ Returns:
     return EFI_SUCCESS;\r
   }\r
 \r
-  Fp = fopen (VtfInfo->CompBinName, "rb");\r
+  Fp = fopen (LongFilePath (VtfInfo->CompBinName), "rb");\r
 \r
   if (Fp == NULL) {\r
     Error (NULL, 0, 0001, "Error opening file", VtfInfo->CompBinName);\r
@@ -1189,7 +1182,7 @@ Returns:
     //\r
     // Read first 64 bytes of PAL header and use it to find version info\r
     //\r
-    NumByteRead = fread (Buffer, sizeof (UINT8), SIZE_OF_PAL_HEADER, Fp);\r
+    fread (Buffer, sizeof (UINT8), SIZE_OF_PAL_HEADER, Fp);\r
 \r
     //\r
     // PAL header contains the version info. Currently, we will use the header\r
@@ -1200,7 +1193,7 @@ Returns:
     }\r
   }\r
 \r
-  NumByteRead = fread (Buffer, sizeof (UINT8), (UINTN) FileSize, Fp);\r
+  fread (Buffer, sizeof (UINT8), (UINTN) FileSize, Fp);\r
   fclose (Fp);\r
 \r
   //\r
@@ -1248,6 +1241,10 @@ Returns:
   }\r
 \r
   GetNextAvailableFitPtr (&CompFitPtr);\r
+  if (CompFitPtr == NULL) {\r
+    free (Buffer);\r
+    return EFI_ABORTED;\r
+  }\r
 \r
   CompFitPtr->CompAddress = CompStartAddress | IPF_CACHE_BIT;\r
   if ((FileSize % 16) != 0) {\r
@@ -1329,12 +1326,11 @@ Returns:
   UINT64      AbsAddress;\r
   UINTN       RelativeAddress;\r
   UINT64      FileSize;\r
-  UINT64      NumByteRead;\r
   UINT8       *Buffer;\r
   FILE        *Fp;\r
   FIT_TABLE   *PalFitPtr;\r
 \r
-  Fp = fopen (VtfInfo->CompBinName, "rb");\r
+  Fp = fopen (LongFilePath (VtfInfo->CompBinName), "rb");\r
 \r
   if (Fp == NULL) {\r
     Error (NULL, 0, 0001, "Error opening file", VtfInfo->CompBinName);\r
@@ -1367,7 +1363,7 @@ Returns:
   //\r
   // Read, Get version Info and discard the PAL header.\r
   //\r
-  NumByteRead = fread (Buffer, sizeof (UINT8), SIZE_OF_PAL_HEADER, Fp);\r
+  fread (Buffer, sizeof (UINT8), SIZE_OF_PAL_HEADER, Fp);\r
 \r
   //\r
   // Extract the version info from header of PAL_A. Once done, discrad this buffer\r
@@ -1379,7 +1375,7 @@ Returns:
   //\r
   // Read PAL_A file in a buffer\r
   //\r
-  NumByteRead = fread (Buffer, sizeof (UINT8), (UINTN) FileSize, Fp);\r
+  fread (Buffer, sizeof (UINT8), (UINTN) FileSize, Fp);\r
   fclose (Fp);\r
 \r
   PalStartAddress       = Fv1EndAddress - (SIZE_TO_OFFSET_PAL_A_END + FileSize);\r
@@ -1553,7 +1549,7 @@ Returns:
     VtfBuffer = (VOID *) RelativeAddress;\r
   }\r
 \r
-  Fp = fopen (FileName, "wb");\r
+  Fp = fopen (LongFilePath (FileName), "wb");\r
   if (Fp == NULL) {\r
     Error (NULL, 0, 0001, "Error opening file", FileName);\r
     return EFI_ABORTED;\r
@@ -1724,7 +1720,7 @@ Returns:
 \r
 --*/\r
 {\r
-  if ((BaseAddress >= 0) && (FwVolSize > 0x40) && ((BaseAddress + FwVolSize) % 8 == 0)) {\r
+  if ((FwVolSize > 0x40) && ((BaseAddress + FwVolSize) % 8 == 0)) {\r
     return EFI_SUCCESS;\r
   }\r
 \r
@@ -1759,14 +1755,13 @@ Returns:
   UINT8 *Buffer;\r
   UINT8 *LocalVtfBuffer;\r
   UINTN FileSize;\r
-  UINTN NumByteRead;\r
   FILE  *Fp;\r
 \r
   if (!strcmp (FileName, "")) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  Fp = fopen (FileName, "rb");\r
+  Fp = fopen (LongFilePath (FileName), "rb");\r
 \r
   if (Fp == NULL) {\r
     Error (NULL, 0, 0001, "Error opening file", FileName);\r
@@ -1784,7 +1779,7 @@ Returns:
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
-  NumByteRead     = fread (Buffer, sizeof (UINT8), FileSize, Fp);\r
+  fread (Buffer, sizeof (UINT8), FileSize, Fp);\r
 \r
   LocalVtfBuffer  = (UINT8 *) Vtf1EndBuffer - SIZE_IA32_RESET_VECT;\r
   memcpy (LocalVtfBuffer, Buffer, FileSize);\r
@@ -2121,7 +2116,6 @@ Returns:
   FILE        *Fp;\r
   UINT64      *StartAddressPtr;\r
   UINTN       FirstFwVSize;\r
-  UINTN       NumByte;\r
 \r
   StartAddressPtr   = malloc (sizeof (UINT64));\r
   if (StartAddressPtr == NULL) {\r
@@ -2129,7 +2123,7 @@ Returns:
   }\r
   *StartAddressPtr = StartAddress;\r
 \r
-  Fp = fopen (OutFileName1, "rb");\r
+  Fp = fopen (LongFilePath (OutFileName1), "rb");\r
 \r
   if (Fp == NULL) {\r
     Error (NULL, 0, 0001, "Error opening file", OutFileName1);\r
@@ -2141,7 +2135,7 @@ Returns:
 \r
   FirstFwVSize = _filelength (fileno (Fp));\r
   fseek (Fp, (long) (FirstFwVSize - (UINTN) (SIZE_IA32_RESET_VECT + SIZE_SALE_ENTRY_POINT)), SEEK_SET);\r
-  NumByte = fwrite ((VOID *) StartAddressPtr, sizeof (UINT64), 1, Fp);\r
+  fwrite ((VOID *) StartAddressPtr, sizeof (UINT64), 1, Fp);\r
 \r
   if (Fp) {\r
     fclose (Fp);\r
@@ -2187,12 +2181,12 @@ Returns:
 {\r
   FILE    *SourceFile;\r
   FILE    *DestFile;\r
-  CHAR8   Buffer[_MAX_PATH];\r
-  CHAR8   Type[_MAX_PATH];\r
-  CHAR8   Address[_MAX_PATH];\r
-  CHAR8   Section[_MAX_PATH];\r
-  CHAR8   Token[_MAX_PATH];\r
-  CHAR8   BaseToken[_MAX_PATH];\r
+  CHAR8   Buffer[MAX_LONG_FILE_PATH];\r
+  CHAR8   Type[MAX_LONG_FILE_PATH];\r
+  CHAR8   Address[MAX_LONG_FILE_PATH];\r
+  CHAR8   Section[MAX_LONG_FILE_PATH];\r
+  CHAR8   Token[MAX_LONG_FILE_PATH];\r
+  CHAR8   BaseToken[MAX_LONG_FILE_PATH];\r
   UINT64  TokenAddress;\r
   long    StartLocation;\r
 \r
@@ -2206,7 +2200,7 @@ Returns:
   //\r
   // Open the source file\r
   //\r
-  SourceFile = fopen (SourceFileName, "r");\r
+  SourceFile = fopen (LongFilePath (SourceFileName), "r");\r
   if (SourceFile == NULL) {\r
 \r
     //\r
@@ -2225,7 +2219,7 @@ Returns:
   //\r
   // Open the destination file\r
   //\r
-  DestFile = fopen (DestFileName, "a+");\r
+  DestFile = fopen (LongFilePath (DestFileName), "a+");\r
   if (DestFile == NULL) {\r
     fclose (SourceFile);\r
     Error (NULL, 0, 0001, "Error opening file", DestFileName);\r
@@ -2256,7 +2250,7 @@ Returns:
   //\r
   // Read the first line\r
   //\r
-  if (fgets (Buffer, _MAX_PATH, SourceFile) == NULL) {\r
+  if (fgets (Buffer, MAX_LONG_FILE_PATH, SourceFile) == NULL) {\r
     Buffer[0] = 0;\r
   }\r
 \r
@@ -2383,7 +2377,7 @@ Returns:
 \r
 --*/\r
 {\r
-  fprintf (stdout, "%s Version %d.%d\n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION);\r
+  fprintf (stdout, "%s Version %d.%d %s \n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION, __BUILD_VERSION);\r
 }\r
 \r
 VOID\r
@@ -2414,7 +2408,7 @@ Returns:
   //\r
   // Copyright declaration\r
   //\r
-  fprintf (stdout, "Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.\n\n");\r
+  fprintf (stdout, "Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.\n\n");\r
   //\r
   // Details Option\r
   //\r
@@ -2550,7 +2544,7 @@ Returns:
       // Get the input VTF file name\r
       //\r
       VtfFileName = argv[Index+1];\r
-      VtfFP = fopen(VtfFileName, "rb");\r
+      VtfFP = fopen (LongFilePath (VtfFileName), "rb");\r
       if (VtfFP == NULL) {\r
         Error (NULL, 0, 0001, "Error opening file", VtfFileName);\r
         goto ERROR;\r
@@ -2663,21 +2657,26 @@ Returns:
     }\r
     SymFileName = VTF_SYM_FILE;\r
   } else {\r
+    assert (OutFileName1);\r
     INTN OutFileNameLen = strlen(OutFileName1);\r
-    INTN Index;\r
+    INTN NewIndex;\r
 \r
-    for (Index = OutFileNameLen; Index > 0; --Index) {\r
-      if (OutFileName1[Index] == '/' || OutFileName1[Index] == '\\') {\r
+    for (NewIndex = OutFileNameLen; NewIndex > 0; --NewIndex) {\r
+      if (OutFileName1[NewIndex] == '/' || OutFileName1[NewIndex] == '\\') {\r
         break;\r
       }\r
     }\r
-    if (Index == 0) {\r
+    if (NewIndex == 0) {\r
       SymFileName = VTF_SYM_FILE;\r
     } else {\r
-      INTN SymFileNameLen = Index + 1 + strlen(VTF_SYM_FILE);\r
+      INTN SymFileNameLen = NewIndex + 1 + strlen(VTF_SYM_FILE);\r
       SymFileName = malloc(SymFileNameLen + 1);\r
-      memcpy(SymFileName, OutFileName1, Index + 1);\r
-      memcpy(SymFileName + Index + 1, VTF_SYM_FILE, strlen(VTF_SYM_FILE));\r
+      if (SymFileName == NULL) {\r
+        Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r
+        goto ERROR;\r
+      }\r
+      memcpy(SymFileName, OutFileName1, NewIndex + 1);\r
+      memcpy(SymFileName + NewIndex + 1, VTF_SYM_FILE, strlen(VTF_SYM_FILE));\r
       SymFileName[SymFileNameLen] = '\0';\r
     }\r
     if (DebugMode) {\r