[U-Boot] [PATCH] fdt: Rewrite the logic in fdt_fixup_ethernet()

Bin Meng bmeng.cn at gmail.com
Tue Oct 27 17:10:39 CET 2015


Currently in fdt_fixup_ethernet() the MAC address fix up is
handled in a loop of which the exit condition is to test the
"eth%daddr" env is not NULL. However this creates unnecessary
constrains that those "eth%daddr" env variables must be
sequential even if "ethernet%d" does not start from 0 in the
"/aliases" node. For example, with "/aliases" node below:

    aliases {
        ethernet3 = &enet3;
        ethernet4 = &enet4;
    };

"ethaddr", "eth1addr", "eth2addr" must exist in order to fix
up ethernet3's MAC address successfully.

Now we change the loop logic to iterate the properties in the
"/aliases" node. For each property, test if it is in a format
of "ethernet%d", then get its MAC address from corresponding
"eth%daddr" env and fix it up in the dtb.

Signed-off-by: Bin Meng <bmeng.cn at gmail.com>
---

 common/fdt_support.c | 53 ++++++++++++++++++++++++++++++++--------------------
 1 file changed, 33 insertions(+), 20 deletions(-)

diff --git a/common/fdt_support.c b/common/fdt_support.c
index f86365e..5cdf185 100644
--- a/common/fdt_support.c
+++ b/common/fdt_support.c
@@ -476,10 +476,11 @@ int fdt_fixup_memory(void *blob, u64 start, u64 size)
 void fdt_fixup_ethernet(void *fdt)
 {
 	int node, i, j;
-	char enet[16], *tmp, *end;
+	char *tmp, *end;
 	char mac[16];
 	const char *path;
 	unsigned char mac_addr[6];
+	int offset;
 
 	node = fdt_path_offset(fdt, "/aliases");
 	if (node < 0)
@@ -496,27 +497,39 @@ void fdt_fixup_ethernet(void *fdt)
 		strcpy(mac, "ethaddr");
 	}
 
-	i = 0;
-	while ((tmp = getenv(mac)) != NULL) {
-		sprintf(enet, "ethernet%d", i);
-		path = fdt_getprop(fdt, node, enet, NULL);
-		if (!path) {
-			debug("No alias for %s\n", enet);
-			sprintf(mac, "eth%daddr", ++i);
-			continue;
-		}
+	for (offset = fdt_first_property_offset(fdt, node);
+	     offset > 0;
+	     offset = fdt_next_property_offset(fdt, offset)) {
+		const char *name;
+		int len = strlen("ethernet");
+
+		path = fdt_getprop_by_offset(fdt, offset, &name, NULL);
+		if (!strncmp(name, "ethernet", len)) {
+			i = trailing_strtol(name);
+			if (i != -1) {
+				if (i == 0)
+					strcpy(mac, "ethaddr");
+				else
+					sprintf(mac, "eth%daddr", i);
+			} else {
+				continue;
+			}
+			tmp = getenv(mac);
+			if (!tmp)
+				continue;
+
+			for (j = 0; j < 6; j++) {
+				mac_addr[j] = tmp ?
+					      simple_strtoul(tmp, &end, 16) : 0;
+				if (tmp)
+					tmp = (*end) ? end + 1 : end;
+			}
 
-		for (j = 0; j < 6; j++) {
-			mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
-			if (tmp)
-				tmp = (*end) ? end+1 : end;
+			do_fixup_by_path(fdt, path, "mac-address",
+					 &mac_addr, 6, 0);
+			do_fixup_by_path(fdt, path, "local-mac-address",
+					 &mac_addr, 6, 1);
 		}
-
-		do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0);
-		do_fixup_by_path(fdt, path, "local-mac-address",
-				&mac_addr, 6, 1);
-
-		sprintf(mac, "eth%daddr", ++i);
 	}
 }
 
-- 
1.8.2.1



More information about the U-Boot mailing list