X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=BaseTools%2FSource%2FPython%2FGenFds%2FRegion.py;h=3b7e30ec85925b1478adf978e88131d92c9aa833;hb=86379ac48ba17c71d4623c57099b064b15118e21;hp=01e998e54c6fb6c5fa2ec1aa5c4d0f8e47622c54;hpb=47fea6afd74af76c7e2a2b03d319b7ac035ac26a;p=mirror_edk2.git diff --git a/BaseTools/Source/Python/GenFds/Region.py b/BaseTools/Source/Python/GenFds/Region.py index 01e998e54c..3b7e30ec85 100644 --- a/BaseTools/Source/Python/GenFds/Region.py +++ b/BaseTools/Source/Python/GenFds/Region.py @@ -1,7 +1,7 @@ ## @file # process FD Region generation # -# Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.
+# Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.
# # This program and the accompanying materials # are licensed and made available under the terms and conditions of the BSD License @@ -17,7 +17,8 @@ # from struct import * from GenFdsGlobalVariable import GenFdsGlobalVariable -import StringIO +from io import BytesIO +import string from CommonDataClass.FdfClass import RegionClassObject import Common.LongFilePathOs as os from stat import * @@ -25,6 +26,7 @@ from Common import EdkLogger from Common.BuildToolError import * from Common.LongFilePathSupport import OpenLongFilePath as open from Common.MultipleWorkspace import MultipleWorkspace as mws +from Common.DataType import BINARY_FILE_TYPE_FV ## generate Region # @@ -39,6 +41,25 @@ class Region(RegionClassObject): RegionClassObject.__init__(self) + ## PadBuffer() + # + # Add padding bytes to the Buffer + # + # @param Buffer The buffer the generated region data will be put + # in + # @param ErasePolarity Flash erase polarity + # @param Size Number of padding bytes requested + # + + def PadBuffer(self, Buffer, ErasePolarity, Size): + if Size > 0: + if (ErasePolarity == '1') : + PadByte = pack('B', 0xFF) + else: + PadByte = pack('B', 0) + PadData = ''.join(PadByte for i in xrange(0, Size)) + Buffer.write(PadData) + ## AddToBuffer() # # Add region data to the Buffer @@ -54,13 +75,16 @@ class Region(RegionClassObject): # @retval string Generated FV file path # - def AddToBuffer(self, Buffer, BaseAddress, BlockSizeList, ErasePolarity, ImageBinDict, vtfDict=None, MacroDict={}): + def AddToBuffer(self, Buffer, BaseAddress, BlockSizeList, ErasePolarity, ImageBinDict, vtfDict=None, MacroDict={}, Flag=False): Size = self.Size - GenFdsGlobalVariable.InfLogger('\nGenerate Region at Offset 0x%X' % self.Offset) - GenFdsGlobalVariable.InfLogger(" Region Size = 0x%X" % Size) + if not Flag: + GenFdsGlobalVariable.InfLogger('\nGenerate Region at Offset 0x%X' % self.Offset) + GenFdsGlobalVariable.InfLogger(" Region Size = 0x%X" % Size) GenFdsGlobalVariable.SharpCounter = 0 + if Flag and (self.RegionType != BINARY_FILE_TYPE_FV): + return - if self.RegionType == 'FV': + if self.RegionType == BINARY_FILE_TYPE_FV: # # Get Fv from FvDict # @@ -71,26 +95,29 @@ class Region(RegionClassObject): FileName = None if RegionData.endswith(".fv"): RegionData = GenFdsGlobalVariable.MacroExtend(RegionData, MacroDict) - GenFdsGlobalVariable.InfLogger(' Region FV File Name = .fv : %s' % RegionData) + if not Flag: + GenFdsGlobalVariable.InfLogger(' Region FV File Name = .fv : %s' % RegionData) if RegionData[1] != ':' : - RegionData = os.path.join (GenFdsGlobalVariable.WorkSpaceDir, RegionData) + RegionData = mws.join (GenFdsGlobalVariable.WorkSpaceDir, RegionData) if not os.path.exists(RegionData): EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=RegionData) FileName = RegionData - elif RegionData.upper() + 'fv' in ImageBinDict.keys(): - GenFdsGlobalVariable.InfLogger(' Region Name = FV') + elif RegionData.upper() + 'fv' in ImageBinDict: + if not Flag: + GenFdsGlobalVariable.InfLogger(' Region Name = FV') FileName = ImageBinDict[RegionData.upper() + 'fv'] else: # # Generate FvImage. # FvObj = None - if RegionData.upper() in GenFdsGlobalVariable.FdfParser.Profile.FvDict.keys(): - FvObj = GenFdsGlobalVariable.FdfParser.Profile.FvDict.get(RegionData.upper()) + if RegionData.upper() in GenFdsGlobalVariable.FdfParser.Profile.FvDict: + FvObj = GenFdsGlobalVariable.FdfParser.Profile.FvDict[RegionData.upper()] - if FvObj != None : - GenFdsGlobalVariable.InfLogger(' Region Name = FV') + if FvObj is not None : + if not Flag: + GenFdsGlobalVariable.InfLogger(' Region Name = FV') # # Call GenFv tool # @@ -100,12 +127,16 @@ class Region(RegionClassObject): if self.FvAddress % FvAlignValue != 0: EdkLogger.error("GenFds", GENFDS_ERROR, "FV (%s) is NOT %s Aligned!" % (FvObj.UiFvName, FvObj.FvAlignment)) - FvBuffer = StringIO.StringIO('') + FvBuffer = BytesIO('') FvBaseAddress = '0x%X' % self.FvAddress BlockSize = None BlockNum = None - FvObj.AddToBuffer(FvBuffer, FvBaseAddress, BlockSize, BlockNum, ErasePolarity, vtfDict) - if FvBuffer.len > Size: + FvObj.AddToBuffer(FvBuffer, FvBaseAddress, BlockSize, BlockNum, ErasePolarity, vtfDict, Flag=Flag) + if Flag: + continue + + FvBufferLen = len(FvBuffer.getvalue()) + if FvBufferLen > Size: FvBuffer.close() EdkLogger.error("GenFds", GENFDS_ERROR, "Size of FV (%s) is larger than Region Size 0x%X specified." % (RegionData, Size)) @@ -114,34 +145,30 @@ class Region(RegionClassObject): # Buffer.write(FvBuffer.getvalue()) FvBuffer.close() - FvOffset = FvOffset + FvBuffer.len - Size = Size - FvBuffer.len + FvOffset = FvOffset + FvBufferLen + Size = Size - FvBufferLen continue else: EdkLogger.error("GenFds", GENFDS_ERROR, "FV (%s) is NOT described in FDF file!" % (RegionData)) # # Add the exist Fv image into FD buffer # - if FileName != None: - FileLength = os.stat(FileName)[ST_SIZE] - if FileLength > Size: - EdkLogger.error("GenFds", GENFDS_ERROR, - "Size of FV File (%s) is larger than Region Size 0x%X specified." \ - % (RegionData, Size)) - BinFile = open(FileName, 'r+b') - Buffer.write(BinFile.read()) - BinFile.close() - Size = Size - FileLength + if not Flag: + if FileName is not None: + FileLength = os.stat(FileName)[ST_SIZE] + if FileLength > Size: + EdkLogger.error("GenFds", GENFDS_ERROR, + "Size of FV File (%s) is larger than Region Size 0x%X specified." \ + % (RegionData, Size)) + BinFile = open(FileName, 'rb') + Buffer.write(BinFile.read()) + BinFile.close() + Size = Size - FileLength # # Pad the left buffer # - if Size > 0: - if (ErasePolarity == '1') : - PadData = 0xFF - else : - PadData = 0 - for i in range(0, Size): - Buffer.write(pack('B', PadData)) + if not Flag: + self.PadBuffer(Buffer, ErasePolarity, Size) if self.RegionType == 'CAPSULE': # @@ -152,12 +179,12 @@ class Region(RegionClassObject): RegionData = GenFdsGlobalVariable.MacroExtend(RegionData, MacroDict) GenFdsGlobalVariable.InfLogger(' Region CAPSULE Image Name = .cap : %s' % RegionData) if RegionData[1] != ':' : - RegionData = os.path.join (GenFdsGlobalVariable.WorkSpaceDir, RegionData) + RegionData = mws.join (GenFdsGlobalVariable.WorkSpaceDir, RegionData) if not os.path.exists(RegionData): EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=RegionData) FileName = RegionData - elif RegionData.upper() + 'cap' in ImageBinDict.keys(): + elif RegionData.upper() + 'cap' in ImageBinDict: GenFdsGlobalVariable.InfLogger(' Region Name = CAPSULE') FileName = ImageBinDict[RegionData.upper() + 'cap'] else: @@ -165,10 +192,10 @@ class Region(RegionClassObject): # Generate Capsule image and Put it into FD buffer # CapsuleObj = None - if RegionData.upper() in GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict.keys(): + if RegionData.upper() in GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict: CapsuleObj = GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict[RegionData.upper()] - if CapsuleObj != None : + if CapsuleObj is not None : CapsuleObj.CapsuleName = RegionData.upper() GenFdsGlobalVariable.InfLogger(' Region Name = CAPSULE') # @@ -187,28 +214,29 @@ class Region(RegionClassObject): EdkLogger.error("GenFds", GENFDS_ERROR, "Size 0x%X of Capsule File (%s) is larger than Region Size 0x%X specified." \ % (FileLength, RegionData, Size)) - BinFile = open(FileName, 'r+b') + BinFile = open(FileName, 'rb') Buffer.write(BinFile.read()) BinFile.close() Size = Size - FileLength # # Pad the left buffer # - if Size > 0: - if (ErasePolarity == '1') : - PadData = 0xFF - else : - PadData = 0 - for i in range(0, Size): - Buffer.write(pack('B', PadData)) + self.PadBuffer(Buffer, ErasePolarity, Size) - if self.RegionType == 'FILE': + if self.RegionType in ('FILE', 'INF'): for RegionData in self.RegionDataList: - RegionData = GenFdsGlobalVariable.MacroExtend(RegionData, MacroDict) - if RegionData[1] != ':' : - RegionData = mws.join (GenFdsGlobalVariable.WorkSpaceDir, RegionData) - if not os.path.exists(RegionData): - EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=RegionData) + if self.RegionType == 'INF': + RegionData.__InfParse__(None) + if len(RegionData.BinFileList) != 1: + EdkLogger.error('GenFds', GENFDS_ERROR, 'INF in FD region can only contain one binary: %s' % RegionData) + File = RegionData.BinFileList[0] + RegionData = RegionData.PatchEfiFile(File.Path, File.Type) + else: + RegionData = GenFdsGlobalVariable.MacroExtend(RegionData, MacroDict) + if RegionData[1] != ':' : + RegionData = mws.join (GenFdsGlobalVariable.WorkSpaceDir, RegionData) + if not os.path.exists(RegionData): + EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=RegionData) # # Add the file image into FD buffer # @@ -225,13 +253,7 @@ class Region(RegionClassObject): # # Pad the left buffer # - if Size > 0: - if (ErasePolarity == '1') : - PadData = 0xFF - else : - PadData = 0 - for i in range(0, Size): - Buffer.write(pack('B', PadData)) + self.PadBuffer(Buffer, ErasePolarity, Size) if self.RegionType == 'DATA' : GenFdsGlobalVariable.InfLogger(' Region Name = DATA') @@ -248,22 +270,11 @@ class Region(RegionClassObject): # # Pad the left buffer # - if Size > 0: - if (ErasePolarity == '1') : - PadData = 0xFF - else : - PadData = 0 - for i in range(0, Size): - Buffer.write(pack('B', PadData)) + self.PadBuffer(Buffer, ErasePolarity, Size) - if self.RegionType == None: + if self.RegionType is None: GenFdsGlobalVariable.InfLogger(' Region Name = None') - if (ErasePolarity == '1') : - PadData = 0xFF - else : - PadData = 0 - for i in range(0, Size): - Buffer.write(pack('B', PadData)) + self.PadBuffer(Buffer, ErasePolarity, Size) def GetFvAlignValue(self, Str): AlignValue = 1 @@ -324,7 +335,7 @@ class Region(RegionClassObject): # first check whether FvObj.BlockSizeList items have only "BlockSize" or "NumBlocks", # if so, use ExpectedList for Item in FvObj.BlockSizeList: - if Item[0] == None or Item[1] == None: + if Item[0] is None or Item[1] is None: FvObj.BlockSizeList = ExpectedList break # make sure region size is no smaller than the summed block size in FV