7423c38c5f544feb4a6deae4f0187f3d528be854
[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 [ -x /usr/sbin/nandwrite ] && RAMFS_COPY_BIN=/usr/sbin/nandwrite
11
12 CI_BLKSZ=65536
13 CI_LDADR=0x80060000
14
15 PLATFORM_DO_UPGRADE_COMBINED_SEPARATE_MTD=0
16
17 platform_find_partitions() {
18 local first dev size erasesize name
19 while read dev size erasesize name; do
20 name=${name#'"'}; name=${name%'"'}
21 case "$name" in
22 vmlinux.bin.l7|vmlinux|kernel|linux|linux.bin|rootfs|filesystem)
23 if [ -z "$first" ]; then
24 first="$name"
25 else
26 echo "$erasesize:$first:$name"
27 break
28 fi
29 ;;
30 esac
31 done < /proc/mtd
32 }
33
34 platform_find_kernelpart() {
35 local part
36 for part in "${1%:*}" "${1#*:}"; do
37 case "$part" in
38 vmlinux.bin.l7|vmlinux|kernel|linux|linux.bin)
39 echo "$part"
40 break
41 ;;
42 esac
43 done
44 }
45
46 platform_find_rootfspart() {
47 local part
48 for part in "${1%:*}" "${1#*:}"; do
49 [ "$part" != "$2" ] && echo "$part" && break
50 done
51 }
52
53 platform_do_upgrade_combined() {
54 local partitions=$(platform_find_partitions)
55 local kernelpart=$(platform_find_kernelpart "${partitions#*:}")
56 local erase_size=$((0x${partitions%%:*})); partitions="${partitions#*:}"
57 local kern_length=0x$(dd if="$1" bs=2 skip=1 count=4 2>/dev/null)
58 local kern_blocks=$(($kern_length / $CI_BLKSZ))
59 local root_blocks=$((0x$(dd if="$1" bs=2 skip=5 count=4 2>/dev/null) / $CI_BLKSZ))
60
61 if [ -n "$partitions" ] && [ -n "$kernelpart" ] && \
62 [ ${kern_blocks:-0} -gt 0 ] && \
63 [ ${root_blocks:-0} -gt 0 ] && \
64 [ ${erase_size:-0} -gt 0 ];
65 then
66 local rootfspart=$(platform_find_rootfspart "$partitions" "$kernelpart")
67 local append=""
68 [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
69
70 if [ "$PLATFORM_DO_UPGRADE_COMBINED_SEPARATE_MTD" -ne 1 ]; then
71 ( dd if="$1" bs=$CI_BLKSZ skip=1 count=$kern_blocks 2>/dev/null; \
72 dd if="$1" bs=$CI_BLKSZ skip=$((1+$kern_blocks)) count=$root_blocks 2>/dev/null ) | \
73 mtd -r $append -F$kernelpart:$kern_length:$CI_LDADR,rootfs write - $partitions
74 elif [ -n "$rootfspart" ]; then
75 dd if="$1" bs=$CI_BLKSZ skip=1 count=$kern_blocks 2>/dev/null | \
76 mtd write - $kernelpart
77 dd if="$1" bs=$CI_BLKSZ skip=$((1+$kern_blocks)) count=$root_blocks 2>/dev/null | \
78 mtd -r $append write - $rootfspart
79 fi
80 fi
81 PLATFORM_DO_UPGRADE_COMBINED_SEPARATE_MTD=0
82 }
83
84 tplink_get_image_hwid() {
85 get_image "$@" | dd bs=4 count=1 skip=16 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
86 }
87
88 tplink_get_image_mid() {
89 get_image "$@" | dd bs=4 count=1 skip=17 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
90 }
91
92 tplink_get_image_boot_size() {
93 get_image "$@" | dd bs=4 count=1 skip=37 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
94 }
95
96 tplink_pharos_check_image() {
97 local magic_long="$(get_magic_long "$1")"
98 [ "$magic_long" != "7f454c46" ] && {
99 echo "Invalid image magic '$magic_long'"
100 return 1
101 }
102
103 local model_string="$(tplink_pharos_get_model_string)"
104 local line
105
106 # Here $1 is given to dd directly instead of get_image as otherwise the skip
107 # will take almost a second (as dd can't seek then)
108 #
109 # This will fail if the image isn't local, but that's fine: as the
110 # read loop won't be executed at all, it will return true, so the image
111 # is accepted (loading the first 1.5M of a remote image for this check seems
112 # a bit extreme)
113 dd if="$1" bs=1 skip=1511432 count=1024 2>/dev/null | while read line; do
114 [ "$line" == "$model_string" ] && break
115 done || {
116 echo "Unsupported image (model not in support-list)"
117 return 1
118 }
119
120 return 0
121 }
122
123 seama_get_type_magic() {
124 get_image "$@" | dd bs=1 count=4 skip=53 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
125 }
126
127 wrgg_get_image_magic() {
128 get_image "$@" | dd bs=4 count=1 skip=8 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
129 }
130
131 cybertan_get_image_magic() {
132 get_image "$@" | dd bs=8 count=1 skip=0 2>/dev/null | hexdump -v -n 8 -e '1/1 "%02x"'
133 }
134
135 cybertan_check_image() {
136 local magic="$(cybertan_get_image_magic "$1")"
137 local fw_magic="$(cybertan_get_hw_magic)"
138
139 [ "$fw_magic" != "$magic" ] && {
140 echo "Invalid image, ID mismatch, got:$magic, but need:$fw_magic"
141 return 1
142 }
143
144 return 0
145 }
146
147 platform_do_upgrade_compex() {
148 local fw_file=$1
149 local fw_part=$PART_NAME
150 local fw_mtd=$(find_mtd_part $fw_part)
151 local fw_length=0x$(dd if="$fw_file" bs=2 skip=1 count=4 2>/dev/null)
152 local fw_blocks=$(($fw_length / 65536))
153
154 if [ -n "$fw_mtd" ] && [ ${fw_blocks:-0} -gt 0 ]; then
155 local append=""
156 [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
157
158 sync
159 dd if="$fw_file" bs=64k skip=1 count=$fw_blocks 2>/dev/null | \
160 mtd $append write - "$fw_part"
161 fi
162 }
163
164 alfa_check_image() {
165 local magic_long="$(get_magic_long "$1")"
166 local fw_part_size=$(mtd_get_part_size firmware)
167
168 case "$magic_long" in
169 "27051956")
170 [ "$fw_part_size" != "16318464" ] && {
171 echo "Invalid image magic \"$magic_long\" for $fw_part_size bytes"
172 return 1
173 }
174 ;;
175 "68737173")
176 [ "$fw_part_size" != "7929856" ] && {
177 echo "Invalid image magic \"$magic_long\" for $fw_part_size bytes"
178 return 1
179 }
180 ;;
181 esac
182
183 return 0
184 }
185
186 platform_nand_board_name() {
187 local board=$(ar71xx_board_name)
188
189 case "$board" in
190 rb*) echo "routerboard";;
191 *) echo "$board";;
192 esac
193 }
194
195 platform_check_image() {
196 local board=$(ar71xx_board_name)
197 local magic="$(get_magic_word "$1")"
198 local magic_long="$(get_magic_long "$1")"
199
200 [ "$#" -gt 1 ] && return 1
201
202 case "$board" in
203 airgatewaypro|\
204 airgateway|\
205 airrouter|\
206 ap132|\
207 ap90q|\
208 archer-c59-v1|\
209 archer-c60-v1|\
210 bullet-m|\
211 c-55|\
212 carambola2|\
213 cf-e316n-v2|\
214 cf-e320n-v2|\
215 cf-e380ac-v1|\
216 cf-e380ac-v2|\
217 cf-e520n|\
218 cf-e530n|\
219 cpe830|\
220 cpe870|\
221 dgl-5500-a1|\
222 dhp-1565-a1|\
223 dir-505-a1|\
224 dir-600-a1|\
225 dir-615-c1|\
226 dir-615-e1|\
227 dir-615-e4|\
228 dir-615-i1|\
229 dir-825-c1|\
230 dir-835-a1|\
231 dlan-hotspot|\
232 dlan-pro-1200-ac|\
233 dlan-pro-500-wp|\
234 dr531|\
235 dragino2|\
236 ebr-2310-c1|\
237 epg5000|\
238 esr1750|\
239 esr900|\
240 ew-dorin-router|\
241 ew-dorin|\
242 gl-ar150|\
243 gl-ar300m|\
244 gl-ar300|\
245 gl-domino|\
246 gl-mifi|\
247 hiwifi-hc6361|\
248 hornet-ub-x2|\
249 jwap230|\
250 lima|\
251 loco-m-xw|\
252 mzk-w04nu|\
253 mzk-w300nh|\
254 nanostation-m-xw|\
255 nanostation-m|\
256 nbg460n_550n_550nh|\
257 pqi-air-pen|\
258 rocket-m-ti|\
259 rocket-m-xw|\
260 rocket-m|\
261 rw2458n|\
262 sc1750|\
263 sc300m|\
264 sc450|\
265 sr3200|\
266 tew-632brp|\
267 tew-712br|\
268 tew-732br|\
269 tew-823dru|\
270 unifi-outdoor|\
271 unifiac-lite|\
272 unifiac-pro|\
273 unifi|\
274 weio|\
275 whr-g301n|\
276 whr-hp-g300n|\
277 whr-hp-gn|\
278 wlae-ag300n|\
279 wndap360|\
280 wpj342|\
281 wpj344|\
282 wpj531|\
283 wrt400n|\
284 wrtnode2q|\
285 wzr-450hp2|\
286 wzr-hp-ag300h|\
287 wzr-hp-g300nh2|\
288 wzr-hp-g300nh|\
289 wzr-hp-g450h|\
290 xd3200)
291 [ "$magic" != "2705" ] && {
292 echo "Invalid image type."
293 return 1
294 }
295
296 return 0
297 ;;
298 alfa-ap96|\
299 alfa-nx|\
300 ap121-mini|\
301 ap121|\
302 ap135-020|\
303 ap136-010|\
304 ap136-020|\
305 ap147-010|\
306 ap152|\
307 ap96|\
308 arduino-yun|\
309 bhr-4grv2|\
310 bxu2000n-2-a1|\
311 db120|\
312 dr344|\
313 dw33d|\
314 f9k1115v2|\
315 hornet-ub|\
316 mr12|\
317 mr16|\
318 wpj558|\
319 zbt-we1526|\
320 zcn-1523h-2|\
321 zcn-1523h-5)
322 [ "$magic_long" != "68737173" -a "$magic_long" != "19852003" ] && {
323 echo "Invalid image type."
324 return 1
325 }
326
327 return 0
328 ;;
329 all0258n|\
330 all0315n|\
331 cap324|\
332 cap4200ag|\
333 cr3000|\
334 cr5000)
335 platform_check_image_allnet "$1" && return 0
336 return 1
337 ;;
338 all0305|\
339 eap300v2|\
340 eap7660d|\
341 ja76pf2|\
342 ja76pf|\
343 jwap003|\
344 ls-sr71|\
345 pb42|\
346 pb44|\
347 rb-750-r2|\
348 rb-750up-r2|\
349 rb-941-2nd|\
350 rb-951ui-2nd|\
351 rb-mapl-2nd|\
352 routerstation-pro|\
353 routerstation|\
354 wp543|\
355 wpe72)
356 [ "$magic" != "4349" ] && {
357 echo "Invalid image. Use *-sysupgrade.bin files on this board"
358 return 1
359 }
360
361 local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
362 local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
363
364 if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
365 return 0
366 else
367 echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
368 return 1
369 fi
370
371 return 0
372 ;;
373 antminer-s1|\
374 antminer-s3|\
375 antrouter-r1|\
376 archer-c5|\
377 archer-c7|\
378 el-m150|\
379 el-mini|\
380 gl-inet|\
381 mc-mac1200r|\
382 minibox-v1|\
383 omy-g1|\
384 omy-x1|\
385 onion-omega|\
386 oolite|\
387 re450|\
388 smart-300|\
389 som9331|\
390 tellstick-znet-lite|\
391 tl-mr10u|\
392 tl-mr11u|\
393 tl-mr12u|\
394 tl-mr13u|\
395 tl-mr3020|\
396 tl-mr3040-v2|\
397 tl-mr3040|\
398 tl-mr3220-v2|\
399 tl-mr3220|\
400 tl-mr3420-v2|\
401 tl-mr3420|\
402 tl-wa701nd-v2|\
403 tl-wa7210n-v2|\
404 tl-wa750re|\
405 tl-wa7510n|\
406 tl-wa801nd-v2|\
407 tl-wa801nd-v3|\
408 tl-wa830re-v2|\
409 tl-wa850re|\
410 tl-wa850re-v2|\
411 tl-wa860re|\
412 tl-wa901nd-v2|\
413 tl-wa901nd-v3|\
414 tl-wa901nd-v4|\
415 tl-wa901nd|\
416 tl-wdr3320-v2|\
417 tl-wdr3500|\
418 tl-wdr4300|\
419 tl-wdr4900-v2|\
420 tl-wdr6500-v2|\
421 tl-wpa8630|\
422 tl-wr1041n-v2|\
423 tl-wr1043nd-v2|\
424 tl-wr1043nd-v4|\
425 tl-wr1043nd|\
426 tl-wr2543n|\
427 tl-wr703n|\
428 tl-wr710n|\
429 tl-wr720n-v3|\
430 tl-wr741nd-v4|\
431 tl-wr741nd|\
432 tl-wr802n-v1|\
433 tl-wr802n-v2|\
434 tl-wr810n|\
435 tl-wr841n-v11|\
436 tl-wr841n-v1|\
437 tl-wr841n-v7|\
438 tl-wr841n-v8|\
439 tl-wr841n-v9|\
440 tl-wr842n-v2|\
441 tl-wr842n-v3|\
442 tl-wr941nd-v5|\
443 tl-wr941nd-v6|\
444 tl-wr940n-v4|\
445 tl-wr941nd)
446 local magic_ver="0100"
447
448 case "$board" in
449 tl-wdr6500-v2)
450 magic_ver="0200"
451 ;;
452 esac
453
454 [ "$magic" != "$magic_ver" ] && {
455 echo "Invalid image type."
456 return 1
457 }
458
459 local hwid
460 local mid
461 local imagehwid
462 local imagemid
463
464 hwid=$(tplink_get_hwid)
465 mid=$(tplink_get_mid)
466 imagehwid=$(tplink_get_image_hwid "$1")
467 imagemid=$(tplink_get_image_mid "$1")
468
469 [ "$hwid" != "$imagehwid" -o "$mid" != "$imagemid" ] && {
470 echo "Invalid image, hardware ID mismatch, hw:$hwid $mid image:$imagehwid $imagemid."
471 return 1
472 }
473
474 local boot_size
475
476 boot_size=$(tplink_get_image_boot_size "$1")
477 [ "$boot_size" != "00000000" ] && {
478 echo "Invalid image, it contains a bootloader."
479 return 1
480 }
481
482 return 0
483 ;;
484 bsb|\
485 dir-825-b1|\
486 tew-673gru)
487 dir825b_check_image "$1" && return 0
488 ;;
489 rb-411|\
490 rb-411u|\
491 rb-433|\
492 rb-433u|\
493 rb-435g|\
494 rb-450|\
495 rb-450g|\
496 rb-493|\
497 rb-493g|\
498 rb-750|\
499 rb-750gl|\
500 rb-751|\
501 rb-751g|\
502 rb-911g-2hpnd|\
503 rb-911g-5hpnd|\
504 rb-911g-5hpacd|\
505 rb-912uag-2hpnd|\
506 rb-912uag-5hpnd|\
507 rb-951g-2hnd|\
508 rb-951ui-2hnd|\
509 rb-2011l|\
510 rb-2011uas|\
511 rb-2011uias|\
512 rb-2011uas-2hnd|\
513 rb-2011uias-2hnd|\
514 rb-sxt2n|\
515 rb-sxt5n)
516 nand_do_platform_check routerboard $1
517 return $?
518 ;;
519 c-60|\
520 nbg6716|\
521 r6100|\
522 wndr3700v4|\
523 wndr4300)
524 nand_do_platform_check $board $1
525 return $?
526 ;;
527 cpe210|\
528 cpe510|\
529 eap120|\
530 wbs210|\
531 wbs510)
532 tplink_pharos_check_image "$1" && return 0
533 return 1
534 ;;
535 a40|\
536 a60|\
537 mr1750v2|\
538 mr1750|\
539 mr600v2|\
540 mr600|\
541 mr900v2|\
542 mr900|\
543 om2p-hsv2|\
544 om2p-hsv3|\
545 om2p-hsv4|\
546 om2p-hs|\
547 om2p-lc|\
548 om2pv2|\
549 om2pv4|\
550 om2p|\
551 om5p-acv2|\
552 om5p-ac|\
553 om5p-an|\
554 om5p)
555 platform_check_image_openmesh "$magic_long" "$1" && return 0
556 return 1
557 ;;
558 mr18|\
559 z1)
560 merakinand_do_platform_check $board $1
561 return $?
562 ;;
563 dir-869-a1|\
564 mynet-n600|\
565 mynet-n750|\
566 qihoo-c301)
567 [ "$magic_long" != "5ea3a417" ] && {
568 echo "Invalid image, bad magic: $magic_long"
569 return 1
570 }
571
572 local typemagic=$(seama_get_type_magic "$1")
573 [ "$typemagic" != "6669726d" ] && {
574 echo "Invalid image, bad type: $typemagic"
575 return 1
576 }
577
578 return 0
579 ;;
580 e2100l|\
581 mynet-rext|\
582 wrt160nl)
583 cybertan_check_image "$1" && return 0
584 return 1
585 ;;
586 nbg6616|\
587 uap-pro|\
588 unifi-outdoor-plus)
589 [ "$magic_long" != "19852003" ] && {
590 echo "Invalid image type."
591 return 1
592 }
593
594 return 0
595 ;;
596 tube2h)
597 alfa_check_image "$1" && return 0
598 return 1
599 ;;
600 wndr3700|\
601 wnr1000-v2|\
602 wnr2000-v3|\
603 wnr612-v2|\
604 wpn824n)
605 local hw_magic
606
607 hw_magic="$(ar71xx_get_mtd_part_magic firmware)"
608 [ "$magic_long" != "$hw_magic" ] && {
609 echo "Invalid image, hardware ID mismatch, hw:$hw_magic image:$magic_long."
610 return 1
611 }
612
613 return 0
614 ;;
615 wnr2000-v4)
616 [ "$magic_long" != "32303034" ] && {
617 echo "Invalid image type."
618 return 1
619 }
620
621 return 0
622 ;;
623 wnr2200)
624 [ "$magic_long" != "32323030" ] && {
625 echo "Invalid image type."
626 return 1
627 }
628
629 return 0
630 ;;
631 dap-2695-a1)
632 local magic=$(wrgg_get_image_magic "$1")
633 [ "$magic" != "21030820" ] && {
634 echo "Invalid image, bad type: $magic"
635 return 1
636 }
637
638 return 0;
639 ;;
640 esac
641
642 echo "Sysupgrade is not yet supported on $board."
643 return 1
644 }
645
646 platform_pre_upgrade() {
647 local board=$(ar71xx_board_name)
648
649 case "$board" in
650 c-60|\
651 nbg6716|\
652 r6100|\
653 rb-411|\
654 rb-411u|\
655 rb-433|\
656 rb-433u|\
657 rb-435g|\
658 rb-450|\
659 rb-450g|\
660 rb-493|\
661 rb-493g|\
662 rb-750|\
663 rb-750gl|\
664 rb-751|\
665 rb-751g|\
666 rb-911g-2hpnd|\
667 rb-911g-5hpnd|\
668 rb-911g-5hpacd|\
669 rb-912uag-2hpnd|\
670 rb-912uag-5hpnd|\
671 rb-951g-2hnd|\
672 rb-951ui-2hnd|\
673 rb-2011l|\
674 rb-2011uas|\
675 rb-2011uias|\
676 rb-2011uas-2hnd|\
677 rb-2011uias-2hnd|\
678 rb-sxt2n|\
679 rb-sxt5n|\
680 wndr3700v4|\
681 wndr4300)
682 nand_do_upgrade "$1"
683 ;;
684 mr18|\
685 z1)
686 merakinand_do_upgrade "$1"
687 ;;
688 esac
689 }
690
691 platform_nand_pre_upgrade() {
692 local board=$(ar71xx_board_name)
693
694 case "$board" in
695 rb*)
696 CI_KERNPART=none
697 local fw_mtd=$(find_mtd_part kernel)
698 fw_mtd="${fw_mtd/block/}"
699 [ -n "$fw_mtd" ] || return
700 mtd erase kernel
701 tar xf "$1" sysupgrade-routerboard/kernel -O | nandwrite -o "$fw_mtd" -
702 ;;
703 esac
704 }
705
706 platform_do_upgrade() {
707 local board=$(ar71xx_board_name)
708
709 case "$board" in
710 rb-750-r2|\
711 rb-750up-r2|\
712 rb-941-2nd|\
713 rb-951ui-2nd|\
714 rb-mapl-2nd)
715 PLATFORM_DO_UPGRADE_COMBINED_SEPARATE_MTD=1
716 platform_do_upgrade_combined "$ARGV"
717 ;;
718 all0258n)
719 platform_do_upgrade_allnet "0x9f050000" "$ARGV"
720 ;;
721 all0305|\
722 eap7660d|\
723 ja76pf2|\
724 ja76pf|\
725 jwap003|\
726 ls-sr71|\
727 pb42|\
728 pb44|\
729 routerstation-pro|\
730 routerstation)
731 platform_do_upgrade_combined "$ARGV"
732 ;;
733 all0315n)
734 platform_do_upgrade_allnet "0x9f080000" "$ARGV"
735 ;;
736 cap4200ag|\
737 eap300v2)
738 platform_do_upgrade_allnet "0xbf0a0000" "$ARGV"
739 ;;
740 dir-825-b1|\
741 tew-673gru)
742 platform_do_upgrade_dir825b "$ARGV"
743 ;;
744 a40|\
745 a60|\
746 mr1750v2|\
747 mr1750|\
748 mr600v2|\
749 mr600|\
750 mr900v2|\
751 mr900|\
752 om2p-hsv2|\
753 om2p-hsv3|\
754 om2p-hsv4|\
755 om2p-hs|\
756 om2p-lc|\
757 om2pv2|\
758 om2pv4|\
759 om2p|\
760 om5p-acv2|\
761 om5p-ac|\
762 om5p-an|\
763 om5p)
764 platform_do_upgrade_openmesh "$ARGV"
765 ;;
766 uap-pro|\
767 unifi-outdoor-plus)
768 MTD_CONFIG_ARGS="-s 0x180000"
769 default_do_upgrade "$ARGV"
770 ;;
771 wp543|\
772 wpe72)
773 platform_do_upgrade_compex "$ARGV"
774 ;;
775 *)
776 default_do_upgrade "$ARGV"
777 ;;
778 esac
779 }
780
781 disable_watchdog() {
782 killall watchdog
783 ( ps | grep -v 'grep' | grep '/dev/watchdog' ) && {
784 echo 'Could not disable watchdog'
785 return 1
786 }
787 }
788
789 append sysupgrade_pre_upgrade disable_watchdog