#include <sys/param.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/if_ether.h>
#include <netinet/ip.h>
#include <stand.h>
#include <net.h>
#include <netif.h>
#include "api_public.h"
#include "glue.h"
#include "libuboot.h"
#include "dev_net.h"
static int net_probe(struct netif *, void *);
static int net_match(struct netif *, void *);
static void net_init(struct iodesc *, void *);
static ssize_t net_get(struct iodesc *, void **, time_t);
static ssize_t net_put(struct iodesc *, void *, size_t);
static void net_end(struct netif *);
extern struct netif_stats net_stats[];
struct netif_dif net_ifs[] = {
{ 0, 1, &net_stats[0], 0, },
};
struct netif_stats net_stats[nitems(net_ifs)];
struct netif_driver uboot_net = {
"uboot_eth",
net_match,
net_probe,
net_init,
net_get,
net_put,
net_end,
net_ifs,
nitems(net_ifs)
};
struct uboot_softc {
uint32_t sc_pad;
uint8_t sc_rxbuf[ETHER_MAX_LEN];
uint8_t sc_txbuf[ETHER_MAX_LEN + PKTALIGN];
uint8_t *sc_txbufp;
int sc_handle;
};
static struct uboot_softc uboot_softc;
static void
get_env_net_params(void)
{
char *envstr;
in_addr_t rootaddr, serveraddr;
if ((envstr = ub_env_get("rootpath")) == NULL)
return;
strlcpy(rootpath, envstr, sizeof(rootpath));
setenv("dhcp.root-path", rootpath, 0);
if ((envstr = ub_env_get("ipaddr")) == NULL)
return;
if ((myip.s_addr = inet_addr(envstr)) == INADDR_NONE) {
printf("Could not parse ipaddr '%s'\n", envstr);
return;
}
if ((envstr = ub_env_get("netmask")) != NULL &&
(netmask = inet_addr(envstr)) == INADDR_NONE) {
printf("Could not parse netmask '%s'\n", envstr);
}
if (netmask == INADDR_NONE) {
if (IN_CLASSA(myip.s_addr))
netmask = IN_CLASSA_NET;
else if (IN_CLASSB(myip.s_addr))
netmask = IN_CLASSB_NET;
else
netmask = IN_CLASSC_NET;
}
serveraddr = INADDR_NONE;
if ((envstr = ub_env_get("serverip")) != NULL) {
if ((serveraddr = inet_addr(envstr)) == INADDR_NONE)
printf("Could not parse serverip '%s'\n", envstr);
}
rootaddr = net_parse_rootpath();
if (rootaddr == INADDR_NONE)
rootaddr = serveraddr;
if (rootaddr == INADDR_NONE) {
printf("No server address for rootpath '%s'\n", envstr);
return;
}
rootip.s_addr = rootaddr;
envstr = ub_env_get("gatewayip");
if (!SAMENET(myip, rootip, netmask)) {
if (envstr == NULL) {
printf("Need gatewayip for a root server on a "
"different network.\n");
rootip.s_addr = 0;
return;
}
if ((gateip.s_addr = inet_addr(envstr)) == INADDR_NONE) {
printf("Could not parse gatewayip '%s'\n", envstr);
rootip.s_addr = 0;
return;
}
}
}
static int
net_match(struct netif *nif, void *machdep_hint)
{
char **a = (char **)machdep_hint;
if (memcmp("net", *a, 3) == 0)
return (1);
printf("net_match: could not match network device\n");
return (0);
}
static int
net_probe(struct netif *nif, void *machdep_hint)
{
struct device_info *di;
int i;
for (i = 0; i < devs_no; i++)
if ((di = ub_dev_get(i)) != NULL)
if (di->type == DEV_TYP_NET)
break;
if (i == devs_no) {
printf("net_probe: no network devices found, maybe not"
" enumerated yet..?\n");
return (-1);
}
#if defined(NETIF_DEBUG)
printf("net_probe: network device found: %d\n", i);
#endif
uboot_softc.sc_handle = i;
return (0);
}
static ssize_t
net_put(struct iodesc *desc, void *pkt, size_t len)
{
struct netif *nif = desc->io_netif;
struct uboot_softc *sc = nif->nif_devdata;
size_t sendlen;
ssize_t rv;
#if defined(NETIF_DEBUG)
struct ether_header *eh;
printf("net_put: desc %p, pkt %p, len %d\n", desc, pkt, len);
eh = pkt;
printf("dst: %s ", ether_sprintf(eh->ether_dhost));
printf("src: %s ", ether_sprintf(eh->ether_shost));
printf("type: 0x%x\n", eh->ether_type & 0xffff);
#endif
if (len < ETHER_MIN_LEN - ETHER_CRC_LEN) {
sendlen = ETHER_MIN_LEN - ETHER_CRC_LEN;
bzero(sc->sc_txbufp, sendlen);
} else
sendlen = len;
memcpy(sc->sc_txbufp, pkt, len);
rv = ub_dev_send(sc->sc_handle, sc->sc_txbufp, sendlen);
#if defined(NETIF_DEBUG)
printf("net_put: ub_send returned %d\n", rv);
#endif
if (rv == 0)
rv = len;
else
rv = -1;
return (rv);
}
static ssize_t
net_get(struct iodesc *desc, void **pkt, time_t timeout)
{
struct netif *nif = desc->io_netif;
struct uboot_softc *sc = nif->nif_devdata;
time_t t;
int err, rlen;
size_t len;
char *buf;
#if defined(NETIF_DEBUG)
printf("net_get: pkt %p, timeout %d\n", pkt, timeout);
#endif
t = getsecs();
len = sizeof(sc->sc_rxbuf);
do {
err = ub_dev_recv(sc->sc_handle, sc->sc_rxbuf, len, &rlen);
if (err != 0) {
printf("net_get: ub_dev_recv() failed, error=%d\n",
err);
rlen = 0;
break;
}
} while ((rlen == -1 || rlen == 0) && (getsecs() - t < timeout));
#if defined(NETIF_DEBUG)
printf("net_get: received len %d (%x)\n", rlen, rlen);
#endif
if (rlen > 0) {
buf = malloc(rlen + ETHER_ALIGN);
if (buf == NULL)
return (-1);
memcpy(buf + ETHER_ALIGN, sc->sc_rxbuf, rlen);
*pkt = buf;
return ((ssize_t)rlen);
}
return (-1);
}
static void
net_init(struct iodesc *desc, void *machdep_hint)
{
struct netif *nif = desc->io_netif;
struct uboot_softc *sc;
struct device_info *di;
int err;
sc = nif->nif_devdata = &uboot_softc;
if ((err = ub_dev_open(sc->sc_handle)) != 0)
panic("%s%d: initialisation failed with error %d",
nif->nif_driver->netif_bname, nif->nif_unit, err);
di = ub_dev_get(sc->sc_handle);
memcpy(desc->myea, di->di_net.hwaddr, 6);
if (memcmp (desc->myea, "\0\0\0\0\0\0", 6) == 0) {
panic("%s%d: empty ethernet address!",
nif->nif_driver->netif_bname, nif->nif_unit);
}
get_env_net_params();
if (myip.s_addr != 0)
desc->myip = myip;
#if defined(NETIF_DEBUG)
printf("network: %s%d attached to %s\n", nif->nif_driver->netif_bname,
nif->nif_unit, ether_sprintf(desc->myea));
#endif
sc->sc_txbufp = sc->sc_txbuf;
if ((unsigned long)sc->sc_txbufp % PKTALIGN)
sc->sc_txbufp += PKTALIGN -
(unsigned long)sc->sc_txbufp % PKTALIGN;
}
static void
net_end(struct netif *nif)
{
struct uboot_softc *sc = nif->nif_devdata;
int err;
if ((err = ub_dev_close(sc->sc_handle)) != 0)
panic("%s%d: net_end failed with error %d",
nif->nif_driver->netif_bname, nif->nif_unit, err);
}