ar71xx: add user-space support for the TP-Link TL-WA901ND v3 board
[openwrt.git] / target / linux / ar71xx / base-files / lib / upgrade / platform.sh
1 #
2 # Copyright (C) 2011 OpenWrt.org
3 #
4
5 . /lib/ar71xx.sh
6
7 PART_NAME=firmware
8 RAMFS_COPY_DATA=/lib/ar71xx.sh
9
10 CI_BLKSZ=65536
11 CI_LDADR=0x80060000
12
13 platform_find_partitions() {
14         local first dev size erasesize name
15         while read dev size erasesize name; do
16                 name=${name#'"'}; name=${name%'"'}
17                 case "$name" in
18                         vmlinux.bin.l7|vmlinux|kernel|linux|linux.bin|rootfs|filesystem)
19                                 if [ -z "$first" ]; then
20                                         first="$name"
21                                 else
22                                         echo "$erasesize:$first:$name"
23                                         break
24                                 fi
25                         ;;
26                 esac
27         done < /proc/mtd
28 }
29
30 platform_find_kernelpart() {
31         local part
32         for part in "${1%:*}" "${1#*:}"; do
33                 case "$part" in
34                         vmlinux.bin.l7|vmlinux|kernel|linux|linux.bin)
35                                 echo "$part"
36                                 break
37                         ;;
38                 esac
39         done
40 }
41
42 platform_do_upgrade_combined() {
43         local partitions=$(platform_find_partitions)
44         local kernelpart=$(platform_find_kernelpart "${partitions#*:}")
45         local erase_size=$((0x${partitions%%:*})); partitions="${partitions#*:}"
46         local kern_length=0x$(dd if="$1" bs=2 skip=1 count=4 2>/dev/null)
47         local kern_blocks=$(($kern_length / $CI_BLKSZ))
48         local root_blocks=$((0x$(dd if="$1" bs=2 skip=5 count=4 2>/dev/null) / $CI_BLKSZ))
49
50         if [ -n "$partitions" ] && [ -n "$kernelpart" ] && \
51            [ ${kern_blocks:-0} -gt 0 ] && \
52            [ ${root_blocks:-0} -gt 0 ] && \
53            [ ${erase_size:-0} -gt 0 ];
54         then
55                 local append=""
56                 [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
57
58                 ( dd if="$1" bs=$CI_BLKSZ skip=1 count=$kern_blocks 2>/dev/null; \
59                   dd if="$1" bs=$CI_BLKSZ skip=$((1+$kern_blocks)) count=$root_blocks 2>/dev/null ) | \
60                         mtd -r $append -F$kernelpart:$kern_length:$CI_LDADR,rootfs write - $partitions
61         fi
62 }
63
64 tplink_get_image_hwid() {
65         get_image "$@" | dd bs=4 count=1 skip=16 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
66 }
67
68 tplink_get_image_boot_size() {
69         get_image "$@" | dd bs=4 count=1 skip=37 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
70 }
71
72 seama_get_type_magic() {
73         get_image "$@" | dd bs=1 count=4 skip=53 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
74 }
75
76 cybertan_get_image_magic() {
77         get_image "$@" | dd bs=8 count=1 skip=0  2>/dev/null | hexdump -v -n 8 -e '1/1 "%02x"'
78 }
79
80 cybertan_check_image() {
81         local magic="$(cybertan_get_image_magic "$1")"
82         local fw_magic="$(cybertan_get_hw_magic)"
83
84         [ "$fw_magic" != "$magic" ] && {
85                 echo "Invalid image, ID mismatch, got:$magic, but need:$fw_magic"
86                 return 1
87         }
88
89         return 0
90 }
91
92 platform_check_image() {
93         local board=$(ar71xx_board_name)
94         local magic="$(get_magic_word "$1")"
95         local magic_long="$(get_magic_long "$1")"
96
97         [ "$ARGC" -gt 1 ] && return 1
98
99         case "$board" in
100         all0315n | \
101         all0258n | \
102         cap4200ag)
103                 platform_check_image_allnet "$1" && return 0
104                 return 1
105                 ;;
106         alfa-ap96 | \
107         alfa-nx | \
108         ap113 | \
109         ap121 | \
110         ap121-mini | \
111         ap136-010 | \
112         ap136-020 | \
113         ap135-020 | \
114         ap96 | \
115         db120 | \
116         hornet-ub | \
117         bxu2000n-2-a1 | \
118         zcn-1523h-2 | \
119         zcn-1523h-5)
120                 [ "$magic_long" != "68737173" -a "$magic_long" != "19852003" ] && {
121                         echo "Invalid image type."
122                         return 1
123                 }
124                 return 0
125                 ;;
126         ap81 | \
127         ap83 | \
128         ap132 | \
129         dir-505-a1 | \
130         dir-600-a1 | \
131         dir-615-c1 | \
132         dir-615-e4 | \
133         dir-825-c1 | \
134         dir-835-a1 | \
135         ew-dorin | \
136         ew-dorin-router | \
137         hornet-ub-x2 | \
138         mzk-w04nu | \
139         mzk-w300nh | \
140         tew-632brp | \
141         tew-712br | \
142         tew-732br | \
143         wrt400n | \
144         airrouter | \
145         bullet-m | \
146         nanostation-m | \
147         rocket-m | \
148         rw2458n | \
149         wndap360 | \
150         wzr-hp-g300nh2 | \
151         wzr-hp-g300nh | \
152         wzr-hp-g450h | \
153         wzr-hp-ag300h | \
154         whr-g301n | \
155         whr-hp-g300n | \
156         whr-hp-gn | \
157         wlae-ag300n | \
158         nbg460n_550n_550nh | \
159         unifi | \
160         unifi-outdoor | \
161         carambola2 )
162                 [ "$magic" != "2705" ] && {
163                         echo "Invalid image type."
164                         return 1
165                 }
166                 return 0
167                 ;;
168
169         dir-825-b1 | \
170         tew-673gru)
171                 dir825b_check_image "$1" && return 0
172                 ;;
173
174         mynet-rext|\
175         wrt160nl)
176                 cybertan_check_image "$1" && return 0
177                 return 1
178                 ;;
179
180         mynet-n600)
181                 [ "$magic_long" != "5ea3a417" ] && {
182                         echo "Invalid image, bad magic: $magic_long"
183                         return 1
184                 }
185
186                 local typemagic=$(seama_get_type_magic "$1")
187                 [ "$typemagic" != "6669726d" ] && {
188                         echo "Invalid image, bad type: $typemagic"
189                         return 1
190                 }
191
192                 return 0;
193                 ;;
194         mr600 | \
195         mr600v2 | \
196         om2p | \
197         om2p-hs | \
198         om2p-lc)
199                 platform_check_image_openmesh "$magic_long" "$1" && return 0
200                 return 1
201                 ;;
202
203         archer-c7 | \
204         tl-mr10u | \
205         tl-mr11u | \
206         tl-mr13u | \
207         tl-mr3020 | \
208         tl-mr3040 | \
209         tl-mr3040-v2 | \
210         tl-mr3220 | \
211         tl-mr3220-v2 | \
212         tl-mr3420 | \
213         tl-mr3420-v2 | \
214         tl-wa7510n | \
215         tl-wa750re | \
216         tl-wa850re | \
217         tl-wa801nd-v2 | \
218         tl-wa901nd | \
219         tl-wa901nd-v2 | \
220         tl-wa901nd-v3 | \
221         tl-wdr3500 | \
222         tl-wdr4300 | \
223         tl-wr703n | \
224         tl-wr710n | \
225         tl-wr720n-v3 | \
226         tl-wr741nd | \
227         tl-wr741nd-v4 | \
228         tl-wr841n-v1 | \
229         tl-wr841n-v7 | \
230         tl-wr841n-v8 | \
231         tl-wr842n-v2 | \
232         tl-wr941nd | \
233         tl-wr1041n-v2 | \
234         tl-wr1043nd | \
235         tl-wr1043nd-v2 | \
236         tl-wr2543n)
237                 [ "$magic" != "0100" ] && {
238                         echo "Invalid image type."
239                         return 1
240                 }
241
242                 local hwid
243                 local imageid
244
245                 hwid=$(tplink_get_hwid)
246                 imageid=$(tplink_get_image_hwid "$1")
247
248                 [ "$hwid" != "$imageid" ] && {
249                         echo "Invalid image, hardware ID mismatch, hw:$hwid image:$imageid."
250                         return 1
251                 }
252
253                 local boot_size
254
255                 boot_size=$(tplink_get_image_boot_size "$1")
256                 [ "$boot_size" != "00000000" ] && {
257                         echo "Invalid image, it contains a bootloader."
258                         return 1
259                 }
260
261                 return 0
262                 ;;
263         uap-pro)
264                 [ "$magic_long" != "19852003" ] && {
265                         echo "Invalid image type."
266                         return 1
267                 }
268                 return 0
269                 ;;
270         wndr3700 | \
271         wnr612-v2)
272                 local hw_magic
273
274                 hw_magic="$(ar71xx_get_mtd_part_magic firmware)"
275                 [ "$magic_long" != "$hw_magic" ] && {
276                         echo "Invalid image, hardware ID mismatch, hw:$hw_magic image:$magic_long."
277                         return 1
278                 }
279                 return 0
280                 ;;
281         routerstation | \
282         routerstation-pro | \
283         ls-sr71 | \
284         pb42 | \
285         pb44 | \
286         all0305 | \
287         eap7660d | \
288         ja76pf | \
289         ja76pf2 | \
290         jwap003)
291                 [ "$magic" != "4349" ] && {
292                         echo "Invalid image. Use *-sysupgrade.bin files on this board"
293                         return 1
294                 }
295
296                 local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
297                 local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
298
299                 if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
300                         return 0
301                 else
302                         echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
303                         return 1
304                 fi
305                 return 0
306                 ;;
307         esac
308
309         echo "Sysupgrade is not yet supported on $board."
310         return 1
311 }
312
313 platform_do_upgrade() {
314         local board=$(ar71xx_board_name)
315
316         case "$board" in
317         routerstation | \
318         routerstation-pro | \
319         ls-sr71 | \
320         all0305 | \
321         eap7660d | \
322         pb42 | \
323         pb44 | \
324         ja76pf | \
325         ja76pf2 | \
326         jwap003)
327                 platform_do_upgrade_combined "$ARGV"
328                 ;;
329         all0258n )
330                 platform_do_upgrade_allnet "0x9f050000" "$ARGV"
331                 ;;
332         all0315n )
333                 platform_do_upgrade_allnet "0x9f080000" "$ARGV"
334                 ;;
335         cap4200ag)
336                 platform_do_upgrade_allnet "0xbf0a0000" "$ARGV"
337                 ;;
338         dir-825-b1 |\
339         tew-673gru)
340                 platform_do_upgrade_dir825b "$ARGV"
341                 ;;
342         mr600 | \
343         mr600v2 | \
344         om2p | \
345         om2p-hs | \
346         om2p-lc)
347                 platform_do_upgrade_openmesh "$ARGV"
348                 ;;
349         uap-pro)
350                 MTD_CONFIG_ARGS="-s 0x180000"
351                 default_do_upgrade "$ARGV"
352                 ;;
353         *)
354                 default_do_upgrade "$ARGV"
355                 ;;
356         esac
357 }
358
359 disable_watchdog() {
360         killall watchdog
361         ( ps | grep -v 'grep' | grep '/dev/watchdog' ) && {
362                 echo 'Could not disable watchdog'
363                 return 1
364         }
365 }
366
367 append sysupgrade_pre_upgrade disable_watchdog