#!/bin/bash
#2018-05-11 rongjq@inspur.com

dir=`pwd`

$dir/SCELNX_64 /o /s a.txt /lang  >/dev/null

biosfile=a.txt


scebios() {
item="$1"

#cat $biosfile|sed -e "/$item/,/^[ \t]*$/!d"

#
biositem=`cat $biosfile|sed -e "/$item/,/^[ \t]*$/!d"|grep Question|awk -F '=' '{print $2 }'`

#exit

#item num
qnum=`cat $biosfile|sed -e "/$item/,/^[ \t]*$/!d"|grep Question |wc -l`

#lang string
mscode=`cat $biosfile|sed -e "/$item/,/^[ \t]*$/!d" |grep Map |awk '{print $4}'`


###################################################################
#add mscode
qnum_item=`cat $biosfile|sed -e "/$item/,/^[ \t]*$/!d"|grep Map |wc -l`
if [ $qnum_item -eq 1 ];then 
   mscode=`cat $biosfile|sed -e "/$item/,/^[ \t]*$/!d"|grep "Map String"|awk -F '=' '{print $2 }'`
   biositem=`cat $biosfile|grep "$item" -B1|grep Question |awk -F = '{print $2}'`
   qnum=1
   #echo $mscode_item  $qnum_item  $bios_item
fi
######################################################################



#main
 if [ $qnum -eq 1 ] 
   then
       
       case $# in
           #read bios
           1)
              curvalue=`./SCELNX_64  /o /ms $mscode |grep "\*" |awk -F "]" '{print $2}'` 
              #echo "$biositem : $curvalue"
                printf "%-40s:%-20s\n" "$biositem"  "$curvalue"
            ;;

           #write bios
           2)
             #item value
             pvalue=`cat $biosfile|sed -e "/$item/,/^[ \t]*$/!d" |sed -e "/Options/,/^[ \t]*$/!d" |grep -i "$2"`
             valuenum=`cat $biosfile|sed -e "/$item/,/^[ \t]*$/!d" |sed -e "/Options/,/^[ \t]*$/!d" |grep -i "$2"|grep -n [0-9] |wc -l`
             
             if [ $valuenum -eq 1 ]
                then
                   value=`echo $pvalue|grep -o '\[.*\]' |grep -o '\[.*\]' |sed 's/\]//g'|sed 's/\[//g'`
                  echo ""
                  echo "##Set $biositem  to $pvalue ###"    
                  ./SCELNX_64  /i /ms  $mscode  /qv 0x$value /ds >/dev/null 
             
             else
                 echo "error! $valuenum  be found in bios item $biositem"
                 cat $biosfile|sed -e "/$item/,/^[ \t]*$/!d"|sed -e "/Options/,/^[ \t]*$/!d"
                 exit 1          
             fi
             ;;

           *)
             echo "
                 args num 1 or 2
                 read:./$0 \$biositem
                 wirte:./$0 \$biositem  \$value
                   "
            ;;

       esac

 else
    echo "Bios Item error! Please check $item "
    
    exit 1
 fi

}

 #read
readbios(){
  
biositem="Hardware Prefetcher,Adjacent Cache Prefetch,PCHS001,sSATA Controller,SpeedStep,Turbo,Hardware P-States,Autonomous Core C-State,CPU C6,C1E,Package C State,Power Performance Tuning,ENERGY_PERF_BIAS_CFG,Mwait Support,Performance Profile,Sharelink Network,UFS,Numa,Hyper Threading Technology,SR-IOV,VT-d,FBO201,FBO202"
   IFS_old=$IFS 
   IFS=$'\n'
   for bios in `echo $biositem |awk -F , '{for(a=1;a<=NF;a++)print $a}'`
      do
        IFS=$IFS_old
        #echo $bios
        scebios "$bios"


  done
}

