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