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