#!/bin/bash
#
# To upload the microcode update (CPU bug fixes) on boot,
# add 'rc:microcode' as the first entry to the SYSINIT array
# in /etc/rc.d/rc.conf.

[[ ! $HAVE_RC_FUNCTIONS ]] && . /etc/rc.d/rc.functions

case $1 in
'start')
	echo 'Uploading the Intel microcode update to the CPU.'
	
	# Load the kernel module if needed.
	UNLOAD_MODULE=0
	if [ ! -w /dev/cpu/microcode ]; then
		UNLOAD_MODULE=1
		modprobe microcode
	fi
	
	# Upload the microcode.
	microcode_ctl -u -q
	
	# Unload the module if we loaded it. It's not needed after
	# the microcode has been uploaded.
	[[ $UNLOAD_MODULE = 1 ]] && modprobe -r microcode
	;;
'stop')
	# Do nothing for compatibility with shutdown scripts.
	;;
*)
	echo "Usage: $0 start"
	;;
esac
