de831b2eaeca4a6832321b6efa47b57dcdd658a1
[openwrt/svn-archive/archive.git] / target / linux / ar71xx / base-files / lib / upgrade / platform.sh
1 #
2 # Copyright (C) 2011 OpenWrt.org
3 #
4
5 . /lib/functions/system.sh
6 . /lib/ar71xx.sh
7
8 PART_NAME=firmware
9 RAMFS_COPY_DATA=/lib/ar71xx.sh
10
11 CI_BLKSZ=65536
12 CI_LDADR=0x80060000
13
14 platform_find_partitions() {
15 local first dev size erasesize name
16 while read dev size erasesize name; do
17 name=${name#'"'}; name=${name%'"'}
18 case "$name" in
19 vmlinux.bin.l7|vmlinux|kernel|linux|linux.bin|rootfs|filesystem)
20 if [ -z "$first" ]; then
21 first="$name"
22 else
23 echo "$erasesize:$first:$name"
24 break
25 fi
26 ;;
27 esac
28 done < /proc/mtd
29 }
30
31 platform_find_kernelpart() {
32 local part
33 for part in "${1%:*}" "${1#*:}"; do
34 case "$part" in
35 vmlinux.bin.l7|vmlinux|kernel|linux|linux.bin)
36 echo "$part"
37 break
38 ;;
39 esac
40 done
41 }
42
43 platform_do_upgrade_combined() {
44 local partitions=$(platform_find_partitions)
45 local kernelpart=$(platform_find_kernelpart "${partitions#*:}")
46 local erase_size=$((0x${partitions%%:*})); partitions="${partitions#*:}"
47 local kern_length=0x$(dd if="$1" bs=2 skip=1 count=4 2>/dev/null)
48 local kern_blocks=$(($kern_length / $CI_BLKSZ))
49 local root_blocks=$((0x$(dd if="$1" bs=2 skip=5 count=4 2>/dev/null) / $CI_BLKSZ))
50
51 if [ -n "$partitions" ] && [ -n "$kernelpart" ] && \
52 [ ${kern_blocks:-0} -gt 0 ] && \
53 [ ${root_blocks:-0} -gt 0 ] && \
54 [ ${erase_size:-0} -gt 0 ];
55 then
56 local append=""
57 [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
58
59 ( dd if="$1" bs=$CI_BLKSZ skip=1 count=$kern_blocks 2>/dev/null; \
60 dd if="$1" bs=$CI_BLKSZ skip=$((1+$kern_blocks)) count=$root_blocks 2>/dev/null ) | \
61 mtd -r $append -F$kernelpart:$kern_length:$CI_LDADR,rootfs write - $partitions
62 fi
63 }
64
65 tplink_get_image_hwid() {
66 get_image "$@" | dd bs=4 count=1 skip=16 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
67 }
68
69 tplink_get_image_boot_size() {
70 get_image "$@" | dd bs=4 count=1 skip=37 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
71 }
72
73 seama_get_type_magic() {
74 get_image "$@" | dd bs=1 count=4 skip=53 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
75 }
76
77 cybertan_get_image_magic() {
78 get_image "$@" | dd bs=8 count=1 skip=0 2>/dev/null | hexdump -v -n 8 -e '1/1 "%02x"'
79 }
80
81 cybertan_check_image() {
82 local magic="$(cybertan_get_image_magic "$1")"
83 local fw_magic="$(cybertan_get_hw_magic)"
84
85 [ "$fw_magic" != "$magic" ] && {
86 echo "Invalid image, ID mismatch, got:$magic, but need:$fw_magic"
87 return 1
88 }
89
90 return 0
91 }
92
93 platform_do_upgrade_compex() {
94 local fw_file=$1
95 local fw_part=$PART_NAME
96 local fw_mtd=$(find_mtd_part $fw_part)
97 local fw_length=0x$(dd if="$fw_file" bs=2 skip=1 count=4 2>/dev/null)
98 local fw_blocks=$(($fw_length / 65536))
99
100 if [ -n "$fw_mtd" ] && [ ${fw_blocks:-0} -gt 0 ]; then
101 local append=""
102 [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
103
104 sync
105 dd if="$fw_file" bs=64k skip=1 count=$fw_blocks 2>/dev/null | \
106 mtd $append write - "$fw_part"
107 fi
108 }
109
110 alfa_check_image() {
111 local magic_long="$(get_magic_long "$1")"
112 local fw_part_size=$(mtd_get_part_size firmware)
113
114 case "$magic_long" in
115 "27051956")
116 [ "$fw_part_size" != "16318464" ] && {
117 echo "Invalid image magic \"$magic_long\" for $fw_part_size bytes"
118 return 1
119 }
120 ;;
121
122 "68737173")
123 [ "$fw_part_size" != "7929856" ] && {
124 echo "Invalid image magic \"$magic_long\" for $fw_part_size bytes"
125 return 1
126 }
127 ;;
128 esac
129
130 return 0
131 }
132
133 platform_check_image() {
134 local board=$(ar71xx_board_name)
135 local magic="$(get_magic_word "$1")"
136 local magic_long="$(get_magic_long "$1")"
137
138 [ "$#" -gt 1 ] && return 1
139
140 case "$board" in
141 all0315n | \
142 all0258n | \
143 cap4200ag)
144 platform_check_image_allnet "$1" && return 0
145 return 1
146 ;;
147 alfa-ap96 | \
148 alfa-nx | \
149 ap113 | \
150 ap121 | \
151 ap121-mini | \
152 ap136-010 | \
153 ap136-020 | \
154 ap135-020 | \
155 ap96 | \
156 db120 | \
157 hornet-ub | \
158 bxu2000n-2-a1 | \
159 zcn-1523h-2 | \
160 zcn-1523h-5)
161 [ "$magic_long" != "68737173" -a "$magic_long" != "19852003" ] && {
162 echo "Invalid image type."
163 return 1
164 }
165 return 0
166 ;;
167 ap81 | \
168 ap83 | \
169 ap132 | \
170 dir-505-a1 | \
171 dir-600-a1 | \
172 dir-615-c1 | \
173 dir-615-e1 | \
174 dir-615-e4 | \
175 dir-825-c1 | \
176 dir-835-a1 | \
177 dragino2 | \
178 esr1750 | \
179 ew-dorin | \
180 ew-dorin-router | \
181 hiwifi-hc6361 | \
182 hornet-ub-x2 | \
183 mzk-w04nu | \
184 mzk-w300nh | \
185 tew-632brp | \
186 tew-712br | \
187 tew-732br | \
188 wrt400n | \
189 airgateway | \
190 airrouter | \
191 bullet-m | \
192 nanostation-m | \
193 rocket-m | \
194 nanostation-m-xw | \
195 rw2458n | \
196 wndap360 | \
197 wzr-hp-g300nh2 | \
198 wzr-hp-g300nh | \
199 wzr-hp-g450h | \
200 wzr-hp-ag300h | \
201 whr-g301n | \
202 whr-hp-g300n | \
203 whr-hp-gn | \
204 wlae-ag300n | \
205 nbg460n_550n_550nh | \
206 unifi | \
207 unifi-outdoor | \
208 carambola2 )
209 [ "$magic" != "2705" ] && {
210 echo "Invalid image type."
211 return 1
212 }
213 return 0
214 ;;
215
216 dir-825-b1 | \
217 tew-673gru)
218 dir825b_check_image "$1" && return 0
219 ;;
220
221 mynet-rext|\
222 wrt160nl)
223 cybertan_check_image "$1" && return 0
224 return 1
225 ;;
226
227 mynet-n600 | \
228 mynet-n750)
229 [ "$magic_long" != "5ea3a417" ] && {
230 echo "Invalid image, bad magic: $magic_long"
231 return 1
232 }
233
234 local typemagic=$(seama_get_type_magic "$1")
235 [ "$typemagic" != "6669726d" ] && {
236 echo "Invalid image, bad type: $typemagic"
237 return 1
238 }
239
240 return 0;
241 ;;
242 mr600 | \
243 mr600v2 | \
244 om2p | \
245 om2pv2 | \
246 om2p-hs | \
247 om2p-hsv2 | \
248 om2p-lc | \
249 om5p)
250 platform_check_image_openmesh "$magic_long" "$1" && return 0
251 return 1
252 ;;
253
254 archer-c7 | \
255 el-m150 | \
256 el-mini | \
257 gl-inet | \
258 oolite | \
259 tl-mr10u | \
260 tl-mr11u | \
261 tl-mr13u | \
262 tl-mr3020 | \
263 tl-mr3040 | \
264 tl-mr3040-v2 | \
265 tl-mr3220 | \
266 tl-mr3220-v2 | \
267 tl-mr3420 | \
268 tl-mr3420-v2 | \
269 tl-wa7510n | \
270 tl-wa750re | \
271 tl-wa850re | \
272 tl-wa860re | \
273 tl-wa801nd-v2 | \
274 tl-wa901nd | \
275 tl-wa901nd-v2 | \
276 tl-wa901nd-v3 | \
277 tl-wdr3500 | \
278 tl-wdr4300 | \
279 tl-wdr4900-v2 | \
280 tl-wr703n | \
281 tl-wr710n | \
282 tl-wr720n-v3 | \
283 tl-wr741nd | \
284 tl-wr741nd-v4 | \
285 tl-wr841n-v1 | \
286 tl-wa830re-v2 | \
287 tl-wr841n-v7 | \
288 tl-wr841n-v8 | \
289 tl-wr841n-v9 | \
290 tl-wr842n-v2 | \
291 tl-wr941nd | \
292 tl-wr1041n-v2 | \
293 tl-wr1043nd | \
294 tl-wr1043nd-v2 | \
295 tl-wr2543n)
296 [ "$magic" != "0100" ] && {
297 echo "Invalid image type."
298 return 1
299 }
300
301 local hwid
302 local imageid
303
304 hwid=$(tplink_get_hwid)
305 imageid=$(tplink_get_image_hwid "$1")
306
307 [ "$hwid" != "$imageid" ] && {
308 echo "Invalid image, hardware ID mismatch, hw:$hwid image:$imageid."
309 return 1
310 }
311
312 local boot_size
313
314 boot_size=$(tplink_get_image_boot_size "$1")
315 [ "$boot_size" != "00000000" ] && {
316 echo "Invalid image, it contains a bootloader."
317 return 1
318 }
319
320 return 0
321 ;;
322
323 tube2h)
324 alfa_check_image "$1" && return 0
325 return 1
326 ;;
327
328 uap-pro)
329 [ "$magic_long" != "19852003" ] && {
330 echo "Invalid image type."
331 return 1
332 }
333 return 0
334 ;;
335 wndr3700 | \
336 wnr2000-v3 | \
337 wnr612-v2)
338 local hw_magic
339
340 hw_magic="$(ar71xx_get_mtd_part_magic firmware)"
341 [ "$magic_long" != "$hw_magic" ] && {
342 echo "Invalid image, hardware ID mismatch, hw:$hw_magic image:$magic_long."
343 return 1
344 }
345 return 0
346 ;;
347 nbg6716 | \
348 wndr3700v4 | \
349 wndr4300 )
350 nand_do_platform_check $board $1
351 return $?;
352 ;;
353 routerstation | \
354 routerstation-pro | \
355 ls-sr71 | \
356 pb42 | \
357 pb44 | \
358 all0305 | \
359 eap300v2 | \
360 eap7660d | \
361 ja76pf | \
362 ja76pf2 | \
363 jwap003 | \
364 wp543 | \
365 wpe72)
366 [ "$magic" != "4349" ] && {
367 echo "Invalid image. Use *-sysupgrade.bin files on this board"
368 return 1
369 }
370
371 local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
372 local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
373
374 if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
375 return 0
376 else
377 echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
378 return 1
379 fi
380 return 0
381 ;;
382 esac
383
384 echo "Sysupgrade is not yet supported on $board."
385 return 1
386 }
387
388 platform_do_upgrade() {
389 local board=$(ar71xx_board_name)
390
391 case "$board" in
392 routerstation | \
393 routerstation-pro | \
394 ls-sr71 | \
395 all0305 | \
396 eap7660d | \
397 pb42 | \
398 pb44 | \
399 ja76pf | \
400 ja76pf2 | \
401 jwap003)
402 platform_do_upgrade_combined "$ARGV"
403 ;;
404 wp543|\
405 wpe72)
406 platform_do_upgrade_compex "$ARGV"
407 ;;
408 all0258n )
409 platform_do_upgrade_allnet "0x9f050000" "$ARGV"
410 ;;
411 all0315n )
412 platform_do_upgrade_allnet "0x9f080000" "$ARGV"
413 ;;
414 eap300v2 |\
415 cap4200ag)
416 platform_do_upgrade_allnet "0xbf0a0000" "$ARGV"
417 ;;
418 dir-825-b1 |\
419 tew-673gru)
420 platform_do_upgrade_dir825b "$ARGV"
421 ;;
422 mr600 | \
423 mr600v2 | \
424 om2p | \
425 om2pv2 | \
426 om2p-hs | \
427 om2p-hsv2 | \
428 om2p-lc | \
429 om5p)
430 platform_do_upgrade_openmesh "$ARGV"
431 ;;
432 uap-pro)
433 MTD_CONFIG_ARGS="-s 0x180000"
434 default_do_upgrade "$ARGV"
435 ;;
436 *)
437 default_do_upgrade "$ARGV"
438 ;;
439 esac
440 }
441
442 disable_watchdog() {
443 killall watchdog
444 ( ps | grep -v 'grep' | grep '/dev/watchdog' ) && {
445 echo 'Could not disable watchdog'
446 return 1
447 }
448 }
449
450 append sysupgrade_pre_upgrade disable_watchdog