dtx_diff 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. #! /bin/bash
  2. # Copyright (C) 2015 Frank Rowand
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; version 2 of the License.
  7. usage() {
  8. # use spaces instead of tabs in the usage message
  9. cat >&2 <<eod
  10. Usage:
  11. `basename $0` DTx
  12. decompile DTx
  13. `basename $0` DTx_1 DTx_2
  14. diff DTx_1 and DTx_2
  15. --color synonym for -c (requires diff with --color support)
  16. -c enable colored output
  17. -f print full dts in diff (--unified=99999)
  18. -h synonym for --help
  19. -help synonym for --help
  20. --help print this message and exit
  21. -s SRCTREE linux kernel source tree is at path SRCTREE
  22. (default is current directory)
  23. -S linux kernel source tree is at root of current git repo
  24. -u unsorted, do not sort DTx
  25. Each DTx is processed by the dtc compiler to produce a sorted dts source
  26. file. If DTx is a dts source file then it is pre-processed in the same
  27. manner as done for the compile of the dts source file in the Linux kernel
  28. build system ('#include' and '/include/' directives are processed).
  29. If two DTx are provided, the resulting dts source files are diffed.
  30. If DTx is a directory, it is treated as a DT subtree, such as
  31. /proc/device-tree.
  32. If DTx contains the binary blob magic value in the first four bytes,
  33. it is treated as a binary blob (aka .dtb or FDT).
  34. Otherwise DTx is treated as a dts source file (aka .dts).
  35. If this script is not run from the root of the linux source tree,
  36. and DTx utilizes '#include' or '/include/' then the path of the
  37. linux source tree can be provided by '-s SRCTREE' or '-S' so that
  38. include paths will be set properly.
  39. The shell variable \${ARCH} must provide the architecture containing
  40. the dts source file for include paths to be set properly for '#include'
  41. or '/include/' to be processed.
  42. If DTx_1 and DTx_2 are in different architectures, then this script
  43. may not work since \${ARCH} is part of the include path. The following
  44. workaround can be used:
  45. `basename $0` ARCH=arch_of_dtx_1 DTx_1 >tmp_dtx_1.dts
  46. `basename $0` ARCH=arch_of_dtx_2 DTx_2 >tmp_dtx_2.dts
  47. `basename $0` tmp_dtx_1.dts tmp_dtx_2.dts
  48. rm tmp_dtx_1.dts tmp_dtx_2.dts
  49. If DTx_1 and DTx_2 are in different directories, then this script will
  50. add the path of DTx_1 and DTx_2 to the include paths. If DTx_2 includes
  51. a local file that exists in both the path of DTx_1 and DTx_2 then the
  52. file in the path of DTx_1 will incorrectly be included. Possible
  53. workaround:
  54. `basename $0` DTx_1 >tmp_dtx_1.dts
  55. `basename $0` DTx_2 >tmp_dtx_2.dts
  56. `basename $0` tmp_dtx_1.dts tmp_dtx_2.dts
  57. rm tmp_dtx_1.dts tmp_dtx_2.dts
  58. eod
  59. }
  60. compile_to_dts() {
  61. dtx="$1"
  62. if [ -d "${dtx}" ] ; then
  63. # ----- input is file tree
  64. if ( ! ${DTC} -I fs ${dtx} ) ; then
  65. exit 3
  66. fi
  67. elif [ -f "${dtx}" ] && [ -r "${dtx}" ] ; then
  68. magic=`hexdump -n 4 -e '/1 "%02x"' ${dtx}`
  69. if [ "${magic}" = "d00dfeed" ] ; then
  70. # ----- input is FDT (binary blob)
  71. if ( ! ${DTC} -I dtb ${dtx} ) ; then
  72. exit 3
  73. fi
  74. return
  75. fi
  76. # ----- input is DTS (source)
  77. if ( cpp ${cpp_flags} -x assembler-with-cpp ${dtx} \
  78. | ${DTC} -I dts ) ; then
  79. return
  80. fi
  81. echo "" >&2
  82. echo "Possible hints to resolve the above error:" >&2
  83. echo " (hints might not fix the problem)" >&2
  84. hint_given=0
  85. if [ "${ARCH}" = "" ] ; then
  86. hint_given=1
  87. echo "" >&2
  88. echo " shell variable \$ARCH not set" >&2
  89. fi
  90. dtx_arch=`echo "/${dtx}" | sed -e 's|.*/arch/||' -e 's|/.*||'`
  91. if [ "${dtx_arch}" != "" -a "${dtx_arch}" != "${ARCH}" ] ; then
  92. hint_given=1
  93. echo "" >&2
  94. echo " architecture ${dtx_arch} is in file path," >&2
  95. echo " but does not match shell variable \$ARCH" >&2
  96. echo " >>\$ARCH<< is: >>${ARCH}<<" >&2
  97. fi
  98. if [ ! -d ${srctree}/arch/${ARCH} ] ; then
  99. hint_given=1
  100. echo "" >&2
  101. echo " ${srctree}/arch/${ARCH}/ does not exist" >&2
  102. echo " Is \$ARCH='${ARCH}' correct?" >&2
  103. echo " Possible fix: use '-s' option" >&2
  104. git_root=`git rev-parse --show-toplevel 2>/dev/null`
  105. if [ -d ${git_root}/arch/ ] ; then
  106. echo " Possible fix: use '-S' option" >&2
  107. fi
  108. fi
  109. if [ $hint_given = 0 ] ; then
  110. echo "" >&2
  111. echo " No hints available." >&2
  112. fi
  113. echo "" >&2
  114. exit 3
  115. else
  116. echo "" >&2
  117. echo "ERROR: ${dtx} does not exist or is not readable" >&2
  118. echo "" >&2
  119. exit 2
  120. fi
  121. }
  122. # ----- start of script
  123. cmd_diff=0
  124. diff_flags="-u"
  125. diff_color=""
  126. dtx_file_1=""
  127. dtx_file_2=""
  128. dtc_sort="-s"
  129. help=0
  130. srctree=""
  131. while [ $# -gt 0 ] ; do
  132. case $1 in
  133. -c | --color )
  134. if diff --color /dev/null /dev/null 2>/dev/null ; then
  135. diff_color="--color=always"
  136. fi
  137. shift
  138. ;;
  139. -f )
  140. diff_flags="--unified=999999"
  141. shift
  142. ;;
  143. -h | -help | --help )
  144. help=1
  145. shift
  146. ;;
  147. -s )
  148. srctree="$2"
  149. shift 2
  150. ;;
  151. -S )
  152. git_root=`git rev-parse --show-toplevel 2>/dev/null`
  153. srctree="${git_root}"
  154. shift
  155. ;;
  156. -u )
  157. dtc_sort=""
  158. shift
  159. ;;
  160. *)
  161. if [ "${dtx_file_1}" = "" ] ; then
  162. dtx_file_1="$1"
  163. elif [ "${dtx_file_2}" = "" ] ; then
  164. dtx_file_2="$1"
  165. else
  166. echo "" >&2
  167. echo "ERROR: Unexpected parameter: $1" >&2
  168. echo "" >&2
  169. exit 2
  170. fi
  171. shift
  172. ;;
  173. esac
  174. done
  175. if [ "${srctree}" = "" ] ; then
  176. srctree="."
  177. fi
  178. if [ "${dtx_file_2}" != "" ]; then
  179. cmd_diff=1
  180. fi
  181. if (( ${help} )) ; then
  182. usage
  183. exit 1
  184. fi
  185. # this must follow check for ${help}
  186. if [ "${dtx_file_1}" = "" ]; then
  187. echo "" >&2
  188. echo "ERROR: parameter DTx required" >&2
  189. echo "" >&2
  190. exit 2
  191. fi
  192. # ----- prefer dtc from linux kernel, allow fallback to dtc in $PATH
  193. if [ "${KBUILD_OUTPUT:0:2}" = ".." ] ; then
  194. __KBUILD_OUTPUT="${srctree}/${KBUILD_OUTPUT}"
  195. elif [ "${KBUILD_OUTPUT}" = "" ] ; then
  196. __KBUILD_OUTPUT="."
  197. else
  198. __KBUILD_OUTPUT="${KBUILD_OUTPUT}"
  199. fi
  200. DTC="${__KBUILD_OUTPUT}/scripts/dtc/dtc"
  201. if [ ! -x ${DTC} ] ; then
  202. __DTC="dtc"
  203. if grep -q "^CONFIG_DTC=y" ${__KBUILD_OUTPUT}/.config 2>/dev/null; then
  204. make_command='
  205. make scripts'
  206. else
  207. make_command='
  208. Enable CONFIG_DTC in the kernel configuration
  209. make scripts'
  210. fi
  211. if ( ! which ${__DTC} >/dev/null ) ; then
  212. # use spaces instead of tabs in the error message
  213. cat >&2 <<eod
  214. ERROR: unable to find a 'dtc' program
  215. Preferred 'dtc' (built from Linux kernel source tree) was not found or
  216. is not executable.
  217. 'dtc' is: ${DTC}
  218. If it does not exist, create it from the root of the Linux source tree:
  219. ${make_command}
  220. If not at the root of the Linux kernel source tree -s SRCTREE or -S
  221. may need to be specified to find 'dtc'.
  222. If 'O=\${dir}' is specified in your Linux builds, this script requires
  223. 'export KBUILD_OUTPUT=\${dir}' or add \${dir}/scripts/dtc to \$PATH
  224. before running.
  225. If \${KBUILD_OUTPUT} is a relative path, then '-s SRCDIR', -S, or run
  226. this script from the root of the Linux kernel source tree is required.
  227. Fallback '${__DTC}' was also not in \${PATH} or is not executable.
  228. eod
  229. exit 2
  230. fi
  231. DTC=${__DTC}
  232. fi
  233. # ----- cpp and dtc flags same as for linux source tree build of .dtb files,
  234. # plus directories of the dtx file(s)
  235. dtx_path_1_dtc_include="-i `dirname ${dtx_file_1}`"
  236. dtx_path_2_dtc_include=""
  237. if (( ${cmd_diff} )) ; then
  238. dtx_path_2_dtc_include="-i `dirname ${dtx_file_2}`"
  239. fi
  240. cpp_flags="\
  241. -nostdinc \
  242. -I${srctree}/arch/${ARCH}/boot/dts \
  243. -I${srctree}/arch/${ARCH}/boot/dts/include \
  244. -I${srctree}/drivers/of/testcase-data \
  245. -undef -D__DTS__"
  246. dtc_flags="\
  247. -i ${srctree}/arch/${ARCH}/boot/dts/ \
  248. -i ${srctree}/kernel/dts \
  249. ${dtx_path_1_dtc_include} \
  250. ${dtx_path_2_dtc_include}"
  251. DTC="${DTC} ${dtc_flags} -O dts -qq -f ${dtc_sort} -o -"
  252. # ----- do the diff or decompile
  253. if (( ${cmd_diff} )) ; then
  254. diff ${diff_flags} ${diff_color} --label "${dtx_file_1}" --label "${dtx_file_2}" \
  255. <(compile_to_dts "${dtx_file_1}") \
  256. <(compile_to_dts "${dtx_file_2}")
  257. else
  258. compile_to_dts "${dtx_file_1}"
  259. fi