sunxi: ensure NanoPi R1 has unique MAC address
authorJan-Niklas Burfeind <git@aiyionpri.me>
Fri, 23 Dec 2022 21:03:14 +0000 (22:03 +0100)
committerDavid Bauer <mail@david-bauer.net>
Sun, 25 Dec 2022 01:27:55 +0000 (02:27 +0100)
Ensure the MAC address for all NanoPi R1 boards is assigned uniquely for
each board.

The vendor 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@david-bauer.net>
Signed-off-by: Jan-Niklas Burfeind <git@aiyionpri.me>
package/base-files/files/lib/functions/system.sh
target/linux/rockchip/armv8/base-files/etc/board.d/02_network
target/linux/sunxi/base-files/etc/board.d/02_network

index 29de2e5dd691e0a1c3c70f09ff36fa50f87a928a..176c10d0655688e9c8a91c3abb36ea413c577054 100644 (file)
@@ -204,6 +204,14 @@ macaddr_add() {
        echo $oui:$nic
 }
 
+macaddr_generate_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}")")"
+}
+
 macaddr_geteui() {
        local mac=$1
        local sep=$2
index b0e64799471c5c4a992ced6183ff12fd5691c208..816045a480318b0bf81966fde37159eeb18dccfd 100644 (file)
@@ -17,15 +17,6 @@ rockchip_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_r4s_get_mac()
 {
        local interface=$1
@@ -38,7 +29,7 @@ nanopi_r4s_get_mac()
                        address=$(macaddr_setbit_la "$address")
                fi
        else
-               address=$(generate_mac_from_mmc_cid mmcblk1)
+               address=$(macaddr_generate_from_mmc_cid mmcblk1)
                if [ "$interface" = "lan" ]; then
                        address=$(macaddr_add "$address" 1)
                fi
@@ -56,7 +47,7 @@ rockchip_setup_macs()
 
        case "$board" in
        friendlyarm,nanopi-r2s)
-               wan_mac=$(generate_mac_from_mmc_cid mmcblk0)
+               wan_mac=$(macaddr_generate_from_mmc_cid mmcblk0)
                lan_mac=$(macaddr_add "$wan_mac" 1)
                ;;
        friendlyarm,nanopi-r4s)
index 46ace1f67c599befd21ef102efd87262a2661ec7..b295dc7daa426c66a519b41eb8faa582b3195abd 100644 (file)
@@ -29,6 +29,27 @@ sunxi_setup_interfaces()
        esac
 }
 
+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=$(macaddr_generate_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 +58,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)
                ;;