]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/GenPatchPcdTable/GenPatchPcdTable.py
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / BaseTools / Source / Python / GenPatchPcdTable / GenPatchPcdTable.py
index 59748763a553d2711470f4b06879767a95055330..d962ab0adda73f9e0e3a5edaff32384dd3c3fcb3 100644 (file)
@@ -1,22 +1,17 @@
 ## @file\r
 # Generate PCD table for 'Patchable In Module' type PCD with given .map file.\r
 #    The Patch PCD table like:\r
-#    \r
+#\r
 #    PCD Name    Offset in binary\r
 #    ========    ================\r
 #\r
 # Copyright (c) 2008 - 2018, 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
-# http://opensource.org/licenses/bsd-license.php\r
-#\r
-# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
 #\r
 #\r
 \r
 #======================================  External Libraries ========================================\r
+from __future__ import print_function\r
 import optparse\r
 import Common.LongFilePathOs as os\r
 import re\r
@@ -24,25 +19,24 @@ import array
 \r
 from Common.BuildToolError import *\r
 import Common.EdkLogger as EdkLogger\r
-from Common.Misc import PeImageClass\r
+from Common.Misc import PeImageClass, startPatternGeneral, addressPatternGeneral, valuePatternGcc, pcdPatternGcc, secReGeneral\r
 from Common.BuildVersion import gBUILD_VERSION\r
 from Common.LongFilePathSupport import OpenLongFilePath as open\r
 \r
 # Version and Copyright\r
 __version_number__ = ("0.10" + " " + gBUILD_VERSION)\r
 __version__ = "%prog Version " + __version_number__\r
-__copyright__ = "Copyright (c) 2008 - 2010, Intel Corporation. All rights reserved."\r
+__copyright__ = "Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved."\r
 \r
 #======================================  Internal Libraries ========================================\r
 \r
 #============================================== Code ===============================================\r
-secRe = re.compile('^([\da-fA-F]+):([\da-fA-F]+) +([\da-fA-F]+)[Hh]? +([.\w\$]+) +(\w+)', re.UNICODE)\r
 symRe = re.compile('^([\da-fA-F]+):([\da-fA-F]+) +([\.\-:\\\\\w\?@\$<>]+) +([\da-fA-F]+)', re.UNICODE)\r
 \r
 def parsePcdInfoFromMapFile(mapfilepath, efifilepath):\r
-    """ Parse map file to get binary patch pcd information \r
+    """ Parse map file to get binary patch pcd information\r
     @param path    Map file absolution path\r
-    \r
+\r
     @return a list which element hold (PcdName, Offset, SectionName)\r
     """\r
     lines = []\r
@@ -52,23 +46,26 @@ def parsePcdInfoFromMapFile(mapfilepath, efifilepath):
         f.close()\r
     except:\r
         return None\r
-    \r
+\r
     if len(lines) == 0: return None\r
     firstline = lines[0].strip()\r
+    if re.match('^\s*Address\s*Size\s*Align\s*Out\s*In\s*Symbol\s*$', firstline):\r
+        return _parseForXcodeAndClang9(lines, efifilepath)\r
     if (firstline.startswith("Archive member included ") and\r
         firstline.endswith(" file (symbol)")):\r
         return _parseForGCC(lines, efifilepath)\r
     if firstline.startswith("# Path:"):\r
-        return _parseForXcode(lines, efifilepath)\r
+        return _parseForXcodeAndClang9(lines, efifilepath)\r
     return _parseGeneral(lines, efifilepath)\r
 \r
-def _parseForXcode(lines, efifilepath):\r
+def _parseForXcodeAndClang9(lines, efifilepath):\r
     valuePattern = re.compile('^([\da-fA-FxX]+)([\s\S]*)([_]*_gPcd_BinaryPatch_([\w]+))')\r
     status = 0\r
     pcds = []\r
     for line in lines:\r
         line = line.strip()\r
-        if status == 0 and line == "# Symbols:":\r
+        if status == 0 and (re.match('^\s*Address\s*Size\s*Align\s*Out\s*In\s*Symbol\s*$', line) \\r
+            or line == "# Symbols:"):\r
             status = 1\r
             continue\r
         if status == 1 and len(line) != 0:\r
@@ -80,9 +77,7 @@ def _parseForXcode(lines, efifilepath):
 \r
 def _parseForGCC(lines, efifilepath):\r
     """ Parse map file generated by GCC linker """\r
-    valuePattern = re.compile('^([\w_\.]+) +([\da-fA-Fx]+) +([\da-fA-Fx]+)$')\r
     dataPattern = re.compile('^.data._gPcd_BinaryPatch_([\w_\d]+)$')\r
-    pcdPattern = re.compile('^([\da-fA-Fx]+) +([\da-fA-Fx]+)')\r
     status = 0\r
     imageBase = -1\r
     sections = []\r
@@ -102,7 +97,7 @@ def _parseForGCC(lines, efifilepath):
 \r
         # status handler\r
         if status == 3:\r
-            m = valuePattern.match(line)\r
+            m = valuePatternGcc.match(line)\r
             if m is not None:\r
                 sections.append(m.groups(0))\r
         if status == 3:\r
@@ -110,10 +105,10 @@ def _parseForGCC(lines, efifilepath):
             if m is not None:\r
                 if lines[index + 1]:\r
                     PcdName = m.groups(0)[0]\r
-                    m = pcdPattern.match(lines[index + 1].strip())\r
+                    m = pcdPatternGcc.match(lines[index + 1].strip())\r
                     if m is not None:\r
-                        bpcds.append((PcdName, int(m.groups(0)[0], 16) , int(sections[-1][1], 16), sections[-1][0]))\r
-                \r
+                        bpcds.append((PcdName, int(m.groups(0)[0], 16), int(sections[-1][1], 16), sections[-1][0]))\r
+\r
     # get section information from efi file\r
     efisecs = PeImageClass(efifilepath).SectionHeaderList\r
     if efisecs is None or len(efisecs) == 0:\r
@@ -131,33 +126,31 @@ def _parseForGCC(lines, efifilepath):
                 #assert efisec[0].strip() == pcd[3].strip() and efisec[1] + redirection == pcd[2], "There are some differences between map file and efi file"\r
                 pcds.append([pcd[0], efisec[2] + pcd[1] - efisec[1] - redirection, efisec[0]])\r
     return pcds\r
-                \r
+\r
 def _parseGeneral(lines, efifilepath):\r
-    """ For MSFT, ICC, EBC \r
+    """ For MSFT, ICC, EBC\r
     @param lines    line array for map file\r
-    \r
+\r
     @return a list which element hold (PcdName, Offset, SectionName)\r
     """\r
     status = 0    #0 - beginning of file; 1 - PE section definition; 2 - symbol table\r
     secs = []    # key = section name\r
     bPcds = []\r
