]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/build/BuildReport.py
BaseTools: generate hash value in build report for each output EFI image
[mirror_edk2.git] / BaseTools / Source / Python / build / BuildReport.py
index 722d8b7f458cbec76d46412f57eda0944d191119..27a5d9736efa32d5f1b7645adb38f5facb356ade 100644 (file)
@@ -4,7 +4,7 @@
 # This module contains the functionality to generate build report after\r
 # build all target completes successfully.\r
 #\r
-# Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>\r
+# Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.<BR>\r
 # This program and the accompanying materials\r
 # are licensed and made available under the terms and conditions of the BSD License\r
 # which accompanies this distribution.  The full text of the license may be found at\r
@@ -24,6 +24,9 @@ import traceback
 import sys\r
 import time\r
 import struct\r
+import hashlib\r
+import subprocess\r
+import threading\r
 from datetime import datetime\r
 from StringIO import StringIO\r
 from Common import EdkLogger\r
@@ -33,6 +36,7 @@ from Common.Misc import GuidStructureStringToGuidString
 from Common.InfClassObject import gComponentType2ModuleType\r
 from Common.BuildToolError import FILE_WRITE_FAILURE\r
 from Common.BuildToolError import CODE_ERROR\r
+from Common.BuildToolError import COMMAND_FAILURE\r
 from Common.DataType import TAB_LINE_BREAK\r
 from Common.DataType import TAB_DEPEX\r
 from Common.DataType import TAB_SLASH\r
@@ -42,6 +46,7 @@ from Common.DataType import TAB_BRG_LIBRARY
 from Common.DataType import TAB_BACK_SLASH\r
 from Common.LongFilePathSupport import OpenLongFilePath as open\r
 from Common.MultipleWorkspace import MultipleWorkspace as mws\r
+import Common.GlobalData as GlobalData\r
 \r
 ## Pattern to extract contents in EDK DXS files\r
 gDxsDependencyPattern = re.compile(r"DEPENDENCY_START(.+)DEPENDENCY_END", re.DOTALL)\r
@@ -527,6 +532,7 @@ class ModuleReport(object):
         self.FileGuid = M.Guid\r
         self.Size = 0\r
         self.BuildTimeStamp = None\r
+        self.Hash = 0\r
         self.DriverType = ""\r
         if not M.IsLibrary:\r
             ModuleType = M.ModuleType\r
@@ -598,12 +604,46 @@ class ModuleReport(object):
             except IOError:\r
                 EdkLogger.warn(None, "Fail to read report file", FwReportFileName)\r
 \r
