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