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