#!/bin/sh
# @file
#
# @copyright
# Copyright 2009, Zedi Inc.  All rights reserved.
# This file contains confidential intellectual property of Zedi Inc.
#
# @details
# Script to determine and report the versions of the CPLD and AVR
# firmware, sleep_sbc executable, and tsuart-pac4.o kernel driver on
# Zed-PAC hardware.  Requires both 'sleep_sbc' and 'peekpoke' to be
# installed and in the PATH.
#
# @testing
#  1) old CPLD (v6)
#  2) new CPLD (v1)
#  3) old AVR (v2) with sleep_sbc v1
#  4) old AVR (v2) with sleep_sbc v2.X
#  5) old AVR (v2) with sleep_sbc v3.X
#  6) new AVR (v4) with sleep_sbc v1
#  7) new AVR (v4) with sleep_sbc v2.X
#  8) new AVR (v4) with sleep_sbc v3.X
#  9) newer AVR (v5) with sleep_sbc v1
# 10) newer AVR (v5) with sleep_sbc v2.X
# 11) newer AVR (v5) with sleep_sbc v3.X
#
# @history
# IncrDev Jun 04, 2009 by fl: remove descriptions for CPLD and AVR revisions
# IncrDev May 26, 2009 by fl: support for AVR v1.6, increment version to 1.1
# BugFix_ May 25, 2009 by fl: fix interpretation of CPLD, tsuart, AVR versions as per Long and MikeB,
#                             handle peekpoke errors
# IncrDev May 23, 2009 by fl: v1.0: fix interpretation of AVR versions
# IncrDev May 22, 2009 by fl: implement AVR version, some fixes
# Created May 21, 2009 by farrenl:
# @endhistory
#

# : version of this script
# NOTE - ONLY CHANGE ON FIRST MODIFICATION FROM LAST RELEASED VERSION
scriptV=1.1

# : initialization (assume the worst)
sbcV="?  (could not find 'sleep_sbc' tool)"
avrV="?  (could not find 'sleep_sbc' tool)"
cpldV="?"
uartV="?  (not installed)"

# : determine AVR (and sleep_sbc) versions using sleep_sbc
which sleep_sbc >/dev/null 2>&1
if [ $? -eq 0 ]; then

# :- determine sleep_sbc version
    t=`sleep_sbc -v 2>&1`
    if echo $t | grep -q Usage: ; then
        sbcV=1.0                    # historical version
    elif echo $t | grep -q version ; then
        sbcV=`echo $t | grep version | cut -d\  -f3`
    fi

# :- determine AVR version using sleep_sbc (bits [7:2] from status code)
    case "$sbcV" in
        1.0)
            version=0                         # can't tell what version
            ;;
        2.*)
            # determine AVR status and update AVR status file
            status=`sleep_sbc | cut -d: -f2`

            # DOC - sleep_sbc v2.x was dumb and the old AVR didn't send back
            # the version number; so if the AVR did not give us a version we
            # must read the status file and determine the version from the
            # protocol type.

            # get protocol type from AVR status file
            type=0
            avr_file=/var/lib/avr/status
            if [ -f $avr_file ]; then
                type=`grep version $avr_file | cut -d: -f2` 
            fi

            # determine version based on status and protocol type
            let version="$status / 4"
            if [ $type -eq 0 ]; then
                version=2               # original AVR code (from avr.c)
            elif [ $status -eq 0 ]; then
                version=3     # newer AVR but not versioned (from avr.c)
            fi
            ;;
        3.* | *)
            version=`sleep_sbc -a | grep version | cut -d: -f2`
            let version="$version + 0"  # removes surounding whitespace
            ;;
    esac

# :- interpret the version and add explanations
    case "$version" in
        0)
            avrV="?  (sleep_sbc can't tell)"
            ;;
        2)
            avrV="1.$version  (original)"
            ;;
        *)
            avrV="1.$version"
            ;;
    esac
fi

# : determine CPLD version (bits [2:0] from address 0x23400000)
which peekpoke >/dev/null 2>&1
if [ $? -eq 0 ]; then
    t=`peekpoke 8 0x23400000`
    if [ $? -eq 0 ]; then
        let cpldV="$t & 7"
        case "$cpldV" in
            6)
                cpldV="1.$cpldV  (original)"
                ;;
            *)
                cpldV="2.$cpldV"
                ;;
        esac
    else
        cpldV="?  (tool 'peekpoke' returned error)"
    fi
else
    cpldV="?  (could not find 'peekpoke' tool)"
fi

# : determine tsuart-pac4.o version (compare md5sum of file)
tsuart=/lib/modules/2.4.26-ts11/kernel/drivers/serial/tsuart-pac4.o
if [ -f $tsuart ]; then
    md5=`md5sum $tsuart | cut -d\  -f1`
    case "$md5" in
        00bcd8de575595e490aa4a70eab27813)
            uartV="1.4"
            ;;
        2719d01c279b0827797d000657ebcb53)
            uartV="1.6"
            ;;
        *)
            uartV="?"        
            ;;
    esac
    uartV="$uartV  (md5sum: $md5)"
fi

# : report versions
echo "Zed-PAC System Versions (script v$scriptV)"
echo "-------------------------------------"
echo "CPLD:           $cpldV"
echo "AVR:            $avrV"
echo "tsuart-pac4.o:  $uartV"
echo "sleep_sbc:      $sbcV"

# : cleanup and return
exit 0
