#!/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

# vm dirty byte counts
vm_dirty_bytes_file="/proc/sys/vm/dirty_bytes"
vm_dirty_ratio_file="/proc/sys/vm/dirty_ratio"
vm_dirty_bytes_val=0
vm_dirty_ratio_val=0
vm_dirty_bytes_min=8192

# -----------------------------------------------------------------------------
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
}

# -----------------------------------------------------------------------------
get_board_name_brego()
{
    board3940=/sys/bus/platform/devices/board_id.1
    board3960=/sys/bus/platform/devices/board_id.2

    if [ -e "$board3940" ]; then
        echo '3940'
    elif [ -e "$board3960" ]; then
        echo '3960'
    else
        echo 'unknown'
    fi
}

# -----------------------------------------------------------------------------
get_board_name_caliondo_5()
{
    case "$(cat /sys/bus/platform/devices/board_info/pwe)" in
            
        0)
            echo '3930ET' ;;
        1)
            echo '3932' ;;
        *)
            echo 'unknown' ;;
    esac
}

# -----------------------------------------------------------------------------
get_board_name_caliondo()
{
    case "$(cat /sys/bus/platform/devices/board_info/id)" in

        1)
            echo '5150' ;;
        2)
            echo '3916' ;;
        3)
            echo '3930' ;;
        4)
            echo '3931' ;;
        5)
            get_board_name_caliondo_5 ;;
        6)
            echo '5142' ;;
        7)
            echo '5160' ;;
        8)
            echo '3942' ;;
        9)
            echo '3938' ;;
        *)
            echo 'unknown' ;;
    esac
}

# -----------------------------------------------------------------------------
get_board_name_dernhelm()
{
    local model_file="/proc/device-tree/model"

    if [ -f "$model_file" ] ; then
        cat $model_file
    else
        echo 'unknown'
    fi
}

# -----------------------------------------------------------------------------
get_board_name()
{
    case "$(cat /family)" in
        "brego")    get_board_name_brego    ;;
        "caliondo") get_board_name_caliondo ;;
        "dernhelm") get_board_name_dernhelm ;;
        *) echo 'unknown' ;;
    esac
}

# -----------------------------------------------------------------------------
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

        vm_dirty_restore

        exit $EXIT_COMMAND_FAILED
    fi
}

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

# -----------------------------------------------------------------------------
vm_dirty_set() {
    echo $2 > $1
}

# -----------------------------------------------------------------------------
vm_dirty_shrink() {

    if [ "${vm_dirty_bytes_val}" != "0" -o \
         "${vm_dirty_ratio_val}" != "0" ]; then
        command_wrapper "setting ${vm_dirty_bytes_file} to ${vm_dirty_bytes_min}" \
            vm_dirty_set ${vm_dirty_bytes_file} ${vm_dirty_bytes_min}
    fi
}

# -----------------------------------------------------------------------------
vm_dirty_restore() {
    # either dirty_bytes or dirty_ratio must be set
    # the other one must be zero
    if [ "${vm_dirty_bytes_val}" != "0" ]; then
        command_wrapper "restoring ${vm_dirty_bytes_file} to ${vm_dirty_bytes_val}" \
            vm_dirty_set ${vm_dirty_bytes_file} ${vm_dirty_bytes_val}
    elif [ "${vm_dirty_ratio_val}" != "0" ]; then
        command_wrapper "restoring ${vm_dirty_ratio_file} to ${vm_dirty_ratio_val}" \
            vm_dirty_set ${vm_dirty_ratio_file} ${vm_dirty_ratio_val}
    fi
}

# -----------------------------------------------------------------------------
extract_lzma_tarball() {
    local image=$1; shift
    cat $image | xz -d -c | tar -xpf - $*
}

# -----------------------------------------------------------------------------
list_lzma_tarball() {
    local image=$1; shift
    cat $image | xz -d -c | tar -tf - $*
}

# ------------------------------------------------------------------------------
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" ]
}

# ------------------------------------------------------------------------------
is_5150()
{
    local board_id='/sys/bus/platform/devices/board_info/id'
    [ -r "${board_id}" ] && [ "$(<${board_id})" == "1" ]
}

# ------------------------------------------------------------------------------
copy_kernel() {
    # This function must agree with all the copy_kernel() in the
    # target-specific board_lib.sh scripts.

    # For boards that boot from SPI flash the kernel isn't in a
    # separate partition.
    spiboot_supported && return 1

    # U-Boot takes the kernel from the main fs image on caliondo NAND
    # boards, and the 5150 is the only caliando board with a NOR
    # flash.
    [[ "$(cat /family)" == "caliondo" ]] && ! is_5150 && return 1

    # Everything else (brego and 5150): copy the kernel.
    return 0
}

