]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/Common/VpdInfoFile.py
Sync EDKII BaseTools to BaseTools project r2065.
[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
138 for Pcd in self._VpdArray.keys():\r
139 for Offset in self._VpdArray[Pcd]:\r
140 PcdValue = str(Pcd.SkuInfoList[Pcd.SkuInfoList.keys()[0]].DefaultValue).strip()\r
141 if PcdValue == "" :\r
142 PcdValue = Pcd.DefaultValue\r
143 \r
144 fd.write("%s.%s|%s|%s|%s \n" % (Pcd.TokenSpaceGuidCName, Pcd.TokenCName, str(Offset).strip(), str(Pcd.MaxDatumSize).strip(),PcdValue))\r
145 except:\r
146 EdkLogger.error("VpdInfoFile",\r
147 BuildToolError.FILE_WRITE_FAILURE,\r
148 "Fail to write file %s" % FilePath) \r
149 fd.close()\r
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
175 PcdName, Offset, Size, Value = Line.split("#")[0].split("|")\r
176 TokenSpaceName, PcdTokenName = PcdName.split(".")\r
177 except:\r
178 EdkLogger.error("BPDG", BuildToolError.PARSER_ERROR, "Fail to parse VPD information file %s" % FilePath)\r
179 \r
180 Found = False\r
181 for VpdObject in self._VpdArray.keys():\r
182 if VpdObject.TokenSpaceGuidCName == TokenSpaceName and VpdObject.TokenCName == PcdTokenName.strip():\r
183 if self._VpdArray[VpdObject][0] == "*":\r
184 if Offset == "*":\r
185 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID, "The offset of %s has not been fixed up by third-party BPDG tool." % PcdName)\r
186 \r
187 self._VpdArray[VpdObject][0] = Offset\r
188 Found = True\r
189 break\r
190 if not Found:\r
191 EdkLogger.error("BPDG", BuildToolError.PARSER_ERROR, "Can not find PCD defined in VPD guid file.")\r
192 \r
193 ## Get count of VPD PCD collected from platform's autogen when building.\r
194 #\r
195 # @return The integer count value \r
196 def GetCount(self):\r
197 Count = 0\r
198 for OffsetList in self._VpdArray.values():\r
199 Count += len(OffsetList)\r
200 \r
201 return Count\r
202 \r
203 ## Get an offset value for a given VPD PCD\r
204 #\r
205 # Because BPDG only support one Sku, so only return offset for SKU default. \r
206 #\r
207 # @param vpd A given VPD PCD \r
208 def GetOffset(self, vpd):\r
209 if not self._VpdArray.has_key(vpd):\r
210 return None\r
211 \r
212 if len(self._VpdArray[vpd]) == 0:\r
213 return None\r
214 \r
215 return self._VpdArray[vpd]\r
216 \r
217## Call external BPDG tool to process VPD file\r
218# \r
219# @param ToolPath The string path name for BPDG tool\r
220# @param VpdFileName The string path name for VPD information guid.txt\r
221# \r
08dd311f 222def CallExtenalBPDGTool(ToolPath, VpdFileName):\r
e56468c0 223 assert ToolPath != None, "Invalid parameter ToolPath"\r
08dd311f 224 assert VpdFileName != None and os.path.exists(VpdFileName), "Invalid parameter VpdFileName"\r
e56468c0 225 \r
08dd311f
LG
226 OutputDir = os.path.dirname(VpdFileName)\r
227 FileName = os.path.basename(VpdFileName)\r
228 BaseName, ext = os.path.splitext(FileName)\r
229 OutputMapFileName = os.path.join(OutputDir, "%s.map" % BaseName)\r
230 OutputBinFileName = os.path.join(OutputDir, "%s.bin" % BaseName)\r
e56468c0 231 \r
232 try:\r
233 PopenObject = subprocess.Popen([ToolPath,\r
234 '-o', OutputBinFileName, \r
235 '-m', OutputMapFileName,\r
08dd311f 236 '-q',\r
e56468c0 237 '-f',\r
08dd311f 238 VpdFileName],\r
e56468c0 239 stdout=subprocess.PIPE, \r
240 stderr= subprocess.PIPE)\r
241 except Exception, X:\r
242 EdkLogger.error("BPDG", BuildToolError.COMMAND_FAILURE, ExtraData="%s" % (str(X)))\r
243 (out, error) = PopenObject.communicate()\r
244 print out\r
245 while PopenObject.returncode == None :\r
246 PopenObject.wait()\r
247 \r
248 if PopenObject.returncode != 0:\r
249 if PopenObject.returncode != 0:\r
250 EdkLogger.debug(EdkLogger.DEBUG_1, "Fail to call BPDG tool", str(error))\r
251 EdkLogger.error("BPDG", BuildToolError.COMMAND_FAILURE, "Fail to execute BPDG tool with exit code: %d, the error message is: \n %s" % \\r
252 (PopenObject.returncode, str(error)))\r
253 \r
254 return PopenObject.returncode\r