2 # Copyright (C) 2011 OpenWrt.org
5 . /lib/functions/system.sh
9 RAMFS_COPY_DATA=/lib/ar71xx.sh
10 [ -x /usr/sbin/nandwrite ] && RAMFS_COPY_BIN=/usr/sbin/nandwrite
15 PLATFORM_DO_UPGRADE_COMBINED_SEPARATE_MTD=0
17 platform_find_partitions() {
18 local first dev size erasesize name
19 while read dev size erasesize name; do
20 name=${name#'"'}; name=${name%'"'}
22 vmlinux.bin.l7|vmlinux|kernel|linux|linux.bin|rootfs|filesystem)
23 if [ -z "$first" ]; then
26 echo "$erasesize:$first:$name"
34 platform_find_kernelpart() {
36 for part in "${1%:*}" "${1#*:}"; do
38 vmlinux.bin.l7|vmlinux|kernel|linux|linux.bin)
46 platform_find_rootfspart() {
48 for part in "${1%:*}" "${1#*:}"; do
49 [ "$part" != "$2" ] && echo "$part" && break
53 platform_do_upgrade_combined() {
54 local partitions=$(platform_find_partitions)
55 local kernelpart=$(platform_find_kernelpart "${partitions#*:}")
56 local erase_size=$((0x${partitions%%:*})); partitions="${partitions#*:}"
57 local kern_length=0x$(dd if="$1" bs=2 skip=1 count=4 2>/dev/null)
58 local kern_blocks=$(($kern_length / $CI_BLKSZ))
59 local root_blocks=$((0x$(dd if="$1" bs=2 skip=5 count=4 2>/dev/null) / $CI_BLKSZ))
61 if [ -n "$partitions" ] && [ -n "$kernelpart" ] && \
62 [ ${kern_blocks:-0} -gt 0 ] && \
63 [ ${root_blocks:-0} -gt 0 ] && \
64 [ ${erase_size:-0} -gt 0 ];
66 local rootfspart=$(platform_find_rootfspart "$partitions" "$kernelpart")
68 [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
70 if [ "$PLATFORM_DO_UPGRADE_COMBINED_SEPARATE_MTD" -ne 1 ]; then
71 ( dd if="$1" bs=$CI_BLKSZ skip=1 count=$kern_blocks 2>/dev/null; \
72 dd if="$1" bs=$CI_BLKSZ skip=$((1+$kern_blocks)) count=$root_blocks 2>/dev/null ) | \
73 mtd -r $append -F$kernelpart:$kern_length:$CI_LDADR,rootfs write - $partitions
74 elif [ -n "$rootfspart" ]; then
75 dd if="$1" bs=$CI_BLKSZ skip=1 count=$kern_blocks 2>/dev/null | \
76 mtd write - $kernelpart
77 dd if="$1" bs=$CI_BLKSZ skip=$((1+$kern_blocks)) count=$root_blocks 2>/dev/null | \
78 mtd -r $append write - $rootfspart
81 PLATFORM_DO_UPGRADE_COMBINED_SEPARATE_MTD=0
84 tplink_get_image_hwid() {
85 get_image "$@" | dd bs=4 count=1 skip=16 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
88 tplink_get_image_mid() {
89 get_image "$@" | dd bs=4 count=1 skip=17 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
92 tplink_get_image_boot_size() {
93 get_image "$@" | dd bs=4 count=1 skip=37 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
96 tplink_pharos_check_image() {
97 local magic_long="$(get_magic_long "$1")"
98 [ "$magic_long" != "7f454c46" ] && {
99 echo "Invalid image magic '$magic_long'"
103 local model_string="$(tplink_pharos_get_model_string)"
106 # Here $1 is given to dd directly instead of get_image as otherwise the skip
107 # will take almost a second (as dd can't seek then)
109 # This will fail if the image isn't local, but that's fine: as the
110 # read loop won't be executed at all, it will return true, so the image
111 # is accepted (loading the first 1.5M of a remote image for this check seems
113 dd if="$1" bs=1 skip=1511432 count=1024 2>/dev/null | while read line; do
114 [ "$line" = "$model_string" ] && break
116 echo "Unsupported image (model not in support-list)"
123 seama_get_type_magic() {
124 get_image "$@" | dd bs=1 count=4 skip=53 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
127 wrgg_get_image_magic() {
128 get_image "$@" | dd bs=4 count=1 skip=8 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
131 cybertan_get_image_magic() {
132 get_image "$@" | dd bs=8 count=1 skip=0 2>/dev/null | hexdump -v -n 8 -e '1/1 "%02x"'
135 cybertan_check_image() {
136 local magic="$(cybertan_get_image_magic "$1")"
137 local fw_magic="$(cybertan_get_hw_magic)"
139 [ "$fw_magic" != "$magic" ] && {
140 echo "Invalid image, ID mismatch, got:$magic, but need:$fw_magic"
147 platform_do_upgrade_compex() {
149 local fw_part=$PART_NAME
150 local fw_mtd=$(find_mtd_part $fw_part)
151 local fw_length=0x$(dd if="$fw_file" bs=2 skip=1 count=4 2>/dev/null)
152 local fw_blocks=$(($fw_length / 65536))
154 if [ -n "$fw_mtd" ] && [ ${fw_blocks:-0} -gt 0 ]; then
156 [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
159 dd if="$fw_file" bs=64k skip=1 count=$fw_blocks 2>/dev/null | \
160 mtd $append write - "$fw_part"
165 local magic_long="$(get_magic_long "$1")"
166 local fw_part_size=$(mtd_get_part_size firmware)
168 case "$magic_long" in
170 [ "$fw_part_size" != "16318464" ] && {
171 echo "Invalid image magic \"$magic_long\" for $fw_part_size bytes"
176 [ "$fw_part_size" != "7929856" ] && {
177 echo "Invalid image magic \"$magic_long\" for $fw_part_size bytes"
186 platform_nand_board_name() {
187 local board=$(ar71xx_board_name)
190 rb*) echo "routerboard";;
195 platform_check_image() {
196 local board=$(ar71xx_board_name)
197 local magic="$(get_magic_word "$1")"
198 local magic_long="$(get_magic_long "$1")"
200 [ "$#" -gt 1 ] && return 1
298 [ "$magic" != "2705" ] && {
299 echo "Invalid image type."
329 [ "$magic_long" != "68737173" -a "$magic_long" != "19852003" ] && {
330 echo "Invalid image type."
342 platform_check_image_allnet "$1" && return 0
358 [ "$magic" != "4349" ] && {
359 echo "Invalid image. Use *-sysupgrade.bin files on this board"
363 local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
364 local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
366 if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
369 echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
392 tellstick-znet-lite|\
453 local magic_ver="0100"
461 [ "$magic" != "$magic_ver" ] && {
462 echo "Invalid image type."
471 hwid=$(tplink_get_hwid)
472 mid=$(tplink_get_mid)
473 imagehwid=$(tplink_get_image_hwid "$1")
474 imagemid=$(tplink_get_image_mid "$1")
476 [ "$hwid" != "$imagehwid" -o "$mid" != "$imagemid" ] && {
477 echo "Invalid image, hardware ID mismatch, hw:$hwid $mid image:$imagehwid $imagemid."
483 boot_size=$(tplink_get_image_boot_size "$1")
484 [ "$boot_size" != "00000000" ] && {
485 echo "Invalid image, it contains a bootloader."
494 dir825b_check_image "$1" && return 0
524 nand_do_platform_check routerboard $1
534 nand_do_platform_check $board $1
542 tplink_pharos_check_image "$1" && return 0
565 platform_check_image_openmesh "$magic_long" "$1" && return 0
570 merakinand_do_platform_check $board $1
577 [ "$magic_long" != "5ea3a417" ] && {
578 echo "Invalid image, bad magic: $magic_long"
582 local typemagic=$(seama_get_type_magic "$1")
583 [ "$typemagic" != "6669726d" ] && {
584 echo "Invalid image, bad type: $typemagic"
593 cybertan_check_image "$1" && return 0
599 [ "$magic_long" != "19852003" ] && {
600 echo "Invalid image type."
607 alfa_check_image "$1" && return 0
617 hw_magic="$(ar71xx_get_mtd_part_magic firmware)"
618 [ "$magic_long" != "$hw_magic" ] && {
619 echo "Invalid image, hardware ID mismatch, hw:$hw_magic image:$magic_long."
626 [ "$magic_long" != "32303034" ] && {
627 echo "Invalid image type."
634 [ "$magic_long" != "32323030" ] && {
635 echo "Invalid image type."
642 local magic=$(wrgg_get_image_magic "$1")
643 [ "$magic" != "21030820" ] && {
644 echo "Invalid image, bad type: $magic"
650 # these boards use metadata images
657 rb-962uigs-5hact2hnt|\
664 echo "Sysupgrade is not yet supported on $board."
668 platform_pre_upgrade() {
669 local board=$(ar71xx_board_name)
714 rb-962uigs-5hact2hnt|\
717 # erase firmware if booted from initramfs
718 [ -z "$(rootfs_type)" ] && mtd erase firmware
722 merakinand_do_upgrade "$1"
727 platform_nand_pre_upgrade() {
728 local board=$(ar71xx_board_name)
733 local fw_mtd=$(find_mtd_part kernel)
734 fw_mtd="${fw_mtd/block/}"
735 [ -n "$fw_mtd" ] || return
737 tar xf "$1" sysupgrade-routerboard/kernel -O | nandwrite -o "$fw_mtd" -
742 platform_do_upgrade() {
743 local board=$(ar71xx_board_name)
747 platform_do_upgrade_allnet "0x9f050000" "$ARGV"
759 platform_do_upgrade_combined "$ARGV"
762 platform_do_upgrade_allnet "0x9f080000" "$ARGV"
767 platform_do_upgrade_allnet "0xbf0a0000" "$ARGV"
771 platform_do_upgrade_dir825b "$ARGV"
793 platform_do_upgrade_openmesh "$ARGV"
797 MTD_CONFIG_ARGS="-s 0x180000"
798 default_do_upgrade "$ARGV"
802 platform_do_upgrade_compex "$ARGV"
805 default_do_upgrade "$ARGV"
812 ( ps | grep -v 'grep' | grep '/dev/watchdog' ) && {
813 echo 'Could not disable watchdog'
818 append sysupgrade_pre_upgrade disable_watchdog