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