X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=BaseTools%2FSource%2FPython%2FGenFds%2FRegion.py;h=6015e24e25a52b3262b0b407b559d30e775f3aa3;hb=df81077f77f91b43f32f70e06950b86e20f711da;hp=ed16c6fa98da99eb3d2cb17dd744fd017f78fbb2;hpb=30fdf1140b8d1ce93f3821d986fa165552023440;p=mirror_edk2.git diff --git a/BaseTools/Source/Python/GenFds/Region.py b/BaseTools/Source/Python/GenFds/Region.py index ed16c6fa98..6015e24e25 100644 --- a/BaseTools/Source/Python/GenFds/Region.py +++ b/BaseTools/Source/Python/GenFds/Region.py @@ -1,9 +1,9 @@ ## @file # process FD Region generation # -# Copyright (c) 2007, Intel Corporation +# Copyright (c) 2007 - 2015, Intel Corporation. All rights reserved.
# -# All rights reserved. This program and the accompanying materials +# This program and the accompanying materials # are licensed and made available under the terms and conditions of the BSD License # which accompanies this distribution. The full text of the license may be found at # http://opensource.org/licenses/bsd-license.php @@ -19,10 +19,12 @@ from struct import * from GenFdsGlobalVariable import GenFdsGlobalVariable import StringIO from CommonDataClass.FdfClass import RegionClassObject -import os +import Common.LongFilePathOs as os +from stat import * from Common import EdkLogger from Common.BuildToolError import * - +from Common.LongFilePathSupport import OpenLongFilePath as open +from Common.MultipleWorkspace import MultipleWorkspace as mws ## generate Region # @@ -52,110 +54,191 @@ class Region(RegionClassObject): # @retval string Generated FV file path # - def AddToBuffer(self, Buffer, BaseAddress, BlockSizeList, ErasePolarity, FvBinDict, vtfDict = None, MacroDict = {}): + def AddToBuffer(self, Buffer, BaseAddress, BlockSizeList, ErasePolarity, ImageBinDict, vtfDict=None, MacroDict={}): Size = self.Size - GenFdsGlobalVariable.InfLogger('Generate Region at Offset 0x%X' % self.Offset) - GenFdsGlobalVariable.InfLogger(" Region Size = 0x%X" %Size) + GenFdsGlobalVariable.InfLogger('\nGenerate Region at Offset 0x%X' % self.Offset) + GenFdsGlobalVariable.InfLogger(" Region Size = 0x%X" % Size) GenFdsGlobalVariable.SharpCounter = 0 if self.RegionType == 'FV': # # Get Fv from FvDict # - FvBuffer = StringIO.StringIO('') - RegionBlockSize = self.BlockSizeOfRegion(BlockSizeList) - RegionBlockNum = self.BlockNumOfRegion(RegionBlockSize) - self.FvAddress = int(BaseAddress, 16) + self.Offset - FvBaseAddress = '0x%X' %self.FvAddress - + FvBaseAddress = '0x%X' % self.FvAddress + FvOffset = 0 for RegionData in self.RegionDataList: - + FileName = None if RegionData.endswith(".fv"): RegionData = GenFdsGlobalVariable.MacroExtend(RegionData, MacroDict) - GenFdsGlobalVariable.InfLogger(' Region FV File Name = .fv : %s'%RegionData) + 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) - BinFile = open (RegionData, 'r+b') - FvBuffer.write(BinFile.read()) - if FvBuffer.len > Size: + FileName = RegionData + elif RegionData.upper() + 'fv' in ImageBinDict.keys(): + 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 FvObj != None : + GenFdsGlobalVariable.InfLogger(' Region Name = FV') + # + # Call GenFv tool + # + self.BlockInfoOfRegion(BlockSizeList, FvObj) + self.FvAddress = self.FvAddress + FvOffset + FvAlignValue = self.GetFvAlignValue(FvObj.FvAlignment) + if self.FvAddress % FvAlignValue != 0: + EdkLogger.error("GenFds", GENFDS_ERROR, + "FV (%s) is NOT %s Aligned!" % (FvObj.UiFvName, FvObj.FvAlignment)) + FvBuffer = StringIO.StringIO('') + FvBaseAddress = '0x%X' % self.FvAddress + BlockSize = None + BlockNum = None + FvObj.AddToBuffer(FvBuffer, FvBaseAddress, BlockSize, BlockNum, ErasePolarity, vtfDict) + if FvBuffer.len > Size: + FvBuffer.close() + EdkLogger.error("GenFds", GENFDS_ERROR, + "Size of FV (%s) is larger than Region Size 0x%X specified." % (RegionData, Size)) + # + # Put the generated image into FD buffer. + # + Buffer.write(FvBuffer.getvalue()) + FvBuffer.close() + FvOffset = FvOffset + FvBuffer.len + Size = Size - FvBuffer.len + 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)) - break - - if RegionData.upper() in FvBinDict.keys(): - continue + BinFile = open(FileName, 'r+b') + 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)) - FvObj = None - if RegionData.upper() in GenFdsGlobalVariable.FdfParser.Profile.FvDict.keys(): - FvObj = GenFdsGlobalVariable.FdfParser.Profile.FvDict.get(RegionData.upper()) + if self.RegionType == 'CAPSULE': + # + # Get Capsule from Capsule Dict + # + for RegionData in self.RegionDataList: + if RegionData.endswith(".cap"): + RegionData = GenFdsGlobalVariable.MacroExtend(RegionData, MacroDict) + GenFdsGlobalVariable.InfLogger(' Region CAPSULE Image Name = .cap : %s' % RegionData) + if RegionData[1] != ':' : + RegionData = mws.join (GenFdsGlobalVariable.WorkSpaceDir, RegionData) + if not os.path.exists(RegionData): + EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=RegionData) - if FvObj != None : - GenFdsGlobalVariable.InfLogger(' Region Name = FV') + FileName = RegionData + elif RegionData.upper() + 'cap' in ImageBinDict.keys(): + GenFdsGlobalVariable.InfLogger(' Region Name = CAPSULE') + FileName = ImageBinDict[RegionData.upper() + 'cap'] + else: # - # Call GenFv tool + # Generate Capsule image and Put it into FD buffer # - BlockSize = RegionBlockSize - BlockNum = RegionBlockNum - if FvObj.BlockSizeList != []: - if FvObj.BlockSizeList[0][0] != None: - BlockSize = FvObj.BlockSizeList[0][0] - if FvObj.BlockSizeList[0][1] != None: - BlockNum = FvObj.BlockSizeList[0][1] - self.FvAddress = self.FvAddress + FvBuffer.len - FvAlignValue = self.GetFvAlignValue(FvObj.FvAlignment) - if self.FvAddress % FvAlignValue != 0: - EdkLogger.error("GenFds", GENFDS_ERROR, - "FV (%s) is NOT %s Aligned!" % (FvObj.UiFvName, FvObj.FvAlignment)) - FvBaseAddress = '0x%X' %self.FvAddress - FileName = FvObj.AddToBuffer(FvBuffer, FvBaseAddress, BlockSize, BlockNum, ErasePolarity, vtfDict) - - if FvBuffer.len > Size: - EdkLogger.error("GenFds", GENFDS_ERROR, - "Size of FV (%s) is larger than Region Size 0x%X specified." % (RegionData, Size)) - else: - EdkLogger.error("GenFds", GENFDS_ERROR, "FV (%s) is NOT described in FDF file!" % (RegionData)) + CapsuleObj = None + if RegionData.upper() in GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict.keys(): + CapsuleObj = GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict[RegionData.upper()] + if CapsuleObj != None : + CapsuleObj.CapsuleName = RegionData.upper() + GenFdsGlobalVariable.InfLogger(' Region Name = CAPSULE') + # + # Call GenFv tool to generate Capsule Image + # + FileName = CapsuleObj.GenCapsule() + CapsuleObj.CapsuleName = None + else: + EdkLogger.error("GenFds", GENFDS_ERROR, "Capsule (%s) is NOT described in FDF file!" % (RegionData)) - if FvBuffer.len > 0: - Buffer.write(FvBuffer.getvalue()) - else: - BinFile = open (FileName, 'rb') + # + # Add the capsule image into FD buffer + # + FileLength = os.stat(FileName)[ST_SIZE] + if FileLength > Size: + 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') 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)) - FvBuffer.close() - - if self.RegionType == 'FILE': - FvBuffer = StringIO.StringIO('') + if self.RegionType in ('FILE', 'INF'): for RegionData in self.RegionDataList: - RegionData = GenFdsGlobalVariable.MacroExtend(RegionData, MacroDict) - GenFdsGlobalVariable.InfLogger(' Region File Name = FILE: %s'%RegionData) - if RegionData[1] != ':' : - RegionData = os.path.join (GenFdsGlobalVariable.WorkSpaceDir, RegionData) - if not os.path.exists(RegionData): - EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=RegionData) - - BinFile = open (RegionData, 'r+b') - FvBuffer.write(BinFile.read()) - if FvBuffer.len > Size : + 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 + # + FileLength = os.stat(RegionData)[ST_SIZE] + if FileLength > Size: EdkLogger.error("GenFds", GENFDS_ERROR, - "Size of File (%s) large than Region Size " % RegionData) - + "Size of File (%s) is larger than Region Size 0x%X specified." \ + % (RegionData, Size)) + GenFdsGlobalVariable.InfLogger(' Region File Name = %s' % RegionData) + BinFile = open(RegionData, 'rb') + Buffer.write(BinFile.read()) + BinFile.close() + Size = Size - FileLength # - # If File contents less than region size, append "0xff" after it + # Pad the left buffer # - if FvBuffer.len < Size: - for index in range(0, (Size-FvBuffer.len)): - if (ErasePolarity == '1'): - FvBuffer.write(pack('B', int('0xFF', 16))) - else: - FvBuffer.write(pack('B', int('0x00', 16))) - Buffer.write(FvBuffer.getvalue()) - FvBuffer.close() + if Size > 0: + if (ErasePolarity == '1') : + PadData = 0xFF + else : + PadData = 0 + for i in range(0, Size): + Buffer.write(pack('B', PadData)) if self.RegionType == 'DATA' : GenFdsGlobalVariable.InfLogger(' Region Name = DATA') @@ -168,12 +251,16 @@ class Region(RegionClassObject): else: for item in Data : Buffer.write(pack('B', int(item, 16))) - if DataSize < Size: - if (ErasePolarity == '1'): + Size = Size - DataSize + # + # Pad the left buffer + # + if Size > 0: + if (ErasePolarity == '1') : PadData = 0xFF - else: + else : PadData = 0 - for i in range(Size - DataSize): + for i in range(0, Size): Buffer.write(pack('B', PadData)) if self.RegionType == None: @@ -193,48 +280,84 @@ class Region(RegionClassObject): Granu = 1024 Str = Str[:-1] elif Str.endswith('M'): - Granu = 1024*1024 + Granu = 1024 * 1024 Str = Str[:-1] elif Str.endswith('G'): - Granu = 1024*1024*1024 + Granu = 1024 * 1024 * 1024 Str = Str[:-1] else: pass - AlignValue = int(Str)*Granu + AlignValue = int(Str) * Granu return AlignValue + ## BlockSizeOfRegion() # # @param BlockSizeList List of block information - # @retval int Block size of region - # - def BlockSizeOfRegion(self, BlockSizeList): - Offset = 0x00 - BlockSize = 0 - for item in BlockSizeList: - Offset = Offset + item[0] * item[1] - GenFdsGlobalVariable.VerboseLogger ("Offset = 0x%X" %Offset) - GenFdsGlobalVariable.VerboseLogger ("self.Offset 0x%X" %self.Offset) - - if self.Offset < Offset : - if Offset - self.Offset < self.Size: - EdkLogger.error("GenFds", GENFDS_ERROR, - "Region at Offset 0x%X can NOT fit into Block array with BlockSize %X" \ - % (self.Offset, item[0])) - BlockSize = item[0] - GenFdsGlobalVariable.VerboseLogger ("BlockSize = %X" %BlockSize) - return BlockSize - return BlockSize - - ## BlockNumOfRegion() - # - # @param BlockSize block size of region - # @retval int Block number of region + # @param FvObj The object for FV # - def BlockNumOfRegion (self, BlockSize): - if BlockSize == 0 : - EdkLogger.error("GenFds", GENFDS_ERROR, "Region: %s is not in the FD address scope!" % self.Offset) - BlockNum = self.Size / BlockSize - GenFdsGlobalVariable.VerboseLogger ("BlockNum = 0x%X" %BlockNum) - return BlockNum + def BlockInfoOfRegion(self, BlockSizeList, FvObj): + Start = 0 + End = 0 + RemindingSize = self.Size + ExpectedList = [] + for (BlockSize, BlockNum, pcd) in BlockSizeList: + End = Start + BlockSize * BlockNum + # region not started yet + if self.Offset >= End: + Start = End + continue + # region located in current blocks + else: + # region ended within current blocks + if self.Offset + self.Size <= End: + ExpectedList.append((BlockSize, (RemindingSize + BlockSize - 1) / BlockSize)) + break + # region not ended yet + else: + # region not started in middle of current blocks + if self.Offset <= Start: + UsedBlockNum = BlockNum + # region started in middle of current blocks + else: + UsedBlockNum = (End - self.Offset) / BlockSize + Start = End + ExpectedList.append((BlockSize, UsedBlockNum)) + RemindingSize -= BlockSize * UsedBlockNum + + if FvObj.BlockSizeList == []: + FvObj.BlockSizeList = ExpectedList + else: + # 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: + FvObj.BlockSizeList = ExpectedList + break + # make sure region size is no smaller than the summed block size in FV + Sum = 0 + for Item in FvObj.BlockSizeList: + Sum += Item[0] * Item[1] + if self.Size < Sum: + EdkLogger.error("GenFds", GENFDS_ERROR, "Total Size of FV %s 0x%x is larger than Region Size 0x%x " + % (FvObj.UiFvName, Sum, self.Size)) + # check whether the BlockStatements in FV section is appropriate + ExpectedListData = '' + for Item in ExpectedList: + ExpectedListData += "BlockSize = 0x%x\n\tNumBlocks = 0x%x\n\t" % Item + Index = 0 + for Item in FvObj.BlockSizeList: + if Item[0] != ExpectedList[Index][0]: + EdkLogger.error("GenFds", GENFDS_ERROR, "BlockStatements of FV %s are not align with FD's, suggested FV BlockStatement" + % FvObj.UiFvName, ExtraData=ExpectedListData) + elif Item[1] != ExpectedList[Index][1]: + if (Item[1] < ExpectedList[Index][1]) and (Index == len(FvObj.BlockSizeList) - 1): + break; + else: + EdkLogger.error("GenFds", GENFDS_ERROR, "BlockStatements of FV %s are not align with FD's, suggested FV BlockStatement" + % FvObj.UiFvName, ExtraData=ExpectedListData) + else: + Index += 1 + +