summaryrefslogtreecommitdiffstats
path: root/etc/rc.firmware
blob: a6e0082602028691828338716c888c928b68accb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/bin/sh

# $Id$
# /etc/rc.firmware
# part of m0n0wall (http://neon1.net/m0n0wall)
#
# Copyright (C) 2003 Manuel Kasper <mk@neon1.net>.
# All rights reserved.

#CFDEVICE=`cat /var/etc/cfdevice`

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

export ACTION=$1
export IMG=$2
if [ $# -eq 3 ]; then
	export CUSTOMIMG=$3
fi

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

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

restore_chflags() {
	TOPROCESS="bin lib libexec sbin usr"
	for files in $TOPROCESS; do
		cd / && /usr/bin/bzcat /tmp/chflags.dist.${files}.bz2 | /usr/sbin/mtree -PU -p /${files} | logger -p daemon.info -i -t UpgradeFlags
	done
}

remove_chflags() {
	TOPROCESS="bin lib libexec sbin usr"
	for files in $TOPROCESS; do
		/bin/chflags -R noschg /${files}
		/bin/chmod -R u+rw /${files}
	done
}

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

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

	   OLD_FILE_MD5=`cat /tmp/patches/${i}.old_file_md5 2>/dev/null`
	   NEW_PATCH_MD5=`cat /tmp/patches/${i}.new_patch_md5 2>/dev/null`
	   NEW_FILE_MD5=`cat /tmp/patches/${i}.new_file_md5 2>/dev/null`
	   PATCHED_MD5=`/sbin/md5 -q /tmp/patched/${FILE} 2>/dev/null`

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

	   /bin/rm /tmp/patched/${FILE} >> ${ERR_F}
	   /bin/rm /tmp/patches/${i} >> ${ERR_F}
	   /bin/rm /tmp/patches/${i}.* >> ${ERR_F}
	done
	/bin/rm -rf /tmp/patched /tmp/patches >> ${ERR_F}
	restore_chflags
}

case $ACTION in
enable)
	#/sbin/mount_mfs -s 15360 -T qp120at -b 8192 -f 1024 dummy /ftmp \
	#	> /dev/null 2>&1
	;;
auto)
	backup_chflags
	remove_chflags
	/etc/rc.firmware_auto
	restore_chflags
	;;
upgrade)
	# wait 5 seconds before beginning
	sleep 5
	backup_chflags
	remove_chflags

	exec </dev/console >/dev/console 2>/dev/console

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

	# backup config
	mkdir /tmp/configbak
	cp -p /conf/* /tmp/configbak

	# unmount /cf
	/sbin/umount -f /cf

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

	# mount /cf
	/sbin/mount -w -o noatime /cf

	# restore config
	cp -p /tmp/configbak/* /conf

	restore_chflags

	rm -f /var/run/firmware.lock

        /bin/sync
        sleep 5

	echo "Done."

	# If /tmp/post_upgrade_command exists after update
	# then execute the command.
	if [ -f /tmp/post_upgrade_command ]; then
		sh /tmp/post_upgrade_command
	fi

	# If the archive has unpacked a file called
	# /tmp/no_upgrade_reboot_required then do
	# not reboot after upgrade.
	if [ -f /tmp/no_upgrade_reboot_required ]; then
		rm /tmp/no_upgrade_reboot_required
	else
		rm -f /var/run/config.lock
		sh /etc/rc.reboot
	fi
	;;
pfSenseupgrade)
	# wait 5 seconds before beginning
	sleep 5

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

	backup_chflags
	remove_chflags

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

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

	# backup config
	/bin/mkdir -p /tmp/configbak
	cp -p /conf/* /tmp/configbak 2>/dev/null
	# mount /cf
	/etc/rc.conf_mount_rw
	/sbin/mount -w -o noatime /cf 2>/dev/null
	/sbin/mount -w -o noatime /   2>/dev/null

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

    # process custom image if its passed
    if [ $# -eq 3 ]; then
	    if [ -f $CUSTOMIMG ]; then
	        echo "Custom image $CUSTOMIMG found." | logger -p daemon.info -i -t Upgrade
	        echo "Custom image ($CUSTOMIMG) found."
	        PWD_DIR=`pwd`
	        cd / && /usr/bin/tar xzPUf $CUSTOMIMG | logger -p daemon.info -i -t Upgrade
	        cd $PWD_DIR
	        echo "Custom image $CUSTOMIMG installed." | logger -p daemon.info -i -t Upgrade
	    fi
    fi

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

	# restore /etc symlinks
	rm /etc/hosts
	ln -s /var/etc/hosts /etc/hosts

	restore_chflags

	# Remove upgrade file
	rm -f $IMG

	rm -f /var/run/firmware.lock

	/bin/sync
	sleep 5

	# remount /cf ro
	rm -rf /etc/rc.conf
	rm -rf /etc/motd
	find / -name CVS -type d -exec rm {} \;
	rm -rf /usr/savecore/*
	/etc/rc.conf_mount_ro
	/sbin/umount -f /cf 2>/dev/null
	/sbin/mount -r /cf 2>/dev/null
	/sbin/umount -f / 2>/dev/null
	/sbin/mount -r / 2>/dev/null
	if [ -e /etc/init_bootloader.sh ]; then
		sh /etc/init_bootloader.sh
	fi
	/bin/sync
	echo "Done." | logger -p daemon.info -i -t Upgrade

	# If /tmp/post_upgrade_command exists after update
	# then execute the command.
	if [ -f /tmp/post_upgrade_command ]; then
		sh /tmp/post_upgrade_command
	fi

	# If the archive has unpacked a file called
	# /tmp/no_upgrade_reboot_required then do
	# not reboot after upgrade.
	if [ -f /tmp/no_upgrade_reboot_required ]; then
		rm /tmp/no_upgrade_reboot_required
	else
		rm -f /var/run/config.lock
		sh /etc/rc.reboot
	fi
	;;
delta_update)
	backup_chflags
	remove_chflags
	binary_update $IMG
	restore_chflags
	rm -rf /etc/rc.conf
	rm -rf /etc/motd
	find / -name CVS -type d -exec rm {} \;
	rm -rf /usr/savecore/*
	/etc/rc.conf_mount_ro
	/sbin/umount -f /cf 2>/dev/null
	/sbin/mount -r /cf 2>/dev/null
	/sbin/umount -f / 2>/dev/null
	/sbin/mount -r / 2>/dev/null
	if [ -e /etc/init_bootloader.sh ]; then
		sh /etc/init_bootloader.sh
	fi

	;;
esac


OpenPOWER on IntegriCloud