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