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