dtx_diff 8.6 KB

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