-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdtsc
executable file
·160 lines (133 loc) · 4.11 KB
/
dtsc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python3
"""
This script is part of BredOS-Tools, licenced under the GPL-3.0 licence.
Bill Sideris <bill88t@bredos.org>
"""
import os
import sys
import argparse
import tempfile
import subprocess
import shutil
def preprocess_dts(input_file: str, temp_file: str, include: str, kernel: str) -> bool:
"""
Preprocess the DTS file using the C preprocessor.
"""
linux_include_path = kernel
if not linux_include_path:
linux_include_path = next(
(
os.path.join("/usr/src", d)
for d in os.listdir("/usr/src")
if "linux" in d and os.path.isdir(os.path.join("/usr/src", d))
),
None,
)
if not linux_include_path:
print(
"Error: Could not find a valid Linux source include directory.",
file=sys.stderr,
)
return False
else:
print("Using kernel: " + linux_include_path)
cmd = [
"cpp",
"-nostdinc",
"-I",
os.path.join(linux_include_path, 'include'),
]
if include is not None:
cmd += ["-I", include]
cmd += [
"-undef",
"-x",
"assembler-with-cpp",
input_file,
temp_file,
]
print("Preprocessing...")
try:
subprocess.run(cmd, check=True)
except subprocess.CalledProcessError:
print("Error: Preprocessing FAILED!", file=sys.stderr)
return False
return True
def compile_dts(temp_file: str, output_file: str, include: str = None) -> bool:
"""
Compile the preprocessed DTS file into a DTBO or DTB.
"""
cmd = ["dtc"]
if include is not None:
cmd += ["-i", include]
cmd += ["-I", "dts", "-O", "dtb", temp_file, "-o", output_file]
print("Compiling...")
try:
subprocess.run(cmd, check=True)
except subprocess.CalledProcessError:
print("Error: Compiling FAILED!", file=sys.stderr)
return False
return True
def check_dependencies() -> None:
"""
Verify required tools are available on the system.
"""
if shutil.which("dtc") is None:
print(
"Error: The 'dtc' command is not installed or not in PATH.", file=sys.stderr
)
sys.exit(1)
def main():
parser = argparse.ArgumentParser(
description="Compile a Device Tree Source (DTS) file into a Device Tree Blob (DTBO or DTB).",
epilog="Example: ./compile_dts.py my_device_tree.dts -o output.dtbo",
)
parser.add_argument("input", nargs="?", help="Input DTS file")
parser.add_argument(
"-o",
"--output",
help="Output file (default: makes input_filename.dtb)",
default=None,
)
parser.add_argument(
"-i",
"--include",
help="Source of additional device tree files (optional)",
default=None,
)
parser.add_argument(
"-k",
"--kernel",
help="Manualy specify a kernel source path (default: autodetect)",
default=None,
)
args = parser.parse_args()
# Print help and exit if no arguments are provided
if not args.input:
parser.print_help()
sys.exit(1)
# Verify required tools
check_dependencies()
input_file = args.input
output_file = args.output or input_file.rsplit(".", 1)[0] + ".dtb"
include = args.include
kernel = args.kernel
if not os.path.isfile(input_file):
print(f"Error: Input file '{input_file}' does not exist.", file=sys.stderr)
sys.exit(1)
if not (input_file.endswith(".dts") or input_file.endswith(".dtsi")):
print("Error: Input file must be a device tree source file.", file=sys.stderr)
sys.exit(1)
with tempfile.NamedTemporaryFile(delete=False) as temp:
temp_file = temp.name
try:
if not preprocess_dts(input_file, temp_file, include, kernel):
sys.exit(1)
if not compile_dts(temp_file, output_file, include):
sys.exit(1)
finally:
if os.path.exists(temp_file):
os.remove(temp_file)
print(f"DTBO successfully created: {output_file}")
if __name__ == "__main__":
main()