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