base-files: sysupgrade: exit if the firmware download failed
[openwrt/staging/jogo.git] / package / base-files / files / sbin / sysupgrade
index 001e6444760628c350213983a4d0505ac5ef1ec2..6c518b780eddd973966537b50d8f7c82539bb1e1 100755 (executable)
@@ -2,6 +2,7 @@
 
 . /lib/functions.sh
 . /lib/functions/system.sh
+. /usr/share/libubox/jshn.sh
 
 # initialize defaults
 export MTD_ARGS=""
@@ -191,9 +192,6 @@ add_overlayfiles() {
        return 0
 }
 
-# hooks
-sysupgrade_image_check="fwtool_check_signature fwtool_check_image platform_check_image"
-
 if [ $SAVE_OVERLAY = 1 ]; then
        [ ! -d /overlay/upper/etc ] && {
                echo "Cannot find '/overlay/upper/etc', required for '-c'" >&2
@@ -296,7 +294,7 @@ type platform_check_image >/dev/null 2>/dev/null || {
 case "$IMAGE" in
        http://*|\
        https://*)
-               wget -O/tmp/sysupgrade.img "$IMAGE"
+               wget -O/tmp/sysupgrade.img "$IMAGE" || exit 1
                IMAGE=/tmp/sysupgrade.img
                ;;
 esac
@@ -316,17 +314,19 @@ case "$IMAGE" in
                ;;
 esac
 
-for check in $sysupgrade_image_check; do
-       ( $check "$IMAGE" ) || {
-               if [ $FORCE -eq 1 ]; then
-                       echo "Image check '$check' failed but --force given - will update anyway!" >&2
-                       break
-               else
-                       echo "Image check '$check' failed." >&2
-                       exit 1
-               fi
-       }
-done
+json_load "$(/usr/libexec/validate_firmware_image "$IMAGE")" || {
+       echo "Failed to check image"
+       exit 1
+}
+json_get_var valid "valid"
+[ "$valid" -eq 0 ] && {
+       if [ $FORCE -eq 1 ]; then
+               echo "Image check failed but --force given - will update anyway!" >&2
+       else
+               echo "Image check failed." >&2
+               exit 1
+       fi
+}
 
 if [ -n "$CONF_IMAGE" ]; then
        case "$(get_magic_word $CONF_IMAGE cat)" in
@@ -351,12 +351,6 @@ if [ $TEST -eq 1 ]; then
        exit 0
 fi
 
-if [ $SAVE_PARTITIONS -eq 0 ]; then
-       touch /tmp/sysupgrade.always.overwrite.bootdisk.partmap
-else
-       rm -f /tmp/sysupgrade.always.overwrite.bootdisk.partmap
-fi
-
 install_bin /sbin/upgraded
 v "Commencing upgrade. Closing all shell sessions."
 
@@ -366,9 +360,18 @@ if [ -n "$FAILSAFE" ]; then
        printf '%s\x00%s\x00%s' "$RAM_ROOT" "$IMAGE" "$COMMAND" >/tmp/sysupgrade
        lock -u /tmp/.failsafe
 else
+       force_attr=""
+       [ $FORCE -eq 1 ] && force_attr="\"force\": true,"
+       backup_attr=""
+       [ $SAVE_CONFIG -eq 1 ] && backup_attr="\"backup\": $(json_string $CONF_TAR),"
        ubus call system sysupgrade "{
                \"prefix\": $(json_string "$RAM_ROOT"),
                \"path\": $(json_string "$IMAGE"),
-               \"command\": $(json_string "$COMMAND")
+               $force_attr
+               $backup_attr
+               \"command\": $(json_string "$COMMAND"),
+               \"options\": {
+                       \"save_partitions\": $SAVE_PARTITIONS
+               }
        }"
 fi