redpitaya-puzzlefw/os/buildroot_overlay/etc/init.d/S03set_ethaddr.sh

45 lines
1.1 KiB
Bash
Executable File

#!/bin/sh
#
# Set Ethernet MAC address based on EEPROM settings.
#
start() {
# Read "ethaddr" variable from U-Boot environment in EEPROM.
#
# Environment starts at address 0x1800 and is 1024 bytes long.
# The environment consists of "key=value" pairs, separated by 0x00 bytes.
#
ethaddr_pair="$( dd if=/sys/bus/i2c/devices/0-0050/eeprom bs=1k skip=6 count=1 status=none | tr '\000' '\n' | grep ^ethaddr= )"
if [ -z "${ethaddr_pair}" ]; then
echo "ERROR: ethaddr not found in EEPROM enviroment" >&2
exit 1
fi
ethaddr="${ethaddr_pair#ethaddr=}"
# Check that ethaddr variable is well-formed.
if [ -n "${ethaddr##[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]}" ]; then
echo "ERROR: Invalid ethaddr found in EEPROM enviroment" >&2
exit 1
fi
echo "Setting MAC address ${ethaddr}"
# Configure MAC address
ip link set dev eth0 addr ${ethaddr}
}
case "$1" in
start)
start
;;
stop|restart|reload)
;;
*)
echo "Usage: $0 start"
exit 1
esac