ubinize-image.sh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/bin/sh
  2. ubootenv=""
  3. ubinize_param=""
  4. kernel=""
  5. rootfs=""
  6. outfile=""
  7. err=""
  8. get_magic_word() {
  9. dd if=$1 bs=2 count=1 2>/dev/null | hexdump -v -n 2 -e '1/1 "%02x"'
  10. }
  11. is_ubifs() {
  12. if [ "$( get_magic_word $1 )" = "3118" ]; then
  13. echo "1"
  14. fi
  15. }
  16. ubivol() {
  17. volid=$1
  18. name=$2
  19. image=$3
  20. autoresize=$4
  21. echo "[$name]"
  22. echo "mode=ubi"
  23. echo "vol_id=$volid"
  24. echo "vol_type=dynamic"
  25. echo "vol_name=$name"
  26. if [ "$image" ]; then
  27. echo "image=$image"
  28. else
  29. echo "vol_size=1MiB"
  30. fi
  31. if [ "$autoresize" ]; then
  32. echo "vol_flags=autoresize"
  33. fi
  34. }
  35. ubilayout() {
  36. local vol_id=0
  37. local root_is_ubifs="$( is_ubifs "$2" )"
  38. if [ "$1" = "ubootenv" ]; then
  39. ubivol $vol_id ubootenv
  40. vol_id=$(( $vol_id + 1 ))
  41. ubivol $vol_id ubootenv2
  42. vol_id=$(( $vol_id + 1 ))
  43. fi
  44. if [ "$3" ]; then
  45. ubivol $vol_id kernel "$3"
  46. vol_id=$(( $vol_id + 1 ))
  47. fi
  48. ubivol $vol_id rootfs "$2" $root_is_ubifs
  49. vol_id=$(( $vol_id + 1 ))
  50. [ "$root_is_ubifs" ] || ubivol $vol_id rootfs_data "" 1
  51. }
  52. while [ "$1" ]; do
  53. case "$1" in
  54. "--uboot-env")
  55. ubootenv="ubootenv"
  56. shift
  57. continue
  58. ;;
  59. "--kernel")
  60. kernel="$2"
  61. shift
  62. continue
  63. ;;
  64. "-"*)
  65. ubinize_param="$@"
  66. break
  67. ;;
  68. *)
  69. if [ ! "$rootfs" ]; then
  70. rootfs=$1
  71. shift
  72. continue
  73. fi
  74. if [ ! "$outfile" ]; then
  75. outfile=$1
  76. shift
  77. continue
  78. fi
  79. ;;
  80. esac
  81. done
  82. if [ ! -r "$rootfs" -o ! -r "$kernel" -a ! "$outfile" ]; then
  83. echo "syntax: $0 [--uboot-env] [--kernel kernelimage] rootfs out [ubinize opts]"
  84. exit 1
  85. fi
  86. ubinize="$( which ubinize )"
  87. if [ ! -x "$ubinize" ]; then
  88. echo "ubinize tool not found or not usable"
  89. exit 1
  90. fi
  91. ubinizecfg="$( mktemp )"
  92. ubilayout "$ubootenv" "$rootfs" "$kernel" > "$ubinizecfg"
  93. cat "$ubinizecfg"
  94. ubinize -o "$outfile" $ubinize_param "$ubinizecfg"
  95. err="$?"
  96. [ ! -e "$outfile" ] && err=2
  97. rm "$ubinizecfg"
  98. exit $err