public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Thierry Reding <thierry.reding@gmail.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v2 14/15] ARM: tegra: Implement cboot_get_ethaddr()
Date: Tue, 19 Mar 2019 00:24:21 +0100	[thread overview]
Message-ID: <20190318232422.24404-15-thierry.reding@gmail.com> (raw)
In-Reply-To: <20190318232422.24404-1-thierry.reding@gmail.com>

From: Thierry Reding <treding@nvidia.com>

This function will attempt to look up an ethernet address in the DTB
that was passed in from cboot. It does so by first trying to locate the
primary ethernet device for the board (identified by the "ethernet"
alias) and if found, reads the "local-mac-address" property. If the
"ethernet" alias does not exist, or if it points to a device tree node
that doesn't exist, or if the device tree node that it points to does
not have a "local-mac-address" property or if the value is invalid, it
will fall back to the legacy mechanism of looking for the MAC address
stored in the "nvidia,ethernet-mac" property of the "/chosen" node.

Signed-off-by: Thierry Reding <treding@nvidia.com>
---
Changes in v2:
- make dummy static inline to avoid duplicate definitions
---
 arch/arm/include/asm/arch-tegra/cboot.h |  6 ++
 arch/arm/mach-tegra/cboot.c             | 78 ++++++++++++++++++++-----
 2 files changed, 69 insertions(+), 15 deletions(-)

