ubinize-image.sh 2.4 KB

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