2 * (C) Copyright 2001-2015
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 * Joe Hershberger, National Instruments
6 * SPDX-License-Identifier: GPL-2.0+
13 #include "eth_internal.h"
15 void eth_parse_enetaddr(const char *addr, uchar *enetaddr)
20 for (i = 0; i < 6; ++i) {
21 enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
23 addr = (*end) ? end + 1 : end;
27 int eth_getenv_enetaddr(const char *name, uchar *enetaddr)
29 eth_parse_enetaddr(getenv(name), enetaddr);
30 return is_valid_ethaddr(enetaddr);
33 int eth_setenv_enetaddr(const char *name, const uchar *enetaddr)
37 sprintf(buf, "%pM", enetaddr);
39 return setenv(name, buf);
42 int eth_getenv_enetaddr_by_index(const char *base_name, int index,
46 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
47 return eth_getenv_enetaddr(enetvar, enetaddr);
50 int eth_setenv_enetaddr_by_index(const char *base_name, int index,
54 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
55 return eth_setenv_enetaddr(enetvar, enetaddr);
58 void eth_common_init(void)
60 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
61 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
70 int eth_mac_skip(int index)
75 sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index);
76 skip_state = getenv(enetvar);
77 return skip_state != NULL;
80 void eth_current_changed(void)
82 char *act = getenv("ethact");
86 * The call to eth_get_dev() below has a side effect of rotating
87 * ethernet device if uc_priv->current == NULL. This is not what
88 * we want when 'ethrotate' variable is 'no'.
90 ethrotate = getenv("ethrotate");
91 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
94 /* update current ethernet name */
96 if (act == NULL || strcmp(act, eth_get_name()) != 0)
97 setenv("ethact", eth_get_name());
100 * remove the variable completely if there is no active
103 else if (act != NULL)
104 setenv("ethact", NULL);
107 void eth_try_another(int first_restart)
109 static void *first_failed;
113 * Do not rotate between network interfaces when
114 * 'ethrotate' variable is set to 'no'.
116 ethrotate = getenv("ethrotate");
117 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
124 first_failed = eth_get_dev();
126 eth_set_current_to_next();
128 eth_current_changed();
130 if (first_failed == eth_get_dev())
131 net_restart_wrap = 1;
134 void eth_set_current(void)
137 static int env_changed_id;
140 env_id = get_env_id();
141 if ((act == NULL) || (env_changed_id != env_id)) {
142 act = getenv("ethact");
143 env_changed_id = env_id;
147 char *ethprime = getenv("ethprime");
151 dev = eth_get_dev_by_name(ethprime);
157 eth_set_dev(eth_get_dev_by_name(act));
160 eth_current_changed();
163 const char *eth_get_name(void)
165 return eth_get_dev() ? eth_get_dev()->name : "unknown";