#!/bin/bash
# chkconfig: 2345 0 99
# description: script to apply cpu microcode

# Check that we're a priviledged user
[ `id -u` = 0 ] || exit 0

DATAFILE=/lib/firmware/microcode.dat

. /etc/init.d/functions

RETVAL=0

# perform the update
function start ()
{
	RETVAL=1

	# Intel 686 and above, AMD family 16 and above
	vendor=`grep "^vendor_id" /proc/cpuinfo | head -n1 | awk -F ": " '{ print $2 }'`
	family=`grep "^cpu family" /proc/cpuinfo | head -n1 | awk -F ": " '{ print $2 }'`

	if [ "$vendor" = "GenuineIntel" ] && [ $family -ge 6 ]; then
		echo -n $"Applying Intel CPU microcode update: "
	elif [ "$vendor" = "AuthenticAMD" ] && [ $family -ge 16 ]; then
		echo $"Loading AMD microcode update module"
		/sbin/modprobe microcode	# and we're done!
		return
	else
		return
	fi

	if [ ! -e $DATAFILE ]; then 
		echo $"$0: CPU microcode data file not present ($DATAFILE)"
		exit 1
	fi

	/sbin/modprobe microcode

	lt=0
	while [ ! -c /dev/cpu/microcode ]; do
		lt=$[lt+1];
		[ $lt -gt 5 ] && break;
		sleep 0.1;
	done

	/sbin/microcode_ctl -Qu
	RETVAL=$?

	/sbin/rmmod microcode

	return $RETVAL
}

stop()
{
	return
}

case "$1" in
  start)
	start
	exit 0
	;;
  stop)
	stop
	;;
  restart|reload|force-reload)
	stop
	start
	;;
  status)
	;;
  *)
	echo $"Usage: $0 {start|stop|restart}"
	exit 1
esac
exit $?

