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