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