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