]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/GenFds/GuidSection.py
License header updated to match correct format.
[mirror_edk2.git] / BaseTools / Source / Python / GenFds / GuidSection.py
CommitLineData
30fdf114
LG
1## @file\r
2# process GUIDed section generation\r
3#\r
1be2ed90 4# Copyright (c) 2007 - 2014, 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
1be2ed90 21import Common.LongFilePathOs as os\r
30fdf114
LG
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
1be2ed90 29from Common.LongFilePathSupport import OpenLongFilePath as open\r
30fdf114
LG
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
52302d4d
LG
68 SectFile = tuple()\r
69 SectAlign = []\r
30fdf114 70 Index = 0\r
52302d4d
LG
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
30fdf114
LG
85 for Sect in self.SectionList:\r
86 Index = Index + 1\r
87 SecIndex = '%s.%d' %(SecNum,Index)\r
52302d4d
LG
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
30fdf114 96 ReturnSectList, align = Sect.GenSection(OutputPath, ModuleName, SecIndex, KeyStringList,FfsInf, Dict)\r
52302d4d
LG
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
30fdf114 106 if ReturnSectList != []:\r
52302d4d
LG
107 if align == None:\r
108 align = "1"\r
30fdf114
LG
109 for file in ReturnSectList:\r
110 SectFile += (file,)\r
52302d4d 111 SectAlign.append(align)\r
30fdf114 112\r
52302d4d
LG
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
30fdf114
LG
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
52302d4d 129 ExternalOption = None\r
30fdf114 130 if self.NameGuid != None:\r
52302d4d
LG
131 ExternalTool, ExternalOption = self.__FindExtendTool__()\r
132\r
30fdf114
LG
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
52302d4d 139 GenFdsGlobalVariable.GenerateSection(OutputFile, SectFile, Section.Section.SectionType[self.SectionType], InputAlign=SectAlign)\r
30fdf114
LG
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
52302d4d 147 DummyFile = OutputFile+".dummy"\r
30fdf114
LG
148 #\r
149 # Call GenSection with DUMMY section type.\r
150 #\r
52302d4d 151 GenFdsGlobalVariable.GenerateSection(DummyFile, SectFile, InputAlign=SectAlign)\r
30fdf114
LG
152 #\r
153 # Use external tool process the Output\r
154 #\r
30fdf114
LG
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
52302d4d
LG
163 FirstCall = False\r
164 CmdOption = '-e'\r
165 if ExternalOption != None:\r
166 CmdOption = CmdOption + ' ' + ExternalOption\r
167 if self.ProcessRequired not in ("TRUE", "1") and self.IncludeFvSection and not FvAddrIsSet and self.FvParentAddr != None:\r
168 #FirstCall is only set for the encapsulated flash FV image without process required attribute.\r
169 FirstCall = True\r
30fdf114
LG
170 #\r
171 # Call external tool\r
172 #\r
52302d4d
LG
173 ReturnValue = [1]\r
174 if FirstCall:\r
175 #first try to call the guided tool with -z option and CmdOption for the no process required guided tool.\r
176 GenFdsGlobalVariable.GuidTool(TempFile, [DummyFile], ExternalTool, '-z' + ' ' + CmdOption, ReturnValue)\r
30fdf114
LG
177\r
178 #\r
52302d4d
LG
179 # when no call or first call failed, ReturnValue are not 1.\r
180 # Call the guided tool with CmdOption\r
30fdf114 181 #\r
52302d4d
LG
182 if ReturnValue[0] != 0:\r
183 FirstCall = False\r
184 ReturnValue[0] = 0\r
185 GenFdsGlobalVariable.GuidTool(TempFile, [DummyFile], ExternalTool, CmdOption)\r
186\r
187 FileHandleIn = open(DummyFile,'rb')\r
188 FileHandleIn.seek(0,2)\r
189 InputFileSize = FileHandleIn.tell()\r
190 \r
191 FileHandleOut = open(TempFile,'rb')\r
192 FileHandleOut.seek(0,2)\r
193 TempFileSize = FileHandleOut.tell()\r
194\r
195 Attribute = []\r
196 HeaderLength = None\r
25918452
LG
197 if self.ExtraHeaderSize != -1:\r
198 HeaderLength = str(self.ExtraHeaderSize)\r
199\r
200 if self.ProcessRequired == "NONE" and HeaderLength == None:\r
97fa0ee9 201 if TempFileSize > InputFileSize:\r
14c48571 202 FileHandleIn.seek(0)\r
203 BufferIn = FileHandleIn.read()\r
204 FileHandleOut.seek(0)\r
205 BufferOut = FileHandleOut.read()\r
206 if BufferIn == BufferOut[TempFileSize - InputFileSize:]:\r
207 HeaderLength = str(TempFileSize - InputFileSize)\r
208 #auto sec guided attribute with process required\r
209 if HeaderLength == None:\r
210 Attribute.append('PROCESSING_REQUIRED')\r
52302d4d
LG
211\r
212 FileHandleIn.close()\r
213 FileHandleOut.close()\r
214 \r
215 if FirstCall and 'PROCESSING_REQUIRED' in Attribute:\r
216 # Guided data by -z option on first call is the process required data. Call the guided tool with the real option.\r
217 GenFdsGlobalVariable.GuidTool(TempFile, [DummyFile], ExternalTool, CmdOption)\r
218 \r
219 #\r
220 # Call Gensection Add Section Header\r
221 #\r
222 if self.ProcessRequired in ("TRUE", "1"):\r
223 if 'PROCESSING_REQUIRED' not in Attribute:\r
224 Attribute.append('PROCESSING_REQUIRED')\r
25918452 225 \r
52302d4d
LG
226 if self.AuthStatusValid in ("TRUE", "1"):\r
227 Attribute.append('AUTH_STATUS_VALID')\r
30fdf114 228 GenFdsGlobalVariable.GenerateSection(OutputFile, [TempFile], Section.Section.SectionType['GUIDED'],\r
52302d4d 229 Guid=self.NameGuid, GuidAttr=Attribute, GuidHdrLen=HeaderLength)\r
30fdf114
LG
230 OutputFileList = []\r
231 OutputFileList.append(OutputFile)\r
52302d4d
LG
232 if 'PROCESSING_REQUIRED' in Attribute:\r
233 # reset guided section alignment to none for the processed required guided data\r
234 self.Alignment = None\r
235 self.IncludeFvSection = False\r
236 self.ProcessRequired = "TRUE"\r
30fdf114
LG
237 return OutputFileList, self.Alignment\r
238\r
239 ## __FindExtendTool()\r
240 #\r
241 # Find location of tools to process section data\r
242 #\r
243 # @param self The object pointer\r
244 #\r
245 def __FindExtendTool__(self):\r
246 # if user not specify filter, try to deduce it from global data.\r
247 if self.KeyStringList == None or self.KeyStringList == []:\r
248 Target = GenFdsGlobalVariable.TargetName\r
249 ToolChain = GenFdsGlobalVariable.ToolChainTag\r
97fa0ee9 250 ToolDb = ToolDefClassObject.ToolDefDict(GenFdsGlobalVariable.ConfDir).ToolsDefTxtDatabase\r
30fdf114
LG
251 if ToolChain not in ToolDb['TOOL_CHAIN_TAG']:\r
252 EdkLogger.error("GenFds", GENFDS_ERROR, "Can not find external tool because tool tag %s is not defined in tools_def.txt!" % ToolChain)\r
253 self.KeyStringList = [Target+'_'+ToolChain+'_'+self.CurrentArchList[0]]\r
254 for Arch in self.CurrentArchList:\r
97fa0ee9
YL
255 if Target + '_' + ToolChain + '_' + Arch not in self.KeyStringList:\r
256 self.KeyStringList.append(Target + '_' + ToolChain + '_' + Arch)\r
257\r
258 ToolDefinition = ToolDefClassObject.ToolDefDict(GenFdsGlobalVariable.ConfDir).ToolsDefTxtDictionary\r
30fdf114
LG
259 ToolPathTmp = None\r
260 for ToolDef in ToolDefinition.items():\r
261 if self.NameGuid == ToolDef[1]:\r
262 KeyList = ToolDef[0].split('_')\r
263 Key = KeyList[0] + \\r
264 '_' + \\r
265 KeyList[1] + \\r
266 '_' + \\r
267 KeyList[2]\r
268 if Key in self.KeyStringList and KeyList[4] == 'GUID':\r
269\r
270 ToolPath = ToolDefinition.get( Key + \\r
271 '_' + \\r
272 KeyList[3] + \\r
273 '_' + \\r
274 'PATH')\r
52302d4d
LG
275\r
276 ToolOption = ToolDefinition.get( Key + \\r
277 '_' + \\r
278 KeyList[3] + \\r
279 '_' + \\r
280 'FLAGS')\r
30fdf114
LG
281 if ToolPathTmp == None:\r
282 ToolPathTmp = ToolPath\r
283 else:\r
284 if ToolPathTmp != ToolPath:\r
285 EdkLogger.error("GenFds", GENFDS_ERROR, "Don't know which tool to use, %s or %s ?" % (ToolPathTmp, ToolPath))\r
286 \r
287 \r
52302d4d 288 return ToolPathTmp, ToolOption\r
30fdf114
LG
289\r
290\r
291\r