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