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