ubinize-image.sh 2.3 KB

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