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