2009-07-17 09:10:31 +00:00
## @file
# process FD generation
#
2018-04-17 22:40:15 +08:00
# Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
2009-07-17 09:10:31 +00:00
#
2019-04-03 16:03:11 -07:00
# SPDX-License-Identifier: BSD-2-Clause-Patent
2009-07-17 09:10:31 +00:00
#
##
# Import Modules
#
2018-10-15 08:27:53 +08:00
from __future__ import absolute_import
2018-07-13 18:18:33 +08:00
from . import Region
from . import Fv
2014-08-15 03:06:48 +00:00
import Common.LongFilePathOs as os
2018-06-25 18:31:36 +08:00
from io import BytesIO
2009-07-17 09:10:31 +00:00
import sys
from struct import *
2018-07-13 18:18:33 +08:00
from .GenFdsGlobalVariable import GenFdsGlobalVariable
2009-07-17 09:10:31 +00:00
from CommonDataClass.FdfClass import FDClassObject
from Common import EdkLogger
from Common.BuildToolError import *
from Common.Misc import SaveFileOnChange
2018-04-27 00:57:56 +08:00
from Common.DataType import BINARY_FILE_TYPE_FV
2009-07-17 09:10:31 +00:00
## generate FD
#
#
class FD ( FDClassObject ):
## The constructor
#
# @param self The object pointer
#
def __init__ ( self ):
FDClassObject . __init__ ( self )
## GenFd() method
#
# Generate FD
#
# @retval string Generated FD file name
#
2017-11-22 15:42:25 +08:00
def GenFd ( self , Flag = False ):
2018-07-13 18:18:36 +08:00
if self . FdUiName . upper () + 'fd' in GenFdsGlobalVariable . ImageBinDict :
return GenFdsGlobalVariable . ImageBinDict [ self . FdUiName . upper () + 'fd' ]
2009-09-11 03:14:43 +00:00
2009-07-17 09:10:31 +00:00
#
# Print Information
#
2017-07-04 11:27:35 +08:00
FdFileName = os . path . join ( GenFdsGlobalVariable . FvDir , self . FdUiName + '.fd' )
2017-11-22 15:42:25 +08:00
if not Flag :
GenFdsGlobalVariable . InfLogger ( " \n Fd File Name: %s ( %s )" % ( self . FdUiName , FdFileName ))
2017-07-04 11:27:35 +08:00
2009-07-17 09:10:31 +00:00
Offset = 0x00
for item in self . BlockSizeList :
Offset = Offset + item [ 0 ] * item [ 1 ]
if Offset != self . Size :
EdkLogger . error ( "GenFds" , GENFDS_ERROR , 'FD %s Size not consistent with block array' % self . FdUiName )
GenFdsGlobalVariable . VerboseLogger ( 'Following Fv will be add to Fd !!!' )
for FvObj in GenFdsGlobalVariable . FdfParser . Profile . FvDict :
GenFdsGlobalVariable . VerboseLogger ( FvObj )
2017-09-05 15:42:23 +08:00
HasCapsuleRegion = False
for RegionObj in self . RegionList :
2009-11-09 11:47:35 +00:00
if RegionObj . RegionType == 'CAPSULE' :
2017-09-05 15:42:23 +08:00
HasCapsuleRegion = True
break
if HasCapsuleRegion :
2019-01-23 10:16:00 +08:00
TempFdBuffer = BytesIO ()
2017-09-05 15:42:23 +08:00
PreviousRegionStart = - 1
PreviousRegionSize = 1
for RegionObj in self . RegionList :
if RegionObj . RegionType == 'CAPSULE' :
continue
if RegionObj . Offset + RegionObj . Size <= PreviousRegionStart :
pass
elif RegionObj . Offset <= PreviousRegionStart or ( RegionObj . Offset >= PreviousRegionStart and RegionObj . Offset < PreviousRegionStart + PreviousRegionSize ):
pass
elif RegionObj . Offset > PreviousRegionStart + PreviousRegionSize :
2017-11-22 15:42:25 +08:00
if not Flag :
GenFdsGlobalVariable . InfLogger ( 'Padding region starting from offset 0x %X , with size 0x %X ' % ( PreviousRegionStart + PreviousRegionSize , RegionObj . Offset - ( PreviousRegionStart + PreviousRegionSize )))
2017-09-05 15:42:23 +08:00
PadRegion = Region . Region ()
PadRegion . Offset = PreviousRegionStart + PreviousRegionSize
PadRegion . Size = RegionObj . Offset - PadRegion . Offset
2017-11-22 15:42:25 +08:00
if not Flag :
2019-01-09 14:44:36 +08:00
PadRegion . AddToBuffer ( TempFdBuffer , self . BaseAddress , self . BlockSizeList , self . ErasePolarity , GenFdsGlobalVariable . ImageBinDict , self . DefineVarDict )
2017-09-05 15:42:23 +08:00
PreviousRegionStart = RegionObj . Offset
PreviousRegionSize = RegionObj . Size
#
# Call each region's AddToBuffer function
#
if PreviousRegionSize > self . Size :
pass
GenFdsGlobalVariable . VerboseLogger ( 'Call each region \' s AddToBuffer function' )
2019-01-09 14:44:36 +08:00
RegionObj . AddToBuffer ( TempFdBuffer , self . BaseAddress , self . BlockSizeList , self . ErasePolarity , GenFdsGlobalVariable . ImageBinDict , self . DefineVarDict )
2018-07-05 17:40:04 +08:00
2019-01-23 10:16:00 +08:00
FdBuffer = BytesIO ()
2009-07-17 09:10:31 +00:00
PreviousRegionStart = - 1
PreviousRegionSize = 1
for RegionObj in self . RegionList :
if RegionObj . Offset + RegionObj . Size <= PreviousRegionStart :
EdkLogger . error ( "GenFds" , GENFDS_ERROR ,
'Region offset 0x %X in wrong order with Region starting from 0x %X , size 0x %X \n Regions in FDF must have offsets appear in ascending order.' \
% ( RegionObj . Offset , PreviousRegionStart , PreviousRegionSize ))
elif RegionObj . Offset <= PreviousRegionStart or ( RegionObj . Offset >= PreviousRegionStart and RegionObj . Offset < PreviousRegionStart + PreviousRegionSize ):
EdkLogger . error ( "GenFds" , GENFDS_ERROR ,
'Region offset 0x %X overlaps with Region starting from 0x %X , size 0x %X ' \
% ( RegionObj . Offset , PreviousRegionStart , PreviousRegionSize ))
elif RegionObj . Offset > PreviousRegionStart + PreviousRegionSize :
2017-11-22 15:42:25 +08:00
if not Flag :
GenFdsGlobalVariable . InfLogger ( 'Padding region starting from offset 0x %X , with size 0x %X ' % ( PreviousRegionStart + PreviousRegionSize , RegionObj . Offset - ( PreviousRegionStart + PreviousRegionSize )))
2009-07-17 09:10:31 +00:00
PadRegion = Region . Region ()
PadRegion . Offset = PreviousRegionStart + PreviousRegionSize
PadRegion . Size = RegionObj . Offset - PadRegion . Offset
2017-11-22 15:42:25 +08:00
if not Flag :
2019-01-09 14:44:36 +08:00
PadRegion . AddToBuffer ( FdBuffer , self . BaseAddress , self . BlockSizeList , self . ErasePolarity , GenFdsGlobalVariable . ImageBinDict , self . DefineVarDict )
2009-07-17 09:10:31 +00:00
PreviousRegionStart = RegionObj . Offset
PreviousRegionSize = RegionObj . Size
#
2013-08-23 02:18:16 +00:00
# Verify current region fits within allocated FD section Size
#
if PreviousRegionStart + PreviousRegionSize > self . Size :
EdkLogger . error ( "GenFds" , GENFDS_ERROR ,
'FD %s size too small to fit region with offset 0x %X and size 0x %X '
% ( self . FdUiName , PreviousRegionStart , PreviousRegionSize ))
#
2009-07-17 09:10:31 +00:00
# Call each region's AddToBuffer function
#
GenFdsGlobalVariable . VerboseLogger ( 'Call each region \' s AddToBuffer function' )
2019-01-09 14:44:36 +08:00
RegionObj . AddToBuffer ( FdBuffer , self . BaseAddress , self . BlockSizeList , self . ErasePolarity , GenFdsGlobalVariable . ImageBinDict , self . DefineVarDict , Flag = Flag )
2009-07-17 09:10:31 +00:00
#
# Write the buffer contents to Fd file
#
GenFdsGlobalVariable . VerboseLogger ( 'Write the buffer contents to Fd file' )
2017-11-22 15:42:25 +08:00
if not Flag :
SaveFileOnChange ( FdFileName , FdBuffer . getvalue ())
FdBuffer . close ()
2018-07-13 18:18:36 +08:00
GenFdsGlobalVariable . ImageBinDict [ self . FdUiName . upper () + 'fd' ] = FdFileName
2009-07-17 09:10:31 +00:00
return FdFileName