summaryrefslogtreecommitdiffhomepage
path: root/modules/freifunk/root/usr/sbin/remote-update
blob: 3f50f32073c3075c80e41ccf429dd5ea972825dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/bin/sh

local tempfile=/tmp/remote-upgrade.img
local D2='\([0-9]\{2\}\)'
local D4='\([0-9]\{4\}\)'
local NL='
'

find_architecture()
{
	opkg list_installed 'base-files-*' | \
		sed -ne 's/base-files-\([^ ]\+\).*/\1/p'
}

find_image()
{
	case "$1" in
		atheros)
			if grep -q '"vmlinux.bin.l7"' /proc/mtd; then
				echo "openwrt-fonera-combined.img"
			else
				echo "openwrt-ubiquity-combined.img"
			fi
		;;
		brcm-2.4)
			echo "openwrt-brcm-2.4-squashfs.trx"
		;;
	esac
}

check_image()
{
	local file; for file in /lib/upgrade/*.sh; do . $file; done
	if platform_check_image "$1" >/dev/null 2>/dev/null; then
		return 0
	fi
	return 1
}

find_remote_checksum()
{
	wget -qO- ${1%/*}/md5sums 2>/dev/null | \
		sed -ne '/'$2'/ { s/ .*//p }'
}

find_local_checksum()
{
	set -- $(md5sum "$tempfile")
	echo $1
}

find_remote_version()
{
	wget -qO- "${1%/*}/VERSION.txt" 2>/dev/null | \
		sed -ne "s!.*$D4/$D2/$D2 $D2:$D2.*!\\1\\2\\3\\4\\5!p;t"
}

find_local_version()
{
	if [ -f /rom/etc/banner ]; then
		sed -ne "s!.*$D4/$D2/$D2 $D2:$D2.*!\\1\\2\\3\\4\\5!p;t" \
			/rom/etc/banner
	else
		date +"%Y%m%d%H%M" -r /bin/sh
	fi
}

stop_service()
{
	[ -x /etc/init.d/$1 ] && {
		echo -n "Stopping service $1 ... "
		/etc/init.d/$1 stop >/dev/null 2>/dev/null
		echo "done"
	}
}

do_wait()
{
	if [ ${1:-0} -gt 0 ]; then
		echo -n "${2:-Waiting} "
		for i in $(seq 1 $1); do
			printf "%-2dseconds" $(($1-$i))
			sleep 1
			echo -en "\b\b\b\b\b\b\b\b\b"
		done
		echo "${NL}"
	fi
}

version_compare()
{
	local v1="$1"
	local v2="$2"

	while [ -n "$v1" -o -n "$v2" ]; do
		if [ -z "${v2:0:4}" -o "${v1:0:4}" -gt "${v2:0:4}" ]; then
			return 1
		elif [ -z "${v1:0:4}" -o "${v1:0:4}" -lt "${v2:0:4}" ]; then
			return 2
		fi

		v1="${v1:4}"
		v2="${v2:4}"
	done

	return 0
}

usage()
{
	cat <<EOT

Usage:
  remote-update -h
  remote-update [-u <update url>] -c
  remote-update [-v] [-y] [-u <update url>] -w
  remote-update [-d] [-n] [-v] [-y] [-s <sleep seconds>] [-u <update url>]

Actions:
  -h    Display this help message and exit.
  -c    Check for firmware update and exit.
  -w    Fetch image and exit, do not perform flash write.

Options:
  -d    Do not detach from terminal.
  -n    Do not backup configuration.
  -v    Skip verification of downloaded image.
  -y    Assume defaults for all questions.

  -s <seconds>
    Sleep given amount of seconds before starting flash write.
    If ommitted and '-y' is not used, 5 seconds are assumed.

  -u <url>
    Fetch firmware image from given url. A file "md5sums" is expected
    in the same remote directory. If there is no such file, use -v to
    suppress verification.

EOT

	exit 1
}


while getopts "s:u:cdnvwyh" flag; do
	case $flag in
		s) sleeptime="$OPTARG";;
		u) updateurl="$OPTARG";;
		c) checkupdate=1;;
		d) nodetach=1;;
		n) nobackup=1;;
		v) noverify=1;;
		w) noflash=1;;
		y) noquestions=1;;
		*) usage;;
	esac
