summaryrefslogtreecommitdiffstats
path: root/etc/rc.firmware
blob: 4b333fe47dc42056caa8a38c240a405bcc551049 (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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#!/bin/sh

# /etc/rc.firmware
# originally part of m0n0wall (http://neon1.net/m0n0wall)
# Copyright (C) 2005-2009 Scott Ullrich <sullrich@pfsense.org>.
# Copyright (C) 2003 Manuel Kasper <mk@neon1.net>.
# All rights reserved.

exec 3>&2 2>>/cf/firmware_update_misc.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

file_notice() {
	/usr/local/bin/php -q -d auto_prepend_file=config.inc <<ENDOFF
	<?php
		require_once("globals.inc");		
		require_once("functions.inc");
		file_notice("$1", "$2", "$1", "");
	?>
ENDOFF
}

output_env_to_log() {
	date >> /cf/upgrade_log.txt
	echo "" >> /cf/upgrade_log.txt
	
	ls -lah /dev/ >> /cf/upgrade_log.txt
	echo "" >> /cf/upgrade_log.txt

	ls -lah $IMG >> /cf/upgrade_log.txt
	echo "" >> /cf/upgrade_log.txt

	md5 $IMG >> /cf/upgrade_log.txt
	echo "" >> /cf/upgrade_log.txt

	mount >> /cf/upgrade_log.txt
	echo "" >> /cf/upgrade_log.txt

	top >> /cf/upgrade_log.txt
	echo "" >> /cf/upgrade_log.txt
}

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 >> /cf/upgrade_log.txt 2>&1
	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} >> /cf/upgrade_log.txt 2>&1
	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)
	touch /cf/upgrade_log.txt
	echo "" > /cf/upgrade_log.txt
	;;
auto)
	touch /var/run/firmware.lock
	backup_chflags
	remove_chflags
	/etc/rc.firmware_auto
	restore_chflags
	;;
