[PATCH 12/13] net: consult device tree for ethernet address in NVMEM as fall-back

Ahmad Fatoum a.fatoum at pengutronix.de
Fri Jun 18 20:45:15 PDT 2021

While barebox fixes up the mac-address into the device tree, it doesn't
care much for extracting a mac address _from_ the device tree, whether
it be from local-mac-address property or from a mac-address nvmem cell.

Fix the latter by calling of_get_mac_addr_nvmem for each Ethernet adapter.
We do this in a very late initcall, because we don't want to enforce a
probe a probe order between nvmem providers and network devices. We
can't do it at randomization time, because we need to fixup Ethernet mac
addresses, even when barebox itself doesn't ifup the netdev.

of_get_mac_addr_nvmem could be replaced by of_get_mac_address to also
parse local-mac-address and brethern, but justifying this change is left
as a future exercise.

Cc: Michael Grzeschik <m.grzeschik at pengutronix.de>
Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
 net/eth.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/net/eth.c b/net/eth.c
index 84f99d3aa822..762c5dfb8aa8 100644
--- a/net/eth.c
+++ b/net/eth.c
@@ -11,6 +11,7 @@
 #include <net.h>
 #include <dma.h>
 #include <of.h>
+#include <of_net.h>
 #include <linux/phy.h>
 #include <errno.h>
 #include <malloc.h>
@@ -504,3 +505,26 @@ void led_trigger_network(enum led_trigger trigger)
 	led_trigger(trigger, TRIGGER_FLASH);
+static int of_populate_ethaddr(void)
+	char str[sizeof("xx:xx:xx:xx:xx:xx")];
+	struct eth_device *edev;
+	int ret;
+	list_for_each_entry(edev, &netdev_list, list) {
+		if (!edev->parent || is_valid_ether_addr(edev->ethaddr))
+			continue;
+		ret = of_get_mac_addr_nvmem(edev->parent->device_node, edev->ethaddr);
+		if (ret)
+			continue;
+		ethaddr_to_string(edev->ethaddr, str);
+		dev_info(&edev->dev, "Got preset MAC address from device tree: %s\n", str);
+		eth_set_ethaddr(edev, edev->ethaddr);
+	}
+	return 0;

More information about the barebox mailing list