scripts: ubinize-image.sh: support static volumes, make size optional
[openwrt/staging/jow.git] / scripts / ubinize-image.sh
1 #!/bin/sh
2
3 . $TOPDIR/scripts/functions.sh
4
5 part=""
6 ubootenv=""
7 ubinize_param=""
8 kernel=""
9 rootfs=""
10 outfile=""
11 err=""
12 ubinize_seq=""
13
14 ubivol() {
15 local volid=$1
16 local name=$2
17 local image=$3
18 local autoresize=$4
19 local size="$5"
20 local voltype="${6:-dynamic}"
21 echo "[$name]"
22 echo "mode=ubi"
23 echo "vol_id=$volid"
24 echo "vol_type=$voltype"
25 echo "vol_name=$name"
26 if [ "$image" ]; then
27 echo "image=$image"
28 [ -n "$size" ] && echo "vol_size=${size}"
29 else
30 echo "vol_size=1MiB"
31 fi
32 if [ "$autoresize" ]; then
33 echo "vol_flags=autoresize"
34 fi
35 }
36
37 ubilayout() {
38 local vol_id=0
39 local rootsize=
40 local autoresize=
41 local rootfs_type="$( get_fs_type "$2" )"
42 local voltype
43
44 if [ "$1" = "ubootenv" ]; then
45 ubivol $vol_id ubootenv
46 vol_id=$(( $vol_id + 1 ))
47 ubivol $vol_id ubootenv2
48 vol_id=$(( $vol_id + 1 ))
49 fi
50 for part in $parts; do
51 name="${part%%=*}"
52 prev="$part"
53 part="${part#*=}"
54 voltype=dynamic
55 [ "$prev" = "$part" ] && part=
56
57 image="${part%%=*}"
58 if [ "${image:0:1}" = ":" ]; then
59 voltype=static
60 image="${image:1}"
61 fi
62 prev="$part"
63 part="${part#*=}"
64 [ "$prev" = "$part" ] && part=
65
66 size="$part"
67 if [ -z "$size" ]; then
68 size="$( round_up "$( stat -c%s "$image" )" 1024 )"
69 else
70 size="${size}MiB"
71 fi
72
73 ubivol $vol_id "$name" "$image" "" "${size}" "$voltype"
74 vol_id=$(( $vol_id + 1 ))
75 done
76 if [ "$3" ]; then
77 ubivol $vol_id kernel "$3"
78 vol_id=$(( $vol_id + 1 ))
79 fi
80
81 if [ "$2" ]; then
82 case "$rootfs_type" in
83 "ubifs")
84 autoresize=1
85 ;;
86 "squashfs")
87 # squashfs uses 1k block size, ensure we do not
88 # violate that
89 rootsize="$( round_up "$( stat -c%s "$2" )" 1024 )"
90 ;;
91 esac
92 ubivol $vol_id rootfs "$2" "$autoresize" "$rootsize" dynamic
93
94 vol_id=$(( $vol_id + 1 ))
95 [ "$rootfs_type" = "ubifs" ] || ubivol $vol_id rootfs_data "" 1 dymamic
96 fi
97 }
98
99 set_ubinize_seq() {
100 if [ -n "$SOURCE_DATE_EPOCH" ] ; then
101 ubinize_seq="-Q $SOURCE_DATE_EPOCH"
102 fi
103 }
104
105 while [ "$1" ]; do
106 case "$1" in
107 "--uboot-env")
108 ubootenv="ubootenv"
109 shift
110 continue
111 ;;
112 "--kernel")
113 kernel="$2"
114 shift
115 shift
116 continue
117 ;;
118 "--rootfs")
119 rootfs="$2"
120 shift
121 shift
122 continue
123 ;;
124 "--part")
125 parts="$parts $2"
126 shift
127 shift
128 continue
129 ;;
130 "-"*)
131 ubinize_param="$@"
132 break
133 ;;
134 *)
135 if [ ! "$outfile" ]; then
136 outfile=$1
137 shift
138 continue
139 fi
140 ;;
141 esac
142 done
143
144 if [ ! -r "$rootfs" -a ! -r "$kernel" -a ! "$outfile" ]; then
145 echo "syntax: $0 [--uboot-env] [--part <name>=<file>] [--kernel kernelimage] [--rootfs rootfsimage] out [ubinize opts]"
146 exit 1
147 fi
148
149 ubinize="$( command -v ubinize )"
150 if [ ! -x "$ubinize" ]; then
151 echo "ubinize tool not found or not usable"
152 exit 1
153 fi
154
155 ubinizecfg="$( mktemp 2> /dev/null )"
156 if [ -z "$ubinizecfg" ]; then
157 # try OSX signature
158 ubinizecfg="$( mktemp -t 'ubitmp' )"
159 fi
160 ubilayout "$ubootenv" "$rootfs" "$kernel" > "$ubinizecfg"
161
162 set_ubinize_seq
163 cat "$ubinizecfg"
164 ubinize $ubinize_seq -o "$outfile" $ubinize_param "$ubinizecfg"
165 err="$?"
166 [ ! -e "$outfile" ] && err=2
167 rm "$ubinizecfg"
168
169 exit $err