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