meta-ampere: mac-update: validate MAC Address
All Ampere developed platforms have BMC MAC Address specified in the FRU
EEPROM at Board Extra.
This commit moves the script to get MAC Address from FRU EEPROM to
common folder so that later platforms can use it. It also adds checking
for valid MAC Address before using.
Tested:
1. Update BMC MAC Address using IPMI OEM Write BMC MAC Address command.
Reboot BMC and ensure the new MAC is used.
2. Write the FRU with invalid value at Board Extra. Reboot BMC and check
for BMC still uses old MAC Address
Signed-off-by: Thang Q. Nguyen <thang@os.amperecomputing.com>
Change-Id: I29a44a9b4965bea1b6d8d7d55536d25bb0dd924c
diff --git a/meta-ampere/meta-common/recipes-ampere/platform/ampere-utils.bb b/meta-ampere/meta-common/recipes-ampere/platform/ampere-utils.bb
index 5d308dc..c0ae7bd 100644
--- a/meta-ampere/meta-common/recipes-ampere/platform/ampere-utils.bb
+++ b/meta-ampere/meta-common/recipes-ampere/platform/ampere-utils.bb
@@ -6,6 +6,7 @@
SRC_URI = " \
file://ampere_add_redfishevent.sh \
+ file://ampere_update_mac.sh \
"
RDEPENDS:${PN} = "bash"
@@ -13,4 +14,5 @@
do_install() {
install -d ${D}/usr/sbin
install -m 0755 ${WORKDIR}/ampere_add_redfishevent.sh ${D}/${sbindir}/
+ install -m 0755 ${WORKDIR}/ampere_update_mac.sh ${D}/${sbindir}/
}
diff --git a/meta-ampere/meta-common/recipes-ampere/platform/ampere-utils/ampere_update_mac.sh b/meta-ampere/meta-common/recipes-ampere/platform/ampere-utils/ampere_update_mac.sh
new file mode 100644
index 0000000..6b86245
--- /dev/null
+++ b/meta-ampere/meta-common/recipes-ampere/platform/ampere-utils/ampere_update_mac.sh
@@ -0,0 +1,92 @@
+#!/bin/bash
+#
+# This script is used to get the BMC MAC Address from FRU EEPROM at Board Extra.
+# and if the eth address is not exist on U-boot Enviroment, this script will update it.
+#
+
+function Usage () {
+ echo "Usage:"
+ echo " ampere_update_mac.sh <ethX> <fru bus> <fru addr>"
+ echo "Example:"
+ echo " ampere_update_mac.sh eth1 3 80"
+}
+
+function read_mac_address () {
+ fruBus=$1
+ fruAddr=$2
+
+ if FRU_OBJECT_PATH=$(busctl tree xyz.openbmc_project.FruDevice | grep "/xyz/openbmc_project/FruDevice/" | tr -s '\n' ' ' | tr -d "|-" | tr -d '`')
+ then
+ IFS=' ' read -r -a FRU_OBJ_PATH_ARR <<< "$FRU_OBJECT_PATH"
+
+ for fruObj in "${FRU_OBJ_PATH_ARR[@]}"
+ do
+ BUS_IDX_RW=$(busctl get-property xyz.openbmc_project.FruDevice "$fruObj" xyz.openbmc_project.FruDevice BUS)
+ BUS_ADDR_RW=$(busctl get-property xyz.openbmc_project.FruDevice "$fruObj" xyz.openbmc_project.FruDevice ADDRESS)
+
+ if [ -z "$BUS_IDX_RW" ] || [ -z "$BUS_IDX_RW" ]; then
+ continue
+ else
+ BUS_IDX_CV=$(echo "$BUS_IDX_RW" | cut -d " " -f 2)
+ BUS_ADDR_CV=$(echo "$BUS_ADDR_RW" | cut -d " " -f 2)
+ if [ "$BUS_IDX_CV" != "$fruBus" ] || [ "$BUS_ADDR_CV" != "$fruAddr" ]; then
+ continue
+ fi
+ fi
+
+ MAC_ADDR_RAW=$(busctl get-property xyz.openbmc_project.FruDevice "$fruObj" xyz.openbmc_project.FruDevice BOARD_INFO_AM1)
+ MAC_ADDR=$(echo "$MAC_ADDR_RAW" | cut -d "\"" -f 2)
+ break
+ done
+ fi
+ echo "$MAC_ADDR"
+}
+
+ETHERNET_INTERFACE=$1
+BMC_FRU_BUS=$2
+BMC_FRU_ADDR=$3
+
+if [ -z "$BMC_FRU_ADDR" ];
+then
+ Usage
+ exit
+fi
+
+# Read FRU Board Custom Field 1 to get the MAC address
+for i in {1..10}; do
+ MAC_ADDR=$(read_mac_address "$BMC_FRU_BUS" "$BMC_FRU_ADDR")
+
+ # Check if BMC MAC address is exported
+ if [ -z "${MAC_ADDR}" ]; then
+ sleep 2
+ continue
+ fi
+
+ if echo "$MAC_ADDR" | grep -q -vE "^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$" ; then
+ echo "ERROR: No valid BMC MAC Address detected from BMC FRU! $MAC_ADDR"
+ exit 0
+ else
+ echo "mac-update: detect BMC MAC $MAC_ADDR at loop $i"
+ break
+ fi
+done
+
+# Check if the Ethernet port has correct MAC Address
+ETH_INCLUDE_MAC=$(ifconfig "${ETHERNET_INTERFACE}" | grep -i "$MAC_ADDR")
+if [ -n "$ETH_INCLUDE_MAC" ]; then
+ echo "BMC MAC Address is already configured"
+ exit 0
+fi
+
+# Request to restart the service
+ifconfig "${ETHERNET_INTERFACE}" down
+fw_setenv bmc_macaddr "${MAC_ADDR}"
+ifconfig "${ETHERNET_INTERFACE}" hw ether "${MAC_ADDR}"
+retval=$?
+if [[ $retval -ne 0 ]]; then
+ echo "ERROR: Can not update MAC ADDR to ${ETHERNET_INTERFACE}"
+ exit 1
+fi
+ifconfig "${ETHERNET_INTERFACE}" up
+
+echo "Successfully update the MAC address ${MAC_ADDR} to ${ETHERNET_INTERFACE}"