ar71xx: add support for TL-WA701ND v2
[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         ap96 | \
183         db120 | \
184         f9k1115v2 |\
185         hornet-ub | \
186         bxu2000n-2-a1 | \
187         zcn-1523h-2 | \
188         zcn-1523h-5)
189                 [ "$magic_long" != "68737173" -a "$magic_long" != "19852003" ] && {
190                         echo "Invalid image type."
191                         return 1
192                 }
193                 return 0
194                 ;;
195         ap81 | \
196         ap83 | \
197         ap132 | \
198         dgl-5500-a1 |\
199         dhp-1565-a1 |\
200         dir-505-a1 | \
201         dir-600-a1 | \
202         dir-615-c1 | \
203         dir-615-e1 | \
204         dir-615-e4 | \
205         dir-825-c1 | \
206         dir-835-a1 | \
207         dragino2 | \
208         esr1750 | \
209         esr900 | \
210         ew-dorin | \
211         ew-dorin-router | \
212         hiwifi-hc6361 | \
213         hornet-ub-x2 | \
214         mzk-w04nu | \
215         mzk-w300nh | \
216         tew-632brp | \
217         tew-712br | \
218         tew-732br | \
219         wrt400n | \
220         airgateway | \
221         airrouter | \
222         bullet-m | \
223         loco-m-xw | \
224         nanostation-m | \
225         rocket-m | \
226         nanostation-m-xw | \
227         rw2458n | \
228         wndap360 | \
229         wzr-hp-g300nh2 | \
230         wzr-hp-g300nh | \
231         wzr-hp-g450h | \
232         wzr-hp-ag300h | \
233         wzr-450hp2 | \
234         whr-g301n | \
235         whr-hp-g300n | \
236         whr-hp-gn | \
237         wlae-ag300n | \
238         nbg460n_550n_550nh | \
239         unifi | \
240         unifi-outdoor | \
241         carambola2 )
242                 [ "$magic" != "2705" ] && {
243                         echo "Invalid image type."
244                         return 1
245                 }
246                 return 0
247                 ;;
248
249         cpe510)
250                 tplink_pharos_check_image "$1" && return 0
251                 return 1
252                 ;;
253
254         dir-825-b1 | \
255         tew-673gru)
256                 dir825b_check_image "$1" && return 0
257                 ;;
258
259         mynet-rext|\
260         wrt160nl)
261                 cybertan_check_image "$1" && return 0
262                 return 1
263                 ;;
264
265         qihoo-c301 | \
266         mynet-n600 | \
267         mynet-n750)
268                 [ "$magic_long" != "5ea3a417" ] && {
269                         echo "Invalid image, bad magic: $magic_long"
270                         return 1
271                 }
272
273                 local typemagic=$(seama_get_type_magic "$1")
274                 [ "$typemagic" != "6669726d" ] && {
275                         echo "Invalid image, bad type: $typemagic"
276                         return 1
277                 }
278
279                 return 0;
280                 ;;
281         mr600 | \
282         mr600v2 | \
283         mr900 | \
284         mr900v2 | \
285         om2p | \
286         om2pv2 | \
287         om2p-hs | \
288         om2p-hsv2 | \
289         om2p-lc | \
290         om5p)
291                 platform_check_image_openmesh "$magic_long" "$1" && return 0
292                 return 1
293                 ;;
294
295         archer-c5 | \
296         archer-c7 | \
297         el-m150 | \
298         el-mini | \
299         gl-inet | \
300         oolite | \
301         smart-300 | \
302         tl-mr10u | \
303         tl-mr11u | \
304         tl-mr13u | \
305         tl-mr3020 | \
306         tl-mr3040 | \
307         tl-mr3040-v2 | \
308         tl-mr3220 | \
309         tl-mr3220-v2 | \
310         tl-mr3420 | \
311         tl-mr3420-v2 | \
312         tl-wa701nd-v2 | \
313         tl-wa7510n | \
314         tl-wa750re | \
315         tl-wa850re | \
316         tl-wa860re | \
317         tl-wa801nd-v2 | \
318         tl-wa901nd | \
319         tl-wa901nd-v2 | \
320         tl-wa901nd-v3 | \
321         tl-wdr3500 | \
322         tl-wdr4300 | \
323         tl-wdr4900-v2 | \
324         tl-wr703n | \
325         tl-wr710n | \
326         tl-wr720n-v3 | \
327         tl-wr741nd | \
328         tl-wr741nd-v4 | \
329         tl-wr841n-v1 | \
330         tl-wa830re-v2 | \
331         tl-wr841n-v7 | \
332         tl-wr841n-v8 | \
333         tl-wr841n-v9 | \
334         tl-wr842n-v2 | \
335         tl-wr941nd | \
336         tl-wr1041n-v2 | \
337         tl-wr1043nd | \
338         tl-wr1043nd-v2 | \
339         tl-wr2543n)
340                 [ "$magic" != "0100" ] && {
341                         echo "Invalid image type."
342                         return 1
343                 }
344
345                 local hwid
346                 local imageid
347
348                 hwid=$(tplink_get_hwid)
349                 imageid=$(tplink_get_image_hwid "$1")
350
351                 [ "$hwid" != "$imageid" ] && {
352                         echo "Invalid image, hardware ID mismatch, hw:$hwid image:$imageid."
353                         return 1
354                 }
355
356                 local boot_size
357
358                 boot_size=$(tplink_get_image_boot_size "$1")
359                 [ "$boot_size" != "00000000" ] && {
360                         echo "Invalid image, it contains a bootloader."
361                         return 1
362                 }
363
364                 return 0
365                 ;;
366
367         tube2h)
368                 alfa_check_image "$1" && return 0
369                 return 1
370                 ;;
371
372         uap-pro)
373                 [ "$magic_long" != "19852003" ] && {
374                         echo "Invalid image type."
375                         return 1
376                 }
377                 return 0
378                 ;;
379         wndr3700 | \
380         wnr2000-v3 | \
381         wnr612-v2)
382                 local hw_magic
383
384                 hw_magic="$(ar71xx_get_mtd_part_magic firmware)"
385                 [ "$magic_long" != "$hw_magic" ] && {
386                         echo "Invalid image, hardware ID mismatch, hw:$hw_magic image:$magic_long."
387                         return 1
388                 }
389                 return 0
390                 ;;
391         nbg6716 | \
392         r6100 | \
393         wndr3700v4 | \
394         wndr4300 )
395                 nand_do_platform_check $board $1
396                 return $?;
397                 ;;
398         routerstation | \
399         routerstation-pro | \
400         ls-sr71 | \
401         pb42 | \
402         pb44 | \
403         all0305 | \
404         eap300v2 | \
405         eap7660d | \
406         ja76pf | \
407         ja76pf2 | \
408         jwap003 | \
409         wp543 | \
410         wpe72)
411                 [ "$magic" != "4349" ] && {
412                         echo "Invalid image. Use *-sysupgrade.bin files on this board"
413                         return 1
414                 }
415
416                 local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
417                 local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
418
419                 if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
420                         return 0
421                 else
422                         echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
423                         return 1
424                 fi
425                 return 0
426                 ;;
427     wnr2000-v4)
428                 [ "$magic_long" != "32303034" ] && {
429                         echo "Invalid image type."
430                         return 1
431                 }
432                 return 0
433                 ;;
434
435         esac
436
437         echo "Sysupgrade is not yet supported on $board."
438         return 1
439 }
440
441 platform_do_upgrade() {
442         local board=$(ar71xx_board_name)
443
444         case "$board" in
445         routerstation | \
446         routerstation-pro | \
447         ls-sr71 | \
448         all0305 | \
449         eap7660d | \
450         pb42 | \
451         pb44 | \
452         ja76pf | \
453         ja76pf2 | \
454         jwap003)
455                 platform_do_upgrade_combined "$ARGV"
456                 ;;
457         wp543|\
458         wpe72)
459                 platform_do_upgrade_compex "$ARGV"
460                 ;;
461         all0258n )
462                 platform_do_upgrade_allnet "0x9f050000" "$ARGV"
463                 ;;
464         all0315n )
465                 platform_do_upgrade_allnet "0x9f080000" "$ARGV"
466                 ;;
467         eap300v2 |\
468         cap4200ag)
469                 platform_do_upgrade_allnet "0xbf0a0000" "$ARGV"
470                 ;;
471         dir-825-b1 |\
472         tew-673gru)
473                 platform_do_upgrade_dir825b "$ARGV"
474                 ;;
475         mr600 | \
476         mr600v2 | \
477         mr900 | \
478         mr900v2 | \
479         om2p | \
480         om2pv2 | \
481         om2p-hs | \
482         om2p-hsv2 | \
483         om2p-lc | \
484         om5p)
485                 platform_do_upgrade_openmesh "$ARGV"
486                 ;;
487         uap-pro)
488                 MTD_CONFIG_ARGS="-s 0x180000"
489                 default_do_upgrade "$ARGV"
490                 ;;
491         *)
492                 default_do_upgrade "$ARGV"
493                 ;;
494         esac
495 }
496
497 disable_watchdog() {
498         killall watchdog
499         ( ps | grep -v 'grep' | grep '/dev/watchdog' ) && {
500                 echo 'Could not disable watchdog'
501                 return 1
502         }
503 }
504
505 append sysupgrade_pre_upgrade disable_watchdog