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