SecurityPkg Variable: Remove mStorageData buffer allocation and use Scratch buffer...
[mirror_edk2.git] / BaseTools / Source / Python / GenFds / FfsInfStatement.py
CommitLineData
30fdf114
LG
1## @file\r
2# process FFS generation from INF statement\r
3#\r
da92f276 4# Copyright (c) 2007 - 2011, 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 Rule\r
19import os\r
20import shutil\r
da92f276
LG
21import StringIO\r
22from struct import *\r
30fdf114
LG
23from GenFdsGlobalVariable import GenFdsGlobalVariable\r
24import Ffs\r
25import subprocess\r
26import sys\r
27import Section\r
28import RuleSimpleFile\r
29import RuleComplexFile\r
30from CommonDataClass.FdfClass import FfsInfStatementClassObject\r
31from Common.String import *\r
32from Common.Misc import PathClass\r
33from Common.Misc import GuidStructureByteArrayToGuidString\r
34from Common import EdkLogger\r
35from Common.BuildToolError import *\r
52302d4d
LG
36from GuidSection import GuidSection\r
37from FvImageSection import FvImageSection\r
38from Common.Misc import PeImageClass\r
b36d134f 39from AutoGen.GenDepex import DependencyExpression\r
30fdf114
LG
40\r
41## generate FFS from INF\r
42#\r
43#\r
44class FfsInfStatement(FfsInfStatementClassObject):\r
45 ## The constructor\r
46 #\r
47 # @param self The object pointer\r
48 #\r
49 def __init__(self):\r
50 FfsInfStatementClassObject.__init__(self)\r
51 self.TargetOverrideList = []\r
52 self.ShadowFromInfFile = None\r
53 self.KeepRelocFromRule = None\r
54 self.InDsc = True\r
55 self.OptRomDefs = {}\r
da92f276 56 self.PiSpecVersion = '0x00000000'\r
4234283c 57 self.InfModule = None\r
b36d134f 58 self.FinalTargetSuffixMap = {}\r
79b74a03
LG
59 self.CurrentLineNum = None\r
60 self.CurrentLineContent = None\r
61 self.FileName = None\r
62 self.InfFileName = None\r
4234283c 63\r
b36d134f 64 ## GetFinalTargetSuffixMap() method\r
4234283c
LG
65 #\r
66 # Get final build target list\r
b36d134f 67 def GetFinalTargetSuffixMap(self):\r
4234283c
LG
68 if not self.InfModule or not self.CurrentArch:\r
69 return []\r
b36d134f
LG
70 if not self.FinalTargetSuffixMap:\r
71 FinalBuildTargetList = GenFdsGlobalVariable.GetModuleCodaTargetList(self.InfModule, self.CurrentArch)\r
72 for File in FinalBuildTargetList:\r
73 self.FinalTargetSuffixMap.setdefault(os.path.splitext(File)[1], []).append(File)\r
74\r
75 # Check if current INF module has DEPEX\r
76 if '.depex' not in self.FinalTargetSuffixMap and self.InfModule.ModuleType != "USER_DEFINED" \\r
77 and not self.InfModule.DxsFile and not self.InfModule.LibraryClass:\r
78 ModuleType = self.InfModule.ModuleType\r
79 PlatformDataBase = GenFdsGlobalVariable.WorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, self.CurrentArch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
80\r
81 if ModuleType != DataType.SUP_MODULE_USER_DEFINED:\r
82 for LibraryClass in PlatformDataBase.LibraryClasses.GetKeys():\r
83 if LibraryClass.startswith("NULL") and PlatformDataBase.LibraryClasses[LibraryClass, ModuleType]:\r
84 self.InfModule.LibraryClasses[LibraryClass] = PlatformDataBase.LibraryClasses[LibraryClass, ModuleType]\r
85\r
86 StrModule = str(self.InfModule)\r
87 PlatformModule = None\r
88 if StrModule in PlatformDataBase.Modules:\r
89 PlatformModule = PlatformDataBase.Modules[StrModule]\r
90 for LibraryClass in PlatformModule.LibraryClasses:\r
91 if LibraryClass.startswith("NULL"):\r
92 self.InfModule.LibraryClasses[LibraryClass] = PlatformModule.LibraryClasses[LibraryClass]\r
93\r
94 DependencyList = [self.InfModule]\r
95 LibraryInstance = {}\r
96 DepexList = []\r
97 while len(DependencyList) > 0:\r
98 Module = DependencyList.pop(0)\r
99 if not Module:\r
100 continue\r
101 for Dep in Module.Depex[self.CurrentArch, ModuleType]:\r
102 if DepexList != []:\r
103 DepexList.append('AND')\r
104 DepexList.append('(')\r
105 DepexList.extend(Dep)\r
106 if DepexList[-1] == 'END': # no need of a END at this time\r
107 DepexList.pop()\r
108 DepexList.append(')')\r
109 if 'BEFORE' in DepexList or 'AFTER' in DepexList:\r
110 break\r
111 for LibName in Module.LibraryClasses:\r
112 if LibName in LibraryInstance:\r
113 continue\r
114 if PlatformModule and LibName in PlatformModule.LibraryClasses:\r
115 LibraryPath = PlatformModule.LibraryClasses[LibName]\r
116 else:\r
117 LibraryPath = PlatformDataBase.LibraryClasses[LibName, ModuleType]\r
118 if not LibraryPath:\r
119 LibraryPath = Module.LibraryClasses[LibName]\r
120 if not LibraryPath:\r
121 continue\r
122 LibraryModule = GenFdsGlobalVariable.WorkSpace.BuildObject[LibraryPath, self.CurrentArch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
123 LibraryInstance[LibName] = LibraryModule\r
124 DependencyList.append(LibraryModule)\r
125 if DepexList:\r
126 Dpx = DependencyExpression(DepexList, ModuleType, True)\r
127 if len(Dpx.PostfixNotation) != 0:\r
128 # It means this module has DEPEX\r
129 self.FinalTargetSuffixMap['.depex'] = [os.path.join(self.EfiOutputPath, self.BaseName) + '.depex']\r
130 return self.FinalTargetSuffixMap\r
4234283c 131\r
30fdf114
LG
132 ## __InfParse() method\r
133 #\r
134 # Parse inf file to get module information\r
135 #\r
136 # @param self The object pointer\r
137 # @param Dict dictionary contains macro and value pair\r
138 #\r
139 def __InfParse__(self, Dict = {}):\r
140\r
141 GenFdsGlobalVariable.VerboseLogger( " Begine parsing INf file : %s" %self.InfFileName)\r
142\r
143 self.InfFileName = self.InfFileName.replace('$(WORKSPACE)', '')\r
144 if self.InfFileName[0] == '\\' or self.InfFileName[0] == '/' :\r
145 self.InfFileName = self.InfFileName[1:]\r
146\r
147 if self.InfFileName.find('$') == -1:\r
148 InfPath = NormPath(self.InfFileName)\r
149 if not os.path.exists(InfPath):\r
150 InfPath = GenFdsGlobalVariable.ReplaceWorkspaceMacro(InfPath)\r
151 if not os.path.exists(InfPath):\r
152 EdkLogger.error("GenFds", GENFDS_ERROR, "Non-existant Module %s !" % (self.InfFileName))\r
153\r
154 self.CurrentArch = self.GetCurrentArch()\r
155 #\r
156 # Get the InfClass object\r
157 #\r
158\r
159 PathClassObj = PathClass(self.InfFileName, GenFdsGlobalVariable.WorkSpaceDir)\r
e56468c0 160 ErrorCode, ErrorInfo = PathClassObj.Validate(".inf")\r
30fdf114
LG
161 if ErrorCode != 0:\r
162 EdkLogger.error("GenFds", ErrorCode, ExtraData=ErrorInfo)\r
163 \r
164 if self.CurrentArch != None:\r
165\r
0d2711a6 166 Inf = GenFdsGlobalVariable.WorkSpace.BuildObject[PathClassObj, self.CurrentArch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
30fdf114
LG
167 #\r
168 # Set Ffs BaseName, MdouleGuid, ModuleType, Version, OutputPath\r
169 #\r
170 self.BaseName = Inf.BaseName\r
171 self.ModuleGuid = Inf.Guid\r
172 self.ModuleType = Inf.ModuleType\r
52302d4d 173 if Inf.Specification != None and 'PI_SPECIFICATION_VERSION' in Inf.Specification:\r
b303ea72 174 self.PiSpecVersion = Inf.Specification['PI_SPECIFICATION_VERSION']\r
30fdf114
LG
175 if Inf.AutoGenVersion < 0x00010005:\r
176 self.ModuleType = Inf.ComponentType\r
177 self.VersionString = Inf.Version\r
178 self.BinFileList = Inf.Binaries\r
179 self.SourceFileList = Inf.Sources\r
180 if self.KeepReloc == None and Inf.Shadow:\r
181 self.ShadowFromInfFile = Inf.Shadow\r
182\r
183 else:\r
0d2711a6 184 Inf = GenFdsGlobalVariable.WorkSpace.BuildObject[PathClassObj, 'COMMON', GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
30fdf114
LG
185 self.BaseName = Inf.BaseName\r
186 self.ModuleGuid = Inf.Guid\r
187 self.ModuleType = Inf.ModuleType\r
52302d4d 188 if Inf.Specification != None and 'PI_SPECIFICATION_VERSION' in Inf.Specification:\r
b303ea72 189 self.PiSpecVersion = Inf.Specification['PI_SPECIFICATION_VERSION']\r
30fdf114
LG
190 self.VersionString = Inf.Version\r
191 self.BinFileList = Inf.Binaries\r
192 self.SourceFileList = Inf.Sources\r
193 if self.BinFileList == []:\r
194 EdkLogger.error("GenFds", GENFDS_ERROR,\r
195 "INF %s specified in FDF could not be found in build ARCH %s!" \\r
196 % (self.InfFileName, GenFdsGlobalVariable.ArchList))\r
197\r
198 if len(self.SourceFileList) != 0 and not self.InDsc:\r
199 EdkLogger.warn("GenFds", GENFDS_ERROR, "Module %s NOT found in DSC file; Is it really a binary module?" % (self.InfFileName))\r
200\r
da92f276 201 if self.ModuleType == 'SMM_CORE' and int(self.PiSpecVersion, 16) < 0x0001000A:\r
b303ea72
LG
202 EdkLogger.error("GenFds", FORMAT_NOT_SUPPORTED, "SMM_CORE module type can't be used in the module with PI_SPECIFICATION_VERSION less than 0x0001000A", File=self.InfFileName) \r
203\r
30fdf114
LG
204 if Inf._Defs != None and len(Inf._Defs) > 0:\r
205 self.OptRomDefs.update(Inf._Defs)\r
4234283c
LG
206 \r
207 self.InfModule = Inf\r
30fdf114
LG
208 \r
209 GenFdsGlobalVariable.VerboseLogger( "BaseName : %s" %self.BaseName)\r
210 GenFdsGlobalVariable.VerboseLogger("ModuleGuid : %s" %self.ModuleGuid)\r
211 GenFdsGlobalVariable.VerboseLogger("ModuleType : %s" %self.ModuleType)\r
212 GenFdsGlobalVariable.VerboseLogger("VersionString : %s" %self.VersionString)\r
213 GenFdsGlobalVariable.VerboseLogger("InfFileName :%s" %self.InfFileName)\r
214\r
215 #\r
216 # Set OutputPath = ${WorkSpace}\Build\Fv\Ffs\${ModuleGuid}+ ${MdouleName}\\r
217 #\r
218\r
219 self.OutputPath = os.path.join(GenFdsGlobalVariable.FfsDir, \\r
220 self.ModuleGuid + self.BaseName)\r
221 if not os.path.exists(self.OutputPath) :\r
222 os.makedirs(self.OutputPath)\r
223\r
224 self.EfiOutputPath = self.__GetEFIOutPutPath__()\r
225 GenFdsGlobalVariable.VerboseLogger( "ModuelEFIPath: " + self.EfiOutputPath)\r
226\r
227 ## GenFfs() method\r
228 #\r
229 # Generate FFS\r
230 #\r
52302d4d
LG
231 # @param self The object pointer\r
232 # @param Dict dictionary contains macro and value pair\r
233 # @param FvChildAddr Array of the inside FvImage base address\r
234 # @param FvParentAddr Parent Fv base address\r
235 # @retval string Generated FFS file name\r
30fdf114 236 #\r
52302d4d 237 def GenFfs(self, Dict = {}, FvChildAddr = [], FvParentAddr=None):\r
30fdf114
LG
238 #\r
239 # Parse Inf file get Module related information\r
240 #\r
241\r
242 self.__InfParse__(Dict)\r
6780eef1
LG
243 \r
244 #\r
245 # Allow binary type module not specify override rule in FDF file.\r
246 # \r
247 if len(self.BinFileList) >0 and not self.InDsc:\r
248 if self.Rule == None or self.Rule == "":\r
249 self.Rule = "BINARY"\r
250 \r
30fdf114
LG
251 #\r
252 # Get the rule of how to generate Ffs file\r
253 #\r
254 Rule = self.__GetRule__()\r
255 GenFdsGlobalVariable.VerboseLogger( "Packing binaries from inf file : %s" %self.InfFileName)\r
b303ea72
LG
256 #\r
257 # Convert Fv File Type for PI1.1 SMM driver.\r
258 #\r
da92f276 259 if self.ModuleType == 'DXE_SMM_DRIVER' and int(self.PiSpecVersion, 16) >= 0x0001000A:\r
b303ea72
LG
260 if Rule.FvFileType == 'DRIVER':\r
261 Rule.FvFileType = 'SMM'\r
262 #\r
263 # Framework SMM Driver has no SMM FV file type\r
264 #\r
da92f276 265 if self.ModuleType == 'DXE_SMM_DRIVER' and int(self.PiSpecVersion, 16) < 0x0001000A:\r
b303ea72
LG
266 if Rule.FvFileType == 'SMM' or Rule.FvFileType == 'SMM_CORE':\r
267 EdkLogger.error("GenFds", FORMAT_NOT_SUPPORTED, "Framework SMM module doesn't support SMM or SMM_CORE FV file type", File=self.InfFileName)\r
30fdf114
LG
268 #\r
269 # For the rule only has simpleFile\r
270 #\r
271 if isinstance (Rule, RuleSimpleFile.RuleSimpleFile) :\r
272 SectionOutputList = self.__GenSimpleFileSection__(Rule)\r
273 FfsOutput = self.__GenSimpleFileFfs__(Rule, SectionOutputList)\r
274 return FfsOutput\r
275 #\r
276 # For Rule has ComplexFile\r
277 #\r
278 elif isinstance(Rule, RuleComplexFile.RuleComplexFile):\r
52302d4d 279 InputSectList, InputSectAlignments = self.__GenComplexFileSection__(Rule, FvChildAddr, FvParentAddr)\r
30fdf114
LG
280 FfsOutput = self.__GenComplexFileFfs__(Rule, InputSectList, InputSectAlignments)\r
281\r
282 return FfsOutput\r
283\r
284 ## __ExtendMacro__() method\r
285 #\r
286 # Replace macro with its value\r
287 #\r
288 # @param self The object pointer\r
289 # @param String The string to be replaced\r
290 # @retval string Macro replaced string\r
291 #\r
292 def __ExtendMacro__ (self, String):\r
293 MacroDict = {\r
294 '$(INF_OUTPUT)' : self.EfiOutputPath,\r
295 '$(MODULE_NAME)' : self.BaseName,\r
296 '$(BUILD_NUMBER)': self.BuildNum,\r
297 '$(INF_VERSION)' : self.VersionString,\r
298 '$(NAMED_GUID)' : self.ModuleGuid\r
299 }\r
300 String = GenFdsGlobalVariable.MacroExtend(String, MacroDict)\r
301 return String\r
302\r
303 ## __GetRule__() method\r
304 #\r
305 # Get correct rule for generating FFS for this INF\r
306 #\r
307 # @param self The object pointer\r
308 # @retval Rule Rule object\r
309 #\r
310 def __GetRule__ (self) :\r
311 CurrentArchList = []\r
312 if self.CurrentArch == None:\r
313 CurrentArchList = ['common']\r
314 else:\r
315 CurrentArchList.append(self.CurrentArch)\r
316\r
317 for CurrentArch in CurrentArchList:\r
318 RuleName = 'RULE' + \\r
319 '.' + \\r
320 CurrentArch.upper() + \\r
321 '.' + \\r
322 self.ModuleType.upper()\r
323 if self.Rule != None:\r
324 RuleName = RuleName + \\r
325 '.' + \\r
326 self.Rule.upper()\r
327\r
328 Rule = GenFdsGlobalVariable.FdfParser.Profile.RuleDict.get(RuleName)\r
329 if Rule != None:\r
330 GenFdsGlobalVariable.VerboseLogger ("Want To Find Rule Name is : " + RuleName)\r
331 return Rule\r
332\r
333 RuleName = 'RULE' + \\r
334 '.' + \\r
335 'COMMON' + \\r
336 '.' + \\r
337 self.ModuleType.upper()\r
338\r
339 if self.Rule != None:\r
340 RuleName = RuleName + \\r
341 '.' + \\r
342 self.Rule.upper()\r
343\r
344 GenFdsGlobalVariable.VerboseLogger ('Trying to apply common rule %s for INF %s' % (RuleName, self.InfFileName))\r
345\r
346 Rule = GenFdsGlobalVariable.FdfParser.Profile.RuleDict.get(RuleName)\r
347 if Rule != None:\r
348 GenFdsGlobalVariable.VerboseLogger ("Want To Find Rule Name is : " + RuleName)\r
349 return Rule\r
350\r
351 if Rule == None :\r
352 EdkLogger.error("GenFds", GENFDS_ERROR, 'Don\'t Find common rule %s for INF %s' \\r
353 % (RuleName, self.InfFileName))\r
354\r
355 ## __GetPlatformArchList__() method\r
356 #\r
357 # Get Arch list this INF built under\r
358 #\r
359 # @param self The object pointer\r
360 # @retval list Arch list\r
361 #\r
362 def __GetPlatformArchList__(self):\r
363\r
364 InfFileKey = os.path.normpath(os.path.join(GenFdsGlobalVariable.WorkSpaceDir, self.InfFileName))\r
365 DscArchList = []\r
0d2711a6 366 PlatformDataBase = GenFdsGlobalVariable.WorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, 'IA32', GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
30fdf114
LG
367 if PlatformDataBase != None:\r
368 if InfFileKey in PlatformDataBase.Modules:\r
369 DscArchList.append ('IA32')\r
370\r
0d2711a6 371 PlatformDataBase = GenFdsGlobalVariable.WorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, 'X64', GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
30fdf114
LG
372 if PlatformDataBase != None:\r
373 if InfFileKey in PlatformDataBase.Modules:\r
374 DscArchList.append ('X64')\r
375\r
0d2711a6 376 PlatformDataBase = GenFdsGlobalVariable.WorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, 'IPF', GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
30fdf114
LG
377 if PlatformDataBase != None:\r
378 if InfFileKey in (PlatformDataBase.Modules):\r
379 DscArchList.append ('IPF')\r
380\r
0d2711a6 381 PlatformDataBase = GenFdsGlobalVariable.WorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, 'ARM', GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
30fdf114
LG
382 if PlatformDataBase != None:\r
383 if InfFileKey in (PlatformDataBase.Modules):\r
384 DscArchList.append ('ARM')\r
385\r
0d2711a6 386 PlatformDataBase = GenFdsGlobalVariable.WorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, 'EBC', GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
30fdf114
LG
387 if PlatformDataBase != None:\r
388 if InfFileKey in (PlatformDataBase.Modules):\r
389 DscArchList.append ('EBC')\r
390\r
4afd3d04
LG
391 PlatformDataBase = GenFdsGlobalVariable.WorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, 'AARCH64', GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
392 if PlatformDataBase != None:\r
393 if InfFileKey in (PlatformDataBase.Modules):\r
394 DscArchList.append ('AARCH64')\r
395\r
30fdf114
LG
396 return DscArchList\r
397\r
398 ## GetCurrentArch() method\r
399 #\r
400 # Get Arch list of the module from this INF is to be placed into flash\r
401 #\r
402 # @param self The object pointer\r
403 # @retval list Arch list\r
404 #\r
405 def GetCurrentArch(self) :\r
406\r
407 TargetArchList = GenFdsGlobalVariable.ArchList\r
408\r
409 PlatformArchList = self.__GetPlatformArchList__()\r
410\r
411 CurArchList = TargetArchList\r
412 if PlatformArchList != []:\r
413 CurArchList = list(set (TargetArchList) & set (PlatformArchList))\r
414 GenFdsGlobalVariable.VerboseLogger ("Valid target architecture(s) is : " + " ".join(CurArchList))\r
415\r
416 ArchList = []\r
417 if self.KeyStringList != []:\r
418 for Key in self.KeyStringList:\r
419 Key = GenFdsGlobalVariable.MacroExtend(Key)\r
420 Target, Tag, Arch = Key.split('_')\r
421 if Arch in CurArchList:\r
422 ArchList.append(Arch)\r
423 if Target not in self.TargetOverrideList:\r
424 self.TargetOverrideList.append(Target)\r
425 else:\r
426 ArchList = CurArchList\r
427\r
428 UseArchList = TargetArchList\r
429 if self.UseArch != None:\r
430 UseArchList = []\r
431 UseArchList.append(self.UseArch)\r
432 ArchList = list(set (UseArchList) & set (ArchList))\r
433\r
434 self.InfFileName = NormPath(self.InfFileName)\r
435 if len(PlatformArchList) == 0:\r
436 self.InDsc = False\r
437 PathClassObj = PathClass(self.InfFileName, GenFdsGlobalVariable.WorkSpaceDir)\r
e56468c0 438 ErrorCode, ErrorInfo = PathClassObj.Validate(".inf")\r
30fdf114
LG
439 if ErrorCode != 0:\r
440 EdkLogger.error("GenFds", ErrorCode, ExtraData=ErrorInfo)\r
441 if len(ArchList) == 1:\r
442 Arch = ArchList[0]\r
443 return Arch\r
444 elif len(ArchList) > 1:\r
445 if len(PlatformArchList) == 0:\r
446 EdkLogger.error("GenFds", GENFDS_ERROR, "GenFds command line option has multiple ARCHs %s. Not able to determine which ARCH is valid for Module %s !" % (str(ArchList), self.InfFileName))\r
447 else:\r
448 EdkLogger.error("GenFds", GENFDS_ERROR, "Module built under multiple ARCHs %s. Not able to determine which output to put into flash for Module %s !" % (str(ArchList), self.InfFileName))\r
449 else:\r
450 EdkLogger.error("GenFds", GENFDS_ERROR, "Module %s appears under ARCH %s in platform %s, but current deduced ARCH is %s, so NO build output could be put into flash." \\r
451 % (self.InfFileName, str(PlatformArchList), GenFdsGlobalVariable.ActivePlatform, str(set (UseArchList) & set (TargetArchList))))\r
452\r
453 ## __GetEFIOutPutPath__() method\r
454 #\r
455 # Get the output path for generated files\r
456 #\r
457 # @param self The object pointer\r
458 # @retval string Path that output files from this INF go to\r
459 #\r
460 def __GetEFIOutPutPath__(self):\r
461 Arch = ''\r
462 OutputPath = ''\r
463 (ModulePath, FileName) = os.path.split(self.InfFileName)\r
79b74a03 464 Index = FileName.rfind('.')\r
30fdf114
LG
465 FileName = FileName[0:Index]\r
466 Arch = "NoneArch"\r
467 if self.CurrentArch != None:\r
468 Arch = self.CurrentArch\r
469\r
470 OutputPath = os.path.join(GenFdsGlobalVariable.OutputDirDict[Arch],\r
471 Arch ,\r
472 ModulePath,\r
473 FileName,\r
474 'OUTPUT'\r
475 )\r
476 OutputPath = os.path.realpath(OutputPath)\r
477 return OutputPath\r
478\r
479 ## __GenSimpleFileSection__() method\r
480 #\r
481 # Generate section by specified file name or a list of files with file extension\r
482 #\r
483 # @param self The object pointer\r
484 # @param Rule The rule object used to generate section\r
485 # @retval string File name of the generated section file\r
486 #\r
487 def __GenSimpleFileSection__(self, Rule):\r
488 #\r
489 # Prepare the parameter of GenSection\r
490 #\r
491 FileList = []\r
492 OutputFileList = []\r
52302d4d 493 GenSecInputFile = None\r
30fdf114
LG
494 if Rule.FileName != None:\r
495 GenSecInputFile = self.__ExtendMacro__(Rule.FileName)\r
da92f276
LG
496 if os.path.isabs(GenSecInputFile):\r
497 GenSecInputFile = os.path.normpath(GenSecInputFile)\r
498 else:\r
52302d4d 499 GenSecInputFile = os.path.normpath(os.path.join(self.EfiOutputPath, GenSecInputFile))\r
30fdf114
LG
500 else:\r
501 FileList, IsSect = Section.Section.GetFileList(self, '', Rule.FileExtension)\r
502\r
503 Index = 1\r
b303ea72
LG
504 SectionType = Rule.SectionType\r
505 #\r
506 # Convert Fv Section Type for PI1.1 SMM driver.\r
507 #\r
da92f276 508 if self.ModuleType == 'DXE_SMM_DRIVER' and int(self.PiSpecVersion, 16) >= 0x0001000A:\r
b303ea72
LG
509 if SectionType == 'DXE_DEPEX':\r
510 SectionType = 'SMM_DEPEX'\r
511 #\r
512 # Framework SMM Driver has no SMM_DEPEX section type\r
513 #\r
da92f276 514 if self.ModuleType == 'DXE_SMM_DRIVER' and int(self.PiSpecVersion, 16) < 0x0001000A:\r
b303ea72
LG
515 if SectionType == 'SMM_DEPEX':\r
516 EdkLogger.error("GenFds", FORMAT_NOT_SUPPORTED, "Framework SMM module doesn't support SMM_DEPEX section type", File=self.InfFileName)\r
30fdf114
LG
517 NoStrip = True\r
518 if self.ModuleType in ('SEC', 'PEI_CORE', 'PEIM'):\r
519 if self.KeepReloc != None:\r
520 NoStrip = self.KeepReloc\r
521 elif Rule.KeepReloc != None:\r
522 NoStrip = Rule.KeepReloc\r
523 elif self.ShadowFromInfFile != None:\r
524 NoStrip = self.ShadowFromInfFile\r
525\r
526 if FileList != [] :\r
527 for File in FileList:\r
528\r
529 SecNum = '%d' %Index\r
530 GenSecOutputFile= self.__ExtendMacro__(Rule.NameGuid) + \\r
531 Ffs.Ffs.SectionSuffix[SectionType] + 'SEC' + SecNum\r
532 Index = Index + 1\r
533 OutputFile = os.path.join(self.OutputPath, GenSecOutputFile)\r
52302d4d
LG
534 File = GenFdsGlobalVariable.MacroExtend(File, Dict, self.CurrentArch)\r
535\r
536 #Get PE Section alignment when align is set to AUTO\r
537 if self.Alignment == 'Auto' and (SectionType == 'PE32' or SectionType == 'TE'):\r
538 ImageObj = PeImageClass (File)\r
539 if ImageObj.SectionAlignment < 0x400:\r
540 self.Alignment = str (ImageObj.SectionAlignment)\r
541 else:\r
542 self.Alignment = str (ImageObj.SectionAlignment / 0x400) + 'K'\r
30fdf114
LG
543\r
544 if not NoStrip:\r
545 FileBeforeStrip = os.path.join(self.OutputPath, ModuleName + '.reloc')\r
546 if not os.path.exists(FileBeforeStrip) or \\r
547 (os.path.getmtime(File) > os.path.getmtime(FileBeforeStrip)):\r
548 shutil.copyfile(File, FileBeforeStrip)\r
549 StrippedFile = os.path.join(self.OutputPath, ModuleName + '.stipped')\r
550 GenFdsGlobalVariable.GenerateFirmwareImage(\r
551 StrippedFile,\r
52302d4d 552 [File],\r
30fdf114
LG
553 Strip=True\r
554 )\r
555 File = StrippedFile\r
556\r
557 if SectionType == 'TE':\r
558 TeFile = os.path.join( self.OutputPath, self.ModuleGuid + 'Te.raw')\r
559 GenFdsGlobalVariable.GenerateFirmwareImage(\r
560 TeFile,\r
52302d4d 561 [File],\r
30fdf114
LG
562 Type='te'\r
563 )\r
564 File = TeFile\r
565\r
566 GenFdsGlobalVariable.GenerateSection(OutputFile, [File], Section.Section.SectionType[SectionType])\r
567 OutputFileList.append(OutputFile)\r
568 else:\r
569 SecNum = '%d' %Index\r
570 GenSecOutputFile= self.__ExtendMacro__(Rule.NameGuid) + \\r
571 Ffs.Ffs.SectionSuffix[SectionType] + 'SEC' + SecNum\r
572 OutputFile = os.path.join(self.OutputPath, GenSecOutputFile)\r
52302d4d
LG
573 GenSecInputFile = GenFdsGlobalVariable.MacroExtend(GenSecInputFile, Dict, self.CurrentArch)\r
574\r
575 #Get PE Section alignment when align is set to AUTO\r
576 if self.Alignment == 'Auto' and (SectionType == 'PE32' or SectionType == 'TE'):\r
577 ImageObj = PeImageClass (GenSecInputFile)\r
578 if ImageObj.SectionAlignment < 0x400:\r
579 self.Alignment = str (ImageObj.SectionAlignment)\r
580 else:\r
581 self.Alignment = str (ImageObj.SectionAlignment / 0x400) + 'K'\r
30fdf114
LG
582\r
583 if not NoStrip:\r
584 FileBeforeStrip = os.path.join(self.OutputPath, ModuleName + '.reloc')\r
585 if not os.path.exists(FileBeforeStrip) or \\r
586 (os.path.getmtime(GenSecInputFile) > os.path.getmtime(FileBeforeStrip)):\r
587 shutil.copyfile(GenSecInputFile, FileBeforeStrip)\r
588 StrippedFile = os.path.join(self.OutputPath, ModuleName + '.stipped')\r
589 GenFdsGlobalVariable.GenerateFirmwareImage(\r
590 StrippedFile,\r
52302d4d 591 [GenSecInputFile],\r
30fdf114
LG
592 Strip=True\r
593 )\r
594 GenSecInputFile = StrippedFile\r
595\r
596 if SectionType == 'TE':\r
597 TeFile = os.path.join( self.OutputPath, self.ModuleGuid + 'Te.raw')\r
598 GenFdsGlobalVariable.GenerateFirmwareImage(\r
599 TeFile,\r
52302d4d 600 [GenSecInputFile],\r
30fdf114
LG
601 Type='te'\r
602 )\r
603 GenSecInputFile = TeFile\r
604\r
605 GenFdsGlobalVariable.GenerateSection(OutputFile, [GenSecInputFile], Section.Section.SectionType[SectionType])\r
606 OutputFileList.append(OutputFile)\r
607\r
608 return OutputFileList\r
609\r
610 ## __GenSimpleFileFfs__() method\r
611 #\r
612 # Generate FFS\r
613 #\r
614 # @param self The object pointer\r
615 # @param Rule The rule object used to generate section\r
616 # @param InputFileList The output file list from GenSection\r
617 # @retval string Generated FFS file name\r
618 #\r
619 def __GenSimpleFileFfs__(self, Rule, InputFileList):\r
620 FfsOutput = self.OutputPath + \\r
621 os.sep + \\r
622 self.__ExtendMacro__(Rule.NameGuid) + \\r
623 '.ffs'\r
624\r
625 GenFdsGlobalVariable.VerboseLogger(self.__ExtendMacro__(Rule.NameGuid))\r
626 InputSection = []\r
627 SectionAlignments = []\r
628 for InputFile in InputFileList:\r
629 InputSection.append(InputFile)\r
52302d4d 630 SectionAlignments.append(Rule.SectAlignment)\r
30fdf114
LG
631\r
632 if Rule.NameGuid != None and Rule.NameGuid.startswith('PCD('):\r
633 PcdValue = GenFdsGlobalVariable.GetPcdValue(Rule.NameGuid)\r
634 if len(PcdValue) == 0:\r
635 EdkLogger.error("GenFds", GENFDS_ERROR, '%s NOT defined.' \\r
636 % (Rule.NameGuid))\r
637 if PcdValue.startswith('{'):\r
638 PcdValue = GuidStructureByteArrayToGuidString(PcdValue)\r
639 RegistryGuidStr = PcdValue\r
640 if len(RegistryGuidStr) == 0:\r
641 EdkLogger.error("GenFds", GENFDS_ERROR, 'GUID value for %s in wrong format.' \\r
642 % (Rule.NameGuid))\r
643 self.ModuleGuid = RegistryGuidStr\r
644\r
645 GenFdsGlobalVariable.GenerateFfs(FfsOutput, InputSection,\r
646 Ffs.Ffs.FdfFvFileTypeToFileType[Rule.FvFileType],\r
647 self.ModuleGuid, Fixed=Rule.Fixed,\r
648 CheckSum=Rule.CheckSum, Align=Rule.Alignment,\r
649 SectionAlign=SectionAlignments\r
650 )\r
651 return FfsOutput\r
652\r
653 ## __GenComplexFileSection__() method\r
654 #\r
655 # Generate section by sections in Rule\r
656 #\r
52302d4d
LG
657 # @param self The object pointer\r
658 # @param Rule The rule object used to generate section\r
659 # @param FvChildAddr Array of the inside FvImage base address\r
660 # @param FvParentAddr Parent Fv base address\r
661 # @retval string File name of the generated section file\r
30fdf114 662 #\r
52302d4d 663 def __GenComplexFileSection__(self, Rule, FvChildAddr, FvParentAddr):\r
30fdf114
LG
664 if self.ModuleType in ('SEC', 'PEI_CORE', 'PEIM'):\r
665 if Rule.KeepReloc != None:\r
666 self.KeepRelocFromRule = Rule.KeepReloc\r
667 SectFiles = []\r
668 SectAlignments = []\r
669 Index = 1\r
da92f276 670 HasGneratedFlag = False\r
30fdf114
LG
671 for Sect in Rule.SectionList:\r
672 SecIndex = '%d' %Index\r
673 SectList = []\r
b303ea72
LG
674 #\r
675 # Convert Fv Section Type for PI1.1 SMM driver.\r
676 #\r
da92f276 677 if self.ModuleType == 'DXE_SMM_DRIVER' and int(self.PiSpecVersion, 16) >= 0x0001000A:\r
b303ea72
LG
678 if Sect.SectionType == 'DXE_DEPEX':\r
679 Sect.SectionType = 'SMM_DEPEX'\r
680 #\r
681 # Framework SMM Driver has no SMM_DEPEX section type\r
682 #\r
da92f276 683 if self.ModuleType == 'DXE_SMM_DRIVER' and int(self.PiSpecVersion, 16) < 0x0001000A:\r
b303ea72
LG
684 if Sect.SectionType == 'SMM_DEPEX':\r
685 EdkLogger.error("GenFds", FORMAT_NOT_SUPPORTED, "Framework SMM module doesn't support SMM_DEPEX section type", File=self.InfFileName)\r
52302d4d
LG
686 #\r
687 # process the inside FvImage from FvSection or GuidSection\r
688 #\r
689 if FvChildAddr != []:\r
690 if isinstance(Sect, FvImageSection):\r
691 Sect.FvAddr = FvChildAddr.pop(0)\r
692 elif isinstance(Sect, GuidSection):\r
693 Sect.FvAddr = FvChildAddr\r
694 if FvParentAddr != None and isinstance(Sect, GuidSection):\r
695 Sect.FvParentAddr = FvParentAddr\r
696 \r
30fdf114
LG
697 if Rule.KeyStringList != []:\r
698 SectList, Align = Sect.GenSection(self.OutputPath , self.ModuleGuid, SecIndex, Rule.KeyStringList, self)\r
699 else :\r
700 SectList, Align = Sect.GenSection(self.OutputPath , self.ModuleGuid, SecIndex, self.KeyStringList, self)\r
da92f276
LG
701 \r
702 if not HasGneratedFlag:\r
703 UniVfrOffsetFileSection = "" \r
704 ModuleFileName = os.path.join(GenFdsGlobalVariable.WorkSpaceDir, self.InfFileName)\r
705 InfData = GenFdsGlobalVariable.WorkSpace.BuildObject[PathClass(ModuleFileName), self.CurrentArch]\r
706 #\r
707 # Search the source list in InfData to find if there are .vfr file exist.\r
708 #\r
709 VfrUniBaseName = {}\r
710 VfrUniOffsetList = []\r
711 for SourceFile in InfData.Sources:\r
712 if SourceFile.Type.upper() == ".VFR" :\r
713 #\r
714 # search the .map file to find the offset of vfr binary in the PE32+/TE file. \r
715 #\r
716 VfrUniBaseName[SourceFile.BaseName] = (SourceFile.BaseName + "Bin")\r
717 if SourceFile.Type.upper() == ".UNI" :\r
718 #\r
719 # search the .map file to find the offset of Uni strings binary in the PE32+/TE file. \r
720 #\r
721 VfrUniBaseName["UniOffsetName"] = (self.BaseName + "Strings")\r
722 \r
723 \r
724 if len(VfrUniBaseName) > 0:\r
725 VfrUniOffsetList = self.__GetBuildOutputMapFileVfrUniInfo(VfrUniBaseName)\r
726 #\r
727 # Generate the Raw data of raw section\r
728 #\r
729 os.path.join( self.OutputPath, self.BaseName + '.offset')\r
730 UniVfrOffsetFileName = os.path.join( self.OutputPath, self.BaseName + '.offset')\r
731 UniVfrOffsetFileSection = os.path.join( self.OutputPath, self.BaseName + 'Offset' + '.raw')\r
732 \r
733 self.__GenUniVfrOffsetFile (VfrUniOffsetList, UniVfrOffsetFileName)\r
734 \r
735 UniVfrOffsetFileNameList = []\r
736 UniVfrOffsetFileNameList.append(UniVfrOffsetFileName)\r
737 """Call GenSection"""\r
738 GenFdsGlobalVariable.GenerateSection(UniVfrOffsetFileSection,\r
739 UniVfrOffsetFileNameList,\r
740 "EFI_SECTION_RAW"\r
741 )\r
742 os.remove(UniVfrOffsetFileName) \r
743 SectList.append(UniVfrOffsetFileSection)\r
744 HasGneratedFlag = True\r
745 \r
30fdf114
LG
746 for SecName in SectList :\r
747 SectFiles.append(SecName)\r
748 SectAlignments.append(Align)\r
749 Index = Index + 1\r
750 return SectFiles, SectAlignments\r
751\r
752 ## __GenComplexFileFfs__() method\r
753 #\r
754 # Generate FFS\r
755 #\r
756 # @param self The object pointer\r
757 # @param Rule The rule object used to generate section\r
758 # @param InputFileList The output file list from GenSection\r
759 # @retval string Generated FFS file name\r
760 #\r
761 def __GenComplexFileFfs__(self, Rule, InputFile, Alignments):\r
762\r
763 if Rule.NameGuid != None and Rule.NameGuid.startswith('PCD('):\r
764 PcdValue = GenFdsGlobalVariable.GetPcdValue(Rule.NameGuid)\r
765 if len(PcdValue) == 0:\r
766 EdkLogger.error("GenFds", GENFDS_ERROR, '%s NOT defined.' \\r
767 % (Rule.NameGuid))\r
768 if PcdValue.startswith('{'):\r
769 PcdValue = GuidStructureByteArrayToGuidString(PcdValue)\r
770 RegistryGuidStr = PcdValue\r
771 if len(RegistryGuidStr) == 0:\r
772 EdkLogger.error("GenFds", GENFDS_ERROR, 'GUID value for %s in wrong format.' \\r
773 % (Rule.NameGuid))\r
774 self.ModuleGuid = RegistryGuidStr\r
775\r
776 FfsOutput = os.path.join( self.OutputPath, self.ModuleGuid + '.ffs')\r
777 GenFdsGlobalVariable.GenerateFfs(FfsOutput, InputFile,\r
778 Ffs.Ffs.FdfFvFileTypeToFileType[Rule.FvFileType],\r
779 self.ModuleGuid, Fixed=Rule.Fixed,\r
780 CheckSum=Rule.CheckSum, Align=Rule.Alignment,\r
781 SectionAlign=Alignments\r
782 )\r
783 return FfsOutput\r
784\r
785 ## __GetGenFfsCmdParameter__() method\r
786 #\r
787 # Create parameter string for GenFfs\r
788 #\r
789 # @param self The object pointer\r
790 # @param Rule The rule object used to generate section\r
791 # @retval tuple (FileType, Fixed, CheckSum, Alignment)\r
792 #\r
793 def __GetGenFfsCmdParameter__(self, Rule):\r
794 result = tuple()\r
795 result += ('-t', Ffs.Ffs.FdfFvFileTypeToFileType[Rule.FvFileType])\r
796 if Rule.Fixed != False:\r
797 result += ('-x',)\r
798 if Rule.CheckSum != False:\r
799 result += ('-s',)\r
800\r
801 if Rule.Alignment != None and Rule.Alignment != '':\r
802 result += ('-a', Rule.Alignment)\r
803\r
804 return result\r
da92f276
LG
805 \r
806 ## __GetBuildOutputMapFileVfrUniInfo() method\r
807 #\r
808 # Find the offset of UNI/INF object offset in the EFI image file.\r
809 #\r
810 # @param self The object pointer\r
811 # @param VfrUniBaseName A name list contain the UNI/INF object name.\r
812 # @retval RetValue A list contain offset of UNI/INF object.\r
813 # \r
814 def __GetBuildOutputMapFileVfrUniInfo(self, VfrUniBaseName):\r
815 \r
816 RetValue = []\r
817 \r
818 MapFileName = os.path.join(self.EfiOutputPath, self.BaseName + ".map")\r
819 try:\r
820 fInputfile = open(MapFileName, "r", 0)\r
821 try:\r
822 FileLinesList = fInputfile.readlines()\r
823 except:\r
824 EdkLogger.error("GenFds", FILE_READ_FAILURE, "File read failed for %s" %MapFileName,None)\r
825 finally:\r
826 fInputfile.close()\r
827 except:\r
828 EdkLogger.error("GenFds", FILE_OPEN_FAILURE, "File open failed for %s" %MapFileName,None)\r
829 \r
830 IsHex = False\r
831 for eachLine in FileLinesList:\r
832 for eachName in VfrUniBaseName.values():\r
833 if eachLine.find(eachName) != -1:\r
834 eachLine = eachLine.strip()\r
835 Element = eachLine.split()\r
836 #\r
837 # MSFT/ICC/EBC map file\r
838 #\r
839 if (len(Element) == 4):\r
840 try:\r
841 int (Element[2], 16)\r
842 IsHex = True\r
843 except:\r
844 IsHex = False\r
845 \r
846 if IsHex:\r
847 RetValue.append((eachName, Element[2]))\r
848 IsHex = False\r
849 #\r
850 # GCC map file\r
851 #\r
852 elif (len(Element) == 2) and Element[0].startswith("0x"):\r
853 RetValue.append((eachName, Element[0]))\r
854 \r
855 return RetValue\r
856 \r
857 ## __GenUniVfrOffsetFile() method\r
858 #\r
859 # Generate the offset file for the module which contain VFR or UNI file.\r
860 #\r
861 # @param self The object pointer\r
862 # @param VfrUniOffsetList A list contain the VFR/UNI offsets in the EFI image file.\r
863 # @param UniVfrOffsetFileName The output offset file name.\r
864 #\r
865 def __GenUniVfrOffsetFile(self, VfrUniOffsetList, UniVfrOffsetFileName):\r
866 \r
867 try:\r
868 fInputfile = open(UniVfrOffsetFileName, "wb+", 0)\r
869 except:\r
870 EdkLogger.error("GenFds", FILE_OPEN_FAILURE, "File open failed for %s" %UniVfrOffsetFileName,None)\r
871 \r
872 # Use a instance of StringIO to cache data\r
873 fStringIO = StringIO.StringIO('') \r
874 \r
875 for Item in VfrUniOffsetList:\r
876 if (Item[0].find("Strings") != -1):\r
877 #\r
878 # UNI offset in image.\r
879 # GUID + Offset\r
880 # { 0x8913c5e0, 0x33f6, 0x4d86, { 0x9b, 0xf1, 0x43, 0xef, 0x89, 0xfc, 0x6, 0x66 } }\r
881 #\r
882 UniGuid = [0xe0, 0xc5, 0x13, 0x89, 0xf6, 0x33, 0x86, 0x4d, 0x9b, 0xf1, 0x43, 0xef, 0x89, 0xfc, 0x6, 0x66]\r
883 UniGuid = [chr(ItemGuid) for ItemGuid in UniGuid]\r
884 fStringIO.write(''.join(UniGuid)) \r
885 UniValue = pack ('Q', int (Item[1], 16))\r
886 fStringIO.write (UniValue)\r
887 else:\r
888 #\r
889 # VFR binary offset in image.\r
890 # GUID + Offset\r
891 # { 0xd0bc7cb4, 0x6a47, 0x495f, { 0xaa, 0x11, 0x71, 0x7, 0x46, 0xda, 0x6, 0xa2 } };\r
892 #\r
893 VfrGuid = [0xb4, 0x7c, 0xbc, 0xd0, 0x47, 0x6a, 0x5f, 0x49, 0xaa, 0x11, 0x71, 0x7, 0x46, 0xda, 0x6, 0xa2]\r
894 VfrGuid = [chr(ItemGuid) for ItemGuid in VfrGuid]\r
895 fStringIO.write(''.join(VfrGuid)) \r
896 type (Item[1]) \r
897 VfrValue = pack ('Q', int (Item[1], 16))\r
898 fStringIO.write (VfrValue)\r
899 \r
900 #\r
901 # write data into file.\r
902 #\r
903 try : \r
904 fInputfile.write (fStringIO.getvalue())\r
905 except:\r
906 EdkLogger.error("GenFds", FILE_WRITE_FAILURE, "Write data to file %s failed, please check whether the file been locked or using by other applications." %UniVfrOffsetFileName,None)\r
907 \r
908 fStringIO.close ()\r
909 fInputfile.close ()\r
910 \r
911 \r
912 \r
913 \r
914 \r
915 \r
916 \r
917 \r