#! /bin/sh
####################################################################
#
#      File: rcS.include
#      Name: Marco Di Benedetto
#
#       Description:
#  initialization script for vegas isan software to be run at boot up from
#	inittab
#	Actual location on disk: /etc/rc.d/include/rcS.include
#
#
# Copyright (c) 1985-2004, 2015-2017 by cisco Systems, Inc.
# All rights reserved.
#
#
# $Id: rcS.include,v 1.20 2002/12/03 04:11:06 cmishra Exp $
# ---------------------------------------------------------------
#####################################################################

# By default, everything gets logged to a file but doesn't go to the
# console

if [ -f /etc/rc.d/init.d/get_card_inst.sh ]; then
    source /etc/rc.d/init.d/get_card_inst.sh
fi

#
#	Trap CTRL-C &c 
#
trap "" INT QUIT TSTP 

# export necessary variables - note we cannot just blindly export everything
# from /proc/cmdline - that would be a security hole (e.g., PATH=xxx)
# so we filter out any variables starting with CAP
# we also filter options with a "." in it (for ex. ide_generic.probe_mask=0x0)
    
    
#for word in $NEWENV;
#do
#    if [ "${word#[A-Z]*}" = "$word" ] && [ "${word#*\.*=}" = "$word" ]; then
#        export $word
#    fi
#done

export BOOTLOG_FILE=/var/log/boot.log
export LOG_CONSOLE=0

boot_debug() {
    if [ $LOG_CONSOLE -eq 0 ]; then
        echo $* >>${BOOTLOG_FILE}
    else
        echo $*
    fi
}

action() {

    if [ "$1" != "" ]; then
        boot_debug $1
    fi

    shift
    boot_debug $*
    $*
    return $?
}

start_vshboot() {
    boot_debug "$1"
    echo $1 > /var/log/vshboot.reason

    action "Entering vshboot" /sbin/telinit 1
}

makedev () {    # usage: makedev name [bcu] major minor owner group mode
        rm -f /dev/$1-
        mknod /dev/$1- $2 $3 $4 &&
        chown $5:$6 /dev/$1- &&
        chmod $7 /dev/$1- &&
        mv /dev/$1- /dev/$1 
}

make_chrdev () {
    local devname=$1
    local major_num
    local dev_name

    dev_name=$(cat /proc/devices|grep $devname|cut -d' ' -f2)
    major_num=$(cat /proc/devices|grep $devname|cut -d' ' -f1)
    mknod /dev/$dev_name c $major_num 0
    
}

make_blkdev () {
    local devname=$1
    local num_parts=$2
    local part_i=1

    local major_num
    if [ ! -e /sys/block/$devname ]; then
       return
    fi
    major_num=$(cat /sys/block/$devname/dev|cut -d':' -f1)
    mknod /dev/$dev_name b $major_num 0
    while [ $part_i -le $num_parts ];
    do
        mknod /dev/$dev_name b $major_num $part_i
        part_i=$((part_i+1))
    done
}

print_execute_header() {
    shopt -q login_shell
    if [ $? -eq 0 ]; then
        boot_debug "Executing login shell..."
    else
        boot_debug "Executing $(dirname $0)/$(basename $0) ..."
    fi
}

# import cmdline parser helpers
proc_cmdline_get() {
    local field=$1
    local val
    echo "$(cat /proc/cmdline|awk -F"$field=" '{print $2}'|cut -d' ' -f1)"
}

get_cmdline_card_index() {
    local cindex=0
    if [ -e "/opt/cisco/thinxr" ]; then
        # This is thinXR so read the card_index from the FPGA,
        # since it isn't present in the cmdline.
        source /etc/rc.d/init.d/spirit_pd.sh
        cindex="$(get_card_index_from_fpga)"
    else
        cindex="$(proc_cmdline_get card_index)"
    fi
    echo "$cindex"
}

 
get_cctrl_card_type() {
   # only RP Is present for zermatt
   # SUP KEYSTONE
    echo "3"
}

# returns the boardtype string with more generic classification
get_cctrl_board_type() {    
    echo "RP"
}

