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