1. Support CAPSULE flash region type in FD section of FDF file.
authorlgao4 <lgao4@7335b38e-4728-0410-8992-fb3ffe349368>
Fri, 21 Aug 2009 08:52:36 +0000 (08:52 +0000)
committerlgao4 <lgao4@7335b38e-4728-0410-8992-fb3ffe349368>
Fri, 21 Aug 2009 08:52:36 +0000 (08:52 +0000)
2. Enhance the circle check between FV, Capsule and FD image.

git-svn-id: https://buildtools.tianocore.org/svn/buildtools/trunk/BaseTools@1688 7335b38e-4728-0410-8992-fb3ffe349368

Source/Python/Common/FdfParserLite.py
Source/Python/GenFds/Capsule.py
Source/Python/GenFds/CapsuleData.py
Source/Python/GenFds/Fd.py
Source/Python/GenFds/FdfParser.py
Source/Python/GenFds/FfsFileStatement.py
Source/Python/GenFds/Fv.py
Source/Python/GenFds/GenFds.py
Source/Python/GenFds/Region.py

index 59006fa..5099ed6 100644 (file)
@@ -1599,7 +1599,7 @@ class FdfParser(object):
         if not self.__GetNextWord():\r
             return True\r
         \r
-        if not self.__Token in ("SET", "FV", "FILE", "DATA"):\r
+        if not self.__Token in ("SET", "FV", "FILE", "DATA", "CAPSULE"):\r
             self.__UndoToken()\r
             RegionObj.PcdOffset = self.__GetNextPcdName()\r
             self.Profile.PcdDict[RegionObj.PcdOffset] = RegionObj.Offset + long(Fd.BaseAddress, 0)\r
@@ -1620,10 +1620,14 @@ class FdfParser(object):
             if not self.__GetNextWord():\r
                 return True\r
             \r
-        if self.__Token == "FV":\r
+        elif self.__Token == "FV":\r
             self.__UndoToken()\r
             self.__GetRegionFvType( RegionObj)\r
 \r
+        elif self.__Token == "CAPSULE":\r
+            self.__UndoToken()\r
+            self.__GetRegionCapType( RegionObj)\r
+\r
         elif self.__Token == "FILE":\r
             self.__UndoToken()\r
             self.__GetRegionFileType( RegionObj)\r
@@ -1664,7 +1668,38 @@ class FdfParser(object):
                 raise Warning("expected FV name At Line ", self.FileName, self.CurrentLineNumber)\r
         \r
             RegionObj.RegionDataList.append(self.__Token)\r
-        \r
+\r
+    ## __GetRegionCapType() method\r
+    #\r
+    #   Get region capsule data for region\r
+    #\r
+    #   @param  self        The object pointer\r
+    #   @param  RegionObj   for whom region data is got\r
+    #\r
+    def __GetRegionCapType(self, RegionObj):\r
+\r
+        if not self.__IsKeyword("CAPSULE"):\r
+            raise Warning("expected Keyword 'CAPSULE' at line", self.FileName, self.CurrentLineNumber)\r
+\r
+        if not self.__IsToken("="):\r
+            raise Warning("expected '=' at line", self.FileName, self.CurrentLineNumber)\r
+\r
+        if not self.__GetNextToken():\r
+            raise Warning("expected CAPSULE name at line", self.FileName, self.CurrentLineNumber)\r
+\r
+        RegionObj.RegionType = "CAPSULE"\r
+        RegionObj.RegionDataList.append(self.__Token)\r
+\r
+        while self.__IsKeyword("CAPSULE"):\r
+\r
+            if not self.__IsToken("="):\r
+                raise Warning("expected '=' at line", self.FileName, self.CurrentLineNumber)\r
+\r
+            if not self.__GetNextToken():\r
+                raise Warning("expected CAPSULE name at line", self.FileName, self.CurrentLineNumber)\r
+\r
+            RegionObj.RegionDataList.append(self.__Token)\r
+\r
     ## __GetRegionFileType() method\r
     #\r
     #   Get region file data for region\r
@@ -2713,7 +2748,7 @@ class FdfParser(object):
         \r
         Arch = self.__SkippedChars.rstrip(".")\r
         if Arch.upper() not in ("IA32", "X64", "IPF", "EBC", "ARM", "COMMON"):\r
-            raise Warning("Unknown Arch At line ", self.FileName, self.CurrentLineNumber)\r
+            raise Warning("Unknown Arch '%s'" % Arch, self.FileName, self.CurrentLineNumber)\r
         \r
         ModuleType = self.__GetModuleType()\r
         \r
@@ -2763,7 +2798,7 @@ class FdfParser(object):
                              "DXE_SMM_DRIVER", "DXE_RUNTIME_DRIVER", \\r
                              "UEFI_DRIVER", "UEFI_APPLICATION", "USER_DEFINED", "DEFAULT", "BASE", \\r
                              "SECURITY_CORE", "COMBINED_PEIM_DRIVER", "PIC_PEIM", "RELOCATABLE_PEIM", \\r
-                             "PE32_PEIM", "BS_DRIVER", "RT_DRIVER", "SAL_RT_DRIVER", "APPLICATION"):\r
+                             "PE32_PEIM", "BS_DRIVER", "RT_DRIVER", "SAL_RT_DRIVER", "APPLICATION", "ACPITABLE", "SMM_DRIVER", "SMM_CORE"):\r
             raise Warning("Unknown Module type At line ", self.FileName, self.CurrentLineNumber)\r
         return self.__Token\r
     \r
@@ -2803,11 +2838,11 @@ class FdfParser(object):
             raise Warning("expected FILE At Line ", self.FileName, self.CurrentLineNumber)\r
         \r
         if not self.__GetNextWord():\r
-            raise Warning("expected FV type At Line ", self.FileName, self.CurrentLineNumber)\r
+            raise Warning("expected FFS type At Line ", self.FileName, self.CurrentLineNumber)\r
         \r
         Type = self.__Token.strip().upper()\r
         if Type not in ("RAW", "FREEFORM", "SEC", "PEI_CORE", "PEIM",\\r
-                             "PEI_DXE_COMBO", "DRIVER", "DXE_CORE", "APPLICATION", "FV_IMAGE"):\r
+                             "PEI_DXE_COMBO", "DRIVER", "DXE_CORE", "APPLICATION", "FV_IMAGE", "SMM_DXE_COMBO", "SMM", "SMM_CORE"):\r
             raise Warning("Unknown FV type At line ", self.FileName, self.CurrentLineNumber)\r
 \r
         if not self.__IsToken("="):\r
