]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/GenFds/GenFds.py
BaseTools: Add mixed PCD support feature
[mirror_edk2.git] / BaseTools / Source / Python / GenFds / GenFds.py
CommitLineData
f51461c8
LG
1## @file\r
2# generate flash image\r
3#\r
3a0f8bde 4# Copyright (c) 2007 - 2016, 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
18from optparse import OptionParser\r
19import sys\r
1be2ed90 20import Common.LongFilePathOs as os\r
f51461c8
LG
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
33import Common.DataType\r
34import Common.GlobalData as GlobalData\r
35from Common import EdkLogger\r
36from Common.String import *\r
47fea6af 37from Common.Misc import DirCache, PathClass\r
f51461c8 38from Common.Misc import SaveFileOnChange\r
97fa0ee9 39from Common.Misc import ClearDuplicatedInf\r
e4ac870f 40from Common.Misc import GuidStructureStringToGuidString\r
f51461c8 41from Common.BuildVersion import gBUILD_VERSION\r
05cc51ad 42from Common.MultipleWorkspace import MultipleWorkspace as mws\r
f51461c8
LG
43\r
44## Version and Copyright\r
45versionNumber = "1.0" + ' ' + gBUILD_VERSION\r
46__version__ = "%prog Version " + versionNumber\r
1be2ed90 47__copyright__ = "Copyright (c) 2007 - 2014, Intel Corporation All rights reserved."\r
f51461c8
LG
48\r
49## Tool entrance method\r
50#\r
51# This method mainly dispatch specific methods per the command line options.\r
52# If no error found, return zero value so the caller of this tool can know\r
53# if it's executed successfully or not.\r
54#\r
55# @retval 0 Tool was successful\r
56# @retval 1 Tool failed\r
57#\r
58def main():\r
59 global Options\r
60 Options = myOptionParser()\r
61\r
62 global Workspace\r
63 Workspace = ""\r
64 ArchList = None\r
65 ReturnCode = 0\r
66\r
67 EdkLogger.Initialize()\r
68 try:\r
69 if Options.verbose != None:\r
70 EdkLogger.SetLevel(EdkLogger.VERBOSE)\r
71 GenFdsGlobalVariable.VerboseMode = True\r
72 \r
73 if Options.FixedAddress != None:\r
74 GenFdsGlobalVariable.FixedLoadAddress = True\r
75 \r
76 if Options.quiet != None:\r
77 EdkLogger.SetLevel(EdkLogger.QUIET)\r
78 if Options.debug != None:\r
79 EdkLogger.SetLevel(Options.debug + 1)\r
80 GenFdsGlobalVariable.DebugLevel = Options.debug\r
81 else:\r
82 EdkLogger.SetLevel(EdkLogger.INFO)\r
83\r
84 if (Options.Workspace == None):\r
85 EdkLogger.error("GenFds", OPTION_MISSING, "WORKSPACE not defined",\r
86 ExtraData="Please use '-w' switch to pass it or set the WORKSPACE environment variable.")\r
87 elif not os.path.exists(Options.Workspace):\r
88 EdkLogger.error("GenFds", PARAMETER_INVALID, "WORKSPACE is invalid",\r
89 ExtraData="Please use '-w' switch to pass it or set the WORKSPACE environment variable.")\r
90 else:\r
91 Workspace = os.path.normcase(Options.Workspace)\r
92 GenFdsGlobalVariable.WorkSpaceDir = Workspace\r
93 if 'EDK_SOURCE' in os.environ.keys():\r
94 GenFdsGlobalVariable.EdkSourceDir = os.path.normcase(os.environ['EDK_SOURCE'])\r
95 if (Options.debug):\r
47fea6af 96 GenFdsGlobalVariable.VerboseLogger("Using Workspace:" + Workspace)\r
f51461c8 97 os.chdir(GenFdsGlobalVariable.WorkSpaceDir)\r
05cc51ad
LY
98 \r
99 # set multiple workspace\r
100 PackagesPath = os.getenv("PACKAGES_PATH")\r
101 mws.setWs(GenFdsGlobalVariable.WorkSpaceDir, PackagesPath)\r
f51461c8
LG
102\r
103 if (Options.filename):\r
104 FdfFilename = Options.filename\r
105 FdfFilename = GenFdsGlobalVariable.ReplaceWorkspaceMacro(FdfFilename)\r
106\r
107 if FdfFilename[0:2] == '..':\r
108 FdfFilename = os.path.realpath(FdfFilename)\r
47fea6af 109 if not os.path.isabs(FdfFilename):\r
05cc51ad 110 FdfFilename = mws.join(GenFdsGlobalVariable.WorkSpaceDir, FdfFilename)\r
f51461c8
LG
111 if not os.path.exists(FdfFilename):\r
112 EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=FdfFilename)\r
f51461c8
LG
113\r
114 GenFdsGlobalVariable.FdfFile = FdfFilename\r
115 GenFdsGlobalVariable.FdfFileTimeStamp = os.path.getmtime(FdfFilename)\r
116 else:\r
117 EdkLogger.error("GenFds", OPTION_MISSING, "Missing FDF filename")\r
118\r
119 if (Options.BuildTarget):\r
120 GenFdsGlobalVariable.TargetName = Options.BuildTarget\r
121 else:\r
122 EdkLogger.error("GenFds", OPTION_MISSING, "Missing build target")\r
123\r
124 if (Options.ToolChain):\r
125 GenFdsGlobalVariable.ToolChainTag = Options.ToolChain\r
126 else:\r
127 EdkLogger.error("GenFds", OPTION_MISSING, "Missing tool chain tag")\r
128\r
129 if (Options.activePlatform):\r
130 ActivePlatform = Options.activePlatform\r
131 ActivePlatform = GenFdsGlobalVariable.ReplaceWorkspaceMacro(ActivePlatform)\r
132\r
133 if ActivePlatform[0:2] == '..':\r
134 ActivePlatform = os.path.realpath(ActivePlatform)\r
135\r
136 if not os.path.isabs (ActivePlatform):\r
05cc51ad 137 ActivePlatform = mws.join(GenFdsGlobalVariable.WorkSpaceDir, ActivePlatform)\r
f51461c8
LG
138\r
139 if not os.path.exists(ActivePlatform) :\r
140 EdkLogger.error("GenFds", FILE_NOT_FOUND, "ActivePlatform doesn't exist!")\r
f51461c8
LG
141 else:\r
142 EdkLogger.error("GenFds", OPTION_MISSING, "Missing active platform")\r
143\r
e642ceb8 144 GenFdsGlobalVariable.ActivePlatform = PathClass(NormPath(ActivePlatform))\r
f51461c8 145\r
97fa0ee9
YL
146 if (Options.ConfDirectory):\r
147 # Get alternate Conf location, if it is absolute, then just use the absolute directory name\r
148 ConfDirectoryPath = os.path.normpath(Options.ConfDirectory)\r
149 if ConfDirectoryPath.startswith('"'):\r
150 ConfDirectoryPath = ConfDirectoryPath[1:]\r
151 if ConfDirectoryPath.endswith('"'):\r
152 ConfDirectoryPath = ConfDirectoryPath[:-1]\r
153 if not os.path.isabs(ConfDirectoryPath):\r
154 # Since alternate directory name is not absolute, the alternate directory is located within the WORKSPACE\r
155 # This also handles someone specifying the Conf directory in the workspace. Using --conf=Conf\r
156 ConfDirectoryPath = os.path.join(GenFdsGlobalVariable.WorkSpaceDir, ConfDirectoryPath)\r
157 else:\r
158 # Get standard WORKSPACE/Conf, use the absolute path to the WORKSPACE/Conf\r
05cc51ad 159 ConfDirectoryPath = mws.join(GenFdsGlobalVariable.WorkSpaceDir, 'Conf')\r
97fa0ee9
YL
160 GenFdsGlobalVariable.ConfDir = ConfDirectoryPath\r
161 BuildConfigurationFile = os.path.normpath(os.path.join(ConfDirectoryPath, "target.txt"))\r
f51461c8
LG
162 if os.path.isfile(BuildConfigurationFile) == True:\r
163 TargetTxtClassObject.TargetTxtClassObject(BuildConfigurationFile)\r
164 else:\r
165 EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=BuildConfigurationFile)\r
166\r
97fa0ee9
YL
167 #Set global flag for build mode\r
168 GlobalData.gIgnoreSource = Options.IgnoreSources\r
169\r
f51461c8
LG
170 if Options.Macros:\r
171 for Pair in Options.Macros:\r
97fa0ee9
YL
172 if Pair.startswith('"'):\r
173 Pair = Pair[1:]\r
174 if Pair.endswith('"'):\r
175 Pair = Pair[:-1]\r
f51461c8
LG
176 List = Pair.split('=')\r
177 if len(List) == 2:\r
178 if List[0].strip() == "EFI_SOURCE":\r
179 GlobalData.gEfiSource = List[1].strip()\r
180 GlobalData.gGlobalDefines["EFI_SOURCE"] = GlobalData.gEfiSource\r
181 continue\r
182 elif List[0].strip() == "EDK_SOURCE":\r
183 GlobalData.gEdkSource = List[1].strip()\r
184 GlobalData.gGlobalDefines["EDK_SOURCE"] = GlobalData.gEdkSource\r
185 continue\r
186 elif List[0].strip() in ["WORKSPACE", "TARGET", "TOOLCHAIN"]:\r
187 GlobalData.gGlobalDefines[List[0].strip()] = List[1].strip()\r
188 else:\r
189 GlobalData.gCommandLineDefines[List[0].strip()] = List[1].strip()\r
190 else:\r
191 GlobalData.gCommandLineDefines[List[0].strip()] = "TRUE"\r
192 os.environ["WORKSPACE"] = Workspace\r
193\r
194 """call Workspace build create database"""\r
97fa0ee9
YL
195 GlobalData.gDatabasePath = os.path.normpath(os.path.join(ConfDirectoryPath, GlobalData.gDatabasePath))\r
196 BuildWorkSpace = WorkspaceDatabase(GlobalData.gDatabasePath)\r
f51461c8
LG
197 BuildWorkSpace.InitDatabase()\r
198 \r
199 #\r
200 # Get files real name in workspace dir\r
201 #\r
202 GlobalData.gAllFiles = DirCache(Workspace)\r
203 GlobalData.gWorkspace = Workspace\r
204\r
205 if (Options.archList) :\r
206 ArchList = Options.archList.split(',')\r
207 else:\r
208# EdkLogger.error("GenFds", OPTION_MISSING, "Missing build ARCH")\r
209 ArchList = BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, 'COMMON', Options.BuildTarget, Options.ToolChain].SupArchList\r
210\r
211 TargetArchList = set(BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, 'COMMON', Options.BuildTarget, Options.ToolChain].SupArchList) & set(ArchList)\r
212 if len(TargetArchList) == 0:\r
213 EdkLogger.error("GenFds", GENFDS_ERROR, "Target ARCH %s not in platform supported ARCH %s" % (str(ArchList), str(BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, 'COMMON'].SupArchList)))\r
214 \r
215 for Arch in ArchList:\r
216 GenFdsGlobalVariable.OutputDirFromDscDict[Arch] = NormPath(BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, Arch, Options.BuildTarget, Options.ToolChain].OutputDirectory)\r
217 GenFdsGlobalVariable.PlatformName = BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, Arch, Options.BuildTarget, Options.ToolChain].PlatformName\r
218\r
219 if (Options.outputDir):\r
220 OutputDirFromCommandLine = GenFdsGlobalVariable.ReplaceWorkspaceMacro(Options.outputDir)\r
221 if not os.path.isabs (OutputDirFromCommandLine):\r
222 OutputDirFromCommandLine = os.path.join(GenFdsGlobalVariable.WorkSpaceDir, OutputDirFromCommandLine)\r
223 for Arch in ArchList:\r
224 GenFdsGlobalVariable.OutputDirDict[Arch] = OutputDirFromCommandLine\r
225 else:\r
226 for Arch in ArchList:\r
227 GenFdsGlobalVariable.OutputDirDict[Arch] = os.path.join(GenFdsGlobalVariable.OutputDirFromDscDict[Arch], GenFdsGlobalVariable.TargetName + '_' + GenFdsGlobalVariable.ToolChainTag)\r
228\r
229 for Key in GenFdsGlobalVariable.OutputDirDict:\r
230 OutputDir = GenFdsGlobalVariable.OutputDirDict[Key]\r
231 if OutputDir[0:2] == '..':\r
232 OutputDir = os.path.realpath(OutputDir)\r
233\r
234 if OutputDir[1] != ':':\r
235 OutputDir = os.path.join (GenFdsGlobalVariable.WorkSpaceDir, OutputDir)\r
236\r
237 if not os.path.exists(OutputDir):\r
238 EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=OutputDir)\r
239 GenFdsGlobalVariable.OutputDirDict[Key] = OutputDir\r
240\r
241 """ Parse Fdf file, has to place after build Workspace as FDF may contain macros from DSC file """\r
242 FdfParserObj = FdfParser.FdfParser(FdfFilename)\r
243 FdfParserObj.ParseFile()\r
244\r
245 if FdfParserObj.CycleReferenceCheck():\r
246 EdkLogger.error("GenFds", FORMAT_NOT_SUPPORTED, "Cycle Reference Detected in FDF file")\r
247\r
248 if (Options.uiFdName) :\r
249 if Options.uiFdName.upper() in FdfParserObj.Profile.FdDict.keys():\r
250 GenFds.OnlyGenerateThisFd = Options.uiFdName\r
251 else:\r
252 EdkLogger.error("GenFds", OPTION_VALUE_INVALID,\r
253 "No such an FD in FDF file: %s" % Options.uiFdName)\r
254\r
255 if (Options.uiFvName) :\r
256 if Options.uiFvName.upper() in FdfParserObj.Profile.FvDict.keys():\r
257 GenFds.OnlyGenerateThisFv = Options.uiFvName\r
258 else:\r
259 EdkLogger.error("GenFds", OPTION_VALUE_INVALID,\r
260 "No such an FV in FDF file: %s" % Options.uiFvName)\r
261\r
262 if (Options.uiCapName) :\r
263 if Options.uiCapName.upper() in FdfParserObj.Profile.CapsuleDict.keys():\r
264 GenFds.OnlyGenerateThisCap = Options.uiCapName\r
265 else:\r
266 EdkLogger.error("GenFds", OPTION_VALUE_INVALID,\r
267 "No such a Capsule in FDF file: %s" % Options.uiCapName)\r
268\r
269 """Modify images from build output if the feature of loading driver at fixed address is on."""\r
270 if GenFdsGlobalVariable.FixedLoadAddress:\r
271 GenFds.PreprocessImage(BuildWorkSpace, GenFdsGlobalVariable.ActivePlatform)\r
272 """Call GenFds"""\r
273 GenFds.GenFd('', FdfParserObj, BuildWorkSpace, ArchList)\r
274\r
275 """Generate GUID cross reference file"""\r
276 GenFds.GenerateGuidXRefFile(BuildWorkSpace, ArchList)\r
277\r
278 """Display FV space info."""\r
279 GenFds.DisplayFvSpaceInfo(FdfParserObj)\r
280\r
281 except FdfParser.Warning, X:\r
47fea6af 282 EdkLogger.error(X.ToolName, FORMAT_INVALID, File=X.FileName, Line=X.LineNumber, ExtraData=X.Message, RaiseError=False)\r
f51461c8
LG
283 ReturnCode = FORMAT_INVALID\r
284 except FatalError, X:\r
285 if Options.debug != None:\r
286 import traceback\r
287 EdkLogger.quiet(traceback.format_exc())\r
288 ReturnCode = X.args[0]\r
289 except:\r
290 import traceback\r
291 EdkLogger.error(\r
292 "\nPython",\r
293 CODE_ERROR,\r
294 "Tools code failure",\r
3a0f8bde 295 ExtraData="Please send email to edk2-devel@lists.01.org for help, attaching following call stack trace!\n",\r
f51461c8
LG
296 RaiseError=False\r
297 )\r
298 EdkLogger.quiet(traceback.format_exc())\r
299 ReturnCode = CODE_ERROR\r
97fa0ee9
YL
300 finally:\r
301 ClearDuplicatedInf()\r
f51461c8
LG
302 return ReturnCode\r
303\r
304gParamCheck = []\r
305def SingleCheckCallback(option, opt_str, value, parser):\r
306 if option not in gParamCheck:\r
307 setattr(parser.values, option.dest, value)\r
308 gParamCheck.append(option)\r
309 else:\r
310 parser.error("Option %s only allows one instance in command line!" % option)\r
311 \r
312## Parse command line options\r
313#\r
314# Using standard Python module optparse to parse command line option of this tool.\r
315#\r
316# @retval Opt A optparse.Values object containing the parsed options\r
317# @retval Args Target of build command\r
318#\r
319def myOptionParser():\r
320 usage = "%prog [options] -f input_file -a arch_list -b build_target -p active_platform -t tool_chain_tag -D \"MacroName [= MacroValue]\""\r
47fea6af 321 Parser = OptionParser(usage=usage, description=__copyright__, version="%prog " + str(versionNumber))\r
f51461c8
LG
322 Parser.add_option("-f", "--file", dest="filename", type="string", help="Name of FDF file to convert", action="callback", callback=SingleCheckCallback)\r
323 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
324 Parser.add_option("-q", "--quiet", action="store_true", type=None, help="Disable all messages except FATAL ERRORS.")\r
325 Parser.add_option("-v", "--verbose", action="store_true", type=None, help="Turn on verbose output with informational messages printed.")\r
326 Parser.add_option("-d", "--debug", action="store", type="int", help="Enable debug messages at specified level.")\r
327 Parser.add_option("-p", "--platform", type="string", dest="activePlatform", help="Set the ACTIVE_PLATFORM, overrides target.txt ACTIVE_PLATFORM setting.",\r
328 action="callback", callback=SingleCheckCallback)\r
329 Parser.add_option("-w", "--workspace", type="string", dest="Workspace", default=os.environ.get('WORKSPACE'), help="Set the WORKSPACE",\r
330 action="callback", callback=SingleCheckCallback)\r
331 Parser.add_option("-o", "--outputDir", type="string", dest="outputDir", help="Name of Build Output directory",\r
332 action="callback", callback=SingleCheckCallback)\r
333 Parser.add_option("-r", "--rom_image", dest="uiFdName", help="Build the image using the [FD] section named by FdUiName.")\r
334 Parser.add_option("-i", "--FvImage", dest="uiFvName", help="Build the FV image using the [FV] section named by UiFvName")\r
335 Parser.add_option("-C", "--CapsuleImage", dest="uiCapName", help="Build the Capsule image using the [Capsule] section named by UiCapName")\r
336 Parser.add_option("-b", "--buildtarget", type="string", dest="BuildTarget", help="Set the build TARGET, overrides target.txt TARGET setting.",\r
337 action="callback", callback=SingleCheckCallback)\r
338 Parser.add_option("-t", "--tagname", type="string", dest="ToolChain", help="Using the tools: TOOL_CHAIN_TAG name to build the platform.",\r
339 action="callback", callback=SingleCheckCallback)\r
340 Parser.add_option("-D", "--define", action="append", type="string", dest="Macros", help="Macro: \"Name [= Value]\".")\r
341 Parser.add_option("-s", "--specifyaddress", dest="FixedAddress", action="store_true", type=None, help="Specify driver load address.")\r
97fa0ee9
YL
342 Parser.add_option("--conf", action="store", type="string", dest="ConfDirectory", help="Specify the customized Conf directory.")\r
343 Parser.add_option("--ignore-sources", action="store_true", dest="IgnoreSources", default=False, help="Focus to a binary build and ignore all source files")\r
344\r
f51461c8
LG
345 (Options, args) = Parser.parse_args()\r
346 return Options\r
347\r
348## The class implementing the EDK2 flash image generation process\r
349#\r
350# This process includes:\r
351# 1. Collect workspace information, includes platform and module information\r
352# 2. Call methods of Fd class to generate FD\r
353# 3. Call methods of Fv class to generate FV that not belong to FD\r
354#\r
355class GenFds :\r
356 FdfParsef = None\r
357 # FvName, FdName, CapName in FDF, Image file name\r
358 ImageBinDict = {}\r
359 OnlyGenerateThisFd = None\r
360 OnlyGenerateThisFv = None\r
361 OnlyGenerateThisCap = None\r
362\r
363 ## GenFd()\r
364 #\r
365 # @param OutputDir Output directory\r
366 # @param FdfParser FDF contents parser\r
367 # @param Workspace The directory of workspace\r
368 # @param ArchList The Arch list of platform\r
369 #\r
370 def GenFd (OutputDir, FdfParser, WorkSpace, ArchList):\r
371 GenFdsGlobalVariable.SetDir ('', FdfParser, WorkSpace, ArchList)\r
372\r
373 GenFdsGlobalVariable.VerboseLogger(" Generate all Fd images and their required FV and Capsule images!")\r
374 if GenFds.OnlyGenerateThisCap != None and GenFds.OnlyGenerateThisCap.upper() in GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict.keys():\r
375 CapsuleObj = GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict.get(GenFds.OnlyGenerateThisCap.upper())\r
376 if CapsuleObj != None:\r
377 CapsuleObj.GenCapsule()\r
378 return\r
379\r
380 if GenFds.OnlyGenerateThisFd != None and GenFds.OnlyGenerateThisFd.upper() in GenFdsGlobalVariable.FdfParser.Profile.FdDict.keys():\r
381 FdObj = GenFdsGlobalVariable.FdfParser.Profile.FdDict.get(GenFds.OnlyGenerateThisFd.upper())\r
382 if FdObj != None:\r
383 FdObj.GenFd()\r
384 return\r
385 elif GenFds.OnlyGenerateThisFd == None and GenFds.OnlyGenerateThisFv == None:\r
386 for FdName in GenFdsGlobalVariable.FdfParser.Profile.FdDict.keys():\r
387 FdObj = GenFdsGlobalVariable.FdfParser.Profile.FdDict[FdName]\r
388 FdObj.GenFd()\r
389\r
390 GenFdsGlobalVariable.VerboseLogger("\n Generate other FV images! ")\r
391 if GenFds.OnlyGenerateThisFv != None and GenFds.OnlyGenerateThisFv.upper() in GenFdsGlobalVariable.FdfParser.Profile.FvDict.keys():\r
392 FvObj = GenFdsGlobalVariable.FdfParser.Profile.FvDict.get(GenFds.OnlyGenerateThisFv.upper())\r
393 if FvObj != None:\r
394 Buffer = StringIO.StringIO()\r
395 FvObj.AddToBuffer(Buffer)\r
396 Buffer.close()\r
397 return\r
398 elif GenFds.OnlyGenerateThisFv == None:\r
399 for FvName in GenFdsGlobalVariable.FdfParser.Profile.FvDict.keys():\r
400 Buffer = StringIO.StringIO('')\r
401 FvObj = GenFdsGlobalVariable.FdfParser.Profile.FvDict[FvName]\r
402 FvObj.AddToBuffer(Buffer)\r
403 Buffer.close()\r
404 \r
405 if GenFds.OnlyGenerateThisFv == None and GenFds.OnlyGenerateThisFd == None and GenFds.OnlyGenerateThisCap == None:\r
406 if GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict != {}:\r
407 GenFdsGlobalVariable.VerboseLogger("\n Generate other Capsule images!")\r
408 for CapsuleName in GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict.keys():\r
409 CapsuleObj = GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict[CapsuleName]\r
410 CapsuleObj.GenCapsule()\r
411\r
412 if GenFdsGlobalVariable.FdfParser.Profile.OptRomDict != {}:\r
413 GenFdsGlobalVariable.VerboseLogger("\n Generate all Option ROM!")\r
414 for DriverName in GenFdsGlobalVariable.FdfParser.Profile.OptRomDict.keys():\r
415 OptRomObj = GenFdsGlobalVariable.FdfParser.Profile.OptRomDict[DriverName]\r
416 OptRomObj.AddToBuffer(None)\r
417\r
418 ## GetFvBlockSize()\r
419 #\r
420 # @param FvObj Whose block size to get\r
421 # @retval int Block size value\r
422 #\r
423 def GetFvBlockSize(FvObj):\r
424 DefaultBlockSize = 0x1\r
425 FdObj = None\r
426 if GenFds.OnlyGenerateThisFd != None and GenFds.OnlyGenerateThisFd.upper() in GenFdsGlobalVariable.FdfParser.Profile.FdDict.keys():\r
427 FdObj = GenFdsGlobalVariable.FdfParser.Profile.FdDict[GenFds.OnlyGenerateThisFd.upper()]\r
428 if FdObj == None:\r
429 for ElementFd in GenFdsGlobalVariable.FdfParser.Profile.FdDict.values():\r
430 for ElementRegion in ElementFd.RegionList:\r
431 if ElementRegion.RegionType == 'FV':\r
432 for ElementRegionData in ElementRegion.RegionDataList:\r
433 if ElementRegionData != None and ElementRegionData.upper() == FvObj.UiFvName:\r
434 if FvObj.BlockSizeList != []:\r
435 return FvObj.BlockSizeList[0][0]\r
436 else:\r
437 return ElementRegion.BlockSizeOfRegion(ElementFd.BlockSizeList)\r
438 if FvObj.BlockSizeList != []:\r
439 return FvObj.BlockSizeList[0][0]\r
440 return DefaultBlockSize\r
441 else:\r
442 for ElementRegion in FdObj.RegionList:\r
443 if ElementRegion.RegionType == 'FV':\r
444 for ElementRegionData in ElementRegion.RegionDataList:\r
445 if ElementRegionData != None and ElementRegionData.upper() == FvObj.UiFvName:\r
446 if FvObj.BlockSizeList != []:\r
447 return FvObj.BlockSizeList[0][0]\r
448 else:\r
449 return ElementRegion.BlockSizeOfRegion(ElementFd.BlockSizeList)\r
450 return DefaultBlockSize\r
451\r
452 ## DisplayFvSpaceInfo()\r
453 #\r
454 # @param FvObj Whose block size to get\r
455 # @retval None\r
456 #\r
457 def DisplayFvSpaceInfo(FdfParser):\r
458 \r
459 FvSpaceInfoList = []\r
460 MaxFvNameLength = 0\r
461 for FvName in FdfParser.Profile.FvDict:\r
462 if len(FvName) > MaxFvNameLength:\r
463 MaxFvNameLength = len(FvName)\r
464 FvSpaceInfoFileName = os.path.join(GenFdsGlobalVariable.FvDir, FvName.upper() + '.Fv.map')\r
465 if os.path.exists(FvSpaceInfoFileName):\r
466 FileLinesList = linecache.getlines(FvSpaceInfoFileName)\r
467 TotalFound = False\r
468 Total = ''\r
469 UsedFound = False\r
470 Used = ''\r
471 FreeFound = False\r
472 Free = ''\r
473 for Line in FileLinesList:\r
474 NameValue = Line.split('=')\r
475 if len(NameValue) == 2:\r
476 if NameValue[0].strip() == 'EFI_FV_TOTAL_SIZE':\r
477 TotalFound = True\r
478 Total = NameValue[1].strip()\r
479 if NameValue[0].strip() == 'EFI_FV_TAKEN_SIZE':\r
480 UsedFound = True\r
481 Used = NameValue[1].strip()\r
482 if NameValue[0].strip() == 'EFI_FV_SPACE_SIZE':\r
483 FreeFound = True\r
484 Free = NameValue[1].strip()\r
485 \r
486 if TotalFound and UsedFound and FreeFound:\r
487 FvSpaceInfoList.append((FvName, Total, Used, Free))\r
488 \r
489 GenFdsGlobalVariable.InfLogger('\nFV Space Information')\r
490 for FvSpaceInfo in FvSpaceInfoList:\r
491 Name = FvSpaceInfo[0]\r
492 TotalSizeValue = long(FvSpaceInfo[1], 0)\r
493 UsedSizeValue = long(FvSpaceInfo[2], 0)\r
494 FreeSizeValue = long(FvSpaceInfo[3], 0)\r
495 if UsedSizeValue == TotalSizeValue:\r
496 Percentage = '100'\r
497 else:\r
47fea6af
YZ
498 Percentage = str((UsedSizeValue + 0.0) / TotalSizeValue)[0:4].lstrip('0.')\r
499\r
f51461c8
LG
500 GenFdsGlobalVariable.InfLogger(Name + ' ' + '[' + Percentage + '%Full] ' + str(TotalSizeValue) + ' total, ' + str(UsedSizeValue) + ' used, ' + str(FreeSizeValue) + ' free')\r
501\r
502 ## PreprocessImage()\r
503 #\r
504 # @param BuildDb Database from build meta data files\r
505 # @param DscFile modules from dsc file will be preprocessed\r
506 # @retval None\r
507 #\r
508 def PreprocessImage(BuildDb, DscFile):\r
509 PcdDict = BuildDb.BuildObject[DscFile, 'COMMON', GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag].Pcds\r
510 PcdValue = ''\r
511 for Key in PcdDict:\r
512 PcdObj = PcdDict[Key]\r
513 if PcdObj.TokenCName == 'PcdBsBaseAddress':\r
514 PcdValue = PcdObj.DefaultValue\r
515 break\r
516 \r
517 if PcdValue == '':\r
518 return\r
519 \r
520 Int64PcdValue = long(PcdValue, 0)\r
521 if Int64PcdValue == 0 or Int64PcdValue < -1: \r
522 return\r
523 \r
524 TopAddress = 0\r
525 if Int64PcdValue > 0:\r
526 TopAddress = Int64PcdValue\r
527 \r
528 ModuleDict = BuildDb.BuildObject[DscFile, 'COMMON', GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag].Modules\r
529 for Key in ModuleDict:\r
530 ModuleObj = BuildDb.BuildObject[Key, 'COMMON', GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
531 print ModuleObj.BaseName + ' ' + ModuleObj.ModuleType\r
532\r
533 def GenerateGuidXRefFile(BuildDb, ArchList):\r
534 GuidXRefFileName = os.path.join(GenFdsGlobalVariable.FvDir, "Guid.xref")\r
535 GuidXRefFile = StringIO.StringIO('')\r
e4ac870f 536 GuidDict = {}\r
f51461c8
LG
537 for Arch in ArchList:\r
538 PlatformDataBase = BuildDb.BuildObject[GenFdsGlobalVariable.ActivePlatform, Arch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
539 for ModuleFile in PlatformDataBase.Modules:\r
540 Module = BuildDb.BuildObject[ModuleFile, Arch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
541 GuidXRefFile.write("%s %s\n" % (Module.Guid, Module.BaseName))\r
e4ac870f
LG
542 for key, item in Module.Protocols.items():\r
543 GuidDict[key] = item\r
544 for key, item in Module.Guids.items():\r
545 GuidDict[key] = item\r
546 for key, item in Module.Ppis.items():\r
547 GuidDict[key] = item\r
548 # Append GUIDs, Protocols, and PPIs to the Xref file\r
549 GuidXRefFile.write("\n")\r
550 for key, item in GuidDict.items():\r
551 GuidXRefFile.write("%s %s\n" % (GuidStructureStringToGuidString(item).upper(), key))\r
552\r
f51461c8
LG
553 if GuidXRefFile.getvalue():\r
554 SaveFileOnChange(GuidXRefFileName, GuidXRefFile.getvalue(), False)\r
555 GenFdsGlobalVariable.InfLogger("\nGUID cross reference file can be found at %s" % GuidXRefFileName)\r
556 elif os.path.exists(GuidXRefFileName):\r
557 os.remove(GuidXRefFileName)\r
558 GuidXRefFile.close()\r
559\r
560 ##Define GenFd as static function\r
561 GenFd = staticmethod(GenFd)\r
562 GetFvBlockSize = staticmethod(GetFvBlockSize)\r
563 DisplayFvSpaceInfo = staticmethod(DisplayFvSpaceInfo)\r
564 PreprocessImage = staticmethod(PreprocessImage)\r
565 GenerateGuidXRefFile = staticmethod(GenerateGuidXRefFile)\r
566\r
567if __name__ == '__main__':\r
568 r = main()\r
569 ## 0-127 is a safe return range, and 1 is a standard default error\r
570 if r < 0 or r > 127: r = 1\r
571 sys.exit(r)\r
572\r