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