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