ubinize-image.sh 1.9 KB

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