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