#!/bin/sh

# (C) Copyright 2022 Xilinx, Inc.
# Copyright (C) 2022 - 2025 Advanced Micro Devices, Inc. All Rights Reserved.
# SPDX-License-Identifier: MIT

# Load firmware and check the return status
function load_firmware() {
	fwname=$1
	dfx-mgr-client -load "${fwname}"

	rv=$?
	if [ $rv -eq 0 ]; then
		echo "Loaded default firmware: ${fwname}"
	elif [ $rv -eq 254 ]; then
		echo "Default firmware not found: ${fwname}"
	else
		echo "Failed to load default firmware: ${fwname}"
	fi

	return $rv
}

# read values from dfx-mgr conf file
conffile="/etc/dfx-mgrd/daemon.conf"
if [ ! -f "${conffile}" ]; then
        echo "dfx-mgrd configuration file not found: ${conffile}"
        exit 1
fi

fwfile=$(grep "default_accel" ${conffile} | sed 's/.*:.*\"\(.*\)\",\?/\1/')
if [ -z "${fwfile}" ]; then
        echo "Property 'default_accel' not found in ${conffile}"
        exit 1
fi

# check if default firmware is already set and present
if [ -f "${fwfile}" ]; then
    fwname=$(cat ${fwfile})

    load_firmware ${fwname}
    ret=$?
    # For user defined default firmware, return success(0) only if the firmware found and loaded successfully
    if [ $ret -eq 0 ]; then
	    exit 0
    fi
    exit 1
fi

# search for firmware based on EEPROM board id
echo "Trying to detect default firmware based on EEPROM..."

# check if board has an eeprom labeled as eeprom_cc
eeprom=$(ls /sys/bus/i2c/devices/*/eeprom_cc*/nvmem 2> /dev/null)
if [ -n "${eeprom}" ]; then
    boardid=$(ipmi-fru --fru-file=${eeprom} --interpret-oem-data | awk -F": " '/FRU Board Product/ { print tolower ($2) }')

    if [ -z "${boardid}" ]; then
        echo "No Board-ID read"
        exit 0
    fi

    revision=$(ipmi-fru --fru-file=${eeprom} --interpret-oem-data | awk -F": " '/FRU Board Custom/ { print tolower ($2); exit }')
    fwname="${boardid}-${revision}"

    load_firmware ${fwname}
    ret=$?
    # For Board-ID default firmware, return success(0) if it is loaded successfully or if the firmware not found
    if [ $ret -eq 0 ] || [ $ret -eq 254 ] ; then
	    exit 0
    fi
    exit 1
fi

# check if board is a SOM product
eeprom=$(ls /sys/bus/i2c/devices/*50/eeprom 2> /dev/null)
if [ -n "${eeprom}" ]; then
    boardid=$(ipmi-fru --fru-file=${eeprom} --interpret-oem-data | awk -F"-" '/FRU Board Product/ { print tolower($2) }')

    if [ -z "${boardid}" ]; then
        echo "No Board-ID read"
        exit 0
    fi

    fwname="${boardid}-starter-kits"

    load_firmware ${fwname}
    ret=$?
    # For Board-ID default firmware, return success(0) if it is loaded successfully or if the firmware not found
    if [ $ret -eq 0 ] || [ $ret -eq 254 ] ; then
	    exit 0
    fi
    exit 1
fi

# check if board is a System Controller product
eeprom=$(ls /sys/bus/i2c/devices/*54/eeprom 2> /dev/null)
if [ -n "${eeprom}" ]; then
    boardid=$(ipmi-fru --fru-file=${eeprom} --interpret-oem-data | awk -F": " '/FRU Board Product/ { print tolower ($2) }')

    if [ -z "${boardid}" ]; then
        echo "No Board-ID read"
        exit 0
    fi

    revision=$(ipmi-fru --fru-file=${eeprom} --interpret-oem-data | awk -F": " '/FRU Board Custom/ { print tolower ($2); exit }')
    fwname="${boardid}-${revision}"

    load_firmware ${fwname}
    ret=$?
    # For Board-ID default firmware, return success(0) if it is loaded successfully or if the firmware not found
    if [ $ret -eq 0 ] || [ $ret -eq 254 ] ; then
	    exit 0
    fi
    exit 1
fi

echo "No Board-ID read"
exit 0