index 7dbb844..961b88a 100755 (executable)
@@ -21,6 +21,7 @@ import os
 import subprocess\r
 import StringIO\r
 from Common.Misc import SaveFileOnChange\r
+from GenFds import GenFds\r
 \r
 \r
 T_CHAR_LF = '\n'\r
@@ -39,17 +40,25 @@ class Capsule (CapsuleClassObject) :
         self.BlockSize = None\r
         # For GenFv\r
         self.BlockNum = None\r
+        self.CapsuleName = None\r
 \r
     ## Generate capsule\r
     #\r
     #   @param  self        The object pointer\r
+    #   @retval string      Generated Capsule file path\r
     #\r
     def GenCapsule(self):\r
+        if self.UiCapsuleName.upper() + 'cap' in GenFds.ImageBinDict.keys():\r
+            return GenFds.ImageBinDict[self.UiCapsuleName.upper() + 'cap']\r
+\r
+        GenFdsGlobalVariable.InfLogger( "\nGenerate %s Capsule" %self.UiCapsuleName)\r
         CapInfFile = self.GenCapInf()\r
         CapInfFile.writelines("[files]" + T_CHAR_LF)\r
         CapFileList = []\r
         for CapsuleDataObj in self.CapsuleDataList :\r
+            CapsuleDataObj.CapsuleName = self.CapsuleName\r
             FileName = CapsuleDataObj.GenCapsuleSubItem()\r
+            CapsuleDataObj.CapsuleName = None\r
             CapFileList.append(FileName)\r
             CapInfFile.writelines("EFI_FILE_NAME = " + \\r
                                    FileName      + \\r
@@ -67,7 +76,11 @@ class Capsule (CapsuleClassObject) :
                                 Capsule=True,\r
                                 FfsList=CapFileList\r
                                 )\r
+\r
+        GenFdsGlobalVariable.VerboseLogger( "\nGenerate %s Capsule Successfully" %self.UiCapsuleName)\r
         GenFdsGlobalVariable.SharpCounter = 0\r
+        GenFds.ImageBinDict[self.UiCapsuleName.upper() + 'cap'] = CapOutputFile\r
+        return CapOutputFile\r
 \r
     ## Generate inf file for capsule\r
     #\r
index db29737..0940094 100755 (executable)
@@ -45,6 +45,7 @@ class CapsuleFfs (CapsuleData):
     #\r
     def __init_(self) :\r
         self.Ffs = None\r
+        self.FvName = None\r
 \r
     ## generate FFS capsule data\r
     #\r
@@ -64,7 +65,9 @@ class CapsuleFv (CapsuleData):
     #   @param  self        The object pointer\r
     #\r
     def __init__(self) :\r
+        self.Ffs = None\r
         self.FvName = None\r
+        self.CapsuleName = None\r
 \r
     ## generate FV capsule data\r
     #\r
@@ -76,9 +79,11 @@ class CapsuleFv (CapsuleData):
             if self.FvName.upper() in GenFdsGlobalVariable.FdfParser.Profile.FvDict.keys():\r
                 FvObj = GenFdsGlobalVariable.FdfParser.Profile.FvDict.get(self.FvName.upper())\r
                 FdBuffer = StringIO.StringIO('')\r
+                FvObj.CapsuleName = self.CapsuleName\r
                 FvFile = FvObj.AddToBuffer(FdBuffer)\r
+                FvObj.CapsuleName = None\r
+                FdBuffer.close()\r
                 return FvFile\r
-            \r
         else:\r
             FvFile = GenFdsGlobalVariable.ReplaceWorkspaceMacro(self.FvName)\r
             return FvFile\r
index 99baa6a..370008c 100644 (file)
@@ -26,6 +26,7 @@ from CommonDataClass.FdfClass import FDClassObject
 from Common import EdkLogger\r
 from Common.BuildToolError import *\r
 from Common.Misc import SaveFileOnChange\r
+from GenFds import GenFds\r
 \r
 ## generate FD\r
 #\r
@@ -42,11 +43,12 @@ class FD(FDClassObject):
     #\r
     #   Generate FD\r
     #\r
-    #   @param  self        The object pointer\r
-    #   @param  FvBinDict   dictionary contains generated FV name and its file name\r
     #   @retval string      Generated FD file name\r
     #\r
-    def GenFd (self, FvBinDict):\r
+    def GenFd (self):\r
+        if self.FdUiName.upper() + 'fd' in GenFds.ImageBinDict.keys():\r
+            return GenFds.ImageBinDict[self.FdUiName.upper() + 'fd']\r
+\r
         #\r
         # Print Information\r
         #\r
@@ -80,7 +82,7 @@ class FD(FDClassObject):
                 PadRegion = Region.Region()\r
                 PadRegion.Offset = PreviousRegionStart + PreviousRegionSize\r
                 PadRegion.Size = RegionObj.Offset - PadRegion.Offset\r
-                PadRegion.AddToBuffer(FdBuffer, self.BaseAddress, self.BlockSizeList, self.ErasePolarity, FvBinDict, self.vtfRawDict, self.DefineVarDict)\r
+                PadRegion.AddToBuffer(FdBuffer, self.BaseAddress, self.BlockSizeList, self.ErasePolarity, GenFds.ImageBinDict, self.vtfRawDict, self.DefineVarDict)\r
             PreviousRegionStart = RegionObj.Offset\r
             PreviousRegionSize = RegionObj.Size\r
             #\r
@@ -89,23 +91,19 @@ class FD(FDClassObject):
             if PreviousRegionSize > self.Size:\r
                 EdkLogger.error("GenFds", GENFDS_ERROR, 'FD %s size too small' % self.FdUiName)\r
             GenFdsGlobalVariable.VerboseLogger('Call each region\'s AddToBuffer function')\r
-            RegionObj.AddToBuffer (FdBuffer, self.BaseAddress, self.BlockSizeList, self.ErasePolarity, FvBinDict, self.vtfRawDict, self.DefineVarDict)\r
+            RegionObj.AddToBuffer (FdBuffer, self.BaseAddress, self.BlockSizeList, self.ErasePolarity, GenFds.ImageBinDict, self.vtfRawDict, self.DefineVarDict)\r
         #\r
         # Create a empty Fd file\r
         #\r
         GenFdsGlobalVariable.VerboseLogger ('Create an empty Fd file')\r
