BaseTools: FdfParser and FdfParserLite share reg exp
[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
30fdf114
LG
25\r
26##define T_CHAR_SPACE ' '\r
27##define T_CHAR_NULL '\0'\r
28##define T_CHAR_CR '\r'\r
29##define T_CHAR_TAB '\t'\r
30##define T_CHAR_LF '\n'\r
31##define T_CHAR_SLASH '/'\r
32##define T_CHAR_BACKSLASH '\\'\r
33##define T_CHAR_DOUBLE_QUOTE '\"'\r
34##define T_CHAR_SINGLE_QUOTE '\''\r
35##define T_CHAR_STAR '*'\r
36##define T_CHAR_HASH '#'\r
37\r
38(T_CHAR_SPACE, T_CHAR_NULL, T_CHAR_CR, T_CHAR_TAB, T_CHAR_LF, T_CHAR_SLASH, \\r
39T_CHAR_BACKSLASH, T_CHAR_DOUBLE_QUOTE, T_CHAR_SINGLE_QUOTE, T_CHAR_STAR, T_CHAR_HASH) = \\r
40(' ', '\0', '\r', '\t', '\n', '/', '\\', '\"', '\'', '*', '#')\r
41\r
42SEPERATOR_TUPLE = ('=', '|', ',', '{', '}') \r
43\r
44IncludeFileList = []\r
45# Macro passed from command line, which has greatest priority and can NOT be overridden by those in FDF\r
46InputMacroDict = {}\r
47# All Macro values when parsing file, not replace existing Macro\r
48AllMacroList = []\r
49\r
4d603daa
CJ
50FileExtensionPattern = re.compile(r'([a-zA-Z][a-zA-Z0-9]*)')\r
51\r
30fdf114
LG
52def GetRealFileLine (File, Line):\r
53 \r
54 InsertedLines = 0\r
55 for Profile in IncludeFileList:\r
56 if Line >= Profile.InsertStartLineNumber and Line < Profile.InsertStartLineNumber + Profile.InsertAdjust + len(Profile.FileLinesList):\r
57 return (Profile.FileName, Line - Profile.InsertStartLineNumber + 1)\r
58 if Line >= Profile.InsertStartLineNumber + Profile.InsertAdjust + len(Profile.FileLinesList):\r
59 InsertedLines += Profile.InsertAdjust + len(Profile.FileLinesList)\r
60 \r
61 return (File, Line - InsertedLines)\r
62\r
63## The exception class that used to report error messages when parsing FDF\r
64#\r
65# Currently the "ToolName" is set to be "FDF Parser".\r
66#\r
67class Warning (Exception):\r
68 ## The constructor\r
69 #\r
70 # @param self The object pointer\r
71 # @param Str The message to record\r
72 # @param File The FDF name\r
73 # @param Line The Line number that error occurs\r
74 #\r
47fea6af
YZ
75 def __init__(self, Str, File=None, Line=None):\r
76\r
30fdf114
LG
77 FileLineTuple = GetRealFileLine(File, Line)\r
78 self.FileName = FileLineTuple[0]\r
79 self.LineNumber = FileLineTuple[1]\r
80 self.message = Str + str(self.LineNumber)\r
81 self.ToolName = 'FDF Parser'\r
82 \r
83## The MACRO class that used to record macro value data when parsing include file\r
84#\r
85#\r
86class MacroProfile :\r
87 ## The constructor\r
88 #\r
89 # @param self The object pointer\r
90 # @param FileName The file that to be parsed\r
91 #\r
92 def __init__(self, FileName, Line):\r
93 self.FileName = FileName \r
94 self.DefinedAtLine = Line\r
95 self.MacroName = None\r
96 self.MacroValue = None\r
97\r
98## The Include file content class that used to record file data when parsing include file\r
99#\r
100# May raise Exception when opening file.\r
101#\r
102class IncludeFileProfile :\r
103 ## The constructor\r
104 #\r
105 # @param self The object pointer\r
106 # @param FileName The file that to be parsed\r
107 #\r
108 def __init__(self, FileName):\r
109 self.FileName = FileName\r
110 self.FileLinesList = []\r
111 try:\r
112 fsock = open(FileName, "rb", 0)\r
113 try:\r
114 self.FileLinesList = fsock.readlines()\r
115 finally:\r
116 fsock.close()\r
117\r
118 except IOError:\r
119 raise Warning("Error when opening file %s" % FileName)\r
120 \r
121 self.InsertStartLineNumber = None\r
122 self.InsertAdjust = 0\r
123\r
124## The FDF content class that used to record file data when parsing FDF\r
125#\r
126# May raise Exception when opening file.\r
127#\r
128class FileProfile :\r
129 ## The constructor\r
130 #\r
131 # @param self The object pointer\r
132 # @param FileName The file that to be parsed\r
133 #\r
134 def __init__(self, FileName):\r
135 self.FileLinesList = []\r
136 try:\r
137 fsock = open(FileName, "rb", 0)\r
138 try:\r
139 self.FileLinesList = fsock.readlines()\r
140 finally:\r
141 fsock.close()\r
142\r
143 except IOError:\r
144 raise Warning("Error when opening file %s" % FileName)\r
145 \r
146 self.PcdDict = {}\r
147 self.InfList = []\r
148 \r
149 self.PcdFileLineDict = {}\r
150 self.InfFileLineList = []\r
151 \r
152 self.FdDict = {}\r
153 self.FvDict = {}\r
154 self.CapsuleList = []\r
155# self.VtfList = []\r
156# self.RuleDict = {}\r
157\r
158## The syntax parser for FDF\r
159#\r
160# PreprocessFile method should be called prior to ParseFile\r
161# CycleReferenceCheck method can detect cycles in FDF contents\r
162#\r
163# GetNext*** procedures mean these procedures will get next token first, then make judgement.\r
164# Get*** procedures mean these procedures will make judgement on current token only.\r
165# \r
166class FdfParser(object):\r
167 ## The constructor\r
168 #\r
169 # @param self The object pointer\r
170 # @param FileName The file that to be parsed\r
171 #\r
172 def __init__(self, FileName):\r
173 self.Profile = FileProfile(FileName)\r
174 self.FileName = FileName\r
175 self.CurrentLineNumber = 1\r
176 self.CurrentOffsetWithinLine = 0\r
177 self.CurrentFdName = None\r
178 self.CurrentFvName = None\r
179 self.__Token = ""\r
180 self.__SkippedChars = ""\r
181 \r
182 self.__WipeOffArea = []\r
183\r
184 ## __IsWhiteSpace() method\r
185 #\r
186 # Whether char at current FileBufferPos is whitespace\r
187 #\r
188 # @param self The object pointer\r
189 # @param Char The char to test\r
190 # @retval True The char is a kind of white space\r
191 # @retval False The char is NOT a kind of white space\r
192 #\r
193 def __IsWhiteSpace(self, Char):\r
194 if Char in (T_CHAR_NULL, T_CHAR_CR, T_CHAR_SPACE, T_CHAR_TAB, T_CHAR_LF):\r
195 return True\r
196 else:\r
197 return False\r
198\r
199 ## __SkipWhiteSpace() method\r
200 #\r
201 # Skip white spaces from current char, return number of chars skipped\r
202 #\r
203 # @param self The object pointer\r
204 # @retval Count The number of chars skipped\r
205 #\r
206 def __SkipWhiteSpace(self):\r
207 Count = 0\r
208 while not self.__EndOfFile():\r
209 Count += 1\r
210 if self.__CurrentChar() in (T_CHAR_NULL, T_CHAR_CR, T_CHAR_LF, T_CHAR_SPACE, T_CHAR_TAB):\r
211 self.__SkippedChars += str(self.__CurrentChar())\r
212 self.__GetOneChar()\r
213\r
214 else:\r
215 Count = Count - 1\r
216 return Count\r
217\r
218 ## __EndOfFile() method\r
219 #\r
220 # Judge current buffer pos is at file end\r
221 #\r
222 # @param self The object pointer\r
223 # @retval True Current File buffer position is at file end\r
224 # @retval False Current File buffer position is NOT at file end\r
225 #\r
226 def __EndOfFile(self):\r
227 NumberOfLines = len(self.Profile.FileLinesList)\r
228 SizeOfLastLine = len(self.Profile.FileLinesList[-1])\r
229 if self.CurrentLineNumber == NumberOfLines and self.CurrentOffsetWithinLine >= SizeOfLastLine - 1:\r
230 return True\r
231 elif self.CurrentLineNumber > NumberOfLines:\r
232 return True\r
233 else:\r
234 return False\r
235\r
236 ## __EndOfLine() method\r
237 #\r
238 # Judge current buffer pos is at line end\r
239 #\r
240 # @param self The object pointer\r
241 # @retval True Current File buffer position is at line end\r
242 # @retval False Current File buffer position is NOT at line end\r
243 #\r
244 def __EndOfLine(self):\r
245 if self.CurrentLineNumber > len(self.Profile.FileLinesList):\r
246 return True\r
247 SizeOfCurrentLine = len(self.Profile.FileLinesList[self.CurrentLineNumber - 1])\r
248 if self.CurrentOffsetWithinLine >= SizeOfCurrentLine:\r
249 return True\r
250 else:\r
251 return False\r
252 \r
253 ## Rewind() method\r
254 #\r
255 # Reset file data buffer to the initial state\r
256 #\r
257 # @param self The object pointer\r
258 #\r
259 def Rewind(self):\r
260 self.CurrentLineNumber = 1\r
261 self.CurrentOffsetWithinLine = 0\r
262 \r
263 ## __UndoOneChar() method\r
264 #\r
265 # Go back one char in the file buffer\r
266 #\r
267 # @param self The object pointer\r
268 # @retval True Successfully go back one char\r
269 # @retval False Not able to go back one char as file beginning reached\r
270 # \r
271 def __UndoOneChar(self):\r
272 \r
273 if self.CurrentLineNumber == 1 and self.CurrentOffsetWithinLine == 0:\r
274 return False\r
275 elif self.CurrentOffsetWithinLine == 0:\r
276 self.CurrentLineNumber -= 1\r
277 self.CurrentOffsetWithinLine = len(self.__CurrentLine()) - 1\r
278 else:\r
279 self.CurrentOffsetWithinLine -= 1\r
280 return True\r
281 \r
282 ## __GetOneChar() method\r
283 #\r
284 # Move forward one char in the file buffer\r
285 #\r
286 # @param self The object pointer\r
287 # \r
288 def __GetOneChar(self):\r
289 if self.CurrentOffsetWithinLine == len(self.Profile.FileLinesList[self.CurrentLineNumber - 1]) - 1:\r
290 self.CurrentLineNumber += 1\r
291 self.CurrentOffsetWithinLine = 0\r
292 else:\r
293 self.CurrentOffsetWithinLine += 1\r
294\r
295 ## __CurrentChar() method\r
296 #\r
297 # Get the char pointed to by the file buffer pointer\r
298 #\r
299 # @param self The object pointer\r
300 # @retval Char Current char\r
301 # \r
302 def __CurrentChar(self):\r
303 return self.Profile.FileLinesList[self.CurrentLineNumber - 1][self.CurrentOffsetWithinLine]\r
304 \r
305 ## __NextChar() method\r
306 #\r
307 # Get the one char pass the char pointed to by the file buffer pointer\r
308 #\r
309 # @param self The object pointer\r
310 # @retval Char Next char\r
311 #\r
312 def __NextChar(self):\r
313 if self.CurrentOffsetWithinLine == len(self.Profile.FileLinesList[self.CurrentLineNumber - 1]) - 1:\r
314 return self.Profile.FileLinesList[self.CurrentLineNumber][0]\r
315 else:\r
316 return self.Profile.FileLinesList[self.CurrentLineNumber - 1][self.CurrentOffsetWithinLine + 1]\r
317 \r
318 ## __SetCurrentCharValue() method\r
319 #\r
320 # Modify the value of current char\r
321 #\r
322 # @param self The object pointer\r
323 # @param Value The new value of current char\r
324 #\r
325 def __SetCurrentCharValue(self, Value):\r
326 self.Profile.FileLinesList[self.CurrentLineNumber - 1][self.CurrentOffsetWithinLine] = Value\r
327 \r
328 ## __CurrentLine() method\r
329 #\r
330 # Get the list that contains current line contents\r
331 #\r
332 # @param self The object pointer\r
333 # @retval List current line contents\r
334 #\r
335 def __CurrentLine(self):\r
336 return self.Profile.FileLinesList[self.CurrentLineNumber - 1]\r
337 \r
338 def __StringToList(self):\r
339 self.Profile.FileLinesList = [list(s) for s in self.Profile.FileLinesList]\r
340 self.Profile.FileLinesList[-1].append(' ')\r
341\r
342 def __ReplaceMacros(self, Str, File, Line):\r
343 MacroEnd = 0\r
344 while Str.find('$(', MacroEnd) >= 0:\r
345 MacroStart = Str.find('$(', MacroEnd)\r
346 if Str.find(')', MacroStart) > 0:\r
347 MacroEnd = Str.find(')', MacroStart)\r
348 Name = Str[MacroStart + 2 : MacroEnd]\r
349 Value = None\r
350 if Name in InputMacroDict:\r
351 Value = InputMacroDict[Name]\r
352 \r
353 else:\r
354 for Profile in AllMacroList:\r
355 if Profile.FileName == File and Profile.MacroName == Name and Profile.DefinedAtLine <= Line:\r
356 Value = Profile.MacroValue\r
357 \r
358 if Value != None:\r
359 Str = Str.replace('$(' + Name + ')', Value)\r
360 MacroEnd = MacroStart + len(Value) \r
361 \r
362 else:\r
363 raise Warning("Macro not complete At Line ", self.FileName, self.CurrentLineNumber)\r
364 return Str\r
47fea6af
YZ
365\r
366 def __ReplaceFragment(self, StartPos, EndPos, Value=' '):\r
30fdf114
LG
367 if StartPos[0] == EndPos[0]:\r
368 Offset = StartPos[1]\r
369 while Offset <= EndPos[1]:\r
370 self.Profile.FileLinesList[StartPos[0]][Offset] = Value\r
371 Offset += 1\r
372 return\r
373 \r
374 Offset = StartPos[1]\r
375 while self.Profile.FileLinesList[StartPos[0]][Offset] not in ('\r', '\n'):\r
376 self.Profile.FileLinesList[StartPos[0]][Offset] = Value\r
377 Offset += 1\r
378 \r
379 Line = StartPos[0]\r
380 while Line < EndPos[0]:\r
381 Offset = 0\r
382 while self.Profile.FileLinesList[Line][Offset] not in ('\r', '\n'):\r
383 self.Profile.FileLinesList[Line][Offset] = Value\r
384 Offset += 1\r
385 Line += 1\r
386 \r
387 Offset = 0\r
388 while Offset <= EndPos[1]:\r
389 self.Profile.FileLinesList[EndPos[0]][Offset] = Value\r
390 Offset += 1\r
d5d56f1b
LG
391\r
392\r
393 def __GetMacroName(self):\r
394 if not self.__GetNextToken():\r
395 raise Warning("expected Macro name", self.FileName, self.CurrentLineNumber)\r
396 MacroName = self.__Token\r
397 NotFlag = False\r
398 if MacroName.startswith('!'):\r
399 NotFlag = True\r
400 MacroName = MacroName[1:].strip()\r
401 \r
402 if not MacroName.startswith('$(') or not MacroName.endswith(')'):\r
403 raise Warning("Macro name expected(Please use '$(%(Token)s)' if '%(Token)s' is a macro.)" % {"Token" : MacroName},\r
404 self.FileName, self.CurrentLineNumber)\r
405 MacroName = MacroName[2:-1]\r
406 return MacroName, NotFlag\r
30fdf114
LG
407 \r
408 ## PreprocessFile() method\r
409 #\r
410 # Preprocess file contents, replace comments with spaces.\r
411 # In the end, rewind the file buffer pointer to the beginning\r
412 # BUGBUG: No !include statement processing contained in this procedure\r
413 # !include statement should be expanded at the same FileLinesList[CurrentLineNumber - 1]\r
414 #\r
415 # @param self The object pointer\r
416 # \r
417 def PreprocessFile(self):\r
418 \r
419 self.Rewind()\r
420 InComment = False\r
421 DoubleSlashComment = False\r
422 HashComment = False\r
423 # HashComment in quoted string " " is ignored.\r
424 InString = False \r
425 \r
426 while not self.__EndOfFile():\r
427 \r
428 if self.__CurrentChar() == T_CHAR_DOUBLE_QUOTE and not InComment:\r
429 InString = not InString\r
430 # meet new line, then no longer in a comment for // and '#'\r
431 if self.__CurrentChar() == T_CHAR_LF:\r
432 self.CurrentLineNumber += 1\r
433 self.CurrentOffsetWithinLine = 0\r
434 if InComment and DoubleSlashComment:\r
435 InComment = False\r
436 DoubleSlashComment = False\r
437 if InComment and HashComment:\r
438 InComment = False\r
439 HashComment = False\r
440 # check for */ comment end\r
441 elif InComment and not DoubleSlashComment and not HashComment and self.__CurrentChar() == T_CHAR_STAR and self.__NextChar() == T_CHAR_SLASH:\r
442 self.__SetCurrentCharValue(T_CHAR_SPACE)\r
443 self.__GetOneChar()\r
444 self.__SetCurrentCharValue(T_CHAR_SPACE)\r
445 self.__GetOneChar()\r
446 InComment = False\r
447 # set comments to spaces\r
448 elif InComment:\r
449 self.__SetCurrentCharValue(T_CHAR_SPACE)\r
450 self.__GetOneChar()\r
451 # check for // comment\r
452 elif self.__CurrentChar() == T_CHAR_SLASH and self.__NextChar() == T_CHAR_SLASH and not self.__EndOfLine():\r
453 InComment = True\r
454 DoubleSlashComment = True\r
455 # check for '#' comment\r
456 elif self.__CurrentChar() == T_CHAR_HASH and not self.__EndOfLine() and not InString:\r
457 InComment = True\r
458 HashComment = True\r
459 # check for /* comment start\r
460 elif self.__CurrentChar() == T_CHAR_SLASH and self.__NextChar() == T_CHAR_STAR:\r
461 self.__SetCurrentCharValue( T_CHAR_SPACE)\r
462 self.__GetOneChar()\r
463 self.__SetCurrentCharValue( T_CHAR_SPACE)\r
464 self.__GetOneChar()\r
465 InComment = True\r
466 else:\r
467 self.__GetOneChar()\r
468 \r
469 # restore from ListOfList to ListOfString\r
470 self.Profile.FileLinesList = ["".join(list) for list in self.Profile.FileLinesList]\r
471 self.Rewind()\r
472\r
473 ## PreprocessIncludeFile() method\r
474 #\r
475 # Preprocess file contents, replace !include statements with file contents.\r
476 # In the end, rewind the file buffer pointer to the beginning\r
477 #\r
478 # @param self The object pointer\r
479 # \r
480 def PreprocessIncludeFile(self):\r
481 \r
482 while self.__GetNextToken():\r
483 \r
484 if self.__Token == '!include':\r
485 IncludeLine = self.CurrentLineNumber\r
486 IncludeOffset = self.CurrentOffsetWithinLine - len('!include')\r
487 if not self.__GetNextToken():\r
488 raise Warning("expected include file name At Line ", self.FileName, self.CurrentLineNumber)\r
489 IncFileName = self.__Token\r
490 if not os.path.isabs(IncFileName):\r
491 if IncFileName.startswith('$(WORKSPACE)'):\r
05cc51ad
LY
492 Str = mws.handleWsMacro(IncFileName)\r
493 Str = Str.replace('$(WORKSPACE)', os.environ.get('WORKSPACE'))\r
30fdf114
LG
494 if os.path.exists(Str):\r
495 if not os.path.isabs(Str):\r
496 Str = os.path.abspath(Str)\r
497 IncFileName = Str\r
498 else:\r
499 # file is in the same dir with FDF file\r
500 FullFdf = self.FileName\r
501 if not os.path.isabs(self.FileName):\r
05cc51ad 502 FullFdf = mws.join(os.environ.get('WORKSPACE'), self.FileName)\r
30fdf114
LG
503 \r
504 IncFileName = os.path.join(os.path.dirname(FullFdf), IncFileName)\r
505 \r
506 if not os.path.exists(os.path.normpath(IncFileName)):\r
507 raise Warning("Include file not exists At Line ", self.FileName, self.CurrentLineNumber)\r
508 \r
509 IncFileProfile = IncludeFileProfile(os.path.normpath(IncFileName))\r
510 \r
511 CurrentLine = self.CurrentLineNumber\r
512 CurrentOffset = self.CurrentOffsetWithinLine\r
513 # list index of the insertion, note that line number is 'CurrentLine + 1'\r
514 InsertAtLine = CurrentLine\r
515 IncFileProfile.InsertStartLineNumber = InsertAtLine + 1\r
516 # deal with remaining portions after "!include filename", if exists.\r
517 if self.__GetNextToken():\r
518 if self.CurrentLineNumber == CurrentLine:\r
519 RemainingLine = self.__CurrentLine()[CurrentOffset:]\r
520 self.Profile.FileLinesList.insert(self.CurrentLineNumber, RemainingLine)\r
521 IncFileProfile.InsertAdjust += 1\r
522 self.CurrentLineNumber += 1\r
523 self.CurrentOffsetWithinLine = 0\r
524 \r
525 for Line in IncFileProfile.FileLinesList:\r
526 self.Profile.FileLinesList.insert(InsertAtLine, Line)\r
527 self.CurrentLineNumber += 1\r
528 InsertAtLine += 1 \r
529 \r
530 IncludeFileList.append(IncFileProfile)\r
531 \r
532 # comment out the processed include file statement\r
533 TempList = list(self.Profile.FileLinesList[IncludeLine - 1])\r
534 TempList.insert(IncludeOffset, '#')\r
535 self.Profile.FileLinesList[IncludeLine - 1] = ''.join(TempList)\r
536 \r
537 self.Rewind()\r
538\r
539 ## PreprocessIncludeFile() method\r
540 #\r
541 # Preprocess file contents, replace !include statements with file contents.\r
542 # In the end, rewind the file buffer pointer to the beginning\r
543 #\r
544 # @param self The object pointer\r
545 # \r
546 def PreprocessConditionalStatement(self):\r
547 # IfList is a stack of if branches with elements of list [Pos, CondSatisfied, BranchDetermined]\r
548 IfList = []\r
549 while self.__GetNextToken():\r
550 if self.__Token == 'DEFINE':\r
551 DefineLine = self.CurrentLineNumber - 1\r
552 DefineOffset = self.CurrentOffsetWithinLine - len('DEFINE')\r
553 if not self.__GetNextToken():\r
554 raise Warning("expected Macro name At Line ", self.FileName, self.CurrentLineNumber)\r
555 Macro = self.__Token\r
556 if not self.__IsToken( "="):\r
557 raise Warning("expected '=' At Line ", self.FileName, self.CurrentLineNumber)\r
558 \r
559 if not self.__GetNextToken():\r
560 raise Warning("expected value At Line ", self.FileName, self.CurrentLineNumber)\r
561 \r
562 if self.__GetStringData():\r
563 pass\r
564 Value = self.__Token\r
565 if not Macro in InputMacroDict:\r
566 FileLineTuple = GetRealFileLine(self.FileName, DefineLine + 1)\r
567 MacProfile = MacroProfile(FileLineTuple[0], FileLineTuple[1])\r
568 MacProfile.MacroName = Macro\r
569 MacProfile.MacroValue = Value\r
570 AllMacroList.append(MacProfile)\r
571 self.__WipeOffArea.append(((DefineLine, DefineOffset), (self.CurrentLineNumber - 1, self.CurrentOffsetWithinLine - 1)))\r
572 \r
573 elif self.__Token in ('!ifdef', '!ifndef', '!if'):\r
574 IfStartPos = (self.CurrentLineNumber - 1, self.CurrentOffsetWithinLine - len(self.__Token))\r
575 IfList.append([IfStartPos, None, None])\r
576 CondLabel = self.__Token\r
577 \r
d5d56f1b 578 MacroName, NotFlag = self.__GetMacroName() \r
30fdf114
LG
579 NotDefineFlag = False\r
580 if CondLabel == '!ifndef':\r
581 NotDefineFlag = True\r
582 if CondLabel == '!ifdef' or CondLabel == '!ifndef':\r
583 if NotFlag:\r
584 raise Warning("'NOT' operation not allowed for Macro name At Line ", self.FileName, self.CurrentLineNumber)\r
585 \r
586 if CondLabel == '!if':\r
587 \r
588 if not self.__GetNextOp():\r
589 raise Warning("expected !endif At Line ", self.FileName, self.CurrentLineNumber)\r
590 \r
591 if self.__Token in ('!=', '==', '>', '<', '>=', '<='):\r
592 Op = self.__Token\r
593 if not self.__GetNextToken():\r
594 raise Warning("expected value At Line ", self.FileName, self.CurrentLineNumber)\r
595 if self.__GetStringData():\r
596 pass\r
597 MacroValue = self.__Token\r
598 ConditionSatisfied = self.__EvaluateConditional(MacroName, IfList[-1][0][0] + 1, Op, MacroValue)\r
599 if NotFlag:\r
600 ConditionSatisfied = not ConditionSatisfied\r
601 BranchDetermined = ConditionSatisfied\r
602 else:\r
603 self.CurrentOffsetWithinLine -= len(self.__Token) \r
604 ConditionSatisfied = self.__EvaluateConditional(MacroName, IfList[-1][0][0] + 1, None, 'Bool')\r
605 if NotFlag:\r
606 ConditionSatisfied = not ConditionSatisfied\r
607 BranchDetermined = ConditionSatisfied\r
608 IfList[-1] = [IfList[-1][0], ConditionSatisfied, BranchDetermined]\r
609 if ConditionSatisfied:\r
610 self.__WipeOffArea.append((IfList[-1][0], (self.CurrentLineNumber - 1, self.CurrentOffsetWithinLine - 1)))\r
611 \r
612 else:\r
613 ConditionSatisfied = self.__EvaluateConditional(MacroName, IfList[-1][0][0] + 1)\r
614 if NotDefineFlag:\r
615 ConditionSatisfied = not ConditionSatisfied\r
616 BranchDetermined = ConditionSatisfied\r
617 IfList[-1] = [IfList[-1][0], ConditionSatisfied, BranchDetermined]\r
618 if ConditionSatisfied:\r
619 self.__WipeOffArea.append((IfStartPos, (self.CurrentLineNumber - 1, self.CurrentOffsetWithinLine - 1)))\r
620 \r
621 elif self.__Token in ('!elseif', '!else'):\r
622 ElseStartPos = (self.CurrentLineNumber - 1, self.CurrentOffsetWithinLine - len(self.__Token))\r
623 if len(IfList) <= 0:\r
624 raise Warning("Missing !if statement At Line ", self.FileName, self.CurrentLineNumber)\r
625 if IfList[-1][1]:\r
626 IfList[-1] = [ElseStartPos, False, True]\r
627 self.__WipeOffArea.append((ElseStartPos, (self.CurrentLineNumber - 1, self.CurrentOffsetWithinLine - 1)))\r
628 else:\r
629 self.__WipeOffArea.append((IfList[-1][0], ElseStartPos))\r
630 IfList[-1] = [ElseStartPos, True, IfList[-1][2]]\r
631 if self.__Token == '!elseif':\r
d5d56f1b 632 MacroName, NotFlag = self.__GetMacroName() \r
30fdf114
LG
633 if not self.__GetNextOp():\r
634 raise Warning("expected !endif At Line ", self.FileName, self.CurrentLineNumber)\r
635 \r
636 if self.__Token in ('!=', '==', '>', '<', '>=', '<='):\r
637 Op = self.__Token\r
638 if not self.__GetNextToken():\r
639 raise Warning("expected value At Line ", self.FileName, self.CurrentLineNumber)\r
640 if self.__GetStringData():\r
641 pass\r
642 MacroValue = self.__Token\r
643 ConditionSatisfied = self.__EvaluateConditional(MacroName, IfList[-1][0][0] + 1, Op, MacroValue)\r
644 if NotFlag:\r
645 ConditionSatisfied = not ConditionSatisfied\r
646\r
647 else:\r
648 self.CurrentOffsetWithinLine -= len(self.__Token) \r
649 ConditionSatisfied = self.__EvaluateConditional(MacroName, IfList[-1][0][0] + 1, None, 'Bool')\r
650 if NotFlag:\r
651 ConditionSatisfied = not ConditionSatisfied\r
652\r
653 IfList[-1] = [IfList[-1][0], ConditionSatisfied, IfList[-1][2]]\r
654 \r
655 if IfList[-1][1]:\r
656 if IfList[-1][2]:\r
657 IfList[-1][1] = False\r
658 else:\r
659 IfList[-1][2] = True\r
660 self.__WipeOffArea.append((IfList[-1][0], (self.CurrentLineNumber - 1, self.CurrentOffsetWithinLine - 1)))\r
661\r
662 \r
663 elif self.__Token == '!endif':\r
664 if IfList[-1][1]:\r
665 self.__WipeOffArea.append(((self.CurrentLineNumber - 1, self.CurrentOffsetWithinLine - len('!endif')), (self.CurrentLineNumber - 1, self.CurrentOffsetWithinLine - 1)))\r
666 else:\r
667 self.__WipeOffArea.append((IfList[-1][0], (self.CurrentLineNumber - 1, self.CurrentOffsetWithinLine - 1)))\r
668 \r
669 IfList.pop()\r
670 \r
671 \r
672 if len(IfList) > 0:\r
673 raise Warning("Missing !endif At Line ", self.FileName, self.CurrentLineNumber)\r
674 self.Rewind()\r
675\r
676 def __EvaluateConditional(self, Name, Line, Op = None, Value = None):\r
677 \r
678 FileLineTuple = GetRealFileLine(self.FileName, Line)\r
679 if Name in InputMacroDict:\r
680 MacroValue = InputMacroDict[Name]\r
681 if Op == None:\r
682 if Value == 'Bool' and MacroValue == None or MacroValue.upper() == 'FALSE':\r
683 return False\r
684 return True\r
685 elif Op == '!=':\r
686 if Value != MacroValue:\r
687 return True\r
688 else:\r
689 return False\r
690 elif Op == '==':\r
691 if Value == MacroValue:\r
692 return True\r
693 else:\r
694 return False\r
695 else:\r
696 if (self.__IsHex(Value) or Value.isdigit()) and (self.__IsHex(MacroValue) or (MacroValue != None and MacroValue.isdigit())):\r
697 InputVal = long(Value, 0)\r
698 MacroVal = long(MacroValue, 0)\r
699 if Op == '>':\r
700 if MacroVal > InputVal:\r
701 return True\r
702 else:\r
703 return False\r
704 elif Op == '>=':\r
705 if MacroVal >= InputVal:\r
706 return True\r
707 else:\r
708 return False\r
709 elif Op == '<':\r
710 if MacroVal < InputVal:\r
711 return True\r
712 else:\r
713 return False\r
714 elif Op == '<=':\r
715 if MacroVal <= InputVal:\r
716 return True\r
717 else:\r
718 return False\r
719 else:\r
720 return False\r
721 else:\r
722 raise Warning("Value %s is not a number At Line ", self.FileName, Line)\r
723 \r
724 for Profile in AllMacroList:\r
725 if Profile.FileName == FileLineTuple[0] and Profile.MacroName == Name and Profile.DefinedAtLine <= FileLineTuple[1]:\r
726 if Op == None:\r
727 if Value == 'Bool' and Profile.MacroValue == None or Profile.MacroValue.upper() == 'FALSE':\r
728 return False\r
729 return True\r
730 elif Op == '!=':\r
731 if Value != Profile.MacroValue:\r
732 return True\r
733 else:\r
734 return False\r
735 elif Op == '==':\r
736 if Value == Profile.MacroValue:\r
737 return True\r
738 else:\r
739 return False\r
740 else:\r
741 if (self.__IsHex(Value) or Value.isdigit()) and (self.__IsHex(Profile.MacroValue) or (Profile.MacroValue != None and Profile.MacroValue.isdigit())):\r
742 InputVal = long(Value, 0)\r
743 MacroVal = long(Profile.MacroValue, 0)\r
744 if Op == '>':\r
745 if MacroVal > InputVal:\r
746 return True\r
747 else:\r
748 return False\r
749 elif Op == '>=':\r
750 if MacroVal >= InputVal:\r
751 return True\r
752 else:\r
753 return False\r
754 elif Op == '<':\r
755 if MacroVal < InputVal:\r
756 return True\r
757 else:\r
758 return False\r
759 elif Op == '<=':\r
760 if MacroVal <= InputVal:\r
761 return True\r
762 else:\r
763 return False\r
764 else:\r
765 return False\r
766 else:\r
767 raise Warning("Value %s is not a number At Line ", self.FileName, Line)\r
768 \r
769 return False\r
770\r
771 ## __IsToken() method\r
772 #\r
773 # Check whether input string is found from current char position along\r
774 # If found, the string value is put into self.__Token\r
775 #\r
776 # @param self The object pointer\r
777 # @param String The string to search\r
778 # @param IgnoreCase Indicate case sensitive/non-sensitive search, default is case sensitive\r
779 # @retval True Successfully find string, file buffer pointer moved forward\r
780 # @retval False Not able to find string, file buffer pointer not changed\r
781 #\r
782 def __IsToken(self, String, IgnoreCase = False):\r
783 self.__SkipWhiteSpace()\r
784\r
785 # Only consider the same line, no multi-line token allowed\r
786 StartPos = self.CurrentOffsetWithinLine\r
787 index = -1\r
788 if IgnoreCase:\r
789 index = self.__CurrentLine()[self.CurrentOffsetWithinLine : ].upper().find(String.upper()) \r
790 else:\r
791 index = self.__CurrentLine()[self.CurrentOffsetWithinLine : ].find(String)\r
792 if index == 0:\r
793 self.CurrentOffsetWithinLine += len(String)\r
794 self.__Token = self.__CurrentLine()[StartPos : self.CurrentOffsetWithinLine]\r
795 return True\r
796 return False\r
797\r
798 ## __IsKeyword() method\r
799 #\r
800 # Check whether input keyword is found from current char position along, whole word only!\r
801 # If found, the string value is put into self.__Token\r
802 #\r
803 # @param self The object pointer\r
804 # @param Keyword The string to search\r
805 # @param IgnoreCase Indicate case sensitive/non-sensitive search, default is case sensitive\r
806 # @retval True Successfully find string, file buffer pointer moved forward\r
807 # @retval False Not able to find string, file buffer pointer not changed\r
808 #\r
809 def __IsKeyword(self, KeyWord, IgnoreCase = False):\r
810 self.__SkipWhiteSpace()\r
811\r
812 # Only consider the same line, no multi-line token allowed\r
813 StartPos = self.CurrentOffsetWithinLine\r
814 index = -1\r
815 if IgnoreCase:\r
816 index = self.__CurrentLine()[self.CurrentOffsetWithinLine : ].upper().find(KeyWord.upper()) \r
817 else:\r
818 index = self.__CurrentLine()[self.CurrentOffsetWithinLine : ].find(KeyWord)\r
819 if index == 0:\r
820 followingChar = self.__CurrentLine()[self.CurrentOffsetWithinLine + len(KeyWord)]\r
821 if not str(followingChar).isspace() and followingChar not in SEPERATOR_TUPLE:\r
822 return False\r
823 self.CurrentOffsetWithinLine += len(KeyWord)\r
824 self.__Token = self.__CurrentLine()[StartPos : self.CurrentOffsetWithinLine]\r
825 return True\r
826 return False\r
827\r
828 ## __GetNextWord() method\r
829 #\r
830 # Get next C name from file lines\r
831 # If found, the string value is put into self.__Token\r
832 #\r
833 # @param self The object pointer\r
834 # @retval True Successfully find a C name string, file buffer pointer moved forward\r
835 # @retval False Not able to find a C name string, file buffer pointer not changed\r
836 #\r
837 def __GetNextWord(self):\r
838 self.__SkipWhiteSpace()\r
839 if self.__EndOfFile():\r
840 return False\r
841 \r
842 TempChar = self.__CurrentChar()\r
843 StartPos = self.CurrentOffsetWithinLine\r
844 if (TempChar >= 'a' and TempChar <= 'z') or (TempChar >= 'A' and TempChar <= 'Z') or TempChar == '_':\r
845 self.__GetOneChar()\r
846 while not self.__EndOfLine():\r
847 TempChar = self.__CurrentChar()\r
848 if (TempChar >= 'a' and TempChar <= 'z') or (TempChar >= 'A' and TempChar <= 'Z') \\r
849 or (TempChar >= '0' and TempChar <= '9') or TempChar == '_' or TempChar == '-':\r
850 self.__GetOneChar()\r
851 \r
852 else:\r
853 break\r
854\r
855 self.__Token = self.__CurrentLine()[StartPos : self.CurrentOffsetWithinLine]\r
856 return True\r
857 \r
858 return False\r
859 \r
860 ## __GetNextToken() method\r
861 #\r
862 # Get next token unit before a seperator\r
863 # If found, the string value is put into self.__Token\r
864 #\r
865 # @param self The object pointer\r
866 # @retval True Successfully find a token unit, file buffer pointer moved forward\r
867 # @retval False Not able to find a token unit, file buffer pointer not changed\r
868 #\r
869 def __GetNextToken(self):\r
870 # Skip leading spaces, if exist.\r
871 self.__SkipWhiteSpace()\r
872 if self.__EndOfFile():\r
873 return False\r
874 # Record the token start position, the position of the first non-space char.\r
875 StartPos = self.CurrentOffsetWithinLine\r
876 StartLine = self.CurrentLineNumber\r
877 while not self.__EndOfLine():\r
878 TempChar = self.__CurrentChar()\r
879 # Try to find the end char that is not a space and not in seperator tuple.\r
880 # That is, when we got a space or any char in the tuple, we got the end of token.\r
881 if not str(TempChar).isspace() and TempChar not in SEPERATOR_TUPLE:\r
882 self.__GetOneChar()\r
883 # if we happen to meet a seperator as the first char, we must proceed to get it.\r
884 # That is, we get a token that is a seperator char. nomally it is the boundary of other tokens.\r
885 elif StartPos == self.CurrentOffsetWithinLine and TempChar in SEPERATOR_TUPLE:\r
886 self.__GetOneChar()\r
887 break\r
888 else:\r
889 break\r
890# else:\r
891# return False\r
892\r
893 EndPos = self.CurrentOffsetWithinLine\r
894 if self.CurrentLineNumber != StartLine:\r
895 EndPos = len(self.Profile.FileLinesList[StartLine-1])\r
896 self.__Token = self.Profile.FileLinesList[StartLine-1][StartPos : EndPos]\r
897 if StartPos != self.CurrentOffsetWithinLine:\r
898 return True\r
899 else:\r
900 return False\r
901 \r
902 def __GetNextOp(self):\r
903 # Skip leading spaces, if exist.\r
904 self.__SkipWhiteSpace()\r
905 if self.__EndOfFile():\r
906 return False\r
907 # Record the token start position, the position of the first non-space char.\r
908 StartPos = self.CurrentOffsetWithinLine\r
909 while not self.__EndOfLine():\r
910 TempChar = self.__CurrentChar()\r
911 # Try to find the end char that is not a space\r
912 if not str(TempChar).isspace():\r
913 self.__GetOneChar()\r
914 else:\r
915 break\r
916 else:\r
917 return False\r
918 \r
919 if StartPos != self.CurrentOffsetWithinLine:\r
920 self.__Token = self.__CurrentLine()[StartPos : self.CurrentOffsetWithinLine]\r
921 return True\r
922 else:\r
923 return False\r
924 ## __GetNextGuid() method\r
925 #\r
926 # Get next token unit before a seperator\r
927 # If found, the GUID string is put into self.__Token\r
928 #\r
929 # @param self The object pointer\r
930 # @retval True Successfully find a registry format GUID, file buffer pointer moved forward\r
931 # @retval False Not able to find a registry format GUID, file buffer pointer not changed\r
932 #\r
933 def __GetNextGuid(self):\r
934 \r
935 if not self.__GetNextToken():\r
936 return False\r
2eb370ff 937 if RangeExpression.RegGuidPattern.match(self.__Token) != None:\r
30fdf114
LG
938 return True\r
939 else:\r
940 self.__UndoToken()\r
941 return False\r
942\r
943 ## __UndoToken() method\r
944 #\r
945 # Go back one token unit in file buffer\r
946 #\r
947 # @param self The object pointer\r
948 #\r
949 def __UndoToken(self):\r
950 self.__UndoOneChar()\r
951 while self.__CurrentChar().isspace():\r
952 if not self.__UndoOneChar():\r
953 self.__GetOneChar()\r
954 return\r
955 \r
956 \r
957 StartPos = self.CurrentOffsetWithinLine\r
958 CurrentLine = self.CurrentLineNumber\r
959 while CurrentLine == self.CurrentLineNumber:\r
960 \r
961 TempChar = self.__CurrentChar()\r
962 # Try to find the end char that is not a space and not in seperator tuple.\r
963 # That is, when we got a space or any char in the tuple, we got the end of token.\r
964 if not str(TempChar).isspace() and not TempChar in SEPERATOR_TUPLE:\r
965 if not self.__UndoOneChar():\r
966 break\r
967 # if we happen to meet a seperator as the first char, we must proceed to get it.\r
968 # That is, we get a token that is a seperator char. nomally it is the boundary of other tokens.\r
969 elif StartPos == self.CurrentOffsetWithinLine and TempChar in SEPERATOR_TUPLE:\r
970 return\r
971 else:\r
972 break\r
973 \r
974 self.__GetOneChar()\r
975 \r
976 ## __HexDigit() method\r
977 #\r
978 # Whether char input is a Hex data bit\r
979 #\r
980 # @param self The object pointer\r
981 # @param TempChar The char to test\r
982 # @retval True The char is a Hex data bit\r
983 # @retval False The char is NOT a Hex data bit\r
984 #\r
985 def __HexDigit(self, TempChar):\r
986 if (TempChar >= 'a' and TempChar <= 'f') or (TempChar >= 'A' and TempChar <= 'F') \\r
987 or (TempChar >= '0' and TempChar <= '9'):\r
988 return True\r
989 else:\r
990 return False\r
991 \r
992 def __IsHex(self, HexStr):\r
993 if not HexStr.upper().startswith("0X"):\r
994 return False\r
995 if len(self.__Token) <= 2:\r
996 return False\r
997 charList = [c for c in HexStr[2 : ] if not self.__HexDigit( c)]\r
998 if len(charList) == 0:\r
999 return True\r
1000 else:\r
1001 return False \r
1002 ## __GetNextHexNumber() method\r
1003 #\r
1004 # Get next HEX data before a seperator\r
1005 # If found, the HEX data is put into self.__Token\r
1006 #\r
1007 # @param self The object pointer\r
1008 # @retval True Successfully find a HEX data, file buffer pointer moved forward\r
1009 # @retval False Not able to find a HEX data, file buffer pointer not changed\r
1010 #\r
1011 def __GetNextHexNumber(self):\r
1012 if not self.__GetNextToken():\r
1013 return False\r
1014 if self.__IsHex(self.__Token):\r
1015 return True\r
1016 else:\r
1017 self.__UndoToken()\r
1018 return False\r
1019 \r
1020 ## __GetNextDecimalNumber() method\r
1021 #\r
1022 # Get next decimal data before a seperator\r
1023 # If found, the decimal data is put into self.__Token\r
1024 #\r
1025 # @param self The object pointer\r
1026 # @retval True Successfully find a decimal data, file buffer pointer moved forward\r
1027 # @retval False Not able to find a decimal data, file buffer pointer not changed\r
1028 #\r
1029 def __GetNextDecimalNumber(self):\r
1030 if not self.__GetNextToken():\r
1031 return False\r
1032 if self.__Token.isdigit():\r
1033 return True\r
1034 else:\r
1035 self.__UndoToken()\r
1036 return False\r
1037 \r
1038 ## __GetNextPcdName() method\r
1039 #\r
1040 # Get next PCD token space C name and PCD C name pair before a seperator\r
1041 # If found, the decimal data is put into self.__Token\r
1042 #\r
1043 # @param self The object pointer\r
1044 # @retval Tuple PCD C name and PCD token space C name pair \r
1045 #\r
1046 def __GetNextPcdName(self):\r
1047 if not self.__GetNextWord():\r
1048 raise Warning("expected PcdTokenSpaceCName.PcdCName At Line ", self.FileName, self.CurrentLineNumber)\r
1049 pcdTokenSpaceCName = self.__Token\r
1050 \r
1051 if not self.__IsToken( "."):\r
1052 raise Warning("expected PcdTokenSpaceCName.PcdCName At Line ", self.FileName, self.CurrentLineNumber)\r
1053 \r
1054 if not self.__GetNextWord():\r
1055 raise Warning("expected PcdTokenSpaceCName.PcdCName At Line ", self.FileName, self.CurrentLineNumber)\r
1056 pcdCName = self.__Token\r
1057 \r
1058 return (pcdCName, pcdTokenSpaceCName) \r
1059 \r
1060 ## __GetStringData() method\r
1061 #\r
1062 # Get string contents quoted in ""\r
1063 # If found, the decimal data is put into self.__Token\r
1064 #\r
1065 # @param self The object pointer\r
1066 # @retval True Successfully find a string data, file buffer pointer moved forward\r
1067 # @retval False Not able to find a string data, file buffer pointer not changed\r
1068 # \r
1069 def __GetStringData(self):\r
1070 if self.__Token.startswith("\"") or self.__Token.startswith("L\""):\r
1071 self.__UndoToken()\r
1072 self.__SkipToToken("\"")\r
1073 currentLineNumber = self.CurrentLineNumber\r
1074 \r
1075 if not self.__SkipToToken("\""):\r
1076 raise Warning("Missing Quote \" for String At Line ", self.FileName, self.CurrentLineNumber)\r
1077 if currentLineNumber != self.CurrentLineNumber:\r
1078 raise Warning("Missing Quote \" for String At Line ", self.FileName, self.CurrentLineNumber)\r
1079 self.__Token = self.__SkippedChars.rstrip('\"')\r
1080 return True\r
1081 \r
1082 elif self.__Token.startswith("\'") or self.__Token.startswith("L\'"):\r
1083 self.__UndoToken()\r
1084 self.__SkipToToken("\'")\r
1085 currentLineNumber = self.CurrentLineNumber\r
1086 \r
1087 if not self.__SkipToToken("\'"):\r
1088 raise Warning("Missing Quote \' for String At Line ", self.FileName, self.CurrentLineNumber)\r
1089 if currentLineNumber != self.CurrentLineNumber:\r
1090 raise Warning("Missing Quote \' for String At Line ", self.FileName, self.CurrentLineNumber)\r
1091 self.__Token = self.__SkippedChars.rstrip('\'')\r
1092 return True\r
1093 \r
1094 else:\r
1095 return False\r
1096 \r
1097 ## __SkipToToken() method\r
1098 #\r
1099 # Search forward in file buffer for the string\r
1100 # The skipped chars are put into self.__SkippedChars\r
1101 #\r
1102 # @param self The object pointer\r
1103 # @param String The string to search\r
1104 # @param IgnoreCase Indicate case sensitive/non-sensitive search, default is case sensitive\r
1105 # @retval True Successfully find the string, file buffer pointer moved forward\r
1106 # @retval False Not able to find the string, file buffer pointer not changed\r
1107 #\r
1108 def __SkipToToken(self, String, IgnoreCase = False):\r
1109 StartPos = self.GetFileBufferPos()\r
1110 \r
1111 self.__SkippedChars = ""\r
1112 while not self.__EndOfFile():\r
1113 index = -1\r
1114 if IgnoreCase:\r
1115 index = self.__CurrentLine()[self.CurrentOffsetWithinLine : ].upper().find(String.upper()) \r
1116 else:\r
1117 index = self.__CurrentLine()[self.CurrentOffsetWithinLine : ].find(String)\r
1118 if index == 0:\r
1119 self.CurrentOffsetWithinLine += len(String)\r
1120 self.__SkippedChars += String\r
1121 return True\r
1122 self.__SkippedChars += str(self.__CurrentChar())\r
1123 self.__GetOneChar()\r
1124 \r
1125 self.SetFileBufferPos( StartPos)\r
1126 self.__SkippedChars = ""\r
1127 return False\r
1128\r
1129 ## GetFileBufferPos() method\r
1130 #\r
1131 # Return the tuple of current line and offset within the line\r
1132 #\r
1133 # @param self The object pointer\r
1134 # @retval Tuple Line number and offset pair \r
1135 #\r
1136 def GetFileBufferPos(self):\r
1137 return (self.CurrentLineNumber, self.CurrentOffsetWithinLine)\r
1138 \r
1139 ## SetFileBufferPos() method\r
1140 #\r
1141 # Restore the file buffer position\r
1142 #\r
1143 # @param self The object pointer\r
1144 # @param Pos The new file buffer position\r
1145 #\r
1146 def SetFileBufferPos(self, Pos):\r
1147 (self.CurrentLineNumber, self.CurrentOffsetWithinLine) = Pos\r
1148 \r
1149 ## ParseFile() method\r
1150 #\r
1151 # Parse the file profile buffer to extract fd, fv ... information\r
1152 # Exception will be raised if syntax error found\r
1153 #\r
1154 # @param self The object pointer\r
1155 #\r
1156 def ParseFile(self):\r
1157\r
1158 try:\r
1159 self.__StringToList()\r
1160 self.PreprocessFile()\r
1161 self.PreprocessIncludeFile()\r
1162 self.__StringToList()\r
1163 self.PreprocessFile()\r
1164 self.PreprocessConditionalStatement()\r
1165 self.__StringToList()\r
1166 for Pos in self.__WipeOffArea:\r
1167 self.__ReplaceFragment(Pos[0], Pos[1])\r
1168 self.Profile.FileLinesList = ["".join(list) for list in self.Profile.FileLinesList]\r
1169 \r
1170 while self.__GetDefines():\r
1171 pass\r
1172 \r
1173 Index = 0\r
1174 while Index < len(self.Profile.FileLinesList):\r
1175 FileLineTuple = GetRealFileLine(self.FileName, Index + 1)\r
1176 self.Profile.FileLinesList[Index] = self.__ReplaceMacros(self.Profile.FileLinesList[Index], FileLineTuple[0], FileLineTuple[1])\r
1177 Index += 1\r
1178 \r
1179 while self.__GetFd():\r
1180 pass\r
1181\r
1182 while self.__GetFv():\r
1183 pass\r
1184\r
1185 while self.__GetCapsule():\r
1186 pass\r
1187\r
1188# while self.__GetVtf():\r
1189# pass\r
1190#\r
1191# while self.__GetRule():\r
1192# pass\r
1193 \r
1194\r
1195 except Warning, X:\r
1196 self.__UndoToken()\r
1197 FileLineTuple = GetRealFileLine(self.FileName, self.CurrentLineNumber)\r
1198 X.message += '\nGot Token: \"%s\" from File %s\n' % (self.__Token, FileLineTuple[0]) + \\r
1199 'Previous Token: \"%s\" At line: %d, Offset Within Line: %d\n' \\r
1200 % (self.Profile.FileLinesList[self.CurrentLineNumber - 1][self.CurrentOffsetWithinLine :].rstrip('\n').rstrip('\r'), FileLineTuple[1], self.CurrentOffsetWithinLine)\r
1201 raise\r
1202\r
1203 ## __GetDefines() method\r
1204 #\r
1205 # Get Defines section contents and store its data into AllMacrosList\r
1206 #\r
1207 # @param self The object pointer\r
1208 # @retval True Successfully find a Defines\r
1209 # @retval False Not able to find a Defines\r
1210 #\r
1211 def __GetDefines(self):\r
1212\r
1213 if not self.__GetNextToken():\r
1214 return False\r
1215\r
1216 S = self.__Token.upper()\r
1217 if S.startswith("[") and not S.startswith("[DEFINES"):\r
1218 if not S.startswith("[FD.") and not S.startswith("[FV.") and not S.startswith("[CAPSULE.") \\r
1219 and not S.startswith("[VTF.") and not S.startswith("[RULE.") and not S.startswith("[OPTIONROM."):\r
1220 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
1221 self.__UndoToken()\r
1222 return False\r
1223\r
1224 self.__UndoToken()\r
1225 if not self.__IsToken("[DEFINES", True):\r
1226 FileLineTuple = GetRealFileLine(self.FileName, self.CurrentLineNumber)\r
1227 #print 'Parsing String: %s in File %s, At line: %d, Offset Within Line: %d' \\r
1228 # % (self.Profile.FileLinesList[self.CurrentLineNumber - 1][self.CurrentOffsetWithinLine :], FileLineTuple[0], FileLineTuple[1], self.CurrentOffsetWithinLine)\r
1229 raise Warning("expected [DEFINES", self.FileName, self.CurrentLineNumber)\r
1230\r
1231 if not self.__IsToken( "]"):\r
1232 raise Warning("expected ']'", self.FileName, self.CurrentLineNumber)\r
1233\r
1234 while self.__GetNextWord():\r
1235 Macro = self.__Token\r
1236 \r
1237 if not self.__IsToken("="):\r
1238 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
1239 if not self.__GetNextToken() or self.__Token.startswith('['):\r
1240 raise Warning("expected MACRO value", self.FileName, self.CurrentLineNumber)\r
1241 Value = self.__Token\r
1242 FileLineTuple = GetRealFileLine(self.FileName, self.CurrentLineNumber)\r
1243 MacProfile = MacroProfile(FileLineTuple[0], FileLineTuple[1])\r
1244 MacProfile.MacroName = Macro\r
1245 MacProfile.MacroValue = Value\r
1246 AllMacroList.append(MacProfile)\r
1247\r
1248 return False\r
1249\r
1250 ## __GetFd() method\r
1251 #\r
1252 # Get FD section contents and store its data into FD dictionary of self.Profile\r
1253 #\r
1254 # @param self The object pointer\r
1255 # @retval True Successfully find a FD\r
1256 # @retval False Not able to find a FD\r
1257 #\r
1258 def __GetFd(self):\r
1259\r
1260 if not self.__GetNextToken():\r
1261 return False\r
1262 \r
1263 S = self.__Token.upper()\r
1264 if S.startswith("[") and not S.startswith("[FD."):\r
1265 if not S.startswith("[FV.") and not S.startswith("[CAPSULE.") \\r
1266 and not S.startswith("[VTF.") and not S.startswith("[RULE."):\r
1267 raise Warning("Unknown section At Line ", self.FileName, self.CurrentLineNumber)\r
1268 self.__UndoToken()\r
1269 return False\r
1270 \r
1271 self.__UndoToken()\r
1272 if not self.__IsToken("[FD.", True):\r
1273 FileLineTuple = GetRealFileLine(self.FileName, self.CurrentLineNumber)\r
1274 print 'Parsing String: %s in File %s, At line: %d, Offset Within Line: %d' \\r
1275 % (self.Profile.FileLinesList[self.CurrentLineNumber - 1][self.CurrentOffsetWithinLine :], FileLineTuple[0], FileLineTuple[1], self.CurrentOffsetWithinLine)\r
1276 raise Warning("expected [FD.] At Line ", self.FileName, self.CurrentLineNumber)\r
1277 \r
1278 FdName = self.__GetUiName()\r
1279 self.CurrentFdName = FdName.upper()\r
1280 \r
1281 if not self.__IsToken( "]"):\r
1282 raise Warning("expected ']' At Line ", self.FileName, self.CurrentLineNumber)\r
1283 \r
1284 FdObj = CommonDataClass.FdfClass.FDClassObject()\r
1285 FdObj.FdUiName = self.CurrentFdName\r
1286 self.Profile.FdDict[self.CurrentFdName] = FdObj\r
1287 Status = self.__GetCreateFile(FdObj)\r
1288 if not Status:\r
1289 raise Warning("FD name error At Line ", self.FileName, self.CurrentLineNumber)\r
1290 \r
1291 if not self.__GetTokenStatements(FdObj):\r
1292 return False\r
1293 \r
1294 self.__GetDefineStatements(FdObj)\r
1295\r
1296 self.__GetSetStatements(FdObj)\r
1297\r
1298 if not self.__GetRegionLayout(FdObj):\r
1299 raise Warning("expected region layout At Line ", self.FileName, self.CurrentLineNumber)\r
1300 \r
1301 while self.__GetRegionLayout(FdObj):\r
1302 pass\r
1303 return True\r
1304 \r
1305 ## __GetUiName() method\r
1306 #\r
1307 # Return the UI name of a section\r
1308 #\r
1309 # @param self The object pointer\r
1310 # @retval FdName UI name\r
1311 #\r
1312 def __GetUiName(self):\r
1313 FdName = ""\r
1314 if self.__GetNextWord():\r
1315 FdName = self.__Token\r
1316 \r
1317 return FdName\r
1318\r
1319 ## __GetCreateFile() method\r
1320 #\r
1321 # Return the output file name of object\r
1322 #\r
1323 # @param self The object pointer\r
1324 # @param Obj object whose data will be stored in file\r
1325 # @retval FdName UI name\r
1326 #\r
1327 def __GetCreateFile(self, Obj):\r
1328\r
1329 if self.__IsKeyword( "CREATE_FILE"):\r
1330 if not self.__IsToken( "="):\r
1331 raise Warning("expected '=' At Line ", self.FileName, self.CurrentLineNumber)\r
1332 \r
1333 if not self.__GetNextToken():\r
1334 raise Warning("expected file name At Line ", self.FileName, self.CurrentLineNumber)\r
1335 \r
1336 FileName = self.__Token\r
1337 Obj.CreateFileName = FileName\r
1338\r
1339 return True\r
1340\r
1341 ## __GetTokenStatements() method\r
1342 #\r
1343 # Get token statements\r
1344 #\r
1345 # @param self The object pointer\r
1346 # @param Obj for whom token statement is got\r
1347 # @retval True Successfully find a token statement\r
1348 # @retval False Not able to find a token statement\r
1349 #\r
1350 def __GetTokenStatements(self, Obj):\r
1351 if not self.__IsKeyword( "BaseAddress"):\r
1352 raise Warning("BaseAddress missing At Line ", self.FileName, self.CurrentLineNumber)\r
1353 \r
1354 if not self.__IsToken( "="):\r
1355 raise Warning("expected '=' At Line ", self.FileName, self.CurrentLineNumber)\r
1356 \r
1357 if not self.__GetNextHexNumber():\r
1358 raise Warning("expected Hex base address At Line ", self.FileName, self.CurrentLineNumber)\r
1359 \r
1360 Obj.BaseAddress = self.__Token\r
1361 \r
1362 if self.__IsToken( "|"):\r
1363 pcdPair = self.__GetNextPcdName()\r
1364 Obj.BaseAddressPcd = pcdPair\r
1365 self.Profile.PcdDict[pcdPair] = long(Obj.BaseAddress, 0)\r
1366 FileLineTuple = GetRealFileLine(self.FileName, self.CurrentLineNumber)\r
1367 self.Profile.PcdFileLineDict[pcdPair] = FileLineTuple\r
1368 \r
1369 if not self.__IsKeyword( "Size"):\r
1370 raise Warning("Size missing At Line ", self.FileName, self.CurrentLineNumber)\r
1371 \r
1372 if not self.__IsToken( "="):\r
1373 raise Warning("expected '=' At Line ", self.FileName, self.CurrentLineNumber)\r
1374 \r
1375 if not self.__GetNextHexNumber():\r
1376 raise Warning("expected Hex size At Line ", self.FileName, self.CurrentLineNumber)\r
1377 \r
1378 \r
1379 Obj.Size = long(self.__Token, 0)\r
1380\r
1381 if self.__IsToken( "|"):\r
1382 pcdPair = self.__GetNextPcdName()\r
1383 Obj.SizePcd = pcdPair\r
1384 self.Profile.PcdDict[pcdPair] = Obj.Size\r
1385 FileLineTuple = GetRealFileLine(self.FileName, self.CurrentLineNumber)\r
1386 self.Profile.PcdFileLineDict[pcdPair] = FileLineTuple\r
1387 \r
1388 if not self.__IsKeyword( "ErasePolarity"):\r
1389 raise Warning("ErasePolarity missing At Line ", self.FileName, self.CurrentLineNumber)\r
1390 \r
1391 if not self.__IsToken( "="):\r
1392 raise Warning("expected '=' At Line ", self.FileName, self.CurrentLineNumber)\r
1393 \r
1394 if not self.__GetNextToken():\r
1395 raise Warning("expected Erase Polarity At Line ", self.FileName, self.CurrentLineNumber)\r
1396 \r
1397 if self.__Token != "1" and self.__Token != "0":\r
1398 raise Warning("expected 1 or 0 Erase Polarity At Line ", self.FileName, self.CurrentLineNumber)\r
1399 \r
1400 Obj.ErasePolarity = self.__Token\r
1401\r
1402 Status = self.__GetBlockStatements(Obj)\r
1403 return Status\r
1404 \r
1405 ## __GetAddressStatements() method\r
1406 #\r
1407 # Get address statements\r
1408 #\r
1409 # @param self The object pointer\r
1410 # @param Obj for whom address statement is got\r
1411 # @retval True Successfully find\r
1412 # @retval False Not able to find\r
1413 #\r
1414 def __GetAddressStatements(self, Obj):\r
1415 \r
1416 if self.__IsKeyword("BsBaseAddress"):\r
1417 if not self.__IsToken( "="):\r
1418 raise Warning("expected '=' At Line ", self.FileName, self.CurrentLineNumber)\r
1419 \r
1420 if not self.__GetNextDecimalNumber() and not self.__GetNextHexNumber():\r
1421 raise Warning("expected address At Line ", self.FileName, self.CurrentLineNumber)\r
1422 \r
1423 BsAddress = long(self.__Token, 0)\r
1424 Obj.BsBaseAddress = BsAddress\r
1425 \r
1426 if self.__IsKeyword("RtBaseAddress"):\r
1427 if not self.__IsToken( "="):\r
1428 raise Warning("expected '=' At Line ", self.FileName, self.CurrentLineNumber)\r
1429 \r
1430 if not self.__GetNextDecimalNumber() and not self.__GetNextHexNumber():\r
1431 raise Warning("expected address At Line ", self.FileName, self.CurrentLineNumber)\r
1432 \r
1433 RtAddress = long(self.__Token, 0)\r
1434 Obj.RtBaseAddress = RtAddress\r
1435 \r
1436 ## __GetBlockStatements() method\r
1437 #\r
1438 # Get block statements\r
1439 #\r
1440 # @param self The object pointer\r
1441 # @param Obj for whom block statement is got\r
1442 # @retval True Successfully find\r
1443 # @retval False Not able to find\r
1444 #\r
1445 def __GetBlockStatements(self, Obj):\r
1446 \r
1447 if not self.__GetBlockStatement(Obj):\r
52302d4d
LG
1448 #set default block size is 1\r
1449 Obj.BlockSizeList.append((1, Obj.Size, None))\r
1450 return True\r
1451\r
30fdf114
LG
1452 while self.__GetBlockStatement(Obj):\r
1453 pass\r
52302d4d
LG
1454 \r
1455 for Item in Obj.BlockSizeList:\r
1456 if Item[0] == None or Item[1] == None:\r
1457 raise Warning("expected block statement for Fd Section", self.FileName, self.CurrentLineNumber)\r
1458\r
30fdf114
LG
1459 return True\r
1460 \r
1461 ## __GetBlockStatement() method\r
1462 #\r
1463 # Get block statement\r
1464 #\r
1465 # @param self The object pointer\r
1466 # @param Obj for whom block statement is got\r
1467 # @retval True Successfully find\r
1468 # @retval False Not able to find\r
1469 #\r
1470 def __GetBlockStatement(self, Obj):\r
1471 if not self.__IsKeyword( "BlockSize"):\r
1472 return False\r
1473 \r
1474 if not self.__IsToken( "="):\r
1475 raise Warning("expected '=' At Line ", self.FileName, self.CurrentLineNumber)\r
1476 \r
1477 if not self.__GetNextHexNumber() and not self.__GetNextDecimalNumber():\r
1478 raise Warning("expected Hex block size At Line ", self.FileName, self.CurrentLineNumber)\r
1479\r
1480 BlockSize = long(self.__Token, 0)\r
1481 BlockSizePcd = None\r
1482 if self.__IsToken( "|"):\r
1483 PcdPair = self.__GetNextPcdName()\r
1484 BlockSizePcd = PcdPair\r
1485 self.Profile.PcdDict[PcdPair] = BlockSize\r
1486 FileLineTuple = GetRealFileLine(self.FileName, self.CurrentLineNumber)\r
1487 self.Profile.PcdFileLineDict[pcdPair] = FileLineTuple\r
1488 \r
1489 BlockNumber = None\r
1490 if self.__IsKeyword( "NumBlocks"):\r
1491 if not self.__IsToken( "="):\r
1492 raise Warning("expected '=' At Line ", self.FileName, self.CurrentLineNumber)\r
1493 \r
1494 if not self.__GetNextDecimalNumber() and not self.__GetNextHexNumber():\r
1495 raise Warning("expected block numbers At Line ", self.FileName, self.CurrentLineNumber)\r
1496 \r
1497 BlockNumber = long(self.__Token, 0)\r
1498 \r
1499 Obj.BlockSizeList.append((BlockSize, BlockNumber, BlockSizePcd))\r
1500 return True\r
1501\r
1502 ## __GetDefineStatements() method\r
1503 #\r
1504 # Get define statements\r
1505 #\r
1506 # @param self The object pointer\r
1507 # @param Obj for whom define statement is got\r
1508 # @retval True Successfully find\r
1509 # @retval False Not able to find\r
1510 #\r
1511 def __GetDefineStatements(self, Obj):\r
1512 while self.__GetDefineStatement( Obj):\r
1513 pass\r
1514 \r
1515 ## __GetDefineStatement() method\r
1516 #\r
1517 # Get define statement\r
1518 #\r
1519 # @param self The object pointer\r
1520 # @param Obj for whom define statement is got\r
1521 # @retval True Successfully find\r
1522 # @retval False Not able to find\r
1523 #\r
1524 def __GetDefineStatement(self, Obj):\r
1525 if self.__IsKeyword("DEFINE"):\r
1526 self.__GetNextToken()\r
1527 Macro = self.__Token\r
1528 if not self.__IsToken( "="):\r
1529 raise Warning("expected '=' At Line ", self.FileName, self.CurrentLineNumber)\r
1530 \r
1531 if not self.__GetNextToken():\r
1532 raise Warning("expected value At Line ", self.FileName, self.CurrentLineNumber)\r
1533\r
1534 Value = self.__Token\r
1535 Macro = '$(' + Macro + ')'\r
1536 Obj.DefineVarDict[Macro] = Value\r
1537 return True\r
1538 \r
1539 return False\r
1540 \r
1541 ## __GetSetStatements() method\r
1542 #\r
1543 # Get set statements\r
1544 #\r
1545 # @param self The object pointer\r
1546 # @param Obj for whom set statement is got\r
1547 # @retval True Successfully find\r
1548 # @retval False Not able to find\r
1549 #\r
1550 def __GetSetStatements(self, Obj):\r
1551 while self.__GetSetStatement(Obj):\r
1552 pass\r
1553\r
1554 ## __GetSetStatement() method\r
1555 #\r
1556 # Get set statement\r
1557 #\r
1558 # @param self The object pointer\r
1559 # @param Obj for whom set statement is got\r
1560 # @retval True Successfully find\r
1561 # @retval False Not able to find\r
1562 #\r
1563 def __GetSetStatement(self, Obj):\r
1564 if self.__IsKeyword("SET"):\r
1565 PcdPair = self.__GetNextPcdName()\r
1566 \r
1567 if not self.__IsToken( "="):\r
1568 raise Warning("expected '=' At Line ", self.FileName, self.CurrentLineNumber)\r
1569 \r
1570 if not self.__GetNextToken():\r
1571 raise Warning("expected value At Line ", self.FileName, self.CurrentLineNumber)\r
1572 \r
1573 Value = self.__Token\r
1574 if Value.startswith("{"):\r
1575 # deal with value with {}\r
1576 if not self.__SkipToToken( "}"):\r
1577 raise Warning("expected '}' At Line ", self.FileName, self.CurrentLineNumber)\r
1578 Value += self.__SkippedChars\r
1579 \r
1580 Obj.SetVarDict[PcdPair] = Value\r
1581 self.Profile.PcdDict[PcdPair] = Value\r
1582 FileLineTuple = GetRealFileLine(self.FileName, self.CurrentLineNumber)\r
1583 self.Profile.PcdFileLineDict[PcdPair] = FileLineTuple\r
1584 return True\r
1585\r
1586 return False\r
1587\r
1588 ## __GetRegionLayout() method\r
1589 #\r
1590 # Get region layout for FD\r
1591 #\r
1592 # @param self The object pointer\r
1593 # @param Fd for whom region is got\r
1594 # @retval True Successfully find\r
1595 # @retval False Not able to find\r
1596 #\r
1597 def __GetRegionLayout(self, Fd):\r
1598 if not self.__GetNextHexNumber():\r
1599 return False\r
1600 \r
1601 RegionObj = CommonDataClass.FdfClass.RegionClassObject()\r
1602 RegionObj.Offset = long(self.__Token, 0)\r
1603 Fd.RegionList.append(RegionObj)\r
1604 \r
1605 if not self.__IsToken( "|"):\r
1606 raise Warning("expected '|' At Line ", self.FileName, self.CurrentLineNumber)\r
1607 \r
1608 if not self.__GetNextHexNumber():\r
1609 raise Warning("expected Region Size At Line ", self.FileName, self.CurrentLineNumber)\r
1610 RegionObj.Size = long(self.__Token, 0)\r
1611 \r
1612 if not self.__GetNextWord():\r
1613 return True\r
1614 \r
fd171542 1615 if not self.__Token in ("SET", "FV", "FILE", "DATA", "CAPSULE"):\r
30fdf114
LG
1616 self.__UndoToken()\r
1617 RegionObj.PcdOffset = self.__GetNextPcdName()\r
1618 self.Profile.PcdDict[RegionObj.PcdOffset] = RegionObj.Offset + long(Fd.BaseAddress, 0)\r
1619 FileLineTuple = GetRealFileLine(self.FileName, self.CurrentLineNumber)\r
1620 self.Profile.PcdFileLineDict[RegionObj.PcdOffset] = FileLineTuple\r
1621 if self.__IsToken( "|"):\r
1622 RegionObj.PcdSize = self.__GetNextPcdName()\r
1623 self.Profile.PcdDict[RegionObj.PcdSize] = RegionObj.Size\r
1624 FileLineTuple = GetRealFileLine(self.FileName, self.CurrentLineNumber)\r
1625 self.Profile.PcdFileLineDict[RegionObj.PcdSize] = FileLineTuple\r
1626 \r
1627 if not self.__GetNextWord():\r
1628 return True\r
1629\r
1630 if self.__Token == "SET":\r
1631 self.__UndoToken()\r
1632 self.__GetSetStatements( RegionObj)\r
1633 if not self.__GetNextWord():\r
1634 return True\r
1635 \r
fd171542 1636 elif self.__Token == "FV":\r
30fdf114
LG
1637 self.__UndoToken()\r
1638 self.__GetRegionFvType( RegionObj)\r
1639\r
fd171542 1640 elif self.__Token == "CAPSULE":\r
1641 self.__UndoToken()\r
1642 self.__GetRegionCapType( RegionObj)\r
1643\r
30fdf114
LG
1644 elif self.__Token == "FILE":\r
1645 self.__UndoToken()\r
1646 self.__GetRegionFileType( RegionObj)\r
1647\r
1648 else:\r
1649 self.__UndoToken()\r
1650 self.__GetRegionDataType( RegionObj)\r
1651 \r
1652 return True\r
1653 \r
1654 ## __GetRegionFvType() method\r
1655 #\r
1656 # Get region fv data for region\r
1657 #\r
1658 # @param self The object pointer\r
1659 # @param RegionObj for whom region data is got\r
1660 #\r
1661 def __GetRegionFvType(self, RegionObj):\r
1662\r
1663 if not self.__IsKeyword( "FV"):\r
1664 raise Warning("expected Keyword 'FV' At Line ", self.FileName, self.CurrentLineNumber)\r
1665 \r
1666 if not self.__IsToken( "="):\r
1667 raise Warning("expected '=' At Line ", self.FileName, self.CurrentLineNumber)\r
1668 \r
1669 if not self.__GetNextToken():\r
1670 raise Warning("expected FV name At Line ", self.FileName, self.CurrentLineNumber)\r
1671 \r
1672 RegionObj.RegionType = "FV"\r
1673 RegionObj.RegionDataList.append(self.__Token)\r
1674 \r
1675 while self.__IsKeyword( "FV"):\r
1676 \r
1677 if not self.__IsToken( "="):\r
1678 raise Warning("expected '=' At Line ", self.FileName, self.CurrentLineNumber)\r
1679 \r
1680 if not self.__GetNextToken():\r
1681 raise Warning("expected FV name At Line ", self.FileName, self.CurrentLineNumber)\r
1682 \r
1683 RegionObj.RegionDataList.append(self.__Token)\r
fd171542 1684\r
1685 ## __GetRegionCapType() method\r
1686 #\r
1687 # Get region capsule data for region\r
1688 #\r
1689 # @param self The object pointer\r
1690 # @param RegionObj for whom region data is got\r
1691 #\r
1692 def __GetRegionCapType(self, RegionObj):\r
1693\r
1694 if not self.__IsKeyword("CAPSULE"):\r
1695 raise Warning("expected Keyword 'CAPSULE' at line", self.FileName, self.CurrentLineNumber)\r
1696\r
1697 if not self.__IsToken("="):\r
1698 raise Warning("expected '=' at line", self.FileName, self.CurrentLineNumber)\r
1699\r
1700 if not self.__GetNextToken():\r
1701 raise Warning("expected CAPSULE name at line", self.FileName, self.CurrentLineNumber)\r
1702\r
1703 RegionObj.RegionType = "CAPSULE"\r
1704 RegionObj.RegionDataList.append(self.__Token)\r
1705\r
1706 while self.__IsKeyword("CAPSULE"):\r
1707\r
1708 if not self.__IsToken("="):\r
1709 raise Warning("expected '=' at line", self.FileName, self.CurrentLineNumber)\r
1710\r
1711 if not self.__GetNextToken():\r
1712 raise Warning("expected CAPSULE name at line", self.FileName, self.CurrentLineNumber)\r
1713\r
1714 RegionObj.RegionDataList.append(self.__Token)\r
1715\r
30fdf114
LG
1716 ## __GetRegionFileType() method\r
1717 #\r
1718 # Get region file data for region\r
1719 #\r
1720 # @param self The object pointer\r
1721 # @param RegionObj for whom region data is got\r
1722 #\r
1723 def __GetRegionFileType(self, RegionObj):\r
1724\r
1725 if not self.__IsKeyword( "FILE"):\r
1726 raise Warning("expected Keyword 'FILE' At Line ", self.FileName, self.CurrentLineNumber)\r
1727\r
1728 if not self.__IsToken( "="):\r
1729 raise Warning("expected '=' At Line ", self.FileName, self.CurrentLineNumber)\r
1730\r
1731 if not self.__GetNextToken():\r
1732 raise Warning("expected File name At Line ", self.FileName, self.CurrentLineNumber)\r
1733\r
1734 RegionObj.RegionType = "FILE"\r
1735 RegionObj.RegionDataList.append( self.__Token)\r
1736 \r
1737 while self.__IsKeyword( "FILE"):\r
1738 \r
1739 if not self.__IsToken( "="):\r
1740 raise Warning("expected '=' At Line ", self.FileName, self.CurrentLineNumber)\r
1741 \r
1742 if not self.__GetNextToken():\r
1743 raise Warning("expected FILE name At Line ", self.FileName, self.CurrentLineNumber)\r
1744 \r
1745 RegionObj.RegionDataList.append(self.__Token)\r
1746\r
1747 ## __GetRegionDataType() method\r
1748 #\r
1749 # Get region array data for region\r
1750 #\r
1751 # @param self The object pointer\r
1752 # @param RegionObj for whom region data is got\r
1753 #\r
1754 def __GetRegionDataType(self, RegionObj):\r
1755 \r
1756 if not self.__IsKeyword( "DATA"):\r
1757 raise Warning("expected Region Data type At Line ", self.FileName, self.CurrentLineNumber)\r
1758\r
1759 if not self.__IsToken( "="):\r
1760 raise Warning("expected '=' At Line ", self.FileName, self.CurrentLineNumber)\r
1761 \r
1762 if not self.__IsToken( "{"):\r
1763 raise Warning("expected '{' At Line ", self.FileName, self.CurrentLineNumber)\r
1764 \r
1765 if not self.__GetNextHexNumber():\r
1766 raise Warning("expected Hex byte At Line ", self.FileName, self.CurrentLineNumber)\r
1767 \r
636f2be6
LG
1768 if len(self.__Token) > 18:\r
1769 raise Warning("Hex string can't be converted to a valid UINT64 value", self.FileName, self.CurrentLineNumber)\r
30fdf114
LG
1770 \r
1771 DataString = self.__Token\r
1772 DataString += ","\r
1773 \r
1774 while self.__IsToken(","):\r
1775 if not self.__GetNextHexNumber():\r
1776 raise Warning("Invalid Hex number At Line ", self.FileName, self.CurrentLineNumber)\r
1777 if len(self.__Token) > 4:\r
1778 raise Warning("Hex byte(must be 2 digits) too long At Line ", self.FileName, self.CurrentLineNumber)\r
1779 DataString += self.__Token\r
1780 DataString += ","\r
1781 \r
1782 if not self.__IsToken( "}"):\r
1783 raise Warning("expected '}' At Line ", self.FileName, self.CurrentLineNumber)\r
1784 \r
1785 DataString = DataString.rstrip(",")\r
1786 RegionObj.RegionType = "DATA"\r
1787 RegionObj.RegionDataList.append( DataString)\r
1788 \r
1789 while self.__IsKeyword( "DATA"):\r
1790\r
1791 if not self.__IsToken( "="):\r
1792 raise Warning("expected '=' At Line ", self.FileName, self.CurrentLineNumber)\r
1793 \r
1794 if not self.__IsToken( "{"):\r
1795 raise Warning("expected '{' At Line ", self.FileName, self.CurrentLineNumber)\r
1796 \r
1797 if not self.__GetNextHexNumber():\r
1798 raise Warning("expected Hex byte At Line ", self.FileName, self.CurrentLineNumber)\r
1799 \r
636f2be6
LG
1800 if len(self.__Token) > 18:\r
1801 raise Warning("Hex string can't be converted to a valid UINT64 value", self.FileName, self.CurrentLineNumber)\r
30fdf114
LG
1802 \r
1803 DataString = self.__Token\r
1804 DataString += ","\r
1805 \r
1806 while self.__IsToken(","):\r
1807 self.__GetNextHexNumber()\r
1808 if len(self.__Token) > 4:\r
1809 raise Warning("Hex byte(must be 2 digits) too long At Line ", self.FileName, self.CurrentLineNumber)\r
1810 DataString += self.__Token\r
1811 DataString += ","\r
1812 \r
1813 if not self.__IsToken( "}"):\r
1814 raise Warning("expected '}' At Line ", self.FileName, self.CurrentLineNumber)\r
1815 \r
1816 DataString = DataString.rstrip(",")\r
1817 RegionObj.RegionDataList.append( DataString)\r
1818 \r
1819 ## __GetFv() method\r
1820 #\r
1821 # Get FV section contents and store its data into FV dictionary of self.Profile\r
1822 #\r
1823 # @param self The object pointer\r
1824 # @retval True Successfully find a FV\r
1825 # @retval False Not able to find a FV\r
1826 # \r
1827 def __GetFv(self):\r
1828 if not self.__GetNextToken():\r
1829 return False\r
1830\r
1831 S = self.__Token.upper()\r
1832 if S.startswith("[") and not S.startswith("[FV."):\r
1833 if not S.startswith("[CAPSULE.") \\r
1834 and not S.startswith("[VTF.") and not S.startswith("[RULE."):\r
1835 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
1836 self.__UndoToken()\r
1837 return False\r
1838\r
1839 self.__UndoToken()\r
1840 if not self.__IsToken("[FV.", True):\r
1841 FileLineTuple = GetRealFileLine(self.FileName, self.CurrentLineNumber)\r
1842 print 'Parsing String: %s in File %s, At line: %d, Offset Within Line: %d' \\r
1843 % (self.Profile.FileLinesList[self.CurrentLineNumber - 1][self.CurrentOffsetWithinLine :], FileLineTuple[0], FileLineTuple[1], self.CurrentOffsetWithinLine)\r
1844 raise Warning("Unknown Keyword At Line ", self.FileName, self.CurrentLineNumber)\r
1845 \r
1846 FvName = self.__GetUiName()\r
1847 self.CurrentFvName = FvName.upper()\r
1848 \r
1849 if not self.__IsToken( "]"):\r
1850 raise Warning("expected ']' At Line ", self.FileName, self.CurrentLineNumber)\r
1851 \r
1852 FvObj = CommonDataClass.FdfClass.FvClassObject()\r
1853 FvObj.UiFvName = self.CurrentFvName\r
1854 self.Profile.FvDict[self.CurrentFvName] = FvObj\r
1855 \r
1856 Status = self.__GetCreateFile(FvObj)\r
1857 if not Status:\r
1858 raise Warning("FV name error At Line ", self.FileName, self.CurrentLineNumber)\r
1859\r
1860 self.__GetDefineStatements(FvObj)\r
1861\r
1862 self.__GetAddressStatements(FvObj)\r
1863 \r
1864 self.__GetBlockStatement(FvObj)\r
1865\r
1866 self.__GetSetStatements(FvObj)\r
1867\r
1868 self.__GetFvAlignment(FvObj)\r
1869\r
1870 self.__GetFvAttributes(FvObj)\r
1871 \r
1872 self.__GetFvNameGuid(FvObj)\r
1873\r
1874 self.__GetAprioriSection(FvObj, FvObj.DefineVarDict.copy())\r
1875 self.__GetAprioriSection(FvObj, FvObj.DefineVarDict.copy())\r
1876 \r
1877 while True:\r
1878 isInf = self.__GetInfStatement(FvObj, MacroDict = FvObj.DefineVarDict.copy())\r
1879 isFile = self.__GetFileStatement(FvObj, MacroDict = FvObj.DefineVarDict.copy())\r
1880 if not isInf and not isFile:\r
1881 break\r
1882 \r
1883 return True\r
1884\r
1885 ## __GetFvAlignment() method\r
1886 #\r
1887 # Get alignment for FV\r
1888 #\r
1889 # @param self The object pointer\r
1890 # @param Obj for whom alignment is got\r
1891 # @retval True Successfully find a alignment statement\r
1892 # @retval False Not able to find a alignment statement\r
1893 #\r
1894 def __GetFvAlignment(self, Obj):\r
1895 \r
1896 if not self.__IsKeyword( "FvAlignment"):\r
1897 return False\r
1898 \r
1899 if not self.__IsToken( "="):\r
1900 raise Warning("expected '=' At Line ", self.FileName, self.CurrentLineNumber)\r
1901 \r
1902 if not self.__GetNextToken():\r
1903 raise Warning("expected alignment value At Line ", self.FileName, self.CurrentLineNumber)\r
1904 \r
1905 if self.__Token.upper() not in ("1", "2", "4", "8", "16", "32", "64", "128", "256", "512", \\r
1906 "1K", "2K", "4K", "8K", "16K", "32K", "64K", "128K", "256K", "512K", \\r
1907 "1M", "2M", "4M", "8M", "16M", "32M", "64M", "128M", "256M", "512M", \\r
1908 "1G", "2G"):\r
1909 raise Warning("Unknown alignment value At Line ", self.FileName, self.CurrentLineNumber)\r
1910 Obj.FvAlignment = self.__Token\r
1911 return True\r
1912 \r
1913 ## __GetFvAttributes() method\r
1914 #\r
1915 # Get attributes for FV\r
1916 #\r
1917 # @param self The object pointer\r
1918 # @param Obj for whom attribute is got\r
1919 # @retval None\r
1920 #\r
1921 def __GetFvAttributes(self, FvObj):\r
1922 \r
1923 while self.__GetNextWord():\r
1924 name = self.__Token\r
1925 if name not in ("ERASE_POLARITY", "MEMORY_MAPPED", \\r
1926 "STICKY_WRITE", "LOCK_CAP", "LOCK_STATUS", "WRITE_ENABLED_CAP", \\r
1927 "WRITE_DISABLED_CAP", "WRITE_STATUS", "READ_ENABLED_CAP", \\r
1928 "READ_DISABLED_CAP", "READ_STATUS", "READ_LOCK_CAP", \\r
1929 "READ_LOCK_STATUS", "WRITE_LOCK_CAP", "WRITE_LOCK_STATUS", \\r
1930 "WRITE_POLICY_RELIABLE"):\r
1931 self.__UndoToken()\r
1932 return\r
1933\r
1934 if not self.__IsToken( "="):\r
1935 raise Warning("expected '=' At Line ", self.FileName, self.CurrentLineNumber)\r
1936 \r
1937 if not self.__GetNextToken() or self.__Token.upper() not in ("TRUE", "FALSE", "1", "0"):\r
1938 raise Warning("expected TRUE/FALSE (1/0) At Line ", self.FileName, self.CurrentLineNumber)\r
1939 \r
1940 FvObj.FvAttributeDict[name] = self.__Token\r
1941\r
1942 return\r
1943 \r
1944 ## __GetFvNameGuid() method\r
1945 #\r
1946 # Get FV GUID for FV\r
1947 #\r
1948 # @param self The object pointer\r
1949 # @param Obj for whom GUID is got\r
1950 # @retval None\r
1951 #\r
1952 def __GetFvNameGuid(self, FvObj):\r
1953\r
1954 if not self.__IsKeyword( "FvNameGuid"):\r
1955 return\r
1956\r
1957 if not self.__IsToken( "="):\r
1958 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
1959\r
1960 if not self.__GetNextGuid():\r
1961 raise Warning("expected FV GUID value", self.FileName, self.CurrentLineNumber)\r
1962\r
1963 FvObj.FvNameGuid = self.__Token\r
1964\r
1965 return\r
1966\r
1967 ## __GetAprioriSection() method\r
1968 #\r
1969 # Get token statements\r
1970 #\r
1971 # @param self The object pointer\r
1972 # @param FvObj for whom apriori is got\r
1973 # @param MacroDict dictionary used to replace macro\r
1974 # @retval True Successfully find apriori statement\r
1975 # @retval False Not able to find apriori statement\r
1976 #\r
1977 def __GetAprioriSection(self, FvObj, MacroDict = {}):\r
1978 \r
1979 if not self.__IsKeyword( "APRIORI"):\r
1980 return False\r
1981 \r
1982 if not self.__IsKeyword("PEI") and not self.__IsKeyword("DXE"):\r
1983 raise Warning("expected Apriori file type At Line ", self.FileName, self.CurrentLineNumber)\r
1984 AprType = self.__Token\r
1985 \r
1986 if not self.__IsToken( "{"):\r
1987 raise Warning("expected '{' At Line ", self.FileName, self.CurrentLineNumber)\r
1988 \r
1989 AprSectionObj = CommonDataClass.FdfClass.AprioriSectionClassObject()\r
1990 AprSectionObj.AprioriType = AprType\r
1991 \r
1992 self.__GetDefineStatements(AprSectionObj)\r
1993 MacroDict.update(AprSectionObj.DefineVarDict)\r
1994 \r
1995 while True:\r
1996 IsInf = self.__GetInfStatement( AprSectionObj, MacroDict = MacroDict)\r
1997 IsFile = self.__GetFileStatement( AprSectionObj)\r
1998 if not IsInf and not IsFile:\r
1999 break\r
2000 \r
2001 if not self.__IsToken( "}"):\r
2002 raise Warning("expected '}' At Line ", self.FileName, self.CurrentLineNumber)\r
2003\r
2004 FvObj.AprioriSectionList.append(AprSectionObj)\r
2005 return True\r
2006\r
2007 ## __GetInfStatement() method\r
2008 #\r
2009 # Get INF statements\r
2010 #\r
2011 # @param self The object pointer\r
2012 # @param Obj for whom inf statement is got\r
2013 # @param MacroDict dictionary used to replace macro\r
2014 # @retval True Successfully find inf statement\r
2015 # @retval False Not able to find inf statement\r
2016 #\r
2017 def __GetInfStatement(self, Obj, ForCapsule = False, MacroDict = {}):\r
2018\r
2019 if not self.__IsKeyword( "INF"):\r
2020 return False\r
2021 \r
2022 ffsInf = CommonDataClass.FdfClass.FfsInfStatementClassObject()\r
2023 self.__GetInfOptions( ffsInf)\r
2024 \r
2025 if not self.__GetNextToken():\r
2026 raise Warning("expected INF file path At Line ", self.FileName, self.CurrentLineNumber)\r
2027 ffsInf.InfFileName = self.__Token\r
2028 \r
2029# if ffsInf.InfFileName.find('$') >= 0:\r
2030# ffsInf.InfFileName = GenFdsGlobalVariable.GenFdsGlobalVariable.MacroExtend(ffsInf.InfFileName, MacroDict)\r
2031 \r
2032 if not ffsInf.InfFileName in self.Profile.InfList:\r
2033 self.Profile.InfList.append(ffsInf.InfFileName)\r
2034 FileLineTuple = GetRealFileLine(self.FileName, self.CurrentLineNumber)\r
2035 self.Profile.InfFileLineList.append(FileLineTuple)\r
2036 \r
2037 if self.__IsToken('|'):\r
2038 if self.__IsKeyword('RELOCS_STRIPPED'):\r
2039 ffsInf.KeepReloc = False\r
2040 elif self.__IsKeyword('RELOCS_RETAINED'):\r
2041 ffsInf.KeepReloc = True\r
2042 else:\r
2043 raise Warning("Unknown reloc strip flag At Line ", self.FileName, self.CurrentLineNumber)\r
2044 \r
2045 if ForCapsule:\r
2046 capsuleFfs = CapsuleData.CapsuleFfs()\r
2047 capsuleFfs.Ffs = ffsInf\r
2048 Obj.CapsuleDataList.append(capsuleFfs)\r
2049 else:\r
2050 Obj.FfsList.append(ffsInf)\r
2051 return True\r
2052 \r
2053 ## __GetInfOptions() method\r
2054 #\r
2055 # Get options for INF\r
2056 #\r
2057 # @param self The object pointer\r
2058 # @param FfsInfObj for whom option is got\r
2059 #\r
2060 def __GetInfOptions(self, FfsInfObj):\r
2061 \r
2062 if self.__IsKeyword( "RuleOverride"):\r
2063 if not self.__IsToken( "="):\r
2064 raise Warning("expected '=' At Line ", self.FileName, self.CurrentLineNumber)\r
2065 if not self.__GetNextToken():\r
2066 raise Warning("expected Rule name At Line ", self.FileName, self.CurrentLineNumber)\r
2067 FfsInfObj.Rule = self.__Token\r
2068 \r
2069 if self.__IsKeyword( "VERSION"):\r
2070 if not self.__IsToken( "="):\r
2071 raise Warning("expected '=' At Line ", self.FileName, self.CurrentLineNumber)\r
2072 if not self.__GetNextToken():\r
2073 raise Warning("expected Version At Line ", self.FileName, self.CurrentLineNumber)\r
2074 \r
2075 if self.__GetStringData():\r
2076 FfsInfObj.Version = self.__Token\r
2077 \r
2078 if self.__IsKeyword( "UI"):\r
2079 if not self.__IsToken( "="):\r
2080 raise Warning("expected '=' At Line ", self.FileName, self.CurrentLineNumber)\r
2081 if not self.__GetNextToken():\r
2082 raise Warning("expected UI name At Line ", self.FileName, self.CurrentLineNumber)\r
2083 \r
2084 if self.__GetStringData():\r
2085 FfsInfObj.Ui = self.__Token\r
2086\r
2087 if self.__IsKeyword( "USE"):\r
2088 if not self.__IsToken( "="):\r
2089 raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
2090 if not self.__GetNextToken():\r
2091 raise Warning("expected ARCH name", self.FileName, self.CurrentLineNumber)\r
2092 FfsInfObj.UseArch = self.__Token\r
2093\r
2094 \r
2095 if self.__GetNextToken():\r
2096 p = re.compile(r'([a-zA-Z0-9\-]+|\$\(TARGET\)|\*)_([a-zA-Z0-9\-]+|\$\(TOOL_CHAIN_TAG\)|\*)_([a-zA-Z0-9\-]+|\$\(ARCH\)|\*)')\r
2097 if p.match(self.__Token):\r
2098 FfsInfObj.KeyStringList.append(self.__Token)\r
2099 if not self.__IsToken(","):\r
2100 return\r
2101 else:\r
2102 self.__UndoToken()\r
2103 return\r
2104 \r
2105 while self.__GetNextToken():\r
2106 if not p.match(self.__Token):\r
2107 raise Warning("expected KeyString \"Target_Tag_Arch\" At Line ", self.FileName, self.CurrentLineNumber)\r
2108 FfsInfObj.KeyStringList.append(self.__Token)\r
2109 \r
2110 if not self.__IsToken(","):\r
2111 break\r
2112 \r
2113 ## __GetFileStatement() method\r
2114 #\r
2115 # Get FILE statements\r
2116 #\r
2117 # @param self The object pointer\r
2118 # @param Obj for whom FILE statement is got\r
2119 # @param MacroDict dictionary used to replace macro\r
2120 # @retval True Successfully find FILE statement\r
2121 # @retval False Not able to find FILE statement\r
2122 #\r
2123 def __GetFileStatement(self, Obj, ForCapsule = False, MacroDict = {}):\r
2124\r
2125 if not self.__IsKeyword( "FILE"):\r
2126 return False\r
2127 \r
2128 FfsFileObj = CommonDataClass.FdfClass.FileStatementClassObject()\r
2129 \r
2130 if not self.__GetNextWord():\r
2131 raise Warning("expected FFS type At Line ", self.FileName, self.CurrentLineNumber)\r
2132 FfsFileObj.FvFileType = self.__Token\r
2133 \r
2134 if not self.__IsToken( "="):\r
2135 raise Warning("expected '=' At Line ", self.FileName, self.CurrentLineNumber)\r
2136\r
2137 if not self.__GetNextGuid():\r
2138 if not self.__GetNextWord():\r
2139 raise Warning("expected File GUID", self.FileName, self.CurrentLineNumber)\r
2140 if self.__Token == 'PCD':\r
2141 if not self.__IsToken( "("):\r
2142 raise Warning("expected '('", self.FileName, self.CurrentLineNumber)\r
2143 PcdPair = self.__GetNextPcdName()\r
2144 if not self.__IsToken( ")"):\r
2145 raise Warning("expected ')'", self.FileName, self.CurrentLineNumber)\r
2146 self.__Token = 'PCD('+PcdPair[1]+'.'+PcdPair[0]+')'\r
2147 \r
2148 FfsFileObj.NameGuid = self.__Token\r
2149 \r
2150 self.__GetFilePart( FfsFileObj, MacroDict.copy())\r
2151 \r
2152 if ForCapsule:\r
2153 capsuleFfs = CapsuleData.CapsuleFfs()\r
2154 capsuleFfs.Ffs = FfsFileObj\r
2155 Obj.CapsuleDataList.append(capsuleFfs)\r
2156 else:\r
2157 Obj.FfsList.append(FfsFileObj)\r
2158 \r
2159 return True\r
2160 \r
2161 ## __FileCouldHaveRelocFlag() method\r
2162 #\r
2163 # Check whether reloc strip flag can be set for a file type.\r
2164 #\r
2165 # @param self The object pointer\r
2166 # @param FileType The file type to check with\r
2167 # @retval True This type could have relocation strip flag\r
2168 # @retval False No way to have it\r
2169 #\r
2170 \r
2171 def __FileCouldHaveRelocFlag (self, FileType):\r
2172 if FileType in ('SEC', 'PEI_CORE', 'PEIM', 'PEI_DXE_COMBO'):\r
2173 return True\r
2174 else:\r
2175 return False\r
2176 \r
2177 ## __SectionCouldHaveRelocFlag() method\r
2178 #\r
2179 # Check whether reloc strip flag can be set for a section type.\r
2180 #\r
2181 # @param self The object pointer\r
2182 # @param SectionType The section type to check with\r
2183 # @retval True This type could have relocation strip flag\r
2184 # @retval False No way to have it\r
2185 #\r
2186 \r
2187 def __SectionCouldHaveRelocFlag (self, SectionType):\r
2188 if SectionType in ('TE', 'PE32'):\r
2189 return True\r
2190 else:\r
2191 return False\r
2192 \r
2193 ## __GetFilePart() method\r
2194 #\r
2195 # Get components for FILE statement\r
2196 #\r
2197 # @param self The object pointer\r
2198 # @param FfsFileObj for whom component is got\r
2199 # @param MacroDict dictionary used to replace macro\r
2200 #\r
2201 def __GetFilePart(self, FfsFileObj, MacroDict = {}):\r
2202 \r
2203 self.__GetFileOpts( FfsFileObj)\r
2204 \r
2205 if not self.__IsToken("{"):\r
2206# if self.__IsKeyword('RELOCS_STRIPPED') or self.__IsKeyword('RELOCS_RETAINED'):\r
2207# if self.__FileCouldHaveRelocFlag(FfsFileObj.FvFileType):\r
2208# if self.__Token == 'RELOCS_STRIPPED':\r
2209# FfsFileObj.KeepReloc = False\r
2210# else:\r
2211# FfsFileObj.KeepReloc = True\r
2212# else:\r
2213# raise Warning("File type %s could not have reloc strip flag At Line %d" % (FfsFileObj.FvFileType, self.CurrentLineNumber), self.FileName, self.CurrentLineNumber)\r
2214# \r
2215# if not self.__IsToken("{"):\r
2216 raise Warning("expected '{' At Line ", self.FileName, self.CurrentLineNumber)\r
2217 \r
2218 if not self.__GetNextToken():\r
2219 raise Warning("expected File name or section data At Line ", self.FileName, self.CurrentLineNumber)\r
2220 \r
2221 if self.__Token == "FV":\r
2222 if not self.__IsToken( "="):\r
2223 raise Warning("expected '=' At Line ", self.FileName, self.CurrentLineNumber)\r
2224 if not self.__GetNextToken():\r
2225 raise Warning("expected FV name At Line ", self.FileName, self.CurrentLineNumber)\r
2226 FfsFileObj.FvName = self.__Token\r
2227 \r
2228 elif self.__Token == "FD":\r
2229 if not self.__IsToken( "="):\r
2230 raise Warning("expected '=' At Line ", self.FileName, self.CurrentLineNumber)\r
2231 if not self.__GetNextToken():\r
2232 raise Warning("expected FD name At Line ", self.FileName, self.CurrentLineNumber)\r
2233 FfsFileObj.FdName = self.__Token\r
2234 \r
2235 elif self.__Token in ("DEFINE", "APRIORI", "SECTION"):\r
2236 self.__UndoToken()\r
2237 self.__GetSectionData( FfsFileObj, MacroDict)\r
2238 else:\r
2239 FfsFileObj.FileName = self.__Token\r
2240 \r
2241 if not self.__IsToken( "}"):\r
2242 raise Warning("expected '}' At Line ", self.FileName, self.CurrentLineNumber)\r
2243 \r
2244 ## __GetFileOpts() method\r
2245 #\r
2246 # Get options for FILE statement\r
2247 #\r
2248 # @param self The object pointer\r
2249 # @param FfsFileObj for whom options is got\r
2250 #\r
2251 def __GetFileOpts(self, FfsFileObj):\r
2252 \r
2253 if self.__GetNextToken():\r
2254 Pattern = re.compile(r'([a-zA-Z0-9\-]+|\$\(TARGET\)|\*)_([a-zA-Z0-9\-]+|\$\(TOOL_CHAIN_TAG\)|\*)_([a-zA-Z0-9\-]+|\$\(ARCH\)|\*)')\r
2255 if Pattern.match(self.__Token):\r
2256 FfsFileObj.KeyStringList.append(self.__Token)\r
2257 if self.__IsToken(","):\r
2258 while self.__GetNextToken():\r
2259 if not Pattern.match(self.__Token):\r
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
2425 if FvObj != None:\r
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
2904 Pattern = re.compile(r'([a-zA-Z0-9\-]+|\$\(TARGET\)|\*)_([a-zA-Z0-9\-]+|\$\(TOOL_CHAIN_TAG\)|\*)_([a-zA-Z0-9\-]+|\$\(ARCH\)|\*)')\r
2905 if Pattern.match(self.__Token):\r
2906 KeyStringList.append(self.__Token)\r
2907 if self.__IsToken(","):\r
2908 while self.__GetNextToken():\r
2909 if not Pattern.match(self.__Token):\r
2910 raise Warning("expected KeyString \"Target_Tag_Arch\" At Line ", self.FileName, self.CurrentLineNumber)\r
2911 KeyStringList.append(self.__Token)\r
2912\r
2913 if not self.__IsToken(","):\r
2914 break\r
2915 \r
2916 else:\r
2917 self.__UndoToken()\r
2918\r
2919 \r
2920 Fixed = False\r
2921 if self.__IsKeyword("Fixed", True):\r
2922 Fixed = True\r
2923 \r
2924 CheckSum = False\r
2925 if self.__IsKeyword("CheckSum", True):\r
2926 CheckSum = True\r
2927 \r
2928 AlignValue = ""\r
2929 if self.__GetAlignment():\r
e921f58d
YZ
2930 if self.__Token not in ("Auto", "8", "16", "32", "64", "128", "512", "1K", "4K", "32K" ,"64K", "128K",\r
2931 "256K", "512K", "1M", "2M", "4M", "8M", "16M"):\r
30fdf114
LG
2932 raise Warning("Incorrect alignment At Line ", self.FileName, self.CurrentLineNumber)\r
2933 AlignValue = self.__Token\r
2934\r
2935 if self.__IsToken("{"):\r
2936 # Complex file rule expected\r
2937 Rule = RuleComplexFile.RuleComplexFile()\r
2938 Rule.FvFileType = Type\r
2939 Rule.NameGuid = NameGuid\r
2940 Rule.Alignment = AlignValue\r
2941 Rule.CheckSum = CheckSum\r
2942 Rule.Fixed = Fixed\r
2943 Rule.KeyStringList = KeyStringList\r
2944 if KeepReloc != None:\r
2945 Rule.KeepReloc = KeepReloc\r
2946 \r
2947 while True:\r
2948 IsEncapsulate = self.__GetRuleEncapsulationSection(Rule)\r
2949 IsLeaf = self.__GetEfiSection(Rule)\r
2950 if not IsEncapsulate and not IsLeaf:\r
2951 break\r
2952 \r
2953 if not self.__IsToken("}"):\r
2954 raise Warning("expected '}' At Line ", self.FileName, self.CurrentLineNumber)\r
2955 \r
2956 return Rule\r
2957 \r
2958 elif self.__IsToken("|"):\r
2959 # Ext rule expected\r
2960 Ext = self.__GetFileExtension()\r
2961 \r
2962 Rule = RuleSimpleFile.RuleSimpleFile()\r
2963\r
2964 Rule.FvFileType = Type\r
2965 Rule.NameGuid = NameGuid\r
2966 Rule.Alignment = AlignValue\r
2967 Rule.CheckSum = CheckSum\r
2968 Rule.Fixed = Fixed\r
2969 Rule.FileExtension = Ext\r
2970 Rule.KeyStringList = KeyStringList\r
2971 if KeepReloc != None:\r
2972 Rule.KeepReloc = KeepReloc\r
2973 \r
2974 return Rule\r
2975 \r
2976 else:\r
2977 # Simple file rule expected\r
2978 if not self.__GetNextWord():\r
2979 raise Warning("expected leaf section type At Line ", self.FileName, self.CurrentLineNumber)\r
2980\r
2981 SectionName = self.__Token\r
2982 \r
2983 if SectionName not in ("COMPAT16", "PE32", "PIC", "TE", "FV_IMAGE", "RAW", "DXE_DEPEX",\\r
2984 "UI", "PEI_DEPEX", "VERSION", "SUBTYPE_GUID", "SMM_DEPEX"):\r
2985 raise Warning("Unknown leaf section name '%s'" % SectionName, self.FileName, self.CurrentLineNumber)\r
2986\r
2987\r
2988 if self.__IsKeyword("Fixed", True):\r
2989 Fixed = True \r
2990 \r
2991 if self.__IsKeyword("CheckSum", True):\r
2992 CheckSum = True\r
2993 \r
2994 if self.__GetAlignment():\r
e921f58d
YZ
2995 if self.__Token not in ("Auto", "8", "16", "32", "64", "128", "512", "1K", "4K", "32K" ,"64K", "128K",\r
2996 "256K", "512K", "1M", "2M", "4M", "8M", "16M"):\r
30fdf114 2997 raise Warning("Incorrect alignment At Line ", self.FileName, self.CurrentLineNumber)\r
52302d4d
LG
2998 if self.__Token == 'Auto' and (not SectionName == 'PE32') and (not SectionName == 'TE'):\r
2999 raise Warning("Auto alignment can only be used in PE32 or TE section ", self.FileName, self.CurrentLineNumber)\r
30fdf114
LG
3000 AlignValue = self.__Token\r
3001 \r
3002 if not self.__GetNextToken():\r
3003 raise Warning("expected File name At Line ", self.FileName, self.CurrentLineNumber)\r
3004 \r
3005 Rule = RuleSimpleFile.RuleSimpleFile()\r
3006 Rule.SectionType = SectionName\r
3007 Rule.FvFileType = Type\r
3008 Rule.NameGuid = NameGuid\r
3009 Rule.Alignment = AlignValue\r
3010 Rule.CheckSum = CheckSum\r
3011 Rule.Fixed = Fixed\r
3012 Rule.FileName = self.__Token\r
3013 Rule.KeyStringList = KeyStringList\r
3014 if KeepReloc != None:\r
3015 Rule.KeepReloc = KeepReloc\r
3016 return Rule\r
3017 \r
3018 ## __GetEfiSection() method\r
3019 #\r
3020 # Get section list for Rule\r
3021 #\r
3022 # @param self The object pointer\r
3023 # @param Obj for whom section is got\r
3024 # @retval True Successfully find section statement\r
3025 # @retval False Not able to find section statement\r
3026 #\r
3027 def __GetEfiSection(self, Obj):\r
3028 \r
3029 OldPos = self.GetFileBufferPos()\r
3030 if not self.__GetNextWord():\r
3031 return False\r
3032 SectionName = self.__Token\r
3033 \r
3034 if SectionName not in ("COMPAT16", "PE32", "PIC", "TE", "FV_IMAGE", "RAW", "DXE_DEPEX",\\r
3035 "UI", "VERSION", "PEI_DEPEX", "GUID", "SMM_DEPEX"):\r
3036 self.__UndoToken()\r
3037 return False\r
3038 \r
3039 if SectionName == "FV_IMAGE":\r
3040 FvImageSectionObj = FvImageSection.FvImageSection()\r
3041 if self.__IsKeyword("FV_IMAGE"):\r
3042 pass\r
3043 if self.__IsToken( "{"):\r
3044 FvObj = Fv.FV()\r
3045 self.__GetDefineStatements(FvObj)\r
3046 self.__GetBlockStatement(FvObj)\r
3047 self.__GetSetStatements(FvObj)\r
3048 self.__GetFvAlignment(FvObj)\r
3049 self.__GetFvAttributes(FvObj)\r
3050 self.__GetAprioriSection(FvObj)\r
3051 self.__GetAprioriSection(FvObj)\r
3052 \r
3053 while True:\r
3054 IsInf = self.__GetInfStatement(FvObj)\r
3055 IsFile = self.__GetFileStatement(FvObj)\r
3056 if not IsInf and not IsFile:\r
3057 break\r
3058 \r
3059 if not self.__IsToken( "}"):\r
3060 raise Warning("expected '}' At Line ", self.FileName, self.CurrentLineNumber)\r
3061 FvImageSectionObj.Fv = FvObj\r
3062 FvImageSectionObj.FvName = None\r
3063 \r
3064 else:\r
3065 if not self.__IsKeyword("FV"):\r
3066 raise Warning("expected 'FV' At Line ", self.FileName, self.CurrentLineNumber)\r
3067 FvImageSectionObj.FvFileType = self.__Token\r
3068 \r
3069 if self.__GetAlignment():\r
e921f58d
YZ
3070 if self.__Token not in ("8", "16", "32", "64", "128", "512", "1K", "4K", "32K" ,"64K", "128K",\r
3071 "256K", "512K", "1M", "2M", "4M", "8M", "16M"):\r
30fdf114
LG
3072 raise Warning("Incorrect alignment At Line ", self.FileName, self.CurrentLineNumber)\r
3073 FvImageSectionObj.Alignment = self.__Token\r
3074 \r
30fdf114
LG
3075 if self.__IsToken('|'):\r
3076 FvImageSectionObj.FvFileExtension = self.__GetFileExtension()\r
3077 elif self.__GetNextToken():\r
3078 if self.__Token not in ("}", "COMPAT16", "PE32", "PIC", "TE", "FV_IMAGE", "RAW", "DXE_DEPEX",\\r
3079 "UI", "VERSION", "PEI_DEPEX", "GUID", "SMM_DEPEX"):\r
3080 FvImageSectionObj.FvFileName = self.__Token\r
3081 else:\r
3082 self.__UndoToken()\r
3083 else:\r
3084 raise Warning("expected FV file name At Line ", self.FileName, self.CurrentLineNumber)\r
3085 \r
3086 Obj.SectionList.append(FvImageSectionObj)\r
3087 return True\r
3088 \r
3089 EfiSectionObj = EfiSection.EfiSection()\r
3090 EfiSectionObj.SectionType = SectionName\r
3091 \r
3092 if not self.__GetNextToken():\r
3093 raise Warning("expected file type At Line ", self.FileName, self.CurrentLineNumber)\r
3094 \r
3095 if self.__Token == "STRING":\r
3096 if not self.__RuleSectionCouldHaveString(EfiSectionObj.SectionType):\r
3097 raise Warning("%s section could NOT have string data At Line %d" % (EfiSectionObj.SectionType, self.CurrentLineNumber), self.FileName, self.CurrentLineNumber)\r
3098 \r
3099 if not self.__IsToken('='):\r
3100 raise Warning("expected '=' At Line ", self.FileName, self.CurrentLineNumber)\r
3101 \r
3102 if not self.__GetNextToken():\r
3103 raise Warning("expected Quoted String At Line ", self.FileName, self.CurrentLineNumber)\r
3104 \r
3105 if self.__GetStringData():\r
3106 EfiSectionObj.StringData = self.__Token\r
3107 \r
3108 if self.__IsKeyword("BUILD_NUM"):\r
3109 if not self.__RuleSectionCouldHaveBuildNum(EfiSectionObj.SectionType):\r
3110 raise Warning("%s section could NOT have BUILD_NUM At Line %d" % (EfiSectionObj.SectionType, self.CurrentLineNumber), self.FileName, self.CurrentLineNumber)\r
3111 \r
3112 if not self.__IsToken("="):\r
3113 raise Warning("expected '=' At Line ", self.FileName, self.CurrentLineNumber)\r
3114 if not self.__GetNextToken():\r
3115 raise Warning("expected Build number At Line ", self.FileName, self.CurrentLineNumber)\r
3116 EfiSectionObj.BuildNum = self.__Token\r
3117 \r
3118 else:\r
3119 EfiSectionObj.FileType = self.__Token\r
3120 self.__CheckRuleSectionFileType(EfiSectionObj.SectionType, EfiSectionObj.FileType)\r
3121 \r
3122 if self.__IsKeyword("Optional"):\r
3123 if not self.__RuleSectionCouldBeOptional(EfiSectionObj.SectionType):\r
3124 raise Warning("%s section could NOT be optional At Line %d" % (EfiSectionObj.SectionType, self.CurrentLineNumber), self.FileName, self.CurrentLineNumber)\r
3125 EfiSectionObj.Optional = True\r
3126 \r
3127 if self.__IsKeyword("BUILD_NUM"):\r
3128 if not self.__RuleSectionCouldHaveBuildNum(EfiSectionObj.SectionType):\r
3129 raise Warning("%s section could NOT have BUILD_NUM At Line %d" % (EfiSectionObj.SectionType, self.CurrentLineNumber), self.FileName, self.CurrentLineNumber)\r
3130 \r
3131 if not self.__IsToken("="):\r
3132 raise Warning("expected '=' At Line ", self.FileName, self.CurrentLineNumber)\r
3133 if not self.__GetNextToken():\r
3134 raise Warning("expected Build number At Line ", self.FileName, self.CurrentLineNumber)\r
3135 EfiSectionObj.BuildNum = self.__Token\r
3136 \r
3137 if self.__GetAlignment():\r
e921f58d
YZ
3138 if self.__Token not in ("Auto", "8", "16", "32", "64", "128", "512", "1K", "4K", "32K" ,"64K", "128K",\r
3139 "256K", "512K", "1M", "2M", "4M", "8M", "16M"):\r
52302d4d
LG
3140 raise Warning("Incorrect alignment '%s'" % self.__Token, self.FileName, self.CurrentLineNumber)\r
3141 if self.__Token == 'Auto' and (not SectionName == 'PE32') and (not SectionName == 'TE'):\r
3142 raise Warning("Auto alignment can only be used in PE32 or TE section ", self.FileName, self.CurrentLineNumber)\r
30fdf114
LG
3143 EfiSectionObj.Alignment = self.__Token\r