bcm53xx: sysupgrade: allow flashing NAND devices without --force
[openwrt.git] / target / linux / bcm53xx / base-files / lib / upgrade / platform.sh
index 17456b6..d3440b7 100644 (file)
@@ -57,13 +57,9 @@ platform_identify() {
 platform_check_image() {
        [ "$#" -gt 1 ] && return 1
 
-       [ "$(platform_flash_type)" = "nand" ] && {
-               echo "Firmware upgrade on NAND devices is not implemented."
-               return 1
-       }
-
        local file_type=$(platform_identify "$1")
        local magic
+       local error=0
 
        case "$file_type" in
                "chk")
@@ -75,16 +71,13 @@ platform_check_image() {
 
                        [ -n "$dev_board_id" -a "chk $board_id" != "$dev_board_id" ] && {
                                echo "Firmware board_id doesn't match device board_id ($dev_board_id)"
-                               return 1
+                               error=1
                        }
 
-                       magic=$(get_magic_long_at "$1" "$header_len")
-                       [ "$magic" != "48445230" ] && {
+                       if ! otrx check "$1" -o "$header_len"; then
                                echo "No valid TRX firmware in the CHK image"
-                               return 1
-                       }
-
-                       return 0
+                               error=1
+                       fi
                ;;
                "cybertan")
                        local pattern=$(dd if="$1" bs=1 count=4 2>/dev/null | hexdump -v -e '1/1 "%c"')
@@ -93,65 +86,103 @@ platform_check_image() {
 
                        [ -n "$dev_pattern" -a "cybertan $pattern" != "$dev_pattern" ] && {
                                echo "Firmware pattern doesn't match device pattern ($dev_pattern)"
-                               return 1
+                               error=1
                        }
 
-                       magic=$(get_magic_long_at "$1" 32)
-                       [ "$magic" != "48445230" ] && {
+                       if ! otrx check "$1" -o 32; then
                                echo "No valid TRX firmware in the CyberTAN image"
-                               return 1
-                       }
-
-                       return 0
+                               error=1
+                       fi
                ;;
                "trx")
-                       return 0
+                       if ! otrx check "$1"; then
+                               echo "Invalid (corrupted?) TRX firmware"
+                               error=1
+                       fi
                ;;
                *)
                        echo "Invalid image type. Please use only .trx files"
-                       return 1
+                       error=1
                ;;
        esac
+
+       return $error
 }
 
-# Extract TRX and use stadard upgrade method
-platform_do_upgrade_chk() {
+platform_extract_trx_from_chk() {
        local header_len=$((0x$(get_magic_long_at "$1" 4)))
-       local trx="/tmp/$1.trx"
 
-       dd if="$1" of="$trx" bs=$header_len skip=1
-       shift
-       platform_do_upgrade_trx "$trx" "$@"
+       dd if="$1" of="$2" bs=$header_len skip=1
 }
 
-# Extract TRX and use stadard upgrade method
-platform_do_upgrade_cybertan() {
-       local trx="/tmp/$1.trx"
-
-       dd if="$1" of="$trx" bs=32 skip=1
-       shift
-       platform_do_upgrade_trx "$trx" "$@"
+platform_extract_trx_from_cybertan() {
+       dd if="$1" of="$2" bs=32 skip=1
 }
 
-platform_do_upgrade_trx() {
-       local flash_type=$(platform_flash_type)
+platform_pre_upgrade() {
+       local file_type=$(platform_identify "$1")
+       local dir="/tmp/sysupgrade-bcm53xx"
+       local trx="$1"
 
-       case "$flash_type" in
-               "serial")
-                       default_do_upgrade "$@"
-               ;;
-               "nand")
-                       echo "Firmware upgrade on NAND devices is REALLY unsupported."
-               ;;
+       [ "$(platform_flash_type)" != "nand" ] && return
+
+       # Extract trx
+       case "$file_type" in
+               "chk")          trx="/tmp/$(basename $1).trx"; platform_extract_trx_from_chk "$1" "$trx";;
+               "cybertan")     trx="/tmp/$(basename $1).trx"; platform_extract_trx_from_cybertan "$1" "$trx";;
        esac
+
+       # Extract partitions from trx
+       rm -fR $dir
+       mkdir -p $dir
+       otrx extract "$trx" \
+               -1 $dir/kernel \
+               -2 $dir/root
+
+       # Firmwares without UBI image should be flashed "normally"
+       local root_type=$(identify $dir/root)
+       [ "$root_type" != "ubi" ] && return
+
+       # Prepare TRX file with just a kernel that will replace current one
+       local linux_length=$(grep "\"linux\"" /proc/mtd | sed "s/mtd[0-9]*:[ \t]*\([^ \t]*\).*/\1/")
+       [ -z "$linux_length" ] && {
+               echo "Unable to find \"linux\" partition size"
+               exit 1
+       }
+       linux_length=$((0x$linux_length + 28))
+       rm -f /tmp/null.bin
+       rm -f /tmp/kernel.trx
+       touch /tmp/null.bin
+       otrx create /tmp/kernel.trx \
+               -f $dir/kernel -b $linux_length \
+               -f /tmp/null.bin
+
+       # Prepare UBI image (drop unwanted extra blocks)
+       local ubi_length=0
+       while [ "$(dd if=$dir/root skip=$ubi_length bs=1 count=4 2>/dev/null)" = "UBI#" ]; do
+               ubi_length=$(($ubi_length + 131072))
+       done
+       echo "ubi_length: $ubi_length"
+       dd if=$dir/root of=/tmp/root.ubi bs=131072 count=$((ubi_length / 131072)) 2>/dev/null
+
+       # Flash
+       mtd write /tmp/kernel.trx firmware
+       nand_do_upgrade /tmp/root.ubi
 }
 
 platform_do_upgrade() {
        local file_type=$(platform_identify "$1")
+       local trx="$1"
+
+       [ "$(platform_flash_type)" == "nand" ] && {
+               echo "Flashing firmware without UBI for rootfs. All erase counters will be lost."
+       }
 
        case "$file_type" in
-               "chk")          platform_do_upgrade_chk "$ARGV";;
-               "cybertan")     platform_do_upgrade_cybertan "$ARGV";;
-               *)              platform_do_upgrade_trx "$ARGV";;
+               "chk")          trx="/tmp/$(basename $1).trx"; platform_extract_trx_from_chk "$1" "$trx";;
+               "cybertan")     trx="/tmp/$(basename $1).trx"; platform_extract_trx_from_cybertan "$1" "$trx";;
        esac
+
+       shift
+       default_do_upgrade "$trx" "$@"
 }