2
0

mkits.sh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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\t(can be specified more than once)\n"
  37. exit 1
  38. }
  39. REFERENCE_CHAR='-'
  40. FDTNUM=1
  41. ROOTFSNUM=1
  42. INITRDNUM=1
  43. HASH=sha1
  44. LOADABLES=
  45. DTOVERLAY=
  46. DTADDR=
  47. while getopts ":A:a:c:C:D:d:e:f:i:k:l:n:o:O:v:r:H:" OPTION
  48. do
  49. case $OPTION in
  50. A ) ARCH=$OPTARG;;
  51. a ) LOAD_ADDR=$OPTARG;;
  52. c ) CONFIG=$OPTARG;;
  53. C ) COMPRESS=$OPTARG;;
  54. D ) DEVICE=$OPTARG;;
  55. d ) DTB=$OPTARG;;
  56. e ) ENTRY_ADDR=$OPTARG;;
  57. f ) COMPATIBLE=$OPTARG;;
  58. i ) INITRD=$OPTARG;;
  59. k ) KERNEL=$OPTARG;;
  60. l ) REFERENCE_CHAR=$OPTARG;;
  61. n ) FDTNUM=$OPTARG;;
  62. o ) OUTPUT=$OPTARG;;
  63. O ) DTOVERLAY="$DTOVERLAY ${OPTARG}";;
  64. r ) ROOTFS=$OPTARG;;
  65. H ) HASH=$OPTARG;;
  66. v ) VERSION=$OPTARG;;
  67. * ) echo "Invalid option passed to '$0' (options:$*)"
  68. usage;;
  69. esac
  70. done
  71. # Make sure user entered all required parameters
  72. if [ -z "${ARCH}" ] || [ -z "${COMPRESS}" ] || [ -z "${LOAD_ADDR}" ] || \
  73. [ -z "${ENTRY_ADDR}" ] || [ -z "${VERSION}" ] || [ -z "${KERNEL}" ] || \
  74. [ -z "${OUTPUT}" ] || [ -z "${CONFIG}" ]; then
  75. usage
  76. fi
  77. ARCH_UPPER=$(echo "$ARCH" | tr '[:lower:]' '[:upper:]')
  78. if [ -n "${COMPATIBLE}" ]; then
  79. COMPATIBLE_PROP="compatible = \"${COMPATIBLE}\";"
  80. fi
  81. [ "$DTOVERLAY" ] && {
  82. dtbsize=$(wc -c "$DTB" | cut -d' ' -f1)
  83. DTADDR=$(printf "0x%08x" $(($LOAD_ADDR - $dtbsize)) )
  84. }
  85. # Conditionally create fdt information
  86. if [ -n "${DTB}" ]; then
  87. FDT_NODE="
  88. fdt${REFERENCE_CHAR}$FDTNUM {
  89. description = \"${ARCH_UPPER} OpenWrt ${DEVICE} device tree blob\";
  90. ${COMPATIBLE_PROP}
  91. data = /incbin/(\"${DTB}\");
  92. type = \"flat_dt\";
  93. ${DTADDR:+load = <${DTADDR}>;}
  94. arch = \"${ARCH}\";
  95. compression = \"none\";
  96. hash@1 {
  97. algo = \"crc32\";
  98. };
  99. hash@2 {
  100. algo = \"${HASH}\";
  101. };
  102. };
  103. "
  104. FDT_PROP="fdt = \"fdt${REFERENCE_CHAR}$FDTNUM\";"
  105. fi
  106. if [ -n "${INITRD}" ]; then
  107. INITRD_NODE="
  108. initrd${REFERENCE_CHAR}$INITRDNUM {
  109. description = \"${ARCH_UPPER} OpenWrt ${DEVICE} initrd\";
  110. ${COMPATIBLE_PROP}
  111. data = /incbin/(\"${INITRD}\");
  112. type = \"ramdisk\";
  113. arch = \"${ARCH}\";
  114. os = \"linux\";
  115. hash@1 {
  116. algo = \"crc32\";
  117. };
  118. hash@2 {
  119. algo = \"${HASH}\";
  120. };
  121. };
  122. "
  123. INITRD_PROP="ramdisk=\"initrd${REFERENCE_CHAR}${INITRDNUM}\";"
  124. fi
  125. if [ -n "${ROOTFS}" ]; then
  126. dd if="${ROOTFS}" of="${ROOTFS}.pagesync" bs=4096 conv=sync
  127. ROOTFS_NODE="
  128. rootfs-$ROOTFSNUM {
  129. description = \"${ARCH_UPPER} OpenWrt ${DEVICE} rootfs\";
  130. ${COMPATIBLE_PROP}
  131. data = /incbin/(\"${ROOTFS}.pagesync\");
  132. type = \"filesystem\";
  133. arch = \"${ARCH}\";
  134. compression = \"none\";
  135. hash@1 {
  136. algo = \"crc32\";
  137. };
  138. hash@2 {
  139. algo = \"${HASH}\";
  140. };
  141. };
  142. "
  143. LOADABLES="${LOADABLES:+$LOADABLES, }\"rootfs${REFERENCE_CHAR}${ROOTFSNUM}\""
  144. fi
  145. # add DT overlay blobs
  146. FDTOVERLAY_NODE=""
  147. OVCONFIGS=""
  148. [ "$DTOVERLAY" ] && for overlay in $DTOVERLAY ; do
  149. overlay_blob=${overlay##*:}
  150. ovname=${overlay%%:*}
  151. ovnode="fdt-$ovname"
  152. ovsize=$(wc -c "$overlay_blob" | cut -d' ' -f1)
  153. echo "$ovname ($overlay_blob) : $ovsize" >&2
  154. DTADDR=$(printf "0x%08x" $(($DTADDR - $ovsize)))
  155. FDTOVERLAY_NODE="$FDTOVERLAY_NODE
  156. $ovnode {
  157. description = \"${ARCH_UPPER} OpenWrt ${DEVICE} device tree overlay $ovname\";
  158. ${COMPATIBLE_PROP}
  159. data = /incbin/(\"${overlay_blob}\");
  160. type = \"flat_dt\";
  161. arch = \"${ARCH}\";
  162. load = <${DTADDR}>;
  163. compression = \"none\";
  164. hash@1 {
  165. algo = \"crc32\";
  166. };
  167. hash@2 {
  168. algo = \"${HASH}\";
  169. };
  170. };
  171. "
  172. OVCONFIGS="$OVCONFIGS
  173. config-$ovname {
  174. description = \"OpenWrt ${DEVICE} with $ovname\";
  175. kernel = \"kernel${REFERENCE_CHAR}1\";
  176. fdt = \"fdt${REFERENCE_CHAR}$FDTNUM\", \"$ovnode\";
  177. ${LOADABLES:+loadables = ${LOADABLES};}
  178. ${COMPATIBLE_PROP}
  179. ${INITRD_PROP}
  180. };
  181. "
  182. done
  183. # Create a default, fully populated DTS file
  184. DATA="/dts-v1/;
  185. / {
  186. description = \"${ARCH_UPPER} OpenWrt FIT (Flattened Image Tree)\";
  187. #address-cells = <1>;
  188. images {
  189. kernel${REFERENCE_CHAR}1 {
  190. description = \"${ARCH_UPPER} OpenWrt Linux-${VERSION}\";
  191. data = /incbin/(\"${KERNEL}\");
  192. type = \"kernel\";
  193. arch = \"${ARCH}\";
  194. os = \"linux\";
  195. compression = \"${COMPRESS}\";
  196. load = <${LOAD_ADDR}>;
  197. entry = <${ENTRY_ADDR}>;
  198. hash@1 {
  199. algo = \"crc32\";
  200. };
  201. hash@2 {
  202. algo = \"$HASH\";
  203. };
  204. };
  205. ${INITRD_NODE}
  206. ${FDT_NODE}
  207. ${FDTOVERLAY_NODE}
  208. ${ROOTFS_NODE}
  209. };
  210. configurations {
  211. default = \"${CONFIG}\";
  212. ${CONFIG} {
  213. description = \"OpenWrt ${DEVICE}\";
  214. kernel = \"kernel${REFERENCE_CHAR}1\";
  215. ${FDT_PROP}
  216. ${LOADABLES:+loadables = ${LOADABLES};}
  217. ${COMPATIBLE_PROP}
  218. ${INITRD_PROP}
  219. };
  220. ${OVCONFIGS}
  221. };
  222. };"
  223. # Write .its file to disk
  224. echo "$DATA" > "${OUTPUT}"