130fdf1140b8d1ce93f3821d986fa165552023440lgao## @file
230fdf1140b8d1ce93f3821d986fa165552023440lgao# process data section generation
330fdf1140b8d1ce93f3821d986fa165552023440lgao#
41be2ed90a20618d71ddf34b8a07d038da0b36854Hess Chen#  Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>
530fdf1140b8d1ce93f3821d986fa165552023440lgao#
640d841f6a8f84e75409178e19e69b95e01bada0flgao#  This program and the accompanying materials
730fdf1140b8d1ce93f3821d986fa165552023440lgao#  are licensed and made available under the terms and conditions of the BSD License
830fdf1140b8d1ce93f3821d986fa165552023440lgao#  which accompanies this distribution.  The full text of the license may be found at
930fdf1140b8d1ce93f3821d986fa165552023440lgao#  http://opensource.org/licenses/bsd-license.php
1030fdf1140b8d1ce93f3821d986fa165552023440lgao#
1130fdf1140b8d1ce93f3821d986fa165552023440lgao#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
1230fdf1140b8d1ce93f3821d986fa165552023440lgao#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
1330fdf1140b8d1ce93f3821d986fa165552023440lgao#
1430fdf1140b8d1ce93f3821d986fa165552023440lgao
1530fdf1140b8d1ce93f3821d986fa165552023440lgao##
1630fdf1140b8d1ce93f3821d986fa165552023440lgao# Import Modules
1730fdf1140b8d1ce93f3821d986fa165552023440lgao#
1830fdf1140b8d1ce93f3821d986fa165552023440lgaoimport Section
1930fdf1140b8d1ce93f3821d986fa165552023440lgaofrom GenFdsGlobalVariable import GenFdsGlobalVariable
2030fdf1140b8d1ce93f3821d986fa165552023440lgaoimport subprocess
2130fdf1140b8d1ce93f3821d986fa165552023440lgaofrom Ffs import Ffs
221be2ed90a20618d71ddf34b8a07d038da0b36854Hess Chenimport Common.LongFilePathOs as os
2330fdf1140b8d1ce93f3821d986fa165552023440lgaofrom CommonDataClass.FdfClass import DataSectionClassObject
249053bc517ea77b9780830bf08b966979eff1cb0fqhuangfrom Common.Misc import PeImageClass
251be2ed90a20618d71ddf34b8a07d038da0b36854Hess Chenfrom Common.LongFilePathSupport import CopyLongFilePath
2630fdf1140b8d1ce93f3821d986fa165552023440lgao
2730fdf1140b8d1ce93f3821d986fa165552023440lgao## generate data section
2830fdf1140b8d1ce93f3821d986fa165552023440lgao#
2930fdf1140b8d1ce93f3821d986fa165552023440lgao#
3030fdf1140b8d1ce93f3821d986fa165552023440lgaoclass DataSection (DataSectionClassObject):
3130fdf1140b8d1ce93f3821d986fa165552023440lgao    ## The constructor
3230fdf1140b8d1ce93f3821d986fa165552023440lgao    #
3330fdf1140b8d1ce93f3821d986fa165552023440lgao    #   @param  self        The object pointer
3430fdf1140b8d1ce93f3821d986fa165552023440lgao    #
3530fdf1140b8d1ce93f3821d986fa165552023440lgao    def __init__(self):
3630fdf1140b8d1ce93f3821d986fa165552023440lgao        DataSectionClassObject.__init__(self)
3730fdf1140b8d1ce93f3821d986fa165552023440lgao
3830fdf1140b8d1ce93f3821d986fa165552023440lgao    ## GenSection() method
3930fdf1140b8d1ce93f3821d986fa165552023440lgao    #
4030fdf1140b8d1ce93f3821d986fa165552023440lgao    #   Generate compressed section
4130fdf1140b8d1ce93f3821d986fa165552023440lgao    #
4230fdf1140b8d1ce93f3821d986fa165552023440lgao    #   @param  self        The object pointer
4330fdf1140b8d1ce93f3821d986fa165552023440lgao    #   @param  OutputPath  Where to place output file
4430fdf1140b8d1ce93f3821d986fa165552023440lgao    #   @param  ModuleName  Which module this section belongs to
4530fdf1140b8d1ce93f3821d986fa165552023440lgao    #   @param  SecNum      Index of section
4630fdf1140b8d1ce93f3821d986fa165552023440lgao    #   @param  KeyStringList  Filter for inputs of section generation
4730fdf1140b8d1ce93f3821d986fa165552023440lgao    #   @param  FfsInf      FfsInfStatement object that contains this section data
4830fdf1140b8d1ce93f3821d986fa165552023440lgao    #   @param  Dict        dictionary contains macro and its value
4930fdf1140b8d1ce93f3821d986fa165552023440lgao    #   @retval tuple       (Generated file name list, section alignment)
5030fdf1140b8d1ce93f3821d986fa165552023440lgao    #
5130fdf1140b8d1ce93f3821d986fa165552023440lgao    def GenSection(self, OutputPath, ModuleName, SecNum, keyStringList, FfsFile = None, Dict = {}):
5230fdf1140b8d1ce93f3821d986fa165552023440lgao        #
5330fdf1140b8d1ce93f3821d986fa165552023440lgao        # Prepare the parameter of GenSection
5430fdf1140b8d1ce93f3821d986fa165552023440lgao        #
5530fdf1140b8d1ce93f3821d986fa165552023440lgao        if FfsFile != None:
5630fdf1140b8d1ce93f3821d986fa165552023440lgao            self.SectFileName = GenFdsGlobalVariable.ReplaceWorkspaceMacro(self.SectFileName)
5730fdf1140b8d1ce93f3821d986fa165552023440lgao            self.SectFileName = GenFdsGlobalVariable.MacroExtend(self.SectFileName, Dict, FfsFile.CurrentArch)
5830fdf1140b8d1ce93f3821d986fa165552023440lgao        else:
5930fdf1140b8d1ce93f3821d986fa165552023440lgao            self.SectFileName = GenFdsGlobalVariable.ReplaceWorkspaceMacro(self.SectFileName)
6030fdf1140b8d1ce93f3821d986fa165552023440lgao            self.SectFileName = GenFdsGlobalVariable.MacroExtend(self.SectFileName, Dict)
6130fdf1140b8d1ce93f3821d986fa165552023440lgao
6230fdf1140b8d1ce93f3821d986fa165552023440lgao        """Check Section file exist or not !"""
6330fdf1140b8d1ce93f3821d986fa165552023440lgao
6430fdf1140b8d1ce93f3821d986fa165552023440lgao        if not os.path.exists(self.SectFileName):
6530fdf1140b8d1ce93f3821d986fa165552023440lgao            self.SectFileName = os.path.join (GenFdsGlobalVariable.WorkSpaceDir,
6630fdf1140b8d1ce93f3821d986fa165552023440lgao                                              self.SectFileName)
6730fdf1140b8d1ce93f3821d986fa165552023440lgao
6830fdf1140b8d1ce93f3821d986fa165552023440lgao        """Copy Map file to Ffs output"""
6930fdf1140b8d1ce93f3821d986fa165552023440lgao        Filename = GenFdsGlobalVariable.MacroExtend(self.SectFileName)
7030fdf1140b8d1ce93f3821d986fa165552023440lgao        if Filename[(len(Filename)-4):] == '.efi':
7130fdf1140b8d1ce93f3821d986fa165552023440lgao            MapFile = Filename.replace('.efi', '.map')
7230fdf1140b8d1ce93f3821d986fa165552023440lgao            if os.path.exists(MapFile):
7330fdf1140b8d1ce93f3821d986fa165552023440lgao                CopyMapFile = os.path.join(OutputPath, ModuleName + '.map')
741be2ed90a20618d71ddf34b8a07d038da0b36854Hess Chen                if not os.path.exists(CopyMapFile) or (os.path.getmtime(MapFile) > os.path.getmtime(CopyMapFile)):
751be2ed90a20618d71ddf34b8a07d038da0b36854Hess Chen                    CopyLongFilePath(MapFile, CopyMapFile)
7630fdf1140b8d1ce93f3821d986fa165552023440lgao
779053bc517ea77b9780830bf08b966979eff1cb0fqhuang        #Get PE Section alignment when align is set to AUTO
789053bc517ea77b9780830bf08b966979eff1cb0fqhuang        if self.Alignment == 'Auto' and self.SecType in ('TE', 'PE32'):
799053bc517ea77b9780830bf08b966979eff1cb0fqhuang            ImageObj = PeImageClass (Filename)
809053bc517ea77b9780830bf08b966979eff1cb0fqhuang            if ImageObj.SectionAlignment < 0x400:
819053bc517ea77b9780830bf08b966979eff1cb0fqhuang                self.Alignment = str (ImageObj.SectionAlignment)
829053bc517ea77b9780830bf08b966979eff1cb0fqhuang            else:
839053bc517ea77b9780830bf08b966979eff1cb0fqhuang                self.Alignment = str (ImageObj.SectionAlignment / 0x400) + 'K'
849053bc517ea77b9780830bf08b966979eff1cb0fqhuang
8530fdf1140b8d1ce93f3821d986fa165552023440lgao        NoStrip = True
8630fdf1140b8d1ce93f3821d986fa165552023440lgao        if self.SecType in ('TE', 'PE32'):
8730fdf1140b8d1ce93f3821d986fa165552023440lgao            if self.KeepReloc != None:
8830fdf1140b8d1ce93f3821d986fa165552023440lgao                NoStrip = self.KeepReloc
8930fdf1140b8d1ce93f3821d986fa165552023440lgao
9030fdf1140b8d1ce93f3821d986fa165552023440lgao        if not NoStrip:
9130fdf1140b8d1ce93f3821d986fa165552023440lgao            FileBeforeStrip = os.path.join(OutputPath, ModuleName + '.efi')
9230fdf1140b8d1ce93f3821d986fa165552023440lgao            if not os.path.exists(FileBeforeStrip) or \
9330fdf1140b8d1ce93f3821d986fa165552023440lgao                (os.path.getmtime(self.SectFileName) > os.path.getmtime(FileBeforeStrip)):
941be2ed90a20618d71ddf34b8a07d038da0b36854Hess Chen                CopyLongFilePath(self.SectFileName, FileBeforeStrip)
9530fdf1140b8d1ce93f3821d986fa165552023440lgao            StrippedFile = os.path.join(OutputPath, ModuleName + '.stripped')
9630fdf1140b8d1ce93f3821d986fa165552023440lgao            GenFdsGlobalVariable.GenerateFirmwareImage(
9730fdf1140b8d1ce93f3821d986fa165552023440lgao                                    StrippedFile,
9830fdf1140b8d1ce93f3821d986fa165552023440lgao                                    [GenFdsGlobalVariable.MacroExtend(self.SectFileName, Dict)],
9930fdf1140b8d1ce93f3821d986fa165552023440lgao                                    Strip=True
10030fdf1140b8d1ce93f3821d986fa165552023440lgao                                    )
10130fdf1140b8d1ce93f3821d986fa165552023440lgao            self.SectFileName = StrippedFile
10230fdf1140b8d1ce93f3821d986fa165552023440lgao
10330fdf1140b8d1ce93f3821d986fa165552023440lgao        if self.SecType == 'TE':
10430fdf1140b8d1ce93f3821d986fa165552023440lgao            TeFile = os.path.join( OutputPath, ModuleName + 'Te.raw')
10530fdf1140b8d1ce93f3821d986fa165552023440lgao            GenFdsGlobalVariable.GenerateFirmwareImage(
10630fdf1140b8d1ce93f3821d986fa165552023440lgao                                    TeFile,
10730fdf1140b8d1ce93f3821d986fa165552023440lgao                                    [GenFdsGlobalVariable.MacroExtend(self.SectFileName, Dict)],
10830fdf1140b8d1ce93f3821d986fa165552023440lgao                                    Type='te'
10930fdf1140b8d1ce93f3821d986fa165552023440lgao                                    )
11030fdf1140b8d1ce93f3821d986fa165552023440lgao            self.SectFileName = TeFile
11130fdf1140b8d1ce93f3821d986fa165552023440lgao
11230fdf1140b8d1ce93f3821d986fa165552023440lgao        OutputFile = os.path.join (OutputPath, ModuleName + 'SEC' + SecNum + Ffs.SectionSuffix.get(self.SecType))
11330fdf1140b8d1ce93f3821d986fa165552023440lgao        OutputFile = os.path.normpath(OutputFile)
11430fdf1140b8d1ce93f3821d986fa165552023440lgao
11530fdf1140b8d1ce93f3821d986fa165552023440lgao        GenFdsGlobalVariable.GenerateSection(OutputFile, [self.SectFileName], Section.Section.SectionType.get(self.SecType))
11630fdf1140b8d1ce93f3821d986fa165552023440lgao        FileList = [OutputFile]
11730fdf1140b8d1ce93f3821d986fa165552023440lgao        return FileList, self.Alignment
118