]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/GenFds/GenFdsGlobalVariable.py
ShellPkg/dh: Refine the dump output
[mirror_edk2.git] / BaseTools / Source / Python / GenFds / GenFdsGlobalVariable.py
CommitLineData
f51461c8
LG
1## @file\r
2# Global variables for GenFds\r
3#\r
213ae077 4# Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.<BR>\r
f51461c8
LG
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
1be2ed90 18import Common.LongFilePathOs as os\r
f51461c8
LG
19import sys\r
20import subprocess\r
21import struct\r
22import array\r
23\r
24from Common.BuildToolError import *\r
25from Common import EdkLogger\r
26from Common.Misc import SaveFileOnChange\r
27\r
28from Common.TargetTxtClassObject import TargetTxtClassObject\r
29from Common.ToolDefClassObject import ToolDefClassObject\r
30from AutoGen.BuildEngine import BuildRule\r
31import Common.DataType as DataType\r
32from Common.Misc import PathClass\r
1be2ed90 33from Common.LongFilePathSupport import OpenLongFilePath as open\r
05cc51ad 34from Common.MultipleWorkspace import MultipleWorkspace as mws\r
f51461c8
LG
35\r
36## Global variables\r
37#\r
38#\r
39class GenFdsGlobalVariable:\r
40 FvDir = ''\r
41 OutputDirDict = {}\r
42 BinDir = ''\r
43 # will be FvDir + os.sep + 'Ffs'\r
44 FfsDir = ''\r
45 FdfParser = None\r
46 LibDir = ''\r
47 WorkSpace = None\r
48 WorkSpaceDir = ''\r
97fa0ee9 49 ConfDir = ''\r
f51461c8
LG
50 EdkSourceDir = ''\r
51 OutputDirFromDscDict = {}\r
52 TargetName = ''\r
53 ToolChainTag = ''\r
54 RuleDict = {}\r
55 ArchList = None\r
56 VtfDict = {}\r
57 ActivePlatform = None\r
58 FvAddressFileName = ''\r
59 VerboseMode = False\r
60 DebugLevel = -1\r
61 SharpCounter = 0\r
62 SharpNumberPerLine = 40\r
63 FdfFile = ''\r
64 FdfFileTimeStamp = 0\r
65 FixedLoadAddress = False\r
66 PlatformName = ''\r
67 \r
68 BuildRuleFamily = "MSFT"\r
69 ToolChainFamily = "MSFT"\r
70 __BuildRuleDatabase = None\r
213ae077 71 GuidToolDefinition = {}\r
f51461c8
LG
72 \r
73 #\r
74 # The list whose element are flags to indicate if large FFS or SECTION files exist in FV.\r
75 # At the beginning of each generation of FV, false flag is appended to the list,\r
76 # after the call to GenerateSection returns, check the size of the output file,\r
77 # if it is greater than 0xFFFFFF, the tail flag in list is set to true,\r
78 # and EFI_FIRMWARE_FILE_SYSTEM3_GUID is passed to C GenFv.\r
79 # At the end of generation of FV, pop the flag.\r
80 # List is used as a stack to handle nested FV generation.\r
81 #\r
82 LargeFileInFvFlags = []\r
83 EFI_FIRMWARE_FILE_SYSTEM3_GUID = '5473C07A-3DCB-4dca-BD6F-1E9689E7349A'\r
84 LARGE_FILE_SIZE = 0x1000000\r
85\r
86 SectionHeader = struct.Struct("3B 1B")\r
87 \r
88 ## LoadBuildRule\r
89 #\r
90 @staticmethod\r
91 def __LoadBuildRule():\r
92 if GenFdsGlobalVariable.__BuildRuleDatabase:\r
93 return GenFdsGlobalVariable.__BuildRuleDatabase\r
97fa0ee9 94 BuildConfigurationFile = os.path.normpath(os.path.join(GenFdsGlobalVariable.ConfDir, "target.txt"))\r
f51461c8
LG
95 TargetTxt = TargetTxtClassObject()\r
96 if os.path.isfile(BuildConfigurationFile) == True:\r
97 TargetTxt.LoadTargetTxtFile(BuildConfigurationFile)\r
98 if DataType.TAB_TAT_DEFINES_BUILD_RULE_CONF in TargetTxt.TargetTxtDictionary:\r
99 BuildRuleFile = TargetTxt.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_BUILD_RULE_CONF]\r
100 if BuildRuleFile in [None, '']:\r
101 BuildRuleFile = 'Conf/build_rule.txt'\r
102 GenFdsGlobalVariable.__BuildRuleDatabase = BuildRule(BuildRuleFile)\r
103 ToolDefinitionFile = TargetTxt.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_TOOL_CHAIN_CONF]\r
104 if ToolDefinitionFile == '':\r
105 ToolDefinitionFile = "Conf/tools_def.txt"\r
106 if os.path.isfile(ToolDefinitionFile):\r
107 ToolDef = ToolDefClassObject()\r
108 ToolDef.LoadToolDefFile(ToolDefinitionFile)\r
109 ToolDefinition = ToolDef.ToolsDefTxtDatabase\r
110 if DataType.TAB_TOD_DEFINES_BUILDRULEFAMILY in ToolDefinition \\r
111 and GenFdsGlobalVariable.ToolChainTag in ToolDefinition[DataType.TAB_TOD_DEFINES_BUILDRULEFAMILY] \\r
112 and ToolDefinition[DataType.TAB_TOD_DEFINES_BUILDRULEFAMILY][GenFdsGlobalVariable.ToolChainTag]:\r
113 GenFdsGlobalVariable.BuildRuleFamily = ToolDefinition[DataType.TAB_TOD_DEFINES_BUILDRULEFAMILY][GenFdsGlobalVariable.ToolChainTag]\r
114 \r
115 if DataType.TAB_TOD_DEFINES_FAMILY in ToolDefinition \\r
116 and GenFdsGlobalVariable.ToolChainTag in ToolDefinition[DataType.TAB_TOD_DEFINES_FAMILY] \\r
117 and ToolDefinition[DataType.TAB_TOD_DEFINES_FAMILY][GenFdsGlobalVariable.ToolChainTag]:\r
118 GenFdsGlobalVariable.ToolChainFamily = ToolDefinition[DataType.TAB_TOD_DEFINES_FAMILY][GenFdsGlobalVariable.ToolChainTag]\r
119 return GenFdsGlobalVariable.__BuildRuleDatabase\r
120\r
121 ## GetBuildRules\r
122 # @param Inf: object of InfBuildData\r
123 # @param Arch: current arch\r
124 #\r
125 @staticmethod\r
126 def GetBuildRules(Inf, Arch):\r
127 if not Arch:\r
128 Arch = 'COMMON'\r
129\r
130 if not Arch in GenFdsGlobalVariable.OutputDirDict:\r
131 return {}\r
132\r
133 BuildRuleDatabase = GenFdsGlobalVariable.__LoadBuildRule()\r
134 if not BuildRuleDatabase:\r
135 return {}\r
136\r
137 PathClassObj = PathClass(Inf.MetaFile.File,\r
138 GenFdsGlobalVariable.WorkSpaceDir)\r
139 Macro = {}\r
140 Macro["WORKSPACE" ] = GenFdsGlobalVariable.WorkSpaceDir\r
141 Macro["MODULE_NAME" ] = Inf.BaseName\r
142 Macro["MODULE_GUID" ] = Inf.Guid\r
143 Macro["MODULE_VERSION" ] = Inf.Version\r
144 Macro["MODULE_TYPE" ] = Inf.ModuleType\r
145 Macro["MODULE_FILE" ] = str(PathClassObj)\r
146 Macro["MODULE_FILE_BASE_NAME" ] = PathClassObj.BaseName\r
147 Macro["MODULE_RELATIVE_DIR" ] = PathClassObj.SubDir\r
148 Macro["MODULE_DIR" ] = PathClassObj.SubDir\r
149\r
150 Macro["BASE_NAME" ] = Inf.BaseName\r
151\r
152 Macro["ARCH" ] = Arch\r
153 Macro["TOOLCHAIN" ] = GenFdsGlobalVariable.ToolChainTag\r
154 Macro["TOOLCHAIN_TAG" ] = GenFdsGlobalVariable.ToolChainTag\r
155 Macro["TOOL_CHAIN_TAG" ] = GenFdsGlobalVariable.ToolChainTag\r
156 Macro["TARGET" ] = GenFdsGlobalVariable.TargetName\r
157\r
158 Macro["BUILD_DIR" ] = GenFdsGlobalVariable.OutputDirDict[Arch]\r
159 Macro["BIN_DIR" ] = os.path.join(GenFdsGlobalVariable.OutputDirDict[Arch], Arch)\r
160 Macro["LIB_DIR" ] = os.path.join(GenFdsGlobalVariable.OutputDirDict[Arch], Arch)\r
161 BuildDir = os.path.join(\r
162 GenFdsGlobalVariable.OutputDirDict[Arch],\r
163 Arch,\r
164 PathClassObj.SubDir,\r
165 PathClassObj.BaseName\r
166 )\r
167 Macro["MODULE_BUILD_DIR" ] = BuildDir\r
168 Macro["OUTPUT_DIR" ] = os.path.join(BuildDir, "OUTPUT")\r
169 Macro["DEBUG_DIR" ] = os.path.join(BuildDir, "DEBUG")\r
170\r
171 BuildRules = {}\r
172 for Type in BuildRuleDatabase.FileTypeList:\r
173 #first try getting build rule by BuildRuleFamily\r
174 RuleObject = BuildRuleDatabase[Type, Inf.BuildType, Arch, GenFdsGlobalVariable.BuildRuleFamily]\r
175 if not RuleObject:\r
176 # build type is always module type, but ...\r
177 if Inf.ModuleType != Inf.BuildType:\r
178 RuleObject = BuildRuleDatabase[Type, Inf.ModuleType, Arch, GenFdsGlobalVariable.BuildRuleFamily]\r
179 #second try getting build rule by ToolChainFamily\r
180 if not RuleObject:\r
181 RuleObject = BuildRuleDatabase[Type, Inf.BuildType, Arch, GenFdsGlobalVariable.ToolChainFamily]\r
182 if not RuleObject:\r
183 # build type is always module type, but ...\r
184 if Inf.ModuleType != Inf.BuildType:\r
185 RuleObject = BuildRuleDatabase[Type, Inf.ModuleType, Arch, GenFdsGlobalVariable.ToolChainFamily]\r
186 if not RuleObject:\r
187 continue\r
188 RuleObject = RuleObject.Instantiate(Macro)\r
189 BuildRules[Type] = RuleObject\r
190 for Ext in RuleObject.SourceFileExtList:\r
191 BuildRules[Ext] = RuleObject\r
192 return BuildRules\r
193\r
194 ## GetModuleCodaTargetList\r
195 #\r
196 # @param Inf: object of InfBuildData\r
197 # @param Arch: current arch\r
198 #\r
199 @staticmethod\r
200 def GetModuleCodaTargetList(Inf, Arch):\r
201 BuildRules = GenFdsGlobalVariable.GetBuildRules(Inf, Arch)\r
202 if not BuildRules:\r
203 return []\r
204\r
205 TargetList = set()\r
206 FileList = []\r
97fa0ee9
YL
207\r
208 if not Inf.IsBinaryModule:\r
209 for File in Inf.Sources:\r
210 if File.TagName in ("", "*", GenFdsGlobalVariable.ToolChainTag) and \\r
211 File.ToolChainFamily in ("", "*", GenFdsGlobalVariable.ToolChainFamily):\r
212 FileList.append((File, DataType.TAB_UNKNOWN_FILE))\r
213\r
f51461c8
LG
214 for File in Inf.Binaries:\r
215 if File.Target in ['COMMON', '*', GenFdsGlobalVariable.TargetName]:\r
216 FileList.append((File, File.Type))\r
217\r
218 for File, FileType in FileList:\r
219 LastTarget = None\r
220 RuleChain = []\r
221 SourceList = [File]\r
222 Index = 0\r
223 while Index < len(SourceList):\r
224 Source = SourceList[Index]\r
225 Index = Index + 1\r
226 \r
227 if File.IsBinary and File == Source and Inf.Binaries != None and File in Inf.Binaries:\r
228 # Skip all files that are not binary libraries\r
229 if not Inf.LibraryClass:\r
230 continue \r
231 RuleObject = BuildRules[DataType.TAB_DEFAULT_BINARY_FILE]\r
232 elif FileType in BuildRules:\r
233 RuleObject = BuildRules[FileType]\r
234 elif Source.Ext in BuildRules:\r
235 RuleObject = BuildRules[Source.Ext]\r
236 else:\r
237 # stop at no more rules\r
238 if LastTarget:\r
239 TargetList.add(str(LastTarget))\r
240 break\r
241 \r
242 FileType = RuleObject.SourceFileType\r
243 \r
244 # stop at STATIC_LIBRARY for library\r
245 if Inf.LibraryClass and FileType == DataType.TAB_STATIC_LIBRARY:\r
246 if LastTarget:\r
247 TargetList.add(str(LastTarget))\r
248 break\r
249 \r
250 Target = RuleObject.Apply(Source)\r
251 if not Target:\r
252 if LastTarget:\r
253 TargetList.add(str(LastTarget))\r
254 break\r
255 elif not Target.Outputs:\r
256 # Only do build for target with outputs\r
257 TargetList.add(str(Target))\r
258 \r
259 # to avoid cyclic rule\r
260 if FileType in RuleChain:\r
261 break\r
262 \r
263 RuleChain.append(FileType)\r
264 SourceList.extend(Target.Outputs)\r
265 LastTarget = Target\r
266 FileType = DataType.TAB_UNKNOWN_FILE\r
267\r
268 return list(TargetList)\r
269\r
270 ## SetDir()\r
271 #\r
272 # @param OutputDir Output directory\r
273 # @param FdfParser FDF contents parser\r
274 # @param Workspace The directory of workspace\r
275 # @param ArchList The Arch list of platform\r
276 #\r
277 def SetDir (OutputDir, FdfParser, WorkSpace, ArchList):\r
47fea6af 278 GenFdsGlobalVariable.VerboseLogger("GenFdsGlobalVariable.OutputDir :%s" % OutputDir)\r
f51461c8
LG
279# GenFdsGlobalVariable.OutputDirDict = OutputDir\r
280 GenFdsGlobalVariable.FdfParser = FdfParser\r
281 GenFdsGlobalVariable.WorkSpace = WorkSpace\r
282 GenFdsGlobalVariable.FvDir = os.path.join(GenFdsGlobalVariable.OutputDirDict[ArchList[0]], 'FV')\r
283 if not os.path.exists(GenFdsGlobalVariable.FvDir) :\r
284 os.makedirs(GenFdsGlobalVariable.FvDir)\r
285 GenFdsGlobalVariable.FfsDir = os.path.join(GenFdsGlobalVariable.FvDir, 'Ffs')\r
286 if not os.path.exists(GenFdsGlobalVariable.FfsDir) :\r
287 os.makedirs(GenFdsGlobalVariable.FfsDir)\r
f51461c8
LG
288\r
289 T_CHAR_LF = '\n'\r
290 #\r
291 # Create FV Address inf file\r
292 #\r
293 GenFdsGlobalVariable.FvAddressFileName = os.path.join(GenFdsGlobalVariable.FfsDir, 'FvAddress.inf')\r
47fea6af 294 FvAddressFile = open(GenFdsGlobalVariable.FvAddressFileName, 'w')\r
f51461c8
LG
295 #\r
296 # Add [Options]\r
297 #\r
298 FvAddressFile.writelines("[options]" + T_CHAR_LF)\r
299 BsAddress = '0'\r
300 for Arch in ArchList:\r
301 if GenFdsGlobalVariable.WorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, Arch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag].BsBaseAddress:\r
302 BsAddress = GenFdsGlobalVariable.WorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, Arch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag].BsBaseAddress\r
303 break\r
304\r
305 FvAddressFile.writelines("EFI_BOOT_DRIVER_BASE_ADDRESS = " + \\r
47fea6af 306 BsAddress + \\r
f51461c8
LG
307 T_CHAR_LF)\r
308\r
309 RtAddress = '0'\r
310 for Arch in ArchList:\r
311 if GenFdsGlobalVariable.WorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, Arch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag].RtBaseAddress:\r
312 RtAddress = GenFdsGlobalVariable.WorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, Arch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag].RtBaseAddress\r
313\r
314 FvAddressFile.writelines("EFI_RUNTIME_DRIVER_BASE_ADDRESS = " + \\r
47fea6af 315 RtAddress + \\r
f51461c8
LG
316 T_CHAR_LF)\r
317\r
318 FvAddressFile.close()\r
319\r
320 ## ReplaceWorkspaceMacro()\r
321 #\r
322 # @param String String that may contain macro\r
323 #\r
324 def ReplaceWorkspaceMacro(String):\r
05cc51ad 325 String = mws.handleWsMacro(String)\r
f51461c8
LG
326 Str = String.replace('$(WORKSPACE)', GenFdsGlobalVariable.WorkSpaceDir)\r
327 if os.path.exists(Str):\r
328 if not os.path.isabs(Str):\r
329 Str = os.path.abspath(Str)\r
330 else:\r
05cc51ad 331 Str = mws.join(GenFdsGlobalVariable.WorkSpaceDir, String)\r
f51461c8
LG
332 return os.path.normpath(Str)\r
333\r
334 ## Check if the input files are newer than output files\r
335 #\r
336 # @param Output Path of output file\r
337 # @param Input Path list of input files\r
338 #\r
339 # @retval True if Output doesn't exist, or any Input is newer\r
340 # @retval False if all Input is older than Output\r
341 #\r
342 @staticmethod\r
343 def NeedsUpdate(Output, Input):\r
344 if not os.path.exists(Output):\r
345 return True\r
346 # always update "Output" if no "Input" given\r
347 if Input == None or len(Input) == 0:\r
348 return True\r
349\r
350 # if fdf file is changed after the 'Output" is generated, update the 'Output'\r
351 OutputTime = os.path.getmtime(Output)\r
352 if GenFdsGlobalVariable.FdfFileTimeStamp > OutputTime:\r
353 return True\r
354\r
355 for F in Input:\r
356 # always update "Output" if any "Input" doesn't exist\r
357 if not os.path.exists(F):\r
358 return True\r
359 # always update "Output" if any "Input" is newer than "Output"\r
360 if os.path.getmtime(F) > OutputTime:\r
361 return True\r
362 return False\r
363\r
364 @staticmethod\r
365 def GenerateSection(Output, Input, Type=None, CompressionType=None, Guid=None,\r
366 GuidHdrLen=None, GuidAttr=[], Ui=None, Ver=None, InputAlign=None, BuildNumber=None):\r
367 Cmd = ["GenSec"]\r
368 if Type not in [None, '']:\r
369 Cmd += ["-s", Type]\r
370 if CompressionType not in [None, '']:\r
371 Cmd += ["-c", CompressionType]\r
372 if Guid != None:\r
373 Cmd += ["-g", Guid]\r
374 if GuidHdrLen not in [None, '']:\r
375 Cmd += ["-l", GuidHdrLen]\r
376 if len(GuidAttr) != 0:\r
377 #Add each guided attribute\r
378 for Attr in GuidAttr:\r
379 Cmd += ["-r", Attr]\r
380 if InputAlign != None:\r
381 #Section Align is only for dummy section without section type\r
382 for SecAlign in InputAlign:\r
383 Cmd += ["--sectionalign", SecAlign]\r
384\r
385 CommandFile = Output + '.txt'\r
386 if Ui not in [None, '']:\r
387 #Cmd += ["-n", '"' + Ui + '"']\r
47fea6af 388 SectionData = array.array('B', [0, 0, 0, 0])\r
f51461c8
LG
389 SectionData.fromstring(Ui.encode("utf_16_le"))\r
390 SectionData.append(0)\r
391 SectionData.append(0)\r
392 Len = len(SectionData)\r
393 GenFdsGlobalVariable.SectionHeader.pack_into(SectionData, 0, Len & 0xff, (Len >> 8) & 0xff, (Len >> 16) & 0xff, 0x15)\r
47fea6af 394 SaveFileOnChange(Output, SectionData.tostring())\r
f51461c8
LG
395 elif Ver not in [None, '']:\r
396 Cmd += ["-n", Ver]\r
397 if BuildNumber:\r
398 Cmd += ["-j", BuildNumber]\r
399 Cmd += ["-o", Output]\r
400\r
401 SaveFileOnChange(CommandFile, ' '.join(Cmd), False)\r
402 if not GenFdsGlobalVariable.NeedsUpdate(Output, list(Input) + [CommandFile]):\r
403 return\r
404\r
405 GenFdsGlobalVariable.CallExternalTool(Cmd, "Failed to generate section")\r
406 else:\r
407 Cmd += ["-o", Output]\r
408 Cmd += Input\r
409\r
410 SaveFileOnChange(CommandFile, ' '.join(Cmd), False)\r
411 if GenFdsGlobalVariable.NeedsUpdate(Output, list(Input) + [CommandFile]):\r
412 GenFdsGlobalVariable.DebugLogger(EdkLogger.DEBUG_5, "%s needs update because of newer %s" % (Output, Input))\r
413 GenFdsGlobalVariable.CallExternalTool(Cmd, "Failed to generate section")\r
414\r
415 if (os.path.getsize(Output) >= GenFdsGlobalVariable.LARGE_FILE_SIZE and\r
416 GenFdsGlobalVariable.LargeFileInFvFlags):\r
417 GenFdsGlobalVariable.LargeFileInFvFlags[-1] = True \r
418\r
419 @staticmethod\r
420 def GetAlignment (AlignString):\r
421 if AlignString == None:\r
422 return 0\r
52302d4d
LG
423 if AlignString in ("1K", "2K", "4K", "8K", "16K", "32K", "64K"):\r
424 return int (AlignString.rstrip('K')) * 1024\r
425 else:\r
426 return int (AlignString)\r
f51461c8
LG
427\r
428 @staticmethod\r
429 def GenerateFfs(Output, Input, Type, Guid, Fixed=False, CheckSum=False, Align=None,\r
430 SectionAlign=None):\r
431 Cmd = ["GenFfs", "-t", Type, "-g", Guid]\r
d2192f12 432 mFfsValidAlign = ["0", "8", "16", "128", "512", "1K", "4K", "32K", "64K"]\r
f51461c8
LG
433 if Fixed == True:\r
434 Cmd += ["-x"]\r
435 if CheckSum:\r
436 Cmd += ["-s"]\r
437 if Align not in [None, '']:\r
d2192f12
YZ
438 if Align not in mFfsValidAlign:\r
439 Align = GenFdsGlobalVariable.GetAlignment (Align)\r
440 for index in range(0, len(mFfsValidAlign) - 1):\r
441 if ((Align > GenFdsGlobalVariable.GetAlignment(mFfsValidAlign[index])) and (Align <= GenFdsGlobalVariable.GetAlignment(mFfsValidAlign[index + 1]))):\r
442 break\r
443 Align = mFfsValidAlign[index + 1]\r
f51461c8
LG
444 Cmd += ["-a", Align]\r
445\r
446 Cmd += ["-o", Output]\r
447 for I in range(0, len(Input)):\r
448 Cmd += ("-i", Input[I])\r
449 if SectionAlign not in [None, '', []] and SectionAlign[I] not in [None, '']:\r
450 Cmd += ("-n", SectionAlign[I])\r
451\r
452 CommandFile = Output + '.txt'\r
453 SaveFileOnChange(CommandFile, ' '.join(Cmd), False)\r
454 if not GenFdsGlobalVariable.NeedsUpdate(Output, list(Input) + [CommandFile]):\r
455 return\r
456 GenFdsGlobalVariable.DebugLogger(EdkLogger.DEBUG_5, "%s needs update because of newer %s" % (Output, Input))\r
457\r
458 GenFdsGlobalVariable.CallExternalTool(Cmd, "Failed to generate FFS")\r
459\r
460 @staticmethod\r
461 def GenerateFirmwareVolume(Output, Input, BaseAddress=None, ForceRebase=None, Capsule=False, Dump=False,\r
462 AddressFile=None, MapFile=None, FfsList=[], FileSystemGuid=None):\r
463 if not GenFdsGlobalVariable.NeedsUpdate(Output, Input+FfsList):\r
464 return\r
465 GenFdsGlobalVariable.DebugLogger(EdkLogger.DEBUG_5, "%s needs update because of newer %s" % (Output, Input))\r
466\r
467 Cmd = ["GenFv"]\r
468 if BaseAddress not in [None, '']:\r
469 Cmd += ["-r", BaseAddress]\r
47fea6af 470\r
f51461c8 471 if ForceRebase == False:\r
47fea6af 472 Cmd += ["-F", "FALSE"]\r
f51461c8 473 elif ForceRebase == True:\r
47fea6af
YZ
474 Cmd += ["-F", "TRUE"]\r
475\r
f51461c8
LG
476 if Capsule:\r
477 Cmd += ["-c"]\r
478 if Dump:\r
479 Cmd += ["-p"]\r
480 if AddressFile not in [None, '']:\r
481 Cmd += ["-a", AddressFile]\r
482 if MapFile not in [None, '']:\r
483 Cmd += ["-m", MapFile]\r
484 if FileSystemGuid:\r
485 Cmd += ["-g", FileSystemGuid]\r
486 Cmd += ["-o", Output]\r
487 for I in Input:\r
488 Cmd += ["-i", I]\r
489\r
490 GenFdsGlobalVariable.CallExternalTool(Cmd, "Failed to generate FV")\r
491\r
492 @staticmethod\r
493 def GenerateVtf(Output, Input, BaseAddress=None, FvSize=None):\r
494 if not GenFdsGlobalVariable.NeedsUpdate(Output, Input):\r
495 return\r
496 GenFdsGlobalVariable.DebugLogger(EdkLogger.DEBUG_5, "%s needs update because of newer %s" % (Output, Input))\r
497\r
498 Cmd = ["GenVtf"]\r
499 if BaseAddress not in [None, ''] and FvSize not in [None, ''] \\r
500 and len(BaseAddress) == len(FvSize):\r
501 for I in range(0, len(BaseAddress)):\r
502 Cmd += ["-r", BaseAddress[I], "-s", FvSize[I]]\r
503 Cmd += ["-o", Output]\r
504 for F in Input:\r
505 Cmd += ["-f", F]\r
506\r
507 GenFdsGlobalVariable.CallExternalTool(Cmd, "Failed to generate VTF")\r
508\r
509 @staticmethod\r
510 def GenerateFirmwareImage(Output, Input, Type="efi", SubType=None, Zero=False,\r
511 Strip=False, Replace=False, TimeStamp=None, Join=False,\r
512 Align=None, Padding=None, Convert=False):\r
513 if not GenFdsGlobalVariable.NeedsUpdate(Output, Input):\r
514 return\r
515 GenFdsGlobalVariable.DebugLogger(EdkLogger.DEBUG_5, "%s needs update because of newer %s" % (Output, Input))\r
516\r
517 Cmd = ["GenFw"]\r
518 if Type.lower() == "te":\r
519 Cmd += ["-t"]\r
520 if SubType not in [None, '']:\r
521 Cmd += ["-e", SubType]\r
522 if TimeStamp not in [None, '']:\r
523 Cmd += ["-s", TimeStamp]\r
524 if Align not in [None, '']:\r
525 Cmd += ["-a", Align]\r
526 if Padding not in [None, '']:\r
527 Cmd += ["-p", Padding]\r
528 if Zero:\r
529 Cmd += ["-z"]\r
530 if Strip:\r
531 Cmd += ["-l"]\r
532 if Replace:\r
533 Cmd += ["-r"]\r
534 if Join:\r
535 Cmd += ["-j"]\r
536 if Convert:\r
537 Cmd += ["-m"]\r
538 Cmd += ["-o", Output]\r
539 Cmd += Input\r
540\r
541 GenFdsGlobalVariable.CallExternalTool(Cmd, "Failed to generate firmware image")\r
542\r
543 @staticmethod\r
544 def GenerateOptionRom(Output, EfiInput, BinaryInput, Compress=False, ClassCode=None,\r
545 Revision=None, DeviceId=None, VendorId=None):\r
546 InputList = [] \r
547 Cmd = ["EfiRom"]\r
548 if len(EfiInput) > 0:\r
549 \r
550 if Compress:\r
551 Cmd += ["-ec"]\r
552 else:\r
553 Cmd += ["-e"]\r
554 \r
555 for EfiFile in EfiInput:\r
556 Cmd += [EfiFile]\r
557 InputList.append (EfiFile)\r
558 \r
559 if len(BinaryInput) > 0:\r
560 Cmd += ["-b"]\r
561 for BinFile in BinaryInput:\r
562 Cmd += [BinFile]\r
563 InputList.append (BinFile)\r
564\r
565 # Check List\r
566 if not GenFdsGlobalVariable.NeedsUpdate(Output, InputList):\r
567 return\r
568 GenFdsGlobalVariable.DebugLogger(EdkLogger.DEBUG_5, "%s needs update because of newer %s" % (Output, InputList))\r
569 \r
570 if ClassCode != None:\r
571 Cmd += ["-l", ClassCode]\r
572 if Revision != None:\r
573 Cmd += ["-r", Revision]\r
574 if DeviceId != None:\r
575 Cmd += ["-i", DeviceId]\r
576 if VendorId != None:\r
577 Cmd += ["-f", VendorId]\r
578\r
47fea6af 579 Cmd += ["-o", Output]\r
f51461c8
LG
580 GenFdsGlobalVariable.CallExternalTool(Cmd, "Failed to generate option rom")\r
581\r
582 @staticmethod\r
583 def GuidTool(Output, Input, ToolPath, Options='', returnValue=[]):\r
584 if not GenFdsGlobalVariable.NeedsUpdate(Output, Input):\r
585 return\r
586 GenFdsGlobalVariable.DebugLogger(EdkLogger.DEBUG_5, "%s needs update because of newer %s" % (Output, Input))\r
587\r
588 Cmd = [ToolPath, ]\r
589 Cmd += Options.split(' ')\r
590 Cmd += ["-o", Output]\r
591 Cmd += Input\r
592\r
593 GenFdsGlobalVariable.CallExternalTool(Cmd, "Failed to call " + ToolPath, returnValue)\r
594\r
595 def CallExternalTool (cmd, errorMess, returnValue=[]):\r
596\r
597 if type(cmd) not in (tuple, list):\r
598 GenFdsGlobalVariable.ErrorLogger("ToolError! Invalid parameter type in call to CallExternalTool")\r
599\r
600 if GenFdsGlobalVariable.DebugLevel != -1:\r
601 cmd += ('--debug', str(GenFdsGlobalVariable.DebugLevel))\r
602 GenFdsGlobalVariable.InfLogger (cmd)\r
603\r
604 if GenFdsGlobalVariable.VerboseMode:\r
605 cmd += ('-v',)\r
606 GenFdsGlobalVariable.InfLogger (cmd)\r
607 else:\r
608 sys.stdout.write ('#')\r
609 sys.stdout.flush()\r
610 GenFdsGlobalVariable.SharpCounter = GenFdsGlobalVariable.SharpCounter + 1\r
611 if GenFdsGlobalVariable.SharpCounter % GenFdsGlobalVariable.SharpNumberPerLine == 0:\r
612 sys.stdout.write('\n')\r
613\r
614 try:\r
47fea6af 615 PopenObject = subprocess.Popen(' '.join(cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\r
f51461c8
LG
616 except Exception, X:\r
617 EdkLogger.error("GenFds", COMMAND_FAILURE, ExtraData="%s: %s" % (str(X), cmd[0]))\r
618 (out, error) = PopenObject.communicate()\r
619\r
620 while PopenObject.returncode == None :\r
621 PopenObject.wait()\r
622 if returnValue != [] and returnValue[0] != 0:\r
623 #get command return value\r
624 returnValue[0] = PopenObject.returncode\r
625 return\r
626 if PopenObject.returncode != 0 or GenFdsGlobalVariable.VerboseMode or GenFdsGlobalVariable.DebugLevel != -1:\r
47fea6af 627 GenFdsGlobalVariable.InfLogger ("Return Value = %d" % PopenObject.returncode)\r
f51461c8
LG
628 GenFdsGlobalVariable.InfLogger (out)\r
629 GenFdsGlobalVariable.InfLogger (error)\r
630 if PopenObject.returncode != 0:\r
631 print "###", cmd\r
632 EdkLogger.error("GenFds", COMMAND_FAILURE, errorMess)\r
633\r
634 def VerboseLogger (msg):\r
635 EdkLogger.verbose(msg)\r
636\r
637 def InfLogger (msg):\r
638 EdkLogger.info(msg)\r
639\r
47fea6af 640 def ErrorLogger (msg, File=None, Line=None, ExtraData=None):\r
f51461c8
LG
641 EdkLogger.error('GenFds', GENFDS_ERROR, msg, File, Line, ExtraData)\r
642\r
643 def DebugLogger (Level, msg):\r
644 EdkLogger.debug(Level, msg)\r
645\r
646 ## ReplaceWorkspaceMacro()\r
647 #\r
648 # @param Str String that may contain macro\r
649 # @param MacroDict Dictionary that contains macro value pair\r
650 #\r
47fea6af 651 def MacroExtend (Str, MacroDict={}, Arch='COMMON'):\r
f51461c8
LG
652 if Str == None :\r
653 return None\r
654\r
655 Dict = {'$(WORKSPACE)' : GenFdsGlobalVariable.WorkSpaceDir,\r
656 '$(EDK_SOURCE)' : GenFdsGlobalVariable.EdkSourceDir,\r
657# '$(OUTPUT_DIRECTORY)': GenFdsGlobalVariable.OutputDirFromDsc,\r
658 '$(TARGET)' : GenFdsGlobalVariable.TargetName,\r
97fa0ee9
YL
659 '$(TOOL_CHAIN_TAG)' : GenFdsGlobalVariable.ToolChainTag,\r
660 '$(SPACE)' : ' '\r
f51461c8
LG
661 }\r
662 OutputDir = GenFdsGlobalVariable.OutputDirFromDscDict[GenFdsGlobalVariable.ArchList[0]]\r
663 if Arch != 'COMMON' and Arch in GenFdsGlobalVariable.ArchList:\r
664 OutputDir = GenFdsGlobalVariable.OutputDirFromDscDict[Arch]\r
665\r
666 Dict['$(OUTPUT_DIRECTORY)'] = OutputDir\r
667\r
668 if MacroDict != None and len (MacroDict) != 0:\r
669 Dict.update(MacroDict)\r
670\r
671 for key in Dict.keys():\r
672 if Str.find(key) >= 0 :\r
673 Str = Str.replace (key, Dict[key])\r
674\r
675 if Str.find('$(ARCH)') >= 0:\r
676 if len(GenFdsGlobalVariable.ArchList) == 1:\r
677 Str = Str.replace('$(ARCH)', GenFdsGlobalVariable.ArchList[0])\r
678 else:\r
679 EdkLogger.error("GenFds", GENFDS_ERROR, "No way to determine $(ARCH) for %s" % Str)\r
680\r
681 return Str\r
682\r
683 ## GetPcdValue()\r
684 #\r
685 # @param PcdPattern pattern that labels a PCD.\r
686 #\r
687 def GetPcdValue (PcdPattern):\r
688 if PcdPattern == None :\r
689 return None\r
690 PcdPair = PcdPattern.lstrip('PCD(').rstrip(')').strip().split('.')\r
691 TokenSpace = PcdPair[0]\r
692 TokenCName = PcdPair[1]\r
693\r
694 PcdValue = ''\r
695 for Arch in GenFdsGlobalVariable.ArchList:\r
696 Platform = GenFdsGlobalVariable.WorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, Arch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
697 PcdDict = Platform.Pcds\r
698 for Key in PcdDict:\r
699 PcdObj = PcdDict[Key]\r
700 if (PcdObj.TokenCName == TokenCName) and (PcdObj.TokenSpaceGuidCName == TokenSpace):\r
701 if PcdObj.Type != 'FixedAtBuild':\r
702 EdkLogger.error("GenFds", GENFDS_ERROR, "%s is not FixedAtBuild type." % PcdPattern)\r
703 if PcdObj.DatumType != 'VOID*':\r
704 EdkLogger.error("GenFds", GENFDS_ERROR, "%s is not VOID* datum type." % PcdPattern)\r
705 \r
706 PcdValue = PcdObj.DefaultValue\r
707 return PcdValue\r
47fea6af
YZ
708\r
709 for Package in GenFdsGlobalVariable.WorkSpace.GetPackageList(GenFdsGlobalVariable.ActivePlatform,\r
710 Arch,\r
711 GenFdsGlobalVariable.TargetName,\r
f51461c8
LG
712 GenFdsGlobalVariable.ToolChainTag):\r
713 PcdDict = Package.Pcds\r
714 for Key in PcdDict:\r
715 PcdObj = PcdDict[Key]\r
716 if (PcdObj.TokenCName == TokenCName) and (PcdObj.TokenSpaceGuidCName == TokenSpace):\r
717 if PcdObj.Type != 'FixedAtBuild':\r
718 EdkLogger.error("GenFds", GENFDS_ERROR, "%s is not FixedAtBuild type." % PcdPattern)\r
719 if PcdObj.DatumType != 'VOID*':\r
720 EdkLogger.error("GenFds", GENFDS_ERROR, "%s is not VOID* datum type." % PcdPattern)\r
721 \r
722 PcdValue = PcdObj.DefaultValue\r
723 return PcdValue\r
724\r
725 return PcdValue\r
726\r
727 SetDir = staticmethod(SetDir)\r
728 ReplaceWorkspaceMacro = staticmethod(ReplaceWorkspaceMacro)\r
729 CallExternalTool = staticmethod(CallExternalTool)\r
730 VerboseLogger = staticmethod(VerboseLogger)\r
731 InfLogger = staticmethod(InfLogger)\r
732 ErrorLogger = staticmethod(ErrorLogger)\r
733 DebugLogger = staticmethod(DebugLogger)\r
734 MacroExtend = staticmethod (MacroExtend)\r
735 GetPcdValue = staticmethod(GetPcdValue)\r