]>
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, 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.
20 import Common
. EdkLogger
as EdkLogger
21 import Common
. BuildToolError
as BuildToolError
24 FILE_COMMENT_TEMPLATE
= \
28 # THIS IS AUTO-GENERATED FILE BY BUILD TOOLS AND PLEASE DO NOT MAKE MODIFICATION.
30 # This file lists all VPD informations for a platform collected by build.exe.
32 # Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
33 # This program and the accompanying materials
34 # are licensed and made available under the terms and conditions of the BSD License
35 # which accompanies this distribution. The full text of the license may be found at
36 # http://opensource.org/licenses/bsd-license.php
38 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
39 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
44 ## The class manage VpdInfoFile.
46 # 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.
47 # Format for this file (using EBNF notation) is:
48 # <File> :: = [<CommentBlock>]
50 # <CommentBlock> ::= ["#" <String> <EOL>]*
51 # <PcdEntry> ::= <PcdName> "|" <Offset> "|" <Size> "|" <Value> <EOL>
52 # <PcdName> ::= <TokenSpaceCName> "." <PcdCName>
53 # <TokenSpaceCName> ::= C Variable Name of the Token Space GUID
54 # <PcdCName> ::= C Variable Name of the PCD
55 # <Offset> ::= {"*"} {<HexNumber>}
56 # <HexNumber> ::= "0x" (a-fA-F0-9){1,8}
57 # <Size> ::= <HexNumber>
58 # <Value> ::= {<HexNumber>} {<NonNegativeInt>} {<QString>} {<Array>}
59 # <NonNegativeInt> ::= (0-9)+
60 # <QString> ::= ["L"] <DblQuote> <String> <DblQuote>
62 # <Array> ::= {<CArray>} {<NList>}
63 # <CArray> ::= "{" <HexNumber> ["," <HexNumber>]* "}"
64 # <NList> ::= <HexNumber> ["," <HexNumber>]*
68 ## The mapping dictionary from datum type to size string.
69 _MAX_SIZE_TYPE
= { "BOOLEAN" : "1" , "UINT8" : "1" , "UINT16" : "2" , "UINT32" : "4" , "UINT64" : "8" }
73 ## Dictionary for VPD in following format
75 # Key : PcdClassObject instance.
76 # @see BuildClassObject.PcdClassObject
77 # Value : offset in different SKU such as [sku1_offset, sku2_offset]
80 ## Add a VPD PCD collected from platform's autogen when building.
82 # @param vpds The list of VPD PCD collected for a platform.
83 # @see BuildClassObject.PcdClassObject
85 # @param offset integer value for VPD's offset in specific SKU.
87 def Add ( self
, Vpd
, Offset
):
89 EdkLogger
. error ( "VpdInfoFile" , BuildToolError
. ATTRIBUTE_UNKNOWN_ERROR
, "Invalid VPD PCD entry." )
91 if not ( Offset
>= 0 or Offset
== "*" ):
92 EdkLogger
. error ( "VpdInfoFile" , BuildToolError
. PARAMETER_INVALID
, "Invalid offset parameter: %s ." % Offset
)
94 if Vpd
. DatumType
== "VOID*" :
95 if Vpd
. MaxDatumSize
<= 0 :
96 EdkLogger
. error ( "VpdInfoFile" , BuildToolError
. PARAMETER_INVALID
,
97 "Invalid max datum size for VPD PCD %s . %s " % ( Vpd
. TokenSpaceGuidCName
, Vpd
. TokenCName
))
98 elif Vpd
. DatumType
in [ "BOOLEAN" , "UINT8" , "UINT16" , "UINT32" , "UINT64" ]:
99 if Vpd
. MaxDatumSize
== None or Vpd
. MaxDatumSize
== "" :
100 Vpd
. MaxDatumSize
= VpdInfoFile
._ MAX
_ SIZE
_ TYPE
[ Vpd
. DatumType
]
102 EdkLogger
. error ( "VpdInfoFile" , BuildToolError
. PARAMETER_INVALID
,
103 "Invalid DatumType %s for VPD PCD %s . %s " % ( Vpd
. DatumType
, Vpd
. TokenSpaceGuidCName
, Vpd
. TokenCName
))
105 if Vpd
not in self
._ VpdArray
. keys ():
107 # If there is no Vpd instance in dict, that imply this offset for a given SKU is a new one
109 self
._ VpdArray
[ Vpd
] = [ Offset
]
112 # If there is an offset for a specific SKU in dict, then append this offset for other sku to array.
114 self
._ VpdArray
[ Vpd
]. append ( Offset
)
117 ## Generate VPD PCD information into a text file
119 # If parameter FilePath is invalid, then assert.
121 # @param FilePath The given file path which would hold VPD information
122 def Write ( self
, FilePath
):
123 if not ( FilePath
!= None or len ( FilePath
) != 0 ):
124 EdkLogger
. error ( "VpdInfoFile" , BuildToolError
. PARAMETER_INVALID
,
125 "Invalid parameter FilePath: %s ." % FilePath
)
127 fd
= open ( FilePath
, "w" )
129 EdkLogger
. error ( "VpdInfoFile" ,
130 BuildToolError
. FILE_OPEN_FAILURE
,
131 "Fail to open file %s for written." % FilePath
)
135 fd
. write ( FILE_COMMENT_TEMPLATE
)
137 # write each of PCD in VPD type
138 for Pcd
in self
._ VpdArray
. keys ():
139 for Offset
in self
._ VpdArray
[ Pcd
]:
140 PcdValue
= str ( Pcd
. SkuInfoList
[ Pcd
. SkuInfoList
. keys ()[ 0 ]]. DefaultValue
). strip ()
142 PcdValue
= Pcd
. DefaultValue
144 fd
. write ( " %s . %s | %s | %s | %s \n " % ( Pcd
. TokenSpaceGuidCName
, Pcd
. TokenCName
, str ( Offset
). strip (), str ( Pcd
. MaxDatumSize
). strip (), PcdValue
))
146 EdkLogger
. error ( "VpdInfoFile" ,
147 BuildToolError
. FILE_WRITE_FAILURE
,
148 "Fail to write file %s " % FilePath
)
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
, Offset
, Size
, Value
= Line
. split ( "#" )[ 0 ]. split ( "|" )
176 TokenSpaceName
, PcdTokenName
= PcdName
. split ( "." )
178 EdkLogger
. error ( "BPDG" , BuildToolError
. PARSER_ERROR
, "Fail to parse VPD information file %s " % FilePath
)
181 for VpdObject
in self
._ VpdArray
. keys ():
182 if VpdObject
. TokenSpaceGuidCName
== TokenSpaceName
and VpdObject
. TokenCName
== PcdTokenName
. strip ():
183 if self
._ VpdArray
[ VpdObject
][ 0 ] == "*" :
185 EdkLogger
. error ( "BPDG" , BuildToolError
. FORMAT_INVALID
, "The offset of %s has not been fixed up by third-party BPDG tool." % PcdName
)
187 self
._ VpdArray
[ VpdObject
][ 0 ] = Offset
191 EdkLogger
. error ( "BPDG" , BuildToolError
. PARSER_ERROR
, "Can not find PCD defined in VPD guid file." )
193 ## Get count of VPD PCD collected from platform's autogen when building.
195 # @return The integer count value
198 for OffsetList
in self
._ VpdArray
. values ():
199 Count
+= len ( OffsetList
)
203 ## Get an offset value for a given VPD PCD
205 # Because BPDG only support one Sku, so only return offset for SKU default.
207 # @param vpd A given VPD PCD
208 def GetOffset ( self
, vpd
):
209 if not self
._ VpdArray
. has_key ( vpd
):
212 if len ( self
._ VpdArray
[ vpd
]) == 0 :
215 return self
._ VpdArray
[ vpd
]
217 ## Call external BPDG tool to process VPD file
219 # @param ToolPath The string path name for BPDG tool
220 # @param VpdFileName The string path name for VPD information guid.txt
222 def CallExtenalBPDGTool ( ToolPath
, VpdFileName
):
223 assert ToolPath
!= None , "Invalid parameter ToolPath"
224 assert VpdFileName
!= None and os
. path
. exists ( VpdFileName
), "Invalid parameter VpdFileName"
226 OutputDir
= os
. path
. dirname ( VpdFileName
)
227 FileName
= os
. path
. basename ( VpdFileName
)
228 BaseName
, ext
= os
. path
. splitext ( FileName
)
229 OutputMapFileName
= os
. path
. join ( OutputDir
, " %s .map" % BaseName
)
230 OutputBinFileName
= os
. path
. join ( OutputDir
, " %s .bin" % BaseName
)
233 PopenObject
= subprocess
. Popen ([ ToolPath
,
234 '-o' , OutputBinFileName
,
235 '-m' , OutputMapFileName
,
239 stdout
= subprocess
. PIPE
,
240 stderr
= subprocess
. PIPE
)
242 EdkLogger
. error ( "BPDG" , BuildToolError
. COMMAND_FAILURE
, ExtraData
= " %s " % ( str ( X
)))
243 ( out
, error
) = PopenObject
. communicate ()
245 while PopenObject
. returncode
== None :
248 if PopenObject
. returncode
!= 0 :
249 if PopenObject
. returncode
!= 0 :
250 EdkLogger
. debug ( EdkLogger
. DEBUG_1
, "Fail to call BPDG tool" , str ( error
))
251 EdkLogger
. error ( "BPDG" , BuildToolError
. COMMAND_FAILURE
, "Fail to execute BPDG tool with exit code: %d , the error message is: \n %s " % \
252 ( PopenObject
. returncode
, str ( error
)))
254 return PopenObject
. returncode