-        FdFileName = os.path.join(GenFdsGlobalVariable.FvDir,\r
-                                  self.FdUiName + '.fd')\r
-        #FdFile = open(FdFileName, 'wb')\r
-\r
+        FdFileName = os.path.join(GenFdsGlobalVariable.FvDir,self.FdUiName + '.fd')\r
         #\r
         # Write the buffer contents to Fd file\r
         #\r
         GenFdsGlobalVariable.VerboseLogger('Write the buffer contents to Fd file')\r
         SaveFileOnChange(FdFileName, FdBuffer.getvalue())\r
-        #FdFile.write(FdBuffer.getvalue());\r
-        #FdFile.close();\r
         FdBuffer.close();\r
+        GenFds.ImageBinDict[self.FdUiName.upper() + 'fd'] = FdFileName\r
         return FdFileName\r
 \r
     ## generate VTF\r
index f35a82d..07de926 100644 (file)
@@ -173,7 +173,7 @@ class FileProfile :
 \r
         self.FdDict = {}\r
         self.FvDict = {}\r
-        self.CapsuleList = []\r
+        self.CapsuleDict = {}\r
         self.VtfList = []\r
         self.RuleDict = {}\r
         self.OptRomDict = {}\r
@@ -1622,7 +1622,7 @@ class FdfParser:
         if not self.__GetNextWord():\r
             return True\r
 \r
-        if not self.__Token in ("SET", "FV", "FILE", "DATA"):\r
+        if not self.__Token in ("SET", "FV", "FILE", "DATA", "CAPSULE"):\r
             self.__UndoToken()\r
             RegionObj.PcdOffset = self.__GetNextPcdName()\r
             self.Profile.PcdDict[RegionObj.PcdOffset] = "0x%08X" % (RegionObj.Offset + long(Fd.BaseAddress, 0))\r
@@ -1639,10 +1639,14 @@ class FdfParser:
             if not self.__GetNextWord():\r
                 return True\r
 \r
-        if self.__Token == "FV":\r
+        elif self.__Token == "FV":\r
             self.__UndoToken()\r
             self.__GetRegionFvType( RegionObj)\r
 \r
+        elif self.__Token == "CAPSULE":\r
+            self.__UndoToken()\r
+            self.__GetRegionCapType( RegionObj)\r
+\r
         elif self.__Token == "FILE":\r
             self.__UndoToken()\r
             self.__GetRegionFileType( RegionObj)\r
@@ -1684,6 +1688,37 @@ class FdfParser:
 \r
             RegionObj.RegionDataList.append(self.__Token)\r
 \r
+    ## __GetRegionCapType() method\r
+    #\r
+    #   Get region capsule data for region\r
+    #\r
+    #   @param  self        The object pointer\r
+    #   @param  RegionObj   for whom region data is got\r
+    #\r
+    def __GetRegionCapType(self, RegionObj):\r
+\r
+        if not self.__IsKeyword("CAPSULE"):\r
+            raise Warning("expected Keyword 'CAPSULE'", self.FileName, self.CurrentLineNumber)\r
+\r
+        if not self.__IsToken("="):\r
+            raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
+\r
+        if not self.__GetNextToken():\r
+            raise Warning("expected CAPSULE name", self.FileName, self.CurrentLineNumber)\r
+\r
+        RegionObj.RegionType = "CAPSULE"\r
+        RegionObj.RegionDataList.append(self.__Token)\r
+\r
+        while self.__IsKeyword("CAPSULE"):\r
+\r
+            if not self.__IsToken("="):\r
+                raise Warning("expected '='", self.FileName, self.CurrentLineNumber)\r
+\r
+            if not self.__GetNextToken():\r
+                raise Warning("expected CAPSULE name", self.FileName, self.CurrentLineNumber)\r
+\r
+            RegionObj.RegionDataList.append(self.__Token)\r
+\r
     ## __GetRegionFileType() method\r
     #\r
     #   Get region file data for region\r
@@ -2624,7 +2659,7 @@ class FdfParser:
             CapsuleObj.CreateFile = self.__Token\r
 \r
         self.__GetCapsuleStatements(CapsuleObj)\r
-        self.Profile.CapsuleList.append(CapsuleObj)\r
+        self.Profile.CapsuleDict[CapsuleObj.UiCapsuleName] = CapsuleObj\r
         return True\r
 \r
     ## __GetCapsuleStatements() method\r
@@ -2638,10 +2673,9 @@ class FdfParser:
         self.__GetCapsuleTokens(Obj)\r
         self.__GetDefineStatements(Obj)\r
         self.__GetSetStatements(Obj)\r
-\r
         self.__GetCapsuleData(Obj)\r
 \r
-    ## __GetCapsuleStatements() method\r
+    ## __GetCapsuleTokens() method\r
     #\r
     #   Get token statements for capsule\r
     #\r
@@ -3637,8 +3671,52 @@ class FdfParser:
         Obj.FfsList.append(FfsFileObj)\r
 \r
         return True\r
-        \r
-            \r
+\r
+    ## __GetCapInFd() method\r
+    #\r
+    #   Get Cap list contained in FD\r
+    #\r
+    #   @param  self        The object pointer\r
+    #   @param  FdName      FD name\r
+    #   @retval CapList     List of Capsule in FD\r
+    #\r
+    def __GetCapInFd (self, FdName):\r
+\r
+        CapList = []\r
+        if FdName.upper() in self.Profile.FdDict.keys():\r
+            FdObj = self.Profile.FdDict[FdName.upper()]\r
+            for elementRegion in FdObj.RegionList:\r
+                if elementRegion.RegionType == 'CAPSULE':\r
+                    for elementRegionData in elementRegion.RegionDataList:\r
+                        if elementRegionData.endswith(".cap"):\r
+                            continue\r
+                        if elementRegionData != None and elementRegionData.upper() not in CapList:\r
+                            CapList.append(elementRegionData.upper())\r
+        return CapList\r
+\r
+    ## __GetReferencedFdCapTuple() method\r
+    #\r
+    #   Get FV and FD list referenced by a capsule image\r
+    #\r
+    #   @param  self        The object pointer\r
+    #   @param  CapObj      Capsule section to be searched\r
+    #   @param  RefFdList   referenced FD by section\r
+    #   @param  RefFvList   referenced FV by section\r
+    #\r
+    def __GetReferencedFdCapTuple(self, CapObj, RefFdList = [], RefFvList = []):\r
+\r
+        for CapsuleDataObj in CapObj.CapsuleDataList :\r
+            if CapsuleDataObj.FvName != None and CapsuleDataObj.FvName.upper() not in RefFvList:\r
+                RefFvList.append (CapsuleDataObj.FvName.upper())\r
+            elif CapsuleDataObj.Ffs != None:\r
+              if isinstance(CapsuleDataObj.Ffs, FfsFileStatement.FileStatement):\r
+                  if CapsuleDataObj.Ffs.FvName != None and CapsuleDataObj.Ffs.FvName.upper() not in RefFvList:\r
+                      RefFvList.append(CapsuleDataObj.Ffs.FvName.upper())\r
+                  elif CapsuleDataObj.Ffs.FdName != None and CapsuleDataObj.Ffs.FdName.upper() not in RefFdList:\r
+                      RefFdList.append(CapsuleDataObj.Ffs.FdName.upper())\r
+                  else:\r
+                      self.__GetReferencedFdFvTupleFromSection(CapsuleDataObj.Ffs, RefFdList, RefFvList)\r
+\r
     ## __GetFvInFd() method\r
     #\r
     #   Get FV list contained in FD\r
