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