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