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