Project

General

Profile

Download (6.92 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
		echo "Extracting $IMG." | logger -p daemon.info -i -t Upgrade
171
		gzip -d $IMG
172
		NEWIMG="`echo $IMG | sed 's/.tgz/.tar/'`"
173
		echo "Installing $NEWIMG." | logger -p daemon.info -i -t Upgrade
174 f8c978e5 Scott Ullrich
		for i in `/usr/bin/tar tvzf $NEWIMG`;
175 b00d1ade Scott Ullrich
		 do
176
			FILE=`basename ${i}`
177 afadfc62 Scott Ullrich
			echo "Working on ${FILE} - /${i}"
178
			echo "Working on ${FILE} - /${i}" | logger -p daemon.info -i -t Upgrade
179 41235422 Scott Ullrich
			(cd /tmp && /usr/bin/tar xf ${NEWIMG} ${i})
180 b00d1ade Scott Ullrich
			echo "Installing /${i}" 
181
			echo "Installing /${i}" | logger -p daemon.info -i -t Upgrade
182 f8c978e5 Scott Ullrich
			/usr/bin/install -S "/tmp/${i}" "/${i}"
183
			chflags noschg "/tmp/${i}"
184
			/bin/rm -f "/tmp/${i}"
185 b00d1ade Scott Ullrich
		done
186
		/usr/bin/find / -name CVS -exec rm -fr {} \;
187
		echo "Image installed $IMG." | logger -p daemon.info -i -t Upgrade
188
	    # process custom image if its passed
189
	    if [ $# -eq 3 ]; then
190
		    if [ -f $CUSTOMIMG ]; then
191
		        echo "Custom image $CUSTOMIMG found." | logger -p daemon.info -i -t Upgrade
192
		        echo "Custom image ($CUSTOMIMG) found."
193
		        PWD_DIR=`pwd`
194
		        cd / && /usr/bin/tar xzPUf $CUSTOMIMG | logger -p daemon.info -i -t Upgrade
195
		        cd $PWD_DIR
196
		        echo "Custom image $CUSTOMIMG installed." | logger -p daemon.info -i -t Upgrade
197
		    fi
198 97ab7fdc Scott Ullrich
	    fi
199 b00d1ade Scott Ullrich
		# restore config
200
		cp -p /tmp/configbak/* /conf 2>/dev/null
201
		# restore /etc symlinks
202
		rm /etc/hosts
203
		ln -s /var/etc/hosts /etc/hosts
204
		restore_chflags
205
		rm -f /var/run/firmware.lock
206
		# Remove upgrade file
207
		rm -f $IMG
208
		/bin/sync
209
		sleep 5
210
		# remount /cf ro
211
		rm -rf /etc/rc.conf
212
		rm -rf /etc/motd
213
		find / -name CVS -type d -exec rm {} \;
214
		rm -rf /usr/savecore/*
215
		/etc/rc.conf_mount_ro
216
		/sbin/umount -f /cf 2>/dev/null
217
		/sbin/mount -r /cf 2>/dev/null
218
		/sbin/umount -f / 2>/dev/null
219
		/sbin/mount -r / 2>/dev/null
220
		if [ -e /etc/init_bootloader.sh ]; then
221
			sh /etc/init_bootloader.sh
222
		fi
223 5d14cbff Scott Ullrich
		rm $NEWIMG
224 b00d1ade Scott Ullrich
		/bin/sync
225
		echo "Done." | logger -p daemon.info -i -t Upgrade
226
		# If /tmp/post_upgrade_command exists after update
227
		# then execute the command.
228
		if [ -f /tmp/post_upgrade_command ]; then
229
			sh /tmp/post_upgrade_command
230
		fi
231
		# If the archive has unpacked a file called
232
		# /tmp/no_upgrade_reboot_required then do
233
		# not reboot after upgrade.
234
		if [ -f /tmp/no_upgrade_reboot_required ]; then
235
			rm /tmp/no_upgrade_reboot_required
236
		else
237
			rm -f /var/run/config.lock
238
			sh /etc/rc.reboot
239
		fi
240
		;;
241 f704d5b0 Colin Smith
delta_update)
242 704ea918 Scott Ullrich
	backup_chflags
243
	remove_chflags
244 24fbf166 Scott Ullrich
	binary_update $IMG
245 704ea918 Scott Ullrich
	restore_chflags
246 58fb67ec Scott Ullrich
	rm -rf /etc/rc.conf
247
	rm -rf /etc/motd
248
	find / -name CVS -type d -exec rm {} \;
249
	rm -rf /usr/savecore/*
250
	/etc/rc.conf_mount_ro
251
	/sbin/umount -f /cf 2>/dev/null
252
	/sbin/mount -r /cf 2>/dev/null
253
	/sbin/umount -f / 2>/dev/null
254
	/sbin/mount -r / 2>/dev/null
255
	if [ -e /etc/init_bootloader.sh ]; then
256
		sh /etc/init_bootloader.sh
257
	fi
258 2b61eeb1 Scott Ullrich
259 3db58e13 Scott Ullrich
	;;
260 8b33f440 Scott Ullrich
esac
261 ac07aad1 Scott Ullrich