Project

General

Profile

Download (6.28 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
		sh /etc/rc.reboot
148
	fi
149
	;;
150
pfSenseupgrade)
151
	# wait 5 seconds before beginning
152
	sleep 5
153

    
154
	# Sanity check - bail early if there's no firmware file!
155
	if [ ! -r $IMG ]; then
156
		echo "2nd parameter has not been passed or file does not exist. Exiting." | logger -p daemon.info -i -t Upgrade
157
		exit
158
	fi
159

    
160
	backup_chflags
161
	remove_chflags
162

    
163
	#exec </dev/console >/dev/console 2>/dev/console
164

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

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

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

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

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

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

    
200
	restore_chflags
201

    
202
	rm -f /var/run/firmware.lock
203

    
204
	/bin/sync
205
	sleep 5
206

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

    
223
	# If /tmp/post_upgrade_command exists after update
224
	# then execute the command.
225
	if [ -f /tmp/post_upgrade_command ]; then
226
		sh /tmp/post_upgrade_command
227
	fi
228

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

    
256
	;;
257
esac
258

    
259

    
(37-37/76)