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