2 * Copyright (c) 2000-2001 Benno Rice
3 * Copyright (c) 2007 Semihalf, Rafal Jaworowski <raj@semihalf.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <netinet/in_systm.h>
38 #include <netinet/if_ether.h>
39 #include <netinet/ip.h>
45 #include "api_public.h"
50 static int net_probe(struct netif *, void *);
51 static int net_match(struct netif *, void *);
52 static void net_init(struct iodesc *, void *);
53 static int net_get(struct iodesc *, void *, size_t, time_t);
54 static int net_put(struct iodesc *, void *, size_t);
55 static void net_end(struct netif *);
57 extern struct netif_stats net_stats[];
59 struct netif_dif net_ifs[] = {
60 /* dif_unit dif_nsel dif_stats dif_private */
61 { 0, 1, &net_stats[0], 0, },
64 struct netif_stats net_stats[NENTS(net_ifs)];
66 struct netif_driver uboot_net = {
67 "uboot_eth", /* netif_bname */
68 net_match, /* netif_match */
69 net_probe, /* netif_probe */
70 net_init, /* netif_init */
71 net_get, /* netif_get */
72 net_put, /* netif_put */
73 net_end, /* netif_end */
74 net_ifs, /* netif_ifs */
75 NENTS(net_ifs) /* netif_nifs */
80 uint8_t sc_rxbuf[ETHER_MAX_LEN];
81 uint8_t sc_txbuf[ETHER_MAX_LEN + PKTALIGN];
83 int sc_handle; /* device handle for ub_dev_xxx */
86 static struct uboot_softc uboot_softc;
89 * get_env_net_params()
91 * Attempt to obtain all the parms we need for netbooting from the U-Boot
92 * environment. If we fail to obtain the values it may still be possible to
93 * netboot; the net_dev code will attempt to get the values from bootp, rarp,
94 * and other such sources.
96 * If rootip.s_addr is non-zero net_dev assumes the required global variables
97 * are set and skips the bootp inquiry. For that reason, we don't set rootip
98 * until we've verified that we have at least the minimum required info.
100 * This is called from netif_init() which can result in it getting called
101 * multiple times, by design. The network code at higher layers zeroes out
102 * rootip when it closes a network interface, so if it gets opened again we have
103 * to obtain all this info again.
109 in_addr_t rootaddr, serveraddr;
111 /* Silently get out right away if we don't have rootpath. */
112 if (ub_env_get("rootpath") == NULL)
116 * Our own IP address must be valid. Silently get out if it's not set,
117 * but whine if it's there and we can't parse it.
119 if ((envstr = ub_env_get("ipaddr")) == NULL)
121 if ((myip.s_addr = inet_addr(envstr)) == INADDR_NONE) {
122 printf("Could not parse ipaddr '%s'\n", envstr);
127 * Netmask is optional, default to the "natural" netmask for our IP, but
128 * whine if it was provided and we couldn't parse it.
130 if ((envstr = ub_env_get("netmask")) != NULL &&
131 (netmask = inet_addr(envstr)) == INADDR_NONE) {
132 printf("Could not parse netmask '%s'\n", envstr);
134 if (netmask == INADDR_NONE) {
135 if (IN_CLASSA(myip.s_addr))
136 netmask = IN_CLASSA_NET;
137 else if (IN_CLASSB(myip.s_addr))
138 netmask = IN_CLASSB_NET;
140 netmask = IN_CLASSC_NET;
144 * Get optional serverip before rootpath; the latter can override it.
145 * Whine only if it's present but can't be parsed.
147 serveraddr = INADDR_NONE;
148 if ((envstr = ub_env_get("serverip")) != NULL) {
149 if ((serveraddr = inet_addr(envstr)) == INADDR_NONE)
150 printf("Could not parse serverip '%s'\n", envstr);
154 * There must be a rootpath. It may be ip:/path or it may be just the
155 * path in which case the ip needs to be in serverip.
157 if ((envstr = ub_env_get("rootpath")) == NULL)
159 strncpy(rootpath, envstr, sizeof(rootpath) - 1);
160 rootaddr = net_parse_rootpath();
161 if (rootaddr == INADDR_NONE)
162 rootaddr = serveraddr;
163 if (rootaddr == INADDR_NONE) {
164 printf("No server address for rootpath '%s'\n", envstr);
167 rootip.s_addr = rootaddr;
170 * Gateway IP is optional unless rootip is on a different net in which
171 * case whine if it's missing or we can't parse it, and set rootip addr
172 * to zero, which signals to other network code that network params
173 * aren't set (so it will try dhcp, bootp, etc).
175 envstr = ub_env_get("gatewayip");
176 if (!SAMENET(myip, rootip, netmask)) {
177 if (envstr == NULL) {
178 printf("Need gatewayip for a root server on a "
179 "different network.\n");
183 if ((gateip.s_addr = inet_addr(envstr) == INADDR_NONE)) {
184 printf("Could not parse gatewayip '%s'\n", envstr);
192 net_match(struct netif *nif, void *machdep_hint)
194 char **a = (char **)machdep_hint;
196 if (memcmp("net", *a, 3) == 0)
199 printf("net_match: could not match network device\n");
204 net_probe(struct netif *nif, void *machdep_hint)
206 struct device_info *di;
209 for (i = 0; i < devs_no; i++)
210 if ((di = ub_dev_get(i)) != NULL)
211 if (di->type == DEV_TYP_NET)
215 printf("net_probe: no network devices found, maybe not"
216 " enumerated yet..?\n");
220 #if defined(NETIF_DEBUG)
221 printf("net_probe: network device found: %d\n", i);
223 uboot_softc.sc_handle = i;
229 net_put(struct iodesc *desc, void *pkt, size_t len)
231 struct netif *nif = desc->io_netif;
232 struct uboot_softc *sc = nif->nif_devdata;
236 #if defined(NETIF_DEBUG)
237 struct ether_header *eh;
239 printf("net_put: desc %p, pkt %p, len %d\n", desc, pkt, len);
241 printf("dst: %s ", ether_sprintf(eh->ether_dhost));
242 printf("src: %s ", ether_sprintf(eh->ether_shost));
243 printf("type: 0x%x\n", eh->ether_type & 0xffff);
246 if (len < ETHER_MIN_LEN - ETHER_CRC_LEN) {
247 sendlen = ETHER_MIN_LEN - ETHER_CRC_LEN;
248 bzero(sc->sc_txbufp, sendlen);
252 memcpy(sc->sc_txbufp, pkt, len);
254 rv = ub_dev_send(sc->sc_handle, sc->sc_txbufp, sendlen);
256 #if defined(NETIF_DEBUG)
257 printf("net_put: ub_send returned %d\n", rv);
268 net_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout)
270 struct netif *nif = desc->io_netif;
271 struct uboot_softc *sc = nif->nif_devdata;
275 #if defined(NETIF_DEBUG)
276 printf("net_get: pkt %p, len %d, timeout %d\n", pkt, len, timeout);
280 err = ub_dev_recv(sc->sc_handle, sc->sc_rxbuf, len, &rlen);
283 printf("net_get: ub_dev_recv() failed, error=%d\n",
288 } while ((rlen == -1 || rlen == 0) && (getsecs() - t < timeout));
290 #if defined(NETIF_DEBUG)
291 printf("net_get: received len %d (%x)\n", rlen, rlen);
295 memcpy(pkt, sc->sc_rxbuf, MIN(len, rlen));
297 #if defined(NETIF_DEBUG)
298 printf("net_get: len %x, rlen %x\n", len, rlen);
308 net_init(struct iodesc *desc, void *machdep_hint)
310 struct netif *nif = desc->io_netif;
311 struct uboot_softc *sc;
312 struct device_info *di;
315 sc = nif->nif_devdata = &uboot_softc;
317 if ((err = ub_dev_open(sc->sc_handle)) != 0)
318 panic("%s%d: initialisation failed with error %d\n",
319 nif->nif_driver->netif_bname, nif->nif_unit, err);
321 /* Get MAC address */
322 di = ub_dev_get(sc->sc_handle);
323 memcpy(desc->myea, di->di_net.hwaddr, 6);
324 if (memcmp (desc->myea, "\0\0\0\0\0\0", 6) == 0) {
325 panic("%s%d: empty ethernet address!",
326 nif->nif_driver->netif_bname, nif->nif_unit);
329 /* Attempt to get netboot params from the u-boot env. */
330 get_env_net_params();
331 if (myip.s_addr != 0)
334 #if defined(NETIF_DEBUG)
335 printf("network: %s%d attached to %s\n", nif->nif_driver->netif_bname,
336 nif->nif_unit, ether_sprintf(desc->myea));
339 /* Set correct alignment for TX packets */
340 sc->sc_txbufp = sc->sc_txbuf;
341 if ((unsigned long)sc->sc_txbufp % PKTALIGN)
342 sc->sc_txbufp += PKTALIGN -
343 (unsigned long)sc->sc_txbufp % PKTALIGN;
347 net_end(struct netif *nif)
349 struct uboot_softc *sc = nif->nif_devdata;
352 if ((err = ub_dev_close(sc->sc_handle)) != 0)
353 panic("%s%d: net_end failed with error %d\n",
354 nif->nif_driver->netif_bname, nif->nif_unit, err);