mkits.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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'\n"
  34. exit 1
  35. }
  36. FDTNUM=1
  37. ROOTFSNUM=1
  38. INITRDNUM=1
  39. HASH=sha1
  40. LOADABLES=
  41. while getopts ":A:a:c:C:D:d:e:f:i:k:n:o:v:r:S" OPTION
  42. do
  43. case $OPTION in
  44. A ) ARCH=$OPTARG;;
  45. a ) LOAD_ADDR=$OPTARG;;
  46. c ) CONFIG=$OPTARG;;
  47. C ) COMPRESS=$OPTARG;;
  48. D ) DEVICE=$OPTARG;;
  49. d ) DTB=$OPTARG;;
  50. e ) ENTRY_ADDR=$OPTARG;;
  51. f ) COMPATIBLE=$OPTARG;;
  52. i ) INITRD=$OPTARG;;
  53. k ) KERNEL=$OPTARG;;
  54. n ) FDTNUM=$OPTARG;;
  55. o ) OUTPUT=$OPTARG;;
  56. r ) ROOTFS=$OPTARG;;
  57. S ) HASH=$OPTARG;;
  58. v ) VERSION=$OPTARG;;
  59. * ) echo "Invalid option passed to '$0' (options:$*)"
  60. usage;;
  61. esac
  62. done
  63. # Make sure user entered all required parameters
  64. if [ -z "${ARCH}" ] || [ -z "${COMPRESS}" ] || [ -z "${LOAD_ADDR}" ] || \
  65. [ -z "${ENTRY_ADDR}" ] || [ -z "${VERSION}" ] || [ -z "${KERNEL}" ] || \
  66. [ -z "${OUTPUT}" ] || [ -z "${CONFIG}" ]; then
  67. usage
  68. fi
  69. ARCH_UPPER=$(echo "$ARCH" | tr '[:lower:]' '[:upper:]')
  70. if [ -n "${COMPATIBLE}" ]; then
  71. COMPATIBLE_PROP="compatible = \"${COMPATIBLE}\";"
  72. fi
  73. # Conditionally create fdt information
  74. if [ -n "${DTB}" ]; then
  75. FDT_NODE="
  76. fdt@$FDTNUM {
  77. description = \"${ARCH_UPPER} OpenWrt ${DEVICE} device tree blob\";
  78. ${COMPATIBLE_PROP}
  79. data = /incbin/(\"${DTB}\");
  80. type = \"flat_dt\";
  81. arch = \"${ARCH}\";
  82. compression = \"none\";
  83. hash@1 {
  84. algo = \"crc32\";
  85. };
  86. hash@2 {
  87. algo = \"${HASH}\";
  88. };
  89. };
  90. "
  91. FDT_PROP="fdt = \"fdt@$FDTNUM\";"
  92. fi
  93. if [ -n "${INITRD}" ]; then
  94. INITRD_NODE="
  95. initrd@$INITRDNUM {
  96. description = \"${ARCH_UPPER} OpenWrt ${DEVICE} initrd\";
  97. ${COMPATIBLE_PROP}
  98. data = /incbin/(\"${INITRD}\");
  99. type = \"ramdisk\";
  100. arch = \"${ARCH}\";
  101. os = \"linux\";
  102. hash@1 {
  103. algo = \"crc32\";
  104. };
  105. hash@2 {
  106. algo = \"${HASH}\";
  107. };
  108. };
  109. "
  110. INITRD_PROP="ramdisk=\"initrd@${INITRDNUM}\";"
  111. fi
  112. if [ -n "${ROOTFS}" ]; then
  113. dd if="${ROOTFS}" of="${ROOTFS}.pagesync" bs=4096 conv=sync
  114. ROOTFS_NODE="
  115. rootfs@$ROOTFSNUM {
  116. description = \"${ARCH_UPPER} OpenWrt ${DEVICE} rootfs\";
  117. ${COMPATIBLE_PROP}
  118. data = /incbin/(\"${ROOTFS}.pagesync\");
  119. type = \"filesystem\";
  120. arch = \"${ARCH}\";
  121. compression = \"none\";
  122. hash@1 {
  123. algo = \"crc32\";
  124. };
  125. hash@2 {
  126. algo = \"${HASH}\";
  127. };
  128. };
  129. "
  130. LOADABLES="${LOADABLES:+$LOADABLES, }\"rootfs@${ROOTFSNUM}\""
  131. fi
  132. # Create a default, fully populated DTS file
  133. DATA="/dts-v1/;
  134. / {
  135. description = \"${ARCH_UPPER} OpenWrt FIT (Flattened Image Tree)\";
  136. #address-cells = <1>;
  137. images {
  138. kernel@1 {
  139. description = \"${ARCH_UPPER} OpenWrt Linux-${VERSION}\";
  140. data = /incbin/(\"${KERNEL}\");
  141. type = \"kernel\";
  142. arch = \"${ARCH}\";
  143. os = \"linux\";
  144. compression = \"${COMPRESS}\";
  145. load = <${LOAD_ADDR}>;
  146. entry = <${ENTRY_ADDR}>;
  147. hash@1 {
  148. algo = \"crc32\";
  149. };
  150. hash@2 {
  151. algo = \"$HASH\";
  152. };
  153. };
  154. ${INITRD_NODE}
  155. ${FDT_NODE}
  156. ${ROOTFS_NODE}
  157. };
  158. configurations {
  159. default = \"${CONFIG}\";
  160. ${CONFIG} {
  161. description = \"OpenWrt ${DEVICE}\";
  162. kernel = \"kernel@1\";
  163. ${FDT_PROP}
  164. ${LOADABLES:+loadables = ${LOADABLES};}
  165. ${COMPATIBLE_PROP}
  166. ${INITRD_PROP}
  167. };
  168. };
  169. };"
  170. # Write .its file to disk
  171. echo "$DATA" > "${OUTPUT}"