Project

General

Profile

Download (7.24 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
#!/bin/sh
2
3 1b8df11b Bill Marquette
# $Id$
4 5b237745 Scott Ullrich
# /etc/rc.firmware
5
# part of m0n0wall (http://neon1.net/m0n0wall)
6
#
7
# Copyright (C) 2003 Manuel Kasper <mk@neon1.net>.
8
# All rights reserved.
9
10 18ff56f2 Scott Ullrich
#CFDEVICE=`cat /var/etc/cfdevice`
11 5b237745 Scott Ullrich
12 dbb59707 Scott Ullrich
#exec 3>&2 2>>/tmp/firmware_update.log
13 51c9db03 Scott Ullrich
14 24fbf166 Scott Ullrich
export ACTION=$1
15
export IMG=$2
16 2b61eeb1 Scott Ullrich
if [ $# -eq 3 ]; then
17 97ab7fdc Scott Ullrich
	export CUSTOMIMG=$3
18
fi
19 24fbf166 Scott Ullrich
20
if [ $ACTION != "upgrade" ]; then
21 5b237745 Scott Ullrich
	/sbin/umount -f /ftmp > /dev/null 2>&1
22
fi
23
24 704ea918 Scott Ullrich
backup_chflags() {
25 444c89d4 Scott Ullrich
	TOPROCESS="bin lib libexec sbin usr"
26 1c895935 Scott Ullrich
	for files in $TOPROCESS; do
27 24fbf166 Scott Ullrich
		/usr/sbin/mtree -Pcp /${files} | bzip2 -9 > /tmp/chflags.dist.${files}.bz2 | logger -p daemon.info -i -t UpgradeFlags
28 1c895935 Scott Ullrich
	done
29 704ea918 Scott Ullrich
}
30
31
restore_chflags() {
32 444c89d4 Scott Ullrich
	TOPROCESS="bin lib libexec sbin usr"
33 1c895935 Scott Ullrich
	for files in $TOPROCESS; do
34
		cd / && /usr/bin/bzcat /tmp/chflags.dist.${files}.bz2 | /usr/sbin/mtree -PU -p /${files} | logger -p daemon.info -i -t UpgradeFlags
35
	done
36 704ea918 Scott Ullrich
}
37
38
remove_chflags() {
39 444c89d4 Scott Ullrich
	TOPROCESS="bin lib libexec sbin usr"
40 1c895935 Scott Ullrich
	for files in $TOPROCESS; do
41 2962941d Scott Ullrich
		/bin/chflags -R noschg /${files}
42 1c895935 Scott Ullrich
		/bin/chmod -R u+rw /${files}
43
	done
44 704ea918 Scott Ullrich
}
45
46 e607da61 Scott Ullrich
binary_update() {
47
	TGZ=$1
48 0a5aabe6 Colin Smith
	ERR_F="/tmp/bdiff.log"
49 9f7369a1 Bill Marquette
	rm ${ERR_F} 2>/dev/null
50 8a1691dd Scott Ullrich
	/bin/mkdir /tmp/patched /tmp/patches 2>>${ERR_F}
51 704ea918 Scott Ullrich
	backup_chflags
52
	remove_chflags
53 b8575f61 Bill Marquette
	cd /tmp/patches
54 316d02b3 Scott Ullrich
	for i in `/usr/bin/tar tvzf $TGZ | egrep -v "(^d|_md5)" | nawk '{print $9;}'`;
55 2b61eeb1 Scott Ullrich
	 do
56 03485a22 Bill Marquette
	   FILE=`basename ${i}`
57 3200f985 Bill Marquette
	   echo "Working on ${i}"
58 e7fb2f76 Bill Marquette
	   # Untar patch file and md5 files
59 9f7369a1 Bill Marquette
	   /usr/bin/tar xzf ${TGZ} ${i} ${i}.old_file_md5 ${i}.new_patch_md5 ${i}.new_file_md5 2>>${ERR_F}
60 e7fb2f76 Bill Marquette
61
	   # Apply patch - oldfile newfile patchfile
62 9f7369a1 Bill Marquette
	   /usr/local/bin/bspatch /${i} /tmp/patched/${FILE} /tmp/patches/${i} 2>>${ERR_F}
63 3200f985 Bill Marquette
64 9f7369a1 Bill Marquette
	   OLD_FILE_MD5=`cat /tmp/patches/${i}.old_file_md5 2>/dev/null`
65
	   NEW_PATCH_MD5=`cat /tmp/patches/${i}.new_patch_md5 2>/dev/null`
66
	   NEW_FILE_MD5=`cat /tmp/patches/${i}.new_file_md5 2>/dev/null`
67
	   PATCHED_MD5=`/sbin/md5 -q /tmp/patched/${FILE} 2>/dev/null`
68 3200f985 Bill Marquette
69
	   if [ "$PATCHED_MD5" = "$NEW_PATCH_MD5" ]; then
70 9f7369a1 Bill Marquette
		/usr/bin/install -S  /tmp/patched/${FILE} /${i}
71 e607da61 Scott Ullrich
	   else
72 9f7369a1 Bill Marquette
		#echo "${i} file does not match intended final md5."
73
		echo "${i} file does not match intended final md5." >> ${ERR_F}
74 e607da61 Scott Ullrich
	   fi
75 dbb59707 Scott Ullrich
		
76
	   chflags noschg /tmp/patched/${FILE} /tmp/patches/${i} /tmp/patches/${i}.*
77
	   /bin/rm -f /tmp/patched/${FILE} >> ${ERR_F}
78
	   /bin/rm -f /tmp/patches/${i} >> ${ERR_F}
79
	   /bin/rm -f /tmp/patches/${i}.* >> ${ERR_F}
80 e607da61 Scott Ullrich
	done
81 8a1691dd Scott Ullrich
	/bin/rm -rf /tmp/patched /tmp/patches >> ${ERR_F}
82 704ea918 Scott Ullrich
	restore_chflags
83 e607da61 Scott Ullrich
}
84
85 24fbf166 Scott Ullrich
case $ACTION in
86 5b237745 Scott Ullrich
enable)
87 242c837f Scott Ullrich
	#/sbin/mount_mfs -s 15360 -T qp120at -b 8192 -f 1024 dummy /ftmp \
88
	#	> /dev/null 2>&1
89 5b237745 Scott Ullrich
	;;
90 18ff56f2 Scott Ullrich
auto)
91 704ea918 Scott Ullrich
	backup_chflags
