From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=134.134.136.24; helo=mga09.intel.com; envelope-from=yonghong.zhu@intel.com; receiver=edk2-devel@lists.01.org Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 0B6202115C096 for ; Sat, 29 Sep 2018 01:46:51 -0700 (PDT) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 29 Sep 2018 01:46:51 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.54,319,1534834800"; d="scan'208";a="74834369" Received: from shwdeopenpsi168.ccr.corp.intel.com ([10.239.158.129]) by fmsmga008.fm.intel.com with ESMTP; 29 Sep 2018 01:46:50 -0700 From: Yonghong Zhu To: edk2-devel@lists.01.org Cc: zhijufan , Liming Gao Date: Sat, 29 Sep 2018 16:46:49 +0800 Message-Id: <1538210809-19672-1-git-send-email-yonghong.zhu@intel.com> X-Mailer: git-send-email 2.6.1.windows.1 Subject: [Patch] BaseTools: do basic check in FvImage with header size and signature X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 29 Sep 2018 08:46:52 -0000 From: zhijufan Add some basic check in FvImage with header size and signature. Fixes: https://bugzilla.tianocore.org/show_bug.cgi?id=1181 Cc: Liming Gao Cc: Yonghong Zhu Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Zhiju.Fan --- BaseTools/Source/Python/GenFds/Fv.py | 44 ++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/BaseTools/Source/Python/GenFds/Fv.py b/BaseTools/Source/Python/GenFds/Fv.py index 0d005eb..510f283 100644 --- a/BaseTools/Source/Python/GenFds/Fv.py +++ b/BaseTools/Source/Python/GenFds/Fv.py @@ -193,36 +193,40 @@ class FV (FvClassObject): ) # # Write the Fv contents to Buffer # - if os.path.isfile(FvOutputFile): + if os.path.isfile(FvOutputFile) and os.path.getsize(FvOutputFile) >= 0x48: FvFileObj = open(FvOutputFile, 'rb') - GenFdsGlobalVariable.VerboseLogger("\nGenerate %s FV Successfully" % self.UiFvName) - GenFdsGlobalVariable.SharpCounter = 0 - - Buffer.write(FvFileObj.read()) - FvFileObj.seek(0) # PI FvHeader is 0x48 byte FvHeaderBuffer = FvFileObj.read(0x48) - # FV alignment position. - FvAlignmentValue = 1 << (ord(FvHeaderBuffer[0x2E]) & 0x1F) - if FvAlignmentValue >= 0x400: - if FvAlignmentValue >= 0x100000: - if FvAlignmentValue >= 0x1000000: - #The max alignment supported by FFS is 16M. - self.FvAlignment = "16M" + Signature = FvHeaderBuffer[0x28:0x32] + if Signature and Signature.startswith('_FVH'): + GenFdsGlobalVariable.VerboseLogger("\nGenerate %s FV Successfully" % self.UiFvName) + GenFdsGlobalVariable.SharpCounter = 0 + + Buffer.write(FvFileObj.read()) + FvFileObj.seek(0) + # FV alignment position. + FvAlignmentValue = 1 << (ord(FvHeaderBuffer[0x2E]) & 0x1F) + if FvAlignmentValue >= 0x400: + if FvAlignmentValue >= 0x100000: + if FvAlignmentValue >= 0x1000000: + #The max alignment supported by FFS is 16M. + self.FvAlignment = "16M" + else: + self.FvAlignment = str(FvAlignmentValue / 0x100000) + "M" else: - self.FvAlignment = str(FvAlignmentValue / 0x100000) + "M" + self.FvAlignment = str(FvAlignmentValue / 0x400) + "K" else: - self.FvAlignment = str(FvAlignmentValue / 0x400) + "K" + # FvAlignmentValue is less than 1K + self.FvAlignment = str (FvAlignmentValue) + FvFileObj.close() + GenFdsGlobalVariable.ImageBinDict[self.UiFvName.upper() + 'fv'] = FvOutputFile + GenFdsGlobalVariable.LargeFileInFvFlags.pop() else: - # FvAlignmentValue is less than 1K - self.FvAlignment = str (FvAlignmentValue) - FvFileObj.close() - GenFdsGlobalVariable.ImageBinDict[self.UiFvName.upper() + 'fv'] = FvOutputFile - GenFdsGlobalVariable.LargeFileInFvFlags.pop() + GenFdsGlobalVariable.ErrorLogger("Invalid FV file %s." % self.UiFvName) else: GenFdsGlobalVariable.ErrorLogger("Failed to generate %s FV file." %self.UiFvName) return FvOutputFile ## _GetBlockSize() -- 2.6.1.windows.1