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
22 import Common
.EdkLogger
as EdkLogger
23 import Common
.BuildToolError
as BuildToolError
25 _FORMAT_CHAR
= {1: 'B',
31 ## The VPD PCD data structure for store and process each VPD PCD entry.
33 # This class contain method to format and pack pcd's value.
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
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
))
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
))
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
))
60 self
._GenOffsetValue
()
62 ## Analyze the string value to judge the PCD's datum type euqal to Boolean or not.
64 # @param ValueString PCD's value
65 # @param Size PCD's size
67 # @retval True PCD's datum type is Boolean
68 # @retval False PCD's datum type is not Boolean.
70 def _IsBoolean(self
, ValueString
, Size
):
72 if ValueString
.upper() in ["TRUE", "FALSE"]:
74 elif ValueString
in ["0", "1", "0x0", "0x1", "0x00", "0x01"]:
79 ## Convert the PCD's value from string to integer.
81 # This function will try to convert the Offset value form string to integer
82 # for both hexadecimal and decimal.
84 def _GenOffsetValue(self
):
85 if self
.PcdOffset
!= "*" :
87 self
.PcdBinOffset
= int (self
.PcdOffset
)
90 self
.PcdBinOffset
= int(self
.PcdOffset
, 16)
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
))
95 ## Pack Boolean type VPD PCD's value form string to binary type.
97 # @param ValueString The boolean type string for pack.
100 def _PackBooleanValue(self
, ValueString
):
101 if ValueString
.upper() == "TRUE" or ValueString
in ["1", "0x1", "0x01"]:
103 self
.PcdValue
= pack(_FORMAT_CHAR
[1], 1)
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
))
109 self
.PcdValue
= pack(_FORMAT_CHAR
[1], 0)
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
))
114 ## Pack Integer type VPD PCD's value form string to binary type.
116 # @param ValueString The Integer type string for pack.
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
))
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
))
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
))
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
))
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
))
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
))
157 self
.PcdValue
= pack(_FORMAT_CHAR
[Size
], IntValue
)
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
))
162 ## Pack VOID* type VPD PCD's value form string to binary type.
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.
169 # @param ValueString The Integer type string for pack.
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
)
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
))
182 ## Pack an Ascii PCD value.
184 # An Ascii string for a PCD should be in format as "".
186 def _PackString(self
, ValueString
, Size
):
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
))
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
))
200 self
.PcdValue
= pack('%ds' % Size
, ValueString
)
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
))
205 ## Pack a byte-array PCD value.
207 # A byte-array for a PCD should be in format as {0x01, 0x02, ...}.
209 def _PackByteArray(self
, ValueString
, Size
):
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
))
215 ValueString
= ValueString
.strip()
216 ValueString
= ValueString
.lstrip('{').strip('}')
217 ValueList
= ValueString
.split(',')
218 ValueList
= [item
.strip() for item
in ValueList
]
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
))
224 ReturnArray
= array
.array('B')
226 for Index
in xrange(len(ValueList
)):
228 if ValueList
[Index
].startswith('0x'):
229 # translate hex value
231 Value
= int(ValueList
[Index
], 16)
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
))
237 # translate decimal value
239 Value
= int(ValueList
[Index
], 10)
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
))
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
))
250 ReturnArray
.append(Value
)
252 for Index
in xrange(len(ValueList
), Size
):
253 ReturnArray
.append(0)
255 self
.PcdValue
= ReturnArray
.tolist()
257 ## Pack a unicode PCD value into byte array.
259 # A unicode string for a PCD should be in format as L"".
261 def _PackUnicode(self
, UnicodeString
, Size
):
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
))
269 UnicodeString
= UnicodeString
[2:-1]
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
))
276 ReturnArray
= array
.array('B')
277 for Value
in UnicodeString
:
279 ReturnArray
.append(ord(Value
))
280 ReturnArray
.append(0)
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
))
286 for Index
in range(len(UnicodeString
) * 2, Size
):
287 ReturnArray
.append(0)
289 self
.PcdValue
= ReturnArray
.tolist()
293 ## The class implementing the BPDG VPD PCD offset fix process
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;
302 ## Constructor of DscBuildData
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.
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
= []
319 fInputfile
= open(InputFileName
, "r", 0)
321 self
.FileLinesList
= fInputfile
.readlines()
323 EdkLogger
.error("BPDG", BuildToolError
.FILE_READ_FAILURE
, "File read failed for %s" %InputFileName
,None)
327 EdkLogger
.error("BPDG", BuildToolError
.FILE_OPEN_FAILURE
, "File open failed for %s" %InputFileName
,None)
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.
333 def ParserInputFile (self
):
335 for line
in self
.FileLinesList
:
336 # Strip "\r\n" generated by readlines ().
338 line
= line
.rstrip(os
.linesep
)
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
349 # Set the comment line to "None"
350 self
.FileLinesList
[count
] = None
353 # The line count contain usage information
355 # Delete useless lines
358 if (self
.FileLinesList
[count
] == None) :
359 del(self
.FileLinesList
[count
])
365 # After remove the useless line, if there are no data remain in the file line list,
366 # Report warning messages to user's.
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.")
372 # Process the pcds one by one base on the pcd's value and size
374 for line
in self
.FileLinesList
:
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(' ')
385 # Store the original pcd value.
386 # This information will be useful while generate the output map file.
388 PCD
.PcdUnpackValue
= str(PCD
.PcdValue
)
391 # Translate PCD size string to an integer value.
394 PackSize
= int(PCD
.PcdSize
, 10)
395 PCD
.PcdBinSize
= PackSize
398 PackSize
= int(PCD
.PcdSize
, 16)
399 PCD
.PcdBinSize
= PackSize
401 EdkLogger
.error("BPDG", BuildToolError
.FORMAT_INVALID
, "Invalid PCD size value %s at file: %s line: %s" % (PCD
.PcdSize
, self
.InputFileName
, PCD
.Lineno
))
403 if PCD
._IsBoolean
(PCD
.PcdValue
, PCD
.PcdSize
):
404 PCD
._PackBooleanValue
(PCD
.PcdValue
)
405 self
.FileLinesList
[count
] = PCD
409 # Try to translate value to an integer firstly.
414 PackValue
= int(PCD
.PcdValue
)
417 PackValue
= int(PCD
.PcdValue
, 16)
422 PCD
._PackIntValue
(PackValue
, PackSize
)
424 PCD
._PackPtrValue
(PCD
.PcdValue
, PackSize
)
426 self
.FileLinesList
[count
] = PCD
432 # This function used to create a clean list only contain useful information and reorganized to make it
435 def FormatFileLine (self
) :
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
)
442 # Use pcd's CName as key, and pcd's Size as value
443 self
.PcdUnknownOffsetList
.append(eachPcd
)
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
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.
455 self
.PcdFixedOffsetSizeList
.sort(lambda x
,y
: cmp(x
.PcdBinOffset
, y
.PcdBinOffset
))
458 # Sort the un-fixed pcd's offset by it's size.
460 self
.PcdUnknownOffsetList
.sort(lambda x
,y
: cmp(x
.PcdBinSize
, y
.PcdBinSize
))
463 # Process all Offset value are "*"
465 if (len(self
.PcdFixedOffsetSizeList
) == 0) and (len(self
.PcdUnknownOffsetList
) != 0) :
466 # The offset start from 0
468 for Pcd
in self
.PcdUnknownOffsetList
:
469 Pcd
.PcdBinOffset
= NowOffset
470 Pcd
.PcdOffset
= str(hex(Pcd
.PcdBinOffset
))
471 NowOffset
+= Pcd
.PcdBinSize
473 self
.PcdFixedOffsetSizeList
= self
.PcdUnknownOffsetList
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.",
481 # Judge whether the offset in fixed pcd offset list is overlapped or not.
482 lenOfList
= len(self
.PcdFixedOffsetSizeList
)
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
),
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
),
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
),
509 LastOffset
= self
.PcdFixedOffsetSizeList
[0].PcdBinOffset
510 FixOffsetSizeListCount
= 0
511 lenOfList
= len(self
.PcdFixedOffsetSizeList
)
512 lenOfUnfixedList
= len(self
.PcdUnknownOffsetList
)
515 # Insert the un-fixed offset pcd's list into fixed offset pcd's list if has free space between those pcds.
517 while (FixOffsetSizeListCount
< lenOfList
) :
519 eachFixedPcd
= self
.PcdFixedOffsetSizeList
[FixOffsetSizeListCount
]
520 NowOffset
= eachFixedPcd
.PcdBinOffset
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
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
)
541 # Delete the item's offset that has been fixed and added into fixed offset list
542 self
.PcdUnknownOffsetList
.pop(countOfUnfixedList
)
544 # After item added, should enlarge the length of fixed pcd offset list
546 FixOffsetSizeListCount
+= 1
548 # Decrease the un-fixed pcd offset list's length
549 countOfUnfixedList
+= 1
550 lenOfUnfixedList
-= 1
552 # Modify the last offset value
553 LastOffset
+= needFixPcdSize
556 # It can not insert into those two pcds, need to check stiil has other space can store it.
557 FixOffsetSizeListCount
+= 1
561 # Set the FixOffsetSizeListCount = lenOfList for quit the loop
563 FixOffsetSizeListCount
= lenOfList
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.
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
),
575 FixOffsetSizeListCount
+= 1
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]
586 NeedFixPcd
.PcdBinOffset
= LastPcd
.PcdBinOffset
+ LastPcd
.PcdBinSize
587 NeedFixPcd
.PcdOffset
= str(hex(NeedFixPcd
.PcdBinOffset
))
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)
595 lenOfUnfixedList
-= 1
597 # Write the final data into output files.
599 def GenerateVpdFile (self
, MapFileName
, BinFileName
):
600 #Open an VPD file to process
603 fVpdFile
= open (BinFileName
, "wb", 0)
606 EdkLogger
.error("BPDG", BuildToolError
.FILE_OPEN_FAILURE
, "File open failed for %s" %self
.VpdFileName
,None)
609 fMapFile
= open (MapFileName
, "w", 0)
612 EdkLogger
.error("BPDG", BuildToolError
.FILE_OPEN_FAILURE
, "File open failed for %s" %self
.MapFileName
,None)
614 # Use a instance of StringIO to cache data
615 fStringIO
= StringIO
.StringIO('')
617 # Write the header of map file.
619 fMapFile
.write (st
.MAP_FILE_COMMENT_TEMPLATE
+ "\n")
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)
623 for eachPcd
in self
.PcdFixedOffsetSizeList
:
626 fMapFile
.write("%s | %s | %s | %s \n" % (eachPcd
.PcdCName
, eachPcd
.PcdOffset
, eachPcd
.PcdSize
,eachPcd
.PcdUnpackValue
))
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)
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
))
636 fStringIO
.write (eachPcd
.PcdValue
)
639 fVpdFile
.write (fStringIO
.getvalue())
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)