From 88228db38ead6836da7a8d24e11569d5fab54c54 Mon Sep 17 00:00:00 2001 From: Pierre Gondois Date: Wed, 1 Jul 2020 22:06:02 +0800 Subject: BaseTools: Rename AmlToHex script to AmlToC The AmlToHex script and Posix/WindowsLike wrappers convert an AML file to a .hex file, containing a C array storing AML bytecode. This ".hex" file can then be included in a C file, allowing to access the AML bytecode from this C file. The EDK2 build system doesn't allow to a depict dependency orders between files of different languages. For instance, in a module containing a ".c" file and a ".asl", the ".c" file may or may not be built prior to the ".asl" file. This prevents any inclusion of a generated ".hex" in a ".c" file since this later ".hex" file may or may not have been created yet. This patch renames the script as AmlToC. It is posted as a separate patch to prevent git from seeing the renaming as a deletion plus addition of a new file. The ending line of the posix-like bin-wrapper script has also been corrected. This is a first step toward generating a C file containing the AML bytecode from an ASL file. This C file will then be handled by the EDK2 build system to generate an object file. Thus, no file inclusion will be required anymore. The C file requiring the AML bytecode as a C array, and the ASL file, will be compiled independently. The C array must be defined as an external symbol. The linker is resolving the reference to the C array symbol. To summarize, the flow goes as: -1. ASL file is compiled to AML; -2. AML file is copied to a ".amli" intermediate file; -3. EDK2 build system applies the rule relevant to ".amli" files. This is, calling the "AmlToC" script, generating a C file from the ".amli" file; -4. EDK2 build system applies the rule relevant to C files. This is creating an object file. -5. EDK2 build system links the object file containing the AML bytecode with the object file requiring it. Signed-off-by: Pierre Gondois Suggested-by: Tomas Pilar Reviewed-by: Bob Feng --- BaseTools/BinWrappers/PosixLike/AmlToC | 14 +++ BaseTools/BinWrappers/PosixLike/AmlToHex | 14 --- BaseTools/BinWrappers/WindowsLike/AmlToC.bat | 3 + BaseTools/BinWrappers/WindowsLike/AmlToHex.bat | 3 - BaseTools/Source/Python/AmlToC/AmlToC.py | 156 +++++++++++++++++++++++++ BaseTools/Source/Python/AmlToHex/AmlToHex.py | 156 ------------------------- 6 files changed, 173 insertions(+), 173 deletions(-) create mode 100755 BaseTools/BinWrappers/PosixLike/AmlToC delete mode 100755 BaseTools/BinWrappers/PosixLike/AmlToHex create mode 100644 BaseTools/BinWrappers/WindowsLike/AmlToC.bat delete mode 100644 BaseTools/BinWrappers/WindowsLike/AmlToHex.bat create mode 100644 BaseTools/Source/Python/AmlToC/AmlToC.py delete mode 100644 BaseTools/Source/Python/AmlToHex/AmlToHex.py (limited to 'BaseTools') diff --git a/BaseTools/BinWrappers/PosixLike/AmlToC b/BaseTools/BinWrappers/PosixLike/AmlToC new file mode 100755 index 0000000000..1dd28e9662 --- /dev/null +++ b/BaseTools/BinWrappers/PosixLike/AmlToC @@ -0,0 +1,14 @@ +#!/usr/bin/env bash +#python `dirname $0`/RunToolFromSource.py `basename $0` $* + +# If a ${PYTHON_COMMAND} command is available, use it in preference to python +if command -v ${PYTHON_COMMAND} >/dev/null 2>&1; then + python_exe=${PYTHON_COMMAND} +fi + +full_cmd=${BASH_SOURCE:-$0} # see http://mywiki.wooledge.org/BashFAQ/028 for a discussion of why $0 is not a good choice here +dir=$(dirname "$full_cmd") +exe=$(basename "$full_cmd") + +export PYTHONPATH="$dir/../../Source/Python${PYTHONPATH:+:"$PYTHONPATH"}" +exec "${python_exe:-python}" "$dir/../../Source/Python/$exe/$exe.py" "$@" diff --git a/BaseTools/BinWrappers/PosixLike/AmlToHex b/BaseTools/BinWrappers/PosixLike/AmlToHex deleted file mode 100755 index 9fb68299e4..0000000000 --- a/BaseTools/BinWrappers/PosixLike/AmlToHex +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env bash -#python `dirname $0`/RunToolFromSource.py `basename $0` $* - -# If a ${PYTHON_COMMAND} command is available, use it in preference to python -if command -v ${PYTHON_COMMAND} >/dev/null 2>&1; then - python_exe=${PYTHON_COMMAND} -fi - -full_cmd=${BASH_SOURCE:-$0} # see http://mywiki.wooledge.org/BashFAQ/028 for a discussion of why $0 is not a good choice here -dir=$(dirname "$full_cmd") -exe=$(basename "$full_cmd") - -export PYTHONPATH="$dir/../../Source/Python${PYTHONPATH:+:"$PYTHONPATH"}" -exec "${python_exe:-python}" "$dir/../../Source/Python/$exe/$exe.py" "$@" diff --git a/BaseTools/BinWrappers/WindowsLike/AmlToC.bat b/BaseTools/BinWrappers/WindowsLike/AmlToC.bat new file mode 100644 index 0000000000..9616cd893b --- /dev/null +++ b/BaseTools/BinWrappers/WindowsLike/AmlToC.bat @@ -0,0 +1,3 @@ +@setlocal +@set ToolName=%~n0% +@%PYTHON_COMMAND% %BASE_TOOLS_PATH%\Source\Python\%ToolName%\%ToolName%.py %* diff --git a/BaseTools/BinWrappers/WindowsLike/AmlToHex.bat b/BaseTools/BinWrappers/WindowsLike/AmlToHex.bat deleted file mode 100644 index 9616cd893b..0000000000 --- a/BaseTools/BinWrappers/WindowsLike/AmlToHex.bat +++ /dev/null @@ -1,3 +0,0 @@ -@setlocal -@set ToolName=%~n0% -@%PYTHON_COMMAND% %BASE_TOOLS_PATH%\Source\Python\%ToolName%\%ToolName%.py %* diff --git a/BaseTools/Source/Python/AmlToC/AmlToC.py b/BaseTools/Source/Python/AmlToC/AmlToC.py new file mode 100644 index 0000000000..643db2910e --- /dev/null +++ b/BaseTools/Source/Python/AmlToC/AmlToC.py @@ -0,0 +1,156 @@ +## @file +# +# Convert an AML file to a .hex file containing the AML bytecode stored in a +# C array. +# By default, "Tables\Dsdt.aml" will generate "Tables\Dsdt.hex". +# "Tables\Dsdt.hex" will contain a C array named "dsdt_aml_code" that contains +# the AML bytecode. +# +# Copyright (c) 2020, ARM Limited. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# + +import argparse +import Common.EdkLogger as EdkLogger +from Common.BuildToolError import * +import sys +import os + +## Parse the command line arguments. +# +# @retval A argparse.NameSpace instance, containing parsed values. +# +def ParseArgs(): + # Initialize the parser. + Parser = argparse.ArgumentParser( + description="Convert an AML file to a .hex file containing the AML " + \ + "bytecode stored in a C array. By default, " + \ + "\"Tables\\Dsdt.aml\" will generate" + \ + "\"Tables\\Dsdt.hex\". \"Tables\\Dsdt.hex\" will " + \ + "contain a C array named \"dsdt_aml_code\" that " + \ + "contains the AML bytecode." + ) + + # Define the possible arguments. + Parser.add_argument( + dest="InputFile", + help="Path to an input AML file to generate a .hex file from." + ) + Parser.add_argument( + "-o", "--out-dir", dest="OutDir", + help="Output directory where the .hex file will be generated. " + \ + "Default is the input file's directory." + ) + + # Parse the input arguments. + Args = Parser.parse_args() + SplitInputName = "" + + if not os.path.exists(Args.InputFile): + EdkLogger.error(__file__, FILE_OPEN_FAILURE, + ExtraData=Args.InputFile) + return None + else: + with open(Args.InputFile, "rb") as fIn: + Signature = str(fIn.read(4)) + if ("DSDT" not in Signature) and ("SSDT" not in Signature): + EdkLogger.info("Invalid file type. " + \ + "File does not have a valid " + \ + "DSDT or SSDT signature: %s" % Args.InputFile) + return None + + # Get the basename of the input file. + SplitInputName = os.path.splitext(Args.InputFile) + BaseName = os.path.basename(SplitInputName[0]) + + # If no output directory is specified, output to the input directory. + if not Args.OutDir: + Args.OutputFile = os.path.join( + os.path.dirname(Args.InputFile), + BaseName + ".hex" + ) + else: + if not os.path.exists(Args.OutDir): + os.mkdir(Args.OutDir) + Args.OutputFile = os.path.join(Args.OutDir, BaseName + ".hex") + + Args.BaseName = BaseName + + return Args + +## Convert an AML file to a .hex file containing the AML bytecode stored +# in a C array. +# +# @param InputFile Path to the input AML file. +# @param OutputFile Path to the output .hex file to generate. +# @param BaseName Base name of the input file. +# This is also the name of the generated .hex file. +# +def AmlToHex(InputFile, OutputFile, BaseName): + + MacroName = "__{}_HEX__".format(BaseName.upper()) + ArrayName = BaseName.lower() + "_aml_code" + + with open(InputFile, "rb") as fIn, open(OutputFile, "w") as fOut: + # Write header. + fOut.write("// This file has been generated from:\n" + \ + "// \tPython script: " + \ + os.path.abspath(__file__) + "\n" + \ + "// \tInput AML file: " + \ + os.path.abspath(InputFile) + "\n\n" + \ + "#ifndef {}\n".format(MacroName) + \ + "#define {}\n\n".format(MacroName) + ) + + # Write the array and its content. + fOut.write("unsigned char {}[] = {{\n ".format(ArrayName)) + cnt = 0 + byte = fIn.read(1) + while len(byte) != 0: + fOut.write("0x{0:02X}, ".format(ord(byte))) + cnt += 1 + if (cnt % 8) == 0: + fOut.write("\n ") + byte = fIn.read(1) + fOut.write("\n};\n") + + # Write footer. + fOut.write("#endif // {}\n".format(MacroName)) + +## Main method +# +# This method: +# 1- Initialize an EdkLogger instance. +# 2- Parses the input arguments. +# 3- Converts an AML file to a .hex file containing the AML bytecode stored +# in a C array. +# +# @retval 0 Success. +# @retval 1 Error. +# +def Main(): + # Initialize an EdkLogger instance. + EdkLogger.Initialize() + + try: + # Parse the input arguments. + CommandArguments = ParseArgs() + if not CommandArguments: + return 1 + + # Convert an AML file to a .hex file containing the AML bytecode stored + # in a C array. + AmlToHex(CommandArguments.InputFile, CommandArguments.OutputFile, + CommandArguments.BaseName) + except Exception as e: + print(e) + return 1 + + return 0 + +if __name__ == '__main__': + r = Main() + # 0-127 is a safe return range, and 1 is a standard default error + if r < 0 or r > 127: r = 1 + sys.exit(r) diff --git a/BaseTools/Source/Python/AmlToHex/AmlToHex.py b/BaseTools/Source/Python/AmlToHex/AmlToHex.py deleted file mode 100644 index 643db2910e..0000000000 --- a/BaseTools/Source/Python/AmlToHex/AmlToHex.py +++ /dev/null @@ -1,156 +0,0 @@ -## @file -# -# Convert an AML file to a .hex file containing the AML bytecode stored in a -# C array. -# By default, "Tables\Dsdt.aml" will generate "Tables\Dsdt.hex". -# "Tables\Dsdt.hex" will contain a C array named "dsdt_aml_code" that contains -# the AML bytecode. -# -# Copyright (c) 2020, ARM Limited. All rights reserved.
-# -# SPDX-License-Identifier: BSD-2-Clause-Patent -# - -import argparse -import Common.EdkLogger as EdkLogger -from Common.BuildToolError import * -import sys -import os - -## Parse the command line arguments. -# -# @retval A argparse.NameSpace instance, containing parsed values. -# -def ParseArgs(): - # Initialize the parser. - Parser = argparse.ArgumentParser( - description="Convert an AML file to a .hex file containing the AML " + \ - "bytecode stored in a C array. By default, " + \ - "\"Tables\\Dsdt.aml\" will generate" + \ - "\"Tables\\Dsdt.hex\". \"Tables\\Dsdt.hex\" will " + \ - "contain a C array named \"dsdt_aml_code\" that " + \ - "contains the AML bytecode." - ) - - # Define the possible arguments. - Parser.add_argument( - dest="InputFile", - help="Path to an input AML file to generate a .hex file from." - ) - Parser.add_argument( - "-o", "--out-dir", dest="OutDir", - help="Output directory where the .hex file will be generated. " + \ - "Default is the input file's directory." - ) - - # Parse the input arguments. - Args = Parser.parse_args() - SplitInputName = "" - - if not os.path.exists(Args.InputFile): - EdkLogger.error(__file__, FILE_OPEN_FAILURE, - ExtraData=Args.InputFile) - return None - else: - with open(Args.InputFile, "rb") as fIn: - Signature = str(fIn.read(4)) - if ("DSDT" not in Signature) and ("SSDT" not in Signature): - EdkLogger.info("Invalid file type. " + \ - "File does not have a valid " + \ - "DSDT or SSDT signature: %s" % Args.InputFile) - return None - - # Get the basename of the input file. - SplitInputName = os.path.splitext(Args.InputFile) - BaseName = os.path.basename(SplitInputName[0]) - - # If no output directory is specified, output to the input directory. - if not Args.OutDir: - Args.OutputFile = os.path.join( - os.path.dirname(Args.InputFile), - BaseName + ".hex" - ) - else: - if not os.path.exists(Args.OutDir): - os.mkdir(Args.OutDir) - Args.OutputFile = os.path.join(Args.OutDir, BaseName + ".hex") - - Args.BaseName = BaseName - - return Args - -## Convert an AML file to a .hex file containing the AML bytecode stored -# in a C array. -# -# @param InputFile Path to the input AML file. -# @param OutputFile Path to the output .hex file to generate. -# @param BaseName Base name of the input file. -# This is also the name of the generated .hex file. -# -def AmlToHex(InputFile, OutputFile, BaseName): - - MacroName = "__{}_HEX__".format(BaseName.upper()) - ArrayName = BaseName.lower() + "_aml_code" - - with open(InputFile, "rb") as fIn, open(OutputFile, "w") as fOut: - # Write header. - fOut.write("// This file has been generated from:\n" + \ - "// \tPython script: " + \ - os.path.abspath(__file__) + "\n" + \ - "// \tInput AML file: " + \ - os.path.abspath(InputFile) + "\n\n" + \ - "#ifndef {}\n".format(MacroName) + \ - "#define {}\n\n".format(MacroName) - ) - - # Write the array and its content. - fOut.write("unsigned char {}[] = {{\n ".format(ArrayName)) - cnt = 0 - byte = fIn.read(1) - while len(byte) != 0: - fOut.write("0x{0:02X}, ".format(ord(byte))) - cnt += 1 - if (cnt % 8) == 0: - fOut.write("\n ") - byte = fIn.read(1) - fOut.write("\n};\n") - - # Write footer. - fOut.write("#endif // {}\n".format(MacroName)) - -## Main method -# -# This method: -# 1- Initialize an EdkLogger instance. -# 2- Parses the input arguments. -# 3- Converts an AML file to a .hex file containing the AML bytecode stored -# in a C array. -# -# @retval 0 Success. -# @retval 1 Error. -# -def Main(): - # Initialize an EdkLogger instance. - EdkLogger.Initialize() - - try: - # Parse the input arguments. - CommandArguments = ParseArgs() - if not CommandArguments: - return 1 - - # Convert an AML file to a .hex file containing the AML bytecode stored - # in a C array. - AmlToHex(CommandArguments.InputFile, CommandArguments.OutputFile, - CommandArguments.BaseName) - except Exception as e: - print(e) - return 1 - - return 0 - -if __name__ == '__main__': - r = Main() - # 0-127 is a safe return range, and 1 is a standard default error - if r < 0 or r > 127: r = 1 - sys.exit(r) -- cgit v1.2.3