]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/AmlToHex/AmlToHex.py
BaseTools: Generate multiple rules when multiple output files
[mirror_edk2.git] / BaseTools / Source / Python / AmlToHex / AmlToHex.py
1 ## @file
2 #
3 # Convert an AML file to a .hex file containing the AML bytecode stored in a
4 # C array.
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
7 # the AML bytecode.
8 #
9 # Copyright (c) 2020, ARM Limited. All rights reserved.<BR>
10 #
11 # SPDX-License-Identifier: BSD-2-Clause-Patent
12 #
13
14 import argparse
15 import Common.EdkLogger as EdkLogger
16 from Common.BuildToolError import *
17 import sys
18 import os
19
20 ## Parse the command line arguments.
21 #
22 # @retval A argparse.NameSpace instance, containing parsed values.
23 #
24 def ParseArgs():
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."
33 )
34
35 # Define the possible arguments.
36 Parser.add_argument(
37 dest="InputFile",
38 help="Path to an input AML file to generate a .hex file from."
39 )
40 Parser.add_argument(
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."
44 )
45
46 # Parse the input arguments.
47 Args = Parser.parse_args()
48 SplitInputName = ""
49
50 if not os.path.exists(Args.InputFile):
51 EdkLogger.error(__file__, FILE_OPEN_FAILURE,
52 ExtraData=Args.InputFile)
53 return None
54 else:
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)
61 return None
62
63 # Get the basename of the input file.
64 SplitInputName = os.path.splitext(Args.InputFile)
65 BaseName = os.path.basename(SplitInputName[0])
66
67 # If no output directory is specified, output to the input directory.
68 if not Args.OutDir:
69 Args.OutputFile = os.path.join(
70 os.path.dirname(Args.InputFile),
71 BaseName + ".hex"
72 )
73 else:
74 if not os.path.exists(Args.OutDir):
75 os.mkdir(Args.OutDir)
76 Args.OutputFile = os.path.join(Args.OutDir, BaseName + ".hex")
77
78 Args.BaseName = BaseName
79
80 return Args
81
82 ## Convert an AML file to a .hex file containing the AML bytecode stored
83 # in a C array.
84 #
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.
89 #
90 def AmlToHex(InputFile, OutputFile, BaseName):
91
92 MacroName = "__{}_HEX__".format(BaseName.upper())
93 ArrayName = BaseName.lower() + "_aml_code"
94
95 with open(InputFile, "rb") as fIn, open(OutputFile, "w") as fOut:
96 # Write header.
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)
104 )
105
106 # Write the array and its content.
107 fOut.write("unsigned char {}[] = {{\n ".format(ArrayName))
108 cnt = 0
109 byte = fIn.read(1)
110 while len(byte) != 0:
111 fOut.write("0x{0:02X}, ".format(ord(byte)))
112 cnt += 1
113 if (cnt % 8) == 0:
114 fOut.write("\n ")
115 byte = fIn.read(1)
116 fOut.write("\n};\n")
117
118 # Write footer.
119 fOut.write("#endif // {}\n".format(MacroName))
120
121 ## Main method
122 #
123 # This method:
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
127 # in a C array.
128 #
129 # @retval 0 Success.
130 # @retval 1 Error.
131 #
132 def Main():
133 # Initialize an EdkLogger instance.
134 EdkLogger.Initialize()
135
136 try:
137 # Parse the input arguments.
138 CommandArguments = ParseArgs()
139 if not CommandArguments:
140 return 1
141
142 # Convert an AML file to a .hex file containing the AML bytecode stored
143 # in a C array.
144 AmlToHex(CommandArguments.InputFile, CommandArguments.OutputFile,
145 CommandArguments.BaseName)
146 except Exception as e:
147 print(e)
148 return 1
149
150 return 0
151
152 if __name__ == '__main__':
153 r = 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
156 sys.exit(r)