# List of one wire codes 
# to be used in the scripts
# Be in sync with include/platform/cctrl_owc.h

PRE_CCTRL_OK=0xe5
CCTRL_OK=0xe6
CHECK_FLASH_BEGIN=0xe7
CHECK_FLASH_OK=0xe8
PRE_SYSMGR_OK=0xe9
KLM_LOAD_FAILED=0x50

send_ow_err_code() {
    local err_code=$(printf "0x800000%x\n" $1)
    echo "Sending one-wire error code using SUP IOFPGA offset" > /dev/console
    iorw w 0xe8000250 $err_code w
}

# import platform vars
#. /etc/rc.d/include/set-platform-vars

# All scripts will print the executing header
print_execute_header



# from the /proc/devices create a /dev entry
# first argument : entry name in /proc/devices
# second argument: File name, e.g: /dev/redun
install_devices () 
{
    local kern_name=$1
    local dev_name=$2
    local devno

    boot_debug "Entering install_devices($1, $2)"

    devno=$(grep $1 /proc/devices | awk '{print $1}')
    if [ -z "${devno}" ]; then
        echo "$1 module not loaded"
        exit 2
    fi

    action "" rm -rf ${dev_name}
    action "" mknod ${dev_name} c ${devno} 0

    chmod 644 ${dev_name}
    
    if [ $? -ne 0 ]; then
	echo "mknod failed for ${dev_name}: error $?"
	exit 3
    fi

    boot_debug "device $2 installed"
}
    
#ModuleExists()
#This function returns 1 if the 
#module $1 is currently loaded in the system
ModuleExists()
{
    exists=`lsmod | grep $1`
    if [ -z "$exists" ]
    then
      return 1;
    else
      return 0;
    fi
}

#InstallModule()
#This function installs a module and creates the device file
#Usage: InstallModule kernel_module_name [kernel_device_name device_file_name]
#Example: InstallModule klm_utaker.o utaker /dev/utaker
InstallModule()
{
    local mod_name=$1

    boot_debug "Entering InstallModule($1, $2, $3)"

    action "Installing $1." insmod -f /lib/modules/${mod_name}.o 

    saved_error=$?
    if [ $saved_error -ne 0 ]
    then
	echo "Installation of ${mod_name} failed: error ${saved_error}."
        exit ${saved_error}
    else
	boot_debug "${mod_name} installed successfully."
    fi
        
    if [ -n "$2" -a -n "$3" ]; then
       install_devices $2 $3
    fi
}
    
#RemoveModule()
#This function removes a module and exits
#if the module exists and cannot be removed
#Usage: RemoveModule kernel_module_name [device_file_name]
#Example: RemoveModule klm_utaker [/dev/utaker]
RemoveModule()
{
    boot_debug "Entering RemoveModule($1, $2)"
  
    ModuleExists $1
    if [ $? -eq 0 ]
    then 
	# kill all users of the device file if they exist
	if [ -n "$2" ]
	then
	    fuser -k $2
	fi

	action "Removing $1." rmmod $1
	    
	saved_error=$?
	if [ $saved_error -ne 0 ]
	then
	    echo "Removal of $1 failed: error ${saved_error}."
	    exit ${saved_error}
	else
	    boot_debug "$1 removed successfully."
	fi
    fi

    if [ -n "$2" ]; then
	action "Removing unused device $2" rm -f $2
    fi
}

my_slot_id () {

    init_iofpga_base_addr_zermatt

    # Read the value
    SLOTADDR=$(printf 0x%08X $(($PCI_BASE_ADDR_CPU_IOFPGA + 0xc + $IOFPGA_OFFSET)))

    SLOT=$(pcimemread $SLOTADDR 4 | grep : | awk '{ print $3 }' \
	| sed 's,\(..\)\(..\)\(..\)\(..\),\4,g')

    SLOT=0x$SLOT   
    let "y= $SLOT % 0x20"
    case $1 in
        x)
            a=`printf "%x" $y`
            ;;
        *)
            a=`printf "%d" $y`
            ;;
    esac
    echo $a
}

