ar71xx: fix ap152 support
[openwrt/openwrt.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 cap324 | \
171 cap4200ag | \
172 cr3000 |\
173 cr5000)
174 platform_check_image_allnet "$1" && return 0
175 return 1
176 ;;
177 alfa-ap96 | \
178 alfa-nx | \
179 arduino-yun | \
180 ap113 | \
181 ap121 | \
182 ap121-mini | \
183 ap136-010 | \
184 ap136-020 | \
185 ap135-020 | \
186 ap147-010 | \
187 ap152 | \
188 ap96 | \
189 bxu2000n-2-a1 | \
190 db120 | \
191 dr344 | \
192 f9k1115v2 |\
193 hornet-ub | \
194 mr12 | \
195 mr16 | \
196 wpj558 | \
197 zcn-1523h-2 | \
198 zcn-1523h-5)
199 [ "$magic_long" != "68737173" -a "$magic_long" != "19852003" ] && {
200 echo "Invalid image type."
201 return 1
202 }
203 return 0
204 ;;
205 ap81 | \
206 ap83 | \
207 ap132 | \
208 c-55 | \
209 cf-e316n-v2 | \
210 dgl-5500-a1 |\
211 dhp-1565-a1 |\
212 dir-505-a1 | \
213 dir-600-a1 | \
214 dir-615-c1 | \
215 dir-615-e1 | \
216 dir-615-e4 | \
217 dir-615-i1 | \
218 dir-825-c1 | \
219 dir-835-a1 | \
220 dlan-hotspot | \
221 dlan-pro-500-wp | \
222 dlan-pro-1200-ac | \
223 dragino2 | \
224 epg5000 | \
225 esr1750 | \
226 esr900 | \
227 ew-dorin | \
228 ew-dorin-router | \
229 gl-ar150 | \
230 gl-ar300 | \
231 gl-domino | \
232 hiwifi-hc6361 | \
233 hornet-ub-x2 | \
234 mzk-w04nu | \
235 mzk-w300nh | \
236 tew-632brp | \
237 tew-712br | \
238 tew-732br | \
239 tew-823dru | \
240 wrt400n | \
241 airgateway | \
242 airgatewaypro | \
243 airrouter | \
244 bullet-m | \
245 loco-m-xw | \
246 nanostation-m | \
247 rocket-m | \
248 rocket-m-xw | \
249 rocket-m-ti | \
250 nanostation-m-xw | \
251 rw2458n | \
252 wpj531 | \
253 wndap360 | \
254 wpj344 | \
255 wzr-hp-g300nh2 | \
256 wzr-hp-g300nh | \
257 wzr-hp-g450h | \
258 wzr-hp-ag300h | \
259 wzr-450hp2 | \
260 whr-g301n | \
261 whr-hp-g300n | \
262 whr-hp-gn | \
263 wlae-ag300n | \
264 nbg460n_550n_550nh | \
265 unifi | \
266 unifi-outdoor | \
267 carambola2 | \
268 weio )
269 [ "$magic" != "2705" ] && {
270 echo "Invalid image type."
271 return 1
272 }
273 return 0
274 ;;
275
276 cpe510)
277 tplink_pharos_check_image "$1" && return 0
278 return 1
279 ;;
280
281 bsb | \
282 dir-825-b1 | \
283 tew-673gru)
284 dir825b_check_image "$1" && return 0
285 ;;
286
287 mynet-rext|\
288 wrt160nl)
289 cybertan_check_image "$1" && return 0
290 return 1
291 ;;
292
293 qihoo-c301 | \
294 mynet-n600 | \
295 mynet-n750)
296 [ "$magic_long" != "5ea3a417" ] && {
297 echo "Invalid image, bad magic: $magic_long"
298 return 1
299 }
300
301 local typemagic=$(seama_get_type_magic "$1")
302 [ "$typemagic" != "6669726d" ] && {
303 echo "Invalid image, bad type: $typemagic"
304 return 1
305 }
306
307 return 0;
308 ;;
309 mr1750 | \
310 mr600 | \
311 mr600v2 | \
312 mr900 | \
313 mr900v2 | \
314 om2p | \
315 om2pv2 | \
316 om2p-hs | \
317 om2p-hsv2 | \
318 om2p-lc | \
319 om5p | \
320 om5p-an)
321 platform_check_image_openmesh "$magic_long" "$1" && return 0
322 return 1
323 ;;
324
325 antminer-s1 | \
326 antminer-s3 | \
327 antrouter-r1 | \
328 archer-c5 | \
329 archer-c7 | \
330 el-m150 | \
331 el-mini | \
332 gl-inet | \
333 mc-mac1200r | \
334 minibox-v1 |\
335 onion-omega | \
336 oolite | \
337 smart-300 | \
338 tl-mr10u | \
339 tl-mr11u | \
340 tl-mr12u | \
341 tl-mr13u | \
342 tl-mr3020 | \
343 tl-mr3040 | \
344 tl-mr3040-v2 | \
345 tl-mr3220 | \
346 tl-mr3220-v2 | \
347 tl-mr3420 | \
348 tl-mr3420-v2 | \
349 tl-wa701nd-v2 | \
350 tl-wa7210n-v2 | \
351 tl-wa7510n | \
352 tl-wa750re | \
353 tl-wa850re | \
354 tl-wa860re | \
355 tl-wa801nd-v2 | \
356 tl-wa901nd | \
357 tl-wa901nd-v2 | \
358 tl-wa901nd-v3 | \
359 tl-wdr3320-v2 | \
360 tl-wdr3500 | \
361 tl-wdr4300 | \
362 tl-wdr4900-v2 | \
363 tl-wdr6500-v2 | \
364 tl-wr703n | \
365 tl-wr710n | \
366 tl-wr720n-v3 | \
367 tl-wr741nd | \
368 tl-wr741nd-v4 | \
369 tl-wr841n-v1 | \
370 tl-wa830re-v2 | \
371 tl-wr841n-v7 | \
372 tl-wr841n-v8 | \
373 tl-wr841n-v9 | \
374 tl-wr842n-v2 | \
375 tl-wr941nd | \
376 tl-wr941nd-v5 | \
377 tl-wr941nd-v6 | \
378 tl-wr1041n-v2 | \
379 tl-wr1043nd | \
380 tl-wr1043nd-v2 | \
381 tl-wr2543n)
382 local magic_ver="0100"
383
384 case "$board" in
385 tl-wdr6500-v2)
386 magic_ver="0200"
387 ;;
388 esac
389
390 [ "$magic" != "$magic_ver" ] && {
391 echo "Invalid image type."
392 return 1
393 }
394
395 local hwid
396 local imageid
397
398 hwid=$(tplink_get_hwid)
399 imageid=$(tplink_get_image_hwid "$1")
400
401 [ "$hwid" != "$imageid" ] && {
402 echo "Invalid image, hardware ID mismatch, hw:$hwid image:$imageid."
403 return 1
404 }
405
406 local boot_size
407
408 boot_size=$(tplink_get_image_boot_size "$1")
409 [ "$boot_size" != "00000000" ] && {
410 echo "Invalid image, it contains a bootloader."
411 return 1
412 }
413
414 return 0
415 ;;
416
417 tube2h)
418 alfa_check_image "$1" && return 0
419 return 1
420 ;;
421
422 nbg6616 | \
423 unifi-outdoor-plus | \
424 uap-pro)
425 [ "$magic_long" != "19852003" ] && {
426 echo "Invalid image type."
427 return 1
428 }
429 return 0
430 ;;
431 wndr3700 | \
432 wnr2000-v3 | \
433 wnr612-v2 | \
434 wnr1000-v2)
435 local hw_magic
436
437 hw_magic="$(ar71xx_get_mtd_part_magic firmware)"
438 [ "$magic_long" != "$hw_magic" ] && {
439 echo "Invalid image, hardware ID mismatch, hw:$hw_magic image:$magic_long."
440 return 1
441 }
442 return 0
443 ;;
444 mr18)
445 merakinand_do_platform_check $board $1
446 return $?;
447 ;;
448 nbg6716 | \
449 r6100 | \
450 wndr3700v4 | \
451 wndr4300 )
452 nand_do_platform_check $board $1
453 return $?;
454 ;;
455 routerstation | \
456 routerstation-pro | \
457 ls-sr71 | \
458 pb42 | \
459 pb44 | \
460 all0305 | \
461 eap300v2 | \
462 eap7660d | \
463 ja76pf | \
464 ja76pf2 | \
465 jwap003 | \
466 wp543 | \
467 wpe72)
468 [ "$magic" != "4349" ] && {
469 echo "Invalid image. Use *-sysupgrade.bin files on this board"
470 return 1
471 }
472
473 local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
474 local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
475
476 if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
477 return 0
478 else
479 echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
480 return 1
481 fi
482 return 0
483 ;;
484 wnr2000-v4)
485 [ "$magic_long" != "32303034" ] && {
486 echo "Invalid image type."
487 return 1
488 }
489 return 0
490 ;;
491 wnr2200)
492 [ "$magic_long" != "32323030" ] && {
493 echo "Invalid image type."
494 return 1
495 }
496 return 0
497 ;;
498
499 esac
500
501 echo "Sysupgrade is not yet supported on $board."
502 return 1
503 }
504
505 platform_pre_upgrade() {
506 local board=$(ar71xx_board_name)
507
508 case "$board" in
509 nbg6716 | \
510 r6100 | \
511 wndr3700v4 | \
512 wndr4300 )
513 nand_do_upgrade "$1"
514 ;;
515 mr18)
516 merakinand_do_upgrade "$1"
517 ;;
518 esac
519 }
520
521 platform_do_upgrade() {
522 local board=$(ar71xx_board_name)
523
524 case "$board" in
525 routerstation | \
526 routerstation-pro | \
527 ls-sr71 | \
528 all0305 | \
529 eap7660d | \
530 pb42 | \
531 pb44 | \
532 ja76pf | \
533 ja76pf2 | \
534 jwap003)
535 platform_do_upgrade_combined "$ARGV"
536 ;;
537 wp543|\
538 wpe72)
539 platform_do_upgrade_compex "$ARGV"
540 ;;
541 all0258n )
542 platform_do_upgrade_allnet "0x9f050000" "$ARGV"
543 ;;
544 all0315n )
545 platform_do_upgrade_allnet "0x9f080000" "$ARGV"
546 ;;
547 eap300v2 |\
548 cap4200ag)
549 platform_do_upgrade_allnet "0xbf0a0000" "$ARGV"
550 ;;
551 dir-825-b1 |\
552 tew-673gru)
553 platform_do_upgrade_dir825b "$ARGV"
554 ;;
555 mr1750 | \
556 mr600 | \
557 mr600v2 | \
558 mr900 | \
559 mr900v2 | \
560 om2p | \
561 om2pv2 | \
562 om2p-hs | \
563 om2p-hsv2 | \
564 om2p-lc | \
565 om5p | \
566 om5p-an)
567 platform_do_upgrade_openmesh "$ARGV"
568 ;;
569 unifi-outdoor-plus | \
570 uap-pro)
571 MTD_CONFIG_ARGS="-s 0x180000"
572 default_do_upgrade "$ARGV"
573 ;;
574 *)
575 default_do_upgrade "$ARGV"
576 ;;
577 esac
578 }
579
580 disable_watchdog() {
581 killall watchdog
582 ( ps | grep -v 'grep' | grep '/dev/watchdog' ) && {
583 echo 'Could not disable watchdog'
584 return 1
585 }
586 }
587
588 append sysupgrade_pre_upgrade disable_watchdog