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 * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
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 * 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 Soomestatic int net_get(struct iodesc *, void *, size_t, time_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int net_put(struct iodesc *, void *, size_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* dif_unit dif_nsel dif_stats dif_private */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int sc_handle; /* device handle for ub_dev_xxx */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * get_env_net_params()
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 * 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 * 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 * 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 * 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 * 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 if ((myip.s_addr = inet_addr(envstr)) == INADDR_NONE) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Could not parse ipaddr '%s'\n", envstr);
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 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 * 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 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 * 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 printf("No server address for rootpath '%s'\n", envstr);
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 printf("Need gatewayip for a root server on a "
199767f8919635c4928607450d9e0abb932109ceToomas Soome "different network.\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((gateip.s_addr = inet_addr(envstr) == INADDR_NONE)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Could not parse gatewayip '%s'\n", envstr);
199767f8919635c4928607450d9e0abb932109ceToomas Soomenet_match(struct netif *nif, void *machdep_hint)
199767f8919635c4928607450d9e0abb932109ceToomas Soome char **a = (char **)machdep_hint;
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_match: could not match network device\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soomenet_probe(struct netif *nif, void *machdep_hint)
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < devs_no; i++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_probe: no network devices found, maybe not"
199767f8919635c4928607450d9e0abb932109ceToomas Soome " enumerated yet..?\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_probe: network device found: %d\n", i);
199767f8919635c4928607450d9e0abb932109ceToomas Soomenet_put(struct iodesc *desc, void *pkt, size_t len)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_put: desc %p, pkt %p, len %d\n", desc, pkt, len);
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 rv = ub_dev_send(sc->sc_handle, sc->sc_txbufp, sendlen);
199767f8919635c4928607450d9e0abb932109ceToomas Soomenet_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_get: pkt %p, len %d, timeout %d\n", pkt, len, timeout);
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = ub_dev_recv(sc->sc_handle, sc->sc_rxbuf, len, &rlen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_get: ub_dev_recv() failed, error=%d\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome } while ((rlen == -1 || rlen == 0) && (getsecs() - t < timeout));
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_get: received len %d (%x)\n", rlen, rlen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("net_get: len %x, rlen %x\n", len, rlen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soomenet_init(struct iodesc *desc, void *machdep_hint)
199767f8919635c4928607450d9e0abb932109ceToomas Soome panic("%s%d: initialisation failed with error %d\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome nif->nif_driver->netif_bname, nif->nif_unit, err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Get MAC address */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (memcmp (desc->myea, "\0\0\0\0\0\0", 6) == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Attempt to get netboot params from the u-boot env. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("network: %s%d attached to %s\n", nif->nif_driver->netif_bname,
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Set correct alignment for TX packets */