Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

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