EDK2 doxygen online documents - Firmware Encoding Index 1
EDK2 doxygen online documents - Firmware Encoding Index

BaseTools/Source/Python/GenFds/GuidSection.py

Go to the documentation of this file.
00001 ## @file
00002 # process GUIDed section generation
00003 #
00004 #  Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
00005 #
00006 #  This program and the accompanying materials
00007 #  are licensed and made available under the terms and conditions of the BSD License
00008 #  which accompanies this distribution.  The full text of the license may be found at
00009 #  http://opensource.org/licenses/bsd-license.php
00010 #
00011 #  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
00012 #  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
00013 #
00014 
00015 ##
00016 # Import Modules
00017 #
00018 import Section
00019 import subprocess
00020 from Ffs import Ffs
00021 import os
00022 from GenFdsGlobalVariable import GenFdsGlobalVariable
00023 from CommonDataClass.FdfClass import GuidSectionClassObject
00024 from Common import ToolDefClassObject
00025 import sys
00026 from Common import EdkLogger
00027 from Common.BuildToolError import *
00028 from FvImageSection import FvImageSection
00029 
00030 ## generate GUIDed section
00031 #
00032 #
00033 class GuidSection(GuidSectionClassObject) :
00034 
00035     ## The constructor
00036     #
00037     #   @param  self        The object pointer
00038     #
00039     def __init__(self):
00040         GuidSectionClassObject.__init__(self)
00041 
00042     ## GenSection() method
00043     #
00044     #   Generate GUIDed section
00045     #
00046     #   @param  self        The object pointer
00047     #   @param  OutputPath  Where to place output file
00048     #   @param  ModuleName  Which module this section belongs to
00049     #   @param  SecNum      Index of section
00050     #   @param  KeyStringList  Filter for inputs of section generation
00051     #   @param  FfsInf      FfsInfStatement object that contains this section data
00052     #   @param  Dict        dictionary contains macro and its value
00053     #   @retval tuple       (Generated file name, section alignment)
00054     #
00055     def GenSection(self, OutputPath, ModuleName, SecNum, KeyStringList, FfsInf = None, Dict = {}):
00056         #
00057         # Generate all section
00058         #
00059         self.KeyStringList = KeyStringList
00060         self.CurrentArchList = GenFdsGlobalVariable.ArchList
00061         if FfsInf != None:
00062             self.Alignment = FfsInf.__ExtendMacro__(self.Alignment)
00063             self.NameGuid = FfsInf.__ExtendMacro__(self.NameGuid)
00064             self.SectionType = FfsInf.__ExtendMacro__(self.SectionType)
00065             self.CurrentArchList = [FfsInf.CurrentArch]
00066 
00067         SectFile  = tuple()
00068         SectAlign = []
00069         Index = 0
00070         MaxAlign = None
00071         if self.FvAddr != []:
00072             FvAddrIsSet = True
00073         else:
00074             FvAddrIsSet = False
00075         
00076         if self.ProcessRequired in ("TRUE", "1"):
00077             if self.FvAddr != []:
00078                 #no use FvAddr when the image is processed.
00079                 self.FvAddr = []
00080             if self.FvParentAddr != None:
00081                 #no use Parent Addr when the image is processed.
00082                 self.FvParentAddr = None
00083 
00084         for Sect in self.SectionList:
00085             Index = Index + 1
00086             SecIndex = '%s.%d' %(SecNum,Index)
00087             # set base address for inside FvImage
00088             if isinstance(Sect, FvImageSection):
00089                 if self.FvAddr != []:
00090                     Sect.FvAddr = self.FvAddr.pop(0)
00091                 self.IncludeFvSection = True
00092             elif isinstance(Sect, GuidSection):
00093                 Sect.FvAddr = self.FvAddr
00094                 Sect.FvParentAddr = self.FvParentAddr
00095             ReturnSectList, align = Sect.GenSection(OutputPath, ModuleName, SecIndex, KeyStringList,FfsInf, Dict)
00096             if isinstance(Sect, GuidSection):
00097                 if Sect.IncludeFvSection:
00098                     self.IncludeFvSection = Sect.IncludeFvSection
00099 
00100             if align != None:
00101                 if MaxAlign == None:
00102                     MaxAlign = align
00103                 if GenFdsGlobalVariable.GetAlignment (align) > GenFdsGlobalVariable.GetAlignment (MaxAlign):
00104                     MaxAlign = align
00105             if ReturnSectList != []:
00106                 if align == None:
00107                     align = "1"
00108                 for file in ReturnSectList:
00109                     SectFile += (file,)
00110                     SectAlign.append(align)
00111 
00112         if MaxAlign != None:
00113             if self.Alignment == None:
00114                 self.Alignment = MaxAlign
00115             else:
00116                 if GenFdsGlobalVariable.GetAlignment (MaxAlign) > GenFdsGlobalVariable.GetAlignment (self.Alignment):
00117                     self.Alignment = MaxAlign
00118 
00119         OutputFile = OutputPath + \
00120                      os.sep     + \
00121                      ModuleName + \
00122                      'SEC'      + \
00123                      SecNum     + \
00124                      Ffs.SectionSuffix['GUIDED']
00125         OutputFile = os.path.normpath(OutputFile)
00126 
00127         ExternalTool = None
00128         ExternalOption = None
00129         if self.NameGuid != None:
00130             ExternalTool, ExternalOption = self.__FindExtendTool__()
00131 
00132         #
00133         # If not have GUID , call default
00134         # GENCRC32 section
00135         #
00136         if self.NameGuid == None :
00137             GenFdsGlobalVariable.VerboseLogger( "Use GenSection function Generate CRC32 Section")
00138             GenFdsGlobalVariable.GenerateSection(OutputFile, SectFile, Section.Section.SectionType[self.SectionType], InputAlign=SectAlign)
00139             OutputFileList = []
00140             OutputFileList.append(OutputFile)
00141             return OutputFileList, self.Alignment
00142         #or GUID not in External Tool List
00143         elif ExternalTool == None:
00144             EdkLogger.error("GenFds", GENFDS_ERROR, "No tool found with GUID %s" % self.NameGuid)
00145         else:
00146             DummyFile = OutputFile+".dummy"
00147             #
00148             # Call GenSection with DUMMY section type.
00149             #
00150             GenFdsGlobalVariable.GenerateSection(DummyFile, SectFile, InputAlign=SectAlign)
00151             #
00152             # Use external tool process the Output
00153             #
00154             TempFile = OutputPath + \
00155                        os.sep     + \
00156                        ModuleName + \
00157                        'SEC'      + \
00158                        SecNum     + \
00159                        '.tmp'
00160             TempFile = os.path.normpath(TempFile)
00161 
00162             FirstCall = False
00163             CmdOption = '-e'
00164             if ExternalOption != None:
00165                 CmdOption = CmdOption + ' ' + ExternalOption
00166             if self.ProcessRequired not in ("TRUE", "1") and self.IncludeFvSection and not FvAddrIsSet and self.FvParentAddr != None:
00167                 #FirstCall is only set for the encapsulated flash FV image without process required attribute.
00168                 FirstCall = True
00169             #
00170             # Call external tool
00171             #
00172             ReturnValue = [1]
00173             if FirstCall:
00174                 #first try to call the guided tool with -z option and CmdOption for the no process required guided tool.
00175                 GenFdsGlobalVariable.GuidTool(TempFile, [DummyFile], ExternalTool, '-z' + ' ' + CmdOption, ReturnValue)
00176 
00177             #
00178             # when no call or first call failed, ReturnValue are not 1.
00179             # Call the guided tool with CmdOption
00180             #
00181             if ReturnValue[0] != 0:
00182                 FirstCall = False
00183                 ReturnValue[0] = 0
00184                 GenFdsGlobalVariable.GuidTool(TempFile, [DummyFile], ExternalTool, CmdOption)
00185 
00186             FileHandleIn = open(DummyFile,'rb')
00187             FileHandleIn.seek(0,2)
00188             InputFileSize = FileHandleIn.tell()
00189             
00190             FileHandleOut = open(TempFile,'rb')
00191             FileHandleOut.seek(0,2)
00192             TempFileSize = FileHandleOut.tell()
00193 
00194             Attribute = []
00195             HeaderLength = None
00196             if self.ProcessRequired == "NONE":
00197                 if TempFileSize > InputFileSize and TempFileSize % 4 == 0:
00198                     FileHandleIn.seek(0)
00199                     BufferIn  = FileHandleIn.read()
00200                     FileHandleOut.seek(0)
00201                     BufferOut = FileHandleOut.read()
00202                     if BufferIn == BufferOut[TempFileSize - InputFileSize:]:
00203                         HeaderLength = str(TempFileSize - InputFileSize)
00204                 #auto sec guided attribute with process required
00205                 if HeaderLength == None:
00206                     Attribute.append('PROCESSING_REQUIRED')
00207 
00208             FileHandleIn.close()
00209             FileHandleOut.close()
00210             
00211             if FirstCall and 'PROCESSING_REQUIRED' in Attribute:
00212                 # Guided data by -z option on first call is the process required data. Call the guided tool with the real option.
00213                 GenFdsGlobalVariable.GuidTool(TempFile, [DummyFile], ExternalTool, CmdOption)
00214             
00215             #
00216             # Call Gensection Add Section Header
00217             #
00218             if self.ProcessRequired in ("TRUE", "1"):
00219                 if 'PROCESSING_REQUIRED' not in Attribute:
00220                     Attribute.append('PROCESSING_REQUIRED')
00221                 HeaderLength = None
00222             if self.AuthStatusValid in ("TRUE", "1"):
00223                 Attribute.append('AUTH_STATUS_VALID')
00224             GenFdsGlobalVariable.GenerateSection(OutputFile, [TempFile], Section.Section.SectionType['GUIDED'],
00225                                                  Guid=self.NameGuid, GuidAttr=Attribute, GuidHdrLen=HeaderLength)
00226             OutputFileList = []
00227             OutputFileList.append(OutputFile)
00228             if 'PROCESSING_REQUIRED' in Attribute:
00229                 # reset guided section alignment to none for the processed required guided data
00230                 self.Alignment = None
00231                 self.IncludeFvSection = False
00232                 self.ProcessRequired = "TRUE"
00233             return OutputFileList, self.Alignment
00234 
00235     ## __FindExtendTool()
00236     #
00237     #    Find location of tools to process section data
00238     #
00239     #   @param  self        The object pointer
00240     #
00241     def __FindExtendTool__(self):
00242         # if user not specify filter, try to deduce it from global data.
00243         if self.KeyStringList == None or self.KeyStringList == []:
00244             Target = GenFdsGlobalVariable.TargetName
00245             ToolChain = GenFdsGlobalVariable.ToolChainTag
00246             ToolDb = ToolDefClassObject.ToolDefDict(GenFdsGlobalVariable.WorkSpaceDir).ToolsDefTxtDatabase
00247             if ToolChain not in ToolDb['TOOL_CHAIN_TAG']:
00248                 EdkLogger.error("GenFds", GENFDS_ERROR, "Can not find external tool because tool tag %s is not defined in tools_def.txt!" % ToolChain)
00249             self.KeyStringList = [Target+'_'+ToolChain+'_'+self.CurrentArchList[0]]
00250             for Arch in self.CurrentArchList:
00251                 if Target+'_'+ToolChain+'_'+Arch not in self.KeyStringList:
00252                     self.KeyStringList.append(Target+'_'+ToolChain+'_'+Arch)
00253                     
00254         ToolDefinition = ToolDefClassObject.ToolDefDict(GenFdsGlobalVariable.WorkSpaceDir).ToolsDefTxtDictionary
00255         ToolPathTmp = None
00256         for ToolDef in ToolDefinition.items():
00257             if self.NameGuid == ToolDef[1]:
00258                 KeyList = ToolDef[0].split('_')
00259                 Key = KeyList[0] + \
00260                       '_'        + \
00261                       KeyList[1] + \
00262                       '_'        + \
00263                       KeyList[2]
00264                 if Key in self.KeyStringList and KeyList[4] == 'GUID':
00265 
00266                     ToolPath = ToolDefinition.get( Key        + \
00267                                                    '_'        + \
00268                                                    KeyList[3] + \
00269                                                    '_'        + \
00270                                                    'PATH')
00271 
00272                     ToolOption = ToolDefinition.get( Key        + \
00273                                                     '_'        + \
00274                                                     KeyList[3] + \
00275                                                     '_'        + \
00276                                                     'FLAGS')
00277                     if ToolPathTmp == None:
00278                         ToolPathTmp = ToolPath
00279                     else:
00280                         if ToolPathTmp != ToolPath:
00281                             EdkLogger.error("GenFds", GENFDS_ERROR, "Don't know which tool to use, %s or %s ?" % (ToolPathTmp, ToolPath))
00282                             
00283                     
00284         return ToolPathTmp, ToolOption
00285 
00286 
00287 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Defines