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"
51 if ! lock
-n /tmp
/sysupgrade.lock
53 echo "Another instance of sysupgrade already running"
54 echo "If you are sure this is wrong remove file /tmp/sysupgrade.lock"
58 # make sure the lock is removed when done even when killed
60 lock
-u /var
/sysupgrade.lock
62 trap finish EXIT INT TERM
64 export CONFFILES
=/tmp
/sysupgrade.conffiles
65 export CONF_TAR
=/tmp
/sysupgrade.tgz
70 [ -z "$ARGV" -a -z "$NEED_IMAGE" -o $HELP -gt 0 ] && {
72 Usage: $0 [<upgrade-option>...] <image file or URL>
73 $0 [-q] [-i] <backup-command> <file>
76 -d <delay> add a delay before rebooting
77 -f <config> restore configuration from .tar.gz (file or url)
79 -c attempt to preserve all changed files in /etc/
80 -n do not save configuration over reflash
81 -p do not attempt to restore the partition table after flash.
83 Verify image and config .tar.gz but do not actually flash.
85 Flash image even if image checks fail, this is dangerous!
88 -h | --help display this help
91 -b | --create-backup <file>
92 create .tar.gz of files specified in sysupgrade.conf
93 then exit. Does not flash an image. If file is '-',
94 i.e. stdout, verbosity is set to 0 (i.e. quiet).
95 -r | --restore-backup <file>
96 restore a .tar.gz created with sysupgrade -b
97 then exit. Does not flash an image. If file is '-',
98 the archive is read from stdin.
100 list the files that would be backed up when calling
101 sysupgrade -b. Does not create a backup file.
107 [ -n "$ARGV" -a -n "$NEED_IMAGE" ] && {
109 -b|--create-backup and -r|--restore-backup do not perform a firmware upgrade.
110 Do not specify both -b|-r and a firmware image.
115 # prevent messages from clobbering the tarball when using stdout
116 [ "$CONF_BACKUP" = "-" ] && export VERBOSE
=0
118 add_uci_conffiles
() {
120 ( find $
(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \
121 /etc
/sysupgrade.conf
/lib
/upgrade
/keep.d
/* 2>/dev
/null
) \
122 -type f
-o -type l
2>/dev
/null
;
123 opkg list-changed-conffiles
) |
sort -u > "$file"
129 if [ -d /overlay
/upper
]; then
130 local overlaydir
="/overlay/upper"
132 local overlaydir
="/overlay"
134 find $overlaydir/etc
/ -type f
-o -type l |
sed \
135 -e 's,^/overlay\/upper/,/,' \
136 -e 's,^/overlay/,/,' \
137 -e '\,/META_[a-zA-Z0-9]*$,d' \
138 -e '\,/functions.sh$,d' \
139 -e '\,/[^/]*-opkg$,d' \
145 sysupgrade_image_check
="fwtool_check_image platform_check_image"
146 sysupgrade_pre_upgrade
="fwtool_pre_upgrade"
147 [ $SAVE_OVERLAY = 0 -o ! -d /overlay
/etc
] && \
148 sysupgrade_init_conffiles
="add_uci_conffiles" || \
149 sysupgrade_init_conffiles
="add_overlayfiles"
153 [ "$1" = "nand" ] && nand_upgrade_stage2 $@
155 do_save_conffiles
() {
156 local conf_tar
="${1:-$CONF_TAR}"
158 [ -z "$(rootfs_type)" ] && {
159 echo "Cannot save config while running from ramdisk."
160 ask_bool
0 "Abort" && exit
163 run_hooks
"$CONFFILES" $sysupgrade_init_conffiles
164 ask_bool
0 "Edit config file list" && vi
"$CONFFILES"
166 v
"Saving config files..."
167 [ "$VERBOSE" -gt 1 ] && TAR_V
="v" || TAR_V
=""
168 tar c
${TAR_V}zf
"$conf_tar" -T "$CONFFILES" 2>/dev
/null
173 if [ $CONF_BACKUP_LIST -eq 1 ]; then
174 add_uci_conffiles
"$CONFFILES"
180 if [ -n "$CONF_BACKUP" ]; then
181 do_save_conffiles
"$CONF_BACKUP"
185 if [ -n "$CONF_RESTORE" ]; then
186 if [ "$CONF_RESTORE" != "-" ] && [ ! -f "$CONF_RESTORE" ]; then
187 echo "Backup archive '$CONF_RESTORE' not found."
191 [ "$VERBOSE" -gt 1 ] && TAR_V
="v" || TAR_V
=""
192 tar -C / -x${TAR_V}zf
"$CONF_RESTORE"
196 type platform_check_image
>/dev
/null
2>/dev
/null ||
{
197 echo "Firmware upgrade is not implemented for this platform."
201 for check
in $sysupgrade_image_check; do
202 ( eval "$check \"\$ARGV\"" ) ||
{
203 if [ $FORCE -eq 1 ]; then
204 echo "Image check '$check' failed but --force given - will update anyway!"
207 echo "Image check '$check' failed."
213 if [ -n "$CONF_IMAGE" ]; then
214 case "$(get_magic_word $CONF_IMAGE cat)" in
218 echo "Invalid config file. Please use only .tar.gz files"
222 get_image
"$CONF_IMAGE" "cat" > "$CONF_TAR"
224 elif ask_bool
$SAVE_CONFIG "Keep config files over reflash"; then
225 [ $TEST -eq 1 ] || do_save_conffiles
231 if [ $TEST -eq 1 ]; then
235 run_hooks
"" $sysupgrade_pre_upgrade
237 # Some platforms/devices may want different sysupgrade process, e.g. without
238 # killing processes yet or calling ubus system upgrade method.
239 # This is needed e.g. on NAND devices where we just want to trigger stage1 at
241 if type 'platform_pre_upgrade' >/dev
/null
2>/dev
/null
; then
242 platform_pre_upgrade
"$ARGV"
245 ubus call system upgrade
246 touch /tmp
/sysupgrade
248 if [ ! -f /tmp
/failsafe
] ; then
254 if [ -n "$(rootfs_type)" ]; then
255 v
"Switching to ramdisk..."
256 run_ramfs
'. /lib/functions.sh; include /lib/upgrade; do_upgrade'