EDK2 doxygen online documents - Firmware Encoding Index 1
EDK2 doxygen online documents - Firmware Encoding Index

BaseTools/Source/Python/GenPatchPcdTable/GenPatchPcdTable.py

Go to the documentation of this file.
00001 ## @file
00002 # Generate PCD table for 'Patchable In Module' type PCD with given .map file.
00003 #    The Patch PCD table like:
00004 #    
00005 #    PCD Name    Offset in binary
00006 #    ========    ================
00007 #
00008 # Copyright (c) 2008 - 2010, Intel Corporation. All rights reserved.<BR>
00009 # This program and the accompanying materials
00010 # are licensed and made available under the terms and conditions of the BSD License
00011 # which accompanies this distribution.  The full text of the license may be found at
00012 # http://opensource.org/licenses/bsd-license.php
00013 #
00014 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
00015 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
00016 #
00017 #
00018 
00019 #======================================  External Libraries ========================================
00020 import optparse
00021 import os
00022 import re
00023 import array
00024 
00025 from Common.BuildToolError import *
00026 import Common.EdkLogger as EdkLogger
00027 from Common.Misc import PeImageClass
00028 from Common.BuildVersion import gBUILD_VERSION
00029 
00030 # Version and Copyright
00031 __version_number__ = ("0.10" + " " + gBUILD_VERSION)
00032 __version__ = "%prog Version " + __version_number__
00033 __copyright__ = "Copyright (c) 2008 - 2010, Intel Corporation. All rights reserved."
00034 
00035 #======================================  Internal Libraries ========================================
00036 
00037 #============================================== Code ===============================================
00038 secRe = re.compile('^([\da-fA-F]+):([\da-fA-F]+) +([\da-fA-F]+)[Hh]? +([.\w\$]+) +(\w+)', re.UNICODE)
00039 symRe = re.compile('^([\da-fA-F]+):([\da-fA-F]+) +([\.:\\\\\w\?@\$]+) +([\da-fA-F]+)', re.UNICODE)
00040 
00041 def parsePcdInfoFromMapFile(mapfilepath, efifilepath):
00042     """ Parse map file to get binary patch pcd information 
00043     @param path    Map file absolution path
00044     
00045     @return a list which element hold (PcdName, Offset, SectionName)
00046     """
00047     lines = []
00048     try:
00049         f = open(mapfilepath, 'r')
00050         lines = f.readlines()
00051         f.close()
00052     except:
00053         return None
00054     
00055     if len(lines) == 0: return None
00056     if lines[0].strip().find("Archive member included because of file (symbol)") != -1:
00057         return _parseForGCC(lines)
00058     return _parseGeneral(lines, efifilepath)
00059     
00060 def _parseForGCC(lines):
00061     """ Parse map file generated by GCC linker """
00062     status       = 0
00063     imageBase    = -1
00064     lastSectionName = None
00065     pcds         = []
00066     for line in lines:
00067         line = line.strip()
00068         # status machine transection
00069         if status == 0 and line == "Linker script and memory map":
00070             status = 1
00071             continue
00072         elif status == 1 and line == 'START GROUP':
00073             status = 2
00074             continue
00075         
00076         # status handler:
00077         if status == 1:
00078             m = re.match('^[\da-fA-FxhH]+ +__image_base__ += +([\da-fA-FhxH]+)', line)
00079             if m != None:
00080                 imageBase = int(m.groups(0)[0], 16)
00081         if status == 2:
00082             m = re.match('^([\w_\.]+) +([\da-fA-Fx]+) +([\da-fA-Fx]+)', line)
00083             if m != None:
00084                 lastSectionName = m.groups(0)[0]
00085         if status == 2:
00086             m = re.match("^([\da-fA-Fx]+) +[_]+gPcd_BinaryPatch_([\w_\d]+)", line)
00087             if m != None:
00088                 assert imageBase != -1, "Fail to get Binary PCD offsest for unknown image base address"
00089                 pcds.append((m.groups(0)[1], int(m.groups(0)[0], 16) - imageBase, lastSectionName))
00090     return pcds
00091                 
00092 def _parseGeneral(lines, efifilepath):
00093     """ For MSFT, ICC, EBC 
00094     @param lines    line array for map file
00095     
00096     @return a list which element hold (PcdName, Offset, SectionName)
00097     """    
00098     status = 0    #0 - beginning of file; 1 - PE section definition; 2 - symbol table
00099     secs  = []    # key = section name
00100     bPcds = []
00101     
00102 
00103     for line in lines:
00104         line = line.strip()
00105         if re.match("^Start[' ']+Length[' ']+Name[' ']+Class", line):
00106             status = 1
00107             continue
00108         if re.match("^Address[' ']+Publics by Value[' ']+Rva\+Base", line):
00109             status = 2
00110             continue
00111         if re.match("^entry point at", line):
00112             status = 3
00113             continue        
00114         if status == 1 and len(line) != 0:
00115             m =  secRe.match(line)
00116             assert m != None, "Fail to parse the section in map file , line is %s" % line
00117             sec_no, sec_start, sec_length, sec_name, sec_class = m.groups(0)
00118             secs.append([int(sec_no, 16), int(sec_start, 16), int(sec_length, 16), sec_name, sec_class])
00119         if status == 2 and len(line) != 0:
00120             m = symRe.match(line)
00121             assert m != None, "Fail to parse the symbol in map file, line is %s" % line
00122             sec_no, sym_offset, sym_name, vir_addr = m.groups(0)
00123             sec_no     = int(sec_no,     16)
00124             sym_offset = int(sym_offset, 16)
00125             vir_addr   = int(vir_addr,   16)
00126             m2 = re.match('^[_]+gPcd_BinaryPatch_([\w]+)', sym_name)
00127             if m2 != None:
00128                 # fond a binary pcd entry in map file
00129                 for sec in secs:
00130                     if sec[0] == sec_no and (sym_offset >= sec[1] and sym_offset < sec[1] + sec[2]):
00131                         bPcds.append([m2.groups(0)[0], sec[3], sym_offset, vir_addr, sec_no])
00132 
00133     if len(bPcds) == 0: return None
00134 
00135     # get section information from efi file
00136     efisecs = PeImageClass(efifilepath).SectionHeaderList
00137     if efisecs == None or len(efisecs) == 0:
00138         return None
00139     
00140     pcds = []
00141     for pcd in bPcds:
00142         index = 0
00143         for efisec in efisecs:
00144             index = index + 1
00145             if pcd[1].strip() == efisec[0].strip():
00146                 pcds.append([pcd[0], efisec[2] + pcd[2], efisec[0]])
00147             elif pcd[4] == index:
00148                 pcds.append([pcd[0], efisec[2] + pcd[2], efisec[0]])
00149     return pcds
00150     
00151 def generatePcdTable(list, pcdpath):
00152     try:
00153         f = open(pcdpath, 'w')
00154     except:
00155         pass
00156 
00157     f.write('PCD Name                       Offset    Section Name\r\n')
00158     
00159     for pcditem in list:
00160         f.write('%-30s 0x%-08X %-6s\r\n' % (pcditem[0], pcditem[1], pcditem[2]))
00161     f.close()
00162 
00163     #print 'Success to generate Binary Patch PCD table at %s!' % pcdpath 
00164     
00165 if __name__ == '__main__':
00166     UsageString = "%prog -m <MapFile> -e <EfiFile> -o <OutFile>"
00167     AdditionalNotes = "\nPCD table is generated in file name with .BinaryPcdTable.txt postfix"
00168     parser = optparse.OptionParser(description=__copyright__, version=__version__, usage=UsageString)
00169     parser.add_option('-m', '--mapfile', action='store', dest='mapfile',
00170                       help='Absolute path of module map file.')
00171     parser.add_option('-e', '--efifile', action='store', dest='efifile',
00172                       help='Absolute path of EFI binary file.')
00173     parser.add_option('-o', '--outputfile', action='store', dest='outfile',
00174                       help='Absolute path of output file to store the got patchable PCD table.')
00175   
00176     (options, args) = parser.parse_args()
00177 
00178     if options.mapfile == None or options.efifile == None:
00179         print parser.get_usage()
00180     elif os.path.exists(options.mapfile) and os.path.exists(options.efifile):
00181         list = parsePcdInfoFromMapFile(options.mapfile, options.efifile) 
00182         if list != None:
00183             if options.outfile != None:
00184                 generatePcdTable(list, options.outfile)
00185             else:
00186                 generatePcdTable(list, options.mapfile.replace('.map', '.BinaryPcdTable.txt')) 
00187         else:
00188             print 'Fail to generate Patch PCD Table based on map file and efi file'
00189     else:
00190         print 'Fail to generate Patch PCD Table for fail to find map file or efi file!'
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Defines