public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: Yonghong Zhu <yonghong.zhu@intel.com>
To: edk2-devel@lists.01.org
Cc: Liming Gao <liming.gao@intel.com>
Subject: [Patch 2/3 V2] BaseTools: Update Trim to generate VfrBinOffset Binary
Date: Tue, 14 Nov 2017 16:27:11 +0800	[thread overview]
Message-ID: <1510648032-7924-3-git-send-email-yonghong.zhu@intel.com> (raw)
In-Reply-To: <1510648032-7924-1-git-send-email-yonghong.zhu@intel.com>

Its usage is
"Trim --Vfr-Uni-Offset -o $(OUTPUT_DIR)(+)$(MODULE_NAME)VfrOffset.sec
--ModuleName=$(MODULE_NAME) --DebugDir=$(DEBUG_DIR)"

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Liming Gao <liming.gao@intel.com>
---
 BaseTools/Source/Python/Trim/Trim.py | 81 ++++++++++++++++++++++++++++++++++--
 1 file changed, 78 insertions(+), 3 deletions(-)

diff --git a/BaseTools/Source/Python/Trim/Trim.py b/BaseTools/Source/Python/Trim/Trim.py
index 9ccc027..d1e40b0 100644
--- a/BaseTools/Source/Python/Trim/Trim.py
+++ b/BaseTools/Source/Python/Trim/Trim.py
@@ -1,9 +1,9 @@
 ## @file
 # Trim files preprocessed by compiler
 #
-# Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2007 - 2017, Intel Corporation. All rights reserved.<BR>
 # This program and the accompanying materials
 # are licensed and made available under the terms and conditions of the BSD License
 # which accompanies this distribution.  The full text of the license may be found at
 # http://opensource.org/licenses/bsd-license.php
 #
@@ -15,10 +15,11 @@
 # Import Modules
 #
 import Common.LongFilePathOs as os
 import sys
 import re
+import StringIO
 
 from optparse import OptionParser
 from optparse import make_option
 from Common.BuildToolError import *
 from Common.Misc import *
@@ -27,11 +28,11 @@ import Common.EdkLogger as EdkLogger
 from Common.LongFilePathSupport import OpenLongFilePath as open
 
 # Version and Copyright
 __version_number__ = ("0.10" + " " + gBUILD_VERSION)
 __version__ = "%prog Version " + __version_number__
-__copyright__ = "Copyright (c) 2007-2016, Intel Corporation. All rights reserved."
+__copyright__ = "Copyright (c) 2007-2017, Intel Corporation. All rights reserved."
 
 ## Regular expression for matching Line Control directive like "#line xxx"
 gLineControlDirective = re.compile('^\s*#(?:line)?\s+([0-9]+)\s+"*([^"]*)"')
 ## Regular expression for matching "typedef struct"
 gTypedefPattern = re.compile("^\s*typedef\s+struct(\s+\w+)?\s*[{]*$", re.MULTILINE)
@@ -428,10 +429,72 @@ def TrimAslFile(Source, Target, IncludePathFile):
         EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)
 
     f.writelines(Lines)
     f.close()
 
+def GenerateVfrBinSec(ModuleName, DebugDir, OutputFile):
+    VfrNameList = []
+    if os.path.isdir(DebugDir):
+        for CurrentDir, Dirs, Files in os.walk(DebugDir):
+            for FileName in Files:
+                Name, Ext = os.path.splitext(FileName)
+                if Ext == '.lst':
+                    VfrNameList.append (Name + 'Bin')
+
+    VfrNameList.append (ModuleName + 'Strings')
+
+    EfiFileName = os.path.join(DebugDir, ModuleName + '.efi')
+    MapFileName = os.path.join(DebugDir, ModuleName + '.map')
+    VfrUniOffsetList = GetVariableOffset(MapFileName, EfiFileName, VfrNameList)
+
+    if not VfrUniOffsetList:
+        return
+
+    try:
+        fInputfile = open(OutputFile, "wb+", 0)
+    except:
+        EdkLogger.error("Trim", FILE_OPEN_FAILURE, "File open failed for %s" %OutputFile, None)
+
+    # Use a instance of StringIO to cache data
+    fStringIO = StringIO.StringIO('')
+
+    for Item in VfrUniOffsetList:
+        if (Item[0].find("Strings") != -1):
+            #
+            # UNI offset in image.
+            # GUID + Offset
+            # { 0x8913c5e0, 0x33f6, 0x4d86, { 0x9b, 0xf1, 0x43, 0xef, 0x89, 0xfc, 0x6, 0x66 } }
+            #
+            UniGuid = [0xe0, 0xc5, 0x13, 0x89, 0xf6, 0x33, 0x86, 0x4d, 0x9b, 0xf1, 0x43, 0xef, 0x89, 0xfc, 0x6, 0x66]
+            UniGuid = [chr(ItemGuid) for ItemGuid in UniGuid]
+            fStringIO.write(''.join(UniGuid))
+            UniValue = pack ('Q', int (Item[1], 16))
+            fStringIO.write (UniValue)
+        else:
+            #
+            # VFR binary offset in image.
+            # GUID + Offset
+            # { 0xd0bc7cb4, 0x6a47, 0x495f, { 0xaa, 0x11, 0x71, 0x7, 0x46, 0xda, 0x6, 0xa2 } };
+            #
+            VfrGuid = [0xb4, 0x7c, 0xbc, 0xd0, 0x47, 0x6a, 0x5f, 0x49, 0xaa, 0x11, 0x71, 0x7, 0x46, 0xda, 0x6, 0xa2]
+            VfrGuid = [chr(ItemGuid) for ItemGuid in VfrGuid]
+            fStringIO.write(''.join(VfrGuid))
+            type (Item[1])
+            VfrValue = pack ('Q', int (Item[1], 16))
+            fStringIO.write (VfrValue)
+
+    #
+    # write data into file.
+    #
+    try :
+        fInputfile.write (fStringIO.getvalue())
+    except:
+        EdkLogger.error("Trim", FILE_WRITE_FAILURE, "Write data to file %s failed, please check whether the file been locked or using by other applications." %OutputFile, None)
+
+    fStringIO.close ()
+    fInputfile.close ()
+
 ## Trim EDK source code file(s)
 #
 #
 # @param  Source    File or directory to be trimmed
 # @param  Target    File or directory to store the trimmed content
