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