ar71xx: add support for Rockeetech AP531B0
[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 ap531b0|\
208 ap90q|\
209 archer-c59-v1|\
210 archer-c60-v1|\
211 bullet-m|\
212 c-55|\
213 carambola2|\
214 cf-e316n-v2|\
215 cf-e320n-v2|\
216 cf-e380ac-v1|\
217 cf-e380ac-v2|\
218 cf-e520n|\
219 cf-e530n|\
220 cpe830|\
221 cpe870|\
222 dgl-5500-a1|\
223 dhp-1565-a1|\
224 dir-505-a1|\
225 dir-600-a1|\
226 dir-615-c1|\
227 dir-615-e1|\
228 dir-615-e4|\
229 dir-615-i1|\
230 dir-825-c1|\
231 dir-835-a1|\
232 dlan-hotspot|\
233 dlan-pro-1200-ac|\
234 dlan-pro-500-wp|\
235 dr531|\
236 dragino2|\
237 ebr-2310-c1|\
238 epg5000|\
239 esr1750|\
240 esr900|\
241 ew-dorin-router|\
242 ew-dorin|\
243 gl-ar150|\
244 gl-ar300m|\
245 gl-ar300|\
246 gl-domino|\
247 gl-mifi|\
248 hiwifi-hc6361|\
249 hornet-ub-x2|\
250 jwap230|\
251 lima|\
252 loco-m-xw|\
253 mzk-w04nu|\
254 mzk-w300nh|\
255 nanostation-m-xw|\
256 nanostation-m|\
257 nbg460n_550n_550nh|\
258 pqi-air-pen|\
259 rocket-m-ti|\
260 rocket-m-xw|\
261 rocket-m|\
262 rw2458n|\
263 sc1750|\
264 sc300m|\
265 sc450|\
266 sr3200|\
267 tew-632brp|\
268 tew-712br|\
269 tew-732br|\
270 tew-823dru|\
271 unifi-outdoor|\
272 unifiac-lite|\
273 unifiac-pro|\
274 unifi|\
275 weio|\
276 whr-g301n|\
277 whr-hp-g300n|\
278 whr-hp-gn|\
279 wlae-ag300n|\
280 wndap360|\
281 wpj342|\
282 wpj344|\
283 wpj531|\
284 wrt400n|\
285 wrtnode2q|\
286 wzr-450hp2|\
287 wzr-hp-ag300h|\
288 wzr-hp-g300nh2|\
289 wzr-hp-g300nh|\
290 wzr-hp-g450h|\
291 xd3200)
292 [ "$magic" != "2705" ] && {
293 echo "Invalid image type."
294 return 1
295 }
296
297 return 0
298 ;;
299 alfa-ap96|\
300 alfa-nx|\
301 ap121-mini|\
302 ap121|\
303 ap135-020|\
304 ap136-010|\
305 ap136-020|\
306 ap147-010|\
307 ap152|\
308 ap96|\
309 arduino-yun|\
310 bhr-4grv2|\
311 bxu2000n-2-a1|\
312 db120|\
313 dr344|\
314 dw33d|\
315 f9k1115v2|\
316 hornet-ub|\
317 mr12|\
318 mr16|\
319 wpj558|\
320 zbt-we1526|\
321 zcn-1523h-2|\
322 zcn-1523h-5)
323 [ "$magic_long" != "68737173" -a "$magic_long" != "19852003" ] && {
324 echo "Invalid image type."
325 return 1
326 }
327
328 return 0
329 ;;
330 all0258n|\
331 all0315n|\
332 cap324|\
333 cap4200ag|\
334 cr3000|\
335 cr5000)
336 platform_check_image_allnet "$1" && return 0
337 return 1
338 ;;
339 all0305|\
340 eap300v2|\
341 eap7660d|\
342 ja76pf2|\
343 ja76pf|\
344 jwap003|\
345 ls-sr71|\
346 pb42|\
347 pb44|\
348 routerstation-pro|\
349 routerstation|\
350 wp543|\
351 wpe72)
352 [ "$magic" != "4349" ] && {
353 echo "Invalid image. Use *-sysupgrade.bin files on this board"
354 return 1
355 }
356
357 local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
358 local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
359
360 if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
361 return 0
362 else
363 echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
364 return 1
365 fi
366
367 return 0
368 ;;
369 antminer-s1|\
370 antminer-s3|\
371 antrouter-r1|\
372 archer-c5|\
373 archer-c7|\
374 el-m150|\
375 el-mini|\
376 gl-inet|\
377 mc-mac1200r|\
378 minibox-v1|\
379 omy-g1|\
380 omy-x1|\
381 onion-omega|\
382 oolite|\
383 re450|\
384 smart-300|\
385 som9331|\
386 tellstick-znet-lite|\
387 tl-mr10u|\
388 tl-mr11u|\
389 tl-mr12u|\
390 tl-mr13u|\
391 tl-mr3020|\
392 tl-mr3040-v2|\
393 tl-mr3040|\
394 tl-mr3220-v2|\
395 tl-mr3220|\
396 tl-mr3420-v2|\
397 tl-mr3420|\
398 tl-wa701nd-v2|\
399 tl-wa7210n-v2|\
400 tl-wa750re|\
401 tl-wa7510n|\
402 tl-wa801nd-v2|\
403 tl-wa801nd-v3|\
404 tl-wa830re-v2|\
405 tl-wa850re|\
406 tl-wa850re-v2|\
407 tl-wa860re|\
408 tl-wa901nd-v2|\
409 tl-wa901nd-v3|\
410 tl-wa901nd-v4|\
411 tl-wa901nd|\
412 tl-wdr3320-v2|\
413 tl-wdr3500|\
414 tl-wdr4300|\
415 tl-wdr4900-v2|\
416 tl-wdr6500-v2|\
417 tl-wpa8630|\
418 tl-wr1041n-v2|\
419 tl-wr1043nd-v2|\
420 tl-wr1043nd-v4|\
421 tl-wr1043nd|\
422 tl-wr2543n|\
423 tl-wr703n|\
424 tl-wr710n|\
425 tl-wr720n-v3|\
426 tl-wr741nd-v4|\
427 tl-wr741nd|\
428 tl-wr802n-v1|\
429 tl-wr802n-v2|\
430 tl-wr810n|\
431 tl-wr841n-v11|\
432 tl-wr841n-v1|\
433 tl-wr841n-v7|\
434 tl-wr841n-v8|\
435 tl-wr841n-v9|\
436 tl-wr842n-v2|\
437 tl-wr842n-v3|\
438 tl-wr941nd-v5|\
439 tl-wr941nd-v6|\
440 tl-wr940n-v4|\
441 tl-wr941nd)
442 local magic_ver="0100"
443
444 case "$board" in
445 tl-wdr6500-v2)
446 magic_ver="0200"
447 ;;
448 esac
449
450 [ "$magic" != "$magic_ver" ] && {
451 echo "Invalid image type."
452 return 1
453 }
454
455 local hwid
456 local mid
457 local imagehwid
458 local imagemid
459
460 hwid=$(tplink_get_hwid)
461 mid=$(tplink_get_mid)
462 imagehwid=$(tplink_get_image_hwid "$1")
463 imagemid=$(tplink_get_image_mid "$1")
464
465 [ "$hwid" != "$imagehwid" -o "$mid" != "$imagemid" ] && {
466 echo "Invalid image, hardware ID mismatch, hw:$hwid $mid image:$imagehwid $imagemid."
467 return 1
468 }
469
470 local boot_size
471
472 boot_size=$(tplink_get_image_boot_size "$1")
473 [ "$boot_size" != "00000000" ] && {
474 echo "Invalid image, it contains a bootloader."
475 return 1
476 }
477
478 return 0
479 ;;
480 bsb|\
481 dir-825-b1|\
482 tew-673gru)
483 dir825b_check_image "$1" && return 0
484 ;;
485 rb-411|\
486 rb-411u|\
487 rb-433|\
488 rb-433u|\
489 rb-435g|\
490 rb-450|\
491 rb-450g|\
492 rb-493|\
493 rb-493g|\
494 rb-750|\
495 rb-750gl|\
496 rb-751|\
497 rb-751g|\
498 rb-911g-2hpnd|\
499 rb-911g-5hpnd|\
500 rb-911g-5hpacd|\
501 rb-912uag-2hpnd|\
502 rb-912uag-5hpnd|\
503 rb-951g-2hnd|\
504 rb-951ui-2hnd|\
505 rb-2011l|\
506 rb-2011uas|\
507 rb-2011uias|\
508 rb-2011uas-2hnd|\
509 rb-2011uias-2hnd|\
510 rb-sxt2n|\
511 rb-sxt5n)
512 nand_do_platform_check routerboard $1
513 return $?
514 ;;
515 c-60|\
516 nbg6716|\
517 r6100|\
518 wndr3700v4|\
519 wndr4300)
520 nand_do_platform_check $board $1
521 return $?
522 ;;
523 cpe210|\
524 cpe510|\
525 eap120|\
526 wbs210|\
527 wbs510)
528 tplink_pharos_check_image "$1" && return 0
529 return 1
530 ;;
531 a40|\
532 a60|\
533 mr1750v2|\
534 mr1750|\
535 mr600v2|\
536 mr600|\
537 mr900v2|\
538 mr900|\
539 om2p-hsv2|\
540 om2p-hsv3|\
541 om2p-hsv4|\
542 om2p-hs|\
543 om2p-lc|\
544 om2pv2|\
545 om2pv4|\
546 om2p|\
547 om5p-acv2|\
548 om5p-ac|\
549 om5p-an|\
550 om5p)
551 platform_check_image_openmesh "$magic_long" "$1" && return 0
552 return 1
553 ;;
554 mr18|\
555 z1)
556 merakinand_do_platform_check $board $1
557 return $?
558 ;;
559 dir-869-a1|\
560 mynet-n600|\
561 mynet-n750|\
562 qihoo-c301)
563 [ "$magic_long" != "5ea3a417" ] && {
564 echo "Invalid image, bad magic: $magic_long"
565 return 1
566 }
567
568 local typemagic=$(seama_get_type_magic "$1")
569 [ "$typemagic" != "6669726d" ] && {
570 echo "Invalid image, bad type: $typemagic"
571 return 1
572 }
573
574 return 0
575 ;;
576 e2100l|\
577 mynet-rext|\
578 wrt160nl)
579 cybertan_check_image "$1" && return 0
580 return 1
581 ;;
582 nbg6616|\
583 uap-pro|\
584 unifi-outdoor-plus)
585 [ "$magic_long" != "19852003" ] && {
586 echo "Invalid image type."
587 return 1
588 }
589
590 return 0
591 ;;
592 tube2h)
593 alfa_check_image "$1" && return 0
594 return 1
595 ;;
596 wndr3700|\
597 wnr1000-v2|\
598 wnr2000-v3|\
599 wnr612-v2|\
600 wpn824n)
601 local hw_magic
602
603 hw_magic="$(ar71xx_get_mtd_part_magic firmware)"
604 [ "$magic_long" != "$hw_magic" ] && {
605 echo "Invalid image, hardware ID mismatch, hw:$hw_magic image:$magic_long."
606 return 1
607 }
608
609 return 0
610 ;;
611 wnr2000-v4)
612 [ "$magic_long" != "32303034" ] && {
613 echo "Invalid image type."
614 return 1
615 }
616
617 return 0
618 ;;
619 wnr2200)
620 [ "$magic_long" != "32323030" ] && {
621 echo "Invalid image type."
622 return 1
623 }
624
625 return 0
626 ;;
627 dap-2695-a1)
628 local magic=$(wrgg_get_image_magic "$1")
629 [ "$magic" != "21030820" ] && {
630 echo "Invalid image, bad type: $magic"
631 return 1
632 }
633
634 return 0;
635 ;;
636 # these boards use metadata images
637 rb-750-r2|\
638 rb-750up-r2|\
639 rb-941-2nd|\
640 rb-951ui-2nd|\
641 rb-mapl-2nd)
642 return 0
643 ;;
644 esac
645
646 echo "Sysupgrade is not yet supported on $board."
647 return 1
648 }
649
650 platform_pre_upgrade() {
651 local board=$(ar71xx_board_name)
652
653 case "$board" in
654 c-60|\
655 nbg6716|\
656 r6100|\
657 rb-411|\
658 rb-411u|\
659 rb-433|\
660 rb-433u|\
661 rb-435g|\
662 rb-450|\
663 rb-450g|\
664 rb-493|\
665 rb-493g|\
666 rb-750|\
667 rb-750gl|\
668 rb-751|\
669 rb-751g|\
670 rb-911g-2hpnd|\
671 rb-911g-5hpnd|\
672 rb-911g-5hpacd|\
673 rb-912uag-2hpnd|\
674 rb-912uag-5hpnd|\
675 rb-951g-2hnd|\
676 rb-951ui-2hnd|\
677 rb-2011l|\
678 rb-2011uas|\
679 rb-2011uias|\
680 rb-2011uas-2hnd|\
681 rb-2011uias-2hnd|\
682 rb-sxt2n|\
683 rb-sxt5n|\
684 wndr3700v4|\
685 wndr4300)
686 nand_do_upgrade "$1"
687 ;;
688 rb-750-r2|\
689 rb-750up-r2|\
690 rb-941-2nd|\
691 rb-951ui-2nd|\
692 rb-mapl-2nd)
693 # erase firmware if booted from initramfs
694 [ -z "$(rootfs_type)" ] && mtd erase firmware
695 ;;
696 mr18|\
697 z1)
698 merakinand_do_upgrade "$1"
699 ;;
700 esac
701 }
702
703 platform_nand_pre_upgrade() {
704 local board=$(ar71xx_board_name)
705
706 case "$board" in
707 rb*)
708 CI_KERNPART=none
709 local fw_mtd=$(find_mtd_part kernel)
710 fw_mtd="${fw_mtd/block/}"
711 [ -n "$fw_mtd" ] || return
712 mtd erase kernel
713 tar xf "$1" sysupgrade-routerboard/kernel -O | nandwrite -o "$fw_mtd" -
714 ;;
715 esac
716 }
717
718 platform_do_upgrade() {
719 local board=$(ar71xx_board_name)
720
721 case "$board" in
722 all0258n)
723 platform_do_upgrade_allnet "0x9f050000" "$ARGV"
724 ;;
725 all0305|\
726 eap7660d|\
727 ja76pf2|\
728 ja76pf|\
729 jwap003|\
730 ls-sr71|\
731 pb42|\
732 pb44|\
733 routerstation-pro|\
734 routerstation)
735 platform_do_upgrade_combined "$ARGV"
736 ;;
737 all0315n)
738 platform_do_upgrade_allnet "0x9f080000" "$ARGV"
739 ;;
740 cap4200ag|\
741 eap300v2)
742 platform_do_upgrade_allnet "0xbf0a0000" "$ARGV"
743 ;;
744 dir-825-b1|\
745 tew-673gru)
746 platform_do_upgrade_dir825b "$ARGV"
747 ;;
748 a40|\
749 a60|\
750 mr1750v2|\
751 mr1750|\
752 mr600v2|\
753 mr600|\
754 mr900v2|\
755 mr900|\
756 om2p-hsv2|\
757 om2p-hsv3|\
758 om2p-hsv4|\
759 om2p-hs|\
760 om2p-lc|\
761 om2pv2|\
762 om2pv4|\
763 om2p|\
764 om5p-acv2|\
765 om5p-ac|\
766 om5p-an|\
767 om5p)
768 platform_do_upgrade_openmesh "$ARGV"
769 ;;
770 uap-pro|\
771 unifi-outdoor-plus)
772 MTD_CONFIG_ARGS="-s 0x180000"
773 default_do_upgrade "$ARGV"
774 ;;
775 wp543|\
776 wpe72)
777 platform_do_upgrade_compex "$ARGV"
778 ;;
779 *)
780 default_do_upgrade "$ARGV"
781 ;;
782 esac
783 }
784
785 disable_watchdog() {
786 killall watchdog
787 ( ps | grep -v 'grep' | grep '/dev/watchdog' ) && {
788 echo 'Could not disable watchdog'
789 return 1
790 }
791 }
792
793 append sysupgrade_pre_upgrade disable_watchdog