ar71xx: add support for TP-LINK CPE210/220/510/520
[openwrt/staging/wigyori.git] / target / linux / ar71xx / base-files / lib / upgrade / platform.sh
1 #
2 # Copyright (C) 2011 OpenWrt.org
3 #
4
5 . /lib/functions/system.sh
6 . /lib/ar71xx.sh
7
8 PART_NAME=firmware
9 RAMFS_COPY_DATA=/lib/ar71xx.sh
10
11 CI_BLKSZ=65536
12 CI_LDADR=0x80060000
13
14 platform_find_partitions() {
15 local first dev size erasesize name
16 while read dev size erasesize name; do
17 name=${name#'"'}; name=${name%'"'}
18 case "$name" in
19 vmlinux.bin.l7|vmlinux|kernel|linux|linux.bin|rootfs|filesystem)
20 if [ -z "$first" ]; then
21 first="$name"
22 else
23 echo "$erasesize:$first:$name"
24 break
25 fi
26 ;;
27 esac
28 done < /proc/mtd
29 }
30
31 platform_find_kernelpart() {
32 local part
33 for part in "${1%:*}" "${1#*:}"; do
34 case "$part" in
35 vmlinux.bin.l7|vmlinux|kernel|linux|linux.bin)
36 echo "$part"
37 break
38 ;;
39 esac
40 done
41 }
42
43 platform_do_upgrade_combined() {
44 local partitions=$(platform_find_partitions)
45 local kernelpart=$(platform_find_kernelpart "${partitions#*:}")
46 local erase_size=$((0x${partitions%%:*})); partitions="${partitions#*:}"
47 local kern_length=0x$(dd if="$1" bs=2 skip=1 count=4 2>/dev/null)
48 local kern_blocks=$(($kern_length / $CI_BLKSZ))
49 local root_blocks=$((0x$(dd if="$1" bs=2 skip=5 count=4 2>/dev/null) / $CI_BLKSZ))
50
51 if [ -n "$partitions" ] && [ -n "$kernelpart" ] && \
52 [ ${kern_blocks:-0} -gt 0 ] && \
53 [ ${root_blocks:-0} -gt 0 ] && \
54 [ ${erase_size:-0} -gt 0 ];
55 then
56 local append=""
57 [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
58
59 ( dd if="$1" bs=$CI_BLKSZ skip=1 count=$kern_blocks 2>/dev/null; \
60 dd if="$1" bs=$CI_BLKSZ skip=$((1+$kern_blocks)) count=$root_blocks 2>/dev/null ) | \
61 mtd -r $append -F$kernelpart:$kern_length:$CI_LDADR,rootfs write - $partitions
62 fi
63 }
64
65 tplink_get_image_hwid() {
66 get_image "$@" | dd bs=4 count=1 skip=16 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
67 }
68
69 tplink_get_image_boot_size() {
70 get_image "$@" | dd bs=4 count=1 skip=37 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
71 }
72
73 tplink_pharos_check_image() {
74 local magic_long="$(get_magic_long "$1")"
75 [ "$magic_long" != "7f454c46" ] && {
76 echo "Invalid image magic '$magic_long'"
77 return 1
78 }
79
80 local model_string="$(tplink_pharos_get_model_string)"
81 local line
82
83 # Here $1 is given to dd directly instead of get_image as otherwise the skip
84 # will take almost a second (as dd can't seek then)
85 #
86 # This will fail if the image isn't local, but that's fine: as the
87 # read loop won't be executed at all, it will return true, so the image
88 # is accepted (loading the first 1.5M of a remote image for this check seems
89 # a bit extreme)
90 dd if="$1" bs=1 skip=1511432 count=1024 2>/dev/null | while read line; do
91 [ "$line" == "$model_string" ] && break
92 done || {
93 echo "Unsupported image (model not in support-list)"
94 return 1
95 }
96
97 return 0
98 }
99
100 seama_get_type_magic() {
101 get_image "$@" | dd bs=1 count=4 skip=53 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
102 }
103
104 cybertan_get_image_magic() {
105 get_image "$@" | dd bs=8 count=1 skip=0 2>/dev/null | hexdump -v -n 8 -e '1/1 "%02x"'
106 }
107
108 cybertan_check_image() {
109 local magic="$(cybertan_get_image_magic "$1")"
110 local fw_magic="$(cybertan_get_hw_magic)"
111
112 [ "$fw_magic" != "$magic" ] && {
113 echo "Invalid image, ID mismatch, got:$magic, but need:$fw_magic"
114 return 1
115 }
116
117 return 0
118 }
119
120 platform_do_upgrade_compex() {
121 local fw_file=$1
122 local fw_part=$PART_NAME
123 local fw_mtd=$(find_mtd_part $fw_part)
124 local fw_length=0x$(dd if="$fw_file" bs=2 skip=1 count=4 2>/dev/null)
125 local fw_blocks=$(($fw_length / 65536))
126
127 if [ -n "$fw_mtd" ] && [ ${fw_blocks:-0} -gt 0 ]; then
128 local append=""
129 [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
130
131 sync
132 dd if="$fw_file" bs=64k skip=1 count=$fw_blocks 2>/dev/null | \
133 mtd $append write - "$fw_part"
134 fi
135 }
136
137 alfa_check_image() {
138 local magic_long="$(get_magic_long "$1")"
139 local fw_part_size=$(mtd_get_part_size firmware)
140
141 case "$magic_long" in
142 "27051956")
143 [ "$fw_part_size" != "16318464" ] && {
144 echo "Invalid image magic \"$magic_long\" for $fw_part_size bytes"
145 return 1
146 }
147 ;;
148
149 "68737173")
150 [ "$fw_part_size" != "7929856" ] && {
151 echo "Invalid image magic \"$magic_long\" for $fw_part_size bytes"
152 return 1
153 }
154 ;;
155 esac
156
157 return 0
158 }
159
160 platform_check_image() {
161 local board=$(ar71xx_board_name)
162 local magic="$(get_magic_word "$1")"
163 local magic_long="$(get_magic_long "$1")"
164
165 [ "$#" -gt 1 ] && return 1
166
167 case "$board" in
168 all0315n | \
169 all0258n | \
170 cap4200ag)
171 platform_check_image_allnet "$1" && return 0
172 return 1
173 ;;
174 alfa-ap96 | \
175 alfa-nx | \
176 ap113 | \
177 ap121 | \
178 ap121-mini | \
179 ap136-010 | \
180 ap136-020 | \
181 ap135-020 | \
182 ap96 | \
183 db120 | \
184 hornet-ub | \
185 bxu2000n-2-a1 | \
186 zcn-1523h-2 | \
187 zcn-1523h-5)
188 [ "$magic_long" != "68737173" -a "$magic_long" != "19852003" ] && {
189 echo "Invalid image type."
190 return 1
191 }
192 return 0
193 ;;
194 ap81 | \
195 ap83 | \
196 ap132 | \
197 dhp-1565-a1 |\
198 dir-505-a1 | \
199 dir-600-a1 | \
200 dir-615-c1 | \
201 dir-615-e1 | \
202 dir-615-e4 | \
203 dir-825-c1 | \
204 dir-835-a1 | \
205 dragino2 | \
206 esr1750 | \
207 esr900 | \
208 ew-dorin | \
209 ew-dorin-router | \
210 hiwifi-hc6361 | \
211 hornet-ub-x2 | \
212 mzk-w04nu | \
213 mzk-w300nh | \
214 tew-632brp | \
215 tew-712br | \
216 tew-732br | \
217 wrt400n | \
218 airgateway | \
219 airrouter | \
220 bullet-m | \
221 loco-m-xw | \
222 nanostation-m | \
223 rocket-m | \
224 nanostation-m-xw | \
225 rw2458n | \
226 wndap360 | \
227 wzr-hp-g300nh2 | \
228 wzr-hp-g300nh | \
229 wzr-hp-g450h | \
230 wzr-hp-ag300h | \
231 wzr-450hp2 | \
232 whr-g301n | \
233 whr-hp-g300n | \
234 whr-hp-gn | \
235 wlae-ag300n | \
236 nbg460n_550n_550nh | \
237 unifi | \
238 unifi-outdoor | \
239 carambola2 )
240 [ "$magic" != "2705" ] && {
241 echo "Invalid image type."
242 return 1
243 }
244 return 0
245 ;;
246
247 cpe510)
248 tplink_pharos_check_image "$1" && return 0
249 return 1
250 ;;
251
252 dir-825-b1 | \
253 tew-673gru)
254 dir825b_check_image "$1" && return 0
255 ;;
256
257 mynet-rext|\
258 wrt160nl)
259 cybertan_check_image "$1" && return 0
260 return 1
261 ;;
262
263 qihoo-c301 | \
264 mynet-n600 | \
265 mynet-n750)
266 [ "$magic_long" != "5ea3a417" ] && {
267 echo "Invalid image, bad magic: $magic_long"
268 return 1
269 }
270
271 local typemagic=$(seama_get_type_magic "$1")
272 [ "$typemagic" != "6669726d" ] && {
273 echo "Invalid image, bad type: $typemagic"
274 return 1
275 }
276
277 return 0;
278 ;;
279 mr600 | \
280 mr600v2 | \
281 mr900 | \
282 mr900v2 | \
283 om2p | \
284 om2pv2 | \
285 om2p-hs | \
286 om2p-hsv2 | \
287 om2p-lc | \
288 om5p)
289 platform_check_image_openmesh "$magic_long" "$1" && return 0
290 return 1
291 ;;
292
293 archer-c5 | \
294 archer-c7 | \
295 el-m150 | \
296 el-mini | \
297 gl-inet | \
298 oolite | \
299 smart-300 | \
300 tl-mr10u | \
301 tl-mr11u | \
302 tl-mr13u | \
303 tl-mr3020 | \
304 tl-mr3040 | \
305 tl-mr3040-v2 | \
306 tl-mr3220 | \
307 tl-mr3220-v2 | \
308 tl-mr3420 | \
309 tl-mr3420-v2 | \
310 tl-wa7510n | \
311 tl-wa750re | \
312 tl-wa850re | \
313 tl-wa860re | \
314 tl-wa801nd-v2 | \
315 tl-wa901nd | \
316 tl-wa901nd-v2 | \
317 tl-wa901nd-v3 | \
318 tl-wdr3500 | \
319 tl-wdr4300 | \
320 tl-wdr4900-v2 | \
321 tl-wr703n | \
322 tl-wr710n | \
323 tl-wr720n-v3 | \
324 tl-wr741nd | \
325 tl-wr741nd-v4 | \
326 tl-wr841n-v1 | \
327 tl-wa830re-v2 | \
328 tl-wr841n-v7 | \
329 tl-wr841n-v8 | \
330 tl-wr841n-v9 | \
331 tl-wr842n-v2 | \
332 tl-wr941nd | \
333 tl-wr1041n-v2 | \
334 tl-wr1043nd | \
335 tl-wr1043nd-v2 | \
336 tl-wr2543n)
337 [ "$magic" != "0100" ] && {
338 echo "Invalid image type."
339 return 1
340 }
341
342 local hwid
343 local imageid
344
345 hwid=$(tplink_get_hwid)
346 imageid=$(tplink_get_image_hwid "$1")
347
348 [ "$hwid" != "$imageid" ] && {
349 echo "Invalid image, hardware ID mismatch, hw:$hwid image:$imageid."
350 return 1
351 }
352
353 local boot_size
354
355 boot_size=$(tplink_get_image_boot_size "$1")
356 [ "$boot_size" != "00000000" ] && {
357 echo "Invalid image, it contains a bootloader."
358 return 1
359 }
360
361 return 0
362 ;;
363
364 tube2h)
365 alfa_check_image "$1" && return 0
366 return 1
367 ;;
368
369 uap-pro)
370 [ "$magic_long" != "19852003" ] && {
371 echo "Invalid image type."
372 return 1
373 }
374 return 0
375 ;;
376 wndr3700 | \
377 wnr2000-v3 | \
378 wnr612-v2)
379 local hw_magic
380
381 hw_magic="$(ar71xx_get_mtd_part_magic firmware)"
382 [ "$magic_long" != "$hw_magic" ] && {
383 echo "Invalid image, hardware ID mismatch, hw:$hw_magic image:$magic_long."
384 return 1
385 }
386 return 0
387 ;;
388 nbg6716 | \
389 r6100 | \
390 wndr3700v4 | \
391 wndr4300 )
392 nand_do_platform_check $board $1
393 return $?;
394 ;;
395 routerstation | \
396 routerstation-pro | \
397 ls-sr71 | \
398 pb42 | \
399 pb44 | \
400 all0305 | \
401 eap300v2 | \
402 eap7660d | \
403 ja76pf | \
404 ja76pf2 | \
405 jwap003 | \
406 wp543 | \
407 wpe72)
408 [ "$magic" != "4349" ] && {
409 echo "Invalid image. Use *-sysupgrade.bin files on this board"
410 return 1
411 }
412
413 local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
414 local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
415
416 if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
417 return 0
418 else
419 echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
420 return 1
421 fi
422 return 0
423 ;;
424 wnr2000-v4)
425 [ "$magic_long" != "32303034" ] && {
426 echo "Invalid image type."
427 return 1
428 }
429 return 0
430 ;;
431
432 esac
433
434 echo "Sysupgrade is not yet supported on $board."
435 return 1
436 }
437
438 platform_do_upgrade() {
439 local board=$(ar71xx_board_name)
440
441 case "$board" in
442 routerstation | \
443 routerstation-pro | \
444 ls-sr71 | \
445 all0305 | \
446 eap7660d | \
447 pb42 | \
448 pb44 | \
449 ja76pf | \
450 ja76pf2 | \
451 jwap003)
452 platform_do_upgrade_combined "$ARGV"
453 ;;
454 wp543|\
455 wpe72)
456 platform_do_upgrade_compex "$ARGV"
457 ;;
458 all0258n )
459 platform_do_upgrade_allnet "0x9f050000" "$ARGV"
460 ;;
461 all0315n )
462 platform_do_upgrade_allnet "0x9f080000" "$ARGV"
463 ;;
464 eap300v2 |\
465 cap4200ag)
466 platform_do_upgrade_allnet "0xbf0a0000" "$ARGV"
467 ;;
468 dir-825-b1 |\
469 tew-673gru)
470 platform_do_upgrade_dir825b "$ARGV"
471 ;;
472 mr600 | \
473 mr600v2 | \
474 mr900 | \
475 mr900v2 | \
476 om2p | \
477 om2pv2 | \
478 om2p-hs | \
479 om2p-hsv2 | \
480 om2p-lc | \
481 om5p)
482 platform_do_upgrade_openmesh "$ARGV"
483 ;;
484 uap-pro)
485 MTD_CONFIG_ARGS="-s 0x180000"
486 default_do_upgrade "$ARGV"
487 ;;
488 *)
489 default_do_upgrade "$ARGV"
490 ;;
491 esac
492 }
493
494 disable_watchdog() {
495 killall watchdog
496 ( ps | grep -v 'grep' | grep '/dev/watchdog' ) && {
497 echo 'Could not disable watchdog'
498 return 1
499 }
500 }
501
502 append sysupgrade_pre_upgrade disable_watchdog