#!/bin/sh
#
# Title      :	bladeRF-install-firmware
# Purpose    :  fetch non-free BladeRF firmware
# Author     :	A. Maitland Bottoms <bottoms@debian.org>
# Date       :	2013-10-10
# Update     :	2026-06-20
#
# Copyright 2013-2026 A. Maitland Bottoms <bottoms@debian.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

set -e

show_usage()
{
    echo "Usage: $0 [-i imagedir] [imagetarball]" >&2
    exit 1
}

FX3UPSTREAM='https://www.nuand.com/fx3/bladeRF_fw_v2.6.0.img'
FX3CHECKSUM='7cc51b4eedebf54dd01be83745d1c59f'
FX3SHA256SUM='a5f65bffd51107c09e36b45eda77593d900d975388bcfd83a9b6c3c45240b54a'
FX3IMGFILE='/usr/share/Nuand/bladeRF/bladeRF_fw.img'

FPGAWLANXA9STREAM='https://www.nuand.com/fpga/v0.15.3/wlanxA9.rbf'
FPGAWLANXA9CHECKSUM='fb1dc76308b64ffe5f2a4146e44c7b79'
FPGAWLANXA9SHA256SUM='9f1fd747561a77f5f23e5d80785f161a4fb01c3f849f87a9fc309590d73c09bf'
FPGAWLANXA9RBFILE='/usr/share/Nuand/bladeRF/wlanxA9.rbf'

FPGA115UPSTREAM='https://www.nuand.com/fpga/v0.16.0/hostedx115.rbf'
FPGA115CHECKSUM='a7f25be8abdab3835e734599eb846ea4'
FPGA115SHA256SUM='7bb0c7b0fb9976cd631b679352aff3383ab371dad29306452140984363711fdf'
FPGA115RBFFILE='/usr/share/Nuand/bladeRF/hostedx115.rbf'

FPGA40UPSTREAM='https://www.nuand.com/fpga/v0.16.0/hostedx40.rbf'
FPGA40CHECKSUM='f3d0e55aa4cae485df6d3c20004b7742'
FPGA40SHA256SUM='b21a50c87bccf915b8b1630354dbd07c87c17892c6011ec086bbf9e50dedc15a'
FPGA40RBFFILE='/usr/share/Nuand/bladeRF/hostedx40.rbf'

FPGAA4UPSTREAM='https://www.nuand.com/fpga/v0.16.0/hostedxA4.rbf'
FPGAA4CHECKSUM='76a74b748d699a9fc348babd34af19c'
FPGAA4SHA256SUM='c0d1ed6a38900629ec7983ddd27b2d438ca7a7d2b897d37364ff0c2511210ace'
FPGAA4RBFFILE='/usr/share/Nuand/bladeRF/hostedxA4.rbf'

FPGAA5UPSTREAM='https://www.nuand.com/fpga/v0.16.0/hostedxA5.rbf'
FPGAA5CHECKSUM='be0909dc27b48a08c54db33675130f27'
FPGAA5SHA256SUM='b85b413c0b305a04bd3fdb604ff6c77a1eac427f07d72251920a60b6a2f5b062'
FPGAA5RBFFILE='/usr/share/Nuand/bladeRF/hostedxA5.rbf'

FPGAA9UPSTREAM='https://www.nuand.com/fpga/v0.16.0/hostedxA9.rbf'
FPGAA9CHECKSUM='1ab3bc8bdcf16a5f0ddf3fcdbaa6556b'
FPGASHA256SUM='3004467110e2b2790c8f6128f998c2149905a88eed91a87f11af3db524d60c65'
FPGAA9RBFFILE='/usr/share/Nuand/bladeRF/hostedxA9.rbf'

ADSBA4UPSTREAM='https://www.nuand.com/fpga/adsbxA4.rbf'
ADSBA4CHECKSUM='2fe523c4c3f62100a19eab1446676f39'
ADSBA4SHA256SUM='e3ef41920c148f34990a16a6d9614a4f1632391ee5ed05f16149dd82c5367d14'
ADSBA4RBFFILE='/usr/share/Nuand/bladeRF/adsbxA4.rbf'

ADSBA9UPSTREAM='https://www.nuand.com/fpga/adsbxA9.rbf'
ADSBA9CHECKSUM='4526ef899fef566619625982fdc5cfb4'
ADSBA9SHA256SUM='b1e1be9228f2b6629f49dd357b5c5e65b5c3c581e6769e9abd341639617ca212'
ADSBA9RBFFILE='/usr/share/Nuand/bladeRF/adsbxA9.rbf'

ADSB40UPSTREAM='https://www.nuand.com/fpga/adsbx40.rbf'
ADSB40CHECKSUM='5c6045066ac2e62c8a2c3e770c6c355c'
ADSB40SHA256SUM='2801df76b13b243d3b4a211f39841326fbbdf8e4656c61761b814ef5386a6a08'
ADSB40RBFFILE='/usr/share/Nuand/bladeRF/adsbx40.rbf'

