ubinize-image.sh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. shift
  63. continue
  64. ;;
  65. "-"*)
  66. ubinize_param="$@"
  67. break
  68. ;;
  69. *)
  70. if [ ! "$rootfs" ]; then
  71. rootfs=$1
  72. shift
  73. continue
  74. fi
  75. if [ ! "$outfile" ]; then
  76. outfile=$1
  77. shift
  78. continue
  79. fi
  80. ;;
  81. esac
  82. done
  83. if [ ! -r "$rootfs" -o ! -r "$kernel" -a ! "$outfile" ]; then
  84. echo "syntax: $0 [--uboot-env] [--kernel kernelimage] rootfs out [ubinize opts]"
  85. exit 1
  86. fi
  87. ubinize="$( which ubinize )"
  88. if [ ! -x "$ubinize" ]; then
  89. echo "ubinize tool not found or not usable"
  90. exit 1
  91. fi
  92. ubinizecfg="$( mktemp )"
  93. ubilayout "$ubootenv" "$rootfs" "$kernel" > "$ubinizecfg"
  94. cat "$ubinizecfg"
  95. ubinize -o "$outfile" $ubinize_param "$ubinizecfg"
  96. err="$?"
  97. [ ! -e "$outfile" ] && err=2
  98. rm "$ubinizecfg"
  99. exit $err