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