diff mbox

[U-Boot,v4] net: allow setting env enetaddr from net device setting

Message ID 1331089414-15080-1-git-send-email-robherring2@gmail.com
State Changes Requested
Delegated to: Joe Hershberger
Headers show

Commit Message

Rob Herring March 7, 2012, 3:03 a.m. UTC
From: Rob Herring <rob.herring@calxeda.com>

If the net driver has setup a valid ethernet address and an ethernet
address is not set in the environment already, then set the environment
variables from the net driver setting.

This enables pxe booting on boards which don't set ethaddr env variable.

Signed-off-by: Rob Herring <rob.herring@calxeda.com>
---
v4:
- rewrite of documentation from Wolfgang

v3:
- print a warning if using mac address from the net device

v2:
- Re-wrote to always setup ethaddr env variables

 doc/README.enetaddr |    6 +++++-
 net/eth.c           |   21 ++++++++++++++++++---
 2 files changed, 23 insertions(+), 4 deletions(-)

Comments

Joe Hershberger April 4, 2012, 3:06 p.m. UTC | #1
Hi Rob,

On Tue, Mar 6, 2012 at 9:03 PM, Rob Herring <robherring2@gmail.com> wrote:
> From: Rob Herring <rob.herring@calxeda.com>
>
> If the net driver has setup a valid ethernet address and an ethernet
> address is not set in the environment already, then set the environment
> variables from the net driver setting.
>
> This enables pxe booting on boards which don't set ethaddr env variable.
>
> Signed-off-by: Rob Herring <rob.herring@calxeda.com>
> ---
> v4:
> - rewrite of documentation from Wolfgang
>
> v3:
> - print a warning if using mac address from the net device
>
> v2:
> - Re-wrote to always setup ethaddr env variables
>
>  doc/README.enetaddr |    6 +++++-
>  net/eth.c           |   21 ++++++++++++++++++---
>  2 files changed, 23 insertions(+), 4 deletions(-)

checkpatch.pl failures:

-------------------------------------

ERROR: trailing whitespace
#48: FILE: doc/README.enetaddr:38:
+   If the environment variable is not set, it will be initialized from $

WARNING: line over 80 characters
#80: FILE: net/eth.c:184:
+       if (!eth_getenv_enetaddr_by_index(base_name, eth_number,
env_enetaddr)) {

total: 1 errors, 1 warnings, 45 lines checked

NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or
      scripts/cleanfile

NOTE: Ignored message types: COMPLEX_MACRO CONSIDER_KSTRTO MINMAX
MULTISTATEMENT_MACRO_USE_DO_WHILE

U-Boot-v4-net-allow-setting-env-enetaddr-from-net-device-setting.patch
has style problems, please review.

----------------------

Also, it seems that just because the enetaddr was read from dev
(possibly from an eeprom or elsewhere) doesn't mean that the MAC
doesn't need you to call write_hwaddr().  I think you shouldn't return
0 if you use the net device's addr, but rather should make the if
(memcmp... into an else if.

-Joe
diff mbox

Patch

diff --git a/doc/README.enetaddr b/doc/README.enetaddr
index 2d8e24f..24b25f7 100644
--- a/doc/README.enetaddr
+++ b/doc/README.enetaddr
@@ -32,7 +32,11 @@  Correct flow of setting up the MAC address (summarized):
 
 1. Read from hardware in initialize() function
 2. Read from environment in net/eth.c after initialize()
-3. Give priority to the value in the environment if a conflict
+3. The environment variable will be compared to the driver initialized
+   struct eth_device->enetaddr. If they differ, a warning is printed, and the
+   environment variable will be used unchanged.
+   If the environment variable is not set, it will be initialized from 
+   eth_device->enetaddr, and a warning will be printed.
 4. Program the address into hardware if the following conditions are met:
 	a) The relevant driver has a 'write_addr' function
 	b) The user hasn't set an 'ethmacskip' environment variable
diff --git a/net/eth.c b/net/eth.c
index f14767b..22cd37d 100644
--- a/net/eth.c
+++ b/net/eth.c
@@ -62,6 +62,15 @@  int eth_getenv_enetaddr_by_index(const char *base_name, int index,
 	return eth_getenv_enetaddr(enetvar, enetaddr);
 }
 
+int eth_setenv_enetaddr_by_index(const char *base_name, int index,
+				 uchar *enetaddr)
+{
+	char enetvar[32];
+	sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
+	return eth_setenv_enetaddr(enetvar, enetaddr);
+}
+
+
 static int eth_mac_skip(int index)
 {
 	char enetvar[15];
@@ -172,9 +181,15 @@  int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
 	unsigned char env_enetaddr[6];
 	int ret = 0;
 
-	if (!eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr))
-		return -1;
-
+	if (!eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr)) {
+		if (!is_valid_ether_addr(dev->enetaddr))
+			return -1;
+		eth_setenv_enetaddr_by_index(base_name, eth_number,
+					     dev->enetaddr);
+		printf("\nWarning: %s using MAC address from net device\n",
+			dev->name);
+		return 0;
+	}
 	if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) {
 		if (memcmp(dev->enetaddr, "\0\0\0\0\0\0", 6) &&
 			memcmp(dev->enetaddr, env_enetaddr, 6)) {