92
	remove_chflags
93 18ff56f2 Scott Ullrich
	/etc/rc.firmware_auto
94 704ea918 Scott Ullrich
	restore_chflags
95 18ff56f2 Scott Ullrich
	;;
96 5b237745 Scott Ullrich
upgrade)
97
	# wait 5 seconds before beginning
98
	sleep 5
99 704ea918 Scott Ullrich
	backup_chflags
100
	remove_chflags
101 2b61eeb1 Scott Ullrich
102 5b237745 Scott Ullrich
	exec </dev/console >/dev/console 2>/dev/console
103
104
	echo
105 be3239e3 Scott Ullrich
	echo "Firmware upgrade in progress..."  | logger -p daemon.info -i -t Upgrade
106 cba9c643 Scott Ullrich
	echo "Firmware upgrade in progress..." | wall
107 5b237745 Scott Ullrich
108
	# backup config
109
	mkdir /tmp/configbak
110
	cp -p /conf/* /tmp/configbak
111
112
	# unmount /cf
113
	/sbin/umount -f /cf
114
115
	# dd image onto card
116 24fbf166 Scott Ullrich
	if [ -r $IMG ]; then
117
		/usr/bin/gunzip -S "" -c $IMG | dd of=/dev/r$CFDEVICE bs=16k > /dev/null 2>&1
118 5b237745 Scott Ullrich
		echo "Image installed."
119
	fi
120
121
	# mount /cf
122
	/sbin/mount -w -o noatime /cf
123
124
	# restore config
125
	cp -p /tmp/configbak/* /conf
126
127 390c8e72 Scott Ullrich
	restore_chflags
128
129 8652a410 Scott Ullrich
	rm -f /var/run/firmware.lock
130
131 21fbee83 Scott Ullrich
        /bin/sync
132
        sleep 5
133
134 7d894a89 Colin Smith
	echo "Done."
135 d0b3b0b1 Scott Ullrich
136
	# If /tmp/post_upgrade_command exists after update
137
	# then execute the command.
138
	if [ -f /tmp/post_upgrade_command ]; then
139
		sh /tmp/post_upgrade_command
140
	fi
141
142 2b61eeb1 Scott Ullrich
	# If the archive has unpacked a file called
143
	# /tmp/no_upgrade_reboot_required then do
144
	# not reboot after upgrade.
145
	if [ -f /tmp/no_upgrade_reboot_required ]; then
146
		rm /tmp/no_upgrade_reboot_required
147
	else
148 8f798977 Scott Ullrich
		rm -f /var/run/config.lock
149 2b61eeb1 Scott Ullrich
		sh /etc/rc.reboot
150
	fi
151 4668f9f7 Scott Ullrich
	;;
152 b00d1ade Scott Ullrich
	pfSenseupgrade)
153
		# wait 5 seconds before beginning
154
		sleep 5
155
		# Sanity check - bail early if there's no firmware file!
156
		if [ ! -r $IMG ]; then
157
			echo "2nd parameter has not been passed or file does not exist. Exiting." | logger -p daemon.info -i -t Upgrade
158
			exit
159
		fi
160
		backup_chflags
161
		remove_chflags
162
		echo "Firmware upgrade in progress..."  | logger -p daemon.info -i -t Upgrade
163
		# backup config
164
		/bin/mkdir -p /tmp/configbak
165
		cp -p /conf/* /tmp/configbak 2>/dev/null
166
		# mount /cf
167
		/etc/rc.conf_mount_rw
168
		/sbin/mount -w -o noatime /cf 2>/dev/null
169
		/sbin/mount -w -o noatime /   2>/dev/null
170 41235422 Scott Ullrich
		NEWIMG="`echo $IMG | sed 's/.tgz/.tar/'`"
171 e725bc6a Scott Ullrich
		if [ -f $NEWIMG ]; then
172
			rm $NEWIMG
173
		fi
174 c01443f1 Scott Ullrich
		echo "Extracting $IMG." | logger -p daemon.info -i -t Upgrade
175
		gzip -d $IMG
176 41235422 Scott Ullrich
		echo "Installing $NEWIMG." | logger -p daemon.info -i -t Upgrade
177 c01443f1 Scott Ullrich
		for i in `/usr/bin/tar tvf $NEWIMG | egrep "(^d)" | nawk '{print $9;}'`;
178 d0b4f116 Scott Ullrich
		do
179
			if [ ! -d /$i ]; then
180
				# directory does not exist, create it
181
				mkdir -p /$i
182
			fi
183
		done		
184
		for i in `/usr/bin/tar tvf $NEWIMG | egrep -v "(^d|_md5)" | nawk '{print $9;}'`;
185
		do
186 b00d1ade Scott Ullrich
			FILE=`basename ${i}`
187 afadfc62 Scott Ullrich
			echo "Working on ${FILE} - /${i}"
188
			echo "Working on ${FILE} - /${i}" | logger -p daemon.info -i -t Upgrade
189 41235422 Scott Ullrich
			(cd /tmp && /usr/bin/tar xf ${NEWIMG} ${i})
190 c01443f1 Scott Ullrich
			echo "Installing /usr/bin/install -S /tmp/${i} /${i}" 
191 b00d1ade Scott Ullrich
			echo "Installing /${i}" | logger -p daemon.info -i -t Upgrade
192 f8c978e5 Scott Ullrich
			/usr/bin/install -S "/tmp/${i}" "/${i}"
193
			chflags noschg "/tmp/${i}"
194 c01443f1 Scott Ullrich
			chflags noschg "/${i}"
195 f8c978e5 Scott Ullrich
			/bin/rm -f "/tmp/${i}"
196 b00d1ade Scott Ullrich
		done
197
		/usr/bin/find / -name CVS -exec rm -fr {} \;
198
		echo "Image installed $IMG." | logger -p daemon.info -i -t Upgrade
199
	    # process custom image if its passed
200
	    if [ $# -eq 3 ]; then
201
		    if [ -f $CUSTOMIMG ]; then
202
		        echo "Custom image $CUSTOMIMG found." | logger -p daemon.info -i -t Upgrade
203
		        echo "Custom image ($CUSTOMIMG) found."
204
		        PWD_DIR=`pwd`
205
		        cd / && /usr/bin/tar xzPUf $CUSTOMIMG | logger -p daemon.info -i -t Upgrade
206
		        cd $PWD_DIR
207
		        echo "Custom image $CUSTOMIMG installed." | logger -p daemon.info -i -t Upgrade
208
		    fi
209 97ab7fdc Scott Ullrich
	    fi
210 b00d1ade Scott Ullrich
		# restore config
211
		cp -p /tmp/configbak/* /conf 2>/dev/null
212
		# restore /etc symlinks
213
		rm /etc/hosts
214
		ln -s /var/etc/hosts /etc/hosts
215
		restore_chflags
216
		rm -f /var/run/firmware.lock
217
		# Remove upgrade file
218
		rm -f $IMG
219
		/bin/sync
220
		sleep 5
221
		# remount /cf ro
222
		rm -rf /etc/rc.conf
223
		rm -rf /etc/motd
224
		find / -name CVS -type d -exec rm {} \;
225
		rm -rf /usr/savecore/*
226
		/etc/rc.conf_mount_ro
227
		/sbin/umount -f /cf 2>/dev/null
228
		/sbin/mount -r /cf 2>/dev/null
229
		/sbin/umount -f / 2>/dev/null
230
		/sbin/mount -r / 2>/dev/null
231
		if [ -e /etc/init_bootloader.sh ]; then
232
			sh /etc/init_bootloader.sh
233
		fi
234 5d14cbff Scott Ullrich
		rm $NEWIMG
235 b00d1ade Scott Ullrich
		/bin/sync
236
		echo "Done." | logger -p daemon.info -i -t Upgrade
237
		# If /tmp/post_upgrade_command exists after update
238
		# then execute the command.
239
		if [ -f /tmp/post_upgrade_command ]; then
240
			sh /tmp/post_upgrade_command
241
		fi
242
		# If the archive has unpacked a file called
243
		# /tmp/no_upgrade_reboot_required then do
244
		# not reboot after upgrade.
245
		if [ -f /tmp/no_upgrade_reboot_required ]; then
246
			rm /tmp/no_upgrade_reboot_required
247
		else
248
			rm -f /var/run/config.lock
249
			sh /etc/rc.reboot
250
		fi
251
		;;
252 f704d5b0 Colin Smith
delta_update)
253 704ea918 Scott Ullrich
	backup_chflags
254
	remove_chflags
255 24fbf166 Scott Ullrich
	binary_update $IMG
256 704ea918 Scott Ullrich
	restore_chflags
257 58fb67ec Scott Ullrich
	rm -rf /etc/rc.conf
258
	rm -rf /etc/motd
259
	find / -name CVS -type d -exec rm {} \;
260
	rm -rf /usr/savecore/*
261
	/etc/rc.conf_mount_ro
262
	/sbin/umount -f /cf 2>/dev/null
263
	/sbin/mount -r /cf 2>/dev/null
264
	/sbin/umount -f / 2>/dev/null
265
	/sbin/mount -r / 2>/dev/null
266
	if [ -e /etc/init_bootloader.sh ]; then
267
		sh /etc/init_bootloader.sh
268
	fi
269 2b61eeb1 Scott Ullrich
270 3db58e13 Scott Ullrich
	;;
271 8b33f440 Scott Ullrich
esac
272 ac07aad1 Scott Ullrich