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