2009-07-17 11:10:31 +02:00
## @file
# process FD generation
#
2018-04-17 16:40:15 +02:00
# Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
2009-07-17 11:10:31 +02:00
#
2019-04-04 01:03:11 +02:00
# SPDX-License-Identifier: BSD-2-Clause-Patent
2009-07-17 11:10:31 +02:00
#
##
# Import Modules
#
2018-10-15 02:27:53 +02:00
from __future__ import absolute_import
2018-07-13 12:18:33 +02:00
from . import Region
from . import Fv
2014-08-15 05:06:48 +02:00
import Common . LongFilePathOs as os
2018-06-25 12:31:36 +02:00
from io import BytesIO
2009-07-17 11:10:31 +02:00
import sys
from struct import *
2018-07-13 12:18:33 +02:00
from . GenFdsGlobalVariable import GenFdsGlobalVariable
2009-07-17 11:10:31 +02:00
from CommonDataClass . FdfClass import FDClassObject
from Common import EdkLogger
from Common . BuildToolError import *
from Common . Misc import SaveFileOnChange
2018-04-26 18:57:56 +02:00
from Common . DataType import BINARY_FILE_TYPE_FV
2009-07-17 11:10:31 +02: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 08:42:25 +01:00
def GenFd ( self , Flag = False ) :
2018-07-13 12:18:36 +02:00
if self . FdUiName . upper ( ) + ' fd ' in GenFdsGlobalVariable . ImageBinDict :
return GenFdsGlobalVariable . ImageBinDict [ self . FdUiName . upper ( ) + ' fd ' ]
2009-09-11 05:14:43 +02:00
2009-07-17 11:10:31 +02:00
#
# Print Information
#
2017-07-04 05:27:35 +02:00
FdFileName = os . path . join ( GenFdsGlobalVariable . FvDir , self . FdUiName + ' .fd ' )
2017-11-22 08:42:25 +01:00
if not Flag :
GenFdsGlobalVariable . InfLogger ( " \n Fd File Name: %s ( %s ) " % ( self . FdUiName , FdFileName ) )
2017-07-04 05:27:35 +02:00
2009-07-17 11:10:31 +02: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 09:42:23 +02:00
HasCapsuleRegion = False
for RegionObj in self . RegionList :
2009-11-09 12:47:35 +01:00
if RegionObj . RegionType == ' CAPSULE ' :
2017-09-05 09:42:23 +02:00
HasCapsuleRegion = True
break
if HasCapsuleRegion :
2019-01-23 03:16:00 +01:00
TempFdBuffer = BytesIO ( )
2017-09-05 09:42:23 +02: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 08:42:25 +01: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 09:42:23 +02:00
PadRegion = Region . Region ( )
PadRegion . Offset = PreviousRegionStart + PreviousRegionSize
PadRegion . Size = RegionObj . Offset - PadRegion . Offset
2017-11-22 08:42:25 +01:00
if not Flag :
2019-01-09 07:44:36 +01:00
PadRegion . AddToBuffer ( TempFdBuffer , self . BaseAddress , self . BlockSizeList , self . ErasePolarity , GenFdsGlobalVariable . ImageBinDict , self . DefineVarDict )
2017-09-05 09:42:23 +02: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 07:44:36 +01:00
RegionObj . AddToBuffer ( TempFdBuffer , self . BaseAddress , self . BlockSizeList , self . ErasePolarity , GenFdsGlobalVariable . ImageBinDict , self . DefineVarDict )
2018-07-05 11:40:04 +02:00
2019-01-23 03:16:00 +01:00
FdBuffer = BytesIO ( )
2009-07-17 11:10:31 +02: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 08:42:25 +01: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 11:10:31 +02:00
PadRegion = Region . Region ( )
PadRegion . Offset = PreviousRegionStart + PreviousRegionSize
PadRegion . Size = RegionObj . Offset - PadRegion . Offset
2017-11-22 08:42:25 +01:00
if not Flag :
2019-01-09 07:44:36 +01:00
PadRegion . AddToBuffer ( FdBuffer , self . BaseAddress , self . BlockSizeList , self . ErasePolarity , GenFdsGlobalVariable . ImageBinDict , self . DefineVarDict )
2009-07-17 11:10:31 +02:00
PreviousRegionStart = RegionObj . Offset
PreviousRegionSize = RegionObj . Size
#
2013-08-23 04:18:16 +02: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 11:10:31 +02:00
# Call each region's AddToBuffer function
#
GenFdsGlobalVariable . VerboseLogger ( ' Call each region \' s AddToBuffer function ' )
2019-01-09 07:44:36 +01:00
RegionObj . AddToBuffer ( FdBuffer , self . BaseAddress , self . BlockSizeList , self . ErasePolarity , GenFdsGlobalVariable . ImageBinDict , self . DefineVarDict , Flag = Flag )
2009-07-17 11:10:31 +02:00
#
# Write the buffer contents to Fd file
#
GenFdsGlobalVariable . VerboseLogger ( ' Write the buffer contents to Fd file ' )
2017-11-22 08:42:25 +01:00
if not Flag :
SaveFileOnChange ( FdFileName , FdBuffer . getvalue ( ) )
FdBuffer . close ( )
2018-07-13 12:18:36 +02:00
GenFdsGlobalVariable . ImageBinDict [ self . FdUiName . upper ( ) + ' fd ' ] = FdFileName
2009-07-17 11:10:31 +02:00
return FdFileName
## generate flash map file
#
# @param self The object pointer
#
def GenFlashMap ( self ) :
pass