#!/bin/sh

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

# 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

# Extract firmware base directories
fwbasedirs=$(grep "firmware_location" ${conffile} | sed 's/.*:.*\[\"\(.*\)\"\],\?/\1/; s/[",]/ /g')
if [ -z "${fwbasedirs}" ]; then
        echo "Property 'firmware_location' not found in ${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})

    # Iterate through each firmware base directories
    for fwbasedir in ${fwbasedirs}; do
        if [ -d "${fwbasedir}" ]; then
            # Search for default firmware within 2 levels of subdirectory
            # flat design firmware will be at 1st level dir
            # PL design firmware will be at 2nd level dir
            if find "${fwbasedir}" -maxdepth 2 -type d -name "${fwname}" | grep -q .; then
                echo "User defined default firmware detected: ${fwname}"
                #load firmware using client command
                dfx-mgr-client -load "${fwname}"
                exit 0
            fi
        fi
    done

    echo "User defined default firmware not found: ${fwname}"
    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}"

    # Iterate through each firmware base directories
    for fwbasedir in ${fwbasedirs}; do
        fwdir="${fwbasedir}/${fwname}"
        if [ -d "${fwdir}" ]; then
            echo "Default firmware detected: ${fwname}"
            #load firmware using client command
            dfx-mgr-client -load "${fwname}"
            exit 0
        fi
    done

    echo "Board-ID default firmware not loaded: ${fwname}"
    exit 0
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"

    # Iterate through each firmware base directories
    for fwbasedir in ${fwbasedirs}; do
        fwdir="${fwbasedir}/${fwname}"
        if [ -d "${fwdir}" ]; then
            echo "Default firmware detected: ${fwname}"
            #load firmware using client command
            dfx-mgr-client -load "${fwname}"
            exit 0
        fi
    done

    echo "Board-ID default firmware not loaded: ${fwname}"
    exit 0
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}"

    # Iterate through each firmware base directories
    for fwbasedir in ${fwbasedirs}; do
        fwdir="${fwbasedir}/${fwname}"
        if [ -d "${fwdir}" ]; then
            echo "Default firmware detected: ${fwname}"
            #load firmware using client command
            dfx-mgr-client -load "${fwname}"
            exit 0
        fi
    done

    echo "Board-ID default firmware not loaded: ${fwname}"
    exit 0
fi

echo "No Board-ID read"
exit 0
