• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#! /bin/bash
2# SPDX-License-Identifier: GPL-2.0-only
3
4# Copyright (C) 2015 Frank Rowand
5#
6
7
8usage() {
9
10	# use spaces instead of tabs in the usage message
11	cat >&2 <<eod
12
13Usage:
14
15   `basename $0` DTx
16        decompile DTx
17
18   `basename $0` DTx_1 DTx_2
19        diff DTx_1 and DTx_2
20
21
22      --annotate    synonym for -T
23       -f           print full dts in diff (--unified=99999)
24       -h           synonym for --help
25       -help        synonym for --help
26      --help        print this message and exit
27       -s SRCTREE   linux kernel source tree is at path SRCTREE
28                        (default is current directory)
29       -S           linux kernel source tree is at root of current git repo
30       -T           Annotate output .dts with input source file and line (-T -T for more details)
31       -u           unsorted, do not sort DTx
32
33
34Each DTx is processed by the dtc compiler to produce a sorted dts source
35file.  If DTx is a dts source file then it is pre-processed in the same
36manner as done for the compile of the dts source file in the Linux kernel
37build system ('#include' and '/include/' directives are processed).
38
39If two DTx are provided, the resulting dts source files are diffed.
40
41If DTx is a directory, it is treated as a DT subtree, such as
42  /proc/device-tree.
43
44If DTx contains the binary blob magic value in the first four bytes,
45  it is treated as a binary blob (aka .dtb or FDT).
46
47Otherwise DTx is treated as a dts source file (aka .dts).
48
49   If this script is not run from the root of the linux source tree,
50   and DTx utilizes '#include' or '/include/' then the path of the
51   linux source tree can be provided by '-s SRCTREE' or '-S' so that
52   include paths will be set properly.
53
54   The shell variable \${ARCH} must provide the architecture containing
55   the dts source file for include paths to be set properly for '#include'
56   or '/include/' to be processed.
57
58   If DTx_1 and DTx_2 are in different architectures, then this script
59   may not work since \${ARCH} is part of the include path.  The following
60   workaround can be used:
61
62      `basename $0` ARCH=arch_of_dtx_1 DTx_1 >tmp_dtx_1.dts
63      `basename $0` ARCH=arch_of_dtx_2 DTx_2 >tmp_dtx_2.dts
64      `basename $0` tmp_dtx_1.dts tmp_dtx_2.dts
65      rm tmp_dtx_1.dts tmp_dtx_2.dts
66
67   If DTx_1 and DTx_2 are in different directories, then this script will
68   add the path of DTx_1 and DTx_2 to the include paths.  If DTx_2 includes
69   a local file that exists in both the path of DTx_1 and DTx_2 then the
70   file in the path of DTx_1 will incorrectly be included.  Possible
71   workaround:
72
73      `basename $0` DTx_1 >tmp_dtx_1.dts
74      `basename $0` DTx_2 >tmp_dtx_2.dts
75      `basename $0` tmp_dtx_1.dts tmp_dtx_2.dts
76      rm tmp_dtx_1.dts tmp_dtx_2.dts
77
78eod
79}
80
81
82compile_to_dts() {
83
84	dtx="$1"
85	dtc_include="$2"
86
87	if [ -d "${dtx}" ] ; then
88
89		# -----  input is file tree
90
91		if ( ! ${DTC} -I fs ${dtx} ) ; then
92			exit 3
93		fi
94
95	elif [ -f "${dtx}" ] && [ -r "${dtx}" ] ; then
96
97		magic=`hexdump -n 4 -e '/1 "%02x"' ${dtx}`
98		if [ "${magic}" = "d00dfeed" ] ; then
99
100			# -----  input is FDT (binary blob)
101
102			if ( ! ${DTC} -I dtb ${dtx} ) ; then
103				exit 3
104			fi
105
106			return
107
108		fi
109
110		# -----  input is DTS (source)
111
112		if ( cpp ${cpp_flags} -x assembler-with-cpp ${dtx} \
113			| ${DTC} ${dtc_include} -I dts ) ; then
114			return
115		fi
116
117		echo ""                                                      >&2
118		echo "Possible hints to resolve the above error:"            >&2
119		echo "  (hints might not fix the problem)"                   >&2
120
121		hint_given=0
122
123		if [ "${ARCH}" = "" ] ; then
124			hint_given=1
125			echo ""                                              >&2
126			echo "  shell variable \$ARCH not set"               >&2
127		fi
128
129		dtx_arch=`echo "/${dtx}" | sed -e 's|.*/arch/||' -e 's|/.*||'`
130
131		if [ "${dtx_arch}" != ""  -a "${dtx_arch}" != "${ARCH}" ] ; then
132			hint_given=1
133			echo ""                                              >&2
134			echo "  architecture ${dtx_arch} is in file path,"   >&2
135			echo "  but does not match shell variable \$ARCH"    >&2
136			echo "  >>\$ARCH<< is: >>${ARCH}<<"                  >&2
137		fi
138
139		if [ ! -d ${srctree}/arch/${ARCH} ] ; then
140			hint_given=1
141			echo ""                                              >&2
142			echo "  ${srctree}/arch/${ARCH}/ does not exist"     >&2
143			echo "  Is \$ARCH='${ARCH}' correct?"                >&2
144			echo "  Possible fix: use '-s' option"               >&2
145
146			git_root=`git rev-parse --show-toplevel 2>/dev/null`
147			if [ -d ${git_root}/arch/ ] ; then
148				echo "  Possible fix: use '-S' option"       >&2
149			fi
150		fi
151
152		if [ $hint_given = 0 ] ; then
153			echo ""                                              >&2
154			echo "  No hints available."                         >&2
155		fi
156
157		echo ""                                                      >&2
158
159		exit 3
160
161	else
162		echo ""                                                     >&2
163		echo "ERROR: ${dtx} does not exist or is not readable"      >&2
164		echo ""                                                     >&2
165		exit 2
166	fi
167
168}
169
170
171# -----  start of script
172
173annotate=""
174cmd_diff=0
175diff_flags="-u"
176dtx_file_1=""
177dtx_file_2=""
178dtc_sort="-s"
179help=0
180srctree=""
181
182
183while [ $# -gt 0 ] ; do
184
185	case $1 in
186
187	-f )
188		diff_flags="--unified=999999"
189		shift
190		;;
191
192	-h | -help | --help )
193		help=1
194		shift
195		;;
196
197	-s )
198		srctree="$2"
199		shift 2
200		;;
201
202	-S )
203		git_root=`git rev-parse --show-toplevel 2>/dev/null`
204		srctree="${git_root}"
205		shift
206		;;
207
208	-T | --annotate )
209		if [ "${annotate}"  = "" ] ; then
210			annotate="-T"
211		elif [ "${annotate}"  = "-T" ] ; then
212			annotate="-T -T"
213		fi
214		shift
215		;;
216	-u )
217		dtc_sort=""
218		shift
219		;;
220
221	*)
222		if [ "${dtx_file_1}"  = "" ] ; then
223			dtx_file_1="$1"
224		elif [ "${dtx_file_2}" = "" ] ; then
225			dtx_file_2="$1"
226		else
227			echo ""                                             >&2
228			echo "ERROR: Unexpected parameter: $1"              >&2
229			echo ""                                             >&2
230			exit 2
231		fi
232		shift
233		;;
234
235	esac
236
237done
238
239if [ "${srctree}" = "" ] ; then
240	srctree="."
241fi
242
243if [ "${dtx_file_2}" != "" ]; then
244	cmd_diff=1
245fi
246
247if (( ${help} )) ; then
248	usage
249	exit 1
250fi
251
252# this must follow check for ${help}
253if [ "${dtx_file_1}" = "" ]; then
254	echo ""                                                             >&2
255	echo "ERROR: parameter DTx required"                                >&2
256	echo ""                                                             >&2
257	exit 2
258fi
259
260
261# -----  prefer dtc from linux kernel, allow fallback to dtc in $PATH
262
263if [ "${KBUILD_OUTPUT:0:2}" = ".." ] ; then
264	__KBUILD_OUTPUT="${srctree}/${KBUILD_OUTPUT}"
265elif [ "${KBUILD_OUTPUT}" = "" ] ; then
266	__KBUILD_OUTPUT="."
267else
268	__KBUILD_OUTPUT="${KBUILD_OUTPUT}"
269fi
270
271DTC="${__KBUILD_OUTPUT}/scripts/dtc/dtc"
272
273if [ ! -x ${DTC} ] ; then
274	__DTC="dtc"
275	if grep -q "^CONFIG_DTC=y" ${__KBUILD_OUTPUT}/.config 2>/dev/null; then
276		make_command='
277         make scripts'
278	else
279		make_command='
280         Enable CONFIG_DTC in the kernel configuration
281         make scripts'
282	fi
283	if ( ! which ${__DTC} >/dev/null ) ; then
284
285		# use spaces instead of tabs in the error message
286		cat >&2 <<eod
287
288ERROR: unable to find a 'dtc' program
289
290   Preferred 'dtc' (built from Linux kernel source tree) was not found or
291   is not executable.
292
293      'dtc' is: ${DTC}
294
295      If it does not exist, create it from the root of the Linux source tree:
296${make_command}
297
298      If not at the root of the Linux kernel source tree -s SRCTREE or -S
299      may need to be specified to find 'dtc'.
300
301      If 'O=\${dir}' is specified in your Linux builds, this script requires
302      'export KBUILD_OUTPUT=\${dir}' or add \${dir}/scripts/dtc to \$PATH
303      before running.
304
305      If \${KBUILD_OUTPUT} is a relative path, then '-s SRCDIR', -S, or run
306      this script from the root of the Linux kernel source tree is required.
307
308   Fallback '${__DTC}' was also not in \${PATH} or is not executable.
309
310eod
311		exit 2
312	fi
313	DTC=${__DTC}
314fi
315
316
317# -----  cpp and dtc flags same as for linux source tree build of .dtb files,
318#        plus directories of the dtx file(s)
319
320dtx_path_1_dtc_include="-i `dirname ${dtx_file_1}`"
321
322dtx_path_2_dtc_include=""
323if (( ${cmd_diff} )) ; then
324	dtx_path_2_dtc_include="-i `dirname ${dtx_file_2}`"
325fi
326
327cpp_flags="\
328	-nostdinc                                  \
329	-I${srctree}/scripts/dtc/include-prefixes  \
330	-undef -D__DTS__"
331
332DTC="\
333	${DTC}                                     \
334	-i ${srctree}/scripts/dtc/include-prefixes \
335	-O dts -qq -f ${dtc_sort} ${annotate} -o -"
336
337
338# -----  do the diff or decompile
339
340if (( ${cmd_diff} )) ; then
341
342	diff ${diff_flags} --label "${dtx_file_1}" --label "${dtx_file_2}" \
343		<(compile_to_dts "${dtx_file_1}" "${dtx_path_1_dtc_include}") \
344		<(compile_to_dts "${dtx_file_2}" "${dtx_path_2_dtc_include}")
345
346else
347
348	compile_to_dts "${dtx_file_1}" "${dtx_path_1_dtc_include}"
349
350fi
351