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