#!/bin/bash
#
# This script is used to upgrade a bank using the contents of a tarball
#

# globals
debug=0       # enabled by --debug or --dry-run
dry_run=0     # enabled by --dry-run
force=0       # enabled by --force

# exit codes (constants - don't change these, as they will be used in app code)
EXIT_SUCCESS=0
EXIT_INVALID_ARGS=1
EXIT_NO_SOURCE=2
EXIT_NOT_ROOT=3
EXIT_COMMAND_FAILED=4

# -----------------------------------------------------------------------------
print_usage_and_exit() {
    echo "Usage: $this_script [option] <A|B> <tarball> [optional tarball]"
    echo
    echo -e "\tThis script will upgrade the given bank with the contents of the given tarball."
    echo
    echo "Options:"
    echo -e "\t--debug    displays debug information while running"
    echo -e "\t--dry-run  shows commands that will be run (but does nothing)"
    echo -e "\t--force    erases and reformats the target bank before starting"
    echo -e "\t--help     shows this message"
    echo
    exit $EXIT_INVALID_ARGS
}

# -----------------------------------------------------------------------------
command_wrapper() {
    local comment=$1; shift
    local command_line="$*"

    # CONSOLE_COLLECTOR must match definition in rcS
    local CONSOLE_COLLECTOR=/tmp/log/console_collector

    # if the collector isn't a pipe then don't use it
    if [ ! -p "$CONSOLE_COLLECTOR" ] ; then
        CONSOLE_COLLECTOR=/dev/null
    fi

    if [ "$debug" -eq "1" ] ; then
        echo "# $comment"
        echo "$command_line"
        echo
    fi

    if [ "$dry_run" -eq "1" ] ; then
        return 0
    fi

    if [ "$debug" -eq "1" ] ; then
        $command_line
    else
        $command_line > /dev/null 2> $CONSOLE_COLLECTOR
    fi

    return "$?"
}

# -----------------------------------------------------------------------------
exit_on_error() {
    local comment=$1

    command_wrapper "$@"

    if [ "$?" -ne "0" ] ; then
        echo "ERROR: $comment failed"

        # try to mount things the way they are supposed to be mounted
        command_wrapper "mount app ro"    mount $dst_app_mount -o remount,ro

        if copy_kernel; then
            command_wrapper "mount kernel ro" mount $dst_kern_mount -o remount,ro
        fi

        exit $EXIT_COMMAND_FAILED
    fi
}

# -----------------------------------------------------------------------------
die() {
    echo $2
    exit $1
}

# -----------------------------------------------------------------------------
extract_lzma_tarball() {
    cat $1 | xz -d -c | tar -xpf -
}

# ------------------------------------------------------------------------------
spiboot_supported() {

    # All current dernhelm platforms should be treated as spi boot
    if [ "$(cat /family)" == "dernhelm" ] ; then
        return 0
    fi

    spiboot_info='/sys/bus/platform/devices/board_info/spiboot'
    [ -r "${spiboot_info}" ] && [ "$(<${spiboot_info})" == "1" ]
}

# ------------------------------------------------------------------------------
cr3916()
{
    cr_info='/sys/bus/platform/devices/board_info/cr'
    [ -r "${cr_info}" ] && [ "$(<${cr_info})" == "1" ]
}

# ------------------------------------------------------------------------------
copy_kernel() {
    # For boards that boot from SPI flash the kernel isn't in a separate partition.
    ! spiboot_supported  && ! cr3916
}

#--------------------------------------------------------------------
find_mtd_device() {
    for x in "$@"; do
        for y in /sys/class/mtd/mtd*; do
            if [ -f $y/name ] && [ "$(<$y/name)" == "$x" ]; then
                basename $y
                return 0
            fi
        done
    done

    return 1
}

# -----------------------------------------------------------------------------
flush_cache() {
    sync
    echo 3 > /proc/sys/vm/drop_caches
}

# -----------------------------------------------------------------------------
sha256sum_check() {
    # run sha256sum check on the files in the manifest
    sha256sum -s -c ./MANIFEST_SHA256
}

