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