]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - BaseTools/Source/Python/GenFds/GenFds.py
BaseTools: Replace StandardError with Expression
[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 __future__ import print_function\r
19from optparse import OptionParser\r
20import sys\r
21import Common.LongFilePathOs as os\r
22import linecache\r
23import FdfParser\r
24import Common.BuildToolError as BuildToolError\r
25from GenFdsGlobalVariable import GenFdsGlobalVariable\r
26from Workspace.WorkspaceDatabase import WorkspaceDatabase\r
27from Workspace.BuildClassObject import PcdClassObject\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.StringUtils 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 is not None:\r
73 EdkLogger.SetLevel(EdkLogger.VERBOSE)\r
74 GenFdsGlobalVariable.VerboseMode = True\r
75 \r
76 if Options.FixedAddress is not None:\r
77 GenFdsGlobalVariable.FixedLoadAddress = True\r
78 \r
79 if Options.quiet is not None:\r
80 EdkLogger.SetLevel(EdkLogger.QUIET)\r
81 if Options.debug is not 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 is 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:\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:\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 is 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:\r
221 GlobalData.gGlobalDefines["TARGET"] = GenFdsGlobalVariable.TargetName\r
222 if "TOOLCHAIN" not in GlobalData.gGlobalDefines:\r
223 GlobalData.gGlobalDefines["TOOLCHAIN"] = GenFdsGlobalVariable.ToolChainTag\r
224 if "TOOL_CHAIN_TAG" not in GlobalData.gGlobalDefines:\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, TAB_COMMON, Options.BuildTarget, Options.ToolChain].SupArchList\r
243\r
244 TargetArchList = set(BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, TAB_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, TAB_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:\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:\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:\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 is not 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 != BINARY_FILE_TYPE_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 as 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 as X:\r
343 if Options.debug is not 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 is 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:\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] == TAB_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 is 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 list(BuildOption.keys()):\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] == TAB_GUID:\r
452 ToolPathKey = Key + '_' + KeyList[3] + '_PATH'\r
453 ToolOptionKey = Key + '_' + KeyList[3] + '_FLAGS'\r
454 if ToolPathKey in BuildOption:\r
455 ToolPathTmp = BuildOption[ToolPathKey]\r
456 if ToolOptionKey in BuildOption:\r
457 ToolOption = BuildOption[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 is not None and GenFds.OnlyGenerateThisCap.upper() in GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict:\r
527 CapsuleObj = GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict[GenFds.OnlyGenerateThisCap.upper()]\r
528 if CapsuleObj is not None:\r
529 CapsuleObj.GenCapsule()\r
530 return\r
531\r
532 if GenFds.OnlyGenerateThisFd is not None and GenFds.OnlyGenerateThisFd.upper() in GenFdsGlobalVariable.FdfParser.Profile.FdDict:\r
533 FdObj = GenFdsGlobalVariable.FdfParser.Profile.FdDict[GenFds.OnlyGenerateThisFd.upper()]\r
534 if FdObj is not None:\r
535 FdObj.GenFd()\r
536 return\r
537 elif GenFds.OnlyGenerateThisFd is None and GenFds.OnlyGenerateThisFv is None:\r
538 for FdObj in GenFdsGlobalVariable.FdfParser.Profile.FdDict.values():\r
539 FdObj.GenFd()\r
540\r
541 GenFdsGlobalVariable.VerboseLogger("\n Generate other FV images! ")\r
542 if GenFds.OnlyGenerateThisFv is not None and GenFds.OnlyGenerateThisFv.upper() in GenFdsGlobalVariable.FdfParser.Profile.FvDict:\r
543 FvObj = GenFdsGlobalVariable.FdfParser.Profile.FvDict[GenFds.OnlyGenerateThisFv.upper()]\r
544 if FvObj is not None:\r
545 Buffer = StringIO.StringIO()\r
546 FvObj.AddToBuffer(Buffer)\r
547 Buffer.close()\r
548 return\r
549 elif GenFds.OnlyGenerateThisFv is None:\r
550 for FvObj in GenFdsGlobalVariable.FdfParser.Profile.FvDict.values():\r
551 Buffer = StringIO.StringIO('')\r
552 FvObj.AddToBuffer(Buffer)\r
553 Buffer.close()\r
554 \r
555 if GenFds.OnlyGenerateThisFv is None and GenFds.OnlyGenerateThisFd is None and GenFds.OnlyGenerateThisCap is None:\r
556 if GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict != {}:\r
557 GenFdsGlobalVariable.VerboseLogger("\n Generate other Capsule images!")\r
558 for CapsuleObj in GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict.values():\r
559 CapsuleObj.GenCapsule()\r
560\r
561 if GenFdsGlobalVariable.FdfParser.Profile.OptRomDict != {}:\r
562 GenFdsGlobalVariable.VerboseLogger("\n Generate all Option ROM!")\r
563 for OptRomObj in GenFdsGlobalVariable.FdfParser.Profile.OptRomDict.values():\r
564 OptRomObj.AddToBuffer(None)\r
565 @staticmethod\r
566 def GenFfsMakefile(OutputDir, FdfParser, WorkSpace, ArchList, GlobalData):\r
567 GenFdsGlobalVariable.SetEnv(FdfParser, WorkSpace, ArchList, GlobalData)\r
568 for FdObj in GenFdsGlobalVariable.FdfParser.Profile.FdDict.values():\r
569 FdObj.GenFd(Flag=True)\r
570\r
571 for FvObj in GenFdsGlobalVariable.FdfParser.Profile.FvDict.values():\r
572 FvObj.AddToBuffer(Buffer=None, Flag=True)\r
573\r
574 if GenFdsGlobalVariable.FdfParser.Profile.OptRomDict != {}:\r
575 for OptRomObj in GenFdsGlobalVariable.FdfParser.Profile.OptRomDict.values():\r
576 OptRomObj.AddToBuffer(Buffer=None, Flag=True)\r
577\r
578 return GenFdsGlobalVariable.FfsCmdDict\r
579\r
580 ## GetFvBlockSize()\r
581 #\r
582 # @param FvObj Whose block size to get\r
583 # @retval int Block size value\r
584 #\r
585 def GetFvBlockSize(FvObj):\r
586 DefaultBlockSize = 0x1\r
587 FdObj = None\r
588 if GenFds.OnlyGenerateThisFd is not None and GenFds.OnlyGenerateThisFd.upper() in GenFdsGlobalVariable.FdfParser.Profile.FdDict:\r
589 FdObj = GenFdsGlobalVariable.FdfParser.Profile.FdDict[GenFds.OnlyGenerateThisFd.upper()]\r
590 if FdObj is None:\r
591 for ElementFd in GenFdsGlobalVariable.FdfParser.Profile.FdDict.values():\r
592 for ElementRegion in ElementFd.RegionList:\r
593 if ElementRegion.RegionType == BINARY_FILE_TYPE_FV:\r
594 for ElementRegionData in ElementRegion.RegionDataList:\r
595 if ElementRegionData is not None and ElementRegionData.upper() == FvObj.UiFvName:\r
596 if FvObj.BlockSizeList != []:\r
597 return FvObj.BlockSizeList[0][0]\r
598 else:\r
599 return ElementRegion.BlockSizeOfRegion(ElementFd.BlockSizeList)\r
600 if FvObj.BlockSizeList != []:\r
601 return FvObj.BlockSizeList[0][0]\r
602 return DefaultBlockSize\r
603 else:\r
604 for ElementRegion in FdObj.RegionList:\r
605 if ElementRegion.RegionType == BINARY_FILE_TYPE_FV:\r
606 for ElementRegionData in ElementRegion.RegionDataList:\r
607 if ElementRegionData is not None and ElementRegionData.upper() == FvObj.UiFvName:\r
608 if FvObj.BlockSizeList != []:\r
609 return FvObj.BlockSizeList[0][0]\r
610 else:\r
611 return ElementRegion.BlockSizeOfRegion(ElementFd.BlockSizeList)\r
612 return DefaultBlockSize\r
613\r
614 ## DisplayFvSpaceInfo()\r
615 #\r
616 # @param FvObj Whose block size to get\r
617 # @retval None\r
618 #\r
619 def DisplayFvSpaceInfo(FdfParser):\r
620 \r
621 FvSpaceInfoList = []\r
622 MaxFvNameLength = 0\r
623 for FvName in FdfParser.Profile.FvDict:\r
624 if len(FvName) > MaxFvNameLength:\r
625 MaxFvNameLength = len(FvName)\r
626 FvSpaceInfoFileName = os.path.join(GenFdsGlobalVariable.FvDir, FvName.upper() + '.Fv.map')\r
627 if os.path.exists(FvSpaceInfoFileName):\r
628 FileLinesList = linecache.getlines(FvSpaceInfoFileName)\r
629 TotalFound = False\r
630 Total = ''\r
631 UsedFound = False\r
632 Used = ''\r
633 FreeFound = False\r
634 Free = ''\r
635 for Line in FileLinesList:\r
636 NameValue = Line.split('=')\r
637 if len(NameValue) == 2:\r
638 if NameValue[0].strip() == 'EFI_FV_TOTAL_SIZE':\r
639 TotalFound = True\r
640 Total = NameValue[1].strip()\r
641 if NameValue[0].strip() == 'EFI_FV_TAKEN_SIZE':\r
642 UsedFound = True\r
643 Used = NameValue[1].strip()\r
644 if NameValue[0].strip() == 'EFI_FV_SPACE_SIZE':\r
645 FreeFound = True\r
646 Free = NameValue[1].strip()\r
647 \r
648 if TotalFound and UsedFound and FreeFound:\r
649 FvSpaceInfoList.append((FvName, Total, Used, Free))\r
650 \r
651 GenFdsGlobalVariable.InfLogger('\nFV Space Information')\r
652 for FvSpaceInfo in FvSpaceInfoList:\r
653 Name = FvSpaceInfo[0]\r
654 TotalSizeValue = long(FvSpaceInfo[1], 0)\r
655 UsedSizeValue = long(FvSpaceInfo[2], 0)\r
656 FreeSizeValue = long(FvSpaceInfo[3], 0)\r
657 if UsedSizeValue == TotalSizeValue:\r
658 Percentage = '100'\r
659 else:\r
660 Percentage = str((UsedSizeValue + 0.0) / TotalSizeValue)[0:4].lstrip('0.')\r
661\r
662 GenFdsGlobalVariable.InfLogger(Name + ' ' + '[' + Percentage + '%Full] ' + str(TotalSizeValue) + ' total, ' + str(UsedSizeValue) + ' used, ' + str(FreeSizeValue) + ' free')\r
663\r
664 ## PreprocessImage()\r
665 #\r
666 # @param BuildDb Database from build meta data files\r
667 # @param DscFile modules from dsc file will be preprocessed\r
668 # @retval None\r
669 #\r
670 def PreprocessImage(BuildDb, DscFile):\r
671 PcdDict = BuildDb.BuildObject[DscFile, TAB_COMMON, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag].Pcds\r
672 PcdValue = ''\r
673 for Key in PcdDict:\r
674 PcdObj = PcdDict[Key]\r
675 if PcdObj.TokenCName == 'PcdBsBaseAddress':\r
676 PcdValue = PcdObj.DefaultValue\r
677 break\r
678 \r
679 if PcdValue == '':\r
680 return\r
681 \r
682 Int64PcdValue = long(PcdValue, 0)\r
683 if Int64PcdValue == 0 or Int64PcdValue < -1: \r
684 return\r
685 \r
686 TopAddress = 0\r
687 if Int64PcdValue > 0:\r
688 TopAddress = Int64PcdValue\r
689 \r
690 ModuleDict = BuildDb.BuildObject[DscFile, TAB_COMMON, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag].Modules\r
691 for Key in ModuleDict:\r
692 ModuleObj = BuildDb.BuildObject[Key, TAB_COMMON, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
693 print(ModuleObj.BaseName + ' ' + ModuleObj.ModuleType)\r
694\r
695 def GenerateGuidXRefFile(BuildDb, ArchList, FdfParserObj):\r
696 GuidXRefFileName = os.path.join(GenFdsGlobalVariable.FvDir, "Guid.xref")\r
697 GuidXRefFile = StringIO.StringIO('')\r
698 GuidDict = {}\r
699 ModuleList = []\r
700 FileGuidList = []\r
701 for Arch in ArchList:\r
702 PlatformDataBase = BuildDb.BuildObject[GenFdsGlobalVariable.ActivePlatform, Arch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
703 for ModuleFile in PlatformDataBase.Modules:\r
704 Module = BuildDb.BuildObject[ModuleFile, Arch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
705 if Module in ModuleList:\r
706 continue\r
707 else:\r
708 ModuleList.append(Module)\r
709 GuidXRefFile.write("%s %s\n" % (Module.Guid, Module.BaseName))\r
710 for key, item in Module.Protocols.items():\r
711 GuidDict[key] = item\r
712 for key, item in Module.Guids.items():\r
713 GuidDict[key] = item\r
714 for key, item in Module.Ppis.items():\r
715 GuidDict[key] = item\r
716 for FvName in FdfParserObj.Profile.FvDict:\r
717 for FfsObj in FdfParserObj.Profile.FvDict[FvName].FfsList:\r
718 if not isinstance(FfsObj, FfsFileStatement.FileStatement):\r
719 InfPath = PathClass(NormPath(mws.join(GenFdsGlobalVariable.WorkSpaceDir, FfsObj.InfFileName)))\r
720 FdfModule = BuildDb.BuildObject[InfPath, Arch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
721 if FdfModule in ModuleList:\r
722 continue\r
723 else:\r
724 ModuleList.append(FdfModule)\r
725 GuidXRefFile.write("%s %s\n" % (FdfModule.Guid, FdfModule.BaseName))\r
726 for key, item in FdfModule.Protocols.items():\r
727 GuidDict[key] = item\r
728 for key, item in FdfModule.Guids.items():\r
729 GuidDict[key] = item\r
730 for key, item in FdfModule.Ppis.items():\r
731 GuidDict[key] = item\r
732 else:\r
733 FileStatementGuid = FfsObj.NameGuid\r
734 if FileStatementGuid in FileGuidList:\r
735 continue\r
736 else:\r
737 FileGuidList.append(FileStatementGuid)\r
738 Name = []\r
739 FfsPath = os.path.join(GenFdsGlobalVariable.FvDir, 'Ffs')\r
740 FfsPath = glob.glob(os.path.join(FfsPath, FileStatementGuid) + '*')\r
741 if not FfsPath:\r
742 continue\r
743 if not os.path.exists(FfsPath[0]):\r
744 continue\r
745 MatchDict = {}\r
746 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
747 FileList = os.listdir(FfsPath[0])\r
748 for File in FileList:\r
749 Match = ReFileEnds.search(File)\r
750 if Match:\r
751 for Index in range(1, 8):\r
752 if Match.group(Index) and Match.group(Index) in MatchDict:\r
753 MatchDict[Match.group(Index)].append(File)\r
754 elif Match.group(Index):\r
755 MatchDict[Match.group(Index)] = [File]\r
756 if not MatchDict:\r
757 continue\r
758 if '.ui' in MatchDict:\r
759 for File in MatchDict['.ui']:\r
760 with open(os.path.join(FfsPath[0], File), 'rb') as F:\r
761 F.read()\r
762 length = F.tell()\r
763 F.seek(4)\r
764 TmpStr = unpack('%dh' % ((length - 4) / 2), F.read())\r
765 Name = ''.join(chr(c) for c in TmpStr[:-1])\r
766 else:\r
767 FileList = []\r
768 if 'fv.sec.txt' in MatchDict:\r
769 FileList = MatchDict['fv.sec.txt']\r
770 elif '.pe32.txt' in MatchDict:\r
771 FileList = MatchDict['.pe32.txt']\r
772 elif '.te.txt' in MatchDict:\r
773 FileList = MatchDict['.te.txt']\r
774 elif '.pic.txt' in MatchDict:\r
775 FileList = MatchDict['.pic.txt']\r
776 elif '.raw.txt' in MatchDict:\r
777 FileList = MatchDict['.raw.txt']\r
778 elif '.ffs.txt' in MatchDict:\r
779 FileList = MatchDict['.ffs.txt']\r
780 else:\r
781 pass\r
782 for File in FileList:\r
783 with open(os.path.join(FfsPath[0], File), 'r') as F:\r
784 Name.append((F.read().split()[-1]))\r
785 if not Name:\r
786 continue\r
787\r
788 Name = ' '.join(Name) if type(Name) == type([]) else Name\r
789 GuidXRefFile.write("%s %s\n" %(FileStatementGuid, Name))\r
790\r
791 # Append GUIDs, Protocols, and PPIs to the Xref file\r
792 GuidXRefFile.write("\n")\r
793 for key, item in GuidDict.items():\r
794 GuidXRefFile.write("%s %s\n" % (GuidStructureStringToGuidString(item).upper(), key))\r
795\r
796 if GuidXRefFile.getvalue():\r
797 SaveFileOnChange(GuidXRefFileName, GuidXRefFile.getvalue(), False)\r
798 GenFdsGlobalVariable.InfLogger("\nGUID cross reference file can be found at %s" % GuidXRefFileName)\r
799 elif os.path.exists(GuidXRefFileName):\r
800 os.remove(GuidXRefFileName)\r
801 GuidXRefFile.close()\r
802\r
803 ##Define GenFd as static function\r
804 GenFd = staticmethod(GenFd)\r
805 GetFvBlockSize = staticmethod(GetFvBlockSize)\r
806 DisplayFvSpaceInfo = staticmethod(DisplayFvSpaceInfo)\r
807 PreprocessImage = staticmethod(PreprocessImage)\r
808 GenerateGuidXRefFile = staticmethod(GenerateGuidXRefFile)\r
809\r
810if __name__ == '__main__':\r
811 r = main()\r
812 ## 0-127 is a safe return range, and 1 is a standard default error\r
813 if r < 0 or r > 127: r = 1\r
814 sys.exit(r)\r
815\r