base-files: validate metadata of sysupgrade images
[openwrt/openwrt.git] / package / base-files / files / sbin / sysupgrade
1 #!/bin/sh
2 . /lib/functions.sh
3 . /lib/functions/system.sh
4
5 # initialize defaults
6 RAMFS_COPY_BIN="" # extra programs for temporary ramfs root
7 RAMFS_COPY_DATA="" # extra data files
8 export MTD_CONFIG_ARGS=""
9 export INTERACTIVE=0
10 export VERBOSE=1
11 export SAVE_CONFIG=1
12 export SAVE_OVERLAY=0
13 export SAVE_PARTITIONS=1
14 export DELAY=
15 export CONF_IMAGE=
16 export CONF_BACKUP_LIST=0
17 export CONF_BACKUP=
18 export CONF_RESTORE=
19 export NEED_IMAGE=
20 export HELP=0
21 export FORCE=0
22 export TEST=0
23
24 # parse options
25 while [ -n "$1" ]; do
26 case "$1" in
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;;
41 -*)
42 echo "Invalid option: $1"
43 exit 1
44 ;;
45 *) break;;
46 esac
47 shift;
48 done
49
50 export CONFFILES=/tmp/sysupgrade.conffiles
51 export CONF_TAR=/tmp/sysupgrade.tgz
52
53 export ARGV="$*"
54 export ARGC="$#"
55
56 [ -z "$ARGV" -a -z "$NEED_IMAGE" -o $HELP -gt 0 ] && {
57 cat <<EOF
58 Usage: $0 [<upgrade-option>...] <image file or URL>
59 $0 [-q] [-i] <backup-command> <file>
60
61 upgrade-option:
62 -d <delay> add a delay before rebooting
63 -f <config> restore configuration from .tar.gz (file or url)
64 -i interactive mode
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.
68 -T | --test
69 Verify image and config .tar.gz but do not actually flash.
70 -F | --force
71 Flash image even if image checks fail, this is dangerous!
72 -q less verbose
73 -v more verbose
74 -h | --help display this help
75
76 backup-command:
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.
85 -l | --list-backup
86 list the files that would be backed up when calling
87 sysupgrade -b. Does not create a backup file.
88
89 EOF
90 exit 1
91 }
92
93 [ -n "$ARGV" -a -n "$NEED_IMAGE" ] && {
94 cat <<-EOF
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.
97 EOF
98 exit 1
99 }
100
101 # prevent messages from clobbering the tarball when using stdout
102 [ "$CONF_BACKUP" = "-" ] && export VERBOSE=0
103
104 add_uci_conffiles() {
105 local file="$1"
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"
110 return 0
111 }
112
113 add_overlayfiles() {
114 local file="$1"
115 if [ -d /overlay/upper ]; then
116 local overlaydir="/overlay/upper"
117 else
118 local overlaydir="/overlay"
119 fi
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' \
126 > "$file"
127 return 0
128 }
129
130 # hooks
131 sysupgrade_image_check="fwtool_check_image platform_check_image"
132 sysupgrade_pre_upgrade="fwtool_pre_upgrade"
133 [ $SAVE_OVERLAY = 0 -o ! -d /overlay/etc ] && \
134 sysupgrade_init_conffiles="add_uci_conffiles" || \
135 sysupgrade_init_conffiles="add_overlayfiles"
136
137 include /lib/upgrade
138
139 [ "$1" = "nand" ] && nand_upgrade_stage2 $@
140
141 do_save_conffiles() {
142 local conf_tar="${1:-$CONF_TAR}"
143
144 [ -z "$(rootfs_type)" ] && {
145 echo "Cannot save config while running from ramdisk."
146 ask_bool 0 "Abort" && exit
147 return 0
148 }
149 run_hooks "$CONFFILES" $sysupgrade_init_conffiles
150 ask_bool 0 "Edit config file list" && vi "$CONFFILES"
151
152 v "Saving config files..."
153 [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
154 tar c${TAR_V}zf "$conf_tar" -T "$CONFFILES" 2>/dev/null
155
156 rm -f "$CONFFILES"
157 }
158
159 if [ $CONF_BACKUP_LIST -eq 1 ]; then
160 add_uci_conffiles "$CONFFILES"
161 cat "$CONFFILES"
162 rm -f "$CONFFILES"
163 exit 0
164 fi
165
166 if [ -n "$CONF_BACKUP" ]; then
167 do_save_conffiles "$CONF_BACKUP"
168 exit $?
169 fi
170
171 if [ -n "$CONF_RESTORE" ]; then
172 if [ "$CONF_RESTORE" != "-" ] && [ ! -f "$CONF_RESTORE" ]; then
173 echo "Backup archive '$CONF_RESTORE' not found."
174 exit 1
175 fi
176
177 [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
178 tar -C / -x${TAR_V}zf "$CONF_RESTORE"
179 exit $?
180 fi
181
182 type platform_check_image >/dev/null 2>/dev/null || {
183 echo "Firmware upgrade is not implemented for this platform."
184 exit 1
185 }
186
187 for check in $sysupgrade_image_check; do
188 ( eval "$check \"\$ARGV\"" ) || {
189 if [ $FORCE -eq 1 ]; then
190 echo "Image check '$check' failed but --force given - will update anyway!"
191 break
192 else
193 echo "Image check '$check' failed."
194 exit 1
195 fi
196 }
197 done
198
199 if [ -n "$CONF_IMAGE" ]; then
200 case "$(get_magic_word $CONF_IMAGE cat)" in
201 # .gz files
202 1f8b) ;;
203 *)
204 echo "Invalid config file. Please use only .tar.gz files"
205 exit 1
206 ;;
207 esac
208 get_image "$CONF_IMAGE" "cat" > "$CONF_TAR"
209 export SAVE_CONFIG=1
210 elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then
211 [ $TEST -eq 1 ] || do_save_conffiles
212 export SAVE_CONFIG=1
213 else
214 export SAVE_CONFIG=0
215 fi
216
217 if [ $TEST -eq 1 ]; then
218 exit 0
219 fi
220
221 run_hooks "" $sysupgrade_pre_upgrade
222
223 # Some platforms/devices may want different sysupgrade process, e.g. without
224 # killing processes yet or calling ubus system upgrade method.
225 # This is needed e.g. on NAND devices where we just want to trigger stage1 at
226 # this point.
227 if type 'platform_pre_upgrade' >/dev/null 2>/dev/null; then
228 platform_pre_upgrade "$ARGV"
229 fi
230
231 ubus call system upgrade
232 touch /tmp/sysupgrade
233
234 if [ ! -f /tmp/failsafe ] ; then
235 kill_remaining TERM
236 sleep 3
237 kill_remaining KILL
238 fi
239
240 if [ -n "$(rootfs_type)" ]; then
241 v "Switching to ramdisk..."
242 run_ramfs '. /lib/functions.sh; include /lib/upgrade; do_upgrade'
243 else
244 do_upgrade
245 fi