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