2 # Copyright (C) 2011 OpenWrt.org
5 . /lib/functions/system.sh
9 RAMFS_COPY_DATA=/lib/ar71xx.sh
14 platform_find_partitions() {
15 local first dev size erasesize name
16 while read dev size erasesize name; do
17 name=${name#'"'}; name=${name%'"'}
19 vmlinux.bin.l7|vmlinux|kernel|linux|linux.bin|rootfs|filesystem)
20 if [ -z "$first" ]; then
23 echo "$erasesize:$first:$name"
31 platform_find_kernelpart() {
33 for part in "${1%:*}" "${1#*:}"; do
35 vmlinux.bin.l7|vmlinux|kernel|linux|linux.bin)
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))
51 if [ -n "$partitions" ] && [ -n "$kernelpart" ] && \
52 [ ${kern_blocks:-0} -gt 0 ] && \
53 [ ${root_blocks:-0} -gt 0 ] && \
54 [ ${erase_size:-0} -gt 0 ];
57 [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
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
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"'
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"'
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"'
77 tplink_pharos_check_image() {
78 local magic_long="$(get_magic_long "$1")"
79 [ "$magic_long" != "7f454c46" ] && {
80 echo "Invalid image magic '$magic_long'"
84 local model_string="$(tplink_pharos_get_model_string)"
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)
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
94 dd if="$1" bs=1 skip=1511432 count=1024 2>/dev/null | while read line; do
95 [ "$line" == "$model_string" ] && break
97 echo "Unsupported image (model not in support-list)"
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"'
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"'
112 cybertan_check_image() {
113 local magic="$(cybertan_get_image_magic "$1")"
114 local fw_magic="$(cybertan_get_hw_magic)"
116 [ "$fw_magic" != "$magic" ] && {
117 echo "Invalid image, ID mismatch, got:$magic, but need:$fw_magic"
124 platform_do_upgrade_compex() {
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))
131 if [ -n "$fw_mtd" ] && [ ${fw_blocks:-0} -gt 0 ]; then
133 [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
136 dd if="$fw_file" bs=64k skip=1 count=$fw_blocks 2>/dev/null | \
137 mtd $append write - "$fw_part"
142 local magic_long="$(get_magic_long "$1")"
143 local fw_part_size=$(mtd_get_part_size firmware)
145 case "$magic_long" in
147 [ "$fw_part_size" != "16318464" ] && {
148 echo "Invalid image magic \"$magic_long\" for $fw_part_size bytes"
154 [ "$fw_part_size" != "7929856" ] && {
155 echo "Invalid image magic \"$magic_long\" for $fw_part_size bytes"
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")"
169 [ "$#" -gt 1 ] && return 1
178 platform_check_image_allnet "$1" && return 0
203 [ "$magic_long" != "68737173" -a "$magic_long" != "19852003" ] && {
204 echo "Invalid image type."
269 nbg460n_550n_550nh | \
276 [ "$magic" != "2705" ] && {
277 echo "Invalid image type."
284 tplink_pharos_check_image "$1" && return 0
291 dir825b_check_image "$1" && return 0
296 cybertan_check_image "$1" && return 0
303 [ "$magic_long" != "5ea3a417" ] && {
304 echo "Invalid image, bad magic: $magic_long"
308 local typemagic=$(seama_get_type_magic "$1")
309 [ "$typemagic" != "6669726d" ] && {
310 echo "Invalid image, bad type: $typemagic"
330 platform_check_image_openmesh "$magic_long" "$1" && return 0
349 tellstick-znet-lite | \
397 local magic_ver="0100"
405 [ "$magic" != "$magic_ver" ] && {
406 echo "Invalid image type."
415 hwid=$(tplink_get_hwid)
416 mid=$(tplink_get_mid)
417 imagehwid=$(tplink_get_image_hwid "$1")
418 imagemid=$(tplink_get_image_mid "$1")
420 [ "$hwid" != "$imagehwid" -o "$mid" != "$imagemid" ] && {
421 echo "Invalid image, hardware ID mismatch, hw:$hwid $mid image:$imagehwid $imagemid."
427 boot_size=$(tplink_get_image_boot_size "$1")
428 [ "$boot_size" != "00000000" ] && {
429 echo "Invalid image, it contains a bootloader."
437 alfa_check_image "$1" && return 0
442 unifi-outdoor-plus | \
444 [ "$magic_long" != "19852003" ] && {
445 echo "Invalid image type."
457 hw_magic="$(ar71xx_get_mtd_part_magic firmware)"
458 [ "$magic_long" != "$hw_magic" ] && {
459 echo "Invalid image, hardware ID mismatch, hw:$hw_magic image:$magic_long."
465 merakinand_do_platform_check $board $1
472 nand_do_platform_check $board $1
476 routerstation-pro | \
488 [ "$magic" != "4349" ] && {
489 echo "Invalid image. Use *-sysupgrade.bin files on this board"
493 local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
494 local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
496 if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
499 echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
505 [ "$magic_long" != "32303034" ] && {
506 echo "Invalid image type."
512 [ "$magic_long" != "32323030" ] && {
513 echo "Invalid image type."
521 echo "Sysupgrade is not yet supported on $board."
525 platform_pre_upgrade() {
526 local board=$(ar71xx_board_name)
536 merakinand_do_upgrade "$1"
541 platform_do_upgrade() {
542 local board=$(ar71xx_board_name)
546 routerstation-pro | \
555 platform_do_upgrade_combined "$ARGV"
559 platform_do_upgrade_compex "$ARGV"
562 platform_do_upgrade_allnet "0x9f050000" "$ARGV"
565 platform_do_upgrade_allnet "0x9f080000" "$ARGV"
569 platform_do_upgrade_allnet "0xbf0a0000" "$ARGV"
573 platform_do_upgrade_dir825b "$ARGV"
589 platform_do_upgrade_openmesh "$ARGV"
591 unifi-outdoor-plus | \
593 MTD_CONFIG_ARGS="-s 0x180000"
594 default_do_upgrade "$ARGV"
597 default_do_upgrade "$ARGV"
604 ( ps | grep -v 'grep' | grep '/dev/watchdog' ) && {
605 echo 'Could not disable watchdog'
610 append sysupgrade_pre_upgrade disable_watchdog