]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Common/VpdInfoFile.py
BaseTools: Remove the "from __future__ import" items
[mirror_edk2.git] / BaseTools / Source / Python / Common / VpdInfoFile.py
1 ## @file
2 #
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
7 #
8 #
9 # Copyright (c) 2010 - 2018, 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
14 #
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.
17 #
18 import Common.LongFilePathOs as os
19 import re
20 import Common.EdkLogger as EdkLogger
21 import Common.BuildToolError as BuildToolError
22 import subprocess
23 import Common.GlobalData as GlobalData
24 from Common.LongFilePathSupport import OpenLongFilePath as open
25 from Common.Misc import SaveFileOnChange
26 from Common.DataType import *
27
28 FILE_COMMENT_TEMPLATE = \
29 """
30 ## @file
31 #
32 # THIS IS AUTO-GENERATED FILE BY BUILD TOOLS AND PLEASE DO NOT MAKE MODIFICATION.
33 #
34 # This file lists all VPD informations for a platform collected by build.exe.
35 #
36 # Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
37 # This program and the accompanying materials
38 # are licensed and made available under the terms and conditions of the BSD License
39 # which accompanies this distribution. The full text of the license may be found at
40 # http://opensource.org/licenses/bsd-license.php
41 #
42 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
43 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
44 #
45
46 """
47
48 ## The class manage VpdInfoFile.
49 #
50 # 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.
51 # Format for this file (using EBNF notation) is:
52 # <File> :: = [<CommentBlock>]
53 # [<PcdEntry>]*
54 # <CommentBlock> ::= ["#" <String> <EOL>]*
55 # <PcdEntry> ::= <PcdName> "|" <Offset> "|" <Size> "|" <Value> <EOL>
56 # <PcdName> ::= <TokenSpaceCName> "." <PcdCName>
57 # <TokenSpaceCName> ::= C Variable Name of the Token Space GUID
58 # <PcdCName> ::= C Variable Name of the PCD
59 # <Offset> ::= {"*"} {<HexNumber>}
60 # <HexNumber> ::= "0x" (a-fA-F0-9){1,8}
61 # <Size> ::= <HexNumber>
62 # <Value> ::= {<HexNumber>} {<NonNegativeInt>} {<QString>} {<Array>}
63 # <NonNegativeInt> ::= (0-9)+
64 # <QString> ::= ["L"] <DblQuote> <String> <DblQuote>
65 # <DblQuote> ::= 0x22
66 # <Array> ::= {<CArray>} {<NList>}
67 # <CArray> ::= "{" <HexNumber> ["," <HexNumber>]* "}"
68 # <NList> ::= <HexNumber> ["," <HexNumber>]*
69 #
70 class VpdInfoFile:
71
72 _rVpdPcdLine = None
73 ## Constructor
74 def __init__(self):
75 ## Dictionary for VPD in following format
76 #
77 # Key : PcdClassObject instance.
78 # @see BuildClassObject.PcdClassObject
79 # Value : offset in different SKU such as [sku1_offset, sku2_offset]
80 self._VpdArray = {}
81 self._VpdInfo = {}
82
83 ## Add a VPD PCD collected from platform's autogen when building.
84 #
85 # @param vpds The list of VPD PCD collected for a platform.
86 # @see BuildClassObject.PcdClassObject
87 #
88 # @param offset integer value for VPD's offset in specific SKU.
89 #
90 def Add(self, Vpd, skuname, Offset):
91 if (Vpd is None):
92 EdkLogger.error("VpdInfoFile", BuildToolError.ATTRIBUTE_UNKNOWN_ERROR, "Invalid VPD PCD entry.")
93
94 if not (Offset >= 0 or Offset == "*"):
95 EdkLogger.error("VpdInfoFile", BuildToolError.PARAMETER_INVALID, "Invalid offset parameter: %s." % Offset)
96
97 if Vpd.DatumType == TAB_VOID:
98 if Vpd.MaxDatumSize <= 0:
99 EdkLogger.error("VpdInfoFile", BuildToolError.PARAMETER_INVALID,
100 "Invalid max datum size for VPD PCD %s.%s" % (Vpd.TokenSpaceGuidCName, Vpd.TokenCName))
101 elif Vpd.DatumType in TAB_PCD_NUMERIC_TYPES:
102 if not Vpd.MaxDatumSize:
103 Vpd.MaxDatumSize = MAX_SIZE_TYPE[Vpd.DatumType]
104 else:
105 if Vpd.MaxDatumSize <= 0:
106 EdkLogger.error("VpdInfoFile", BuildToolError.PARAMETER_INVALID,
107 "Invalid max datum size for VPD PCD %s.%s" % (Vpd.TokenSpaceGuidCName, Vpd.TokenCName))
108
109 if Vpd not in self._VpdArray:
110 #
111 # If there is no Vpd instance in dict, that imply this offset for a given SKU is a new one
112 #
113 self._VpdArray[Vpd] = {}
114
115 self._VpdArray[Vpd].update({skuname:Offset})
116
117
118 ## Generate VPD PCD information into a text file
119 #
120 # If parameter FilePath is invalid, then assert.
121 # If
122 # @param FilePath The given file path which would hold VPD information
123 def Write(self, FilePath):
124 if not (FilePath is not None or len(FilePath) != 0):
125 EdkLogger.error("VpdInfoFile", BuildToolError.PARAMETER_INVALID,
126 "Invalid parameter FilePath: %s." % FilePath)
127
128 Content = FILE_COMMENT_TEMPLATE
129 Pcds = sorted(self._VpdArray.keys())
130 for Pcd in Pcds:
131 i = 0
132 PcdTokenCName = Pcd.TokenCName
133 for PcdItem in GlobalData.MixedPcd:
134 if (Pcd.TokenCName, Pcd.TokenSpaceGuidCName) in GlobalData.MixedPcd[PcdItem]:
135 PcdTokenCName = PcdItem[0]
136 for skuname in self._VpdArray[Pcd]:
137 PcdValue = str(Pcd.SkuInfoList[skuname].DefaultValue).strip()
138 if PcdValue == "" :
139 PcdValue = Pcd.DefaultValue
140
141 Content += "%s.%s|%s|%s|%s|%s \n" % (Pcd.TokenSpaceGuidCName, PcdTokenCName, skuname, str(self._VpdArray[Pcd][skuname]).strip(), str(Pcd.MaxDatumSize).strip(), PcdValue)
142 i += 1
143
144 return SaveFileOnChange(FilePath, Content, False)
145
146 ## Read an existing VPD PCD info file.
147 #
148 # This routine will read VPD PCD information from existing file and construct
149 # internal PcdClassObject array.
150 # This routine could be used by third-party tool to parse VPD info file content.
151 #
152 # @param FilePath The full path string for existing VPD PCD info file.
153 def Read(self, FilePath):
154 try:
155 fd = open(FilePath, "r")
156 except:
157 EdkLogger.error("VpdInfoFile",
158 BuildToolError.FILE_OPEN_FAILURE,
159 "Fail to open file %s for written." % FilePath)
160 Lines = fd.readlines()
161 for Line in Lines:
162 Line = Line.strip()
163 if len(Line) == 0 or Line.startswith("#"):
164 continue
165
166 #
167 # the line must follow output format defined in BPDG spec.
168 #
169 try:
170 PcdName, SkuId, Offset, Size, Value = Line.split("#")[0].split("|")
171 PcdName, SkuId, Offset, Size, Value = PcdName.strip(), SkuId.strip(), Offset.strip(), Size.strip(), Value.strip()
172 TokenSpaceName, PcdTokenName = PcdName.split(".")
173 except:
174 EdkLogger.error("BPDG", BuildToolError.PARSER_ERROR, "Fail to parse VPD information file %s" % FilePath)
175
176 Found = False
177
178 if (TokenSpaceName, PcdTokenName) not in self._VpdInfo:
179 self._VpdInfo[(TokenSpaceName, PcdTokenName)] = []
180 self._VpdInfo[(TokenSpaceName, PcdTokenName)].append((SkuId, Offset, Value))
181 for VpdObject in self._VpdArray:
182 VpdObjectTokenCName = VpdObject.TokenCName
183 for PcdItem in GlobalData.MixedPcd:
184 if (VpdObject.TokenCName, VpdObject.TokenSpaceGuidCName) in GlobalData.MixedPcd[PcdItem]:
185 VpdObjectTokenCName = PcdItem[0]
186 for sku in VpdObject.SkuInfoList:
187 if VpdObject.TokenSpaceGuidCName == TokenSpaceName and VpdObjectTokenCName == PcdTokenName.strip() and sku == SkuId:
188 if self._VpdArray[VpdObject][sku] == "*":
189 if Offset == "*":
190 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID, "The offset of %s has not been fixed up by third-party BPDG tool." % PcdName)
191 self._VpdArray[VpdObject][sku] = Offset
192 Found = True
193 if not Found:
194 EdkLogger.error("BPDG", BuildToolError.PARSER_ERROR, "Can not find PCD defined in VPD guid file.")
195
196 ## Get count of VPD PCD collected from platform's autogen when building.
197 #
198 # @return The integer count value
199 def GetCount(self):
200 Count = 0
201 for OffsetList in self._VpdArray.values():
202 Count += len(OffsetList)
203
204 return Count
205
206 ## Get an offset value for a given VPD PCD
207 #
208 # Because BPDG only support one Sku, so only return offset for SKU default.
209 #
210 # @param vpd A given VPD PCD
211 def GetOffset(self, vpd):
212 if vpd not in self._VpdArray:
213 return None
214
215 if len(self._VpdArray[vpd]) == 0:
216 return None
217
218 return self._VpdArray[vpd]
219 def GetVpdInfo(self, arg):
220 (PcdTokenName, TokenSpaceName) = arg
221 return self._VpdInfo.get((TokenSpaceName, PcdTokenName))
222
223 ## Call external BPDG tool to process VPD file
224 #
225 # @param ToolPath The string path name for BPDG tool
226 # @param VpdFileName The string path name for VPD information guid.txt
227 #
228 def CallExtenalBPDGTool(ToolPath, VpdFileName):
229 assert ToolPath is not None, "Invalid parameter ToolPath"
230 assert VpdFileName is not None and os.path.exists(VpdFileName), "Invalid parameter VpdFileName"
231
232 OutputDir = os.path.dirname(VpdFileName)
233 FileName = os.path.basename(VpdFileName)
234 BaseName, ext = os.path.splitext(FileName)
235 OutputMapFileName = os.path.join(OutputDir, "%s.map" % BaseName)
236 OutputBinFileName = os.path.join(OutputDir, "%s.bin" % BaseName)
237
238 try:
239 PopenObject = subprocess.Popen(' '.join([ToolPath,
240 '-o', OutputBinFileName,
241 '-m', OutputMapFileName,
242 '-q',
243 '-f',
244 VpdFileName]),
245 stdout=subprocess.PIPE,
246 stderr= subprocess.PIPE,
247 shell=True)
248 except Exception as X:
249 EdkLogger.error("BPDG", BuildToolError.COMMAND_FAILURE, ExtraData=str(X))
250 (out, error) = PopenObject.communicate()
251 print(out)
252 while PopenObject.returncode is None :
253 PopenObject.wait()
254
255 if PopenObject.returncode != 0:
256 if PopenObject.returncode != 0:
257 EdkLogger.debug(EdkLogger.DEBUG_1, "Fail to call BPDG tool", str(error))
258 EdkLogger.error("BPDG", BuildToolError.COMMAND_FAILURE, "Fail to execute BPDG tool with exit code: %d, the error message is: \n %s" % \
259 (PopenObject.returncode, str(error)))
260
261 return PopenObject.returncode