From c3f0829b341618b12d69874ff676d9f720a32f3c Mon Sep 17 00:00:00 2001 From: Chasel Chiu Date: Fri, 21 Jun 2019 08:43:22 +0800 Subject: IntelFsp2Pkg: FSP Python scripts to support 3.x. https://bugzilla.tianocore.org/show_bug.cgi?id=1930 Updated FSP Python scripts to support both 2.x and 3.x. Test: . Verified with Python 2.7.12 and 3.6.6. . Verified tool result is the same before the change. . Both py -2 and py -3 built binary can boot. Cc: Maurice Ma Cc: Nate DeSimone Cc: Star Zeng Signed-off-by: Chasel Chiu Reviewed-by: Star Zeng Reviewed-by: Nate DeSimone --- IntelFsp2Pkg/Tools/GenCfgOpt.py | 61 ++++++++++++++++---------------- IntelFsp2Pkg/Tools/PatchFv.py | 36 ++++++++++++------- IntelFsp2Pkg/Tools/SplitFspBin.py | 74 +++++++++++++++++++++++++++++---------- 3 files changed, 109 insertions(+), 62 deletions(-) (limited to 'IntelFsp2Pkg') diff --git a/IntelFsp2Pkg/Tools/GenCfgOpt.py b/IntelFsp2Pkg/Tools/GenCfgOpt.py index 450c4e3eb9..c4e1e6239d 100644 --- a/IntelFsp2Pkg/Tools/GenCfgOpt.py +++ b/IntelFsp2Pkg/Tools/GenCfgOpt.py @@ -1,6 +1,6 @@ ## @ GenCfgOpt.py # -# Copyright (c) 2014 - 2018, Intel Corporation. All rights reserved.
+# Copyright (c) 2014 - 2019, Intel Corporation. All rights reserved.
# SPDX-License-Identifier: BSD-2-Clause-Patent # ## @@ -10,6 +10,7 @@ import re import sys import struct from datetime import date +from functools import reduce # Generated file copyright header @@ -90,11 +91,11 @@ class CLogicalExpression: self.string = '' def errExit(self, err = ''): - print "ERROR: Express parsing for:" - print " %s" % self.string - print " %s^" % (' ' * self.index) + print ("ERROR: Express parsing for:") + print (" %s" % self.string) + print (" %s^" % (' ' * self.index)) if err: - print "INFO : %s" % err + print ("INFO : %s" % err) raise SystemExit def getNonNumber (self, n1, n2): @@ -338,15 +339,15 @@ EndList else: Error = 0 if self.Debug: - print "INFO : Macro dictionary:" + print ("INFO : Macro dictionary:") for Each in self._MacroDict: - print " $(%s) = [ %s ]" % (Each , self._MacroDict[Each]) + print (" $(%s) = [ %s ]" % (Each , self._MacroDict[Each])) return Error def EvaulateIfdef (self, Macro): Result = Macro in self._MacroDict if self.Debug: - print "INFO : Eval Ifdef [%s] : %s" % (Macro, Result) + print ("INFO : Eval Ifdef [%s] : %s" % (Macro, Result)) return Result def ExpandMacros (self, Input): @@ -359,7 +360,7 @@ EndList Line = Line.replace(Each, self._MacroDict[Variable]) else: if self.Debug: - print "WARN : %s is not defined" % Each + print ("WARN : %s is not defined" % Each) Line = Line.replace(Each, Each[2:-1]) return Line @@ -372,7 +373,7 @@ EndList Line = Line.replace(PcdName, self._PcdsDict[PcdName]) else: if self.Debug: - print "WARN : %s is not defined" % PcdName + print ("WARN : %s is not defined" % PcdName) return Line def EvaluateExpress (self, Expr): @@ -381,7 +382,7 @@ EndList LogExpr = CLogicalExpression() Result = LogExpr.evaluateExpress (ExpExpr) if self.Debug: - print "INFO : Eval Express [%s] : %s" % (Expr, Result) + print ("INFO : Eval Express [%s] : %s" % (Expr, Result)) return Result def FormatListValue(self, ConfigDict): @@ -406,7 +407,7 @@ EndList bytearray = [] for each in dataarray: value = each - for loop in xrange(unit): + for loop in range(int(unit)): bytearray.append("0x%02X" % (value & 0xFF)) value = value >> 8 newvalue = '{' + ','.join(bytearray) + '}' @@ -548,7 +549,7 @@ EndList if Match: self._MacroDict[Match.group(1)] = Match.group(2) if self.Debug: - print "INFO : DEFINE %s = [ %s ]" % (Match.group(1), Match.group(2)) + print ("INFO : DEFINE %s = [ %s ]" % (Match.group(1), Match.group(2))) elif IsPcdSect: #gSiPkgTokenSpaceGuid.PcdTxtEnable|FALSE #gSiPkgTokenSpaceGuid.PcdOverclockEnable|TRUE @@ -556,7 +557,7 @@ EndList if Match: self._PcdsDict[Match.group(1)] = Match.group(2) if self.Debug: - print "INFO : PCD %s = [ %s ]" % (Match.group(1), Match.group(2)) + print ("INFO : PCD %s = [ %s ]" % (Match.group(1), Match.group(2))) i = 0 while i < len(BuildOptionPcd): Match = re.match("\s*([\w\.]+)\s*\=\s*(\w+)", BuildOptionPcd[i]) @@ -774,7 +775,7 @@ EndList bitsvalue = bitsvalue[::-1] bitslen = len(bitsvalue) if start > bitslen or end > bitslen: - print "Invalid bits offset [%d,%d] for %s" % (start, end, subitem['name']) + print ("Invalid bits offset [%d,%d] for %s" % (start, end, subitem['name'])) raise SystemExit return hex(int(bitsvalue[start:end][::-1], 2)) @@ -1031,7 +1032,7 @@ EndList if Match and Match.group(3) == 'END': if (StructName != Match.group(1)) or (VariableName != Match.group(2)): - print "Unmatched struct name '%s' and '%s' !" % (StructName, Match.group(1)) + print ("Unmatched struct name '%s' and '%s' !" % (StructName, Match.group(1))) else: if IsUpdHdrDefined != True or IsUpdHeader != True: NewTextBody.append ('} %s;\n\n' % StructName) @@ -1464,11 +1465,11 @@ EndList def Usage(): - print "GenCfgOpt Version 0.53" - print "Usage:" - print " GenCfgOpt UPDTXT PlatformDscFile BuildFvDir [-D Macros]" - print " GenCfgOpt HEADER PlatformDscFile BuildFvDir InputHFile [-D Macros]" - print " GenCfgOpt GENBSF PlatformDscFile BuildFvDir BsfOutFile [-D Macros]" + print ("GenCfgOpt Version 0.54") + print ("Usage:") + print (" GenCfgOpt UPDTXT PlatformDscFile BuildFvDir [-D Macros]") + print (" GenCfgOpt HEADER PlatformDscFile BuildFvDir InputHFile [-D Macros]") + print (" GenCfgOpt GENBSF PlatformDscFile BuildFvDir BsfOutFile [-D Macros]") def Main(): # @@ -1489,7 +1490,7 @@ def Main(): else: DscFile = sys.argv[2] if not os.path.exists(DscFile): - print "ERROR: Cannot open DSC file '%s' !" % DscFile + print ("ERROR: Cannot open DSC file '%s' !" % DscFile) return 2 OutFile = '' @@ -1501,7 +1502,7 @@ def Main(): Start = 5 if argc > Start: if GenCfgOpt.ParseMacros(sys.argv[Start:]) != 0: - print "ERROR: Macro parsing failed !" + print ("ERROR: Macro parsing failed !") return 3 FvDir = sys.argv[3] @@ -1509,11 +1510,11 @@ def Main(): os.makedirs(FvDir) if GenCfgOpt.ParseDscFile(DscFile, FvDir) != 0: - print "ERROR: %s !" % GenCfgOpt.Error + print ("ERROR: %s !" % GenCfgOpt.Error) return 5 if GenCfgOpt.UpdateSubRegionDefaultValue() != 0: - print "ERROR: %s !" % GenCfgOpt.Error + print ("ERROR: %s !" % GenCfgOpt.Error) return 7 if sys.argv[1] == "UPDTXT": @@ -1521,23 +1522,23 @@ def Main(): if Ret != 0: # No change is detected if Ret == 256: - print "INFO: %s !" % (GenCfgOpt.Error) + print ("INFO: %s !" % (GenCfgOpt.Error)) else : - print "ERROR: %s !" % (GenCfgOpt.Error) + print ("ERROR: %s !" % (GenCfgOpt.Error)) return Ret elif sys.argv[1] == "HEADER": if GenCfgOpt.CreateHeaderFile(OutFile) != 0: - print "ERROR: %s !" % GenCfgOpt.Error + print ("ERROR: %s !" % GenCfgOpt.Error) return 8 elif sys.argv[1] == "GENBSF": if GenCfgOpt.GenerateBsfFile(OutFile) != 0: - print "ERROR: %s !" % GenCfgOpt.Error + print ("ERROR: %s !" % GenCfgOpt.Error) return 9 else: if argc < 5: Usage() return 1 - print "ERROR: Unknown command '%s' !" % sys.argv[1] + print ("ERROR: Unknown command '%s' !" % sys.argv[1]) Usage() return 1 return 0 diff --git a/IntelFsp2Pkg/Tools/PatchFv.py b/IntelFsp2Pkg/Tools/PatchFv.py index 19bffd146a..2173984dea 100644 --- a/IntelFsp2Pkg/Tools/PatchFv.py +++ b/IntelFsp2Pkg/Tools/PatchFv.py @@ -1,6 +1,6 @@ ## @ PatchFv.py # -# Copyright (c) 2014 - 2018, Intel Corporation. All rights reserved.
+# Copyright (c) 2014 - 2019, Intel Corporation. All rights reserved.
# SPDX-License-Identifier: BSD-2-Clause-Patent # ## @@ -25,7 +25,10 @@ def readDataFromFile (binfile, offset, len=1): if (offval & 0x80000000): offval = fsize - (0xFFFFFFFF - offval + 1) fd.seek(offval) - bytearray = [ord(b) for b in fd.read(len)] + if sys.version_info[0] < 3: + bytearray = [ord(b) for b in fd.read(len)] + else: + bytearray = [b for b in fd.read(len)] value = 0 idx = len - 1 while idx >= 0: @@ -45,7 +48,7 @@ def IsFspHeaderValid (binfile): fd = open (binfile, "rb") bindat = fd.read(0x200) # only read first 0x200 bytes fd.close() - HeaderList = ['FSPH' , 'FSPP' , 'FSPE'] # Check 'FSPH', 'FSPP', and 'FSPE' in the FSP header + HeaderList = [b'FSPH' , b'FSPP' , b'FSPE'] # Check 'FSPH', 'FSPP', and 'FSPE' in the FSP header OffsetList = [] for each in HeaderList: if each in bindat: @@ -55,7 +58,10 @@ def IsFspHeaderValid (binfile): OffsetList.append(idx) if not OffsetList[0] or not OffsetList[1]: # If 'FSPH' or 'FSPP' is missing, it will return false return False - Revision = ord(bindat[OffsetList[0] + 0x0B]) + if sys.version_info[0] < 3: + Revision = ord(bindat[OffsetList[0] + 0x0B]) + else: + Revision = bindat[OffsetList[0] + 0x0B] # # if revision is bigger than 1, it means it is FSP v1.1 or greater revision, which must contain 'FSPE'. # @@ -86,7 +92,10 @@ def patchDataInFile (binfile, offset, value, len=1): value = value >> 8 idx = idx + 1 fd.seek(offval) - fd.write("".join(chr(b) for b in bytearray)) + if sys.version_info[0] < 3: + fd.write("".join(chr(b) for b in bytearray)) + else: + fd.write(bytes(bytearray)) fd.close() return len @@ -791,7 +800,7 @@ class Symbols: # retval ret # def getSymbols(self, value): - if self.dictSymbolAddress.has_key(value): + if value in self.dictSymbolAddress: # Module:Function ret = int (self.dictSymbolAddress[value], 16) else: @@ -827,8 +836,9 @@ class Symbols: # # Print out the usage # -def usage(): - print "Usage: \n\tPatchFv FvBuildDir [FvFileBaseNames:]FdFileBaseNameToPatch \"Offset, Value\"" +def Usage(): + print ("PatchFv Version 0.50") + print ("Usage: \n\tPatchFv FvBuildDir [FvFileBaseNames:]FdFileBaseNameToPatch \"Offset, Value\"") def main(): # @@ -847,7 +857,7 @@ def main(): # If it fails to create dictionaries, then return an error. # if symTables.createDicts(sys.argv[1], sys.argv[2]) != 0: - print "ERROR: Failed to create symbol dictionary!!" + print ("ERROR: Failed to create symbol dictionary!!") return 2 # @@ -907,7 +917,7 @@ def main(): if ret: raise Exception ("Patch failed for offset 0x%08X" % offset) else: - print "Patched offset 0x%08X:[%08X] with value 0x%08X # %s" % (offset, oldvalue, value, comment) + print ("Patched offset 0x%08X:[%08X] with value 0x%08X # %s" % (offset, oldvalue, value, comment)) elif command == "COPY": # @@ -928,13 +938,13 @@ def main(): if ret: raise Exception ("Copy failed from offset 0x%08X to offset 0x%08X!" % (src, dest)) else : - print "Copied %d bytes from offset 0x%08X ~ offset 0x%08X # %s" % (clen, src, dest, comment) + print ("Copied %d bytes from offset 0x%08X ~ offset 0x%08X # %s" % (clen, src, dest, comment)) else: raise Exception ("Unknown command %s!" % command) return 0 - except Exception as (ex): - print "ERROR: %s" % ex + except Exception as ex: + print ("ERROR: %s" % ex) return 1 if __name__ == '__main__': diff --git a/IntelFsp2Pkg/Tools/SplitFspBin.py b/IntelFsp2Pkg/Tools/SplitFspBin.py index 15c8bebee2..b326241fa9 100644 --- a/IntelFsp2Pkg/Tools/SplitFspBin.py +++ b/IntelFsp2Pkg/Tools/SplitFspBin.py @@ -12,6 +12,7 @@ import copy import struct import argparse from ctypes import * +from functools import reduce """ This utility supports some operations for Intel FSP 1.x/2.x image. @@ -340,6 +341,31 @@ def Bytes2Val (bytes): def Val2Bytes (value, blen): return [(value>>(i*8) & 0xff) for i in range(blen)] +def IsIntegerType (val): + if sys.version_info[0] < 3: + if type(val) in (int, long): + return True + else: + if type(val) is int: + return True + return False + +def IsStrType (val): + if sys.version_info[0] < 3: + if type(val) is str: + return True + else: + if type(val) is bytes: + return True + return False + +def HandleNameStr (val): + if sys.version_info[0] < 3: + rep = "0x%X ('%s')" % (Bytes2Val (bytearray (val)), val) + else: + rep = "0x%X ('%s')" % (Bytes2Val (bytearray (val)), str (val, 'utf-8')) + return rep + def OutputStruct (obj, indent = 0, plen = 0): if indent: body = '' @@ -361,15 +387,19 @@ def OutputStruct (obj, indent = 0, plen = 0): body += OutputStruct (val, indent + 1) plen -= sizeof(val) else: - if type(val) is str: - rep = "0x%X ('%s')" % (Bytes2Val(bytearray(val)), val) - elif type(val) in (int, long): + if IsStrType (val): + rep = HandleNameStr (val) + elif IsIntegerType (val): rep = '0x%X' % val elif isinstance(val, c_uint24): rep = '0x%X' % val.get_value() elif 'c_ubyte_Array' in str(type(val)): if sizeof(val) == 16: - rep = str(uuid.UUID(bytes = str(bytearray(val)))).upper() + if sys.version_info[0] < 3: + rep = str(bytearray(val)) + else: + rep = bytes(val) + rep = str(uuid.UUID(bytes_le = rep)).upper() else: res = ['0x%02X'%i for i in bytearray(val)] rep = '[%s]' % (','.join(res)) @@ -487,7 +517,7 @@ class FirmwareDevice: self.FvList = [] while offset < fdsize: fvh = EFI_FIRMWARE_VOLUME_HEADER.from_buffer (self.FdData, offset) - if '_FVH' != fvh.Signature: + if b'_FVH' != fvh.Signature: raise Exception("ERROR: Invalid FV header !") fv = FirmwareVolume (offset, self.FdData[offset:offset + fvh.FvLength]) fv.ParseFv () @@ -524,7 +554,7 @@ class FirmwareDevice: fspoffset = fv.Offset offset = fspoffset + fihoffset fih = FSP_INFORMATION_HEADER.from_buffer (self.FdData, offset) - if 'FSPH' != fih.Signature: + if b'FSPH' != fih.Signature: continue offset += fih.HeaderLength @@ -532,7 +562,7 @@ class FirmwareDevice: plist = [] while True: fch = FSP_COMMON_HEADER.from_buffer (self.FdData, offset) - if 'FSPP' != fch.Signature: + if b'FSPP' != fch.Signature: offset += fch.HeaderLength offset = AlignPtr(offset, 4) else: @@ -557,9 +587,9 @@ class PeTeImage: def __init__(self, offset, data): self.Offset = offset tehdr = EFI_TE_IMAGE_HEADER.from_buffer (data, 0) - if tehdr.Signature == 'VZ': # TE image + if tehdr.Signature == b'VZ': # TE image self.TeHdr = tehdr - elif tehdr.Signature == 'MZ': # PE image + elif tehdr.Signature == b'MZ': # PE image self.TeHdr = None self.DosHdr = EFI_IMAGE_DOS_HEADER.from_buffer (data, 0) self.PeHdr = EFI_IMAGE_NT_HEADERS32.from_buffer (data, self.DosHdr.e_lfanew) @@ -604,7 +634,7 @@ class PeTeImage: offset += sizeof(blkhdr) # Read relocation type,offset pairs rlen = blkhdr.BlockSize - sizeof(PE_RELOC_BLOCK_HEADER) - rnum = rlen/sizeof(c_uint16) + rnum = int (rlen/sizeof(c_uint16)) rdata = (c_uint16 * rnum).from_buffer(self.Data, offset) for each in rdata: roff = each & 0xfff @@ -666,8 +696,11 @@ def ShowFspInfo (fspfile): if not name: name = '\xff' * 16 else: - name = str(bytearray(name)) - guid = uuid.UUID(bytes = name) + if sys.version_info[0] < 3: + name = str(bytearray(name)) + else: + name = bytes(name) + guid = uuid.UUID(bytes_le = name) print ("FV%d:" % idx) print (" GUID : %s" % str(guid).upper()) print (" Offset : 0x%08X" % fv.Offset) @@ -695,7 +728,10 @@ def GenFspHdr (fspfile, outdir, hfile): for fsp in fd.FspList: fih = fsp.Fih if firstfv: - hfsp.write("#define FSP_IMAGE_ID 0x%016X /* '%s' */\n" % (Bytes2Val(bytearray(fih.ImageId)), fih.ImageId)) + if sys.version_info[0] < 3: + hfsp.write("#define FSP_IMAGE_ID 0x%016X /* '%s' */\n" % (Bytes2Val(bytearray(fih.ImageId)), fih.ImageId)) + else: + hfsp.write("#define FSP_IMAGE_ID 0x%016X /* '%s' */\n" % (Bytes2Val(bytearray(fih.ImageId)), str (fih.ImageId, 'utf-8'))) hfsp.write("#define FSP_IMAGE_REV 0x%08X \n\n" % fih.ImageRevision) firstfv = False fv = fd.FvList[fsp.FvIdxList[0]] @@ -733,7 +769,7 @@ def RebaseFspBin (FspBinary, FspComponent, FspBase, OutputDir, OutputFile): numcomp = len(FspComponent) baselist = FspBase if numcomp != len(baselist): - print "ERROR: Required number of base does not match number of FSP component !" + print ("ERROR: Required number of base does not match number of FSP component !") return newfspbin = fd.FdData[:] @@ -753,7 +789,7 @@ def RebaseFspBin (FspBinary, FspComponent, FspBase, OutputDir, OutputFile): break if not found: - print "ERROR: Could not find FSP_%c component to rebase !" % fspcomp.upper() + print ("ERROR: Could not find FSP_%c component to rebase !" % fspcomp.upper()) return fspbase = baselist[idx] @@ -763,7 +799,7 @@ def RebaseFspBin (FspBinary, FspComponent, FspBase, OutputDir, OutputFile): newbase = int(fspbase) oldbase = fsp.Fih.ImageBase delta = newbase - oldbase - print "Rebase FSP-%c from 0x%08X to 0x%08X:" % (ftype.upper(),oldbase,newbase) + print ("Rebase FSP-%c from 0x%08X to 0x%08X:" % (ftype.upper(),oldbase,newbase)) imglist = [] for fvidx in fsp.FvIdxList: @@ -782,12 +818,12 @@ def RebaseFspBin (FspBinary, FspComponent, FspBase, OutputDir, OutputFile): pcount += img.Rebase(delta, newfspbin) fcount += 1 - print " Patched %d entries in %d TE/PE32 images." % (pcount, fcount) + print (" Patched %d entries in %d TE/PE32 images." % (pcount, fcount)) (count, applied) = fsp.Patch(delta, newfspbin) - print " Patched %d entries using FSP patch table." % applied + print (" Patched %d entries using FSP patch table." % applied) if count != applied: - print " %d invalid entries are ignored !" % (count - applied) + print (" %d invalid entries are ignored !" % (count - applied)) if OutputFile == '': filename = os.path.basename(FspBinary) -- cgit v1.2.3