BaseTools: Move OverrideAttribs to OptRomInfStatement.py
[mirror_edk2.git] / BaseTools / Source / Python / GenFds / FdfParser.py
CommitLineData
30fdf114
LG
1## @file\r
2# parse FDF file\r
3#\r
a87e79d9 4# Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
118bf096 5# Copyright (c) 2015, Hewlett Packard Enterprise Development, L.P.<BR>\r
30fdf114 6#\r
40d841f6 7# This program and the accompanying materials\r
30fdf114
LG
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
16##\r
17# Import Modules\r
18#\r
72443dd2 19from __future__ import print_function\r
bfa65b61 20from __future__ import absolute_import\r
0d2711a6
LG
21import re\r
22\r
bfa65b61
GL
23from . import Fd\r
24from . import Region\r
25from . import Fv\r
26from . import AprioriSection\r
27from . import FfsInfStatement\r
28from . import FfsFileStatement\r
29from . import VerSection\r
30from . import UiSection\r
31from . import FvImageSection\r
32from . import DataSection\r
33from . import DepexSection\r
34from . import CompressSection\r
35from . import GuidSection\r
36from . import Capsule\r
37from . import CapsuleData\r
38from . import Rule\r
39from . import RuleComplexFile\r
40from . import RuleSimpleFile\r
41from . import EfiSection\r
42from . import Vtf\r
43from . import ComponentStatement\r
44from . import OptionRom\r
45from . import OptRomInfStatement\r
46from . import OptRomFileStatement\r
cfbe3c35 47import string\r
30fdf114 48\r
bfa65b61 49from .GenFdsGlobalVariable import GenFdsGlobalVariable\r
30fdf114
LG
50from Common.BuildToolError import *\r
51from Common import EdkLogger\r
14c48571 52from Common.Misc import PathClass\r
5a57246e 53from Common.StringUtils import NormPath\r
0d2711a6
LG
54import Common.GlobalData as GlobalData\r
55from Common.Expression import *\r
df692f02 56from Common import GlobalData\r
55c84777 57from Common.DataType import *\r
5a57246e 58from Common.StringUtils import ReplaceMacro\r
91ae2988 59import uuid\r
d0acc87a 60from Common.Misc import tdict\r
94e4bcbb 61from Common.MultipleWorkspace import MultipleWorkspace as mws\r
1be2ed90
HC
62import Common.LongFilePathOs as os\r
63from Common.LongFilePathSupport import OpenLongFilePath as open\r
bfa65b61
GL
64from .Capsule import EFI_CERT_TYPE_PKCS7_GUID\r
65from .Capsule import EFI_CERT_TYPE_RSA2048_SHA256_GUID\r
2eb370ff 66from Common.RangeExpression import RangeExpression\r
30fdf114
LG
67\r
68##define T_CHAR_SPACE ' '\r
69##define T_CHAR_NULL '\0'\r
70##define T_CHAR_CR '\r'\r
71##define T_CHAR_TAB '\t'\r
72##define T_CHAR_LF '\n'\r
73##define T_CHAR_SLASH '/'\r
74##define T_CHAR_BACKSLASH '\\'\r
75##define T_CHAR_DOUBLE_QUOTE '\"'\r
76##define T_CHAR_SINGLE_QUOTE '\''\r
77##define T_CHAR_STAR '*'\r
78##define T_CHAR_HASH '#'\r
79\r
80(T_CHAR_SPACE, T_CHAR_NULL, T_CHAR_CR, T_CHAR_TAB, T_CHAR_LF, T_CHAR_SLASH, \\r
81T_CHAR_BACKSLASH, T_CHAR_DOUBLE_QUOTE, T_CHAR_SINGLE_QUOTE, T_CHAR_STAR, T_CHAR_HASH) = \\r
82(' ', '\0', '\r', '\t', '\n', '/', '\\', '\"', '\'', '*', '#')\r
83\r
84SEPERATOR_TUPLE = ('=', '|', ',', '{', '}')\r
85\r
0d2711a6 86RegionSizePattern = re.compile("\s*(?P<base>(?:0x|0X)?[a-fA-F0-9]+)\s*\|\s*(?P<size>(?:0x|0X)?[a-fA-F0-9]+)\s*")\r
543f5ac3
B
87RegionSizeGuidPattern = re.compile("\s*(?P<base>\w+\.\w+[\.\w\[\]]*)\s*\|\s*(?P<size>\w+\.\w+[\.\w\[\]]*)\s*")\r
88RegionOffsetPcdPattern = re.compile("\s*(?P<base>\w+\.\w+[\.\w\[\]]*)\s*$")\r
64b2609f 89ShortcutPcdPattern = re.compile("\s*\w+\s*=\s*(?P<value>(?:0x|0X)?[a-fA-F0-9]+)\s*\|\s*(?P<name>\w+\.\w+)\s*")\r
ffe720c5 90BaseAddrValuePattern = re.compile('^0[xX][0-9a-fA-F]+')\r
95cc4962
FY
91FileExtensionPattern = re.compile(r'([a-zA-Z][a-zA-Z0-9]*)')\r
92TokenFindPattern = re.compile(r'([a-zA-Z0-9\-]+|\$\(TARGET\)|\*)_([a-zA-Z0-9\-]+|\$\(TOOL_CHAIN_TAG\)|\*)_([a-zA-Z0-9\-]+|\$\(ARCH\)|\*)')\r
118bf096
CS
93AllIncludeFileList = []\r
94\r
95# Get the closest parent\r
96def GetParentAtLine (Line):\r
97 for Profile in AllIncludeFileList:\r
98 if Profile.IsLineInFile(Line):\r
99 return Profile\r
100 return None\r
101\r
102# Check include loop\r
103def IsValidInclude (File, Line):\r
104 for Profile in AllIncludeFileList:\r
105 if Profile.IsLineInFile(Line) and Profile.FileName == File:\r
106 return False\r
107\r
108 return True\r
30fdf114
LG
109\r
110def GetRealFileLine (File, Line):\r
111\r
112 InsertedLines = 0\r
118bf096
CS
113 for Profile in AllIncludeFileList:\r
114 if Profile.IsLineInFile(Line):\r
115 return Profile.GetLineInFile(Line)\r
116 elif Line >= Profile.InsertStartLineNumber and Profile.Level == 1:\r
df81077f 117 InsertedLines += Profile.GetTotalLines()\r
30fdf114
LG
118\r
119 return (File, Line - InsertedLines)\r
120\r
121## The exception class that used to report error messages when parsing FDF\r
122#\r
123# Currently the "ToolName" is set to be "FDF Parser".\r
124#\r
125class Warning (Exception):\r
126 ## The constructor\r
127 #\r
128 # @param self The object pointer\r
129 # @param Str The message to record\r
130 # @param File The FDF name\r
131 # @param Line The Line number that error occurs\r
132 #\r
133 def __init__(self, Str, File = None, Line = None):\r
134\r
135 FileLineTuple = GetRealFileLine(File, Line)\r
136 self.FileName = FileLineTuple[0]\r
137 self.LineNumber = FileLineTuple[1]\r
118bf096 138 self.OriginalLineNumber = Line\r
30fdf114
LG
139 self.Message = Str\r
140 self.ToolName = 'FdfParser'\r
141\r
142 def __str__(self):\r
143 return self.Message\r
144\r
30fdf114
LG
145## The Include file content class that used to record file data when parsing include file\r
146#\r
147# May raise Exception when opening file.\r
148#\r
149class IncludeFileProfile :\r
150 ## The constructor\r
151 #\r
152 # @param self The object pointer\r
153 # @param FileName The file that to be parsed\r
154 #\r
155 def __init__(self, FileName):\r
156 self.FileName = FileName\r
157 self.FileLinesList = []\r
158 try:\r
159 fsock = open(FileName, "rb", 0)\r
160 try:\r
161 self.FileLinesList = fsock.readlines()\r
47a29bc7
YF
162 for index, line in enumerate(self.FileLinesList):\r
163 if not line.endswith('\n'):\r
164 self.FileLinesList[index] += '\n'\r
165\r
30fdf114
LG
166 finally:\r
167 fsock.close()\r
168\r
169 except:\r
170 EdkLogger.error("FdfParser", FILE_OPEN_FAILURE, ExtraData=FileName)\r
171\r
172 self.InsertStartLineNumber = None\r
173 self.InsertAdjust = 0\r
118bf096
CS
174 self.IncludeFileList = []\r
175 self.Level = 1 # first level include file\r
f7496d71 176\r
118bf096
CS
177 def GetTotalLines(self):\r
178 TotalLines = self.InsertAdjust + len(self.FileLinesList)\r
179\r
180 for Profile in self.IncludeFileList:\r
df81077f 181 TotalLines += Profile.GetTotalLines()\r
118bf096
CS
182\r
183 return TotalLines\r
184\r
185 def IsLineInFile(self, Line):\r
186 if Line >= self.InsertStartLineNumber and Line < self.InsertStartLineNumber + self.GetTotalLines():\r
187 return True\r
188\r
189 return False\r
190\r
191 def GetLineInFile(self, Line):\r
192 if not self.IsLineInFile (Line):\r
193 return (self.FileName, -1)\r
f7496d71 194\r
118bf096
CS
195 InsertedLines = self.InsertStartLineNumber\r
196\r
197 for Profile in self.IncludeFileList:\r
198 if Profile.IsLineInFile(Line):\r
199 return Profile.GetLineInFile(Line)\r
200 elif Line >= Profile.InsertStartLineNumber:\r
201 InsertedLines += Profile.GetTotalLines()\r
202\r
203 return (self.FileName, Line - InsertedLines + 1)\r
204\r
205\r
30fdf114
LG
206\r
207## The FDF content class that used to record file data when parsing FDF\r
208#\r
209# May raise Exception when opening file.\r
210#\r
211class FileProfile :\r
212 ## The constructor\r
213 #\r
214 # @param self The object pointer\r
215 # @param FileName The file that to be parsed\r
216 #\r
217 def __init__(self, FileName):\r
218 self.FileLinesList = []\r
219 try:\r
220 fsock = open(FileName, "rb", 0)\r
221 try:\r
222 self.FileLinesList = fsock.readlines()\r
223 finally:\r
224 fsock.close()\r
225\r
226 except:\r
227 EdkLogger.error("FdfParser", FILE_OPEN_FAILURE, ExtraData=FileName)\r
228\r
543f5ac3 229 self.FileName = FileName\r
30fdf114 230 self.PcdDict = {}\r
543f5ac3 231 self.PcdLocalDict = {}\r
30fdf114 232 self.InfList = []\r
2502b735 233 self.InfDict = {'ArchTBD':[]}\r
d0acc87a
LG
234 # ECC will use this Dict and List information\r
235 self.PcdFileLineDict = {}\r
236 self.InfFileLineList = []\r
f7496d71 237\r
30fdf114 238 self.FdDict = {}\r
52302d4d 239 self.FdNameNotSet = False\r
30fdf114 240 self.FvDict = {}\r
fd171542 241 self.CapsuleDict = {}\r
30fdf114
LG
242 self.VtfList = []\r
243 self.RuleDict = {}\r
244 self.OptRomDict = {}\r
a3251d84 245 self.FmpPayloadDict = {}\r
30fdf114
LG
246\r
247## The syntax parser for FDF\r
248#\r
249# PreprocessFile method should be called prior to ParseFile\r
250# CycleReferenceCheck method can detect cycles in FDF contents\r
251#\r
252# GetNext*** procedures mean these procedures will get next token first, then make judgement.\r
253# Get*** procedures mean these procedures will make judgement on current token only.\r
254#\r
255class FdfParser:\r
256 ## The constructor\r
257 #\r
258 # @param self The object pointer\r
259 # @param FileName The file that to be parsed\r
260 #\r
261 def __init__(self, FileName):\r
262 self.Profile = FileProfile(FileName)\r
263 self.FileName = FileName\r
264 self.CurrentLineNumber = 1\r
265 self.CurrentOffsetWithinLine = 0\r
266 self.CurrentFdName = None\r
267 self.CurrentFvName = None\r
268 self.__Token = ""\r
269 self.__SkippedChars = ""\r
97fa0ee9 270 GlobalData.gFdfParser = self\r
30fdf114 271\r
d0acc87a
LG
272 # Used to section info\r
273 self.__CurSection = []\r
274 # Key: [section name, UI name, arch]\r
275 # Value: {MACRO_NAME : MACRO_VALUE}\r
276 self.__MacroDict = tdict(True, 3)\r
277 self.__PcdDict = {}\r
278\r
30fdf114 279 self.__WipeOffArea = []\r
14c48571 280 if GenFdsGlobalVariable.WorkSpaceDir == '':\r
281 GenFdsGlobalVariable.WorkSpaceDir = os.getenv("WORKSPACE")\r
30fdf114 282\r
30fdf114
LG
283 ## __SkipWhiteSpace() method\r
284 #\r
285 # Skip white spaces from current char, return number of chars skipped\r
286 #\r
287 # @param self The object pointer\r
288 # @retval Count The number of chars skipped\r
289 #\r
290 def __SkipWhiteSpace(self):\r
291 Count = 0\r
292 while not self.__EndOfFile():\r
293 Count += 1\r
294 if self.__CurrentChar() in (T_CHAR_NULL, T_CHAR_CR, T_CHAR_LF, T_CHAR_SPACE, T_CHAR_TAB):\r
295 self.__SkippedChars += str(self.__CurrentChar())\r
296 self.__GetOneChar()\r
297\r
298 else:\r
299 Count = Count - 1\r
300 return Count\r
301\r
302 ## __EndOfFile() method\r
303 #\r
304 # Judge current buffer pos is at file end\r
305 #\r
306 # @param self The object pointer\r
307 # @retval True Current File buffer position is at file end\r
308 # @retval False Current File buffer position is NOT at file end\r
309 #\r
310 def __EndOfFile(self):\r
311 NumberOfLines = len(self.Profile.FileLinesList)\r
312 SizeOfLastLine = len(self.Profile.FileLinesList[-1])\r
313 if self.CurrentLineNumber == NumberOfLines and self.CurrentOffsetWithinLine >= SizeOfLastLine - 1:\r
314 return True\r
315 elif self.CurrentLineNumber > NumberOfLines:\r
316 return True\r
317 else:\r
318 return False\r
319\r
320 ## __EndOfLine() method\r
321 #\r
322 # Judge current buffer pos is at line end\r
323 #\r
324 # @param self The object pointer\r
325 # @retval True Current File buffer position is at line end\r
326 # @retval False Current File buffer position is NOT at line end\r
327 #\r
328 def __EndOfLine(self):\r
329 if self.CurrentLineNumber > len(self.Profile.FileLinesList):\r
330 return True\r
331 SizeOfCurrentLine = len(self.Profile.FileLinesList[self.CurrentLineNumber - 1])\r
332 if self.CurrentOffsetWithinLine >= SizeOfCurrentLine:\r
333 return True\r
334 else:\r
335 return False\r
336\r
337 ## Rewind() method\r
338 #\r
339 # Reset file data buffer to the initial state\r
340 #\r
341 # @param self The object pointer\r
118bf096 342 # @param DestLine Optional new destination line number.\r
f7496d71 343 # @param DestOffset Optional new destination offset.\r
30fdf114 344 #\r
f7496d71
LG
345 def Rewind(self, DestLine = 1, DestOffset = 0):\r
346 self.CurrentLineNumber = DestLine\r
347 self.CurrentOffsetWithinLine = DestOffset\r
30fdf114
LG
348\r
349 ## __UndoOneChar() method\r
350 #\r
351 # Go back one char in the file buffer\r
352 #\r
353 # @param self The object pointer\r
354 # @retval True Successfully go back one char\r
355 # @retval False Not able to go back one char as file beginning reached\r
356 #\r
357 def __UndoOneChar(self):\r
358\r
359 if self.CurrentLineNumber == 1 and self.CurrentOffsetWithinLine == 0:\r
360 return False\r
361 elif self.CurrentOffsetWithinLine == 0:\r
362 self.CurrentLineNumber -= 1\r
363 self.CurrentOffsetWithinLine = len(self.__CurrentLine()) - 1\r
364 else:\r
365 self.CurrentOffsetWithinLine -= 1\r
366 return True\r
367\r
368 ## __GetOneChar() method\r
369 #\r
370 # Move forward one char in the file buffer\r
371 #\r
372 # @param self The object pointer\r
373 #\r
374 def __GetOneChar(self):\r
375 if self.CurrentOffsetWithinLine == len(self.Profile.FileLinesList[self.CurrentLineNumber - 1]) - 1:\r
0d2711a6
LG
376 self.CurrentLineNumber += 1\r
377 self.CurrentOffsetWithinLine = 0\r
30fdf114 378 else:\r
0d2711a6 379 self.CurrentOffsetWithinLine += 1\r
30fdf114
LG
380\r
381 ## __CurrentChar() method\r
382 #\r
383 # Get the char pointed to by the file buffer pointer\r
384 #\r
385 # @param self The object pointer\r
386 # @retval Char Current char\r
387 #\r
388 def __CurrentChar(self):\r
389 return self.Profile.FileLinesList[self.CurrentLineNumber - 1][self.CurrentOffsetWithinLine]\r
390\r
391 ## __NextChar() method\r
392 #\r
393 # Get the one char pass the char pointed to by the file buffer pointer\r
394 #\r
395 # @param self The object pointer\r
396 # @retval Char Next char\r
397 #\r
398 def __NextChar(self):\r
399 if self.CurrentOffsetWithinLine == len(self.Profile.FileLinesList[self.CurrentLineNumber - 1]) - 1:\r
400 return self.Profile.FileLinesList[self.CurrentLineNumber][0]\r
401 else:\r
402 return self.Profile.FileLinesList[self.CurrentLineNumber - 1][self.CurrentOffsetWithinLine + 1]\r
403\r
404 ## __SetCurrentCharValue() method\r
405 #\r
406 # Modify the value of current char\r
407 #\r
408 # @param self The object pointer\r
409 # @param Value The new value of current char\r
410 #\r
411 def __SetCurrentCharValue(self, Value):\r
412 self.Profile.FileLinesList[self.CurrentLineNumber - 1][self.CurrentOffsetWithinLine] = Value\r
413\r
414 ## __CurrentLine() method\r
415 #\r
416 # Get the list that contains current line contents\r
417 #\r
418 # @param self The object pointer\r
419 # @retval List current line contents\r
420 #\r
421 def __CurrentLine(self):\r
422 return self.Profile.FileLinesList[self.CurrentLineNumber - 1]\r
423\r
424 def __StringToList(self):\r
425 self.Profile.FileLinesList = [list(s) for s in self.Profile.FileLinesList]\r
426 self.Profile.FileLinesList[-1].append(' ')\r
427\r
30fdf114
LG
428 def __ReplaceFragment(self, StartPos, EndPos, Value = ' '):\r
429 if StartPos[0] == EndPos[0]:\r
430 Offset = StartPos[1]\r
431 while Offset <= EndPos[1]:\r
432 self.Profile.FileLinesList[StartPos[0]][Offset] = Value\r
433 Offset += 1\r
434 return\r
435\r
436 Offset = StartPos[1]\r
437 while self.Profile.FileLinesList[StartPos[0]][Offset] not in ('\r', '\n'):\r
438 self.Profile.FileLinesList[StartPos[0]][Offset] = Value\r
439 Offset += 1\r
440\r
441 Line = StartPos[0]\r
442 while Line < EndPos[0]:\r
443 Offset = 0\r
444 while self.Profile.FileLinesList[Line][Offset] not in ('\r', '\n'):\r
445 self.Profile.FileLinesList[Line][Offset] = Value\r
446 Offset += 1\r
447 Line += 1\r
448\r
449 Offset = 0\r
450 while Offset <= EndPos[1]:\r
451 self.Profile.FileLinesList[EndPos[0]][Offset] = Value\r
452 Offset += 1\r
453\r
454\r
d5d56f1b
LG
455 def __GetMacroName(self):\r
456 if not self.__GetNextToken():\r
457 raise Warning("expected Macro name", self.FileName, self.CurrentLineNumber)\r
458 MacroName = self.__Token\r
459 NotFlag = False\r
460 if MacroName.startswith('!'):\r
461 NotFlag = True\r
462 MacroName = MacroName[1:].strip()\r
f7496d71 463\r
d5d56f1b
LG
464 if not MacroName.startswith('$(') or not MacroName.endswith(')'):\r
465 raise Warning("Macro name expected(Please use '$(%(Token)s)' if '%(Token)s' is a macro.)" % {"Token" : MacroName},\r
466 self.FileName, self.CurrentLineNumber)\r
467 MacroName = MacroName[2:-1]\r
468 return MacroName, NotFlag\r
d0acc87a
LG
469\r
470 def __SetMacroValue(self, Macro, Value):\r
471 if not self.__CurSection:\r
472 return\r
473\r
474 MacroDict = {}\r
475 if not self.__MacroDict[self.__CurSection[0], self.__CurSection[1], self.__CurSection[2]]:\r
476 self.__MacroDict[self.__CurSection[0], self.__CurSection[1], self.__CurSection[2]] = MacroDict\r
477 else:\r
478 MacroDict = self.__MacroDict[self.__CurSection[0], self.__CurSection[1], self.__CurSection[2]]\r
479 MacroDict[Macro] = Value\r
480\r
481 def __GetMacroValue(self, Macro):\r
482 # Highest priority\r
483 if Macro in GlobalData.gCommandLineDefines:\r
484 return GlobalData.gCommandLineDefines[Macro]\r
485 if Macro in GlobalData.gGlobalDefines:\r
486 return GlobalData.gGlobalDefines[Macro]\r
487\r
488 if self.__CurSection:\r
489 MacroDict = self.__MacroDict[\r
490 self.__CurSection[0],\r
491 self.__CurSection[1],\r
492 self.__CurSection[2]\r
493 ]\r
494 if MacroDict and Macro in MacroDict:\r
495 return MacroDict[Macro]\r
496\r
497 # Lowest priority\r
498 if Macro in GlobalData.gPlatformDefines:\r
499 return GlobalData.gPlatformDefines[Macro]\r
500 return None\r
501\r
502 def __SectionHeaderParser(self, Section):\r
503 # [Defines]\r
504 # [FD.UiName]: use dummy instead if UI name is optional\r
505 # [FV.UiName]\r
506 # [Capsule.UiName]\r
507 # [Rule]: don't take rule section into account, macro is not allowed in this section\r
508 # [VTF.arch.UiName, arch]\r
509 # [OptionRom.DriverName]\r
510 self.__CurSection = []\r
511 Section = Section.strip()[1:-1].upper().replace(' ', '').strip('.')\r
512 ItemList = Section.split('.')\r
513 Item = ItemList[0]\r
514 if Item == '' or Item == 'RULE':\r
515 return\r
516\r
55c84777
CJ
517 if Item == TAB_COMMON_DEFINES.upper():\r
518 self.__CurSection = [TAB_COMMON, TAB_COMMON, TAB_COMMON]\r
d0acc87a
LG
519 elif Item == 'VTF' and len(ItemList) == 3:\r
520 UiName = ItemList[2]\r
521 Pos = UiName.find(',')\r
522 if Pos != -1:\r
523 UiName = UiName[:Pos]\r
524 self.__CurSection = ['VTF', UiName, ItemList[1]]\r
525 elif len(ItemList) > 1:\r
55c84777 526 self.__CurSection = [ItemList[0], ItemList[1], TAB_COMMON]\r
d0acc87a 527 elif len(ItemList) > 0:\r
55c84777 528 self.__CurSection = [ItemList[0], 'DUMMY', TAB_COMMON]\r
d0acc87a 529\r
30fdf114
LG
530 ## PreprocessFile() method\r
531 #\r
532 # Preprocess file contents, replace comments with spaces.\r
533 # In the end, rewind the file buffer pointer to the beginning\r
534 # BUGBUG: No !include statement processing contained in this procedure\r
535 # !include statement should be expanded at the same FileLinesList[CurrentLineNumber - 1]\r
536 #\r
537 # @param self The object pointer\r
538 #\r
539 def PreprocessFile(self):\r
540\r
541 self.Rewind()\r
542 InComment = False\r
543 DoubleSlashComment = False\r
544 HashComment = False\r
545 # HashComment in quoted string " " is ignored.\r
546 InString = False\r
547\r
548 while not self.__EndOfFile():\r
549\r
550 if self.__CurrentChar() == T_CHAR_DOUBLE_QUOTE and not InComment:\r
551 InString = not InString\r
552 # meet new line, then no longer in a comment for // and '#'\r
553 if self.__CurrentChar() == T_CHAR_LF:\r
554 self.CurrentLineNumber += 1\r
555 self.CurrentOffsetWithinLine = 0\r
556 if InComment and DoubleSlashComment:\r
557 InComment = False\r
558 DoubleSlashComment = False\r
559 if InComment and HashComment:\r
560 InComment = False\r
561 HashComment = False\r
562 # check for */ comment end\r
563 elif InComment and not DoubleSlashComment and not HashComment and self.__CurrentChar() == T_CHAR_STAR and self.__NextChar() == T_CHAR_SLASH:\r
564 self.__SetCurrentCharValue(T_CHAR_SPACE)\r
565 self.__GetOneChar()\r
566 self.__SetCurrentCharValue(T_CHAR_SPACE)\r
567 self.__GetOneChar()\r
568 InComment = False\r
569 # set comments to spaces\r
570 elif InComment:\r
571 self.__SetCurrentCharValue(T_CHAR_SPACE)\r
572 self.__GetOneChar()\r
573 # check for // comment\r
574 elif self.__CurrentChar() == T_CHAR_SLASH and self.__NextChar() == T_CHAR_SLASH and not self.__EndOfLine():\r
575 InComment = True\r
576 DoubleSlashComment = True\r
577 # check for '#' comment\r
578 elif self.__CurrentChar() == T_CHAR_HASH and not self.__EndOfLine() and not InString:\r
579 InComment = True\r
580 HashComment = True\r
581 # check for /* comment start\r
582 elif self.__CurrentChar() == T_CHAR_SLASH and self.__NextChar() == T_CHAR_STAR:\r
583 self.__SetCurrentCharValue( T_CHAR_SPACE)\r
584 self.__GetOneChar()\r
585 self.__SetCurrentCharValue( T_CHAR_SPACE)\r
586 self.__GetOneChar()\r
587 InComment = True\r
588 else:\r
589 self.__GetOneChar()\r
590\r
591 # restore from ListOfList to ListOfString\r
592 self.Profile.FileLinesList = ["".join(list) for list in self.Profile.FileLinesList]\r
593 self.Rewind()\r
594\r
595 ## PreprocessIncludeFile() method\r
596 #\r
597 # Preprocess file contents, replace !include statements with file contents.\r
598 # In the end, rewind the file buffer pointer to the beginning\r
599 #\r
600 # @param self The object pointer\r
601 #\r
602 def PreprocessIncludeFile(self):\r
f7496d71 603 # nested include support\r
118bf096 604 Processed = False\r
0fdfe274 605 MacroDict = {}\r
30fdf114
LG
606 while self.__GetNextToken():\r
607\r
0fdfe274
YZ
608 if self.__Token == 'DEFINE':\r
609 if not self.__GetNextToken():\r
610 raise Warning("expected Macro name", self.FileName, self.CurrentLineNumber)\r
611 Macro = self.__Token\r
612 if not self.__IsToken( "="):\r
613 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
614 Value = self.__GetExpression()\r
615 MacroDict[Macro] = Value\r
616\r
617 elif self.__Token == '!include':\r
118bf096 618 Processed = True\r
30fdf114
LG
619 IncludeLine = self.CurrentLineNumber\r
620 IncludeOffset = self.CurrentOffsetWithinLine - len('!include')\r
621 if not self.__GetNextToken():\r
622 raise Warning("expected include file name", self.FileName, self.CurrentLineNumber)\r
623 IncFileName = self.__Token\r
0fdfe274
YZ
624 PreIndex = 0\r
625 StartPos = IncFileName.find('$(', PreIndex)\r
626 EndPos = IncFileName.find(')', StartPos+2)\r
627 while StartPos != -1 and EndPos != -1:\r
628 Macro = IncFileName[StartPos+2 : EndPos]\r
d0acc87a 629 MacroVal = self.__GetMacroValue(Macro)\r
0fdfe274
YZ
630 if not MacroVal:\r
631 if Macro in MacroDict:\r
632 MacroVal = MacroDict[Macro]\r
4231a819 633 if MacroVal is not None:\r
0fdfe274
YZ
634 IncFileName = IncFileName.replace('$(' + Macro + ')', MacroVal, 1)\r
635 if MacroVal.find('$(') != -1:\r
636 PreIndex = StartPos\r
637 else:\r
638 PreIndex = StartPos + len(MacroVal)\r
639 else:\r
640 raise Warning("The Macro %s is not defined" %Macro, self.FileName, self.CurrentLineNumber)\r
641 StartPos = IncFileName.find('$(', PreIndex)\r
642 EndPos = IncFileName.find(')', StartPos+2)\r
643\r
644 IncludedFile = NormPath(IncFileName)\r
2bcc713e
LG
645 #\r
646 # First search the include file under the same directory as FDF file\r
647 #\r
648 IncludedFile1 = PathClass(IncludedFile, os.path.dirname(self.FileName))\r
649 ErrorCode = IncludedFile1.Validate()[0]\r
650 if ErrorCode != 0:\r
651 #\r
652 # Then search the include file under the same directory as DSC file\r
653 #\r
d0acc87a
LG
654 PlatformDir = ''\r
655 if GenFdsGlobalVariable.ActivePlatform:\r
656 PlatformDir = GenFdsGlobalVariable.ActivePlatform.Dir\r
657 elif GlobalData.gActivePlatform:\r
658 PlatformDir = GlobalData.gActivePlatform.MetaFile.Dir\r
659 IncludedFile1 = PathClass(IncludedFile, PlatformDir)\r
2bcc713e
LG
660 ErrorCode = IncludedFile1.Validate()[0]\r
661 if ErrorCode != 0:\r
662 #\r
663 # Also search file under the WORKSPACE directory\r
664 #\r
665 IncludedFile1 = PathClass(IncludedFile, GlobalData.gWorkspace)\r
666 ErrorCode = IncludedFile1.Validate()[0]\r
667 if ErrorCode != 0:\r
f7496d71 668 raise Warning("The include file does not exist under below directories: \n%s\n%s\n%s\n"%(os.path.dirname(self.FileName), PlatformDir, GlobalData.gWorkspace),\r
2bcc713e 669 self.FileName, self.CurrentLineNumber)\r
30fdf114 670\r
118bf096
CS
671 if not IsValidInclude (IncludedFile1.Path, self.CurrentLineNumber):\r
672 raise Warning("The include file {0} is causing a include loop.\n".format (IncludedFile1.Path), self.FileName, self.CurrentLineNumber)\r
673\r
2bcc713e 674 IncFileProfile = IncludeFileProfile(IncludedFile1.Path)\r
30fdf114
LG
675\r
676 CurrentLine = self.CurrentLineNumber\r
677 CurrentOffset = self.CurrentOffsetWithinLine\r
678 # list index of the insertion, note that line number is 'CurrentLine + 1'\r
679 InsertAtLine = CurrentLine\r
118bf096 680 ParentProfile = GetParentAtLine (CurrentLine)\r
4231a819 681 if ParentProfile is not None:\r
118bf096
CS
682 ParentProfile.IncludeFileList.insert(0, IncFileProfile)\r
683 IncFileProfile.Level = ParentProfile.Level + 1\r
30fdf114
LG
684 IncFileProfile.InsertStartLineNumber = InsertAtLine + 1\r
685 # deal with remaining portions after "!include filename", if exists.\r
686 if self.__GetNextToken():\r
687 if self.CurrentLineNumber == CurrentLine:\r
688 RemainingLine = self.__CurrentLine()[CurrentOffset:]\r
689 self.Profile.FileLinesList.insert(self.CurrentLineNumber, RemainingLine)\r
690 IncFileProfile.InsertAdjust += 1\r
691 self.CurrentLineNumber += 1\r
692 self.CurrentOffsetWithinLine = 0\r
693\r
694 for Line in IncFileProfile.FileLinesList:\r
695 self.Profile.FileLinesList.insert(InsertAtLine, Line)\r
696 self.CurrentLineNumber += 1\r
697 InsertAtLine += 1\r
698\r
118bf096
CS
699 # reversely sorted to better determine error in file\r
700 AllIncludeFileList.insert(0, IncFileProfile)\r
30fdf114
LG
701\r
702 # comment out the processed include file statement\r
703 TempList = list(self.Profile.FileLinesList[IncludeLine - 1])\r
704 TempList.insert(IncludeOffset, '#')\r
705 self.Profile.FileLinesList[IncludeLine - 1] = ''.join(TempList)\r
118bf096
CS
706 if Processed: # Nested and back-to-back support\r
707 self.Rewind(DestLine = IncFileProfile.InsertStartLineNumber - 1)\r
708 Processed = False\r
709 # Preprocess done.\r
30fdf114 710 self.Rewind()\r
f7496d71 711\r
5bcf1d56
CJ
712 @staticmethod\r
713 def __GetIfListCurrentItemStat(IfList):\r
da92f276
LG
714 if len(IfList) == 0:\r
715 return True\r
f7496d71 716\r
da92f276
LG
717 for Item in IfList:\r
718 if Item[1] == False:\r
719 return False\r
f7496d71 720\r
da92f276 721 return True\r
f7496d71 722\r
6780eef1 723 ## PreprocessConditionalStatement() method\r
30fdf114 724 #\r
6780eef1 725 # Preprocess conditional statement.\r
30fdf114
LG
726 # In the end, rewind the file buffer pointer to the beginning\r
727 #\r
728 # @param self The object pointer\r
729 #\r
730 def PreprocessConditionalStatement(self):\r
731 # IfList is a stack of if branches with elements of list [Pos, CondSatisfied, BranchDetermined]\r
732 IfList = []\r
0d2711a6 733 RegionLayoutLine = 0\r
d0acc87a 734 ReplacedLine = -1\r
30fdf114 735 while self.__GetNextToken():\r
d0acc87a
LG
736 # Determine section name and the location dependent macro\r
737 if self.__GetIfListCurrentItemStat(IfList):\r
738 if self.__Token.startswith('['):\r
739 Header = self.__Token\r
740 if not self.__Token.endswith(']'):\r
741 self.__SkipToToken(']')\r
742 Header += self.__SkippedChars\r
743 if Header.find('$(') != -1:\r
744 raise Warning("macro cannot be used in section header", self.FileName, self.CurrentLineNumber)\r
745 self.__SectionHeaderParser(Header)\r
746 continue\r
747 # Replace macros except in RULE section or out of section\r
748 elif self.__CurSection and ReplacedLine != self.CurrentLineNumber:\r
749 ReplacedLine = self.CurrentLineNumber\r
750 self.__UndoToken()\r
751 CurLine = self.Profile.FileLinesList[ReplacedLine - 1]\r
752 PreIndex = 0\r
753 StartPos = CurLine.find('$(', PreIndex)\r
754 EndPos = CurLine.find(')', StartPos+2)\r
64b2609f 755 while StartPos != -1 and EndPos != -1 and self.__Token not in ['!ifdef', '!ifndef', '!if', '!elseif']:\r
d0acc87a
LG
756 MacroName = CurLine[StartPos+2 : EndPos]\r
757 MacorValue = self.__GetMacroValue(MacroName)\r
4231a819 758 if MacorValue is not None:\r
d0acc87a
LG
759 CurLine = CurLine.replace('$(' + MacroName + ')', MacorValue, 1)\r
760 if MacorValue.find('$(') != -1:\r
761 PreIndex = StartPos\r
762 else:\r
763 PreIndex = StartPos + len(MacorValue)\r
764 else:\r
765 PreIndex = EndPos + 1\r
766 StartPos = CurLine.find('$(', PreIndex)\r
767 EndPos = CurLine.find(')', StartPos+2)\r
768 self.Profile.FileLinesList[ReplacedLine - 1] = CurLine\r
769 continue\r
770\r
30fdf114 771 if self.__Token == 'DEFINE':\r
d0acc87a
LG
772 if self.__GetIfListCurrentItemStat(IfList):\r
773 if not self.__CurSection:\r
774 raise Warning("macro cannot be defined in Rule section or out of section", self.FileName, self.CurrentLineNumber)\r
da92f276
LG
775 DefineLine = self.CurrentLineNumber - 1\r
776 DefineOffset = self.CurrentOffsetWithinLine - len('DEFINE')\r
777 if not self.__GetNextToken():\r
778 raise Warning("expected Macro name", self.FileName, self.CurrentLineNumber)\r
779 Macro = self.__Token\r
780 if not self.__IsToken( "="):\r
781 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
f7496d71 782\r
d0acc87a
LG
783 Value = self.__GetExpression()\r
784 self.__SetMacroValue(Macro, Value)\r
da92f276 785 self.__WipeOffArea.append(((DefineLine, DefineOffset), (self.CurrentLineNumber - 1, self.CurrentOffsetWithinLine - 1)))\r
0d2711a6 786 elif self.__Token == 'SET':\r
2f04e527
YL
787 if not self.__GetIfListCurrentItemStat(IfList):\r
788 continue\r
64b2609f
LG
789 SetLine = self.CurrentLineNumber - 1\r
790 SetOffset = self.CurrentOffsetWithinLine - len('SET')\r
543f5ac3 791 PcdPair = self.__GetNextPcdSettings()\r
0d2711a6
LG
792 PcdName = "%s.%s" % (PcdPair[1], PcdPair[0])\r
793 if not self.__IsToken( "="):\r
794 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
795\r
d0acc87a
LG
796 Value = self.__GetExpression()\r
797 Value = self.__EvaluateConditional(Value, self.CurrentLineNumber, 'eval', True)\r
30fdf114 798\r
d0acc87a 799 self.__PcdDict[PcdName] = Value\r
64b2609f
LG
800\r
801 self.Profile.PcdDict[PcdPair] = Value\r
543f5ac3 802 self.SetPcdLocalation(PcdPair)\r
64b2609f
LG
803 FileLineTuple = GetRealFileLine(self.FileName, self.CurrentLineNumber)\r
804 self.Profile.PcdFileLineDict[PcdPair] = FileLineTuple\r
805\r
806 self.__WipeOffArea.append(((SetLine, SetOffset), (self.CurrentLineNumber - 1, self.CurrentOffsetWithinLine - 1)))\r
30fdf114
LG
807 elif self.__Token in ('!ifdef', '!ifndef', '!if'):\r
808 IfStartPos = (self.CurrentLineNumber - 1, self.CurrentOffsetWithinLine - len(self.__Token))\r
809 IfList.append([IfStartPos, None, None])\r
0d2711a6 810\r
30fdf114 811 CondLabel = self.__Token\r
0d2711a6 812 Expression = self.__GetExpression()\r
f7496d71 813\r
30fdf114 814 if CondLabel == '!if':\r
0d2711a6 815 ConditionSatisfied = self.__EvaluateConditional(Expression, IfList[-1][0][0] + 1, 'eval')\r
30fdf114 816 else:\r
0d2711a6
LG
817 ConditionSatisfied = self.__EvaluateConditional(Expression, IfList[-1][0][0] + 1, 'in')\r
818 if CondLabel == '!ifndef':\r
30fdf114 819 ConditionSatisfied = not ConditionSatisfied\r
30fdf114 820\r
0d2711a6
LG
821 BranchDetermined = ConditionSatisfied\r
822 IfList[-1] = [IfList[-1][0], ConditionSatisfied, BranchDetermined]\r
823 if ConditionSatisfied:\r
f7496d71 824 self.__WipeOffArea.append((IfList[-1][0], (self.CurrentLineNumber - 1, self.CurrentOffsetWithinLine - 1)))\r
30fdf114
LG
825 elif self.__Token in ('!elseif', '!else'):\r
826 ElseStartPos = (self.CurrentLineNumber - 1, self.CurrentOffsetWithinLine - len(self.__Token))\r
827 if len(IfList) <= 0:\r
828 raise Warning("Missing !if statement", self.FileName, self.CurrentLineNumber)\r
0d2711a6 829\r
30fdf114
LG
830 if IfList[-1][1]:\r
831 IfList[-1] = [ElseStartPos, False, True]\r
832 self.__WipeOffArea.append((ElseStartPos, (self.CurrentLineNumber - 1, self.CurrentOffsetWithinLine - 1)))\r
833 else:\r
834 self.__WipeOffArea.append((IfList[-1][0], ElseStartPos))\r
835 IfList[-1] = [ElseStartPos, True, IfList[-1][2]]\r
836 if self.__Token == '!elseif':\r
0d2711a6
LG
837 Expression = self.__GetExpression()\r
838 ConditionSatisfied = self.__EvaluateConditional(Expression, IfList[-1][0][0] + 1, 'eval')\r
30fdf114
LG
839 IfList[-1] = [IfList[-1][0], ConditionSatisfied, IfList[-1][2]]\r
840\r
841 if IfList[-1][1]:\r
842 if IfList[-1][2]:\r
843 IfList[-1][1] = False\r
844 else:\r
845 IfList[-1][2] = True\r
846 self.__WipeOffArea.append((IfList[-1][0], (self.CurrentLineNumber - 1, self.CurrentOffsetWithinLine - 1)))\r
30fdf114 847 elif self.__Token == '!endif':\r
d0acc87a
LG
848 if len(IfList) <= 0:\r
849 raise Warning("Missing !if statement", self.FileName, self.CurrentLineNumber)\r
30fdf114
LG
850 if IfList[-1][1]:\r
851 self.__WipeOffArea.append(((self.CurrentLineNumber - 1, self.CurrentOffsetWithinLine - len('!endif')), (self.CurrentLineNumber - 1, self.CurrentOffsetWithinLine - 1)))\r
852 else:\r
853 self.__WipeOffArea.append((IfList[-1][0], (self.CurrentLineNumber - 1, self.CurrentOffsetWithinLine - 1)))\r
854\r
855 IfList.pop()\r
0d2711a6
LG
856 elif not IfList: # Don't use PCDs inside conditional directive\r
857 if self.CurrentLineNumber <= RegionLayoutLine:\r
858 # Don't try the same line twice\r
859 continue\r
64b2609f
LG
860 SetPcd = ShortcutPcdPattern.match(self.Profile.FileLinesList[self.CurrentLineNumber - 1])\r
861 if SetPcd:\r
862 self.__PcdDict[SetPcd.group('name')] = SetPcd.group('value')\r
863 RegionLayoutLine = self.CurrentLineNumber\r
864 continue\r
0d2711a6
LG
865 RegionSize = RegionSizePattern.match(self.Profile.FileLinesList[self.CurrentLineNumber - 1])\r
866 if not RegionSize:\r
867 RegionLayoutLine = self.CurrentLineNumber\r
868 continue\r
869 RegionSizeGuid = RegionSizeGuidPattern.match(self.Profile.FileLinesList[self.CurrentLineNumber])\r
870 if not RegionSizeGuid:\r
871 RegionLayoutLine = self.CurrentLineNumber + 1\r
872 continue\r
d0acc87a
LG
873 self.__PcdDict[RegionSizeGuid.group('base')] = RegionSize.group('base')\r
874 self.__PcdDict[RegionSizeGuid.group('size')] = RegionSize.group('size')\r
0d2711a6
LG
875 RegionLayoutLine = self.CurrentLineNumber + 1\r
876\r
877 if IfList:\r
30fdf114
LG
878 raise Warning("Missing !endif", self.FileName, self.CurrentLineNumber)\r
879 self.Rewind()\r
880\r
d0acc87a
LG
881 def __CollectMacroPcd(self):\r
882 MacroDict = {}\r
883\r
884 # PCD macro\r
64b2609f 885 MacroDict.update(GlobalData.gPlatformPcds)\r
d0acc87a
LG
886 MacroDict.update(self.__PcdDict)\r
887\r
888 # Lowest priority\r
889 MacroDict.update(GlobalData.gPlatformDefines)\r
890\r
891 if self.__CurSection:\r
892 # Defines macro\r
55c84777 893 ScopeMacro = self.__MacroDict[TAB_COMMON, TAB_COMMON, TAB_COMMON]\r
d0acc87a
LG
894 if ScopeMacro:\r
895 MacroDict.update(ScopeMacro)\r
f7496d71 896\r
d0acc87a
LG
897 # Section macro\r
898 ScopeMacro = self.__MacroDict[\r
899 self.__CurSection[0],\r
900 self.__CurSection[1],\r
901 self.__CurSection[2]\r
902 ]\r
903 if ScopeMacro:\r
904 MacroDict.update(ScopeMacro)\r
905\r
906 MacroDict.update(GlobalData.gGlobalDefines)\r
907 MacroDict.update(GlobalData.gCommandLineDefines)\r
1fa7fdf6
FY
908 if GlobalData.BuildOptionPcd:\r
909 for Item in GlobalData.BuildOptionPcd:\r
0d1f5b2b 910 if isinstance(Item, tuple):\r
705ed563 911 continue\r
1fa7fdf6 912 PcdName, TmpValue = Item.split("=")\r
8565b582 913 TmpValue = BuildOptionValue(TmpValue, {})\r
1fa7fdf6 914 MacroDict[PcdName.strip()] = TmpValue\r
d0acc87a
LG
915 # Highest priority\r
916\r
917 return MacroDict\r
918\r
0d2711a6 919 def __EvaluateConditional(self, Expression, Line, Op = None, Value = None):\r
d0acc87a 920 MacroPcdDict = self.__CollectMacroPcd()\r
0d2711a6
LG
921 if Op == 'eval':\r
922 try:\r
d0acc87a
LG
923 if Value:\r
924 return ValueExpression(Expression, MacroPcdDict)(True)\r
925 else:\r
926 return ValueExpression(Expression, MacroPcdDict)()\r
5b0671c1 927 except WrnExpression as Excpt:\r
f7496d71 928 #\r
0d2711a6
LG
929 # Catch expression evaluation warning here. We need to report\r
930 # the precise number of line and return the evaluation result\r
931 #\r
932 EdkLogger.warn('Parser', "Suspicious expression: %s" % str(Excpt),\r
f7496d71 933 File=self.FileName, ExtraData=self.__CurrentLine(),\r
0d2711a6
LG
934 Line=Line)\r
935 return Excpt.result\r
5b0671c1 936 except Exception as Excpt:\r
64b2609f
LG
937 if hasattr(Excpt, 'Pcd'):\r
938 if Excpt.Pcd in GlobalData.gPlatformOtherPcds:\r
939 Info = GlobalData.gPlatformOtherPcds[Excpt.Pcd]\r
940 raise Warning("Cannot use this PCD (%s) in an expression as"\r
941 " it must be defined in a [PcdsFixedAtBuild] or [PcdsFeatureFlag] section"\r
942 " of the DSC file (%s), and it is currently defined in this section:"\r
943 " %s, line #: %d." % (Excpt.Pcd, GlobalData.gPlatformOtherPcds['DSCFILE'], Info[0], Info[1]),\r
20274d23 944 self.FileName, Line)\r
64b2609f
LG
945 else:\r
946 raise Warning("PCD (%s) is not defined in DSC file (%s)" % (Excpt.Pcd, GlobalData.gPlatformOtherPcds['DSCFILE']),\r
20274d23 947 self.FileName, Line)\r
64b2609f 948 else:\r
20274d23 949 raise Warning(str(Excpt), self.FileName, Line)\r
0d2711a6
LG
950 else:\r
951 if Expression.startswith('$(') and Expression[-1] == ')':\r
f7496d71 952 Expression = Expression[2:-1]\r
d0acc87a 953 return Expression in MacroPcdDict\r
30fdf114
LG
954\r
955 ## __IsToken() method\r
956 #\r
957 # Check whether input string is found from current char position along\r
958 # If found, the string value is put into self.__Token\r
959 #\r
960 # @param self The object pointer\r
961 # @param String The string to search\r
962 # @param IgnoreCase Indicate case sensitive/non-sensitive search, default is case sensitive\r
963 # @retval True Successfully find string, file buffer pointer moved forward\r
964 # @retval False Not able to find string, file buffer pointer not changed\r
965 #\r
966 def __IsToken(self, String, IgnoreCase = False):\r
967 self.__SkipWhiteSpace()\r
968\r
969 # Only consider the same line, no multi-line token allowed\r
970 StartPos = self.CurrentOffsetWithinLine\r
971 index = -1\r
972 if IgnoreCase:\r
973 index = self.__CurrentLine()[self.CurrentOffsetWithinLine : ].upper().find(String.upper())\r
974 else:\r
975 index = self.__CurrentLine()[self.CurrentOffsetWithinLine : ].find(String)\r
976 if index == 0:\r
977 self.CurrentOffsetWithinLine += len(String)\r
978 self.__Token = self.__CurrentLine()[StartPos : self.CurrentOffsetWithinLine]\r
979 return True\r
980 return False\r
981\r
982 ## __IsKeyword() method\r
983 #\r
984 # Check whether input keyword is found from current char position along, whole word only!\r
985 # If found, the string value is put into self.__Token\r
986 #\r
987 # @param self The object pointer\r
988 # @param Keyword The string to search\r
989 # @param IgnoreCase Indicate case sensitive/non-sensitive search, default is case sensitive\r
990 # @retval True Successfully find string, file buffer pointer moved forward\r
991 # @retval False Not able to find string, file buffer pointer not changed\r
992 #\r
993 def __IsKeyword(self, KeyWord, IgnoreCase = False):\r
994 self.__SkipWhiteSpace()\r
995\r
996 # Only consider the same line, no multi-line token allowed\r
997 StartPos = self.CurrentOffsetWithinLine\r
998 index = -1\r
999 if IgnoreCase:\r
1000 index = self.__CurrentLine()[self.CurrentOffsetWithinLine : ].upper().find(KeyWord.upper())\r
1001 else:\r
1002 index = self.__CurrentLine()[self.CurrentOffsetWithinLine : ].find(KeyWord)\r
1003 if index == 0:\r
1004 followingChar = self.__CurrentLine()[self.CurrentOffsetWithinLine + len(KeyWord)]\r
1005 if not str(followingChar).isspace() and followingChar not in SEPERATOR_TUPLE:\r
1006 return False\r
1007 self.CurrentOffsetWithinLine += len(KeyWord)\r
1008 self.__Token = self.__CurrentLine()[StartPos : self.CurrentOffsetWithinLine]\r
1009 return True\r
1010 return False\r
1011\r
0d2711a6
LG
1012 def __GetExpression(self):\r
1013 Line = self.Profile.FileLinesList[self.CurrentLineNumber - 1]\r
1014 Index = len(Line) - 1\r
1015 while Line[Index] in ['\r', '\n']:\r
1016 Index -= 1\r
1017 ExpressionString = self.Profile.FileLinesList[self.CurrentLineNumber - 1][self.CurrentOffsetWithinLine:Index+1]\r
1018 self.CurrentOffsetWithinLine += len(ExpressionString)\r
1019 ExpressionString = ExpressionString.strip()\r
1020 return ExpressionString\r
1021\r
30fdf114
LG
1022 ## __GetNextWord() method\r
1023 #\r
1024 # Get next C name from file lines\r
1025 # If found, the string value is put into self.__Token\r
1026 #\r
1027 # @param self The object pointer\r
1028 # @retval True Successfully find a C name string, file buffer pointer moved forward\r
1029 # @retval False Not able to find a C name string, file buffer pointer not changed\r
1030 #\r
1031 def __GetNextWord(self):\r
1032 self.__SkipWhiteSpace()\r
1033 if self.__EndOfFile():\r
1034 return False\r
1035\r
1036 TempChar = self.__CurrentChar()\r
1037 StartPos = self.CurrentOffsetWithinLine\r
1038 if (TempChar >= 'a' and TempChar <= 'z') or (TempChar >= 'A' and TempChar <= 'Z') or TempChar == '_':\r
1039 self.__GetOneChar()\r
1040 while not self.__EndOfLine():\r
1041 TempChar = self.__CurrentChar()\r
1042 if (TempChar >= 'a' and TempChar <= 'z') or (TempChar >= 'A' and TempChar <= 'Z') \\r
1043 or (TempChar >= '0' and TempChar <= '9') or TempChar == '_' or TempChar == '-':\r
1044 self.__GetOneChar()\r
1045\r
1046 else:\r
1047 break\r
1048\r
1049 self.__Token = self.__CurrentLine()[StartPos : self.CurrentOffsetWithinLine]\r
1050 return True\r
1051\r
1052 return False\r
1053\r
543f5ac3
B
1054 def __GetNextPcdWord(self):\r
1055 self.__SkipWhiteSpace()\r
1056 if self.__EndOfFile():\r
1057 return False\r
1058\r
1059 TempChar = self.__CurrentChar()\r
1060 StartPos = self.CurrentOffsetWithinLine\r
1061 if (TempChar >= 'a' and TempChar <= 'z') or (TempChar >= 'A' and TempChar <= 'Z') or TempChar == '_' or TempChar == '[' or TempChar == ']':\r
1062 self.__GetOneChar()\r
1063 while not self.__EndOfLine():\r
1064 TempChar = self.__CurrentChar()\r
1065 if (TempChar >= 'a' and TempChar <= 'z') or (TempChar >= 'A' and TempChar <= 'Z') \\r
1066 or (TempChar >= '0' and TempChar <= '9') or TempChar == '_' or TempChar == '-' or TempChar == '[' or TempChar == ']':\r
1067 self.__GetOneChar()\r
1068\r
1069 else:\r
1070 break\r
1071\r
1072 self.__Token = self.__CurrentLine()[StartPos : self.CurrentOffsetWithinLine]\r
1073 return True\r
1074\r
1075 return False\r
1076\r
30fdf114
LG
1077 ## __GetNextToken() method\r
1078 #\r
1079 # Get next token unit before a seperator\r
1080 # If found, the string value is put into self.__Token\r
1081 #\r
1082 # @param self The object pointer\r
1083 # @retval True Successfully find a token unit, file buffer pointer moved forward\r
1084 # @retval False Not able to find a token unit, file buffer pointer not changed\r
1085 #\r
1086 def __GetNextToken(self):\r
1087 # Skip leading spaces, if exist.\r
1088 self.__SkipWhiteSpace()\r
1089 if self.__EndOfFile():\r
1090 return False\r
1091 # Record the token start position, the position of the first non-space char.\r
1092 StartPos = self.CurrentOffsetWithinLine\r
1093 StartLine = self.CurrentLineNumber\r
d0acc87a 1094 while StartLine == self.CurrentLineNumber:\r
30fdf114
LG
1095 TempChar = self.__CurrentChar()\r
1096 # Try to find the end char that is not a space and not in seperator tuple.\r
1097 # That is, when we got a space or any char in the tuple, we got the end of token.\r
1098 if not str(TempChar).isspace() and TempChar not in SEPERATOR_TUPLE:\r
1099 self.__GetOneChar()\r
1100 # if we happen to meet a seperator as the first char, we must proceed to get it.\r
1101 # That is, we get a token that is a seperator char. nomally it is the boundary of other tokens.\r
1102 elif StartPos == self.CurrentOffsetWithinLine and TempChar in SEPERATOR_TUPLE:\r
1103 self.__GetOneChar()\r
1104 break\r
1105 else:\r
1106 break\r
1107# else:\r
1108# return False\r
1109\r
1110 EndPos = self.CurrentOffsetWithinLine\r
1111 if self.CurrentLineNumber != StartLine:\r
1112 EndPos = len(self.Profile.FileLinesList[StartLine-1])\r
1113 self.__Token = self.Profile.FileLinesList[StartLine-1][StartPos : EndPos]\r
1114 if StartPos != self.CurrentOffsetWithinLine:\r
1115 return True\r
1116 else:\r
1117 return False\r
1118\r
1119 def __GetNextOp(self):\r
1120 # Skip leading spaces, if exist.\r
1121 self.__SkipWhiteSpace()\r
1122 if self.__EndOfFile():\r
1123 return False\r
1124 # Record the token start position, the position of the first non-space char.\r
1125 StartPos = self.CurrentOffsetWithinLine\r
1126 while not self.__EndOfLine():\r
1127 TempChar = self.__CurrentChar()\r
1128 # Try to find the end char that is not a space\r
1129 if not str(TempChar).isspace():\r
1130 self.__GetOneChar()\r
1131 else:\r
1132 break\r
1133 else:\r
1134 return False\r
1135\r
1136 if StartPos != self.CurrentOffsetWithinLine:\r
1137 self.__Token = self.__CurrentLine()[StartPos : self.CurrentOffsetWithinLine]\r
1138 return True\r
1139 else:\r
1140 return False\r
1141 ## __GetNextGuid() method\r
1142 #\r
1143 # Get next token unit before a seperator\r
1144 # If found, the GUID string is put into self.__Token\r
1145 #\r
1146 # @param self The object pointer\r
1147 # @retval True Successfully find a registry format GUID, file buffer pointer moved forward\r
1148 # @retval False Not able to find a registry format GUID, file buffer pointer not changed\r
1149 #\r
1150 def __GetNextGuid(self):\r
1151\r
1152 if not self.__GetNextToken():\r
1153 return False\r
4231a819 1154 if gGuidPattern.match(self.__Token) is not None:\r
30fdf114
LG
1155 return True\r
1156 else:\r
1157 self.__UndoToken()\r
1158 return False\r
1159\r
227dbb11
CJ
1160 @staticmethod\r
1161 def __Verify(Name, Value, Scope):\r
bff74750 1162 # value verification only applies to numeric values.\r
053cd183 1163 if Scope not in TAB_PCD_NUMERIC_TYPES:\r
bff74750
CJ
1164 return\r
1165\r
1166 ValueNumber = 0\r
1167 try:\r
1168 ValueNumber = int(Value, 0)\r
1169 except:\r
1170 EdkLogger.error("FdfParser", FORMAT_INVALID, "The value is not valid dec or hex number for %s." % Name)\r
1171 if ValueNumber < 0:\r
1172 EdkLogger.error("FdfParser", FORMAT_INVALID, "The value can't be set to negative value for %s." % Name)\r
1173 if ValueNumber > MAX_VAL_TYPE[Scope]:\r
1174 EdkLogger.error("FdfParser", FORMAT_INVALID, "Too large value for %s." % Name)\r
1175 return True\r
91ae2988 1176\r
30fdf114
LG
1177 ## __UndoToken() method\r
1178 #\r
1179 # Go back one token unit in file buffer\r
1180 #\r
1181 # @param self The object pointer\r
1182 #\r
1183 def __UndoToken(self):\r
1184 self.__UndoOneChar()\r
1185 while self.__CurrentChar().isspace():\r
1186 if not self.__UndoOneChar():\r
1187 self.__GetOneChar()\r
1188 return\r
1189\r
1190\r
1191 StartPos = self.CurrentOffsetWithinLine\r
1192 CurrentLine = self.CurrentLineNumber\r
1193 while CurrentLine == self.CurrentLineNumber:\r
1194\r
1195 TempChar = self.__CurrentChar()\r
1196 # Try to find the end char that is not a space and not in seperator tuple.\r
1197 # That is, when we got a space or any char in the tuple, we got the end of token.\r
1198 if not str(TempChar).isspace() and not TempChar in SEPERATOR_TUPLE:\r
1199 if not self.__UndoOneChar():\r
d0acc87a 1200 return\r
30fdf114
LG
1201 # if we happen to meet a seperator as the first char, we must proceed to get it.\r
1202 # That is, we get a token that is a seperator char. nomally it is the boundary of other tokens.\r
1203 elif StartPos == self.CurrentOffsetWithinLine and TempChar in SEPERATOR_TUPLE:\r
1204 return\r
1205 else:\r
1206 break\r
1207\r
1208 self.__GetOneChar()\r
1209\r
30fdf114
LG
1210 ## __GetNextHexNumber() method\r
1211 #\r
1212 # Get next HEX data before a seperator\r
1213 # If found, the HEX data is put into self.__Token\r
1214 #\r
1215 # @param self The object pointer\r
1216 # @retval True Successfully find a HEX data, file buffer pointer moved forward\r
1217 # @retval False Not able to find a HEX data, file buffer pointer not changed\r
1218 #\r
1219 def __GetNextHexNumber(self):\r
1220 if not self.__GetNextToken():\r
1221 return False\r
6553c617 1222 if gHexPatternAll.match(self.__Token):\r
30fdf114
LG
1223 return True\r
1224 else:\r
1225 self.__UndoToken()\r
1226 return False\r
1227\r
1228 ## __GetNextDecimalNumber() method\r
1229 #\r
1230 # Get next decimal data before a seperator\r
1231 # If found, the decimal data is put into self.__Token\r
1232 #\r
1233 # @param self The object pointer\r
1234 # @retval True Successfully find a decimal data, file buffer pointer moved forward\r
1235 # @retval False Not able to find a decimal data, file buffer pointer not changed\r
1236 #\r
1237 def __GetNextDecimalNumber(self):\r
1238 if not self.__GetNextToken():\r
1239 return False\r
1240 if self.__Token.isdigit():\r
1241 return True\r
1242 else:\r
1243 self.__UndoToken()\r
1244 return False\r
1245\r
1246 ## __GetNextPcdName() method\r
1247 #\r
1248 # Get next PCD token space C name and PCD C name pair before a seperator\r
1249 # If found, the decimal data is put into self.__Token\r
1250 #\r
1251 # @param self The object pointer\r
1252 # @retval Tuple PCD C name and PCD token space C name pair\r
1253 #\r
1254 def __GetNextPcdName(self):\r
1255 if not self.__GetNextWord():\r
1256 raise Warning("expected format of <PcdTokenSpaceCName>.<PcdCName>", self.FileName, self.CurrentLineNumber)\r
1257 pcdTokenSpaceCName = self.__Token\r
1258\r
1259 if not self.__IsToken( "."):\r
1260 raise Warning("expected format of <PcdTokenSpaceCName>.<PcdCName>", self.FileName, self.CurrentLineNumber)\r
1261\r
1262 if not self.__GetNextWord():\r
1263 raise Warning("expected format of <PcdTokenSpaceCName>.<PcdCName>", self.FileName, self.CurrentLineNumber)\r
1264 pcdCName = self.__Token\r
1265\r
1266 return (pcdCName, pcdTokenSpaceCName)\r
1267\r
543f5ac3
B
1268 def __GetNextPcdSettings(self):\r
1269 if not self.__GetNextWord():\r
1270 raise Warning("expected format of <PcdTokenSpaceCName>.<PcdCName>", self.FileName, self.CurrentLineNumber)\r
1271 pcdTokenSpaceCName = self.__Token\r
1272\r
1273 if not self.__IsToken( "."):\r
1274 raise Warning("expected format of <PcdTokenSpaceCName>.<PcdCName>", self.FileName, self.CurrentLineNumber)\r
1275\r
1276 if not self.__GetNextWord():\r
1277 raise Warning("expected format of <PcdTokenSpaceCName>.<PcdCName>", self.FileName, self.CurrentLineNumber)\r
1278 pcdCName = self.__Token\r
1279\r
1280 Fields = []\r
1281 while self.__IsToken("."):\r
1282 if not self.__GetNextPcdWord():\r
1283 raise Warning("expected format of <PcdTokenSpaceCName>.<PcdCName>", self.FileName, self.CurrentLineNumber)\r
1284 Fields.append(self.__Token)\r
1285\r
1286 return (pcdCName, pcdTokenSpaceCName,".".join(Fields))\r
1287\r
30fdf114
LG
1288 ## __GetStringData() method\r
1289 #\r
1290 # Get string contents quoted in ""\r
1291 # If found, the decimal data is put into self.__Token\r
1292 #\r
1293 # @param self The object pointer\r
1294 # @retval True Successfully find a string data, file buffer pointer moved forward\r
1295 # @retval False Not able to find a string data, file buffer pointer not changed\r
1296 #\r
1297 def __GetStringData(self):\r
1298 if self.__Token.startswith("\"") or self.__Token.startswith("L\""):\r
1299 self.__UndoToken()\r
1300 self.__SkipToToken("\"")\r
1301 currentLineNumber = self.CurrentLineNumber\r
1302\r
1303 if not self.__SkipToToken("\""):\r
1304 raise Warning("Missing Quote \" for String", self.FileName, self.CurrentLineNumber)\r
1305 if currentLineNumber != self.CurrentLineNumber:\r
1306 raise Warning("Missing Quote \" for String", self.FileName, self.CurrentLineNumber)\r
1307 self.__Token = self.__SkippedChars.rstrip('\"')\r
1308 return True\r
1309\r
1310 elif self.__Token.startswith("\'") or self.__Token.startswith("L\'"):\r
1311 self.__UndoToken()\r
1312 self.__SkipToToken("\'")\r
1313 currentLineNumber = self.CurrentLineNumber\r
1314\r
1315 if not self.__SkipToToken("\'"):\r
1316 raise Warning("Missing Quote \' for String", self.FileName, self.CurrentLineNumber)\r
1317 if currentLineNumber != self.CurrentLineNumber:\r
1318 raise Warning("Missing Quote \' for String", self.FileName, self.CurrentLineNumber)\r
1319 self.__Token = self.__SkippedChars.rstrip('\'')\r
1320 return True\r
1321\r
1322 else:\r
1323 return False\r
1324\r
1325 ## __SkipToToken() method\r
1326 #\r
1327 # Search forward in file buffer for the string\r
1328 # The skipped chars are put into self.__SkippedChars\r
1329 #\r
1330 # @param self The object pointer\r
1331 # @param String The string to search\r
1332 # @param IgnoreCase Indicate case sensitive/non-sensitive search, default is case sensitive\r
1333 # @retval True Successfully find the string, file buffer pointer moved forward\r
1334 # @retval False Not able to find the string, file buffer pointer not changed\r
1335 #\r
1336 def __SkipToToken(self, String, IgnoreCase = False):\r
1337 StartPos = self.GetFileBufferPos()\r
1338\r
1339 self.__SkippedChars = ""\r
1340 while not self.__EndOfFile():\r
1341 index = -1\r
1342 if IgnoreCase:\r
1343 index = self.__CurrentLine()[self.CurrentOffsetWithinLine : ].upper().find(String.upper())\r
1344 else:\r
1345 index = self.__CurrentLine()[self.CurrentOffsetWithinLine : ].find(String)\r
1346 if index == 0:\r
1347 self.CurrentOffsetWithinLine += len(String)\r
1348 self.__SkippedChars += String\r
1349 return True\r
1350 self.__SkippedChars += str(self.__CurrentChar())\r
1351 self.__GetOneChar()\r
1352\r
1353 self.SetFileBufferPos( StartPos)\r
1354 self.__SkippedChars = ""\r
1355 return False\r
1356\r
1357 ## GetFileBufferPos() method\r
1358 #\r
1359 # Return the tuple of current line and offset within the line\r
1360 #\r
1361 # @param self The object pointer\r
1362 # @retval Tuple Line number and offset pair\r
1363 #\r
1364 def GetFileBufferPos(self):\r
1365 return (self.CurrentLineNumber, self.CurrentOffsetWithinLine)\r
1366\r
1367 ## SetFileBufferPos() method\r
1368 #\r
1369 # Restore the file buffer position\r
1370 #\r
1371 # @param self The object pointer\r
1372 # @param Pos The new file buffer position\r
1373 #\r
1374 def SetFileBufferPos(self, Pos):\r
1375 (self.CurrentLineNumber, self.CurrentOffsetWithinLine) = Pos\r
1376\r
d40b2ee6
LG
1377 ## Preprocess() method\r
1378 #\r
1379 # Preprocess comment, conditional directive, include directive, replace macro.\r
1380 # Exception will be raised if syntax error found\r
1381 #\r
1382 # @param self The object pointer\r
1383 #\r
1384 def Preprocess(self):\r
1385 self.__StringToList()\r
1386 self.PreprocessFile()\r
1387 self.PreprocessIncludeFile()\r
1388 self.__StringToList()\r
1389 self.PreprocessFile()\r
1390 self.PreprocessConditionalStatement()\r
1391 self.__StringToList()\r
1392 for Pos in self.__WipeOffArea:\r
1393 self.__ReplaceFragment(Pos[0], Pos[1])\r
1394 self.Profile.FileLinesList = ["".join(list) for list in self.Profile.FileLinesList]\r
1395\r
1396 while self.__GetDefines():\r
1397 pass\r
d40b2ee6 1398\r
30fdf114
LG
1399 ## ParseFile() method\r
1400 #\r
1401 # Parse the file profile buffer to extract fd, fv ... information\r
1402 # Exception will be raised if syntax error found\r
1403 #\r
1404 # @param self The object pointer\r
1405 #\r
1406 def ParseFile(self):\r
1407\r
1408 try:\r
d40b2ee6 1409 self.Preprocess()\r
09ef8e92 1410 self.__GetError()\r
dd170333
MK
1411 #\r
1412 # Keep processing sections of the FDF until no new sections or a syntax error is found\r
1413 #\r
1414 while self.__GetFd() or self.__GetFv() or self.__GetFmp() or self.__GetCapsule() or self.__GetVtf() or self.__GetRule() or self.__GetOptionRom():\r
30fdf114
LG
1415 pass\r
1416\r
5b0671c1 1417 except Warning as X:\r
30fdf114 1418 self.__UndoToken()\r
30fdf114 1419 #'\n\tGot Token: \"%s\" from File %s\n' % (self.__Token, FileLineTuple[0]) + \\r
118bf096
CS
1420 # At this point, the closest parent would be the included file itself\r
1421 Profile = GetParentAtLine(X.OriginalLineNumber)\r
4231a819 1422 if Profile is not None:\r
118bf096
CS
1423 X.Message += ' near line %d, column %d: %s' \\r
1424 % (X.LineNumber, 0, Profile.FileLinesList[X.LineNumber-1])\r
1425 else:\r
1426 FileLineTuple = GetRealFileLine(self.FileName, self.CurrentLineNumber)\r
1427 X.Message += ' near line %d, column %d: %s' \\r
30fdf114
LG
1428 % (FileLineTuple[1], self.CurrentOffsetWithinLine + 1, self.Profile.FileLinesList[self.CurrentLineNumber - 1][self.CurrentOffsetWithinLine :].rstrip('\n').rstrip('\r'))\r
1429 raise\r
1430\r
df81077f
YZ
1431 ## SectionParser() method\r
1432 #\r
1433 # Parse the file section info\r
1434 # Exception will be raised if syntax error found\r
1435 #\r
1436 # @param self The object pointer\r
1437 # @param section The section string\r
1438\r
1439 def SectionParser(self, section):\r
1440 S = section.upper()\r
1441 if not S.startswith("[DEFINES") and not S.startswith("[FD.") and not S.startswith("[FV.") and not S.startswith("[CAPSULE.") \\r
1442 and not S.startswith("[VTF.") and not S.startswith("[RULE.") and not S.startswith("[OPTIONROM.") and not S.startswith('[FMPPAYLOAD.'):\r
1443 raise Warning("Unknown section or section appear sequence error (The correct sequence should be [DEFINES], [FD.], [FV.], [Capsule.], [VTF.], [Rule.], [OptionRom.], [FMPPAYLOAD.])", self.FileName, self.CurrentLineNumber)\r
1444\r
30fdf114
LG
1445 ## __GetDefines() method\r
1446 #\r
1447 # Get Defines section contents and store its data into AllMacrosList\r
1448 #\r
1449 # @param self The object pointer\r
1450 # @retval True Successfully find a Defines\r
1451 # @retval False Not able to find a Defines\r
1452 #\r
1453 def __GetDefines(self):\r
1454\r
1455 if not self.__GetNextToken():\r
1456 return False\r
1457\r
1458 S = self.__Token.upper()\r
1459 if S.startswith("[") and not S.startswith("[DEFINES"):\r
df81077f 1460 self.SectionParser(S)\r
30fdf114
LG
1461 self.__UndoToken()\r
1462 return False\r
1463\r
1464 self.__UndoToken()\r
1465 if not self.__IsToken("[DEFINES", True):\r
1466 FileLineTuple = GetRealFileLine(self.FileName, self.CurrentLineNumber)\r
1467 #print 'Parsing String: %s in File %s, At line: %d, Offset Within Line: %d' \\r
1468 # % (self.Profile.FileLinesList[self.CurrentLineNumber - 1][self.CurrentOffsetWithinLine :], FileLineTuple[0], FileLineTuple[1], self.CurrentOffsetWithinLine)\r
1469 raise Warning("expected [DEFINES", self.FileName, self.CurrentLineNumber)\r
1470\r
1471 if not self.__IsToken( "]"):\r
1472 raise Warning("expected ']'", self.FileName, self.CurrentLineNumber)\r
1473\r
1474 while self.__GetNextWord():\r
6780eef1
LG
1475 # handle the SET statement\r
1476 if self.__Token == 'SET':\r
1477 self.__UndoToken()\r
1478 self.__GetSetStatement(None)\r
1479 continue\r
f7496d71 1480\r
30fdf114 1481 Macro = self.__Token\r
f7496d71 1482\r
30fdf114
LG
1483 if not self.__IsToken("="):\r
1484 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
1485 if not self.__GetNextToken() or self.__Token.startswith('['):\r
1486 raise Warning("expected MACRO value", self.FileName, self.CurrentLineNumber)\r
1487 Value = self.__Token\r
30fdf114
LG
1488\r
1489 return False\r
1490\r
09ef8e92
YF
1491 ##__GetError() method\r
1492 def __GetError(self):\r
1493 #save the Current information\r
1494 CurrentLine = self.CurrentLineNumber\r
1495 CurrentOffset = self.CurrentOffsetWithinLine\r
1496 while self.__GetNextToken():\r
1497 if self.__Token == TAB_ERROR:\r
1498 EdkLogger.error('FdfParser', ERROR_STATEMENT, self.__CurrentLine().replace(TAB_ERROR, '', 1), File=self.FileName, Line=self.CurrentLineNumber)\r
1499 self.CurrentLineNumber = CurrentLine\r
1500 self.CurrentOffsetWithinLine = CurrentOffset\r
1501\r
30fdf114
LG
1502 ## __GetFd() method\r
1503 #\r
1504 # Get FD section contents and store its data into FD dictionary of self.Profile\r
1505 #\r
1506 # @param self The object pointer\r
1507 # @retval True Successfully find a FD\r
1508 # @retval False Not able to find a FD\r
1509 #\r
1510 def __GetFd(self):\r
1511\r
1512 if not self.__GetNextToken():\r
1513 return False\r
1514\r
1515 S = self.__Token.upper()\r
1516 if S.startswith("[") and not S.startswith("[FD."):\r
a3251d84 1517 if not S.startswith("[FV.") and not S.startswith('[FMPPAYLOAD.') and not S.startswith("[CAPSULE.") \\r
30fdf114
LG
1518 and not S.startswith("[VTF.") and not S.startswith("[RULE.") and not S.startswith("[OPTIONROM."):\r
1519 raise Warning("Unknown section", self.FileName, self.CurrentLineNumber)\r
1520 self.__UndoToken()\r
1521 return False\r
1522\r
1523 self.__UndoToken()\r
1524 if not self.__IsToken("[FD.", True):\r
1525 FileLineTuple = GetRealFileLine(self.FileName, self.CurrentLineNumber)\r
1526 #print 'Parsing String: %s in File %s, At line: %d, Offset Within Line: %d' \\r
1527 # % (self.Profile.FileLinesList[self.CurrentLineNumber - 1][self.CurrentOffsetWithinLine :], FileLineTuple[0], FileLineTuple[1], self.CurrentOffsetWithinLine)\r
1528 raise Warning("expected [FD.]", self.FileName, self.CurrentLineNumber)\r
1529\r
1530 FdName = self.__GetUiName()\r
52302d4d
LG
1531 if FdName == "":\r
1532 if len (self.Profile.FdDict) == 0:\r
1533 FdName = GenFdsGlobalVariable.PlatformName\r
d0acc87a
LG
1534 if FdName == "" and GlobalData.gActivePlatform:\r
1535 FdName = GlobalData.gActivePlatform.PlatformName\r
52302d4d
LG
1536 self.Profile.FdNameNotSet = True\r
1537 else:\r
1538 raise Warning("expected FdName in [FD.] section", self.FileName, self.CurrentLineNumber)\r
30fdf114 1539 self.CurrentFdName = FdName.upper()\r
f7496d71 1540\r
52302d4d
LG
1541 if self.CurrentFdName in self.Profile.FdDict:\r
1542 raise Warning("Unexpected the same FD name", self.FileName, self.CurrentLineNumber)\r
30fdf114
LG
1543\r
1544 if not self.__IsToken( "]"):\r
1545 raise Warning("expected ']'", self.FileName, self.CurrentLineNumber)\r
1546\r
1547 FdObj = Fd.FD()\r
1548 FdObj.FdUiName = self.CurrentFdName\r
1549 self.Profile.FdDict[self.CurrentFdName] = FdObj\r
52302d4d
LG
1550\r
1551 if len (self.Profile.FdDict) > 1 and self.Profile.FdNameNotSet:\r
1552 raise Warning("expected all FDs have their name", self.FileName, self.CurrentLineNumber)\r
1553\r
30fdf114
LG
1554 Status = self.__GetCreateFile(FdObj)\r
1555 if not Status:\r
1556 raise Warning("FD name error", self.FileName, self.CurrentLineNumber)\r
1557\r
e8a47801
LG
1558 while self.__GetTokenStatements(FdObj):\r
1559 pass\r
1560 for Attr in ("BaseAddress", "Size", "ErasePolarity"):\r
4231a819 1561 if getattr(FdObj, Attr) is None:\r
e8a47801
LG
1562 self.__GetNextToken()\r
1563 raise Warning("Keyword %s missing" % Attr, self.FileName, self.CurrentLineNumber)\r
1564\r
1565 if not FdObj.BlockSizeList:\r
1566 FdObj.BlockSizeList.append((1, FdObj.Size, None))\r
30fdf114
LG
1567\r
1568 self.__GetDefineStatements(FdObj)\r
1569\r
1570 self.__GetSetStatements(FdObj)\r
1571\r
1572 if not self.__GetRegionLayout(FdObj):\r
1573 raise Warning("expected region layout", self.FileName, self.CurrentLineNumber)\r
1574\r
1575 while self.__GetRegionLayout(FdObj):\r
1576 pass\r
1577 return True\r
1578\r
1579 ## __GetUiName() method\r
1580 #\r
1581 # Return the UI name of a section\r
1582 #\r
1583 # @param self The object pointer\r
1584 # @retval FdName UI name\r
1585 #\r
1586 def __GetUiName(self):\r
1587 Name = ""\r
1588 if self.__GetNextWord():\r
1589 Name = self.__Token\r
1590\r
1591 return Name\r
1592\r
1593 ## __GetCreateFile() method\r
1594 #\r
1595 # Return the output file name of object\r
1596 #\r
1597 # @param self The object pointer\r
1598 # @param Obj object whose data will be stored in file\r
1599 # @retval FdName UI name\r
1600 #\r
1601 def __GetCreateFile(self, Obj):\r
1602\r
1603 if self.__IsKeyword( "CREATE_FILE"):\r
1604 if not self.__IsToken( "="):\r
1605 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
1606\r
1607 if not self.__GetNextToken():\r
1608 raise Warning("expected file name", self.FileName, self.CurrentLineNumber)\r
1609\r
1610 FileName = self.__Token\r
1611 Obj.CreateFileName = FileName\r
1612\r
1613 return True\r
1614\r
543f5ac3
B
1615 def SetPcdLocalation(self,pcdpair):\r
1616 self.Profile.PcdLocalDict[pcdpair] = (self.Profile.FileName,self.CurrentLineNumber)\r
1617\r
30fdf114
LG
1618 ## __GetTokenStatements() method\r
1619 #\r
1620 # Get token statements\r
1621 #\r
1622 # @param self The object pointer\r
1623 # @param Obj for whom token statement is got\r
30fdf114
LG
1624 #\r
1625 def __GetTokenStatements(self, Obj):\r
e8a47801
LG
1626 if self.__IsKeyword( "BaseAddress"):\r
1627 if not self.__IsToken( "="):\r
1628 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
f7496d71 1629\r
e8a47801
LG
1630 if not self.__GetNextHexNumber():\r
1631 raise Warning("expected Hex base address", self.FileName, self.CurrentLineNumber)\r
f7496d71 1632\r
e8a47801 1633 Obj.BaseAddress = self.__Token\r
f7496d71 1634\r
e8a47801 1635 if self.__IsToken( "|"):\r
543f5ac3 1636 pcdPair = self.__GetNextPcdSettings()\r
e8a47801
LG
1637 Obj.BaseAddressPcd = pcdPair\r
1638 self.Profile.PcdDict[pcdPair] = Obj.BaseAddress\r
543f5ac3 1639 self.SetPcdLocalation(pcdPair)\r
e8a47801
LG
1640 FileLineTuple = GetRealFileLine(self.FileName, self.CurrentLineNumber)\r
1641 self.Profile.PcdFileLineDict[pcdPair] = FileLineTuple\r
1642 return True\r
30fdf114 1643\r
e8a47801
LG
1644 if self.__IsKeyword( "Size"):\r
1645 if not self.__IsToken( "="):\r
1646 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
f7496d71 1647\r
e8a47801
LG
1648 if not self.__GetNextHexNumber():\r
1649 raise Warning("expected Hex size", self.FileName, self.CurrentLineNumber)\r
30fdf114 1650\r
e8a47801
LG
1651 Size = self.__Token\r
1652 if self.__IsToken( "|"):\r
543f5ac3 1653 pcdPair = self.__GetNextPcdSettings()\r
e8a47801
LG
1654 Obj.SizePcd = pcdPair\r
1655 self.Profile.PcdDict[pcdPair] = Size\r
543f5ac3 1656 self.SetPcdLocalation(pcdPair)\r
e8a47801
LG
1657 FileLineTuple = GetRealFileLine(self.FileName, self.CurrentLineNumber)\r
1658 self.Profile.PcdFileLineDict[pcdPair] = FileLineTuple\r
1659 Obj.Size = long(Size, 0)\r
1660 return True\r
30fdf114 1661\r
e8a47801
LG
1662 if self.__IsKeyword( "ErasePolarity"):\r
1663 if not self.__IsToken( "="):\r
1664 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
f7496d71 1665\r
e8a47801
LG
1666 if not self.__GetNextToken():\r
1667 raise Warning("expected Erase Polarity", self.FileName, self.CurrentLineNumber)\r
f7496d71 1668\r
e8a47801
LG
1669 if self.__Token != "1" and self.__Token != "0":\r
1670 raise Warning("expected 1 or 0 Erase Polarity", self.FileName, self.CurrentLineNumber)\r
f7496d71 1671\r
e8a47801
LG
1672 Obj.ErasePolarity = self.__Token\r
1673 return True\r
30fdf114 1674\r
e8a47801 1675 return self.__GetBlockStatements(Obj)\r
30fdf114
LG
1676\r
1677 ## __GetAddressStatements() method\r
1678 #\r
1679 # Get address statements\r
1680 #\r
1681 # @param self The object pointer\r
1682 # @param Obj for whom address statement is got\r
1683 # @retval True Successfully find\r
1684 # @retval False Not able to find\r
1685 #\r
1686 def __GetAddressStatements(self, Obj):\r
1687\r
1688 if self.__IsKeyword("BsBaseAddress"):\r
1689 if not self.__IsToken( "="):\r
1690 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
1691\r
1692 if not self.__GetNextDecimalNumber() and not self.__GetNextHexNumber():\r
1693 raise Warning("expected address", self.FileName, self.CurrentLineNumber)\r
1694\r
1695 BsAddress = long(self.__Token, 0)\r
1696 Obj.BsBaseAddress = BsAddress\r
1697\r
1698 if self.__IsKeyword("RtBaseAddress"):\r
1699 if not self.__IsToken( "="):\r
1700 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
1701\r
1702 if not self.__GetNextDecimalNumber() and not self.__GetNextHexNumber():\r
1703 raise Warning("expected address", self.FileName, self.CurrentLineNumber)\r
1704\r
1705 RtAddress = long(self.__Token, 0)\r
1706 Obj.RtBaseAddress = RtAddress\r
1707\r
1708 ## __GetBlockStatements() method\r
1709 #\r
1710 # Get block statements\r
1711 #\r
1712 # @param self The object pointer\r
1713 # @param Obj for whom block statement is got\r
30fdf114
LG
1714 #\r
1715 def __GetBlockStatements(self, Obj):\r
e8a47801 1716 IsBlock = False\r
30fdf114 1717 while self.__GetBlockStatement(Obj):\r
e8a47801 1718 IsBlock = True\r
f7496d71 1719\r
e8a47801 1720 Item = Obj.BlockSizeList[-1]\r
4231a819 1721 if Item[0] is None or Item[1] is None:\r
6780eef1 1722 raise Warning("expected block statement", self.FileName, self.CurrentLineNumber)\r
e8a47801 1723 return IsBlock\r
30fdf114
LG
1724\r
1725 ## __GetBlockStatement() method\r
1726 #\r
1727 # Get block statement\r
1728 #\r
1729 # @param self The object pointer\r
1730 # @param Obj for whom block statement is got\r
1731 # @retval True Successfully find\r
1732 # @retval False Not able to find\r
1733 #\r
1734 def __GetBlockStatement(self, Obj):\r
1735 if not self.__IsKeyword( "BlockSize"):\r
1736 return False\r
1737\r
1738 if not self.__IsToken( "="):\r
1739 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
1740\r
1741 if not self.__GetNextHexNumber() and not self.__GetNextDecimalNumber():\r
6780eef1 1742 raise Warning("expected Hex or Integer block size", self.FileName, self.CurrentLineNumber)\r
30fdf114
LG
1743\r
1744 BlockSize = self.__Token\r
1745 BlockSizePcd = None\r
1746 if self.__IsToken( "|"):\r
543f5ac3 1747 PcdPair = self.__GetNextPcdSettings()\r
30fdf114
LG
1748 BlockSizePcd = PcdPair\r
1749 self.Profile.PcdDict[PcdPair] = BlockSize\r
543f5ac3 1750 self.SetPcdLocalation(PcdPair)\r
d0acc87a
LG
1751 FileLineTuple = GetRealFileLine(self.FileName, self.CurrentLineNumber)\r
1752 self.Profile.PcdFileLineDict[PcdPair] = FileLineTuple\r
52302d4d 1753 BlockSize = long(BlockSize, 0)\r
30fdf114
LG
1754\r
1755 BlockNumber = None\r
1756 if self.__IsKeyword( "NumBlocks"):\r
1757 if not self.__IsToken( "="):\r
1758 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
1759\r
1760 if not self.__GetNextDecimalNumber() and not self.__GetNextHexNumber():\r
1761 raise Warning("expected block numbers", self.FileName, self.CurrentLineNumber)\r
1762\r
1763 BlockNumber = long(self.__Token, 0)\r
1764\r
1765 Obj.BlockSizeList.append((BlockSize, BlockNumber, BlockSizePcd))\r
1766 return True\r
1767\r
1768 ## __GetDefineStatements() method\r
1769 #\r
1770 # Get define statements\r
1771 #\r
1772 # @param self The object pointer\r
1773 # @param Obj for whom define statement is got\r
1774 # @retval True Successfully find\r
1775 # @retval False Not able to find\r
1776 #\r
1777 def __GetDefineStatements(self, Obj):\r
1778 while self.__GetDefineStatement( Obj):\r
1779 pass\r
1780\r
1781 ## __GetDefineStatement() method\r
1782 #\r
1783 # Get define statement\r
1784 #\r
1785 # @param self The object pointer\r
1786 # @param Obj for whom define statement is got\r
1787 # @retval True Successfully find\r
1788 # @retval False Not able to find\r
1789 #\r
1790 def __GetDefineStatement(self, Obj):\r
1791 if self.__IsKeyword("DEFINE"):\r
1792 self.__GetNextToken()\r
1793 Macro = self.__Token\r
1794 if not self.__IsToken( "="):\r
1795 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
1796\r
1797 if not self.__GetNextToken():\r
1798 raise Warning("expected value", self.FileName, self.CurrentLineNumber)\r
1799\r
1800 Value = self.__Token\r
1801 Macro = '$(' + Macro + ')'\r
1802 Obj.DefineVarDict[Macro] = Value\r
1803 return True\r
1804\r
1805 return False\r
1806\r
1807 ## __GetSetStatements() method\r
1808 #\r
1809 # Get set statements\r
1810 #\r
1811 # @param self The object pointer\r
1812 # @param Obj for whom set statement is got\r
1813 # @retval True Successfully find\r
1814 # @retval False Not able to find\r
1815 #\r
1816 def __GetSetStatements(self, Obj):\r
1817 while self.__GetSetStatement(Obj):\r
1818 pass\r
1819\r
1820 ## __GetSetStatement() method\r
1821 #\r
1822 # Get set statement\r
1823 #\r
1824 # @param self The object pointer\r
1825 # @param Obj for whom set statement is got\r
1826 # @retval True Successfully find\r
1827 # @retval False Not able to find\r
1828 #\r
1829 def __GetSetStatement(self, Obj):\r
1830 if self.__IsKeyword("SET"):\r
543f5ac3 1831 PcdPair = self.__GetNextPcdSettings()\r
30fdf114
LG
1832\r
1833 if not self.__IsToken( "="):\r
1834 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
1835\r
d0acc87a
LG
1836 Value = self.__GetExpression()\r
1837 Value = self.__EvaluateConditional(Value, self.CurrentLineNumber, 'eval', True)\r
30fdf114 1838\r
6780eef1
LG
1839 if Obj:\r
1840 Obj.SetVarDict[PcdPair] = Value\r
30fdf114 1841 self.Profile.PcdDict[PcdPair] = Value\r
543f5ac3 1842 self.SetPcdLocalation(PcdPair)\r
d0acc87a
LG
1843 FileLineTuple = GetRealFileLine(self.FileName, self.CurrentLineNumber)\r
1844 self.Profile.PcdFileLineDict[PcdPair] = FileLineTuple\r
30fdf114
LG
1845 return True\r
1846\r
1847 return False\r
1848\r
4afd3d04
LG
1849 ## __CalcRegionExpr(self)\r
1850 #\r
1851 # Calculate expression for offset or size of a region\r
1852 #\r
1853 # @return: None if invalid expression\r
1854 # Calculated number if successfully\r
1855 #\r
1856 def __CalcRegionExpr(self):\r
1857 StartPos = self.GetFileBufferPos()\r
1858 Expr = ''\r
1859 PairCount = 0\r
1860 while not self.__EndOfFile():\r
1861 CurCh = self.__CurrentChar()\r
1862 if CurCh == '(':\r
1863 PairCount += 1\r
1864 elif CurCh == ')':\r
1865 PairCount -= 1\r
1866\r
1867 if CurCh in '|\r\n' and PairCount == 0:\r
1868 break\r
1869 Expr += CurCh\r
1870 self.__GetOneChar()\r
1871 try:\r
1872 return long(\r
1873 ValueExpression(Expr,\r
2bc3256c 1874 self.__CollectMacroPcd()\r
ccaa7754 1875 )(True), 0)\r
4afd3d04
LG
1876 except Exception:\r
1877 self.SetFileBufferPos(StartPos)\r
1878 return None\r
1879\r
30fdf114
LG
1880 ## __GetRegionLayout() method\r
1881 #\r
1882 # Get region layout for FD\r
1883 #\r
1884 # @param self The object pointer\r
1885 # @param Fd for whom region is got\r
1886 # @retval True Successfully find\r
1887 # @retval False Not able to find\r
1888 #\r
1889 def __GetRegionLayout(self, Fd):\r
f7496d71 1890 Offset = self.__CalcRegionExpr()\r
4231a819 1891 if Offset is None:\r
30fdf114
LG
1892 return False\r
1893\r
1894 RegionObj = Region.Region()\r
4afd3d04 1895 RegionObj.Offset = Offset\r
30fdf114
LG
1896 Fd.RegionList.append(RegionObj)\r
1897\r
1898 if not self.__IsToken( "|"):\r
1899 raise Warning("expected '|'", self.FileName, self.CurrentLineNumber)\r
1900\r
4afd3d04 1901 Size = self.__CalcRegionExpr()\r
4231a819 1902 if Size is None:\r
30fdf114 1903 raise Warning("expected Region Size", self.FileName, self.CurrentLineNumber)\r
4afd3d04 1904 RegionObj.Size = Size\r
30fdf114
LG
1905\r
1906 if not self.__GetNextWord():\r
1907 return True\r
1908\r
91fa33ee 1909 if not self.__Token in ("SET", BINARY_FILE_TYPE_FV, "FILE", "DATA", "CAPSULE", "INF"):\r
2bc3256c
LG
1910 #\r
1911 # If next token is a word which is not a valid FV type, it might be part of [PcdOffset[|PcdSize]]\r
1912 # Or it might be next region's offset described by an expression which starts with a PCD.\r
1913 # PcdOffset[|PcdSize] or OffsetPcdExpression|Size\r
1914 #\r
30fdf114 1915 self.__UndoToken()\r
2bc3256c
LG
1916 IsRegionPcd = (RegionSizeGuidPattern.match(self.__CurrentLine()[self.CurrentOffsetWithinLine:]) or\r
1917 RegionOffsetPcdPattern.match(self.__CurrentLine()[self.CurrentOffsetWithinLine:]))\r
1918 if IsRegionPcd:\r
543f5ac3 1919 RegionObj.PcdOffset = self.__GetNextPcdSettings()\r
2bc3256c 1920 self.Profile.PcdDict[RegionObj.PcdOffset] = "0x%08X" % (RegionObj.Offset + long(Fd.BaseAddress, 0))\r
543f5ac3 1921 self.SetPcdLocalation(RegionObj.PcdOffset)\r
2bc3256c 1922 self.__PcdDict['%s.%s' % (RegionObj.PcdOffset[1], RegionObj.PcdOffset[0])] = "0x%x" % RegionObj.Offset\r
d0acc87a 1923 FileLineTuple = GetRealFileLine(self.FileName, self.CurrentLineNumber)\r
2bc3256c
LG
1924 self.Profile.PcdFileLineDict[RegionObj.PcdOffset] = FileLineTuple\r
1925 if self.__IsToken( "|"):\r
543f5ac3 1926 RegionObj.PcdSize = self.__GetNextPcdSettings()\r
2bc3256c 1927 self.Profile.PcdDict[RegionObj.PcdSize] = "0x%08X" % RegionObj.Size\r
543f5ac3 1928 self.SetPcdLocalation(RegionObj.PcdSize)\r
2bc3256c
LG
1929 self.__PcdDict['%s.%s' % (RegionObj.PcdSize[1], RegionObj.PcdSize[0])] = "0x%x" % RegionObj.Size\r
1930 FileLineTuple = GetRealFileLine(self.FileName, self.CurrentLineNumber)\r
1931 self.Profile.PcdFileLineDict[RegionObj.PcdSize] = FileLineTuple\r
30fdf114
LG
1932\r
1933 if not self.__GetNextWord():\r
1934 return True\r
1935\r
1936 if self.__Token == "SET":\r
1937 self.__UndoToken()\r
1938 self.__GetSetStatements( RegionObj)\r
1939 if not self.__GetNextWord():\r
1940 return True\r
1941\r
91fa33ee 1942 elif self.__Token == BINARY_FILE_TYPE_FV:\r
30fdf114
LG
1943 self.__UndoToken()\r
1944 self.__GetRegionFvType( RegionObj)\r
1945\r
fd171542 1946 elif self.__Token == "CAPSULE":\r
1947 self.__UndoToken()\r
1948 self.__GetRegionCapType( RegionObj)\r
1949\r
30fdf114
LG
1950 elif self.__Token == "FILE":\r
1951 self.__UndoToken()\r
b21a13fb
YZ
1952 self.__GetRegionFileType(RegionObj)\r
1953\r
1954 elif self.__Token == "INF":\r
1955 self.__UndoToken()\r
1956 RegionObj.RegionType = "INF"\r
1957 while self.__IsKeyword("INF"):\r
1958 self.__UndoToken()\r
1959 ffsInf = self.__ParseInfStatement()\r
1960 if not ffsInf:\r
1961 break\r
1962 RegionObj.RegionDataList.append(ffsInf)\r
30fdf114 1963\r
79b74a03 1964 elif self.__Token == "DATA":\r
30fdf114 1965 self.__UndoToken()\r
b21a13fb 1966 self.__GetRegionDataType(RegionObj)\r
79b74a03 1967 else:\r
2bc3256c
LG
1968 self.__UndoToken()\r
1969 if self.__GetRegionLayout(Fd):\r
1970 return True\r
79b74a03 1971 raise Warning("A valid region type was not found. "\r
b21a13fb 1972 "Valid types are [SET, FV, CAPSULE, FILE, DATA, INF]. This error occurred",\r
79b74a03 1973 self.FileName, self.CurrentLineNumber)\r
30fdf114
LG
1974\r
1975 return True\r
1976\r
1977 ## __GetRegionFvType() method\r
1978 #\r
1979 # Get region fv data for region\r
1980 #\r
1981 # @param self The object pointer\r
1982 # @param RegionObj for whom region data is got\r
1983 #\r
1984 def __GetRegionFvType(self, RegionObj):\r
1985\r
91fa33ee
CJ
1986 if not self.__IsKeyword( BINARY_FILE_TYPE_FV):\r
1987 raise Warning("expected Keyword BINARY_FILE_TYPE_FV", self.FileName, self.CurrentLineNumber)\r
30fdf114
LG
1988\r
1989 if not self.__IsToken( "="):\r
1990 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
1991\r
1992 if not self.__GetNextToken():\r
1993 raise Warning("expected FV name", self.FileName, self.CurrentLineNumber)\r
1994\r
91fa33ee 1995 RegionObj.RegionType = BINARY_FILE_TYPE_FV\r
0199377c 1996 RegionObj.RegionDataList.append((self.__Token).upper())\r
30fdf114 1997\r
91fa33ee 1998 while self.__IsKeyword( BINARY_FILE_TYPE_FV):\r
30fdf114
LG
1999\r
2000 if not self.__IsToken( "="):\r
2001 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
2002\r
2003 if not self.__GetNextToken():\r
2004 raise Warning("expected FV name", self.FileName, self.CurrentLineNumber)\r
2005\r
0199377c 2006 RegionObj.RegionDataList.append((self.__Token).upper())\r
30fdf114 2007\r
fd171542 2008 ## __GetRegionCapType() method\r
2009 #\r
2010 # Get region capsule data for region\r
2011 #\r
2012 # @param self The object pointer\r
2013 # @param RegionObj for whom region data is got\r
2014 #\r
2015 def __GetRegionCapType(self, RegionObj):\r
2016\r
2017 if not self.__IsKeyword("CAPSULE"):\r
2018 raise Warning("expected Keyword 'CAPSULE'", self.FileName, self.CurrentLineNumber)\r
2019\r
2020 if not self.__IsToken("="):\r
2021 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
2022\r
2023 if not self.__GetNextToken():\r
2024 raise Warning("expected CAPSULE name", self.FileName, self.CurrentLineNumber)\r
2025\r
2026 RegionObj.RegionType = "CAPSULE"\r
2027 RegionObj.RegionDataList.append(self.__Token)\r
2028\r
2029 while self.__IsKeyword("CAPSULE"):\r
2030\r
2031 if not self.__IsToken("="):\r
2032 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
2033\r
2034 if not self.__GetNextToken():\r
2035 raise Warning("expected CAPSULE name", self.FileName, self.CurrentLineNumber)\r
2036\r
2037 RegionObj.RegionDataList.append(self.__Token)\r
2038\r
30fdf114
LG
2039 ## __GetRegionFileType() method\r
2040 #\r
2041 # Get region file data for region\r
2042 #\r
2043 # @param self The object pointer\r
2044 # @param RegionObj for whom region data is got\r
2045 #\r
2046 def __GetRegionFileType(self, RegionObj):\r
2047\r
2048 if not self.__IsKeyword( "FILE"):\r
2049 raise Warning("expected Keyword 'FILE'", self.FileName, self.CurrentLineNumber)\r
2050\r
2051 if not self.__IsToken( "="):\r
2052 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
2053\r
2054 if not self.__GetNextToken():\r
2055 raise Warning("expected File name", self.FileName, self.CurrentLineNumber)\r
2056\r
2057 RegionObj.RegionType = "FILE"\r
2058 RegionObj.RegionDataList.append( self.__Token)\r
2059\r
2060 while self.__IsKeyword( "FILE"):\r
2061\r
2062 if not self.__IsToken( "="):\r
2063 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
2064\r
2065 if not self.__GetNextToken():\r
2066 raise Warning("expected FILE name", self.FileName, self.CurrentLineNumber)\r
2067\r
2068 RegionObj.RegionDataList.append(self.__Token)\r
2069\r
2070 ## __GetRegionDataType() method\r
2071 #\r
2072 # Get region array data for region\r
2073 #\r
2074 # @param self The object pointer\r
2075 # @param RegionObj for whom region data is got\r
2076 #\r
2077 def __GetRegionDataType(self, RegionObj):\r
2078\r
2079 if not self.__IsKeyword( "DATA"):\r
2080 raise Warning("expected Region Data type", self.FileName, self.CurrentLineNumber)\r
2081\r
2082 if not self.__IsToken( "="):\r
2083 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
2084\r
2085 if not self.__IsToken( "{"):\r
2086 raise Warning("expected '{'", self.FileName, self.CurrentLineNumber)\r
2087\r
2088 if not self.__GetNextHexNumber():\r
2089 raise Warning("expected Hex byte", self.FileName, self.CurrentLineNumber)\r
2090\r
636f2be6
LG
2091 if len(self.__Token) > 18:\r
2092 raise Warning("Hex string can't be converted to a valid UINT64 value", self.FileName, self.CurrentLineNumber)\r
2093\r
2094 # convert hex string value to byte hex string array\r
2095 AllString = self.__Token\r
2096 AllStrLen = len (AllString)\r
2097 DataString = ""\r
2098 while AllStrLen > 4:\r
2099 DataString = DataString + "0x" + AllString[AllStrLen - 2: AllStrLen] + ","\r
2100 AllStrLen = AllStrLen - 2\r
2101 DataString = DataString + AllString[:AllStrLen] + ","\r
2102\r
2103 # byte value array\r
2104 if len (self.__Token) <= 4:\r
2105 while self.__IsToken(","):\r
2106 if not self.__GetNextHexNumber():\r
2107 raise Warning("Invalid Hex number", self.FileName, self.CurrentLineNumber)\r
2108 if len(self.__Token) > 4:\r
2109 raise Warning("Hex byte(must be 2 digits) too long", self.FileName, self.CurrentLineNumber)\r
2110 DataString += self.__Token\r
2111 DataString += ","\r
30fdf114
LG
2112\r
2113 if not self.__IsToken( "}"):\r
2114 raise Warning("expected '}'", self.FileName, self.CurrentLineNumber)\r
2115\r
2116 DataString = DataString.rstrip(",")\r
2117 RegionObj.RegionType = "DATA"\r
2118 RegionObj.RegionDataList.append( DataString)\r
2119\r
2120 while self.__IsKeyword( "DATA"):\r
2121\r
2122 if not self.__IsToken( "="):\r
2123 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
2124\r
2125 if not self.__IsToken( "{"):\r
2126 raise Warning("expected '{'", self.FileName, self.CurrentLineNumber)\r
2127\r
2128 if not self.__GetNextHexNumber():\r
2129 raise Warning("expected Hex byte", self.FileName, self.CurrentLineNumber)\r
2130\r
636f2be6
LG
2131 if len(self.__Token) > 18:\r
2132 raise Warning("Hex string can't be converted to a valid UINT64 value", self.FileName, self.CurrentLineNumber)\r
2133\r
2134 # convert hex string value to byte hex string array\r
2135 AllString = self.__Token\r
2136 AllStrLen = len (AllString)\r
2137 DataString = ""\r
2138 while AllStrLen > 4:\r
2139 DataString = DataString + "0x" + AllString[AllStrLen - 2: AllStrLen] + ","\r
2140 AllStrLen = AllStrLen - 2\r
2141 DataString = DataString + AllString[:AllStrLen] + ","\r
2142\r
2143 # byte value array\r
2144 if len (self.__Token) <= 4:\r
2145 while self.__IsToken(","):\r
2146 if not self.__GetNextHexNumber():\r
2147 raise Warning("Invalid Hex number", self.FileName, self.CurrentLineNumber)\r
2148 if len(self.__Token) > 4:\r
2149 raise Warning("Hex byte(must be 2 digits) too long", self.FileName, self.CurrentLineNumber)\r
2150 DataString += self.__Token\r
2151 DataString += ","\r
30fdf114
LG
2152\r
2153 if not self.__IsToken( "}"):\r
2154 raise Warning("expected '}'", self.FileName, self.CurrentLineNumber)\r
2155\r
2156 DataString = DataString.rstrip(",")\r
2157 RegionObj.RegionDataList.append( DataString)\r
2158\r
2159 ## __GetFv() method\r
2160 #\r
2161 # Get FV section contents and store its data into FV dictionary of self.Profile\r
2162 #\r
2163 # @param self The object pointer\r
2164 # @retval True Successfully find a FV\r
2165 # @retval False Not able to find a FV\r
2166 #\r
2167 def __GetFv(self):\r
2168 if not self.__GetNextToken():\r
2169 return False\r
2170\r
2171 S = self.__Token.upper()\r
2172 if S.startswith("[") and not S.startswith("[FV."):\r
df81077f 2173 self.SectionParser(S)\r
30fdf114
LG
2174 self.__UndoToken()\r
2175 return False\r
2176\r
2177 self.__UndoToken()\r
2178 if not self.__IsToken("[FV.", True):\r
2179 FileLineTuple = GetRealFileLine(self.FileName, self.CurrentLineNumber)\r
2180 #print 'Parsing String: %s in File %s, At line: %d, Offset Within Line: %d' \\r
2181 # % (self.Profile.FileLinesList[self.CurrentLineNumber - 1][self.CurrentOffsetWithinLine :], FileLineTuple[0], FileLineTuple[1], self.CurrentOffsetWithinLine)\r
2182 raise Warning("Unknown Keyword '%s'" % self.__Token, self.FileName, self.CurrentLineNumber)\r
2183\r
2184 FvName = self.__GetUiName()\r
2185 self.CurrentFvName = FvName.upper()\r
2186\r
2187 if not self.__IsToken( "]"):\r
2188 raise Warning("expected ']'", self.FileName, self.CurrentLineNumber)\r
2189\r
2190 FvObj = Fv.FV()\r
2191 FvObj.UiFvName = self.CurrentFvName\r
2192 self.Profile.FvDict[self.CurrentFvName] = FvObj\r
2193\r
2194 Status = self.__GetCreateFile(FvObj)\r
2195 if not Status:\r
2196 raise Warning("FV name error", self.FileName, self.CurrentLineNumber)\r
2197\r
2198 self.__GetDefineStatements(FvObj)\r
2199\r
2200 self.__GetAddressStatements(FvObj)\r
2201\r
b303ea72
LG
2202 FvObj.FvExtEntryTypeValue = []\r
2203 FvObj.FvExtEntryType = []\r
2204 FvObj.FvExtEntryData = []\r
2205 while True:\r
e8a47801
LG
2206 self.__GetSetStatements(FvObj)\r
2207\r
f7496d71
LG
2208 if not (self.__GetBlockStatement(FvObj) or self.__GetFvBaseAddress(FvObj) or\r
2209 self.__GetFvForceRebase(FvObj) or self.__GetFvAlignment(FvObj) or\r
2210 self.__GetFvAttributes(FvObj) or self.__GetFvNameGuid(FvObj) or\r
aaf8aa7b 2211 self.__GetFvExtEntryStatement(FvObj) or self.__GetFvNameString(FvObj)):\r
b303ea72
LG
2212 break\r
2213\r
aaf8aa7b
YL
2214 if FvObj.FvNameString == 'TRUE' and not FvObj.FvNameGuid:\r
2215 raise Warning("FvNameString found but FvNameGuid was not found", self.FileName, self.CurrentLineNumber)\r
2216\r
30fdf114
LG
2217 self.__GetAprioriSection(FvObj, FvObj.DefineVarDict.copy())\r
2218 self.__GetAprioriSection(FvObj, FvObj.DefineVarDict.copy())\r
2219\r
2220 while True:\r
cdc9b0c2 2221 isInf = self.__GetInfStatement(FvObj)\r
30fdf114
LG
2222 isFile = self.__GetFileStatement(FvObj, MacroDict = FvObj.DefineVarDict.copy())\r
2223 if not isInf and not isFile:\r
2224 break\r
2225\r
2226 return True\r
2227\r
2228 ## __GetFvAlignment() method\r
2229 #\r
2230 # Get alignment for FV\r
2231 #\r
2232 # @param self The object pointer\r
2233 # @param Obj for whom alignment is got\r
2234 # @retval True Successfully find a alignment statement\r
2235 # @retval False Not able to find a alignment statement\r
2236 #\r
2237 def __GetFvAlignment(self, Obj):\r
2238\r
2239 if not self.__IsKeyword( "FvAlignment"):\r
2240 return False\r
2241\r
2242 if not self.__IsToken( "="):\r
2243 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
2244\r
2245 if not self.__GetNextToken():\r
2246 raise Warning("expected alignment value", self.FileName, self.CurrentLineNumber)\r
2247\r
2248 if self.__Token.upper() not in ("1", "2", "4", "8", "16", "32", "64", "128", "256", "512", \\r
2249 "1K", "2K", "4K", "8K", "16K", "32K", "64K", "128K", "256K", "512K", \\r
2250 "1M", "2M", "4M", "8M", "16M", "32M", "64M", "128M", "256M", "512M", \\r
2251 "1G", "2G"):\r
2252 raise Warning("Unknown alignment value '%s'" % self.__Token, self.FileName, self.CurrentLineNumber)\r
2253 Obj.FvAlignment = self.__Token\r
2254 return True\r
f7496d71 2255\r
4234283c
LG
2256 ## __GetFvBaseAddress() method\r
2257 #\r
2258 # Get BaseAddress for FV\r
2259 #\r
2260 # @param self The object pointer\r
2261 # @param Obj for whom FvBaseAddress is got\r
2262 # @retval True Successfully find a FvBaseAddress statement\r
2263 # @retval False Not able to find a FvBaseAddress statement\r
2264 #\r
2265 def __GetFvBaseAddress(self, Obj):\r
2266\r
2267 if not self.__IsKeyword("FvBaseAddress"):\r
2268 return False\r
2269\r
2270 if not self.__IsToken( "="):\r
2271 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
2272\r
2273 if not self.__GetNextToken():\r
2274 raise Warning("expected FV base address value", self.FileName, self.CurrentLineNumber)\r
2275\r
ffe720c5 2276 if not BaseAddrValuePattern.match(self.__Token.upper()):\r
79b74a03 2277 raise Warning("Unknown FV base address value '%s'" % self.__Token, self.FileName, self.CurrentLineNumber)\r
4234283c 2278 Obj.FvBaseAddress = self.__Token\r
f7496d71
LG
2279 return True\r
2280\r
79b74a03
LG
2281 ## __GetFvForceRebase() method\r
2282 #\r
2283 # Get FvForceRebase for FV\r
2284 #\r
2285 # @param self The object pointer\r
2286 # @param Obj for whom FvForceRebase is got\r
2287 # @retval True Successfully find a FvForceRebase statement\r
2288 # @retval False Not able to find a FvForceRebase statement\r
2289 #\r
2290 def __GetFvForceRebase(self, Obj):\r
2291\r
2292 if not self.__IsKeyword("FvForceRebase"):\r
2293 return False\r
2294\r
2295 if not self.__IsToken( "="):\r
2296 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
2297\r
2298 if not self.__GetNextToken():\r
2299 raise Warning("expected FvForceRebase value", self.FileName, self.CurrentLineNumber)\r
30fdf114 2300\r
79b74a03
LG
2301 if self.__Token.upper() not in ["TRUE", "FALSE", "0", "0X0", "0X00", "1", "0X1", "0X01"]:\r
2302 raise Warning("Unknown FvForceRebase value '%s'" % self.__Token, self.FileName, self.CurrentLineNumber)\r
f7496d71 2303\r
79b74a03
LG
2304 if self.__Token.upper() in ["TRUE", "1", "0X1", "0X01"]:\r
2305 Obj.FvForceRebase = True\r
2306 elif self.__Token.upper() in ["FALSE", "0", "0X0", "0X00"]:\r
2307 Obj.FvForceRebase = False\r
2308 else:\r
2309 Obj.FvForceRebase = None\r
f7496d71 2310\r
79b74a03 2311 return True\r
0d2711a6
LG
2312\r
2313\r
30fdf114
LG
2314 ## __GetFvAttributes() method\r
2315 #\r
2316 # Get attributes for FV\r
2317 #\r
2318 # @param self The object pointer\r
2319 # @param Obj for whom attribute is got\r
2320 # @retval None\r
2321 #\r
2322 def __GetFvAttributes(self, FvObj):\r
2bc3256c 2323 IsWordToken = False\r
30fdf114 2324 while self.__GetNextWord():\r
2bc3256c 2325 IsWordToken = True\r
30fdf114
LG
2326 name = self.__Token\r
2327 if name not in ("ERASE_POLARITY", "MEMORY_MAPPED", \\r
2328 "STICKY_WRITE", "LOCK_CAP", "LOCK_STATUS", "WRITE_ENABLED_CAP", \\r
2329 "WRITE_DISABLED_CAP", "WRITE_STATUS", "READ_ENABLED_CAP", \\r
2330 "READ_DISABLED_CAP", "READ_STATUS", "READ_LOCK_CAP", \\r
2331 "READ_LOCK_STATUS", "WRITE_LOCK_CAP", "WRITE_LOCK_STATUS", \\r
9425b349 2332 "WRITE_POLICY_RELIABLE", "WEAK_ALIGNMENT", "FvUsedSizeEnable"):\r
30fdf114 2333 self.__UndoToken()\r
e8a47801 2334 return False\r
30fdf114
LG
2335\r
2336 if not self.__IsToken( "="):\r
2337 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
2338\r
2339 if not self.__GetNextToken() or self.__Token.upper() not in ("TRUE", "FALSE", "1", "0"):\r
2340 raise Warning("expected TRUE/FALSE (1/0)", self.FileName, self.CurrentLineNumber)\r
2341\r
2342 FvObj.FvAttributeDict[name] = self.__Token\r
2343\r
2bc3256c 2344 return IsWordToken\r
f7496d71 2345\r
30fdf114
LG
2346 ## __GetFvNameGuid() method\r
2347 #\r
2348 # Get FV GUID for FV\r
2349 #\r
2350 # @param self The object pointer\r
2351 # @param Obj for whom GUID is got\r
2352 # @retval None\r
2353 #\r
2354 def __GetFvNameGuid(self, FvObj):\r
2355\r
2356 if not self.__IsKeyword( "FvNameGuid"):\r
e8a47801 2357 return False\r
30fdf114
LG
2358\r
2359 if not self.__IsToken( "="):\r
2360 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
2361\r
2362 if not self.__GetNextGuid():\r
2363 raise Warning("expected FV GUID value", self.FileName, self.CurrentLineNumber)\r
2364\r
2365 FvObj.FvNameGuid = self.__Token\r
2366\r
e8a47801 2367 return True\r
30fdf114 2368\r
aaf8aa7b
YL
2369 def __GetFvNameString(self, FvObj):\r
2370\r
2371 if not self.__IsKeyword( "FvNameString"):\r
2372 return False\r
2373\r
2374 if not self.__IsToken( "="):\r
2375 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
2376\r
2377 if not self.__GetNextToken() or self.__Token not in ('TRUE', 'FALSE'):\r
2378 raise Warning("expected TRUE or FALSE for FvNameString", self.FileName, self.CurrentLineNumber)\r
2379\r
2380 FvObj.FvNameString = self.__Token\r
2381\r
2382 return True\r
2383\r
b303ea72
LG
2384 def __GetFvExtEntryStatement(self, FvObj):\r
2385\r
92d07e48 2386 if not (self.__IsKeyword( "FV_EXT_ENTRY") or self.__IsKeyword( "FV_EXT_ENTRY_TYPE")):\r
b303ea72
LG
2387 return False\r
2388\r
2389 if not self.__IsKeyword ("TYPE"):\r
2390 raise Warning("expected 'TYPE'", self.FileName, self.CurrentLineNumber)\r
f7496d71 2391\r
b303ea72
LG
2392 if not self.__IsToken( "="):\r
2393 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
2394\r
2395 if not self.__GetNextHexNumber() and not self.__GetNextDecimalNumber():\r
2396 raise Warning("expected Hex FV extension entry type value At Line ", self.FileName, self.CurrentLineNumber)\r
2397\r
caf74495 2398 FvObj.FvExtEntryTypeValue.append(self.__Token)\r
b303ea72
LG
2399\r
2400 if not self.__IsToken( "{"):\r
2401 raise Warning("expected '{'", self.FileName, self.CurrentLineNumber)\r
2402\r
2403 if not self.__IsKeyword ("FILE") and not self.__IsKeyword ("DATA"):\r
2404 raise Warning("expected 'FILE' or 'DATA'", self.FileName, self.CurrentLineNumber)\r
2405\r
caf74495 2406 FvObj.FvExtEntryType.append(self.__Token)\r
b303ea72
LG
2407\r
2408 if self.__Token == 'DATA':\r
2409\r
2410 if not self.__IsToken( "="):\r
2411 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
f7496d71 2412\r
b303ea72
LG
2413 if not self.__IsToken( "{"):\r
2414 raise Warning("expected '{'", self.FileName, self.CurrentLineNumber)\r
2415\r
2416 if not self.__GetNextHexNumber():\r
2417 raise Warning("expected Hex byte", self.FileName, self.CurrentLineNumber)\r
2418\r
2419 if len(self.__Token) > 4:\r
2420 raise Warning("Hex byte(must be 2 digits) too long", self.FileName, self.CurrentLineNumber)\r
2421\r
2422 DataString = self.__Token\r
2423 DataString += ","\r
2424\r
2425 while self.__IsToken(","):\r
2426 if not self.__GetNextHexNumber():\r
2427 raise Warning("Invalid Hex number", self.FileName, self.CurrentLineNumber)\r
2428 if len(self.__Token) > 4:\r
2429 raise Warning("Hex byte(must be 2 digits) too long", self.FileName, self.CurrentLineNumber)\r
2430 DataString += self.__Token\r
2431 DataString += ","\r
2432\r
2433 if not self.__IsToken( "}"):\r
2434 raise Warning("expected '}'", self.FileName, self.CurrentLineNumber)\r
2435\r
2436 if not self.__IsToken( "}"):\r
2437 raise Warning("expected '}'", self.FileName, self.CurrentLineNumber)\r
2438\r
2439 DataString = DataString.rstrip(",")\r
caf74495 2440 FvObj.FvExtEntryData.append(DataString)\r
b303ea72
LG
2441\r
2442 if self.__Token == 'FILE':\r
f7496d71 2443\r
b303ea72
LG
2444 if not self.__IsToken( "="):\r
2445 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
f7496d71 2446\r
b303ea72
LG
2447 if not self.__GetNextToken():\r
2448 raise Warning("expected FV Extension Entry file path At Line ", self.FileName, self.CurrentLineNumber)\r
f7496d71 2449\r
caf74495 2450 FvObj.FvExtEntryData.append(self.__Token)\r
b303ea72
LG
2451\r
2452 if not self.__IsToken( "}"):\r
2453 raise Warning("expected '}'", self.FileName, self.CurrentLineNumber)\r
2454\r
2455 return True\r
2456\r
30fdf114
LG
2457 ## __GetAprioriSection() method\r
2458 #\r
2459 # Get token statements\r
2460 #\r
2461 # @param self The object pointer\r
2462 # @param FvObj for whom apriori is got\r
2463 # @param MacroDict dictionary used to replace macro\r
2464 # @retval True Successfully find apriori statement\r
2465 # @retval False Not able to find apriori statement\r
2466 #\r
2467 def __GetAprioriSection(self, FvObj, MacroDict = {}):\r
2468\r
2469 if not self.__IsKeyword( "APRIORI"):\r
2470 return False\r
2471\r
2472 if not self.__IsKeyword("PEI") and not self.__IsKeyword("DXE"):\r
2473 raise Warning("expected Apriori file type", self.FileName, self.CurrentLineNumber)\r
2474 AprType = self.__Token\r
2475\r
2476 if not self.__IsToken( "{"):\r
2477 raise Warning("expected '{'", self.FileName, self.CurrentLineNumber)\r
2478\r
2479 AprSectionObj = AprioriSection.AprioriSection()\r
2480 AprSectionObj.AprioriType = AprType\r
2481\r
2482 self.__GetDefineStatements(AprSectionObj)\r
2483 MacroDict.update(AprSectionObj.DefineVarDict)\r
2484\r
2485 while True:\r
cdc9b0c2 2486 IsInf = self.__GetInfStatement(AprSectionObj)\r
30fdf114
LG
2487 IsFile = self.__GetFileStatement( AprSectionObj)\r
2488 if not IsInf and not IsFile:\r
2489 break\r
2490\r
2491 if not self.__IsToken( "}"):\r
2492 raise Warning("expected '}'", self.FileName, self.CurrentLineNumber)\r
2493\r
2494 FvObj.AprioriSectionList.append(AprSectionObj)\r
2495 return True\r
2496\r
b21a13fb
YZ
2497 def __ParseInfStatement(self):\r
2498 if not self.__IsKeyword("INF"):\r
2499 return None\r
30fdf114
LG
2500\r
2501 ffsInf = FfsInfStatement.FfsInfStatement()\r
b21a13fb 2502 self.__GetInfOptions(ffsInf)\r
30fdf114
LG
2503\r
2504 if not self.__GetNextToken():\r
2505 raise Warning("expected INF file path", self.FileName, self.CurrentLineNumber)\r
2506 ffsInf.InfFileName = self.__Token\r
a87e79d9
YZ
2507 if not ffsInf.InfFileName.endswith('.inf'):\r
2508 raise Warning("expected .inf file path", self.FileName, self.CurrentLineNumber)\r
64b2609f
LG
2509\r
2510 ffsInf.CurrentLineNum = self.CurrentLineNumber\r
2511 ffsInf.CurrentLineContent = self.__CurrentLine()\r
2512\r
97fa0ee9
YL
2513 #Replace $(SAPCE) with real space\r
2514 ffsInf.InfFileName = ffsInf.InfFileName.replace('$(SPACE)', ' ')\r
2515\r
14c48571 2516 if ffsInf.InfFileName.replace('$(WORKSPACE)', '').find('$') == -1:\r
2517 #do case sensitive check for file path\r
2518 ErrorCode, ErrorInfo = PathClass(NormPath(ffsInf.InfFileName), GenFdsGlobalVariable.WorkSpaceDir).Validate()\r
2519 if ErrorCode != 0:\r
2520 EdkLogger.error("GenFds", ErrorCode, ExtraData=ErrorInfo)\r
30fdf114 2521\r
30fdf114
LG
2522 if not ffsInf.InfFileName in self.Profile.InfList:\r
2523 self.Profile.InfList.append(ffsInf.InfFileName)\r
d0acc87a
LG
2524 FileLineTuple = GetRealFileLine(self.FileName, self.CurrentLineNumber)\r
2525 self.Profile.InfFileLineList.append(FileLineTuple)\r
2502b735
YZ
2526 if ffsInf.UseArch:\r
2527 if ffsInf.UseArch not in self.Profile.InfDict:\r
2528 self.Profile.InfDict[ffsInf.UseArch] = [ffsInf.InfFileName]\r
2529 else:\r
2530 self.Profile.InfDict[ffsInf.UseArch].append(ffsInf.InfFileName)\r
2531 else:\r
2532 self.Profile.InfDict['ArchTBD'].append(ffsInf.InfFileName)\r
30fdf114
LG
2533\r
2534 if self.__IsToken('|'):\r
2535 if self.__IsKeyword('RELOCS_STRIPPED'):\r
2536 ffsInf.KeepReloc = False\r
2537 elif self.__IsKeyword('RELOCS_RETAINED'):\r
2538 ffsInf.KeepReloc = True\r
2539 else:\r
2540 raise Warning("Unknown reloc strip flag '%s'" % self.__Token, self.FileName, self.CurrentLineNumber)\r
b21a13fb
YZ
2541 return ffsInf\r
2542\r
2543 ## __GetInfStatement() method\r
2544 #\r
2545 # Get INF statements\r
2546 #\r
2547 # @param self The object pointer\r
2548 # @param Obj for whom inf statement is got\r
b21a13fb
YZ
2549 # @retval True Successfully find inf statement\r
2550 # @retval False Not able to find inf statement\r
2551 #\r
cdc9b0c2 2552 def __GetInfStatement(self, Obj, ForCapsule=False):\r
b21a13fb
YZ
2553 ffsInf = self.__ParseInfStatement()\r
2554 if not ffsInf:\r
2555 return False\r
2556\r
30fdf114
LG
2557 if ForCapsule:\r
2558 capsuleFfs = CapsuleData.CapsuleFfs()\r
2559 capsuleFfs.Ffs = ffsInf\r
2560 Obj.CapsuleDataList.append(capsuleFfs)\r
2561 else:\r
2562 Obj.FfsList.append(ffsInf)\r
2563 return True\r
2564\r
2565 ## __GetInfOptions() method\r
2566 #\r
2567 # Get options for INF\r
2568 #\r
2569 # @param self The object pointer\r
2570 # @param FfsInfObj for whom option is got\r
2571 #\r
2572 def __GetInfOptions(self, FfsInfObj):\r
97fa0ee9
YL
2573 if self.__IsKeyword("FILE_GUID"):\r
2574 if not self.__IsToken("="):\r
2575 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
2576 if not self.__GetNextGuid():\r
2577 raise Warning("expected GUID value", self.FileName, self.CurrentLineNumber)\r
2578 FfsInfObj.OverrideGuid = self.__Token\r
30fdf114
LG
2579\r
2580 if self.__IsKeyword( "RuleOverride"):\r
2581 if not self.__IsToken( "="):\r
2582 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
2583 if not self.__GetNextToken():\r
2584 raise Warning("expected Rule name", self.FileName, self.CurrentLineNumber)\r
2585 FfsInfObj.Rule = self.__Token\r
2586\r
2587 if self.__IsKeyword( "VERSION"):\r
2588 if not self.__IsToken( "="):\r
2589 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
2590 if not self.__GetNextToken():\r
2591 raise Warning("expected Version", self.FileName, self.CurrentLineNumber)\r
2592\r
2593 if self.__GetStringData():\r
2594 FfsInfObj.Version = self.__Token\r
2595\r
91fa33ee 2596 if self.__IsKeyword( BINARY_FILE_TYPE_UI):\r
30fdf114
LG
2597 if not self.__IsToken( "="):\r
2598 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
2599 if not self.__GetNextToken():\r
2600 raise Warning("expected UI name", self.FileName, self.CurrentLineNumber)\r
2601\r
2602 if self.__GetStringData():\r
2603 FfsInfObj.Ui = self.__Token\r
2604\r
2605 if self.__IsKeyword( "USE"):\r
2606 if not self.__IsToken( "="):\r
2607 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
2608 if not self.__GetNextToken():\r
2609 raise Warning("expected ARCH name", self.FileName, self.CurrentLineNumber)\r
2610 FfsInfObj.UseArch = self.__Token\r
2611\r
f7496d71 2612\r
30fdf114 2613 if self.__GetNextToken():\r
97fa0ee9
YL
2614 p = re.compile(r'([a-zA-Z0-9\-]+|\$\(TARGET\)|\*)_([a-zA-Z0-9\-]+|\$\(TOOL_CHAIN_TAG\)|\*)_([a-zA-Z0-9\-]+|\$\(ARCH\))')\r
2615 if p.match(self.__Token) and p.match(self.__Token).span()[1] == len(self.__Token):\r
30fdf114
LG
2616 FfsInfObj.KeyStringList.append(self.__Token)\r
2617 if not self.__IsToken(","):\r
2618 return\r
2619 else:\r
2620 self.__UndoToken()\r
2621 return\r
2622\r
2623 while self.__GetNextToken():\r
2624 if not p.match(self.__Token):\r
2625 raise Warning("expected KeyString \"Target_Tag_Arch\"", self.FileName, self.CurrentLineNumber)\r
2626 FfsInfObj.KeyStringList.append(self.__Token)\r
2627\r
2628 if not self.__IsToken(","):\r
2629 break\r
2630\r
2631 ## __GetFileStatement() method\r
2632 #\r
2633 # Get FILE statements\r
2634 #\r
2635 # @param self The object pointer\r
2636 # @param Obj for whom FILE statement is got\r
2637 # @param MacroDict dictionary used to replace macro\r
2638 # @retval True Successfully find FILE statement\r
2639 # @retval False Not able to find FILE statement\r
2640 #\r
2641 def __GetFileStatement(self, Obj, ForCapsule = False, MacroDict = {}):\r
2642\r
2643 if not self.__IsKeyword( "FILE"):\r
2644 return False\r
2645\r
30fdf114
LG
2646 if not self.__GetNextWord():\r
2647 raise Warning("expected FFS type", self.FileName, self.CurrentLineNumber)\r
b36d134f
LG
2648\r
2649 if ForCapsule and self.__Token == 'DATA':\r
2650 self.__UndoToken()\r
2651 self.__UndoToken()\r
2652 return False\r
f7496d71 2653\r
b36d134f 2654 FfsFileObj = FfsFileStatement.FileStatement()\r
30fdf114
LG
2655 FfsFileObj.FvFileType = self.__Token\r
2656\r
2657 if not self.__IsToken( "="):\r
2658 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
2659\r
2660 if not self.__GetNextGuid():\r
2661 if not self.__GetNextWord():\r
2662 raise Warning("expected File GUID", self.FileName, self.CurrentLineNumber)\r
2663 if self.__Token == 'PCD':\r
2664 if not self.__IsToken( "("):\r
2665 raise Warning("expected '('", self.FileName, self.CurrentLineNumber)\r
543f5ac3 2666 PcdPair = self.__GetNextPcdSettings()\r
30fdf114
LG
2667 if not self.__IsToken( ")"):\r
2668 raise Warning("expected ')'", self.FileName, self.CurrentLineNumber)\r
2669 self.__Token = 'PCD('+PcdPair[1]+'.'+PcdPair[0]+')'\r
f7496d71 2670\r
30fdf114 2671 FfsFileObj.NameGuid = self.__Token\r
f7496d71 2672\r
30fdf114
LG
2673 self.__GetFilePart( FfsFileObj, MacroDict.copy())\r
2674\r
2675 if ForCapsule:\r
2676 capsuleFfs = CapsuleData.CapsuleFfs()\r
2677 capsuleFfs.Ffs = FfsFileObj\r
2678 Obj.CapsuleDataList.append(capsuleFfs)\r
2679 else:\r
2680 Obj.FfsList.append(FfsFileObj)\r
2681\r
2682 return True\r
2683\r
2684 ## __FileCouldHaveRelocFlag() method\r
2685 #\r
2686 # Check whether reloc strip flag can be set for a file type.\r
2687 #\r
30fdf114
LG
2688 # @param FileType The file type to check with\r
2689 # @retval True This type could have relocation strip flag\r
2690 # @retval False No way to have it\r
2691 #\r
5bcf1d56
CJ
2692 @staticmethod\r
2693 def __FileCouldHaveRelocFlag (FileType):\r
8bb63e37 2694 if FileType in (SUP_MODULE_SEC, SUP_MODULE_PEI_CORE, SUP_MODULE_PEIM, 'PEI_DXE_COMBO'):\r
30fdf114
LG
2695 return True\r
2696 else:\r
2697 return False\r
2698\r
2699 ## __SectionCouldHaveRelocFlag() method\r
2700 #\r
2701 # Check whether reloc strip flag can be set for a section type.\r
2702 #\r
30fdf114
LG
2703 # @param SectionType The section type to check with\r
2704 # @retval True This type could have relocation strip flag\r
2705 # @retval False No way to have it\r
2706 #\r
5bcf1d56
CJ
2707 @staticmethod\r
2708 def __SectionCouldHaveRelocFlag (SectionType):\r
91fa33ee 2709 if SectionType in (BINARY_FILE_TYPE_TE, BINARY_FILE_TYPE_PE32):\r
30fdf114
LG
2710 return True\r
2711 else:\r
2712 return False\r
2713\r
2714 ## __GetFilePart() method\r
2715 #\r
2716 # Get components for FILE statement\r
2717 #\r
2718 # @param self The object pointer\r
2719 # @param FfsFileObj for whom component is got\r
2720 # @param MacroDict dictionary used to replace macro\r
2721 #\r
2722 def __GetFilePart(self, FfsFileObj, MacroDict = {}):\r
2723\r
2724 self.__GetFileOpts( FfsFileObj)\r
2725\r
2726 if not self.__IsToken("{"):\r
4afd3d04
LG
2727 if self.__IsKeyword('RELOCS_STRIPPED') or self.__IsKeyword('RELOCS_RETAINED'):\r
2728 if self.__FileCouldHaveRelocFlag(FfsFileObj.FvFileType):\r
2729 if self.__Token == 'RELOCS_STRIPPED':\r
2730 FfsFileObj.KeepReloc = False\r
2731 else:\r
2732 FfsFileObj.KeepReloc = True\r
2733 else:\r
2734 raise Warning("File type %s could not have reloc strip flag%d" % (FfsFileObj.FvFileType, self.CurrentLineNumber), self.FileName, self.CurrentLineNumber)\r
2735\r
2736 if not self.__IsToken("{"):\r
30fdf114
LG
2737 raise Warning("expected '{'", self.FileName, self.CurrentLineNumber)\r
2738\r
2739 if not self.__GetNextToken():\r
2740 raise Warning("expected File name or section data", self.FileName, self.CurrentLineNumber)\r
2741\r
91fa33ee 2742 if self.__Token == BINARY_FILE_TYPE_FV:\r
30fdf114
LG
2743 if not self.__IsToken( "="):\r
2744 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
2745 if not self.__GetNextToken():\r
2746 raise Warning("expected FV name", self.FileName, self.CurrentLineNumber)\r
2747 FfsFileObj.FvName = self.__Token\r
2748\r
2749 elif self.__Token == "FD":\r
2750 if not self.__IsToken( "="):\r
2751 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
2752 if not self.__GetNextToken():\r
2753 raise Warning("expected FD name", self.FileName, self.CurrentLineNumber)\r
2754 FfsFileObj.FdName = self.__Token\r
2755\r
2756 elif self.__Token in ("DEFINE", "APRIORI", "SECTION"):\r
2757 self.__UndoToken()\r
2758 self.__GetSectionData( FfsFileObj, MacroDict)\r
860992ed
YZ
2759\r
2760 elif hasattr(FfsFileObj, 'FvFileType') and FfsFileObj.FvFileType == 'RAW':\r
2761 self.__UndoToken()\r
2762 self.__GetRAWData(FfsFileObj, MacroDict)\r
2763\r
30fdf114 2764 else:\r
64b2609f
LG
2765 FfsFileObj.CurrentLineNum = self.CurrentLineNumber\r
2766 FfsFileObj.CurrentLineContent = self.__CurrentLine()\r
97fa0ee9 2767 FfsFileObj.FileName = self.__Token.replace('$(SPACE)', ' ')\r
2bc3256c 2768 self.__VerifyFile(FfsFileObj.FileName)\r
0d2711a6 2769\r
30fdf114
LG
2770 if not self.__IsToken( "}"):\r
2771 raise Warning("expected '}'", self.FileName, self.CurrentLineNumber)\r
2772\r
860992ed
YZ
2773 ## __GetRAWData() method\r
2774 #\r
2775 # Get RAW data for FILE statement\r
2776 #\r
2777 # @param self The object pointer\r
2778 # @param FfsFileObj for whom section is got\r
2779 # @param MacroDict dictionary used to replace macro\r
2780 #\r
2781 def __GetRAWData(self, FfsFileObj, MacroDict = {}):\r
2782 FfsFileObj.FileName = []\r
cfaaf99b 2783 FfsFileObj.SubAlignment = []\r
860992ed
YZ
2784 while True:\r
2785 AlignValue = None\r
2786 if self.__GetAlignment():\r
ccaa7754 2787 if self.__Token not in ("Auto", "8", "16", "32", "64", "128", "512", "1K", "4K", "32K", "64K", "128K",\r
e921f58d 2788 "256K", "512K", "1M", "2M", "4M", "8M", "16M"):\r
860992ed 2789 raise Warning("Incorrect alignment '%s'" % self.__Token, self.FileName, self.CurrentLineNumber)\r
f475f1e2
YZ
2790 #For FFS, Auto is default option same to ""\r
2791 if not self.__Token == "Auto":\r
2792 AlignValue = self.__Token\r
860992ed
YZ
2793 if not self.__GetNextToken():\r
2794 raise Warning("expected Filename value", self.FileName, self.CurrentLineNumber)\r
2795\r
2796 FileName = self.__Token.replace('$(SPACE)', ' ')\r
2797 if FileName == '}':\r
2798 self.__UndoToken()\r
2799 raise Warning("expected Filename value", self.FileName, self.CurrentLineNumber)\r
860992ed
YZ
2800\r
2801 self.__VerifyFile(FileName)\r
2802 File = PathClass(NormPath(FileName), GenFdsGlobalVariable.WorkSpaceDir)\r
2803 FfsFileObj.FileName.append(File.Path)\r
cfaaf99b 2804 FfsFileObj.SubAlignment.append(AlignValue)\r
860992ed
YZ
2805\r
2806 if self.__IsToken( "}"):\r
2807 self.__UndoToken()\r
2808 break\r
2809\r
cfaaf99b
YZ
2810 if len(FfsFileObj.SubAlignment) == 1:\r
2811 FfsFileObj.SubAlignment = FfsFileObj.SubAlignment[0]\r
860992ed
YZ
2812 if len(FfsFileObj.FileName) == 1:\r
2813 FfsFileObj.FileName = FfsFileObj.FileName[0]\r
2814\r
30fdf114
LG
2815 ## __GetFileOpts() method\r
2816 #\r
2817 # Get options for FILE statement\r
2818 #\r
2819 # @param self The object pointer\r
2820 # @param FfsFileObj for whom options is got\r
2821 #\r
2822 def __GetFileOpts(self, FfsFileObj):\r
2823\r
2824 if self.__GetNextToken():\r
147a656b 2825 if TokenFindPattern.match(self.__Token):\r
30fdf114
LG
2826 FfsFileObj.KeyStringList.append(self.__Token)\r
2827 if self.__IsToken(","):\r
2828 while self.__GetNextToken():\r
147a656b 2829 if not TokenFindPattern.match(self.__Token):\r
30fdf114
LG
2830 raise Warning("expected KeyString \"Target_Tag_Arch\"", self.FileName, self.CurrentLineNumber)\r
2831 FfsFileObj.KeyStringList.append(self.__Token)\r
2832\r
2833 if not self.__IsToken(","):\r
2834 break\r
2835\r
2836 else:\r
2837 self.__UndoToken()\r
2838\r
2839 if self.__IsKeyword( "FIXED", True):\r
2840 FfsFileObj.Fixed = True\r
2841\r
2842 if self.__IsKeyword( "CHECKSUM", True):\r
2843 FfsFileObj.CheckSum = True\r
2844\r
2845 if self.__GetAlignment():\r
ccaa7754 2846 if self.__Token not in ("Auto", "8", "16", "32", "64", "128", "512", "1K", "4K", "32K", "64K", "128K",\r
e921f58d 2847 "256K", "512K", "1M", "2M", "4M", "8M", "16M"):\r
9053bc51 2848 raise Warning("Incorrect alignment '%s'" % self.__Token, self.FileName, self.CurrentLineNumber)\r
2849 #For FFS, Auto is default option same to ""\r
2850 if not self.__Token == "Auto":\r
2851 FfsFileObj.Alignment = self.__Token\r
30fdf114
LG
2852\r
2853 ## __GetAlignment() method\r
2854 #\r
2855 # Return the alignment value\r
2856 #\r
2857 # @param self The object pointer\r
2858 # @retval True Successfully find alignment\r
2859 # @retval False Not able to find alignment\r
2860 #\r
2861 def __GetAlignment(self):\r
2862 if self.__IsKeyword( "Align", True):\r
2863 if not self.__IsToken( "="):\r
2864 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
2865\r
2866 if not self.__GetNextToken():\r
2867 raise Warning("expected alignment value", self.FileName, self.CurrentLineNumber)\r
2868 return True\r
2869\r
2870 return False\r
2871\r
2872 ## __GetFilePart() method\r
2873 #\r
2874 # Get section data for FILE statement\r
2875 #\r
2876 # @param self The object pointer\r
2877 # @param FfsFileObj for whom section is got\r
2878 # @param MacroDict dictionary used to replace macro\r
2879 #\r
2880 def __GetSectionData(self, FfsFileObj, MacroDict = {}):\r
2881 Dict = {}\r
2882 Dict.update(MacroDict)\r
2883\r
2884 self.__GetDefineStatements(FfsFileObj)\r
2885\r
2886 Dict.update(FfsFileObj.DefineVarDict)\r
2887 self.__GetAprioriSection(FfsFileObj, Dict.copy())\r
2888 self.__GetAprioriSection(FfsFileObj, Dict.copy())\r
2889\r
2890 while True:\r
2891 IsLeafSection = self.__GetLeafSection(FfsFileObj, Dict)\r
2892 IsEncapSection = self.__GetEncapsulationSec(FfsFileObj)\r
2893 if not IsLeafSection and not IsEncapSection:\r
2894 break\r
2895\r
2896 ## __GetLeafSection() method\r
2897 #\r
2898 # Get leaf section for Obj\r
2899 #\r
2900 # @param self The object pointer\r
2901 # @param Obj for whom leaf section is got\r
2902 # @param MacroDict dictionary used to replace macro\r
2903 # @retval True Successfully find section statement\r
2904 # @retval False Not able to find section statement\r
2905 #\r
2906 def __GetLeafSection(self, Obj, MacroDict = {}):\r
2907\r
2908 OldPos = self.GetFileBufferPos()\r
2909\r
2910 if not self.__IsKeyword( "SECTION"):\r
2911 if len(Obj.SectionList) == 0:\r
2912 raise Warning("expected SECTION", self.FileName, self.CurrentLineNumber)\r
2913 else:\r
2914 return False\r
2915\r
2916 AlignValue = None\r
2917 if self.__GetAlignment():\r
ccaa7754 2918 if self.__Token not in ("Auto", "8", "16", "32", "64", "128", "512", "1K", "4K", "32K", "64K", "128K",\r
e921f58d 2919 "256K", "512K", "1M", "2M", "4M", "8M", "16M"):\r
52302d4d 2920 raise Warning("Incorrect alignment '%s'" % self.__Token, self.FileName, self.CurrentLineNumber)\r
30fdf114
LG
2921 AlignValue = self.__Token\r
2922\r
2923 BuildNum = None\r
2924 if self.__IsKeyword( "BUILD_NUM"):\r
2925 if not self.__IsToken( "="):\r
2926 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r