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