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