]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/GenFds/GuidSection.py
Sync BaseTools Trunk (version r2524) 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
25918452
LG
196 if self.ExtraHeaderSize != -1:\r
197 HeaderLength = str(self.ExtraHeaderSize)\r
198\r
199 if self.ProcessRequired == "NONE" and HeaderLength == None:\r
14c48571 200 if TempFileSize > InputFileSize and TempFileSize % 4 == 0:\r
201 FileHandleIn.seek(0)\r
202 BufferIn = FileHandleIn.read()\r
203 FileHandleOut.seek(0)\r
204 BufferOut = FileHandleOut.read()\r
205 if BufferIn == BufferOut[TempFileSize - InputFileSize:]:\r
206 HeaderLength = str(TempFileSize - InputFileSize)\r
207 #auto sec guided attribute with process required\r
208 if HeaderLength == None:\r
209 Attribute.append('PROCESSING_REQUIRED')\r
52302d4d
LG
210\r
211 FileHandleIn.close()\r
212 FileHandleOut.close()\r
213 \r
214 if FirstCall and 'PROCESSING_REQUIRED' in Attribute:\r
215 # Guided data by -z option on first call is the process required data. Call the guided tool with the real option.\r
216 GenFdsGlobalVariable.GuidTool(TempFile, [DummyFile], ExternalTool, CmdOption)\r
217 \r
218 #\r
219 # Call Gensection Add Section Header\r
220 #\r
221 if self.ProcessRequired in ("TRUE", "1"):\r
222 if 'PROCESSING_REQUIRED' not in Attribute:\r
223 Attribute.append('PROCESSING_REQUIRED')\r
25918452 224 \r
52302d4d
LG
225 if self.AuthStatusValid in ("TRUE", "1"):\r
226 Attribute.append('AUTH_STATUS_VALID')\r
30fdf114 227 GenFdsGlobalVariable.GenerateSection(OutputFile, [TempFile], Section.Section.SectionType['GUIDED'],\r
52302d4d 228 Guid=self.NameGuid, GuidAttr=Attribute, GuidHdrLen=HeaderLength)\r
30fdf114
LG
229 OutputFileList = []\r
230 OutputFileList.append(OutputFile)\r
52302d4d
LG
231 if 'PROCESSING_REQUIRED' in Attribute:\r
232 # reset guided section alignment to none for the processed required guided data\r
233 self.Alignment = None\r
234 self.IncludeFvSection = False\r
235 self.ProcessRequired = "TRUE"\r
30fdf114
LG
236 return OutputFileList, self.Alignment\r
237\r
238 ## __FindExtendTool()\r
239 #\r
240 # Find location of tools to process section data\r
241 #\r
242 # @param self The object pointer\r
243 #\r
244 def __FindExtendTool__(self):\r
245 # if user not specify filter, try to deduce it from global data.\r
246 if self.KeyStringList == None or self.KeyStringList == []:\r
247 Target = GenFdsGlobalVariable.TargetName\r
248 ToolChain = GenFdsGlobalVariable.ToolChainTag\r
249 ToolDb = ToolDefClassObject.ToolDefDict(GenFdsGlobalVariable.WorkSpaceDir).ToolsDefTxtDatabase\r
250 if ToolChain not in ToolDb['TOOL_CHAIN_TAG']:\r
251 EdkLogger.error("GenFds", GENFDS_ERROR, "Can not find external tool because tool tag %s is not defined in tools_def.txt!" % ToolChain)\r
252 self.KeyStringList = [Target+'_'+ToolChain+'_'+self.CurrentArchList[0]]\r
253 for Arch in self.CurrentArchList:\r
254 if Target+'_'+ToolChain+'_'+Arch not in self.KeyStringList:\r
255 self.KeyStringList.append(Target+'_'+ToolChain+'_'+Arch)\r
256 \r
257 ToolDefinition = ToolDefClassObject.ToolDefDict(GenFdsGlobalVariable.WorkSpaceDir).ToolsDefTxtDictionary\r
258 ToolPathTmp = None\r
259 for ToolDef in ToolDefinition.items():\r
260 if self.NameGuid == ToolDef[1]:\r
261 KeyList = ToolDef[0].split('_')\r
262 Key = KeyList[0] + \\r
263 '_' + \\r
264 KeyList[1] + \\r
265 '_' + \\r
266 KeyList[2]\r
267 if Key in self.KeyStringList and KeyList[4] == 'GUID':\r
268\r
269 ToolPath = ToolDefinition.get( Key + \\r
270 '_' + \\r
271 KeyList[3] + \\r
272 '_' + \\r
273 'PATH')\r
52302d4d
LG
274\r
275 ToolOption = ToolDefinition.get( Key + \\r
276 '_' + \\r
277 KeyList[3] + \\r
278 '_' + \\r
279 'FLAGS')\r
30fdf114
LG
280 if ToolPathTmp == None:\r
281 ToolPathTmp = ToolPath\r
282 else:\r
283 if ToolPathTmp != ToolPath:\r
284 EdkLogger.error("GenFds", GENFDS_ERROR, "Don't know which tool to use, %s or %s ?" % (ToolPathTmp, ToolPath))\r
285 \r
286 \r
52302d4d 287 return ToolPathTmp, ToolOption\r
30fdf114
LG
288\r
289\r
290\r