#!/bin/sh

#
#     Copyright (C) 2010 Intel Corporation.  All Rights Reserved.
#
#     This file is part of SEP Development Kit
#
#     SEP Development Kit is free software; you can redistribute it
#     and/or modify it under the terms of the GNU General Public License
#     version 2 as published by the Free Software Foundation.
#
#     SEP Development Kit is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
#
#     You should have received a copy of the GNU General Public License
#     along with SEP Development Kit; if not, write to the Free Software
#     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
#     As a special exception, you may use this file as part of a free software
#     library without restriction.  Specifically, if other files instantiate
#     templates or use macros or inline functions from this file, or you compile
#     this file and link it with other files to produce an executable, this
#     file does not by itself cause the resulting executable to be covered by
#     the GNU General Public License.  This exception does not however
#     invalidate any other reasons why the executable file might be covered by
#     the GNU General Public License.
#

# ------------------------------ CONSTANTS -----------------------------------

DRIVER_NAME=vtsspp
DEFAULT_GROUP=${GROUP:-root}
DEFAULT_PERMS=660

# ------------------------------- OUTPUT -------------------------------------

print_msg()
{
  MSG="$*"
  echo "$MSG"
}

print_nnl()
{
  MSG="$*"
  echo -n "$MSG"
}

print_err()
{
  MSG="$*"
  if [ -w /dev/stderr ] ; then
    echo "$MSG" >> /dev/stderr
  else
    echo "$MSG"
  fi
}

# set the path to include "standard" locations so commands below can be found
PATH="/sbin:/usr/sbin:/usr/local/sbin:/bin:/usr/bin/:/usr/local/sbin:/usr/local/bin:/usr/local/gnu/bin:"${PATH}""
export PATH

# ------------------------------ COMMANDS ------------------------------------

CUT="cut"
DIRNAME="dirname"
GREP="grep"
LSMOD="lsmod"
INSMOD="insmod"
STAT="stat"
SU="su"
UNAME="uname"
WHICH="which"
GETENT="getent"

COMMANDS_TO_CHECK="${CUT} ${DIRNAME} ${GREP} ${LSMOD} ${INSMOD} ${UNAME}"

# ------------------------------ FUNCTIONS -----------------------------------

# function to show usage and exit
print_usage_and_exit()
{
  err=${1:-0}
  print_msg ""
  print_msg "Usage: $0 [ options ]"
  print_msg ""
  print_msg " where \"options\" are the following:"
  print_msg ""
  print_msg "    -g | --group group"
  print_msg "      restricts access to the ${DRIVER_NAME} driver to users in the specified"
  print_msg "      group; if this option is not provided, the group \"${DEFAULT_GROUP}\""
  print_msg "      will be used"
  print_msg ""
  print_msg "    -p | --perms fileperms"
  print_msg "      restricts access to the ${DRIVER_NAME} driver based on the specified"
  print_msg "      file permissions; if this option is not provided, then file"
  print_msg "      permissions \"${DEFAULT_PERMS}\" (or equivalently, \"ug+rw\") will be used"
  print_msg ""
  print_msg "    -q | --query"
  print_msg "      returns 0 if driver is already loaded, non-zero otherwise;"
  print_msg "      if driver is loaded, information on group ownership"
  print_msg "      and file permissions on driver device will be displayed"
  print_msg ""
  print_msg "    -r | --reload"
  print_msg "      attempt to reload the driver; note that the driver will"
  print_msg "      not unload if it is still in use"
  print_msg ""
  print_msg "    -re | --restricted-environment"
  print_msg "      restricted environment mode: minimal requirements to the system runtime"
  print_msg "      like in busybox case"
  print_msg ""
  exit $err
}

# check for certain options
reload_driver=0
while [ $# -gt 0 ] ; do
  case "$1" in
    -h | --help)
      print_usage_and_exit 0
      ;;
    -g | --group)
      DRIVER_GROUP=$2
      if [ -z "$DRIVER_GROUP" ] ; then
        print_err ""
        print_err "ERROR: must provide a correct group name or id"
        print_usage_and_exit 254
      fi
      shift
      ;;
    -p | --perms)
      DRIVER_PERMS=$2
      if [ -z "$DRIVER_PERMS" ] ; then
        print_err ""
        print_err "ERROR: must provide the file permissions"
        print_usage_and_exit 254
      fi
      shift
      ;;
    -q | --query)
      err=0
      driver_loaded=`${LSMOD} | ${GREP} ${DRIVER_NAME}`
      if [ -z "$driver_loaded" ] ; then
        err=214
        print_msg "${DRIVER_NAME} driver is not loaded."
      else
        if [ -e /proc/${DRIVER_NAME}/.control ] ; then
          drv_group=`${STAT} -c "%G" /proc/${DRIVER_NAME}/.control`
          drv_perms=`${STAT} -c "%a" /proc/${DRIVER_NAME}/.control`
          print_msg "${DRIVER_NAME} driver is loaded and owned by group \"${drv_group}\" with file permissions \"${drv_perms}\"."
        else
          err=214
          print_msg "${DRIVER_NAME} driver is not correctly loaded."
        fi
      fi
      exit $err
      ;;
    -r | --reload)
      reload_driver=1
      ;;
    -re | --restricted-environment)
      BUSYBOX_SHELL=yes
      ;;
    *)
      print_err ""
      print_err "ERROR: unrecognized option \"$1\""
      print_usage_and_exit 254
      ;;
  esac
  shift
done

#
# Note: Busybox has a restricted shell environment, and
#       conventional system utilities may not be present;
#       so need to account for this ...
#

