dtx_diff 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. dtc_include="$2"
  64. if [ -d "${dtx}" ] ; then
  65. # ----- input is file tree
  66. if ( ! ${DTC} -I fs ${dtx} ) ; then
  67. exit 3
  68. fi
  69. elif [ -f "${dtx}" ] && [ -r "${dtx}" ] ; then
  70. magic=`hexdump -n 4 -e '/1 "%02x"' ${dtx}`
  71. if [ "${magic}" = "d00dfeed" ] ; then
  72. # ----- input is FDT (binary blob)
  73. if ( ! ${DTC} -I dtb ${dtx} ) ; then
  74. exit 3
  75. fi
  76. return
  77. fi
  78. # ----- input is DTS (source)
  79. if ( cpp ${cpp_flags} -x assembler-with-cpp ${dtx} \
  80. | ${DTC} ${dtc_include} -I dts ) ; then
  81. return
  82. fi
  83. echo "" >&2
  84. echo "Possible hints to resolve the above error:" >&2
  85. echo " (hints might not fix the problem)" >&2
  86. hint_given=0
  87. if [ "${ARCH}" = "" ] ; then
  88. hint_given=1
  89. echo "" >&2
  90. echo " shell variable \$ARCH not set" >&2
  91. fi
  92. dtx_arch=`echo "/${dtx}" | sed -e 's|.*/arch/||' -e 's|/.*||'`
  93. if [ "${dtx_arch}" != "" -a "${dtx_arch}" != "${ARCH}" ] ; then
  94. hint_given=1
  95. echo "" >&2
  96. echo " architecture ${dtx_arch} is in file path," >&2
  97. echo " but does not match shell variable \$ARCH" >&2
  98. echo " >>\$ARCH<< is: >>${ARCH}<<" >&2
  99. fi
  100. if [ ! -d ${srctree}/arch/${ARCH} ] ; then
  101. hint_given=1
  102. echo "" >&2
  103. echo " ${srctree}/arch/${ARCH}/ does not exist" >&2
  104. echo " Is \$ARCH='${ARCH}' correct?" >&2
  105. echo " Possible fix: use '-s' option" >&2
  106. git_root=`git rev-parse --show-toplevel 2>/dev/null`
  107. if [ -d ${git_root}/arch/ ] ; then
  108. echo " Possible fix: use '-S' option" >&2
  109. fi
  110. fi
  111. if [ $hint_given = 0 ] ; then
  112. echo "" >&2
  113. echo " No hints available." >&2
  114. fi
  115. echo "" >&2
  116. exit 3
  117. else
  118. echo "" >&2
  119. echo "ERROR: ${dtx} does not exist or is not readable" >&2
  120. echo "" >&2
  121. exit 2
  122. fi
  123. }
  124. # ----- start of script
  125. cmd_diff=0
  126. diff_flags="-u"
  127. dtx_file_1=""
  128. dtx_file_2=""
  129. dtc_sort="-s"
  130. help=0
  131. srctree=""
  132. while [ $# -gt 0 ] ; do
  133. case $1 in
  134. -f )
  135. diff_flags="--unified=999999"
  136. shift
  137. ;;
  138. -h | -help | --help )
  139. help=1
  140. shift
  141. ;;
  142. -s )
  143. srctree="$2"
  144. shift 2
  145. ;;
  146. -S )
  147. git_root=`git rev-parse --show-toplevel 2>/dev/null`
  148. srctree="${git_root}"
  149. shift
  150. ;;
  151. -u )
  152. dtc_sort=""
  153. shift
  154. ;;
  155. *)
  156. if [ "${dtx_file_1}" = "" ] ; then
  157. dtx_file_1="$1"
  158. elif [ "${dtx_file_2}" = "" ] ; then
  159. dtx_file_2="$1"
  160. else
  161. echo "" >&2
  162. echo "ERROR: Unexpected parameter: $1" >&2
  163. echo "" >&2
  164. exit 2
  165. fi
  166. shift
  167. ;;
  168. esac
  169. done
  170. if [ "${srctree}" = "" ] ; then
  171. srctree="."
  172. fi
  173. if [ "${dtx_file_2}" != "" ]; then
  174. cmd_diff=1
  175. fi
  176. if (( ${help} )) ; then
  177. usage
  178. exit 1
  179. fi
  180. # this must follow check for ${help}
  181. if [ "${dtx_file_1}" = "" ]; then
  182. echo "" >&2
  183. echo "ERROR: parameter DTx required" >&2
  184. echo "" >&2
  185. exit 2
  186. fi
  187. # ----- prefer dtc from linux kernel, allow fallback to dtc in $PATH
  188. if [ "${KBUILD_OUTPUT:0:2}" = ".." ] ; then
  189. __KBUILD_OUTPUT="${srctree}/${KBUILD_OUTPUT}"
  190. elif [ "${KBUILD_OUTPUT}" = "" ] ; then
  191. __KBUILD_OUTPUT="."
  192. else
  193. __KBUILD_OUTPUT="${KBUILD_OUTPUT}"
  194. fi
  195. DTC="${__KBUILD_OUTPUT}/scripts/dtc/dtc"
  196. if [ ! -x ${DTC} ] ; then
  197. __DTC="dtc"
  198. if grep -q "^CONFIG_DTC=y" ${__KBUILD_OUTPUT}/.config 2>/dev/null; then
  199. make_command='
  200. make scripts'
  201. else
  202. make_command='
  203. Enable CONFIG_DTC in the kernel configuration
  204. make scripts'
  205. fi
  206. if ( ! which ${__DTC} >/dev/null ) ; then
  207. # use spaces instead of tabs in the error message
  208. cat >&2 <<eod
  209. ERROR: unable to find a 'dtc' program
  210. Preferred 'dtc' (built from Linux kernel source tree) was not found or
  211. is not executable.
  212. 'dtc' is: ${DTC}
  213. If it does not exist, create it from the root of the Linux source tree:
  214. ${make_command}
  215. If not at the root of the Linux kernel source tree -s SRCTREE or -S
  216. may need to be specified to find 'dtc'.
  217. If 'O=\${dir}' is specified in your Linux builds, this script requires
  218. 'export KBUILD_OUTPUT=\${dir}' or add \${dir}/scripts/dtc to \$PATH
  219. before running.
  220. If \${KBUILD_OUTPUT} is a relative path, then '-s SRCDIR', -S, or run
  221. this script from the root of the Linux kernel source tree is required.
  222. Fallback '${__DTC}' was also not in \${PATH} or is not executable.
  223. eod
  224. exit 2
  225. fi
  226. DTC=${__DTC}
  227. fi
  228. # ----- cpp and dtc flags same as for linux source tree build of .dtb files,
  229. # plus directories of the dtx file(s)
  230. dtx_path_1_dtc_include="-i `dirname ${dtx_file_1}`"
  231. dtx_path_2_dtc_include=""
  232. if (( ${cmd_diff} )) ; then
  233. dtx_path_2_dtc_include="-i `dirname ${dtx_file_2}`"
  234. fi
  235. cpp_flags="\
  236. -nostdinc \
  237. -I${srctree}/scripts/dtc/include-prefixes \
  238. -undef -D__DTS__"
  239. DTC="\
  240. ${DTC} \
  241. -i ${srctree}/scripts/dtc/include-prefixes \
  242. -O dts -qq -f ${dtc_sort} -o -"
  243. # ----- do the diff or decompile
  244. if (( ${cmd_diff} )) ; then
  245. diff ${diff_flags} --label "${dtx_file_1}" --label "${dtx_file_2}" \
  246. <(compile_to_dts "${dtx_file_1}" "${dtx_path_1_dtc_include}") \
  247. <(compile_to_dts "${dtx_file_2}" "${dtx_path_2_dtc_include}")
  248. else
  249. compile_to_dts "${dtx_file_1}" "${dtx_path_1_dtc_include}"
  250. fi