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