X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=net%2Feth.c;h=eac4f7b3d0ed602c852d0f47dd9580670f8292da;hb=b5e7c84f72ee6ce991569a4db610ab0b4a2a9978;hp=d9a643073d8a0eecd305f07e3605d97a39e71c4b;hpb=de30122bb58fee7b0f94bcfabab595b6ad757336;p=u-boot diff --git a/net/eth.c b/net/eth.c index d9a643073d..eac4f7b3d0 100644 --- a/net/eth.c +++ b/net/eth.c @@ -2,23 +2,7 @@ * (C) Copyright 2001-2010 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. * - * See file CREDITS for list of people who contributed to this - * project. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of - * the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, - * MA 02111-1307 USA + * SPDX-License-Identifier: GPL-2.0+ */ #include @@ -26,6 +10,7 @@ #include #include #include +#include void eth_parse_enetaddr(const char *addr, uchar *enetaddr) { @@ -62,6 +47,15 @@ int eth_getenv_enetaddr_by_index(const char *base_name, int index, return eth_getenv_enetaddr(enetvar, enetaddr); } +static inline 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]; @@ -90,12 +84,8 @@ static struct { static unsigned int eth_rcv_current, eth_rcv_last; #endif -static struct eth_device *eth_devices, *eth_current; - -struct eth_device *eth_get_dev(void) -{ - return eth_current; -} +static struct eth_device *eth_devices; +struct eth_device *eth_current; struct eth_device *eth_get_dev_by_name(const char *devname) { @@ -163,6 +153,11 @@ static void eth_current_changed(void) setenv("ethact", NULL); } +static int eth_address_set(unsigned char *addr) +{ + return memcmp(addr, "\0\0\0\0\0\0", 6); +} + int eth_write_hwaddr(struct eth_device *dev, const char *base_name, int eth_number) { @@ -171,8 +166,8 @@ int eth_write_hwaddr(struct eth_device *dev, const char *base_name, eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr); - if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) { - if (memcmp(dev->enetaddr, "\0\0\0\0\0\0", 6) && + if (eth_address_set(env_enetaddr)) { + if (eth_address_set(dev->enetaddr) && memcmp(dev->enetaddr, env_enetaddr, 6)) { printf("\nWarning: %s MAC addresses don't match:\n", dev->name); @@ -183,12 +178,28 @@ int eth_write_hwaddr(struct eth_device *dev, const char *base_name, } memcpy(dev->enetaddr, env_enetaddr, 6); + } else if (is_valid_ether_addr(dev->enetaddr)) { + eth_setenv_enetaddr_by_index(base_name, eth_number, + dev->enetaddr); + printf("\nWarning: %s using MAC address from net device\n", + dev->name); + } else if (!(eth_address_set(dev->enetaddr))) { + printf("\nError: %s address not set.\n", + dev->name); + return -EINVAL; } - if (dev->write_hwaddr && - !eth_mac_skip(eth_number) && - is_valid_ether_addr(dev->enetaddr)) + if (dev->write_hwaddr && !eth_mac_skip(eth_number)) { + if (!is_valid_ether_addr(dev->enetaddr)) { + printf("\nError: %s address %pM illegal value\n", + dev->name, dev->enetaddr); + return -EINVAL; + } + ret = dev->write_hwaddr(dev); + if (ret) + printf("\nWarning: %s failed to set MAC address\n", dev->name); + } return ret; } @@ -260,7 +271,7 @@ int eth_initialize(bd_t *bis) eth_current = NULL; bootstage_mark(BOOTSTAGE_ID_NET_ETH_START); -#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) +#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB) miiphy_init(); #endif @@ -306,8 +317,7 @@ int eth_initialize(bd_t *bis) puts("\nWarning: eth device name has a space!" "\n"); - if (eth_write_hwaddr(dev, "eth", dev->index)) - puts("\nWarning: failed to set MAC address\n"); + eth_write_hwaddr(dev, "eth", dev->index); dev = dev->next; num_devices++; @@ -464,10 +474,7 @@ int eth_receive(void *packet, int length) return -1; } - if (length < eth_rcv_bufs[eth_rcv_current].length) - return -1; - - length = eth_rcv_bufs[eth_rcv_current].length; + length = min(eth_rcv_bufs[eth_rcv_current].length, length); for (i = 0; i < length; i++) p[i] = eth_rcv_bufs[eth_rcv_current].data[i];