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
|
touch /var/run/firmware.lock
|
91
|
backup_chflags
|
92
|
remove_chflags
|
93
|
/etc/rc.firmware_auto
|
94
|
restore_chflags
|
95
|
;;
|
96
|
upgrade)
|
97
|
|
98
|
touch /var/run/firmware.lock
|
99
|
|
100
|
# wait 5 seconds before beginning
|
101
|
sleep 5
|
102
|
backup_chflags
|
103
|
remove_chflags
|
104
|
|
105
|
exec </dev/console >/dev/console 2>/dev/console
|
106
|
|
107
|
echo
|
108
|
echo "Firmware upgrade in progress..." | logger -p daemon.info -i -t Upgrade
|
109
|
echo "Firmware upgrade in progress..." | wall
|
110
|
|
111
|
# backup config
|
112
|
mkdir /tmp/configbak
|
113
|
cp -p /conf/* /tmp/configbak
|
114
|
|
115
|
# unmount /cf
|
116
|
/sbin/umount -f /cf
|
117
|
|
118
|
# dd image onto card
|
119
|
if [ -r $IMG ]; then
|
120
|
/usr/bin/gunzip -S "" -c $IMG | dd of=/dev/r$CFDEVICE bs=16k > /dev/null 2>&1
|
121
|
echo "Image installed."
|
122
|
fi
|
123
|
|
124
|
# mount /cf
|
125
|
/sbin/mount -w -o noatime /cf
|
126
|
|
127
|
# restore config
|
128
|
cp -p /tmp/configbak/* /conf
|
129
|
|
130
|
restore_chflags
|
131
|
|
132
|
rm -f /var/run/firmware.lock
|
133
|
|
134
|
/bin/sync
|
135
|
sleep 5
|
136
|
|
137
|
echo "Done."
|
138
|
|
139
|
# If /tmp/post_upgrade_command exists after update
|
140
|
# then execute the command.
|
141
|
if [ -f /tmp/post_upgrade_command ]; then
|
142
|
sh /tmp/post_upgrade_command
|
143
|
fi
|
144
|
|
145
|
# If the archive has unpacked a file called
|
146
|
# /tmp/no_upgrade_reboot_required then do
|
147
|
# not reboot after upgrade.
|
148
|
if [ -f /tmp/no_upgrade_reboot_required ]; then
|
149
|
rm /tmp/no_upgrade_reboot_required
|
150
|
else
|
151
|
rm -f /var/run/config.lock
|
152
|
sh /etc/rc.reboot
|
153
|
fi
|
154
|
;;
|
155
|
pfSenseupgrade)
|
156
|
|
157
|
touch /var/run/firmware.lock
|
158
|
|
159
|
# wait 1 seconds before beginning
|
160
|
sleep 1
|
161
|
|
162
|
# Sanity check - bail early if there's no firmware file!
|
163
|
if [ ! -r $IMG ]; then
|
164
|
echo "2nd parameter has not been passed or file does not exist. Exiting." | logger -p daemon.info -i -t Upgrade
|
165
|
exit
|
166
|
fi
|
167
|
|
168
|
backup_chflags
|
169
|
remove_chflags
|
170
|
|
171
|
# Do we have a pre-upgrade hook in the update file?
|
172
|
if [ `tar tvzf $IMG | grep /tmp/pre_upgrade_command | wc -l` -gt 0 ]; then
|
173
|
tar xzvf $IMG -C / ./tmp/pre_upgrade_command
|
174
|
chmod a+rx /tmp/pre_upgrade_command
|
175
|
sh /tmp/pre_upgrade_command
|
176
|
fi
|
177
|
|
178
|
#exec </dev/console >/dev/console 2>/dev/console
|
179
|
|
180
|
echo "Firmware upgrade in progress..." | logger -p daemon.info -i -t Upgrade
|
181
|
|
182
|
# backup config
|
183
|
/bin/mkdir -p /tmp/configbak
|
184
|
cp -p /conf/* /tmp/configbak 2>/dev/null
|
185
|
# mount /cf
|
186
|
/etc/rc.conf_mount_rw
|
187
|
/sbin/mount -w -o noatime /cf 2>/dev/null
|
188
|
/sbin/mount -w -o noatime / 2>/dev/null
|
189
|
|
190
|
# tar explode image onto hd
|
191
|
echo "Installing $IMG." | logger -p daemon.info -i -t Upgrade
|
192
|
cd / && /usr/bin/tar xzUPf $IMG | logger -p daemon.info -i -t Upgrade
|
193
|
/usr/bin/find / -name CVS -exec rm -fr {} \;
|
194
|
echo "Image installed $IMG." | logger -p daemon.info -i -t Upgrade
|
195
|
|
196
|
# process custom image if its passed
|
197
|
if [ $# -eq 3 ]; then
|
198
|
if [ -f $CUSTOMIMG ]; then
|
199
|
echo "Custom image $CUSTOMIMG found." | logger -p daemon.info -i -t Upgrade
|
200
|
echo "Custom image ($CUSTOMIMG) found."
|
201
|
PWD_DIR=`pwd`
|
202
|
cd / && /usr/bin/tar xzPUf $CUSTOMIMG | logger -p daemon.info -i -t Upgrade
|
203
|
cd $PWD_DIR
|
204
|
echo "Custom image $CUSTOMIMG installed." | logger -p daemon.info -i -t Upgrade
|
205
|
fi
|
206
|
fi
|
207
|
|
208
|
# restore config
|
209
|
cp -p /tmp/configbak/* /conf 2>/dev/null
|
210
|
|
211
|
# restore /etc symlinks
|
212
|
rm /etc/hosts
|
213
|
ln -s /var/etc/hosts /etc/hosts
|
214
|
|
215
|
restore_chflags
|
216
|
|
217
|
# Remove upgrade file
|
218
|
rm -f $IMG
|
219
|
|
220
|
if [ -e /etc/init_bootloader.sh ]; then
|
221
|
sh /etc/init_bootloader.sh
|
222
|
fi
|
223
|
|
224
|
# If /tmp/post_upgrade_command exists after update
|
225
|
# then execute the command.
|
226
|
if [ -f /tmp/post_upgrade_command ]; then
|
227
|
sh /tmp/post_upgrade_command
|
228
|
fi
|
229
|
|
230
|
# remount /cf ro
|
231
|
rm -rf /etc/rc.conf
|
232
|
rm -rf /etc/motd
|
233
|
find / -name CVS -type d -exec rm {} \;
|
234
|
rm -rf /usr/savecore/*
|
235
|
/etc/rc.conf_mount_ro
|
236
|
/sbin/umount -f /cf 2>/dev/null
|
237
|
/sbin/mount -r /cf 2>/dev/null
|
238
|
/sbin/umount -f / 2>/dev/null
|
239
|
/sbin/mount -r / 2>/dev/null
|
240
|
|
241
|
sleep 3
|
242
|
rm -f /var/run/firmware.lock
|
243
|
/bin/sync
|
244
|
sleep 2
|
245
|
echo "Done." | logger -p daemon.info -i -t Upgrade
|
246
|
|
247
|
# If the archive has unpacked a file called
|
248
|
# /tmp/no_upgrade_reboot_required then do
|
249
|
# not reboot after upgrade.
|
250
|
if [ -f /tmp/no_upgrade_reboot_required ]; then
|
251
|
rm /tmp/no_upgrade_reboot_required
|
252
|
else
|
253
|
rm -f /var/run/config.lock
|
254
|
sh /etc/rc.reboot
|
255
|
fi
|
256
|
|
257
|
;;
|
258
|
delta_update)
|
259
|
touch /var/run/firmware.lock
|
260
|
backup_chflags
|
261
|
remove_chflags
|
262
|
binary_update $IMG
|
263
|
restore_chflags
|
264
|
rm -rf /etc/rc.conf
|
265
|
rm -rf /etc/motd
|
266
|
find / -name CVS -type d -exec rm {} \;
|
267
|
rm -rf /usr/savecore/*
|
268
|
/etc/rc.conf_mount_ro
|
269
|
/sbin/umount -f /cf 2>/dev/null
|
270
|
/sbin/mount -r /cf 2>/dev/null
|
271
|
/sbin/umount -f / 2>/dev/null
|
272
|
/sbin/mount -r / 2>/dev/null
|
273
|
if [ -e /etc/init_bootloader.sh ]; then
|
274
|
sh /etc/init_bootloader.sh
|
275
|
fi
|
276
|
|
277
|
;;
|
278
|
esac
|
279
|
|
280
|
|