199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2000-2001 Benno Rice
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2007 Semihalf, Rafal Jaworowski <raj@semihalf.com>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are met:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome__FBSDID("$FreeBSD$");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/param.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/types.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/socket.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <net/if.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <netinet/in.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <netinet/in_systm.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <netinet/if_ether.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <netinet/ip.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stand.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <net.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <netif.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "api_public.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "glue.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "libuboot.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "dev_net.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int net_probe(struct netif *, void *);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int net_match(struct netif *, void *);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void net_init(struct iodesc *, void *);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int net_get(struct iodesc *, void *, size_t, time_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int net_put(struct iodesc *, void *, size_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void net_end(struct netif *);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern struct netif_stats net_stats[];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct netif_dif net_ifs[] = {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* dif_unit dif_nsel dif_stats dif_private */
199767f8919635c4928607450d9e0abb932109ceToomas Soome { 0, 1, &net_stats[0], 0, },
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct netif_stats net_stats[NENTS(net_ifs)];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct netif_driver uboot_net = {
199767f8919635c4928607450d9e0abb932109ceToomas Soome "uboot_eth", /* netif_bname */
199767f8919635c4928607450d9e0abb932109ceToomas Soome net_match, /* netif_match */
199767f8919635c4928607450d9e0abb932109ceToomas Soome net_probe, /* netif_probe */
199767f8919635c4928607450d9e0abb932109ceToomas Soome net_init, /* netif_init */
199767f8919635c4928607450d9e0abb932109ceToomas Soome net_get, /* netif_get */
199767f8919635c4928607450d9e0abb932109ceToomas Soome net_put, /* netif_put */
199767f8919635c4928607450d9e0abb932109ceToomas Soome net_end, /* netif_end */
199767f8919635c4928607450d9e0abb932109ceToomas Soome net_ifs, /* netif_ifs */
199767f8919635c4928607450d9e0abb932109ceToomas Soome NENTS(net_ifs) /* netif_nifs */
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct uboot_softc {
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t sc_pad;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint8_t sc_rxbuf[ETHER_MAX_LEN];
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint8_t sc_txbuf[ETHER_MAX_LEN + PKTALIGN];
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint8_t *sc_txbufp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int sc_handle; /* device handle for ub_dev_xxx */
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic struct uboot_softc uboot_softc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * get_env_net_params()
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Attempt to obtain all the parms we need for netbooting from the U-Boot
199767f8919635c4928607450d9e0abb932109ceToomas Soome * environment. If we fail to obtain the values it may still be possible to
199767f8919635c4928607450d9e0abb932109ceToomas Soome * netboot; the net_dev code will attempt to get the values from bootp, rarp,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * and other such sources.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If rootip.s_addr is non-zero net_dev assumes the required global variables
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are set and skips the bootp inquiry. For that reason, we don't set rootip
199767f8919635c4928607450d9e0abb932109ceToomas Soome * until we've verified that we have at least the minimum required info.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This is called from netif_init() which can result in it getting called
199767f8919635c4928607450d9e0abb932109ceToomas Soome * multiple times, by design. The network code at higher layers zeroes out
199767f8919635c4928607450d9e0abb932109ceToomas Soome * rootip when it closes a network interface, so if it gets opened again we have
199767f8919635c4928607450d9e0abb932109ceToomas Soome * to obtain all this info again.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomeget_env_net_params()
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *envstr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome in_addr_t rootaddr, serveraddr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Silently get out right away if we don't have rootpath, because none
199767f8919635c4928607450d9e0abb932109ceToomas Soome * of the other info we obtain below is sufficient to boot without it.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If we do have rootpath, copy it into the global var and also set
199767f8919635c4928607450d9e0abb932109ceToomas Soome * dhcp.root-path in the env. If we don't get all the other info from
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the u-boot env below, we will still try dhcp/bootp, but the server-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * provided path will not replace the user-provided value we set here.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((envstr = ub_env_get("rootpath")) == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome strlcpy(rootpath, envstr, sizeof(rootpath));
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("dhcp.root-path", rootpath, 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Our own IP address must be valid. Silently get out if it's not set,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * but whine if it's there and we can't parse it.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((envstr = ub_env_get("ipaddr")) == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((myip.s_addr = inet_addr(envstr)) == INADDR_NONE) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Could not parse ipaddr '%s'\n", envstr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Netmask is optional, default to the "natural" netmask for our IP, but
199767f8919635c4928607450d9e0abb932109ceToomas Soome * whine if it was provided and we couldn't parse it.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((envstr = ub_env_get("netmask")) != NULL &&
199767f8919635c4928607450d9e0abb932109ceToomas Soome (netmask = inet_addr(envstr)) == INADDR_NONE) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Could not parse netmask '%s'\n", envstr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (netmask == INADDR_NONE) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (IN_CLASSA(myip.s_addr))
199767f8919635c4928607450d9e0abb932109ceToomas Soome netmask = IN_CLASSA_NET;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (IN_CLASSB(myip.s_addr))
199767f8919635c4928607450d9e0abb932109ceToomas Soome netmask = IN_CLASSB_NET;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome netmask = IN_CLASSC_NET;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Get optional serverip before rootpath; the latter can override it.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Whine only if it's present but can't be parsed.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome serveraddr = INADDR_NONE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((envstr = ub_env_get("serverip")) != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((serveraddr = inet_addr(envstr)) == INADDR_NONE)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Could not parse serverip '%s'\n", envstr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * There must be a rootpath. It may be ip:/path or it may be just the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * path in which case the ip needs to be in serverip.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome rootaddr = net_parse_rootpath();
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rootaddr == INADDR_NONE)
199767f8919635c4928607450d9e0abb932109ceToomas Soome rootaddr = serveraddr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rootaddr == INADDR_NONE) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("No server address for rootpath '%s'\n", envstr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome rootip.s_addr = rootaddr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Gateway IP is optional unless rootip is on a different net in which
199767f8919635c4928607450d9e0abb932109ceToomas Soome * case whine if it's missing or we can't parse it, and set rootip addr
199767f8919635c4928607450d9e0abb932109ceToomas Soome * to zero, which signals to other network code that network params
199767f8919635c4928607450d9e0abb932109ceToomas Soome * aren't set (so it will try dhcp, bootp, etc).
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome envstr = ub_env_get("gatewayip");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!SAMENET(myip, rootip, netmask)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (envstr == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Need gatewayip for a root server on a "
199767f8919635c4928607450d9e0abb932109ceToomas Soome "different network.\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome rootip.s_addr = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((gateip.s_addr = inet_addr(envstr) == INADDR_NONE)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Could not parse gatewayip '%s'\n", envstr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome rootip.s_addr = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomenet_match(struct netif *nif, void *machdep_hint)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char **a = (char **)machdep_hint;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (memcmp("net", *a, 3) == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_match: could not match network device\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomenet_probe(struct netif *nif, void *machdep_hint)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct device_info *di;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < devs_no; i++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((di = ub_dev_get(i)) != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (di->type == DEV_TYP_NET)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (i == devs_no) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_probe: no network devices found, maybe not"
199767f8919635c4928607450d9e0abb932109ceToomas Soome " enumerated yet..?\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(NETIF_DEBUG)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_probe: network device found: %d\n", i);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome uboot_softc.sc_handle = i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomenet_put(struct iodesc *desc, void *pkt, size_t len)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct netif *nif = desc->io_netif;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct uboot_softc *sc = nif->nif_devdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t sendlen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ssize_t rv;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(NETIF_DEBUG)
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct ether_header *eh;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_put: desc %p, pkt %p, len %d\n", desc, pkt, len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome eh = pkt;
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("dst: %s ", ether_sprintf(eh->ether_dhost));
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("src: %s ", ether_sprintf(eh->ether_shost));
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("type: 0x%x\n", eh->ether_type & 0xffff);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (len < ETHER_MIN_LEN - ETHER_CRC_LEN) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sendlen = ETHER_MIN_LEN - ETHER_CRC_LEN;
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(sc->sc_txbufp, sendlen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else
199767f8919635c4928607450d9e0abb932109ceToomas Soome sendlen = len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome memcpy(sc->sc_txbufp, pkt, len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome rv = ub_dev_send(sc->sc_handle, sc->sc_txbufp, sendlen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(NETIF_DEBUG)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_put: ub_send returned %d\n", rv);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rv == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome rv = len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome rv = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rv);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomenet_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct netif *nif = desc->io_netif;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct uboot_softc *sc = nif->nif_devdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome time_t t;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int err, rlen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(NETIF_DEBUG)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_get: pkt %p, len %d, timeout %d\n", pkt, len, timeout);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome t = getsecs();
199767f8919635c4928607450d9e0abb932109ceToomas Soome do {
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = ub_dev_recv(sc->sc_handle, sc->sc_rxbuf, len, &rlen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (err != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_get: ub_dev_recv() failed, error=%d\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome rlen = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome } while ((rlen == -1 || rlen == 0) && (getsecs() - t < timeout));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(NETIF_DEBUG)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_get: received len %d (%x)\n", rlen, rlen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rlen > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome memcpy(pkt, sc->sc_rxbuf, MIN(len, rlen));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rlen != len) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(NETIF_DEBUG)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_get: len %x, rlen %x\n", len, rlen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rlen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomenet_init(struct iodesc *desc, void *machdep_hint)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct netif *nif = desc->io_netif;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct uboot_softc *sc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct device_info *di;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int err;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome sc = nif->nif_devdata = &uboot_softc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((err = ub_dev_open(sc->sc_handle)) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome panic("%s%d: initialisation failed with error %d\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome nif->nif_driver->netif_bname, nif->nif_unit, err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Get MAC address */
199767f8919635c4928607450d9e0abb932109ceToomas Soome di = ub_dev_get(sc->sc_handle);
199767f8919635c4928607450d9e0abb932109ceToomas Soome memcpy(desc->myea, di->di_net.hwaddr, 6);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (memcmp (desc->myea, "\0\0\0\0\0\0", 6) == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome panic("%s%d: empty ethernet address!",
199767f8919635c4928607450d9e0abb932109ceToomas Soome nif->nif_driver->netif_bname, nif->nif_unit);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Attempt to get netboot params from the u-boot env. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome get_env_net_params();
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (myip.s_addr != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome desc->myip = myip;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(NETIF_DEBUG)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("network: %s%d attached to %s\n", nif->nif_driver->netif_bname,
199767f8919635c4928607450d9e0abb932109ceToomas Soome nif->nif_unit, ether_sprintf(desc->myea));
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Set correct alignment for TX packets */
199767f8919635c4928607450d9e0abb932109ceToomas Soome sc->sc_txbufp = sc->sc_txbuf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((unsigned long)sc->sc_txbufp % PKTALIGN)
199767f8919635c4928607450d9e0abb932109ceToomas Soome sc->sc_txbufp += PKTALIGN -
199767f8919635c4928607450d9e0abb932109ceToomas Soome (unsigned long)sc->sc_txbufp % PKTALIGN;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomenet_end(struct netif *nif)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct uboot_softc *sc = nif->nif_devdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int err;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((err = ub_dev_close(sc->sc_handle)) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome panic("%s%d: net_end failed with error %d\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome nif->nif_driver->netif_bname, nif->nif_unit, err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}