otrx: change command line API to start with a mode
[openwrt/staging/lynxis.git] / target / linux / bcm53xx / base-files / lib / upgrade / platform.sh
index 17456b6cdab24deedac7258358d6f474f768e3cf..876da4d6f809faa37430a16ef70fda16ca3bede5 100644 (file)
@@ -64,6 +64,7 @@ platform_check_image() {
 
        local file_type=$(platform_identify "$1")
        local magic
+       local error=0
 
        case "$file_type" in
                "chk")
@@ -75,16 +76,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 +91,80 @@ 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/$1.trx"; platform_extract_trx_from_chk "$1" "$trx";;
+               "cybertan")     trx="/tmp/$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
+
+       echo "Provided firmware contains kernel and UBI image, but flashing it is unsupported yet"
+       exit 1
 }
 
 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/$1.trx"; platform_extract_trx_from_chk "$1" "$trx";;
+               "cybertan")     trx="/tmp/$1.trx"; platform_extract_trx_from_cybertan "$1" "$trx";;
        esac
+
+       shift
+       default_do_upgrade "$trx" "$@"
 }