ar71xx: add support for COMFAST CF-E320N v2 and CF-E520N/CF-E530N
[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_mid() {
70 get_image "$@" | dd bs=4 count=1 skip=17 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
71 }
72
73 tplink_get_image_boot_size() {
74 get_image "$@" | dd bs=4 count=1 skip=37 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
75 }
76
77 tplink_pharos_check_image() {
78 local magic_long="$(get_magic_long "$1")"
79 [ "$magic_long" != "7f454c46" ] && {
80 echo "Invalid image magic '$magic_long'"
81 return 1
82 }
83
84 local model_string="$(tplink_pharos_get_model_string)"
85 local line
86
87 # Here $1 is given to dd directly instead of get_image as otherwise the skip
88 # will take almost a second (as dd can't seek then)
89 #
90 # This will fail if the image isn't local, but that's fine: as the
91 # read loop won't be executed at all, it will return true, so the image
92 # is accepted (loading the first 1.5M of a remote image for this check seems
93 # a bit extreme)
94 dd if="$1" bs=1 skip=1511432 count=1024 2>/dev/null | while read line; do
95 [ "$line" == "$model_string" ] && break
96 done || {
97 echo "Unsupported image (model not in support-list)"
98 return 1
99 }
100
101 return 0
102 }
103
104 seama_get_type_magic() {
105 get_image "$@" | dd bs=1 count=4 skip=53 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
106 }
107
108 wrgg_get_image_magic() {
109 get_image "$@" | dd bs=4 count=1 skip=8 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
110 }
111
112 cybertan_get_image_magic() {
113 get_image "$@" | dd bs=8 count=1 skip=0 2>/dev/null | hexdump -v -n 8 -e '1/1 "%02x"'
114 }
115
116 cybertan_check_image() {
117 local magic="$(cybertan_get_image_magic "$1")"
118 local fw_magic="$(cybertan_get_hw_magic)"
119
120 [ "$fw_magic" != "$magic" ] && {
121 echo "Invalid image, ID mismatch, got:$magic, but need:$fw_magic"
122 return 1
123 }
124
125 return 0
126 }
127
128 platform_do_upgrade_compex() {
129 local fw_file=$1
130 local fw_part=$PART_NAME
131 local fw_mtd=$(find_mtd_part $fw_part)
132 local fw_length=0x$(dd if="$fw_file" bs=2 skip=1 count=4 2>/dev/null)
133 local fw_blocks=$(($fw_length / 65536))
134
135 if [ -n "$fw_mtd" ] && [ ${fw_blocks:-0} -gt 0 ]; then
136 local append=""
137 [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
138
139 sync
140 dd if="$fw_file" bs=64k skip=1 count=$fw_blocks 2>/dev/null | \
141 mtd $append write - "$fw_part"
142 fi
143 }
144
145 alfa_check_image() {
146 local magic_long="$(get_magic_long "$1")"
147 local fw_part_size=$(mtd_get_part_size firmware)
148
149 case "$magic_long" in
150 "27051956")
151 [ "$fw_part_size" != "16318464" ] && {
152 echo "Invalid image magic \"$magic_long\" for $fw_part_size bytes"
153 return 1
154 }
155 ;;
156 "68737173")
157 [ "$fw_part_size" != "7929856" ] && {
158 echo "Invalid image magic \"$magic_long\" for $fw_part_size bytes"
159 return 1
160 }
161 ;;
162 esac
163
164 return 0
165 }
166
167 platform_check_image() {
168 local board=$(ar71xx_board_name)
169 local magic="$(get_magic_word "$1")"
170 local magic_long="$(get_magic_long "$1")"
171
172 [ "$#" -gt 1 ] && return 1
173
174 case "$board" in
175 airgatewaypro|\
176 airgateway|\
177 airrouter|\
178 ap132|\
179 ap81|\
180 ap83|\
181 bullet-m|\
182 c-55|\
183 carambola2|\
184 cf-e316n-v2|\
185 cf-e320n-v2|\
186 cf-e520n|\
187 cf-e530n|\
188 dgl-5500-a1|\
189 dhp-1565-a1|\
190 dir-505-a1|\
191 dir-600-a1|\
192 dir-615-c1|\
193 dir-615-e1|\
194 dir-615-e4|\
195 dir-615-i1|\
196 dir-825-c1|\
197 dir-835-a1|\
198 dlan-hotspot|\
199 dlan-pro-1200-ac|\
200 dlan-pro-500-wp|\
201 dr531|\
202 dragino2|\
203 epg5000|\
204 esr1750|\
205 esr900|\
206 ew-dorin-router|\
207 ew-dorin|\
208 gl-ar150|\
209 gl-ar300m|\
210 gl-ar300|\
211 gl-domino|\
212 gl-mifi|\
213 hiwifi-hc6361|\
214 hornet-ub-x2|\
215 jwap230|\
216 loco-m-xw|\
217 mzk-w04nu|\
218 mzk-w300nh|\
219 nanostation-m-xw|\
220 nanostation-m|\
221 nbg460n_550n_550nh|\
222 rocket-m-ti|\
223 rocket-m-xw|\
224 rocket-m|\
225 rw2458n|\
226 tew-632brp|\
227 tew-712br|\
228 tew-732br|\
229 tew-823dru|\
230 unifi-outdoor|\
231 unifiac-lite|\
232 unifiac-pro|\
233 unifi|\
234 weio|\
235 whr-g301n|\
236 whr-hp-g300n|\
237 whr-hp-gn|\
238 wlae-ag300n|\
239 wndap360|\
240 wpj342|\
241 wpj344|\
242 wpj531|\
243 wrt400n|\
244 wrtnode2q|\
245 wzr-450hp2|\
246 wzr-hp-ag300h|\
247 wzr-hp-g300nh2|\
248 wzr-hp-g300nh|\
249 wzr-hp-g450h)
250 [ "$magic" != "2705" ] && {
251 echo "Invalid image type."
252 return 1
253 }
254
255 return 0
256 ;;
257 alfa-ap96|\
258 alfa-nx|\
259 ap113|\
260 ap121-mini|\
261 ap121|\
262 ap135-020|\
263 ap136-010|\
264 ap136-020|\
265 ap147-010|\
266 ap152|\
267 ap96|\
268 arduino-yun|\
269 bhr-4grv2|\
270 bxu2000n-2-a1|\
271 db120|\
272 dr344|\
273 dw33d|\
274 f9k1115v2|\
275 hornet-ub|\
276 mr12|\
277 mr16|\
278 wpj558|\
279 zbt-we1526|\
280 zcn-1523h-2|\
281 zcn-1523h-5)
282 [ "$magic_long" != "68737173" -a "$magic_long" != "19852003" ] && {
283 echo "Invalid image type."
284 return 1
285 }
286
287 return 0
288 ;;
289 all0258n|\
290 all0315n|\
291 cap324|\
292 cap4200ag|\
293 cr3000|\
294 cr5000)
295 platform_check_image_allnet "$1" && return 0
296 return 1
297 ;;
298 all0305|\
299 eap300v2|\
300 eap7660d|\
301 ja76pf2|\
302 ja76pf|\
303 jwap003|\
304 ls-sr71|\
305 pb42|\
306 pb44|\
307 routerstation-pro|\
308 routerstation|\
309 wp543|\
310 wpe72)
311 [ "$magic" != "4349" ] && {
312 echo "Invalid image. Use *-sysupgrade.bin files on this board"
313 return 1
314 }
315
316 local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
317 local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
318
319 if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
320 return 0
321 else
322 echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
323 return 1
324 fi
325
326 return 0
327 ;;
328 antminer-s1|\
329 antminer-s3|\
330 antrouter-r1|\
331 archer-c5|\
332 archer-c7|\
333 el-m150|\
334 el-mini|\
335 gl-inet|\
336 mc-mac1200r|\
337 minibox-v1|\
338 omy-g1|\
339 omy-x1|\
340 onion-omega|\
341 oolite|\
342 smart-300|\
343 som9331|\
344 tellstick-znet-lite|\
345 tl-mr10u|\
346 tl-mr11u|\
347 tl-mr12u|\
348 tl-mr13u|\
349 tl-mr3020|\
350 tl-mr3040-v2|\
351 tl-mr3040|\
352 tl-mr3220-v2|\
353 tl-mr3220|\
354 tl-mr3420-v2|\
355 tl-mr3420|\
356 tl-wa701nd-v2|\
357 tl-wa7210n-v2|\
358 tl-wa750re|\
359 tl-wa7510n|\
360 tl-wa801nd-v2|\
361 tl-wa830re-v2|\
362 tl-wa850re|\
363 tl-wa860re|\
364 tl-wa901nd-v2|\
365 tl-wa901nd-v3|\
366 tl-wa901nd-v4|\
367 tl-wa901nd|\
368 tl-wdr3320-v2|\
369 tl-wdr3500|\
370 tl-wdr4300|\
371 tl-wdr4900-v2|\
372 tl-wdr6500-v2|\
373 tl-wpa8630|\
374 tl-wr1041n-v2|\
375 tl-wr1043nd-v2|\
376 tl-wr1043nd|\
377 tl-wr2543n|\
378 tl-wr703n|\
379 tl-wr710n|\
380 tl-wr720n-v3|\
381 tl-wr741nd-v4|\
382 tl-wr741nd|\
383 tl-wr802n-v1|\
384 tl-wr810n|\
385 tl-wr841n-v11|\
386 tl-wr841n-v1|\
387 tl-wr841n-v7|\
388 tl-wr841n-v8|\
389 tl-wr841n-v9|\
390 tl-wr842n-v2|\
391 tl-wr842n-v3|\
392 tl-wr941nd-v5|\
393 tl-wr941nd-v6|\
394 tl-wr941nd)
395 local magic_ver="0100"
396
397 case "$board" in
398 tl-wdr6500-v2)
399 magic_ver="0200"
400 ;;
401 esac
402
403 [ "$magic" != "$magic_ver" ] && {
404 echo "Invalid image type."
405 return 1
406 }
407
408 local hwid
409 local mid
410 local imagehwid
411 local imagemid
412
413 hwid=$(tplink_get_hwid)
414 mid=$(tplink_get_mid)
415 imagehwid=$(tplink_get_image_hwid "$1")
416 imagemid=$(tplink_get_image_mid "$1")
417
418 [ "$hwid" != "$imagehwid" -o "$mid" != "$imagemid" ] && {
419 echo "Invalid image, hardware ID mismatch, hw:$hwid $mid image:$imagehwid $imagemid."
420 return 1
421 }
422
423 local boot_size
424
425 boot_size=$(tplink_get_image_boot_size "$1")
426 [ "$boot_size" != "00000000" ] && {
427 echo "Invalid image, it contains a bootloader."
428 return 1
429 }
430
431 return 0
432 ;;
433 bsb|\
434 dir-825-b1|\
435 tew-673gru)
436 dir825b_check_image "$1" && return 0
437 ;;
438 c-60|\
439 nbg6716|\
440 r6100|\
441 wndr3700v4|\
442 wndr4300)
443 nand_do_platform_check $board $1
444 return $?
445 ;;
446 cpe210|\
447 cpe510|\
448 eap120)
449 tplink_pharos_check_image "$1" && return 0
450 return 1
451 ;;
452 mr1750v2|\
453 mr1750|\
454 mr600v2|\
455 mr600|\
456 mr900v2|\
457 mr900|\
458 om2p-hsv2|\
459 om2p-hsv3|\
460 om2p-hs|\
461 om2p-lc|\
462 om2pv2|\
463 om2p|\
464 om5p-acv2|\
465 om5p-ac|\
466 om5p-an|\
467 om5p)
468 platform_check_image_openmesh "$magic_long" "$1" && return 0
469 return 1
470 ;;
471 mr18|\
472 z1)
473 merakinand_do_platform_check $board $1
474 return $?
475 ;;
476 dir-869-a1|\
477 mynet-n600|\
478 mynet-n750|\
479 qihoo-c301)
480 [ "$magic_long" != "5ea3a417" ] && {
481 echo "Invalid image, bad magic: $magic_long"
482 return 1
483 }
484
485 local typemagic=$(seama_get_type_magic "$1")
486 [ "$typemagic" != "6669726d" ] && {
487 echo "Invalid image, bad type: $typemagic"
488 return 1
489 }
490
491 return 0
492 ;;
493 mynet-rext|\
494 wrt160nl)
495 cybertan_check_image "$1" && return 0
496 return 1
497 ;;
498 nbg6616|\
499 uap-pro|\
500 unifi-outdoor-plus)
501 [ "$magic_long" != "19852003" ] && {
502 echo "Invalid image type."
503 return 1
504 }
505
506 return 0
507 ;;
508 tube2h)
509 alfa_check_image "$1" && return 0
510 return 1
511 ;;
512 wndr3700|\
513 wnr1000-v2|\
514 wnr2000-v3|\
515 wnr612-v2|\
516 wpn824n)
517 local hw_magic
518
519 hw_magic="$(ar71xx_get_mtd_part_magic firmware)"
520 [ "$magic_long" != "$hw_magic" ] && {
521 echo "Invalid image, hardware ID mismatch, hw:$hw_magic image:$magic_long."
522 return 1
523 }
524
525 return 0
526 ;;
527 wnr2000-v4)
528 [ "$magic_long" != "32303034" ] && {
529 echo "Invalid image type."
530 return 1
531 }
532
533 return 0
534 ;;
535 wnr2200)
536 [ "$magic_long" != "32323030" ] && {
537 echo "Invalid image type."
538 return 1
539 }
540
541 return 0
542 ;;
543 dap-2695-a1)
544 local magic=$(wrgg_get_image_magic "$1")
545 [ "$magic" != "21030820" ] && {
546 echo "Invalid image, bad type: $magic"
547 return 1
548 }
549
550 return 0;
551 ;;
552 esac
553
554 echo "Sysupgrade is not yet supported on $board."
555 return 1
556 }
557
558 platform_pre_upgrade() {
559 local board=$(ar71xx_board_name)
560
561 case "$board" in
562 c-60|\
563 nbg6716|\
564 r6100|\
565 wndr3700v4|\
566 wndr4300)
567 nand_do_upgrade "$1"
568 ;;
569 mr18|\
570 z1)
571 merakinand_do_upgrade "$1"
572 ;;
573 esac
574 }
575
576 platform_do_upgrade() {
577 local board=$(ar71xx_board_name)
578
579 case "$board" in
580 all0258n)
581 platform_do_upgrade_allnet "0x9f050000" "$ARGV"
582 ;;
583 all0305|\
584 eap7660d|\
585 ja76pf2|\
586 ja76pf|\
587 jwap003|\
588 ls-sr71|\
589 pb42|\
590 pb44|\
591 routerstation-pro|\
592 routerstation)
593 platform_do_upgrade_combined "$ARGV"
594 ;;
595 all0315n)
596 platform_do_upgrade_allnet "0x9f080000" "$ARGV"
597 ;;
598 cap4200ag|\
599 eap300v2)
600 platform_do_upgrade_allnet "0xbf0a0000" "$ARGV"
601 ;;
602 dir-825-b1|\
603 tew-673gru)
604 platform_do_upgrade_dir825b "$ARGV"
605 ;;
606 mr1750v2|\
607 mr1750|\
608 mr600v2|\
609 mr600|\
610 mr900v2|\
611 mr900|\
612 om2p-hsv2|\
613 om2p-hsv3|\
614 om2p-hs|\
615 om2p-lc|\
616 om2pv2|\
617 om2p|\
618 om5p-acv2|\
619 om5p-ac|\
620 om5p-an|\
621 om5p)
622 platform_do_upgrade_openmesh "$ARGV"
623 ;;
624 uap-pro|\
625 unifi-outdoor-plus)
626 MTD_CONFIG_ARGS="-s 0x180000"
627 default_do_upgrade "$ARGV"
628 ;;
629 wp543|\
630 wpe72)
631 platform_do_upgrade_compex "$ARGV"
632 ;;
633 *)
634 default_do_upgrade "$ARGV"
635 ;;
636 esac
637 }
638
639 disable_watchdog() {
640 killall watchdog
641 ( ps | grep -v 'grep' | grep '/dev/watchdog' ) && {
642 echo 'Could not disable watchdog'
643 return 1
644 }
645 }
646
647 append sysupgrade_pre_upgrade disable_watchdog