2016-12-05 13:12:19 -06:00
|
|
|
#!/bin/bash
|
|
|
|
|
2018-10-05 12:33:40 -05:00
|
|
|
declare -r SCRIPT_NAME="$(basename "$0")"
|
|
|
|
declare -r SCRIPT_PATH="$(readlink -f "$0")"
|
|
|
|
declare -r SCRIPT_DIR="$(dirname "$SCRIPT_PATH")"
|
|
|
|
|
2019-02-14 01:08:04 -06:00
|
|
|
declare -r YES_PARAM="yes"
|
|
|
|
declare -r NO_PARAM="no"
|
|
|
|
|
|
|
|
declare -r VERBOSE_ERROR="1"
|
|
|
|
declare -r VERBOSE_WARNING="2"
|
|
|
|
declare -r VERBOSE_NOTICE="3"
|
|
|
|
declare -r VERBOSE_INFO="4"
|
|
|
|
|
|
|
|
declare -r VERBOSE_MAX="${VERBOSE_INFO}"
|
|
|
|
declare -r VERBOSE_MIN="${VERBOSE_ERROR}"
|
|
|
|
|
2018-10-05 12:33:40 -05:00
|
|
|
declare -r EXIT_SUCCESS="0"
|
2019-02-14 01:08:04 -06:00
|
|
|
declare -r EXIT_FAILURE="1"
|
2018-10-05 12:33:40 -05:00
|
|
|
|
|
|
|
declare -r QUERY_CMD="mlxfwmanager --query"
|
2020-12-01 10:15:28 -06:00
|
|
|
declare -r LIST_CONTENT_CMD="mlxfwmanager --list-content"
|
2018-10-05 12:33:40 -05:00
|
|
|
declare -r BURN_CMD="mlxfwmanager -u -f -y"
|
|
|
|
|
2019-02-14 01:08:04 -06:00
|
|
|
declare -r QUERY_FILE="/tmp/mlxfwmanager-query.log"
|
2020-12-01 10:15:28 -06:00
|
|
|
declare -r LIST_CONTENT_FILE="/tmp/mlxfwmanager-list-content.log"
|
2018-10-05 12:33:40 -05:00
|
|
|
|
2019-02-14 01:08:04 -06:00
|
|
|
declare -r SPC1_ASIC="spc1"
|
|
|
|
declare -r SPC2_ASIC="spc2"
|
2020-03-09 10:58:49 -05:00
|
|
|
declare -r SPC3_ASIC="spc3"
|
2022-11-29 08:38:41 -06:00
|
|
|
declare -r SPC4_ASIC="spc4"
|
2019-02-14 01:08:04 -06:00
|
|
|
declare -r UNKN_ASIC="unknown"
|
2021-02-04 11:44:16 -06:00
|
|
|
declare -r UNKN_MST="unknown"
|
2018-10-05 12:33:40 -05:00
|
|
|
|
2019-02-14 01:08:04 -06:00
|
|
|
declare -rA FW_FILE_MAP=( \
|
|
|
|
[$SPC1_ASIC]="/etc/mlnx/fw-SPC.mfa" \
|
|
|
|
[$SPC2_ASIC]="/etc/mlnx/fw-SPC2.mfa" \
|
2020-03-09 10:58:49 -05:00
|
|
|
[$SPC3_ASIC]="/etc/mlnx/fw-SPC3.mfa" \
|
2022-11-29 08:38:41 -06:00
|
|
|
[$SPC4_ASIC]="/etc/mlnx/fw-SPC4.mfa" \
|
2019-02-14 01:08:04 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
IMAGE_UPGRADE="${NO_PARAM}"
|
2023-01-25 12:53:39 -06:00
|
|
|
SYSLOG_LOGGER="${NO_PARAM}"
|
2019-02-14 01:08:04 -06:00
|
|
|
VERBOSE_LEVEL="${VERBOSE_MIN}"
|
2018-10-05 12:33:40 -05:00
|
|
|
|
|
|
|
function PrintHelp() {
|
|
|
|
echo
|
|
|
|
echo "Usage: ./${SCRIPT_NAME} [OPTIONS]"
|
|
|
|
echo
|
|
|
|
echo "OPTIONS:"
|
2019-02-14 01:08:04 -06:00
|
|
|
echo " -u, --upgrade Upgrade ASIC firmware using next boot image (useful after SONiC-To-SONiC update)"
|
2023-01-25 12:53:39 -06:00
|
|
|
echo " -s, --syslog Use syslog logger (enabled when -u|--upgrade)"
|
|
|
|
echo " -v, --verbose Verbose mode (enabled when -u|--upgrade)"
|
2018-10-05 12:33:40 -05:00
|
|
|
echo " -h, --help Print help"
|
|
|
|
echo
|
|
|
|
echo "Examples:"
|
2019-02-14 01:08:04 -06:00
|
|
|
echo " ./${SCRIPT_NAME} --verbose"
|
2018-10-05 12:33:40 -05:00
|
|
|
echo " ./${SCRIPT_NAME} --upgrade"
|
|
|
|
echo " ./${SCRIPT_NAME} --help"
|
|
|
|
echo
|
2016-12-05 13:12:19 -06:00
|
|
|
}
|
|
|
|
|
2018-10-05 12:33:40 -05:00
|
|
|
function ParseArguments() {
|
2019-02-14 01:08:04 -06:00
|
|
|
while [ "$#" -ge "1" ]; do
|
2018-10-05 12:33:40 -05:00
|
|
|
case "$1" in
|
|
|
|
-u|--upgrade)
|
2019-02-14 01:08:04 -06:00
|
|
|
IMAGE_UPGRADE="${YES_PARAM}"
|
2023-01-25 12:53:39 -06:00
|
|
|
SYSLOG_LOGGER="${YES_PARAM}"
|
2019-02-14 01:08:04 -06:00
|
|
|
;;
|
|
|
|
-v|--verbose)
|
|
|
|
VERBOSE_LEVEL="${VERBOSE_MAX}"
|
2018-10-05 12:33:40 -05:00
|
|
|
;;
|
2023-01-25 12:53:39 -06:00
|
|
|
-s|--syslog)
|
|
|
|
SYSLOG_LOGGER="${YES_PARAM}"
|
|
|
|
;;
|
2018-10-05 12:33:40 -05:00
|
|
|
-h|--help)
|
|
|
|
PrintHelp
|
|
|
|
exit "${EXIT_SUCCESS}"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2019-02-14 01:08:04 -06:00
|
|
|
function LogError() {
|
|
|
|
if [[ "${VERBOSE_LEVEL}" -ge "${VERBOSE_ERROR}" ]]; then
|
|
|
|
echo "ERROR: $*"
|
2023-01-25 12:53:39 -06:00
|
|
|
logger -p "ERROR" -t "${SCRIPT_NAME}" "$*"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "${SYSLOG_LOGGER}" = "${YES_PARAM}" ]]; then
|
|
|
|
logger -p "ERROR" -t "${SCRIPT_NAME}" "$*"
|
2019-02-14 01:08:04 -06:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function LogWarning() {
|
|
|
|
if [[ "${VERBOSE_LEVEL}" -ge "${VERBOSE_WARNING}" ]]; then
|
|
|
|
echo "WARNING: $*"
|
|
|
|
fi
|
2023-01-25 12:53:39 -06:00
|
|
|
|
|
|
|
if [[ "${SYSLOG_LOGGER}" = "${YES_PARAM}" ]]; then
|
|
|
|
logger -p "WARNING" -t "${SCRIPT_NAME}" "$*"
|
|
|
|
fi
|
2019-02-14 01:08:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function LogNotice() {
|
|
|
|
if [[ "${VERBOSE_LEVEL}" -ge "${VERBOSE_NOTICE}" ]]; then
|
|
|
|
echo "NOTICE: $*"
|
|
|
|
fi
|
2023-01-25 12:53:39 -06:00
|
|
|
|
|
|
|
if [[ "${SYSLOG_LOGGER}" = "${YES_PARAM}" ]]; then
|
|
|
|
logger -p "NOTICE" -t "${SCRIPT_NAME}" "$*"
|
|
|
|
fi
|
2019-02-14 01:08:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function LogInfo() {
|
|
|
|
if [[ "${VERBOSE_LEVEL}" -ge "${VERBOSE_INFO}" ]]; then
|
|
|
|
echo "INFO: $*"
|
|
|
|
fi
|
2023-01-25 12:53:39 -06:00
|
|
|
|
|
|
|
if [[ "${SYSLOG_LOGGER}" = "${YES_PARAM}" ]]; then
|
|
|
|
logger -p "INFO" -t "${SCRIPT_NAME}" "$*"
|
|
|
|
fi
|
2019-02-14 01:08:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function ExitFailure() {
|
|
|
|
if [[ "${VERBOSE_LEVEL}" -ge "${VERBOSE_ERROR}" ]]; then
|
|
|
|
echo
|
|
|
|
LogError "$@"
|
|
|
|
echo
|
|
|
|
fi
|
|
|
|
|
|
|
|
exit "${EXIT_FAILURE}"
|
|
|
|
}
|
|
|
|
|
|
|
|
function ExitSuccess() {
|
|
|
|
if [[ "${VERBOSE_LEVEL}" -ge "${VERBOSE_INFO}" ]]; then
|
|
|
|
echo
|
|
|
|
LogInfo "$@"
|
|
|
|
echo
|
|
|
|
fi
|
|
|
|
|
|
|
|
exit "${EXIT_SUCCESS}"
|
|
|
|
}
|
|
|
|
|
2018-10-05 12:33:40 -05:00
|
|
|
function WaitForDevice() {
|
|
|
|
local -i QUERY_RETRY_COUNT_MAX="10"
|
|
|
|
local -i QUERY_RETRY_COUNT="0"
|
|
|
|
|
|
|
|
${QUERY_CMD} > /dev/null
|
|
|
|
|
2019-02-14 01:08:04 -06:00
|
|
|
while [[ ("${QUERY_RETRY_COUNT}" -lt "${QUERY_RETRY_COUNT_MAX}") && ("$?" -ne "${EXIT_SUCCESS}") ]]; do
|
2018-10-05 12:33:40 -05:00
|
|
|
sleep 1s
|
|
|
|
((QUERY_RETRY_COUNT++))
|
|
|
|
${QUERY_CMD} > /dev/null
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2019-02-14 01:08:04 -06:00
|
|
|
function GetAsicType() {
|
|
|
|
local -r VENDOR_ID="15b3"
|
|
|
|
|
|
|
|
local -r SPC1_PRODUCT_ID="cb84"
|
|
|
|
local -r SPC2_PRODUCT_ID="cf6c"
|
2020-03-09 10:58:49 -05:00
|
|
|
local -r SPC3_PRODUCT_ID="cf70"
|
2022-11-29 08:38:41 -06:00
|
|
|
local -r SPC4_PRODUCT_ID="cf80"
|
2019-02-14 01:08:04 -06:00
|
|
|
|
|
|
|
if lspci -n | grep "${VENDOR_ID}:${SPC1_PRODUCT_ID}" &>/dev/null; then
|
|
|
|
echo "${SPC1_ASIC}"
|
|
|
|
exit "${EXIT_SUCCESS}"
|
|
|
|
elif lspci -n | grep "${VENDOR_ID}:${SPC2_PRODUCT_ID}" &>/dev/null; then
|
|
|
|
echo "${SPC2_ASIC}"
|
|
|
|
exit "${EXIT_SUCCESS}"
|
2020-03-09 10:58:49 -05:00
|
|
|
elif lspci -n | grep "${VENDOR_ID}:${SPC3_PRODUCT_ID}" &>/dev/null; then
|
|
|
|
echo "${SPC3_ASIC}"
|
|
|
|
exit "${EXIT_SUCCESS}"
|
2022-11-29 08:38:41 -06:00
|
|
|
elif lspci -n | grep "${VENDOR_ID}:${SPC4_PRODUCT_ID}" &>/dev/null; then
|
|
|
|
echo "${SPC4_ASIC}"
|
|
|
|
exit "${EXIT_SUCCESS}"
|
2019-02-14 01:08:04 -06:00
|
|
|
fi
|
|
|
|
|
|
|
|
echo "${UNKN_ASIC}"
|
|
|
|
exit "${EXIT_FAILURE}"
|
|
|
|
}
|
|
|
|
|
2021-02-04 11:44:16 -06:00
|
|
|
function GetMstDevice() {
|
|
|
|
local _MST_DEVICE="$(ls /dev/mst/*_pci_cr0 2>&1)"
|
|
|
|
|
|
|
|
if [[ ! -c "${_MST_DEVICE}" ]]; then
|
|
|
|
echo "${UNKN_MST}"
|
|
|
|
else
|
|
|
|
echo "${_MST_DEVICE}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
exit "${EXIT_SUCCESS}"
|
|
|
|
}
|
|
|
|
|
2018-10-05 12:33:40 -05:00
|
|
|
function RunCmd() {
|
2019-02-14 01:08:04 -06:00
|
|
|
local ERROR_CODE="${EXIT_SUCCESS}"
|
|
|
|
|
|
|
|
if [[ "${VERBOSE_LEVEL}" -eq "${VERBOSE_MAX}" ]]; then
|
|
|
|
eval "$@"
|
|
|
|
else
|
|
|
|
eval "$@" &>/dev/null
|
|
|
|
fi
|
|
|
|
|
|
|
|
ERROR_CODE="$?"
|
|
|
|
if [[ "${ERROR_CODE}" != "${EXIT_SUCCESS}" ]]; then
|
|
|
|
ExitFailure "command failed: $@"
|
2018-10-05 12:33:40 -05:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-01-25 12:53:39 -06:00
|
|
|
function RunFwUpdateCmd() {
|
|
|
|
local ERROR_CODE="${EXIT_SUCCESS}"
|
|
|
|
local COMMAND="${BURN_CMD} $@"
|
|
|
|
|
|
|
|
if [[ "${VERBOSE_LEVEL}" -eq "${VERBOSE_MAX}" ]]; then
|
|
|
|
output=$(eval "${COMMAND}")
|
|
|
|
else
|
|
|
|
output=$(eval "${COMMAND}") >/dev/null 2>&1
|
|
|
|
fi
|
|
|
|
|
|
|
|
ERROR_CODE="$?"
|
|
|
|
if [[ "${ERROR_CODE}" != "${EXIT_SUCCESS}" ]]; then
|
|
|
|
failure_msg="${output#*Fail : }"
|
|
|
|
ExitFailure "FW Update command: ${COMMAND} failed with error: ${failure_msg}"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2018-10-05 12:33:40 -05:00
|
|
|
function UpgradeFW() {
|
2019-02-14 01:08:04 -06:00
|
|
|
local -r _FS_MOUNTPOINT="$1"
|
|
|
|
|
|
|
|
local -r _ASIC_TYPE="$(GetAsicType)"
|
|
|
|
if [[ "${_ASIC_TYPE}" = "${UNKN_ASIC}" ]]; then
|
|
|
|
ExitFailure "failed to detect ASIC type"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! -z "${_FS_MOUNTPOINT}" ]; then
|
|
|
|
local -r _FW_FILE="${_FS_MOUNTPOINT}/${FW_FILE_MAP[$_ASIC_TYPE]}"
|
2018-08-29 10:51:02 -05:00
|
|
|
else
|
2019-02-14 01:08:04 -06:00
|
|
|
local -r _FW_FILE="${FW_FILE_MAP[$_ASIC_TYPE]}"
|
2020-12-01 10:15:28 -06:00
|
|
|
fi
|
2019-02-14 01:08:04 -06:00
|
|
|
|
2020-12-01 10:15:28 -06:00
|
|
|
if [ ! -f "${_FW_FILE}" ]; then
|
|
|
|
ExitFailure "no such file: ${_FW_FILE}"
|
2018-10-05 12:33:40 -05:00
|
|
|
fi
|
2018-08-29 10:51:02 -05:00
|
|
|
|
2020-12-01 10:15:28 -06:00
|
|
|
RunCmd "${QUERY_CMD} -o ${QUERY_FILE}"
|
|
|
|
local -r _FW_CURRENT_INFO="$(grep FW ${QUERY_FILE})"
|
|
|
|
local -r _FW_CURRENT="$(echo ${_FW_CURRENT_INFO} | cut -f2 -d' ')"
|
|
|
|
local -r _PSID_INFO="$(grep PSID ${QUERY_FILE})"
|
|
|
|
local -r _PSID="$(echo ${_PSID_INFO} | cut -f2 -d' ')"
|
|
|
|
|
|
|
|
RunCmd "${LIST_CONTENT_CMD} -i ${_FW_FILE} -o ${LIST_CONTENT_FILE}"
|
|
|
|
local -r _FW_AVAILABLE_INFO="$(grep ${_PSID} ${LIST_CONTENT_FILE})"
|
|
|
|
local -r _FW_AVAILABLE="$(echo ${_FW_AVAILABLE_INFO} | cut -f4 -d' ')"
|
|
|
|
|
2018-10-05 12:33:40 -05:00
|
|
|
if [[ -z "${_FW_CURRENT}" ]]; then
|
2019-02-14 01:08:04 -06:00
|
|
|
ExitFailure "could not retreive current FW version"
|
2018-08-29 10:51:02 -05:00
|
|
|
fi
|
2018-10-05 12:33:40 -05:00
|
|
|
|
|
|
|
if [[ -z "${_FW_AVAILABLE}" ]]; then
|
2019-02-14 01:08:04 -06:00
|
|
|
ExitFailure "could not retreive available FW version"
|
2018-08-29 10:51:02 -05:00
|
|
|
fi
|
2018-10-05 12:33:40 -05:00
|
|
|
|
|
|
|
if [[ "${_FW_CURRENT}" == "${_FW_AVAILABLE}" ]]; then
|
2019-02-14 01:08:04 -06:00
|
|
|
ExitSuccess "firmware is up to date"
|
2018-10-05 12:33:40 -05:00
|
|
|
else
|
2019-02-14 01:08:04 -06:00
|
|
|
LogNotice "firmware upgrade is required. Installing compatible version..."
|
2021-02-04 11:44:16 -06:00
|
|
|
local -r _MST_DEVICE="$(GetMstDevice)"
|
|
|
|
if [[ "${_MST_DEVICE}" = "${UNKN_MST}" ]]; then
|
|
|
|
LogWarning "could not find fastest mst device, using default device"
|
2023-01-25 12:53:39 -06:00
|
|
|
RunFwUpdateCmd "-i ${_FW_FILE}"
|
2021-02-04 11:44:16 -06:00
|
|
|
else
|
2023-01-25 12:53:39 -06:00
|
|
|
RunFwUpdateCmd "-d ${_MST_DEVICE} -i ${_FW_FILE}"
|
2021-02-04 11:44:16 -06:00
|
|
|
fi
|
2018-08-29 10:51:02 -05:00
|
|
|
fi
|
2018-10-05 12:33:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function UpgradeFWFromImage() {
|
2020-08-07 10:49:39 -05:00
|
|
|
local -r _NEXT_SONIC_IMAGE="$(sonic-installer list | grep "Next: " | cut -f2 -d' ')"
|
|
|
|
local -r _CURRENT_SONIC_IMAGE="$(sonic-installer list | grep "Current: " | cut -f2 -d' ')"
|
2018-10-05 12:33:40 -05:00
|
|
|
|
|
|
|
local -r _FS_PATH="/host/image-${_NEXT_SONIC_IMAGE#SONiC-OS-}/fs.squashfs"
|
|
|
|
local -r _FS_MOUNTPOINT="/tmp/image-${_NEXT_SONIC_IMAGE#SONiC-OS-}-fs"
|
|
|
|
|
|
|
|
if [[ "${_CURRENT_SONIC_IMAGE}" == "${_NEXT_SONIC_IMAGE}" ]]; then
|
2019-02-14 01:08:04 -06:00
|
|
|
ExitSuccess "firmware is up to date"
|
2018-08-29 10:51:02 -05:00
|
|
|
else
|
2018-10-05 12:33:40 -05:00
|
|
|
mkdir -p "${_FS_MOUNTPOINT}"
|
|
|
|
mount -t squashfs "${_FS_PATH}" "${_FS_MOUNTPOINT}"
|
|
|
|
|
2019-02-14 01:08:04 -06:00
|
|
|
UpgradeFW "${_FS_MOUNTPOINT}"
|
2018-10-05 12:33:40 -05:00
|
|
|
|
|
|
|
umount -rf "${_FS_MOUNTPOINT}"
|
|
|
|
rm -rf "${_FS_MOUNTPOINT}"
|
2018-08-29 10:51:02 -05:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2019-04-10 23:55:14 -05:00
|
|
|
function ExitIfQEMU() {
|
2021-02-25 04:41:29 -06:00
|
|
|
if [ -n "$(lspci -vvv | grep SimX)" ]; then
|
2019-04-10 23:55:14 -05:00
|
|
|
ExitSuccess "No FW upgrade for SimX platform"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2018-10-05 12:33:40 -05:00
|
|
|
ParseArguments "$@"
|
|
|
|
|
2019-04-10 23:55:14 -05:00
|
|
|
ExitIfQEMU
|
|
|
|
|
2018-10-05 12:33:40 -05:00
|
|
|
WaitForDevice
|
|
|
|
|
2019-02-14 01:08:04 -06:00
|
|
|
if [ "${IMAGE_UPGRADE}" != "${YES_PARAM}" ]; then
|
2018-10-05 12:33:40 -05:00
|
|
|
UpgradeFW
|
|
|
|
else
|
|
|
|
UpgradeFWFromImage
|
|
|
|
fi
|
|
|
|
|
2019-02-14 01:08:04 -06:00
|
|
|
ExitSuccess "firmware upgrade is completed"
|