2 # Copyright (C) 2011 OpenWrt.org
5 . /lib/functions/system.sh
9 RAMFS_COPY_DATA=/lib/ar71xx.sh
10 RAMFS_COPY_BIN='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_check_image() {
187 local board=$(board_name)
188 local magic="$(get_magic_word "$1")"
189 local magic_long="$(get_magic_long "$1")"
191 [ "$#" -gt 1 ] && return 1
307 [ "$magic" != "2705" ] && {
308 echo "Invalid image type."
338 [ "$magic_long" != "68737173" -a "$magic_long" != "19852003" ] && {
339 echo "Invalid image type."
351 platform_check_image_allnet "$1" && return 0
367 [ "$magic" != "4349" ] && {
368 echo "Invalid image. Use *-sysupgrade.bin files on this board"
372 local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
373 local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
375 if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
378 echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
403 tellstick-znet-lite|\
467 local magic_ver="0100"
475 [ "$magic" != "$magic_ver" ] && {
476 echo "Invalid image type."
485 hwid=$(tplink_get_hwid)
486 mid=$(tplink_get_mid)
487 imagehwid=$(tplink_get_image_hwid "$1")
488 imagemid=$(tplink_get_image_mid "$1")
490 [ "$hwid" != "$imagehwid" -o "$mid" != "$imagemid" ] && {
491 echo "Invalid image, hardware ID mismatch, hw:$hwid $mid image:$imagehwid $imagemid."
497 boot_size=$(tplink_get_image_boot_size "$1")
498 [ "$boot_size" != "00000000" ] && {
499 echo "Invalid image, it contains a bootloader."
508 dir825b_check_image "$1" && return 0
539 nand_do_platform_check routerboard $1
550 nand_do_platform_check $board $1
558 tplink_pharos_check_image "$1" && return 0
581 platform_check_image_openmesh "$magic_long" "$1" && return 0
586 merakinand_do_platform_check $board $1
593 [ "$magic_long" != "5ea3a417" ] && {
594 echo "Invalid image, bad magic: $magic_long"
598 local typemagic=$(seama_get_type_magic "$1")
599 [ "$typemagic" != "6669726d" ] && {
600 echo "Invalid image, bad type: $typemagic"
609 cybertan_check_image "$1" && return 0
615 [ "$magic_long" != "19852003" ] && {
616 echo "Invalid image type."
623 alfa_check_image "$1" && return 0
633 hw_magic="$(ar71xx_get_mtd_part_magic firmware)"
634 [ "$magic_long" != "$hw_magic" ] && {
635 echo "Invalid image, hardware ID mismatch, hw:$hw_magic image:$magic_long."
642 [ "$magic_long" != "32303034" ] && {
643 echo "Invalid image type."
650 [ "$magic_long" != "32323030" ] && {
651 echo "Invalid image type."
658 local magic=$(wrgg_get_image_magic "$1")
659 [ "$magic" != "21030820" ] && {
660 echo "Invalid image, bad type: $magic"
666 # these boards use metadata images
676 rb-962uigs-5hact2hnt|\
686 echo "Sysupgrade is not yet supported on $board."
690 platform_pre_upgrade() {
691 local board=$(board_name)
702 rb-962uigs-5hact2hnt|\
708 # erase firmware if booted from initramfs
709 [ -z "$(rootfs_type)" ] && mtd erase firmware
714 platform_nand_pre_upgrade() {
715 local board=$(board_name)
720 local fw_mtd=$(find_mtd_part kernel)
721 fw_mtd="${fw_mtd/block/}"
722 [ -n "$fw_mtd" ] || return
724 tar xf "$1" sysupgrade-routerboard/kernel -O | nandwrite -o "$fw_mtd" -
727 case "$(fw_printenv -n dualPartition)" in
729 fw_setenv dualPartition imgB
730 fw_setenv ActImg NokiaImageB
733 fw_setenv dualPartition imgA
734 fw_setenv ActImg NokiaImageA
737 ubiblock -r /dev/ubiblock0_0 2>/dev/null >/dev/null
738 rm -f /dev/ubiblock0_0
739 ubidetach -d 0 2>/dev/null >/dev/null
741 CI_KERNPART=kernel_alt
746 platform_do_upgrade() {
747 local board=$(board_name)
751 platform_do_upgrade_allnet "0x9f050000" "$ARGV"
763 platform_do_upgrade_combined "$ARGV"
766 platform_do_upgrade_allnet "0x9f080000" "$ARGV"
771 platform_do_upgrade_allnet "0xbf0a0000" "$ARGV"
775 platform_do_upgrade_dir825b "$ARGV"
797 platform_do_upgrade_openmesh "$ARGV"
840 merakinand_do_upgrade "$1"
844 MTD_CONFIG_ARGS="-s 0x180000"
845 default_do_upgrade "$ARGV"
849 platform_do_upgrade_compex "$ARGV"
852 default_do_upgrade "$ARGV"