]>
Commit | Line | Data |
---|---|---|
111be80f | 1 | ## @file\r |
2 | # This file include GenVpd class for fix the Vpd type PCD offset, and PcdEntry for describe\r | |
3 | # and process each entry of vpd type PCD.\r | |
4 | #\r | |
86737681 | 5 | # Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>\r |
111be80f | 6 | #\r |
7 | # This program and the accompanying materials\r | |
8 | # are licensed and made available under the terms and conditions of the BSD License\r | |
9 | # which accompanies this distribution. The full text of the license may be found at\r | |
10 | # http://opensource.org/licenses/bsd-license.php\r | |
11 | #\r | |
12 | # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
13 | # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
14 | #\r | |
15 | \r | |
1ccc4d89 | 16 | from __future__ import absolute_import\r |
1be2ed90 | 17 | import Common.LongFilePathOs as os\r |
86379ac4 | 18 | from io import BytesIO\r |
72a836c0 | 19 | from . import StringTable as st\r |
111be80f | 20 | import array\r |
6780eef1 | 21 | import re\r |
1be2ed90 | 22 | from Common.LongFilePathSupport import OpenLongFilePath as open\r |
111be80f | 23 | from struct import *\r |
bc39c5cb | 24 | from Common.DataType import MAX_SIZE_TYPE, MAX_VAL_TYPE, TAB_STAR\r |
111be80f | 25 | import Common.EdkLogger as EdkLogger\r |
26 | import Common.BuildToolError as BuildToolError\r | |
27 | \r | |
28 | _FORMAT_CHAR = {1: 'B',\r | |
29 | 2: 'H',\r | |
30 | 4: 'I',\r | |
31 | 8: 'Q'\r | |
32 | }\r | |
33 | \r | |
08dd311f LG |
34 | ## The VPD PCD data structure for store and process each VPD PCD entry.\r |
35 | #\r | |
f7496d71 | 36 | # This class contain method to format and pack pcd's value.\r |
08dd311f | 37 | #\r |
111be80f | 38 | class PcdEntry:\r |
f7496d71 | 39 | def __init__(self, PcdCName, SkuId,PcdOffset, PcdSize, PcdValue, Lineno=None, FileName=None, PcdUnpackValue=None,\r |
86737681 | 40 | PcdBinOffset=None, PcdBinSize=None, Alignment=None):\r |
111be80f | 41 | self.PcdCName = PcdCName.strip()\r |
e8a47801 | 42 | self.SkuId = SkuId.strip()\r |
111be80f | 43 | self.PcdOffset = PcdOffset.strip()\r |
44 | self.PcdSize = PcdSize.strip()\r | |
45 | self.PcdValue = PcdValue.strip()\r | |
46 | self.Lineno = Lineno.strip()\r | |
47 | self.FileName = FileName.strip()\r | |
48 | self.PcdUnpackValue = PcdUnpackValue\r | |
49 | self.PcdBinOffset = PcdBinOffset\r | |
50 | self.PcdBinSize = PcdBinSize\r | |
86737681 | 51 | self.Alignment = Alignment\r |
f7496d71 | 52 | \r |
111be80f | 53 | if self.PcdValue == '' :\r |
47fea6af YZ |
54 | EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r |
55 | "Invalid PCD format(Name: %s File: %s line: %s) , no Value specified!" % (self.PcdCName, self.FileName, self.Lineno))\r | |
56 | \r | |
111be80f | 57 | if self.PcdOffset == '' :\r |
47fea6af YZ |
58 | EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r |
59 | "Invalid PCD format(Name: %s File: %s Line: %s) , no Offset specified!" % (self.PcdCName, self.FileName, self.Lineno))\r | |
60 | \r | |
111be80f | 61 | if self.PcdSize == '' :\r |
47fea6af YZ |
62 | EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r |
63 | "Invalid PCD format(Name: %s File: %s Line: %s), no PcdSize specified!" % (self.PcdCName, self.FileName, self.Lineno))\r | |
64 | \r | |
111be80f | 65 | self._GenOffsetValue ()\r |
47fea6af | 66 | \r |
d5988a8a | 67 | ## Analyze the string value to judge the PCD's datum type equal to Boolean or not.\r |
f7496d71 | 68 | #\r |
08dd311f LG |
69 | # @param ValueString PCD's value\r |
70 | # @param Size PCD's size\r | |
f7496d71 | 71 | #\r |
08dd311f | 72 | # @retval True PCD's datum type is Boolean\r |
f7496d71 LG |
73 | # @retval False PCD's datum type is not Boolean.\r |
74 | #\r | |
08dd311f LG |
75 | def _IsBoolean(self, ValueString, Size):\r |
76 | if (Size == "1"):\r | |
77 | if ValueString.upper() in ["TRUE", "FALSE"]:\r | |
78 | return True\r | |
79 | elif ValueString in ["0", "1", "0x0", "0x1", "0x00", "0x01"]:\r | |
47fea6af YZ |
80 | return True\r |
81 | \r | |
111be80f | 82 | return False\r |
47fea6af | 83 | \r |
08dd311f | 84 | ## Convert the PCD's value from string to integer.\r |
f7496d71 | 85 | #\r |
08dd311f LG |
86 | # This function will try to convert the Offset value form string to integer\r |
87 | # for both hexadecimal and decimal.\r | |
f7496d71 | 88 | #\r |
111be80f | 89 | def _GenOffsetValue(self):\r |
bc39c5cb | 90 | if self.PcdOffset != TAB_STAR:\r |
111be80f | 91 | try:\r |
92 | self.PcdBinOffset = int (self.PcdOffset)\r | |
93 | except:\r | |
94 | try:\r | |
95 | self.PcdBinOffset = int(self.PcdOffset, 16)\r | |
96 | except:\r | |
47fea6af YZ |
97 | EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r |
98 | "Invalid offset value %s for PCD %s (File: %s Line: %s)" % (self.PcdOffset, self.PcdCName, self.FileName, self.Lineno))\r | |
99 | \r | |
08dd311f | 100 | ## Pack Boolean type VPD PCD's value form string to binary type.\r |
f7496d71 | 101 | #\r |
08dd311f | 102 | # @param ValueString The boolean type string for pack.\r |
f7496d71 LG |
103 | #\r |
104 | #\r | |
111be80f | 105 | def _PackBooleanValue(self, ValueString):\r |
08dd311f | 106 | if ValueString.upper() == "TRUE" or ValueString in ["1", "0x1", "0x01"]:\r |
47fea6af YZ |
107 | try:\r |
108 | self.PcdValue = pack(_FORMAT_CHAR[1], 1)\r | |
111be80f | 109 | except:\r |
47fea6af YZ |
110 | EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r |
111 | "Invalid size or value for PCD %s to pack(File: %s Line: %s)." % (self.PcdCName, self.FileName, self.Lineno))\r | |
111be80f | 112 | else:\r |
113 | try:\r | |
47fea6af | 114 | self.PcdValue = pack(_FORMAT_CHAR[1], 0)\r |
111be80f | 115 | except:\r |
47fea6af YZ |
116 | EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r |
117 | "Invalid size or value for PCD %s to pack(File: %s Line: %s)." % (self.PcdCName, self.FileName, self.Lineno))\r | |
118 | \r | |
08dd311f | 119 | ## Pack Integer type VPD PCD's value form string to binary type.\r |
f7496d71 | 120 | #\r |
08dd311f | 121 | # @param ValueString The Integer type string for pack.\r |
f7496d71 LG |
122 | #\r |
123 | #\r | |
111be80f | 124 | def _PackIntValue(self, IntValue, Size):\r |
9eb87141 | 125 | if Size not in _FORMAT_CHAR:\r |
47fea6af YZ |
126 | EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r |
127 | "Invalid size %d for PCD %s in integer datum size(File: %s Line: %s)." % (Size, self.PcdCName, self.FileName, self.Lineno))\r | |
128 | \r | |
c526dcd4 | 129 | for Type, MaxSize in MAX_SIZE_TYPE.items():\r |
130 | if Type == 'BOOLEAN':\r | |
131 | continue\r | |
132 | if Size == MaxSize:\r | |
133 | if IntValue < 0:\r | |
134 | EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r | |
135 | "PCD can't be set to negative value %d for PCD %s in %s datum type(File: %s Line: %s)." % (\r | |
136 | IntValue, self.PcdCName, Type, self.FileName, self.Lineno))\r | |
137 | elif IntValue > MAX_VAL_TYPE[Type]:\r | |
138 | EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r | |
139 | "Too large PCD value %d for datum type %s for PCD %s(File: %s Line: %s)." % (\r | |
140 | IntValue, Type, self.PcdCName, self.FileName, self.Lineno))\r | |
47fea6af | 141 | \r |
111be80f | 142 | try:\r |
47fea6af | 143 | self.PcdValue = pack(_FORMAT_CHAR[Size], IntValue)\r |
111be80f | 144 | except:\r |
47fea6af YZ |
145 | EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r |
146 | "Invalid size or value for PCD %s to pack(File: %s Line: %s)." % (self.PcdCName, self.FileName, self.Lineno))\r | |
08dd311f LG |
147 | \r |
148 | ## Pack VOID* type VPD PCD's value form string to binary type.\r | |
149 | #\r | |
150 | # The VOID* type of string divided into 3 sub-type:\r | |
d5988a8a YZ |
151 | # 1: L"String"/L'String', Unicode type string.\r |
152 | # 2: "String"/'String', Ascii type string.\r | |
08dd311f LG |
153 | # 3: {bytearray}, only support byte-array.\r |
154 | #\r | |
155 | # @param ValueString The Integer type string for pack.\r | |
f7496d71 | 156 | #\r |
111be80f | 157 | def _PackPtrValue(self, ValueString, Size):\r |
d5988a8a | 158 | if ValueString.startswith('L"') or ValueString.startswith("L'"):\r |
111be80f | 159 | self._PackUnicode(ValueString, Size)\r |
160 | elif ValueString.startswith('{') and ValueString.endswith('}'):\r | |
161 | self._PackByteArray(ValueString, Size)\r | |
d5988a8a | 162 | elif (ValueString.startswith('"') and ValueString.endswith('"')) or (ValueString.startswith("'") and ValueString.endswith("'")):\r |
111be80f | 163 | self._PackString(ValueString, Size)\r |
164 | else:\r | |
47fea6af YZ |
165 | EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r |
166 | "Invalid VOID* type PCD %s value %s (File: %s Line: %s)" % (self.PcdCName, ValueString, self.FileName, self.Lineno))\r | |
167 | \r | |
08dd311f | 168 | ## Pack an Ascii PCD value.\r |
f7496d71 | 169 | #\r |
d5988a8a | 170 | # An Ascii string for a PCD should be in format as ""/''.\r |
f7496d71 | 171 | #\r |
111be80f | 172 | def _PackString(self, ValueString, Size):\r |
173 | if (Size < 0):\r | |
47fea6af | 174 | EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r |
111be80f | 175 | "Invalid parameter Size %s of PCD %s!(File: %s Line: %s)" % (self.PcdBinSize, self.PcdCName, self.FileName, self.Lineno))\r |
176 | if (ValueString == ""):\r | |
47fea6af | 177 | 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 |
d5988a8a YZ |
178 | \r |
179 | QuotedFlag = True\r | |
180 | if ValueString.startswith("'"):\r | |
181 | QuotedFlag = False\r | |
47fea6af | 182 | \r |
111be80f | 183 | ValueString = ValueString[1:-1]\r |
f7496d71 | 184 | # No null-terminator in 'string'\r |
d5988a8a | 185 | if (QuotedFlag and len(ValueString) + 1 > Size) or (not QuotedFlag and len(ValueString) > Size):\r |
47fea6af | 186 | EdkLogger.error("BPDG", BuildToolError.RESOURCE_OVERFLOW,\r |
111be80f | 187 | "PCD value string %s is exceed to size %d(File: %s Line: %s)" % (ValueString, Size, self.FileName, self.Lineno))\r |
188 | try:\r | |
1ccc4d89 | 189 | self.PcdValue = pack('%ds' % Size, ValueString)\r |
111be80f | 190 | except:\r |
47fea6af YZ |
191 | EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r |
192 | "Invalid size or value for PCD %s to pack(File: %s Line: %s)." % (self.PcdCName, self.FileName, self.Lineno))\r | |
193 | \r | |
08dd311f | 194 | ## Pack a byte-array PCD value.\r |
f7496d71 | 195 | #\r |
08dd311f | 196 | # A byte-array for a PCD should be in format as {0x01, 0x02, ...}.\r |
f7496d71 | 197 | #\r |
111be80f | 198 | def _PackByteArray(self, ValueString, Size):\r |
47fea6af | 199 | if (Size < 0):\r |
111be80f | 200 | 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))\r |
201 | if (ValueString == ""):\r | |
47fea6af YZ |
202 | 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 |
203 | \r | |
111be80f | 204 | ValueString = ValueString.strip()\r |
205 | ValueString = ValueString.lstrip('{').strip('}')\r | |
206 | ValueList = ValueString.split(',')\r | |
207 | ValueList = [item.strip() for item in ValueList]\r | |
47fea6af | 208 | \r |
111be80f | 209 | if len(ValueList) > Size:\r |
47fea6af | 210 | EdkLogger.error("BPDG", BuildToolError.RESOURCE_OVERFLOW,\r |
111be80f | 211 | "The byte array %s is too large for size %d(File: %s Line: %s)" % (ValueString, Size, self.FileName, self.Lineno))\r |
47fea6af | 212 | \r |
111be80f | 213 | ReturnArray = array.array('B')\r |
47fea6af | 214 | \r |
e77e59c9 | 215 | for Index in range(len(ValueList)):\r |
111be80f | 216 | Value = None\r |
25918452 | 217 | if ValueList[Index].lower().startswith('0x'):\r |
111be80f | 218 | # translate hex value\r |
219 | try:\r | |
220 | Value = int(ValueList[Index], 16)\r | |
221 | except:\r | |
47fea6af | 222 | EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r |
111be80f | 223 | "The value item %s in byte array %s is an invalid HEX value.(File: %s Line: %s)" % \\r |
224 | (ValueList[Index], ValueString, self.FileName, self.Lineno))\r | |
225 | else:\r | |
226 | # translate decimal value\r | |
227 | try:\r | |
228 | Value = int(ValueList[Index], 10)\r | |
229 | except:\r | |
230 | EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r | |
231 | "The value item %s in byte array %s is an invalid DECIMAL value.(File: %s Line: %s)" % \\r | |
232 | (ValueList[Index], ValueString, self.FileName, self.Lineno))\r | |
47fea6af | 233 | \r |
111be80f | 234 | if Value > 255:\r |
47fea6af YZ |
235 | EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r |
236 | "The value item %s in byte array %s do not in range 0 ~ 0xFF(File: %s Line: %s)" % \\r | |
111be80f | 237 | (ValueList[Index], ValueString, self.FileName, self.Lineno))\r |
47fea6af | 238 | \r |
111be80f | 239 | ReturnArray.append(Value)\r |
47fea6af | 240 | \r |
e77e59c9 | 241 | for Index in range(len(ValueList), Size):\r |
111be80f | 242 | ReturnArray.append(0)\r |
47fea6af YZ |
243 | \r |
244 | self.PcdValue = ReturnArray.tolist()\r | |
111be80f | 245 | \r |
246 | ## Pack a unicode PCD value into byte array.\r | |
f7496d71 | 247 | #\r |
d5988a8a | 248 | # A unicode string for a PCD should be in format as L""/L''.\r |
111be80f | 249 | #\r |
250 | def _PackUnicode(self, UnicodeString, Size):\r | |
47fea6af YZ |
251 | if (Size < 0):\r |
252 | EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID, "Invalid parameter Size %s of PCD %s!(File: %s Line: %s)" % \\r | |
111be80f | 253 | (self.PcdBinSize, self.PcdCName, self.FileName, self.Lineno))\r |
47fea6af | 254 | \r |
d5988a8a YZ |
255 | QuotedFlag = True\r |
256 | if UnicodeString.startswith("L'"):\r | |
f7496d71 | 257 | QuotedFlag = False\r |
111be80f | 258 | UnicodeString = UnicodeString[2:-1]\r |
47fea6af | 259 | \r |
d5988a8a YZ |
260 | # No null-terminator in L'string'\r |
261 | if (QuotedFlag and (len(UnicodeString) + 1) * 2 > Size) or (not QuotedFlag and len(UnicodeString) * 2 > Size):\r | |
111be80f | 262 | EdkLogger.error("BPDG", BuildToolError.RESOURCE_OVERFLOW,\r |
263 | "The size of unicode string %s is too larger for size %s(File: %s Line: %s)" % \\r | |
264 | (UnicodeString, Size, self.FileName, self.Lineno))\r | |
47fea6af | 265 | \r |
111be80f | 266 | ReturnArray = array.array('B')\r |
267 | for Value in UnicodeString:\r | |
268 | try:\r | |
269 | ReturnArray.append(ord(Value))\r | |
270 | ReturnArray.append(0)\r | |
271 | except:\r | |
47fea6af | 272 | EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID,\r |
111be80f | 273 | "Invalid unicode character %s in unicode string %s(File: %s Line: %s)" % \\r |
274 | (Value, UnicodeString, self.FileName, self.Lineno))\r | |
47fea6af | 275 | \r |
e77e59c9 | 276 | for Index in range(len(UnicodeString) * 2, Size):\r |
111be80f | 277 | ReturnArray.append(0)\r |
47fea6af YZ |
278 | \r |
279 | self.PcdValue = ReturnArray.tolist()\r | |
08dd311f LG |
280 | \r |
281 | \r | |
282 | \r | |
283 | ## The class implementing the BPDG VPD PCD offset fix process\r | |
284 | #\r | |
285 | # The VPD PCD offset fix process includes:\r | |
286 | # 1. Parse the input guided.txt file and store it in the data structure;\r | |
287 | # 2. Format the input file data to remove unused lines;\r | |
288 | # 3. Fixed offset if needed;\r | |
289 | # 4. Generate output file, including guided.map and guided.bin file;\r | |
f7496d71 | 290 | #\r |
47fea6af | 291 | class GenVPD :\r |
111be80f | 292 | ## Constructor of DscBuildData\r |
293 | #\r | |
294 | # Initialize object of GenVPD\r | |
295 | # @Param InputFileName The filename include the vpd type pcd information\r | |
296 | # @param MapFileName The filename of map file that stores vpd type pcd information.\r | |
297 | # This file will be generated by the BPDG tool after fix the offset\r | |
298 | # and adjust the offset to make the pcd data aligned.\r | |
299 | # @param VpdFileName The filename of Vpd file that hold vpd pcd information.\r | |
300 | #\r | |
301 | def __init__(self, InputFileName, MapFileName, VpdFileName):\r | |
302 | self.InputFileName = InputFileName\r | |
303 | self.MapFileName = MapFileName\r | |
304 | self.VpdFileName = VpdFileName\r | |
305 | self.FileLinesList = []\r | |
306 | self.PcdFixedOffsetSizeList = []\r | |
307 | self.PcdUnknownOffsetList = []\r | |
308 | try:\r | |
1ccc4d89 | 309 | fInputfile = open(InputFileName, "r", 0)\r |
111be80f | 310 | try:\r |
311 | self.FileLinesList = fInputfile.readlines()\r | |
312 | except:\r | |
47fea6af | 313 | EdkLogger.error("BPDG", BuildToolError.FILE_READ_FAILURE, "File read failed for %s" % InputFileName, None)\r |
111be80f | 314 | finally:\r |
315 | fInputfile.close()\r | |
316 | except:\r | |
47fea6af YZ |
317 | EdkLogger.error("BPDG", BuildToolError.FILE_OPEN_FAILURE, "File open failed for %s" % InputFileName, None)\r |
318 | \r | |
111be80f | 319 | ##\r |
f7496d71 | 320 | # Parser the input file which is generated by the build tool. Convert the value of each pcd's\r |
111be80f | 321 | # from string to it's real format. Also remove the useless line in the input file.\r |
f7496d71 | 322 | #\r |
111be80f | 323 | def ParserInputFile (self):\r |
47fea6af | 324 | count = 0\r |
111be80f | 325 | for line in self.FileLinesList:\r |
326 | # Strip "\r\n" generated by readlines ().\r | |
327 | line = line.strip()\r | |
328 | line = line.rstrip(os.linesep)\r | |
47fea6af | 329 | \r |
111be80f | 330 | # Skip the comment line\r |
6780eef1 LG |
331 | if (not line.startswith("#")) and len(line) > 1 :\r |
332 | #\r | |
333 | # Enhanced for support "|" character in the string.\r | |
334 | #\r | |
ccaa7754 | 335 | ValueList = ['', '', '', '', '']\r |
47fea6af YZ |
336 | \r |
337 | ValueRe = re.compile(r'\s*L?\".*\|.*\"\s*$')\r | |
6780eef1 | 338 | PtrValue = ValueRe.findall(line)\r |
47fea6af | 339 | \r |
6780eef1 | 340 | ValueUpdateFlag = False\r |
47fea6af | 341 | \r |
6780eef1 LG |
342 | if len(PtrValue) >= 1:\r |
343 | line = re.sub(ValueRe, '', line)\r | |
47fea6af YZ |
344 | ValueUpdateFlag = True\r |
345 | \r | |
6780eef1 LG |
346 | TokenList = line.split('|')\r |
347 | ValueList[0:len(TokenList)] = TokenList\r | |
47fea6af | 348 | \r |
6780eef1 | 349 | if ValueUpdateFlag:\r |
22061fab | 350 | ValueList[4] = PtrValue[0]\r |
6780eef1 | 351 | self.FileLinesList[count] = ValueList\r |
111be80f | 352 | # Store the line number\r |
47fea6af | 353 | self.FileLinesList[count].append(str(count + 1))\r |
111be80f | 354 | elif len(line) <= 1 :\r |
355 | # Set the blank line to "None"\r | |
356 | self.FileLinesList[count] = None\r | |
357 | else :\r | |
358 | # Set the comment line to "None"\r | |
359 | self.FileLinesList[count] = None\r | |
360 | count += 1\r | |
47fea6af | 361 | \r |
111be80f | 362 | # The line count contain usage information\r |
47fea6af | 363 | count = 0\r |
111be80f | 364 | # Delete useless lines\r |
365 | while (True) :\r | |
366 | try :\r | |
4231a819 | 367 | if (self.FileLinesList[count] is None) :\r |
111be80f | 368 | del(self.FileLinesList[count])\r |
369 | else :\r | |
370 | count += 1\r | |
371 | except :\r | |
47fea6af | 372 | break\r |
111be80f | 373 | #\r |
374 | # After remove the useless line, if there are no data remain in the file line list,\r | |
375 | # Report warning messages to user's.\r | |
f7496d71 | 376 | #\r |
111be80f | 377 | if len(self.FileLinesList) == 0 :\r |
47fea6af | 378 | EdkLogger.warn('BPDG', BuildToolError.RESOURCE_NOT_AVAILABLE,\r |
111be80f | 379 | "There are no VPD type pcds defined in DSC file, Please check it.")\r |
47fea6af | 380 | \r |
111be80f | 381 | # Process the pcds one by one base on the pcd's value and size\r |
382 | count = 0\r | |
47fea6af | 383 | for line in self.FileLinesList:\r |
4231a819 | 384 | if line is not None :\r |
ccaa7754 | 385 | PCD = PcdEntry(line[0], line[1], line[2], line[3], line[4], line[5], self.InputFileName)\r |
111be80f | 386 | # Strip the space char\r |
387 | PCD.PcdCName = PCD.PcdCName.strip(' ')\r | |
e8a47801 | 388 | PCD.SkuId = PCD.SkuId.strip(' ')\r |
111be80f | 389 | PCD.PcdOffset = PCD.PcdOffset.strip(' ')\r |
390 | PCD.PcdSize = PCD.PcdSize.strip(' ')\r | |
22061fab | 391 | PCD.PcdValue = PCD.PcdValue.strip(' ')\r |
111be80f | 392 | PCD.Lineno = PCD.Lineno.strip(' ')\r |
22061fab | 393 | \r |
111be80f | 394 | #\r |
395 | # Store the original pcd value.\r | |
396 | # This information will be useful while generate the output map file.\r | |
397 | #\r | |
22061fab YZ |
398 | PCD.PcdUnpackValue = str(PCD.PcdValue)\r |
399 | \r | |
53c13295 YZ |
400 | #\r |
401 | # Translate PCD size string to an integer value.\r | |
402 | PackSize = None\r | |
403 | try:\r | |
404 | PackSize = int(PCD.PcdSize, 10)\r | |
405 | PCD.PcdBinSize = PackSize\r | |
406 | except:\r | |
407 | try:\r | |
408 | PackSize = int(PCD.PcdSize, 16)\r | |
409 | PCD.PcdBinSize = PackSize\r | |
410 | except:\r | |
411 | EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID, "Invalid PCD size value %s at file: %s line: %s" % (PCD.PcdSize, self.InputFileName, PCD.Lineno))\r | |
412 | \r | |
22061fab YZ |
413 | #\r |
414 | # If value is Unicode string (e.g. L""), then use 2-byte alignment\r | |
415 | # If value is byte array (e.g. {}), then use 8-byte alignment\r | |
416 | #\r | |
53c13295 | 417 | PCD.PcdOccupySize = PCD.PcdBinSize\r |
22061fab YZ |
418 | if PCD.PcdUnpackValue.startswith("{"):\r |
419 | Alignment = 8\r | |
420 | elif PCD.PcdUnpackValue.startswith("L"):\r | |
421 | Alignment = 2\r | |
422 | else:\r | |
423 | Alignment = 1\r | |
815ada26 | 424 | \r |
86737681 | 425 | PCD.Alignment = Alignment\r |
bc39c5cb | 426 | if PCD.PcdOffset != TAB_STAR:\r |
815ada26 YZ |
427 | if PCD.PcdOccupySize % Alignment != 0:\r |
428 | if PCD.PcdUnpackValue.startswith("{"):\r | |
429 | EdkLogger.warn("BPDG", "The offset value of PCD %s is not 8-byte aligned!" %(PCD.PcdCName), File=self.InputFileName)\r | |
430 | else:\r | |
431 | EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID, 'The offset value of PCD %s should be %s-byte aligned.' % (PCD.PcdCName, Alignment))\r | |
432 | else:\r | |
433 | if PCD.PcdOccupySize % Alignment != 0:\r | |
1ccc4d89 | 434 | PCD.PcdOccupySize = (PCD.PcdOccupySize / Alignment + 1) * Alignment\r |
111be80f | 435 | \r |
86737681 | 436 | PackSize = PCD.PcdOccupySize\r |
08dd311f | 437 | if PCD._IsBoolean(PCD.PcdValue, PCD.PcdSize):\r |
111be80f | 438 | PCD._PackBooleanValue(PCD.PcdValue)\r |
439 | self.FileLinesList[count] = PCD\r | |
440 | count += 1\r | |
441 | continue\r | |
442 | #\r | |
443 | # Try to translate value to an integer firstly.\r | |
444 | #\r | |
445 | IsInteger = True\r | |
47fea6af | 446 | PackValue = None\r |
111be80f | 447 | try:\r |
448 | PackValue = int(PCD.PcdValue)\r | |
449 | except:\r | |
450 | try:\r | |
451 | PackValue = int(PCD.PcdValue, 16)\r | |
452 | except:\r | |
453 | IsInteger = False\r | |
47fea6af | 454 | \r |
111be80f | 455 | if IsInteger:\r |
456 | PCD._PackIntValue(PackValue, PackSize)\r | |
457 | else:\r | |
458 | PCD._PackPtrValue(PCD.PcdValue, PackSize)\r | |
47fea6af | 459 | \r |
111be80f | 460 | self.FileLinesList[count] = PCD\r |
461 | count += 1\r | |
462 | else :\r | |
463 | continue\r | |
47fea6af | 464 | \r |
111be80f | 465 | ##\r |
f7496d71 | 466 | # This function used to create a clean list only contain useful information and reorganized to make it\r |
111be80f | 467 | # easy to be sorted\r |
468 | #\r | |
469 | def FormatFileLine (self) :\r | |
47fea6af | 470 | \r |
111be80f | 471 | for eachPcd in self.FileLinesList :\r |
bc39c5cb | 472 | if eachPcd.PcdOffset != TAB_STAR :\r |
f7496d71 | 473 | # Use pcd's Offset value as key, and pcd's Value as value\r |
111be80f | 474 | self.PcdFixedOffsetSizeList.append(eachPcd)\r |
475 | else :\r | |
476 | # Use pcd's CName as key, and pcd's Size as value\r | |
477 | self.PcdUnknownOffsetList.append(eachPcd)\r | |
47fea6af YZ |
478 | \r |
479 | \r | |
111be80f | 480 | ##\r |
481 | # This function is use to fix the offset value which the not specified in the map file.\r | |
482 | # Usually it use the star (meaning any offset) character in the offset field\r | |
f7496d71 | 483 | #\r |
47fea6af | 484 | def FixVpdOffset (self):\r |
111be80f | 485 | # At first, the offset should start at 0\r |
486 | # Sort fixed offset list in order to find out where has free spaces for the pcd's offset\r | |
bc39c5cb | 487 | # value is TAB_STAR to insert into.\r |
47fea6af | 488 | \r |
87010d3d | 489 | self.PcdFixedOffsetSizeList.sort(key=lambda x: x.PcdBinOffset)\r |
47fea6af | 490 | \r |
111be80f | 491 | #\r |
492 | # Sort the un-fixed pcd's offset by it's size.\r | |
493 | #\r | |
87010d3d | 494 | self.PcdUnknownOffsetList.sort(key=lambda x: x.PcdBinSize)\r |
47fea6af | 495 | \r |
0b6c5954 LG |
496 | index =0\r |
497 | for pcd in self.PcdUnknownOffsetList:\r | |
498 | index += 1\r | |
ccaa7754 | 499 | if pcd.PcdCName == ".".join(("gEfiMdeModulePkgTokenSpaceGuid", "PcdNvStoreDefaultValueBuffer")):\r |
0b6c5954 LG |
500 | if index != len(self.PcdUnknownOffsetList):\r |
501 | for i in range(len(self.PcdUnknownOffsetList) - index):\r | |
ccaa7754 | 502 | self.PcdUnknownOffsetList[index+i -1 ], self.PcdUnknownOffsetList[index+i] = self.PcdUnknownOffsetList[index+i], self.PcdUnknownOffsetList[index+i -1]\r |
0b6c5954 | 503 | \r |
111be80f | 504 | #\r |
bc39c5cb | 505 | # Process all Offset value are TAB_STAR\r |
111be80f | 506 | #\r |
507 | if (len(self.PcdFixedOffsetSizeList) == 0) and (len(self.PcdUnknownOffsetList) != 0) :\r | |
508 | # The offset start from 0\r | |
509 | NowOffset = 0\r | |
47fea6af | 510 | for Pcd in self.PcdUnknownOffsetList :\r |
86737681 | 511 | if NowOffset % Pcd.Alignment != 0:\r |
1ccc4d89 | 512 | NowOffset = (NowOffset/ Pcd.Alignment + 1) * Pcd.Alignment\r |
111be80f | 513 | Pcd.PcdBinOffset = NowOffset\r |
514 | Pcd.PcdOffset = str(hex(Pcd.PcdBinOffset))\r | |
22061fab | 515 | NowOffset += Pcd.PcdOccupySize\r |
f7496d71 | 516 | \r |
111be80f | 517 | self.PcdFixedOffsetSizeList = self.PcdUnknownOffsetList\r |
518 | return\r | |
47fea6af | 519 | \r |
f7496d71 | 520 | # Check the offset of VPD type pcd's offset start from 0.\r |
47fea6af | 521 | if self.PcdFixedOffsetSizeList[0].PcdBinOffset != 0 :\r |
111be80f | 522 | EdkLogger.warn("BPDG", "The offset of VPD type pcd should start with 0, please check it.",\r |
47fea6af YZ |
523 | None)\r |
524 | \r | |
111be80f | 525 | # Judge whether the offset in fixed pcd offset list is overlapped or not.\r |
526 | lenOfList = len(self.PcdFixedOffsetSizeList)\r | |
f7496d71 | 527 | count = 0\r |
111be80f | 528 | while (count < lenOfList - 1) :\r |
529 | PcdNow = self.PcdFixedOffsetSizeList[count]\r | |
530 | PcdNext = self.PcdFixedOffsetSizeList[count+1]\r | |
f7496d71 | 531 | # Two pcd's offset is same\r |
111be80f | 532 | if PcdNow.PcdBinOffset == PcdNext.PcdBinOffset :\r |
47fea6af YZ |
533 | EdkLogger.error("BPDG", BuildToolError.ATTRIBUTE_GET_FAILURE,\r |
534 | "The offset of %s at line: %s is same with %s at line: %s in file %s" % \\r | |
111be80f | 535 | (PcdNow.PcdCName, PcdNow.Lineno, PcdNext.PcdCName, PcdNext.Lineno, PcdNext.FileName),\r |
536 | None)\r | |
47fea6af | 537 | \r |
f7496d71 | 538 | # Overlapped\r |
22061fab | 539 | if PcdNow.PcdBinOffset + PcdNow.PcdOccupySize > PcdNext.PcdBinOffset :\r |
47fea6af YZ |
540 | EdkLogger.error("BPDG", BuildToolError.ATTRIBUTE_GET_FAILURE,\r |
541 | "The offset of %s at line: %s is overlapped with %s at line: %s in file %s" % \\r | |
111be80f | 542 | (PcdNow.PcdCName, PcdNow.Lineno, PcdNext.PcdCName, PcdNext.Lineno, PcdNext.FileName),\r |
543 | None)\r | |
47fea6af | 544 | \r |
f7496d71 | 545 | # Has free space, raise a warning message\r |
22061fab | 546 | if PcdNow.PcdBinOffset + PcdNow.PcdOccupySize < PcdNext.PcdBinOffset :\r |
47fea6af YZ |
547 | EdkLogger.warn("BPDG", BuildToolError.ATTRIBUTE_GET_FAILURE,\r |
548 | "The offsets have free space of between %s at line: %s and %s at line: %s in file %s" % \\r | |
111be80f | 549 | (PcdNow.PcdCName, PcdNow.Lineno, PcdNext.PcdCName, PcdNext.Lineno, PcdNext.FileName),\r |
550 | None)\r | |
551 | count += 1\r | |
f7496d71 | 552 | \r |
111be80f | 553 | LastOffset = self.PcdFixedOffsetSizeList[0].PcdBinOffset\r |
554 | FixOffsetSizeListCount = 0\r | |
555 | lenOfList = len(self.PcdFixedOffsetSizeList)\r | |
556 | lenOfUnfixedList = len(self.PcdUnknownOffsetList)\r | |
f7496d71 | 557 | \r |
111be80f | 558 | ##\r |
f7496d71 LG |
559 | # Insert the un-fixed offset pcd's list into fixed offset pcd's list if has free space between those pcds.\r |
560 | #\r | |
111be80f | 561 | while (FixOffsetSizeListCount < lenOfList) :\r |
f7496d71 LG |
562 | \r |
563 | eachFixedPcd = self.PcdFixedOffsetSizeList[FixOffsetSizeListCount]\r | |
111be80f | 564 | NowOffset = eachFixedPcd.PcdBinOffset\r |
f7496d71 LG |
565 | \r |
566 | # Has free space\r | |
111be80f | 567 | if LastOffset < NowOffset :\r |
568 | if lenOfUnfixedList != 0 :\r | |
569 | countOfUnfixedList = 0\r | |
47fea6af | 570 | while(countOfUnfixedList < lenOfUnfixedList) :\r |
111be80f | 571 | eachUnfixedPcd = self.PcdUnknownOffsetList[countOfUnfixedList]\r |
22061fab | 572 | needFixPcdSize = eachUnfixedPcd.PcdOccupySize\r |
111be80f | 573 | # Not been fixed\r |
bc39c5cb | 574 | if eachUnfixedPcd.PcdOffset == TAB_STAR :\r |
86737681 | 575 | if LastOffset % eachUnfixedPcd.Alignment != 0:\r |
1ccc4d89 | 576 | LastOffset = (LastOffset / eachUnfixedPcd.Alignment + 1) * eachUnfixedPcd.Alignment\r |
111be80f | 577 | # The offset un-fixed pcd can write into this free space\r |
578 | if needFixPcdSize <= (NowOffset - LastOffset) :\r | |
579 | # Change the offset value of un-fixed pcd\r | |
580 | eachUnfixedPcd.PcdOffset = str(hex(LastOffset))\r | |
581 | eachUnfixedPcd.PcdBinOffset = LastOffset\r | |
582 | # Insert this pcd into fixed offset pcd list.\r | |
ccaa7754 | 583 | self.PcdFixedOffsetSizeList.insert(FixOffsetSizeListCount, eachUnfixedPcd)\r |
f7496d71 | 584 | \r |
111be80f | 585 | # Delete the item's offset that has been fixed and added into fixed offset list\r |
586 | self.PcdUnknownOffsetList.pop(countOfUnfixedList)\r | |
f7496d71 | 587 | \r |
111be80f | 588 | # After item added, should enlarge the length of fixed pcd offset list\r |
f7496d71 | 589 | lenOfList += 1\r |
111be80f | 590 | FixOffsetSizeListCount += 1\r |
f7496d71 | 591 | \r |
111be80f | 592 | # Decrease the un-fixed pcd offset list's length\r |
111be80f | 593 | lenOfUnfixedList -= 1\r |
f7496d71 LG |
594 | \r |
595 | # Modify the last offset value\r | |
596 | LastOffset += needFixPcdSize\r | |
111be80f | 597 | else :\r |
6780eef1 | 598 | # It can not insert into those two pcds, need to check still has other space can store it.\r |
22061fab | 599 | LastOffset = NowOffset + self.PcdFixedOffsetSizeList[FixOffsetSizeListCount].PcdOccupySize\r |
111be80f | 600 | FixOffsetSizeListCount += 1\r |
6780eef1 | 601 | break\r |
f7496d71 | 602 | \r |
111be80f | 603 | # Set the FixOffsetSizeListCount = lenOfList for quit the loop\r |
604 | else :\r | |
f7496d71 LG |
605 | FixOffsetSizeListCount = lenOfList\r |
606 | \r | |
607 | # No free space, smoothly connect with previous pcd.\r | |
111be80f | 608 | elif LastOffset == NowOffset :\r |
22061fab | 609 | LastOffset = NowOffset + eachFixedPcd.PcdOccupySize\r |
111be80f | 610 | FixOffsetSizeListCount += 1\r |
f7496d71 | 611 | # Usually it will not enter into this thunk, if so, means it overlapped.\r |
111be80f | 612 | else :\r |
47fea6af YZ |
613 | EdkLogger.error("BPDG", BuildToolError.ATTRIBUTE_NOT_AVAILABLE,\r |
614 | "The offset value definition has overlapped at pcd: %s, it's offset is: %s, in file: %s line: %s" % \\r | |
111be80f | 615 | (eachFixedPcd.PcdCName, eachFixedPcd.PcdOffset, eachFixedPcd.InputFileName, eachFixedPcd.Lineno),\r |
616 | None)\r | |
617 | FixOffsetSizeListCount += 1\r | |
f7496d71 LG |
618 | \r |
619 | # Continue to process the un-fixed offset pcd's list, add this time, just append them behind the fixed pcd's offset list.\r | |
111be80f | 620 | lenOfUnfixedList = len(self.PcdUnknownOffsetList)\r |
621 | lenOfList = len(self.PcdFixedOffsetSizeList)\r | |
622 | while (lenOfUnfixedList > 0) :\r | |
623 | # Still has items need to process\r | |
624 | # The last pcd instance\r | |
625 | LastPcd = self.PcdFixedOffsetSizeList[lenOfList-1]\r | |
626 | NeedFixPcd = self.PcdUnknownOffsetList[0]\r | |
f7496d71 | 627 | \r |
22061fab | 628 | NeedFixPcd.PcdBinOffset = LastPcd.PcdBinOffset + LastPcd.PcdOccupySize\r |
86737681 | 629 | if NeedFixPcd.PcdBinOffset % NeedFixPcd.Alignment != 0:\r |
1ccc4d89 | 630 | NeedFixPcd.PcdBinOffset = (NeedFixPcd.PcdBinOffset / NeedFixPcd.Alignment + 1) * NeedFixPcd.Alignment\r |
86737681 | 631 | \r |
111be80f | 632 | NeedFixPcd.PcdOffset = str(hex(NeedFixPcd.PcdBinOffset))\r |
f7496d71 | 633 | \r |
111be80f | 634 | # Insert this pcd into fixed offset pcd list's tail.\r |
635 | self.PcdFixedOffsetSizeList.insert(lenOfList, NeedFixPcd)\r | |
636 | # Delete the item's offset that has been fixed and added into fixed offset list\r | |
637 | self.PcdUnknownOffsetList.pop(0)\r | |
f7496d71 | 638 | \r |
111be80f | 639 | lenOfList += 1\r |
f7496d71 | 640 | lenOfUnfixedList -= 1\r |
111be80f | 641 | ##\r |
642 | # Write the final data into output files.\r | |
f7496d71 | 643 | #\r |
111be80f | 644 | def GenerateVpdFile (self, MapFileName, BinFileName):\r |
645 | #Open an VPD file to process\r | |
646 | \r | |
647 | try:\r | |
47fea6af | 648 | fVpdFile = open(BinFileName, "wb", 0)\r |
111be80f | 649 | except:\r |
650 | # Open failed\r | |
47fea6af YZ |
651 | EdkLogger.error("BPDG", BuildToolError.FILE_OPEN_FAILURE, "File open failed for %s" % self.VpdFileName, None)\r |
652 | \r | |
111be80f | 653 | try :\r |
1ccc4d89 | 654 | fMapFile = open(MapFileName, "w", 0)\r |
111be80f | 655 | except:\r |
656 | # Open failed\r | |
47fea6af YZ |
657 | EdkLogger.error("BPDG", BuildToolError.FILE_OPEN_FAILURE, "File open failed for %s" % self.MapFileName, None)\r |
658 | \r | |
86379ac4 | 659 | # Use a instance of BytesIO to cache data\r |
1ccc4d89 | 660 | fStringIO = BytesIO('')\r |
47fea6af | 661 | \r |
111be80f | 662 | # Write the header of map file.\r |
663 | try :\r | |
664 | fMapFile.write (st.MAP_FILE_COMMENT_TEMPLATE + "\n")\r | |
665 | except:\r | |
47fea6af YZ |
666 | 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 |
667 | \r | |
111be80f | 668 | for eachPcd in self.PcdFixedOffsetSizeList :\r |
669 | # write map file\r | |
670 | try :\r | |
ccaa7754 | 671 | fMapFile.write("%s | %s | %s | %s | %s \n" % (eachPcd.PcdCName, eachPcd.SkuId, eachPcd.PcdOffset, eachPcd.PcdSize, eachPcd.PcdUnpackValue))\r |
111be80f | 672 | except:\r |
47fea6af YZ |
673 | 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 |
674 | \r | |
111be80f | 675 | # Write Vpd binary file\r |
47fea6af | 676 | fStringIO.seek (eachPcd.PcdBinOffset)\r |
111be80f | 677 | if isinstance(eachPcd.PcdValue, list):\r |
1ccc4d89 LG |
678 | ValueList = [chr(Item) for Item in eachPcd.PcdValue]\r |
679 | fStringIO.write(''.join(ValueList))\r | |
47fea6af | 680 | else:\r |
111be80f | 681 | fStringIO.write (eachPcd.PcdValue)\r |
47fea6af YZ |
682 | \r |
683 | try :\r | |
111be80f | 684 | fVpdFile.write (fStringIO.getvalue())\r |
685 | except:\r | |
47fea6af YZ |
686 | 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)\r |
687 | \r | |
111be80f | 688 | fStringIO.close ()\r |
689 | fVpdFile.close ()\r | |
690 | fMapFile.close ()\r | |
f7496d71 | 691 | \r |