1
0

mkits.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #!/bin/sh
  2. #
  3. # Licensed under the terms of the GNU GPL License version 2 or later.
  4. #
  5. # Author: Peter Tyser <ptyser@xes-inc.com>
  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. [ "$FDTADDR" ] && {
  84. DTADDR="$FDTADDR"
  85. }
  86. # Conditionally create fdt information
  87. if [ -n "${DTB}" ]; then
  88. FDT_NODE="
  89. fdt${REFERENCE_CHAR}$FDTNUM {
  90. description = \"${ARCH_UPPER} libreCMC ${DEVICE} device tree blob\";
  91. ${COMPATIBLE_PROP}
  92. data = /incbin/(\"${DTB}\");
  93. type = \"flat_dt\";
  94. ${DTADDR:+load = <${DTADDR}>;}
  95. arch = \"${ARCH}\";
  96. compression = \"none\";
  97. hash${REFERENCE_CHAR}1 {
  98. algo = \"crc32\";
  99. };
  100. hash${REFERENCE_CHAR}2 {
  101. algo = \"${HASH}\";
  102. };
  103. };
  104. "
  105. FDT_PROP="fdt = \"fdt${REFERENCE_CHAR}$FDTNUM\";"
  106. fi
  107. if [ -n "${INITRD}" ]; then
  108. INITRD_NODE="
  109. initrd${REFERENCE_CHAR}$INITRDNUM {
  110. description = \"${ARCH_UPPER} libreCMC ${DEVICE} initrd\";
  111. ${COMPATIBLE_PROP}
  112. data = /incbin/(\"${INITRD}\");
  113. type = \"ramdisk\";
  114. arch = \"${ARCH}\";
  115. os = \"linux\";
  116. hash${REFERENCE_CHAR}1 {
  117. algo = \"crc32\";
  118. };
  119. hash${REFERENCE_CHAR}2 {
  120. algo = \"${HASH}\";
  121. };
  122. };
  123. "
  124. INITRD_PROP="ramdisk=\"initrd${REFERENCE_CHAR}${INITRDNUM}\";"
  125. fi
  126. if [ -n "${ROOTFS}" ]; then
  127. dd if="${ROOTFS}" of="${ROOTFS}.pagesync" bs=4096 conv=sync
  128. ROOTFS_NODE="
  129. rootfs${REFERENCE_CHAR}$ROOTFSNUM {
  130. description = \"${ARCH_UPPER} libreCMC ${DEVICE} rootfs\";
  131. ${COMPATIBLE_PROP}
  132. data = /incbin/(\"${ROOTFS}.pagesync\");
  133. type = \"filesystem\";
  134. arch = \"${ARCH}\";
  135. compression = \"none\";
  136. hash${REFERENCE_CHAR}1 {
  137. algo = \"crc32\";
  138. };
  139. hash${REFERENCE_CHAR}2 {
  140. algo = \"${HASH}\";
  141. };
  142. };
  143. "
  144. LOADABLES="${LOADABLES:+$LOADABLES, }\"rootfs${REFERENCE_CHAR}${ROOTFSNUM}\""
  145. fi
  146. # add DT overlay blobs
  147. FDTOVERLAY_NODE=""
  148. OVCONFIGS=""
  149. [ "$DTOVERLAY" ] && for overlay in $DTOVERLAY ; do
  150. overlay_blob=${overlay##*:}
  151. ovname=${overlay%%:*}
  152. ovnode="fdt-$ovname"
  153. ovsize=$(wc -c "$overlay_blob" | awk '{print $1}')
  154. echo "$ovname ($overlay_blob) : $ovsize" >&2
  155. FDTOVERLAY_NODE="$FDTOVERLAY_NODE
  156. $ovnode {
  157. description = \"${ARCH_UPPER} libreCMC ${DEVICE} device tree overlay $ovname\";
  158. ${COMPATIBLE_PROP}
  159. data = /incbin/(\"${overlay_blob}\");
  160. type = \"flat_dt\";
  161. arch = \"${ARCH}\";
  162. compression = \"none\";
  163. hash${REFERENCE_CHAR}1 {
  164. algo = \"crc32\";
  165. };
  166. hash${REFERENCE_CHAR}2 {
  167. algo = \"${HASH}\";
  168. };
  169. };
  170. "
  171. OVCONFIGS="$OVCONFIGS
  172. $ovname {
  173. description = \"libreCMC ${DEVICE} overlay $ovname\";
  174. fdt = \"$ovnode\";
  175. ${COMPATIBLE_PROP}
  176. };
  177. "
  178. done
  179. # Create a default, fully populated DTS file
  180. DATA="/dts-v1/;
  181. / {
  182. description = \"${ARCH_UPPER} libreCMC FIT (Flattened Image Tree)\";
  183. #address-cells = <1>;
  184. images {
  185. kernel${REFERENCE_CHAR}1 {
  186. description = \"${ARCH_UPPER} libreCMC Linux-${VERSION}\";
  187. data = /incbin/(\"${KERNEL}\");
  188. type = \"kernel\";
  189. arch = \"${ARCH}\";
  190. os = \"linux\";
  191. compression = \"${COMPRESS}\";
  192. load = <${LOAD_ADDR}>;
  193. entry = <${ENTRY_ADDR}>;
  194. hash${REFERENCE_CHAR}1 {
  195. algo = \"crc32\";
  196. };
  197. hash${REFERENCE_CHAR}2 {
  198. algo = \"$HASH\";
  199. };
  200. };
  201. ${INITRD_NODE}
  202. ${FDT_NODE}
  203. ${FDTOVERLAY_NODE}
  204. ${ROOTFS_NODE}
  205. };
  206. configurations {
  207. default = \"${CONFIG}\";
  208. ${CONFIG} {
  209. description = \"libreCMC ${DEVICE}\";
  210. kernel = \"kernel${REFERENCE_CHAR}1\";
  211. ${FDT_PROP}
  212. ${LOADABLES:+loadables = ${LOADABLES};}
  213. ${COMPATIBLE_PROP}
  214. ${INITRD_PROP}
  215. };
  216. ${OVCONFIGS}
  217. };
  218. };"
  219. # Write .its file to disk
  220. echo "$DATA" > "${OUTPUT}"