done


local image_url="$updateurl"
local image_name="${image_url##*/}"

[ -z "$image_url" ] && {
	local arch=$(find_architecture)
	local image=$(find_image "$arch")
	local repo=$(uci get freifunk.upgrade.repository 2>/dev/null)

	[ -z "$arch" ] && {
		echo "Can not determine the current architecture."
		exit 1
	}

	[ -z "$repo" ] && {
		echo "No repository configured in 'freifunk.upgrade.repository'."
		echo "Use the '-u' flag to specify an image location."
		exit 1
	}

	[ -z "$image" ] && {
		echo "No suitable image for the '$arch' architecture."
		echo "Your platform is not supported."
		exit 1
	}

	echo "Architecture: $arch"
	echo "Repository:   $repo"

	image_name="$image"
	image_url="${repo%/}/$arch/$image"
}


if [ "$checkupdate" = 1 ]; then
	local v1=$(find_local_version)
	local v2=$(find_remote_version "$image_url")

	[ -n "$v1" -a -n "$v2" ] && {
		version_compare "$v1" "$v2"
		[ $? == 2 ] && {
			echo "Update available! $v1 -> $v2"
		} || {
			echo "Local version $v1 is up to date"
		}
	} || {
		echo "No remote time stamp found."
		exit 1
	}
else
	if [ "$noquestions" != 1 ]; then
		echo -n "${NL}About to download $image_name. Continue? [y] "
		read answer
		case "$answer" in
			[nN]) exit 1;;
		esac
	fi

	echo -n "Downloading $image_name ... "
	rm -f $tempfile
	wget -qO $tempfile "$image_url" 2>/dev/null
	[ $? == 0 ] && echo done || {
		echo failed
		rm -f $tempfile
		exit 1
	}

	if [ "$noverify" != 1 ]; then
		echo -n "Verifying $image_name ... "

		local md5_remote=$(find_remote_checksum "$image_url" "$image_name")
		local md5_local=$(find_local_checksum)

		check_image "$tempfile"
		local image_ok=$?

		if [ $image_ok = 0 -a -n "$md5_remote" -a -n "$md5_local" -a "$md5_remote" = "$md5_local" ]; then
			echo "done"
		else
			if [ $image_ok != 0 ]; then
				echo "unsupported image type"
			else
				echo "checksum mismatch! (local:${md5_local:-(none)} remote:${md5_remote:-(none)})"
			fi

			local answer=n
			if [ "$noquestions" != 1 ]; then
				echo -n "${NL}Verification failed. Continue anyway? [n] "
				read answer
			fi

			case "$answer" in
				[yYjJ]*) : ;;
				*)
					echo "Aborting."
					rm -f $tempfile
					exit 1
				;;
			esac
		fi
	fi

	if [ "$noflash" != 1 ]; then
		if [ -f "$tempfile" ]; then
			if [ "$noquestions" == 1 ]; then
				do_wait ${sleeptime:-5} "${NL}About to start flashing, hit <Ctrl-C> to abort!${NL}${NL}Starting in"
			else
				if [ -z "$nobackup" ]; then
					echo -n "${NL}Keep configuration files? [y] "
					read answer
					case "$answer" in
						[nN]) nobackup=1;;
					esac
				fi

				echo -n "${NL}About to start flashing!${NL}Hit <Enter> to continue or <Ctrl-C> to abort.${NL}"
				read answer
			fi

			for s in lucid collectd; do stop_service $s; done

			if [ "$nodetach" != 1 ]; then
				echo -n "Starting sysupgrade in background ... "
				/bin/busybox start-stop-daemon -S -b -x /sbin/sysupgrade -- ${nobackup:+-n} "$tempfile"
				echo "done"
			else
				echo "Executing sysupgrade ... "
				exec /sbin/sysupgrade ${nobackup:+-n} "$tempfile"
			fi
		else
			echo "No upgrade image found!"
			exit 1
		fi
	else
		echo "Image saved in '$tempfile'"
	fi
fi