]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/GenFds/Region.py
NetworkPkg: Fix protocol handler service in HttpDxe.
[mirror_edk2.git] / BaseTools / Source / Python / GenFds / Region.py
index 09971b3cd0084de886d0323640e8605d96ce7b8b..945c5489fdfede83b46ce4f5fadc15f91b7629f0 100644 (file)
@@ -1,7 +1,7 @@
 ## @file\r
 # process FD Region generation\r
 #\r
-#  Copyright (c) 2007, Intel Corporation. All rights reserved.<BR>\r
+#  Copyright (c) 2007 - 2015, Intel Corporation. All rights reserved.<BR>\r
 #\r
 #  This program and the accompanying materials\r
 #  are licensed and made available under the terms and conditions of the BSD License\r
 from struct import *\r
 from GenFdsGlobalVariable import GenFdsGlobalVariable\r
 import StringIO\r
+import string\r
 from CommonDataClass.FdfClass import RegionClassObject\r
-import os\r
+import Common.LongFilePathOs as os\r
 from stat import *\r
 from Common import EdkLogger\r
 from Common.BuildToolError import *\r
+from Common.LongFilePathSupport import OpenLongFilePath as open\r
+from Common.MultipleWorkspace import MultipleWorkspace as mws\r
 \r
 ## generate Region\r
 #\r
@@ -37,6 +40,25 @@ class Region(RegionClassObject):
         RegionClassObject.__init__(self)\r
 \r
 \r
+    ## PadBuffer()\r
+    #\r
+    #   Add padding bytes to the Buffer\r
+    #\r
+    #   @param Buffer         The buffer the generated region data will be put\r
+    #                         in\r
+    #   @param ErasePolarity  Flash erase polarity\r
+    #   @param Size           Number of padding bytes requested\r
+    #\r
+\r
+    def PadBuffer(self, Buffer, ErasePolarity, Size):\r
+        if Size > 0:\r
+            if (ErasePolarity == '1') :\r
+                PadByte = pack('B', 0xFF)\r
+            else:\r
+                PadByte = pack('B', 0)\r
+            PadData = ''.join(PadByte for i in xrange(0, Size))\r
+            Buffer.write(PadData)\r
+\r
     ## AddToBuffer()\r
     #\r
     #   Add region data to the Buffer\r
@@ -52,29 +74,26 @@ class Region(RegionClassObject):
     #   @retval string      Generated FV file path\r
     #\r
 \r
-    def AddToBuffer(self, Buffer, BaseAddress, BlockSizeList, ErasePolarity, ImageBinDict, vtfDict = None, MacroDict = {}):\r
+    def AddToBuffer(self, Buffer, BaseAddress, BlockSizeList, ErasePolarity, ImageBinDict, vtfDict=None, MacroDict={}):\r
         Size = self.Size\r
         GenFdsGlobalVariable.InfLogger('\nGenerate Region at Offset 0x%X' % self.Offset)\r
-        GenFdsGlobalVariable.InfLogger("   Region Size = 0x%X" %Size)\r
+        GenFdsGlobalVariable.InfLogger("   Region Size = 0x%X" % Size)\r
         GenFdsGlobalVariable.SharpCounter = 0\r
 \r
         if self.RegionType == 'FV':\r
             #\r
             # Get Fv from FvDict\r
             #\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
-            FvOffset       = 0\r
+            FvBaseAddress = '0x%X' % self.FvAddress\r
+            FvOffset = 0\r
             for RegionData in self.RegionDataList:\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
+                    GenFdsGlobalVariable.InfLogger('   Region FV File Name = .fv : %s' % RegionData)\r
                     if RegionData[1] != ':' :\r
-                        RegionData = os.path.join (GenFdsGlobalVariable.WorkSpaceDir, RegionData)\r
+                        RegionData = mws.join (GenFdsGlobalVariable.WorkSpaceDir, RegionData)\r
                     if not os.path.exists(RegionData):\r
                         EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=RegionData)\r
 \r
@@ -95,20 +114,16 @@ class Region(RegionClassObject):
                         #\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.BlockInfoOfRegion(BlockSizeList, FvObj)\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
+                        FvBaseAddress = '0x%X' % self.FvAddress\r
+                        BlockSize = None\r
+                        BlockNum = None\r
                         FvObj.AddToBuffer(FvBuffer, FvBaseAddress, BlockSize, BlockNum, ErasePolarity, vtfDict)\r
                         if FvBuffer.len > Size:\r
                             FvBuffer.close()\r
