ar71xx: add support for OpenEmbed SOM9331
[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 wpj342 | \
259 wpj344 | \
260 wzr-hp-g300nh2 | \
261 wzr-hp-g300nh | \
262 wzr-hp-g450h | \
263 wzr-hp-ag300h | \
264 wzr-450hp2 | \
265 whr-g301n | \
266 whr-hp-g300n | \
267 whr-hp-gn | \
268 wlae-ag300n | \
269 nbg460n_550n_550nh | \
270 unifi | \
271 unifiac-lite | \
272 unifiac-pro | \
273 unifi-outdoor | \
274 carambola2 | \
275 weio | \
276 wrtnode2q)
277 [ "$magic" != "2705" ] && {
278 echo "Invalid image type."
279 return 1
280 }
281 return 0
282 ;;
283
284 cpe210|\
285 cpe510)
286 tplink_pharos_check_image "$1" && return 0
287 return 1
288 ;;
289
290 bsb | \
291 dir-825-b1 | \
292 tew-673gru)
293 dir825b_check_image "$1" && return 0
294 ;;
295
296 mynet-rext|\
297 wrt160nl)
298 cybertan_check_image "$1" && return 0
299 return 1
300 ;;
301
302 qihoo-c301 | \
303 mynet-n600 | \
304 mynet-n750)
305 [ "$magic_long" != "5ea3a417" ] && {
306 echo "Invalid image, bad magic: $magic_long"
307 return 1
308 }
309
310 local typemagic=$(seama_get_type_magic "$1")
311 [ "$typemagic" != "6669726d" ] && {
312 echo "Invalid image, bad type: $typemagic"
313 return 1
314 }
315
316 return 0;
317 ;;
318 mr1750 | \
319 mr1750v2 | \
320 mr600 | \
321 mr600v2 | \
322 mr900 | \
323 mr900v2 | \
324 om2p | \
325 om2pv2 | \
326 om2p-hs | \
327 om2p-hsv2 | \
328 om2p-hsv3 | \
329 om2p-lc | \
330 om5p | \
331 om5p-an | \
332 om5p-ac | \
333 om5p-acv2)
334 platform_check_image_openmesh "$magic_long" "$1" && return 0
335 return 1
336 ;;
337
338 antminer-s1 | \
339 antminer-s3 | \
340 antrouter-r1 | \
341 archer-c5 | \
342 archer-c7 | \
343 el-m150 | \
344 el-mini | \
345 gl-inet | \
346 mc-mac1200r | \
347 minibox-v1 |\
348 omy-g1 |\
349 omy-x1 |\
350 onion-omega | \
351 oolite | \
352 smart-300 | \
353 som9331 | \
354 tellstick-znet-lite | \
355 tl-mr10u | \
356 tl-mr11u | \
357 tl-mr12u | \
358 tl-mr13u | \
359 tl-mr3020 | \
360 tl-mr3040 | \
361 tl-mr3040-v2 | \
362 tl-mr3220 | \
363 tl-mr3220-v2 | \
364 tl-mr3420 | \
365 tl-mr3420-v2 | \
366 tl-wa701nd-v2 | \
367 tl-wa7210n-v2 | \
368 tl-wa7510n | \
369 tl-wa750re | \
370 tl-wa850re | \
371 tl-wa860re | \
372 tl-wa801nd-v2 | \
373 tl-wa901nd | \
374 tl-wa901nd-v2 | \
375 tl-wa901nd-v3 | \
376 tl-wa901nd-v4 | \
377 tl-wdr3320-v2 | \
378 tl-wdr3500 | \
379 tl-wdr4300 | \
380 tl-wdr4900-v2 | \
381 tl-wdr6500-v2 | \
382 tl-wr703n | \
383 tl-wr710n | \
384 tl-wr720n-v3 | \
385 tl-wr741nd | \
386 tl-wr741nd-v4 | \
387 tl-wr810n | \
388 tl-wr841n-v1 | \
389 tl-wa830re-v2 | \
390 tl-wr841n-v7 | \
391 tl-wr841n-v8 | \
392 tl-wr841n-v9 | \
393 tl-wr841n-v11 | \
394 tl-wr842n-v2 | \
395 tl-wr842n-v3 | \
396 tl-wr941nd | \
397 tl-wr941nd-v5 | \
398 tl-wr941nd-v6 | \
399 tl-wr1041n-v2 | \
400 tl-wr1043nd | \
401 tl-wr1043nd-v2 | \
402 tl-wr2543n)
403 local magic_ver="0100"
404
405 case "$board" in
406 tl-wdr6500-v2)
407 magic_ver="0200"
408 ;;
409 esac
410
411 [ "$magic" != "$magic_ver" ] && {
412 echo "Invalid image type."
413 return 1
414 }
415
416 local hwid
417 local mid
418 local imagehwid
419 local imagemid
420
421 hwid=$(tplink_get_hwid)
422 mid=$(tplink_get_mid)
423 imagehwid=$(tplink_get_image_hwid "$1")
424 imagemid=$(tplink_get_image_mid "$1")
425
426 [ "$hwid" != "$imagehwid" -o "$mid" != "$imagemid" ] && {
427 echo "Invalid image, hardware ID mismatch, hw:$hwid $mid image:$imagehwid $imagemid."
428 return 1
429 }
430
431 local boot_size
432
433 boot_size=$(tplink_get_image_boot_size "$1")
434 [ "$boot_size" != "00000000" ] && {
435 echo "Invalid image, it contains a bootloader."
436 return 1
437 }
438
439 return 0
440 ;;
441
442 tube2h)
443 alfa_check_image "$1" && return 0
444 return 1
445 ;;
446
447 nbg6616 | \
448 unifi-outdoor-plus | \
449 uap-pro)
450 [ "$magic_long" != "19852003" ] && {
451 echo "Invalid image type."
452 return 1
453 }
454 return 0
455 ;;
456 wndr3700 | \
457 wnr2000-v3 | \
458 wnr612-v2 | \
459 wnr1000-v2 | \
460 wpn824n)
461 local hw_magic
462
463 hw_magic="$(ar71xx_get_mtd_part_magic firmware)"
464 [ "$magic_long" != "$hw_magic" ] && {
465 echo "Invalid image, hardware ID mismatch, hw:$hw_magic image:$magic_long."
466 return 1
467 }
468 return 0
469 ;;
470 mr18)
471 merakinand_do_platform_check $board $1
472 return $?;
473 ;;
474 nbg6716 | \
475 r6100 | \
476 wndr3700v4 | \
477 wndr4300 )
478 nand_do_platform_check $board $1
479 return $?;
480 ;;
481 routerstation | \
482 routerstation-pro | \
483 ls-sr71 | \
484 pb42 | \
485 pb44 | \
486 all0305 | \
487 eap300v2 | \
488 eap7660d | \
489 ja76pf | \
490 ja76pf2 | \
491 jwap003 | \
492 wp543 | \
493 wpe72)
494 [ "$magic" != "4349" ] && {
495 echo "Invalid image. Use *-sysupgrade.bin files on this board"
496 return 1
497 }
498
499 local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
500 local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
501
502 if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
503 return 0
504 else
505 echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
506 return 1
507 fi
508 return 0
509 ;;
510 wnr2000-v4)
511 [ "$magic_long" != "32303034" ] && {
512 echo "Invalid image type."
513 return 1
514 }
515 return 0
516 ;;
517 wnr2200)
518 [ "$magic_long" != "32323030" ] && {
519 echo "Invalid image type."
520 return 1
521 }
522 return 0
523 ;;
524
525 esac
526
527 echo "Sysupgrade is not yet supported on $board."
528 return 1
529 }
530
531 platform_pre_upgrade() {
532 local board=$(ar71xx_board_name)
533
534 case "$board" in
535 nbg6716 | \
536 r6100 | \
537 wndr3700v4 | \
538 wndr4300 )
539 nand_do_upgrade "$1"
540 ;;
541 mr18)
542 merakinand_do_upgrade "$1"
543 ;;
544 esac
545 }
546
547 platform_do_upgrade() {
548 local board=$(ar71xx_board_name)
549
550 case "$board" in
551 routerstation | \
552 routerstation-pro | \
553 ls-sr71 | \
554 all0305 | \
555 eap7660d | \
556 pb42 | \
557 pb44 | \
558 ja76pf | \
559 ja76pf2 | \
560 jwap003)
561 platform_do_upgrade_combined "$ARGV"
562 ;;
563 wp543|\
564 wpe72)
565 platform_do_upgrade_compex "$ARGV"
566 ;;
567 all0258n )
568 platform_do_upgrade_allnet "0x9f050000" "$ARGV"
569 ;;
570 all0315n )
571 platform_do_upgrade_allnet "0x9f080000" "$ARGV"
572 ;;
573 eap300v2 |\
574 cap4200ag)
575 platform_do_upgrade_allnet "0xbf0a0000" "$ARGV"
576 ;;
577 dir-825-b1 |\
578 tew-673gru)
579 platform_do_upgrade_dir825b "$ARGV"
580 ;;
581 mr1750 | \
582 mr1750v2 | \
583 mr600 | \
584 mr600v2 | \
585 mr900 | \
586 mr900v2 | \
587 om2p | \
588 om2pv2 | \
589 om2p-hs | \
590 om2p-hsv2 | \
591 om2p-hsv3 | \
592 om2p-lc | \
593 om5p | \
594 om5p-an | \
595 om5p-ac | \
596 om5p-acv2)
597 platform_do_upgrade_openmesh "$ARGV"
598 ;;
599 unifi-outdoor-plus | \
600 uap-pro)
601 MTD_CONFIG_ARGS="-s 0x180000"
602 default_do_upgrade "$ARGV"
603 ;;
604 *)
605 default_do_upgrade "$ARGV"
606 ;;
607 esac
608 }
609
610 disable_watchdog() {
611 killall watchdog
612 ( ps | grep -v 'grep' | grep '/dev/watchdog' ) && {
613 echo 'Could not disable watchdog'
614 return 1
615 }
616 }
617
618 append sysupgrade_pre_upgrade disable_watchdog