#--------------------------------------------------------------------
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 "unmark partial manifest" manifest_remove_blackmark
     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
}

# -----------------------------------------------------------------------------
function extract_uboot() {
    $extract_cmd -O $uboot_path | dd bs=128k of=$dst_uboot
}

# -----------------------------------------------------------------------------
function prepare_exclusion_list() {
    local uboot_path=$1

    local xlist=($uboot_path) # Exclude u-boot from main extraction

    # Write exclusion list to file, return filename
    local templist=$(mktemp -t temp$$.XXXXXX)
    printf "%s\n" "${xlist[@]}" > $templist
    echo "$templist"
}

# -----------------------------------------------------------------------------
# Escapes given string for use as a sed keyword
function sed_escape() {
    printf "$1" | sed 's/[]\/$*.^|[]/\\&/g'
}

# -----------------------------------------------------------------------------
function update_manifest() {
    local manifest_file=$1
    local xtract_list=$2

    # Remove excluded files from manifest
    while read x;
    do
        sed_x=$(sed_escape "$x")
        sed -i "/$sed_x/d" ${manifest_file}
    done < ${xtract_list}
}

# -----------------------------------------------------------------------------
manifest_add_blackmark() {
    # Add an entry to the manifest that will never exist.  This marks the
    # bank as invalid until the manifest file is fixed (used for two-part
    # upgrades)
    echo "00000000000000000000000000000000 *./blackmark/" >> ./MANIFEST
    echo "0000000000000000000000000000000000000000000000000000000000000000 *./blackmark/" >> ./MANIFEST_SHA256
}

# -----------------------------------------------------------------------------
manifest_remove_blackmark() {
    sed -i "/\*\.\/blackmark/d" ./MANIFEST
    sed -i "/\*\.\/blackmark/d" ./MANIFEST_SHA256
}

# -----------------------------------------------------------------------------
# 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"
invalidate_manifest="no"

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
         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
         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
    uboot_path=./ciena/images/u-boot-spi.img
else
    uboot_path=./ciena/images/u-boot.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 "uboot_path    = $uboot_path"
    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
        ;;
    *)
        case "$(get_board_name)" in
            "3932"|"5160")
                # For these platforms, we are not done until a second file
                # is installed, so make sure that the manifest reflects a
                # missing part.
                invalidate_manifest="yes" ;;
            *)
                ;;
        esac
        ;;
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

# Save the current vm dirty_bytes/dirty_ratio values.
read vm_dirty_bytes_val < "${vm_dirty_bytes_file}" || vm_dirty_bytes_val=0
read vm_dirty_ratio_val < "${vm_dirty_ratio_file}" || vm_dirty_ratio_val=0
# Set vm_dirty_bytes to the lowest possible value.
vm_dirty_shrink

tempxlist=$(prepare_exclusion_list $uboot_path)

# 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}/*

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}/*
    #
    # Aim ${dst_app_mount}/boot straight at ${dst_kern_mount}. And
    # remember to update the soft link between the alternate banks in
    # the software_protect script.
    #
    # The tar extract_cmd will redirect all files under ./boot to the
    # /mnt/kernel[AB] partition - which is the desired location on
    # brego and NOR-flash-based caliondo boards.
    #
    # The soft link also leaves the manifest file paths intact, and it
    # keeps happy the FPGA installers that expect images under ./boot.
    #
    exit_on_error "add boot soft link" ln -s ${dst_kern_mount} ${dst_app_mount}/boot
fi

# extract the rest of the files
exit_on_error "extract new files"      $extract_cmd -X $tempxlist
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

if copy_kernel; then
    exit_on_error "remount kernel ro"  mount ${dst_kern_mount} -o remount,ro
fi

# Exclude the U-Boot image from the manifest files
update_manifest ./MANIFEST_SHA256 ${tempxlist}
update_manifest ./MANIFEST ${tempxlist}

# Finally remove the exclusion list file
rm -f $tempxlist

# 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 [ "$invalidate_manifest" == "yes" ] ; then
    exit_on_error "remount app rw"         mount $dst_app_mount -o remount,rw
    exit_on_error "mark partial manifest"  manifest_add_blackmark
    exit_on_error "remount app ro"         mount $dst_app_mount -o remount,ro
fi

exit_on_error "extract u-boot"         extract_uboot
exit_on_error "syncing app"            sync

# Restore the original vm dirty values.
vm_dirty_restore

exit $EXIT_SUCCESS
