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