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