ADSB115UPSTREAM='https://www.nuand.com/fpga/adsbx115.rbf'
ADSB115CHECKSUM='178f1d952be3a05f2f0a4a0e9429746a'
ADSB115SHA256SUM='e5ff37596f6d46ca9124001bde73237a85c435024963c911658b922e6e960142'
ADSB115RBFFILE='/usr/share/Nuand/bladeRF/adsbx115.rbf'

# Default values:
# The imagedir is the path bladerf-host and libbladerf packages
# use to find firmware.
imagedir="/usr/share/Nuand/bladeRF"
# The imagetarball is the source of firmware built with matching
# source code.
fpgaimageurl=""
usbimageurl=""
while [ "$1" != "" ]; do
    case $1 in
        -h | --help )           show_usage
                                exit 0
                                ;;
        -i | --imagedir )       shift
	                        imagedir=$1
                                ;;
        -f | --fpgafile )       shift
                                fpgaimageurl=$1
                                ;;
        -u | --usbfile )        shift
                                usbimageurl=$1
                                ;;
        * )                     fpgaimageurl=$1
                                ;;
    esac
    shift
done

if mkdir -p $imagedir ; then
    if [ -w $imagedir ] ;then
	echo Using imagedir: $imagedir
    else
	echo You need to run this script as a user who can write to $imagedir
	exit 1
    fi
else
    echo You need to run this script as a user who can create $imagedir
    exit 1
fi

tdir=`mktemp -d`
fetchlist=""
echo "Using tempdir:" $tdir
if [ "x$fpgaimageurl" = "x" ] && [ "x$usbimageurl" = "x" ] ; then
    fetchlist="$FX3UPSTREAM $FPGA40UPSTREAM $FPGA115UPSTREAM $FPGAA4UPSTREAM $FPGAA5UPSTREAM $FPGAA9UPSTREAM \
    			    $ADSB40UPSTREAM $ADSB115UPSTREAM $ADSBA4UPSTREAM $ADSBA9UPSTREAM"
else
    if [ "x$fpgaimageurl" != "x" ] ; then
	fetchlist="$fetchlist $fpgaimageurl"
    fi
    if [ "x$usbimageurl" != "x" ] ; then
	fetchlist="$fetchlist $usbimageurl"
    fi
fi

for item in $fetchlist; do
echo "Using: " $item

# if URL, fetch it first
    if echo $item | grep -q :// ; then
	echo Fetching $item ;
	if [ -x /usr/bin/wget ] ; then
	    /usr/bin/wget --user-agent="Debian BladeRF image installer" -O $tdir/`basename $item` $item
	else
	    curl --user-agent "Debian BladeRF image installer" -o $tdir/`basename $item` $item
	fi
    else
	if [ -f $item ] ; then
	    echo Copying $item
	    cp -p $item $tdir/
	else
	    echo "Cannot find" $item;
	    show_usage
	    exit 1
	fi
    fi;
    if [ `basename $item` = "bladeRF_fw_v2.4.0.img" ] ; then
	md5sum --check - <<- EOF
$FX3CHECKSUM  $tdir/`basename $item`
EOF
	sha256sum --check - <<- EOF
$FX3SHA256SUM  $tdir/`basename $item`
EOF
	cp -p $tdir/`basename $item` $imagedir/bladeRF_fw.img ;
    fi
    if [ `basename $item` = "hostedx40.rbf" ] ; then
	md5sum --check - <<- EOF
$FPGA40CHECKSUM  $tdir/`basename $item`
EOF
	sha256sum --check - <<- EOF
$FPGA40SHA256SUM  $tdir/`basename $item`
EOF
    fi
    if [ `basename $item` = "hostedx115.rbf" ] ; then
	md5sum --check - <<- EOF
$FPGA115CHECKSUM  $tdir/`basename $item`
EOF
	sha256sum --check - <<- EOF
$FPGA115SHA256SUM  $tdir/`basename $item`
EOF
    fi
    if [ `basename $item` = "hostedxa4.rbf" ] ; then
	md5sum --check - <<- EOF
$FPGAA4CHECKSUM  $tdir/`basename $item`
EOF
	sha256sum --check - <<- EOF
$FPGAA4SHA256SUM  $tdir/`basename $item`
EOF
    fi
    if [ `basename $item` = "hostedxa5.rbf" ] ; then
	md5sum --check - <<- EOF
$FPGAA5CHECKSUM  $tdir/`basename $item`
EOF
	sha256sum --check - <<- EOF
$FPGAA5SHA256SUM  $tdir/`basename $item`
EOF
    fi
    if [ `basename $item` = "hostedxa9.rbf" ] ; then
	md5sum --check - <<- EOF
$FPGAA9CHECKSUM  $tdir/`basename $item`
EOF
	sha256sum --check - <<- EOF
$FPGAA9SHA256SUM  $tdir/`basename $item`
EOF
    fi
    cp -p $tdir/`basename $item` $imagedir/`basename $item`
done

for file in `ls $imagedir`; do md5sum $imagedir/$file ; done
rm -i -rf $tdir

exit 0