# busybox binary check
if [ -z "${BUSYBOX_SHELL}" ]; then
  # if not forced by command line option -re then check it
  BUSYBOX_SHELL=`${GREP} --help 2>&1 | ${GREP} BusyBox`
fi

if [ -n "${BUSYBOX_SHELL}" ] ; then
  DEFAULT_GROUP=${GROUP:-0}
else
  COMMANDS_TO_CHECK="${GETENT} ${STAT} ${SU} ${COMMANDS_TO_CHECK}"
fi

# if any of the COMMANDS_TO_CHECK are not executable, then exit script
OK="true"
for c in ${COMMANDS_TO_CHECK} ; do
  CMD=`${WHICH} $c 2>&1` ;
  if [ -z "${CMD}" ] ; then
    OK="false"
    print_err "ERROR: unable to find command \"$c\" !"
  fi
done
if [ ${OK} != "true" ] ; then
  print_err "If you are using BusyBox, please re-run this script with the '-re' flag added"
  print_err "Otherwise, please add the above commands to your PATH and re-run the script ... exiting."
  exit 255
fi

# ------------------------------ VARIABLES -----------------------------------

SCRIPT=$0
PLATFORM=`${UNAME} -m`
KERNEL_VERSION=`${UNAME} -r`
DRIVER_DIRECTORY=`${DIRNAME} ${SCRIPT}`
DRIVER_GROUP=${DRIVER_GROUP:-${DEFAULT_GROUP}}
DRIVER_PERMS=${DRIVER_PERMS:-${DEFAULT_PERMS}}

# check if platform is supported
if [ "${PLATFORM}" = "x86_64" ] ; then
  ARCH="x32_64"
else
  print_err ""
  print_err "ERROR: Unsupported platform \"${PLATFORM}\" ... exiting."
  print_err ""
  exit 253
fi

# XEN dom0 check
if [ -d /proc/xen ] ; then
    if ${GREP} control_d /proc/xen/capabilities &>/dev/null; then
        print_err ""
        print_err "ERROR: XEN dom0 is not supported ... exiting."
        print_err ""
        exit 253
    fi
fi

# ------------------------------- MAIN ---------------------------------------

# check if USER is root
if [ -z "${BUSYBOX_SHELL}" ] ; then
  if [ "${USER}x" != "rootx" ] ; then
    if [ ! -w /dev ] ; then
      print_msg "NOTE:  super-user or \"root\" privileges are required in order to continue."
      print_nnl "Please enter \"root\" "
      OPTIONS="-g ${DRIVER_GROUP} -p ${DRIVER_PERMS}"
      if [ $reload_driver -eq 1 ] ; then
        OPTIONS="$OPTIONS -r"
      fi
      exec ${SU} -c "/bin/sh ${SCRIPT} ${OPTIONS}"
      print_msg ""
      exit 0
    fi
  fi
fi

# if requested, reload driver
if [ $reload_driver -eq 1 ] ; then
  ${DRIVER_DIRECTORY}/rmmod-${DRIVER_NAME}
  err=$?
  if [ $err -ne 0 ] ; then
    print_err ""
    print_err "Error: failed to reload ${DRIVER_NAME} driver"
    print_err ""
    exit $err
  fi
fi

# kernel is SMP
ARITY="smp"

# file name of driver to load
DRIVER_FILENAME=${DRIVER_NAME}-${ARCH}-${KERNEL_VERSION}${ARITY}.ko

if [ ! -r ${DRIVER_DIRECTORY}/${DRIVER_FILENAME} ] ; then
  print_err ""
  print_err "Warning: ${DRIVER_NAME} driver \"${DRIVER_FILENAME}\""
  print_err "was not found in directory \"${DRIVER_DIRECTORY}\" !"
  print_err ""
  print_err "This means you may need to build ${DRIVER_NAME} driver from the provided"
  print_err "driver sources.  Please see the driver README for instructions."
  print_err ""
  exit 0
fi

if [ -z "${BUSYBOX_SHELL}" ]; then
  DRIVER_GROUP_ID=`${GETENT} group ${DRIVER_GROUP} | ${CUT} -d: -f3`
  if [ -z "$DRIVER_GROUP_ID" ] ; then
    print_err ""
    print_err "ERROR: Cannot resolve group ID for ${DRIVER_GROUP}"
    print_usage_and_exit 235
  fi
else
  DRIVER_GROUP_ID=0
fi

# kallsyms_lookup_name function address
KALLSYMS_LOOKUP_NAME_ADDR=`${GREP} ' T kallsyms_lookup_name$' /proc/kallsyms | ${CUT} -d' ' -f1`

# insmod command to execute
INSMOD_CMD="${INSMOD} ${DRIVER_DIRECTORY}/${DRIVER_FILENAME} gid=${DRIVER_GROUP_ID} mode=0${DRIVER_PERMS} ksyms=\"${KALLSYMS_LOOKUP_NAME_ADDR}\""

# execute the command
print_msg "Executing: ${INSMOD_CMD}"
${INSMOD_CMD}
INSMOD_RESULT=$?

# abort if unable to load the driver
if [ ${INSMOD_RESULT} -ne 0 ] ; then
  print_err ""
  print_err "Error:  ${DRIVER_NAME} driver failed to load!"
  print_err ""
  print_err "You may need to build ${DRIVER_NAME} driver for your kernel."
  print_err "Please see the ${DRIVER_NAME} driver README for instructions."
  print_err ""
  exit 0
fi

# show that the driver was successfully loaded
print_msg "The ${DRIVER_NAME} driver has been successfully loaded."
exit 0
