ar71xx: sysupgrade for My Net Wi-Fi Range Extender
[openwrt/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 cybertan_check_image "$1" && return 0
176 return 1
177 ;;
178
179 mynet-n600)
180 [ "$magic_long" != "5ea3a417" ] && {
181 echo "Invalid image, bad magic: $magic_long"
182 return 1
183 }
184
185 local typemagic=$(seama_get_type_magic "$1")
186 [ "$typemagic" != "6669726d" ] && {
187 echo "Invalid image, bad type: $typemagic"
188 return 1
189 }
190
191 return 0;
192 ;;
193 mr600 | \
194 mr600v2 | \
195 om2p | \
196 om2p-hs | \
197 om2p-lc)
198 platform_check_image_openmesh "$magic_long" "$1" && return 0
199 return 1
200 ;;
201
202 archer-c7 | \
203 tl-mr10u | \
204 tl-mr11u | \
205 tl-mr13u | \
206 tl-mr3020 | \
207 tl-mr3040 | \
208 tl-mr3040-v2 | \
209 tl-mr3220 | \
210 tl-mr3220-v2 | \
211 tl-mr3420 | \
212 tl-mr3420-v2 | \
213 tl-wa7510n | \
214 tl-wa750re | \
215 tl-wa850re | \
216 tl-wa901nd | \
217 tl-wa901nd-v2 | \
218 tl-wdr3500 | \
219 tl-wdr4300 | \
220 tl-wr703n | \
221 tl-wr710n | \
222 tl-wr720n-v3 | \
223 tl-wr741nd | \
224 tl-wr741nd-v4 | \
225 tl-wr841n-v1 | \
226 tl-wr841n-v7 | \
227 tl-wr841n-v8 | \
228 tl-wr842n-v2 | \
229 tl-wr941nd | \
230 tl-wr1041n-v2 | \
231 tl-wr1043nd | \
232 tl-wr1043nd-v2 | \
233 tl-wr2543n)
234 [ "$magic" != "0100" ] && {
235 echo "Invalid image type."
236 return 1
237 }
238
239 local hwid
240 local imageid
241
242 hwid=$(tplink_get_hwid)
243 imageid=$(tplink_get_image_hwid "$1")
244
245 [ "$hwid" != "$imageid" ] && {
246 echo "Invalid image, hardware ID mismatch, hw:$hwid image:$imageid."
247 return 1
248 }
249
250 local boot_size
251
252 boot_size=$(tplink_get_image_boot_size "$1")
253 [ "$boot_size" != "00000000" ] && {
254 echo "Invalid image, it contains a bootloader."
255 return 1
256 }
257
258 return 0
259 ;;
260 uap-pro)
261 [ "$magic_long" != "19852003" ] && {
262 echo "Invalid image type."
263 return 1
264 }
265 return 0
266 ;;
267 wndr3700 | \
268 wnr612-v2)
269 local hw_magic
270
271 hw_magic="$(ar71xx_get_mtd_part_magic firmware)"
272 [ "$magic_long" != "$hw_magic" ] && {
273 echo "Invalid image, hardware ID mismatch, hw:$hw_magic image:$magic_long."
274 return 1
275 }
276 return 0
277 ;;
278 wrt160nl)
279 [ "$magic" != "4e4c" ] && {
280 echo "Invalid image type."
281 return 1
282 }
283 return 0
284 ;;
285 routerstation | \
286 routerstation-pro | \
287 ls-sr71 | \
288 pb42 | \
289 pb44 | \
290 all0305 | \
291 eap7660d | \
292 ja76pf | \
293 ja76pf2 | \
294 jwap003)
295 [ "$magic" != "4349" ] && {
296 echo "Invalid image. Use *-sysupgrade.bin files on this board"
297 return 1
298 }
299
300 local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
301 local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
302
303 if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
304 return 0
305 else
306 echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
307 return 1
308 fi
309 return 0
310 ;;
311 esac
312
313 echo "Sysupgrade is not yet supported on $board."
314 return 1
315 }
316
317 platform_do_upgrade() {
318 local board=$(ar71xx_board_name)
319
320 case "$board" in
321 routerstation | \
322 routerstation-pro | \
323 ls-sr71 | \
324 all0305 | \
325 eap7660d | \
326 pb42 | \
327 pb44 | \
328 ja76pf | \
329 ja76pf2 | \
330 jwap003)
331 platform_do_upgrade_combined "$ARGV"
332 ;;
333 all0258n )
334 platform_do_upgrade_allnet "0x9f050000" "$ARGV"
335 ;;
336 all0315n )
337 platform_do_upgrade_allnet "0x9f080000" "$ARGV"
338 ;;
339 cap4200ag)
340 platform_do_upgrade_allnet "0xbf0a0000" "$ARGV"
341 ;;
342 dir-825-b1 |\
343 tew-673gru)
344 platform_do_upgrade_dir825b "$ARGV"
345 ;;
346 mr600 | \
347 mr600v2 | \
348 om2p | \
349 om2p-hs | \
350 om2p-lc)
351 platform_do_upgrade_openmesh "$ARGV"
352 ;;
353 uap-pro)
354 MTD_CONFIG_ARGS="-s 0x180000"
355 default_do_upgrade "$ARGV"
356 ;;
357 *)
358 default_do_upgrade "$ARGV"
359 ;;
360 esac
361 }
362
363 disable_watchdog() {
364 killall watchdog
365 ( ps | grep -v 'grep' | grep '/dev/watchdog' ) && {
366 echo 'Could not disable watchdog'
367 return 1
368 }
369 }
370
371 append sysupgrade_pre_upgrade disable_watchdog