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

# vars:
#
# START			distribution specific way of kicking programs
# END			distribution specific way of checking return status
# PROGRAM		the executable to run
# ARGUMENTS		the argument we're going to call PROGRAM with

DEVICE=/dev/cpu/0/microcode
ARGUMENTS=-Qui
RETVAL=0
PROGRAM=/sbin/microcode_ctl

# Lets just be sure we have a device file...
if [ ! -e $DEVICE ];
then 
	echo "$0: microcode device $DEVICE doesn't exist?"
	exit 1
elif [ ! -c $DEVICE ];
then
	echo "$0: $DEVICE not a character device?"
	exit 1
fi

if [ ! -e /etc/firmware/microcode.dat ];
then 
	echo "$0: microcode datafile not present (/etc/firmware/microcode.dat)"
	exit 1
fi


. /etc/init.d/functions

# perform the update
function start ()
{
	RETVAL=1
	# Make sure we are on an intel machine
	vendor=`cat /proc/cpuinfo | \
	    grep "^vendor_id" | head -1 | \
	    awk -F ": " '{ print $2 }'`
	if [ "$vendor" != "GenuineIntel" ]; then
	    return
	fi

	echo -n "Applying Intel IA32 Microcode update: "

	daemon $PROGRAM $ARGUMENTS
	RETVAL=$?

	# trap the most common case, errno 19 = no device
	if [ $RETVAL -eq 19 ];
	then
		echo $"$0: kernel does not have microcode device support"
	fi
	echo
}


case "$1" in
  start|""|reload|force-reload|restart)
	start
	exit 0
	;;
  stop)
	;;
  status)
	# I wonder when the driver will let us do this...
	# maybe I'll write it ;)
	echo $"$0: reading microcode status is not yet supported"
	;;
  *)
	echo $"Usage: $0 {start|restart}"
	exit 1
esac