#!/bin/sh
#
#    Copyright (c) 1996-2001 by Brocade Communications Systems, Inc.
#    All rights reserved.
#
#
# NAME
#      cftest - run compact flash memory sanity check
# 
# SYNOPSIS
#      cftest [ -t mode ] [ -r pattern ] [ -p npass] [ -w secs ]
# 
# AVAILABILITY
#      factory
# 
# DESCRIPTION
#      This command will test one partition of compact flash memory
#      which is not being mounted under root. The READONLY mode and
#      READWRITE mode are not destructive but the file system might
#      be  damaged if the user interrupts the test in the middle of
#      the test If the file system is damaged, you can recover  the
#      partition  from  the other partion (the root file system) by
#      running :
# 
#            > mkfs -t xfs /dev/hda2 (or /dev/hda1)
#            > mount -t xfs /dev/hda2 (or /dev/hda1) /mnt
#            > firmwarecommit
# 
#      (WARNING) Your  use  of  the  functionality  made  available
#      through the root or factory account is at your sole risk and
#      you assume all liability resulting from such use.
# 
# OPTIONS
#      The following operands are optional and the  default  action
#      is to test the second partition in READONLY mode once.
# 
#      -t mode   Test mode selection.  There  are  three  modes  of
#                tests.
# 
#                0    READONLY - It detects unreadable blocks  (bad
#                     blocks).
# 
#                1    READWRITE - It detects content corruptions as
#                     well  as  unreadable/unwritable  blocks. This
#                     test is not destructive as far as it has  not
#                     been interrupted.
# 
#                2    EXTENDED - It detects incorrect addressing in
#                     addition  to  those  detectable  by READWRITE
#                     mode. It writes the entire  blocks,  then  it
#                     verifies  the  contents.   If -p is specified
#                     with npass.  The  test  will  repeat  content
#                     verification as many times as npass.
# 
#      -r pattern
#                Select a pattern for the test. It does not  affect
#                READONLY mode.  The default pattern is random pat-
#                tern.
# 
#                0    0XAA
# 
#                1    0X55
# 
#                2    0XFF
# 
#                3    0X00
# 
#                4    Random pattern
# 
#      -p npass  Number of repetition. In EXTENDED mode.  It writes
#                once  and  reads/verifies  blocks  repetitively as
#                many times as npass.
# 
#      -w secs   Interval between writing phase and reading  phase.
#                The unit is in seconds. This only affects EXTENDED
#                mode.
# 
# SEE ALSO
#      blockcheck(1d)
# 
# NOTES
#      This command calls blockcheck internally.
# 

######################################################################
# utility programs with full path
mountcmd="/bin/mount"
umountcmd="/bin/umount"
grepcmd="/bin/grep"
envcmd="/sbin/bootenv"
cutcmd="/usr/bin/cut"
headcmd="/usr/bin/head"
testcmd="/fabos/factory/blockcheck"
######################################################################

######################################################################
# default option values
mode="0"
pat="4"
wait="0"
pass="1"
######################################################################

usage="cftest [-t mode] [-r pattern] [-p pass [-w interval]]"
while getopts "t:r:p:w:" opt; do
    case $opt in
	t  ) mode=$OPTARG ;;
	r  ) pat=$OPTARG ;;
	p  ) pass=$OPTARG ;;
	w  ) wait=$OPTARG ;;
	\? ) echo $usage
	     exit 1 ;;
    esac
done

mounttmp="$mountcmd | $grepcmd \"hda\"  | $headcmd -1 "
partition=`eval $mounttmp | $cutcmd -d' ' -f1`

######################################################################
# root partition and the other one can be known by "bootenv"
# command if "mount" doesn't get the information needed.
#
# [note] the device name is not full path(sans /dev/).
envtemp="$envcmd | $grepcmd \"OSRootPartition\" | $cutcmd -d'=' -f2 "
######################################################################
if [ -n "$partition" ]; then
    if $umountcmd $partition
    then
	echo "$partition is unmounted for test successfully."
    else
	echo "Unmounting $partition failed."
	echo "Please run badblocks manually."
	exit 1
    fi
    
    $testcmd -v -r $pat -p $pass -t $mode -w $wait $partition

    mount_point=`eval $mounttmp | $cutcmd -d' ' -f3`
    mount_fstype=`eval $mounttmp | $cutcmd -d' ' -f5`

    echo "Now mounting the partition under original mount point."

    if [ $mode = 2 ] || [ $mode = 3 ]; then
	##########################################################
	# file system might have been corrupted during test
	# (caution) extended test & read/write test are destructive.
	echo "The file system on $partition is unusable."
	echo "To recover the partition manually, please run :"
	echo ">mkfs  -t $mount_fstype $partition"
	echo ">mount -t $mount_fstype $partition $mount_point"
	echo ">bootenv Upgrade $root_partition"
	echo ">firmwarecommit"
	##########################################################
    else
	if $mountcmd -t $mount_fstype $partition $mount_point
	then
	    echo "$partition is mounted successfully."
	else
	    root_partition=`eval $envtemp | $cutcmd -d';' -f1`

	    ##########################################################
	    # file system might have been corrupted during test
	    echo "Mounting $partition failed."
	    ##########################################################
	    exit 1
	fi
    fi
else
    ##################################################################
    # test partition is not mounted
    partition=`eval $envtemp | $cutcmd -d';' -f2`
    $testcmd -v -t $mode -r $pat -p $pass -w $wait /dev/$partition
    ##################################################################
fi



