]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/GenFds/GenFds.py
BaseTools: GlobalData.gConfDirectory is None when run GenFds
[mirror_edk2.git] / BaseTools / Source / Python / GenFds / GenFds.py
1 ## @file
2 # generate flash image
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 optparse import OptionParser
19 import sys
20 import Common.LongFilePathOs as os
21 import linecache
22 import FdfParser
23 import Common.BuildToolError as BuildToolError
24 from GenFdsGlobalVariable import GenFdsGlobalVariable
25 from Workspace.WorkspaceDatabase import WorkspaceDatabase
26 from Workspace.BuildClassObject import PcdClassObject
27 from Workspace.BuildClassObject import ModuleBuildClassObject
28 import RuleComplexFile
29 from EfiSection import EfiSection
30 import StringIO
31 import Common.TargetTxtClassObject as TargetTxtClassObject
32 import Common.ToolDefClassObject as ToolDefClassObject
33 from Common.DataType import *
34 import Common.GlobalData as GlobalData
35 from Common import EdkLogger
36 from Common.String import *
37 from Common.Misc import DirCache, PathClass
38 from Common.Misc import SaveFileOnChange
39 from Common.Misc import ClearDuplicatedInf
40 from Common.Misc import GuidStructureStringToGuidString
41 from Common.Misc import CheckPcdDatum
42 from Common.Misc import BuildOptionPcdValueFormat
43 from Common.BuildVersion import gBUILD_VERSION
44 from Common.MultipleWorkspace import MultipleWorkspace as mws
45 import FfsFileStatement
46 import glob
47 from struct import unpack
48
49 ## Version and Copyright
50 versionNumber = "1.0" + ' ' + gBUILD_VERSION
51 __version__ = "%prog Version " + versionNumber
52 __copyright__ = "Copyright (c) 2007 - 2017, Intel Corporation All rights reserved."
53
54 ## Tool entrance method
55 #
56 # This method mainly dispatch specific methods per the command line options.
57 # If no error found, return zero value so the caller of this tool can know
58 # if it's executed successfully or not.
59 #
60 # @retval 0 Tool was successful
61 # @retval 1 Tool failed
62 #
63 def main():
64 global Options
65 Options = myOptionParser()
66
67 global Workspace
68 Workspace = ""
69 ArchList = None
70 ReturnCode = 0
71
72 EdkLogger.Initialize()
73 try:
74 if Options.verbose != None:
75 EdkLogger.SetLevel(EdkLogger.VERBOSE)
76 GenFdsGlobalVariable.VerboseMode = True
77
78 if Options.FixedAddress != None:
79 GenFdsGlobalVariable.FixedLoadAddress = True
80
81 if Options.quiet != None:
82 EdkLogger.SetLevel(EdkLogger.QUIET)
83 if Options.debug != None:
84 EdkLogger.SetLevel(Options.debug + 1)
85 GenFdsGlobalVariable.DebugLevel = Options.debug
86 else:
87 EdkLogger.SetLevel(EdkLogger.INFO)
88
89 if (Options.Workspace == None):
90 EdkLogger.error("GenFds", OPTION_MISSING, "WORKSPACE not defined",
91 ExtraData="Please use '-w' switch to pass it or set the WORKSPACE environment variable.")
92 elif not os.path.exists(Options.Workspace):
93 EdkLogger.error("GenFds", PARAMETER_INVALID, "WORKSPACE is invalid",
94 ExtraData="Please use '-w' switch to pass it or set the WORKSPACE environment variable.")
95 else:
96 Workspace = os.path.normcase(Options.Workspace)
97 GenFdsGlobalVariable.WorkSpaceDir = Workspace
98 if 'EDK_SOURCE' in os.environ.keys():
99 GenFdsGlobalVariable.EdkSourceDir = os.path.normcase(os.environ['EDK_SOURCE'])
100 if (Options.debug):
101 GenFdsGlobalVariable.VerboseLogger("Using Workspace:" + Workspace)
102 if Options.GenfdsMultiThread:
103 GenFdsGlobalVariable.EnableGenfdsMultiThread = True
104 os.chdir(GenFdsGlobalVariable.WorkSpaceDir)
105
106 # set multiple workspace
107 PackagesPath = os.getenv("PACKAGES_PATH")
108 mws.setWs(GenFdsGlobalVariable.WorkSpaceDir, PackagesPath)
109
110 if (Options.filename):
111 FdfFilename = Options.filename
112 FdfFilename = GenFdsGlobalVariable.ReplaceWorkspaceMacro(FdfFilename)
113
114 if FdfFilename[0:2] == '..':
115 FdfFilename = os.path.realpath(FdfFilename)
116 if not os.path.isabs(FdfFilename):
117 FdfFilename = mws.join(GenFdsGlobalVariable.WorkSpaceDir, FdfFilename)
118 if not os.path.exists(FdfFilename):
119 EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=FdfFilename)
120
121 GenFdsGlobalVariable.FdfFile = FdfFilename
122 GenFdsGlobalVariable.FdfFileTimeStamp = os.path.getmtime(FdfFilename)
123 else:
124 EdkLogger.error("GenFds", OPTION_MISSING, "Missing FDF filename")
125
126 if (Options.BuildTarget):
127 GenFdsGlobalVariable.TargetName = Options.BuildTarget
128
129 if (Options.ToolChain):
130 GenFdsGlobalVariable.ToolChainTag = Options.ToolChain
131
132 if (Options.activePlatform):
133 ActivePlatform = Options.activePlatform
134 ActivePlatform = GenFdsGlobalVariable.ReplaceWorkspaceMacro(ActivePlatform)
135
136 if ActivePlatform[0:2] == '..':
137 ActivePlatform = os.path.realpath(ActivePlatform)
138
139 if not os.path.isabs (ActivePlatform):
140 ActivePlatform = mws.join(GenFdsGlobalVariable.WorkSpaceDir, ActivePlatform)
141
142 if not os.path.exists(ActivePlatform) :
143 EdkLogger.error("GenFds", FILE_NOT_FOUND, "ActivePlatform doesn't exist!")
144 else:
145 EdkLogger.error("GenFds", OPTION_MISSING, "Missing active platform")
146
147 GenFdsGlobalVariable.ActivePlatform = PathClass(NormPath(ActivePlatform))
148
149 if (Options.ConfDirectory):
150 # Get alternate Conf location, if it is absolute, then just use the absolute directory name
151 ConfDirectoryPath = os.path.normpath(Options.ConfDirectory)
152 if ConfDirectoryPath.startswith('"'):
153 ConfDirectoryPath = ConfDirectoryPath[1:]
154 if ConfDirectoryPath.endswith('"'):
155 ConfDirectoryPath = ConfDirectoryPath[:-1]
156 if not os.path.isabs(ConfDirectoryPath):
157 # Since alternate directory name is not absolute, the alternate directory is located within the WORKSPACE
158 # This also handles someone specifying the Conf directory in the workspace. Using --conf=Conf
159 ConfDirectoryPath = os.path.join(GenFdsGlobalVariable.WorkSpaceDir, ConfDirectoryPath)
160 else:
161 if "CONF_PATH" in os.environ.keys():
162 ConfDirectoryPath = os.path.normcase(os.environ["CONF_PATH"])
163 else:
164 # Get standard WORKSPACE/Conf, use the absolute path to the WORKSPACE/Conf
165 ConfDirectoryPath = mws.join(GenFdsGlobalVariable.WorkSpaceDir, 'Conf')
166 GenFdsGlobalVariable.ConfDir = ConfDirectoryPath
167 if not GlobalData.gConfDirectory:
168 GlobalData.gConfDirectory = GenFdsGlobalVariable.ConfDir
169 BuildConfigurationFile = os.path.normpath(os.path.join(ConfDirectoryPath, "target.txt"))
170 if os.path.isfile(BuildConfigurationFile) == True:
171 TargetTxt = TargetTxtClassObject.TargetTxtClassObject()
172 TargetTxt.LoadTargetTxtFile(BuildConfigurationFile)
173 # if no build target given in command line, get it from target.txt
174 if not GenFdsGlobalVariable.TargetName:
175 BuildTargetList = TargetTxt.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_TARGET]
176 if len(BuildTargetList) != 1:
177 EdkLogger.error("GenFds", OPTION_VALUE_INVALID, ExtraData="Only allows one instance for Target.")
178 GenFdsGlobalVariable.TargetName = BuildTargetList[0]
179
180 # if no tool chain given in command line, get it from target.txt
181 if not GenFdsGlobalVariable.ToolChainTag:
182 ToolChainList = TargetTxt.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_TOOL_CHAIN_TAG]
183 if ToolChainList == None or len(ToolChainList) == 0:
184 EdkLogger.error("GenFds", RESOURCE_NOT_AVAILABLE, ExtraData="No toolchain given. Don't know how to build.")
185 if len(ToolChainList) != 1:
186 EdkLogger.error("GenFds", OPTION_VALUE_INVALID, ExtraData="Only allows one instance for ToolChain.")
187 GenFdsGlobalVariable.ToolChainTag = ToolChainList[0]
188 else:
189 EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=BuildConfigurationFile)
190
191 #Set global flag for build mode
192 GlobalData.gIgnoreSource = Options.IgnoreSources
193
194 if Options.Macros:
195 for Pair in Options.Macros:
196 if Pair.startswith('"'):
197 Pair = Pair[1:]
198 if Pair.endswith('"'):
199 Pair = Pair[:-1]
200 List = Pair.split('=')
201 if len(List) == 2:
202 if not List[1].strip():
203 EdkLogger.error("GenFds", OPTION_VALUE_INVALID, ExtraData="No Value given for Macro %s" %List[0])
204 if List[0].strip() == "EFI_SOURCE":
205 GlobalData.gEfiSource = List[1].strip()
206 GlobalData.gGlobalDefines["EFI_SOURCE"] = GlobalData.gEfiSource
207 continue
208 elif List[0].strip() == "EDK_SOURCE":
209 GlobalData.gEdkSource = List[1].strip()
210 GlobalData.gGlobalDefines["EDK_SOURCE"] = GlobalData.gEdkSource
211 continue
212 elif List[0].strip() in ["WORKSPACE", "TARGET", "TOOLCHAIN"]:
213 GlobalData.gGlobalDefines[List[0].strip()] = List[1].strip()
214 else:
215 GlobalData.gCommandLineDefines[List[0].strip()] = List[1].strip()
216 else:
217 GlobalData.gCommandLineDefines[List[0].strip()] = "TRUE"
218 os.environ["WORKSPACE"] = Workspace
219
220 # Use the -t and -b option as gGlobalDefines's TOOLCHAIN and TARGET if they are not defined
221 if "TARGET" not in GlobalData.gGlobalDefines.keys():
222 GlobalData.gGlobalDefines["TARGET"] = GenFdsGlobalVariable.TargetName
223 if "TOOLCHAIN" not in GlobalData.gGlobalDefines.keys():
224 GlobalData.gGlobalDefines["TOOLCHAIN"] = GenFdsGlobalVariable.ToolChainTag
225 if "TOOL_CHAIN_TAG" not in GlobalData.gGlobalDefines.keys():
226 GlobalData.gGlobalDefines['TOOL_CHAIN_TAG'] = GenFdsGlobalVariable.ToolChainTag
227
228 """call Workspace build create database"""
229 GlobalData.gDatabasePath = os.path.normpath(os.path.join(ConfDirectoryPath, GlobalData.gDatabasePath))
230 BuildWorkSpace = WorkspaceDatabase(GlobalData.gDatabasePath)
231 BuildWorkSpace.InitDatabase()
232
233 #
234 # Get files real name in workspace dir
235 #
236 GlobalData.gAllFiles = DirCache(Workspace)
237 GlobalData.gWorkspace = Workspace
238
239 if (Options.archList) :
240 ArchList = Options.archList.split(',')
241 else:
242 # EdkLogger.error("GenFds", OPTION_MISSING, "Missing build ARCH")
243 ArchList = BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, 'COMMON', Options.BuildTarget, Options.ToolChain].SupArchList
244
245 TargetArchList = set(BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, 'COMMON', Options.BuildTarget, Options.ToolChain].SupArchList) & set(ArchList)
246 if len(TargetArchList) == 0:
247 EdkLogger.error("GenFds", GENFDS_ERROR, "Target ARCH %s not in platform supported ARCH %s" % (str(ArchList), str(BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, 'COMMON'].SupArchList)))
248
249 for Arch in ArchList:
250 GenFdsGlobalVariable.OutputDirFromDscDict[Arch] = NormPath(BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, Arch, Options.BuildTarget, Options.ToolChain].OutputDirectory)
251 GenFdsGlobalVariable.PlatformName = BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, Arch, Options.BuildTarget, Options.ToolChain].PlatformName
252
253 if (Options.outputDir):
254 OutputDirFromCommandLine = GenFdsGlobalVariable.ReplaceWorkspaceMacro(Options.outputDir)
255 if not os.path.isabs (OutputDirFromCommandLine):
256 OutputDirFromCommandLine = os.path.join(GenFdsGlobalVariable.WorkSpaceDir, OutputDirFromCommandLine)
257 for Arch in ArchList:
258 GenFdsGlobalVariable.OutputDirDict[Arch] = OutputDirFromCommandLine
259 else:
260 for Arch in ArchList:
261 GenFdsGlobalVariable.OutputDirDict[Arch] = os.path.join(GenFdsGlobalVariable.OutputDirFromDscDict[Arch], GenFdsGlobalVariable.TargetName + '_' + GenFdsGlobalVariable.ToolChainTag)
262
263 for Key in GenFdsGlobalVariable.OutputDirDict:
264 OutputDir = GenFdsGlobalVariable.OutputDirDict[Key]
265 if OutputDir[0:2] == '..':
266 OutputDir = os.path.realpath(OutputDir)
267
268 if OutputDir[1] != ':':
269 OutputDir = os.path.join (GenFdsGlobalVariable.WorkSpaceDir, OutputDir)
270
271 if not os.path.exists(OutputDir):
272 EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=OutputDir)
273 GenFdsGlobalVariable.OutputDirDict[Key] = OutputDir
274
275 """ Parse Fdf file, has to place after build Workspace as FDF may contain macros from DSC file """
276 FdfParserObj = FdfParser.FdfParser(FdfFilename)
277 FdfParserObj.ParseFile()
278
279 if FdfParserObj.CycleReferenceCheck():
280 EdkLogger.error("GenFds", FORMAT_NOT_SUPPORTED, "Cycle Reference Detected in FDF file")
281
282 if (Options.uiFdName) :
283 if Options.uiFdName.upper() in FdfParserObj.Profile.FdDict.keys():
284 GenFds.OnlyGenerateThisFd = Options.uiFdName
285 else:
286 EdkLogger.error("GenFds", OPTION_VALUE_INVALID,
287 "No such an FD in FDF file: %s" % Options.uiFdName)
288
289 if (Options.uiFvName) :
290 if Options.uiFvName.upper() in FdfParserObj.Profile.FvDict.keys():
291 GenFds.OnlyGenerateThisFv = Options.uiFvName
292 else:
293 EdkLogger.error("GenFds", OPTION_VALUE_INVALID,
294 "No such an FV in FDF file: %s" % Options.uiFvName)
295
296 if (Options.uiCapName) :
297 if Options.uiCapName.upper() in FdfParserObj.Profile.CapsuleDict.keys():
298 GenFds.OnlyGenerateThisCap = Options.uiCapName
299 else:
300 EdkLogger.error("GenFds", OPTION_VALUE_INVALID,
301 "No such a Capsule in FDF file: %s" % Options.uiCapName)
302
303 GenFdsGlobalVariable.WorkSpace = BuildWorkSpace
304 if ArchList != None:
305 GenFdsGlobalVariable.ArchList = ArchList
306
307 # Dsc Build Data will handle Pcd Settings from CommandLine.
308
309 """Modify images from build output if the feature of loading driver at fixed address is on."""
310 if GenFdsGlobalVariable.FixedLoadAddress:
311 GenFds.PreprocessImage(BuildWorkSpace, GenFdsGlobalVariable.ActivePlatform)
312
313 # Record the FV Region info that may specific in the FD
314 if FdfParserObj.Profile.FvDict and FdfParserObj.Profile.FdDict:
315 for Fv in FdfParserObj.Profile.FvDict:
316 FvObj = FdfParserObj.Profile.FvDict[Fv]
317 for Fd in FdfParserObj.Profile.FdDict:
318 FdObj = FdfParserObj.Profile.FdDict[Fd]
319 for RegionObj in FdObj.RegionList:
320 if RegionObj.RegionType != 'FV':
321 continue
322 for RegionData in RegionObj.RegionDataList:
323 if FvObj.UiFvName.upper() == RegionData.upper():
324 if FvObj.FvRegionInFD:
325 if FvObj.FvRegionInFD != RegionObj.Size:
326 EdkLogger.error("GenFds", FORMAT_INVALID, "The FV %s's region is specified in multiple FD with different value." %FvObj.UiFvName)
327 else:
328 FvObj.FvRegionInFD = RegionObj.Size
329 RegionObj.BlockInfoOfRegion(FdObj.BlockSizeList, FvObj)
330
331 GlobalData.BuildOptionPcd = Options.OptionPcd if Options.OptionPcd else {}
332 """Call GenFds"""
333 GenFds.GenFd('', FdfParserObj, BuildWorkSpace, ArchList)
334
335 """Generate GUID cross reference file"""
336 GenFds.GenerateGuidXRefFile(BuildWorkSpace, ArchList, FdfParserObj)
337
338 """Display FV space info."""
339 GenFds.DisplayFvSpaceInfo(FdfParserObj)
340
341 except FdfParser.Warning, X:
342 EdkLogger.error(X.ToolName, FORMAT_INVALID, File=X.FileName, Line=X.LineNumber, ExtraData=X.Message, RaiseError=False)
343 ReturnCode = FORMAT_INVALID
344 except FatalError, X:
345 if Options.debug != None:
346 import traceback
347 EdkLogger.quiet(traceback.format_exc())
348 ReturnCode = X.args[0]
349 except:
350 import traceback
351 EdkLogger.error(
352 "\nPython",
353 CODE_ERROR,
354 "Tools code failure",
355 ExtraData="Please send email to edk2-devel@lists.01.org for help, attaching following call stack trace!\n",
356 RaiseError=False
357 )
358 EdkLogger.quiet(traceback.format_exc())
359 ReturnCode = CODE_ERROR
360 finally:
361 ClearDuplicatedInf()
362 return ReturnCode
363
364 gParamCheck = []
365 def SingleCheckCallback(option, opt_str, value, parser):
366 if option not in gParamCheck:
367 setattr(parser.values, option.dest, value)
368 gParamCheck.append(option)
369 else:
370 parser.error("Option %s only allows one instance in command line!" % option)
371
372 def CheckBuildOptionPcd():
373 for Arch in GenFdsGlobalVariable.ArchList:
374 PkgList = GenFdsGlobalVariable.WorkSpace.GetPackageList(GenFdsGlobalVariable.ActivePlatform, Arch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag)
375 for i, pcd in enumerate(GlobalData.BuildOptionPcd):
376 if type(pcd) is tuple:
377 continue
378 (pcdname, pcdvalue) = pcd.split('=')
379 if not pcdvalue:
380 EdkLogger.error('GenFds', OPTION_MISSING, "No Value specified for the PCD %s." % (pcdname))
381 if '.' in pcdname:
382 (TokenSpaceGuidCName, TokenCName) = pcdname.split('.')
383 HasTokenSpace = True
384 else:
385 TokenCName = pcdname
386 TokenSpaceGuidCName = ''
387 HasTokenSpace = False
388 TokenSpaceGuidCNameList = []
389 FoundFlag = False
390 PcdDatumType = ''
391 NewValue = ''
392 for package in PkgList:
393 for key in package.Pcds:
394 PcdItem = package.Pcds[key]
395 if HasTokenSpace:
396 if (PcdItem.TokenCName, PcdItem.TokenSpaceGuidCName) == (TokenCName, TokenSpaceGuidCName):
397 PcdDatumType = PcdItem.DatumType
398 NewValue = BuildOptionPcdValueFormat(TokenSpaceGuidCName, TokenCName, PcdDatumType, pcdvalue)
399 FoundFlag = True
400 else:
401 if PcdItem.TokenCName == TokenCName:
402 if not PcdItem.TokenSpaceGuidCName in TokenSpaceGuidCNameList:
403 if len (TokenSpaceGuidCNameList) < 1:
404 TokenSpaceGuidCNameList.append(PcdItem.TokenSpaceGuidCName)
405 PcdDatumType = PcdItem.DatumType
406 TokenSpaceGuidCName = PcdItem.TokenSpaceGuidCName
407 NewValue = BuildOptionPcdValueFormat(TokenSpaceGuidCName, TokenCName, PcdDatumType, pcdvalue)
408 FoundFlag = True
409 else:
410 EdkLogger.error(
411 'GenFds',
412 PCD_VALIDATION_INFO_ERROR,
413 "The Pcd %s is found under multiple different TokenSpaceGuid: %s and %s." % (TokenCName, PcdItem.TokenSpaceGuidCName, TokenSpaceGuidCNameList[0])
414 )
415
416 GlobalData.BuildOptionPcd[i] = (TokenSpaceGuidCName, TokenCName, NewValue)
417
418
419 ## FindExtendTool()
420 #
421 # Find location of tools to process data
422 #
423 # @param KeyStringList Filter for inputs of section generation
424 # @param CurrentArchList Arch list
425 # @param NameGuid The Guid name
426 #
427 def FindExtendTool(KeyStringList, CurrentArchList, NameGuid):
428 ToolDb = ToolDefClassObject.ToolDefDict(GenFdsGlobalVariable.ConfDir).ToolsDefTxtDatabase
429 # if user not specify filter, try to deduce it from global data.
430 if KeyStringList == None or KeyStringList == []:
431 Target = GenFdsGlobalVariable.TargetName
432 ToolChain = GenFdsGlobalVariable.ToolChainTag
433 if ToolChain not in ToolDb['TOOL_CHAIN_TAG']:
434 EdkLogger.error("GenFds", GENFDS_ERROR, "Can not find external tool because tool tag %s is not defined in tools_def.txt!" % ToolChain)
435 KeyStringList = [Target + '_' + ToolChain + '_' + CurrentArchList[0]]
436 for Arch in CurrentArchList:
437 if Target + '_' + ToolChain + '_' + Arch not in KeyStringList:
438 KeyStringList.append(Target + '_' + ToolChain + '_' + Arch)
439
440 if GenFdsGlobalVariable.GuidToolDefinition:
441 if NameGuid in GenFdsGlobalVariable.GuidToolDefinition.keys():
442 return GenFdsGlobalVariable.GuidToolDefinition[NameGuid]
443
444 ToolDefinition = ToolDefClassObject.ToolDefDict(GenFdsGlobalVariable.ConfDir).ToolsDefTxtDictionary
445 ToolPathTmp = None
446 ToolOption = None
447 ToolPathKey = None
448 ToolOptionKey = None
449 KeyList = None
450 for ToolDef in ToolDefinition.items():
451 if NameGuid == ToolDef[1]:
452 KeyList = ToolDef[0].split('_')
453 Key = KeyList[0] + \
454 '_' + \
455 KeyList[1] + \
456 '_' + \
457 KeyList[2]
458 if Key in KeyStringList and KeyList[4] == 'GUID':
459 ToolPathKey = Key + '_' + KeyList[3] + '_PATH'
460 ToolOptionKey = Key + '_' + KeyList[3] + '_FLAGS'
461 ToolPath = ToolDefinition.get(ToolPathKey)
462 ToolOption = ToolDefinition.get(ToolOptionKey)
463 if ToolPathTmp == None:
464 ToolPathTmp = ToolPath
465 else:
466 if ToolPathTmp != ToolPath:
467 EdkLogger.error("GenFds", GENFDS_ERROR, "Don't know which tool to use, %s or %s ?" % (ToolPathTmp, ToolPath))
468
469 BuildOption = {}
470 for Arch in CurrentArchList:
471 Platform = GenFdsGlobalVariable.WorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, Arch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]
472 # key is (ToolChainFamily, ToolChain, CodeBase)
473 for item in Platform.BuildOptions:
474 if '_PATH' in item[1] or '_FLAGS' in item[1] or '_GUID' in item[1]:
475 if not item[0] or (item[0] and GenFdsGlobalVariable.ToolChainFamily== item[0]):
476 if item[1] not in BuildOption:
477 BuildOption[item[1]] = Platform.BuildOptions[item]
478 if BuildOption:
479 ToolList = [TAB_TOD_DEFINES_TARGET, TAB_TOD_DEFINES_TOOL_CHAIN_TAG, TAB_TOD_DEFINES_TARGET_ARCH]
480 for Index in range(2, -1, -1):
481 for Key in dict(BuildOption):
482 List = Key.split('_')
483 if List[Index] == '*':
484 for String in ToolDb[ToolList[Index]]:
485 if String in [Arch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]:
486 List[Index] = String
487 NewKey = '%s_%s_%s_%s_%s' % tuple(List)
488 if NewKey not in BuildOption:
489 BuildOption[NewKey] = BuildOption[Key]
490 continue
491 del BuildOption[Key]
492 elif List[Index] not in ToolDb[ToolList[Index]]:
493 del BuildOption[Key]
494 if BuildOption:
495 if not KeyList:
496 for Op in BuildOption:
497 if NameGuid == BuildOption[Op]:
498 KeyList = Op.split('_')
499 Key = KeyList[0] + '_' + KeyList[1] +'_' + KeyList[2]
500 if Key in KeyStringList and KeyList[4] == 'GUID':
501 ToolPathKey = Key + '_' + KeyList[3] + '_PATH'
502 ToolOptionKey = Key + '_' + KeyList[3] + '_FLAGS'
503 if ToolPathKey in BuildOption.keys():
504 ToolPathTmp = BuildOption.get(ToolPathKey)
505 if ToolOptionKey in BuildOption.keys():
506 ToolOption = BuildOption.get(ToolOptionKey)
507
508 GenFdsGlobalVariable.GuidToolDefinition[NameGuid] = (ToolPathTmp, ToolOption)
509 return ToolPathTmp, ToolOption
510
511 ## Parse command line options
512 #
513 # Using standard Python module optparse to parse command line option of this tool.
514 #
515 # @retval Opt A optparse.Values object containing the parsed options
516 # @retval Args Target of build command
517 #
518 def myOptionParser():
519 usage = "%prog [options] -f input_file -a arch_list -b build_target -p active_platform -t tool_chain_tag -D \"MacroName [= MacroValue]\""
520 Parser = OptionParser(usage=usage, description=__copyright__, version="%prog " + str(versionNumber))
521 Parser.add_option("-f", "--file", dest="filename", type="string", help="Name of FDF file to convert", action="callback", callback=SingleCheckCallback)
522 Parser.add_option("-a", "--arch", dest="archList", help="comma separated list containing one or more of: IA32, X64, IPF, ARM, AARCH64 or EBC which should be built, overrides target.txt?s TARGET_ARCH")
523 Parser.add_option("-q", "--quiet", action="store_true", type=None, help="Disable all messages except FATAL ERRORS.")
524 Parser.add_option("-v", "--verbose", action="store_true", type=None, help="Turn on verbose output with informational messages printed.")
525 Parser.add_option("-d", "--debug", action="store", type="int", help="Enable debug messages at specified level.")
526 Parser.add_option("-p", "--platform", type="string", dest="activePlatform", help="Set the ACTIVE_PLATFORM, overrides target.txt ACTIVE_PLATFORM setting.",
527 action="callback", callback=SingleCheckCallback)
528 Parser.add_option("-w", "--workspace", type="string", dest="Workspace", default=os.environ.get('WORKSPACE'), help="Set the WORKSPACE",
529 action="callback", callback=SingleCheckCallback)
530 Parser.add_option("-o", "--outputDir", type="string", dest="outputDir", help="Name of Build Output directory",
531 action="callback", callback=SingleCheckCallback)
532 Parser.add_option("-r", "--rom_image", dest="uiFdName", help="Build the image using the [FD] section named by FdUiName.")
533 Parser.add_option("-i", "--FvImage", dest="uiFvName", help="Build the FV image using the [FV] section named by UiFvName")
534 Parser.add_option("-C", "--CapsuleImage", dest="uiCapName", help="Build the Capsule image using the [Capsule] section named by UiCapName")
535 Parser.add_option("-b", "--buildtarget", type="string", dest="BuildTarget", help="Set the build TARGET, overrides target.txt TARGET setting.",
536 action="callback", callback=SingleCheckCallback)
537 Parser.add_option("-t", "--tagname", type="string", dest="ToolChain", help="Using the tools: TOOL_CHAIN_TAG name to build the platform.",
538 action="callback", callback=SingleCheckCallback)
539 Parser.add_option("-D", "--define", action="append", type="string", dest="Macros", help="Macro: \"Name [= Value]\".")
540 Parser.add_option("-s", "--specifyaddress", dest="FixedAddress", action="store_true", type=None, help="Specify driver load address.")
541 Parser.add_option("--conf", action="store", type="string", dest="ConfDirectory", help="Specify the customized Conf directory.")
542 Parser.add_option("--ignore-sources", action="store_true", dest="IgnoreSources", default=False, help="Focus to a binary build and ignore all source files")
543 Parser.add_option("--pcd", action="append", dest="OptionPcd", help="Set PCD value by command line. Format: \"PcdName=Value\" ")
544 Parser.add_option("--genfds-multi-thread", action="store_true", dest="GenfdsMultiThread", default=False, help="Enable GenFds multi thread to generate ffs file.")
545
546 (Options, args) = Parser.parse_args()
547 return Options
548
549 ## The class implementing the EDK2 flash image generation process
550 #
551 # This process includes:
552 # 1. Collect workspace information, includes platform and module information
553 # 2. Call methods of Fd class to generate FD
554 # 3. Call methods of Fv class to generate FV that not belong to FD
555 #
556 class GenFds :
557 FdfParsef = None
558 # FvName, FdName, CapName in FDF, Image file name
559 ImageBinDict = {}
560 OnlyGenerateThisFd = None
561 OnlyGenerateThisFv = None
562 OnlyGenerateThisCap = None
563
564 ## GenFd()
565 #
566 # @param OutputDir Output directory
567 # @param FdfParser FDF contents parser
568 # @param Workspace The directory of workspace
569 # @param ArchList The Arch list of platform
570 #
571 def GenFd (OutputDir, FdfParser, WorkSpace, ArchList):
572 GenFdsGlobalVariable.SetDir ('', FdfParser, WorkSpace, ArchList)
573
574 GenFdsGlobalVariable.VerboseLogger(" Generate all Fd images and their required FV and Capsule images!")
575 if GenFds.OnlyGenerateThisCap != None and GenFds.OnlyGenerateThisCap.upper() in GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict.keys():
576 CapsuleObj = GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict.get(GenFds.OnlyGenerateThisCap.upper())
577 if CapsuleObj != None:
578 CapsuleObj.GenCapsule()
579 return
580
581 if GenFds.OnlyGenerateThisFd != None and GenFds.OnlyGenerateThisFd.upper() in GenFdsGlobalVariable.FdfParser.Profile.FdDict.keys():
582 FdObj = GenFdsGlobalVariable.FdfParser.Profile.FdDict.get(GenFds.OnlyGenerateThisFd.upper())
583 if FdObj != None:
584 FdObj.GenFd()
585 return
586 elif GenFds.OnlyGenerateThisFd == None and GenFds.OnlyGenerateThisFv == None:
587 for FdName in GenFdsGlobalVariable.FdfParser.Profile.FdDict.keys():
588 FdObj = GenFdsGlobalVariable.FdfParser.Profile.FdDict[FdName]
589 FdObj.GenFd()
590
591 GenFdsGlobalVariable.VerboseLogger("\n Generate other FV images! ")
592 if GenFds.OnlyGenerateThisFv != None and GenFds.OnlyGenerateThisFv.upper() in GenFdsGlobalVariable.FdfParser.Profile.FvDict.keys():
593 FvObj = GenFdsGlobalVariable.FdfParser.Profile.FvDict.get(GenFds.OnlyGenerateThisFv.upper())
594 if FvObj != None:
595 Buffer = StringIO.StringIO()
596 FvObj.AddToBuffer(Buffer)
597 Buffer.close()
598 return
599 elif GenFds.OnlyGenerateThisFv == None:
600 for FvName in GenFdsGlobalVariable.FdfParser.Profile.FvDict.keys():
601 Buffer = StringIO.StringIO('')
602 FvObj = GenFdsGlobalVariable.FdfParser.Profile.FvDict[FvName]
603 FvObj.AddToBuffer(Buffer)
604 Buffer.close()
605
606 if GenFds.OnlyGenerateThisFv == None and GenFds.OnlyGenerateThisFd == None and GenFds.OnlyGenerateThisCap == None:
607 if GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict != {}:
608 GenFdsGlobalVariable.VerboseLogger("\n Generate other Capsule images!")
609 for CapsuleName in GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict.keys():
610 CapsuleObj = GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict[CapsuleName]
611 CapsuleObj.GenCapsule()
612
613 if GenFdsGlobalVariable.FdfParser.Profile.OptRomDict != {}:
614 GenFdsGlobalVariable.VerboseLogger("\n Generate all Option ROM!")
615 for DriverName in GenFdsGlobalVariable.FdfParser.Profile.OptRomDict.keys():
616 OptRomObj = GenFdsGlobalVariable.FdfParser.Profile.OptRomDict[DriverName]
617 OptRomObj.AddToBuffer(None)
618 @staticmethod
619 def GenFfsMakefile(OutputDir, FdfParser, WorkSpace, ArchList, GlobalData):
620 GenFdsGlobalVariable.SetEnv(FdfParser, WorkSpace, ArchList, GlobalData)
621 for FdName in GenFdsGlobalVariable.FdfParser.Profile.FdDict.keys():
622 FdObj = GenFdsGlobalVariable.FdfParser.Profile.FdDict[FdName]
623 FdObj.GenFd(Flag=True)
624
625 for FvName in GenFdsGlobalVariable.FdfParser.Profile.FvDict.keys():
626 FvObj = GenFdsGlobalVariable.FdfParser.Profile.FvDict[FvName]
627 FvObj.AddToBuffer(Buffer=None, Flag=True)
628
629 if GenFdsGlobalVariable.FdfParser.Profile.OptRomDict != {}:
630 for DriverName in GenFdsGlobalVariable.FdfParser.Profile.OptRomDict.keys():
631 OptRomObj = GenFdsGlobalVariable.FdfParser.Profile.OptRomDict[DriverName]
632 OptRomObj.AddToBuffer(Buffer=None, Flag=True)
633
634 return GenFdsGlobalVariable.FfsCmdDict
635
636 ## GetFvBlockSize()
637 #
638 # @param FvObj Whose block size to get
639 # @retval int Block size value
640 #
641 def GetFvBlockSize(FvObj):
642 DefaultBlockSize = 0x1
643 FdObj = None
644 if GenFds.OnlyGenerateThisFd != None and GenFds.OnlyGenerateThisFd.upper() in GenFdsGlobalVariable.FdfParser.Profile.FdDict.keys():
645 FdObj = GenFdsGlobalVariable.FdfParser.Profile.FdDict[GenFds.OnlyGenerateThisFd.upper()]
646 if FdObj == None:
647 for ElementFd in GenFdsGlobalVariable.FdfParser.Profile.FdDict.values():
648 for ElementRegion in ElementFd.RegionList:
649 if ElementRegion.RegionType == 'FV':
650 for ElementRegionData in ElementRegion.RegionDataList:
651 if ElementRegionData != None and ElementRegionData.upper() == FvObj.UiFvName:
652 if FvObj.BlockSizeList != []:
653 return FvObj.BlockSizeList[0][0]
654 else:
655 return ElementRegion.BlockSizeOfRegion(ElementFd.BlockSizeList)
656 if FvObj.BlockSizeList != []:
657 return FvObj.BlockSizeList[0][0]
658 return DefaultBlockSize
659 else:
660 for ElementRegion in FdObj.RegionList:
661 if ElementRegion.RegionType == 'FV':
662 for ElementRegionData in ElementRegion.RegionDataList:
663 if ElementRegionData != None and ElementRegionData.upper() == FvObj.UiFvName:
664 if FvObj.BlockSizeList != []:
665 return FvObj.BlockSizeList[0][0]
666 else:
667 return ElementRegion.BlockSizeOfRegion(ElementFd.BlockSizeList)
668 return DefaultBlockSize
669
670 ## DisplayFvSpaceInfo()
671 #
672 # @param FvObj Whose block size to get
673 # @retval None
674 #
675 def DisplayFvSpaceInfo(FdfParser):
676
677 FvSpaceInfoList = []
678 MaxFvNameLength = 0
679 for FvName in FdfParser.Profile.FvDict:
680 if len(FvName) > MaxFvNameLength:
681 MaxFvNameLength = len(FvName)
682 FvSpaceInfoFileName = os.path.join(GenFdsGlobalVariable.FvDir, FvName.upper() + '.Fv.map')
683 if os.path.exists(FvSpaceInfoFileName):
684 FileLinesList = linecache.getlines(FvSpaceInfoFileName)
685 TotalFound = False
686 Total = ''
687 UsedFound = False
688 Used = ''
689 FreeFound = False
690 Free = ''
691 for Line in FileLinesList:
692 NameValue = Line.split('=')
693 if len(NameValue) == 2:
694 if NameValue[0].strip() == 'EFI_FV_TOTAL_SIZE':
695 TotalFound = True
696 Total = NameValue[1].strip()
697 if NameValue[0].strip() == 'EFI_FV_TAKEN_SIZE':
698 UsedFound = True
699 Used = NameValue[1].strip()
700 if NameValue[0].strip() == 'EFI_FV_SPACE_SIZE':
701 FreeFound = True
702 Free = NameValue[1].strip()
703
704 if TotalFound and UsedFound and FreeFound:
705 FvSpaceInfoList.append((FvName, Total, Used, Free))
706
707 GenFdsGlobalVariable.InfLogger('\nFV Space Information')
708 for FvSpaceInfo in FvSpaceInfoList:
709 Name = FvSpaceInfo[0]
710 TotalSizeValue = long(FvSpaceInfo[1], 0)
711 UsedSizeValue = long(FvSpaceInfo[2], 0)
712 FreeSizeValue = long(FvSpaceInfo[3], 0)
713 if UsedSizeValue == TotalSizeValue:
714 Percentage = '100'
715 else:
716 Percentage = str((UsedSizeValue + 0.0) / TotalSizeValue)[0:4].lstrip('0.')
717
718 GenFdsGlobalVariable.InfLogger(Name + ' ' + '[' + Percentage + '%Full] ' + str(TotalSizeValue) + ' total, ' + str(UsedSizeValue) + ' used, ' + str(FreeSizeValue) + ' free')
719
720 ## PreprocessImage()
721 #
722 # @param BuildDb Database from build meta data files
723 # @param DscFile modules from dsc file will be preprocessed
724 # @retval None
725 #
726 def PreprocessImage(BuildDb, DscFile):
727 PcdDict = BuildDb.BuildObject[DscFile, 'COMMON', GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag].Pcds
728 PcdValue = ''
729 for Key in PcdDict:
730 PcdObj = PcdDict[Key]
731 if PcdObj.TokenCName == 'PcdBsBaseAddress':
732 PcdValue = PcdObj.DefaultValue
733 break
734
735 if PcdValue == '':
736 return
737
738 Int64PcdValue = long(PcdValue, 0)
739 if Int64PcdValue == 0 or Int64PcdValue < -1:
740 return
741
742 TopAddress = 0
743 if Int64PcdValue > 0:
744 TopAddress = Int64PcdValue
745
746 ModuleDict = BuildDb.BuildObject[DscFile, 'COMMON', GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag].Modules
747 for Key in ModuleDict:
748 ModuleObj = BuildDb.BuildObject[Key, 'COMMON', GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]
749 print ModuleObj.BaseName + ' ' + ModuleObj.ModuleType
750
751 def GenerateGuidXRefFile(BuildDb, ArchList, FdfParserObj):
752 GuidXRefFileName = os.path.join(GenFdsGlobalVariable.FvDir, "Guid.xref")
753 GuidXRefFile = StringIO.StringIO('')
754 GuidDict = {}
755 ModuleList = []
756 FileGuidList = []
757 for Arch in ArchList:
758 PlatformDataBase = BuildDb.BuildObject[GenFdsGlobalVariable.ActivePlatform, Arch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]
759 for ModuleFile in PlatformDataBase.Modules:
760 Module = BuildDb.BuildObject[ModuleFile, Arch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]
761 if Module in ModuleList:
762 continue
763 else:
764 ModuleList.append(Module)
765 GuidXRefFile.write("%s %s\n" % (Module.Guid, Module.BaseName))
766 for key, item in Module.Protocols.items():
767 GuidDict[key] = item
768 for key, item in Module.Guids.items():
769 GuidDict[key] = item
770 for key, item in Module.Ppis.items():
771 GuidDict[key] = item
772 for FvName in FdfParserObj.Profile.FvDict:
773 for FfsObj in FdfParserObj.Profile.FvDict[FvName].FfsList:
774 if not isinstance(FfsObj, FfsFileStatement.FileStatement):
775 InfPath = PathClass(NormPath(mws.join(GenFdsGlobalVariable.WorkSpaceDir, FfsObj.InfFileName)))
776 FdfModule = BuildDb.BuildObject[InfPath, Arch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]
777 if FdfModule in ModuleList:
778 continue
779 else:
780 ModuleList.append(FdfModule)
781 GuidXRefFile.write("%s %s\n" % (FdfModule.Guid, FdfModule.BaseName))
782 for key, item in FdfModule.Protocols.items():
783 GuidDict[key] = item
784 for key, item in FdfModule.Guids.items():
785 GuidDict[key] = item
786 for key, item in FdfModule.Ppis.items():
787 GuidDict[key] = item
788 else:
789 FileStatementGuid = FfsObj.NameGuid
790 if FileStatementGuid in FileGuidList:
791 continue
792 else:
793 FileGuidList.append(FileStatementGuid)
794 Name = []
795 FfsPath = os.path.join(GenFdsGlobalVariable.FvDir, 'Ffs')
796 FfsPath = glob.glob(os.path.join(FfsPath, FileStatementGuid) + '*')
797 if not FfsPath:
798 continue
799 if not os.path.exists(FfsPath[0]):
800 continue
801 MatchDict = {}
802 ReFileEnds = re.compile('\S+(.ui)$|\S+(fv.sec.txt)$|\S+(.pe32.txt)$|\S+(.te.txt)$|\S+(.pic.txt)$|\S+(.raw.txt)$|\S+(.ffs.txt)$')
803 FileList = os.listdir(FfsPath[0])
804 for File in FileList:
805 Match = ReFileEnds.search(File)
806 if Match:
807 for Index in range(1, 8):
808 if Match.group(Index) and Match.group(Index) in MatchDict:
809 MatchDict[Match.group(Index)].append(File)
810 elif Match.group(Index):
811 MatchDict[Match.group(Index)] = [File]
812 if not MatchDict:
813 continue
814 if '.ui' in MatchDict:
815 for File in MatchDict['.ui']:
816 with open(os.path.join(FfsPath[0], File), 'rb') as F:
817 F.read()
818 length = F.tell()
819 F.seek(4)
820 TmpStr = unpack('%dh' % ((length - 4) / 2), F.read())
821 Name = ''.join([chr(c) for c in TmpStr[:-1]])
822 else:
823 FileList = []
824 if 'fv.sec.txt' in MatchDict:
825 FileList = MatchDict['fv.sec.txt']
826 elif '.pe32.txt' in MatchDict:
827 FileList = MatchDict['.pe32.txt']
828 elif '.te.txt' in MatchDict:
829 FileList = MatchDict['.te.txt']
830 elif '.pic.txt' in MatchDict:
831 FileList = MatchDict['.pic.txt']
832 elif '.raw.txt' in MatchDict:
833 FileList = MatchDict['.raw.txt']
834 elif '.ffs.txt' in MatchDict:
835 FileList = MatchDict['.ffs.txt']
836 else:
837 pass
838 for File in FileList:
839 with open(os.path.join(FfsPath[0], File), 'r') as F:
840 Name.append((F.read().split()[-1]))
841 if not Name:
842 continue
843
844 Name = ' '.join(Name) if type(Name) == type([]) else Name
845 GuidXRefFile.write("%s %s\n" %(FileStatementGuid, Name))
846
847 # Append GUIDs, Protocols, and PPIs to the Xref file
848 GuidXRefFile.write("\n")
849 for key, item in GuidDict.items():
850 GuidXRefFile.write("%s %s\n" % (GuidStructureStringToGuidString(item).upper(), key))
851
852 if GuidXRefFile.getvalue():
853 SaveFileOnChange(GuidXRefFileName, GuidXRefFile.getvalue(), False)
854 GenFdsGlobalVariable.InfLogger("\nGUID cross reference file can be found at %s" % GuidXRefFileName)
855 elif os.path.exists(GuidXRefFileName):
856 os.remove(GuidXRefFileName)
857 GuidXRefFile.close()
858
859 ##Define GenFd as static function
860 GenFd = staticmethod(GenFd)
861 GetFvBlockSize = staticmethod(GetFvBlockSize)
862 DisplayFvSpaceInfo = staticmethod(DisplayFvSpaceInfo)
863 PreprocessImage = staticmethod(PreprocessImage)
864 GenerateGuidXRefFile = staticmethod(GenerateGuidXRefFile)
865
866 if __name__ == '__main__':
867 r = main()
868 ## 0-127 is a safe return range, and 1 is a standard default error
869 if r < 0 or r > 127: r = 1
870 sys.exit(r)
871