Project

General

Profile

Download (6.62 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/bin/sh
2

    
3
# $Id$
4
# /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
#CFDEVICE=`cat /var/etc/cfdevice`
11

    
12
exec 3>&2 2>>/tmp/firmware_update.log
13

    
14
export ACTION=$1
15
export IMG=$2
16
if [ $# -eq 3 ]; then
17
	export CUSTOMIMG=$3
18
fi
19

    
20
if [ $ACTION != "upgrade" ]; then
21
	/sbin/umount -f /ftmp > /dev/null 2>&1
22
fi
23

    
24
backup_chflags() {
25
	TOPROCESS="bin lib libexec sbin usr"
26
	for files in $TOPROCESS; do
27
		/usr/sbin/mtree -Pcp /${files} | bzip2 -9 > /tmp/chflags.dist.${files}.bz2 | logger -p daemon.info -i -t UpgradeFlags
28
	done
29
}
30

    
31
restore_chflags() {
32
	TOPROCESS="bin lib libexec sbin usr"
33
	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
}
37

    
38
remove_chflags() {
39
	TOPROCESS="bin lib libexec sbin usr"
40
	for files in $TOPROCESS; do
41
		/bin/chflags -R noschg /${files}
42
		/bin/chmod -R u+rw /${files}
43
	done
44
}
45

    
46
binary_update() {
47
	TGZ=$1
48
	ERR_F="/tmp/bdiff.log"
49
	rm ${ERR_F} 2>/dev/null
50
	/bin/mkdir /tmp/patched /tmp/patches 2>>${ERR_F}
51
	backup_chflags
52
	remove_chflags
53
	cd /tmp/patches
54
	for i in `/usr/bin/tar tvzf $TGZ | egrep -v "(^d|_md5)" | nawk '{print $9;}'`;
55
	 do
56
	   FILE=`basename ${i}`
57
	   echo "Working on ${i}"
58
	   # Untar patch file and md5 files
59
	   /usr/bin/tar xzf ${TGZ} ${i} ${i}.old_file_md5 ${i}.new_patch_md5 ${i}.new_file_md5 2>>${ERR_F}
60

    
61
	   # Apply patch - oldfile newfile patchfile
62
	   /usr/local/bin/bspatch /${i} /tmp/patched/${FILE} /tmp/patches/${i} 2>>${ERR_F}
63

    
64
	   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

    
69
	   if [ "$PATCHED_MD5" = "$NEW_PATCH_MD5" ]; then
70
		/usr/bin/install -S  /tmp/patched/${FILE} /${i}
71
	   else
72
		#echo "${i} file does not match intended final md5."
73
		echo "${i} file does not match intended final md5." >> ${ERR_F}
74
	   fi
75

    
76
	   /bin/rm /tmp/patched/${FILE} >> ${ERR_F}
77
	   /bin/rm /tmp/patches/${i} >> ${ERR_F}
78
	   /bin/rm /tmp/patches/${i}.* >> ${ERR_F}
79
	done
80
	/bin/rm -rf /tmp/patched /tmp/patches >> ${ERR_F}
81
	restore_chflags
82
}
83

    
84
case $ACTION in
85
enable)
86
	#/sbin/mount_mfs -s 15360 -T qp120at -b 8192 -f 1024 dummy /ftmp \
87
	#	> /dev/null 2>&1
88
	;;
89
auto)
90
	backup_chflags
91
	remove_chflags
92
	/etc/rc.firmware_auto
93
	restore_chflags
94
	;;
95
upgrade)
96
	# wait 5 seconds before beginning
97
	sleep 5
98
	backup_chflags
99
	remove_chflags
100

    
101
	exec </dev/console >/dev/console 2>/dev/console
102

    
103
	echo
104
	echo "Firmware upgrade in progress..."  | logger -p daemon.info -i -t Upgrade
105
	echo "Firmware upgrade in progress..." | wall
106

    
107
	# backup config
108
	mkdir /tmp/configbak