-    startPattern = re.compile("^Start[' ']+Length[' ']+Name[' ']+Class")\r
-    addressPattern = re.compile("^Address[' ']+Publics by Value[' ']+Rva\+Base")\r
     symPattern = re.compile('^[_]+gPcd_BinaryPatch_([\w]+)')\r
 \r
     for line in lines:\r
         line = line.strip()\r
-        if startPattern.match(line):\r
+        if startPatternGeneral.match(line):\r
             status = 1\r
             continue\r
-        if addressPattern.match(line):\r
+        if addressPatternGeneral.match(line):\r
             status = 2\r
             continue\r
         if line.startswith("entry point at"):\r
             status = 3\r
             continue\r
         if status == 1 and len(line) != 0:\r
-            m = secRe.match(line)\r
+            m = secReGeneral.match(line)\r
             assert m is not None, "Fail to parse the section in map file , line is %s" % line\r
             sec_no, sec_start, sec_length, sec_name, sec_class = m.groups(0)\r
             secs.append([int(sec_no, 16), int(sec_start, 16), int(sec_length, 16), sec_name, sec_class])\r
@@ -181,7 +174,7 @@ def _parseGeneral(lines, efifilepath):
     efisecs = PeImageClass(efifilepath).SectionHeaderList\r
     if efisecs is None or len(efisecs) == 0:\r
         return None\r
-    \r
+\r
     pcds = []\r
     for pcd in bPcds:\r
         index = 0\r
@@ -192,7 +185,7 @@ def _parseGeneral(lines, efifilepath):
             elif pcd[4] == index:\r
                 pcds.append([pcd[0], efisec[2] + pcd[2], efisec[0]])\r
     return pcds\r
-    \r
+\r
 def generatePcdTable(list, pcdpath):\r
     try:\r
         f = open(pcdpath, 'w')\r
@@ -200,12 +193,12 @@ def generatePcdTable(list, pcdpath):
         pass\r
 \r
     f.write('PCD Name                       Offset    Section Name\r\n')\r
-    \r
+\r
     for pcditem in list:\r
         f.write('%-30s 0x%-08X %-6s\r\n' % (pcditem[0], pcditem[1], pcditem[2]))\r
     f.close()\r
 \r
-    #print 'Success to generate Binary Patch PCD table at %s!' % pcdpath \r
+    #print 'Success to generate Binary Patch PCD table at %s!' % pcdpath\r
 \r
 if __name__ == '__main__':\r
     UsageString = "%prog -m <MapFile> -e <EfiFile> -o <OutFile>"\r
@@ -217,11 +210,11 @@ if __name__ == '__main__':
                       help='Absolute path of EFI binary file.')\r
     parser.add_option('-o', '--outputfile', action='store', dest='outfile',\r
                       help='Absolute path of output file to store the got patchable PCD table.')\r
-  \r
+\r
     (options, args) = parser.parse_args()\r
 \r
     if options.mapfile is None or options.efifile is None:\r
-        print parser.get_usage()\r
+        print(parser.get_usage())\r
     elif os.path.exists(options.mapfile) and os.path.exists(options.efifile):\r
         list = parsePcdInfoFromMapFile(options.mapfile, options.efifile)\r
         if list is not None:\r
@@ -230,6 +223,6 @@ if __name__ == '__main__':
             else:\r
                 generatePcdTable(list, options.mapfile.replace('.map', '.BinaryPcdTable.txt'))\r
         else:\r
-            print 'Fail to generate Patch PCD Table based on map file and efi file'\r
+            print('Fail to generate Patch PCD Table based on map file and efi file')\r
     else:\r
-        print 'Fail to generate Patch PCD Table for fail to find map file or efi file!'\r
+        print('Fail to generate Patch PCD Table for fail to find map file or efi file!')\r