b97c076519d50ebe97e24becbcb996b33daffb84
[openwrt/openwrt.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 tplink_pharos_check_image() {
74 local magic_long="$(get_magic_long "$1")"
75 [ "$magic_long" != "7f454c46" ] && {
76 echo "Invalid image magic '$magic_long'"
77 return 1
78 }
79
80 local model_string="$(tplink_pharos_get_model_string)"
81 local line
82
83 # Here $1 is given to dd directly instead of get_image as otherwise the skip
84 # will take almost a second (as dd can't seek then)
85 #
86 # This will fail if the image isn't local, but that's fine: as the
87 # read loop won't be executed at all, it will return true, so the image
88 # is accepted (loading the first 1.5M of a remote image for this check seems
89 # a bit extreme)
90 dd if="$1" bs=1 skip=1511432 count=1024 2>/dev/null | while read line; do
91 [ "$line" == "$model_string" ] && break
92 done || {
93 echo "Unsupported image (model not in support-list)"
94 return 1
95 }
96
97 return 0
98 }
99
100 seama_get_type_magic() {
101 get_image "$@" | dd bs=1 count=4 skip=53 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
102 }
103
104 cybertan_get_image_magic() {
105 get_image "$@" | dd bs=8 count=1 skip=0 2>/dev/null | hexdump -v -n 8 -e '1/1 "%02x"'
106 }
107
108 cybertan_check_image() {
109 local magic="$(cybertan_get_image_magic "$1")"
110 local fw_magic="$(cybertan_get_hw_magic)"
111
112 [ "$fw_magic" != "$magic" ] && {
113 echo "Invalid image, ID mismatch, got:$magic, but need:$fw_magic"
114 return 1
115 }
116
117 return 0
118 }
119
120 platform_do_upgrade_compex() {
121 local fw_file=$1
122 local fw_part=$PART_NAME
123 local fw_mtd=$(find_mtd_part $fw_part)
124 local fw_length=0x$(dd if="$fw_file" bs=2 skip=1 count=4 2>/dev/null)
125 local fw_blocks=$(($fw_length / 65536))
126
127 if [ -n "$fw_mtd" ] && [ ${fw_blocks:-0} -gt 0 ]; then
128 local append=""
129 [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
130
131 sync
132 dd if="$fw_file" bs=64k skip=1 count=$fw_blocks 2>/dev/null | \
133 mtd $append write - "$fw_part"
134 fi
135 }
136
137 alfa_check_image() {
138 local magic_long="$(get_magic_long "$1")"
139 local fw_part_size=$(mtd_get_part_size firmware)
140
141 case "$magic_long" in
142 "27051956")
143 [ "$fw_part_size" != "16318464" ] && {
144 echo "Invalid image magic \"$magic_long\" for $fw_part_size bytes"
145 return 1
146 }
147 ;;
148
149 "68737173")
150 [ "$fw_part_size" != "7929856" ] && {
151 echo "Invalid image magic \"$magic_long\" for $fw_part_size bytes"
152 return 1
153 }
154 ;;
155 esac
156
157 return 0
158 }
159
160 platform_check_image() {
161 local board=$(ar71xx_board_name)
162 local magic="$(get_magic_word "$1")"
163 local magic_long="$(get_magic_long "$1")"
164
165 [ "$#" -gt 1 ] && return 1
166
167 case "$board" in
168 all0315n | \
169 all0258n | \
170 cap4200ag)
171 platform_check_image_allnet "$1" && return 0
172 return 1
173 ;;
174 alfa-ap96 | \
175 alfa-nx | \
176 arduino-yun | \
177 ap113 | \
178 ap121 | \
179 ap121-mini | \
180 ap136-010 | \
181 ap136-020 | \
182 ap135-020 | \
183 ap147-010 | \
184 ap96 | \
185 bxu2000n-2-a1 | \
186 db120 | \
187 f9k1115v2 |\
188 hornet-ub | \
189 mr12 | \
190 mr16 | \
191 wpj558 | \
192 zcn-1523h-2 | \
193 zcn-1523h-5)
194 [ "$magic_long" != "68737173" -a "$magic_long" != "19852003" ] && {
195 echo "Invalid image type."
196 return 1
197 }
198 return 0
199 ;;
200 ap81 | \
201 ap83 | \
202 ap132 | \
203 cf-e316n-v2 | \
204 dgl-5500-a1 |\
205 dhp-1565-a1 |\
206 dir-505-a1 | \
207 dir-600-a1 | \
208 dir-615-c1 | \
209 dir-615-e1 | \
210 dir-615-e4 | \
211 dir-615-i1 | \
212 dir-825-c1 | \
213 dir-835-a1 | \
214 dlan-pro-500-wp | \
215 dlan-pro-1200-ac | \
216 dragino2 | \
217 epg5000 | \
218 esr1750 | \
219 esr900 | \
220 ew-dorin | \
221 ew-dorin-router | \
222 hiwifi-hc6361 | \
223 hornet-ub-x2 | \
224 mzk-w04nu | \
225 mzk-w300nh | \
226 tew-632brp | \
227 tew-712br | \
228 tew-732br | \
229 wrt400n | \
230 airgateway | \
231 airgatewaypro | \
232 airrouter | \
233 bullet-m | \
234 loco-m-xw | \
235 nanostation-m | \
236 rocket-m | \
237 rocket-m-xw | \
238 rocket-m-ti | \
239 nanostation-m-xw | \
240 rw2458n | \
241 wpj531 | \
242 wndap360 | \
243 wpj344 | \
244 wzr-hp-g300nh2 | \
245 wzr-hp-g300nh | \
246 wzr-hp-g450h | \
247 wzr-hp-ag300h | \
248 wzr-450hp2 | \
249 whr-g301n | \
250 whr-hp-g300n | \
251 whr-hp-gn | \
252 wlae-ag300n | \
253 nbg460n_550n_550nh | \
254 unifi | \
255 unifi-outdoor | \
256 carambola2 | \
257 weio )
258 [ "$magic" != "2705" ] && {
259 echo "Invalid image type."
260 return 1
261 }
262 return 0
263 ;;
264
265 cpe510)
266 tplink_pharos_check_image "$1" && return 0
267 return 1
268 ;;
269
270 bsb | \
271 dir-825-b1 | \
272 tew-673gru)
273 dir825b_check_image "$1" && return 0
274 ;;
275
276 mynet-rext|\
277 wrt160nl)
278 cybertan_check_image "$1" && return 0
279 return 1
280 ;;
281
282 qihoo-c301 | \
283 mynet-n600 | \
284 mynet-n750)
285 [ "$magic_long" != "5ea3a417" ] && {
286 echo "Invalid image, bad magic: $magic_long"
287 return 1
288 }
289
290 local typemagic=$(seama_get_type_magic "$1")
291 [ "$typemagic" != "6669726d" ] && {
292 echo "Invalid image, bad type: $typemagic"
293 return 1
294 }
295
296 return 0;
297 ;;
298 mr1750 | \
299 mr600 | \
300 mr600v2 | \
301 mr900 | \
302 mr900v2 | \
303 om2p | \
304 om2pv2 | \
305 om2p-hs | \
306 om2p-hsv2 | \
307 om2p-lc | \
308 om5p | \
309 om5p-an)
310 platform_check_image_openmesh "$magic_long" "$1" && return 0
311 return 1
312 ;;
313
314 antminer-s1 | \
315 antminer-s3 | \
316 archer-c5 | \
317 archer-c7 | \
318 el-m150 | \
319 el-mini | \
320 gl-inet | \
321 mc-mac1200r | \
322 minibox-v1 |\
323 onion-omega | \
324 oolite | \
325 smart-300 | \
326 tl-mr10u | \
327 tl-mr11u | \
328 tl-mr12u | \
329 tl-mr13u | \
330 tl-mr3020 | \
331 tl-mr3040 | \
332 tl-mr3040-v2 | \
333 tl-mr3220 | \
334 tl-mr3220-v2 | \
335 tl-mr3420 | \
336 tl-mr3420-v2 | \
337 tl-wa701nd-v2 | \
338 tl-wa7210n-v2 | \
339 tl-wa7510n | \
340 tl-wa750re | \
341 tl-wa850re | \
342 tl-wa860re | \
343 tl-wa801nd-v2 | \
344 tl-wa901nd | \
345 tl-wa901nd-v2 | \
346 tl-wa901nd-v3 | \
347 tl-wdr3320-v2 | \
348 tl-wdr3500 | \
349 tl-wdr4300 | \
350 tl-wdr4900-v2 | \
351 tl-wdr6500-v2 | \
352 tl-wr703n | \
353 tl-wr710n | \
354 tl-wr720n-v3 | \
355 tl-wr741nd | \
356 tl-wr741nd-v4 | \
357 tl-wr841n-v1 | \
358 tl-wa830re-v2 | \
359 tl-wr841n-v7 | \
360 tl-wr841n-v8 | \
361 tl-wr841n-v9 | \
362 tl-wr842n-v2 | \
363 tl-wr941nd | \
364 tl-wr941nd-v5 | \
365 tl-wr941nd-v6 | \
366 tl-wr1041n-v2 | \
367 tl-wr1043nd | \
368 tl-wr1043nd-v2 | \
369 tl-wr2543n)
370 local magic_ver="0100"
371
372 case "$board" in
373 tl-wdr6500-v2)
374 magic_ver="0200"
375 ;;
376 esac
377
378 [ "$magic" != "$magic_ver" ] && {
379 echo "Invalid image type."
380 return 1
381 }
382
383 local hwid
384 local imageid
385
386 hwid=$(tplink_get_hwid)
387 imageid=$(tplink_get_image_hwid "$1")
388
389 [ "$hwid" != "$imageid" ] && {
390 echo "Invalid image, hardware ID mismatch, hw:$hwid image:$imageid."
391 return 1
392 }
393
394 local boot_size
395
396 boot_size=$(tplink_get_image_boot_size "$1")
397 [ "$boot_size" != "00000000" ] && {
398 echo "Invalid image, it contains a bootloader."
399 return 1
400 }
401
402 return 0
403 ;;
404
405 tube2h)
406 alfa_check_image "$1" && return 0
407 return 1
408 ;;
409
410 nbg6616 | \
411 unifi-outdoor-plus | \
412 uap-pro)
413 [ "$magic_long" != "19852003" ] && {
414 echo "Invalid image type."
415 return 1
416 }
417 return 0
418 ;;
419 wndr3700 | \
420 wnr2000-v3 | \
421 wnr612-v2 | \
422 wnr1000-v2)
423 local hw_magic
424
425 hw_magic="$(ar71xx_get_mtd_part_magic firmware)"
426 [ "$magic_long" != "$hw_magic" ] && {
427 echo "Invalid image, hardware ID mismatch, hw:$hw_magic image:$magic_long."
428 return 1
429 }
430 return 0
431 ;;
432 nbg6716 | \
433 r6100 | \
434 wndr3700v4 | \
435 wndr4300 )
436 nand_do_platform_check $board $1
437 return $?;
438 ;;
439 routerstation | \
440 routerstation-pro | \
441 ls-sr71 | \
442 pb42 | \
443 pb44 | \
444 all0305 | \
445 eap300v2 | \
446 eap7660d | \
447 ja76pf | \
448 ja76pf2 | \
449 jwap003 | \
450 wp543 | \
451 wpe72)
452 [ "$magic" != "4349" ] && {
453 echo "Invalid image. Use *-sysupgrade.bin files on this board"
454 return 1
455 }
456
457 local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
458 local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
459
460 if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
461 return 0
462 else
463 echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
464 return 1
465 fi
466 return 0
467 ;;
468 wnr2000-v4)
469 [ "$magic_long" != "32303034" ] && {
470 echo "Invalid image type."
471 return 1
472 }
473 return 0
474 ;;
475
476 esac
477
478 echo "Sysupgrade is not yet supported on $board."
479 return 1
480 }
481
482 platform_pre_upgrade() {
483 local board=$(ar71xx_board_name)
484
485 case "$board" in
486 nbg6716 | \
487 r6100 | \
488 wndr3700v4 | \
489 wndr4300 )
490 nand_do_upgrade "$1"
491 ;;
492 esac
493 }
494
495 platform_do_upgrade() {
496 local board=$(ar71xx_board_name)
497
498 case "$board" in
499 routerstation | \
500 routerstation-pro | \
501 ls-sr71 | \
502 all0305 | \
503 eap7660d | \
504 pb42 | \
505 pb44 | \
506 ja76pf | \
507 ja76pf2 | \
508 jwap003)
509 platform_do_upgrade_combined "$ARGV"
510 ;;
511 wp543|\
512 wpe72)
513 platform_do_upgrade_compex "$ARGV"
514 ;;
515 all0258n )
516 platform_do_upgrade_allnet "0x9f050000" "$ARGV"
517 ;;
518 all0315n )
519 platform_do_upgrade_allnet "0x9f080000" "$ARGV"
520 ;;
521 eap300v2 |\
522 cap4200ag)
523 platform_do_upgrade_allnet "0xbf0a0000" "$ARGV"
524 ;;
525 dir-825-b1 |\
526 tew-673gru)
527 platform_do_upgrade_dir825b "$ARGV"
528 ;;
529 mr1750 | \
530 mr600 | \
531 mr600v2 | \
532 mr900 | \
533 mr900v2 | \
534 om2p | \
535 om2pv2 | \
536 om2p-hs | \
537 om2p-hsv2 | \
538 om2p-lc | \
539 om5p | \
540 om5p-an)
541 platform_do_upgrade_openmesh "$ARGV"
542 ;;
543 unifi-outdoor-plus | \
544 uap-pro)
545 MTD_CONFIG_ARGS="-s 0x180000"
546 default_do_upgrade "$ARGV"
547 ;;
548 *)
549 default_do_upgrade "$ARGV"
550 ;;
551 esac
552 }
553
554 disable_watchdog() {
555 killall watchdog
556 ( ps | grep -v 'grep' | grep '/dev/watchdog' ) && {
557 echo 'Could not disable watchdog'
558 return 1
559 }
560 }
561
562 append sysupgrade_pre_upgrade disable_watchdog