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