ar71xx: Add userspace support for AP135
[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 ${kern_blocks:-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 platform_check_image() {
73         local board=$(ar71xx_board_name)
74         local magic="$(get_magic_word "$1")"
75         local magic_long="$(get_magic_long "$1")"
76
77         [ "$ARGC" -gt 1 ] && return 1
78
79         case "$board" in
80         all0315n | \
81         all0258n | \
82         cap4200ag)
83                 platform_check_image_allnet "$1" && return 0
84                 return 1
85                 ;;
86         alfa-ap96 | \
87         alfa-nx | \
88         ap113 | \
89         ap121 | \
90         ap121-mini | \
91         ap136-010 | \
92         ap136-020 | \
93         ap135-020 | \
94         ap96 | \
95         db120 | \
96         hornet-ub | \
97         zcn-1523h-2 | \
98         zcn-1523h-5)
99                 [ "$magic_long" != "68737173" -a "$magic_long" != "19852003" ] && {
100                         echo "Invalid image type."
101                         return 1
102                 }
103                 return 0
104                 ;;
105         ap81 | \
106         ap83 | \
107         dir-600-a1 | \
108         dir-615-c1 | \
109         dir-615-e4 | \
110         ew-dorin | \
111         ew-dorin-router | \
112         mzk-w04nu | \
113         mzk-w300nh | \
114         tew-632brp | \
115         tew-712br | \
116         wrt400n | \
117         airrouter | \
118         bullet-m | \
119         nanostation-m | \
120         rocket-m | \
121         rw2458n | \
122         wzr-hp-g300nh2 | \
123         wzr-hp-g300nh | \
124         wzr-hp-g450h | \
125         wzr-hp-ag300h | \
126         whr-g301n | \
127         whr-hp-g300n | \
128         whr-hp-gn | \
129         wlae-ag300n | \
130         nbg460n_550n_550nh | \
131         unifi | \
132         unifi-outdoor )
133                 [ "$magic" != "2705" ] && {
134                         echo "Invalid image type."
135                         return 1
136                 }
137                 return 0
138                 ;;
139
140         dir-825-b1 | \
141         tew-673gru)
142                 dir825b_check_image "$1" && return 0
143                 ;;
144
145         mr600 | \
146         om2p | \
147         om2p-hs | \
148         om2p-lc)
149                 platform_check_image_openmesh "$magic_long" "$1" && return 0
150                 return 1
151                 ;;
152         tl-mr11u | \
153         tl-mr3020 | \
154         tl-mr3040 | \
155         tl-mr3220 | \
156         tl-mr3220-v2 | \
157         tl-mr3420 | \
158         tl-wa7510n | \
159         tl-wa901nd | \
160         tl-wa901nd-v2 | \
161         tl-wdr4300 | \
162         tl-wr703n | \
163         tl-wr741nd | \
164         tl-wr741nd-v4 | \
165         tl-wr841n-v1 | \
166         tl-wr841n-v7 | \
167         tl-wr841n-v8 | \
168         tl-wr941nd | \
169         tl-wr1041n-v2 | \
170         tl-wr1043nd | \
171         tl-wr2543n)
172                 [ "$magic" != "0100" ] && {
173                         echo "Invalid image type."
174                         return 1
175                 }
176
177                 local hwid
178                 local imageid
179
180                 hwid=$(tplink_get_hwid)
181                 imageid=$(tplink_get_image_hwid "$1")
182
183                 [ "$hwid" != "$imageid" ] && {
184                         echo "Invalid image, hardware ID mismatch, hw:$hwid image:$imageid."
185                         return 1
186                 }
187
188                 local boot_size
189
190                 boot_size=$(tplink_get_image_boot_size "$1")
191                 [ "$boot_size" != "00000000" ] && {
192                         echo "Invalid image, it contains a bootloader."
193                         return 1
194                 }
195
196                 return 0
197                 ;;
198         uap-pro)
199                 [ "$magic_long" != "19852003" ] && {
200                         echo "Invalid image type."
201                         return 1
202                 }
203                 return 0
204                 ;;
205         wndr3700)
206                 local hw_magic
207
208                 hw_magic="$(ar71xx_get_mtd_part_magic firmware)"
209                 [ "$magic_long" != "$hw_magic" ] && {
210                         echo "Invalid image, hardware ID mismatch, hw:$hw_magic image:$magic_long."
211                         return 1
212                 }
213                 return 0
214                 ;;
215         wrt160nl)
216                 [ "$magic" != "4e4c" ] && {
217                         echo "Invalid image type."
218                         return 1
219                 }
220                 return 0
221                 ;;
222         routerstation | \
223         routerstation-pro | \
224         ls-sr71 | \
225         pb42 | \
226         pb44 | \
227         all0305 | \
228         eap7660d | \
229         ja76pf | \
230         ja76pf2 | \
231         jwap003)
232                 [ "$magic" != "4349" ] && {
233                         echo "Invalid image. Use *-sysupgrade.bin files on this board"
234                         return 1
235                 }
236
237                 local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
238                 local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
239
240                 if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
241                         return 0
242                 else
243                         echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
244                         return 1
245                 fi
246                 return 0
247                 ;;
248         esac
249
250         echo "Sysupgrade is not yet supported on $board."
251         return 1
252 }
253
254 platform_do_upgrade() {
255         local board=$(ar71xx_board_name)
256
257         case "$board" in
258         routerstation | \
259         routerstation-pro | \
260         ls-sr71 | \
261         all0305 | \
262         eap7660d | \
263         pb42 | \
264         pb44 | \
265         ja76pf | \
266         ja76pf2 | \
267         jwap003)
268                 platform_do_upgrade_combined "$ARGV"
269                 ;;
270         all0258n )
271                 platform_do_upgrade_allnet "0x9f050000" "$ARGV"
272                 ;;
273         all0315n )
274                 platform_do_upgrade_allnet "0x9f080000" "$ARGV"
275                 ;;
276         cap4200ag)
277                 platform_do_upgrade_allnet "0xbf0a0000" "$ARGV"
278                 ;;
279         dir-825-b1 |\
280         tew-673gru)
281                 platform_do_upgrade_dir825b "$ARGV"
282                 ;;
283         mr600 | \
284         om2p | \
285         om2p-hs | \
286         om2p-lc)
287                 platform_do_upgrade_openmesh "$ARGV"
288                 ;;
289         *)
290                 default_do_upgrade "$ARGV"
291                 ;;
292         esac
293 }
294
295 disable_watchdog() {
296         killall watchdog
297         ( ps | grep -v 'grep' | grep '/dev/watchdog' ) && {
298                 echo 'Could not disable watchdog'
299                 return 1
300         }
301 }
302
303 append sysupgrade_pre_upgrade disable_watchdog