# -----------------------------------------------------------------------------
get_mtd_part() {
    # Convert partitions of the format /dev/mtdXblock or mtd:name to their
    # character device counterparts /dev/mtdX or /dev/mtd/name.

    local mnt=$1
    local part=${mnt/block}

    if [ "$mnt" != "$part" ]; then
        echo $part
    else
        echo /dev/mtd/${mnt/#mtd:}
    fi
}

# -----------------------------------------------------------------------------
# $1 -- directory $2- install command
install_board_specific_files() {
     local dir=$1
     local cmd=$2
     local tmpManifest=$(mktemp -t temp$$.XXXXXX)
     exit_on_error "remount $dir rw"        mount $dir -o remount,rw
     exit_on_error "cd to $dir"             cd $dir

     # save the MANIFEST file from the previous tarball
     exit_on_error "mv MANIFEST(sha256)"     mv $dir/MANIFEST_SHA256 $tmpManifest

     exit_on_error "extract new files"      $cmd
     exit_on_error "flushing cache"         flush_cache

     if [ -f "$dir/MANIFEST_SHA256" ]; then
        exit_on_error "sha256sum check"    sha256sum_check
        chmod 644 $dir/MANIFEST_SHA256
        cat $tmpManifest >> $dir/MANIFEST_SHA256
        rm  $tmpManifest
     else
        exit_on_error "restore manifest(sha256)"  mv $tmpManifest $dir/MANIFEST_SHA256
     fi
     exit_on_error "flushing cache"         flush_cache
     exit_on_error "remount $dir ro"        mount $dir -o remount,ro
}

# -----------------------------------------------------------------------------
flash_format() {
    local jffs2=""

    case $1 in
        "--jffs2")  jffs2="--jffs2"; shift ;;
    esac

    local device=$1

    [ -c "$device" ] || die $EXIT_COMMAND_FAILED "No such device ($device)"

    # Support for --version was atomically introduced when mtd-utils
    # deprecated flash_eraseall.
    if flash_erase --version > /dev/null 2>&1; then
        nice flash_erase    --quiet $jffs2 $device 0 0
    else
        nice flash_eraseall --quiet $jffs2 $device
    fi

}

# -----------------------------------------------------------------------------
find_ubi_volume() {
    # $1: volume name
    for x in /sys/class/ubi/ubi*; do
        if [ -f $x/name ] && [ "$(<$x/name)" == "$1" ]; then
            basename $x
            return 0
        fi
    done

    return 1
}


# -----------------------------------------------------------------------------
ubifs_truncate() {
    local volume=$1
    local vid

    vid=$(find_ubi_volume $volume)

    [ $? == 0 ] || die $EXIT_COMMAND_FAILED "Failed to find ubi volume $volume"

    nice ubiupdatevol /dev/${vid} --truncate

}

# -----------------------------------------------------------------------------
erase_partition() {
    local name=$1
    local mount=$2

    local fs_entry
    local fs_type
    local device
    local rc

    while read -a fs_entry; do
        if [ ${fs_entry[1]} = $mount ]; then
            device=${fs_entry[0]}
            fs_type=${fs_entry[2]}
            break
        fi
    done < /proc/mounts

    if [ "$debug" -eq "1" ] ; then
        echo "erase_partition()"
        echo "$name device  = $device"
        echo "$name fs_type = $fs_type"
    fi

    command_wrapper "unmount $name partition" umount $mount

    case "$fs_type" in
        jffs2) command_wrapper "format $name partition" \
                        flash_format --jffs2 $(get_mtd_part $device) ;;
        ubifs) command_wrapper "truncate $name partition" \
                        ubifs_truncate ${device#*:} ;;
        *)     die $EXIT_COMMAND_FAILED "Unknown filesystem for $name" ;;
    esac

    rc=$?

    # ubifs needs to be mounted read/write the first time
    exit_on_error "mount $name partition" mount -o rw $mount

    return $rc
}

# -----------------------------------------------------------------------------
# Main script

# Take note of our name as invoked
this_script=$(basename $0)

# handle options
while :; do
    case "$1" in
        "--help"|"-h"|"-?"|"help") print_usage_and_exit ;;
        "--debug") debug=1 ; shift ;;
        "--dry-run") debug=1 ; dry_run=1; shift ;;
        "--force") force=1; shift ;;
        *) break ;;
    esac
done

if [ "$#" -ne "2" ] ; then
    if [ "$#" -ne "3" ] ; then
        echo "ERROR: wrong number of parameters"
        print_usage_and_exit
    fi
fi

active_image="$1"
src_image="$2"
src2_image="$3"

if [ "$(whoami)" != "root" ] ; then
    die $EXIT_NOT_ROOT "ERROR: must run $this_script as root"
fi

