BaseTools: Update FMP Capsule support to follow FDF spec

Current the FMP Capsule feature is supported, but its format has a little
different with FDF spec. so this patch 1) Align the FMP Capsule with FDF
spec. 2) fix some style issue, eg: Tab. 3) Add a SectionParser function to
check the section header info since this method is used in 7 places.

Cc: Liming Gao <liming.gao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Yonghong Zhu <yonghong.zhu@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
diff --git a/BaseTools/Source/Python/GenFds/FdfParser.py b/BaseTools/Source/Python/GenFds/FdfParser.py
index 28af09b..72fb3dc 100644
--- a/BaseTools/Source/Python/GenFds/FdfParser.py
+++ b/BaseTools/Source/Python/GenFds/FdfParser.py
@@ -55,7 +55,6 @@
 

 from Common.Misc import tdict

 

-import re

 import Common.LongFilePathOs as os

 from Common.LongFilePathSupport import OpenLongFilePath as open

 

@@ -106,7 +105,7 @@
         if Profile.IsLineInFile(Line):

             return Profile.GetLineInFile(Line)

         elif Line >= Profile.InsertStartLineNumber and Profile.Level == 1:

-           InsertedLines += Profile.GetTotalLines()

+            InsertedLines += Profile.GetTotalLines()

 

     return (File, Line - InsertedLines)

 

@@ -181,7 +180,7 @@
         TotalLines = self.InsertAdjust + len(self.FileLinesList)

 

         for Profile in self.IncludeFileList:

-          TotalLines += Profile.GetTotalLines()

+            TotalLines += Profile.GetTotalLines()

 

         return TotalLines

 

@@ -1398,6 +1397,20 @@
                 % (FileLineTuple[1], self.CurrentOffsetWithinLine + 1, self.Profile.FileLinesList[self.CurrentLineNumber - 1][self.CurrentOffsetWithinLine :].rstrip('\n').rstrip('\r'))

             raise

 

+    ## SectionParser() method

+    #

+    #   Parse the file section info

+    #   Exception will be raised if syntax error found

+    #

+    #   @param  self          The object pointer

+    #   @param  section       The section string

+

+    def SectionParser(self, section):

+        S = section.upper()

+        if not S.startswith("[DEFINES") and not S.startswith("[FD.") and not S.startswith("[FV.") and not S.startswith("[CAPSULE.") \

+            and not S.startswith("[VTF.") and not S.startswith("[RULE.") and not S.startswith("[OPTIONROM.") and not S.startswith('[FMPPAYLOAD.'):

+            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)

+

     ## __GetDefines() method

     #

     #   Get Defines section contents and store its data into AllMacrosList

@@ -1413,9 +1426,7 @@
 

         S = self.__Token.upper()

         if S.startswith("[") and not S.startswith("[DEFINES"):

-            if not S.startswith("[FD.") and not S.startswith("[FV.") and not S.startswith("[CAPSULE.") \

-                and not S.startswith("[VTF.") and not S.startswith("[RULE.") and not S.startswith("[OPTIONROM."):

-                raise Warning("Unknown section or section appear sequence error (The correct sequence should be [DEFINES], [FD.], [FV.], [Capsule.], [VTF.], [Rule.], [OptionRom.])", self.FileName, self.CurrentLineNumber)

+            self.SectionParser(S)

             self.__UndoToken()

             return False

 

@@ -2108,9 +2119,7 @@
 

         S = self.__Token.upper()

         if S.startswith("[") and not S.startswith("[FV."):

-            if not S.startswith('[FMPPAYLOAD.') and not S.startswith("[CAPSULE.") \

-                and not S.startswith("[VTF.") and not S.startswith("[RULE.") and not S.startswith("[OPTIONROM."):

-                raise Warning("Unknown section or section appear sequence error (The correct sequence should be [FD.], [FV.], [Capsule.], [VTF.], [Rule.], [OptionRom.])", self.FileName, self.CurrentLineNumber)

+            self.SectionParser(S)

             self.__UndoToken()

             return False

 

@@ -3151,9 +3160,8 @@
         if not self.__GetNextToken():

             return False

         S = self.__Token.upper()

-        if not S.startswith("[FMPPAYLOAD."):

