ar71xx: fix platform_find_rootfspart()
[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 sr3200|\
258 tew-632brp|\
259 tew-712br|\
260 tew-732br|\
261 tew-823dru|\
262 unifi-outdoor|\
263 unifiac-lite|\
264 unifiac-pro|\
265 unifi|\
266 weio|\
267 whr-g301n|\
268 whr-hp-g300n|\
269 whr-hp-gn|\
270 wlae-ag300n|\
271 wndap360|\
272 wpj342|\
273 wpj344|\
274 wpj531|\
275 wrt400n|\
276 wrtnode2q|\
277 wzr-450hp2|\
278 wzr-hp-ag300h|\
279 wzr-hp-g300nh2|\
280 wzr-hp-g300nh|\
281 wzr-hp-g450h|\
282 xd3200)
283 [ "$magic" != "2705" ] && {
284 echo "Invalid image type."
285 return 1
286 }
287
288 return 0
289 ;;
290 alfa-ap96|\
291 alfa-nx|\
292 ap121-mini|\
293 ap121|\
294 ap135-020|\
295 ap136-010|\
296 ap136-020|\
297 ap147-010|\
298 ap152|\
299 ap96|\
300 arduino-yun|\
301 bhr-4grv2|\
302 bxu2000n-2-a1|\
303 db120|\
304 dr344|\
305 dw33d|\
306 f9k1115v2|\
307 hornet-ub|\
308 mr12|\
309 mr16|\
310 wpj558|\
311 zbt-we1526|\
312 zcn-1523h-2|\
313 zcn-1523h-5)
314 [ "$magic_long" != "68737173" -a "$magic_long" != "19852003" ] && {
315 echo "Invalid image type."
316 return 1
317 }
318
319 return 0
320 ;;
321 all0258n|\
322 all0315n|\
323 cap324|\
324 cap4200ag|\
325 cr3000|\
326 cr5000)
327 platform_check_image_allnet "$1" && return 0
328 return 1
329 ;;
330 all0305|\
331 eap300v2|\
332 eap7660d|\
333 ja76pf2|\
334 ja76pf|\
335 jwap003|\
336 ls-sr71|\
337 pb42|\
338 pb44|\
339 rb-941-2nd|\
340 routerstation-pro|\
341 routerstation|\
342 wp543|\
343 wpe72)
344 [ "$magic" != "4349" ] && {
345 echo "Invalid image. Use *-sysupgrade.bin files on this board"
346 return 1
347 }
348
349 local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
350 local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
351
352 if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
353 return 0
354 else
355 echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
356 return 1
357 fi
358
359 return 0
360 ;;
361 antminer-s1|\
362 antminer-s3|\
363 antrouter-r1|\
364 archer-c5|\
365 archer-c7|\
366 el-m150|\
367 el-mini|\
368 gl-inet|\
369 mc-mac1200r|\
370 minibox-v1|\
371 omy-g1|\
372 omy-x1|\
373 onion-omega|\
374 oolite|\
375 re450|\
376 smart-300|\
377 som9331|\
378 tellstick-znet-lite|\
379 tl-mr10u|\
380 tl-mr11u|\
381 tl-mr12u|\
382 tl-mr13u|\
383 tl-mr3020|\
384 tl-mr3040-v2|\
385 tl-mr3040|\
386 tl-mr3220-v2|\
387 tl-mr3220|\
388 tl-mr3420-v2|\
389 tl-mr3420|\
390 tl-wa701nd-v2|\
391 tl-wa7210n-v2|\
392 tl-wa750re|\
393 tl-wa7510n|\
394 tl-wa801nd-v2|\
395 tl-wa801nd-v3|\
396 tl-wa830re-v2|\
397 tl-wa850re|\
398 tl-wa860re|\
399 tl-wa901nd-v2|\
400 tl-wa901nd-v3|\
401 tl-wa901nd-v4|\
402 tl-wa901nd|\
403 tl-wdr3320-v2|\
404 tl-wdr3500|\
405 tl-wdr4300|\
406 tl-wdr4900-v2|\
407 tl-wdr6500-v2|\
408 tl-wpa8630|\
409 tl-wr1041n-v2|\
410 tl-wr1043nd-v2|\
411 tl-wr1043nd-v4|\
412 tl-wr1043nd|\
413 tl-wr2543n|\
414 tl-wr703n|\
415 tl-wr710n|\
416 tl-wr720n-v3|\
417 tl-wr741nd-v4|\
418 tl-wr741nd|\
419 tl-wr802n-v1|\
420 tl-wr810n|\
421 tl-wr841n-v11|\
422 tl-wr841n-v1|\
423 tl-wr841n-v7|\
424 tl-wr841n-v8|\
425 tl-wr841n-v9|\
426 tl-wr842n-v2|\
427 tl-wr842n-v3|\
428 tl-wr941nd-v5|\
429 tl-wr941nd-v6|\
430 tl-wr940n-v4|\
431 tl-wr941nd)
432 local magic_ver="0100"
433
434 case "$board" in
435 tl-wdr6500-v2)
436 magic_ver="0200"
437 ;;
438 esac
439
440 [ "$magic" != "$magic_ver" ] && {
441 echo "Invalid image type."
442 return 1
443 }
444
445 local hwid
446 local mid
447 local imagehwid
448 local imagemid
449
450 hwid=$(tplink_get_hwid)
451 mid=$(tplink_get_mid)
452 imagehwid=$(tplink_get_image_hwid "$1")
453 imagemid=$(tplink_get_image_mid "$1")
454
455 [ "$hwid" != "$imagehwid" -o "$mid" != "$imagemid" ] && {
456 echo "Invalid image, hardware ID mismatch, hw:$hwid $mid image:$imagehwid $imagemid."
457 return 1
458 }
459
460 local boot_size
461
462 boot_size=$(tplink_get_image_boot_size "$1")
463 [ "$boot_size" != "00000000" ] && {
464 echo "Invalid image, it contains a bootloader."
465 return 1
466 }
467
468 return 0
469 ;;
470 bsb|\
471 dir-825-b1|\
472 tew-673gru)
473 dir825b_check_image "$1" && return 0
474 ;;
475 rb*)
476 nand_do_platform_check routerboard $1
477 return $?
478 ;;
479 c-60|\
480 nbg6716|\
481 r6100|\
482 wndr3700v4|\
483 wndr4300)
484 nand_do_platform_check $board $1
485 return $?
486 ;;
487 cpe210|\
488 cpe510|\
489 eap120|\
490 wbs210|\
491 wbs510)
492 tplink_pharos_check_image "$1" && return 0
493 return 1
494 ;;
495 a40|\
496 a60|\
497 mr1750v2|\
498 mr1750|\
499 mr600v2|\
500 mr600|\
501 mr900v2|\
502 mr900|\
503 om2p-hsv2|\
504 om2p-hsv3|\
505 om2p-hsv4|\
506 om2p-hs|\
507 om2p-lc|\
508 om2pv2|\
509 om2pv4|\
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 a40|\
677 a60|\
678 mr1750v2|\
679 mr1750|\
680 mr600v2|\
681 mr600|\
682 mr900v2|\
683 mr900|\
684 om2p-hsv2|\
685 om2p-hsv3|\
686 om2p-hsv4|\
687 om2p-hs|\
688 om2p-lc|\
689 om2pv2|\
690 om2pv4|\
691 om2p|\
692 om5p-acv2|\
693 om5p-ac|\
694 om5p-an|\
695 om5p)
696 platform_do_upgrade_openmesh "$ARGV"
697 ;;
698 uap-pro|\
699 unifi-outdoor-plus)
700 MTD_CONFIG_ARGS="-s 0x180000"
701 default_do_upgrade "$ARGV"
702 ;;
703 wp543|\
704 wpe72)
705 platform_do_upgrade_compex "$ARGV"
706 ;;
707 *)
708 default_do_upgrade "$ARGV"
709 ;;
710 esac
711 }
712
713 disable_watchdog() {
714 killall watchdog
715 ( ps | grep -v 'grep' | grep '/dev/watchdog' ) && {
716 echo 'Could not disable watchdog'
717 return 1
718 }
719 }
720
721 append sysupgrade_pre_upgrade disable_watchdog