@@ -3655,6 +3733,8 @@ class FdfParser:
             for elementRegion in FdObj.RegionList:\r
                 if elementRegion.RegionType == 'FV':\r
                     for elementRegionData in elementRegion.RegionDataList:\r
+                        if elementRegionData.endswith(".fv"):\r
+                            continue\r
                         if elementRegionData != None and elementRegionData.upper() not in FvList:\r
                             FvList.append(elementRegionData.upper())\r
         return FvList\r
@@ -3713,60 +3793,126 @@ class FdfParser:
     #   @retval False       Not exists cycle reference\r
     #\r
     def CycleReferenceCheck(self):\r
+        #\r
+        # Check the cycle between FV and FD image\r
+        #\r
+        MaxLength = len (self.Profile.FvDict)\r
+        for FvName in self.Profile.FvDict.keys():\r
+            LogStr = "\nCycle Reference Checking for FV: %s\n" % FvName\r
+            RefFvStack = []\r
+            RefFvStack.append(FvName)\r
+            FdAnalyzedList = []\r
+            \r
+            Index = 0\r
+            while RefFvStack != [] and Index < MaxLength:\r
+                Index = Index + 1\r
+                FvNameFromStack = RefFvStack.pop()\r
+                if FvNameFromStack.upper() in self.Profile.FvDict.keys():\r
+                    FvObj = self.Profile.FvDict[FvNameFromStack.upper()]\r
+                else:\r
+                    continue\r
 \r
-        CycleRefExists = False\r
+                RefFdList = []\r
+                RefFvList = []\r
+                self.__GetReferencedFdFvTuple(FvObj, RefFdList, RefFvList)\r
 \r
-        try:\r
-            for FvName in self.Profile.FvDict.keys():\r
-                LogStr = "Cycle Reference Checking for FV: %s\n" % FvName\r
-                RefFvStack = []\r
-                RefFvStack.append(FvName)\r
-                FdAnalyzedList = []\r
-\r
-                while RefFvStack != []:\r
-                    FvNameFromStack = RefFvStack.pop()\r
-                    if FvNameFromStack.upper() in self.Profile.FvDict.keys():\r
-                        FvObj = self.Profile.FvDict[FvNameFromStack.upper()]\r
-                    else:\r
+                for RefFdName in RefFdList:\r
+                    if RefFdName in FdAnalyzedList:\r
                         continue\r
 \r
-                    RefFdList = []\r
-                    RefFvList = []\r
-                    self.__GetReferencedFdFvTuple(FvObj, RefFdList, RefFvList)\r
+                    LogStr += "FV %s contains FD %s\n" % (FvNameFromStack, RefFdName)\r
+                    FvInFdList = self.__GetFvInFd(RefFdName)\r
+                    if FvInFdList != []:\r
+                        for FvNameInFd in FvInFdList:\r
+                            LogStr += "FD %s contains FV %s\n" % (RefFdName,FvNameInFd)\r
+                            if FvNameInFd not in RefFvStack:\r
+                                RefFvStack.append(FvNameInFd)\r
+\r
+                            if FvName in RefFvStack or FvNameFromStack in RefFvStack:\r
+                                EdkLogger.info(LogStr)\r
+                                return True\r
+                    FdAnalyzedList.append(RefFdName)\r
 \r
+                for RefFvName in RefFvList:\r
+                    LogStr += "FV %s contains FV %s\n" % (FvNameFromStack, RefFvName)\r
+                    if RefFvName not in RefFvStack:\r
+                        RefFvStack.append(RefFvName)\r
+\r
+                    if FvName in RefFvStack or FvNameFromStack in RefFvStack:\r
+                        EdkLogger.info(LogStr)\r
+                        return True\r
+\r
+        #\r
+        # Check the cycle between Capsule and FD image\r
+        #\r
+        MaxLength = len (self.Profile.CapsuleDict)\r
+        for CapName in self.Profile.CapsuleDict.keys():\r
+            #\r
+            # Capsule image to be checked.\r
+            #\r
+            LogStr = "\n\n\nCycle Reference Checking for Capsule: %s\n" % CapName\r
+            RefCapStack = []\r
+            RefCapStack.append(CapName)\r
+            FdAnalyzedList = []\r
+            FvAnalyzedList = []\r
+            \r
+            Index = 0\r
+            while RefCapStack != [] and Index < MaxLength:\r
+                Index = Index + 1\r
+                CapNameFromStack = RefCapStack.pop()\r
+                if CapNameFromStack.upper() in self.Profile.CapsuleDict.keys():\r
+                    CapObj = self.Profile.CapsuleDict[CapNameFromStack.upper()]\r
+                else:\r
+                    continue\r
+\r
+                RefFvList = []\r
+                RefFdList = []\r
+                self.__GetReferencedFdCapTuple(CapObj, RefFdList, RefFvList)\r
+\r
+                FvListLength = 0\r
+                FdListLength = 0\r
+                while FvListLength < len (RefFvList) or FdListLength < len (RefFdList):\r
                     for RefFdName in RefFdList:\r
                         if RefFdName in FdAnalyzedList:\r
                             continue\r
 \r