performance()
{

  echo "Set BIOS to Performance Mode,Please Waiting ......"
  
  scebios "Performance Profile" custom
  scebios "SpeedStep"    enable
  scebios "Turbo Mode" enable
  scebios "Autonomous Core C-State" disable
  scebios "CPU C6"     disable 
  scebios "C1E"        disable
  scebios "Package C State"     "C1 state"
  scebios "CPU C6"     disable
  scebios "ENERGY_PERF_BIAS_CFG" 00       #performance
  scebios "Mwait Support"  disable

}

viren()
{
  echo "open vm,but bios default has been enabled "
  scebios "VT-d" enable
  scebios "SR-IOV" enable

}

energypower_performance_banlance()
{
  scebios "Performance Profile" custom
  scebios "SpeedStep"    enable
  scebios "Turbo Mode" enable
  scebios "Autonomous Core C-State" enable
  scebios "CPU C6"     enable
  scebios "C1E"        enable
  scebios "Package C State"     "No Limit"
  scebios "ENERGY_PERF_BIAS_CFG" "Balanced Performance"
  scebios "Mwait Support"  enable

}

power_performance()
{

  scebios "Performance Profile" custom
  scebios "SpeedStep"    disable
  scebios "Turbo Mode"   disable
  scebios "Autonomous Core C-State" disable
  scebios "CPU C6"     disable
  scebios "C1E"        disable
  scebios "Package C State"     "C1 state"
  scebios "ENERGY_PERF_BIAS_CFG" 00
  scebios "Mwait Support"  disable

}

CSM_uefi()
{

  #BOOT mode
  echo "BOOT mode to uefi"
  ./SCELNX_64  /i /ms  CSM006  /qv 0x02 /ds
  
 # network
  ./SCELNX_64  /i /ms  CSM007  /qv 0x01 /ds
 #storage
 ./SCELNX_64  /i /ms  CSM008  /qv 0x01 /ds
 #video
 ./SCELNX_64  /i /ms  CSM009  /qv 0x01 /ds
 #pcie 
 ./SCELNX_64  /i /ms  CSM010  /qv 0x01 /ds

}

CSM_legacy()
{

  #BOOT mode
  #BOOT mode
  echo "BOOT mode to legacy"
  ./SCELNX_64  /i /ms  CSM006  /qv 0x01 /ds

 # network
  ./SCELNX_64  /i /ms  CSM007  /qv 0x02 /ds
 #storage
 ./SCELNX_64  /i /ms  CSM008  /qv 0x02 /ds
 #video
 ./SCELNX_64  /i /ms  CSM009  /qv 0x02 /ds
 #pcie 
 ./SCELNX_64  /i /ms  CSM010  /qv 0x02 /ds

}

boot_list()
{
 echo “Boot Order:”
  scebios FBO201
  scebios FBO202
  scebios FBO203
  scebios FBO204
  scebios FBO205 
 
 echo "Boot Device:"
  scebios FBO101
  scebios FBO102
  scebios FBO103
 
 echo "Disk Order:"
   cat $biosfile|grep "Map String" -A11|sed -e "/Boot Option/,/^[ \t]*$/!d"|grep -F *[|grep -v -E "Enabled|Hard Disk|Netw|UEFI AP|DVD|USB Device" |awk '{
       if($0~"=*//"){
          split($0,a,"[=/]");
          print a[2];}
       else
          print $0;
    }'



 echo "Network Order:"

}

mapping_list()
{
 
 cat $1 |grep -E "Question|Map"


}


##################################################################

#Main
################################################################

case $1 in 

   -set)
        case "$2" in 

          per) 
              performance
           ;;
          balan)
              energypower_performance_banlance
           ;;
          poper)
              power_performance
          ;;
          legacy)
              CSM_legacy
           ;;
            uefi)
              CSM_uefi
             ;;
           *)
           scebios "$2"  "$3"
           ;; 
         esac
     ;;
      
  
    -get)
        case "$2" in

          all)
              readbios 
           ;;
          *)
              scebios "$2"
            ;;   
        esac
      ;;
    -list)

        mapping_list $biosfile 
      ;;

    -bootlist)
         boot_list
      ;;
     *)
      echo "error!"
      cat readme.txt
    ;;
esac


