mkits.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #!/bin/sh
  2. #
  3. # Licensed under the terms of the GNU GPL License version 2 or later.
  4. #
  5. # Author: Peter Tyser <[email protected]>
  6. #
  7. # U-Boot firmware supports the booting of images in the Flattened Image
  8. # Tree (FIT) format. The FIT format uses a device tree structure to
  9. # describe a kernel image, device tree blob, ramdisk, etc. This script
  10. # creates an Image Tree Source (.its file) which can be passed to the
  11. # 'mkimage' utility to generate an Image Tree Blob (.itb file). The .itb
  12. # file can then be booted by U-Boot (or other bootloaders which support
  13. # FIT images). See doc/uImage.FIT/howto.txt in U-Boot source code for
  14. # additional information on FIT images.
  15. #
  16. usage() {
  17. printf "Usage: %s -A arch -C comp -a addr -e entry" "$(basename "$0")"
  18. printf " -v version -k kernel [-D name -n address -d dtb] -o its_file"
  19. printf "\n\t-A ==> set architecture to 'arch'"
  20. printf "\n\t-C ==> set compression type 'comp'"
  21. printf "\n\t-c ==> set config name 'config'"
  22. printf "\n\t-a ==> set load address to 'addr' (hex)"
  23. printf "\n\t-e ==> set entry point to 'entry' (hex)"
  24. printf "\n\t-f ==> set device tree compatible string"
  25. printf "\n\t-i ==> include initrd Blob 'initrd'"
  26. printf "\n\t-v ==> set kernel version to 'version'"
  27. printf "\n\t-k ==> include kernel image 'kernel'"
  28. printf "\n\t-D ==> human friendly Device Tree Blob 'name'"
  29. printf "\n\t-n ==> fdt unit-address 'address'"
  30. printf "\n\t-d ==> include Device Tree Blob 'dtb'"
  31. printf "\n\t-r ==> include RootFS blob 'rootfs'"
  32. printf "\n\t-H ==> specify hash algo instead of SHA1"
  33. printf "\n\t-l ==> legacy mode character (@ etc otherwise -)"
  34. printf "\n\t-o ==> create output file 'its_file'"
  35. printf "\n\t-O ==> create config with dt overlay 'name:dtb'"
  36. printf "\n\t-s ==> set FDT load address to 'addr' (hex)"
  37. printf "\n\t\t(can be specified more than once)\n"
  38. exit 1
  39. }
  40. REFERENCE_CHAR='-'
  41. FDTNUM=1
  42. ROOTFSNUM=1
  43. INITRDNUM=1
  44. HASH=sha1
  45. LOADABLES=
  46. DTOVERLAY=
  47. DTADDR=
  48. while getopts ":A:a:c:C:D:d:e:f:i:k:l:n:o:O:v:r:s:H:" OPTION
  49. do
  50. case $OPTION in
  51. A ) ARCH=$OPTARG;;
  52. a ) LOAD_ADDR=$OPTARG;;
  53. c ) CONFIG=$OPTARG;;
  54. C ) COMPRESS=$OPTARG;;
  55. D ) DEVICE=$OPTARG;;
  56. d ) DTB=$OPTARG;;
  57. e ) ENTRY_ADDR=$OPTARG;;
  58. f ) COMPATIBLE=$OPTARG;;
  59. i ) INITRD=$OPTARG;;
  60. k ) KERNEL=$OPTARG;;
  61. l ) REFERENCE_CHAR=$OPTARG;;
  62. n ) FDTNUM=$OPTARG;;
  63. o ) OUTPUT=$OPTARG;;
  64. O ) DTOVERLAY="$DTOVERLAY ${OPTARG}";;
  65. r ) ROOTFS=$OPTARG;;
  66. s ) FDTADDR=$OPTARG;;
  67. H ) HASH=$OPTARG;;
  68. v ) VERSION=$OPTARG;;
  69. * ) echo "Invalid option passed to '$0' (options:$*)"
  70. usage;;
  71. esac
  72. done
  73. # Make sure user entered all required parameters
  74. if [ -z "${ARCH}" ] || [ -z "${COMPRESS}" ] || [ -z "${LOAD_ADDR}" ] || \
  75. [ -z "${ENTRY_ADDR}" ] || [ -z "${VERSION}" ] || [ -z "${KERNEL}" ] || \
  76. [ -z "${OUTPUT}" ] || [ -z "${CONFIG}" ]; then
  77. usage
  78. fi
  79. ARCH_UPPER=$(echo "$ARCH" | tr '[:lower:]' '[:upper:]')
  80. if [ -n "${COMPATIBLE}" ]; then
  81. COMPATIBLE_PROP="compatible = \"${COMPATIBLE}\";"
  82. fi
  83. [ "$DTOVERLAY" ] && {
  84. dtbsize=$(wc -c "$DTB" | awk '{print $1}')
  85. DTADDR=$(printf "0x%08x" $(($LOAD_ADDR - $dtbsize)) )
  86. }
  87. [ "$FDTADDR" ] && {
  88. DTADDR="$FDTADDR"
  89. }
  90. # Conditionally create fdt information
  91. if [ -n "${DTB}" ]; then
  92. FDT_NODE="
  93. fdt${REFERENCE_CHAR}$FDTNUM {
  94. description = \"${ARCH_UPPER} OpenWrt ${DEVICE} device tree blob\";
  95. ${COMPATIBLE_PROP}
  96. data = /incbin/(\"${DTB}\");
  97. type = \"flat_dt\";
  98. ${DTADDR:+load = <${DTADDR}>;}
  99. arch = \"${ARCH}\";
  100. compression = \"none\";
  101. hash${REFERENCE_CHAR}1 {
  102. algo = \"crc32\";
  103. };
  104. hash${REFERENCE_CHAR}2 {
  105. algo = \"${HASH}\";
  106. };
  107. };
  108. "
  109. FDT_PROP="fdt = \"fdt${REFERENCE_CHAR}$FDTNUM\";"
  110. fi
  111. if [ -n "${INITRD}" ]; then
  112. INITRD_NODE="
  113. initrd${REFERENCE_CHAR}$INITRDNUM {
  114. description = \"${ARCH_UPPER} OpenWrt ${DEVICE} initrd\";
  115. ${COMPATIBLE_PROP}
  116. data = /incbin/(\"${INITRD}\");
  117. type = \"ramdisk\";
  118. arch = \"${ARCH}\";
  119. os = \"linux\";
  120. hash${REFERENCE_CHAR}1 {
  121. algo = \"crc32\";
  122. };
  123. hash${REFERENCE_CHAR}2 {
  124. algo = \"${HASH}\";
  125. };
  126. };
  127. "
  128. INITRD_PROP="ramdisk=\"initrd${REFERENCE_CHAR}${INITRDNUM}\";"
  129. fi
  130. if [ -n "${ROOTFS}" ]; then
  131. dd if="${ROOTFS}" of="${ROOTFS}.pagesync" bs=4096 conv=sync
  132. ROOTFS_NODE="
  133. rootfs${REFERENCE_CHAR}$ROOTFSNUM {
  134. description = \"${ARCH_UPPER} OpenWrt ${DEVICE} rootfs\";
  135. ${COMPATIBLE_PROP}
  136. data = /incbin/(\"${ROOTFS}.pagesync\");
  137. type = \"filesystem\";
  138. arch = \"${ARCH}\";
  139. compression = \"none\";
  140. hash${REFERENCE_CHAR}1 {
  141. algo = \"crc32\";
  142. };
  143. hash${REFERENCE_CHAR}2 {
  144. algo = \"${HASH}\";
  145. };
  146. };
  147. "
  148. LOADABLES="${LOADABLES:+$LOADABLES, }\"rootfs${REFERENCE_CHAR}${ROOTFSNUM}\""
  149. fi
  150. # add DT overlay blobs
  151. FDTOVERLAY_NODE=""
  152. OVCONFIGS=""
  153. [ "$DTOVERLAY" ] && for overlay in $DTOVERLAY ; do
  154. overlay_blob=${overlay##*:}
  155. ovname=${overlay%%:*}
  156. ovnode="fdt-$ovname"
  157. ovsize=$(wc -c "$overlay_blob" | awk '{print $1}')
  158. echo "$ovname ($overlay_blob) : $ovsize" >&2
  159. DTADDR=$(printf "0x%08x" $(($DTADDR - $ovsize)))
  160. FDTOVERLAY_NODE="$FDTOVERLAY_NODE
  161. $ovnode {
  162. description = \"${ARCH_UPPER} OpenWrt ${DEVICE} device tree overlay $ovname\";
  163. ${COMPATIBLE_PROP}
  164. data = /incbin/(\"${overlay_blob}\");
  165. type = \"flat_dt\";
  166. arch = \"${ARCH}\";
  167. load = <${DTADDR}>;
  168. compression = \"none\";
  169. hash${REFERENCE_CHAR}1 {
  170. algo = \"crc32\";
  171. };
  172. hash${REFERENCE_CHAR}2 {
  173. algo = \"${HASH}\";
  174. };
  175. };
  176. "
  177. OVCONFIGS="$OVCONFIGS
  178. $ovname {
  179. description = \"OpenWrt ${DEVICE} overlay $ovname\";
  180. fdt = \"$ovnode\";
  181. ${COMPATIBLE_PROP}
  182. };
  183. "
  184. done
  185. # Create a default, fully populated DTS file
  186. DATA="/dts-v1/;
  187. / {
  188. description = \"${ARCH_UPPER} OpenWrt FIT (Flattened Image Tree)\";
  189. #address-cells = <1>;
  190. images {
  191. kernel${REFERENCE_CHAR}1 {
  192. description = \"${ARCH_UPPER} OpenWrt Linux-${VERSION}\";
  193. data = /incbin/(\"${KERNEL}\");
  194. type = \"kernel\";
  195. arch = \"${ARCH}\";
  196. os = \"linux\";
  197. compression = \"${COMPRESS}\";
  198. load = <${LOAD_ADDR}>;
  199. entry = <${ENTRY_ADDR}>;
  200. hash${REFERENCE_CHAR}1 {
  201. algo = \"crc32\";
  202. };
  203. hash${REFERENCE_CHAR}2 {
  204. algo = \"$HASH\";
  205. };
  206. };
  207. ${INITRD_NODE}
  208. ${FDT_NODE}
  209. ${FDTOVERLAY_NODE}
  210. ${ROOTFS_NODE}
  211. };
  212. configurations {
  213. default = \"${CONFIG}\";
  214. ${CONFIG} {
  215. description = \"OpenWrt ${DEVICE}\";
  216. kernel = \"kernel${REFERENCE_CHAR}1\";
  217. ${FDT_PROP}
  218. ${LOADABLES:+loadables = ${LOADABLES};}
  219. ${COMPATIBLE_PROP}
  220. ${INITRD_PROP}
  221. };
  222. ${OVCONFIGS}
  223. };
  224. };"
  225. # Write .its file to disk
  226. echo "$DATA" > "${OUTPUT}"