+        if "HASH" in ReportType:\r
+            OutputDir = os.path.join(self._BuildDir, "OUTPUT")\r
+            DefaultEFIfile = os.path.join(OutputDir, self.ModuleName + ".efi")\r
+            if os.path.isfile(DefaultEFIfile):\r
+                Tempfile = os.path.join(OutputDir, self.ModuleName + "_hash.tmp")\r
+                # rebase the efi image since its base address may not zero\r
+                cmd = ["GenFw", "--rebase", str(0), "-o", Tempfile, DefaultEFIfile]\r
+                try:\r
+                    PopenObject = subprocess.Popen(' '.join(cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\r
+                except Exception, X:\r
+                    EdkLogger.error("GenFw", COMMAND_FAILURE, ExtraData="%s: %s" % (str(X), cmd[0]))\r
+                EndOfProcedure = threading.Event()\r
+                EndOfProcedure.clear()\r
+                if PopenObject.stderr:\r
+                    StdErrThread = threading.Thread(target=ReadMessage, args=(PopenObject.stderr, EdkLogger.quiet, EndOfProcedure))\r
+                    StdErrThread.setName("STDERR-Redirector")\r
+                    StdErrThread.setDaemon(False)\r
+                    StdErrThread.start()\r
+                # waiting for program exit\r
+                PopenObject.wait()\r
+                if PopenObject.stderr:\r
+                    StdErrThread.join()\r
+                if PopenObject.returncode != 0:\r
+                    EdkLogger.error("GenFw", COMMAND_FAILURE, "Failed to generate firmware hash image for %s" % (DefaultEFIfile))\r
+                if os.path.isfile(Tempfile):\r
+                    self.Hash = hashlib.sha1()\r
+                    buf = open(Tempfile, 'rb').read()\r
+                    if self.Hash.update(buf):\r
+                        self.Hash = self.Hash.update(buf)\r
+                    self.Hash = self.Hash.hexdigest()\r
+                    os.remove(Tempfile)\r
+\r
         FileWrite(File, "Module Summary")\r
         FileWrite(File, "Module Name:          %s" % self.ModuleName)\r
         FileWrite(File, "Module INF Path:      %s" % self.ModuleInfPath)\r
         FileWrite(File, "File GUID:            %s" % self.FileGuid)\r
         if self.Size:\r
             FileWrite(File, "Size:                 0x%X (%.2fK)" % (self.Size, self.Size / 1024.0))\r
+        if self.Hash:\r
+            FileWrite(File, "SHA1 HASH:            %s *%s" % (self.Hash, self.ModuleName + ".efi"))\r
         if self.BuildTimeStamp:\r
             FileWrite(File, "Build Time Stamp:     %s" % self.BuildTimeStamp)\r
         if self.DriverType:\r
@@ -638,6 +678,18 @@ class ModuleReport(object):
 \r
         FileWrite(File, gSectionEnd)\r
 \r
+def ReadMessage(From, To, ExitFlag):\r
+    while True:\r
+        # read one line a time\r
+        Line = From.readline()\r
+        # empty string means "end"\r
+        if Line != None and Line != "":\r
+            To(Line.rstrip())\r
+        else:\r
+            break\r
+        if ExitFlag.isSet():\r
+            break\r
+\r
 ##\r
 # Reports platform and module PCD information\r
 #\r
@@ -657,6 +709,8 @@ class PcdReport(object):
     #\r
     def __init__(self, Wa):\r
         self.AllPcds = {}\r
+        self.UnusedPcds = {}\r
+        self.ConditionalPcds = {}\r
         self.MaxLen = 0\r
         if Wa.FdfProfile:\r
             self.FdfPcdSet = Wa.FdfProfile.PcdDict\r
@@ -675,6 +729,63 @@ class PcdReport(object):
                     PcdList.append(Pcd)\r
                 if len(Pcd.TokenCName) > self.MaxLen:\r
                     self.MaxLen = len(Pcd.TokenCName)\r
+            #\r
+            # Collect the PCD defined in DSC/FDF file, but not used in module\r
+            #\r
+            UnusedPcdFullList = []\r
+            for item in Pa.Platform.Pcds:\r
+                Pcd = Pa.Platform.Pcds[item]\r
+                if not Pcd.Type:\r
+                    PcdTypeFlag = False\r
+                    for package in Pa.PackageList:\r
+                        for T in ["FixedAtBuild", "PatchableInModule", "FeatureFlag", "Dynamic", "DynamicEx"]:\r
+                            if (Pcd.TokenCName, Pcd.TokenSpaceGuidCName, T) in package.Pcds:\r
+                                Pcd.Type = T\r
+                                PcdTypeFlag = True\r
+                                if not Pcd.DatumType:\r
+                                    Pcd.DatumType = package.Pcds[(Pcd.TokenCName, Pcd.TokenSpaceGuidCName, T)].DatumType\r
+                                break\r
+                        if PcdTypeFlag:\r
+                            break\r
+                if not Pcd.DatumType:\r
+                    PcdType = Pcd.Type\r
+                    # Try to remove Hii and Vpd suffix\r
+                    if PcdType.startswith("DynamicEx"):\r
+                        PcdType = "DynamicEx"\r
+                    elif PcdType.startswith("Dynamic"):\r
+                        PcdType = "Dynamic"\r
+                    for package in Pa.PackageList:\r
+                        if (Pcd.TokenCName, Pcd.TokenSpaceGuidCName, PcdType) in package.Pcds:\r
+                            Pcd.DatumType = package.Pcds[(Pcd.TokenCName, Pcd.TokenSpaceGuidCName, PcdType)].DatumType\r
+                            break\r
+\r
+                PcdList = self.AllPcds.setdefault(Pcd.TokenSpaceGuidCName, {}).setdefault(Pcd.Type, [])\r
+                if Pcd not in PcdList and Pcd not in UnusedPcdFullList:\r
+                    UnusedPcdFullList.append(Pcd)\r
+                if len(Pcd.TokenCName) > self.MaxLen:\r
+                    self.MaxLen = len(Pcd.TokenCName)\r
+\r
+            if GlobalData.gConditionalPcds:\r
+                for PcdItem in GlobalData.gConditionalPcds:\r
+                    if '.' in PcdItem:\r
+                        (TokenSpaceGuidCName, TokenCName) = PcdItem.split('.')\r
+                        if (TokenCName, TokenSpaceGuidCName) in Pa.Platform.Pcds.keys():\r
+                            Pcd = Pa.Platform.Pcds[(TokenCName, TokenSpaceGuidCName)]\r
+                            PcdList = self.ConditionalPcds.setdefault(Pcd.TokenSpaceGuidCName, {}).setdefault(Pcd.Type, [])\r
+                            if Pcd not in PcdList:\r
+                                PcdList.append(Pcd)\r
+\r
+            UnusedPcdList = []\r
+            if UnusedPcdFullList:\r
+                for Pcd in UnusedPcdFullList:\r
+                    if Pcd.TokenSpaceGuidCName + '.' + Pcd.TokenCName in GlobalData.gConditionalPcds:\r
+                        continue\r
+                    UnusedPcdList.append(Pcd)\r
+\r
+            for Pcd in UnusedPcdList:\r
+                PcdList = self.UnusedPcds.setdefault(Pcd.TokenSpaceGuidCName, {}).setdefault(Pcd.Type, [])\r
+                if Pcd not in PcdList:\r
+                    PcdList.append(Pcd)\r
 \r
             for Module in Pa.Platform.Modules.values():\r
                 #\r
@@ -708,6 +819,13 @@ class PcdReport(object):
                 if DscDefaultValue:\r
                     self.DscPcdDefault[(TokenCName, TokenSpaceGuidCName)] = DscDefaultValue\r
 \r
+    def GenerateReport(self, File, ModulePcdSet):\r
+        if self.ConditionalPcds:\r
+            self.GenerateReportDetail(File, ModulePcdSet, 1)\r
+        if self.UnusedPcds:\r
+            self.GenerateReportDetail(File, ModulePcdSet, 2)\r
+        self.GenerateReportDetail(File, ModulePcdSet)\r
+\r
     ##\r
     # Generate report for PCD information\r
     #\r
@@ -718,38 +836,52 @@ class PcdReport(object):
     # @param File            The file object for report\r
     # @param ModulePcdSet    Set of all PCDs referenced by module or None for\r
     #                        platform PCD report\r
+    # @param ReportySubType  0 means platform/module PCD report, 1 means Conditional\r
+    #                        directives section report, 2 means Unused Pcds section report\r
     # @param DscOverridePcds Module DSC override PCDs set\r
     #\r
-    def GenerateReport(self, File, ModulePcdSet):\r
+    def GenerateReportDetail(self, File, ModulePcdSet, ReportSubType = 0):\r
+        PcdDict = self.AllPcds\r
+        if ReportSubType == 1:\r
+            PcdDict = self.ConditionalPcds\r
+        elif ReportSubType == 2:\r
+            PcdDict = self.UnusedPcds\r
+\r
         if ModulePcdSet == None:\r
-            #\r
-            # For platform global PCD section\r
-            #\r
             FileWrite(File, gSectionStart)\r
-            FileWrite(File, "Platform Configuration Database Report")\r
+            if ReportSubType == 1:\r
+                FileWrite(File, "Conditional Directives used by the build system")\r
+            elif ReportSubType == 2:\r
+                FileWrite(File, "PCDs not used by modules or in conditional directives")\r
+            else:\r
+                FileWrite(File, "Platform Configuration Database Report")\r
+\r
+            FileWrite(File, "  *B  - PCD override in the build option")\r
             FileWrite(File, "  *P  - Platform scoped PCD override in DSC file")\r
             FileWrite(File, "  *F  - Platform scoped PCD override in FDF file")\r
-            FileWrite(File, "  *M  - Module scoped PCD override")\r
+            if not ReportSubType:\r
+                FileWrite(File, "  *M  - Module scoped PCD override")\r
             FileWrite(File, gSectionSep)\r
         else:\r
-            #\r
-            # For module PCD sub-section\r
-            #\r
-            FileWrite(File, gSubSectionStart)\r
-            FileWrite(File, TAB_BRG_PCD)\r
-            FileWrite(File, gSubSectionSep)\r
+            if not ReportSubType:\r
+                #\r
+                # For module PCD sub-section\r
+                #\r
+                FileWrite(File, gSubSectionStart)\r
+                FileWrite(File, TAB_BRG_PCD)\r
+                FileWrite(File, gSubSectionSep)\r
 \r
-        for Key in self.AllPcds:\r
+        for Key in PcdDict:\r
             #\r
             # Group PCD by their token space GUID C Name\r
             #\r
             First = True\r
-            for Type in self.AllPcds[Key]:\r
+            for Type in PcdDict[Key]:\r
                 #\r
                 # Group PCD by their usage type\r
                 #\r
                 TypeName, DecType = gPcdTypeMap.get(Type, ("", Type))\r
-                for Pcd in self.AllPcds[Key][Type]:\r
+                for Pcd in PcdDict[Key][Type]:\r
                     #\r
                     # Get PCD default value and their override relationship\r
                     #\r
@@ -767,6 +899,15 @@ class PcdReport(object):
                         InfDefault, PcdValue = ModulePcdSet[Pcd.TokenCName, Pcd.TokenSpaceGuidCName, Type]\r
                         if InfDefault == "":\r
                             InfDefault = None\r
+\r
+                    BuildOptionMatch = False\r
+                    if GlobalData.BuildOptionPcd:\r
+                        for pcd in GlobalData.BuildOptionPcd:\r
+                            if (Pcd.TokenSpaceGuidCName, Pcd.TokenCName) == (pcd[0], pcd[1]):\r
+                                PcdValue = pcd[2]\r
+                                BuildOptionMatch = True\r
+                                break\r
+\r
                     if First:\r
                         if ModulePcdSet == None:\r
                             FileWrite(File, "")\r
@@ -812,7 +953,9 @@ class PcdReport(object):
                     #\r
                     # Report PCD item according to their override relationship\r
                     #\r
-                    if DecMatch and InfMatch:\r
+                    if BuildOptionMatch:\r
+                        FileWrite(File, ' *B %-*s: %6s %10s = %-22s' % (self.MaxLen, Pcd.TokenCName, TypeName, '(' + Pcd.DatumType + ')', PcdValue.strip()))\r
+                    elif DecMatch and InfMatch:\r
                         FileWrite(File, '    %-*s: %6s %10s = %-22s' % (self.MaxLen, Pcd.TokenCName, TypeName, '(' + Pcd.DatumType + ')', PcdValue.strip()))\r
                     else:\r
                         if DscMatch:\r
@@ -840,22 +983,24 @@ class PcdReport(object):
                         FileWrite(File, '    %*s = %s' % (self.MaxLen + 19, 'DEC DEFAULT', DecDefaultValue.strip()))\r
 \r
                     if ModulePcdSet == None:\r
-                        ModuleOverride = self.ModulePcdOverride.get((Pcd.TokenCName, Pcd.TokenSpaceGuidCName), {})\r
-                        for ModulePath in ModuleOverride:\r
-                            ModuleDefault = ModuleOverride[ModulePath]\r
-                            if Pcd.DatumType in ('UINT8', 'UINT16', 'UINT32', 'UINT64'):\r
-                                ModulePcdDefaultValueNumber = int(ModuleDefault.strip(), 0)\r
-                                Match = (ModulePcdDefaultValueNumber == PcdValueNumber)\r
-                            else:\r
-                                Match = (ModuleDefault.strip() == PcdValue.strip())\r
-                            if Match:\r
-                                continue\r
-                            FileWrite(File, ' *M %-*s = %s' % (self.MaxLen + 19, ModulePath, ModuleDefault.strip()))\r
+                        if not BuildOptionMatch:\r
+                            ModuleOverride = self.ModulePcdOverride.get((Pcd.TokenCName, Pcd.TokenSpaceGuidCName), {})\r
+                            for ModulePath in ModuleOverride:\r
+                                ModuleDefault = ModuleOverride[ModulePath]\r
+                                if Pcd.DatumType in ('UINT8', 'UINT16', 'UINT32', 'UINT64'):\r
+                                    ModulePcdDefaultValueNumber = int(ModuleDefault.strip(), 0)\r
+                                    Match = (ModulePcdDefaultValueNumber == PcdValueNumber)\r
+                                else:\r
+                                    Match = (ModuleDefault.strip() == PcdValue.strip())\r
+                                if Match:\r
+                                    continue\r
+                                FileWrite(File, ' *M %-*s = %s' % (self.MaxLen + 19, ModulePath, ModuleDefault.strip()))\r
 \r
         if ModulePcdSet == None:\r
             FileWrite(File, gSectionEnd)\r
         else:\r
-            FileWrite(File, gSubSectionEnd)\r
+            if not ReportSubType:\r
+                FileWrite(File, gSubSectionEnd)\r
 \r
 \r
 \r
@@ -1175,18 +1320,20 @@ class FdRegionReport(object):
     # @param Wa              Workspace context information\r
     #\r
     def _DiscoverNestedFvList(self, FvName, Wa):\r
-        for Ffs in Wa.FdfProfile.FvDict[FvName.upper()].FfsList:\r
-            for Section in Ffs.SectionList:\r
-                try:\r
-                    for FvSection in Section.SectionList:\r
-                        if FvSection.FvName in self.FvList:\r
-                            continue\r
-                        self._GuidsDb[Ffs.NameGuid.upper()] = FvSection.FvName\r
-                        self.FvList.append(FvSection.FvName)\r
-                        self.FvInfo[FvSection.FvName] = ("Nested FV", 0, 0)\r
-                        self._DiscoverNestedFvList(FvSection.FvName, Wa)\r
-                except AttributeError:\r
-                    pass\r
+        FvDictKey=FvName.upper()\r
+        if FvDictKey in Wa.FdfProfile.FvDict:\r
+            for Ffs in Wa.FdfProfile.FvDict[FvName.upper()].FfsList:\r
+                for Section in Ffs.SectionList:\r
+                    try:\r
+                        for FvSection in Section.SectionList:\r
+                            if FvSection.FvName in self.FvList:\r
+                                continue\r
+                            self._GuidsDb[Ffs.NameGuid.upper()] = FvSection.FvName\r
+                            self.FvList.append(FvSection.FvName)\r
+                            self.FvInfo[FvSection.FvName] = ("Nested FV", 0, 0)\r
+                            self._DiscoverNestedFvList(FvSection.FvName, Wa)\r
+                    except AttributeError:\r
+                        pass\r
 \r
     ##\r
     # Constructor function for class FdRegionReport\r
@@ -1264,27 +1411,29 @@ class FdRegionReport(object):
         # Collect the GUID map in the FV firmware volume\r
         #\r
         for FvName in self.FvList:\r
-            for Ffs in Wa.FdfProfile.FvDict[FvName.upper()].FfsList:\r
-                try:\r
-                    #\r
-                    # collect GUID map for binary EFI file in FDF file.\r
-                    #\r
-                    Guid = Ffs.NameGuid.upper()\r
-                    Match = gPcdGuidPattern.match(Ffs.NameGuid)\r
-                    if Match:\r
-                        PcdTokenspace = Match.group(1)\r
-                        PcdToken = Match.group(2)\r
-                        if (PcdToken, PcdTokenspace) in PlatformPcds:\r
-                            GuidValue = PlatformPcds[(PcdToken, PcdTokenspace)]\r
-                            Guid = GuidStructureByteArrayToGuidString(GuidValue).upper()\r
-                    for Section in Ffs.SectionList:\r
-                        try:\r
-                            ModuleSectFile = mws.join(Wa.WorkspaceDir, Section.SectFileName)\r
-                            self._GuidsDb[Guid] = ModuleSectFile\r
-                        except AttributeError:\r
-                            pass\r
-                except AttributeError:\r
-                    pass\r
+            FvDictKey=FvName.upper()\r
+            if FvDictKey in Wa.FdfProfile.FvDict:\r
+                for Ffs in Wa.FdfProfile.FvDict[FvName.upper()].FfsList:\r
+                    try:\r
+                        #\r
+                        # collect GUID map for binary EFI file in FDF file.\r
+                        #\r
+                        Guid = Ffs.NameGuid.upper()\r
+                        Match = gPcdGuidPattern.match(Ffs.NameGuid)\r
+                        if Match:\r
+                            PcdTokenspace = Match.group(1)\r
+                            PcdToken = Match.group(2)\r
+                            if (PcdToken, PcdTokenspace) in PlatformPcds:\r
+                                GuidValue = PlatformPcds[(PcdToken, PcdTokenspace)]\r
+                                Guid = GuidStructureByteArrayToGuidString(GuidValue).upper()\r
+                        for Section in Ffs.SectionList:\r
+                            try:\r
+                                ModuleSectFile = mws.join(Wa.WorkspaceDir, Section.SectFileName)\r
+                                self._GuidsDb[Guid] = ModuleSectFile\r
+                            except AttributeError:\r
+                                pass\r
+                    except AttributeError:\r
+                        pass\r
 \r
 \r
     ##\r
@@ -1389,11 +1538,11 @@ class FdReport(object):
         self.FdRegionList = [FdRegionReport(FdRegion, Wa) for FdRegion in Fd.RegionList]\r
         self.FvPath = os.path.join(Wa.BuildDir, "FV")\r
         self.VpdFilePath = os.path.join(self.FvPath, "%s.map" % Wa.Platform.VpdToolGuid)\r
-        VpdPcdToken = 'gEfiMdeModulePkgTokenSpaceGuid'\r
-        VpdPcdName = 'PcdVpdBaseAddress'\r
+        self.VPDBaseAddress = 0\r
+        self.VPDSize = 0\r
         self.VPDInfoList = []\r
         for index, FdRegion in enumerate(Fd.RegionList):\r
-            if (VpdPcdName, VpdPcdToken) == FdRegion.PcdOffset:\r
+            if str(FdRegion.RegionType) is 'FILE' and Wa.Platform.VpdToolGuid in str(FdRegion.RegionDataList):\r
                 self.VPDBaseAddress = self.FdRegionList[index].BaseAddress\r
                 self.VPDSize = self.FdRegionList[index].Size\r
                 break\r
@@ -1569,7 +1718,7 @@ class BuildReport(object):
                     if ReportTypeItem not in self.ReportType:\r
                         self.ReportType.append(ReportTypeItem)\r
             else:\r
-                self.ReportType = ["PCD", "LIBRARY", "BUILD_FLAGS", "DEPEX", "FLASH", "FIXED_ADDRESS"]\r
+                self.ReportType = ["PCD", "LIBRARY", "BUILD_FLAGS", "DEPEX", "HASH", "FLASH", "FIXED_ADDRESS"]\r
     ##\r
     # Adds platform report to the list\r
     #\r