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