BaseTools: Enhance BaseTools supports FixedAtBuild usage in VFR file
[mirror_edk2.git] / BaseTools / Source / Python / BPDG / GenVpd.py
CommitLineData
111be80f 1## @file\r
2# This file include GenVpd class for fix the Vpd type PCD offset, and PcdEntry for describe\r
3# and process each entry of vpd type PCD.\r
4#\r
53c13295 5# Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.<BR>\r
111be80f 6#\r
7# This program and the accompanying materials\r
8# are licensed and made available under the terms and conditions of the BSD License\r
9# which accompanies this distribution. The full text of the license may be found at\r
10# http://opensource.org/licenses/bsd-license.php\r
11#\r
12# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14#\r
15\r
1be2ed90 16import Common.LongFilePathOs as os\r
111be80f 17import StringIO\r
18import StringTable as st\r
19import array\r
6780eef1 20import re\r
1be2ed90 21from Common.LongFilePathSupport import OpenLongFilePath as open\r
111be80f 22from struct import *\r
23import Common.EdkLogger as EdkLogger\r
24import Common.BuildToolError as BuildToolError\r
25\r
26_FORMAT_CHAR = {1: 'B',\r
27 2: 'H',\r
28 4: 'I',\r
29 8: 'Q'\r
30 }\r
31\r
08dd311f
LG
32## The VPD PCD data structure for store and process each VPD PCD entry.\r
33#\r
34# This class contain method to format and pack pcd's value. \r
35#\r
111be80f 36class PcdEntry:\r
e8a47801 37 def __init__(self, PcdCName, SkuId,PcdOffset, PcdSize, PcdValue, Lineno=None, FileName=None, PcdUnpackValue=None, \r
111be80f 38 PcdBinOffset=None, PcdBinSize=None):\r
39 self.PcdCName = PcdCName.strip()\r
e8a47801 40 self.SkuId = SkuId.strip()\r
111be80f 41 self.PcdOffset = PcdOffset.strip()\r
42 self.PcdSize = PcdSize.strip()\r
43 self.PcdValue = PcdValue.strip()\r
44 self.Lineno = Lineno.strip()\r
45 self.FileName = FileName.strip()\r
46 self.PcdUnpackValue = PcdUnpackValue\r
47 self.PcdBinOffset = PcdBinOffset\r
48 self.PcdBinSize = PcdBinSize\r
49 \r
50 if self.PcdValue == '' :\r
47fea6af
YZ
51 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r
52 "Invalid PCD format(Name: %s File: %s line: %s) , no Value specified!" % (self.PcdCName, self.FileName, self.Lineno))\r
53\r
111be80f 54 if self.PcdOffset == '' :\r
47fea6af
YZ
55 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r
56 "Invalid PCD format(Name: %s File: %s Line: %s) , no Offset specified!" % (self.PcdCName, self.FileName, self.Lineno))\r
57\r
111be80f 58 if self.PcdSize == '' :\r
47fea6af
YZ
59 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r
60 "Invalid PCD format(Name: %s File: %s Line: %s), no PcdSize specified!" % (self.PcdCName, self.FileName, self.Lineno))\r
61\r
111be80f 62 self._GenOffsetValue ()\r
47fea6af 63\r
08dd311f
LG
64 ## Analyze the string value to judge the PCD's datum type euqal to Boolean or not.\r
65 # \r
66 # @param ValueString PCD's value\r
67 # @param Size PCD's size\r
68 # \r
69 # @retval True PCD's datum type is Boolean\r
70 # @retval False PCD's datum type is not Boolean. \r
71 # \r
72 def _IsBoolean(self, ValueString, Size):\r
73 if (Size == "1"):\r
74 if ValueString.upper() in ["TRUE", "FALSE"]:\r
75 return True\r
76 elif ValueString in ["0", "1", "0x0", "0x1", "0x00", "0x01"]:\r
47fea6af
YZ
77 return True\r
78\r
111be80f 79 return False\r
47fea6af 80\r
08dd311f
LG
81 ## Convert the PCD's value from string to integer.\r
82 # \r
83 # This function will try to convert the Offset value form string to integer\r
84 # for both hexadecimal and decimal.\r
85 # \r
111be80f 86 def _GenOffsetValue(self):\r
87 if self.PcdOffset != "*" :\r
88 try:\r
89 self.PcdBinOffset = int (self.PcdOffset)\r
90 except:\r
91 try:\r
92 self.PcdBinOffset = int(self.PcdOffset, 16)\r
93 except:\r
47fea6af
YZ
94 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r
95 "Invalid offset value %s for PCD %s (File: %s Line: %s)" % (self.PcdOffset, self.PcdCName, self.FileName, self.Lineno))\r
96\r
08dd311f
LG
97 ## Pack Boolean type VPD PCD's value form string to binary type.\r
98 # \r
99 # @param ValueString The boolean type string for pack.\r
100 # \r
101 # \r
111be80f 102 def _PackBooleanValue(self, ValueString):\r
08dd311f 103 if ValueString.upper() == "TRUE" or ValueString in ["1", "0x1", "0x01"]:\r
47fea6af
YZ
104 try:\r
105 self.PcdValue = pack(_FORMAT_CHAR[1], 1)\r
111be80f 106 except:\r
47fea6af
YZ
107 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r
108 "Invalid size or value for PCD %s to pack(File: %s Line: %s)." % (self.PcdCName, self.FileName, self.Lineno))\r
111be80f 109 else:\r
110 try:\r
47fea6af 111 self.PcdValue = pack(_FORMAT_CHAR[1], 0)\r
111be80f 112 except:\r
47fea6af
YZ
113 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r
114 "Invalid size or value for PCD %s to pack(File: %s Line: %s)." % (self.PcdCName, self.FileName, self.Lineno))\r
115\r
08dd311f
LG
116 ## Pack Integer type VPD PCD's value form string to binary type.\r
117 # \r
118 # @param ValueString The Integer type string for pack.\r
119 # \r
120 # \r
111be80f 121 def _PackIntValue(self, IntValue, Size):\r
122 if Size not in _FORMAT_CHAR.keys():\r
47fea6af
YZ
123 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r
124 "Invalid size %d for PCD %s in integer datum size(File: %s Line: %s)." % (Size, self.PcdCName, self.FileName, self.Lineno))\r
125\r
126 if Size == 1:\r
08dd311f 127 if IntValue < 0:\r
47fea6af 128 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r
08dd311f
LG
129 "PCD can't be set to negative value %d for PCD %s in UINT8 datum type(File: %s Line: %s)." % (IntValue, self.PcdCName, self.FileName, self.Lineno))\r
130 elif IntValue >= 0x100:\r
47fea6af
YZ
131 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r
132 "Too large PCD value %d for datum type UINT8 for PCD %s(File: %s Line: %s)." % (IntValue, self.PcdCName, self.FileName, self.Lineno))\r
08dd311f
LG
133 elif Size == 2:\r
134 if IntValue < 0:\r
47fea6af 135 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r
08dd311f
LG
136 "PCD can't be set to negative value %d for PCD %s in UINT16 datum type(File: %s Line: %s)." % (IntValue, self.PcdCName, self.FileName, self.Lineno))\r
137 elif IntValue >= 0x10000:\r
47fea6af
YZ
138 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r
139 "Too large PCD value %d for datum type UINT16 for PCD %s(File: %s Line: %s)." % (IntValue, self.PcdCName, self.FileName, self.Lineno))\r
08dd311f
LG
140 elif Size == 4:\r
141 if IntValue < 0:\r
47fea6af 142 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r
08dd311f
LG
143 "PCD can't be set to negative value %d for PCD %s in UINT32 datum type(File: %s Line: %s)." % (IntValue, self.PcdCName, self.FileName, self.Lineno))\r
144 elif IntValue >= 0x100000000:\r
47fea6af 145 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r
08dd311f
LG
146 "Too large PCD value %d for datum type UINT32 for PCD %s(File: %s Line: %s)." % (IntValue, self.PcdCName, self.FileName, self.Lineno))\r
147 elif Size == 8:\r
148 if IntValue < 0:\r
47fea6af 149 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r
08dd311f
LG
150 "PCD can't be set to negative value %d for PCD %s in UINT32 datum type(File: %s Line: %s)." % (IntValue, self.PcdCName, self.FileName, self.Lineno))\r
151 elif IntValue >= 0x10000000000000000:\r
47fea6af 152 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r
08dd311f
LG
153 "Too large PCD value %d for datum type UINT32 for PCD %s(File: %s Line: %s)." % (IntValue, self.PcdCName, self.FileName, self.Lineno))\r
154 else:\r
47fea6af
YZ
155 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r
156 "Invalid size %d for PCD %s in integer datum size(File: %s Line: %s)." % (Size, self.PcdCName, self.FileName, self.Lineno))\r
157\r
111be80f 158 try:\r
47fea6af 159 self.PcdValue = pack(_FORMAT_CHAR[Size], IntValue)\r
111be80f 160 except:\r
47fea6af
YZ
161 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r
162 "Invalid size or value for PCD %s to pack(File: %s Line: %s)." % (self.PcdCName, self.FileName, self.Lineno))\r
08dd311f
LG
163\r
164 ## Pack VOID* type VPD PCD's value form string to binary type.\r
165 #\r
166 # The VOID* type of string divided into 3 sub-type:\r
167 # 1: L"String", Unicode type string.\r
168 # 2: "String", Ascii type string.\r
169 # 3: {bytearray}, only support byte-array.\r
170 #\r
171 # @param ValueString The Integer type string for pack.\r
172 # \r
111be80f 173 def _PackPtrValue(self, ValueString, Size):\r
174 if ValueString.startswith('L"'):\r
175 self._PackUnicode(ValueString, Size)\r
176 elif ValueString.startswith('{') and ValueString.endswith('}'):\r
177 self._PackByteArray(ValueString, Size)\r
178 elif ValueString.startswith('"') and ValueString.endswith('"'):\r
179 self._PackString(ValueString, Size)\r
180 else:\r
47fea6af
YZ
181 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r
182 "Invalid VOID* type PCD %s value %s (File: %s Line: %s)" % (self.PcdCName, ValueString, self.FileName, self.Lineno))\r
183\r
08dd311f
LG
184 ## Pack an Ascii PCD value.\r
185 # \r
186 # An Ascii string for a PCD should be in format as "".\r
187 # \r
111be80f 188 def _PackString(self, ValueString, Size):\r
189 if (Size < 0):\r
47fea6af 190 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r
111be80f 191 "Invalid parameter Size %s of PCD %s!(File: %s Line: %s)" % (self.PcdBinSize, self.PcdCName, self.FileName, self.Lineno))\r
192 if (ValueString == ""):\r
47fea6af 193 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID, "Invalid parameter ValueString %s of PCD %s!(File: %s Line: %s)" % (self.PcdUnpackValue, self.PcdCName, self.FileName, self.Lineno))\r
111be80f 194 if (len(ValueString) < 2):\r
195 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID, "For PCD: %s ,ASCII string %s at least contains two!(File: %s Line: %s)" % (self.PcdCName, self.PcdUnpackValue, self.FileName, self.Lineno))\r
47fea6af 196\r
111be80f 197 ValueString = ValueString[1:-1]\r
198 if len(ValueString) + 1 > Size:\r
47fea6af 199 EdkLogger.error("BPDG", BuildToolError.RESOURCE_OVERFLOW,\r
111be80f 200 "PCD value string %s is exceed to size %d(File: %s Line: %s)" % (ValueString, Size, self.FileName, self.Lineno))\r
201 try:\r
47fea6af 202 self.PcdValue = pack('%ds' % Size, ValueString)\r
111be80f 203 except:\r
47fea6af
YZ
204 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r
205 "Invalid size or value for PCD %s to pack(File: %s Line: %s)." % (self.PcdCName, self.FileName, self.Lineno))\r
206\r
08dd311f
LG
207 ## Pack a byte-array PCD value.\r
208 # \r
209 # A byte-array for a PCD should be in format as {0x01, 0x02, ...}.\r
210 # \r
111be80f 211 def _PackByteArray(self, ValueString, Size):\r
47fea6af 212 if (Size < 0):\r
111be80f 213 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID, "Invalid parameter Size %s of PCD %s!(File: %s Line: %s)" % (self.PcdBinSize, self.PcdCName, self.FileName, self.Lineno))\r
214 if (ValueString == ""):\r
47fea6af
YZ
215 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID, "Invalid parameter ValueString %s of PCD %s!(File: %s Line: %s)" % (self.PcdUnpackValue, self.PcdCName, self.FileName, self.Lineno))\r
216\r
111be80f 217 ValueString = ValueString.strip()\r
218 ValueString = ValueString.lstrip('{').strip('}')\r
219 ValueList = ValueString.split(',')\r
220 ValueList = [item.strip() for item in ValueList]\r
47fea6af 221\r
111be80f 222 if len(ValueList) > Size:\r
47fea6af 223 EdkLogger.error("BPDG", BuildToolError.RESOURCE_OVERFLOW,\r
111be80f 224 "The byte array %s is too large for size %d(File: %s Line: %s)" % (ValueString, Size, self.FileName, self.Lineno))\r
47fea6af 225\r
111be80f 226 ReturnArray = array.array('B')\r
47fea6af 227\r
111be80f 228 for Index in xrange(len(ValueList)):\r
229 Value = None\r
25918452 230 if ValueList[Index].lower().startswith('0x'):\r
111be80f 231 # translate hex value\r
232 try:\r
233 Value = int(ValueList[Index], 16)\r
234 except:\r
47fea6af 235 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r
111be80f 236 "The value item %s in byte array %s is an invalid HEX value.(File: %s Line: %s)" % \\r
237 (ValueList[Index], ValueString, self.FileName, self.Lineno))\r
238 else:\r
239 # translate decimal value\r
240 try:\r
241 Value = int(ValueList[Index], 10)\r
242 except:\r
243 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r
244 "The value item %s in byte array %s is an invalid DECIMAL value.(File: %s Line: %s)" % \\r
245 (ValueList[Index], ValueString, self.FileName, self.Lineno))\r
47fea6af 246\r
111be80f 247 if Value > 255:\r
47fea6af
YZ
248 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r
249 "The value item %s in byte array %s do not in range 0 ~ 0xFF(File: %s Line: %s)" % \\r
111be80f 250 (ValueList[Index], ValueString, self.FileName, self.Lineno))\r
47fea6af 251\r
111be80f 252 ReturnArray.append(Value)\r
47fea6af 253\r
111be80f 254 for Index in xrange(len(ValueList), Size):\r
255 ReturnArray.append(0)\r
47fea6af
YZ
256\r
257 self.PcdValue = ReturnArray.tolist()\r
111be80f 258\r
259 ## Pack a unicode PCD value into byte array.\r
260 # \r
261 # A unicode string for a PCD should be in format as L"".\r
262 #\r
263 def _PackUnicode(self, UnicodeString, Size):\r
47fea6af
YZ
264 if (Size < 0):\r
265 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID, "Invalid parameter Size %s of PCD %s!(File: %s Line: %s)" % \\r
111be80f 266 (self.PcdBinSize, self.PcdCName, self.FileName, self.Lineno))\r
267 if (len(UnicodeString) < 3):\r
47fea6af 268 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID, "For PCD: %s ,ASCII string %s at least contains two!(File: %s Line: %s)" % \\r
111be80f 269 (self.PcdCName, self.PcdUnpackValue, self.FileName, self.Lineno))\r
47fea6af 270\r
111be80f 271 UnicodeString = UnicodeString[2:-1]\r
47fea6af 272\r
111be80f 273 if (len(UnicodeString) + 1) * 2 > Size:\r
274 EdkLogger.error("BPDG", BuildToolError.RESOURCE_OVERFLOW,\r
275 "The size of unicode string %s is too larger for size %s(File: %s Line: %s)" % \\r
276 (UnicodeString, Size, self.FileName, self.Lineno))\r
47fea6af 277\r
111be80f 278 ReturnArray = array.array('B')\r
279 for Value in UnicodeString:\r
280 try:\r
281 ReturnArray.append(ord(Value))\r
282 ReturnArray.append(0)\r
283 except:\r
47fea6af 284 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r
111be80f 285 "Invalid unicode character %s in unicode string %s(File: %s Line: %s)" % \\r
286 (Value, UnicodeString, self.FileName, self.Lineno))\r
47fea6af 287\r
e8a47801 288 for Index in xrange(len(UnicodeString) * 2, Size):\r
111be80f 289 ReturnArray.append(0)\r
47fea6af
YZ
290\r
291 self.PcdValue = ReturnArray.tolist()\r
08dd311f
LG
292\r
293\r
294\r
295## The class implementing the BPDG VPD PCD offset fix process\r
296#\r
297# The VPD PCD offset fix process includes:\r
298# 1. Parse the input guided.txt file and store it in the data structure;\r
299# 2. Format the input file data to remove unused lines;\r
300# 3. Fixed offset if needed;\r
301# 4. Generate output file, including guided.map and guided.bin file;\r
302# \r
47fea6af 303class GenVPD :\r
111be80f 304 ## Constructor of DscBuildData\r
305 #\r
306 # Initialize object of GenVPD\r
307 # @Param InputFileName The filename include the vpd type pcd information\r
308 # @param MapFileName The filename of map file that stores vpd type pcd information.\r
309 # This file will be generated by the BPDG tool after fix the offset\r
310 # and adjust the offset to make the pcd data aligned.\r
311 # @param VpdFileName The filename of Vpd file that hold vpd pcd information.\r
312 #\r
313 def __init__(self, InputFileName, MapFileName, VpdFileName):\r
314 self.InputFileName = InputFileName\r
315 self.MapFileName = MapFileName\r
316 self.VpdFileName = VpdFileName\r
317 self.FileLinesList = []\r
318 self.PcdFixedOffsetSizeList = []\r
319 self.PcdUnknownOffsetList = []\r
320 try:\r
321 fInputfile = open(InputFileName, "r", 0)\r
322 try:\r
323 self.FileLinesList = fInputfile.readlines()\r
324 except:\r
47fea6af 325 EdkLogger.error("BPDG", BuildToolError.FILE_READ_FAILURE, "File read failed for %s" % InputFileName, None)\r
111be80f 326 finally:\r
327 fInputfile.close()\r
328 except:\r
47fea6af
YZ
329 EdkLogger.error("BPDG", BuildToolError.FILE_OPEN_FAILURE, "File open failed for %s" % InputFileName, None)\r
330\r
111be80f 331 ##\r
332 # Parser the input file which is generated by the build tool. Convert the value of each pcd's \r
333 # from string to it's real format. Also remove the useless line in the input file.\r
334 # \r
335 def ParserInputFile (self):\r
47fea6af 336 count = 0\r
111be80f 337 for line in self.FileLinesList:\r
338 # Strip "\r\n" generated by readlines ().\r
339 line = line.strip()\r
340 line = line.rstrip(os.linesep)\r
47fea6af 341\r
111be80f 342 # Skip the comment line\r
6780eef1
LG
343 if (not line.startswith("#")) and len(line) > 1 :\r
344 #\r
345 # Enhanced for support "|" character in the string.\r
346 #\r
22061fab 347 ValueList = ['', '', '', '','']\r
47fea6af
YZ
348\r
349 ValueRe = re.compile(r'\s*L?\".*\|.*\"\s*$')\r
6780eef1 350 PtrValue = ValueRe.findall(line)\r
47fea6af 351\r
6780eef1 352 ValueUpdateFlag = False\r
47fea6af 353\r
6780eef1
LG
354 if len(PtrValue) >= 1:\r
355 line = re.sub(ValueRe, '', line)\r
47fea6af
YZ
356 ValueUpdateFlag = True\r
357\r
6780eef1
LG
358 TokenList = line.split('|')\r
359 ValueList[0:len(TokenList)] = TokenList\r
47fea6af 360\r
6780eef1 361 if ValueUpdateFlag:\r
22061fab 362 ValueList[4] = PtrValue[0]\r
6780eef1 363 self.FileLinesList[count] = ValueList\r
111be80f 364 # Store the line number\r
47fea6af 365 self.FileLinesList[count].append(str(count + 1))\r
111be80f 366 elif len(line) <= 1 :\r
367 # Set the blank line to "None"\r
368 self.FileLinesList[count] = None\r
369 else :\r
370 # Set the comment line to "None"\r
371 self.FileLinesList[count] = None\r
372 count += 1\r
47fea6af 373\r
111be80f 374 # The line count contain usage information\r
47fea6af 375 count = 0\r
111be80f 376 # Delete useless lines\r
377 while (True) :\r
378 try :\r
379 if (self.FileLinesList[count] == None) :\r
380 del(self.FileLinesList[count])\r
381 else :\r
382 count += 1\r
383 except :\r
47fea6af 384 break\r
111be80f 385 #\r
386 # After remove the useless line, if there are no data remain in the file line list,\r
387 # Report warning messages to user's.\r
388 # \r
389 if len(self.FileLinesList) == 0 :\r
47fea6af 390 EdkLogger.warn('BPDG', BuildToolError.RESOURCE_NOT_AVAILABLE,\r
111be80f 391 "There are no VPD type pcds defined in DSC file, Please check it.")\r
47fea6af 392\r
111be80f 393 # Process the pcds one by one base on the pcd's value and size\r
394 count = 0\r
47fea6af 395 for line in self.FileLinesList:\r
111be80f 396 if line != None :\r
e8a47801 397 PCD = PcdEntry(line[0], line[1], line[2], line[3], line[4],line[5], self.InputFileName) \r
111be80f 398 # Strip the space char\r
399 PCD.PcdCName = PCD.PcdCName.strip(' ')\r
e8a47801 400 PCD.SkuId = PCD.SkuId.strip(' ')\r
111be80f 401 PCD.PcdOffset = PCD.PcdOffset.strip(' ')\r
402 PCD.PcdSize = PCD.PcdSize.strip(' ')\r
22061fab 403 PCD.PcdValue = PCD.PcdValue.strip(' ')\r
111be80f 404 PCD.Lineno = PCD.Lineno.strip(' ')\r
22061fab 405\r
111be80f 406 #\r
407 # Store the original pcd value.\r
408 # This information will be useful while generate the output map file.\r
409 #\r
22061fab
YZ
410 PCD.PcdUnpackValue = str(PCD.PcdValue)\r
411\r
53c13295
YZ
412 #\r
413 # Translate PCD size string to an integer value.\r
414 PackSize = None\r
415 try:\r
416 PackSize = int(PCD.PcdSize, 10)\r
417 PCD.PcdBinSize = PackSize\r
418 except:\r
419 try:\r
420 PackSize = int(PCD.PcdSize, 16)\r
421 PCD.PcdBinSize = PackSize\r
422 except:\r
423 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID, "Invalid PCD size value %s at file: %s line: %s" % (PCD.PcdSize, self.InputFileName, PCD.Lineno))\r
424\r
22061fab
YZ
425 #\r
426 # If value is Unicode string (e.g. L""), then use 2-byte alignment\r
427 # If value is byte array (e.g. {}), then use 8-byte alignment\r
428 #\r
53c13295 429 PCD.PcdOccupySize = PCD.PcdBinSize\r
22061fab
YZ
430 if PCD.PcdUnpackValue.startswith("{"):\r
431 Alignment = 8\r
432 elif PCD.PcdUnpackValue.startswith("L"):\r
433 Alignment = 2\r
434 else:\r
435 Alignment = 1\r
815ada26
YZ
436\r
437 if PCD.PcdOffset != '*':\r
438 if PCD.PcdOccupySize % Alignment != 0:\r
439 if PCD.PcdUnpackValue.startswith("{"):\r
440 EdkLogger.warn("BPDG", "The offset value of PCD %s is not 8-byte aligned!" %(PCD.PcdCName), File=self.InputFileName)\r
441 else:\r
442 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID, 'The offset value of PCD %s should be %s-byte aligned.' % (PCD.PcdCName, Alignment))\r
443 else:\r
444 if PCD.PcdOccupySize % Alignment != 0:\r
445 PCD.PcdOccupySize = (PCD.PcdOccupySize / Alignment + 1) * Alignment\r
111be80f 446\r
08dd311f 447 if PCD._IsBoolean(PCD.PcdValue, PCD.PcdSize):\r
111be80f 448 PCD._PackBooleanValue(PCD.PcdValue)\r
449 self.FileLinesList[count] = PCD\r
450 count += 1\r
451 continue\r
452 #\r
453 # Try to translate value to an integer firstly.\r
454 #\r
455 IsInteger = True\r
47fea6af 456 PackValue = None\r
111be80f 457 try:\r
458 PackValue = int(PCD.PcdValue)\r
459 except:\r
460 try:\r
461 PackValue = int(PCD.PcdValue, 16)\r
462 except:\r
463 IsInteger = False\r
47fea6af 464\r
111be80f 465 if IsInteger:\r
466 PCD._PackIntValue(PackValue, PackSize)\r
467 else:\r
468 PCD._PackPtrValue(PCD.PcdValue, PackSize)\r
47fea6af 469\r
111be80f 470 self.FileLinesList[count] = PCD\r
471 count += 1\r
472 else :\r
473 continue\r
47fea6af 474\r
111be80f 475 ##\r
476 # This function used to create a clean list only contain useful information and reorganized to make it \r
477 # easy to be sorted\r
478 #\r
479 def FormatFileLine (self) :\r
47fea6af 480\r
111be80f 481 for eachPcd in self.FileLinesList :\r
482 if eachPcd.PcdOffset != '*' :\r
483 # Use pcd's Offset value as key, and pcd's Value as value \r
484 self.PcdFixedOffsetSizeList.append(eachPcd)\r
485 else :\r
486 # Use pcd's CName as key, and pcd's Size as value\r
487 self.PcdUnknownOffsetList.append(eachPcd)\r
47fea6af
YZ
488\r
489\r
111be80f 490 ##\r
491 # This function is use to fix the offset value which the not specified in the map file.\r
492 # Usually it use the star (meaning any offset) character in the offset field\r
493 # \r
47fea6af 494 def FixVpdOffset (self):\r
111be80f 495 # At first, the offset should start at 0\r
496 # Sort fixed offset list in order to find out where has free spaces for the pcd's offset\r
497 # value is "*" to insert into. \r
47fea6af
YZ
498\r
499 self.PcdFixedOffsetSizeList.sort(lambda x, y: cmp(x.PcdBinOffset, y.PcdBinOffset))\r
500\r
111be80f 501 #\r
502 # Sort the un-fixed pcd's offset by it's size.\r
503 #\r
47fea6af
YZ
504 self.PcdUnknownOffsetList.sort(lambda x, y: cmp(x.PcdBinSize, y.PcdBinSize))\r
505\r
111be80f 506 #\r
507 # Process all Offset value are "*"\r
508 #\r
509 if (len(self.PcdFixedOffsetSizeList) == 0) and (len(self.PcdUnknownOffsetList) != 0) :\r
510 # The offset start from 0\r
511 NowOffset = 0\r
47fea6af 512 for Pcd in self.PcdUnknownOffsetList :\r
111be80f 513 Pcd.PcdBinOffset = NowOffset\r
514 Pcd.PcdOffset = str(hex(Pcd.PcdBinOffset))\r
22061fab 515 NowOffset += Pcd.PcdOccupySize\r
111be80f 516 \r
517 self.PcdFixedOffsetSizeList = self.PcdUnknownOffsetList\r
518 return\r
47fea6af 519\r
111be80f 520 # Check the offset of VPD type pcd's offset start from 0. \r
47fea6af 521 if self.PcdFixedOffsetSizeList[0].PcdBinOffset != 0 :\r
111be80f 522 EdkLogger.warn("BPDG", "The offset of VPD type pcd should start with 0, please check it.",\r
47fea6af
YZ
523 None)\r
524\r
111be80f 525 # Judge whether the offset in fixed pcd offset list is overlapped or not.\r
526 lenOfList = len(self.PcdFixedOffsetSizeList)\r
527 count = 0 \r
528 while (count < lenOfList - 1) :\r
529 PcdNow = self.PcdFixedOffsetSizeList[count]\r
530 PcdNext = self.PcdFixedOffsetSizeList[count+1]\r
531 # Two pcd's offset is same \r
532 if PcdNow.PcdBinOffset == PcdNext.PcdBinOffset :\r
47fea6af
YZ
533 EdkLogger.error("BPDG", BuildToolError.ATTRIBUTE_GET_FAILURE,\r
534 "The offset of %s at line: %s is same with %s at line: %s in file %s" % \\r
111be80f 535 (PcdNow.PcdCName, PcdNow.Lineno, PcdNext.PcdCName, PcdNext.Lineno, PcdNext.FileName),\r
536 None)\r
47fea6af 537\r
111be80f 538 # Overlapped \r
22061fab 539 if PcdNow.PcdBinOffset + PcdNow.PcdOccupySize > PcdNext.PcdBinOffset :\r
47fea6af
YZ
540 EdkLogger.error("BPDG", BuildToolError.ATTRIBUTE_GET_FAILURE,\r
541 "The offset of %s at line: %s is overlapped with %s at line: %s in file %s" % \\r
111be80f 542 (PcdNow.PcdCName, PcdNow.Lineno, PcdNext.PcdCName, PcdNext.Lineno, PcdNext.FileName),\r
543 None)\r
47fea6af 544\r
111be80f 545 # Has free space, raise a warning message \r
22061fab 546 if PcdNow.PcdBinOffset + PcdNow.PcdOccupySize < PcdNext.PcdBinOffset :\r
47fea6af
YZ
547 EdkLogger.warn("BPDG", BuildToolError.ATTRIBUTE_GET_FAILURE,\r
548 "The offsets have free space of between %s at line: %s and %s at line: %s in file %s" % \\r
111be80f 549 (PcdNow.PcdCName, PcdNow.Lineno, PcdNext.PcdCName, PcdNext.Lineno, PcdNext.FileName),\r
550 None)\r
551 count += 1\r
552 \r
553 LastOffset = self.PcdFixedOffsetSizeList[0].PcdBinOffset\r
554 FixOffsetSizeListCount = 0\r
555 lenOfList = len(self.PcdFixedOffsetSizeList)\r
556 lenOfUnfixedList = len(self.PcdUnknownOffsetList)\r
557 \r
558 ##\r
559 # Insert the un-fixed offset pcd's list into fixed offset pcd's list if has free space between those pcds. \r
560 # \r
561 while (FixOffsetSizeListCount < lenOfList) :\r
562 \r
563 eachFixedPcd = self.PcdFixedOffsetSizeList[FixOffsetSizeListCount] \r
564 NowOffset = eachFixedPcd.PcdBinOffset\r
565 \r
566 # Has free space \r
567 if LastOffset < NowOffset :\r
568 if lenOfUnfixedList != 0 :\r
569 countOfUnfixedList = 0\r
47fea6af 570 while(countOfUnfixedList < lenOfUnfixedList) :\r
111be80f 571 eachUnfixedPcd = self.PcdUnknownOffsetList[countOfUnfixedList]\r
22061fab 572 needFixPcdSize = eachUnfixedPcd.PcdOccupySize\r
111be80f 573 # Not been fixed\r
574 if eachUnfixedPcd.PcdOffset == '*' :\r
575 # The offset un-fixed pcd can write into this free space\r
576 if needFixPcdSize <= (NowOffset - LastOffset) :\r
577 # Change the offset value of un-fixed pcd\r
578 eachUnfixedPcd.PcdOffset = str(hex(LastOffset))\r
579 eachUnfixedPcd.PcdBinOffset = LastOffset\r
580 # Insert this pcd into fixed offset pcd list.\r
581 self.PcdFixedOffsetSizeList.insert(FixOffsetSizeListCount,eachUnfixedPcd)\r
582 \r
583 # Delete the item's offset that has been fixed and added into fixed offset list\r
584 self.PcdUnknownOffsetList.pop(countOfUnfixedList)\r
585 \r
586 # After item added, should enlarge the length of fixed pcd offset list\r
587 lenOfList += 1 \r
588 FixOffsetSizeListCount += 1\r
589 \r
590 # Decrease the un-fixed pcd offset list's length\r
111be80f 591 lenOfUnfixedList -= 1\r
592 \r
593 # Modify the last offset value \r
6780eef1 594 LastOffset += needFixPcdSize \r
111be80f 595 else :\r
6780eef1 596 # It can not insert into those two pcds, need to check still has other space can store it.\r
22061fab 597 LastOffset = NowOffset + self.PcdFixedOffsetSizeList[FixOffsetSizeListCount].PcdOccupySize\r
111be80f 598 FixOffsetSizeListCount += 1\r
6780eef1
LG
599 break\r
600 \r
111be80f 601 # Set the FixOffsetSizeListCount = lenOfList for quit the loop\r
602 else :\r
603 FixOffsetSizeListCount = lenOfList \r
604 \r
605 # No free space, smoothly connect with previous pcd. \r
606 elif LastOffset == NowOffset :\r
22061fab 607 LastOffset = NowOffset + eachFixedPcd.PcdOccupySize\r
111be80f 608 FixOffsetSizeListCount += 1\r
609 # Usually it will not enter into this thunk, if so, means it overlapped. \r
610 else :\r
47fea6af
YZ
611 EdkLogger.error("BPDG", BuildToolError.ATTRIBUTE_NOT_AVAILABLE,\r
612 "The offset value definition has overlapped at pcd: %s, it's offset is: %s, in file: %s line: %s" % \\r
111be80f 613 (eachFixedPcd.PcdCName, eachFixedPcd.PcdOffset, eachFixedPcd.InputFileName, eachFixedPcd.Lineno),\r
614 None)\r
615 FixOffsetSizeListCount += 1\r
616 \r
617 # Continue to process the un-fixed offset pcd's list, add this time, just append them behind the fixed pcd's offset list. \r
618 lenOfUnfixedList = len(self.PcdUnknownOffsetList)\r
619 lenOfList = len(self.PcdFixedOffsetSizeList)\r
620 while (lenOfUnfixedList > 0) :\r
621 # Still has items need to process\r
622 # The last pcd instance\r
623 LastPcd = self.PcdFixedOffsetSizeList[lenOfList-1]\r
624 NeedFixPcd = self.PcdUnknownOffsetList[0]\r
625 \r
22061fab 626 NeedFixPcd.PcdBinOffset = LastPcd.PcdBinOffset + LastPcd.PcdOccupySize\r
111be80f 627 NeedFixPcd.PcdOffset = str(hex(NeedFixPcd.PcdBinOffset))\r
628 \r
629 # Insert this pcd into fixed offset pcd list's tail.\r
630 self.PcdFixedOffsetSizeList.insert(lenOfList, NeedFixPcd)\r
631 # Delete the item's offset that has been fixed and added into fixed offset list\r
632 self.PcdUnknownOffsetList.pop(0)\r
633 \r
634 lenOfList += 1\r
635 lenOfUnfixedList -= 1 \r
636 ##\r
637 # Write the final data into output files.\r
638 # \r
639 def GenerateVpdFile (self, MapFileName, BinFileName):\r
640 #Open an VPD file to process\r
641\r
642 try:\r
47fea6af 643 fVpdFile = open(BinFileName, "wb", 0)\r
111be80f 644 except:\r
645 # Open failed\r
47fea6af
YZ
646 EdkLogger.error("BPDG", BuildToolError.FILE_OPEN_FAILURE, "File open failed for %s" % self.VpdFileName, None)\r
647\r
111be80f 648 try :\r
47fea6af 649 fMapFile = open(MapFileName, "w", 0)\r
111be80f 650 except:\r
651 # Open failed\r
47fea6af
YZ
652 EdkLogger.error("BPDG", BuildToolError.FILE_OPEN_FAILURE, "File open failed for %s" % self.MapFileName, None)\r
653\r
111be80f 654 # Use a instance of StringIO to cache data\r
47fea6af
YZ
655 fStringIO = StringIO.StringIO('')\r
656\r
111be80f 657 # Write the header of map file.\r
658 try :\r
659 fMapFile.write (st.MAP_FILE_COMMENT_TEMPLATE + "\n")\r
660 except:\r
47fea6af
YZ
661 EdkLogger.error("BPDG", BuildToolError.FILE_WRITE_FAILURE, "Write data to file %s failed, please check whether the file been locked or using by other applications." % self.MapFileName, None)\r
662\r
111be80f 663 for eachPcd in self.PcdFixedOffsetSizeList :\r
664 # write map file\r
665 try :\r
e8a47801 666 fMapFile.write("%s | %s | %s | %s | %s \n" % (eachPcd.PcdCName, eachPcd.SkuId,eachPcd.PcdOffset, eachPcd.PcdSize,eachPcd.PcdUnpackValue))\r
111be80f 667 except:\r
47fea6af
YZ
668 EdkLogger.error("BPDG", BuildToolError.FILE_WRITE_FAILURE, "Write data to file %s failed, please check whether the file been locked or using by other applications." % self.MapFileName, None)\r
669\r
111be80f 670 # Write Vpd binary file\r
47fea6af 671 fStringIO.seek (eachPcd.PcdBinOffset)\r
111be80f 672 if isinstance(eachPcd.PcdValue, list):\r
673 ValueList = [chr(Item) for Item in eachPcd.PcdValue]\r
47fea6af
YZ
674 fStringIO.write(''.join(ValueList))\r
675 else:\r
111be80f 676 fStringIO.write (eachPcd.PcdValue)\r
47fea6af
YZ
677\r
678 try :\r
111be80f 679 fVpdFile.write (fStringIO.getvalue())\r
680 except:\r
47fea6af
YZ
681 EdkLogger.error("BPDG", BuildToolError.FILE_WRITE_FAILURE, "Write data to file %s failed, please check whether the file been locked or using by other applications." % self.VpdFileName, None)\r
682\r
111be80f 683 fStringIO.close ()\r
684 fVpdFile.close ()\r
685 fMapFile.close ()\r
686 \r