| 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 | export CONF_BACKUP= |
| 14 | export HELP=0 |
| 15 | export FORCE=0 |
| 16 | |
| 17 | # parse options |
| 18 | while [ -n "$1" ]; do |
| 19 | case "$1" in |
| 20 | -i) export INTERACTIVE=1;; |
| 21 | -d) export DELAY="$2"; shift;; |
| 22 | -v) export VERBOSE="$(($VERBOSE + 1))";; |
| 23 | -q) export VERBOSE="$(($VERBOSE - 1))";; |
| 24 | -n) export SAVE_CONFIG=0;; |
| 25 | -c) export SAVE_OVERLAY=1;; |
| 26 | -b|--create-backup) export CONF_BACKUP="$2"; shift;; |
| 27 | -f) export CONF_IMAGE="$2"; shift;; |
| 28 | -F|--force) export FORCE=1; shift;; |
| 29 | -h|--help) export HELP=1; break;; |
| 30 | -*) |
| 31 | echo "Invalid option: $1" |
| 32 | exit 1 |
| 33 | ;; |
| 34 | *) break;; |
| 35 | esac |
| 36 | shift; |
| 37 | done |
| 38 | |
| 39 | export CONFFILES=/tmp/sysupgrade.conffiles |
| 40 | export CONF_TAR=/tmp/sysupgrade.tgz |
| 41 | |
| 42 | export ARGV="$*" |
| 43 | export ARGC="$#" |
| 44 | |
| 45 | [ -z "$ARGV" -a -z "$CONF_BACKUP" -o $HELP -gt 0 ] && { |
| 46 | cat <<EOF |
| 47 | Usage: $0 [options] <image file or URL> |
| 48 | |
| 49 | Options: |
| 50 | -d <delay> add a delay before rebooting |
| 51 | -f <config> restore configuration from .tar.gz (file or url) |
| 52 | -i interactive mode |
| 53 | -c attempt to preserve all changed files in /etc/ |
| 54 | -b / --create-backup <file> |
| 55 | create .tar.gz of files specified in sysupgrade.conf |
| 56 | then exit. Does not flash an image. If file is '-', |
| 57 | i.e. stdout, verbosity is set to 0 (i.e. quiet). |
| 58 | -n do not save configuration over reflash |
| 59 | -F / --force |
| 60 | Flash image even if image checks fail, this is dangerous! |
| 61 | -q less verbose |
| 62 | -v more verbose |
| 63 | -h / --help display this help |
| 64 | |
| 65 | EOF |
| 66 | exit 1 |
| 67 | } |
| 68 | |
| 69 | [ -n "$ARGV" -a -n "$CONF_BACKUP" ] && { |
| 70 | cat <<-EOF |
| 71 | -b/--create-backup does not perform a firmware upgrade. Do not |
| 72 | specify both -b and a firmware image. |
| 73 | EOF |
| 74 | exit 1 |
| 75 | } |
| 76 | |
| 77 | # prevent messages from clobbering the tarball when using stdout |
| 78 | [ "$CONF_BACKUP" = "-" ] && export VERBOSE=0 |
| 79 | |
| 80 | add_uci_conffiles() { |
| 81 | local file="$1" |
| 82 | ( find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \ |
| 83 | /etc/sysupgrade.conf /lib/upgrade/keep.d/* 2>/dev/null) \ |
| 84 | -type f 2>/dev/null; |
| 85 | opkg list-changed-conffiles ) | sort -u > "$file" |
| 86 | return 0 |
| 87 | } |
| 88 | |
| 89 | add_overlayfiles() { |
| 90 | local file="$1" |
| 91 | find /overlay/etc/ -type f | sed \ |
| 92 | -e 's,^/overlay/,/,' \ |
| 93 | -e '\,/META_[a-zA-Z0-9]*$,d' \ |
| 94 | -e '\,/functions.sh$,d' \ |
| 95 | -e '\,/[^/]*-opkg$,d' \ |
| 96 | > "$file" |
| 97 | return 0 |
| 98 | } |
| 99 | |
| 100 | # hooks |
| 101 | sysupgrade_image_check="platform_check_image" |
| 102 | [ $SAVE_OVERLAY = 0 -o ! -d /overlay/etc ] && \ |
| 103 | sysupgrade_init_conffiles="add_uci_conffiles" || \ |
| 104 | sysupgrade_init_conffiles="add_overlayfiles" |
| 105 | |
| 106 | include /lib/upgrade |
| 107 | |
| 108 | do_save_conffiles() { |
| 109 | local conf_tar="${1:-$CONF_TAR}" |
| 110 | |
| 111 | [ -z "$(rootfs_type)" ] && { |
| 112 | echo "Cannot save config while running from ramdisk." |
| 113 | ask_bool 0 "Abort" && exit |
| 114 | return 0 |
| 115 | } |
| 116 | run_hooks "$CONFFILES" $sysupgrade_init_conffiles |
| 117 | ask_bool 0 "Edit config file list" && vi "$CONFFILES" |
| 118 | |
| 119 | v "Saving config files..." |
| 120 | [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V="" |
| 121 | tar c${TAR_V}zf "$conf_tar" -T "$CONFFILES" 2>/dev/null |
| 122 | } |
| 123 | |
| 124 | if [ -n "$CONF_BACKUP" ]; then |
| 125 | do_save_conffiles "$CONF_BACKUP" |
| 126 | exit $? |
| 127 | fi |
| 128 | |
| 129 | type platform_check_image >/dev/null 2>/dev/null || { |
| 130 | echo "Firmware upgrade is not implemented for this platform." |
| 131 | exit 1 |
| 132 | } |
| 133 | |
| 134 | for check in $sysupgrade_image_check; do |
| 135 | ( eval "$check \"\$ARGV\"" ) || { |
| 136 | if [ $FORCE -eq 1 ]; then |
| 137 | echo "Image check '$check' failed but --force given - will update anyway!" |
| 138 | break |
| 139 | else |
| 140 | echo "Image check '$check' failed." |
| 141 | exit 1 |
| 142 | fi |
| 143 | } |
| 144 | done |
| 145 | |
| 146 | if [ -n "$CONF_IMAGE" ]; then |
| 147 | case "$(get_magic_word $CONF_IMAGE cat)" in |
| 148 | # .gz files |
| 149 | 1f8b) ;; |
| 150 | *) |
| 151 | echo "Invalid config file. Please use only .tar.gz files" |
| 152 | exit 1 |
| 153 | ;; |
| 154 | esac |
| 155 | get_image "$CONF_IMAGE" "cat" > "$CONF_TAR" |
| 156 | export SAVE_CONFIG=1 |
| 157 | elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then |
| 158 | do_save_conffiles |
| 159 | export SAVE_CONFIG=1 |
| 160 | else |
| 161 | export SAVE_CONFIG=0 |
| 162 | fi |
| 163 | |
| 164 | run_hooks "" $sysupgrade_pre_upgrade |
| 165 | |
| 166 | kill_remaining TERM |
| 167 | sleep 3 |
| 168 | kill_remaining KILL |
| 169 | |
| 170 | if [ -n "$(rootfs_type)" ]; then |
| 171 | v "Switching to ramdisk..." |
| 172 | run_ramfs '. /etc/functions.sh; include /lib/upgrade; do_upgrade' |
| 173 | else |
| 174 | do_upgrade |
| 175 | fi |
| 176 | |