-                        LogStr += "FD %s is referenced by FV %s\n" % (RefFdName, FvNameFromStack)\r
+                        LogStr += "Capsule %s contains FD %s\n" % (CapNameFromStack, RefFdName)\r
+                        CapInFdList = self.__GetCapInFd(RefFdName)\r
+                        if CapInFdList != []:\r
+                            for CapNameInFd in CapInFdList:\r
+                                LogStr += "FD %s contains Capsule %s\n" % (RefFdName,CapNameInFd)\r
+                                if CapNameInFd not in RefCapStack:\r
+                                    RefCapStack.append(CapNameInFd)\r
+\r
+                                if CapName in RefCapStack or CapNameFromStack in RefCapStack:\r
+                                    EdkLogger.info(LogStr)\r
+                                    return True\r
+\r
                         FvInFdList = self.__GetFvInFd(RefFdName)\r
                         if FvInFdList != []:\r
-                            LogStr += "FD %s contains FV: " % RefFdName\r
-                            for FvObj in FvInFdList:\r
-                                LogStr += FvObj\r
-                                LogStr += ' \n'\r
-                                if FvObj not in RefFvStack:\r
-                                    RefFvStack.append(FvObj)\r
-\r
-                                if FvName in RefFvStack:\r
-                                    CycleRefExists = True\r
-                                    raise Warning(LogStr)\r
-                        FdAnalyzedList.append(RefFdName)\r
+                            for FvNameInFd in FvInFdList:\r
+                                LogStr += "FD %s contains FV %s\n" % (RefFdName,FvNameInFd)\r
+                                if FvNameInFd not in RefFvList:\r
+                                    RefFvList.append(FvNameInFd)\r
 \r
+                        FdAnalyzedList.append(RefFdName)\r
+                    #\r
+                    # the number of the parsed FV and FD image\r
+                    #\r
+                    FvListLength = len (RefFvList)\r
+                    FdListLength = len (RefFdList)\r
                     for RefFvName in RefFvList:\r
-                        LogStr += "FV %s is referenced by FV %s\n" % (RefFvName, FvNameFromStack)\r
-                        if RefFvName not in RefFvStack:\r
-                            RefFvStack.append(RefFvName)\r
-\r
-                        if FvName in RefFvStack:\r
-                            CycleRefExists = True\r
-                            raise Warning(LogStr)\r
-\r
-        except Warning:\r
-            print LogStr\r
+                        if RefFvName in FvAnalyzedList:\r
+                            continue\r
+                        LogStr += "Capsule %s contains FV %s\n" % (CapNameFromStack, RefFvName)\r
+                        if RefFvName.upper() in self.Profile.FvDict.keys():\r
+                            FvObj = self.Profile.FvDict[RefFvName.upper()]\r
+                        else:\r
+                            continue\r
+                        self.__GetReferencedFdFvTuple(FvObj, RefFdList, RefFvList)\r
+                        FvAnalyzedList.append(RefFvName)\r
 \r
-        finally:\r
-            return CycleRefExists\r
+        return False\r
 \r
 if __name__ == "__main__":\r
     parser = FdfParser("..\LakeportX64Pkg.fdf")\r
index ed778f3..e3f2e68 100755 (executable)
@@ -78,8 +78,7 @@ class FileStatement (FileStatementClassObject) :
             if self.FdName.upper() not in GenFdsGlobalVariable.FdfParser.Profile.FdDict.keys():\r
                 EdkLogger.error("GenFds", GENFDS_ERROR, "FD (%s) is NOT described in FDF file!" % (self.FdName))\r
             Fd = GenFdsGlobalVariable.FdfParser.Profile.FdDict.get(self.FdName.upper())\r
-            FvBin = {}\r
-            FileName = Fd.GenFd(FvBin)\r
+            FileName = Fd.GenFd()\r
             SectionFiles = [FileName]\r
 \r
         elif self.FileName != None:\r
index 74248f7..23ec582 100644 (file)
@@ -44,6 +44,7 @@ class FV (FvClassObject):
         self.BaseAddress = None\r
         self.InfFileName = None\r
         self.FvAddressFileName = None\r
+        self.CapsuleName = None\r
 \r
     ## AddToBuffer()\r
     #\r
@@ -61,10 +62,27 @@ class FV (FvClassObject):
     #\r
     def AddToBuffer (self, Buffer, BaseAddress=None, BlockSize= None, BlockNum=None, ErasePloarity='1', VtfDict=None, MacroDict = {}) :\r
 \r
-        if self.UiFvName.upper() in GenFds.FvBinDict.keys():\r
-            return GenFds.FvBinDict[self.UiFvName.upper()]\r
-\r
-        GenFdsGlobalVariable.InfLogger( "\nGenerating %s FV ..." %self.UiFvName)\r
+        if self.UiFvName.upper() + 'fv' in GenFds.ImageBinDict.keys():\r
+            return GenFds.ImageBinDict[self.UiFvName.upper() + 'fv']\r
+        \r
+        #\r
+        # Check whether FV in Capsule is in FD flash region.\r
+        # If yes, return error. Doesn't support FV in Capsule image is also in FD flash region.\r
+        #\r
+        if self.CapsuleName != None:\r
+            for FdName in GenFdsGlobalVariable.FdfParser.Profile.FdDict.keys():
+                FdObj = GenFdsGlobalVariable.FdfParser.Profile.FdDict[FdName]
+                for RegionObj in FdObj.RegionList:\r
+                    if RegionObj.RegionType == 'FV':\r
+                        for RegionData in RegionObj.RegionDataList:\r
+                            if RegionData.endswith(".fv"):\r
+                                continue\r
+                            elif RegionData.upper() + 'fv' in GenFds.ImageBinDict.keys():\r
+                                continue\r
+                            elif self.UiFvName.upper() == RegionData.upper():\r
+                               GenFdsGlobalVariable.ErrorLogger("Capsule %s in FD region can't contain a FV %s in FD region." % (self.CapsuleName, self.UiFvName.upper()))\r
+\r
+        GenFdsGlobalVariable.InfLogger( "\nGenerating %s FV" %self.UiFvName)\r
 \r
         self.__InitializeInf__(BaseAddress, BlockSize, BlockNum, ErasePloarity, VtfDict)\r
         #\r
@@ -115,12 +133,12 @@ class FV (FvClassObject):
         #\r
         FvFileObj = open ( FvOutputFile,'r+b')\r
 \r
