mkits.sh 5.9 KB

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