]> git.proxmox.com Git - mirror_edk2.git/commitdiff
BaseTools: Script for converting .aml to .hex
authorPierre Gondois <pierre.gondois@arm.com>
Wed, 5 Feb 2020 14:52:03 +0000 (22:52 +0800)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Thu, 6 Feb 2020 13:52:11 +0000 (13:52 +0000)
The "-tc" option of the iasl compiler allows to generate a
.hex file containing a C array storing AML bytecode.

An online discussion suggested that this "-tc" option
was specific to the iasl compiler and it shouldn't be relied
on. This conversation is available at:
https://edk2.groups.io/g/devel/topic/39786201#49659

A way to address this issue is to implement a compiler
independent script that takes an AML file as input, and
generates a .hex file.

This patch implements a Python script that converts an AML
file to a .hex file, containing a C array storing AML bytecode.
This scipt has been tested with the AML output from the
following compilers supported by the EDKII implementation:
  * Intel ASL compiler
  * Microsoft ASL compiler

Signed-off-by: Pierre Gondois <pierre.gondois@arm.com>
Reviewed-by: Bob Feng <bob.c.feng@intel.com>
BaseTools/BinWrappers/PosixLike/AmlToHex [new file with mode: 0755]
BaseTools/BinWrappers/WindowsLike/AmlToHex.bat [new file with mode: 0644]
BaseTools/Conf/build_rule.template
BaseTools/Source/Python/AmlToHex/AmlToHex.py [new file with mode: 0644]

diff --git a/BaseTools/BinWrappers/PosixLike/AmlToHex b/BaseTools/BinWrappers/PosixLike/AmlToHex
new file mode 100755 (executable)
index 0000000..9fb6829
--- /dev/null
@@ -0,0 +1,14 @@
+#!/usr/bin/env bash\r
+#python `dirname $0`/RunToolFromSource.py `basename $0` $*\r
+\r
+# If a ${PYTHON_COMMAND} command is available, use it in preference to python\r
+if command -v ${PYTHON_COMMAND} >/dev/null 2>&1; then\r
+    python_exe=${PYTHON_COMMAND}\r
+fi\r
+\r
+full_cmd=${BASH_SOURCE:-$0} # see http://mywiki.wooledge.org/BashFAQ/028 for a discussion of why $0 is not a good choice here\r
+dir=$(dirname "$full_cmd")\r
+exe=$(basename "$full_cmd")\r
+\r
+export PYTHONPATH="$dir/../../Source/Python${PYTHONPATH:+:"$PYTHONPATH"}"\r
+exec "${python_exe:-python}" "$dir/../../Source/Python/$exe/$exe.py" "$@"\r
diff --git a/BaseTools/BinWrappers/WindowsLike/AmlToHex.bat b/BaseTools/BinWrappers/WindowsLike/AmlToHex.bat
new file mode 100644 (file)
index 0000000..9616cd8
--- /dev/null
@@ -0,0 +1,3 @@
+@setlocal\r
+@set ToolName=%~n0%\r
+@%PYTHON_COMMAND% %BASE_TOOLS_PATH%\Source\Python\%ToolName%\%ToolName%.py %*\r
index 51748bc0655a5c656258a3007b4db6b2dc941ea0..0822b681fcd9f61c6508e6f93ffc31fa70fd7059 100755 (executable)
@@ -1,6 +1,7 @@
 #\r
 #  Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
 #  Portions copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>\r
+#  Copyright (c) 2020, ARM Ltd. All rights reserved.<BR>\r
 #  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 #\r
 \r
         "$(ASLPP)" $(DEPS_FLAGS) $(ASLPP_FLAGS) $(INC) /I${s_path} $(OUTPUT_DIR)(+)${s_dir}(+)${s_base}.i > $(OUTPUT_DIR)(+)${s_dir}(+)${s_base}.iii\r
         Trim --source-code -l -o $(OUTPUT_DIR)(+)${s_dir}(+)${s_base}.iiii $(OUTPUT_DIR)(+)${s_dir}(+)${s_base}.iii \r
         "$(ASL)" $(ASL_FLAGS) $(ASL_OUTFLAGS)${dst} $(OUTPUT_DIR)(+)${s_dir}(+)${s_base}.iiii\r
