]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/BPDG/GenVpd.py
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[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 - 2018, Intel Corporation. All rights reserved.<BR>
6 #
7 # SPDX-License-Identifier: BSD-2-Clause-Patent
8 #
9
10 from __future__ import absolute_import
11 import Common.LongFilePathOs as os
12 from io import BytesIO
13 from . import StringTable as st
14 import array
15 import re
16 from Common.LongFilePathSupport import OpenLongFilePath as open
17 from struct import *
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
21
22 _FORMAT_CHAR = {1: 'B',
23 2: 'H',
24 4: 'I',
25 8: 'Q'
26 }
27
28 ## The VPD PCD data structure for store and process each VPD PCD entry.
29 #
30 # This class contain method to format and pack pcd's value.
31 #
32 class PcdEntry:
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
46
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))
50
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))
54
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))
58
59 self._GenOffsetValue ()
60
61 ## Analyze the string value to judge the PCD's datum type equal to Boolean or not.
62 #
63 # @param ValueString PCD's value
64 # @param Size PCD's size
65 #
66 # @retval True PCD's datum type is Boolean
67 # @retval False PCD's datum type is not Boolean.
68 #
69 def _IsBoolean(self, ValueString, Size):
70 if (Size == "1"):
71 if ValueString.upper() in ["TRUE", "FALSE"]:
72 return True
73 elif ValueString in ["0", "1", "0x0", "0x1", "0x00", "0x01"]:
74 return True
75
76 return False
77
78 ## Convert the PCD's value from string to integer.
79 #
80 # This function will try to convert the Offset value form string to integer
81 # for both hexadecimal and decimal.
82 #
83 def _GenOffsetValue(self):
84 if self.PcdOffset != TAB_STAR:
85 try:
86 self.PcdBinOffset = int (self.PcdOffset)
87 except:
88 try:
89 self.PcdBinOffset = int(self.PcdOffset, 16)
90 except:
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))
93
94 ## Pack Boolean type VPD PCD's value form string to binary type.
95 #
96 # @param ValueString The boolean type string for pack.
97 #
98 #
99 def _PackBooleanValue(self, ValueString):
100 if ValueString.upper() == "TRUE" or ValueString in ["1", "0x1", "0x01"]:
101 try:
102 self.PcdValue = pack(_FORMAT_CHAR[1], 1)
103 except:
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))
106 else:
107 try:
108 self.PcdValue = pack(_FORMAT_CHAR[1], 0)
109 except:
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))
112
113 ## Pack Integer type VPD PCD's value form string to binary type.
114 #
115 # @param ValueString The Integer type string for pack.
116 #
117 #
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))
122
123 for Type, MaxSize in MAX_SIZE_TYPE.items():
124 if Type == 'BOOLEAN':
125 continue
126 if Size == MaxSize:
127 if IntValue < 0:
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))
135
136 try:
137 self.PcdValue = pack(_FORMAT_CHAR[Size], IntValue)
138 except:
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))
141
142 ## Pack VOID* type VPD PCD's value form string to binary type.
143 #
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.
148 #
149 # @param ValueString The Integer type string for pack.
150 #
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)
158 else:
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))
161
162 ## Pack an Ascii PCD value.
163 #
164 # An Ascii string for a PCD should be in format as ""/''.
165 #
166 def _PackString(self, ValueString, Size):
167 if (Size < 0):
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))
172
173 QuotedFlag = True
174 if ValueString.startswith("'"):
175 QuotedFlag = False
176
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))
182 try:
183 self.PcdValue = pack('%ds' % Size, ValueString.encode('utf-8'))
184 except:
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))
187
188 ## Pack a byte-array PCD value.
189 #
190 # A byte-array for a PCD should be in format as {0x01, 0x02, ...}.
191 #
192 def _PackByteArray(self, ValueString, Size):
193 if (Size < 0):
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))
197
198 ValueString = ValueString.strip()
199 ValueString = ValueString.lstrip('{').strip('}')
200 ValueList = ValueString.split(',')
201 ValueList = [item.strip() for item in ValueList]
202
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))
206
207 ReturnArray = array.array('B')
208
209 for Index in range(len(ValueList)):
210 Value = None
211 if ValueList[Index].lower().startswith('0x'):
212 # translate hex value
213 try:
214 Value = int(ValueList[Index], 16)
215 except:
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))
219 else:
220 # translate decimal value
221 try:
222 Value = int(ValueList[Index], 10)
223 except:
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))
227
228 if Value > 255:
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))
232
233 ReturnArray.append(Value)
234
235 for Index in range(len(ValueList), Size):
236 ReturnArray.append(0)
237
238 self.PcdValue = ReturnArray.tolist()
239
240 ## Pack a unicode PCD value into byte array.
241 #
242 # A unicode string for a PCD should be in format as L""/L''.
243 #
244 def _PackUnicode(self, UnicodeString, Size):
245 if (Size < 0):
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))
248
249 QuotedFlag = True
250 if UnicodeString.startswith("L'"):
251 QuotedFlag = False
252 UnicodeString = UnicodeString[2:-1]
253
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))
259
260 ReturnArray = array.array('B')
261 for Value in UnicodeString:
262 try:
263 ReturnArray.append(ord(Value))
264 ReturnArray.append(0)
265 except:
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))
269
270 for Index in range(len(UnicodeString) * 2, Size):
271 ReturnArray.append(0)
272
273 self.PcdValue = ReturnArray.tolist()
274
275
276
277 ## The class implementing the BPDG VPD PCD offset fix process
278 #
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;
284 #
285 class GenVPD :
286 ## Constructor of DscBuildData
287 #
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.
294 #
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 = []
302 try:
303 fInputfile = open(InputFileName, "r")
304 try:
305 self.FileLinesList = fInputfile.readlines()
306 except:
307 EdkLogger.error("BPDG", BuildToolError.FILE_READ_FAILURE, "File read failed for %s" % InputFileName, None)
308 finally:
309 fInputfile.close()
310 except:
311 EdkLogger.error("BPDG", BuildToolError.FILE_OPEN_FAILURE, "File open failed for %s" % InputFileName, None)
312
313 ##
314 # Parser the input file which is generated by the build tool. Convert the value of each pcd's
315 # from string to its real format. Also remove the useless line in the input file.
316 #
317 def ParserInputFile (self):
318 count = 0
319 for line in self.FileLinesList:
320 # Strip "\r\n" generated by readlines ().
321 line = line.strip()
322 line = line.rstrip(os.linesep)
323
324 # Skip the comment line
325 if (not line.startswith("#")) and len(line) > 1 :
326 #
327 # Enhanced for support "|" character in the string.
328 #
329 ValueList = ['', '', '', '', '']
330
331 ValueRe = re.compile(r'\s*L?\".*\|.*\"\s*$')
332 PtrValue = ValueRe.findall(line)
333
334 ValueUpdateFlag = False
335
336 if len(PtrValue) >= 1:
337 line = re.sub(ValueRe, '', line)
338 ValueUpdateFlag = True
339
340 TokenList = line.split('|')
341 ValueList[0:len(TokenList)] = TokenList
342
343 if ValueUpdateFlag:
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
351 else :
352 # Set the comment line to "None"
353 self.FileLinesList[count] = None
354 count += 1
355
356 # The line count contain usage information
357 count = 0
358 # Delete useless lines
359 while (True) :
360 try :
361 if (self.FileLinesList[count] is None) :
362 del(self.FileLinesList[count])
363 else :
364 count += 1
365 except :
366 break
367 #
368 # After remove the useless line, if there are no data remain in the file line list,
369 # Report warning messages to user's.
370 #
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.")
374
375 # Process the pcds one by one base on the pcd's value and size
376 count = 0
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(' ')
387
388 #
389 # Store the original pcd value.
390 # This information will be useful while generate the output map file.
391 #
392 PCD.PcdUnpackValue = str(PCD.PcdValue)
393
394 #
395 # Translate PCD size string to an integer value.
396 PackSize = None
397 try:
398 PackSize = int(PCD.PcdSize, 10)
399 PCD.PcdBinSize = PackSize
400 except:
401 try:
402 PackSize = int(PCD.PcdSize, 16)
403 PCD.PcdBinSize = PackSize
404 except:
405 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID, "Invalid PCD size value %s at file: %s line: %s" % (PCD.PcdSize, self.InputFileName, PCD.Lineno))
406
407 #
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
410 #
411 PCD.PcdOccupySize = PCD.PcdBinSize
412 if PCD.PcdUnpackValue.startswith("{"):
413 Alignment = 8
414 elif PCD.PcdUnpackValue.startswith("L"):
415 Alignment = 2
416 else:
417 Alignment = 1
418
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)
424 else:
425 EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID, 'The offset value of PCD %s should be %s-byte aligned.' % (PCD.PcdCName, Alignment))
426 else:
427 if PCD.PcdOccupySize % Alignment != 0:
428 PCD.PcdOccupySize = (PCD.PcdOccupySize // Alignment + 1) * Alignment
429
430 PackSize = PCD.PcdOccupySize
431 if PCD._IsBoolean(PCD.PcdValue, PCD.PcdSize):
432 PCD._PackBooleanValue(PCD.PcdValue)
433 self.FileLinesList[count] = PCD
434 count += 1
435 continue
436 #
437 # Try to translate value to an integer firstly.
438 #
439 IsInteger = True
440 PackValue = None
441 try:
442 PackValue = int(PCD.PcdValue)
443 except:
444 try:
445 PackValue = int(PCD.PcdValue, 16)
446 except:
447 IsInteger = False
448
449 if IsInteger:
450 PCD._PackIntValue(PackValue, PackSize)
451 else:
452 PCD._PackPtrValue(PCD.PcdValue, PackSize)
453
454 self.FileLinesList[count] = PCD
455 count += 1
456 else :
457 continue
458
459 ##
460 # This function used to create a clean list only contain useful information and reorganized to make it
461 # easy to be sorted
462 #
463 def FormatFileLine (self) :
464
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)
469 else :
470 # Use pcd's CName as key, and pcd's Size as value
471 self.PcdUnknownOffsetList.append(eachPcd)
472
473
474 ##
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
477 #
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.
482
483 self.PcdFixedOffsetSizeList.sort(key=lambda x: x.PcdBinOffset)
484
485 #
486 # Sort the un-fixed pcd's offset by its size.
487 #
488 self.PcdUnknownOffsetList.sort(key=lambda x: x.PcdBinSize)
489
490 index =0
491 for pcd in self.PcdUnknownOffsetList:
492 index += 1
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]
497
498 #
499 # Process all Offset value are TAB_STAR
500 #
501 if (len(self.PcdFixedOffsetSizeList) == 0) and (len(self.PcdUnknownOffsetList) != 0) :
502 # The offset start from 0
503 NowOffset = 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
510
511 self.PcdFixedOffsetSizeList = self.PcdUnknownOffsetList
512 return
513
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.",
517 None)
518
519 # Judge whether the offset in fixed pcd offset list is overlapped or not.
520 lenOfList = len(self.PcdFixedOffsetSizeList)
521 count = 0
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),
530 None)
531
532 # Overlapped
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),
537 None)
538
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),
544 None)
545 count += 1
546
547 LastOffset = self.PcdFixedOffsetSizeList[0].PcdBinOffset
548 FixOffsetSizeListCount = 0
549 lenOfList = len(self.PcdFixedOffsetSizeList)
550 lenOfUnfixedList = len(self.PcdUnknownOffsetList)
551
552 ##
553 # Insert the un-fixed offset pcd's list into fixed offset pcd's list if has free space between those pcds.
554 #
555 while (FixOffsetSizeListCount < lenOfList) :
556
557 eachFixedPcd = self.PcdFixedOffsetSizeList[FixOffsetSizeListCount]
558 NowOffset = eachFixedPcd.PcdBinOffset
559
560 # Has free space
561 if LastOffset < NowOffset :
562 if lenOfUnfixedList != 0 :
563 countOfUnfixedList = 0
564 while(countOfUnfixedList < lenOfUnfixedList) :
565 eachUnfixedPcd = self.PcdUnknownOffsetList[countOfUnfixedList]
566 needFixPcdSize = eachUnfixedPcd.PcdOccupySize
567 # Not been fixed
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)
578
579 # Delete the item's offset that has been fixed and added into fixed offset list
580 self.PcdUnknownOffsetList.pop(countOfUnfixedList)
581
582 # After item added, should enlarge the length of fixed pcd offset list
583 lenOfList += 1
584 FixOffsetSizeListCount += 1
585
586 # Decrease the un-fixed pcd offset list's length
587 lenOfUnfixedList -= 1
588
589 # Modify the last offset value
590 LastOffset += needFixPcdSize
591 else :
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
595 break
596
597 # Set the FixOffsetSizeListCount = lenOfList for quit the loop
598 else :
599 FixOffsetSizeListCount = lenOfList
600
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.
606 else :
607 EdkLogger.error("BPDG", BuildToolError.ATTRIBUTE_NOT_AVAILABLE,
608 "The offset value definition has overlapped at pcd: %s, its offset is: %s, in file: %s line: %s" % \
609 (eachFixedPcd.PcdCName, eachFixedPcd.PcdOffset, eachFixedPcd.InputFileName, eachFixedPcd.Lineno),
610 None)
611 FixOffsetSizeListCount += 1
612
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]
621
622 NeedFixPcd.PcdBinOffset = LastPcd.PcdBinOffset + LastPcd.PcdOccupySize
623 if NeedFixPcd.PcdBinOffset % NeedFixPcd.Alignment != 0:
624 NeedFixPcd.PcdBinOffset = (NeedFixPcd.PcdBinOffset // NeedFixPcd.Alignment + 1) * NeedFixPcd.Alignment
625
626 NeedFixPcd.PcdOffset = str(hex(NeedFixPcd.PcdBinOffset))
627
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)
632
633 lenOfList += 1
634 lenOfUnfixedList -= 1
635 ##
636 # Write the final data into output files.
637 #
638 def GenerateVpdFile (self, MapFileName, BinFileName):
639 #Open an VPD file to process
640
641 try:
642 fVpdFile = open(BinFileName, "wb")
643 except:
644 # Open failed
645 EdkLogger.error("BPDG", BuildToolError.FILE_OPEN_FAILURE, "File open failed for %s" % self.VpdFileName, None)
646
647 try :
648 fMapFile = open(MapFileName, "w")
649 except:
650 # Open failed
651 EdkLogger.error("BPDG", BuildToolError.FILE_OPEN_FAILURE, "File open failed for %s" % self.MapFileName, None)
652
653 # Use a instance of BytesIO to cache data
654 fStringIO = BytesIO()
655
656 # Write the header of map file.
657 try :
658 fMapFile.write (st.MAP_FILE_COMMENT_TEMPLATE + "\n")
659 except:
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)
661
662 for eachPcd in self.PcdFixedOffsetSizeList :
663 # write map file
664 try :
665 fMapFile.write("%s | %s | %s | %s | %s \n" % (eachPcd.PcdCName, eachPcd.SkuId, eachPcd.PcdOffset, eachPcd.PcdSize, eachPcd.PcdUnpackValue))
666 except:
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)
668
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]))
676 else:
677 fStringIO.write(bytes(Value))
678 else:
679 fStringIO.write (eachPcd.PcdValue)
680
681 try :
682 fVpdFile.write (fStringIO.getvalue())
683 except:
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)
685
686 fStringIO.close ()
687 fVpdFile.close ()
688 fMapFile.close ()
689