109
	cp -p /conf/* /tmp/configbak
110

    
111
	# unmount /cf
112
	/sbin/umount -f /cf
113

    
114
	# dd image onto card
115
	if [ -r $IMG ]; then
116
		/usr/bin/gunzip -S "" -c $IMG | dd of=/dev/r$CFDEVICE bs=16k > /dev/null 2>&1
117
		echo "Image installed."
118
	fi
119

    
120
	# mount /cf
121
	/sbin/mount -w -o noatime /cf
122

    
123
	# restore config
124
	cp -p /tmp/configbak/* /conf
125

    
126
	restore_chflags
127

    
128
	rm -f /var/run/firmware.lock
129

    
130
        /bin/sync
131
        sleep 5
132

    
133
	echo "Done."
134

    
135
	# If /tmp/post_upgrade_command exists after update
136
	# then execute the command.
137
	if [ -f /tmp/post_upgrade_command ]; then
138
		sh /tmp/post_upgrade_command
139
	fi
140

    
141
	# If the archive has unpacked a file called
142
	# /tmp/no_upgrade_reboot_required then do
143
	# not reboot after upgrade.
144
	if [ -f /tmp/no_upgrade_reboot_required ]; then
145
		rm /tmp/no_upgrade_reboot_required
146
	else
147
		rm -f /var/run/config.lock
148
		sh /etc/rc.reboot
149
	fi
150
	;;
151
pfSenseupgrade)
152
	# wait 5 seconds before beginning
153
	sleep 5
154

    
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

    
161
	backup_chflags
162
	remove_chflags
163

    
164
	# Do we have a pre-upgrade hook in the update file?
165
	if [ `tar tvzf $IMG | grep /tmp/pre_upgrade_command | wc -l` -gt 0 ]; then 
166
		tar xzvf $IMG -C / ./tmp/pre_upgrade_command
167
		chmod a+rx /tmp/pre_upgrade_command
168
		sh /tmp/pre_upgrade_command
169
	fi
170

    
171
	#exec </dev/console >/dev/console 2>/dev/console
172

    
173
	echo "Firmware upgrade in progress..."  | logger -p daemon.info -i -t Upgrade
174

    
175
	# backup config
176
	/bin/mkdir -p /tmp/configbak
177
	cp -p /conf/* /tmp/configbak 2>/dev/null
178
	# mount /cf
179
	/etc/rc.conf_mount_rw
180
	/sbin/mount -w -o noatime /cf 2>/dev/null
181
	/sbin/mount -w -o noatime /   2>/dev/null
182

    
183
	# tar explode image onto hd
184
	echo "Installing $IMG." | logger -p daemon.info -i -t Upgrade
185
	cd / && /usr/bin/tar xzUPf $IMG | logger -p daemon.info -i -t Upgrade
186
	/usr/bin/find / -name CVS -exec rm -fr {} \;
187
	echo "Image installed $IMG." | logger -p daemon.info -i -t Upgrade
188

    
189
    # process custom image if its passed
190
    if [ $# -eq 3 ]; then
191
	    if [ -f $CUSTOMIMG ]; then
192
	        echo "Custom image $CUSTOMIMG found." | logger -p daemon.info -i -t Upgrade
193
	        echo "Custom image ($CUSTOMIMG) found."
194
	        PWD_DIR=`pwd`
195
	        cd / && /usr/bin/tar xzPUf $CUSTOMIMG | logger -p daemon.info -i -t Upgrade
196
	        cd $PWD_DIR
197
	        echo "Custom image $CUSTOMIMG installed." | logger -p daemon.info -i -t Upgrade
198
	    fi
199
    fi
200

    
201
	# restore config
202
	cp -p /tmp/configbak/* /conf 2>/dev/null
203

    
204
	# restore /etc symlinks
205
	rm /etc/hosts
206
	ln -s /var/etc/hosts /etc/hosts
207

    
208
	restore_chflags
209

    
210
	# Remove upgrade file
211
	rm -f $IMG
212

    
213
	rm -f /var/run/firmware.lock
214

    
215
	/bin/sync
216
	sleep 5
217

    
218
	# remount /cf ro
219
	rm -rf /etc/rc.conf
220
	rm -rf /etc/motd
221
	find / -name CVS -type d -exec rm {} \;
222
	rm -rf /usr/savecore/*
223
	/etc/rc.conf_mount_ro
224
	/sbin/umount -f /cf 2>/dev/null
225
	/sbin/mount -r /cf 2>/dev/null
226
	/sbin/umount -f / 2>/dev/null
227
	/sbin/mount -r / 2>/dev/null
228
	if [ -e /etc/init_bootloader.sh ]; then
229
		sh /etc/init_bootloader.sh
230
	fi
231
	/bin/sync
232
	echo "Done." | logger -p daemon.info -i -t Upgrade
233

    
234
	# If /tmp/post_upgrade_command exists after update
235
	# then execute the command.
236
	if [ -f /tmp/post_upgrade_command ]; then
237
		sh /tmp/post_upgrade_command
238
	fi
239

    
240
	# If the archive has unpacked a file called
241
	# /tmp/no_upgrade_reboot_required then do
242
	# not reboot after upgrade.
243
	if [ -f /tmp/no_upgrade_reboot_required ]; then
244
		rm /tmp/no_upgrade_reboot_required
245
	else
246
		rm -f /var/run/config.lock
247
		sh /etc/rc.reboot
248
	fi
249
	;;
250
delta_update)
251
	backup_chflags
252
	remove_chflags
253
	binary_update $IMG
254
	restore_chflags
255
	rm -rf /etc/rc.conf
256
	rm -rf /etc/motd
257
	find / -name CVS -type d -exec rm {} \;
258
	rm -rf /usr/savecore/*
259
	/etc/rc.conf_mount_ro
260
	/sbin/umount -f /cf 2>/dev/null
261
	/sbin/mount -r /cf 2>/dev/null
262
	/sbin/umount -f / 2>/dev/null
263
	/sbin/mount -r / 2>/dev/null
264
	if [ -e /etc/init_bootloader.sh ]; then
265
		sh /etc/init_bootloader.sh
266
	fi
267

    
268
	;;
269
esac
270

    
271

    
(43-43/85)