case "$active_image" in
    a|A) active_image="A"
         dst_app_mount=/mnt/imageA
         dst_kern_mount=/mnt/kernelA
         src_kernel=${dst_app_mount}/boot/*
         dst_kernel=${dst_kern_mount}/
         src_uboot=${dst_app_mount}/ciena/images/u-boot-A.img

         dst_uboot=/dev/$(find_mtd_device u-boot-a "Uboot A")
         if [ "$?" -ne "0" ] ; then
             die $EXIT_COMMAND_FAILED "Unable to locate mtd device for u-boot a"
         fi
         ;;
    b|B) active_image="B"
         dst_app_mount=/mnt/imageB
         dst_kern_mount=/mnt/kernelB
         src_kernel=${dst_app_mount}/boot/*
         dst_kernel=${dst_kern_mount}/
         src_uboot=${dst_app_mount}/ciena/images/u-boot-B.img

         dst_uboot=/dev/$(find_mtd_device u-boot-b "Uboot B")
         if [ $? -ne 0 ]; then
             die $EXIT_COMMAND_FAILED "Unable to locate mtd device for u-boot b"
         fi
         ;;
    *)   active_image=""
         ;;
esac

# Boards that boot from SPI flash use a special U-Boot image.
if spiboot_supported; then
    src_uboot=${dst_app_mount}/ciena/images/u-boot-spi.img
fi

if [ -z "$active_image" ] ; then
    die $EXIT_NO_SOURCE "ERROR: unable to determine source bank"
fi

if [ ! -f "$src_image" ] ; then
    die $EXIT_NO_SOURCE "ERROR: $src_image missing"
fi

case "$src_image" in
    *.gz)
         extract_cmd="tar -xzpf $src_image"
         ;;
    *.xz)
         extract_cmd="extract_lzma_tarball $src_image"
         ;;
    *)
         echo "ERROR: $src_image has invalid extension"
         exit $EXIT_NO_SOURCE
         ;;
esac

case "$src2_image" in
    *.gz)
         extract2_cmd="tar -xzpf $src2_image"
         ;;
    *.xz)
         extract2_cmd="extract_lzma_tarball $src2_image"
         ;;
    *) ;;
esac

if [ "$debug" -eq "1" ] ; then
    echo "active_image  = $active_image"
    echo "dst_app_mount = $dst_app_mount"
    echo "dst_kern_mount= $dst_kern_mount"
    echo "src_kernel    = $src_kernel"
    echo "dst_kernel    = $dst_kernel"
    echo "src_uboot     = $src_uboot"
    echo "dst_uboot     = $dst_uboot"
    echo "extract_cmd   = $extract_cmd"
fi

case "$src_image" in
    *.3932.tar.gz|*.5160.tar.gz)
        install_board_specific_files "$dst_app_mount" "$extract_cmd"
        exit $EXIT_SUCCESS
        ;;
    *)
        ;;
esac

# If the force option is specified, then unmount the destination partitions
# and erase them.
#
if [ "$force" -eq "1" ] ; then
    if copy_kernel; then
        erase_partition "kernel" ${dst_kern_mount} \
            || die $EXIT_COMMAND_FAILED "failed to erase kernel partition"
    fi

    erase_partition "app"  ${dst_app_mount}  \
            || die $EXIT_COMMAND_FAILED "failed to erase app partition"
fi

# Main protect logic
#
# Ensure that the u-boot partition is erased first, as this marks the partition
# as bad, in case we get interrupted or encounter an error.
#
exit_on_error "erase u-boot partition" flash_format $dst_uboot
exit_on_error "remount app rw"         mount $dst_app_mount -o remount,rw
exit_on_error "cd to $dst_app_mount"   cd $dst_app_mount
exit_on_error "remove old files"       rm -rf ${dst_app_mount}/*
exit_on_error "extract new files"      $extract_cmd
exit_on_error "remove fpga files"      rm -rf /mnt/sysfs/fpga/*
# sometimes need 2nd source file (like for 5160)
if [ -n "${extract2_cmd}" ]; then
   exit_on_error "extract additional files"	$extract2_cmd
fi

# flush the cache to ensure the sha256sum runs against the flash content
exit_on_error "flushing cache"         flush_cache
exit_on_error "remount app ro"         mount $dst_app_mount -o remount,ro
exit_on_error "sha256sum check"        sha256sum_check

if copy_kernel; then
    exit_on_error "remount kernel rw"      mount $dst_kern_mount -o remount,rw
    exit_on_error "remove old kernel"      rm -rf ${dst_kern_mount}/*
    exit_on_error "copy kernel"            cp $src_kernel $dst_kernel
    # flush the cache again to ensure the diff runs against the flash content
    exit_on_error "flushing cache"         flush_cache
    exit_on_error "remount kernel ro"      mount $dst_kern_mount -o remount,ro
    exit_on_error "checking kernel"        diff -r ${src_kernel%\/*} $dst_kernel
fi

exit_on_error "copy u-boot"            dd bs=128k if=$src_uboot of=$dst_uboot
exit_on_error "syncing app"            sync

exit $EXIT_SUCCESS
