]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Common/VpdInfoFile.py
3 # This package manage the VPD PCD information file which will be generated
4 # by build tool's autogen.
5 # The VPD PCD information file will be input for third-party BPDG tool which
6 # is pointed by *_*_*_VPD_TOOL_GUID in conf/tools_def.txt
9 # Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.<BR>
10 # This program and the accompanying materials
11 # are licensed and made available under the terms and conditions of the BSD License
12 # which accompanies this distribution. The full text of the license may be found at
13 # http://opensource.org/licenses/bsd-license.php
15 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 import Common
. LongFilePathOs
as os
20 import Common
. EdkLogger
as EdkLogger
21 import Common
. BuildToolError
as BuildToolError
23 import Common
. GlobalData
as GlobalData
24 from Common
. LongFilePathSupport
import OpenLongFilePath
as open
25 from Common
. Misc
import SaveFileOnChange
27 FILE_COMMENT_TEMPLATE
= \
31 # THIS IS AUTO-GENERATED FILE BY BUILD TOOLS AND PLEASE DO NOT MAKE MODIFICATION.
33 # This file lists all VPD informations for a platform collected by build.exe.
35 # Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
36 # This program and the accompanying materials
37 # are licensed and made available under the terms and conditions of the BSD License
38 # which accompanies this distribution. The full text of the license may be found at
39 # http://opensource.org/licenses/bsd-license.php
41 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
42 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
47 ## The class manage VpdInfoFile.
49 # This file contains an ordered (based on position in the DSC file) list of the PCDs specified in the platform description file (DSC). The Value field that will be assigned to the PCD comes from the DSC file, INF file (if not defined in the DSC file) or the DEC file (if not defined in the INF file). This file is used as an input to the BPDG tool.
50 # Format for this file (using EBNF notation) is:
51 # <File> :: = [<CommentBlock>]
53 # <CommentBlock> ::= ["#" <String> <EOL>]*
54 # <PcdEntry> ::= <PcdName> "|" <Offset> "|" <Size> "|" <Value> <EOL>
55 # <PcdName> ::= <TokenSpaceCName> "." <PcdCName>
56 # <TokenSpaceCName> ::= C Variable Name of the Token Space GUID
57 # <PcdCName> ::= C Variable Name of the PCD
58 # <Offset> ::= {"*"} {<HexNumber>}
59 # <HexNumber> ::= "0x" (a-fA-F0-9){1,8}
60 # <Size> ::= <HexNumber>
61 # <Value> ::= {<HexNumber>} {<NonNegativeInt>} {<QString>} {<Array>}
62 # <NonNegativeInt> ::= (0-9)+
63 # <QString> ::= ["L"] <DblQuote> <String> <DblQuote>
65 # <Array> ::= {<CArray>} {<NList>}
66 # <CArray> ::= "{" <HexNumber> ["," <HexNumber>]* "}"
67 # <NList> ::= <HexNumber> ["," <HexNumber>]*
71 ## The mapping dictionary from datum type to size string.
72 _MAX_SIZE_TYPE
= { "BOOLEAN" : "1" , "UINT8" : "1" , "UINT16" : "2" , "UINT32" : "4" , "UINT64" : "8" }
76 ## Dictionary for VPD in following format
78 # Key : PcdClassObject instance.
79 # @see BuildClassObject.PcdClassObject
80 # Value : offset in different SKU such as [sku1_offset, sku2_offset]
84 ## Add a VPD PCD collected from platform's autogen when building.
86 # @param vpds The list of VPD PCD collected for a platform.
87 # @see BuildClassObject.PcdClassObject
89 # @param offset integer value for VPD's offset in specific SKU.
91 def Add ( self
, Vpd
, Offset
):
93 EdkLogger
. error ( "VpdInfoFile" , BuildToolError
. ATTRIBUTE_UNKNOWN_ERROR
, "Invalid VPD PCD entry." )
95 if not ( Offset
>= 0 or Offset
== "*" ):
96 EdkLogger
. error ( "VpdInfoFile" , BuildToolError
. PARAMETER_INVALID
, "Invalid offset parameter: %s ." % Offset
)
98 if Vpd
. DatumType
== "VOID*" :
99 if Vpd
. MaxDatumSize
<= 0 :
100 EdkLogger
. error ( "VpdInfoFile" , BuildToolError
. PARAMETER_INVALID
,
101 "Invalid max datum size for VPD PCD %s . %s " % ( Vpd
. TokenSpaceGuidCName
, Vpd
. TokenCName
))
102 elif Vpd
. DatumType
in [ "BOOLEAN" , "UINT8" , "UINT16" , "UINT32" , "UINT64" ]:
103 if Vpd
. MaxDatumSize
== None or Vpd
. MaxDatumSize
== "" :
104 Vpd
. MaxDatumSize
= VpdInfoFile
._ MAX
_ SIZE
_ TYPE
[ Vpd
. DatumType
]
106 if Vpd
. MaxDatumSize
<= 0 :
107 EdkLogger
. error ( "VpdInfoFile" , BuildToolError
. PARAMETER_INVALID
,
108 "Invalid max datum size for VPD PCD %s . %s " % ( Vpd
. TokenSpaceGuidCName
, Vpd
. TokenCName
))
110 if Vpd
not in self
._ VpdArray
. keys ():
112 # If there is no Vpd instance in dict, that imply this offset for a given SKU is a new one
114 self
._ VpdArray
[ Vpd
] = [ Offset
]
117 # If there is an offset for a specific SKU in dict, then append this offset for other sku to array.
119 self
._ VpdArray
[ Vpd
]. append ( Offset
)
122 ## Generate VPD PCD information into a text file
124 # If parameter FilePath is invalid, then assert.
126 # @param FilePath The given file path which would hold VPD information
127 def Write ( self
, FilePath
):
128 if not ( FilePath
!= None or len ( FilePath
) != 0 ):
129 EdkLogger
. error ( "VpdInfoFile" , BuildToolError
. PARAMETER_INVALID
,
130 "Invalid parameter FilePath: %s ." % FilePath
)
132 Content
= FILE_COMMENT_TEMPLATE
133 Pcds
= self
._ VpdArray
. keys ()
137 PcdTokenCName
= Pcd
. TokenCName
138 for PcdItem
in GlobalData
. MixedPcd
:
139 if ( Pcd
. TokenCName
, Pcd
. TokenSpaceGuidCName
) in GlobalData
. MixedPcd
[ PcdItem
]:
140 PcdTokenCName
= PcdItem
[ 0 ]
141 for Offset
in self
._ VpdArray
[ Pcd
]:
142 PcdValue
= str ( Pcd
. SkuInfoList
[ Pcd
. SkuInfoList
. keys ()[ i
]]. DefaultValue
). strip ()
144 PcdValue
= Pcd
. DefaultValue
146 Content
+= " %s . %s | %s | %s | %s | %s \n " % ( Pcd
. TokenSpaceGuidCName
, PcdTokenCName
, str ( Pcd
. SkuInfoList
. keys ()[ i
]), str ( Offset
). strip (), str ( Pcd
. MaxDatumSize
). strip (), PcdValue
)
149 return SaveFileOnChange ( FilePath
, Content
, False )
151 ## Read an existing VPD PCD info file.
153 # This routine will read VPD PCD information from existing file and construct
154 # internal PcdClassObject array.
155 # This routine could be used by third-party tool to parse VPD info file content.
157 # @param FilePath The full path string for existing VPD PCD info file.
158 def Read ( self
, FilePath
):
160 fd
= open ( FilePath
, "r" )
162 EdkLogger
. error ( "VpdInfoFile" ,
163 BuildToolError
. FILE_OPEN_FAILURE
,
164 "Fail to open file %s for written." % FilePath
)
165 Lines
= fd
. readlines ()
168 if len ( Line
) == 0 or Line
. startswith ( "#" ):
172 # the line must follow output format defined in BPDG spec.
175 PcdName
, SkuId
, Offset
, Size
, Value
= Line
. split ( "#" )[ 0 ]. split ( "|" )
176 PcdName
, SkuId
, Offset
, Size
, Value
= PcdName
. strip (), SkuId
. strip (), Offset
. strip (), Size
. strip (), Value
. strip ()
177 TokenSpaceName
, PcdTokenName
= PcdName
. split ( "." )
179 EdkLogger
. error ( "BPDG" , BuildToolError
. PARSER_ERROR
, "Fail to parse VPD information file %s " % FilePath
)
183 if ( TokenSpaceName
, PcdTokenName
) not in self
._ VpdInfo
:
184 self
._ VpdInfo
[( TokenSpaceName
, PcdTokenName
)] = []
185 self
._ VpdInfo
[( TokenSpaceName
, PcdTokenName
)]. append (( SkuId
, Offset
, Value
))
186 for VpdObject
in self
._ VpdArray
. keys ():
187 VpdObjectTokenCName
= VpdObject
. TokenCName
188 for PcdItem
in GlobalData
. MixedPcd
:
189 if ( VpdObject
. TokenCName
, VpdObject
. TokenSpaceGuidCName
) in GlobalData
. MixedPcd
[ PcdItem
]:
190 VpdObjectTokenCName
= PcdItem
[ 0 ]
191 for sku
in VpdObject
. SkuInfoList
. keys ():
192 if VpdObject
. TokenSpaceGuidCName
== TokenSpaceName
and VpdObjectTokenCName
== PcdTokenName
. strip () and sku
== SkuId
:
193 if self
._ VpdArray
[ VpdObject
][ VpdObject
. SkuInfoList
. keys (). index ( sku
)] == "*" :
195 EdkLogger
. error ( "BPDG" , BuildToolError
. FORMAT_INVALID
, "The offset of %s has not been fixed up by third-party BPDG tool." % PcdName
)
196 self
._ VpdArray
[ VpdObject
][ VpdObject
. SkuInfoList
. keys (). index ( sku
)] = Offset
199 EdkLogger
. error ( "BPDG" , BuildToolError
. PARSER_ERROR
, "Can not find PCD defined in VPD guid file." )
201 ## Get count of VPD PCD collected from platform's autogen when building.
203 # @return The integer count value
206 for OffsetList
in self
._ VpdArray
. values ():
207 Count
+= len ( OffsetList
)
211 ## Get an offset value for a given VPD PCD
213 # Because BPDG only support one Sku, so only return offset for SKU default.
215 # @param vpd A given VPD PCD
216 def GetOffset ( self
, vpd
):
217 if not self
._ VpdArray
. has_key ( vpd
):
220 if len ( self
._ VpdArray
[ vpd
]) == 0 :
223 return self
._ VpdArray
[ vpd
]
224 def GetVpdInfo ( self
,( PcdTokenName
, TokenSpaceName
)):
225 return self
._ VpdInfo
. get (( TokenSpaceName
, PcdTokenName
))
227 ## Call external BPDG tool to process VPD file
229 # @param ToolPath The string path name for BPDG tool
230 # @param VpdFileName The string path name for VPD information guid.txt
232 def CallExtenalBPDGTool ( ToolPath
, VpdFileName
):
233 assert ToolPath
!= None , "Invalid parameter ToolPath"
234 assert VpdFileName
!= None and os
. path
. exists ( VpdFileName
), "Invalid parameter VpdFileName"
236 OutputDir
= os
. path
. dirname ( VpdFileName
)
237 FileName
= os
. path
. basename ( VpdFileName
)
238 BaseName
, ext
= os
. path
. splitext ( FileName
)
239 OutputMapFileName
= os
. path
. join ( OutputDir
, " %s .map" % BaseName
)
240 OutputBinFileName
= os
. path
. join ( OutputDir
, " %s .bin" % BaseName
)
243 PopenObject
= subprocess
. Popen ( ' ' . join ([ ToolPath
,
244 '-o' , OutputBinFileName
,
245 '-m' , OutputMapFileName
,
249 stdout
= subprocess
. PIPE
,
250 stderr
= subprocess
. PIPE
,
253 EdkLogger
. error ( "BPDG" , BuildToolError
. COMMAND_FAILURE
, ExtraData
= " %s " % ( str ( X
)))
254 ( out
, error
) = PopenObject
. communicate ()
256 while PopenObject
. returncode
== None :
259 if PopenObject
. returncode
!= 0 :
260 if PopenObject
. returncode
!= 0 :
261 EdkLogger
. debug ( EdkLogger
. DEBUG_1
, "Fail to call BPDG tool" , str ( error
))
262 EdkLogger
. error ( "BPDG" , BuildToolError
. COMMAND_FAILURE
, "Fail to execute BPDG tool with exit code: %d , the error message is: \n %s " % \
263 ( PopenObject
. returncode
, str ( error
)))
265 return PopenObject
. returncode