ubinize-image.sh 2.0 KB

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