# This file include GenVpd class for fix the Vpd type PCD offset, and PcdEntry for describe\r
# and process each entry of vpd type PCD.\r
#\r
-# Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>\r
+# Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>\r
#\r
# This program and the accompanying materials\r
# are licensed and made available under the terms and conditions of the BSD License\r
#\r
\r
import Common.LongFilePathOs as os\r
-import StringIO\r
+from io import BytesIO\r
import StringTable as st\r
import array\r
import re\r
#\r
class PcdEntry:\r
def __init__(self, PcdCName, SkuId,PcdOffset, PcdSize, PcdValue, Lineno=None, FileName=None, PcdUnpackValue=None, \r
- PcdBinOffset=None, PcdBinSize=None):\r
+ PcdBinOffset=None, PcdBinSize=None, Alignment=None):\r
self.PcdCName = PcdCName.strip()\r
self.SkuId = SkuId.strip()\r
self.PcdOffset = PcdOffset.strip()\r
self.PcdUnpackValue = PcdUnpackValue\r
self.PcdBinOffset = PcdBinOffset\r
self.PcdBinSize = PcdBinSize\r
+ self.Alignment = Alignment\r
\r
if self.PcdValue == '' :\r
EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r
\r
self._GenOffsetValue ()\r
\r
- ## Analyze the string value to judge the PCD's datum type euqal to Boolean or not.\r
+ ## Analyze the string value to judge the PCD's datum type equal to Boolean or not.\r
# \r
# @param ValueString PCD's value\r
# @param Size PCD's size\r
# \r
# \r
def _PackIntValue(self, IntValue, Size):\r
- if Size not in _FORMAT_CHAR.keys():\r
+ if Size not in _FORMAT_CHAR:\r
EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r
"Invalid size %d for PCD %s in integer datum size(File: %s Line: %s)." % (Size, self.PcdCName, self.FileName, self.Lineno))\r
\r
## Pack VOID* type VPD PCD's value form string to binary type.\r
#\r
# The VOID* type of string divided into 3 sub-type:\r
- # 1: L"String", Unicode type string.\r
- # 2: "String", Ascii type string.\r
+ # 1: L"String"/L'String', Unicode type string.\r
+ # 2: "String"/'String', Ascii type string.\r
# 3: {bytearray}, only support byte-array.\r
#\r
# @param ValueString The Integer type string for pack.\r
# \r
def _PackPtrValue(self, ValueString, Size):\r
- if ValueString.startswith('L"'):\r
+ if ValueString.startswith('L"') or ValueString.startswith("L'"):\r
self._PackUnicode(ValueString, Size)\r
elif ValueString.startswith('{') and ValueString.endswith('}'):\r
self._PackByteArray(ValueString, Size)\r
- elif ValueString.startswith('"') and ValueString.endswith('"'):\r
+ elif (ValueString.startswith('"') and ValueString.endswith('"')) or (ValueString.startswith("'") and ValueString.endswith("'")):\r
self._PackString(ValueString, Size)\r
else:\r
EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r
\r
## Pack an Ascii PCD value.\r
# \r
- # An Ascii string for a PCD should be in format as "".\r
+ # An Ascii string for a PCD should be in format as ""/''.\r
# \r
def _PackString(self, ValueString, Size):\r
if (Size < 0):\r
"Invalid parameter Size %s of PCD %s!(File: %s Line: %s)" % (self.PcdBinSize, self.PcdCName, self.FileName, self.Lineno))\r
if (ValueString == ""):\r
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))\r
- if (len(ValueString) < 2):\r
- 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))\r
+\r
+ QuotedFlag = True\r
+ if ValueString.startswith("'"):\r
+ QuotedFlag = False\r
\r
ValueString = ValueString[1:-1]\r
- if len(ValueString) + 1 > Size:\r
+ # No null-terminator in 'string' \r
+ if (QuotedFlag and len(ValueString) + 1 > Size) or (not QuotedFlag and len(ValueString) > Size):\r
EdkLogger.error("BPDG", BuildToolError.RESOURCE_OVERFLOW,\r
"PCD value string %s is exceed to size %d(File: %s Line: %s)" % (ValueString, Size, self.FileName, self.Lineno))\r
try:\r
\r
## Pack a unicode PCD value into byte array.\r
# \r
- # A unicode string for a PCD should be in format as L"".\r
+ # A unicode string for a PCD should be in format as L""/L''.\r
#\r
def _PackUnicode(self, UnicodeString, Size):\r
if (Size < 0):\r
EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID, "Invalid parameter Size %s of PCD %s!(File: %s Line: %s)" % \\r
(self.PcdBinSize, self.PcdCName, self.FileName, self.Lineno))\r
- if (len(UnicodeString) < 3):\r
- EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID, "For PCD: %s ,ASCII string %s at least contains two!(File: %s Line: %s)" % \\r
- (self.PcdCName, self.PcdUnpackValue, self.FileName, self.Lineno))\r
\r
+ QuotedFlag = True\r
+ if UnicodeString.startswith("L'"):\r
+ QuotedFlag = False \r
UnicodeString = UnicodeString[2:-1]\r
\r
- if (len(UnicodeString) + 1) * 2 > Size:\r
+ # No null-terminator in L'string'\r
+ if (QuotedFlag and (len(UnicodeString) + 1) * 2 > Size) or (not QuotedFlag and len(UnicodeString) * 2 > Size):\r
EdkLogger.error("BPDG", BuildToolError.RESOURCE_OVERFLOW,\r
"The size of unicode string %s is too larger for size %s(File: %s Line: %s)" % \\r
(UnicodeString, Size, self.FileName, self.Lineno))\r
#\r
# Enhanced for support "|" character in the string.\r
#\r
- ValueList = ['', '', '', '','']\r
+ ValueList = ['', '', '', '', '']\r
\r
ValueRe = re.compile(r'\s*L?\".*\|.*\"\s*$')\r
PtrValue = ValueRe.findall(line)\r
# Delete useless lines\r
while (True) :\r
try :\r
- if (self.FileLinesList[count] == None) :\r
+ if (self.FileLinesList[count] is None) :\r
del(self.FileLinesList[count])\r
else :\r
count += 1\r
# Process the pcds one by one base on the pcd's value and size\r
count = 0\r
for line in self.FileLinesList:\r
- if line != None :\r
- PCD = PcdEntry(line[0], line[1], line[2], line[3], line[4],line[5], self.InputFileName) \r
+ if line is not None :\r
+ PCD = PcdEntry(line[0], line[1], line[2], line[3], line[4], line[5], self.InputFileName)\r
# Strip the space char\r
PCD.PcdCName = PCD.PcdCName.strip(' ')\r
PCD.SkuId = PCD.SkuId.strip(' ')\r
#\r
PCD.PcdUnpackValue = str(PCD.PcdValue)\r
\r
- #\r
- # If value is Unicode string (e.g. L""), then use 2-byte alignment\r
- # If value is byte array (e.g. {}), then use 8-byte alignment\r
- #\r
- PCD.PcdOccupySize = int(PCD.PcdSize)\r
- if PCD.PcdUnpackValue.startswith("{"):\r
- Alignment = 8\r
- elif PCD.PcdUnpackValue.startswith("L"):\r
- Alignment = 2\r
- else:\r
- Alignment = 1\r
- if PCD.PcdOccupySize % Alignment != 0:\r
- PCD.PcdOccupySize = (PCD.PcdOccupySize / Alignment + 1) * Alignment\r
-\r
#\r
# Translate PCD size string to an integer value.\r
PackSize = None\r
except:\r
EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID, "Invalid PCD size value %s at file: %s line: %s" % (PCD.PcdSize, self.InputFileName, PCD.Lineno))\r
\r
+ #\r
+ # If value is Unicode string (e.g. L""), then use 2-byte alignment\r
+ # If value is byte array (e.g. {}), then use 8-byte alignment\r
+ #\r
+ PCD.PcdOccupySize = PCD.PcdBinSize\r
+ if PCD.PcdUnpackValue.startswith("{"):\r
+ Alignment = 8\r
+ elif PCD.PcdUnpackValue.startswith("L"):\r
+ Alignment = 2\r
+ else:\r
+ Alignment = 1\r
+\r
+ PCD.Alignment = Alignment\r
+ if PCD.PcdOffset != '*':\r
+ if PCD.PcdOccupySize % Alignment != 0:\r
+ if PCD.PcdUnpackValue.startswith("{"):\r
+ EdkLogger.warn("BPDG", "The offset value of PCD %s is not 8-byte aligned!" %(PCD.PcdCName), File=self.InputFileName)\r
+ else:\r
+ EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID, 'The offset value of PCD %s should be %s-byte aligned.' % (PCD.PcdCName, Alignment))\r
+ else:\r
+ if PCD.PcdOccupySize % Alignment != 0:\r
+ PCD.PcdOccupySize = (PCD.PcdOccupySize / Alignment + 1) * Alignment\r
+\r
+ PackSize = PCD.PcdOccupySize\r
if PCD._IsBoolean(PCD.PcdValue, PCD.PcdSize):\r
PCD._PackBooleanValue(PCD.PcdValue)\r
self.FileLinesList[count] = PCD\r
#\r
self.PcdUnknownOffsetList.sort(lambda x, y: cmp(x.PcdBinSize, y.PcdBinSize))\r
\r
+ index =0\r
+ for pcd in self.PcdUnknownOffsetList:\r
+ index += 1\r
+ if pcd.PcdCName == ".".join(("gEfiMdeModulePkgTokenSpaceGuid", "PcdNvStoreDefaultValueBuffer")):\r
+ if index != len(self.PcdUnknownOffsetList):\r
+ for i in range(len(self.PcdUnknownOffsetList) - index):\r
+ self.PcdUnknownOffsetList[index+i -1 ], self.PcdUnknownOffsetList[index+i] = self.PcdUnknownOffsetList[index+i], self.PcdUnknownOffsetList[index+i -1]\r
+\r
#\r
# Process all Offset value are "*"\r
#\r
# The offset start from 0\r
NowOffset = 0\r
for Pcd in self.PcdUnknownOffsetList :\r
+ if NowOffset % Pcd.Alignment != 0:\r
+ NowOffset = (NowOffset/ Pcd.Alignment + 1) * Pcd.Alignment\r
Pcd.PcdBinOffset = NowOffset\r
Pcd.PcdOffset = str(hex(Pcd.PcdBinOffset))\r
NowOffset += Pcd.PcdOccupySize\r
needFixPcdSize = eachUnfixedPcd.PcdOccupySize\r
# Not been fixed\r
if eachUnfixedPcd.PcdOffset == '*' :\r
+ if LastOffset % eachUnfixedPcd.Alignment != 0:\r
+ LastOffset = (LastOffset / eachUnfixedPcd.Alignment + 1) * eachUnfixedPcd.Alignment\r
# The offset un-fixed pcd can write into this free space\r
if needFixPcdSize <= (NowOffset - LastOffset) :\r
# Change the offset value of un-fixed pcd\r
eachUnfixedPcd.PcdOffset = str(hex(LastOffset))\r
eachUnfixedPcd.PcdBinOffset = LastOffset\r
# Insert this pcd into fixed offset pcd list.\r
- self.PcdFixedOffsetSizeList.insert(FixOffsetSizeListCount,eachUnfixedPcd)\r
+ self.PcdFixedOffsetSizeList.insert(FixOffsetSizeListCount, eachUnfixedPcd)\r
\r
# Delete the item's offset that has been fixed and added into fixed offset list\r
self.PcdUnknownOffsetList.pop(countOfUnfixedList)\r
NeedFixPcd = self.PcdUnknownOffsetList[0]\r
\r
NeedFixPcd.PcdBinOffset = LastPcd.PcdBinOffset + LastPcd.PcdOccupySize\r
+ if NeedFixPcd.PcdBinOffset % NeedFixPcd.Alignment != 0:\r
+ NeedFixPcd.PcdBinOffset = (NeedFixPcd.PcdBinOffset / NeedFixPcd.Alignment + 1) * NeedFixPcd.Alignment\r
+\r
NeedFixPcd.PcdOffset = str(hex(NeedFixPcd.PcdBinOffset))\r
\r
# Insert this pcd into fixed offset pcd list's tail.\r
# Open failed\r
EdkLogger.error("BPDG", BuildToolError.FILE_OPEN_FAILURE, "File open failed for %s" % self.MapFileName, None)\r
\r
- # Use a instance of StringIO to cache data\r
- fStringIO = StringIO.StringIO('')\r
+ # Use a instance of BytesIO to cache data\r
+ fStringIO = BytesIO('')\r
\r
# Write the header of map file.\r
try :\r
for eachPcd in self.PcdFixedOffsetSizeList :\r
# write map file\r
try :\r
- fMapFile.write("%s | %s | %s | %s | %s \n" % (eachPcd.PcdCName, eachPcd.SkuId,eachPcd.PcdOffset, eachPcd.PcdSize,eachPcd.PcdUnpackValue))\r
+ fMapFile.write("%s | %s | %s | %s | %s \n" % (eachPcd.PcdCName, eachPcd.SkuId, eachPcd.PcdOffset, eachPcd.PcdSize, eachPcd.PcdUnpackValue))\r
except:\r
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)\r
\r