@@ -133,20 +148,14 @@ class Region(RegionClassObject):
                         EdkLogger.error("GenFds", GENFDS_ERROR,\r
                                         "Size of FV File (%s) is larger than Region Size 0x%X specified." \\r
                                         % (RegionData, Size))\r
-                    BinFile = open (FileName, 'r+b')\r
+                    BinFile = open(FileName, 'rb')\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
+            self.PadBuffer(Buffer, ErasePolarity, Size)\r
 \r
         if self.RegionType == 'CAPSULE':\r
             #\r
@@ -155,9 +164,9 @@ class Region(RegionClassObject):
             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
+                    GenFdsGlobalVariable.InfLogger('   Region CAPSULE Image Name = .cap : %s' % RegionData)\r
                     if RegionData[1] != ':' :\r
-                        RegionData = os.path.join (GenFdsGlobalVariable.WorkSpaceDir, RegionData)\r
+                        RegionData = mws.join (GenFdsGlobalVariable.WorkSpaceDir, RegionData)\r
                     if not os.path.exists(RegionData):\r
                         EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=RegionData)\r
 \r
@@ -192,28 +201,29 @@ class Region(RegionClassObject):
                     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
+                BinFile = open(FileName, 'rb')\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
+            self.PadBuffer(Buffer, ErasePolarity, Size)\r
 \r
-        if self.RegionType == 'FILE':\r
+        if self.RegionType in ('FILE', 'INF'):\r
             for RegionData in self.RegionDataList:\r
-                RegionData = GenFdsGlobalVariable.MacroExtend(RegionData, MacroDict)\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
+                if self.RegionType == 'INF':\r
+                    RegionData.__InfParse__(None)\r
+                    if len(RegionData.BinFileList) != 1:\r
+                        EdkLogger.error('GenFds', GENFDS_ERROR, 'INF in FD region can only contain one binary: %s' % RegionData)\r
+                    File = RegionData.BinFileList[0]\r
+                    RegionData = RegionData.PatchEfiFile(File.Path, File.Type)\r
+                else:\r
+                    RegionData = GenFdsGlobalVariable.MacroExtend(RegionData, MacroDict)\r
+                    if RegionData[1] != ':' :\r
+                        RegionData = mws.join (GenFdsGlobalVariable.WorkSpaceDir, RegionData)\r
+                    if not os.path.exists(RegionData):\r
+                        EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=RegionData)\r
                 #\r
                 # Add the file image into FD buffer\r
                 #\r
