]>
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 - 2018, Intel Corporation. All rights reserved.<BR>
10 # SPDX-License-Identifier: BSD-2-Clause-Patent
12 from __future__
import print_function
13 import Common
. LongFilePathOs
as os
15 import Common
. EdkLogger
as EdkLogger
16 import Common
. BuildToolError
as BuildToolError
18 import Common
. GlobalData
as GlobalData
19 from Common
. LongFilePathSupport
import OpenLongFilePath
as open
20 from Common
. Misc
import SaveFileOnChange
21 from Common
. DataType
import *
23 FILE_COMMENT_TEMPLATE
= \
27 # THIS IS AUTO-GENERATED FILE BY BUILD TOOLS AND PLEASE DO NOT MAKE MODIFICATION.
29 # This file lists all VPD informations for a platform collected by build.exe.
31 # Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
32 # This program and the accompanying materials
33 # are licensed and made available under the terms and conditions of the BSD License
34 # which accompanies this distribution. The full text of the license may be found at
35 # http://opensource.org/licenses/bsd-license.php
37 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
38 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
43 ## The class manage VpdInfoFile.
45 # 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.
46 # Format for this file (using EBNF notation) is:
47 # <File> :: = [<CommentBlock>]
49 # <CommentBlock> ::= ["#" <String> <EOL>]*
50 # <PcdEntry> ::= <PcdName> "|" <Offset> "|" <Size> "|" <Value> <EOL>
51 # <PcdName> ::= <TokenSpaceCName> "." <PcdCName>
52 # <TokenSpaceCName> ::= C Variable Name of the Token Space GUID
53 # <PcdCName> ::= C Variable Name of the PCD
54 # <Offset> ::= {TAB_STAR} {<HexNumber>}
55 # <HexNumber> ::= "0x" (a-fA-F0-9){1,8}
56 # <Size> ::= <HexNumber>
57 # <Value> ::= {<HexNumber>} {<NonNegativeInt>} {<QString>} {<Array>}
58 # <NonNegativeInt> ::= (0-9)+
59 # <QString> ::= ["L"] <DblQuote> <String> <DblQuote>
61 # <Array> ::= {<CArray>} {<NList>}
62 # <CArray> ::= "{" <HexNumber> ["," <HexNumber>]* "}"
63 # <NList> ::= <HexNumber> ["," <HexNumber>]*
70 ## Dictionary for VPD in following format
72 # Key : PcdClassObject instance.
73 # @see BuildClassObject.PcdClassObject
74 # Value : offset in different SKU such as [sku1_offset, sku2_offset]
78 ## Add a VPD PCD collected from platform's autogen when building.
80 # @param vpds The list of VPD PCD collected for a platform.
81 # @see BuildClassObject.PcdClassObject
83 # @param offset integer value for VPD's offset in specific SKU.
85 def Add ( self
, Vpd
, skuname
, Offset
):
87 EdkLogger
. error ( "VpdInfoFile" , BuildToolError
. ATTRIBUTE_UNKNOWN_ERROR
, "Invalid VPD PCD entry." )
89 if not ( Offset
>= "0" or Offset
== TAB_STAR
):
90 EdkLogger
. error ( "VpdInfoFile" , BuildToolError
. PARAMETER_INVALID
, "Invalid offset parameter: %s ." % Offset
)
92 if Vpd
. DatumType
== TAB_VOID
:
93 if Vpd
. MaxDatumSize
<= "0" :
94 EdkLogger
. error ( "VpdInfoFile" , BuildToolError
. PARAMETER_INVALID
,
95 "Invalid max datum size for VPD PCD %s . %s " % ( Vpd
. TokenSpaceGuidCName
, Vpd
. TokenCName
))
96 elif Vpd
. DatumType
in TAB_PCD_NUMERIC_TYPES
:
97 if not Vpd
. MaxDatumSize
:
98 Vpd
. MaxDatumSize
= MAX_SIZE_TYPE
[ Vpd
. DatumType
]
100 if Vpd
. MaxDatumSize
<= "0" :
101 EdkLogger
. error ( "VpdInfoFile" , BuildToolError
. PARAMETER_INVALID
,
102 "Invalid max datum size for VPD PCD %s . %s " % ( Vpd
. TokenSpaceGuidCName
, Vpd
. TokenCName
))
104 if Vpd
not in self
._ VpdArray
:
106 # If there is no Vpd instance in dict, that imply this offset for a given SKU is a new one
108 self
._ VpdArray
[ Vpd
] = {}
110 self
._ VpdArray
[ Vpd
]. update ({ skuname
: Offset
})
113 ## Generate VPD PCD information into a text file
115 # If parameter FilePath is invalid, then assert.
117 # @param FilePath The given file path which would hold VPD information
118 def Write ( self
, FilePath
):
119 if not ( FilePath
is not None or len ( FilePath
) != 0 ):
120 EdkLogger
. error ( "VpdInfoFile" , BuildToolError
. PARAMETER_INVALID
,
121 "Invalid parameter FilePath: %s ." % FilePath
)
123 Content
= FILE_COMMENT_TEMPLATE
124 Pcds
= sorted ( self
._ VpdArray
. keys (), key
= lambda x
: x
. TokenCName
)
127 PcdTokenCName
= Pcd
. TokenCName
128 for PcdItem
in GlobalData
. MixedPcd
:
129 if ( Pcd
. TokenCName
, Pcd
. TokenSpaceGuidCName
) in GlobalData
. MixedPcd
[ PcdItem
]:
130 PcdTokenCName
= PcdItem
[ 0 ]
131 for skuname
in self
._ VpdArray
[ Pcd
]:
132 PcdValue
= str ( Pcd
. SkuInfoList
[ skuname
]. DefaultValue
). strip ()
134 PcdValue
= Pcd
. DefaultValue
136 Content
+= " %s . %s | %s | %s | %s | %s \n " % ( Pcd
. TokenSpaceGuidCName
, PcdTokenCName
, skuname
, str ( self
._ VpdArray
[ Pcd
][ skuname
]). strip (), str ( Pcd
. MaxDatumSize
). strip (), PcdValue
)
139 return SaveFileOnChange ( FilePath
, Content
, False )
141 ## Read an existing VPD PCD info file.
143 # This routine will read VPD PCD information from existing file and construct
144 # internal PcdClassObject array.
145 # This routine could be used by third-party tool to parse VPD info file content.
147 # @param FilePath The full path string for existing VPD PCD info file.
148 def Read ( self
, FilePath
):
150 fd
= open ( FilePath
, "r" )
152 EdkLogger
. error ( "VpdInfoFile" ,
153 BuildToolError
. FILE_OPEN_FAILURE
,
154 "Fail to open file %s for written." % FilePath
)
155 Lines
= fd
. readlines ()
158 if len ( Line
) == 0 or Line
. startswith ( "#" ):
162 # the line must follow output format defined in BPDG spec.
165 PcdName
, SkuId
, Offset
, Size
, Value
= Line
. split ( "#" )[ 0 ]. split ( "|" )
166 PcdName
, SkuId
, Offset
, Size
, Value
= PcdName
. strip (), SkuId
. strip (), Offset
. strip (), Size
. strip (), Value
. strip ()
167 TokenSpaceName
, PcdTokenName
= PcdName
. split ( "." )
169 EdkLogger
. error ( "BPDG" , BuildToolError
. PARSER_ERROR
, "Fail to parse VPD information file %s " % FilePath
)
173 if ( TokenSpaceName
, PcdTokenName
) not in self
._ VpdInfo
:
174 self
._ VpdInfo
[( TokenSpaceName
, PcdTokenName
)] = {}
175 self
._ VpdInfo
[( TokenSpaceName
, PcdTokenName
)][( SkuId
, Offset
)] = Value
176 for VpdObject
in self
._ VpdArray
:
177 VpdObjectTokenCName
= VpdObject
. TokenCName
178 for PcdItem
in GlobalData
. MixedPcd
:
179 if ( VpdObject
. TokenCName
, VpdObject
. TokenSpaceGuidCName
) in GlobalData
. MixedPcd
[ PcdItem
]:
180 VpdObjectTokenCName
= PcdItem
[ 0 ]
181 for sku
in VpdObject
. SkuInfoList
:
182 if VpdObject
. TokenSpaceGuidCName
== TokenSpaceName
and VpdObjectTokenCName
== PcdTokenName
. strip () and sku
== SkuId
:
183 if self
._ VpdArray
[ VpdObject
][ sku
] == TAB_STAR
:
184 if Offset
== TAB_STAR
:
185 EdkLogger
. error ( "BPDG" , BuildToolError
. FORMAT_INVALID
, "The offset of %s has not been fixed up by third-party BPDG tool." % PcdName
)
186 self
._ VpdArray
[ VpdObject
][ sku
] = Offset
189 EdkLogger
. error ( "BPDG" , BuildToolError
. PARSER_ERROR
, "Can not find PCD defined in VPD guid file." )
191 ## Get count of VPD PCD collected from platform's autogen when building.
193 # @return The integer count value
196 for OffsetList
in self
._ VpdArray
. values ():
197 Count
+= len ( OffsetList
)
201 ## Get an offset value for a given VPD PCD
203 # Because BPDG only support one Sku, so only return offset for SKU default.
205 # @param vpd A given VPD PCD
206 def GetOffset ( self
, vpd
):
207 if vpd
not in self
._ VpdArray
:
210 if len ( self
._ VpdArray
[ vpd
]) == 0 :
213 return self
._ VpdArray
[ vpd
]
214 def GetVpdInfo ( self
, arg
):
215 ( PcdTokenName
, TokenSpaceName
) = arg
216 return [( sku
, offset
, value
) for ( sku
, offset
), value
in self
._ VpdInfo
. get (( TokenSpaceName
, PcdTokenName
)). items ()]
218 ## Call external BPDG tool to process VPD file
220 # @param ToolPath The string path name for BPDG tool
221 # @param VpdFileName The string path name for VPD information guid.txt
223 def CallExtenalBPDGTool ( ToolPath
, VpdFileName
):
224 assert ToolPath
is not None , "Invalid parameter ToolPath"
225 assert VpdFileName
is not None and os
. path
. exists ( VpdFileName
), "Invalid parameter VpdFileName"
227 OutputDir
= os
. path
. dirname ( VpdFileName
)
228 FileName
= os
. path
. basename ( VpdFileName
)
229 BaseName
, ext
= os
. path
. splitext ( FileName
)
230 OutputMapFileName
= os
. path
. join ( OutputDir
, " %s .map" % BaseName
)
231 OutputBinFileName
= os
. path
. join ( OutputDir
, " %s .bin" % BaseName
)
234 PopenObject
= subprocess
. Popen ( ' ' . join ([ ToolPath
,
235 '-o' , OutputBinFileName
,
236 '-m' , OutputMapFileName
,
240 stdout
= subprocess
. PIPE
,
241 stderr
= subprocess
. PIPE
,
243 except Exception as X
:
244 EdkLogger
. error ( "BPDG" , BuildToolError
. COMMAND_FAILURE
, ExtraData
= str ( X
))
245 ( out
, error
) = PopenObject
. communicate ()
246 print ( out
. decode ( encoding
= 'utf-8' , errors
= 'ignore' ))
247 while PopenObject
. returncode
is None :
250 if PopenObject
. returncode
!= 0 :
251 EdkLogger
. debug ( EdkLogger
. DEBUG_1
, "Fail to call BPDG tool" , str ( error
))
252 EdkLogger
. error ( "BPDG" , BuildToolError
. COMMAND_FAILURE
, "Fail to execute BPDG tool with exit code: %d , the error message is: \n %s " % \
253 ( PopenObject
. returncode
, str ( error
)))
255 return PopenObject
. returncode