ar71xx: add support for Zbtlink ZBT-WE1526
[oweals/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
11 CI_BLKSZ=65536
12 CI_LDADR=0x80060000
13
14 platform_find_partitions() {
15         local first dev size erasesize name
16         while read dev size erasesize name; do
17                 name=${name#'"'}; name=${name%'"'}
18                 case "$name" in
19                         vmlinux.bin.l7|vmlinux|kernel|linux|linux.bin|rootfs|filesystem)
20                                 if [ -z "$first" ]; then
21                                         first="$name"
22                                 else
23                                         echo "$erasesize:$first:$name"
24                                         break
25                                 fi
26                         ;;
27                 esac
28         done < /proc/mtd
29 }
30
31 platform_find_kernelpart() {
32         local part
33         for part in "${1%:*}" "${1#*:}"; do
34                 case "$part" in
35                         vmlinux.bin.l7|vmlinux|kernel|linux|linux.bin)
36                                 echo "$part"
37                                 break
38                         ;;
39                 esac
40         done
41 }
42
43 platform_do_upgrade_combined() {
44         local partitions=$(platform_find_partitions)
45         local kernelpart=$(platform_find_kernelpart "${partitions#*:}")
46         local erase_size=$((0x${partitions%%:*})); partitions="${partitions#*:}"
47         local kern_length=0x$(dd if="$1" bs=2 skip=1 count=4 2>/dev/null)
48         local kern_blocks=$(($kern_length / $CI_BLKSZ))
49         local root_blocks=$((0x$(dd if="$1" bs=2 skip=5 count=4 2>/dev/null) / $CI_BLKSZ))
50
51         if [ -n "$partitions" ] && [ -n "$kernelpart" ] && \
52            [ ${kern_blocks:-0} -gt 0 ] && \
53            [ ${root_blocks:-0} -gt 0 ] && \
54            [ ${erase_size:-0} -gt 0 ];
55         then
56                 local append=""
57                 [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
58
59                 ( dd if="$1" bs=$CI_BLKSZ skip=1 count=$kern_blocks 2>/dev/null; \
60                   dd if="$1" bs=$CI_BLKSZ skip=$((1+$kern_blocks)) count=$root_blocks 2>/dev/null ) | \
61                         mtd -r $append -F$kernelpart:$kern_length:$CI_LDADR,rootfs write - $partitions
62         fi
63 }
64
65 tplink_get_image_hwid() {
66         get_image "$@" | dd bs=4 count=1 skip=16 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
67 }
68
69 tplink_get_image_mid() {
70         get_image "$@" | dd bs=4 count=1 skip=17 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
71 }
72
73 tplink_get_image_boot_size() {
74         get_image "$@" | dd bs=4 count=1 skip=37 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
75 }
76
77 tplink_pharos_check_image() {
78         local magic_long="$(get_magic_long "$1")"
79         [ "$magic_long" != "7f454c46" ] && {
80                 echo "Invalid image magic '$magic_long'"
81                 return 1
82         }
83
84         local model_string="$(tplink_pharos_get_model_string)"
85         local line
86
87         # Here $1 is given to dd directly instead of get_image as otherwise the skip
88         # will take almost a second (as dd can't seek then)
89         #
90         # This will fail if the image isn't local, but that's fine: as the
91         # read loop won't be executed at all, it will return true, so the image
92         # is accepted (loading the first 1.5M of a remote image for this check seems
93         # a bit extreme)
94         dd if="$1" bs=1 skip=1511432 count=1024 2>/dev/null | while read line; do
95                 [ "$line" == "$model_string" ] && break
96         done || {
97                 echo "Unsupported image (model not in support-list)"
98                 return 1
99         }
100
101         return 0
102 }
103
104 seama_get_type_magic() {
105         get_image "$@" | dd bs=1 count=4 skip=53 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
106 }
107
108 cybertan_get_image_magic() {
109         get_image "$@" | dd bs=8 count=1 skip=0  2>/dev/null | hexdump -v -n 8 -e '1/1 "%02x"'
110 }
111
112 cybertan_check_image() {
113         local magic="$(cybertan_get_image_magic "$1")"
114         local fw_magic="$(cybertan_get_hw_magic)"
115
116         [ "$fw_magic" != "$magic" ] && {
117                 echo "Invalid image, ID mismatch, got:$magic, but need:$fw_magic"
118                 return 1
119         }
120
121         return 0
122 }
123
124 platform_do_upgrade_compex() {
125         local fw_file=$1
126         local fw_part=$PART_NAME
127         local fw_mtd=$(find_mtd_part $fw_part)
128         local fw_length=0x$(dd if="$fw_file" bs=2 skip=1 count=4 2>/dev/null)
129         local fw_blocks=$(($fw_length / 65536))
130
131         if [ -n "$fw_mtd" ] &&  [ ${fw_blocks:-0} -gt 0 ]; then
132                 local append=""
133                 [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
134
135                 sync
136                 dd if="$fw_file" bs=64k skip=1 count=$fw_blocks 2>/dev/null | \
137                         mtd $append write - "$fw_part"
138         fi
139 }
140
141 alfa_check_image() {
142         local magic_long="$(get_magic_long "$1")"
143         local fw_part_size=$(mtd_get_part_size firmware)
144
145         case "$magic_long" in
146         "27051956")
147                 [ "$fw_part_size" != "16318464" ] && {
148                         echo "Invalid image magic \"$magic_long\" for $fw_part_size bytes"
149                         return 1
150                 }
151                 ;;
152
153         "68737173")
154                 [ "$fw_part_size" != "7929856" ] && {
155                         echo "Invalid image magic \"$magic_long\" for $fw_part_size bytes"
156                         return 1
157                 }
158                 ;;
159         esac
160
161         return 0
162 }
163
164 platform_check_image() {
165         local board=$(ar71xx_board_name)
166         local magic="$(get_magic_word "$1")"
167         local magic_long="$(get_magic_long "$1")"
168
169         [ "$#" -gt 1 ] && return 1
170
171         case "$board" in
172         all0315n | \
173         all0258n | \
174         cap324 | \
175         cap4200ag | \
176         cr3000 |\
177         cr5000)
178                 platform_check_image_allnet "$1" && return 0
179                 return 1
180                 ;;
181         alfa-ap96 | \
182         alfa-nx | \
183         arduino-yun | \
184         ap113 | \
185         ap121 | \
186         ap121-mini | \
187         ap136-010 | \
188         ap136-020 | \
189         ap135-020 | \
190         ap147-010 | \
191         ap152 | \
192         ap96 | \
193         bxu2000n-2-a1 | \
194         db120 | \
195         dr344 | \
196         f9k1115v2 |\
197         hornet-ub | \
198         mr12 | \
199         mr16 | \
200         wpj558 | \
201         zbt-we1526 | \
202         zcn-1523h-2 | \
203         zcn-1523h-5)
204                 [ "$magic_long" != "68737173" -a "$magic_long" != "19852003" ] && {
205                         echo "Invalid image type."
206                         return 1
207                 }
208                 return 0
209                 ;;
210         ap81 | \
211         ap83 | \
212         ap132 | \
213         c-55 | \
214         cf-e316n-v2 | \
215         dgl-5500-a1 |\
216         dhp-1565-a1 |\
217         dir-505-a1 | \
218         dir-600-a1 | \
219         dir-615-c1 | \
220         dir-615-e1 | \
221         dir-615-e4 | \
222         dir-615-i1 | \
223         dir-825-c1 | \
224         dir-835-a1 | \
225         dlan-hotspot | \
226         dlan-pro-500-wp | \
227         dlan-pro-1200-ac | \
228         dr531 | \
229         dragino2 | \
230         epg5000 | \
231         esr1750 | \
232         esr900 | \
233         ew-dorin | \
234         ew-dorin-router | \
235         gl-ar150 | \
236         gl-mifi | \
237         gl-ar300 | \
238         gl-ar300m | \
239         gl-domino | \
240         hiwifi-hc6361 | \
241         hornet-ub-x2 | \
242         jwap230 | \
243         mzk-w04nu | \
244         mzk-w300nh | \
245         tew-632brp | \
246         tew-712br | \
247         tew-732br | \
248         tew-823dru | \
249         wrt400n | \
250         airgateway | \
251         airgatewaypro | \
252         airrouter | \
253         bullet-m | \
254         loco-m-xw | \
255         nanostation-m | \
256         rocket-m | \
257         rocket-m-xw | \
258         rocket-m-ti | \
259         nanostation-m-xw | \
260         rw2458n | \
261         wpj531 | \
262         wndap360 | \
263         wpj342 | \
264         wpj344 | \
265         wzr-hp-g300nh2 | \
266         wzr-hp-g300nh | \
267         wzr-hp-g450h | \
268         wzr-hp-ag300h | \
269         wzr-450hp2 | \
270         whr-g301n | \
271         whr-hp-g300n | \
272         whr-hp-gn | \
273         wlae-ag300n | \
274         nbg460n_550n_550nh | \
275         unifi | \
276         unifiac-lite | \
277         unifiac-pro | \
278         unifi-outdoor | \
279         carambola2 | \
280         weio | \
281         wrtnode2q)
282                 [ "$magic" != "2705" ] && {
283                         echo "Invalid image type."
284                         return 1
285                 }
286                 return 0
287                 ;;
288
289         cpe210|\
290         cpe510)
291                 tplink_pharos_check_image "$1" && return 0
292                 return 1
293                 ;;
294
295         bsb | \
296         dir-825-b1 | \
297         tew-673gru)
298                 dir825b_check_image "$1" && return 0
299                 ;;
300
301         mynet-rext|\
302         wrt160nl)
303                 cybertan_check_image "$1" && return 0
304                 return 1
305                 ;;
306
307         qihoo-c301 | \
308         mynet-n600 | \
309         mynet-n750)
310                 [ "$magic_long" != "5ea3a417" ] && {
311                         echo "Invalid image, bad magic: $magic_long"
312                         return 1
313                 }
314
315                 local typemagic=$(seama_get_type_magic "$1")
316                 [ "$typemagic" != "6669726d" ] && {
317                         echo "Invalid image, bad type: $typemagic"
318                         return 1
319                 }
320
321                 return 0;
322                 ;;
323         mr1750 | \
324         mr1750v2 | \
325         mr600 | \
326         mr600v2 | \
327         mr900 | \
328         mr900v2 | \
329         om2p | \
330         om2pv2 | \
331         om2p-hs | \
332         om2p-hsv2 | \
333         om2p-hsv3 | \
334         om2p-lc | \
335         om5p | \
336         om5p-an | \
337         om5p-ac | \
338         om5p-acv2)
339                 platform_check_image_openmesh "$magic_long" "$1" && return 0
340                 return 1
341                 ;;
342
343         antminer-s1 | \
344         antminer-s3 | \
345         antrouter-r1 | \
346         archer-c5 | \
347         archer-c7 | \
348         el-m150 | \
349         el-mini | \
350         gl-inet | \
351         mc-mac1200r | \
352         minibox-v1 |\
353         omy-g1 |\
354         omy-x1 |\
355         onion-omega | \
356         oolite | \
357         smart-300 | \
358         som9331 | \
359         tellstick-znet-lite | \
360         tl-mr10u | \
361         tl-mr11u | \
362         tl-mr12u | \
363         tl-mr13u | \
364         tl-mr3020 | \
365         tl-mr3040 | \
366         tl-mr3040-v2 | \
367         tl-mr3220 | \
368         tl-mr3220-v2 | \
369         tl-mr3420 | \
370         tl-mr3420-v2 | \
371         tl-wa701nd-v2 | \
372         tl-wa7210n-v2 | \
373         tl-wa7510n | \
374         tl-wa750re | \
375         tl-wa850re | \
376         tl-wa860re | \
377         tl-wa801nd-v2 | \
378         tl-wa901nd | \
379         tl-wa901nd-v2 | \
380         tl-wa901nd-v3 | \
381         tl-wa901nd-v4 | \
382         tl-wdr3320-v2 | \
383         tl-wdr3500 | \
384         tl-wdr4300 | \
385         tl-wdr4900-v2 | \
386         tl-wdr6500-v2 | \
387         tl-wr703n | \
388         tl-wr710n | \
389         tl-wr720n-v3 | \
390         tl-wr741nd | \
391         tl-wr741nd-v4 | \
392         tl-wr810n | \
393         tl-wr841n-v1 | \
394         tl-wa830re-v2 | \
395         tl-wr841n-v7 | \
396         tl-wr841n-v8 | \
397         tl-wr841n-v9 | \
398         tl-wr841n-v11 | \
399         tl-wr842n-v2 | \
400         tl-wr842n-v3 | \
401         tl-wr941nd | \
402         tl-wr941nd-v5 | \
403         tl-wr941nd-v6 | \
404         tl-wr1041n-v2 | \
405         tl-wr1043nd | \
406         tl-wr1043nd-v2 | \
407         tl-wr2543n)
408                 local magic_ver="0100"
409
410                 case "$board" in
411                 tl-wdr6500-v2)
412                         magic_ver="0200"
413                         ;;
414                 esac
415
416                 [ "$magic" != "$magic_ver" ] && {
417                         echo "Invalid image type."
418                         return 1
419                 }
420
421                 local hwid
422                 local mid
423                 local imagehwid
424                 local imagemid
425
426                 hwid=$(tplink_get_hwid)
427                 mid=$(tplink_get_mid)
428                 imagehwid=$(tplink_get_image_hwid "$1")
429                 imagemid=$(tplink_get_image_mid "$1")
430
431                 [ "$hwid" != "$imagehwid" -o "$mid" != "$imagemid" ] && {
432                         echo "Invalid image, hardware ID mismatch, hw:$hwid $mid image:$imagehwid $imagemid."
433                         return 1
434                 }
435
436                 local boot_size
437
438                 boot_size=$(tplink_get_image_boot_size "$1")
439                 [ "$boot_size" != "00000000" ] && {
440                         echo "Invalid image, it contains a bootloader."
441                         return 1
442                 }
443
444                 return 0
445                 ;;
446
447         tube2h)
448                 alfa_check_image "$1" && return 0
449                 return 1
450                 ;;
451
452         nbg6616 | \
453         unifi-outdoor-plus | \
454         uap-pro)
455                 [ "$magic_long" != "19852003" ] && {
456                         echo "Invalid image type."
457                         return 1
458                 }
459                 return 0
460                 ;;
461         wndr3700 | \
462         wnr2000-v3 | \
463         wnr612-v2 | \
464         wnr1000-v2 | \
465         wpn824n)
466                 local hw_magic
467
468                 hw_magic="$(ar71xx_get_mtd_part_magic firmware)"
469                 [ "$magic_long" != "$hw_magic" ] && {
470                         echo "Invalid image, hardware ID mismatch, hw:$hw_magic image:$magic_long."
471                         return 1
472                 }
473                 return 0
474                 ;;
475         mr18)
476                 merakinand_do_platform_check $board $1
477                 return $?;
478                 ;;
479         nbg6716 | \
480         r6100 | \
481         wndr3700v4 | \
482         wndr4300 )
483                 nand_do_platform_check $board $1
484                 return $?;
485                 ;;
486         routerstation | \
487         routerstation-pro | \
488         ls-sr71 | \
489         pb42 | \
490         pb44 | \
491         all0305 | \
492         eap300v2 | \
493         eap7660d | \
494         ja76pf | \
495         ja76pf2 | \
496         jwap003 | \
497         wp543 | \
498         wpe72)
499                 [ "$magic" != "4349" ] && {
500                         echo "Invalid image. Use *-sysupgrade.bin files on this board"
501                         return 1
502                 }
503
504                 local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
505                 local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
506
507                 if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
508                         return 0
509                 else
510                         echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
511                         return 1
512                 fi
513                 return 0
514                 ;;
515         wnr2000-v4)
516                 [ "$magic_long" != "32303034" ] && {
517                         echo "Invalid image type."
518                         return 1
519                 }
520                 return 0
521                 ;;
522         wnr2200)
523                 [ "$magic_long" != "32323030" ] && {
524                         echo "Invalid image type."
525                         return 1
526                 }
527                 return 0
528                 ;;
529
530         esac
531
532         echo "Sysupgrade is not yet supported on $board."
533         return 1
534 }
535
536 platform_pre_upgrade() {
537         local board=$(ar71xx_board_name)
538
539         case "$board" in
540         nbg6716 | \
541         r6100 | \
542         wndr3700v4 | \
543         wndr4300 )
544                 nand_do_upgrade "$1"
545                 ;;
546         mr18)
547                 merakinand_do_upgrade "$1"
548                 ;;
549         esac
550 }
551
552 platform_do_upgrade() {
553         local board=$(ar71xx_board_name)
554
555         case "$board" in
556         routerstation | \
557         routerstation-pro | \
558         ls-sr71 | \
559         all0305 | \
560         eap7660d | \
561         pb42 | \
562         pb44 | \
563         ja76pf | \
564         ja76pf2 | \
565         jwap003)
566                 platform_do_upgrade_combined "$ARGV"
567                 ;;
568         wp543|\
569         wpe72)
570                 platform_do_upgrade_compex "$ARGV"
571                 ;;
572         all0258n )
573                 platform_do_upgrade_allnet "0x9f050000" "$ARGV"
574                 ;;
575         all0315n )
576                 platform_do_upgrade_allnet "0x9f080000" "$ARGV"
577                 ;;
578         eap300v2 |\
579         cap4200ag)
580                 platform_do_upgrade_allnet "0xbf0a0000" "$ARGV"
581                 ;;
582         dir-825-b1 |\
583         tew-673gru)
584                 platform_do_upgrade_dir825b "$ARGV"
585                 ;;
586         mr1750 | \
587         mr1750v2 | \
588         mr600 | \
589         mr600v2 | \
590         mr900 | \
591         mr900v2 | \
592         om2p | \
593         om2pv2 | \
594         om2p-hs | \
595         om2p-hsv2 | \
596         om2p-hsv3 | \
597         om2p-lc | \
598         om5p | \
599         om5p-an | \
600         om5p-ac | \
601         om5p-acv2)
602                 platform_do_upgrade_openmesh "$ARGV"
603                 ;;
604         unifi-outdoor-plus | \
605         uap-pro)
606                 MTD_CONFIG_ARGS="-s 0x180000"
607                 default_do_upgrade "$ARGV"
608                 ;;
609         *)
610                 default_do_upgrade "$ARGV"
611                 ;;
612         esac
613 }
614
615 disable_watchdog() {
616         killall watchdog
617         ( ps | grep -v 'grep' | grep '/dev/watchdog' ) && {
618                 echo 'Could not disable watchdog'
619                 return 1
620         }
621 }
622
623 append sysupgrade_pre_upgrade disable_watchdog