[PATCH 1/3] net: Add a CONFIG_NET_BOARD_ETHADDR

Detlev Casanova detlev.casanova at collabora.com
Thu Mar 14 15:43:46 CET 2024


On some boards, a MAC address is set based on the CPU ID or other
information. This is usually done in the misc_init_r() function.

This becomes a problem for net devices that are probed after the call to
misc_init_r(), for example, when the ethernet is on a PCI port, which
needs to be enumerated.

In this case, misc_init_r() will set the ethaddr variable, then, when
the ethernet device is probed, if it has a ROM address, u-boot will warn
about a MAC address mismatch and use the misc_init_r() address instead
of the one in ROM.

The operating system later will most likely use the ROM MAC address,
which can be confusing.

To avoid that, this commit introduces a CONFIG_NET_BOARD_ETHADDR that
allows board files to implement a function to set an ethaddr in the
environment, that will only be called when necessary. The logic is now:

- If there is there an ethaddr env var, use it.
- If not, if there is a DT MAC address, use it.
- If not, if there is a ROM MAC address, use it.
- If not, if CONFIG_NET_BOARD_ETHADDR, call board_gen_ethaddr() and use
  it.
- If not, if CONFIG_NET_RANDOM_ETHADDR, generate random MAC
- If not, fail with No valid MAC address found

Signed-off-by: Detlev Casanova <detlev.casanova at collabora.com>
---
 net/Kconfig      |  7 +++++++
 net/eth-uclass.c | 17 +++++++++++++++--
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/net/Kconfig b/net/Kconfig
index 5dff6336293..6dd333ddb9e 100644
--- a/net/Kconfig
+++ b/net/Kconfig
@@ -54,6 +54,13 @@ config NET_RANDOM_ETHADDR
 	  generated. It will be saved to the appropriate environment variable,
 	  too.
 
+config NET_BOARD_ETHADDR
+	bool "Board specific ethaddr if unset"
+	help
+	  Allow a board function to set a specific Ethernet address that can
+	  be, e.g., based on the CPU ID. If set, this will be tried before
+	  setting a random address (if set).
+
 config NETCONSOLE
 	bool "NetConsole support"
 	help
diff --git a/net/eth-uclass.c b/net/eth-uclass.c
index 3d0ec91dfa4..f194df8512a 100644
--- a/net/eth-uclass.c
+++ b/net/eth-uclass.c
@@ -56,6 +56,12 @@ __weak int board_interface_eth_init(struct udevice *dev,
 	return 0;
 }
 
+/* board-specific MAC Address generation. */
+__weak int board_gen_ethaddr(int dev_num, u8 *mac_addr)
+{
+	return 0;
+}
+
 static struct eth_uclass_priv *eth_get_uclass_priv(void)
 {
 	struct uclass *uc;
@@ -563,13 +569,20 @@ static int eth_post_probe(struct udevice *dev)
 	if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
 	    !is_valid_ethaddr(pdata->enetaddr)) {
 		/* Check if the device has a MAC address in ROM */
+		int ret = -1;
 		if (eth_get_ops(dev)->read_rom_hwaddr) {
-			int ret;
-
 			ret = eth_get_ops(dev)->read_rom_hwaddr(dev);
 			if (!ret)
 				source = "ROM";
 		}
+		if (IS_ENABLED(CONFIG_NET_BOARD_ETHADDR) && ret) {
+			board_gen_ethaddr(dev_seq(dev), pdata->enetaddr);
+
+			if (!is_zero_ethaddr(pdata->enetaddr) &&
+			    is_valid_ethaddr(pdata->enetaddr)) {
+				source = "board";
+			}
+		}
 	}
 
 	eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr);
-- 
2.43.2



More information about the U-Boot mailing list