[PATCH] sunxi: ensure NanoPi R1 has unique MAC address

Jan-Niklas Burfeind git at aiyionpri.me
Thu Dec 22 12:12:01 PST 2022


Ensure the MAC address for all NanoPi R1 boards is assigned unique for
each board.

The venor ships the device in two variants; one with and one without
eMMC; but both without static mac-addresses.
In order to assign both board types unique MAC addresses, fall back on
the same method used for the NanoPi R2S and R4S in case the EEPROM
chip is not present by generating the board MAC from the SD card CID.

[0] https://wiki.friendlyelec.com/wiki/index.php/NanoPi_R1#Hardware_Spec

Similar too and based on:

commit b5675f500daf ("rockchip: ensure NanoPi R4S has unique MAC address")

Co-authored-by: David Bauer <mail at david-bauer.net>
Signed-off-by: Jan-Niklas Burfeind <git at aiyionpri.me>
---
 .../sunxi/base-files/etc/board.d/02_network   | 34 +++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/target/linux/sunxi/base-files/etc/board.d/02_network b/target/linux/sunxi/base-files/etc/board.d/02_network
index 46ace1f67c..311699dfb5 100644
--- a/target/linux/sunxi/base-files/etc/board.d/02_network
+++ b/target/linux/sunxi/base-files/etc/board.d/02_network
@@ -29,6 +29,36 @@ sunxi_setup_interfaces()
 	esac
 }
 
+generate_mac_from_mmc_cid()
+{
+	local mmc_dev=$1
+
+	local sd_hash=$(sha256sum /sys/class/block/$mmc_dev/device/cid)
+	local mac_base=$(macaddr_canonicalize "$(echo "${sd_hash}" | dd bs=1 count=12 2>/dev/null)")
+	echo "$(macaddr_unsetbit_mc "$(macaddr_setbit_la "${mac_base}")")"
+}
+
+nanopi_r1_get_mac()
+{
+	local interface=$1
+	local eeprom_path="/sys/bus/i2c/devices/2-0051/eeprom"
+	local address
+
+	if [ -f "$eeprom_path" ]; then
+		address=$(get_mac_binary "$eeprom_path" 0xfa)
+		if [ "$interface" = "lan" ]; then
+			address=$(macaddr_setbit_la "$address")
+		fi
+	else
+		address=$(generate_mac_from_mmc_cid mmcblk1)
+		if [ "$interface" = "lan" ]; then
+			address=$(macaddr_add "$address" 1)
+		fi
+	fi
+
+	echo "$address"
+}
+
 sunxi_setup_macs()
 {
 	local board="$1"
@@ -37,6 +67,10 @@ sunxi_setup_macs()
 	local label_mac=""
 
 	case "$board" in
+	friendlyarm,nanopi-r1)
+		wan_mac=$(nanopi_r1_get_mac wan)
+		lan_mac=$(nanopi_r1_get_mac lan)
+		;;
 	friendlyarm,nanopi-r1s-h5)
 		lan_mac=$(get_mac_binary "/sys/bus/i2c/devices/0-0051/eeprom" 0xfa)
 		;;
-- 
2.39.0




More information about the openwrt-devel mailing list