ar71xx: add support for jjPlus JWAP230
[librecmc/librecmc.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         zcn-1523h-2 | \
202         zcn-1523h-5)
203                 [ "$magic_long" != "68737173" -a "$magic_long" != "19852003" ] && {
204                         echo "Invalid image type."
205                         return 1
206                 }
207                 return 0
208                 ;;
209         ap81 | \
210         ap83 | \
211         ap132 | \
212         c-55 | \
213         cf-e316n-v2 | \
214         dgl-5500-a1 |\
215         dhp-1565-a1 |\
216         dir-505-a1 | \
217         dir-600-a1 | \
218         dir-615-c1 | \
219         dir-615-e1 | \
220         dir-615-e4 | \
221         dir-615-i1 | \
222         dir-825-c1 | \
223         dir-835-a1 | \
224         dlan-hotspot | \
225         dlan-pro-500-wp | \
226         dlan-pro-1200-ac | \
227         dr531 | \
228         dragino2 | \
229         epg5000 | \
230         esr1750 | \
231         esr900 | \
232         ew-dorin | \
233         ew-dorin-router | \
234         gl-ar150 | \
235         gl-ar300 | \
236         gl-domino | \
237         hiwifi-hc6361 | \
238         hornet-ub-x2 | \
239         jwap230 | \
240         mzk-w04nu | \
241         mzk-w300nh | \
242         tew-632brp | \
243         tew-712br | \
244         tew-732br | \
245         tew-823dru | \
246         wrt400n | \
247         airgateway | \
248         airgatewaypro | \
249         airrouter | \
250         bullet-m | \
251         loco-m-xw | \
252         nanostation-m | \
253         rocket-m | \
254         rocket-m-xw | \
255         rocket-m-ti | \
256         nanostation-m-xw | \
257         rw2458n | \
258         wpj531 | \
259         wndap360 | \
260         wpj342 | \
261         wpj344 | \
262         wzr-hp-g300nh2 | \
263         wzr-hp-g300nh | \
264         wzr-hp-g450h | \
265         wzr-hp-ag300h | \
266         wzr-450hp2 | \
267         whr-g301n | \
268         whr-hp-g300n | \
269         whr-hp-gn | \
270         wlae-ag300n | \
271         nbg460n_550n_550nh | \
272         unifi | \
273         unifiac-lite | \
274         unifiac-pro | \
275         unifi-outdoor | \
276         carambola2 | \
277         weio | \
278         wrtnode2q)
279                 [ "$magic" != "2705" ] && {
280                         echo "Invalid image type."
281                         return 1
282                 }
283                 return 0
284                 ;;
285
286         cpe210|\
287         cpe510)
288                 tplink_pharos_check_image "$1" && return 0
289                 return 1
290                 ;;
291
292         bsb | \
293         dir-825-b1 | \
294         tew-673gru)
295                 dir825b_check_image "$1" && return 0
296                 ;;
297
298         mynet-rext|\
299         wrt160nl)
300                 cybertan_check_image "$1" && return 0
301                 return 1
302                 ;;
303
304         qihoo-c301 | \
305         mynet-n600 | \
306         mynet-n750)
307                 [ "$magic_long" != "5ea3a417" ] && {
308                         echo "Invalid image, bad magic: $magic_long"
309                         return 1
310                 }
311
312                 local typemagic=$(seama_get_type_magic "$1")
313                 [ "$typemagic" != "6669726d" ] && {
314                         echo "Invalid image, bad type: $typemagic"
315                         return 1
316                 }
317
318                 return 0;
319                 ;;
320         mr1750 | \
321         mr1750v2 | \
322         mr600 | \
323         mr600v2 | \
324         mr900 | \
325         mr900v2 | \
326         om2p | \
327         om2pv2 | \
328         om2p-hs | \
329         om2p-hsv2 | \
330         om2p-hsv3 | \
331         om2p-lc | \
332         om5p | \
333         om5p-an | \
334         om5p-ac | \
335         om5p-acv2)
336                 platform_check_image_openmesh "$magic_long" "$1" && return 0
337                 return 1
338                 ;;
339
340         antminer-s1 | \
341         antminer-s3 | \
342         antrouter-r1 | \
343         archer-c5 | \
344         archer-c7 | \
345         el-m150 | \
346         el-mini | \
347         gl-inet | \
348         mc-mac1200r | \
349         minibox-v1 |\
350         omy-g1 |\
351         omy-x1 |\
352         onion-omega | \
353         oolite | \
354         smart-300 | \
355         som9331 | \
356         tellstick-znet-lite | \
357         tl-mr10u | \
358         tl-mr11u | \
359         tl-mr12u | \
360         tl-mr13u | \
361         tl-mr3020 | \
362         tl-mr3040 | \
363         tl-mr3040-v2 | \
364         tl-mr3220 | \
365         tl-mr3220-v2 | \
366         tl-mr3420 | \
367         tl-mr3420-v2 | \
368         tl-wa701nd-v2 | \
369         tl-wa7210n-v2 | \
370         tl-wa7510n | \
371         tl-wa750re | \
372         tl-wa850re | \
373         tl-wa860re | \
374         tl-wa801nd-v2 | \
375         tl-wa901nd | \
376         tl-wa901nd-v2 | \
377         tl-wa901nd-v3 | \
378         tl-wa901nd-v4 | \
379         tl-wdr3320-v2 | \
380         tl-wdr3500 | \
381         tl-wdr4300 | \
382         tl-wdr4900-v2 | \
383         tl-wdr6500-v2 | \
384         tl-wr703n | \
385         tl-wr710n | \
386         tl-wr720n-v3 | \
387         tl-wr741nd | \
388         tl-wr741nd-v4 | \
389         tl-wr810n | \
390         tl-wr841n-v1 | \
391         tl-wa830re-v2 | \
392         tl-wr841n-v7 | \
393         tl-wr841n-v8 | \
394         tl-wr841n-v9 | \
395         tl-wr841n-v11 | \
396         tl-wr842n-v2 | \
397         tl-wr842n-v3 | \
398         tl-wr941nd | \
399         tl-wr941nd-v5 | \
400         tl-wr941nd-v6 | \
401         tl-wr1041n-v2 | \
402         tl-wr1043nd | \
403         tl-wr1043nd-v2 | \
404         tl-wr2543n)
405                 local magic_ver="0100"
406
407                 case "$board" in
408                 tl-wdr6500-v2)
409                         magic_ver="0200"
410                         ;;
411                 esac
412
413                 [ "$magic" != "$magic_ver" ] && {
414                         echo "Invalid image type."
415                         return 1
416                 }
417
418                 local hwid
419                 local mid
420                 local imagehwid
421                 local imagemid
422
423                 hwid=$(tplink_get_hwid)
424                 mid=$(tplink_get_mid)
425                 imagehwid=$(tplink_get_image_hwid "$1")
426                 imagemid=$(tplink_get_image_mid "$1")
427
428                 [ "$hwid" != "$imagehwid" -o "$mid" != "$imagemid" ] && {
429                         echo "Invalid image, hardware ID mismatch, hw:$hwid $mid image:$imagehwid $imagemid."
430                         return 1
431                 }
432
433                 local boot_size
434
435                 boot_size=$(tplink_get_image_boot_size "$1")
436                 [ "$boot_size" != "00000000" ] && {
437                         echo "Invalid image, it contains a bootloader."
438                         return 1
439                 }
440
441                 return 0
442                 ;;
443
444         tube2h)
445                 alfa_check_image "$1" && return 0
446                 return 1
447                 ;;
448
449         nbg6616 | \
450         unifi-outdoor-plus | \
451         uap-pro)
452                 [ "$magic_long" != "19852003" ] && {
453                         echo "Invalid image type."
454                         return 1
455                 }
456                 return 0
457                 ;;
458         wndr3700 | \
459         wnr2000-v3 | \
460         wnr612-v2 | \
461         wnr1000-v2 | \
462         wpn824n)
463                 local hw_magic
464
465                 hw_magic="$(ar71xx_get_mtd_part_magic firmware)"
466                 [ "$magic_long" != "$hw_magic" ] && {
467                         echo "Invalid image, hardware ID mismatch, hw:$hw_magic image:$magic_long."
468                         return 1
469                 }
470                 return 0
471                 ;;
472         mr18)
473                 merakinand_do_platform_check $board $1
474                 return $?;
475                 ;;
476         nbg6716 | \
477         r6100 | \
478         wndr3700v4 | \
479         wndr4300 )
480                 nand_do_platform_check $board $1
481                 return $?;
482                 ;;
483         routerstation | \
484         routerstation-pro | \
485         ls-sr71 | \
486         pb42 | \
487         pb44 | \
488         all0305 | \
489         eap300v2 | \
490         eap7660d | \
491         ja76pf | \
492         ja76pf2 | \
493         jwap003 | \
494         wp543 | \
495         wpe72)
496                 [ "$magic" != "4349" ] && {
497                         echo "Invalid image. Use *-sysupgrade.bin files on this board"
498                         return 1
499                 }
500
501                 local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
502                 local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
503
504                 if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
505                         return 0
506                 else
507                         echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
508                         return 1
509                 fi
510                 return 0
511                 ;;
512         wnr2000-v4)
513                 [ "$magic_long" != "32303034" ] && {
514                         echo "Invalid image type."
515                         return 1
516                 }
517                 return 0
518                 ;;
519         wnr2200)
520                 [ "$magic_long" != "32323030" ] && {
521                         echo "Invalid image type."
522                         return 1
523                 }
524                 return 0
525                 ;;
526
527         esac
528
529         echo "Sysupgrade is not yet supported on $board."
530         return 1
531 }
532
533 platform_pre_upgrade() {
534         local board=$(ar71xx_board_name)
535
536         case "$board" in
537         nbg6716 | \
538         r6100 | \
539         wndr3700v4 | \
540         wndr4300 )
541                 nand_do_upgrade "$1"
542                 ;;
543         mr18)
544                 merakinand_do_upgrade "$1"
545                 ;;
546         esac
547 }
548
549 platform_do_upgrade() {
550         local board=$(ar71xx_board_name)
551
552         case "$board" in
553         routerstation | \
554         routerstation-pro | \
555         ls-sr71 | \
556         all0305 | \
557         eap7660d | \
558         pb42 | \
559         pb44 | \
560         ja76pf | \
561         ja76pf2 | \
562         jwap003)
563                 platform_do_upgrade_combined "$ARGV"
564                 ;;
565         wp543|\
566         wpe72)
567                 platform_do_upgrade_compex "$ARGV"
568                 ;;
569         all0258n )
570                 platform_do_upgrade_allnet "0x9f050000" "$ARGV"
571                 ;;
572         all0315n )
573                 platform_do_upgrade_allnet "0x9f080000" "$ARGV"
574                 ;;
575         eap300v2 |\
576         cap4200ag)
577                 platform_do_upgrade_allnet "0xbf0a0000" "$ARGV"
578                 ;;
579         dir-825-b1 |\
580         tew-673gru)
581                 platform_do_upgrade_dir825b "$ARGV"
582                 ;;
583         mr1750 | \
584         mr1750v2 | \
585         mr600 | \
586         mr600v2 | \
587         mr900 | \
588         mr900v2 | \
589         om2p | \
590         om2pv2 | \
591         om2p-hs | \
592         om2p-hsv2 | \
593         om2p-hsv3 | \
594         om2p-lc | \
595         om5p | \
596         om5p-an | \
597         om5p-ac | \
598         om5p-acv2)
599                 platform_do_upgrade_openmesh "$ARGV"
600                 ;;
601         unifi-outdoor-plus | \
602         uap-pro)
603                 MTD_CONFIG_ARGS="-s 0x180000"
604                 default_do_upgrade "$ARGV"
605                 ;;
606         *)
607                 default_do_upgrade "$ARGV"
608                 ;;
609         esac
610 }
611
612 disable_watchdog() {
613         killall watchdog
614         ( ps | grep -v 'grep' | grep '/dev/watchdog' ) && {
615                 echo 'Could not disable watchdog'
616                 return 1
617         }
618 }
619
620 append sysupgrade_pre_upgrade disable_watchdog