-        GenFdsGlobalVariable.InfLogger( "\nGenerate %s FV Successfully" %self.UiFvName)\r
+        GenFdsGlobalVariable.VerboseLogger( "\nGenerate %s FV Successfully" %self.UiFvName)\r
         GenFdsGlobalVariable.SharpCounter = 0\r
 \r
         Buffer.write(FvFileObj.read())\r
         FvFileObj.close()\r
-        GenFds.FvBinDict[self.UiFvName.upper()] = FvOutputFile\r
+        GenFds.ImageBinDict[self.UiFvName.upper() + 'fv'] = FvOutputFile\r
         return FvOutputFile\r
 \r
     ## __InitializeInf__()\r
index 3e332df..e2804fa 100644 (file)
@@ -309,8 +309,8 @@ def myOptionParser():
 #
 class GenFds :
     FdfParsef = None
-    # FvName in FDF, FvBinFile name
-    FvBinDict = {}
+    # FvName, FdName, CapName in FDF, Image file name
+    ImageBinDict = {}
     OnlyGenerateThisFd = None
     OnlyGenerateThisFv = None
 
@@ -324,17 +324,17 @@ class GenFds :
     def GenFd (OutputDir, FdfParser, WorkSpace, ArchList):
         GenFdsGlobalVariable.SetDir ('', FdfParser, WorkSpace, ArchList)
 
-        GenFdsGlobalVariable.VerboseLogger("   Gen Fd  !")
+        GenFdsGlobalVariable.VerboseLogger(" Generate all Fd images and their required FV and Capsule images!")
         if GenFds.OnlyGenerateThisFd != None and GenFds.OnlyGenerateThisFd.upper() in GenFdsGlobalVariable.FdfParser.Profile.FdDict.keys():
             FdObj = GenFdsGlobalVariable.FdfParser.Profile.FdDict.get(GenFds.OnlyGenerateThisFd.upper())
             if FdObj != None:
-                FdObj.GenFd(GenFds.FvBinDict)
-        elif GenFds.OnlyGenerateThisFv == None:
+                FdObj.GenFd()
+        elif GenFds.OnlyGenerateThisFd == None:
             for FdName in GenFdsGlobalVariable.FdfParser.Profile.FdDict.keys():
                 FdObj = GenFdsGlobalVariable.FdfParser.Profile.FdDict[FdName]
-                FdObj.GenFd(GenFds.FvBinDict)
+                FdObj.GenFd()
 
-        GenFdsGlobalVariable.VerboseLogger(" Gen FV ! ")
+        GenFdsGlobalVariable.VerboseLogger("\n Generate other FV images! ")
         if GenFds.OnlyGenerateThisFv != None and GenFds.OnlyGenerateThisFv.upper() in GenFdsGlobalVariable.FdfParser.Profile.FvDict.keys():
             FvObj = GenFdsGlobalVariable.FdfParser.Profile.FvDict.get(GenFds.OnlyGenerateThisFv.upper())
             if FvObj != None:
@@ -343,7 +343,7 @@ class GenFds :
                 FvObj.AddToBuffer(Buffer, None, GenFds.GetFvBlockSize(FvObj))
                 Buffer.close()
                 return
-        elif GenFds.OnlyGenerateThisFd == None:
+        elif GenFds.OnlyGenerateThisFv == None:
             for FvName in GenFdsGlobalVariable.FdfParser.Profile.FvDict.keys():
                 Buffer = StringIO.StringIO('')
                 FvObj = GenFdsGlobalVariable.FdfParser.Profile.FvDict[FvName]
@@ -352,12 +352,14 @@ class GenFds :
                 Buffer.close()
 
         if GenFds.OnlyGenerateThisFv == None and GenFds.OnlyGenerateThisFd == None:
-            GenFdsGlobalVariable.VerboseLogger(" Gen Capsule !")
-            for CapsuleObj in GenFdsGlobalVariable.FdfParser.Profile.CapsuleList:
-                CapsuleObj.GenCapsule()
-                
+            if GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict != {}:
+                GenFdsGlobalVariable.VerboseLogger("\n Generate other Capsule images!")
+                for CapsuleName in GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict.keys():
+                    CapsuleObj = GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict[CapsuleName]
+                    CapsuleObj.GenCapsule()
+
             if GenFdsGlobalVariable.FdfParser.Profile.OptRomDict != {}:
-                GenFdsGlobalVariable.VerboseLogger(" Gen Option ROM !")
+                GenFdsGlobalVariable.VerboseLogger("\n Generate all Option ROM!")
                 for DriverName in GenFdsGlobalVariable.FdfParser.Profile.OptRomDict.keys():
                     OptRomObj = GenFdsGlobalVariable.FdfParser.Profile.OptRomDict[DriverName]
                     OptRomObj.AddToBuffer(None)
index ed16c6f..99f1ac3 100644 (file)
@@ -20,10 +20,10 @@ from GenFdsGlobalVariable import GenFdsGlobalVariable
 import StringIO\r
 from CommonDataClass.FdfClass import RegionClassObject\r
 import os\r
+from stat import *\r
 from Common import EdkLogger\r
 from Common.BuildToolError import *\r
 \r
-\r
 ## generate Region\r
 #\r
 #\r
@@ -52,9 +52,9 @@ class Region(RegionClassObject):
     #   @retval string      Generated FV file path\r
     #\r
 \r
-    def AddToBuffer(self, Buffer, BaseAddress, BlockSizeList, ErasePolarity, FvBinDict, vtfDict = None, MacroDict = {}):\r
+    def AddToBuffer(self, Buffer, BaseAddress, BlockSizeList, ErasePolarity, ImageBinDict, vtfDict = None, MacroDict = {}):\r
         Size = self.Size\r
-        GenFdsGlobalVariable.InfLogger('Generate Region at Offset 0x%X' % self.Offset)\r
+        GenFdsGlobalVariable.InfLogger('\nGenerate Region at Offset 0x%X' % self.Offset)\r
         GenFdsGlobalVariable.InfLogger("   Region Size = 0x%X" %Size)\r
         GenFdsGlobalVariable.SharpCounter = 0\r
 \r
@@ -62,15 +62,14 @@ class Region(RegionClassObject):
             #\r
             # Get Fv from FvDict\r
             #\r
-            FvBuffer = StringIO.StringIO('')\r
             RegionBlockSize = self.BlockSizeOfRegion(BlockSizeList)\r
             RegionBlockNum = self.BlockNumOfRegion(RegionBlockSize)\r
 \r
             self.FvAddress = int(BaseAddress, 16) + self.Offset\r
