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