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