-            FvBaseAddress = '0x%X' %self.FvAddress\r
-\r
+            FvBaseAddress  = '0x%X' %self.FvAddress\r
+            FvOffset       = 0\r
             for RegionData in self.RegionDataList:\r
-\r
+                FileName = None\r
                 if RegionData.endswith(".fv"):\r
                     RegionData = GenFdsGlobalVariable.MacroExtend(RegionData, MacroDict)\r
                     GenFdsGlobalVariable.InfLogger('   Region FV File Name = .fv : %s'%RegionData)\r
@@ -79,83 +78,165 @@ class Region(RegionClassObject):
                     if not os.path.exists(RegionData):\r
                         EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=RegionData)\r
 \r
-                    BinFile = open (RegionData, 'r+b')\r
-                    FvBuffer.write(BinFile.read())\r
-                    if FvBuffer.len > Size:\r
+                    FileName = RegionData\r
+                elif RegionData.upper() + 'fv' in ImageBinDict.keys():\r
+                    GenFdsGlobalVariable.InfLogger('   Region Name = FV')\r
+                    FileName = ImageBinDict[RegionData.upper() + 'fv']\r
+                else:\r
+                    #\r
+                    # Generate FvImage.\r
+                    #\r
+                    FvObj = None\r
+                    if RegionData.upper() in GenFdsGlobalVariable.FdfParser.Profile.FvDict.keys():\r
+                        FvObj = GenFdsGlobalVariable.FdfParser.Profile.FvDict.get(RegionData.upper())\r
+\r
+                    if FvObj != None :\r
+                        GenFdsGlobalVariable.InfLogger('   Region Name = FV')\r
+                        #\r
+                        # Call GenFv tool\r
+                        #\r
+                        BlockSize = RegionBlockSize\r
+                        BlockNum = RegionBlockNum\r
+                        if FvObj.BlockSizeList != []:\r
+                            if FvObj.BlockSizeList[0][0] != None:\r
+                                BlockSize = FvObj.BlockSizeList[0][0]\r
+                            if FvObj.BlockSizeList[0][1] != None:\r
+                                BlockNum = FvObj.BlockSizeList[0][1]\r
+                        self.FvAddress = self.FvAddress + FvOffset\r
+                        FvAlignValue = self.GetFvAlignValue(FvObj.FvAlignment)\r
+                        if self.FvAddress % FvAlignValue != 0:\r
+                            EdkLogger.error("GenFds", GENFDS_ERROR,\r
+                                            "FV (%s) is NOT %s Aligned!" % (FvObj.UiFvName, FvObj.FvAlignment))\r
+                        FvBuffer = StringIO.StringIO('')\r
+                        FvBaseAddress = '0x%X' %self.FvAddress\r
+                        FvObj.AddToBuffer(FvBuffer, FvBaseAddress, BlockSize, BlockNum, ErasePolarity, vtfDict)\r
+                        if FvBuffer.len > Size:\r
+                            FvBuffer.close()\r
+                            EdkLogger.error("GenFds", GENFDS_ERROR,\r
+                                            "Size of FV (%s) is larger than Region Size 0x%X specified." % (RegionData, Size))\r
+                        #\r
+                        # Put the generated image into FD buffer.\r
+                        #\r
+                        Buffer.write(FvBuffer.getvalue())\r
+                        FvBuffer.close()\r
+                        FvOffset = FvOffset + FvBuffer.len\r
+                        Size = Size - FvBuffer.len\r
+                        continue\r
+                    else:\r
+                        EdkLogger.error("GenFds", GENFDS_ERROR, "FV (%s) is NOT described in FDF file!" % (RegionData))\r
+                #\r
+                # Add the exist Fv image into FD buffer\r
+                #\r
+                if FileName != None:\r
+                    FileLength = os.stat(FileName)[ST_SIZE]\r
+                    if FileLength > Size:\r
                         EdkLogger.error("GenFds", GENFDS_ERROR,\r
                                         "Size of FV File (%s) is larger than Region Size 0x%X specified." \\r
                                         % (RegionData, Size))\r
-                    break\r
-\r
-                if RegionData.upper() in FvBinDict.keys():\r
-                    continue\r
+                    BinFile = open (FileName, 'r+b')\r
+                    Buffer.write(BinFile.read())\r
+                    BinFile.close()\r
+                    Size = Size - FileLength\r
+            #\r
+            # Pad the left buffer\r
+            #\r
+            if Size > 0:\r
+                if (ErasePolarity == '1') :\r
+                    PadData = 0xFF\r
+                else :\r
+                    PadData = 0\r
+                for i in range(0, Size):\r
+                    Buffer.write(pack('B', PadData))\r
 \r
-                FvObj = None\r
-                if RegionData.upper() in GenFdsGlobalVariable.FdfParser.Profile.FvDict.keys():\r
-                    FvObj = GenFdsGlobalVariable.FdfParser.Profile.FvDict.get(RegionData.upper())\r
+        if self.RegionType == 'CAPSULE':\r
+            #\r
+            # Get Capsule from Capsule Dict\r
+            #\r
+            for RegionData in self.RegionDataList:\r
+                if RegionData.endswith(".cap"):\r
+                    RegionData = GenFdsGlobalVariable.MacroExtend(RegionData, MacroDict)\r
+                    GenFdsGlobalVariable.InfLogger('   Region CAPSULE Image Name = .cap : %s'%RegionData)\r
+                    if RegionData[1] != ':' :\r
+                        RegionData = os.path.join (GenFdsGlobalVariable.WorkSpaceDir, RegionData)\r
+                    if not os.path.exists(RegionData):\r
+                        EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=RegionData)\r
 \r
-                if FvObj != None :\r
-                    GenFdsGlobalVariable.InfLogger('   Region Name = FV')\r
+                    FileName = RegionData\r
+                elif RegionData.upper() + 'cap' in ImageBinDict.keys():\r
+                    GenFdsGlobalVariable.InfLogger('   Region Name = CAPSULE')\r
+                    FileName = ImageBinDict[RegionData.upper() + 'cap']\r
+                else:\r
                     #\r
-                    # Call GenFv tool\r
+                    # Generate Capsule image and Put it into FD buffer\r
                     #\r
