xref: /freebsd/stand/uboot/net.c (revision 3c4ba5f55438f7afd4f4b0b56f88f2bb505fd6a6)
1 /*-
2  * Copyright (c) 2000-2001 Benno Rice
3  * Copyright (c) 2007 Semihalf, Rafal Jaworowski <raj@semihalf.com>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 
35 #include <net/if.h>
36 #include <netinet/in.h>
37 #include <netinet/in_systm.h>
38 #include <netinet/if_ether.h>
39 #include <netinet/ip.h>
40 
41 #include <stand.h>
42 #include <net.h>
43 #include <netif.h>
44 
45 #include "api_public.h"
46 #include "glue.h"
47 #include "libuboot.h"
48 #include "dev_net.h"
49 
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 ssize_t	net_get(struct iodesc *, void **, time_t);
54 static ssize_t	net_put(struct iodesc *, void *, size_t);
55 static void	net_end(struct netif *);
56 
57 extern struct netif_stats net_stats[];
58 
59 struct netif_dif net_ifs[] = {
60 	/*	dif_unit	dif_nsel	dif_stats	dif_private */
61 	{	0,		1,		&net_stats[0],	0,	},
62 };
63 
64 struct netif_stats net_stats[nitems(net_ifs)];
65 
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 	nitems(net_ifs)		/* netif_nifs */
76 };
77 
78 struct uboot_softc {
79 	uint32_t	sc_pad;
80 	uint8_t		sc_rxbuf[ETHER_MAX_LEN];
81 	uint8_t		sc_txbuf[ETHER_MAX_LEN + PKTALIGN];
82 	uint8_t		*sc_txbufp;
83 	int		sc_handle;	/* device handle for ub_dev_xxx */
84 };
85 
86 static struct uboot_softc uboot_softc;
87 
88 /*
89  * get_env_net_params()
90  *
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.
95  *
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.
99  *
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.
104  */
105 static void
106 get_env_net_params()
107 {
108 	char *envstr;
109 	in_addr_t rootaddr, serveraddr;
110 
111 	/*
112 	 * Silently get out right away if we don't have rootpath, because none
113 	 * of the other info we obtain below is sufficient to boot without it.
114 	 *
115 	 * If we do have rootpath, copy it into the global var and also set
116 	 * dhcp.root-path in the env.  If we don't get all the other info from
117 	 * the u-boot env below, we will still try dhcp/bootp, but the server-
118 	 * provided path will not replace the user-provided value we set here.
119 	 */
120 	if ((envstr = ub_env_get("rootpath")) == NULL)
121 		return;
122 	strlcpy(rootpath, envstr, sizeof(rootpath));
123 	setenv("dhcp.root-path", rootpath, 0);
124 
125 	/*
126 	 * Our own IP address must be valid.  Silently get out if it's not set,
127 	 * but whine if it's there and we can't parse it.
128 	 */
129 	if ((envstr = ub_env_get("ipaddr")) == NULL)
130 		return;
131 	if ((myip.s_addr = inet_addr(envstr)) == INADDR_NONE) {
132 		printf("Could not parse ipaddr '%s'\n", envstr);
133 		return;
134 	}
135 
136 	/*
137 	 * Netmask is optional, default to the "natural" netmask for our IP, but
138 	 * whine if it was provided and we couldn't parse it.
139 	 */
140 	if ((envstr = ub_env_get("netmask")) != NULL &&
141 	    (netmask = inet_addr(envstr)) == INADDR_NONE) {
142 		printf("Could not parse netmask '%s'\n", envstr);
143 	}
144 	if (netmask == INADDR_NONE) {
145 		if (IN_CLASSA(myip.s_addr))
146 			netmask = IN_CLASSA_NET;
147 		else if (IN_CLASSB(myip.s_addr))
148 			netmask = IN_CLASSB_NET;
149 		else
150 			netmask = IN_CLASSC_NET;
151 	}
152 
153 	/*
154 	 * Get optional serverip before rootpath; the latter can override it.
155 	 * Whine only if it's present but can't be parsed.
156 	 */
157 	serveraddr = INADDR_NONE;
158 	if ((envstr = ub_env_get("serverip")) != NULL) {
159 		if ((serveraddr = inet_addr(envstr)) == INADDR_NONE)
160 			printf("Could not parse serverip '%s'\n", envstr);
161 	}
162 
163 	/*
164 	 * There must be a rootpath.  It may be ip:/path or it may be just the
165 	 * path in which case the ip needs to be in serverip.
166 	 */
167 	rootaddr = net_parse_rootpath();
168 	if (rootaddr == INADDR_NONE)
169 		rootaddr = serveraddr;
170 	if (rootaddr == INADDR_NONE) {
171 		printf("No server address for rootpath '%s'\n", envstr);
172 		return;
173 	}
174 	rootip.s_addr = rootaddr;
175 
176 	/*
177 	 * Gateway IP is optional unless rootip is on a different net in which
178 	 * case whine if it's missing or we can't parse it, and set rootip addr
179 	 * to zero, which signals to other network code that network params
180 	 * aren't set (so it will try dhcp, bootp, etc).
181 	 */
182 	envstr = ub_env_get("gatewayip");
183 	if (!SAMENET(myip, rootip, netmask)) {
184 		if (envstr == NULL)  {
185 			printf("Need gatewayip for a root server on a "
186 			    "different network.\n");
187 			rootip.s_addr = 0;
188 			return;
189 		}
190 		if ((gateip.s_addr = inet_addr(envstr)) == INADDR_NONE) {
191 			printf("Could not parse gatewayip '%s'\n", envstr);
192 			rootip.s_addr = 0;
193 			return;
194 		}
195 	}
196 }
197 
198 static int
199 net_match(struct netif *nif, void *machdep_hint)
200 {
201 	char **a = (char **)machdep_hint;
202 
203 	if (memcmp("net", *a, 3) == 0)
204 		return (1);
205 
206 	printf("net_match: could not match network device\n");
207 	return (0);
208 }
209 
210 static int
211 net_probe(struct netif *nif, void *machdep_hint)
212 {
213 	struct device_info *di;
214 	int i;
215 
216 	for (i = 0; i < devs_no; i++)
217 		if ((di = ub_dev_get(i)) != NULL)
218 			if (di->type == DEV_TYP_NET)
219 				break;
220 
221 	if (i == devs_no) {
222 		printf("net_probe: no network devices found, maybe not"
223 		    " enumerated yet..?\n");
224 		return (-1);
225 	}
226 
227 #if defined(NETIF_DEBUG)
228 	printf("net_probe: network device found: %d\n", i);
229 #endif
230 	uboot_softc.sc_handle = i;
231 
232 	return (0);
233 }
234 
235 static ssize_t
236 net_put(struct iodesc *desc, void *pkt, size_t len)
237 {
238 	struct netif *nif = desc->io_netif;
239 	struct uboot_softc *sc = nif->nif_devdata;
240 	size_t sendlen;
241 	ssize_t rv;
242 
243 #if defined(NETIF_DEBUG)
244 	struct ether_header *eh;
245 
246 	printf("net_put: desc %p, pkt %p, len %d\n", desc, pkt, len);
247 	eh = pkt;
248 	printf("dst: %s ", ether_sprintf(eh->ether_dhost));
249 	printf("src: %s ", ether_sprintf(eh->ether_shost));
250 	printf("type: 0x%x\n", eh->ether_type & 0xffff);
251 #endif
252 
253 	if (len < ETHER_MIN_LEN - ETHER_CRC_LEN) {
254 		sendlen = ETHER_MIN_LEN - ETHER_CRC_LEN;
255 		bzero(sc->sc_txbufp, sendlen);
256 	} else
257 		sendlen = len;
258 
259 	memcpy(sc->sc_txbufp, pkt, len);
260 
261 	rv = ub_dev_send(sc->sc_handle, sc->sc_txbufp, sendlen);
262 
263 #if defined(NETIF_DEBUG)
264 	printf("net_put: ub_send returned %d\n", rv);
265 #endif
266 	if (rv == 0)
267 		rv = len;
268 	else
269 		rv = -1;
270 
271 	return (rv);
272 }
273 
274 static ssize_t
275 net_get(struct iodesc *desc, void **pkt, time_t timeout)
276 {
277 	struct netif *nif = desc->io_netif;
278 	struct uboot_softc *sc = nif->nif_devdata;
279 	time_t t;
280 	int err, rlen;
281 	size_t len;
282 	char *buf;
283 
284 #if defined(NETIF_DEBUG)
285 	printf("net_get: pkt %p, timeout %d\n", pkt, timeout);
286 #endif
287 	t = getsecs();
288 	len = sizeof(sc->sc_rxbuf);
289 	do {
290 		err = ub_dev_recv(sc->sc_handle, sc->sc_rxbuf, len, &rlen);
291 
292 		if (err != 0) {
293 			printf("net_get: ub_dev_recv() failed, error=%d\n",
294 			    err);
295 			rlen = 0;
296 			break;
297 		}
298 	} while ((rlen == -1 || rlen == 0) && (getsecs() - t < timeout));
299 
300 #if defined(NETIF_DEBUG)
301 	printf("net_get: received len %d (%x)\n", rlen, rlen);
302 #endif
303 
304 	if (rlen > 0) {
305 		buf = malloc(rlen + ETHER_ALIGN);
306 		if (buf == NULL)
307 			return (-1);
308 		memcpy(buf + ETHER_ALIGN, sc->sc_rxbuf, rlen);
309 		*pkt = buf;
310 		return ((ssize_t)rlen);
311 	}
312 
313 	return (-1);
314 }
315 
316 static void
317 net_init(struct iodesc *desc, void *machdep_hint)
318 {
319 	struct netif *nif = desc->io_netif;
320 	struct uboot_softc *sc;
321 	struct device_info *di;
322 	int err;
323 
324 	sc = nif->nif_devdata = &uboot_softc;
325 
326 	if ((err = ub_dev_open(sc->sc_handle)) != 0)
327 		panic("%s%d: initialisation failed with error %d",
328 		    nif->nif_driver->netif_bname, nif->nif_unit, err);
329 
330 	/* Get MAC address */
331 	di = ub_dev_get(sc->sc_handle);
332 	memcpy(desc->myea, di->di_net.hwaddr, 6);
333 	if (memcmp (desc->myea, "\0\0\0\0\0\0", 6) == 0) {
334 		panic("%s%d: empty ethernet address!",
335 		    nif->nif_driver->netif_bname, nif->nif_unit);
336 	}
337 
338 	/* Attempt to get netboot params from the u-boot env. */
339 	get_env_net_params();
340 	if (myip.s_addr != 0)
341 		desc->myip = myip;
342 
343 #if defined(NETIF_DEBUG)
344 	printf("network: %s%d attached to %s\n", nif->nif_driver->netif_bname,
345 	    nif->nif_unit, ether_sprintf(desc->myea));
346 #endif
347 
348 	/* Set correct alignment for TX packets */
349 	sc->sc_txbufp = sc->sc_txbuf;
350 	if ((unsigned long)sc->sc_txbufp % PKTALIGN)
351 		sc->sc_txbufp += PKTALIGN -
352 		    (unsigned long)sc->sc_txbufp % PKTALIGN;
353 }
354 
355 static void
356 net_end(struct netif *nif)
357 {
358 	struct uboot_softc *sc = nif->nif_devdata;
359 	int err;
360 
361 	if ((err = ub_dev_close(sc->sc_handle)) != 0)
362 		panic("%s%d: net_end failed with error %d",
363 		    nif->nif_driver->netif_bname, nif->nif_unit, err);
364 }
365