#!/bin/bash

# Perform various BIOS function
# Possible exit code: 0 everything worked
#		      1 BIOS flash failed / netboot disabled
#		      2 Model does not support operation
#		      3 BIOS is already up to date


# LATEST_CR2_BIOS="39"
# LATEST_CR3_BIOS="32"
# LATEST_CR4_BIOS="111"
vpd=`/opt/hsc/bin/getHMCVPD`
bioslvl=`/opt/hsc/bin/getHMCVPD -b`
# Check for VPD first. Do not change this logic.
# This way if this script is run on unsupported model
# it will return a 2.
# Only do this function on Machines that are supported
# Storage does not use this function
case "$vpd" in
  *CR2 )
	if [ "$1" == "update" ]; then
	   echo $bioslvl | grep -q "39"
	   if [ $? -ne 0 ]; then
	     /opt/hsc/sbin/39y4894l.sh -s >/dev/null 2>&1
	     if [ $? -ne 0 ]; then
		exit 1
  	     fi
	   else
	     exit 3
	   fi
	fi
	;;
  *CR3 )
	if [ "$1" == "update" ]; then
	   echo $bioslvl | grep -q "32"
	   if [ $? -ne 0 ]; then
	     /opt/hsc/sbin/42c5632l.sh -s >/dev/null 2>&1
	     if [ $? -ne 0 ]; then
		exit 1
	     fi
	   else
	     exit 3
	   fi
	fi
	;;
#  *CR4 )
#	if [ "$1" == "update" ]; then
#	   echo $bioslvl | grep -q "111"
#	   if [ $? -ne 0 ]; then
#	     /opt/hsc/sbin/ibm_fw_bios_gfe111a_linux_intel32.sh -s >/dev/null 2>&1
#	     if [ $? -ne 0 ]; then
#		exit 1
#	     fi
#	   else
#	     exit 3
#	   fi
#	fi
#	;;
  7310C05 )
	if [ "$1" == "update" ]; then
	   echo $bioslvl | grep -q "22"
	   if [ $? -ne 0 ]; then
	     /opt/hsc/sbin/PAJT122B.sh -s >/dev/null 2>&1
	     if [ $? -ne 0 ]; then
		exit 1
	     fi
	   else
	     exit 3
	   fi
	fi
	;;
  *)
	if [ "$1" == "update" ]; then
           exit 2
	fi
        ;;
esac
if [ "$1" == "modify" ]; then
   # make network the first
   case "$vpd" in
    *CR2 | *CR3 | *CR4 | 8676* | 8837* | 7978* )
       /opt/hsc/sbin/asu set CMOS_PrimaryBootDevice1 "Network"
       /opt/hsc/sbin/asu set CMOS_PrimaryBootDevice2 "Diskette Drive 0"
       /opt/hsc/sbin/asu set CMOS_PrimaryBootDevice3 "CD ROM"
       /opt/hsc/sbin/asu set CMOS_PrimaryBootDevice4 "Hard Disk 0"
       /opt/hsc/sbin/asu set CMOS_ENET_PXE_ENABLE "Planar Ethernet 1 and 2"
       ;;
    7310C05 )
       /opt/hsc/sbin/asu set cmosStartupPrimary1 "Network-Planar0"
       /opt/hsc/sbin/asu set cmosStartupPrimary2 "Removable"
       /opt/hsc/sbin/asu set cmosStartupPrimary3 "CD/DVD-ROM"
       /opt/hsc/sbin/asu set cmosStartupPrimary4 "Hard Disk"
       ;;
  *)
       exit 2
       ;;
   esac
fi
if [ "$1" == "reset" ]; then
   # set back to default settings
   case "$vpd" in
    *CR2 | *CR3 | *CR4 | 8676* | 8837* | 7978* )
      /opt/hsc/sbin/asu set CMOS_PrimaryBootDevice1 "Diskette Drive 0"
      /opt/hsc/sbin/asu set CMOS_PrimaryBootDevice2 "CD ROM"
      /opt/hsc/sbin/asu set CMOS_PrimaryBootDevice3 "Hard Disk 0"
      /opt/hsc/sbin/asu set CMOS_PrimaryBootDevice4 "Network"
      ;;
    7310C05 )
       /opt/hsc/sbin/asu set cmosStartupPrimary1 "Removable"
       /opt/hsc/sbin/asu set cmosStartupPrimary2 "CD/DVD-ROM"
       /opt/hsc/sbin/asu set cmosStartupPrimary3 "Hard Disk"
       /opt/hsc/sbin/asu set cmosStartupPrimary4 "Network-Planar0"
       ;;
  *)
       exit 2
       ;;
   esac
fi
if [ "$1" == "viewnetboot" ]; then
   case "$vpd" in
    *CR2 | *CR3 | *CR4 | 8676* | 8837* | 7978* )
      /opt/hsc/sbin/asu show CMOS_PrimaryBootDevice1 | grep -q "Network"
      exit $?
      ;;
    7310C05 )
      /opt/hsc/sbin/asu show cmosStartupPrimary1 | grep -q "Network"
      exit $?
      ;;
    *)
       exit 2
       ;;
    esac
fi
exit 0
