]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/GenFds/GuidSection.py
36ad4f58ef8189f2cb13b2308179baea08b18138
[mirror_edk2.git] / BaseTools / Source / Python / GenFds / GuidSection.py
1 ## @file
2 # process GUIDed section generation
3 #
4 # Copyright (c) 2007 - 2010, 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 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
30 ## generate GUIDed section
31 #
32 #
33 class GuidSection(GuidSectionClassObject) :
34
35 ## The constructor
36 #
37 # @param self The object pointer
38 #
39 def __init__(self):
40 GuidSectionClassObject.__init__(self)
41
42 ## GenSection() method
43 #
44 # Generate GUIDed section
45 #
46 # @param self The object pointer
47 # @param OutputPath Where to place output file
48 # @param ModuleName Which module this section belongs to
49 # @param SecNum Index of section
50 # @param KeyStringList Filter for inputs of section generation
51 # @param FfsInf FfsInfStatement object that contains this section data
52 # @param Dict dictionary contains macro and its value
53 # @retval tuple (Generated file name, section alignment)
54 #
55 def GenSection(self, OutputPath, ModuleName, SecNum, KeyStringList, FfsInf = None, Dict = {}):
56 #
57 # Generate all section
58 #
59 self.KeyStringList = KeyStringList
60 self.CurrentArchList = GenFdsGlobalVariable.ArchList
61 if FfsInf != None:
62 self.Alignment = FfsInf.__ExtendMacro__(self.Alignment)
63 self.NameGuid = FfsInf.__ExtendMacro__(self.NameGuid)
64 self.SectionType = FfsInf.__ExtendMacro__(self.SectionType)
65 self.CurrentArchList = [FfsInf.CurrentArch]
66
67 SectFile = tuple()
68 SectAlign = []
69 Index = 0
70 MaxAlign = None
71 if self.FvAddr != []:
72 FvAddrIsSet = True
73 else:
74 FvAddrIsSet = False
75
76 if self.ProcessRequired in ("TRUE", "1"):
77 if self.FvAddr != []:
78 #no use FvAddr when the image is processed.
79 self.FvAddr = []
80 if self.FvParentAddr != None:
81 #no use Parent Addr when the image is processed.
82 self.FvParentAddr = None
83
84 for Sect in self.SectionList:
85 Index = Index + 1
86 SecIndex = '%s.%d' %(SecNum,Index)
87 # set base address for inside FvImage
88 if isinstance(Sect, FvImageSection):
89 if self.FvAddr != []:
90 Sect.FvAddr = self.FvAddr.pop(0)
91 self.IncludeFvSection = True
92 elif isinstance(Sect, GuidSection):
93 Sect.FvAddr = self.FvAddr
94 Sect.FvParentAddr = self.FvParentAddr
95 ReturnSectList, align = Sect.GenSection(OutputPath, ModuleName, SecIndex, KeyStringList,FfsInf, Dict)
96 if isinstance(Sect, GuidSection):
97 if Sect.IncludeFvSection:
98 self.IncludeFvSection = Sect.IncludeFvSection
99
100 if align != None:
101 if MaxAlign == None:
102 MaxAlign = align
103 if GenFdsGlobalVariable.GetAlignment (align) > GenFdsGlobalVariable.GetAlignment (MaxAlign):
104 MaxAlign = align
105 if ReturnSectList != []:
106 if align == None:
107 align = "1"
108 for file in ReturnSectList:
109 SectFile += (file,)
110 SectAlign.append(align)
111
112 if MaxAlign != None:
113 if self.Alignment == None:
114 self.Alignment = MaxAlign
115 else:
116 if GenFdsGlobalVariable.GetAlignment (MaxAlign) > GenFdsGlobalVariable.GetAlignment (self.Alignment):
117 self.Alignment = MaxAlign
118
119 OutputFile = OutputPath + \
120 os.sep + \
121 ModuleName + \
122 'SEC' + \
123 SecNum + \
124 Ffs.SectionSuffix['GUIDED']
125 OutputFile = os.path.normpath(OutputFile)
126
127 ExternalTool = None
128 ExternalOption = None
129 if self.NameGuid != None:
130 ExternalTool, ExternalOption = self.__FindExtendTool__()
131
132 #
133 # If not have GUID , call default
134 # GENCRC32 section
135 #
136 if self.NameGuid == None :
137 GenFdsGlobalVariable.VerboseLogger( "Use GenSection function Generate CRC32 Section")
138 GenFdsGlobalVariable.GenerateSection(OutputFile, SectFile, Section.Section.SectionType[self.SectionType], InputAlign=SectAlign)
139 OutputFileList = []
140 OutputFileList.append(OutputFile)
141 return OutputFileList, self.Alignment
142 #or GUID not in External Tool List
143 elif ExternalTool == None:
144 EdkLogger.error("GenFds", GENFDS_ERROR, "No tool found with GUID %s" % self.NameGuid)
145 else:
146 DummyFile = OutputFile+".dummy"
147 #
148 # Call GenSection with DUMMY section type.
149 #
150 GenFdsGlobalVariable.GenerateSection(DummyFile, SectFile, InputAlign=SectAlign)
151 #
152 # Use external tool process the Output
153 #
154 TempFile = OutputPath + \
155 os.sep + \
156 ModuleName + \
157 'SEC' + \
158 SecNum + \
159 '.tmp'
160 TempFile = os.path.normpath(TempFile)
161
162 FirstCall = False
163 CmdOption = '-e'
164 if ExternalOption != None:
165 CmdOption = CmdOption + ' ' + ExternalOption
166 if self.ProcessRequired not in ("TRUE", "1") and self.IncludeFvSection and not FvAddrIsSet and self.FvParentAddr != None:
167 #FirstCall is only set for the encapsulated flash FV image without process required attribute.
168 FirstCall = True
169 #
170 # Call external tool
171 #
172 ReturnValue = [1]
173 if FirstCall:
174 #first try to call the guided tool with -z option and CmdOption for the no process required guided tool.
175 GenFdsGlobalVariable.GuidTool(TempFile, [DummyFile], ExternalTool, '-z' + ' ' + CmdOption, ReturnValue)
176
177 #
178 # when no call or first call failed, ReturnValue are not 1.
179 # Call the guided tool with CmdOption
180 #
181 if ReturnValue[0] != 0:
182 FirstCall = False
183 ReturnValue[0] = 0
184 GenFdsGlobalVariable.GuidTool(TempFile, [DummyFile], ExternalTool, CmdOption)
185
186 FileHandleIn = open(DummyFile,'rb')
187 FileHandleIn.seek(0,2)
188 InputFileSize = FileHandleIn.tell()
189
190 FileHandleOut = open(TempFile,'rb')
191 FileHandleOut.seek(0,2)
192 TempFileSize = FileHandleOut.tell()
193
194 Attribute = []
195 HeaderLength = None
196 if self.ExtraHeaderSize != -1:
197 HeaderLength = str(self.ExtraHeaderSize)
198
199 if self.ProcessRequired == "NONE" and HeaderLength == None:
200 if TempFileSize > InputFileSize and TempFileSize % 4 == 0:
201 FileHandleIn.seek(0)
202 BufferIn = FileHandleIn.read()
203 FileHandleOut.seek(0)
204 BufferOut = FileHandleOut.read()
205 if BufferIn == BufferOut[TempFileSize - InputFileSize:]:
206 HeaderLength = str(TempFileSize - InputFileSize)
207 #auto sec guided attribute with process required
208 if HeaderLength == None:
209 Attribute.append('PROCESSING_REQUIRED')
210
211 FileHandleIn.close()
212 FileHandleOut.close()
213
214 if FirstCall and 'PROCESSING_REQUIRED' in Attribute:
215 # Guided data by -z option on first call is the process required data. Call the guided tool with the real option.
216 GenFdsGlobalVariable.GuidTool(TempFile, [DummyFile], ExternalTool, CmdOption)
217
218 #
219 # Call Gensection Add Section Header
220 #
221 if self.ProcessRequired in ("TRUE", "1"):
222 if 'PROCESSING_REQUIRED' not in Attribute:
223 Attribute.append('PROCESSING_REQUIRED')
224
225 if self.AuthStatusValid in ("TRUE", "1"):
226 Attribute.append('AUTH_STATUS_VALID')
227 GenFdsGlobalVariable.GenerateSection(OutputFile, [TempFile], Section.Section.SectionType['GUIDED'],
228 Guid=self.NameGuid, GuidAttr=Attribute, GuidHdrLen=HeaderLength)
229 OutputFileList = []
230 OutputFileList.append(OutputFile)
231 if 'PROCESSING_REQUIRED' in Attribute:
232 # reset guided section alignment to none for the processed required guided data
233 self.Alignment = None
234 self.IncludeFvSection = False
235 self.ProcessRequired = "TRUE"
236 return OutputFileList, self.Alignment
237
238 ## __FindExtendTool()
239 #
240 # Find location of tools to process section data
241 #
242 # @param self The object pointer
243 #
244 def __FindExtendTool__(self):
245 # if user not specify filter, try to deduce it from global data.
246 if self.KeyStringList == None or self.KeyStringList == []:
247 Target = GenFdsGlobalVariable.TargetName
248 ToolChain = GenFdsGlobalVariable.ToolChainTag
249 ToolDb = ToolDefClassObject.ToolDefDict(GenFdsGlobalVariable.WorkSpaceDir).ToolsDefTxtDatabase
250 if ToolChain not in ToolDb['TOOL_CHAIN_TAG']:
251 EdkLogger.error("GenFds", GENFDS_ERROR, "Can not find external tool because tool tag %s is not defined in tools_def.txt!" % ToolChain)
252 self.KeyStringList = [Target+'_'+ToolChain+'_'+self.CurrentArchList[0]]
253 for Arch in self.CurrentArchList:
254 if Target+'_'+ToolChain+'_'+Arch not in self.KeyStringList:
255 self.KeyStringList.append(Target+'_'+ToolChain+'_'+Arch)
256
257 ToolDefinition = ToolDefClassObject.ToolDefDict(GenFdsGlobalVariable.WorkSpaceDir).ToolsDefTxtDictionary
258 ToolPathTmp = None
259 for ToolDef in ToolDefinition.items():
260 if self.NameGuid == ToolDef[1]:
261 KeyList = ToolDef[0].split('_')
262 Key = KeyList[0] + \
263 '_' + \
264 KeyList[1] + \
265 '_' + \
266 KeyList[2]
267 if Key in self.KeyStringList and KeyList[4] == 'GUID':
268
269 ToolPath = ToolDefinition.get( Key + \
270 '_' + \
271 KeyList[3] + \
272 '_' + \
273 'PATH')
274
275 ToolOption = ToolDefinition.get( Key + \
276 '_' + \
277 KeyList[3] + \
278 '_' + \
279 'FLAGS')
280 if ToolPathTmp == None:
281 ToolPathTmp = ToolPath
282 else:
283 if ToolPathTmp != ToolPath:
284 EdkLogger.error("GenFds", GENFDS_ERROR, "Don't know which tool to use, %s or %s ?" % (ToolPathTmp, ToolPath))
285
286
287 return ToolPathTmp, ToolOption
288
289
290