1## @file
2# This file is used to parse a Module file of .PKG file
3#
4# Copyright (c) 2011 - 2014, Intel Corporation. All rights reserved.<BR>
5#
6# This program and the accompanying materials are licensed and made available
7# under the terms and conditions of the BSD License which accompanies this
8# distribution. The full text of the license may be found at
9# http://opensource.org/licenses/bsd-license.php
10#
11# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13#
14
15'''
16ModuleSurfaceAreaXml
17'''
18from xml.dom import minidom
19
20from Library.String import ConvertNEToNOTEQ
21from Library.String import ConvertNOTEQToNE
22from Library.String import GetStringOfList
23from Library.String import IsMatchArch
24from Library.Xml.XmlRoutines import XmlElement
25from Library.Xml.XmlRoutines import XmlAttribute
26from Library.Xml.XmlRoutines import XmlNode
27from Library.Xml.XmlRoutines import XmlList
28from Library.Xml.XmlRoutines import CreateXmlElement
29from Object.POM.CommonObject import GuidVersionObject
30from Object.POM.ModuleObject import BootModeObject
31from Object.POM.ModuleObject import DepexObject
32from Object.POM.ModuleObject import ModuleObject
33from Object.POM.ModuleObject import EventObject
34from Object.POM.ModuleObject import HobObject
35from Object.POM.ModuleObject import SourceFileObject
36from Object.POM.ModuleObject import PackageDependencyObject
37from Object.POM.ModuleObject import ExternObject
38from Object.POM.ModuleObject import BinaryFileObject
39from Object.POM.ModuleObject import AsBuiltObject
40from Object.POM.ModuleObject import BinaryBuildFlagObject
41from Xml.CommonXml import ClonedFromXml
42from Xml.CommonXml import HeaderXml
43from Xml.CommonXml import HelpTextXml
44from Xml.CommonXml import CommonDefinesXml
45from Xml.CommonXml import LibraryClassXml
46from Xml.CommonXml import UserExtensionsXml
47from Xml.CommonXml import MiscellaneousFileXml
48from Xml.CommonXml import FilenameXml
49from Xml.GuidProtocolPpiXml import GuidXml
50from Xml.GuidProtocolPpiXml import ProtocolXml
51from Xml.GuidProtocolPpiXml import PpiXml
52from Xml.PcdXml import PcdEntryXml
53from Xml.XmlParserMisc import GetHelpTextList
54from Library import GlobalData
55from Library.Misc import GetSplitValueList
56
57##   BinaryFileXml
58#
59#    represent the following XML item
60#
61#    <BinaryFile>
62#    <Filename
63#    FileType=" FileType " {1}
64#    SupArchList=" ArchListType " {0,1}
65#    FeatureFlag=" FeatureFlagExpression " {0,1} >
66#    xs:anyURI
67#    </Filename> {1,}
68#    <AsBuilt> ... </AsBuilt> {0,}
69#    </BinaryFile> {1,}
70#
71class BinaryFileXml(object):
72    def __init__(self):
73        self.FileNames = []
74        self.AsBuiltList = []
75        self.PatchPcdValues = ''
76        self.PcdExValues = ''
77        self.LibraryInstances = ''
78        self.BuildFlags = ''
79
80    def FromXml(self, Item, Key):
81        if self.FileNames:
82            pass
83        BinaryFile = BinaryFileObject()
84        FilenameList = []
85        SupArchList = ['COMMON']
86        for SubItem in XmlList(Item, '%s/Filename' % Key):
87            Axml = FilenameXml()
88            Bxml = Axml.FromXml(SubItem, 'Filename')
89            FilenameList.append(Bxml)
90        BinaryFile.SetFileNameList(FilenameList)
91        for FileName in FilenameList:
92            if FileName.GetSupArchList():
93                SupArchList = FileName.GetSupArchList()
94        BinaryFile.SetSupArchList(SupArchList)
95        if GlobalData.gIS_BINARY_INF:
96            AsBuiltList = []
97            for AsBuiltItem in XmlList(Item, '%s/AsBuilt' % Key):
98                AsBuilt = AsBuiltObject()
99
100                PatchPcdValueList = []
101                for SubItem in XmlList(AsBuiltItem, 'AsBuilt/PatchPcdValue'):
102                    Axml = PcdEntryXml()
103                    Bxml = Axml.FromXml(SubItem, 'PatchPcdValue')
104                    PatchPcdValueList.append(Bxml)
105                AsBuilt.SetPatchPcdList(PatchPcdValueList)
106                PcdExValueList = []
107                for SubItem in XmlList(AsBuiltItem, 'AsBuilt/PcdExValue'):
108                    Axml = PcdEntryXml()
109                    Bxml = Axml.FromXml(SubItem, 'PcdExValue')
110                    PcdExValueList.append(Bxml)
111                AsBuilt.SetPcdExList(PcdExValueList)
112                LibraryList = []
113                for SubItem in XmlList(Item, '%s/AsBuilt/LibraryInstances/GUID' % Key):
114                    GuidVerObj = GuidVersionObject()
115                    GUID = XmlElement(SubItem, 'GUID')
116                    Version = XmlAttribute(XmlNode(SubItem, 'GUID'), 'Version')
117                    GuidVerObj.SetGuid(GUID)
118                    GuidVerObj.SetVersion(Version)
119                    LibraryList.append(GuidVerObj)
120                if XmlList(Item, '%s/AsBuilt/LibraryInstances' % Key) and not LibraryList:
121                    LibraryList = [None]
122                AsBuilt.SetLibraryInstancesList(LibraryList)
123                BuildFlagList = []
124                for SubItem in XmlList(Item, '%s/AsBuilt/BuildFlags' % Key):
125                    BuildFlag = BuildFlagXml()
126                    BuildFlagList.append(BuildFlag.FromXml2(SubItem, 'BuildFlags'))
127                AsBuilt.SetBuildFlagsList(BuildFlagList)
128                AsBuiltList.append(AsBuilt)
129            BinaryFile.SetAsBuiltList(AsBuiltList)
130        return BinaryFile
131
132    def ToXml(self, BinaryFile, Key):
133        if self.FileNames:
134            pass
135        NodeList = []
136        FilenameList = BinaryFile.GetFileNameList()
137        SupportArch = None
138        for Filename in FilenameList:
139            Tmp = FilenameXml()
140            NodeList.append(Tmp.ToXml(Filename, 'Filename'))
141            SupportArch = Filename.SupArchList
142
143        AsBuildList = BinaryFile.GetAsBuiltList()
144        PatchPcdValueList = AsBuildList.GetPatchPcdList()
145        PcdExList = AsBuildList.GetPcdExList()
146        LibGuidVerList = AsBuildList.GetLibraryInstancesList()
147        BuildFlagList = AsBuildList.GetBuildFlagsList()
148
149        AsBuiltNodeList = []
150
151        for Pcd in PatchPcdValueList:
152            if IsMatchArch(Pcd.SupArchList, SupportArch):
153                Tmp = PcdEntryXml()
154                AsBuiltNodeList.append(Tmp.ToXml4(Pcd, 'PatchPcdValue'))
155
156        for Pcd in PcdExList:
157            if IsMatchArch(Pcd.SupArchList, SupportArch):
158                Tmp = PcdEntryXml()
159                AsBuiltNodeList.append(Tmp.ToXml4(Pcd, 'PcdExValue'))
160
161        GuiVerElemList = []
162        for LibGuidVer in LibGuidVerList:
163            if LibGuidVer.GetLibGuid() and IsMatchArch(LibGuidVer.GetSupArchList(), SupportArch):
164                GuiVerElem = \
165                CreateXmlElement('GUID', LibGuidVer.GetLibGuid(), [], [['Version', LibGuidVer.GetLibVersion()]])
166                GuiVerElemList.append(GuiVerElem)
167        if len(GuiVerElemList) > 0:
168            LibGuidVerElem = CreateXmlElement('LibraryInstances', '', GuiVerElemList, [])
169            AsBuiltNodeList.append(LibGuidVerElem)
170
171        for BuildFlag in BuildFlagList:
172            if IsMatchArch(BuildFlag.GetSupArchList(), SupportArch):
173                for Item in BuildFlag.GetAsBuildList():
174                    Tmp = BuildFlagXml()
175                    Elem = CreateXmlElement('BuildFlags', ''.join(Item), [], [])
176                    AsBuiltNodeList.append(Elem)
177
178        if len(AsBuiltNodeList) > 0:
179            Element = CreateXmlElement('AsBuilt', '', AsBuiltNodeList, [])
180            NodeList.append(Element)
181
182        Root = CreateXmlElement('%s' % Key, '', NodeList, [])
183
184        return Root
185
186    def __str__(self):
187        Str = "BinaryFiles:"
188        for Item in self.FileNames:
189            Str = Str + '\n\t' + str(Item)
190        for Item in self.PatchPcdValues:
191            Str = Str + '\n\t' + str(Item)
192        for Item in self.PcdExValues:
193            Str = Str + '\n\t' + str(Item)
194        for Item in self.LibraryInstances:
195            Str = Str + '\n\t' + str(Item)
196        for Item in self.BuildFlags:
197            Str = Str + '\n\t' + str(Item)
198        return Str
199
200##
201# PackageXml
202#
203class PackageXml(object):
204    def __init__(self):
205        self.Description = ''
206        self.Guid = ''
207        self.Version = ''
208        self.CommonDefines = CommonDefinesXml()
209
210    def FromXml(self, Item, Key):
211        self.Description = XmlElement(Item, '%s/Description' % Key)
212        self.Guid = XmlElement(Item, '%s/GUID' % Key)
213        self.Version = XmlAttribute(XmlNode(Item, '%s/GUID' % Key), 'Version')
214        self.CommonDefines.FromXml(XmlNode(Item, '%s' % Key), Key)
215
216        PackageDependency = PackageDependencyObject()
217        PackageDependency.SetPackage(self.Description)
218        PackageDependency.SetGuid(self.Guid)
219        PackageDependency.SetVersion(self.Version)
220        PackageDependency.SetFeatureFlag(ConvertNOTEQToNE(self.CommonDefines.FeatureFlag))
221        PackageDependency.SetSupArchList(self.CommonDefines.SupArchList)
222
223        return PackageDependency
224
225    def ToXml(self, PackageDependency, Key):
226        if self.Guid:
227            pass
228        AttributeList = [['SupArchList', GetStringOfList(PackageDependency.GetSupArchList())],
229                         ['FeatureFlag', ConvertNEToNOTEQ(PackageDependency.GetFeatureFlag())], ]
230        Element1 = CreateXmlElement('GUID', PackageDependency.GetGuid(), [],
231                                    [['Version', PackageDependency.GetVersion()]])
232        NodeList = [['Description', PackageDependency.GetPackage()], Element1, ]
233        Root = CreateXmlElement('%s' % Key, '', NodeList, AttributeList)
234
235        return Root
236
237    def __str__(self):
238        Str = "Description = %s Guid = %s Version = %s %s" \
239              % (self.Description, self.Guid, self.Version, self.CommonDefines)
240        return Str
241##
242# ExternXml
243#
244class ExternXml(object):
245    def __init__(self):
246        self.CommonDefines = CommonDefinesXml()
247        self.EntryPoint = ''
248        self.UnloadImage = ''
249        self.Constructor = ''
250        self.Destructor = ''
251        self.SupModList = ''
252        self.SupArchList = ''
253        self.HelpText = []
254
255    def FromXml(self, Item, Key):
256        self.CommonDefines.FromXml(Item, Key)
257        self.EntryPoint = XmlElement(Item, '%s/EntryPoint' % Key)
258        self.UnloadImage = XmlElement(Item, '%s/UnloadImage' % Key)
259        self.Constructor = XmlElement(Item, '%s/Constructor' % Key)
260        self.Destructor = XmlElement(Item, '%s/Destructor' % Key)
261
262        Extern = ExternObject()
263        Extern.SetEntryPoint(self.EntryPoint)
264        Extern.SetUnloadImage(self.UnloadImage)
265        Extern.SetConstructor(self.Constructor)
266        Extern.SetDestructor(self.Destructor)
267        if self.CommonDefines.SupModList:
268            Extern.SetSupModList(self.CommonDefines.SupModList)
269        if self.CommonDefines.SupArchList:
270            Extern.SetSupArchList(self.CommonDefines.SupArchList)
271        return Extern
272
273    def ToXml(self, Extern, Key):
274        if self.HelpText:
275            pass
276
277        NodeList = []
278        if Extern.GetEntryPoint():
279            NodeList.append(['EntryPoint', Extern.GetEntryPoint()])
280        if Extern.GetUnloadImage():
281            NodeList.append(['UnloadImage', Extern.GetUnloadImage()])
282        if Extern.GetConstructor():
283            NodeList.append(['Constructor', Extern.GetConstructor()])
284        if Extern.GetDestructor():
285            NodeList.append(['Destructor', Extern.GetDestructor()])
286        Root = CreateXmlElement('%s' % Key, '', NodeList, [])
287
288        return Root
289
290    def __str__(self):
291        Str = "EntryPoint = %s UnloadImage = %s Constructor = %s Destructor = %s %s" \
292              % (self.EntryPoint, self.UnloadImage, self.Constructor, self.Destructor, self.CommonDefines)
293        for Item in self.HelpText:
294            Str = Str + '\n\t' + str(Item)
295        return Str
296##
297# DepexXml
298#
299class DepexXml(object):
300    def __init__(self):
301        self.CommonDefines = CommonDefinesXml()
302        self.Expression = None
303        self.HelpText = []
304
305    def FromXml(self, Item, Key):
306        if not Item:
307            return None
308        self.CommonDefines.FromXml(Item, Key)
309        self.Expression = XmlElement(Item, '%s/Expression' % Key)
310        for HelpTextItem in XmlList(Item, '%s/HelpText' % Key):
311            HelpTextObj = HelpTextXml()
312            HelpTextObj.FromXml(HelpTextItem, '%s/HelpText' % Key)
313            self.HelpText.append(HelpTextObj)
314
315        Depex = DepexObject()
316        Depex.SetDepex(self.Expression)
317        Depex.SetModuleType(self.CommonDefines.SupModList)
318        Depex.SetSupArchList(self.CommonDefines.SupArchList)
319        Depex.SetFeatureFlag(self.CommonDefines.FeatureFlag)
320        Depex.SetHelpTextList(GetHelpTextList(self.HelpText))
321
322        return Depex
323
324    def ToXml(self, Depex, Key):
325        if self.HelpText:
326            pass
327        AttributeList = [['SupArchList', GetStringOfList(Depex.GetSupArchList())],
328                         ['SupModList', Depex.GetModuleType()]]
329        NodeList = [['Expression', Depex.GetDepex()]]
330        if Depex.GetHelpText():
331            Tmp = HelpTextXml()
332            NodeList.append(Tmp.ToXml(Depex.GetHelpText(), 'HelpText'))
333
334        Root = CreateXmlElement('%s' % Key, '', NodeList, AttributeList)
335        return Root
336
337    def __str__(self):
338        Str = "Expression = %s" % (self.Expression)
339        for Item in self.HelpText:
340            Str = Str + '\n\t' + str(Item)
341        return Str
342
343##
344# BootModeXml
345#
346class BootModeXml(object):
347    def __init__(self):
348        self.SupportedBootModes = ''
349        self.CommonDefines = CommonDefinesXml()
350        self.HelpText = []
351
352    def FromXml(self, Item, Key):
353        self.SupportedBootModes = \
354        XmlElement(Item, '%s/SupportedBootModes' % Key)
355        self.CommonDefines.FromXml(Item, Key)
356        for HelpTextItem in XmlList(Item, '%s/HelpText' % Key):
357            HelpTextObj = HelpTextXml()
358            HelpTextObj.FromXml(HelpTextItem, '%s/HelpText' % Key)
359            self.HelpText.append(HelpTextObj)
360
361        BootMode = BootModeObject()
362        BootMode.SetSupportedBootModes(self.SupportedBootModes)
363        BootMode.SetUsage(self.CommonDefines.Usage)
364        BootMode.SetHelpTextList(GetHelpTextList(self.HelpText))
365
366        return BootMode
367
368    def ToXml(self, BootMode, Key):
369        if self.HelpText:
370            pass
371        AttributeList = [['Usage', BootMode.GetUsage()], ]
372        NodeList = [['SupportedBootModes', BootMode.GetSupportedBootModes()]]
373        for Item in BootMode.GetHelpTextList():
374            Tmp = HelpTextXml()
375            NodeList.append(Tmp.ToXml(Item, 'HelpText'))
376        Root = CreateXmlElement('%s' % Key, '', NodeList, AttributeList)
377
378        return Root
379
380    def __str__(self):
381        Str = "SupportedBootModes = %s %s" % (self.SupportedBootModes, self.CommonDefines)
382        for Item in self.HelpText:
383            Str = Str + '\n\t' + str(Item)
384        return Str
385##
386# EventXml
387#
388class EventXml(object):
389    def __init__(self):
390        self.EventType = ''
391        self.Name = ''
392        self.CommonDefines = CommonDefinesXml()
393        self.HelpText = []
394
395    def FromXml(self, Item, Key):
396        self.EventType = XmlAttribute(XmlNode(Item, '%s' % Key), 'EventType')
397        self.Name = XmlElement(Item, '%s' % Key)
398        self.CommonDefines.FromXml(Item, Key)
399        for HelpTextItem in XmlList(Item, '%s/HelpText' % Key):
400            HelpTextObj = HelpTextXml()
401            HelpTextObj.FromXml(HelpTextItem, '%s/HelpText' % Key)
402            self.HelpText.append(HelpTextObj)
403
404        Event = EventObject()
405        Event.SetEventType(self.EventType)
406        Event.SetUsage(self.CommonDefines.Usage)
407        Event.SetHelpTextList(GetHelpTextList(self.HelpText))
408
409        return Event
410
411    def ToXml(self, Event, Key):
412        if self.HelpText:
413            pass
414        AttributeList = [['EventType', Event.GetEventType()],
415                         ['Usage', Event.GetUsage()],
416                        ]
417        NodeList = []
418        for Item in Event.GetHelpTextList():
419            Tmp = HelpTextXml()
420            NodeList.append(Tmp.ToXml(Item, 'HelpText'))
421        Root = CreateXmlElement('%s' % Key, '', NodeList, AttributeList)
422
423        return Root
424
425    def __str__(self):
426        Str = "EventType = %s %s" % (self.EventType, self.CommonDefines)
427        for Item in self.HelpText:
428            Str = Str + '\n\t' + str(Item)
429        return Str
430##
431# HobXml
432#
433class HobXml(object):
434    def __init__(self):
435        self.HobType = ''
436        self.Name = ''
437        self.CommonDefines = CommonDefinesXml()
438        self.HelpText = []
439
440    def FromXml(self, Item, Key):
441        self.HobType = XmlAttribute(XmlNode(Item, '%s' % Key), 'HobType')
442        self.Name = XmlElement(Item, '%s' % Key)
443        self.CommonDefines.FromXml(Item, Key)
444        for HelpTextItem in XmlList(Item, '%s/HelpText' % Key):
445            HelpTextObj = HelpTextXml()
446            HelpTextObj.FromXml(HelpTextItem, '%s/HelpText' % Key)
447            self.HelpText.append(HelpTextObj)
448
449        Hob = HobObject()
450        Hob.SetHobType(self.HobType)
451        Hob.SetSupArchList(self.CommonDefines.SupArchList)
452        Hob.SetUsage(self.CommonDefines.Usage)
453        Hob.SetHelpTextList(GetHelpTextList(self.HelpText))
454
455        return Hob
456
457    def ToXml(self, Hob, Key):
458        if self.Name:
459            pass
460        AttributeList = [['HobType', Hob.GetHobType()],
461                         ['Usage', Hob.GetUsage()],
462                         ['SupArchList', GetStringOfList(Hob.GetSupArchList())], ]
463        NodeList = []
464        for Item in Hob.GetHelpTextList():
465            Tmp = HelpTextXml()
466            NodeList.append(Tmp.ToXml(Item, 'HelpText'))
467        Root = CreateXmlElement('%s' % Key, '', NodeList, AttributeList)
468
469        return Root
470
471    def __str__(self):
472        Str = "HobType = %s %s" % (self.HobType, self.CommonDefines)
473        for Item in self.HelpText:
474            Str = Str + '\n\t' + str(Item)
475        return Str
476
477##
478# SourceFileXml
479#
480class SourceFileXml(object):
481    def __init__(self):
482        self.SourceFile = ''
483        self.ToolChainFamily = ''
484        self.FileType = ''
485        self.CommonDefines = CommonDefinesXml()
486
487    def FromXml(self, Item, Key):
488        self.ToolChainFamily = XmlAttribute(Item, 'Family')
489        self.SourceFile = XmlElement(Item, 'Filename')
490        self.CommonDefines.FromXml(Item, Key)
491
492        self.CommonDefines.FeatureFlag = ConvertNOTEQToNE(self.CommonDefines.FeatureFlag)
493
494        SourceFile = SourceFileObject()
495        SourceFile.SetSourceFile(self.SourceFile)
496        SourceFile.SetFamily(self.ToolChainFamily)
497        SourceFile.SetSupArchList(self.CommonDefines.SupArchList)
498        SourceFile.SetFeatureFlag(self.CommonDefines.FeatureFlag)
499
500        return SourceFile
501
502    def ToXml(self, SourceFile, Key):
503        if self.SourceFile:
504            pass
505        FeatureFlag = ConvertNEToNOTEQ(SourceFile.GetFeatureFlag())
506        AttributeList = [['SupArchList', GetStringOfList(SourceFile.GetSupArchList())],
507                         ['Family', SourceFile.GetFamily()],
508                         ['FeatureFlag', FeatureFlag], ]
509        Root = CreateXmlElement('%s' % Key, SourceFile.GetSourceFile(), [], AttributeList)
510        return Root
511
512##
513# ModulePropertyXml
514#
515class ModulePropertyXml(object):
516    def __init__(self):
517        self.CommonDefines = CommonDefinesXml()
518        self.ModuleType = ''
519        self.Path = ''
520        self.PcdIsDriver = ''
521        self.UefiSpecificationVersion = ''
522        self.PiSpecificationVersion = ''
523        self.SpecificationList = []
524        self.SpecificationVersion = ''
525        self.BootModes = []
526        self.Events = []
527        self.HOBs = []
528
529    def FromXml(self, Item, Key, Header=None):
530        self.CommonDefines.FromXml(Item, Key)
531        self.ModuleType = XmlElement(Item, '%s/ModuleType' % Key)
532        self.Path = XmlElement(Item, '%s/Path' % Key)
533        self.PcdIsDriver = XmlElement(Item, '%s/PcdIsDriver' % Key)
534        self.UefiSpecificationVersion = XmlElement(Item, '%s/UefiSpecificationVersion' % Key)
535        self.PiSpecificationVersion = XmlElement(Item, '%s/PiSpecificationVersion' % Key)
536        for SubItem in XmlList(Item, '%s/Specification' % Key):
537            Specification = XmlElement(SubItem, '/Specification')
538            Version = XmlAttribute(XmlNode(SubItem, '/Specification'), 'Version')
539            self.SpecificationList.append((Specification, Version))
540        for SubItem in XmlList(Item, '%s/BootMode' % Key):
541            Axml = BootModeXml()
542            BootMode = Axml.FromXml(SubItem, 'BootMode')
543            self.BootModes.append(BootMode)
544        for SubItem in XmlList(Item, '%s/Event' % Key):
545            Axml = EventXml()
546            Event = Axml.FromXml(SubItem, 'Event')
547            self.Events.append(Event)
548        for SubItem in XmlList(Item, '%s/HOB' % Key):
549            Axml = HobXml()
550            Hob = Axml.FromXml(SubItem, 'HOB')
551            self.HOBs.append(Hob)
552
553        if Header == None:
554            Header = ModuleObject()
555
556        Header.SetModuleType(self.ModuleType)
557        Header.SetSupArchList(self.CommonDefines.SupArchList)
558        Header.SetModulePath(self.Path)
559
560        Header.SetPcdIsDriver(self.PcdIsDriver)
561        Header.SetUefiSpecificationVersion(self.UefiSpecificationVersion)
562        Header.SetPiSpecificationVersion(self.PiSpecificationVersion)
563        Header.SetSpecList(self.SpecificationList)
564
565        return Header, self.BootModes, self.Events, self.HOBs
566
567
568    def ToXml(self, Header, BootModes, Events, Hobs, Key):
569        if self.ModuleType:
570            pass
571        AttributeList = [['SupArchList', GetStringOfList(Header.GetSupArchList())], ]
572
573        NodeList = [['ModuleType', Header.GetModuleType()],
574                    ['Path', Header.GetModulePath()],
575                    ['PcdIsDriver', Header.GetPcdIsDriver()],
576                    ['UefiSpecificationVersion', Header.GetUefiSpecificationVersion()],
577                    ['PiSpecificationVersion', Header.GetPiSpecificationVersion()],
578                   ]
579        for Item in Header.GetSpecList():
580            Spec, Version = Item
581            SpecElem = CreateXmlElement('Specification', Spec, [], [['Version', Version]])
582            NodeList.append(SpecElem)
583
584        for Item in BootModes:
585            Tmp = BootModeXml()
586            NodeList.append(Tmp.ToXml(Item, 'BootMode'))
587        for Item in Events:
588            Tmp = EventXml()
589            NodeList.append(Tmp.ToXml(Item, 'Event'))
590        for Item in Hobs:
591            Tmp = HobXml()
592            NodeList.append(Tmp.ToXml(Item, 'HOB'))
593        Root = CreateXmlElement('%s' % Key, '', NodeList, AttributeList)
594
595        return Root
596
597    def __str__(self):
598        Str = "ModuleType = %s Path = %s PcdIsDriver = %s UefiSpecificationVersion = %s PiSpecificationVersion = %s \
599               Specification = %s SpecificationVersion = %s %s" % \
600        (self.ModuleType, self.Path, self.PcdIsDriver, \
601         self.UefiSpecificationVersion, self.PiSpecificationVersion, \
602         self.SpecificationList, self.SpecificationVersion, self.CommonDefines)
603        for Item in self.BootModes:
604            Str = Str + '\n\t' + str(Item)
605        for Item in self.Events:
606            Str = Str + '\n\t' + str(Item)
607        for Item in self.HOBs:
608            Str = Str + '\n\t' + str(Item)
609        return Str
610
611##
612# ModuleXml
613#
614class ModuleSurfaceAreaXml(object):
615    def __init__(self, Package=''):
616        self.Module = None
617        #
618        # indicate the package that this module resides in
619        #
620        self.Package = Package
621
622    def FromXml2(self, Item, Module):
623        if self.Module:
624            pass
625        #
626        # PeiDepex
627        #
628        PeiDepexList = []
629        for SubItem in XmlList(Item, '/ModuleSurfaceArea/PeiDepex'):
630            Tmp = DepexXml()
631            Depex = Tmp.FromXml(XmlNode(SubItem, 'PeiDepex'), 'PeiDepex')
632            PeiDepexList.append(Depex)
633        Module.SetPeiDepex(PeiDepexList)
634
635        #
636        # DxeDepex
637        #
638        DxeDepexList = []
639        for SubItem in XmlList(Item, '/ModuleSurfaceArea/DxeDepex'):
640            Tmp = DepexXml()
641            Depex = Tmp.FromXml(XmlNode(SubItem, 'DxeDepex'), 'DxeDepex')
642            DxeDepexList.append(Depex)
643        Module.SetDxeDepex(DxeDepexList)
644
645        #
646        # SmmDepex
647        #
648        SmmDepexList = []
649        for SubItem in XmlList(Item, '/ModuleSurfaceArea/SmmDepex'):
650            Tmp = DepexXml()
651            Depex = Tmp.FromXml(XmlNode(SubItem, 'SmmDepex'), 'SmmDepex')
652            SmmDepexList.append(Depex)
653        Module.SetSmmDepex(SmmDepexList)
654
655        #
656        # MiscellaneousFile
657        Tmp = MiscellaneousFileXml()
658        MiscFileList = Tmp.FromXml(XmlNode(Item, '/ModuleSurfaceArea/MiscellaneousFiles'), 'MiscellaneousFiles')
659        if MiscFileList:
660            Module.SetMiscFileList([MiscFileList])
661        else:
662            Module.SetMiscFileList([])
663
664        #
665        # UserExtensions
666        #
667        for Item in XmlList(Item, '/ModuleSurfaceArea/UserExtensions'):
668            Tmp = UserExtensionsXml()
669            UserExtension = Tmp.FromXml(Item, 'UserExtensions')
670            Module.SetUserExtensionList(Module.GetUserExtensionList() + [UserExtension])
671
672        return Module
673
674    def FromXml(self, Item, Key, IsStandAlongModule=False):
675        IsBinaryModule = XmlAttribute(Item, 'BinaryModule')
676        #
677        # Header
678        #
679        Tmp = HeaderXml()
680        Module = Tmp.FromXml(XmlNode(Item, '/%s/Header' % Key), 'Header', True, IsStandAlongModule)
681        Module.SetBinaryModule(IsBinaryModule)
682
683        if IsBinaryModule:
684            GlobalData.gIS_BINARY_INF = True
685
686        #
687        # ModuleProperties
688        #
689        Tmp = ModulePropertyXml()
690        (Module, BootModes, Events, HOBs) = \
691        Tmp.FromXml(XmlNode(Item, '/ModuleSurfaceArea/ModuleProperties'), 'ModuleProperties', Module)
692        Module.SetBootModeList(BootModes)
693        Module.SetEventList(Events)
694        Module.SetHobList(HOBs)
695        #
696        # ClonedFrom
697        #
698        Tmp = ClonedFromXml()
699        ClonedFrom = Tmp.FromXml(XmlNode(Item, '/ModuleSurfaceArea/ClonedFrom'), 'ClonedFrom')
700        if ClonedFrom:
701            Module.SetClonedFrom(ClonedFrom)
702
703        #
704        # LibraryClass
705        #
706        for SubItem in XmlList(Item, '/ModuleSurfaceArea/LibraryClassDefinitions/LibraryClass'):
707            Tmp = LibraryClassXml()
708            LibraryClass = Tmp.FromXml(SubItem, 'LibraryClass')
709            Module.SetLibraryClassList(Module.GetLibraryClassList() + [LibraryClass])
710
711        if XmlList(Item, '/ModuleSurfaceArea/LibraryClassDefinitions') and \
712           not XmlList(Item, '/ModuleSurfaceArea/LibraryClassDefinitions/LibraryClass'):
713            Module.SetLibraryClassList([None])
714
715        #
716        # SourceFiles
717        #
718        for SubItem in XmlList(Item, '/ModuleSurfaceArea/SourceFiles/Filename'):
719            Tmp = SourceFileXml()
720            SourceFile = Tmp.FromXml(SubItem, 'Filename')
721            Module.SetSourceFileList(Module.GetSourceFileList() + [SourceFile])
722
723        if XmlList(Item, '/ModuleSurfaceArea/SourceFiles') and \
724           not XmlList(Item, '/ModuleSurfaceArea/SourceFiles/Filename') :
725            Module.SetSourceFileList([None])
726
727        #
728        # BinaryFile
729        #
730        for SubItem in XmlList(Item, '/ModuleSurfaceArea/BinaryFiles/BinaryFile'):
731            Tmp = BinaryFileXml()
732            BinaryFile = Tmp.FromXml(SubItem, 'BinaryFile')
733            Module.SetBinaryFileList(Module.GetBinaryFileList() + [BinaryFile])
734
735        if XmlList(Item, '/ModuleSurfaceArea/BinaryFiles') and \
736           not XmlList(Item, '/ModuleSurfaceArea/BinaryFiles/BinaryFile') :
737            Module.SetBinaryFileList([None])
738        #
739        # PackageDependencies
740        #
741        for SubItem in XmlList(Item, '/ModuleSurfaceArea/PackageDependencies/Package'):
742            Tmp = PackageXml()
743            PackageDependency = Tmp.FromXml(SubItem, 'Package')
744            Module.SetPackageDependencyList(Module.GetPackageDependencyList() + [PackageDependency])
745
746        if XmlList(Item, '/ModuleSurfaceArea/PackageDependencies') and \
747           not XmlList(Item, '/ModuleSurfaceArea/PackageDependencies/Package'):
748            Module.SetPackageDependencyList([None])
749
750        #
751        # Guid
752        #
753        for SubItem in XmlList(Item, '/ModuleSurfaceArea/Guids/GuidCName'):
754            Tmp = GuidXml('Module')
755            GuidProtocolPpi = Tmp.FromXml(SubItem, 'GuidCName')
756            Module.SetGuidList(Module.GetGuidList() + [GuidProtocolPpi])
757
758        if XmlList(Item, '/ModuleSurfaceArea/Guids') and not XmlList(Item, '/ModuleSurfaceArea/Guids/GuidCName'):
759            Module.SetGuidList([None])
760
761        #
762        # Protocol
763        #
764        for SubItem in XmlList(Item, '/ModuleSurfaceArea/Protocols/Protocol'):
765            Tmp = ProtocolXml('Module')
766            GuidProtocolPpi = Tmp.FromXml(SubItem, 'Protocol')
767            Module.SetProtocolList(Module.GetProtocolList() + [GuidProtocolPpi])
768
769        if XmlList(Item, '/ModuleSurfaceArea/Protocols') and not XmlList(Item, '/ModuleSurfaceArea/Protocols/Protocol'):
770            Module.SetProtocolList([None])
771
772        #
773        # Ppi
774        #
775        for SubItem in XmlList(Item, '/ModuleSurfaceArea/PPIs/Ppi'):
776            Tmp = PpiXml('Module')
777            GuidProtocolPpi = Tmp.FromXml(SubItem, 'Ppi')
778            Module.SetPpiList(Module.GetPpiList() + [GuidProtocolPpi])
779
780        if XmlList(Item, '/ModuleSurfaceArea/PPIs') and not XmlList(Item, '/ModuleSurfaceArea/PPIs/Ppi'):
781            Module.SetPpiList([None])
782
783        #
784        # Extern
785        #
786        for SubItem in XmlList(Item, '/ModuleSurfaceArea/Externs/Extern'):
787            Tmp = ExternXml()
788            Extern = Tmp.FromXml(SubItem, 'Extern')
789            Module.SetExternList(Module.GetExternList() + [Extern])
790
791        if XmlList(Item, '/ModuleSurfaceArea/Externs') and not XmlList(Item, '/ModuleSurfaceArea/Externs/Extern'):
792            Module.SetExternList([None])
793
794        if not Module.GetBinaryModule():
795            #
796            # PcdCoded
797            #
798            for SubItem in XmlList(Item, '/ModuleSurfaceArea/PcdCoded/PcdEntry'):
799                Tmp = PcdEntryXml()
800                PcdEntry = Tmp.FromXml3(SubItem, 'PcdEntry')
801                Module.SetPcdList(Module.GetPcdList() + [PcdEntry])
802
803            if XmlList(Item, '/ModuleSurfaceArea/PcdCoded') and \
804                not XmlList(Item, '/ModuleSurfaceArea/PcdCoded/PcdEntry'):
805                Module.SetPcdList([None])
806
807        Module = self.FromXml2(Item, Module)
808        #
809        # return the module object
810        #
811        self.Module = Module
812        return self.Module
813
814    def ToXml(self, Module):
815        if self.Package:
816            pass
817        #
818        # Create root node of module surface area
819        #
820        DomModule = minidom.Document().createElement('ModuleSurfaceArea')
821        if Module.GetBinaryModule():
822            DomModule.setAttribute('BinaryModule', 'true')
823
824        #
825        # Header
826        #
827        Tmp = HeaderXml()
828        DomModule.appendChild(Tmp.ToXml(Module, 'Header'))
829        #
830        # ModuleProperties
831        #
832        Tmp = ModulePropertyXml()
833        DomModule.appendChild(Tmp.ToXml(Module, Module.GetBootModeList(), Module.GetEventList(), Module.GetHobList(), \
834                                        'ModuleProperties'))
835        #
836        # ClonedFrom
837        #
838        Tmp = ClonedFromXml()
839        if Module.GetClonedFrom():
840            DomModule.appendChild(Tmp.ToXml(Module.GetClonedFrom(), 'ClonedFrom'))
841        #
842        # LibraryClass
843        #
844        LibraryClassNode = CreateXmlElement('LibraryClassDefinitions', '', [], [])
845        for LibraryClass in Module.GetLibraryClassList():
846            Tmp = LibraryClassXml()
847            LibraryClassNode.appendChild(Tmp.ToXml2(LibraryClass, 'LibraryClass'))
848        DomModule.appendChild(LibraryClassNode)
849        #
850        # SourceFile
851        #
852        SourceFileNode = CreateXmlElement('SourceFiles', '', [], [])
853        for SourceFile in Module.GetSourceFileList():
854            Tmp = SourceFileXml()
855            SourceFileNode.appendChild(Tmp.ToXml(SourceFile, 'Filename'))
856        DomModule.appendChild(SourceFileNode)
857        #
858        # BinaryFile
859        #
860        BinaryFileNode = CreateXmlElement('BinaryFiles', '', [], [])
861        for BinaryFile in Module.GetBinaryFileList():
862            Tmp = BinaryFileXml()
863            BinaryFileNode.appendChild(Tmp.ToXml(BinaryFile, 'BinaryFile'))
864        DomModule.appendChild(BinaryFileNode)
865        #
866        # PackageDependencies
867        #
868        PackageDependencyNode = CreateXmlElement('PackageDependencies', '', [], [])
869        for PackageDependency in Module.GetPackageDependencyList():
870            Tmp = PackageXml()
871            PackageDependencyNode.appendChild(Tmp.ToXml(PackageDependency, 'Package'))
872        DomModule.appendChild(PackageDependencyNode)
873
874        #
875        # Guid
876        #
877        GuidProtocolPpiNode = CreateXmlElement('Guids', '', [], [])
878        for GuidProtocolPpi in Module.GetGuidList():
879            Tmp = GuidXml('Module')
880            GuidProtocolPpiNode.appendChild(Tmp.ToXml(GuidProtocolPpi, 'GuidCName'))
881        DomModule.appendChild(GuidProtocolPpiNode)
882
883        #
884        # Protocol
885        #
886        GuidProtocolPpiNode = CreateXmlElement('Protocols', '', [], [])
887        for GuidProtocolPpi in Module.GetProtocolList():
888            Tmp = ProtocolXml('Module')
889            GuidProtocolPpiNode.appendChild(Tmp.ToXml(GuidProtocolPpi, 'Protocol'))
890        DomModule.appendChild(GuidProtocolPpiNode)
891
892        #
893        # Ppi
894        #
895        GuidProtocolPpiNode = CreateXmlElement('PPIs', '', [], [])
896        for GuidProtocolPpi in Module.GetPpiList():
897            Tmp = PpiXml('Module')
898            GuidProtocolPpiNode.appendChild(Tmp.ToXml(GuidProtocolPpi, 'Ppi'))
899        DomModule.appendChild(GuidProtocolPpiNode)
900        #
901        # Extern
902        #
903        ExternNode = CreateXmlElement('Externs', '', [], [])
904        for Extern in Module.GetExternList():
905            Tmp = ExternXml()
906            ExternNode.appendChild(Tmp.ToXml(Extern, 'Extern'))
907        DomModule.appendChild(ExternNode)
908        #
909        # PcdCoded
910        #
911        PcdEntryNode = CreateXmlElement('PcdCoded', '', [], [])
912        for PcdEntry in Module.GetPcdList():
913            Tmp = PcdEntryXml()
914            PcdEntryNode.appendChild(Tmp.ToXml3(PcdEntry, 'PcdEntry'))
915        DomModule.appendChild(PcdEntryNode)
916
917        #
918        # PeiDepex
919        #
920        if Module.GetPeiDepex():
921            for Item in Module.GetPeiDepex():
922                Tmp = DepexXml()
923                DomModule.appendChild(Tmp.ToXml(Item, 'PeiDepex'))
924
925        #
926        # DxeDepex
927        #
928        if Module.GetDxeDepex():
929            for Item in Module.GetDxeDepex():
930                Tmp = DepexXml()
931                DomModule.appendChild(Tmp.ToXml(Item, 'DxeDepex'))
932
933        #
934        # SmmDepex
935        #
936        if Module.GetSmmDepex():
937            for Item in Module.GetSmmDepex():
938                Tmp = DepexXml()
939                DomModule.appendChild(Tmp.ToXml(Item, 'SmmDepex'))
940
941        #
942        # MiscellaneousFile
943        #
944        if Module.GetMiscFileList():
945            Tmp = MiscellaneousFileXml()
946            DomModule.appendChild(Tmp.ToXml(Module.GetMiscFileList()[0], 'MiscellaneousFiles'))
947        #
948        # UserExtensions
949        #
950        if Module.GetUserExtensionList():
951            for UserExtension in Module.GetUserExtensionList():
952                Tmp = UserExtensionsXml()
953                DomModule.appendChild(Tmp.ToXml(UserExtension, 'UserExtensions'))
954
955        return DomModule
956
957##
958# BuildFlagXml used to generate BuildFlag for <AsBuilt>
959#
960class BuildFlagXml(object):
961    def __init__(self):
962        self.Target = ''
963        self.TagName = ''
964        self.Family = ''
965        self.AsBuiltFlags = ''
966
967    def FromXml(self, Item, Key):
968        self.Target = XmlElement(Item, '%s/Target' % Key)
969        self.TagName = XmlElement(Item, '%s/TagName' % Key)
970        self.Family = XmlElement(Item, '%s/Family' % Key)
971
972        BuildFlag = BinaryBuildFlagObject()
973
974        BuildFlag.SetTarget(self.Target)
975        BuildFlag.SetTagName(self.TagName)
976        BuildFlag.SetFamily(self.Family)
977
978        return BuildFlag
979
980    #
981    # For AsBuild INF usage
982    #
983    def FromXml2(self, Item, Key):
984        self.AsBuiltFlags = XmlElement(Item, '%s' % Key)
985
986        LineList = GetSplitValueList(self.AsBuiltFlags, '\n')
987        ReturnLine = ''
988        Count = 0
989        for Line in LineList:
990            if Count == 0:
991                ReturnLine = "# " + Line
992            else:
993                ReturnLine = ReturnLine + '\n' + '# ' + Line
994            Count += 1
995
996        BuildFlag = BinaryBuildFlagObject()
997        BuildFlag.SetAsBuiltOptionFlags(ReturnLine)
998
999        return BuildFlag
1000
1001    def ToXml(self, BuildFlag, Key):
1002        if self.Target:
1003            pass
1004        AttributeList = []
1005        NodeList = []
1006        NodeList.append(['BuildFlags', BuildFlag])
1007
1008        Root = CreateXmlElement('%s' % Key, '', NodeList, AttributeList)
1009        return Root
1010