diff --git a/arch/arm/include/asm/arch-tegra/cboot.h b/arch/arm/include/asm/arch-tegra/cboot.h
index b3441ec178b3..021c24617575 100644
--- a/arch/arm/include/asm/arch-tegra/cboot.h
+++ b/arch/arm/include/asm/arch-tegra/cboot.h
@@ -14,6 +14,7 @@ void cboot_save_boot_params(unsigned long x0, unsigned long x1,
 int cboot_dram_init(void);
 int cboot_dram_init_banksize(void);
 ulong cboot_get_usable_ram_top(ulong total_size);
+int cboot_get_ethaddr(const void *fdt, uint8_t mac[ETH_ALEN]);
 #else
 static inline void cboot_save_boot_params(unsigned long x0, unsigned long x1,
 					  unsigned long x2, unsigned long x3)
@@ -34,6 +35,11 @@ static inline ulong cboot_get_usable_ram_top(ulong total_size)
 {
 	return 0;
 }
+
+static inline int cboot_get_ethaddr(const void *fdt, uint8_t mac[ETH_ALEN])
+{
+	return -ENOSYS;
+}
 #endif
 
 #endif
diff --git a/arch/arm/mach-tegra/cboot.c b/arch/arm/mach-tegra/cboot.c
index 8708c4ec9727..c7a38d258cce 100644
--- a/arch/arm/mach-tegra/cboot.c
+++ b/arch/arm/mach-tegra/cboot.c
@@ -4,6 +4,7 @@
  */
 
 #include <common.h>
+#include <environment.h>
 #include <fdt_support.h>
 #include <fdtdec.h>
 #include <stdlib.h>
@@ -465,46 +466,93 @@ static int set_fdt_addr(void)
  * Attempt to use /chosen/nvidia,ethernet-mac in the cboot DTB to U-Boot's
  * ethaddr environment variable if possible.
  */
-static int set_ethaddr_from_cboot(void)
+static int cboot_get_ethaddr_legacy(const void *fdt, uint8_t mac[ETH_ALEN])
 {
-	const void *cboot_blob = (void *)cboot_boot_x0;
-	int ret, node, len;
-	const u32 *prop;
-
-	/* Already a valid address in the environment? If so, keep it */
-	if (env_get("ethaddr"))
-		return 0;
+	const char *prop;
+	int node, len;
 
-	node = fdt_path_offset(cboot_blob, "/chosen");
+	node = fdt_path_offset(fdt, "/chosen");
 	if (node < 0) {
 		printf("Can't find /chosen node in cboot DTB\n");
 		return node;
 	}
-	prop = fdt_getprop(cboot_blob, node, "nvidia,ethernet-mac", &len);
+
+	prop = fdt_getprop(fdt, node, "nvidia,ethernet-mac", &len);
 	if (!prop) {
 		printf("Can't find nvidia,ether-mac property in cboot DTB\n");
 		return -ENOENT;
 	}
 
-	ret = env_set("ethaddr", (void *)prop);
-	if (ret) {
-		printf("Failed to set ethaddr from cboot DTB: %d\n", ret);
-		return ret;
+	eth_parse_enetaddr(prop, mac);
+
+	if (!is_valid_ethaddr(mac)) {
+		printf("Invalid MAC address: %s\n", prop);
+		return -EINVAL;
 	}
 
+	debug("Legacy MAC address: %pM\n", mac);
+
 	return 0;
 }
 
+int cboot_get_ethaddr(const void *fdt, uint8_t mac[ETH_ALEN])
+{
+	int node, len, err = 0;
+	const uchar *prop;
+	const char *path;
+
+	path = fdt_get_alias(fdt, "ethernet");
+	if (!path) {
+		err = -ENOENT;
+		goto out;
+	}
+
+	debug("ethernet alias found: %s\n", path);
+
+	node = fdt_path_offset(fdt, path);
+	if (node < 0) {
+		err = -ENOENT;
+		goto out;
+	}
+
+	prop = fdt_getprop(fdt, node, "local-mac-address", &len);
+	if (!prop) {
+		err = -ENOENT;
+		goto out;
+	}
+
+	if (len != ETH_ALEN) {
+		err = -EINVAL;
+		goto out;
+	}
+
+	debug("MAC address: %pM\n", prop);
+	memcpy(mac, prop, ETH_ALEN);
+
+out:
+	if (err < 0)
+		err = cboot_get_ethaddr_legacy(fdt, mac);
+
+	return err;
+}
+
 int cboot_late_init(void)
 {
+	const void *fdt = (const void *)cboot_boot_x0;
+	uint8_t mac[ETH_ALEN];
+	int err;
+
 	set_calculated_env_vars();
 	/*
 	 * Ignore errors here; the value may not be used depending on
 	 * extlinux.conf or boot script content.
 	 */
 	set_fdt_addr();
+
 	/* Ignore errors here; not all cases care about Ethernet addresses */
-	set_ethaddr_from_cboot();
+	err = cboot_get_ethaddr(fdt, mac);
+	if (!err)
+		eth_env_set_enetaddr("ethaddr", mac);
 
 	return 0;
 }
-- 
2.21.0

  parent reply	other threads:[~2019-03-18 23:24 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-18 23:24 [U-Boot] [PATCH v2 00/15] ARM: tegra: Add NVIDIA Jetson Nano Developer Kit support Thierry Reding
2019-03-18 23:24 ` [U-Boot] [PATCH v2 01/15] ARM: tegra: Use common header for PMU declarations Thierry Reding
2019-03-18 23:24 ` [U-Boot] [PATCH v2 02/15] ARM: tegra: Guard clock code with a Kconfig symbol Thierry Reding
2019-03-18 23:24 ` [U-Boot] [PATCH v2 03/15] ARM: tegra: Guard GP pad control " Thierry Reding
2019-03-18 23:24 ` [U-Boot] [PATCH v2 04/15] ARM: tegra: Guard memory controller " Thierry Reding
2019-03-18 23:24 ` [U-Boot] [PATCH v2 05/15] ARM: tegra: Guard pin " Thierry Reding
2019-03-18 23:24 ` [U-Boot] [PATCH v2 06/15] ARM: tegra: Guard powergate " Thierry Reding
2019-03-18 23:24 ` [U-Boot] [PATCH v2 07/15] ARM: tegra: Fix save_boot_params() prototype Thierry Reding
2019-03-18 23:24 ` [U-Boot] [PATCH v2 08/15] ARM: tegra: Allow boards to override boot target devices Thierry Reding
2019-03-18 23:24 ` [U-Boot] [PATCH v2 09/15] ARM: tegra: Support TZ-only access to PMC Thierry Reding
2019-03-18 23:24 ` [U-Boot] [PATCH v2 10/15] ARM: tegra: Workaround UDC boot issues only if necessary Thierry Reding
2019-03-18 23:24 ` [U-Boot] [PATCH v2 11/15] ARM: tegra: Restore DRAM bank count Thierry Reding
2019-03-18 23:24 ` [U-Boot] [PATCH v2 12/15] ARM: tegra: Unify Tegra186 builds Thierry Reding
2019-03-18 23:24 ` [U-Boot] [PATCH v2 13/15] ARM: tegra: Implement cboot_save_boot_params() in C Thierry Reding
2019-03-18 23:24 ` Thierry Reding [this message]
2019-03-18 23:24 ` [U-Boot] [PATCH v2 15/15] ARM: tegra: Add NVIDIA Jetson Nano Developer Kit support Thierry Reding
2019-03-19 17:18   ` Stephen Warren
2019-03-20 10:37     ` Thierry Reding
2019-03-20 11:22 ` [U-Boot] [PATCH v2 00/15] " Peter Robinson
2019-03-26 16:00   ` Thierry Reding
2019-03-26 16:06     ` Peter Robinson
2019-03-26 17:19       ` Thierry Reding

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190318232422.24404-15-thierry.reding@gmail.com \
    --to=thierry.reding@gmail.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox