ubinize-image.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/bin/sh
  2. . $TOPDIR/scripts/functions.sh
  3. part=""
  4. ubootenv=""
  5. ubinize_param=""
  6. kernel=""
  7. rootfs=""
  8. outfile=""
  9. err=""
  10. ubinize_seq=""
  11. ubivol() {
  12. volid=$1
  13. name=$2
  14. image=$3
  15. autoresize=$4
  16. size="$5"
  17. echo "[$name]"
  18. echo "mode=ubi"
  19. echo "vol_id=$volid"
  20. echo "vol_type=dynamic"
  21. echo "vol_name=$name"
  22. if [ "$image" ]; then
  23. echo "image=$image"
  24. [ -n "$size" ] && echo "vol_size=${size}"
  25. else
  26. echo "vol_size=1MiB"
  27. fi
  28. if [ "$autoresize" ]; then
  29. echo "vol_flags=autoresize"
  30. fi
  31. }
  32. ubilayout() {
  33. local vol_id=0
  34. local rootsize=
  35. local autoresize=
  36. local rootfs_type="$( get_fs_type "$2" )"
  37. if [ "$1" = "ubootenv" ]; then
  38. ubivol $vol_id ubootenv
  39. vol_id=$(( $vol_id + 1 ))
  40. ubivol $vol_id ubootenv2
  41. vol_id=$(( $vol_id + 1 ))
  42. fi
  43. for part in $parts; do
  44. name="${part%%=*}"
  45. prev="$part"
  46. part="${part#*=}"
  47. [ "$prev" = "$part" ] && part=
  48. image="${part%%=*}"
  49. prev="$part"
  50. part="${part#*=}"
  51. [ "$prev" = "$part" ] && part=
  52. size="$part"
  53. ubivol $vol_id "$name" "$image" "" "${size}MiB"
  54. vol_id=$(( $vol_id + 1 ))
  55. done
  56. if [ "$3" ]; then
  57. ubivol $vol_id kernel "$3"
  58. vol_id=$(( $vol_id + 1 ))
  59. fi
  60. case "$rootfs_type" in
  61. "ubifs")
  62. autoresize=1
  63. ;;
  64. "squashfs")
  65. # squashfs uses 1k block size, ensure we do not
  66. # violate that
  67. rootsize="$( round_up "$( stat -c%s "$2" )" 1024 )"
  68. ;;
  69. esac
  70. ubivol $vol_id rootfs "$2" "$autoresize" "$rootsize"
  71. vol_id=$(( $vol_id + 1 ))
  72. [ "$rootfs_type" = "ubifs" ] || ubivol $vol_id rootfs_data "" 1
  73. }
  74. set_ubinize_seq() {
  75. if [ -n "$SOURCE_DATE_EPOCH" ] ; then
  76. ubinize_seq="-Q $SOURCE_DATE_EPOCH"
  77. fi
  78. }
  79. while [ "$1" ]; do
  80. case "$1" in
  81. "--uboot-env")
  82. ubootenv="ubootenv"
  83. shift
  84. continue
  85. ;;
  86. "--kernel")
  87. kernel="$2"
  88. shift
  89. shift
  90. continue
  91. ;;
  92. "--part")
  93. parts="$parts $2"
  94. shift
  95. shift
  96. continue
  97. ;;
  98. "-"*)
  99. ubinize_param="$@"
  100. break
  101. ;;
  102. *)
  103. if [ ! "$rootfs" ]; then
  104. rootfs=$1
  105. shift
  106. continue
  107. fi
  108. if [ ! "$outfile" ]; then
  109. outfile=$1
  110. shift
  111. continue
  112. fi
  113. ;;
  114. esac
  115. done
  116. if [ ! -r "$rootfs" -o ! -r "$kernel" -a ! "$outfile" ]; then
  117. echo "syntax: $0 [--uboot-env] [--part <name>=<file>] [--kernel kernelimage] rootfs out [ubinize opts]"
  118. exit 1
  119. fi
  120. ubinize="$( which ubinize )"
  121. if [ ! -x "$ubinize" ]; then
  122. echo "ubinize tool not found or not usable"
  123. exit 1
  124. fi
  125. ubinizecfg="$( mktemp 2> /dev/null )"
  126. if [ -z "$ubinizecfg" ]; then
  127. # try OSX signature
  128. ubinizecfg="$( mktemp -t 'ubitmp' )"
  129. fi
  130. ubilayout "$ubootenv" "$rootfs" "$kernel" > "$ubinizecfg"
  131. set_ubinize_seq
  132. cat "$ubinizecfg"
  133. ubinize $ubinize_seq -o "$outfile" $ubinize_param "$ubinizecfg"
  134. err="$?"
  135. [ ! -e "$outfile" ] && err=2
  136. rm "$ubinizecfg"
  137. exit $err