pfSenseNanoBSDupgrade)

	# mount /cf
	/etc/rc.conf_mount_rw

	# 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." >> /cf/upgrade_log.txt 2>&1
		/etc/rc.conf_mount_ro
		exit 1
	fi

	# Prevent full upgrade file from being used to upgrade
	if [ `echo $IMG | grep "full"` ]; then
		echo "You cannot use a full file for upgrade.  Please use a file labeled upgrade."
		file_notice "NanoBSDUpgradeFailure" "You have attemped to use a full NanoBSD installation file as an upgrade.  Please use a NanoBSD file labeled 'upgrade' instead."
		/etc/rc.conf_mount_ro		
		exit 1
	fi

	touch /var/run/firmware.lock

	echo "Firmware upgrade in progress..."  >> /cf/upgrade_log.txt 2>&1
	echo "Firmware upgrade in progress..." | wall

	# backup config
	/bin/mkdir -p /tmp/configbak
	cp -p /conf/* /tmp/configbak 2>/dev/null

	touch /cf/upgrade_log.txt
	echo "" > /cf/upgrade_log.txt

	echo "Installing $IMG." >> /cf/upgrade_log.txt 2>&1
	echo "Installing $IMG." >> /cf/upgrade_log.txt

	# resolve glabel label that we booted from
	BOOT_DEVICE=`/sbin/mount | /usr/bin/grep pfsense | /usr/bin/cut -d'/' -f4 | /usr/bin/cut -d' ' -f1`
	# resolve glabel to the real boot dev entry
	REAL_BOOT_DEVICE=`/sbin/glabel list | /usr/bin/grep -B2 ufs/${BOOT_DEVICE} | /usr/bin/head -n 1 | /usr/bin/cut -f3 -d' '`
	# grab the boot device, example ad1, ad0
	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`
	# test the slice.  if we are on slice 1 we need to flash 2 and vica versa
	if [ `echo $REAL_BOOT_DEVICE | /usr/bin/grep "s1"` ]; then 
		SLICE="2"
		OLDSLICE="1"
		TOFLASH="${BOOT_DRIVE}s${SLICE}"
		COMPLETE_PATH="${BOOT_DRIVE}s${SLICE}a"
		GLABEL_SLICE="pfsense1"
		UFS_ID="1"
		OLD_UFS_ID="0"
	else
		SLICE="1"
		OLDSLICE="2"		
		TOFLASH="${BOOT_DRIVE}s${SLICE}"
		COMPLETE_PATH="${BOOT_DRIVE}s${SLICE}a"
		GLABEL_SLICE="pfsense0"
		UFS_ID="0"
		OLD_UFS_ID="1"
	fi

	# Output specifc information that this script is using
	echo "SLICE $SLICE" >> /cf/upgrade_log.txt
	echo "OLDSLICE $OLDSLICE" >> /cf/upgrade_log.txt
	echo "TOFLASH $TOFLASH" >> /cf/upgrade_log.txt
	echo "COMPLETE_PATH $COMPLETE_PATH" >> /cf/upgrade_log.txt
	echo "GLABEL_SLICE $GLABEL_SLICE" >> /cf/upgrade_log.txt

	# Output environment information to log file
	output_env_to_log

	# Log that we are really doing a NanoBSD upgrade
	echo "" >> /cf/upgrade_log.txt
	echo "NanoBSD upgrade starting" >> /cf/upgrade_log.txt
	echo "" >> /cf/upgrade_log.txt
	
	# Foot shooting is fun!
	echo "" >> /cf/upgrade_log.txt
	echo "/sbin/sysctl kern.geom.debugflags=16" >> /cf/upgrade_log.txt
	/sbin/sysctl kern.geom.debugflags=16 >> /cf/upgrade_log.txt 2>&1

	# Remove TOFLASH and get ready for new flash image
	echo "" >> /cf/upgrade_log.txt
	echo "dd if=/dev/zero of=/dev/${TOFLASH} bs=1m count=1" >> /cf/upgrade_log.txt	
	dd if=/dev/zero of=/dev/${TOFLASH} bs=1m count=1 >> /cf/upgrade_log.txt 2>&1

	# Stream gzipped image to dd and explode image to new area
	echo "" >> /cf/upgrade_log.txt
	echo "/usr/bin/gzcat $IMG | /bin/dd of=/dev/${TOFLASH} bs=16k" >> /cf/upgrade_log.txt
	/usr/bin/gzcat $IMG | /bin/dd of=/dev/${TOFLASH} bs=16k >> /cf/upgrade_log.txt 2>&1
	
	# Ensure that our new system is sound and bail if it is not and file a notice
	echo "" >> /cf/upgrade_log.txt
	echo "/sbin/fsck_ffs -n /dev/$COMPLETE_PATH" >> /cf/upgrade_log.txt
	/sbin/fsck_ffs -n /dev/$COMPLETE_PATH >> /cf/upgrade_log.txt 2>&1
	if [ $? != 0 ]; then
		file_notice "UpgradeFailure" "{\$g['product_name']} upgrade has failed.   Your system has been left in a usable state."
		rm /var/run/firmware.lock
		/etc/rc.conf_mount_ro		
		exit 1
	fi

	# Add back the corresponding glabel
	echo "" >> /cf/upgrade_log.txt
	echo "/sbin/tunefs -L ${GLABEL_SLICE} /dev/$COMPLETE_PATH" >> /cf/upgrade_log.txt
	/sbin/tunefs -L ${GLABEL_SLICE} /dev/$COMPLETE_PATH >> /cf/upgrade_log.txt 2>&1

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

	# Remove upgrade file
	rm -f $IMG

	# Mount newly prepared slice
	mkdir /tmp/$GLABEL_SLICE
	mount /dev/ufs/$GLABEL_SLICE /tmp/$GLABEL_SLICE

	# If /tmp/$GLABEL_SLICE/tmp/post_upgrade_command exists 
	# after update then execute the command.
	if [ -f /tmp/$GLABEL_SLICE/tmp/post_upgrade_command ]; then
		sh /tmp/$GLABEL_SLICE/tmp/post_upgrade_command >> /cf/upgrade_log.txt 2>&1
	fi

	# Update fstab
	cp /etc/fstab /tmp/$GLABEL_SLICE/etc/fstab
	sed -i "" "s/pfsense${OLD_UFS_ID}/pfsense${UFS_ID}/g" /tmp/$GLABEL_SLICE/etc/fstab
	if [ $? != 0 ]; then
		echo "Something went wrong when trying to update the fstab entry.  Aborting upgrade."
		file_notice "UpgradeFailure" "Something went wrong when trying to update the fstab entry.  Aborting upgrade."
		rm /var/run/firmware.lock
		umount /tmp/$GLABEL_SLICE
		/etc/rc.conf_mount_ro
		exit 1
	fi
	echo "" >> /cf/upgrade_log.txt
	cat /tmp/$GLABEL_SLICE/etc/fstab >> /cf/upgrade_log.txt

	# Unmount newly prepared slice
	umount /tmp/$GLABEL_SLICE

	# Set active mount slice in fdisk
	echo "" >> /cf/upgrade_log.txt
	echo "gpart set -a active -i ${SLICE} ${BOOT_DRIVE}" >> /cf/upgrade_log.txt
	gpart set -a active -i ${SLICE} ${BOOT_DRIVE}

	# Set active boot source
	echo "" >> /cf/upgrade_log.txt
	echo "/usr/sbin/boot0cfg -s ${SLICE} -v /dev/${BOOT_DRIVE}" >> /cf/upgrade_log.txt
	/usr/sbin/boot0cfg -s ${SLICE} -v /dev/${BOOT_DRIVE} >> /cf/upgrade_log.txt 2>&1

	# Remove extra stuff
	rm -rf /etc/rc.conf
	rm -rf /etc/motd
	rm -rf /usr/savecore/*

	date >> /cf/upgrade_log.txt
	echo "" >> /cf/upgrade_log.txt

	# Trigger a package reinstallation on reobot
	touch /conf/needs_package_sync

	# remount /cf ro
	/etc/rc.conf_mount_ro
	/bin/sync
	echo "Done." >> /cf/upgrade_log.txt 2>&1

	# Enough fun for now.
	echo "" >> /cf/upgrade_log.txt
	echo "/sbin/sysctl kern.geom.debugflags=0" >> /cf/upgrade_log.txt
	/sbin/sysctl kern.geom.debugflags=0 >> /cf/upgrade_log.txt 2>&1

	rm -f /var/run/firmware.lock
	sh /etc/rc.reboot

	;;
pfSenseupgrade)

	# mount /cf
	/etc/rc.conf_mount_rw

	# 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." >> /cf/upgrade_log.txt 2>&1
		/etc/rc.conf_mount_ro
		exit
	fi

	# wait 1 seconds before beginning
	sleep 1

	# Log that we are really doing a NanoBSD upgrade
	echo "" >> /cf/upgrade_log.txt
	echo "NanoBSD upgrade starting" >> /cf/upgrade_log.txt
	echo "" >> /cf/upgrade_log.txt

	touch /var/run/firmware.lock

	touch /cf/upgrade_log.txt
	echo "" >> /cf/upgrade_log.txt

	# Output environment information to log file
	output_env_to_log

	backup_chflags
	remove_chflags

	# Do we have a pre-upgrade hook in the update file?
	if [ `tar tvzf $IMG | grep /tmp/pre_upgrade_command | wc -l` -gt 0 ]; then 
		tar xzvf $IMG -C / ./tmp/pre_upgrade_command >> /cf/upgrade_log.txt 2>&1
		chmod a+rx /tmp/pre_upgrade_command >> /cf/upgrade_log.txt 2>&1
		sh /tmp/pre_upgrade_command >> /cf/upgrade_log.txt 2>&1
	fi

	echo "Firmware upgrade in progress..."  >> /cf/upgrade_log.txt 2>&1
	echo "Firmware upgrade in progress..."  | wall

	# backup config
	/bin/mkdir -p /tmp/configbak
	cp -p /conf/* /tmp/configbak 2>/dev/null

	# tar explode image onto hd
	echo "Installing $IMG." >> /cf/upgrade_log.txt 2>&1
	cd / && /usr/bin/tar xzUPf $IMG >> /cf/upgrade_log.txt 2>&1
	/usr/bin/find / -name CVS -exec rm -fr {} \;
	echo "Image installed $IMG." >> /cf/upgrade_log.txt 2>&1

    # process custom image if its passed
    if [ $# -eq 3 ]; then
	    if [ -f $CUSTOMIMG ]; then
	        echo "Custom image $CUSTOMIMG found." >> /cf/upgrade_log.txt 2>&1
	        echo "Custom image ($CUSTOMIMG) found." >> /cf/upgrade_log.txt 2>&1
	        PWD_DIR=`pwd`
	        cd / && /usr/bin/tar xzPUf $CUSTOMIMG >> /cf/upgrade_log.txt 2>&1
	        cd $PWD_DIR
	        echo "Custom image $CUSTOMIMG installed." >> /cf/upgrade_log.txt 2>&1
	    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

	if [ -e /etc/init_bootloader.sh ]; then
		sh /etc/init_bootloader.sh >> /cf/upgrade_log.txt 2>&1
	fi

	# If /tmp/post_upgrade_command exists after update
	# then execute the command.
	if [ -f /tmp/post_upgrade_command ]; then
		sh /tmp/post_upgrade_command >> /cf/upgrade_log.txt 2>&1
	fi

	# remove unused files
	rm -rf /etc/rc.conf
	rm -rf /etc/motd
	rm -rf /usr/savecore/*

	date >> /cf/upgrade_log.txt
	echo "" >> /cf/upgrade_log.txt

	# remount /cf ro
	/etc/rc.conf_mount_ro

	# release the firmware lock
	rm -f /var/run/firmware.lock
	/bin/sync

	# 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)
	touch /var/run/firmware.lock
	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