treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / scripts / dtc / dtx_diff
blob541c432e7d1994575e247a686e9a9937aa924bbe
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0-only
4 # Copyright (C) 2015 Frank Rowand
8 usage() {
10 # use spaces instead of tabs in the usage message
11 cat >&2 <<eod
13 Usage:
15 `basename $0` DTx
16 decompile DTx
18 `basename $0` DTx_1 DTx_2
19 diff DTx_1 and 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)
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
36 Each DTx is processed by the dtc compiler to produce a sorted dts source
37 file. If DTx is a dts source file then it is pre-processed in the same
38 manner as done for the compile of the dts source file in the Linux kernel
39 build system ('#include' and '/include/' directives are processed).
41 If two DTx are provided, the resulting dts source files are diffed.
43 If DTx is a directory, it is treated as a DT subtree, such as
44 /proc/device-tree.
46 If DTx contains the binary blob magic value in the first four bytes,
47 it is treated as a binary blob (aka .dtb or FDT).
49 Otherwise DTx is treated as a dts source file (aka .dts).
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.
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.
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:
64 `basename $0` \\
65 <(ARCH=arch_of_dtx_1 `basename $0` DTx_1) \\
66 <(ARCH=arch_of_dtx_2 `basename $0` DTx_2)
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
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:
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
84 eod
88 compile_to_dts() {
90 dtx="$1"
91 dtc_include="$2"
93 if [ -d "${dtx}" ] ; then
95 # ----- input is file tree
97 if ( ! ${DTC} -I fs ${dtx} ) ; then
98 exit 3
101 elif [ -f "${dtx}" ] && [ -r "${dtx}" ] ; then
103 magic=`hexdump -n 4 -e '/1 "%02x"' ${dtx}`
104 if [ "${magic}" = "d00dfeed" ] ; then
106 # ----- input is FDT (binary blob)
108 if ( ! ${DTC} -I dtb ${dtx} ) ; then
109 exit 3
112 return
116 # ----- input is DTS (source)
118 if ( cpp ${cpp_flags} -x assembler-with-cpp ${dtx} \
119 | ${DTC} ${dtc_include} -I dts ) ; then
120 return
123 echo "" >&2
124 echo "Possible hints to resolve the above error:" >&2
125 echo " (hints might not fix the problem)" >&2
127 hint_given=0
129 if [ "${ARCH}" = "" ] ; then
130 hint_given=1
131 echo "" >&2
132 echo " shell variable \$ARCH not set" >&2
135 dtx_arch=`echo "/${dtx}" | sed -e 's|.*/arch/||' -e 's|/.*||'`
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
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
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
158 if [ $hint_given = 0 ] ; then
159 echo "" >&2
160 echo " No hints available." >&2
163 echo "" >&2
165 exit 3
167 else
168 echo "" >&2
169 echo "ERROR: ${dtx} does not exist or is not readable" >&2
170 echo "" >&2
171 exit 2
177 # ----- start of script
179 annotate=""
180 cmd_diff=0
181 diff_flags="-u"
182 diff_color=""
183 dtx_file_1=""
184 dtx_file_2=""
185 dtc_sort="-s"
186 help=0
187 srctree=""
190 while [ $# -gt 0 ] ; do
192 case $1 in
194 -c | --color )
195 if diff --color /dev/null /dev/null 2>/dev/null ; then
196 diff_color="--color=always"
198 shift
201 -f )
202 diff_flags="--unified=999999"
203 shift
206 -h | -help | --help )
207 help=1
208 shift
211 -s )
212 srctree="$2"
213 shift 2
216 -S )
217 git_root=`git rev-parse --show-toplevel 2>/dev/null`
218 srctree="${git_root}"
219 shift
222 -T | --annotate )
223 if [ "${annotate}" = "" ] ; then
224 annotate="-T"
225 elif [ "${annotate}" = "-T" ] ; then
226 annotate="-T -T"
228 shift
230 -u )
231 dtc_sort=""
232 shift
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
246 shift
249 esac
251 done
253 if [ "${srctree}" = "" ] ; then
254 srctree="."
257 if [ "${dtx_file_2}" != "" ]; then
258 cmd_diff=1
261 if (( ${help} )) ; then
262 usage
263 exit 1
266 # this must follow check for ${help}
267 if [ "${dtx_file_1}" = "" ]; then
268 echo "" >&2
269 echo "ERROR: parameter DTx required" >&2
270 echo "" >&2
271 exit 2
275 # ----- prefer dtc from linux kernel, allow fallback to dtc in $PATH
277 if [ "${KBUILD_OUTPUT:0:2}" = ".." ] ; then
278 __KBUILD_OUTPUT="${srctree}/${KBUILD_OUTPUT}"
279 elif [ "${KBUILD_OUTPUT}" = "" ] ; then
280 __KBUILD_OUTPUT="."
281 else
282 __KBUILD_OUTPUT="${KBUILD_OUTPUT}"
285 DTC="${__KBUILD_OUTPUT}/scripts/dtc/dtc"
287 if [ ! -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'
297 if ( ! which ${__DTC} >/dev/null ) ; then
299 # use spaces instead of tabs in the error message
300 cat >&2 <<eod
302 ERROR: unable to find a 'dtc' program
304 Preferred 'dtc' (built from Linux kernel source tree) was not found or
305 is not executable.
307 'dtc' is: ${DTC}
309 If it does not exist, create it from the root of the Linux source tree:
310 ${make_command}
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'.
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.
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.
322 Fallback '${__DTC}' was also not in \${PATH} or is not executable.
325 exit 2
327 DTC=${__DTC}
331 # ----- cpp and dtc flags same as for linux source tree build of .dtb files,
332 # plus directories of the dtx file(s)
334 dtx_path_1_dtc_include="-i `dirname ${dtx_file_1}`"
336 dtx_path_2_dtc_include=""
337 if (( ${cmd_diff} )) ; then
338 dtx_path_2_dtc_include="-i `dirname ${dtx_file_2}`"
341 cpp_flags="\
342 -nostdinc \
343 -I${srctree}/scripts/dtc/include-prefixes \
344 -undef -D__DTS__"
346 DTC="\
347 ${DTC} \
348 -i ${srctree}/scripts/dtc/include-prefixes \
349 -O dts -qq -f ${dtc_sort} ${annotate} -o -"
352 # ----- do the diff or decompile
354 if (( ${cmd_diff} )) ; then
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}")
360 else
362 compile_to_dts "${dtx_file_1}" "${dtx_path_1_dtc_include}"