]> git.proxmox.com Git - mirror_edk2.git/commitdiff
BaseTools: do basic check in FvImage with header size and signature
authorzhijufan <zhijux.fan@intel.com>
Tue, 25 Sep 2018 02:55:46 +0000 (10:55 +0800)
committerYonghong Zhu <yonghong.zhu@intel.com>
Mon, 15 Oct 2018 02:16:07 +0000 (10:16 +0800)
Add some basic check in FvImage with header size and signature.

Fixes: https://bugzilla.tianocore.org/show_bug.cgi?id=1181
Cc: Liming Gao <liming.gao@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Zhiju.Fan <zhijux.fan@intel.com>
Reviewed-by: Yonghong Zhu <yonghong.zhu@intel.com>
BaseTools/Source/Python/GenFds/Fv.py

index 0d005ebf5bb630fa9e5a6124be2b690da4dd1e54..510f2834a88122a7dea4004945837e15d6f34b71 100644 (file)
@@ -195,32 +195,36 @@ class FV (FvClassObject):
             #\r
             # Write the Fv contents to Buffer\r
             #\r
-            if os.path.isfile(FvOutputFile):\r
+            if os.path.isfile(FvOutputFile) and os.path.getsize(FvOutputFile) >= 0x48:\r
                 FvFileObj = open(FvOutputFile, 'rb')\r
-                GenFdsGlobalVariable.VerboseLogger("\nGenerate %s FV Successfully" % self.UiFvName)\r
-                GenFdsGlobalVariable.SharpCounter = 0\r
-\r
-                Buffer.write(FvFileObj.read())\r
-                FvFileObj.seek(0)\r
                 # PI FvHeader is 0x48 byte\r
                 FvHeaderBuffer = FvFileObj.read(0x48)\r
-                # FV alignment position.\r
-                FvAlignmentValue = 1 << (ord(FvHeaderBuffer[0x2E]) & 0x1F)\r
-                if FvAlignmentValue >= 0x400:\r
-                    if FvAlignmentValue >= 0x100000:\r
-                        if FvAlignmentValue >= 0x1000000:\r
-                        #The max alignment supported by FFS is 16M.\r
-                            self.FvAlignment = "16M"\r
+                Signature = FvHeaderBuffer[0x28:0x32]\r
+                if Signature and Signature.startswith('_FVH'):\r
+                    GenFdsGlobalVariable.VerboseLogger("\nGenerate %s FV Successfully" % self.UiFvName)\r
+                    GenFdsGlobalVariable.SharpCounter = 0\r
+\r
+                    Buffer.write(FvFileObj.read())\r
+                    FvFileObj.seek(0)\r
+                    # FV alignment position.\r
+                    FvAlignmentValue = 1 << (ord(FvHeaderBuffer[0x2E]) & 0x1F)\r
+                    if FvAlignmentValue >= 0x400:\r
+                        if FvAlignmentValue >= 0x100000:\r
+                            if FvAlignmentValue >= 0x1000000:\r
+                            #The max alignment supported by FFS is 16M.\r
+                                self.FvAlignment = "16M"\r
+                            else:\r
+                                self.FvAlignment = str(FvAlignmentValue / 0x100000) + "M"\r
                         else:\r
-                            self.FvAlignment = str(FvAlignmentValue / 0x100000) + "M"\r
+                            self.FvAlignment = str(FvAlignmentValue / 0x400) + "K"\r
                     else:\r
-                        self.FvAlignment = str(FvAlignmentValue / 0x400) + "K"\r
+                        # FvAlignmentValue is less than 1K\r
+                        self.FvAlignment = str (FvAlignmentValue)\r
+                    FvFileObj.close()\r
+                    GenFdsGlobalVariable.ImageBinDict[self.UiFvName.upper() + 'fv'] = FvOutputFile\r
+                    GenFdsGlobalVariable.LargeFileInFvFlags.pop()\r
                 else:\r
-                    # FvAlignmentValue is less than 1K\r
-                    self.FvAlignment = str (FvAlignmentValue)\r
-                FvFileObj.close()\r
-                GenFdsGlobalVariable.ImageBinDict[self.UiFvName.upper() + 'fv'] = FvOutputFile\r
-                GenFdsGlobalVariable.LargeFileInFvFlags.pop()\r
+                    GenFdsGlobalVariable.ErrorLogger("Invalid FV file %s." % self.UiFvName)\r
             else:\r
                 GenFdsGlobalVariable.ErrorLogger("Failed to generate %s FV file." %self.UiFvName)\r
         return FvOutputFile\r