+        -AmlToHex $(OUTPUT_DIR)(+)${s_dir}(+)${s_base}.aml\r
 \r
     <Command.GCC>\r
         Trim --asl-file --asl-deps -o $(OUTPUT_DIR)(+)${s_dir}(+)${s_base}.i -i $(INC_LIST) ${src}\r
         "$(ASLPP)" $(DEPS_FLAGS) $(ASLPP_FLAGS) $(INC) -I${s_path} $(OUTPUT_DIR)(+)${s_dir}(+)${s_base}.i > $(OUTPUT_DIR)(+)${s_dir}(+)${s_base}.iii\r
         Trim --source-code -l -o $(OUTPUT_DIR)(+)${s_dir}(+)${s_base}.iiii $(OUTPUT_DIR)(+)${s_dir}(+)${s_base}.iii \r
         "$(ASL)" $(ASL_FLAGS) $(ASL_OUTFLAGS)${dst} $(OUTPUT_DIR)(+)${s_dir}(+)${s_base}.iiii\r
+        -AmlToHex $(OUTPUT_DIR)(+)${s_dir}(+)${s_base}.aml\r
 \r
 [C-Code-File.AcpiTable]\r
     <InputFile>\r
diff --git a/BaseTools/Source/Python/AmlToHex/AmlToHex.py b/BaseTools/Source/Python/AmlToHex/AmlToHex.py
new file mode 100644 (file)
index 0000000..643db29
--- /dev/null
@@ -0,0 +1,156 @@
+## @file\r
+#\r
+# Convert an AML file to a .hex file containing the AML bytecode stored in a\r
+# C array.\r
+# By default, "Tables\Dsdt.aml" will generate "Tables\Dsdt.hex".\r
+# "Tables\Dsdt.hex" will contain a C array named "dsdt_aml_code" that contains\r
+# the AML bytecode.\r
+#\r
+# Copyright (c) 2020, ARM Limited. All rights reserved.<BR>\r
+#\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
+#\r
+\r
+import argparse\r
+import Common.EdkLogger as EdkLogger\r
+from Common.BuildToolError import *\r
+import sys\r
+import os\r
+\r
+## Parse the command line arguments.\r
+#\r
+# @retval A argparse.NameSpace instance, containing parsed values.\r
+#\r
+def ParseArgs():\r
+    # Initialize the parser.\r
+    Parser = argparse.ArgumentParser(\r
+      description="Convert an AML file to a .hex file containing the AML " + \\r
+                  "bytecode stored in a C array. By default, " + \\r
+                  "\"Tables\\Dsdt.aml\" will generate" + \\r
+                  "\"Tables\\Dsdt.hex\". \"Tables\\Dsdt.hex\" will " + \\r
+                  "contain a C array named \"dsdt_aml_code\" that " + \\r
+                  "contains the AML bytecode."\r
+      )\r
+\r
+    # Define the possible arguments.\r
+    Parser.add_argument(\r
+      dest="InputFile",\r
+      help="Path to an input AML file to generate a .hex file from."\r
+      )\r
+    Parser.add_argument(\r
+      "-o", "--out-dir", dest="OutDir",\r
+      help="Output directory where the .hex file will be generated. " + \\r
+           "Default is the input file's directory."\r
+      )\r
+\r
+    # Parse the input arguments.\r
+    Args = Parser.parse_args()\r
+    SplitInputName = ""\r
+\r
+    if not os.path.exists(Args.InputFile):\r
+        EdkLogger.error(__file__, FILE_OPEN_FAILURE,\r
+                        ExtraData=Args.InputFile)\r
+        return None\r
+    else:\r
+        with open(Args.InputFile, "rb") as fIn:\r
+            Signature = str(fIn.read(4))\r
+            if ("DSDT" not in Signature) and ("SSDT" not in Signature):\r
+                EdkLogger.info("Invalid file type. " + \\r
+                                "File does not have a valid " + \\r
+                                "DSDT or SSDT signature: %s" % Args.InputFile)\r
+                return None\r
+\r
+    # Get the basename of the input file.\r
+    SplitInputName = os.path.splitext(Args.InputFile)\r
+    BaseName = os.path.basename(SplitInputName[0])\r
+\r
+    # If no output directory is specified, output to the input directory.\r
+    if not Args.OutDir:\r
+        Args.OutputFile = os.path.join(\r
+          os.path.dirname(Args.InputFile),\r
+          BaseName + ".hex"\r
+          )\r
+    else:\r
+        if not os.path.exists(Args.OutDir):\r
+            os.mkdir(Args.OutDir)\r
+        Args.OutputFile = os.path.join(Args.OutDir, BaseName + ".hex")\r
+\r
+    Args.BaseName = BaseName\r
+\r
+    return Args\r
+\r
+## Convert an AML file to a .hex file containing the AML bytecode stored\r
+#  in a C array.\r
+#\r
+# @param  InputFile     Path to the input AML file.\r
+# @param  OutputFile    Path to the output .hex file to generate.\r
+# @param  BaseName      Base name of the input file.\r
+#                       This is also the name of the generated .hex file.\r
+#\r
+def AmlToHex(InputFile, OutputFile, BaseName):\r
+\r
+    MacroName = "__{}_HEX__".format(BaseName.upper())\r
+    ArrayName = BaseName.lower() + "_aml_code"\r
+\r
+    with open(InputFile, "rb") as fIn, open(OutputFile, "w") as fOut:\r
+        # Write header.\r
+        fOut.write("// This file has been generated from:\n" + \\r
+                   "// \tPython script: " + \\r
+                   os.path.abspath(__file__) + "\n" + \\r
+                   "// \tInput AML file: " + \\r
+                   os.path.abspath(InputFile) + "\n\n" + \\r
+                   "#ifndef {}\n".format(MacroName) + \\r
+                   "#define {}\n\n".format(MacroName)\r
+                   )\r
+\r
+        # Write the array and its content.\r
+        fOut.write("unsigned char {}[] = {{\n  ".format(ArrayName))\r
+        cnt = 0\r
+        byte = fIn.read(1)\r
+        while len(byte) != 0:\r
+            fOut.write("0x{0:02X}, ".format(ord(byte)))\r
+            cnt += 1\r
+            if (cnt % 8) == 0:\r
+                fOut.write("\n  ")\r
+            byte = fIn.read(1)\r
+        fOut.write("\n};\n")\r
+\r
+        # Write footer.\r
+        fOut.write("#endif // {}\n".format(MacroName))\r
+\r
+## Main method\r
+#\r
+# This method:\r
+#   1-  Initialize an EdkLogger instance.\r
+#   2-  Parses the input arguments.\r
+#   3-  Converts an AML file to a .hex file containing the AML bytecode stored\r
+#       in a C array.\r
+#\r
+# @retval 0     Success.\r
+# @retval 1     Error.\r
+#\r
+def Main():\r
+    # Initialize an EdkLogger instance.\r
+    EdkLogger.Initialize()\r
+\r
+    try:\r
+        # Parse the input arguments.\r
+        CommandArguments = ParseArgs()\r
+        if not CommandArguments:\r
+            return 1\r
+\r
+        # Convert an AML file to a .hex file containing the AML bytecode stored\r
+        # in a C array.\r
+        AmlToHex(CommandArguments.InputFile, CommandArguments.OutputFile,\r
+                          CommandArguments.BaseName)\r
+    except Exception as e:\r
+        print(e)\r
+        return 1\r
+\r
+    return 0\r
+\r
+if __name__ == '__main__':\r
+    r = Main()\r
+    # 0-127 is a safe return range, and 1 is a standard default error\r
+    if r < 0 or r > 127: r = 1\r
+    sys.exit(r)\r