#!/bin/sh -e

# Copyright (c) 2017, Mellanox Technologies
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation are those
# of the authors and should not be interpreted as representing official policies,
# either expressed or implied, of the FreeBSD Project.

PROGNAME=$(basename "$0")
TMP_DIR=$(mktemp -d)
trap "rm -rf $TMP_DIR" EXIT
trap "rm -rf $TMP_DIR; exit 1" TERM INT

usage ()
{
    cat <<EOF
Usage: $PROGNAME [--help|-h]
       $PROGNAME [--part|-p PARTITIONS]

Prints version information for currently installed software.

Options:
  --help,-h              print this message
  --part,-p PARTITIONS   print version information for all boot partitions
                         specified in a comma-delimited list. For example,
                         "-p0,1" will print bootloader versions for
                         partition 0, and then partition 1. "-p1" will
                         print only partition 1. If -p is not specified,
                         the current primary partition will be printed.
EOF
}

print_bootloader_ver () {
    DEV_PATH=$1
    CURRENT_BFB="$TMP_DIR/$(basename $DEV_PATH).bfb"

    # Retrieve default.bfb from the eMMC
    mlxbf-bootctl -r "$DEV_PATH" -b "$CURRENT_BFB" 2>&1 >/dev/null

    echo "--$DEV_PATH"

    # Find and print the ATF version of current bfb
    echo BlueField ATF version: "$(strings "$CURRENT_BFB" | grep -m 1 "(\(release\|debug\))")"

    # Find and print the UEFI of current bfb
    echo BlueField UEFI version: "$(strings -e l "$CURRENT_BFB" | grep "BlueField" | cut -d':' -f 2)"
}


PARSED_OPTIONS=$(getopt -n "$PROGNAME" -o hp: -l help,part: -- "$@")
eval set -- "$PARSED_OPTIONS"

PARTLIST=

while true
do
    case $1 in
        -h | --help)
            usage
            exit 0
            ;;
        -p | --part)
            PARTLIST="$2"
            shift 2
            ;;
        --)
            shift
            break
            ;;
    esac
done

# Print bootloader versions
if [ -z "$PARTLIST" ]; then
    # Identify current primary boot partition and print versions
    print_bootloader_ver $(mlxbf-bootctl | grep "primary" | cut -d ' ' -f 2)
else
    for part in $(echo $PARTLIST | cut -d"," -f1- --output-delimiter=" "); do
        if [ "$part" -ne 0 ] && [ "$part" -ne 1 ]; then
            echo "$PROGNAME: err: bad partition $part" >&2
            continue
        fi

        print_bootloader_ver /dev/mmcblk0boot$part
    done
fi

echo

# Print BlueField version number if Yocto version file is present
if [ -e "/etc/bluefield_version" ]; then
	echo BlueField Release Version: "$(cat /etc/bluefield_version)"
else
	echo No Yocto version file present
fi
