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