2 # SPDX-License-Identifier: GPL-2.0-only
4 # Copyright (C) 2015 Frank Rowand
10 # use spaces instead of tabs in the usage message
18 `basename $0` DTx_1 DTx_2
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)
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
33 (-T -T for more details)
34 -u unsorted, do not sort DTx
37 Each DTx is processed by the dtc compiler to produce a sorted dts source
38 file. If DTx is a dts source file then it is pre-processed in the same
39 manner as done for the compile of the dts source file in the Linux kernel
40 build system ('#include' and '/include/' directives are processed).
42 If two DTx are provided, the resulting dts source files are diffed.
44 If DTx is a directory, it is treated as a DT subtree, such as
47 If DTx contains the binary blob magic value in the first four bytes,
48 it is treated as a binary blob (aka .dtb or FDT).
50 Otherwise DTx is treated as a dts source file (aka .dts).
52 If this script is not run from the root of the linux source tree,
53 and DTx utilizes '#include' or '/include/' then the path of the
54 linux source tree can be provided by '-s SRCTREE' or '-S' so that
55 include paths will be set properly.
57 The shell variable \${ARCH} must provide the architecture containing
58 the dts source file for include paths to be set properly for '#include'
59 or '/include/' to be processed.
61 If DTx_1 and DTx_2 are in different architectures, then this script
62 may not work since \${ARCH} is part of the include path. The following
63 workaround can be used:
65 `basename $0` ARCH=arch_of_dtx_1 DTx_1 >tmp_dtx_1.dts
66 `basename $0` ARCH=arch_of_dtx_2 DTx_2 >tmp_dtx_2.dts
67 `basename $0` tmp_dtx_1.dts tmp_dtx_2.dts
68 rm tmp_dtx_1.dts tmp_dtx_2.dts
70 If DTx_1 and DTx_2 are in different directories, then this script will
71 add the path of DTx_1 and DTx_2 to the include paths. If DTx_2 includes
72 a local file that exists in both the path of DTx_1 and DTx_2 then the
73 file in the path of DTx_1 will incorrectly be included. Possible
76 `basename $0` DTx_1 >tmp_dtx_1.dts
77 `basename $0` DTx_2 >tmp_dtx_2.dts
78 `basename $0` tmp_dtx_1.dts tmp_dtx_2.dts
79 rm tmp_dtx_1.dts tmp_dtx_2.dts
90 if [ -d "${dtx}" ] ; then
92 # ----- input is file tree
94 if ( ! ${DTC} -I fs
${dtx} ) ; then
98 elif [ -f "${dtx}" ] && [ -r "${dtx}" ] ; then
100 magic
=`hexdump -n 4 -e '/1 "%02x"' ${dtx}`
101 if [ "${magic}" = "d00dfeed" ] ; then
103 # ----- input is FDT (binary blob)
105 if ( ! ${DTC} -I dtb
${dtx} ) ; then
113 # ----- input is DTS (source)
115 if ( cpp
${cpp_flags} -x assembler-with-cpp
${dtx} \
116 |
${DTC} ${dtc_include} -I dts
) ; then
121 echo "Possible hints to resolve the above error:" >&2
122 echo " (hints might not fix the problem)" >&2
126 if [ "${ARCH}" = "" ] ; then
129 echo " shell variable \$ARCH not set" >&2
132 dtx_arch
=`echo "/${dtx}" | sed -e 's|.*/arch/||' -e 's|/.*||'`
134 if [ "${dtx_arch}" != "" -a "${dtx_arch}" != "${ARCH}" ] ; then
137 echo " architecture ${dtx_arch} is in file path," >&2
138 echo " but does not match shell variable \$ARCH" >&2
139 echo " >>\$ARCH<< is: >>${ARCH}<<" >&2
142 if [ ! -d ${srctree}/arch
/${ARCH} ] ; then
145 echo " ${srctree}/arch/${ARCH}/ does not exist" >&2
146 echo " Is \$ARCH='${ARCH}' correct?" >&2
147 echo " Possible fix: use '-s' option" >&2
149 git_root
=`git rev-parse --show-toplevel 2>/dev/null`
150 if [ -d ${git_root}/arch
/ ] ; then
151 echo " Possible fix: use '-S' option" >&2
155 if [ $hint_given = 0 ] ; then
157 echo " No hints available." >&2
166 echo "ERROR: ${dtx} does not exist or is not readable" >&2
174 # ----- start of script
187 while [ $# -gt 0 ] ; do
192 if diff --color /dev
/null
/dev
/null
2>/dev
/null
; then
193 diff_color
="--color=always"
199 diff_flags
="--unified=999999"
203 -h |
-help |
--help )
214 git_root
=`git rev-parse --show-toplevel 2>/dev/null`
215 srctree
="${git_root}"
220 if [ "${annotate}" = "" ] ; then
222 elif [ "${annotate}" = "-T" ] ; then
233 if [ "${dtx_file_1}" = "" ] ; then
235 elif [ "${dtx_file_2}" = "" ] ; then
239 echo "ERROR: Unexpected parameter: $1" >&2
250 if [ "${srctree}" = "" ] ; then
254 if [ "${dtx_file_2}" != "" ]; then
258 if (( ${help} )) ; then
263 # this must follow check for ${help}
264 if [ "${dtx_file_1}" = "" ]; then
266 echo "ERROR: parameter DTx required" >&2
272 # ----- prefer dtc from linux kernel, allow fallback to dtc in $PATH
274 if [ "${KBUILD_OUTPUT:0:2}" = ".." ] ; then
275 __KBUILD_OUTPUT
="${srctree}/${KBUILD_OUTPUT}"
276 elif [ "${KBUILD_OUTPUT}" = "" ] ; then
279 __KBUILD_OUTPUT
="${KBUILD_OUTPUT}"
282 DTC
="${__KBUILD_OUTPUT}/scripts/dtc/dtc"
284 if [ ! -x ${DTC} ] ; then
286 if grep -q "^CONFIG_DTC=y" ${__KBUILD_OUTPUT}/.config
2>/dev
/null
; then
291 Enable CONFIG_DTC in the kernel configuration
294 if ( ! which ${__DTC} >/dev
/null
) ; then
296 # use spaces instead of tabs in the error message
299 ERROR: unable to find a 'dtc' program
301 Preferred 'dtc' (built from Linux kernel source tree) was not found or
306 If it does not exist, create it from the root of the Linux source tree:
309 If not at the root of the Linux kernel source tree -s SRCTREE or -S
310 may need to be specified to find 'dtc'.
312 If 'O=\${dir}' is specified in your Linux builds, this script requires
313 'export KBUILD_OUTPUT=\${dir}' or add \${dir}/scripts/dtc to \$PATH
316 If \${KBUILD_OUTPUT} is a relative path, then '-s SRCDIR', -S, or run
317 this script from the root of the Linux kernel source tree is required.
319 Fallback '${__DTC}' was also not in \${PATH} or is not executable.
328 # ----- cpp and dtc flags same as for linux source tree build of .dtb files,
329 # plus directories of the dtx file(s)
331 dtx_path_1_dtc_include
="-i `dirname ${dtx_file_1}`"
333 dtx_path_2_dtc_include
=""
334 if (( ${cmd_diff} )) ; then
335 dtx_path_2_dtc_include
="-i `dirname ${dtx_file_2}`"
340 -I${srctree}/scripts/dtc/include-prefixes \
345 -i ${srctree}/scripts/dtc/include-prefixes \
346 -O dts -qq -f ${dtc_sort} ${annotate} -o -"
349 # ----- do the diff or decompile
351 if (( ${cmd_diff} )) ; then
353 diff ${diff_flags} ${diff_color} --label "${dtx_file_1}" --label "${dtx_file_2}" \
354 <(compile_to_dts "${dtx_file_1}" "${dtx_path_1_dtc_include}") \
355 <(compile_to_dts "${dtx_file_2}" "${dtx_path_2_dtc_include}")
359 compile_to_dts "${dtx_file_1}" "${dtx_path_1_dtc_include}"