#!/bin/sh
#
# $Id: rtai,v 1.16.2.2 2004/05/07 18:17:45 tom Exp $
# $Source: /home/CVS/fireballTop/fireballFS/root/etc/init.d/Attic/rtai,v $

#  Utility : rtai
#   Author : Sven Dietrich
#     Date : July 17 2002
# Warranty : GPL
#   System : Linux 2.4.x kernels
# 
# NOTE: 
# A full list of RTAI modules must be edited into the "INSERT_LIST"
# variable below in the proper sequence of insertion

RTL_MODULES_LIST="rtai rtai_fifos rtai_sched_up rtai_shm"

if [ $# -eq 0 ]
then
  echo "Invalid number of parameters"
else
  COMMAND=$1
fi

# binary used to insert the modules
if [ -x /sbin/modprobe ]
then
  MODPROBE="/sbin/modprobe -a"
else
  MODPROBE="`which modprobe` -a"
  if [ ! -x $MODPROBE ]
  then
	echo "ERROR: 'modprobe' was not found in your path"
	exit 1
  fi
fi

########################################################################
function checkChecksum
########################################################################
# Perform a checksum on a specified file and compare that to the 
# expected results.  This is designed to detect file system buffer
# corruptions which could result in faulty operation.
{
  file=$1
  cs=$2
  set - $(/usr/bin/cksum $1)
  if [[ $1 != $cs ]]
  then
    echo "Bad checksum on $file.  CS is $1 not $cs.  Rebooting"
    init 6
  fi
}


# binary used to remove the modules
if [ -x /sbin/rmmod ]
then
  #RMMOD="/sbin/rmmod -r"
  RMMOD="/sbin/rmmod"
else
  RMMOD="`which rmmod` -r"
  if [ ! -x $RMMOD ]
  then
    echo "ERROR: 'rmmod' was not found in your path"
    exit 1
  fi
fi

# binary used to list modules
if [ -x /sbin/lsmod ]
then
  LSMOD=/sbin/lsmod
else
  LSMOD=`which lsmod`
  if [ ! -x $LSMOD ]
  then
	echo "ERROR: 'lsmod' was not found in your path"
	exit 1
  fi
fi

# path to grep
if [ -x /bin/grep ]
then
  GREP=/bin/grep
else
  GREP=`which grep`
  if [ ! -x $GREP ]
  then
    echo "ERROR: 'grep' was not found in your path"
    exit 1
  fi
fi


# Add the user-specified module to the insert and remove lists.
USER_INS_LIST="kernel_stinger"
TOTAL_LIST=${RTL_MODULES_LIST}

if [ $# -ge 2 ] 
then
  shift 1
  for modules in $@
  do
    TEMP=`echo $modules | sed -e s/"\.o"$//`
    USER_INS_LIST="$USER_INS_LIST $TEMP.o"
  done
fi

# reverse the USER_INS_LIST to create a removal sequence 
# for user modules.
USER_DEL_LIST=""
for modules in $USER_INS_LIST
do
  TEMP=`echo $modules | sed -e s/"\.o"$//`
  USER_DEL_LIST="$TEMP $USER_DEL_LIST"
done
TOTAL_LIST="${USER_DEL_LIST} ${TOTAL_LIST}"


# Process the request (start/stop/etc.)
case "$COMMAND" in

  debugstart)
	${MODPROBE} ${RTL_MODULES_LIST}

	for modules in $USER_INS_LIST
	do
      ${MODPROBE} $modules
	done
	cat /proc/ksyms > /tmp/latest.ksyms
	
	$0 status ${USER_INS_LIST}
	;;

  start|insert)
	#${MODPROBE} ${RTL_MODULES_LIST}

    /usr/local/fireball/flushBuffers
	for module in $RTL_MODULES_LIST
	do
      echo "Loading $module"
      ${MODPROBE} $module
      /usr/local/fireball/flushBuffers --verify
	  sleep 1
	done

	for modules in $USER_INS_LIST
	do
      sleep 1
      ${MODPROBE} $modules
	done

    # Loading the first rtai module sometimes overflows the kernel
    # stack (we think) resulting in corruption of the file system
    # buffers for some programs that are already in memory.
    # Here we check those that are known to have been corrupted to
    # verify that they are still okay.
    checkChecksum /sbin/modprobe 795814846
    checkChecksum /bin/bash 3496647556
    checkChecksum /bin/busybox 1079250080
    checkChecksum /lib/libcrypt-2.2.3.so 2246793921
    checkChecksum /lib/libm-2.2.3.so 1171445454
    checkChecksum /lib/libc-2.2.3.so 3604821520
    checkChecksum /lib/ld-2.2.3.so 779241293
    
    sleep 2
	;;

  stop|remove)
	for modules in $USER_DEL_LIST
	do
      MODINS=`${LSMOD} | ${GREP} ^$modules`
      if [ "$MODINS" ]
      then
        ${RMMOD} $modules
      fi
	done

	for modules in ${RTL_MODULES_LIST}
	do
      MODINS=`${LSMOD} | ${GREP} ^$modules`
      if [ "$MODINS" ]
      then
        ${RMMOD} $modules
      fi
	done

	;;
  *)
    usage 1 1
    exit 1
esac

# Exit normally
exit 0
