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 -f print full dts in diff (--unified=99999)
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
34 Each DTx is processed by the dtc compiler to produce a sorted dts source
35 file. If DTx is a dts source file then it is pre-processed in the same
36 manner as done for the compile of the dts source file in the Linux kernel
37 build system ('#include' and '/include/' directives are processed).
39 If two DTx are provided, the resulting dts source files are diffed.
41 If DTx is a directory, it is treated as a DT subtree, such as
44 If DTx contains the binary blob magic value in the first four bytes,
45 it is treated as a binary blob (aka .dtb or FDT).
47 Otherwise DTx is treated as a dts source file (aka .dts).
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.
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.
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
63 <(ARCH=arch_of_dtx_1 `basename $0` DTx_1) \\
64 <(ARCH=arch_of_dtx_2 `basename $0` DTx_2)
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
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
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
91 if [ -d "${dtx}" ] ; then
93 # ----- input is file tree
95 if ( ! ${DTC} -I fs
${dtx} ) ; then
99 elif [ -f "${dtx}" ] && [ -r "${dtx}" ] ; then
101 magic
=`hexdump -n 4 -e '/1 "%02x"' ${dtx}`
102 if [ "${magic}" = "d00dfeed" ] ; then
104 # ----- input is FDT (binary blob)
106 if ( ! ${DTC} -I dtb
${dtx} ) ; then
114 # ----- input is DTS (source)
116 if ( cpp
${cpp_flags} -x assembler-with-cpp
${dtx} \
117 |
${DTC} ${dtc_include} -I dts
) ; then
122 echo "Possible hints to resolve the above error:" >&2
123 echo " (hints might not fix the problem)" >&2
127 if [ "${ARCH}" = "" ] ; then
130 echo " shell variable \$ARCH not set" >&2
133 dtx_arch
=`echo "/${dtx}" | sed -e 's|.*/arch/||' -e 's|/.*||'`
135 if [ "${dtx_arch}" != "" -a "${dtx_arch}" != "${ARCH}" ] ; then
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
143 if [ ! -d ${srctree}/arch
/${ARCH} ] ; then
146 echo " ${srctree}/arch/${ARCH}/ does not exist" >&2
147 echo " Is \$ARCH='${ARCH}' correct?" >&2
148 echo " Possible fix: use '-s' option" >&2
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
156 if [ $hint_given = 0 ] ; then
158 echo " No hints available." >&2
167 echo "ERROR: ${dtx} does not exist or is not readable" >&2
175 # ----- start of script
187 while [ $# -gt 0 ] ; do
192 diff_flags
="--unified=999999"
196 -h |
-help |
--help )
207 git_root
=`git rev-parse --show-toplevel 2>/dev/null`
208 srctree
="${git_root}"
213 if [ "${annotate}" = "" ] ; then
215 elif [ "${annotate}" = "-T" ] ; then
226 if [ "${dtx_file_1}" = "" ] ; then
228 elif [ "${dtx_file_2}" = "" ] ; then
232 echo "ERROR: Unexpected parameter: $1" >&2
243 if [ "${srctree}" = "" ] ; then
247 if [ "${dtx_file_2}" != "" ]; then
251 if (( ${help} )) ; then
256 # this must follow check for ${help}
257 if [ "${dtx_file_1}" = "" ]; then
259 echo "ERROR: parameter DTx required" >&2
265 # ----- prefer dtc from linux kernel, allow fallback to dtc in $PATH
267 if [ "${KBUILD_OUTPUT:0:2}" = ".." ] ; then
268 __KBUILD_OUTPUT
="${srctree}/${KBUILD_OUTPUT}"
269 elif [ "${KBUILD_OUTPUT}" = "" ] ; then
272 __KBUILD_OUTPUT
="${KBUILD_OUTPUT}"
275 DTC
="${__KBUILD_OUTPUT}/scripts/dtc/dtc"
277 if [ ! -x ${DTC} ] ; then
279 if grep -q "^CONFIG_DTC=y" ${__KBUILD_OUTPUT}/.config
2>/dev
/null
; then
284 Enable CONFIG_DTC in the kernel configuration
287 if ( ! which ${__DTC} >/dev
/null
) ; then
289 # use spaces instead of tabs in the error message
292 ERROR: unable to find a 'dtc' program
294 Preferred 'dtc' (built from Linux kernel source tree) was not found or
299 If it does not exist, create it from the root of the Linux source tree:
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'.
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
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.
312 Fallback '${__DTC}' was also not in \${PATH} or is not executable.
321 # ----- cpp and dtc flags same as for linux source tree build of .dtb files,
322 # plus directories of the dtx file(s)
324 dtx_path_1_dtc_include
="-i `dirname ${dtx_file_1}`"
326 dtx_path_2_dtc_include
=""
327 if (( ${cmd_diff} )) ; then
328 dtx_path_2_dtc_include
="-i `dirname ${dtx_file_2}`"
333 -I${srctree}/scripts/dtc/include-prefixes \
338 -i ${srctree}/scripts/dtc/include-prefixes \
339 -O dts -qq -f ${dtc_sort} ${annotate} -o -"
342 # ----- do the diff or decompile
344 if (( ${cmd_diff} )) ; then
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}")
352 compile_to_dts "${dtx_file_1}" "${dtx_path_1_dtc_include}"