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