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.
5 # Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
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
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.
18 import StringTable
as st
23 import Common
.EdkLogger
as EdkLogger
24 import Common
.BuildToolError
as BuildToolError
26 _FORMAT_CHAR
= {1: 'B',
32 ## The VPD PCD data structure for store and process each VPD PCD entry.
34 # This class contain method to format and pack pcd's value.
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
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
))
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
))
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
))
61 self
._GenOffsetValue
()
63 ## Analyze the string value to judge the PCD's datum type euqal to Boolean or not.
65 # @param ValueString PCD's value
66 # @param Size PCD's size
68 # @retval True PCD's datum type is Boolean
69 # @retval False PCD's datum type is not Boolean.
71 def _IsBoolean(self
, ValueString
, Size
):
73 if ValueString
.upper() in ["TRUE", "FALSE"]:
75 elif ValueString
in ["0", "1", "0x0", "0x1", "0x00", "0x01"]:
80 ## Convert the PCD's value from string to integer.
82 # This function will try to convert the Offset value form string to integer
83 # for both hexadecimal and decimal.
85 def _GenOffsetValue(self
):
86 if self
.PcdOffset
!= "*" :
88 self
.PcdBinOffset
= int (self
.PcdOffset
)
91 self
.PcdBinOffset
= int(self
.PcdOffset
, 16)
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
))
96 ## Pack Boolean type VPD PCD's value form string to binary type.
98 # @param ValueString The boolean type string for pack.
101 def _PackBooleanValue(self
, ValueString
):
102 if ValueString
.upper() == "TRUE" or ValueString
in ["1", "0x1", "0x01"]:
104 self
.PcdValue
= pack(_FORMAT_CHAR
[1], 1)
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
))
110 self
.PcdValue
= pack(_FORMAT_CHAR
[1], 0)
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
))
115 ## Pack Integer type VPD PCD's value form string to binary type.
117 # @param ValueString The Integer type string for pack.
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
))
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
))
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
))
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
))
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
))
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
))
158 self
.PcdValue
= pack(_FORMAT_CHAR
[Size
], IntValue
)
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
))
163 ## Pack VOID* type VPD PCD's value form string to binary type.
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.
170 # @param ValueString The Integer type string for pack.
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
)
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
))
183 ## Pack an Ascii PCD value.
185 # An Ascii string for a PCD should be in format as "".
187 def _PackString(self
, ValueString
, Size
):
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
))
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
))
201 self
.PcdValue
= pack('%ds' % Size
, ValueString
)
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
))
206 ## Pack a byte-array PCD value.
208 # A byte-array for a PCD should be in format as {0x01, 0x02, ...}.
210 def _PackByteArray(self
, ValueString
, Size
):
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
))
216 ValueString
= ValueString
.strip()
217 ValueString
= ValueString
.lstrip('{').strip('}')
218 ValueList
= ValueString
.split(',')
219 ValueList
= [item
.strip() for item
in ValueList
]
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
))
225 ReturnArray
= array
.array('B')
227 for Index
in xrange(len(ValueList
)):
229 if ValueList
[Index
].startswith('0x'):
230 # translate hex value
232 Value
= int(ValueList
[Index
], 16)
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
))
238 # translate decimal value
240 Value
= int(ValueList
[Index
], 10)
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
))
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
))
251 ReturnArray
.append(Value
)
253 for Index
in xrange(len(ValueList
), Size
):
254 ReturnArray
.append(0)
256 self
.PcdValue
= ReturnArray
.tolist()
258 ## Pack a unicode PCD value into byte array.
260 # A unicode string for a PCD should be in format as L"".
262 def _PackUnicode(self
, UnicodeString
, Size
):
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
))
270 UnicodeString
= UnicodeString
[2:-1]
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
))
277 ReturnArray
= array
.array('B')
278 for Value
in UnicodeString
:
280 ReturnArray
.append(ord(Value
))
281 ReturnArray
.append(0)
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
))
287 for Index
in range(len(UnicodeString
) * 2, Size
):
288 ReturnArray
.append(0)
290 self
.PcdValue
= ReturnArray
.tolist()
294 ## The class implementing the BPDG VPD PCD offset fix process
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;
303 ## Constructor of DscBuildData
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.
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
= []
320 fInputfile
= open(InputFileName
, "r", 0)
322 self
.FileLinesList
= fInputfile
.readlines()
324 EdkLogger
.error("BPDG", BuildToolError
.FILE_READ_FAILURE
, "File read failed for %s" %InputFileName
,None)
328 EdkLogger
.error("BPDG", BuildToolError
.FILE_OPEN_FAILURE
, "File open failed for %s" %InputFileName
,None)
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.
334 def ParserInputFile (self
):
336 for line
in self
.FileLinesList
:
337 # Strip "\r\n" generated by readlines ().
339 line
= line
.rstrip(os
.linesep
)
341 # Skip the comment line
342 if (not line
.startswith("#")) and len(line
) > 1 :
344 # Enhanced for support "|" character in the string.
346 ValueList
= ['', '', '', '']
348 ValueRe
= re
.compile(r
'\s*L?\".*\|.*\"\s*$')
349 PtrValue
= ValueRe
.findall(line
)
351 ValueUpdateFlag
= False
353 if len(PtrValue
) >= 1:
354 line
= re
.sub(ValueRe
, '', line
)
355 ValueUpdateFlag
= True
357 TokenList
= line
.split('|')
358 ValueList
[0:len(TokenList
)] = TokenList
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
369 # Set the comment line to "None"
370 self
.FileLinesList
[count
] = None
373 # The line count contain usage information
375 # Delete useless lines
378 if (self
.FileLinesList
[count
] == None) :
379 del(self
.FileLinesList
[count
])
385 # After remove the useless line, if there are no data remain in the file line list,
386 # Report warning messages to user's.
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.")
392 # Process the pcds one by one base on the pcd's value and size
394 for line
in self
.FileLinesList
:
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(' ')
405 # Store the original pcd value.
406 # This information will be useful while generate the output map file.
408 PCD
.PcdUnpackValue
= str(PCD
.PcdValue
)
411 # Translate PCD size string to an integer value.
414 PackSize
= int(PCD
.PcdSize
, 10)
415 PCD
.PcdBinSize
= PackSize
418 PackSize
= int(PCD
.PcdSize
, 16)
419 PCD
.PcdBinSize
= PackSize
421 EdkLogger
.error("BPDG", BuildToolError
.FORMAT_INVALID
, "Invalid PCD size value %s at file: %s line: %s" % (PCD
.PcdSize
, self
.InputFileName
, PCD
.Lineno
))
423 if PCD
._IsBoolean
(PCD
.PcdValue
, PCD
.PcdSize
):
424 PCD
._PackBooleanValue
(PCD
.PcdValue
)
425 self
.FileLinesList
[count
] = PCD
429 # Try to translate value to an integer firstly.
434 PackValue
= int(PCD
.PcdValue
)
437 PackValue
= int(PCD
.PcdValue
, 16)
442 PCD
._PackIntValue
(PackValue
, PackSize
)
444 PCD
._PackPtrValue
(PCD
.PcdValue
, PackSize
)
446 self
.FileLinesList
[count
] = PCD
452 # This function used to create a clean list only contain useful information and reorganized to make it
455 def FormatFileLine (self
) :
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
)
462 # Use pcd's CName as key, and pcd's Size as value
463 self
.PcdUnknownOffsetList
.append(eachPcd
)
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
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.
475 self
.PcdFixedOffsetSizeList
.sort(lambda x
,y
: cmp(x
.PcdBinOffset
, y
.PcdBinOffset
))
478 # Sort the un-fixed pcd's offset by it's size.
480 self
.PcdUnknownOffsetList
.sort(lambda x
,y
: cmp(x
.PcdBinSize
, y
.PcdBinSize
))
483 # Process all Offset value are "*"
485 if (len(self
.PcdFixedOffsetSizeList
) == 0) and (len(self
.PcdUnknownOffsetList
) != 0) :
486 # The offset start from 0
488 for Pcd
in self
.PcdUnknownOffsetList
:
489 Pcd
.PcdBinOffset
= NowOffset
490 Pcd
.PcdOffset
= str(hex(Pcd
.PcdBinOffset
))
491 NowOffset
+= Pcd
.PcdBinSize
493 self
.PcdFixedOffsetSizeList
= self
.PcdUnknownOffsetList
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.",
501 # Judge whether the offset in fixed pcd offset list is overlapped or not.
502 lenOfList
= len(self
.PcdFixedOffsetSizeList
)
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
),
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
),
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
),
529 LastOffset
= self
.PcdFixedOffsetSizeList
[0].PcdBinOffset
530 FixOffsetSizeListCount
= 0
531 lenOfList
= len(self
.PcdFixedOffsetSizeList
)
532 lenOfUnfixedList
= len(self
.PcdUnknownOffsetList
)
535 # Insert the un-fixed offset pcd's list into fixed offset pcd's list if has free space between those pcds.
537 while (FixOffsetSizeListCount
< lenOfList
) :
539 eachFixedPcd
= self
.PcdFixedOffsetSizeList
[FixOffsetSizeListCount
]
540 NowOffset
= eachFixedPcd
.PcdBinOffset
543 if LastOffset
< NowOffset
:
544 if lenOfUnfixedList
!= 0 :
545 countOfUnfixedList
= 0
546 while(countOfUnfixedList
< lenOfUnfixedList
) :
547 eachUnfixedPcd
= self
.PcdUnknownOffsetList
[countOfUnfixedList
]
548 needFixPcdSize
= eachUnfixedPcd
.PcdBinSize
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
)
559 # Delete the item's offset that has been fixed and added into fixed offset list
560 self
.PcdUnknownOffsetList
.pop(countOfUnfixedList
)
562 # After item added, should enlarge the length of fixed pcd offset list
564 FixOffsetSizeListCount
+= 1
566 # Decrease the un-fixed pcd offset list's length
567 lenOfUnfixedList
-= 1
569 # Modify the last offset value
570 LastOffset
+= needFixPcdSize
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
577 # Set the FixOffsetSizeListCount = lenOfList for quit the loop
579 FixOffsetSizeListCount
= lenOfList
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.
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
),
591 FixOffsetSizeListCount
+= 1
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]
602 NeedFixPcd
.PcdBinOffset
= LastPcd
.PcdBinOffset
+ LastPcd
.PcdBinSize
603 NeedFixPcd
.PcdOffset
= str(hex(NeedFixPcd
.PcdBinOffset
))
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)
611 lenOfUnfixedList
-= 1
613 # Write the final data into output files.
615 def GenerateVpdFile (self
, MapFileName
, BinFileName
):
616 #Open an VPD file to process
619 fVpdFile
= open (BinFileName
, "wb", 0)
622 EdkLogger
.error("BPDG", BuildToolError
.FILE_OPEN_FAILURE
, "File open failed for %s" %self
.VpdFileName
,None)
625 fMapFile
= open (MapFileName
, "w", 0)
628 EdkLogger
.error("BPDG", BuildToolError
.FILE_OPEN_FAILURE
, "File open failed for %s" %self
.MapFileName
,None)
630 # Use a instance of StringIO to cache data
631 fStringIO
= StringIO
.StringIO('')
633 # Write the header of map file.
635 fMapFile
.write (st
.MAP_FILE_COMMENT_TEMPLATE
+ "\n")
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)
639 for eachPcd
in self
.PcdFixedOffsetSizeList
:
642 fMapFile
.write("%s | %s | %s | %s \n" % (eachPcd
.PcdCName
, eachPcd
.PcdOffset
, eachPcd
.PcdSize
,eachPcd
.PcdUnpackValue
))
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)
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
))
652 fStringIO
.write (eachPcd
.PcdValue
)
655 fVpdFile
.write (fStringIO
.getvalue())
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)