contrib/package: target name was stripped from base-files package name upstream,...
[project/luci.git] / contrib / package / remote-update / files / usr / sbin / remote-update
1 #!/bin/sh
2
3 local tempfile=/tmp/remote-upgrade.img
4 local D2='\([0-9]\{2\}\)'
5 local D4='\([0-9]\{4\}\)'
6 local NL='
7 '
8
9 find_architecture()
10 {
11         local ifs="$IFS"; IFS="-"
12         set -- $(opkg list_installed kernel)
13         IFS="$ifs"
14
15         echo "$3"
16 }
17
18 find_image()
19 {
20         case "$1" in
21                 atheros)
22                         if grep -q '"vmlinux.bin.l7"' /proc/mtd; then
23                                 echo "openwrt-fonera-combined.img"
24                         else
25                                 echo "openwrt-ubiquity-combined.img"
26                         fi
27                 ;;
28                 ar71xx)
29                         if grep -q '"kernel"' /proc/mtd; then
30                                 echo "openwrt-ar71xx-combined.img"
31                         fi
32                 ;;
33                 brcm-2.4)
34                         echo "openwrt-brcm-2.4-squashfs.trx"
35                 ;;
36         esac
37 }
38
39 check_image()
40 {
41         local file; for file in /lib/upgrade/*.sh; do . $file; done
42         if platform_check_image "$1" >/dev/null 2>/dev/null; then
43                 return 0
44         fi
45         return 1
46 }
47
48 find_remote_checksum()
49 {
50         wget -qO- ${1%/*}/md5sums 2>/dev/null | \
51                 sed -ne '/'$2'/ { s/ .*//p }'
52 }
53
54 find_local_checksum()
55 {
56         set -- $(md5sum "$tempfile")
57         echo $1
58 }
59
60 find_remote_info()
61 {
62         wget -qO- "${1%/*}/VERSION.txt" 2>/dev/null
63 }
64
65 find_remote_version()
66 {
67         find_remote_info "$1" | \
68                 sed -ne "s!.*$D4/$D2/$D2 $D2:$D2.*!\\1\\2\\3\\4\\5!p;t"
69 }
70
71 find_local_version()
72 {
73         if [ -f /rom/etc/banner ]; then
74                 sed -ne "s!.*$D4/$D2/$D2 $D2:$D2.*!\\1\\2\\3\\4\\5!p;t" \
75                         /rom/etc/banner
76         else
77                 date +"%Y%m%d%H%M" -r /bin/sh
78         fi
79 }
80
81 stop_service()
82 {
83         [ -x /etc/init.d/$1 ] && {
84                 echo -n "Stopping service $1 ... "
85                 /etc/init.d/$1 stop >/dev/null 2>/dev/null
86                 echo "done"
87         }
88 }
89
90 do_wait()
91 {
92         if [ ${1:-0} -gt 0 ]; then
93                 echo -n "${2:-Waiting} "
94                 for i in $(seq 1 $1); do
95                         printf "%-2dseconds" $(($1-$i))
96                         sleep 1
97                         echo -en "\b\b\b\b\b\b\b\b\b"
98                 done
99                 echo "${NL}"
100         fi
101 }
102
103 version_compare()
104 {
105         local v1="$1"
106         local v2="$2"
107
108         while [ -n "$v1" -o -n "$v2" ]; do
109                 if [ -z "${v2:0:4}" -o "${v1:0:4}" -gt "${v2:0:4}" ]; then
110                         return 1
111                 elif [ -z "${v1:0:4}" -o "${v1:0:4}" -lt "${v2:0:4}" ]; then
112                         return 2
113                 fi
114
115                 v1="${v1:4}"
116                 v2="${v2:4}"
117         done
118
119         return 0
120 }
121
122 usage()
123 {
124         cat <<EOT
125
126 Usage:
127   remote-update -h
128   remote-update [-u <update url>] -c
129   remote-update [-v] [-y] [-u <update url>] -w
130   remote-update [-d] [-n] [-v] [-y] [-s <sleep seconds>] [-u <update url>]
131
132 Actions:
133   -h    Display this help message and exit.
134   -c    Check for firmware update and exit.
135   -w    Fetch image and exit, do not perform flash write.
136
137 Options:
138   -d    Do not detach from terminal.
139   -n    Do not backup configuration.
140   -v    Skip verification of downloaded image.
141   -y    Assume defaults for all questions.
142
143   -s <seconds>
144     Sleep given amount of seconds before starting flash write.
145     If ommitted and '-y' is not used, 5 seconds are assumed.
146
147   -u <url>
148     Fetch firmware image from given url. A file "md5sums" is expected
149     in the same remote directory. If there is no such file, use -v to
150     suppress verification.
151
152 EOT
153
154         exit 1
155 }
156
157
158 while getopts "s:u:cdnvwyh" flag; do
159         case $flag in
160                 s) sleeptime="$OPTARG";;
161                 u) updateurl="$OPTARG";;
162                 c) checkupdate=1;;
163                 d) nodetach=1;;
164                 n) nobackup=1;;
165                 v) noverify=1;;
166                 w) noflash=1;;
167                 y) noquestions=1;;
168                 *) usage;;
169         esac
170 done
171
172
173 local image_url="$updateurl"
174 local image_name="${image_url##*/}"
175
176 [ -z "$image_url" ] && {
177         local arch=$(find_architecture)
178         local image=$(find_image "$arch")
179         local repo=$(uci get freifunk.upgrade.repository 2>/dev/null)
180         repo=${repo:-$(uci get system.upgrade.repository 2>/dev/null)}
181
182         [ -z "$arch" ] && {
183                 echo "Can not determine the current architecture."
184                 exit 1
185         }
186
187         [ -z "$repo" ] && {
188                 echo "No repository configured in 'system.upgrade.repository'."
189                 echo "Use the '-u' flag to specify an image location."
190                 exit 1
191         }
192
193         [ -z "$image" ] && {
194                 echo "No suitable image for the '$arch' architecture."
195                 echo "Your platform is not supported."
196                 exit 1
197         }
198
199         echo "Architecture: $arch"
200         echo "Repository:   $repo"
201
202         image_name="$image"
203         image_url="${repo%/}/$arch/$image"
204 }
205
206
207 if [ "$checkupdate" = 1 ]; then
208         local v1=$(find_local_version)
209         local v2=$(find_remote_version "$image_url")
210
211         [ -n "$v1" -a -n "$v2" ] && {
212                 version_compare "$v1" "$v2"
213                 [ $? == 2 ] && {
214                         echo "Update available!${NL}Local:  $v1${NL}Remote: $v2${NL}--"
215                         find_remote_info "$image_url"
216                         exit 0
217                 } || {
218                         echo "Local version $v1 is up to date"
219                         exit 2
220                 }
221         } || {
222                 echo "No remote time stamp found."
223                 exit 1
224         }
225 else
226         if [ "$noquestions" != 1 ]; then
227                 echo -n "${NL}About to download $image_name. Continue? [y] "
228                 read answer
229                 case "$answer" in
230                         [nN]) exit 1;;
231                 esac
232         fi
233
234         echo -n "Downloading $image_name ... "
235         rm -f $tempfile
236         wget -qO $tempfile "$image_url" 2>/dev/null
237         [ $? == 0 ] && echo done || {
238                 echo failed
239                 rm -f $tempfile
240                 exit 1
241         }
242
243         if [ "$noverify" != 1 ]; then
244                 echo -n "Verifying $image_name ... "
245
246                 local md5_remote=$(find_remote_checksum "$image_url" "$image_name")
247                 local md5_local=$(find_local_checksum)
248
249                 check_image "$tempfile"
250                 local image_ok=$?
251
252                 if [ $image_ok = 0 -a -n "$md5_remote" -a -n "$md5_local" -a "$md5_remote" = "$md5_local" ]; then
253                         echo "done"
254                 else
255                         if [ $image_ok != 0 ]; then
256                                 echo "unsupported image type"
257                         else
258                                 echo "checksum mismatch! (local:${md5_local:-(none)} remote:${md5_remote:-(none)})"
259                         fi
260
261                         local answer=n
262                         if [ "$noquestions" != 1 ]; then
263                                 echo -n "${NL}Verification failed. Continue anyway? [n] "
264                                 read answer
265                         fi
266
267                         case "$answer" in
268                                 [yYjJ]*) : ;;
269                                 *)
270                                         echo "Aborting."
271                                         rm -f $tempfile
272                                         exit 1
273                                 ;;
274                         esac
275                 fi
276         fi
277
278         if [ "$noflash" != 1 ]; then
279                 if [ -f "$tempfile" ]; then
280                         if [ "$noquestions" == 1 ]; then
281                                 do_wait ${sleeptime:-5} "${NL}About to start flashing, hit <Ctrl-C> to abort!${NL}${NL}Starting in"
282                         else
283                                 if [ -z "$nobackup" ]; then
284                                         echo -n "${NL}Keep configuration files? [y] "
285                                         read answer
286                                         case "$answer" in
287                                                 [nN]) nobackup=1;;
288                                         esac
289                                 fi
290
291                                 echo -n "${NL}About to start flashing!${NL}Hit <Enter> to continue or <Ctrl-C> to abort.${NL}"
292                                 read answer
293                         fi
294
295                         for s in lucid collectd; do stop_service $s; done
296
297                         if [ "$nodetach" != 1 ]; then
298                                 echo -n "Starting sysupgrade in background ... "
299                                 /bin/busybox start-stop-daemon -S -b -x /sbin/sysupgrade -- ${nobackup:+-n} "$tempfile"
300                                 echo "done"
301                         else
302                                 echo "Executing sysupgrade ... "
303                                 exec /sbin/sysupgrade ${nobackup:+-n} "$tempfile"
304                         fi
305                 else
306                         echo "No upgrade image found!"
307                         exit 1
308                 fi
309         else
310                 echo "Image saved in '$tempfile'"
311         fi
312 fi