Skip to content

Commit d5e522b

Browse files
nazariigandriymoroz-mlnx
authored andcommitted
Fixed FW upgrade sequence. (sonic-net#2111)
* Fixed FW upgrade sequence. * Removed code duplication.
1 parent 5718272 commit d5e522b

File tree

7 files changed

+166
-52
lines changed

7 files changed

+166
-52
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../x86_64-mlnx_msn2700-r0/platform_reboot
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../x86_64-mlnx_msn2700-r0/platform_reboot
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../x86_64-mlnx_msn2700-r0/platform_reboot
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../x86_64-mlnx_msn2700-r0/platform_reboot
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
3+
declare -r EXIT_SUCCESS="0"
4+
declare -r EXIT_ERROR="1"
5+
6+
declare -r FW_UPGRADE_SCRIPT="/usr/bin/mlnx-fw-upgrade.sh"
7+
8+
FORCE_REBOOT="no"
9+
10+
function ParseArguments() {
11+
while [ $# -ge 1 ]; do
12+
case "$1" in
13+
-f|--force)
14+
FORCE_REBOOT="yes"
15+
;;
16+
esac
17+
shift
18+
done
19+
}
20+
21+
ParseArguments "$@"
22+
23+
${FW_UPGRADE_SCRIPT} --upgrade
24+
EXIT_CODE="$?"
25+
if [[ "${EXIT_CODE}" != "${EXIT_SUCCESS}" ]]; then
26+
echo "Failed to burn MLNX FW: errno=${EXIT_CODE}"
27+
28+
if [[ "${FORCE_REBOOT}" != "yes" ]]; then
29+
echo "Reboot is interrupted: use -f|--force to override"
30+
exit "${EXIT_ERROR}"
31+
fi
32+
fi
33+
34+
exec /sbin/reboot $@
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../x86_64-mlnx_msn2700-r0/platform_reboot

platform/mellanox/mlnx-fw-upgrade.j2

+127-52
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,143 @@
11
#!/bin/bash
22

3-
fw_required="{{ MLNX_FW_VERSION }}"
4-
query_retry_count_max="10"
5-
fw_file=/etc/mlnx/fw-SPC.mfa
6-
7-
run_or_fail() {
8-
$1
9-
if [[ $? != 0 ]]; then
10-
echo $1 failed
11-
exit 1
12-
fi
3+
declare -r SCRIPT_NAME="$(basename "$0")"
4+
declare -r SCRIPT_PATH="$(readlink -f "$0")"
5+
declare -r SCRIPT_DIR="$(dirname "$SCRIPT_PATH")"
6+
7+
declare -r EXIT_SUCCESS="0"
8+
declare -r EXIT_ERROR="1"
9+
10+
declare -r QUERY_CMD="mlxfwmanager --query"
11+
declare -r BURN_CMD="mlxfwmanager -u -f -y"
12+
13+
declare -r FW_FILE="/etc/mlnx/fw-SPC.mfa"
14+
declare -r QUERY_FILE="/tmp/mlnxfwmanager-query.txt"
15+
16+
declare -r FW_REQUIRED="{{ MLNX_FW_VERSION }}"
17+
18+
IMAGE_UPGRADE="no"
19+
20+
function PrintHelp() {
21+
echo
22+
echo "Usage: ./${SCRIPT_NAME} [OPTIONS]"
23+
echo
24+
echo "OPTIONS:"
25+
echo " -u, --upgrade Upgrade MLNX ASIC firmware using next boot image (useful after SONiC-To-SONiC update)"
26+
echo " -h, --help Print help"
27+
echo
28+
echo "Examples:"
29+
echo " ./${SCRIPT_NAME}"
30+
echo " ./${SCRIPT_NAME} --upgrade"
31+
echo " ./${SCRIPT_NAME} --help"
32+
echo
1333
}
1434

15-
# wait until devices will be available
16-
query_retry_count="0"
17-
query_cmd="mlxfwmanager --query"
18-
${query_cmd} > /dev/null
19-
20-
while [[ (${query_retry_count} -lt ${query_retry_count_max}) && ($? -ne "0") ]]; do
21-
sleep 1
22-
query_retry_count=$[${query_retry_count}+1]
23-
${query_cmd} > /dev/null
24-
done
25-
26-
function upgrade() {
27-
if [ ! -z "$1" ]; then
28-
fw_file="$1"
29-
if [ ! -f "$fw_file" ]; then
30-
>&2 echo "No such file: $fw_file"
31-
exit 1
32-
fi
35+
function ParseArguments() {
36+
while [ $# -ge 1 ]; do
37+
case "$1" in
38+
-u|--upgrade)
39+
IMAGE_UPGRADE="yes"
40+
;;
41+
-h|--help)
42+
PrintHelp
43+
exit "${EXIT_SUCCESS}"
44+
;;
45+
esac
46+
shift
47+
done
48+
}
49+
50+
function WaitForDevice() {
51+
local -i QUERY_RETRY_COUNT_MAX="10"
52+
local -i QUERY_RETRY_COUNT="0"
53+
54+
${QUERY_CMD} > /dev/null
55+
56+
while [[ ("${QUERY_RETRY_COUNT}" -lt "${QUERY_RETRY_COUNT_MAX}") && ("$?" -ne "0") ]]; do
57+
sleep 1s
58+
((QUERY_RETRY_COUNT++))
59+
${QUERY_CMD} > /dev/null
60+
done
61+
}
62+
63+
function RunCmd() {
64+
$1
65+
if [[ $? != 0 ]]; then
66+
echo "Command failed: cmd=$1, errno=$?"
67+
exit "${EXIT_ERROR}"
68+
fi
69+
}
70+
71+
function UpgradeFW() {
72+
local _FW_FILE="$1"
3373

34-
run_or_fail "${query_cmd} -i ${fw_file}" > /tmp/mlnxfwmanager-query.txt
74+
if [ ! -z "${_FW_FILE}" ]; then
75+
if [ ! -f "${_FW_FILE}" ]; then
76+
echo "No such file: ${_FW_FILE}"
77+
exit "${EXIT_ERROR}"
78+
fi
3579

36-
# get current firmware version and available version from $fw_file
37-
fw_info=$(grep FW /tmp/mlnxfwmanager-query.txt)
38-
fw_current=$(echo $fw_info | cut -f2 -d' ')
39-
fw_available=$(echo $fw_info | cut -f3 -d' ')
80+
RunCmd "${QUERY_CMD} -i ${_FW_FILE}" > "${QUERY_FILE}"
4081

41-
fw_required=$fw_available
82+
local -r _FW_INFO="$(grep FW ${QUERY_FILE})"
83+
local -r _FW_CURRENT="$(echo ${_FW_INFO} | cut -f2 -d' ')"
84+
local -r _FW_AVAILABLE="$(echo ${_FW_INFO} | cut -f3 -d' ')"
4285
else
43-
run_or_fail "${query_cmd}" > /tmp/mlnxfwmanager-query.txt
86+
RunCmd "${QUERY_CMD}" > "${QUERY_FILE}"
87+
88+
local -r _FW_INFO="$(grep FW ${QUERY_FILE})"
89+
local -r _FW_CURRENT="$(echo ${_FW_INFO} | cut -f2 -d' ')"
90+
local -r _FW_AVAILABLE="${FW_REQUIRED}"
91+
92+
_FW_FILE="${FW_FILE}"
93+
fi
4494

45-
# get current firmware version and required version
46-
fw_info=$(grep FW /tmp/mlnxfwmanager-query.txt)
47-
fw_current=$(echo $fw_info | cut -f2 -d' ')
95+
if [[ -z "${_FW_CURRENT}" ]]; then
96+
echo "Could not retreive current FW version"
97+
exit "${EXIT_ERROR}"
4898
fi
49-
50-
if [[ -z ${fw_current} ]]; then
51-
echo "Could not retreive current FW version."
52-
exit 1
99+
100+
if [[ -z "${_FW_AVAILABLE}" ]]; then
101+
echo "Could not retreive available FW version"
102+
exit "${EXIT_ERROR}"
53103
fi
54-
55-
if [[ -z ${fw_required} ]]; then
56-
echo "Could not retreive required FW version."
57-
exit 1
104+
105+
if [[ "${_FW_CURRENT}" == "${_FW_AVAILABLE}" ]]; then
106+
echo "Mellanox firmware is up to date"
107+
else
108+
echo "Mellanox firmware upgrade is required. Installing compatible version..."
109+
RunCmd "${BURN_CMD} -i ${_FW_FILE}"
58110
fi
59-
60-
if [[ ${fw_current} == ${fw_required} ]]; then
61-
echo "Mellanox firmware is up to date."
111+
}
112+
113+
function UpgradeFWFromImage() {
114+
local -r _NEXT_SONIC_IMAGE="$(sonic_installer list | grep "Next: " | cut -f2 -d' ')"
115+
local -r _CURRENT_SONIC_IMAGE="$(sonic_installer list | grep "Current: " | cut -f2 -d' ')"
116+
117+
local -r _FS_PATH="/host/image-${_NEXT_SONIC_IMAGE#SONiC-OS-}/fs.squashfs"
118+
local -r _FS_MOUNTPOINT="/tmp/image-${_NEXT_SONIC_IMAGE#SONiC-OS-}-fs"
119+
120+
if [[ "${_CURRENT_SONIC_IMAGE}" == "${_NEXT_SONIC_IMAGE}" ]]; then
121+
echo "Mellanox firmware is up to date"
62122
else
63-
echo "Mellanox firmware required version is ${fw_required}. Installing compatible version..."
64-
run_or_fail "mlxfwmanager -i ${fw_file} -u -f -y"
123+
mkdir -p "${_FS_MOUNTPOINT}"
124+
mount -t squashfs "${_FS_PATH}" "${_FS_MOUNTPOINT}"
125+
126+
UpgradeFW "${_FS_MOUNTPOINT}/etc/mlnx/fw-SPC.mfa"
127+
128+
umount -rf "${_FS_MOUNTPOINT}"
129+
rm -rf "${_FS_MOUNTPOINT}"
65130
fi
66131
}
67132

68-
upgrade "$1"
133+
ParseArguments "$@"
134+
135+
WaitForDevice
136+
137+
if [ "${IMAGE_UPGRADE}" != "yes" ]; then
138+
UpgradeFW
139+
else
140+
UpgradeFWFromImage
141+
fi
142+
143+
exit "${EXIT_SUCCESS}"

0 commit comments

Comments
 (0)