]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/AmlToHex/AmlToHex.py
3 # Convert an AML file to a .hex file containing the AML bytecode stored in a
5 # By default, "Tables\Dsdt.aml" will generate "Tables\Dsdt.hex".
6 # "Tables\Dsdt.hex" will contain a C array named "dsdt_aml_code" that contains
9 # Copyright (c) 2020, ARM Limited. All rights reserved.<BR>
11 # SPDX-License-Identifier: BSD-2-Clause-Patent
15 import Common
.EdkLogger
as EdkLogger
16 from Common
.BuildToolError
import *
20 ## Parse the command line arguments.
22 # @retval A argparse.NameSpace instance, containing parsed values.
25 # Initialize the parser.
26 Parser
= argparse
.ArgumentParser(
27 description
="Convert an AML file to a .hex file containing the AML " + \
28 "bytecode stored in a C array. By default, " + \
29 "\"Tables\\Dsdt.aml\" will generate" + \
30 "\"Tables\\Dsdt.hex\". \"Tables\\Dsdt.hex\" will " + \
31 "contain a C array named \"dsdt_aml_code\" that " + \
32 "contains the AML bytecode."
35 # Define the possible arguments.
38 help="Path to an input AML file to generate a .hex file from."
41 "-o", "--out-dir", dest
="OutDir",
42 help="Output directory where the .hex file will be generated. " + \
43 "Default is the input file's directory."
46 # Parse the input arguments.
47 Args
= Parser
.parse_args()
50 if not os
.path
.exists(Args
.InputFile
):
51 EdkLogger
.error(__file__
, FILE_OPEN_FAILURE
,
52 ExtraData
=Args
.InputFile
)
55 with
open(Args
.InputFile
, "rb") as fIn
:
56 Signature
= str(fIn
.read(4))
57 if ("DSDT" not in Signature
) and ("SSDT" not in Signature
):
58 EdkLogger
.info("Invalid file type. " + \
59 "File does not have a valid " + \
60 "DSDT or SSDT signature: %s" % Args
.InputFile
)
63 # Get the basename of the input file.
64 SplitInputName
= os
.path
.splitext(Args
.InputFile
)
65 BaseName
= os
.path
.basename(SplitInputName
[0])
67 # If no output directory is specified, output to the input directory.
69 Args
.OutputFile
= os
.path
.join(
70 os
.path
.dirname(Args
.InputFile
),
74 if not os
.path
.exists(Args
.OutDir
):
76 Args
.OutputFile
= os
.path
.join(Args
.OutDir
, BaseName
+ ".hex")
78 Args
.BaseName
= BaseName
82 ## Convert an AML file to a .hex file containing the AML bytecode stored
85 # @param InputFile Path to the input AML file.
86 # @param OutputFile Path to the output .hex file to generate.
87 # @param BaseName Base name of the input file.
88 # This is also the name of the generated .hex file.
90 def AmlToHex(InputFile
, OutputFile
, BaseName
):
92 MacroName
= "__{}_HEX__".format(BaseName
.upper())
93 ArrayName
= BaseName
.lower() + "_aml_code"
95 with
open(InputFile
, "rb") as fIn
, open(OutputFile
, "w") as fOut
:
97 fOut
.write("// This file has been generated from:\n" + \
98 "// \tPython script: " + \
99 os
.path
.abspath(__file__
) + "\n" + \
100 "// \tInput AML file: " + \
101 os
.path
.abspath(InputFile
) + "\n\n" + \
102 "#ifndef {}\n".format(MacroName
) + \
103 "#define {}\n\n".format(MacroName
)
106 # Write the array and its content.
107 fOut
.write("unsigned char {}[] = {{\n ".format(ArrayName
))
110 while len(byte
) != 0:
111 fOut
.write("0x{0:02X}, ".format(ord(byte
)))
119 fOut
.write("#endif // {}\n".format(MacroName
))
124 # 1- Initialize an EdkLogger instance.
125 # 2- Parses the input arguments.
126 # 3- Converts an AML file to a .hex file containing the AML bytecode stored
133 # Initialize an EdkLogger instance.
134 EdkLogger
.Initialize()
137 # Parse the input arguments.
138 CommandArguments
= ParseArgs()
139 if not CommandArguments
:
142 # Convert an AML file to a .hex file containing the AML bytecode stored
144 AmlToHex(CommandArguments
.InputFile
, CommandArguments
.OutputFile
,
145 CommandArguments
.BaseName
)
146 except Exception as e
:
152 if __name__
== '__main__':
154 # 0-127 is a safe return range, and 1 is a standard default error
155 if r
< 0 or r
> 127: r
= 1