@@ -533,10 +596,12 @@ def Options():
     OptionList = [
         make_option("-s", "--source-code", dest="FileType", const="SourceCode", action="store_const",
                           help="The input file is preprocessed source code, including C or assembly code"),
         make_option("-r", "--vfr-file", dest="FileType", const="Vfr", action="store_const",
                           help="The input file is preprocessed VFR file"),
+        make_option("--Vfr-Uni-Offset", dest="FileType", const="VfrOffsetBin", action="store_const",
+                          help="The input file is EFI image"),
         make_option("-a", "--asl-file", dest="FileType", const="Asl", action="store_const",
                           help="The input file is ASL file"),
         make_option("-8", "--Edk-source-code", dest="FileType", const="EdkSourceCode", action="store_const",
                           help="The input file is source code for Edk to be trimmed for ECP"),
 
@@ -547,30 +612,38 @@ def Options():
                           help="Remove postfix of long number"),
         make_option("-i", "--include-path-file", dest="IncludePathFile",
                           help="The input file is include path list to search for ASL include file"),
         make_option("-o", "--output", dest="OutputFile",
                           help="File to store the trimmed content"),
+        make_option("--ModuleName", dest="ModuleName", help="The module's BASE_NAME"),
+        make_option("--DebugDir", dest="DebugDir",
+                          help="Debug Output directory to store the output files"),
         make_option("-v", "--verbose", dest="LogLevel", action="store_const", const=EdkLogger.VERBOSE,
                           help="Run verbosely"),
         make_option("-d", "--debug", dest="LogLevel", type="int",
                           help="Run with debug information"),
         make_option("-q", "--quiet", dest="LogLevel", action="store_const", const=EdkLogger.QUIET,
                           help="Run quietly"),
         make_option("-?", action="help", help="show this help message and exit"),
     ]
 
     # use clearer usage to override default usage message
-    UsageString = "%prog [-s|-r|-a] [-c] [-v|-d <debug_level>|-q] [-i <include_path_file>] [-o <output_file>] <input_file>"
+    UsageString = "%prog [-s|-r|-a|--Vfr-Uni-Offset] [-c] [-v|-d <debug_level>|-q] [-i <include_path_file>] [-o <output_file>] [--ModuleName <ModuleName>] [--DebugDir <DebugDir>] [<input_file>]"
 
     Parser = OptionParser(description=__copyright__, version=__version__, option_list=OptionList, usage=UsageString)
     Parser.set_defaults(FileType="Vfr")
     Parser.set_defaults(ConvertHex=False)
     Parser.set_defaults(LogLevel=EdkLogger.INFO)
 
     Options, Args = Parser.parse_args()
 
     # error check
+    if Options.FileType == 'VfrOffsetBin':
+        if len(Args) == 0:
+            return Options, ''
+        elif len(Args) > 1:
+            EdkLogger.error("Trim", OPTION_NOT_SUPPORTED, ExtraData=Parser.get_usage())
     if len(Args) == 0:
         EdkLogger.error("Trim", OPTION_MISSING, ExtraData=Parser.get_usage())
     if len(Args) > 1:
         EdkLogger.error("Trim", OPTION_NOT_SUPPORTED, ExtraData=Parser.get_usage())
 
@@ -606,10 +679,12 @@ def Main():
             if CommandOptions.OutputFile == None:
                 CommandOptions.OutputFile = os.path.splitext(InputFile)[0] + '.iii'
             TrimAslFile(InputFile, CommandOptions.OutputFile, CommandOptions.IncludePathFile)
         elif CommandOptions.FileType == "EdkSourceCode":
             TrimEdkSources(InputFile, CommandOptions.OutputFile)
+        elif CommandOptions.FileType == "VfrOffsetBin":
+            GenerateVfrBinSec(CommandOptions.ModuleName, CommandOptions.DebugDir, CommandOptions.OutputFile)
         else :
             if CommandOptions.OutputFile == None:
                 CommandOptions.OutputFile = os.path.splitext(InputFile)[0] + '.iii'
             TrimPreprocessedFile(InputFile, CommandOptions.OutputFile, CommandOptions.ConvertHex, CommandOptions.TrimLong)
     except FatalError, X:
-- 
2.6.1.windows.1



  parent reply	other threads:[~2017-11-14  8:23 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-14  8:27 [Patch 0/3 V2] BaseTools: Enable multiple thread to generate FFS file Yonghong Zhu
2017-11-14  8:27 ` [Patch 1/3 V2] BaseTools: GenFfs support to get alignment value from SectionFile Yonghong Zhu
2017-11-14  8:27 ` Yonghong Zhu [this message]
2017-11-14  8:27 ` [Patch 3/3 V2] BaseTools: Update Makefile to support FFS file generation Yonghong Zhu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1510648032-7924-3-git-send-email-yonghong.zhu@intel.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox