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 -f <config> restore configuration from .tar.gz (file or url)
60 -c attempt to preserve all changed files in /etc/
61 -n do not save configuration over reflash
62 -p do not attempt to restore the partition table after flash.
64 Verify image and config .tar.gz but do not actually flash.
66 Flash image even if image checks fail, this is dangerous!
69 -h | --help display this help
72 -b | --create-backup <file>
73 create .tar.gz of files specified in sysupgrade.conf
74 then exit. Does not flash an image. If file is '-',
75 i.e. stdout, verbosity is set to 0 (i.e. quiet).
76 -r | --restore-backup <file>
77 restore a .tar.gz created with sysupgrade -b
78 then exit. Does not flash an image. If file is '-',
79 the archive is read from stdin.
81 list the files that would be backed up when calling
82 sysupgrade -b. Does not create a backup file.
88 [ -n "$IMAGE" -a -n "$NEED_IMAGE" ] && {
90 -b|--create-backup and -r|--restore-backup do not perform a firmware upgrade.
91 Do not specify both -b|-r and a firmware image.
96 # prevent messages from clobbering the tarball when using stdout
97 [ "$CONF_BACKUP" = "-" ] && export VERBOSE
=0
102 BEGIN { conffiles = 0 }
103 /^Conffiles:/ { conffiles = 1; next }
104 !/^ / { conffiles = 0; next }
105 conffiles == 1 { print }
106 ' /usr
/lib
/opkg
/status
109 list_changed_conffiles
() {
110 # Cannot handle spaces in filenames - but opkg cannot either...
111 list_conffiles |
while read file csum
; do
112 [ -r "$file" ] ||
continue
114 echo "${csum} ${file}" | sha256sum
-sc - ||
echo "$file"
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 list_changed_conffiles
) |
sort -u > "$file"
129 find /overlay
/upper
/etc
/ -type f
-o -type l |
sed \
130 -e 's,^/overlay\/upper/,/,' \
131 -e '\,/META_[a-zA-Z0-9]*$,d' \
132 -e '\,/functions.sh$,d' \
133 -e '\,/[^/]*-opkg$,d' \
139 sysupgrade_image_check
="fwtool_check_signature fwtool_check_image platform_check_image"
141 if [ $SAVE_OVERLAY = 1 ]; then
142 [ ! -d /overlay
/upper
/etc
] && {
143 echo "Cannot find '/overlay/upper/etc', required for '-c'"
146 sysupgrade_init_conffiles
="add_overlayfiles"
148 sysupgrade_init_conffiles
="add_uci_conffiles"
153 do_save_conffiles
() {
154 local conf_tar
="${1:-$CONF_TAR}"
156 [ -z "$(rootfs_type)" ] && {
157 echo "Cannot save config while running from ramdisk."
158 ask_bool
0 "Abort" && exit
162 run_hooks
"$CONFFILES" $sysupgrade_init_conffiles
163 ask_bool
0 "Edit config file list" && vi
"$CONFFILES"
165 v
"Saving config files..."
166 [ "$VERBOSE" -gt 1 ] && TAR_V
="v" || TAR_V
=""
167 tar c
${TAR_V}zf
"$conf_tar" -T "$CONFFILES" 2>/dev
/null
168 if [ "$?" -ne 0 ]; then
169 echo "Failed to create the configuration backup."
177 if [ $CONF_BACKUP_LIST -eq 1 ]; then
178 run_hooks
"$CONFFILES" $sysupgrade_init_conffiles
184 if [ -n "$CONF_BACKUP" ]; then
185 do_save_conffiles
"$CONF_BACKUP"
189 if [ -n "$CONF_RESTORE" ]; then
190 if [ "$CONF_RESTORE" != "-" ] && [ ! -f "$CONF_RESTORE" ]; then
191 echo "Backup archive '$CONF_RESTORE' not found."
195 [ "$VERBOSE" -gt 1 ] && TAR_V
="v" || TAR_V
=""
196 tar -C / -x${TAR_V}zf
"$CONF_RESTORE"
200 type platform_check_image
>/dev
/null
2>/dev
/null ||
{
201 echo "Firmware upgrade is not implemented for this platform."
207 wget
-O/tmp
/sysupgrade.img
"$IMAGE"
208 IMAGE
=/tmp
/sysupgrade.img
212 IMAGE
="$(readlink -f "$IMAGE")"
216 echo "Image file not found."
221 v
"Image not in /tmp, copying..."
222 cp -f "$IMAGE" /tmp
/sysupgrade.img
223 IMAGE
=/tmp
/sysupgrade.img
230 for check
in $sysupgrade_image_check; do
231 ( $check "$IMAGE" ) ||
{
232 if [ $FORCE -eq 1 ]; then
233 echo "Image check '$check' failed but --force given - will update anyway!"
236 echo "Image check '$check' failed."
242 if [ -n "$CONF_IMAGE" ]; then
243 case "$(get_magic_word $CONF_IMAGE cat)" in
247 echo "Invalid config file. Please use only .tar.gz files"
251 get_image
"$CONF_IMAGE" "cat" > "$CONF_TAR"
253 elif ask_bool
$SAVE_CONFIG "Keep config files over reflash"; then
254 [ $TEST -eq 1 ] || do_save_conffiles
257 [ $TEST -eq 1 ] ||
rm -f "$CONF_TAR"
261 if [ $TEST -eq 1 ]; then
265 if [ $SAVE_PARTITIONS -eq 0 ]; then
266 touch /tmp
/sysupgrade.always.overwrite.bootdisk.partmap
268 rm -f /tmp
/sysupgrade.always.overwrite.bootdisk.partmap
271 install_bin
/sbin
/upgraded
272 v
"Commencing upgrade. Closing all shell sessions."
274 COMMAND
='. /lib/functions.sh; include /lib/upgrade; do_upgrade_stage2'
276 if [ -n "$FAILSAFE" ]; then
277 printf '%s\x00%s\x00%s' "$RAM_ROOT" "$IMAGE" "$COMMAND" >/tmp
/sysupgrade
278 lock
-u /tmp
/.failsafe
280 ubus call system sysupgrade
"{
281 \"prefix\": $(json_string "$RAM_ROOT"),
282 \"path\": $(json_string "$IMAGE"),
283 \"command\": $(json_string "$COMMAND")