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 - 2018, Intel Corporation. All rights reserved.<BR>
7 # SPDX-License-Identifier: BSD-2-Clause-Patent
10 from __future__
import absolute_import
11 import Common
.LongFilePathOs
as os
12 from io
import BytesIO
13 from . import StringTable
as st
16 from Common
.LongFilePathSupport
import OpenLongFilePath
as open
18 from Common
.DataType
import MAX_SIZE_TYPE
, MAX_VAL_TYPE
, TAB_STAR
19 import Common
.EdkLogger
as EdkLogger
20 import Common
.BuildToolError
as BuildToolError
22 _FORMAT_CHAR
= {1: 'B',
28 ## The VPD PCD data structure for store and process each VPD PCD entry.
30 # This class contain method to format and pack pcd's value.
33 def __init__(self
, PcdCName
, SkuId
,PcdOffset
, PcdSize
, PcdValue
, Lineno
=None, FileName
=None, PcdUnpackValue
=None,
34 PcdBinOffset
=None, PcdBinSize
=None, Alignment
=None):
35 self
.PcdCName
= PcdCName
.strip()
36 self
.SkuId
= SkuId
.strip()
37 self
.PcdOffset
= PcdOffset
.strip()
38 self
.PcdSize
= PcdSize
.strip()
39 self
.PcdValue
= PcdValue
.strip()
40 self
.Lineno
= Lineno
.strip()
41 self
.FileName
= FileName
.strip()
42 self
.PcdUnpackValue
= PcdUnpackValue
43 self
.PcdBinOffset
= PcdBinOffset
44 self
.PcdBinSize
= PcdBinSize
45 self
.Alignment
= Alignment
47 if self
.PcdValue
== '' :
48 EdkLogger
.error("BPDG", BuildToolError
.FORMAT_INVALID
,
49 "Invalid PCD format(Name: %s File: %s line: %s) , no Value specified!" % (self
.PcdCName
, self
.FileName
, self
.Lineno
))
51 if self
.PcdOffset
== '' :
52 EdkLogger
.error("BPDG", BuildToolError
.FORMAT_INVALID
,
53 "Invalid PCD format(Name: %s File: %s Line: %s) , no Offset specified!" % (self
.PcdCName
, self
.FileName
, self
.Lineno
))
55 if self
.PcdSize
== '' :
56 EdkLogger
.error("BPDG", BuildToolError
.FORMAT_INVALID
,
57 "Invalid PCD format(Name: %s File: %s Line: %s), no PcdSize specified!" % (self
.PcdCName
, self
.FileName
, self
.Lineno
))
59 self
._GenOffsetValue
()
61 ## Analyze the string value to judge the PCD's datum type equal to Boolean or not.
63 # @param ValueString PCD's value
64 # @param Size PCD's size
66 # @retval True PCD's datum type is Boolean
67 # @retval False PCD's datum type is not Boolean.
69 def _IsBoolean(self
, ValueString
, Size
):
71 if ValueString
.upper() in ["TRUE", "FALSE"]:
73 elif ValueString
in ["0", "1", "0x0", "0x1", "0x00", "0x01"]:
78 ## Convert the PCD's value from string to integer.
80 # This function will try to convert the Offset value form string to integer
81 # for both hexadecimal and decimal.
83 def _GenOffsetValue(self
):
84 if self
.PcdOffset
!= TAB_STAR
:
86 self
.PcdBinOffset
= int (self
.PcdOffset
)
89 self
.PcdBinOffset
= int(self
.PcdOffset
, 16)
91 EdkLogger
.error("BPDG", BuildToolError
.FORMAT_INVALID
,
92 "Invalid offset value %s for PCD %s (File: %s Line: %s)" % (self
.PcdOffset
, self
.PcdCName
, self
.FileName
, self
.Lineno
))
94 ## Pack Boolean type VPD PCD's value form string to binary type.
96 # @param ValueString The boolean type string for pack.
99 def _PackBooleanValue(self
, ValueString
):
100 if ValueString
.upper() == "TRUE" or ValueString
in ["1", "0x1", "0x01"]:
102 self
.PcdValue
= pack(_FORMAT_CHAR
[1], 1)
104 EdkLogger
.error("BPDG", BuildToolError
.FORMAT_INVALID
,
105 "Invalid size or value for PCD %s to pack(File: %s Line: %s)." % (self
.PcdCName
, self
.FileName
, self
.Lineno
))
108 self
.PcdValue
= pack(_FORMAT_CHAR
[1], 0)
110 EdkLogger
.error("BPDG", BuildToolError
.FORMAT_INVALID
,
111 "Invalid size or value for PCD %s to pack(File: %s Line: %s)." % (self
.PcdCName
, self
.FileName
, self
.Lineno
))
113 ## Pack Integer type VPD PCD's value form string to binary type.
115 # @param ValueString The Integer type string for pack.
118 def _PackIntValue(self
, IntValue
, Size
):
119 if Size
not in _FORMAT_CHAR
:
120 EdkLogger
.error("BPDG", BuildToolError
.FORMAT_INVALID
,
121 "Invalid size %d for PCD %s in integer datum size(File: %s Line: %s)." % (Size
, self
.PcdCName
, self
.FileName
, self
.Lineno
))
123 for Type
, MaxSize
in MAX_SIZE_TYPE
.items():
124 if Type
== 'BOOLEAN':
128 EdkLogger
.error("BPDG", BuildToolError
.FORMAT_INVALID
,
129 "PCD can't be set to negative value %d for PCD %s in %s datum type(File: %s Line: %s)." % (
130 IntValue
, self
.PcdCName
, Type
, self
.FileName
, self
.Lineno
))
131 elif IntValue
> MAX_VAL_TYPE
[Type
]:
132 EdkLogger
.error("BPDG", BuildToolError
.FORMAT_INVALID
,
133 "Too large PCD value %d for datum type %s for PCD %s(File: %s Line: %s)." % (
134 IntValue
, Type
, self
.PcdCName
, self
.FileName
, self
.Lineno
))
137 self
.PcdValue
= pack(_FORMAT_CHAR
[Size
], IntValue
)
139 EdkLogger
.error("BPDG", BuildToolError
.FORMAT_INVALID
,
140 "Invalid size or value for PCD %s to pack(File: %s Line: %s)." % (self
.PcdCName
, self
.FileName
, self
.Lineno
))
142 ## Pack VOID* type VPD PCD's value form string to binary type.
144 # The VOID* type of string divided into 3 sub-type:
145 # 1: L"String"/L'String', Unicode type string.
146 # 2: "String"/'String', Ascii type string.
147 # 3: {bytearray}, only support byte-array.
149 # @param ValueString The Integer type string for pack.
151 def _PackPtrValue(self
, ValueString
, Size
):
152 if ValueString
.startswith('L"') or ValueString
.startswith("L'"):
153 self
._PackUnicode
(ValueString
, Size
)
154 elif ValueString
.startswith('{') and ValueString
.endswith('}'):
155 self
._PackByteArray
(ValueString
, Size
)
156 elif (ValueString
.startswith('"') and ValueString
.endswith('"')) or (ValueString
.startswith("'") and ValueString
.endswith("'")):
157 self
._PackString
(ValueString
, Size
)
159 EdkLogger
.error("BPDG", BuildToolError
.FORMAT_INVALID
,
160 "Invalid VOID* type PCD %s value %s (File: %s Line: %s)" % (self
.PcdCName
, ValueString
, self
.FileName
, self
.Lineno
))
162 ## Pack an Ascii PCD value.
164 # An Ascii string for a PCD should be in format as ""/''.
166 def _PackString(self
, ValueString
, Size
):
168 EdkLogger
.error("BPDG", BuildToolError
.FORMAT_INVALID
,
169 "Invalid parameter Size %s of PCD %s!(File: %s Line: %s)" % (self
.PcdBinSize
, self
.PcdCName
, self
.FileName
, self
.Lineno
))
170 if (ValueString
== ""):
171 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
))
174 if ValueString
.startswith("'"):
177 ValueString
= ValueString
[1:-1]
178 # No null-terminator in 'string'
179 if (QuotedFlag
and len(ValueString
) + 1 > Size
) or (not QuotedFlag
and len(ValueString
) > Size
):
180 EdkLogger
.error("BPDG", BuildToolError
.RESOURCE_OVERFLOW
,
181 "PCD value string %s is exceed to size %d(File: %s Line: %s)" % (ValueString
, Size
, self
.FileName
, self
.Lineno
))
183 self
.PcdValue
= pack('%ds' % Size
, ValueString
.encode('utf-8'))
185 EdkLogger
.error("BPDG", BuildToolError
.FORMAT_INVALID
,
186 "Invalid size or value for PCD %s to pack(File: %s Line: %s)." % (self
.PcdCName
, self
.FileName
, self
.Lineno
))
188 ## Pack a byte-array PCD value.
190 # A byte-array for a PCD should be in format as {0x01, 0x02, ...}.
192 def _PackByteArray(self
, ValueString
, Size
):
194 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
))
195 if (ValueString
== ""):
196 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
))
198 ValueString
= ValueString
.strip()
199 ValueString
= ValueString
.lstrip('{').strip('}')
200 ValueList
= ValueString
.split(',')
201 ValueList
= [item
.strip() for item
in ValueList
]
203 if len(ValueList
) > Size
:
204 EdkLogger
.error("BPDG", BuildToolError
.RESOURCE_OVERFLOW
,
205 "The byte array %s is too large for size %d(File: %s Line: %s)" % (ValueString
, Size
, self
.FileName
, self
.Lineno
))
207 ReturnArray
= array
.array('B')
209 for Index
in range(len(ValueList
)):
211 if ValueList
[Index
].lower().startswith('0x'):
212 # translate hex value
214 Value
= int(ValueList
[Index
], 16)
216 EdkLogger
.error("BPDG", BuildToolError
.FORMAT_INVALID
,
217 "The value item %s in byte array %s is an invalid HEX value.(File: %s Line: %s)" % \
218 (ValueList
[Index
], ValueString
, self
.FileName
, self
.Lineno
))
220 # translate decimal value
222 Value
= int(ValueList
[Index
], 10)
224 EdkLogger
.error("BPDG", BuildToolError
.FORMAT_INVALID
,
225 "The value item %s in byte array %s is an invalid DECIMAL value.(File: %s Line: %s)" % \
226 (ValueList
[Index
], ValueString
, self
.FileName
, self
.Lineno
))
229 EdkLogger
.error("BPDG", BuildToolError
.FORMAT_INVALID
,
230 "The value item %s in byte array %s do not in range 0 ~ 0xFF(File: %s Line: %s)" % \
231 (ValueList
[Index
], ValueString
, self
.FileName
, self
.Lineno
))
233 ReturnArray
.append(Value
)
235 for Index
in range(len(ValueList
), Size
):
236 ReturnArray
.append(0)
238 self
.PcdValue
= ReturnArray
.tolist()
240 ## Pack a unicode PCD value into byte array.
242 # A unicode string for a PCD should be in format as L""/L''.
244 def _PackUnicode(self
, UnicodeString
, Size
):
246 EdkLogger
.error("BPDG", BuildToolError
.FORMAT_INVALID
, "Invalid parameter Size %s of PCD %s!(File: %s Line: %s)" % \
247 (self
.PcdBinSize
, self
.PcdCName
, self
.FileName
, self
.Lineno
))
250 if UnicodeString
.startswith("L'"):
252 UnicodeString
= UnicodeString
[2:-1]
254 # No null-terminator in L'string'
255 if (QuotedFlag
and (len(UnicodeString
) + 1) * 2 > Size
) or (not QuotedFlag
and len(UnicodeString
) * 2 > Size
):
256 EdkLogger
.error("BPDG", BuildToolError
.RESOURCE_OVERFLOW
,
257 "The size of unicode string %s is too larger for size %s(File: %s Line: %s)" % \
258 (UnicodeString
, Size
, self
.FileName
, self
.Lineno
))
260 ReturnArray
= array
.array('B')
261 for Value
in UnicodeString
:
263 ReturnArray
.append(ord(Value
))
264 ReturnArray
.append(0)
266 EdkLogger
.error("BPDG", BuildToolError
.FORMAT_INVALID
,
267 "Invalid unicode character %s in unicode string %s(File: %s Line: %s)" % \
268 (Value
, UnicodeString
, self
.FileName
, self
.Lineno
))
270 for Index
in range(len(UnicodeString
) * 2, Size
):
271 ReturnArray
.append(0)
273 self
.PcdValue
= ReturnArray
.tolist()
277 ## The class implementing the BPDG VPD PCD offset fix process
279 # The VPD PCD offset fix process includes:
280 # 1. Parse the input guided.txt file and store it in the data structure;
281 # 2. Format the input file data to remove unused lines;
282 # 3. Fixed offset if needed;
283 # 4. Generate output file, including guided.map and guided.bin file;
286 ## Constructor of DscBuildData
288 # Initialize object of GenVPD
289 # @Param InputFileName The filename include the vpd type pcd information
290 # @param MapFileName The filename of map file that stores vpd type pcd information.
291 # This file will be generated by the BPDG tool after fix the offset
292 # and adjust the offset to make the pcd data aligned.
293 # @param VpdFileName The filename of Vpd file that hold vpd pcd information.
295 def __init__(self
, InputFileName
, MapFileName
, VpdFileName
):
296 self
.InputFileName
= InputFileName
297 self
.MapFileName
= MapFileName
298 self
.VpdFileName
= VpdFileName
299 self
.FileLinesList
= []
300 self
.PcdFixedOffsetSizeList
= []
301 self
.PcdUnknownOffsetList
= []
303 fInputfile
= open(InputFileName
, "r")
305 self
.FileLinesList
= fInputfile
.readlines()
307 EdkLogger
.error("BPDG", BuildToolError
.FILE_READ_FAILURE
, "File read failed for %s" % InputFileName
, None)
311 EdkLogger
.error("BPDG", BuildToolError
.FILE_OPEN_FAILURE
, "File open failed for %s" % InputFileName
, None)
314 # Parser the input file which is generated by the build tool. Convert the value of each pcd's
315 # from string to it's real format. Also remove the useless line in the input file.
317 def ParserInputFile (self
):
319 for line
in self
.FileLinesList
:
320 # Strip "\r\n" generated by readlines ().
322 line
= line
.rstrip(os
.linesep
)
324 # Skip the comment line
325 if (not line
.startswith("#")) and len(line
) > 1 :
327 # Enhanced for support "|" character in the string.
329 ValueList
= ['', '', '', '', '']
331 ValueRe
= re
.compile(r
'\s*L?\".*\|.*\"\s*$')
332 PtrValue
= ValueRe
.findall(line
)
334 ValueUpdateFlag
= False
336 if len(PtrValue
) >= 1:
337 line
= re
.sub(ValueRe
, '', line
)
338 ValueUpdateFlag
= True
340 TokenList
= line
.split('|')
341 ValueList
[0:len(TokenList
)] = TokenList
344 ValueList
[4] = PtrValue
[0]
345 self
.FileLinesList
[count
] = ValueList
346 # Store the line number
347 self
.FileLinesList
[count
].append(str(count
+ 1))
348 elif len(line
) <= 1 :
349 # Set the blank line to "None"
350 self
.FileLinesList
[count
] = None
352 # Set the comment line to "None"
353 self
.FileLinesList
[count
] = None
356 # The line count contain usage information
358 # Delete useless lines
361 if (self
.FileLinesList
[count
] is None) :
362 del(self
.FileLinesList
[count
])
368 # After remove the useless line, if there are no data remain in the file line list,
369 # Report warning messages to user's.
371 if len(self
.FileLinesList
) == 0 :
372 EdkLogger
.warn('BPDG', BuildToolError
.RESOURCE_NOT_AVAILABLE
,
373 "There are no VPD type pcds defined in DSC file, Please check it.")
375 # Process the pcds one by one base on the pcd's value and size
377 for line
in self
.FileLinesList
:
378 if line
is not None :
379 PCD
= PcdEntry(line
[0], line
[1], line
[2], line
[3], line
[4], line
[5], self
.InputFileName
)
380 # Strip the space char
381 PCD
.PcdCName
= PCD
.PcdCName
.strip(' ')
382 PCD
.SkuId
= PCD
.SkuId
.strip(' ')
383 PCD
.PcdOffset
= PCD
.PcdOffset
.strip(' ')
384 PCD
.PcdSize
= PCD
.PcdSize
.strip(' ')
385 PCD
.PcdValue
= PCD
.PcdValue
.strip(' ')
386 PCD
.Lineno
= PCD
.Lineno
.strip(' ')
389 # Store the original pcd value.
390 # This information will be useful while generate the output map file.
392 PCD
.PcdUnpackValue
= str(PCD
.PcdValue
)
395 # Translate PCD size string to an integer value.
398 PackSize
= int(PCD
.PcdSize
, 10)
399 PCD
.PcdBinSize
= PackSize
402 PackSize
= int(PCD
.PcdSize
, 16)
403 PCD
.PcdBinSize
= PackSize
405 EdkLogger
.error("BPDG", BuildToolError
.FORMAT_INVALID
, "Invalid PCD size value %s at file: %s line: %s" % (PCD
.PcdSize
, self
.InputFileName
, PCD
.Lineno
))
408 # If value is Unicode string (e.g. L""), then use 2-byte alignment
409 # If value is byte array (e.g. {}), then use 8-byte alignment
411 PCD
.PcdOccupySize
= PCD
.PcdBinSize
412 if PCD
.PcdUnpackValue
.startswith("{"):
414 elif PCD
.PcdUnpackValue
.startswith("L"):
419 PCD
.Alignment
= Alignment
420 if PCD
.PcdOffset
!= TAB_STAR
:
421 if PCD
.PcdOccupySize
% Alignment
!= 0:
422 if PCD
.PcdUnpackValue
.startswith("{"):
423 EdkLogger
.warn("BPDG", "The offset value of PCD %s is not 8-byte aligned!" %(PCD
.PcdCName
), File
=self
.InputFileName
)
425 EdkLogger
.error("BPDG", BuildToolError
.FORMAT_INVALID
, 'The offset value of PCD %s should be %s-byte aligned.' % (PCD
.PcdCName
, Alignment
))
427 if PCD
.PcdOccupySize
% Alignment
!= 0:
428 PCD
.PcdOccupySize
= (PCD
.PcdOccupySize
// Alignment
+ 1) * Alignment
430 PackSize
= PCD
.PcdOccupySize
431 if PCD
._IsBoolean
(PCD
.PcdValue
, PCD
.PcdSize
):
432 PCD
._PackBooleanValue
(PCD
.PcdValue
)
433 self
.FileLinesList
[count
] = PCD
437 # Try to translate value to an integer firstly.
442 PackValue
= int(PCD
.PcdValue
)
445 PackValue
= int(PCD
.PcdValue
, 16)
450 PCD
._PackIntValue
(PackValue
, PackSize
)
452 PCD
._PackPtrValue
(PCD
.PcdValue
, PackSize
)
454 self
.FileLinesList
[count
] = PCD
460 # This function used to create a clean list only contain useful information and reorganized to make it
463 def FormatFileLine (self
) :
465 for eachPcd
in self
.FileLinesList
:
466 if eachPcd
.PcdOffset
!= TAB_STAR
:
467 # Use pcd's Offset value as key, and pcd's Value as value
468 self
.PcdFixedOffsetSizeList
.append(eachPcd
)
470 # Use pcd's CName as key, and pcd's Size as value
471 self
.PcdUnknownOffsetList
.append(eachPcd
)
475 # This function is use to fix the offset value which the not specified in the map file.
476 # Usually it use the star (meaning any offset) character in the offset field
478 def FixVpdOffset (self
):
479 # At first, the offset should start at 0
480 # Sort fixed offset list in order to find out where has free spaces for the pcd's offset
481 # value is TAB_STAR to insert into.
483 self
.PcdFixedOffsetSizeList
.sort(key
=lambda x
: x
.PcdBinOffset
)
486 # Sort the un-fixed pcd's offset by it's size.
488 self
.PcdUnknownOffsetList
.sort(key
=lambda x
: x
.PcdBinSize
)
491 for pcd
in self
.PcdUnknownOffsetList
:
493 if pcd
.PcdCName
== ".".join(("gEfiMdeModulePkgTokenSpaceGuid", "PcdNvStoreDefaultValueBuffer")):
494 if index
!= len(self
.PcdUnknownOffsetList
):
495 for i
in range(len(self
.PcdUnknownOffsetList
) - index
):
496 self
.PcdUnknownOffsetList
[index
+i
-1 ], self
.PcdUnknownOffsetList
[index
+i
] = self
.PcdUnknownOffsetList
[index
+i
], self
.PcdUnknownOffsetList
[index
+i
-1]
499 # Process all Offset value are TAB_STAR
501 if (len(self
.PcdFixedOffsetSizeList
) == 0) and (len(self
.PcdUnknownOffsetList
) != 0) :
502 # The offset start from 0
504 for Pcd
in self
.PcdUnknownOffsetList
:
505 if NowOffset
% Pcd
.Alignment
!= 0:
506 NowOffset
= (NowOffset
// Pcd
.Alignment
+ 1) * Pcd
.Alignment
507 Pcd
.PcdBinOffset
= NowOffset
508 Pcd
.PcdOffset
= str(hex(Pcd
.PcdBinOffset
))
509 NowOffset
+= Pcd
.PcdOccupySize
511 self
.PcdFixedOffsetSizeList
= self
.PcdUnknownOffsetList
514 # Check the offset of VPD type pcd's offset start from 0.
515 if self
.PcdFixedOffsetSizeList
[0].PcdBinOffset
!= 0 :
516 EdkLogger
.warn("BPDG", "The offset of VPD type pcd should start with 0, please check it.",
519 # Judge whether the offset in fixed pcd offset list is overlapped or not.
520 lenOfList
= len(self
.PcdFixedOffsetSizeList
)
522 while (count
< lenOfList
- 1) :
523 PcdNow
= self
.PcdFixedOffsetSizeList
[count
]
524 PcdNext
= self
.PcdFixedOffsetSizeList
[count
+1]
525 # Two pcd's offset is same
526 if PcdNow
.PcdBinOffset
== PcdNext
.PcdBinOffset
:
527 EdkLogger
.error("BPDG", BuildToolError
.ATTRIBUTE_GET_FAILURE
,
528 "The offset of %s at line: %s is same with %s at line: %s in file %s" % \
529 (PcdNow
.PcdCName
, PcdNow
.Lineno
, PcdNext
.PcdCName
, PcdNext
.Lineno
, PcdNext
.FileName
),
533 if PcdNow
.PcdBinOffset
+ PcdNow
.PcdOccupySize
> PcdNext
.PcdBinOffset
:
534 EdkLogger
.error("BPDG", BuildToolError
.ATTRIBUTE_GET_FAILURE
,
535 "The offset of %s at line: %s is overlapped with %s at line: %s in file %s" % \
536 (PcdNow
.PcdCName
, PcdNow
.Lineno
, PcdNext
.PcdCName
, PcdNext
.Lineno
, PcdNext
.FileName
),
539 # Has free space, raise a warning message
540 if PcdNow
.PcdBinOffset
+ PcdNow
.PcdOccupySize
< PcdNext
.PcdBinOffset
:
541 EdkLogger
.warn("BPDG", BuildToolError
.ATTRIBUTE_GET_FAILURE
,
542 "The offsets have free space of between %s at line: %s and %s at line: %s in file %s" % \
543 (PcdNow
.PcdCName
, PcdNow
.Lineno
, PcdNext
.PcdCName
, PcdNext
.Lineno
, PcdNext
.FileName
),
547 LastOffset
= self
.PcdFixedOffsetSizeList
[0].PcdBinOffset
548 FixOffsetSizeListCount
= 0
549 lenOfList
= len(self
.PcdFixedOffsetSizeList
)
550 lenOfUnfixedList
= len(self
.PcdUnknownOffsetList
)
553 # Insert the un-fixed offset pcd's list into fixed offset pcd's list if has free space between those pcds.
555 while (FixOffsetSizeListCount
< lenOfList
) :
557 eachFixedPcd
= self
.PcdFixedOffsetSizeList
[FixOffsetSizeListCount
]
558 NowOffset
= eachFixedPcd
.PcdBinOffset
561 if LastOffset
< NowOffset
:
562 if lenOfUnfixedList
!= 0 :
563 countOfUnfixedList
= 0
564 while(countOfUnfixedList
< lenOfUnfixedList
) :
565 eachUnfixedPcd
= self
.PcdUnknownOffsetList
[countOfUnfixedList
]
566 needFixPcdSize
= eachUnfixedPcd
.PcdOccupySize
568 if eachUnfixedPcd
.PcdOffset
== TAB_STAR
:
569 if LastOffset
% eachUnfixedPcd
.Alignment
!= 0:
570 LastOffset
= (LastOffset
// eachUnfixedPcd
.Alignment
+ 1) * eachUnfixedPcd
.Alignment
571 # The offset un-fixed pcd can write into this free space
572 if needFixPcdSize
<= (NowOffset
- LastOffset
) :
573 # Change the offset value of un-fixed pcd
574 eachUnfixedPcd
.PcdOffset
= str(hex(LastOffset
))
575 eachUnfixedPcd
.PcdBinOffset
= LastOffset
576 # Insert this pcd into fixed offset pcd list.
577 self
.PcdFixedOffsetSizeList
.insert(FixOffsetSizeListCount
, eachUnfixedPcd
)
579 # Delete the item's offset that has been fixed and added into fixed offset list
580 self
.PcdUnknownOffsetList
.pop(countOfUnfixedList
)
582 # After item added, should enlarge the length of fixed pcd offset list
584 FixOffsetSizeListCount
+= 1
586 # Decrease the un-fixed pcd offset list's length
587 lenOfUnfixedList
-= 1
589 # Modify the last offset value
590 LastOffset
+= needFixPcdSize
592 # It can not insert into those two pcds, need to check still has other space can store it.
593 LastOffset
= NowOffset
+ self
.PcdFixedOffsetSizeList
[FixOffsetSizeListCount
].PcdOccupySize
594 FixOffsetSizeListCount
+= 1
597 # Set the FixOffsetSizeListCount = lenOfList for quit the loop
599 FixOffsetSizeListCount
= lenOfList
601 # No free space, smoothly connect with previous pcd.
602 elif LastOffset
== NowOffset
:
603 LastOffset
= NowOffset
+ eachFixedPcd
.PcdOccupySize
604 FixOffsetSizeListCount
+= 1
605 # Usually it will not enter into this thunk, if so, means it overlapped.
607 EdkLogger
.error("BPDG", BuildToolError
.ATTRIBUTE_NOT_AVAILABLE
,
608 "The offset value definition has overlapped at pcd: %s, it's offset is: %s, in file: %s line: %s" % \
609 (eachFixedPcd
.PcdCName
, eachFixedPcd
.PcdOffset
, eachFixedPcd
.InputFileName
, eachFixedPcd
.Lineno
),
611 FixOffsetSizeListCount
+= 1
613 # Continue to process the un-fixed offset pcd's list, add this time, just append them behind the fixed pcd's offset list.
614 lenOfUnfixedList
= len(self
.PcdUnknownOffsetList
)
615 lenOfList
= len(self
.PcdFixedOffsetSizeList
)
616 while (lenOfUnfixedList
> 0) :
617 # Still has items need to process
618 # The last pcd instance
619 LastPcd
= self
.PcdFixedOffsetSizeList
[lenOfList
-1]
620 NeedFixPcd
= self
.PcdUnknownOffsetList
[0]
622 NeedFixPcd
.PcdBinOffset
= LastPcd
.PcdBinOffset
+ LastPcd
.PcdOccupySize
623 if NeedFixPcd
.PcdBinOffset
% NeedFixPcd
.Alignment
!= 0:
624 NeedFixPcd
.PcdBinOffset
= (NeedFixPcd
.PcdBinOffset
// NeedFixPcd
.Alignment
+ 1) * NeedFixPcd
.Alignment
626 NeedFixPcd
.PcdOffset
= str(hex(NeedFixPcd
.PcdBinOffset
))
628 # Insert this pcd into fixed offset pcd list's tail.
629 self
.PcdFixedOffsetSizeList
.insert(lenOfList
, NeedFixPcd
)
630 # Delete the item's offset that has been fixed and added into fixed offset list
631 self
.PcdUnknownOffsetList
.pop(0)
634 lenOfUnfixedList
-= 1
636 # Write the final data into output files.
638 def GenerateVpdFile (self
, MapFileName
, BinFileName
):
639 #Open an VPD file to process
642 fVpdFile
= open(BinFileName
, "wb")
645 EdkLogger
.error("BPDG", BuildToolError
.FILE_OPEN_FAILURE
, "File open failed for %s" % self
.VpdFileName
, None)
648 fMapFile
= open(MapFileName
, "w")
651 EdkLogger
.error("BPDG", BuildToolError
.FILE_OPEN_FAILURE
, "File open failed for %s" % self
.MapFileName
, None)
653 # Use a instance of BytesIO to cache data
654 fStringIO
= BytesIO()
656 # Write the header of map file.
658 fMapFile
.write (st
.MAP_FILE_COMMENT_TEMPLATE
+ "\n")
660 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)
662 for eachPcd
in self
.PcdFixedOffsetSizeList
:
665 fMapFile
.write("%s | %s | %s | %s | %s \n" % (eachPcd
.PcdCName
, eachPcd
.SkuId
, eachPcd
.PcdOffset
, eachPcd
.PcdSize
, eachPcd
.PcdUnpackValue
))
667 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)
669 # Write Vpd binary file
670 fStringIO
.seek (eachPcd
.PcdBinOffset
)
671 if isinstance(eachPcd
.PcdValue
, list):
672 for i
in range(len(eachPcd
.PcdValue
)):
673 Value
= eachPcd
.PcdValue
[i
:i
+ 1]
674 if isinstance(bytes(Value
), str):
675 fStringIO
.write(chr(Value
[0]))
677 fStringIO
.write(bytes(Value
))
679 fStringIO
.write (eachPcd
.PcdValue
)
682 fVpdFile
.write (fStringIO
.getvalue())
684 EdkLogger
.error("BPDG", BuildToolError
.FILE_WRITE_FAILURE
, "Write data to file %s failed, please check whether the file been locked or using by other applications." % self
.VpdFileName
, None)