1
0

ubinize-image.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. if [ "$2" ]; then
  61. case "$rootfs_type" in
  62. "ubifs")
  63. autoresize=1
  64. ;;
  65. "squashfs")
  66. # squashfs uses 1k block size, ensure we do not
  67. # violate that
  68. rootsize="$( round_up "$( stat -c%s "$2" )" 1024 )"
  69. ;;
  70. esac
  71. ubivol $vol_id rootfs "$2" "$autoresize" "$rootsize"
  72. vol_id=$(( $vol_id + 1 ))
  73. [ "$rootfs_type" = "ubifs" ] || ubivol $vol_id rootfs_data "" 1
  74. fi
  75. }
  76. set_ubinize_seq() {
  77. if [ -n "$SOURCE_DATE_EPOCH" ] ; then
  78. ubinize_seq="-Q $SOURCE_DATE_EPOCH"
  79. fi
  80. }
  81. while [ "$1" ]; do
  82. case "$1" in
  83. "--uboot-env")
  84. ubootenv="ubootenv"
  85. shift
  86. continue
  87. ;;
  88. "--kernel")
  89. kernel="$2"
  90. shift
  91. shift
  92. continue
  93. ;;
  94. "--rootfs")
  95. rootfs="$2"
  96. shift
  97. shift
  98. continue
  99. ;;
  100. "--part")
  101. parts="$parts $2"
  102. shift
  103. shift
  104. continue
  105. ;;
  106. "-"*)
  107. ubinize_param="$@"
  108. break
  109. ;;
  110. *)
  111. if [ ! "$outfile" ]; then
  112. outfile=$1
  113. shift
  114. continue
  115. fi
  116. ;;
  117. esac
  118. done
  119. if [ ! -r "$rootfs" -a ! -r "$kernel" -a ! "$outfile" ]; then
  120. echo "syntax: $0 [--uboot-env] [--part <name>=<file>] [--kernel kernelimage] [--rootfs rootfsimage] out [ubinize opts]"
  121. exit 1
  122. fi
  123. ubinize="$( command -v ubinize )"
  124. if [ ! -x "$ubinize" ]; then
  125. echo "ubinize tool not found or not usable"
  126. exit 1
  127. fi
  128. ubinizecfg="$( mktemp 2> /dev/null )"
  129. if [ -z "$ubinizecfg" ]; then
  130. # try OSX signature
  131. ubinizecfg="$( mktemp -t 'ubitmp' )"
  132. fi
  133. ubilayout "$ubootenv" "$rootfs" "$kernel" > "$ubinizecfg"
  134. set_ubinize_seq
  135. cat "$ubinizecfg"
  136. ubinize $ubinize_seq -o "$outfile" $ubinize_param "$ubinizecfg"
  137. err="$?"
  138. [ ! -e "$outfile" ] && err=2
  139. rm "$ubinizecfg"
  140. exit $err