]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/GenFds/GuidSection.py
License header updated to match correct format.
[mirror_edk2.git] / BaseTools / Source / Python / GenFds / GuidSection.py
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 #
18 import Section
19 import subprocess
20 from Ffs import Ffs
21 import Common.LongFilePathOs as os
22 from GenFdsGlobalVariable import GenFdsGlobalVariable
23 from CommonDataClass.FdfClass import GuidSectionClassObject
24 from Common import ToolDefClassObject
25 import sys
26 from Common import EdkLogger
27 from Common.BuildToolError import *
28 from FvImageSection import FvImageSection
29 from Common.LongFilePathSupport import OpenLongFilePath as open
30
31 ## generate GUIDed section
32 #
33 #
34 class 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 FirstCall = False
164 CmdOption = '-e'
165 if ExternalOption != None:
166 CmdOption = CmdOption + ' ' + ExternalOption
167 if self.ProcessRequired not in ("TRUE", "1") and self.IncludeFvSection and not FvAddrIsSet and self.FvParentAddr != None:
168 #FirstCall is only set for the encapsulated flash FV image without process required attribute.
169 FirstCall = True
170 #
171 # Call external tool
172 #
173 ReturnValue = [1]
174 if FirstCall:
175 #first try to call the guided tool with -z option and CmdOption for the no process required guided tool.
176 GenFdsGlobalVariable.GuidTool(TempFile, [DummyFile], ExternalTool, '-z' + ' ' + CmdOption, ReturnValue)
177
178 #
179 # when no call or first call failed, ReturnValue are not 1.
180 # Call the guided tool with CmdOption
181 #
182 if ReturnValue[0] != 0:
183 FirstCall = False
184 ReturnValue[0] = 0
185 GenFdsGlobalVariable.GuidTool(TempFile, [DummyFile], ExternalTool, CmdOption)
186
187 FileHandleIn = open(DummyFile,'rb')
188 FileHandleIn.seek(0,2)
189 InputFileSize = FileHandleIn.tell()
190
191 FileHandleOut = open(TempFile,'rb')
192 FileHandleOut.seek(0,2)
193 TempFileSize = FileHandleOut.tell()
194
195 Attribute = []
196 HeaderLength = None
197 if self.ExtraHeaderSize != -1:
198 HeaderLength = str(self.ExtraHeaderSize)
199
200 if self.ProcessRequired == "NONE" and HeaderLength == None:
201 if TempFileSize > InputFileSize:
202 FileHandleIn.seek(0)
203 BufferIn = FileHandleIn.read()
204 FileHandleOut.seek(0)
205 BufferOut = FileHandleOut.read()
206 if BufferIn == BufferOut[TempFileSize - InputFileSize:]:
207 HeaderLength = str(TempFileSize - InputFileSize)
208 #auto sec guided attribute with process required
209 if HeaderLength == None:
210 Attribute.append('PROCESSING_REQUIRED')
211
212 FileHandleIn.close()
213 FileHandleOut.close()
214
215 if FirstCall and 'PROCESSING_REQUIRED' in Attribute:
216 # Guided data by -z option on first call is the process required data. Call the guided tool with the real option.
217 GenFdsGlobalVariable.GuidTool(TempFile, [DummyFile], ExternalTool, CmdOption)
218
219 #
220 # Call Gensection Add Section Header
221 #
222 if self.ProcessRequired in ("TRUE", "1"):
223 if 'PROCESSING_REQUIRED' not in Attribute:
224 Attribute.append('PROCESSING_REQUIRED')
225
226 if self.AuthStatusValid in ("TRUE", "1"):
227 Attribute.append('AUTH_STATUS_VALID')
228 GenFdsGlobalVariable.GenerateSection(OutputFile, [TempFile], Section.Section.SectionType['GUIDED'],
229 Guid=self.NameGuid, GuidAttr=Attribute, GuidHdrLen=HeaderLength)
230 OutputFileList = []
231 OutputFileList.append(OutputFile)
232 if 'PROCESSING_REQUIRED' in Attribute:
233 # reset guided section alignment to none for the processed required guided data
234 self.Alignment = None
235 self.IncludeFvSection = False
236 self.ProcessRequired = "TRUE"
237 return OutputFileList, self.Alignment
238
239 ## __FindExtendTool()
240 #
241 # Find location of tools to process section data
242 #
243 # @param self The object pointer
244 #
245 def __FindExtendTool__(self):
246 # if user not specify filter, try to deduce it from global data.
247 if self.KeyStringList == None or self.KeyStringList == []:
248 Target = GenFdsGlobalVariable.TargetName
249 ToolChain = GenFdsGlobalVariable.ToolChainTag
250 ToolDb = ToolDefClassObject.ToolDefDict(GenFdsGlobalVariable.ConfDir).ToolsDefTxtDatabase
251 if ToolChain not in ToolDb['TOOL_CHAIN_TAG']:
252 EdkLogger.error("GenFds", GENFDS_ERROR, "Can not find external tool because tool tag %s is not defined in tools_def.txt!" % ToolChain)
253 self.KeyStringList = [Target+'_'+ToolChain+'_'+self.CurrentArchList[0]]
254 for Arch in self.CurrentArchList:
255 if Target + '_' + ToolChain + '_' + Arch not in self.KeyStringList:
256 self.KeyStringList.append(Target + '_' + ToolChain + '_' + Arch)
257
258 ToolDefinition = ToolDefClassObject.ToolDefDict(GenFdsGlobalVariable.ConfDir).ToolsDefTxtDictionary
259 ToolPathTmp = None
260 for ToolDef in ToolDefinition.items():
261 if self.NameGuid == ToolDef[1]:
262 KeyList = ToolDef[0].split('_')
263 Key = KeyList[0] + \
264 '_' + \
265 KeyList[1] + \
266 '_' + \
267 KeyList[2]
268 if Key in self.KeyStringList and KeyList[4] == 'GUID':
269
270 ToolPath = ToolDefinition.get( Key + \
271 '_' + \
272 KeyList[3] + \
273 '_' + \
274 'PATH')
275
276 ToolOption = ToolDefinition.get( Key + \
277 '_' + \
278 KeyList[3] + \
279 '_' + \
280 'FLAGS')
281 if ToolPathTmp == None:
282 ToolPathTmp = ToolPath
283 else:
284 if ToolPathTmp != ToolPath:
285 EdkLogger.error("GenFds", GENFDS_ERROR, "Don't know which tool to use, %s or %s ?" % (ToolPathTmp, ToolPath))
286
287
288 return ToolPathTmp, ToolOption
289
290
291