-            if not S.startswith("[CAPSULE.") and not S.startswith("[VTF.") and not S.startswith("[RULE.") and not S.startswith("[OPTIONROM."):

-                raise Warning("Unknown section or section appear sequence error (The correct sequence should be [FD.], [FV.], [FmpPayload.], [Capsule.], [VTF.], [Rule.], [OptionRom.])", self.FileName, self.CurrentLineNumber)

+        if S.startswith("[") and not S.startswith("[FMPPAYLOAD."):

+            self.SectionParser(S)

             self.__UndoToken()

             return False

 

@@ -3223,8 +3231,7 @@
 

         S = self.__Token.upper()

         if S.startswith("[") and not S.startswith("[CAPSULE."):

-            if not S.startswith("[VTF.") and not S.startswith("[RULE.") and not S.startswith("[OPTIONROM."):

-                raise Warning("Unknown section or section appear sequence error (The correct sequence should be [FD.], [FV.], [Capsule.], [VTF.], [Rule.], [OptionRom.])", self.FileName, self.CurrentLineNumber)

+            self.SectionParser(S)

             self.__UndoToken()

             return False

 

@@ -3394,18 +3401,19 @@
         return True

 

     def __GetFmpStatement(self, CapsuleObj):

-        if not self.__IsKeyword("FMP"):

-            return False

+        if not self.__IsKeyword("FMP_PAYLOAD"):

+            if not self.__IsKeyword("FMP"):

+                return False

 

-        if not self.__IsKeyword("PAYLOAD"):

-            self.__UndoToken()

-            return False

+            if not self.__IsKeyword("PAYLOAD"):

+                self.__UndoToken()

+                return False

 

         if not self.__IsToken("="):

             raise Warning("expected '='", self.FileName, self.CurrentLineNumber)

 

         if not self.__GetNextToken():

-            raise Warning("expected payload name after FMP PAYLOAD =", self.FileName, self.CurrentLineNumber)

+            raise Warning("expected payload name after FMP_PAYLOAD =", self.FileName, self.CurrentLineNumber)

         Payload = self.__Token.upper()

         if Payload not in self.Profile.FmpPayloadDict:

             raise Warning("This FMP Payload does not exist: %s" % self.__Token, self.FileName, self.CurrentLineNumber)

@@ -3507,8 +3515,7 @@
 

         S = self.__Token.upper()

         if S.startswith("[") and not S.startswith("[RULE."):

-            if not S.startswith("[OPTIONROM."):

-                raise Warning("Unknown section or section appear sequence error (The correct sequence should be [FD.], [FV.], [Capsule.], [VTF.], [Rule.], [OptionRom.])", self.FileName, self.CurrentLineNumber)

+            self.SectionParser(S)

             self.__UndoToken()

             return False

         self.__UndoToken()

@@ -3586,7 +3593,7 @@
     #

     def __GetFileExtension(self):

         if not self.__IsToken("."):

-                raise Warning("expected '.'", self.FileName, self.CurrentLineNumber)

+            raise Warning("expected '.'", self.FileName, self.CurrentLineNumber)

 

         Ext = ""

         if self.__GetNextToken():

@@ -4084,8 +4091,7 @@
 

         S = self.__Token.upper()

         if S.startswith("[") and not S.startswith("[VTF."):

-            if not S.startswith("[RULE.") and not S.startswith("[OPTIONROM."):

-                raise Warning("Unknown section or section appear sequence error (The correct sequence should be [FD.], [FV.], [Capsule.], [VTF.], [Rule.], [OptionRom.])", self.FileName, self.CurrentLineNumber)

+            self.SectionParser(S)

             self.__UndoToken()

             return False

 

@@ -4291,7 +4297,9 @@
 

         S = self.__Token.upper()

         if S.startswith("[") and not S.startswith("[OPTIONROM."):

-            raise Warning("Unknown section or section appear sequence error (The correct sequence should be [FD.], [FV.], [Capsule.], [VTF.], [Rule.], [OptionRom.])", self.FileName, self.CurrentLineNumber)

+            self.SectionParser(S)

+            self.__UndoToken()

+            return False

         

         self.__UndoToken()

         if not self.__IsToken("[OptionRom.", True):