@@ -222,21 +232,15 @@ class Region(RegionClassObject):
                     EdkLogger.error("GenFds", GENFDS_ERROR,\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
+                GenFdsGlobalVariable.InfLogger('   Region File Name = %s' % RegionData)\r
+                BinFile = open(RegionData, 'rb')\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
+            self.PadBuffer(Buffer, ErasePolarity, Size)\r
 \r
         if self.RegionType == 'DATA' :\r
             GenFdsGlobalVariable.InfLogger('   Region Name = DATA')\r
@@ -253,22 +257,11 @@ class Region(RegionClassObject):
             #\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
+            self.PadBuffer(Buffer, ErasePolarity, Size)\r
 \r
         if self.RegionType == None:\r
             GenFdsGlobalVariable.InfLogger('   Region Name = None')\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
+            self.PadBuffer(Buffer, ErasePolarity, Size)\r
 \r
     def GetFvAlignValue(self, Str):\r
         AlignValue = 1\r
@@ -278,48 +271,84 @@ class Region(RegionClassObject):
             Granu = 1024\r
             Str = Str[:-1]\r
         elif Str.endswith('M'):\r
-            Granu = 1024*1024\r
+            Granu = 1024 * 1024\r
             Str = Str[:-1]\r
         elif Str.endswith('G'):\r
-            Granu = 1024*1024*1024\r
+            Granu = 1024 * 1024 * 1024\r
             Str = Str[:-1]\r
         else:\r
             pass\r
 \r
-        AlignValue = int(Str)*Granu\r
+        AlignValue = int(Str) * Granu\r
         return AlignValue\r
+\r
     ## BlockSizeOfRegion()\r
     #\r
     #   @param  BlockSizeList        List of block information\r
-    #   @retval int                  Block size of region\r
+    #   @param  FvObj                The object for FV\r
     #\r
-    def BlockSizeOfRegion(self, BlockSizeList):\r
-        Offset = 0x00\r
-        BlockSize = 0\r
-        for item in BlockSizeList:\r
-            Offset = Offset + item[0]  * item[1]\r
-            GenFdsGlobalVariable.VerboseLogger ("Offset = 0x%X" %Offset)\r
-            GenFdsGlobalVariable.VerboseLogger ("self.Offset 0x%X" %self.Offset)\r
+    def BlockInfoOfRegion(self, BlockSizeList, FvObj):\r
+        Start = 0\r
+        End = 0\r
+        RemindingSize = self.Size\r
+        ExpectedList = []\r
+        for (BlockSize, BlockNum, pcd) in BlockSizeList:\r
+            End = Start + BlockSize * BlockNum\r
+            # region not started yet\r
+            if self.Offset >= End:\r
+                Start = End\r
+                continue\r
+            # region located in current blocks \r
+            else:\r
+                # region ended within current blocks\r
+                if self.Offset + self.Size <= End:\r
+                    ExpectedList.append((BlockSize, (RemindingSize + BlockSize - 1) / BlockSize))\r
+                    break\r
+                # region not ended yet\r
+                else:\r
+                    # region not started in middle of current blocks\r
+                    if self.Offset <= Start:\r
+                        UsedBlockNum = BlockNum\r
+                    # region started in middle of current blocks\r
+                    else:\r
+                        UsedBlockNum = (End - self.Offset) / BlockSize\r
+                    Start = End\r
+                    ExpectedList.append((BlockSize, UsedBlockNum))\r
+                    RemindingSize -= BlockSize * UsedBlockNum\r
 \r
-            if self.Offset < Offset :\r
-                if Offset - self.Offset < self.Size:\r
-                    EdkLogger.error("GenFds", GENFDS_ERROR,\r
-                                    "Region at Offset 0x%X can NOT fit into Block array with BlockSize %X" \\r
-                                    % (self.Offset, item[0]))\r
-                BlockSize = item[0]\r
-                GenFdsGlobalVariable.VerboseLogger ("BlockSize = %X" %BlockSize)\r
-                return BlockSize\r
-        return BlockSize\r
+        if FvObj.BlockSizeList == []:\r
+            FvObj.BlockSizeList = ExpectedList\r
+        else:\r
+            # first check whether FvObj.BlockSizeList items have only "BlockSize" or "NumBlocks",\r
+            # if so, use ExpectedList\r
+            for Item in FvObj.BlockSizeList:\r
+                if Item[0] == None or Item[1] == None:\r
+                    FvObj.BlockSizeList = ExpectedList\r
+                    break\r
+            # make sure region size is no smaller than the summed block size in FV\r
+            Sum = 0\r
+            for Item in FvObj.BlockSizeList:\r
+                Sum += Item[0] * Item[1]\r
+            if self.Size < Sum:\r
+                EdkLogger.error("GenFds", GENFDS_ERROR, "Total Size of FV %s 0x%x is larger than Region Size 0x%x "\r
+                                % (FvObj.UiFvName, Sum, self.Size))\r
+            # check whether the BlockStatements in FV section is appropriate\r
+            ExpectedListData = ''\r
+            for Item in ExpectedList:\r
+                ExpectedListData += "BlockSize = 0x%x\n\tNumBlocks = 0x%x\n\t" % Item\r
+            Index = 0\r
+            for Item in FvObj.BlockSizeList:\r
+                if Item[0] != ExpectedList[Index][0]:\r
+                    EdkLogger.error("GenFds", GENFDS_ERROR, "BlockStatements of FV %s are not align with FD's, suggested FV BlockStatement"\r
+                                    % FvObj.UiFvName, ExtraData=ExpectedListData)\r
+                elif Item[1] != ExpectedList[Index][1]:\r
+                    if (Item[1] < ExpectedList[Index][1]) and (Index == len(FvObj.BlockSizeList) - 1):\r
+                        break;\r
+                    else:\r
+                        EdkLogger.error("GenFds", GENFDS_ERROR, "BlockStatements of FV %s are not align with FD's, suggested FV BlockStatement"\r
+                                        % FvObj.UiFvName, ExtraData=ExpectedListData)\r
+                else:\r
+                    Index += 1\r
 \r
-    ## BlockNumOfRegion()\r
-    #\r
-    #   @param  BlockSize            block size of region\r
-    #   @retval int                  Block number of region\r
-    #\r
-    def BlockNumOfRegion (self, BlockSize):\r
-        if BlockSize == 0 :\r
-            EdkLogger.error("GenFds", GENFDS_ERROR, "Region: %s is not in the FD address scope!" % self.Offset)\r
-        BlockNum = self.Size / BlockSize\r
-        GenFdsGlobalVariable.VerboseLogger ("BlockNum = 0x%X" %BlockNum)\r
-        return BlockNum\r
+            \r
 \r