-                    BlockSize = RegionBlockSize\r
-                    BlockNum = RegionBlockNum\r
-                    if FvObj.BlockSizeList != []:\r
-                        if FvObj.BlockSizeList[0][0] != None:\r
-                            BlockSize = FvObj.BlockSizeList[0][0]\r
-                        if FvObj.BlockSizeList[0][1] != None:\r
-                            BlockNum = FvObj.BlockSizeList[0][1]\r
-                    self.FvAddress = self.FvAddress + FvBuffer.len\r
-                    FvAlignValue = self.GetFvAlignValue(FvObj.FvAlignment)\r
-                    if self.FvAddress % FvAlignValue != 0:\r
-                        EdkLogger.error("GenFds", GENFDS_ERROR,\r
-                                        "FV (%s) is NOT %s Aligned!" % (FvObj.UiFvName, FvObj.FvAlignment))\r
-                    FvBaseAddress = '0x%X' %self.FvAddress\r
-                    FileName = FvObj.AddToBuffer(FvBuffer, FvBaseAddress, BlockSize, BlockNum, ErasePolarity, vtfDict)\r
-\r
-                    if FvBuffer.len > Size:\r
-                        EdkLogger.error("GenFds", GENFDS_ERROR,\r
-                                        "Size of FV (%s) is larger than Region Size 0x%X specified." % (RegionData, Size))\r
-                else:\r
-                    EdkLogger.error("GenFds", GENFDS_ERROR, "FV (%s) is NOT described in FDF file!" % (RegionData))\r
+                    CapsuleObj = None\r
+                    if RegionData.upper() in GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict.keys():\r
+                        CapsuleObj = GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict[RegionData.upper()]\r
 \r
+                    if CapsuleObj != None :\r
+                        CapsuleObj.CapsuleName = RegionData.upper()\r
+                        GenFdsGlobalVariable.InfLogger('   Region Name = CAPSULE')\r
+                        #\r
+                        # Call GenFv tool to generate Capsule Image\r
+                        #\r
+                        FileName = CapsuleObj.GenCapsule()\r
+                        CapsuleObj.CapsuleName = None\r
+                    else:\r
+                        EdkLogger.error("GenFds", GENFDS_ERROR, "Capsule (%s) is NOT described in FDF file!" % (RegionData))\r
 \r
-            if FvBuffer.len > 0:\r
-                Buffer.write(FvBuffer.getvalue())\r
-            else:\r
-                BinFile = open (FileName, 'rb')\r
+                #\r
+                # Add the capsule image into FD buffer\r
+                #\r
+                FileLength = os.stat(FileName)[ST_SIZE]\r
+                if FileLength > Size:\r
+                    EdkLogger.error("GenFds", GENFDS_ERROR,\r
+                                    "Size 0x%X of Capsule File (%s) is larger than Region Size 0x%X specified." \\r
+                                    % (FileLength, RegionData, Size))\r
+                BinFile = open (FileName, 'r+b')\r
                 Buffer.write(BinFile.read())\r
-\r
-            FvBuffer.close()\r
+                BinFile.close()\r
+                Size = Size - FileLength\r
+            #\r
+            # Pad the left buffer\r
+            #\r
+            if Size > 0:\r
+                if (ErasePolarity == '1') :\r
+                    PadData = 0xFF\r
+                else :\r
+                    PadData = 0\r
+                for i in range(0, Size):\r
+                    Buffer.write(pack('B', PadData))\r
 \r
         if self.RegionType == 'FILE':\r
-            FvBuffer = StringIO.StringIO('')\r
             for RegionData in self.RegionDataList:\r
                 RegionData = GenFdsGlobalVariable.MacroExtend(RegionData, MacroDict)\r
-                GenFdsGlobalVariable.InfLogger('   Region File Name = FILE: %s'%RegionData)\r
                 if RegionData[1] != ':' :\r
                     RegionData = os.path.join (GenFdsGlobalVariable.WorkSpaceDir, RegionData)\r
                 if not os.path.exists(RegionData):\r
                     EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=RegionData)\r
-\r
-                BinFile = open (RegionData, 'r+b')\r
-                FvBuffer.write(BinFile.read())\r
-                if FvBuffer.len > Size :\r
+                #\r
+                # Add the file image into FD buffer\r
+                #\r
+                FileLength = os.stat(RegionData)[ST_SIZE]\r
+                if FileLength > Size:\r
                     EdkLogger.error("GenFds", GENFDS_ERROR,\r
-                                    "Size of File (%s) large than Region Size " % RegionData)\r
-\r
+                                    "Size of File (%s) is larger than Region Size 0x%X specified." \\r
+                                    % (RegionData, Size))\r
+                GenFdsGlobalVariable.InfLogger('   Region File Name = %s'%RegionData)\r
+                BinFile = open (RegionData, 'r+b')\r
+                Buffer.write(BinFile.read())\r
+                BinFile.close()\r
+                Size = Size - FileLength\r
             #\r
-            # If File contents less than region size, append "0xff" after it\r
+            # Pad the left buffer\r
             #\r
-            if FvBuffer.len < Size:\r
-                for index in range(0, (Size-FvBuffer.len)):\r
-                    if (ErasePolarity == '1'):\r
-                        FvBuffer.write(pack('B', int('0xFF', 16)))\r
-                    else:\r
-                        FvBuffer.write(pack('B', int('0x00', 16)))\r
-            Buffer.write(FvBuffer.getvalue())\r
-            FvBuffer.close()\r
+            if Size > 0:\r
+                if (ErasePolarity == '1') :\r
+                    PadData = 0xFF\r
+                else :\r
+                    PadData = 0\r
+                for i in range(0, Size):\r
+                    Buffer.write(pack('B', PadData))\r
 \r
         if self.RegionType == 'DATA' :\r
             GenFdsGlobalVariable.InfLogger('   Region Name = DATA')\r
@@ -168,12 +249,16 @@ class Region(RegionClassObject):
                 else:\r
                     for item in Data :\r
                         Buffer.write(pack('B', int(item, 16)))\r
-            if DataSize < Size:\r
-                if (ErasePolarity == '1'):\r
+                Size = Size - DataSize\r
+            #\r
+            # Pad the left buffer\r
+            #\r
+            if Size > 0:\r
+                if (ErasePolarity == '1') :\r
                     PadData = 0xFF\r
-                else:\r
+                else :\r
                     PadData = 0\r
-                for i in range(Size - DataSize):\r
+                for i in range(0, Size):\r
                     Buffer.write(pack('B', PadData))\r
 \r
         if self.RegionType == None:\r