1## @file 2# process GUIDed section generation 3# 4# Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR> 5# 6# This program and the accompanying materials 7# are licensed and made available under the terms and conditions of the BSD License 8# which accompanies this distribution. The full text of the license may be found at 9# http://opensource.org/licenses/bsd-license.php 10# 11# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, 12# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. 13# 14 15## 16# Import Modules 17# 18import Section 19import subprocess 20from Ffs import Ffs 21import Common.LongFilePathOs as os 22from GenFdsGlobalVariable import GenFdsGlobalVariable 23from CommonDataClass.FdfClass import GuidSectionClassObject 24from Common import ToolDefClassObject 25import sys 26from Common import EdkLogger 27from Common.BuildToolError import * 28from FvImageSection import FvImageSection 29from Common.LongFilePathSupport import OpenLongFilePath as open 30 31## generate GUIDed section 32# 33# 34class GuidSection(GuidSectionClassObject) : 35 36 ## The constructor 37 # 38 # @param self The object pointer 39 # 40 def __init__(self): 41 GuidSectionClassObject.__init__(self) 42 43 ## GenSection() method 44 # 45 # Generate GUIDed section 46 # 47 # @param self The object pointer 48 # @param OutputPath Where to place output file 49 # @param ModuleName Which module this section belongs to 50 # @param SecNum Index of section 51 # @param KeyStringList Filter for inputs of section generation 52 # @param FfsInf FfsInfStatement object that contains this section data 53 # @param Dict dictionary contains macro and its value 54 # @retval tuple (Generated file name, section alignment) 55 # 56 def GenSection(self, OutputPath, ModuleName, SecNum, KeyStringList, FfsInf=None, Dict={}): 57 # 58 # Generate all section 59 # 60 self.KeyStringList = KeyStringList 61 self.CurrentArchList = GenFdsGlobalVariable.ArchList 62 if FfsInf != None: 63 self.Alignment = FfsInf.__ExtendMacro__(self.Alignment) 64 self.NameGuid = FfsInf.__ExtendMacro__(self.NameGuid) 65 self.SectionType = FfsInf.__ExtendMacro__(self.SectionType) 66 self.CurrentArchList = [FfsInf.CurrentArch] 67 68 SectFile = tuple() 69 SectAlign = [] 70 Index = 0 71 MaxAlign = None 72 if self.FvAddr != []: 73 FvAddrIsSet = True 74 else: 75 FvAddrIsSet = False 76 77 if self.ProcessRequired in ("TRUE", "1"): 78 if self.FvAddr != []: 79 #no use FvAddr when the image is processed. 80 self.FvAddr = [] 81 if self.FvParentAddr != None: 82 #no use Parent Addr when the image is processed. 83 self.FvParentAddr = None 84 85 for Sect in self.SectionList: 86 Index = Index + 1 87 SecIndex = '%s.%d' % (SecNum, Index) 88 # set base address for inside FvImage 89 if isinstance(Sect, FvImageSection): 90 if self.FvAddr != []: 91 Sect.FvAddr = self.FvAddr.pop(0) 92 self.IncludeFvSection = True 93 elif isinstance(Sect, GuidSection): 94 Sect.FvAddr = self.FvAddr 95 Sect.FvParentAddr = self.FvParentAddr 96 ReturnSectList, align = Sect.GenSection(OutputPath, ModuleName, SecIndex, KeyStringList, FfsInf, Dict) 97 if isinstance(Sect, GuidSection): 98 if Sect.IncludeFvSection: 99 self.IncludeFvSection = Sect.IncludeFvSection 100 101 if align != None: 102 if MaxAlign == None: 103 MaxAlign = align 104 if GenFdsGlobalVariable.GetAlignment (align) > GenFdsGlobalVariable.GetAlignment (MaxAlign): 105 MaxAlign = align 106 if ReturnSectList != []: 107 if align == None: 108 align = "1" 109 for file in ReturnSectList: 110 SectFile += (file,) 111 SectAlign.append(align) 112 113 if MaxAlign != None: 114 if self.Alignment == None: 115 self.Alignment = MaxAlign 116 else: 117 if GenFdsGlobalVariable.GetAlignment (MaxAlign) > GenFdsGlobalVariable.GetAlignment (self.Alignment): 118 self.Alignment = MaxAlign 119 120 OutputFile = OutputPath + \ 121 os.sep + \ 122 ModuleName + \ 123 'SEC' + \ 124 SecNum + \ 125 Ffs.SectionSuffix['GUIDED'] 126 OutputFile = os.path.normpath(OutputFile) 127 128 ExternalTool = None 129 ExternalOption = None 130 if self.NameGuid != None: 131 ExternalTool, ExternalOption = self.__FindExtendTool__() 132 133 # 134 # If not have GUID , call default 135 # GENCRC32 section 136 # 137 if self.NameGuid == None : 138 GenFdsGlobalVariable.VerboseLogger("Use GenSection function Generate CRC32 Section") 139 GenFdsGlobalVariable.GenerateSection(OutputFile, SectFile, Section.Section.SectionType[self.SectionType], InputAlign=SectAlign) 140 OutputFileList = [] 141 OutputFileList.append(OutputFile) 142 return OutputFileList, self.Alignment 143 #or GUID not in External Tool List 144 elif ExternalTool == None: 145 EdkLogger.error("GenFds", GENFDS_ERROR, "No tool found with GUID %s" % self.NameGuid) 146 else: 147 DummyFile = OutputFile + ".dummy" 148 # 149 # Call GenSection with DUMMY section type. 150 # 151 GenFdsGlobalVariable.GenerateSection(DummyFile, SectFile, InputAlign=SectAlign) 152 # 153 # Use external tool process the Output 154 # 155 TempFile = OutputPath + \ 156 os.sep + \ 157 ModuleName + \ 158 'SEC' + \ 159 SecNum + \ 160 '.tmp' 161 TempFile = os.path.normpath(TempFile) 162 # 163 # Remove temp file if its time stamp is older than dummy file 164 # Just in case the external tool fails at this time but succeeded before 165 # Error should be reported if the external tool does not generate a new output based on new input 166 # 167 if os.path.exists(TempFile) and os.path.exists(DummyFile) and os.path.getmtime(TempFile) < os.path.getmtime(DummyFile): 168 os.remove(TempFile) 169 170 FirstCall = False 171 CmdOption = '-e' 172 if ExternalOption != None: 173 CmdOption = CmdOption + ' ' + ExternalOption 174 if self.ProcessRequired not in ("TRUE", "1") and self.IncludeFvSection and not FvAddrIsSet and self.FvParentAddr != None: 175 #FirstCall is only set for the encapsulated flash FV image without process required attribute. 176 FirstCall = True 177 # 178 # Call external tool 179 # 180 ReturnValue = [1] 181 if FirstCall: 182 #first try to call the guided tool with -z option and CmdOption for the no process required guided tool. 183 GenFdsGlobalVariable.GuidTool(TempFile, [DummyFile], ExternalTool, '-z' + ' ' + CmdOption, ReturnValue) 184 185 # 186 # when no call or first call failed, ReturnValue are not 1. 187 # Call the guided tool with CmdOption 188 # 189 if ReturnValue[0] != 0: 190 FirstCall = False 191 ReturnValue[0] = 0 192 GenFdsGlobalVariable.GuidTool(TempFile, [DummyFile], ExternalTool, CmdOption) 193 # 194 # There is external tool which does not follow standard rule which return nonzero if tool fails 195 # The output file has to be checked 196 # 197 if not os.path.exists(TempFile): 198 EdkLogger.error("GenFds", COMMAND_FAILURE, 'Fail to call %s, no output file was generated' % ExternalTool) 199 200 FileHandleIn = open(DummyFile, 'rb') 201 FileHandleIn.seek(0, 2) 202 InputFileSize = FileHandleIn.tell() 203 204 FileHandleOut = open(TempFile, 'rb') 205 FileHandleOut.seek(0, 2) 206 TempFileSize = FileHandleOut.tell() 207 208 Attribute = [] 209 HeaderLength = None 210 if self.ExtraHeaderSize != -1: 211 HeaderLength = str(self.ExtraHeaderSize) 212 213 if self.ProcessRequired == "NONE" and HeaderLength == None: 214 if TempFileSize > InputFileSize: 215 FileHandleIn.seek(0) 216 BufferIn = FileHandleIn.read() 217 FileHandleOut.seek(0) 218 BufferOut = FileHandleOut.read() 219 if BufferIn == BufferOut[TempFileSize - InputFileSize:]: 220 HeaderLength = str(TempFileSize - InputFileSize) 221 #auto sec guided attribute with process required 222 if HeaderLength == None: 223 Attribute.append('PROCESSING_REQUIRED') 224 225 FileHandleIn.close() 226 FileHandleOut.close() 227 228 if FirstCall and 'PROCESSING_REQUIRED' in Attribute: 229 # Guided data by -z option on first call is the process required data. Call the guided tool with the real option. 230 GenFdsGlobalVariable.GuidTool(TempFile, [DummyFile], ExternalTool, CmdOption) 231 232 # 233 # Call Gensection Add Section Header 234 # 235 if self.ProcessRequired in ("TRUE", "1"): 236 if 'PROCESSING_REQUIRED' not in Attribute: 237 Attribute.append('PROCESSING_REQUIRED') 238 239 if self.AuthStatusValid in ("TRUE", "1"): 240 Attribute.append('AUTH_STATUS_VALID') 241 GenFdsGlobalVariable.GenerateSection(OutputFile, [TempFile], Section.Section.SectionType['GUIDED'], 242 Guid=self.NameGuid, GuidAttr=Attribute, GuidHdrLen=HeaderLength) 243 OutputFileList = [] 244 OutputFileList.append(OutputFile) 245 if 'PROCESSING_REQUIRED' in Attribute: 246 # reset guided section alignment to none for the processed required guided data 247 self.Alignment = None 248 self.IncludeFvSection = False 249 self.ProcessRequired = "TRUE" 250 return OutputFileList, self.Alignment 251 252 ## __FindExtendTool() 253 # 254 # Find location of tools to process section data 255 # 256 # @param self The object pointer 257 # 258 def __FindExtendTool__(self): 259 # if user not specify filter, try to deduce it from global data. 260 if self.KeyStringList == None or self.KeyStringList == []: 261 Target = GenFdsGlobalVariable.TargetName 262 ToolChain = GenFdsGlobalVariable.ToolChainTag 263 ToolDb = ToolDefClassObject.ToolDefDict(GenFdsGlobalVariable.ConfDir).ToolsDefTxtDatabase 264 if ToolChain not in ToolDb['TOOL_CHAIN_TAG']: 265 EdkLogger.error("GenFds", GENFDS_ERROR, "Can not find external tool because tool tag %s is not defined in tools_def.txt!" % ToolChain) 266 self.KeyStringList = [Target + '_' + ToolChain + '_' + self.CurrentArchList[0]] 267 for Arch in self.CurrentArchList: 268 if Target + '_' + ToolChain + '_' + Arch not in self.KeyStringList: 269 self.KeyStringList.append(Target + '_' + ToolChain + '_' + Arch) 270 271 ToolDefinition = ToolDefClassObject.ToolDefDict(GenFdsGlobalVariable.ConfDir).ToolsDefTxtDictionary 272 ToolPathTmp = None 273 ToolOption = None 274 for ToolDef in ToolDefinition.items(): 275 if self.NameGuid == ToolDef[1]: 276 KeyList = ToolDef[0].split('_') 277 Key = KeyList[0] + \ 278 '_' + \ 279 KeyList[1] + \ 280 '_' + \ 281 KeyList[2] 282 if Key in self.KeyStringList and KeyList[4] == 'GUID': 283 284 ToolPath = ToolDefinition.get(Key + \ 285 '_' + \ 286 KeyList[3] + \ 287 '_' + \ 288 'PATH') 289 290 ToolOption = ToolDefinition.get(Key + \ 291 '_' + \ 292 KeyList[3] + \ 293 '_' + \ 294 'FLAGS') 295 if ToolPathTmp == None: 296 ToolPathTmp = ToolPath 297 else: 298 if ToolPathTmp != ToolPath: 299 EdkLogger.error("GenFds", GENFDS_ERROR, "Don't know which tool to use, %s or %s ?" % (ToolPathTmp, ToolPath)) 300 301 302 return ToolPathTmp, ToolOption 303 304 305 306