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