Project

General

Profile

Download (6.37 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
	#exec </dev/console >/dev/console 2>/dev/console
165

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

    
168
	# backup config
169
	/bin/mkdir -p /tmp/configbak
170
	cp -p /conf/* /tmp/configbak 2>/dev/null
171
	# mount /cf
172
	/etc/rc.conf_mount_rw
173
	/sbin/mount -w -o noatime /cf 2>/dev/null
174
	/sbin/mount -w -o noatime /   2>/dev/null
175

    
176
	# tar explode image onto hd
177
	echo "Installing $IMG." | logger -p daemon.info -i -t Upgrade
178
	cd / && /usr/bin/tar xzUPf $IMG | logger -p daemon.info -i -t Upgrade
179
	/usr/bin/find / -name CVS -exec rm -fr {} \;
180
	echo "Image installed $IMG." | logger -p daemon.info -i -t Upgrade
181

    
182
    # process custom image if its passed
183
    if [ $# -eq 3 ]; then
184
	    if [ -f $CUSTOMIMG ]; then
185
	        echo "Custom image $CUSTOMIMG found." | logger -p daemon.info -i -t Upgrade
186
	        echo "Custom image ($CUSTOMIMG) found."
187
	        PWD_DIR=`pwd`
188
	        cd / && /usr/bin/tar xzPUf $CUSTOMIMG | logger -p daemon.info -i -t Upgrade
189
	        cd $PWD_DIR
190
	        echo "Custom image $CUSTOMIMG installed." | logger -p daemon.info -i -t Upgrade
191
	    fi
192
    fi
193

    
194
	# restore config
195
	cp -p /tmp/configbak/* /conf 2>/dev/null
196

    
197
	# restore /etc symlinks
198
	rm /etc/hosts
199
	ln -s /var/etc/hosts /etc/hosts
200

    
201
	restore_chflags
202

    
203
	# Remove upgrade file
204
	rm -f $IMG
205

    
206
	rm -f /var/run/firmware.lock
207

    
208
	/bin/sync
209
	sleep 5
210

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

    
227
	# If /tmp/post_upgrade_command exists after update
228
	# then execute the command.
229
	if [ -f /tmp/post_upgrade_command ]; then
230
		sh /tmp/post_upgrade_command
231
	fi
232

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

    
261
	;;
262
esac
263

    
264

    
(38-38/79)