#! /usr/bin/bash

# BSD 2-Clause License
#
# Copyright (c) 2025 Frederick Boniface
# 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 HOLDER 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.


mkdir ml
cd ml

# Install Dependencies
dnf install -y gcc make dkms rpm-build
dnf install -y "kernel-devel-$(uname -r)"

# Download, Extract & Install Software
wget https://www.mellanox.com/downloads/MFT/mft-4.22.1-307-x86_64-rpm.tgz
tar -xf mft-4.22.1-307-x86_64-rpm.tgz
cd mft-4.22.1-307-x86_64-rpm
./install.sh

# Download Firmware
wget http://www.mellanox.com/downloads/firmware/fw-ConnectX3-rel-2_42_5000-MCX354A-FCB_A2-A5-FlexBoot-3.4.752.bin.zip
unzip fw-ConnectX3-rel-2_42_5000-MCX354A-FCB_A2-A5-FlexBoot-3.4.752.bin.zip
mv fw-ConnectX3-rel-2_42_5000-MCX354A-FCB_A2-A5-FlexBoot-3.4.752.bin fw.bin

# End
echo "To install firmare, run 'mst start', 'mst status' to get the (cr0) device address"
echo "Then run flint -d [device_address] -i fw.bin --allow_psid_change burn"
echo "Server must be rebooted after firmware upgrade"
echo "To set ports to ethernet: 'mlxconfig -d [device_address] set LINK_TYPE_P1=2 LINK_TYPE_P2=2'"
echo "To turn off Bootrom: use the set command as above with 'BOOT_OPTION_ROM_EN_P1=false', Then do the same for P2., then LEGACY_BOOT_PROTOCOL_P1=0, and the same for P2."