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