3 . /lib/functions/system.sh
6 RAMFS_COPY_BIN="" # extra programs for temporary ramfs root
7 RAMFS_COPY_DATA="" # extra data files
8 export MTD_CONFIG_ARGS=""
13 export SAVE_PARTITIONS=1
16 export CONF_BACKUP_LIST=0
27 -i) export INTERACTIVE=1;;
28 -d) export DELAY="$2"; shift;;
29 -v) export VERBOSE="$(($VERBOSE + 1))";;
30 -q) export VERBOSE="$(($VERBOSE - 1))";;
31 -n) export SAVE_CONFIG=0;;
32 -c) export SAVE_OVERLAY=1;;
33 -p) export SAVE_PARTITIONS=0;;
34 -b|--create-backup) export CONF_BACKUP="$2" NEED_IMAGE=1; shift;;
35 -r|--restore-backup) export CONF_RESTORE="$2" NEED_IMAGE=1; shift;;
36 -l|--list-backup) export CONF_BACKUP_LIST=1; break;;
37 -f) export CONF_IMAGE="$2"; shift;;
38 -F|--force) export FORCE=1;;
39 -T|--test) export TEST=1;;
40 -h|--help) export HELP=1; break;;
42 echo "Invalid option: $1"
50 export CONFFILES=/tmp/sysupgrade.conffiles
51 export CONF_TAR=/tmp/sysupgrade.tgz
56 [ -z "$ARGV" -a -z "$NEED_IMAGE" -o $HELP -gt 0 ] && {
58 Usage: $0 [<upgrade-option>...] <image file or URL>
59 $0 [-q] [-i] <backup-command> <file>
62 -d <delay> add a delay before rebooting
63 -f <config> restore configuration from .tar.gz (file or url)
65 -c attempt to preserve all changed files in /etc/
66 -n do not save configuration over reflash
67 -p do not attempt to restore the partition table after flash.
69 Verify image and config .tar.gz but do not actually flash.
71 Flash image even if image checks fail, this is dangerous!
74 -h | --help display this help
77 -b | --create-backup <file>
78 create .tar.gz of files specified in sysupgrade.conf
79 then exit. Does not flash an image. If file is '-',
80 i.e. stdout, verbosity is set to 0 (i.e. quiet).
81 -r | --restore-backup <file>
82 restore a .tar.gz created with sysupgrade -b
83 then exit. Does not flash an image. If file is '-',
84 the archive is read from stdin.
86 list the files that would be backed up when calling
87 sysupgrade -b. Does not create a backup file.
93 [ -n "$ARGV" -a -n "$NEED_IMAGE" ] && {
95 -b|--create-backup and -r|--restore-backup do not perform a firmware upgrade.
96 Do not specify both -b|-r and a firmware image.
101 # prevent messages from clobbering the tarball when using stdout
102 [ "$CONF_BACKUP" = "-" ] && export VERBOSE=0
104 add_uci_conffiles() {
106 ( find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \
107 /etc/sysupgrade.conf /lib/upgrade/keep.d/* 2>/dev/null) \
108 -type f -o -type l 2>/dev/null;
109 opkg list-changed-conffiles ) | sort -u > "$file"
115 if [ -d /overlay/upper ]; then
116 local overlaydir="/overlay/upper"
118 local overlaydir="/overlay"
120 find $overlaydir/etc/ -type f -o -type l | sed \
121 -e 's,^/overlay\/upper/,/,' \
122 -e 's,^/overlay/,/,' \
123 -e '\,/META_[a-zA-Z0-9]*$,d' \
124 -e '\,/functions.sh$,d' \
125 -e '\,/[^/]*-opkg$,d' \
131 sysupgrade_image_check="platform_check_image"
132 [ $SAVE_OVERLAY = 0 -o ! -d /overlay/etc ] && \
133 sysupgrade_init_conffiles="add_uci_conffiles" || \
134 sysupgrade_init_conffiles="add_overlayfiles"
138 [ "$1" = "nand" ] && nand_upgrade_stage2 $@
140 do_save_conffiles() {
141 local conf_tar="${1:-$CONF_TAR}"
143 [ -z "$(rootfs_type)" ] && {
144 echo "Cannot save config while running from ramdisk."
145 ask_bool 0 "Abort" && exit
148 run_hooks "$CONFFILES" $sysupgrade_init_conffiles
149 ask_bool 0 "Edit config file list" && vi "$CONFFILES"
151 v "Saving config files..."
152 [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
153 tar c${TAR_V}zf "$conf_tar" -T "$CONFFILES" 2>/dev/null
158 if [ $CONF_BACKUP_LIST -eq 1 ]; then
159 add_uci_conffiles "$CONFFILES"
165 if [ -n "$CONF_BACKUP" ]; then
166 do_save_conffiles "$CONF_BACKUP"
170 if [ -n "$CONF_RESTORE" ]; then
171 if [ "$CONF_RESTORE" != "-" ] && [ ! -f "$CONF_RESTORE" ]; then
172 echo "Backup archive '$CONF_RESTORE' not found."
176 [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
177 tar -C / -x${TAR_V}zf "$CONF_RESTORE"
181 type platform_check_image >/dev/null 2>/dev/null || {
182 echo "Firmware upgrade is not implemented for this platform."
186 for check in $sysupgrade_image_check; do
187 ( eval "$check \"\$ARGV\"" ) || {
188 if [ $FORCE -eq 1 ]; then
189 echo "Image check '$check' failed but --force given - will update anyway!"
192 echo "Image check '$check' failed."
198 if [ -n "$CONF_IMAGE" ]; then
199 case "$(get_magic_word $CONF_IMAGE cat)" in
203 echo "Invalid config file. Please use only .tar.gz files"
207 get_image "$CONF_IMAGE" "cat" > "$CONF_TAR"
209 elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then
210 [ $TEST -eq 1 ] || do_save_conffiles
216 if [ $TEST -eq 1 ]; then
220 run_hooks "" $sysupgrade_pre_upgrade
222 # Some platforms/devices may want different sysupgrade process, e.g. without
223 # killing processes yet or calling ubus system upgrade method.
224 # This is needed e.g. on NAND devices where we just want to trigger stage1 at
226 if type 'platform_pre_upgrade' >/dev/null 2>/dev/null; then
227 platform_pre_upgrade "$ARGV"
230 ubus call system upgrade
231 touch /tmp/sysupgrade
233 if [ ! -f /tmp/failsafe ] ; then
239 if [ -n "$(rootfs_type)" ]; then
240 v "Switching to ramdisk..."
241 run_ramfs '. /lib/functions.sh; include /lib/upgrade; do_upgrade'