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