| 1 | #!/bin/sh |
| 2 | . /etc/functions.sh |
| 3 | |
| 4 | # initialize defaults |
| 5 | RAMFS_COPY_BIN="" # extra programs for temporary ramfs root |
| 6 | RAMFS_COPY_DATA="" # extra data files |
| 7 | export INTERACTIVE=0 |
| 8 | export VERBOSE=1 |
| 9 | export SAVE_CONFIG=1 |
| 10 | export SAVE_OVERLAY=0 |
| 11 | export DELAY= |
| 12 | export CONF_IMAGE= |
| 13 | |
| 14 | # parse options |
| 15 | while [ -n "$1" ]; do |
| 16 | case "$1" in |
| 17 | -i) export INTERACTIVE=1;; |
| 18 | -d) export DELAY="$2"; shift;; |
| 19 | -v) export VERBOSE="$(($VERBOSE + 1))";; |
| 20 | -q) export VERBOSE="$(($VERBOSE - 1))";; |
| 21 | -n) export SAVE_CONFIG=0;; |
| 22 | -c) export SAVE_OVERLAY=1;; |
| 23 | -f) export CONF_IMAGE="$2"; shift;; |
| 24 | -*) |
| 25 | echo "Invalid option: $1" |
| 26 | exit 1 |
| 27 | ;; |
| 28 | *) break;; |
| 29 | esac |
| 30 | shift; |
| 31 | done |
| 32 | |
| 33 | export CONFFILES=/tmp/sysupgrade.conffiles |
| 34 | export CONF_TAR=/tmp/sysupgrade.tgz |
| 35 | |
| 36 | export ARGV="$*" |
| 37 | export ARGC="$#" |
| 38 | |
| 39 | [ -z "$ARGV" ] && { |
| 40 | cat <<EOF |
| 41 | Usage: $0 [options] <image file or URL> |
| 42 | |
| 43 | Options: |
| 44 | -d <delay> add a delay before rebooting |
| 45 | -f <config> restore configuration from .tar.gz (file or url) |
| 46 | -i interactive mode |
| 47 | -c attempt to preserve all changed files in /etc/ |
| 48 | -n do not save configuration over reflash |
| 49 | -q less verbose |
| 50 | -v more verbose |
| 51 | |
| 52 | EOF |
| 53 | exit 1 |
| 54 | } |
| 55 | |
| 56 | add_uci_conffiles() { |
| 57 | local file="$1" |
| 58 | ( find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \ |
| 59 | /etc/sysupgrade.conf /lib/upgrade/keep.d/* 2>/dev/null) \ |
| 60 | -type f 2>/dev/null; |
| 61 | opkg list-changed-conffiles ) | sort -u > "$file" |
| 62 | return 0 |
| 63 | } |
| 64 | |
| 65 | add_overlayfiles() { |
| 66 | local file="$1" |
| 67 | find /overlay/etc/ -type f | sed \ |
| 68 | -e 's,^/overlay/,/,' \ |
| 69 | -e '\,/META_[a-zA-Z0-9]*$,d' \ |
| 70 | -e '\,/functions.sh$,d' \ |
| 71 | -e '\,/[^/]*-opkg$,d' \ |
| 72 | > "$file" |
| 73 | return 0 |
| 74 | } |
| 75 | |
| 76 | # hooks |
| 77 | sysupgrade_image_check="platform_check_image" |
| 78 | [ $SAVE_OVERLAY = 0 -o ! -d /overlay/etc ] && \ |
| 79 | sysupgrade_init_conffiles="add_uci_conffiles" || \ |
| 80 | sysupgrade_init_conffiles="add_overlayfiles" |
| 81 | |
| 82 | include /lib/upgrade |
| 83 | |
| 84 | do_save_conffiles() { |
| 85 | [ -z "$(rootfs_type)" ] && { |
| 86 | echo "Cannot save config while running from ramdisk." |
| 87 | ask_bool 0 "Abort" && exit |
| 88 | return 0 |
| 89 | } |
| 90 | run_hooks "$CONFFILES" $sysupgrade_init_conffiles |
| 91 | ask_bool 0 "Edit config file list" && vi "$CONFFILES" |
| 92 | |
| 93 | v "Saving config files..." |
| 94 | [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V="" |
| 95 | tar c${TAR_V}zf "$CONF_TAR" -T "$CONFFILES" 2>/dev/null |
| 96 | } |
| 97 | |
| 98 | type platform_check_image >/dev/null 2>/dev/null || { |
| 99 | echo "Firmware upgrade is not implemented for this platform." |
| 100 | exit 1 |
| 101 | } |
| 102 | |
| 103 | for check in $sysupgrade_image_check; do |
| 104 | ( eval "$check \"\$ARGV\"" ) || { |
| 105 | echo "Image check '$check' failed." |
| 106 | exit 1 |
| 107 | } |
| 108 | done |
| 109 | |
| 110 | if [ -n "$CONF_IMAGE" ]; then |
| 111 | case "$(get_magic_word $CONF_IMAGE cat)" in |
| 112 | # .gz files |
| 113 | 1f8b) ;; |
| 114 | *) |
| 115 | echo "Invalid config file. Please use only .tar.gz files" |
| 116 | exit 1 |
| 117 | ;; |
| 118 | esac |
| 119 | get_image "$CONF_IMAGE" "cat" > "$CONF_TAR" |
| 120 | export SAVE_CONFIG=1 |
| 121 | elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then |
| 122 | do_save_conffiles |
| 123 | export SAVE_CONFIG=1 |
| 124 | else |
| 125 | export SAVE_CONFIG=0 |
| 126 | fi |
| 127 | run_hooks "" $sysupgrade_pre_upgrade |
| 128 | |
| 129 | if [ -n "$(rootfs_type)" ]; then |
| 130 | v "Switching to ramdisk..." |
| 131 | run_ramfs '. /etc/functions.sh; include /lib/upgrade; do_upgrade' |
| 132 | else |
| 133 | do_upgrade |
| 134 | fi |
| 135 | |