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_env_get_enetaddr(const char *name, uchar *enetaddr)
29 eth_parse_enetaddr(env_get(name), enetaddr);
30 return is_valid_ethaddr(enetaddr);
33 int eth_env_set_enetaddr(const char *name, const uchar *enetaddr)
35 char buf[ARP_HLEN_ASCII + 1];
37 if (eth_env_get_enetaddr(name, (uchar *)buf))
40 sprintf(buf, "%pM", enetaddr);
42 return env_set(name, buf);
45 int eth_env_get_enetaddr_by_index(const char *base_name, int index,
49 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
50 return eth_env_get_enetaddr(enetvar, enetaddr);
53 int eth_env_set_enetaddr_by_index(const char *base_name, int index,
57 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
58 return eth_env_set_enetaddr(enetvar, enetaddr);
61 void eth_common_init(void)
63 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
64 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
73 int eth_mac_skip(int index)
78 sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index);
79 skip_state = env_get(enetvar);
80 return skip_state != NULL;
83 void eth_current_changed(void)
85 char *act = env_get("ethact");
89 * The call to eth_get_dev() below has a side effect of rotating
90 * ethernet device if uc_priv->current == NULL. This is not what
91 * we want when 'ethrotate' variable is 'no'.
93 ethrotate = env_get("ethrotate");
94 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
97 /* update current ethernet name */
99 if (act == NULL || strcmp(act, eth_get_name()) != 0)
100 env_set("ethact", eth_get_name());
103 * remove the variable completely if there is no active
106 else if (act != NULL)
107 env_set("ethact", NULL);
110 void eth_try_another(int first_restart)
112 static void *first_failed;
116 * Do not rotate between network interfaces when
117 * 'ethrotate' variable is set to 'no'.
119 ethrotate = env_get("ethrotate");
120 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
127 first_failed = eth_get_dev();
129 eth_set_current_to_next();
131 eth_current_changed();
133 if (first_failed == eth_get_dev())
134 net_restart_wrap = 1;
137 void eth_set_current(void)
140 static int env_changed_id;
143 env_id = get_env_id();
144 if ((act == NULL) || (env_changed_id != env_id)) {
145 act = env_get("ethact");
146 env_changed_id = env_id;
150 char *ethprime = env_get("ethprime");
154 dev = eth_get_dev_by_name(ethprime);
160 eth_set_dev(eth_get_dev_by_name(act));
163 eth_current_changed();
166 const char *eth_get_name(void)
168 return eth_get_dev() ? eth_get_dev()->name : "unknown";