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