4 .
/lib
/functions
/system.sh
7 export MTD_CONFIG_ARGS
=""
12 export SAVE_PARTITIONS
=1
14 export CONF_BACKUP_LIST
=0
25 -i) export INTERACTIVE
=1;;
26 -v) export VERBOSE
="$(($VERBOSE + 1))";;
27 -q) export VERBOSE
="$(($VERBOSE - 1))";;
28 -n) export SAVE_CONFIG
=0;;
29 -c) export SAVE_OVERLAY
=1;;
30 -p) export SAVE_PARTITIONS
=0;;
31 -b|
--create-backup) export CONF_BACKUP
="$2" NEED_IMAGE
=1; shift;;
32 -r|
--restore-backup) export CONF_RESTORE
="$2" NEED_IMAGE
=1; shift;;
33 -l|
--list-backup) export CONF_BACKUP_LIST
=1; break;;
34 -f) export CONF_IMAGE
="$2"; shift;;
35 -F|
--force) export FORCE
=1;;
36 -T|
--test) export TEST
=1;;
37 -h|
--help) export HELP
=1; break;;
39 echo "Invalid option: $1"
47 export CONFFILES
=/tmp
/sysupgrade.conffiles
48 export CONF_TAR
=/tmp
/sysupgrade.tgz
52 [ -z "$IMAGE" -a -z "$NEED_IMAGE" -o $HELP -gt 0 ] && {
54 Usage: $0 [<upgrade-option>...] <image file or URL>
55 $0 [-q] [-i] <backup-command> <file>
58 -d <delay> add a delay before rebooting
59 -f <config> restore configuration from .tar.gz (file or url)
61 -c attempt to preserve all changed files in /etc/
62 -n do not save configuration over reflash
63 -p do not attempt to restore the partition table after flash.
65 Verify image and config .tar.gz but do not actually flash.
67 Flash image even if image checks fail, this is dangerous!
70 -h | --help display this help
73 -b | --create-backup <file>
74 create .tar.gz of files specified in sysupgrade.conf
75 then exit. Does not flash an image. If file is '-',
76 i.e. stdout, verbosity is set to 0 (i.e. quiet).
77 -r | --restore-backup <file>
78 restore a .tar.gz created with sysupgrade -b
79 then exit. Does not flash an image. If file is '-',
80 the archive is read from stdin.
82 list the files that would be backed up when calling
83 sysupgrade -b. Does not create a backup file.
89 [ -n "$IMAGE" -a -n "$NEED_IMAGE" ] && {
91 -b|--create-backup and -r|--restore-backup do not perform a firmware upgrade.
92 Do not specify both -b|-r and a firmware image.
97 # prevent messages from clobbering the tarball when using stdout
98 [ "$CONF_BACKUP" = "-" ] && export VERBOSE
=0
100 add_uci_conffiles
() {
102 ( find $
(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \
103 /etc
/sysupgrade.conf
/lib
/upgrade
/keep.d
/* 2>/dev
/null
) \
104 -type f
-o -type l
2>/dev
/null
;
105 opkg list-changed-conffiles
) |
sort -u > "$file"
111 if [ -d /overlay
/upper
]; then
112 local overlaydir
="/overlay/upper"
114 local overlaydir
="/overlay"
116 find $overlaydir/etc
/ -type f
-o -type l |
sed \
117 -e 's,^/overlay\/upper/,/,' \
118 -e 's,^/overlay/,/,' \
119 -e '\,/META_[a-zA-Z0-9]*$,d' \
120 -e '\,/functions.sh$,d' \
121 -e '\,/[^/]*-opkg$,d' \
127 sysupgrade_image_check
="fwtool_check_image platform_check_image"
128 sysupgrade_pre_upgrade
="fwtool_pre_upgrade"
129 [ $SAVE_OVERLAY = 0 -o ! -d /overlay
/etc
] && \
130 sysupgrade_init_conffiles
="add_uci_conffiles" || \
131 sysupgrade_init_conffiles
="add_overlayfiles"
135 do_save_conffiles
() {
136 local conf_tar
="${1:-$CONF_TAR}"
138 [ -z "$(rootfs_type)" ] && {
139 echo "Cannot save config while running from ramdisk."
140 ask_bool
0 "Abort" && exit
144 run_hooks
"$CONFFILES" $sysupgrade_init_conffiles
145 ask_bool
0 "Edit config file list" && vi
"$CONFFILES"
147 v
"Saving config files..."
148 [ "$VERBOSE" -gt 1 ] && TAR_V
="v" || TAR_V
=""
149 tar c
${TAR_V}zf
"$conf_tar" -T "$CONFFILES" 2>/dev
/null
154 if [ $CONF_BACKUP_LIST -eq 1 ]; then
155 add_uci_conffiles
"$CONFFILES"
161 if [ -n "$CONF_BACKUP" ]; then
162 do_save_conffiles
"$CONF_BACKUP"
166 if [ -n "$CONF_RESTORE" ]; then
167 if [ "$CONF_RESTORE" != "-" ] && [ ! -f "$CONF_RESTORE" ]; then
168 echo "Backup archive '$CONF_RESTORE' not found."
172 [ "$VERBOSE" -gt 1 ] && TAR_V
="v" || TAR_V
=""
173 tar -C / -x${TAR_V}zf
"$CONF_RESTORE"
177 type platform_check_image
>/dev
/null
2>/dev
/null ||
{
178 echo "Firmware upgrade is not implemented for this platform."
184 wget
-O/tmp
/sysupgrade.img
"$IMAGE"
185 IMAGE
=/tmp
/sysupgrade.img
189 IMAGE
="$(readlink -f "$IMAGE")"
193 echo "Image file not found."
198 v
"Image not in /tmp, copying..."
199 cp -f "$IMAGE" /tmp
/sysupgrade.img
200 IMAGE
=/tmp
/sysupgrade.img
207 for check
in $sysupgrade_image_check; do
208 ( $check "$IMAGE" ) ||
{
209 if [ $FORCE -eq 1 ]; then
210 echo "Image check '$check' failed but --force given - will update anyway!"
213 echo "Image check '$check' failed."
219 if [ -n "$CONF_IMAGE" ]; then
220 case "$(get_magic_word $CONF_IMAGE cat)" in
224 echo "Invalid config file. Please use only .tar.gz files"
228 get_image
"$CONF_IMAGE" "cat" > "$CONF_TAR"
230 elif ask_bool
$SAVE_CONFIG "Keep config files over reflash"; then
231 [ $TEST -eq 1 ] || do_save_conffiles
234 [ $TEST -eq 1 ] ||
rm -f "$CONF_TAR"
238 if [ $TEST -eq 1 ]; then
242 if [ $SAVE_PARTITIONS -eq 0 ]; then
243 touch /tmp
/sysupgrade.always.overwrite.bootdisk.partmap
245 rm -f /tmp
/sysupgrade.always.overwrite.bootdisk.partmap
248 run_hooks
"" $sysupgrade_pre_upgrade
250 install_bin
/sbin
/upgraded
251 v
"Commencing upgrade. All shell sessions will be closed now."
253 COMMAND
='. /lib/functions.sh; include /lib/upgrade; do_upgrade_stage2'
255 if [ -n "$FAILSAFE" ]; then
256 printf '%s\x00%s\x00%s' "$RAM_ROOT" "$IMAGE" "$COMMAND" >/tmp
/sysupgrade
257 lock
-u /tmp
/.failsafe
259 ubus call system sysupgrade
"{
260 \"prefix\": $(json_string "$RAM_ROOT"),
261 \"path\": $(json_string "$IMAGE"),
262 \"command\": $(json_string "$COMMAND")