1
|
#!/bin/sh
|
2
|
|
3
|
# $Id$
|
4
|
# /etc/rc.firmware
|
5
|
# part of m0n0wall (http://neon1.net/m0n0wall)
|
6
|
# Copyright (C) 2005-2009 Scott Ullrich <sullrich@pfsense.org>.
|
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>>/cf/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
|
pfSenseNanoBSDupgrade)
|
156
|
|
157
|
# Sanity check - bail early if there's no firmware file!
|
158
|
if [ ! -r $IMG ]; then
|
159
|
echo "2nd parameter has not been passed or file does not exist. Exiting." | logger -p daemon.info -i -t Upgrade
|
160
|
exit 1
|
161
|
fi
|
162
|
|
163
|
touch /var/run/firmware.lock
|
164
|
|
165
|
echo "Firmware upgrade in progress..." | logger -p daemon.info -i -t Upgrade
|
166
|
|
167
|
# mount /cf
|
168
|
/etc/rc.conf_mount_rw
|
169
|
# backup config
|
170
|
/bin/mkdir -p /tmp/configbak
|
171
|
cp -p /conf/* /tmp/configbak 2>/dev/null
|
172
|
|
173
|
touch /cf/upgrade_log.txt
|
174
|
echo "" >> /cf/upgrade_log.txt
|
175
|
|
176
|
echo "Installing $IMG." | logger -p daemon.info -i -t Upgrade
|
177
|
echo "Installing $IMG." >> /cf/upgrade_log.txt
|
178
|
|
179
|
# resolve glabel label that we booted from
|
180
|
BOOT_DEVICE=`/sbin/mount | /usr/bin/grep pfsense | /usr/bin/cut -d'/' -f4 | /usr/bin/cut -d' ' -f1`
|
181
|
# resolve glabel to the real boot dev entry
|
182
|
REAL_BOOT_DEVICE=`/sbin/glabel list | /usr/bin/grep -B2 ufs/${BOOT_DEVICE} | /usr/bin/head -n 1 | /usr/bin/cut -f3 -d' '`
|
183
|
# grab the boot device, example ad1, ad0
|
184
|
BOOT_DRIVE=`/sbin/glabel list | /usr/bin/grep -B2 ufs/pfsense | /usr/bin/head -n 1 | /usr/bin/cut -f3 -d' ' | /usr/bin/cut -d's' -f1`
|
185
|
# test the slice. if we are on slice 1 we need to flash 2 and vica versa
|
186
|
if [ `echo $REAL_BOOT_DEVICE | /usr/bin/grep "s1"` ]; then
|
187
|
SLICE="2"
|
188
|
OLDSLICE="1"
|
189
|
TOFLASH="${BOOT_DRIVE}s${SLICE}"
|
190
|
COMPLETE_PATH="${BOOT_DRIVE}s${SLICE}a"
|
191
|
GLABEL_SLICE="pfsense1"
|
192
|
else
|
193
|
SLICE="1"
|
194
|
OLDSLICE="2"
|
195
|
TOFLASH="${BOOT_DRIVE}s${SLICE}"
|
196
|
COMPLETE_PATH="${BOOT_DRIVE}s${SLICE}a"
|
197
|
GLABEL_SLICE="pfsense0"
|
198
|
fi
|
199
|
|
200
|
echo "SLICE $SLICE" >> /cf/upgrade_log.txt
|
201
|
echo "OLDSLICE $OLDSLICE" >> /cf/upgrade_log.txt
|
202
|
echo "TOFLASH $TOFLASH" >> /cf/upgrade_log.txt
|
203
|
echo "COMPLETE_PATH $COMPLETE_PATH" >> /cf/upgrade_log.txt
|
204
|
echo "GLABEL_SLICE $GLABEL_SLICE" >> /cf/upgrade_log.txt
|
205
|
|
206
|
echo "" >> /cf/upgrade_log.txt
|
207
|
ls -lah $IMG >> /cf/upgrade_log.txt
|
208
|
echo "" >> /cf/upgrade_log.txt
|
209
|
mount >> /cf/upgrade_log.txt
|
210
|
echo "" >> /cf/upgrade_log.txt
|
211
|
top >> /cf/upgrade_log.txt
|
212
|
|
213
|
# Foot shooting is fun!
|
214
|
echo "" >> /cf/upgrade_log.txt
|
215
|
echo "/sbin/sysctl kern.geom.debugflags=16" >> /cf/upgrade_log.txt
|
216
|
/sbin/sysctl kern.geom.debugflags=16 >> /cf/upgrade_log.txt 2>&1
|
217
|
|
218
|
# Remove TOFLASH and get ready for new flash image
|
219
|
echo "" >> /cf/upgrade_log.txt
|
220
|
echo "dd if=/dev/zero of=/dev/${TOFLASH} bs=1m count=1" >> /cf/upgrade_log.txt
|
221
|
dd if=/dev/zero of=/dev/${TOFLASH} bs=1m count=1 >> /cf/upgrade_log.txt 2>&1
|
222
|
|
223
|
# Stream gzipped image to dd and explode image to new area
|
224
|
echo "" >> /cf/upgrade_log.txt
|
225
|
echo "/usr/bin/gunzip -S "" -c $IMG | /bin/dd of=/dev/${TOFLASH} bs=16k" >> /cf/upgrade_log.txt
|
226
|
/usr/bin/gunzip -S "" -c $IMG | /bin/dd of=/dev/${TOFLASH} bs=16k >> /cf/upgrade_log.txt 2>&1
|
227
|
|
228
|
# Ensure that our new system is sound and bail if it is not and file a notice
|
229
|
echo "" >> /cf/upgrade_log.txt
|
230
|
echo "/sbin/fsck_ffs -y /dev/$COMPLETE_PATH" >> /cf/upgrade_log.txt
|
231
|
/sbin/fsck_ffs -y /dev/$COMPLETE_PATH >> /cf/upgrade_log.txt 2>&1
|
232
|
if [ $? != 0 ]; then
|
233
|
/usr/local/bin/php -q -d auto_prepend_file=config.inc <<ENDOFF
|
234
|
<?php
|
235
|
require_once("globals.inc");
|
236
|
require_once("functions.inc");
|
237
|
file_notice("UpgradeFailure", "{\$g['product_name']} upgrade has failed. Your system has been left in a usable state.", "UpgradeFailure", "");
|
238
|
?>
|
239
|
ENDOFF
|
240
|
rm /var/run/firmware.lock
|
241
|
exit 1
|
242
|
fi
|
243
|
|
244
|
# Add back the corresponding glabel
|
245
|
echo "" >> /cf/upgrade_log.txt
|
246
|
echo "/sbin/tunefs -L pfsense${GLABEL_SLICE} /dev/$COMPLETE_PATH" >> /cf/upgrade_log.txt
|
247
|
/sbin/tunefs -L ${GLABEL_SLICE} /dev/$COMPLETE_PATH >> /cf/upgrade_log.txt 2>&1
|
248
|
|
249
|
echo "" >> /cf/upgrade_log.txt
|
250
|
echo "/usr/sbin/boot0cfg -s ${SLICE} -v /dev/${BOOT_DRIVE}" >> /cf/upgrade_log.txt
|
251
|
/usr/sbin/boot0cfg -s ${SLICE} -v /dev/${BOOT_DRIVE} >> /cf/upgrade_log.txt 2>&1
|
252
|
|
253
|
# restore config
|
254
|
cp -p /tmp/configbak/* /conf 2>/dev/null
|
255
|
|
256
|
# Remove upgrade file
|
257
|
rm -f $IMG
|
258
|
|
259
|
mkdir /tmp/$COMPLETE_PATH
|
260
|
mount /dev/$COMPLETE_PATH /tmp/$COMPLETE_PATH
|
261
|
# If /tmp/$TOFLASH/tmp/post_upgrade_command exists
|
262
|
# after update then execute the command.
|
263
|
if [ -f /tmp/$TOFLASH/tmp/post_upgrade_command ]; then
|
264
|
sh /tmp/$TOFLASH/tmp/post_upgrade_command >> /cf/upgrade_log.txt 2>&1
|
265
|
fi
|
266
|
# Update fstab
|
267
|
sed -i "" "s/pfsense${OLDSLICE}/pfsense${SLICE}/g" /tmp/$TOFLASH/etc/fstab
|
268
|
echo "" >> /cf/upgrade_log.txt
|
269
|
cat /tmp/$TOFLASH/etc/fstab >> /cf/upgrade_log.txt
|
270
|
umount /tmp/$TOFLASH
|
271
|
|
272
|
# remount /cf ro
|
273
|
rm -rf /etc/rc.conf
|
274
|
rm -rf /etc/motd
|
275
|
find / -name CVS -type d -exec rm {} \;
|
276
|
rm -rf /usr/savecore/*
|
277
|
/etc/rc.conf_mount_ro
|
278
|
|
279
|
rm -f /var/run/firmware.lock
|
280
|
/bin/sync
|
281
|
echo "Done." | logger -p daemon.info -i -t Upgrade
|
282
|
|
283
|
sh /etc/rc.reboot
|
284
|
|
285
|
;;
|
286
|
pfSenseupgrade)
|
287
|
|
288
|
# Sanity check - bail early if there's no firmware file!
|
289
|
if [ ! -r $IMG ]; then
|
290
|
echo "2nd parameter has not been passed or file does not exist. Exiting." | logger -p daemon.info -i -t Upgrade
|
291
|
exit
|
292
|
fi
|
293
|
|
294
|
# wait 1 seconds before beginning
|
295
|
sleep 1
|
296
|
|
297
|
touch /var/run/firmware.lock
|
298
|
|
299
|
backup_chflags
|
300
|
remove_chflags
|
301
|
|
302
|
# Do we have a pre-upgrade hook in the update file?
|
303
|
if [ `tar tvzf $IMG | grep /tmp/pre_upgrade_command | wc -l` -gt 0 ]; then
|
304
|
tar xzvf $IMG -C / ./tmp/pre_upgrade_command
|
305
|
chmod a+rx /tmp/pre_upgrade_command
|
306
|
sh /tmp/pre_upgrade_command
|
307
|
fi
|
308
|
|
309
|
#exec </dev/console >/dev/console 2>/dev/console
|
310
|
|
311
|
echo "Firmware upgrade in progress..." | logger -p daemon.info -i -t Upgrade
|
312
|
|
313
|
# backup config
|
314
|
/bin/mkdir -p /tmp/configbak
|
315
|
cp -p /conf/* /tmp/configbak 2>/dev/null
|
316
|
# mount /cf
|
317
|
/etc/rc.conf_mount_rw
|
318
|
/sbin/mount -w -o noatime /cf 2>/dev/null
|
319
|
/sbin/mount -w -o noatime / 2>/dev/null
|
320
|
|
321
|
# tar explode image onto hd
|
322
|
echo "Installing $IMG." | logger -p daemon.info -i -t Upgrade
|
323
|
cd / && /usr/bin/tar xzUPf $IMG | logger -p daemon.info -i -t Upgrade
|
324
|
/usr/bin/find / -name CVS -exec rm -fr {} \;
|
325
|
echo "Image installed $IMG." | logger -p daemon.info -i -t Upgrade
|
326
|
|
327
|
# process custom image if its passed
|
328
|
if [ $# -eq 3 ]; then
|
329
|
if [ -f $CUSTOMIMG ]; then
|
330
|
echo "Custom image $CUSTOMIMG found." | logger -p daemon.info -i -t Upgrade
|
331
|
echo "Custom image ($CUSTOMIMG) found."
|
332
|
PWD_DIR=`pwd`
|
333
|
cd / && /usr/bin/tar xzPUf $CUSTOMIMG | logger -p daemon.info -i -t Upgrade
|
334
|
cd $PWD_DIR
|
335
|
echo "Custom image $CUSTOMIMG installed." | logger -p daemon.info -i -t Upgrade
|
336
|
fi
|
337
|
fi
|
338
|
|
339
|
# restore config
|
340
|
cp -p /tmp/configbak/* /conf 2>/dev/null
|
341
|
|
342
|
# restore /etc symlinks
|
343
|
rm /etc/hosts
|
344
|
ln -s /var/etc/hosts /etc/hosts
|
345
|
|
346
|
restore_chflags
|
347
|
|
348
|
# Remove upgrade file
|
349
|
rm -f $IMG
|
350
|
|
351
|
if [ -e /etc/init_bootloader.sh ]; then
|
352
|
sh /etc/init_bootloader.sh
|
353
|
fi
|
354
|
|
355
|
# If /tmp/post_upgrade_command exists after update
|
356
|
# then execute the command.
|
357
|
if [ -f /tmp/post_upgrade_command ]; then
|
358
|
sh /tmp/post_upgrade_command
|
359
|
fi
|
360
|
|
361
|
# remount /cf ro
|
362
|
rm -rf /etc/rc.conf
|
363
|
rm -rf /etc/motd
|
364
|
find / -name CVS -type d -exec rm {} \;
|
365
|
rm -rf /usr/savecore/*
|
366
|
/etc/rc.conf_mount_ro
|
367
|
/sbin/umount -f /cf 2>/dev/null
|
368
|
/sbin/mount -r /cf 2>/dev/null
|
369
|
/sbin/umount -f / 2>/dev/null
|
370
|
/sbin/mount -r / 2>/dev/null
|
371
|
|
372
|
sleep 3
|
373
|
rm -f /var/run/firmware.lock
|
374
|
/bin/sync
|
375
|
sleep 2
|
376
|
echo "Done." | logger -p daemon.info -i -t Upgrade
|
377
|
|
378
|
# If the archive has unpacked a file called
|
379
|
# /tmp/no_upgrade_reboot_required then do
|
380
|
# not reboot after upgrade.
|
381
|
if [ -f /tmp/no_upgrade_reboot_required ]; then
|
382
|
rm /tmp/no_upgrade_reboot_required
|
383
|
else
|
384
|
rm -f /var/run/config.lock
|
385
|
sh /etc/rc.reboot
|
386
|
fi
|
387
|
|
388
|
;;
|
389
|
delta_update)
|
390
|
touch /var/run/firmware.lock
|
391
|
backup_chflags
|
392
|
remove_chflags
|
393
|
binary_update $IMG
|
394
|
restore_chflags
|
395
|
rm -rf /etc/rc.conf
|
396
|
rm -rf /etc/motd
|
397
|
find / -name CVS -type d -exec rm {} \;
|
398
|
rm -rf /usr/savecore/*
|
399
|
/etc/rc.conf_mount_ro
|
400
|
/sbin/umount -f /cf 2>/dev/null
|
401
|
/sbin/mount -r /cf 2>/dev/null
|
402
|
/sbin/umount -f / 2>/dev/null
|
403
|
/sbin/mount -r / 2>/dev/null
|
404
|
if [ -e /etc/init_bootloader.sh ]; then
|
405
|
sh /etc/init_bootloader.sh
|
406
|
fi
|
407
|
|
408
|
;;
|
409
|
esac
|
410
|
|
411
|
|